1 /* $NetBSD: arm32_machdep.c,v 1.44 2004/03/24 15:34:47 atatat Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: BSD-4-Clause
5 *
6 * Copyright (c) 2004 Olivier Houchard
7 * Copyright (c) 1994-1998 Mark Brinicombe.
8 * Copyright (c) 1994 Brini.
9 * All rights reserved.
10 *
11 * This code is derived from software written for Brini by Mark Brinicombe
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by Mark Brinicombe
24 * for the NetBSD Project.
25 * 4. The name of the company nor the name of the author may be used to
26 * endorse or promote products derived from this software without specific
27 * prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 */
41
42 #include <sys/param.h>
43 #include <sys/exec.h>
44 #include <sys/imgact.h>
45 #include <sys/kdb.h>
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/lock.h>
49 #include <sys/mutex.h>
50 #include <sys/proc.h>
51 #include <sys/rwlock.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/vmmeter.h>
56
57 #include <machine/asm.h>
58 #include <machine/machdep.h>
59 #include <machine/pcb.h>
60 #include <machine/sysarch.h>
61 #include <machine/vfp.h>
62 #include <machine/vmparam.h>
63
64 #include <vm/vm.h>
65 #include <vm/vm_param.h>
66 #include <vm/pmap.h>
67 #include <vm/vm_map.h>
68
69 _Static_assert(sizeof(mcontext_t) == 208, "mcontext_t size incorrect");
70 _Static_assert(sizeof(ucontext_t) == 260, "ucontext_t size incorrect");
71 _Static_assert(sizeof(siginfo_t) == 64, "siginfo_t size incorrect");
72
73 /*
74 * Clear registers on exec
75 */
76 void
exec_setregs(struct thread * td,struct image_params * imgp,uintptr_t stack)77 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
78 {
79 struct trapframe *tf = td->td_frame;
80
81 memset(tf, 0, sizeof(*tf));
82 tf->tf_usr_sp = stack;
83 tf->tf_usr_lr = imgp->entry_addr;
84 tf->tf_svc_lr = 0x77777777;
85 tf->tf_pc = imgp->entry_addr;
86 tf->tf_spsr = PSR_USR32_MODE;
87 if ((register_t)imgp->entry_addr & 1)
88 tf->tf_spsr |= PSR_T;
89 }
90
91 #ifdef VFP
92 /*
93 * Get machine VFP context.
94 */
95 void
get_vfpcontext(struct thread * td,mcontext_vfp_t * vfp)96 get_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
97 {
98 struct pcb *pcb;
99
100 MPASS(td == curthread || TD_IS_SUSPENDED(td) ||
101 P_SHOULDSTOP(td->td_proc));
102
103 pcb = td->td_pcb;
104 if ((pcb->pcb_fpflags & PCB_FP_STARTED) != 0 && td == curthread) {
105 critical_enter();
106 vfp_store(&pcb->pcb_vfpstate, false);
107 critical_exit();
108 }
109 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate,
110 ("Called get_vfpcontext while the kernel is using the VFP"));
111 memcpy(vfp, &pcb->pcb_vfpstate, sizeof(*vfp));
112 }
113
114 /*
115 * Set machine VFP context.
116 */
117 void
set_vfpcontext(struct thread * td,mcontext_vfp_t * vfp)118 set_vfpcontext(struct thread *td, mcontext_vfp_t *vfp)
119 {
120 struct pcb *pcb;
121
122 pcb = td->td_pcb;
123 if (td == curthread) {
124 critical_enter();
125 vfp_discard(td);
126 critical_exit();
127 }
128 KASSERT(pcb->pcb_vfpsaved == &pcb->pcb_vfpstate,
129 ("Called set_vfpcontext while the kernel is using the VFP"));
130 memcpy(&pcb->pcb_vfpstate, vfp, sizeof(*vfp));
131 }
132 #endif
133
134 int
arm_get_vfpstate(struct thread * td,void * args)135 arm_get_vfpstate(struct thread *td, void *args)
136 {
137 int rv;
138 struct arm_get_vfpstate_args ua;
139 mcontext_vfp_t mcontext_vfp;
140
141 rv = copyin(args, &ua, sizeof(ua));
142 if (rv != 0)
143 return (rv);
144 if (ua.mc_vfp_size != sizeof(mcontext_vfp_t))
145 return (EINVAL);
146 #ifdef VFP
147 get_vfpcontext(td, &mcontext_vfp);
148 #else
149 bzero(&mcontext_vfp, sizeof(mcontext_vfp));
150 #endif
151
152 rv = copyout(&mcontext_vfp, ua.mc_vfp, sizeof(mcontext_vfp));
153 if (rv != 0)
154 return (rv);
155 return (0);
156 }
157
158 /*
159 * Get machine context.
160 */
161 int
get_mcontext(struct thread * td,mcontext_t * mcp,int clear_ret)162 get_mcontext(struct thread *td, mcontext_t *mcp, int clear_ret)
163 {
164 struct trapframe *tf = td->td_frame;
165 __greg_t *gr = mcp->__gregs;
166 mcontext_vfp_t mcontext_vfp;
167 int rv;
168
169 if (clear_ret & GET_MC_CLEAR_RET) {
170 gr[_REG_R0] = 0;
171 gr[_REG_CPSR] = tf->tf_spsr & ~PSR_C;
172 } else {
173 gr[_REG_R0] = tf->tf_r0;
174 gr[_REG_CPSR] = tf->tf_spsr;
175 }
176 gr[_REG_R1] = tf->tf_r1;
177 gr[_REG_R2] = tf->tf_r2;
178 gr[_REG_R3] = tf->tf_r3;
179 gr[_REG_R4] = tf->tf_r4;
180 gr[_REG_R5] = tf->tf_r5;
181 gr[_REG_R6] = tf->tf_r6;
182 gr[_REG_R7] = tf->tf_r7;
183 gr[_REG_R8] = tf->tf_r8;
184 gr[_REG_R9] = tf->tf_r9;
185 gr[_REG_R10] = tf->tf_r10;
186 gr[_REG_R11] = tf->tf_r11;
187 gr[_REG_R12] = tf->tf_r12;
188 gr[_REG_SP] = tf->tf_usr_sp;
189 gr[_REG_LR] = tf->tf_usr_lr;
190 gr[_REG_PC] = tf->tf_pc;
191
192 #ifdef VFP
193 if (mcp->mc_vfp_size != sizeof(mcontext_vfp_t))
194 return (EINVAL);
195 get_vfpcontext(td, &mcontext_vfp);
196 #else
197 bzero(&mcontext_vfp, sizeof(mcontext_vfp));
198 #endif
199
200 if (mcp->mc_vfp_ptr != NULL) {
201 rv = copyout(&mcontext_vfp, mcp->mc_vfp_ptr, sizeof(mcontext_vfp));
202 if (rv != 0)
203 return (rv);
204 }
205
206 return (0);
207 }
208
209 /*
210 * Set machine context.
211 *
212 * However, we don't set any but the user modifiable flags, and we won't
213 * touch the cs selector.
214 */
215 int
set_mcontext(struct thread * td,mcontext_t * mcp)216 set_mcontext(struct thread *td, mcontext_t *mcp)
217 {
218 mcontext_vfp_t mc_vfp, *vfp;
219 struct trapframe *tf = td->td_frame;
220 const __greg_t *gr = mcp->__gregs;
221 int spsr;
222
223 /*
224 * Make sure the processor mode has not been tampered with and
225 * interrupts have not been disabled.
226 */
227 spsr = gr[_REG_CPSR];
228 if ((spsr & PSR_MODE) != PSR_USR32_MODE ||
229 (spsr & (PSR_I | PSR_F)) != 0)
230 return (EINVAL);
231
232 #ifdef WITNESS
233 if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_size != sizeof(mc_vfp)) {
234 printf("%s: %s: Malformed mc_vfp_size: %d (0x%08X)\n",
235 td->td_proc->p_comm, __func__,
236 mcp->mc_vfp_size, mcp->mc_vfp_size);
237 } else if (mcp->mc_vfp_size != 0 && mcp->mc_vfp_ptr == NULL) {
238 printf("%s: %s: c_vfp_size != 0 but mc_vfp_ptr == NULL\n",
239 td->td_proc->p_comm, __func__);
240 }
241 #endif
242
243 if (mcp->mc_vfp_size == sizeof(mc_vfp) && mcp->mc_vfp_ptr != NULL) {
244 if (copyin(mcp->mc_vfp_ptr, &mc_vfp, sizeof(mc_vfp)) != 0)
245 return (EFAULT);
246 vfp = &mc_vfp;
247 } else {
248 vfp = NULL;
249 }
250
251 tf->tf_r0 = gr[_REG_R0];
252 tf->tf_r1 = gr[_REG_R1];
253 tf->tf_r2 = gr[_REG_R2];
254 tf->tf_r3 = gr[_REG_R3];
255 tf->tf_r4 = gr[_REG_R4];
256 tf->tf_r5 = gr[_REG_R5];
257 tf->tf_r6 = gr[_REG_R6];
258 tf->tf_r7 = gr[_REG_R7];
259 tf->tf_r8 = gr[_REG_R8];
260 tf->tf_r9 = gr[_REG_R9];
261 tf->tf_r10 = gr[_REG_R10];
262 tf->tf_r11 = gr[_REG_R11];
263 tf->tf_r12 = gr[_REG_R12];
264 tf->tf_usr_sp = gr[_REG_SP];
265 tf->tf_usr_lr = gr[_REG_LR];
266 tf->tf_pc = gr[_REG_PC];
267 tf->tf_spsr = gr[_REG_CPSR];
268 #ifdef VFP
269 if (vfp != NULL)
270 set_vfpcontext(td, vfp);
271 #endif
272 return (0);
273 }
274
275 void
sendsig(sig_t catcher,ksiginfo_t * ksi,sigset_t * mask)276 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
277 {
278 struct thread *td;
279 struct proc *p;
280 struct trapframe *tf;
281 struct sigframe *fp, frame;
282 struct sigacts *psp;
283 struct sysentvec *sysent;
284 int onstack;
285 int sig;
286
287 td = curthread;
288 p = td->td_proc;
289 PROC_LOCK_ASSERT(p, MA_OWNED);
290 sig = ksi->ksi_signo;
291 psp = p->p_sigacts;
292 mtx_assert(&psp->ps_mtx, MA_OWNED);
293 tf = td->td_frame;
294 onstack = sigonstack(tf->tf_usr_sp);
295
296 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
297 catcher, sig);
298
299 /* Allocate and validate space for the signal handler context. */
300 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !(onstack) &&
301 SIGISMEMBER(psp->ps_sigonstack, sig)) {
302 fp = (struct sigframe *)((uintptr_t)td->td_sigstk.ss_sp +
303 td->td_sigstk.ss_size);
304 #if defined(COMPAT_43)
305 td->td_sigstk.ss_flags |= SS_ONSTACK;
306 #endif
307 } else
308 fp = (struct sigframe *)td->td_frame->tf_usr_sp;
309
310 /* make room on the stack */
311 fp--;
312
313 /* make the stack aligned */
314 fp = (struct sigframe *)STACKALIGN(fp);
315 /* Populate the siginfo frame. */
316 bzero(&frame, sizeof(frame));
317 get_mcontext(td, &frame.sf_uc.uc_mcontext, 0);
318 frame.sf_si = ksi->ksi_info;
319 frame.sf_uc.uc_sigmask = *mask;
320 frame.sf_uc.uc_stack = td->td_sigstk;
321 frame.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK) != 0 ?
322 (onstack ? SS_ONSTACK : 0) : SS_DISABLE;
323 mtx_unlock(&psp->ps_mtx);
324 PROC_UNLOCK(td->td_proc);
325
326 /* Copy the sigframe out to the user's stack. */
327 if (copyout(&frame, fp, sizeof(*fp)) != 0) {
328 /* Process has trashed its stack. Kill it. */
329 CTR2(KTR_SIG, "sendsig: sigexit td=%p fp=%p", td, fp);
330 PROC_LOCK(p);
331 sigexit(td, SIGILL);
332 }
333
334 /*
335 * Build context to run handler in. We invoke the handler
336 * directly, only returning via the trampoline. Note the
337 * trampoline version numbers are coordinated with machine-
338 * dependent code in libc.
339 */
340
341 tf->tf_r0 = sig;
342 tf->tf_r1 = (register_t)&fp->sf_si;
343 tf->tf_r2 = (register_t)&fp->sf_uc;
344
345 /* the trampoline uses r5 as the uc address */
346 tf->tf_r5 = (register_t)&fp->sf_uc;
347 tf->tf_pc = (register_t)catcher;
348 tf->tf_usr_sp = (register_t)fp;
349 sysent = p->p_sysent;
350 if (PROC_HAS_SHP(p))
351 tf->tf_usr_lr = (register_t)PROC_SIGCODE(p);
352 else
353 tf->tf_usr_lr = (register_t)(PROC_PS_STRINGS(p) -
354 *(sysent->sv_szsigcode));
355 /* Set the mode to enter in the signal handler */
356 if ((register_t)catcher & 1)
357 tf->tf_spsr |= PSR_T;
358 else
359 tf->tf_spsr &= ~PSR_T;
360
361 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td, tf->tf_usr_lr,
362 tf->tf_usr_sp);
363
364 PROC_LOCK(p);
365 mtx_lock(&psp->ps_mtx);
366 }
367
368 int
sys_sigreturn(struct thread * td,struct sigreturn_args * uap)369 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
370 {
371 ucontext_t uc;
372 int error;
373
374 if (uap == NULL)
375 return (EFAULT);
376 if (copyin(uap->sigcntxp, &uc, sizeof(uc)))
377 return (EFAULT);
378 /* Restore register context. */
379 error = set_mcontext(td, &uc.uc_mcontext);
380 if (error != 0)
381 return (error);
382
383 /* Restore signal mask. */
384 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
385
386 return (EJUSTRETURN);
387 }
388