switchcontext.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /* PowerPC support code for fibers and multithreading.
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "../common/threadasm.S"
  20. #if !defined(__PPC64__) && !defined(__MACH__)
  21. /**
  22. * Performs a context switch.
  23. *
  24. * r3 - old context pointer
  25. * r4 - new context pointer
  26. *
  27. */
  28. .text
  29. .globl CSYM(fiber_switchContext)
  30. .type CSYM(fiber_switchContext), @function
  31. .align 2
  32. CSYM(fiber_switchContext):
  33. .cfi_startproc
  34. /* Save linkage area */
  35. mflr 0
  36. mfcr 5
  37. stw 0, 8(1)
  38. stw 5, 4(1)
  39. /* Save GPRs */
  40. stw 11, (-1 * 4)(1)
  41. stw 13, (-2 * 4)(1)
  42. stw 14, (-3 * 4)(1)
  43. stw 15, (-4 * 4)(1)
  44. stw 16, (-5 * 4)(1)
  45. stw 17, (-6 * 4)(1)
  46. stw 18, (-7 * 4)(1)
  47. stw 19, (-8 * 4)(1)
  48. stw 20, (-9 * 4)(1)
  49. stw 21, (-10 * 4)(1)
  50. stw 22, (-11 * 4)(1)
  51. stw 23, (-12 * 4)(1)
  52. stw 24, (-13 * 4)(1)
  53. stw 25, (-14 * 4)(1)
  54. stw 26, (-15 * 4)(1)
  55. stw 27, (-16 * 4)(1)
  56. stw 28, (-17 * 4)(1)
  57. stw 29, (-18 * 4)(1)
  58. stw 30, (-19 * 4)(1)
  59. stwu 31, (-20 * 4)(1)
  60. /* We update the stack pointer here, since we do not want the GC to
  61. scan the floating point registers. */
  62. /* Save FPRs */
  63. stfd 14, (-1 * 8)(1)
  64. stfd 15, (-2 * 8)(1)
  65. stfd 16, (-3 * 8)(1)
  66. stfd 17, (-4 * 8)(1)
  67. stfd 18, (-5 * 8)(1)
  68. stfd 19, (-6 * 8)(1)
  69. stfd 20, (-7 * 8)(1)
  70. stfd 21, (-8 * 8)(1)
  71. stfd 22, (-9 * 8)(1)
  72. stfd 23, (-10 * 8)(1)
  73. stfd 24, (-11 * 8)(1)
  74. stfd 25, (-12 * 8)(1)
  75. stfd 26, (-13 * 8)(1)
  76. stfd 27, (-14 * 8)(1)
  77. stfd 28, (-15 * 8)(1)
  78. stfd 29, (-16 * 8)(1)
  79. stfd 30, (-17 * 8)(1)
  80. stfd 31, (-18 * 8)(1)
  81. /* Update the old stack pointer */
  82. stw 1, 0(3)
  83. /* Set new stack pointer */
  84. addi 1, 4, 20 * 4
  85. /* Restore linkage area */
  86. lwz 0, 8(1)
  87. lwz 5, 4(1)
  88. /* Restore GPRs */
  89. lwz 11, (-1 * 4)(1)
  90. lwz 13, (-2 * 4)(1)
  91. lwz 14, (-3 * 4)(1)
  92. lwz 15, (-4 * 4)(1)
  93. lwz 16, (-5 * 4)(1)
  94. lwz 17, (-6 * 4)(1)
  95. lwz 18, (-7 * 4)(1)
  96. lwz 19, (-8 * 4)(1)
  97. lwz 20, (-9 * 4)(1)
  98. lwz 21, (-10 * 4)(1)
  99. lwz 22, (-11 * 4)(1)
  100. lwz 23, (-12 * 4)(1)
  101. lwz 24, (-13 * 4)(1)
  102. lwz 25, (-14 * 4)(1)
  103. lwz 26, (-15 * 4)(1)
  104. lwz 27, (-16 * 4)(1)
  105. lwz 28, (-17 * 4)(1)
  106. lwz 29, (-18 * 4)(1)
  107. lwz 30, (-19 * 4)(1)
  108. lwz 31, (-20 * 4)(1)
  109. /* Restore FPRs */
  110. lfd 14, (-1 * 8)(4)
  111. lfd 15, (-2 * 8)(4)
  112. lfd 16, (-3 * 8)(4)
  113. lfd 17, (-4 * 8)(4)
  114. lfd 18, (-5 * 8)(4)
  115. lfd 19, (-6 * 8)(4)
  116. lfd 20, (-7 * 8)(4)
  117. lfd 21, (-8 * 8)(4)
  118. lfd 22, (-9 * 8)(4)
  119. lfd 23, (-10 * 8)(4)
  120. lfd 24, (-11 * 8)(4)
  121. lfd 25, (-12 * 8)(4)
  122. lfd 26, (-13 * 8)(4)
  123. lfd 27, (-14 * 8)(4)
  124. lfd 28, (-15 * 8)(4)
  125. lfd 29, (-16 * 8)(4)
  126. lfd 30, (-17 * 8)(4)
  127. lfd 31, (-18 * 8)(4)
  128. /* Set condition and link register */
  129. mtcr 5
  130. mtlr 0
  131. /* Return and switch context */
  132. blr
  133. .cfi_endproc
  134. .size CSYM(fiber_switchContext),.-CSYM(fiber_switchContext)
  135. #elif defined(__MACH__)
  136. /* Implementation for Darwin/macOS preserving callee-saved regs.
  137. FIXME : There is no unwind frame.
  138. FIXME : not sure if we should save the vsave reg (perhaps using the slot we have
  139. r11 in at present). */
  140. /* Darwin has a red zone (220 bytes for PPC 288 for PPC64) which we can write
  141. to before the stack is updated without worrying about it being clobbered by
  142. signals or hardware interrupts.
  143. The stack will be 16byte aligned on entry with:
  144. PPC PPC64
  145. SP-> +---------------------------------------+
  146. | back chain to caller | 0 0
  147. +---------------------------------------+
  148. | slot to save CR | 4 8
  149. +---------------------------------------+
  150. | slot to save LR | 8 16
  151. +---------------------------------------+
  152. | etc.. etc.. as per C calling conv. | */
  153. # if __PPC64__
  154. # define LD ld
  155. # define ST std
  156. # define STU stdu
  157. # define SZ 8
  158. # define MACHINE ppc64
  159. # define RED_ZONE 288
  160. # else
  161. # define LD lwz
  162. # define ST stw
  163. # define STU stwu
  164. # define SZ 4
  165. # define MACHINE ppc7400
  166. # define RED_ZONE 220
  167. # endif
  168. # define SAVE_VECTORS 0
  169. /**
  170. * Performs a context switch.
  171. *
  172. * r3 - old context pointer
  173. * r4 - new context pointer
  174. *
  175. */
  176. .machine MACHINE
  177. .text
  178. .globl CSYM(fiber_switchContext)
  179. .align 2
  180. CSYM(fiber_switchContext):
  181. LFB0:
  182. /* Get the link reg. */
  183. mflr r0
  184. /* Get the callee-saved crs (well all of them, actually). */
  185. mfcr r12
  186. /* Save GPRs, we save the static chain here too although it is not clear if we need to. */
  187. ST r31, ( -1 * SZ)(r1)
  188. ST r30, ( -2 * SZ)(r1)
  189. ST r29, ( -3 * SZ)(r1)
  190. ST r28, ( -4 * SZ)(r1)
  191. ST r27, ( -5 * SZ)(r1)
  192. ST r26, ( -6 * SZ)(r1)
  193. ST r25, ( -7 * SZ)(r1)
  194. ST r24, ( -8 * SZ)(r1)
  195. ST r23, ( -9 * SZ)(r1)
  196. ST r22, (-10 * SZ)(r1)
  197. ST r21, (-11 * SZ)(r1)
  198. ST r20, (-12 * SZ)(r1)
  199. ST r19, (-13 * SZ)(r1)
  200. ST r18, (-14 * SZ)(r1)
  201. ST r17, (-15 * SZ)(r1)
  202. ST r16, (-16 * SZ)(r1)
  203. ST r15, (-17 * SZ)(r1)
  204. ST r14, (-18 * SZ)(r1)
  205. ST r13, (-19 * SZ)(r1)
  206. /* Save the lr and cr into the normal function linkage area. */
  207. ST r0, 2*SZ(r1)
  208. ST r12, SZ(r1)
  209. /* We update the stack pointer here, since we do not want the GC to
  210. scan the floating point registers. We are still 16-byte aligned. */
  211. STU r11, (-20 * SZ)(r1)
  212. /* Update the stack pointer in the old context as per comment above. */
  213. ST r1, 0(r3)
  214. /* Save FPRs - same for PPC and PPC64 */
  215. stfd f14, (-18 * 8)(r1)
  216. stfd f15, (-17 * 8)(r1)
  217. stfd f16, (-16 * 8)(r1)
  218. stfd f17, (-15 * 8)(r1)
  219. stfd f18, (-14 * 8)(r1)
  220. stfd f19, (-13 * 8)(r1)
  221. stfd f20, (-12 * 8)(r1)
  222. stfd f21, (-11 * 8)(r1)
  223. stfd f22, (-10 * 8)(r1)
  224. stfd f23, ( -9 * 8)(r1)
  225. stfd f24, ( -8 * 8)(r1)
  226. stfd f25, ( -7 * 8)(r1)
  227. stfd f26, ( -6 * 8)(r1)
  228. stfd f27, ( -5 * 8)(r1)
  229. stfd f28, ( -4 * 8)(r1)
  230. stfd f29, ( -3 * 8)(r1)
  231. stfd f30, ( -2 * 8)(r1)
  232. stfd f31, ( -1 * 8)(r1)
  233. #if SAVE_VECTORS
  234. /* We are still 16byte aligned - so we are ok for vector saves.
  235. but the combined size of the vectors (12 x 16) + the FPRs (144) exceeds the
  236. red zone size so we need to adjust the stack again - note this means careful
  237. ordering is needed on the restore. */
  238. addi r1, r1, -(12*16+18*8)
  239. li r11, 0
  240. stvx v20,r11,r1
  241. addi r11, r11, 16
  242. stvx v21,r11,r1
  243. addi r11, r11, 16
  244. stvx v22,r11,r1
  245. addi r11, r11, 16
  246. stvx v23,r11,r1
  247. addi r11, r11, 16
  248. stvx v24,r11,r1
  249. addi r11, r11, 16
  250. stvx v25,r11,r1
  251. addi r11, r11, 16
  252. stvx v26,r11,r1
  253. addi r11, r11, 16
  254. stvx v27,r11,r1
  255. addi r11, r11, 16
  256. stvx v28,r11,r1
  257. addi r11, r11, 16
  258. stvx v29,r11,r1
  259. addi r11, r11, 16
  260. stvx v30,r11,r1
  261. addi r11, r11, 16
  262. stvx v31,r11,r1
  263. /* Now do the same thing in reverse - starting with r4 pointing to
  264. the block of GPRs - stage 1 point to the saved vectors and fprs. */
  265. addi r1, r4, -(12*16+18*8)
  266. li r11, 0
  267. lvx v20,r11,r1
  268. addi r11, r11, 16
  269. lvx v21,r11,r1
  270. addi r11, r11, 16
  271. lvx v22,r11,r1
  272. addi r11, r11, 16
  273. lvx v23,r11,r1
  274. addi r11, r11, 16
  275. lvx v24,r11,r1
  276. addi r11, r11, 16
  277. lvx v25,r11,r1
  278. addi r11, r11, 16
  279. lvx v26,r11,r1
  280. addi r11, r11, 16
  281. lvx v27,r11,r1
  282. addi r11, r11, 16
  283. lvx v28,r11,r1
  284. addi r11, r11, 16
  285. lvx v29,r11,r1
  286. addi r11, r11, 16
  287. lvx v30,r11,r1
  288. addi r11, r11, 16
  289. lvx v31,r11,r1
  290. #endif
  291. /* Now it is safe to update the stack pointer since the combined
  292. size of the GPRs and FPRs will not exceed the red zone. */
  293. addi r1, r4, 20 * SZ
  294. /* Restore FPRs */
  295. lfd f14, (-18 * 8)(r4)
  296. lfd f15, (-17 * 8)(r4)
  297. lfd f16, (-16 * 8)(r4)
  298. lfd f17, (-15 * 8)(r4)
  299. lfd f18, (-14 * 8)(r4)
  300. lfd f19, (-13 * 8)(r4)
  301. lfd f20, (-12 * 8)(r4)
  302. lfd f21, (-11 * 8)(r4)
  303. lfd f22, (-10 * 8)(r4)
  304. lfd f23, ( -9 * 8)(r4)
  305. lfd f24, ( -8 * 8)(r4)
  306. lfd f25, ( -7 * 8)(r4)
  307. lfd f26, ( -6 * 8)(r4)
  308. lfd f27, ( -5 * 8)(r4)
  309. lfd f28, ( -4 * 8)(r4)
  310. lfd f29, ( -3 * 8)(r4)
  311. lfd f30, ( -2 * 8)(r4)
  312. lfd f31, ( -1 * 8)(r4)
  313. /* Pick up lr and cr */
  314. LD r0, 2*SZ(r1)
  315. LD r12, SZ(r1)
  316. /* Restore GPRs */
  317. LD r11, (-20 * SZ)(r1)
  318. LD r13, (-19 * SZ)(r1)
  319. LD r14, (-18 * SZ)(r1)
  320. LD r15, (-17 * SZ)(r1)
  321. LD r16, (-16 * SZ)(r1)
  322. LD r17, (-15 * SZ)(r1)
  323. LD r18, (-14 * SZ)(r1)
  324. LD r19, (-13 * SZ)(r1)
  325. LD r20, (-12 * SZ)(r1)
  326. LD r21, (-11 * SZ)(r1)
  327. LD r22, (-10 * SZ)(r1)
  328. LD r23, ( -9 * SZ)(r1)
  329. LD r24, ( -8 * SZ)(r1)
  330. LD r25, ( -7 * SZ)(r1)
  331. LD r26, ( -6 * SZ)(r1)
  332. LD r27, ( -5 * SZ)(r1)
  333. LD r28, ( -4 * SZ)(r1)
  334. LD r29, ( -3 * SZ)(r1)
  335. LD r30, ( -2 * SZ)(r1)
  336. LD r31, ( -1 * SZ)(r1)
  337. /* Set cr and lr */
  338. mtcr r12
  339. mtlr r0
  340. /* Return and switch context */
  341. blr
  342. LFE0:
  343. /* Minimal CFI / FDE which does not describe the stacking of the GPRs - but only that
  344. the routine has been entered/exited. */
  345. # if __PPC64__
  346. # define DATA_ALIGN 0x78
  347. # define ALIGN_SIZE 3
  348. # define ADDRD .quad
  349. # else
  350. # define DATA_ALIGN 0x7c
  351. # define ALIGN_SIZE 3
  352. # define ADDRD .long
  353. # endif
  354. .section __TEXT,__eh_frame,coalesced,no_toc+strip_static_syms+live_support
  355. EH_frame1:
  356. .set L$set$0,LECIE1-LSCIE1
  357. .long L$set$0 ; Length of Common Information Entry
  358. LSCIE1:
  359. .long 0 ; CIE Identifier Tag
  360. .byte 0x3 ; CIE Version
  361. .ascii "zR\0" ; CIE Augmentation
  362. .byte 0x1 ; uleb128 0x1; CIE Code Alignment Factor
  363. .byte DATA_ALIGN ; sleb128 -4/-8; CIE Data Alignment Factor
  364. .byte 0x41 ; uleb128 0x41; CIE RA Column
  365. .byte 0x1 ; uleb128 0x1; Augmentation size
  366. .byte 0x10 ; FDE Encoding (pcrel)
  367. .byte 0xc ; DW_CFA_def_cfa
  368. .byte 0x1 ; uleb128 0x1
  369. .byte 0 ; uleb128 0
  370. .p2align ALIGN_SIZE,0
  371. LECIE1:
  372. LSFDE1:
  373. .set L$set$1,LEFDE1-LASFDE1
  374. .long L$set$1 ; FDE Length
  375. LASFDE1:
  376. .long LASFDE1-EH_frame1 ; FDE CIE offset
  377. ADDRD LFB0-. ; FDE initial location
  378. .set L$set$2,LFE0-LFB0
  379. ADDRD L$set$2 ; FDE address range
  380. .byte 0 ; uleb128 0; Augmentation size
  381. .p2align ALIGN_SIZE,0
  382. LEFDE1:
  383. #endif /* defined(__MACH__) */