Re: which memory allocation system to use
Posted: Fri Jul 23, 2021 7:25 am
Actually, I was talking about allocating page tables. Here is how my flow will go:rdos wrote:In 32-bit mode, there are only 1024 PDEs, and 256 of those would be for the 1G kernel space. Allocating these on demand has no speed implications. However, if you pre-allocate them then you will waste 256 x 4k = 2M of physical memory (4M for PAE). Compare that with 4k for the system mapping. It actually doesn't matter if you set bit 0 or not. If you are not allocating them dynamically, then you will need to reserve physical memory for them that cannot be used for other things.
User or kernel request, say, 12K of memory.
Kernel searches AVL tree for region of that size
Kernel allocates Address Region descriptor
Kernel now access PDE for this region
If the page table is not present, the kernel goes ahead and allocates the page table from zero filled memory and maps it.
The kernel sets the corresponding PTEs to point to the ARD for this region, unsetting bit 0 so the kernel doesn't think it has been mapped.
User or kernel access this region
Kernel grabs the PTE for the faulting area
It checks the ADR pointed to by the PTE as to what kind of memory this is (zero fill, swapped, mapped to a file, etc. I will only explain how zero fill will work)
If zero fill, the kernel allocates a zero filled page and makes the PTE point to it, also setting bits such as R/W, NX, and so on according to the ADR.
Kernel then restarts the instruction.
Of course, thing are much different for swapped or memory mapped files, but, I haven't planned those yet
I will have one more level of indirection to make allocations more efficient, and also to allow for versatility with swap and memory mapped files.rdos wrote:I set unallocated PTEs to zero, and when they are allocated I set bit 1 to indicate this. When the PF handler see a PTE with bit 0 zero and bit 1 as one, it will allocate physical memory for it, set up the PTE, and reexecute. So, if I allocate 10 pages in kernel, they will be setup with PTEs containing "2", and will consume no physical memory. When a pagefault occurs, physical memory is mapped automatically. The pagefault handler will generate a pagefault exception if bit 1 is not set. I also have a more specialized function where I can link a callback. This is not really demand paging, rather lazy allocation of physical memory in kernel space.