linux_machdep.c revision 1.25.4.1 1 /* $NetBSD: linux_machdep.c,v 1.25.4.1 2007/12/26 19:49:04 ad 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.25.4.1 2007/12/26 19:49:04 ad 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/common/linux_ipc.h>
76 #include <compat/linux/common/linux_sem.h>
77 #include <compat/linux/linux_syscall.h>
78 #include <compat/linux/linux_syscallargs.h>
79
80 static void linux_buildcontext(struct lwp *, void *, void *);
81
82 void
83 linux_setregs(struct lwp *l, struct exec_package *epp, u_long stack)
84 {
85 struct pcb *pcb = &l->l_addr->u_pcb;
86 struct trapframe *tf;
87
88 /* If we were using the FPU, forget about it. */
89 if (l->l_addr->u_pcb.pcb_fpcpu != NULL)
90 fpusave_lwp(l, 0);
91
92 l->l_md.md_flags &= ~MDP_USEDFPU;
93 pcb->pcb_flags = 0;
94 pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
95 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
96 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
97 pcb->pcb_fs = 0;
98 pcb->pcb_gs = 0;
99
100 l->l_proc->p_flag &= ~PK_32;
101
102 tf = l->l_md.md_regs;
103 tf->tf_rax = 0;
104 tf->tf_rbx = 0;
105 tf->tf_rcx = epp->ep_entry;
106 tf->tf_rdx = 0;
107 tf->tf_rsi = 0;
108 tf->tf_rdi = 0;
109 tf->tf_rbp = 0;
110 tf->tf_rsp = stack;
111 tf->tf_r8 = 0;
112 tf->tf_r9 = 0;
113 tf->tf_r10 = 0;
114 tf->tf_r11 = 0;
115 tf->tf_r12 = 0;
116 tf->tf_r13 = 0;
117 tf->tf_r14 = 0;
118 tf->tf_r15 = 0;
119 tf->tf_rip = epp->ep_entry;
120 tf->tf_rflags = PSL_USERSET;
121 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
122 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
123 tf->tf_ds = 0;
124 tf->tf_es = 0;
125 tf->tf_fs = 0;
126 tf->tf_gs = 0;
127
128 return;
129 }
130
131 void
132 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
133 {
134 struct lwp *l = curlwp;
135 struct proc *p = l->l_proc;
136 struct sigacts *ps = p->p_sigacts;
137 int onstack, error;
138 int sig = ksi->ksi_signo;
139 struct linux_rt_sigframe *sfp, sigframe;
140 struct linux__fpstate *fpsp, fpstate;
141 struct fpreg fpregs;
142 struct trapframe *tf = l->l_md.md_regs;
143 sig_t catcher = SIGACTION(p, sig).sa_handler;
144 linux_sigset_t lmask;
145 char *sp;
146
147 /* Do we need to jump onto the signal stack? */
148 onstack =
149 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
150 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
151
152 /* Allocate space for the signal handler context. */
153 if (onstack)
154 sp = ((char *)l->l_sigstk.ss_sp +
155 l->l_sigstk.ss_size);
156 else
157 sp = (char *)tf->tf_rsp - 128;
158
159 /*
160 * Save FPU state, if any
161 */
162 if (l->l_md.md_flags & MDP_USEDFPU) {
163 sp = (char *)
164 (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
165 fpsp = (struct linux__fpstate *)sp;
166 } else
167 fpsp = NULL;
168
169 /*
170 * Populate the rt_sigframe
171 */
172 sp = (char *)
173 ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
174 sfp = (struct linux_rt_sigframe *)sp;
175
176 bzero(&sigframe, sizeof(sigframe));
177 if (ps->sa_sigdesc[sig].sd_vers != 0)
178 sigframe.pretcode =
179 (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
180 else
181 sigframe.pretcode = NULL;
182
183 /*
184 * The user context
185 */
186 sigframe.uc.luc_flags = 0;
187 sigframe.uc.luc_link = NULL;
188
189 /* This is used regardless of SA_ONSTACK in Linux */
190 sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
191 sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
192 sigframe.uc.luc_stack.ss_flags = 0;
193 if (l->l_sigstk.ss_flags & SS_ONSTACK)
194 sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
195 if (l->l_sigstk.ss_flags & SS_DISABLE)
196 sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
197
198 sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
199 sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
200 sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
201 sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
202 sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
203 sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
204 sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
205 sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
206 sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
207 sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
208 sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
209 sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
210 sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
211 sigframe.uc.luc_mcontext.rax = tf->tf_rax;
212 sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
213 sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
214 sigframe.uc.luc_mcontext.rip = tf->tf_rip;
215 sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
216 sigframe.uc.luc_mcontext.cs = tf->tf_cs;
217 sigframe.uc.luc_mcontext.gs = tf->tf_gs;
218 sigframe.uc.luc_mcontext.fs = tf->tf_fs;
219 sigframe.uc.luc_mcontext.err = tf->tf_err;
220 sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
221 native_to_linux_sigset(&lmask, mask);
222 sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
223 sigframe.uc.luc_mcontext.cr2 = (long)l->l_addr->u_pcb.pcb_onfault;
224 sigframe.uc.luc_mcontext.fpstate = fpsp;
225 native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
226
227 /*
228 * the siginfo structure
229 */
230 sigframe.info.lsi_signo = native_to_linux_signo[sig];
231 sigframe.info.lsi_errno = native_to_linux_errno[ksi->ksi_errno];
232 sigframe.info.lsi_code = ksi->ksi_code;
233
234 /* XXX This is a rought conversion, taken from i386 code */
235 switch (sigframe.info.lsi_signo) {
236 case LINUX_SIGILL:
237 case LINUX_SIGFPE:
238 case LINUX_SIGSEGV:
239 case LINUX_SIGBUS:
240 case LINUX_SIGTRAP:
241 sigframe.info._sifields._sigfault._addr = ksi->ksi_addr;
242 break;
243 case LINUX_SIGCHLD:
244 sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
245 sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
246 sigframe.info._sifields._sigchld._utime = ksi->ksi_utime;
247 sigframe.info._sifields._sigchld._stime = ksi->ksi_stime;
248
249 if (WCOREDUMP(ksi->ksi_status)) {
250 sigframe.info.lsi_code = LINUX_CLD_DUMPED;
251 sigframe.info._sifields._sigchld._status =
252 _WSTATUS(ksi->ksi_status);
253 } else if (_WSTATUS(ksi->ksi_status)) {
254 sigframe.info.lsi_code = LINUX_CLD_KILLED;
255 sigframe.info._sifields._sigchld._status =
256 _WSTATUS(ksi->ksi_status);
257 } else {
258 sigframe.info.lsi_code = LINUX_CLD_EXITED;
259 sigframe.info._sifields._sigchld._status =
260 ((ksi->ksi_status & 0xff00U) >> 8);
261 }
262 break;
263 case LINUX_SIGIO:
264 sigframe.info._sifields._sigpoll._band = ksi->ksi_band;
265 sigframe.info._sifields._sigpoll._fd = ksi->ksi_fd;
266 break;
267 default:
268 sigframe.info._sifields._sigchld._pid = ksi->ksi_pid;
269 sigframe.info._sifields._sigchld._uid = ksi->ksi_uid;
270 if ((sigframe.info.lsi_signo == LINUX_SIGALRM) ||
271 (sigframe.info.lsi_signo >= LINUX_SIGRTMIN))
272 sigframe.info._sifields._timer._sigval.sival_ptr =
273 ksi->ksi_value.sival_ptr;
274 break;
275 }
276
277 sendsig_reset(l, sig);
278 mutex_exit(&p->p_smutex);
279 error = 0;
280
281 /*
282 * Save FPU state, if any
283 */
284 if (fpsp != NULL) {
285 (void)process_read_fpregs(l, &fpregs);
286 bzero(&fpstate, sizeof(fpstate));
287 fpstate.cwd = fpregs.fp_fcw;
288 fpstate.swd = fpregs.fp_fsw;
289 fpstate.twd = fpregs.fp_ftw;
290 fpstate.fop = fpregs.fp_fop;
291 fpstate.rip = fpregs.fp_rip;
292 fpstate.rdp = fpregs.fp_rdp;
293 fpstate.mxcsr = fpregs.fp_mxcsr;
294 fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
295 memcpy(&fpstate.st_space, &fpregs.fp_st,
296 sizeof(fpstate.st_space));
297 memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
298 sizeof(fpstate.xmm_space));
299 error = copyout(&fpstate, fpsp, sizeof(fpstate));
300 }
301
302 if (error == 0)
303 error = copyout(&sigframe, sp, sizeof(sigframe));
304
305 mutex_enter(&p->p_smutex);
306
307 if (error != 0) {
308 sigexit(l, SIGILL);
309 return;
310 }
311
312 linux_buildcontext(l, catcher, sp);
313 tf->tf_rdi = sigframe.info.lsi_signo;
314 tf->tf_rax = 0;
315 tf->tf_rsi = (long)&sfp->info;
316 tf->tf_rdx = (long)&sfp->uc;
317
318 /*
319 * Remember we use signal stack
320 */
321 if (onstack)
322 l->l_sigstk.ss_flags |= SS_ONSTACK;
323 return;
324 }
325
326 int
327 linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
328 {
329 printf("linux_sys_modify_ldt\n");
330 return 0;
331 }
332
333 int
334 linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
335 {
336 return 0;
337 }
338
339 int
340 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
341 {
342 return 0;
343 }
344
345 dev_t
346 linux_fakedev(dev_t dev, int raw)
347 {
348
349 extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
350 const struct cdevsw *cd = cdevsw_lookup(dev);
351
352 if (raw) {
353 #if (NWSDISPLAY > 0)
354 extern const struct cdevsw wsdisplay_cdevsw;
355 if (cd == &wsdisplay_cdevsw)
356 return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
357 #endif
358 }
359
360 if (cd == &ptc_cdevsw)
361 return makedev(LINUX_PTC_MAJOR, minor(dev));
362 if (cd == &pts_cdevsw)
363 return makedev(LINUX_PTS_MAJOR, minor(dev));
364
365 return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
366 | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
367 | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
368 }
369
370 int
371 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
372 {
373 return 0;
374 }
375
376 int
377 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
378 {
379 struct linux_ucontext *luctx;
380 struct trapframe *tf = l->l_md.md_regs;
381 struct linux_sigcontext *lsigctx;
382 struct linux__fpstate fpstate;
383 struct linux_rt_sigframe frame, *fp;
384 ucontext_t uctx;
385 mcontext_t *mctx;
386 struct fxsave64 *fxarea;
387 int error;
388
389 fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
390 if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
391 mutex_enter(&l->l_proc->p_smutex);
392 sigexit(l, SIGILL);
393 return error;
394 }
395 luctx = &frame.uc;
396 lsigctx = &luctx->luc_mcontext;
397
398 bzero(&uctx, sizeof(uctx));
399 mctx = (mcontext_t *)&uctx.uc_mcontext;
400 fxarea = (struct fxsave64 *)&mctx->__fpregs;
401
402 /*
403 * Set the flags. Linux always have CPU, stack and signal state,
404 * FPU is optional. uc_flags is not used to tell what we have.
405 */
406 uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
407 if (lsigctx->fpstate != NULL)
408 uctx.uc_flags |= _UC_FPU;
409 uctx.uc_link = NULL;
410
411 /*
412 * Signal set
413 */
414 linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
415
416 /*
417 * CPU state
418 */
419 mctx->__gregs[_REG_R8] = lsigctx->r8;
420 mctx->__gregs[_REG_R9] = lsigctx->r9;
421 mctx->__gregs[_REG_R10] = lsigctx->r10;
422 mctx->__gregs[_REG_R11] = lsigctx->r11;
423 mctx->__gregs[_REG_R12] = lsigctx->r12;
424 mctx->__gregs[_REG_R13] = lsigctx->r13;
425 mctx->__gregs[_REG_R14] = lsigctx->r14;
426 mctx->__gregs[_REG_R15] = lsigctx->r15;
427 mctx->__gregs[_REG_RDI] = lsigctx->rdi;
428 mctx->__gregs[_REG_RSI] = lsigctx->rsi;
429 mctx->__gregs[_REG_RBP] = lsigctx->rbp;
430 mctx->__gregs[_REG_RBX] = lsigctx->rbx;
431 mctx->__gregs[_REG_RAX] = lsigctx->rax;
432 mctx->__gregs[_REG_RDX] = lsigctx->rdx;
433 mctx->__gregs[_REG_RCX] = lsigctx->rcx;
434 mctx->__gregs[_REG_RIP] = lsigctx->rip;
435 mctx->__gregs[_REG_RFL] = lsigctx->eflags;
436 mctx->__gregs[_REG_CS] = lsigctx->cs;
437 mctx->__gregs[_REG_GS] = lsigctx->gs;
438 mctx->__gregs[_REG_FS] = lsigctx->fs;
439 mctx->__gregs[_REG_ERR] = lsigctx->err;
440 mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
441 mctx->__gregs[_REG_ES] = tf->tf_es;
442 mctx->__gregs[_REG_DS] = tf->tf_ds;
443 mctx->__gregs[_REG_URSP] = lsigctx->rsp; /* XXX */
444 mctx->__gregs[_REG_SS] = tf->tf_ss;
445
446 /*
447 * FPU state
448 */
449 if (lsigctx->fpstate != NULL) {
450 error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
451 if (error != 0) {
452 mutex_enter(&l->l_proc->p_smutex);
453 sigexit(l, SIGILL);
454 return error;
455 }
456
457 fxarea->fx_fcw = fpstate.cwd;
458 fxarea->fx_fsw = fpstate.swd;
459 fxarea->fx_ftw = fpstate.twd;
460 fxarea->fx_fop = fpstate.fop;
461 fxarea->fx_rip = fpstate.rip;
462 fxarea->fx_rdp = fpstate.rdp;
463 fxarea->fx_mxcsr = fpstate.mxcsr;
464 fxarea->fx_mxcsr_mask = fpstate.mxcsr_mask;
465 memcpy(&fxarea->fx_st, &fpstate.st_space,
466 sizeof(fxarea->fx_st));
467 memcpy(&fxarea->fx_xmm, &fpstate.xmm_space,
468 sizeof(fxarea->fx_xmm));
469 }
470
471 /*
472 * And the stack
473 */
474 uctx.uc_stack.ss_flags = 0;
475 if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK);
476 uctx.uc_stack.ss_flags = SS_ONSTACK;
477
478 if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE);
479 uctx.uc_stack.ss_flags = SS_DISABLE;
480
481 uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
482 uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
483
484 /*
485 * And let setucontext deal with that.
486 */
487 mutex_enter(&l->l_proc->p_smutex);
488 error = setucontext(l, &uctx);
489 mutex_exit(&l->l_proc->p_smutex);
490 if (error)
491 return error;
492
493 return EJUSTRETURN;
494 }
495
496 int
497 linux_sys_arch_prctl(struct lwp *l, const struct linux_sys_arch_prctl_args *uap, register_t *retval)
498 {
499 /* {
500 syscallarg(int) code;
501 syscallarg(unsigned long) addr;
502 } */
503 struct pcb *pcb = &l->l_addr->u_pcb;
504 struct trapframe *tf = l->l_md.md_regs;
505 int error;
506 uint64_t taddr;
507
508 switch(SCARG(uap, code)) {
509 case LINUX_ARCH_SET_GS:
510 taddr = SCARG(uap, addr);
511 if (taddr >= VM_MAXUSER_ADDRESS)
512 return EINVAL;
513 pcb->pcb_gs = taddr;
514 pcb->pcb_flags |= PCB_GS64;
515 if (l == curlwp)
516 wrmsr(MSR_KERNELGSBASE, taddr);
517 break;
518
519 case LINUX_ARCH_GET_GS:
520 if (pcb->pcb_flags & PCB_GS64)
521 taddr = pcb->pcb_gs;
522 else {
523 error = memseg_baseaddr(l, tf->tf_fs, NULL, 0, &taddr);
524 if (error != 0)
525 return error;
526 }
527 error = copyout(&taddr, (char *)SCARG(uap, addr), 8);
528 if (error != 0)
529 return error;
530 break;
531
532 case LINUX_ARCH_SET_FS:
533 taddr = SCARG(uap, addr);
534 if (taddr >= VM_MAXUSER_ADDRESS)
535 return EINVAL;
536 pcb->pcb_fs = taddr;
537 pcb->pcb_flags |= PCB_FS64;
538 if (l == curlwp)
539 wrmsr(MSR_FSBASE, taddr);
540 break;
541
542 case LINUX_ARCH_GET_FS:
543 if (pcb->pcb_flags & PCB_FS64)
544 taddr = pcb->pcb_fs;
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 default:
556 #ifdef DEBUG_LINUX
557 printf("linux_sys_arch_prctl: unexpected code %d\n",
558 SCARG(uap, code));
559 #endif
560 return EINVAL;
561 }
562
563 return 0;
564 }
565
566 const int linux_vsyscall_to_syscall[] = {
567 LINUX_SYS_gettimeofday,
568 LINUX_SYS_time,
569 LINUX_SYS_nosys,
570 LINUX_SYS_nosys,
571 };
572
573 int
574 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
575 {
576 struct trapframe *tf = arg;
577 uint64_t retaddr;
578 int vsyscallnr;
579
580 /*
581 * Check for a vsyscall. %rip must be the fault address,
582 * and the address must be in the Linux vsyscall area.
583 * Also, vsyscalls are only done at 1024-byte boundaries.
584 */
585
586 if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
587 return 0;
588
589 if (trapaddr != tf->tf_rip)
590 return 0;
591
592 if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
593 return 0;
594
595 vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
596
597 if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
598 return 0;
599
600 /*
601 * Get the return address from the top of the stack,
602 * and fix up the return address.
603 * This assumes the faulting instruction was callq *reg,
604 * which is the only way that vsyscalls are ever entered.
605 */
606 if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
607 return 0;
608 tf->tf_rip = retaddr;
609 tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
610 tf->tf_rsp += 8; /* "pop" the return address */
611
612 #if 0
613 printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
614 (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
615 vsyscallnr, (int)tf->tf_rax);
616 #endif
617
618 (*l->l_proc->p_md.md_syscall)(tf);
619
620 return 1;
621 }
622
623 static void
624 linux_buildcontext(struct lwp *l, void *catcher, void *f)
625 {
626 struct trapframe *tf = l->l_md.md_regs;
627
628 tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
629 tf->tf_rip = (u_int64_t)catcher;
630 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
631 tf->tf_rflags &= ~(PSL_T|PSL_VM|PSL_AC);
632 tf->tf_rsp = (u_int64_t)f;
633 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
634 }
635
636 void *
637 linux_get_newtls(struct lwp *l)
638 {
639 struct trapframe *tf = l->l_md.md_regs;
640
641 return (void *)tf->tf_r8;
642 }
643
644 int
645 linux_set_newtls(struct lwp *l, void *tls)
646 {
647 struct linux_sys_arch_prctl_args cup;
648 register_t retval;
649
650 SCARG(&cup, code) = LINUX_ARCH_SET_FS;
651 SCARG(&cup, addr) = (unsigned long)tls;
652
653 return linux_sys_arch_prctl(l, &cup, &retval);
654 }
655