raw pointer dance for the SWITCHTO_CALLER

This commit is contained in:
bunnie 2024-12-02 19:06:25 +08:00
parent 8876f6e7f5
commit b59177176b

View file

@ -45,7 +45,7 @@ enum ExecutionType {
} }
#[cfg(baremetal)] #[cfg(baremetal)]
pub fn reset_switchto_caller() { unsafe { SWITCHTO_CALLER = None }; } pub fn reset_switchto_caller() { unsafe { *(&mut *(&raw mut SWITCHTO_CALLER)) = None }; }
fn retry_syscall(pid: PID, tid: TID) -> SysCallResult { fn retry_syscall(pid: PID, tid: TID) -> SysCallResult {
if cfg!(baremetal) { if cfg!(baremetal) {
@ -62,8 +62,9 @@ fn do_yield(_pid: PID, tid: TID) -> SysCallResult {
return Ok(xous_kernel::Result::Ok); return Ok(xous_kernel::Result::Ok);
} }
let (parent_pid, parent_ctx) = let (parent_pid, parent_ctx) = unsafe {
unsafe { SWITCHTO_CALLER.take().expect("yielded when no parent context was present") }; (&mut *(&raw mut SWITCHTO_CALLER)).take().expect("yielded when no parent context was present")
};
//println!("\n\r ***YIELD CALLED***"); //println!("\n\r ***YIELD CALLED***");
SystemServices::with_mut(|ss| { SystemServices::with_mut(|ss| {
// TODO: Advance thread // TODO: Advance thread
@ -190,7 +191,7 @@ fn send_message(pid: PID, tid: TID, cid: CID, message: Message) -> SysCallResult
return if blocking && cfg!(baremetal) { return if blocking && cfg!(baremetal) {
if !runnable { if !runnable {
// If it's not runnable (e.g. it's being debugged), switch to the parent. // If it's not runnable (e.g. it's being debugged), switch to the parent.
let (ppid, ptid) = unsafe { SWITCHTO_CALLER.take().unwrap() }; let (ppid, ptid) = unsafe { (&mut *(&raw mut SWITCHTO_CALLER)).take().unwrap() };
klog!( klog!(
"Activating Server parent process (server is blocked) and switching away from Client" "Activating Server parent process (server is blocked) and switching away from Client"
); );
@ -812,11 +813,11 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
SysCall::SwitchTo(new_pid, new_tid) => SystemServices::with_mut(|ss| { SysCall::SwitchTo(new_pid, new_tid) => SystemServices::with_mut(|ss| {
unsafe { unsafe {
assert!( assert!(
SWITCHTO_CALLER.is_none(), (&mut *(&raw mut SWITCHTO_CALLER)).is_none(),
"SWITCHTO_CALLER was {:?} and not None, indicating SwitchTo was called twice", "SWITCHTO_CALLER was {:?} and not None, indicating SwitchTo was called twice",
SWITCHTO_CALLER, (&mut *(&raw mut SWITCHTO_CALLER)),
); );
SWITCHTO_CALLER = Some((pid, tid)); *(&mut *(&raw mut SWITCHTO_CALLER)) = Some((pid, tid));
} }
// println!( // println!(
// "Activating process thread {} in pid {} coming from pid {} thread {}", // "Activating process thread {} in pid {} coming from pid {} thread {}",
@ -836,7 +837,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
SysCall::Yield => do_yield(pid, tid), SysCall::Yield => do_yield(pid, tid),
SysCall::ReturnToParent(_pid, _cpuid) => { SysCall::ReturnToParent(_pid, _cpuid) => {
unsafe { unsafe {
if let Some((parent_pid, parent_ctx)) = SWITCHTO_CALLER.take() { if let Some((parent_pid, parent_ctx)) = (&mut *(&raw mut SWITCHTO_CALLER)).take() {
crate::arch::irq::set_isr_return_pair(parent_pid, parent_ctx) crate::arch::irq::set_isr_return_pair(parent_pid, parent_ctx)
} }
}; };
@ -847,7 +848,7 @@ pub fn handle_inner(pid: PID, tid: TID, in_irq: bool, call: SysCall) -> SysCallR
SysCall::WaitEvent => SystemServices::with_mut(|ss| { SysCall::WaitEvent => SystemServices::with_mut(|ss| {
let process = ss.get_process(pid).expect("Can't get current process"); let process = ss.get_process(pid).expect("Can't get current process");
let ppid = process.ppid; let ppid = process.ppid;
unsafe { SWITCHTO_CALLER = None }; unsafe { *(&mut *(&raw mut SWITCHTO_CALLER)) = None };
// TODO: Advance thread // TODO: Advance thread
if cfg!(baremetal) { if cfg!(baremetal) {
let result = ss let result = ss