Exceptions

AArch64 defines an exception vector table with 16 entries, for 4 types of exceptions (synchronous, IRQ, FIQ, SError) from 4 states (current EL with SP0, current EL with SPx, lower EL using AArch64, lower EL using AArch32). We implement this in assembly to save volatile registers to the stack before calling into Rust code:

  1. use log::error;
  2. use smccc::psci::system_off;
  3. use smccc::Hvc;
  4. // SAFETY: There is no other global function of this name.
  5. #[unsafe(no_mangle)]
  6. extern "C" fn sync_exception_current(_elr: u64, _spsr: u64) {
  7.     error!("sync_exception_current");
  8.     system_off::<Hvc>().unwrap();
  9. }
  10. // SAFETY: There is no other global function of this name.
  11. #[unsafe(no_mangle)]
  12. extern "C" fn irq_current(_elr: u64, _spsr: u64) {
  13.     error!("irq_current");
  14.     system_off::<Hvc>().unwrap();
  15. }
  16. // SAFETY: There is no other global function of this name.
  17. #[unsafe(no_mangle)]
  18. extern "C" fn fiq_current(_elr: u64, _spsr: u64) {
  19.     error!("fiq_current");
  20.     system_off::<Hvc>().unwrap();
  21. }
  22. // SAFETY: There is no other global function of this name.
  23. #[unsafe(no_mangle)]
  24. extern "C" fn serr_current(_elr: u64, _spsr: u64) {
  25.     error!("serr_current");
  26.     system_off::<Hvc>().unwrap();
  27. }
  28. // SAFETY: There is no other global function of this name.
  29. #[unsafe(no_mangle)]
  30. extern "C" fn sync_lower(_elr: u64, _spsr: u64) {
  31.     error!("sync_lower");
  32.     system_off::<Hvc>().unwrap();
  33. }
  34. // SAFETY: There is no other global function of this name.
  35. #[unsafe(no_mangle)]
  36. extern "C" fn irq_lower(_elr: u64, _spsr: u64) {
  37.     error!("irq_lower");
  38.     system_off::<Hvc>().unwrap();
  39. }
  40. // SAFETY: There is no other global function of this name.
  41. #[unsafe(no_mangle)]
  42. extern "C" fn fiq_lower(_elr: u64, _spsr: u64) {
  43.     error!("fiq_lower");
  44.     system_off::<Hvc>().unwrap();
  45. }
  46. // SAFETY: There is no other global function of this name.
  47. #[unsafe(no_mangle)]
  48. extern "C" fn serr_lower(_elr: u64, _spsr: u64) {
  49.     error!("serr_lower");
  50.     system_off::<Hvc>().unwrap();
  51. }
  • EL is exception level; all our examples this afternoon run in EL1.
  • For simplicity we aren’t distinguishing between SP0 and SPx for the current EL exceptions, or between AArch32 and AArch64 for the lower EL exceptions.
  • For this example we just log the exception and power down, as we don’t expect any of them to actually happen.
  • We can think of exception handlers and our main execution context more or less like different threads. Send and Sync will control what we can share between them, just like with threads. For example, if we want to share some value between exception handlers and the rest of the program, and it’s Send but not Sync, then we’ll need to wrap it in something like a Mutex and put it in a static.