diff --git a/artiq/firmware/ksupport/api.rs b/artiq/firmware/ksupport/api.rs index 1682f818b..7b1fa4ec2 100644 --- a/artiq/firmware/ksupport/api.rs +++ b/artiq/firmware/ksupport/api.rs @@ -167,4 +167,11 @@ static mut API: &'static [(&'static str, *const ())] = &[ api!(spi_set_config = ::nrt_bus::spi::set_config), api!(spi_write = ::nrt_bus::spi::write), api!(spi_read = ::nrt_bus::spi::read), + + /* + * syscall for unit tests + * Used in `artiq.tests.coredevice.test_exceptions.ExceptionTest.test_raise_exceptions_kernel` + * `EmbeddingMap` (`artiq.compiler.embedding`) and `EXCEPTION_ID_LOOKUP` (`artiq.firmware.ksupport.eh_artiq`) + */ + api!(test_exception_id_sync = ::eh_artiq::test_exception_id_sync) ]; diff --git a/artiq/firmware/ksupport/eh_artiq.rs b/artiq/firmware/ksupport/eh_artiq.rs index 66c78d23d..44dda1015 100644 --- a/artiq/firmware/ksupport/eh_artiq.rs +++ b/artiq/firmware/ksupport/eh_artiq.rs @@ -333,7 +333,7 @@ extern fn stop_fn(_version: c_int, } } -// Must be kept in sync with `artq.compiler.embedding` +// Must be kept in sync with `artiq.compiler.embedding` static EXCEPTION_ID_LOOKUP: [(&str, u32); 19] = [ ("RTIOUnderflow", 0), ("RTIOOverflow", 1), @@ -365,3 +365,25 @@ pub fn get_exception_id(name: &str) -> u32 { unimplemented!("unallocated internal exception id") } +// Performs a reverse lookup on `EXCEPTION_ID_LOOKUP` +// Unconditionally raises an exception given its id +#[no_mangle] +#[unwind(allowed)] +pub extern fn test_exception_id_sync(exn_id: u32) { + let message = EXCEPTION_ID_LOOKUP + .iter() + .find_map(|&(name, id)| if id == exn_id { Some(name) } else { None }) + .unwrap_or("Unknown exception ID"); + + let exn = Exception { + id: exn_id, + file: file!().as_c_slice(), + line: 0, + column: 0, + function: "test_exception_id_sync".as_c_slice(), + message: message.as_c_slice(), + param: [0, 0, 0] + }; + unsafe { raise(&exn) }; +} +