RISC-V

This page describes the status of the FreeBSD/RISC-V port, an adaptation of the FreeBSD Operating System to run on the RISC-V Instruction-Set Architecture (ISA). As a result of this work, FreeBSD is able to boot to multi-user mode on the Spike simulator and QEMU emulator. Following a number of commits to the FreeBSD subversion repository in January 2016 (see below), FreeBSD is the first operating system to have bootable in-tree support for RISC-V. This work was described in an EE Times article on the January 2016 RISC-V workshop. Next steps include multicore support, and the ability to boot on budding RISC-V SoCs such as Cambridge's lowRISC open-source SoC. This work was supported by the DARPA CTSRD Project at the University of Cambridge Computer Laboratory and is led by RuslanBukin.

The FreeBSD Foundation posted a blog article describing the RISC-V porting effort and merge of architectural support to the FreeBSD base system in January 2016.

Though RISC-V can support up to a 48-bit virtual addressing scheme, the FreeBSD/RISC-V port currently supports the 39-bit virtual addressing scheme (r298580).

Current status

Spike

fully supported

SMP

QEMU

fully supported

SMP, virtio-block, virtio-net

Rocketchip

status unknown

lowRISC

status unknown

Source

Delivery into head:

Review

Commit

Description

D4445

r292124

kernel-toolchain

D4554

r292407

includes

D4679

r292691

rtld-elf

D4943

r294227

libc/libstand

D4982

r295041

kernel

D5014

r294571

xlint

D5020

r294573

jemalloc

D5021

r294577

compiler-rt

D5035

r294574

libunwind stubs

D5039

r294634

gcc config

D5040

r294662

libproc support

D5046

r294664

elftoolchain

D5048

r294665

usr.bin/ldd

D5064

r294908

libthread_db

D5063

r294912

libthr

D5178

r295253

reuse gp for pcpup

N/A

r295972

SMP support

N/A

r296614

DDB support

N/A

r303660

Privilege v1.9 update

N/A

r303908

Operation in machine mode removed

D8529

r308731

Full softfloat and hardfloat support

D11901

r322168

Compiler built-in macros changes

D11800

r322361

Support for privilege spec v1.10

N/A

335007

PLIC

head build status: https://ci.freebsd.org/job/FreeBSD-head-riscv64-build/

People

RuslanBukin, ArunThomas, and EdMaste are doing the work. RobertWatson, AndrewTurner, DavidChisnall, and others are providing advice and moral support.

IRC channel

The related IRC channel related to this port is #freebsd-riscv in EFnet.

Mailing list

freebsd-riscv

Toolchain

The only toolchain is supported at this moment is GNU one.

We would welcome efforts to improve RISC-V support in clang/LLVM, so we can switch. Clang/LLVM work is happening in the riscv-trunk branches of the RISC-V clang and LLVM repos:

https://github.com/riscv/riscv-clang/tree/riscv-trunk

https://github.com/riscv/riscv-llvm/tree/riscv-trunk

Sources

https://github.com/freebsd-riscv

Instructions

Setup toolchain installation directory

setenv PREFIX $HOME/riscv

Install required packages

sudo pkg install bison gmp mpfr mpc gawk gsed

Build Toolchain

git clone https://github.com/freebsd-riscv/riscv-gnu-toolchain
cd riscv-gnu-toolchain
git submodule update --init --recursive
./configure --prefix=$PREFIX
gmake -j16 freebsd
mv ${PREFIX}/lib/gcc/riscv64-unknown-freebsd11.2/8.1.0/include-fixed ${PREFIX}/lib/gcc/riscv64-unknown-freebsd11.2/8.1.0/include-fixed.rm

Alternatively, install devel/riscv64-xtoolchain-gcc, which gives you both devel/riscv64-binutils and devel/riscv64-gcc and a pre-defined riscv64-gcc.mk .

Build FreeBSD world

cd
svn co http://svn.freebsd.org/base/head freebsd-riscv
cd freebsd-riscv

When using toolchain built by hand:

setenv MAKEOBJDIRPREFIX /home/${USER}/obj/
setenv CROSS_BINUTILS_PREFIX $PREFIX/riscv64-unknown-freebsd12.0/bin/
setenv CROSS_COMPILER_PREFIX $PREFIX/bin/riscv64-unknown-freebsd12.0-
setenv XCC ${CROSS_COMPILER_PREFIX}gcc
setenv XCXX ${CROSS_COMPILER_PREFIX}c++
setenv XCPP ${CROSS_COMPILER_PREFIX}cpp
setenv STRIPBIN ${CROSS_COMPILER_PREFIX}strip

setenv X_COMPILER_TYPE gcc

make TARGET_ARCH=riscv64 buildworld

When using devel/riscv64-xtoolchain-gcc:

setenv MAKEOBJDIRPREFIX /home/${USER}/obj/
setenv WITHOUT_FORMAT_EXTENSIONS yes
make CROSS_TOOLCHAIN=riscv64-gcc TARGET_ARCH=riscv64 buildworld

Build complete rootfs image to run in QEMU

