linux_machdep.c revision 1.26 1 /* $NetBSD: linux_machdep.c,v 1.26 2003/10/08 00:39:57 thorpej 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.26 2003/10/08 00:39:57 thorpej 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 #ifdef __HAVE_SIGINFO
120 void
121 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
122 #else
123 void
124 linux_sendsig(sig, mask, code) /* XXX Check me */
125 int sig;
126 const sigset_t *mask;
127 u_long code;
128 #endif
129 {
130 #ifdef __HAVE_SIGINFO
131 const int sig = ksi->ksi_signo;
132 #endif
133 struct lwp *l = curlwp;
134 struct proc *p = l->l_proc;
135 struct trapframe *tf;
136 sig_t catcher = SIGACTION(p, sig).sa_handler;
137 struct linux_sigregs frame;
138 struct linux_pt_regs linux_regs;
139 struct linux_sigcontext sc;
140 register_t fp;
141 int onstack;
142 int i;
143
144 tf = trapframe(l);
145
146 /*
147 * Do we need to jump onto the signal stack?
148 */
149 onstack =
150 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
151 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
152
153 /*
154 * Signal stack is broken (see at the end of linux_sigreturn), so we do
155 * not use it yet. XXX fix this.
156 */
157 onstack=0;
158
159 /*
160 * Allocate space for the signal handler context.
161 */
162 if (onstack) {
163 fp = (register_t)
164 ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
165 p->p_sigctx.ps_sigstk.ss_size);
166 } else {
167 fp = tf->fixreg[1];
168 }
169 #ifdef DEBUG_LINUX
170 printf("fp at start of linux_sendsig = %x\n", fp);
171 #endif
172 fp -= sizeof(struct linux_sigregs);
173 fp &= ~0xf;
174
175 /*
176 * Prepare a sigcontext for later.
177 */
178 memset(&sc, 0, sizeof sc);
179 sc.lsignal = (int)native_to_linux_signo[sig];
180 sc.lhandler = (unsigned long)catcher;
181 native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
182 sc.lregs = (struct linux_pt_regs*)fp;
183
184 /*
185 * Setup the signal stack frame as Linux does it in
186 * arch/ppc/kernel/signal.c:setup_frame()
187 *
188 * Save register context.
189 */
190 for (i = 0; i < 32; i++)
191 linux_regs.lgpr[i] = tf->fixreg[i];
192 linux_regs.lnip = tf->srr0;
193 linux_regs.lmsr = tf->srr1;
194 linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
195 linux_regs.lctr = tf->ctr;
196 linux_regs.llink = tf->lr;
197 linux_regs.lxer = tf->xer;
198 linux_regs.lccr = tf->cr;
199 linux_regs.lmq = 0; /* Unused, 601 only */
200 linux_regs.ltrap = tf->exc;
201 linux_regs.ldar = tf->dar;
202 linux_regs.ldsisr = tf->dsisr;
203 linux_regs.lresult = 0;
204
205 memset(&frame, 0, sizeof(frame));
206 memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
207
208 save_fpu_lwp(curlwp);
209 memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpr, sizeof(frame.lfp_regs));
210
211 /*
212 * Copy Linux's signal trampoline on the user stack It should not
213 * be used, but Linux binaries might expect it to be there.
214 */
215 frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
216 frame.ltramp[1] = 0x44000002; /* sc */
217
218 /*
219 * Move it to the user stack
220 * There is a little trick here, about the LINUX_ABIGAP: the
221 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
222 * binaries. But the Linux kernel seems to do without it, and it
223 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
224 */
225 if (copyout(&frame, (caddr_t)fp, sizeof (frame) - LINUX_ABIGAP) != 0) {
226 /*
227 * Process has trashed its stack; give it an illegal
228 * instruction to halt it in its tracks.
229 */
230 sigexit(l, SIGILL);
231 /* NOTREACHED */
232 }
233
234 /*
235 * Add a sigcontext on the stack
236 */
237 fp -= sizeof(struct linux_sigcontext);
238 if (copyout(&sc, (caddr_t)fp, sizeof (struct linux_sigcontext)) != 0) {
239 /*
240 * Process has trashed its stack; give it an illegal
241 * instruction to halt it in its tracks.
242 */
243 sigexit(l, SIGILL);
244 /* NOTREACHED */
245 }
246
247 /*
248 * Set the registers according to how the Linux process expects them.
249 * "Mind the gap" Linux expects a gap here.
250 */
251 tf->fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
252 tf->lr = (int)catcher;
253 tf->fixreg[3] = (int)native_to_linux_signo[sig];
254 tf->fixreg[4] = fp;
255 tf->srr0 = (int)p->p_sigctx.ps_sigcode;
256
257 #ifdef DEBUG_LINUX
258 printf("fp at end of linux_sendsig = %x\n", fp);
259 #endif
260 /*
261 * Remember that we're now on the signal stack.
262 */
263 if (onstack)
264 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
265 #ifdef DEBUG_LINUX
266 printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
267 #endif
268 }
269
270 /*
271 * System call to cleanup state after a signal
272 * has been taken. Reset signal mask and
273 * stack state from context left by sendsig (above).
274 * Return to previous pc and psl as specified by
275 * context left by sendsig. Check carefully to
276 * make sure that the user has not modified the
277 * psl to gain improper privileges or to cause
278 * a machine fault.
279 *
280 * XXX not tested
281 */
282 int
283 linux_sys_rt_sigreturn(l, v, retval)
284 struct lwp *l;
285 void *v;
286 register_t *retval;
287 {
288 struct linux_sys_rt_sigreturn_args /* {
289 syscallarg(struct linux_rt_sigframe *) sfp;
290 } */ *uap = v;
291 struct proc *p = l->l_proc;
292 struct linux_rt_sigframe *scp, sigframe;
293 struct linux_sigregs sregs;
294 struct linux_pt_regs *lregs;
295 struct trapframe *tf;
296 sigset_t mask;
297 int i;
298
299 /*
300 * The trampoline code hands us the context.
301 * It is unsafe to keep track of it ourselves, in the event that a
302 * program jumps out of a signal handler.
303 */
304 scp = SCARG(uap, sfp);
305
306 /*
307 * Get the context from user stack
308 */
309 if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
310 return (EFAULT);
311
312 /*
313 * Make sure, fpu is sync'ed
314 */
315 save_fpu_lwp(curlwp);
316
317 /*
318 * Restore register context.
319 */
320 if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
321 &sregs, sizeof(sregs)))
322 return (EFAULT);
323 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
324
325 tf = trapframe(l);
326 #ifdef DEBUG_LINUX
327 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
328 (unsigned long)tf, (unsigned long)scp);
329 #endif
330
331 if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
332 return (EINVAL);
333
334 for (i = 0; i < 32; i++)
335 tf->fixreg[i] = lregs->lgpr[i];
336 tf->lr = lregs->llink;
337 tf->cr = lregs->lccr;
338 tf->xer = lregs->lxer;
339 tf->ctr = lregs->lctr;
340 tf->srr0 = lregs->lnip;
341 tf->srr1 = lregs->lmsr;
342
343 memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
344 sizeof(curpcb->pcb_fpu.fpr));
345
346 /*
347 * Restore signal stack.
348 *
349 * XXX cannot find the onstack information in Linux sig context.
350 * Is signal stack really supported on Linux?
351 *
352 * It seems to be supported in libc6...
353 */
354 /* if (sc.sc_onstack & SS_ONSTACK)
355 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
356 else */
357 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
358
359 /*
360 * Grab the signal mask
361 */
362 linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
363 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
364
365 return (EJUSTRETURN);
366 }
367
368
369 /*
370 * The following needs code review for potential security issues
371 */
372 int
373 linux_sys_sigreturn(l, v, retval)
374 struct lwp *l;
375 void *v;
376 register_t *retval;
377 {
378 struct linux_sys_sigreturn_args /* {
379 syscallarg(struct linux_sigcontext *) scp;
380 } */ *uap = v;
381 struct proc *p = l->l_proc;
382 struct linux_sigcontext *scp, context;
383 struct linux_sigregs sregs;
384 struct linux_pt_regs *lregs;
385 struct trapframe *tf;
386 sigset_t mask;
387 int i;
388
389 /*
390 * The trampoline code hands us the context.
391 * It is unsafe to keep track of it ourselves, in the event that a
392 * program jumps out of a signal handler.
393 */
394 scp = SCARG(uap, scp);
395
396 /*
397 * Get the context from user stack
398 */
399 if (copyin(scp, &context, sizeof(*scp)))
400 return (EFAULT);
401
402 /*
403 * Make sure, fpu is in sync
404 */
405 save_fpu_lwp(curlwp);
406
407 /*
408 * Restore register context.
409 */
410 if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
411 return (EFAULT);
412 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
413
414 tf = trapframe(l);
415 #ifdef DEBUG_LINUX
416 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
417 (unsigned long)tf, (unsigned long)scp);
418 #endif
419
420 if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
421 return (EINVAL);
422
423 for (i = 0; i < 32; i++)
424 tf->fixreg[i] = lregs->lgpr[i];
425 tf->lr = lregs->llink;
426 tf->cr = lregs->lccr;
427 tf->xer = lregs->lxer;
428 tf->ctr = lregs->lctr;
429 tf->srr0 = lregs->lnip;
430 tf->srr1 = lregs->lmsr;
431
432 memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
433 sizeof(curpcb->pcb_fpu.fpr));
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 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
444 else
445 #endif
446 p->p_sigctx.ps_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(p, SIG_SETMASK, &mask, 0);
452
453 return (EJUSTRETURN);
454 }
455
456
457 #if 0
458 int
459 linux_sys_modify_ldt(p, v, retval)
460 struct proc *p;
461 void *v;
462 register_t *retval;
463 {
464 /*
465 * This syscall is not implemented in Linux/PowerPC: we should not
466 * be here
467 */
468 #ifdef DEBUG_LINUX
469 printf("linux_sys_modify_ldt: should not be here.\n");
470 #endif
471 return 0;
472 }
473 #endif
474
475 /*
476 * major device numbers remapping
477 */
478 dev_t
479 linux_fakedev(dev, raw)
480 dev_t dev;
481 int raw;
482 {
483 /* XXX write me */
484 return dev;
485 }
486
487 /*
488 * We come here in a last attempt to satisfy a Linux ioctl() call
489 */
490 int
491 linux_machdepioctl(p, v, retval)
492 struct proc *p;
493 void *v;
494 register_t *retval;
495 {
496 struct linux_sys_ioctl_args /* {
497 syscallarg(int) fd;
498 syscallarg(u_long) com;
499 syscallarg(caddr_t) data;
500 } */ *uap = v;
501 struct sys_ioctl_args bia;
502 u_long com;
503
504 SCARG(&bia, fd) = SCARG(uap, fd);
505 SCARG(&bia, data) = SCARG(uap, data);
506 com = SCARG(uap, com);
507
508 switch (com) {
509 default:
510 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
511 return EINVAL;
512 }
513 SCARG(&bia, com) = com;
514 /* XXX NJWLWP */
515 return sys_ioctl(curlwp, &bia, retval);
516 }
517 #if 0
518 /*
519 * Set I/O permissions for a process. Just set the maximum level
520 * right away (ignoring the argument), otherwise we would have
521 * to rely on I/O permission maps, which are not implemented.
522 */
523 int
524 linux_sys_iopl(l, v, retval)
525 struct lwp *l;
526 void *v;
527 register_t *retval;
528 {
529 /*
530 * This syscall is not implemented in Linux/PowerPC: we should not be here
531 */
532 #ifdef DEBUG_LINUX
533 printf("linux_sys_iopl: should not be here.\n");
534 #endif
535 return 0;
536 }
537 #endif
538
539 /*
540 * See above. If a root process tries to set access to an I/O port,
541 * just let it have the whole range.
542 */
543 int
544 linux_sys_ioperm(l, v, retval)
545 struct lwp *l;
546 void *v;
547 register_t *retval;
548 {
549 /*
550 * This syscall is not implemented in Linux/PowerPC: we should not be here
551 */
552 #ifdef DEBUG_LINUX
553 printf("linux_sys_ioperm: should not be here.\n");
554 #endif
555 return 0;
556 }
557
558 /*
559 * wrapper linux_sys_new_uname() -> linux_sys_uname()
560 */
561 int
562 linux_sys_new_uname(l, v, retval)
563 struct lwp *l;
564 void *v;
565 register_t *retval;
566 {
567 return linux_sys_uname(l, v, retval);
568 }
569
570 /*
571 * wrapper linux_sys_new_select() -> linux_sys_select()
572 */
573 int
574 linux_sys_new_select(l, v, retval)
575 struct lwp *l;
576 void *v;
577 register_t *retval;
578 {
579 return linux_sys_select(l, v, retval);
580 }
581