xref: /freebsd/sys/powerpc/powerpc/exec_machdep.c (revision e17f5b1d)
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*-
34  * Copyright (C) 2001 Benno Rice
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  *	$NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
57  */
58 
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61 
62 #include "opt_fpu_emu.h"
63 
64 #include <sys/param.h>
65 #include <sys/proc.h>
66 #include <sys/systm.h>
67 #include <sys/bio.h>
68 #include <sys/buf.h>
69 #include <sys/bus.h>
70 #include <sys/cons.h>
71 #include <sys/cpu.h>
72 #include <sys/exec.h>
73 #include <sys/imgact.h>
74 #include <sys/kernel.h>
75 #include <sys/ktr.h>
76 #include <sys/lock.h>
77 #include <sys/malloc.h>
78 #include <sys/mutex.h>
79 #include <sys/signalvar.h>
80 #include <sys/syscallsubr.h>
81 #include <sys/syscall.h>
82 #include <sys/sysent.h>
83 #include <sys/sysproto.h>
84 #include <sys/ucontext.h>
85 #include <sys/uio.h>
86 
87 #include <machine/altivec.h>
88 #include <machine/cpu.h>
89 #include <machine/elf.h>
90 #include <machine/fpu.h>
91 #include <machine/pcb.h>
92 #include <machine/reg.h>
93 #include <machine/sigframe.h>
94 #include <machine/trap.h>
95 #include <machine/vmparam.h>
96 
97 #include <vm/pmap.h>
98 
99 #ifdef FPU_EMU
100 #include <powerpc/fpu/fpu_extern.h>
101 #endif
102 
103 #ifdef COMPAT_FREEBSD32
104 #include <compat/freebsd32/freebsd32_signal.h>
105 #include <compat/freebsd32/freebsd32_util.h>
106 #include <compat/freebsd32/freebsd32_proto.h>
107 
108 typedef struct __ucontext32 {
109 	sigset_t		uc_sigmask;
110 	mcontext32_t		uc_mcontext;
111 	uint32_t		uc_link;
112 	struct sigaltstack32    uc_stack;
113 	uint32_t		uc_flags;
114 	uint32_t		__spare__[4];
115 } ucontext32_t;
116 
117 struct sigframe32 {
118 	ucontext32_t		sf_uc;
119 	struct siginfo32	sf_si;
120 };
121 
122 static int	grab_mcontext32(struct thread *td, mcontext32_t *, int flags);
123 #endif
124 
125 static int	grab_mcontext(struct thread *, mcontext_t *, int);
126 
127 static void	cleanup_power_extras(struct thread *);
128 
129 #ifdef __powerpc64__
130 extern struct sysentvec elf64_freebsd_sysvec_v2;
131 #endif
132 
133 void
134 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
135 {
136 	struct trapframe *tf;
137 	struct sigacts *psp;
138 	struct sigframe sf;
139 	struct thread *td;
140 	struct proc *p;
141 	#ifdef COMPAT_FREEBSD32
142 	struct siginfo32 siginfo32;
143 	struct sigframe32 sf32;
144 	#endif
145 	size_t sfpsize;
146 	caddr_t sfp, usfp;
147 	register_t sp;
148 	int oonstack, rndfsize;
149 	int sig;
150 	int code;
151 
152 	td = curthread;
153 	p = td->td_proc;
154 	PROC_LOCK_ASSERT(p, MA_OWNED);
155 
156 	psp = p->p_sigacts;
157 	mtx_assert(&psp->ps_mtx, MA_OWNED);
158 	tf = td->td_frame;
159 
160 	/*
161 	 * Fill siginfo structure.
162 	 */
163 	ksi->ksi_info.si_signo = ksi->ksi_signo;
164 	ksi->ksi_info.si_addr =
165 	    (void *)((tf->exc == EXC_DSI || tf->exc == EXC_DSE) ?
166 	    tf->dar : tf->srr0);
167 
168 	#ifdef COMPAT_FREEBSD32
169 	if (SV_PROC_FLAG(p, SV_ILP32)) {
170 		siginfo_to_siginfo32(&ksi->ksi_info, &siginfo32);
171 		sig = siginfo32.si_signo;
172 		code = siginfo32.si_code;
173 		sfp = (caddr_t)&sf32;
174 		sfpsize = sizeof(sf32);
175 		rndfsize = roundup(sizeof(sf32), 16);
176 		sp = (uint32_t)tf->fixreg[1];
177 		oonstack = sigonstack(sp);
178 
179 		/*
180 		 * Save user context
181 		 */
182 
183 		memset(&sf32, 0, sizeof(sf32));
184 		grab_mcontext32(td, &sf32.sf_uc.uc_mcontext, 0);
185 
186 		sf32.sf_uc.uc_sigmask = *mask;
187 		sf32.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
188 		sf32.sf_uc.uc_stack.ss_size = (uint32_t)td->td_sigstk.ss_size;
189 		sf32.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
190 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
191 
192 		sf32.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
193 	} else {
194 	#endif
195 		sig = ksi->ksi_signo;
196 		code = ksi->ksi_code;
197 		sfp = (caddr_t)&sf;
198 		sfpsize = sizeof(sf);
199 		#ifdef __powerpc64__
200 		/*
201 		 * 64-bit PPC defines a 288 byte scratch region
202 		 * below the stack.
203 		 */
204 		rndfsize = 288 + roundup(sizeof(sf), 48);
205 		#else
206 		rndfsize = roundup(sizeof(sf), 16);
207 		#endif
208 		sp = tf->fixreg[1];
209 		oonstack = sigonstack(sp);
210 
211 		/*
212 		 * Save user context
213 		 */
214 
215 		memset(&sf, 0, sizeof(sf));
216 		grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
217 
218 		sf.sf_uc.uc_sigmask = *mask;
219 		sf.sf_uc.uc_stack = td->td_sigstk;
220 		sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
221 		    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
222 
223 		sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
224 	#ifdef COMPAT_FREEBSD32
225 	}
226 	#endif
227 
228 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
229 	     catcher, sig);
230 
231 	/*
232 	 * Allocate and validate space for the signal handler context.
233 	 */
234 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
235 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
236 		usfp = (void *)(((uintptr_t)td->td_sigstk.ss_sp +
237 		   td->td_sigstk.ss_size - rndfsize) & ~0xFul);
238 	} else {
239 		usfp = (void *)((sp - rndfsize) & ~0xFul);
240 	}
241 
242 	/*
243 	 * Save the floating-point state, if necessary, then copy it.
244 	 */
245 	/* XXX */
246 
247 	/*
248 	 * Set up the registers to return to sigcode.
249 	 *
250 	 *   r1/sp - sigframe ptr
251 	 *   lr    - sig function, dispatched to by blrl in trampoline
252 	 *   r3    - sig number
253 	 *   r4    - SIGINFO ? &siginfo : exception code
254 	 *   r5    - user context
255 	 *   srr0  - trampoline function addr
256 	 */
257 	tf->lr = (register_t)catcher;
258 	tf->fixreg[1] = (register_t)usfp;
259 	tf->fixreg[FIRSTARG] = sig;
260 	#ifdef COMPAT_FREEBSD32
261 	tf->fixreg[FIRSTARG+2] = (register_t)usfp +
262 	    ((SV_PROC_FLAG(p, SV_ILP32)) ?
263 	    offsetof(struct sigframe32, sf_uc) :
264 	    offsetof(struct sigframe, sf_uc));
265 	#else
266 	tf->fixreg[FIRSTARG+2] = (register_t)usfp +
267 	    offsetof(struct sigframe, sf_uc);
268 	#endif
269 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
270 		/*
271 		 * Signal handler installed with SA_SIGINFO.
272 		 */
273 		#ifdef COMPAT_FREEBSD32
274 		if (SV_PROC_FLAG(p, SV_ILP32)) {
275 			sf32.sf_si = siginfo32;
276 			tf->fixreg[FIRSTARG+1] = (register_t)usfp +
277 			    offsetof(struct sigframe32, sf_si);
278 			sf32.sf_si = siginfo32;
279 		} else  {
280 		#endif
281 			tf->fixreg[FIRSTARG+1] = (register_t)usfp +
282 			    offsetof(struct sigframe, sf_si);
283 			sf.sf_si = ksi->ksi_info;
284 		#ifdef COMPAT_FREEBSD32
285 		}
286 		#endif
287 	} else {
288 		/* Old FreeBSD-style arguments. */
289 		tf->fixreg[FIRSTARG+1] = code;
290 		tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ?
291 		    tf->dar : tf->srr0;
292 	}
293 	mtx_unlock(&psp->ps_mtx);
294 	PROC_UNLOCK(p);
295 
296 	tf->srr0 = (register_t)p->p_sysent->sv_sigcode_base;
297 
298 	/*
299 	 * copy the frame out to userland.
300 	 */
301 	if (copyout(sfp, usfp, sfpsize) != 0) {
302 		/*
303 		 * Process has trashed its stack. Kill it.
304 		 */
305 		CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
306 		PROC_LOCK(p);
307 		sigexit(td, SIGILL);
308 	}
309 
310 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
311 	     tf->srr0, tf->fixreg[1]);
312 
313 	PROC_LOCK(p);
314 	mtx_lock(&psp->ps_mtx);
315 }
316 
317 int
318 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
319 {
320 	ucontext_t uc;
321 	int error;
322 
323 	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
324 
325 	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
326 		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
327 		return (EFAULT);
328 	}
329 
330 	error = set_mcontext(td, &uc.uc_mcontext);
331 	if (error != 0)
332 		return (error);
333 
334 	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
335 
336 	CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
337 	     td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
338 
339 	return (EJUSTRETURN);
340 }
341 
342 #ifdef COMPAT_FREEBSD4
343 int
344 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
345 {
346 
347 	return sys_sigreturn(td, (struct sigreturn_args *)uap);
348 }
349 #endif
350 
351 /*
352  * Construct a PCB from a trapframe. This is called from kdb_trap() where
353  * we want to start a backtrace from the function that caused us to enter
354  * the debugger. We have the context in the trapframe, but base the trace
355  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
356  * enough for a backtrace.
357  */
358 void
359 makectx(struct trapframe *tf, struct pcb *pcb)
360 {
361 
362 	pcb->pcb_lr = tf->srr0;
363 	pcb->pcb_sp = tf->fixreg[1];
364 }
365 
366 /*
367  * get_mcontext/sendsig helper routine that doesn't touch the
368  * proc lock
369  */
370 static int
371 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
372 {
373 	struct pcb *pcb;
374 	int i;
375 
376 	pcb = td->td_pcb;
377 
378 	memset(mcp, 0, sizeof(mcontext_t));
379 
380 	mcp->mc_vers = _MC_VERSION;
381 	mcp->mc_flags = 0;
382 	memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
383 	if (flags & GET_MC_CLEAR_RET) {
384 		mcp->mc_gpr[3] = 0;
385 		mcp->mc_gpr[4] = 0;
386 	}
387 
388 	/*
389 	 * This assumes that floating-point context is *not* lazy,
390 	 * so if the thread has used FP there would have been a
391 	 * FP-unavailable exception that would have set things up
392 	 * correctly.
393 	 */
394 	if (pcb->pcb_flags & PCB_FPREGS) {
395 		if (pcb->pcb_flags & PCB_FPU) {
396 			KASSERT(td == curthread,
397 				("get_mcontext: fp save not curthread"));
398 			critical_enter();
399 			save_fpu(td);
400 			critical_exit();
401 		}
402 		mcp->mc_flags |= _MC_FP_VALID;
403 		memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
404 		for (i = 0; i < 32; i++)
405 			memcpy(&mcp->mc_fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
406 			    sizeof(double));
407 	}
408 
409 	if (pcb->pcb_flags & PCB_VSX) {
410 		for (i = 0; i < 32; i++)
411 			memcpy(&mcp->mc_vsxfpreg[i],
412 			    &pcb->pcb_fpu.fpr[i].vsr[2], sizeof(double));
413 	}
414 
415 	/*
416 	 * Repeat for Altivec context
417 	 */
418 
419 	if (pcb->pcb_flags & PCB_VEC) {
420 		KASSERT(td == curthread,
421 			("get_mcontext: fp save not curthread"));
422 		critical_enter();
423 		save_vec(td);
424 		critical_exit();
425 		mcp->mc_flags |= _MC_AV_VALID;
426 		mcp->mc_vscr  = pcb->pcb_vec.vscr;
427 		mcp->mc_vrsave =  pcb->pcb_vec.vrsave;
428 		memcpy(mcp->mc_avec, pcb->pcb_vec.vr, sizeof(mcp->mc_avec));
429 	}
430 
431 	mcp->mc_len = sizeof(*mcp);
432 
433 	return (0);
434 }
435 
436 int
437 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
438 {
439 	int error;
440 
441 	error = grab_mcontext(td, mcp, flags);
442 	if (error == 0) {
443 		PROC_LOCK(curthread->td_proc);
444 		mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
445 		PROC_UNLOCK(curthread->td_proc);
446 	}
447 
448 	return (error);
449 }
450 
451 int
452 set_mcontext(struct thread *td, mcontext_t *mcp)
453 {
454 	struct pcb *pcb;
455 	struct trapframe *tf;
456 	register_t tls;
457 	int i;
458 
459 	pcb = td->td_pcb;
460 	tf = td->td_frame;
461 
462 	if (mcp->mc_vers != _MC_VERSION || mcp->mc_len != sizeof(*mcp))
463 		return (EINVAL);
464 
465 	/*
466 	 * Don't let the user change privileged MSR bits.
467 	 *
468 	 * psl_userstatic is used here to mask off any bits that can
469 	 * legitimately vary between user contexts (Floating point
470 	 * exception control and any facilities that we are using the
471 	 * "enable on first use" pattern with.)
472 	 *
473 	 * All other bits are required to match psl_userset(32).
474 	 *
475 	 * Remember to update the platform cpu_init code when implementing
476 	 * support for a new conditional facility!
477 	 */
478 	if ((mcp->mc_srr1 & psl_userstatic) != (tf->srr1 & psl_userstatic)) {
479 		return (EINVAL);
480 	}
481 
482 	/* Copy trapframe, preserving TLS pointer across context change */
483 	if (SV_PROC_FLAG(td->td_proc, SV_LP64))
484 		tls = tf->fixreg[13];
485 	else
486 		tls = tf->fixreg[2];
487 	memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
488 	if (SV_PROC_FLAG(td->td_proc, SV_LP64))
489 		tf->fixreg[13] = tls;
490 	else
491 		tf->fixreg[2] = tls;
492 
493 	/*
494 	 * Force the FPU back off to ensure the new context will not bypass
495 	 * the enable_fpu() setup code accidentally.
496 	 *
497 	 * This prevents an issue where a process that uses floating point
498 	 * inside a signal handler could end up in a state where the MSR
499 	 * did not match pcb_flags.
500 	 *
501 	 * Additionally, ensure VSX is disabled as well, as it is illegal
502 	 * to leave it turned on when FP or VEC are off.
503 	 */
504 	tf->srr1 &= ~(PSL_FP | PSL_VSX);
505 	pcb->pcb_flags &= ~(PCB_FPU | PCB_VSX);
506 
507 	if (mcp->mc_flags & _MC_FP_VALID) {
508 		/* enable_fpu() will happen lazily on a fault */
509 		pcb->pcb_flags |= PCB_FPREGS;
510 		memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
511 		bzero(pcb->pcb_fpu.fpr, sizeof(pcb->pcb_fpu.fpr));
512 		for (i = 0; i < 32; i++) {
513 			memcpy(&pcb->pcb_fpu.fpr[i].fpr, &mcp->mc_fpreg[i],
514 			    sizeof(double));
515 			memcpy(&pcb->pcb_fpu.fpr[i].vsr[2],
516 			    &mcp->mc_vsxfpreg[i], sizeof(double));
517 		}
518 	}
519 
520 	if (mcp->mc_flags & _MC_AV_VALID) {
521 		if ((pcb->pcb_flags & PCB_VEC) != PCB_VEC) {
522 			critical_enter();
523 			enable_vec(td);
524 			critical_exit();
525 		}
526 		pcb->pcb_vec.vscr = mcp->mc_vscr;
527 		pcb->pcb_vec.vrsave = mcp->mc_vrsave;
528 		memcpy(pcb->pcb_vec.vr, mcp->mc_avec, sizeof(mcp->mc_avec));
529 	} else {
530 		tf->srr1 &= ~PSL_VEC;
531 		pcb->pcb_flags &= ~PCB_VEC;
532 	}
533 
534 	return (0);
535 }
536 
537 /*
538  * Clean up extra POWER state.  Some per-process registers and states are not
539  * managed by the MSR, so must be cleaned up explicitly on thread exit.
540  *
541  * Currently this includes:
542  * DSCR -- Data stream control register (PowerISA 2.06+)
543  * FSCR -- Facility Status and Control Register (PowerISA 2.07+)
544  */
545 static void
546 cleanup_power_extras(struct thread *td)
547 {
548 	uint32_t pcb_flags;
549 
550 	if (td != curthread)
551 		return;
552 
553 	pcb_flags = td->td_pcb->pcb_flags;
554 	/* Clean up registers not managed by MSR. */
555 	if (pcb_flags & PCB_CFSCR)
556 		mtspr(SPR_FSCR, 0);
557 	if (pcb_flags & PCB_CDSCR)
558 		mtspr(SPR_DSCRP, 0);
559 }
560 
561 /*
562  * Set set up registers on exec.
563  */
564 void
565 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
566 {
567 	struct trapframe	*tf;
568 	register_t		argc;
569 
570 	tf = trapframe(td);
571 	bzero(tf, sizeof *tf);
572 	#ifdef __powerpc64__
573 	tf->fixreg[1] = -roundup(-stack + 48, 16);
574 	#else
575 	tf->fixreg[1] = -roundup(-stack + 8, 16);
576 	#endif
577 
578 	/*
579 	 * Set up arguments for _start():
580 	 *	_start(argc, argv, envp, obj, cleanup, ps_strings);
581 	 *
582 	 * Notes:
583 	 *	- obj and cleanup are the auxilliary and termination
584 	 *	  vectors.  They are fixed up by ld.elf_so.
585 	 *	- ps_strings is a NetBSD extention, and will be
586 	 * 	  ignored by executables which are strictly
587 	 *	  compliant with the SVR4 ABI.
588 	 */
589 
590 	/* Collect argc from the user stack */
591 	argc = fuword((void *)stack);
592 
593 	tf->fixreg[3] = argc;
594 	tf->fixreg[4] = stack + sizeof(register_t);
595 	tf->fixreg[5] = stack + (2 + argc)*sizeof(register_t);
596 	tf->fixreg[6] = 0;				/* auxillary vector */
597 	tf->fixreg[7] = 0;				/* termination vector */
598 	tf->fixreg[8] = (register_t)imgp->ps_strings;	/* NetBSD extension */
599 
600 	tf->srr0 = imgp->entry_addr;
601 	#ifdef __powerpc64__
602 	tf->fixreg[12] = imgp->entry_addr;
603 	#endif
604 	tf->srr1 = psl_userset | PSL_FE_DFLT;
605 	cleanup_power_extras(td);
606 	td->td_pcb->pcb_flags = 0;
607 }
608 
609 #ifdef COMPAT_FREEBSD32
610 void
611 ppc32_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
612 {
613 	struct trapframe	*tf;
614 	uint32_t		argc;
615 
616 	tf = trapframe(td);
617 	bzero(tf, sizeof *tf);
618 	tf->fixreg[1] = -roundup(-stack + 8, 16);
619 
620 	argc = fuword32((void *)stack);
621 
622 	tf->fixreg[3] = argc;
623 	tf->fixreg[4] = stack + sizeof(uint32_t);
624 	tf->fixreg[5] = stack + (2 + argc)*sizeof(uint32_t);
625 	tf->fixreg[6] = 0;				/* auxillary vector */
626 	tf->fixreg[7] = 0;				/* termination vector */
627 	tf->fixreg[8] = (register_t)imgp->ps_strings;	/* NetBSD extension */
628 
629 	tf->srr0 = imgp->entry_addr;
630 	tf->srr1 = psl_userset32 | PSL_FE_DFLT;
631 	cleanup_power_extras(td);
632 	td->td_pcb->pcb_flags = 0;
633 }
634 #endif
635 
636 int
637 fill_regs(struct thread *td, struct reg *regs)
638 {
639 	struct trapframe *tf;
640 
641 	tf = td->td_frame;
642 	memcpy(regs, tf, sizeof(struct reg));
643 
644 	return (0);
645 }
646 
647 int
648 fill_dbregs(struct thread *td, struct dbreg *dbregs)
649 {
650 	/* No debug registers on PowerPC */
651 	return (ENOSYS);
652 }
653 
654 int
655 fill_fpregs(struct thread *td, struct fpreg *fpregs)
656 {
657 	struct pcb *pcb;
658 	int i;
659 
660 	pcb = td->td_pcb;
661 
662 	if ((pcb->pcb_flags & PCB_FPREGS) == 0)
663 		memset(fpregs, 0, sizeof(struct fpreg));
664 	else {
665 		memcpy(&fpregs->fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
666 		for (i = 0; i < 32; i++)
667 			memcpy(&fpregs->fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
668 			    sizeof(double));
669 	}
670 
671 	return (0);
672 }
673 
674 int
675 set_regs(struct thread *td, struct reg *regs)
676 {
677 	struct trapframe *tf;
678 
679 	tf = td->td_frame;
680 	memcpy(tf, regs, sizeof(struct reg));
681 
682 	return (0);
683 }
684 
685 int
686 set_dbregs(struct thread *td, struct dbreg *dbregs)
687 {
688 	/* No debug registers on PowerPC */
689 	return (ENOSYS);
690 }
691 
692 int
693 set_fpregs(struct thread *td, struct fpreg *fpregs)
694 {
695 	struct pcb *pcb;
696 	int i;
697 
698 	pcb = td->td_pcb;
699 	pcb->pcb_flags |= PCB_FPREGS;
700 	memcpy(&pcb->pcb_fpu.fpscr, &fpregs->fpscr, sizeof(double));
701 	for (i = 0; i < 32; i++) {
702 		memcpy(&pcb->pcb_fpu.fpr[i].fpr, &fpregs->fpreg[i],
703 		    sizeof(double));
704 	}
705 
706 	return (0);
707 }
708 
709 #ifdef COMPAT_FREEBSD32
710 int
711 set_regs32(struct thread *td, struct reg32 *regs)
712 {
713 	struct trapframe *tf;
714 	int i;
715 
716 	tf = td->td_frame;
717 	for (i = 0; i < 32; i++)
718 		tf->fixreg[i] = regs->fixreg[i];
719 	tf->lr = regs->lr;
720 	tf->cr = regs->cr;
721 	tf->xer = regs->xer;
722 	tf->ctr = regs->ctr;
723 	tf->srr0 = regs->pc;
724 
725 	return (0);
726 }
727 
728 int
729 fill_regs32(struct thread *td, struct reg32 *regs)
730 {
731 	struct trapframe *tf;
732 	int i;
733 
734 	tf = td->td_frame;
735 	for (i = 0; i < 32; i++)
736 		regs->fixreg[i] = tf->fixreg[i];
737 	regs->lr = tf->lr;
738 	regs->cr = tf->cr;
739 	regs->xer = tf->xer;
740 	regs->ctr = tf->ctr;
741 	regs->pc = tf->srr0;
742 
743 	return (0);
744 }
745 
746 static int
747 grab_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
748 {
749 	mcontext_t mcp64;
750 	int i, error;
751 
752 	error = grab_mcontext(td, &mcp64, flags);
753 	if (error != 0)
754 		return (error);
755 
756 	mcp->mc_vers = mcp64.mc_vers;
757 	mcp->mc_flags = mcp64.mc_flags;
758 	mcp->mc_onstack = mcp64.mc_onstack;
759 	mcp->mc_len = mcp64.mc_len;
760 	memcpy(mcp->mc_avec,mcp64.mc_avec,sizeof(mcp64.mc_avec));
761 	memcpy(mcp->mc_av,mcp64.mc_av,sizeof(mcp64.mc_av));
762 	for (i = 0; i < 42; i++)
763 		mcp->mc_frame[i] = mcp64.mc_frame[i];
764 	memcpy(mcp->mc_fpreg,mcp64.mc_fpreg,sizeof(mcp64.mc_fpreg));
765 	memcpy(mcp->mc_vsxfpreg,mcp64.mc_vsxfpreg,sizeof(mcp64.mc_vsxfpreg));
766 
767 	return (0);
768 }
769 
770 static int
771 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
772 {
773 	int error;
774 
775 	error = grab_mcontext32(td, mcp, flags);
776 	if (error == 0) {
777 		PROC_LOCK(curthread->td_proc);
778 		mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
779 		PROC_UNLOCK(curthread->td_proc);
780 	}
781 
782 	return (error);
783 }
784 
785 static int
786 set_mcontext32(struct thread *td, mcontext32_t *mcp)
787 {
788 	mcontext_t mcp64;
789 	int i, error;
790 
791 	mcp64.mc_vers = mcp->mc_vers;
792 	mcp64.mc_flags = mcp->mc_flags;
793 	mcp64.mc_onstack = mcp->mc_onstack;
794 	mcp64.mc_len = mcp->mc_len;
795 	memcpy(mcp64.mc_avec,mcp->mc_avec,sizeof(mcp64.mc_avec));
796 	memcpy(mcp64.mc_av,mcp->mc_av,sizeof(mcp64.mc_av));
797 	for (i = 0; i < 42; i++)
798 		mcp64.mc_frame[i] = mcp->mc_frame[i];
799 	mcp64.mc_srr1 |= (td->td_frame->srr1 & 0xFFFFFFFF00000000ULL);
800 	memcpy(mcp64.mc_fpreg,mcp->mc_fpreg,sizeof(mcp64.mc_fpreg));
801 	memcpy(mcp64.mc_vsxfpreg,mcp->mc_vsxfpreg,sizeof(mcp64.mc_vsxfpreg));
802 
803 	error = set_mcontext(td, &mcp64);
804 
805 	return (error);
806 }
807 #endif
808 
809 #ifdef COMPAT_FREEBSD32
810 int
811 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
812 {
813 	ucontext32_t uc;
814 	int error;
815 
816 	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
817 
818 	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
819 		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
820 		return (EFAULT);
821 	}
822 
823 	error = set_mcontext32(td, &uc.uc_mcontext);
824 	if (error != 0)
825 		return (error);
826 
827 	kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
828 
829 	CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
830 	     td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
831 
832 	return (EJUSTRETURN);
833 }
834 
835 /*
836  * The first two fields of a ucontext_t are the signal mask and the machine
837  * context.  The next field is uc_link; we want to avoid destroying the link
838  * when copying out contexts.
839  */
840 #define	UC32_COPY_SIZE	offsetof(ucontext32_t, uc_link)
841 
842 int
843 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
844 {
845 	ucontext32_t uc;
846 	int ret;
847 
848 	if (uap->ucp == NULL)
849 		ret = EINVAL;
850 	else {
851 		bzero(&uc, sizeof(uc));
852 		get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
853 		PROC_LOCK(td->td_proc);
854 		uc.uc_sigmask = td->td_sigmask;
855 		PROC_UNLOCK(td->td_proc);
856 		ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
857 	}
858 	return (ret);
859 }
860 
861 int
862 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
863 {
864 	ucontext32_t uc;
865 	int ret;
866 
867 	if (uap->ucp == NULL)
868 		ret = EINVAL;
869 	else {
870 		ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
871 		if (ret == 0) {
872 			ret = set_mcontext32(td, &uc.uc_mcontext);
873 			if (ret == 0) {
874 				kern_sigprocmask(td, SIG_SETMASK,
875 				    &uc.uc_sigmask, NULL, 0);
876 			}
877 		}
878 	}
879 	return (ret == 0 ? EJUSTRETURN : ret);
880 }
881 
882 int
883 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
884 {
885 	ucontext32_t uc;
886 	int ret;
887 
888 	if (uap->oucp == NULL || uap->ucp == NULL)
889 		ret = EINVAL;
890 	else {
891 		bzero(&uc, sizeof(uc));
892 		get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
893 		PROC_LOCK(td->td_proc);
894 		uc.uc_sigmask = td->td_sigmask;
895 		PROC_UNLOCK(td->td_proc);
896 		ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
897 		if (ret == 0) {
898 			ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
899 			if (ret == 0) {
900 				ret = set_mcontext32(td, &uc.uc_mcontext);
901 				if (ret == 0) {
902 					kern_sigprocmask(td, SIG_SETMASK,
903 					    &uc.uc_sigmask, NULL, 0);
904 				}
905 			}
906 		}
907 	}
908 	return (ret == 0 ? EJUSTRETURN : ret);
909 }
910 
911 #endif
912 
913 void
914 cpu_set_syscall_retval(struct thread *td, int error)
915 {
916 	struct proc *p;
917 	struct trapframe *tf;
918 	int fixup;
919 
920 	if (error == EJUSTRETURN)
921 		return;
922 
923 	p = td->td_proc;
924 	tf = td->td_frame;
925 
926 	if (tf->fixreg[0] == SYS___syscall &&
927 	    (SV_PROC_FLAG(p, SV_ILP32))) {
928 		int code = tf->fixreg[FIRSTARG + 1];
929 		fixup = (
930 #if defined(COMPAT_FREEBSD6) && defined(SYS_freebsd6_lseek)
931 		    code != SYS_freebsd6_lseek &&
932 #endif
933 		    code != SYS_lseek) ?  1 : 0;
934 	} else
935 		fixup = 0;
936 
937 	switch (error) {
938 	case 0:
939 		if (fixup) {
940 			/*
941 			 * 64-bit return, 32-bit syscall. Fixup byte order
942 			 */
943 			tf->fixreg[FIRSTARG] = 0;
944 			tf->fixreg[FIRSTARG + 1] = td->td_retval[0];
945 		} else {
946 			tf->fixreg[FIRSTARG] = td->td_retval[0];
947 			tf->fixreg[FIRSTARG + 1] = td->td_retval[1];
948 		}
949 		tf->cr &= ~0x10000000;		/* Unset summary overflow */
950 		break;
951 	case ERESTART:
952 		/*
953 		 * Set user's pc back to redo the system call.
954 		 */
955 		tf->srr0 -= 4;
956 		break;
957 	default:
958 		tf->fixreg[FIRSTARG] = SV_ABI_ERRNO(p, error);
959 		tf->cr |= 0x10000000;		/* Set summary overflow */
960 		break;
961 	}
962 }
963 
964 /*
965  * Threading functions
966  */
967 void
968 cpu_thread_exit(struct thread *td)
969 {
970 	cleanup_power_extras(td);
971 }
972 
973 void
974 cpu_thread_clean(struct thread *td)
975 {
976 }
977 
978 void
979 cpu_thread_alloc(struct thread *td)
980 {
981 	struct pcb *pcb;
982 
983 	pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
984 	    sizeof(struct pcb)) & ~0x2fUL);
985 	td->td_pcb = pcb;
986 	td->td_frame = (struct trapframe *)pcb - 1;
987 }
988 
989 void
990 cpu_thread_free(struct thread *td)
991 {
992 }
993 
994 int
995 cpu_set_user_tls(struct thread *td, void *tls_base)
996 {
997 
998 	if (SV_PROC_FLAG(td->td_proc, SV_LP64))
999 		td->td_frame->fixreg[13] = (register_t)tls_base + 0x7010;
1000 	else
1001 		td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
1002 	return (0);
1003 }
1004 
1005 void
1006 cpu_copy_thread(struct thread *td, struct thread *td0)
1007 {
1008 	struct pcb *pcb2;
1009 	struct trapframe *tf;
1010 	struct callframe *cf;
1011 
1012 	pcb2 = td->td_pcb;
1013 
1014 	/* Copy the upcall pcb */
1015 	bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
1016 
1017 	/* Create a stack for the new thread */
1018 	tf = td->td_frame;
1019 	bcopy(td0->td_frame, tf, sizeof(struct trapframe));
1020 	tf->fixreg[FIRSTARG] = 0;
1021 	tf->fixreg[FIRSTARG + 1] = 0;
1022 	tf->cr &= ~0x10000000;
1023 
1024 	/* Set registers for trampoline to user mode. */
1025 	cf = (struct callframe *)tf - 1;
1026 	memset(cf, 0, sizeof(struct callframe));
1027 	cf->cf_func = (register_t)fork_return;
1028 	cf->cf_arg0 = (register_t)td;
1029 	cf->cf_arg1 = (register_t)tf;
1030 
1031 	pcb2->pcb_sp = (register_t)cf;
1032 	#if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
1033 	pcb2->pcb_lr = ((register_t *)fork_trampoline)[0];
1034 	pcb2->pcb_toc = ((register_t *)fork_trampoline)[1];
1035 	#else
1036 	pcb2->pcb_lr = (register_t)fork_trampoline;
1037 	pcb2->pcb_context[0] = pcb2->pcb_lr;
1038 	#endif
1039 	pcb2->pcb_cpu.aim.usr_vsid = 0;
1040 #ifdef __SPE__
1041 	pcb2->pcb_vec.vscr = SPEFSCR_FINVE | SPEFSCR_FDBZE |
1042 	    SPEFSCR_FUNFE | SPEFSCR_FOVFE;
1043 #endif
1044 
1045 	/* Setup to release spin count in fork_exit(). */
1046 	td->td_md.md_spinlock_count = 1;
1047 	td->td_md.md_saved_msr = psl_kernset;
1048 }
1049 
1050 void
1051 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
1052     stack_t *stack)
1053 {
1054 	struct trapframe *tf;
1055 	uintptr_t sp;
1056 
1057 	tf = td->td_frame;
1058 	/* align stack and alloc space for frame ptr and saved LR */
1059 	#ifdef __powerpc64__
1060 	sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 48) &
1061 	    ~0x1f;
1062 	#else
1063 	sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 8) &
1064 	    ~0x1f;
1065 	#endif
1066 	bzero(tf, sizeof(struct trapframe));
1067 
1068 	tf->fixreg[1] = (register_t)sp;
1069 	tf->fixreg[3] = (register_t)arg;
1070 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1071 		tf->srr0 = (register_t)entry;
1072 		#ifdef __powerpc64__
1073 		tf->srr1 = psl_userset32 | PSL_FE_DFLT;
1074 		#else
1075 		tf->srr1 = psl_userset | PSL_FE_DFLT;
1076 		#endif
1077 	} else {
1078 	    #ifdef __powerpc64__
1079 		if (td->td_proc->p_sysent == &elf64_freebsd_sysvec_v2) {
1080 			tf->srr0 = (register_t)entry;
1081 			/* ELFv2 ABI requires that the global entry point be in r12. */
1082 			tf->fixreg[12] = (register_t)entry;
1083 		}
1084 		else {
1085 			register_t entry_desc[3];
1086 			(void)copyin((void *)entry, entry_desc, sizeof(entry_desc));
1087 			tf->srr0 = entry_desc[0];
1088 			tf->fixreg[2] = entry_desc[1];
1089 			tf->fixreg[11] = entry_desc[2];
1090 		}
1091 		tf->srr1 = psl_userset | PSL_FE_DFLT;
1092 	    #endif
1093 	}
1094 
1095 	td->td_pcb->pcb_flags = 0;
1096 #ifdef __SPE__
1097 	td->td_pcb->pcb_vec.vscr = SPEFSCR_FINVE | SPEFSCR_FDBZE |
1098 	    SPEFSCR_FUNFE | SPEFSCR_FOVFE;
1099 #endif
1100 
1101 	td->td_retval[0] = (register_t)entry;
1102 	td->td_retval[1] = 0;
1103 }
1104 
1105 static int
1106 emulate_mfspr(int spr, int reg, struct trapframe *frame){
1107 	struct thread *td;
1108 
1109 	td = curthread;
1110 
1111 	if (spr == SPR_DSCR || spr == SPR_DSCRP) {
1112 		if (!(cpu_features2 & PPC_FEATURE2_DSCR))
1113 			return (SIGILL);
1114 		// If DSCR was never set, get the default DSCR
1115 		if ((td->td_pcb->pcb_flags & PCB_CDSCR) == 0)
1116 			td->td_pcb->pcb_dscr = mfspr(SPR_DSCRP);
1117 
1118 		frame->fixreg[reg] = td->td_pcb->pcb_dscr;
1119 		frame->srr0 += 4;
1120 		return (0);
1121 	} else
1122 		return (SIGILL);
1123 }
1124 
1125 static int
1126 emulate_mtspr(int spr, int reg, struct trapframe *frame){
1127 	struct thread *td;
1128 
1129 	td = curthread;
1130 
1131 	if (spr == SPR_DSCR || spr == SPR_DSCRP) {
1132 		if (!(cpu_features2 & PPC_FEATURE2_DSCR))
1133 			return (SIGILL);
1134 		td->td_pcb->pcb_flags |= PCB_CDSCR;
1135 		td->td_pcb->pcb_dscr = frame->fixreg[reg];
1136 		mtspr(SPR_DSCRP, frame->fixreg[reg]);
1137 		frame->srr0 += 4;
1138 		return (0);
1139 	} else
1140 		return (SIGILL);
1141 }
1142 
1143 #define XFX 0xFC0007FF
1144 int
1145 ppc_instr_emulate(struct trapframe *frame, struct thread *td)
1146 {
1147 	struct pcb *pcb;
1148 	uint32_t instr;
1149 	int reg, sig;
1150 	int rs, spr;
1151 
1152 	instr = fuword32((void *)frame->srr0);
1153 	sig = SIGILL;
1154 
1155 	if ((instr & 0xfc1fffff) == 0x7c1f42a6) {	/* mfpvr */
1156 		reg = (instr & ~0xfc1fffff) >> 21;
1157 		frame->fixreg[reg] = mfpvr();
1158 		frame->srr0 += 4;
1159 		return (0);
1160 	} else if ((instr & XFX) == 0x7c0002a6) {	/* mfspr */
1161 		rs = (instr &  0x3e00000) >> 21;
1162 		spr = (instr & 0x1ff800) >> 16;
1163 		return emulate_mfspr(spr, rs, frame);
1164 	} else if ((instr & XFX) == 0x7c0003a6) {	/* mtspr */
1165 		rs = (instr &  0x3e00000) >> 21;
1166 		spr = (instr & 0x1ff800) >> 16;
1167 		return emulate_mtspr(spr, rs, frame);
1168 	} else if ((instr & 0xfc000ffe) == 0x7c0004ac) {	/* various sync */
1169 		powerpc_sync(); /* Do a heavy-weight sync */
1170 		frame->srr0 += 4;
1171 		return (0);
1172 	}
1173 
1174 	pcb = td->td_pcb;
1175 #ifdef FPU_EMU
1176 	if (!(pcb->pcb_flags & PCB_FPREGS)) {
1177 		bzero(&pcb->pcb_fpu, sizeof(pcb->pcb_fpu));
1178 		pcb->pcb_flags |= PCB_FPREGS;
1179 	} else if (pcb->pcb_flags & PCB_FPU)
1180 		save_fpu(td);
1181 	sig = fpu_emulate(frame, &pcb->pcb_fpu);
1182 	if ((sig == 0 || sig == SIGFPE) && pcb->pcb_flags & PCB_FPU)
1183 		enable_fpu(td);
1184 #endif
1185 	if (sig == SIGILL) {
1186 		if (pcb->pcb_lastill != frame->srr0) {
1187 			/* Allow a second chance, in case of cache sync issues. */
1188 			sig = 0;
1189 			pmap_sync_icache(PCPU_GET(curpmap), frame->srr0, 4);
1190 			pcb->pcb_lastill = frame->srr0;
1191 		}
1192 	}
1193 
1194 	return (sig);
1195 }
1196 
1197