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