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