virtual memory manager

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
teodori
Member
Member
Posts: 103
Joined: Wed Nov 14, 2012 4:55 pm

virtual memory manager

Post by teodori »

Hello, after finishing my physical memory manager, I am ready to write a virtual manager. In my last topic physical memory manager, I got a few hints how to write a virtual memory manager, but I need a more deep understanding. Brendan said something about using the last entry of each page table to make it point to itself, and I think it's called recursive mapping. Am I correct? This also means I loose 1/512 of the available virtual address space. The total available 48 bit address space is 256 TiB, and I use at maximum 512 GiB for recursive mapping, which is 0.2%. Those 0.2% are reasonable, or not?
User avatar
max
Member
Member
Posts: 616
Joined: Mon Mar 05, 2012 11:23 am
Libera.chat IRC: maxdev
Location: Germany
Contact:

Re: virtual memory manager

Post by max »

Hey teodori,

its called recursive mapping, but you do not map the last entry of each page table to itself, you only map the last entry of the page directory to itself. Then you can access each table by accessing (0xFFC00000 + tableIndex * 0x1000) and the last page 0xFFFFF000 is the page directory itself.

Short (simplified) explanation why: When paging is enabled, and you access for example the address 0xDEADBEEF, the CPU first looks into the page directory and gets the table at index ((address / 0x1000) / 1024). Inside of the table it then looks for the page at index ((address / 0x1000) % 1024). At that index lays the physical address of a page, for example 0xFA57F000 and therefore accesses the physical address 0xFA57FEEF.

Now, when you use the recursion trick, the following happens: because the directory is mapped to itself in its last entry, it is therefore treated like any other page table and mapped to the addresses above 0xFFC00000. The access to an address in that range is just translated as any other access - but the CPU expects that there is a page table (instead of the directory) - therefore its still translated as before, but the physical page that you access in the end is not a page in a table, but one of the page tables in the page directory. ;)

Thus you only "waste" 4MB of the address space (for 32bit), and I think thats reasonable.

Greets, Max
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: virtual memory manager

Post by alexfru »

Fractions of a percent outweigh complexity of doing it a different way. Besides, you haven't got anything that big at this point anyway.
Post Reply