Porting Netbsd to the RISC-V Zachary Mcgrew, Philip A
Total Page:16
File Type:pdf, Size:1020Kb
1 Porting NetBSD to the RISC-V Zachary McGrew, Philip A. Nelson Western Washington University Computer Science Department I. ABSTRACT extension (F), and the double-precision floating-point While NetBSD runs on 16 different types of CPU ar- extension (D). These extensions are what the User Level chitectures, it did not run on the RISC-V. In order to live ISA collectively refers to as the general-purpose ISA, known as G. The basic set of operations on integers are up to the slogan “Of course it runs NetBSD” the project defined as an extension, because they are not required to of completing the port of the NetBSD kernel to the new RISC-V architecture was started. Adapting the kernel to be present. Instead, an embedded form (E) of these basic take advantage of the new platform features while still instructions may be used instead, but this also presents maintaining NetBSD’s portability was challenging, but a smaller number of registers. Even things that many became and interesting problem to solve. While many would consider to be standard on a processor such as multiply and divide are an optional extension, as this issues were discovered in the process, the final outcome of booting a kernel on a new architecture was informative allows for flexibility when creating chips for embedded and rewarding. systems. The general extensions that define what would commonly be found on a desktop-class or server-class processor are bundled into the general extension (G), II. INTRODUCTION which encompasses I, M, A, F, and D. Additionally the NetBSD [1] is an open source Unix-like operating Privileged ISA specification defines the machine (M), system with roots in both 386BSD [2] and the BSD hypervisor (H), supervisor (S), and usermode (U) speci- Net/2 [3] release. NetBSD-1.0 (the second formal release fications for operating modes. The S and U modes allow of NetBSD) supported five CPU architectures [4]. Since for the Unix model of security, separating the kernel that release in 1994 it has been ported to over 16 different mode from the user mode, to be enforced in hardware. CPU architectures [5], which has lead to the slogan of The RISC-V comes in four varieties: RV32I, RV32E, “Of course it runs NetBSD” [1]. It is this slogan that RV64I, and RV128I, with the RV128I specification not advanced NetBSD on many platforms that may not even completed as of the time of this paper. be considered as a computer, such as the Dreamcast [6] This project was a Master’s project for the Computer video game console, or a toaster [7]. One of NetBSD’s Science Department at Western Washington University. original focuses was architecture independence [8], and It aimed to complete the port of the NetBSD kernel this has made it an excellent choice for porting to new to the RISC-V architecture. In particular, the port tar- architectures. To continue on the tradition of running geted the RV64GSU, or the expanded form known as NetBSD on as many different architectures as possible, RV64IMAFDSU, variety of the RISC-V. While there many people have ported NetBSD to most architectures are many more extensions that are defined in the User that are capable of supporting it. When a new open Level ISA specification, they are not needed for NetBSD source architecture that is capable of running NetBSD to function, though NetBSD could be extended to use became available it became only a matter of time until them in the future as they become available. The current a port to that architecture would take place. User Level ISA specification 2.2 defines both 32-bit The RISC-V [9] architecture is a free and open in- and 64-bit modes, the 64-bit mode was selected for the struction set architecture (ISA). The original design came NetBSD kernel port as the initial target, though there from the University of California, Berkeley for research is a possibility of supporting both in the future. This and educational purposes [10]. Currently under the con- project used a single version of both specifications, the trol of the RISC-V Foundation, it is comprised of two User Level ISA specification 2.2 and the Privileged ISA specifications: The User Level ISA Specification [11], specification 1.10. While these specifications are subject and the Privileged ISA Specification [12]. Designed as an to change in the future, they are not expected to change extensible architecture, the User Level ISA specification in a manner that breaks existing code. defines a set of extensions that may be implemented. The NetBSD port to the RISC-V had already started Some extension examples are: the base integer extension when this project was proposed. Initially contributed by (I), the multiplication and division extension (M), the Matt Thomas in early 2015, the committed code was atomic extension (A), the single-precision floating-point a good starting point, but was neither compiling nor 2 functionally correct. While some of the bugs addressed (fdt) [18]. It also relies on a boot loader to load the in the initial code were truly bugs, many of them weren’t. kernel and provide these hardware interaction services. When the port was initially started it was targeting The Berkeley Boot Loader (BBL) [19] was used when a different User Level ISA specification as well as a developing the NetBSD port. different Privileged ISA specification. It remains unclear BBL loads the kernel at a known address, and begins exactly which previous version of the specifications Matt running code at the pre-defined entry point in the ELF Thomas was using, but after reading older specifications binary that it loads. The BBL was not just built once it appears to be at least a few versions behind for both. and given a kernel file to load. Rather it is required While the specifications were mostly compatible, some to be compiled each time a new kernel was built, in key components have changed. For example, the bit order to embed the kernel inside. Additionally, BBL fields have changed in some of the crucial control and required a C library in order to compile, as it utilizes status registers (csr). Some of these settings now live in the string manipulation functions. This meant that an different bit positions, different registers altogether, or additional toolchain needed to be built, one just for BBL, have been removed entrely in favor of another option. as the toolchain that compiled the NetBSD kernel and One such example of this is in the way that floating additional tools needed to not link against or reference point support is disabled. Another large change was the a C library and required a special naming scheme. page table entries in the Privileged ISA specification. Once Spike was built, two cross-compiling toolchains Both their bit fields as a whole have changed, as well were needed. The first was used to build BBL, and the as how the leaf nodes are indicated in the page tables. second to build the NetBSD kernel. As NetBSD’s in-tree There were many subtle changes that went unnoticed version of GCC [20] was currently too old to support for extended periods of time during this port. There was the RISC-V architecture, this dual external toolchain even an instruction that was removed from the Privi- approach was used. The first toolchain was identified leged ISA specification, but the assembler would still by the triplet prefix [21] “riscv64-unknown-elf” and emit the opcode for it. The “SFENCE.VM” instruction consisted of Binutils and GCC that was built as a two- was replaced with the “SFENCE.VMA” instruction. The stage compile. The initial compile of GCC was built with subtle one character difference is all it took between the Newlib [22] flag but no path to Newlib’s headers actually emitting a valid instruction that flushes sections was defined. This was followed by the GNU Newlib of the TLB and causing an invalid instruction fault. version of the C library being compiled using the stage- These specification changes caused the majority of the 1 compiler. Finally, GCC was compiled again with the locore.S start function to need to be rewritten in Newlib flag, but was also given the path to the recently order to initialize and activate the page tables needed built Newlib headers. This toolchain was used just for for virtual memory to function. building BBL. The second toolchain used the triplet prefix “riscv64- III. SIMULATORS,TOOLCHAINS, -netbsd,” which was form that NetBSD was already AND BUILD SCRIPTS,OH MY! setup to use. It consisted of Binutils and a single stage When the project started no physical RISC-V hard- build of GCC without a C library. The “riscv64--netbsd” ware existed. In order to run RISC-V binaries a simulator toolchain was used to compile the NetBSD kernel, and needed to be built for the development system. To to provide additional libraries for the tools that NetBSD the benefit of researchers and developers, the RISC- build system needed to compile the kernel. V Foundation released a simulator supporting both the Due to the complexity of building cross-toolchains, User Level ISA and Privileged ISA specifications named a build script was created just to manage compiling Spike [13]. However, Spike has both build and run- the simulator, the boot loader, and the two toolchains. time dependencies on the “Frontend Server” (fesvr) [14]. This allowed changes to the configuration options of Fesvr enables access to the host’s resources inside the the various tools to be performed quickly, as many re- simulator.