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