1.0.20: release, will be tagged sbcl_1_0_20
[sbcl/eslaughter.git] / src / runtime / x86-bsd-os.c
blobc44c7db4af7eb75538f1558cb1ea2029146bf7ef
1 #include <signal.h>
2 #include "sbcl.h"
3 #include "runtime.h"
4 #include "thread.h"
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
18 #if defined(LISP_FEATURE_FREEBSD)
19 #include "machine/npx.h"
20 #endif
22 /* KLUDGE: There is strong family resemblance in the signal context
23 * stuff in FreeBSD and OpenBSD, but in detail they're different in
24 * almost every line of code. It would be nice to find some way to
25 * factor out the commonality better; failing that, it might be best
26 * just to split this generic-BSD code into one variant for each BSD.
28 * KLUDGE II: this split has begun with the addition of the Darwin BSD
29 * flavour, with the cross-architecture complications that this
30 * entails; unfortunately, currently the situation is worse, not
31 * better, than in the above paragraph. */
33 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
34 int *
35 os_context_register_addr(os_context_t *context, int offset)
37 switch(offset) {
38 case 0:
39 return (int *)CONTEXT_ADDR_FROM_STEM(eax);
40 case 2:
41 return (int *)CONTEXT_ADDR_FROM_STEM(ecx);
42 case 4:
43 return (int *)CONTEXT_ADDR_FROM_STEM(edx);
44 case 6:
45 return (int *)CONTEXT_ADDR_FROM_STEM(ebx);
46 case 8:
47 return (int *)CONTEXT_ADDR_FROM_STEM(esp);
48 case 10:
49 return (int *)CONTEXT_ADDR_FROM_STEM(ebp);
50 case 12:
51 return (int *)CONTEXT_ADDR_FROM_STEM(esi);
52 case 14:
53 return (int *)CONTEXT_ADDR_FROM_STEM(edi);
54 default:
55 return 0;
59 int *
60 os_context_sp_addr(os_context_t *context)
62 return (int *)CONTEXT_ADDR_FROM_STEM(esp);
65 #endif /* __FreeBSD__ || __OpenBSD__ */
67 #ifdef __NetBSD__
68 int *
69 os_context_register_addr(os_context_t *context, int offset)
71 switch(offset) {
72 case 0:
73 return CONTEXT_ADDR_FROM_STEM(EAX);
74 case 2:
75 return CONTEXT_ADDR_FROM_STEM(ECX);
76 case 4:
77 return CONTEXT_ADDR_FROM_STEM(EDX);
78 case 6:
79 return CONTEXT_ADDR_FROM_STEM(EBX);
80 case 8:
81 return CONTEXT_ADDR_FROM_STEM(ESP);
82 case 10:
83 return CONTEXT_ADDR_FROM_STEM(EBP);
84 case 12:
85 return CONTEXT_ADDR_FROM_STEM(ESI);
86 case 14:
87 return CONTEXT_ADDR_FROM_STEM(EDI);
88 case 16:
89 return CONTEXT_ADDR_FROM_STEM(UESP);
90 default:
91 return 0;
95 int *
96 os_context_sp_addr(os_context_t *context)
98 return &(_UC_MACHINE_SP(context));
101 #endif /* __NetBSD__ */
103 int *os_context_pc_addr(os_context_t *context)
105 #if defined __FreeBSD__
106 return CONTEXT_ADDR_FROM_STEM(eip);
107 #elif defined __OpenBSD__
108 return CONTEXT_ADDR_FROM_STEM(pc);
109 #elif defined __NetBSD__
110 return CONTEXT_ADDR_FROM_STEM(EIP);
111 #elif defined(LISP_FEATURE_DARWIN) && defined(LISP_FEATURE_X86)
112 return (int *)CONTEXT_ADDR_FROM_STEM(eip);
113 #elif defined LISP_FEATURE_DARWIN
114 return &context->uc_mcontext->ss.srr0;
115 #else
116 #error unsupported BSD variant
117 #endif
120 /* FIXME: If this can be a no-op on BSD/x86, then it
121 * deserves a more precise name.
123 * (Perhaps os_prepare_data_area_to_be_executed()?) */
124 void
125 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
129 /* Note: the Darwin versions of arch_os_thread_init found in
130 * x86-darwin-os.c
132 #if !defined(LISP_FEATURE_DARWIN)
134 #ifdef LISP_FEATURE_SB_THREAD
136 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
138 desc->sd_lolimit = (size - 1) & 0xffff;
139 desc->sd_hilimit = ((size - 1) >> 16) &0xf;
142 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
144 desc->sd_lobase = (unsigned int)addr & 0xffffff;
145 desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
148 #endif
150 int arch_os_thread_init(struct thread *thread) {
152 #ifdef LISP_FEATURE_SB_THREAD
153 int n;
154 int sel;
156 struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
157 0, 0, 1, 0, 0 };
159 set_data_desc_addr(&ldt_entry, thread);
160 set_data_desc_size(&ldt_entry, dynamic_values_bytes);
162 n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
163 if (n < 0) {
164 perror("i386_set_ldt");
165 lose("unexpected i386_set_ldt(..) failure\n");
167 FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
168 sel = LSEL(n, SEL_UPL);
169 load_fs(sel);
171 thread->tls_cookie=n;
172 #ifdef LISP_FEATURE_GCC_TLS
173 current_thread = thread;
174 #else
175 pthread_setspecific(specials,thread);
176 #endif
177 #endif
179 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
180 stack_t sigstack;
182 /* Signal handlers are run on the control stack, so if it is exhausted
183 * we had better use an alternate stack for whatever signal tells us
184 * we've exhausted it */
185 sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
186 sigstack.ss_flags=0;
187 sigstack.ss_size = 32*SIGSTKSZ;
188 sigaltstack(&sigstack,0);
189 #endif
191 return 1; /* success */
194 int arch_os_thread_cleanup(struct thread *thread) {
196 #if defined(LISP_FEATURE_SB_THREAD)
197 int n = thread->tls_cookie;
199 /* Set the %%fs register back to 0 and free the ldt by setting it
200 * to NULL.
202 FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
204 __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
205 i386_set_ldt(n, NULL, 1);
206 #endif
208 return 1; /* success */
211 #endif /* !LISP_FEATURE_DARWIN */
213 #if defined(LISP_FEATURE_FREEBSD)
214 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
215 void
216 os_restore_tls_segment_register(os_context_t *context)
218 load_fs(context->uc_mcontext.mc_fs);
220 #endif
222 void
223 os_restore_fp_control(os_context_t *context)
225 /* FPU state is saved per context on post-KSE systems.
226 * On earlier systems, it is shared in a whole process.
228 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
229 struct envxmm *ex = (struct envxmm *)(context->uc_mcontext.mc_fpstate);
230 __asm__ __volatile__ ("fldcw %0" : : "m" (ex->en_cw));
231 #endif
232 #if defined(LISP_FEATURE_RESTORE_TLS_SEGMENT_REGISTER_FROM_CONTEXT)
233 /* Calling this function here may not be good idea. Or rename
234 * function name os_restore_fp_control to os_restore_context or
235 * so, to match the behavior? */
236 os_restore_tls_segment_register(context);
237 #endif
239 #endif