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/frame.h>
24 #include <machine/npx.h>
28 /* KLUDGE: There is strong family resemblance in the signal context
29 * stuff in FreeBSD and OpenBSD, but in detail they're different in
30 * almost every line of code. It would be nice to find some way to
31 * factor out the commonality better; failing that, it might be best
32 * just to split this generic-BSD code into one variant for each BSD.
34 * KLUDGE II: this split has begun with the addition of the Darwin BSD
35 * flavour, with the cross-architecture complications that this
36 * entails; unfortunately, currently the situation is worse, not
37 * better, than in the above paragraph. */
39 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
41 os_context_register_addr(os_context_t
*context
, int offset
)
45 return (int *)CONTEXT_ADDR_FROM_STEM(eax
);
47 return (int *)CONTEXT_ADDR_FROM_STEM(ecx
);
49 return (int *)CONTEXT_ADDR_FROM_STEM(edx
);
51 return (int *)CONTEXT_ADDR_FROM_STEM(ebx
);
53 return (int *)CONTEXT_ADDR_FROM_STEM(esp
);
55 return (int *)CONTEXT_ADDR_FROM_STEM(ebp
);
57 return (int *)CONTEXT_ADDR_FROM_STEM(esi
);
59 return (int *)CONTEXT_ADDR_FROM_STEM(edi
);
66 os_context_sp_addr(os_context_t
*context
)
68 return (int *)CONTEXT_ADDR_FROM_STEM(esp
);
71 #endif /* __FreeBSD__ || __OpenBSD__ */
75 os_context_register_addr(os_context_t
*context
, int offset
)
79 return CONTEXT_ADDR_FROM_STEM(EAX
);
81 return CONTEXT_ADDR_FROM_STEM(ECX
);
83 return CONTEXT_ADDR_FROM_STEM(EDX
);
85 return CONTEXT_ADDR_FROM_STEM(EBX
);
87 return CONTEXT_ADDR_FROM_STEM(ESP
);
89 return CONTEXT_ADDR_FROM_STEM(EBP
);
91 return CONTEXT_ADDR_FROM_STEM(ESI
);
93 return CONTEXT_ADDR_FROM_STEM(EDI
);
95 return CONTEXT_ADDR_FROM_STEM(UESP
);
102 os_context_sp_addr(os_context_t
*context
)
104 return &(_UC_MACHINE_SP(context
));
107 #endif /* __NetBSD__ */
109 int *os_context_pc_addr(os_context_t
*context
)
111 #if defined __FreeBSD__
112 return CONTEXT_ADDR_FROM_STEM(eip
);
113 #elif defined __OpenBSD__
114 return CONTEXT_ADDR_FROM_STEM(pc
);
115 #elif defined __NetBSD__
116 return CONTEXT_ADDR_FROM_STEM(EIP
);
117 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
118 return (int *)CONTEXT_ADDR_FROM_STEM(eip
);
119 #elif defined LISP_FEATURE_DARWIN
120 return &context
->uc_mcontext
->ss
.srr0
;
122 #error unsupported BSD variant
126 /* FIXME: If this can be a no-op on BSD/x86, then it
127 * deserves a more precise name.
129 * (Perhaps os_prepare_data_area_to_be_executed()?) */
131 os_flush_icache(os_vm_address_t address
, os_vm_size_t length
)
135 /* Note: the Darwin versions of arch_os_thread_init found in
138 #if !defined(LISP_FEATURE_DARWIN)
140 #ifdef LISP_FEATURE_SB_THREAD
142 void set_data_desc_size(struct segment_descriptor
* desc
, unsigned long size
)
144 desc
->sd_lolimit
= (size
- 1) & 0xffff;
145 desc
->sd_hilimit
= ((size
- 1) >> 16) &0xf;
148 void set_data_desc_addr(struct segment_descriptor
* desc
, void* addr
)
150 desc
->sd_lobase
= (unsigned int)addr
& 0xffffff;
151 desc
->sd_hibase
= ((unsigned int)addr
& 0xff000000) >> 24;
156 int arch_os_thread_init(struct thread
*thread
) {
158 #ifdef LISP_FEATURE_SB_THREAD
162 struct segment_descriptor ldt_entry
= { 0, 0, SDT_MEMRW
, SEL_UPL
, 1,
165 set_data_desc_addr(&ldt_entry
, thread
);
166 set_data_desc_size(&ldt_entry
, dynamic_values_bytes
);
168 n
= i386_set_ldt(LDT_AUTO_ALLOC
, (union descriptor
*) &ldt_entry
, 1);
170 perror("i386_set_ldt");
171 lose("unexpected i386_set_ldt(..) failure\n");
173 FSHOW_SIGNAL((stderr
, "/ TLS: Allocated LDT %x\n", n
));
174 sel
= LSEL(n
, SEL_UPL
);
177 thread
->tls_cookie
=n
;
178 #ifdef LISP_FEATURE_GCC_TLS
179 current_thread
= thread
;
181 pthread_setspecific(specials
,thread
);
185 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
188 /* Signal handlers are run on the control stack, so if it is exhausted
189 * we had better use an alternate stack for whatever signal tells us
190 * we've exhausted it */
191 sigstack
.ss_sp
=((void *) thread
)+dynamic_values_bytes
;
193 sigstack
.ss_size
= 32*SIGSTKSZ
;
194 sigaltstack(&sigstack
,0);
197 return 1; /* success */
200 int arch_os_thread_cleanup(struct thread
*thread
) {
202 #if defined(LISP_FEATURE_SB_THREAD)
203 int n
= thread
->tls_cookie
;
205 /* Set the %%fs register back to 0 and free the ldt by setting it
208 FSHOW_SIGNAL((stderr
, "/ TLS: Freeing LDT %x\n", n
));
210 __asm__
__volatile__ ("mov %0, %%fs" : : "r"(0));
211 i386_set_ldt(n
, NULL
, 1);
214 return 1; /* success */
217 #endif /* !LISP_FEATURE_DARWIN */
219 #if defined(LISP_FEATURE_FREEBSD)
220 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
222 os_restore_tls_segment_register(os_context_t
*context
)
224 load_fs(context
->uc_mcontext
.mc_fs
);
229 os_restore_fp_control(os_context_t
*context
)
231 /* FPU state is saved per context on post-KSE systems.
232 * On earlier systems, it is shared in a whole process.
234 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
235 struct envxmm
*ex
= (struct envxmm
*)(context
->uc_mcontext
.mc_fpstate
);
236 __asm__
__volatile__ ("fldcw %0" : : "m" (ex
->en_cw
));
238 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
239 /* Calling this function here may not be good idea. Or rename
240 * function name os_restore_fp_control to os_restore_context or
241 * so, to match the behavior? */
242 os_restore_tls_segment_register(context
);
247 #if defined(LISP_FEATURE_OPENBSD)
249 os_restore_fp_control(os_context_t
*context
)
251 struct sigframe
*frame
;
254 frame
= (struct sigframe
*)((char*)context
- offsetof(struct sigframe
, sf_sc
));
255 fpu
= frame
->sf_fpstate
;
256 if (openbsd_use_fxsave
)
257 __asm__
__volatile__ ("fldcw %0" : : "m" (fpu
->sv_xmm
.sv_env
.en_cw
));
259 __asm__
__volatile__ ("fldcw %0" : : "m" (fpu
->sv_87
.sv_env
.en_cw
));