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