Page 1 of 1

Memory Management and Paging confusion.

Posted: Thu Feb 08, 2007 4:15 am
by Jabus
I've spent a few weeks puzzling over memory management adn I just don't seem to get it. Here is what I've understood from it and I'd just like you to clarify if what I'm saying is true.
1. Memory management is used so that programs don't interfere with other programs memory.
2. One method of doing so is paging.
3. Paging uses page directories and page tables. A page directory is a list of page tables and a page table reference 4 mb of memory. A page directory references 4 Gb of memory.

That is what i have understood so far. However if a bootloader creates a page directory adn 2 page tables. It then loads the kernel at memory location 0xFF800000 which is mapped to 0x00100000. If I were to have a kernel function that creates a page directory would it be created in physical memory or would it be created in the page that the kernel is currently in? Could I overwrite the page directory the bootloader creates? The bootloader I am using is BOOTF02. I hope you can help me.

Posted: Thu Feb 08, 2007 4:50 am
by Otter
If your paging is active, your kernel can only access physical pages when they are mapped in your virtual kernel space. So lets say your kernel function shall create a new page table, your memory manager has to do the following things:
1) Find an unused physical page
2) Find an unused virtual pagf ( it has not to be the same as the physical page )
3) Map this virtual page to this phyiscal page
Now you can access your new page table, so
4) fill it with the correct values ( physcial addresses | flags )
5) you need access to your page directory to register your new page table. So your first paging directories and page tables should map your page directory to virtual space.

Posted: Thu Feb 08, 2007 11:07 am
by Jabus
I've worked out where the page tables are created by the bootloader. Now if I was to create a table with an entry map say memory location 0xFFFF000 to 0xB8000 would I have to disable paging and then enable paging for that to take affect or would it happen automatically.

Posted: Thu Feb 08, 2007 12:27 pm
by JAAman
it will be changed immediately, except:

the CPU caches the contents of page tables it has used recently, in a special cache called the TLB (Translation Lookaside Buffer), if the page you changed is in a TLB, then it wont automatically be updated until its removed from the TLB and then reloaded, so what you need to do, is tell the CPU that that page mapping has changed

if you have a 486 or later, you can use invlpg -- invalidate page
on the 386, you must reload CR3 (which tells the CPU to invalidate all TLBs -- except those marked as global, but thats only available on the P6+ iirc)

using invlpg will do nothing if that page is not currently in a TLB, if that page table is currently in a TLB, then it will mark that TLB as invalid, so that next time that page is referenced, it will reread the page table into memory