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