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

时间:2016-07-04 04:57:17

标签: assembly x86 nasm bootloader osdev

我正在编写一个基于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装配的新手,所以我无法弄清楚问题出在哪里。代码有什么问题?如何解决?

2 个答案:

答案 0 :(得分:5)

潜在的错误

我相信你的大部分问题都源于你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偏移量和非零描述符基数。然后我们可以使用ptr16:32 FAR JMP标签entry_32的偏移量。通过保持 CS = DS = ES = SS ,您可以使用4gb线性地址空间来简化代码保持一致可以避免将来潜在的编码错误。

答案 1 :(得分:1)

从代码中,我看到你有:

rdi

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

SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0

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

相关问题