Change immobile space free pointers to alien vars
[sbcl.git] / src / runtime / runtime.h
blobd289c297e594c66f22c7c94b3f9601f5ec4cf7f0
1 /*
2 * This software is part of the SBCL system. See the README file for
3 * more information.
5 * This software is derived from the CMU CL system, which was
6 * written at Carnegie Mellon University and released into the
7 * public domain. The software is in the public domain and is
8 * provided with absolutely no warranty. See the COPYING and CREDITS
9 * files for more information.
12 /* FIXME: Aren't symbols with underscore prefixes supposed to be
13 * reserved for system libraries? Perhaps rename stuff like this
14 * to names like INCLUDED_SBCL_RUNTIME_H. */
15 #ifndef _SBCL_RUNTIME_H_
16 #define _SBCL_RUNTIME_H_
18 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
19 # include "pthreads_win32.h"
20 #else
21 # include <signal.h>
22 # ifdef LISP_FEATURE_SB_THREAD
23 # include <pthread.h>
24 # endif
25 #endif
27 #include <stdint.h>
29 #if defined(LISP_FEATURE_SB_THREAD)
30 #define thread_self() pthread_self()
31 #define thread_kill pthread_kill
33 #ifdef LISP_FEATURE_WIN32
34 #define thread_sigmask _sbcl_pthread_sigmask
35 #else
36 #define thread_sigmask pthread_sigmask
37 #endif
39 #define thread_mutex_lock(l) pthread_mutex_lock(l)
40 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
41 #else
42 #define thread_self() 0
43 #define thread_kill kill_safely
44 #define thread_sigmask sigprocmask
45 #define thread_mutex_lock(l) 0
46 #define thread_mutex_unlock(l) 0
47 #endif
49 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
50 void os_preinit();
51 #endif
53 void os_link_runtime();
55 #if defined(LISP_FEATURE_SB_SAFEPOINT)
57 typedef enum {
58 GC_NONE=0,
59 GC_FLIGHT,
60 GC_MESSAGE,
61 GC_INVOKED,
62 GC_QUIET,
63 GC_SETTLED,
64 GC_COLLECT,
65 GC_NPHASES
66 } gc_phase_t;
68 void map_gc_page();
69 void unmap_gc_page();
70 int check_pending_interrupts();
71 void gc_state_lock();
72 void gc_state_wait(gc_phase_t);
73 void gc_state_unlock();
75 #endif
78 * The next few defines serve as configuration -- edit them inline if
79 * you are a developer and want to affect FSHOW behaviour.
82 /* Block blockable interrupts for each SHOW, if not 0.
83 * (On Windows, this setting has no effect.)
85 * In principle, this is a "configuration option", but I am not aware of
86 * any reason why or when it would be advantageous to disable it. */
87 #define QSHOW_SIGNAL_SAFE 1
89 /* Enable extra-verbose low-level debugging output for signals? (You
90 * probably don't want this unless you're trying to debug very early
91 * cold boot on a new machine, or one where you've just messed up
92 * signal handling.)
94 * Note: It may be that doing this is fundamentally unsound, since it
95 * causes output from signal handlers, and the i/o libraries aren't
96 * necessarily reentrant. But it can still be very convenient for
97 * figuring out what's going on when you have a signal handling
98 * problem.
100 * Possible values are:
101 * 0 -- Never show signal-related output. There is absolutely no
102 * run-time overhead from FSHOW_SIGNAL in this case.
104 * 1 -- (recommended)
105 * Show signal-related output only if selected at run-time
106 * (otherwise almost no run-time overhead).
108 * 2 -- Unconditionally show signal-related output.
109 * Very significant overhead.
111 * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
112 * through :SB-QSHOW.
114 * With option 1, set up environment variable SBCL_DYNDEBUG to include
115 * "fshow" or "fshow_signal" before starting SBCL to enable output.
117 * There is no particular advantage to option 2 except that you do not
118 * need to set environment variables in this case.
120 #ifdef LISP_FEATURE_SB_QSHOW
121 # define QSHOW_SIGNALS 1
122 #else
123 # define QSHOW_SIGNALS 0
124 #endif
126 /* Enable low-level debugging output, if not zero. Defaults to enabled
127 * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
128 * low-level debugging output but not the whole signal mess. */
129 #define QSHOW QSHOW_SIGNALS
132 * Configuration options end here -- the following defines do not
133 * generally need customization.
136 #define odxprint(topic, fmt, ...) \
137 do \
138 if (dyndebug_config.dyndebug_##topic) \
139 odxprint_fun(fmt "\n", ##__VA_ARGS__); \
140 while (0)
142 void odxprint_fun(const char *fmt, ...);
143 void fshow_fun(void *ignored, const char *fmt, ...);
145 /* Flags defined in a structure to avoid code duplication between
146 * declaration and definition. */
147 extern struct dyndebug_config {
148 int dyndebug_fshow;
149 int dyndebug_fshow_signal;
150 int dyndebug_gencgc_verbose;
151 int dyndebug_safepoints;
152 int dyndebug_seh;
153 int dyndebug_misc;
154 int dyndebug_pagefaults;
155 int dyndebug_backtrace_when_lost;
156 int dyndebug_sleep_when_lost;
157 int dyndebug_io;
158 int dyndebug_runtime_link;
159 } dyndebug_config;
161 #ifdef LISP_FEATURE_GENCGC
162 extern int gencgc_verbose;
163 #endif
165 void dyndebug_init(void);
167 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
169 extern sigset_t blockable_sigset;
171 #define QSHOW_BLOCK \
172 sigset_t oldset; \
173 thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
174 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
175 #else
176 #define QSHOW_BLOCK
177 #define QSHOW_UNBLOCK
178 #endif
180 /* The following macros duplicate the expansion of odxprint, because the
181 * extra level of parentheses around `args' prevents us from
182 * implementing FSHOW in terms of odxprint directly. (They also differ
183 * in a newline.)
186 #if QSHOW
187 # define FSHOW(args) \
188 do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
189 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
190 #else
191 # define FSHOW(args)
192 # define SHOW(string)
193 #endif
195 #if QSHOW_SIGNALS
196 # define FSHOW_SIGNAL(args) \
197 do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
198 #else
199 # define FSHOW_SIGNAL(args)
200 #endif
202 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
203 * in practice the "foo int" definitions work for all the machines
204 * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
205 * other non-32-bit machine we'll probably need real machine-dependent
206 * and OS-dependent definitions again. */
207 /* even on alpha, int happens to be 4 bytes. long is longer. */
208 /* FIXME: these names really shouldn't reflect their length and this
209 is not quite right for some of the FFI stuff */
210 #if defined(LISP_FEATURE_WIN32)&&defined(LISP_FEATURE_X86_64)
211 typedef unsigned long long u64;
212 typedef signed long long s64;
213 #else
214 typedef unsigned long u64;
215 typedef signed long s64;
216 #endif
217 typedef unsigned int u32;
218 typedef signed int s32;
220 #ifdef _WIN64
221 #define AMD64_SYSV_ABI __attribute__((sysv_abi))
222 #else
223 #define AMD64_SYSV_ABI
224 #endif
226 #include <sys/types.h>
228 #if defined(LISP_FEATURE_SB_THREAD)
229 typedef pthread_t os_thread_t;
230 #else
231 typedef pid_t os_thread_t;
232 #endif
234 #ifndef LISP_FEATURE_ALPHA
235 typedef uintptr_t uword_t;
236 typedef intptr_t sword_t;
237 #else
238 /* The alpha32 port uses non-intptr-sized words */
239 typedef u32 uword_t;
240 typedef s32 sword_t;
241 #endif
243 /* FIXME: we do things this way because of the alpha32 port. once
244 alpha64 has arrived, all this nastiness can go away */
245 #if 64 == N_WORD_BITS
246 #define LOW_WORD(c) ((uintptr_t)c)
247 #define OBJ_FMTX "lx"
248 typedef uintptr_t lispobj;
249 #else
250 #define OBJ_FMTX "x"
251 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
252 /* fake it on alpha32 */
253 typedef unsigned int lispobj;
254 #endif
256 static inline int
257 lowtag_of(lispobj obj)
259 return obj & LOWTAG_MASK;
262 static inline int
263 widetag_of(lispobj obj)
265 return obj & WIDETAG_MASK;
268 static inline uword_t
269 HeaderValue(lispobj obj)
271 return obj >> N_WIDETAG_BITS;
274 #ifdef LISP_FEATURE_IMMOBILE_SPACE
275 #define HEADER_VALUE_MASKED(x) (HeaderValue(x) & SHORT_HEADER_MAX_WORDS)
276 #else
277 #define HEADER_VALUE_MASKED(x) HeaderValue(x)
278 #endif
279 static inline uword_t instance_length(lispobj header)
281 return HEADER_VALUE_MASKED(header);
283 static inline lispobj instance_layout(lispobj* instance_ptr) // native ptr
285 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
286 return instance_ptr[0] >> 32; // the high half of the header is the layout
287 #else
288 return instance_ptr[1]; // the word following the header is the layout
289 #endif
291 static inline lispobj fin_layout(lispobj* instance_ptr) // native ptr
293 #ifdef LISP_FEATURE_COMPACT_INSTANCE_HEADER
294 return instance_ptr[0] >> 32; // the high half of the header is the layout
295 #else
296 // first 4 words are: header, trampoline, fin-fun, layout
297 return instance_ptr[3];
298 #endif
301 /* Is the Lisp object obj something with pointer nature (as opposed to
302 * e.g. a fixnum or character or unbound marker)? */
303 static inline int
304 is_lisp_pointer(lispobj obj)
306 #if N_WORD_BITS == 64
307 return (obj & 3) == 3;
308 #else
309 return obj & 1;
310 #endif
313 #include "fixnump.h"
315 /* Is the Lisp object obj something with immediate nature (e.g. a
316 * fixnum or character or unbound marker)? */
317 static inline int
318 is_lisp_immediate(lispobj obj)
320 return (fixnump(obj)
321 || (widetag_of(obj) == CHARACTER_WIDETAG)
322 #if N_WORD_BITS == 64
323 || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
324 #endif
325 || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
328 /* Convert from a lispobj with type bits to a native (ordinary
329 * C/assembly) pointer to the beginning of the object. */
330 static inline lispobj *
331 native_pointer(lispobj obj)
333 return (lispobj *) ((uintptr_t) (obj & ~LOWTAG_MASK));
336 /* inverse operation: create a suitably tagged lispobj from a native
337 * pointer or integer.*/
338 static inline lispobj
339 make_lispobj(void *o, int low_tag)
341 return LOW_WORD(o) | low_tag;
344 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
345 static inline lispobj
346 make_fixnum(sword_t n)
348 return MAKE_FIXNUM(n);
351 static inline sword_t
352 fixnum_value(lispobj n)
354 return n >> N_FIXNUM_TAG_BITS;
357 static inline uword_t
358 code_header_words(lispobj header) // given header = code->header
360 return HeaderValue(header) & SHORT_HEADER_MAX_WORDS;
363 static inline sword_t
364 code_instruction_words(lispobj n) // given n = code->code_size
366 /* Convert bytes into words, double-word aligned. */
367 sword_t x = ((n >> N_FIXNUM_TAG_BITS) + LOWTAG_MASK) & ~LOWTAG_MASK;
369 return x >> WORD_SHIFT;
371 #undef HEADER_VALUE_MASKED
373 #if defined(LISP_FEATURE_WIN32)
374 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
375 * shlobj.h.
377 * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
378 * or get rid of our own boolean type. If the boolean type is only used in
379 * the runtime, and never passed to Lisp, then it doesn't matter which one
380 * we use.
382 #define boolean rpcndr_boolean
383 #include <shlobj.h>
384 #undef boolean
385 #endif
386 typedef int boolean;
388 static inline boolean
389 other_immediate_lowtag_p(lispobj header)
391 /* These lowtags are spaced 4 apart throughout the lowtag space. */
392 return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
395 static inline int
396 is_cons_half(lispobj obj)
398 /* A word that satisfies other_immediate_lowtag_p is a headered object
399 * and can not be half of a cons, except that widetags which satisfy
400 * other_immediate and are Lisp immediates can be half of a cons */
401 return !other_immediate_lowtag_p(obj)
402 #if N_WORD_BITS == 64
403 || ((uword_t)IMMEDIATE_WIDETAGS_MASK >> (widetag_of(obj) >> 2)) & 1;
404 #else
405 /* The above bit-shifting approach is not applicable
406 * since we can't employ a 64-bit unsigned integer constant. */
407 || widetag_of(obj) == CHARACTER_WIDETAG
408 || widetag_of(obj) == UNBOUND_MARKER_WIDETAG;
409 #endif
412 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
413 * "this function never returns". This is the way that you do it
414 * in GCC later than version 2.5 or so. */
415 #if defined(__GNUC__)
416 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
417 #define never_returns __attribute__ ((noreturn))
418 #else
419 #define never_returns
420 #endif
421 #else
422 #define never_returns
423 #endif
425 extern void *successful_malloc (size_t size);
426 extern char *copied_string (char *string);
428 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
429 # define THREADS_USING_GCSIGNAL 1
430 #endif
432 /* Now that SPARC has precise GENCGC, several places that used to be
433 * #ifdef PCC need adjustment. Clearly, "PPC or SPARC" is as unhelpful
434 * a test as its reverse, "x86 or x86-64". However, the feature
435 * commonly used to differentiate between those two worlds is
436 * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
437 * opinion), at some point we'd like to have precise GC on x86 while
438 * still sharing the C stack, so stack usage ought not imply GC
439 * conservativeness. So let's have a helper feature that makes the code
440 * a bit more future-proof, even if it is itself currently defined in
441 * the naive way: */
442 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
443 # define GENCGC_IS_PRECISE 1
444 #endif
446 void *os_dlsym_default(char *name);
448 struct lisp_startup_options {
449 boolean noinform;
452 /* Even with just -O1, gcc optimizes the jumps in this "loop" away
453 * entirely, giving the ability to define WITH-FOO-style macros. */
454 #define RUN_BODY_ONCE(prefix, finally_do) \
455 int prefix##done = 0; \
456 for (; !prefix##done; finally_do, prefix##done = 1)
458 // casting to void is no longer enough to suppress a warning about unused
459 // results of libc functions declared with warn_unused_result.
460 // from http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/ignore-value.h
461 #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
462 # define ignore_value(x) \
463 (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
464 #else
465 # define ignore_value(x) ((void) (x))
466 #endif
468 #endif /* _SBCL_RUNTIME_H_ */