基础命令

rustup

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup update

vi .cargo/config

# 清华源
[source.crates-io]
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

cargo

rustup install nightly
rustup default nightly

rustup component add llvm-tools-preview
rustup component add rust-src

cargo install cargo-binutils
cargo install bootimage
cargo install cargo-xbuild

rustc --print target-list
rustup target add thumbv7em-none-eabihf
cargo new he
cargo build
cargo run

alpine

apk add build-base

ubuntu

Writing an OS in Rust

  • 禁用标准库
  • 实现 panic 处理函数
  • eh_personality 语言项 - 禁用栈展开
  • start 语言项

vi src/main.rs

#![no_std]
#![no_main]

#[no_mangle]
pub extern "C" fn _start() -> ! {
   loop{}
}

use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
    loop{}
}

vi Cargo.toml

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

vi .cargo/config

[build]
target = "thumbv7em-none-eabihf"

其他参考

Writing an OS in Rust

  • 打印字符串
  • 目标配置清单
  • 编译内核
  • 创建引导映像
  • qemu启动内核

vi src/main.rs

#[no_mangle]
pub extern "C" fn _start() -> ! {
    let vga = 0xb8000 as *mut u8;
    let b: &[u8] = b"Hello World";
    for (i, &byte) in b.iter().enumerate() {
        unsafe {
            *vga.offset(i as isize * 2) = byte;
            *vga.offset(i as isize * 2 + 1) = 0xb;
        }
    }
    loop {}
}

vi x86_64.json

{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": "64",
    "target-c-int-width": "32",
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float"
}

vi Cargo.toml

[package.metadata.bootimage]
build-command = ["xbuild"]

[dependencies]
bootloader = "0.9.22"

vi .cargo/config

[build]
target = "x86_64.json"

[target.'cfg(target_os = "none")']
runner = "bootimage runner"

qemu

cargo xbuild
cargo bootimage
cargo xrun
qemu-system-x86_64.exe -drive format=raw,file=target\x86_64\debug\bootimage-he.bin

Writing an OS in Rust

  • VGA字符模式
  • 格式化宏
  • println! 宏

VGA text mode

0xb8000

vi vga.rs

use core::fmt::{ Write, Result, Arguments };
static mut CURSOR: usize = 0;
struct TextMode {
    w: usize,
    // h: usize,
}
impl Write for TextMode {
    fn write_str(&mut self, s: &str) -> Result {
        let buffer = 0xb8000 as *mut u8;
        for (i, &byte) in s.as_bytes().iter().enumerate() {
            unsafe {
                if b'\n' == byte {
                    CURSOR = CURSOR + self.w - (CURSOR % self.w) - 1;
                } else {
                    let j = (i + CURSOR) as isize;
                    *buffer.offset(j * 2) = byte;
                    *buffer.offset(j * 2 + 1) = 0xf;
                }
            }
        }
        unsafe {
            CURSOR = CURSOR + s.len();
        }
        Ok(())
    }
}

vi vga.rs

#[macro_export]
macro_rules! print {
    ($($arg:tt)*) => ($crate::vga::_print(format_args!($($arg)*)));
}

#[macro_export]
macro_rules! println {
    () => (print!("\n"));
    ($($arg:tt)*) => (print!("{}\n", format_args!($($arg)*)));
}

#[doc(hidden)]
pub fn _print(args: Arguments) {
    let mut writer = TextMode { w: 80 }; //, h: 25
    writer.write_fmt(args).unwrap();
}