xref: /netbsd-src/sys/arch/sparc64/sparc64/netbsd32_machdep_13.c (revision 3194cf25b2bcc7b155490c9dd74d3ebbd7fc1636)
1 /*	$NetBSD: netbsd32_machdep_13.c,v 1.5 2023/12/14 20:17:18 andvar Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep_13.c,v 1.5 2023/12/14 20:17:18 andvar Exp $");
31 
32 #ifdef _KERNEL_OPT
33 #include "opt_compat_netbsd.h"
34 #include "opt_modular.h"
35 #include "opt_execfmt.h"
36 #include "firm_events.h"
37 #endif
38 
39 #include <sys/param.h>
40 #include <sys/exec.h>
41 #include <sys/exec_aout.h>
42 #include <sys/filedesc.h>
43 #include <sys/file.h>
44 #include <sys/proc.h>
45 #include <sys/signalvar.h>
46 #include <sys/systm.h>
47 #include <sys/core.h>
48 #include <sys/mount.h>
49 #include <sys/buf.h>
50 #include <sys/vnode.h>
51 #include <sys/select.h>
52 #include <sys/socketvar.h>
53 #include <sys/ucontext.h>
54 #include <sys/ioctl.h>
55 #include <sys/kmem.h>
56 
57 #include <dev/sun/event_var.h>
58 
59 #include <net/if.h>
60 #include <net/route.h>
61 
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/igmp.h>
65 #include <netinet/igmp_var.h>
66 #include <netinet/ip_mroute.h>
67 
68 #include <compat/netbsd32/netbsd32.h>
69 #include <compat/netbsd32/netbsd32_ioctl.h>
70 #include <compat/netbsd32/netbsd32_syscallargs.h>
71 #include <compat/netbsd32/netbsd32_exec.h>
72 
73 #include <compat/sys/signal.h>
74 #include <compat/sys/signalvar.h>
75 #include <compat/sys/siginfo.h>
76 #include <compat/sys/ucontext.h>
77 
78 #include <machine/frame.h>
79 #include <machine/pcb.h>
80 #include <machine/reg.h>
81 #include <machine/vmparam.h>
82 #include <machine/vuid_event.h>
83 #include <machine/netbsd32_machdep.h>
84 #include <machine/userret.h>
85 
86 #ifdef DEBUG
87 #include <sparc64/sparc64/sigdebug.h>
88 #endif
89 
90 int
compat_13_netbsd32_sigreturn(struct lwp * l,const struct compat_13_netbsd32_sigreturn_args * uap,register_t * retval)91 compat_13_netbsd32_sigreturn(struct lwp *l, const struct compat_13_netbsd32_sigreturn_args *uap, register_t *retval)
92 {
93 	/* {
94 		syscallarg(struct netbsd32_sigcontext13 *) sigcntxp;
95 	} */
96 	struct netbsd32_sigcontext13 *scp;
97 	struct netbsd32_sigcontext13 sc;
98 	struct trapframe64 *tf;
99 	struct proc *p = l->l_proc;
100 	sigset_t mask;
101 
102 	/* First ensure consistent stack state (see sendsig). */
103 	write_user_windows();
104 	if (rwindow_save(l)) {
105 #ifdef DEBUG
106 		printf("%s: rwindow_save(%p) failed, sending SIGILL\n",
107 		    __func__, p);
108 		console_debugger();
109 #endif
110 		mutex_enter(p->p_lock);
111 		sigexit(l, SIGILL);
112 	}
113 #ifdef DEBUG
114 	if (sigdebug & SDB_FOLLOW) {
115 		printf("%s: %s[%d], sigcntxp %u\n", __func__,
116 		    p->p_comm, p->p_pid, SCARG(uap, sigcntxp));
117 		if (sigdebug & SDB_DDB) console_debugger();
118 	}
119 #endif
120 	scp = (struct netbsd32_sigcontext13 *)(u_long)SCARG(uap, sigcntxp);
121  	if ((vaddr_t)scp & 3 || (copyin((void *)scp, &sc, sizeof sc) != 0))
122 	{
123 #ifdef DEBUG
124 		printf("%s: copyin failed\n", __func__);
125 		console_debugger();
126 #endif
127 		return (EINVAL);
128 	}
129 	scp = &sc;
130 
131 	tf = l->l_md.md_tf;
132 	/*
133 	 * Only the icc bits in the psr are used, so it need not be
134 	 * verified.  pc and npc must be multiples of 4.  This is all
135 	 * that is required; if it holds, just do it.
136 	 */
137 	if (((sc.sc_pc | sc.sc_npc) & 3) != 0)
138 #ifdef DEBUG
139 	{
140 		printf("%s: pc %d or npc %d invalid\n",
141 		   __func__, sc.sc_pc, sc.sc_npc);
142 		console_debugger();
143 		return (EINVAL);
144 	}
145 #else
146 		return (EINVAL);
147 #endif
148 	/* take only psr ICC field */
149 	tf->tf_tstate = (int64_t)(tf->tf_tstate & ~TSTATE_CCR) | PSRCC_TO_TSTATE(sc.sc_psr);
150 	tf->tf_pc = (int64_t)sc.sc_pc;
151 	tf->tf_npc = (int64_t)sc.sc_npc;
152 	tf->tf_global[1] = (int64_t)sc.sc_g1;
153 	tf->tf_out[0] = (int64_t)sc.sc_o0;
154 	tf->tf_out[6] = (int64_t)sc.sc_sp;
155 #ifdef DEBUG
156 	if (sigdebug & SDB_FOLLOW) {
157 		printf("%s: return trapframe pc=%d sp=%d tstate=%x\n", __func__,
158 		       (int)tf->tf_pc, (int)tf->tf_out[6], (int)tf->tf_tstate);
159 		if (sigdebug & SDB_DDB) console_debugger();
160 	}
161 #endif
162 	mutex_enter(p->p_lock);
163 	if (scp->sc_onstack & SS_ONSTACK)
164 		l->l_sigstk.ss_flags |= SS_ONSTACK;
165 	else
166 		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
167 	/* Restore signal mask */
168 	native_sigset13_to_sigset((sigset13_t *)&scp->sc_mask, &mask);
169 	(void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
170 	mutex_exit(p->p_lock);
171 
172 	return (EJUSTRETURN);
173 }
174 
175 void
netbsd32_machdep_md_13_init(void)176 netbsd32_machdep_md_13_init(void)
177 {
178 
179 	/* Nothing to do */
180 }
181 
182 void
netbsd32_machdep_md_13_fini(void)183 netbsd32_machdep_md_13_fini(void)
184 {
185 
186 	/* Nothing to do */
187 }
188