Port safepoints to FreeBSD/x86
[sbcl.git] / src / runtime / x86-bsd-os.c
1 #include <signal.h>
2 #include <stdio.h>
3 #include "sbcl.h"
4 #include "runtime.h"
5 #include "thread.h"
6
7
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>
13 #else
14 #include <machine/segments.h>
15 #include <machine/sysarch.h>
16 #endif /* LISP_FEATURE_DARWIN */
17 #endif
18
19 #if defined(LISP_FEATURE_FREEBSD)
20 #include "machine/npx.h"
21 #endif
22
23 #if defined(LISP_FEATURE_OPENBSD)
24 #include <machine/npx.h>
25 #include <stddef.h>
26 #include "openbsd-sigcontext.h"
27 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
28 # include <machine/frame.h>
29 #endif
30 #endif
31
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.
37  *
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. */
42
43 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
44 int *
45 os_context_register_addr(os_context_t *context, int offset)
46 {
47     switch(offset) {
48     case  0:
49         return (int *)CONTEXT_ADDR_FROM_STEM(eax);
50     case  2:
51         return (int *)CONTEXT_ADDR_FROM_STEM(ecx);
52     case  4:
53         return (int *)CONTEXT_ADDR_FROM_STEM(edx);
54     case  6:
55         return (int *)CONTEXT_ADDR_FROM_STEM(ebx);
56     case  8:
57         return (int *)CONTEXT_ADDR_FROM_STEM(esp);
58     case 10:
59         return (int *)CONTEXT_ADDR_FROM_STEM(ebp);
60     case 12:
61         return (int *)CONTEXT_ADDR_FROM_STEM(esi);
62     case 14:
63         return (int *)CONTEXT_ADDR_FROM_STEM(edi);
64     default:
65         return 0;
66     }
67 }
68
69 int *
70 os_context_sp_addr(os_context_t *context)
71 {
72     return (int *)CONTEXT_ADDR_FROM_STEM(esp);
73 }
74
75 #endif /* __FreeBSD__ || __OpenBSD__ */
76
77 #ifdef __NetBSD__
78 int *
79 os_context_register_addr(os_context_t *context, int offset)
80 {
81     switch(offset) {
82     case  0:
83         return CONTEXT_ADDR_FROM_STEM(EAX);
84     case  2:
85         return CONTEXT_ADDR_FROM_STEM(ECX);
86     case  4:
87         return CONTEXT_ADDR_FROM_STEM(EDX);
88     case  6:
89         return CONTEXT_ADDR_FROM_STEM(EBX);
90     case  8:
91         return CONTEXT_ADDR_FROM_STEM(ESP);
92     case 10:
93         return CONTEXT_ADDR_FROM_STEM(EBP);
94     case 12:
95         return CONTEXT_ADDR_FROM_STEM(ESI);
96     case 14:
97         return CONTEXT_ADDR_FROM_STEM(EDI);
98     case 16:
99         return CONTEXT_ADDR_FROM_STEM(UESP);
100     default:
101         return 0;
102     }
103 }
104
105 int *
106 os_context_sp_addr(os_context_t *context)
107 {
108     return &(_UC_MACHINE_SP(context));
109 }
110
111 #endif  /* __NetBSD__ */
112
113 int *os_context_pc_addr(os_context_t *context)
114 {
115 #if defined __FreeBSD__
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;
125 #else
126 #error unsupported BSD variant
127 #endif
128 }
129
130 /* FIXME: If this can be a no-op on BSD/x86, then it
131  * deserves a more precise name.
132  *
133  * (Perhaps os_prepare_data_area_to_be_executed()?) */
134 void
135 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
136 {
137 }
138
139 /* Note: the Darwin versions of arch_os_thread_init found in
140  * x86-darwin-os.c
141 */
142 #if !defined(LISP_FEATURE_DARWIN)
143
144 #ifdef LISP_FEATURE_SB_THREAD
145
146 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
147 {
148     desc->sd_lolimit = (size - 1) & 0xffff;
149     desc->sd_hilimit = ((size - 1) >> 16) &0xf;
150 }
151
152 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
153 {
154     desc->sd_lobase = (unsigned int)addr & 0xffffff;
155     desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
156 }
157
158 #endif
159
160 int arch_os_thread_init(struct thread *thread) {
161
162 #ifdef LISP_FEATURE_SB_THREAD
163     int n;
164     int sel;
165
166     struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
167                                             0, 0, 1, 0, 0 };
168
169     set_data_desc_addr(&ldt_entry, thread);
170     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
171
172     n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
173     if (n < 0) {
174         perror("i386_set_ldt");
175         lose("unexpected i386_set_ldt(..) failure\n");
176     }
177     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
178     sel =  LSEL(n, SEL_UPL);
179     load_fs(sel);
180
181     thread->tls_cookie=n;
182 #ifdef LISP_FEATURE_GCC_TLS
183     current_thread = thread;
184 #else
185     pthread_setspecific(specials,thread);
186 #endif
187 #endif
188
189 #ifdef LISP_FEATURE_SB_SAFEPOINT
190     thread->selfptr = thread;
191 #endif
192
193 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
194     stack_t sigstack;
195
196     /* Signal handlers are run on the control stack, so if it is exhausted
197      * we had better use an alternate stack for whatever signal tells us
198      * we've exhausted it */
199     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
200     sigstack.ss_flags=0;
201     sigstack.ss_size = 32*SIGSTKSZ;
202     sigaltstack(&sigstack,0);
203 #endif
204
205     return 1;                  /* success */
206 }
207
208 int arch_os_thread_cleanup(struct thread *thread) {
209
210 #if defined(LISP_FEATURE_SB_THREAD)
211     int n = thread->tls_cookie;
212
213     /* Set the %%fs register back to 0 and free the ldt by setting it
214      * to NULL.
215      */
216     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
217
218     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
219     i386_set_ldt(n, NULL, 1);
220 #endif
221
222     return 1;                  /* success */
223 }
224
225 #endif /* !LISP_FEATURE_DARWIN */
226
227 #if defined(LISP_FEATURE_FREEBSD)
228 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
229 void
230 os_restore_tls_segment_register(os_context_t *context)
231 {
232     load_fs(context->uc_mcontext.mc_fs);
233 }
234 #endif
235
236 void
237 os_restore_fp_control(os_context_t *context)
238 {
239     /* FPU state is saved per context on post-KSE systems.
240      * On earlier systems, it is shared in a whole process.
241      */
242 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
243     struct envxmm *ex = (struct envxmm *)(context->uc_mcontext.mc_fpstate);
244     __asm__ __volatile__ ("fldcw %0" : : "m" (ex->en_cw));
245 #endif
246 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
247     /* Calling this function here may not be good idea.  Or rename
248      * function name os_restore_fp_control to os_restore_context or
249      * so, to match the behavior?  */
250     os_restore_tls_segment_register(context);
251 #endif
252 }
253 #endif
254
255 #if defined(LISP_FEATURE_OPENBSD)
256 void
257 os_restore_fp_control(os_context_t *context)
258 {
259 #ifdef OS_OPENBSD_FPSTATE_IN_SIGFRAME
260     struct sigframe *frame = (struct sigframe *)((char*)context -
261         offsetof(struct sigframe, sf_sc));
262     union savefpu *fpu = frame->sf_fpstate;
263 #elif defined(OS_OPENBSD_FPSTATE_IN_SIGCONTEXT)
264     union savefpu *fpu = context->sc_fpstate;
265 #endif
266
267     if (openbsd_use_fxsave)
268         __asm__ __volatile__ ("fldcw %0" : : "m" (fpu->sv_xmm.sv_env.en_cw));
269     else
270         __asm__ __volatile__ ("fldcw %0" : : "m" (fpu->sv_87.sv_env.en_cw));
271 }
272 #endif