Multiboot 1 高半区 x86 入门基础

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

HistoricalCuriosity.png

本条目或本节的内容被视为历史趣闻,不应考虑用于新设计。


难度级别
Difficulty 2.png
中等

本教程讲解如何编写一个基本的 32 位 x86 高半区内核 ,使用 Multiboot 1 协议。它可用作构建面向过时(Intel 386 时代)硬件的操作系统的基础。

由于 Multiboot 1 的局限性,在为现代硬件构建操作系统时,应考虑使用不同的协议(至少 Multiboot 2,或其他某种 引导加载程序),同时还应考虑面向 x86-64。

原理

高半区内核是指被映射到虚拟地址空间高半区的内核,尽管它被加载到物理地址空间的低半区。为了理解这一概念,建议你牢固掌握 入门基础 和 分页 中的理论。

当 GRUB 将控制权交给内核时,分页处于禁用状态。这意味着物理地址与虚拟地址对应相同的地址。

编写高半区内核时,所需的步骤是:

  • 为初始映射保留一些页面,以便在启用内存管理之前能够解析 GRUB 结构。
  • 创建包含内核映像页框的页表。
  • 创建引导页目录,其中在低半区和高半区都包含上述页表。
  • 启用分页。
  • 跳转到高半区。
  • 移除低半区的内核映射。

代码

以下是一个内核的示例代码,该内核由 GRUB 引导加载程序 加载,然后映射到虚拟内存的上半区。在此例中,内核被加载到物理地址 0x00100000,并映射到虚拟地址 0xC0100000。

假设

此示例包含许多假设:

  • 它假设内核小于 3 MiB(仅保留了一个引导页表)。
  • 它假设开发者希望将内核映射到 0xC0000000。
  • 它假设每个 ELF 节都既可读又可写(对于 .text 和 .rodata 并非如此)

boot.s

在 GRUB 将控制权交给内核后,这段代码从 _start 开始执行。它本质上通过执行上述所需步骤并调用 main() 来引导内核。

# Declare constants for the multiboot header.
.set ALIGN,    1<<0             # align loaded modules on page boundaries
.set MEMINFO,  1<<1             # provide memory map
.set FLAGS,    ALIGN | MEMINFO  # this is the Multiboot 'flag' field
.set MAGIC,    0x1BADB002       # 'magic number' lets bootloader find the header
.set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot

# Declare a multiboot header that marks the program as a kernel.
.section .multiboot.data, "aw"
.align 4
.long MAGIC
.long FLAGS
.long CHECKSUM

# Allocate the initial stack.
.section .bootstrap_stack, "aw", @nobits
stack_bottom:
.skip 16384 # 16 KiB
stack_top:

# Preallocate pages used for paging. Don't hard-code addresses and assume they
# are available, as the bootloader might have loaded its multiboot structures or
# modules there. This lets the bootloader know it must avoid the addresses.
.section .bss, "aw", @nobits
	.align 4096
boot_page_directory:
	.skip 4096
boot_page_table1:
	.skip 4096
# Further page tables may be required if the kernel grows beyond 3 MiB.

# The kernel entry point.
.section .multiboot.text, "a"
.global _start
.type _start, @function
_start:
	# Physical address of boot_page_table1.
	# TODO: I recall seeing some assembly that used a macro to do the
	#       conversions to and from physical. Maybe this should be done in this
	#       code as well?
	movl $(boot_page_table1 - 0xC0000000), %edi
	# First address to map is address 0.
	# TODO: Start at the first kernel page instead. Alternatively map the first
	#       1 MiB as it can be generally useful, and there's no need to
	#       specially map the VGA buffer.
	movl $0, %esi
	# Map 1023 pages. The 1024th will be the VGA text buffer.
	movl $1023, %ecx

1:
	# Only map the kernel.
	cmpl $_kernel_start, %esi
	jl 2f
	cmpl $(_kernel_end - 0xC0000000), %esi
	jge 3f

	# Map physical address as "present, writable". Note that this maps
	# .text and .rodata as writable. Mind security and map them as non-writable.
	movl %esi, %edx
	orl $0x003, %edx
	movl %edx, (%edi)

