2 * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
4 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 * Copyright (C) 1995, 1996 TooLs GmbH.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
34 * Copyright (C) 2001 Benno Rice
35 * All rights reserved.
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
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.
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 $
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #include "opt_compat.h"
63 #include "opt_fpu_emu.h"
65 #include <sys/param.h>
67 #include <sys/systm.h>
74 #include <sys/imgact.h>
75 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/mutex.h>
80 #include <sys/signalvar.h>
81 #include <sys/syscallsubr.h>
82 #include <sys/syscall.h>
83 #include <sys/sysent.h>
84 #include <sys/sysproto.h>
85 #include <sys/ucontext.h>
88 #include <machine/altivec.h>
89 #include <machine/cpu.h>
90 #include <machine/elf.h>
91 #include <machine/fpu.h>
92 #include <machine/pcb.h>
93 #include <machine/reg.h>
94 #include <machine/sigframe.h>
95 #include <machine/trap.h>
96 #include <machine/vmparam.h>
99 #include <powerpc/fpu/fpu_extern.h>
102 #ifdef COMPAT_FREEBSD32
103 #include <compat/freebsd32/freebsd32_signal.h>
104 #include <compat/freebsd32/freebsd32_util.h>
105 #include <compat/freebsd32/freebsd32_proto.h>
107 typedef struct __ucontext32 {
109 mcontext32_t uc_mcontext;
111 struct sigaltstack32 uc_stack;
113 uint32_t __spare__[4];
118 struct siginfo32 sf_si;
121 static int grab_mcontext32(struct thread *td, mcontext32_t *, int flags);
124 static int grab_mcontext(struct thread *, mcontext_t *, int);
127 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
129 struct trapframe *tf;
134 #ifdef COMPAT_FREEBSD32
135 struct siginfo32 siginfo32;
136 struct sigframe32 sf32;
140 int oonstack, rndfsize;
146 PROC_LOCK_ASSERT(p, MA_OWNED);
149 mtx_assert(&psp->ps_mtx, MA_OWNED);
151 oonstack = sigonstack(tf->fixreg[1]);
154 * Fill siginfo structure.
156 ksi->ksi_info.si_signo = ksi->ksi_signo;
157 ksi->ksi_info.si_addr = (void *)((tf->exc == EXC_DSI) ?
160 #ifdef COMPAT_FREEBSD32
161 if (SV_PROC_FLAG(p, SV_ILP32)) {
162 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo32);
163 sig = siginfo32.si_signo;
164 code = siginfo32.si_code;
165 sfp = (caddr_t)&sf32;
166 sfpsize = sizeof(sf32);
167 rndfsize = roundup(sizeof(sf32), 16);
173 memset(&sf32, 0, sizeof(sf32));
174 grab_mcontext32(td, &sf32.sf_uc.uc_mcontext, 0);
176 sf32.sf_uc.uc_sigmask = *mask;
177 sf32.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
178 sf32.sf_uc.uc_stack.ss_size = (uint32_t)td->td_sigstk.ss_size;
179 sf32.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
180 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
182 sf32.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
185 sig = ksi->ksi_signo;
186 code = ksi->ksi_code;
188 sfpsize = sizeof(sf);
191 * 64-bit PPC defines a 288 byte scratch region
194 rndfsize = 288 + roundup(sizeof(sf), 48);
196 rndfsize = roundup(sizeof(sf), 16);
203 memset(&sf, 0, sizeof(sf));
204 grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
206 sf.sf_uc.uc_sigmask = *mask;
207 sf.sf_uc.uc_stack = td->td_sigstk;
208 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
209 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
211 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
212 #ifdef COMPAT_FREEBSD32
216 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
220 * Allocate and validate space for the signal handler context.
222 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
223 SIGISMEMBER(psp->ps_sigonstack, sig)) {
224 usfp = (void *)(((uintptr_t)td->td_sigstk.ss_sp +
225 td->td_sigstk.ss_size - rndfsize) & ~0xFul);
227 usfp = (void *)((tf->fixreg[1] - rndfsize) & ~0xFul);
231 * Save the floating-point state, if necessary, then copy it.
236 * Set up the registers to return to sigcode.
238 * r1/sp - sigframe ptr
239 * lr - sig function, dispatched to by blrl in trampoline
241 * r4 - SIGINFO ? &siginfo : exception code
243 * srr0 - trampoline function addr
245 tf->lr = (register_t)catcher;
246 tf->fixreg[1] = (register_t)usfp;
247 tf->fixreg[FIRSTARG] = sig;
248 #ifdef COMPAT_FREEBSD32
249 tf->fixreg[FIRSTARG+2] = (register_t)usfp +
250 ((SV_PROC_FLAG(p, SV_ILP32)) ?
251 offsetof(struct sigframe32, sf_uc) :
252 offsetof(struct sigframe, sf_uc));
254 tf->fixreg[FIRSTARG+2] = (register_t)usfp +
255 offsetof(struct sigframe, sf_uc);
257 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
259 * Signal handler installed with SA_SIGINFO.
261 #ifdef COMPAT_FREEBSD32
262 if (SV_PROC_FLAG(p, SV_ILP32)) {
263 sf32.sf_si = siginfo32;
264 tf->fixreg[FIRSTARG+1] = (register_t)usfp +
265 offsetof(struct sigframe32, sf_si);
266 sf32.sf_si = siginfo32;
269 tf->fixreg[FIRSTARG+1] = (register_t)usfp +
270 offsetof(struct sigframe, sf_si);
271 sf.sf_si = ksi->ksi_info;
272 #ifdef COMPAT_FREEBSD32
276 /* Old FreeBSD-style arguments. */
277 tf->fixreg[FIRSTARG+1] = code;
278 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ?
281 mtx_unlock(&psp->ps_mtx);
284 tf->srr0 = (register_t)p->p_sysent->sv_sigcode_base;
287 * copy the frame out to userland.
289 if (copyout(sfp, usfp, sfpsize) != 0) {
291 * Process has trashed its stack. Kill it.
293 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
298 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
299 tf->srr0, tf->fixreg[1]);
302 mtx_lock(&psp->ps_mtx);
306 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
311 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
313 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
314 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
318 error = set_mcontext(td, &uc.uc_mcontext);
322 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
324 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
325 td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
327 return (EJUSTRETURN);
330 #ifdef COMPAT_FREEBSD4
332 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
335 return sys_sigreturn(td, (struct sigreturn_args *)uap);
340 * Construct a PCB from a trapframe. This is called from kdb_trap() where
341 * we want to start a backtrace from the function that caused us to enter
342 * the debugger. We have the context in the trapframe, but base the trace
343 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
344 * enough for a backtrace.
347 makectx(struct trapframe *tf, struct pcb *pcb)
350 pcb->pcb_lr = tf->srr0;
351 pcb->pcb_sp = tf->fixreg[1];
355 * get_mcontext/sendsig helper routine that doesn't touch the
359 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
366 memset(mcp, 0, sizeof(mcontext_t));
368 mcp->mc_vers = _MC_VERSION;
370 memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
371 if (flags & GET_MC_CLEAR_RET) {
377 * This assumes that floating-point context is *not* lazy,
378 * so if the thread has used FP there would have been a
379 * FP-unavailable exception that would have set things up
382 if (pcb->pcb_flags & PCB_FPREGS) {
383 if (pcb->pcb_flags & PCB_FPU) {
384 KASSERT(td == curthread,
385 ("get_mcontext: fp save not curthread"));
390 mcp->mc_flags |= _MC_FP_VALID;
391 memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
392 for (i = 0; i < 32; i++)
393 memcpy(&mcp->mc_fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
397 if (pcb->pcb_flags & PCB_VSX) {
398 for (i = 0; i < 32; i++)
399 memcpy(&mcp->mc_vsxfpreg[i],
400 &pcb->pcb_fpu.fpr[i].vsr[2], sizeof(double));
404 * Repeat for Altivec context
407 if (pcb->pcb_flags & PCB_VEC) {
408 KASSERT(td == curthread,
409 ("get_mcontext: fp save not curthread"));
413 mcp->mc_flags |= _MC_AV_VALID;
414 mcp->mc_vscr = pcb->pcb_vec.vscr;
415 mcp->mc_vrsave = pcb->pcb_vec.vrsave;
416 memcpy(mcp->mc_avec, pcb->pcb_vec.vr, sizeof(mcp->mc_avec));
419 mcp->mc_len = sizeof(*mcp);
425 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
429 error = grab_mcontext(td, mcp, flags);
431 PROC_LOCK(curthread->td_proc);
432 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
433 PROC_UNLOCK(curthread->td_proc);
440 set_mcontext(struct thread *td, mcontext_t *mcp)
443 struct trapframe *tf;
450 if (mcp->mc_vers != _MC_VERSION || mcp->mc_len != sizeof(*mcp))
454 * Don't let the user set privileged MSR bits
456 if ((mcp->mc_srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC)) {
460 /* Copy trapframe, preserving TLS pointer across context change */
461 if (SV_PROC_FLAG(td->td_proc, SV_LP64))
462 tls = tf->fixreg[13];
465 memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
466 if (SV_PROC_FLAG(td->td_proc, SV_LP64))
467 tf->fixreg[13] = tls;
471 if (mcp->mc_flags & _MC_FP_VALID) {
472 /* enable_fpu() will happen lazily on a fault */
473 pcb->pcb_flags |= PCB_FPREGS;
474 memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
475 bzero(pcb->pcb_fpu.fpr, sizeof(pcb->pcb_fpu.fpr));
476 for (i = 0; i < 32; i++) {
477 memcpy(&pcb->pcb_fpu.fpr[i].fpr, &mcp->mc_fpreg[i],
479 memcpy(&pcb->pcb_fpu.fpr[i].vsr[2],
480 &mcp->mc_vsxfpreg[i], sizeof(double));
484 if (mcp->mc_flags & _MC_AV_VALID) {
485 if ((pcb->pcb_flags & PCB_VEC) != PCB_VEC) {
490 pcb->pcb_vec.vscr = mcp->mc_vscr;
491 pcb->pcb_vec.vrsave = mcp->mc_vrsave;
492 memcpy(pcb->pcb_vec.vr, mcp->mc_avec, sizeof(mcp->mc_avec));
499 * Set set up registers on exec.
502 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
504 struct trapframe *tf;
508 bzero(tf, sizeof *tf);
510 tf->fixreg[1] = -roundup(-stack + 48, 16);
512 tf->fixreg[1] = -roundup(-stack + 8, 16);
516 * Set up arguments for _start():
517 * _start(argc, argv, envp, obj, cleanup, ps_strings);
520 * - obj and cleanup are the auxilliary and termination
521 * vectors. They are fixed up by ld.elf_so.
522 * - ps_strings is a NetBSD extention, and will be
523 * ignored by executables which are strictly
524 * compliant with the SVR4 ABI.
527 /* Collect argc from the user stack */
528 argc = fuword((void *)stack);
530 tf->fixreg[3] = argc;
531 tf->fixreg[4] = stack + sizeof(register_t);
532 tf->fixreg[5] = stack + (2 + argc)*sizeof(register_t);
533 tf->fixreg[6] = 0; /* auxillary vector */
534 tf->fixreg[7] = 0; /* termination vector */
535 tf->fixreg[8] = (register_t)imgp->ps_strings; /* NetBSD extension */
537 tf->srr0 = imgp->entry_addr;
539 tf->fixreg[12] = imgp->entry_addr;
541 tf->srr1 = PSL_SF | PSL_USERSET | PSL_FE_DFLT;
542 if (mfmsr() & PSL_HV)
545 tf->srr1 = PSL_CM | PSL_USERSET | PSL_FE_DFLT;
548 tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
550 td->td_pcb->pcb_flags = 0;
553 #ifdef COMPAT_FREEBSD32
555 ppc32_setregs(struct thread *td, struct image_params *imgp, u_long stack)
557 struct trapframe *tf;
561 bzero(tf, sizeof *tf);
562 tf->fixreg[1] = -roundup(-stack + 8, 16);
564 argc = fuword32((void *)stack);
566 tf->fixreg[3] = argc;
567 tf->fixreg[4] = stack + sizeof(uint32_t);
568 tf->fixreg[5] = stack + (2 + argc)*sizeof(uint32_t);
569 tf->fixreg[6] = 0; /* auxillary vector */
570 tf->fixreg[7] = 0; /* termination vector */
571 tf->fixreg[8] = (register_t)imgp->ps_strings; /* NetBSD extension */
573 tf->srr0 = imgp->entry_addr;
574 tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
577 if (mfmsr() & PSL_HV)
582 td->td_pcb->pcb_flags = 0;
587 fill_regs(struct thread *td, struct reg *regs)
589 struct trapframe *tf;
592 memcpy(regs, tf, sizeof(struct reg));
598 fill_dbregs(struct thread *td, struct dbreg *dbregs)
600 /* No debug registers on PowerPC */
605 fill_fpregs(struct thread *td, struct fpreg *fpregs)
612 if ((pcb->pcb_flags & PCB_FPREGS) == 0)
613 memset(fpregs, 0, sizeof(struct fpreg));
615 memcpy(&fpregs->fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
616 for (i = 0; i < 32; i++)
617 memcpy(&fpregs->fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
625 set_regs(struct thread *td, struct reg *regs)
627 struct trapframe *tf;
630 memcpy(tf, regs, sizeof(struct reg));
636 set_dbregs(struct thread *td, struct dbreg *dbregs)
638 /* No debug registers on PowerPC */
643 set_fpregs(struct thread *td, struct fpreg *fpregs)
649 pcb->pcb_flags |= PCB_FPREGS;
650 memcpy(&pcb->pcb_fpu.fpscr, &fpregs->fpscr, sizeof(double));
651 for (i = 0; i < 32; i++) {
652 memcpy(&pcb->pcb_fpu.fpr[i].fpr, &fpregs->fpreg[i],
659 #ifdef COMPAT_FREEBSD32
661 set_regs32(struct thread *td, struct reg32 *regs)
663 struct trapframe *tf;
667 for (i = 0; i < 32; i++)
668 tf->fixreg[i] = regs->fixreg[i];
679 fill_regs32(struct thread *td, struct reg32 *regs)
681 struct trapframe *tf;
685 for (i = 0; i < 32; i++)
686 regs->fixreg[i] = tf->fixreg[i];
697 grab_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
702 error = grab_mcontext(td, &mcp64, flags);
706 mcp->mc_vers = mcp64.mc_vers;
707 mcp->mc_flags = mcp64.mc_flags;
708 mcp->mc_onstack = mcp64.mc_onstack;
709 mcp->mc_len = mcp64.mc_len;
710 memcpy(mcp->mc_avec,mcp64.mc_avec,sizeof(mcp64.mc_avec));
711 memcpy(mcp->mc_av,mcp64.mc_av,sizeof(mcp64.mc_av));
712 for (i = 0; i < 42; i++)
713 mcp->mc_frame[i] = mcp64.mc_frame[i];
714 memcpy(mcp->mc_fpreg,mcp64.mc_fpreg,sizeof(mcp64.mc_fpreg));
715 memcpy(mcp->mc_vsxfpreg,mcp64.mc_vsxfpreg,sizeof(mcp64.mc_vsxfpreg));
721 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
725 error = grab_mcontext32(td, mcp, flags);
727 PROC_LOCK(curthread->td_proc);
728 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
729 PROC_UNLOCK(curthread->td_proc);
736 set_mcontext32(struct thread *td, mcontext32_t *mcp)
741 mcp64.mc_vers = mcp->mc_vers;
742 mcp64.mc_flags = mcp->mc_flags;
743 mcp64.mc_onstack = mcp->mc_onstack;
744 mcp64.mc_len = mcp->mc_len;
745 memcpy(mcp64.mc_avec,mcp->mc_avec,sizeof(mcp64.mc_avec));
746 memcpy(mcp64.mc_av,mcp->mc_av,sizeof(mcp64.mc_av));
747 for (i = 0; i < 42; i++)
748 mcp64.mc_frame[i] = mcp->mc_frame[i];
749 mcp64.mc_srr1 |= (td->td_frame->srr1 & 0xFFFFFFFF00000000ULL);
750 memcpy(mcp64.mc_fpreg,mcp->mc_fpreg,sizeof(mcp64.mc_fpreg));
751 memcpy(mcp64.mc_vsxfpreg,mcp->mc_vsxfpreg,sizeof(mcp64.mc_vsxfpreg));
753 error = set_mcontext(td, &mcp64);
759 #ifdef COMPAT_FREEBSD32
761 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
766 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
768 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
769 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
773 error = set_mcontext32(td, &uc.uc_mcontext);
777 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
779 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
780 td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
782 return (EJUSTRETURN);
786 * The first two fields of a ucontext_t are the signal mask and the machine
787 * context. The next field is uc_link; we want to avoid destroying the link
788 * when copying out contexts.
790 #define UC32_COPY_SIZE offsetof(ucontext32_t, uc_link)
793 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
798 if (uap->ucp == NULL)
801 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
802 PROC_LOCK(td->td_proc);
803 uc.uc_sigmask = td->td_sigmask;
804 PROC_UNLOCK(td->td_proc);
805 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
811 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
816 if (uap->ucp == NULL)
819 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
821 ret = set_mcontext32(td, &uc.uc_mcontext);
823 kern_sigprocmask(td, SIG_SETMASK,
824 &uc.uc_sigmask, NULL, 0);
828 return (ret == 0 ? EJUSTRETURN : ret);
832 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
837 if (uap->oucp == NULL || uap->ucp == NULL)
840 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
841 PROC_LOCK(td->td_proc);
842 uc.uc_sigmask = td->td_sigmask;
843 PROC_UNLOCK(td->td_proc);
844 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
846 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
848 ret = set_mcontext32(td, &uc.uc_mcontext);
850 kern_sigprocmask(td, SIG_SETMASK,
851 &uc.uc_sigmask, NULL, 0);
856 return (ret == 0 ? EJUSTRETURN : ret);
862 cpu_set_syscall_retval(struct thread *td, int error)
865 struct trapframe *tf;
868 if (error == EJUSTRETURN)
874 if (tf->fixreg[0] == SYS___syscall &&
875 (SV_PROC_FLAG(p, SV_ILP32))) {
876 int code = tf->fixreg[FIRSTARG + 1];
877 if (p->p_sysent->sv_mask)
878 code &= p->p_sysent->sv_mask;
880 #if defined(COMPAT_FREEBSD6) && defined(SYS_freebsd6_lseek)
881 code != SYS_freebsd6_lseek &&
883 code != SYS_lseek) ? 1 : 0;
891 * 64-bit return, 32-bit syscall. Fixup byte order
893 tf->fixreg[FIRSTARG] = 0;
894 tf->fixreg[FIRSTARG + 1] = td->td_retval[0];
896 tf->fixreg[FIRSTARG] = td->td_retval[0];
897 tf->fixreg[FIRSTARG + 1] = td->td_retval[1];
899 tf->cr &= ~0x10000000; /* Unset summary overflow */
903 * Set user's pc back to redo the system call.
908 tf->fixreg[FIRSTARG] = SV_ABI_ERRNO(p, error);
909 tf->cr |= 0x10000000; /* Set summary overflow */
915 * Threading functions
918 cpu_thread_exit(struct thread *td)
923 cpu_thread_clean(struct thread *td)
928 cpu_thread_alloc(struct thread *td)
932 pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
933 sizeof(struct pcb)) & ~0x2fUL);
935 td->td_frame = (struct trapframe *)pcb - 1;
939 cpu_thread_free(struct thread *td)
944 cpu_set_user_tls(struct thread *td, void *tls_base)
947 if (SV_PROC_FLAG(td->td_proc, SV_LP64))
948 td->td_frame->fixreg[13] = (register_t)tls_base + 0x7010;
950 td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
955 cpu_copy_thread(struct thread *td, struct thread *td0)
958 struct trapframe *tf;
959 struct callframe *cf;
963 /* Copy the upcall pcb */
964 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
966 /* Create a stack for the new thread */
968 bcopy(td0->td_frame, tf, sizeof(struct trapframe));
969 tf->fixreg[FIRSTARG] = 0;
970 tf->fixreg[FIRSTARG + 1] = 0;
971 tf->cr &= ~0x10000000;
973 /* Set registers for trampoline to user mode. */
974 cf = (struct callframe *)tf - 1;
975 memset(cf, 0, sizeof(struct callframe));
976 cf->cf_func = (register_t)fork_return;
977 cf->cf_arg0 = (register_t)td;
978 cf->cf_arg1 = (register_t)tf;
980 pcb2->pcb_sp = (register_t)cf;
981 #if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
982 pcb2->pcb_lr = ((register_t *)fork_trampoline)[0];
983 pcb2->pcb_toc = ((register_t *)fork_trampoline)[1];
985 pcb2->pcb_lr = (register_t)fork_trampoline;
986 pcb2->pcb_context[0] = pcb2->pcb_lr;
988 pcb2->pcb_cpu.aim.usr_vsid = 0;
990 /* Setup to release spin count in fork_exit(). */
991 td->td_md.md_spinlock_count = 1;
992 td->td_md.md_saved_msr = PSL_KERNSET;
996 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
999 struct trapframe *tf;
1003 /* align stack and alloc space for frame ptr and saved LR */
1004 #ifdef __powerpc64__
1005 sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 48) &
1008 sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 8) &
1011 bzero(tf, sizeof(struct trapframe));
1013 tf->fixreg[1] = (register_t)sp;
1014 tf->fixreg[3] = (register_t)arg;
1015 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1016 tf->srr0 = (register_t)entry;
1017 tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
1018 #ifdef __powerpc64__
1019 tf->srr1 &= ~PSL_SF;
1022 #ifdef __powerpc64__
1023 register_t entry_desc[3];
1024 (void)copyin((void *)entry, entry_desc, sizeof(entry_desc));
1025 tf->srr0 = entry_desc[0];
1026 tf->fixreg[2] = entry_desc[1];
1027 tf->fixreg[11] = entry_desc[2];
1028 tf->srr1 = PSL_SF | PSL_USERSET | PSL_FE_DFLT;
1032 #ifdef __powerpc64__
1033 if (mfmsr() & PSL_HV)
1036 td->td_pcb->pcb_flags = 0;
1038 td->td_retval[0] = (register_t)entry;
1039 td->td_retval[1] = 0;
1043 ppc_instr_emulate(struct trapframe *frame, struct pcb *pcb)
1048 instr = fuword32((void *)frame->srr0);
1051 if ((instr & 0xfc1fffff) == 0x7c1f42a6) { /* mfpvr */
1052 reg = (instr & ~0xfc1fffff) >> 21;
1053 frame->fixreg[reg] = mfpvr();
1058 if ((instr & 0xfc000ffe) == 0x7c0004ac) { /* various sync */
1059 powerpc_sync(); /* Do a heavy-weight sync */
1065 if (!(pcb->pcb_flags & PCB_FPREGS)) {
1066 bzero(&pcb->pcb_fpu, sizeof(pcb->pcb_fpu));
1067 pcb->pcb_flags |= PCB_FPREGS;
1069 sig = fpu_emulate(frame, &pcb->pcb_fpu);