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