* gc_os_dep.c (GC_noop1): ifdef out (unused) to quiet warning.
[bpt/guile.git] / libguile / gc_os_dep.c
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
5 * Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
6 * Copyright (c) 2000 Free Software Foundation
7 *
8 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
9 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
10 *
11 * Permission is hereby granted to use or copy this program
12 * for any purpose, provided the above notices are retained on all copies.
13 * Permission to modify the code and to distribute modified code is granted,
14 * provided the above notices are retained, and a notice that the code was
15 * modified is included with the above copyright notice.
16 *
17 */
18
19 /*
20 * Copied from gc5.2, files "os_dep.c", "gc_priv.h", "mark.c" and "gcconfig.h",
21 * and modified for Guile by Marius Vollmer.
22 */
23
24 #include <ctype.h>
25 #include "libguile/gc.h"
26
27 #define ABORT(msg) abort ()
28
29 typedef char * ptr_t; /* A generic pointer to which we can add */
30 /* byte displacements. */
31 /* Preferably identical to caddr_t, if it */
32 /* exists. */
33
34 /* Define word and signed_word to be unsigned and signed types of the */
35 /* size as char * or void *. There seems to be no way to do this */
36 /* even semi-portably. The following is probably no better/worse */
37 /* than almost anything else. */
38 /* The ANSI standard suggests that size_t and ptr_diff_t might be */
39 /* better choices. But those appear to have incorrect definitions */
40 /* on may systems. Notably "typedef int size_t" seems to be both */
41 /* frequent and WRONG. */
42 typedef unsigned long GC_word;
43 typedef long GC_signed_word;
44
45 typedef GC_word word;
46 typedef GC_signed_word signed_word;
47
48 typedef int GC_bool;
49 # define TRUE 1
50 # define FALSE 0
51
52 #if defined(__STDC__)
53 # include <stdlib.h>
54 # if !(defined( sony_news ) )
55 # include <stddef.h>
56 # endif
57 # define VOLATILE volatile
58 #else
59 # ifdef MSWIN32
60 # include <stdlib.h>
61 # endif
62 # define VOLATILE
63 #endif
64
65 #if 0 /* currently unused (as of 2001-07-12) */
66
67 /* Single argument version, robust against whole program analysis. */
68 static void
69 GC_noop1(x)
70 word x;
71 {
72 static VOLATILE word sink;
73
74 sink = x;
75 }
76
77 #endif
78
79 /* Machine dependent parameters. Some tuning parameters can be found */
80 /* near the top of gc_private.h. */
81
82 /* Machine specific parts contributed by various people. See README file. */
83
84 /* First a unified test for Linux: */
85 # if defined(linux) || defined(__linux__)
86 # define LINUX
87 # endif
88
89 /* Determine the machine type: */
90 # if defined(sun) && defined(mc68000)
91 # define M68K
92 # define SUNOS4
93 # define mach_type_known
94 # endif
95 # if defined(hp9000s300)
96 # define M68K
97 # define HP
98 # define mach_type_known
99 # endif
100 # if defined(__OpenBSD__) && defined(m68k)
101 # define M68K
102 # define OPENBSD
103 # define mach_type_known
104 # endif
105 # if defined(__OpenBSD__) && defined(__sparc__)
106 # define SPARC
107 # define OPENBSD
108 # define mach_type_known
109 # endif
110 # if defined(__NetBSD__) && defined(m68k)
111 # define M68K
112 # define NETBSD
113 # define mach_type_known
114 # endif
115 # if defined(__NetBSD__) && defined(arm32)
116 # define ARM32
117 # define NETBSD
118 # define mach_type_known
119 # endif
120 # if defined(vax)
121 # define VAX
122 # ifdef ultrix
123 # define ULTRIX
124 # else
125 # define BSD
126 # endif
127 # define mach_type_known
128 # endif
129 # if defined(mips) || defined(__mips)
130 # define MIPS
131 # if !defined(LINUX)
132 # if defined(ultrix) || defined(__ultrix) || defined(__NetBSD__)
133 # define ULTRIX
134 # else
135 # if defined(_SYSTYPE_SVR4) || defined(SYSTYPE_SVR4) \
136 || defined(__SYSTYPE_SVR4__)
137 # define IRIX5 /* or IRIX 6.X */
138 # else
139 # define RISCOS /* or IRIX 4.X */
140 # endif
141 # endif
142 # endif /* !LINUX */
143 # define mach_type_known
144 # endif
145 # if defined(sequent) && defined(i386)
146 # define I386
147 # define SEQUENT
148 # define mach_type_known
149 # endif
150 # if defined(sun) && defined(i386)
151 # define I386
152 # define SUNOS5
153 # define mach_type_known
154 # endif
155 # if (defined(__OS2__) || defined(__EMX__)) && defined(__32BIT__)
156 # define I386
157 # define OS2
158 # define mach_type_known
159 # endif
160 # if defined(ibm032)
161 # define RT
162 # define mach_type_known
163 # endif
164 # if defined(sun) && (defined(sparc) || defined(__sparc))
165 # define SPARC
166 /* Test for SunOS 5.x */
167 # include <errno.h>
168 # ifdef ECHRNG
169 # define SUNOS5
170 # else
171 # define SUNOS4
172 # endif
173 # define mach_type_known
174 # endif
175 # if defined(sparc) && defined(unix) && !defined(sun) && !defined(linux) \
176 && !defined(__OpenBSD__)
177 # define SPARC
178 # define DRSNX
179 # define mach_type_known
180 # endif
181 # if defined(_IBMR2)
182 # define RS6000
183 # define mach_type_known
184 # endif
185 # if defined(_M_XENIX) && defined(_M_SYSV) && defined(_M_I386)
186 /* The above test may need refinement */
187 # define I386
188 # if defined(_SCO_ELF)
189 # define SCO_ELF
190 # else
191 # define SCO
192 # endif
193 # define mach_type_known
194 # endif
195 # if defined(_AUX_SOURCE)
196 # define M68K
197 # define SYSV
198 # define mach_type_known
199 # endif
200 # if defined(_PA_RISC1_0) || defined(_PA_RISC1_1) || defined(_PA_RISC2_0) \
201 || defined(hppa) || defined(__hppa__)
202 # define HP_PA
203 # ifndef LINUX
204 # define HPUX
205 # endif
206 # define mach_type_known
207 # endif
208 # if defined(LINUX) && (defined(i386) || defined(__i386__))
209 # define I386
210 # define mach_type_known
211 # endif
212 # if defined(LINUX) && (defined(__ia64__) || defined(__ia64))
213 # define IA64
214 # define mach_type_known
215 # endif
216 # if defined(LINUX) && defined(powerpc)
217 # define POWERPC
218 # define mach_type_known
219 # endif
220 # if defined(LINUX) && defined(__mc68000__)
221 # define M68K
222 # define mach_type_known
223 # endif
224 # if defined(LINUX) && (defined(sparc) || defined(__sparc__))
225 # define SPARC
226 # define mach_type_known
227 # endif
228 # if defined(LINUX) && defined(arm)
229 # define ARM32
230 # define mach_type_known
231 # endif
232 # if defined(__alpha) || defined(__alpha__)
233 # define ALPHA
234 # if !defined(LINUX)
235 # define OSF1 /* a.k.a Digital Unix */
236 # endif
237 # define mach_type_known
238 # endif
239 # if defined(_AMIGA) && !defined(AMIGA)
240 # define AMIGA
241 # endif
242 # ifdef AMIGA
243 # define M68K
244 # define mach_type_known
245 # endif
246 # if defined(THINK_C) || defined(__MWERKS__) && !defined(__powerc)
247 # define M68K
248 # define MACOS
249 # define mach_type_known
250 # endif
251 # if defined(__MWERKS__) && defined(__powerc)
252 # define POWERPC
253 # define MACOS
254 # define mach_type_known
255 # endif
256 # if defined(macosx)
257 # define MACOSX
258 # define POWERPC
259 # define mach_type_known
260 # endif
261 # if defined(NeXT) && defined(mc68000)
262 # define M68K
263 # define NEXT
264 # define mach_type_known
265 # endif
266 # if defined(NeXT) && defined(i386)
267 # define I386
268 # define NEXT
269 # define mach_type_known
270 # endif
271 # if defined(__OpenBSD__) && defined(i386)
272 # define I386
273 # define OPENBSD
274 # define mach_type_known
275 # endif
276 # if defined(__FreeBSD__) && defined(i386)
277 # define I386
278 # define FREEBSD
279 # define mach_type_known
280 # endif
281 # if defined(__NetBSD__) && defined(i386)
282 # define I386
283 # define NETBSD
284 # define mach_type_known
285 # endif
286 # if defined(bsdi) && defined(i386)
287 # define I386
288 # define BSDI
289 # define mach_type_known
290 # endif
291 # if !defined(mach_type_known) && defined(__386BSD__)
292 # define I386
293 # define THREE86BSD
294 # define mach_type_known
295 # endif
296 # if defined(_CX_UX) && defined(_M88K)
297 # define M88K
298 # define CX_UX
299 # define mach_type_known
300 # endif
301 # if defined(DGUX)
302 # define M88K
303 /* DGUX defined */
304 # define mach_type_known
305 # endif
306 # if (defined(_MSDOS) || defined(_MSC_VER)) && (_M_IX86 >= 300) \
307 || defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__)
308 # define I386
309 # define MSWIN32 /* or Win32s */
310 # define mach_type_known
311 # endif
312 # if defined(__DJGPP__)
313 # define I386
314 # ifndef DJGPP
315 # define DJGPP /* MSDOS running the DJGPP port of GCC */
316 # endif
317 # define mach_type_known
318 # endif
319 # if defined(__CYGWIN32__) || defined(__CYGWIN__)
320 # define I386
321 # define CYGWIN32
322 # define mach_type_known
323 # endif
324 # if defined(__MINGW32__)
325 # define I386
326 # define MSWIN32
327 # define mach_type_known
328 # endif
329 # if defined(__BORLANDC__)
330 # define I386
331 # define MSWIN32
332 # define mach_type_known
333 # endif
334 # if defined(_UTS) && !defined(mach_type_known)
335 # define S370
336 # define UTS4
337 # define mach_type_known
338 # endif
339 # if defined(__pj__)
340 # define PJ
341 # define mach_type_known
342 # endif
343 /* Ivan Demakov */
344 # if defined(__WATCOMC__) && defined(__386__)
345 # define I386
346 # if !defined(OS2) && !defined(MSWIN32) && !defined(DOS4GW)
347 # if defined(__OS2__)
348 # define OS2
349 # else
350 # if defined(__WINDOWS_386__) || defined(__NT__)
351 # define MSWIN32
352 # else
353 # define DOS4GW
354 # endif
355 # endif
356 # endif
357 # define mach_type_known
358 # endif
359
360 /* Feel free to add more clauses here */
361
362 /* Or manually define the machine type here. A machine type is */
363 /* characterized by the architecture. Some */
364 /* machine types are further subdivided by OS. */
365 /* the macros ULTRIX, RISCOS, and BSD to distinguish. */
366 /* Note that SGI IRIX is treated identically to RISCOS. */
367 /* SYSV on an M68K actually means A/UX. */
368 /* The distinction in these cases is usually the stack starting address */
369 # ifndef mach_type_known
370 --> unknown machine type
371 # endif
372 /* Mapping is: M68K ==> Motorola 680X0 */
373 /* (SUNOS4,HP,NEXT, and SYSV (A/UX), */
374 /* MACOS and AMIGA variants) */
375 /* I386 ==> Intel 386 */
376 /* (SEQUENT, OS2, SCO, LINUX, NETBSD, */
377 /* FREEBSD, THREE86BSD, MSWIN32, */
378 /* BSDI,SUNOS5, NEXT, other variants) */
379 /* NS32K ==> Encore Multimax */
380 /* MIPS ==> R2000 or R3000 */
381 /* (RISCOS, ULTRIX variants) */
382 /* VAX ==> DEC VAX */
383 /* (BSD, ULTRIX variants) */
384 /* RS6000 ==> IBM RS/6000 AIX3.X */
385 /* RT ==> IBM PC/RT */
386 /* HP_PA ==> HP9000/700 & /800 */
387 /* HP/UX */
388 /* SPARC ==> SPARC under SunOS */
389 /* (SUNOS4, SUNOS5, */
390 /* DRSNX variants) */
391 /* ALPHA ==> DEC Alpha */
392 /* (OSF1 and LINUX variants) */
393 /* M88K ==> Motorola 88XX0 */
394 /* (CX_UX and DGUX) */
395 /* S370 ==> 370-like machine */
396 /* running Amdahl UTS4 */
397 /* ARM32 ==> Intel StrongARM */
398 /* IA64 ==> Intel IA64 */
399 /* (e.g. Itanium) */
400
401
402 /*
403 * For each architecture and OS, the following need to be defined:
404 *
405 * CPP_WORD_SZ is a simple integer constant representing the word size.
406 * in bits. We assume byte addressibility, where a byte has 8 bits.
407 * We also assume CPP_WORD_SZ is either 32 or 64.
408 * (We care about the length of pointers, not hardware
409 * bus widths. Thus a 64 bit processor with a C compiler that uses
410 * 32 bit pointers should use CPP_WORD_SZ of 32, not 64. Default is 32.)
411 *
412 * MACH_TYPE is a string representation of the machine type.
413 * OS_TYPE is analogous for the OS.
414 *
415 * ALIGNMENT is the largest N, such that
416 * all pointer are guaranteed to be aligned on N byte boundaries.
417 * defining it to be 1 will always work, but perform poorly.
418 *
419 * DATASTART is the beginning of the data segment.
420 * On UNIX systems, the collector will scan the area between DATASTART
421 * and DATAEND for root pointers.
422 *
423 * DATAEND, if not &end.
424 *
425 * ALIGN_DOUBLE of GC_malloc should return blocks aligned to twice
426 * the pointer size.
427 *
428 * STACKBOTTOM is the cool end of the stack, which is usually the
429 * highest address in the stack.
430 * Under PCR or OS/2, we have other ways of finding thread stacks.
431 * For each machine, the following should:
432 * 1) define STACK_GROWS_UP if the stack grows toward higher addresses, and
433 * 2) define exactly one of
434 * STACKBOTTOM (should be defined to be an expression)
435 * HEURISTIC1
436 * HEURISTIC2
437 * If either of the last two macros are defined, then STACKBOTTOM is computed
438 * during collector startup using one of the following two heuristics:
439 * HEURISTIC1: Take an address inside GC_init's frame, and round it up to
440 * the next multiple of STACK_GRAN.
441 * HEURISTIC2: Take an address inside GC_init's frame, increment it repeatedly
442 * in small steps (decrement if STACK_GROWS_UP), and read the value
443 * at each location. Remember the value when the first
444 * Segmentation violation or Bus error is signalled. Round that
445 * to the nearest plausible page boundary, and use that instead
446 * of STACKBOTTOM.
447 *
448 * Gustavo Rodriguez-Rivera points out that on most (all?) Unix machines,
449 * the value of environ is a pointer that can serve as STACKBOTTOM.
450 * I expect that HEURISTIC2 can be replaced by this approach, which
451 * interferes far less with debugging.
452 *
453 * If no expression for STACKBOTTOM can be found, and neither of the above
454 * heuristics are usable, the collector can still be used with all of the above
455 * undefined, provided one of the following is done:
456 * 1) GC_mark_roots can be changed to somehow mark from the correct stack(s)
457 * without reference to STACKBOTTOM. This is appropriate for use in
458 * conjunction with thread packages, since there will be multiple stacks.
459 * (Allocating thread stacks in the heap, and treating them as ordinary
460 * heap data objects is also possible as a last resort. However, this is
461 * likely to introduce significant amounts of excess storage retention
462 * unless the dead parts of the thread stacks are periodically cleared.)
463 * 2) Client code may set GC_stackbottom before calling any GC_ routines.
464 * If the author of the client code controls the main program, this is
465 * easily accomplished by introducing a new main program, setting
466 * GC_stackbottom to the address of a local variable, and then calling
467 * the original main program. The new main program would read something
468 * like:
469 *
470 * # include "gc_private.h"
471 *
472 * main(argc, argv, envp)
473 * int argc;
474 * char **argv, **envp;
475 * {
476 * int dummy;
477 *
478 * GC_stackbottom = (ptr_t)(&dummy);
479 * return(real_main(argc, argv, envp));
480 * }
481 *
482 *
483 * Each architecture may also define the style of virtual dirty bit
484 * implementation to be used:
485 * MPROTECT_VDB: Write protect the heap and catch faults.
486 * PROC_VDB: Use the SVR4 /proc primitives to read dirty bits.
487 *
488 * An architecture may define DYNAMIC_LOADING if dynamic_load.c
489 * defined GC_register_dynamic_libraries() for the architecture.
490 *
491 * An architecture may define PREFETCH(x) to preload the cache with *x.
492 * This defaults to a no-op.
493 *
494 * PREFETCH_FOR_WRITE(x) is used if *x is about to be written.
495 *
496 * An architecture may also define CLEAR_DOUBLE(x) to be a fast way to
497 * clear the two words at GC_malloc-aligned address x. By default,
498 * word stores of 0 are used instead.
499 */
500
501
502 # define STACK_GRAN 0x1000000
503 # ifdef M68K
504 # define MACH_TYPE "M68K"
505 # define ALIGNMENT 2
506 # ifdef OPENBSD
507 # define OS_TYPE "OPENBSD"
508 # define HEURISTIC2
509 extern char etext;
510 # define DATASTART ((ptr_t)(&etext))
511 # endif
512 # ifdef NETBSD
513 # define OS_TYPE "NETBSD"
514 # define HEURISTIC2
515 extern char etext;
516 # define DATASTART ((ptr_t)(&etext))
517 # endif
518 # ifdef LINUX
519 # define OS_TYPE "LINUX"
520 # define STACKBOTTOM ((ptr_t)0xf0000000)
521 # define MPROTECT_VDB
522 # ifdef __ELF__
523 # define DYNAMIC_LOADING
524 extern char **__environ;
525 # define DATASTART ((ptr_t)(&__environ))
526 /* hideous kludge: __environ is the first */
527 /* word in crt0.o, and delimits the start */
528 /* of the data segment, no matter which */
529 /* ld options were passed through. */
530 /* We could use _etext instead, but that */
531 /* would include .rodata, which may */
532 /* contain large read-only data tables */
533 /* that we'd rather not scan. */
534 extern int _end;
535 # define DATAEND (&_end)
536 # else
537 extern int etext;
538 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
539 # endif
540 # endif
541 # ifdef SUNOS4
542 # define OS_TYPE "SUNOS4"
543 extern char etext;
544 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ffff) & ~0x1ffff))
545 # define HEURISTIC1 /* differs */
546 # define DYNAMIC_LOADING
547 # endif
548 # ifdef HP
549 # define OS_TYPE "HP"
550 extern char etext;
551 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
552 # define STACKBOTTOM ((ptr_t) 0xffeffffc)
553 /* empirically determined. seems to work. */
554 # include <unistd.h>
555 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
556 # endif
557 # ifdef SYSV
558 # define OS_TYPE "SYSV"
559 extern etext;
560 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
561 & ~0x3fffff) \
562 +((word)&etext & 0x1fff))
563 /* This only works for shared-text binaries with magic number 0413.
564 The other sorts of SysV binaries put the data at the end of the text,
565 in which case the default of &etext would work. Unfortunately,
566 handling both would require having the magic-number available.
567 -- Parag
568 */
569 # define STACKBOTTOM ((ptr_t)0xFFFFFFFE)
570 /* The stack starts at the top of memory, but */
571 /* 0x0 cannot be used as setjump_test complains */
572 /* that the stack direction is incorrect. Two */
573 /* bytes down from 0x0 should be safe enough. */
574 /* --Parag */
575 # include <sys/mmu.h>
576 # define GETPAGESIZE() PAGESIZE /* Is this still right? */
577 # endif
578 # ifdef AMIGA
579 # define OS_TYPE "AMIGA"
580 /* STACKBOTTOM and DATASTART handled specially */
581 /* in os_dep.c */
582 # define DATAEND /* not needed */
583 # define GETPAGESIZE() 4096
584 # endif
585 # ifdef MACOS
586 # ifndef __LOWMEM__
587 # include <LowMem.h>
588 # endif
589 # define OS_TYPE "MACOS"
590 /* see os_dep.c for details of global data segments. */
591 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
592 # define DATAEND /* not needed */
593 # define GETPAGESIZE() 4096
594 # endif
595 # ifdef NEXT
596 # define OS_TYPE "NEXT"
597 # define DATASTART ((ptr_t) get_etext())
598 # define STACKBOTTOM ((ptr_t) 0x4000000)
599 # define DATAEND /* not needed */
600 # endif
601 # endif
602
603 # ifdef POWERPC
604 # define MACH_TYPE "POWERPC"
605 # ifdef MACOS
606 # define ALIGNMENT 2 /* Still necessary? Could it be 4? */
607 # ifndef __LOWMEM__
608 # include <LowMem.h>
609 # endif
610 # define OS_TYPE "MACOS"
611 /* see os_dep.c for details of global data segments. */
612 # define STACKBOTTOM ((ptr_t) LMGetCurStackBase())
613 # define DATAEND /* not needed */
614 # endif
615 # ifdef LINUX
616 # define ALIGNMENT 4 /* Guess. Can someone verify? */
617 /* This was 2, but that didn't sound right. */
618 # define OS_TYPE "LINUX"
619 # define HEURISTIC1
620 # define DYNAMIC_LOADING
621 # undef STACK_GRAN
622 # define STACK_GRAN 0x10000000
623 /* Stack usually starts at 0x80000000 */
624 # define LINUX_DATA_START
625 extern int _end;
626 # define DATAEND (&_end)
627 # endif
628 # ifdef MACOSX
629 # define ALIGNMENT 4
630 # define OS_TYPE "MACOSX"
631 # define DATASTART ((ptr_t) get_etext())
632 # define STACKBOTTOM ((ptr_t) 0xc0000000)
633 # define DATAEND /* not needed */
634 # endif
635 # endif
636
637 # ifdef VAX
638 # define MACH_TYPE "VAX"
639 # define ALIGNMENT 4 /* Pointers are longword aligned by 4.2 C compiler */
640 extern char etext;
641 # define DATASTART ((ptr_t)(&etext))
642 # ifdef BSD
643 # define OS_TYPE "BSD"
644 # define HEURISTIC1
645 /* HEURISTIC2 may be OK, but it's hard to test. */
646 # endif
647 # ifdef ULTRIX
648 # define OS_TYPE "ULTRIX"
649 # define STACKBOTTOM ((ptr_t) 0x7fffc800)
650 # endif
651 # endif
652
653 # ifdef RT
654 # define MACH_TYPE "RT"
655 # define ALIGNMENT 4
656 # define DATASTART ((ptr_t) 0x10000000)
657 # define STACKBOTTOM ((ptr_t) 0x1fffd800)
658 # endif
659
660 # ifdef SPARC
661 # define MACH_TYPE "SPARC"
662 # define ALIGNMENT 4 /* Required by hardware */
663 # define ALIGN_DOUBLE
664 extern int etext;
665 # ifdef SUNOS5
666 # define OS_TYPE "SUNOS5"
667 extern int _etext;
668 extern int _end;
669 extern char * GC_SysVGetDataStart();
670 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
671 # define DATAEND (&_end)
672 # ifndef USE_MMAP
673 # define USE_MMAP
674 # endif
675 # ifdef USE_MMAP
676 # define HEAP_START (ptr_t)0x40000000
677 # else
678 # define HEAP_START DATAEND
679 # endif
680 # define PROC_VDB
681 /* HEURISTIC1 reportedly no longer works under 2.7. Thus we */
682 /* switched to HEURISTIC2, eventhough it creates some debugging */
683 /* issues. */
684 # define HEURISTIC2
685 # include <unistd.h>
686 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
687 /* getpagesize() appeared to be missing from at least one */
688 /* Solaris 5.4 installation. Weird. */
689 # define DYNAMIC_LOADING
690 # endif
691 # ifdef SUNOS4
692 # define OS_TYPE "SUNOS4"
693 /* [If you have a weak stomach, don't read this.] */
694 /* We would like to use: */
695 /* # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1fff) & ~0x1fff)) */
696 /* This fails occasionally, due to an ancient, but very */
697 /* persistent ld bug. &etext is set 32 bytes too high. */
698 /* We instead read the text segment size from the a.out */
699 /* header, which happens to be mapped into our address space */
700 /* at the start of the text segment. The detective work here */
701 /* was done by Robert Ehrlich, Manuel Serrano, and Bernard */
702 /* Serpette of INRIA. */
703 /* This assumes ZMAGIC, i.e. demand-loadable executables. */
704 # define TEXTSTART 0x2000
705 # define DATASTART ((ptr_t)(*(int *)(TEXTSTART+0x4)+TEXTSTART))
706 # define MPROTECT_VDB
707 # define HEURISTIC1
708 # define DYNAMIC_LOADING
709 # endif
710 # ifdef DRSNX
711 # define CPP_WORDSZ 32
712 # define OS_TYPE "DRSNX"
713 extern char * GC_SysVGetDataStart();
714 extern int etext;
715 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
716 # define MPROTECT_VDB
717 # define STACKBOTTOM ((ptr_t) 0xdfff0000)
718 # define DYNAMIC_LOADING
719 # endif
720 # ifdef LINUX
721 # define OS_TYPE "LINUX"
722 # ifdef __ELF__
723 # define LINUX_DATA_START
724 # define DYNAMIC_LOADING
725 # else
726 Linux Sparc non elf ?
727 # endif
728 extern int _end;
729 # define DATAEND (&_end)
730 # define SVR4
731 # define STACKBOTTOM ((ptr_t) 0xf0000000)
732 # endif
733 # ifdef OPENBSD
734 # define OS_TYPE "OPENBSD"
735 # define STACKBOTTOM ((ptr_t) 0xf8000000)
736 # define DATASTART ((ptr_t)(&etext))
737 # endif
738 # endif
739
740 # ifdef I386
741 # define MACH_TYPE "I386"
742 # define ALIGNMENT 4 /* Appears to hold for all "32 bit" compilers */
743 /* except Borland. The -a4 option fixes */
744 /* Borland. */
745 /* Ivan Demakov: For Watcom the option is -zp4. */
746 # ifndef SMALL_CONFIG
747 # define ALIGN_DOUBLE /* Not strictly necessary, but may give speed */
748 /* improvement on Pentiums. */
749 # endif
750 # ifdef SEQUENT
751 # define OS_TYPE "SEQUENT"
752 extern int etext;
753 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
754 # define STACKBOTTOM ((ptr_t) 0x3ffff000)
755 # endif
756 # ifdef SUNOS5
757 # define OS_TYPE "SUNOS5"
758 extern int etext, _start;
759 extern char * GC_SysVGetDataStart();
760 # define DATASTART GC_SysVGetDataStart(0x1000, &etext)
761 # define STACKBOTTOM ((ptr_t)(&_start))
762 /** At least in Solaris 2.5, PROC_VDB gives wrong values for dirty bits. */
763 /*# define PROC_VDB*/
764 # define DYNAMIC_LOADING
765 # ifndef USE_MMAP
766 # define USE_MMAP
767 # endif
768 # ifdef USE_MMAP
769 # define HEAP_START (ptr_t)0x40000000
770 # else
771 # define HEAP_START DATAEND
772 # endif
773 # endif
774 # ifdef SCO
775 # define OS_TYPE "SCO"
776 extern int etext;
777 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x3fffff) \
778 & ~0x3fffff) \
779 +((word)&etext & 0xfff))
780 # define STACKBOTTOM ((ptr_t) 0x7ffffffc)
781 # endif
782 # ifdef SCO_ELF
783 # define OS_TYPE "SCO_ELF"
784 extern int etext;
785 # define DATASTART ((ptr_t)(&etext))
786 # define STACKBOTTOM ((ptr_t) 0x08048000)
787 # define DYNAMIC_LOADING
788 # define ELF_CLASS ELFCLASS32
789 # endif
790 # ifdef LINUX
791 # define OS_TYPE "LINUX"
792 # define LINUX_STACKBOTTOM
793 # if 0
794 # define HEURISTIC1
795 # undef STACK_GRAN
796 # define STACK_GRAN 0x10000000
797 /* STACKBOTTOM is usually 0xc0000000, but this changes with */
798 /* different kernel configurations. In particular, systems */
799 /* with 2GB physical memory will usually move the user */
800 /* address space limit, and hence initial SP to 0x80000000. */
801 # endif
802 # if !defined(LINUX_THREADS) || !defined(REDIRECT_MALLOC)
803 # define MPROTECT_VDB
804 # else
805 /* We seem to get random errors in incremental mode, */
806 /* possibly because Linux threads is itself a malloc client */
807 /* and can't deal with the signals. */
808 # endif
809 # ifdef __ELF__
810 # define DYNAMIC_LOADING
811 # ifdef UNDEFINED /* includes ro data */
812 extern int _etext;
813 # define DATASTART ((ptr_t)((((word) (&_etext)) + 0xfff) & ~0xfff))
814 # endif
815 # include <features.h>
816 # if defined(__GLIBC__) && __GLIBC__ >= 2
817 # define LINUX_DATA_START
818 # else
819 extern char **__environ;
820 # define DATASTART ((ptr_t)(&__environ))
821 /* hideous kludge: __environ is the first */
822 /* word in crt0.o, and delimits the start */
823 /* of the data segment, no matter which */
824 /* ld options were passed through. */
825 /* We could use _etext instead, but that */
826 /* would include .rodata, which may */
827 /* contain large read-only data tables */
828 /* that we'd rather not scan. */
829 # endif
830 extern int _end;
831 # define DATAEND (&_end)
832 # else
833 extern int etext;
834 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
835 # endif
836 # ifdef USE_I686_PREFETCH
837 # define PREFETCH(x) \
838 __asm__ __volatile__ (" prefetchnta %0": : "m"(*(char *)(x)))
839 /* Empirically prefetcht0 is much more effective at reducing */
840 /* cache miss stalls for the targetted load instructions. But it */
841 /* seems to interfere enough with other cache traffic that the net */
842 /* result is worse than prefetchnta. */
843 # if 0
844 /* Using prefetches for write seems to have a slight negative */
845 /* impact on performance, at least for a PIII/500. */
846 # define PREFETCH_FOR_WRITE(x) \
847 __asm__ __volatile__ (" prefetcht0 %0": : "m"(*(char *)(x)))
848 # endif
849 # endif
850 # ifdef USE_3DNOW_PREFETCH
851 # define PREFETCH(x) \
852 __asm__ __volatile__ (" prefetch %0": : "m"(*(char *)(x)))
853 # define PREFETCH_FOR_WRITE(x)
854 __asm__ __volatile__ (" prefetchw %0": : "m"(*(char *)(x)))
855 # endif
856 # endif
857 # ifdef CYGWIN32
858 # define OS_TYPE "CYGWIN32"
859 extern int _data_start__;
860 extern int _data_end__;
861 extern int _bss_start__;
862 extern int _bss_end__;
863 /* For binutils 2.9.1, we have */
864 /* DATASTART = _data_start__ */
865 /* DATAEND = _bss_end__ */
866 /* whereas for some earlier versions it was */
867 /* DATASTART = _bss_start__ */
868 /* DATAEND = _data_end__ */
869 /* To get it right for both, we take the */
870 /* minumum/maximum of the two. */
871 # define MAX(x,y) ((x) > (y) ? (x) : (y))
872 # define MIN(x,y) ((x) < (y) ? (x) : (y))
873 # define DATASTART ((ptr_t) MIN(&_data_start__, &_bss_start__))
874 # define DATAEND ((ptr_t) MAX(&_data_end__, &_bss_end__))
875 # undef STACK_GRAN
876 # define STACK_GRAN 0x10000
877 # define HEURISTIC1
878 # endif
879 # ifdef OS2
880 # define OS_TYPE "OS2"
881 /* STACKBOTTOM and DATASTART are handled specially in */
882 /* os_dep.c. OS2 actually has the right */
883 /* system call! */
884 # define DATAEND /* not needed */
885 # endif
886 # ifdef MSWIN32
887 # define OS_TYPE "MSWIN32"
888 /* STACKBOTTOM and DATASTART are handled specially in */
889 /* os_dep.c. */
890 # ifndef __WATCOMC__
891 # define MPROTECT_VDB
892 # endif
893 # define DATAEND /* not needed */
894 # endif
895 # ifdef DJGPP
896 # define OS_TYPE "DJGPP"
897 # include "stubinfo.h"
898 extern int etext;
899 extern int _stklen;
900 extern int __djgpp_stack_limit;
901 # define DATASTART ((ptr_t)((((word) (&etext)) + 0x1ff) & ~0x1ff))
902 /* # define STACKBOTTOM ((ptr_t)((word) _stubinfo + _stubinfo->size \
903 + _stklen)) */
904 # define STACKBOTTOM ((ptr_t)((word) __djgpp_stack_limit + _stklen))
905 /* This may not be right. */
906 # endif
907 # ifdef OPENBSD
908 # define OS_TYPE "OPENBSD"
909 # endif
910 # ifdef FREEBSD
911 # define OS_TYPE "FREEBSD"
912 # define MPROTECT_VDB
913 # endif
914 # ifdef NETBSD
915 # define OS_TYPE "NETBSD"
916 # endif
917 # ifdef THREE86BSD
918 # define OS_TYPE "THREE86BSD"
919 # endif
920 # ifdef BSDI
921 # define OS_TYPE "BSDI"
922 # endif
923 # if defined(OPENBSD) || defined(FREEBSD) || defined(NETBSD) \
924 || defined(THREE86BSD) || defined(BSDI)
925 # define HEURISTIC2
926 extern char etext;
927 # define DATASTART ((ptr_t)(&etext))
928 # endif
929 # ifdef NEXT
930 # define OS_TYPE "NEXT"
931 # define DATASTART ((ptr_t) get_etext())
932 # define STACKBOTTOM ((ptr_t)0xc0000000)
933 # define DATAEND /* not needed */
934 # endif
935 # ifdef DOS4GW
936 # define OS_TYPE "DOS4GW"
937 extern long __nullarea;
938 extern char _end;
939 extern char *_STACKTOP;
940 /* Depending on calling conventions Watcom C either precedes
941 or does not precedes with undescore names of C-variables.
942 Make sure startup code variables always have the same names. */
943 #pragma aux __nullarea "*";
944 #pragma aux _end "*";
945 # define STACKBOTTOM ((ptr_t) _STACKTOP)
946 /* confused? me too. */
947 # define DATASTART ((ptr_t) &__nullarea)
948 # define DATAEND ((ptr_t) &_end)
949 # endif
950 # endif
951
952 # ifdef NS32K
953 # define MACH_TYPE "NS32K"
954 # define ALIGNMENT 4
955 extern char **environ;
956 # define DATASTART ((ptr_t)(&environ))
957 /* hideous kludge: environ is the first */
958 /* word in crt0.o, and delimits the start */
959 /* of the data segment, no matter which */
960 /* ld options were passed through. */
961 # define STACKBOTTOM ((ptr_t) 0xfffff000) /* for Encore */
962 # endif
963
964 # ifdef MIPS
965 # define MACH_TYPE "MIPS"
966 /* # define STACKBOTTOM ((ptr_t)0x7fff8000) sometimes also works. */
967 # ifdef LINUX
968 /* This was developed for a linuxce style platform. Probably */
969 /* needs to be tweaked for workstation class machines. */
970 # define OS_TYPE "LINUX"
971 extern int __data_start;
972 # define DATASTART ((ptr_t)(&__data_start))
973 # define ALIGNMENT 4
974 # define USE_GENERIC_PUSH_REGS 1
975 # define STACKBOTTOM 0x80000000
976 /* In many cases, this should probably use LINUX_STACKBOTTOM */
977 /* instead. But some kernel versions seem to give the wrong */
978 /* value from /proc. */
979 # endif /* Linux */
980 # ifdef ULTRIX
981 # define HEURISTIC2
982 # define DATASTART (ptr_t)0x10000000
983 /* Could probably be slightly higher since */
984 /* startup code allocates lots of stuff. */
985 # define OS_TYPE "ULTRIX"
986 # define ALIGNMENT 4
987 # endif
988 # ifdef RISCOS
989 # define HEURISTIC2
990 # define DATASTART (ptr_t)0x10000000
991 # define OS_TYPE "RISCOS"
992 # define ALIGNMENT 4 /* Required by hardware */
993 # endif
994 # ifdef IRIX5
995 # define HEURISTIC2
996 extern int _fdata;
997 # define DATASTART ((ptr_t)(&_fdata))
998 # ifdef USE_MMAP
999 # define HEAP_START (ptr_t)0x30000000
1000 # else
1001 # define HEAP_START DATASTART
1002 # endif
1003 /* Lowest plausible heap address. */
1004 /* In the MMAP case, we map there. */
1005 /* In either case it is used to identify */
1006 /* heap sections so they're not */
1007 /* considered as roots. */
1008 # define OS_TYPE "IRIX5"
1009 # define MPROTECT_VDB
1010 # ifdef _MIPS_SZPTR
1011 # define CPP_WORDSZ _MIPS_SZPTR
1012 # define ALIGNMENT (_MIPS_SZPTR/8)
1013 # if CPP_WORDSZ != 64
1014 # define ALIGN_DOUBLE
1015 # endif
1016 # else
1017 # define ALIGNMENT 4
1018 # define ALIGN_DOUBLE
1019 # endif
1020 # define DYNAMIC_LOADING
1021 # endif
1022 # endif
1023
1024 # ifdef RS6000
1025 # define MACH_TYPE "RS6000"
1026 # define ALIGNMENT 4
1027 # define DATASTART ((ptr_t)0x20000000)
1028 extern int errno;
1029 # define STACKBOTTOM ((ptr_t)((ulong)&errno))
1030 # define DYNAMIC_LOADING
1031 /* For really old versions of AIX, this may have to be removed. */
1032 # endif
1033
1034 # ifdef HP_PA
1035 /* OS is assumed to be HP/UX */
1036 # define MACH_TYPE "HP_PA"
1037 # define OS_TYPE "HPUX"
1038 # ifdef __LP64__
1039 # define CPP_WORDSZ 64
1040 # define ALIGNMENT 8
1041 # else
1042 # define CPP_WORDSZ 32
1043 # define ALIGNMENT 4
1044 # define ALIGN_DOUBLE
1045 # endif
1046 extern int __data_start;
1047 # define DATASTART ((ptr_t)(&__data_start))
1048 # if 0
1049 /* The following appears to work for 7xx systems running HP/UX */
1050 /* 9.xx Furthermore, it might result in much faster */
1051 /* collections than HEURISTIC2, which may involve scanning */
1052 /* segments that directly precede the stack. It is not the */
1053 /* default, since it may not work on older machine/OS */
1054 /* combinations. (Thanks to Raymond X.T. Nijssen for uncovering */
1055 /* this.) */
1056 # define STACKBOTTOM ((ptr_t) 0x7b033000) /* from /etc/conf/h/param.h */
1057 # else
1058 /* Gustavo Rodriguez-Rivera suggested changing HEURISTIC2 */
1059 /* to this. We'll probably do this on other platforms, too. */
1060 /* For now I'll use it where I can test it. */
1061 extern char ** environ;
1062 # define STACKBOTTOM ((ptr_t)environ)
1063 # endif
1064 # define STACK_GROWS_UP
1065 # define DYNAMIC_LOADING
1066 # ifndef HPUX_THREADS
1067 # define MPROTECT_VDB
1068 # endif
1069 # include <unistd.h>
1070 # define GETPAGESIZE() sysconf(_SC_PAGE_SIZE)
1071 # endif
1072
1073 # ifdef ALPHA
1074 # define MACH_TYPE "ALPHA"
1075 # define ALIGNMENT 8
1076 # define USE_GENERIC_PUSH_REGS
1077 /* Gcc and probably the DEC/Compaq compiler spill pointers to preserved */
1078 /* fp registers in some cases when the target is a 21264. The assembly */
1079 /* code doesn't handle that yet, and version dependencies make that a */
1080 /* bit tricky. Do the easy thing for now. */
1081 # ifdef OSF1
1082 # define OS_TYPE "OSF1"
1083 # define DATASTART ((ptr_t) 0x140000000)
1084 extern _end;
1085 # define DATAEND ((ptr_t) &_end)
1086 # define HEURISTIC2
1087 /* Normally HEURISTIC2 is too conervative, since */
1088 /* the text segment immediately follows the stack. */
1089 /* Hence we give an upper pound. */
1090 extern int __start;
1091 # define HEURISTIC2_LIMIT ((ptr_t)((word)(&__start) & ~(getpagesize()-1)))
1092 # define CPP_WORDSZ 64
1093 # define MPROTECT_VDB
1094 # define DYNAMIC_LOADING
1095 # endif
1096 # ifdef LINUX
1097 # define OS_TYPE "LINUX"
1098 # define CPP_WORDSZ 64
1099 # define STACKBOTTOM ((ptr_t) 0x120000000)
1100 # ifdef __ELF__
1101 # define LINUX_DATA_START
1102 # define DYNAMIC_LOADING
1103 /* This doesn't work if the collector is in a dynamic library. */
1104 # else
1105 # define DATASTART ((ptr_t) 0x140000000)
1106 # endif
1107 extern int _end;
1108 # define DATAEND (&_end)
1109 # define MPROTECT_VDB
1110 /* Has only been superficially tested. May not */
1111 /* work on all versions. */
1112 # endif
1113 # endif
1114
1115 # ifdef IA64
1116 # define MACH_TYPE "IA64"
1117 # define ALIGN_DOUBLE
1118 /* Requires 16 byte alignment for malloc */
1119 # define ALIGNMENT 8
1120 # define USE_GENERIC_PUSH_REGS
1121 /* We need to get preserved registers in addition to register windows. */
1122 /* That's easiest to do with setjmp. */
1123 # ifdef HPUX
1124 --> needs work
1125 # endif
1126 # ifdef LINUX
1127 # define OS_TYPE "LINUX"
1128 # define CPP_WORDSZ 64
1129 /* This should really be done through /proc, but that */
1130 /* requires we run on an IA64 kernel. */
1131 # define STACKBOTTOM ((ptr_t) 0xa000000000000000l)
1132 /* We also need the base address of the register stack */
1133 /* backing store. There is probably a better way to */
1134 /* get that, too ... */
1135 # define BACKING_STORE_BASE ((ptr_t) 0x9fffffff80000000l)
1136 # if 1
1137 # define SEARCH_FOR_DATA_START
1138 # define DATASTART GC_data_start
1139 # else
1140 extern int data_start;
1141 # define DATASTART ((ptr_t)(&data_start))
1142 # endif
1143 # define DYNAMIC_LOADING
1144 # define MPROTECT_VDB
1145 /* Requires Linux 2.3.47 or later. */
1146 extern int _end;
1147 # define DATAEND (&_end)
1148 # define PREFETCH(x) \
1149 __asm__ (" lfetch [%0]": : "r"((void *)(x)))
1150 # define PREFETCH_FOR_WRITE(x) \
1151 __asm__ (" lfetch.excl [%0]": : "r"((void *)(x)))
1152 # define CLEAR_DOUBLE(x) \
1153 __asm__ (" stf.spill [%0]=f0": : "r"((void *)(x)))
1154 # endif
1155 # endif
1156
1157 # ifdef M88K
1158 # define MACH_TYPE "M88K"
1159 # define ALIGNMENT 4
1160 # define ALIGN_DOUBLE
1161 extern int etext;
1162 # ifdef CX_UX
1163 # define OS_TYPE "CX_UX"
1164 # define DATASTART ((((word)&etext + 0x3fffff) & ~0x3fffff) + 0x10000)
1165 # endif
1166 # ifdef DGUX
1167 # define OS_TYPE "DGUX"
1168 extern char * GC_SysVGetDataStart();
1169 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &etext)
1170 # endif
1171 # define STACKBOTTOM ((char*)0xf0000000) /* determined empirically */
1172 # endif
1173
1174 # ifdef S370
1175 # define MACH_TYPE "S370"
1176 # define OS_TYPE "UTS4"
1177 # define ALIGNMENT 4 /* Required by hardware */
1178 extern int etext;
1179 extern int _etext;
1180 extern int _end;
1181 extern char * GC_SysVGetDataStart();
1182 # define DATASTART (ptr_t)GC_SysVGetDataStart(0x10000, &_etext)
1183 # define DATAEND (&_end)
1184 # define HEURISTIC2
1185 # endif
1186
1187 # if defined(PJ)
1188 # define ALIGNMENT 4
1189 extern int _etext;
1190 # define DATASTART ((ptr_t)(&_etext))
1191 # define HEURISTIC1
1192 # endif
1193
1194 # ifdef ARM32
1195 # define CPP_WORDSZ 32
1196 # define MACH_TYPE "ARM32"
1197 # define ALIGNMENT 4
1198 # ifdef NETBSD
1199 # define OS_TYPE "NETBSD"
1200 # define HEURISTIC2
1201 extern char etext;
1202 # define DATASTART ((ptr_t)(&etext))
1203 # define USE_GENERIC_PUSH_REGS
1204 # endif
1205 # ifdef LINUX
1206 # define OS_TYPE "LINUX"
1207 # define HEURISTIC1
1208 # undef STACK_GRAN
1209 # define STACK_GRAN 0x10000000
1210 # define USE_GENERIC_PUSH_REGS
1211 # ifdef __ELF__
1212 # define DYNAMIC_LOADING
1213 # include <features.h>
1214 # if defined(__GLIBC__) && __GLIBC__ >= 2
1215 # define LINUX_DATA_START
1216 # else
1217 extern char **__environ;
1218 # define DATASTART ((ptr_t)(&__environ))
1219 /* hideous kludge: __environ is the first */
1220 /* word in crt0.o, and delimits the start */
1221 /* of the data segment, no matter which */
1222 /* ld options were passed through. */
1223 /* We could use _etext instead, but that */
1224 /* would include .rodata, which may */
1225 /* contain large read-only data tables */
1226 /* that we'd rather not scan. */
1227 # endif
1228 extern int _end;
1229 # define DATAEND (&_end)
1230 # else
1231 extern int etext;
1232 # define DATASTART ((ptr_t)((((word) (&etext)) + 0xfff) & ~0xfff))
1233 # endif
1234 # endif
1235 #endif
1236
1237 #ifdef LINUX_DATA_START
1238 /* Some Linux distributions arrange to define __data_start. Some */
1239 /* define data_start as a weak symbol. The latter is technically */
1240 /* broken, since the user program may define data_start, in which */
1241 /* case we lose. Nonetheless, we try both, prefering __data_start. */
1242 /* We assume gcc. */
1243 # pragma weak __data_start
1244 extern int __data_start;
1245 # pragma weak data_start
1246 extern int data_start;
1247 # define DATASTART ((ptr_t)(&__data_start != 0? &__data_start : &data_start))
1248 #endif
1249
1250 # ifndef STACK_GROWS_UP
1251 # define STACK_GROWS_DOWN
1252 # endif
1253
1254 # ifndef CPP_WORDSZ
1255 # define CPP_WORDSZ 32
1256 # endif
1257
1258 # ifndef OS_TYPE
1259 # define OS_TYPE ""
1260 # endif
1261
1262 # ifndef DATAEND
1263 extern int end;
1264 # define DATAEND (&end)
1265 # endif
1266
1267 # if defined(SVR4) && !defined(GETPAGESIZE)
1268 # include <unistd.h>
1269 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
1270 # endif
1271
1272 # ifndef GETPAGESIZE
1273 # if defined(SUNOS5) || defined(IRIX5)
1274 # include <unistd.h>
1275 # endif
1276 # define GETPAGESIZE() getpagesize()
1277 # endif
1278
1279 # if defined(SUNOS5) || defined(DRSNX) || defined(UTS4)
1280 /* OS has SVR4 generic features. Probably others also qualify. */
1281 # define SVR4
1282 # endif
1283
1284 # if defined(SUNOS5) || defined(DRSNX)
1285 /* OS has SUNOS5 style semi-undocumented interface to dynamic */
1286 /* loader. */
1287 # define SUNOS5DL
1288 /* OS has SUNOS5 style signal handlers. */
1289 # define SUNOS5SIGS
1290 # endif
1291
1292 # if defined(HPUX)
1293 # define SUNOS5SIGS
1294 # endif
1295
1296 # if CPP_WORDSZ != 32 && CPP_WORDSZ != 64
1297 -> bad word size
1298 # endif
1299
1300 # ifdef PCR
1301 # undef DYNAMIC_LOADING
1302 # undef STACKBOTTOM
1303 # undef HEURISTIC1
1304 # undef HEURISTIC2
1305 # undef PROC_VDB
1306 # undef MPROTECT_VDB
1307 # define PCR_VDB
1308 # endif
1309
1310 # ifdef SRC_M3
1311 /* Postponed for now. */
1312 # undef PROC_VDB
1313 # undef MPROTECT_VDB
1314 # endif
1315
1316 # ifdef SMALL_CONFIG
1317 /* Presumably not worth the space it takes. */
1318 # undef PROC_VDB
1319 # undef MPROTECT_VDB
1320 # endif
1321
1322 # ifdef USE_MUNMAP
1323 # undef MPROTECT_VDB /* Can't deal with address space holes. */
1324 # endif
1325
1326 # if !defined(PCR_VDB) && !defined(PROC_VDB) && !defined(MPROTECT_VDB)
1327 # define DEFAULT_VDB
1328 # endif
1329
1330 # ifndef PREFETCH
1331 # define PREFETCH(x)
1332 # define NO_PREFETCH
1333 # endif
1334
1335 # ifndef PREFETCH_FOR_WRITE
1336 # define PREFETCH_FOR_WRITE(x)
1337 # define NO_PREFETCH_FOR_WRITE
1338 # endif
1339
1340 # ifndef CACHE_LINE_SIZE
1341 # define CACHE_LINE_SIZE 32 /* Wild guess */
1342 # endif
1343
1344 # ifndef CLEAR_DOUBLE
1345 # define CLEAR_DOUBLE(x) \
1346 ((word*)x)[0] = 0; \
1347 ((word*)x)[1] = 0;
1348 # endif /* CLEAR_DOUBLE */
1349
1350 # if defined(_SOLARIS_PTHREADS) && !defined(SOLARIS_THREADS)
1351 # define SOLARIS_THREADS
1352 # endif
1353 # if defined(IRIX_THREADS) && !defined(IRIX5)
1354 --> inconsistent configuration
1355 # endif
1356 # if defined(IRIX_JDK_THREADS) && !defined(IRIX5)
1357 --> inconsistent configuration
1358 # endif
1359 # if defined(LINUX_THREADS) && !defined(LINUX)
1360 --> inconsistent configuration
1361 # endif
1362 # if defined(SOLARIS_THREADS) && !defined(SUNOS5)
1363 --> inconsistent configuration
1364 # endif
1365 # if defined(HPUX_THREADS) && !defined(HPUX)
1366 --> inconsistent configuration
1367 # endif
1368 # if defined(PCR) || defined(SRC_M3) || \
1369 defined(SOLARIS_THREADS) || defined(WIN32_THREADS) || \
1370 defined(IRIX_THREADS) || defined(LINUX_THREADS) || \
1371 defined(IRIX_JDK_THREADS) || defined(HPUX_THREADS)
1372 # define THREADS
1373 # endif
1374
1375 # if defined(HP_PA) || defined(M88K) || defined(POWERPC) \
1376 || (defined(I386) && defined(OS2)) || defined(UTS4) || defined(LINT)
1377 /* Use setjmp based hack to mark from callee-save registers. */
1378 # define USE_GENERIC_PUSH_REGS
1379 # endif
1380 # if defined(SPARC) && !defined(LINUX)
1381 # define SAVE_CALL_CHAIN
1382 # define ASM_CLEAR_CODE /* Stack clearing is crucial, and we */
1383 /* include assembly code to do it well. */
1384 # endif
1385
1386 # if defined(LINUX) && !defined(POWERPC)
1387
1388 # if 0
1389 # include <linux/version.h>
1390 # if (LINUX_VERSION_CODE <= 0x10400)
1391 /* Ugly hack to get struct sigcontext_struct definition. Required */
1392 /* for some early 1.3.X releases. Will hopefully go away soon. */
1393 /* in some later Linux releases, asm/sigcontext.h may have to */
1394 /* be included instead. */
1395 # define __KERNEL__
1396 # include <asm/signal.h>
1397 # undef __KERNEL__
1398 # endif
1399
1400 # else
1401
1402 /* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
1403 /* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
1404 /* prototypes, so we have to include the top-level sigcontext.h to */
1405 /* make sure the former gets defined to be the latter if appropriate. */
1406 # include <features.h>
1407 # if 2 <= __GLIBC__
1408 # if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
1409 /* glibc 2.1 no longer has sigcontext.h. But signal.h */
1410 /* has the right declaration for glibc 2.1. */
1411 # include <sigcontext.h>
1412 # endif /* 0 == __GLIBC_MINOR__ */
1413 # else /* not 2 <= __GLIBC__ */
1414 /* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
1415 /* one. Check LINUX_VERSION_CODE to see which we should reference. */
1416 # include <asm/sigcontext.h>
1417 # endif /* 2 <= __GLIBC__ */
1418 # endif
1419 # endif
1420 # if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
1421 # include <sys/types.h>
1422 # if !defined(MSWIN32) && !defined(SUNOS4)
1423 # include <unistd.h>
1424 # endif
1425 # endif
1426
1427 # include <signal.h>
1428
1429 /* Blatantly OS dependent routines, except for those that are related */
1430 /* to dynamic loading. */
1431
1432 # if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
1433 # define NEED_FIND_LIMIT
1434 # endif
1435
1436 # if defined(IRIX_THREADS) || defined(HPUX_THREADS)
1437 # define NEED_FIND_LIMIT
1438 # endif
1439
1440 # if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
1441 # define NEED_FIND_LIMIT
1442 # endif
1443
1444 # if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
1445 # define NEED_FIND_LIMIT
1446 # endif
1447
1448 # if defined(LINUX) && \
1449 (defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
1450 || defined(MIPS))
1451 # define NEED_FIND_LIMIT
1452 # endif
1453
1454 #ifdef NEED_FIND_LIMIT
1455 # include <setjmp.h>
1456 #endif
1457
1458 #ifdef FREEBSD
1459 # include <machine/trap.h>
1460 #endif
1461
1462 #ifdef AMIGA
1463 # include <proto/exec.h>
1464 # include <proto/dos.h>
1465 # include <dos/dosextens.h>
1466 # include <workbench/startup.h>
1467 #endif
1468
1469 #ifdef MSWIN32
1470 # define WIN32_LEAN_AND_MEAN
1471 # define NOSERVICE
1472 # include <windows.h>
1473 #endif
1474
1475 #ifdef MACOS
1476 # include <Processes.h>
1477 #endif
1478
1479 #ifdef IRIX5
1480 # include <sys/uio.h>
1481 # include <malloc.h> /* for locking */
1482 #endif
1483 #ifdef USE_MMAP
1484 # include <sys/types.h>
1485 # include <sys/mman.h>
1486 # include <sys/stat.h>
1487 # include <fcntl.h>
1488 #endif
1489
1490 #ifdef SUNOS5SIGS
1491 # include <sys/siginfo.h>
1492 # undef setjmp
1493 # undef longjmp
1494 # define setjmp(env) sigsetjmp(env, 1)
1495 # define longjmp(env, val) siglongjmp(env, val)
1496 # define jmp_buf sigjmp_buf
1497 #endif
1498
1499 #ifdef DJGPP
1500 /* Apparently necessary for djgpp 2.01. May casuse problems with */
1501 /* other versions. */
1502 typedef long unsigned int caddr_t;
1503 #endif
1504
1505 #ifdef PCR
1506 # include "il/PCR_IL.h"
1507 # include "th/PCR_ThCtl.h"
1508 # include "mm/PCR_MM.h"
1509 #endif
1510
1511 #if !defined(NO_EXECUTE_PERMISSION)
1512 # define OPT_PROT_EXEC PROT_EXEC
1513 #else
1514 # define OPT_PROT_EXEC 0
1515 #endif
1516
1517 # ifdef OS2
1518
1519 # include <stddef.h>
1520
1521 # if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
1522
1523 # else /* IBM's compiler */
1524
1525 /* A kludge to get around what appears to be a header file bug */
1526 # ifndef WORD
1527 # define WORD unsigned short
1528 # endif
1529 # ifndef DWORD
1530 # define DWORD unsigned long
1531 # endif
1532
1533 # define EXE386 1
1534 # include <newexe.h>
1535 # include <exe386.h>
1536
1537 # endif /* __IBMC__ */
1538
1539 # define INCL_DOSEXCEPTIONS
1540 # define INCL_DOSPROCESS
1541 # define INCL_DOSERRORS
1542 # define INCL_DOSMODULEMGR
1543 # define INCL_DOSMEMMGR
1544 # include <os2.h>
1545
1546 # endif /*!OS/2 */
1547
1548 /*
1549 * Find the base of the stack.
1550 * Used only in single-threaded environment.
1551 * With threads, GC_mark_roots needs to know how to do this.
1552 * Called with allocator lock held.
1553 */
1554 # ifdef MSWIN32
1555 # define is_writable(prot) ((prot) == PAGE_READWRITE \
1556 || (prot) == PAGE_WRITECOPY \
1557 || (prot) == PAGE_EXECUTE_READWRITE \
1558 || (prot) == PAGE_EXECUTE_WRITECOPY)
1559 /* Return the number of bytes that are writable starting at p. */
1560 /* The pointer p is assumed to be page aligned. */
1561 /* If base is not 0, *base becomes the beginning of the */
1562 /* allocation region containing p. */
1563 static word GC_get_writable_length(ptr_t p, ptr_t *base)
1564 {
1565 MEMORY_BASIC_INFORMATION buf;
1566 word result;
1567 word protect;
1568
1569 result = VirtualQuery(p, &buf, sizeof(buf));
1570 if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
1571 if (base != 0) *base = (ptr_t)(buf.AllocationBase);
1572 protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
1573 if (!is_writable(protect)) {
1574 return(0);
1575 }
1576 if (buf.State != MEM_COMMIT) return(0);
1577 return(buf.RegionSize);
1578 }
1579
1580 void *scm_get_stack_base()
1581 {
1582 int dummy;
1583 ptr_t sp = (ptr_t)(&dummy);
1584 ptr_t trunc_sp;
1585 word size;
1586 static word GC_page_size = 0;
1587 if (!GC_page_size) {
1588 SYSTEM_INFO sysinfo;
1589 GetSystemInfo(&sysinfo);
1590 GC_page_size = sysinfo.dwPageSize;
1591 }
1592 trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
1593 size = GC_get_writable_length(trunc_sp, 0);
1594 return(trunc_sp + size);
1595 }
1596
1597
1598 # else
1599
1600 # ifdef OS2
1601
1602 void *scm_get_stack_base()
1603 {
1604 PTIB ptib;
1605 PPIB ppib;
1606
1607 if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
1608 GC_err_printf0("DosGetInfoBlocks failed\n");
1609 ABORT("DosGetInfoBlocks failed\n");
1610 }
1611 return((ptr_t)(ptib -> tib_pstacklimit));
1612 }
1613
1614 # else
1615
1616 # ifdef AMIGA
1617
1618 void *scm_get_stack_base()
1619 {
1620 struct Process *proc = (struct Process*)SysBase->ThisTask;
1621
1622 /* Reference: Amiga Guru Book Pages: 42,567,574 */
1623 if (proc->pr_Task.tc_Node.ln_Type==NT_PROCESS
1624 && proc->pr_CLI != NULL) {
1625 /* first ULONG is StackSize */
1626 /*longPtr = proc->pr_ReturnAddr;
1627 size = longPtr[0];*/
1628
1629 return (char *)proc->pr_ReturnAddr + sizeof(ULONG);
1630 } else {
1631 return (char *)proc->pr_Task.tc_SPUpper;
1632 }
1633 }
1634
1635 #if 0 /* old version */
1636 void *scm_get_stack_base()
1637 {
1638 extern struct WBStartup *_WBenchMsg;
1639 extern long __base;
1640 extern long __stack;
1641 struct Task *task;
1642 struct Process *proc;
1643 struct CommandLineInterface *cli;
1644 long size;
1645
1646 if ((task = FindTask(0)) == 0) {
1647 GC_err_puts("Cannot find own task structure\n");
1648 ABORT("task missing");
1649 }
1650 proc = (struct Process *)task;
1651 cli = BADDR(proc->pr_CLI);
1652
1653 if (_WBenchMsg != 0 || cli == 0) {
1654 size = (char *)task->tc_SPUpper - (char *)task->tc_SPLower;
1655 } else {
1656 size = cli->cli_DefaultStack * 4;
1657 }
1658 return (ptr_t)(__base + GC_max(size, __stack));
1659 }
1660 #endif /* 0 */
1661
1662 # else /* !AMIGA, !OS2, ... */
1663
1664 # ifdef NEED_FIND_LIMIT
1665 /* Some tools to implement HEURISTIC2 */
1666 # define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
1667 /* static */ jmp_buf GC_jmp_buf;
1668
1669 /*ARGSUSED*/
1670 static void GC_fault_handler(sig)
1671 int sig;
1672 {
1673 longjmp(GC_jmp_buf, 1);
1674 }
1675
1676 # ifdef __STDC__
1677 typedef void (*handler)(int);
1678 # else
1679 typedef void (*handler)();
1680 # endif
1681
1682 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1683 static struct sigaction old_segv_act;
1684 # if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
1685 static struct sigaction old_bus_act;
1686 # endif
1687 # else
1688 static handler old_segv_handler, old_bus_handler;
1689 # endif
1690
1691 static void GC_setup_temporary_fault_handler()
1692 {
1693 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1694 struct sigaction act;
1695
1696 act.sa_handler = GC_fault_handler;
1697 act.sa_flags = SA_RESTART | SA_NODEFER;
1698 /* The presence of SA_NODEFER represents yet another gross */
1699 /* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
1700 /* interact correctly with -lthread. We hide the confusion */
1701 /* by making sure that signal handling doesn't affect the */
1702 /* signal mask. */
1703
1704 (void) sigemptyset(&act.sa_mask);
1705 # ifdef IRIX_THREADS
1706 /* Older versions have a bug related to retrieving and */
1707 /* and setting a handler at the same time. */
1708 (void) sigaction(SIGSEGV, 0, &old_segv_act);
1709 (void) sigaction(SIGSEGV, &act, 0);
1710 # else
1711 (void) sigaction(SIGSEGV, &act, &old_segv_act);
1712 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1713 || defined(HPUX)
1714 /* Under Irix 5.x or HP/UX, we may get SIGBUS. */
1715 /* Pthreads doesn't exist under Irix 5.x, so we */
1716 /* don't have to worry in the threads case. */
1717 (void) sigaction(SIGBUS, &act, &old_bus_act);
1718 # endif
1719 # endif /* IRIX_THREADS */
1720 # else
1721 old_segv_handler = signal(SIGSEGV, GC_fault_handler);
1722 # ifdef SIGBUS
1723 old_bus_handler = signal(SIGBUS, GC_fault_handler);
1724 # endif
1725 # endif
1726 }
1727
1728 static void GC_reset_fault_handler()
1729 {
1730 # if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
1731 (void) sigaction(SIGSEGV, &old_segv_act, 0);
1732 # if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
1733 || defined(HPUX)
1734 (void) sigaction(SIGBUS, &old_bus_act, 0);
1735 # endif
1736 # else
1737 (void) signal(SIGSEGV, old_segv_handler);
1738 # ifdef SIGBUS
1739 (void) signal(SIGBUS, old_bus_handler);
1740 # endif
1741 # endif
1742 }
1743
1744 /* Return the first nonaddressible location > p (up) or */
1745 /* the smallest location q s.t. [q,p] is addressible (!up). */
1746 static ptr_t GC_find_limit(p, up)
1747 ptr_t p;
1748 GC_bool up;
1749 {
1750 static VOLATILE ptr_t result;
1751 /* Needs to be static, since otherwise it may not be */
1752 /* preserved across the longjmp. Can safely be */
1753 /* static since it's only called once, with the */
1754 /* allocation lock held. */
1755
1756
1757 GC_setup_temporary_fault_handler();
1758 if (setjmp(GC_jmp_buf) == 0) {
1759 result = (ptr_t)(((word)(p))
1760 & ~(MIN_PAGE_SIZE-1));
1761 for (;;) {
1762 if (up) {
1763 result += MIN_PAGE_SIZE;
1764 } else {
1765 result -= MIN_PAGE_SIZE;
1766 }
1767 GC_noop1((word)(*result));
1768 }
1769 }
1770 GC_reset_fault_handler();
1771 if (!up) {
1772 result += MIN_PAGE_SIZE;
1773 }
1774 return(result);
1775 }
1776 # endif
1777
1778 #ifdef LINUX_STACKBOTTOM
1779
1780 #include <sys/types.h>
1781 #include <sys/stat.h>
1782 #include <fcntl.h>
1783
1784 # define STAT_SKIP 27 /* Number of fields preceding startstack */
1785 /* field in /proc/self/stat */
1786
1787 static ptr_t GC_linux_stack_base(void)
1788 {
1789 /* We read the stack base value from /proc/self/stat. We do this */
1790 /* using direct I/O system calls in order to avoid calling malloc */
1791 /* in case REDIRECT_MALLOC is defined. */
1792 # define STAT_BUF_SIZE 4096
1793 # ifdef USE_LD_WRAP
1794 # define STAT_READ __real_read
1795 # else
1796 # define STAT_READ read
1797 # endif
1798 char stat_buf[STAT_BUF_SIZE];
1799 int f;
1800 char c;
1801 word result = 0;
1802 size_t i, buf_offset = 0;
1803
1804 f = open("/proc/self/stat", O_RDONLY);
1805 if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
1806 ABORT("Couldn't read /proc/self/stat");
1807 }
1808 c = stat_buf[buf_offset++];
1809 /* Skip the required number of fields. This number is hopefully */
1810 /* constant across all Linux implementations. */
1811 for (i = 0; i < STAT_SKIP; ++i) {
1812 while (isspace(c)) c = stat_buf[buf_offset++];
1813 while (!isspace(c)) c = stat_buf[buf_offset++];
1814 }
1815 while (isspace(c)) c = stat_buf[buf_offset++];
1816 while (isdigit(c)) {
1817 result *= 10;
1818 result += c - '0';
1819 c = stat_buf[buf_offset++];
1820 }
1821 close(f);
1822 if (result < 0x10000000) ABORT("Absurd stack bottom value");
1823 return (ptr_t)result;
1824 }
1825
1826 #endif /* LINUX_STACKBOTTOM */
1827
1828 void *scm_get_stack_base()
1829 {
1830 word dummy;
1831 void *result = &dummy; /* initialize to silence compiler */
1832
1833 # define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
1834
1835 # ifdef STACKBOTTOM
1836 return(STACKBOTTOM);
1837 # else
1838 # ifdef HEURISTIC1
1839 # ifdef STACK_GROWS_DOWN
1840 result = (ptr_t)((((word)(&dummy))
1841 + STACKBOTTOM_ALIGNMENT_M1)
1842 & ~STACKBOTTOM_ALIGNMENT_M1);
1843 # else
1844 result = (ptr_t)(((word)(&dummy))
1845 & ~STACKBOTTOM_ALIGNMENT_M1);
1846 # endif
1847 # endif /* HEURISTIC1 */
1848 # ifdef LINUX_STACKBOTTOM
1849 result = GC_linux_stack_base();
1850 # endif
1851 # ifdef HEURISTIC2
1852 # ifdef STACK_GROWS_DOWN
1853 result = GC_find_limit((ptr_t)(&dummy), TRUE);
1854 # ifdef HEURISTIC2_LIMIT
1855 if (result > HEURISTIC2_LIMIT
1856 && (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
1857 result = HEURISTIC2_LIMIT;
1858 }
1859 # endif
1860 # else
1861 result = GC_find_limit((ptr_t)(&dummy), FALSE);
1862 # ifdef HEURISTIC2_LIMIT
1863 if (result < HEURISTIC2_LIMIT
1864 && (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
1865 result = HEURISTIC2_LIMIT;
1866 }
1867 # endif
1868 # endif
1869
1870 # endif /* HEURISTIC2 */
1871 # ifdef STACK_GROWS_DOWN
1872 if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
1873 # endif
1874 return(result);
1875 # endif /* STACKBOTTOM */
1876 }
1877
1878 # endif /* ! AMIGA */
1879 # endif /* ! OS2 */
1880 # endif /* ! MSWIN32 */