Page 2 of 2
Re: User mode syscalls problem
Posted: Tue Oct 04, 2022 1:18 pm
by Octocontrabass
WinExperements wrote:Your example can save and restore tasks in ring 3?
Yes.
WinExperements wrote:This must called by the interrupt handler or the scheduler method?
You can call it anywhere you're able to call an ordinary C function. The only limitation is that you must update your TSS and CR3 before you call it.
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 10:35 am
by WinExperements
Hello! I have problem with user space and brendan's multitasking. After second switch to user space process, instead of jumping to saved EIP it jumps to the function caller(in my case to scheduling function). Why?
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 10:41 am
by devc1
How do you build your stack frame ?
How do you save thread's EIP ?
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 10:54 am
by WinExperements
devc1 wrote:How do you build your stack frame ?
How do you save thread's EIP ?
Creating and saving the thread state like in Brendan's tutorial, but i changed the switching method:
Before the restoring if the task is in user space, it's jumps to it, and restors stack.
What i am doing wrong in the switching function?
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 10:59 am
by devc1
Wait, so you are in an IRQ handler which is in kernel mode and you jump to user mode, you meant far jump right ?
However just for your info (to not face a problem in the futur), jmp/far jmp/call... instructions change EFLAGS so you cannot use them in an IRQ handler.
You should iret to restore EFLAGS.
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 11:20 am
by WinExperements
devc1 wrote:Wait, so you are in an IRQ handler which is in kernel mode and you jump to user mode, you meant far jump right ?
However just for your info (to not face a problem in the futur), jmp/far jmp/call... instructions change EFLAGS so you cannot use them in an IRQ handler.
You should iret to restore EFLAGS.
Okay, how i can correctly add user space processes support using Brendan's tutorial? Can you give example of it?
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 2:30 pm
by devc1
I didn't see brendans tutorial but I predict that you already have a scheduler which will select the next thread to run.
You should save/restore all the registers and segments, if you have paging this gets more complicated, you will have to map your kernel in the user's address space.
I never made a 32 Bit OS, but I will see if this helps.
This is a simple Interrupt Handler that saves thread state, calls the Scheduler Function and context switches to the next thread.
If it works, then you can optimize this code with lots of ideas.
Code: Select all
; The Stack is formatted as below:
; EIP, CS, EFLAGS, ESP, SS
SwitchTaskISR:
push edi
mov edi, [CurrentThread]
; Save EDI
pop dword [CurrentThread + EDI]
; Save EIP, CS, EFLAGS, ESP, SS, DS, FS, GS, ES
pop dword [edi + EIP]
pop dword [edi + CS]
pop dword [edi + EFLAGS]
pop dword [edi + ESP]
pop dword [edi + SS]
mov [edi + DS], ds
mov [edi + FS], fs
mov [edi + GS], gs
mov [edi + ES], es
; Save the registers
mov [edi + EAX], eax
mov [edi + EBX], ebx
mov [edi + ECX], ecx
mov [edi + EDX], edx
mov [edi + ESI], esi
mov [edi + EBP], ebp
call Schedule ; Current thread in EAX
mov [CurrentThread], eax
; Build the Stack Frame (These registers will be restored when you do an iret
push dword [eax + SS]
push dword [eax + ESP]
push dword [eax + EFLAGS]
push dword [eax + CS]
push dword [eax + EIP]
; If you use paging (you can also do a cmp to see if they have the same page tables)
mov ebx, [eax + CR3]
mov cr3, ebx
; Restore the registers of the task
.....
; Context Switch
iretd
As far as I know, in 32 Bit you can use the TSS and save/load your task with one instruction.
Re: User mode syscalls problem
Posted: Mon Oct 10, 2022 3:43 pm
by Octocontrabass
WinExperements wrote:Hello! I have problem with user space and brendan's multitasking. After second switch to user space process, instead of jumping to saved EIP it jumps to the function caller(in my case to scheduling function). Why?
That's how it's supposed to work. From the caller's perspective, it's a function that does nothing for a while and then returns.