pudding_kernel/
context.rs1use core::ptr;
2
3#[cfg(target_arch = "x86_64")]
4pub mod x86_64;
5#[cfg(target_arch = "x86_64")]
6pub use self::x86_64::*;
7
8#[cfg(target_arch = "arm")]
9pub mod arm;
10#[cfg(target_arch = "arm")]
11pub use self::arm::*;
12
13#[cfg(target_arch = "aarch64")]
14pub mod aarch64;
15#[cfg(target_arch = "aarch64")]
16pub use self::aarch64::*;
17
18#[cfg(not(any(target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64")))]
19pub mod dummy;
20#[cfg(not(any(target_arch = "x86_64", target_arch = "arm", target_arch = "aarch64")))]
21pub use self::dummy::*;
22
23static mut SYSTEM_CONTEXT: Context = Context::new();
24static mut CURRENT_CONTEXT: *mut Context = ptr::null_mut();
25
26pub(crate) unsafe fn context_switch_to_system() {
27 SYSTEM_CONTEXT.switch();
28}
29
30pub(crate) fn context_initialize() {
31 unsafe {
32 CURRENT_CONTEXT = &mut SYSTEM_CONTEXT as *mut Context;
33 }
34}
35
36impl Context {
37 pub(crate) fn create(&mut self, stack: &mut [u8], entry: extern "C" fn(isize), exinf: isize) {
38 unsafe {
39 self._create(stack, entry, exinf);
40 }
41 }
42
43 pub(crate) fn switch(&mut self) {
44 unsafe {
45 let cur_ctx = CURRENT_CONTEXT;
46 CURRENT_CONTEXT = self as *mut Context;
47 self._switch(&mut *cur_ctx);
48 }
49 }
50
51 pub fn is_current(&self) -> bool {
52 unsafe {
53 let ptr0 = CURRENT_CONTEXT as *const Context;
54 let ptr1 = self as *const Context;
55 ptr0 == ptr1
56 }
57 }
58}