1 /* $NetBSD: linux_machdep.c,v 1.48 2014/11/09 17:48:08 maxv Exp $ */
2
3 /*-
4 * Copyright (c) 1995, 2000, 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Frank van der Linden and Emmanuel Dreyfus.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.48 2014/11/09 17:48:08 maxv Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/signalvar.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/buf.h>
41 #include <sys/reboot.h>
42 #include <sys/conf.h>
43 #include <sys/exec.h>
44 #include <sys/file.h>
45 #include <sys/callout.h>
46 #include <sys/mbuf.h>
47 #include <sys/msgbuf.h>
48 #include <sys/mount.h>
49 #include <sys/vnode.h>
50 #include <sys/device.h>
51 #include <sys/syscallargs.h>
52 #include <sys/filedesc.h>
53 #include <sys/exec_elf.h>
54 #include <sys/disklabel.h>
55 #include <sys/ioctl.h>
56 #include <miscfs/specfs/specdev.h>
57
58 #include <compat/linux/common/linux_types.h>
59 #include <compat/linux/common/linux_signal.h>
60 #include <compat/linux/common/linux_util.h>
61 #include <compat/linux/common/linux_ioctl.h>
62 #include <compat/linux/common/linux_hdio.h>
63 #include <compat/linux/common/linux_exec.h>
64 #include <compat/linux/common/linux_machdep.h>
65
66 #include <compat/linux/linux_syscallargs.h>
67
68 #include <sys/cpu.h>
69 #include <machine/fpu.h>
70 #include <machine/psl.h>
71 #include <machine/pcb.h>
72 #include <machine/vmparam.h>
73
74 /*
75 * To see whether wscons is configured (for virtual console ioctl calls).
76 */
77 #if defined(_KERNEL_OPT)
78 #include "wsdisplay.h"
79 #endif
80 #if (NWSDISPLAY > 0)
81 #include <dev/wscons/wsconsio.h>
82 #include <dev/wscons/wsdisplay_usl_io.h>
83 #endif
84
85 /*
86 * Set set up registers on exec.
87 */
88 void
linux_setregs(struct lwp * l,struct exec_package * pack,vaddr_t stack)89 linux_setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
90 {
91 setregs(l, pack, stack);
92 }
93
94 /*
95 * Send an interrupt to process.
96 *
97 * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
98 * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
99 *
100 * XXX Does not work well yet with RT signals
101 *
102 */
103
104 void
linux_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)105 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
106 {
107 const int sig = ksi->ksi_signo;
108 struct lwp *l = curlwp;
109 struct proc *p = l->l_proc;
110 struct trapframe *tf;
111 sig_t catcher = SIGACTION(p, sig).sa_handler;
112 struct linux_sigregs frame;
113 struct linux_pt_regs linux_regs;
114 struct linux_sigcontext sc;
115 register_t fp;
116 int onstack, error;
117 int i;
118
119 tf = trapframe(l);
120
121 /*
122 * Do we need to jump onto the signal stack?
123 */
124 onstack =
125 (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
126 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
127
128 /*
129 * Signal stack is broken (see at the end of linux_sigreturn), so we do
130 * not use it yet. XXX fix this.
131 */
132 onstack=0;
133
134 /*
135 * Allocate space for the signal handler context.
136 */
137 if (onstack) {
138 fp = (register_t)
139 ((char *)l->l_sigstk.ss_sp +
140 l->l_sigstk.ss_size);
141 } else {
142 fp = tf->tf_fixreg[1];
143 }
144 #ifdef DEBUG_LINUX
145 printf("fp at start of linux_sendsig = %x\n", fp);
146 #endif
147 fp -= sizeof(struct linux_sigregs);
148 fp &= ~0xf;
149
150 /*
151 * Prepare a sigcontext for later.
152 */
153 memset(&sc, 0, sizeof sc);
154 sc.lsignal = (int)native_to_linux_signo[sig];
155 sc.lhandler = (unsigned long)catcher;
156 native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
157 sc.lregs = (struct linux_pt_regs*)fp;
158
159 /*
160 * Setup the signal stack frame as Linux does it in
161 * arch/ppc/kernel/signal.c:setup_frame()
162 *
163 * Save register context.
164 */
165 for (i = 0; i < 32; i++)
166 linux_regs.lgpr[i] = tf->tf_fixreg[i];
167 linux_regs.lnip = tf->tf_srr0;
168 linux_regs.lmsr = tf->tf_srr1 & PSL_USERSRR1;
169 linux_regs.lorig_gpr3 = tf->tf_fixreg[3]; /* XXX Is that right? */
170 linux_regs.lctr = tf->tf_ctr;
171 linux_regs.llink = tf->tf_lr;
172 linux_regs.lxer = tf->tf_xer;
173 linux_regs.lccr = tf->tf_cr;
174 linux_regs.lmq = 0; /* Unused, 601 only */
175 linux_regs.ltrap = tf->tf_exc;
176 linux_regs.ldar = tf->tf_dar;
177 linux_regs.ldsisr = tf->tf_dsisr;
178 linux_regs.lresult = 0;
179
180 memset(&frame, 0, sizeof(frame));
181 memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
182
183 #ifdef PPC_HAVE_FPU
184 fpu_save();
185 #endif
186 memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpreg, sizeof(frame.lfp_regs));
187
188 /*
189 * Copy Linux's signal trampoline on the user stack It should not
190 * be used, but Linux binaries might expect it to be there.
191 */
192 frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
193 frame.ltramp[1] = 0x44000002; /* sc */
194
195 /*
196 * Move it to the user stack
197 * There is a little trick here, about the LINUX_ABIGAP: the
198 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
199 * binaries. But the Linux kernel seems to do without it, and it
200 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
201 */
202 sendsig_reset(l, sig);
203 mutex_exit(p->p_lock);
204 error = copyout(&frame, (void *)fp, sizeof (frame) - LINUX_ABIGAP);
205
206 if (error != 0) {
207 /*
208 * Process has trashed its stack; give it an illegal
209 * instruction to halt it in its tracks.
210 */
211 mutex_enter(p->p_lock);
212 sigexit(l, SIGILL);
213 /* NOTREACHED */
214 }
215
216 /*
217 * Add a sigcontext on the stack
218 */
219 fp -= sizeof(struct linux_sigcontext);
220 error = copyout(&sc, (void *)fp, sizeof (struct linux_sigcontext));
221 mutex_enter(p->p_lock);
222
223 if (error != 0) {
224 /*
225 * Process has trashed its stack; give it an illegal
226 * instruction to halt it in its tracks.
227 */
228 sigexit(l, SIGILL);
229 /* NOTREACHED */
230 }
231
232 /*
233 * Set the registers according to how the Linux process expects them.
234 * "Mind the gap" Linux expects a gap here.
235 */
236 tf->tf_fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
237 tf->tf_lr = (int)catcher;
238 tf->tf_fixreg[3] = (int)native_to_linux_signo[sig];
239 tf->tf_fixreg[4] = fp;
240 tf->tf_srr0 = (int)p->p_sigctx.ps_sigcode;
241
242 #ifdef DEBUG_LINUX
243 printf("fp at end of linux_sendsig = %x\n", fp);
244 #endif
245 /*
246 * Remember that we're now on the signal stack.
247 */
248 if (onstack)
249 l->l_sigstk.ss_flags |= SS_ONSTACK;
250 #ifdef DEBUG_LINUX
251 printf("linux_sendsig: exiting. fp=0x%lx\n",(long)fp);
252 #endif
253 }
254
255 /*
256 * System call to cleanup state after a signal
257 * has been taken. Reset signal mask and
258 * stack state from context left by sendsig (above).
259 * Return to previous pc and psl as specified by
260 * context left by sendsig. Check carefully to
261 * make sure that the user has not modified the
262 * psl to gain improper privileges or to cause
263 * a machine fault.
264 *
265 * XXX not tested
266 */
267 int
linux_sys_rt_sigreturn(struct lwp * l,const struct linux_sys_rt_sigreturn_args * uap,register_t * retval)268 linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval)
269 {
270 /* {
271 syscallarg(struct linux_rt_sigframe *) sfp;
272 } */
273 struct proc *p = l->l_proc;
274 struct linux_rt_sigframe *scp, sigframe;
275 struct linux_sigregs sregs;
276 struct linux_pt_regs *lregs;
277 struct trapframe *tf;
278 sigset_t mask;
279 int i;
280
281 /*
282 * The trampoline code hands us the context.
283 * It is unsafe to keep track of it ourselves, in the event that a
284 * program jumps out of a signal handler.
285 */
286 scp = SCARG(uap, sfp);
287
288 /*
289 * Get the context from user stack
290 */
291 if (copyin((void *)scp, &sigframe, sizeof(*scp)))
292 return (EFAULT);
293
294 /*
295 * Restore register context.
296 */
297 if (copyin((void *)sigframe.luc.luc_context.lregs,
298 &sregs, sizeof(sregs)))
299 return (EFAULT);
300 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
301
302 tf = trapframe(l);
303 #ifdef DEBUG_LINUX
304 printf("linux_sys_rt_sigreturn: trapframe=0x%lx scp=0x%lx\n",
305 (unsigned long)tf, (unsigned long)scp);
306 #endif
307
308 if (!PSL_USEROK_P(lregs->lmsr))
309 return (EINVAL);
310
311 for (i = 0; i < 32; i++)
312 tf->tf_fixreg[i] = lregs->lgpr[i];
313 tf->tf_lr = lregs->llink;
314 tf->tf_cr = lregs->lccr;
315 tf->tf_xer = lregs->lxer;
316 tf->tf_ctr = lregs->lctr;
317 tf->tf_srr0 = lregs->lnip;
318 tf->tf_srr1 = lregs->lmsr;
319
320 /*
321 * Make sure the fpu state is discarded
322 */
323 #ifdef PPC_HAVE_FPU
324 fpu_discard();
325 #endif
326
327 memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
328 sizeof(curpcb->pcb_fpu.fpreg));
329
330 fpu_mark_used(curlwp);
331
332 mutex_enter(p->p_lock);
333
334 /*
335 * Restore signal stack.
336 *
337 * XXX cannot find the onstack information in Linux sig context.
338 * Is signal stack really supported on Linux?
339 *
340 * It seems to be supported in libc6...
341 */
342 /* if (sc.sc_onstack & SS_ONSTACK)
343 l->l_sigstk.ss_flags |= SS_ONSTACK;
344 else */
345 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
346
347 /*
348 * Grab the signal mask
349 */
350 linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
351 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
352
353 mutex_exit(p->p_lock);
354
355 return (EJUSTRETURN);
356 }
357
358
359 /*
360 * The following needs code review for potential security issues
361 */
362 int
linux_sys_sigreturn(struct lwp * l,const struct linux_sys_sigreturn_args * uap,register_t * retval)363 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
364 {
365 /* {
366 syscallarg(struct linux_sigcontext *) scp;
367 } */
368 struct proc *p = l->l_proc;
369 struct linux_sigcontext *scp, context;
370 struct linux_sigregs sregs;
371 struct linux_pt_regs *lregs;
372 struct trapframe *tf;
373 sigset_t mask;
374 int i;
375
376 /*
377 * The trampoline code hands us the context.
378 * It is unsafe to keep track of it ourselves, in the event that a
379 * program jumps out of a signal handler.
380 */
381 scp = SCARG(uap, scp);
382
383 /*
384 * Get the context from user stack
385 */
386 if (copyin(scp, &context, sizeof(*scp)))
387 return (EFAULT);
388
389 /*
390 * Restore register context.
391 */
392 if (copyin((void *)context.lregs, &sregs, sizeof(sregs)))
393 return (EFAULT);
394 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
395
396 tf = trapframe(l);
397 #ifdef DEBUG_LINUX
398 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
399 (unsigned long)tf, (unsigned long)scp);
400 #endif
401
402 if (!PSL_USEROK_P(lregs->lmsr))
403 return (EINVAL);
404
405 for (i = 0; i < 32; i++)
406 tf->tf_fixreg[i] = lregs->lgpr[i];
407 tf->tf_lr = lregs->llink;
408 tf->tf_cr = lregs->lccr;
409 tf->tf_xer = lregs->lxer;
410 tf->tf_ctr = lregs->lctr;
411 tf->tf_srr0 = lregs->lnip;
412 tf->tf_srr1 = lregs->lmsr;
413
414 /*
415 * Make sure the fpu state is discarded
416 */
417 #ifdef PPC_HAVE_FPU
418 fpu_discard();
419 #endif
420
421 memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
422 sizeof(curpcb->pcb_fpu.fpreg));
423
424 fpu_mark_used(curlwp);
425
426 mutex_enter(p->p_lock);
427
428 /*
429 * Restore signal stack.
430 *
431 * XXX cannot find the onstack information in Linux sig context.
432 * Is signal stack really supported on Linux?
433 */
434 #if 0
435 if (sc.sc_onstack & SS_ONSTACK)
436 l->l_sigstk.ss_flags |= SS_ONSTACK;
437 else
438 #endif
439 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
440
441 /* Restore signal mask. */
442 linux_old_extra_to_native_sigset(&mask, &context.lmask,
443 &context._unused[3]);
444 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
445
446 mutex_exit(p->p_lock);
447
448 return (EJUSTRETURN);
449 }
450
451 /*
452 * major device numbers remapping
453 */
454 dev_t
linux_fakedev(dev_t dev,int raw)455 linux_fakedev(dev_t dev, int raw)
456 {
457 /* XXX write me */
458 return dev;
459 }
460
461 /*
462 * We come here in a last attempt to satisfy a Linux ioctl() call
463 */
464 int
linux_machdepioctl(struct lwp * l,const struct linux_sys_ioctl_args * uap,register_t * retval)465 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
466 {
467 /* {
468 syscallarg(int) fd;
469 syscallarg(u_long) com;
470 syscallarg(void *) data;
471 } */
472 struct sys_ioctl_args bia;
473 u_long com;
474
475 SCARG(&bia, fd) = SCARG(uap, fd);
476 SCARG(&bia, data) = SCARG(uap, data);
477 com = SCARG(uap, com);
478
479 switch (com) {
480 default:
481 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
482 return EINVAL;
483 }
484 SCARG(&bia, com) = com;
485 /* XXX NJWLWP */
486 return sys_ioctl(curlwp, &bia, retval);
487 }
488
489 /*
490 * wrapper linux_sys_new_uname() -> linux_sys_uname()
491 */
492 int
linux_sys_new_uname(struct lwp * l,const struct linux_sys_new_uname_args * uap,register_t * retval)493 linux_sys_new_uname(struct lwp *l, const struct linux_sys_new_uname_args *uap, register_t *retval)
494 {
495 return linux_sys_uname(l, (const void *)uap, retval);
496 }
497
498 /*
499 * wrapper linux_sys_new_select() -> linux_sys_select()
500 */
501 int
linux_sys_new_select(struct lwp * l,const struct linux_sys_new_select_args * uap,register_t * retval)502 linux_sys_new_select(struct lwp *l, const struct linux_sys_new_select_args *uap, register_t *retval)
503 {
504 return linux_sys_select(l, (const void *)uap, retval);
505 }
506
507 int
linux_usertrap(struct lwp * l,vaddr_t trapaddr,void * arg)508 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
509 {
510 return 0;
511 }
512