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