7 #if defined(LISP_FEATURE_FREEBSD)
8 #include <machine/fpu.h>
11 #if defined(LISP_FEATURE_OPENBSD)
12 #include <machine/fpu.h>
15 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
16 #include <mach/mach.h>
18 kern_return_t mach_thread_init(mach_port_t thread_exception_port);
21 /* KLUDGE: There is strong family resemblance in the signal context
22 * stuff in FreeBSD and OpenBSD, but in detail they're different in
23 * almost every line of code. It would be nice to find some way to
24 * factor out the commonality better; failing that, it might be best
25 * just to split this generic-BSD code into one variant for each BSD.
27 * KLUDGE II: this split has begun with the addition of the Darwin BSD
28 * flavour, with the cross-architecture complications that this
29 * entails; unfortunately, currently the situation is worse, not
30 * better, than in the above paragraph. */
32 #if defined(LISP_FEATURE_FREEBSD) || defined(LISP_FEATURE_DARWIN) || defined(LISP_FEATURE_OPENBSD)
33 os_context_register_t *
34 os_context_register_addr(os_context_t *context, int offset)
38 return CONTEXT_ADDR_FROM_STEM(rax);
40 return CONTEXT_ADDR_FROM_STEM(rcx);
42 return CONTEXT_ADDR_FROM_STEM(rdx);
44 return CONTEXT_ADDR_FROM_STEM(rbx);
46 return CONTEXT_ADDR_FROM_STEM(rsp);
48 return CONTEXT_ADDR_FROM_STEM(rbp);
50 return CONTEXT_ADDR_FROM_STEM(rsi);
52 return CONTEXT_ADDR_FROM_STEM(rdi);
54 return CONTEXT_ADDR_FROM_STEM(r8);
56 return CONTEXT_ADDR_FROM_STEM(r9);
58 return CONTEXT_ADDR_FROM_STEM(r10);
60 return CONTEXT_ADDR_FROM_STEM(r11);
62 return CONTEXT_ADDR_FROM_STEM(r12);
64 return CONTEXT_ADDR_FROM_STEM(r13);
66 return CONTEXT_ADDR_FROM_STEM(r14);
68 return CONTEXT_ADDR_FROM_STEM(r15);
74 os_context_register_t *
75 os_context_sp_addr(os_context_t *context)
77 return CONTEXT_ADDR_FROM_STEM(rsp);
80 os_context_register_t *
81 os_context_pc_addr(os_context_t *context)
83 return CONTEXT_ADDR_FROM_STEM(rip);
86 #elif defined(LISP_FEATURE_NETBSD)
87 os_context_register_t *
88 os_context_register_addr(os_context_t *context, int offset)
92 return CONTEXT_ADDR_FROM_STEM(RAX);
94 return CONTEXT_ADDR_FROM_STEM(RCX);
96 return CONTEXT_ADDR_FROM_STEM(RDX);
98 return CONTEXT_ADDR_FROM_STEM(RBX);
100 return CONTEXT_ADDR_FROM_STEM(RSP);
102 return CONTEXT_ADDR_FROM_STEM(RBP);
104 return CONTEXT_ADDR_FROM_STEM(RSI);
106 return CONTEXT_ADDR_FROM_STEM(RDI);
108 return CONTEXT_ADDR_FROM_STEM(R8);
110 return CONTEXT_ADDR_FROM_STEM(R9);
112 return CONTEXT_ADDR_FROM_STEM(R10);
114 return CONTEXT_ADDR_FROM_STEM(R11);
116 return CONTEXT_ADDR_FROM_STEM(R12);
118 return CONTEXT_ADDR_FROM_STEM(R13);
120 return CONTEXT_ADDR_FROM_STEM(R14);
122 return CONTEXT_ADDR_FROM_STEM(R15);
128 os_context_register_t *
129 os_context_sp_addr(os_context_t *context)
131 return CONTEXT_ADDR_FROM_STEM(RSP);
134 os_context_register_t *
135 os_context_pc_addr(os_context_t *context)
137 return CONTEXT_ADDR_FROM_STEM(RIP);
143 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
147 int arch_os_thread_init(struct thread *thread) {
149 #ifdef LISP_FEATURE_SB_THREAD
150 #ifdef LISP_FEATURE_GCC_TLS
151 current_thread = thread;
153 pthread_setspecific(specials,thread);
157 #ifdef LISP_FEATURE_MACH_EXCEPTION_HANDLER
158 mach_thread_init(THREAD_STRUCT_TO_EXCEPTION_PORT(thread));
161 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
162 /* Signal handlers are run on the control stack, so if it is exhausted
163 * we had better use an alternate stack for whatever signal tells us
164 * we've exhausted it */
165 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
167 sigstack.ss_size = 32*SIGSTKSZ;
168 sigaltstack(&sigstack,0);
170 return 1; /* success */
173 int arch_os_thread_cleanup(struct thread *thread) {
174 return 1; /* success */
177 #if defined(LISP_FEATURE_FREEBSD)
179 os_restore_fp_control(os_context_t *context)
181 struct envxmm *ex = (struct envxmm*)(&context->uc_mcontext.mc_fpstate);
182 /* reset exception flags and restore control flags on SSE2 FPU */
183 unsigned int temp = (ex->en_mxcsr) & ~0x3F;
184 asm ("ldmxcsr %0" : : "m" (temp));
185 /* same for x87 FPU. */
186 asm ("fldcw %0" : : "m" (ex->en_cw));
190 #if defined(LISP_FEATURE_OPENBSD)
192 os_restore_fp_control(os_context_t *context)
194 if (context->sc_fpstate != NULL) {
195 u_int32_t mxcsr = context->sc_fpstate->fx_mxcsr & ~0x3F;
196 u_int16_t cw = context->sc_fpstate->fx_fcw;
197 asm ("ldmxcsr %0" : : "m" (mxcsr));
198 asm ("fldcw %0" : : "m" (cw));