Setting Up Paging: Difference between revisions

More notes about Page_Frame_Allocation. Improved clarity.
[unchecked revision][unchecked revision]
(→‎Creating a Blank Page Directory: Page alignment notes)
(More notes about Page_Frame_Allocation. Improved clarity.)
Line 1:
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. Paging normally should not be the first part of your memory manager that you write. It is generally considered a good idea to first write a Page Frame Allocator (see [[Page_Frame_Allocation]]) that will manage the computer's physical memory for you. You can then design your paging system to not have to manage the physical memory on which it sits.
 
 
This example will concentrate on Legacy Non-PSE Non-PAE paging.
 
==Basic Paging==
Line 24:
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 aligned addresses,. soIf it'syou have already probablywritten a goodpage ideaframe toallocator makethen ayou pagecan allocatoruse firstit andto haveallocate thatthe dishfirst outfree pagespage forafter your pagingkernel codefor the page directory. UntilIf you createhave not created a proper page allocator though, simply finding the first free page-aligned address after the kernel will be fine.
 
<pre>
Line 54:
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.
 
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 past the page directory. If we properly page-aligned the page directory, then the page table should also be properly page aligned.
<pre>
//our first page table comes right after the page directory
Line 85:
 
===Enable Paging===
The final step is to actually enable paging. First we tell the processor where to find our page table 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 inline assembly is written for GCC. If you use a different compiler then you will need to translate between this assembly format and the format supported by your compiler.
 
<pre>
//moves page_directory (which is a pointer) into the cr3 register.
asm volatile("mov %0, %%cr3":: "b"(page_directory));
</pre>
Line 93 ⟶ 94:
Finally we switch the paging bit on the cr0 register. This operation also requires inline assembly code.
<pre>
//reads cr0, switches the "paging enable" bit, and writes it back.
unsigned int cr0;
asm volatile("mov %%cr0, %0": "=b"(cr0));
Anonymous user