linux_machdep.c revision 1.29.2.5 1 1.29.2.5 yamt /* $NetBSD: linux_machdep.c,v 1.29.2.5 2010/08/11 22:53:04 yamt Exp $ */
2 1.1 manu
3 1.1 manu /*-
4 1.1 manu * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
5 1.1 manu *
6 1.1 manu * Redistribution and use in source and binary forms, with or without
7 1.1 manu * modification, are permitted provided that the following conditions
8 1.1 manu * are met:
9 1.1 manu * 1. Redistributions of source code must retain the above copyright
10 1.1 manu * notice, this list of conditions and the following disclaimer.
11 1.1 manu * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 manu * notice, this list of conditions and the following disclaimer in the
13 1.1 manu * documentation and/or other materials provided with the distribution.
14 1.1 manu * 3. All advertising materials mentioning features or use of this software
15 1.1 manu * must display the following acknowledgement:
16 1.1 manu * This product includes software developed by Emmanuel Dreyfus
17 1.1 manu * 4. The name of the author may not be used to endorse or promote
18 1.1 manu * products derived from this software without specific prior written
19 1.1 manu * permission.
20 1.1 manu *
21 1.1 manu * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 1.1 manu * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 1.1 manu * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 1.1 manu * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 1.1 manu * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 1.1 manu * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 1.1 manu * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 1.1 manu * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 1.1 manu * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 1.1 manu * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 1.1 manu * POSSIBILITY OF SUCH DAMAGE.
32 1.1 manu */
33 1.1 manu
34 1.1 manu #include <sys/cdefs.h>
35 1.1 manu
36 1.29.2.5 yamt __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.29.2.5 2010/08/11 22:53:04 yamt Exp $");
37 1.1 manu
38 1.1 manu #include <sys/param.h>
39 1.1 manu #include <sys/types.h>
40 1.1 manu #include <sys/systm.h>
41 1.1 manu #include <sys/signal.h>
42 1.1 manu #include <sys/exec.h>
43 1.1 manu #include <sys/proc.h>
44 1.1 manu #include <sys/ptrace.h> /* for process_read_fpregs() */
45 1.1 manu #include <sys/ucontext.h>
46 1.21 manu #include <sys/conf.h>
47 1.1 manu
48 1.1 manu #include <machine/reg.h>
49 1.1 manu #include <machine/pcb.h>
50 1.1 manu #include <machine/fpu.h>
51 1.1 manu #include <machine/mcontext.h>
52 1.2 fvdl #include <machine/specialreg.h>
53 1.2 fvdl #include <machine/vmparam.h>
54 1.29.2.1 yamt #include <machine/cpufunc.h>
55 1.1 manu
56 1.21 manu /*
57 1.21 manu * To see whether wscons is configured (for virtual console ioctl calls).
58 1.21 manu */
59 1.21 manu #if defined(_KERNEL_OPT)
60 1.21 manu #include "wsdisplay.h"
61 1.21 manu #endif
62 1.21 manu #if (NWSDISPLAY > 0)
63 1.21 manu #include <dev/wscons/wsconsio.h>
64 1.21 manu #include <dev/wscons/wsdisplay_usl_io.h>
65 1.21 manu #endif
66 1.21 manu
67 1.21 manu
68 1.1 manu #include <compat/linux/common/linux_signal.h>
69 1.1 manu #include <compat/linux/common/linux_errno.h>
70 1.1 manu #include <compat/linux/common/linux_exec.h>
71 1.1 manu #include <compat/linux/common/linux_ioctl.h>
72 1.1 manu #include <compat/linux/common/linux_prctl.h>
73 1.1 manu #include <compat/linux/common/linux_machdep.h>
74 1.25 njoly #include <compat/linux/common/linux_ipc.h>
75 1.25 njoly #include <compat/linux/common/linux_sem.h>
76 1.4 fvdl #include <compat/linux/linux_syscall.h>
77 1.1 manu #include <compat/linux/linux_syscallargs.h>
78 1.1 manu
79 1.5 fvdl static void linux_buildcontext(struct lwp *, void *, void *);
80 1.5 fvdl
81 1.1 manu void
82 1.29.2.5 yamt linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
83 1.1 manu {
84 1.29.2.4 yamt struct pcb *pcb = lwp_getpcb(l);
85 1.1 manu struct trapframe *tf;
86 1.1 manu
87 1.1 manu /* If we were using the FPU, forget about it. */
88 1.29.2.4 yamt if (pcb->pcb_fpcpu != NULL)
89 1.1 manu fpusave_lwp(l, 0);
90 1.1 manu
91 1.1 manu l->l_md.md_flags &= ~MDP_USEDFPU;
92 1.1 manu pcb->pcb_flags = 0;
93 1.1 manu pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
94 1.1 manu pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
95 1.1 manu pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
96 1.1 manu
97 1.18 pavel l->l_proc->p_flag &= ~PK_32;
98 1.1 manu
99 1.1 manu tf = l->l_md.md_regs;
100 1.1 manu tf->tf_rax = 0;
101 1.1 manu tf->tf_rbx = 0;
102 1.1 manu tf->tf_rcx = epp->ep_entry;
103 1.1 manu tf->tf_rdx = 0;
104 1.1 manu tf->tf_rsi = 0;
105 1.1 manu tf->tf_rdi = 0;
106 1.1 manu tf->tf_rbp = 0;
107 1.1 manu tf->tf_rsp = stack;
108 1.1 manu tf->tf_r8 = 0;
109 1.1 manu tf->tf_r9 = 0;
110 1.1 manu tf->tf_r10 = 0;
111 1.1 manu tf->tf_r11 = 0;
112 1.1 manu tf->tf_r12 = 0;
113 1.1 manu tf->tf_r13 = 0;
114 1.1 manu tf->tf_r14 = 0;
115 1.1 manu tf->tf_r15 = 0;
116 1.1 manu tf->tf_rip = epp->ep_entry;
117 1.4 fvdl tf->tf_rflags = PSL_USERSET;
118 1.4 fvdl tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
119 1.4 fvdl tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
120 1.1 manu tf->tf_ds = 0;
121 1.1 manu tf->tf_es = 0;
122 1.29.2.5 yamt cpu_fsgs_zero(l);
123 1.1 manu
124 1.1 manu return;
125 1.1 manu }
126 1.1 manu
127 1.27 dsl void
128 1.26 dsl linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
129 1.1 manu {
130 1.1 manu struct lwp *l = curlwp;
131 1.1 manu struct proc *p = l->l_proc;
132 1.29.2.4 yamt struct pcb *pcb = lwp_getpcb(l);
133 1.1 manu struct sigacts *ps = p->p_sigacts;
134 1.16 ad int onstack, error;
135 1.1 manu int sig = ksi->ksi_signo;
136 1.1 manu struct linux_rt_sigframe *sfp, sigframe;
137 1.1 manu struct linux__fpstate *fpsp, fpstate;
138 1.1 manu struct fpreg fpregs;
139 1.1 manu struct trapframe *tf = l->l_md.md_regs;
140 1.1 manu sig_t catcher = SIGACTION(p, sig).sa_handler;
141 1.1 manu linux_sigset_t lmask;
142 1.1 manu char *sp;
143 1.4 fvdl
144 1.1 manu /* Do we need to jump onto the signal stack? */
145 1.1 manu onstack =
146 1.17 ad (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
147 1.1 manu (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
148 1.1 manu
149 1.1 manu /* Allocate space for the signal handler context. */
150 1.1 manu if (onstack)
151 1.20 christos sp = ((char *)l->l_sigstk.ss_sp +
152 1.17 ad l->l_sigstk.ss_size);
153 1.1 manu else
154 1.20 christos sp = (char *)tf->tf_rsp - 128;
155 1.1 manu
156 1.1 manu /*
157 1.1 manu * Save FPU state, if any
158 1.1 manu */
159 1.1 manu if (l->l_md.md_flags & MDP_USEDFPU) {
160 1.1 manu sp = (char *)
161 1.1 manu (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
162 1.1 manu fpsp = (struct linux__fpstate *)sp;
163 1.17 ad } else
164 1.1 manu fpsp = NULL;
165 1.1 manu
166 1.1 manu /*
167 1.1 manu * Populate the rt_sigframe
168 1.1 manu */
169 1.1 manu sp = (char *)
170 1.1 manu ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
171 1.1 manu sfp = (struct linux_rt_sigframe *)sp;
172 1.1 manu
173 1.29.2.2 yamt memset(&sigframe, 0, sizeof(sigframe));
174 1.3 manu if (ps->sa_sigdesc[sig].sd_vers != 0)
175 1.7 manu sigframe.pretcode =
176 1.7 manu (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
177 1.3 manu else
178 1.3 manu sigframe.pretcode = NULL;
179 1.1 manu
180 1.1 manu /*
181 1.1 manu * The user context
182 1.1 manu */
183 1.1 manu sigframe.uc.luc_flags = 0;
184 1.1 manu sigframe.uc.luc_link = NULL;
185 1.1 manu
186 1.1 manu /* This is used regardless of SA_ONSTACK in Linux */
187 1.17 ad sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
188 1.17 ad sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
189 1.1 manu sigframe.uc.luc_stack.ss_flags = 0;
190 1.17 ad if (l->l_sigstk.ss_flags & SS_ONSTACK)
191 1.1 manu sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
192 1.17 ad if (l->l_sigstk.ss_flags & SS_DISABLE)
193 1.1 manu sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
194 1.1 manu
195 1.1 manu sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
196 1.1 manu sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
197 1.1 manu sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
198 1.1 manu sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
199 1.1 manu sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
200 1.1 manu sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
201 1.1 manu sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
202 1.1 manu sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
203 1.1 manu sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
204 1.1 manu sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
205 1.1 manu sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
206 1.1 manu sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
207 1.1 manu sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
208 1.23 njoly sigframe.uc.luc_mcontext.rax = tf->tf_rax;
209 1.1 manu sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
210 1.1 manu sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
211 1.6 fvdl sigframe.uc.luc_mcontext.rip = tf->tf_rip;
212 1.1 manu sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
213 1.1 manu sigframe.uc.luc_mcontext.cs = tf->tf_cs;
214 1.1 manu sigframe.uc.luc_mcontext.gs = tf->tf_gs;
215 1.1 manu sigframe.uc.luc_mcontext.fs = tf->tf_fs;
216 1.1 manu sigframe.uc.luc_mcontext.err = tf->tf_err;
217 1.1 manu sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
218 1.1 manu native_to_linux_sigset(&lmask, mask);
219 1.1 manu sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
220 1.29.2.4 yamt sigframe.uc.luc_mcontext.cr2 = (long)pcb->pcb_onfault;
221 1.1 manu sigframe.uc.luc_mcontext.fpstate = fpsp;
222 1.1 manu native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
223 1.1 manu
224 1.1 manu /*
225 1.1 manu * the siginfo structure
226 1.1 manu */
227 1.1 manu sigframe.info.lsi_signo = native_to_linux_signo[sig];
228 1.1 manu sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
229 1.29.2.2 yamt sigframe.info.lsi_code = native_to_linux_si_code(ksi->ksi_code);
230 1.1 manu
231 1.1 manu /* XXX This is a rought conversion, taken from i386 code */
232 1.1 manu switch (sigframe.info.lsi_signo) {
233 1.1 manu case LINUX_SIGILL:
234 1.1 manu case LINUX_SIGFPE:
235 1.1 manu case LINUX_SIGSEGV:
236 1.1 manu case LINUX_SIGBUS:
237 1.1 manu case LINUX_SIGTRAP:
238 1.1 manu sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
239 1.1 manu break;
240 1.1 manu case LINUX_SIGCHLD:
241 1.1 manu sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
242 1.1 manu sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
243 1.1 manu sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
244 1.1 manu sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
245 1.29.2.3 yamt sigframe.info._sifields._sigchld._status =
246 1.29.2.3 yamt native_to_linux_si_status(ksi->ksi_code, ksi->ksi_status);
247 1.1 manu break;
248 1.1 manu case LINUX_SIGIO:
249 1.1 manu sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
250 1.1 manu sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
251 1.1 manu break;
252 1.1 manu default:
253 1.1 manu sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
254 1.1 manu sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
255 1.1 manu if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
256 1.1 manu (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
257 1.1 manu sigframe.info._sifields._timer._sigval.sival_ptr =
258 1.22 christos ksi->ksi_value.sival_ptr;
259 1.1 manu break;
260 1.1 manu }
261 1.1 manu
262 1.16 ad sendsig_reset(l, sig);
263 1.29 ad mutex_exit(p->p_lock);
264 1.17 ad error = 0;
265 1.17 ad
266 1.17 ad /*
267 1.17 ad * Save FPU state, if any
268 1.17 ad */
269 1.17 ad if (fpsp != NULL) {
270 1.17 ad (void)process_read_fpregs(l, &fpregs);
271 1.29.2.2 yamt memset(&fpstate, 0, sizeof(fpstate));
272 1.17 ad fpstate.cwd = fpregs.fp_fcw;
273 1.17 ad fpstate.swd = fpregs.fp_fsw;
274 1.17 ad fpstate.twd = fpregs.fp_ftw;
275 1.17 ad fpstate.fop = fpregs.fp_fop;
276 1.17 ad fpstate.rip = fpregs.fp_rip;
277 1.17 ad fpstate.rdp = fpregs.fp_rdp;
278 1.17 ad fpstate.mxcsr = fpregs.fp_mxcsr;
279 1.17 ad fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
280 1.17 ad memcpy(&fpstate.st_space, &fpregs.fp_st,
281 1.17 ad sizeof(fpstate.st_space));
282 1.17 ad memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
283 1.17 ad sizeof(fpstate.xmm_space));
284 1.17 ad error = copyout(&fpstate, fpsp, sizeof(fpstate));
285 1.17 ad }
286 1.17 ad
287 1.17 ad if (error == 0)
288 1.17 ad error = copyout(&sigframe, sp, sizeof(sigframe));
289 1.17 ad
290 1.29 ad mutex_enter(p->p_lock);
291 1.16 ad
292 1.16 ad if (error != 0) {
293 1.1 manu sigexit(l, SIGILL);
294 1.1 manu return;
295 1.1 manu }
296 1.1 manu
297 1.6 fvdl linux_buildcontext(l, catcher, sp);
298 1.1 manu tf->tf_rdi = sigframe.info.lsi_signo;
299 1.1 manu tf->tf_rax = 0;
300 1.1 manu tf->tf_rsi = (long)&sfp->info;
301 1.1 manu tf->tf_rdx = (long)&sfp->uc;
302 1.1 manu
303 1.1 manu /*
304 1.1 manu * Remember we use signal stack
305 1.1 manu */
306 1.1 manu if (onstack)
307 1.17 ad l->l_sigstk.ss_flags |= SS_ONSTACK;
308 1.1 manu return;
309 1.1 manu }
310 1.1 manu
311 1.27 dsl int
312 1.27 dsl linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
313 1.27 dsl {
314 1.14 manu printf("linux_sys_modify_ldt\n");
315 1.1 manu return 0;
316 1.1 manu }
317 1.1 manu
318 1.27 dsl int
319 1.27 dsl linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
320 1.27 dsl {
321 1.1 manu return 0;
322 1.1 manu }
323 1.1 manu
324 1.27 dsl int
325 1.27 dsl linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
326 1.27 dsl {
327 1.1 manu return 0;
328 1.1 manu }
329 1.1 manu
330 1.1 manu dev_t
331 1.26 dsl linux_fakedev(dev_t dev, int raw)
332 1.1 manu {
333 1.21 manu
334 1.21 manu extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
335 1.21 manu const struct cdevsw *cd = cdevsw_lookup(dev);
336 1.21 manu
337 1.21 manu if (raw) {
338 1.21 manu #if (NWSDISPLAY > 0)
339 1.21 manu extern const struct cdevsw wsdisplay_cdevsw;
340 1.21 manu if (cd == &wsdisplay_cdevsw)
341 1.21 manu return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
342 1.21 manu #endif
343 1.21 manu }
344 1.21 manu
345 1.21 manu if (cd == &ptc_cdevsw)
346 1.21 manu return makedev(LINUX_PTC_MAJOR, minor(dev));
347 1.21 manu if (cd == &pts_cdevsw)
348 1.21 manu return makedev(LINUX_PTS_MAJOR, minor(dev));
349 1.21 manu
350 1.15 manu return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
351 1.15 manu | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
352 1.15 manu | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
353 1.1 manu }
354 1.1 manu
355 1.27 dsl int
356 1.27 dsl linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
357 1.27 dsl {
358 1.1 manu return 0;
359 1.1 manu }
360 1.1 manu
361 1.1 manu int
362 1.27 dsl linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
363 1.27 dsl {
364 1.6 fvdl struct linux_ucontext *luctx;
365 1.1 manu struct trapframe *tf = l->l_md.md_regs;
366 1.1 manu struct linux_sigcontext *lsigctx;
367 1.1 manu struct linux__fpstate fpstate;
368 1.6 fvdl struct linux_rt_sigframe frame, *fp;
369 1.1 manu ucontext_t uctx;
370 1.1 manu mcontext_t *mctx;
371 1.24 ad struct fxsave64 *fxarea;
372 1.1 manu int error;
373 1.1 manu
374 1.6 fvdl fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
375 1.6 fvdl if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
376 1.29 ad mutex_enter(l->l_proc->p_lock);
377 1.1 manu sigexit(l, SIGILL);
378 1.1 manu return error;
379 1.1 manu }
380 1.6 fvdl luctx = &frame.uc;
381 1.6 fvdl lsigctx = &luctx->luc_mcontext;
382 1.1 manu
383 1.29.2.2 yamt memset(&uctx, 0, sizeof(uctx));
384 1.1 manu mctx = (mcontext_t *)&uctx.uc_mcontext;
385 1.24 ad fxarea = (struct fxsave64 *)&mctx->__fpregs;
386 1.1 manu
387 1.1 manu /*
388 1.1 manu * Set the flags. Linux always have CPU, stack and signal state,
389 1.1 manu * FPU is optional. uc_flags is not used to tell what we have.
390 1.1 manu */
391 1.1 manu uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
392 1.1 manu if (lsigctx->fpstate != NULL)
393 1.1 manu uctx.uc_flags |= _UC_FPU;
394 1.1 manu uctx.uc_link = NULL;
395 1.1 manu
396 1.1 manu /*
397 1.1 manu * Signal set
398 1.1 manu */
399 1.6 fvdl linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
400 1.1 manu
401 1.1 manu /*
402 1.1 manu * CPU state
403 1.1 manu */
404 1.1 manu mctx->__gregs[_REG_R8] = lsigctx->r8;
405 1.1 manu mctx->__gregs[_REG_R9] = lsigctx->r9;
406 1.1 manu mctx->__gregs[_REG_R10] = lsigctx->r10;
407 1.1 manu mctx->__gregs[_REG_R11] = lsigctx->r11;
408 1.1 manu mctx->__gregs[_REG_R12] = lsigctx->r12;
409 1.1 manu mctx->__gregs[_REG_R13] = lsigctx->r13;
410 1.1 manu mctx->__gregs[_REG_R14] = lsigctx->r14;
411 1.1 manu mctx->__gregs[_REG_R15] = lsigctx->r15;
412 1.1 manu mctx->__gregs[_REG_RDI] = lsigctx->rdi;
413 1.1 manu mctx->__gregs[_REG_RSI] = lsigctx->rsi;
414 1.1 manu mctx->__gregs[_REG_RBP] = lsigctx->rbp;
415 1.1 manu mctx->__gregs[_REG_RBX] = lsigctx->rbx;
416 1.23 njoly mctx->__gregs[_REG_RAX] = lsigctx->rax;
417 1.1 manu mctx->__gregs[_REG_RDX] = lsigctx->rdx;
418 1.1 manu mctx->__gregs[_REG_RCX] = lsigctx->rcx;
419 1.1 manu mctx->__gregs[_REG_RIP] = lsigctx->rip;
420 1.28 dsl mctx->__gregs[_REG_RFLAGS] = lsigctx->eflags;
421 1.1 manu mctx->__gregs[_REG_CS] = lsigctx->cs;
422 1.1 manu mctx->__gregs[_REG_GS] = lsigctx->gs;
423 1.1 manu mctx->__gregs[_REG_FS] = lsigctx->fs;
424 1.1 manu mctx->__gregs[_REG_ERR] = lsigctx->err;
425 1.1 manu mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
426 1.1 manu mctx->__gregs[_REG_ES] = tf->tf_es;
427 1.1 manu mctx->__gregs[_REG_DS] = tf->tf_ds;
428 1.28 dsl mctx->__gregs[_REG_RSP] = lsigctx->rsp; /* XXX */
429 1.1 manu mctx->__gregs[_REG_SS] = tf->tf_ss;
430 1.1 manu
431 1.1 manu /*
432 1.1 manu * FPU state
433 1.1 manu */
434 1.1 manu if (lsigctx->fpstate != NULL) {
435 1.1 manu error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
436 1.1 manu if (error != 0) {
437 1.29 ad mutex_enter(l->l_proc->p_lock);
438 1.1 manu sigexit(l, SIGILL);
439 1.1 manu return error;
440 1.1 manu }
441 1.1 manu
442 1.24 ad fxarea->fx_fcw = fpstate.cwd;
443 1.24 ad fxarea->fx_fsw = fpstate.swd;
444 1.24 ad fxarea->fx_ftw = fpstate.twd;
445 1.24 ad fxarea->fx_fop = fpstate.fop;
446 1.24 ad fxarea->fx_rip = fpstate.rip;
447 1.24 ad fxarea->fx_rdp = fpstate.rdp;
448 1.24 ad fxarea->fx_mxcsr = fpstate.mxcsr;
449 1.24 ad fxarea->fx_mxcsr_mask = fpstate.mxcsr_mask;
450 1.24 ad memcpy(&fxarea->fx_st, &fpstate.st_space,
451 1.24 ad sizeof(fxarea->fx_st));
452 1.24 ad memcpy(&fxarea->fx_xmm, &fpstate.xmm_space,
453 1.24 ad sizeof(fxarea->fx_xmm));
454 1.1 manu }
455 1.1 manu
456 1.1 manu /*
457 1.1 manu * And the stack
458 1.1 manu */
459 1.1 manu uctx.uc_stack.ss_flags = 0;
460 1.29.2.2 yamt if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
461 1.29.2.2 yamt uctx.uc_stack.ss_flags |= SS_ONSTACK;
462 1.1 manu
463 1.29.2.2 yamt if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
464 1.29.2.2 yamt uctx.uc_stack.ss_flags |= SS_DISABLE;
465 1.1 manu
466 1.6 fvdl uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
467 1.6 fvdl uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
468 1.1 manu
469 1.1 manu /*
470 1.1 manu * And let setucontext deal with that.
471 1.1 manu */
472 1.29 ad mutex_enter(l->l_proc->p_lock);
473 1.16 ad error = setucontext(l, &uctx);
474 1.29 ad mutex_exit(l->l_proc->p_lock);
475 1.23 njoly if (error)
476 1.23 njoly return error;
477 1.16 ad
478 1.23 njoly return EJUSTRETURN;
479 1.1 manu }
480 1.1 manu
481 1.1 manu int
482 1.29.2.4 yamt linux_sys_arch_prctl(struct lwp *l,
483 1.29.2.4 yamt const struct linux_sys_arch_prctl_args *uap, register_t *retval)
484 1.1 manu {
485 1.27 dsl /* {
486 1.1 manu syscallarg(int) code;
487 1.1 manu syscallarg(unsigned long) addr;
488 1.27 dsl } */
489 1.29.2.5 yamt void *addr = (void *)SCARG(uap, addr);
490 1.1 manu
491 1.1 manu switch(SCARG(uap, code)) {
492 1.1 manu case LINUX_ARCH_SET_GS:
493 1.29.2.5 yamt return x86_set_sdbase(addr, 'g', l, true);
494 1.1 manu
495 1.1 manu case LINUX_ARCH_GET_GS:
496 1.29.2.5 yamt return x86_get_sdbase(addr, 'g');
497 1.1 manu
498 1.1 manu case LINUX_ARCH_SET_FS:
499 1.29.2.5 yamt return x86_set_sdbase(addr, 'f', l, true);
500 1.1 manu
501 1.1 manu case LINUX_ARCH_GET_FS:
502 1.29.2.5 yamt return x86_get_sdbase(addr, 'f');
503 1.1 manu
504 1.1 manu default:
505 1.1 manu #ifdef DEBUG_LINUX
506 1.1 manu printf("linux_sys_arch_prctl: unexpected code %d\n",
507 1.1 manu SCARG(uap, code));
508 1.1 manu #endif
509 1.1 manu return EINVAL;
510 1.1 manu }
511 1.29.2.5 yamt /* NOTREACHED */
512 1.1 manu }
513 1.4 fvdl
514 1.4 fvdl const int linux_vsyscall_to_syscall[] = {
515 1.4 fvdl LINUX_SYS_gettimeofday,
516 1.4 fvdl LINUX_SYS_time,
517 1.29.2.2 yamt LINUX_SYS_nosys, /* nosys */
518 1.29.2.2 yamt LINUX_SYS_nosys, /* nosys */
519 1.4 fvdl };
520 1.4 fvdl
521 1.4 fvdl int
522 1.4 fvdl linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
523 1.4 fvdl {
524 1.4 fvdl struct trapframe *tf = arg;
525 1.4 fvdl uint64_t retaddr;
526 1.4 fvdl int vsyscallnr;
527 1.4 fvdl
528 1.4 fvdl /*
529 1.4 fvdl * Check for a vsyscall. %rip must be the fault address,
530 1.4 fvdl * and the address must be in the Linux vsyscall area.
531 1.4 fvdl * Also, vsyscalls are only done at 1024-byte boundaries.
532 1.4 fvdl */
533 1.4 fvdl
534 1.4 fvdl if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
535 1.4 fvdl return 0;
536 1.4 fvdl
537 1.4 fvdl if (trapaddr != tf->tf_rip)
538 1.4 fvdl return 0;
539 1.4 fvdl
540 1.4 fvdl if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
541 1.4 fvdl return 0;
542 1.4 fvdl
543 1.4 fvdl vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
544 1.4 fvdl
545 1.4 fvdl if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
546 1.4 fvdl return 0;
547 1.4 fvdl
548 1.4 fvdl /*
549 1.4 fvdl * Get the return address from the top of the stack,
550 1.4 fvdl * and fix up the return address.
551 1.4 fvdl * This assumes the faulting instruction was callq *reg,
552 1.4 fvdl * which is the only way that vsyscalls are ever entered.
553 1.4 fvdl */
554 1.4 fvdl if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
555 1.4 fvdl return 0;
556 1.4 fvdl tf->tf_rip = retaddr;
557 1.4 fvdl tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
558 1.4 fvdl tf->tf_rsp += 8; /* "pop" the return address */
559 1.4 fvdl
560 1.4 fvdl #if 0
561 1.4 fvdl printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
562 1.4 fvdl (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
563 1.4 fvdl vsyscallnr, (int)tf->tf_rax);
564 1.4 fvdl #endif
565 1.4 fvdl
566 1.4 fvdl (*l->l_proc->p_md.md_syscall)(tf);
567 1.4 fvdl
568 1.4 fvdl return 1;
569 1.4 fvdl }
570 1.5 fvdl
571 1.5 fvdl static void
572 1.5 fvdl linux_buildcontext(struct lwp *l, void *catcher, void *f)
573 1.5 fvdl {
574 1.5 fvdl struct trapframe *tf = l->l_md.md_regs;
575 1.5 fvdl
576 1.5 fvdl tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
577 1.5 fvdl tf->tf_rip = (u_int64_t)catcher;
578 1.5 fvdl tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
579 1.29.2.2 yamt tf->tf_rflags &= ~PSL_CLEARSIG;
580 1.5 fvdl tf->tf_rsp = (u_int64_t)f;
581 1.5 fvdl tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
582 1.5 fvdl }
583