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