1.0.45.22: non-racy RUN-PROGRAM :PTY on OpenBSD
[sbcl.git] / src / runtime / x86-darwin-os.c
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
6
7 #include "thread.h"
8 #include "validate.h"
9 #include "runtime.h"
10 #include "interrupt.h"
11 #include "x86-darwin-os.h"
12 #include "genesis/fdefn.h"
13
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/vm_region.h>
19 #include <mach/machine/thread_state.h>
20 #include <mach/machine/thread_status.h>
21 #include <sys/_types.h>
22 #include <sys/ucontext.h>
23 #include <pthread.h>
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27
28 #ifdef LISP_FEATURE_SB_THREAD
29
30 pthread_mutex_t modify_ldt_lock = PTHREAD_MUTEX_INITIALIZER;
31
32 void set_data_desc_size(data_desc_t* desc, unsigned long size)
33 {
34     desc->limit00 = (size - 1) & 0xffff;
35     desc->limit16 = ((size - 1) >> 16) &0xf;
36 }
37
38 void set_data_desc_addr(data_desc_t* desc, void* addr)
39 {
40     desc->base00 = (unsigned int)addr & 0xffff;
41     desc->base16 = ((unsigned int)addr & 0xff0000) >> 16;
42     desc->base24 = ((unsigned int)addr & 0xff000000) >> 24;
43 }
44
45 #endif
46
47 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
48 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
49 #endif
50
51 int arch_os_thread_init(struct thread *thread) {
52 #ifdef LISP_FEATURE_SB_THREAD
53     int n;
54     sel_t sel;
55
56     data_desc_t ldt_entry = { 0, 0, 0, DESC_DATA_WRITE,
57                               3, 1, 0, DESC_DATA_32B, DESC_GRAN_BYTE, 0 };
58
59     set_data_desc_addr(&ldt_entry, thread);
60     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
61
62     thread_mutex_lock(&modify_ldt_lock);
63     n = i386_set_ldt(LDT_AUTO_ALLOC, (union ldt_entry*) &ldt_entry, 1);
64
65     if (n < 0) {
66         perror("i386_set_ldt");
67         lose("unexpected i386_set_ldt(..) failure\n");
68     }
69     thread_mutex_unlock(&modify_ldt_lock);
70
71     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
72     sel.index = n;
73     sel.rpl = USER_PRIV;
74     sel.ti = SEL_LDT;
75
76     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
77
78     thread->tls_cookie=n;
79     pthread_setspecific(specials,thread);
80 #endif
81 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
82     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread));
83 #endif
84
85 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
86     stack_t sigstack;
87
88     /* Signal handlers are run on the control stack, so if it is exhausted
89      * we had better use an alternate stack for whatever signal tells us
90      * we've exhausted it */
91     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
92     sigstack.ss_flags=0;
93     sigstack.ss_size = 32*SIGSTKSZ;
94     sigaltstack(&sigstack,0);
95 #endif
96     return 1;                  /* success */
97 }
98
99 int arch_os_thread_cleanup(struct thread *thread) {
100 #if defined(LISP_FEATURE_SB_THREAD)
101     int n = thread->tls_cookie;
102
103     /* Set the %%fs register back to 0 and free the ldt by setting it
104      * to NULL.
105      */
106     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
107
108     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
109     thread_mutex_lock(&modify_ldt_lock);
110     i386_set_ldt(n, NULL, 1);
111     thread_mutex_unlock(&modify_ldt_lock);
112 #endif
113     return 1;                  /* success */
114 }
115
116 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
117
118 void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context);
119 void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context);
120 void memory_fault_handler(int signal, siginfo_t *siginfo,
121                           os_context_t *context);
122
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.
126  *
127  */
128 extern boolean_t exc_server();
129
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;
139 }
140
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);
149 }
150
151 /* Modify a context to push new data on its stack. */
152 void push_context(u32 data, x86_thread_state32_t *thread_state)
153 {
154     u32 *stack_pointer;
155
156     stack_pointer = (u32*) thread_state->ESP;
157     *(--stack_pointer) = data;
158     thread_state->ESP = (unsigned int) stack_pointer;
159 }
160
161 void align_context_stack(x86_thread_state32_t *thread_state)
162 {
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);
166 }
167
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;");
174
175 void open_stack_allocation(x86_thread_state32_t *thread_state)
176 {
177     void stack_allocation_recover(void);
178
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;
183
184     align_context_stack(thread_state);
185 }
186
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)
191 {
192     /* round up size to 16byte multiple */
193     size = (size + 15) & -16;
194
195     thread_state->ESP = ((u32)thread_state->ESP) - size;
196
197     return (void *)thread_state->ESP;
198 }
199
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                                 ...)
209 {
210     va_list ap;
211     int i;
212     u32 *stack_pointer;
213
214     /* Set up to restore stack on exit. */
215     open_stack_allocation(thread_state);
216
217     /* Have to keep stack 16byte aligned on x86/darwin. */
218     for (i = (3 & -nargs); i; i--) {
219         push_context(0, thread_state);
220     }
221
222     thread_state->ESP = ((u32)thread_state->ESP) - nargs * 4;
223     stack_pointer = (u32 *)thread_state->ESP;
224
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);
229     }
230     va_end(ap);
231
232     push_context(thread_state->EIP, thread_state);
233     thread_state->EIP = (unsigned int) function;
234 }
235
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 *))
241 {
242
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).
254      */
255
256     os_context_t *context;
257     mcontext_t *regs;
258
259     context = (os_context_t*) os_validate(0, sizeof(os_context_t));
260     regs = (mcontext_t*) os_validate(0, sizeof(mcontext_t));
261     context->uc_mcontext = regs;
262
263     /* when BSD signals are fired, they mask they signals in sa_mask
264        which always seem to be the blockable_sigset, for us, so we
265        need to:
266        1) save the current sigmask
267        2) block blockable signals
268        3) call the signal handler
269        4) restore the sigmask */
270
271     build_fake_signal_context(context, thread_state, float_state);
272
273     block_blockable_signals(0, 0);
274
275     handler(signal, siginfo, context);
276
277     update_thread_state_from_context(thread_state, float_state, context);
278
279     os_invalidate((os_vm_address_t)context, sizeof(os_context_t));
280     os_invalidate((os_vm_address_t)regs, sizeof(mcontext_t));
281
282     /* Trap to restore the signal context. */
283     asm volatile ("movl %0, %%eax; movl %1, %%ebx; .long 0xffff0b0f"
284                   : : "r" (thread_state), "r" (float_state));
285 }
286
287 /* Convenience wrapper for the above */
288 void call_handler_on_thread(mach_port_t thread,
289                             x86_thread_state32_t *thread_state,
290                             int signal,
291                             siginfo_t *siginfo,
292                             void (*handler)(int, siginfo_t *, void *))
293 {
294     x86_thread_state32_t new_state;
295     x86_thread_state32_t *save_thread_state;
296     x86_float_state32_t *save_float_state;
297     mach_msg_type_number_t state_count;
298     siginfo_t *save_siginfo;
299     kern_return_t ret;
300     /* Initialize the new state */
301     new_state = *thread_state;
302     open_stack_allocation(&new_state);
303     stack_allocate(&new_state, 256);
304     /* Save old state */
305     save_thread_state = (x86_thread_state32_t *)stack_allocate(&new_state, sizeof(*save_thread_state));
306     *save_thread_state = *thread_state;
307     /* Save float state */
308     save_float_state = (x86_float_state32_t *)stack_allocate(&new_state, sizeof(*save_float_state));
309     state_count = x86_FLOAT_STATE32_COUNT;
310     if ((ret = thread_get_state(thread,
311                                 x86_FLOAT_STATE32,
312                                 (thread_state_t)save_float_state,
313                                 &state_count)) != KERN_SUCCESS)
314         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
315     /* Set up siginfo */
316     save_siginfo = stack_allocate(&new_state, sizeof(*siginfo));
317     if (siginfo == NULL)
318         save_siginfo = siginfo;
319     else
320         *save_siginfo = *siginfo;
321     /* Prepare to call */
322     call_c_function_in_context(&new_state,
323                                signal_emulation_wrapper,
324                                5,
325                                save_thread_state,
326                                save_float_state,
327                                signal,
328                                save_siginfo,
329                                handler);
330     /* Update the thread state */
331     state_count = x86_THREAD_STATE32_COUNT;
332     if ((ret = thread_set_state(thread,
333                                 x86_THREAD_STATE32,
334                                 (thread_state_t)&new_state,
335                                 state_count)) != KERN_SUCCESS)
336         lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
337
338 }
339
340 #if defined DUMP_CONTEXT
341 void dump_context(x86_thread_state32_t *thread_state)
342 {
343     int i;
344     u32 *stack_pointer;
345
346     printf("eax: %08lx  ecx: %08lx  edx: %08lx  ebx: %08lx\n",
347            thread_state->EAX, thread_state->ECX, thread_state->EDX, thread_state->EAX);
348     printf("esp: %08lx  ebp: %08lx  esi: %08lx  edi: %08lx\n",
349            thread_state->ESP, thread_state->EBP, thread_state->ESI, thread_state->EDI);
350     printf("eip: %08lx  eflags: %08lx\n",
351            thread_state->EIP, thread_state->EFLAGS);
352     printf("cs: %04hx  ds: %04hx  es: %04hx  "
353            "ss: %04hx  fs: %04hx  gs: %04hx\n",
354            thread_state->CS,
355            thread_state->DS,
356            thread_state->ES,
357            thread_state->SS,
358            thread_state->FS,
359            thread_state->GS);
360
361     stack_pointer = (u32 *)thread_state->ESP;
362     for (i = 0; i < 48; i+=4) {
363         printf("%08x:  %08x %08x %08x %08x\n",
364                thread_state->ESP + (i * 4),
365                stack_pointer[i],
366                stack_pointer[i+1],
367                stack_pointer[i+2],
368                stack_pointer[i+3]);
369     }
370 }
371 #endif
372
373 void
374 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
375                                 os_context_t *context) {
376
377     unblock_signals_in_context_and_maybe_warn(context);
378     arrange_return_to_lisp_function
379         (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
380 }
381
382 void
383 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
384     arrange_return_to_lisp_function
385         (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
386 }
387
388 kern_return_t
389 catch_exception_raise(mach_port_t exception_port,
390                       mach_port_t thread,
391                       mach_port_t task,
392                       exception_type_t exception,
393                       exception_data_t code_vector,
394                       mach_msg_type_number_t code_count)
395 {
396     struct thread *th = (struct thread*) exception_port;
397     x86_thread_state32_t thread_state;
398     mach_msg_type_number_t state_count;
399     vm_address_t region_addr;
400     vm_size_t region_size;
401     vm_region_basic_info_data_t region_info;
402     mach_msg_type_number_t info_count;
403     mach_port_t region_name;
404     void *addr = NULL;
405     int signal = 0;
406     void (*handler)(int, siginfo_t *, void *) = NULL;
407     siginfo_t siginfo;
408     kern_return_t ret;
409
410     /* Get state and info */
411     state_count = x86_THREAD_STATE32_COUNT;
412     if ((ret = thread_get_state(thread,
413                                 x86_THREAD_STATE32,
414                                 (thread_state_t)&thread_state,
415                                 &state_count)) != KERN_SUCCESS)
416         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
417     switch (exception) {
418     case EXC_BAD_ACCESS:
419         signal = SIGBUS;
420         /* Check if write protection fault */
421         if ((code_vector[0] & OS_VM_PROT_ALL) == 0) {
422             ret = KERN_INVALID_RIGHT;
423             break;
424         }
425         addr = (void*)code_vector[1];
426         /* Undefined alien */
427         if (os_trunc_to_page(addr) == undefined_alien_address) {
428             handler = undefined_alien_handler;
429             break;
430         }
431         /* At stack guard */
432         if (os_trunc_to_page(addr) == CONTROL_STACK_GUARD_PAGE(th)) {
433             lower_thread_control_stack_guard_page(th);
434             handler = control_stack_exhausted_handler;
435             break;
436         }
437         /* Return from stack guard */
438         if (os_trunc_to_page(addr) == CONTROL_STACK_RETURN_GUARD_PAGE(th)) {
439             reset_thread_control_stack_guard_page(th);
440             break;
441         }
442         /* Regular memory fault */
443         handler = memory_fault_handler;
444         break;
445     case EXC_BAD_INSTRUCTION:
446         signal = SIGTRAP;
447         /* Check if illegal instruction trap */
448         if (code_vector[0] != EXC_I386_INVOP) {
449             ret = KERN_INVALID_RIGHT;
450             break;
451         }
452         /* Check if UD2 instruction */
453         if (*(unsigned short *)thread_state.EIP != 0x0b0f) {
454             /* KLUDGE: There are two ways we could get here:
455              * 1) We're executing data and we've hit some truly
456              *    illegal opcode, of which there are a few, see
457              *    Intel 64 and IA-32 Architectures
458              *    Sofware Developer's Manual
459              *    Volume 3A page 5-34)
460              * 2) The kernel started an unrelated signal handler
461              *    before we got a chance to run. The context that
462              *    caused the exception is saved in a stack frame
463              *    somewhere down below.
464              * In either case we rely on the exception to retrigger,
465              * eventually bailing out if we're spinning on case 2).
466              */
467             static mach_port_t last_thread;
468             static unsigned int last_eip;
469             if (last_thread == thread && last_eip == thread_state.EIP)
470                 ret = KERN_INVALID_RIGHT;
471             else
472                 ret = KERN_SUCCESS;
473             last_thread = thread;
474             last_eip = thread_state.EIP;
475             break;
476         }
477         /* Skip the trap code */
478         thread_state.EIP += 2;
479         /* Return from handler? */
480         if (*(unsigned short *)thread_state.EIP == 0xffff) {
481             if ((ret = thread_set_state(thread,
482                                         x86_THREAD_STATE32,
483                                         (thread_state_t)thread_state.EAX,
484                                         x86_THREAD_STATE32_COUNT)) != KERN_SUCCESS)
485                 lose("thread_set_state (x86_THREAD_STATE32) failed %d\n", ret);
486             if ((ret = thread_set_state(thread,
487                                         x86_FLOAT_STATE32,
488                                         (thread_state_t)thread_state.EBX,
489                                         x86_FLOAT_STATE32_COUNT)) != KERN_SUCCESS)
490                 lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
491             break;
492         }
493         /* Trap call */
494         handler = sigtrap_handler;
495         break;
496     default:
497         ret = KERN_INVALID_RIGHT;
498     }
499     /* Call handler */
500     if (handler != 0) {
501       siginfo.si_signo = signal;
502       siginfo.si_addr = addr;
503       call_handler_on_thread(thread, &thread_state, signal, &siginfo, handler);
504     }
505     return ret;
506 }
507
508 void *
509 mach_exception_handler(void *port)
510 {
511   mach_msg_server(exc_server, 2048, (mach_port_t) port, 0);
512   /* mach_msg_server should never return, but it should dispatch mach
513    * exceptions to our catch_exception_raise function
514    */
515   lose("mach_msg_server returned");
516 }
517
518 #endif
519
520 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
521
522 /* Sets up the thread that will listen for mach exceptions. note that
523    the exception handlers will be run on this thread. This is
524    different from the BSD-style signal handling situation in which the
525    signal handlers run in the relevant thread directly. */
526
527 mach_port_t mach_exception_handler_port_set = MACH_PORT_NULL;
528
529 pthread_t
530 setup_mach_exception_handling_thread()
531 {
532     kern_return_t ret;
533     pthread_t mach_exception_handling_thread = NULL;
534     pthread_attr_t attr;
535
536     /* allocate a mach_port for this process */
537     ret = mach_port_allocate(mach_task_self(),
538                              MACH_PORT_RIGHT_PORT_SET,
539                              &mach_exception_handler_port_set);
540
541     /* create the thread that will receive the mach exceptions */
542
543     FSHOW((stderr, "Creating mach_exception_handler thread!\n"));
544
545     pthread_attr_init(&attr);
546     pthread_create(&mach_exception_handling_thread,
547                    &attr,
548                    mach_exception_handler,
549                    (void*) mach_exception_handler_port_set);
550     pthread_attr_destroy(&attr);
551
552     return mach_exception_handling_thread;
553 }
554
555 /* tell the kernel that we want EXC_BAD_ACCESS exceptions sent to the
556    exception port (which is being listened to do by the mach
557    exception handling thread). */
558 kern_return_t
559 mach_thread_init(mach_port_t thread_exception_port)
560 {
561     kern_return_t ret;
562     /* allocate a named port for the thread */
563
564     FSHOW((stderr, "Allocating mach port %x\n", thread_exception_port));
565
566     ret = mach_port_allocate_name(mach_task_self(),
567                                   MACH_PORT_RIGHT_RECEIVE,
568                                   thread_exception_port);
569     if (ret) {
570         lose("mach_port_allocate_name failed with return_code %d\n", ret);
571     }
572
573     /* establish the right for the thread_exception_port to send messages */
574     ret = mach_port_insert_right(mach_task_self(),
575                                  thread_exception_port,
576                                  thread_exception_port,
577                                  MACH_MSG_TYPE_MAKE_SEND);
578     if (ret) {
579         lose("mach_port_insert_right failed with return_code %d\n", ret);
580     }
581
582     ret = thread_set_exception_ports(mach_thread_self(),
583                                      EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION,
584                                      thread_exception_port,
585                                      EXCEPTION_DEFAULT,
586                                      THREAD_STATE_NONE);
587     if (ret) {
588         lose("thread_set_exception_port failed with return_code %d\n", ret);
589     }
590
591     ret = mach_port_move_member(mach_task_self(),
592                                 thread_exception_port,
593                                 mach_exception_handler_port_set);
594     if (ret) {
595         lose("mach_port_ failed with return_code %d\n", ret);
596     }
597
598     return ret;
599 }
600
601 void
602 setup_mach_exceptions() {
603     setup_mach_exception_handling_thread();
604     mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(all_threads));
605 }
606
607 pid_t
608 mach_fork() {
609     pid_t pid = fork();
610     if (pid == 0) {
611         setup_mach_exceptions();
612         return pid;
613     } else {
614         return pid;
615     }
616 }
617
618 #endif