44 */
55
66#include <assert.h>
7- #include <setjmp.h>
87#include <stdbool.h>
98#include <stdint.h>
109#include <stdio.h>
@@ -52,7 +51,9 @@ extern struct target_ops gdbstub_ops;
5251 _(breakpoint, 3) /* Breakpoint */ \
5352 _ (load_misaligned , 4 ) /* Load address misaligned */ \
5453 _ (store_misaligned , 6 ) /* Store/AMO address misaligned */ \
55- _ (ecall_M , 11 ) /* Environment call from M-mode */
54+ IIF (RV32_HAS (SYSTEM ))(, \
55+ _ (ecall_M , 11 ) /* Environment call from M-mode */ \
56+ )
5657/* clang-format on */
5758
5859enum {
@@ -67,56 +68,56 @@ static void rv_trap_default_handler(riscv_t *rv)
6768 rv -> PC = rv -> csr_mepc ; /* mret */
6869}
6970
70- /* Trap might occurs during block emulation. For instance, page fault.
71+ /*
72+ * Trap might occurs during block emulation. For instance, page fault.
7173 * In order to handle trap, we have to escape from block and execute
7274 * registered trap handler. This trap_handler function helps to execute
7375 * the registered trap handler, PC by PC. Once the trap is handled,
7476 * resume the previous execution flow where cause the trap.
77+ *
78+ * Since the system emulation has not yet included in rv32emu, the page
79+ * fault is not practical in current test suite. Instead, we try to
80+ * emulate the misaligned handling in the test suite.
7581 */
7682#if RV32_HAS (SYSTEM )
7783static void trap_handler (riscv_t * rv );
78- #else
79- /* should not be called in non-SYSTEM mode since default trap handler is capable
80- * to handle traps
81- */
82- static void trap_handler (riscv_t * rv UNUSED ) {}
8384#endif
8485
85- /* When a trap occurs in M-mode, mtval is either initialized to zero or
86+ /* When a trap occurs in M-mode/S-mode, m/stval is either initialized to zero or
8687 * populated with exception-specific details to assist software in managing
87- * the trap. Otherwise, the implementation never modifies mtval , although
88+ * the trap. Otherwise, the implementation never modifies m/stval , although
8889 * software can explicitly write to it. The hardware platform will define
8990 * which exceptions are required to informatively set mtval and which may
9091 * consistently set it to zero.
9192 *
9293 * When a hardware breakpoint is triggered or an exception like address
9394 * misalignment, access fault, or page fault occurs during an instruction
94- * fetch, load, or store operation, mtval is updated with the virtual address
95- * that caused the fault. In the case of an illegal instruction trap, mtval
95+ * fetch, load, or store operation, m/stval is updated with the virtual address
96+ * that caused the fault. In the case of an illegal instruction trap, m/stval
9697 * might be updated with the first XLEN or ILEN bits of the offending
97- * instruction. For all other traps, mtval is simply set to zero. However,
98- * it is worth noting that a future standard could redefine how mtval is
98+ * instruction. For all other traps, m/stval is simply set to zero. However,
99+ * it is worth noting that a future standard could redefine how m/stval is
99100 * handled for different types of traps.
101+ *
102+ * For simplicity and clarity, abstracting stval and mtval into a single
103+ * identifier called tval, as both are handled by TRAP_HANDLER_IMPL.
100104 */
101- static jmp_buf env ;
102105#define TRAP_HANDLER_IMPL (type , code ) \
103- static void rv_trap_##type(riscv_t *rv, uint32_t mtval) \
106+ static void rv_trap_##type(riscv_t *rv, uint32_t tval) \
104107 { \
105108 /* m/stvec (Machine/Supervisor Trap-Vector Base Address Register) \
106109 * m/stvec[MXLEN-1:2]: vector base address \
107110 * m/stvec[1:0] : vector mode \
108- */ \
109- uint32_t base ; \
110- uint32_t mode ; \
111- /* m/sepc (Machine/Supervisor Exception Program Counter) \
111+ * m/sepc (Machine/Supervisor Exception Program Counter) \
112112 * m/stval (Machine/Supervisor Trap Value Register) \
113113 * m/scause (Machine/Supervisor Cause Register): store exception code \
114114 * m/sstatus (Machine/Supervisor Status Register): keep track of and \
115115 * controls the hart’s current operating state \
116116 */ \
117- /* supervisor */ \
118- if (rv -> csr_medeleg & (1U << code ) || \
119- rv -> csr_mideleg & (1U << code )) { \
117+ uint32_t base ; \
118+ uint32_t mode ; \
119+ /* user or supervisor */ \
120+ if (RV_PRIV_IS_U_OR_S_MODE ()) { \
120121 const uint32_t sstatus_sie = \
121122 (rv -> csr_sstatus & SSTATUS_SIE ) >> SSTATUS_SIE_SHIFT ; \
122123 rv -> csr_sstatus |= (sstatus_sie << SSTATUS_SPIE_SHIFT ); \
@@ -126,10 +127,9 @@ static jmp_buf env;
126127 base = rv -> csr_stvec & ~0x3 ; \
127128 mode = rv -> csr_stvec & 0x3 ; \
128129 rv -> csr_sepc = rv -> PC ; \
129- rv -> csr_stval = mtval ; \
130+ rv -> csr_stval = tval ; \
130131 rv -> csr_scause = code ; \
131- rv -> csr_sstatus |= SSTATUS_SPP ; /* set privilege mode */ \
132- } else { /* machine */ \
132+ } else { /* machine */ \
133133 const uint32_t mstatus_mie = \
134134 (rv -> csr_mstatus & MSTATUS_MIE ) >> MSTATUS_MIE_SHIFT ; \
135135 rv -> csr_mstatus |= (mstatus_mie << MSTATUS_MPIE_SHIFT ); \
@@ -139,9 +139,8 @@ static jmp_buf env;
139139 base = rv -> csr_mtvec & ~0x3 ; \
140140 mode = rv -> csr_mtvec & 0x3 ; \
141141 rv -> csr_mepc = rv -> PC ; \
142- rv -> csr_mtval = mtval ; \
142+ rv -> csr_mtval = tval ; \
143143 rv -> csr_mcause = code ; \
144- rv -> csr_mstatus |= MSTATUS_MPP ; /* set privilege mode */ \
145144 if (!rv -> csr_mtvec ) { /* in case CSR is not configured */ \
146145 rv_trap_default_handler (rv ); \
147146 return ; \
@@ -159,14 +158,7 @@ static jmp_buf env;
159158 rv -> PC = base + 4 * (code & MASK (31 )); \
160159 break ; \
161160 } \
162- /* block escaping for trap handling */ \
163- if (rv -> is_trapped ) { \
164- if (setjmp (env ) == 0 ) { \
165- trap_handler (rv ); \
166- } else { \
167- fprintf (stderr , "setjmp failed" ); \
168- } \
169- } \
161+ IIF (RV32_HAS (SYSTEM ))(if (rv -> is_trapped ) trap_handler (rv );, ) \
170162 }
171163
172164/* RISC-V exception handlers */
@@ -188,7 +180,7 @@ RV_TRAP_LIST
188180 rv->compressed = compress; \
189181 rv->csr_cycle = cycle; \
190182 rv->PC = PC; \
191- rv->is_trapped = true; \
183+ IIF(RV32_HAS(SYSTEM))( rv->is_trapped = true, ); \
192184 rv_trap_##type##_misaligned(rv, IIF(IO)(addr, mask_or_pc)); \
193185 return false; \
194186 }
@@ -455,9 +447,10 @@ enum {
455447};
456448
457449#if RV32_HAS (GDBSTUB )
458- #define RVOP_NO_NEXT (ir ) (!ir->next | rv->debug_mode | rv->is_trapped)
450+ #define RVOP_NO_NEXT (ir ) \
451+ (!ir->next | rv->debug_mode IIF(RV32_HAS(SYSTEM))(| rv->is_trapped, ))
459452#else
460- #define RVOP_NO_NEXT (ir ) (!ir->next | rv->is_trapped)
453+ #define RVOP_NO_NEXT (ir ) (!ir->next IIF(RV32_HAS(SYSTEM))( | rv->is_trapped, ) )
461454#endif
462455
463456/* record whether the branch is taken or not during emulation */
@@ -643,8 +636,10 @@ FORCE_INLINE bool insn_is_unconditional_branch(uint8_t opcode)
643636 case rv_insn_ebreak :
644637 case rv_insn_jal :
645638 case rv_insn_jalr :
646- case rv_insn_sret :
647639 case rv_insn_mret :
640+ #if RV32_HAS (SYSTEM )
641+ case rv_insn_sret :
642+ #endif
648643#if RV32_HAS (EXT_C )
649644 case rv_insn_cj :
650645 case rv_insn_cjalr :
@@ -1132,9 +1127,12 @@ static void trap_handler(riscv_t *rv)
11321127 rv_insn_t * ir = mpool_alloc (rv -> block_ir_mp );
11331128 assert (ir );
11341129
1130+ /* set to false by sret/mret implementation */
11351131 uint32_t insn ;
1136- while (rv -> is_trapped ) { /* set to false by sret/mret implementation */
1137- insn = rv -> io .mem_ifetch (rv , rv -> PC );
1132+ while (rv -> is_trapped && !rv_has_halted (rv )) {
1133+ insn = rv -> io .mem_ifetch (rv -> PC );
1134+ assert (insn );
1135+
11381136 rv_decode (ir , insn );
11391137 ir -> impl = dispatch_table [ir -> opcode ];
11401138 rv -> compressed = is_compressed (insn );
@@ -1152,8 +1150,13 @@ void ebreak_handler(riscv_t *rv)
11521150void ecall_handler (riscv_t * rv )
11531151{
11541152 assert (rv );
1153+ #if RV32_HAS (SYSTEM )
1154+ syscall_handler (rv );
1155+ rv -> PC += 4 ;
1156+ #else
11551157 rv_trap_ecall_M (rv , 0 );
11561158 syscall_handler (rv );
1159+ #endif
11571160}
11581161
11591162void memset_handler (riscv_t * rv )
0 commit comments