11.24Sriastrad/* $NetBSD: compat_16_machdep.c,v 1.24 2025/04/25 00:59:26 riastradh Exp $ */ 21.1Sskd 31.1Sskd/*- 41.1Sskd * Copyright (c) 2003 The NetBSD Foundation, Inc. 51.1Sskd * All rights reserved. 61.1Sskd * 71.1Sskd * This code is derived from software contributed to The NetBSD Foundation 81.1Sskd * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 91.1Sskd * NASA Ames Research Center and by Chris G. Demetriou. 101.1Sskd * 111.1Sskd * Redistribution and use in source and binary forms, with or without 121.1Sskd * modification, are permitted provided that the following conditions 131.1Sskd * are met: 141.1Sskd * 1. Redistributions of source code must retain the above copyright 151.1Sskd * notice, this list of conditions and the following disclaimer. 161.1Sskd * 2. Redistributions in binary form must reproduce the above copyright 171.1Sskd * notice, this list of conditions and the following disclaimer in the 181.1Sskd * documentation and/or other materials provided with the distribution. 191.1Sskd * 201.1Sskd * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 211.1Sskd * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 221.1Sskd * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 231.1Sskd * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 241.1Sskd * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 251.1Sskd * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 261.1Sskd * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 271.1Sskd * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 281.1Sskd * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 291.1Sskd * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 301.1Sskd * POSSIBILITY OF SUCH DAMAGE. 311.1Sskd */ 321.1Sskd 331.1Sskd/* 341.1Sskd * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University. 351.1Sskd * All rights reserved. 361.1Sskd * 371.1Sskd * Author: Chris G. Demetriou 381.18Smatt * 391.1Sskd * Permission to use, copy, modify and distribute this software and 401.1Sskd * its documentation is hereby granted, provided that both the copyright 411.1Sskd * notice and this permission notice appear in all copies of the 421.1Sskd * software, derivative works or modified versions, and any portions 431.1Sskd * thereof, and that both notices appear in supporting documentation. 441.18Smatt * 451.18Smatt * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 461.18Smatt * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 471.1Sskd * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 481.18Smatt * 491.1Sskd * Carnegie Mellon requests users of this software to return to 501.1Sskd * 511.1Sskd * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 521.1Sskd * School of Computer Science 531.1Sskd * Carnegie Mellon University 541.1Sskd * Pittsburgh PA 15213-3890 551.1Sskd * 561.1Sskd * any improvements or extensions that they make and grant Carnegie the 571.1Sskd * rights to redistribute these changes. 581.1Sskd */ 591.1Sskd 601.15She#ifdef _KERNEL_OPT 611.1Sskd#include "opt_ddb.h" 621.1Sskd#include "opt_kgdb.h" 631.1Sskd#include "opt_multiprocessor.h" 641.1Sskd#include "opt_dec_3000_300.h" 651.1Sskd#include "opt_dec_3000_500.h" 661.1Sskd#include "opt_compat_netbsd.h" 671.1Sskd#include "opt_execfmt.h" 681.15She#endif /* _KERNEL_OPT */ 691.1Sskd 701.1Sskd#include <sys/cdefs.h> /* RCS ID & Copyright macro defns */ 711.1Sskd#include <sys/types.h> 721.1Sskd#include <sys/param.h> 731.1Sskd#include <sys/signal.h> 741.1Sskd#include <sys/mount.h> 751.1Sskd#include <sys/proc.h> 761.1Sskd#include <sys/systm.h> 771.1Sskd#include <sys/syscall.h> 781.1Sskd#include <sys/syscallargs.h> 791.1Sskd 801.22Smaxv#if defined(COMPAT_13) 811.7Smartin#include <compat/sys/signal.h> 821.7Smartin#include <compat/sys/signalvar.h> 831.8She#endif 841.7Smartin 851.1Sskd#include <machine/cpu.h> 861.1Sskd#include <machine/reg.h> 871.1Sskd 881.24Sriastrad__KERNEL_RCSID(0, "$NetBSD: compat_16_machdep.c,v 1.24 2025/04/25 00:59:26 riastradh Exp $"); 891.1Sskd 901.1Sskd 911.1Sskd#ifdef DEBUG 921.1Sskd#include <machine/sigdebug.h> 931.1Sskd#endif 941.1Sskd 951.1Sskd#include <machine/alpha.h> 961.1Sskd 971.15She#include <sys/ksyms.h> 981.1Sskd 991.1Sskd/* 1001.1Sskd * Send an interrupt to process, old style 1011.1Sskd */ 1021.1Sskdvoid 1031.1Sskdsendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask) 1041.1Sskd{ 1051.1Sskd struct lwp *l = curlwp; 1061.1Sskd struct proc *p = l->l_proc; 1071.16Srmind struct pcb *pcb = lwp_getpcb(l); 1081.1Sskd struct sigacts *ps = p->p_sigacts; 1091.10Sad int onstack, sig = ksi->ksi_signo, error; 1101.1Sskd struct sigframe_sigcontext *fp, frame; 1111.1Sskd struct trapframe *tf; 1121.1Sskd sig_t catcher = SIGACTION(p, sig).sa_handler; 1131.1Sskd 1141.1Sskd tf = l->l_md.md_tf; 1151.24Sriastrad 1161.24Sriastrad /* Allocate space for the signal handler context. */ 1171.24Sriastrad fp = getframe(l, sig, &onstack, sizeof(*fp), _Alignof(*fp)); 1181.1Sskd 1191.1Sskd#ifdef DEBUG 1201.1Sskd if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 1211.1Sskd printf("sendsig_sigcontext(%d): sig %d ssp %p usp %p\n", 1221.3Snathanw p->p_pid, sig, &onstack, fp); 1231.1Sskd#endif 1241.1Sskd 1251.1Sskd /* Build stack frame for signal trampoline. */ 1261.1Sskd frame.sf_sc.sc_pc = tf->tf_regs[FRAME_PC]; 1271.1Sskd frame.sf_sc.sc_ps = tf->tf_regs[FRAME_PS]; 1281.1Sskd 1291.1Sskd /* Save register context. */ 1301.1Sskd frametoreg(tf, (struct reg *)frame.sf_sc.sc_regs); 1311.1Sskd frame.sf_sc.sc_regs[R_ZERO] = 0xACEDBADE; /* magic number */ 1321.1Sskd frame.sf_sc.sc_regs[R_SP] = alpha_pal_rdusp(); 1331.1Sskd 1341.1Sskd /* save the floating-point state, if necessary, then copy it. */ 1351.21Schs fpu_save(l); 1361.19Smatt frame.sf_sc.sc_ownedfp = fpu_valid_p(l); 1371.16Srmind memcpy((struct fpreg *)frame.sf_sc.sc_fpregs, &pcb->pcb_fp, 1381.1Sskd sizeof(struct fpreg)); 1391.1Sskd frame.sf_sc.sc_fp_control = alpha_read_fp_c(l); 1401.1Sskd memset(frame.sf_sc.sc_reserved, 0, sizeof frame.sf_sc.sc_reserved); 1411.1Sskd memset(frame.sf_sc.sc_xxx, 0, sizeof frame.sf_sc.sc_xxx); /* XXX */ 1421.1Sskd 1431.1Sskd /* Save signal stack. */ 1441.10Sad frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; 1451.1Sskd 1461.1Sskd /* Save signal mask. */ 1471.1Sskd frame.sf_sc.sc_mask = *mask; 1481.1Sskd 1491.22Smaxv#if defined(COMPAT_13) 1501.1Sskd /* 1511.1Sskd * XXX We always have to save an old style signal mask because 1521.1Sskd * XXX we might be delivering a signal to a process which will 1531.1Sskd * XXX escape from the signal in a non-standard way and invoke 1541.1Sskd * XXX sigreturn() directly. 1551.1Sskd */ 1561.1Sskd { 1571.1Sskd /* Note: it's a long in the stack frame. */ 1581.1Sskd sigset13_t mask13; 1591.1Sskd 1601.1Sskd native_sigset_to_sigset13(mask, &mask13); 1611.1Sskd frame.sf_sc.__sc_mask13 = mask13; 1621.1Sskd } 1631.1Sskd#endif 1641.1Sskd 1651.10Sad sendsig_reset(l, sig); 1661.13Sad mutex_exit(p->p_lock); 1671.11Schristos error = copyout(&frame, (void *)fp, sizeof(frame)); 1681.13Sad mutex_enter(p->p_lock); 1691.10Sad 1701.10Sad if (error != 0) { 1711.1Sskd /* 1721.1Sskd * Process has trashed its stack; give it an illegal 1731.1Sskd * instruction to halt it in its tracks. 1741.1Sskd */ 1751.1Sskd#ifdef DEBUG 1761.1Sskd if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 1771.1Sskd printf("sendsig_sigcontext(%d): copyout failed on sig %d\n", 1781.1Sskd p->p_pid, sig); 1791.1Sskd#endif 1801.1Sskd sigexit(l, SIGILL); 1811.1Sskd /* NOTREACHED */ 1821.1Sskd } 1831.1Sskd#ifdef DEBUG 1841.1Sskd if (sigdebug & SDB_FOLLOW) 1851.3Snathanw printf("sendsig_sigcontext(%d): sig %d usp %p code %x\n", 1861.3Snathanw p->p_pid, sig, fp, ksi->ksi_code); 1871.1Sskd#endif 1881.1Sskd 1891.1Sskd /* 1901.1Sskd * Set up the registers to directly invoke the signal handler. The 1911.1Sskd * signal trampoline is then used to return from the signal. Note 1921.1Sskd * the trampoline version numbers are coordinated with machine- 1931.1Sskd * dependent code in libc. 1941.1Sskd */ 1951.1Sskd switch (ps->sa_sigdesc[sig].sd_vers) { 1961.23Sthorpej case __SIGTRAMP_SIGCODE_VERSION: /* legacy on-stack sigtramp */ 1971.1Sskd buildcontext(l,(void *)catcher, 1981.1Sskd (void *)p->p_sigctx.ps_sigcode, 1991.1Sskd (void *)fp); 2001.1Sskd break; 2011.5Sdrochner#ifdef COMPAT_16 2021.23Sthorpej case __SIGTRAMP_SIGCONTEXT_VERSION: 2031.1Sskd buildcontext(l,(void *)catcher, 2041.6Sdrochner (const void *)ps->sa_sigdesc[sig].sd_tramp, 2051.1Sskd (void *)fp); 2061.1Sskd break; 2071.5Sdrochner#endif 2081.1Sskd default: 2091.1Sskd /* Don't know what trampoline version; kill it. */ 2101.1Sskd sigexit(l, SIGILL); 2111.1Sskd } 2121.1Sskd 2131.1Sskd /* sigcontext specific trap frame */ 2141.1Sskd tf->tf_regs[FRAME_A0] = sig; 2151.1Sskd 2161.1Sskd /* tf->tf_regs[FRAME_A1] = ksi->ksi_code; */ 2171.2Sthorpej tf->tf_regs[FRAME_A1] = KSI_TRAPCODE(ksi); 2181.18Smatt tf->tf_regs[FRAME_A2] = (uint64_t)&fp->sf_sc; 2191.1Sskd 2201.1Sskd /* Remember that we're now on the signal stack. */ 2211.1Sskd if (onstack) 2221.10Sad l->l_sigstk.ss_flags |= SS_ONSTACK; 2231.1Sskd 2241.1Sskd#ifdef DEBUG 2251.1Sskd if (sigdebug & SDB_FOLLOW) 2261.1Sskd printf("sendsig(%d): pc %lx, catcher %lx\n", p->p_pid, 2271.3Snathanw tf->tf_regs[FRAME_PC], tf->tf_regs[FRAME_A3]); 2281.1Sskd if ((sigdebug & SDB_KSTACK) && p->p_pid == sigpid) 2291.1Sskd printf("sendsig(%d): sig %d returns\n", 2301.1Sskd p->p_pid, sig); 2311.1Sskd#endif 2321.1Sskd} 2331.4Sdrochner 2341.22Smaxv#ifdef COMPAT_16 2351.1Sskd/* 2361.1Sskd * System call to cleanup state after a signal 2371.1Sskd * has been taken. Reset signal mask and 2381.1Sskd * stack state from context left by sendsig (above). 2391.1Sskd * Return to previous pc and psl as specified by 2401.1Sskd * context left by sendsig. Check carefully to 2411.1Sskd * make sure that the user has not modified the 2421.1Sskd * psl to gain improper privileges or to cause 2431.1Sskd * a machine fault. 2441.1Sskd */ 2451.1Sskd/* ARGSUSED */ 2461.1Sskdint 2471.12Sdslcompat_16_sys___sigreturn14(struct lwp *l, const struct compat_16_sys___sigreturn14_args *uap, register_t *retval) 2481.1Sskd{ 2491.12Sdsl /* { 2501.1Sskd syscallarg(struct sigcontext *) sigcntxp; 2511.12Sdsl } */ 2521.1Sskd struct sigcontext *scp, ksc; 2531.1Sskd struct proc *p = l->l_proc; 2541.16Srmind struct pcb *pcb; 2551.1Sskd 2561.1Sskd /* 2571.1Sskd * The trampoline code hands us the context. 2581.1Sskd * It is unsafe to keep track of it ourselves, in the event that a 2591.1Sskd * program jumps out of a signal handler. 2601.1Sskd */ 2611.1Sskd scp = SCARG(uap, sigcntxp); 2621.1Sskd#ifdef DEBUG 2631.1Sskd if (sigdebug & SDB_FOLLOW) 2641.1Sskd printf("sigreturn: pid %d, scp %p\n", p->p_pid, scp); 2651.1Sskd#endif 2661.18Smatt if (ALIGN(scp) != (uint64_t)scp) 2671.1Sskd return (EINVAL); 2681.1Sskd 2691.11Schristos if (copyin((void *)scp, &ksc, sizeof(ksc)) != 0) 2701.1Sskd return (EFAULT); 2711.1Sskd 2721.1Sskd if (ksc.sc_regs[R_ZERO] != 0xACEDBADE) /* magic number */ 2731.1Sskd return (EINVAL); 2741.1Sskd 2751.1Sskd /* Restore register context. */ 2761.1Sskd l->l_md.md_tf->tf_regs[FRAME_PC] = ksc.sc_pc; 2771.1Sskd l->l_md.md_tf->tf_regs[FRAME_PS] = 2781.1Sskd (ksc.sc_ps | ALPHA_PSL_USERSET) & ~ALPHA_PSL_USERCLR; 2791.1Sskd 2801.1Sskd regtoframe((struct reg *)ksc.sc_regs, l->l_md.md_tf); 2811.1Sskd alpha_pal_wrusp(ksc.sc_regs[R_SP]); 2821.1Sskd 2831.16Srmind pcb = lwp_getpcb(l); 2841.21Schs fpu_discard(l, true); 2851.16Srmind memcpy(&pcb->pcb_fp, (struct fpreg *)ksc.sc_fpregs, 2861.1Sskd sizeof(struct fpreg)); 2871.16Srmind pcb->pcb_fp.fpr_cr = ksc.sc_fpcr; 2881.19Smatt l->l_md.md_flags = ksc.sc_fp_control & MDLWP_FP_C; 2891.1Sskd 2901.13Sad mutex_enter(p->p_lock); 2911.1Sskd /* Restore signal stack. */ 2921.1Sskd if (ksc.sc_onstack & SS_ONSTACK) 2931.10Sad l->l_sigstk.ss_flags |= SS_ONSTACK; 2941.1Sskd else 2951.10Sad l->l_sigstk.ss_flags &= ~SS_ONSTACK; 2961.1Sskd /* Restore signal mask. */ 2971.10Sad (void) sigprocmask1(l, SIG_SETMASK, &ksc.sc_mask, 0); 2981.13Sad mutex_exit(p->p_lock); 2991.1Sskd 3001.1Sskd#ifdef DEBUG 3011.1Sskd if (sigdebug & SDB_FOLLOW) 3021.1Sskd printf("sigreturn(%d): returns\n", p->p_pid); 3031.1Sskd#endif 3041.1Sskd return (EJUSTRETURN); 3051.1Sskd} 3061.1Sskd#endif /* COMPAT_16 */ 307