0

I understand that with Linux, RAM gets divided into kernel space and user space. Kernel space is reserved for running the kernel, and user space applications should not have direct access to the memory there.

How exactly does this memory get divided into kernel space and user space? Is it defined in the linker script when building the kernel?

Engineer999
  • 1,151
  • It is not simply a case of kernel and user space. Every user-level process is also protected from every other such process. The essential method is that the virtual address mapping to RAM for a running process is booby-trapped (shameless hand-waving on my part, I know). – Paul_Pedant Mar 16 '22 at 23:14
  • @Paul_Pedant Ok, I still don't understand though. Does the Kernel initialze the virtual address mapping? – Engineer999 Mar 17 '22 at 06:53
  • Short answer: no user process could logically have that privilege. The full answer is within https://docs.kernel.org/admin-guide/mm/index.html. The idea is a process sees its memory as a private linear address space. The kernel frequently rearranges virtual address tables such that the underlying RAM resources are correctly mapped (including e.g. dealing with non-mapped areas where swapping has occurred). – Paul_Pedant Mar 17 '22 at 09:41

1 Answers1

2

There are three main aspects to this.

The first is address space.

Some architectures strongly suggest an address space split; for example, on x86-64, the virtual address space is split into two halves, growing from either end of the address space. On such architectures, the kernel follows the suggestion; on x86-64, the bottom half of the address space is used for user-space allocations, the top half for kernel allocations. See How a 64-bit process virtual address space is divided in Linux?

Other architectures don’t provide such structure inherently. The Linux kernel (and most other operating systems with privilege levels) sets up its own address space split, for example on x86, 3G/1G, 2G/2G, 1G/3G (user-space/kernel).

The second is memory allocation.

While the address space split is static, memory allocations aren’t; memory is only allocated as needed. The address space split determines what use can be made of virtual addresses, but it doesn’t affect physical memory use, and doesn’t determine how much of the virtual address space is actually used either. The kernel itself allocates memory for its own binary, and any additional memory it needs for its own purposes; this memory can’t be swapped out (see Does linux kernel use virtual memory (for its data)?).

The third is memory mapping.

This is entirely managed by the kernel, with the help of hardware (the memory management unit). Multiple mappings are maintained at all times: the kernel has one or two of its own, shared by all processes, and each process gets its own user-space mapping. This ensures that processes are protected from each other, and that the kernel is protected from user-space. See What's the use of having a kernel part in the virtual memory space of Linux processes? and The Kernel space in the address space is reserved for us by what?

Stephen Kitt
  • 434,908