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