linux_machdep.c revision 1.1 1 /* $NetBSD: linux_machdep.c,v 1.1 2001/01/19 01:36:51 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 <miscfs/specfs/specdev.h>
63
64 #include <compat/linux/common/linux_types.h>
65 #include <compat/linux/common/linux_signal.h>
66 #include <compat/linux/common/linux_util.h>
67 #include <compat/linux/common/linux_ioctl.h>
68 #include <compat/linux/common/linux_hdio.h>
69 #include <compat/linux/common/linux_exec.h>
70 #include <compat/linux/common/linux_machdep.h>
71
72 #include <compat/linux/linux_syscallargs.h>
73
74 #include <machine/cpu.h>
75 #include <machine/psl.h>
76 #include <machine/reg.h>
77 #include <machine/vmparam.h>
78
79 /*
80 * To see whether wscons is configured (for virtual console ioctl calls).
81 */
82 #if defined(_KERNEL) && !defined(_LKM)
83 #include "wsdisplay.h"
84 #endif
85 #if (NWSDISPLAY > 0)
86 #include <sys/ioctl.h>
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.
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 *fp, frame;
127 struct linux_pt_regs linux_regs;
128 struct linux_sigcontext sc;
129 int onstack;
130 int i;
131
132 tf = trapframe(p);
133
134 /*
135 * Do we need to jump onto the signal stack?
136 */
137 onstack =
138 (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
139 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
140
141 /*
142 * Signal stack is broken (see at the end of linux_sigreturn), so we do
143 * not use it yet. XXX fix this.
144 */
145 onstack=0;
146
147 /*
148 * Allocate space for the signal handler context.
149 */
150 if (onstack)
151 fp = (struct linux_sigregs *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
152 p->p_sigctx.ps_sigstk.ss_size);
153 else
154 fp = (struct linux_sigregs *)tf->fixreg[1];
155 fp = (struct linux_sigregs *)((int)(fp - 1) & ~0xf);
156
157 /*
158 * Prepare a sigcontext for later.
159 */
160 sc.lsignal = (int)native_to_linux_sig[sig];
161 sc.lhandler = (unsigned long)catcher;
162 native_to_linux_old_sigset(mask, &sc.lmask);
163 sc.lregs = (struct linux_pt_regs*)fp;
164
165 /*
166 * Setup the signal stack frame as Linux does it in
167 * arch/ppc/kernel/signal.c:setup_frame()
168 *
169 * Save register context.
170 */
171 for (i=0; i<=31; i++)
172 linux_regs.lgpr[i] = tf->fixreg[i];
173 linux_regs.lnip = tf->srr0;
174 linux_regs.lmsr = tf->srr1;
175 linux_regs.lorig_gpr3 = tf->fixreg[3]; /* XXX Is that right? */
176 linux_regs.lctr = tf->ctr;
177 linux_regs.llink = tf->lr;
178 linux_regs.lxer = tf->xer;
179 linux_regs.lccr = tf->cr;
180 linux_regs.lmq = 0; /* Unused, 601 only */
181 linux_regs.ltrap = 0; /* XXX What is ltrap counterpart in NetBSD ? */
182 linux_regs.ldar = tf->dar;
183 linux_regs.ldsisr = tf->dsisr;
184 linux_regs.lresult = tf->exc;
185 bcopy (&linux_regs, &frame.lgp_regs, sizeof (frame.lgp_regs));
186
187 /*
188 * NetBSD does not uses the FPU in the kernel, so there is no
189 * need to save floating point register. However, Linux expects
190 * them to be saved on the stack. Therefore we just keep a
191 * gap of zero'ed data where the FP registers should be stored
192 */
193 memset (&frame.lfp_regs, 0, sizeof (frame.lfp_regs));
194
195 /*
196 * Copy Linux's signal trampoline on the user stack It should not be used,
197 * but Linux binaries might expect him to be there.
198 */
199 frame.ltramp[0]=0x38997777; /* li r0, 0x7777 */
200 frame.ltramp[1]=0x44000002; /* sc */
201
202 /*
203 * Move it to the user stack
204 * There is a little trick here, about the LINUX_ABIGAP: the
205 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
206 * binaries. But the Linux kernel seems to do without it, and it
207 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
208 */
209 if (copyout(&frame, fp, sizeof (frame) - LINUX_ABIGAP)) {
210 /*
211 * Process has trashed its stack; give it an illegal
212 * instructoin to halt it in its tracks.
213 */
214 sigexit(p, SIGILL);
215 /* NOTREACHED */
216 }
217
218 /*
219 * adjust stack pointer after the previous data copy
220 */
221 (unsigned long)fp -= (sizeof (frame) - LINUX_ABIGAP);
222
223 /*
224 * "Mind the gap" Linux expects a gap here.
225 */
226 (unsigned long)fp -= LINUX__SIGNAL_FRAMESIZE;
227
228 /*
229 * Add a sigcontext on the stack
230 */
231 if (copyout(&sc, fp, sizeof (struct linux_sigcontext))) {
232 /*
233 * Process has trashed its stack; give it an illegal
234 * instructoin to halt it in its tracks.
235 */
236 sigexit(p, SIGILL);
237 /* NOTREACHED */
238 }
239
240 /*
241 * Here, I expected to need a stack pointer adjust after the copy.
242 * Something like this: (unsigned long)fp-=sizeof(struct sigcontext)
243 * But if we do it, the signal handler does not get its arguments as
244 * expected.
245 */
246
247 /*
248 * Set the registers according to how the Linux process expects them
249 */
250 tf->fixreg[1] = (int)fp;
251 tf->lr = (int)catcher;
252 tf->fixreg[3] = (int)sig;
253 tf->fixreg[4] = (int)code;
254 tf->fixreg[5] = (int)&fp->lgp_regs;
255 tf->srr0 = (int)p->p_sigctx.ps_sigcode;
256
257 /*
258 * Remember that we're now on the signal stack.
259 */
260 if (onstack)
261 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
262 #ifdef DEBUG_LINUX
263 printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
264 #endif
265 }
266
267 /*
268 * System call to cleanup state after a signal
269 * has been taken. Reset signal mask and
270 * stack state from context left by sendsig (above).
271 * Return to previous pc and psl as specified by
272 * context left by sendsig. Check carefully to
273 * make sure that the user has not modified the
274 * psl to gain improper privileges or to cause
275 * a machine fault.
276 *
277 * XXX not tested
278 */
279 int
280 linux_sys_rt_sigreturn(p, v, retval)
281 struct proc *p;
282 void *v;
283 register_t *retval;
284 {
285 struct linux_sys_rt_sigreturn_args /* {
286 syscallarg(struct linux_rt_sigframe *) sfp;
287 } */ *uap = v;
288 struct linux_rt_sigframe *scp, sigframe;
289 struct trapframe *tf;
290 sigset_t mask;
291 int i;
292
293 /*
294 * The trampoline code hands us the context.
295 * It is unsafe to keep track of it ourselves, in the event that a
296 * program jumps out of a signal handler.
297 */
298 scp = SCARG(uap, sfp);
299
300 /*
301 * It seems we need a 16 bytes alignement here (it just works with it, don't
302 * ask me why
303 */
304 (unsigned long)scp = (unsigned long) scp & ~0xfUL;
305
306 /*
307 * Get the context from user stack
308 */
309 if (copyin((caddr_t)scp, &sigframe, sizeof(struct linux_rt_sigframe)) != 0)
310 return (EFAULT);
311
312 /*
313 * Grab the signal mask
314 */
315 linux_to_native_sigset(&sigframe.luc.luc_sigmask, &mask);
316
317 /*
318 * Restore register context. XXX need security review
319 */
320 tf = trapframe(p);
321 #ifdef DEBUG_LINUX
322 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
323 (unsigned long)tf, (unsigned long)scp);
324 #endif
325
326 if ((sigframe.luc.luc_context.lregs->lmsr & PSL_USERSTATIC) !=
327 (tf->srr1 & PSL_USERSTATIC))
328 return (EINVAL);
329
330 for (i=0; i<=31; i++)
331 tf->fixreg[i] = sigframe.luc.luc_context.lregs->lgpr[i];
332 tf->lr = sigframe.luc.luc_context.lregs->llink;
333 tf->cr = sigframe.luc.luc_context.lregs->lccr;
334 tf->xer = sigframe.luc.luc_context.lregs->lxer;
335 tf->ctr = sigframe.luc.luc_context.lregs->lctr;
336 tf->srr0 = sigframe.luc.luc_context.lregs->lnip;
337 tf->srr1 = sigframe.luc.luc_context.lregs->lmsr;
338 tf->dar = sigframe.luc.luc_context.lregs->ldar;
339 tf->dsisr = sigframe.luc.luc_context.lregs->ldsisr;
340 tf->exc = sigframe.luc.luc_context.lregs->lresult;
341
342 /*
343 * Restore signal stack.
344 *
345 * XXX cannot find the onstack information in Linux sig context.
346 * Is signal stack really supported on Linux?
347 *
348 * It seems to be supported in libc6...
349 */
350 /* if (sc.sc_onstack & SS_ONSTACK)
351 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
352 else */
353 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
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 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 * It seems we need a 16 bytes alignement here (it just works with it, don't
385 * ask me why
386 */
387 (unsigned long)scp = (unsigned long) scp & ~0xfUL;
388
389 /*
390 * Get the context from user stack
391 */
392 if (copyin((caddr_t)scp, &context, sizeof(struct linux_sigcontext)) != 0)
393 return (EFAULT);
394
395 /*
396 * Restore register context. XXX need security review
397 */
398 tf = trapframe(p);
399 #ifdef DEBUG_LINUX
400 printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
401 (unsigned long)tf, (unsigned long)scp);
402 #endif
403
404 if ((context.lregs->lmsr & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
405 return (EINVAL);
406
407 for (i=0; i<=31; i++)
408 tf->fixreg[i] = context.lregs->lgpr[i];
409 tf->lr = context.lregs->llink;
410 tf->cr = context.lregs->lccr;
411 tf->xer = context.lregs->lxer;
412 tf->ctr = context.lregs->lctr;
413 tf->srr0 = context.lregs->lnip;
414 tf->srr1 = context.lregs->lmsr;
415 tf->dar = context.lregs->ldar;
416 tf->dsisr = context.lregs->ldsisr;
417 tf->exc = context.lregs->lresult;
418
419 /*
420 * Restore signal stack.
421 *
422 * XXX cannot find the onstack information in Linux sig context.
423 * Is signal stack really supported on Linux?
424 */
425 /* if (sc.sc_onstack & SS_ONSTACK)
426 p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
427 else */
428 p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
429
430 /* Restore signal mask. */
431 linux_old_to_native_sigset(&context.lmask, &mask);
432 (void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
433
434 return (EJUSTRETURN);
435 }
436
437
438 int
439 linux_sys_modify_ldt(p, v, retval)
440 struct proc *p;
441 void *v;
442 register_t *retval;
443 {
444 /*
445 * This syscall is not implemented in Linux/PowerPC: we should not be here
446 */
447 #ifdef DEBUG_LINUX
448 printf("linux_sys_modify_ldt: should not be here.\n");
449 #endif
450 return 0;
451 }
452
453 /* hack to make svgalib work */
454 dev_t
455 linux_fakedev(dev)
456 dev_t dev;
457 {
458 /* XXX write me */
459 return dev;
460 }
461
462 /*
463 * We come here in a last attempt to satisfy a Linux ioctl() call
464 */
465 int
466 linux_machdepioctl(p, v, retval)
467 struct proc *p;
468 void *v;
469 register_t *retval;
470 {
471 struct linux_sys_ioctl_args /* {
472 syscallarg(int) fd;
473 syscallarg(u_long) com;
474 syscallarg(caddr_t) data;
475 } */ *uap = v;
476 struct sys_ioctl_args bia;
477 u_long com;
478
479 SCARG(&bia, fd) = SCARG(uap, fd);
480 SCARG(&bia, data) = SCARG(uap, data);
481 com = SCARG(uap, com);
482
483 switch (com) {
484 default:
485 printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
486 return EINVAL;
487 }
488 SCARG(&bia, com) = com;
489 return sys_ioctl(p, &bia, retval);
490 }
491 /*
492 * Set I/O permissions for a process. Just set the maximum level
493 * right away (ignoring the argument), otherwise we would have
494 * to rely on I/O permission maps, which are not implemented.
495 */
496 int
497 linux_sys_iopl(p, v, retval)
498 struct proc *p;
499 void *v;
500 register_t *retval;
501 {
502 /*
503 * This syscall is not implemented in Linux/PowerPC: we should not be here
504 */
505 #ifdef DEBUG_LINUX
506 printf("linux_sys_iopl: should not be here.\n");
507 #endif
508 return 0;
509 }
510
511 /*
512 * See above. If a root process tries to set access to an I/O port,
513 * just let it have the whole range.
514 */
515 int
516 linux_sys_ioperm(p, v, retval)
517 struct proc *p;
518 void *v;
519 register_t *retval;
520 {
521 /*
522 * This syscall is not implemented in Linux/PowerPC: we should not be here
523 */
524 #ifdef DEBUG_LINUX
525 printf("linux_sys_ioperm: should not be here.\n");
526 #endif
527 return 0;
528 }
529
530 /*
531 * wrapper linux_sys_new_uname() -> linux_sys_uname()
532 */
533 int
534 linux_sys_new_uname(p, v, retval)
535 struct proc *p;
536 void *v;
537 register_t *retval;
538 {
539 return linux_sys_uname(p, v, retval);
540 }
541
542 /*
543 * wrapper linux_sys_new_select() -> linux_sys_select()
544 */
545 int
546 linux_sys_new_select(p, v, retval)
547 struct proc *p;
548 void *v;
549 register_t *retval;
550 {
551 return linux_sys_select(p, v, retval);
552 }
553