stateDiagram-v2
R: running
S: sleeping
D: disk sleep
T: stopped
t: tracing stop
X: dead
Z: zombie
P: parked
I: idle
R --> S: schedule_timeout()
R --> D: Wait for Disk I/O
R --> T: SIGTSTP
R --> t: gdb/strace
S --> R: wake_up_process()
D --> R: I/O Completed
T --> R: SIGCONT
T --> t: gdb/strace
T --> Z: SIGKILL But Sth Wrong with Its Parent
R --> Z: Exit But Sth Wrong with Its Parent
t --> T: Quit gdb
#[panic_handler] fnpanic(_: &PanicInfo) -> ! { loop {} // Loop forever to halt the program }
#[no_mangle] #[cfg(target_arch = "x86_64")] pubunsafeextern"C"fn_start() { letmessage = b"Hello, world!\n"; letfd: usize = 1; // File descriptor for stdout letsyscall_no: usize = 1; // Syscall number for write in Linux x86_64 // Use inline asm macro to perform the syscall core::arch::asm!( "syscall", in("rax") syscall_no, // syscall number in("rdi") fd, // file descriptor in("rsi") message.as_ptr(), // pointer to the message in("rdx") message.len(), // length of the message out("rcx") _, out("r11") _, // telling compiler these registers will // be clobbered by syscalls );
// Exit the program with status code 0 core::arch::asm!( "syscall", in("rax") 60, // syscall number for exit in("rdi") 0, // exit status code out("rcx") _, out("r11") _, // telling compiler these registers will // be clobbered by syscalls ); }
Rust for Linux
Rust 是 2022 年 10 月随 Linux 6.1-rc1 进入内核主线的。Linux 内核从此就变成了一个双语言项目,首先要解决的问题之一就是 C 和 Rust 函数互相调用的问题,这就需要一个自动生成 Rust FFI bindings to C 的工具 bindgen, 所以 bindgen 也是构建内核中 Rust 代码的依赖之一。