无法在保护模式下设置 GS 寄存器
Cannot set GS register in Protected Mode
我正在根据 OSDev Wiki 和一些相关书籍中的信息编写操作系统。发现跳入Protected Mode后,段初始化例程没有正常执行。这是我的第二阶段加载程序和内核代码:
; 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 汇编的新手,所以我不知道问题出在哪里。代码有什么问题?如何解决?
从代码中,我看到你有:
SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
这给出了 GDT0 的 "length"。然后你打电话
jmp dword SELECTOR_CODE:0 ; -> protected mode!!!
不确定你的意图是什么,但我不认为那是在你想要的地方跳转。你想跳到哪里?
潜在错误
我相信您的大部分问题都源于某些 GDT descriptors 中的标志问题。在 CODE 描述符中 table 你有:
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 1111
是 0xcf
,而不是 0x9f
。您还可以使用二进制值定义 DBs:
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 need ES being set, it is a good idea to have ES=DS. If you use any of the instructions that require a direction flag it is a good idea to explicitly set it. Most code assumes forward movement that can be set using the 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
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 你通过使用从 0 开始的 4gb 线性地址 space 来简化代码。保持一致可以避免将来出现潜在的编码错误。
我正在根据 OSDev Wiki 和一些相关书籍中的信息编写操作系统。发现跳入Protected Mode后,段初始化例程没有正常执行。这是我的第二阶段加载程序和内核代码:
; 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 汇编的新手,所以我不知道问题出在哪里。代码有什么问题?如何解决?
从代码中,我看到你有:
SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
这给出了 GDT0 的 "length"。然后你打电话
jmp dword SELECTOR_CODE:0 ; -> protected mode!!!
不确定你的意图是什么,但我不认为那是在你想要的地方跳转。你想跳到哪里?
潜在错误
我相信您的大部分问题都源于某些 GDT descriptors 中的标志问题。在 CODE 描述符中 table 你有:
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 1111
是 0xcf
,而不是 0x9f
。您还可以使用二进制值定义 DBs:
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 need ES being set, it is a good idea to have ES=DS. If you use any of the instructions that require a direction flag it is a good idea to explicitly set it. Most code assumes forward movement that can be set using the 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
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 你通过使用从 0 开始的 4gb 线性地址 space 来简化代码。保持一致可以避免将来出现潜在的编码错误。