Assembly 从引导加载程序进入保护模式时出现问题

Assembly 从引导加载程序进入保护模式时出现问题,assembly,x86,nasm,bootloader,protected-mode,Assembly,X86,Nasm,Bootloader,Protected Mode,此程序集代码从引导加载程序进入受保护模式,但在调用远跳转并重新启动后,无法重置CS段(或执行远跳转)。如果我删除远跳转,它将在保护模式下进入无限循环(0x66,jmp$),无需重新启动 [bits 16] [org 0x7c00] xor ax,ax xor eax,eax add eax,ENTRY_POINT_32 ;address to plug to far jmp mov [ENTRY_OFF],eax xor eax,eax mov eax,GDT ;l

此程序集代码从引导加载程序进入受保护模式,但在调用远跳转并重新启动后,无法重置CS段(或执行远跳转)。如果我删除远跳转,它将在保护模式下进入无限循环(0x66,jmp$),无需重新启动

[bits 16]
[org 0x7c00]
xor ax,ax
xor eax,eax
add eax,ENTRY_POINT_32 ;address to plug to far jmp
mov [ENTRY_OFF],eax
xor eax,eax
mov eax,GDT                ;load GDT label address
mov [GDTR+2],eax ; load it into address space in GDTR
lgdt [GDTR]                   ;load GDTR
cli                                    ;turn off masked interrupts
in al,0x70
or al,0x80
out 0x70,al                     ;turn off nonmasked interrupts
in al,0x92
or al,2
out 0x92, al ;open line A20 (change address 20 to 32 bits)
mov eax,cr0
or al,1
mov cr0,eax                 ;switch to protected mode
db 0x66                        ;prefix of opcode to change bitness
db 0xEA                       ;opcode of jmp far
ENTRY_OFF dd 0x0 ;32 bit offset of 32 bit instructions
dw 00001000b ; selector 1st descriptor CODE_descr,=1
ENTRY_POINT_32:
db 0x66                      ;prefix of opcode to change bitness
jmp $                          ;infinite jump to the same location
GDT:
NULL_descr dd 0x0,0x0 ; must be present in GDT
CODE_descr db  0xFF,0xFF,0x0,0x0,0x0,10011010b,11001111b,0x0
;descriptor of 32 bit code segment, base 0, size ffffffff
DATA_descr db 0xFF,0xFF,0x0,0x0,0x0,10010010b,11001111b,0x0
;descriptor of 32 bit data segment, base 0, size ffffffff
VIDEO_descr 0xFF,0xFF,0x0,0x80,0x0B,10010010b,01000000b,0x0
;descriptor of video buffer, base 0x000B8000, size ffff
GDT_size db $-GDT ;size of GDT table
GDTR dw GDT_size-1 ;next 3 words are size &
dd 0x0 ;address of beginning of GDT, loaded in code
times 510 - ($ - $$) db 0
dw 0xaa55

wasm.in的原始代码,稍作修改。

在实模式下,所有内存操作数上都有一个隐含段。如果内存操作数不包含BP作为基,则隐含段为DS。如果内存操作数不包含BP,则隐含的基数为SS。内存操作数不使用BP,因此隐含的段是DS。具有如下内存操作数的指令:

mov [ENTRY_POINT_32],eax
bits 16
org 0x7c00

start:
    xor ax,ax
    mov ds,ax                   ; Explicitly set DS to zero

    lgdt [GDTR]                 ; load GDTR

    cli                         ; turn off masked interrupts
    in al,0x70
    or al,0x80
    out 0x70,al                 ; turn off nonmasked interrupts
    in al,0x92
    or al,2
    out 0x92, al                ; enable A20 line

    ; Enter protected mode
    mov eax,cr0
    or al,1
    mov cr0,eax                 ; switch to protected mode
    jmp CODE32_SEL:ENTRY_POINT_32

bits 32
ENTRY_POINT_32:
    mov eax, DATA32_SEL         ; Set the protected mode selector
    mov ds, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    mov esp, 0x9C000            ; Set protected mode stack below EBDA

    mov eax, VIDEO32_SEL        ; Set the video memory selector
    mov es, ax

    ; Print some characters to top left of the screen in white on magenta
    xor ebx, ebx
    mov word [es:ebx],   0x57 << 8 | 'M'
    mov word [es:ebx+2], 0x57 << 8 | 'D'
    mov word [es:ebx+4], 0x57 << 8 | 'P'

    jmp $                       ; infinite jump to the same location

