Dynamic space relocation, part 1 of 2
[sbcl.git] / src / runtime / runtime.h
blob45c856a3448abbfa92de7f567850e83fb1394d2b
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 static inline struct cons *
302 CONS(lispobj obj)
304 return (struct cons *)(obj - LIST_POINTER_LOWTAG);
307 static inline struct vector *
308 VECTOR(lispobj obj)
310 return (struct vector *)(obj - OTHER_POINTER_LOWTAG);
313 static inline struct symbol *
314 SYMBOL(lispobj obj)
316 return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
319 static inline struct fdefn *
320 FDEFN(lispobj obj)
322 return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
325 /* Is the Lisp object obj something with pointer nature (as opposed to
326 * e.g. a fixnum or character or unbound marker)? */
327 static inline int
328 is_lisp_pointer(lispobj obj)
330 #if N_WORD_BITS == 64
331 return (obj & 3) == 3;
332 #else
333 return obj & 1;
334 #endif
337 #include "fixnump.h"
339 /* Is the Lisp object obj something with immediate nature (e.g. a
340 * fixnum or character or unbound marker)? */
341 static inline int
342 is_lisp_immediate(lispobj obj)
344 return (fixnump(obj)
345 || (widetag_of(obj) == CHARACTER_WIDETAG)
346 #if N_WORD_BITS == 64
347 || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
348 #endif
349 || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
352 /* Convert from a lispobj with type bits to a native (ordinary
353 * C/assembly) pointer to the beginning of the object. */
354 static inline lispobj *
355 native_pointer(lispobj obj)
357 return (lispobj *) ((uintptr_t) (obj & ~LOWTAG_MASK));
360 /* inverse operation: create a suitably tagged lispobj from a native
361 * pointer or integer.*/
362 static inline lispobj
363 make_lispobj(void *o, int low_tag)
365 return LOW_WORD(o) | low_tag;
368 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
369 static inline lispobj
370 make_fixnum(sword_t n)
372 return MAKE_FIXNUM(n);
375 static inline sword_t
376 fixnum_value(lispobj n)
378 return n >> N_FIXNUM_TAG_BITS;
381 static inline uword_t
382 code_header_words(lispobj header) // given header = code->header
384 return HeaderValue(header) & SHORT_HEADER_MAX_WORDS;
387 static inline sword_t
388 code_instruction_words(lispobj n) // given n = code->code_size
390 /* Convert bytes into words, double-word aligned. */
391 sword_t x = ((n >> N_FIXNUM_TAG_BITS) + LOWTAG_MASK) & ~LOWTAG_MASK;
393 return x >> WORD_SHIFT;
395 #undef HEADER_VALUE_MASKED
397 #if defined(LISP_FEATURE_WIN32)
398 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
399 * shlobj.h.
401 * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
402 * or get rid of our own boolean type. If the boolean type is only used in
403 * the runtime, and never passed to Lisp, then it doesn't matter which one
404 * we use.
406 #define boolean rpcndr_boolean
407 #include <shlobj.h>
408 #undef boolean
409 #endif
410 typedef int boolean;
412 static inline boolean
413 other_immediate_lowtag_p(lispobj header)
415 /* These lowtags are spaced 4 apart throughout the lowtag space. */
416 return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
419 static inline int
420 is_cons_half(lispobj obj)
422 /* A word that satisfies other_immediate_lowtag_p is a headered object
423 * and can not be half of a cons, except that widetags which satisfy
424 * other_immediate and are Lisp immediates can be half of a cons */
425 return !other_immediate_lowtag_p(obj)
426 #if N_WORD_BITS == 64
427 || ((uword_t)IMMEDIATE_WIDETAGS_MASK >> (widetag_of(obj) >> 2)) & 1;
428 #else
429 /* The above bit-shifting approach is not applicable
430 * since we can't employ a 64-bit unsigned integer constant. */
431 || widetag_of(obj) == CHARACTER_WIDETAG
432 || widetag_of(obj) == UNBOUND_MARKER_WIDETAG;
433 #endif
436 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
437 * "this function never returns". This is the way that you do it
438 * in GCC later than version 2.5 or so. */
439 #if defined(__GNUC__)
440 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
441 #define never_returns __attribute__ ((noreturn))
442 #else
443 #define never_returns
444 #endif
445 #else
446 #define never_returns
447 #endif
449 extern void *successful_malloc (size_t size);
450 extern char *copied_string (char *string);
452 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
453 # define THREADS_USING_GCSIGNAL 1
454 #endif
456 /* Now that SPARC has precise GENCGC, several places that used to be
457 * #ifdef PCC need adjustment. Clearly, "PPC or SPARC" is as unhelpful
458 * a test as its reverse, "x86 or x86-64". However, the feature
459 * commonly used to differentiate between those two worlds is
460 * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
461 * opinion), at some point we'd like to have precise GC on x86 while
462 * still sharing the C stack, so stack usage ought not imply GC
463 * conservativeness. So let's have a helper feature that makes the code
464 * a bit more future-proof, even if it is itself currently defined in
465 * the naive way: */
466 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
467 # define GENCGC_IS_PRECISE 1
468 #endif
470 void *os_dlsym_default(char *name);
472 struct lisp_startup_options {
473 boolean noinform;
476 /* Even with just -O1, gcc optimizes the jumps in this "loop" away
477 * entirely, giving the ability to define WITH-FOO-style macros. */
478 #define RUN_BODY_ONCE(prefix, finally_do) \
479 int prefix##done = 0; \
480 for (; !prefix##done; finally_do, prefix##done = 1)
482 // casting to void is no longer enough to suppress a warning about unused
483 // results of libc functions declared with warn_unused_result.
484 // from http://git.savannah.gnu.org/cgit/gnulib.git/tree/lib/ignore-value.h
485 #if 3 < __GNUC__ + (4 <= __GNUC_MINOR__)
486 # define ignore_value(x) \
487 (__extension__ ({ __typeof__ (x) __x = (x); (void) __x; }))
488 #else
489 # define ignore_value(x) ((void) (x))
490 #endif
492 #endif /* _SBCL_RUNTIME_H_ */