/* * Copyright (c) 2019-Present Nuclei Limited. All rights reserved. * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2023/09/25 Huaqi First Nuclei RISC-V porting implementation For IAR CC */ #include "riscv_encoding.h" DISABLE_MIE MACRO csrci CSR_MSTATUS, MSTATUS_MIE ENDM SAVE_CONTEXT MACRO #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2) #else csrrw sp, CSR_MSCRATCHCSWL, sp /* Allocate stack space for context saving */ #ifndef __riscv_32e addi sp, sp, -20*REGBYTES #else addi sp, sp, -14*REGBYTES #endif /* __riscv_32e */ STORE x1, 0*REGBYTES(sp) STORE x4, 1*REGBYTES(sp) STORE x5, 2*REGBYTES(sp) STORE x6, 3*REGBYTES(sp) STORE x7, 4*REGBYTES(sp) STORE x10, 5*REGBYTES(sp) STORE x11, 6*REGBYTES(sp) STORE x12, 7*REGBYTES(sp) STORE x13, 8*REGBYTES(sp) STORE x14, 9*REGBYTES(sp) STORE x15, 10*REGBYTES(sp) #ifndef __riscv_32e STORE x16, 14*REGBYTES(sp) STORE x17, 15*REGBYTES(sp) STORE x28, 16*REGBYTES(sp) STORE x29, 17*REGBYTES(sp) STORE x30, 18*REGBYTES(sp) STORE x31, 19*REGBYTES(sp) #endif /* __riscv_32e */ #endif ENDM RESTORE_CONTEXT MACRO #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2) #else LOAD x1, 0*REGBYTES(sp) LOAD x4, 1*REGBYTES(sp) LOAD x5, 2*REGBYTES(sp) LOAD x6, 3*REGBYTES(sp) LOAD x7, 4*REGBYTES(sp) LOAD x10, 5*REGBYTES(sp) LOAD x11, 6*REGBYTES(sp) LOAD x12, 7*REGBYTES(sp) LOAD x13, 8*REGBYTES(sp) LOAD x14, 9*REGBYTES(sp) LOAD x15, 10*REGBYTES(sp) #ifndef __riscv_32e LOAD x16, 14*REGBYTES(sp) LOAD x17, 15*REGBYTES(sp) LOAD x28, 16*REGBYTES(sp) LOAD x29, 17*REGBYTES(sp) LOAD x30, 18*REGBYTES(sp) LOAD x31, 19*REGBYTES(sp) /* De-allocate the stack space */ addi sp, sp, 20*REGBYTES #else /* De-allocate the stack space */ addi sp, sp, 14*REGBYTES #endif /* __riscv_32e */ csrrw sp, CSR_MSCRATCHCSWL, sp #endif ENDM SAVE_CSR_CONTEXT MACRO #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2) #else /* Store CSR mcause to stack using pushmcause */ csrrwi x0, CSR_PUSHMCAUSE, 11 /* Store CSR mepc to stack using pushmepc */ csrrwi x0, CSR_PUSHMEPC, 12 /* Store CSR msub to stack using pushmsub */ csrrwi x0, CSR_PUSHMSUBM, 13 #endif ENDM RESTORE_CSR_CONTEXT MACRO #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2) #else LOAD x5, 13*REGBYTES(sp) csrw CSR_MSUBM, x5 LOAD x5, 12*REGBYTES(sp) csrw CSR_MEPC, x5 LOAD x5, 11*REGBYTES(sp) csrw CSR_MCAUSE, x5 #endif ENDM PUBLIC exc_entry, irq_entry, default_intexc_handler PUBLIC Undef_Handler EXTERN core_exception_handler EXTERN CSTACK$$Limit SECTION `.text`:CODE:NOROOT(2) CODE ALIGN 6 exc_entry: /* Save the caller saving registers (context) */ SAVE_CONTEXT /* Save the necessary CSR registers */ SAVE_CSR_CONTEXT /* * Set the exception handler function arguments * argument 1: mcause value * argument 2: current stack point(SP) value */ csrr a0, mcause mv a1, sp /* * TODO: Call the exception handler function * By default, the function template is provided in * system_Device.c, you can adjust it as you want */ call core_exception_handler /* Restore the necessary CSR registers */ RESTORE_CSR_CONTEXT /* Restore the caller saving registers (context) */ RESTORE_CONTEXT /* Return to regular code */ #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2) csrrwi x0, CSR_POPXRET, 0 #else mret #endif ALIGN 2 irq_entry: /* Save the caller saving registers (context) */ SAVE_CONTEXT /* Save the necessary CSR registers */ SAVE_CSR_CONTEXT /* This special CSR read/write operation, which is actually * claim the CLIC to find its pending highest ID, if the ID * is not 0, then automatically enable the mstatus.MIE, and * jump to its vector-entry-label, and update the link register */ csrrw ra, CSR_JALMNXTI, ra /* Critical section with interrupts disabled */ DISABLE_MIE /* Restore the necessary CSR registers */ RESTORE_CSR_CONTEXT /* Restore the caller saving registers (context) */ RESTORE_CONTEXT /* Return to regular code */ #if defined(ECLIC_HW_CTX_AUTO) && defined(CFG_HAS_ECLICV2) csrrwi x0, CSR_POPXRET, 0 #else mret #endif default_intexc_handler: Undef_Handler: j Undef_Handler END