add dynamic glitch response via IRQs

non-paranoid mode responses happen through IRQs (recall that
paranoid mode is a hair-trigger hardware reset with no
exceptions). This patch installs the IRQ handlers in boot0
and boot1.

- boot0 in particular now even has an IRQ handler. Previously,
it didn't have the IRQ facility configured, because there was no need
for one.
- boot0 sets up the sensors, and the IRQs
- boot1 merely continues with boot0's settings, but re-installing
the trap handler to be its own and double-checking some
register settings.

An old testing IRQ handler stub for keyboard presses was also removed,
as it can never be triggered as that IRQ is never set up.
This commit is contained in:
bunnie 2025-12-07 01:37:55 +08:00
parent 53c7a729f7
commit c85425ae91
No known key found for this signature in database
4 changed files with 63 additions and 22 deletions

View file

@ -9,10 +9,8 @@ use utralib::utra;
use utralib::utra::sysctrl;
#[cfg(feature = "unsafe-dev")]
use crate::platform::{
debug::setup_rx,
irq::{enable_irq, irq_setup},
};
use crate::platform::debug::setup_rx;
use crate::platform::irq::{enable_irq, irq_setup};
#[global_allocator]
static ALLOCATOR: linked_list_allocator::LockedHeap = linked_list_allocator::LockedHeap::empty();
@ -149,6 +147,31 @@ pub fn early_init() -> Csprng {
let mut csprng = Csprng::new();
csprng.random_delay();
irq_setup();
// set up the reactive sensors
bao1x_api::bollard!(4);
bao1x_hal::hardening::reset_sensors();
bao1x_api::bollard!(4);
bollard!(die, 4);
csprng.random_delay();
// ensure paranoid mode is respected
if paranoid2 != 0 {
bollard!(die, 4);
paranoid_mode();
}
// set up IRQ to respond to all possible sensor errors
let mut irq13 = CSR::new(utra::irqarray13::HW_IRQARRAY13_BASE as *mut u32);
irq13.wo(utra::irqarray13::EV_EDGE_TRIGGERED, 0xFFFF_FFFF);
irq13.wo(utra::irqarray13::EV_POLARITY, 0xFFFF_FFFF);
irq13.wo(utra::irqarray13::EV_PENDING, 0xFFFF_FFFF);
irq13.wo(utra::irqarray13::EV_ENABLE, 0xFFFF_FFFF);
enable_irq(utra::irqarray13::IRQARRAY13_IRQ);
csprng.random_delay();
if paranoid1 != 0 {
bollard!(die, 4);
paranoid_mode();
}
// setup heap alloc
// glitch_safety: failing to initialize this will generally cause heap allocs to hang
setup_alloc();
@ -193,7 +216,6 @@ pub fn early_init() -> Csprng {
#[cfg(feature = "unsafe-dev")]
let mut udma_uart = {
let mut udma_uart = setup_rx(perclk);
irq_setup();
enable_irq(utra::irqarray5::IRQARRAY5_IRQ);
udma_uart
};

View file

@ -1,13 +1,12 @@
#[cfg(feature = "unsafe-dev")]
use riscv::register::mstatus;
use riscv::register::{mcause, mie};
#[cfg(feature = "unsafe-dev")]
use vexriscv::register::vexriscv::mim;
use vexriscv::register::vexriscv::mip;
use crate::*;
#[cfg(feature = "unsafe-dev")]
static ATTACKS_SINCE_BOOT: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);
pub fn irq_setup() {
unsafe {
#[rustfmt::skip]
@ -28,7 +27,6 @@ pub fn irq_setup() {
unsafe { mie::set_mext() };
}
#[cfg(feature = "unsafe-dev")]
pub fn enable_irq(irq_no: usize) {
// Note that the vexriscv "IRQ Mask" register is inverse-logic --
// that is, setting a bit in the "mask" register unmasks (i.e. enables) it.
@ -195,12 +193,17 @@ pub extern "C" fn trap_handler(
} else if (irqs_pending & (1 << utra::irqarray5::IRQARRAY5_IRQ)) != 0 {
#[cfg(feature = "unsafe-dev")]
crate::uart_irq_handler();
} else if (irqs_pending & (1 << utra::irqarray2::IRQARRAY2_IRQ)) != 0 {
// just clear it for now
let mut irqarray2 = CSR::new(utralib::HW_IRQARRAY2_BASE as *mut u32);
let pending = irqarray2.r(utra::irqarray2::EV_PENDING);
crate::println!("keypress interrupt {:x}", pending);
irqarray2.wo(utra::irqarray2::EV_PENDING, pending);
} else if (irqs_pending & (1 << utra::irqarray13::IRQARRAY13_IRQ)) != 0 {
// irq13 -> {aoramerr, secirq, ifsuberr, sceerr, coresuberr}
// this IRQ will trigger an attack response if any of the sensors trigger *or*
// if the ECC-protected memory banks trigger an ECC error
bao1x_hal::hardening::glitch_handler(
ATTACKS_SINCE_BOOT.fetch_add(1, core::sync::atomic::Ordering::SeqCst),
);
} else if (irqs_pending & (1 << utra::irqarray15::IRQARRAY15_IRQ)) != 0 {
// irq15 -> {meshirq, sensorcirq, glcirq}
// those one is kind of redundant with irq13 in this scenario, because secirq is the OR
// of all three IRQs going into this array. So, we handle this simply in irqarray13.
}
}

View file

@ -148,6 +148,16 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa
crate::println!("backup region is clean!");
}
irq_setup();
// all sensors & IRQs setup already by boot0, but it doesn't hurt to re-write these registers
// in case they were defeated somehow previously
let mut irq13 = CSR::new(utra::irqarray13::HW_IRQARRAY13_BASE as *mut u32);
irq13.wo(utra::irqarray13::EV_EDGE_TRIGGERED, 0xFFFF_FFFF);
irq13.wo(utra::irqarray13::EV_POLARITY, 0xFFFF_FFFF);
irq13.wo(utra::irqarray13::EV_ENABLE, 0xFFFF_FFFF);
// enable the IRQ because it was disabled by the previous stage's exit
enable_irq(utra::irqarray13::IRQARRAY13_IRQ);
let iox = Iox::new(utra::iox::HW_IOX_BASE as *mut u32);
// setup board-specific I/Os - early boot set. These are items that have to be
@ -343,7 +353,6 @@ pub fn early_init(mut board_type: bao1x_api::BoardTypeCoding) -> (bao1x_api::Boa
// Rx setup
let _udma_uart = setup_console(&board_type, &iox, perclk);
irq_setup();
enable_irq(utra::irqarray5::IRQARRAY5_IRQ);
crate::debug::USE_CONSOLE.store(true, core::sync::atomic::Ordering::SeqCst);

View file

@ -6,6 +6,8 @@ use vexriscv::register::vexriscv::{mim, mip};
use crate::usb::USB;
use crate::*;
static ATTACKS_SINCE_BOOT: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0);
pub fn irq_setup() {
unsafe {
#[rustfmt::skip]
@ -212,12 +214,17 @@ pub extern "C" fn trap_handler(
timer0.wfo(utra::timer0::RELOAD_RELOAD, (DEFAULT_FCLK_FREQUENCY / 1_000) * ms);
} else if (irqs_pending & (1 << utra::irqarray5::IRQARRAY5_IRQ)) != 0 {
crate::uart_irq_handler();
} else if (irqs_pending & (1 << utra::irqarray2::IRQARRAY2_IRQ)) != 0 {
// just clear it for now
let mut irqarray2 = CSR::new(utralib::HW_IRQARRAY2_BASE as *mut u32);
let pending = irqarray2.r(utra::irqarray2::EV_PENDING);
crate::println!("keypress interrupt {:x}", pending);
irqarray2.wo(utra::irqarray2::EV_PENDING, pending);
} else if (irqs_pending & (1 << utra::irqarray13::IRQARRAY13_IRQ)) != 0 {
// irq13 -> {aoramerr, secirq, ifsuberr, sceerr, coresuberr}
// this IRQ will trigger an attack response if any of the sensors trigger *or*
// if the ECC-protected memory banks trigger an ECC error
bao1x_hal::hardening::glitch_handler(
ATTACKS_SINCE_BOOT.fetch_add(1, core::sync::atomic::Ordering::SeqCst),
);
} else if (irqs_pending & (1 << utra::irqarray15::IRQARRAY15_IRQ)) != 0 {
// irq15 -> {meshirq, sensorcirq, glcirq}
// those one is kind of redundant with irq13 in this scenario, because secirq is the OR
// of all three IRQs going into this array. So, we handle this simply in irqarray13.
}
if (irqs_pending & (1 << utralib::utra::irqarray1::IRQARRAY1_IRQ)) != 0 {
// handle USB interrupt