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