NMSIS-Core  Version 1.4.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   20
 Max exception handler number, don't include the NMI(0xFFF) one. More...
 
#define SYSTEM_CORE_INTNUM   16
 

Typedefs

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

Functions

static void system_mmode_extirq_handler (unsigned long exccode, unsigned long sp)
 M-Mode external interrupt handler common entry for plic interrupt mode. More...
 
static void core_interrupt_handler (unsigned long exccode, unsigned long sp)
 M-Mode Common Interrupt handler entry when in clint/plic mode. More...
 
uint32_t core_exception_handler (unsigned long mcause, unsigned long sp)
 M-Mode Common NMI/Exception/Interrupt handler entry. More...
 
static void system_default_interrupt_handler_s (unsigned long scause, unsigned long sp)
 s-mode System Default Interrupt Handler for CLINT/PLIC Interrupt Mode More...
 
static void system_smode_extirq_handler (unsigned long exccode, unsigned long sp)
 S-Mode external interrupt handler common entry for plic interrupt mode. More...
 
static void core_interrupt_handler_s (unsigned long exccode, unsigned long sp)
 S-Mode Common Interrupt handler entry when in clint/plic mode. More...
 
void Exception_DumpFrame (unsigned long sp, uint8_t mode)
 Dump Exception Frame. More...
 
static void system_default_interrupt_handler (unsigned long mcause, unsigned long sp)
 M-Mode System Default Interrupt Handler for CLINT/PLIC Interrupt Mode. More...
 
void Interrupt_Register_CoreIRQ (uint32_t irqn, unsigned long int_handler)
 Register a m-mode core interrupt handler for core interrupt number. More...
 
unsigned long Interrupt_Get_CoreIRQ (uint32_t irqn)
 Get a m-mode core interrupt handler for core interrupt number. More...
 
void Interrupt_Register_ExtIRQ (uint32_t irqn, unsigned long int_handler)
 Register a m-mode external interrupt handler for plic external interrupt number. More...
 
unsigned long Interrupt_Get_ExtIRQ (uint32_t irqn)
 Get a m-mode external interrupt handler for external interrupt number. More...
 
void Exception_Register_EXC (uint32_t EXCn, unsigned long exc_handler)
 Register a m-mode exception handler for exception code EXCn. More...
 
unsigned long Exception_Get_EXC (uint32_t EXCn)
 Get current m-mode exception handler for exception code EXCn. More...
 
static void Exception_Init (void)
 Initialize all the default core exception handlers. More...
 
uint32_t core_exception_handler_s (unsigned long scause, unsigned long sp)
 common Exception handler entry of supervisor mode 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...
 
void Interrupt_Register_CoreIRQ_S (uint32_t irqn, unsigned long int_handler)
 Register an s-mode core interrupt handler for core interrupt number. More...
 
unsigned long Interrupt_Get_CoreIRQ_S (uint32_t irqn)
 Get a s-mode core interrupt handler for core interrupt number. More...
 
void Interrupt_Register_ExtIRQ_S (uint32_t irqn, unsigned long int_handler)
 Register an s-mode external interrupt handler for plic external interrupt number. More...
 
unsigned long Interrupt_Get_ExtIRQ_S (uint32_t irqn)
 Get an s-mode external interrupt handler for external interrupt number. More...
 
static void system_default_exception_handler (unsigned long mcause, unsigned long sp)
 M-Mode System Default Exception Handler. More...
 
static void system_default_exception_handler_s (unsigned long scause, unsigned long sp)
 Supervisor mode system Default Exception Handler. More...
 

Variables

static unsigned long SystemExceptionHandlers [MAX_SYSTEM_EXCEPTION_NUM+1]
 Store the exception handlers for each exception ID. More...
 
static unsigned long SystemMExtInterruptHandlers [__PLIC_INTNUM]
 
static unsigned long SystemCoreInterruptHandlers [SYSTEM_CORE_INTNUM]
 
static INT_HANDLER system_core_interrupt_handler = NULL
 
static unsigned long SystemExceptionHandlers_S [MAX_SYSTEM_EXCEPTION_NUM]
 Store the exception handlers for each exception ID in supervisor mode. More...
 
static INT_HANDLER system_core_interrupt_handler_s = NULL
 
static unsigned long SystemCoreInterruptHandlers_S [SYSTEM_CORE_INTNUM]
 
