1 /* $NetBSD: linux_machdep.c,v 1.44 2009/03/18 17:06:48 cegger 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.44 2009/03/18 17:06:48 cegger 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/user.h> 46 #include <sys/buf.h> 47 #include <sys/reboot.h> 48 #include <sys/conf.h> 49 #include <sys/exec.h> 50 #include <sys/file.h> 51 #include <sys/callout.h> 52 #include <sys/malloc.h> 53 #include <sys/mbuf.h> 54 #include <sys/msgbuf.h> 55 #include <sys/mount.h> 56 #include <sys/vnode.h> 57 #include <sys/device.h> 58 #include <sys/syscallargs.h> 59 #include <sys/filedesc.h> 60 #include <sys/exec_elf.h> 61 #include <sys/ioctl.h> 62 #include <sys/kauth.h> 63 64 #include <uvm/uvm_extern.h> 65 66 #include <compat/linux/common/linux_types.h> 67 #include <compat/linux/common/linux_signal.h> 68 #include <compat/linux/common/linux_siginfo.h> 69 #include <compat/linux/common/linux_util.h> 70 #include <compat/linux/common/linux_ioctl.h> 71 #include <compat/linux/common/linux_exec.h> 72 #include <compat/linux/common/linux_machdep.h> 73 #include <compat/linux/common/linux_emuldata.h> 74 75 #include <compat/linux/linux_syscallargs.h> 76 77 #include <machine/alpha.h> 78 #include <machine/reg.h> 79 80 #if defined(_KERNEL_OPT) 81 #include "wsdisplay.h" 82 #endif 83 #if (NWSDISPLAY >0) 84 #include <dev/wscons/wsdisplay_usl_io.h> 85 #endif 86 #ifdef DEBUG 87 #include <machine/sigdebug.h> 88 #endif 89 90 /* 91 * Deal with some alpha-specific things in the Linux emulation code. 92 */ 93 94 void 95 linux_setregs(struct lwp *l, struct exec_package *epp, u_long stack) 96 { 97 #ifdef DEBUG 98 struct trapframe *tfp = l->l_md.md_tf; 99 #endif 100 101 setregs(l, epp, stack); 102 #ifdef DEBUG 103 /* 104 * Linux has registers set to zero on entry; for DEBUG kernels 105 * the alpha setregs() fills registers with 0xbabefacedeadbeef. 106 */ 107 memset(tfp->tf_regs, 0, FRAME_SIZE * sizeof tfp->tf_regs[0]); 108 #endif 109 } 110 111 void 112 setup_linux_rt_sigframe(struct trapframe *tf, int sig, const sigset_t *mask) 113 { 114 struct lwp *l = curlwp; 115 struct proc *p = l->l_proc; 116 struct linux_rt_sigframe *sfp, sigframe; 117 int onstack, error; 118 int fsize, rndfsize; 119 extern char linux_rt_sigcode[], linux_rt_esigcode[]; 120 121 /* Do we need to jump onto the signal stack? */ 122 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 123 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 124 125 /* Allocate space for the signal handler context. */ 126 fsize = sizeof(struct linux_rt_sigframe); 127 rndfsize = ((fsize + 15) / 16) * 16; 128 129 if (onstack) 130 sfp = (struct linux_rt_sigframe *) 131 ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size); 132 else 133 sfp = (struct linux_rt_sigframe *)(alpha_pal_rdusp()); 134 sfp = (struct linux_rt_sigframe *)((char *)sfp - rndfsize); 135 136 #ifdef DEBUG 137 if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid)) 138 printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid, 139 sig, &onstack, sfp); 140 #endif /* DEBUG */ 141 142 /* 143 * Build the signal context to be used by sigreturn. 144 */ 145 memset(&sigframe.uc, 0, sizeof(struct linux_ucontext)); 146 sigframe.uc.uc_mcontext.sc_onstack = onstack; 147 148 /* Setup potentially partial signal mask in sc_mask. */ 149 /* But get all of it in uc_sigmask */ 150 native_to_linux_old_sigset(&sigframe.uc.uc_mcontext.sc_mask, mask); 151 native_to_linux_sigset(&sigframe.uc.uc_sigmask, mask); 152 153 sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC]; 154 sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE; 155 frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs); 156 sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp(); 157 158 alpha_enable_fp(l, 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_psstr - (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 alpha_pal_wrfen(1); 260 savefpstate(&l->l_addr->u_pcb.pcb_fp); 261 alpha_pal_wrfen(0); 262 sigframe.sf_sc.sc_fpcr = l->l_addr->u_pcb.pcb_fp.fpr_cr; 263 fpcurlwp = NULL; 264 } 265 /* XXX ownedfp ? etc...? */ 266 267 sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0]; 268 sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1]; 269 sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2]; 270 271 sendsig_reset(l, sig); 272 mutex_exit(p->p_lock); 273 error = copyout((void *)&sigframe, (void *)sfp, fsize); 274 mutex_enter(p->p_lock); 275 276 if (error != 0) { 277 #ifdef DEBUG 278 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 279 printf("sendsig(%d): copyout failed on sig %d\n", 280 p->p_pid, sig); 281 #endif 282 /* 283 * Process has trashed its stack; give it an illegal 284 * instruction to halt it in its tracks. 285 */ 286 sigexit(l, SIGILL); 287 /* NOTREACHED */ 288 } 289 290 /* Pass pointers to sigcontext in the regs */ 291 tf->tf_regs[FRAME_A1] = 0; 292 tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc; 293 294 /* Address of trampoline code. End up at this PC after mi_switch */ 295 tf->tf_regs[FRAME_PC] = 296 (u_int64_t)(p->p_psstr - (linux_esigcode - linux_sigcode)); 297 298 /* Adjust the stack */ 299 alpha_pal_wrusp((unsigned long)sfp); 300 301 /* Remember that we're now on the signal stack. */ 302 if (onstack) 303 l->l_sigstk.ss_flags |= SS_ONSTACK; 304 } 305 306 /* 307 * Send an interrupt to process. 308 * 309 * Stack is set up to allow sigcode stored 310 * in u. to call routine, followed by kcall 311 * to sigreturn routine below. After sigreturn 312 * resets the signal mask, the stack, and the 313 * frame pointer, it returns to the user 314 * specified pc, psl. 315 */ 316 void 317 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 318 { 319 struct lwp *l = curlwp; 320 struct proc *p = l->l_proc; 321 struct trapframe *tf = l->l_md.md_tf; 322 const int sig = ksi->ksi_signo; 323 sig_t catcher = SIGACTION(p, sig).sa_handler; 324 #ifdef notyet 325 struct linux_emuldata *edp; 326 327 /* Setup the signal frame (and part of the trapframe) */ 328 /*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/ 329 /* XXX XAX this is broken now. need someplace to store what 330 XXX XAX kind of signal handler a signal has.*/ 331 #if 0 332 edp = (struct linux_emuldata *)p->p_emuldata; 333 #else 334 edp = 0; 335 #endif 336 if (edp && sigismember(&edp->ps_siginfo, sig)) 337 setup_linux_rt_sigframe(tf, sig, mask); 338 else 339 #endif /* notyet */ 340 setup_linux_sigframe(tf, sig, mask); 341 342 /* Signal handler for trampoline code */ 343 tf->tf_regs[FRAME_T12] = (u_int64_t)catcher; 344 tf->tf_regs[FRAME_A0] = native_to_linux_signo[sig]; 345 346 /* 347 * Linux has a custom restorer option. To support it we would 348 * need to store an array of restorers and a sigcode block 349 * which knew to use it. Doesn't seem worth the trouble. 350 * -erh 351 */ 352 353 #ifdef DEBUG 354 if (sigdebug & SDB_FOLLOW) 355 printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid, 356 tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]); 357 if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid) 358 printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig); 359 #endif 360 } 361 362 /* 363 * System call to cleanup state after a signal 364 * has been taken. Reset signal mask and 365 * stack state from context left by sendsig (above). 366 * Return to previous pc as specified by context 367 * left by sendsig. 368 * Linux real-time signals use a different sigframe, 369 * but the sigcontext is the same. 370 */ 371 372 int 373 linux_restore_sigcontext(struct lwp *l, struct linux_sigcontext context, 374 sigset_t *mask) 375 { 376 struct proc *p = l->l_proc; 377 378 /* 379 * Linux doesn't (yet) have alternate signal stacks. 380 * However, the OSF/1 sigcontext which they use has 381 * an onstack member. This could be needed in the future. 382 */ 383 mutex_enter(p->p_lock); 384 if (context.sc_onstack & LINUX_SA_ONSTACK) 385 l->l_sigstk.ss_flags |= SS_ONSTACK; 386 else 387 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 388 389 /* Reset the signal mask */ 390 (void) sigprocmask1(l, SIG_SETMASK, mask, 0); 391 mutex_exit(p->p_lock); 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 l->l_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc; 401 l->l_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps; 402 403 regtoframe((struct reg *)context.sc_regs, l->l_md.md_tf); 404 alpha_pal_wrusp(context.sc_regs[R_SP]); 405 406 if (l == fpcurlwp) 407 fpcurlwp = NULL; 408 409 /* Restore fp regs and fpr_cr */ 410 memcpy( &l->l_addr->u_pcb.pcb_fp, (struct fpreg *)context.sc_fpregs, 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(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval) 424 { 425 /* { 426 syscallarg(struct linux_rt_sigframe *) sfp; 427 } */ 428 struct linux_rt_sigframe *sfp, sigframe; 429 sigset_t mask; 430 431 /* 432 * The trampoline code hands us the context. 433 * It is unsafe to keep track of it ourselves, in the event that a 434 * program jumps out of a signal handler. 435 */ 436 437 sfp = SCARG(uap, sfp); 438 439 if (ALIGN(sfp) != (u_int64_t)sfp) 440 return(EINVAL); 441 442 /* 443 * Fetch the frame structure. 444 */ 445 if (copyin((void *)sfp, &sigframe, 446 sizeof(struct linux_rt_sigframe)) != 0) 447 return (EFAULT); 448 449 /* Grab the signal mask */ 450 linux_to_native_sigset(&mask, &sigframe.uc.uc_sigmask); 451 452 return(linux_restore_sigcontext(l, sigframe.uc.uc_mcontext, &mask)); 453 } 454 455 456 int 457 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval) 458 { 459 /* { 460 syscallarg(struct linux_sigframe *) sfp; 461 } */ 462 struct linux_sigframe *sfp, frame; 463 sigset_t mask; 464 465 /* 466 * The trampoline code hands us the context. 467 * It is unsafe to keep track of it ourselves, in the event that a 468 * program jumps out of a signal handler. 469 */ 470 471 sfp = SCARG(uap, sfp); 472 if (ALIGN(sfp) != (u_int64_t)sfp) 473 return(EINVAL); 474 475 /* 476 * Fetch the frame structure. 477 */ 478 if (copyin((void *)sfp, &frame, sizeof(struct linux_sigframe)) != 0) 479 return(EFAULT); 480 481 /* Grab the signal mask. */ 482 /* XXX use frame.extramask */ 483 linux_old_to_native_sigset(&mask, frame.sf_sc.sc_mask); 484 485 return(linux_restore_sigcontext(l, frame.sf_sc, &mask)); 486 } 487 488 /* 489 * We come here in a last attempt to satisfy a Linux ioctl() call 490 */ 491 /* XXX XAX update this, add maps, etc... */ 492 int 493 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval) 494 { 495 /* { 496 syscallarg(int) fd; 497 syscallarg(u_long) com; 498 syscallarg(void *) data; 499 } */ 500 struct sys_ioctl_args bia; 501 u_long com; 502 503 SCARG(&bia, fd) = SCARG(uap, fd); 504 SCARG(&bia, data) = SCARG(uap, data); 505 com = SCARG(uap, com); 506 507 switch (com) { 508 default: 509 printf("linux_machdepioctl: invalid ioctl %08lx\n", com); 510 return EINVAL; 511 } 512 SCARG(&bia, com) = com; 513 return sys_ioctl(l, &bia, retval); 514 } 515 516 /* XXX XAX fix this */ 517 dev_t 518 linux_fakedev(dev_t dev, int raw) 519 { 520 return dev; 521 } 522 523 int 524 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg) 525 { 526 return 0; 527 } 528