linux_machdep.c revision 1.41 1 /* $NetBSD: linux_machdep.c,v 1.41 2013/10/23 20:18:51 drochner 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.41 2013/10/23 20:18:51 drochner 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/ucontext.h>
46 #include <sys/conf.h>
47 #include <sys/pcu.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 #include <machine/cpufunc.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 extern const pcu_ops_t fpu_ops;
69
70 #include <compat/linux/common/linux_signal.h>
71 #include <compat/linux/common/linux_errno.h>
72 #include <compat/linux/common/linux_exec.h>
73 #include <compat/linux/common/linux_ioctl.h>
74 #include <compat/linux/common/linux_prctl.h>
75 #include <compat/linux/common/linux_machdep.h>
76 #include <compat/linux/common/linux_ipc.h>
77 #include <compat/linux/common/linux_sem.h>
78 #include <compat/linux/linux_syscall.h>
79 #include <compat/linux/linux_syscallargs.h>
80
81 static void linux_buildcontext(struct lwp *, void *, void *);
82
83 void
84 linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
85 {
86 struct pcb *pcb = lwp_getpcb(l);
87 struct trapframe *tf;
88
89 pcu_discard(&fpu_ops, false);
90
91 pcb->pcb_flags = 0;
92 pcb->pcb_savefpu.fp_fxsave.fx_fcw = __NetBSD_NPXCW__;
93 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr = __INITIAL_MXCSR__;
94 pcb->pcb_savefpu.fp_fxsave.fx_mxcsr_mask = __INITIAL_MXCSR_MASK__;
95
96 l->l_proc->p_flag &= ~PK_32;
97
98 tf = l->l_md.md_regs;
99 tf->tf_rax = 0;
100 tf->tf_rbx = 0;
101 tf->tf_rcx = epp->ep_entry;
102 tf->tf_rdx = 0;
103 tf->tf_rsi = 0;
104 tf->tf_rdi = 0;
105 tf->tf_rbp = 0;
106 tf->tf_rsp = stack;
107 tf->tf_r8 = 0;
108 tf->tf_r9 = 0;
109 tf->tf_r10 = 0;
110 tf->tf_r11 = 0;
111 tf->tf_r12 = 0;
112 tf->tf_r13 = 0;
113 tf->tf_r14 = 0;
114 tf->tf_r15 = 0;
115 tf->tf_rip = epp->ep_entry;
116 tf->tf_rflags = PSL_USERSET;
117 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
118 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
119 tf->tf_ds = 0;
120 tf->tf_es = 0;
121 cpu_fsgs_zero(l);
122
123 return;
124 }
125
126 void
127 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
128 {
129 struct lwp *l = curlwp;
130 struct proc *p = l->l_proc;
131 struct pcb *pcb = lwp_getpcb(l);
132 struct sigacts *ps = p->p_sigacts;
133 int onstack, error;
134 int sig = ksi->ksi_signo;
135 struct linux_rt_sigframe *sfp, sigframe;
136 struct linux__fpstate *fpsp, fpstate;
137 struct fpreg fpregs;
138 struct trapframe *tf = l->l_md.md_regs;
139 sig_t catcher = SIGACTION(p, sig).sa_handler;
140 linux_sigset_t lmask;
141 char *sp;
142
143 /* Do we need to jump onto the signal stack? */
144 onstack =
145 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
146 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
147
148 /* Allocate space for the signal handler context. */
149 if (onstack)
150 sp = ((char *)l->l_sigstk.ss_sp +
151 l->l_sigstk.ss_size);
152 else
153 sp = (char *)tf->tf_rsp - 128;
154
155 /*
156 * Save FPU state, if any
157 */
158 if (pcu_used_p(&fpu_ops)) {
159 sp = (char *)
160 (((long)sp - sizeof(struct linux__fpstate)) & ~0xfUL);
161 fpsp = (struct linux__fpstate *)sp;
162 } else
163 fpsp = NULL;
164
165 /*
166 * Populate the rt_sigframe
167 */
168 sp = (char *)
169 ((((long)sp - sizeof(struct linux_rt_sigframe)) & ~0xfUL) - 8);
170 sfp = (struct linux_rt_sigframe *)sp;
171
172 memset(&sigframe, 0, sizeof(sigframe));
173 if (ps->sa_sigdesc[sig].sd_vers != 0)
174 sigframe.pretcode =
175 (char *)(u_long)ps->sa_sigdesc[sig].sd_tramp;
176 else
177 sigframe.pretcode = NULL;
178
179 /*
180 * The user context
181 */
182 sigframe.uc.luc_flags = 0;
183 sigframe.uc.luc_link = NULL;
184
185 /* This is used regardless of SA_ONSTACK in Linux */
186 sigframe.uc.luc_stack.ss_sp = l->l_sigstk.ss_sp;
187 sigframe.uc.luc_stack.ss_size = l->l_sigstk.ss_size;
188 sigframe.uc.luc_stack.ss_flags = 0;
189 if (l->l_sigstk.ss_flags & SS_ONSTACK)
190 sigframe.uc.luc_stack.ss_flags |= LINUX_SS_ONSTACK;
191 if (l->l_sigstk.ss_flags & SS_DISABLE)
192 sigframe.uc.luc_stack.ss_flags |= LINUX_SS_DISABLE;
193
194 sigframe.uc.luc_mcontext.r8 = tf->tf_r8;
195 sigframe.uc.luc_mcontext.r9 = tf->tf_r9;
196 sigframe.uc.luc_mcontext.r10 = tf->tf_r10;
197 sigframe.uc.luc_mcontext.r11 = tf->tf_r11;
198 sigframe.uc.luc_mcontext.r12 = tf->tf_r12;
199 sigframe.uc.luc_mcontext.r13 = tf->tf_r13;
200 sigframe.uc.luc_mcontext.r14 = tf->tf_r14;
201 sigframe.uc.luc_mcontext.r15 = tf->tf_r15;
202 sigframe.uc.luc_mcontext.rdi = tf->tf_rdi;
203 sigframe.uc.luc_mcontext.rsi = tf->tf_rsi;
204 sigframe.uc.luc_mcontext.rbp = tf->tf_rbp;
205 sigframe.uc.luc_mcontext.rbx = tf->tf_rbx;
206 sigframe.uc.luc_mcontext.rdx = tf->tf_rdx;
207 sigframe.uc.luc_mcontext.rax = tf->tf_rax;
208 sigframe.uc.luc_mcontext.rcx = tf->tf_rcx;
209 sigframe.uc.luc_mcontext.rsp = tf->tf_rsp;
210 sigframe.uc.luc_mcontext.rip = tf->tf_rip;
211 sigframe.uc.luc_mcontext.eflags = tf->tf_rflags;
212 sigframe.uc.luc_mcontext.cs = tf->tf_cs;
213 sigframe.uc.luc_mcontext.gs = tf->tf_gs;
214 sigframe.uc.luc_mcontext.fs = tf->tf_fs;
215 sigframe.uc.luc_mcontext.err = tf->tf_err;
216 sigframe.uc.luc_mcontext.trapno = tf->tf_trapno;
217 native_to_linux_sigset(&lmask, mask);
218 sigframe.uc.luc_mcontext.oldmask = lmask.sig[0];
219 sigframe.uc.luc_mcontext.cr2 = (long)pcb->pcb_onfault;
220 sigframe.uc.luc_mcontext.fpstate = fpsp;
221 native_to_linux_sigset(&sigframe.uc.luc_sigmask, mask);
222 native_to_linux_siginfo(&sigframe.info, &ksi->ksi_info);
223 sendsig_reset(l, sig);
224 mutex_exit(p->p_lock);
225 error = 0;
226
227 /*
228 * Save FPU state, if any
229 */
230 if (fpsp != NULL) {
231 (void)process_read_fpregs(l, &fpregs);
232 memset(&fpstate, 0, sizeof(fpstate));
233 fpstate.cwd = fpregs.fp_fcw;
234 fpstate.swd = fpregs.fp_fsw;
235 fpstate.twd = fpregs.fp_ftw;
236 fpstate.fop = fpregs.fp_fop;
237 fpstate.rip = fpregs.fp_rip;
238 fpstate.rdp = fpregs.fp_rdp;
239 fpstate.mxcsr = fpregs.fp_mxcsr;
240 fpstate.mxcsr_mask = fpregs.fp_mxcsr_mask;
241 memcpy(&fpstate.st_space, &fpregs.fp_st,
242 sizeof(fpstate.st_space));
243 memcpy(&fpstate.xmm_space, &fpregs.fp_xmm,
244 sizeof(fpstate.xmm_space));
245 error = copyout(&fpstate, fpsp, sizeof(fpstate));
246 }
247
248 if (error == 0)
249 error = copyout(&sigframe, sp, sizeof(sigframe));
250
251 mutex_enter(p->p_lock);
252
253 if (error != 0) {
254 sigexit(l, SIGILL);
255 return;
256 }
257
258 linux_buildcontext(l, catcher, sp);
259 tf->tf_rdi = sigframe.info.lsi_signo;
260 tf->tf_rax = 0;
261 tf->tf_rsi = (long)&sfp->info;
262 tf->tf_rdx = (long)&sfp->uc;
263
264 /*
265 * Remember we use signal stack
266 */
267 if (onstack)
268 l->l_sigstk.ss_flags |= SS_ONSTACK;
269 return;
270 }
271
272 int
273 linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
274 {
275 printf("linux_sys_modify_ldt\n");
276 return 0;
277 }
278
279 int
280 linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
281 {
282 return 0;
283 }
284
285 int
286 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
287 {
288 return 0;
289 }
290
291 dev_t
292 linux_fakedev(dev_t dev, int raw)
293 {
294
295 extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
296 const struct cdevsw *cd = cdevsw_lookup(dev);
297
298 if (raw) {
299 #if (NWSDISPLAY > 0)
300 extern const struct cdevsw wsdisplay_cdevsw;
301 if (cd == &wsdisplay_cdevsw)
302 return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
303 #endif
304 }
305
306 if (cd == &ptc_cdevsw)
307 return makedev(LINUX_PTC_MAJOR, minor(dev));
308 if (cd == &pts_cdevsw)
309 return makedev(LINUX_PTS_MAJOR, minor(dev));
310
311 return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
312 | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
313 | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
314 }
315
316 int
317 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
318 {
319 return 0;
320 }
321
322 int
323 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
324 {
325 struct linux_ucontext *luctx;
326 struct trapframe *tf = l->l_md.md_regs;
327 struct linux_sigcontext *lsigctx;
328 struct linux__fpstate fpstate;
329 struct linux_rt_sigframe frame, *fp;
330 ucontext_t uctx;
331 mcontext_t *mctx;
332 struct fxsave64 *fxarea;
333 int error;
334
335 fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
336 if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
337 mutex_enter(l->l_proc->p_lock);
338 sigexit(l, SIGILL);
339 return error;
340 }
341 luctx = &frame.uc;
342 lsigctx = &luctx->luc_mcontext;
343
344 memset(&uctx, 0, sizeof(uctx));
345 mctx = (mcontext_t *)&uctx.uc_mcontext;
346 fxarea = (struct fxsave64 *)&mctx->__fpregs;
347
348 /*
349 * Set the flags. Linux always have CPU, stack and signal state,
350 * FPU is optional. uc_flags is not used to tell what we have.
351 */
352 uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
353 if (lsigctx->fpstate != NULL)
354 uctx.uc_flags |= _UC_FPU;
355 uctx.uc_link = NULL;
356
357 /*
358 * Signal set
359 */
360 linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
361
362 /*
363 * CPU state
364 */
365 mctx->__gregs[_REG_R8] = lsigctx->r8;
366 mctx->__gregs[_REG_R9] = lsigctx->r9;
367 mctx->__gregs[_REG_R10] = lsigctx->r10;
368 mctx->__gregs[_REG_R11] = lsigctx->r11;
369 mctx->__gregs[_REG_R12] = lsigctx->r12;
370 mctx->__gregs[_REG_R13] = lsigctx->r13;
371 mctx->__gregs[_REG_R14] = lsigctx->r14;
372 mctx->__gregs[_REG_R15] = lsigctx->r15;
373 mctx->__gregs[_REG_RDI] = lsigctx->rdi;
374 mctx->__gregs[_REG_RSI] = lsigctx->rsi;
375 mctx->__gregs[_REG_RBP] = lsigctx->rbp;
376 mctx->__gregs[_REG_RBX] = lsigctx->rbx;
377 mctx->__gregs[_REG_RAX] = lsigctx->rax;
378 mctx->__gregs[_REG_RDX] = lsigctx->rdx;
379 mctx->__gregs[_REG_RCX] = lsigctx->rcx;
380 mctx->__gregs[_REG_RIP] = lsigctx->rip;
381 mctx->__gregs[_REG_RFLAGS] = lsigctx->eflags;
382 mctx->__gregs[_REG_CS] = lsigctx->cs;
383 mctx->__gregs[_REG_GS] = lsigctx->gs;
384 mctx->__gregs[_REG_FS] = lsigctx->fs;
385 mctx->__gregs[_REG_ERR] = lsigctx->err;
386 mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
387 mctx->__gregs[_REG_ES] = tf->tf_es;
388 mctx->__gregs[_REG_DS] = tf->tf_ds;
389 mctx->__gregs[_REG_RSP] = lsigctx->rsp; /* XXX */
390 mctx->__gregs[_REG_SS] = tf->tf_ss;
391
392 /*
393 * FPU state
394 */
395 if (lsigctx->fpstate != NULL) {
396 error = copyin(lsigctx->fpstate, &fpstate, sizeof(fpstate));
397 if (error != 0) {
398 mutex_enter(l->l_proc->p_lock);
399 sigexit(l, SIGILL);
400 return error;
401 }
402
403 fxarea->fx_fcw = fpstate.cwd;
404 fxarea->fx_fsw = fpstate.swd;
405 fxarea->fx_ftw = fpstate.twd;
406 fxarea->fx_fop = fpstate.fop;
407 fxarea->fx_rip = fpstate.rip;
408 fxarea->fx_rdp = fpstate.rdp;
409 fxarea->fx_mxcsr = fpstate.mxcsr;
410 fxarea->fx_mxcsr_mask = fpstate.mxcsr_mask;
411 memcpy(&fxarea->fx_st, &fpstate.st_space,
412 sizeof(fxarea->fx_st));
413 memcpy(&fxarea->fx_xmm, &fpstate.xmm_space,
414 sizeof(fxarea->fx_xmm));
415 }
416
417 /*
418 * And the stack
419 */
420 uctx.uc_stack.ss_flags = 0;
421 if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
422 uctx.uc_stack.ss_flags |= SS_ONSTACK;
423
424 if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
425 uctx.uc_stack.ss_flags |= SS_DISABLE;
426
427 uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
428 uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
429
430 /*
431 * And let setucontext deal with that.
432 */
433 mutex_enter(l->l_proc->p_lock);
434 error = setucontext(l, &uctx);
435 mutex_exit(l->l_proc->p_lock);
436 if (error)
437 return error;
438
439 return EJUSTRETURN;
440 }
441
442 int
443 linux_sys_arch_prctl(struct lwp *l,
444 const struct linux_sys_arch_prctl_args *uap, register_t *retval)
445 {
446 /* {
447 syscallarg(int) code;
448 syscallarg(unsigned long) addr;
449 } */
450 void *addr = (void *)SCARG(uap, addr);
451
452 switch(SCARG(uap, code)) {
453 case LINUX_ARCH_SET_GS:
454 return x86_set_sdbase(addr, 'g', l, true);
455
456 case LINUX_ARCH_GET_GS:
457 return x86_get_sdbase(addr, 'g');
458
459 case LINUX_ARCH_SET_FS:
460 return x86_set_sdbase(addr, 'f', l, true);
461
462 case LINUX_ARCH_GET_FS:
463 return x86_get_sdbase(addr, 'f');
464
465 default:
466 #ifdef DEBUG_LINUX
467 printf("linux_sys_arch_prctl: unexpected code %d\n",
468 SCARG(uap, code));
469 #endif
470 return EINVAL;
471 }
472 /* NOTREACHED */
473 }
474
475 const int linux_vsyscall_to_syscall[] = {
476 LINUX_SYS_gettimeofday,
477 LINUX_SYS_time,
478 LINUX_SYS_nosys, /* nosys */
479 LINUX_SYS_nosys, /* nosys */
480 };
481
482 int
483 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
484 {
485 struct trapframe *tf = arg;
486 uint64_t retaddr;
487 int vsyscallnr;
488
489 /*
490 * Check for a vsyscall. %rip must be the fault address,
491 * and the address must be in the Linux vsyscall area.
492 * Also, vsyscalls are only done at 1024-byte boundaries.
493 */
494
495 if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
496 return 0;
497
498 if (trapaddr != tf->tf_rip)
499 return 0;
500
501 if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
502 return 0;
503
504 vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
505
506 if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
507 return 0;
508
509 /*
510 * Get the return address from the top of the stack,
511 * and fix up the return address.
512 * This assumes the faulting instruction was callq *reg,
513 * which is the only way that vsyscalls are ever entered.
514 */
515 if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
516 return 0;
517 tf->tf_rip = retaddr;
518 tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
519 tf->tf_rsp += 8; /* "pop" the return address */
520
521 #if 0
522 printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
523 (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
524 vsyscallnr, (int)tf->tf_rax);
525 #endif
526
527 (*l->l_proc->p_md.md_syscall)(tf);
528
529 return 1;
530 }
531
532 static void
533 linux_buildcontext(struct lwp *l, void *catcher, void *f)
534 {
535 struct trapframe *tf = l->l_md.md_regs;
536
537 tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
538 tf->tf_rip = (u_int64_t)catcher;
539 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
540 tf->tf_rflags &= ~PSL_CLEARSIG;
541 tf->tf_rsp = (u_int64_t)f;
542 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
543 }
544