GDT:
    NULL_descr: dd 0x0,0x0      ; must be first entry in GDT

    ; descriptor of 32 bit code segment, base 0, size ffffffff
    CODE_descr: db  0xFF,0xFF,0x0,0x0,0x0,10011010b,11001111b,0x0

    ; descriptor of 32 bit data segment, base 0, size ffffffff
    DATA_descr: db 0xFF,0xFF,0x0,0x0,0x0,10010010b,11001111b,0x0

    VIDEO_descr: db 0xFF,0xFF,0x0,0x80,0x0B,10010010b,01000000b,0x0
    ; descriptor of video buffer, base 0x000B8000, size ffff
GDT_END:
CODE32_SEL  equ CODE_descr-GDT
DATA32_SEL  equ DATA_descr-GDT
VIDEO32_SEL equ VIDEO_descr-GDT

GDTR dw GDT_END-GDT-1            ; Size of GDT (minus 1)
     dd GDT                      ; address of beginning of GDT

times 510 - ($ - $$) db 0
dw 0xaa55
相当于:

mov [ds:ENTRY_POINT_32],eax

实模式用于到达物理内存地址。如果DS错误,您将写入错误的内存位置。20位物理地址=(段)您没有设置
DS
以便
mov[ENTRY\u OFF],eax
写谁知道在哪里。你应该做一个
mov ds,ax
作为零
ds
的第二条指令。用一个已知的零操作数使用
add
是愚蠢的,而且无论如何,你可以直接将偏移量插入
ENTRY\u OFF
。显然,你可以使用一个正常的16位远跳转,你甚至不需要手动创建一个32位的过程中遇到麻烦。
CS
是设置的,无论您使用16位还是32位。大小只是控制偏移量,但在这种情况下,16位显然足够了,因为您的
入口点\u 32
在前64k之内。是的。每个内存访问都有一个隐式段。请参见表3-5。t中的默认段选择规则基本架构手册。请注意,说明中有
8
编码(您认为
dw 00001000b
是什么?)还有你的
add
中的偏移量。所以你无论如何都需要更新一些东西。如果你忘了你被搞砸了。简单的解决方案不会更糟。见鬼,显然你甚至没有意识到你需要更改
00001000b
LOL。通过使所有相关的源代码行彼此靠近。例如,您可以将
code32_seg eq 8
放在GDT定义附近。code32_SEL eq code_descr-GDT这将为选择器分配GDT条目之间的字节大小差,而选择器是GDT中条目的线性计数。否?@user145453 GDT由8个描述符条目组成每个字节。选择器是您在保护模式下放入段寄存器的值。每个描述符的开始和GDT的开始之间的差异恰好是选择器值。因此CODE32_SEL将为8(0x08),DATA32_SEL将为16(0x10),VIDEO 32_SEL将为24(0x18)。这样做的好处是,如果您切换每个8字节描述符的顺序,选择器值将自动匹配。感谢您,解释了为什么选择器位是这样定位的。已达到粉色MDP检查点!而不是
jmp dword far[bp]
您可以使用
o32 retf
将远端地址放在最顶端的堆栈插槽中。@ecm:这是真的,我通常使用jmp而不是
ret
技巧有一个微妙的原因,那就是在许多处理器上执行ret时,如果没有相应的调用,可能会招致惩罚,因为它会使空间,我绝对会做
o32 retf
。我这样做主要是出于习惯。你会发现我的许多答案在存在选择的地方做了类似的事情。
db 0x66                      ;prefix of opcode to change bitness
GDT_size db $-GDT ;size of GDT table
GDTR dw GDT_size-1 ;next 3 words are size &
GDT:
    NULL_descr: dd 0x0,0x0      ; must be first entry in GDT

    ; descriptor of 32 bit code segment, base 0, size ffffffff
    CODE_descr: db  0xFF,0xFF,0x0,0x0,0x0,10011010b,11001111b,0x0

    ; descriptor of 32 bit data segment, base 0, size ffffffff
    DATA_descr: db 0xFF,0xFF,0x0,0x0,0x0,10010010b,11001111b,0x0

    ; descriptor of video buffer, base 0x000B8000, size ffff
    VIDEO_descr: db 0xFF,0xFF,0x0,0x80,0x0B,10010010b,01000000b,0x0
GDT_END:

GDTR dw GDT_END-GDT-1            ; Size of GDT (minus 1)
     dd 0x0                      ; address of beginning of GDT, loaded in code
    mov [ENTRY_OFF],eax
    jmp clear_prefetch          ; Clear the instruction prefetch queue
                                ;     by jumping to next instruction
clear_prefetch:
bits 16
org 0x7c00

start:
    xor eax,eax
    mov ds, ax                  ; Explicitly set DS to zero

    add eax,ENTRY_POINT_32      ; address to plug to far jmp
    mov [ENTRY_OFF],eax
    jmp clear_prefetch          ; Clear the instruction prefetch queue
                                ;     by jumping to next instruction
