x86.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #if defined(__clang__) || defined(__GNUC__)
  10. #define fastcall __attribute__((fastcall))
  11. #elif defined(_MSC_VER)
  12. #define fastcall __fastcall
  13. #else
  14. #error "libco: please define fastcall macro"
  15. #endif
  16. static thread_local long co_active_buffer[64];
  17. static thread_local cothread_t co_active_handle = 0;
  18. static void (fastcall *co_swap)(cothread_t, cothread_t) = 0;
  19. #ifdef LIBCO_MPROTECT
  20. alignas(4096)
  21. #else
  22. section(text)
  23. #endif
  24. /* ABI: fastcall */
  25. static const unsigned char co_swap_function[4096] = {
  26. 0x89, 0x22, /* mov [edx],esp */
  27. 0x8b, 0x21, /* mov esp,[ecx] */
  28. 0x58, /* pop eax */
  29. 0x89, 0x6a, 0x04, /* mov [edx+ 4],ebp */
  30. 0x89, 0x72, 0x08, /* mov [edx+ 8],esi */
  31. 0x89, 0x7a, 0x0c, /* mov [edx+12],edi */
  32. 0x89, 0x5a, 0x10, /* mov [edx+16],ebx */
  33. 0x8b, 0x69, 0x04, /* mov ebp,[ecx+ 4] */
  34. 0x8b, 0x71, 0x08, /* mov esi,[ecx+ 8] */
  35. 0x8b, 0x79, 0x0c, /* mov edi,[ecx+12] */
  36. 0x8b, 0x59, 0x10, /* mov ebx,[ecx+16] */
  37. 0xff, 0xe0, /* jmp eax */
  38. };
  39. #ifdef _WIN32
  40. #include <windows.h>
  41. static void co_init() {
  42. #ifdef LIBCO_MPROTECT
  43. DWORD old_privileges;
  44. VirtualProtect((void*)co_swap_function, sizeof co_swap_function, PAGE_EXECUTE_READ, &old_privileges);
  45. #endif
  46. }
  47. #else
  48. #ifdef LIBCO_MPROTECT
  49. #include <unistd.h>
  50. #include <sys/mman.h>
  51. #endif
  52. static void co_init() {
  53. #ifdef LIBCO_MPROTECT
  54. unsigned long addr = (unsigned long)co_swap_function;
  55. unsigned long base = addr - (addr % sysconf(_SC_PAGESIZE));
  56. unsigned long size = (addr - base) + sizeof co_swap_function;
  57. mprotect((void*)base, size, PROT_READ | PROT_EXEC);
  58. #endif
  59. }
  60. #endif
  61. static void crash() {
  62. assert(0); /* called only if cothread_t entrypoint returns */
  63. }
  64. cothread_t co_active() {
  65. if(!co_active_handle) co_active_handle = &co_active_buffer;
  66. return co_active_handle;
  67. }
  68. cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) {
  69. cothread_t handle;
  70. if(!co_swap) {
  71. co_init();
  72. co_swap = (void (fastcall*)(cothread_t, cothread_t))co_swap_function;
  73. }
  74. if(!co_active_handle) co_active_handle = &co_active_buffer;
  75. if(handle = (cothread_t)memory) {
  76. unsigned int offset = (size & ~15) - 32;
  77. long *p = (long*)((char*)handle + offset); /* seek to top of stack */
  78. *--p = (long)crash; /* crash if entrypoint returns */
  79. *--p = (long)entrypoint; /* start of function */
  80. *(long*)handle = (long)p; /* stack pointer */
  81. }
  82. return handle;
  83. }
  84. cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
  85. void* memory = malloc(size);
  86. if(!memory) return (cothread_t)0;
  87. return co_derive(memory, size, entrypoint);
  88. }
  89. void co_delete(cothread_t handle) {
  90. free(handle);
  91. }
  92. void co_switch(cothread_t handle) {
  93. register cothread_t co_previous_handle = co_active_handle;
  94. co_swap(co_active_handle = handle, co_previous_handle);
  95. }
  96. int co_serializable() {
  97. return 1;
  98. }
  99. #ifdef __cplusplus
  100. }
  101. #endif