10 #include "sw/device/lib/base/multibits.h"
14 #include "sw/device/lib/testing/lc_ctrl_testutils.h"
15 #include "sw/device/lib/testing/otp_ctrl_testutils.h"
16 #include "sw/device/lib/testing/sram_ctrl_testutils.h"
17 #include "sw/device/lib/testing/test_framework/check.h"
18 #include "sw/device/lib/testing/test_framework/ottf_isrs.h"
19 #include "sw/device/lib/testing/test_framework/ottf_macros.h"
21 #include "sw/device/lib/testing/test_framework/status.h"
24 #include "otp_ctrl_regs.h"
26 OTTF_DEFINE_TEST_CONFIG();
28 static dif_sram_ctrl_t sram_ctrl;
35 static volatile bool exception_observed;
48 static const uint32_t kOtpIfetchHwRelativeOffset =
49 OTP_CTRL_PARAM_EN_SRAM_IFETCH_OFFSET - OTP_CTRL_PARAM_HW_CFG1_OFFSET;
59 void execute_code_in_sram(
void) {
asm volatile(
"jalr zero, 0(ra)"); }
61 static bool otp_ifetch_enabled(
void) {
63 CHECK_DIF_OK(dif_otp_ctrl_init(
68 .integrity_period_mask = 0x3ffff,
69 .consistency_period_mask = 0x3ffffff,
74 kOtpIfetchHwRelativeOffset));
76 CHECK_STATUS_OK(otp_ctrl_testutils_wait_for_dai(&otp));
106 void ottf_exception_handler(uint32_t *exc_info) {
121 uintptr_t ret_addr = *(uintptr_t *)(mepc_stack_addr + OTTF_WORD_SIZE);
123 LOG_INFO(
"Handling exception: mepc = %p, (trapped) return address = %p",
130 case kIbexExcInstrAccessFault:
131 LOG_INFO(
"Instruction access fault handler");
132 exception_observed =
true;
133 *(uintptr_t *)mepc_stack_addr = ret_addr;
135 case kIbexExcIllegalInstrFault:
136 LOG_INFO(
"Illegal instruction fault handler");
137 exception_observed =
true;
138 *(uintptr_t *)mepc_stack_addr = ret_addr;
141 LOG_FATAL(
"Unexpected exception id = 0x%x", exception);
152 void do_execute_test(
bool debug_func,
bool ifetch_en) {
153 bool csr_enabled_exception_expected;
154 bool csr_disabled_exception_expected;
156 if (debug_func && !ifetch_en) {
157 csr_enabled_exception_expected =
false;
158 csr_disabled_exception_expected =
false;
159 }
else if (debug_func && ifetch_en) {
160 csr_enabled_exception_expected =
false;
161 csr_disabled_exception_expected =
true;
162 }
else if (!debug_func && !ifetch_en) {
163 csr_enabled_exception_expected =
true;
164 csr_disabled_exception_expected =
true;
166 csr_enabled_exception_expected =
false;
167 csr_disabled_exception_expected =
true;
171 exception_observed =
false;
173 execute_code_in_sram();
174 CHECK(exception_observed == csr_enabled_exception_expected,
175 "Expected exception not observed whilst executing from SRAM!");
177 exception_observed =
false;
179 execute_code_in_sram();
180 CHECK(exception_observed == csr_disabled_exception_expected,
181 "Expected exception not observed whilst executing from SRAM!");
193 uintptr_t func_address = (uintptr_t)execute_code_in_sram;
194 CHECK(func_address >= kRamStartAddr && func_address <= kRamEndAddr,
195 "Test code resides outside of the Main SRAM: function address = %x",
198 CHECK_DIF_OK(dif_sram_ctrl_init(
205 CHECK(!locked,
"Execution is disabled and locked, cannot perform the test");
208 CHECK_DIF_OK(dif_lc_ctrl_init(
211 bool debug_func =
false;
212 CHECK_STATUS_OK(lc_ctrl_testutils_debug_func_enabled(&lc, &debug_func));
215 do_execute_test(debug_func, otp_ifetch_enabled());
220 test_status_set(kTestStatusInWfi);