User Tools

Site Tools


developers:env_emul

QEMU

For testing your kernel builds it's much easier to use QEMU than normal hardware, especially if you are working on arch-specific code. QEMU has upstream support for RISC-V since version 2.12 so you may use the upstream binaries from your distribution or build your own either from the official qemu repo or from the riscv-qemu repo.

Build QEMU from riscv-qemu

The process is the same no matter where you got your QEMU sources from, it's pretty standard…

git clone --depth=1 https://github.com/riscv/riscv-qemu.git
cd riscv-qemu
./configure --target-list=riscv64-softmmu,riscv32-softmmu --prefix=<qemu install dir>
make

Running Linux on QEMU

There are various ways of invoking qemu, lets start with a standard virt machine that boots the test environment.

<qemu install dir>/bin/qemu-system-riscv64 -nographic -machine virt -smp <number of cores> -m <X>G -s \
				-bios <path to bbl or fw_jump.elf (in case of OpenSBI)> \
				-kernel <linux sources dir>/vmlinux \
				-initrd <path to initramfs.img> \
				-netdev user,id=unet -device virtio-net-device,netdev=unet -net user \
				-object rng-random,filename=/dev/urandom,id=rng0 \
				-device virtio-rng-device,rng=rng0

In case you built bbl or OpenSBI with the Linux kernel as its payload you can omit the -bios parameter and point the -kernel parameter directly to the combined bbl / fw_payload.elf binary.

Debugging with QEMU

With the -s option, QEMU allows a remote gdb session, to debug the running system. You may connect to the debugging session through gdb by typing target remote localhost:1234, here is an example debug session:

mick@trampakoulas ~/kexec/kexec-tools $ riscv64-unknown-linux-gnu-gdb

GNU gdb (GDB) 8.2.50.20181127-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=riscv64-unknown-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) target remote localhost:1234
Remote debugging using localhost:1234
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
0xffffffffb8916080 in ?? ()
(gdb) i r
ra             0xffffffff80033f4c	0xffffffff80033f4c
sp             0xffffffffbadd3d60	0xffffffffbadd3d60
gp             0xffffffff807a7b58	0xffffffff807a7b58
tp             0xffffffffb883b480	0xffffffffb883b480
t0             0xbafd5002	3137163266
t1             0x2	2
t2             0x0	0
fp             0xbafd5000	0xbafd5000
s1             0x80200000	2149580800
a0             0x6	6
a1             0x0	0
a2             0x0	0
a3             0x0	0
a4             0xffffffff80765578	-2139728520
a5             0xffffffff80765578	-2139728520
a6             0x78	120
a7             0x2e	46
s2             0x809f8000	2157936640
s3             0x0	0
s4             0x0	0
s5             0x200	512
s6             0x1	1
s7             0xfffffffeffe00000	-4297064448
s8             0xb8b16058	3098632280
s9             0x2	2
s10            0x1	1
s11            0x0	0
t3             0x1e108	123144
t4             0x1e108	123144
t5             0x0	0
t6             0xffffffff807b3465	-2139409307
pc             0xffffffffb8916080	0xffffffffb8916080
(gdb) q
A debugging session is active.

	Inferior 1 [Remote target] will be detached.

Quit anyway? (y or n) y
Detaching from program: , Remote target
Ending remote debugging.
[Inferior 1 (Remote target) detached]
developers/env_emul.txt · Last modified: 2019/04/12 19:16 by mick