0
votes

The virtual memory in Linux can be split into two parts: user address space and kernel address space.

The task works in either kernel mode or user mode according to the privilege level.

But considering i386 arch, what's the relationship between kernel mode and kernel address space, user mode and user address space?

1
This is too broad: a lot of Linux versions run on a lot of architectures and they may not all use the same design. Details can vary such as whether the processor does privilege level access checking on page table entries or whether user mode and kernel mode have distinct page tables (with entries for kernel memory regions being missing from the page tables active for user-mode threads) - Ben Voigt
@Ben Voigt, thanks and I just updated question for the i386 arch. Based on your comment, does that mean there is not forced relationship between kernel mode and kernel address space? - Jason
You will find some embedded systems with "memory protection unit" instead of "memory management unit" that do not support virtual memory and simply make some areas of the system inaccessible to unprivileged code. But for a modern OS and accompanying memory virtualization hardware, the relationship is 100% controlled by the OS software. It can map pages of kernel memory into a process (e.g. mmap on /dev/kmem). It can map a page of one process into another (shared memory). And even the division of address space is just a convention, to reduce fragmentation and make it easy to recognize. - Ben Voigt
@Ben Voigt, Thanks. I just figured out that the user mode and kernel mode are the different states with according to "cs" register to have different privileges to do with operating system resources. The mode is nothing to do with the space-split convention of virtual memory. If above conclusion is correct, which means when the process trapped into kernel mode, it may access all the virtual space, no matter user space or kernel space. And so does user mode too? Or the user mode can not access to kernel space? This is my confusion point. - Jason
@Ben Voigt, technically, when process in user model it must only access to user address space, since the DPL of segment descriptor field should be 0 when this segment is in kernel address space, right? If yes, the RPL of process in user mode is 3 which means it can't access to this segment with DPL set to 0. However, why process trapped into kernel mode does not access to user space directly, but use the highmem as a map to user space? what's the point? - Jason

1 Answers

1
votes

According to the comments and personal research, generally there is not a forced relationship between kernel mode and kernel address space, user mode and user address space. For modern OS(Linux), the relationship is 100% controlled by OS software which can map user address space for kernel mode can access to, typically with highmem.
Basically, the division of virtual memory space is a convention reduces fragmentation and make it easy to recognize when programming. For the 86x86 arch, there does have a "relationship" based on the following facts.

  1. When the task is trapped to kernel mode, the CPL within cs register is set to 0 which means highest privilege. Technically, it can access to all the virtual memory space. A kind of map, normally highmen, is used to make kernel safely access to user address space.
  2. When the task is in user mode, the CPL within cs register is set to 3 which means lowest privilege. It can only access to the segment whose DPL field is 3. However, the segments in kernel address space all filled with the 0 DPL which means user mode task does not have the privilege to access. The two points don't consider the RPL, there should only be a condition that DPL >= max(RPL,CPL) the segment can be accessed.