2:
	# Size of page is 4096 bytes.
	addl $4096, %esi
	# Size of entries in boot_page_table1 is 4 bytes.
	addl $4, %edi
	# Loop to the next entry if we haven't finished.
	loop 1b

3:
	# Map VGA video memory to 0xC03FF000 as "present, writable".
	movl $(0x000B8000 | 0x003), boot_page_table1 - 0xC0000000 + 1023 * 4

	# The page table is used at both page directory entry 0 (virtually from 0x0
	# to 0x3FFFFF) (thus identity mapping the kernel) and page directory entry
	# 768 (virtually from 0xC0000000 to 0xC03FFFFF) (thus mapping it in the
	# higher half). The kernel is identity mapped because enabling paging does
	# not change the next instruction, which continues to be physical. The CPU
	# would instead page fault if there was no identity mapping.

	# Map the page table to both virtual addresses 0x00000000 and 0xC0000000.
	movl $(boot_page_table1 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 0
	movl $(boot_page_table1 - 0xC0000000 + 0x003), boot_page_directory - 0xC0000000 + 768 * 4

	# Set cr3 to the address of the boot_page_directory.
	movl $(boot_page_directory - 0xC0000000), %ecx
	movl %ecx, %cr3

	# Enable paging and the write-protect bit.
	movl %cr0, %ecx
	orl $0x80010000, %ecx
	movl %ecx, %cr0

	# Jump to higher half with an absolute jump. 
	lea 4f, %ecx
	jmp *%ecx

.section .text

4:
	# At this point, paging is fully set up and enabled.

	# Unmap the identity mapping as it is now unnecessary. 
	movl $0, boot_page_directory + 0

	# Reload crc3 to force a TLB flush so the changes to take effect.
	movl %cr3, %ecx
	movl %ecx, %cr3

	# Set up the stack.
	mov $stack_top, %esp

	# Enter the high-level kernel.
	call kernel_main

	# Infinite loop if the system has nothing more to do.
	cli
1:	hlt
	jmp 1b

linker.ld

这比 入门基础教程 中的情况要棘手一些,因为你需要区分虚拟地址(将位于高半区)和物理地址(GRUB 需要据此决定将你的内核放置在何处)。

ENTRY (_start)

SECTIONS
{
        . = 0x00100000;
	/* The kernel will live at 3GB + 1MB in the virtual address space, */
	/* which will be mapped to 1MB in the physical address space. */
	/* Note that we page-align the sections. */

	_kernel_start = .;
        .multiboot.data : {
            *(.multiboot.data)
        }

       .multiboot.text : {
           *(.multiboot.text)
       }

	. += 0xC0000000;
	/* Add a symbol that indicates the start address of the kernel. */
	.text ALIGN (4K) : AT (ADDR (.text) - 0xC0000000)
	{
		*(.text)
	}
	.rodata ALIGN (4K) : AT (ADDR (.rodata) - 0xC0000000)
	{
		*(.rodata)
	}
	.data ALIGN (4K) : AT (ADDR (.data) - 0xC0000000)
	{
		*(.data)
	}
	.bss ALIGN (4K) : AT (ADDR (.bss) - 0xC0000000)
	{
		*(COMMON)
		*(.bss)
		*(.bootstrap_stack)
	}
	/* Add a symbol that indicates the end address of the kernel. */
	_kernel_end = .;
}

kernel.c

使用 Bare Bones 中的 kernel.c 中的 kernel.c,并作如下修改。在 boot.s 中,我们为 VGA 文本缓冲区保留了第 1024 个页面。这对应于虚拟地址 0xC03FF000。将 terminal_buffer 的初始化改为:

	terminal_buffer = (uint16_t*) 0xC03FF000;

来源与翻译说明

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

原文:Multiboot 1 Higher Half x86 Bare Bones;离线修订号:30681。

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

评论

暂无评论