Why the x86 Linux kernel is part of every process's address space
January 19, 2008
In an earlier entry I mentioned that the Linux kernel takes the top 1 gigabyte of a process's address space for itself. In fact this is not just address space that the kernel reserves for its own use; the entire kernel itself actually lives in the top gigabyte of every process's memory map.
(You can't read it or run it because it is protected address space, accessible only when the system is in kernel mode, except for the VDSO it exports to processes these days.)
The kernel does this because it significantly decreases the overhead of system calls and interrupts. With the full kernel always present in virtual memory, most of what you need to do to start running kernel code is to switch from user mode to kernel mode, which is reasonably fast. If the full kernel was not present in virtual memory, switching into the kernel would require mapping the kernel in by changing page tables and flushing the TLB, and this is a relatively slow operation on x86 machines.
(For some discussion of how slow, see here.)
I suspect that historically this is a little white lie and the kernel being present in everyone's virtual memory was originally implemented just because it's simpler; you don't need to write any code to manipulate page tables when you enter and leave the kernel.
(This is one of the entries I write to get at least some of the details straight in my head.)
* * *
Atom feeds are available; see the bottom of most pages.