ppc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /* ppc64le (ELFv2) is not currently supported */
  2. #define LIBCO_C
  3. #include "libco.h"
  4. #include "settings.h"
  5. #include <stdint.h>
  6. #include <string.h>
  7. #ifdef LIBCO_MPROTECT
  8. #include <unistd.h>
  9. #include <sys/mman.h>
  10. #endif
  11. /* state format (offsets in 32-bit words)
  12. +0 pointer to swap code
  13. rest of function descriptor for entry function
  14. +8 PC
  15. +10 SP
  16. special registers
  17. GPRs
  18. FPRs
  19. VRs
  20. stack
  21. */
  22. enum { state_size = 1024 };
  23. enum { above_stack = 2048 };
  24. enum { stack_align = 256 };
  25. static thread_local cothread_t co_active_handle = 0;
  26. /* determine environment */
  27. #define LIBCO_PPC64 (_ARCH_PPC64 || __PPC64__ || __ppc64__ || __powerpc64__)
  28. /* whether function calls are indirect through a descriptor, or are directly to function */
  29. #ifndef LIBCO_PPCDESC
  30. #if !_CALL_SYSV && (_CALL_AIX || _CALL_AIXDESC || (LIBCO_PPC64 && (!defined(_CALL_ELF) || _CALL_ELF == 1)))
  31. #define LIBCO_PPCDESC 1
  32. #endif
  33. #endif
  34. #ifdef LIBCO_MPROTECT
  35. alignas(4096)
  36. #else
  37. section(text)
  38. #endif
  39. static const uint32_t libco_ppc_code[1024] = {
  40. #if LIBCO_PPC64
  41. 0x7d000026, /* mfcr r8 */
  42. 0xf8240028, /* std r1,40(r4) */
  43. 0x7d2802a6, /* mflr r9 */
  44. 0xf9c40048, /* std r14,72(r4) */
  45. 0xf9e40050, /* std r15,80(r4) */
  46. 0xfa040058, /* std r16,88(r4) */
  47. 0xfa240060, /* std r17,96(r4) */
  48. 0xfa440068, /* std r18,104(r4) */
  49. 0xfa640070, /* std r19,112(r4) */
  50. 0xfa840078, /* std r20,120(r4) */
  51. 0xfaa40080, /* std r21,128(r4) */
  52. 0xfac40088, /* std r22,136(r4) */
  53. 0xfae40090, /* std r23,144(r4) */
  54. 0xfb040098, /* std r24,152(r4) */
  55. 0xfb2400a0, /* std r25,160(r4) */
  56. 0xfb4400a8, /* std r26,168(r4) */
  57. 0xfb6400b0, /* std r27,176(r4) */
  58. 0xfb8400b8, /* std r28,184(r4) */
  59. 0xfba400c0, /* std r29,192(r4) */
  60. 0xfbc400c8, /* std r30,200(r4) */
  61. 0xfbe400d0, /* std r31,208(r4) */
  62. 0xf9240020, /* std r9,32(r4) */
  63. 0xe8e30020, /* ld r7,32(r3) */
  64. 0xe8230028, /* ld r1,40(r3) */
  65. 0x48000009, /* bl 1 */
  66. 0x7fe00008, /* trap */
  67. 0x91040030, /*1:stw r8,48(r4) */
  68. 0x80c30030, /* lwz r6,48(r3) */
  69. 0x7ce903a6, /* mtctr r7 */
  70. 0xe9c30048, /* ld r14,72(r3) */
  71. 0xe9e30050, /* ld r15,80(r3) */
  72. 0xea030058, /* ld r16,88(r3) */
  73. 0xea230060, /* ld r17,96(r3) */
  74. 0xea430068, /* ld r18,104(r3) */
  75. 0xea630070, /* ld r19,112(r3) */
  76. 0xea830078, /* ld r20,120(r3) */
  77. 0xeaa30080, /* ld r21,128(r3) */
  78. 0xeac30088, /* ld r22,136(r3) */
  79. 0xeae30090, /* ld r23,144(r3) */
  80. 0xeb030098, /* ld r24,152(r3) */
  81. 0xeb2300a0, /* ld r25,160(r3) */
  82. 0xeb4300a8, /* ld r26,168(r3) */
  83. 0xeb6300b0, /* ld r27,176(r3) */
  84. 0xeb8300b8, /* ld r28,184(r3) */
  85. 0xeba300c0, /* ld r29,192(r3) */
  86. 0xebc300c8, /* ld r30,200(r3) */
  87. 0xebe300d0, /* ld r31,208(r3) */
  88. 0x7ccff120, /* mtcr r6 */
  89. #else
  90. 0x7d000026, /* mfcr r8 */
  91. 0x90240028, /* stw r1,40(r4) */
  92. 0x7d2802a6, /* mflr r9 */
  93. 0x91a4003c, /* stw r13,60(r4) */
  94. 0x91c40040, /* stw r14,64(r4) */
  95. 0x91e40044, /* stw r15,68(r4) */
  96. 0x92040048, /* stw r16,72(r4) */
  97. 0x9224004c, /* stw r17,76(r4) */
  98. 0x92440050, /* stw r18,80(r4) */
  99. 0x92640054, /* stw r19,84(r4) */
  100. 0x92840058, /* stw r20,88(r4) */
  101. 0x92a4005c, /* stw r21,92(r4) */
  102. 0x92c40060, /* stw r22,96(r4) */
  103. 0x92e40064, /* stw r23,100(r4) */
  104. 0x93040068, /* stw r24,104(r4) */
  105. 0x9324006c, /* stw r25,108(r4) */
  106. 0x93440070, /* stw r26,112(r4) */
  107. 0x93640074, /* stw r27,116(r4) */
  108. 0x93840078, /* stw r28,120(r4) */
  109. 0x93a4007c, /* stw r29,124(r4) */
  110. 0x93c40080, /* stw r30,128(r4) */
  111. 0x93e40084, /* stw r31,132(r4) */
  112. 0x91240020, /* stw r9,32(r4) */
  113. 0x80e30020, /* lwz r7,32(r3) */
  114. 0x80230028, /* lwz r1,40(r3) */
  115. 0x48000009, /* bl 1 */
  116. 0x7fe00008, /* trap */
  117. 0x91040030, /*1:stw r8,48(r4) */
  118. 0x80c30030, /* lwz r6,48(r3) */
  119. 0x7ce903a6, /* mtctr r7 */
  120. 0x81a3003c, /* lwz r13,60(r3) */
  121. 0x81c30040, /* lwz r14,64(r3) */
  122. 0x81e30044, /* lwz r15,68(r3) */
  123. 0x82030048, /* lwz r16,72(r3) */
  124. 0x8223004c, /* lwz r17,76(r3) */
  125. 0x82430050, /* lwz r18,80(r3) */
  126. 0x82630054, /* lwz r19,84(r3) */
  127. 0x82830058, /* lwz r20,88(r3) */
  128. 0x82a3005c, /* lwz r21,92(r3) */
  129. 0x82c30060, /* lwz r22,96(r3) */
  130. 0x82e30064, /* lwz r23,100(r3) */
  131. 0x83030068, /* lwz r24,104(r3) */
  132. 0x8323006c, /* lwz r25,108(r3) */
  133. 0x83430070, /* lwz r26,112(r3) */
  134. 0x83630074, /* lwz r27,116(r3) */
  135. 0x83830078, /* lwz r28,120(r3) */
  136. 0x83a3007c, /* lwz r29,124(r3) */
  137. 0x83c30080, /* lwz r30,128(r3) */
  138. 0x83e30084, /* lwz r31,132(r3) */
  139. 0x7ccff120, /* mtcr r6 */
  140. #endif
  141. #ifndef LIBCO_PPC_NOFP
  142. 0xd9c400e0, /* stfd f14,224(r4) */
  143. 0xd9e400e8, /* stfd f15,232(r4) */
  144. 0xda0400f0, /* stfd f16,240(r4) */
  145. 0xda2400f8, /* stfd f17,248(r4) */
  146. 0xda440100, /* stfd f18,256(r4) */
  147. 0xda640108, /* stfd f19,264(r4) */
  148. 0xda840110, /* stfd f20,272(r4) */
  149. 0xdaa40118, /* stfd f21,280(r4) */
  150. 0xdac40120, /* stfd f22,288(r4) */
  151. 0xdae40128, /* stfd f23,296(r4) */
  152. 0xdb040130, /* stfd f24,304(r4) */
  153. 0xdb240138, /* stfd f25,312(r4) */
  154. 0xdb440140, /* stfd f26,320(r4) */
  155. 0xdb640148, /* stfd f27,328(r4) */
  156. 0xdb840150, /* stfd f28,336(r4) */
  157. 0xdba40158, /* stfd f29,344(r4) */
  158. 0xdbc40160, /* stfd f30,352(r4) */
  159. 0xdbe40168, /* stfd f31,360(r4) */
  160. 0xc9c300e0, /* lfd f14,224(r3) */
  161. 0xc9e300e8, /* lfd f15,232(r3) */
  162. 0xca0300f0, /* lfd f16,240(r3) */
  163. 0xca2300f8, /* lfd f17,248(r3) */
  164. 0xca430100, /* lfd f18,256(r3) */
  165. 0xca630108, /* lfd f19,264(r3) */
  166. 0xca830110, /* lfd f20,272(r3) */
  167. 0xcaa30118, /* lfd f21,280(r3) */
  168. 0xcac30120, /* lfd f22,288(r3) */
  169. 0xcae30128, /* lfd f23,296(r3) */
  170. 0xcb030130, /* lfd f24,304(r3) */
  171. 0xcb230138, /* lfd f25,312(r3) */
  172. 0xcb430140, /* lfd f26,320(r3) */
  173. 0xcb630148, /* lfd f27,328(r3) */
  174. 0xcb830150, /* lfd f28,336(r3) */
  175. 0xcba30158, /* lfd f29,344(r3) */
  176. 0xcbc30160, /* lfd f30,352(r3) */
  177. 0xcbe30168, /* lfd f31,360(r3) */
  178. #endif
  179. #ifdef __ALTIVEC__
  180. 0x7ca042a6, /* mfvrsave r5 */
  181. 0x39040180, /* addi r8,r4,384 */
  182. 0x39240190, /* addi r9,r4,400 */
  183. 0x70a00fff, /* andi. r0,r5,4095 */
  184. 0x90a40034, /* stw r5,52(r4) */
  185. 0x4182005c, /* beq- 2 */
  186. 0x7e8041ce, /* stvx v20,r0,r8 */
  187. 0x39080020, /* addi r8,r8,32 */
  188. 0x7ea049ce, /* stvx v21,r0,r9 */
  189. 0x39290020, /* addi r9,r9,32 */
  190. 0x7ec041ce, /* stvx v22,r0,r8 */
  191. 0x39080020, /* addi r8,r8,32 */
  192. 0x7ee049ce, /* stvx v23,r0,r9 */
  193. 0x39290020, /* addi r9,r9,32 */
  194. 0x7f0041ce, /* stvx v24,r0,r8 */
  195. 0x39080020, /* addi r8,r8,32 */
  196. 0x7f2049ce, /* stvx v25,r0,r9 */
  197. 0x39290020, /* addi r9,r9,32 */
  198. 0x7f4041ce, /* stvx v26,r0,r8 */
  199. 0x39080020, /* addi r8,r8,32 */
  200. 0x7f6049ce, /* stvx v27,r0,r9 */
  201. 0x39290020, /* addi r9,r9,32 */
  202. 0x7f8041ce, /* stvx v28,r0,r8 */
  203. 0x39080020, /* addi r8,r8,32 */
  204. 0x7fa049ce, /* stvx v29,r0,r9 */
  205. 0x39290020, /* addi r9,r9,32 */
  206. 0x7fc041ce, /* stvx v30,r0,r8 */
  207. 0x7fe049ce, /* stvx v31,r0,r9 */
  208. 0x80a30034, /*2:lwz r5,52(r3) */
  209. 0x39030180, /* addi r8,r3,384 */
  210. 0x39230190, /* addi r9,r3,400 */
  211. 0x70a00fff, /* andi. r0,r5,4095 */
  212. 0x7ca043a6, /* mtvrsave r5 */
  213. 0x4d820420, /* beqctr */
  214. 0x7e8040ce, /* lvx v20,r0,r8 */
  215. 0x39080020, /* addi r8,r8,32 */
  216. 0x7ea048ce, /* lvx v21,r0,r9 */
  217. 0x39290020, /* addi r9,r9,32 */
  218. 0x7ec040ce, /* lvx v22,r0,r8 */
  219. 0x39080020, /* addi r8,r8,32 */
  220. 0x7ee048ce, /* lvx v23,r0,r9 */
  221. 0x39290020, /* addi r9,r9,32 */
  222. 0x7f0040ce, /* lvx v24,r0,r8 */
  223. 0x39080020, /* addi r8,r8,32 */
  224. 0x7f2048ce, /* lvx v25,r0,r9 */
  225. 0x39290020, /* addi r9,r9,32 */
  226. 0x7f4040ce, /* lvx v26,r0,r8 */
  227. 0x39080020, /* addi r8,r8,32 */
  228. 0x7f6048ce, /* lvx v27,r0,r9 */
  229. 0x39290020, /* addi r9,r9,32 */
  230. 0x7f8040ce, /* lvx v28,r0,r8 */
  231. 0x39080020, /* addi r8,r8,32 */
  232. 0x7fa048ce, /* lvx v29,r0,r9 */
  233. 0x39290020, /* addi r9,r9,32 */
  234. 0x7fc040ce, /* lvx v30,r0,r8 */
  235. 0x7fe048ce, /* lvx v31,r0,r9 */
  236. #endif
  237. 0x4e800420, /* bctr */
  238. };
  239. #if LIBCO_PPCDESC
  240. /* function call goes through indirect descriptor */
  241. #define CO_SWAP_ASM(x, y) ((void (*)(cothread_t, cothread_t))(uintptr_t)x)(x, y)
  242. #else
  243. /* function call goes directly to code */
  244. #define CO_SWAP_ASM(x, y) ((void (*)(cothread_t, cothread_t))(uintptr_t)libco_ppc_code)(x, y)
  245. #endif
  246. static uint32_t* co_derive_(void* memory, unsigned size, uintptr_t entry) {
  247. (void)entry;
  248. uint32_t* t = (uint32_t*)memory;
  249. #if LIBCO_PPCDESC
  250. if(t) {
  251. memcpy(t, (void*)entry, sizeof(void*) * 3); /* copy entry's descriptor */
  252. *(const void**)t = libco_ppc_code; /* set function pointer to swap routine */
  253. }
  254. #endif
  255. return t;
  256. }
  257. cothread_t co_derive(void* memory, unsigned int size, void (*entry_)(void)) {
  258. uintptr_t entry = (uintptr_t)entry_;
  259. uint32_t* t = 0;
  260. /* be sure main thread was successfully allocated */
  261. if(co_active()) {
  262. t = co_derive_(memory, size, entry);
  263. }
  264. if(t) {
  265. uintptr_t sp;
  266. int shift;
  267. /* save current registers into new thread, so that any special ones will have proper values when thread is begun */
  268. CO_SWAP_ASM(t, t);
  269. #if LIBCO_PPCDESC
  270. entry = (uintptr_t)*(void**)entry; /* get real address */
  271. #endif
  272. /* put stack near end of block, and align */
  273. sp = (uintptr_t)t + size - above_stack;
  274. sp -= sp % stack_align;
  275. /* on PPC32, we save and restore GPRs as 32 bits. for PPC64, we
  276. save and restore them as 64 bits, regardless of the size the ABI
  277. uses. so, we manually write pointers at the proper size. we always
  278. save and restore at the same address, and since PPC is big-endian,
  279. we must put the low byte first on PPC32. */
  280. /* if uintptr_t is 32 bits, >>32 is undefined behavior,
  281. so we do two shifts and don't have to care how many bits uintptr_t is. */
  282. #if LIBCO_PPC64
  283. shift = 16;
  284. #else
  285. shift = 0;
  286. #endif
  287. /* set up so entry will be called on next swap */
  288. t[ 8] = (uint32_t)(entry >> shift >> shift);
  289. t[ 9] = (uint32_t)entry;
  290. t[10] = (uint32_t)(sp >> shift >> shift);
  291. t[11] = (uint32_t)sp;
  292. }
  293. return t;
  294. }
  295. static uint32_t* co_create_(unsigned size, uintptr_t entry) {
  296. (void)entry;
  297. uint32_t* t = (uint32_t*)LIBCO_MALLOC(size);
  298. #if LIBCO_PPCDESC
  299. if(t) {
  300. memcpy(t, (void*)entry, sizeof(void*) * 3); /* copy entry's descriptor */
  301. *(const void**)t = libco_ppc_code; /* set function pointer to swap routine */
  302. }
  303. #endif
  304. return t;
  305. }
  306. cothread_t co_create(unsigned int size, void (*entry_)(void)) {
  307. uintptr_t entry = (uintptr_t)entry_;
  308. uint32_t* t = 0;
  309. /* be sure main thread was successfully allocated */
  310. if(co_active()) {
  311. size += state_size + above_stack + stack_align;
  312. t = co_create_(size, entry);
  313. }
  314. if(t) {
  315. uintptr_t sp;
  316. int shift;
  317. /* save current registers into new thread, so that any special ones will have proper values when thread is begun */
  318. CO_SWAP_ASM(t, t);
  319. #if LIBCO_PPCDESC
  320. entry = (uintptr_t)*(void**)entry; /* get real address */
  321. #endif
  322. /* put stack near end of block, and align */
  323. sp = (uintptr_t)t + size - above_stack;
  324. sp -= sp % stack_align;
  325. /* on PPC32, we save and restore GPRs as 32 bits. for PPC64, we
  326. save and restore them as 64 bits, regardless of the size the ABI
  327. uses. so, we manually write pointers at the proper size. we always
  328. save and restore at the same address, and since PPC is big-endian,
  329. we must put the low byte first on PPC32. */
  330. /* if uintptr_t is 32 bits, >>32 is undefined behavior,
  331. so we do two shifts and don't have to care how many bits uintptr_t is. */
  332. #if LIBCO_PPC64
  333. shift = 16;
  334. #else
  335. shift = 0;
  336. #endif
  337. /* set up so entry will be called on next swap */
  338. t[ 8] = (uint32_t)(entry >> shift >> shift);
  339. t[ 9] = (uint32_t)entry;
  340. t[10] = (uint32_t)(sp >> shift >> shift);
  341. t[11] = (uint32_t)sp;
  342. }
  343. return t;
  344. }
  345. void co_delete(cothread_t t) {
  346. LIBCO_FREE(t);
  347. }
  348. static void co_init_(void) {
  349. #if LIBCO_MPROTECT
  350. long page_size = sysconf(_SC_PAGESIZE);
  351. if(page_size > 0) {
  352. uintptr_t align = page_size;
  353. uintptr_t begin = (uintptr_t)libco_ppc_code;
  354. uintptr_t end = begin + sizeof libco_ppc_code;
  355. /* align beginning and end */
  356. end += align - 1;
  357. end -= end % align;
  358. begin -= begin % align;
  359. mprotect((void*)begin, end - begin, PROT_READ | PROT_EXEC);
  360. }
  361. #endif
  362. co_active_handle = co_create_(state_size, (uintptr_t)&co_switch);
  363. }
  364. cothread_t co_active() {
  365. if(!co_active_handle) co_init_();
  366. return co_active_handle;
  367. }
  368. void co_switch(cothread_t t) {
  369. cothread_t old = co_active_handle;
  370. co_active_handle = t;
  371. CO_SWAP_ASM(t, old);
  372. }
  373. int co_serializable() {
  374. return 0;
  375. }