What is the difference between kernel stack and user stack?

32,252

Solution 1

There is (basically) one "kernel stack" per CPU. There is one "user stack" for each process, though each thread has its own stack, including both user and kernel threads.

How "trapping changes the stack" is actually fairly simple.

The CPU changes processes or "modes", as a result of an interrupt. The interrupt can occur for many different reasons - a fault occurs, (like an error, or page-fault), or a physical hardware interrupt (like from a device) - or a timer interrupt (which occurs for example when a process has used all of it's allotted CPU time").

Either way - when this interrupt is called, the CPU registers are saved on the stack - all the registers - including the stack pointer itself.

Typically then a "scheduler" would be called. The scheduler then chooses another process to be run - restoring all of its saved registers including the stack pointer, and continues execution from where it left off (stored in the return-address pointer).

This is called a "Context Switch".

I'm simplifying a few things - like how memory management context are saved and restored, but that's the idea. It's just saving and restoring registers in response to an interrupt - including the "stack pointer" register.

So each program or thread has it's own ("user mode") stack (i.e. a multi-threaded program would have multiple stacks) - and the context switch switches between these.

More specially, "Kernel Mode" stacks exist for when the machine (or a specific CPU) is running in the kernel. The exact handing is a OS specific - for example Linux will have one interrupt (kernel) stack per CPU (which would be generally used for interrupts, including page-faults and syscalls, which inherently includes nearly everything - like device drivers and the scheduler). Like user-space threads, Linux kernel also has separate stacks for kernel threads. (Windows Kernel does something different).

Solution 2

There are 2 stacks because there are 2 CPU execution contexts. The user mode stack will cater to your program with respect to creating stack frames for functions, local variables, return addresses etc. When the CPU switches context to kernel mode, for instance during system call execution, it needs access to kernel memory and data structures and so switches to using it's kernel stack. And yes, Unix I believe uses a per process kernel stack.

Solution 3

I am learning OS in university, and our project is based on OS/161 built by Harvard. So my answer is all based on this OS.

In OS/161, every thread has 2 stacks - one for user/application program, one for kernel program.

1. What is the need of using two different stacks in same program?

Say we only use stack in application mode. Since the memory space is shared by multiple threads, if some other thread accidently overwrite the address used by kernel, then kernel might be crashed, which leads to a very vulnerable OS.

2. How does trap change the current stack of program from user stack to kernel stack?

in OS/161, trap is used to transfer from an application program to kernel.There are three mechanisms that could invoke trap: System calls, Exceptions, and Interrupts. The trap frame in kernel stack is used to save current thread context.

Following is the detailed process(from lecture note of UWaterloo CS350):

  • When one of above mechanism occurs, the hardware switches the CPU into privileged mode and transfers control to a predefined location, at which a kernel handler should be located.

  • The kernel handler creates a trap frame and uses it to saves the application thread context so that the handler code can be executed on the CPU.

  • Just before the kernel handler finishes executing, it restores the application thread context from the trap frame, before returning control to the application.

3. How does it come back to user stack after completing system call?

The process above explains clearly on this question as well.

Solution 4

One of the reasons for having a separate kernel stack is that the kernel needs a place to store information where user-mode code can't touch it. That prevents user-mode code running in a different thread/process from accidentally or maliciously affecting execution of the kernel.

Solution 5

what is the need of using two different stacks in same program

I've never heard of both a kernel and user stack in terms of a single process, though it may be extremely common. It's discussed here.

The kernel stack must be isolated from the user mode stack. Otherwise, user mode code could corrupt the kernel stack, causing a kernel crash.

how does trap changes the current stack of program from user stack to kernel stack

You may want to look for something like the Intel Software Developer's Manuals.

does each process has kernel and user stack

I assume this varies with operating system design, though perhaps it's fairly universal. The links I provided above indicate that Linux uses two (or more) stacks per process. I haven't heard of Windows using a per-process kernel-mode stack.

Share:
32,252
user609306
Author by

user609306

Updated on October 24, 2020

Comments

  • user609306
    user609306 over 3 years

    What is the need of using two different stacks in same program? How does trap change the current stack of program from user stack to kernel stack? How does it come back to user stack after completing system call?

    Does every process have a kernel and user stack?