Skip to content

Latest commit

 

History

History
2606 lines (2280 loc) · 71.7 KB

os_dep.c

File metadata and controls

2606 lines (2280 loc) · 71.7 KB
 
Oct 2, 2000
Oct 2, 2000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
* Copyright (c) 1991-1995 by Xerox Corporation. All rights reserved.
* Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
* Copyright (c) 1999 by Hewlett-Packard Company. All rights reserved.
*
* THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
* OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
*
* Permission is hereby granted to use or copy this program
* for any purpose, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*/
# include "gc_priv.h"
# if defined(LINUX) && !defined(POWERPC)
# include <linux/version.h>
# if (LINUX_VERSION_CODE <= 0x10400)
/* Ugly hack to get struct sigcontext_struct definition. Required */
/* for some early 1.3.X releases. Will hopefully go away soon. */
/* in some later Linux releases, asm/sigcontext.h may have to */
/* be included instead. */
# define __KERNEL__
# include <asm/signal.h>
# undef __KERNEL__
# else
/* Kernels prior to 2.1.1 defined struct sigcontext_struct instead of */
/* struct sigcontext. libc6 (glibc2) uses "struct sigcontext" in */
/* prototypes, so we have to include the top-level sigcontext.h to */
/* make sure the former gets defined to be the latter if appropriate. */
# include <features.h>
# if 2 <= __GLIBC__
# if 2 == __GLIBC__ && 0 == __GLIBC_MINOR__
/* glibc 2.1 no longer has sigcontext.h. But signal.h */
/* has the right declaration for glibc 2.1. */
# include <sigcontext.h>
# endif /* 0 == __GLIBC_MINOR__ */
# else /* not 2 <= __GLIBC__ */
/* libc5 doesn't have <sigcontext.h>: go directly with the kernel */
/* one. Check LINUX_VERSION_CODE to see which we should reference. */
# include <asm/sigcontext.h>
# endif /* 2 <= __GLIBC__ */
# endif
# endif
# if !defined(OS2) && !defined(PCR) && !defined(AMIGA) && !defined(MACOS)
# include <sys/types.h>
# if !defined(MSWIN32) && !defined(SUNOS4)
# include <unistd.h>
# endif
# endif
# include <stdio.h>
# include <signal.h>
/* Blatantly OS dependent routines, except for those that are related */
/* to dynamic loading. */
# if !defined(THREADS) && !defined(STACKBOTTOM) && defined(HEURISTIC2)
# define NEED_FIND_LIMIT
# endif
# if defined(IRIX_THREADS) || defined(HPUX_THREADS)
# define NEED_FIND_LIMIT
# endif
# if (defined(SUNOS4) && defined(DYNAMIC_LOADING)) && !defined(PCR)
# define NEED_FIND_LIMIT
# endif
# if (defined(SVR4) || defined(AUX) || defined(DGUX)) && !defined(PCR)
# define NEED_FIND_LIMIT
# endif
# if defined(LINUX) && \
(defined(POWERPC) || defined(SPARC) || defined(ALPHA) || defined(IA64) \
|| defined(MIPS))
# define NEED_FIND_LIMIT
# endif
#ifdef NEED_FIND_LIMIT
# include <setjmp.h>
#endif
#ifdef FREEBSD
# include <machine/trap.h>
#endif
#ifdef AMIGA
# include <proto/exec.h>
# include <proto/dos.h>
# include <dos/dosextens.h>
# include <workbench/startup.h>
#endif
#ifdef MSWIN32
# define WIN32_LEAN_AND_MEAN
# define NOSERVICE
# include <windows.h>
#endif
#ifdef MACOS
# include <Processes.h>
#endif
#ifdef IRIX5
# include <sys/uio.h>
# include <malloc.h> /* for locking */
#endif
#ifdef USE_MMAP
# include <sys/types.h>
# include <sys/mman.h>
# include <sys/stat.h>
# include <fcntl.h>
#endif
#ifdef SUNOS5SIGS
# include <sys/siginfo.h>
# undef setjmp
# undef longjmp
# define setjmp(env) sigsetjmp(env, 1)
# define longjmp(env, val) siglongjmp(env, val)
# define jmp_buf sigjmp_buf
#endif
#ifdef DJGPP
/* Apparently necessary for djgpp 2.01. May casuse problems with */
/* other versions. */
typedef long unsigned int caddr_t;
#endif
#ifdef PCR
# include "il/PCR_IL.h"
# include "th/PCR_ThCtl.h"
# include "mm/PCR_MM.h"
#endif
#if !defined(NO_EXECUTE_PERMISSION)
# define OPT_PROT_EXEC PROT_EXEC
#else
# define OPT_PROT_EXEC 0
#endif
#if defined(SEARCH_FOR_DATA_START)
/* The following doesn't work if the GC is in a dynamic library. */
/* The I386 case can be handled without a search. The Alpha case */
/* used to be handled differently as well, but the rules changed */
/* for recent Linux versions. This seems to be the easiest way to */
/* cover all versions. */
ptr_t GC_data_start;
extern char * GC_copyright[]; /* Any data symbol would do. */
void GC_init_linux_data_start()
{
extern ptr_t GC_find_limit();
GC_data_start = GC_find_limit((ptr_t)GC_copyright, FALSE);
}
#endif
# ifdef OS2
# include <stddef.h>
# if !defined(__IBMC__) && !defined(__WATCOMC__) /* e.g. EMX */
struct exe_hdr {
unsigned short magic_number;
unsigned short padding[29];
long new_exe_offset;
};
#define E_MAGIC(x) (x).magic_number
#define EMAGIC 0x5A4D
#define E_LFANEW(x) (x).new_exe_offset
struct e32_exe {
unsigned char magic_number[2];
unsigned char byte_order;
unsigned char word_order;
unsigned long exe_format_level;
unsigned short cpu;
unsigned short os;
unsigned long padding1[13];
unsigned long object_table_offset;
unsigned long object_count;
unsigned long padding2[31];
};
#define E32_MAGIC1(x) (x).magic_number[0]
#define E32MAGIC1 'L'
#define E32_MAGIC2(x) (x).magic_number[1]
#define E32MAGIC2 'X'
#define E32_BORDER(x) (x).byte_order
#define E32LEBO 0
#define E32_WORDER(x) (x).word_order
#define E32LEWO 0
#define E32_CPU(x) (x).cpu
#define E32CPU286 1
#define E32_OBJTAB(x) (x).object_table_offset
#define E32_OBJCNT(x) (x).object_count
struct o32_obj {
unsigned long size;
unsigned long base;
unsigned long flags;
unsigned long pagemap;
unsigned long mapsize;
unsigned long reserved;
};
#define O32_FLAGS(x) (x).flags
#define OBJREAD 0x0001L
#define OBJWRITE 0x0002L
#define OBJINVALID 0x0080L
#define O32_SIZE(x) (x).size
#define O32_BASE(x) (x).base
# else /* IBM's compiler */
/* A kludge to get around what appears to be a header file bug */
# ifndef WORD
# define WORD unsigned short
# endif
# ifndef DWORD
# define DWORD unsigned long
# endif
# define EXE386 1
# include <newexe.h>
# include <exe386.h>
# endif /* __IBMC__ */
# define INCL_DOSEXCEPTIONS
# define INCL_DOSPROCESS
# define INCL_DOSERRORS
# define INCL_DOSMODULEMGR
# define INCL_DOSMEMMGR
# include <os2.h>
/* Disable and enable signals during nontrivial allocations */
void GC_disable_signals(void)
{
ULONG nest;
DosEnterMustComplete(&nest);
if (nest != 1) ABORT("nested GC_disable_signals");
}
void GC_enable_signals(void)
{
ULONG nest;
DosExitMustComplete(&nest);
if (nest != 0) ABORT("GC_enable_signals");
}
# else
# if !defined(PCR) && !defined(AMIGA) && !defined(MSWIN32) \
&& !defined(MACOS) && !defined(DJGPP) && !defined(DOS4GW)
# if defined(sigmask) && !defined(UTS4)
/* Use the traditional BSD interface */
# define SIGSET_T int
# define SIG_DEL(set, signal) (set) &= ~(sigmask(signal))
# define SIG_FILL(set) (set) = 0x7fffffff
/* Setting the leading bit appears to provoke a bug in some */
/* longjmp implementations. Most systems appear not to have */
/* a signal 32. */
# define SIGSETMASK(old, new) (old) = sigsetmask(new)
# else
/* Use POSIX/SYSV interface */
# define SIGSET_T sigset_t
# define SIG_DEL(set, signal) sigdelset(&(set), (signal))
# define SIG_FILL(set) sigfillset(&set)
# define SIGSETMASK(old, new) sigprocmask(SIG_SETMASK, &(new), &(old))
# endif
static GC_bool mask_initialized = FALSE;
static SIGSET_T new_mask;
static SIGSET_T old_mask;
static SIGSET_T dummy;
#if defined(PRINTSTATS) && !defined(THREADS)
# define CHECK_SIGNALS
int GC_sig_disabled = 0;
#endif
void GC_disable_signals()
{
if (!mask_initialized) {
SIG_FILL(new_mask);
SIG_DEL(new_mask, SIGSEGV);
SIG_DEL(new_mask, SIGILL);
SIG_DEL(new_mask, SIGQUIT);
# ifdef SIGBUS
SIG_DEL(new_mask, SIGBUS);
# endif
# ifdef SIGIOT
SIG_DEL(new_mask, SIGIOT);
# endif
# ifdef SIGEMT
SIG_DEL(new_mask, SIGEMT);
# endif
# ifdef SIGTRAP
SIG_DEL(new_mask, SIGTRAP);
# endif
mask_initialized = TRUE;
}
# ifdef CHECK_SIGNALS
if (GC_sig_disabled != 0) ABORT("Nested disables");
GC_sig_disabled++;
# endif
SIGSETMASK(old_mask,new_mask);
}
void GC_enable_signals()
{
# ifdef CHECK_SIGNALS
if (GC_sig_disabled != 1) ABORT("Unmatched enable");
GC_sig_disabled--;
# endif
SIGSETMASK(dummy,old_mask);
}
# endif /* !PCR */
# endif /*!OS/2 */
/* Ivan Demakov: simplest way (to me) */
#ifdef DOS4GW
void GC_disable_signals() { }
void GC_enable_signals() { }
#endif
/* Find the page size */
word GC_page_size;
# ifdef MSWIN32
void GC_setpagesize()
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
GC_page_size = sysinfo.dwPageSize;
}
# else
# if defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(USE_MMAP) \
|| defined(USE_MUNMAP)
void GC_setpagesize()
{
GC_page_size = GETPAGESIZE();
}
# else
/* It's acceptable to fake it. */
void GC_setpagesize()
{
GC_page_size = HBLKSIZE;
}
# endif
# endif
/*
* Find the base of the stack.
* Used only in single-threaded environment.
* With threads, GC_mark_roots needs to know how to do this.
* Called with allocator lock held.
*/
# ifdef MSWIN32
# define is_writable(prot) ((prot) == PAGE_READWRITE \
|| (prot) == PAGE_WRITECOPY \
|| (prot) == PAGE_EXECUTE_READWRITE \
|| (prot) == PAGE_EXECUTE_WRITECOPY)
/* Return the number of bytes that are writable starting at p. */
/* The pointer p is assumed to be page aligned. */
/* If base is not 0, *base becomes the beginning of the */
/* allocation region containing p. */
word GC_get_writable_length(ptr_t p, ptr_t *base)
{
MEMORY_BASIC_INFORMATION buf;
word result;
word protect;
result = VirtualQuery(p, &buf, sizeof(buf));
if (result != sizeof(buf)) ABORT("Weird VirtualQuery result");
if (base != 0) *base = (ptr_t)(buf.AllocationBase);
protect = (buf.Protect & ~(PAGE_GUARD | PAGE_NOCACHE));
if (!is_writable(protect)) {
return(0);
}
if (buf.State != MEM_COMMIT) return(0);
return(buf.RegionSize);
}
ptr_t GC_get_stack_base()
{
int dummy;
ptr_t sp = (ptr_t)(&dummy);
ptr_t trunc_sp = (ptr_t)((word)sp & ~(GC_page_size - 1));
word size = GC_get_writable_length(trunc_sp, 0);
return(trunc_sp + size);
}
# else
# ifdef OS2
ptr_t GC_get_stack_base()
{
PTIB ptib;
PPIB ppib;
if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
GC_err_printf0("DosGetInfoBlocks failed\n");
ABORT("DosGetInfoBlocks failed\n");
}
return((ptr_t)(ptib -> tib_pstacklimit));
}
# else
# ifdef AMIGA
ptr_t GC_get_stack_base()
{
struct Process *proc = (struct Process*)SysBase->ThisTask;
/* Reference: Amiga Guru Book Pages: 42,567,574 */
if (proc->pr_Task.tc_Node.ln_Type==NT_PROCESS
&& proc->pr_CLI != NULL) {
/* first ULONG is StackSize */
/*longPtr = proc->pr_ReturnAddr;
size = longPtr[0];*/
return (char *)proc->pr_ReturnAddr + sizeof(ULONG);
} else {
return (char *)proc->pr_Task.tc_SPUpper;
}
}
#if 0 /* old version */
ptr_t GC_get_stack_base()
{
extern struct WBStartup *_WBenchMsg;
extern long __base;
extern long __stack;
struct Task *task;
struct Process *proc;
struct CommandLineInterface *cli;
long size;
if ((task = FindTask(0)) == 0) {
GC_err_puts("Cannot find own task structure\n");
ABORT("task missing");
}
proc = (struct Process *)task;
cli = BADDR(proc->pr_CLI);
if (_WBenchMsg != 0 || cli == 0) {
size = (char *)task->tc_SPUpper - (char *)task->tc_SPLower;
} else {
size = cli->cli_DefaultStack * 4;
}
return (ptr_t)(__base + GC_max(size, __stack));
}
#endif /* 0 */
# else /* !AMIGA, !OS2, ... */
# ifdef NEED_FIND_LIMIT
/* Some tools to implement HEURISTIC2 */
# define MIN_PAGE_SIZE 256 /* Smallest conceivable page size, bytes */
/* static */ jmp_buf GC_jmp_buf;
/*ARGSUSED*/
void GC_fault_handler(sig)
int sig;
{
longjmp(GC_jmp_buf, 1);
}
# ifdef __STDC__
typedef void (*handler)(int);
# else
typedef void (*handler)();
# endif
# if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
static struct sigaction old_segv_act;
# if defined(_sigargs) || defined(HPUX) /* !Irix6.x */
static struct sigaction old_bus_act;
# endif
# else
static handler old_segv_handler, old_bus_handler;
# endif
void GC_setup_temporary_fault_handler()
{
# if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
struct sigaction act;
act.sa_handler = GC_fault_handler;
act.sa_flags = SA_RESTART | SA_NODEFER;
/* The presence of SA_NODEFER represents yet another gross */
/* hack. Under Solaris 2.3, siglongjmp doesn't appear to */
/* interact correctly with -lthread. We hide the confusion */
/* by making sure that signal handling doesn't affect the */
/* signal mask. */
(void) sigemptyset(&act.sa_mask);
# ifdef IRIX_THREADS
/* Older versions have a bug related to retrieving and */
/* and setting a handler at the same time. */
(void) sigaction(SIGSEGV, 0, &old_segv_act);
(void) sigaction(SIGSEGV, &act, 0);
# else
(void) sigaction(SIGSEGV, &act, &old_segv_act);
# if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
|| defined(HPUX)
/* Under Irix 5.x or HP/UX, we may get SIGBUS. */
/* Pthreads doesn't exist under Irix 5.x, so we */
/* don't have to worry in the threads case. */
(void) sigaction(SIGBUS, &act, &old_bus_act);
# endif
# endif /* IRIX_THREADS */
# else
old_segv_handler = signal(SIGSEGV, GC_fault_handler);
# ifdef SIGBUS
old_bus_handler = signal(SIGBUS, GC_fault_handler);
# endif
# endif
}
void GC_reset_fault_handler()
{
# if defined(SUNOS5SIGS) || defined(IRIX5) || defined(OSF1)
(void) sigaction(SIGSEGV, &old_segv_act, 0);
# if defined(IRIX5) && defined(_sigargs) /* Irix 5.x, not 6.x */ \
|| defined(HPUX)
(void) sigaction(SIGBUS, &old_bus_act, 0);
# endif
# else
(void) signal(SIGSEGV, old_segv_handler);
# ifdef SIGBUS
(void) signal(SIGBUS, old_bus_handler);
# endif
# endif
}
/* Return the first nonaddressible location > p (up) or */
/* the smallest location q s.t. [q,p] is addressible (!up). */
ptr_t GC_find_limit(p, up)
ptr_t p;
GC_bool up;
{
static VOLATILE ptr_t result;
/* Needs to be static, since otherwise it may not be */
/* preserved across the longjmp. Can safely be */
/* static since it's only called once, with the */
/* allocation lock held. */
GC_setup_temporary_fault_handler();
if (setjmp(GC_jmp_buf) == 0) {
result = (ptr_t)(((word)(p))
& ~(MIN_PAGE_SIZE-1));
for (;;) {
if (up) {
result += MIN_PAGE_SIZE;
} else {
result -= MIN_PAGE_SIZE;
}
GC_noop1((word)(*result));
}
}
GC_reset_fault_handler();
if (!up) {
result += MIN_PAGE_SIZE;
}
return(result);
}
# endif
#ifdef LINUX_STACKBOTTOM
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
# define STAT_SKIP 27 /* Number of fields preceding startstack */
/* field in /proc/self/stat */
ptr_t GC_linux_stack_base(void)
{
/* We read the stack base value from /proc/self/stat. We do this */
/* using direct I/O system calls in order to avoid calling malloc */
/* in case REDIRECT_MALLOC is defined. */
# define STAT_BUF_SIZE 4096
# ifdef USE_LD_WRAP
# define STAT_READ __real_read
# else
# define STAT_READ read
# endif
char stat_buf[STAT_BUF_SIZE];
int f;
char c;
word result = 0;
size_t i, buf_offset = 0;
f = open("/proc/self/stat", O_RDONLY);
if (f < 0 || STAT_READ(f, stat_buf, STAT_BUF_SIZE) < 2 * STAT_SKIP) {
ABORT("Couldn't read /proc/self/stat");
}
c = stat_buf[buf_offset++];
/* Skip the required number of fields. This number is hopefully */
/* constant across all Linux implementations. */
for (i = 0; i < STAT_SKIP; ++i) {
while (isspace(c)) c = stat_buf[buf_offset++];
while (!isspace(c)) c = stat_buf[buf_offset++];
}
while (isspace(c)) c = stat_buf[buf_offset++];
while (isdigit(c)) {
result *= 10;
result += c - '0';
c = stat_buf[buf_offset++];
}
close(f);
if (result < 0x10000000) ABORT("Absurd stack bottom value");
return (ptr_t)result;
}
#endif /* LINUX_STACKBOTTOM */
ptr_t GC_get_stack_base()
{
word dummy;
ptr_t result;
# define STACKBOTTOM_ALIGNMENT_M1 ((word)STACK_GRAN - 1)
# ifdef STACKBOTTOM
return(STACKBOTTOM);
# else
# ifdef HEURISTIC1
# ifdef STACK_GROWS_DOWN
result = (ptr_t)((((word)(&dummy))
+ STACKBOTTOM_ALIGNMENT_M1)
& ~STACKBOTTOM_ALIGNMENT_M1);
# else
result = (ptr_t)(((word)(&dummy))
& ~STACKBOTTOM_ALIGNMENT_M1);
# endif
# endif /* HEURISTIC1 */
# ifdef LINUX_STACKBOTTOM
result = GC_linux_stack_base();
# endif
# ifdef HEURISTIC2
# ifdef STACK_GROWS_DOWN
result = GC_find_limit((ptr_t)(&dummy), TRUE);
# ifdef HEURISTIC2_LIMIT
if (result > HEURISTIC2_LIMIT
&& (ptr_t)(&dummy) < HEURISTIC2_LIMIT) {
result = HEURISTIC2_LIMIT;
}
# endif
# else
result = GC_find_limit((ptr_t)(&dummy), FALSE);
# ifdef HEURISTIC2_LIMIT
if (result < HEURISTIC2_LIMIT
&& (ptr_t)(&dummy) > HEURISTIC2_LIMIT) {
result = HEURISTIC2_LIMIT;
}
# endif
# endif
# endif /* HEURISTIC2 */
# ifdef STACK_GROWS_DOWN
if (result == 0) result = (ptr_t)(signed_word)(-sizeof(ptr_t));
# endif
return(result);
# endif /* STACKBOTTOM */
}
# endif /* ! AMIGA */
# endif /* ! OS2 */
# endif /* ! MSWIN32 */
/*
* Register static data segment(s) as roots.
* If more data segments are added later then they need to be registered
* add that point (as we do with SunOS dynamic loading),
* or GC_mark_roots needs to check for them (as we do with PCR).
* Called with allocator lock held.
*/
# ifdef OS2
void GC_register_data_segments()
{
PTIB ptib;
PPIB ppib;
HMODULE module_handle;
# define PBUFSIZ 512
UCHAR path[PBUFSIZ];
FILE * myexefile;
struct exe_hdr hdrdos; /* MSDOS header. */
struct e32_exe hdr386; /* Real header for my executable */
struct o32_obj seg; /* Currrent segment */
int nsegs;
if (DosGetInfoBlocks(&ptib, &ppib) != NO_ERROR) {
GC_err_printf0("DosGetInfoBlocks failed\n");
ABORT("DosGetInfoBlocks failed\n");
}
module_handle = ppib -> pib_hmte;
if (DosQueryModuleName(module_handle, PBUFSIZ, path) != NO_ERROR) {
GC_err_printf0("DosQueryModuleName failed\n");
ABORT("DosGetInfoBlocks failed\n");
}
myexefile = fopen(path, "rb");
if (myexefile == 0) {
GC_err_puts("Couldn't open executable ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Failed to open executable\n");
}
if (fread((char *)(&hdrdos), 1, sizeof hdrdos, myexefile) < sizeof hdrdos) {
GC_err_puts("Couldn't read MSDOS header from ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Couldn't read MSDOS header");
}
if (E_MAGIC(hdrdos) != EMAGIC) {
GC_err_puts("Executable has wrong DOS magic number: ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Bad DOS magic number");
}
if (fseek(myexefile, E_LFANEW(hdrdos), SEEK_SET) != 0) {
GC_err_puts("Seek to new header failed in ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Bad DOS magic number");
}
if (fread((char *)(&hdr386), 1, sizeof hdr386, myexefile) < sizeof hdr386) {
GC_err_puts("Couldn't read MSDOS header from ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Couldn't read OS/2 header");
}
if (E32_MAGIC1(hdr386) != E32MAGIC1 || E32_MAGIC2(hdr386) != E32MAGIC2) {
GC_err_puts("Executable has wrong OS/2 magic number:");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Bad OS/2 magic number");
}
if ( E32_BORDER(hdr386) != E32LEBO || E32_WORDER(hdr386) != E32LEWO) {
GC_err_puts("Executable %s has wrong byte order: ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Bad byte order");
}
if ( E32_CPU(hdr386) == E32CPU286) {
GC_err_puts("GC can't handle 80286 executables: ");
GC_err_puts(path); GC_err_puts("\n");
EXIT();
}
if (fseek(myexefile, E_LFANEW(hdrdos) + E32_OBJTAB(hdr386),
SEEK_SET) != 0) {
GC_err_puts("Seek to object table failed: ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Seek to object table failed");
}
for (nsegs = E32_OBJCNT(hdr386); nsegs > 0; nsegs--) {
int flags;
if (fread((char *)(&seg), 1, sizeof seg, myexefile) < sizeof seg) {
GC_err_puts("Couldn't read obj table entry from ");
GC_err_puts(path); GC_err_puts("\n");
ABORT("Couldn't read obj table entry");
}
flags = O32_FLAGS(seg);
if (!(flags & OBJWRITE)) continue;
if (!(flags & OBJREAD)) continue;
if (flags & OBJINVALID) {
GC_err_printf0("Object with invalid pages?\n");
continue;
}
GC_add_roots_inner(O32_BASE(seg), O32_BASE(seg)+O32_SIZE(seg), FALSE);
}
}
# else
# ifdef MSWIN32
/* Unfortunately, we have to handle win32s very differently from NT, */
/* Since VirtualQuery has very different semantics. In particular, */
/* under win32s a VirtualQuery call on an unmapped page returns an */
/* invalid result. Under GC_register_data_segments is a noop and */
/* all real work is done by GC_register_dynamic_libraries. Under */
/* win32s, we cannot find the data segments associated with dll's. */
/* We rgister the main data segment here. */
GC_bool GC_win32s = FALSE; /* We're running under win32s. */
GC_bool GC_is_win32s()
{
DWORD v = GetVersion();
/* Check that this is not NT, and Windows major version <= 3 */
return ((v & 0x80000000) && (v & 0xff) <= 3);
}
void GC_init_win32()
{
GC_win32s = GC_is_win32s();
}
/* Return the smallest address a such that VirtualQuery */
/* returns correct results for all addresses between a and start. */
/* Assumes VirtualQuery returns correct information for start. */
ptr_t GC_least_described_address(ptr_t start)
{
MEMORY_BASIC_INFORMATION buf;
SYSTEM_INFO sysinfo;
DWORD result;
LPVOID limit;
ptr_t p;
LPVOID q;
GetSystemInfo(&sysinfo);
limit = sysinfo.lpMinimumApplicationAddress;
p = (ptr_t)((word)start & ~(GC_page_size - 1));
for (;;) {
q = (LPVOID)(p - GC_page_size);
if ((ptr_t)q > (ptr_t)p /* underflow */ || q < limit) break;
result = VirtualQuery(q, &buf, sizeof(buf));
if (result != sizeof(buf) || buf.AllocationBase == 0) break;
p = (ptr_t)(buf.AllocationBase);
}
return(p);
}
/* Is p the start of either the malloc heap, or of one of our */
/* heap sections? */
GC_bool GC_is_heap_base (ptr_t p)
{
register unsigned i;
# ifndef REDIRECT_MALLOC
static ptr_t malloc_heap_pointer = 0;
if (0 == malloc_heap_pointer) {
MEMORY_BASIC_INFORMATION buf;
register DWORD result = VirtualQuery(malloc(1), &buf, sizeof(buf));
if (result != sizeof(buf)) {
ABORT("Weird VirtualQuery result");
}
malloc_heap_pointer = (ptr_t)(buf.AllocationBase);
}
if (p == malloc_heap_pointer) return(TRUE);
# endif
for (i = 0; i < GC_n_heap_bases; i++) {
if (GC_heap_bases[i] == p) return(TRUE);
}
return(FALSE);
}
void GC_register_root_section(ptr_t static_root)
{
MEMORY_BASIC_INFORMATION buf;
SYSTEM_INFO sysinfo;
DWORD result;
DWORD protect;
LPVOID p;
char * base;
char * limit, * new_limit;
if (!GC_win32s) return;
p = base = limit = GC_least_described_address(static_root);
GetSystemInfo(&sysinfo);
while (p < sysinfo.lpMaximumApplicationAddress) {
result = VirtualQuery(p, &buf, sizeof(buf));
if (result != sizeof(buf) || buf.AllocationBase == 0
|| GC_is_heap_base(buf.AllocationBase)) break;
new_limit = (char *)p + buf.RegionSize;
protect = buf.Protect;
if (buf.State == MEM_COMMIT
&& is_writable(protect)) {
if ((char *)p == limit) {
limit = new_limit;
} else {
if (base != limit) GC_add_roots_inner(base, limit, FALSE);
base = p;
limit = new_limit;
}
}
if (p > (LPVOID)new_limit /* overflow */) break;
p = (LPVOID)new_limit;
}
if (base != limit) GC_add_roots_inner(base, limit, FALSE);
}
void GC_register_data_segments()
{
static char dummy;
GC_register_root_section((ptr_t)(&dummy));
}
# else
# ifdef AMIGA
void GC_register_data_segments()
{
struct Process *proc;
struct CommandLineInterface *cli;
BPTR myseglist;
ULONG *data;
int num;
# ifdef __GNUC__
ULONG dataSegSize;
GC_bool found_segment = FALSE;
extern char __data_size[];
dataSegSize=__data_size+8;
/* Can`t find the Location of __data_size, because
it`s possible that is it, inside the segment. */
# endif
proc= (struct Process*)SysBase->ThisTask;
/* Reference: Amiga Guru Book Pages: 538ff,565,573
and XOper.asm */
if (proc->pr_Task.tc_Node.ln_Type==NT_PROCESS) {
if (proc->pr_CLI == NULL) {
myseglist = proc->pr_SegList;
} else {
/* ProcLoaded 'Loaded as a command: '*/
cli = BADDR(proc->pr_CLI);
myseglist = cli->cli_Module;
}
} else {
ABORT("Not a Process.");
}
if (myseglist == NULL) {
ABORT("Arrrgh.. can't find segments, aborting");
}
/* xoper hunks Shell Process */
num=0;
for (data = (ULONG *)BADDR(myseglist); data != NULL;
data = (ULONG *)BADDR(data[0])) {
if (((ULONG) GC_register_data_segments < (ULONG) &data[1]) ||
((ULONG) GC_register_data_segments > (ULONG) &data[1] + data[-1])) {
# ifdef __GNUC__
if (dataSegSize == data[-1]) {
found_segment = TRUE;
}
# endif
GC_add_roots_inner((char *)&data[1],
((char *)&data[1]) + data[-1], FALSE);
}
++num;
} /* for */
# ifdef __GNUC__
if (!found_segment) {
ABORT("Can`t find correct Segments.\nSolution: Use an newer version of ixemul.library");
}
# endif
}
#if 0 /* old version */
void GC_register_data_segments()
{
extern struct WBStartup *_WBenchMsg;
struct Process *proc;
struct CommandLineInterface *cli;
BPTR myseglist;
ULONG *data;
if ( _WBenchMsg != 0 ) {
if ((myseglist = _WBenchMsg->sm_Segment) == 0) {
GC_err_puts("No seglist from workbench\n");
return;
}