Alignment problem in bootloader? [SOLVED](memory corruption)
Posted: Thu Jun 13, 2024 2:42 pm
I am working on a bootloader. In my bootloader I switch to long mode. On the long jump which loads the CS register, I get a triple fault in QEMU. Here is the code:
However, if I add a nop immediately after Then the code works fine in QEMU! My GDT and DAP are 32bit aligned. Any suggestions?
Code: Select all
org 0x7c00
bits 16
%define IDT_ADDR 0xF000
%define KERNEL_ADDR 0x10000
%define STACK_ADDR 0x40000
%define BLOCK_COUNT 127
; Canonicalize IP
jmp 0x0000:start
start:
cli
cld
; Setup
xor ax, ax
mov ds, ax
mov es, ax
mov bx, 0x1000
mov ss, bx
mov sp, ax
push 0xb800
pop gs
; Read 127 sectors (64k) from begining of disk to memory
load: mov word [dap + 2], BLOCK_COUNT
mov ah, 42h
mov si, dap
int 0x13
jc load
; Turn on A20 gate (fast A20)
a20: in al, 0x92
or al, 2
out 0x92, al
; Set up long mode paging
mov edi, 0x1000
mov cr3, edi
xor eax, eax
mov ecx, 0x6000
rep stosd
; 6 MB identity mapped
mov word [0x1000], 0x2003
mov word [0x2000], 0x3003
mov word [0x3000], 0x4003
mov word [0x3008], 0x5003
mov word [0x3010], 0x6003
mov ebx, 3
mov ecx, 512 * 3
mov edi, 0x4000
page: mov dword [edi], ebx ; [0x4000] = 0x0003, [0x4008] = 0x1003, ..., [0>
add ebx, 0x1000
add edi, 8
loop page
; Enable PAE
pae: mov eax, cr4
or eax, 1 << 5
mov cr4, eax
; Load gdt
lgdt [gdtr]
; Switch to long mode
mov ecx, 0xc0000080 ; LM-bit
rdmsr
or eax, 1 << 8
wrmsr
mov eax, cr0 ; Enable paging and protected mode
or eax, 1 << 31 | 1 << 0
mov cr0, eax
jmp CODE_SEG:lmode
bits 64
lmode:
Code: Select all
start: