logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

Porting-Alpine-Linux-to-RISC-V.md (6116B)


  1. ---
  2. date: 2018-12-20
  3. layout: post
  4. title: Porting Alpine Linux to RISC-V
  5. tags: ["alpine linux", "risc-v"]
  6. ---
  7. I recently received my [HiFive Unleashed][hifiveu], after several excruciating
  8. months of waiting, and it's incredibly cool. For those unaware, the HiFive
  9. Unleashed is the first consumer-facing Linux-capable [RISC-V][riscv] hardware.
  10. For anyone who's still lost, RISC-V is an [open](https://github.com/riscv),
  11. royalty-free [instruction set
  12. architecture](https://en.wikipedia.org/wiki/Instruction_set_architecture), and
  13. the HiFive is an [open](https://github.com/sifive) CPU implementing it. And here
  14. it is on my dining room table:
  15. [hifiveu]: https://www.sifive.com/boards/hifive-unleashed
  16. [riscv]: https://en.wikipedia.org/wiki/RISC-V
  17. ![](https://sr.ht/JMao.jpg)
  18. This board is *cool*. I'm working on making this hardware available to
  19. [builds.sr.ht][srht] users in the next few months, where I intend to use it to
  20. automate the remainder of the Alpine Linux port and make it available to any
  21. other operating systems (including non-Linux) and userspace software which are
  22. interested in working on a RISC-V port. I'm fairly certain that this will be the
  23. first time hardware-backed RISC-V cycles are being made available to the public.
  24. [srht]: https://meta.sr.ht
  25. There are two phases to porting an operating system to a new architecture:
  26. bootstrapping and, uh, porting. For lack of a better term. As part of
  27. bootstrapping, you need to obtain a cross-compiler, port libc, and cross-compile
  28. the basics. Bootstrapping ends once the system is *self-hosting*: able to
  29. compile itself. The "porting" process involves compiling all of the packages
  30. available for your operating system, which can take a long time and is generally
  31. automated.
  32. The first order of business is the cross-compiler. RISC-V support landed in
  33. binutils 2.28 and gcc 7.1 several releases ago, so no need to worry about adding
  34. a RISC-V target to our compiler. Building both with
  35. `--target=riscv64-linux-musl` is sufficient to complete this step. The other
  36. major piece is the C standard library, or libc. Unlike the C compiler, this step
  37. required some extra effort on my part - the RISC-V port of musl libc, which
  38. Alpine Linux is based on, is a work-in-progress and has not yet been upstreamed.
  39. There does exist [a patch][musl-port] for RISC-V support, though it had never
  40. been tested at a scale like this. Accordingly, I ran into several bugs, for
  41. which I wrote several patches ([1][1] [2][2] [3][3]). Having a working distro
  42. based on the RISC-V port makes a much more compelling argument for the maturity
  43. of the port, and for its inclusion upstream, so I'm happy to have caught these
  44. issues. Until then, I added the port and my patches to the Alpine Linux musl
  45. package manually.
  46. [musl-port]: https://github.com/riscv/riscv-musl
  47. [1]: https://github.com/riscv/riscv-musl/pull/2
  48. [2]: https://github.com/riscv/riscv-musl/pull/3
  49. [3]: https://github.com/riscv/riscv-musl/pull/4
  50. A C compiler and libc implementation open the floodgates to porting a huge
  51. volume of software to your platform. The next step is to identify and port the
  52. essential packages for a self-hosting system. For this, Alpine has a great
  53. [bootstrapping script][bootstrap.sh] which handles preparing the cross-compiler
  54. and building the base system. Many (if not most) of these packages required
  55. patching, tweaks, and manual intervention - this isn't a turnkey solution - but
  56. it is an incredibly useful tool. The most important packages at this step are
  57. the native toolchain[^1], the package manager itself, and various other useful
  58. things like tar, patch, openssl, and so on.
  59. [bootstrap.sh]: https://git.alpinelinux.org/cgit/aports/tree/scripts/bootstrap.sh
  60. [^1]: Meaning a compiler which both *targets* RISC-V and *runs* on RISC-V.
  61. Once the essential packages are built and the system can compile itself, the
  62. long porting process begins. It's generally wise to drop the cross-compiler here
  63. and start doing native builds, if your hardware is fast enough. This is a
  64. tradeoff, because the RISC-V system is somewhat slower than my x86_64 bootstrap
  65. machine - but many packages require lots of manual tweaks and patching to get
  66. cross-compiling working. The time saved by not worrying about this makes up for
  67. the slower build times[^2].
  68. [^2]: I was actually really impressed with the speed of the HiFive Unleashed. The main bottleneck is the mmcblk driver - once you get files in the kernel cache things are quite pleasant and snappy.
  69. There are thousands of packages, so the next step for me (and anyone else
  70. working on a port) is to automate the remainder of the process. For me, an
  71. intermediate step is integrating this with builds.sr.ht to organize my own work
  72. and to make cycles available to other people interested in RISC-V. Not all
  73. packages are going to be ported for free - but many will! Once you unlock the
  74. programming languages - C, Python, Perl, Ruby[^3], etc - most open source
  75. software is pretty portable across architectures. One of my core goals with
  76. sr.ht is to encourage portable software to proliferate!
  77. [^3]: I have all four of these now!
  78. If any readers have their own RISC-V hardware, or want to try it with qemu, I
  79. have a RISC-V Alpine Linux repository here[^4]. Something like this will install
  80. it to /mnt:
  81. ```sh
  82. apk add \
  83. -X https://mirror.sr.ht/alpine/main/ \
  84. --allow-untrusted \
  85. --arch=riscv64 \
  86. --root=/mnt \
  87. alpine-base alpine-sdk vim chrony
  88. ```
  89. Run `/bin/busybox --install` and `apk fix` on first boot. This is still a work
  90. in progress, so configuring the rest is an exercise left to the reader until I
  91. can clean up the process and make a nice install script. Good luck!
  92. [^4]: [main](https://mirror.sr.ht/alpine/main/), [community](https://mirror.sr.ht/alpine/community/), [testing](https://mirror.sr.ht/alpine/testing/)
  93. ---
  94. Closing note: big thanks to the help from the community in #riscv on Freenode,
  95. and to the hard work of the Debian and Fedora teams paving a lot of the way and
  96. getting patches out there for lots of software! I still got to have all the fun
  97. working on musl so I wasn't entirely on the beaten path :)