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