4 #include "genesis/sbcl.h"
10 #ifdef LISP_FEATURE_SB_THREAD
11 #ifdef LISP_FEATURE_DARWIN
12 #include <architecture/i386/table.h>
13 #include <i386/user_ldt.h>
14 #include <mach/mach_init.h>
16 #include <machine/segments.h>
17 #include <machine/sysarch.h>
18 #endif /* LISP_FEATURE_DARWIN */
21 #if defined(LISP_FEATURE_FREEBSD) || defined(LISP_FEATURE_DRAGONFLY)
22 #include "machine/npx.h"
25 #if defined(LISP_FEATURE_OPENBSD)
26 #include <machine/npx.h>
28 #include "openbsd-sigcontext.h"
29 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
30 # include <machine/frame.h>
34 /* KLUDGE: There is strong family resemblance in the signal context
35 * stuff in FreeBSD and OpenBSD, but in detail they're different in
36 * almost every line of code. It would be nice to find some way to
37 * factor out the commonality better; failing that, it might be best
38 * just to split this generic-BSD code into one variant for each BSD.
40 * KLUDGE II: this split has begun with the addition of the Darwin BSD
41 * flavour, with the cross-architecture complications that this
42 * entails; unfortunately, currently the situation is worse, not
43 * better, than in the above paragraph. */
45 #ifdef LISP_FEATURE_NETBSD
46 #define _REG_eax _REG_EAX
47 #define _REG_ecx _REG_ECX
48 #define _REG_edx _REG_EDX
49 #define _REG_ebx _REG_EBX
50 #define _REG_esp _REG_ESP
51 #define _REG_ebp _REG_EBP
52 #define _REG_esi _REG_ESI
53 #define _REG_edi _REG_EDI
57 os_context_register_addr(os_context_t
*context
, int offset
)
61 return (int *)CONTEXT_ADDR_FROM_STEM(eax
);
63 return (int *)CONTEXT_ADDR_FROM_STEM(ecx
);
65 return (int *)CONTEXT_ADDR_FROM_STEM(edx
);
67 return (int *)CONTEXT_ADDR_FROM_STEM(ebx
);
69 return (int *)CONTEXT_ADDR_FROM_STEM(esp
);
71 return (int *)CONTEXT_ADDR_FROM_STEM(ebp
);
73 return (int *)CONTEXT_ADDR_FROM_STEM(esi
);
75 return (int *)CONTEXT_ADDR_FROM_STEM(edi
);
77 /* Arguably the line in interrupt.c which uses reg_UESP could be changed
78 * to access c->uc_mcontext.__gregs[_REG_UESP] directly since nothing else
79 * needs this case, but I don't care enough to figure out why x86 + NetBSD
80 * crashes in cold-init regardless of any recent changes */
82 return CONTEXT_ADDR_FROM_STEM(UESP
);
90 os_context_fp_addr(os_context_t
*context
)
92 return (int *)CONTEXT_ADDR_FROM_STEM(ebp
);
95 #if defined(LISP_FEATURE_FREEBSD) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN) || defined(__DragonFly__)
97 os_context_sp_addr(os_context_t
*context
)
99 return (int *)CONTEXT_ADDR_FROM_STEM(esp
);
105 os_context_sp_addr(os_context_t
*context
)
107 // UC_MACHINE_SP refers to _REG_UESP, not _REG_ESP
108 return &(_UC_MACHINE_SP(context
));
111 #endif /* __NetBSD__ */
113 /* FIXME: If this can be a no-op on BSD/x86, then it
114 * deserves a more precise name.
116 * (Perhaps os_prepare_data_area_to_be_executed()?) */
118 os_flush_icache(os_vm_address_t address
, os_vm_size_t length
)
122 /* Note: the Darwin versions of arch_os_thread_init found in
125 #if !defined(LISP_FEATURE_DARWIN)
127 #ifdef LISP_FEATURE_SB_THREAD
129 void set_data_desc_size(struct segment_descriptor
* desc
, unsigned long size
)
131 desc
->sd_lolimit
= (size
- 1) & 0xffff;
132 desc
->sd_hilimit
= ((size
- 1) >> 16) &0xf;
135 void set_data_desc_addr(struct segment_descriptor
* desc
, void* addr
)
137 desc
->sd_lobase
= (unsigned int)addr
& 0xffffff;
138 desc
->sd_hibase
= ((unsigned int)addr
& 0xff000000) >> 24;
143 #ifdef LISP_FEATURE_SB_THREAD
145 arch_os_load_ldt(struct thread
*thread
)
147 int sel
= LSEL(thread
->tls_cookie
, SEL_UPL
);
148 unsigned int fs
= rfs();
150 /* Load FS only if it's necessary. Modifying a selector
151 * causes privilege checking and it takes long time. */
157 int arch_os_thread_init(struct thread
*thread
) {
159 #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");
173 thread
->tls_cookie
=n
;
174 arch_os_load_ldt(thread
);
177 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
180 /* Signal handlers are run on the control stack, so if it is exhausted
181 * we had better use an alternate stack for whatever signal tells us
182 * we've exhausted it */
183 sigstack
.ss_sp
= calc_altstack_base(thread
);
184 sigstack
.ss_flags
= 0;
185 sigstack
.ss_size
= calc_altstack_size(thread
);
186 if (sigaltstack(&sigstack
,0)<0)
187 lose("Cannot sigaltstack: %s",strerror(errno
));
190 return 1; /* success */
193 int arch_os_thread_cleanup(struct thread
*thread
) {
195 #if defined(LISP_FEATURE_SB_THREAD)
196 int n
= thread
->tls_cookie
;
198 /* Set the %%fs register back to 0 and free the ldt by setting it
201 __asm__
__volatile__ ("mov %0, %%fs" : : "r"(0));
202 i386_set_ldt(n
, NULL
, 1);
205 return 1; /* success */
208 #endif /* !LISP_FEATURE_DARWIN */
210 #if defined(LISP_FEATURE_FREEBSD)
211 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
213 os_restore_tls_segment_register(os_context_t
*context
)
215 load_fs(context
->uc_mcontext
.mc_fs
);
220 os_restore_fp_control(os_context_t
*context
)
222 /* FPU state is saved per context on post-KSE systems.
223 * On earlier systems, it is shared in a whole process.
225 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
226 struct envxmm
*ex
= (struct envxmm
*)(context
->uc_mcontext
.mc_fpstate
);
227 __asm__
__volatile__ ("fldcw %0" : : "m" (ex
->en_cw
));
229 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
230 /* Calling this function here may not be good idea. Or rename
231 * function name os_restore_fp_control to os_restore_context or
232 * so, to match the behavior? */
233 os_restore_tls_segment_register(context
);
238 #if defined(LISP_FEATURE_DRAGONFLY)
239 void os_restore_fp_control (os_context_t
*context
)
241 struct envxmm
*ex
= (struct envxmm
*)(context
->uc_mcontext
.mc_fpregs
);
242 __asm__
__volatile__ ("fldcw %0" : : "m" (ex
->en_cw
));
244 #endif /* LISP_FEATURE_DRAGONFLY */
246 #if defined(LISP_FEATURE_OPENBSD)
248 os_restore_fp_control(os_context_t
*context
)
250 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
251 struct sigframe
*frame
= (struct sigframe
*)((char*)context
-
252 offsetof(struct sigframe
, sf_sc
));
253 union savefpu
*fpu
= frame
->sf_fpstate
;
254 #elif defined(OS_OPENBSD_FPSTATE_IN_SIGCONTEXT)
255 union savefpu
*fpu
= context
->sc_fpstate
;
258 if (openbsd_use_fxsave
)
259 __asm__
__volatile__ ("fldcw %0" : : "m" (fpu
->sv_xmm
.sv_env
.en_cw
));
261 __asm__
__volatile__ ("fldcw %0" : : "m" (fpu
->sv_87
.sv_env
.en_cw
));