1 /* $NetBSD: freebsd_machdep.c,v 1.4 2017/09/17 09:41:35 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Charles M. Hannum. 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 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: freebsd_machdep.c,v 1.4 2017/09/17 09:41:35 maxv Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/signalvar.h> 38 #include <sys/proc.h> 39 #include <sys/exec.h> 40 #include <sys/mount.h> 41 42 #include <compat/sys/signal.h> 43 44 #include <machine/cpufunc.h> 45 #include <x86/fpu.h> 46 #include <machine/reg.h> 47 #include <machine/vmparam.h> 48 #include <compat/freebsd/freebsd_machdep.h> 49 50 51 #include <compat/freebsd/freebsd_syscallargs.h> 52 #include <compat/freebsd/freebsd_exec.h> 53 #include <compat/freebsd/freebsd_signal.h> 54 55 void 56 freebsd_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack) 57 { 58 59 setregs(l, epp, stack); 60 fpu_set_default_cw(l, __FreeBSD_NPXCW__); 61 } 62 63 /* 64 * signal support 65 */ 66 67 /* 68 * Send an interrupt to process. 69 * 70 * Stack is set up to allow sigcode stored 71 * in u. to call routine, followed by kcall 72 * to sigreturn routine below. After sigreturn 73 * resets the signal mask, the stack, and the 74 * frame pointer, it returns to the user 75 * specified pc, psl. 76 */ 77 void 78 freebsd_sendsig(const ksiginfo_t *ksi, const sigset_t *mask) 79 { 80 int sig = ksi->ksi_signo; 81 u_long code = KSI_TRAPCODE(ksi); 82 struct lwp *l = curlwp; 83 struct proc *p = l->l_proc; 84 int onstack, error; 85 struct freebsd_sigframe *fp = getframe(l, sig, &onstack), frame; 86 sig_t catcher = SIGACTION(p, sig).sa_handler; 87 struct trapframe *tf = l->l_md.md_regs; 88 89 fp--; 90 91 /* Build stack frame for signal trampoline. */ 92 frame.sf_signum = sig; 93 frame.sf_code = code; 94 frame.sf_scp = &fp->sf_sc; 95 frame.sf_addr = (char *)rcr2(); 96 frame.sf_handler = catcher; 97 98 /* Save context. */ 99 frame.sf_sc.sc_gs = tf->tf_gs; 100 frame.sf_sc.sc_fs = tf->tf_fs; 101 frame.sf_sc.sc_es = tf->tf_es; 102 frame.sf_sc.sc_ds = tf->tf_ds; 103 frame.sf_sc.sc_efl = tf->tf_eflags; 104 105 frame.sf_sc.sc_edi = tf->tf_edi; 106 frame.sf_sc.sc_esi = tf->tf_esi; 107 frame.sf_sc.sc_ebp = tf->tf_ebp; 108 frame.sf_sc.sc_isp = 0; /* don't have to pass kernel sp to user. */ 109 frame.sf_sc.sc_ebx = tf->tf_ebx; 110 frame.sf_sc.sc_edx = tf->tf_edx; 111 frame.sf_sc.sc_ecx = tf->tf_ecx; 112 frame.sf_sc.sc_eax = tf->tf_eax; 113 frame.sf_sc.sc_eip = tf->tf_eip; 114 frame.sf_sc.sc_cs = tf->tf_cs; 115 frame.sf_sc.sc_esp = tf->tf_esp; 116 frame.sf_sc.sc_ss = tf->tf_ss; 117 118 /* Save signal stack. */ 119 frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK; 120 121 /* Save signal mask. */ 122 /* XXX freebsd_osigcontext compat? */ 123 frame.sf_sc.sc_mask = *mask; 124 125 sendsig_reset(l, sig); 126 127 mutex_exit(p->p_lock); 128 error = copyout(&frame, fp, sizeof(frame)); 129 mutex_enter(p->p_lock); 130 131 if (error != 0) { 132 /* 133 * Process has trashed its stack; give it an illegal 134 * instruction to halt it in its tracks. 135 */ 136 sigexit(l, SIGILL); 137 /* NOTREACHED */ 138 } 139 140 buildcontext(l, GUCODEBIG_SEL, p->p_sigctx.ps_sigcode, fp); 141 142 /* Remember that we're now on the signal stack. */ 143 if (onstack) 144 l->l_sigstk.ss_flags |= SS_ONSTACK; 145 } 146 147 /* 148 * System call to cleanup state after a signal 149 * has been taken. Reset signal mask and 150 * stack state from context left by sendsig (above). 151 * Return to previous pc and psl as specified by 152 * context left by sendsig. Check carefully to 153 * make sure that the user has not modified the 154 * psl to gain improper privileges or to cause 155 * a machine fault. 156 */ 157 int 158 freebsd_sys_sigreturn(struct lwp *l, const struct freebsd_sys_sigreturn_args *uap, register_t *retval) 159 { 160 /* { 161 syscallarg(struct freebsd_sigcontext *) scp; 162 } */ 163 struct proc *p = l->l_proc; 164 struct freebsd_sigcontext *scp, context; 165 struct trapframe *tf; 166 sigset_t mask; 167 168 /* 169 * The trampoline code hands us the context. 170 * It is unsafe to keep track of it ourselves, in the event that a 171 * program jumps out of a signal handler. 172 */ 173 scp = SCARG(uap, scp); 174 if (copyin((void *)scp, &context, sizeof(*scp)) != 0) 175 return (EFAULT); 176 177 /* Restore register context. */ 178 tf = l->l_md.md_regs; 179 180 /* 181 * Check for security violations. If we're returning to 182 * protected mode, the CPU will validate the segment registers 183 * automatically and generate a trap on violations. We handle 184 * the trap, rather than doing all of the checking here. 185 */ 186 if (((context.sc_efl ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 || 187 !USERMODE(context.sc_cs)) 188 return (EINVAL); 189 190 tf->tf_gs = context.sc_gs; 191 tf->tf_fs = context.sc_fs; 192 tf->tf_es = context.sc_es; 193 tf->tf_ds = context.sc_ds; 194 tf->tf_eflags &= ~PSL_USER; 195 tf->tf_eflags |= context.sc_efl & PSL_USER; 196 197 tf->tf_edi = context.sc_edi; 198 tf->tf_esi = context.sc_esi; 199 tf->tf_ebp = context.sc_ebp; 200 /* FreeBSD's context.sc_isp is useless. (`popal' ignores it.) */ 201 tf->tf_ebx = context.sc_ebx; 202 tf->tf_edx = context.sc_edx; 203 tf->tf_ecx = context.sc_ecx; 204 tf->tf_eax = context.sc_eax; 205 tf->tf_eip = context.sc_eip; 206 tf->tf_cs = context.sc_cs; 207 tf->tf_esp = context.sc_esp; 208 tf->tf_ss = context.sc_ss; 209 210 mutex_enter(p->p_lock); 211 /* Restore signal stack. */ 212 if (context.sc_onstack & SS_ONSTACK) 213 l->l_sigstk.ss_flags |= SS_ONSTACK; 214 else 215 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 216 /* Restore signal mask. */ 217 /* XXX freebsd_osigcontext compat? */ 218 mask = context.sc_mask; 219 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0); 220 mutex_exit(p->p_lock); 221 222 return (EJUSTRETURN); 223 } 224