gc_os_dep.c 55 KB

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