setenv DESTDIR /home/${USER}/riscv-world
make TARGET_ARCH=riscv64 -DNO_ROOT DESTDIR=$DESTDIR installworld
make TARGET_ARCH=riscv64 -DNO_ROOT DESTDIR=$DESTDIR distribution
cd  /home/${USER}/riscv-world
echo 'hostname="qemu"' > etc/rc.conf
echo "/dev/vtbd0        /       ufs     ro      1       1" > etc/fstab
echo "./etc/fstab type=file uname=root gname=wheel mode=0644" >> METALOG
echo "./etc/rc.conf type=file uname=root gname=wheel mode=0644" >> METALOG
makefs -D -f 5000000 -o version=2 -s 20g riscv.img METALOG

Build small rootfs for Spike / mdroot

fetch https://raw.githubusercontent.com/bukinr/riscv-tools/master/image/basic.files
tools/tools/makeroot/makeroot.sh -s 32m -f basic.files riscv.img $DESTDIR

Or get from https://artifact.ci.freebsd.org/snapshot/head/latest/riscv/riscv64/riscv.img.xz

Ensure you have correct path to riscv.img in your kernel config for Spike:

options         MD_ROOT                                                                                                
options         MD_ROOT_SIZE=32768      # 32MB ram disk                                                                
makeoptions     MFS_IMAGE=/path/to/riscv.img                                                                           
options         ROOTDEVNAME=\"ufs:/dev/md0\"  

Build FreeBSD kernel

make TARGET_ARCH=riscv64 buildkernel

When using devel/riscv64-xtoolchain-gcc, add CROSS_TOOLCHAIN=riscv64-gcc in make parameters.

Or get from: https://artifact.ci.freebsd.org/snapshot/head/latest/riscv/riscv64/kernel.xz

Build bbl

You must build BBL in order to boot FreeBSD

git clone https://github.com/freebsd-riscv/riscv-pk
cd riscv-pk
mkdir build && cd build
setenv PATH ${PATH}:${PREFIX}/bin
setenv CPP cpp
setenv CFLAGS "-nostdlib"
setenv OBJCOPY riscv64-freebsd-objcopy
setenv READELF riscv64-freebsd-readelf
setenv RANLIB riscv64-freebsd-ranlib
../configure --enable-logo --prefix=$PREFIX --host=riscv64-unknown-freebsd12.0 --with-payload=path_to_freebsd_kernel
gmake bbl
unsetenv CFLAGS
unsetenv CPP

Or get from: https://artifact.ci.freebsd.org/snapshot/head/latest/riscv/riscv64/bbl.xz

Build Spike simulator

# Use clang on FreeBSD
setenv CXX c++
setenv PREFIX $HOME/riscv

git clone https://github.com/freebsd-riscv/riscv-fesvr
cd riscv-fesvr
mkdir build && cd build
../configure --prefix=$PREFIX
gmake install

git clone https://github.com/freebsd-riscv/riscv-isa-sim
cd riscv-isa-sim
mkdir build && cd build
setenv CPPFLAGS -DRISCV_ENABLE_DIRTY=1
../configure --prefix=$PREFIX --with-fesvr=$PREFIX
gmake install

Or install emulators/riscv-isa-sim

Run Spike simulator

Note: we rely on FDT data built-in to the kernel, so by default we have to run spike with 2 cores. Amount of memory can be any because we use sbi_query_memory() machine call instead of FDT data at this moment.

./riscv-isa-sim/build/spike -m2048 -p2 /path/to/bbl

Run RocketChip

Create sdcard using 'Quick instructions' on https://github.com/ucb-bar/fpga-zynq, then copy bbl to sdcard (to the first msdos partition). Once you have booted linux from sdcard on Zedboard, then you can run FreeBSD/RISC-V:

./fesvr-zynq /mnt/boot/bbl

Build QEMU emulator

git clone https://github.com/riscv/riscv-qemu
cd riscv-qemu
mkdir build && cd build
../configure --target-list=riscv64-softmmu --prefix=$PREFIX
gmake
gmake install

Or install emulators/qemu-riscv

Run QEMU emulator

sudo qemu-system-riscv64 -machine virt -m 2048M -kernel /path/to/bbl -nographic -drive file=/path/to/riscv.img,format=raw,id=hd0 -device virtio-blk-device,drive=hd0 -netdev tap,ifname=tap0,script=no,id=net0 -device virtio-net-device,netdev=net0

TODO

Project

Owner

Comments

Bootloader

FreeBSD boot loader

32-bit kernel

All the Syntacore RISC-V CPUs are 32-bit, including Unix-capable.

QEMU user mode

Implement FreeBSD-bits in QEMU for user-mode emulation, required for cross-building ports

PMC

No counters available yet in hardware

busdma

The implementation of busdma(9)

DTrace FBT provider

Support for C-compressed extension has to be added

DTrace SDT provider

SDT MD part missing for new SDT implementation by markj@

KDB disassembler

br

Support for C-compressed extension has to be added

KDB disassembler

br

Needs update KDB disassembler to new ISA (instruction format changed)

pmap fixes

markj

Address TODO comments in riscv/pmap.c

kgdb and kernel dumps

Need to support offline kernel debugging

RISC-V testsuite

br

Reduce failed test cases

SSTATUS_SUM

Set SUM (Supervisor User Modify) bit in sstatus register only when we need it

minidump machdep

Implement machine dependent part for minidump

Known issues

Issue

Comments

No known issues

FreeBSD/RISC-V workshop slides video

FreeBSD Foundation blog post on the RISC-V port

ELF handling for thread-local storage

riscv (last edited 2018-08-11 11:08:16 by MateuszPiotrowski)