static unsigned long SystemSExtInterruptHandlers [__PLIC_INTNUM]
 

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   20

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

Definition at line 243 of file system_Device.c.

◆ SYSTEM_CORE_INTNUM

#define SYSTEM_CORE_INTNUM   16

Definition at line 258 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 239 of file system_Device.c.

◆ INT_HANDLER

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

Definition at line 240 of file system_Device.c.

Function Documentation

◆ core_exception_handler()

uint32_t core_exception_handler ( unsigned long  mcause,
unsigned long  sp 
)

M-Mode Common NMI/Exception/Interrupt 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 and interrupt if not in eclic mode. 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.
  • If you are in eclic interrupt mode, please use ECLIC_Register_IRQ to register both internal and external interrupt
  • If you are not in eclic interrupt mode, please use please use Interrupt_Register_CoreIRQ to register internal interrupt and use Interrupt_Register_ExtIRQ to register external interrupt

Definition at line 406 of file system_Device.c.

407 {
408 #if defined(CODESIZE) && (CODESIZE == 1)
409  // TODO when CODESIZE macro is defined
410  // Exception_xxx APIs will not be used, all the m-mode exception handlers
411  // will goto this function, and you can handle it here by yourself
412  while (1);
413 #else
414 
415  unsigned long exccode = (mcause & MCAUSE_CAUSE);
416  EXC_HANDLER exc_handler;
417 
418  if (mcause & MCAUSE_INTR) {
419  if (system_core_interrupt_handler != NULL) {
420  system_core_interrupt_handler(exccode, sp);
421  }
422  } else {
423  if (exccode < MAX_SYSTEM_EXCEPTION_NUM) {
424  exc_handler = (EXC_HANDLER)SystemExceptionHandlers[exccode];
425  } else if (exccode == NMI_EXCn) {
427  } else {
429  }
430  if (exc_handler != NULL) {
431  exc_handler(mcause, sp);
432  }
433  }
434 
435  return 0;
436 #endif
437 }
#define MCAUSE_INTR
#define MCAUSE_CAUSE
#define MAX_SYSTEM_EXCEPTION_NUM
Max exception handler number, don't include the NMI(0xFFF) one.
static INT_HANDLER system_core_interrupt_handler
static void system_default_exception_handler(unsigned long mcause, unsigned long sp)
M-Mode System Default Exception Handler.
static unsigned long SystemExceptionHandlers[MAX_SYSTEM_EXCEPTION_NUM+1]
Store the exception handlers for each exception ID.
void(* EXC_HANDLER)(unsigned long cause, unsigned long sp)
Exception Handler Function Typedef.

References MAX_SYSTEM_EXCEPTION_NUM, MCAUSE_CAUSE, MCAUSE_INTR, system_core_interrupt_handler, system_default_exception_handler(), and SystemExceptionHandlers.

◆ 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 704 of file system_Device.c.

705 {
706 #if defined(CODESIZE) && (CODESIZE == 1)
707  // TODO when CODESIZE macro is defined
708  // Exception_xxx_S APIs will not be used, all the s-mode exception handlers
709  // will goto this function, and you can handle it here by yourself
710  while(1);
711 #else
712  unsigned long exccode = (scause & SCAUSE_CAUSE);
713  EXC_HANDLER exc_handler;
714  if (scause & MCAUSE_INTR) {
715  if (system_core_interrupt_handler_s != NULL) {
716  system_core_interrupt_handler_s(exccode, sp);
717  }
718  } else {
719  if (exccode < MAX_SYSTEM_EXCEPTION_NUM) {
720  exc_handler = (EXC_HANDLER)SystemExceptionHandlers_S[exccode];
721  } else {
723  }
724  if (exc_handler != NULL) {
725  exc_handler(scause, sp);
726  }
727  }
728  return 0;
729 #endif
730 }
#define SCAUSE_CAUSE
static unsigned long SystemExceptionHandlers_S[MAX_SYSTEM_EXCEPTION_NUM]
Store the exception handlers for each exception ID in supervisor mode.
static INT_HANDLER system_core_interrupt_handler_s
static void system_default_exception_handler_s(unsigned long scause, unsigned long sp)
Supervisor mode system Default Exception Handler.

References MAX_SYSTEM_EXCEPTION_NUM, MCAUSE_INTR, SCAUSE_CAUSE, system_core_interrupt_handler_s, system_default_exception_handler_s(), and SystemExceptionHandlers_S.

