xref: /netbsd-src/sys/arch/amd64/amd64/netbsd32_machdep_13.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: netbsd32_machdep_13.c,v 1.4 2020/07/19 13:55:09 maxv 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_13.c,v 1.4 2020/07/19 13:55:09 maxv Exp $");
40 
41 #ifdef _KERNEL_OPT
42 #include "opt_compat_netbsd.h"
43 #include "opt_execfmt.h"
44 #include "opt_mtrr.h"
45 #endif
46 
47 #include <sys/param.h>
48 #include <sys/exec.h>
49 #include <sys/exec_aout.h>
50 #include <sys/kmem.h>
51 #include <sys/malloc.h>
52 #include <sys/proc.h>
53 #include <sys/signalvar.h>
54 #include <sys/systm.h>
55 #include <sys/core.h>
56 #include <sys/mount.h>
57 #include <sys/buf.h>
58 #include <sys/vnode.h>
59 #include <sys/ras.h>
60 #include <sys/ptrace.h>
61 #include <sys/kauth.h>
62 
63 #include <x86/fpu.h>
64 #include <x86/dbregs.h>
65 #include <machine/frame.h>
66 #include <machine/reg.h>
67 #include <machine/vmparam.h>
68 #ifdef MTRR
69 #include <machine/mtrr.h>
70 #endif
71 #include <machine/netbsd32_machdep.h>
72 #include <machine/sysarch.h>
73 #include <machine/userret.h>
74 
75 #include <compat/netbsd32/netbsd32.h>
76 #include <compat/netbsd32/netbsd32_exec.h>
77 #include <compat/netbsd32/netbsd32_syscallargs.h>
78 
79 #include <compat/sys/signal.h>
80 #include <compat/sys/signalvar.h>
81 
82 int check_sigcontext32(struct lwp *, const struct netbsd32_sigcontext *);
83 
84 int
85 compat_13_netbsd32_sigreturn(struct lwp *l, const struct compat_13_netbsd32_sigreturn_args *uap, register_t *retval)
86 {
87 	/* {
88 		syscallarg(struct netbsd32_sigcontext13 *) sigcntxp;
89 	} */
90 	struct proc *p = l->l_proc;
91 	struct netbsd32_sigcontext13 *scp, context;
92 	struct trapframe *tf;
93 	sigset_t mask;
94 	int error;
95 
96 	/*
97 	 * The trampoline code hands us the context.
98 	 * It is unsafe to keep track of it ourselves, in the event that a
99 	 * program jumps out of a signal handler.
100 	 */
101 	scp = (struct netbsd32_sigcontext13 *)NETBSD32PTR64(SCARG(uap, sigcntxp));
102 	if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
103 		return (EFAULT);
104 
105 	/* Restore register context. */
106 	tf = l->l_md.md_regs;
107 
108 	/*
109 	 * Check for security violations.
110 	 */
111 	error = check_sigcontext32(l, (const struct netbsd32_sigcontext *)&context);
112 	if (error != 0)
113 		return error;
114 
115 	tf->tf_gs = context.sc_gs & 0xFFFF;
116 	tf->tf_fs = context.sc_fs & 0xFFFF;
117 	tf->tf_es = context.sc_es & 0xFFFF;
118 	tf->tf_ds = context.sc_ds & 0xFFFF;
119 	tf->tf_rflags = context.sc_eflags;
120 	tf->tf_rdi = context.sc_edi;
121 	tf->tf_rsi = context.sc_esi;
122 	tf->tf_rbp = context.sc_ebp;
123 	tf->tf_rbx = context.sc_ebx;
124 	tf->tf_rdx = context.sc_edx;
125 	tf->tf_rcx = context.sc_ecx;
126 	tf->tf_rax = context.sc_eax;
127 	tf->tf_rip = context.sc_eip;
128 	tf->tf_cs = context.sc_cs & 0xFFFF;
129 	tf->tf_rsp = context.sc_esp;
130 	tf->tf_ss = context.sc_ss & 0xFFFF;
131 
132 	mutex_enter(p->p_lock);
133 	/* Restore signal stack. */
134 	if (context.sc_onstack & SS_ONSTACK)
135 		l->l_sigstk.ss_flags |= SS_ONSTACK;
136 	else
137 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
138 	/* Restore signal mask. */
139 	native_sigset13_to_sigset((sigset13_t *)&context.sc_mask, &mask);
140 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
141 	mutex_exit(p->p_lock);
142 
143 	return (EJUSTRETURN);
144 }
145 
146 void
147 netbsd32_machdep_md_13_init(void)
148 {
149 
150 	/* Nothing to do */
151 }
152 
153 void
154 netbsd32_machdep_md_13_fini(void)
155 {
156 
157 	/* Nothing to do */
158 }
159