linux_machdep.c revision 1.39.2.1 1 /* $NetBSD: linux_machdep.c,v 1.39.2.1 2008/05/10 23:48:55 wrstuden 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.39.2.1 2008/05/10 23:48:55 wrstuden 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/user.h>
41 #include <sys/buf.h>
42 #include <sys/reboot.h>
43 #include <sys/conf.h>
44 #include <sys/exec.h>
45 #include <sys/file.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/mbuf.h>
49 #include <sys/msgbuf.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/device.h>
53 #include <sys/sa.h>
54 #include <sys/syscallargs.h>
55 #include <sys/filedesc.h>
56 #include <sys/exec_elf.h>
57 #include <sys/disklabel.h>
58 #include <sys/ioctl.h>
59 #include <miscfs/specfs/specdev.h>
60
61 #include <compat/linux/common/linux_types.h>
62 #include <compat/linux/common/linux_signal.h>
63 #include <compat/linux/common/linux_util.h>
64 #include <compat/linux/common/linux_ioctl.h>
65 #include <compat/linux/common/linux_hdio.h>
66 #include <compat/linux/common/linux_exec.h>
67 #include <compat/linux/common/linux_machdep.h>
68
69 #include <compat/linux/linux_syscallargs.h>
70
71 #include <sys/cpu.h>
72 #include <machine/fpu.h>
73 #include <machine/psl.h>
74 #include <machine/reg.h>
75 #include <machine/vmparam.h>
76
77 /*
78 * To see whether wscons is configured (for virtual console ioctl calls).
79 */
80 #if defined(_KERNEL_OPT)
81 #include "wsdisplay.h"
82 #endif
83 #if (NWSDISPLAY > 0)
84 #include <dev/wscons/wsconsio.h>
85 #include <dev/wscons/wsdisplay_usl_io.h>
86 #endif
87
88 /*
89 * Set set up registers on exec.
90 * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
91 * entry uses NetBSD's native setregs instead of linux_setregs
92 */
93 void
94 linux_setregs(struct lwp *l, struct exec_package *pack, u_long stack)
95 {
96 setregs(l, pack, stack);
97 }
98
99 /*
100 * Send an interrupt to process.
101 *
102 * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
103 * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
104 *
105 * XXX Does not work well yet with RT signals
106 *
107 */
108
109 void
110 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
111 {
112 const int sig = ksi->ksi_signo;
113 struct lwp *l = curlwp;
114 struct proc *p = l->l_proc;
115 struct trapframe *tf;
116 sig_t catcher = SIGACTION(p, sig).sa_handler;
117 struct linux_sigregs frame;
118 struct linux_pt_regs linux_regs;
119 struct linux_sigcontext sc;
120 register_t fp;
121 int onstack, error;
122 int i;
123
124 tf = trapframe(l);
125
126 /*
127 * Do we need to jump onto the signal stack?
128 */
129 onstack =
130 (l->l_sigstk->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
131 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
132
133 /*
134 * Signal stack is broken (see at the end of linux_sigreturn), so we do
135 * not use it yet. XXX fix this.
136 */
137 onstack=0;
138
139 /*
140 * Allocate space for the signal handler context.
141 */
142 if (onstack) {
143 fp = (register_t)
144 ((char *)l->l_sigstk->ss_sp +
145 l->l_sigstk->ss_size);
146 } else {
147 fp = tf->fixreg[1];
148 }
149 #ifdef DEBUG_LINUX
150 printf("fp at start of linux_sendsig = %x\n", fp);
151 #endif
152 fp -= sizeof(struct linux_sigregs);
153 fp &= ~0xf;
154
155 /*
156 * Prepare a sigcontext for later.
157 */
158 memset(&sc, 0, sizeof sc);
159 sc.lsignal = (int)native_to_linux_signo[sig];
160 sc.lhandler = (unsigned long)catcher;
161 native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
162 sc.lregs = (struct linux_pt_regs*)fp;
163
164 /*
165 * Setup the signal stack frame as Linux does it in
166 * arch/ppc/kernel/signal.c:setup_frame()
167 *
168 * Save register context.
169 */
170 for (i = 0; i < 32; i++)
171 linux_regs.lgpr[i] = tf->fixreg[i];
172 linux_regs.lnip = tf->srr0;
173 linux_regs.lmsr = tf->srr1 & PSL_USERSRR1;
174 linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
175 linux_regs.lctr = tf->ctr;
176 linux_regs.llink = tf->lr;
177 linux_regs.lxer = tf->xer;
178 linux_regs.lccr = tf->cr;
179 linux_regs.lmq = 0; /* Unused, 601 only */
180 linux_regs.ltrap = tf->exc;
181 linux_regs.ldar = tf->dar;
182 linux_regs.ldsisr = tf->dsisr;
183 linux_regs.lresult = 0;
184
185 memset(&frame, 0, sizeof(frame));
186 memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
187
188 save_fpu_lwp(curlwp, FPU_SAVE);
189 memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpreg, sizeof(frame.lfp_regs));
190
191 /*
192 * Copy Linux's signal trampoline on the user stack It should not
193 * be used, but Linux binaries might expect it to be there.
194 */
195 frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
196 frame.ltramp[1] = 0x44000002; /* sc */
197
198 /*
199 * Move it to the user stack
200 * There is a little trick here, about the LINUX_ABIGAP: the
201 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
202 * binaries. But the Linux kernel seems to do without it, and it
203 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
204 */
205 sendsig_reset(l, sig);
206 mutex_exit(p->p_lock);
207 error = copyout(&frame, (void *)fp, sizeof (frame) - LINUX_ABIGAP);
208
209 if (error != 0) {
210 /*
211 * Process has trashed its stack; give it an illegal
212 * instruction to halt it in its tracks.
213 */
214 mutex_enter(p->p_lock);
215 sigexit(l, SIGILL);
216 /* NOTREACHED */
217 }
218
219 /*
220 * Add a sigcontext on the stack
221 */
222 fp -= sizeof(struct linux_sigcontext);
223 error = copyout(&sc, (void *)fp, sizeof (struct linux_sigcontext));
224 mutex_enter(p->p_lock);
225
226 if (error != 0) {
227 /*
228 * Process has trashed its stack; give it an illegal
229 * instruction to halt it in its tracks.
230 */
231 sigexit(l, SIGILL);
232 /* NOTREACHED */
233 }
234
235 /*
236 * Set the registers according to how the Linux process expects them.
237 * "Mind the gap" Linux expects a gap here.
238 */
239 tf->fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
240 tf->lr = (int)catcher;
241 tf->fixreg[3] = (int)native_to_linux_signo[sig];
242 tf->fixreg[4] = fp;
243 tf->srr0 = (int)p->p_sigctx.ps_sigcode;
244
245 #ifdef DEBUG_LINUX
246 printf("fp at end of linux_sendsig = %x\n", fp);
247 #endif
248 /*
249 * Remember that we're now on the signal stack.
250 */
251 if (onstack)
252 l->l_sigstk->ss_flags |= SS_ONSTACK;
253 #ifdef DEBUG_LINUX
254 printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
255 #endif
256 }
257
258 /*
259 * System call to cleanup state after a signal
260 * has been taken. Reset signal mask and
261 * stack state from context left by sendsig (above).
262 * Return to previous pc and psl as specified by
263 * context left by sendsig. Check carefully to
264 * make sure that the user has not modified the
265 * psl to gain improper privileges or to cause
266 * a machine fault.
267 *
268 * XXX not tested
269 */
270 int
271 linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval)
272 {
273 /* {
274 syscallarg(struct linux_rt_sigframe *) sfp;
275 } */
276 struct proc *p = l->l_proc;
277 struct linux_rt_sigframe *scp, sigframe;
278 struct linux_sigregs sregs;
279 struct linux_pt_regs *lregs;
280 struct trapframe *tf;
281 sigset_t mask;
282 int i;
283
284 /*
285 * The trampoline code hands us the context.
286 * It is unsafe to keep track of it ourselves, in the event that a
287 * program jumps out of a signal handler.
288 */
289 scp = SCARG(uap, sfp);
290
291 /*
292 * Get the context from user stack
293 */
294 if (copyin((void *)scp, &sigframe, sizeof(*scp)))
295 return (EFAULT);
296
297 /*
298 * Restore register context.
299 */
300 if (copyin((void *)sigframe.luc.luc_context.lregs,
301 &sregs, sizeof(sregs)))
302 return (EFAULT);
303 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
304
305 tf = trapframe(l);
306 #ifdef DEBUG_LINUX
307 (unsigned long)tf, (unsigned long)scp);
308 #endif
309
310 if (!PSL_USEROK_P(lregs->lmsr))
311 return (EINVAL);
312
313 for (i = 0; i < 32; i++)
314 tf->fixreg[i] = lregs->lgpr[i];
315 tf->lr = lregs->llink;
316 tf->cr = lregs->lccr;
317 tf->xer = lregs->lxer;
318 tf->ctr = lregs->lctr;
319 tf->srr0 = lregs->lnip;
320 tf->srr1 = lregs->lmsr;
321
322 /*
323 * Make sure the fpu state is discarded
324 */
325 save_fpu_lwp(curlwp, FPU_DISCARD);
326
327 memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
328 sizeof(curpcb->pcb_fpu.fpreg));
329
330 mutex_enter(p->p_lock);
331
332 /*
333 * Restore signal stack.
334 *
335 * XXX cannot find the onstack information in Linux sig context.
336 * Is signal stack really supported on Linux?
337 *
338 * It seems to be supported in libc6...
339 */
340 /* if (sc.sc_onstack & SS_ONSTACK)
341 l->l_sigstk->ss_flags |= SS_ONSTACK;
342 else */
343 l->l_sigstk->ss_flags &= ~SS_ONSTACK;
344
345 /*
346 * Grab the signal mask
347 */
348 linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
349 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
350
351 mutex_exit(p->p_lock);
352
353 return (EJUSTRETURN);
354 }
355
356
357 /*
358 * The following needs code review for potential security issues
359 */
360 int
361 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
362 {
363 /* {
364 syscallarg(struct linux_sigcontext *) scp;
365 } */
366 struct proc *p = l->l_proc;
367 struct linux_sigcontext *scp, context;
368 struct linux_sigregs sregs;
369 struct linux_pt_regs *lregs;
370 struct trapframe *tf;
371 sigset_t mask;
372 int i;
373
374 /*
375 * The trampoline code hands us the context.
376 * It is unsafe to keep track of it ourselves, in the event that a
377 * program jumps out of a signal handler.
378 */
379 scp = SCARG(uap, scp);
380
381 /*
382 * Get the context from user stack
383 */
384 if (copyin(scp, &context, sizeof(*scp)))
385 return (EFAULT);
386
387 /*
388 * Restore register context.
389 */
390 if (copyin((void *)context.lregs, &sregs, sizeof(sregs)))
391 return (EFAULT);
392 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
393
394 tf = trapframe(l);
395 #ifdef DEBUG_LINUX
396 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
397 (unsigned long)tf, (unsigned long)scp);
398 #endif
399
400 if (!PSL_USEROK_P(lregs->lmsr))
401 return (EINVAL);
402
403 for (i = 0; i < 32; i++)
404 tf->fixreg[i] = lregs->lgpr[i];
405 tf->lr = lregs->llink;
406 tf->cr = lregs->lccr;
407 tf->xer = lregs->lxer;
408 tf->ctr = lregs->lctr;
409 tf->srr0 = lregs->lnip;
410 tf->srr1 = lregs->lmsr;
411
412 /*
413 * Make sure the fpu state is discarded
414 */
415 save_fpu_lwp(curlwp, FPU_DISCARD);
416
417 memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
418 sizeof(curpcb->pcb_fpu.fpreg));
419
420 mutex_enter(p->p_lock);
421
422 /*
423 * Restore signal stack.
424 *
425 * XXX cannot find the onstack information in Linux sig context.
426 * Is signal stack really supported on Linux?
427 */
428 #if 0
429 if (sc.sc_onstack & SS_ONSTACK)
430 l->l_sigstk->ss_flags |= SS_ONSTACK;
431 else
432 #endif
433 l->l_sigstk->ss_flags &= ~SS_ONSTACK;
434
435 /* Restore signal mask. */
436 linux_old_extra_to_native_sigset(&mask, &context.lmask,
437 &context._unused[3]);
438 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
439
440 mutex_exit(p->p_lock);
441
442 return (EJUSTRETURN);
443 }
444
445
446 #if 0
447 int
448 linux_sys_modify_ldt(struct proc *p, void *v, register_t *retval)
449 {
450 /*
451 * This syscall is not implemented in Linux/PowerPC: we should not
452 * be here
453 */
454 #ifdef DEBUG_LINUX
455 printf("linux_sys_modify_ldt: should not be here.\n");
456 #endif
457 return 0;
458 }
459 #endif
460
461 /*
462 * major device numbers remapping
463 */
464 dev_t
465 linux_fakedev(dev_t dev, int raw)
466 {
467 /* XXX write me */
468 return dev;
469 }
470
471 /*
472 * We come here in a last attempt to satisfy a Linux ioctl() call
473 */
474 int
475 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
476 {
477 /* {
478 syscallarg(int) fd;
479 syscallarg(u_long) com;
480 syscallarg(void *) data;
481 } */
482 struct sys_ioctl_args bia;
483 u_long com;
484
485 SCARG(&bia, fd) = SCARG(uap, fd);
486 SCARG(&bia, data) = SCARG(uap, data);
487 com = SCARG(uap, com);
488
489 switch (com) {
490 default:
491 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
492 return EINVAL;
493 }
494 SCARG(&bia, com) = com;
495 /* XXX NJWLWP */
496 return sys_ioctl(curlwp, &bia, retval);
497 }
498 #if 0
499 /*
500 * Set I/O permissions for a process. Just set the maximum level
501 * right away (ignoring the argument), otherwise we would have
502 * to rely on I/O permission maps, which are not implemented.
503 */
504 int
505 linux_sys_iopl(struct lwp *l, const void *v, register_t *retval)
506 {
507 /*
508 * This syscall is not implemented in Linux/PowerPC: we should not be here
509 */
510 #ifdef DEBUG_LINUX
511 printf("linux_sys_iopl: should not be here.\n");
512 #endif
513 return 0;
514 }
515 #endif
516
517 /*
518 * See above. If a root process tries to set access to an I/O port,
519 * just let it have the whole range.
520 */
521 int
522 linux_sys_ioperm(struct lwp *l, const struct linux_sys_ioperm_args *uap, register_t *retval)
523 {
524 /*
525 * This syscall is not implemented in Linux/PowerPC: we should not be here
526 */
527 #ifdef DEBUG_LINUX
528 printf("linux_sys_ioperm: should not be here.\n");
529 #endif
530 return 0;
531 }
532
533 /*
534 * wrapper linux_sys_new_uname() -> linux_sys_uname()
535 */
536 int
537 linux_sys_new_uname(struct lwp *l, const struct linux_sys_new_uname_args *uap, register_t *retval)
538 {
539 return linux_sys_uname(l, (const void *)uap, retval);
540 }
541
542 /*
543 * wrapper linux_sys_new_select() -> linux_sys_select()
544 */
545 int
546 linux_sys_new_select(struct lwp *l, const struct linux_sys_new_select_args *uap, register_t *retval)
547 {
548 return linux_sys_select(l, (const void *)uap, retval);
549 }
550
551 int
552 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
553 {
554 return 0;
555 }
556