647eb52d9ea3ac2eddb2df31d3838270d89268b8
[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 /* 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.
27  *
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. */
32
33 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(LISP_FEATURE_DARWIN)
34 int *
35 os_context_register_addr(os_context_t *context, int offset)
36 {
37     switch(offset) {
38     case  0:
39         return CONTEXT_ADDR_FROM_STEM(eax);
40     case  2:
41         return CONTEXT_ADDR_FROM_STEM(ecx);
42     case  4:
43         return CONTEXT_ADDR_FROM_STEM(edx);
44     case  6:
45         return CONTEXT_ADDR_FROM_STEM(ebx);
46     case  8:
47         return CONTEXT_ADDR_FROM_STEM(esp);
48     case 10:
49         return CONTEXT_ADDR_FROM_STEM(ebp);
50     case 12:
51         return CONTEXT_ADDR_FROM_STEM(esi);
52     case 14:
53         return CONTEXT_ADDR_FROM_STEM(edi);
54     default:
55         return 0;
56     }
57 }
58
59 int *
60 os_context_sp_addr(os_context_t *context)
61 {
62     return CONTEXT_ADDR_FROM_STEM(esp);
63 }
64
65 #endif /* __FreeBSD__ || __OpenBSD__ */
66
67 #ifdef __NetBSD__
68 int *
69 os_context_register_addr(os_context_t *context, int offset)
70 {
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;
92     }
93 }
94
95 int *
96 os_context_sp_addr(os_context_t *context)
97 {
98     return &(_UC_MACHINE_SP(context));
99 }
100
101 #endif  /* __NetBSD__ */
102
103
104 /* FIXME: If this can be a no-op on BSD/x86, then it
105  * deserves a more precise name.
106  *
107  * (Perhaps os_prepare_data_area_to_be_executed()?) */
108 void
109 os_flush_icache(os_vm_address_t address, os_vm_size_t length)
110 {
111 }
112
113 /* Note: the Darwin versions of arch_os_thread_init found in
114  * x86-darwin-os.c
115 */
116 #if !defined(LISP_FEATURE_DARWIN)
117
118 #ifdef LISP_FEATURE_SB_THREAD
119
120 void set_data_desc_size(struct segment_descriptor* desc, unsigned long size)
121 {
122     desc->sd_lolimit = (size - 1) & 0xffff;
123     desc->sd_hilimit = ((size - 1) >> 16) &0xf;
124 }
125
126 void set_data_desc_addr(struct segment_descriptor* desc, void* addr)
127 {
128     desc->sd_lobase = (unsigned int)addr & 0xffffff;
129     desc->sd_hibase = ((unsigned int)addr & 0xff000000) >> 24;
130 }
131
132 #endif
133
134 int arch_os_thread_init(struct thread *thread) {
135
136 #ifdef LISP_FEATURE_SB_THREAD
137     int n;
138     int sel;
139
140     struct segment_descriptor ldt_entry = { 0, 0, SDT_MEMRW, SEL_UPL, 1,
141                                             0, 0, 1, 0, 0 };
142
143     set_data_desc_addr(&ldt_entry, (unsigned long) thread);
144     set_data_desc_size(&ldt_entry, dynamic_values_bytes);
145
146     n = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor*) &ldt_entry, 1);
147     if (n < 0) {
148         perror("i386_set_ldt");
149         lose("unexpected i386_set_ldt(..) failure\n");
150     }
151     FSHOW_SIGNAL((stderr, "/ TLS: Allocated LDT %x\n", n));
152     sel =  LSEL(n, SEL_UPL);
153     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(sel));
154
155     thread->tls_cookie=n;
156     pthread_setspecific(specials,thread);
157 #endif
158
159 #ifdef LISP_FEATURE_C_STACK_IS_CONTROL_STACK
160     stack_t sigstack;
161
162     /* Signal handlers are run on the control stack, so if it is exhausted
163      * we had better use an alternate stack for whatever signal tells us
164      * we've exhausted it */
165     sigstack.ss_sp=((void *) thread)+dynamic_values_bytes;
166     sigstack.ss_flags=0;
167     sigstack.ss_size = 32*SIGSTKSZ;
168     sigaltstack(&sigstack,0);
169 #endif
170
171     return 1;                  /* success */
172 }
173
174 int arch_os_thread_cleanup(struct thread *thread) {
175
176 #if defined(LISP_FEATURE_SB_THREAD)
177     int n = thread->tls_cookie;
178
179     /* Set the %%fs register back to 0 and free the the ldt
180      * by setting it to NULL.
181      */
182     FSHOW_SIGNAL((stderr, "/ TLS: Freeing LDT %x\n", n));
183
184     __asm__ __volatile__ ("mov %0, %%fs" : : "r"(0));
185     i386_set_ldt(n, NULL, 1);
186 #endif
187
188     return 1;                  /* success */
189 }
190
191 #endif /* !LISP_FEATURE_DARWIN */
192
193 #if defined(LISP_FEATURE_FREEBSD)
194 void
195 os_restore_fp_control(os_context_t *context)
196 {
197     /* FPU state is saved per context on post-KSE systems.
198      * On earlier systems, it is shared in a whole process.
199      */
200 #if defined(__FreeBSD_version) && __FreeBSD_version >= 500040
201     struct envxmm *ex = (struct envxmm*)(&context->uc_mcontext.mc_fpstate);
202     asm ("fldcw %0" : : "m" (ex->en_cw));
203 #endif
204 }
205 #endif