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
52 #define fpu_fcw __fpu_fcw
53 #define fpu_mxcsr __fpu_mxcsr
57 typedef struct ucontext darwin_ucontext;
58 typedef struct mcontext darwin_mcontext;
62 #ifdef LISP_FEATURE_SB_THREAD
63 pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER;
66 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
68 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
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);
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;
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);
96 /* Modify a context to push new data on its stack. */
97 void push_context(u64 data, x86_thread_state64_t *context)
101 stack_pointer = (u64*) context->rsp;
102 *(--stack_pointer) = data;
103 context->rsp = (u64) stack_pointer;
106 void align_context_stack(x86_thread_state64_t *context)
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);
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; \
120 _stack_allocation_recover: \
121 lea -48(%rbp), %rsp; \
131 void open_stack_allocation(x86_thread_state64_t *context)
133 void stack_allocation_recover(void);
135 push_context(context->rip, context);
136 push_context(context->rbp, context);
137 context->rbp = context->rsp;
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);
146 context->rip = (u64) stack_allocation_recover;
148 align_context_stack(context);
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
154 void *stack_allocate(x86_thread_state64_t *context, size_t size)
156 /* round up size to 16byte multiple */
157 size = (size + 15) & -16;
159 context->rsp = ((u64)context->rsp) - size;
161 return (void *)context->rsp;
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,
178 /* Set up to restore stack on exit. */
179 open_stack_allocation(context);
181 /* Have to keep stack 16byte aligned on x86/darwin. */
182 for (i = (1 & -nargs); i; i--) {
183 push_context(0, context);
186 context->rsp = ((u64)context->rsp) - nargs * 8;
187 stack_pointer = (u64 *)context->rsp;
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);
201 push_context(context->rip, context);
202 context->rip = (u64) function;
205 void signal_emulation_wrapper(x86_thread_state64_t *thread_state,
206 x86_float_state64_t *float_state,
209 void (*handler)(int, siginfo_t *, void *))
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).
225 darwin_ucontext *context;
226 darwin_mcontext *regs;
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;
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
235 1) save the current sigmask
236 2) block blockable signals
237 3) call the signal handler
238 4) restore the sigmask */
240 build_fake_signal_context(context, thread_state, float_state);
242 block_blockable_signals(0, 0);
244 handler(signal, siginfo, context);
246 update_thread_state_from_context(thread_state, float_state, context);
248 os_invalidate((os_vm_address_t)context, sizeof(darwin_ucontext));
249 os_invalidate((os_vm_address_t)regs, sizeof(darwin_mcontext));
251 /* Trap to restore the signal context. */
252 asm volatile (".quad 0xffffffffffff0b0f"
253 : : "a" (thread_state), "b" (float_state));
256 #if defined DUMP_CONTEXT
257 void dump_context(x86_thread_state64_t *context)
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);
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),
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));
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));
300 catch_exception_raise(mach_port_t exception_port,
303 exception_type_t exception,
304 exception_data_t code_vector,
305 mach_msg_type_number_t code_count)
307 kern_return_t ret, dealloc_ret;
311 #ifdef LISP_FEATURE_SB_THREAD
312 thread_mutex_lock(&mach_exception_lock);
315 x86_thread_state64_t thread_state;
316 mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
318 x86_float_state64_t float_state;
319 mach_msg_type_number_t float_state_count = x86_FLOAT_STATE64_COUNT;
321 x86_exception_state64_t exception_state;
322 mach_msg_type_number_t exception_state_count = x86_EXCEPTION_STATE64_COUNT;
324 x86_thread_state64_t backup_thread_state;
325 x86_thread_state64_t *target_thread_state;
326 x86_float_state64_t *target_float_state;
328 os_vm_address_t addr;
330 struct thread *th = (struct thread*) exception_port;
332 FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
338 ret = thread_get_state(thread,
340 (thread_state_t)&thread_state,
341 &thread_state_count);
342 ret = thread_get_state(thread,
344 (thread_state_t)&float_state,
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;
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
361 lower_thread_control_stack_guard_page(th);
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.
368 stack_allocate(&thread_state, 256);
370 /* Save thread state */
371 target_thread_state =
372 stack_allocate(&thread_state, sizeof(*target_thread_state));
373 (*target_thread_state) = backup_thread_state;
375 /* Save float state */
377 stack_allocate(&thread_state, sizeof(*target_float_state));
378 (*target_float_state) = float_state;
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;
387 call_c_function_in_context(&thread_state,
388 signal_emulation_wrapper,
394 control_stack_exhausted_handler);
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);
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);
410 /* Save thread state */
411 target_thread_state =
412 stack_allocate(&thread_state, sizeof(*target_thread_state));
413 (*target_thread_state) = backup_thread_state;
416 stack_allocate(&thread_state, sizeof(*target_float_state));
417 (*target_float_state) = float_state;
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;
426 call_c_function_in_context(&thread_state,
427 signal_emulation_wrapper,
433 undefined_alien_handler);
436 backup_thread_state = thread_state;
437 open_stack_allocation(&thread_state);
438 stack_allocate(&thread_state, 256);
440 /* Save thread state */
441 target_thread_state =
442 stack_allocate(&thread_state, sizeof(*target_thread_state));
443 (*target_thread_state) = backup_thread_state;
446 stack_allocate(&thread_state, sizeof(*target_float_state));
447 (*target_float_state) = float_state;
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;
456 call_c_function_in_context(&thread_state,
457 signal_emulation_wrapper,
463 memory_fault_handler);
465 ret = thread_set_state(thread,
467 (thread_state_t)&thread_state,
470 ret = thread_set_state(thread,
472 (thread_state_t)&float_state,
474 #ifdef LISP_FEATURE_SB_THREAD
475 thread_mutex_unlock(&mach_exception_lock);
480 case EXC_BAD_INSTRUCTION:
482 ret = thread_get_state(thread,
484 (thread_state_t)&thread_state,
485 &thread_state_count);
486 ret = thread_get_state(thread,
488 (thread_state_t)&float_state,
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. */
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); */
501 ret = thread_set_state(thread,
503 (thread_state_t) thread_state.rax,
507 ret = thread_set_state(thread,
509 (thread_state_t) thread_state.rbx,
514 backup_thread_state = thread_state;
515 open_stack_allocation(&thread_state);
516 stack_allocate(&thread_state, 256);
518 /* Save thread state */
519 target_thread_state =
520 stack_allocate(&thread_state, sizeof(*target_thread_state));
521 (*target_thread_state) = backup_thread_state;
524 stack_allocate(&thread_state, sizeof(*target_float_state));
525 (*target_float_state) = float_state;
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) {
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,
546 siginfo->si_signo = signal;
547 siginfo->si_addr = (void*)exception_state.faultvaddr;
549 call_c_function_in_context(&thread_state,
550 signal_emulation_wrapper,
558 ret = thread_set_state(thread,
560 (thread_state_t)&thread_state,
562 ret = thread_set_state(thread,
564 (thread_state_t)&float_state,
567 #ifdef LISP_FEATURE_SB_THREAD
568 thread_mutex_unlock(&mach_exception_lock);
574 #ifdef LISP_FEATURE_SB_THREAD
575 thread_mutex_unlock(&mach_exception_lock);
577 ret = KERN_INVALID_RIGHT;
580 dealloc_ret = mach_port_deallocate (current_mach_task, thread);
582 lose("mach_port_deallocate (thread) failed with return_code %d\n", dealloc_ret);
585 dealloc_ret = mach_port_deallocate (current_mach_task, task);
587 lose("mach_port_deallocate (task) failed with return_code %d\n", dealloc_ret);
594 os_restore_fp_control(os_context_t *context)
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
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));