◆ core_interrupt_handler()

static void core_interrupt_handler ( unsigned long  exccode,
unsigned long  sp 
)
static

M-Mode Common Interrupt handler entry when in clint/plic mode.

This function provided a command entry for interrupt in clint/plic mode

Parameters
[in]exccodeException Code
[in]spstack pointer
Remarks

Definition at line 381 of file system_Device.c.

382 {
383  INT_HANDLER int_handler = NULL;
384  int_handler = (INT_HANDLER)(SystemCoreInterruptHandlers[exccode]);
385  if (int_handler != NULL) {
386  int_handler(exccode, sp);
387  }
388 }
static unsigned long SystemCoreInterruptHandlers[SYSTEM_CORE_INTNUM]
void(* INT_HANDLER)(unsigned long cause, unsigned long sp)

References SystemCoreInterruptHandlers.

Referenced by CLINT_Interrupt_Init().

◆ core_interrupt_handler_s()

static void core_interrupt_handler_s ( unsigned long  exccode,
unsigned long  sp 
)
static

S-Mode Common Interrupt handler entry when in clint/plic mode.

This function provided a command entry for interrupt in clint/plic mode

Parameters
[in]exccodeException Code
[in]spstack pointer
Remarks

Definition at line 658 of file system_Device.c.

659 {
660 #if defined(__SMODE_PRESENT) && (__SMODE_PRESENT == 1)
661  INT_HANDLER int_handler = NULL;
662  int_handler = (INT_HANDLER)(SystemCoreInterruptHandlers_S[exccode]);
663  if (int_handler != NULL) {
664  int_handler(exccode, sp);
665  }
666 #endif
667 }
static unsigned long SystemCoreInterruptHandlers_S[SYSTEM_CORE_INTNUM]

References SystemCoreInterruptHandlers_S.

Referenced by CLINT_Interrupt_Init().

◆ 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 294 of file system_Device.c.

295 {
296 #if defined(CODESIZE) && (CODESIZE == 1)
297 
298 #else
299  EXC_Frame_Type *exc_frame = (EXC_Frame_Type *)sp;
300 
301 #ifndef __riscv_32e
302  NSDK_DEBUG("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" \
303  "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" \
304  "cause: 0x%lx, epc: 0x%lx\n", exc_frame->ra, exc_frame->tp, exc_frame->t0, \
305  exc_frame->t1, exc_frame->t2, exc_frame->t3, exc_frame->t4, exc_frame->t5, exc_frame->t6, \
306  exc_frame->a0, exc_frame->a1, exc_frame->a2, exc_frame->a3, exc_frame->a4, exc_frame->a5, \
307  exc_frame->a6, exc_frame->a7, exc_frame->cause, exc_frame->epc);
308 #else
309  NSDK_DEBUG("ra: 0x%lx, tp: 0x%lx, t0: 0x%lx, t1: 0x%lx, t2: 0x%lx\n" \
310  "a0: 0x%lx, a1: 0x%lx, a2: 0x%lx, a3: 0x%lx, a4: 0x%lx, a5: 0x%lx\n" \
311  "cause: 0x%lx, epc: 0x%lx\n", exc_frame->ra, exc_frame->tp, exc_frame->t0, \
312  exc_frame->t1, exc_frame->t2, exc_frame->a0, exc_frame->a1, exc_frame->a2, exc_frame->a3, \
313  exc_frame->a4, exc_frame->a5, exc_frame->cause, exc_frame->epc);
314 #endif
315 
316  if (PRV_M == mode) {
317  /* msubm is exclusive to machine mode */
318  NSDK_DEBUG("msubm: 0x%lx\n", exc_frame->msubm);
319  }
320 #endif
321 }
#define PRV_M

References PRV_M.

Referenced by system_default_exception_handler(), and system_default_exception_handler_s().

◆ Exception_Get_EXC()

unsigned long Exception_Get_EXC ( uint32_t  EXCn)

Get current m-mode 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 559 of file system_Device.c.

560 {
561 #if defined(CODESIZE) && (CODESIZE == 1)
562  return 0;
563 #else
564  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
565  return SystemExceptionHandlers[EXCn];
566  } else if (EXCn == NMI_EXCn) {
568  } else {
569  return 0;
570  }
571 #endif
572 }

