d071666d1077a3fda2e9fee0e1247c8c1d0c5ab4
[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/frame.h>
24 #include <machine/npx.h>
25 #include <stddef.h>
26 #endif
27
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.
33  *
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. */
38
39 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
40 int *
41 os_context_register_addr(os_context_t *context, int offset)
42 {
43     switch(offset) {
44     case  0:
45         return (int *)CONTEXT_ADDR_FROM_STEM(eax);
46     case  2:
47         return (int *)CONTEXT_ADDR_FROM_STEM(ecx);
48     case  4:
49         return (int *)CONTEXT_ADDR_FROM_STEM(edx);
50     case  6:
51         return (int *)CONTEXT_ADDR_FROM_STEM(ebx);
52     case  8:
53         return (int *)CONTEXT_ADDR_FROM_STEM(esp);
54     case 10:
55         return (int *)CONTEXT_ADDR_FROM_STEM(ebp);
56     case 12:
57         return (int *)CONTEXT_ADDR_FROM_STEM(esi);
58     case 14:
59         return (int *)CONTEXT_ADDR_FROM_STEM(edi);
60     default:
61         return 0;
62     }
63 }
64
65 int *
66 os_context_sp_addr(os_context_t *context)
67 {
68     return (int *)CONTEXT_ADDR_FROM_STEM(esp);
69 }
70
71 #endif /* __FreeBSD__ || __OpenBSD__ */
72
73 #ifdef __NetBSD__
74 int *
75 os_context_register_addr(os_context_t *context, int offset)
76 {
77     switch(offset) {
78     case  0:
79         return CONTEXT_ADDR_FROM_STEM(EAX);
80     case  2:
81         return CONTEXT_ADDR_FROM_STEM(ECX);
82     case  4:
83         return CONTEXT_ADDR_FROM_STEM(EDX);
84     case  6:
85         return CONTEXT_ADDR_FROM_STEM(EBX);
86     case  8:
87         return CONTEXT_ADDR_FROM_STEM(ESP);
88     case 10:
89         return CONTEXT_ADDR_FROM_STEM(EBP);
90     case 12:
91         return CONTEXT_ADDR_FROM_STEM(ESI);
92     case 14:
93         return CONTEXT_ADDR_FROM_STEM(EDI);
94     case 16:
95         return CONTEXT_ADDR_FROM_STEM(UESP);
96     default:
97         return 0;
98     }
99 }
100
101 int *
102 os_context_sp_addr(os_context_t *context)
103 {
104     return &(_UC_MACHINE_SP(context));
105 }
106
107 #endif  /* __NetBSD__ */
108
109 int *os_context_pc_addr(os_context_t *context)
110 {
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;
121 #else
122 #error unsupported BSD variant
123 #endif
124 }
125
126 /* FIXME: If this can be a no-op on BSD/x86, then it
127  * deserves a more precise name.
128  *
129  * (Perhaps os_prepare_data_area_to_be_executed()?) */
130 void
131 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
132 {
133 }
134
135 /* Note: the Darwin versions of arch_os_thread_init found in
136  * x86-darwin-os.c
137 */
138 #if !defined(LISP_FEATURE_DARWIN)
139
140 #ifdef LISP_FEATURE_SB_THREAD
141
142 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
143 {
144     desc->sd_lolimit = (size - 1) & 0xffff;
145     desc->sd_hilimit = ((size - 1) >> 16) &0xf;
146 }
147
148 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
149 {
150     desc->sd_lobase = (unsigned int)addr & 0xffffff;
151     desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
152 }
153
154 #endif
155
156 int arch_os_thread_init(struct thread *thread) {
157
158 #ifdef LISP_FEATURE_SB_THREAD
159     int n;
160     int sel;
161
162     struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
163                                             0, 0, 1, 0, 0 };
164
165     set_data_desc_addr(&ldt_entry, thread);
166     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
167
168     n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
169     if (n < 0) {
170         perror("i386_set_ldt");
171         lose("unexpected i386_set_ldt(..) failure\n");
172     }
173     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
174     sel =  LSEL(n, SEL_UPL);
175     load_fs(sel);
176
177     thread->tls_cookie=n;
178 #ifdef LISP_FEATURE_GCC_TLS
179     current_thread = thread;
180 #else
181     pthread_setspecific(specials,thread);
182 #endif
183 #endif
184
185 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
186     stack_t sigstack;
187
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;
192     sigstack.ss_flags=0;
193     sigstack.ss_size = 32*SIGSTKSZ;
194     sigaltstack(&sigstack,0);
195 #endif
196
197     return 1;                  /* success */
198 }
199
200 int arch_os_thread_cleanup(struct thread *thread) {
201
202 #if defined(LISP_FEATURE_SB_THREAD)
203     int n = thread->tls_cookie;
204
205     /* Set the %%fs register back to 0 and free the ldt by setting it
206      * to NULL.
207      */
208     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
209
210     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
211     i386_set_ldt(n, NULL, 1);
212 #endif
213
214     return 1;                  /* success */
215 }
216
217 #endif /* !LISP_FEATURE_DARWIN */
218
219 #if defined(LISP_FEATURE_FREEBSD)
220 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
221 void
222 os_restore_tls_segment_register(os_context_t *context)
223 {
224     load_fs(context->uc_mcontext.mc_fs);
225 }
226 #endif
227
228 void
229 os_restore_fp_control(os_context_t *context)
230 {
231     /* FPU state is saved per context on post-KSE systems.
232      * On earlier systems, it is shared in a whole process.
233      */
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));
237 #endif
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);
243 #endif
244 }
245 #endif
246
247 #if defined(LISP_FEATURE_OPENBSD)
248 void
249 os_restore_fp_control(os_context_t *context)
250 {
251     struct sigframe *frame;
252     union savefpu *fpu;
253
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));
258     else
259         __asm__ __volatile__ ("fldcw %0" : : "m" (fpu->sv_87.sv_env.en_cw));
260 }
261 #endif