8 #ifdef LISP_FEATURE_SB_THREAD
9 #ifdef LISP_FEATURE_DARWIN
10 #include <architecture/i386/table.h>
11 #include <i386/user_ldt.h>
12 #include <mach/mach_init.h>
14 #include <machine/segments.h>
15 #include <machine/sysarch.h>
16 #endif /* LISP_FEATURE_DARWIN */
19 #if defined(LISP_FEATURE_FREEBSD) || defined(LISP_FEATURE_DRAGONFLY)
20 #include "machine/npx.h"
23 #if defined(LISP_FEATURE_OPENBSD)
24 #include <machine/npx.h>
26 #include "openbsd-sigcontext.h"
27 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
28 # include <machine/frame.h>
32 /* KLUDGE: There is strong family resemblance in the signal context
33 * stuff in FreeBSD and OpenBSD, but in detail they're different in
34 * almost every line of code. It would be nice to find some way to
35 * factor out the commonality better; failing that, it might be best
36 * just to split this generic-BSD code into one variant for each BSD.
38 * KLUDGE II: this split has begun with the addition of the Darwin BSD
39 * flavour, with the cross-architecture complications that this
40 * entails; unfortunately, currently the situation is worse, not
41 * better, than in the above paragraph. */
43 #if defined(LISP_FEATURE_FREEBSD) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN) || defined(__DragonFly__)
45 os_context_register_addr(os_context_t
*context
, int offset
)
49 return (int *)CONTEXT_ADDR_FROM_STEM(eax
);
51 return (int *)CONTEXT_ADDR_FROM_STEM(ecx
);
53 return (int *)CONTEXT_ADDR_FROM_STEM(edx
);
55 return (int *)CONTEXT_ADDR_FROM_STEM(ebx
);
57 return (int *)CONTEXT_ADDR_FROM_STEM(esp
);
59 return (int *)CONTEXT_ADDR_FROM_STEM(ebp
);
61 return (int *)CONTEXT_ADDR_FROM_STEM(esi
);
63 return (int *)CONTEXT_ADDR_FROM_STEM(edi
);
70 os_context_sp_addr(os_context_t
*context
)
72 return (int *)CONTEXT_ADDR_FROM_STEM(esp
);
75 #endif /* LISP_FEATURE_FREEBSD || __OpenBSD__ || __DragonFly__ */
79 os_context_register_addr(os_context_t
*context
, int offset
)
83 return CONTEXT_ADDR_FROM_STEM(EAX
);
85 return CONTEXT_ADDR_FROM_STEM(ECX
);
87 return CONTEXT_ADDR_FROM_STEM(EDX
);
89 return CONTEXT_ADDR_FROM_STEM(EBX
);
91 return CONTEXT_ADDR_FROM_STEM(ESP
);
93 return CONTEXT_ADDR_FROM_STEM(EBP
);
95 return CONTEXT_ADDR_FROM_STEM(ESI
);
97 return CONTEXT_ADDR_FROM_STEM(EDI
);
99 return CONTEXT_ADDR_FROM_STEM(UESP
);
106 os_context_sp_addr(os_context_t
*context
)
108 return &(_UC_MACHINE_SP(context
));
111 #endif /* __NetBSD__ */
113 int *os_context_pc_addr(os_context_t
*context
)
115 #if defined(LISP_FEATURE_FREEBSD) || defined(__DragonFly__)
116 return CONTEXT_ADDR_FROM_STEM(eip
);
117 #elif defined __OpenBSD__
118 return CONTEXT_ADDR_FROM_STEM(pc
);
119 #elif defined __NetBSD__
120 return CONTEXT_ADDR_FROM_STEM(EIP
);
121 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
122 return (int *)CONTEXT_ADDR_FROM_STEM(eip
);
123 #elif defined LISP_FEATURE_DARWIN
124 return &context
->uc_mcontext
->ss
.srr0
;
126 #error unsupported BSD variant
130 /* FIXME: If this can be a no-op on BSD/x86, then it
131 * deserves a more precise name.
133 * (Perhaps os_prepare_data_area_to_be_executed()?) */
135 os_flush_icache(os_vm_address_t address
, os_vm_size_t length
)
139 /* Note: the Darwin versions of arch_os_thread_init found in
142 #if !defined(LISP_FEATURE_DARWIN)
144 #ifdef LISP_FEATURE_SB_THREAD
146 void set_data_desc_size(struct segment_descriptor
* desc
, unsigned long size
)
148 desc
->sd_lolimit
= (size
- 1) & 0xffff;
149 desc
->sd_hilimit
= ((size
- 1) >> 16) &0xf;
152 void set_data_desc_addr(struct segment_descriptor
* desc
, void* addr
)
154 desc
->sd_lobase
= (unsigned int)addr
& 0xffffff;
155 desc
->sd_hibase
= ((unsigned int)addr
& 0xff000000) >> 24;
160 #ifdef LISP_FEATURE_SB_THREAD
162 arch_os_load_ldt(struct thread
*thread
)
164 int sel
= LSEL(thread
->tls_cookie
, SEL_UPL
);
165 unsigned int fs
= rfs();
167 /* Load FS only if it's necessary. Modifying a selector
168 * causes privilege checking and it takes long time. */
174 int arch_os_thread_init(struct thread
*thread
) {
176 #ifdef LISP_FEATURE_SB_THREAD
179 struct segment_descriptor ldt_entry
= { 0, 0, SDT_MEMRW
, SEL_UPL
, 1,
182 set_data_desc_addr(&ldt_entry
, thread
);
183 set_data_desc_size(&ldt_entry
, dynamic_values_bytes
);
185 n
= i386_set_ldt(LDT_AUTO_ALLOC
, (union descriptor
*) &ldt_entry
, 1);
187 perror("i386_set_ldt");
188 lose("unexpected i386_set_ldt(..) failure\n");
190 FSHOW_SIGNAL((stderr
, "/ TLS: Allocated LDT %x\n", n
));
191 thread
->tls_cookie
=n
;
192 arch_os_load_ldt(thread
);
194 #ifdef LISP_FEATURE_GCC_TLS
195 current_thread
= thread
;
197 pthread_setspecific(specials
,thread
);
202 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
205 /* Signal handlers are run on the control stack, so if it is exhausted
206 * we had better use an alternate stack for whatever signal tells us
207 * we've exhausted it */
208 sigstack
.ss_sp
=((void *) thread
)+dynamic_values_bytes
;
210 sigstack
.ss_size
= 32*SIGSTKSZ
;
211 sigaltstack(&sigstack
,0);
214 return 1; /* success */
217 int arch_os_thread_cleanup(struct thread
*thread
) {
219 #if defined(LISP_FEATURE_SB_THREAD)
220 int n
= thread
->tls_cookie
;
222 /* Set the %%fs register back to 0 and free the ldt by setting it
225 FSHOW_SIGNAL((stderr
, "/ TLS: Freeing LDT %x\n", n
));
227 __asm__
__volatile__ ("mov %0, %%fs" : : "r"(0));
228 i386_set_ldt(n
, NULL
, 1);
231 return 1; /* success */
234 #endif /* !LISP_FEATURE_DARWIN */
236 #if defined(LISP_FEATURE_FREEBSD)
237 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
239 os_restore_tls_segment_register(os_context_t
*context
)
241 load_fs(context
->uc_mcontext
.mc_fs
);
246 os_restore_fp_control(os_context_t
*context
)
248 /* FPU state is saved per context on post-KSE systems.
249 * On earlier systems, it is shared in a whole process.
251 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
252 struct envxmm
*ex
= (struct envxmm
*)(context
->uc_mcontext
.mc_fpstate
);
253 __asm__
__volatile__ ("fldcw %0" : : "m" (ex
->en_cw
));
255 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
256 /* Calling this function here may not be good idea. Or rename
257 * function name os_restore_fp_control to os_restore_context or
258 * so, to match the behavior? */
259 os_restore_tls_segment_register(context
);
264 #if defined(LISP_FEATURE_DRAGONFLY)
265 void os_restore_fp_control (os_context_t
*context
)
267 struct envxmm
*ex
= (struct envxmm
*)(context
->uc_mcontext
.mc_fpregs
);
268 __asm__
__volatile__ ("fldcw %0" : : "m" (ex
->en_cw
));
270 #endif /* LISP_FEATURE_DRAGONFLY */
272 #if defined(LISP_FEATURE_OPENBSD)
274 os_restore_fp_control(os_context_t
*context
)
276 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
277 struct sigframe
*frame
= (struct sigframe
*)((char*)context
-
278 offsetof(struct sigframe
, sf_sc
));
279 union savefpu
*fpu
= frame
->sf_fpstate
;
280 #elif defined(OS_OPENBSD_FPSTATE_IN_SIGCONTEXT)
281 union savefpu
*fpu
= context
->sc_fpstate
;
284 if (openbsd_use_fxsave
)
285 __asm__
__volatile__ ("fldcw %0" : : "m" (fpu
->sv_xmm
.sv_env
.en_cw
));
287 __asm__
__volatile__ ("fldcw %0" : : "m" (fpu
->sv_87
.sv_env
.en_cw
));