In x86 arch_os_get_current_thread(), do not load from %fs
[sbcl.git] / src / runtime / x86-darwin-os.c
1 #ifdef LISP_FEATURE_SB_THREAD
2 #include <architecture/i386/table.h>
3 #include <i386/user_ldt.h>
4 #include <mach/mach_init.h>
5 #endif
6
7 #include "thread.h"
8 #include "validate.h"
9 #include "runtime.h"
10 #include "interrupt.h"
11 #include "x86-darwin-os.h"
12 #include "genesis/fdefn.h"
13
14 #include <mach/mach.h>
15 #include <mach/mach_error.h>
16 #include <mach/mach_types.h>
17 #include <mach/sync_policy.h>
18 #include <mach/vm_region.h>
19 #include <mach/machine/thread_state.h>
20 #include <mach/machine/thread_status.h>
21 #include <sys/_types.h>
22 #include <sys/ucontext.h>
23 #include <pthread.h>
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #ifdef LISP_FEATURE_SB_THREAD
29
30 pthread_mutex_t modify_ldt_lock = PTHREAD_MUTEX_INITIALIZER;
31
32 void set_data_desc_size(data_desc_t* desc, unsigned long size)
33 {
34     desc->limit00 = (size - 1) & 0xffff;
35     desc->limit16 = ((size - 1) >> 16) &0xf;
36 }
37
38 void set_data_desc_addr(data_desc_t* desc, void* addr)
39 {
40     desc->base00 = (unsigned int)addr & 0xffff;
41     desc->base16 = ((unsigned int)addr & 0xff0000) >> 16;
42     desc->base24 = ((unsigned int)addr & 0xff000000) >> 24;
43 }
44
45 #endif
46
47 #ifdef LISP_FEATURE_SB_THREAD
48 void
49 arch_os_load_ldt(struct thread *thread)
50 {
51     sel_t sel;
52
53     sel.index = thread->tls_cookie;
54     sel.rpl = USER_PRIV;
55     sel.ti = SEL_LDT;
56
57     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
58 }
59 #endif
60
61 int arch_os_thread_init(struct thread *thread) {
62 #ifdef LISP_FEATURE_SB_THREAD
63     int n;
64
65     data_desc_t ldt_entry = { 0, 0, 0, DESC_DATA_WRITE,
66                               3, 1, 0, DESC_DATA_32B, DESC_GRAN_BYTE, 0 };
67
68     set_data_desc_addr(&ldt_entry, thread);
69     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
70
71     thread_mutex_lock(&modify_ldt_lock);
72     n = i386_set_ldt(LDT_AUTO_ALLOC, (union ldt_entry*) &ldt_entry, 1);
73
74     if (n < 0) {
75         perror("i386_set_ldt");
76         lose("unexpected i386_set_ldt(..) failure\n");
77     }
78     thread_mutex_unlock(&modify_ldt_lock);
79
80     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
81     thread->tls_cookie=n;
82     arch_os_load_ldt(thread);
83
84     pthread_setspecific(specials,thread);
85 #endif
86 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
87     mach_lisp_thread_init(thread);
88 #endif
89
90 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
91     stack_t sigstack;
92
93     /* Signal handlers are run on the control stack, so if it is exhausted
94      * we had better use an alternate stack for whatever signal tells us
95      * we've exhausted it */
96     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
97     sigstack.ss_flags=0;
98     sigstack.ss_size = 32*SIGSTKSZ;
99     sigaltstack(&sigstack,0);
100 #endif
101     return 1;                  /* success */
102 }
103
104 int arch_os_thread_cleanup(struct thread *thread) {
105 #if defined(LISP_FEATURE_SB_THREAD)
106     int n = thread->tls_cookie;
107
108     /* Set the %%fs register back to 0 and free the ldt by setting it
109      * to NULL.
110      */
111     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
112
113     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
114     thread_mutex_lock(&modify_ldt_lock);
115     i386_set_ldt(n, NULL, 1);
116     thread_mutex_unlock(&modify_ldt_lock);
117 #endif
118     return 1;                  /* success */
119 }
120
121 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
122
123 void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context);
124 void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context);
125 void memory_fault_handler(int signal, siginfo_t *siginfo,
126                           os_context_t *context);
127
128 /* This executes in the faulting thread as part of the signal
129  * emulation.  It is passed a context with the uc_mcontext field
130  * pointing to a valid block of memory. */
131 void build_fake_signal_context(os_context_t *context,
132                                x86_thread_state32_t *thread_state,
133                                x86_float_state32_t *float_state) {
134     pthread_sigmask(0, NULL, &context->uc_sigmask);
135     context->uc_mcontext->SS = *thread_state;
136     context->uc_mcontext->FS = *float_state;
137 }
138
139 /* This executes in the faulting thread as part of the signal
140  * emulation.  It is effectively the inverse operation from above. */
141 void update_thread_state_from_context(x86_thread_state32_t *thread_state,
142                                       x86_float_state32_t *float_state,
143                                       os_context_t *context) {
144     *thread_state = context->uc_mcontext->SS;
145     *float_state = context->uc_mcontext->FS;
146     pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
147 }
148
149 /* Modify a context to push new data on its stack. */
150 void push_context(u32 data, x86_thread_state32_t *thread_state)
151 {
152     u32 *stack_pointer;
153
154     stack_pointer = (u32*) thread_state->ESP;
155     *(--stack_pointer) = data;
156     thread_state->ESP = (unsigned int) stack_pointer;
157 }
158
159 void align_context_stack(x86_thread_state32_t *thread_state)
160 {
161     /* 16byte align the stack (provided that the stack is, as it
162      * should be, 4byte aligned. */
163     while (thread_state->ESP & 15) push_context(0, thread_state);
164 }
165
166 /* Stack allocation starts with a context that has a mod-4 ESP value
167  * and needs to leave a context with a mod-16 ESP that will restore
168  * the old ESP value and other register state when activated.  The
169  * first part of this is the recovery trampoline, which loads ESP from
170  * EBP, pops EBP, and returns. */
171 asm("_stack_allocation_recover: movl %ebp, %esp; popl %ebp; ret;");
172
173 void open_stack_allocation(x86_thread_state32_t *thread_state)
174 {
175     void stack_allocation_recover(void);
176
177     push_context(thread_state->EIP, thread_state);
178     push_context(thread_state->EBP, thread_state);
179     thread_state->EBP = thread_state->ESP;
180     thread_state->EIP = (unsigned int) stack_allocation_recover;
181
182     align_context_stack(thread_state);
183 }
184
185 /* Stack allocation of data starts with a context with a mod-16 ESP
186  * value and reserves some space on it by manipulating the ESP
187  * register. */
188 void *stack_allocate(x86_thread_state32_t *thread_state, size_t size)
189 {
190     /* round up size to 16byte multiple */
191     size = (size + 15) & -16;
192
193     thread_state->ESP = ((u32)thread_state->ESP) - size;
194
195     return (void *)thread_state->ESP;
196 }
197
198 /* Arranging to invoke a C function is tricky, as we have to assume
199  * cdecl calling conventions (caller removes args) and x86/darwin
200  * alignment requirements.  The simplest way to arrange this,
201  * actually, is to open a new stack allocation.
202  * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
203 void call_c_function_in_context(x86_thread_state32_t *thread_state,
204                                 void *function,
205                                 int nargs,
206                                 ...)
207 {
208     va_list ap;
209     int i;
210     u32 *stack_pointer;
211
212     /* Set up to restore stack on exit. */
213     open_stack_allocation(thread_state);
214
215     /* Have to keep stack 16byte aligned on x86/darwin. */
216     for (i = (3 & -nargs); i; i--) {
217         push_context(0, thread_state);
218     }
219
220     thread_state->ESP = ((u32)thread_state->ESP) - nargs * 4;
221     stack_pointer = (u32 *)thread_state->ESP;
222
223     va_start(ap, nargs);
224     for (i = 0; i < nargs; i++) {
225         //push_context(va_arg(ap, u32), thread_state);
226         stack_pointer[i] = va_arg(ap, u32);
227     }
228     va_end(ap);
229
230     push_context(thread_state->EIP, thread_state);
231     thread_state->EIP = (unsigned int) function;
232 }
233
234 void signal_emulation_wrapper(x86_thread_state32_t *thread_state,
235                               x86_float_state32_t *float_state,
236                               int signal,
237                               siginfo_t *siginfo,
238                               void (*handler)(int, siginfo_t *, void *))
239 {
240
241     /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
242      * context and regs on the stack as local variables, but this
243      * causes problems for the lisp debugger. When it walks the stack
244      * for a back trace, it sees the 1) address of the local variable
245      * on the stack and thinks that is a frame pointer to a lisp
246      * frame, and, 2) the address of the sap that we alloc'ed in
247      * dynamic space and thinks that is a return address, so it,
248      * heuristicly (and wrongly), chooses that this should be
249      * interpreted as a lisp frame instead of as a C frame.
250      * We can work around this in this case by os_validating the
251      * context (and regs just for symmetry).
252      */
253
254     os_context_t *context;
255     mcontext_t *regs;
256
257     context = (os_context_t*) os_validate(0, sizeof(os_context_t));
258     regs = (mcontext_t*) os_validate(0, sizeof(mcontext_t));
259     context->uc_mcontext = regs;
260
261     /* when BSD signals are fired, they mask they signals in sa_mask
262        which always seem to be the blockable_sigset, for us, so we
263        need to:
264        1) save the current sigmask
265        2) block blockable signals
266        3) call the signal handler
267        4) restore the sigmask */
268
269     build_fake_signal_context(context, thread_state, float_state);
270
271     block_blockable_signals(0, 0);
272
273     handler(signal, siginfo, context);
274
275     update_thread_state_from_context(thread_state, float_state, context);
276
277     os_invalidate((os_vm_address_t)context, sizeof(os_context_t));
278     os_invalidate((os_vm_address_t)regs, sizeof(mcontext_t));
279
280     /* Trap to restore the signal context. */
281     asm volatile (".long 0xffff0b0f"
282                   : : "a" (thread_state), "c" (float_state));
283 }
284
285 /* Convenience wrapper for the above */
286 void call_handler_on_thread(mach_port_t thread,
287                             x86_thread_state32_t *thread_state,
288                             int signal,
289                             siginfo_t *siginfo,
290                             void (*handler)(int, siginfo_t *, void *))
291 {
292     x86_thread_state32_t new_state;
293     x86_thread_state32_t *save_thread_state;
294     x86_float_state32_t *save_float_state;
295     mach_msg_type_number_t state_count;
296     siginfo_t *save_siginfo;
297     kern_return_t ret;
298     /* Initialize the new state */
299     new_state = *thread_state;
300     open_stack_allocation(&new_state);
301     stack_allocate(&new_state, 256);
302     /* Save old state */
303     save_thread_state = (x86_thread_state32_t *)stack_allocate(&new_state, sizeof(*save_thread_state));
304     *save_thread_state = *thread_state;
305     /* Save float state */
306     save_float_state = (x86_float_state32_t *)stack_allocate(&new_state, sizeof(*save_float_state));
307     state_count = x86_FLOAT_STATE32_COUNT;
308     if ((ret = thread_get_state(thread,
309                                 x86_FLOAT_STATE32,
310                                 (thread_state_t)save_float_state,
311                                 &state_count)) != KERN_SUCCESS)
312         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
313     /* Set up siginfo */
314     save_siginfo = stack_allocate(&new_state, sizeof(*siginfo));
315     if (siginfo == NULL)
316         save_siginfo = siginfo;
317     else
318         *save_siginfo = *siginfo;
319     /* Prepare to call */
320     call_c_function_in_context(&new_state,
321                                signal_emulation_wrapper,
322                                5,
323                                save_thread_state,
324                                save_float_state,
325                                signal,
326                                save_siginfo,
327                                handler);
328     /* Update the thread state */
329     state_count = x86_THREAD_STATE32_COUNT;
330     if ((ret = thread_set_state(thread,
331                                 x86_THREAD_STATE32,
332                                 (thread_state_t)&new_state,
333                                 state_count)) != KERN_SUCCESS)
334         lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
335
336 }
337
338 #if defined DUMP_CONTEXT
339 void dump_context(x86_thread_state32_t *thread_state)
340 {
341     int i;
342     u32 *stack_pointer;
343
344     printf("eax: %08lx  ecx: %08lx  edx: %08lx  ebx: %08lx\n",
345            thread_state->EAX, thread_state->ECX, thread_state->EDX, thread_state->EAX);
346     printf("esp: %08lx  ebp: %08lx  esi: %08lx  edi: %08lx\n",
347            thread_state->ESP, thread_state->EBP, thread_state->ESI, thread_state->EDI);
348     printf("eip: %08lx  eflags: %08lx\n",
349            thread_state->EIP, thread_state->EFLAGS);
350     printf("cs: %04hx  ds: %04hx  es: %04hx  "
351            "ss: %04hx  fs: %04hx  gs: %04hx\n",
352            thread_state->CS,
353            thread_state->DS,
354            thread_state->ES,
355            thread_state->SS,
356            thread_state->FS,
357            thread_state->GS);
358
359     stack_pointer = (u32 *)thread_state->ESP;
360     for (i = 0; i < 48; i+=4) {
361         printf("%08x:  %08x %08x %08x %08x\n",
362                thread_state->ESP + (i * 4),
363                stack_pointer[i],
364                stack_pointer[i+1],
365                stack_pointer[i+2],
366                stack_pointer[i+3]);
367     }
368 }
369 #endif
370
371 void
372 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
373                                 os_context_t *context) {
374
375     unblock_signals_in_context_and_maybe_warn(context);
376     arrange_return_to_lisp_function
377         (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
378 }
379
380 void
381 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
382     arrange_return_to_lisp_function
383         (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
384 }
385
386 kern_return_t
387 catch_exception_raise(mach_port_t exception_port,
388                       mach_port_t thread,
389                       mach_port_t task,
390                       exception_type_t exception,
391                       exception_data_t code_vector,
392                       mach_msg_type_number_t code_count)
393 {
394     x86_thread_state32_t thread_state;
395     mach_msg_type_number_t state_count;
396     vm_address_t region_addr;
397     vm_size_t region_size;
398     vm_region_basic_info_data_t region_info;
399     mach_msg_type_number_t info_count;
400     mach_port_t region_name;
401     void *addr = NULL;
402     int signal = 0;
403     void (*handler)(int, siginfo_t *, void *) = NULL;
404     siginfo_t siginfo;
405     kern_return_t ret, dealloc_ret;
406
407     struct thread *th;
408
409     FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
410     th = *(struct thread**)exception_port;
411     /* Get state and info */
412     state_count = x86_THREAD_STATE32_COUNT;
413     if ((ret = thread_get_state(thread,
414                                 x86_THREAD_STATE32,
415                                 (thread_state_t)&thread_state,
416                                 &state_count)) != KERN_SUCCESS)
417         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
418     switch (exception) {
419     case EXC_BAD_ACCESS:
420         signal = SIGBUS;
421         /* Check if write protection fault */
422         if ((code_vector[0] & OS_VM_PROT_ALL) == 0) {
423             ret = KERN_INVALID_RIGHT;
424             break;
425         }
426         addr = (void*)code_vector[1];
427         /* Undefined alien */
428         if (os_trunc_to_page(addr) == undefined_alien_address) {
429             handler = undefined_alien_handler;
430             break;
431         }
432         /* At stack guard */
433         if (os_trunc_to_page(addr) == CONTROL_STACK_GUARD_PAGE(th)) {
434             lower_thread_control_stack_guard_page(th);
435             handler = control_stack_exhausted_handler;
436             break;
437         }
438         /* Return from stack guard */
439         if (os_trunc_to_page(addr) == CONTROL_STACK_RETURN_GUARD_PAGE(th)) {
440             reset_thread_control_stack_guard_page(th);
441             break;
442         }
443         /* Regular memory fault */
444         handler = memory_fault_handler;
445         break;
446     case EXC_BAD_INSTRUCTION:
447         signal = SIGTRAP;
448         /* Check if illegal instruction trap */
449         if (code_vector[0] != EXC_I386_INVOP) {
450             ret = KERN_INVALID_RIGHT;
451             break;
452         }
453         /* Check if UD2 instruction */
454         if (*(unsigned short *)thread_state.EIP != 0x0b0f) {
455             /* KLUDGE: There are two ways we could get here:
456              * 1) We're executing data and we've hit some truly
457              *    illegal opcode, of which there are a few, see
458              *    Intel 64 and IA-32 Architectures
459              *    Sofware Developer's Manual
460              *    Volume 3A page 5-34)
461              * 2) The kernel started an unrelated signal handler
462              *    before we got a chance to run. The context that
463              *    caused the exception is saved in a stack frame
464              *    somewhere down below.
465              * In either case we rely on the exception to retrigger,
466              * eventually bailing out if we're spinning on case 2).
467              */
468             static mach_port_t last_thread;
469             static unsigned int last_eip;
470             if (last_thread == thread && last_eip == thread_state.EIP)
471                 ret = KERN_INVALID_RIGHT;
472             else
473                 ret = KERN_SUCCESS;
474             last_thread = thread;
475             last_eip = thread_state.EIP;
476             break;
477         }
478         /* Skip the trap code */
479         thread_state.EIP += 2;
480         /* Return from handler? */
481         if (*(unsigned short *)thread_state.EIP == 0xffff) {
482             if ((ret = thread_set_state(thread,
483                                         x86_THREAD_STATE32,
484                                         (thread_state_t)thread_state.EAX,
485                                         x86_THREAD_STATE32_COUNT)) != KERN_SUCCESS)
486                 lose("thread_set_state (x86_THREAD_STATE32) failed %d\n", ret);
487             if ((ret = thread_set_state(thread,
488                                         x86_FLOAT_STATE32,
489                                         (thread_state_t)thread_state.ECX,
490                                         x86_FLOAT_STATE32_COUNT)) != KERN_SUCCESS)
491                 lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
492             break;
493         }
494         /* Trap call */
495         handler = sigtrap_handler;
496         break;
497     default:
498         ret = KERN_INVALID_RIGHT;
499     }
500     /* Call handler */
501     if (handler != 0) {
502       siginfo.si_signo = signal;
503       siginfo.si_addr = addr;
504       call_handler_on_thread(thread, &thread_state, signal, &siginfo, handler);
505     }
506
507     dealloc_ret = mach_port_deallocate (current_mach_task, thread);
508     if (dealloc_ret) {
509       lose("mach_port_deallocate (thread) failed with return_code %d\n", dealloc_ret);
510     }
511
512     dealloc_ret = mach_port_deallocate (current_mach_task, task);
513     if (dealloc_ret) {
514       lose("mach_port_deallocate (task) failed with return_code %d\n", dealloc_ret);
515     }
516
517     return ret;
518 }
519
520 void
521 os_restore_fp_control(os_context_t *context)
522 {
523     /* KLUDGE: The x87 FPU control word is some nasty bitfield struct
524      * thing.  Rather than deal with that, just grab it as a 16-bit
525      * integer. */
526     unsigned short fpu_control_word =
527         *((unsigned short *)&context->uc_mcontext->FS.FPU_FCW);
528     /* reset exception flags and restore control flags on x87 FPU */
529     asm ("fldcw %0" : : "m" (fpu_control_word));
530 }
531
532 #endif