首页 文章

无法在保护模式下设置GS寄存器

提问于
浏览
3

我正在编写一个基于OSDev Wiki和一些相关书籍的信息的操作系统 . 我发现在跳转到保护模式后,段初始化例程没有正确执行 . 这是我的第二阶段加载器和内核代码:

; stage2
; still -f bin
[org 0xd200] ; calculated by first stage loader

; GDTs are defined here
jmp entry

GDT_DESC_START:

GDT0:
dw 0 ; Limit 0~15
dw 0 ; Base 0~15
db 0 ; Base 16~23
db 0 ; Access Byte
db 0 ; Limit 16~19 + Flags
db 0 ; Base 24~31

DESCRIPTOR_CODE:
dw 0xffff ; Limit 0~15
CODE_BASE_AX dw 0 ; Base 0~15
CODE_BASE_EAHL db 0 ; Base 16~23
db 0x9a ; 1001 1011 - Vaild, ring 0, executable, strict, readable,
; accessed
db 0x9f; 1100 1111 - paging, 32-bit
CODE_BASE_EAHH db 0 ; Base 24~31

DESCRIPTOR_DATA: 
dw 0xffff ; Limit 0~15
dw 0x0000 ; Base 0~15
db 0x00 ; Base 16~23
db 0x92 ; 1001 0011 - vaild, ring 0, data, forward, writable, accessed
db 0x9F ; 1100 1111 - 4KB, 32-bits
db 0x0 ; Base 24~31

; Note that in further development, VRAM descriptor should be
; created dynamically
; Base = 0x000b8000
DESCRIPTOR_VIDEO:
dw 0xffff
dw 0x8000
db 0x0b
db 0x92
db 0x4F
db 0x00

GDT_DESC_END:

GDTR0:
        dw GDT_DESC_END - GDT_DESC_START - 1
        dd GDT_DESC_START

SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
SELECTOR_DATA equ DESCRIPTOR_DATA - GDT0
SELECTOR_VIDEO equ DESCRIPTOR_VIDEO - GDT0

; --- code ------------

[bits 16]

entry:

cli
; open a20 gate
in al, 0x92
or al, 0x02
and al, ~1
out 0x92, al
; goto protected mode
; rewrite code descriptor
xor eax, eax
mov eax, entry_32
mov [CODE_BASE_AX], ax
shr eax, 16
mov [CODE_BASE_EAHH], ah
mov [CODE_BASE_EAHL], al
; load the gdt
lgdt [GDTR0]
mov eax, cr0
or eax, 1
mov cr0, eax
jmp dword SELECTOR_CODE:0 ; -> protected mode!!!

[bits 32]
entry_32:
mov ax, SELECTOR_DATA
mov ds, ax
mov ss, ax
mov ax, SELECTOR_VIDEO
mov gs, ax

; print a 'P'
xor ax, ax
mov edi, 0
mov ah, 0x07
mov al, 'P'
mov [gs:edi], ax

.sleep:
        hlt
        jmp .sleep

P 没有打印在屏幕上,我发现 GS=0 而不是 GS=0x18 . P 被写入线性地址 0x0 . 奇怪的是, HLT 循环正常工作 . 我是x86程序集的新手,所以我无法弄清楚问题出在哪里 . 代码有什么问题?怎么修好?

