ppc64v2.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* author: Shawn Anastasio */
  2. #define LIBCO_C
  3. #include "libco.h"
  4. #include "settings.h"
  5. #include <stdint.h>
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. struct ppc64_context {
  10. /* GPRs */
  11. uint64_t gprs[32];
  12. uint64_t lr;
  13. uint64_t ccr;
  14. /* FPRs */
  15. uint64_t fprs[32];
  16. #ifdef __ALTIVEC__
  17. /* Altivec (VMX) */
  18. uint64_t vmx[12 * 2];
  19. uint32_t vrsave;
  20. #endif
  21. };
  22. static thread_local struct ppc64_context* co_active_handle = 0;
  23. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  24. #define ALIGN(p, x) ((void*)((uintptr_t)(p) & ~((x) - 1)))
  25. #define MIN_STACK 0x10000lu
  26. #define MIN_STACK_FRAME 0x20lu
  27. #define STACK_ALIGN 0x10lu
  28. void swap_context(struct ppc64_context* read, struct ppc64_context* write);
  29. __asm__(
  30. ".text\n"
  31. ".align 4\n"
  32. ".type swap_context @function\n"
  33. "swap_context:\n"
  34. ".cfi_startproc\n"
  35. /* save GPRs */
  36. "std 1, 8(4)\n"
  37. "std 2, 16(4)\n"
  38. "std 12, 96(4)\n"
  39. "std 13, 104(4)\n"
  40. "std 14, 112(4)\n"
  41. "std 15, 120(4)\n"
  42. "std 16, 128(4)\n"
  43. "std 17, 136(4)\n"
  44. "std 18, 144(4)\n"
  45. "std 19, 152(4)\n"
  46. "std 20, 160(4)\n"
  47. "std 21, 168(4)\n"
  48. "std 22, 176(4)\n"
  49. "std 23, 184(4)\n"
  50. "std 24, 192(4)\n"
  51. "std 25, 200(4)\n"
  52. "std 26, 208(4)\n"
  53. "std 27, 216(4)\n"
  54. "std 28, 224(4)\n"
  55. "std 29, 232(4)\n"
  56. "std 30, 240(4)\n"
  57. "std 31, 248(4)\n"
  58. /* save LR */
  59. "mflr 5\n"
  60. "std 5, 256(4)\n"
  61. /* save CCR */
  62. "mfcr 5\n"
  63. "std 5, 264(4)\n"
  64. /* save FPRs */
  65. "stfd 14, 384(4)\n"
  66. "stfd 15, 392(4)\n"
  67. "stfd 16, 400(4)\n"
  68. "stfd 17, 408(4)\n"
  69. "stfd 18, 416(4)\n"
  70. "stfd 19, 424(4)\n"
  71. "stfd 20, 432(4)\n"
  72. "stfd 21, 440(4)\n"
  73. "stfd 22, 448(4)\n"
  74. "stfd 23, 456(4)\n"
  75. "stfd 24, 464(4)\n"
  76. "stfd 25, 472(4)\n"
  77. "stfd 26, 480(4)\n"
  78. "stfd 27, 488(4)\n"
  79. "stfd 28, 496(4)\n"
  80. "stfd 29, 504(4)\n"
  81. "stfd 30, 512(4)\n"
  82. "stfd 31, 520(4)\n"
  83. #ifdef __ALTIVEC__
  84. /* save VMX */
  85. "li 5, 528\n"
  86. "stvxl 20, 4, 5\n"
  87. "addi 5, 5, 16\n"
  88. "stvxl 21, 4, 5\n"
  89. "addi 5, 5, 16\n"
  90. "stvxl 22, 4, 5\n"
  91. "addi 5, 5, 16\n"
  92. "stvxl 23, 4, 5\n"
  93. "addi 5, 5, 16\n"
  94. "stvxl 24, 4, 5\n"
  95. "addi 5, 5, 16\n"
  96. "stvxl 25, 4, 5\n"
  97. "addi 5, 5, 16\n"
  98. "stvxl 26, 4, 5\n"
  99. "addi 5, 5, 16\n"
  100. "stvxl 27, 4, 5\n"
  101. "addi 5, 5, 16\n"
  102. "stvxl 28, 4, 5\n"
  103. "addi 5, 5, 16\n"
  104. "stvxl 29, 4, 5\n"
  105. "addi 5, 5, 16\n"
  106. "stvxl 30, 4, 5\n"
  107. "addi 5, 5, 16\n"
  108. "stvxl 31, 4, 5\n"
  109. "addi 5, 5, 16\n"
  110. /* save VRSAVE */
  111. "mfvrsave 5\n"
  112. "stw 5, 736(4)\n"
  113. #endif
  114. /* restore GPRs */
  115. "ld 1, 8(3)\n"
  116. "ld 2, 16(3)\n"
  117. "ld 12, 96(3)\n"
  118. "ld 13, 104(3)\n"
  119. "ld 14, 112(3)\n"
  120. "ld 15, 120(3)\n"
  121. "ld 16, 128(3)\n"
  122. "ld 17, 136(3)\n"
  123. "ld 18, 144(3)\n"
  124. "ld 19, 152(3)\n"
  125. "ld 20, 160(3)\n"
  126. "ld 21, 168(3)\n"
  127. "ld 22, 176(3)\n"
  128. "ld 23, 184(3)\n"
  129. "ld 24, 192(3)\n"
  130. "ld 25, 200(3)\n"
  131. "ld 26, 208(3)\n"
  132. "ld 27, 216(3)\n"
  133. "ld 28, 224(3)\n"
  134. "ld 29, 232(3)\n"
  135. "ld 30, 240(3)\n"
  136. "ld 31, 248(3)\n"
  137. /* restore LR */
  138. "ld 5, 256(3)\n"
  139. "mtlr 5\n"
  140. /* restore CCR */
  141. "ld 5, 264(3)\n"
  142. "mtcr 5\n"
  143. /* restore FPRs */
  144. "lfd 14, 384(3)\n"
  145. "lfd 15, 392(3)\n"
  146. "lfd 16, 400(3)\n"
  147. "lfd 17, 408(3)\n"
  148. "lfd 18, 416(3)\n"
  149. "lfd 19, 424(3)\n"
  150. "lfd 20, 432(3)\n"
  151. "lfd 21, 440(3)\n"
  152. "lfd 22, 448(3)\n"
  153. "lfd 23, 456(3)\n"
  154. "lfd 24, 464(3)\n"
  155. "lfd 25, 472(3)\n"
  156. "lfd 26, 480(3)\n"
  157. "lfd 27, 488(3)\n"
  158. "lfd 28, 496(3)\n"
  159. "lfd 29, 504(3)\n"
  160. "lfd 30, 512(3)\n"
  161. "lfd 31, 520(3)\n"
  162. #ifdef __ALTIVEC__
  163. /* restore VMX */
  164. "li 5, 528\n"
  165. "lvxl 20, 3, 5\n"
  166. "addi 5, 5, 16\n"
  167. "lvxl 21, 3, 5\n"
  168. "addi 5, 5, 16\n"
  169. "lvxl 22, 3, 5\n"
  170. "addi 5, 5, 16\n"
  171. "lvxl 23, 3, 5\n"
  172. "addi 5, 5, 16\n"
  173. "lvxl 24, 3, 5\n"
  174. "addi 5, 5, 16\n"
  175. "lvxl 25, 3, 5\n"
  176. "addi 5, 5, 16\n"
  177. "lvxl 26, 3, 5\n"
  178. "addi 5, 5, 16\n"
  179. "lvxl 27, 3, 5\n"
  180. "addi 5, 5, 16\n"
  181. "lvxl 28, 3, 5\n"
  182. "addi 5, 5, 16\n"
  183. "lvxl 29, 3, 5\n"
  184. "addi 5, 5, 16\n"
  185. "lvxl 30, 3, 5\n"
  186. "addi 5, 5, 16\n"
  187. "lvxl 31, 3, 5\n"
  188. "addi 5, 5, 16\n"
  189. /* restore VRSAVE */
  190. "lwz 5, 720(3)\n"
  191. "mtvrsave 5\n"
  192. #endif
  193. /* branch to LR */
  194. "blr\n"
  195. ".cfi_endproc\n"
  196. ".size swap_context, .-swap_context\n"
  197. );
  198. cothread_t co_active() {
  199. if(!co_active_handle) {
  200. co_active_handle = (struct ppc64_context*)LIBCO_MALLOC(MIN_STACK + sizeof(struct ppc64_context));
  201. }
  202. return (cothread_t)co_active_handle;
  203. }
  204. cothread_t co_derive(void* memory, unsigned int size, void (*coentry)(void)) {
  205. uint8_t* sp;
  206. struct ppc64_context* context = (struct ppc64_context*)memory;
  207. /* save current context into new context to initialize it */
  208. swap_context(context, context);
  209. /* align stack */
  210. sp = (uint8_t*)memory + size - STACK_ALIGN;
  211. sp = (uint8_t*)ALIGN(sp, STACK_ALIGN);
  212. /* write 0 for initial backchain */
  213. *(uint64_t*)sp = 0;
  214. /* create new frame with backchain */
  215. sp -= MIN_STACK_FRAME;
  216. *(uint64_t*)sp = (uint64_t)(sp + MIN_STACK_FRAME);
  217. /* update context with new stack (r1) and entrypoint (r12, lr) */
  218. context->gprs[ 1] = (uint64_t)sp;
  219. context->gprs[12] = (uint64_t)coentry;
  220. context->lr = (uint64_t)coentry;
  221. return (cothread_t)memory;
  222. }
  223. cothread_t co_create(unsigned int size, void (*coentry)(void)) {
  224. void* memory = LIBCO_MALLOC(size);
  225. if(!memory) return (cothread_t)0;
  226. return co_derive(memory, size, coentry);
  227. }
  228. void co_delete(cothread_t handle) {
  229. LIBCO_FREE(handle);
  230. }
  231. void co_switch(cothread_t to) {
  232. struct ppc64_context* from = co_active_handle;
  233. co_active_handle = (struct ppc64_context*)to;
  234. swap_context((struct ppc64_context*)to, from);
  235. }
  236. int co_serializable() {
  237. return 1;
  238. }
  239. #ifdef __cplusplus
  240. }
  241. #endif