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 /* KLUDGE: There is strong family resemblance in the signal context
23 * stuff in FreeBSD and OpenBSD, but in detail they're different in
24 * almost every line of code. It would be nice to find some way to
25 * factor out the commonality better; failing that, it might be best
26 * just to split this generic-BSD code into one variant for each BSD.
28 * KLUDGE II: this split has begun with the addition of the Darwin BSD
29 * flavour, with the cross-architecture complications that this
30 * entails; unfortunately, currently the situation is worse, not
31 * better, than in the above paragraph. */
33 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
35 os_context_register_addr(os_context_t *context, int offset)
39 return CONTEXT_ADDR_FROM_STEM(eax);
41 return CONTEXT_ADDR_FROM_STEM(ecx);
43 return CONTEXT_ADDR_FROM_STEM(edx);
45 return CONTEXT_ADDR_FROM_STEM(ebx);
47 return CONTEXT_ADDR_FROM_STEM(esp);
49 return CONTEXT_ADDR_FROM_STEM(ebp);
51 return CONTEXT_ADDR_FROM_STEM(esi);
53 return CONTEXT_ADDR_FROM_STEM(edi);
60 os_context_sp_addr(os_context_t *context)
62 return CONTEXT_ADDR_FROM_STEM(esp);
65 #endif /* __FreeBSD__ || __OpenBSD__ */
69 os_context_register_addr(os_context_t *context, int offset)
73 return CONTEXT_ADDR_FROM_STEM(EAX);
75 return CONTEXT_ADDR_FROM_STEM(ECX);
77 return CONTEXT_ADDR_FROM_STEM(EDX);
79 return CONTEXT_ADDR_FROM_STEM(EBX);
81 return CONTEXT_ADDR_FROM_STEM(ESP);
83 return CONTEXT_ADDR_FROM_STEM(EBP);
85 return CONTEXT_ADDR_FROM_STEM(ESI);
87 return CONTEXT_ADDR_FROM_STEM(EDI);
89 return CONTEXT_ADDR_FROM_STEM(UESP);
96 os_context_sp_addr(os_context_t *context)
98 return &(_UC_MACHINE_SP(context));
101 #endif /* __NetBSD__ */
104 /* FIXME: If this can be a no-op on BSD/x86, then it
105 * deserves a more precise name.
107 * (Perhaps os_prepare_data_area_to_be_executed()?) */
109 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
113 /* Note: the Darwin versions of arch_os_thread_init found in
116 #if !defined(LISP_FEATURE_DARWIN)
118 #ifdef LISP_FEATURE_SB_THREAD
120 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
122 desc->sd_lolimit = (size - 1) & 0xffff;
123 desc->sd_hilimit = ((size - 1) >> 16) &0xf;
126 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
128 desc->sd_lobase = (unsigned int)addr & 0xffffff;
129 desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
134 int arch_os_thread_init(struct thread *thread) {
136 #ifdef LISP_FEATURE_SB_THREAD
140 struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
143 set_data_desc_addr(&ldt_entry, (unsigned long) thread);
144 set_data_desc_size(&ldt_entry, dynamic_values_bytes);
146 n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
148 perror("i386_set_ldt");
149 lose("unexpected i386_set_ldt(..) failure\n");
151 FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
152 sel = LSEL(n, SEL_UPL);
153 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
155 thread->tls_cookie=n;
156 pthread_setspecific(specials,thread);
159 #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);
171 return 1; /* success */
174 int arch_os_thread_cleanup(struct thread *thread) {
176 #if defined(LISP_FEATURE_SB_THREAD)
177 int n = thread->tls_cookie;
179 /* Set the %%fs register back to 0 and free the the ldt
180 * by setting it to NULL.
182 FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
184 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
185 i386_set_ldt(n, NULL, 1);
188 return 1; /* success */
191 #endif /* !LISP_FEATURE_DARWIN */
193 #if defined(LISP_FEATURE_FREEBSD)
195 os_restore_fp_control(os_context_t *context)
197 /* FPU state is saved per context on post-KSE systems.
198 * On earlier systems, it is shared in a whole process.
200 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
201 struct envxmm *ex = (struct envxmm*)(&context->uc_mcontext.mc_fpstate);
202 asm ("fldcw %0" : : "m" (ex->en_cw));