QEMU AArch64 Virt 裸机基础
来源:OSDev Wiki· 阅读原文 ↗
文章目录
QEMU 是一个自由开源的硬件模拟器,支持多种架构,包括 ARM。QEMU 有自己主要用于虚拟化用途的通用开发板,名为 virt。virt 支持 ARM 和 RISC-V 架构。
在本教程中,我们将为 QEMU virt 开发板编写一个基础内核,具体是 Aarch64。
先决条件
在本教程中,你需要:
- 一个 ARM 交叉编译工具链
- QEMU,当然你需要 QEMU,因为本教程是为 QEMU virt 设计的。
代码
现在我们需要为我们的小内核编写一些代码。
boot.s
.global _start _start: ldr x30, =stack_top mov sp, x30 bl kmain b .
kernel.c
#include <stdint.h> volatile uint8_t *uart = (uint8_t *) 0x09000000; void putchar(char c) { *uart = c; } void print(const char *s) { while(*s != '\0') { putchar(*s); s++; } } void kmain(void) { print("Hello world!\n"); }
linker.ld
ENTRY(_start) SECTIONS { . = 0x40100000; /* RAM starts at 0x40000000 but if we ask to load the kernel there, QEMU will not load a DTB */ .startup . : { boot.o(.text) } .text : { *(.text) } .data : { *(.data) } .bss : { *(.bss COMMON) } . = ALIGN(16); /* two times pointer size */ . += 0x1000; /* 4kB of stack memory */ stack_top = .; }
编译内核
现在,我们需要编译内核,以便能够启动它。
$ aarch64-elf-as boot.s -o boot.o $ aarch64-elf-gcc -ffreestanding -c kernel.c -o kernel.o $ aarch64-elf-ld -nostdlib -Tlinker.ld boot.o kernel.o -o kernel.elf
启动我们的内核
由于我们的内核现在已经编译完成,我们将使用 QEMU 启动它。
$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -kernel kernel.elf -nographic Hello world!
另请参阅
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。
原文:QEMU AArch64 Virt Bare Bones;离线修订号:30865。对应原文版本

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