3
votes

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)

1
An entire book is needed to answer. Read Operating Systems: Three Easy Pieces -freely downloadable. Your question is too broad. Your vision of virtual address space is too naive: try cat /proc/$$/maps in a terminal, see proc(5)Basile Starynkevitch

1 Answers

2
votes

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.