clear_prefetch:

    xor eax,eax
    mov eax,GDT                 ; load GDT label address
    mov [GDTR+2],eax            ; load it into address space in GDTR
    lgdt [GDTR]                 ; load GDTR

    cli                         ; turn off masked interrupts
    in al,0x70
    or al,0x80
    out 0x70,al                 ; turn off nonmasked interrupts
    in al,0x92
    or al,2
    out 0x92, al                ; enable A20 line
    mov eax,cr0
    or al,1
    mov cr0,eax                 ; switch to protected mode

    db 0x66                     ; prefix of opcode to change bitness
    db 0xEA                     ; opcode of jmp far
ENTRY_OFF:
    dd 0x0                      ; 32 bit offset of 32 bit instructions
    dw 00001000b                ; selector 1st descriptor CODE_descr,=1

bits 32
ENTRY_POINT_32:
    jmp $                       ; infinite jump to the same location

GDT:
    NULL_descr: dd 0x0,0x0      ; must be first entry in GDT

    ; descriptor of 32 bit code segment, base 0, size ffffffff
    CODE_descr: db  0xFF,0xFF,0x0,0x0,0x0,10011010b,11001111b,0x0

    ; descriptor of 32 bit data segment, base 0, size ffffffff
    DATA_descr: db 0xFF,0xFF,0x0,0x0,0x0,10010010b,11001111b,0x0

    ; descriptor of video buffer, base 0x000B8000, size ffff
    VIDEO_descr: db 0xFF,0xFF,0x0,0x80,0x0B,10010010b,01000000b,0x0
GDT_END:

GDTR dw GDT_END-GDT-1            ; Size of GDT (minus 1)
     dd 0x0                      ; address of beginning of GDT, loaded in code

times 510 - ($ - $$) db 0
dw 0xaa55
bits 16
org 0x7c00

start:
    xor ax,ax
    mov ds,ax                   ; Explicitly set DS to zero

    lgdt [GDTR]                 ; load GDTR

    cli                         ; turn off masked interrupts
    in al,0x70
    or al,0x80
    out 0x70,al                 ; turn off nonmasked interrupts
    in al,0x92
    or al,2
    out 0x92, al                ; enable A20 line

    ; Enter protected mode
    mov eax,cr0
    or al,1
    mov cr0,eax                 ; switch to protected mode
    jmp CODE32_SEL:ENTRY_POINT_32

bits 32
ENTRY_POINT_32:
    mov eax, DATA32_SEL         ; Set the protected mode selector
    mov ds, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    mov esp, 0x9C000            ; Set protected mode stack below EBDA

    mov eax, VIDEO32_SEL        ; Set the video memory selector
    mov es, ax

    ; Print some characters to top left of the screen in white on magenta
    xor ebx, ebx
    mov word [es:ebx],   0x57 << 8 | 'M'
    mov word [es:ebx+2], 0x57 << 8 | 'D'
    mov word [es:ebx+4], 0x57 << 8 | 'P'

    jmp $                       ; infinite jump to the same location

GDT:
    NULL_descr: dd 0x0,0x0      ; must be first entry in GDT

    ; descriptor of 32 bit code segment, base 0, size ffffffff
    CODE_descr: db  0xFF,0xFF,0x0,0x0,0x0,10011010b,11001111b,0x0

    ; descriptor of 32 bit data segment, base 0, size ffffffff
    DATA_descr: db 0xFF,0xFF,0x0,0x0,0x0,10010010b,11001111b,0x0

    VIDEO_descr: db 0xFF,0xFF,0x0,0x80,0x0B,10010010b,01000000b,0x0
    ; descriptor of video buffer, base 0x000B8000, size ffff
GDT_END:
CODE32_SEL  equ CODE_descr-GDT
DATA32_SEL  equ DATA_descr-GDT
VIDEO32_SEL equ VIDEO_descr-GDT

GDTR dw GDT_END-GDT-1            ; Size of GDT (minus 1)
     dd GDT                      ; address of beginning of GDT

times 510 - ($ - $$) db 0
dw 0xaa55
; Assemble with NASM as
;     nasm -f bin enterpm.asm -o enterpm.com

STACK32_TOP EQU 0x200000
CODE32_REL  EQU 0x110000
VIDEOMEM    EQU 0x0b8000

use16
; COM program CS=DS=SS
org 100h

    call check_pmode    ; Check if we are already in protected mode
                        ;    This may be the case if we are in a VM8086 task.
                        ;    EMM386 and other expanded memory manager often
                        ;    run DOS in a VM8086 task. DOS extenders will have
                        ;    the same effect

    jz not_prot_mode    ; If not in protected mode proceed to switch
    mov dx, in_pmode_str;    otherwise print an error and exit back to DOS
    mov ah, 0x9
    int 0x21            ; Print Error
    ret

