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