简体   繁体   中英

How is the anatomy of memory mapped Kernel space

I try to understand the mechanism in Linux of mapping kernel mode space into user mode space using mmap .

First I have a loadable kernel module (LKM) which provides a character device with mmap -functionality. Then a user space application open the device and calls mmap the LKM allocate memory space on the heap of the LKM inside the kernel mode space (virtual high address). On user space side the data pointer points to a virtual low address.

The following picture shows how I imagine the anatomy of memory is. Is this right?

内存映射

Please let me know if question is not clear, I will try to add more details.


Edit: The picture was edited regarding to Gil Hamilton. The black arrow now points to a physical address.

The drawing is missing out a few important underlying assumptions.

The kernel does not need to mmap() to access user space memory. If a user process has the memory, it's already mapped in the address space by definition. In that sense, the memory is already shared between user and kernel.

mmap() creates a new region in user's virtual address space, so that the address region can be populated by physical memory if later accessed. The actual allocation of memory and modifying the page table entry is done by the kernel.

mmap() only makes sense for managing user-half of the virtual address space. Kernel-half of the address space is managed completely differently.

Also, the kernel-half is shared by all processes in the system. Each process has its dedicated virtual address space, but the page tables are programmed in such a way that the page table entries for the kernel-half are set exactly the same for all processes.

Again, the kernel does not mmap() in order to access user space memory. mmap() is rather a service provided by kernel to user to modify the current mapping in user's virtual address space.

BTW, the kernel actually has a few ways to access user memory if it wants to.

First of all, the kernel has a dedicated region of kernel address space (as part of its kernel space) which maps the entirety of the physical memory present in consecutive fashion. (This is true in all 64-bit system. In 32-bit system the kernel has to 'remap' on-the-fly to achieve this.)

Second, if the kernel is entered via a system call or exception, not by hardware interrupt, you have valid process context, so the kernel can directly "dereference" user space pointer to get the correct value.

Third, if kernel wants to deference a user space pointer of a process while executing in a borrowed context such as in an interrupt handler, kernel can trace process's virtual address by traversing the vm_area_struct tree for permission and walking the page table to find out actual physical page frame.

You can check the memory regions by iterating through vma's "struct vm_area_struct" through current.

If you walk pagetables and derive mapped physical addresses for virtual addresses which is not related to user space then memory layout will be more clear.

Apart from this minor correction in this figure, BSS is not a segment but section which is embed to Data segment, refer ELF specification for more details, linker script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM