OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Process switching between kernel and user processes
PostPosted: Sun Mar 05, 2023 2:22 pm 
Offline

Joined: Sun Mar 05, 2023 1:29 pm
Posts: 3
(Hello! First post!)

I'm trying to implement preemptive multitasking for the first time, but I've hit a bit of a snag with how x86 does interrupt handling. My OS has a kernel process in addition to all the user processes, which the scheduler treats exactly the same as any other task except that it happens to be in ring 0. The problem, though, is that interrupts in x86 only push SS:ESP and load the new stack from the TSS if there's a privilege change from user to kernel, and iret only loads SS:ESP from the stack when going from kernel to user. This means there's a misalignment when switching between the kernel process and a user process or vice versa. I've come up with a partial solution that involves manually switching stacks, copying between them, etc., but it has to handle four different cases (kernel to user, user to kernel, user to user, and kernel to kernel) so it's a bit convoluted and I haven't gotten it to work yet, and I can't help but think there's probably an easier way to do this. (Or maybe there isn't and I just have to suffer through getting this to work...)

I've searched around a little bit but haven't found anything on my own about how to handle this, but I'm sure it's something anyone else who's also made kernel processes has had to work around, so I'd like to hear what the recommended way of dealing with this is.


Top
 Profile  
 
 Post subject: Re: Process switching between kernel and user processes
PostPosted: Mon Apr 03, 2023 5:45 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
It sounds like you don't have enough stacks. The typical kernel design has one kernel stack per thread. Any threads that run in ring 3 will have an additional stack in ring 3, but that's irrelevant to your multitasking code - all you need to do to switch tasks is switch kernel stacks.

This wiki article does a pretty good job of explaining things, though keep in mind the example code is only an example - you shouldn't blindly trust it to be correct or ideal for your OS. (And, of course, you might not want the typical kernel design. But in that case, you should still learn how the usual design works before you try to come up with something else.)


Top
 Profile  
 
 Post subject: Re: Process switching between kernel and user processes
PostPosted: Mon Apr 03, 2023 9:26 pm 
Offline

Joined: Sun Mar 05, 2023 1:29 pm
Posts: 3
I posted this quite a while ago and had to wait for it to be approved since it was my first post, but fortunately this isn't still an issue I'm working on! Originally I was doing a really basic microkernel-esque design that only had one kernel stack, as most system calls would just be quick non-interruptible message passing. (Probably should have clarified this design decision in the original post.) I did eventually write some more clever assembly code in the interrupt handler that solved the original problem I was having and worked fine, though since then I have actually switched to the kernel stack per task design, so this isn't relevant anymore.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 69 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