DEV Community

loading...

How to build xv6-riscv on mac

Tingwei
A journey of self-discovery
・2 min read

There're three parts:

  1. riscv-gnu-toolchain installation
  2. qemu installation
  3. build xv6-riscv

Part1: riscv-gnu-toolchain installation

Env: macOS Catalina 10.15.7

Step1:

git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
Enter fullscreen mode Exit fullscreen mode

Although it doesn't need --recursive in README, this issue still happened on mac.

Step2:

brew install python3 gawk gnu-sed gmp mpfr libmpc isl zlib expat
Enter fullscreen mode Exit fullscreen mode

Step3:

cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv --enable-multilib
Enter fullscreen mode Exit fullscreen mode

To build either cross-compiler with support for both 32-bit and 64-bit

Step4: find your .zshrc, append this line

export PATH="$PATH:/opt/riscv/bin"
Enter fullscreen mode Exit fullscreen mode

and then

source .zshrc
Enter fullscreen mode Exit fullscreen mode

Part2: qemu installation

After installing riscv-gnu-toolchain, I chose to building qemu for macOS first and installed qemu.

Step 1: install prerequisites

brew install libffi gettext glib pkg-config autoconf automake pixman ninja
Enter fullscreen mode Exit fullscreen mode

Explained

Step2: pull the source of qemu

curl -L https://download.qemu.org/qemu-5.2.0.tar.xz > qemu-5.2.0.tar.xz
Enter fullscreen mode Exit fullscreen mode

(why qemu 5.2.0)

Step3: unzip

tar xf qemu-5.2.0.tar.xz
Enter fullscreen mode Exit fullscreen mode

Step4: build and install qemu

cd qemu-5.2.0
Enter fullscreen mode Exit fullscreen mode

and

mkdir build && cd build
../confgiure`
make
make install
Enter fullscreen mode Exit fullscreen mode

Part3: build xv6-riscv

After installing qemu sucessfully, let's start to build xv6-riscv

Step1: Pull the source of xv6-riscv

git clone git://github.com/mit-pdos/xv6-riscv-fall19.git
Enter fullscreen mode Exit fullscreen mode

NOTE: The xv6-riscv-fall19 repository differs slightly from the book's xv6-riscv in order to make the labs easier.

Step2:

cd xv6-riscv-fall19
Enter fullscreen mode Exit fullscreen mode

and

make
make qemu
Enter fullscreen mode Exit fullscreen mode

That's it!
Thanks for your reading~

Discussion (2)

Collapse
tingwei628 profile image
Tingwei Author

Here is a RISC-V "Hello World" example

Step 1: write rv-hello.s


.global _start                                                                                      

_start: addi  a0, x0, 1
        la    a1, helloworld
        addi  a2, x0, 13
        addi  a7, x0, 64
        ecall

        addi    a0, x0, 0
        addi    a7, x0, 93
        ecall

.data
helloworld:      .ascii “Hello World!\n” 
Enter fullscreen mode Exit fullscreen mode

Step2: assemble rv-hello.s and link

riscv64-unknown-elf-gcc -o rv-hello rv-hello.s -nostdlib -static
Enter fullscreen mode Exit fullscreen mode

Step3: run the program

riscv64-unknown-elf-run rv-hello
Enter fullscreen mode Exit fullscreen mode

// Hello World!

Collapse
tingwei628 profile image
Tingwei Author • Edited

Exit qemu : Ctrl + a then x