References MAX_SYSTEM_EXCEPTION_NUM, and SystemExceptionHandlers.

◆ 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 758 of file system_Device.c.

759 {
760 #if defined(CODESIZE) && (CODESIZE == 1)
761  return 0;
762 #else
763  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
764  return SystemExceptionHandlers_S[EXCn];
765  } else {
766  return 0;
767  }
768 #endif
769 }

References MAX_SYSTEM_EXCEPTION_NUM, and SystemExceptionHandlers_S.

◆ 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 582 of file system_Device.c.

583 {
584 #if defined(CODESIZE) && (CODESIZE == 1)
585  // TODO when CODESIZE macro is defined
586  // the exception handler table for m/s mode will not be initialized
587  // since all the exception handlers will not be classified, and just
588  // goto core_exception_handler or core_exception_handler_s for m/s exception
589 #else
590  for (int i = 0; i < MAX_SYSTEM_EXCEPTION_NUM; i++) {
592 #if (defined(__SMODE_PRESENT) && (__SMODE_PRESENT == 1))
594 #endif
595  }
597 #endif
598 }

References MAX_SYSTEM_EXCEPTION_NUM, system_default_exception_handler(), system_default_exception_handler_s(), SystemExceptionHandlers, and SystemExceptionHandlers_S.

Referenced by _premain_init().

◆ Exception_Register_EXC()

void Exception_Register_EXC ( uint32_t  EXCn,
unsigned long  exc_handler 
)

Register a m-mode 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 538 of file system_Device.c.

539 {
540 #if defined(CODESIZE) && (CODESIZE == 1)
541 
542 #else
543  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
544  SystemExceptionHandlers[EXCn] = exc_handler;
545  } else if (EXCn == NMI_EXCn) {
547  }
548 #endif
549 }

References MAX_SYSTEM_EXCEPTION_NUM, and SystemExceptionHandlers.

◆ 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 741 of file system_Device.c.

742 {
743 #if defined(CODESIZE) && (CODESIZE == 1)
744 #else
745  if (EXCn < MAX_SYSTEM_EXCEPTION_NUM) {
746  SystemExceptionHandlers_S[EXCn] = exc_handler;
747  }
748 #endif
749 }

References MAX_SYSTEM_EXCEPTION_NUM, and SystemExceptionHandlers_S.

◆ Interrupt_Get_CoreIRQ()

unsigned long Interrupt_Get_CoreIRQ ( uint32_t  irqn)

Get a m-mode core interrupt handler for core interrupt number.

Parameters
irqnSee IRQn
Returns
The core interrupt handler for this interrupt code irqn, only used in non-eclic mode.
Remarks
You can only use it when you are not in ECLIC interrupt mode.

Definition at line 485 of file system_Device.c.

486 {
487  if ((irqn < SYSTEM_CORE_INTNUM) && (irqn >= 0)) {
488  return SystemCoreInterruptHandlers[irqn];
489  }
490  return 0;
491 }
#define SYSTEM_CORE_INTNUM

References SYSTEM_CORE_INTNUM, and SystemCoreInterruptHandlers.

◆ Interrupt_Get_CoreIRQ_S()

unsigned long Interrupt_Get_CoreIRQ_S ( uint32_t  irqn)

Get a s-mode core interrupt handler for core interrupt number.

Parameters
irqnSee IRQn
Returns
The core interrupt handler for this interrupt code irqn, only used in non-eclic mode.
Remarks
You can only use it when you are not in ECLIC interrupt mode.

Definition at line 795 of file system_Device.c.

796 {
797  if ((irqn < SYSTEM_CORE_INTNUM) && (irqn >= 0)) {
798  return SystemCoreInterruptHandlers_S[irqn];
799  }
800  return 0;
801 }

References SYSTEM_CORE_INTNUM, and SystemCoreInterruptHandlers_S.

◆ Interrupt_Get_ExtIRQ()

unsigned long Interrupt_Get_ExtIRQ ( uint32_t  irqn)

Get a m-mode external interrupt handler for external interrupt number.

Parameters
irqnSee IRQn
Returns
The external interrupt handler for this interrupt code irqn
Remarks
You can only use it when you are in PLIC interrupt mode.

Definition at line 519 of file system_Device.c.

