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