Page 1 of 1

Modifying page directory

Posted: Sat Oct 13, 2012 2:22 pm
by lrod
How do I modify page directory when paging is enabled?

In wiki there is a paragraph "Manipulation", but it is very brief. Can some one explain it in detail?

Re: Modifying page directory

Posted: Sat Oct 13, 2012 6:36 pm
by Brendan
Hi,
lrod wrote:How do I modify page directory when paging is enabled?

In wiki there is a paragraph "Manipulation", but it is very brief. Can some one explain it in detail?
When paging is enabled; software can only modify physical pages that are mapped into the virtual address space that is currently being used. The page directory is a physical page; therefore software can only modify a page directory if it is mapped into the virtual address space that is currently being used. How you map page directory/ies into virtual address space/s depends on how you feel like doing it for your OS.

I'm unable to determine how you feel like doing it for your OS; which makes it hard to explain how you feel like doing it for your OS in further detail.

However; a lot of people use a trick where you pretend that the physical page containing the page directory is also a physical page containing a page table. You need to understand how paging works before you can really understand how this trick works.

The paging hardware splits a virtual address into 3 parts:
  • an index into the page directory that selects which page table to use
  • an index into the page table that selects which physical page to use
  • an offset into a physical page (where "physical address of selected page + offset within that page = physical address that corresponds to the original virtual address")
If you're pretending that the page directory is also one of the page tables; then the paging hardware splits a virtual address into 3 parts:
  • an index into the page directory that selects which page table to use; where the selected page table may be the page directory itself
  • an index into the page table that selects which physical page to use; where the selected physical page may actually be a page table (and may be the page directory pretending to be a page table)
  • an offset into the physical page; which may actually be an offset into a page table (and may be an offset into the page directory pretending to be a page table)
If the highest page directory entry points to the physical address of the page directory (instead of pointing to the physical address of a page table), then:
  • for virtual addresses from 0xFFC00000 to 0xFFFFFFFF, the index into the page directory would be 0x3FF, which is the highest page directory entry that points to the physical address of the "page directory pretending to be a page table"
  • for virtual addresses from 0xFFC00000 to 0xFFFFFFFF, the index into the page table would be an index into the "page directory pretending to be a page table"
  • for virtual addresses from 0xFFC00000 to 0xFFFFFFFF, the offset in the physical page would be an offset into a page table.
This means that, by making the highest page directory entry point to the physical address of the page directory itself, you end up with a 4 MiB area of the virtual address space that contains a mapping of all page tables.

In addition; if the highest page directory entry points to the physical address of the page directory (instead of pointing to the physical address of a page table), then:
  • for virtual addresses from 0xFFFFFF000 to 0xFFFFFFFF, the index into the page directory would be 0x3FF, which is the highest page directory entry that points to the physical address of the "page directory pretending to be a page table"
  • for virtual addresses from 0xFFFFF000 to 0xFFFFFFFF, the index into the "page directory pretending to be a page table" would be 0x3FF, which is the highest page directory entry that points to the physical address of the "page directory pretending to be a page table" again
  • for virtual addresses from 0xFFFFF000 to 0xFFFFFFFF, the offset in the physical page would actually be an offset into the page directory itself.
This means that, by making the highest page directory entry point to the physical address of the page directory itself, you end up with a 4 MiB area of the virtual address space that contains a mapping of all page tables, and a 4 KiB area of the virtual address space that contains a mapping of the page directory itself.


Cheers,

Brendan

Re: Modifying page directory

Posted: Sun Oct 14, 2012 4:07 am
by iansjack
Note that after you update the Page Directory you need to invalidate the TLB buffer for that entry, otherwise the change won't take effect. You can either do this on a page basis, with the INVLPG instruction, or you can reload cr3 and so invalidate the whole TLB (with certain exceptions).

Re: Modifying page directory

Posted: Sun Oct 14, 2012 9:22 am
by lrod
Brendan wrote:Hi,
...
Cheers,

Brendan
Thanks for answer!

I think I almost got it, but one thing I don't understand. From 0xffc00000 to 0xffffffff there is 4Mb, right? And 1024 page directory entres takes exactly 4Mb, so where does additional 4Kb comes from to map page directory?

Re: Modifying page directory

Posted: Sun Oct 14, 2012 11:04 am
by egos
Last 4-kb page table and page directory are the same page.