Higher Half x86 Bare Bones (Backup)

来源:OSDev Wiki· 阅读原文 ↗
文章封面

本条目或章节的事实准确性 存在争议。
请参阅 讨论页 上的相关讨论。

本页面或章节使用 I、我、我们 或 我们 来指代读者或编辑者。应 编辑 为百科全书式的语气。

本教程需要解释代码的作用,因为教程不只是复制粘贴。你可以通过 编辑 本页面来补充更多关于代码作用的上下文。

等等!你是否已经阅读了 Getting Started、Beginner Mistakes,以及一些相关的 OS theory?

难度级别
Difficulty 2.png
中等

以下是一些示例代码,展示一个由 GRUB 加载并映射到内存上半部分的内核。在此情况下,内核在物理地址空间中加载于 1MB(0x00100000),但在虚拟地址空间中映射于 3GB + 1MB(0xC0100000)。建议你在尝试之前牢固掌握 Bare bones tutorial 和 Paging 的内容。

loader.asm

这段代码从 Multiboot 引导加载程序接管控制权。它设置一个页目录,其中的页表项对前 4MB 进行恒等映射,同时将前 4MB 映射到虚拟地址 3GB。设置好分页后,它取消恒等映射,使内核完全位于上半部分,并跳转到内核主体。

global _loader                          ; Make entry point visible to linker.
extern _main                            ; _main is defined elsewhere

; setting up the Multiboot header - see GRUB docs for details
MODULEALIGN equ  1<<0             ; align loaded modules on page boundaries
MEMINFO     equ  1<<1             ; provide memory map
FLAGS       equ  MODULEALIGN | MEMINFO  ; this is the Multiboot 'flag' field
MAGIC       equ    0x1BADB002     ; 'magic number' lets bootloader find the header
CHECKSUM    equ -(MAGIC + FLAGS)  ; checksum required

; This is the virtual base address of kernel space. It must be used to convert virtual
; addresses into physical addresses until paging is enabled. Note that this is not
; the virtual address where the kernel image itself is loaded -- just the amount that must
; be subtracted from a virtual address to get a physical address.
KERNEL_VIRTUAL_BASE equ 0xC0000000                  ; 3GB
KERNEL_PAGE_NUMBER equ (KERNEL_VIRTUAL_BASE >> 22)  ; Page directory index of kernel's 4MB PTE.


section .data
align 0x1000
BootPageDirectory:
    ; This page directory entry identity-maps the first 4MB of the 32-bit physical address space.
    ; All bits are clear except the following:
    ; bit 7: PS The kernel page is 4MB.
    ; bit 1: RW The kernel page is read/write.
    ; bit 0: P  The kernel page is present.
    ; This entry must be here -- otherwise the kernel will crash immediately after paging is
    ; enabled because it can't fetch the next instruction! It's ok to unmap this page later.
    dd 0x00000083
    times (KERNEL_PAGE_NUMBER - 1) dd 0                 ; Pages before kernel space.
    ; This page directory entry defines a 4MB page containing the kernel.
    dd 0x00000083
    times (1024 - KERNEL_PAGE_NUMBER - 1) dd 0  ; Pages after the kernel image.


section .text
align 4
MultiBootHeader:
    dd MAGIC
    dd FLAGS
    dd CHECKSUM

; reserve initial kernel stack space -- that's 16k.
STACKSIZE equ 0x4000

; setting up entry point for linker
loader equ (_loader - 0xC0000000)
global loader

