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