Microsoft Word – User-KernelTransitionsWithStacks
CS444 User‐Kernel Transitions, showing stacks (uniprocessor case)
System call from user code (single‐threaded process)
User space Kernel space
User execution: IF=1, CPU mode = user Kernel execution: IF=1 (mostly), CPU mode = kernel
___________________________________
____________________________ IDT syscall code process/thread stacks
User code user stack (in kernel code) (in kernel data)
C code C lib read
with call in assembler
to read
• While the syscall executes in the kernel, it uses (“legitimately”) the current
process’s kernel stack.
• A single-threaded process has one kernel stack, for its one thread, so we can say it’s
the process’s kernel stack. A multi-threaded process has multiple user thread stacks
and multiple kernel stacks.
Interrupt in user code (single‐threaded process, no interrupt stack in use by kernel)
User space Kernel space
User execution: IF=1, CPU mode = user Kernel execution: IF=0 to start, CPU mode = kernel
___________________________________
____________________________ IDT interrupt handler process/thread stacks
User code user stack (in kernel code) (in kernel data)
An interrupt can happen between any two instructions
• While the interrupt handler executes, it uses (“borrows”) the current process’s kernel
stack. I’ve drawn it smaller to indicate that it has just started growing from being
empty during user execution.
This process’s
kernel stack
This process’s
kernel stack
System call from user code (multi‐threaded process)
User space Kernel space
User execution: IF=1, CPU mode = user Kernel execution: IF=1, mode = kernel after trap
cycle
___________________________________
____________________________ IDT syscall code thread stacks
C code C lib read user stacks
with call in as for threads A, B
to read
• While the syscall executes in the kernel, it uses (“legitimately”) the current
thread’s kernel stack.
• The other thread is preempted or blocked, so also has a sizable kernel stack
Interrupt in user code (multi‐threaded process, 2 threads, one interrupted)
User space Kernel space
User execution: IF=1, CPU mode = user Kernel execution: IF=0, CPU mode = kernel
___________________________________
____________________________ IDT interrupt handler thread stacks
User code user stacks (in kernel code) (in kernel data)
• While the interrupt handler executes, it uses (“borrows”) the current thread’s kernel
stack.
• Here thread A is interrupted from user execution, so has a small kernel stack
• Thread B has been preempted or blocked, so has a bigger kernel stack
Figure 2-9, pg. 99 Web server code (a)Dispatcher (b) Worker
Thread A’s
kernel stack
Thread B’s
kernel stack
Thread A’s
kernel stack
Thread B’s
kernel stack