gc_os_dep.c 56 KB

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