linux_machdep.c revision 1.23 1 /* $NetBSD: linux_machdep.c,v 1.23 2003/09/25 18:42:18 matt 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.23 2003/09/25 18:42:18 matt 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(ksi, mask) /* XXX Check me */
122 ksiginfo_t *ksi;
123 sigset_t *mask;
124 #else
125 void
126 linux_sendsig(sig, mask, code) /* XXX Check me */
127 int sig;
128 sigset_t *mask;
129 u_long code;
130 #endif
131 {
132 #ifdef __HAVE_SIGINFO
133 const int sig = ksi->_signo;
134 #endif
135 struct lwp *l = curlwp;
136 struct proc *p = l->l_proc;
137 struct trapframe *tf;
138 sig_t catcher = SIGACTION(p, sig).sa_handler;
139 struct linux_sigregs frame;
140 struct linux_pt_regs linux_regs;
141 struct linux_sigcontext sc;
142 register_t fp;
143 int onstack;
144 int i;
145
146 tf = trapframe(l);
147
148 /*
149 * Do we need to jump onto the signal stack?
150 */
151 onstack =
152 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
153 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
154
155 /*
156 * Signal stack is broken (see at the end of linux_sigreturn), so we do
157 * not use it yet. XXX fix this.
158 */
159 onstack=0;
160
161 /*
162 * Allocate space for the signal handler context.
163 */
164 if (onstack) {
165 fp = (register_t)
166 ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
167 p->p_sigctx.ps_sigstk.ss_size);
168 } else {
169 fp = tf->fixreg[1];
170 }
171 #ifdef DEBUG_LINUX
172 printf("fp at start of linux_sendsig = %x\n", fp);
173 #endif
174 fp -= sizeof(struct linux_sigregs);
175 fp &= ~0xf;
176
177 /*
178 * Prepare a sigcontext for later.
179 */
180 memset(&sc, 0, sizeof sc);
181 sc.lsignal = (int)native_to_linux_signo[sig];
182 sc.lhandler = (unsigned long)catcher;
183 native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
184 sc.lregs = (struct linux_pt_regs*)fp;
185
186 /*
187 * Setup the signal stack frame as Linux does it in
188 * arch/ppc/kernel/signal.c:setup_frame()
189 *
190 * Save register context.
191 */
192 for (i = 0; i < 32; i++)
193 linux_regs.lgpr[i] = tf->fixreg[i];
194 linux_regs.lnip = tf->srr0;
195 linux_regs.lmsr = tf->srr1;
196 linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
197 linux_regs.lctr = tf->ctr;
198 linux_regs.llink = tf->lr;
199 linux_regs.lxer = tf->xer;
200 linux_regs.lccr = tf->cr;
201 linux_regs.lmq = 0; /* Unused, 601 only */
202 linux_regs.ltrap = tf->exc;
203 linux_regs.ldar = tf->dar;
204 linux_regs.ldsisr = tf->dsisr;
205 linux_regs.lresult = 0;
206
207 memset(&frame, 0, sizeof(frame));
208 memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
209
210 save_fpu_lwp(curlwp);
211 memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpr, sizeof(frame.lfp_regs));
212
213 /*
214 * Copy Linux's signal trampoline on the user stack It should not
215 * be used, but Linux binaries might expect it to be there.
216 */
217 frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
218 frame.ltramp[1] = 0x44000002; /* sc */
219
220 /*
221 * Move it to the user stack
222 * There is a little trick here, about the LINUX_ABIGAP: the
223 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
224 * binaries. But the Linux kernel seems to do without it, and it
225 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
226 */
227 if (copyout(&frame, (caddr_t)fp, sizeof (frame) - LINUX_ABIGAP) != 0) {
228 /*
229 * Process has trashed its stack; give it an illegal
230 * instruction to halt it in its tracks.
231 */
232 sigexit(l, SIGILL);
233 /* NOTREACHED */
234 }
235
236 /*
237 * Add a sigcontext on the stack
238 */
239 fp -= sizeof(struct linux_sigcontext);
240 if (copyout(&sc, (caddr_t)fp, sizeof (struct linux_sigcontext)) != 0) {
241 /*
242 * Process has trashed its stack; give it an illegal
243 * instruction to halt it in its tracks.
244 */
245 sigexit(l, SIGILL);
246 /* NOTREACHED */
247 }
248
249 /*
250 * Set the registers according to how the Linux process expects them.
251 * "Mind the gap" Linux expects a gap here.
252 */
253 tf->fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
254 tf->lr = (int)catcher;
255 tf->fixreg[3] = (int)native_to_linux_signo[sig];
256 tf->fixreg[4] = fp;
257 tf->srr0 = (int)p->p_sigctx.ps_sigcode;
258
259 #ifdef DEBUG_LINUX
260 printf("fp at end of linux_sendsig = %x\n", fp);
261 #endif
262 /*
263 * Remember that we're now on the signal stack.
264 */
265 if (onstack)
266 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
267 #ifdef DEBUG_LINUX
268 printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
269 #endif
270 }
271
272 /*
273 * System call to cleanup state after a signal
274 * has been taken. Reset signal mask and
275 * stack state from context left by sendsig (above).
276 * Return to previous pc and psl as specified by
277 * context left by sendsig. Check carefully to
278 * make sure that the user has not modified the
279 * psl to gain improper privileges or to cause
280 * a machine fault.
281 *
282 * XXX not tested
283 */
284 int
285 linux_sys_rt_sigreturn(l, v, retval)
286 struct lwp *l;
287 void *v;
288 register_t *retval;
289 {
290 struct linux_sys_rt_sigreturn_args /* {
291 syscallarg(struct linux_rt_sigframe *) sfp;
292 } */ *uap = v;
293 struct proc *p = l->l_proc;
294 struct linux_rt_sigframe *scp, sigframe;
295 struct linux_sigregs sregs;
296 struct linux_pt_regs *lregs;
297 struct trapframe *tf;
298 sigset_t mask;
299 int i;
300
301 /*
302 * The trampoline code hands us the context.
303 * It is unsafe to keep track of it ourselves, in the event that a
304 * program jumps out of a signal handler.
305 */
306 scp = SCARG(uap, sfp);
307
308 /*
309 * Get the context from user stack
310 */
311 if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
312 return (EFAULT);
313
314 /*
315 * Make sure, fpu is sync'ed
316 */
317 save_fpu_lwp(curlwp);
318
319 /*
320 * Restore register context.
321 */
322 if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
323 &sregs, sizeof(sregs)))
324 return (EFAULT);
325 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
326
327 tf = trapframe(l);
328 #ifdef DEBUG_LINUX
329 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
330 (unsigned long)tf, (unsigned long)scp);
331 #endif
332
333 if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
334 return (EINVAL);
335
336 for (i = 0; i < 32; i++)
337 tf->fixreg[i] = lregs->lgpr[i];
338 tf->lr = lregs->llink;
339 tf->cr = lregs->lccr;
340 tf->xer = lregs->lxer;
341 tf->ctr = lregs->lctr;
342 tf->srr0 = lregs->lnip;
343 tf->srr1 = lregs->lmsr;
344
345 memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
346 sizeof(curpcb->pcb_fpu.fpr));
347
348 /*
349 * Restore signal stack.
350 *
351 * XXX cannot find the onstack information in Linux sig context.
352 * Is signal stack really supported on Linux?
353 *
354 * It seems to be supported in libc6...
355 */
356 /* if (sc.sc_onstack & SS_ONSTACK)
357 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
358 else */
359 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
360
361 /*
362 * Grab the signal mask
363 */
364 linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
365 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
366
367 return (EJUSTRETURN);
368 }
369
370
371 /*
372 * The following needs code review for potential security issues
373 */
374 int
375 linux_sys_sigreturn(l, v, retval)
376 struct lwp *l;
377 void *v;
378 register_t *retval;
379 {
380 struct linux_sys_sigreturn_args /* {
381 syscallarg(struct linux_sigcontext *) scp;
382 } */ *uap = v;
383 struct proc *p = l->l_proc;
384 struct linux_sigcontext *scp, context;
385 struct linux_sigregs sregs;
386 struct linux_pt_regs *lregs;
387 struct trapframe *tf;
388 sigset_t mask;
389 int i;
390
391 /*
392 * The trampoline code hands us the context.
393 * It is unsafe to keep track of it ourselves, in the event that a
394 * program jumps out of a signal handler.
395 */
396 scp = SCARG(uap, scp);
397
398 /*
399 * Get the context from user stack
400 */
401 if (copyin(scp, &context, sizeof(*scp)))
402 return (EFAULT);
403
404 /*
405 * Make sure, fpu is in sync
406 */
407 save_fpu_lwp(curlwp);
408
409 /*
410 * Restore register context.
411 */
412 if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
413 return (EFAULT);
414 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
415
416 tf = trapframe(l);
417 #ifdef DEBUG_LINUX
418 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
419 (unsigned long)tf, (unsigned long)scp);
420 #endif
421
422 if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
423 return (EINVAL);
424
425 for (i = 0; i < 32; i++)
426 tf->fixreg[i] = lregs->lgpr[i];
427 tf->lr = lregs->llink;
428 tf->cr = lregs->lccr;
429 tf->xer = lregs->lxer;
430 tf->ctr = lregs->lctr;
431 tf->srr0 = lregs->lnip;
432 tf->srr1 = lregs->lmsr;
433
434 memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
435 sizeof(curpcb->pcb_fpu.fpr));
436
437 /*
438 * Restore signal stack.
439 *
440 * XXX cannot find the onstack information in Linux sig context.
441 * Is signal stack really supported on Linux?
442 */
443 #if 0
444 if (sc.sc_onstack & SS_ONSTACK)
445 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
446 else
447 #endif
448 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
449
450 /* Restore signal mask. */
451 linux_old_extra_to_native_sigset(&mask, &context.lmask,
452 &context._unused[3]);
453 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
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(p, v, retval)
494 struct proc *p;
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