aarch64.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #define LIBCO_C
  2. #include "libco.h"
  3. #include "settings.h"
  4. #include <assert.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #ifdef LIBCO_MPROTECT
  8. #include <unistd.h>
  9. #include <sys/mman.h>
  10. #endif
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. static thread_local unsigned long co_active_buffer[64];
  15. static thread_local cothread_t co_active_handle = 0;
  16. static void (*co_swap)(cothread_t, cothread_t) = 0;
  17. #ifdef LIBCO_MPROTECT
  18. alignas(4096)
  19. #else
  20. section(text)
  21. #endif
  22. static const uint32_t co_swap_function[1024] = {
  23. 0xa9002428, /* stp x8,x9,[x1] */
  24. 0xa9012c2a, /* stp x10,x11,[x1,#16] */
  25. 0xa902342c, /* stp x12,x13,[x1,#32] */
  26. 0xa9033c2e, /* stp x14,x15,[x1,#48] */
  27. 0xf9002433, /* str x19,[x1,#72] */
  28. 0xa9055434, /* stp x20,x21,[x1,#80] */
  29. 0xa9065c36, /* stp x22,x23,[x1,#96] */
  30. 0xa9076438, /* stp x24,x25,[x1,#112] */
  31. 0xa9086c3a, /* stp x26,x27,[x1,#128] */
  32. 0xa909743c, /* stp x28,x29,[x1,#144] */
  33. 0x910003f0, /* mov x16,sp */
  34. 0xa90a7830, /* stp x16,x30,[x1,#160] */
  35. 0xa9402408, /* ldp x8,x9,[x0] */
  36. 0xa9412c0a, /* ldp x10,x11,[x0,#16] */
  37. 0xa942340c, /* ldp x12,x13,[x0,#32] */
  38. 0xa9433c0e, /* ldp x14,x15,[x0,#48] */
  39. 0xf9402413, /* ldr x19,[x0,#72] */
  40. 0xa9455414, /* ldp x20,x21,[x0,#80] */
  41. 0xa9465c16, /* ldp x22,x23,[x0,#96] */
  42. 0xa9476418, /* ldp x24,x25,[x0,#112] */
  43. 0xa9486c1a, /* ldp x26,x27,[x0,#128] */
  44. 0xa949741c, /* ldp x28,x29,[x0,#144] */
  45. 0xa94a4410, /* ldp x16,x17,[x0,#160] */
  46. 0x9100021f, /* mov sp,x16 */
  47. 0xd61f0220, /* br x17 */
  48. };
  49. static void co_init() {
  50. #ifdef LIBCO_MPROTECT
  51. unsigned long addr = (unsigned long)co_swap_function;
  52. unsigned long base = addr - (addr % sysconf(_SC_PAGESIZE));
  53. unsigned long size = (addr - base) + sizeof co_swap_function;
  54. mprotect((void*)base, size, PROT_READ | PROT_EXEC);
  55. #endif
  56. }
  57. cothread_t co_active() {
  58. if(!co_active_handle) co_active_handle = &co_active_buffer;
  59. return co_active_handle;
  60. }
  61. cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) {
  62. unsigned long* handle;
  63. if(!co_swap) {
  64. co_init();
  65. co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
  66. }
  67. if(!co_active_handle) co_active_handle = &co_active_buffer;
  68. if(handle = (unsigned long*)memory) {
  69. unsigned int offset = (size & ~15);
  70. unsigned long* p = (unsigned long*)((unsigned char*)handle + offset);
  71. handle[19] = (unsigned long)p; /* x29 (frame pointer) */
  72. handle[20] = (unsigned long)p; /* x30 (stack pointer) */
  73. handle[21] = (unsigned long)entrypoint; /* x31 (link register) */
  74. }
  75. return handle;
  76. }
  77. cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
  78. void* memory = malloc(size);
  79. if(!memory) return (cothread_t)0;
  80. return co_derive(memory, size, entrypoint);
  81. }
  82. void co_delete(cothread_t handle) {
  83. free(handle);
  84. }
  85. void co_switch(cothread_t handle) {
  86. cothread_t co_previous_handle = co_active_handle;
  87. co_swap(co_active_handle = handle, co_previous_handle);
  88. }
  89. int co_serializable() {
  90. return 1;
  91. }
  92. #ifdef __cplusplus
  93. }
  94. #endif