Click here to Skip to main content
15,924,317 members
Articles / Containers / Virtual Machine
Tip/Trick

Run Debian RISC-V In QEMU: Virtual Environment For Assembly development

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
28 May 2024CPOL3 min read 1.7K   2   8
After a lot of research, I’ve discovered the simplest way to get a QEMU RISC-V Linux image running.
Get a RISC-V-based Debian installation up and running in QEMU (emulator) extremely fast so you can write RISC-V Assembly for running and debugging.

Introduction

I've also published a similar article at my blog, at the following link: 

Run Debian RISC-V In QEMU: Virtual Environment For Assembly development[^]

Background

I recently stumbled upon this new book that I’m reading right now:

RISC-V Assembly Language Programming: Unlock the Power of the RISC-V Instruction Set[^]

It is a fantastic book for learning Assembly running on RISC-V machines : the RISC-V ISA (Instruction Set Architecture).

The book explains how to run a RISC-V based version of Ubuntu in the QEMU emulator.

After a lot of research, I’ve discovered the simplest way to get a QEMU RISC-V Linux image running. This one will run Debian and it is far smaller than the Ubuntu one too so it will consume less memory on your main Linux (where you run QEMU).

The Simple Steps

Here are the simple steps:

Pre-Step

Install QEMU on your system.  Follow the instructions at the official QEMU site^.


1) go to Debian Quick Image Baker pre-baked images[^] — this contains all the latest build images for QEMU virtualization (including a RISC-V build.

2) When you get there, look for the link labeled: “Images for riscv64-virt” — clicking that link (Images for riscv64-virt[^]) will instantly begin the download of a (zip) file which is named very oddly, like a hash value (2baed3d2ab30e7a4ff39c7e587c8b16dce3885afb2dc047dae555a8bc13e is the current name).

3) Once you download it, you can unzip the contents to its own folder to keep everything organized.

The contents of that file are:

  • image.qcow2 — image file
  • initrd – linux stuff
  •  kernel – linux kernel
  •  readme.txt – readme provides command line for QEMU
  •  ssh_user_ecdsa_key — stuff I don’t know
  •  ssh_user_ed25519_key — don’t know
  •  ssh_user_rsa_key — don’t know.

The readme contains the QEMU command that you can use to start virtual environment.

We Need Two Additional Files

We actually need two additional files which are the bios and the kernel files for RISC-V Linux.

  1. fw_jump.elf
  2. uboot.elf

These files are part of the Open Source Supervisor Binary Interface.

For more information you can learn about them at : 

GitHub - riscv-software-src/opensbi: RISC-V Open Source Supervisor Binary Interface[^]

Download uboot.elf & fw_jump.elf

You can actually download those two files and place them anywhere you want on your local computer.

RISC-V-Assembly-Language-Programming/Chapter 1/QEMU at main · Apress/RISC-V-Assembly-Language-Programming · GitHub[^]

Make sure that when you download each file that you download the RAW file.  GitHub does a weird thing and will download them as odd text files if you don't use the RAW option.

Download To Your Local Machine

Just make sure you reference the full path to them (wherever you placed them on your machine) when you run the QEMU command below.

You can see those two files are referenced in the QEMU command below.

Note: There cannot be any spaces after the line-continuation-character (\).

I've broken the command up over lines so you can see each argument which is passed to the QEMU executable?  Did you know there is a separate EXE for each QEMU platform (riscv64, x86_64, etc.)?  

BAT
qemu-system-riscv64 -machine 'virt' \
-cpu 'rv64' \
-m 1G -device virtio-blk-device,drive=hd \
-drive file=image.qcow2,if=none,id=hd \
-device virtio-net-device,netdev=net \
-netdev user,id=net,hostfwd=tcp::2222-:22 \
-bios /usr/lib/riscv64-linux-gnu/opensbi/generic/fw_jump.elf \
-kernel /usr/lib/u-boot/qemu-riscv64_smode/uboot.elf \
-object rng-random,filename=/dev/urandom,id=rng \
-device virtio-rng-device,rng=rng \
-nographic \
-append "root=LABEL=rootfs console=ttyS0"

Once you start the virtual environment login using :
root (username) and root (pwd)

Now you can install the build_essential packages so you can write assembly and link.
You are root so no need for sudo:

BAT
$ apt install build-essential

You can check that the Assembler (as) and the linker (ld) are installed after that, by doing the following which will give you the versions of each exe.

BAT
$ as --version

$ ld --version

Once you do that.

You can open up the nano editor and write your asm and go through the rest of the samples in the book (RISC-V Assembly Language Programming: Unlock the Power of the RISC-V Instruction Set[^]).

Points of Interest

If you've ever wanted to emulate machines running QEMU (QEMU Offical site / docs[^]) this will be a good starter for you.  I had to work through a lot to get this going since I've never ran QEMU on my own before.

If you try this out and get Debian running on your machine, I think you'll be pleasantly surprised.

I got this running on my Mac Pro M3 Laptop too.  I'll write that one up soon too.

History

May 28, 2024 - First publication

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) RADDev Publishing
United States United States
"Everything should be made as simple as possible, but not simpler."

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA31-May-24 8:21
professionalȘtefan-Mihai MOGA31-May-24 8:21 
Suggestionthanks but Pin
Pernath31-May-24 7:34
Pernath31-May-24 7:34 
GeneralRe: thanks but Pin
raddevus1-Jun-24 6:52
mvaraddevus1-Jun-24 6:52 
GeneralRe: thanks but Pin
Pernath1-Jun-24 7:51
Pernath1-Jun-24 7:51 
GeneralRe: thanks but Pin
raddevus1-Jun-24 12:19
mvaraddevus1-Jun-24 12:19 
Thanks again. I updated the article and made additions / fixes you suggested. Thumbs Up | :thumbsup:
I also updated the blog article too.
GeneralRe: thanks but Pin
Pernath1-Jun-24 22:32
Pernath1-Jun-24 22:32 
Questiondevelopment Pin
WeDigital India28-May-24 23:59
professionalWeDigital India28-May-24 23:59 
AnswerRe: development Pin
raddevus29-May-24 2:56
mvaraddevus29-May-24 2:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.