enable 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
guest

enable paging

Post by guest »

hi, I want to enable paging. How could I do this in ASM?
Tim

Re:enable paging

Post by Tim »

You should read the relevant sections of the Intel manuals while looking at some sample source.

But in summary:
1. Allocate a page directory and clear it to zero
2. Allocate a page table and clear it to zero. This will map the bottom 4MB of memory.
3. Add mappings in the page table. You need to map at least the location of your code, i.e. the 4KB page where EIP is sitting. You might as well map the whole bottom 4MB.
4. Plug the address of the page table into the page directory
5. Plug the address of the page directory into CR3
6. Set the PG bit in CR0
7. Execute the JMP instruction
8. Watch as your code works painlessly first time ;)

Notes:
-- Page table and page directory must be on 4KB-aligned addresses
-- You must set the Present bit in the page directory entry for the page table and in each entry in the page table
-- Test this on Bochs until it works, because it will triple fault each time you run it until it is correct

Read http://www.osdever.net/tutorials/memory1.php for how to allocate and free pages (you'll need this for your page directories and page tables) and how to make virtual-to-physical mappings (you'll need this to set up the initial page directory and page table).
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:enable paging

Post by Pype.Clicker »

basically you have to set the physical address of your page directory in CR3 and then toggle the highest bit of CR0 ...

Code: Select all

section .data
   .align 4096
PgDir:
   resb 4096
PgTable:
   resb 4096

section .text
enable_paging:
    call prepare_page_directory
    call prepare_page_table
    mov eax, PgDir
    add eax, BASE_OF_DS_SEGMENT_IF_NOT_0
    mov cr3,eax
    mov eax,cr0
    or eax, 0x80000000
    mov cr0,eax
I suggest you read the paging chapter of Holy Intel Manuals in details before you try this. Also make sure the mapping of your current code, stack and data remains available.

And finally ...
do not enable paging before you have a page fault handler installed and a working interrupt management system ;)
Post Reply