Setting Up Paging: Difference between revisions

m
Bot: fixing lint errors, replacing obsolete HTML tags:
[unchecked revision][unchecked revision]
No edit summary
m (Bot: fixing lint errors, replacing obsolete HTML tags:)
 
(9 intermediate revisions by 7 users not shown)
Line 4:
 
Paging is a term that refers to the management of the computer's ''virtual memory''. If you have not yet created a ''physical memory manager'', please read and follow [[Page Frame Allocation]] before continuing with this article.
 
==Basic Paging==
Paging allows you to have more than one virtual address space mapped into the physical address space. The [[MMU]] uses what is called a Page Directory to map virtual addresses to physical addresses.
Line 19 ⟶ 18:
 
Each index in a Page Table contains the physical memory address to which a certain page should be mapped.
 
===Creating a Blank Page Directory===
The first step is to create a blank page directory. The page directory is blank because we have not yet created any page tables where the entries in the page directory can point.
 
Note that all of your paging structures need to be at page-aligned addresses (i.e. being a multiple of 4096). If you have already written a page frame allocator then you can use it to allocate the first free page after your kernel for the page directory. If you have not created a proper page allocator, simply finding the first free page-aligned address after the kernel will be fine, but you should write the page frame allocator as soon as possible. Another temporary solution (used in this tutorial) is to simply declare global objects with __attribute__((align(4096))). Note that this is a GCC extension. It allows you to declare data aligned with some mark, such as 4KiB here. We can use this because we are only using one page directory and one page table. Please note that on the real world, dynamic allocation is too basic to be missing, and paging structures are constantly being added, deleted, and modified. For now, just use static objects;
<syntaxhighlight lang="c">
 
<source lang="c">
uint32_t page_directory[1024] __attribute__((aligned(4096)));
</syntaxhighlight>
</source>
 
Now that we have a page directory, we need to blank it. The page directory should have exactly 1024 entries. We will set each entry to not present so that if the MMU looks for that page table, it will see that it is not there (...yet. We will add the first page table in a moment).
<syntaxhighlight lang="c">
 
<source lang="c">
//set each entry to not present
int i;
Line 42 ⟶ 37:
page_directory[i] = 0x00000002;
}
</syntaxhighlight>
</source>
A page is "not present" when it's not (intended to be) used. If the MMU finds one, it will Page Fault. Non-present pages are useful for techniques 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.
 
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 create a new page table.
<sourcesyntaxhighlight lang="c">
uint32_t first_page_table[1024] __attribute__((aligned(4096)));
</syntaxhighlight>
</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'll get the address (in KiB) at which the page will be loaded. For example, page index 123 in table index 456 will be mapped to (456 * 1024) + 123 = 467067. 467067 * 4 = 1868268 KiB = 1824.48046875 MiB = 1.781719207763671875 GiB. It's easy, right?
<syntaxhighlight lang="c">
 
<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.
Line 68 ⟶ 59:
first_page_table[i] = (i * 0x1000) | 3; // attributes: supervisor level, read/write, present.
}
</syntaxhighlight>
</source>
 
===Put the Page Table in the Page Directory===
The third step is to put the newly created page table into our blank page directory. We do this by setting the first entry in the page directory to the address of our page table.
<syntaxhighlight lang="c">
 
<source lang="c">
// attributes: supervisor level, read/write, present
page_directory[0] = ((unsigned int)first_page_table) | 3;
</syntaxhighlight>
</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 cannot directly access the computer's registers, we will need to use assembly code to access CR3. The following assembly is written for GAS. If you use a different assembler then you will need to translate between this assembly format and the format supported by your assembler.
<syntaxhighlight lang="asm">
 
<source lang="asm">
.text
.globl loadPageDirectory
Line 92 ⟶ 79:
pop %ebp
ret
</syntaxhighlight>
</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.
<sourcesyntaxhighlight lang="asm">
.text
.globl enablePaging
Line 107 ⟶ 93:
pop %ebp
ret
</syntaxhighlight>
</source>
Now let's call the functions!
 
