首页 文章

保护模式寻址

提问于
浏览
3

我正在尝试启动加载程序开发教程 . 我能够使用FAT12约定读取stage2引导加载程序 . 现在我试图在实模式下加载内核,然后将其复制到0x0100000地址 .

我在复制跳转到0x0100000时遇到三重故障 . 基本上我无法弄清楚如何访问或跳转到0x0100000 .

我的代码在我使用时有效

IMAGE_PMODE_BASE equ 0x1000
IMAGE_RMODE_BASE equ 0x1000

krnl32.bin: boot/kernel_entry.o ${OBJ}
    i386-elf-ld -o $@ -Ttext 0x01000 $^ --oformat binary

我错过了什么?我读到了描述符:保护模式下的偏移量寻址,它表示DESC:OFFSET(16位) . 使用GDT中的粒度设置,也将偏移量乘以4KB .

我已经设置了这样的gdt:

gdt_start: 
    dd 0                ; null descriptor
    dd 0 

; gdt code:             ; code descriptor
    dw 0FFFFh           ; limit low
    dw 0                ; base low
    db 0                ; base middle
    db 10011010b        ; access
    db 11001111b        ; granularity
    db 0                ; base high

; gdt data:             ; data descriptor
    dw 0FFFFh           ; limit low (Same as code)10:56 AM 7/8/2007
    dw 0                ; base low
    db 0                ; base middle
    db 10010010b        ; access
    db 11001111b        ; granularity
    db 0                ; base high

end_of_gdt:
toc: 
    dw end_of_gdt - gdt_start - 1   ; limit (Size of GDT)
    dd gdt_start            ; base of GDT

; give the descriptor offsets names

NULL_DESC equ 0
CODE_DESC equ 0x8
DATA_DESC equ 0x10

我正在连接内核如下:

krnl32.bin: boot/kernel_entry.o ${OBJ}
    i386-elf-ld -o $@ -Ttext 0x0100000 $^ --oformat binary

阶段2启动加载程序

[bits 16]
[org 0x500]

jmp main

%include "boot/stage2/print16.s"
%include "boot/stage2/print32.s"
%include "boot/stage2/floppy16_driver.s"
%include "boot/stage2/fat12.s"
%include "boot/stage2/gdt.s"
%include "boot/stage2/a20.s"

;*******************************************************
;   Data Section
;*******************************************************

msgFailure db 0x0D, 0x0A, "Failed", 0x00
welcomeMessage db 0x0D, 0x0A, "Landed in STAGE TWO...", 0x00
enableA20Msg db 0x0D, 0x0A, "Enabled A20. Installed GDT", 0x00
ImageName     db "KRNL32  BIN"
ImageSize     db 0
IMAGE_PMODE_BASE equ 0xffff
IMAGE_RMODE_BASE equ 0x1000


main:
    ;-------------------------------;
    ;   Setup segments and stack    ;
    ;-------------------------------;

    cli                    ; clear interrupts
    xor     ax, ax             ; null segments
    mov     ds, ax
    mov     es, ax
    mov     ax, 0x0000         ; stack begins at 0x9000-0xffff
    mov     ss, ax
    mov     sp, 0xFFFF
    sti                    ; enable interrupts

    mov si, welcomeMessage
    call Print16

    call    _EnableA20
    call    InstallGDT
    sti
    mov si, enableA20Msg
    call Print16

    call    LoadRoot
    mov     ebx, 0
    mov     ebp, IMAGE_RMODE_BASE
    mov     esi, ImageName
    call    LoadFile        ; load our file
    mov     dword [ImageSize], ecx
    cmp     ax, 0
    je      EnterStage3
    mov     si, msgFailure
    call    Print16
    mov     ah, 0
    int     0x16                    ; await keypress
    int     0x19                    ; warm boot computer

    jmp $;

EnterStage3:

    cli   
    mov eax, cr0
    or  eax, 1
    mov cr0, eax

    jmp CODE_DESC:Stage3           


[bits 32]
Stage3:
    mov ax, DATA_DESC       ; set data segments to data selector (0x10)
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov esp, 90000h     ; stack begins from 90000h

CopyImage:
    mov eax, dword [ImageSize]
    movzx   ebx, word [bpbBytesPerSector]
    mul ebx
    mov ebx, 4
    div ebx
    cld
    mov    esi, IMAGE_RMODE_BASE
    mov edi, IMAGE_PMODE_BASE
    mov ecx, eax
    rep movsd                   ; copy image to its protected mode address
    jmp IMAGE_PMODE_BASE
    jmp $;

1 回答

  • 3

    您设置了一个GDT,它具有代码的32位描述符和数据的32位描述符 . 在保护模式下,寄存器CS / DS / ES / SS / FS / GS不再是在实模式下看到的段寄存器 . 在保护模式下,它们包含一个指向GDT(或LDT)中的条目的选择器 . 此代码加载数据寄存器:

    mov ax, DATA_DESC       ; set data segments to data selector (0x10)
    mov ds, ax
    mov ss, ax
    mov es, ax
    

    您不能像这样设置CS寄存器 . FAR jmp可以设置所需的代码段选择器,并在一条指令中设置偏移量 . 这就是这个指令的作用:

    jmp CODE_DESC:Stage3
    

    您的GDT设置了代码和数据描述符,这些描述符是4GB平面内存模型,其中基数为0x00000000,限制为0xffffffff . 这意味着一旦处于保护模式并且您正在使用指向这些描述符的选择器,您可以直接从0x00000000到0xffffffff访问所有内存 . 这意味着您需要做的就是 jmp 0x100000 跳转到内存地址0x100000 . 在您的代码中,您将不仅要使用0x100000作为跳转位置,还要使用内存副本用作其基础的内存位置 .

    考虑到这一点,简单的解决方法是改变:

    IMAGE_PMODE_BASE equ 0x1000
    

    至:

    IMAGE_PMODE_BASE equ 0x100000
    

相关问题