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