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