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