Rust in Action Notes Chapter 11 and Chapter 12 Simple Operating System Kernel

This chapter explains how to write a simple operating system (FledgeOS) with Rust. In addition to the knowledge of Rust, you can understand the main components of the operating system and deepen your understanding of the operating system.

  1. First of all, you need to master and understand some tools or technologies. QEMU is a virtualization technology used to build a virtual machine to let your program run on it; the library bootimagecan help reduce the development of the underlying code;
  2. At the beginning, you need to install several libraries, so that cargo-binutilscargo can directly operate the bootimageexecutable file, so that cargo can directly build a boot image, which can be initialized directly on the hardware; rustup toolchain install nightly & rustup default nightlyuse some unstable features and functions of rust; rustup component add rust-srcdownload the source code of the rust language, so that rust can compile a compiler for the new operating system; rustup component add llvm-tools-previewinstall the extension of the LLVM compiler, which is a part of the rust compiler;
  3. After using the corresponding code in the book to compile successfully, you can see a QEMU black box, indicating successful compilation. During this process, the library has helped to do a bootimagelot of things, a. Create a series of machine-readable definitions and terms for the target operating system, such as the target CUP architecture; b. Compile the Rust core code to run on the target OS; c. Use the newly compiled Rust language to compile the OS kernel; d. The boot program is executed in the boot program, and the boot program runs the OS kernel;
  4. It appears in the project structure .cargo/config.toml, which provides additional configuration parameters. The function here is to let the compiler compile std::corethe part itself, instead of relying on the pre-installed version; the one cargo.tomlthat appears in it run-command = ["qemu-system-x86_64", "-drive", "format=raw,file={}"]means cargo runto run a QEMU session when
  5. 代码列表11.4里出现了很多新的属性(attributes),Page373页有详细介绍;主程序的代码永不返回,设置了返回值是!;当程序挂掉的时候相当于整个电脑崩溃了,这里用到的是LLVM的abort()方法;在新的OS里为了避免动态分配内存选择了禁用标准库#![no_std];Rust的编译器有一部分是LLVM提供的,叫做intrinsic functions,为了使用这部分功能需要在开头加上#![core_intrinsics]属性,同时由于LLVM跟Rust的非耦合关系,其提供的API可能会变化,所以要显式地引入Rust nightly的编译器;Rust在引入外部crate的时候为了避免有些变量的名称冲突会引入name_mangling,但在这里需要禁用这个功能,通过#![no_mangle]属性;引入外部C代码要加上关键字extern "C";禁用main函数,使用#![no_main]属性,因为main函数的参数是由编译器_start()提供,所以这里不能有main函数;
  6. In VGA compatible mode, the logic of writing data to the screen is that the screen is divided into an 80x25 grid in the hardware, each grid is represented by 2 bytes, including background color, character color, etc., see Page375 for details, this grid is called, and the buffer size is known to be 4000 bytes through calculation. This project sets 0xb8000 as the starting position of the buffer to locate the position of the buffer to be written, and write data to and from the frame buffercorresponding .offset(1)byte .write_volatie(0x30);
  7. How to start FledgeOS is not through the main() function, but requires software that can directly communicate with the CPU. This software is BootLoader, and the linker links a symbol (_start) to the initial code (entry point), and the CPU reads the instruction of the initial code to start executing;
  8. x86_64The library can directly write instructions to communicate with the CPU, such as hlt()allowing the CPU to sleep, reducing CPU usage and power;
  9. #![feature(lang_items)]The comment indicates that the current file provides language items. Language items are libraries implemented by Rust other than the compiler. Because it was marked at the beginning that the #[no_std]Rust standard library cannot be used, some necessary libraries and functions must be implemented for the new OS;
  10. The follow-up OS mainly talks about how to handle input and output and panic handling in the QEMU virtual environment, which is no different from other OS development. For details, please refer to the corresponding chapters;
  11. Distinguish signals (signals) and interrupts (interrupts). Signals are an abstraction at the software level, which is related to the OS; interrupts are an abstraction at the hardware level, mainly related to the CPU; see the figure belowFigure 12.1
  12. The interrupt will force the control flow of the application to change. The signal (signal) can be ignored (ignored) in some cases. Once the interrupt is received, the CPU will jump to the corresponding interrupt handling code. The interrupt handling code is pre-defined by the BIOS and OS in the initialization process (bootup process);
  13. The Rust implementation of soft interrupts can bring attributes through std::asmthe library , and macros can insert interrupts. The most common hardware interrupt is the keyboard interrupt. The principle is shown in Figure 12.3 below.#![feature(asm)asm!("int 42")fig12.3

Guess you like

Origin blog.csdn.net/Mint2yx4/article/details/131538509