2 回答

  • 1

    潜在的错误

    我相信你的大多数问题都源于你们某些GDT descriptors中的旗帜问题 . 在CODE描述符表中,您有:

    db 0x9a ; 1001 1011 - Vaild, ring 0, executable, strict, readable,
    db 0x9f ; 1100 1111 - paging, 32-bit
    

    应该是:

    db 0x9a ; 1001 1010 - Valid, ring 0, executable, strict, readable,
    db 0xcf ; 1100 1111 - paging, 32-bit
    

    特别是 1100 11110xcf ,而不是 0x9f . 您也可以使用二进制值定义DB,例如:

    db 10011010b  ; Valid, ring 0, executable, strict, readable,
    db 11001111b  ; paging, 32-bit
    

    通过这种方式使用二进制值可以减少错误并且更具可读性 .

    在DATA描述符中,您有:

    db 0x92 ; 1001 0011 - vaild, ring 0, data, forward, writable, accessed
    db 0x9F ; 1100 1111 - 4KB, 32-bits
    

    我相信应该是:

    db 10010010b  ; valid, ring 0, data, forward, writable, accessed
    db 11001111b  ; 4KB, 32-bits
    

    你的 0x9F 应该是 0xCF (11001111)


    其他考虑因素

    进入保护模式后,最好设置SS:ESP . 像这样的代码可以工作:

    mov es, ax                       ; Set ES as well
    mov ss, ax
    mov esp,0x90000                  ; We should specify a stack pointer
    cld                              ; Set direction flag forward
    

    0x90000 可以是任何有效的线性地址 . 由于某些指令如MOVSB需要设置ES,因此最好使用ES = DS . 如果您使用任何需要方向标志的指令,最好明确设置它 . 大多数代码都假定可以使用CLD指令设置的向前移动 .

    作为实验,您可以添加此代码以显示内存中的字符串 . 这将测试为DS生成的偏移按预期工作;并测试GS段是否指向0xb8000处彩色文本视频内存的基础 .

    ; print "Hello, World!"
    xor ax, ax
    mov edi, 0
    mov ecx, 0
    mov ah, 0x07
    
    ; Display a nul terminated string from a variable to video memory
    ; using a character attribute in AH
    strloop:
    mov al, [mystr+ecx]
    test al,al
    je .endstrloop
    mov [gs:edi+ecx*2], ax
    inc ecx
    jmp strloop
    .endstrloop:
    

    修订后的代码

    修改后的stage2代码可能如下所示:

    ; stage2
    ; still -f bin
    [org 0xd200] ; calculated by first stage loader
    
    ; GDTs are defined here
    jmp entry
    
    GDT_DESC_START:
    
    GDT0:
    dw 0 ; Limit 0~15
    dw 0 ; Base 0~15
    db 0 ; Base 16~23
    db 0 ; Access Byte
    db 0 ; Limit 16~19 + Flags
    db 0 ; Base 24~31
    
    DESCRIPTOR_CODE:
    dw 0xffff ; Limit 0~15
    CODE_BASE_AX dw 0 ; Base 0~15
    CODE_BASE_EAHL db 0 ; Base 16~23
    db 10011010b ; Valid, ring 0, executable, strict, readable,
    ; accessed
    db 11001111b ; paging, 32-bit
    CODE_BASE_EAHH db 0 ; Base 24~31
    
    DESCRIPTOR_DATA:
    dw 0xffff ; Limit 0~15
    dw 0x0000 ; Base 0~15
    db 0x00 ; Base 16~23
    db 10010010b ; valid, ring 0, data, forward, writable, accessed
    db 11001111b ; 4KB, 32-bits
    db 0x0 ; Base 24~31
    
    ; Note that in further development, VRAM descriptor should be
    ; created dynamically
    ; Base = 0x000b8000
    DESCRIPTOR_VIDEO:
    dw 0xffff
    dw 0x8000
    db 0x0b
    db 0x92
    db 0x4F
    db 0x00
    
    GDT_DESC_END:
    
    GDTR0:
            dw GDT_DESC_END - GDT_DESC_START - 1
            dd GDT_DESC_START
    
    SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
    SELECTOR_DATA equ DESCRIPTOR_DATA - GDT0
    SELECTOR_VIDEO equ DESCRIPTOR_VIDEO - GDT0
    
    ; --- code ------------
    
    [bits 16]
    
    entry:
    
    cli
    ; open a20 gate
    in al, 0x92
    or al, 0x02
    and al, ~1
    out 0x92, al
    
    ; goto protected mode
    ; rewrite code descriptor
    xor eax, eax
    mov eax, entry_32
    mov [CODE_BASE_AX], ax
    shr eax, 16
    mov [CODE_BASE_EAHH], ah
    mov [CODE_BASE_EAHL], al
    
    ; load the gdt
    lgdt [GDTR0]
    mov eax, cr0
    or eax, 1
    mov cr0, eax
    jmp dword SELECTOR_CODE:0x0      ; -> protected mode!!!
    
    [bits 32]
    entry_32:
    
    mov ax, SELECTOR_DATA
    mov ds, ax
    mov es, ax                       ; Set ES as well
    mov ss, ax
    mov esp,0x90000                  ; We should specify a stack pointer
    mov ax, SELECTOR_VIDEO
    mov gs, ax
    cld                              ; Set direction flag forward
    
    ; print "Hello, World!"
    xor ax, ax
    mov edi, 0
    mov ecx, 0
    mov ah, 0x07
    
    ; Display a nul terminated string from a variable to video memory
    ; using a character attribute in AH
    strloop:
    mov al, [mystr+ecx]
    test al,al
    je .endstrloop
    mov [gs:edi+ecx*2], ax
    inc ecx
    jmp strloop
    .endstrloop:
    
    .sleep:
            hlt
            jmp .sleep
    
    mystr: db "Hello, World!",0
    

    虽然你的问题没有第一阶段,但我为想要测试的读者提供了一个简单的问题:

    [BITS 16]
    org 0x7C00
    
            STAGE2OFFSET equ 0xd200
    
    start:
            ; This section of code is added based on Michael Petch's bootloader tips
            ; See http://stackoverflow.com/a/32705076/3857942
            xor ax,ax          ; We want a segment of 0 for DS for this question
            mov ds,ax          ;     Set AX to appropriate segment value for your situation
            mov es,ax          ; In this case we'll default to ES=DS
            mov bx,0x7c00      ; Stack segment can be any usable memory
    
            cli                ; Disable interrupts to circumvent bug on early 8088 CPUs
            mov ss,ax      
            mov sp,bx          ; This places top of the stack @ 0x0000:0x7c00
                               ;    just below the bootsector.
            sti                ; Re-enable interrupts
            cld                ; Set the direction flag to be positive direction
    
            mov ah, 0x02       ; Read sectors from drive
            mov al, 4          ; Read 4 sectors
            mov ch, 0          ; Cylinder 0
            mov cl, 2          ; Sector 2
            mov dh, 0          ; Head 0
            xor bx, bx
            mov es, bx
            mov bx, STAGE2OFFSET    ; ES:BX = 0x0000:0xd200 (start of memory to read into)
            int 0x13
    
            jmp 0x0000:STAGE2OFFSET ; Far Jump to second stage. Sets CS=0
    
    times   510-($-$$) db 0         ; Create padding to fill out to 510 bytes
    dw      0xaa55                  ; Magic number in the trailer of a boot sector
    

    可以使用以下命令将这些文件组装并构建到磁盘映像中:

    dd if=/dev/zero of=floppy.img bs=1024 count=1440
    nasm -f bin bootload.asm -o bootload.bin
    nasm -f bin stage2.asm -o stage2.bin
    
    dd if=bootload.bin of=floppy.img conv=notrunc
    dd if=stage2.bin of=floppy.img bs=512 seek=1 conv=notrunc
    

    代码段建议

    虽然您设置CODE描述符的方式可行,但另一种方法是使用零基数 . 这可以通过以下方式实现:

    • remove 这些行:

    ;重写代码描述符
    xor eax,eax
    mov eax,entry_32
    mov [CODE_BASE_AX],ax
    shr eax,16岁
    mov [CODE_BASE_EAHH],啊
    mov [CODE_BASE_EAHL],al

    • 修改ptr16:32 FAR JMP以使用 entry_32 的偏移量:

    jmp dword SELECTOR_CODE:entry_32; - >保护模式!!!

    我们只使用0的描述符基数而不是0偏移量和非零描述符基数 . 然后我们可以使用标签 entry_32 的偏移量来表示ptr16:32 FAR JMP . 通过保持CS = DS = ES = SS,您可以通过使用从0开始的4gb线性地址空间来简化代码 . 保持一致可以避免将来潜在的编码错误 .

  • 5

    从代码中,我看到你有:

    SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
    

    这给出了GDT0的“长度” . 然后你打电话

    jmp dword SELECTOR_CODE:0 ; -> protected mode!!!
    

    不确定你的意图是什么,但我不认为那是你想要跳的地方 . 你想在哪里跳?

相关问题