1 /* $NetBSD: freebsd_machdep.c,v 1.5 2021/09/07 11:43:03 riastradh 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.5 2021/09/07 11:43:03 riastradh 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
freebsd_setregs(struct lwp * l,struct exec_package * epp,vaddr_t stack)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
freebsd_sendsig(const ksiginfo_t * ksi,const sigset_t * mask)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 memset(&frame, 0, sizeof(frame));
92
93 /* Build stack frame for signal trampoline. */
94 frame.sf_signum = sig;
95 frame.sf_code = code;
96 frame.sf_scp = &fp->sf_sc;
97 frame.sf_addr = (char *)rcr2();
98 frame.sf_handler = catcher;
99
100 /* Save context. */
101 frame.sf_sc.sc_gs = tf->tf_gs;
102 frame.sf_sc.sc_fs = tf->tf_fs;
103 frame.sf_sc.sc_es = tf->tf_es;
104 frame.sf_sc.sc_ds = tf->tf_ds;
105 frame.sf_sc.sc_efl = tf->tf_eflags;
106
107 frame.sf_sc.sc_edi = tf->tf_edi;
108 frame.sf_sc.sc_esi = tf->tf_esi;
109 frame.sf_sc.sc_ebp = tf->tf_ebp;
110 frame.sf_sc.sc_isp = 0; /* don't have to pass kernel sp to user. */
111 frame.sf_sc.sc_ebx = tf->tf_ebx;
112 frame.sf_sc.sc_edx = tf->tf_edx;
113 frame.sf_sc.sc_ecx = tf->tf_ecx;
114 frame.sf_sc.sc_eax = tf->tf_eax;
115 frame.sf_sc.sc_eip = tf->tf_eip;
116 frame.sf_sc.sc_cs = tf->tf_cs;
117 frame.sf_sc.sc_esp = tf->tf_esp;
118 frame.sf_sc.sc_ss = tf->tf_ss;
119
120 /* Save signal stack. */
121 frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
122
123 /* Save signal mask. */
124 /* XXX freebsd_osigcontext compat? */
125 frame.sf_sc.sc_mask = *mask;
126
127 sendsig_reset(l, sig);
128
129 mutex_exit(p->p_lock);
130 error = copyout(&frame, fp, sizeof(frame));
131 mutex_enter(p->p_lock);
132
133 if (error != 0) {
134 /*
135 * Process has trashed its stack; give it an illegal
136 * instruction to halt it in its tracks.
137 */
138 sigexit(l, SIGILL);
139 /* NOTREACHED */
140 }
141
142 buildcontext(l, GUCODEBIG_SEL, p->p_sigctx.ps_sigcode, fp);
143
144 /* Remember that we're now on the signal stack. */
145 if (onstack)
146 l->l_sigstk.ss_flags |= SS_ONSTACK;
147 }
148
149 /*
150 * System call to cleanup state after a signal
151 * has been taken. Reset signal mask and
152 * stack state from context left by sendsig (above).
153 * Return to previous pc and psl as specified by
154 * context left by sendsig. Check carefully to
155 * make sure that the user has not modified the
156 * psl to gain improper privileges or to cause
157 * a machine fault.
158 */
159 int
freebsd_sys_sigreturn(struct lwp * l,const struct freebsd_sys_sigreturn_args * uap,register_t * retval)160 freebsd_sys_sigreturn(struct lwp *l, const struct freebsd_sys_sigreturn_args *uap, register_t *retval)
161 {
162 /* {
163 syscallarg(struct freebsd_sigcontext *) scp;
164 } */
165 struct proc *p = l->l_proc;
166 struct freebsd_sigcontext *scp, context;
167 struct trapframe *tf;
168 sigset_t mask;
169
170 /*
171 * The trampoline code hands us the context.
172 * It is unsafe to keep track of it ourselves, in the event that a
173 * program jumps out of a signal handler.
174 */
175 scp = SCARG(uap, scp);
176 if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
177 return (EFAULT);
178
179 /* Restore register context. */
180 tf = l->l_md.md_regs;
181
182 /*
183 * Check for security violations. If we're returning to
184 * protected mode, the CPU will validate the segment registers
185 * automatically and generate a trap on violations. We handle
186 * the trap, rather than doing all of the checking here.
187 */
188 if (((context.sc_efl ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
189 !USERMODE(context.sc_cs))
190 return (EINVAL);
191
192 tf->tf_gs = context.sc_gs;
193 tf->tf_fs = context.sc_fs;
194 tf->tf_es = context.sc_es;
195 tf->tf_ds = context.sc_ds;
196 tf->tf_eflags &= ~PSL_USER;
197 tf->tf_eflags |= context.sc_efl & PSL_USER;
198
199 tf->tf_edi = context.sc_edi;
200 tf->tf_esi = context.sc_esi;
201 tf->tf_ebp = context.sc_ebp;
202 /* FreeBSD's context.sc_isp is useless. (`popal' ignores it.) */
203 tf->tf_ebx = context.sc_ebx;
204 tf->tf_edx = context.sc_edx;
205 tf->tf_ecx = context.sc_ecx;
206 tf->tf_eax = context.sc_eax;
207 tf->tf_eip = context.sc_eip;
208 tf->tf_cs = context.sc_cs;
209 tf->tf_esp = context.sc_esp;
210 tf->tf_ss = context.sc_ss;
211
212 mutex_enter(p->p_lock);
213 /* Restore signal stack. */
214 if (context.sc_onstack & SS_ONSTACK)
215 l->l_sigstk.ss_flags |= SS_ONSTACK;
216 else
217 l->l_sigstk.ss_flags &= ~SS_ONSTACK;
218 /* Restore signal mask. */
219 /* XXX freebsd_osigcontext compat? */
220 mask = context.sc_mask;
221 (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
222 mutex_exit(p->p_lock);
223
224 return (EJUSTRETURN);
225 }
226