Editor: Zhang Handong

RCore OS tutorial Introduction

As we all know, the operating system course of Tsinghua University is a national excellent course. Tsinghua University is also the first university in China to use Rust for operating system teaching. At present, Chen yu and his student Wu Yifan are working on a new textbook on operating systems. Rcore-os.github. IO/rcore-tutor… rcore-os.github. .

This tutorial is designed to show step-by-step how to write a UNIx-like kernel based on the RISC-V architecture in Rust from scratch. It is worth noting that this project supports not only emulator environments (Qemu/ Terminus, etc.) but also Kendryte K210, a real hardware platform.

Nearly 200,000 words have been published so far, and each chapter is a fully functioning kernel. The first four chapters have been completed to enable the kernel to print characters in bare metal, support system calls and privilege switching, task switching, and virtual storage, respectively. Support for processes, interprocess communication, and data persistence will follow, in turn. The code has been written and the tutorial documentation needs to be updated. Chen and Wu also hope to attract more readers who are interested in Rust and OS through this tutorial. They can build their own tutorials from scratch or expand on them, as well as provide feedback to improve the quality of the tutorials.

At the current pace of the tutorial, this is the perfect time to learn how to write an operating system from scratch.

Why learn operating systems?

Most of a programmer’s work is done on the operating system. Learning operating system, in-depth understanding of operating system principle, is every qualified programmer must experience.

The difficulty many people have learning Rust is basically a result of poor operating system fundamentals.

By implementing an operating system yourself, you can take your understanding of the operating system beyond the conceptual. And implementing an operating system in Rust is even more fun for Rust enthusiasts.

I hope friends in the community can realize their own operating system according to this tutorial. If you need to communicate, you can contact me (Zhang Handong). We can establish a learning group together and feedback the problems in learning to Professor Chen Yu and Wu Yifan directly.

The following is an excerpt from Chapter 0 of the rCore OS tutorial.

Most common operating system kernels are based on C. Why is Rust recommended?

Yes, C was created to write UNIX. Dennis Ritchie and KenThompson did not expect a new language to help develop complex application business logic efficiently and concisely, but rather to abstract the behavior of a computer in a concise way that would make it easier to write an operating system that controlled the computer hardware. The result was C.

C language pointer angel and devil, and C language lacks effective concurrency support, resulting in memory and concurrency vulnerabilities become the nightmare of the current operating system.

The Rust language has the same hardware control capabilities as C and greatly enhances secure programming. From a certain perspective, the core goal of Rust language is to solve the shortcomings of C and replace C. So writing an OS in Rust is a great experience to develop and run.

The price of writing an OS in Rust is simply learning to program in Rust.

The current common CPUS are x86 and ARM, why recommend RISC-V?

Yes, the most common cpus are x86 and ARM, which are widely used in servers, desktops, mobile terminals and many embedded systems. They need to support so many software systems and application requirements that they become increasingly complex.

X86’s policy of compatibility with the past ensured its place in the game, but prevented it from discarding many of its more outdated hardware designs, leaving the operating system struggling to adapt to those hardware features.

X86 and ARM are both successful, mainly commercially, because their widespread use makes their CPU hardware logic increasingly complex, not open enough to change, and not open source, making it difficult for OS developers to learn.

From a certain perspective, the core goal of the emerging RISC-V is to flexibly adapt to future AIoT scenarios, ensuring basic functionality and providing configurable extensions. Its open source features make it easy for students to design a RISC-V CPU.

The trade-off for writing RISC-V-oriented OS is simply that you know the RISC-V Supevisor privileged mode and the OS’s control over the Supevisor privileged mode.

Why did Tsinghua university write this book on operating systems?

There are a series of excellent Operating system textbooks at home and abroad, such as Operating Systems Internals and Design Principles by William Stallings. Avi Silberschatz, Peter Baer Galvin and Greg Gagne’s Operating System Concepts, “Operating Systems: Three Easy Pieces” by Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau. However, from our teaching practice since 2000, some classic textbooks attach great importance to the concept and principle of operating system, but there are still some problems to be improved as follows:

Disconnection between principle and practice: The lack of a bridge between operating system concepts/principles and operating system design/implementation leads students to find a large gap between operating system implementation-related experiments and operating system concepts.

