1 /*	$NetBSD: linux_machdep.c,v 1.45 2011/01/20 11:59:24 he 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.45 2011/01/20 11:59:24 he Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/signalvar.h>
38 #include <sys/kernel.h>
39 #include <sys/proc.h>
40 #include <sys/buf.h>
41 #include <sys/reboot.h>
42 #include <sys/conf.h>
43 #include <sys/exec.h>
44 #include <sys/file.h>
45 #include <sys/callout.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/msgbuf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/device.h>
52 #include <sys/syscallargs.h>
53 #include <sys/filedesc.h>
54 #include <sys/exec_elf.h>
55 #include <sys/disklabel.h>
56 #include <sys/ioctl.h>
57 #include <miscfs/specfs/specdev.h>
58 
59 #include <compat/linux/common/linux_types.h>
60 #include <compat/linux/common/linux_signal.h>
61 #include <compat/linux/common/linux_util.h>
62 #include <compat/linux/common/linux_ioctl.h>
63 #include <compat/linux/common/linux_hdio.h>
64 #include <compat/linux/common/linux_exec.h>
65 #include <compat/linux/common/linux_machdep.h>
66 
67 #include <compat/linux/linux_syscallargs.h>
68 
69 #include <sys/cpu.h>
70 #include <machine/fpu.h>
71 #include <machine/psl.h>
72 #include <machine/pcb.h>
73 #include <machine/vmparam.h>
74 
75 /*
76  * To see whether wscons is configured (for virtual console ioctl calls).
77  */
78 #if defined(_KERNEL_OPT)
79 #include "wsdisplay.h"
80 #endif
81 #if (NWSDISPLAY > 0)
82 #include <dev/wscons/wsconsio.h>
83 #include <dev/wscons/wsdisplay_usl_io.h>
84 #endif
85 
86 /*
87  * Set set up registers on exec.
88  */
89 void
90 linux_setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack)
91 {
92 	setregs(l, pack, stack);
93 }
94 
95 /*
96  * Send an interrupt to process.
97  *
98  * Adapted from arch/powerpc/powerpc/sig_machdep.c:sendsig and
99  * compat/linux/arch/i386/linux_machdep.c:linux_sendsig
100  *
101  * XXX Does not work well yet with RT signals
102  *
103  */
104 
105 void
106 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
107 {
108 	const int sig = ksi->ksi_signo;
109 	struct lwp *l = curlwp;
110 	struct proc *p = l->l_proc;
111 	struct trapframe *tf;
112 	sig_t catcher = SIGACTION(p, sig).sa_handler;
113 	struct linux_sigregs frame;
114 	struct linux_pt_regs linux_regs;
115 	struct linux_sigcontext sc;
116 	register_t fp;
117 	int onstack, error;
118 	int i;
119 
120 	tf = trapframe(l);
121 
122 	/*
123 	 * Do we need to jump onto the signal stack?
124 	 */
125 	onstack =
126 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
127 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
128 
129 	/*
130 	 * Signal stack is broken (see at the end of linux_sigreturn), so we do
131 	 * not use it yet. XXX fix this.
132 	 */
133 	onstack=0;
134 
135 	/*
136 	 * Allocate space for the signal handler context.
137 	 */
138 	if (onstack) {
139 		fp = (register_t)
140 		    ((char *)l->l_sigstk.ss_sp +
141 		    l->l_sigstk.ss_size);
142 	} else {
143 		fp = tf->tf_fixreg[1];
144 	}
145 #ifdef DEBUG_LINUX
146 	printf("fp at start of linux_sendsig = %x\n", fp);
147 #endif
148 	fp -= sizeof(struct linux_sigregs);
149 	fp &= ~0xf;
150 
151 	/*
152 	 * Prepare a sigcontext for later.
153 	 */
154 	memset(&sc, 0, sizeof sc);
155 	sc.lsignal = (int)native_to_linux_signo[sig];
156 	sc.lhandler = (unsigned long)catcher;
157 	native_to_linux_old_extra_sigset(&sc.lmask, &sc._unused[3], mask);
158 	sc.lregs = (struct linux_pt_regs*)fp;
159 
160 	/*
161 	 * Setup the signal stack frame as Linux does it in
162 	 * arch/ppc/kernel/signal.c:setup_frame()
163 	 *
164 	 * Save register context.
165 	 */
166 	for (i = 0; i < 32; i++)
167 		linux_regs.lgpr[i] = tf->tf_fixreg[i];
168 	linux_regs.lnip = tf->tf_srr0;
169 	linux_regs.lmsr = tf->tf_srr1 & PSL_USERSRR1;
170 	linux_regs.lorig_gpr3 = tf->tf_fixreg[3]; /* XXX Is that right? */
171 	linux_regs.lctr = tf->tf_ctr;
172 	linux_regs.llink = tf->tf_lr;
173 	linux_regs.lxer = tf->tf_xer;
174 	linux_regs.lccr = tf->tf_cr;
175 	linux_regs.lmq = 0;  			/* Unused, 601 only */
176 	linux_regs.ltrap = tf->tf_exc;
177 	linux_regs.ldar = tf->tf_dar;
178 	linux_regs.ldsisr = tf->tf_dsisr;
179 	linux_regs.lresult = 0;
180 
181 	memset(&frame, 0, sizeof(frame));
182 	memcpy(&frame.lgp_regs, &linux_regs, sizeof(linux_regs));
183 
184 	fpu_save_lwp(curlwp, FPU_SAVE);
185 	memcpy(&frame.lfp_regs, curpcb->pcb_fpu.fpreg, sizeof(frame.lfp_regs));
186 
187 	/*
188 	 * Copy Linux's signal trampoline on the user stack It should not
189 	 * be used, but Linux binaries might expect it to be there.
190 	 */
191 	frame.ltramp[0] = 0x38997777; /* li r0, 0x7777 */
192 	frame.ltramp[1] = 0x44000002; /* sc */
193 
194 	/*
195 	 * Move it to the user stack
196 	 * There is a little trick here, about the LINUX_ABIGAP: the
197 	 * linux_sigreg structure has a 56 int gap to support rs6000/xcoff
198 	 * binaries. But the Linux kernel seems to do without it, and it
199 	 * just skip it when building the stack frame. Hence the LINUX_ABIGAP.
200 	 */
201 	sendsig_reset(l, sig);
202 	mutex_exit(p->p_lock);
203 	error = copyout(&frame, (void *)fp, sizeof (frame) - LINUX_ABIGAP);
204 
205 	if (error != 0) {
206 		/*
207 		 * Process has trashed its stack; give it an illegal
208 		 * instruction to halt it in its tracks.
209 		 */
210 		mutex_enter(p->p_lock);
211 		sigexit(l, SIGILL);
212 		/* NOTREACHED */
213 	}
214 
215 	/*
216 	 * Add a sigcontext on the stack
217 	 */
218 	fp -= sizeof(struct linux_sigcontext);
219 	error = copyout(&sc, (void *)fp, sizeof (struct linux_sigcontext));
220 	mutex_enter(p->p_lock);
221 
222 	if (error != 0) {
223 		/*
224 		 * Process has trashed its stack; give it an illegal
225 		 * instruction to halt it in its tracks.
226 		 */
227 		sigexit(l, SIGILL);
228 		/* NOTREACHED */
229 	}
230 
231 	/*
232 	 * Set the registers according to how the Linux process expects them.
233 	 * "Mind the gap" Linux expects a gap here.
234 	 */
235 	tf->tf_fixreg[1] = fp - LINUX__SIGNAL_FRAMESIZE;
236 	tf->tf_lr = (int)catcher;
237 	tf->tf_fixreg[3] = (int)native_to_linux_signo[sig];
238 	tf->tf_fixreg[4] = fp;
239 	tf->tf_srr0 = (int)p->p_sigctx.ps_sigcode;
240 
241 #ifdef DEBUG_LINUX
242 	printf("fp at end of linux_sendsig = %x\n", fp);
243 #endif
244 	/*
245 	 * Remember that we're now on the signal stack.
246 	 */
247 	if (onstack)
248 		l->l_sigstk.ss_flags |= SS_ONSTACK;
249 #ifdef DEBUG_LINUX
250 	printf("linux_sendsig: exitting. fp=0x%lx\n",(long)fp);
251 #endif
252 }
253 
254 /*
255  * System call to cleanup state after a signal
256  * has been taken.  Reset signal mask and
257  * stack state from context left by sendsig (above).
258  * Return to previous pc and psl as specified by
259  * context left by sendsig. Check carefully to
260  * make sure that the user has not modified the
261  * psl to gain improper privileges or to cause
262  * a machine fault.
263  *
264  * XXX not tested
265  */
266 int
267 linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval)
268 {
269 	/* {
270 		syscallarg(struct linux_rt_sigframe *) sfp;
271 	} */
272 	struct proc *p = l->l_proc;
273 	struct linux_rt_sigframe *scp, sigframe;
274 	struct linux_sigregs sregs;
275 	struct linux_pt_regs *lregs;
276 	struct trapframe *tf;
277 	sigset_t mask;
278 	int i;
279 
280 	/*
281 	 * The trampoline code hands us the context.
282 	 * It is unsafe to keep track of it ourselves, in the event that a
283 	 * program jumps out of a signal handler.
284 	 */
285 	scp = SCARG(uap, sfp);
286 
287 	/*
288 	 * Get the context from user stack
289 	 */
290 	if (copyin((void *)scp, &sigframe, sizeof(*scp)))
291 		return (EFAULT);
292 
293 	/*
294 	 *  Restore register context.
295 	 */
296 	if (copyin((void *)sigframe.luc.luc_context.lregs,
297 		   &sregs, sizeof(sregs)))
298 		return (EFAULT);
299 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
300 
301 	tf = trapframe(l);
302 #ifdef DEBUG_LINUX
303 	printf("linux_sys_rt_sigreturn: trapframe=0x%lx scp=0x%lx\n",
304 	    (unsigned long)tf, (unsigned long)scp);
305 #endif
306 
307 	if (!PSL_USEROK_P(lregs->lmsr))
308 		return (EINVAL);
309 
310 	for (i = 0; i < 32; i++)
311 		tf->tf_fixreg[i] = lregs->lgpr[i];
312 	tf->tf_lr = lregs->llink;
313 	tf->tf_cr = lregs->lccr;
314 	tf->tf_xer = lregs->lxer;
315 	tf->tf_ctr = lregs->lctr;
316 	tf->tf_srr0 = lregs->lnip;
317 	tf->tf_srr1 = lregs->lmsr;
318 
319 	/*
320 	 * Make sure the fpu state is discarded
321 	 */
322 	fpu_save_lwp(curlwp, FPU_DISCARD);
323 
324 	memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
325 	       sizeof(curpcb->pcb_fpu.fpreg));
326 
327 	mutex_enter(p->p_lock);
328 
329 	/*
330 	 * Restore signal stack.
331 	 *
332 	 * XXX cannot find the onstack information in Linux sig context.
333 	 * Is signal stack really supported on Linux?
334 	 *
335 	 * It seems to be supported in libc6...
336 	 */
337 	/* if (sc.sc_onstack & SS_ONSTACK)
338 		l->l_sigstk.ss_flags |= SS_ONSTACK;
339 	else */
340 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
341 
342 	/*
343 	 * Grab the signal mask
344 	 */
345 	linux_to_native_sigset(&mask, &sigframe.luc.luc_sigmask);
346 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
347 
348 	mutex_exit(p->p_lock);
349 
350 	return (EJUSTRETURN);
351 }
352 
353 
354 /*
355  * The following needs code review for potential security issues
356  */
357 int
358 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
359 {
360 	/* {
361 		syscallarg(struct linux_sigcontext *) scp;
362 	} */
363 	struct proc *p = l->l_proc;
364 	struct linux_sigcontext *scp, context;
365 	struct linux_sigregs sregs;
366 	struct linux_pt_regs *lregs;
367 	struct trapframe *tf;
368 	sigset_t mask;
369 	int i;
370 
371 	/*
372 	 * The trampoline code hands us the context.
373 	 * It is unsafe to keep track of it ourselves, in the event that a
374 	 * program jumps out of a signal handler.
375 	 */
376 	scp = SCARG(uap, scp);
377 
378 	/*
379 	 * Get the context from user stack
380 	 */
381 	if (copyin(scp, &context, sizeof(*scp)))
382 		return (EFAULT);
383 
384 	/*
385 	 *  Restore register context.
386 	 */
387 	if (copyin((void *)context.lregs, &sregs, sizeof(sregs)))
388 		return (EFAULT);
389 	lregs = (struct linux_pt_regs *)&sregs.lgp_regs;
390 
391 	tf = trapframe(l);
392 #ifdef DEBUG_LINUX
393 	printf("linux_sys_sigreturn: trapframe=0x%lx scp=0x%lx\n",
394 	    (unsigned long)tf, (unsigned long)scp);
395 #endif
396 
397 	if (!PSL_USEROK_P(lregs->lmsr))
398 		return (EINVAL);
399 
400 	for (i = 0; i < 32; i++)
401 		tf->tf_fixreg[i] = lregs->lgpr[i];
402 	tf->tf_lr = lregs->llink;
403 	tf->tf_cr = lregs->lccr;
404 	tf->tf_xer = lregs->lxer;
405 	tf->tf_ctr = lregs->lctr;
406 	tf->tf_srr0 = lregs->lnip;
407 	tf->tf_srr1 = lregs->lmsr;
408 
409 	/*
410 	 * Make sure the fpu state is discarded
411 	 */
412 	fpu_save_lwp(curlwp, FPU_DISCARD);
413 
414 	memcpy(curpcb->pcb_fpu.fpreg, (void *)&sregs.lfp_regs,
415 	       sizeof(curpcb->pcb_fpu.fpreg));
416 
417 	mutex_enter(p->p_lock);
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 0
426 	if (sc.sc_onstack & SS_ONSTACK)
427 		l->l_sigstk.ss_flags |= SS_ONSTACK;
428 	else
429 #endif
430 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
431 
432 	/* Restore signal mask. */
433 	linux_old_extra_to_native_sigset(&mask, &context.lmask,
434 	    &context._unused[3]);
435 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
436 
437 	mutex_exit(p->p_lock);
438 
439 	return (EJUSTRETURN);
440 }
441 
442 /*
443  * major device numbers remapping
444  */
445 dev_t
446 linux_fakedev(dev_t dev, int raw)
447 {
448 	/* XXX write me */
449 	return dev;
450 }
451 
452 /*
453  * We come here in a last attempt to satisfy a Linux ioctl() call
454  */
455 int
456 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
457 {
458 	/* {
459 		syscallarg(int) fd;
460 		syscallarg(u_long) com;
461 		syscallarg(void *) data;
462 	} */
463 	struct sys_ioctl_args bia;
464 	u_long com;
465 
466 	SCARG(&bia, fd) = SCARG(uap, fd);
467 	SCARG(&bia, data) = SCARG(uap, data);
468 	com = SCARG(uap, com);
469 
470 	switch (com) {
471 	default:
472 		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
473 		return EINVAL;
474 	}
475 	SCARG(&bia, com) = com;
476 	/* XXX NJWLWP */
477 	return sys_ioctl(curlwp, &bia, retval);
478 }
479 
480 /*
481  * wrapper linux_sys_new_uname() -> linux_sys_uname()
482  */
483 int
484 linux_sys_new_uname(struct lwp *l, const struct linux_sys_new_uname_args *uap, register_t *retval)
485 {
486 	return linux_sys_uname(l, (const void *)uap, retval);
487 }
488 
489 /*
490  * wrapper linux_sys_new_select() -> linux_sys_select()
491  */
492 int
493 linux_sys_new_select(struct lwp *l, const struct linux_sys_new_select_args *uap, register_t *retval)
494 {
495 	return linux_sys_select(l, (const void *)uap, retval);
496 }
497 
498 int
499 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
500 {
501 	return 0;
502 }
503