520 {
521 #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
522  if ((irqn < __PLIC_INTNUM) && (irqn >= 0)) {
523  return SystemMExtInterruptHandlers[irqn];
524  }
525 #endif
526  return 0;
527 }
static unsigned long SystemMExtInterruptHandlers[__PLIC_INTNUM]

References SystemMExtInterruptHandlers.

◆ Interrupt_Get_ExtIRQ_S()

unsigned long Interrupt_Get_ExtIRQ_S ( uint32_t  irqn)

Get an s-mode external interrupt handler for external interrupt number.

Parameters
irqnSee IRQn
Returns
The external interrupt handler for this interrupt code irqn
Remarks
You can only use it when you are in PLIC interrupt mode.

Definition at line 831 of file system_Device.c.

832 {
833 #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
834 #if defined(__SMODE_PRESENT) && (__SMODE_PRESENT == 1)
835  if ((irqn < __PLIC_INTNUM) && (irqn >= 0)) {
836  return SystemSExtInterruptHandlers[irqn];
837  }
838 #endif
839 #endif
840  return 0;
841 }
static unsigned long SystemSExtInterruptHandlers[__PLIC_INTNUM]

References SystemSExtInterruptHandlers.

◆ Interrupt_Register_CoreIRQ()

void Interrupt_Register_CoreIRQ ( uint32_t  irqn,
unsigned long  int_handler 
)

Register a m-mode core interrupt handler for core interrupt number.

  • For irqn <= SYSTEM_CORE_INTNUM, it will be registered into SystemCoreInterruptHandlers[irqn-1], only used in non-eclic mode.
    Parameters
    irqnSee IRQn
    int_handlerThe core interrupt handler for this interrupt code irqn
    Remarks
    You can only use it when you are not in ECLIC interrupt mode.

Definition at line 470 of file system_Device.c.

471 {
472  if ((irqn < SYSTEM_CORE_INTNUM) && (irqn >= 0)) {
473  SystemCoreInterruptHandlers[irqn] = int_handler;
474  }
475 }

References SYSTEM_CORE_INTNUM, and SystemCoreInterruptHandlers.

Referenced by Core_Register_IRQ().

◆ Interrupt_Register_CoreIRQ_S()

void Interrupt_Register_CoreIRQ_S ( uint32_t  irqn,
unsigned long  int_handler 
)

Register an s-mode core interrupt handler for core interrupt number.

  • For irqn <= SYSTEM_CORE_INTNUM, it will be registered into SystemCoreInterruptHandlers[irqn-1], only used in non-eclic mode.
    Parameters
    irqnSee IRQn
    int_handlerThe core interrupt handler for this interrupt code irqn
    Remarks
    You can only use it when you are not in ECLIC interrupt mode.

Definition at line 780 of file system_Device.c.

781 {
782  if ((irqn < SYSTEM_CORE_INTNUM) && (irqn >= 0)) {
783  SystemCoreInterruptHandlers_S[irqn] = int_handler;
784  }
785 }

References SYSTEM_CORE_INTNUM, and SystemCoreInterruptHandlers_S.

Referenced by Core_Register_IRQ_S().

◆ Interrupt_Register_ExtIRQ()

void Interrupt_Register_ExtIRQ ( uint32_t  irqn,
unsigned long  int_handler 
)

Register a m-mode external interrupt handler for plic external interrupt number.

  • For irqn <= __PLIC_INTNUM, it will be registered into SystemMExtInterruptHandlers[irqn-1].
    Parameters
    irqnSee IRQn
    int_handlerThe external interrupt handler for this interrupt code irqn
    Remarks
    You can only use it when you are in PLIC interrupt mode.

Definition at line 502 of file system_Device.c.

503 {
504 #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
505  if ((irqn < __PLIC_INTNUM) && (irqn >= 0)) {
506  SystemMExtInterruptHandlers[irqn] = int_handler;
507  }
508 #endif
509 }

References SystemMExtInterruptHandlers.

Referenced by PLIC_Register_IRQ().

◆ Interrupt_Register_ExtIRQ_S()

void Interrupt_Register_ExtIRQ_S ( uint32_t  irqn,
unsigned long  int_handler 
)

Register an s-mode external interrupt handler for plic external interrupt number.

  • For irqn <= __PLIC_INTNUM, it will be registered into SystemSExtInterruptHandlers[irqn-1].
    Parameters
    irqnSee IRQn
    int_handlerThe external interrupt handler for this interrupt code irqn
    Remarks
    You can only use it when you are in PLIC interrupt mode.

