os/arch/riscv/kernel/
context.rs

1//! RISC-V 架构的上下文切换相关功能
2
3/// 在发生调度时保存的上下文信息
4/// 相较于TrapFrame只保存切换所需的最少量寄存器
5#[repr(C)]
6#[derive(Debug, Clone, Copy)]
7pub struct Context {
8    /// 返回地址
9    pub ra: usize,
10    /// 栈指针
11    pub sp: usize,
12    /// 保存s0-s11寄存器
13    pub s: [usize; 12],
14}
15
16impl Context {
17    /// 创建一个全零初始化的上下文
18    pub fn zero_init() -> Self {
19        Context {
20            ra: 0,
21            sp: 0,
22            s: [0; 12],
23        }
24    }
25
26    /// 设置线程的初始上下文
27    /// 参数:
28    /// * `entry`: 线程入口地址
29    /// * `kstack_top`: 内核栈顶地址
30    pub fn set_init_context(&mut self, entry: usize, kstack_top: usize) {
31        self.sp = kstack_top;
32        self.ra = entry;
33    }
34}