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