2c2b9785c70770d8dcfaed3b7a69220dda0e520e
[sbcl.git] / src / runtime / x86-darwin-os.c
1
2
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
8
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"
15
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>
28
29 #ifdef LISP_FEATURE_SB_THREAD
30
31 pthread_mutex_t modify_ldt_lock = PTHREAD_MUTEX_INITIALIZER;
32
33 void set_data_desc_size(data_desc_t* desc, unsigned long size)
34 {
35     desc->limit00 = (size - 1) & 0xffff;
36     desc->limit16 = ((size - 1) >> 16) &0xf;
37 }
38
39 void set_data_desc_addr(data_desc_t* desc, void* addr)
40 {
41     desc->base00 = (unsigned int)addr & 0xffff;
42     desc->base16 = ((unsigned int)addr & 0xff0000) >> 16;
43     desc->base24 = ((unsigned int)addr & 0xff000000) >> 24;
44 }
45
46 #endif
47
48 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
49 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
50 #endif
51
52 int arch_os_thread_init(struct thread *thread) {
53 #ifdef LISP_FEATURE_SB_THREAD
54     int n;
55     sel_t sel;
56
57     data_desc_t ldt_entry = { 0, 0, 0, DESC_DATA_WRITE,
58                               3, 1, 0, DESC_DATA_32B, DESC_GRAN_BYTE, 0 };
59
60     set_data_desc_addr(&ldt_entry, thread);
61     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
62
63     thread_mutex_lock(&modify_ldt_lock);
64     n = i386_set_ldt(LDT_AUTO_ALLOC, (union ldt_entry*) &ldt_entry, 1);
65
66     if (n < 0) {
67         perror("i386_set_ldt");
68         lose("unexpected i386_set_ldt(..) failure\n");
69     }
70     thread_mutex_unlock(&modify_ldt_lock);
71
72     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
73     sel.index = n;
74     sel.rpl = USER_PRIV;
75     sel.ti = SEL_LDT;
76
77     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
78
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
85
86 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
87     stack_t sigstack;
88
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 */
98 }
99
100 int arch_os_thread_cleanup(struct thread *thread) {
101 #if defined(LISP_FEATURE_SB_THREAD)
102     int n = thread->tls_cookie;
103
104     /* Set the %%fs register back to 0 and free the ldt by setting it
105      * to NULL.
106      */
107     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
108
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 */
115 }
116
117 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
118
119 void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context);
120 void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context);
121 void memory_fault_handler(int signal, siginfo_t *siginfo,
122                           os_context_t *context);
123
124 /* exc_server handles mach exception messages from the kernel and
125  * calls catch exception raise. We use the system-provided
126  * mach_msg_server, which, I assume, calls exc_server in a loop.
127  *
128  */
129 extern boolean_t exc_server();
130
131 /* This executes in the faulting thread as part of the signal
132  * emulation.  It is passed a context with the uc_mcontext field
133  * pointing to a valid block of memory. */
134 void build_fake_signal_context(os_context_t *context,
135                                x86_thread_state32_t *thread_state,
136                                x86_float_state32_t *float_state) {
137     pthread_sigmask(0, NULL, &context->uc_sigmask);
138     context->uc_mcontext->SS = *thread_state;
139     context->uc_mcontext->FS = *float_state;
140 }
141
142 /* This executes in the faulting thread as part of the signal
143  * emulation.  It is effectively the inverse operation from above. */
144 void update_thread_state_from_context(x86_thread_state32_t *thread_state,
145                                       x86_float_state32_t *float_state,
146                                       os_context_t *context) {
147     *thread_state = context->uc_mcontext->SS;
148     *float_state = context->uc_mcontext->FS;
149     pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
150 }
151
152 /* Modify a context to push new data on its stack. */
153 void push_context(u32 data, x86_thread_state32_t *thread_state)
154 {
155     u32 *stack_pointer;
156
157     stack_pointer = (u32*) thread_state->ESP;
158     *(--stack_pointer) = data;
159     thread_state->ESP = (unsigned int) stack_pointer;
160 }
161
162 void align_context_stack(x86_thread_state32_t *thread_state)
163 {
164     /* 16byte align the stack (provided that the stack is, as it
165      * should be, 4byte aligned. */
166     while (thread_state->ESP & 15) push_context(0, thread_state);
167 }
168
169 /* Stack allocation starts with a context that has a mod-4 ESP value
170  * and needs to leave a context with a mod-16 ESP that will restore
171  * the old ESP value and other register state when activated.  The
172  * first part of this is the recovery trampoline, which loads ESP from
173  * EBP, pops EBP, and returns. */
174 asm("_stack_allocation_recover: movl %ebp, %esp; popl %ebp; ret;");
175
176 void open_stack_allocation(x86_thread_state32_t *thread_state)
177 {
178     void stack_allocation_recover(void);
179
180     push_context(thread_state->EIP, thread_state);
181     push_context(thread_state->EBP, thread_state);
182     thread_state->EBP = thread_state->ESP;
183     thread_state->EIP = (unsigned int) stack_allocation_recover;
184
185     align_context_stack(thread_state);
186 }
187
188 /* Stack allocation of data starts with a context with a mod-16 ESP
189  * value and reserves some space on it by manipulating the ESP
190  * register. */
191 void *stack_allocate(x86_thread_state32_t *thread_state, size_t size)
192 {
193     /* round up size to 16byte multiple */
194     size = (size + 15) & -16;
195
196     thread_state->ESP = ((u32)thread_state->ESP) - size;
197
198     return (void *)thread_state->ESP;
199 }
200
201 /* Arranging to invoke a C function is tricky, as we have to assume
202  * cdecl calling conventions (caller removes args) and x86/darwin
203  * alignment requirements.  The simplest way to arrange this,
204  * actually, is to open a new stack allocation.
205  * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
206 void call_c_function_in_context(x86_thread_state32_t *thread_state,
207                                 void *function,
208                                 int nargs,
209                                 ...)
210 {
211     va_list ap;
212     int i;
213     u32 *stack_pointer;
214
215     /* Set up to restore stack on exit. */
216     open_stack_allocation(thread_state);
217
218     /* Have to keep stack 16byte aligned on x86/darwin. */
219     for (i = (3 & -nargs); i; i--) {
220         push_context(0, thread_state);
221     }
222
223     thread_state->ESP = ((u32)thread_state->ESP) - nargs * 4;
224     stack_pointer = (u32 *)thread_state->ESP;
225
226     va_start(ap, nargs);
227     for (i = 0; i < nargs; i++) {
228         //push_context(va_arg(ap, u32), thread_state);
229         stack_pointer[i] = va_arg(ap, u32);
230     }
231     va_end(ap);
232
233     push_context(thread_state->EIP, thread_state);
234     thread_state->EIP = (unsigned int) function;
235 }
236
237 void signal_emulation_wrapper(x86_thread_state32_t *thread_state,
238                               x86_float_state32_t *float_state,
239                               int signal,
240                               siginfo_t *siginfo,
241                               void (*handler)(int, siginfo_t *, void *))
242 {
243
244     /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
245      * context and regs on the stack as local variables, but this
246      * causes problems for the lisp debugger. When it walks the stack
247      * for a back trace, it sees the 1) address of the local variable
248      * on the stack and thinks that is a frame pointer to a lisp
249      * frame, and, 2) the address of the sap that we alloc'ed in
250      * dynamic space and thinks that is a return address, so it,
251      * heuristicly (and wrongly), chooses that this should be
252      * interpreted as a lisp frame instead of as a C frame.
253      * We can work around this in this case by os_validating the
254      * context (and regs just for symmetry).
255      */
256
257     os_context_t *context;
258     mcontext_t *regs;
259
260     context = (os_context_t*) os_validate(0, sizeof(os_context_t));
261     regs = (mcontext_t*) os_validate(0, sizeof(mcontext_t));
262     context->uc_mcontext = regs;
263
264     /* when BSD signals are fired, they mask they signals in sa_mask
265        which always seem to be the blockable_sigset, for us, so we
266        need to:
267        1) save the current sigmask
268        2) block blockable signals
269        3) call the signal handler
270        4) restore the sigmask */
271
272     build_fake_signal_context(context, thread_state, float_state);
273
274     block_blockable_signals();
275
276     handler(signal, siginfo, context);
277
278     update_thread_state_from_context(thread_state, float_state, context);
279
280     os_invalidate((os_vm_address_t)context, sizeof(os_context_t));
281     os_invalidate((os_vm_address_t)regs, sizeof(mcontext_t));
282
283     /* Trap to restore the signal context. */
284     asm volatile ("movl %0, %%eax; movl %1, %%ebx; .long 0xffff0b0f"
285                   : : "r" (thread_state), "r" (float_state));
286 }
287
288 /* Convenience wrapper for the above */
289 void call_handler_on_thread(mach_port_t thread,
290                             x86_thread_state32_t *thread_state,
291                             int signal,
292                             siginfo_t *siginfo,
293                             void (*handler)(int, siginfo_t *, void *))
294 {
295     x86_thread_state32_t new_state;
296     x86_thread_state32_t *save_thread_state;
297     x86_float_state32_t *save_float_state;
298     mach_msg_type_number_t state_count;
299     siginfo_t *save_siginfo;
300     kern_return_t ret;
301     /* Initialize the new state */
302     new_state = *thread_state;
303     open_stack_allocation(&new_state);
304     stack_allocate(&new_state, 256);
305     /* Save old state */
306     save_thread_state = (x86_thread_state32_t *)stack_allocate(&new_state, sizeof(*save_thread_state));
307     *save_thread_state = *thread_state;
308     /* Save float state */
309     save_float_state = (x86_float_state32_t *)stack_allocate(&new_state, sizeof(*save_float_state));
310     state_count = x86_FLOAT_STATE32_COUNT;
311     if ((ret = thread_get_state(thread,
312                                 x86_FLOAT_STATE32,
313                                 (thread_state_t)save_float_state,
314                                 &state_count)) != KERN_SUCCESS)
315         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
316     /* Set up siginfo */
317     save_siginfo = stack_allocate(&new_state, sizeof(*siginfo));
318     if (siginfo == NULL)
319         save_siginfo = siginfo;
320     else
321         *save_siginfo = *siginfo;
322     /* Prepare to call */
323     call_c_function_in_context(&new_state,
324                                signal_emulation_wrapper,
325                                5,
326                                save_thread_state,
327                                save_float_state,
328                                signal,
329                                save_siginfo,
330                                handler);
331     /* Update the thread state */
332     state_count = x86_THREAD_STATE32_COUNT;
333     if ((ret = thread_set_state(thread,
334                                 x86_THREAD_STATE32,
335                                 (thread_state_t)&new_state,
336                                 state_count)) != KERN_SUCCESS)
337         lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
338
339 }
340
341 #if defined DUMP_CONTEXT
342 void dump_context(x86_thread_state32_t *thread_state)
343 {
344     int i;
345     u32 *stack_pointer;
346
347     printf("eax: %08lx  ecx: %08lx  edx: %08lx  ebx: %08lx\n",
348            thread_state->EAX, thread_state->ECX, thread_state->EDX, thread_state->EAX);
349     printf("esp: %08lx  ebp: %08lx  esi: %08lx  edi: %08lx\n",
350            thread_state->ESP, thread_state->EBP, thread_state->ESI, thread_state->EDI);
351     printf("eip: %08lx  eflags: %08lx\n",
352            thread_state->EIP, thread_state->EFLAGS);
353     printf("cs: %04hx  ds: %04hx  es: %04hx  "
354            "ss: %04hx  fs: %04hx  gs: %04hx\n",
355            thread_state->CS,
356            thread_state->DS,
357            thread_state->ES,
358            thread_state->SS,
359            thread_state->FS,
360            thread_state->GS);
361
362     stack_pointer = (u32 *)thread_state->ESP;
363     for (i = 0; i < 48; i+=4) {
364         printf("%08x:  %08x %08x %08x %08x\n",
365                thread_state->ESP + (i * 4),
366                stack_pointer[i],
367                stack_pointer[i+1],
368                stack_pointer[i+2],
369                stack_pointer[i+3]);
370     }
371 }
372 #endif
373
374 void
375 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
376                                 os_context_t *context) {
377
378     unblock_signals_in_context_and_maybe_warn(context);
379     arrange_return_to_lisp_function
380         (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
381 }
382
383 void
384 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
385     arrange_return_to_lisp_function
386         (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
387 }
388
389 kern_return_t
390 catch_exception_raise(mach_port_t exception_port,
391                       mach_port_t thread,
392                       mach_port_t task,
393                       exception_type_t exception,
394                       exception_data_t code_vector,
395                       mach_msg_type_number_t code_count)
396 {
397     struct thread *th = (struct thread*) exception_port;
398     x86_thread_state32_t thread_state;
399     mach_msg_type_number_t state_count;
400     vm_address_t region_addr;
401     vm_size_t region_size;
402     vm_region_basic_info_data_t region_info;
403     mach_msg_type_number_t info_count;
404     mach_port_t region_name;
405     void *addr = NULL;
406     int signal = 0;
407     void (*handler)(int, siginfo_t *, void *) = NULL;
408     siginfo_t siginfo;
409     kern_return_t ret;
410
411     /* Get state and info */
412     state_count = x86_THREAD_STATE32_COUNT;
413     if ((ret = thread_get_state(thread,
414                                 x86_THREAD_STATE32,
415                                 (thread_state_t)&thread_state,
416                                 &state_count)) != KERN_SUCCESS)
417         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
418     switch (exception) {
419     case EXC_BAD_ACCESS:
420         signal = SIGBUS;
421         /* Check if write protection fault */
422         if ((code_vector[0] & OS_VM_PROT_ALL) == 0) {
423             ret = KERN_INVALID_RIGHT;
424             break;
425         }
426         addr = (void*)code_vector[1];
427         /* Undefined alien */
428         if (os_trunc_to_page(addr) == undefined_alien_address) {
429             handler = undefined_alien_handler;
430             break;
431         }
432         /* At stack guard */
433         if (os_trunc_to_page(addr) == CONTROL_STACK_GUARD_PAGE(th)) {
434             protect_control_stack_guard_page(0, th);
435             protect_control_stack_return_guard_page(1, th);
436             handler = control_stack_exhausted_handler;
437             break;
438         }
439         /* Return from stack guard */
440         if (os_trunc_to_page(addr) == CONTROL_STACK_RETURN_GUARD_PAGE(th)) {
441             protect_control_stack_guard_page(1, th);
442             protect_control_stack_return_guard_page(0, th);
443             break;
444         }
445         /* Regular memory fault */
446         handler = memory_fault_handler;
447         break;
448     case EXC_BAD_INSTRUCTION:
449         signal = SIGTRAP;
450         /* Check if illegal instruction trap */
451         if (code_vector[0] != EXC_I386_INVOP) {
452             ret = KERN_INVALID_RIGHT;
453             break;
454         }
455         /* Check if UD2 instruction */
456         if (*(unsigned short *)thread_state.EIP != 0x0b0f) {
457             /* KLUDGE: There are two ways we could get here:
458              * 1) We're executing data and we've hit some truly
459              *    illegal opcode, of which there are a few, see
460              *    Intel 64 and IA-32 Architectures
461              *    Sofware Developer's Manual
462              *    Volume 3A page 5-34)
463              * 2) The kernel started an unrelated signal handler
464              *    before we got a chance to run. The context that
465              *    caused the exception is saved in a stack frame
466              *    somewhere down below.
467              * In either case we rely on the exception to retrigger,
468              * eventually bailing out if we're spinning on case 2).
469              */
470             static mach_port_t last_thread;
471             static unsigned int last_eip;
472             if (last_thread == thread && last_eip == thread_state.EIP)
473                 ret = KERN_INVALID_RIGHT;
474             else
475                 ret = KERN_SUCCESS;
476             last_thread = thread;
477             last_eip = thread_state.EIP;
478             break;
479         }
480         /* Skip the trap code */
481         thread_state.EIP += 2;
482         /* Return from handler? */
483         if (*(unsigned short *)thread_state.EIP == 0xffff) {
484             if ((ret = thread_set_state(thread,
485                                         x86_THREAD_STATE32,
486                                         (thread_state_t)thread_state.EAX,
487                                         x86_THREAD_STATE32_COUNT)) != KERN_SUCCESS)
488                 lose("thread_set_state (x86_THREAD_STATE32) failed %d\n", ret);
489             if ((ret = thread_set_state(thread,
490                                         x86_FLOAT_STATE32,
491                                         (thread_state_t)thread_state.EBX,
492                                         x86_FLOAT_STATE32_COUNT)) != KERN_SUCCESS)
493                 lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
494             break;
495         }
496         /* Trap call */
497         handler = sigtrap_handler;
498         break;
499     default:
500         ret = KERN_INVALID_RIGHT;
501     }
502     /* Call handler */
503     if (handler != 0) {
504       siginfo.si_signo = signal;
505       siginfo.si_addr = addr;
506       call_handler_on_thread(thread, &thread_state, signal, &siginfo, handler);
507     }
508     return ret;
509 }
510
511 void *
512 mach_exception_handler(void *port)
513 {
514   mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
515   /* mach_msg_server should never return, but it should dispatch mach
516    * exceptions to our catch_exception_raise function
517    */
518   abort();
519 }
520
521 #endif
522
523 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
524
525 /* Sets up the thread that will listen for mach exceptions. note that
526    the exception handlers will be run on this thread. This is
527    different from the BSD-style signal handling situation in which the
528    signal handlers run in the relevant thread directly. */
529
530 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
531
532 pthread_t
533 setup_mach_exception_handling_thread()
534 {
535     kern_return_t ret;
536     pthread_t mach_exception_handling_thread = NULL;
537     pthread_attr_t attr;
538
539     /* allocate a mach_port for this process */
540     ret = mach_port_allocate(mach_task_self(),
541                              MACH_PORT_RIGHT_PORT_SET,
542                              &mach_exception_handler_port_set);
543
544     /* create the thread that will receive the mach exceptions */
545
546     FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
547
548     pthread_attr_init(&attr);
549     pthread_create(&mach_exception_handling_thread,
550                    &attr,
551                    mach_exception_handler,
552                    (void*) mach_exception_handler_port_set);
553     pthread_attr_destroy(&attr);
554
555     return mach_exception_handling_thread;
556 }
557
558 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
559    exception port (which is being listened to do by the mach
560    exception handling thread). */
561 kern_return_t
562 mach_thread_init(mach_port_t thread_exception_port)
563 {
564     kern_return_t ret;
565     /* allocate a named port for the thread */
566
567     FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
568
569     ret = mach_port_allocate_name(mach_task_self(),
570                                   MACH_PORT_RIGHT_RECEIVE,
571                                   thread_exception_port);
572     if (ret) {
573         lose("mach_port_allocate_name failed with return_code %d\n", ret);
574     }
575
576     /* establish the right for the thread_exception_port to send messages */
577     ret = mach_port_insert_right(mach_task_self(),
578                                  thread_exception_port,
579                                  thread_exception_port,
580                                  MACH_MSG_TYPE_MAKE_SEND);
581     if (ret) {
582         lose("mach_port_insert_right failed with return_code %d\n", ret);
583     }
584
585     ret = thread_set_exception_ports(mach_thread_self(),
586                                      EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
587                                      thread_exception_port,
588                                      EXCEPTION_DEFAULT,
589                                      THREAD_STATE_NONE);
590     if (ret) {
591         lose("thread_set_exception_port failed with return_code %d\n", ret);
592     }
593
594     ret = mach_port_move_member(mach_task_self(),
595                                 thread_exception_port,
596                                 mach_exception_handler_port_set);
597     if (ret) {
598         lose("mach_port_ failed with return_code %d\n", ret);
599     }
600
601     return ret;
602 }
603
604 void
605 setup_mach_exceptions() {
606     setup_mach_exception_handling_thread();
607     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
608 }
609
610 pid_t
611 mach_fork() {
612     pid_t pid = fork();
613     if (pid == 0) {
614         setup_mach_exceptions();
615         return pid;
616     } else {
617         return pid;
618     }
619 }
620
621 #endif