Paging in assembly
Posted: Tue Jul 03, 2012 6:04 pm
I've been working on making a 32-bit assembly bootstrapper to sit between GRUB and a 64-bit kernel. I've made some progress, but I'm stuck at page setup. I can create the page directories/tables, and they all seem to be aligned and filled properly, but QEMU resets when I actually enable paging. I've tried digging around the forum with no results.
Currently, my code just identity pages the first two MiB of memory, which should be plenty to hold the bootstrapper.
Here's my code:
Currently, my code just identity pages the first two MiB of memory, which should be plenty to hold the bootstrapper.
Here's my code:
Code: Select all
BITS 32
MODULEALIGN equ 1<<0
MEMINFO equ 1<<1
FLAGS equ MODULEALIGN | MEMINFO
MAGIC equ 0x1BADB002
CHECKSUM equ -(MAGIC + FLAGS)
PAGEDIRPTABLESIZE equ 4 * 8; 4 64-bit ints
PAGEDIRSIZE equ 512 * 8
PAGETABLESIZE equ 512 * 8
PAGESIZE equ 4096
BOOT32MAPSIZE equ 512 * PAGESIZE
section .text ; Next is the Grub Multiboot Header
align PAGESIZE
multiboot_header:
dd MAGIC
dd FLAGS
dd CHECKSUM
STACKSIZE equ 0x4000
boot:
mov esp, STACKSIZE+stack
push eax
push ebx
lgdt [gdtr]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
call init_paging
;eax, ebx are already on the stack.
;call main
init_paging:
;Set up basic structure
;To do: consolidate movs?
mov eax, page_dir
mov [page_dir_ptable], eax
mov eax, page_table
or eax, 3
mov [page_dir], eax
;Identity map
mov ecx, BOOT32MAPSIZE - PAGESIZE
mov edi, page_table + PAGETABLESIZE - 8
map_identity:
mov eax, ecx
or eax, 3
mov [edi], eax
sub edi, 8
sub ecx, PAGESIZE
jnz map_identity
mov eax, ecx
or eax, 3
mov [edi], eax
;Enable PAE and paging
cli
mov eax, cr4
or eax, 1 << 5
mov cr4, eax
mov eax, page_dir_ptable
mov cr3, eax
mov eax, cr0
or eax, 1 << 31
mov cr0, eax
stop:
hlt
jmp stop
ret
;;----;;
;;Data;;
;;----;;
section .data
align PAGESIZE
gdtr:
dw gdt_end - gdt - 1 ; size of the GDT
dd gdt ; linear address of GDT
gdt:
dd 0, 0
db 0xFF, 0xFF, 0, 0, 0, 0x9A, 0xC0 | 0xF, 0
db 0xFF, 0xFF, 0, 0, 0, 0x92, 0xC0 | 0xF, 0
gdt_end:
section .bss
align PAGESIZE
stack:
resb STACKSIZE
page_dir:
resb PAGEDIRSIZE
page_table:
resb PAGETABLESIZE
page_dir_ptable:
resb PAGEDIRPTABLESIZE