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