portASM.S 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*
  29. * The FreeRTOS kernel's RISC-V port is split between the the code that is
  30. * common across all currently supported RISC-V chips (implementations of the
  31. * RISC-V ISA), and code which tailors the port to a specific RISC-V chip:
  32. *
  33. * + The code that is common to all RISC-V chips is implemented in
  34. * FreeRTOS\Source\portable\GCC\RISC-V\portASM.S. There is only one
  35. * portASM.S file because the same file is used no matter which RISC-V chip is
  36. * in use.
  37. *
  38. * + The code that tailors the kernel's RISC-V port to a specific RISC-V
  39. * chip is implemented in freertos_risc_v_chip_specific_extensions.h. There
  40. * is one freertos_risc_v_chip_specific_extensions.h that can be used with any
  41. * RISC-V chip that both includes a standard CLINT and does not add to the
  42. * base set of RISC-V registers. There are additional
  43. * freertos_risc_v_chip_specific_extensions.h files for RISC-V implementations
  44. * that do not include a standard CLINT or do add to the base set of RISC-V
  45. * registers.
  46. *
  47. * CARE MUST BE TAKEN TO INCLDUE THE CORRECT
  48. * freertos_risc_v_chip_specific_extensions.h HEADER FILE FOR THE CHIP
  49. * IN USE. To include the correct freertos_risc_v_chip_specific_extensions.h
  50. * header file ensure the path to the correct header file is in the assembler's
  51. * include path.
  52. *
  53. * This freertos_risc_v_chip_specific_extensions.h is for use on RISC-V chips
  54. * that include a standard CLINT and do not add to the base set of RISC-V
  55. * registers.
  56. *
  57. */
  58. #include "portContext.h"
  59. /* Check the freertos_risc_v_chip_specific_extensions.h and/or command line
  60. definitions. */
  61. #if defined( portasmHAS_CLINT ) && defined( portasmHAS_MTIME )
  62. #error The portasmHAS_CLINT constant has been deprecated. Please replace it with portasmHAS_MTIME. portasmHAS_CLINT and portasmHAS_MTIME cannot both be defined at once. See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  63. #endif
  64. #ifdef portasmHAS_CLINT
  65. #warning The portasmHAS_CLINT constant has been deprecated. Please replace it with portasmHAS_MTIME and portasmHAS_SIFIVE_CLINT. For now portasmHAS_MTIME and portasmHAS_SIFIVE_CLINT are derived from portasmHAS_CLINT. See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  66. #define portasmHAS_MTIME portasmHAS_CLINT
  67. #define portasmHAS_SIFIVE_CLINT portasmHAS_CLINT
  68. #endif
  69. #ifndef portasmHAS_MTIME
  70. #error freertos_risc_v_chip_specific_extensions.h must define portasmHAS_MTIME to either 1 (MTIME clock present) or 0 (MTIME clock not present). See https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  71. #endif
  72. #ifndef portasmHAS_SIFIVE_CLINT
  73. #define portasmHAS_SIFIVE_CLINT 0
  74. #endif
  75. .global xPortStartFirstTask
  76. .global pxPortInitialiseStack
  77. .global freertos_risc_v_trap_handler
  78. .global freertos_risc_v_exception_handler
  79. .global freertos_risc_v_interrupt_handler
  80. .global freertos_risc_v_mtimer_interrupt_handler
  81. .extern vTaskSwitchContext
  82. .extern xTaskIncrementTick
  83. .extern pullMachineTimerCompareRegister
  84. .extern pullNextTime
  85. .extern uxTimerIncrementsForOneTick /* size_t type so 32-bit on 32-bit core and 64-bits on 64-bit core. */
  86. .extern xTaskReturnAddress
  87. .weak freertos_risc_v_application_exception_handler
  88. .weak freertos_risc_v_application_interrupt_handler
  89. /*-----------------------------------------------------------*/
  90. .macro portUPDATE_MTIMER_COMPARE_REGISTER
  91. load_x a0, pullMachineTimerCompareRegister /* Load address of compare register into a0. */
  92. load_x a1, pullNextTime /* Load the address of ullNextTime into a1. */
  93. #if( __riscv_xlen == 32 )
  94. /* Update the 64-bit mtimer compare match value in two 32-bit writes. */
  95. li a4, -1
  96. lw a2, 0(a1) /* Load the low word of ullNextTime into a2. */
  97. lw a3, 4(a1) /* Load the high word of ullNextTime into a3. */
  98. sw a4, 0(a0) /* Low word no smaller than old value to start with - will be overwritten below. */
  99. sw a3, 4(a0) /* Store high word of ullNextTime into compare register. No smaller than new value. */
  100. sw a2, 0(a0) /* Store low word of ullNextTime into compare register. */
  101. lw t0, uxTimerIncrementsForOneTick /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */
  102. add a4, t0, a2 /* Add the low word of ullNextTime to the timer increments for one tick (assumes timer increment for one tick fits in 32-bits). */
  103. sltu t1, a4, a2 /* See if the sum of low words overflowed (what about the zero case?). */
  104. add t2, a3, t1 /* Add overflow to high word of ullNextTime. */
  105. sw a4, 0(a1) /* Store new low word of ullNextTime. */
  106. sw t2, 4(a1) /* Store new high word of ullNextTime. */
  107. #endif /* __riscv_xlen == 32 */
  108. #if( __riscv_xlen == 64 )
  109. /* Update the 64-bit mtimer compare match value. */
  110. ld t2, 0(a1) /* Load ullNextTime into t2. */
  111. sd t2, 0(a0) /* Store ullNextTime into compare register. */
  112. ld t0, uxTimerIncrementsForOneTick /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */
  113. add t4, t0, t2 /* Add ullNextTime to the timer increments for one tick. */
  114. sd t4, 0(a1) /* Store ullNextTime. */
  115. #endif /* __riscv_xlen == 64 */
  116. .endm
  117. /*-----------------------------------------------------------*/
  118. /*
  119. * Unlike other ports pxPortInitialiseStack() is written in assembly code as it
  120. * needs access to the portasmADDITIONAL_CONTEXT_SIZE constant. The prototype
  121. * for the function is as per the other ports:
  122. * StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters );
  123. *
  124. * As per the standard RISC-V ABI pxTopOfStack is passed in in a0, pxCode in
  125. * a1, and pvParameters in a2. The new top of stack is passed out in a0.
  126. *
  127. * RISC-V maps registers to ABI names as follows (X1 to X31 integer registers
  128. * for the 'I' profile, X1 to X15 for the 'E' profile, currently I assumed).
  129. *
  130. * Register ABI Name Description Saver
  131. * x0 zero Hard-wired zero -
  132. * x1 ra Return address Caller
  133. * x2 sp Stack pointer Callee
  134. * x3 gp Global pointer -
  135. * x4 tp Thread pointer -
  136. * x5-7 t0-2 Temporaries Caller
  137. * x8 s0/fp Saved register/Frame pointer Callee
  138. * x9 s1 Saved register Callee
  139. * x10-11 a0-1 Function Arguments/return values Caller
  140. * x12-17 a2-7 Function arguments Caller
  141. * x18-27 s2-11 Saved registers Callee
  142. * x28-31 t3-6 Temporaries Caller
  143. *
  144. * The RISC-V context is saved to FreeRTOS tasks in the following stack frame,
  145. * where the global and thread pointers are currently assumed to be constant so
  146. * are not saved:
  147. *
  148. * xCriticalNesting
  149. * x31
  150. * x30
  151. * x29
  152. * x28
  153. * x27
  154. * x26
  155. * x25
  156. * x24
  157. * x23
  158. * x22
  159. * x21
  160. * x20
  161. * x19
  162. * x18
  163. * x17
  164. * x16
  165. * x15
  166. * x14
  167. * x13
  168. * x12
  169. * x11
  170. * pvParameters
  171. * x9
  172. * x8
  173. * x7
  174. * x6
  175. * x5
  176. * portTASK_RETURN_ADDRESS
  177. * [FPU registers (when enabled/available) go here]
  178. * [VPU registers (when enabled/available) go here]
  179. * mstatus
  180. * [chip specific registers go here]
  181. * pxCode
  182. */
  183. pxPortInitialiseStack:
  184. addi a0, a0, -portWORD_SIZE /* Space for critical nesting count. */
  185. store_x x0, 0(a0) /* Critical nesting count starts at 0 for every task. */
  186. #ifdef __riscv_32e
  187. addi a0, a0, -(6 * portWORD_SIZE) /* Space for registers x10-x15. */
  188. #else
  189. addi a0, a0, -(22 * portWORD_SIZE) /* Space for registers x10-x31. */
  190. #endif
  191. store_x a2, 0(a0) /* Task parameters (pvParameters parameter) goes into register x10/a0 on the stack. */
  192. addi a0, a0, -(6 * portWORD_SIZE) /* Space for registers x5-x9 + taskReturnAddress (register x1). */
  193. load_x t0, xTaskReturnAddress
  194. store_x t0, 0(a0) /* Return address onto the stack. */
  195. csrr t0, mstatus /* Obtain current mstatus value. */
  196. andi t0, t0, ~0x8 /* Ensure interrupts are disabled when the stack is restored within an ISR. Required when a task is created after the scheduler has been started, otherwise interrupts would be disabled anyway. */
  197. addi t1, x0, 0x188 /* Generate the value 0x1880, which are the MPIE=1 and MPP=M_Mode in mstatus. */
  198. slli t1, t1, 4
  199. or t0, t0, t1 /* Set MPIE and MPP bits in mstatus value. */
  200. #if( configENABLE_FPU == 1 )
  201. /* Mark the FPU as clean in the mstatus value. */
  202. li t1, ~MSTATUS_FS_MASK
  203. and t0, t0, t1
  204. li t1, MSTATUS_FS_CLEAN
  205. or t0, t0, t1
  206. #endif
  207. #if( configENABLE_VPU == 1 )
  208. /* Mark the VPU as clean in the mstatus value. */
  209. li t1, ~MSTATUS_VS_MASK
  210. and t0, t0, t1
  211. li t1, MSTATUS_VS_CLEAN
  212. or t0, t0, t1
  213. #endif
  214. addi a0, a0, -portWORD_SIZE
  215. store_x t0, 0(a0) /* mstatus onto the stack. */
  216. addi t0, x0, portasmADDITIONAL_CONTEXT_SIZE /* The number of chip specific additional registers. */
  217. chip_specific_stack_frame: /* First add any chip specific registers to the stack frame being created. */
  218. beq t0, x0, 1f /* No more chip specific registers to save. */
  219. addi a0, a0, -portWORD_SIZE /* Make space for chip specific register. */
  220. store_x x0, 0(a0) /* Give the chip specific register an initial value of zero. */
  221. addi t0, t0, -1 /* Decrement the count of chip specific registers remaining. */
  222. j chip_specific_stack_frame /* Until no more chip specific registers. */
  223. 1:
  224. addi a0, a0, -portWORD_SIZE
  225. store_x a1, 0(a0) /* mret value (pxCode parameter) onto the stack. */
  226. ret
  227. /*-----------------------------------------------------------*/
  228. xPortStartFirstTask:
  229. load_x sp, pxCurrentTCB /* Load pxCurrentTCB. */
  230. load_x sp, 0( sp ) /* Read sp from first TCB member. */
  231. load_x x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */
  232. portasmRESTORE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
  233. load_x x5, 1 * portWORD_SIZE( sp ) /* Initial mstatus into x5 (t0). */
  234. addi x5, x5, 0x08 /* Set MIE bit so the first task starts with interrupts enabled - required as returns with ret not eret. */
  235. csrw mstatus, x5 /* Interrupts enabled from here! */
  236. load_x x7, 5 * portWORD_SIZE( sp ) /* t2 */
  237. load_x x8, 6 * portWORD_SIZE( sp ) /* s0/fp */
  238. load_x x9, 7 * portWORD_SIZE( sp ) /* s1 */
  239. load_x x10, 8 * portWORD_SIZE( sp ) /* a0 */
  240. load_x x11, 9 * portWORD_SIZE( sp ) /* a1 */
  241. load_x x12, 10 * portWORD_SIZE( sp ) /* a2 */
  242. load_x x13, 11 * portWORD_SIZE( sp ) /* a3 */
  243. load_x x14, 12 * portWORD_SIZE( sp ) /* a4 */
  244. load_x x15, 13 * portWORD_SIZE( sp ) /* a5 */
  245. #ifndef __riscv_32e
  246. load_x x16, 14 * portWORD_SIZE( sp ) /* a6 */
  247. load_x x17, 15 * portWORD_SIZE( sp ) /* a7 */
  248. load_x x18, 16 * portWORD_SIZE( sp ) /* s2 */
  249. load_x x19, 17 * portWORD_SIZE( sp ) /* s3 */
  250. load_x x20, 18 * portWORD_SIZE( sp ) /* s4 */
  251. load_x x21, 19 * portWORD_SIZE( sp ) /* s5 */
  252. load_x x22, 20 * portWORD_SIZE( sp ) /* s6 */
  253. load_x x23, 21 * portWORD_SIZE( sp ) /* s7 */
  254. load_x x24, 22 * portWORD_SIZE( sp ) /* s8 */
  255. load_x x25, 23 * portWORD_SIZE( sp ) /* s9 */
  256. load_x x26, 24 * portWORD_SIZE( sp ) /* s10 */
  257. load_x x27, 25 * portWORD_SIZE( sp ) /* s11 */
  258. load_x x28, 26 * portWORD_SIZE( sp ) /* t3 */
  259. load_x x29, 27 * portWORD_SIZE( sp ) /* t4 */
  260. load_x x30, 28 * portWORD_SIZE( sp ) /* t5 */
  261. load_x x31, 29 * portWORD_SIZE( sp ) /* t6 */
  262. #endif
  263. load_x x5, portCRITICAL_NESTING_OFFSET * portWORD_SIZE( sp ) /* Obtain xCriticalNesting value for this task from task's stack. */
  264. load_x x6, pxCriticalNesting /* Load the address of xCriticalNesting into x6. */
  265. store_x x5, 0( x6 ) /* Restore the critical nesting value for this task. */
  266. load_x x5, 3 * portWORD_SIZE( sp ) /* Initial x5 (t0) value. */
  267. load_x x6, 4 * portWORD_SIZE( sp ) /* Initial x6 (t1) value. */
  268. addi sp, sp, portCONTEXT_SIZE
  269. ret
  270. /*-----------------------------------------------------------*/
  271. freertos_risc_v_application_exception_handler:
  272. csrr t0, mcause /* For viewing in the debugger only. */
  273. csrr t1, mepc /* For viewing in the debugger only */
  274. csrr t2, mstatus /* For viewing in the debugger only */
  275. j .
  276. /*-----------------------------------------------------------*/
  277. freertos_risc_v_application_interrupt_handler:
  278. csrr t0, mcause /* For viewing in the debugger only. */
  279. csrr t1, mepc /* For viewing in the debugger only */
  280. csrr t2, mstatus /* For viewing in the debugger only */
  281. j .
  282. /*-----------------------------------------------------------*/
  283. .section .text.freertos_risc_v_exception_handler
  284. freertos_risc_v_exception_handler:
  285. portcontextSAVE_EXCEPTION_CONTEXT
  286. /* a0 now contains mcause. */
  287. li t0, 11 /* 11 == environment call. */
  288. bne a0, t0, other_exception /* Not an M environment call, so some other exception. */
  289. call vTaskSwitchContext
  290. portcontextRESTORE_CONTEXT
  291. other_exception:
  292. call freertos_risc_v_application_exception_handler
  293. portcontextRESTORE_CONTEXT
  294. /*-----------------------------------------------------------*/
  295. .section .text.freertos_risc_v_interrupt_handler
  296. freertos_risc_v_interrupt_handler:
  297. portcontextSAVE_INTERRUPT_CONTEXT
  298. call freertos_risc_v_application_interrupt_handler
  299. portcontextRESTORE_CONTEXT
  300. /*-----------------------------------------------------------*/
  301. .section .text.freertos_risc_v_mtimer_interrupt_handler
  302. freertos_risc_v_mtimer_interrupt_handler:
  303. portcontextSAVE_INTERRUPT_CONTEXT
  304. portUPDATE_MTIMER_COMPARE_REGISTER
  305. call xTaskIncrementTick
  306. beqz a0, exit_without_context_switch /* Don't switch context if incrementing tick didn't unblock a task. */
  307. call vTaskSwitchContext
  308. exit_without_context_switch:
  309. portcontextRESTORE_CONTEXT
  310. /*-----------------------------------------------------------*/
  311. .section .text.freertos_risc_v_trap_handler
  312. .align 8
  313. freertos_risc_v_trap_handler:
  314. portcontextSAVE_CONTEXT_INTERNAL
  315. csrr a0, mcause
  316. csrr a1, mepc
  317. bge a0, x0, synchronous_exception
  318. asynchronous_interrupt:
  319. store_x a1, 0( sp ) /* Asynchronous interrupt so save unmodified exception return address. */
  320. load_x sp, xISRStackTop /* Switch to ISR stack. */
  321. j handle_interrupt
  322. synchronous_exception:
  323. addi a1, a1, 4 /* Synchronous so update exception return address to the instruction after the instruction that generated the exeption. */
  324. store_x a1, 0( sp ) /* Save updated exception return address. */
  325. load_x sp, xISRStackTop /* Switch to ISR stack. */
  326. j handle_exception
  327. handle_interrupt:
  328. #if( portasmHAS_MTIME != 0 )
  329. test_if_mtimer: /* If there is a CLINT then the mtimer is used to generate the tick interrupt. */
  330. addi t0, x0, 1
  331. slli t0, t0, __riscv_xlen - 1 /* LSB is already set, shift into MSB. Shift 31 on 32-bit or 63 on 64-bit cores. */
  332. addi t1, t0, 7 /* 0x8000[]0007 == machine timer interrupt. */
  333. bne a0, t1, application_interrupt_handler
  334. portUPDATE_MTIMER_COMPARE_REGISTER
  335. call xTaskIncrementTick
  336. beqz a0, processed_source /* Don't switch context if incrementing tick didn't unblock a task. */
  337. call vTaskSwitchContext
  338. j processed_source
  339. #endif /* portasmHAS_MTIME */
  340. application_interrupt_handler:
  341. call freertos_risc_v_application_interrupt_handler
  342. j processed_source
  343. handle_exception:
  344. /* a0 contains mcause. */
  345. li t0, 11 /* 11 == environment call. */
  346. bne a0, t0, application_exception_handler /* Not an M environment call, so some other exception. */
  347. call vTaskSwitchContext
  348. j processed_source
  349. application_exception_handler:
  350. call freertos_risc_v_application_exception_handler
  351. j processed_source /* No other exceptions handled yet. */
  352. processed_source:
  353. portcontextRESTORE_CONTEXT
  354. /*-----------------------------------------------------------*/