Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 无法在保护模式下设置GS寄存器_Assembly_X86_Nasm_Bootloader_Osdev - Fatal编程技术网

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

Assembly 无法在保护模式下设置GS寄存器,assembly,x86,nasm,bootloader,osdev,Assembly,X86,Nasm,Bootloader,Osdev,我正在根据和一些相关书籍中的信息编写一个操作系统。我发现跳入保护模式后,段初始化例程没有正确执行。下面是我的第二阶段加载程序和内核代码: ; 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~

我正在根据和一些相关书籍中的信息编写一个操作系统。我发现跳入保护模式后,段初始化例程没有正确执行。下面是我的第二阶段加载程序和内核代码:

; 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
db 0x9a ; 1001 1011 - Vaild, ring 0, executable, strict, readable,
db 0x9f ; 1100 1111 - paging, 32-bit
这给出了GDT0的“长度”。然后你打电话

jmp dword SELECTOR_CODE:0 ; -> protected mode!!!
我不知道你的意图是什么,但我不认为这是你想要的。您想跳转到哪里?

潜在的bug 我相信你们的大部分问题都源于你们一些国家的国旗问题。在代码描述符表中,您有:

SELECTOR_CODE equ DESCRIPTOR_CODE - GDT0
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
db 10010010b  ; valid, ring 0, data, forward, writable, accessed
db 11001111b  ; 4KB, 32-bits
尤其是
1100 1111
0xcf
,而不是
0x9f
。您还可以使用二进制值定义DBs,如:

db 10011010b  ; Valid, ring 0, executable, strict, readable,
db 11001111b  ; paging, 32-bit
通过使用二进制值这样做,它不容易出错,并且可读性更好

在您拥有的数据描述符中:

db 0x92 ; 1001 0011 - vaild, ring 0, data, forward, writable, accessed
db 0x9F ; 1100 1111 - 4KB, 32-bits
我认为应该是:

db 0x9a ; 1001 1010 - Valid, ring 0, executable, strict, readable,
db 0xcf ; 1100 1111 - paging, 32-bit
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
可以是任何有效的线性地址。由于某些指令(如ES)需要设置,因此最好设置ES=DS。如果使用任何需要方向标志的指令,最好明确设置方向标志。大多数代码假定可以使用指令设置向前移动

作为一个实验,您可以添加此代码以显示内存中的字符串。这将测试为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

代码段建议 尽管设置代码描述符的方式可以工作,但另一种选择是使用零基数。这可以通过以下方式实现:

  • 删除这些行:
  • 修改ptr16:32 FAR JMP以使用
    条目的偏移量\u 32

我们只使用描述符基0,而不是0偏移量和非零描述符基。然后,我们可以使用标签
条目_32
的偏移量。通过保持CS=DS=ES=SS,您可以使用从0开始的4gb线性地址空间来简化代码。保持一致可能会避免将来潜在的编码错误。

在评论中提出这一问题可能比回答问题更好<代码>选择器\代码等效描述符\代码-GDT0正确。它根据描述符_代码到GDT0的距离定义代码选择器。该方法适用于描述符。我还没有机会查看代码的正确性,但我可以说,他试图动态创建代码描述符,以
entry_32
为基数,而不是以0为基数。理论上,如果一切都设置正确,jmp dword选择器_code:0将跳转到0x08:0x00000000。由于描述符的基是
entry\u 32
,它应该跳转到
entry\u 32:
,这实际上是有效的,因为环0 GDT选择器恰好与其在GDT中的偏移量具有相同的值。选择器的位0和1是请求的特权级别(RPL),位2对于GDT选择器是清除的,对于LDT选择器是设置的。位3-15是GDT/LDT的索引。由于GDT/LDT条目的长度为8字节,这意味着第二个GDT条目位于偏移量8处,而其选择器值也是8(索引1,GDT选择器,RPL 0)。