stackframe.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-02-02 lizhirui first version
  9. * 2021-02-11 lizhirui fixed gp save/store bug
  10. * 2021-11-18 JasonHu add fpu registers save/restore
  11. */
  12. #ifndef __STACKFRAME_H__
  13. #define __STACKFRAME_H__
  14. #define BYTES(idx) ((idx) * REGBYTES)
  15. #define FRAME_OFF_SSTATUS BYTES(2)
  16. #define FRAME_OFF_SP BYTES(32)
  17. #include "cpuport.h"
  18. #include "encoding.h"
  19. #ifdef ENABLE_FPU
  20. #define FPU_CTX_F0_OFF 0 /* offsetof(fpu_context_t, fpustatus.f[0]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  21. #define FPU_CTX_F1_OFF 8 /* offsetof(fpu_context_t, fpustatus.f[1]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  22. #define FPU_CTX_F2_OFF 16 /* offsetof(fpu_context_t, fpustatus.f[2]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  23. #define FPU_CTX_F3_OFF 24 /* offsetof(fpu_context_t, fpustatus.f[3]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  24. #define FPU_CTX_F4_OFF 32 /* offsetof(fpu_context_t, fpustatus.f[4]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  25. #define FPU_CTX_F5_OFF 40 /* offsetof(fpu_context_t, fpustatus.f[5]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  26. #define FPU_CTX_F6_OFF 48 /* offsetof(fpu_context_t, fpustatus.f[6]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  27. #define FPU_CTX_F7_OFF 56 /* offsetof(fpu_context_t, fpustatus.f[7]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  28. #define FPU_CTX_F8_OFF 64 /* offsetof(fpu_context_t, fpustatus.f[8]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  29. #define FPU_CTX_F9_OFF 72 /* offsetof(fpu_context_t, fpustatus.f[9]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  30. #define FPU_CTX_F10_OFF 80 /* offsetof(fpu_context_t, fpustatus.f[10]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  31. #define FPU_CTX_F11_OFF 88 /* offsetof(fpu_context_t, fpustatus.f[11]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  32. #define FPU_CTX_F12_OFF 96 /* offsetof(fpu_context_t, fpustatus.f[12]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  33. #define FPU_CTX_F13_OFF 104 /* offsetof(fpu_context_t, fpustatus.f[13]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  34. #define FPU_CTX_F14_OFF 112 /* offsetof(fpu_context_t, fpustatus.f[14]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  35. #define FPU_CTX_F15_OFF 120 /* offsetof(fpu_context_t, fpustatus.f[15]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  36. #define FPU_CTX_F16_OFF 128 /* offsetof(fpu_context_t, fpustatus.f[16]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  37. #define FPU_CTX_F17_OFF 136 /* offsetof(fpu_context_t, fpustatus.f[17]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  38. #define FPU_CTX_F18_OFF 144 /* offsetof(fpu_context_t, fpustatus.f[18]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  39. #define FPU_CTX_F19_OFF 152 /* offsetof(fpu_context_t, fpustatus.f[19]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  40. #define FPU_CTX_F20_OFF 160 /* offsetof(fpu_context_t, fpustatus.f[20]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  41. #define FPU_CTX_F21_OFF 168 /* offsetof(fpu_context_t, fpustatus.f[21]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  42. #define FPU_CTX_F22_OFF 176 /* offsetof(fpu_context_t, fpustatus.f[22]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  43. #define FPU_CTX_F23_OFF 184 /* offsetof(fpu_context_t, fpustatus.f[23]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  44. #define FPU_CTX_F24_OFF 192 /* offsetof(fpu_context_t, fpustatus.f[24]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  45. #define FPU_CTX_F25_OFF 200 /* offsetof(fpu_context_t, fpustatus.f[25]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  46. #define FPU_CTX_F26_OFF 208 /* offsetof(fpu_context_t, fpustatus.f[26]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  47. #define FPU_CTX_F27_OFF 216 /* offsetof(fpu_context_t, fpustatus.f[27]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  48. #define FPU_CTX_F28_OFF 224 /* offsetof(fpu_context_t, fpustatus.f[28]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  49. #define FPU_CTX_F29_OFF 232 /* offsetof(fpu_context_t, fpustatus.f[29]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  50. #define FPU_CTX_F30_OFF 240 /* offsetof(fpu_context_t, fpustatus.f[30]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  51. #define FPU_CTX_F31_OFF 248 /* offsetof(fpu_context_t, fpustatus.f[31]) - offsetof(fpu_context_t, fpustatus.f[0]) */
  52. #endif /* ENABLE_FPU */
  53. /**
  54. * The register `tp` always save/restore when context switch,
  55. * we call `lwp_user_setting_save` when syscall enter,
  56. * call `lwp_user_setting_restore` when syscall exit
  57. * and modify context stack after `lwp_user_setting_restore` called
  58. * so that the `tp` can be the correct thread area value.
  59. */
  60. .macro SAVE_ALL
  61. #ifdef ENABLE_FPU
  62. /* reserve float registers */
  63. addi sp, sp, -CTX_FPU_REG_NR * REGBYTES
  64. #endif /* ENABLE_FPU */
  65. /* save general registers */
  66. addi sp, sp, -CTX_GENERAL_REG_NR * REGBYTES
  67. STORE x1, 1 * REGBYTES(sp)
  68. csrr x1, sstatus
  69. STORE x1, 2 * REGBYTES(sp)
  70. csrr x1, sepc
  71. STORE x1, 0 * REGBYTES(sp)
  72. STORE x3, 3 * REGBYTES(sp)
  73. STORE x4, 4 * REGBYTES(sp) /* save tp */
  74. STORE x5, 5 * REGBYTES(sp)
  75. STORE x6, 6 * REGBYTES(sp)
  76. STORE x7, 7 * REGBYTES(sp)
  77. STORE x8, 8 * REGBYTES(sp)
  78. STORE x9, 9 * REGBYTES(sp)
  79. STORE x10, 10 * REGBYTES(sp)
  80. STORE x11, 11 * REGBYTES(sp)
  81. STORE x12, 12 * REGBYTES(sp)
  82. STORE x13, 13 * REGBYTES(sp)
  83. STORE x14, 14 * REGBYTES(sp)
  84. STORE x15, 15 * REGBYTES(sp)
  85. STORE x16, 16 * REGBYTES(sp)
  86. STORE x17, 17 * REGBYTES(sp)
  87. STORE x18, 18 * REGBYTES(sp)
  88. STORE x19, 19 * REGBYTES(sp)
  89. STORE x20, 20 * REGBYTES(sp)
  90. STORE x21, 21 * REGBYTES(sp)
  91. STORE x22, 22 * REGBYTES(sp)
  92. STORE x23, 23 * REGBYTES(sp)
  93. STORE x24, 24 * REGBYTES(sp)
  94. STORE x25, 25 * REGBYTES(sp)
  95. STORE x26, 26 * REGBYTES(sp)
  96. STORE x27, 27 * REGBYTES(sp)
  97. STORE x28, 28 * REGBYTES(sp)
  98. STORE x29, 29 * REGBYTES(sp)
  99. STORE x30, 30 * REGBYTES(sp)
  100. STORE x31, 31 * REGBYTES(sp)
  101. csrr t0, sscratch
  102. STORE t0, 32 * REGBYTES(sp)
  103. #ifdef ENABLE_FPU
  104. /* backup sp and adjust sp to save float registers */
  105. mv t1, sp
  106. addi t1, t1, CTX_GENERAL_REG_NR * REGBYTES
  107. li t0, SSTATUS_FS
  108. csrs sstatus, t0
  109. fsd f0, FPU_CTX_F0_OFF(t1)
  110. fsd f1, FPU_CTX_F1_OFF(t1)
  111. fsd f2, FPU_CTX_F2_OFF(t1)
  112. fsd f3, FPU_CTX_F3_OFF(t1)
  113. fsd f4, FPU_CTX_F4_OFF(t1)
  114. fsd f5, FPU_CTX_F5_OFF(t1)
  115. fsd f6, FPU_CTX_F6_OFF(t1)
  116. fsd f7, FPU_CTX_F7_OFF(t1)
  117. fsd f8, FPU_CTX_F8_OFF(t1)
  118. fsd f9, FPU_CTX_F9_OFF(t1)
  119. fsd f10, FPU_CTX_F10_OFF(t1)
  120. fsd f11, FPU_CTX_F11_OFF(t1)
  121. fsd f12, FPU_CTX_F12_OFF(t1)
  122. fsd f13, FPU_CTX_F13_OFF(t1)
  123. fsd f14, FPU_CTX_F14_OFF(t1)
  124. fsd f15, FPU_CTX_F15_OFF(t1)
  125. fsd f16, FPU_CTX_F16_OFF(t1)
  126. fsd f17, FPU_CTX_F17_OFF(t1)
  127. fsd f18, FPU_CTX_F18_OFF(t1)
  128. fsd f19, FPU_CTX_F19_OFF(t1)
  129. fsd f20, FPU_CTX_F20_OFF(t1)
  130. fsd f21, FPU_CTX_F21_OFF(t1)
  131. fsd f22, FPU_CTX_F22_OFF(t1)
  132. fsd f23, FPU_CTX_F23_OFF(t1)
  133. fsd f24, FPU_CTX_F24_OFF(t1)
  134. fsd f25, FPU_CTX_F25_OFF(t1)
  135. fsd f26, FPU_CTX_F26_OFF(t1)
  136. fsd f27, FPU_CTX_F27_OFF(t1)
  137. fsd f28, FPU_CTX_F28_OFF(t1)
  138. fsd f29, FPU_CTX_F29_OFF(t1)
  139. fsd f30, FPU_CTX_F30_OFF(t1)
  140. fsd f31, FPU_CTX_F31_OFF(t1)
  141. /* clr FS domain */
  142. csrc sstatus, t0
  143. /* clean status would clr sr_sd; */
  144. li t0, SSTATUS_FS_CLEAN
  145. csrs sstatus, t0
  146. #endif /* ENABLE_FPU */
  147. .endm
  148. .macro RESTORE_ALL
  149. #ifdef ENABLE_FPU
  150. /* restore float register */
  151. mv t2, sp
  152. addi t2, t2, CTX_GENERAL_REG_NR * REGBYTES /* skip all normal reg */
  153. li t0, SSTATUS_FS
  154. csrs sstatus, t0
  155. fld f0, FPU_CTX_F0_OFF(t2)
  156. fld f1, FPU_CTX_F1_OFF(t2)
  157. fld f2, FPU_CTX_F2_OFF(t2)
  158. fld f3, FPU_CTX_F3_OFF(t2)
  159. fld f4, FPU_CTX_F4_OFF(t2)
  160. fld f5, FPU_CTX_F5_OFF(t2)
  161. fld f6, FPU_CTX_F6_OFF(t2)
  162. fld f7, FPU_CTX_F7_OFF(t2)
  163. fld f8, FPU_CTX_F8_OFF(t2)
  164. fld f9, FPU_CTX_F9_OFF(t2)
  165. fld f10, FPU_CTX_F10_OFF(t2)
  166. fld f11, FPU_CTX_F11_OFF(t2)
  167. fld f12, FPU_CTX_F12_OFF(t2)
  168. fld f13, FPU_CTX_F13_OFF(t2)
  169. fld f14, FPU_CTX_F14_OFF(t2)
  170. fld f15, FPU_CTX_F15_OFF(t2)
  171. fld f16, FPU_CTX_F16_OFF(t2)
  172. fld f17, FPU_CTX_F17_OFF(t2)
  173. fld f18, FPU_CTX_F18_OFF(t2)
  174. fld f19, FPU_CTX_F19_OFF(t2)
  175. fld f20, FPU_CTX_F20_OFF(t2)
  176. fld f21, FPU_CTX_F21_OFF(t2)
  177. fld f22, FPU_CTX_F22_OFF(t2)
  178. fld f23, FPU_CTX_F23_OFF(t2)
  179. fld f24, FPU_CTX_F24_OFF(t2)
  180. fld f25, FPU_CTX_F25_OFF(t2)
  181. fld f26, FPU_CTX_F26_OFF(t2)
  182. fld f27, FPU_CTX_F27_OFF(t2)
  183. fld f28, FPU_CTX_F28_OFF(t2)
  184. fld f29, FPU_CTX_F29_OFF(t2)
  185. fld f30, FPU_CTX_F30_OFF(t2)
  186. fld f31, FPU_CTX_F31_OFF(t2)
  187. /* clr FS domain */
  188. csrc sstatus, t0
  189. /* clean status would clr sr_sd; */
  190. li t0, SSTATUS_FS_CLEAN
  191. csrs sstatus, t0
  192. #endif /* ENABLE_FPU */
  193. /* restore general register */
  194. /* resw ra to sepc */
  195. LOAD x1, 0 * REGBYTES(sp)
  196. csrw sepc, x1
  197. LOAD x1, 2 * REGBYTES(sp)
  198. csrw sstatus, x1
  199. LOAD x1, 1 * REGBYTES(sp)
  200. LOAD x3, 3 * REGBYTES(sp)
  201. LOAD x4, 4 * REGBYTES(sp) /* restore tp */
  202. LOAD x5, 5 * REGBYTES(sp)
  203. LOAD x6, 6 * REGBYTES(sp)
  204. LOAD x7, 7 * REGBYTES(sp)
  205. LOAD x8, 8 * REGBYTES(sp)
  206. LOAD x9, 9 * REGBYTES(sp)
  207. LOAD x10, 10 * REGBYTES(sp)
  208. LOAD x11, 11 * REGBYTES(sp)
  209. LOAD x12, 12 * REGBYTES(sp)
  210. LOAD x13, 13 * REGBYTES(sp)
  211. LOAD x14, 14 * REGBYTES(sp)
  212. LOAD x15, 15 * REGBYTES(sp)
  213. LOAD x16, 16 * REGBYTES(sp)
  214. LOAD x17, 17 * REGBYTES(sp)
  215. LOAD x18, 18 * REGBYTES(sp)
  216. LOAD x19, 19 * REGBYTES(sp)
  217. LOAD x20, 20 * REGBYTES(sp)
  218. LOAD x21, 21 * REGBYTES(sp)
  219. LOAD x22, 22 * REGBYTES(sp)
  220. LOAD x23, 23 * REGBYTES(sp)
  221. LOAD x24, 24 * REGBYTES(sp)
  222. LOAD x25, 25 * REGBYTES(sp)
  223. LOAD x26, 26 * REGBYTES(sp)
  224. LOAD x27, 27 * REGBYTES(sp)
  225. LOAD x28, 28 * REGBYTES(sp)
  226. LOAD x29, 29 * REGBYTES(sp)
  227. LOAD x30, 30 * REGBYTES(sp)
  228. LOAD x31, 31 * REGBYTES(sp)
  229. /* restore user sp */
  230. LOAD sp, 32 * REGBYTES(sp)
  231. .endm
  232. .macro RESTORE_SYS_GP
  233. .option push
  234. .option norelax
  235. la gp, __global_pointer$
  236. .option pop
  237. .endm
  238. .macro OPEN_INTERRUPT
  239. csrsi sstatus, 2
  240. .endm
  241. .macro CLOSE_INTERRUPT
  242. csrci sstatus, 2
  243. .endm
  244. #endif