Page 1 of 1
Questions about paging
Posted: Sun Dec 16, 2007 8:59 am
by Kieran
Hi, I have a few questions about paging.
As I see it, paging works with the kernel looking up the PHYS address of a 4KB block in the page table, pointed to by the page directory which contrains page tables containing the PHYS address of the virtual 4KB blocks pointed to by the page table entrys...
Can each page directory entry only address 4MB of memory?
Does the page size have to be 4KB?
Posted: Sun Dec 16, 2007 9:37 am
by AJ
Hi,
Yes, each Page Table represents a 4MB range, split in to 4KB pages and so each PTE has to be 4kb.
However, If you are looking for larger pages, simply pointing a PDE to a 4MB range (instead of a page table) and setting bit 7 of that PDE allows you to use 4MB pages (you do not need any page tables for this). This requires setting the Page Size Extensions (bit 4) flag in CR4.
If you are using either PAE or long mode, the system changes somewhat. In this system, each and PDE is 64 bytes. This means that each PDE only represents 2MB of virtual space rather thatn 4MB. You therefore have a Page Map Level 4 table (PML4) and a Page Directory Pointer Table) which take you up to 256TiB of virtual address space.
HTH
Adam
Posted: Mon Dec 17, 2007 5:17 am
by Kieran
So if I am to access above 4MB how do I do that?
Posted: Mon Dec 17, 2007 5:29 am
by AJ
Add a second page directory entry. Each PDE represents 4MB. The PD has 1024 entries. This means you have 4GB of virtual address space (in 32 bit mode without PAE enabled). You can find the PDE of any virtual space by virtual_addresss / 0x400000.
If you do not have PSE enabled, the PDE then points to the PT (as you said). Each PTE points to a 4k range.
If you do have PSE enabled and bit 7 of the PDE is set, you do not need a PTE - the PDE just points to a 4MB contiguous physical space.
Cheers,
Adam
Posted: Mon Dec 17, 2007 6:18 am
by digo_rp
use that ->
#define PDI(X) ((X >> 22) & 0x3FF)
you can use in that way
PAGE_DIR_4MB[PDI(my_wanted_addr)] = my_phys_addr | my_permissions | 0x7;
| 0x07 <- 4MB page_dir_entries
Posted: Mon Dec 17, 2007 7:51 am
by Kieran
Im using the 4K pages at the moment, I will kepp using this for the moment.
Posted: Mon Dec 17, 2007 7:55 am
by digo_rp
ok use in that way too.
/* get index into page directory from virtual address */
#define PDI(X) ((X >> 22) & 0x3FF)
/* get index into page table from virtual address */
#define PTI(X) ((X >> 12) & 0x3FF)
page_table[PTI(x)] = x | attr;
page_dir[PDI(x)] = page_table;