Sapphire SoC DS Sapphire SoC UG Sapphire HP SoC DS Sapphire HP SoC UG RISC-V Embedded IDE UG Board Support Package
Loading...
Searching...
No Matches
mtrap.c
Go to the documentation of this file.
1
2// Copyright (C) 2013-2026 Efinix Inc. All rights reserved.
3// Full license header bsp/efinix/EfxSapphireSocRV64/include/LICENSE.MD
5
6
16
17#include <stddef.h>
18#include "mtrap.h"
19
20int (*interrupt_vector_table[64])(void) =
21{
26 0, // 4
27 0, // 5
31 0, // 9
32 0, // 10
42 0, // 20
50 0, // 28
51 0, // 29
62};
63
64
65/******************************************************************************
66*
67* @brief This function uses for setting up the trap entry point.
68* @note Naked function to setup the stack and call the C trap handler.
69*
70******************************************************************************/
71__attribute__((naked, aligned(4)))
72void trap_entry (void) {
73
74 asm volatile (
75 "addi sp,sp, -(16*" STR(WORD)")\n"
76
77 STORE " x1, 0*"STR(WORD)"(sp)\n"
78 STORE " x5, 1*"STR(WORD)"(sp)\n"
79 STORE " x6, 2*"STR(WORD)"(sp)\n"
80 STORE " x7, 3*"STR(WORD)"(sp)\n"
81 STORE " x10, 4*"STR(WORD)"(sp)\n"
82 STORE " x11, 5*"STR(WORD)"(sp)\n"
83 STORE " x12, 6*"STR(WORD)"(sp)\n"
84 STORE " x13, 7*"STR(WORD)"(sp)\n"
85 STORE " x14, 8*"STR(WORD)"(sp)\n"
86 STORE " x15, 9*"STR(WORD)"(sp)\n"
87 STORE " x16, 10*"STR(WORD)"(sp)\n"
88 STORE " x17, 11*"STR(WORD)"(sp)\n"
89 STORE " x28, 12*"STR(WORD)"(sp)\n"
90 STORE " x29, 13*"STR(WORD)"(sp)\n"
91 STORE " x30, 14*"STR(WORD)"(sp)\n"
92 STORE " x31, 15*"STR(WORD)"(sp)\n"
93 "call trap\n"
94 LOAD " x1 , 0*"STR(WORD)"(sp)\n"
95 LOAD " x5, 1*"STR(WORD)"(sp)\n"
96 LOAD " x6, 2*"STR(WORD)"(sp)\n"
97 LOAD " x7, 3*"STR(WORD)"(sp)\n"
98 LOAD " x10, 4*"STR(WORD)"(sp)\n"
99 LOAD " x11, 5*"STR(WORD)"(sp)\n"
100 LOAD " x12, 6*"STR(WORD)"(sp)\n"
101 LOAD " x13, 7*"STR(WORD)"(sp)\n"
102 LOAD " x14, 8*"STR(WORD)"(sp)\n"
103 LOAD " x15, 9*"STR(WORD)"(sp)\n"
104 LOAD " x16, 10*"STR(WORD)"(sp)\n"
105 LOAD " x17, 11*"STR(WORD)"(sp)\n"
106 LOAD " x28, 12*"STR(WORD)"(sp)\n"
107 LOAD " x29, 13*"STR(WORD)"(sp)\n"
108 LOAD " x30, 14*"STR(WORD)"(sp)\n"
109 LOAD " x31, 15*"STR(WORD)"(sp)\n"
110 "addi sp,sp, 16*"STR(WORD)"\n"
111 "mret\n"
112 );
113
114}
115
116/******************************************************************************
117*
118* @brief Claim pending interrupts for a hart and call their corresponding handlers.
119*
120* @param hart_id Hart ID
121*
122* @note Uses PLIC claim/release mechanism.
123*
124******************************************************************************/
126{
127 volatile uintptr_t id;
128
129 while ((id = plic_claimExtIRQ_m())) {
130
131 // Call the external interrupt handler
133
135 }
136}
137
138/******************************************************************************
139*
140* @brief This function handles to report exception counter and cause .
141*
142* @note Default exception handler to read and print mepc and mcause.
143* __attribute__((weak)) means the user can override this function.
144*
145******************************************************************************/
146__attribute__((weak))
148{
149 LOG_ERR(DBG_MOD_FAULT,"Exception occurred!\n");
150 LOG_ERR(DBG_MOD_FAULT,"mepc: 0x%lx\n", csr_read(mepc));
151 LOG_ERR(DBG_MOD_FAULT,"mcause: 0x%lx\n", csr_read(mcause));
152
153}
154
155/******************************************************************************
156*
157* @brief This function uses when unexpected cause happens.
158*
159* @note Default crash handler for any unhandled exceptions or interrupts.
160* __attribute__((weak)) means the user can override this function.
161*
162******************************************************************************/
163__attribute__((weak))
164void crash()
165{
166 LOG_ERR(DBG_MOD_FAULT,"\n*** Unsupported irq or exception ***\n");
167 while(1) {
168 __asm__ volatile ("wfi");
169 }
170}
171
172/******************************************************************************
173*
174* @brief Default function handles both exceptions and interrupts.
175*
176* @note Called by trap_entry (from trap.S) on any trap event.
177* It reads mcause to determine the type of trap, and then
178* calls the corresponding handler function.
179* If unhandled, it calls crash().
180* __attribute__((weak)) means the user can override this function.
181*
182******************************************************************************/
183__attribute__((weak))
184void trap(){
185 // Read the current hart/core ID
186 volatile uintptr_t mhartid = csr_read(mhartid);
187
188 // Read the cause of the trap
189 volatile intptr_t mcause = csr_read(mcause);
190
191 // Determine if it is an interrupt (true) or exception (false)
192 intptr_t interrupt = mcause < 0;
193 intptr_t cause = mcause & 0xF; // Lower bits indicate cause
194
195 if(interrupt){ // Handle interrupts
196 switch(cause){
197
199 // Needs a handler that clears the CLINT MSIP bit.
201 break;
202
204 // Needs a handler that updates the CLINT mtimecmp register.
206 break;
207
209 // It handles the PLIC "Claim/Release" dance.
210 irq_handleExt();
211 break;
212 default:
213 crash(); // Call crash if interrupt is unhandled
214 }
215 } else { // Handle exceptions
216 switch (cause){
217 case CAUSE_INSTRUCTION_ADDR_MISALIGNED: // Instruction address misaligned
218 case CAUSE_ACCESS_FAULT: // Memory access fault
219 case CAUSE_ILLEGAL_INSTRUCTION: // Illegal instruction executed
220 case CAUSE_BREAKPOINT: // Breakpoint trap
221 case CAUSE_LOAD_ADDR_MISALIGNED: // Load address misaligned
222 case CAUSE_LOAD_ACCESS_FAULT: // Load access fault
223 case CAUSE_STORE_AMO_ADDR_MISALIGNED: // Store/AMO address misaligned
224 case CAUSE_STORE_AMO_ACCESS_FAULT: // Store/AMO access fault
225 case CAUSE_ENV_CALL_U_MODE: // Environment call from U-mode
226 case CAUSE_ENV_CALL_S_MODE: // Environment call from S-mode
227 case CAUSE_ENV_CALL_M_MODE: // Environment call from M-mode
228 case CAUSE_INSTRUCTION_PAGE_FAULT: // Instruction page fault
229 case CAUSE_LOAD_PAGE_FAULT: // Load page fault
230 case CAUSE_STORE_AMO_PAGE_FAULT: // Store/AMO page fault
232 break;
233 default: // Any other exception
234 crash();
235 }
236 }
237}
#define LOG_ERR(debug, fmt,...)
Definition debug.h:239
#define DBG_MOD_FAULT
Hard Faults / Errors.
Definition debug.h:104
void irq_handleExt()
External Interrupt Handler (PLIC).
Definition mtrap.c:125
void trap_entry(void)
The Main Trap Entry Point (Naked).
Definition mtrap.c:72
void crash()
Fatal Exception Handler.
Definition mtrap.c:164
int irq_handleSoft(void)
Handlers for CPU interrupts (Software).
void trap()
Main C Trap Dispatcher.
Definition mtrap.c:184
int irq_handleTimer(void)
Handlers for CPU interrupts (Timer).
#define LOAD
Load Word (32-bit).
Definition mtrap.h:63
#define STR(x)
Context Helper Only.
Definition mtrap.h:68
#define WORD
Word size in bytes.
Definition mtrap.h:64
#define STORE
Store Word (32-bit).
Definition mtrap.h:62
int(* interrupt_vector_table[64])(void)
Global Interrupt Vector Table.
Definition mtrap.c:20
int irq_m_gpio1_1_handler(void)
int irq_m_user_h_handler(void)
int irq_m_user_c_handler(void)
int irq_m_gpio0_0_handler(void)
GPIO (IDs 16-19).
int irq_m_spi1_handler(void)
int irq_m_user_f_handler(void)
int irq_m_gpio0_1_handler(void)
int irq_m_userTimer2_handler(void)
int irq_m_userTimer4_handler(void)
int irq_m_uart1_handler(void)
int irq_m_watchDog1_handler(void)
int irq_m_i2c3_handler(void)
int irq_m_invalid_handler(void)
System Interrupts.
int irq_m_l2Cache_handler(void)
L2 Cache Control (ID 31).
int irq_m_user_e_handler(void)
int irq_m_i2c0_handler(void)
I2C (IDs 11-15).
int irq_m_userTimer1_handler(void)
int irq_m_user_g_handler(void)
int irq_m_userTimer0_handler(void)
User Timer (IDs 21-25).
int irq_m_i2c1_handler(void)
int irq_m_uart0_handler(void)
UART (IDs 1-3) Meaning: Machine Mode UART 0 Handler.
int irq_m_i2c2_handler(void)
int irq_m_axiA_handler(void)
AXI A (ID 30).
int irq_m_gpio1_0_handler(void)
int irq_m_watchDog0_handler(void)
WatchDog Timer (IDs 26-27).
int irq_m_userTimer3_handler(void)
int irq_m_user_b_handler(void)
int irq_m_spi0_handler(void)
SPI (IDs 6-8).
int irq_m_uart2_handler(void)
int irq_m_spi2_handler(void)
int irq_m_user_d_handler(void)
int irq_m_i2c4_handler(void)
int irq_m_user_a_handler(void)
User Interrupts (IDs 32-39) /*.
void plic_releaseExtIRQ_m(u32 gateway)
Release ID source from external IRQ.
Definition plic.c:34
u32 plic_claimExtIRQ_m()
Initialize the PLIC instance with a base address. Calculates the internal pointers for the register b...
Definition plic.c:28
#define CAUSE_ILLEGAL_INSTRUCTION
Exception: Illegal Instruction.
Definition riscv.h:59
#define CAUSE_ENV_CALL_M_MODE
Exception: Environment Call from M Mode.
Definition riscv.h:67
#define CAUSE_STORE_AMO_PAGE_FAULT
Exception: Store/AMO Page Fault.
Definition riscv.h:70
#define CAUSE_MACHINE_EXTERNAL
Interrupt: Machine External.
Definition riscv.h:51
#define CAUSE_MACHINE_SOFTWARE
Interrupt: Machine Software.
Definition riscv.h:45
#define CAUSE_ENV_CALL_U_MODE
Exception: Environment Call from U Mode.
Definition riscv.h:65
#define CAUSE_LOAD_ADDR_MISALIGNED
Exception: Load Address Misaligned.
Definition riscv.h:61
#define CAUSE_ACCESS_FAULT
Exception: Instruction Access Fault.
Definition riscv.h:58
#define CAUSE_BREAKPOINT
Exception: Breakpoint.
Definition riscv.h:60
#define CAUSE_STORE_AMO_ACCESS_FAULT
Exception: Store/AMO Access Fault.
Definition riscv.h:64
#define CAUSE_INSTRUCTION_ADDR_MISALIGNED
Exception: Instruction Address Misaligned.
Definition riscv.h:57
#define CAUSE_LOAD_ACCESS_FAULT
Exception: Load Access Fault.
Definition riscv.h:62
#define CAUSE_INSTRUCTION_PAGE_FAULT
Exception: Instruction Page Fault.
Definition riscv.h:68
#define CAUSE_MACHINE_TIMER
Interrupt: Machine Timer.
Definition riscv.h:48
#define CAUSE_LOAD_PAGE_FAULT
Exception: Load Page Fault.
Definition riscv.h:69
#define CAUSE_STORE_AMO_ADDR_MISALIGNED
Exception: Store/AMO Address Misaligned.
Definition riscv.h:63
#define CAUSE_ENV_CALL_S_MODE
Exception: Environment Call from S Mode.
Definition riscv.h:66
#define csr_read(csr)
This function is used to read the value of a CSR.
Definition riscv.h:188
void irq_handleException()
Definition mtrap.c:147
Trap (Interrupt) and Exception Handling Definitions.