1 #ifdef LISP_FEATURE_SB_THREAD
2 #include <architecture/i386/table.h>
3 #include <i386/user_ldt.h>
4 #include <mach/mach_init.h>
10 #include "interrupt.h"
11 #include "x86-64-darwin-os.h"
12 #include "genesis/fdefn.h"
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>
28 #include <sys/_structs.h>
33 typedef struct __darwin_ucontext darwin_ucontext;
34 typedef struct __darwin_mcontext64 darwin_mcontext;
47 #define faultvaddr __faultvaddr
54 typedef struct ucontext darwin_ucontext;
55 typedef struct mcontext darwin_mcontext;
59 #ifdef LISP_FEATURE_SB_THREAD
60 pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER;
63 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
65 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
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);
72 /* exc_server handles mach exception messages from the kernel and
73 * calls catch exception raise. We use the system-provided
74 * mach_msg_server, which, I assume, calls exc_server in a loop.
77 extern boolean_t exc_server();
79 /* This executes in the faulting thread as part of the signal
80 * emulation. It is passed a context with the uc_mcontext field
81 * pointing to a valid block of memory. */
82 void build_fake_signal_context(darwin_ucontext *context,
83 x86_thread_state64_t *thread_state,
84 x86_float_state64_t *float_state) {
85 pthread_sigmask(0, NULL, &context->uc_sigmask);
86 context->uc_mcontext->ss = *thread_state;
87 context->uc_mcontext->fs = *float_state;
90 /* This executes in the faulting thread as part of the signal
91 * emulation. It is effectively the inverse operation from above. */
92 void update_thread_state_from_context(x86_thread_state64_t *thread_state,
93 x86_float_state64_t *float_state,
94 darwin_ucontext *context) {
95 *thread_state = context->uc_mcontext->ss;
96 *float_state = context->uc_mcontext->fs;
97 pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
100 /* Modify a context to push new data on its stack. */
101 void push_context(u64 data, x86_thread_state64_t *context)
105 stack_pointer = (u64*) context->rsp;
106 *(--stack_pointer) = data;
107 context->rsp = (u64) stack_pointer;
110 void align_context_stack(x86_thread_state64_t *context)
112 /* 16byte align the stack (provided that the stack is, as it
113 * should be, 8byte aligned. */
114 while (context->rsp & 15) push_context(0, context);
117 /* Stack allocation starts with a context that has a mod-4 ESP value
118 * and needs to leave a context with a mod-16 ESP that will restore
119 * the old ESP value and other register state when activated. The
120 * first part of this is the recovery trampoline, which loads ESP from
121 * EBP, pops EBP, and returns. */
122 asm(".globl _stack_allocation_recover; \
124 _stack_allocation_recover: \
125 lea -48(%rbp), %rsp; \
135 void open_stack_allocation(x86_thread_state64_t *context)
137 void stack_allocation_recover(void);
139 push_context(context->rip, context);
140 push_context(context->rbp, context);
141 context->rbp = context->rsp;
143 push_context(context->r9, context);
144 push_context(context->r8, context);
145 push_context(context->rcx, context);
146 push_context(context->rdx, context);
147 push_context(context->rsi, context);
148 push_context(context->rdi, context);
150 context->rip = (u64) stack_allocation_recover;
152 align_context_stack(context);
155 /* Stack allocation of data starts with a context with a mod-16 ESP
156 * value and reserves some space on it by manipulating the ESP
158 void *stack_allocate(x86_thread_state64_t *context, size_t size)
160 /* round up size to 16byte multiple */
161 size = (size + 15) & -16;
163 context->rsp = ((u64)context->rsp) - size;
165 return (void *)context->rsp;
168 /* Arranging to invoke a C function is tricky, as we have to assume
169 * cdecl calling conventions (caller removes args) and x86/darwin
170 * alignment requirements. The simplest way to arrange this,
171 * actually, is to open a new stack allocation.
172 * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
173 void call_c_function_in_context(x86_thread_state64_t *context,
182 /* Set up to restore stack on exit. */
183 open_stack_allocation(context);
185 /* Have to keep stack 16byte aligned on x86/darwin. */
186 for (i = (1 & -nargs); i; i--) {
187 push_context(0, context);
190 context->rsp = ((u64)context->rsp) - nargs * 8;
191 stack_pointer = (u64 *)context->rsp;
194 if (nargs > 0) context->rdi = va_arg(ap, u64);
195 if (nargs > 1) context->rsi = va_arg(ap, u64);
196 if (nargs > 2) context->rdx = va_arg(ap, u64);
197 if (nargs > 3) context->rcx = va_arg(ap, u64);
198 if (nargs > 4) context->r8 = va_arg(ap, u64);
199 if (nargs > 5) context->r9 = va_arg(ap, u64);
200 for (i = 6; i < nargs; i++) {
201 stack_pointer[i] = va_arg(ap, u64);
205 push_context(context->rip, context);
206 context->rip = (u64) function;
209 void signal_emulation_wrapper(x86_thread_state64_t *thread_state,
210 x86_float_state64_t *float_state,
213 void (*handler)(int, siginfo_t *, void *))
216 /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
217 * context and regs on the stack as local variables, but this
218 * causes problems for the lisp debugger. When it walks the stack
219 * for a back trace, it sees the 1) address of the local variable
220 * on the stack and thinks that is a frame pointer to a lisp
221 * frame, and, 2) the address of the sap that we alloc'ed in
222 * dynamic space and thinks that is a return address, so it,
223 * heuristicly (and wrongly), chooses that this should be
224 * interpreted as a lisp frame instead of as a C frame.
225 * We can work around this in this case by os_validating the
226 * context (and regs just for symmetry).
229 darwin_ucontext *context;
230 darwin_mcontext *regs;
232 context = (darwin_ucontext *) os_validate(0, sizeof(darwin_ucontext));
233 regs = (darwin_mcontext*) os_validate(0, sizeof(darwin_mcontext));
234 context->uc_mcontext = regs;
236 /* when BSD signals are fired, they mask they signals in sa_mask
237 which always seem to be the blockable_sigset, for us, so we
239 1) save the current sigmask
240 2) block blockable signals
241 3) call the signal handler
242 4) restore the sigmask */
244 build_fake_signal_context(context, thread_state, float_state);
246 block_blockable_signals(0, 0);
248 handler(signal, siginfo, context);
250 update_thread_state_from_context(thread_state, float_state, context);
252 os_invalidate((os_vm_address_t)context, sizeof(darwin_ucontext));
253 os_invalidate((os_vm_address_t)regs, sizeof(darwin_mcontext));
255 /* Trap to restore the signal context. */
256 asm volatile ("mov %0, %%rax; mov %1, %%rbx; .quad 0xffffffffffff0b0f"
257 : : "r" (thread_state), "r" (float_state));
260 #if defined DUMP_CONTEXT
261 void dump_context(x86_thread_state64_t *context)
266 printf("rax: %08lx rcx: %08lx rdx: %08lx rbx: %08lx\n",
267 context->rax, context->rcx, context->rdx, context->rbx);
268 printf("rsp: %08lx rbp: %08lx rsi: %08lx rdi: %08lx\n",
269 context->rsp, context->rbp, context->rsi, context->rdi);
270 printf("rip: %08lx eflags: %08lx\n",
271 context->rip, context->rflags);
272 printf("cs: %04hx ds: %04hx es: %04hx "
273 "ss: %04hx fs: %04hx gs: %04hx\n",
274 context->cs, context->ds, context->rs,
275 context->ss, context->fs, context->gs);
277 stack_pointer = (u64 *)context->rsp;
278 for (i = 0; i < 48; i+=4) {
279 printf("%08x: %08x %08x %08x %08x\n",
280 context->rsp + (i * 4),
290 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
291 os_context_t *context) {
292 unblock_signals_in_context_and_maybe_warn(context);
293 arrange_return_to_lisp_function
294 (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
298 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
299 arrange_return_to_lisp_function
300 (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
304 catch_exception_raise(mach_port_t exception_port,
307 exception_type_t exception,
308 exception_data_t code_vector,
309 mach_msg_type_number_t code_count)
315 #ifdef LISP_FEATURE_SB_THREAD
316 thread_mutex_lock(&mach_exception_lock);
319 x86_thread_state64_t thread_state;
320 mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
322 x86_float_state64_t float_state;
323 mach_msg_type_number_t float_state_count = x86_FLOAT_STATE64_COUNT;
325 x86_exception_state64_t exception_state;
326 mach_msg_type_number_t exception_state_count = x86_EXCEPTION_STATE64_COUNT;
328 x86_thread_state64_t backup_thread_state;
329 x86_thread_state64_t *target_thread_state;
330 x86_float_state64_t *target_float_state;
332 os_vm_address_t addr;
334 struct thread *th = (struct thread*) exception_port;
336 FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
342 ret = thread_get_state(thread,
344 (thread_state_t)&thread_state,
345 &thread_state_count);
346 ret = thread_get_state(thread,
348 (thread_state_t)&float_state,
350 ret = thread_get_state(thread,
351 x86_EXCEPTION_STATE64,
352 (thread_state_t)&exception_state,
353 &exception_state_count);
354 addr = (void*)exception_state.faultvaddr;
357 /* note the os_context hackery here. When the signal handler returns,
358 * it won't go back to what it was doing ... */
359 if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
360 addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
361 /* We hit the end of the control stack: disable guard page
362 * protection so the error handler has some headroom, protect the
363 * previous page so that we can catch returns from the guard page
365 lower_thread_control_stack_guard_page(th);
367 backup_thread_state = thread_state;
368 open_stack_allocation(&thread_state);
369 /* Reserve a 256 byte zone for signal handlers
370 * to use on the interrupted thread stack.
372 stack_allocate(&thread_state, 256);
374 /* Save thread state */
375 target_thread_state =
376 stack_allocate(&thread_state, sizeof(*target_thread_state));
377 (*target_thread_state) = backup_thread_state;
379 /* Save float state */
381 stack_allocate(&thread_state, sizeof(*target_float_state));
382 (*target_float_state) = float_state;
385 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
386 /* what do we need to put in our fake siginfo? It looks like
387 * the x86 code only uses si_signo and si_adrr. */
388 siginfo->si_signo = signal;
389 siginfo->si_addr = (void*)exception_state.faultvaddr;
391 call_c_function_in_context(&thread_state,
392 signal_emulation_wrapper,
398 control_stack_exhausted_handler);
400 else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
401 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
402 /* We're returning from the guard page: reprotect it, and
403 * unprotect this one. This works even if we somehow missed
404 * the return-guard-page, and hit it on our way to new
405 * exhaustion instead. */
406 reset_thread_control_stack_guard_page(th);
408 else if (addr >= undefined_alien_address &&
409 addr < undefined_alien_address + os_vm_page_size) {
410 backup_thread_state = thread_state;
411 open_stack_allocation(&thread_state);
412 stack_allocate(&thread_state, 256);
414 /* Save thread state */
415 target_thread_state =
416 stack_allocate(&thread_state, sizeof(*target_thread_state));
417 (*target_thread_state) = backup_thread_state;
420 stack_allocate(&thread_state, sizeof(*target_float_state));
421 (*target_float_state) = float_state;
424 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
425 /* what do we need to put in our fake siginfo? It looks like
426 * the x86 code only uses si_signo and si_adrr. */
427 siginfo->si_signo = signal;
428 siginfo->si_addr = (void*)exception_state.faultvaddr;
430 call_c_function_in_context(&thread_state,
431 signal_emulation_wrapper,
437 undefined_alien_handler);
440 backup_thread_state = thread_state;
441 open_stack_allocation(&thread_state);
442 stack_allocate(&thread_state, 256);
444 /* Save thread state */
445 target_thread_state =
446 stack_allocate(&thread_state, sizeof(*target_thread_state));
447 (*target_thread_state) = backup_thread_state;
450 stack_allocate(&thread_state, sizeof(*target_float_state));
451 (*target_float_state) = float_state;
454 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
455 /* what do we need to put in our fake siginfo? It looks like
456 * the x86 code only uses si_signo and si_adrr. */
457 siginfo->si_signo = signal;
458 siginfo->si_addr = (void*)exception_state.faultvaddr;
460 call_c_function_in_context(&thread_state,
461 signal_emulation_wrapper,
467 memory_fault_handler);
469 ret = thread_set_state(thread,
471 (thread_state_t)&thread_state,
474 ret = thread_set_state(thread,
476 (thread_state_t)&float_state,
478 #ifdef LISP_FEATURE_SB_THREAD
479 thread_mutex_unlock(&mach_exception_lock);
483 case EXC_BAD_INSTRUCTION:
485 ret = thread_get_state(thread,
487 (thread_state_t)&thread_state,
488 &thread_state_count);
489 ret = thread_get_state(thread,
491 (thread_state_t)&float_state,
493 ret = thread_get_state(thread,
494 x86_EXCEPTION_STATE64,
495 (thread_state_t)&exception_state,
496 &exception_state_count);
497 if (0xffffffffffff0b0f == *((u64 *)thread_state.rip)) {
498 /* fake sigreturn. */
500 /* When we get here, thread_state.rax is a pointer to a
501 * thread_state to restore. */
502 /* thread_state = *((thread_state_t *)thread_state.rax); */
504 ret = thread_set_state(thread,
506 (thread_state_t) thread_state.rax,
510 ret = thread_set_state(thread,
512 (thread_state_t) thread_state.rbx,
517 backup_thread_state = thread_state;
518 open_stack_allocation(&thread_state);
519 stack_allocate(&thread_state, 256);
521 /* Save thread state */
522 target_thread_state =
523 stack_allocate(&thread_state, sizeof(*target_thread_state));
524 (*target_thread_state) = backup_thread_state;
527 stack_allocate(&thread_state, sizeof(*target_float_state));
528 (*target_float_state) = float_state;
531 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
532 /* what do we need to put in our fake siginfo? It looks like
533 * the x86 code only uses si_signo and si_adrr. */
534 if (*((unsigned short *)target_thread_state->rip) == 0x0b0f) {
536 siginfo->si_signo = signal;
537 siginfo->si_addr = (void*)exception_state.faultvaddr;
538 target_thread_state->rip += 2;
539 call_c_function_in_context(&thread_state,
540 signal_emulation_wrapper,
549 siginfo->si_signo = signal;
550 siginfo->si_addr = (void*)exception_state.faultvaddr;
552 call_c_function_in_context(&thread_state,
553 signal_emulation_wrapper,
561 ret = thread_set_state(thread,
563 (thread_state_t)&thread_state,
565 ret = thread_set_state(thread,
567 (thread_state_t)&float_state,
570 #ifdef LISP_FEATURE_SB_THREAD
571 thread_mutex_unlock(&mach_exception_lock);
576 #ifdef LISP_FEATURE_SB_THREAD
577 thread_mutex_unlock(&mach_exception_lock);
579 return KERN_INVALID_RIGHT;
584 mach_exception_handler(void *port)
586 mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
587 /* mach_msg_server should never return, but it should dispatch mach
588 * exceptions to our catch_exception_raise function
590 lose("mach_msg_server returned");
593 /* Sets up the thread that will listen for mach exceptions. note that
594 the exception handlers will be run on this thread. This is
595 different from the BSD-style signal handling situation in which the
596 signal handlers run in the relevant thread directly. */
598 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
601 setup_mach_exception_handling_thread()
604 pthread_t mach_exception_handling_thread = NULL;
607 /* allocate a mach_port for this process */
608 ret = mach_port_allocate(mach_task_self(),
609 MACH_PORT_RIGHT_PORT_SET,
610 &mach_exception_handler_port_set);
612 /* create the thread that will receive the mach exceptions */
614 FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
616 pthread_attr_init(&attr);
617 pthread_create(&mach_exception_handling_thread,
619 mach_exception_handler,
620 (void*) mach_exception_handler_port_set);
621 pthread_attr_destroy(&attr);
623 return mach_exception_handling_thread;
626 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
627 exception port (which is being listened to do by the mach
628 exception handling thread). */
630 mach_thread_init(mach_port_t thread_exception_port)
633 /* allocate a named port for the thread */
635 FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
637 ret = mach_port_allocate_name(mach_task_self(),
638 MACH_PORT_RIGHT_RECEIVE,
639 thread_exception_port);
641 lose("mach_port_allocate_name failed with return_code %d\n", ret);
644 /* establish the right for the thread_exception_port to send messages */
645 ret = mach_port_insert_right(mach_task_self(),
646 thread_exception_port,
647 thread_exception_port,
648 MACH_MSG_TYPE_MAKE_SEND);
650 lose("mach_port_insert_right failed with return_code %d\n", ret);
653 ret = thread_set_exception_ports(mach_thread_self(),
654 EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
655 thread_exception_port,
659 lose("thread_set_exception_port failed with return_code %d\n", ret);
662 ret = mach_port_move_member(mach_task_self(),
663 thread_exception_port,
664 mach_exception_handler_port_set);
666 lose("mach_port_ failed with return_code %d\n", ret);
673 setup_mach_exceptions() {
674 setup_mach_exception_handling_thread();
675 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
682 setup_mach_exceptions();