linux_machdep.c revision 1.9 1 /* $NetBSD: linux_machdep.c,v 1.9 2001/05/27 21:11:53 manu 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) && !defined(_LKM)
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 #if defined(_KERNEL) && !defined(_LKM)
90 #endif
91 #endif
92
93 /*
94 * Set set up registers on exec.
95 * XXX not used at the moment since in sys/kern/exec_conf, LINUX_COMPAT
96 * entry uses NetBSD's native setregs instead of linux_setregs
97 */
98 void
99 linux_setregs(p, pack, stack)
100 struct proc *p;
101 struct exec_package *pack;
102 u_long stack;
103 {
104 setregs(p, pack, stack);
105 }
106
107 /*
108 * Send an interrupt to process.
109 *
110 * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
111 * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
112 *
113 * XXX Does not work well yet with RT signals
114 *
115 */
116
117 void
118 linux_sendsig(catcher, sig, mask, code) /* XXX Check me */
119 sig_t catcher;
120 int sig;
121 sigset_t *mask;
122 u_long code;
123 {
124 struct proc *p = curproc;
125 struct trapframe *tf;
126 struct linux_sigregs frame;
127 struct linux_pt_regs linux_regs;
128 struct linux_sigcontext sc;
129 register_t fp;
130 int onstack;
131 int i;
132
133 tf = trapframe(p);
134
135 /*
136 * Do we need to jump onto the signal stack?
137 */
138 onstack =
139 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
140 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
141
142 /*
143 * Signal stack is broken (see at the end of linux_sigreturn), so we do
144 * not use it yet. XXX fix this.
145 */
146 onstack=0;
147
148 /*
149 * Allocate space for the signal handler context.
150 */
151 if (onstack) {
152 fp = (register_t)
153 ((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
154 p->p_sigctx.ps_sigstk.ss_size);
155 } else {
156 fp = tf->fixreg[1];
157 }
158 #ifdef DEBUG_LINUX
159 printf("fp at start of linux_sendsig = %x\n", fp);
160 #endif
161 fp -= sizeof(struct linux_sigregs);
162 fp &= ~0xf;
163
164 /*
165 * Prepare a sigcontext for later.
166 */
167 memset(&sc, 0, sizeof sc);
168 sc.lsignal = (int)native_to_linux_sig[sig];
169 sc.lhandler = (unsigned long)catcher;
170 native_to_linux_old_extra_sigset(mask, &sc.lmask, &sc._unused[3]);
171 sc.lregs = (struct linux_pt_regs*)fp;
172
173 /*
174 * Setup the signal stack frame as Linux does it in
175 * arch/ppc/kernel/signal.c:setup_frame()
176 *
177 * Save register context.
178 */
179 for (i = 0; i < 32; i++)
180 linux_regs.lgpr[i] = tf->fixreg[i];
181 linux_regs.lnip = tf->srr0;
182 linux_regs.lmsr = tf->srr1;
183 linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
184 linux_regs.lctr = tf->ctr;
185 linux_regs.llink = tf->lr;
186 linux_regs.lxer = tf->xer;
187 linux_regs.lccr = tf->cr;
188 linux_regs.lmq = 0; /* Unused, 601 only */
189 linux_regs.ltrap = tf->exc;
190 linux_regs.ldar = tf->dar;
191 linux_regs.ldsisr = tf->dsisr;
192 linux_regs.lresult = 0;
193
194 memset(&frame, 0, sizeof(frame));
195 memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
196
197 if (curproc == fpuproc)
198 save_fpu(curproc);
199 memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpr, 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(p, 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(p, 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_sig[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(p, v, retval)
274 struct proc *p;
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 linux_rt_sigframe *scp, sigframe;
282 struct linux_sigregs sregs;
283 struct linux_pt_regs *lregs;
284 struct trapframe *tf;
285 sigset_t mask;
286 int i;
287
288 /*
289 * The trampoline code hands us the context.
290 * It is unsafe to keep track of it ourselves, in the event that a
291 * program jumps out of a signal handler.
292 */
293 scp = SCARG(uap, sfp);
294
295 /*
296 * Get the context from user stack
297 */
298 if (copyin((caddr_t)scp, &sigframe, sizeof(*scp)))
299 return (EFAULT);
300
301 /*
302 * Make sure, fpu is sync'ed
303 */
304 if (curproc == fpuproc)
305 save_fpu(curproc);
306
307 /*
308 * Restore register context.
309 */
310 if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
311 &sregs, sizeof(sregs)))
312 return (EFAULT);
313 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
314
315 tf = trapframe(p);
316 #ifdef DEBUG_LINUX
317 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
318 (unsigned long)tf, (unsigned long)scp);
319 #endif
320
321 if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
322 return (EINVAL);
323
324 for (i = 0; i < 32; i++)
325 tf->fixreg[i] = lregs->lgpr[i];
326 tf->lr = lregs->llink;
327 tf->cr = lregs->lccr;
328 tf->xer = lregs->lxer;
329 tf->ctr = lregs->lctr;
330 tf->srr0 = lregs->lnip;
331 tf->srr1 = lregs->lmsr;
332
333 memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
334 sizeof(curpcb->pcb_fpu.fpr));
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(&sigframe.luc.luc_sigmask, &mask);
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(p, v, retval)
364 struct proc *p;
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 linux_sigcontext *scp, context;
372 struct linux_sigregs sregs;
373 struct linux_pt_regs *lregs;
374 struct trapframe *tf;
375 sigset_t mask;
376 int i;
377
378 /*
379 * The trampoline code hands us the context.
380 * It is unsafe to keep track of it ourselves, in the event that a
381 * program jumps out of a signal handler.
382 */
383 scp = SCARG(uap, scp);
384
385 /*
386 * Get the context from user stack
387 */
388 if (copyin(scp, &context, sizeof(*scp)))
389 return (EFAULT);
390
391 /*
392 * Make sure, fpu is in sync
393 */
394 if (curproc == fpuproc)
395 save_fpu(curproc);
396
397 /*
398 * Restore register context.
399 */
400 if (copyin((caddr_t)context.lregs, &sregs, sizeof(sregs)))
401 return (EFAULT);
402 lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
403
404 tf = trapframe(p);
405 #ifdef DEBUG_LINUX
406 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
407 (unsigned long)tf, (unsigned long)scp);
408 #endif
409
410 if ((lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
411 return (EINVAL);
412
413 for (i = 0; i < 32; i++)
414 tf->fixreg[i] = lregs->lgpr[i];
415 tf->lr = lregs->llink;
416 tf->cr = lregs->lccr;
417 tf->xer = lregs->lxer;
418 tf->ctr = lregs->lctr;
419 tf->srr0 = lregs->lnip;
420 tf->srr1 = lregs->lmsr;
421
422 memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
423 sizeof(curpcb->pcb_fpu.fpr));
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(&context.lmask,
440 &context._unused[3],
441 &mask);
442 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
443
444 return (EJUSTRETURN);
445 }
446
447
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
464 /*
465 * major device numbers remapping
466 */
467 dev_t
468 linux_fakedev(dev)
469 dev_t dev;
470 {
471 /* XXX write me */
472 return dev;
473 }
474
475 /*
476 * We come here in a last attempt to satisfy a Linux ioctl() call
477 */
478 int
479 linux_machdepioctl(p, v, retval)
480 struct proc *p;
481 void *v;
482 register_t *retval;
483 {
484 struct linux_sys_ioctl_args /* {
485 syscallarg(int) fd;
486 syscallarg(u_long) com;
487 syscallarg(caddr_t) data;
488 } */ *uap = v;
489 struct sys_ioctl_args bia;
490 u_long com;
491
492 SCARG(&bia, fd) = SCARG(uap, fd);
493 SCARG(&bia, data) = SCARG(uap, data);
494 com = SCARG(uap, com);
495
496 switch (com) {
497 default:
498 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
499 return EINVAL;
500 }
501 SCARG(&bia, com) = com;
502 return sys_ioctl(p, &bia, retval);
503 }
504 /*
505 * Set I/O permissions for a process. Just set the maximum level
506 * right away (ignoring the argument), otherwise we would have
507 * to rely on I/O permission maps, which are not implemented.
508 */
509 int
510 linux_sys_iopl(p, v, retval)
511 struct proc *p;
512 void *v;
513 register_t *retval;
514 {
515 /*
516 * This syscall is not implemented in Linux/PowerPC: we should not be here
517 */
518 #ifdef DEBUG_LINUX
519 printf("linux_sys_iopl: should not be here.\n");
520 #endif
521 return 0;
522 }
523
524 /*
525 * See above. If a root process tries to set access to an I/O port,
526 * just let it have the whole range.
527 */
528 int
529 linux_sys_ioperm(p, v, retval)
530 struct proc *p;
531 void *v;
532 register_t *retval;
533 {
534 /*
535 * This syscall is not implemented in Linux/PowerPC: we should not be here
536 */
537 #ifdef DEBUG_LINUX
538 printf("linux_sys_ioperm: should not be here.\n");
539 #endif
540 return 0;
541 }
542
543 /*
544 * wrapper linux_sys_new_uname() -> linux_sys_uname()
545 */
546 int
547 linux_sys_new_uname(p, v, retval)
548 struct proc *p;
549 void *v;
550 register_t *retval;
551 {
552 return linux_sys_uname(p, v, retval);
553 }
554
555 /*
556 * wrapper linux_sys_new_select() -> linux_sys_select()
557 */
558 int
559 linux_sys_new_select(p, v, retval)
560 struct proc *p;
561 void *v;
562 register_t *retval;
563 {
564 return linux_sys_select(p, v, retval);
565 }
566