_loader:
    ; NOTE: Until paging is set up, the code must be position-independent and use physical
    ; addresses, not virtual ones!
    mov ecx, (BootPageDirectory - KERNEL_VIRTUAL_BASE)
    mov cr3, ecx                                        ; Load Page Directory Base Register.

    mov ecx, cr4
    or ecx, 0x00000010                          ; Set PSE bit in CR4 to enable 4MB pages.
    mov cr4, ecx

    mov ecx, cr0
    or ecx, 0x80000000                          ; Set PG bit in CR0 to enable paging.
    mov cr0, ecx

    ; Start fetching instructions in kernel space.
    ; Since eip at this point holds the physical address of this command (approximately 0x00100000)
    ; we need to do a long jump to the correct virtual address of StartInHigherHalf which is
    ; approximately 0xC0100000.
    lea ecx, [StartInHigherHalf]
    jmp ecx                                                     ; NOTE: Must be absolute jump!

StartInHigherHalf:
    ; Unmap the identity-mapped first 4MB of physical address space. It should not be needed
    ; anymore.
    mov dword [BootPageDirectory], 0
    invlpg [0]

    ; NOTE: From now on, paging should be enabled. The first 4MB of physical address space is
    ; mapped starting at KERNEL_VIRTUAL_BASE. Everything is linked to this address, so no more
    ; position-independent code or funny business with virtual-to-physical address translation
    ; should be necessary. We now have a higher-half kernel.
    mov esp, stack+STACKSIZE           ; set up the stack
    push eax                           ; pass Multiboot magic number

    ; pass Multiboot info structure -- WARNING: This is a physical address and may not be
    ; in the first 4MB!
    push ebx

    call  _main                  ; call kernel proper
    hlt                          ; halt machine should kernel return


section .bss
align 32
stack:
    resb STACKSIZE      ; reserve 16k stack on a uint64_t boundary

linker.ld

这比 C kernel tutorial中的做法要棘手一些,因为你需要区分虚拟地址(将位于上半部分)和加载地址,GRUB 需要后者来决定将你的内核放在哪里。

ENTRY(loader)
OUTPUT_FORMAT(elf32-i386)

SECTIONS {
   /* The kernel will live at 3GB + 1MB in the virtual
      address space, which will be mapped to 1MB in the
      physical address space. */
   . = 0xC0100000;

   .text : AT(ADDR(.text) - 0xC0000000) {
       *(.text)
       *(.rodata*)
   }

   .data ALIGN (0x1000) : AT(ADDR(.data) - 0xC0000000) {
       *(.data)
   }

   .bss : AT(ADDR(.bss) - 0xC0000000) {
       _sbss = .;
       *(COMMON)
       *(.bss)
       _ebss = .;
   }
}

注意我们使用 loader(而不是 _loader)作为入口点。这是因为 _loader 的地址大约是 0xC0100000,如果我们尝试将 eip 设置为该地址,将找不到我们的 loader 函数。另请注意,我们的入口点没有被转换为物理地址。GRUB 在计算 EIP 的起始值时会进行此转换,如果你尝试进行该转换,可能会在你不希望的时候执行,或者得到 "entry point isn't in a segment" 错误。

kernel.c

使用原始 bare bones 教程中的 kernel.c code 可以正常工作,只需做一处小改动。在 terminal_initialize() 的第四行,将:

terminal_buffer = (uint16_t*) 0xB8000;

改为:

terminal_buffer = (uint16_t*) 0xC00B8000;

这适应了内核在上半部分空间中的新偏移。此时内核的任何直接内存访问在必要时都应相对于该偏移进行。

故障排除

访问我的 GRUB Multiboot 信息结构时出现页错误(#PF)

loader.s 传递的地址是物理地址,你必须将其转换为虚拟地址,即加上你的虚拟基址。例如,在你的 loader.s 中:

   add ebx, KERNEL_VIRTUAL_BASE ; make the address virtual
   push ebx ; push it on the stack for _main()

别忘了将 Multiboot 信息结构中指向内存位置的所有地址也转换为虚拟地址。

另请参阅

文章

来源与翻译说明

本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

原文:Higher Half x86 Bare Bones (Backup);离线修订号:23056。对应原文版本

原作者:OSDev Wiki 社区贡献者。原文及引用资料的版权与许可声明继续适用:版权声明 许可说明

评论

暂无评论