如何 运行 QEMU 上的裸机 ELF 文件?

How to run a bare metal ELF file on QEMU?

你如何 运行 QEMU 上的 elf 文件?这是我最好的猜测:

qemu-system-i386 -hda kernel.elf

这个有用吗? elf文件是从这个tutorial.

生成的内核

只需使用 -kernel 选项:

qemu-system-i386 -kernel kernel.elf

最小可运行示例

来源:https://github.com/cirosantilli/aarch64-bare-metal-qemu/tree/27537fb1dd0c27d6d91516bf4fc7e1d9564f5a40

运行 与:

make
qemu-system-aarch64 -M virt -cpu cortex-a57 -nographic -kernel test64.elf -serial mon:stdio

结果:向 UART 打印单个字符 H,然后进入无限循环。

来源:

==> test64.ld <==
ENTRY(_Reset)
SECTIONS
{
    . = 0x40000000;
    .startup . : { startup64.o(.text) }
    .text : { *(.text) }
    .data : { *(.data) }
    .bss : { *(.bss COMMON) }
    . = ALIGN(8);
    . = . + 0x1000; /* 4kB of stack memory */
    stack_top = .;
}

==> test64.c <==
volatile unsigned int * const UART0DR = (unsigned int *) 0x09000000;

void print_uart0(const char *s) {
    while(*s != '[=11=]') {         /* Loop until end of string */
         *UART0DR = (unsigned int)(*s); /* Transmit char */
          s++;                  /* Next char */
    }
}

void c_entry() {
     print_uart0("Hello world!\n");
}

==> startup64.s <==
.global _Reset
_Reset:
    mov x0, 0x48
    ldr x1, =0x09000000
    str x0, [x1]
    b .

==> Makefile <==
CROSS_PREFIX=aarch64-linux-gnu-

all: test64.elf

startup64.o: startup64.s
    $(CROSS_PREFIX)as -g -c $< -o $@

test64.elf: startup64.o
    $(CROSS_PREFIX)ld -Ttest64.ld $^ -o $@

clean:
    rm -f test64.elf startup64.o test64.o

您可以将入口地址 0x40000000 更改为几乎任何内容(只要它没有映射到某些设备的内存?)。

QEMU 只是从elf 文件中解析出入口地址,并将PC 放在那里开始。您可以使用 GDB 验证:

qemu-system-aarch64 -M virt -cpu cortex-a57 -nographic -kernel test64.elf -S -s &
gdb-multiarch -q -ex 'file test64.elf' -ex 'target remote localhost:1234'

这里我列出了一些您可能感兴趣的其他设置:

测试于 Ubuntu 18.04.