This begins a series of commits to cleanup a static_mut warning

So in Rust 1.83, we now get a strongly worded warning telling
us to really, really don't use static muts. They aren't wrong,
except that in some places like the kernel we have few options:
we are conjuring pointers out of thin air, creating memory mappings,
and then doing things with them that are hard for the compiler
to reason about.

I think the newly minted warning was made because the intention
is that we could use the new fancy `const` semantics to take
the place of static mut usage by wrapping things in UnsafeCell,
and I think maaaaybe in some
cases this could apply here, but as a simple example a dive
into the kernel debug UART indicates that to initialize the
shared variable we have to allocate pages in the page table,
which involves calling the memory manager and doing some
decidedly non-const type of things. So one option is we could
just push the debug UART initialization into the loader and
paper over the whole thing by pretending it's static.

Instead of that, we adopt a pattern of casting all our static
muts to a raw pointer, and then back into what we want. The
cast to raw pointer is like an "uber unsafe" marker that says
"no we really mean that this pointer is where it is and we
conjured it out of thin air and we promise that the behavior
is defined and values are representable, etc.".

In general we're fairly judicious about the use of static
mut in the kernel, and when in the kernel there's no thread
pre-emption (although we could take an interrupt or page
fault) so generally we are single-threaded. The trickiest
spots are when we have to take a page fault while handling
a page fault (for example, allocating a new L1 page table
entry would require something like this) but this code path
is fairly carefully vetted.

So instead of just doing a global lint silence, we use an
`&raw` idiom to call out the fact that we're doing something
naughty wherever we do it, which makes the compiler "happy"
(for now) while still making the code ugly enough that it
looks sufficiently scary and you have to apply some
cognitive burden to even reason about what's going on,
which I *think* is the intention of Rust in this case.

Basically you get a get out of jail free card but you can't
simply wear it on a lanyard and pass through the gate -- every
time we need to use it, we have to do a little dance and
wash ourselves in the waters of unsafety and recite the
mantras about defined behavior, representable numbers, thread
safety, etc.
This commit is contained in:
bunnie 2024-12-02 18:55:18 +08:00
parent 2ace34abee
commit 95611d9598

View file

@ -92,7 +92,7 @@ static mut PREVIOUS_PAIR: Option<(PID, TID)> = None;
pub unsafe fn set_isr_return_pair(pid: PID, tid: TID) { PREVIOUS_PAIR = Some((pid, tid)); }
#[cfg(feature = "gdb-stub")]
pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> { PREVIOUS_PAIR.take() }
pub unsafe fn take_isr_return_pair() -> Option<(PID, TID)> { (&mut *(&raw mut PREVIOUS_PAIR)).take() }
/// Finish a pending ISR. Return `false` if there was none.
fn finish_isr() -> bool {
@ -104,7 +104,7 @@ fn finish_isr() -> bool {
// we're in an interrupt context, it is safe to access this
// global variable.
let (previous_pid, previous_context) =
unsafe { PREVIOUS_PAIR.take().expect("got RETURN_FROM_ISR with no previous PID") };
unsafe { (&mut *(&raw mut PREVIOUS_PAIR)).take().expect("got RETURN_FROM_ISR with no previous PID") };
// println!(
// "ISR: Resuming previous pair of ({}, {})",
// previous_pid, previous_context
@ -227,8 +227,9 @@ pub extern "C" fn trap_handler(
})
});
let response = crate::syscall::handle(pid, tid, unsafe { PREVIOUS_PAIR.is_some() }, call)
.unwrap_or_else(xous_kernel::Result::Error);
let response =
crate::syscall::handle(pid, tid, unsafe { (&mut *(&raw mut PREVIOUS_PAIR)).is_some() }, call)
.unwrap_or_else(xous_kernel::Result::Error);
// println!("Syscall Result: {:?}", response);
ArchProcess::with_current_mut(|p| {
@ -251,7 +252,7 @@ pub extern "C" fn trap_handler(
// Safe to access globals since interrupts are disabled
// when this function runs.
unsafe {
if PREVIOUS_PAIR.is_none() {
if (&mut *(&raw mut PREVIOUS_PAIR)).is_none() {
let tid = crate::arch::process::current_tid();
// This is pretty verbose, so leave it commented out unless we're debugging a process
// transition
@ -259,7 +260,7 @@ pub extern "C" fn trap_handler(
// if pid.get() != 1 {
// println!("Hardware IRQ set PID{:?}, TID{:?}", pid, tid);
// }
PREVIOUS_PAIR = Some((pid, tid));
*(&mut *(&raw mut PREVIOUS_PAIR)) = Some((pid, tid));
}
}
HANDLING_IRQ.store(true, Ordering::Relaxed);