c - 从实模式切换到保护模式后远跳

标签 c assembly operating-system intel bootloader

根据这个tutorial创建一个简单的操作系统就足够了,只需像以下代码一样简单地切换到保护模式,而无需其他众所周知的操作,例如启用 A20...

无论如何,我是这个领域的新手,我写了下面的代码,正如他们提到的那样,并根据这个 SO 进行了修改。 .

代码结构: 这个简单的操作系统应按如下方式短暂加载:

  1. 加载/读取 15 个扇区
  2. 启用 GDT
  3. 切换到保护模式(并打印“Successfully landing in 32-bit Protected Mode”)。
  4. 加载内核并打印“X”

但是模拟器仍在重新启动。请找到随附的完整代码。

bootloader.asm

[bits 16]
[org 0x7C00]

KERNEL_OFFSET equ 0x1000

xor ax, ax
mov ds, ax
mov es, ax
mov [BOOT_DRIVE], dl
mov ax, 0x07E0                  ; End of stack
cli
mov ss, ax
mov sp, 0x1200                  ; Size of Stack. By this, we assume that stack starts at 9000h
                            ; of size 1200h and ends at 7E00h to avoid being overwritten.
sti

call    load_kernel
call    switch_to_pm

jmp $

%include "src/functions/disk_load.asm"

load_kernel:
    mov bx, KERNEL_OFFSET   
    mov dh, 15
    mov dl, [BOOT_DRIVE]
    call    disk_load

    ret

; Global variables
BOOT_DRIVE  db 0
SECTORS     db 0
MSG_PROT_MODE   db "Successfully landed in 32-bit Protected Mode" , 0

%include "src/functions/gdt.asm"
%include "src/functions/switch_to_pm.asm"

[ bits 32]
; This is where we arrive after switching to and initialising protected mode.
BEGIN_PM:
    mov ebx , MSG_PROT_MODE
    call    print_string_pm     ; Use our 32 - bit print routine.


    ;call   KERNEL_OFFSET       ; Now jump to the address of our loaded
                    ; kernel code , assume the brace position ,
                    ; and cross your fingers. Here we go !

    jmp $           ; Hang.

%include "src/functions/writing_video_mode.asm"

; Bootsector padding
times 510-($-$$) db 0
dw 0xAA55

; 15 sector padding
times 15*256 dw 0xDADA

disk_load.asm

disk_load:
    mov [SECTORS], dh
    mov ch, 0x00            ;C=0
    mov dh, 0x00            ;H=0
    mov cl, 0x02            ;S=2

next_group:
    mov di, 5                ;Max 5 tries

again: 
    mov ah, 0x02            ;Read sectors
    mov al, [SECTORS]
    int 0x13
    jc   maybe_retry
    sub [SECTORS], al ;Remaining sectors
    jz  ready
    mov cl, 0x01            ;Always sector 1
    xor dh, 1                ;Next head on diskette!
    jnz next_group
    inc ch                      ;Next cylinder
    jmp next_group

maybe_retry:
    mov ah, 0x00            ;Reset diskdrive
    int 0x13
    dec di
    jnz again
    jmp disk_error

ready:
    ret

disk_error:
    mov ah, 0x0e
    mov al, 'Y'
    int 0x10
    jmp $

DISK_ERROR_MSG db "Disk read error!", 0

gdt.asm

gdt_start:

    gdt_null:
        dd 0x0 ; ’ dd ’ means define double word ( i.e. 4 bytes )
        dd 0x0

    gdt_code:
        dw 0xffff
        dw 0x0
        db 0x0
        db 10011010b ; 1 st flags , type flags
        db 11001111b ; 2 nd flags , Limit ( bits 16 -19)
        db 0x0

    gdt_data:
        dw 0xffff
        dw 0x0
        db 0x0
        db 10010010b ; 1 st flags , type flags
        db 11001111b ; 2 nd flags , Limit ( bits 16 -19)
        db 0x0

    gdt_end:

    gdt_descriptor:
        dw gdt_end - gdt_start - 1
        dd gdt_start

CODE_SEG equ gdt_code - gdt_start
DATA_SEG equ gdt_data - gdt_start

