context.S 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "riscv_encoding.h"
  2. #ifndef __riscv_32e
  3. #define portRegNum 32
  4. #else
  5. #define portRegNum 14
  6. #endif
  7. #define portCONTEXT_SIZE ( portRegNum * REGBYTES )
  8. EXTERN _tx_thread_current_ptr
  9. EXTERN _tx_thread_execute_ptr
  10. EXTERN CSTACK$$Limit
  11. EXTERN PortThreadSwitch
  12. PUBLIC _tx_thread_schedule, eclic_msip_handler
  13. SECTION `.text`:CODE:NOROOT(2)
  14. CODE
  15. /*
  16. * VOID _tx_thread_schedule(VOID);
  17. */
  18. /* Start the first task. This also clears the bit that indicates the FPU is
  19. in use in case the FPU was used before the scheduler was started - which
  20. would otherwise result in the unnecessary leaving of space in the stack
  21. for lazy saving of FPU registers. */
  22. ALIGN 3
  23. _tx_thread_schedule:
  24. /* Setup Interrupt Stack using
  25. The stack that was used by main()
  26. before the scheduler is started is
  27. no longer required after the scheduler is started.
  28. Interrupt stack pointer is stored in CSR_MSCRATCH */
  29. la t0, CSTACK$$Limit
  30. csrw CSR_MSCRATCH, t0
  31. /* Enable interrupt to wait _tx_thread_execute_ptr is not null */
  32. csrsi CSR_MSTATUS, MSTATUS_MIE
  33. _tx_thread_schedule_loop:
  34. la a0, _tx_thread_execute_ptr /* Pick current thread to a0 */
  35. LOAD a0, 0(a0)
  36. beqz a0, _tx_thread_schedule_loop
  37. _tx_thread_schedule_ready:
  38. /* Disable interrupt to load first thread */
  39. csrci CSR_MSTATUS, MSTATUS_MIE
  40. la a1, _tx_thread_current_ptr /* _tx_thread_current_ptr = _tx_thread_execute_ptr */
  41. STORE a0, 0(a1)
  42. /* Increment the run count for this thread. */
  43. /* _tx_thread_current_ptr -> tx_thread_run_count++; */
  44. LOAD t0, 1 * REGBYTES(a0)
  45. addi t0, t0, 1
  46. STORE t0, 1 * REGBYTES(a0)
  47. LOAD sp, 2 * REGBYTES(a0) /* Read sp from _tx_thread_execute_ptr -> tx_thread_stack_ptr */
  48. /* Pop PC from stack and set MEPC */
  49. LOAD t0, 0 * REGBYTES(sp)
  50. csrw CSR_MEPC, t0
  51. /* Pop mstatus from stack and set it */
  52. LOAD t0, (portRegNum - 1) * REGBYTES(sp)
  53. csrw CSR_MSTATUS, t0
  54. /* Interrupt still disable here */
  55. /* Restore Registers from Stack */
  56. LOAD x1, 1 * REGBYTES(sp) /* RA */
  57. LOAD x5, 2 * REGBYTES(sp)
  58. LOAD x6, 3 * REGBYTES(sp)
  59. LOAD x7, 4 * REGBYTES(sp)
  60. LOAD x8, 5 * REGBYTES(sp)
  61. LOAD x9, 6 * REGBYTES(sp)
  62. LOAD x10, 7 * REGBYTES(sp)
  63. LOAD x11, 8 * REGBYTES(sp)
  64. LOAD x12, 9 * REGBYTES(sp)
  65. LOAD x13, 10 * REGBYTES(sp)
  66. LOAD x14, 11 * REGBYTES(sp)
  67. LOAD x15, 12 * REGBYTES(sp)
  68. #ifndef __riscv_32e
  69. LOAD x16, 13 * REGBYTES(sp)
  70. LOAD x17, 14 * REGBYTES(sp)
  71. LOAD x18, 15 * REGBYTES(sp)
  72. LOAD x19, 16 * REGBYTES(sp)
  73. LOAD x20, 17 * REGBYTES(sp)
  74. LOAD x21, 18 * REGBYTES(sp)
  75. LOAD x22, 19 * REGBYTES(sp)
  76. LOAD x23, 20 * REGBYTES(sp)
  77. LOAD x24, 21 * REGBYTES(sp)
  78. LOAD x25, 22 * REGBYTES(sp)
  79. LOAD x26, 23 * REGBYTES(sp)
  80. LOAD x27, 24 * REGBYTES(sp)
  81. LOAD x28, 25 * REGBYTES(sp)
  82. LOAD x29, 26 * REGBYTES(sp)
  83. LOAD x30, 27 * REGBYTES(sp)
  84. LOAD x31, 28 * REGBYTES(sp)
  85. #endif
  86. addi sp, sp, portCONTEXT_SIZE
  87. mret
  88. ALIGN 2
  89. eclic_msip_handler:
  90. addi sp, sp, -portCONTEXT_SIZE
  91. STORE x1, 1 * REGBYTES(sp) /* RA */
  92. STORE x5, 2 * REGBYTES(sp)
  93. STORE x6, 3 * REGBYTES(sp)
  94. STORE x7, 4 * REGBYTES(sp)
  95. STORE x8, 5 * REGBYTES(sp)
  96. STORE x9, 6 * REGBYTES(sp)
  97. STORE x10, 7 * REGBYTES(sp)
  98. STORE x11, 8 * REGBYTES(sp)
  99. STORE x12, 9 * REGBYTES(sp)
  100. STORE x13, 10 * REGBYTES(sp)
  101. STORE x14, 11 * REGBYTES(sp)
  102. STORE x15, 12 * REGBYTES(sp)
  103. #ifndef __riscv_32e
  104. STORE x16, 13 * REGBYTES(sp)
  105. STORE x17, 14 * REGBYTES(sp)
  106. STORE x18, 15 * REGBYTES(sp)
  107. STORE x19, 16 * REGBYTES(sp)
  108. STORE x20, 17 * REGBYTES(sp)
  109. STORE x21, 18 * REGBYTES(sp)
  110. STORE x22, 19 * REGBYTES(sp)
  111. STORE x23, 20 * REGBYTES(sp)
  112. STORE x24, 21 * REGBYTES(sp)
  113. STORE x25, 22 * REGBYTES(sp)
  114. STORE x26, 23 * REGBYTES(sp)
  115. STORE x27, 24 * REGBYTES(sp)
  116. STORE x28, 25 * REGBYTES(sp)
  117. STORE x29, 26 * REGBYTES(sp)
  118. STORE x30, 27 * REGBYTES(sp)
  119. STORE x31, 28 * REGBYTES(sp)
  120. #endif
  121. /* Push mstatus to stack */
  122. csrr t0, CSR_MSTATUS
  123. STORE t0, (portRegNum - 1) * REGBYTES(sp)
  124. /* Push additional registers */
  125. /* If _tx_thread_current_ptr is null, no sp need to be saved */
  126. la t0, _tx_thread_current_ptr
  127. LOAD t0, 0(t0)
  128. beqz t0, _tx_thread_switch
  129. /* Store sp to task stack to _tx_thread_current_ptr -> tx_thread_stack_ptr */
  130. STORE sp, 2 * REGBYTES(t0)
  131. /* Store last pc to thread stack */
  132. csrr t0, CSR_MEPC
  133. STORE t0, 0(sp)
  134. _tx_thread_switch:
  135. jal PortThreadSwitch
  136. /* Switch task context to _tx_thread_execute_ptr */
  137. la t0, _tx_thread_execute_ptr
  138. LOAD t0, 0(t0)
  139. LOAD sp, 2 * REGBYTES(t0)
  140. /* Pop PC from stack and set MEPC */
  141. LOAD t0, 0 * REGBYTES(sp)
  142. csrw CSR_MEPC, t0
  143. /* Pop additional registers */
  144. /* Pop mstatus from stack and set it */
  145. LOAD t0, (portRegNum - 1) * REGBYTES(sp)
  146. csrw CSR_MSTATUS, t0
  147. /* Interrupt still disable here */
  148. /* Restore Registers from Stack */
  149. LOAD x1, 1 * REGBYTES(sp) /* RA */
  150. LOAD x5, 2 * REGBYTES(sp)
  151. LOAD x6, 3 * REGBYTES(sp)
  152. LOAD x7, 4 * REGBYTES(sp)
  153. LOAD x8, 5 * REGBYTES(sp)
  154. LOAD x9, 6 * REGBYTES(sp)
  155. LOAD x10, 7 * REGBYTES(sp)
  156. LOAD x11, 8 * REGBYTES(sp)
  157. LOAD x12, 9 * REGBYTES(sp)
  158. LOAD x13, 10 * REGBYTES(sp)
  159. LOAD x14, 11 * REGBYTES(sp)
  160. LOAD x15, 12 * REGBYTES(sp)
  161. #ifndef __riscv_32e
  162. LOAD x16, 13 * REGBYTES(sp)
  163. LOAD x17, 14 * REGBYTES(sp)
  164. LOAD x18, 15 * REGBYTES(sp)
  165. LOAD x19, 16 * REGBYTES(sp)
  166. LOAD x20, 17 * REGBYTES(sp)
  167. LOAD x21, 18 * REGBYTES(sp)
  168. LOAD x22, 19 * REGBYTES(sp)
  169. LOAD x23, 20 * REGBYTES(sp)
  170. LOAD x24, 21 * REGBYTES(sp)
  171. LOAD x25, 22 * REGBYTES(sp)
  172. LOAD x26, 23 * REGBYTES(sp)
  173. LOAD x27, 24 * REGBYTES(sp)
  174. LOAD x28, 25 * REGBYTES(sp)
  175. LOAD x29, 26 * REGBYTES(sp)
  176. LOAD x30, 27 * REGBYTES(sp)
  177. LOAD x31, 28 * REGBYTES(sp)
  178. #endif
  179. addi sp, sp, portCONTEXT_SIZE
  180. mret
  181. END