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