After initially gearing up to use the Motorola 68020 or 68030 as a porting target for a
study of Unix in the 1980-1985 era, I reconsidered and used Risc-V as a target instead. As
the original RISC and MIPS projects were contemporaneous with early 32-bit Unix (32V, BSD,
SysIII and SVr1,r2) it seems appropriate and there is currently considerable interest
(hype?) around Risc-V.
From a programming perspective, the Risk-V ISA does not feel (at least to me) all that
different from what was current in the early 80’s — the number of WTFs/min is low. The
modularity is a pleasant surprise, as is the observation that the 32-bit and 64-bit
instruction sets are almost identical and that compressed instructions mingle nicely with
full size ones. The MMU design appears straightforward. Maybe this is because the ISA is
still relatively new and has not acquired much historical baggage at this point in its
lifespan, but it also seems to be a good synthesis of insights gained over the last 4
decades and applied with a sense of minimalism.
At first I was thinking to create a toolchain based on pcc or pcc2 for the SysIII porting
effort, based on some preparation I had done when I was still thinking about 68030 as a
target (the surviving Blit code includes a pcc-based 68000 compiler and the SysV/68 source
archive contains a pcc2-based compiler). Before I got underway with that, I came across a
presentation Richard Miller had done about his Risc-V compiler:
https://riscv.org/news/2020/10/a-plan-9-c-compiler-for-rv32gc-and-rv64gc/
Richard was kind enough to share the source code for his Risc-V back-end. The first
complication was that the source code assumes that it will be running inside a Plan-9
environment, whereas I was looking for a Unix/Posix environment. Luckily somebody already
had assembled the libraries needed for this:
https://github.com/aryx/fork-kencc
I’m not sure where it came from, but I would assume it has some roots in the "Plan-9
from user space" effort. From this work I extracted the minimum needed to get the C
compiler working and to build from scratch. The libraries mostly just worked. The compiler
was a bit harder: the source code assumes a LLP64 model in a few places and compiling this
with clang (which uses a LP64 model) introduces issues in a handful of places. Other than
this initial hurdle, the compiler and tools have worked flawlessly, both for 64-bit code
and for 32-bit code, and have been a joy to use. One particular nicety is that Plan 9
style "abstract assembler" source for 64-bit code is even more identical to its
32-bit variant than with the mainstream Risc-V assembler syntax. My repo for the tool
chain is here:
https://gitlab.com/pnru/riscv-kencc
Initially, my expectation was that I could only use these compilers as cross-compilers and
that I would need to do a pcc2 version for native compilation at some point. However, when
I saw how small and fast the tools were, I experimented with using them on SysIII. Much to
my surprise the effort required was absolutely minimal, all that was needed was adding a
dozen simple standard functions in libc (see here:
https://gitlab.com/pnru/SysIII_rv64/-/tree/master/libc/compat) and adding the ‘dup2'
sys call. I had not expected that SysIII was so close to the Unix systems of the 1990’s in
this regard. This result inspires ideas for future projects: as I plan to add an 8th
edition style file system switch anyway, maybe it will not be all that hard to make the
Plan-9 debugger work on this “SysIII+” as well.
Another observation has been that the code size of binaries compiled for Risc-V by this
tool chain is almost the same as those compiled for the VAX by pcc (the Risc-V ones are
maybe 10-20% larger). This is using the compressed instructions where possible. This is I
think another indication that both the Risc-V ISA and the tool chain are quite well done.
The one less positive surprise has been the memory use of the compiler. Even on a
relatively simple program file it will quickly use 1 megabyte or more of ram. I understood
from Richard that this is because the compiler only malloc()’s and never free()’s by
design. This has been a mixed blessing. Such large memory images don’t work all that well
with the "scatter paging + partial swapping" memory management of SysIII when
memory is constrained to say 4MB of core to mimic the systems of the era. On the other
hand, parallel compiling the kernel on SysIII itself heavily exercises the partial
swapping code and has been a great test case for debugging.
Many thanks to Ken, Rob, Richard and all the others who created this fine tool chain!