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