Hello
Maybe my question could sound a little strange, but even if I'm not a newby I have a major doubt about this issue. All the things below refer to an x86 32bit processor.
Normally on the beginning of a Interrupt, IRQ or Syscall I save the machine state for the running process and at the end I restore the machine state of the new one. More precisely, on a ring3->ring0 transaction I push the registers on the kernel stack; if I'm going to switch task I memcpy() them in the process structure and at the end of the scheduler I memcpy() the new process structure on the stack and return to ring3, otherwise (in case that the process isn't changed) I simply re-pop the old values.
Here my question comes: if I have to switch during a syscall (let's say, for example, a read() have to wait for the disk), this method simply doesn't work, because I have to save (push?) somewhere another machine state for that process. I really don't manage to figure out how can I do that!
I know that's a stupid question, that I'm missing an easy point (probably I just lost my mind after too much OS development), but I can't find an elegant solution to this problem.
Any help is appreciated.. Thank you!
Context switch in syscall?
- Love4Boobies
- Member

- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Context switch in syscall?
When using kernel threads (as you are), it's the blocking system call that picks the next thread on the ready queue and switches to it. When the I/O operation is finished, you get an IRQ and you put the callee on the ready queue again.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Context switch in syscall?
I'm not using kernel threads.. I simply need to switch to another task if the current system call has to wait. In that case I'll have one state for the original process (saved at the beginning) and one for the interrupted syscall, that has to go somewhere... but it's pushed nowhere as the switch is not IRQ driven.
- Love4Boobies
- Member

- Posts: 2111
- Joined: Fri Mar 07, 2008 5:36 pm
- Location: Bucharest, Romania
Re: Context switch in syscall?
You don't have to do that. The syscall can finish and when the IRQ arrives, you can have a handler that does the rest.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
[ Project UDI ]
Re: Context switch in syscall?
I really didn't get that oneLove4Boobies wrote:You don't have to do that. The syscall can finish and when the IRQ arrives, you can have a handler that does the rest.
Anyway, I'm starting to understand how to do this only by pushing/popping on different stack pointers - hopefully writing some code will help.
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Context switch in syscall?
I don't really see a point in having to store the processor state twice - I mean, if you already store the state to the stack, then you can simply add another state to the stack in the scheduler code.
like:
--stack--
[state frame (system call)]
[function 1]
[function 2]
[function 3]
[state frame (scheduler)]
[scheduler temporaries]
--top of stack-- <- pointer stored by scheduler
Now you can switch threads, and when the scheduler comes back to your thread, it pops the stackframe and gets you back to where the system call left off.
like:
--stack--
[state frame (system call)]
[function 1]
[function 2]
[function 3]
[state frame (scheduler)]
[scheduler temporaries]
--top of stack-- <- pointer stored by scheduler
Now you can switch threads, and when the scheduler comes back to your thread, it pops the stackframe and gets you back to where the system call left off.
- Colonel Kernel
- Member

- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: Context switch in syscall?
I think the OP has only one kernel stack, not one for each thread. With only one kernel stack, your suggestion gets pretty tricky.Combuster wrote:I don't really see a point in having to store the processor state twice - I mean, if you already store the state to the stack, then you can simply add another state to the stack in the scheduler code.
like:
--stack--
[state frame (system call)]
[function 1]
[function 2]
[function 3]
[state frame (scheduler)]
[scheduler temporaries]
--top of stack-- <- pointer stored by scheduler
Now you can switch threads, and when the scheduler comes back to your thread, it pops the stackframe and gets you back to where the system call left off.
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager
- Combuster
- Member

- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Context switch in syscall?
True, but with a single kernel stack, it will always be tricky to have a (voluntarily) preemptible kernel anyway, because you'll need at least some way to go back to your system call with enough information to continue/restart it. And being able to abort and restart a system call puts a large burden on the programmer.
The question is: what's better? Getting a complex syscall handling scheme working and maintaining it, or implementing multiple kernel stacks and maintaining it. I can answer that for my case, but not the general case.
The question is: what's better? Getting a complex syscall handling scheme working and maintaining it, or implementing multiple kernel stacks and maintaining it. I can answer that for my case, but not the general case.
- Colonel Kernel
- Member

- Posts: 1437
- Joined: Tue Oct 17, 2006 6:06 pm
- Location: Vancouver, BC, Canada
- Contact:
Re: Context switch in syscall?
Which programmer, the kernel programmer or user app programmer?Combuster wrote:True, but with a single kernel stack, it will always be tricky to have a (voluntarily) preemptible kernel anyway, because you'll need at least some way to go back to your system call with enough information to continue/restart it. And being able to abort and restart a system call puts a large burden on the programmer.
QNX actually does implement pre-emption with only a single kernel stack per CPU. It's neat how they do it. Basically each system call is written almost like a transaction. All changes to system data structures are batched up in one place. At any point before the system call "commits" by writing its changes, it can be preempted. If it is, it basically tweaks the return address in the user thread's context structure so that it will simply enter the system call again, with the same arguments. At that point, because no global state has changed, the kernel can just abandon the current contents of the kernel stack and let it get overwritten by the preempting code.
I think this technique is pretty neat -- it's the first time I heard of it. You can read more about it on the QNX wiki (and in the source code of course).
Top three reasons why my OS project died:
- Too much overtime at work
- Got married
- My brain got stuck in an infinite loop while trying to design the memory manager