NMSIS-Core  Version 1.2.0
NMSIS-Core support for Nuclei processor-based devices
Interrupt and Exception and NMI Handling

Functions for interrupt, exception and nmi handle available in system_<device>.c. More...

Macros

#define MAX_SYSTEM_EXCEPTION_NUM   26
 Max exception handler number, don't include the NMI(0xFFF) one. More...
 

Typedefs

typedef void(* EXC_HANDLER) (unsigned long cause, unsigned long sp)
 Exception Handler Function Typedef. More...
 

Functions

static void system_default_exception_handler (unsigned long mcause, unsigned long sp)
 System Default Exception Handler. More...
 
static void Exception_Init (void)
 Initialize all the default core exception handlers. More...
 
void Exception_DumpFrame (unsigned long sp, uint8_t mode)
 Dump Exception Frame. More...
 
void Exception_Register_EXC (uint32_t EXCn, unsigned long exc_handler)
 Register an exception handler for exception code EXCn. More...
 
unsigned long Exception_Get_EXC (uint32_t EXCn)
 Get current exception handler for exception code EXCn. More...
 
uint32_t core_exception_handler (unsigned long mcause, unsigned long sp)
 Common NMI and Exception handler entry. More...
 
static void system_default_exception_handler_s (unsigned long scause, unsigned long sp)
 Supervisor mode system Default Exception Handler. More...
 
void Exception_Register_EXC_S (uint32_t EXCn, unsigned long exc_handler)
 Register an exception handler for exception code EXCn of supervisor mode. More...
 
unsigned long Exception_Get_EXC_S (uint32_t EXCn)
 Get current exception handler for exception code EXCn of supervisor mode. More...
 
uint32_t core_exception_handler_s (unsigned long scause, unsigned long sp)
 common Exception handler entry of supervisor mode More...
 

Variables

static unsigned long SystemExceptionHandlers [MAX_SYSTEM_EXCEPTION_NUM+1]
 Store the exception handlers for each exception ID. More...
 
static unsigned long SystemExceptionHandlers_S [MAX_SYSTEM_EXCEPTION_NUM]
 Store the exception handlers for each exception ID in supervisor mode. More...
 

Detailed Description

Functions for interrupt, exception and nmi handle available in system_<device>.c.

Nuclei provide a template for interrupt, exception and NMI handling. Silicon Vendor could adapat according to their requirement. Silicon vendor could implement interface for different exception code and replace current implementation.

Macro Definition Documentation

◆ MAX_SYSTEM_EXCEPTION_NUM

#define MAX_SYSTEM_EXCEPTION_NUM   26

Max exception handler number, don't include the NMI(0xFFF) one.

Definition at line 269 of file system_Device.c.

Typedef Documentation

◆ EXC_HANDLER

typedef void(* EXC_HANDLER) (unsigned long cause, unsigned long sp)

Exception Handler Function Typedef.

Note
This typedef is only used internal in this system_<Device>.c file. It is used to do type conversion for registered exception handler before calling it.

Definition at line 297 of file system_Device.c.

Function Documentation

◆ core_exception_handler()

uint32_t core_exception_handler ( unsigned long  mcause,
unsigned long  sp 
)

Common NMI and Exception handler entry.

This function provided a command entry for NMI and exception. Silicon Vendor could modify this template implementation according to requirement.

Parameters
[in]mcausecode indicating the reason that caused the trap in machine mode
[in]spstack pointer
Remarks
  • RISCV provided common entry for all types of exception. This is proposed code template for exception entry function, Silicon Vendor could modify the implementation.
  • For the core_exception_handler template, we provided exception register function Exception_Register_EXC which can help developer to register your exception handler for specific exception number.

Definition at line 425 of file system_Device.c.

426 {
427  uint32_t EXCn = (uint32_t)(mcause & 0X00000fff);
428  EXC_HANDLER exc_handler;
429 
430  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
431  exc_handler = (EXC_HANDLER)SystemExceptionHandlers[EXCn];
432  } else if (EXCn == NMI_EXCn) {
434  } else {
436  }
437  if (exc_handler != NULL) {
438  exc_handler(mcause, sp);
439  }
440  return 0;
441 }

◆ core_exception_handler_s()

