1

Recently, I started learning about linux and there is a series of question, I am going to ask.

  1. While reading, I encountered a term 'kernel stack' and I am not able to understand why does the linux kernel use kernel stack for each process ?

  2. when a process is executing, why does it happen that the esp register has address of top of
    the kernel stack of the process and why not the address of top of the user mode stack of the respective process ?

1
  • 1
    Are you sure about (2)? – Stephen Kitt Aug 7 '20 at 13:57
2

When a process enters kernel mode as the result of a system call, the stack is switched from the user mode stack to the kernel stack. This is done to preserve the integrity of the kernel, otherwise the process (another thread, for example) could do some nasty things to the stack.

A separate kernel stack is needed for each process to save the state of the process. The state needs to be saved in case a task switch is performed, i.e. the current process is put to sleep, and some other process scheduled to run. When this happens, the register values (including the stack pointer register) are restored so the process to be run can continue from the exact point it was when it was suspended.

1
  • 1
    In addition to this, the kernel stack is used to store each thread’s thread_info struct. Regarding your first paragraph, threads aren’t even required to have a valid user mode stack, so the kernel wouldn’t be able to rely on one anyway. – Stephen Kitt Aug 7 '20 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.