tg-rcore-tutorial-syscall
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等
使用示例
Kernel side (with kernel feature)
use ;
init_io;
init_process;
init_scheduling;
let result = handle;
let _ = result;
User side (with user feature)
use ;
write;
exit;
- 章节内真实用法:
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-ch1~tg-rcore-tutorial-ch8 的关系
- 直接依赖章节:
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
License
Licensed under either of MIT license or Apache License, Version 2.0 at your option.