使用独立加载器创建 64 位内核
文章目录
| 难度级别 |
|---|
高级 |
| 内核设计 |
|---|
| 模型 |
| 其他概念 |
本页面是以下内容的扩展: 创建 64 位内核,具体是其中的章节: 使用独立加载器加载。如果你还没有阅读过,请现在去阅读。
这可能适用于你的内核,也可能不适用;你可能需要对其进行调整才能使其与你的内核配合工作。
64 位内核
内核本身不应包含任何 multiboot 头,也不需要汇编引导程序(如果你认为有必要,可以自由使用)。
在你的链接脚本中指定的 OUTPUT_FORMAT 应为 elf64-x86-64,以便加载器能正确加载它。
在你的加载器将控制权交给内核后,你应该设置 64 位 IDT 和分页。
使用以下命令编译 C 文件:
x86_64-elf-gcc -m64 -c <source file> -o <output> -ffreestanding -z max-page-size=0x1000 -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -std=gnu99 -O2 -Wall -Wextra -I <path to your header files>
使用以下命令链接内核:
x86_64-elf-gcc -T <linker script> -o <output> <all object files> -ffreestanding -O2 -nostdlib -lgcc
你的输出应复制到 loader_build/boot/kernel.bin(参见 32 位加载器 GRUB)
32 位加载器
你的加载器将类似于 Bare Bones 教程中制作的内核。
汇编引导程序
它以一个 multiboot 头和一个设置栈并调用 C 代码的汇编例程开始。
此外,它可能会检查 eax 是否包含 0x2BADB002(这意味着它是由 multiboot 引导加载程序加载的)
_start: cli ; Clear interrupts, we don't want them as long as we are in the loader mov esp, stack_top ; Set up a valid stack mov ebp, stack_top push ebx ; Push pointer to multiboot info structure call lmain
加载器
加载器现在必须设置自己的 32 位 GDT,并解析引导程序传给它的 multiboot 结构。
一个包含有用 multiboot 结构的头文件可以在 此处 下载。
#include "multiboot.h" /* This function gets called by the bootloader */ void lmain(const void* multiboot_struct) { // Set up GDT const multiboot_info_t* mb_info = multiboot_struct; /* Make pointer to multiboot_info_t struct */ multiboot_uint32_t mb_flags = mb_info->flags; /* Get flags from mb_info */ void* kentry = NULL; /* Pointer to the kernel entry point */ if (mb_flags & MULTIBOOT_INFO_MODS) { /* Check if modules are available */ multiboot_uint32_t mods_count = mb_info->mods_count; /* Get the amount of modules available */ multiboot_uint32_t mods_addr = mb_info->mods_addr; /* And the starting address of the modules */ for (uint32_t mod = 0; mod < mods_count; mod++) { multiboot_module_t* module = (multiboot_module_t*)(mods_addr + (mod * sizeof(multiboot_module_t))); /* Loop through all modules */ } } }
每个 multiboot 模块都关联有一个命令行,可以从 multiboot_module_t 中获得指向它的指针。
此时,可以将所有可用的模块加载到内存中,或者检查它们的特定命令行以仅加载特定的子集。
const char* module_string = (const char*)module->cmdline; /* Here I check if module_string is equals to the one i assigned my kernel you could skip this check if you had a way of determining the kernel module */ if(strcmp(module_string, kernel_bin_string)){ kentry = load_elf_module(module->mod_start, module->mod_end); }
现在,我们必须解析引导加载程序为我们加载到内存中的内核 ELF 文件。
这可以很容易地使用一个辅助函数和一个我按照 ELF64 规范编写的头文件(可在 [|此处] 获得)以及函数声明(可在 [|此处] 获得)来完成。
#include "elf64.h" // Also requires elf64.c char* kernel_elf_space[sizeof(elf_file_data_t)]; elf_file_data_t* kernel_elf = (elf_file_data_t*) kernel_elf_space; /* Pointer to elf file structure (remember there is no memory management yet) */ /* This function parses the ELF file and returns the entry point */ void* load_elf_module(multiboot_uint32_t mod_start, multiboot_uint32_t mod_end) { unsigned long err = parse_elf_executable((void*)mod_start, sizeof(elf_file_data_t), kernel_elf); /* Parses ELF file and returns an error code */ if(err == 0){ /* No errors occurred while parsing the file */ for(int i = 0; i < kernel_elf->numSegments; i++){ elf_file_segment_t seg = kernel_elf->segments[i]; /* Load all the program segments into memory */ /* if you want to do relocation you should do so here, */ const void* src = (const void*) (mod_start + seg.foffset); /* though that would require some changes to parse_elf_executable */ memcpy((void*) seg.address, src, seg.flength); } return (void*) kernel_elf->entryAddr; /* Finally we can return the entry address */ } return NULL; }
最后,我们可以设置长模式并跳转到 64 位内核。
我们通过在 lmain 函数末尾放置 2 个汇编函数来实现这一点。
extern void setup_longmode(); extern void enter_kernel(void* entry, uint32_t multiboot_info); void lmain(const void* multiboot_struct) { // Set up GDT // Check modules setup_longmode(); /* Set up long mode and jump to the kernel code */ enter_kernel(kentry, (uint32_t) mb_info); }
设置长模式
在我们考虑切换到长模式之前,必须确保当前 CPU 支持它。
- 首先,我们通过翻转 eflags 寄存器中的第 21 位(CPUID)来检查 CPUID 指令是否可用。如果该位被翻转,则 CPUID 可用。
- 其次,我们必须检查扩展 CPUID 功能是否可用。这通过检查 CPUID 功能 0x80000000,看其是否大于或等于 0x80000001 来完成。
- 最后,通过执行 CPUID 功能 0x80000001 并测试第 29 位(长模式)是否被设置,来检查长模式是否可用。
要设置长模式,必须完成许多工作。
- 禁用你的引导加载程序可能已设置的任何分页(清除 CR0 中的第 31 位)
- 然后,设置 64 位分页(但 不要 启用它)。这包括 PML4T、PDPT、PDT 和 PT,以恒等映射在你的 64 位内核设置自己的分页之前所需的所有内存。
- 通过设置 CR4 的第 5 位来启用 PAE(物理地址扩展)
- 通过设置 MSR 0xC0000080 中的第 8 位(长模式启用)切换到 IA32e(兼容模式)
- 通过设置 CR0 中的第 31 位再次启用分页
现在我们处于兼容模式,要进入长模式,我们必须设置 64 位 GDT(这将是你的内核使用的 GDT,所以你必须在这里设置所有内容)
下面的代码一点也不优化,你应该尝试自己组合出替代方案。
global enter_kernel enter_kernel: push ebp mov ebp, esp ; Set up the stack so the variables passed from the C code can be read mov esi, [ebp+8] ; This is the kernel entry point mov [k_ptr], esi lgdt [GDT.pointer] ; Load GDT mov ax, GDT.data ; Reload data segment selectors mov ss, ax mov ds, ax mov es, ax jmp GDT.code:.jmp_k ; Reload code selector by jumping to 64-bit code .jmp_k: mov edi, [ebp + 12] ; 1st argument of kernel_main (pointer to multiboot structure) mov eax, [k_ptr] ; This is transformed to mov rax, [k_ptr] and uses the double word reserved below dd 0 ; Trick the processor, contains high address of k_ptr ; as higher half of the address to k_ptr jmp eax ; This part is plain bad, tricking the processor is not the best thing to do here section .data k_ptr: dd 0
编译和链接加载器
加载器必须编译并链接为 32 位 ELF 文件,且 Multiboot 头位于前 8 kB 内(参见 Bare Bones 教程了解如何操作)
GRUB
如果你使用 GRUB,以下是设置目录结构的示例:
- loader_build/
- boot/
- grub/
- grub.cfg
- kernel.bin(这是 64 位内核 ELF 文件)
- loader.bin(这是 32 位加载器 ELF 文件)
- grub/
- boot/
你的 grub.cfg 文件将至少包含以下内容:
menuentry "Kernel" {
multiboot /boot/loader.bin // Path to the loader executable
module /boot/kernel.bin "KERNEL_BIN" // Path to the kernel executable, the string in "" is your command line
// More modules may be added here in the form 'module <path> "<cmdline>"'
}
注意:如果你使用 Multiboot 2,请将上述 multiboot/module 行替换为 multiboot2/module2。
构建它:
grub-mkrescue -o os.iso loader_build/
另请参阅
文章
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:Creating a 64-bit kernel using a separate loader;离线修订号:30494。对应原文版本

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论