User:Cmsigler/RISC-V
Working Document: Planning to Port Arch to RISC-V -- please edit :)
Please edit this to your heart's content, without destroying prior work of value, or of historical or reference significance. I would like to construct a plan for porting Arch to the newly emerging RISC-V hardware.
I am very much a novice to this. I've had success running ARM SBCs via ArchLinuxArm, but I've never ported anything :\
A simple RISC-V logo
I quickly created a personal, non-professional, simple text logo for RISC-V. I don't have sysop upload privilege, so here's a link to view:
https://drive.google.com/file/d/1WKZLl0G_BriPsmRQaMz6yGtIcjJ7pqmd/view?usp=sharing
I license this logo under CC BY-SA 2.0. If it is of any interest, feel free to use it with attribution.
Target hardware
- α -- Allwinner D1 single-core XuanTie C906 64-bit RISC-V (RV64GCV) processor @ up to 1 GHz; 22nm manufacturing process (Called AllWinner D1-H (H for HDMI) now, with a cheaper D1s version that include 64MB of DDR2 RAM on SoC itself). See also this LinuxGizmos article. support Matrix of the D1 on mainline Linux.
- 2 cheap boards are available with 512MB or 1GB of RAM, Sipeed Lichee RV (https://fr.aliexpress.com/item/1005003741287162.html Dock is needed for connectors) and MangoPi MQ-Pro
- This first one is nice because it's cheap (US$12.50 IIUC), albeit underpowered compared to ARM SoC boards available at this time (November, 2020).
- β -- RISC-V International Open Source (RIOS) Laboratory is collaborating with Imagination technologies to bring PicoRio RISC-V SBC to market at a price point similar to Raspberry Pi. See also this blog article from RISC-V International.
- This second one is also projected to cost close to the RPi.
- γ -- BeagleBoard.org and Seeed unveiled an open-spec, $119-and-up “BeagleV” SBC with a StarFive JH7100 SoC with dual SiFive U74 RISC-V cores, 1-TOPS NPU, DSP, and VPU. The SBC ditches the Cape expansion for a Pi-like 40-pin GPIO. See also this LinuxGizmos article, and this ExtremeTech article.
- Initial prices are given as US$119 and US$149, which is much more affordable than the original system mobo.
- δ -- RVBoards Nezha D1, using the XuanTie C906 mentioned above.
- Current price is $99, in-line with the other boards.
- ε -- VisionFive 2, "open source quad-core RISC-V dev board", StarFive JH7110 SOC with 4x U74 cores, GPU etc. Clearly aimed straight at RasPi, has 40-pin GPIO, a bit larger form factor (100x72 vs 86x57mm), close to RPi4 CPU performance, better IO including M.2 M-Key. Already has Debian & Fedora ports, more to follow. Now in pre-order via Kickstarter, lots of good info there.
- Current price (kickstarter/preorder) is US$55-85, depending mainly on RAM (2-8GB).
- φ -- Pine64 Ox64 SBC using Bouffalolab BL808 (Looks like this company switched from SiFive closed source core (in BL602 and BL702) to faster and open source T-Head core, "open source C906 RV64GCV RISC-V microprocessor + E907 RV32 microcontroller + Bouffalo NPU accelerator. The C906 RV64 is similar to D1 one but at lower frequency, should boot Linux, and the embedded RV32 could execute microcontroller dedicated code (a Rust toolkit is developed by Bouffalo) 64MB on chip RAM.
- Current price from US$6 with 16MB flash to US$10 with 128MB flash.
(Any others? I don't plan on spending US$1,000 on a development system/motherboard.)
RiscV Arch image builder
These scripts allow to build RISC-V Arch linux image for Sipeed Licee RV, based on AllWinner D1.
Experimental development: Procedures and steps used
- Initial setup, testing for further work
- Git repository setup -- Push commits to gitlab repos, mirror gitlab to forked github repos
- Clone RISC-V repositories on github to local repos
- Fork RISC-V repositories on github
- Create gitlab repositories
- Rename github origins for cloned repos, then add gitlab repos as origins
- Push local repos to gitlab
- Configure gitlab repos to mirror to github forks using a personal access token generated on github
- Test running x86_64/amd64 installations via chroot (or systemd-nspawn)
- Gentoo:
- Create a subdirectory for the chroot installation; untar Gentoo amd64 stage3 tarball into subdirectory
$ cd ./subdir/ && sudo tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner && cd ..
- Configure and emerge:
- Configure Gentoo compilation options in
./subdir/etc/portage/make.conf
to set GENTOO_MIRRORS and edit other env vars; copy DNS info into./subdir/etc/resolv.conf
- Enter chroot environment
$ sudo arch-chroot ./subdir/
# source /etc/profile && source $HOME/.bashrc
- Optionally, update default prompt
# export PS1="(chroot) ${PS1}"
# emerge --sync
- Double-check system profile (e.g., amd64 systemd, rv64_lp64d systemd) is correct
# eselect profile list
# emerge --ask --verbose --update --deep --newuse @world
- Optionally, configure USE variable
# emerge --info | grep ^USE
# nano -w /etc/portage/make.conf
- Configure timezone
# echo "Region/Location" > /etc/timezone
# emerge --config sys-libs/timezone-data
- Configure locales
# nano -w /etc/locale.gen
Add desired locales, e.g., 'en_US.UTF-8 UTF-8', 'en_US ISO-8859-1'; remove unnecessary entries# locale-gen
- Select locale
# nano -w /etc/env.d/02locale
Add, e.g., 'LANG="en_US.UTF-8"' and 'LC_COLLATE="C"'# env-update && source /etc/profile
- Optionally, set
/etc/conf.d/hostname
; add 'hostname="system_name"' - Exit chroot environment
# exit
- Configure Gentoo compilation options in
- Chroot (or systemd-nspawn) into subdirectory and test normal administrative operations, e.g.,
# emerge --sync
# emerge --ask --verbose --update --deep --newuse @world
- Create a subdirectory for the chroot installation; untar Gentoo amd64 stage3 tarball into subdirectory
- Arch:
- Create a subdirectory for the chroot installation; install base, base-devel and a basic editor
$ sudo pacstrap -c ./subdir/ base base-devel nano
- Configure:
- Copy DNS info into
./subdir/etc/resolv.conf
- Create bind mountpoint for subdir
$ sudo mount --bind ./subdir/ ./subdir/
- Enter chroot environment
$ sudo arch-chroot ./subdir/
# source /etc/profile
- Optionally, update default prompt
# export PS1="(chroot) ${PS1}"
- Configure timezone
# ln -sf /usr/share/zoneinfo/Region/Location /etc/localtime
- Configure locales
# nano -w /etc/locale.gen
Uncomment desired locales, e.g., 'en_US.UTF-8 UTF-8', 'en_US ISO-8859-1'# locale-gen
# nano -w /etc/locale.conf
Add, e.g., 'LANG="en_US.UTF-8"' and 'LC_COLLATE="C"' - Optionally, update system
# pacman -Syu
- Optionally, set
/etc/hostname
; add 'hostname="system_name"' - Exit chroot environment
# exit
- Copy DNS info into
- Chroot (or systemd-nspawn) into subdirectory and test normal administrative operations, e.g.,
# pacman -Syu
- Create a subdirectory for the chroot installation; install base, base-devel and a basic editor
- Gentoo:
- Test running rv64_lp64d/riscv64 installations via chroot (or systemd-nspawn) using same installation and configuration procedures
- Git repository setup -- Push commits to gitlab repos, mirror gitlab to forked github repos
- RISC-V binary tests and demos
- Test cross-compiling, linking RISC-V (riscv64-lp64d) C programs
- Test building, executing on host system
- Install riscv64-linux-gnu-gcc, qemu-arch-extra, qemu-user-static-bin, binfmt-qemu-static-all-arch
- Write hello_world.c, other test programs
- Compile:
$ riscv64-linux-gnu-gcc -c -Wall -o hello_world-riscv64.o hello_world.c
- Link:
$ riscv64-linux-gnu-gcc -o hello_world-riscv64 hello_world-riscv64.o
$ riscv64-linux-gnu-gcc -static -o hello_world-riscv64-static hello_world-riscv64.o
- Run:
$ qemu-riscv64 -L /usr/riscv64-linux-gnu/ ./hello_world-riscv64
$ ./hello_world-riscv64-static
- Test building, executing under RISC-V system chroot
- Install, configure Gentoo (later, Arch) RISC-V system under chroot subdirectory
- Copy source files and cross-compiled RISC-V binaries from host system into chroot
- Enter RISC-V chroot
- Compile, link source files inside chroot using native gcc
- Test running both host cross-compiled and chroot native-compiled binaries inside chroot
- Any additional tests, experiments
- Test building, executing on host system
- Repository of pre-built RISC-V pkgs from FelixOnMars
- Configure binfmt-qemu-static to run RISC-V binaries as native programs
- Following the Arch installation guide, use pacstrap to install base pkgs into risc-v subtree
- Run arch-chroot to run subtree as a RISC-V container
- Test running RISC-V binaries from coreutils
- Test running other binaries, and installing other RISC-V pkgs from this repository
- Test using systemd-nspawn to run subtree as a RISC-V container
- Test cross-compiling, linking RISC-V (riscv64-lp64d) C programs
- Cross-bootstrapping to build basic RISC-V system
- RISC-V PKGBUILD repository pkgs from FelixOnMars
- Experiment with cross-compiling pkgs from RISC-V PKGBUILDs
- Test building cross-compiled pkgs from all PKGBUILDs
- Fix PKGBUILDs for which building fails
- Add and commit patches for RISC-V PKGBUILDs to local repo
- Push to origin (personal gitlab repo)
- Send merge request upstream
- archlinux-cross-bootstrap
- Use a build tree separate from the git source tree (is this the default?)
- Initially, test build of all 4 stages without creating build/.KEEP_GOING file to ignore build errors; read .MAKEPKGLOG in each build directory, as well as teeing output of overall build process into a log file
- Identify pkgs with build errors; debug and patch
- Individually test rebuilding pkgs which failed with errors; correct patches
- Add and commit patches to local git repository
- Push to origin (personal gitlab repo)
- Send merge request upstream
- RISC-V PKGBUILD repository pkgs from FelixOnMars
Basic thoughts and ideas for porting
Notes 2021/01/31:
Finally making some more progress:
- Tested creating a Gentoo amd64/systemd chroot by unpacking a stage3 tarball into a target subdir, then pre-configuring, chrooting (via arch-chroot) into the target, and updating and configuring as needed the chroot system. This seems to work fine. Also installed a minimal X windows program, qemacs, inside the chroot and ran it after allowing X windows programs access to the parent X server.
- Installed Arch base and base-devel into a target subdir via
pacstrap -c
, then chrooted (via arch-chroot) into the target and configured the installation for timezone, locale and hostname. - Wrote, tested and debugged a few simple C programs on x86_64 in order to test compilation for the RISC-V riscv64-lp64d target. Then, after installing riscv64-linux-gnu-gcc to cross-compile, compiled and linked these to produce riscv64-lp64d binaries. After installing qemu-arch-extra, tested running them with
qemu-riscv64 -L /usr/riscv64-linux-gnu/
.
Additional info 2020/11/14:
Thanks to eschwartz, here are links to resources produced by FelixOnMars, who has tackled some RISC-V porting in his spare time:
There's a lot of finished work he's done -- good stuff :) and one shouldn't be spending most of one's efforts reinventing the wheel ;)
Revised 2020/11/13:
In essence, what I'm looking to do is:
- Build RISC-V pkgs for Arch.
- Build bare-metal RISC-V Arch disk images that can potentially be booted on physical hardware. Of course, device tree/drivers will have to be individually customized for each mainboard/SBC. Getting a disk image that works in KVM/QEMU would scratch my itch of getting most of the work done. Someone with more bare-metal hardware/driver work may have to put the finishing touches on it.
Hoping my understanding and itemized steps are more-or-less correct.... Methods to make porting progress:
- So, if we want to build Arch RISC-V pkgs, we can simply cross-compile them. When a pkg won't build, we patch it until it does (assuming we can eventually get it working).
- Note that Vadim Kaushan (Disasm on github) has patched upstream parabola-cross-bootstrap from Andreas Grapentin (oaken-source on github) to create archlinux-cross-bootstrap. The last commit was January, 2019. Some things may have b0rken but it's worth trying (and patching) this to cross compile base-devel pkgs to RISC-V. Then we can perhaps extend its coverage to more repository pkgs.
- We can unpack a RISC-V stage tarball (or image). Then we can chroot, or use something really fancy like systemd-nspawn, into that tree and use QEMU user-mode emulation to run RISC-V binaries.
- Question: Will this even work? Answer: Yes! Host needs extra, AUR pkgs qemu-arch-extra and/or qemu-user-static (or qemu-user-static-bin), and binfmt-qemu-static (or binfmt-qemu-static-all-arch). Copy required binaries down into the chroot tree, and/or take care of binfmt_misc support.
- The AUR pkg proot, PRoot wiki page, may be useful.
- Note that to run dynamically linked binaries the linker path must be passed; see Running ARM Programs under Linux (also listed below). Quote:
- If you want a dynamically-linked executable, you've to pass the linker path too:
arm-linux-gnueabihf-gcc -ohello hello.c
qemu-arm -L /usr/arm-linux-gnueabihf/ ./hello # or qemu-arm-static
- If you want a dynamically-linked executable, you've to pass the linker path too:
- Some references:
- Chrooting into arm/arm64 environment from x86_64
- Transparently running binaries from any architecture in Linux with QEMU and binfmt_misc
- Stackexchange: Chroot into a filesystem with a different architecture
- How to set up binfmt_misc for qemu the hard way
- Gentoo Embedded Handbook/Compiling with qemu user chroot
- Debian QEMU User Emulation
- Running ARM Programs under Linux
- Creating Arch pkgs in openSUSE Open Build Service; hmmm, pkgs could be built automatically and remotely...
- I'm not an lxc/Docker/Kubernetes guy, but IIUC this would be roughly similar to running a RISC-V container (no?). This may come in handy to make sure binary packages work as expected, I suppose. But it doesn't seem like a better way to build RISC-V pkgs than cross-compiling, although we could build dev infrastructure then pkgs inside the container.
- We can create a VM disk image (GPT, of course) and unpack inside it, e.g., a RISC-V Gentoo stage3 and complete installation and configuration. We also need to compile a RISC-V kernel and bbl (Berkeley Boot Loader from the riscv-pk github project) (some instructions here and here), or we may be able to download pre-built kernel/binaries/utilities. Then we can boot the image with KVM/QEMU.
- Also available are Debian RISC-V, Fedora RISC-V and openSUSE RISC-V images. In those wiki pages are some instructions for putting together bare-metal disk images.
- Also, there are ready-made QEMU images that one can download and run. See Debian, Fedora, additional Fedora, AltLinux. I guess this ready-made solution would be the ultimate in ease of use, but it doesn't solve the problem of building from scratch. But at least inside a running VM I could download and build dev infrastructure (pacman, et al) and then build pkgs as if I were running on bare metal. It might be only a little slower than running on a SBC?...
So, in the end, the best solutions seem to be:
- Cross-compiling using existing scripting/tooling to build Arch pkgs. archlinux-cross-bootstrap should build base-devel pkgs. Using its scripts with modified PKGBUILDs, it should be possible to build other repository pkgs.
- To test cross-compiled pkgs, create a chroot to use via QEMU user-mode emulation (after setting up qemu-user-static and binfmt_misc; note proot). Follow the usual installation instructions using pacstrap, etc., to install RISC-V pkgs inside the chroot, then complete installation and configuration.
- Note that this creates a running RISC-V Arch system.... Tinker with it to your heart's content. Then you could make an archive (tar) snapshot of it and use this as the basis of a bare-metal VM image :)
- However, since these cross-compiled pkgs will contain dynamically linked binaries, I'm not sure ATM how binfmt_misc will work to run them with the "-L" flag to reference the RISC-V linker path? Although, once inside the chroot will user-mode QEMU just use the .so libraries inside the chroot?....
- For final image building, create a VM disk image, follow the usual installation instructions to install an Arch RISC-V system inside the image, configure it just like a bare-metal installation, and see if it works :)
- When running the VM disk image like bare metal, bootloading to boot the kernel will need to work. Otherwise, the bootloader and kernel used to boot and run the VM will have to be exterior to the disk image. We want to create a turnkey disk image including bootloading....
Revised 2020/11/11:
Thanks to Awebb for helping to distill disorganized ideas.
Perhaps the best way to make progress on porting to RISC-V (mainboard and embedded/SBC) is simply to build Arch packages. The bootstrapping and embedded intricacies can be dealt with later. Plus, building packages can be done bit by by when time is available. So....
There are two ways to build packages, I believe:
- Cross-compile using available Arch RISC-V tooling (see below)
- Install an existing RISC-V system in a KVM/QEMU VM
Frankly, the VM method seems the easiest (and laziest) way. (Update: Probably not as easy/lazy as I was thinking.) There are Gentoo stage3 tarballs so, assuming RISC-V support in KVM/QEMU is generally bug-free, setting up a RISC-V Gentoo VM should be a mechanical process. After this, Arch tools would be compiled inside the VM, and then base, base-devel and then core pkgs would be built. (For related information, see the Gentoo 32-bit RaspberryPi page and the Gentoo 64-bit RaspberryPi page.)
See links below for documentation and instructions.
Original text:
According to the initial press release on the above α hardware target/SoC board, this "Allwinner RISC-V processor will run the Debian Linux operating system (Tina OS)." Provided that "Tina OS" licensing is "clean," one should be able to boot the supplied Debian OS and use that to develop an Arch bootstrap, or perhaps an entire port?
IIUC, that should include the bootstrapping and device tree stuff along with device drivers for on-board equipment. (Drivers for optional devices and for devices on available modules/daughter cards will need to be developed, too.)
After that, base and base-devel packages need to be built. Then building packages can be done on bare metal. However, it would obviously be faster and easier to build them using the available RISC-V toolchain in Arch. Right? (I wonder how ArchLinuxArm handles building packages for and maintaining their repositories, as well as their AUR? Note to self: Research ALARM infrastructure....)
Topics to be researched and written up or references pointed to:
- HOWTO bootstrap a new port from ground zero -- See references below
- How the bootstrapping process is booted/initiated
- How device trees work
- How device drivers work in bootstrapping, e.g., via initrd image
- Cross-building Arch packages for RISC-V
- Infra for a new port's repositories (CB/CI tools, repository hosting, front end w/hosting, etc.)
- Infra for a new port's AUR
- Possibility of free/open (non-binary blob) bootstrapping image/BIOS (similar to coreboot)
Necessary tools for porting
KVM/QEMU RISC-V emulation documentation, information and instructions
qemu-arch-extra pkg required for RISC-V (and other architecture) full-system emulation
qemu-user-static (or qemu-user-static-bin) for running QEMU user-mode emulation inside a chroot; binfmt-qemu-static (or binfmt-qemu-static-all-arch) for these
Arch QEMU pkgs and variants from repository and AUR
RISC-V Linux on QEMU Getting Started Guide
(See also: SiFive RISC-V QEMU Upstream Announcement)
How To Run Linux on RISC-V with QEMU Emulator
Tools available under Arch
Tools from repositories
Results of "riscv" keyword search
Tools from AUR
Results of AUR "RISCV" keyword search
Results of AUR "risc-v" keyword search
Tools from other dists
Obsolete: Experimental Gentoo stages
Parabola GNU/Linux-libre QEMU image
Non-Arch tools
Cross-Linux From Scratch (this project seems to be dormant...)
As-is experimental RISC-V pkgs from work done by FelixOnMars
References -- PLEASE add to and curate this list!
https://github.com/archlinux-riscv-abandoned/archlinux-cross-bootstrap
https://bbs.archlinux.org/viewtopic.php?id=237370
https://bbs.archlinux.org/viewtopic.php?id=260639
https://five-embeddev.com/toolchain/2019/06/26/gcc-targets/
https://wiki.qemu.org/Documentation/Platforms/RISCV
https://wiki.gentoo.org/wiki/Project:RISC-V
https://wiki.gentoo.org/wiki/Raspberry_Pi (for embedded build and cross-compiling info)
https://wiki.gentoo.org/wiki/Raspberry_Pi_3_64_bit_Install
https://wiki.archlinux.org/index.php/Cross-compiling_tools_package_guidelines
https://github.com/crosstool-ng/crosstool-ng
https://git.busybox.net/buildroot
https://github.com/michaeljclark/busybear-linux
https://github.com/janvrany/riscv-debian
(http://dl.sipeed.com/shareURL/MAIX/K210_Linux/Firmware)
https://archlinuxpower.org (WIP RISC-V Arch port co-located with PowerPC Arch builds, might be good as a source base) https://github.com/kth5/archpower