switch_to_pm.asm

[ bits 16 ]

switch_to_pm:
    cli
    lgdt [ gdt_descriptor ]
    mov eax , cr0
    or eax , 0x1
    mov cr0 , eax
    jmp CODE_SEG:init_pm

[ bits 32 ]

init_pm:
    mov ax, DATA_SEG
    mov ds, ax
    mov ss, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ebp , 0x90000
    mov esp , ebp
    call BEGIN_PM

为了确保我们进入保护模式:

writing_video_mode.asm

[ bits 32]

VIDEO_MEMORY equ 0xb8000
WHITE_ON_BLACK equ 0x0f

print_string_pm:
    push eax
    push ebx
    push edx
    mov edx , VIDEO_MEMORY ; Set edx to the start of vid mem.

    print_string_pm_loop:
        mov al, [ebx]
        mov ah, WHITE_ON_BLACK
        cmp al, 0
        je print_string_pm_done
        mov [edx], ax
        add ebx, 1
        add edx, 2
        jmp print_string_pm_loop

    print_string_pm_done:
        pop edx 
        pop ebx 
        pop eax 
        ret

内核.c

void main () {
    char * video_memory = (char *) 0xb8000;
    *video_memory = 'X';
}

顺便说一句,我正在使用这个Makefile:

all: bootloader.bin kernel.bin

bootloader.bin: src/bootloader.asm
    nasm src/bootloader.asm -f bin -o output/bootloader.bin

kernel.o: src/kernel/kernel.c
    gcc -ffreestanding -c src/kernel/kernel.c -o output/kernel.o -m32

kernel.bin: kernel.o
    ld -o output/kernel.bin -Ttext 0x1000 --oformat binary output/kernel.o -melf_i386

clean:
    rm -f output/*.* output/*

为了将它移动到闪存中,我使用了这些命令:

cat output/bootloader.bin output/kernel.bin > os-image
sudo dd if=os-image of=/dev/sdb bs=512 conv=notrunc && sync

为了运行它,我使用带有此命令的 qemu:

qemu-system-i386 -hda /dev/sdb

注意/dev/sdb 是我的闪存驱动器。

问题: 实际上,代码正在进入保护模式(即打印“成功进入 32 位保护模式”),就在 bootloader.asm 中禁用/注释 call KERNEL_OFFSET 时>。但是,当启用此行时,它会开始引导和重新启动。

我希望我已经提供了所有需要的信息。在我看来,远跳不应该这样做。任何意见表示赞赏。

最佳答案

只需删除

times 15*256 dw 0xDADA

(顺便说一句,为什么是 DADA?)
然后编译你的内核,在那之后

cat output/bootloader.bin output/kernel.bin > os-image

并以某种方式使您的操作系统镜像长 8192 字节(16 个扇区,引导加载程序 + 15)。我不是 Linux/Unix 粉丝(甚至不能使用它们),但我认为 dd 命令(类似于 dd if=dev\zero of=temp_file count=(8192 - < em>file actual size),然后 cat os-image temp-file > os-image) 应该可以完成这项工作。我也不确定这个编译命令是否正确(只是不确定)。我会从链接器命令中删除“-melf_i386”,但是我知道,我只在 Windows 上使用过 MinGW(它只类似于 GCC)。

抱歉我的英语不好,我希望我能帮到你。

关于c - 从实模式切换到保护模式后远跳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42482999/

相关文章:

operating-system - 复制粘贴怎么可能?

c - 这个反转字符串代码有什么问题(C)

c - 基于比特选择的算法效率

c - SSE 将寄存器设置为 0.0's and 1.0' s 的最佳方法?

java - 技嘉 vs 吉比 & 技嘉 vs 吉比

linux - 我现在如何在 Linux 上使用 Windows 7 上安装的预安装 Intellij Idea?

c - 父进程和子进程使用管道进行通信的问题

"function"错误的冲突类型 (C)

c++ - 为什么 gcc 在按值传递微不足道的结构时会发出不需要的内存访问?

assembly - ARM cortex m4 DSP 代码上的快速 1 位透明 blit