RISC-V

 
 
RISC-V is an open standard instruction set architecture (ISA) based on established reduced instruction set computer (RISC) principles. The RISC-V ISA is provided under open source licenses that do not require fees to use.

More information about RISC-V can be found at:
https://riscv.org/








How to build QEMU on macOS.



Information
QEMU (Quick EMUlator) is an open source virtual machine for Linux, Mac, Windows, with ports to various BSDs and sundry as well.
It supports a wide range of guest architectures – x86, the various flavors of ARM, m68k, S390x, and RISC-V.

QEMU can emulate both 32-bit and 64-bit RISC-V CPUs.
Use the qemu-system-riscv64 executable to simulate a 64-bit RISC-V machine, qemu-system-riscv32 executable to simulate a 32-bit RISC-V machine.

The RISC-V QEMU port is developed in the upstream QEMU repository.

More information about QEMU: https://www.qemu.org/

Operating system used
macOS Monterey

Software prerequisites
Homebrew

Procedure
  • This tutorial is a continuation of tutorial:
    How to build the Nuclei RISC-V GNU Compiler Toolchain (riscv-gnu-toolchain) on macOS.
    Please follow that tutorial first, otherwise the steps below do not make any sense.

    The qemu submodule:

    The qemu submodule

  • The upstream qemu submodule location is:
    https://git.qemu.org/git/qemu.git

    See: ~/temp/riscv-gnu-toolchain/.gitmodules

  • Goto qemu folder.
    Type: cd ~/temp/riscv-gnu-toolchain/qemu
    Type: git submodule update --init --recursive

  • Create directory ~/temp/riscv-gnu-toolchain/qemu/build where the build is stored.
    Type: mkdir build

  • Create the Makefile.
    Type: cd build

    The --prefix option does not seem to work.
    That is why I execute the configure script INSIDE the build directory.

    Type: ../configure --target-list=riscv32-softmmu,riscv64-softmmu
    Both riscv32-softmmu and riscv64-softmmu are being build.

    More information about configure:
    Type: ./configure --help

    mmu stands for memory management unit.

  • Build qemu, it will take approx. 5 min depending on your machine
    Type: make

  • The following tools were build:
    ~/temp/riscv-gnu-toolchain/qemu/build/riscv32-softmmu/qemu-system-riscv32
    ~/temp/riscv-gnu-toolchain/qemu/build/riscv64-softmmu/qemu-system-riscv64

  • To build again, first delete generated files:
    Type: make distclean && rm -rf *-linux-user *-softmmu"

  • Instead of building QEMU yourself, you can install it using homebrew.
    Type: brew install qemu

  • QEMU is installed at:
    /usr/local/bin/

    qemu-edid
    qemu-img
    qemu-io
    qemu-nbd
    qemu-storage-daemon
    qemu-system-aarch64
    qemu-system-alpha
    qemu-system-arm
    qemu-system-avr
    qemu-system-cris
    qemu-system-hppa
    qemu-system-i386
    qemu-system-m68k
    qemu-system-microblaze
    qemu-system-microblazeel
    qemu-system-mips
    qemu-system-mips64
    qemu-system-mips64el
    qemu-system-mipsel
    qemu-system-nios2
    qemu-system-or1k
    qemu-system-ppc
    qemu-system-ppc64
    qemu-system-riscv32
    qemu-system-riscv64
    qemu-system-rx
    qemu-system-s390x
    qemu-system-sh4
    qemu-system-sh4eb
    qemu-system-sparc
    qemu-system-sparc64
    qemu-system-tricore
    qemu-system-x86_64
    qemu-system-xtensa
    qemu-system-xtensaeb

  • In this tutorial I will be using the homebrew QEMU.

  • Type: which qemu-system-riscv32

    /usr/local/bin/qemu-system-riscv32

  • Type: qemu-system-riscv32 --version

    QEMU emulator version 6.2.0
    Copyright (c) 2003-2021 Fabrice Bellard and the QEMU Project developers


  • Type: qemu-system-riscv32 -machine help

    Supported machines are:
    none         empty machine
    opentitan    RISC-V Board compatible with OpenTitan
    sifive_e     RISC-V Board compatible with SiFive E SDK
    sifive_u     RISC-V Board compatible with SiFive U SDK
    spike        RISC-V Spike board (default)
    virt         RISC-V VirtIO board


  • More information about qemu-system-riscv32:
    Type: qemu-system-riscv32 --help
    See: qemu_system_riscv32_help.txt

  • In tutorial How to build the Nuclei RISC-V GNU Compiler Toolchain (riscv-gnu-toolchain) on macOS, I created a hello program.

    This program can be run using the qemu-system-riscv32 executable to simulate a 32-bit RISC-V machine.

    Type: cd ~/temp
    Type: qemu-system-riscv32 -machine virt -m 128M -bios none -device loader,file=./hello -nographic -s

  • To terminate the QEMU terminal:
    Press keys: CTRL + A
    Press key: x


  • I was not able to make QEMU to work.