Memory allocation for page directories and tables in the vmm

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
prithivimarudhoo
Posts: 1
Joined: Wed Mar 23, 2022 10:58 am
Libera.chat IRC: prithivimarudhoo

Memory allocation for page directories and tables in the vmm

Post by prithivimarudhoo »

I am having trouble figuring out a way to allocate memory for the page tables and directories in my virtual memory manager.
I'm working my way up from the BareBones tutorial and currently have a reasonably functional Physical Memory Manager.
My bootloader enables paging and set's up the kernel to be higher half (at 0xC0000000). Obviously, when I allocate a block using the physical memory manager and
try to use it, a page fault is thrown, as it should be.

My question is how do I allocate and use memory for the virtual memory manager structures? I can't do the same thing I did for the PhysicalMemoryManager and
attach these VMM structures to the end of the kernel since they will have to be freed and allocated. If I allocate space for the Page Tables and the Page Directories using the
Physical Memory Allocator, I won't be able to use them since any attempt to use them will throw a page fault.

Any help is appreciated. Thank you.
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Memory allocation for page directories and tables in the

Post by Octocontrabass »

Your bootloader needs to map the page tables somewhere so your VMM can modify them. That way, when you allocate memory from the PMM, your VMM can assign a virtual address.
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Memory allocation for page directories and tables in the

Post by alexfru »

You should probably look up and learn recursive mapping.
It'll let you easily modify page tables to map/unmap things on demand. Your original data structures describing the available RAM (what your bootloader finds from the BIOS or UEFI) should be mapped too, obviously.

You can implement your basic heap that would allocate/deallocate the virtual address space and map/unmap the underlying frames using Flat List. The control information may be directly adjacent to the blocks of allocated/free memory, e.g. (this is the virtual address space view):

[size,other metadata] [allocated/free block 1] [size,other metadata] [allocated/free block 2] ...

You start with one huge free block. Then you subdivide it to allocate and you map pages as needed. When freeing, you coalesce adjacent free blocks and unmap unused pages.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Memory allocation for page directories and tables in the

Post by iansjack »

Have a look at this blog post: https://os.phil-opp.com/paging-implementation/ It describes your problem and various solutions. Although the blog is about writing an OS in Rust, the discussion and theory there is equally applicable to C/C++.

If you are writing a 64-bit OS, the approach of creating a mapping of all physical memory to a virtual address range is easiest and most versatile. (Some people see this as a security risk, but I’m not convinced that it’s worse than any other approach.)
Post Reply