uint32_t core_exception_handler_s ( unsigned long  scause,
unsigned long  sp 
)

common Exception handler entry of supervisor mode

This function provided a supervisor mode common entry for exception. Silicon Vendor could modify this template implementation according to requirement.

Parameters
[in]scausecode indicating the reason that caused the trap in supervisor mode
[in]spstack pointer
Remarks
  • RISCV provided supervisor mode common entry for all types of exception. This is proposed code template for exception entry function, Silicon Vendor could modify the implementation.
  • For the core_exception_handler_s template, we provided exception register function Exception_Register_EXC_S which can help developer to register your exception handler for specific exception number.

Definition at line 514 of file system_Device.c.

515 {
516  uint32_t EXCn = (uint32_t)(scause & 0X00000fff);
517  EXC_HANDLER exc_handler;
518 
519  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
520  exc_handler = (EXC_HANDLER)SystemExceptionHandlers_S[EXCn];
521  } else {
523  }
524  if (exc_handler != NULL) {
525  exc_handler(scause, sp);
526  }
527  return 0;
528 }

◆ Exception_DumpFrame()

void Exception_DumpFrame ( unsigned long  sp,
uint8_t  mode 
)

Dump Exception Frame.

This function provided feature to dump exception frame stored in stack.

Parameters
[in]spstackpoint
[in]modeprivileged mode to decide whether to dump msubm CSR

Definition at line 351 of file system_Device.c.

352 {
353  EXC_Frame_Type *exc_frame = (EXC_Frame_Type *)sp;
354 
355 #ifndef __riscv_32e
356  printf("ra: 0x%lx, tp: 0x%lx, t0: 0x%lx, t1: 0x%lx, t2: 0x%lx, t3: 0x%lx, t4: 0x%lx, t5: 0x%lx, t6: 0x%lx\n" \
357  "a0: 0x%lx, a1: 0x%lx, a2: 0x%lx, a3: 0x%lx, a4: 0x%lx, a5: 0x%lx, a6: 0x%lx, a7: 0x%lx\n" \
358  "cause: 0x%lx, epc: 0x%lx\n", exc_frame->ra, exc_frame->tp, exc_frame->t0, \
359  exc_frame->t1, exc_frame->t2, exc_frame->t3, exc_frame->t4, exc_frame->t5, exc_frame->t6, \
360  exc_frame->a0, exc_frame->a1, exc_frame->a2, exc_frame->a3, exc_frame->a4, exc_frame->a5, \
361  exc_frame->a6, exc_frame->a7, exc_frame->cause, exc_frame->epc);
362 #else
363  printf("ra: 0x%lx, tp: 0x%lx, t0: 0x%lx, t1: 0x%lx, t2: 0x%lx\n" \
364  "a0: 0x%lx, a1: 0x%lx, a2: 0x%lx, a3: 0x%lx, a4: 0x%lx, a5: 0x%lx\n" \
365  "cause: 0x%lx, epc: 0x%lx\n", exc_frame->ra, exc_frame->tp, exc_frame->t0, \
366  exc_frame->t1, exc_frame->t2, exc_frame->a0, exc_frame->a1, exc_frame->a2, exc_frame->a3, \
367  exc_frame->a4, exc_frame->a5, exc_frame->cause, exc_frame->epc);
368 #endif
369 
370  if (PRV_M == mode) {
371  /* msubm is exclusive to machine mode */
372  printf("msubm: 0x%lx\n", exc_frame->msubm);
373  }
374 }

◆ Exception_Get_EXC()

unsigned long Exception_Get_EXC ( uint32_t  EXCn)

Get current exception handler for exception code EXCn.

  • For EXCn < MAX_SYSTEM_EXCEPTION_NUM, it will return SystemExceptionHandlers[EXCn-1].
  • For EXCn == NMI_EXCn, it will return SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM].
    Parameters
    [in]EXCnSee EXCn_Type
    Returns
    Current exception handler for exception code EXCn, if not found, return 0.

Definition at line 401 of file system_Device.c.

402 {
403  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
404  return SystemExceptionHandlers[EXCn];
405  } else if (EXCn == NMI_EXCn) {
407  } else {
408  return 0;
409  }
410 }

