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