首页 文章

跳入保护模式时出现三重故障

提问于
浏览
4

我正在开发一个启动加载程序,它将在切换到保护模式后启动到一个简单的内核 . 我在第四章或第五章的某处使用了this paper作为教程 . 从理论上讲,它应该以16位实模式启动,将内核加载到内存中,切换到32位保护模式并开始执行内核代码 .

但是,当我切换到保护模式并远程跳转或跳转到另一个段时,它会出现三重故障 . 这是主引导扇区代码:

[org 0x7c00]

KERNEL_OFFSET equ 0x1000

mov [BOOT_DRIVE], dl    ;Get the current boot drive from the BIOS

mov bp, 0x9000          ;Set up stack, with enough room to grow downwards
mov sp, bp

mov bx, REAL_MODE_MSG
call print_string

call load_kernel

call switch_to_pm

jmp $                       ;Jump to current position and loop forever

%include "boot/util/print_string.asm"
%include "boot/util/disk.asm"
%include "boot/gdt/gdt.asm"
%include "boot/util/print_string_pm.asm"
%include "boot/switch_to_pm.asm"

[bits 16]
load_kernel:
    mov bx, LOAD_KERNEL_MSG ;Print a message saying we are loading the kernel
    call print_string
    mov bx, KERNEL_OFFSET       ;Set up disk_load routine parameters
    mov dh, 15
    mov dl, [BOOT_DRIVE]
    call disk_load              ;Call disk_load
    ret

[bits 32]
BEGIN_PM:
    mov ebx, PROT_MODE_MSG
    call print_string_pm
    call KERNEL_OFFSET

    jmp $

; Data
BOOT_DRIVE: db 0
REAL_MODE_MSG: db "Started in real mode.", 0
PROT_MODE_MSG: db "Successfully entered 32-bit protected mode.", 0
LOAD_KERNEL_MSG: db "Loading Kernel into memory", 0

; Bootsector padding
times 510-($-$$) db 0
dw 0xaa55

这是GDT:

;Global Descriptor Table
gdt_start:

gdt_null:   ; We need a null descriptor at the start (8 bytes)
    dd 0x0
    dd 0x0

gdt_code:   ; Code segment descriptor
    ; Base=0x0, Limit=0xfffff
    ; 1st flags : (present)1 (privilege)00 (descriptor type)1 -> 1001b
    ; type flags : (code)1 (conforming)0 (readable)1 (accessed)0 -> 1010b
    ; 2nd flags : (granularity)1 (32 - bit default)1 (64 - bit seg)0 (AVL)0 -> 1100b
    dw 0xffff       ; Limit (bits 0-15)
    dw 0x0      ; Base (0-15)
    dw 0x0          ; Base (16-23)
    db 10011010b    ; 1st flags and type flags
    db 11001111b    ; 2nd flags and Limit (16-19)
    db 0x0          ; Base (24-31)

gdt_data:   ; Data segment descriptor
    ;Same as CSD except for type flags
    ; (code)0 (expand down)0 (writable)1 (accessed)0 -> 0010b
    dw 0xffff       ; Limit (bits 0-15)
    dw 0x0          ; Base (0-15)
    dw 0x0          ; Base (16-23)
    db 10010010b    ; 1st flags and type flags
    db 11001111b    ; 2nd flags and Limit (16-19)
    db 0x0          ; Base (24-31)

gdt_end:


;GDT Descriptor
gdt_descriptor:
    dw gdt_end - gdt_start - 1
    dd gdt_start

;Some Constants
CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

以下是切换到保护模式的代码,其中三重故障:

[bits 16]
switch_to_pm:
    cli
    lgdt [gdt_descriptor]   ; load the gdt
    mov eax, cr0            ; turn pm on
    or eax, 0x1
    mov cr0, eax
    jmp CODE_SEG:init_pm    ; THIS IS WHERE THE PROBLEM IS!

[bits 32]
init_pm:
    mov ax, DATA_SEG ; Point segment registers to the data
    mov ds, ax       ; selector defined in the gdt
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ebp, 0x90000 ; Update our stack
    mov esp, ebp
    call BEGIN_PM ;Move on

当我在 jmp CODE_SEG:init_pm 指令之前的某个位置放置 jmp $ 指令空闲时,它会在那里空闲并且不会出现三重故障 . 当我将它放在该指令之后,在标签 init_pm 内,它会发生三重故障 . 所以我很确定这是原因 . 我是GDT的一个问题 . 我是操作系统开发和引导加载程序的新手 . 有关如何解决这个问题的任何建议?

2 回答

  • 2

    Michael Petch在评论中给出了这个问题的正确答案 . 不幸的是,这似乎被几个人遗漏了,因为现在已经发布了三个不正确的答案,其中两个犯了同样的错误 . 在这里,他的评论作为答案发布,希望它更加明显:

    你确定你的GDT是正确的吗?我认为粗略看起来很突出的是每个条目都是9字节(72位) . GDT条目是8个字节(64位) . 似乎你的意思是db 0x0; Base(16-23)而不是dw 0x0;基地(16-23)?注意区别在于dw更改为db . 错误的GDT条目会产生三重故障 .

    Michael Petch也做了一个很好的后续评论,指出了引导程序的其他问题:

    我还建议查看我的一般bootloader提示 . 您可以假设DS(数据段)寄存器在进入时为零(因为您使用组织0x7c00) . 您应该明确地将其设置为零 . 您还以奇怪的方式设置堆栈 . 您将SP设置为9000,但是您没有设置SS,这意味着您并不真正知道将堆栈放在内存中的位置 . 您应该设置SS寄存器,然后设置SP寄存器 . 我的引导程序提示提供了一个示例 .

  • 2

    问题在于你 jmp CODE_SEG:init_pm . 在16位模式下,它是一个4字节跳转到16位地址,如 segment:offset . 但是你需要将6字节远程跳转到32位地址 . 在fasm语法中它将是

    jmp fword CODE_SEG:init_pm
    

    这将向指令添加操作数大小前缀 0x66 ,并将 init_pm 视为32位偏移量 . 不确定如何在nasm中实现相同,但你明白了 .

相关问题