◆ Exception_Get_EXC_S()

unsigned long Exception_Get_EXC_S ( uint32_t  EXCn)

Get current exception handler for exception code EXCn of supervisor mode.

  • For EXCn < MAX_SYSTEM_EXCEPTION_NUM, it will return SystemExceptionHandlers_S[EXCn-1].
    Parameters
    [in]EXCnSee EXCn_Type
    Returns
    Current exception handler for exception code EXCn, if not found, return 0.

Definition at line 492 of file system_Device.c.

493 {
494  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
495  return SystemExceptionHandlers[EXCn];
496  } else {
497  return 0;
498  }
499 }

◆ Exception_Init()

static void Exception_Init ( void  )
static

Initialize all the default core exception handlers.

The core exception handler for each exception id will be initialized to system_default_exception_handler.

Note
Called in _init function, used to initialize default exception handlers for all exception IDs SystemExceptionHandlers contains NMI, but SystemExceptionHandlers_S not, because NMI can't be delegated to S-mode.

Definition at line 333 of file system_Device.c.

334 {
335  for (int i = 0; i < MAX_SYSTEM_EXCEPTION_NUM; i++) {
337 #if defined(__TEE_PRESENT) && (__TEE_PRESENT == 1)
339 #endif
340  }
342 }

◆ Exception_Register_EXC()

void Exception_Register_EXC ( uint32_t  EXCn,
unsigned long  exc_handler 
)

Register an exception handler for exception code EXCn.

  • For EXCn < MAX_SYSTEM_EXCEPTION_NUM, it will be registered into SystemExceptionHandlers[EXCn-1].
  • For EXCn == NMI_EXCn, it will be registered into SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM].
    Parameters
    [in]EXCnSee EXCn_Type
    [in]exc_handlerThe exception handler for this exception code EXCn

Definition at line 384 of file system_Device.c.

385 {
386  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
387  SystemExceptionHandlers[EXCn] = exc_handler;
388  } else if (EXCn == NMI_EXCn) {
390  }
391 }

◆ Exception_Register_EXC_S()

void Exception_Register_EXC_S ( uint32_t  EXCn,
unsigned long  exc_handler 
)

Register an exception handler for exception code EXCn of supervisor mode.

-For EXCn < MAX_SYSTEM_EXCEPTION_NUM, it will be registered into SystemExceptionHandlers_S[EXCn-1]. -For EXCn == NMI_EXCn, The NMI (Non-maskable-interrupt) cannot be trapped to the supervisor-mode or user-mode for any configuration, so NMI won't be registered into SystemExceptionHandlers_S.

Parameters
[in]EXCnSee EXCn_Type
[in]exc_handlerThe exception handler for this exception code EXCn

Definition at line 478 of file system_Device.c.

479 {
480  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
481  SystemExceptionHandlers_S[EXCn] = exc_handler;
482  }
483 }

◆ system_default_exception_handler()

static void system_default_exception_handler ( unsigned long  mcause,
unsigned long  sp 
)
static

System Default Exception Handler.

This function provides a default exception and NMI handler for all exception ids. By default, It will just print some information for debug, Vendor can customize it according to its requirements.

Parameters
[in]mcausecode indicating the reason that caused the trap in machine mode
[in]spstack pointer

Definition at line 307 of file system_Device.c.

