explicit structure sharing in typed accessor function definitions
[sbcl.git] / src / runtime / x86-64-darwin-os.c
blobc989ee66d0bd5681b2898f1b8ac6f807829285e5
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 pthread_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 pthread_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 /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
209 * context and regs on the stack as local variables, but this
210 * causes problems for the lisp debugger. When it walks the stack
211 * for a back trace, it sees the 1) address of the local variable
212 * on the stack and thinks that is a frame pointer to a lisp
213 * frame, and, 2) the address of the sap that we alloc'ed in
214 * dynamic space and thinks that is a return address, so it,
215 * heuristicly (and wrongly), chooses that this should be
216 * interpreted as a lisp frame instead of as a C frame.
217 * We can work around this in this case by os_validating the
218 * context (and regs just for symmetry).
221 darwin_ucontext *context;
222 darwin_mcontext *regs;
224 context = (darwin_ucontext *) os_validate(0, sizeof(darwin_ucontext));
225 regs = (darwin_mcontext*) os_validate(0, sizeof(darwin_mcontext));
226 context->uc_mcontext = regs;
228 /* when BSD signals are fired, they mask they signals in sa_mask
229 which always seem to be the blockable_sigset, for us, so we
230 need to:
231 1) save the current sigmask
232 2) block blockable signals
233 3) call the signal handler
234 4) restore the sigmask */
236 build_fake_signal_context(context, thread_state, float_state);
238 block_blockable_signals(0, 0);
240 handler(signal, siginfo, context);
242 update_thread_state_from_context(thread_state, float_state, context);
244 os_invalidate((os_vm_address_t)context, sizeof(darwin_ucontext));
245 os_invalidate((os_vm_address_t)regs, sizeof(darwin_mcontext));
247 /* Trap to restore the signal context. */
248 asm volatile (".quad 0xffffffffffff0b0f"
249 : : "a" (thread_state), "b" (float_state));
252 #if defined DUMP_CONTEXT
253 void dump_context(x86_thread_state64_t *context)
255 int i;
256 u64 *stack_pointer;
258 printf("rax: %08lx rcx: %08lx rdx: %08lx rbx: %08lx\n",
259 context->rax, context->rcx, context->rdx, context->rbx);
260 printf("rsp: %08lx rbp: %08lx rsi: %08lx rdi: %08lx\n",
261 context->rsp, context->rbp, context->rsi, context->rdi);
262 printf("rip: %08lx eflags: %08lx\n",
263 context->rip, context->rflags);
264 printf("cs: %04hx ds: %04hx es: %04hx "
265 "ss: %04hx fs: %04hx gs: %04hx\n",
266 context->cs, context->ds, context->rs,
267 context->ss, context->fs, context->gs);
269 stack_pointer = (u64 *)context->rsp;
270 for (i = 0; i < 48; i+=4) {
271 printf("%08x: %08x %08x %08x %08x\n",
272 context->rsp + (i * 4),
273 stack_pointer[i],
274 stack_pointer[i+1],
275 stack_pointer[i+2],
276 stack_pointer[i+3]);
279 #endif
281 void
282 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
283 os_context_t *context) {
284 unblock_signals_in_context_and_maybe_warn(context);
285 arrange_return_to_lisp_function
286 (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
289 void
290 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
291 arrange_return_to_lisp_function
292 (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
295 kern_return_t
296 catch_exception_raise(mach_port_t exception_port,
297 mach_port_t thread,
298 mach_port_t task,
299 exception_type_t exception,
300 exception_data_t code_vector,
301 mach_msg_type_number_t code_count)
303 kern_return_t ret, dealloc_ret;
304 int signal;
305 siginfo_t* siginfo;
307 #ifdef LISP_FEATURE_SB_THREAD
308 thread_mutex_lock(&mach_exception_lock);
309 #endif
311 x86_thread_state64_t thread_state;
312 mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
314 x86_float_state64_t float_state;
315 mach_msg_type_number_t float_state_count = x86_FLOAT_STATE64_COUNT;
317 x86_exception_state64_t exception_state;
318 mach_msg_type_number_t exception_state_count = x86_EXCEPTION_STATE64_COUNT;
320 x86_thread_state64_t backup_thread_state;
321 x86_thread_state64_t *target_thread_state;
322 x86_float_state64_t *target_float_state;
324 os_vm_address_t addr;
326 struct thread *th;
328 FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
329 th = *(struct thread**)exception_port;
331 switch (exception) {
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 /* note the os_context hackery here. When the signal handler returns,
349 * it won't go back to what it was doing ... */
350 if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
351 addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
352 /* We hit the end of the control stack: disable guard page
353 * protection so the error handler has some headroom, protect the
354 * previous page so that we can catch returns from the guard page
355 * and restore it. */
356 lower_thread_control_stack_guard_page(th);
358 backup_thread_state = thread_state;
359 open_stack_allocation(&thread_state);
360 /* Reserve a 256 byte zone for signal handlers
361 * to use on the interrupted thread stack.
363 stack_allocate(&thread_state, 256);
365 /* Save thread state */
366 target_thread_state =
367 stack_allocate(&thread_state, sizeof(*target_thread_state));
368 (*target_thread_state) = backup_thread_state;
370 /* Save float state */
371 target_float_state =
372 stack_allocate(&thread_state, sizeof(*target_float_state));
373 (*target_float_state) = float_state;
375 /* Set up siginfo */
376 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
377 /* what do we need to put in our fake siginfo? It looks like
378 * the x86 code only uses si_signo and si_adrr. */
379 siginfo->si_signo = signal;
380 siginfo->si_addr = (void*)exception_state.faultvaddr;
382 call_c_function_in_context(&thread_state,
383 signal_emulation_wrapper,
385 target_thread_state,
386 target_float_state,
387 signal,
388 siginfo,
389 control_stack_exhausted_handler);
391 else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
392 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
393 /* We're returning from the guard page: reprotect it, and
394 * unprotect this one. This works even if we somehow missed
395 * the return-guard-page, and hit it on our way to new
396 * exhaustion instead. */
397 reset_thread_control_stack_guard_page(th);
399 else if (addr >= undefined_alien_address &&
400 addr < undefined_alien_address + os_vm_page_size) {
401 backup_thread_state = thread_state;
402 open_stack_allocation(&thread_state);
403 stack_allocate(&thread_state, 256);
405 /* Save thread state */
406 target_thread_state =
407 stack_allocate(&thread_state, sizeof(*target_thread_state));
408 (*target_thread_state) = backup_thread_state;
410 target_float_state =
411 stack_allocate(&thread_state, sizeof(*target_float_state));
412 (*target_float_state) = float_state;
414 /* Set up siginfo */
415 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
416 /* what do we need to put in our fake siginfo? It looks like
417 * the x86 code only uses si_signo and si_adrr. */
418 siginfo->si_signo = signal;
419 siginfo->si_addr = (void*)exception_state.faultvaddr;
421 call_c_function_in_context(&thread_state,
422 signal_emulation_wrapper,
424 target_thread_state,
425 target_float_state,
426 signal,
427 siginfo,
428 undefined_alien_handler);
429 } else {
431 backup_thread_state = thread_state;
432 open_stack_allocation(&thread_state);
433 stack_allocate(&thread_state, 256);
435 /* Save thread state */
436 target_thread_state =
437 stack_allocate(&thread_state, sizeof(*target_thread_state));
438 (*target_thread_state) = backup_thread_state;
440 target_float_state =
441 stack_allocate(&thread_state, sizeof(*target_float_state));
442 (*target_float_state) = float_state;
444 /* Set up siginfo */
445 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
446 /* what do we need to put in our fake siginfo? It looks like
447 * the x86 code only uses si_signo and si_adrr. */
448 siginfo->si_signo = signal;
449 siginfo->si_addr = (void*)exception_state.faultvaddr;
451 call_c_function_in_context(&thread_state,
452 signal_emulation_wrapper,
454 target_thread_state,
455 target_float_state,
456 signal,
457 siginfo,
458 memory_fault_handler);
460 ret = thread_set_state(thread,
461 x86_THREAD_STATE64,
462 (thread_state_t)&thread_state,
463 thread_state_count);
465 ret = thread_set_state(thread,
466 x86_FLOAT_STATE64,
467 (thread_state_t)&float_state,
468 float_state_count);
469 #ifdef LISP_FEATURE_SB_THREAD
470 thread_mutex_unlock(&mach_exception_lock);
471 #endif
472 ret = KERN_SUCCESS;
473 break;
475 case EXC_BAD_INSTRUCTION:
477 ret = thread_get_state(thread,
478 x86_THREAD_STATE64,
479 (thread_state_t)&thread_state,
480 &thread_state_count);
481 ret = thread_get_state(thread,
482 x86_FLOAT_STATE64,
483 (thread_state_t)&float_state,
484 &float_state_count);
485 ret = thread_get_state(thread,
486 x86_EXCEPTION_STATE64,
487 (thread_state_t)&exception_state,
488 &exception_state_count);
489 if (0xffffffffffff0b0f == *((u64 *)thread_state.rip)) {
490 /* fake sigreturn. */
492 /* When we get here, thread_state.rax is a pointer to a
493 * thread_state to restore. */
494 /* thread_state = *((thread_state_t *)thread_state.rax); */
496 ret = thread_set_state(thread,
497 x86_THREAD_STATE64,
498 (thread_state_t) thread_state.rax,
499 /* &thread_state, */
500 thread_state_count);
502 ret = thread_set_state(thread,
503 x86_FLOAT_STATE64,
504 (thread_state_t) thread_state.rbx,
505 /* &thread_state, */
506 float_state_count);
507 } else {
509 backup_thread_state = thread_state;
510 open_stack_allocation(&thread_state);
511 stack_allocate(&thread_state, 256);
513 /* Save thread state */
514 target_thread_state =
515 stack_allocate(&thread_state, sizeof(*target_thread_state));
516 (*target_thread_state) = backup_thread_state;
518 target_float_state =
519 stack_allocate(&thread_state, sizeof(*target_float_state));
520 (*target_float_state) = float_state;
522 /* Set up siginfo */
523 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
524 /* what do we need to put in our fake siginfo? It looks like
525 * the x86 code only uses si_signo and si_adrr. */
526 if (*((unsigned short *)target_thread_state->rip) == 0x0b0f) {
527 signal = SIGTRAP;
528 siginfo->si_signo = signal;
529 siginfo->si_addr = (void*)exception_state.faultvaddr;
530 target_thread_state->rip += 2;
531 call_c_function_in_context(&thread_state,
532 signal_emulation_wrapper,
534 target_thread_state,
535 target_float_state,
536 signal,
537 siginfo,
538 sigtrap_handler);
539 } else {
540 signal = SIGILL;
541 siginfo->si_signo = signal;
542 siginfo->si_addr = (void*)exception_state.faultvaddr;
544 call_c_function_in_context(&thread_state,
545 signal_emulation_wrapper,
547 target_thread_state,
548 target_float_state,
549 signal,
550 siginfo,
551 sigill_handler);
553 ret = thread_set_state(thread,
554 x86_THREAD_STATE64,
555 (thread_state_t)&thread_state,
556 thread_state_count);
557 ret = thread_set_state(thread,
558 x86_FLOAT_STATE64,
559 (thread_state_t)&float_state,
560 float_state_count);
562 #ifdef LISP_FEATURE_SB_THREAD
563 thread_mutex_unlock(&mach_exception_lock);
564 #endif
565 ret = KERN_SUCCESS;
566 break;
568 default:
569 #ifdef LISP_FEATURE_SB_THREAD
570 thread_mutex_unlock(&mach_exception_lock);
571 #endif
572 ret = KERN_INVALID_RIGHT;
575 if (current_mach_task == MACH_PORT_NULL)
576 current_mach_task = mach_task_self();
578 dealloc_ret = mach_port_deallocate (current_mach_task, thread);
579 if (dealloc_ret) {
580 lose("mach_port_deallocate (thread) failed with return_code %d\n", dealloc_ret);
583 dealloc_ret = mach_port_deallocate (current_mach_task, task);
584 if (dealloc_ret) {
585 lose("mach_port_deallocate (task) failed with return_code %d\n", dealloc_ret);
588 return ret;
591 void
592 os_restore_fp_control(os_context_t *context)
594 /* KLUDGE: The x87 FPU control word is some nasty bitfield struct
595 * thing. Rather than deal with that, just grab it as a 16-bit
596 * integer. */
597 unsigned short fpu_control_word =
598 *((unsigned short *)&context->uc_mcontext->fs.fpu_fcw);
599 /* reset exception flags and restore control flags on SSE2 FPU */
600 unsigned int temp = (context->uc_mcontext->fs.fpu_mxcsr) & ~0x3F;
601 asm ("ldmxcsr %0" : : "m" (temp));
602 /* same for x87 FPU. */
603 asm ("fldcw %0" : : "m" (fpu_control_word));
606 #endif