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