Debian Codegraph @

gc/mach_dep.c

raw
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 *
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 *
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
13 */
14
15 #include "private/gc_priv.h"
16
17 #if !defined(PLATFORM_MACH_DEP) && !defined(SN_TARGET_PSP2)
18
19 #include <stdio.h>
20
21 #ifdef AMIGA
22 # ifndef __GNUC__
23 # include <dos.h>
24 # else
25 # include <machine/reg.h>
26 # endif
27 #endif
28
29 #ifdef E2K
30 # include <errno.h>
31 # include <asm/e2k_syswork.h>
32 # include <sys/mman.h>
33 # include <sys/syscall.h>
34
35 GC_INNER size_t GC_get_procedure_stack(ptr_t buf, size_t buf_sz) {
36 word new_sz;
37
38 GC_ASSERT(0 == buf_sz || buf != NULL);
39 for (;;) {
40 word stack_ofs;
41
42 new_sz = 0;
43 if (syscall(__NR_access_hw_stacks, E2K_GET_PROCEDURE_STACK_SIZE,
44 NULL, NULL, 0, &new_sz) == -1) {
45 if (errno != EAGAIN)
46 ABORT_ARG1("Cannot get size of procedure stack",
47 ": errno= %d", errno);
48 continue;
49 }
50 GC_ASSERT(new_sz > 0 && new_sz % sizeof(word) == 0);
51 if (new_sz > buf_sz)
52 break;
53 /* Immediately read the stack right after checking its size. */
54 stack_ofs = 0;
55 if (syscall(__NR_access_hw_stacks, E2K_READ_PROCEDURE_STACK_EX,
56 &stack_ofs, buf, new_sz, NULL) != -1)
57 break;
58 if (errno != EAGAIN)
59 ABORT_ARG2("Cannot read procedure stack",
60 ": new_sz= %lu, errno= %d", (unsigned long)new_sz, errno);
61 }
62 return (size_t)new_sz;
63 }
64
65 ptr_t GC_save_regs_in_stack(void) {
66 __asm__ __volatile__ ("flushr");
67 return NULL;
68 }
69
70 GC_INNER ptr_t GC_mmap_procedure_stack_buf(size_t aligned_sz)
71 {
72 void *buf = mmap(NULL, aligned_sz, PROT_READ | PROT_WRITE,
73 MAP_PRIVATE | MAP_ANON, 0 /* fd */, 0 /* offset */);
74 if (MAP_FAILED == buf)
75 ABORT_ARG2("Could not map memory for procedure stack",
76 ": requested %lu bytes, errno= %d",
77 (unsigned long)aligned_sz, errno);
78 return (ptr_t)buf;
79 }
80
81 GC_INNER void GC_unmap_procedure_stack_buf(ptr_t buf, size_t sz)
82 {
83 if (munmap(buf, ROUNDUP_PAGESIZE(sz)) == -1)
84 ABORT_ARG1("munmap failed (for procedure stack space)",
85 ": errno= %d", errno);
86 }
87
88 # ifdef THREADS
89 GC_INNER size_t GC_alloc_and_get_procedure_stack(ptr_t *pbuf)
90 {
91 /* TODO: support saving from non-zero ofs in stack */
92 ptr_t buf = NULL;
93 size_t new_sz, buf_sz;
94
95 GC_ASSERT(I_HOLD_LOCK());
96 for (buf_sz = 0; ; buf_sz = new_sz) {
97 new_sz = GC_get_procedure_stack(buf, buf_sz);
98 if (new_sz <= buf_sz) break;
99
100 if (EXPECT(buf != NULL, FALSE))
101 GC_INTERNAL_FREE(buf);
102 buf = (ptr_t)GC_INTERNAL_MALLOC_IGNORE_OFF_PAGE(new_sz, PTRFREE);
103 if (NULL == buf)
104 ABORT("Could not allocate memory for procedure stack");
105 }
106 *pbuf = buf;
107 return new_sz;
108 }
109 # endif /* THREADS */
110 #endif /* E2K */
111
112 #if defined(MACOS) && defined(__MWERKS__)
113
114 #if defined(POWERPC)
115
116 # define NONVOLATILE_GPR_COUNT 19
117 struct ppc_registers {
118 unsigned long gprs[NONVOLATILE_GPR_COUNT]; /* R13-R31 */
119 };
120 typedef struct ppc_registers ppc_registers;
121
122 # if defined(CPPCHECK)
123 void getRegisters(ppc_registers* regs);
124 # else
125 asm static void getRegisters(register ppc_registers* regs)
126 {
127 stmw r13,regs->gprs /* save R13-R31 */
128 blr
129 }
130 # endif
131
132 static void PushMacRegisters(void)
133 {
134 ppc_registers regs;
135 int i;
136 getRegisters(&regs);
137 for (i = 0; i < NONVOLATILE_GPR_COUNT; i++)
138 GC_push_one(regs.gprs[i]);
139 }
140
141 #else /* M68K */
142
143 asm static void PushMacRegisters(void)
144 {
145 sub.w #4,sp /* reserve space for one parameter */
146 move.l a2,(sp)
147 jsr GC_push_one
148 move.l a3,(sp)
149 jsr GC_push_one
150 move.l a4,(sp)
151 jsr GC_push_one
152 # if !__option(a6frames)
153 /* <pcb> perhaps a6 should be pushed if stack frames are not being used */
154 move.l a6,(sp)
155 jsr GC_push_one
156 # endif
157 /* skip a5 (globals), a6 (frame pointer), and a7 (stack pointer) */
158 move.l d2,(sp)
159 jsr GC_push_one
160 move.l d3,(sp)
161 jsr GC_push_one
162 move.l d4,(sp)
163 jsr GC_push_one
164 move.l d5,(sp)
165 jsr GC_push_one
166 move.l d6,(sp)
167 jsr GC_push_one
168 move.l d7,(sp)
169 jsr GC_push_one
170 add.w #4,sp /* fix stack */
171 rts
172 }
173
174 #endif /* M68K */
175
176 #endif /* MACOS && __MWERKS__ */
177
178 # if defined(SPARC) || defined(IA64)
179 /* Value returned from register flushing routine; either sp (SPARC) */
180 /* or ar.bsp (IA64). */
181 GC_INNER ptr_t GC_save_regs_ret_val = NULL;
182 # endif
183
184 /* Routine to mark from registers that are preserved by the C compiler. */
185 /* This must be ported to every new architecture. It is not optional, */
186 /* and should not be used on platforms that are either UNIX-like, or */
187 /* require thread support. */
188
189 #undef HAVE_PUSH_REGS
190
191 #if defined(USE_ASM_PUSH_REGS)
192 # define HAVE_PUSH_REGS
193 #else /* No asm implementation */
194
195 # ifdef STACK_NOT_SCANNED
196 void GC_push_regs(void)
197 {
198 /* empty */
199 }
200 # define HAVE_PUSH_REGS
201
202 # elif defined(M68K) && defined(AMIGA)
203 /* This function is not static because it could also be */
204 /* erroneously defined in .S file, so this error would be caught */
205 /* by the linker. */
206 void GC_push_regs(void)
207 {
208 /* AMIGA - could be replaced by generic code */
209 /* a0, a1, d0 and d1 are caller save */
210
211 # ifdef __GNUC__
212 asm("subq.w &0x4,%sp"); /* allocate word on top of stack */
213
214 asm("mov.l %a2,(%sp)"); asm("jsr _GC_push_one");
215 asm("mov.l %a3,(%sp)"); asm("jsr _GC_push_one");
216 asm("mov.l %a4,(%sp)"); asm("jsr _GC_push_one");
217 asm("mov.l %a5,(%sp)"); asm("jsr _GC_push_one");
218 asm("mov.l %a6,(%sp)"); asm("jsr _GC_push_one");
219 /* Skip frame pointer and stack pointer */
220 asm("mov.l %d2,(%sp)"); asm("jsr _GC_push_one");
221 asm("mov.l %d3,(%sp)"); asm("jsr _GC_push_one");
222 asm("mov.l %d4,(%sp)"); asm("jsr _GC_push_one");
223 asm("mov.l %d5,(%sp)"); asm("jsr _GC_push_one");
224 asm("mov.l %d6,(%sp)"); asm("jsr _GC_push_one");
225 asm("mov.l %d7,(%sp)"); asm("jsr _GC_push_one");
226
227 asm("addq.w &0x4,%sp"); /* put stack back where it was */
228 # else /* !__GNUC__ */
229 GC_push_one(getreg(REG_A2));
230 GC_push_one(getreg(REG_A3));
231 # ifndef __SASC
232 /* Can probably be changed to #if 0 -Kjetil M. (a4=globals) */
233 GC_push_one(getreg(REG_A4));
234 # endif
235 GC_push_one(getreg(REG_A5));
236 GC_push_one(getreg(REG_A6));
237 /* Skip stack pointer */
238 GC_push_one(getreg(REG_D2));
239 GC_push_one(getreg(REG_D3));
240 GC_push_one(getreg(REG_D4));
241 GC_push_one(getreg(REG_D5));
242 GC_push_one(getreg(REG_D6));
243 GC_push_one(getreg(REG_D7));
244 # endif /* !__GNUC__ */
245 }
246 # define HAVE_PUSH_REGS
247
248 # elif defined(MACOS)
249
250 # if defined(M68K) && defined(THINK_C) && !defined(CPPCHECK)
251 # define PushMacReg(reg) \
252 move.l reg,(sp) \
253 jsr GC_push_one
254 void GC_push_regs(void)
255 {
256 asm {
257 sub.w #4,sp ; reserve space for one parameter.
258 PushMacReg(a2);
259 PushMacReg(a3);
260 PushMacReg(a4);
261 ; skip a5 (globals), a6 (frame pointer), and a7 (stack pointer)
262 PushMacReg(d2);
263 PushMacReg(d3);
264 PushMacReg(d4);
265 PushMacReg(d5);
266 PushMacReg(d6);
267 PushMacReg(d7);
268 add.w #4,sp ; fix stack.
269 }
270 }
271 # define HAVE_PUSH_REGS
272 # undef PushMacReg
273 # elif defined(__MWERKS__)
274 void GC_push_regs(void)
275 {
276 PushMacRegisters();
277 }
278 # define HAVE_PUSH_REGS
279 # endif /* __MWERKS__ */
280 # endif /* MACOS */
281
282 #endif /* !USE_ASM_PUSH_REGS */
283
284 #if defined(HAVE_PUSH_REGS) && defined(THREADS)
285 # error GC_push_regs cannot be used with threads
286 /* Would fail for GC_do_blocking. There are probably other safety */
287 /* issues. */
288 # undef HAVE_PUSH_REGS
289 #endif
290
291 #if !defined(HAVE_PUSH_REGS) && defined(UNIX_LIKE)
292 # include <signal.h>
293 # ifndef NO_GETCONTEXT
294 # if defined(DARWIN) \
295 && (MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 /*MAC_OS_X_VERSION_10_6*/)
296 # include <sys/ucontext.h>
297 # else
298 # include <ucontext.h>
299 # endif /* !DARWIN */
300 # ifdef GETCONTEXT_FPU_EXCMASK_BUG
301 # include <fenv.h>
302 # endif
303 # endif
304 #endif /* !HAVE_PUSH_REGS */
305
306 /* Ensure that either registers are pushed, or callee-save registers */
307 /* are somewhere on the stack, and then call fn(arg, ctxt). */
308 /* ctxt is either a pointer to a ucontext_t we generated, or NULL. */
309 GC_ATTR_NO_SANITIZE_ADDR
310 GC_INNER void GC_with_callee_saves_pushed(void (*fn)(ptr_t, void *),
311 volatile ptr_t arg)
312 {
313 volatile int dummy;
314 volatile ptr_t context = 0;
315
316 # if defined(HAVE_PUSH_REGS)
317 GC_push_regs();
318 # elif defined(EMSCRIPTEN)
319 /* No-op, "registers" are pushed in GC_push_other_roots(). */
320 # else
321 # if defined(UNIX_LIKE) && !defined(NO_GETCONTEXT)
322 /* Older versions of Darwin seem to lack getcontext(). */
323 /* ARM and MIPS Linux often doesn't support a real */
324 /* getcontext(). */
325 static signed char getcontext_works = 0; /* (-1) - broken, 1 - works */
326 ucontext_t ctxt;
327 # ifdef GETCONTEXT_FPU_EXCMASK_BUG
328 /* Workaround a bug (clearing the FPU exception mask) in */
329 /* getcontext on Linux/x64. */
330 # ifdef X86_64
331 /* We manipulate FPU control word here just not to force the */
332 /* client application to use -lm linker option. */
333 unsigned short old_fcw;
334
335 # if defined(CPPCHECK)
336 GC_noop1((word)&old_fcw);
337 # endif
338 __asm__ __volatile__ ("fstcw %0" : "=m" (*&old_fcw));
339 # else
340 int except_mask = fegetexcept();
341 # endif
342 # endif
343
344 if (getcontext_works >= 0) {
345 if (getcontext(&ctxt) < 0) {
346 WARN("getcontext failed:"
347 " using another register retrieval method...\n", 0);
348 /* getcontext() is broken, do not try again. */
349 /* E.g., to workaround a bug in Docker ubuntu_32bit. */
350 } else {
351 context = (ptr_t)&ctxt;
352 }
353 if (EXPECT(0 == getcontext_works, FALSE))
354 getcontext_works = context != NULL ? 1 : -1;
355 }
356 # ifdef GETCONTEXT_FPU_EXCMASK_BUG
357 # ifdef X86_64
358 __asm__ __volatile__ ("fldcw %0" : : "m" (*&old_fcw));
359 {
360 unsigned mxcsr;
361 /* And now correct the exception mask in SSE MXCSR. */
362 __asm__ __volatile__ ("stmxcsr %0" : "=m" (*&mxcsr));
363 mxcsr = (mxcsr & ~(FE_ALL_EXCEPT << 7)) |
364 ((old_fcw & FE_ALL_EXCEPT) << 7);
365 __asm__ __volatile__ ("ldmxcsr %0" : : "m" (*&mxcsr));
366 }
367 # else /* !X86_64 */
368 if (feenableexcept(except_mask) < 0)
369 ABORT("feenableexcept failed");
370 # endif
371 # endif /* GETCONTEXT_FPU_EXCMASK_BUG */
372 # if defined(E2K) || defined(IA64) || defined(SPARC)
373 /* On a register window machine, we need to save register */
374 /* contents on the stack for this to work. This may already be */
375 /* subsumed by the getcontext() call. */
376 # if defined(IA64) || defined(SPARC)
377 GC_save_regs_ret_val =
378 # endif
379 GC_save_regs_in_stack();
380 # endif
381 if (NULL == context) /* getcontext failed */
382 # endif /* !NO_GETCONTEXT */
383 {
384 # if defined(HAVE_BUILTIN_UNWIND_INIT)
385 /* This was suggested by Richard Henderson as the way to */
386 /* force callee-save registers and register windows onto */
387 /* the stack. */
388 __builtin_unwind_init();
389 # elif defined(NO_CRT) && defined(MSWIN32)
390 CONTEXT ctx;
391 RtlCaptureContext(&ctx);
392 # else
393 /* Generic code */
394 /* The idea is due to Parag Patel at HP. */
395 /* We're not sure whether he would like */
396 /* to be acknowledged for it or not. */
397 jmp_buf regs;
398 word *i = (word *)&regs[0];
399 ptr_t lim = (ptr_t)(&regs[0]) + sizeof(regs);
400
401 /* Setjmp doesn't always clear all of the buffer. */
402 /* That tends to preserve garbage. Clear it. */
403 for (; (word)i < (word)lim; i++) {
404 *i = 0;
405 }
406 # if defined(MSWIN32) || defined(MSWINCE) || defined(UTS4) \
407 || defined(OS2) || defined(CX_UX) || defined(__CC_ARM) \
408 || defined(LINUX) || defined(EWS4800) || defined(RTEMS) \
409 || defined(DJGPP)
410 (void) setjmp(regs);
411 # else
412 (void) _setjmp(regs);
413 /* We don't want to mess with signals. According to */
414 /* SUSV3, setjmp() may or may not save signal mask. */
415 /* _setjmp won't, but is less portable. */
416 # endif
417 # endif /* !HAVE_BUILTIN_UNWIND_INIT */
418 }
419 # endif /* !HAVE_PUSH_REGS */
420 /* TODO: context here is sometimes just zero. At the moment, the */
421 /* callees don't really need it. */
422 fn(arg, (/* no volatile */ void *)context);
423 /* Strongly discourage the compiler from treating the above */
424 /* as a tail-call, since that would pop the register */
425 /* contents before we get a chance to look at them. */
426 GC_noop1(COVERT_DATAFLOW(&dummy));
427 }
428
429 #endif /* !PLATFORM_MACH_DEP && !SN_TARGET_PSP2 */