What's the use of having a kernel part in the virtual memory space of Linux processes?

281

Solution 1

  1. The kernel mapping exists primarily for the kernel’s purposes, not user processes’. From the CPU’s perspective, any physical memory address which isn’t mapped as a linear address might as well not exist. But the CPU does need to be able to call into the kernel: to service interrupts, to handle exceptions... It also needs to be able to call into the kernel when a user process issues a system call (there are various ways this can happen so I won’t go into details). On most if not all architectures, this happens without the opportunity to switch page tables — see for example SYSENTER. So at minimum, entry points into the kernel have to be mapped into the current address space at all times.

  2. Kernel allocations are dynamic, but the address space isn’t. On 32-bit x86, various splits are available, such as the 3/1 GiB split shown in your diagram; on 64-bit x86, the top half of the address space is reserved for the kernel (see the memory map in the kernel documentation). That split can’t move. (Note that libraries are loaded into user space. Kernel modules are loaded into kernel space, but again that only changes the allocations, not the address space split.)

  3. In user mode, there is a single mapping for the kernel, shared across all processes. When a kernel-side page mapping changes, that change is reflected everywhere.

    When KPTI is enabled, the kernel has its own private mappings, which aren’t exposed when running user-space code; so with KPTI there are two mappings, and changes to the kernel-private one won’t be visible to user-space (which is the whole point of KPTI).

    The kernel memory map always maps all the kernel (in kernel mode when running KPTI), but it’s not necessarily one-to-one — on 64-bit x86 for example it includes a full map of physical memory, so all kernel physical addresses are mapped at least twice.

Solution 2

Another small tip for more common understanding by definition of kernel it should be active all the time to perform administrative services and to provide services to user applications. This activeness is practically achieved by logically binding the kernel in every process.

This makes sense also, consider multiple processes are running in a single processor machine environment, and also consider that the process structure does not contains the kernel mapping. As there is only one processor, once a process is scheduled for running, it definitely means that the kernel is not active, as the CPU is occupied by the process, furthermore we have assumed that there is no mapping of kernel available in the process. Now the logical question is how would the kernel perform its services than, the solution is to map kernel in every process.

That's how I think every human being has a mapping to the creator.

Share:
281

Related videos on Youtube

Luiza Guerra
Author by

Luiza Guerra

Updated on September 18, 2022

Comments

  • Luiza Guerra
    Luiza Guerra over 1 year

    I am new to PHP and need to write automated tests to test a WSDL with phpunit. I did not find much practical material with baby steps to help me. Someone to give me some help getting started?

    • JdeBP
      JdeBP over 5 years
      Tim also used that diagram at unix.stackexchange.com/questions/466389 , where I pointed out that it was incomplete (in the application-mode part), and at unix.stackexchange.com/questions/466443 .
    • John P
      John P over 5 years
      @JdeBP also what about Dynamic libraries? in some pictures there is a dynamic library between stack and heap! i.stack.imgur.com/Dsv4b.jpg
    • Stephen Kitt
      Stephen Kitt over 5 years
      What about libraries? What specifically do you want to know about them?
    • John P
      John P over 5 years
      @StephenKitt i want to know if there is a dynamic library part between heap and stack in linux or not
    • Stephen Kitt
      Stephen Kitt over 5 years
      That doesn’t have much to do with the kernel, but yes, on 32-bit x86 there’s an area for dynamic libraries between the heap and the stack. It’s really the mmap area; dynamic libraries are mmapped in.
    • John P
      John P over 5 years
      @StephenKitt so basically the picture in my question is wrong too, is there any complete and reliable picture for linux process virtual space?
    • Stephen Kitt
      Stephen Kitt over 5 years
      This question has a different diagram (contrary to JdeBP’s comment above) which is somewhat more accurate for the user-space side of things. There is no doubt a complete diagram somewhere on the Internet but I don’t have a link handy.
  • John P
    John P over 5 years
    Thanks for answer, but i didn't get the last part, what do you mean kernel physical addresses are mapped twice? i just want to understand what is inside this kernel part of virtual memory, like that 1gb in 32 bit, i mean why would that be the entire kernel anyways? shouldn't only the O.S services that processes might need be there and not the entire kernel?
  • Stephen Kitt
    Stephen Kitt over 5 years
    On 64-bit x86, the kernel memory map includes a direct mapping of all physical memory, so everything in memory appears there; it also includes separate mappings for the kernel, modules etc., so the physical addresses containing the kernel appear in at least two different mappings (the direct physical mapping and the various kernel mappings).
  • Stephen Kitt
    Stephen Kitt over 5 years
    As to why the entire kernel is mapped, when KPTI isn’t enabled, it’s mainly because it’s simpler that way. The kernel consists pretty much entirely of OS services that processes might need, either because they implement system calls and their supporting infrastructure (file systems etc.), or because they provide hardware support (device drivers including interrupt handlers etc.). The kernel can’t know in advance what a process is going to use.