This does fine. I double check with removing entry in PTE and it crash as expected, so the paging are in effect.
Then I try "higher half" it, it however crash when I jump to high address.
My code:
Code: Select all
cpu x86-64
bits 64
global bootstrap
extern kprintf, kmain
KERNEL_PMA equ (0x00100000)
KERNEL_VMA equ (0xFFFFFFFF80000000 + KERNEL_PMA)
%define PMA(x) ((x) - KERNEL_VMA + KERNEL_PMA)
section .bss
; ----------------------------------------------
align 4096
k_PML4 resb 4096
k_PDPTE resb 4096
k_PDE resb 4096
k_PTE0 resb 4096
k_PTE1 resb 4096
section .data
; ----------------------------------------------
msg db 'HighHalf = %X', 0
section .text
; ----------------------------------------------
bootstrap:
; Setup page tables
; ----------------------------------------------
mov rdi, PMA(k_PML4)
mov qword [rdi], PMA(k_PDPTE) +1
mov qword [rdi+4096], PMA(k_PDE) +1
mov qword [rdi+8192], PMA(k_PTE0) +1
; mov qword [rdi+8192+8], PMA(k_PTE1) +1
; higher half
mov qword [rdi+((KERNEL_VMA>>39)&0x1FF) *8], PMA(k_PDPTE) +1
mov qword [rdi+4096+((KERNEL_VMA>>30)&0x1FF) *8], PMA(k_PDE) +1
mov rdi, PMA(k_PTE0)
mov rcx, 512*2
mov rax, 3
.1:
mov [rdi], rax
add rax, 0x1000
add rdi, 8
loop .1
mov rdi, PMA(k_PML4)
mov cr3, rdi
mov rcx, qword .HigherHalf
; if i put hlt here, it halt with no problem, so the new page structure covered low addresses.
; hlt
jmp rcx ; crash here
.HigherHalf:
mov edi, 0xB8000
mov rax, 0x1F311F321F331F34
mov ecx, 500
rep stosq
hlt
Code: Select all
Filling page structures:
ffffffff80100000 <.text>:
ffffffff80100000: 48 bf 00 40 10 00 00 movabs $0x104000,%rdi
ffffffff80100007: 00 00 00
ffffffff8010000a: 48 c7 07 01 50 10 00 movq $0x105001,(%rdi)
ffffffff80100011: 48 c7 87 00 10 00 00 movq $0x106001,0x1000(%rdi)
ffffffff80100018: 01 60 10 00
ffffffff8010001c: 48 c7 87 00 20 00 00 movq $0x107001,0x2000(%rdi)
ffffffff80100023: 01 70 10 00
ffffffff80100027: 48 c7 87 f8 0f 00 00 movq $0x105001,0xff8(%rdi)
ffffffff8010002e: 01 50 10 00
ffffffff80100032: 48 c7 87 f0 1f 00 00 movq $0x106001,0x1ff0(%rdi)
The jmp:
ffffffff80100077: 48 b9 83 00 10 80 ff movabs $0xffffffff80100083,%rcx
ffffffff8010007e: ff ff ff
ffffffff80100081: ff e1 jmpq *%rcx
The higher Half code:
ffffffff80100083: bf 00 80 0b 00 mov $0xb8000,%edi
ffffffff80100088: 48 b8 34 1f 33 1f 32 movabs $0x1f311f321f331f34,%rax
ffffffff8010008f: 1f 31 1f
ffffffff80100092: b9 f4 01 00 00 mov $0x1f4,%ecx
ffffffff80100097: f3 48 ab rep stos %rax,%es:(%rdi)
ffffffff8010009a: f4 hlt
the above code is assemble with nasm -f elf64
linked together with x86_64-elf-ld -nostdinc -nostdlib -nodefaultlibs -Tkernel.ld ...
EDIT: SOLVED