7 #ifdef LISP_FEATURE_SB_THREAD
8 #ifdef LISP_FEATURE_DARWIN
9 #include <architecture/i386/table.h>
10 #include <i386/user_ldt.h>
11 #include <mach/mach_init.h>
13 #include <machine/segments.h>
14 #include <machine/sysarch.h>
15 #endif /* LISP_FEATURE_DARWIN */
18 #if defined(LISP_FEATURE_FREEBSD)
19 #include "machine/npx.h"
22 #if defined(LISP_FEATURE_OPENBSD)
23 #include <machine/npx.h>
25 #include "openbsd-sigcontext.h"
26 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
27 # include <machine/frame.h>
31 /* KLUDGE: There is strong family resemblance in the signal context
32 * stuff in FreeBSD and OpenBSD, but in detail they're different in
33 * almost every line of code. It would be nice to find some way to
34 * factor out the commonality better; failing that, it might be best
35 * just to split this generic-BSD code into one variant for each BSD.
37 * KLUDGE II: this split has begun with the addition of the Darwin BSD
38 * flavour, with the cross-architecture complications that this
39 * entails; unfortunately, currently the situation is worse, not
40 * better, than in the above paragraph. */
42 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
44 os_context_register_addr(os_context_t *context, int offset)
48 return (int *)CONTEXT_ADDR_FROM_STEM(eax);
50 return (int *)CONTEXT_ADDR_FROM_STEM(ecx);
52 return (int *)CONTEXT_ADDR_FROM_STEM(edx);
54 return (int *)CONTEXT_ADDR_FROM_STEM(ebx);
56 return (int *)CONTEXT_ADDR_FROM_STEM(esp);
58 return (int *)CONTEXT_ADDR_FROM_STEM(ebp);
60 return (int *)CONTEXT_ADDR_FROM_STEM(esi);
62 return (int *)CONTEXT_ADDR_FROM_STEM(edi);
69 os_context_sp_addr(os_context_t *context)
71 return (int *)CONTEXT_ADDR_FROM_STEM(esp);
74 #endif /* __FreeBSD__ || __OpenBSD__ */
78 os_context_register_addr(os_context_t *context, int offset)
82 return CONTEXT_ADDR_FROM_STEM(EAX);
84 return CONTEXT_ADDR_FROM_STEM(ECX);
86 return CONTEXT_ADDR_FROM_STEM(EDX);
88 return CONTEXT_ADDR_FROM_STEM(EBX);
90 return CONTEXT_ADDR_FROM_STEM(ESP);
92 return CONTEXT_ADDR_FROM_STEM(EBP);
94 return CONTEXT_ADDR_FROM_STEM(ESI);
96 return CONTEXT_ADDR_FROM_STEM(EDI);
98 return CONTEXT_ADDR_FROM_STEM(UESP);
105 os_context_sp_addr(os_context_t *context)
107 return &(_UC_MACHINE_SP(context));
110 #endif /* __NetBSD__ */
112 int *os_context_pc_addr(os_context_t *context)
114 #if defined __FreeBSD__
115 return CONTEXT_ADDR_FROM_STEM(eip);
116 #elif defined __OpenBSD__
117 return CONTEXT_ADDR_FROM_STEM(pc);
118 #elif defined __NetBSD__
119 return CONTEXT_ADDR_FROM_STEM(EIP);
120 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
121 return (int *)CONTEXT_ADDR_FROM_STEM(eip);
122 #elif defined LISP_FEATURE_DARWIN
123 return &context->uc_mcontext->ss.srr0;
125 #error unsupported BSD variant
129 /* FIXME: If this can be a no-op on BSD/x86, then it
130 * deserves a more precise name.
132 * (Perhaps os_prepare_data_area_to_be_executed()?) */
134 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
138 /* Note: the Darwin versions of arch_os_thread_init found in
141 #if !defined(LISP_FEATURE_DARWIN)
143 #ifdef LISP_FEATURE_SB_THREAD
145 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
147 desc->sd_lolimit = (size - 1) & 0xffff;
148 desc->sd_hilimit = ((size - 1) >> 16) &0xf;
151 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
153 desc->sd_lobase = (unsigned int)addr & 0xffffff;
154 desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
159 int arch_os_thread_init(struct thread *thread) {
161 #ifdef LISP_FEATURE_SB_THREAD
165 struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
168 set_data_desc_addr(&ldt_entry, thread);
169 set_data_desc_size(&ldt_entry, dynamic_values_bytes);
171 n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
173 perror("i386_set_ldt");
174 lose("unexpected i386_set_ldt(..) failure\n");
176 FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
177 sel = LSEL(n, SEL_UPL);
180 thread->tls_cookie=n;
181 #ifdef LISP_FEATURE_GCC_TLS
182 current_thread = thread;
184 pthread_setspecific(specials,thread);
188 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
191 /* Signal handlers are run on the control stack, so if it is exhausted
192 * we had better use an alternate stack for whatever signal tells us
193 * we've exhausted it */
194 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
196 sigstack.ss_size = 32*SIGSTKSZ;
197 sigaltstack(&sigstack,0);
200 return 1; /* success */
203 int arch_os_thread_cleanup(struct thread *thread) {
205 #if defined(LISP_FEATURE_SB_THREAD)
206 int n = thread->tls_cookie;
208 /* Set the %%fs register back to 0 and free the ldt by setting it
211 FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
213 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
214 i386_set_ldt(n, NULL, 1);
217 return 1; /* success */
220 #endif /* !LISP_FEATURE_DARWIN */
222 #if defined(LISP_FEATURE_FREEBSD)
223 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
225 os_restore_tls_segment_register(os_context_t *context)
227 load_fs(context->uc_mcontext.mc_fs);
232 os_restore_fp_control(os_context_t *context)
234 /* FPU state is saved per context on post-KSE systems.
235 * On earlier systems, it is shared in a whole process.
237 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
238 struct envxmm *ex = (struct envxmm *)(context->uc_mcontext.mc_fpstate);
239 __asm__ __volatile__ ("fldcw %0" : : "m" (ex->en_cw));
241 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
242 /* Calling this function here may not be good idea. Or rename
243 * function name os_restore_fp_control to os_restore_context or
244 * so, to match the behavior? */
245 os_restore_tls_segment_register(context);
250 #if defined(LISP_FEATURE_OPENBSD)
252 os_restore_fp_control(os_context_t *context)
254 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
255 struct sigframe *frame = (struct sigframe *)((char*)context -
256 offsetof(struct sigframe, sf_sc));
257 union savefpu *fpu = frame->sf_fpstate;
258 #elif defined(OS_OPENBSD_FPSTATE_IN_SIGCONTEXT)
259 union savefpu *fpu = context->sc_fpstate;
262 if (openbsd_use_fxsave)
263 __asm__ __volatile__ ("fldcw %0" : : "m" (fpu->sv_xmm.sv_env.en_cw));
265 __asm__ __volatile__ ("fldcw %0" : : "m" (fpu->sv_87.sv_env.en_cw));