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
27 #include <stdio.h>
28
29 #include "py/mpstate.h"
30 #include "py/gc.h"
31 #include "shared/runtime/gchelper.h"
32
33 #if MICROPY_ENABLE_GC
34
35 // Even if we have specific support for an architecture, it is
36 // possible to force use of setjmp-based implementation.
37 #if !MICROPY_GCREGS_SETJMP
38
39 // We capture here callee-save registers, i.e. ones which may contain
40 // interesting values held there by our callers. It doesn't make sense
41 // to capture caller-saved registers, because they, well, put on the
42 // stack already by the caller.
43 #if defined(__x86_64__)
44
gc_helper_get_regs(gc_helper_regs_t arr)45 STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
46 register long rbx asm ("rbx");
47 register long rbp asm ("rbp");
48 register long r12 asm ("r12");
49 register long r13 asm ("r13");
50 register long r14 asm ("r14");
51 register long r15 asm ("r15");
52 #ifdef __clang__
53 // TODO:
54 // This is dirty workaround for Clang. It tries to get around
55 // uncompliant (wrt to GCC) behavior of handling register variables.
56 // Application of this patch here is random, and done only to unbreak
57 // MacOS build. Better, cross-arch ways to deal with Clang issues should
58 // be found.
59 asm ("" : "=r" (rbx));
60 asm ("" : "=r" (rbp));
61 asm ("" : "=r" (r12));
62 asm ("" : "=r" (r13));
63 asm ("" : "=r" (r14));
64 asm ("" : "=r" (r15));
65 #endif
66 arr[0] = rbx;
67 arr[1] = rbp;
68 arr[2] = r12;
69 arr[3] = r13;
70 arr[4] = r14;
71 arr[5] = r15;
72 }
73
74 #elif defined(__i386__)
75
gc_helper_get_regs(gc_helper_regs_t arr)76 STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
77 register long ebx asm ("ebx");
78 register long esi asm ("esi");
79 register long edi asm ("edi");
80 register long ebp asm ("ebp");
81 #ifdef __clang__
82 // TODO:
83 // This is dirty workaround for Clang. It tries to get around
84 // uncompliant (wrt to GCC) behavior of handling register variables.
85 // Application of this patch here is random, and done only to unbreak
86 // MacOS build. Better, cross-arch ways to deal with Clang issues should
87 // be found.
88 asm ("" : "=r" (ebx));
89 asm ("" : "=r" (esi));
90 asm ("" : "=r" (edi));
91 asm ("" : "=r" (ebp));
92 #endif
93 arr[0] = ebx;
94 arr[1] = esi;
95 arr[2] = edi;
96 arr[3] = ebp;
97 }
98
99 #elif defined(__thumb2__) || defined(__thumb__) || defined(__arm__)
100
101 // Fallback implementation, prefer gchelper_m0.s or gchelper_m3.s
102
103 // Modified bt HaaS begin
104 #ifndef asm
105 #define asm __asm
106 #endif
107 // Modified bt HaaS end
108
gc_helper_get_regs(gc_helper_regs_t arr)109 STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
110 register long r4 asm ("r4");
111 register long r5 asm ("r5");
112 register long r6 asm ("r6");
113 register long r7 asm ("r7");
114 register long r8 asm ("r8");
115 register long r9 asm ("r9");
116 register long r10 asm ("r10");
117 register long r11 asm ("r11");
118 register long r12 asm ("r12");
119 register long r13 asm ("r13");
120 arr[0] = r4;
121 arr[1] = r5;
122 arr[2] = r6;
123 arr[3] = r7;
124 arr[4] = r8;
125 arr[5] = r9;
126 arr[6] = r10;
127 arr[7] = r11;
128 arr[8] = r12;
129 arr[9] = r13;
130 }
131
132 #elif defined(__aarch64__)
133
gc_helper_get_regs(gc_helper_regs_t arr)134 STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
135 const register long x19 asm ("x19");
136 const register long x20 asm ("x20");
137 const register long x21 asm ("x21");
138 const register long x22 asm ("x22");
139 const register long x23 asm ("x23");
140 const register long x24 asm ("x24");
141 const register long x25 asm ("x25");
142 const register long x26 asm ("x26");
143 const register long x27 asm ("x27");
144 const register long x28 asm ("x28");
145 const register long x29 asm ("x29");
146 arr[0] = x19;
147 arr[1] = x20;
148 arr[2] = x21;
149 arr[3] = x22;
150 arr[4] = x23;
151 arr[5] = x24;
152 arr[6] = x25;
153 arr[7] = x26;
154 arr[8] = x27;
155 arr[9] = x28;
156 arr[10] = x29;
157 }
158
159 #else
160
161 #error "Architecture not supported for gc_helper_get_regs. Set MICROPY_GCREGS_SETJMP to use the fallback implementation."
162
163 #endif
164
165 #else // !MICROPY_GCREGS_SETJMP
166
167 // Even if we have specific support for an architecture, it is
168 // possible to force use of setjmp-based implementation.
169
gc_helper_get_regs(gc_helper_regs_t arr)170 STATIC void gc_helper_get_regs(gc_helper_regs_t arr) {
171 setjmp(arr);
172 }
173
174 #endif // MICROPY_GCREGS_SETJMP
175
176 // Explicitly mark this as noinline to make sure the regs variable
177 // is effectively at the top of the stack: otherwise, in builds where
178 // LTO is enabled and a lot of inlining takes place we risk a stack
179 // layout where regs is lower on the stack than pointers which have
180 // just been allocated but not yet marked, and get incorrectly sweeped.
gc_helper_collect_regs_and_stack(void)181 MP_NOINLINE void gc_helper_collect_regs_and_stack(void) {
182 gc_helper_regs_t regs;
183 gc_helper_get_regs(regs);
184 // GC stack (and regs because we captured them)
185 void **regs_ptr = (void **)(void *)®s;
186 gc_collect_root(regs_ptr, ((uintptr_t)MP_STATE_THREAD(stack_top) - (uintptr_t)®s) / sizeof(uintptr_t));
187 }
188
189 #endif // MICROPY_ENABLE_GC
190