linux_machdep.c revision 1.46 1 /* $NetBSD: linux_machdep.c,v 1.46 2014/02/11 20:17:16 dsl 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.46 2014/02/11 20:17:16 dsl 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
48 #include <machine/reg.h>
49 #include <machine/pcb.h>
50 #include <machine/mcontext.h>
51 #include <machine/specialreg.h>
52 #include <machine/vmparam.h>
53 #include <machine/cpufunc.h>
54
55 /*
56 * To see whether wscons is configured (for virtual console ioctl calls).
57 */
58 #if defined(_KERNEL_OPT)
59 #include "wsdisplay.h"
60 #endif
61 #if (NWSDISPLAY > 0)
62 #include <dev/wscons/wsconsio.h>
63 #include <dev/wscons/wsdisplay_usl_io.h>
64 #endif
65
66
67 #include <compat/linux/common/linux_signal.h>
68 #include <compat/linux/common/linux_errno.h>
69 #include <compat/linux/common/linux_exec.h>
70 #include <compat/linux/common/linux_ioctl.h>
71 #include <compat/linux/common/linux_prctl.h>
72 #include <compat/linux/common/linux_machdep.h>
73 #include <compat/linux/common/linux_ipc.h>
74 #include <compat/linux/common/linux_sem.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(struct lwp *l, struct exec_package *epp, vaddr_t stack)
82 {
83 struct pcb *pcb = lwp_getpcb(l);
84 struct trapframe *tf;
85
86 /* If we were using the FPU, forget about it. */
87 if (pcb->pcb_fpcpu != NULL)
88 fpusave_lwp(l, 0);
89
90 l->l_md.md_flags &= ~MDL_USEDFPU;
91 pcb->pcb_flags = 0;
92 pcb->pcb_savefpu.sv_xmm.fx_cw = __NetBSD_NPXCW__;
93 pcb->pcb_savefpu.sv_xmm.fx_mxcsr = __INITIAL_MXCSR__;
94 pcb->pcb_savefpu.sv_xmm.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 = GSEL(GUDATA_SEL, SEL_UPL);
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;
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 (l->l_md.md_flags & MDL_USEDFPU) {
159 sp = (char *)
160 (((long)sp - sizeof (*fpsp)) & ~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 size_t fp_size = sizeof fpregs;
232 /* The netbsd and linux structures both match the fxsave data */
233 (void)process_read_fpregs(l, &fpregs, &fp_size);
234 error = copyout(&fpregs, fpsp, sizeof(*fpsp));
235 }
236
237 if (error == 0)
238 error = copyout(&sigframe, sp, sizeof(sigframe));
239
240 mutex_enter(p->p_lock);
241
242 if (error != 0) {
243 sigexit(l, SIGILL);
244 return;
245 }
246
247 linux_buildcontext(l, catcher, sp);
248 tf->tf_rdi = sigframe.info.lsi_signo;
249 tf->tf_rax = 0;
250 tf->tf_rsi = (long)&sfp->info;
251 tf->tf_rdx = (long)&sfp->uc;
252
253 /*
254 * Remember we use signal stack
255 */
256 if (onstack)
257 l->l_sigstk.ss_flags |= SS_ONSTACK;
258 return;
259 }
260
261 int
262 linux_sys_modify_ldt(struct lwp *l, const struct linux_sys_modify_ldt_args *v, register_t *retval)
263 {
264 printf("linux_sys_modify_ldt\n");
265 return 0;
266 }
267
268 int
269 linux_sys_iopl(struct lwp *l, const struct linux_sys_iopl_args *v, register_t *retval)
270 {
271 return 0;
272 }
273
274 int
275 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *v, register_t *retval)
276 {
277 return 0;
278 }
279
280 dev_t
281 linux_fakedev(dev_t dev, int raw)
282 {
283
284 extern const struct cdevsw ptc_cdevsw, pts_cdevsw;
285 const struct cdevsw *cd = cdevsw_lookup(dev);
286
287 if (raw) {
288 #if (NWSDISPLAY > 0)
289 extern const struct cdevsw wsdisplay_cdevsw;
290 if (cd == &wsdisplay_cdevsw)
291 return makedev(LINUX_CONS_MAJOR, (minor(dev) + 1));
292 #endif
293 }
294
295 if (cd == &ptc_cdevsw)
296 return makedev(LINUX_PTC_MAJOR, minor(dev));
297 if (cd == &pts_cdevsw)
298 return makedev(LINUX_PTS_MAJOR, minor(dev));
299
300 return ((minor(dev) & 0xff) | ((major(dev) & 0xfff) << 8)
301 | (((unsigned long long int) (minor(dev) & ~0xff)) << 12)
302 | (((unsigned long long int) (major(dev) & ~0xfff)) << 32));
303 }
304
305 int
306 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *v, register_t *retval)
307 {
308 return 0;
309 }
310
311 int
312 linux_sys_rt_sigreturn(struct lwp *l, const void *v, register_t *retval)
313 {
314 struct linux_ucontext *luctx;
315 struct trapframe *tf = l->l_md.md_regs;
316 struct linux_sigcontext *lsigctx;
317 struct linux_rt_sigframe frame, *fp;
318 ucontext_t uctx;
319 mcontext_t *mctx;
320 struct fxsave *fxarea;
321 int error;
322
323 fp = (struct linux_rt_sigframe *)(tf->tf_rsp - 8);
324 if ((error = copyin(fp, &frame, sizeof(frame))) != 0) {
325 mutex_enter(l->l_proc->p_lock);
326 sigexit(l, SIGILL);
327 return error;
328 }
329 luctx = &frame.uc;
330 lsigctx = &luctx->luc_mcontext;
331
332 memset(&uctx, 0, sizeof(uctx));
333 mctx = (mcontext_t *)&uctx.uc_mcontext;
334 fxarea = (struct fxsave *)&mctx->__fpregs;
335
336 /*
337 * Set the flags. Linux always have CPU, stack and signal state,
338 * FPU is optional. uc_flags is not used to tell what we have.
339 */
340 uctx.uc_flags = (_UC_SIGMASK|_UC_CPU|_UC_STACK|_UC_CLRSTACK);
341 if (lsigctx->fpstate != NULL)
342 uctx.uc_flags |= _UC_FPU;
343 uctx.uc_link = NULL;
344
345 /*
346 * Signal set
347 */
348 linux_to_native_sigset(&uctx.uc_sigmask, &luctx->luc_sigmask);
349
350 /*
351 * CPU state
352 */
353 mctx->__gregs[_REG_R8] = lsigctx->r8;
354 mctx->__gregs[_REG_R9] = lsigctx->r9;
355 mctx->__gregs[_REG_R10] = lsigctx->r10;
356 mctx->__gregs[_REG_R11] = lsigctx->r11;
357 mctx->__gregs[_REG_R12] = lsigctx->r12;
358 mctx->__gregs[_REG_R13] = lsigctx->r13;
359 mctx->__gregs[_REG_R14] = lsigctx->r14;
360 mctx->__gregs[_REG_R15] = lsigctx->r15;
361 mctx->__gregs[_REG_RDI] = lsigctx->rdi;
362 mctx->__gregs[_REG_RSI] = lsigctx->rsi;
363 mctx->__gregs[_REG_RBP] = lsigctx->rbp;
364 mctx->__gregs[_REG_RBX] = lsigctx->rbx;
365 mctx->__gregs[_REG_RAX] = lsigctx->rax;
366 mctx->__gregs[_REG_RDX] = lsigctx->rdx;
367 mctx->__gregs[_REG_RCX] = lsigctx->rcx;
368 mctx->__gregs[_REG_RIP] = lsigctx->rip;
369 mctx->__gregs[_REG_RFLAGS] = lsigctx->eflags;
370 mctx->__gregs[_REG_CS] = lsigctx->cs;
371 mctx->__gregs[_REG_GS] = lsigctx->gs;
372 mctx->__gregs[_REG_FS] = lsigctx->fs;
373 mctx->__gregs[_REG_ERR] = lsigctx->err;
374 mctx->__gregs[_REG_TRAPNO] = lsigctx->trapno;
375 mctx->__gregs[_REG_ES] = tf->tf_es;
376 mctx->__gregs[_REG_DS] = tf->tf_ds;
377 mctx->__gregs[_REG_RSP] = lsigctx->rsp; /* XXX */
378 mctx->__gregs[_REG_SS] = tf->tf_ss;
379
380 /*
381 * FPU state
382 */
383 if (lsigctx->fpstate != NULL) {
384 /* Both structures match the fxstate data */
385 error = copyin(lsigctx->fpstate, fxarea, sizeof(*fxarea));
386 if (error != 0) {
387 mutex_enter(l->l_proc->p_lock);
388 sigexit(l, SIGILL);
389 return error;
390 }
391 }
392
393 /*
394 * And the stack
395 */
396 uctx.uc_stack.ss_flags = 0;
397 if (luctx->luc_stack.ss_flags & LINUX_SS_ONSTACK)
398 uctx.uc_stack.ss_flags |= SS_ONSTACK;
399
400 if (luctx->luc_stack.ss_flags & LINUX_SS_DISABLE)
401 uctx.uc_stack.ss_flags |= SS_DISABLE;
402
403 uctx.uc_stack.ss_sp = luctx->luc_stack.ss_sp;
404 uctx.uc_stack.ss_size = luctx->luc_stack.ss_size;
405
406 /*
407 * And let setucontext deal with that.
408 */
409 mutex_enter(l->l_proc->p_lock);
410 error = setucontext(l, &uctx);
411 mutex_exit(l->l_proc->p_lock);
412 if (error)
413 return error;
414
415 return EJUSTRETURN;
416 }
417
418 int
419 linux_sys_arch_prctl(struct lwp *l,
420 const struct linux_sys_arch_prctl_args *uap, register_t *retval)
421 {
422 /* {
423 syscallarg(int) code;
424 syscallarg(unsigned long) addr;
425 } */
426 void *addr = (void *)SCARG(uap, addr);
427
428 switch(SCARG(uap, code)) {
429 case LINUX_ARCH_SET_GS:
430 return x86_set_sdbase(addr, 'g', l, true);
431
432 case LINUX_ARCH_GET_GS:
433 return x86_get_sdbase(addr, 'g');
434
435 case LINUX_ARCH_SET_FS:
436 return x86_set_sdbase(addr, 'f', l, true);
437
438 case LINUX_ARCH_GET_FS:
439 return x86_get_sdbase(addr, 'f');
440
441 default:
442 #ifdef DEBUG_LINUX
443 printf("linux_sys_arch_prctl: unexpected code %d\n",
444 SCARG(uap, code));
445 #endif
446 return EINVAL;
447 }
448 /* NOTREACHED */
449 }
450
451 const int linux_vsyscall_to_syscall[] = {
452 LINUX_SYS_gettimeofday,
453 LINUX_SYS_time,
454 LINUX_SYS_nosys, /* nosys */
455 LINUX_SYS_nosys, /* nosys */
456 };
457
458 int
459 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
460 {
461 struct trapframe *tf = arg;
462 uint64_t retaddr;
463 int vsyscallnr;
464
465 /*
466 * Check for a vsyscall. %rip must be the fault address,
467 * and the address must be in the Linux vsyscall area.
468 * Also, vsyscalls are only done at 1024-byte boundaries.
469 */
470
471 if (__predict_true(trapaddr < LINUX_VSYSCALL_START))
472 return 0;
473
474 if (trapaddr != tf->tf_rip)
475 return 0;
476
477 if ((tf->tf_rip & (LINUX_VSYSCALL_SIZE - 1)) != 0)
478 return 0;
479
480 vsyscallnr = (tf->tf_rip - LINUX_VSYSCALL_START) / LINUX_VSYSCALL_SIZE;
481
482 if (vsyscallnr > LINUX_VSYSCALL_MAXNR)
483 return 0;
484
485 /*
486 * Get the return address from the top of the stack,
487 * and fix up the return address.
488 * This assumes the faulting instruction was callq *reg,
489 * which is the only way that vsyscalls are ever entered.
490 */
491 if (copyin((void *)tf->tf_rsp, &retaddr, sizeof retaddr) != 0)
492 return 0;
493 tf->tf_rip = retaddr;
494 tf->tf_rax = linux_vsyscall_to_syscall[vsyscallnr];
495 tf->tf_rsp += 8; /* "pop" the return address */
496
497 #if 0
498 printf("usertrap: rip %p rsp %p retaddr %p vsys %d sys %d\n",
499 (void *)tf->tf_rip, (void *)tf->tf_rsp, (void *)retaddr,
500 vsyscallnr, (int)tf->tf_rax);
501 #endif
502
503 (*l->l_proc->p_md.md_syscall)(tf);
504
505 return 1;
506 }
507
508 static void
509 linux_buildcontext(struct lwp *l, void *catcher, void *f)
510 {
511 struct trapframe *tf = l->l_md.md_regs;
512
513 tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
514 tf->tf_rip = (u_int64_t)catcher;
515 tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
516 tf->tf_rflags &= ~PSL_CLEARSIG;
517 tf->tf_rsp = (u_int64_t)f;
518 tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
519 }
520