My RISC-V development environment for programming and reverse engineering purposes
How I set up a RISC-V environment on my machine to develop assembly programs and analyze RISC-V Linux malware.
First, what is RISC-V?
RISC-V is an open standard instruction set architecture (ISA) that began as a project at UC Berkeley in 2010 and is based on established reduced instruction set computer (RISC) principles. Unlike most other ISA designs, RISC-V is provided under open source licenses that do not require fees to use.
This new architecture has many applications. Here are a few examples:
- The European Processor Initiative (EPI), a European Union supercomputer project based on RISC-V
- 2,000 RV64GC-based laptops planned by the end of 2022, announced in June 2021 by the Institute of Software of the Chinese Academy of Sciences
- A low-power RISC-V IoT processor optimized for artificial intelligence
- Alibaba’s open source RISC-V processors
The growing presence of this architecture in highly critical installations led me to question its security.
I will soon invest in a development board, but I didn’t have one when I wanted to start tinkering. So I set up a development environment on my non-RISC-V machine, which is the topic of this first publication about RISC-V systems: a setup to ease the development of assembly programs when you don’t have a RISC-V machine at home.
Table of contents:
- How I will structure my setup
- Downloading a 64-bit RISC-V Debian image
- Using QEMU to emulate a RISC-V machine
- Sharing a directory between the host and guest machines
- Installing debugging and compilation tools
- Demonstration
- Conclusion
- To go further
How I will structure my setup
I started by mapping out what I was going to need:
- A 64-bit RISC-V Debian image, since I know this distribution quite well. Fedora also has a RISC-V version.
- A folder for useful scripts that automate repetitive actions (compiling assembly code, launching the setup…).
- A shared folder between the host and guest machines.
- A folder for the assembly programs I write.
Here is the directory tree of my project:
RISC-V_Setup
├── image # Debian image
├── scripts # Useful scripts (run.sh, compile.sh...)
├── share # Directory shared between host and guest
└── src # RISC-V assembly code
Downloading a 64-bit RISC-V Debian image
I create an image directory, download a pre-made 64-bit RISC-V Debian image into it and unzip it. Then I create an overlay disk on top of it: the guest only writes to the overlay, so the original image stays clean and can be restored by simply recreating the overlay.
mkdir image
wget "https://gitlab.com/api/v4/projects/giomasce%2Fdqib/jobs/artifacts/master/download?job=convert_riscv64-virt" \
-O ./image/debian-rv64.zip
unzip ./image/debian-rv64.zip -d ./image
qemu-img create -f qcow2 -b image.qcow2 -F qcow2 ./image/dqib_riscv64-virt/overlay.qcow2
The default credentials are debian:debian and root:root.
Using QEMU to emulate a RISC-V machine
From here on, my main machine is the host and the RISC-V machine emulated with QEMU is the guest.
We emulate a 64-bit RISC-V processor using QEMU. This requires qemu-system-riscv64, as well as the OpenSBI firmware and the U-Boot image used below. On Debian 13, you can install all of them with sudo apt-get install qemu-system-riscv qemu-utils opensbi u-boot-qemu (on older releases, QEMU’s RISC-V emulator is in qemu-system-misc). It is also possible to emulate a 32-bit processor with qemu-system-riscv32.
qemu-system-riscv64 \
-machine virt \
-cpu rv64 \
-m 1G \
-device virtio-net-device,netdev=net \
-netdev user,id=net,hostfwd=tcp::2222-:22 \
-device virtio-blk-device,drive=hd \
-drive file=./image/dqib_riscv64-virt/overlay.qcow2,if=none,id=hd \
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
-append "root=LABEL=rootfs console=ttyS0" \
-nographic \
-fsdev local,security_model=passthrough,id=fsdev0,path=./share \
-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare
Some of these options need further explanation:
-cpu rv64- Selects a 64-bit RISC-V CPU.
-m 1G- Allocates 1 GB of RAM to the guest machine. Adjust it to your needs and to the RAM you have available.
-netdev user,id=net,hostfwd=tcp::2222-:22- Makes port 22 of the guest accessible at
localhost:2222, which lets us connect over SSH. -bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf- The location of OpenSBI. Replace it with yours if needed, but make sure it’s the same configuration.
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf- The path of my U-Boot image.
-append "root=LABEL=rootfs console=ttyS0"- Adds extra options to the kernel command line.
-nographic- Disables graphical output, so that QEMU is a simple command-line application.
-fsdev local,security_model=passthrough,id=fsdev0,path=./share-device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare- Create a common folder between the host and guest machines.
Sharing a directory between the host and guest machines
Some people use scp to exchange files between their host and guest machines. I propose a different method: a shared folder.
We run this script on the guest machine:
SHARED_FOLDER="/mnt/share"
# Create shared folder
mkdir ${SHARED_FOLDER}
mount -t 9p -o trans=virtio,version=9p2000.L hostshare ${SHARED_FOLDER}
The contents of ./share on the host and /mnt/share on the guest are now the same.
Installing debugging and compilation tools
For reverse engineering, I sometimes need a debugger to analyze the behavior of a binary. I am familiar with GDB and Radare2, so I will show you how to use both.
On Debian, the easiest way is to install the RISC-V cross toolchain packages. They contain a compiler (GCC), an assembler and a linker, while gdb-multiarch is our favorite debugger (GDB) with RISC-V support, and qemu-user lets us run RISC-V binaries directly on the host. You can also build the RISC-V GNU toolchain from source.
sudo apt-get install gcc-riscv64-linux-gnu binutils-riscv64-linux-gnu gdb-multiarch qemu-user
GDB
To debug a RISC-V binary on the host, run it under QEMU’s user-mode emulation with a GDB stub, then attach to it from another terminal:
$ ~ qemu-riscv64 -g 1234 ./binary
$ ~ gdb-multiarch -ex "target remote :1234" ./binary
GCC
To compile a binary for the RISC-V architecture:
$ ~ riscv64-linux-gnu-gcc -g -static -o binary binary.c
Radare2
Radare2 is pretty cool for working with RISC-V binaries, since it has built-in RISC-V support. You can analyze a binary simply by running it as usual:
$ ~ r2 ./riscv-binary
Demonstration
Here is a quick reminder of how my setup is organized:
RISC-V_Setup
├── share # Directory shared between host and guest
├── src # RISC-V assembly code
└── ...
We write our assembly code in ./src and compile it into ./share to access it from the guest machine. Let’s start with program.s, a basic RISC-V program that prints “Hello World!” to the standard output (we must start somewhere :D):
#
# Risc-V Assembler program to print "Hello World!"
# to stdout.
#
# a0-a2 - parameters to linux function services
# a7 - linux function number
#
.global _start # Provide program starting address to linker
# Setup the parameters to print hello world
# and then call Linux to do it.
_start: addi a0, x0, 1 # 1 = StdOut
la a1, helloworld # load address of helloworld
addi a2, x0, 13 # length of our string
addi a7, x0, 64 # linux write system call
ecall # Call linux to output the string
# Setup the parameters to exit the program
# and then call Linux to do it.
addi a0, x0, 0 # Use 0 return code
addi a7, x0, 93 # Service command code 93 terminates
ecall # Call linux to terminate the program
.data
helloworld: .ascii "Hello World!\n"
Next, we compile this code for a 64-bit RISC-V architecture from our host machine, using two tools:
riscv64-linux-gnu-asto assemble the programriscv64-linux-gnu-ldto link the object file into an executable
I wrote a bash script, stored in ./scripts, to do the job automatically:
#!/bin/bash
ASSEMBLY_DIR="$(dirname $0)/../src"
SHARE_DIR="$(dirname $0)/../share"
riscv64-linux-gnu-as -march=rv64imac -o ${SHARE_DIR}/program.o ${ASSEMBLY_DIR}/program.s
riscv64-linux-gnu-ld -o ${SHARE_DIR}/program ${SHARE_DIR}/program.o
rm ${SHARE_DIR}/program.o
chmod +x ${SHARE_DIR}/program
We launch it:
$ ~ ./scripts/compile.sh
We now have an executable named program in /mnt/share on the guest machine. Let’s test it:
debian@debian:/mnt/share$ ./program
Hello World!
And it’s working! We can now debug the binary on the host machine with gdb-multiarch or Radare2.
Conclusion
My goal was to help those who would like to write RISC-V assembly or do reverse engineering from a Linux environment, without having a RISC-V machine. My complete setup, with installation instructions, is available on GitHub.
If you have any questions or corrections, you can reach me at r0g3r5@protonmail.com. You can also follow me on X, even if I am very (very) inactive.
I hope I helped you, or taught you something :D
