5 #include "sw/device/lib/testing/test_framework/ottf_isrs.h"
7 #include "dt/dt_sram_ctrl.h"
15 #include "sw/device/lib/testing/test_framework/check.h"
17 dif_rv_plic_t ottf_plic;
21 static const char *exception_reason[] = {
22 "Instruction Misaligned",
24 "Illegal Instruction",
26 "Load Address Misaligned",
28 "Store Address Misaligned",
34 "Instruction Page Fault",
56 static const char *exc_frame[] = {
57 "mepc",
" ra",
" t0",
" t1",
" t2",
" s0",
" s1",
" a0",
58 " a1",
" a2",
" a3",
" a4",
" a5",
" a6",
" a7",
" s2",
59 " s3",
" s4",
" s5",
" s6",
" s7",
" s8",
" s9",
" s10",
60 " s11",
" t3",
" t4",
" t5",
" t6",
"msts",
63 void ottf_generic_fault_print(uint32_t *exc_info,
const char *reason,
65 enum { kExcWords = 30 };
69 base_printf(
"===== Exception Frame @ %08x =====", exc_info);
70 for (
size_t i = 0; i < kExcWords; ++i) {
74 const char *name = exc_frame[i];
79 uint32_t *sp = exc_info + kExcWords;
81 uint32_t ram_base_addr =
82 dt_sram_ctrl_reg_block(kDtSramCtrlMain, kDtSramCtrlRegBlockRam);
83 uint32_t *ram_start = (uint32_t *)ram_base_addr;
85 #if defined(OPENTITAN_IS_EARLGREY)
87 #elif defined(OPENTITAN_IS_DARJEELING)
88 uint32_t ram_size = TOP_DARJEELING_SRAM_CTRL_MAIN_RAM_SIZE_BYTES;
90 #error unsupported top
92 uint32_t *ram_end = (uint32_t *)(ram_base_addr + ram_size);
94 extern const char _text_start[], _text_end[];
95 const uint32_t text_start = (uint32_t)_text_start;
96 const uint32_t text_end = (uint32_t)_text_end;
98 for (; sp >= ram_start && sp < ram_end; ++sp) {
100 if (val >= text_start && val < text_end) {
105 LOG_ERROR(
"FAULT: %s. MCAUSE=%08x MEPC=%08x MTVAL=%08x", reason, mcause, mepc,
109 static void generic_fault_handler(uint32_t *exc_info) {
111 ottf_generic_fault_print(exc_info, exception_reason[mcause & kIbexExcMax],
123 void *pxCurrentTCB = NULL;
126 void ottf_exception_handler(uint32_t *exc_info) {
130 case kIbexExcInstrMisaligned:
131 ottf_instr_misaligned_fault_handler(exc_info);
133 case kIbexExcInstrAccessFault:
134 ottf_instr_access_fault_handler(exc_info);
136 case kIbexExcIllegalInstrFault:
137 ottf_illegal_instr_fault_handler(exc_info);
139 case kIbexExcBreakpoint:
140 ottf_breakpoint_handler(exc_info);
142 case kIbexExcLoadAccessFault:
143 ottf_load_store_fault_handler(exc_info);
145 case kIbexExcStoreAccessFault:
146 ottf_load_store_fault_handler(exc_info);
148 case kIbexExcMachineECall:
149 ottf_machine_ecall_handler(exc_info);
151 case kIbexExcUserECall:
152 ottf_user_ecall_handler(exc_info);
155 generic_fault_handler(exc_info);
161 void ottf_instr_misaligned_fault_handler(uint32_t *exc_info);
165 void ottf_instr_access_fault_handler(uint32_t *exc_info);
169 void ottf_illegal_instr_fault_handler(uint32_t *exc_info);
173 void ottf_breakpoint_handler(uint32_t *exc_info);
177 void ottf_load_store_fault_handler(uint32_t *exc_info);
181 void ottf_machine_ecall_handler(uint32_t *exc_info);
185 void ottf_user_ecall_handler(uint32_t *exc_info);
188 void ottf_software_isr(uint32_t *exc_info) {
194 void ottf_timer_isr(uint32_t *exc_info) {
200 bool ottf_console_flow_control_isr(uint32_t *exc_info) {
return false; }
203 void ottf_external_isr(uint32_t *exc_info) {
204 const uint32_t kPlicTarget = 0;
208 dt_instance_id_t devid = dt_plic_id_to_instance_id(plic_irq_id);
210 bool handled = ottf_handle_irq(exc_info, devid, plic_irq_id);
212 if (handled || ottf_console_flow_control_isr(exc_info)) {
219 LOG_ERROR(
"unhandled IRQ: plic_id=%d, instance ID=%d", plic_irq_id, devid);
224 static void generic_internal_irq_handler(uint32_t *exc_info) {
230 bool ottf_handle_irq(uint32_t *exc_info, dt_instance_id_t devid,
236 OT_ALIAS(
"generic_internal_irq_handler")
237 void ottf_external_nmi_handler(uint32_t *exc_info);
240 OT_ALIAS("generic_internal_irq_handler")
241 void ottf_load_integrity_error_handler(uint32_t *exc_info);
244 void ottf_internal_isr(uint32_t *exc_info) {
247 case kIbexInternalIrqLoadInteg:
248 ottf_load_integrity_error_handler(exc_info);
250 case kIbexInternalIrqNmi:
251 ottf_external_nmi_handler(exc_info);
254 generic_internal_irq_handler(exc_info);