Skip to main content
← PSoXide SDK guides and examples · SDK f2fec1c301f1 · All Cargo features enabled; check feature gates before use.

declare_stack_safe_handler

Function declare_stack_safe_handler 

Source
pub unsafe fn declare_stack_safe_handler(handler: unsafe extern "C" fn())
Expand description

Declare that the game’s own exception handler at handler is safe to take an interrupt on any stack, including a ScratchpadStack.

With the scratchpad-stack-check feature, a stack switch traps when interrupts are on and the general exception vector jumps anywhere but psx-rt’s handler, because a handler that pushes onto the interrupted stack would write below the scratchpad stack’s frames. A game that installs its own vector (hk-psx wraps psx-rt’s handler to service CD interrupts, then jumps to it) calls this once with the address it puts in the vector; the check then accepts exactly that vector word, and still traps any other handler, such as a BIOS vector restored behind the game’s back. Declaring does not install anything.

unsafe extern "C" fn game_exception_wrapper() {}
// SAFETY: game_exception_wrapper switches to its own stack before it
// stores anything and restores $sp before it hands back.
unsafe { psx_rt::interrupts::declare_stack_safe_handler(game_exception_wrapper) };

§Safety

handler, and everything it calls or jumps to, must leave $sp and the memory below it alone: it uses only $k0/$k1 or saves state to memory of its own and runs on a stack of its own, and it returns (or chains to psx-rt’s handler) with $sp unchanged.