amd64.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #define LIBCO_C
  2. #include "libco.h"
  3. #include "settings.h"
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. static thread_local long long co_active_buffer[64];
  8. static thread_local cothread_t co_active_handle = 0;
  9. static void (*co_swap)(cothread_t, cothread_t) = 0;
  10. #ifdef LIBCO_MPROTECT
  11. alignas(4096)
  12. #else
  13. section(text)
  14. #endif
  15. #ifdef _WIN32
  16. /* ABI: Win64 */
  17. static const unsigned char co_swap_function[4096] = {
  18. 0x48, 0x89, 0x22, /* mov [rdx],rsp */
  19. 0x48, 0x8b, 0x21, /* mov rsp,[rcx] */
  20. 0x58, /* pop rax */
  21. 0x48, 0x89, 0x6a, 0x08, /* mov [rdx+ 8],rbp */
  22. 0x48, 0x89, 0x72, 0x10, /* mov [rdx+16],rsi */
  23. 0x48, 0x89, 0x7a, 0x18, /* mov [rdx+24],rdi */
  24. 0x48, 0x89, 0x5a, 0x20, /* mov [rdx+32],rbx */
  25. 0x4c, 0x89, 0x62, 0x28, /* mov [rdx+40],r12 */
  26. 0x4c, 0x89, 0x6a, 0x30, /* mov [rdx+48],r13 */
  27. 0x4c, 0x89, 0x72, 0x38, /* mov [rdx+56],r14 */
  28. 0x4c, 0x89, 0x7a, 0x40, /* mov [rdx+64],r15 */
  29. #if !defined(LIBCO_NO_SSE)
  30. 0x0f, 0x29, 0x72, 0x50, /* movaps [rdx+ 80],xmm6 */
  31. 0x0f, 0x29, 0x7a, 0x60, /* movaps [rdx+ 96],xmm7 */
  32. 0x44, 0x0f, 0x29, 0x42, 0x70, /* movaps [rdx+112],xmm8 */
  33. 0x48, 0x83, 0xc2, 0x70, /* add rdx,112 */
  34. 0x44, 0x0f, 0x29, 0x4a, 0x10, /* movaps [rdx+ 16],xmm9 */
  35. 0x44, 0x0f, 0x29, 0x52, 0x20, /* movaps [rdx+ 32],xmm10 */
  36. 0x44, 0x0f, 0x29, 0x5a, 0x30, /* movaps [rdx+ 48],xmm11 */
  37. 0x44, 0x0f, 0x29, 0x62, 0x40, /* movaps [rdx+ 64],xmm12 */
  38. 0x44, 0x0f, 0x29, 0x6a, 0x50, /* movaps [rdx+ 80],xmm13 */
  39. 0x44, 0x0f, 0x29, 0x72, 0x60, /* movaps [rdx+ 96],xmm14 */
  40. 0x44, 0x0f, 0x29, 0x7a, 0x70, /* movaps [rdx+112],xmm15 */
  41. #endif
  42. 0x48, 0x8b, 0x69, 0x08, /* mov rbp,[rcx+ 8] */
  43. 0x48, 0x8b, 0x71, 0x10, /* mov rsi,[rcx+16] */
  44. 0x48, 0x8b, 0x79, 0x18, /* mov rdi,[rcx+24] */
  45. 0x48, 0x8b, 0x59, 0x20, /* mov rbx,[rcx+32] */
  46. 0x4c, 0x8b, 0x61, 0x28, /* mov r12,[rcx+40] */
  47. 0x4c, 0x8b, 0x69, 0x30, /* mov r13,[rcx+48] */
  48. 0x4c, 0x8b, 0x71, 0x38, /* mov r14,[rcx+56] */
  49. 0x4c, 0x8b, 0x79, 0x40, /* mov r15,[rcx+64] */
  50. #if !defined(LIBCO_NO_SSE)
  51. 0x0f, 0x28, 0x71, 0x50, /* movaps xmm6, [rcx+ 80] */
  52. 0x0f, 0x28, 0x79, 0x60, /* movaps xmm7, [rcx+ 96] */
  53. 0x44, 0x0f, 0x28, 0x41, 0x70, /* movaps xmm8, [rcx+112] */
  54. 0x48, 0x83, 0xc1, 0x70, /* add rcx,112 */
  55. 0x44, 0x0f, 0x28, 0x49, 0x10, /* movaps xmm9, [rcx+ 16] */
  56. 0x44, 0x0f, 0x28, 0x51, 0x20, /* movaps xmm10,[rcx+ 32] */
  57. 0x44, 0x0f, 0x28, 0x59, 0x30, /* movaps xmm11,[rcx+ 48] */
  58. 0x44, 0x0f, 0x28, 0x61, 0x40, /* movaps xmm12,[rcx+ 64] */
  59. 0x44, 0x0f, 0x28, 0x69, 0x50, /* movaps xmm13,[rcx+ 80] */
  60. 0x44, 0x0f, 0x28, 0x71, 0x60, /* movaps xmm14,[rcx+ 96] */
  61. 0x44, 0x0f, 0x28, 0x79, 0x70, /* movaps xmm15,[rcx+112] */
  62. #endif
  63. 0xff, 0xe0, /* jmp rax */
  64. };
  65. #include <windows.h>
  66. static void co_init() {
  67. #ifdef LIBCO_MPROTECT
  68. DWORD old_privileges;
  69. VirtualProtect((void*)co_swap_function, sizeof co_swap_function, PAGE_EXECUTE_READ, &old_privileges);
  70. #endif
  71. }
  72. #else
  73. /* ABI: SystemV */
  74. static const unsigned char co_swap_function[4096] = {
  75. 0x48, 0x89, 0x26, /* mov [rsi],rsp */
  76. 0x48, 0x8b, 0x27, /* mov rsp,[rdi] */
  77. 0x58, /* pop rax */
  78. 0x48, 0x89, 0x6e, 0x08, /* mov [rsi+ 8],rbp */
  79. 0x48, 0x89, 0x5e, 0x10, /* mov [rsi+16],rbx */
  80. 0x4c, 0x89, 0x66, 0x18, /* mov [rsi+24],r12 */
  81. 0x4c, 0x89, 0x6e, 0x20, /* mov [rsi+32],r13 */
  82. 0x4c, 0x89, 0x76, 0x28, /* mov [rsi+40],r14 */
  83. 0x4c, 0x89, 0x7e, 0x30, /* mov [rsi+48],r15 */
  84. 0x48, 0x8b, 0x6f, 0x08, /* mov rbp,[rdi+ 8] */
  85. 0x48, 0x8b, 0x5f, 0x10, /* mov rbx,[rdi+16] */
  86. 0x4c, 0x8b, 0x67, 0x18, /* mov r12,[rdi+24] */
  87. 0x4c, 0x8b, 0x6f, 0x20, /* mov r13,[rdi+32] */
  88. 0x4c, 0x8b, 0x77, 0x28, /* mov r14,[rdi+40] */
  89. 0x4c, 0x8b, 0x7f, 0x30, /* mov r15,[rdi+48] */
  90. 0xff, 0xe0, /* jmp rax */
  91. };
  92. #ifdef LIBCO_MPROTECT
  93. #include <unistd.h>
  94. #include <sys/mman.h>
  95. #endif
  96. static void co_init() {
  97. #ifdef LIBCO_MPROTECT
  98. unsigned long long addr = (unsigned long long)co_swap_function;
  99. unsigned long long base = addr - (addr % sysconf(_SC_PAGESIZE));
  100. unsigned long long size = (addr - base) + sizeof co_swap_function;
  101. mprotect((void*)base, size, PROT_READ | PROT_EXEC);
  102. #endif
  103. }
  104. #endif
  105. static void crash() {
  106. LIBCO_ASSERT(0); /* called only if cothread_t entrypoint returns */
  107. }
  108. cothread_t co_active() {
  109. if(!co_active_handle) co_active_handle = &co_active_buffer;
  110. return co_active_handle;
  111. }
  112. cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) {
  113. cothread_t handle;
  114. if(!co_swap) {
  115. co_init();
  116. co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
  117. }
  118. if(!co_active_handle) co_active_handle = &co_active_buffer;
  119. if(handle = (cothread_t)memory) {
  120. unsigned int offset = (size & ~15) - 32;
  121. long long *p = (long long*)((char*)handle + offset); /* seek to top of stack */
  122. *--p = (long long)crash; /* crash if entrypoint returns */
  123. *--p = (long long)entrypoint; /* start of function */
  124. *(long long*)handle = (long long)p; /* stack pointer */
  125. }
  126. return handle;
  127. }
  128. cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
  129. void* memory = LIBCO_MALLOC(size);
  130. if(!memory) return (cothread_t)0;
  131. return co_derive(memory, size, entrypoint);
  132. }
  133. void co_delete(cothread_t handle) {
  134. LIBCO_FREE(handle);
  135. }
  136. void co_switch(cothread_t handle) {
  137. register cothread_t co_previous_handle = co_active_handle;
  138. co_swap(co_active_handle = handle, co_previous_handle);
  139. }
  140. int co_serializable() {
  141. return 1;
  142. }
  143. #ifdef __cplusplus
  144. }
  145. #endif