1/*	$NetBSD: linux_machdep.c,v 1.48 2011/06/09 13:41:40 matt Exp $	*/
2
3/*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Eric Haszlakiewicz.
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 * Based on sys/arch/i386/i386/linux_machdep.c:
32 *	linux_machdep.c,v 1.42 1998/09/11 12:50:06 mycroft Exp
33 *	written by Frank van der Linden
34 *
35 */
36
37#include <sys/cdefs.h>
38__KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.48 2011/06/09 13:41:40 matt Exp $");
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/signalvar.h>
43#include <sys/kernel.h>
44#include <sys/proc.h>
45#include <sys/buf.h>
46#include <sys/reboot.h>
47#include <sys/conf.h>
48#include <sys/exec.h>
49#include <sys/file.h>
50#include <sys/callout.h>
51#include <sys/malloc.h>
52#include <sys/mbuf.h>
53#include <sys/msgbuf.h>
54#include <sys/mount.h>
55#include <sys/vnode.h>
56#include <sys/device.h>
57#include <sys/syscallargs.h>
58#include <sys/filedesc.h>
59#include <sys/exec_elf.h>
60#include <sys/ioctl.h>
61#include <sys/kauth.h>
62
63#include <uvm/uvm_extern.h>
64
65#include <compat/linux/common/linux_types.h>
66#include <compat/linux/common/linux_signal.h>
67#include <compat/linux/common/linux_siginfo.h>
68#include <compat/linux/common/linux_util.h>
69#include <compat/linux/common/linux_ioctl.h>
70#include <compat/linux/common/linux_exec.h>
71#include <compat/linux/common/linux_machdep.h>
72#include <compat/linux/common/linux_emuldata.h>
73
74#include <compat/linux/linux_syscallargs.h>
75
76#include <machine/alpha.h>
77#include <machine/reg.h>
78
79#if defined(_KERNEL_OPT)
80#include "wsdisplay.h"
81#endif
82#if (NWSDISPLAY >0)
83#include <dev/wscons/wsdisplay_usl_io.h>
84#endif
85#ifdef DEBUG
86#include <machine/sigdebug.h>
87#endif
88
89/*
90 * Deal with some alpha-specific things in the Linux emulation code.
91 */
92
93void
94linux_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
95{
96#ifdef DEBUG
97	struct trapframe *tfp = l->l_md.md_tf;
98#endif
99
100	setregs(l, epp, stack);
101#ifdef DEBUG
102	/*
103	 * Linux has registers set to zero on entry; for DEBUG kernels
104	 * the alpha setregs() fills registers with 0xbabefacedeadbeef.
105	 */
106	memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]);
107#endif
108}
109
110void
111setup_linux_rt_sigframe(struct trapframe *tf, const ksiginfo_t *ksi,
112    const sigset_t *mask)
113{
114	struct lwp *l = curlwp;
115	struct proc *p = l->l_proc;
116	struct linux_rt_sigframe *sfp, sigframe;
117	int onstack, error;
118	int fsize, rndfsize;
119	int sig = ksi->ksi_signo;
120	extern char linux_rt_sigcode[], linux_rt_esigcode[];
121
122	/* Do we need to jump onto the signal stack? */
123	onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
124		  (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
125
126	/* Allocate space for the signal handler context.  */
127	fsize = sizeof(struct linux_rt_sigframe);
128	rndfsize = ((fsize + 15) / 16) * 16;
129
130	if (onstack)
131		sfp = (struct linux_rt_sigframe *)
132		    ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
133	else
134		sfp = (struct linux_rt_sigframe *)(alpha_pal_rdusp());
135	sfp = (struct linux_rt_sigframe *)((char *)sfp - rndfsize);
136
137#ifdef DEBUG
138	if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
139		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
140		    sig, &onstack, sfp);
141#endif /* DEBUG */
142
143	/*
144	 * Build the signal context to be used by sigreturn.
145	 */
146	memset(&sigframe.uc, 0, sizeof(struct linux_ucontext));
147	sigframe.uc.uc_mcontext.sc_onstack = onstack;
148
149	/* Setup potentially partial signal mask in sc_mask. */
150	/* But get all of it in uc_sigmask */
151	native_to_linux_old_sigset(&sigframe.uc.uc_mcontext.sc_mask, mask);
152	native_to_linux_sigset(&sigframe.uc.uc_sigmask, mask);
153
154	sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC];
155	sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE;
156	frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs);
157	sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp();
158
159	fpu_load();
160	alpha_pal_wrfen(1);
161	sigframe.uc.uc_mcontext.sc_fpcr = alpha_read_fpcr();
162	sigframe.uc.uc_mcontext.sc_fp_control = alpha_read_fp_c(l);
163	alpha_pal_wrfen(0);
164
165	sigframe.uc.uc_mcontext.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
166	sigframe.uc.uc_mcontext.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
167	sigframe.uc.uc_mcontext.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
168	native_to_linux_siginfo(&sigframe.info, &ksi->ksi_info);
169	sendsig_reset(l, sig);
170	mutex_exit(p->p_lock);
171	error = copyout((void *)&sigframe, (void *)sfp, fsize);
172	mutex_enter(p->p_lock);
173
174	if (error != 0) {
175#ifdef DEBUG
176		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
177			printf("sendsig(%d): copyout failed on sig %d\n",
178			    p->p_pid, sig);
179#endif
180		/*
181		 * Process has trashed its stack; give it an illegal
182		 * instruction to halt it in its tracks.
183		 */
184		sigexit(l, SIGILL);
185		/* NOTREACHED */
186	}
187
188	/* Pass pointers to siginfo and ucontext in the regs */
189	tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info;
190	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc;
191
192	/* Address of trampoline code.  End up at this PC after mi_switch */
193	tf->tf_regs[FRAME_PC] =
194	    (u_int64_t)(p->p_psstrp - (linux_rt_esigcode - linux_rt_sigcode));
195
196	/* Adjust the stack */
197	alpha_pal_wrusp((unsigned long)sfp);
198
199	/* Remember that we're now on the signal stack. */
200	if (onstack)
201		l->l_sigstk.ss_flags |= SS_ONSTACK;
202}
203
204void setup_linux_sigframe(struct trapframe *tf, const ksiginfo_t *ksi,
205    const sigset_t *mask)
206{
207	struct lwp *l = curlwp;
208	struct proc *p = l->l_proc;
209	struct linux_sigframe *sfp, sigframe;
210	int onstack, error;
211	int fsize, rndfsize;
212	int sig = ksi->ksi_signo;
213	extern char linux_sigcode[], linux_esigcode[];
214
215	/* Do we need to jump onto the signal stack? */
216	onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
217		  (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
218
219	/* Allocate space for the signal handler context.  */
220	fsize = sizeof(struct linux_sigframe);
221	rndfsize = ((fsize + 15) / 16) * 16;
222
223	if (onstack)
224		sfp = (struct linux_sigframe *)
225		    ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
226	else
227		sfp = (struct linux_sigframe *)(alpha_pal_rdusp());
228	sfp = (struct linux_sigframe *)((char *)sfp - rndfsize);
229
230#ifdef DEBUG
231	if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid))
232		printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid,
233		    sig, &onstack, sfp);
234#endif /* DEBUG */
235
236	/*
237	 * Build the signal context to be used by sigreturn.
238	 */
239	memset(&sigframe.sf_sc, 0, sizeof(struct linux_sigcontext));
240	sigframe.sf_sc.sc_onstack = onstack;
241	native_to_linux_old_sigset(&sigframe.sf_sc.sc_mask, mask);
242	sigframe.sf_sc.sc_pc = tf->tf_regs[FRAME_PC];
243	sigframe.sf_sc.sc_ps = ALPHA_PSL_USERMODE;
244	frametoreg(tf, (struct reg *)sigframe.sf_sc.sc_regs);
245	sigframe.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp();
246
247	if (l == fpcurlwp) {
248		struct pcb *pcb = lwp_getpcb(l);
249
250		alpha_pal_wrfen(1);
251		savefpstate(&pcb->pcb_fp);
252		alpha_pal_wrfen(0);
253		sigframe.sf_sc.sc_fpcr = pcb->pcb_fp.fpr_cr;
254		fpcurlwp = NULL;
255	}
256	/* XXX ownedfp ? etc...? */
257
258	sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0];
259	sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1];
260	sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2];
261
262	sendsig_reset(l, sig);
263	mutex_exit(p->p_lock);
264	error = copyout((void *)&sigframe, (void *)sfp, fsize);
265	mutex_enter(p->p_lock);
266
267	if (error != 0) {
268#ifdef DEBUG
269		if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid)
270			printf("sendsig(%d): copyout failed on sig %d\n",
271			    p->p_pid, sig);
272#endif
273		/*
274		 * Process has trashed its stack; give it an illegal
275		 * instruction to halt it in its tracks.
276		 */
277		sigexit(l, SIGILL);
278		/* NOTREACHED */
279	}
280
281	/* Pass pointers to sigcontext in the regs */
282	tf->tf_regs[FRAME_A1] = 0;
283	tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc;
284
285	/* Address of trampoline code.  End up at this PC after mi_switch */
286	tf->tf_regs[FRAME_PC] =
287	    (u_int64_t)(p->p_psstrp - (linux_esigcode - linux_sigcode));
288
289	/* Adjust the stack */
290	alpha_pal_wrusp((unsigned long)sfp);
291
292	/* Remember that we're now on the signal stack. */
293	if (onstack)
294		l->l_sigstk.ss_flags |= SS_ONSTACK;
295}
296
297/*
298 * Send an interrupt to process.
299 *
300 * Stack is set up to allow sigcode stored
301 * in u. to call routine, followed by kcall
302 * to sigreturn routine below.  After sigreturn
303 * resets the signal mask, the stack, and the
304 * frame pointer, it returns to the user
305 * specified pc, psl.
306 */
307void
308linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
309{
310	struct lwp *l = curlwp;
311	struct proc *p = l->l_proc;
312	struct trapframe *tf = l->l_md.md_tf;
313	const int sig = ksi->ksi_signo;
314	sig_t catcher = SIGACTION(p, sig).sa_handler;
315#ifdef notyet
316	struct linux_emuldata *edp;
317
318	/* Setup the signal frame (and part of the trapframe) */
319	/*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/
320/*	XXX XAX this is broken now.  need someplace to store what
321	XXX XAX kind of signal handler a signal has.*/
322#if 0
323	edp = (struct linux_emuldata *)p->p_emuldata;
324#else
325	edp = 0;
326#endif
327	if (edp && sigismember(&edp->ps_siginfo, sig))
328		setup_linux_rt_sigframe(tf, ksi, mask);
329	else
330#endif /* notyet */
331		setup_linux_sigframe(tf, ksi, mask);
332
333	/* Signal handler for trampoline code */
334	tf->tf_regs[FRAME_T12] = (u_int64_t)catcher;
335	tf->tf_regs[FRAME_A0] = native_to_linux_signo[sig];
336
337	/*
338	 * Linux has a custom restorer option.  To support it we would
339	 * need to store an array of restorers and a sigcode block
340	 * which knew to use it.  Doesn't seem worth the trouble.
341	 * -erh
342	 */
343
344#ifdef DEBUG
345	if (sigdebug & SDB_FOLLOW)
346		printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid,
347		    tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]);
348	if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid)
349		printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig);
350#endif
351}
352
353/*
354 * System call to cleanup state after a signal
355 * has been taken.  Reset signal mask and
356 * stack state from context left by sendsig (above).
357 * Return to previous pc as specified by context
358 * left by sendsig.
359 * Linux real-time signals use a different sigframe,
360 * but the sigcontext is the same.
361 */
362
363int
364linux_restore_sigcontext(struct lwp *l, struct linux_sigcontext context,
365			 sigset_t *mask)
366{
367	struct proc *p = l->l_proc;
368	struct pcb *pcb;
369
370	/*
371	 * Linux doesn't (yet) have alternate signal stacks.
372	 * However, the OSF/1 sigcontext which they use has
373	 * an onstack member.  This could be needed in the future.
374	 */
375	mutex_enter(p->p_lock);
376	if (context.sc_onstack & LINUX_SA_ONSTACK)
377	    l->l_sigstk.ss_flags |= SS_ONSTACK;
378	else
379	    l->l_sigstk.ss_flags &= ~SS_ONSTACK;
380
381	/* Reset the signal mask */
382	(void) sigprocmask1(l, SIG_SETMASK, mask, 0);
383	mutex_exit(p->p_lock);
384
385	/*
386	 * Check for security violations.
387	 * Linux doesn't allow any changes to the PSL.
388	 */
389	if (context.sc_ps != ALPHA_PSL_USERMODE)
390	    return(EINVAL);
391
392	l->l_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc;
393	l->l_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps;
394
395	regtoframe((struct reg *)context.sc_regs, l->l_md.md_tf);
396	alpha_pal_wrusp(context.sc_regs[R_SP]);
397
398	if (l == fpcurlwp)
399	    fpcurlwp = NULL;
400
401	/* Restore fp regs and fpr_cr */
402	pcb = lwp_getpcb(l);
403	memcpy(&pcb->pcb_fp, (struct fpreg *)context.sc_fpregs,
404	    sizeof(struct fpreg));
405	/* XXX sc_ownedfp ? */
406	/* XXX sc_fp_control ? */
407
408#ifdef DEBUG
409	if (sigdebug & SDB_FOLLOW)
410		printf("linux_rt_sigreturn(%d): returns\n", p->p_pid);
411#endif
412	return (EJUSTRETURN);
413}
414
415int
416linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval)
417{
418	/* {
419		syscallarg(struct linux_rt_sigframe *) sfp;
420	} */
421	struct linux_rt_sigframe *sfp, sigframe;
422	sigset_t mask;
423
424	/*
425	 * The trampoline code hands us the context.
426	 * It is unsafe to keep track of it ourselves, in the event that a
427	 * program jumps out of a signal handler.
428	 */
429
430	sfp = SCARG(uap, sfp);
431
432	if (ALIGN(sfp) != (u_int64_t)sfp)
433		return(EINVAL);
434
435	/*
436	 * Fetch the frame structure.
437	 */
438	if (copyin((void *)sfp, &sigframe,
439			sizeof(struct linux_rt_sigframe)) != 0)
440		return (EFAULT);
441
442	/* Grab the signal mask */
443	linux_to_native_sigset(&mask, &sigframe.uc.uc_sigmask);
444
445	return(linux_restore_sigcontext(l, sigframe.uc.uc_mcontext, &mask));
446}
447
448
449int
450linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval)
451{
452	/* {
453		syscallarg(struct linux_sigframe *) sfp;
454	} */
455	struct linux_sigframe *sfp, frame;
456	sigset_t mask;
457
458	/*
459	 * The trampoline code hands us the context.
460	 * It is unsafe to keep track of it ourselves, in the event that a
461	 * program jumps out of a signal handler.
462	 */
463
464	sfp = SCARG(uap, sfp);
465	if (ALIGN(sfp) != (u_int64_t)sfp)
466		return(EINVAL);
467
468	/*
469	 * Fetch the frame structure.
470	 */
471	if (copyin((void *)sfp, &frame, sizeof(struct linux_sigframe)) != 0)
472		return(EFAULT);
473
474	/* Grab the signal mask. */
475	/* XXX use frame.extramask */
476	linux_old_to_native_sigset(&mask, frame.sf_sc.sc_mask);
477
478	return(linux_restore_sigcontext(l, frame.sf_sc, &mask));
479}
480
481/*
482 * We come here in a last attempt to satisfy a Linux ioctl() call
483 */
484/* XXX XAX update this, add maps, etc... */
485int
486linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
487{
488	/* {
489		syscallarg(int) fd;
490		syscallarg(u_long) com;
491		syscallarg(void *) data;
492	} */
493	struct sys_ioctl_args bia;
494	u_long com;
495
496	SCARG(&bia, fd) = SCARG(uap, fd);
497	SCARG(&bia, data) = SCARG(uap, data);
498	com = SCARG(uap, com);
499
500	switch (com) {
501	default:
502		printf("linux_machdepioctl: invalid ioctl %08lx\n", com);
503		return EINVAL;
504	}
505	SCARG(&bia, com) = com;
506	return sys_ioctl(l, &bia, retval);
507}
508
509/* XXX XAX fix this */
510dev_t
511linux_fakedev(dev_t dev, int raw)
512{
513	return dev;
514}
515
516int
517linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg)
518{
519	return 0;
520}
521