1 #ifdef LISP_FEATURE_SB_THREAD
2 #include <mach/mach_init.h>
9 #include "x86-64-darwin-os.h"
10 #include "x86-64-arch.h"
11 #include "genesis/fdefn.h"
13 #include <mach/mach.h>
14 #include <mach/mach_error.h>
15 #include <mach/mach_types.h>
16 #include <mach/sync_policy.h>
17 #include <mach/machine/thread_state.h>
18 #include <mach/machine/thread_status.h>
19 #include <sys/_types.h>
20 #include <sys/ucontext.h>
27 #include <sys/_structs.h>
32 typedef struct __darwin_ucontext darwin_ucontext
;
33 typedef struct __darwin_mcontext64 darwin_mcontext
;
46 #define faultvaddr __faultvaddr
51 #define fpu_fcw __fpu_fcw
52 #define fpu_mxcsr __fpu_mxcsr
56 typedef struct ucontext darwin_ucontext
;
57 typedef struct mcontext darwin_mcontext
;
61 #ifdef LISP_FEATURE_SB_THREAD
62 pthread_mutex_t mach_exception_lock
= PTHREAD_MUTEX_INITIALIZER
;
65 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
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 /* This executes in the faulting thread as part of the signal
73 * emulation. It is passed a context with the uc_mcontext field
74 * pointing to a valid block of memory. */
75 void build_fake_signal_context(darwin_ucontext
*context
,
76 x86_thread_state64_t
*thread_state
,
77 x86_float_state64_t
*float_state
) {
78 thread_sigmask(0, NULL
, &context
->uc_sigmask
);
79 context
->uc_mcontext
->ss
= *thread_state
;
80 context
->uc_mcontext
->fs
= *float_state
;
83 /* This executes in the faulting thread as part of the signal
84 * emulation. It is effectively the inverse operation from above. */
85 void update_thread_state_from_context(x86_thread_state64_t
*thread_state
,
86 x86_float_state64_t
*float_state
,
87 darwin_ucontext
*context
) {
88 *thread_state
= context
->uc_mcontext
->ss
;
89 *float_state
= context
->uc_mcontext
->fs
;
90 thread_sigmask(SIG_SETMASK
, &context
->uc_sigmask
, NULL
);
93 /* Modify a context to push new data on its stack. */
94 void push_context(u64 data
, x86_thread_state64_t
*context
)
98 stack_pointer
= (u64
*) context
->rsp
;
99 *(--stack_pointer
) = data
;
100 context
->rsp
= (u64
) stack_pointer
;
103 void align_context_stack(x86_thread_state64_t
*context
)
105 /* 16byte align the stack (provided that the stack is, as it
106 * should be, 8byte aligned. */
107 while (context
->rsp
& 15) push_context(0, context
);
110 /* Stack allocation starts with a context that has a mod-4 ESP value
111 * and needs to leave a context with a mod-16 ESP that will restore
112 * the old ESP value and other register state when activated. The
113 * first part of this is the recovery trampoline, which loads ESP from
114 * EBP, pops EBP, and returns. */
115 asm(".globl _stack_allocation_recover; \
117 _stack_allocation_recover: \
118 lea -48(%rbp), %rsp; \
128 void open_stack_allocation(x86_thread_state64_t
*context
)
130 void stack_allocation_recover(void);
132 push_context(context
->rip
, context
);
133 push_context(context
->rbp
, context
);
134 context
->rbp
= context
->rsp
;
136 push_context(context
->r9
, context
);
137 push_context(context
->r8
, context
);
138 push_context(context
->rcx
, context
);
139 push_context(context
->rdx
, context
);
140 push_context(context
->rsi
, context
);
141 push_context(context
->rdi
, context
);
143 context
->rip
= (u64
) stack_allocation_recover
;
145 align_context_stack(context
);
148 /* Stack allocation of data starts with a context with a mod-16 ESP
149 * value and reserves some space on it by manipulating the ESP
151 void *stack_allocate(x86_thread_state64_t
*context
, size_t size
)
153 /* round up size to 16byte multiple */
154 size
= (size
+ 15) & -16;
156 context
->rsp
= ((u64
)context
->rsp
) - size
;
158 return (void *)context
->rsp
;
161 /* Arranging to invoke a C function is tricky, as we have to assume
162 * cdecl calling conventions (caller removes args) and x86/darwin
163 * alignment requirements. The simplest way to arrange this,
164 * actually, is to open a new stack allocation.
165 * WARNING!!! THIS DOES NOT PRESERVE REGISTERS! */
166 void call_c_function_in_context(x86_thread_state64_t
*context
,
175 /* Set up to restore stack on exit. */
176 open_stack_allocation(context
);
178 /* Have to keep stack 16byte aligned on x86/darwin. */
179 for (i
= (1 & -nargs
); i
; i
--) {
180 push_context(0, context
);
183 context
->rsp
= ((u64
)context
->rsp
) - nargs
* 8;
184 stack_pointer
= (u64
*)context
->rsp
;
187 if (nargs
> 0) context
->rdi
= va_arg(ap
, u64
);
188 if (nargs
> 1) context
->rsi
= va_arg(ap
, u64
);
189 if (nargs
> 2) context
->rdx
= va_arg(ap
, u64
);
190 if (nargs
> 3) context
->rcx
= va_arg(ap
, u64
);
191 if (nargs
> 4) context
->r8
= va_arg(ap
, u64
);
192 if (nargs
> 5) context
->r9
= va_arg(ap
, u64
);
193 for (i
= 6; i
< nargs
; i
++) {
194 stack_pointer
[i
] = va_arg(ap
, u64
);
198 push_context(context
->rip
, context
);
199 context
->rip
= (u64
) function
;
202 void signal_emulation_wrapper(x86_thread_state64_t
*thread_state
,
203 x86_float_state64_t
*float_state
,
206 void (*handler
)(int, siginfo_t
*, void *))
209 darwin_ucontext context
;
210 darwin_mcontext regs
;
212 context
.uc_mcontext
= ®s
;
214 /* when BSD signals are fired, they mask they signals in sa_mask
215 which always seem to be the blockable_sigset, for us, so we
217 1) save the current sigmask
218 2) block blockable signals
219 3) call the signal handler
220 4) restore the sigmask */
222 build_fake_signal_context(&context
, thread_state
, float_state
);
224 block_blockable_signals(0);
226 handler(signal
, siginfo
, &context
);
228 update_thread_state_from_context(thread_state
, float_state
, &context
);
230 /* Trap to restore the signal context. */
231 asm volatile (".quad 0xffffffffffff0b0f"
232 : : "a" (thread_state
), "b" (float_state
));
235 #if defined DUMP_CONTEXT
236 void dump_context(x86_thread_state64_t
*context
)
241 printf("rax: %08lx rcx: %08lx rdx: %08lx rbx: %08lx\n",
242 context
->rax
, context
->rcx
, context
->rdx
, context
->rbx
);
243 printf("rsp: %08lx rbp: %08lx rsi: %08lx rdi: %08lx\n",
244 context
->rsp
, context
->rbp
, context
->rsi
, context
->rdi
);
245 printf("rip: %08lx eflags: %08lx\n",
246 context
->rip
, context
->rflags
);
247 printf("cs: %04hx ds: %04hx es: %04hx "
248 "ss: %04hx fs: %04hx gs: %04hx\n",
249 context
->cs
, context
->ds
, context
->rs
,
250 context
->ss
, context
->fs
, context
->gs
);
252 stack_pointer
= (u64
*)context
->rsp
;
253 for (i
= 0; i
< 48; i
+=4) {
254 printf("%08x: %08x %08x %08x %08x\n",
255 context
->rsp
+ (i
* 4),
265 control_stack_exhausted_handler(int signal
, siginfo_t
*siginfo
,
266 os_context_t
*context
) {
267 extern void unblock_signals_in_context_and_maybe_warn(os_context_t
*);
268 unblock_signals_in_context_and_maybe_warn(context
);
269 arrange_return_to_lisp_function
270 (context
, StaticSymbolFunction(CONTROL_STACK_EXHAUSTED_ERROR
));
274 undefined_alien_handler(int signal
, siginfo_t
*siginfo
, os_context_t
*context
) {
275 arrange_return_to_lisp_function
276 (context
, StaticSymbolFunction(UNDEFINED_ALIEN_VARIABLE_ERROR
));
280 catch_exception_raise(mach_port_t exception_port
,
283 exception_type_t exception
,
284 exception_data_t code_vector
,
285 mach_msg_type_number_t code_count
)
287 kern_return_t ret
= KERN_SUCCESS
, dealloc_ret
;
288 int signal
, rip_offset
= 0;
290 void (*handler
)(int, siginfo_t
*, os_context_t
*);
292 #ifdef LISP_FEATURE_SB_THREAD
293 thread_mutex_lock(&mach_exception_lock
);
296 x86_thread_state64_t thread_state
;
297 mach_msg_type_number_t thread_state_count
= x86_THREAD_STATE64_COUNT
;
299 #ifdef x86_AVX_STATE64_COUNT
300 x86_avx_state64_t float_state
;
301 mach_msg_type_number_t float_state_count
= avx_supported
? x86_AVX_STATE64_COUNT
: x86_FLOAT_STATE64_COUNT
;
302 x86_avx_state64_t
*target_float_state
;
303 int float_state_flavor
= avx_supported
? x86_AVX_STATE64
: x86_FLOAT_STATE64
;
305 x86_float_state64_t float_state
;
306 mach_msg_type_number_t float_state_count
= x86_FLOAT_STATE64_COUNT
;
307 x86_float_state64_t
*target_float_state
;
308 int float_state_flavor
= x86_FLOAT_STATE64
;
311 x86_exception_state64_t exception_state
;
312 mach_msg_type_number_t exception_state_count
= x86_EXCEPTION_STATE64_COUNT
;
314 x86_thread_state64_t backup_thread_state
;
315 x86_thread_state64_t
*target_thread_state
;
317 os_vm_address_t addr
;
321 FSHOW((stderr
,"/entering catch_exception_raise with exception: %d\n", exception
));
322 if (mach_port_get_context(mach_task_self(), exception_port
, (mach_vm_address_t
*)&th
)
324 lose("Can't find the thread for an exception %p", exception_port
);
326 thread_get_state(thread
, x86_THREAD_STATE64
,
327 (thread_state_t
)&thread_state
, &thread_state_count
);
328 thread_get_state(thread
, float_state_flavor
,
329 (thread_state_t
)&float_state
, &float_state_count
);
330 thread_get_state(thread
, x86_EXCEPTION_STATE64
,
331 (thread_state_t
)&exception_state
, &exception_state_count
);
333 if (code_count
&& exception
== EXC_BAD_ACCESS
&& code_vector
[0] == EXC_I386_GPFLT
) {
334 /* This can happen for addresses larger than 48 bits,
335 resulting in bogus faultvaddr. */
338 addr
= (void*)exception_state
.faultvaddr
;
345 if(addr
>= CONTROL_STACK_RETURN_GUARD_PAGE(th
) &&
346 addr
< CONTROL_STACK_RETURN_GUARD_PAGE(th
) + os_vm_page_size
) {
347 /* We're returning from the guard page: reprotect it, and
348 * unprotect this one. This works even if we somehow missed
349 * the return-guard-page, and hit it on our way to new
350 * exhaustion instead. */
351 reset_thread_control_stack_guard_page(th
);
355 /* note the os_context hackery here. When the signal handler returns,
356 * it won't go back to what it was doing ... */
357 if(addr
>= CONTROL_STACK_GUARD_PAGE(th
) &&
358 addr
< CONTROL_STACK_GUARD_PAGE(th
) + os_vm_page_size
) {
359 /* We hit the end of the control stack: disable guard page
360 * protection so the error handler has some headroom, protect the
361 * previous page so that we can catch returns from the guard page
363 lower_thread_control_stack_guard_page(th
);
364 handler
= control_stack_exhausted_handler
;
366 else if (addr
>= undefined_alien_address
&&
367 addr
< undefined_alien_address
+ os_vm_page_size
) {
368 handler
= undefined_alien_handler
;
370 handler
= memory_fault_handler
;
373 case EXC_BAD_INSTRUCTION
:
375 if (*((u64
*)thread_state
.rip
) == 0xffffffffffff0b0f) {
376 /* fake sigreturn. */
378 /* When we get here, thread_state.rax is a pointer to a
379 * thread_state to restore. */
380 /* thread_state = *((thread_state_t *)thread_state.rax); */
382 thread_set_state(thread
, x86_THREAD_STATE64
,
383 (thread_state_t
) thread_state
.rax
, thread_state_count
);
384 thread_set_state(thread
, float_state_flavor
,
385 (thread_state_t
) thread_state
.rbx
, float_state_count
);
387 } else if (*((unsigned short *)thread_state
.rip
) == 0x0b0f) {
390 handler
= sigtrap_handler
;
393 handler
= sigill_handler
;
399 ret
= KERN_INVALID_RIGHT
;
403 backup_thread_state
= thread_state
;
405 /* The ABI has a 128-byte red zone. */
406 stack_allocate(&thread_state
, 128);
408 open_stack_allocation(&thread_state
);
409 /* Reserve a 256 byte zone for signal handlers
410 * to use on the interrupted thread stack.
412 stack_allocate(&thread_state
, 256);
414 /* Save thread state */
415 target_thread_state
= stack_allocate(&thread_state
, sizeof(*target_thread_state
));
416 (*target_thread_state
) = backup_thread_state
;
418 target_thread_state
->rip
+= rip_offset
;
419 /* Save float state */
420 target_float_state
= stack_allocate(&thread_state
, sizeof(*target_float_state
));
421 (*target_float_state
) = float_state
;
424 siginfo
= stack_allocate(&thread_state
, sizeof(*siginfo
));
426 siginfo
->si_signo
= signal
;
427 siginfo
->si_addr
= addr
;
429 call_c_function_in_context(&thread_state
,
430 signal_emulation_wrapper
,
437 thread_set_state(thread
, x86_THREAD_STATE64
,
438 (thread_state_t
)&thread_state
, thread_state_count
);
439 thread_set_state(thread
, float_state_flavor
,
440 (thread_state_t
)&float_state
, float_state_count
);
442 #ifdef LISP_FEATURE_SB_THREAD
443 thread_mutex_unlock(&mach_exception_lock
);
446 dealloc_ret
= mach_port_deallocate (mach_task_self(), thread
);
448 lose("mach_port_deallocate (thread) failed with return_code %d\n", dealloc_ret
);
451 dealloc_ret
= mach_port_deallocate (mach_task_self(), task
);
453 lose("mach_port_deallocate (task) failed with return_code %d\n", dealloc_ret
);
460 os_restore_fp_control(os_context_t
*context
)
462 /* KLUDGE: The x87 FPU control word is some nasty bitfield struct
463 * thing. Rather than deal with that, just grab it as a 16-bit
465 unsigned short fpu_control_word
=
466 *((unsigned short *)&context
->uc_mcontext
->fs
.fpu_fcw
);
467 /* reset exception flags and restore control flags on SSE2 FPU */
468 unsigned int temp
= (context
->uc_mcontext
->fs
.fpu_mxcsr
) & ~0x3F;
469 asm ("ldmxcsr %0" : : "m" (temp
));
470 /* same for x87 FPU. */
471 asm ("fldcw %0" : : "m" (fpu_control_word
));
476 os_context_register_t
*
477 os_context_float_register_addr(os_context_t
*context
, int offset
)
479 return (os_context_register_t
*)((&context
->uc_mcontext
->__fs
.__fpu_xmm0
) + offset
);