<syntaxhighlight lang="c">
Now lets call the functions!
<source lang="c">
// This should go outside any function..
extern void loadPageDirectory(unsigned int*);
Line 117 ⟶ 102:
loadPageDirectory(page_directory);
enablePaging();
</syntaxhighlight>
</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 do almost all of them for now. Just remember that you have a little friend in the CR3 register that will help you one day.
 
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==
Add sections on how to dynamically get and free pages...
 
Here we are going to make it it little bit more complicated. The code you have now enables paging, but we want to be able to allocate and free pages dynamically. For this, we need to find 4KB aligned addresses using alloc (WARNING, this version of the alloc functions needs to be disabled as soon as you have working heap, otherwise you will destroy your heap).
 
===Temporary Malloc===
 
First, we need to find the end of our kernel, to make sure we don't overwrite our data. For this, we need a symbol at the end of our linker script.
<pre>
/* Read-write data (uninitialized) and stack */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
*(.bootstrap_stack)
}
/* a symbol without a value to be able to find the last address of our kernel */
end_kernel = .;
/* The compiler may produce other sections, put them in the proper place in
in this file, if you'd like to include them in the final kernel. */
}
</pre>
We can now reference this symbol by using
<source lang="c">
extern uint32_t end_kernel;
uint32_t end_pointer = &end_kernel;
</source>
Congratulations, we can now find the end of our kernel. Now we can start writing a temporary malloc function.
<source lang="c">
uint32_t malloc_temp(uint32_t size, uint32_t paging, uint32_t* phys){
if (align == 1 && (placement_address & 0xFFFFF000)) // If the address is not already page-aligned
{
// Align it.
end_pointer &= 0xFFFFF000;
end_pointer += 0x1000;
}
if(phys){
*phys = end_pointer;
}
uint32_t temp = end_pointer;
end_pointer += size;
return temp;
}
</source>
===Allocating and Freeing Pages===
Now that we have a malloc function we can start with our page_alloc and page_free.
<source lang="c">
void page_alloc(uint32_t virtual_address, uint32_t flags){
virtual_address/=0x1000;
uint32_t table_index = virtual_address/1024;
uint32_t page_index = virtual_address%1024;
uint32_t *table;
// if the table doesn't exist
if(page_directory[table_index]==0){
//create it
uint32_t *phys;
table = malloc_temp(0x1000, 1, phys);
memset(table, 0, 0x1000); // I do hope you have this?
page_directory[table_index] = phys | 0x1 | 0x2 | 0x4 // the numbers are flags we need to set.
// 0x1 = present, 0x2 = read_write (just to be sure),
// 0x4 = user (tables where user isn't set cannot be accessed by a usermode program even if the page has this flag)
}else{
// setting the table if we didn't need to create it
table = page_directory[table_index];
table &= 0xFFFFF000;
}
// adding the page to the table, while making sure the present flag is set
table[page_index] = frame_alloc() | 0x1 | flags;
}
void page_free(uint32_t virtual_address){
virtual_address/=0x1000;
uint32_t table_index = virtual_address/1024;
uint32_t page_index = virtual_address%1024;
uint32_t *table;
// if the table doesn't exist we don't need to do anything
if(page_directory[table_index]==0){
return;
}
table = page_directory[table_index];
table &= 0xFFFFF000;
table[page_index] = 0; //setting the page entry to zero
frame_free(virtual_address);
}
</source>
This gives you a function to allocate a page and one to free a page. For the flags you can check [[Paging]] but because I'm nice I will give you the defines for them.
===flags===
<source lang="c">
#define PAGE_PRESENT 0x1
#define PAGE_READ_WRITE 0x2
#define PAGE_USER 0x4
#define PAGE_WRITE_THROUGH 0x8
#define PAGE_CACHE 0x10
#define PAGE_ACCESSED 0x20
#define PAGE_DIRTY 0x40
#define PAGE_GLOBAL 0x100
</source>
Note:
This tutorial requires that you have a working page frame allocator ([[Writing_A_Page_Frame_Allocator]]) and a memset implementation.
[[Category:X86 CPU]]
[[Category:Tutorials]]
40

edits