x86.c 3.1 KB

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