Page 1 of 1

Consufing kernel space

Posted: Wed Nov 04, 2020 8:56 am
by yjxqwed
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!

Re: Consufing kernel space

Posted: Thu Nov 05, 2020 4:31 am
by Octocontrabass
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?
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:2. Does Linux kernel occupy the low 896M physical memory after initialized?
No. Most of that memory is available for user processes (or disk caches).
yjxqwed wrote:If not, how can the kernel guarantee that the NORMAL ZONE is directly mapped?
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:Do you have good memory management subsystem development tutorials?
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.

Re: Consufing kernel space

Posted: Thu Nov 05, 2020 4:52 pm
by thewrongchristian
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?
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.

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.
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?
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.

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.)
yjxqwed wrote:
Do you have good memory management subsystem development tutorials?

Many thanks!
NetBSD UVM is well documented.

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.