QEMU AArch64 Virt Bare Bones: Difference between revisions

From OSDev.wiki
Jump to navigation Jump to search
[unchecked revision][unchecked revision]
Content deleted Content added
Created page with "QEMU is a free and open source hardware emulator that supports multiple architecture, including ARM. QEMU has it's own board with PCI support, called virt..."
 
No edit summary
Line 12: Line 12:


=== boot.s ===
=== boot.s ===
<source lang="armasm">
<source lang="asm">
.global _start
.global _start
_start:
_start:
Line 56: Line 56:
== Compiling the kernel ==
== Compiling the kernel ==
Now, we'll need to compile our kernel so we can boot it.
Now, we'll need to compile our kernel so we can boot it.
<source lang="shell">
<source lang="bash">
$ aarch64-elf-as boot.s -o boot.o
$ aarch64-elf-as boot.s -o boot.o
$ aarch64-elf-gcc -ffreestanding -c kernel.c -o kernel.o
$ aarch64-elf-gcc -ffreestanding -c kernel.c -o kernel.o
Line 64: Line 64:
== Booting our kernel ==
== Booting our kernel ==
Since our kernel is now compiled, we'll boot it with QEMU
Since our kernel is now compiled, we'll boot it with QEMU
<source lang="shell">
<source lang="bash">
$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -kernel kernel.elf -nographic
$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -kernel kernel.elf -nographic
Hello world!
Hello world!

Revision as of 08:02, 20 October 2021

QEMU is a free and open source hardware emulator that supports multiple architecture, including ARM. QEMU has it's own board with PCI support, called virt. virt supports ARM and RISC-V architecture.

In this tutorial, we'll write a basic kernel for QEMU virt board, specifically Aarch64.

Prerequisites

For this tutorial, you'll need

  • An ARM cross-compiler toolchain
  • QEMU, of course you'll need QEMU because this tutorial designed for QEMU virt.

Code

Now we'll need to write some code for our little kernel.

boot.s

.global _start
_start:
    ldr x30, =stack_top
    mov sp, x30
    bl kmain
    b .

kernel.c

#include <stdint.h>

volatile uint32_t *UART = (uint32_t *) 0x09000000;

void print(const char *s) {
    while(*s != '\0') {
        *UART = (uint8_t)(*s);
        s++;
    }
}

void kmain(void) {
     print("Hello world!\n");
}

linker.ld

ENTRY(_start)
SECTIONS {
    . = 0x40000000;
    .startup . : { boot.o(.text) }
    .text : { *(.text) }
    .data : { *(.data) }
    .bss : { *(.bss COMMON) }
    . = ALIGN(8);
    . = . + 0x1000; /* 4kB of stack memory */
    stack_top = .;
}

Compiling the kernel

Now, we'll need to compile our kernel so we can boot it.

$ 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

Booting our kernel

Since our kernel is now compiled, we'll boot it with QEMU

$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -kernel kernel.elf -nographic
Hello world!