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