veridian_kernel/arch/
context.rs1use crate::sched::task::TaskContext;
7
8pub trait ThreadContext: Sized {
10 fn new() -> Self;
12
13 fn init(&mut self, entry_point: usize, stack_pointer: usize, kernel_stack: usize);
15
16 fn get_instruction_pointer(&self) -> usize;
18
19 fn set_instruction_pointer(&mut self, ip: usize);
21
22 fn get_stack_pointer(&self) -> usize;
24
25 fn set_stack_pointer(&mut self, sp: usize);
27
28 fn get_kernel_stack(&self) -> usize;
30
31 fn set_kernel_stack(&mut self, sp: usize);
33
34 fn set_return_value(&mut self, value: usize);
36
37 fn set_tls_base(&mut self, base: u64);
39
40 fn tls_base(&self) -> u64;
42
43 fn clone_from(&mut self, other: &Self);
45
46 fn to_task_context(&self) -> TaskContext;
48}
49
50#[cfg(target_arch = "x86_64")]
52pub type ArchThreadContext = crate::arch::x86_64::context::X86_64Context;
53
54#[cfg(target_arch = "aarch64")]
55pub type ArchThreadContext = crate::arch::aarch64::context::AArch64Context;
56
57#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
58pub type ArchThreadContext = crate::arch::riscv::context::RiscVContext;
59
60pub unsafe fn switch_context(from: &mut ArchThreadContext, to: &ArchThreadContext) {
66 #[cfg(target_arch = "x86_64")]
67 crate::arch::x86_64::context::switch_context(from, to);
68
69 #[cfg(target_arch = "aarch64")]
70 crate::arch::aarch64::context::switch_context(from, to);
71
72 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
73 crate::arch::riscv::context::switch_context(from, to);
74}
75
76pub fn init_fpu() {
78 #[cfg(target_arch = "x86_64")]
79 crate::arch::x86_64::context::init_fpu();
80
81 #[cfg(target_arch = "aarch64")]
82 crate::arch::aarch64::context::init_fpu();
83
84 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
85 crate::arch::riscv::context::init_fpu();
86}
87
88pub fn save_fpu_state(state: &mut [u8]) {
90 #[cfg(target_arch = "x86_64")]
91 unsafe {
95 crate::arch::x86_64::context::save_fpu_state(
96 &mut *(state.as_mut_ptr() as *mut crate::arch::x86_64::context::FpuState),
97 );
98 }
99
100 #[cfg(target_arch = "aarch64")]
101 {
102 let fpu =
105 unsafe { &mut *(state.as_mut_ptr() as *mut crate::arch::aarch64::context::FpuState) };
106 crate::arch::aarch64::context::save_fpu_state(fpu);
107 }
108
109 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
110 {
111 let fpu =
114 unsafe { &mut *(state.as_mut_ptr() as *mut crate::arch::riscv::context::FpuState) };
115 crate::arch::riscv::context::save_fpu_state(fpu);
116 }
117}
118
119pub fn restore_fpu_state(state: &[u8]) {
121 #[cfg(target_arch = "x86_64")]
122 unsafe {
126 crate::arch::x86_64::context::restore_fpu_state(
127 &*(state.as_ptr() as *const crate::arch::x86_64::context::FpuState),
128 );
129 }
130
131 #[cfg(target_arch = "aarch64")]
132 {
133 let fpu = unsafe { &*(state.as_ptr() as *const crate::arch::aarch64::context::FpuState) };
135 crate::arch::aarch64::context::restore_fpu_state(fpu);
136 }
137
138 #[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
139 {
140 let fpu = unsafe { &*(state.as_ptr() as *const crate::arch::riscv::context::FpuState) };
142 crate::arch::riscv::context::restore_fpu_state(fpu);
143 }
144}