a8956a93980f2d04a0c5c9c271f54fee3b667843
[sbcl.git] / src / runtime / runtime.h
1 /*
2  * This software is part of the SBCL system. See the README file for
3  * more information.
4  *
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.
10  */
11
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_
17
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
26
27 #include <stdint.h>
28
29 #if defined(LISP_FEATURE_SB_THREAD)
30 #define thread_self() pthread_self()
31 #define thread_kill pthread_kill
32 #define thread_sigmask pthread_sigmask
33 #define thread_mutex_lock(l) pthread_mutex_lock(l)
34 #define thread_mutex_unlock(l) pthread_mutex_unlock(l)
35 #else
36 #define thread_self() 0
37 #define thread_kill kill_safely
38 #define thread_sigmask sigprocmask
39 #define thread_mutex_lock(l) 0
40 #define thread_mutex_unlock(l) 0
41 #endif
42
43 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
44 void os_preinit();
45 #endif
46
47 #if defined(LISP_FEATURE_SB_SAFEPOINT)
48
49 typedef enum {
50     GC_NONE=0,
51     GC_FLIGHT,
52     GC_MESSAGE,
53     GC_INVOKED,
54     GC_QUIET,
55     GC_SETTLED,
56     GC_COLLECT,
57     GC_NPHASES
58 }  gc_phase_t;
59
60 void map_gc_page();
61 void unmap_gc_page();
62 int check_pending_interrupts();
63 void gc_state_lock();
64 void gc_state_wait(gc_phase_t);
65 void gc_state_unlock();
66
67 #endif
68
69 /*
70  * The next few defines serve as configuration -- edit them inline if
71  * you are a developer and want to affect FSHOW behaviour.
72  */
73
74 /* Block blockable interrupts for each SHOW, if not 0.
75  * (On Windows, this setting has no effect.)
76  *
77  * In principle, this is a "configuration option", but I am not aware of
78  * any reason why or when it would be advantageous to disable it. */
79 #define QSHOW_SIGNAL_SAFE 1
80
81 /* Enable extra-verbose low-level debugging output for signals? (You
82  * probably don't want this unless you're trying to debug very early
83  * cold boot on a new machine, or one where you've just messed up
84  * signal handling.)
85  *
86  * Note: It may be that doing this is fundamentally unsound, since it
87  * causes output from signal handlers, and the i/o libraries aren't
88  * necessarily reentrant. But it can still be very convenient for
89  * figuring out what's going on when you have a signal handling
90  * problem.
91  *
92  * Possible values are:
93  *   0 -- Never show signal-related output.  There is absolutely no
94  *        run-time overhead from FSHOW_SIGNAL in this case.
95  *
96  *   1 -- (recommended)
97  *        Show signal-related output only if selected at run-time
98  *        (otherwise almost no run-time overhead).
99  *
100  *   2 -- Unconditionally show signal-related output.
101  *        Very significant overhead.
102  *
103  * For reasons of tradition, we default to 0 on POSIX and 1 on Windows
104  * through :SB-QSHOW.
105  *
106  * With option 1, set up environment variable SBCL_DYNDEBUG to include
107  * "fshow" or "fshow_signal" before starting SBCL to enable output.
108  *
109  * There is no particular advantage to option 2 except that you do not
110  * need to set environment variables in this case.
111  */
112 #ifdef LISP_FEATURE_SB_QSHOW
113 # define QSHOW_SIGNALS 1
114 #else
115 # define QSHOW_SIGNALS 0
116 #endif
117
118 /* Enable low-level debugging output, if not zero. Defaults to enabled
119  * if QSHOW_SIGNALS, disabled otherwise. Change it to 1 or 2 if you want
120  * low-level debugging output but not the whole signal mess. */
121 #define QSHOW QSHOW_SIGNALS
122
123 /*
124  * Configuration options end here -- the following defines do not
125  * generally need customization.
126  */
127
128 #define odxprint(topic, fmt, ...)                       \
129     do                                                  \
130         if (dyndebug_config.dyndebug_##topic)           \
131             odxprint_fun(fmt "\n", ##__VA_ARGS__);      \
132     while (0)
133
134 void odxprint_fun(const char *fmt, ...);
135 void fshow_fun(void *ignored, const char *fmt, ...);
136
137 /* Flags defined in a structure to avoid code duplication between
138  * declaration and definition. */
139 extern struct dyndebug_config {
140     int dyndebug_fshow;
141     int dyndebug_fshow_signal;
142     int dyndebug_gencgc_verbose;
143     int dyndebug_safepoints;
144     int dyndebug_seh;
145     int dyndebug_misc;
146     int dyndebug_pagefaults;
147     int dyndebug_backtrace_when_lost;
148     int dyndebug_sleep_when_lost;
149     int dyndebug_io;
150     int dyndebug_runtime_link;
151 } dyndebug_config;
152
153 #ifdef LISP_FEATURE_GENCGC
154 extern int gencgc_verbose;
155 #endif
156
157 void dyndebug_init(void);
158
159 #if QSHOW_SIGNAL_SAFE == 1 && !defined(LISP_FEATURE_WIN32)
160
161 extern sigset_t blockable_sigset;
162
163 #define QSHOW_BLOCK                                             \
164         sigset_t oldset;                                        \
165         thread_sigmask(SIG_BLOCK, &blockable_sigset, &oldset)
166 #define QSHOW_UNBLOCK thread_sigmask(SIG_SETMASK,&oldset,0)
167 #else
168 #define QSHOW_BLOCK
169 #define QSHOW_UNBLOCK
170 #endif
171
172 /* The following macros duplicate the expansion of odxprint, because the
173  * extra level of parentheses around `args' prevents us from
174  * implementing FSHOW in terms of odxprint directly.  (They also differ
175  * in a newline.)
176  */
177
178 #if QSHOW
179 # define FSHOW(args) \
180     do if (dyndebug_config.dyndebug_fshow) fshow_fun args; while (0)
181 # define SHOW(string) FSHOW((stderr, "/%s\n", string))
182 #else
183 # define FSHOW(args)
184 # define SHOW(string)
185 #endif
186
187 #if QSHOW_SIGNALS
188 # define FSHOW_SIGNAL(args)                                             \
189     do if (dyndebug_config.dyndebug_fshow_signal) fshow_fun args; while (0)
190 #else
191 # define FSHOW_SIGNAL(args)
192 #endif
193
194 /* KLUDGE: These are in theory machine-dependent and OS-dependent, but
195  * in practice the "foo int" definitions work for all the machines
196  * that SBCL runs on as of 0.6.7. If we port to the Alpha or some
197  * other non-32-bit machine we'll probably need real machine-dependent
198  * and OS-dependent definitions again. */
199 /* even on alpha, int happens to be 4 bytes.  long is longer. */
200 /* FIXME: these names really shouldn't reflect their length and this
201    is not quite right for some of the FFI stuff */
202 typedef unsigned long u64;
203 typedef signed long s64;
204 typedef unsigned int u32;
205 typedef signed int s32;
206
207 /* this is an integral type the same length as a machine pointer */
208 typedef uintptr_t pointer_sized_uint_t;
209
210 #include <sys/types.h>
211
212 #if defined(LISP_FEATURE_SB_THREAD)
213 typedef pthread_t os_thread_t;
214 #else
215 typedef pid_t os_thread_t;
216 #endif
217
218 typedef uintptr_t uword_t;
219 typedef intptr_t  sword_t;
220
221 /* FIXME: we do things this way because of the alpha32 port.  once
222    alpha64 has arrived, all this nastiness can go away */
223 #if 64 == N_WORD_BITS
224 #define LOW_WORD(c) ((pointer_sized_uint_t)c)
225 #define OBJ_FMTX "lx"
226 typedef uintptr_t lispobj;
227 #else
228 #define OBJ_FMTX "x"
229 #define LOW_WORD(c) ((long)(c) & 0xFFFFFFFFL)
230 /* fake it on alpha32 */
231 typedef unsigned int lispobj;
232 #endif
233
234 static inline int
235 lowtag_of(lispobj obj)
236 {
237     return obj & LOWTAG_MASK;
238 }
239
240 static inline int
241 widetag_of(lispobj obj)
242 {
243     return obj & WIDETAG_MASK;
244 }
245
246 static inline uword_t
247 HeaderValue(lispobj obj)
248 {
249   return obj >> N_WIDETAG_BITS;
250 }
251
252 static inline struct cons *
253 CONS(lispobj obj)
254 {
255   return (struct cons *)(obj - LIST_POINTER_LOWTAG);
256 }
257
258 static inline struct symbol *
259 SYMBOL(lispobj obj)
260 {
261   return (struct symbol *)(obj - OTHER_POINTER_LOWTAG);
262 }
263
264 static inline struct fdefn *
265 FDEFN(lispobj obj)
266 {
267   return (struct fdefn *)(obj - OTHER_POINTER_LOWTAG);
268 }
269
270 /* Is the Lisp object obj something with pointer nature (as opposed to
271  * e.g. a fixnum or character or unbound marker)? */
272 static inline int
273 is_lisp_pointer(lispobj obj)
274 {
275 #if N_WORD_BITS == 64
276     return (obj & 3) == 3;
277 #else
278     return obj & 1;
279 #endif
280 }
281
282 #include "fixnump.h"
283
284 /* Is the Lisp object obj something with immediate nature (e.g. a
285  * fixnum or character or unbound marker)? */
286 static inline int
287 is_lisp_immediate(lispobj obj)
288 {
289     return (fixnump(obj)
290             || (widetag_of(obj) == CHARACTER_WIDETAG)
291 #if N_WORD_BITS == 64
292             || (widetag_of(obj) == SINGLE_FLOAT_WIDETAG)
293 #endif
294             || (widetag_of(obj) == UNBOUND_MARKER_WIDETAG));
295 }
296
297 /* Convert from a lispobj with type bits to a native (ordinary
298  * C/assembly) pointer to the beginning of the object. */
299 static inline lispobj *
300 native_pointer(lispobj obj)
301 {
302     return (lispobj *) ((pointer_sized_uint_t) (obj & ~LOWTAG_MASK));
303 }
304
305 /* inverse operation: create a suitably tagged lispobj from a native
306  * pointer or integer.*/
307 static inline lispobj
308 make_lispobj(void *o, int low_tag)
309 {
310     return LOW_WORD(o) | low_tag;
311 }
312
313 #define MAKE_FIXNUM(n) (n << N_FIXNUM_TAG_BITS)
314 static inline lispobj
315 make_fixnum(sword_t n)
316 {
317     return MAKE_FIXNUM(n);
318 }
319
320 static inline sword_t
321 fixnum_value(lispobj n)
322 {
323     return n >> N_FIXNUM_TAG_BITS;
324 }
325
326 #if defined(LISP_FEATURE_WIN32)
327 /* KLUDGE: Avoid double definition of boolean by rpcndr.h included via
328  * shlobj.h.
329  *
330  * FIXME: We should probably arrange to use the rpcndr.h boolean on Windows,
331  * or get rid of our own boolean type.  If the boolean type is only used in
332  * the runtime, and never passed to Lisp, then it doesn't matter which one
333  * we use.
334  */
335 #define boolean rpcndr_boolean
336 #include <shlobj.h>
337 #undef boolean
338 #endif
339 typedef int boolean;
340
341 static inline boolean
342 other_immediate_lowtag_p(lispobj header)
343 {
344     /* These lowtags are spaced 4 apart throughout the lowtag space. */
345     return (lowtag_of(header) & 3) == OTHER_IMMEDIATE_0_LOWTAG;
346 }
347
348 /* KLUDGE: As far as I can tell there's no ANSI C way of saying
349  * "this function never returns". This is the way that you do it
350  * in GCC later than version 2.5 or so. */
351 #if defined(__GNUC__)
352 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
353 #define never_returns __attribute__ ((noreturn))
354 #else
355 #define never_returns
356 #endif
357 #else
358 #define never_returns
359 #endif
360
361 extern void *successful_malloc (size_t size);
362 extern char *copied_string (char *string);
363
364 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
365 # define THREADS_USING_GCSIGNAL 1
366 #endif
367
368 /* Now that SPARC has precise GENCGC, several places that used to be
369  * #ifdef PCC need adjustment.  Clearly, "PPC or SPARC" is as unhelpful
370  * a test as its reverse, "x86 or x86-64".  However, the feature
371  * commonly used to differentiate between those two worlds is
372  * C_STACK_IS_CONTROL_STACK, and clearly (or at least in my humble
373  * opinion), at some point we'd like to have precise GC on x86 while
374  * still sharing the C stack, so stack usage ought not imply GC
375  * conservativeness.  So let's have a helper feature that makes the code
376  * a bit more future-proof, even if it is itself currently defined in
377  * the naive way: */
378 #if defined(LISP_FEATURE_GENCGC) && !defined(LISP_FEATURE_C_STACK_IS_CONTROL_STACK)
379 # define GENCGC_IS_PRECISE 1
380 #endif
381
382 void *os_dlsym_default(char *name);
383
384 #endif /* _SBCL_RUNTIME_H_ */