Where Are the Stacks for the Other Threads Located in a Process Virtual Address Space

How are virtual addresses corresponding to kernel stack mapped?

Note: This is the OS agnostic answer. Details do vary slightly with OS in question (e.g. Darwin and continuations..), and possibly with architectural (ARMv8, x86, etc) implementations.

When a process performs a system call, the user mode state (registers) is saved, including the user mode stack pointer. At that point, a kernel mode stack pointer is loaded, which is usually maintained somewhere in the thread control block.

You are correct in saying that there is only one kernel space. What follows is, that (in theory) one thread in kernel space could easily see and/or tamper with any others in kernel space (just like same process threads can "see" each other in user space) This, however, is (almost always) in theory only, since the kernel code presumably respects memory boundaries (as is assumed user mode does, with thread local storage, etc). That said, "almost always", because if the kernel code can be exploited, then all of kernel memory will be laid bare to the exploiter, and potentially read and/or compromised.

multi-threading: does stack may overlap on physical memory

  1. When we say the same process share the same physical memory space and the same virtual memory addresses, it actually means that the same process share the same physical memory space and the same virtual memory addresses, different processes have different physical memory space, saying the process A can not modify/access the process B’s physical memory space except shared memory. This protected separate memory sections for each process is achieved by the memory management unit (MMU).

  2. When we say threads share the same physical memory. It means that all threads have the same address space, which means that they also share the same global variables. And every thread can access every memory address within the process' address space, the thread A can read, write, or even completely wipe out the thread B's stack.

  3. Back to the question: How do threads share the same physical memory? For LinuxThreads, it uses (CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND) parameters to call clone () to create "threads", which means shared memory, shared file system access count, Shared file descriptor table, and shared signal processing. http://man7.org/linux/man-pages/man2/clone.2.html
    The difference between threads is the program counter (keeping track of which instruction to execute next), registers(holding its current working variables), thread stack(containing the execution history).



Related Topics



Leave a reply



Submit