System call definitions and interfaces for the rCore tutorial operating system.
- 统一管理教学内核的系统调用号、参数约定和返回约定。
- 提供内核侧"可插拔 trait 注册 + 分发器"机制。
- 提供用户侧 syscall 封装函数,减少内联汇编重复代码。
- 通用定义:
SyscallId、Caller、SyscallResult、STDOUT/STDIN等常量与类型。
feature = "kernel":- 各能力 trait(
IO、Process、Scheduling、Clock、Memory、Signal、Thread、SyncMutex、Trace)。 init_*注册函数与handle(...)分发函数。
- 各能力 trait(
feature = "user":write/read/exit/fork/...等用户态包装。native::syscall0..syscall6底层调用入口。
- 支持
kernel与user双模式。 - trait 驱动的系统调用实现,便于按章节逐步增量接入。
- 覆盖教学中常用 syscall 族:I/O、进程、调度、时间、信号、线程、同步等。
no_std友好,适配裸机内核与用户程序运行时。
- 内核侧通过
init_*注册实现,再由handle(caller, id, args)统一分发。 - 用户侧封装遵循 RISC-V 调用约定,将参数落在
a0-a5/a7。 - 分发结果以
SyscallResult表达"完成/不支持"等状态,便于上层处理。
- 类型/常量:
SyscallId,Caller,SyscallResultClockId,TimeSpec,Stat,OpenFlagsSTDIN,STDOUT,STDDEBUG
- 内核侧(
kernel):- trait:
IO,Process,Scheduling,Clock,Memory,Signal,Thread,SyncMutex,Trace - 函数:
init_io,init_process,init_scheduling,init_clock,init_memory,init_signal,init_thread,init_sync_mutex,init_trace - 分发:
handle(caller, id, args)
- trait:
- 用户侧(
user):write,read,open,close,exit,fork,exec,wait,getpid,clock_gettime,sched_yield等
use tg_syscall::{Caller, SyscallId, SyscallResult};
tg_syscall::init_io(&my_io_impl);
tg_syscall::init_process(&my_process_impl);
tg_syscall::init_scheduling(&my_sched_impl);
let result = tg_syscall::handle(Caller { entity: 0, flow: 0 }, SyscallId::WRITE, [0; 6]);
let _ = result;use tg_syscall::{write, exit, STDOUT};
write(STDOUT, b"hello\n");
exit(0);- 章节内真实用法:
tg-rcore-tutorial-ch2/src/main.rs注册IO和Process并调用handle。tg-rcore-tutorial-ch3/src/main.rs增加Scheduling、Clock。tg-rcore-tutorial-ch8/src/main.rs增加Thread、SyncMutex。
- 直接依赖章节:
tg-rcore-tutorial-ch2到tg-rcore-tutorial-ch8。 - 关键职责:作为"用户态请求 -> 内核实现"的中间协议层与分发层。
- 关键引用文件:
tg-rcore-tutorial-ch2/src/main.rstg-rcore-tutorial-ch3/src/main.rstg-rcore-tutorial-ch5/src/main.rstg-rcore-tutorial-ch8/src/main.rs
Licensed under either of MIT license or Apache License, Version 2.0 at your option.