Page directory and table setup

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
stones3000
Posts: 7
Joined: Thu Aug 09, 2007 5:08 am

Page directory and table setup

Post by stones3000 »

I have two questions regarding Paging and Physical Frame Allocation.

(1) I see some kernel implementations which set up paging with 4MB page size. However, for physical frame allocation, they use 4KB page size. I'm quite confused how can different page sizes be used.

(2) If I want to enable paging with 4KB page size, I can use the code below to setup page directory. But, How can I setup page table? and How to link page table to page directory:

Code: Select all

//setup page directory
uint32_t page_directory[1024] __attribute__ (aligned (4096));
uint32_t i;

for (i = 0; i < 1024; i++) {
    page_directory[i] = i * 4096 | 3; //supervisor level, read/write, present(011 in binary);
}
Thanks in advance!!!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

The paging mechanism consists of a hierarchy of tables. The processor takes top bits of the address and uses that as an index into the table. It then reads the corresponding entry, then considers based upon what's there. In most cases, it'll find it points to another table, it takes the next bits and use those at an index and looks up the entry in the new table. The process is repeated until the table reads that the address is a physical address rather than a table's address. When it sees that, it takes the physical address and adds the remaining bits to that before looking it up in main memory.

the hierarchy has a maximum depth - because there are a limited amount of bits that can used. In standard protected mode you have two levels, in other modes up to four.

4MB paging (or when you get further, 2MB pages and 1GB pages) works simply by stating that an entry is a physical address at a level higher than the lowest. the processor eats the top bytes, finds a physical address, and has a lot more bits left to add to that address (4M combinations), than when the processor has consumed two sets of bits (4k / 4096 combinations)

For the details, I suggest you grab a copy of the intel manuals.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Hi,



To answer your questions:

1) In the 4KiB structure, you have a page table with 1024 entries, each of which points to a 4KiB page frame - you seem to understand this OK. In the 4MiB scheme, your page directory points to a 4MiB range instead of a page table, thus giving you a 4MiB continuous range (with PAE enabled, large pages are actually 2MiB).

2) Have a look at http://www.jamesmolloy.co.uk - there is a good paging tutorial there.

Cheers,
Adam

[edit]too late![/edit]
Post Reply