amd64.c 6.0 KB

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