| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- /*
- * FreeRTOS Kernel Port For Nuclei RISC-V Processor
- * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * http://www.FreeRTOS.org
- * http://aws.amazon.com/freertos
- *
- * 1 tab == 4 spaces!
- */
- #include "riscv_encoding.h"
- #ifndef __riscv_32e
- #define portRegNum 32
- #else
- #define portRegNum 14
- #endif
- #define portCONTEXT_SIZE ( portRegNum * REGBYTES )
- // If you want to use SMP freertos
- // configNUMBER_OF_CORES must be defined to the number of cores in your system.
- // If you define configNUMBER_OF_CORES using -D option, you must make sure that
- // asm option must contains this macro defined
- #ifndef configNUMBER_OF_CORES
- #define configNUMBER_OF_CORES 1
- #endif
- 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
- PUBLIC prvPortStartFirstTask, eclic_msip_handler
- EXTERN core_exception_handler
- EXTERN xPortTaskSwitch
- #if ( configNUMBER_OF_CORES == 1 )
- EXTERN pxCurrentTCB
- #else
- EXTERN pxCurrentTCBs
- #endif
- 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
- /* Start the first task. This also clears the bit that indicates the FPU is
- in use in case the FPU was used before the scheduler was started - which
- would otherwise result in the unnecessary leaving of space in the stack
- for lazy saving of FPU registers. */
- ALIGN 3
- prvPortStartFirstTask:
- /* Setup Interrupt Stack using
- The stack that was used by main()
- before the scheduler is started is
- no longer required after the scheduler is started.
- Interrupt stack pointer is stored in CSR_MSCRATCH */
- #if defined(SMP_CPU_CNT) && (SMP_CPU_CNT > 1)
- /* Set correct sp for each cpu
- * each stack size is __STACK_SIZE
- * defined in linker script */
- EXTERN __STACK_SIZE
- lui t1, %hi(__STACK_SIZE)
- addi t1, t1, %lo(__STACK_SIZE)
- la t0, CSTACK$$Limit
- csrr a0, mhartid
- andi a0, a0, 0xFF
- li a1, 0
- _per_cpu_init_sp_cont:
- beq a0, a1, _per_cpu_init_sp_fin
- sub t0, t0, t1
- addi a1, a1, 1
- j _per_cpu_init_sp_cont
- _per_cpu_init_sp_fin:
- #else
- la t0, CSTACK$$Limit
- #endif
- csrw CSR_MSCRATCH, t0
- #if ( configNUMBER_OF_CORES == 1 )
- LOAD t0, pxCurrentTCB /* Load pxCurrentTCB. */
- #else
- la t0, pxCurrentTCBs /* Load pxCurrentTCBs[core] */
- csrr t1, CSR_MHARTID
- slli t1, t1, LOG_REGBYTES
- add t0, t0, t1
- LOAD t0, 0(t0)
- #endif
- LOAD sp, 0x0(t0) /* Read sp from first TCB member */
- /* Pop PC from stack and set MEPC */
- LOAD t0, 0 * REGBYTES(sp)
- csrw CSR_MEPC, t0
- /* Pop mstatus from stack and set it */
- LOAD t0, (portRegNum - 1) * REGBYTES(sp)
- csrw CSR_MSTATUS, t0
- /* Interrupt still disable here */
- /* Restore Registers from Stack */
- LOAD x1, 1 * REGBYTES(sp) /* RA */
- LOAD x5, 2 * REGBYTES(sp)
- LOAD x6, 3 * REGBYTES(sp)
- LOAD x7, 4 * REGBYTES(sp)
- LOAD x8, 5 * REGBYTES(sp)
- LOAD x9, 6 * REGBYTES(sp)
- LOAD x10, 7 * REGBYTES(sp)
- LOAD x11, 8 * REGBYTES(sp)
- LOAD x12, 9 * REGBYTES(sp)
- LOAD x13, 10 * REGBYTES(sp)
- LOAD x14, 11 * REGBYTES(sp)
- LOAD x15, 12 * REGBYTES(sp)
- #ifndef __riscv_32e
- LOAD x16, 13 * REGBYTES(sp)
- LOAD x17, 14 * REGBYTES(sp)
- LOAD x18, 15 * REGBYTES(sp)
- LOAD x19, 16 * REGBYTES(sp)
- LOAD x20, 17 * REGBYTES(sp)
- LOAD x21, 18 * REGBYTES(sp)
- LOAD x22, 19 * REGBYTES(sp)
- LOAD x23, 20 * REGBYTES(sp)
- LOAD x24, 21 * REGBYTES(sp)
- LOAD x25, 22 * REGBYTES(sp)
- LOAD x26, 23 * REGBYTES(sp)
- LOAD x27, 24 * REGBYTES(sp)
- LOAD x28, 25 * REGBYTES(sp)
- LOAD x29, 26 * REGBYTES(sp)
- LOAD x30, 27 * REGBYTES(sp)
- LOAD x31, 28 * REGBYTES(sp)
- #endif
- addi sp, sp, portCONTEXT_SIZE
- mret
- ALIGN 2
- eclic_msip_handler:
- addi sp, sp, -portCONTEXT_SIZE
- STORE x1, 1 * REGBYTES(sp) /* RA */
- STORE x5, 2 * REGBYTES(sp)
- STORE x6, 3 * REGBYTES(sp)
- STORE x7, 4 * REGBYTES(sp)
- STORE x8, 5 * REGBYTES(sp)
- STORE x9, 6 * REGBYTES(sp)
- STORE x10, 7 * REGBYTES(sp)
- STORE x11, 8 * REGBYTES(sp)
- STORE x12, 9 * REGBYTES(sp)
- STORE x13, 10 * REGBYTES(sp)
- STORE x14, 11 * REGBYTES(sp)
- STORE x15, 12 * REGBYTES(sp)
- #ifndef __riscv_32e
- STORE x16, 13 * REGBYTES(sp)
- STORE x17, 14 * REGBYTES(sp)
- STORE x18, 15 * REGBYTES(sp)
- STORE x19, 16 * REGBYTES(sp)
- STORE x20, 17 * REGBYTES(sp)
- STORE x21, 18 * REGBYTES(sp)
- STORE x22, 19 * REGBYTES(sp)
- STORE x23, 20 * REGBYTES(sp)
- STORE x24, 21 * REGBYTES(sp)
- STORE x25, 22 * REGBYTES(sp)
- STORE x26, 23 * REGBYTES(sp)
- STORE x27, 24 * REGBYTES(sp)
- STORE x28, 25 * REGBYTES(sp)
- STORE x29, 26 * REGBYTES(sp)
- STORE x30, 27 * REGBYTES(sp)
- STORE x31, 28 * REGBYTES(sp)
- #endif
- /* Push mstatus to stack */
- csrr t0, CSR_MSTATUS
- STORE t0, (portRegNum - 1) * REGBYTES(sp)
- /* Push additional registers */
- /* Store sp to task stack */
- #if ( configNUMBER_OF_CORES == 1 )
- LOAD t0, pxCurrentTCB /* Load pxCurrentTCB. */
- #else
- la t0, pxCurrentTCBs /* Load pxCurrentTCBs[core] */
- csrr t1, CSR_MHARTID
- slli t1, t1, LOG_REGBYTES
- add t0, t0, t1
- LOAD t0, 0(t0)
- #endif
- STORE sp, 0(t0)
- csrr t0, CSR_MEPC
- STORE t0, 0(sp)
- jal xPortTaskSwitch
- /* Switch task context */
- #if ( configNUMBER_OF_CORES == 1 )
- LOAD t0, pxCurrentTCB /* Load pxCurrentTCB. */
- #else
- la t0, pxCurrentTCBs /* Load pxCurrentTCBs[core] */
- csrr t1, CSR_MHARTID
- slli t1, t1, LOG_REGBYTES
- add t0, t0, t1
- LOAD t0, 0(t0)
- #endif
- LOAD sp, 0x0(t0) /* Read sp from first TCB member */
- /* Pop PC from stack and set MEPC */
- LOAD t0, 0 * REGBYTES(sp)
- csrw CSR_MEPC, t0
- /* Pop additional registers */
- /* Pop mstatus from stack and set it */
- LOAD t0, (portRegNum - 1) * REGBYTES(sp)
- csrw CSR_MSTATUS, t0
- /* Interrupt still disable here */
- /* Restore Registers from Stack */
- LOAD x1, 1 * REGBYTES(sp) /* RA */
- LOAD x5, 2 * REGBYTES(sp)
- LOAD x6, 3 * REGBYTES(sp)
- LOAD x7, 4 * REGBYTES(sp)
- LOAD x8, 5 * REGBYTES(sp)
- LOAD x9, 6 * REGBYTES(sp)
- LOAD x10, 7 * REGBYTES(sp)
- LOAD x11, 8 * REGBYTES(sp)
- LOAD x12, 9 * REGBYTES(sp)
- LOAD x13, 10 * REGBYTES(sp)
- LOAD x14, 11 * REGBYTES(sp)
- LOAD x15, 12 * REGBYTES(sp)
- #ifndef __riscv_32e
- LOAD x16, 13 * REGBYTES(sp)
- LOAD x17, 14 * REGBYTES(sp)
- LOAD x18, 15 * REGBYTES(sp)
- LOAD x19, 16 * REGBYTES(sp)
- LOAD x20, 17 * REGBYTES(sp)
- LOAD x21, 18 * REGBYTES(sp)
- LOAD x22, 19 * REGBYTES(sp)
- LOAD x23, 20 * REGBYTES(sp)
- LOAD x24, 21 * REGBYTES(sp)
- LOAD x25, 22 * REGBYTES(sp)
- LOAD x26, 23 * REGBYTES(sp)
- LOAD x27, 24 * REGBYTES(sp)
- LOAD x28, 25 * REGBYTES(sp)
- LOAD x29, 26 * REGBYTES(sp)
- LOAD x30, 27 * REGBYTES(sp)
- LOAD x31, 28 * REGBYTES(sp)
- #endif
- addi sp, sp, portCONTEXT_SIZE
- mret
- default_intexc_handler:
- Undef_Handler:
- j Undef_Handler
- END
|