1.0.12.19: runtime cleanups by Daniel Lowe
[sbcl/simd.git] / src / runtime / x86-darwin-os.c
blob248e782dc0307492df158d0ff7dedfd3194b4621
3 #ifdef LISP_FEATURE_SB_THREAD
4 #include <architecture/i386/table.h>
5 #include <i386/user_ldt.h>
6 #include <mach/mach_init.h>
7 #endif
9 #include "thread.h"
10 #include "validate.h"
11 #include "runtime.h"
12 #include "interrupt.h"
13 #include "x86-darwin-os.h"
14 #include "genesis/fdefn.h"
16 #include <mach/mach.h>
17 #include <mach/mach_error.h>
18 #include <mach/mach_types.h>
19 #include <mach/sync_policy.h>
20 #include <mach/vm_region.h>
21 #include <mach/machine/thread_state.h>
22 #include <mach/machine/thread_status.h>
23 #include <sys/_types.h>
24 #include <sys/ucontext.h>
25 #include <pthread.h>
26 #include <assert.h>
27 #include <stdlib.h>
29 #ifdef LISP_FEATURE_SB_THREAD
31 pthread_mutex_t modify_ldt_lock = PTHREAD_MUTEX_INITIALIZER;
33 void set_data_desc_size(data_desc_t* desc, unsigned long size)
35 desc->limit00 = (size - 1) & 0xffff;
36 desc->limit16 = ((size - 1) >> 16) &0xf;
39 void set_data_desc_addr(data_desc_t* desc, void* addr)
41 desc->base00 = (unsigned int)addr & 0xffff;
42 desc->base16 = ((unsigned int)addr & 0xff0000) >> 16;
43 desc->base24 = ((unsigned int)addr & 0xff000000) >> 24;
46 #endif
48 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
49 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
50 #endif
52 int arch_os_thread_init(struct thread *thread) {
53 #ifdef LISP_FEATURE_SB_THREAD
54 int n;
55 sel_t sel;
57 data_desc_t ldt_entry = { 0, 0, 0, DESC_DATA_WRITE,
58 3, 1, 0, DESC_DATA_32B, DESC_GRAN_BYTE, 0 };
60 set_data_desc_addr(&ldt_entry, thread);
61 set_data_desc_size(&ldt_entry, dynamic_values_bytes);
63 thread_mutex_lock(&modify_ldt_lock);
64 n = i386_set_ldt(LDT_AUTO_ALLOC, (union ldt_entry*) &ldt_entry, 1);
66 if (n < 0) {
67 perror("i386_set_ldt");
68 lose("unexpected i386_set_ldt(..) failure\n");
70 thread_mutex_unlock(&modify_ldt_lock);
72 FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
73 sel.index = n;
74 sel.rpl = USER_PRIV;
75 sel.ti = SEL_LDT;
77 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
79 thread->tls_cookie=n;
80 pthread_setspecific(specials,thread);
81 #endif
82 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
83 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread));
84 #endif
86 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
87 stack_t sigstack;
89 /* Signal handlers are run on the control stack, so if it is exhausted
90 * we had better use an alternate stack for whatever signal tells us
91 * we've exhausted it */
92 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
93 sigstack.ss_flags=0;
94 sigstack.ss_size = 32*SIGSTKSZ;
95 sigaltstack(&sigstack,0);
96 #endif
97 return 1; /* success */
100 int arch_os_thread_cleanup(struct thread *thread) {
101 #if defined(LISP_FEATURE_SB_THREAD)
102 int n = thread->tls_cookie;
104 /* Set the %%fs register back to 0 and free the ldt by setting it
105 * to NULL.
107 FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
109 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
110 thread_mutex_lock(&modify_ldt_lock);
111 i386_set_ldt(n, NULL, 1);
112 thread_mutex_unlock(&modify_ldt_lock);
113 #endif
114 return 1; /* success */
117 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
119 void sigill_handler(int signal, siginfo_t *siginfo, void *void_context);
120 void sigtrap_handler(int signal, siginfo_t *siginfo, void *void_context);
121 void memory_fault_handler(int signal, siginfo_t *siginfo, void *void_context);
123 /* exc_server handles mach exception messages from the kernel and
124 * calls catch exception raise. We use the system-provided
125 * mach_msg_server, which, I assume, calls exc_server in a loop.
128 extern boolean_t exc_server();
130 /* This executes in the faulting thread as part of the signal
131 * emulation. It is passed a context with the uc_mcontext field
132 * pointing to a valid block of memory. */
133 void build_fake_signal_context(os_context_t *context,
134 x86_thread_state32_t *thread_state,
135 x86_float_state32_t *float_state) {
136 pthread_sigmask(0, NULL, &context->uc_sigmask);
137 context->uc_mcontext->SS = *thread_state;
138 context->uc_mcontext->FS = *float_state;
141 /* This executes in the faulting thread as part of the signal
142 * emulation. It is effectively the inverse operation from above. */
143 void update_thread_state_from_context(x86_thread_state32_t *thread_state,
144 x86_float_state32_t *float_state,
145 os_context_t *context) {
146 *thread_state = context->uc_mcontext->SS;
147 *float_state = context->uc_mcontext->FS;
148 pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
151 /* Modify a context to push new data on its stack. */
152 void push_context(u32 data, x86_thread_state32_t *thread_state)
154 u32 *stack_pointer;
156 stack_pointer = (u32*) thread_state->ESP;
157 *(--stack_pointer) = data;
158 thread_state->ESP = (unsigned int) stack_pointer;
161 void align_context_stack(x86_thread_state32_t *thread_state)
163 /* 16byte align the stack (provided that the stack is, as it
164 * should be, 4byte aligned. */
165 while (thread_state->ESP & 15) push_context(0, thread_state);
168 /* Stack allocation starts with a context that has a mod-4 ESP value
169 * and needs to leave a context with a mod-16 ESP that will restore
170 * the old ESP value and other register state when activated. The
171 * first part of this is the recovery trampoline, which loads ESP from
172 * EBP, pops EBP, and returns. */
173 asm("_stack_allocation_recover: movl %ebp, %esp; popl %ebp; ret;");
175 void open_stack_allocation(x86_thread_state32_t *thread_state)
177 void stack_allocation_recover(void);
179 push_context(thread_state->EIP, thread_state);
180 push_context(thread_state->EBP, thread_state);
181 thread_state->EBP = thread_state->ESP;
182 thread_state->EIP = (unsigned int) stack_allocation_recover;
184 align_context_stack(thread_state);
187 /* Stack allocation of data starts with a context with a mod-16 ESP
188 * value and reserves some space on it by manipulating the ESP
189 * register. */
190 void *stack_allocate(x86_thread_state32_t *thread_state, size_t size)
192 /* round up size to 16byte multiple */
193 size = (size + 15) & -16;
195 thread_state->ESP = ((u32)thread_state->ESP) - size;
197 return (void *)thread_state->ESP;
200 /* Arranging to invoke a C function is tricky, as we have to assume
201 * cdecl calling conventions (caller removes args) and x86/darwin
202 * alignment requirements. The simplest way to arrange this,
203 * actually, is to open a new stack allocation.
204 * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
205 void call_c_function_in_context(x86_thread_state32_t *thread_state,
206 void *function,
207 int nargs,
208 ...)
210 va_list ap;
211 int i;
212 u32 *stack_pointer;
214 /* Set up to restore stack on exit. */
215 open_stack_allocation(thread_state);
217 /* Have to keep stack 16byte aligned on x86/darwin. */
218 for (i = (3 & -nargs); i; i--) {
219 push_context(0, thread_state);
222 thread_state->ESP = ((u32)thread_state->ESP) - nargs * 4;
223 stack_pointer = (u32 *)thread_state->ESP;
225 va_start(ap, nargs);
226 for (i = 0; i < nargs; i++) {
227 //push_context(va_arg(ap, u32), thread_state);
228 stack_pointer[i] = va_arg(ap, u32);
230 va_end(ap);
232 push_context(thread_state->EIP, thread_state);
233 thread_state->EIP = (unsigned int) function;
236 void signal_emulation_wrapper(x86_thread_state32_t *thread_state,
237 x86_float_state32_t *float_state,
238 int signal,
239 siginfo_t *siginfo,
240 void (*handler)(int, siginfo_t *, void *))
243 /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
244 * context and regs on the stack as local variables, but this
245 * causes problems for the lisp debugger. When it walks the stack
246 * for a back trace, it sees the 1) address of the local variable
247 * on the stack and thinks that is a frame pointer to a lisp
248 * frame, and, 2) the address of the sap that we alloc'ed in
249 * dynamic space and thinks that is a return address, so it,
250 * heuristicly (and wrongly), chooses that this should be
251 * interpreted as a lisp frame instead of as a C frame.
252 * We can work around this in this case by os_validating the
253 * context (and regs just for symmetry).
256 os_context_t *context;
257 #if MAC_OS_X_VERSION_10_5
258 struct __darwin_mcontext32 *regs;
259 #else
260 struct mcontext *regs;
261 #endif
263 context = (os_context_t*) os_validate(0, sizeof(os_context_t));
264 #if MAC_OS_X_VERSION_10_5
265 regs = (struct __darwin_mcontext32*) os_validate(0, sizeof(struct __darwin_mcontext32));
266 #else
267 regs = (struct mcontext*) os_validate(0, sizeof(struct mcontext));
268 #endif
269 context->uc_mcontext = regs;
271 /* when BSD signals are fired, they mask they signals in sa_mask
272 which always seem to be the blockable_sigset, for us, so we
273 need to:
274 1) save the current sigmask
275 2) block blockable signals
276 3) call the signal handler
277 4) restore the sigmask */
279 build_fake_signal_context(context, thread_state, float_state);
281 block_blockable_signals();
283 handler(signal, siginfo, context);
285 update_thread_state_from_context(thread_state, float_state, context);
287 os_invalidate((os_vm_address_t)context, sizeof(os_context_t));
288 #if MAC_OS_X_VERSION_10_5
289 os_invalidate((os_vm_address_t)regs, sizeof(struct __darwin_mcontext32));
290 #else
291 os_invalidate((os_vm_address_t)regs, sizeof(struct mcontext));
292 #endif
294 /* Trap to restore the signal context. */
295 asm volatile ("movl %0, %%eax; movl %1, %%ebx; .long 0xffff0b0f"
296 : : "r" (thread_state), "r" (float_state));
299 /* Convenience wrapper for the above */
300 void call_handler_on_thread(mach_port_t thread,
301 x86_thread_state32_t *thread_state,
302 int signal,
303 siginfo_t *siginfo,
304 void (*handler)(int, siginfo_t *, void *))
306 x86_thread_state32_t new_state;
307 x86_thread_state32_t *save_thread_state;
308 x86_float_state32_t *save_float_state;
309 mach_msg_type_number_t state_count;
310 siginfo_t *save_siginfo;
311 kern_return_t ret;
312 /* Initialize the new state */
313 new_state = *thread_state;
314 open_stack_allocation(&new_state);
315 /* Save old state */
316 save_thread_state = (x86_thread_state32_t *)stack_allocate(&new_state, sizeof(*save_thread_state));
317 *save_thread_state = *thread_state;
318 /* Save float state */
319 save_float_state = (x86_float_state32_t *)stack_allocate(&new_state, sizeof(*save_float_state));
320 state_count = x86_FLOAT_STATE32_COUNT;
321 if ((ret = thread_get_state(thread,
322 x86_FLOAT_STATE32,
323 (thread_state_t)save_float_state,
324 &state_count)) != KERN_SUCCESS)
325 lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
326 /* Set up siginfo */
327 save_siginfo = stack_allocate(&new_state, sizeof(*siginfo));
328 if (siginfo == NULL)
329 save_siginfo = siginfo;
330 else
331 *save_siginfo = *siginfo;
332 /* Prepare to call */
333 call_c_function_in_context(&new_state,
334 signal_emulation_wrapper,
336 save_thread_state,
337 save_float_state,
338 signal,
339 save_siginfo,
340 handler);
341 /* Update the thread state */
342 state_count = x86_THREAD_STATE32_COUNT;
343 if ((ret = thread_set_state(thread,
344 x86_THREAD_STATE32,
345 (thread_state_t)&new_state,
346 state_count)) != KERN_SUCCESS)
347 lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
351 #if defined DUMP_CONTEXT
352 void dump_context(x86_thread_state32_t *thread_state)
354 int i;
355 u32 *stack_pointer;
357 printf("eax: %08lx ecx: %08lx edx: %08lx ebx: %08lx\n",
358 thread_state->EAX, thread_state->ECX, thread_state->EDX, thread_state->EAX);
359 printf("esp: %08lx ebp: %08lx esi: %08lx edi: %08lx\n",
360 thread_state->ESP, thread_state->EBP, thread_state->ESI, thread_state->EDI);
361 printf("eip: %08lx eflags: %08lx\n",
362 thread_state->EIP, thread_state->EFLAGS);
363 printf("cs: %04hx ds: %04hx es: %04hx "
364 "ss: %04hx fs: %04hx gs: %04hx\n",
365 thread_state->CS,
366 thread_state->DS,
367 thread_state->ES,
368 thread_state->SS,
369 thread_state->FS,
370 thread_state->GS);
372 stack_pointer = (u32 *)thread_state->ESP;
373 for (i = 0; i < 48; i+=4) {
374 printf("%08x: %08x %08x %08x %08x\n",
375 thread_state->ESP + (i * 4),
376 stack_pointer[i],
377 stack_pointer[i+1],
378 stack_pointer[i+2],
379 stack_pointer[i+3]);
382 #endif
384 void
385 control_stack_exhausted_handler(int signal, siginfo_t *siginfo, void *void_context) {
386 os_context_t *context = arch_os_get_context(&void_context);
388 arrange_return_to_lisp_function
389 (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
392 void
393 undefined_alien_handler(int signal, siginfo_t *siginfo, void *void_context) {
394 os_context_t *context = arch_os_get_context(&void_context);
396 arrange_return_to_lisp_function
397 (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
400 kern_return_t
401 catch_exception_raise(mach_port_t exception_port,
402 mach_port_t thread,
403 mach_port_t task,
404 exception_type_t exception,
405 exception_data_t code_vector,
406 mach_msg_type_number_t code_count)
408 struct thread *th = (struct thread*) exception_port;
409 x86_thread_state32_t thread_state;
410 mach_msg_type_number_t state_count;
411 vm_address_t region_addr;
412 vm_size_t region_size;
413 vm_region_basic_info_data_t region_info;
414 mach_msg_type_number_t info_count;
415 mach_port_t region_name;
416 void *addr = NULL;
417 int signal = 0;
418 void (*handler)(int, siginfo_t *, void *) = NULL;
419 siginfo_t siginfo;
420 kern_return_t ret;
422 /* Get state and info */
423 state_count = x86_THREAD_STATE32_COUNT;
424 if ((ret = thread_get_state(thread,
425 x86_THREAD_STATE32,
426 (thread_state_t)&thread_state,
427 &state_count)) != KERN_SUCCESS)
428 lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
429 switch (exception) {
430 case EXC_BAD_ACCESS:
431 signal = SIGBUS;
432 /* Check if write protection fault */
433 if ((code_vector[0] & OS_VM_PROT_ALL) == 0) {
434 ret = KERN_INVALID_RIGHT;
435 break;
437 addr = (void*)code_vector[1];
438 /* Undefined alien */
439 if (os_trunc_to_page(addr) == undefined_alien_address) {
440 handler = undefined_alien_handler;
441 break;
443 /* At stack guard */
444 if (os_trunc_to_page(addr) == CONTROL_STACK_GUARD_PAGE(th)) {
445 protect_control_stack_guard_page_thread(0, th);
446 protect_control_stack_return_guard_page_thread(1, th);
447 handler = control_stack_exhausted_handler;
448 break;
450 /* Return from stack guard */
451 if (os_trunc_to_page(addr) == CONTROL_STACK_RETURN_GUARD_PAGE(th)) {
452 protect_control_stack_guard_page_thread(1, th);
453 protect_control_stack_return_guard_page_thread(0, th);
454 break;
456 /* Get vm_region info */
457 region_addr = (vm_address_t)code_vector[1];
458 info_count = VM_REGION_BASIC_INFO_COUNT;
459 if ((ret = vm_region(mach_task_self(),
460 &region_addr,
461 &region_size,
462 VM_REGION_BASIC_INFO,
463 (vm_region_info_t)&region_info,
464 &info_count,
465 &region_name)))
466 lose("vm_region (VM_REGION_BASIC_INFO) failed failed %d\n", ret);
467 /* Check if still protected */
468 if ((region_info.protection & OS_VM_PROT_ALL) == 0) {
469 /* KLUDGE:
470 * If two threads fault on the same page, the protection
471 * is cleared as the first thread runs memory_fault_handler.
472 * Grep for "not marked as write-protected" in gencgc.c
474 ret = KERN_SUCCESS;
475 break;
477 /* Regular memory fault */
478 handler = memory_fault_handler;
479 break;
480 case EXC_BAD_INSTRUCTION:
481 signal = SIGTRAP;
482 /* Check if illegal instruction trap */
483 if (code_vector[0] != EXC_I386_INVOP) {
484 ret = KERN_INVALID_RIGHT;
485 break;
487 /* Check if UD2 instruction */
488 if (*(unsigned short *)thread_state.EIP != 0x0b0f) {
489 /* KLUDGE: There are two ways we could get here:
490 * 1) We're executing data and we've hit some truly
491 * illegal opcode, of which there are a few, see
492 * Intel 64 and IA-32 Architectures
493 * Sofware Developer's Manual
494 * Volume 3A page 5-34)
495 * 2) The kernel started an unrelated signal handler
496 * before we got a chance to run. The context that
497 * caused the exception is saved in a stack frame
498 * somewhere down below.
499 * In either case we rely on the exception to retrigger,
500 * eventually bailing out if we're spinning on case 2).
502 static mach_port_t last_thread;
503 static unsigned int last_eip;
504 if (last_thread == thread && last_eip == thread_state.EIP)
505 ret = KERN_INVALID_RIGHT;
506 else
507 ret = KERN_SUCCESS;
508 last_thread = thread;
509 last_eip = thread_state.EIP;
510 break;
512 /* Skip the trap code */
513 thread_state.EIP += 2;
514 /* Return from handler? */
515 if (*(unsigned short *)thread_state.EIP == 0xffff) {
516 if ((ret = thread_set_state(thread,
517 x86_THREAD_STATE32,
518 (thread_state_t)thread_state.EAX,
519 x86_THREAD_STATE32_COUNT)) != KERN_SUCCESS)
520 lose("thread_set_state (x86_THREAD_STATE32) failed %d\n", ret);
521 if ((ret = thread_set_state(thread,
522 x86_FLOAT_STATE32,
523 (thread_state_t)thread_state.EBX,
524 x86_FLOAT_STATE32_COUNT)) != KERN_SUCCESS)
525 lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
526 break;
528 /* Trap call */
529 handler = sigtrap_handler;
530 break;
531 default:
532 ret = KERN_INVALID_RIGHT;
534 /* Call handler */
535 if (handler != 0) {
536 siginfo.si_signo = signal;
537 siginfo.si_addr = addr;
538 call_handler_on_thread(thread, &thread_state, signal, &siginfo, handler);
540 return ret;
543 void *
544 mach_exception_handler(void *port)
546 mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
547 /* mach_msg_server should never return, but it should dispatch mach
548 * exceptions to our catch_exception_raise function
550 abort();
553 #endif
555 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
557 /* Sets up the thread that will listen for mach exceptions. note that
558 the exception handlers will be run on this thread. This is
559 different from the BSD-style signal handling situation in which the
560 signal handlers run in the relevant thread directly. */
562 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
564 pthread_t
565 setup_mach_exception_handling_thread()
567 kern_return_t ret;
568 pthread_t mach_exception_handling_thread = NULL;
569 pthread_attr_t attr;
571 /* allocate a mach_port for this process */
572 ret = mach_port_allocate(mach_task_self(),
573 MACH_PORT_RIGHT_PORT_SET,
574 &mach_exception_handler_port_set);
576 /* create the thread that will receive the mach exceptions */
578 FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
580 pthread_attr_init(&attr);
581 pthread_create(&mach_exception_handling_thread,
582 &attr,
583 mach_exception_handler,
584 (void*) mach_exception_handler_port_set);
585 pthread_attr_destroy(&attr);
587 return mach_exception_handling_thread;
590 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
591 exception port (which is being listened to do by the mach
592 exception handling thread). */
593 kern_return_t
594 mach_thread_init(mach_port_t thread_exception_port)
596 kern_return_t ret;
597 /* allocate a named port for the thread */
599 FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
601 ret = mach_port_allocate_name(mach_task_self(),
602 MACH_PORT_RIGHT_RECEIVE,
603 thread_exception_port);
604 if (ret) {
605 lose("mach_port_allocate_name failed with return_code %d\n", ret);
608 /* establish the right for the thread_exception_port to send messages */
609 ret = mach_port_insert_right(mach_task_self(),
610 thread_exception_port,
611 thread_exception_port,
612 MACH_MSG_TYPE_MAKE_SEND);
613 if (ret) {
614 lose("mach_port_insert_right failed with return_code %d\n", ret);
617 ret = thread_set_exception_ports(mach_thread_self(),
618 EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
619 thread_exception_port,
620 EXCEPTION_DEFAULT,
621 THREAD_STATE_NONE);
622 if (ret) {
623 lose("thread_set_exception_port failed with return_code %d\n", ret);
626 ret = mach_port_move_member(mach_task_self(),
627 thread_exception_port,
628 mach_exception_handler_port_set);
629 if (ret) {
630 lose("mach_port_ failed with return_code %d\n", ret);
633 return ret;
636 void
637 setup_mach_exceptions() {
638 setup_mach_exception_handling_thread();
639 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
642 pid_t
643 mach_fork() {
644 pid_t pid = fork();
645 if (pid == 0) {
646 setup_mach_exceptions();
647 return pid;
648 } else {
649 return pid;
653 #endif