Miscellaneous cleanups for threaded darwin platforms
[sbcl.git] / src / runtime / x86-64-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-64-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/machine/thread_state.h>
19 #include <mach/machine/thread_status.h>
20 #include <sys/_types.h>
21 #include <sys/ucontext.h>
22 #include <pthread.h>
23 #include <assert.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #if __DARWIN_UNIX03
28 #include <sys/_structs.h>
29 #endif
30
31 #if __DARWIN_UNIX03
32
33 typedef struct __darwin_ucontext darwin_ucontext;
34 typedef struct __darwin_mcontext64 darwin_mcontext;
35
36 #define rip __rip
37 #define rsp __rsp
38 #define rbp __rbp
39 #define rax __rax
40 #define rbx __rbx
41 #define rcx __rcx
42 #define rdx __rdx
43 #define rsi __rsi
44 #define rdi __rdi
45 #define r8 __r8
46 #define r9 __r9
47 #define faultvaddr __faultvaddr
48 #define ss __ss
49 #define es __es
50 #define fs __fs
51
52 #define fpu_fcw __fpu_fcw
53 #define fpu_mxcsr __fpu_mxcsr
54
55 #else
56
57 typedef struct ucontext darwin_ucontext;
58 typedef struct mcontext darwin_mcontext;
59
60 #endif
61
62 #ifdef LISP_FEATURE_SB_THREAD
63 pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER;
64 #endif
65
66 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
67
68 void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context);
69 void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context);
70 void memory_fault_handler(int signal, siginfo_t *siginfo,
71                           os_context_t *context);
72
73 /* This executes in the faulting thread as part of the signal
74  * emulation.  It is passed a context with the uc_mcontext field
75  * pointing to a valid block of memory. */
76 void build_fake_signal_context(darwin_ucontext *context,
77                                x86_thread_state64_t *thread_state,
78                                x86_float_state64_t *float_state) {
79     pthread_sigmask(0, NULL, &context->uc_sigmask);
80     context->uc_mcontext->ss = *thread_state;
81     context->uc_mcontext->fs = *float_state;
82 }
83
84 /* This executes in the faulting thread as part of the signal
85  * emulation.  It is effectively the inverse operation from above. */
86 void update_thread_state_from_context(x86_thread_state64_t *thread_state,
87                                       x86_float_state64_t *float_state,
88                                       darwin_ucontext  *context) {
89     *thread_state = context->uc_mcontext->ss;
90     *float_state = context->uc_mcontext->fs;
91     pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
92 }
93
94 /* Modify a context to push new data on its stack. */
95 void push_context(u64 data, x86_thread_state64_t *context)
96 {
97     u64 *stack_pointer;
98
99     stack_pointer = (u64*) context->rsp;
100     *(--stack_pointer) = data;
101     context->rsp = (u64) stack_pointer;
102 }
103
104 void align_context_stack(x86_thread_state64_t *context)
105 {
106     /* 16byte align the stack (provided that the stack is, as it
107      * should be, 8byte aligned. */
108     while (context->rsp & 15) push_context(0, context);
109 }
110
111 /* Stack allocation starts with a context that has a mod-4 ESP value
112  * and needs to leave a context with a mod-16 ESP that will restore
113  * the old ESP value and other register state when activated.  The
114  * first part of this is the recovery trampoline, which loads ESP from
115  * EBP, pops EBP, and returns. */
116 asm(".globl _stack_allocation_recover; \
117     .align 4; \
118  _stack_allocation_recover: \
119     lea -48(%rbp), %rsp; \
120     pop %rsi; \
121     pop %rdi; \
122     pop %rdx; \
123     pop %rcx; \
124     pop %r8; \
125     pop %r9; \
126     pop %rbp; \
127     ret;");
128
129 void open_stack_allocation(x86_thread_state64_t *context)
130 {
131     void stack_allocation_recover(void);
132
133     push_context(context->rip, context);
134     push_context(context->rbp, context);
135     context->rbp = context->rsp;
136
137     push_context(context->r9, context);
138     push_context(context->r8, context);
139     push_context(context->rcx, context);
140     push_context(context->rdx, context);
141     push_context(context->rsi, context);
142     push_context(context->rdi, context);
143
144     context->rip = (u64) stack_allocation_recover;
145
146     align_context_stack(context);
147 }
148
149 /* Stack allocation of data starts with a context with a mod-16 ESP
150  * value and reserves some space on it by manipulating the ESP
151  * register. */
152 void *stack_allocate(x86_thread_state64_t *context, size_t size)
153 {
154     /* round up size to 16byte multiple */
155     size = (size + 15) & -16;
156
157     context->rsp = ((u64)context->rsp) - size;
158
159     return (void *)context->rsp;
160 }
161
162 /* Arranging to invoke a C function is tricky, as we have to assume
163  * cdecl calling conventions (caller removes args) and x86/darwin
164  * alignment requirements.  The simplest way to arrange this,
165  * actually, is to open a new stack allocation.
166  * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
167 void call_c_function_in_context(x86_thread_state64_t *context,
168                                 void *function,
169                                 int nargs,
170                                 ...)
171 {
172     va_list ap;
173     int i;
174     u64 *stack_pointer;
175
176     /* Set up to restore stack on exit. */
177     open_stack_allocation(context);
178
179     /* Have to keep stack 16byte aligned on x86/darwin. */
180     for (i = (1 & -nargs); i; i--) {
181         push_context(0, context);
182     }
183
184     context->rsp = ((u64)context->rsp) - nargs * 8;
185     stack_pointer = (u64 *)context->rsp;
186
187     va_start(ap, nargs);
188     if (nargs > 0) context->rdi = va_arg(ap, u64);
189     if (nargs > 1) context->rsi = va_arg(ap, u64);
190     if (nargs > 2) context->rdx = va_arg(ap, u64);
191     if (nargs > 3) context->rcx = va_arg(ap, u64);
192     if (nargs > 4) context->r8 = va_arg(ap, u64);
193     if (nargs > 5) context->r9 = va_arg(ap, u64);
194     for (i = 6; i < nargs; i++) {
195         stack_pointer[i] = va_arg(ap, u64);
196     }
197     va_end(ap);
198
199     push_context(context->rip, context);
200     context->rip = (u64) function;
201 }
202
203 void signal_emulation_wrapper(x86_thread_state64_t *thread_state,
204                               x86_float_state64_t *float_state,
205                               int signal,
206                               siginfo_t *siginfo,
207                               void (*handler)(int, siginfo_t *, void *))
208 {
209
210     /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
211      * context and regs on the stack as local variables, but this
212      * causes problems for the lisp debugger. When it walks the stack
213      * for a back trace, it sees the 1) address of the local variable
214      * on the stack and thinks that is a frame pointer to a lisp
215      * frame, and, 2) the address of the sap that we alloc'ed in
216      * dynamic space and thinks that is a return address, so it,
217      * heuristicly (and wrongly), chooses that this should be
218      * interpreted as a lisp frame instead of as a C frame.
219      * We can work around this in this case by os_validating the
220      * context (and regs just for symmetry).
221      */
222
223     darwin_ucontext  *context;
224     darwin_mcontext *regs;
225
226     context = (darwin_ucontext *) os_validate(0, sizeof(darwin_ucontext));
227     regs = (darwin_mcontext*) os_validate(0, sizeof(darwin_mcontext));
228     context->uc_mcontext = regs;
229
230     /* when BSD signals are fired, they mask they signals in sa_mask
231        which always seem to be the blockable_sigset, for us, so we
232        need to:
233        1) save the current sigmask
234        2) block blockable signals
235        3) call the signal handler
236        4) restore the sigmask */
237
238     build_fake_signal_context(context, thread_state, float_state);
239
240     block_blockable_signals(0, 0);
241
242     handler(signal, siginfo, context);
243
244     update_thread_state_from_context(thread_state, float_state, context);
245
246     os_invalidate((os_vm_address_t)context, sizeof(darwin_ucontext));
247     os_invalidate((os_vm_address_t)regs, sizeof(darwin_mcontext));
248
249     /* Trap to restore the signal context. */
250     asm volatile (".quad 0xffffffffffff0b0f"
251                   : : "a" (thread_state), "b" (float_state));
252 }
253
254 #if defined DUMP_CONTEXT
255 void dump_context(x86_thread_state64_t *context)
256 {
257     int i;
258     u64 *stack_pointer;
259
260     printf("rax: %08lx  rcx: %08lx  rdx: %08lx  rbx: %08lx\n",
261            context->rax, context->rcx, context->rdx, context->rbx);
262     printf("rsp: %08lx  rbp: %08lx  rsi: %08lx  rdi: %08lx\n",
263            context->rsp, context->rbp, context->rsi, context->rdi);
264     printf("rip: %08lx  eflags: %08lx\n",
265            context->rip, context->rflags);
266     printf("cs: %04hx  ds: %04hx  es: %04hx  "
267            "ss: %04hx  fs: %04hx  gs: %04hx\n",
268            context->cs, context->ds, context->rs,
269            context->ss, context->fs, context->gs);
270
271     stack_pointer = (u64 *)context->rsp;
272     for (i = 0; i < 48; i+=4) {
273         printf("%08x:  %08x %08x %08x %08x\n",
274                context->rsp + (i * 4),
275                stack_pointer[i],
276                stack_pointer[i+1],
277                stack_pointer[i+2],
278                stack_pointer[i+3]);
279     }
280 }
281 #endif
282
283 void
284 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
285                                 os_context_t *context) {
286     unblock_signals_in_context_and_maybe_warn(context);
287     arrange_return_to_lisp_function
288         (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
289 }
290
291 void
292 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
293     arrange_return_to_lisp_function
294         (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
295 }
296
297 kern_return_t
298 catch_exception_raise(mach_port_t exception_port,
299                       mach_port_t thread,
300                       mach_port_t task,
301                       exception_type_t exception,
302                       exception_data_t code_vector,
303                       mach_msg_type_number_t code_count)
304 {
305     kern_return_t ret, dealloc_ret;
306     int signal;
307     siginfo_t* siginfo;
308
309 #ifdef LISP_FEATURE_SB_THREAD
310     thread_mutex_lock(&mach_exception_lock);
311 #endif
312
313     x86_thread_state64_t thread_state;
314     mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
315
316     x86_float_state64_t float_state;
317     mach_msg_type_number_t float_state_count = x86_FLOAT_STATE64_COUNT;
318
319     x86_exception_state64_t exception_state;
320     mach_msg_type_number_t exception_state_count = x86_EXCEPTION_STATE64_COUNT;
321
322     x86_thread_state64_t backup_thread_state;
323     x86_thread_state64_t *target_thread_state;
324     x86_float_state64_t *target_float_state;
325
326     os_vm_address_t addr;
327
328     struct thread *th = (struct thread*) exception_port;
329
330     FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
331
332     switch (exception) {
333
334     case EXC_BAD_ACCESS:
335         signal = SIGBUS;
336         ret = thread_get_state(thread,
337                                x86_THREAD_STATE64,
338                                (thread_state_t)&thread_state,
339                                &thread_state_count);
340         ret = thread_get_state(thread,
341                                x86_FLOAT_STATE64,
342                                (thread_state_t)&float_state,
343                                &float_state_count);
344         ret = thread_get_state(thread,
345                                x86_EXCEPTION_STATE64,
346                                (thread_state_t)&exception_state,
347                                &exception_state_count);
348         addr = (void*)exception_state.faultvaddr;
349
350
351         /* note the os_context hackery here.  When the signal handler returns,
352          * it won't go back to what it was doing ... */
353         if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
354            addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
355             /* We hit the end of the control stack: disable guard page
356              * protection so the error handler has some headroom, protect the
357              * previous page so that we can catch returns from the guard page
358              * and restore it. */
359             lower_thread_control_stack_guard_page(th);
360
361             backup_thread_state = thread_state;
362             open_stack_allocation(&thread_state);
363             /* Reserve a 256 byte zone for signal handlers
364              * to use on the interrupted thread stack.
365              */
366             stack_allocate(&thread_state, 256);
367
368             /* Save thread state */
369             target_thread_state =
370                 stack_allocate(&thread_state, sizeof(*target_thread_state));
371             (*target_thread_state) = backup_thread_state;
372
373             /* Save float state */
374             target_float_state =
375                 stack_allocate(&thread_state, sizeof(*target_float_state));
376             (*target_float_state) = float_state;
377
378             /* Set up siginfo */
379             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
380             /* what do we need to put in our fake siginfo?  It looks like
381              * the x86 code only uses si_signo and si_adrr. */
382             siginfo->si_signo = signal;
383             siginfo->si_addr = (void*)exception_state.faultvaddr;
384
385             call_c_function_in_context(&thread_state,
386                                        signal_emulation_wrapper,
387                                        5,
388                                        target_thread_state,
389                                        target_float_state,
390                                        signal,
391                                        siginfo,
392                                        control_stack_exhausted_handler);
393         }
394         else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
395                 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
396             /* We're returning from the guard page: reprotect it, and
397              * unprotect this one. This works even if we somehow missed
398              * the return-guard-page, and hit it on our way to new
399              * exhaustion instead. */
400             reset_thread_control_stack_guard_page(th);
401         }
402         else if (addr >= undefined_alien_address &&
403                  addr < undefined_alien_address + os_vm_page_size) {
404             backup_thread_state = thread_state;
405             open_stack_allocation(&thread_state);
406             stack_allocate(&thread_state, 256);
407
408             /* Save thread state */
409             target_thread_state =
410                 stack_allocate(&thread_state, sizeof(*target_thread_state));
411             (*target_thread_state) = backup_thread_state;
412
413             target_float_state =
414                 stack_allocate(&thread_state, sizeof(*target_float_state));
415             (*target_float_state) = float_state;
416
417             /* Set up siginfo */
418             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
419             /* what do we need to put in our fake siginfo?  It looks like
420              * the x86 code only uses si_signo and si_adrr. */
421             siginfo->si_signo = signal;
422             siginfo->si_addr = (void*)exception_state.faultvaddr;
423
424             call_c_function_in_context(&thread_state,
425                                        signal_emulation_wrapper,
426                                        5,
427                                        target_thread_state,
428                                        target_float_state,
429                                        signal,
430                                        siginfo,
431                                        undefined_alien_handler);
432         } else {
433
434             backup_thread_state = thread_state;
435             open_stack_allocation(&thread_state);
436             stack_allocate(&thread_state, 256);
437
438             /* Save thread state */
439             target_thread_state =
440                 stack_allocate(&thread_state, sizeof(*target_thread_state));
441             (*target_thread_state) = backup_thread_state;
442
443             target_float_state =
444                 stack_allocate(&thread_state, sizeof(*target_float_state));
445             (*target_float_state) = float_state;
446
447             /* Set up siginfo */
448             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
449             /* what do we need to put in our fake siginfo?  It looks like
450              * the x86 code only uses si_signo and si_adrr. */
451             siginfo->si_signo = signal;
452             siginfo->si_addr = (void*)exception_state.faultvaddr;
453
454             call_c_function_in_context(&thread_state,
455                                        signal_emulation_wrapper,
456                                        5,
457                                        target_thread_state,
458                                        target_float_state,
459                                        signal,
460                                        siginfo,
461                                        memory_fault_handler);
462         }
463         ret = thread_set_state(thread,
464                                x86_THREAD_STATE64,
465                                (thread_state_t)&thread_state,
466                                thread_state_count);
467
468         ret = thread_set_state(thread,
469                                x86_FLOAT_STATE64,
470                                (thread_state_t)&float_state,
471                                float_state_count);
472 #ifdef LISP_FEATURE_SB_THREAD
473         thread_mutex_unlock(&mach_exception_lock);
474 #endif
475         ret = KERN_SUCCESS;
476         break;
477
478     case EXC_BAD_INSTRUCTION:
479
480         ret = thread_get_state(thread,
481                                x86_THREAD_STATE64,
482                                (thread_state_t)&thread_state,
483                                &thread_state_count);
484         ret = thread_get_state(thread,
485                                x86_FLOAT_STATE64,
486                                (thread_state_t)&float_state,
487                                &float_state_count);
488         ret = thread_get_state(thread,
489                                x86_EXCEPTION_STATE64,
490                                (thread_state_t)&exception_state,
491                                &exception_state_count);
492         if (0xffffffffffff0b0f == *((u64 *)thread_state.rip)) {
493             /* fake sigreturn. */
494
495             /* When we get here, thread_state.rax is a pointer to a
496              * thread_state to restore. */
497             /* thread_state = *((thread_state_t *)thread_state.rax); */
498
499             ret = thread_set_state(thread,
500                                    x86_THREAD_STATE64,
501                                    (thread_state_t) thread_state.rax,
502                                    /* &thread_state, */
503                                    thread_state_count);
504
505             ret = thread_set_state(thread,
506                                    x86_FLOAT_STATE64,
507                                    (thread_state_t) thread_state.rbx,
508                                    /* &thread_state, */
509                                    float_state_count);
510         } else {
511
512             backup_thread_state = thread_state;
513             open_stack_allocation(&thread_state);
514             stack_allocate(&thread_state, 256);
515
516             /* Save thread state */
517             target_thread_state =
518                 stack_allocate(&thread_state, sizeof(*target_thread_state));
519             (*target_thread_state) = backup_thread_state;
520
521             target_float_state =
522                 stack_allocate(&thread_state, sizeof(*target_float_state));
523             (*target_float_state) = float_state;
524
525             /* Set up siginfo */
526             siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
527             /* what do we need to put in our fake siginfo?  It looks like
528              * the x86 code only uses si_signo and si_adrr. */
529             if (*((unsigned short *)target_thread_state->rip) == 0x0b0f) {
530                 signal = SIGTRAP;
531                 siginfo->si_signo = signal;
532                 siginfo->si_addr = (void*)exception_state.faultvaddr;
533                 target_thread_state->rip += 2;
534                 call_c_function_in_context(&thread_state,
535                                            signal_emulation_wrapper,
536                                            5,
537                                            target_thread_state,
538                                            target_float_state,
539                                            signal,
540                                            siginfo,
541                                            sigtrap_handler);
542             } else {
543                 signal = SIGILL;
544                 siginfo->si_signo = signal;
545                 siginfo->si_addr = (void*)exception_state.faultvaddr;
546
547                 call_c_function_in_context(&thread_state,
548                                            signal_emulation_wrapper,
549                                            5,
550                                            target_thread_state,
551                                            target_float_state,
552                                            signal,
553                                            siginfo,
554                                            sigill_handler);
555             }
556             ret = thread_set_state(thread,
557                                    x86_THREAD_STATE64,
558                                    (thread_state_t)&thread_state,
559                                    thread_state_count);
560             ret = thread_set_state(thread,
561                                    x86_FLOAT_STATE64,
562                                    (thread_state_t)&float_state,
563                                    float_state_count);
564         }
565 #ifdef LISP_FEATURE_SB_THREAD
566         thread_mutex_unlock(&mach_exception_lock);
567 #endif
568         ret = KERN_SUCCESS;
569         break;
570
571     default:
572 #ifdef LISP_FEATURE_SB_THREAD
573         thread_mutex_unlock(&mach_exception_lock);
574 #endif
575         ret = KERN_INVALID_RIGHT;
576     }
577
578     dealloc_ret = mach_port_deallocate (current_mach_task, thread);
579     if (dealloc_ret) {
580       lose("mach_port_deallocate (thread) failed with return_code %d\n", dealloc_ret);
581     }
582
583     dealloc_ret = mach_port_deallocate (current_mach_task, task);
584     if (dealloc_ret) {
585       lose("mach_port_deallocate (task) failed with return_code %d\n", dealloc_ret);
586     }
587
588     return ret;
589 }
590
591 void
592 os_restore_fp_control(os_context_t *context)
593 {
594     /* KLUDGE: The x87 FPU control word is some nasty bitfield struct
595      * thing.  Rather than deal with that, just grab it as a 16-bit
596      * integer. */
597     unsigned short fpu_control_word =
598         *((unsigned short *)&context->uc_mcontext->fs.fpu_fcw);
599     /* reset exception flags and restore control flags on SSE2 FPU */
600     unsigned int temp = (context->uc_mcontext->fs.fpu_mxcsr) & ~0x3F;
601     asm ("ldmxcsr %0" : : "m" (temp));
602     /* same for x87 FPU. */
603     asm ("fldcw %0" : : "m" (fpu_control_word));
604 }
605
606 #endif