Home | History | Annotate | Line # | Download | only in amd64
      1 /*	$NetBSD: netbsd32_machdep_16.c,v 1.8 2023/12/20 06:36:01 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 Wasabi Systems, Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Frank van der Linden for Wasabi Systems, Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed for the NetBSD Project by
     20  *      Wasabi Systems, Inc.
     21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  *    or promote products derived from this software without specific prior
     23  *    written permission.
     24  *
     25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  * POSSIBILITY OF SUCH DAMAGE.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_16.c,v 1.8 2023/12/20 06:36:01 thorpej Exp $");
     40 
     41 #ifdef _KERNEL_OPT
     42 #include "opt_compat_netbsd.h"
     43 #include "opt_execfmt.h"
     44 #endif
     45 
     46 #include <sys/param.h>
     47 #include <sys/exec.h>
     48 #include <sys/exec_aout.h>
     49 #include <sys/kmem.h>
     50 #include <sys/proc.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/systm.h>
     53 #include <sys/core.h>
     54 #include <sys/mount.h>
     55 #include <sys/buf.h>
     56 #include <sys/vnode.h>
     57 #include <sys/ras.h>
     58 #include <sys/ptrace.h>
     59 #include <sys/kauth.h>
     60 #include <sys/module_hook.h>
     61 
     62 #include <x86/fpu.h>
     63 #include <x86/dbregs.h>
     64 #include <machine/frame.h>
     65 #include <machine/reg.h>
     66 #include <machine/vmparam.h>
     67 #include <machine/netbsd32_machdep.h>
     68 #include <machine/sysarch.h>
     69 #include <machine/userret.h>
     70 
     71 #include <compat/netbsd32/netbsd32.h>
     72 #include <compat/netbsd32/netbsd32_exec.h>
     73 #include <compat/netbsd32/netbsd32_syscallargs.h>
     74 
     75 #include <compat/sys/signal.h>
     76 #include <compat/sys/signalvar.h>
     77 
     78 void netbsd32_buildcontext(struct lwp *, struct trapframe *, void *,
     79     sig_t, int);
     80 
     81 int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
     82 
     83 static void
     84 netbsd32_sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
     85 {
     86 	struct lwp *l = curlwp;
     87 	struct proc *p = l->l_proc;
     88 	struct trapframe *tf;
     89 	int sig = ksi->ksi_signo;
     90 	sig_t catcher = SIGACTION(p, sig).sa_handler;
     91 	struct netbsd32_sigframe_sigcontext *fp, frame;
     92 	int onstack, error;
     93 	struct sigacts *ps = p->p_sigacts;
     94 
     95 	tf = l->l_md.md_regs;
     96 
     97 	/* Do we need to jump onto the signal stack? */
     98 	onstack =
     99 	    (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
    100 	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
    101 
    102 	/* Allocate space for the signal handler context. */
    103 	if (onstack)
    104 		fp = (struct netbsd32_sigframe_sigcontext *)
    105 		    ((char *)l->l_sigstk.ss_sp + l->l_sigstk.ss_size);
    106 	else
    107 		fp = (struct netbsd32_sigframe_sigcontext *)tf->tf_rsp;
    108 	fp--;
    109 
    110 	memset(&frame, 0, sizeof(frame));
    111 
    112 	/* Build stack frame for signal trampoline. */
    113 	switch (ps->sa_sigdesc[sig].sd_vers) {
    114 	case __SIGTRAMP_SIGCODE_VERSION:
    115 		frame.sf_ra = (uint32_t)(u_long)p->p_sigctx.ps_sigcode;
    116 		break;
    117 	case __SIGTRAMP_SIGCONTEXT_VERSION:
    118 		frame.sf_ra = (uint32_t)(u_long)ps->sa_sigdesc[sig].sd_tramp;
    119 		break;
    120 	default:
    121 		/* Don't know what trampoline version; kill it. */
    122 		sigexit(l, SIGILL);
    123 	}
    124 	frame.sf_signum = sig;
    125 	frame.sf_code = ksi->ksi_trap;
    126 	frame.sf_scp = (uint32_t)(u_long)&fp->sf_sc;
    127 
    128 	frame.sf_sc.sc_ds = tf->tf_ds & 0xFFFF;
    129 	frame.sf_sc.sc_es = tf->tf_es & 0xFFFF;
    130 	frame.sf_sc.sc_fs = tf->tf_fs & 0xFFFF;
    131 	frame.sf_sc.sc_gs = tf->tf_gs & 0xFFFF;
    132 
    133 	frame.sf_sc.sc_eflags = tf->tf_rflags;
    134 	frame.sf_sc.sc_edi = tf->tf_rdi;
    135 	frame.sf_sc.sc_esi = tf->tf_rsi;
    136 	frame.sf_sc.sc_ebp = tf->tf_rbp;
    137 	frame.sf_sc.sc_ebx = tf->tf_rbx;
    138 	frame.sf_sc.sc_edx = tf->tf_rdx;
    139 	frame.sf_sc.sc_ecx = tf->tf_rcx;
    140 	frame.sf_sc.sc_eax = tf->tf_rax;
    141 	frame.sf_sc.sc_eip = tf->tf_rip;
    142 	frame.sf_sc.sc_cs = tf->tf_cs & 0xFFFF;
    143 	frame.sf_sc.sc_esp = tf->tf_rsp;
    144 	frame.sf_sc.sc_ss = tf->tf_ss & 0xFFFF;
    145 	frame.sf_sc.sc_trapno = tf->tf_trapno;
    146 	frame.sf_sc.sc_err = tf->tf_err;
    147 
    148 	/* Save signal stack. */
    149 	frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
    150 
    151 	/* Save signal mask. */
    152 	frame.sf_sc.sc_mask = *mask;
    153 
    154 	sendsig_reset(l, sig);
    155 
    156 	mutex_exit(p->p_lock);
    157 	error = copyout(&frame, fp, sizeof(frame));
    158 	mutex_enter(p->p_lock);
    159 
    160 	if (error != 0) {
    161 		/*
    162 		 * Process has trashed its stack; give it an illegal
    163 		 * instruction to halt it in its tracks.
    164 		 */
    165 		sigexit(l, SIGILL);
    166 		/* NOTREACHED */
    167 	}
    168 
    169 	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
    170 }
    171 
    172 int
    173 compat_16_netbsd32___sigreturn14(struct lwp *l,
    174     const struct compat_16_netbsd32___sigreturn14_args *uap,
    175     register_t *retval)
    176 {
    177 	/* {
    178 		syscallarg(netbsd32_sigcontextp_t) sigcntxp;
    179 	} */
    180 	struct netbsd32_sigcontext *scp, context;
    181 	struct proc *p = l->l_proc;
    182 	struct trapframe *tf;
    183 	int error;
    184 
    185 	/*
    186 	 * The trampoline code hands us the context.
    187 	 * It is unsafe to keep track of it ourselves, in the event that a
    188 	 * program jumps out of a signal handler.
    189 	 */
    190 	scp = NETBSD32PTR64(SCARG(uap, sigcntxp));
    191 	if (copyin(scp, &context, sizeof(*scp)) != 0)
    192 		return (EFAULT);
    193 
    194 	/*
    195 	 * Check for security violations.
    196 	 */
    197 	error = check_sigcontext32(l, &context);
    198 	if (error != 0)
    199 		return error;
    200 
    201 	/* Restore register context. */
    202 	tf = l->l_md.md_regs;
    203 	tf->tf_ds = context.sc_ds & 0xFFFF;
    204 	tf->tf_es = context.sc_es & 0xFFFF;
    205 	cpu_fsgs_reload(l, context.sc_fs, context.sc_gs);
    206 	tf->tf_rflags = context.sc_eflags;
    207 	tf->tf_rdi = context.sc_edi;
    208 	tf->tf_rsi = context.sc_esi;
    209 	tf->tf_rbp = context.sc_ebp;
    210 	tf->tf_rbx = context.sc_ebx;
    211 	tf->tf_rdx = context.sc_edx;
    212 	tf->tf_rcx = context.sc_ecx;
    213 	tf->tf_rax = context.sc_eax;
    214 
    215 	tf->tf_rip = context.sc_eip;
    216 	tf->tf_cs = context.sc_cs & 0xFFFF;
    217 	tf->tf_rsp = context.sc_esp;
    218 	tf->tf_ss = context.sc_ss & 0xFFFF;
    219 
    220 	mutex_enter(p->p_lock);
    221 	/* Restore signal stack. */
    222 	if (context.sc_onstack & SS_ONSTACK)
    223 		l->l_sigstk.ss_flags |= SS_ONSTACK;
    224 	else
    225 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
    226 	/* Restore signal mask. */
    227 	(void) sigprocmask1(l, SIG_SETMASK, &context.sc_mask, 0);
    228 	mutex_exit(p->p_lock);
    229 
    230 	return (EJUSTRETURN);
    231 }
    232 
    233 void
    234 netbsd32_machdep_md_16_init(void)
    235 {
    236 
    237 	MODULE_HOOK_SET(netbsd32_sendsig_sigcontext_16_hook,
    238 	    netbsd32_sendsig_sigcontext);
    239 }
    240 
    241 void
    242 netbsd32_machdep_md_16_fini(void)
    243 {
    244 
    245 	MODULE_HOOK_UNSET(netbsd32_sendsig_sigcontext_16_hook);
    246 }
    247