-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathuspace.rs
More file actions
110 lines (93 loc) · 3.24 KB
/
Copy pathuspace.rs
File metadata and controls
110 lines (93 loc) · 3.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! Structures and functions for user space.
use memory_addr::VirtAddr;
#[cfg(feature = "fp-simd")]
use riscv::register::sstatus::FS;
use riscv::register::sstatus::Sstatus;
use crate::{GeneralRegisters, TrapFrame};
/// Context to enter user space.
pub struct UspaceContext(TrapFrame);
impl UspaceContext {
/// Creates an empty context with all registers set to zero.
pub const fn empty() -> Self {
unsafe { core::mem::MaybeUninit::zeroed().assume_init() }
}
/// Creates a new context with the given entry point, user stack pointer,
/// and the argument.
pub fn new(entry: usize, ustack_top: VirtAddr, arg0: usize) -> Self {
let mut sstatus = Sstatus::from_bits(0);
sstatus.set_spie(true); // enable interrupts
sstatus.set_sum(true); // enable user memory access in supervisor mode
#[cfg(feature = "fp-simd")]
sstatus.set_fs(FS::Initial); // set the FPU to initial state
Self(TrapFrame {
regs: GeneralRegisters {
a0: arg0,
sp: ustack_top.as_usize(),
..Default::default()
},
sepc: entry,
sstatus,
})
}
/// Creates a new context from the given [`TrapFrame`].
pub const fn from(trap_frame: &TrapFrame) -> Self {
Self(*trap_frame)
}
/// Gets the instruction pointer.
pub const fn get_ip(&self) -> usize {
self.0.sepc
}
/// Gets the stack pointer.
pub const fn get_sp(&self) -> usize {
self.0.regs.sp
}
/// Sets the instruction pointer.
pub const fn set_ip(&mut self, pc: usize) {
self.0.sepc = pc;
}
/// Sets the stack pointer.
pub const fn set_sp(&mut self, sp: usize) {
self.0.regs.sp = sp;
}
/// Sets the return value register.
pub const fn set_retval(&mut self, a0: usize) {
self.0.regs.a0 = a0;
}
/// Enters user space.
///
/// It restores the user registers and jumps to the user entry point
/// (saved in `sepc`).
/// When an exception or syscall occurs, the kernel stack pointer is
/// switched to `kstack_top`.
///
/// # Safety
///
/// This function is unsafe because it changes processor mode and the stack.
pub unsafe fn enter_uspace(&self, kstack_top: VirtAddr) -> ! {
use riscv::register::{sepc, sscratch};
crate::asm::disable_irqs();
// Address of the top of the kernel stack after saving the trap frame.
let kernel_trap_addr = kstack_top.as_usize() - core::mem::size_of::<TrapFrame>();
unsafe {
sscratch::write(kstack_top.as_usize());
sepc::write(self.0.sepc);
core::arch::asm!(
include_asm_macros!(),
"
mv sp, {tf}
STR gp, {kernel_trap_addr}, 3
LDR gp, sp, 3
STR tp, {kernel_trap_addr}, 4
LDR tp, sp, 4
LDR t0, sp, 33
csrw sstatus, t0
POP_GENERAL_REGS
LDR sp, sp, 2
sret",
tf = in(reg) &(self.0),
kernel_trap_addr = in(reg) kernel_trap_addr,
options(noreturn),
)
}
}
}