From cd4a0bb39efe9dee8902d6f4158ca4d5f2544627 Mon Sep 17 00:00:00 2001 From: abdul124 Date: Mon, 12 Aug 2024 12:59:06 +0800 Subject: [PATCH] firmware/ksupport: add exception unittests --- artiq/firmware/ksupport/api.rs | 7 +++++++ artiq/firmware/ksupport/eh_artiq.rs | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/artiq/firmware/ksupport/api.rs b/artiq/firmware/ksupport/api.rs index 47b2e5551..95ba5dbc5 100644 --- a/artiq/firmware/ksupport/api.rs +++ b/artiq/firmware/ksupport/api.rs @@ -173,4 +173,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 fbddb47ec..52e73a7b0 100644 --- a/artiq/firmware/ksupport/eh_artiq.rs +++ b/artiq/firmware/ksupport/eh_artiq.rs @@ -328,7 +328,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); 20] = [ ("RTIOUnderflow", 0), ("RTIOOverflow", 1), @@ -361,3 +361,24 @@ 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] +pub extern "C-unwind" 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) }; +} +