1 /* $NetBSD: linux_machdep.c,v 1.52 2021/09/07 11:43:03 riastradh 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.52 2021/09/07 11:43:03 riastradh 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 memset(&sigframe, 0, sizeof(sigframe)); 143 144 /* 145 * Build the signal context to be used by sigreturn. 146 */ 147 memset(&sigframe.uc, 0, sizeof(struct linux_ucontext)); 148 sigframe.uc.uc_mcontext.sc_onstack = onstack; 149 150 /* Setup potentially partial signal mask in sc_mask. */ 151 /* But get all of it in uc_sigmask */ 152 native_to_linux_old_sigset(&sigframe.uc.uc_mcontext.sc_mask, mask); 153 native_to_linux_sigset(&sigframe.uc.uc_sigmask, mask); 154 155 sigframe.uc.uc_mcontext.sc_pc = tf->tf_regs[FRAME_PC]; 156 sigframe.uc.uc_mcontext.sc_ps = ALPHA_PSL_USERMODE; 157 frametoreg(tf, (struct reg *)sigframe.uc.uc_mcontext.sc_regs); 158 sigframe.uc.uc_mcontext.sc_regs[R_SP] = alpha_pal_rdusp(); 159 160 fpu_load(); 161 alpha_pal_wrfen(1); 162 sigframe.uc.uc_mcontext.sc_fpcr = alpha_read_fpcr(); 163 sigframe.uc.uc_mcontext.sc_fp_control = alpha_read_fp_c(l); 164 alpha_pal_wrfen(0); 165 166 sigframe.uc.uc_mcontext.sc_traparg_a0 = tf->tf_regs[FRAME_A0]; 167 sigframe.uc.uc_mcontext.sc_traparg_a1 = tf->tf_regs[FRAME_A1]; 168 sigframe.uc.uc_mcontext.sc_traparg_a2 = tf->tf_regs[FRAME_A2]; 169 native_to_linux_siginfo(&sigframe.info, &ksi->ksi_info); 170 sendsig_reset(l, sig); 171 mutex_exit(p->p_lock); 172 error = copyout((void *)&sigframe, (void *)sfp, fsize); 173 mutex_enter(p->p_lock); 174 175 if (error != 0) { 176 #ifdef DEBUG 177 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 178 printf("sendsig(%d): copyout failed on sig %d\n", 179 p->p_pid, sig); 180 #endif 181 /* 182 * Process has trashed its stack; give it an illegal 183 * instruction to halt it in its tracks. 184 */ 185 sigexit(l, SIGILL); 186 /* NOTREACHED */ 187 } 188 189 /* Pass pointers to siginfo and ucontext in the regs */ 190 tf->tf_regs[FRAME_A1] = (unsigned long)&sfp->info; 191 tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->uc; 192 193 /* Address of trampoline code. End up at this PC after mi_switch */ 194 tf->tf_regs[FRAME_PC] = 195 (u_int64_t)(p->p_psstrp - (linux_rt_esigcode - linux_rt_sigcode)); 196 197 /* Adjust the stack */ 198 alpha_pal_wrusp((unsigned long)sfp); 199 200 /* Remember that we're now on the signal stack. */ 201 if (onstack) 202 l->l_sigstk.ss_flags |= SS_ONSTACK; 203 } 204 205 void setup_linux_sigframe(struct trapframe *tf, const ksiginfo_t *ksi, 206 const sigset_t *mask) 207 { 208 struct lwp *l = curlwp; 209 struct proc *p = l->l_proc; 210 struct linux_sigframe *sfp, sigframe; 211 int onstack, error; 212 int fsize, rndfsize; 213 int sig = ksi->ksi_signo; 214 extern char linux_sigcode[], linux_esigcode[]; 215 216 /* Do we need to jump onto the signal stack? */ 217 onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 && 218 (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 219 220 /* Allocate space for the signal handler context. */ 221 fsize = sizeof(struct linux_sigframe); 222 rndfsize = ((fsize + 15) / 16) * 16; 223 224 if (onstack) 225 sfp = (struct linux_sigframe *) 226 ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size); 227 else 228 sfp = (struct linux_sigframe *)(alpha_pal_rdusp()); 229 sfp = (struct linux_sigframe *)((char *)sfp - rndfsize); 230 231 #ifdef DEBUG 232 if ((sigdebug & SDB_KSTACK) && (p->p_pid == sigpid)) 233 printf("linux_sendsig(%d): sig %d ssp %p usp %p\n", p->p_pid, 234 sig, &onstack, sfp); 235 #endif /* DEBUG */ 236 237 memset(&sigframe, 0, sizeof(sigframe)); 238 239 /* 240 * Build the signal context to be used by sigreturn. 241 */ 242 memset(&sigframe.sf_sc, 0, sizeof(struct linux_sigcontext)); 243 sigframe.sf_sc.sc_onstack = onstack; 244 native_to_linux_old_sigset(&sigframe.sf_sc.sc_mask, mask); 245 sigframe.sf_sc.sc_pc = tf->tf_regs[FRAME_PC]; 246 sigframe.sf_sc.sc_ps = ALPHA_PSL_USERMODE; 247 frametoreg(tf, (struct reg *)sigframe.sf_sc.sc_regs); 248 sigframe.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp(); 249 250 if (fpu_valid_p(l)) { 251 struct pcb *pcb = lwp_getpcb(l); 252 253 fpu_save(l); 254 sigframe.sf_sc.sc_fpcr = pcb->pcb_fp.fpr_cr; 255 } 256 /* XXX ownedfp ? etc...? */ 257 258 sigframe.sf_sc.sc_traparg_a0 = tf->tf_regs[FRAME_A0]; 259 sigframe.sf_sc.sc_traparg_a1 = tf->tf_regs[FRAME_A1]; 260 sigframe.sf_sc.sc_traparg_a2 = tf->tf_regs[FRAME_A2]; 261 262 sendsig_reset(l, sig); 263 mutex_exit(p->p_lock); 264 error = copyout((void *)&sigframe, (void *)sfp, fsize); 265 mutex_enter(p->p_lock); 266 267 if (error != 0) { 268 #ifdef DEBUG 269 if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 270 printf("sendsig(%d): copyout failed on sig %d\n", 271 p->p_pid, sig); 272 #endif 273 /* 274 * Process has trashed its stack; give it an illegal 275 * instruction to halt it in its tracks. 276 */ 277 sigexit(l, SIGILL); 278 /* NOTREACHED */ 279 } 280 281 /* Pass pointers to sigcontext in the regs */ 282 tf->tf_regs[FRAME_A1] = 0; 283 tf->tf_regs[FRAME_A2] = (unsigned long)&sfp->sf_sc; 284 285 /* Address of trampoline code. End up at this PC after mi_switch */ 286 tf->tf_regs[FRAME_PC] = 287 (u_int64_t)(p->p_psstrp - (linux_esigcode - linux_sigcode)); 288 289 /* Adjust the stack */ 290 alpha_pal_wrusp((unsigned long)sfp); 291 292 /* Remember that we're now on the signal stack. */ 293 if (onstack) 294 l->l_sigstk.ss_flags |= SS_ONSTACK; 295 } 296 297 /* 298 * Send an interrupt to process. 299 * 300 * Stack is set up to allow sigcode stored 301 * in u. to call routine, followed by kcall 302 * to sigreturn routine below. After sigreturn 303 * resets the signal mask, the stack, and the 304 * frame pointer, it returns to the user 305 * specified pc, psl. 306 */ 307 void 308 linux_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 309 { 310 struct lwp *l = curlwp; 311 struct proc *p = l->l_proc; 312 struct trapframe *tf = l->l_md.md_tf; 313 const int sig = ksi->ksi_signo; 314 sig_t catcher = SIGACTION(p, sig).sa_handler; 315 #ifdef notyet 316 struct linux_emuldata *edp; 317 318 /* Setup the signal frame (and part of the trapframe) */ 319 /*OLD: if (p->p_sigacts->ps_siginfo & sigmask(sig))*/ 320 /* XXX XAX this is broken now. need someplace to store what 321 XXX XAX kind of signal handler a signal has.*/ 322 #if 0 323 edp = (struct linux_emuldata *)p->p_emuldata; 324 #else 325 edp = 0; 326 #endif 327 if (edp && sigismember(&edp->ps_siginfo, sig)) 328 setup_linux_rt_sigframe(tf, ksi, mask); 329 else 330 #endif /* notyet */ 331 setup_linux_sigframe(tf, ksi, mask); 332 333 /* Signal handler for trampoline code */ 334 tf->tf_regs[FRAME_T12] = (u_int64_t)catcher; 335 tf->tf_regs[FRAME_A0] = native_to_linux_signo[sig]; 336 337 /* 338 * Linux has a custom restorer option. To support it we would 339 * need to store an array of restorers and a sigcode block 340 * which knew to use it. Doesn't seem worth the trouble. 341 * -erh 342 */ 343 344 #ifdef DEBUG 345 if (sigdebug & SDB_FOLLOW) 346 printf("sendsig(%d): pc %lx, catcher %lx\n", l->l_proc->p_pid, 347 tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]); 348 if ((sigdebug & SDB_KSTACK) && l->l_proc->p_pid == sigpid) 349 printf("sendsig(%d): sig %d returns\n", l->l_proc->p_pid, sig); 350 #endif 351 } 352 353 /* 354 * System call to cleanup state after a signal 355 * has been taken. Reset signal mask and 356 * stack state from context left by sendsig (above). 357 * Return to previous pc as specified by context 358 * left by sendsig. 359 * Linux real-time signals use a different sigframe, 360 * but the sigcontext is the same. 361 */ 362 363 int 364 linux_restore_sigcontext(struct lwp *l, struct linux_sigcontext context, 365 sigset_t *mask) 366 { 367 struct proc *p = l->l_proc; 368 struct pcb *pcb; 369 370 /* 371 * Linux doesn't (yet) have alternate signal stacks. 372 * However, the OSF/1 sigcontext which they use has 373 * an onstack member. This could be needed in the future. 374 */ 375 mutex_enter(p->p_lock); 376 if (context.sc_onstack & LINUX_SA_ONSTACK) 377 l->l_sigstk.ss_flags |= SS_ONSTACK; 378 else 379 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 380 381 /* Reset the signal mask */ 382 (void) sigprocmask1(l, SIG_SETMASK, mask, 0); 383 mutex_exit(p->p_lock); 384 385 /* 386 * Check for security violations. 387 * Linux doesn't allow any changes to the PSL. 388 */ 389 if (context.sc_ps != ALPHA_PSL_USERMODE) 390 return(EINVAL); 391 392 l->l_md.md_tf->tf_regs[FRAME_PC] = context.sc_pc; 393 l->l_md.md_tf->tf_regs[FRAME_PS] = context.sc_ps; 394 395 regtoframe((struct reg *)context.sc_regs, l->l_md.md_tf); 396 alpha_pal_wrusp(context.sc_regs[R_SP]); 397 398 /* Restore fp regs and fpr_cr */ 399 pcb = lwp_getpcb(l); 400 memcpy(&pcb->pcb_fp, (struct fpreg *)context.sc_fpregs, 401 sizeof(struct fpreg)); 402 /* XXX sc_ownedfp ? */ 403 /* XXX sc_fp_control ? */ 404 405 #ifdef DEBUG 406 if (sigdebug & SDB_FOLLOW) 407 printf("linux_rt_sigreturn(%d): returns\n", p->p_pid); 408 #endif 409 return (EJUSTRETURN); 410 } 411 412 int 413 linux_sys_rt_sigreturn(struct lwp *l, const struct linux_sys_rt_sigreturn_args *uap, register_t *retval) 414 { 415 /* { 416 syscallarg(struct linux_rt_sigframe *) sfp; 417 } */ 418 struct linux_rt_sigframe *sfp, sigframe; 419 sigset_t mask; 420 421 /* 422 * The trampoline code hands us the context. 423 * It is unsafe to keep track of it ourselves, in the event that a 424 * program jumps out of a signal handler. 425 */ 426 427 sfp = SCARG(uap, sfp); 428 429 if (ALIGN(sfp) != (u_int64_t)sfp) 430 return(EINVAL); 431 432 /* 433 * Fetch the frame structure. 434 */ 435 if (copyin((void *)sfp, &sigframe, 436 sizeof(struct linux_rt_sigframe)) != 0) 437 return (EFAULT); 438 439 /* Grab the signal mask */ 440 linux_to_native_sigset(&mask, &sigframe.uc.uc_sigmask); 441 442 return(linux_restore_sigcontext(l, sigframe.uc.uc_mcontext, &mask)); 443 } 444 445 446 int 447 linux_sys_sigreturn(struct lwp *l, const struct linux_sys_sigreturn_args *uap, register_t *retval) 448 { 449 /* { 450 syscallarg(struct linux_sigframe *) sfp; 451 } */ 452 struct linux_sigframe *sfp, frame; 453 sigset_t mask; 454 455 /* 456 * The trampoline code hands us the context. 457 * It is unsafe to keep track of it ourselves, in the event that a 458 * program jumps out of a signal handler. 459 */ 460 461 sfp = SCARG(uap, sfp); 462 if (ALIGN(sfp) != (u_int64_t)sfp) 463 return(EINVAL); 464 465 /* 466 * Fetch the frame structure. 467 */ 468 if (copyin((void *)sfp, &frame, sizeof(struct linux_sigframe)) != 0) 469 return(EFAULT); 470 471 /* Grab the signal mask. */ 472 /* XXX use frame.extramask */ 473 linux_old_to_native_sigset(&mask, frame.sf_sc.sc_mask); 474 475 return(linux_restore_sigcontext(l, frame.sf_sc, &mask)); 476 } 477 478 /* 479 * We come here in a last attempt to satisfy a Linux ioctl() call 480 */ 481 /* XXX XAX update this, add maps, etc... */ 482 int 483 linux_machdepioctl(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval) 484 { 485 /* { 486 syscallarg(int) fd; 487 syscallarg(u_long) com; 488 syscallarg(void *) data; 489 } */ 490 struct sys_ioctl_args bia; 491 u_long com; 492 493 SCARG(&bia, fd) = SCARG(uap, fd); 494 SCARG(&bia, data) = SCARG(uap, data); 495 com = SCARG(uap, com); 496 497 switch (com) { 498 default: 499 printf("linux_machdepioctl: invalid ioctl %08lx\n", com); 500 return EINVAL; 501 } 502 SCARG(&bia, com) = com; 503 return sys_ioctl(l, &bia, retval); 504 } 505 506 /* XXX XAX fix this */ 507 dev_t 508 linux_fakedev(dev_t dev, int raw) 509 { 510 return dev; 511 } 512 513 int 514 linux_usertrap(struct lwp *l, vaddr_t trapaddr, void *arg) 515 { 516 return 0; 517 } 518