I'm currently developing a simple kernel. And my current focus is the memory management (mm) subsystem. To design a mm, I read some materials about Linux. However, I'm confused :
1. Why does Linux kernel define the NORMAL ZONE, which is directly mapped (linear address = physical address + PAGE_OFFSET)? Is it just for efficiency?
2. Does Linux kernel occupy the low 896M physical memory after initialized? If not, how can the kernel guarantee that the NORMAL ZONE is directly mapped? If so, what physical memory will be used for the user processes if the machine has memory less than 896M?
Do you have good memory management subsystem development tutorials?
Many thanks!
Consufing kernel space
-
- Member
- Posts: 5568
- Joined: Mon Mar 25, 2013 7:01 pm
Re: Consufing kernel space
It's for efficiency. On x86, the kernel can't access memory that isn't mapped, and it takes time to map and unmap memory. Since ZONE_NORMAL is always mapped, the kernel doesn't need to spend time mapping and unmapping memory every time it needs to access something in that zone.yjxqwed wrote:1. Why does Linux kernel define the NORMAL ZONE, which is directly mapped (linear address = physical address + PAGE_OFFSET)? Is it just for efficiency?
No. Most of that memory is available for user processes (or disk caches).yjxqwed wrote:2. Does Linux kernel occupy the low 896M physical memory after initialized?
It sets up the mapping during initialization and then never changes the mapping. Memory can be mapped at more than one virtual address at the same time, so the kernel doesn't need to unmap ZONE_NORMAL memory to use it elsewhere.yjxqwed wrote:If not, how can the kernel guarantee that the NORMAL ZONE is directly mapped?
I don't think there are any good tutorials for this. You're probably better off researching how different kernels use their address space and why they use it the way they do.yjxqwed wrote:Do you have good memory management subsystem development tutorials?
-
- Member
- Posts: 426
- Joined: Tue Apr 03, 2018 2:44 am
Re: Consufing kernel space
I think it's largely historical. Linux was designed in 1991, when 16MB would have been a large amount of memory for a consumer level machine, and UNIX workstations topped out at maybe 64MB or 128MB.yjxqwed wrote:I'm currently developing a simple kernel. And my current focus is the memory management (mm) subsystem. To design a mm, I read some materials about Linux. However, I'm confused :
1. Why does Linux kernel define the NORMAL ZONE, which is directly mapped (linear address = physical address + PAGE_OFFSET)? Is it just for efficiency?
At the time, 896MB of memory was huge, and it seemed perfectly reasonable to map in 64MB of physical RAM its entirety in one place to make working with physical memory easier.
The Linux model is probably not a good model to emulate.
Linux just maps the low 896MB of memory as a convenience. It gives nice, easy access to certain physical pages with a convenient mapping between virtual and physical addresses. It there makes code that works with physical buffers, like device drivers, a simpler time translating between virtual and physical memory addresses, allowing a simple offset to go from one to the other.yjxqwed wrote: 2. Does Linux kernel occupy the low 896M physical memory after initialized? If not, how can the kernel guarantee that the NORMAL ZONE is directly mapped? If so, what physical memory will be used for the user processes if the machine has memory less than 896M?
This gives a simple overview.
But all the pages in the memory can be used for any purpose, including user pages. Its just those pages will have multiple mappings (the kernel direct mapping + any user mappings.)
NetBSD UVM is well documented.yjxqwed wrote:
Do you have good memory management subsystem development tutorials?
Many thanks!
Of course, not in the form of a tutorial, but UVM is a nice, clean, virtual memory system, and I personally prefer how UVM splits machine independent from the machine dependent parts compared to Linux's method of defining an abstract machine independent page table structure.
My own hobby kernel uses a system more like UVM than Linux's abstract page table, using what is basically a virtual-TLB like API.
But for virtual memory, I find text books tend to be better than online resources.