Add very hacky symbol constant support and function calling.
[sbcl/llvm.git] / src / runtime / x86-64-darwin-os.c
blobed8abbbf16b581eb032dc9319032e80822cb7710
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
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"
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>
27 #if __DARWIN_UNIX03
28 #include <sys/_structs.h>
29 #endif
31 #if __DARWIN_UNIX03
33 typedef struct __darwin_ucontext darwin_ucontext;
34 typedef struct __darwin_mcontext64 darwin_mcontext;
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
52 #else
54 typedef struct ucontext darwin_ucontext;
55 typedef struct mcontext darwin_mcontext;
57 #endif
59 #ifdef LISP_FEATURE_SB_THREAD
60 pthread_mutex_t mach_exception_lock = PTHREAD_MUTEX_INITIALIZER;
61 #endif
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)
103 u64 *stack_pointer;
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; .align 4; _stack_allocation_recover: mov %rbp, %rsp; pop %rsi; pop %rdi; pop \
123 %rdx; pop %rcx; pop %r8; pop %r9; pop %rbp; ret;");
125 void open_stack_allocation(x86_thread_state64_t *context)
127 void stack_allocation_recover(void);
129 push_context(context->rip, context);
130 push_context(context->rbp, context);
132 push_context(context->r9, context);
133 push_context(context->r8, context);
134 push_context(context->rcx, context);
135 push_context(context->rdx, context);
136 push_context(context->rsi, context);
137 push_context(context->rdi, context);
139 context->rbp = context->rsp;
140 context->rip = (u64) stack_allocation_recover;
142 align_context_stack(context);
145 /* Stack allocation of data starts with a context with a mod-16 ESP
146 * value and reserves some space on it by manipulating the ESP
147 * register. */
148 void *stack_allocate(x86_thread_state64_t *context, size_t size)
150 /* round up size to 16byte multiple */
151 size = (size + 15) & -16;
153 context->rsp = ((u64)context->rsp) - size;
155 return (void *)context->rsp;
158 /* Arranging to invoke a C function is tricky, as we have to assume
159 * cdecl calling conventions (caller removes args) and x86/darwin
160 * alignment requirements. The simplest way to arrange this,
161 * actually, is to open a new stack allocation.
162 * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
163 void call_c_function_in_context(x86_thread_state64_t *context,
164 void *function,
165 int nargs,
166 ...)
168 va_list ap;
169 int i;
170 u64 *stack_pointer;
172 /* Set up to restore stack on exit. */
173 open_stack_allocation(context);
175 /* Have to keep stack 16byte aligned on x86/darwin. */
176 for (i = (1 & -nargs); i; i--) {
177 push_context(0, context);
180 context->rsp = ((u64)context->rsp) - nargs * 8;
181 stack_pointer = (u64 *)context->rsp;
183 va_start(ap, nargs);
184 if (nargs > 0) context->rdi = va_arg(ap, u64);
185 if (nargs > 1) context->rsi = va_arg(ap, u64);
186 if (nargs > 2) context->rdx = va_arg(ap, u64);
187 if (nargs > 3) context->rcx = va_arg(ap, u64);
188 if (nargs > 4) context->r8 = va_arg(ap, u64);
189 if (nargs > 5) context->r9 = va_arg(ap, u64);
190 for (i = 6; i < nargs; i++) {
191 stack_pointer[i] = va_arg(ap, u64);
193 va_end(ap);
195 push_context(context->rip, context);
196 context->rip = (u64) function;
199 void signal_emulation_wrapper(x86_thread_state64_t *thread_state,
200 x86_float_state64_t *float_state,
201 int signal,
202 siginfo_t *siginfo,
203 void (*handler)(int, siginfo_t *, void *))
206 /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
207 * context and regs on the stack as local variables, but this
208 * causes problems for the lisp debugger. When it walks the stack
209 * for a back trace, it sees the 1) address of the local variable
210 * on the stack and thinks that is a frame pointer to a lisp
211 * frame, and, 2) the address of the sap that we alloc'ed in
212 * dynamic space and thinks that is a return address, so it,
213 * heuristicly (and wrongly), chooses that this should be
214 * interpreted as a lisp frame instead of as a C frame.
215 * We can work around this in this case by os_validating the
216 * context (and regs just for symmetry).
219 darwin_ucontext *context;
220 darwin_mcontext *regs;
222 context = (darwin_ucontext *) os_validate(0, sizeof(darwin_ucontext));
223 regs = (darwin_mcontext*) os_validate(0, sizeof(darwin_mcontext));
224 context->uc_mcontext = regs;
226 /* when BSD signals are fired, they mask they signals in sa_mask
227 which always seem to be the blockable_sigset, for us, so we
228 need to:
229 1) save the current sigmask
230 2) block blockable signals
231 3) call the signal handler
232 4) restore the sigmask */
234 build_fake_signal_context(context, thread_state, float_state);
236 block_blockable_signals(0, 0);
238 handler(signal, siginfo, context);
240 update_thread_state_from_context(thread_state, float_state, context);
242 os_invalidate((os_vm_address_t)context, sizeof(darwin_ucontext));
243 os_invalidate((os_vm_address_t)regs, sizeof(darwin_mcontext));
245 /* Trap to restore the signal context. */
246 asm volatile ("mov %0, %%rax; mov %1, %%rbx; .quad 0xffffffffffff0b0f"
247 : : "r" (thread_state), "r" (float_state));
250 #if defined DUMP_CONTEXT
251 void dump_context(x86_thread_state64_t *context)
253 int i;
254 u64 *stack_pointer;
256 printf("rax: %08lx rcx: %08lx rdx: %08lx rbx: %08lx\n",
257 context->rax, context->rcx, context->rdx, context->rbx);
258 printf("rsp: %08lx rbp: %08lx rsi: %08lx rdi: %08lx\n",
259 context->rsp, context->rbp, context->rsi, context->rdi);
260 printf("rip: %08lx eflags: %08lx\n",
261 context->rip, context->rflags);
262 printf("cs: %04hx ds: %04hx es: %04hx "
263 "ss: %04hx fs: %04hx gs: %04hx\n",
264 context->cs, context->ds, context->rs,
265 context->ss, context->fs, context->gs);
267 stack_pointer = (u64 *)context->rsp;
268 for (i = 0; i < 48; i+=4) {
269 printf("%08x: %08x %08x %08x %08x\n",
270 context->rsp + (i * 4),
271 stack_pointer[i],
272 stack_pointer[i+1],
273 stack_pointer[i+2],
274 stack_pointer[i+3]);
277 #endif
279 void
280 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
281 os_context_t *context) {
282 unblock_signals_in_context_and_maybe_warn(context);
283 arrange_return_to_lisp_function
284 (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
287 void
288 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
289 arrange_return_to_lisp_function
290 (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
293 kern_return_t
294 catch_exception_raise(mach_port_t exception_port,
295 mach_port_t thread,
296 mach_port_t task,
297 exception_type_t exception,
298 exception_data_t code_vector,
299 mach_msg_type_number_t code_count)
301 kern_return_t ret;
302 int signal;
303 siginfo_t* siginfo;
305 #ifdef LISP_FEATURE_SB_THREAD
306 thread_mutex_lock(&mach_exception_lock);
307 #endif
309 x86_thread_state64_t thread_state;
310 mach_msg_type_number_t thread_state_count = x86_THREAD_STATE64_COUNT;
312 x86_float_state64_t float_state;
313 mach_msg_type_number_t float_state_count = x86_FLOAT_STATE64_COUNT;
315 x86_exception_state64_t exception_state;
316 mach_msg_type_number_t exception_state_count = x86_EXCEPTION_STATE64_COUNT;
318 x86_thread_state64_t backup_thread_state;
319 x86_thread_state64_t *target_thread_state;
320 x86_float_state64_t *target_float_state;
322 os_vm_address_t addr;
324 struct thread *th = (struct thread*) exception_port;
326 FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
328 switch (exception) {
330 case EXC_BAD_ACCESS:
331 signal = SIGBUS;
332 ret = thread_get_state(thread,
333 x86_THREAD_STATE64,
334 (thread_state_t)&thread_state,
335 &thread_state_count);
336 ret = thread_get_state(thread,
337 x86_FLOAT_STATE64,
338 (thread_state_t)&float_state,
339 &float_state_count);
340 ret = thread_get_state(thread,
341 x86_EXCEPTION_STATE64,
342 (thread_state_t)&exception_state,
343 &exception_state_count);
344 addr = (void*)exception_state.faultvaddr;
347 /* note the os_context hackery here. When the signal handler returns,
348 * it won't go back to what it was doing ... */
349 if(addr >= CONTROL_STACK_GUARD_PAGE(th) &&
350 addr < CONTROL_STACK_GUARD_PAGE(th) + os_vm_page_size) {
351 /* We hit the end of the control stack: disable guard page
352 * protection so the error handler has some headroom, protect the
353 * previous page so that we can catch returns from the guard page
354 * and restore it. */
355 lower_thread_control_stack_guard_page(th);
357 backup_thread_state = thread_state;
358 open_stack_allocation(&thread_state);
359 /* Reserve a 256 byte zone for signal handlers
360 * to use on the interrupted thread stack.
362 stack_allocate(&thread_state, 256);
364 /* Save thread state */
365 target_thread_state =
366 stack_allocate(&thread_state, sizeof(*target_thread_state));
367 (*target_thread_state) = backup_thread_state;
369 /* Save float state */
370 target_float_state =
371 stack_allocate(&thread_state, sizeof(*target_float_state));
372 (*target_float_state) = float_state;
374 /* Set up siginfo */
375 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
376 /* what do we need to put in our fake siginfo? It looks like
377 * the x86 code only uses si_signo and si_adrr. */
378 siginfo->si_signo = signal;
379 siginfo->si_addr = (void*)exception_state.faultvaddr;
381 call_c_function_in_context(&thread_state,
382 signal_emulation_wrapper,
384 target_thread_state,
385 target_float_state,
386 signal,
387 siginfo,
388 control_stack_exhausted_handler);
390 else if(addr >= CONTROL_STACK_RETURN_GUARD_PAGE(th) &&
391 addr < CONTROL_STACK_RETURN_GUARD_PAGE(th) + os_vm_page_size) {
392 /* We're returning from the guard page: reprotect it, and
393 * unprotect this one. This works even if we somehow missed
394 * the return-guard-page, and hit it on our way to new
395 * exhaustion instead. */
396 reset_thread_control_stack_guard_page(th);
398 else if (addr >= undefined_alien_address &&
399 addr < undefined_alien_address + os_vm_page_size) {
400 backup_thread_state = thread_state;
401 open_stack_allocation(&thread_state);
402 stack_allocate(&thread_state, 256);
404 /* Save thread state */
405 target_thread_state =
406 stack_allocate(&thread_state, sizeof(*target_thread_state));
407 (*target_thread_state) = backup_thread_state;
409 target_float_state =
410 stack_allocate(&thread_state, sizeof(*target_float_state));
411 (*target_float_state) = float_state;
413 /* Set up siginfo */
414 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
415 /* what do we need to put in our fake siginfo? It looks like
416 * the x86 code only uses si_signo and si_adrr. */
417 siginfo->si_signo = signal;
418 siginfo->si_addr = (void*)exception_state.faultvaddr;
420 call_c_function_in_context(&thread_state,
421 signal_emulation_wrapper,
423 target_thread_state,
424 target_float_state,
425 signal,
426 siginfo,
427 undefined_alien_handler);
428 } else {
430 backup_thread_state = thread_state;
431 open_stack_allocation(&thread_state);
432 stack_allocate(&thread_state, 256);
434 /* Save thread state */
435 target_thread_state =
436 stack_allocate(&thread_state, sizeof(*target_thread_state));
437 (*target_thread_state) = backup_thread_state;
439 target_float_state =
440 stack_allocate(&thread_state, sizeof(*target_float_state));
441 (*target_float_state) = float_state;
443 /* Set up siginfo */
444 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
445 /* what do we need to put in our fake siginfo? It looks like
446 * the x86 code only uses si_signo and si_adrr. */
447 siginfo->si_signo = signal;
448 siginfo->si_addr = (void*)exception_state.faultvaddr;
450 call_c_function_in_context(&thread_state,
451 signal_emulation_wrapper,
453 target_thread_state,
454 target_float_state,
455 signal,
456 siginfo,
457 memory_fault_handler);
459 ret = thread_set_state(thread,
460 x86_THREAD_STATE64,
461 (thread_state_t)&thread_state,
462 thread_state_count);
464 ret = thread_set_state(thread,
465 x86_FLOAT_STATE64,
466 (thread_state_t)&float_state,
467 float_state_count);
468 #ifdef LISP_FEATURE_SB_THREAD
469 thread_mutex_unlock(&mach_exception_lock);
470 #endif
471 return KERN_SUCCESS;
473 case EXC_BAD_INSTRUCTION:
475 ret = thread_get_state(thread,
476 x86_THREAD_STATE64,
477 (thread_state_t)&thread_state,
478 &thread_state_count);
479 ret = thread_get_state(thread,
480 x86_FLOAT_STATE64,
481 (thread_state_t)&float_state,
482 &float_state_count);
483 ret = thread_get_state(thread,
484 x86_EXCEPTION_STATE64,
485 (thread_state_t)&exception_state,
486 &exception_state_count);
487 if (0xffffffffffff0b0f == *((u64 *)thread_state.rip)) {
488 /* fake sigreturn. */
490 /* When we get here, thread_state.rax is a pointer to a
491 * thread_state to restore. */
492 /* thread_state = *((thread_state_t *)thread_state.rax); */
494 ret = thread_set_state(thread,
495 x86_THREAD_STATE64,
496 (thread_state_t) thread_state.rax,
497 /* &thread_state, */
498 thread_state_count);
500 ret = thread_set_state(thread,
501 x86_FLOAT_STATE64,
502 (thread_state_t) thread_state.rbx,
503 /* &thread_state, */
504 float_state_count);
505 } else {
507 backup_thread_state = thread_state;
508 open_stack_allocation(&thread_state);
509 stack_allocate(&thread_state, 256);
511 /* Save thread state */
512 target_thread_state =
513 stack_allocate(&thread_state, sizeof(*target_thread_state));
514 (*target_thread_state) = backup_thread_state;
516 target_float_state =
517 stack_allocate(&thread_state, sizeof(*target_float_state));
518 (*target_float_state) = float_state;
520 /* Set up siginfo */
521 siginfo = stack_allocate(&thread_state, sizeof(*siginfo));
522 /* what do we need to put in our fake siginfo? It looks like
523 * the x86 code only uses si_signo and si_adrr. */
524 if (*((unsigned short *)target_thread_state->rip) == 0x0b0f) {
525 signal = SIGTRAP;
526 siginfo->si_signo = signal;
527 siginfo->si_addr = (void*)exception_state.faultvaddr;
528 target_thread_state->rip += 2;
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 sigtrap_handler);
537 } else {
538 signal = SIGILL;
539 siginfo->si_signo = signal;
540 siginfo->si_addr = (void*)exception_state.faultvaddr;
542 call_c_function_in_context(&thread_state,
543 signal_emulation_wrapper,
545 target_thread_state,
546 target_float_state,
547 signal,
548 siginfo,
549 sigill_handler);
551 ret = thread_set_state(thread,
552 x86_THREAD_STATE64,
553 (thread_state_t)&thread_state,
554 thread_state_count);
555 ret = thread_set_state(thread,
556 x86_FLOAT_STATE64,
557 (thread_state_t)&float_state,
558 float_state_count);
560 #ifdef LISP_FEATURE_SB_THREAD
561 thread_mutex_unlock(&mach_exception_lock);
562 #endif
563 return KERN_SUCCESS;
565 default:
566 #ifdef LISP_FEATURE_SB_THREAD
567 thread_mutex_unlock(&mach_exception_lock);
568 #endif
569 return KERN_INVALID_RIGHT;
573 void *
574 mach_exception_handler(void *port)
576 mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
577 /* mach_msg_server should never return, but it should dispatch mach
578 * exceptions to our catch_exception_raise function
580 lose("mach_msg_server returned");
583 /* Sets up the thread that will listen for mach exceptions. note that
584 the exception handlers will be run on this thread. This is
585 different from the BSD-style signal handling situation in which the
586 signal handlers run in the relevant thread directly. */
588 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
590 pthread_t
591 setup_mach_exception_handling_thread()
593 kern_return_t ret;
594 pthread_t mach_exception_handling_thread = NULL;
595 pthread_attr_t attr;
597 /* allocate a mach_port for this process */
598 ret = mach_port_allocate(mach_task_self(),
599 MACH_PORT_RIGHT_PORT_SET,
600 &mach_exception_handler_port_set);
602 /* create the thread that will receive the mach exceptions */
604 FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
606 pthread_attr_init(&attr);
607 pthread_create(&mach_exception_handling_thread,
608 &attr,
609 mach_exception_handler,
610 (void*) mach_exception_handler_port_set);
611 pthread_attr_destroy(&attr);
613 return mach_exception_handling_thread;
616 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
617 exception port (which is being listened to do by the mach
618 exception handling thread). */
619 kern_return_t
620 mach_thread_init(mach_port_t thread_exception_port)
622 kern_return_t ret;
623 /* allocate a named port for the thread */
625 FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
627 ret = mach_port_allocate_name(mach_task_self(),
628 MACH_PORT_RIGHT_RECEIVE,
629 thread_exception_port);
630 if (ret) {
631 lose("mach_port_allocate_name failed with return_code %d\n", ret);
634 /* establish the right for the thread_exception_port to send messages */
635 ret = mach_port_insert_right(mach_task_self(),
636 thread_exception_port,
637 thread_exception_port,
638 MACH_MSG_TYPE_MAKE_SEND);
639 if (ret) {
640 lose("mach_port_insert_right failed with return_code %d\n", ret);
643 ret = thread_set_exception_ports(mach_thread_self(),
644 EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
645 thread_exception_port,
646 EXCEPTION_DEFAULT,
647 THREAD_STATE_NONE);
648 if (ret) {
649 lose("thread_set_exception_port failed with return_code %d\n", ret);
652 ret = mach_port_move_member(mach_task_self(),
653 thread_exception_port,
654 mach_exception_handler_port_set);
655 if (ret) {
656 lose("mach_port_ failed with return_code %d\n", ret);
659 return ret;
662 void
663 setup_mach_exceptions() {
664 setup_mach_exception_handling_thread();
665 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
668 pid_t
669 mach_fork() {
670 pid_t pid = fork();
671 if (pid == 0) {
672 setup_mach_exceptions();
673 return pid;
674 } else {
675 return pid;
679 #endif