308 {
309  /* TODO: Uncomment this if you have implement printf function */
310  printf("MCAUSE : 0x%lx\r\n", mcause);
311  printf("MDCAUSE: 0x%lx\r\n", __RV_CSR_READ(CSR_MDCAUSE));
312  printf("MEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_MEPC));
313  printf("MTVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_MTVAL));
314  printf("HARTID : %u\r\n", (unsigned int)__get_hart_id());
316 #if defined(SIMULATION_MODE)
317  // directly exit if in SIMULATION
318  extern void simulation_exit(int status);
319  simulation_exit(1);
320 #else
321  while (1);
322 #endif
323 }

◆ system_default_exception_handler_s()

static void system_default_exception_handler_s ( unsigned long  scause,
unsigned long  sp 
)
static

Supervisor mode system Default Exception Handler.

This function provided a default supervisor mode exception and NMI handling code for all exception ids. By default, It will just print some information for debug, Vendor can customize it according to its requirements.

Parameters
[in]scausecode indicating the reason that caused the trap in supervisor mode
[in]spstack pointer

Definition at line 452 of file system_Device.c.

453 {
454  /* TODO: Uncomment this if you have implement printf function */
455  printf("SCAUSE : 0x%lx\r\n", scause);
456  printf("SDCAUSE: 0x%lx\r\n", __RV_CSR_READ(CSR_SDCAUSE));
457  printf("SEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_SEPC));
458  printf("STVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_STVAL));
460 #if defined(SIMULATION_MODE)
461  // directly exit if in SIMULATION
462  extern void simulation_exit(int status);
463  simulation_exit(1);
464 #else
465  while (1);
466 #endif
467 }

Variable Documentation

◆ SystemExceptionHandlers

unsigned long SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM+1]
static

Store the exception handlers for each exception ID.

Note
  • This SystemExceptionHandlers are used to store all the handlers for all the exception codes Nuclei N/NX core provided.
  • Exception code 0 - 25, totally 26 exceptions are mapped to SystemExceptionHandlers[0:25]
  • Exception for NMI is also re-routed to exception handling(exception code 0xFFF) in startup code configuration, the handler itself is mapped to SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM]

Definition at line 278 of file system_Device.c.

◆ SystemExceptionHandlers_S

unsigned long SystemExceptionHandlers_S[MAX_SYSTEM_EXCEPTION_NUM]
static

Store the exception handlers for each exception ID in supervisor mode.

Note
  • This SystemExceptionHandlers_S are used to store all the handlers for all the exception codes Nuclei N/NX core provided.
  • Exception code 0 - 11, totally 12 exceptions are mapped to SystemExceptionHandlers_S[0:11]
  • The NMI (Non-maskable-interrupt) cannot be trapped to the supervisor-mode or user-mode for any configuration

Definition at line 289 of file system_Device.c.

SystemExceptionHandlers_S
static unsigned long SystemExceptionHandlers_S[MAX_SYSTEM_EXCEPTION_NUM]
Store the exception handlers for each exception ID in supervisor mode.
Definition: system_Device.c:289
system_default_exception_handler
static void system_default_exception_handler(unsigned long mcause, unsigned long sp)
System Default Exception Handler.
Definition: system_Device.c:307
Exception_DumpFrame
void Exception_DumpFrame(unsigned long sp, uint8_t mode)
Dump Exception Frame.
Definition: system_Device.c:351
EXC_HANDLER
void(* EXC_HANDLER)(unsigned long cause, unsigned long sp)
Exception Handler Function Typedef.
Definition: system_Device.c:297
MAX_SYSTEM_EXCEPTION_NUM
#define MAX_SYSTEM_EXCEPTION_NUM
Max exception handler number, don't include the NMI(0xFFF) one.
Definition: system_Device.c:269
PRV_S
#define PRV_S
Definition: riscv_encoding.h:317
PRV_M
#define PRV_M
Definition: riscv_encoding.h:319
__get_hart_id
__STATIC_FORCEINLINE unsigned long __get_hart_id(void)
Get hart id of current cluster.
Definition: core_feature_base.h:796
CSR_SEPC
#define CSR_SEPC
Definition: riscv_encoding.h:514
CSR_MTVAL
#define CSR_MTVAL
Definition: riscv_encoding.h:582
system_default_exception_handler_s
static void system_default_exception_handler_s(unsigned long scause, unsigned long sp)
Supervisor mode system Default Exception Handler.
Definition: system_Device.c:452
SystemExceptionHandlers
static unsigned long SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM+1]
Store the exception handlers for each exception ID.
Definition: system_Device.c:278
CSR_SDCAUSE
#define CSR_SDCAUSE
Definition: riscv_encoding.h:966
__RV_CSR_READ
#define __RV_CSR_READ(csr)
CSR operation Macro for csrr instruction.
Definition: core_feature_base.h:481
CSR_STVAL
#define CSR_STVAL
Definition: riscv_encoding.h:516
CSR_MEPC
#define CSR_MEPC
Definition: riscv_encoding.h:580
CSR_MDCAUSE
#define CSR_MDCAUSE
Definition: riscv_encoding.h:917