1 /*	$NetBSD: linux_machdep.c,v 1.19 2002/09/25 22:21:35 thorpej 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.19 2002/09/25 22:21:35 thorpej 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 <machine/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(p, pack, stack)
101 	struct proc *p;
102 	struct exec_package *pack;
103 	u_long stack;
104 {
105 	setregs(p, pack, stack);
106 }
107 
108 /*
109  * Send an interrupt to process.
110  *
111  * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
112  * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
113  *
114  * XXX Does not work well yet with RT signals
115  *
116  */
117 
118 void
119 linux_sendsig(sig, mask, code)  /* XXX Check me */
120 	int sig;
121 	sigset_t *mask;
122 	u_long code;
123 {
124 	struct proc *p = curproc;
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(p);
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;
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_proc(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_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(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 	save_fpu_proc(curproc);
305 
306 	/*
307 	 *  Restore register context.
308 	 */
309 	if (copyin((caddr_t)sigframe.luc.luc_context.lregs,
310 		   &sregs, sizeof(sregs)))
311 		return (EFAULT);
312 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
313 
314 	tf = trapframe(p);
315 #ifdef DEBUG_LINUX
316 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
317 	    (unsigned long)tf, (unsigned long)scp);
318 #endif
319 
320 	if ((lregs->lmsr & PSL_USERSTATIC) !=  (tf->srr1 & PSL_USERSTATIC))
321 		return (EINVAL);
322 
323 	for (i = 0; i < 32; i++)
324 		tf->fixreg[i] = lregs->lgpr[i];
325 	tf->lr = lregs->llink;
326 	tf->cr = lregs->lccr;
327 	tf->xer = lregs->lxer;
328 	tf->ctr = lregs->lctr;
329 	tf->srr0 = lregs->lnip;
330 	tf->srr1 = lregs->lmsr;
331 
332 	memcpy(curpcb->pcb_fpu.fpr, (caddr_t)&sregs.lfp_regs,
333 	       sizeof(curpcb->pcb_fpu.fpr));
334 
335 	/*
336 	 * Restore signal stack.
337 	 *
338 	 * XXX cannot find the onstack information in Linux sig context.
339 	 * Is signal stack really supported on Linux?
340 	 *
341 	 * It seems to be supported in libc6...
342 	 */
343 	/* if (sc.sc_onstack & SS_ONSTACK)
344 		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
345 	else */
346 		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
347 
348 	/*
349 	 * Grab the signal mask
350 	 */
351 	linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
352 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
353 
354 	return (EJUSTRETURN);
355 }
356 
357 
358 /*
359  * The following needs code review for potential security issues
360  */
361 int
362 linux_sys_sigreturn(p, v, retval)
363 	struct proc *p;
364 	void *v;
365 	register_t *retval;
366 {
367 	struct linux_sys_sigreturn_args /* {
368 		syscallarg(struct linux_sigcontext *) scp;
369 	} */ *uap = v;
370 	struct linux_sigcontext *scp, context;
371 	struct linux_sigregs sregs;
372 	struct linux_pt_regs *lregs;
373 	struct trapframe *tf;
374 	sigset_t mask;
375 	int i;
376 
377 	/*
378 	 * The trampoline code hands us the context.
379 	 * It is unsafe to keep track of it ourselves, in the event that a
380 	 * program jumps out of a signal handler.
381 	 */
382 	scp = SCARG(uap, scp);
383 
384 	/*
385 	 * Get the context from user stack
386 	 */
387 	if (copyin(scp, &context, sizeof(*scp)))
388 		return (EFAULT);
389 
390 	/*
391 	 * Make sure, fpu is in sync
392 	 */
393 	save_fpu_proc(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(&mask, &context.lmask,
438 	    &context._unused[3]);
439 	(void) sigprocmask1(p, SIG_SETMASK, &mask, 0);
440 
441 	return (EJUSTRETURN);
442 }
443 
444 
445 #if 0
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 #endif
462 
463 /*
464  * major device numbers remapping
465  */
466 dev_t
467 linux_fakedev(dev, raw)
468 	dev_t dev;
469 	int raw;
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 #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(p, v, retval)
512 	struct proc *p;
513 	void *v;
514 	register_t *retval;
515 {
516 	/*
517 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
518 	 */
519 #ifdef DEBUG_LINUX
520 	printf("linux_sys_iopl: should not be here.\n");
521 #endif
522 	return 0;
523 }
524 #endif
525 
526 /*
527  * See above. If a root process tries to set access to an I/O port,
528  * just let it have the whole range.
529  */
530 int
531 linux_sys_ioperm(p, v, retval)
532 	struct proc *p;
533 	void *v;
534 	register_t *retval;
535 {
536 	/*
537 	 * This syscall is not implemented in Linux/PowerPC: we should not be here
538 	 */
539 #ifdef DEBUG_LINUX
540 	printf("linux_sys_ioperm: should not be here.\n");
541 #endif
542 	return 0;
543 }
544 
545 /*
546  * wrapper linux_sys_new_uname() -> linux_sys_uname()
547  */
548 int
549 linux_sys_new_uname(p, v, retval)
550 	struct proc *p;
551 	void *v;
552 	register_t *retval;
553 {
554 	return linux_sys_uname(p, v, retval);
555 }
556 
557 /*
558  * wrapper linux_sys_new_select() -> linux_sys_select()
559  */
560 int
561 linux_sys_new_select(p, v, retval)
562 	struct proc *p;
563 	void *v;
564 	register_t *retval;
565 {
566 	return linux_sys_select(p, v, retval);
567 }
568