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 (int *)CONTEXT_ADDR_FROM_STEM(eax);
41 return (int *)CONTEXT_ADDR_FROM_STEM(ecx);
43 return (int *)CONTEXT_ADDR_FROM_STEM(edx);
45 return (int *)CONTEXT_ADDR_FROM_STEM(ebx);
47 return (int *)CONTEXT_ADDR_FROM_STEM(esp);
49 return (int *)CONTEXT_ADDR_FROM_STEM(ebp);
51 return (int *)CONTEXT_ADDR_FROM_STEM(esi);
53 return (int *)CONTEXT_ADDR_FROM_STEM(edi);
60 os_context_sp_addr(os_context_t *context)
62 return (int *)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__ */
103 int *os_context_pc_addr(os_context_t *context)
105 #if defined __FreeBSD__
106 return CONTEXT_ADDR_FROM_STEM(eip);
107 #elif defined __OpenBSD__
108 return CONTEXT_ADDR_FROM_STEM(pc);
109 #elif defined __NetBSD__
110 return CONTEXT_ADDR_FROM_STEM(EIP);
111 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
112 return (int *)CONTEXT_ADDR_FROM_STEM(eip);
113 #elif defined LISP_FEATURE_DARWIN
114 return &context->uc_mcontext->ss.srr0;
116 #error unsupported BSD variant
120 /* FIXME: If this can be a no-op on BSD/x86, then it
121 * deserves a more precise name.
123 * (Perhaps os_prepare_data_area_to_be_executed()?) */
125 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
129 /* Note: the Darwin versions of arch_os_thread_init found in
132 #if !defined(LISP_FEATURE_DARWIN)
134 #ifdef LISP_FEATURE_SB_THREAD
136 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
138 desc->sd_lolimit = (size - 1) & 0xffff;
139 desc->sd_hilimit = ((size - 1) >> 16) &0xf;
142 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
144 desc->sd_lobase = (unsigned int)addr & 0xffffff;
145 desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
150 int arch_os_thread_init(struct thread *thread) {
152 #ifdef LISP_FEATURE_SB_THREAD
156 struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
159 set_data_desc_addr(&ldt_entry, (unsigned long) thread);
160 set_data_desc_size(&ldt_entry, dynamic_values_bytes);
162 n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
164 perror("i386_set_ldt");
165 lose("unexpected i386_set_ldt(..) failure\n");
167 FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
168 sel = LSEL(n, SEL_UPL);
171 thread->tls_cookie=n;
172 pthread_setspecific(specials,thread);
175 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
178 /* Signal handlers are run on the control stack, so if it is exhausted
179 * we had better use an alternate stack for whatever signal tells us
180 * we've exhausted it */
181 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
183 sigstack.ss_size = 32*SIGSTKSZ;
184 sigaltstack(&sigstack,0);
187 return 1; /* success */
190 int arch_os_thread_cleanup(struct thread *thread) {
192 #if defined(LISP_FEATURE_SB_THREAD)
193 int n = thread->tls_cookie;
195 /* Set the %%fs register back to 0 and free the the ldt
196 * by setting it to NULL.
198 FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
200 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
201 i386_set_ldt(n, NULL, 1);
204 return 1; /* success */
207 #endif /* !LISP_FEATURE_DARWIN */
209 #if defined(LISP_FEATURE_FREEBSD)
211 os_restore_fp_control(os_context_t *context)
213 /* FPU state is saved per context on post-KSE systems.
214 * On earlier systems, it is shared in a whole process.
216 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
217 struct envxmm *ex = (struct envxmm*)(&context->uc_mcontext.mc_fpstate);
218 asm ("fldcw %0" : : "m" (ex->en_cw));