runtime/thread.h: Fix non-thread binding and control stack pointer access.
[sbcl.git] / src / runtime / thread.h
1 #if !defined(_INCLUDE_THREAD_H_)
2 #define _INCLUDE_THREAD_H_
3
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <stddef.h>
7 #include "sbcl.h"
8 #include "globals.h"
9 #include "runtime.h"
10 #include "os.h"
11 #ifdef LISP_FEATURE_GENCGC
12 #include "gencgc-alloc-region.h"
13 #endif
14 #ifdef LISP_FEATURE_WIN32
15 #include "win32-thread-private-events.h"
16 #endif
17 #include "genesis/symbol.h"
18 #include "genesis/static-symbols.h"
19
20 #include "genesis/thread.h"
21 #include "genesis/fdefn.h"
22 #include "interrupt.h"
23 #include "validate.h"           /* for BINDING_STACK_SIZE etc */
24
25 #define STATE_RUNNING MAKE_FIXNUM(1)
26 #define STATE_STOPPED MAKE_FIXNUM(2)
27 #define STATE_DEAD MAKE_FIXNUM(3)
28 #if defined(LISP_FEATURE_SB_SAFEPOINT)
29 # define STATE_SUSPENDED_BRIEFLY MAKE_FIXNUM(4)
30 # define STATE_GC_BLOCKER MAKE_FIXNUM(5)
31 # define STATE_PHASE1_BLOCKER MAKE_FIXNUM(5)
32 # define STATE_PHASE2_BLOCKER MAKE_FIXNUM(6)
33 # define STATE_INTERRUPT_BLOCKER MAKE_FIXNUM(7)
34 #endif
35
36 #ifdef LISP_FEATURE_SB_THREAD
37 lispobj thread_state(struct thread *thread);
38 void set_thread_state(struct thread *thread, lispobj state);
39 void wait_for_thread_state_change(struct thread *thread, lispobj state);
40
41 #if defined(LISP_FEATURE_SB_SAFEPOINT)
42 enum threads_suspend_reason {
43     SUSPEND_REASON_NONE,
44     SUSPEND_REASON_GC,
45     SUSPEND_REASON_INTERRUPT,
46     SUSPEND_REASON_GCING
47 };
48
49 struct threads_suspend_info {
50     int suspend;
51     pthread_mutex_t world_lock;
52     pthread_mutex_t lock;
53     enum threads_suspend_reason reason;
54     int phase;
55     struct thread * gc_thread;
56     struct thread * interrupted_thread;
57     int blockers;
58     int used_gc_page;
59 };
60
61 struct suspend_phase {
62     int suspend;
63     enum threads_suspend_reason reason;
64     int phase;
65     struct suspend_phase *next;
66 };
67
68 extern struct threads_suspend_info suspend_info;
69
70 struct gcing_safety {
71     lispobj csp_around_foreign_call;
72     lispobj* pc_around_foreign_call;
73 };
74
75 int handle_safepoint_violation(os_context_t *context, os_vm_address_t addr);
76 void** os_get_csp(struct thread* th);
77 void alloc_gc_page();
78 void assert_on_stack(struct thread *th, void *esp);
79 #endif /* defined(LISP_FEATURE_SB_SAFEPOINT) */
80
81 extern pthread_key_t lisp_thread;
82 #endif
83
84 extern int kill_safely(os_thread_t os_thread, int signal);
85
86 #define THREAD_SLOT_OFFSET_WORDS(c) \
87  (offsetof(struct thread,c)/(sizeof (struct thread *)))
88
89 union per_thread_data {
90     struct thread thread;
91     lispobj dynamic_values[1];  /* actually more like 4000 or so */
92 };
93
94 /* A helper structure for data local to a thread, which is not pointer-sized.
95  *
96  * Originally, all layouting of these fields was done manually in C code
97  * with pointer arithmetic.  We let the C compiler figure it out now.
98  *
99  * (Why is this not part of `struct thread'?  Because that structure is
100  * declared using genesis, and we would run into issues with fields that
101  * are of unknown length.)
102  */
103 struct nonpointer_thread_data
104 {
105 #if defined(LISP_FEATURE_SB_THREAD) && !defined(LISP_FEATURE_SB_SAFEPOINT)
106     os_sem_t state_sem;
107     os_sem_t state_not_running_sem;
108     os_sem_t state_not_stopped_sem;
109 #else
110     /* An unused field follows, to ensure that the struct is non-empty
111      * for non-GCC compilers. */
112     int unused;
113 #endif
114 };
115
116 extern struct thread *all_threads;
117 extern int dynamic_values_bytes;
118
119 #if defined(LISP_FEATURE_DARWIN)
120 #define CONTROL_STACK_ALIGNMENT_BYTES 8192 /* darwin wants page-aligned stacks */
121 #define THREAD_ALIGNMENT_BYTES CONTROL_STACK_ALIGNMENT_BYTES
122 #else
123 #define THREAD_ALIGNMENT_BYTES BACKEND_PAGE_BYTES
124 #define CONTROL_STACK_ALIGNMENT_BYTES 16
125 #endif
126
127
128 #ifdef LISP_FEATURE_SB_THREAD
129 #define for_each_thread(th) for(th=all_threads;th;th=th->next)
130 #else
131 /* there's some possibility a SSC could notice this never actually
132  * loops  */
133 #define for_each_thread(th) for(th=all_threads;th;th=0)
134 #endif
135
136 static inline lispobj *
137 SymbolValueAddress(u64 tagged_symbol_pointer, void *thread)
138 {
139     struct symbol *sym= (struct symbol *)
140         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
141 #ifdef LISP_FEATURE_SB_THREAD
142     if(thread && sym->tls_index) {
143         lispobj *r = &(((union per_thread_data *)thread)
144                        ->dynamic_values[(sym->tls_index) >> WORD_SHIFT]);
145         if((*r)!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
146     }
147 #endif
148     return &sym->value;
149 }
150
151 static inline lispobj
152 SymbolValue(u64 tagged_symbol_pointer, void *thread)
153 {
154     struct symbol *sym= (struct symbol *)
155         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
156 #ifdef LISP_FEATURE_SB_THREAD
157     if(thread && sym->tls_index) {
158         lispobj r=
159             ((union per_thread_data *)thread)
160             ->dynamic_values[(sym->tls_index) >> WORD_SHIFT];
161         if(r!=NO_TLS_VALUE_MARKER_WIDETAG) return r;
162     }
163 #endif
164     return sym->value;
165 }
166
167 static inline lispobj
168 SymbolTlValue(u64 tagged_symbol_pointer, void *thread)
169 {
170     struct symbol *sym= (struct symbol *)
171         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
172 #ifdef LISP_FEATURE_SB_THREAD
173     return ((union per_thread_data *)thread)
174         ->dynamic_values[(sym->tls_index) >> WORD_SHIFT];
175 #else
176     return sym->value;
177 #endif
178 }
179
180 static inline void
181 SetSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
182 {
183     struct symbol *sym= (struct symbol *)
184         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
185 #ifdef LISP_FEATURE_SB_THREAD
186     if(thread && sym->tls_index) {
187         lispobj *pr= &(((union per_thread_data *)thread)
188                        ->dynamic_values[(sym->tls_index) >> WORD_SHIFT]);
189         if(*pr!=NO_TLS_VALUE_MARKER_WIDETAG) {
190             *pr=val;
191             return;
192         }
193     }
194 #endif
195     sym->value = val;
196 }
197
198 static inline void
199 SetTlSymbolValue(u64 tagged_symbol_pointer,lispobj val, void *thread)
200 {
201 #ifdef LISP_FEATURE_SB_THREAD
202     struct symbol *sym= (struct symbol *)
203         (pointer_sized_uint_t)(tagged_symbol_pointer-OTHER_POINTER_LOWTAG);
204     ((union per_thread_data *)thread)
205         ->dynamic_values[(sym->tls_index) >> WORD_SHIFT]
206         =val;
207 #else
208     SetSymbolValue(tagged_symbol_pointer,val,thread) ;
209 #endif
210 }
211
212 /* This only works for static symbols. */
213 static inline lispobj
214 StaticSymbolFunction(lispobj sym)
215 {
216     return ((struct fdefn *)native_pointer(SymbolValue(sym, 0)))->fun;
217 }
218
219 /* These are for use during GC, on the current thread, or on prenatal
220  * threads only. */
221 #if defined(LISP_FEATURE_SB_THREAD)
222 #define get_binding_stack_pointer(thread)       \
223     ((thread)->binding_stack_pointer)
224 #define set_binding_stack_pointer(thread,value) \
225     ((thread)->binding_stack_pointer = (lispobj *)(value))
226 #define access_control_stack_pointer(thread) \
227     ((thread)->control_stack_pointer)
228 #  if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
229 #define access_control_frame_pointer(thread) \
230     ((thread)->control_frame_pointer)
231 #  endif
232 #else
233 #  if defined(BINDING_STACK_POINTER)
234 #define get_binding_stack_pointer(thread)       \
235     SymbolValue(BINDING_STACK_POINTER, thread)
236 #define set_binding_stack_pointer(thread,value) \
237     SetSymbolValue(BINDING_STACK_POINTER, (lispobj)(value), thread)
238 #  else
239 #define get_binding_stack_pointer(thread)       \
240     (current_binding_stack_pointer)
241 #define set_binding_stack_pointer(thread,value) \
242     (current_binding_stack_pointer = (lispobj *)(value))
243 #  endif
244 #define access_control_stack_pointer(thread)    \
245     (current_control_stack_pointer)
246 #  if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
247 #define access_control_frame_pointer(thread) \
248     (current_control_frame_pointer)
249 #  endif
250 #endif
251
252 #if defined(LISP_FEATURE_SB_THREAD) && defined(LISP_FEATURE_GCC_TLS)
253 extern __thread struct thread *current_thread;
254 #endif
255
256 #ifdef LISP_FEATURE_SB_SAFEPOINT
257 # define THREAD_CSP_PAGE_SIZE BACKEND_PAGE_BYTES
258 #else
259 # define THREAD_CSP_PAGE_SIZE 0
260 #endif
261
262 #ifdef LISP_FEATURE_WIN32
263 /*
264  * Win32 doesn't have SIGSTKSZ, and we're not switching stacks anyway,
265  * so define it arbitrarily
266  */
267 #define SIGSTKSZ 1024
268 #endif
269
270 #define THREAD_STRUCT_SIZE (thread_control_stack_size + BINDING_STACK_SIZE + \
271                             ALIEN_STACK_SIZE +                          \
272                             sizeof(struct nonpointer_thread_data) +     \
273                             dynamic_values_bytes +                      \
274                             32 * SIGSTKSZ +                             \
275                             THREAD_ALIGNMENT_BYTES +                    \
276                             THREAD_CSP_PAGE_SIZE)
277
278 #if defined(LISP_FEATURE_WIN32)
279 static inline struct thread* arch_os_get_current_thread()
280     __attribute__((__const__));
281 #endif
282
283 /* This is clearly per-arch and possibly even per-OS code, but we can't
284  * put it somewhere sensible like x86-linux-os.c because it needs too
285  * much stuff like struct thread and all_threads to be defined, which
286  * usually aren't by that time.  So, it's here instead.  Sorry */
287
288 static inline struct thread *arch_os_get_current_thread(void)
289 {
290 #if defined(LISP_FEATURE_SB_THREAD)
291 #if defined(LISP_FEATURE_X86)
292     register struct thread *me=0;
293 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
294     __asm__ ("movl %%fs:0xE10+(4*63), %0" : "=r"(me) :);
295     return me;
296 #endif
297     if(all_threads) {
298 #if defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_RESTORE_FS_SEGMENT_REGISTER_FROM_TLS)
299         sel_t sel;
300         struct thread *th = pthread_getspecific(specials);
301         sel.index = th->tls_cookie;
302         sel.rpl = USER_PRIV;
303         sel.ti = SEL_LDT;
304         __asm__ __volatile__ ("movw %w0, %%fs" : : "r"(sel));
305 #elif defined(LISP_FEATURE_FREEBSD)
306 #ifdef LISP_FEATURE_GCC_TLS
307         struct thread *th = current_thread;
308 #else
309         struct thread *th = pthread_getspecific(specials);
310 #endif
311 #ifdef LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_TLS
312         unsigned int sel = LSEL(th->tls_cookie, SEL_UPL);
313         unsigned int fs = rfs();
314
315         /* Load FS only if it's necessary.  Modifying a selector
316          * causes privilege checking and it takes long time. */
317         if (fs != sel)
318             load_fs(sel);
319 #endif
320         return th;
321 #endif
322         __asm__ ("movl %%fs:%c1,%0" : "=r" (me)
323                  : "i" (offsetof (struct thread,this)));
324     }
325     return me;
326 #else
327 #ifdef LISP_FEATURE_GCC_TLS
328     return current_thread;
329 #else
330     return pthread_getspecific(specials);
331 #endif
332 #endif /* x86 */
333 #else
334      return all_threads;
335 #endif
336 }
337
338 #if defined(LISP_FEATURE_MACH_EXCEPTION_HANDLER)
339 extern kern_return_t mach_lisp_thread_init(struct thread *thread);
340 extern kern_return_t mach_lisp_thread_destroy(struct thread *thread);
341 #endif
342
343 #ifdef LISP_FEATURE_SB_SAFEPOINT
344 void thread_in_safety_transition(os_context_t *ctx);
345 void thread_in_lisp_raised(os_context_t *ctx);
346 void thread_interrupted(os_context_t *ctx);
347 void thread_pitstop(os_context_t *ctxptr);
348 extern void thread_register_gc_trigger();
349
350 # ifdef LISP_FEATURE_SB_THRUPTION
351 int wake_thread(os_thread_t os_thread);
352 #  ifdef LISP_FEATURE_WIN32
353 void wake_thread_win32(struct thread *thread);
354 #  else
355 int wake_thread_posix(os_thread_t os_thread);
356 #  endif
357 # endif
358
359 static inline
360 void push_gcing_safety(struct gcing_safety *into)
361 {
362     struct thread* th = arch_os_get_current_thread();
363     asm volatile ("");
364     if ((into->csp_around_foreign_call =
365          *th->csp_around_foreign_call)) {
366         *th->csp_around_foreign_call = 0;
367         asm volatile ("");
368         into->pc_around_foreign_call = th->pc_around_foreign_call;
369         th->pc_around_foreign_call = 0;
370         asm volatile ("");
371     } else {
372         into->pc_around_foreign_call = 0;
373     }
374 }
375
376 static inline
377 void pop_gcing_safety(struct gcing_safety *from)
378 {
379     struct thread* th = arch_os_get_current_thread();
380     if (from->csp_around_foreign_call) {
381         asm volatile ("");
382         *th->csp_around_foreign_call = from->csp_around_foreign_call;
383         asm volatile ("");
384         th->pc_around_foreign_call = from->pc_around_foreign_call;
385         asm volatile ("");
386     }
387 }
388
389 /* Even with just -O1, gcc optimizes the jumps in this "loop" away
390  * entirely, giving the ability to define WITH-FOO-style macros. */
391 #define RUN_BODY_ONCE(prefix, finally_do)               \
392     int prefix##done = 0;                               \
393     for (; !prefix##done; finally_do, prefix##done = 1)
394
395 #define WITH_GC_AT_SAFEPOINTS_ONLY_hygenic(var)        \
396     struct gcing_safety var;                    \
397     push_gcing_safety(&var);                    \
398     RUN_BODY_ONCE(var, pop_gcing_safety(&var))
399
400 #define WITH_GC_AT_SAFEPOINTS_ONLY()                           \
401     WITH_GC_AT_SAFEPOINTS_ONLY_hygenic(sbcl__gc_safety)
402
403 #define WITH_STATE_SEM_hygenic(var, thread)                             \
404     os_sem_wait((thread)->state_sem, "thread_state");                   \
405     RUN_BODY_ONCE(var, os_sem_post((thread)->state_sem, "thread_state"))
406
407 #define WITH_STATE_SEM(thread)                                     \
408     WITH_STATE_SEM_hygenic(sbcl__state_sem, thread)
409
410 int check_pending_thruptions(os_context_t *ctx);
411
412 #endif
413
414 extern void create_initial_thread(lispobj);
415
416 #ifdef LISP_FEATURE_SB_THREAD
417 extern pthread_mutex_t all_threads_lock;
418 #endif
419
420 #endif /* _INCLUDE_THREAD_H_ */