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