Lack of historical development context: the concept and principle of operating system is from the actual operating system design and implementation process, from scratch gradually evolved and produced, has its historical origin and law of development. However, most of the current textbooks only mention the concept and principle of the current mainstream operating system, with the feeling of “appearing out of thin air”, students do not know the cause and effect of the emergence of these contents.

Ignore hardware details or use complex hardware: Many textbooks ignore or abstract hardware details, making operating system concepts difficult to implement. Some textbooks regard x86 as the hardware reference platform of the operating system experiment, and lack the experimental support of risC-V and other architectures, which are rapidly developing at present. As a result, students may need to spend a lot of money to understand the relatively complicated details of x86 hardware in the operating system experiment, which affects the effect of the operating system experiment.

These problems increase the difficulty for students to learn and master the operating system. We want to try to solve the above three problems, to relieve the pressure of learning the operating system, promote their interest, so that they can better master the operating system within a semester. In order to deal with the problem of “disconnection between principle and practice”, we emphasize the idea of practice first and practice leading principle. MIT professor Frans Kaashoek and other teachers and students designed and implemented the XV6 teaching operating system based on UNIX V6 for the annual undergraduate operating system class experiment, and in the course explanation of the principle and experiment together, has been widely recognized in the world. All these give us a good inspiration. After more than ten years of practice, it is challenging but feasible for an undergraduate majoring in computer to design and implement an operating system (including CPU). The premise is that the actual operating system should be small and can reflect the core ideas of the operating system. In this way, students can deepen their understanding of the operating system principles and concepts, and make the operating system principles and concepts fall into place.

In response to the lack of historical development vein, we redesigned the operating system experiment and teaching content, according to the historical development of the operating system to establish multiple relatively independent small experiments, each experiment reflect the history of the operating system of a miniature, and to sum up the operating system, related concepts and principles And guide students to understand how these concepts and principles evolve step by step.

To deal with the problem of “ignoring hardware details or using complex hardware”, we have experimented for years with hardware (x86, ARM, MIPS, RISC-V, etc.) and programming language (C, C++, Go, Rust, etc.) choices. In 2017, RISC-V CPU was introduced as the hardware environment for operating system experiments, and Rust programming language was introduced as the programming language for operating system development in 2018, enabling students to write an operating system running on RISC-V in Rust language with relatively small development and debugging costs. And convenient and simplify the concept and principle of the operating system visualization, visualization process. Students can directly correspond the concept and principle of the operating system to the program code, hardware specifications and the actual implementation of the operating system, strengthen the practical experience and feeling of the connotation of the operating system.

Therefore, the goal of this book is based on the simple RISC-V CPU as the underlying hardware, according to the needs of the upper application from small to large, according to the history of OS development, gradually explain how to design and implement a “small to large” multiple “small” operating systems to meet these needs. And in the process of designing and implementing the operating system, gradually analyze the knowledge points of various concepts and principles of the operating system, corresponding to do have “principle” to follow and have “code” can be checked, and finally let readers through active operating system design and implementation to deeply grasp the concept and principle of the operating system.

In the writing process, Chapter 0 is an overview of the operating system, so that readers can have a general understanding of the history, definition, characteristics and other concepts of the operating system. Each chapter reflects a miniature historical development process of operating system, that is, from the perspective of supporting applications from simple to complex, each chapter will explain how to design an operating system that can run applications and meet the phased requirements of applications. Thus, readers can learn how to form a relatively perfect “small” operating system similar to UNIX from a trivial “small” operating system to add or enhance operating system functions according to application requirements through the corresponding operating system design experiments. Each step is small enough to feel manageable, and at the end of each step you have a “small” operating system that works. In addition, with a sufficiently detailed test procedure, you can always verify that the operating system implemented by the reader works properly after each update. As the code size of experiment and implementation complexity in a gradually increasing manageable, readers can combine corresponding to the operating system, the principle of design experiments to further explain to establish operating system concept, principle and actual implementation of the relation between so that they can through the practice of the operating system experiment process to enhance the understanding of concepts, Through the theoretical concept to further guide the realization and improvement of operating system experiment.

Before you start reading and practicing the contents of this book, you need to decide what programming language to use for operating system experiments. You can implement the operating system in any programming language you like and on any CPU you like. Our recommended programming language is Rust, and our recommended CPU is RISC-V.