linux_machdep.c revision 1.29 1 /* $NetBSD: linux_machdep.c,v 1.29 2005/02/26 23:10:19 perry 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.29 2005/02/26 23:10:19 perry 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;
132 int i;
133
134 tf = trapframe(l);
135
136 /*
137 * Do we need to jump onto the signal stack?
138 */
139 onstack =
140 (p->p_sigctx.ps_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)p->p_sigctx.ps_sigstk.ss_sp +
155 p->p_sigctx.ps_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 if (copyout(&frame, (caddr_t)fp, sizeof (frame) - LINUX_ABIGAP) != 0) {
216 /*
217 * Process has trashed its stack; give it an illegal
218 * instruction to halt it in its tracks.
219 */
220 sigexit(l, SIGILL);
221 /* NOTREACHED */
222 }
223
224 /*
225 * Add a sigcontext on the stack
226 */
227 fp -= sizeof(struct linux_sigcontext);
228 if (copyout(&sc, (caddr_t)fp, sizeof (struct linux_sigcontext)) != 0) {
229 /*
230 * Process has trashed its stack; give it an illegal
231 * instruction to halt it in its tracks.
232 */
233 sigexit(l, SIGILL);
234 /* NOTREACHED */
235 }
236
237 /*
238 * Set the registers according to how the Linux process expects them.
239 * "Mind the gap" Linux expects a gap here.
240 */
241 tf->fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
242 tf->lr = (int)catcher;
243 tf->fixreg[3] = (int)native_to_linux_signo[sig];
244 tf->fixreg[4] = fp;
245 tf->srr0 = (int)p->p_sigctx.ps_sigcode;
246
247 #ifdef DEBUG_LINUX
248 printf("fp at end of linux_sendsig = %x\n", fp);
249 #endif
250 /*
251 * Remember that we're now on the signal stack.
252 */
253 if (onstack)
254 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
255 #ifdef DEBUG_LINUX
256 printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
257 #endif
258 }
259
260 /*
261 * System call to cleanup state after a signal
262 * has been taken. Reset signal mask and
263 * stack state from context left by sendsig (above).
264 * Return to previous pc and psl as specified by
265 * context left by sendsig. Check carefully to
266 * make sure that the user has not modified the
267 * psl to gain improper privileges or to cause
268 * a machine fault.
269 *
270 * XXX not tested
271 */
272 int
273 linux_sys_rt_sigreturn(l, v, retval)
274 struct lwp *l;
275 void *v;
276 register_t *retval;
277 {
278 struct linux_sys_rt_sigreturn_args /* {
279 syscallarg(struct linux_rt_sigframe *) sfp;
280 } */ *uap = v;
281 struct proc *p = l->l_proc;
282 struct linux_rt_sigframe *scp, sigframe;
283 struct linux_sigregs sregs;
284 struct linux_pt_regs *lregs;
285 struct trapframe *tf;
286 sigset_t mask;
287 int i;
288
289 /*
290 * The trampoline code hands us the context.
291 * It is unsafe to keep track of it ourselves, in the event that a
292 * program jumps out of a signal handler.
293 */
294 scp = SCARG(uap, sfp);
295
296 /*
297 * Get the context from user stack
298 */
299 if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
300 return (EFAULT);
301
302 /*
303 * Restore register context.
304 */
305 if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
306 &sregs, sizeof(sregs)))
307 return (EFAULT);
308 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
309
310 tf = trapframe(l);
311 #ifdef DEBUG_LINUX
312 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
313 (unsigned long)tf, (unsigned long)scp);
314 #endif
315
316 if (!PSL_USEROK_P(lregs->lmsr))
317 return (EINVAL);
318
319 for (i = 0; i < 32; i++)
320 tf->fixreg[i] = lregs->lgpr[i];
321 tf->lr = lregs->llink;
322 tf->cr = lregs->lccr;
323 tf->xer = lregs->lxer;
324 tf->ctr = lregs->lctr;
325 tf->srr0 = lregs->lnip;
326 tf->srr1 = lregs->lmsr;
327
328 /*
329 * Make sure the fpu state is discarded
330 */
331 save_fpu_lwp(curlwp, FPU_DISCARD);
332
333 memcpy(curpcb->pcb_fpu.fpreg, (caddr_t)&sregs.lfp_regs,
334 sizeof(curpcb->pcb_fpu.fpreg));
335
336 /*
337 * Restore signal stack.
338 *
339 * XXX cannot find the onstack information in Linux sig context.
340 * Is signal stack really supported on Linux?
341 *
342 * It seems to be supported in libc6...
343 */
344 /* if (sc.sc_onstack & SS_ONSTACK)
345 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
346 else */
347 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
348
349 /*
350 * Grab the signal mask
351 */
352 linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
353 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
354
355 return (EJUSTRETURN);
356 }
357
358
359 /*
360 * The following needs code review for potential security issues
361 */
362 int
363 linux_sys_sigreturn(l, v, retval)
364 struct lwp *l;
365 void *v;
366 register_t *retval;
367 {
368 struct linux_sys_sigreturn_args /* {
369 syscallarg(struct linux_sigcontext *) scp;
370 } */ *uap = v;
371 struct proc *p = l->l_proc;
372 struct linux_sigcontext *scp, context;
373 struct linux_sigregs sregs;
374 struct linux_pt_regs *lregs;
375 struct trapframe *tf;
376 sigset_t mask;
377 int i;
378
379 /*
380 * The trampoline code hands us the context.
381 * It is unsafe to keep track of it ourselves, in the event that a
382 * program jumps out of a signal handler.
383 */
384 scp = SCARG(uap, scp);
385
386 /*
387 * Get the context from user stack
388 */
389 if (copyin(scp, &context, sizeof(*scp)))
390 return (EFAULT);
391
392 /*
393 * Restore register context.
394 */
395 if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
396 return (EFAULT);
397 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
398
399 tf = trapframe(l);
400 #ifdef DEBUG_LINUX
401 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
402 (unsigned long)tf, (unsigned long)scp);
403 #endif
404
405 if (!PSL_USEROK_P(lregs->lmsr))
406 return (EINVAL);
407
408 for (i = 0; i < 32; i++)
409 tf->fixreg[i] = lregs->lgpr[i];
410 tf->lr = lregs->llink;
411 tf->cr = lregs->lccr;
412 tf->xer = lregs->lxer;
413 tf->ctr = lregs->lctr;
414 tf->srr0 = lregs->lnip;
415 tf->srr1 = lregs->lmsr;
416
417 /*
418 * Make sure the fpu state is discarded
419 */
420 save_fpu_lwp(curlwp, FPU_DISCARD);
421
422 memcpy(curpcb->pcb_fpu.fpreg, (caddr_t)&sregs.lfp_regs,
423 sizeof(curpcb->pcb_fpu.fpreg));
424
425 /*
426 * Restore signal stack.
427 *
428 * XXX cannot find the onstack information in Linux sig context.
429 * Is signal stack really supported on Linux?
430 */
431 #if 0
432 if (sc.sc_onstack & SS_ONSTACK)
433 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
434 else
435 #endif
436 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
437
438 /* Restore signal mask. */
439 linux_old_extra_to_native_sigset(&mask, &context.lmask,
440 &context._unused[3]);
441 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
442
443 return (EJUSTRETURN);
444 }
445
446
447 #if 0
448 int
449 linux_sys_modify_ldt(p, v, retval)
450 struct proc *p;
451 void *v;
452 register_t *retval;
453 {
454 /*
455 * This syscall is not implemented in Linux/PowerPC: we should not
456 * be here
457 */
458 #ifdef DEBUG_LINUX
459 printf("linux_sys_modify_ldt: should not be here.\n");
460 #endif
461 return 0;
462 }
463 #endif
464
465 /*
466 * major device numbers remapping
467 */
468 dev_t
469 linux_fakedev(dev, raw)
470 dev_t dev;
471 int raw;
472 {
473 /* XXX write me */
474 return dev;
475 }
476
477 /*
478 * We come here in a last attempt to satisfy a Linux ioctl() call
479 */
480 int
481 linux_machdepioctl(p, v, retval)
482 struct proc *p;
483 void *v;
484 register_t *retval;
485 {
486 struct linux_sys_ioctl_args /* {
487 syscallarg(int) fd;
488 syscallarg(u_long) com;
489 syscallarg(caddr_t) data;
490 } */ *uap = v;
491 struct sys_ioctl_args bia;
492 u_long com;
493
494 SCARG(&bia, fd) = SCARG(uap, fd);
495 SCARG(&bia, data) = SCARG(uap, data);
496 com = SCARG(uap, com);
497
498 switch (com) {
499 default:
500 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
501 return EINVAL;
502 }
503 SCARG(&bia, com) = com;
504 /* XXX NJWLWP */
505 return sys_ioctl(curlwp, &bia, retval);
506 }
507 #if 0
508 /*
509 * Set I/O permissions for a process. Just set the maximum level
510 * right away (ignoring the argument), otherwise we would have
511 * to rely on I/O permission maps, which are not implemented.
512 */
513 int
514 linux_sys_iopl(l, v, retval)
515 struct lwp *l;
516 void *v;
517 register_t *retval;
518 {
519 /*
520 * This syscall is not implemented in Linux/PowerPC: we should not be here
521 */
522 #ifdef DEBUG_LINUX
523 printf("linux_sys_iopl: should not be here.\n");
524 #endif
525 return 0;
526 }
527 #endif
528
529 /*
530 * See above. If a root process tries to set access to an I/O port,
531 * just let it have the whole range.
532 */
533 int
534 linux_sys_ioperm(l, v, retval)
535 struct lwp *l;
536 void *v;
537 register_t *retval;
538 {
539 /*
540 * This syscall is not implemented in Linux/PowerPC: we should not be here
541 */
542 #ifdef DEBUG_LINUX
543 printf("linux_sys_ioperm: should not be here.\n");
544 #endif
545 return 0;
546 }
547
548 /*
549 * wrapper linux_sys_new_uname() -> linux_sys_uname()
550 */
551 int
552 linux_sys_new_uname(l, v, retval)
553 struct lwp *l;
554 void *v;
555 register_t *retval;
556 {
557 return linux_sys_uname(l, v, retval);
558 }
559
560 /*
561 * wrapper linux_sys_new_select() -> linux_sys_select()
562 */
563 int
564 linux_sys_new_select(l, v, retval)
565 struct lwp *l;
566 void *v;
567 register_t *retval;
568 {
569 return linux_sys_select(l, v, retval);
570 }
571