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