freebsd_machdep.c revision 1.1 1 1.1 maxv /* $NetBSD: freebsd_machdep.c,v 1.1 2017/08/01 14:43:54 maxv Exp $ */
2 1.1 maxv
3 1.1 maxv /*-
4 1.1 maxv * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 1.1 maxv * All rights reserved.
6 1.1 maxv *
7 1.1 maxv * This code is derived from software contributed to The NetBSD Foundation
8 1.1 maxv * by Charles M. Hannum.
9 1.1 maxv *
10 1.1 maxv * Redistribution and use in source and binary forms, with or without
11 1.1 maxv * modification, are permitted provided that the following conditions
12 1.1 maxv * are met:
13 1.1 maxv * 1. Redistributions of source code must retain the above copyright
14 1.1 maxv * notice, this list of conditions and the following disclaimer.
15 1.1 maxv * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 maxv * notice, this list of conditions and the following disclaimer in the
17 1.1 maxv * documentation and/or other materials provided with the distribution.
18 1.1 maxv *
19 1.1 maxv * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 maxv * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 maxv * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 maxv * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 maxv * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 maxv * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 maxv * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 maxv * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 maxv * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 maxv * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 maxv * POSSIBILITY OF SUCH DAMAGE.
30 1.1 maxv */
31 1.1 maxv
32 1.1 maxv #include <sys/cdefs.h>
33 1.1 maxv __KERNEL_RCSID(0, "$NetBSD: freebsd_machdep.c,v 1.1 2017/08/01 14:43:54 maxv Exp $");
34 1.1 maxv
35 1.1 maxv #if defined(_KERNEL_OPT)
36 1.1 maxv #include "opt_vm86.h"
37 1.1 maxv #endif
38 1.1 maxv
39 1.1 maxv #include <sys/param.h>
40 1.1 maxv #include <sys/systm.h>
41 1.1 maxv #include <sys/signalvar.h>
42 1.1 maxv #include <sys/proc.h>
43 1.1 maxv #include <sys/exec.h>
44 1.1 maxv #include <sys/mount.h>
45 1.1 maxv
46 1.1 maxv #include <compat/sys/signal.h>
47 1.1 maxv
48 1.1 maxv #include <machine/cpufunc.h>
49 1.1 maxv #include <x86/fpu.h>
50 1.1 maxv #include <machine/reg.h>
51 1.1 maxv #include <machine/vm86.h>
52 1.1 maxv #include <machine/vmparam.h>
53 1.1 maxv #include <machine/freebsd_machdep.h>
54 1.1 maxv
55 1.1 maxv
56 1.1 maxv #include <compat/freebsd/freebsd_syscallargs.h>
57 1.1 maxv #include <compat/freebsd/freebsd_exec.h>
58 1.1 maxv #include <compat/freebsd/freebsd_signal.h>
59 1.1 maxv
60 1.1 maxv void
61 1.1 maxv freebsd_setregs(struct lwp *l, struct exec_package *epp, vaddr_t stack)
62 1.1 maxv {
63 1.1 maxv
64 1.1 maxv setregs(l, epp, stack);
65 1.1 maxv fpu_set_default_cw(l, __FreeBSD_NPXCW__);
66 1.1 maxv }
67 1.1 maxv
68 1.1 maxv /*
69 1.1 maxv * signal support
70 1.1 maxv */
71 1.1 maxv
72 1.1 maxv /*
73 1.1 maxv * Send an interrupt to process.
74 1.1 maxv *
75 1.1 maxv * Stack is set up to allow sigcode stored
76 1.1 maxv * in u. to call routine, followed by kcall
77 1.1 maxv * to sigreturn routine below. After sigreturn
78 1.1 maxv * resets the signal mask, the stack, and the
79 1.1 maxv * frame pointer, it returns to the user
80 1.1 maxv * specified pc, psl.
81 1.1 maxv */
82 1.1 maxv void
83 1.1 maxv freebsd_sendsig(const ksiginfo_t *ksi, const sigset_t *mask)
84 1.1 maxv {
85 1.1 maxv int sig = ksi->ksi_signo;
86 1.1 maxv u_long code = KSI_TRAPCODE(ksi);
87 1.1 maxv struct lwp *l = curlwp;
88 1.1 maxv struct proc *p = l->l_proc;
89 1.1 maxv int onstack, error;
90 1.1 maxv struct freebsd_sigframe *fp = getframe(l, sig, &onstack), frame;
91 1.1 maxv sig_t catcher = SIGACTION(p, sig).sa_handler;
92 1.1 maxv struct trapframe *tf = l->l_md.md_regs;
93 1.1 maxv
94 1.1 maxv fp--;
95 1.1 maxv
96 1.1 maxv /* Build stack frame for signal trampoline. */
97 1.1 maxv frame.sf_signum = sig;
98 1.1 maxv frame.sf_code = code;
99 1.1 maxv frame.sf_scp = &fp->sf_sc;
100 1.1 maxv frame.sf_addr = (char *)rcr2();
101 1.1 maxv frame.sf_handler = catcher;
102 1.1 maxv
103 1.1 maxv /* Save context. */
104 1.1 maxv #ifdef VM86
105 1.1 maxv if (tf->tf_eflags & PSL_VM) {
106 1.1 maxv frame.sf_sc.sc_gs = tf->tf_vm86_gs;
107 1.1 maxv frame.sf_sc.sc_fs = tf->tf_vm86_fs;
108 1.1 maxv frame.sf_sc.sc_es = tf->tf_vm86_es;
109 1.1 maxv frame.sf_sc.sc_ds = tf->tf_vm86_ds;
110 1.1 maxv frame.sf_sc.sc_efl = get_vflags(l);
111 1.1 maxv (*p->p_emul->e_syscall_intern)(p);
112 1.1 maxv } else
113 1.1 maxv #endif
114 1.1 maxv {
115 1.1 maxv frame.sf_sc.sc_gs = tf->tf_gs;
116 1.1 maxv frame.sf_sc.sc_fs = tf->tf_fs;
117 1.1 maxv frame.sf_sc.sc_es = tf->tf_es;
118 1.1 maxv frame.sf_sc.sc_ds = tf->tf_ds;
119 1.1 maxv frame.sf_sc.sc_efl = tf->tf_eflags;
120 1.1 maxv }
121 1.1 maxv frame.sf_sc.sc_edi = tf->tf_edi;
122 1.1 maxv frame.sf_sc.sc_esi = tf->tf_esi;
123 1.1 maxv frame.sf_sc.sc_ebp = tf->tf_ebp;
124 1.1 maxv frame.sf_sc.sc_isp = 0; /* don't have to pass kernel sp to user. */
125 1.1 maxv frame.sf_sc.sc_ebx = tf->tf_ebx;
126 1.1 maxv frame.sf_sc.sc_edx = tf->tf_edx;
127 1.1 maxv frame.sf_sc.sc_ecx = tf->tf_ecx;
128 1.1 maxv frame.sf_sc.sc_eax = tf->tf_eax;
129 1.1 maxv frame.sf_sc.sc_eip = tf->tf_eip;
130 1.1 maxv frame.sf_sc.sc_cs = tf->tf_cs;
131 1.1 maxv frame.sf_sc.sc_esp = tf->tf_esp;
132 1.1 maxv frame.sf_sc.sc_ss = tf->tf_ss;
133 1.1 maxv
134 1.1 maxv /* Save signal stack. */
135 1.1 maxv frame.sf_sc.sc_onstack = l->l_sigstk.ss_flags & SS_ONSTACK;
136 1.1 maxv
137 1.1 maxv /* Save signal mask. */
138 1.1 maxv /* XXX freebsd_osigcontext compat? */
139 1.1 maxv frame.sf_sc.sc_mask = *mask;
140 1.1 maxv
141 1.1 maxv sendsig_reset(l, sig);
142 1.1 maxv
143 1.1 maxv mutex_exit(p->p_lock);
144 1.1 maxv error = copyout(&frame, fp, sizeof(frame));
145 1.1 maxv mutex_enter(p->p_lock);
146 1.1 maxv
147 1.1 maxv if (error != 0) {
148 1.1 maxv /*
149 1.1 maxv * Process has trashed its stack; give it an illegal
150 1.1 maxv * instruction to halt it in its tracks.
151 1.1 maxv */
152 1.1 maxv sigexit(l, SIGILL);
153 1.1 maxv /* NOTREACHED */
154 1.1 maxv }
155 1.1 maxv
156 1.1 maxv buildcontext(l, GUCODEBIG_SEL, p->p_sigctx.ps_sigcode, fp);
157 1.1 maxv
158 1.1 maxv /* Remember that we're now on the signal stack. */
159 1.1 maxv if (onstack)
160 1.1 maxv l->l_sigstk.ss_flags |= SS_ONSTACK;
161 1.1 maxv }
162 1.1 maxv
163 1.1 maxv /*
164 1.1 maxv * System call to cleanup state after a signal
165 1.1 maxv * has been taken. Reset signal mask and
166 1.1 maxv * stack state from context left by sendsig (above).
167 1.1 maxv * Return to previous pc and psl as specified by
168 1.1 maxv * context left by sendsig. Check carefully to
169 1.1 maxv * make sure that the user has not modified the
170 1.1 maxv * psl to gain improper privileges or to cause
171 1.1 maxv * a machine fault.
172 1.1 maxv */
173 1.1 maxv int
174 1.1 maxv freebsd_sys_sigreturn(struct lwp *l, const struct freebsd_sys_sigreturn_args *uap, register_t *retval)
175 1.1 maxv {
176 1.1 maxv /* {
177 1.1 maxv syscallarg(struct freebsd_sigcontext *) scp;
178 1.1 maxv } */
179 1.1 maxv struct proc *p = l->l_proc;
180 1.1 maxv struct freebsd_sigcontext *scp, context;
181 1.1 maxv struct trapframe *tf;
182 1.1 maxv sigset_t mask;
183 1.1 maxv
184 1.1 maxv /*
185 1.1 maxv * The trampoline code hands us the context.
186 1.1 maxv * It is unsafe to keep track of it ourselves, in the event that a
187 1.1 maxv * program jumps out of a signal handler.
188 1.1 maxv */
189 1.1 maxv scp = SCARG(uap, scp);
190 1.1 maxv if (copyin((void *)scp, &context, sizeof(*scp)) != 0)
191 1.1 maxv return (EFAULT);
192 1.1 maxv
193 1.1 maxv /* Restore register context. */
194 1.1 maxv tf = l->l_md.md_regs;
195 1.1 maxv #ifdef VM86
196 1.1 maxv if (context.sc_efl & PSL_VM) {
197 1.1 maxv void syscall_vm86(struct trapframe *);
198 1.1 maxv
199 1.1 maxv tf->tf_vm86_gs = context.sc_gs;
200 1.1 maxv tf->tf_vm86_fs = context.sc_fs;
201 1.1 maxv tf->tf_vm86_es = context.sc_es;
202 1.1 maxv tf->tf_vm86_ds = context.sc_ds;
203 1.1 maxv set_vflags(l, context.sc_efl);
204 1.1 maxv p->p_md.md_syscall = syscall_vm86;
205 1.1 maxv } else
206 1.1 maxv #endif
207 1.1 maxv {
208 1.1 maxv /*
209 1.1 maxv * Check for security violations. If we're returning to
210 1.1 maxv * protected mode, the CPU will validate the segment registers
211 1.1 maxv * automatically and generate a trap on violations. We handle
212 1.1 maxv * the trap, rather than doing all of the checking here.
213 1.1 maxv */
214 1.1 maxv if (((context.sc_efl ^ tf->tf_eflags) & PSL_USERSTATIC) != 0 ||
215 1.1 maxv !USERMODE(context.sc_cs, context.sc_efl))
216 1.1 maxv return (EINVAL);
217 1.1 maxv
218 1.1 maxv tf->tf_gs = context.sc_gs;
219 1.1 maxv tf->tf_fs = context.sc_fs;
220 1.1 maxv tf->tf_es = context.sc_es;
221 1.1 maxv tf->tf_ds = context.sc_ds;
222 1.1 maxv tf->tf_eflags &= ~PSL_USER;
223 1.1 maxv tf->tf_eflags |= context.sc_efl & PSL_USER;
224 1.1 maxv }
225 1.1 maxv tf->tf_edi = context.sc_edi;
226 1.1 maxv tf->tf_esi = context.sc_esi;
227 1.1 maxv tf->tf_ebp = context.sc_ebp;
228 1.1 maxv /* FreeBSD's context.sc_isp is useless. (`popal' ignores it.) */
229 1.1 maxv tf->tf_ebx = context.sc_ebx;
230 1.1 maxv tf->tf_edx = context.sc_edx;
231 1.1 maxv tf->tf_ecx = context.sc_ecx;
232 1.1 maxv tf->tf_eax = context.sc_eax;
233 1.1 maxv tf->tf_eip = context.sc_eip;
234 1.1 maxv tf->tf_cs = context.sc_cs;
235 1.1 maxv tf->tf_esp = context.sc_esp;
236 1.1 maxv tf->tf_ss = context.sc_ss;
237 1.1 maxv
238 1.1 maxv mutex_enter(p->p_lock);
239 1.1 maxv /* Restore signal stack. */
240 1.1 maxv if (context.sc_onstack & SS_ONSTACK)
241 1.1 maxv l->l_sigstk.ss_flags |= SS_ONSTACK;
242 1.1 maxv else
243 1.1 maxv l->l_sigstk.ss_flags &= ~SS_ONSTACK;
244 1.1 maxv /* Restore signal mask. */
245 1.1 maxv /* XXX freebsd_osigcontext compat? */
246 1.1 maxv mask = context.sc_mask;
247 1.1 maxv (void) sigprocmask1(l, SIG_SETMASK, &mask, 0);
248 1.1 maxv mutex_exit(p->p_lock);
249 1.1 maxv
250 1.1 maxv return (EJUSTRETURN);
251 1.1 maxv }
252 1.1 maxv
253