OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 9:26 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: User mode syscalls problem
PostPosted: Tue Oct 04, 2022 1:18 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 10:35 am 
Offline
Member
Member

Joined: Thu Jul 14, 2022 9:45 am
Posts: 91
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?


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 10:41 am 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
How do you build your stack frame ?
How do you save thread's EIP ?


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 10:54 am 
Offline
Member
Member

Joined: Thu Jul 14, 2022 9:45 am
Posts: 91
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?


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 10:59 am 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
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.


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 11:20 am 
Offline
Member
Member

Joined: Thu Jul 14, 2022 9:45 am
Posts: 91
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?


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 2:30 pm 
Offline
Member
Member

Joined: Fri Feb 11, 2022 4:55 am
Posts: 435
Location: behind the keyboard
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:
; 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.


Top
 Profile  
 
 Post subject: Re: User mode syscalls problem
PostPosted: Mon Oct 10, 2022 3:43 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 23 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 50 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group