Linux – Understanding Kernel Stack and Memory Layout of a Program

linuxprocessvirtual-memory

From The Linux Programming Interface:

enter image description here

  1. Where is the kernel stack (mentioned in the quote below) in the above diagram?

    Is it the top part "Kernel (mapped into process virtual memory, but no accessilto program)" in the above diagram?

    the term user stack is used to distinguish the stack we describe here
    from the kernel stack. The kernel stack is a per-process memory region maintained
    in kernel memory that is used as the stack for execution of the functions called
    internally during the execution of a system call. (The kernel can’t employ the user
    stack for this purpose since it resides in unprotected user memory.)

  2. Where are "Frames for C run-time startup functions" and "Frame for main()" (mentioned from the diagram below) in the above diagram?

    Is "argv, environ" in the above diagram "Frames for C run-time startup functions", "Frame for main()", or part of either?

    enter image description here

  3. What is the lowest segment between 0x00000000 and 0x08048000 used for?

Thanks.

Best Answer

  1. There is not a kernel stack. For each thread, there is a memory region that is used as stack space when the process makes a system call. There are also separate "interrupt stacks", one per CPU, which are used by the interrupt handler. These memory areas reside in the kernel address space (above 0xc0000000 in your figure.

  2. The stack frames (C runtime frames, the frame for main, etc.) are part of the stack. The process arguments (argv) and the environment are separate areas, and are not part of the stack.

  3. The area between 0x0 and 0x08048000 (about 128 MB) is not used for anything. Originally, the i386 System V ABI reserved this area for the stack, but Linux does things differently. Leaving the area unused does not waste RAM, only address space, because the area is not mapped. Note that this information is almost totally obsolete by now, since it describes how things are done on the 32-bit x86 architecture. 32-bit only x86 machines are hard to find today, and distributions are phasing out support for them.

Related Question