1 /* $NetBSD: compat_13_machdep.c,v 1.27 2017/09/17 09:41:35 maxv Exp $ */ 2 3 /*- 4 * Copyright (c) 1996, 1997, 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: compat_13_machdep.c,v 1.27 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/kernel.h> 39 #include <sys/proc.h> 40 #include <sys/mount.h> 41 #include <sys/syscallargs.h> 42 43 #include <compat/sys/signal.h> 44 #include <compat/sys/signalvar.h> 45 46 int 47 compat_13_sys_sigreturn(struct lwp *l, const struct compat_13_sys_sigreturn_args *uap, register_t *retval) 48 { 49 /* { 50 syscallarg(struct sigcontext13 *) sigcntxp; 51 } */ 52 struct proc *p = l->l_proc; 53 struct sigcontext13 *scp, context; 54 struct trapframe *tf; 55 sigset_t mask; 56 57 /* 58 * The trampoline code hands us the context. 59 * It is unsafe to keep track of it ourselves, in the event that a 60 * program jumps out of a signal handler. 61 */ 62 scp = SCARG(uap, sigcntxp); 63 if (copyin((void *)scp, &context, sizeof(*scp)) != 0) 64 return (EFAULT); 65 66 /* Restore register context. */ 67 tf = l->l_md.md_regs; 68 69 /* 70 * Check for security violations. If we're returning to 71 * protected mode, the CPU will validate the segment registers 72 * automatically and generate a trap on violations. We handle 73 * the trap, rather than doing all of the checking here. 74 */ 75 if (((context.sc_eflags ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 || 76 !USERMODE(context.sc_cs)) 77 return (EINVAL); 78 79 tf->tf_gs = context.sc_gs; 80 tf->tf_fs = context.sc_fs; 81 tf->tf_es = context.sc_es; 82 tf->tf_ds = context.sc_ds; 83 tf->tf_eflags &= ~PSL_USER; 84 tf->tf_eflags |= context.sc_eflags & PSL_USER; 85 86 tf->tf_edi = context.sc_edi; 87 tf->tf_esi = context.sc_esi; 88 tf->tf_ebp = context.sc_ebp; 89 tf->tf_ebx = context.sc_ebx; 90 tf->tf_edx = context.sc_edx; 91 tf->tf_ecx = context.sc_ecx; 92 tf->tf_eax = context.sc_eax; 93 tf->tf_eip = context.sc_eip; 94 tf->tf_cs = context.sc_cs; 95 tf->tf_esp = context.sc_esp; 96 tf->tf_ss = context.sc_ss; 97 98 mutex_enter(p->p_lock); 99 /* Restore signal stack. */ 100 if (context.sc_onstack & SS_ONSTACK) 101 l->l_sigstk.ss_flags |= SS_ONSTACK; 102 else 103 l->l_sigstk.ss_flags &= ~SS_ONSTACK; 104 /* Restore signal mask. */ 105 native_sigset13_to_sigset(&context.sc_mask, &mask); 106 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0); 107 mutex_exit(p->p_lock); 108 109 return (EJUSTRETURN); 110 } 111