Questions about paging

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
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Questions about paging

Post 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?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Post by Kieran »

So if I am to access above 4MB how do I do that?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post 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
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Post 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
Kieran
Member
Member
Posts: 54
Joined: Mon Apr 11, 2005 11:00 pm

Post by Kieran »

Im using the 4K pages at the moment, I will kepp using this for the moment.
digo_rp
Member
Member
Posts: 233
Joined: Sun Jun 05, 2005 11:00 pm

Post 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;
Post Reply