Anonymous user
Setting Up Paging: Difference between revisions
Jump to navigation
Jump to search
no edit summary
[unchecked revision] | [unchecked revision] |
m (Removed underscores from link to Page Frame Allocation) |
No edit summary |
||
Line 2:
This is a guide to setting up paging. It will teach you the basic concepts behind paging and how it can help you with your OS. This example will concentrate on Legacy Non-PSE Non-PAE paging (See also [[Setting Up Paging With PAE]]).
Paging is a term that refers to the management of the computer's ''virtual
==Basic Paging==
Line 25:
If you know where the end of your kernel is, then you can put the page directory right after it.
Note that all of your paging structures need to be at page
To get the first page after your kernel, you must modify your Linker Script. Add this to the end of your "SECTIONS" block (*inside* it):
<source lang="c">▼
<source>
__end__ = .;
unsigned int *page_directory = (unsigned int*)page_aligned_end;▼
</source>
<source lang="c">
extern const unsigned int __end__;
unsigned int *
</source>
Line 44 ⟶ 43:
<source lang="c">
//set each entry to not present
int i
for(i = 0; i < 1024; i++)
{
// This sets the following flags to the pages:
//attribute: supervisor level, read/write, not present.▼
// Supervisor: Only kernel-mode can access them
page_directory[i] = 0 | 2; ▼
// Write Enabled: It can be both read from and written to
// Not Present: The page table is not present
}
</source>
A page is "not present" is one which is not (intented to be) used. If the MMU finds one, it will Page Fault. Non-present pages are useful for technics such as Lazy Loading. It's also used when a page has been swapped to disk, so the Page Fault is not interpreted as an error by the OS. To the OS, it means someone needs a page it swapped to disk, so it is restored. A page fault over a page that was never swapped is a error by which the OS has a reason to kill the process.
===Creating Your First Page Table===
The second step is to create a basic page table. In this example we choose to fill up the whole first page table with addresses for the MMU. Because each page is 4 kilobytes large, and because each page table has exactly 1024 entries, filling up the whole table causes us to map 4 megabytes of memory. Also, the page directory is 1024 entries long, so everything can map up to 4GiB, the full 32-bit address space. Remembered the non-present page trick? Without it, we would use 16MiB per each paging structure. A single page directory needs 4KiB, but it can map some tables as non-present, effectively removing their space needs.
Now, its time to put a new page table in the directory! We start like we did with the page directory, by finding a piece of free memory where we can keep our page table. Again, use your page allocator for this if you have written one. If not, then put the page table one page (i.e. +4KiB) past the page directory. If we properly page-aligned the page directory, then the page table should also be properly page aligned.
<source lang="c">
//our first page table comes right after the page directory
unsigned int *first_page_table = (unsigned int*)((unsigned int)page_directory +
</source>
We now need to fill each index in the table with an address to which the MMU will map that page. Index 0 (zero) holds the address from where the first page will be mapped. Likewise, index 1 (one) holds the address for the second page and index 1023 holds the address of the 1024th page. That's for the first table. So, to get the page at which a certain index is mapped is as simple as (PageDirIndexOfTable * 1024) + PageTabIndexOfPage. If you multiply that by 4, you'
<source lang="c">
// holds the physical address where we want to start mapping these pages to.
// in this case, we want to map these pages to the very beginning of memory.
unsigned int i;
//we will fill all 1024 entries in the table, mapping 4 megabytes
for(i = 0; i < 1024; i++)
{
// Those bits are used by the attributes ;)
first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
}
</source>
Line 81 ⟶ 85:
<source lang="c">
page_directory[0]
</source>
===Enable Paging===
The final step is to actually enable paging. First we tell the processor where to find our page directory by putting it's address into the cr3 register. Because C code can not directly access the computer's registers, we will need to use inline assembly to set cr3. The following
<source lang="
.text
.globl loadPageDirectory
loadPageDirectory:
push %ebp
mov %esp, %ebp
mov 8(%esp), %eax
mov %eax, %cr3
mov %ebp, %esp
pop %ebp
ret
</source>
This small assembly function takes one parameter: the address of the page directory. It then loads the address onto the CR3 register, where the MMU will find it. But wait! Paging is not still enabled. That's what we will do next. We must set the 32th bit in the CR0 register, the paging bit. This operation also requires assembly code. Once done, paging will be enabled.
.text
.globl enablePaging
enablePaging:
push %ebp
mov %esp, %ebp
mov %cr0, %eax
or $0x80000000, %eax
mov %eax, %cr0
mov %ebp, %esp
pop %ebp
ret
</source>
Now lets call the functions!
<source lang="c">
// This should go outside any function..
extern void loadPageDirectory(unsigned int
extern void enablePaging();
// And this inside a function
loadPageDirectory(page_directory);
enablePaging();
</source>
Paging should now be enabled. Try printing something to screen like "Hello, paging world!". If all goes well, congratulations! You've just learned the basics of paging. But there are lots of other things to do with it. You won't be able to almost all of them for now. Just remember that you have a little friend in the CR3 register that will help you one day.
==More Advanced Paging Example==
|