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