fix bug in SYMBOL-VALUE CAS expansion for constant arguments
[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 int arch_os_thread_init(struct thread *thread) {
48 #ifdef LISP_FEATURE_SB_THREAD
49     int n;
50     sel_t sel;
51
52     data_desc_t ldt_entry = { 0, 0, 0, DESC_DATA_WRITE,
53                               3, 1, 0, DESC_DATA_32B, DESC_GRAN_BYTE, 0 };
54
55     set_data_desc_addr(&ldt_entry, thread);
56     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
57
58     thread_mutex_lock(&modify_ldt_lock);
59     n = i386_set_ldt(LDT_AUTO_ALLOC, (union ldt_entry*) &ldt_entry, 1);
60
61     if (n < 0) {
62         perror("i386_set_ldt");
63         lose("unexpected i386_set_ldt(..) failure\n");
64     }
65     thread_mutex_unlock(&modify_ldt_lock);
66
67     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
68     sel.index = n;
69     sel.rpl = USER_PRIV;
70     sel.ti = SEL_LDT;
71
72     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
73
74     thread->tls_cookie=n;
75     pthread_setspecific(specials,thread);
76 #endif
77 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
78     mach_lisp_thread_init(thread);
79 #endif
80
81 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
82     stack_t sigstack;
83
84     /* Signal handlers are run on the control stack, so if it is exhausted
85      * we had better use an alternate stack for whatever signal tells us
86      * we've exhausted it */
87     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
88     sigstack.ss_flags=0;
89     sigstack.ss_size = 32*SIGSTKSZ;
90     sigaltstack(&sigstack,0);
91 #endif
92     return 1;                  /* success */
93 }
94
95 int arch_os_thread_cleanup(struct thread *thread) {
96 #if defined(LISP_FEATURE_SB_THREAD)
97     int n = thread->tls_cookie;
98
99     /* Set the %%fs register back to 0 and free the ldt by setting it
100      * to NULL.
101      */
102     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
103
104     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
105     thread_mutex_lock(&modify_ldt_lock);
106     i386_set_ldt(n, NULL, 1);
107     thread_mutex_unlock(&modify_ldt_lock);
108 #endif
109     return 1;                  /* success */
110 }
111
112 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
113
114 void sigill_handler(int signal, siginfo_t *siginfo, os_context_t *context);
115 void sigtrap_handler(int signal, siginfo_t *siginfo, os_context_t *context);
116 void memory_fault_handler(int signal, siginfo_t *siginfo,
117                           os_context_t *context);
118
119 /* This executes in the faulting thread as part of the signal
120  * emulation.  It is passed a context with the uc_mcontext field
121  * pointing to a valid block of memory. */
122 void build_fake_signal_context(os_context_t *context,
123                                x86_thread_state32_t *thread_state,
124                                x86_float_state32_t *float_state) {
125     pthread_sigmask(0, NULL, &context->uc_sigmask);
126     context->uc_mcontext->SS = *thread_state;
127     context->uc_mcontext->FS = *float_state;
128 }
129
130 /* This executes in the faulting thread as part of the signal
131  * emulation.  It is effectively the inverse operation from above. */
132 void update_thread_state_from_context(x86_thread_state32_t *thread_state,
133                                       x86_float_state32_t *float_state,
134                                       os_context_t *context) {
135     *thread_state = context->uc_mcontext->SS;
136     *float_state = context->uc_mcontext->FS;
137     pthread_sigmask(SIG_SETMASK, &context->uc_sigmask, NULL);
138 }
139
140 /* Modify a context to push new data on its stack. */
141 void push_context(u32 data, x86_thread_state32_t *thread_state)
142 {
143     u32 *stack_pointer;
144
145     stack_pointer = (u32*) thread_state->ESP;
146     *(--stack_pointer) = data;
147     thread_state->ESP = (unsigned int) stack_pointer;
148 }
149
150 void align_context_stack(x86_thread_state32_t *thread_state)
151 {
152     /* 16byte align the stack (provided that the stack is, as it
153      * should be, 4byte aligned. */
154     while (thread_state->ESP & 15) push_context(0, thread_state);
155 }
156
157 /* Stack allocation starts with a context that has a mod-4 ESP value
158  * and needs to leave a context with a mod-16 ESP that will restore
159  * the old ESP value and other register state when activated.  The
160  * first part of this is the recovery trampoline, which loads ESP from
161  * EBP, pops EBP, and returns. */
162 asm("_stack_allocation_recover: movl %ebp, %esp; popl %ebp; ret;");
163
164 void open_stack_allocation(x86_thread_state32_t *thread_state)
165 {
166     void stack_allocation_recover(void);
167
168     push_context(thread_state->EIP, thread_state);
169     push_context(thread_state->EBP, thread_state);
170     thread_state->EBP = thread_state->ESP;
171     thread_state->EIP = (unsigned int) stack_allocation_recover;
172
173     align_context_stack(thread_state);
174 }
175
176 /* Stack allocation of data starts with a context with a mod-16 ESP
177  * value and reserves some space on it by manipulating the ESP
178  * register. */
179 void *stack_allocate(x86_thread_state32_t *thread_state, size_t size)
180 {
181     /* round up size to 16byte multiple */
182     size = (size + 15) & -16;
183
184     thread_state->ESP = ((u32)thread_state->ESP) - size;
185
186     return (void *)thread_state->ESP;
187 }
188
189 /* Arranging to invoke a C function is tricky, as we have to assume
190  * cdecl calling conventions (caller removes args) and x86/darwin
191  * alignment requirements.  The simplest way to arrange this,
192  * actually, is to open a new stack allocation.
193  * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
194 void call_c_function_in_context(x86_thread_state32_t *thread_state,
195                                 void *function,
196                                 int nargs,
197                                 ...)
198 {
199     va_list ap;
200     int i;
201     u32 *stack_pointer;
202
203     /* Set up to restore stack on exit. */
204     open_stack_allocation(thread_state);
205
206     /* Have to keep stack 16byte aligned on x86/darwin. */
207     for (i = (3 & -nargs); i; i--) {
208         push_context(0, thread_state);
209     }
210
211     thread_state->ESP = ((u32)thread_state->ESP) - nargs * 4;
212     stack_pointer = (u32 *)thread_state->ESP;
213
214     va_start(ap, nargs);
215     for (i = 0; i < nargs; i++) {
216         //push_context(va_arg(ap, u32), thread_state);
217         stack_pointer[i] = va_arg(ap, u32);
218     }
219     va_end(ap);
220
221     push_context(thread_state->EIP, thread_state);
222     thread_state->EIP = (unsigned int) function;
223 }
224
225 void signal_emulation_wrapper(x86_thread_state32_t *thread_state,
226                               x86_float_state32_t *float_state,
227                               int signal,
228                               siginfo_t *siginfo,
229                               void (*handler)(int, siginfo_t *, void *))
230 {
231
232     /* CLH: FIXME **NOTE: HACK ALERT!** Ideally, we would allocate
233      * context and regs on the stack as local variables, but this
234      * causes problems for the lisp debugger. When it walks the stack
235      * for a back trace, it sees the 1) address of the local variable
236      * on the stack and thinks that is a frame pointer to a lisp
237      * frame, and, 2) the address of the sap that we alloc'ed in
238      * dynamic space and thinks that is a return address, so it,
239      * heuristicly (and wrongly), chooses that this should be
240      * interpreted as a lisp frame instead of as a C frame.
241      * We can work around this in this case by os_validating the
242      * context (and regs just for symmetry).
243      */
244
245     os_context_t *context;
246     mcontext_t *regs;
247
248     context = (os_context_t*) os_validate(0, sizeof(os_context_t));
249     regs = (mcontext_t*) os_validate(0, sizeof(mcontext_t));
250     context->uc_mcontext = regs;
251
252     /* when BSD signals are fired, they mask they signals in sa_mask
253        which always seem to be the blockable_sigset, for us, so we
254        need to:
255        1) save the current sigmask
256        2) block blockable signals
257        3) call the signal handler
258        4) restore the sigmask */
259
260     build_fake_signal_context(context, thread_state, float_state);
261
262     block_blockable_signals(0, 0);
263
264     handler(signal, siginfo, context);
265
266     update_thread_state_from_context(thread_state, float_state, context);
267
268     os_invalidate((os_vm_address_t)context, sizeof(os_context_t));
269     os_invalidate((os_vm_address_t)regs, sizeof(mcontext_t));
270
271     /* Trap to restore the signal context. */
272     asm volatile (".long 0xffff0b0f"
273                   : : "a" (thread_state), "c" (float_state));
274 }
275
276 /* Convenience wrapper for the above */
277 void call_handler_on_thread(mach_port_t thread,
278                             x86_thread_state32_t *thread_state,
279                             int signal,
280                             siginfo_t *siginfo,
281                             void (*handler)(int, siginfo_t *, void *))
282 {
283     x86_thread_state32_t new_state;
284     x86_thread_state32_t *save_thread_state;
285     x86_float_state32_t *save_float_state;
286     mach_msg_type_number_t state_count;
287     siginfo_t *save_siginfo;
288     kern_return_t ret;
289     /* Initialize the new state */
290     new_state = *thread_state;
291     open_stack_allocation(&new_state);
292     stack_allocate(&new_state, 256);
293     /* Save old state */
294     save_thread_state = (x86_thread_state32_t *)stack_allocate(&new_state, sizeof(*save_thread_state));
295     *save_thread_state = *thread_state;
296     /* Save float state */
297     save_float_state = (x86_float_state32_t *)stack_allocate(&new_state, sizeof(*save_float_state));
298     state_count = x86_FLOAT_STATE32_COUNT;
299     if ((ret = thread_get_state(thread,
300                                 x86_FLOAT_STATE32,
301                                 (thread_state_t)save_float_state,
302                                 &state_count)) != KERN_SUCCESS)
303         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
304     /* Set up siginfo */
305     save_siginfo = stack_allocate(&new_state, sizeof(*siginfo));
306     if (siginfo == NULL)
307         save_siginfo = siginfo;
308     else
309         *save_siginfo = *siginfo;
310     /* Prepare to call */
311     call_c_function_in_context(&new_state,
312                                signal_emulation_wrapper,
313                                5,
314                                save_thread_state,
315                                save_float_state,
316                                signal,
317                                save_siginfo,
318                                handler);
319     /* Update the thread state */
320     state_count = x86_THREAD_STATE32_COUNT;
321     if ((ret = thread_set_state(thread,
322                                 x86_THREAD_STATE32,
323                                 (thread_state_t)&new_state,
324                                 state_count)) != KERN_SUCCESS)
325         lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
326
327 }
328
329 #if defined DUMP_CONTEXT
330 void dump_context(x86_thread_state32_t *thread_state)
331 {
332     int i;
333     u32 *stack_pointer;
334
335     printf("eax: %08lx  ecx: %08lx  edx: %08lx  ebx: %08lx\n",
336            thread_state->EAX, thread_state->ECX, thread_state->EDX, thread_state->EAX);
337     printf("esp: %08lx  ebp: %08lx  esi: %08lx  edi: %08lx\n",
338            thread_state->ESP, thread_state->EBP, thread_state->ESI, thread_state->EDI);
339     printf("eip: %08lx  eflags: %08lx\n",
340            thread_state->EIP, thread_state->EFLAGS);
341     printf("cs: %04hx  ds: %04hx  es: %04hx  "
342            "ss: %04hx  fs: %04hx  gs: %04hx\n",
343            thread_state->CS,
344            thread_state->DS,
345            thread_state->ES,
346            thread_state->SS,
347            thread_state->FS,
348            thread_state->GS);
349
350     stack_pointer = (u32 *)thread_state->ESP;
351     for (i = 0; i < 48; i+=4) {
352         printf("%08x:  %08x %08x %08x %08x\n",
353                thread_state->ESP + (i * 4),
354                stack_pointer[i],
355                stack_pointer[i+1],
356                stack_pointer[i+2],
357                stack_pointer[i+3]);
358     }
359 }
360 #endif
361
362 void
363 control_stack_exhausted_handler(int signal, siginfo_t *siginfo,
364                                 os_context_t *context) {
365
366     unblock_signals_in_context_and_maybe_warn(context);
367     arrange_return_to_lisp_function
368         (context, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR));
369 }
370
371 void
372 undefined_alien_handler(int signal, siginfo_t *siginfo, os_context_t *context) {
373     arrange_return_to_lisp_function
374         (context, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR));
375 }
376
377 kern_return_t
378 catch_exception_raise(mach_port_t exception_port,
379                       mach_port_t thread,
380                       mach_port_t task,
381                       exception_type_t exception,
382                       exception_data_t code_vector,
383                       mach_msg_type_number_t code_count)
384 {
385     x86_thread_state32_t thread_state;
386     mach_msg_type_number_t state_count;
387     vm_address_t region_addr;
388     vm_size_t region_size;
389     vm_region_basic_info_data_t region_info;
390     mach_msg_type_number_t info_count;
391     mach_port_t region_name;
392     void *addr = NULL;
393     int signal = 0;
394     void (*handler)(int, siginfo_t *, void *) = NULL;
395     siginfo_t siginfo;
396     kern_return_t ret, dealloc_ret;
397
398     struct thread *th;
399
400     FSHOW((stderr,"/entering catch_exception_raise with exception: %d\n", exception));
401     th = *(struct thread**)exception_port;
402     /* Get state and info */
403     state_count = x86_THREAD_STATE32_COUNT;
404     if ((ret = thread_get_state(thread,
405                                 x86_THREAD_STATE32,
406                                 (thread_state_t)&thread_state,
407                                 &state_count)) != KERN_SUCCESS)
408         lose("thread_get_state (x86_THREAD_STATE32) failed %d\n", ret);
409     switch (exception) {
410     case EXC_BAD_ACCESS:
411         signal = SIGBUS;
412         /* Check if write protection fault */
413         if ((code_vector[0] & OS_VM_PROT_ALL) == 0) {
414             ret = KERN_INVALID_RIGHT;
415             break;
416         }
417         addr = (void*)code_vector[1];
418         /* Undefined alien */
419         if (os_trunc_to_page(addr) == undefined_alien_address) {
420             handler = undefined_alien_handler;
421             break;
422         }
423         /* At stack guard */
424         if (os_trunc_to_page(addr) == CONTROL_STACK_GUARD_PAGE(th)) {
425             lower_thread_control_stack_guard_page(th);
426             handler = control_stack_exhausted_handler;
427             break;
428         }
429         /* Return from stack guard */
430         if (os_trunc_to_page(addr) == CONTROL_STACK_RETURN_GUARD_PAGE(th)) {
431             reset_thread_control_stack_guard_page(th);
432             break;
433         }
434         /* Regular memory fault */
435         handler = memory_fault_handler;
436         break;
437     case EXC_BAD_INSTRUCTION:
438         signal = SIGTRAP;
439         /* Check if illegal instruction trap */
440         if (code_vector[0] != EXC_I386_INVOP) {
441             ret = KERN_INVALID_RIGHT;
442             break;
443         }
444         /* Check if UD2 instruction */
445         if (*(unsigned short *)thread_state.EIP != 0x0b0f) {
446             /* KLUDGE: There are two ways we could get here:
447              * 1) We're executing data and we've hit some truly
448              *    illegal opcode, of which there are a few, see
449              *    Intel 64 and IA-32 Architectures
450              *    Sofware Developer's Manual
451              *    Volume 3A page 5-34)
452              * 2) The kernel started an unrelated signal handler
453              *    before we got a chance to run. The context that
454              *    caused the exception is saved in a stack frame
455              *    somewhere down below.
456              * In either case we rely on the exception to retrigger,
457              * eventually bailing out if we're spinning on case 2).
458              */
459             static mach_port_t last_thread;
460             static unsigned int last_eip;
461             if (last_thread == thread && last_eip == thread_state.EIP)
462                 ret = KERN_INVALID_RIGHT;
463             else
464                 ret = KERN_SUCCESS;
465             last_thread = thread;
466             last_eip = thread_state.EIP;
467             break;
468         }
469         /* Skip the trap code */
470         thread_state.EIP += 2;
471         /* Return from handler? */
472         if (*(unsigned short *)thread_state.EIP == 0xffff) {
473             if ((ret = thread_set_state(thread,
474                                         x86_THREAD_STATE32,
475                                         (thread_state_t)thread_state.EAX,
476                                         x86_THREAD_STATE32_COUNT)) != KERN_SUCCESS)
477                 lose("thread_set_state (x86_THREAD_STATE32) failed %d\n", ret);
478             if ((ret = thread_set_state(thread,
479                                         x86_FLOAT_STATE32,
480                                         (thread_state_t)thread_state.ECX,
481                                         x86_FLOAT_STATE32_COUNT)) != KERN_SUCCESS)
482                 lose("thread_set_state (x86_FLOAT_STATE32) failed %d\n", ret);
483             break;
484         }
485         /* Trap call */
486         handler = sigtrap_handler;
487         break;
488     default:
489         ret = KERN_INVALID_RIGHT;
490     }
491     /* Call handler */
492     if (handler != 0) {
493       siginfo.si_signo = signal;
494       siginfo.si_addr = addr;
495       call_handler_on_thread(thread, &thread_state, signal, &siginfo, handler);
496     }
497
498     dealloc_ret = mach_port_deallocate (current_mach_task, thread);
499     if (dealloc_ret) {
500       lose("mach_port_deallocate (thread) failed with return_code %d\n", dealloc_ret);
501     }
502
503     dealloc_ret = mach_port_deallocate (current_mach_task, task);
504     if (dealloc_ret) {
505       lose("mach_port_deallocate (task) failed with return_code %d\n", dealloc_ret);
506     }
507
508     return ret;
509 }
510
511 void
512 os_restore_fp_control(os_context_t *context)
513 {
514     /* KLUDGE: The x87 FPU control word is some nasty bitfield struct
515      * thing.  Rather than deal with that, just grab it as a 16-bit
516      * integer. */
517     unsigned short fpu_control_word =
518         *((unsigned short *)&context->uc_mcontext->FS.FPU_FCW);
519     /* reset exception flags and restore control flags on x87 FPU */
520     asm ("fldcw %0" : : "m" (fpu_control_word));
521 }
522
523 #endif