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