3

I know kernel space is the memory section where the core of operating system executes and provides its services, and the user space is one where user programs run. Also I know that a process has its own stack, heap, data, and text section in its address space. But I'm confused with the concepts of user stack and kernel stack. My question is :

  1. Does the process stack I mentioned before consist of user stack and kernel stack?
  2. Is kernel stack part of the kernel space?
  3. Are the two stacks separated in a processes' virtual memory address?
  4. In the code segment: void main(){user_mode_call(); system_call()} do the stack frames of the two calls reside in user stack and kernel stack respectively?


Thanks for your time, any related literature and links would also be helpful!
(My questions may be naive but I'll keep updating them as soon as I know how put them in a more professional way)

Mengfan Ma
  • 351
  • 2
  • 13
  • 2
    An entire book is needed to answer. Read [*Operating Systems: Three Easy Pieces*](http://pages.cs.wisc.edu/~remzi/OSTEP/) -freely downloadable. Your question is too broad. Your vision of [virtual address space](https://en.wikipedia.org/wiki/Virtual_address_space) is too naive: try `cat /proc/$$/maps` in a terminal, see [proc(5)](http://man7.org/linux/man-pages/man5/proc.5.html) – Basile Starynkevitch Dec 31 '17 at 08:06

1 Answers1

2

The stack structure is usually specified by the processor. Each process usually has one stack per processor mode (user, kernel + any others used by the processor) per process and one interrupt stack per processor (another kernel stack).

Does the process stack I mentioned before consist of user stack and kernel stack?

No. The kernel stack has to be protected from user-mode access.

Is kernel stack part of the kernel space?

It might be or it could be protected memory in the user space.

Are the two stacks separated in a processes' virtual memory address?

yes.

In the code segment: void main(){user_mode_call(); system_call()} do the stack frames of the two calls reside in user stack and kernel stack respectively?

"Code segment" is pedagogical construct. The stack frames of both are in the user stack. System calls invoke a wrapper function that sets up register values then causes an exception that switches the processor to kernel mode. At that point, most processors change the default stack to the kernel mode stack. Parameters have to be passed to system calls because the user stack is not directly accessible through the SP register in kernel mode.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • So what does the stack we usually refer to mean? Is it the user stack? – Mengfan Ma Jan 03 '18 at 09:21
  • In the first paragraph, “per process“ should probably be “per thread“. – Felix Dombek Jan 03 '18 at 12:49
  • The "stack" is usually the user stack. And you could say "per thread" or from the processor point of view it's still per process with multiple processes sharing the same address space. Processor documentation talks in terms of processes rather than threads. – user3344003 Jan 04 '18 at 02:51
  • @user3344003 According to the answer for "Is kernel stack part of the kernel space?", could you please shed more light on it? In what kind of situation kernel stack is not in kernel space? Thanks, – codexplorer Aug 10 '21 at 04:19