【问题标题】:Cannot set GS register in Protected Mode无法在保护模式下设置 GS 寄存器
【发布时间】:2016-11-05 17:48:22
【问题描述】:

我正在根据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=0x18P 被写入线性地址0x0。奇怪的是,HLT 循环正常工作。我是 x86 程序集的新手,所以我不知道问题出在哪里。代码有什么问题?怎么解决?

【问题讨论】:

    标签: assembly x86 nasm bootloader osdev


    【解决方案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 描述符的方式可行,但另一种选择是使用零基数。这可以通过以下方式完成:

    • 删除这些行:
    ; 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
    
    • 修改ptr16:32 FAR JMP以使用entry_32的偏移量:
    jmp dword SELECTOR_CODE:entry_32 ; -> protected mode!!!
    

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

    【讨论】:

      【解决方案2】:

      从代码中,我看到你有:

      SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
      

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

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

      不确定你的意图是什么,但我认为这不是你想要的地方。你想往哪里跳?

      【讨论】:

      • 在评论中问这个问题可能比在答案中问得好。 SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0 是正确的。它根据DESCRIPTOR_CODE 与GDT0 的距离定义代码选择器。适合描述符。我没有机会查看代码的正确性,但我可以说他试图动态创建 CODE 描述符,以 entry_32 而不是 0 为基数。理论上如果一切都设置正确@ 987654325@ 会跳转到 0x08:0x00000000。由于描述符的基础是entry_32,它应该跳转到entry_32:
      • 这实际上是有效的,因为环 0 GDT 选择器恰好具有与其在 GDT 中的偏移量相同的值。选择器的位 0 和 1 是请求的特权级别 (RPL),位 2 对于 GDT 选择器是清除的,并为 LDT 选择器设置。位 3-15 是 GDT/LDT 的索引。由于 GDT/LDT 条目的长度为 8 个字节,这意味着第二个 GDT 条目的偏移量为 8,而其选择器值也是 8(索引 1,GDT 选择器,RPL 0)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2015-06-24
      • 2016-04-06
      • 2020-11-04
      • 2015-09-25
      • 2021-10-12
      相关资源
      最近更新 更多