Qemu 在设置 GDT 时无限重启

Qemu is infinitely rebooting when setting GDT

我主要用 C++ 制作 OS,但对于引导加载程序,我使用 FASM。当我尝试设置 GDT 时,Qemu 清除屏幕并在顶部重新打印“SeaBIOS”。它会继续这样循环,直到我关闭它。这是它的动图:

我用 -nographic 尝试 运行 它,但它在 Windows 控制台中做同样的事情。

哦,是的,OS/version 信息。
Windows: 20H2
FASM:1.73.25
Qemu: 5.1.0

这是我的代码:

gdt_start:
    dd 0x00
    dd 0x00

gdt_code:
    dw 0xffff
    dw 0x0
    db 0x0
    db 10011010b
    db 11001111b
    db 0x0

gdt_data:
     dw   0xffff
     dw   0x0
     db   0x0
     db   10010010b
     db   11001111b
     db   0x0

gdt_end:

gdt_descriptor:
    dw gdt_end - gdt_start - 1
    dd gdt_start

CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

switch_to_pm:
    cli
    lgdt [gdt_descriptor]
    mov eax, cr0
    or eax, 0x1
    mov cr0, eax
    jmp CODE_SEG:init_pm

use32

init_pm:
    mov ax, DATA_SEG
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    
    mov ebp, 0x90000
    mov esp, ebp
    
    call BEGIN_PM

BEGIN_PM:
    mov ebx, MSG_PROT_MODE
    call print_string_pm
    call KERNEL_OFFSET
    jmp $

更新:完整代码:

; boot.asm

org 0x7c00
KERNEL_OFFSET equ 0x1000

mov [BOOT_DRIVE], dl
mov bp, 0x9000
mov sp, bp

mov bx, MSG_REAL_MODE
call print
call print_nl

call load_kernel

call switch_to_pm

jmp $

print:
    pusha

start:
    mov al, [bx]
    cmp al, 0
    je done
    
    mov ah, 0x0e
    int 0x10
    
    add bx, 1
    jmp start
    
done:
    popa
    ret

print_nl:
    pusha
    
    mov ah, 0x0e
    mov al, 0x0a
    int 0x10
    mov al, 0x0d
    int 0x10
    
    popa
    ret

load_kernel:
    mov bx, MSG_LOAD_KERNEL
    call print
    call print_nl
    
    mov bx, KERNEL_OFFSET
    mov dh, 1
    mov dl, [BOOT_DRIVE]
    call disk_load
    ret

disk_load:
    pusha
    push dx
    
    mov ah, 0x02
    mov al, dh
    mov cl, 0x02
    
    mov ch, 0x00
    mov dh, 0x00
    
    int 0x13
    jc disk_error
    
    pop dx
    cmp al, dh
    jne sectors_error
    popa
    ret

disk_error:
    mov bx, DISK_ERROR
    call print
    call print_nl
    mov dh, ah
    call print_hex
    jmp disk_loop

sectors_error:
    mov bx, SECTORS_ERROR
    call print

disk_loop:
    jmp $

print_hex:
    pusha
    mov cx, 0

hex_loop:
    cmp cx, 4
    je end_hex
    
    mov ax, dx
    and ax, 0x000f
    add al, 0x30
    cmp al, 0x39
    jle step2
    add al, 7

step2:
    mov bx, HEX_OUT + 5
    sub bx, cx
    mov [bx], al
    ror dx, 4
    
    add cx, 1
    jmp hex_loop

end_hex:
    mov bx, HEX_OUT
    call print
    popa
    ret

HEX_OUT:
    db '0x0000', 0

gdt_start:
    dd 0x00
    dd 0x00

gdt_code:
    dw 0xffff
    dw 0x0
    db 0x0
    db 10011010b
    db 11001111b
    db 0x0

gdt_data:
     dw   0xffff
     dw   0x0
     db   0x0
     db   10010010b
     db   11001111b
     db   0x0

gdt_end:

gdt_descriptor:
    dw gdt_end - gdt_start - 1
    dd gdt_start

CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

switch_to_pm:
    cli
    lgdt [gdt_descriptor]
    mov eax, cr0
    or eax, 0x1
    mov cr0, eax
    jmp CODE_SEG:init_pm

use32

init_pm:
    mov ax, DATA_SEG
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    
    mov ebp, 0x90000
    mov esp, ebp
    
    call BEGIN_PM

BEGIN_PM:
    mov ebx, MSG_PROT_MODE
    call print_string_pm
    call KERNEL_OFFSET
    jmp $

VIDEO_MEMORY equ 0xb8000
WHITE_ON_BLACK equ 0x0f

print_string_pm:
    pusha
    mov edx, VIDEO_MEMORY

print_string_pm_loop:
    mov al, [ebx]
    mov ah, WHITE_ON_BLACK
    
    cmp al, 0
    je print_string_pm_done
    
    mov [edx], ax
    add ebx, 1
    add edx, 2
    
    jmp print_string_pm_loop

print_string_pm_done:
    popa
    ret

BOOT_DRIVE db 0
MSG_REAL_MODE db "Started in 16-bit real mode", 0
MSG_LOAD_KERNEL db "Loading kernel into memory", 0
DISK_ERROR db "Disk read error", 0
SECTORS_ERROR db "Incorrect number of sectors read", 0
MSG_PROT_MODE db "Loaded 32-bit protected mode", 0

times 510-($-$$) db 0
dw 0xAA55
; loader.asm

format ELF

extrn main

public _start

_start:
    call main
    jmp $
// kernel.cpp
void main() {
    char* video_memory = (char*) 0xb8000;
    *video_memory = 'X';
}
rem compile.bat

@echo off

<NUL set /p="Cleaning binaries..."
    del *.bin > NUL
    del *.o > NUL
    del *.elf > NUL
echo Done

<NUL set /p="Compiling boot.asm..."
    fasm boot.asm > NUL
echo Done

<NUL set /p="Compiling loader.asm..."
    fasm loader.asm > NUL
echo Done

<NUL set /p="Compiling kernel.c..."
    wsl gcc -m32 -ffreestanding -c kernel.cpp -o kernel.o
echo Done

<NUL set /p="Linking..."
    wsl objcopy kernel.o -O elf32-i386 kernel.elf
    wsl /usr/local/i386elfgcc/bin/i386-elf-ld -o kernel.bin -Ttext 0x1000 loader.o kernel.elf
    type boot.bin kernel.bin > os_image.bin
echo Done

<NUL set /p="Running..."
    qemu-system-i386 os_image.bin
echo Done

问题不在于代码,而在于你如何构建它。这个序列实际上创建了一个名为 kernel.bin:

的 ELF 可执行文件
wsl objcopy kernel.o -O elf32-i386 kernel.elf
wsl /usr/local/i386elfgcc/bin/i386-elf-ld -o kernel.bin -Ttext 0x1000 loader.o kernel.elf

应该是:

wsl /usr/local/i386elfgcc/bin/i386-elf-ld -o kernel.elf -Ttext 0x1000 loader.o kernel.o
wsl objcopy -O binary kernel.elf kernel.bin

此更改将目标文件链接到 ELF 可执行文件 kernel.elf,然后将 ELF 可执行文件转换为二进制文件 kernel.bin,引导加载程序可以将其加载到地址 0x01000。