[c06b8c]: src / runtime / x86-bsd-os.c Maximize Restore History

Download this file

x86-bsd-os.c    293 lines (251 with data), 8.0 kB

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