aarch64.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #define LIBCO_C
  2. #include "libco.h"
  3. #include "settings.h"
  4. #include <stdint.h>
  5. #ifdef LIBCO_MPROTECT
  6. #include <unistd.h>
  7. #include <sys/mman.h>
  8. #endif
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. static thread_local unsigned long co_active_buffer[64];
  13. static thread_local cothread_t co_active_handle = 0;
  14. static void (*co_swap)(cothread_t, cothread_t) = 0;
  15. #ifdef LIBCO_MPROTECT
  16. alignas(4096)
  17. #else
  18. section(text)
  19. #endif
  20. static const uint32_t co_swap_function[1024] = {
  21. 0x910003f0, /* mov x16,sp */
  22. 0xa9007830, /* stp x16,x30,[x1] */
  23. 0xa9407810, /* ldp x16,x30,[x0] */
  24. 0x9100021f, /* mov sp,x16 */
  25. 0xa9015033, /* stp x19,x20,[x1, 16] */
  26. 0xa9415013, /* ldp x19,x20,[x0, 16] */
  27. 0xa9025835, /* stp x21,x22,[x1, 32] */
  28. 0xa9425815, /* ldp x21,x22,[x0, 32] */
  29. 0xa9036037, /* stp x23,x24,[x1, 48] */
  30. 0xa9436017, /* ldp x23,x24,[x0, 48] */
  31. 0xa9046839, /* stp x25,x26,[x1, 64] */
  32. 0xa9446819, /* ldp x25,x26,[x0, 64] */
  33. 0xa905703b, /* stp x27,x28,[x1, 80] */
  34. 0xa945701b, /* ldp x27,x28,[x0, 80] */
  35. 0xf900303d, /* str x29, [x1, 96] */
  36. 0xf940301d, /* ldr x29, [x0, 96] */
  37. 0x6d072428, /* stp d8, d9, [x1,112] */
  38. 0x6d472408, /* ldp d8, d9, [x0,112] */
  39. 0x6d082c2a, /* stp d10,d11,[x1,128] */
  40. 0x6d482c0a, /* ldp d10,d11,[x0,128] */
  41. 0x6d09342c, /* stp d12,d13,[x1,144] */
  42. 0x6d49340c, /* ldp d12,d13,[x0,144] */
  43. 0x6d0a3c2e, /* stp d14,d15,[x1,160] */
  44. 0x6d4a3c0e, /* ldp d14,d15,[x0,160] */
  45. 0xd61f03c0, /* br x30 */
  46. };
  47. static void co_init() {
  48. #ifdef LIBCO_MPROTECT
  49. unsigned long addr = (unsigned long)co_swap_function;
  50. unsigned long base = addr - (addr % sysconf(_SC_PAGESIZE));
  51. unsigned long size = (addr - base) + sizeof co_swap_function;
  52. mprotect((void*)base, size, PROT_READ | PROT_EXEC);
  53. #endif
  54. }
  55. cothread_t co_active() {
  56. if(!co_active_handle) co_active_handle = &co_active_buffer;
  57. return co_active_handle;
  58. }
  59. cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) {
  60. unsigned long* handle;
  61. if(!co_swap) {
  62. co_init();
  63. co_swap = (void (*)(cothread_t, cothread_t))co_swap_function;
  64. }
  65. if(!co_active_handle) co_active_handle = &co_active_buffer;
  66. if(handle = (unsigned long*)memory) {
  67. unsigned int offset = (size & ~15);
  68. unsigned long* p = (unsigned long*)((unsigned char*)handle + offset);
  69. handle[0] = (unsigned long)p; /* x16 (stack pointer) */
  70. handle[1] = (unsigned long)entrypoint; /* x30 (link register) */
  71. handle[12] = (unsigned long)p; /* x29 (frame pointer) */
  72. }
  73. return handle;
  74. }
  75. cothread_t co_create(unsigned int size, void (*entrypoint)(void)) {
  76. void* memory = LIBCO_MALLOC(size);
  77. if(!memory) return (cothread_t)0;
  78. return co_derive(memory, size, entrypoint);
  79. }
  80. void co_delete(cothread_t handle) {
  81. LIBCO_FREE(handle);
  82. }
  83. void co_switch(cothread_t handle) {
  84. cothread_t co_previous_handle = co_active_handle;
  85. co_swap(co_active_handle = handle, co_previous_handle);
  86. }
  87. int co_serializable() {
  88. return 1;
  89. }
  90. #ifdef __cplusplus
  91. }
  92. #endif