gchelper_generic.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include "py/mpstate.h"
  28. #include "py/gc.h"
  29. #include "shared/runtime/gchelper.h"
  30. #if MICROPY_ENABLE_GC
  31. // Even if we have specific support for an architecture, it is
  32. // possible to force use of setjmp-based implementation.
  33. #if !MICROPY_GCREGS_SETJMP
  34. // We capture here callee-save registers, i.e. ones which may contain
  35. // interesting values held there by our callers. It doesn't make sense
  36. // to capture caller-saved registers, because they, well, put on the
  37. // stack already by the caller.
  38. #if defined(__x86_64__)
  39. static void gc_helper_get_regs(gc_helper_regs_t arr) {
  40. register long rbx asm ("rbx");
  41. register long rbp asm ("rbp");
  42. register long r12 asm ("r12");
  43. register long r13 asm ("r13");
  44. register long r14 asm ("r14");
  45. register long r15 asm ("r15");
  46. #ifdef __clang__
  47. // TODO:
  48. // This is dirty workaround for Clang. It tries to get around
  49. // uncompliant (wrt to GCC) behavior of handling register variables.
  50. // Application of this patch here is random, and done only to unbreak
  51. // MacOS build. Better, cross-arch ways to deal with Clang issues should
  52. // be found.
  53. asm ("" : "=r" (rbx));
  54. asm ("" : "=r" (rbp));
  55. asm ("" : "=r" (r12));
  56. asm ("" : "=r" (r13));
  57. asm ("" : "=r" (r14));
  58. asm ("" : "=r" (r15));
  59. #endif
  60. arr[0] = rbx;
  61. arr[1] = rbp;
  62. arr[2] = r12;
  63. arr[3] = r13;
  64. arr[4] = r14;
  65. arr[5] = r15;
  66. }
  67. #elif defined(__i386__)
  68. static void gc_helper_get_regs(gc_helper_regs_t arr) {
  69. register long ebx asm ("ebx");
  70. register long esi asm ("esi");
  71. register long edi asm ("edi");
  72. register long ebp asm ("ebp");
  73. #ifdef __clang__
  74. // TODO:
  75. // This is dirty workaround for Clang. It tries to get around
  76. // uncompliant (wrt to GCC) behavior of handling register variables.
  77. // Application of this patch here is random, and done only to unbreak
  78. // MacOS build. Better, cross-arch ways to deal with Clang issues should
  79. // be found.
  80. asm ("" : "=r" (ebx));
  81. asm ("" : "=r" (esi));
  82. asm ("" : "=r" (edi));
  83. asm ("" : "=r" (ebp));
  84. #endif
  85. arr[0] = ebx;
  86. arr[1] = esi;
  87. arr[2] = edi;
  88. arr[3] = ebp;
  89. }
  90. #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
  91. // Fallback implementation, prefer gchelper_thumb1.s or gchelper_thumb2.s
  92. static void gc_helper_get_regs(gc_helper_regs_t arr) {
  93. register long r4 asm ("r4");
  94. register long r5 asm ("r5");
  95. register long r6 asm ("r6");
  96. register long r7 asm ("r7");
  97. register long r8 asm ("r8");
  98. register long r9 asm ("r9");
  99. register long r10 asm ("r10");
  100. register long r11 asm ("r11");
  101. register long r12 asm ("r12");
  102. register long r13 asm ("r13");
  103. arr[0] = r4;
  104. arr[1] = r5;
  105. arr[2] = r6;
  106. arr[3] = r7;
  107. arr[4] = r8;
  108. arr[5] = r9;
  109. arr[6] = r10;
  110. arr[7] = r11;
  111. arr[8] = r12;
  112. arr[9] = r13;
  113. }
  114. #elif defined(__aarch64__)
  115. static void gc_helper_get_regs(gc_helper_regs_t arr) {
  116. const register long x19 asm ("x19");
  117. const register long x20 asm ("x20");
  118. const register long x21 asm ("x21");
  119. const register long x22 asm ("x22");
  120. const register long x23 asm ("x23");
  121. const register long x24 asm ("x24");
  122. const register long x25 asm ("x25");
  123. const register long x26 asm ("x26");
  124. const register long x27 asm ("x27");
  125. const register long x28 asm ("x28");
  126. const register long x29 asm ("x29");
  127. arr[0] = x19;
  128. arr[1] = x20;
  129. arr[2] = x21;
  130. arr[3] = x22;
  131. arr[4] = x23;
  132. arr[5] = x24;
  133. arr[6] = x25;
  134. arr[7] = x26;
  135. arr[8] = x27;
  136. arr[9] = x28;
  137. arr[10] = x29;
  138. }
  139. #else
  140. #error "Architecture not supported for gc_helper_get_regs. Set MICROPY_GCREGS_SETJMP to use the fallback implementation."
  141. #endif
  142. #else // !MICROPY_GCREGS_SETJMP
  143. // Even if we have specific support for an architecture, it is
  144. // possible to force use of setjmp-based implementation.
  145. static void gc_helper_get_regs(gc_helper_regs_t arr) {
  146. setjmp(arr);
  147. }
  148. #endif // MICROPY_GCREGS_SETJMP
  149. // Explicitly mark this as noinline to make sure the regs variable
  150. // is effectively at the top of the stack: otherwise, in builds where
  151. // LTO is enabled and a lot of inlining takes place we risk a stack
  152. // layout where regs is lower on the stack than pointers which have
  153. // just been allocated but not yet marked, and get incorrectly sweeped.
  154. MP_NOINLINE void gc_helper_collect_regs_and_stack(void) {
  155. gc_helper_regs_t regs;
  156. gc_helper_get_regs(regs);
  157. // GC stack (and regs because we captured them)
  158. void **regs_ptr = (void **)(void *)&regs;
  159. gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)&regs) / sizeof(uintptr_t));
  160. }
  161. #endif // MICROPY_ENABLE_GC