not_prot_mode:
    call a20_on         ; Enable A20 gate (uses Fast method as proof of concept)
    cli

    ; Compute linear address of label gdt_start
    ; Using (segment << 4) + offset
    mov eax,cs          ; EAX = CS
    shl eax,4           ; EAX = (CS << 4)
    mov ebx,eax         ; Make a copy of (CS << 4)
    add [gdtr+2],eax    ; Add base linear address to gdt_start address
                        ;     in the gdtr
    lgdt [gdtr]         ; Load gdt

    ; Compute linear address of label code_32bit
    ; Using (segment << 4) + offset
    add ebx,code_32bit  ; EBX = (CS << 4) + code_32bit

    push dword 0x08     ; CS Selector
    push ebx            ; Linear offset of code_32bit
    mov bp, sp          ; m16:32 address on top of stack, point BP to it

    mov eax,cr0
    or eax,1
    mov cr0,eax         ; Set protected mode flag

    jmp dword far [bp]  ; Indirect m16:32 FAR jmp with
                        ;    m16:32 constructed at top of stack
                        ;    DWORD allows us to use a 32-bit offset in 16-bit code

; 16-bit functions that run in real mode

; Check if protected mode is enabled, effectively checking if we are
; in in a VM8086 task. Set ZF to 1 if in protected mode

check_pmode:
    smsw ax
    test ax, 0x1
    ret


; Enable a20 (fast method). This may not work on all hardware
a20_on:
    cli
    in al, 0x92         ; Read System Control Port A
    test al, 0x02       ; Test current a20 value (bit 1)
    jnz .skipfa20       ; If already 1 skip a20 enable
    or al, 0x02         ; Set a20 bit (bit 1) to 1
    and al, 0xfe        ; Always write a zero to bit 0 to avoid
                        ;     a fast reset into real mode
    out 0x92, al        ; Enable a20
.skipfa20:
    sti
    ret

in_pmode_str: db "Processor already in protected mode - exiting",0x0a,0x0d,"$"

align 4
gdtr:
    dw gdt_end-gdt_start-1
    dd gdt_start

gdt_start:
    ; First entry is always the Null Descriptor
    dd 0
    dd 0

gdt_code:
    ; 4gb flat r/w/executable code descriptor
    dw 0xFFFF           ; limit low
    dw 0                ; base low
    db 0                ; base middle
    db 0b10011010       ; access
    db 0b11001111       ; granularity
    db 0                ; base high

gdt_data:
    ; 4gb flat r/w data descriptor
    dw 0xFFFF           ; limit low
    dw 0                ; base low
    db 0                ; base middle
    db 0b10010010       ; access
    db 0b11001111       ; granularity
    db 0                ; base high
gdt_end:

; Code that will run in 32-bit protected mode
; Align code to 4 byte boundary. code_32bit label is
; relative to the origin point 100h
align 4
code_32bit:
use32
; Set virtual memory address of pm code/data to CODE32_REL
; We will be relocating this section from low memory where DOS
; originally loaded it.
section protectedmode vstart=CODE32_REL, valign=4
start_32:
    cld                 ; Direction flag forward
    mov eax,0x10        ; 0x10 is flat selector for data
    mov ds,eax
    mov es,eax
    mov fs,eax
    mov gs,eax
    mov ss,eax
    mov esp,STACK32_TOP ; Should set ESP to a usable memory location
                        ; Stack will be grow down from this location

    mov edi,start_32    ; EDI = linear address where PM code will be copied
    mov esi,ebx         ; ESI = linear address of code_32bit
    mov ecx,PMSIZE_LONG ; ECX = number of DWORDs to copy
    rep movsd           ; Copy all code/data from code_32bit to CODE32_REL
    jmp 0x08:.relentry  ; Absolute jump to relocated code

.relentry:
    mov ah, 0x57        ; Attribute white on magenta

    ; Print a string to display
    mov esi,str         ; ESI = address of string to print
    mov edi,VIDEOMEM    ; EDI = base address of video memory
    call print_string_attr

    cli
endloop:
    hlt                 ; Halt CPU with infinite loop
    jmp endloop

print_string_attr:
    push ecx
    xor ecx,ecx         ; ECX = 0 current video offset
    jmp .loopentry
.printloop:
    mov [edi+ecx*2],ax  ; Copy attr and character to display
    inc ecx             ; Next word position
.loopentry:
    mov al,[esi+ecx]    ; Get next character to print
    test al,al
    jnz .printloop      ; If it's not NUL continue
.endprint:
    pop ecx
    ret

str: db "Protected Mode",0

PMSIZE_LONG equ ($-$$+3)>>2
                        ; Number of DWORDS that the protected mode
                        ;    code and data takes up (rounded up)