Definition at line 812 of file system_Device.c.

813 {
814 #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
815 #if defined(__SMODE_PRESENT) && (__SMODE_PRESENT == 1)
816  if ((irqn < __PLIC_INTNUM) && (irqn >= 0)) {
817  SystemSExtInterruptHandlers[irqn] = int_handler;
818  }
819 #endif
820 #endif
821 }

References SystemSExtInterruptHandlers.

Referenced by PLIC_Register_IRQ_S().

◆ system_default_exception_handler()

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

M-Mode 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 331 of file system_Device.c.

332 {
333 #if defined(CODESIZE) && (CODESIZE == 1)
334 
335 #else
336  NSDK_DEBUG("MCAUSE : 0x%lx\r\n", mcause);
337  NSDK_DEBUG("MDCAUSE: 0x%lx\r\n", __RV_CSR_READ(CSR_MDCAUSE));
338  NSDK_DEBUG("MEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_MEPC));
339  NSDK_DEBUG("MTVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_MTVAL));
340  NSDK_DEBUG("HARTID : %u\r\n", (unsigned int)__get_hart_id());
342 #if defined(SIMULATION_MODE)
343  // directly exit if in SIMULATION
344  extern void simulation_exit(int status);
345  simulation_exit(1);
346 #else
347  while (1);
348 #endif
349 #endif
350 }
#define __RV_CSR_READ(csr)
CSR operation Macro for csrr instruction.
__STATIC_FORCEINLINE unsigned long __get_hart_id(void)
Get hart id of current cluster.
#define CSR_MEPC
#define CSR_MTVAL
#define CSR_MDCAUSE
void Exception_DumpFrame(unsigned long sp, uint8_t mode)
Dump Exception Frame.

References __get_hart_id(), __RV_CSR_READ, CSR_MDCAUSE, CSR_MEPC, CSR_MTVAL, Exception_DumpFrame(), and PRV_M.

Referenced by core_exception_handler(), and Exception_Init().

◆ 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 609 of file system_Device.c.

610 {
611 #if defined(CODESIZE) && (CODESIZE == 1)
612 #else
613  /* TODO: Uncomment this if you have implement NSDK_DEBUG function */
614  NSDK_DEBUG("SCAUSE : 0x%lx\r\n", scause);
615  NSDK_DEBUG("SDCAUSE: 0x%lx\r\n", __RV_CSR_READ(CSR_SDCAUSE));
616  NSDK_DEBUG("SEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_SEPC));
617  NSDK_DEBUG("STVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_STVAL));
619 #if defined(SIMULATION_MODE)
620  // directly exit if in SIMULATION
621  extern void simulation_exit(int status);
622  simulation_exit(1);
623 #else
624  while (1);
625 #endif
626 #endif
627 }
#define PRV_S
#define CSR_SEPC
#define CSR_STVAL
#define CSR_SDCAUSE

References __RV_CSR_READ, CSR_SDCAUSE, CSR_SEPC, CSR_STVAL, Exception_DumpFrame(), and PRV_S.

Referenced by core_exception_handler_s(), and Exception_Init().

◆ system_default_interrupt_handler()

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

M-Mode System Default Interrupt Handler for CLINT/PLIC Interrupt Mode.

This function provided a default interrupt handling code for all interrupt ids.

Definition at line 357 of file system_Device.c.

358 {
359 #if defined(CODESIZE) && (CODESIZE == 1)
360 
361 #else
362  NSDK_DEBUG("Trap in Interrupt\r\n");
363  NSDK_DEBUG("MCAUSE: 0x%lx\r\n", mcause);
364  NSDK_DEBUG("MEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_MEPC));
365  NSDK_DEBUG("MTVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_MTVAL));
366 #endif
367 }

References __RV_CSR_READ, CSR_MEPC, and CSR_MTVAL.

Referenced by CLINT_Interrupt_Init(), and PLIC_Interrupt_Init().

◆ system_default_interrupt_handler_s()

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

s-mode System Default Interrupt Handler for CLINT/PLIC Interrupt Mode

This function provided a default interrupt handling code for all interrupt ids.

Definition at line 634 of file system_Device.c.

635 {
636 #if defined(CODESIZE) && (CODESIZE == 1)
637 
638 #else
639  NSDK_DEBUG("Trap in S-Mode Interrupt\r\n");
640  NSDK_DEBUG("SCAUSE: 0x%lx\r\n", scause);
641  NSDK_DEBUG("SEPC : 0x%lx\r\n", __RV_CSR_READ(CSR_SEPC));
642  NSDK_DEBUG("STVAL : 0x%lx\r\n", __RV_CSR_READ(CSR_STVAL));
643 #endif
644 }

References __RV_CSR_READ, CSR_SEPC, and CSR_STVAL.

Referenced by CLINT_Interrupt_Init(), and PLIC_Interrupt_Init().

◆ system_mmode_extirq_handler()

static void system_mmode_extirq_handler ( unsigned long  exccode,
unsigned long  sp 
)
static

M-Mode external interrupt handler common entry for plic interrupt mode.

This function provide common entry for m-mode external interrupt for plic interrupt mode.

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

Definition at line 446 of file system_Device.c.

447 {
448 #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
449  uint32_t irqn = PLIC_ClaimInterrupt();
450  INT_HANDLER int_handler = NULL;
451  if (irqn < __PLIC_INTNUM) {
452  int_handler = (INT_HANDLER)(SystemMExtInterruptHandlers[irqn]);
453  if (int_handler != NULL) {
454  int_handler(exccode, sp);
455  }
456  }
458 #endif
459 }
#define PLIC_CompleteInterrupt(source)
#define PLIC_ClaimInterrupt()

References PLIC_ClaimInterrupt, PLIC_CompleteInterrupt, and SystemMExtInterruptHandlers.

Referenced by PLIC_Interrupt_Init().

◆ system_smode_extirq_handler()

static void system_smode_extirq_handler ( unsigned long  exccode,
unsigned long  sp 
)
static

S-Mode external interrupt handler common entry for plic interrupt mode.

This function provide common entry for s-mode external interrupt for plic interrupt mode.

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

Definition at line 676 of file system_Device.c.

677 {
678 #if defined(__PLIC_PRESENT) && (__PLIC_PRESENT == 1)
679  uint32_t irqn = PLIC_ClaimInterrupt_S();
680  INT_HANDLER int_handler = NULL;
681  if (irqn < __PLIC_INTNUM) {
682  int_handler = (INT_HANDLER)(SystemSExtInterruptHandlers[irqn]);
683  if (int_handler != NULL) {
684  int_handler(exccode, sp);
685  }
686  }
688 #endif
689 }
#define PLIC_CompleteInterrupt_S(source)
#define PLIC_ClaimInterrupt_S()

References PLIC_ClaimInterrupt_S, PLIC_CompleteInterrupt_S, and SystemSExtInterruptHandlers.

Referenced by PLIC_Interrupt_Init().

Variable Documentation

◆ system_core_interrupt_handler

INT_HANDLER system_core_interrupt_handler = NULL
static

Definition at line 265 of file system_Device.c.

Referenced by CLINT_Interrupt_Init(), and core_exception_handler().

◆ system_core_interrupt_handler_s

INT_HANDLER system_core_interrupt_handler_s = NULL
static

Definition at line 280 of file system_Device.c.

Referenced by CLINT_Interrupt_Init(), and core_exception_handler_s().

◆ SystemCoreInterruptHandlers

unsigned long SystemCoreInterruptHandlers[SYSTEM_CORE_INTNUM]
static

◆ SystemCoreInterruptHandlers_S

unsigned long SystemCoreInterruptHandlers_S[SYSTEM_CORE_INTNUM]
static

◆ 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 - 19, totally 20 exceptions are mapped to SystemExceptionHandlers[0:19]
  • 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 252 of file system_Device.c.

Referenced by core_exception_handler(), Exception_Get_EXC(), Exception_Init(), and Exception_Register_EXC().

◆ 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 - 19, totally 20 exceptions are mapped to SystemExceptionHandlers_S[0:19]

Definition at line 275 of file system_Device.c.

Referenced by core_exception_handler_s(), Exception_Get_EXC_S(), Exception_Init(), and Exception_Register_EXC_S().

◆ SystemMExtInterruptHandlers

unsigned long SystemMExtInterruptHandlers[__PLIC_INTNUM]
static

◆ SystemSExtInterruptHandlers

unsigned long SystemSExtInterruptHandlers[__PLIC_INTNUM]
static