portASM.s 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  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-RV32\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. #if __riscv_xlen == 64
  59. #define portWORD_SIZE 8
  60. #define store_x sd
  61. #define load_x ld
  62. #elif __riscv_xlen == 32
  63. #define store_x sw
  64. #define load_x lw
  65. #define portWORD_SIZE 4
  66. #else
  67. #error Assembler did not define __riscv_xlen
  68. #endif
  69. #include "freertos_risc_v_chip_specific_extensions.h"
  70. /* Check the freertos_risc_v_chip_specific_extensions.h and/or command line
  71. definitions. */
  72. #if defined( portasmHAS_CLINT ) && defined( portasmHAS_MTIME )
  73. #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
  74. #endif
  75. #ifdef portasmHAS_CLINT
  76. #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
  77. #define portasmHAS_MTIME portasmHAS_CLINT
  78. #define portasmHAS_SIFIVE_CLINT portasmHAS_CLINT
  79. #endif
  80. #ifndef portasmHAS_MTIME
  81. #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
  82. #endif
  83. #ifndef portasmHANDLE_INTERRUPT
  84. #error portasmHANDLE_INTERRUPT must be defined to the function to be called to handle external/peripheral interrupts. portasmHANDLE_INTERRUPT can be defined on the assembler command line or in the appropriate freertos_risc_v_chip_specific_extensions.h header file. https://www.FreeRTOS.org/Using-FreeRTOS-on-RISC-V.html
  85. #endif
  86. #ifndef portasmHAS_SIFIVE_CLINT
  87. #define portasmHAS_SIFIVE_CLINT 0
  88. #endif
  89. /* CSR definitions. */
  90. #define CSR_MSTATUS 0x300
  91. #define CSR_MTVEC 0x305
  92. #define CSR_MEPC 0x341
  93. #define CSR_MCAUSE 0x342
  94. /* Only the standard core registers are stored by default. Any additional
  95. registers must be saved by the portasmSAVE_ADDITIONAL_REGISTERS and
  96. portasmRESTORE_ADDITIONAL_REGISTERS macros - which can be defined in a chip
  97. specific version of freertos_risc_v_chip_specific_extensions.h. See the notes
  98. at the top of this file. */
  99. #define portCONTEXT_SIZE ( 30 * portWORD_SIZE )
  100. PUBLIC xPortStartFirstTask
  101. PUBLIC freertos_risc_v_trap_handler
  102. PUBLIC pxPortInitialiseStack
  103. EXTERN pxCurrentTCB
  104. EXTERN ulPortTrapHandler
  105. EXTERN vTaskSwitchContext
  106. EXTERN xTaskIncrementTick
  107. EXTERN Timer_IRQHandler
  108. EXTERN pullMachineTimerCompareRegister
  109. EXTERN pullNextTime
  110. EXTERN uxTimerIncrementsForOneTick /* size_t type so 32-bit on 32-bit core and 64-bits on 64-bit core. */
  111. EXTERN xISRStackTop
  112. EXTERN portasmHANDLE_INTERRUPT
  113. /*-----------------------------------------------------------*/
  114. SECTION `.text`:CODE:NOROOT(2)
  115. CODE
  116. freertos_risc_v_trap_handler:
  117. addi sp, sp, -portCONTEXT_SIZE
  118. store_x x1, 1 * portWORD_SIZE( sp )
  119. store_x x5, 2 * portWORD_SIZE( sp )
  120. store_x x6, 3 * portWORD_SIZE( sp )
  121. store_x x7, 4 * portWORD_SIZE( sp )
  122. store_x x8, 5 * portWORD_SIZE( sp )
  123. store_x x9, 6 * portWORD_SIZE( sp )
  124. store_x x10, 7 * portWORD_SIZE( sp )
  125. store_x x11, 8 * portWORD_SIZE( sp )
  126. store_x x12, 9 * portWORD_SIZE( sp )
  127. store_x x13, 10 * portWORD_SIZE( sp )
  128. store_x x14, 11 * portWORD_SIZE( sp )
  129. store_x x15, 12 * portWORD_SIZE( sp )
  130. store_x x16, 13 * portWORD_SIZE( sp )
  131. store_x x17, 14 * portWORD_SIZE( sp )
  132. store_x x18, 15 * portWORD_SIZE( sp )
  133. store_x x19, 16 * portWORD_SIZE( sp )
  134. store_x x20, 17 * portWORD_SIZE( sp )
  135. store_x x21, 18 * portWORD_SIZE( sp )
  136. store_x x22, 19 * portWORD_SIZE( sp )
  137. store_x x23, 20 * portWORD_SIZE( sp )
  138. store_x x24, 21 * portWORD_SIZE( sp )
  139. store_x x25, 22 * portWORD_SIZE( sp )
  140. store_x x26, 23 * portWORD_SIZE( sp )
  141. store_x x27, 24 * portWORD_SIZE( sp )
  142. store_x x28, 25 * portWORD_SIZE( sp )
  143. store_x x29, 26 * portWORD_SIZE( sp )
  144. store_x x30, 27 * portWORD_SIZE( sp )
  145. store_x x31, 28 * portWORD_SIZE( sp )
  146. csrr t0, CSR_MSTATUS /* Required for MPIE bit. */
  147. store_x t0, 29 * portWORD_SIZE( sp )
  148. portasmSAVE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to save any registers unique to the RISC-V implementation. */
  149. load_x t0, pxCurrentTCB /* Load pxCurrentTCB. */
  150. store_x sp, 0( t0 ) /* Write sp to first TCB member. */
  151. csrr a0, CSR_MCAUSE
  152. csrr a1, CSR_MEPC
  153. test_if_asynchronous:
  154. srli a2, a0, __riscv_xlen - 1 /* MSB of mcause is 1 if handing an asynchronous interrupt - shift to LSB to clear other bits. */
  155. beq a2, x0, handle_synchronous /* Branch past interrupt handing if not asynchronous. */
  156. store_x a1, 0( sp ) /* Asynch so save unmodified exception return address. */
  157. handle_asynchronous:
  158. #if( portasmHAS_MTIME != 0 )
  159. test_if_mtimer: /* If there is a CLINT then the mtimer is used to generate the tick interrupt. */
  160. addi t0, x0, 1
  161. slli t0, t0, __riscv_xlen - 1 /* LSB is already set, shift into MSB. Shift 31 on 32-bit or 63 on 64-bit cores. */
  162. addi t1, t0, 7 /* 0x8000[]0007 == machine timer interrupt. */
  163. bne a0, t1, test_if_external_interrupt
  164. load_x t0, pullMachineTimerCompareRegister /* Load address of compare register into t0. */
  165. load_x t1, pullNextTime /* Load the address of ullNextTime into t1. */
  166. #if( __riscv_xlen == 32 )
  167. /* Update the 64-bit mtimer compare match value in two 32-bit writes. */
  168. li t4, -1
  169. lw t2, 0(t1) /* Load the low word of ullNextTime into t2. */
  170. lw t3, 4(t1) /* Load the high word of ullNextTime into t3. */
  171. sw t4, 0(t0) /* Low word no smaller than old value to start with - will be overwritten below. */
  172. sw t3, 4(t0) /* Store high word of ullNextTime into compare register. No smaller than new value. */
  173. sw t2, 0(t0) /* Store low word of ullNextTime into compare register. */
  174. lw t0, uxTimerIncrementsForOneTick /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */
  175. add t4, t0, t2 /* Add the low word of ullNextTime to the timer increments for one tick (assumes timer increment for one tick fits in 32-bits). */
  176. sltu t5, t4, t2 /* See if the sum of low words overflowed (what about the zero case?). */
  177. add t6, t3, t5 /* Add overflow to high word of ullNextTime. */
  178. sw t4, 0(t1) /* Store new low word of ullNextTime. */
  179. sw t6, 4(t1) /* Store new high word of ullNextTime. */
  180. #endif /* __riscv_xlen == 32 */
  181. #if( __riscv_xlen == 64 )
  182. /* Update the 64-bit mtimer compare match value. */
  183. ld t2, 0(t1) /* Load ullNextTime into t2. */
  184. sd t2, 0(t0) /* Store ullNextTime into compare register. */
  185. ld t0, uxTimerIncrementsForOneTick /* Load the value of ullTimerIncrementForOneTick into t0 (could this be optimized by storing in an array next to pullNextTime?). */
  186. add t4, t0, t2 /* Add ullNextTime to the timer increments for one tick. */
  187. sd t4, 0(t1) /* Store ullNextTime. */
  188. #endif /* __riscv_xlen == 64 */
  189. load_x sp, xISRStackTop /* Switch to ISR stack before function call. */
  190. jal xTaskIncrementTick
  191. beqz a0, processed_source /* Don't switch context if incrementing tick didn't unblock a task. */
  192. jal vTaskSwitchContext
  193. j processed_source
  194. test_if_external_interrupt: /* If there is a CLINT and the mtimer interrupt is not pending then check to see if an external interrupt is pending. */
  195. addi t1, t1, 4 /* 0x80000007 + 4 = 0x8000000b == Machine external interrupt. */
  196. bne a0, t1, as_yet_unhandled /* Something as yet unhandled. */
  197. #endif /* portasmHAS_MTIME */
  198. load_x sp, xISRStackTop /* Switch to ISR stack before function call. */
  199. jal portasmHANDLE_INTERRUPT /* Jump to the interrupt handler if there is no CLINT or if there is a CLINT and it has been determined that an external interrupt is pending. */
  200. j processed_source
  201. handle_synchronous:
  202. addi a1, a1, 4 /* Synchronous so updated exception return address to the instruction after the instruction that generated the exeption. */
  203. store_x a1, 0( sp ) /* Save updated exception return address. */
  204. test_if_environment_call:
  205. li t0, 11 /* 11 == environment call. */
  206. bne a0, t0, is_exception /* Not an M environment call, so some other exception. */
  207. load_x sp, xISRStackTop /* Switch to ISR stack before function call. */
  208. jal vTaskSwitchContext
  209. j processed_source
  210. is_exception:
  211. csrr t0, CSR_MCAUSE /* For viewing in the debugger only. */
  212. csrr t1, CSR_MEPC /* For viewing in the debugger only */
  213. csrr t2, CSR_MSTATUS
  214. j is_exception /* No other exceptions handled yet. */
  215. as_yet_unhandled:
  216. csrr t0, mcause /* For viewing in the debugger only. */
  217. j as_yet_unhandled
  218. processed_source:
  219. load_x t1, pxCurrentTCB /* Load pxCurrentTCB. */
  220. load_x sp, 0( t1 ) /* Read sp from first TCB member. */
  221. /* Load mret with the address of the next instruction in the task to run next. */
  222. load_x t0, 0( sp )
  223. csrw CSR_MEPC, t0
  224. portasmRESTORE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
  225. /* Load mstatus with the interrupt enable bits used by the task. */
  226. load_x t0, 29 * portWORD_SIZE( sp )
  227. csrw CSR_MSTATUS, t0 /* Required for MPIE bit. */
  228. load_x x1, 1 * portWORD_SIZE( sp )
  229. load_x x5, 2 * portWORD_SIZE( sp ) /* t0 */
  230. load_x x6, 3 * portWORD_SIZE( sp ) /* t1 */
  231. load_x x7, 4 * portWORD_SIZE( sp ) /* t2 */
  232. load_x x8, 5 * portWORD_SIZE( sp ) /* s0/fp */
  233. load_x x9, 6 * portWORD_SIZE( sp ) /* s1 */
  234. load_x x10, 7 * portWORD_SIZE( sp ) /* a0 */
  235. load_x x11, 8 * portWORD_SIZE( sp ) /* a1 */
  236. load_x x12, 9 * portWORD_SIZE( sp ) /* a2 */
  237. load_x x13, 10 * portWORD_SIZE( sp ) /* a3 */
  238. load_x x14, 11 * portWORD_SIZE( sp ) /* a4 */
  239. load_x x15, 12 * portWORD_SIZE( sp ) /* a5 */
  240. load_x x16, 13 * portWORD_SIZE( sp ) /* a6 */
  241. load_x x17, 14 * portWORD_SIZE( sp ) /* a7 */
  242. load_x x18, 15 * portWORD_SIZE( sp ) /* s2 */
  243. load_x x19, 16 * portWORD_SIZE( sp ) /* s3 */
  244. load_x x20, 17 * portWORD_SIZE( sp ) /* s4 */
  245. load_x x21, 18 * portWORD_SIZE( sp ) /* s5 */
  246. load_x x22, 19 * portWORD_SIZE( sp ) /* s6 */
  247. load_x x23, 20 * portWORD_SIZE( sp ) /* s7 */
  248. load_x x24, 21 * portWORD_SIZE( sp ) /* s8 */
  249. load_x x25, 22 * portWORD_SIZE( sp ) /* s9 */
  250. load_x x26, 23 * portWORD_SIZE( sp ) /* s10 */
  251. load_x x27, 24 * portWORD_SIZE( sp ) /* s11 */
  252. load_x x28, 25 * portWORD_SIZE( sp ) /* t3 */
  253. load_x x29, 26 * portWORD_SIZE( sp ) /* t4 */
  254. load_x x30, 27 * portWORD_SIZE( sp ) /* t5 */
  255. load_x x31, 28 * portWORD_SIZE( sp ) /* t6 */
  256. addi sp, sp, portCONTEXT_SIZE
  257. mret
  258. /*-----------------------------------------------------------*/
  259. xPortStartFirstTask:
  260. #if( portasmHAS_SIFIVE_CLINT != 0 )
  261. /* If there is a clint then interrupts can branch directly to the FreeRTOS
  262. trap handler. Otherwise the interrupt controller will need to be configured
  263. outside of this file. */
  264. la t0, freertos_risc_v_trap_handler
  265. csrw CSR_MTVEC, t0
  266. #endif /* portasmHAS_CLILNT */
  267. load_x sp, pxCurrentTCB /* Load pxCurrentTCB. */
  268. load_x sp, 0( sp ) /* Read sp from first TCB member. */
  269. load_x x1, 0( sp ) /* Note for starting the scheduler the exception return address is used as the function return address. */
  270. portasmRESTORE_ADDITIONAL_REGISTERS /* Defined in freertos_risc_v_chip_specific_extensions.h to restore any registers unique to the RISC-V implementation. */
  271. load_x x6, 3 * portWORD_SIZE( sp ) /* t1 */
  272. load_x x7, 4 * portWORD_SIZE( sp ) /* t2 */
  273. load_x x8, 5 * portWORD_SIZE( sp ) /* s0/fp */
  274. load_x x9, 6 * portWORD_SIZE( sp ) /* s1 */
  275. load_x x10, 7 * portWORD_SIZE( sp ) /* a0 */
  276. load_x x11, 8 * portWORD_SIZE( sp ) /* a1 */
  277. load_x x12, 9 * portWORD_SIZE( sp ) /* a2 */
  278. load_x x13, 10 * portWORD_SIZE( sp ) /* a3 */
  279. load_x x14, 11 * portWORD_SIZE( sp ) /* a4 */
  280. load_x x15, 12 * portWORD_SIZE( sp ) /* a5 */
  281. load_x x16, 13 * portWORD_SIZE( sp ) /* a6 */
  282. load_x x17, 14 * portWORD_SIZE( sp ) /* a7 */
  283. load_x x18, 15 * portWORD_SIZE( sp ) /* s2 */
  284. load_x x19, 16 * portWORD_SIZE( sp ) /* s3 */
  285. load_x x20, 17 * portWORD_SIZE( sp ) /* s4 */
  286. load_x x21, 18 * portWORD_SIZE( sp ) /* s5 */
  287. load_x x22, 19 * portWORD_SIZE( sp ) /* s6 */
  288. load_x x23, 20 * portWORD_SIZE( sp ) /* s7 */
  289. load_x x24, 21 * portWORD_SIZE( sp ) /* s8 */
  290. load_x x25, 22 * portWORD_SIZE( sp ) /* s9 */
  291. load_x x26, 23 * portWORD_SIZE( sp ) /* s10 */
  292. load_x x27, 24 * portWORD_SIZE( sp ) /* s11 */
  293. load_x x28, 25 * portWORD_SIZE( sp ) /* t3 */
  294. load_x x29, 26 * portWORD_SIZE( sp ) /* t4 */
  295. load_x x30, 27 * portWORD_SIZE( sp ) /* t5 */
  296. load_x x31, 28 * portWORD_SIZE( sp ) /* t6 */
  297. load_x x5, 29 * portWORD_SIZE( sp ) /* Initial mstatus into x5 (t0) */
  298. addi x5, x5, 0x08 /* Set MIE bit so the first task starts with interrupts enabled - required as returns with ret not eret. */
  299. csrrw x0, CSR_MSTATUS, x5 /* Interrupts enabled from here! */
  300. load_x x5, 2 * portWORD_SIZE( sp ) /* Initial x5 (t0) value. */
  301. addi sp, sp, portCONTEXT_SIZE
  302. ret
  303. /*-----------------------------------------------------------*/
  304. /*
  305. * Unlike other ports pxPortInitialiseStack() is written in assembly code as it
  306. * needs access to the portasmADDITIONAL_CONTEXT_SIZE constant. The prototype
  307. * for the function is as per the other ports:
  308. * StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters );
  309. *
  310. * As per the standard RISC-V ABI pxTopcOfStack is passed in in a0, pxCode in
  311. * a1, and pvParameters in a2. The new top of stack is passed out in a0.
  312. *
  313. * RISC-V maps registers to ABI names as follows (X1 to X31 integer registers
  314. * for the 'I' profile, X1 to X15 for the 'E' profile, currently I assumed).
  315. *
  316. * Register ABI Name Description Saver
  317. * x0 zero Hard-wired zero -
  318. * x1 ra Return address Caller
  319. * x2 sp Stack pointer Callee
  320. * x3 gp Global pointer -
  321. * x4 tp Thread pointer -
  322. * x5-7 t0-2 Temporaries Caller
  323. * x8 s0/fp Saved register/Frame pointer Callee
  324. * x9 s1 Saved register Callee
  325. * x10-11 a0-1 Function Arguments/return values Caller
  326. * x12-17 a2-7 Function arguments Caller
  327. * x18-27 s2-11 Saved registers Callee
  328. * x28-31 t3-6 Temporaries Caller
  329. *
  330. * The RISC-V context is saved t FreeRTOS tasks in the following stack frame,
  331. * where the global and thread pointers are currently assumed to be constant so
  332. * are not saved:
  333. *
  334. * mstatus
  335. * x31
  336. * x30
  337. * x29
  338. * x28
  339. * x27
  340. * x26
  341. * x25
  342. * x24
  343. * x23
  344. * x22
  345. * x21
  346. * x20
  347. * x19
  348. * x18
  349. * x17
  350. * x16
  351. * x15
  352. * x14
  353. * x13
  354. * x12
  355. * x11
  356. * pvParameters
  357. * x9
  358. * x8
  359. * x7
  360. * x6
  361. * x5
  362. * portTASK_RETURN_ADDRESS
  363. * [chip specific registers go here]
  364. * pxCode
  365. */
  366. pxPortInitialiseStack:
  367. csrr t0, CSR_MSTATUS /* Obtain current mstatus value. */
  368. andi t0, t0, ~0x8 /* Ensure interrupts are disabled when the stack is restored within an ISR. Required when a task is created after the schedulre has been started, otherwise interrupts would be disabled anyway. */
  369. addi t1, x0, 0x188 /* Generate the value 0x1880, which are the MPIE and MPP bits to set in mstatus. */
  370. slli t1, t1, 4
  371. or t0, t0, t1 /* Set MPIE and MPP bits in mstatus value. */
  372. addi a0, a0, -portWORD_SIZE
  373. store_x t0, 0(a0) /* mstatus onto the stack. */
  374. addi a0, a0, -(22 * portWORD_SIZE) /* Space for registers x11-x31. */
  375. store_x a2, 0(a0) /* Task parameters (pvParameters parameter) goes into register X10/a0 on the stack. */
  376. addi a0, a0, -(6 * portWORD_SIZE) /* Space for registers x5-x9. */
  377. store_x x0, 0(a0) /* Return address onto the stack, could be portTASK_RETURN_ADDRESS */
  378. addi t0, x0, portasmADDITIONAL_CONTEXT_SIZE /* The number of chip specific additional registers. */
  379. chip_specific_stack_frame: /* First add any chip specific registers to the stack frame being created. */
  380. beq t0, x0, no_more_regs /* No more chip specific registers to save. */
  381. addi a0, a0, -portWORD_SIZE /* Make space for chip specific register. */
  382. store_x x0, 0(a0) /* Give the chip specific register an initial value of zero. */
  383. addi t0, t0, -1 /* Decrement the count of chip specific registers remaining. */
  384. j chip_specific_stack_frame /* Until no more chip specific registers. */
  385. no_more_regs:
  386. addi a0, a0, -portWORD_SIZE
  387. store_x a1, 0(a0) /* mret value (pxCode parameter) onto the stack. */
  388. ret
  389. /*-----------------------------------------------------------*/