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