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