OS

Rust

"RustOS 第一章"

Posted by HZY on March 31, 2026

对于学习到的一些基础知识,这里不再介绍,着重强调一下用户态这个东西,也就是说U,S,M三种模式

no_std

在linux上面开启一个编译risvc的rust程序也很简单。

  • in your project, find the .cargo and go into the file.cargo/config.toml
    • add the following content:
    1
    2
    
      [build]
      target = "riscv64gc-unknown-none-elf"
    
  • now, go into your main.rs, you will find that there is a error
    • run following commands
    1
    
      rustup target add riscv64gc-unknown-none-elf
    
    • then, to fix the error use no_std
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    
      // src/main.rs
      #![no_std]
      #![no_main]
    
      mod lang_items;
        
      fn main(){
    
      }
    
      //src/lang_items.rs
      use core::panic::PanicInfo;
    
      #[panic_handler]
      fn panic(_info: &PanicInfo) -> ! {
          loop {}
      }
      // without this, your project won't go on
      // because you give up std lib
      // in no_std panic_handler is empty
      // which means you must implement it by yourself
      // then you could biuld your project
    
  • debug your project

    • install tools
    1
    2
    
      cargo install cargo-binutils
      rustup component add llvm-tools-preview
    
    • DEBUG
    1
    2
    3
    4
    5
    
      file target/riscv64gc-unknown-none-elf/debug/hello_os
    
      rust-readobj -h target/riscv64gc-unknown-none-elf/debug/hello_os
    
      rust-objdump -S target/riscv64gc-unknown-none-elf/debug/hello_os
    

learn qemu

1
2
3
4
5
6
7
qemu-system-riscv64 \
    -machine virt \
    -nographic \
    -bios ../bootloader/rustsbi-qemu.bin \
    -device loader,file=target/riscv64gc-unknown-none-elf/release/os.bin,addr=0x80200000