5

RISC-V Emulation Revisited

 1 year ago
source link: https://smist08.wordpress.com/2023/04/28/risc-v-emulation-revisited/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

Back in September, 2019, I posted an article presenting a RISC-V Assembly Language “Hello World” program which I ran using the TinyEMU system emulator. There hasn’t been any work on TinyEMU since then and the much more powerful QEMU emulator/virtualizer recently released their version 8. Ubuntu is now releasing install images compiled for RISC-V along with pre-installed images that are ready to run. It’s really cool that you can run/emulate a RISC-V version of Linux on an Intel/Amd version of either Windows or Linux.

qemu.jpg?w=398

Installing and Running RISC-V Linux Under Windows

Here are the instructions to install QEMU and get a RISC-V version of Linux up to the first login prompt.

  1. Install Qemu. Download the Windows install image here. Run the downloaded program saying Yes to the Windows security prompts.
  2. Add c:\Program Files\Qemu to your system PATH, assuming the default installation folder.
  3. Download the Ubuntu Server preinstalled image here.
  4. Use 7-Zip or any uncompression program that understands xz format to extract the .img file from what you downloaded.
  5. Rename the img file as ubunturv.img, or change the name in the commands that follow.
  6. Add some free space to the image with:
    qemu-img resize ubunturv.img +10G
    This gives you some room to install programs once you are running.
  7. To run the image, two additional files are required: fw_jump.elf and uboot.elf. These are easy to install on Linux, but a bit hard to find for Windows, so I placed them both here. These then act as the emulated board’s BIOS.
  8. Now we are ready to run QEMU with the rather long command:
    qemu-system-riscv64 ^
        -machine virt ^
        -cpu rv64 ^
        -m 2G ^
        -device virtio-blk-device,drive=hd ^
        -drive file=ubunturv.img,if=none,id=hd ^
        -device virtio-net-device,netdev=net ^
        -netdev user,id=net,hostfwd=tcp::2222-:22 ^
        -bios fw_jump.elf ^
        -kernel uboot.elf ^
        -append “root=LABEL=rootfs console=ttyS0” ^
        -nographic
    This should then boot Linux and eventually lead you to a login prompt.

Installing and Running RISC-V Linux Under Linux

Now let’s do the same thing for Linux.

  1. Install QEMU with:
    sudo apt install qemu-system-riscv64
    sudo apt install u-boot-qemu opensbi
  2. Extract the img file from the xz file with:
    unxz < downloaded_file_name.img.xz > ubunturv.img
  3. Expand the image, so there is some disk space to play around with:
    sudo qemu-img resize ubunturv.img +10G
  4. Now we are ready to run with the rather long qemu command:
    qemu-system-riscv64 \
        -machine virt \
        -cpu rv64 \
        -m 2G \
        -device virtio-blk-device,drive=hd \
        -drive file=ubunturv.img,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 \
        -append “root=LABEL=rootfs console=ttyS0” \
        -nographic
    This should then boot Linux and eventually lead you to a login prompt.

Logging in and Playing with RISC-V Assembly Language

The initial user id/password is ubuntu/ubuntu. The first time you login, Linux will force you to change the password. The computer name is also ubuntu which you can change if you need. Once logged in we can add the GNU Compiler Collection with:

sudo apt update
sudo apt install build-essential

This will also install GDB and a few other GCC related tools.

We can now copy the HelloWorld.s program from my previous article and paste it into vi to save it to the new image. Then we can compile and run the program:

as  -o HelloWorld.o HelloWorld.s
ld -o HelloWorld HelloWorld.o
./HelloWorld

riscvassem.png?w=1024

At this point we can use gdb to debug the program, use objdump to look at the generated executable or perform any of the usual Linux type development activities.

Summary

Emulation has come a long way. It is amazing the way QEMU can emulate so many processors and that it works quite efficiently. This is a good option for anyone that wants to learn about RISC-V processors without investing in a RISC-V board. In fact QEMU supports quite a few CPU types that you can use to learn about those processors, even IBM 390 series mainframes.

Loading...

Related

Introducing Risc-VSeptember 6, 2019In "Business"

Risc-V Assembly Language Hello WorldSeptember 7, 2019In "RiscV"

Simulating RISC-V on a Raspberry PiApril 1, 2023In "assembly language"


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK