2 * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3 * Copyright (C) 1995, 1996 TooLs GmbH.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by TooLs GmbH.
17 * 4. The name of TooLs GmbH may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (C) 2001 Benno Rice
33 * All rights reserved.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
44 * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
45 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
47 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
49 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
50 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
53 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
60 #include "opt_compat.h"
62 #include <sys/param.h>
64 #include <sys/systm.h>
71 #include <sys/imgact.h>
72 #include <sys/kernel.h>
75 #include <sys/malloc.h>
76 #include <sys/mutex.h>
77 #include <sys/signalvar.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/syscall.h>
80 #include <sys/sysent.h>
81 #include <sys/sysproto.h>
82 #include <sys/ucontext.h>
85 #include <machine/altivec.h>
86 #include <machine/cpu.h>
87 #include <machine/elf.h>
88 #include <machine/fpu.h>
89 #include <machine/pcb.h>
90 #include <machine/reg.h>
91 #include <machine/sigframe.h>
92 #include <machine/trap.h>
93 #include <machine/vmparam.h>
95 #ifdef COMPAT_FREEBSD32
96 #include <compat/freebsd32/freebsd32_signal.h>
97 #include <compat/freebsd32/freebsd32_util.h>
98 #include <compat/freebsd32/freebsd32_proto.h>
100 typedef struct __ucontext32 {
102 mcontext32_t uc_mcontext;
104 struct sigaltstack32 uc_stack;
106 uint32_t __spare__[4];
111 struct siginfo32 sf_si;
114 static int grab_mcontext32(struct thread *td, mcontext32_t *, int flags);
117 static int grab_mcontext(struct thread *, mcontext_t *, int);
120 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
122 struct trapframe *tf;
127 #ifdef COMPAT_FREEBSD32
128 struct siginfo32 siginfo32;
129 struct sigframe32 sf32;
133 int oonstack, rndfsize;
139 PROC_LOCK_ASSERT(p, MA_OWNED);
142 mtx_assert(&psp->ps_mtx, MA_OWNED);
144 oonstack = sigonstack(tf->fixreg[1]);
147 * Fill siginfo structure.
149 ksi->ksi_info.si_signo = ksi->ksi_signo;
151 ksi->ksi_info.si_addr = (void *)((tf->exc == EXC_DSI) ?
152 tf->cpu.aim.dar : tf->srr0);
154 ksi->ksi_info.si_addr = (void *)((tf->exc == EXC_DSI) ?
155 tf->cpu.booke.dear : tf->srr0);
158 #ifdef COMPAT_FREEBSD32
159 if (SV_PROC_FLAG(p, SV_ILP32)) {
160 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo32);
161 sig = siginfo32.si_signo;
162 code = siginfo32.si_code;
163 sfp = (caddr_t)&sf32;
164 sfpsize = sizeof(sf32);
165 rndfsize = ((sizeof(sf32) + 15) / 16) * 16;
171 memset(&sf32, 0, sizeof(sf32));
172 grab_mcontext32(td, &sf32.sf_uc.uc_mcontext, 0);
174 sf32.sf_uc.uc_sigmask = *mask;
175 sf32.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
176 sf32.sf_uc.uc_stack.ss_size = (uint32_t)td->td_sigstk.ss_size;
177 sf32.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
178 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
180 sf32.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
183 sig = ksi->ksi_signo;
184 code = ksi->ksi_code;
186 sfpsize = sizeof(sf);
189 * 64-bit PPC defines a 288 byte scratch region
192 rndfsize = 288 + ((sizeof(sf) + 47) / 48) * 48;
194 rndfsize = ((sizeof(sf) + 15) / 16) * 16;
201 memset(&sf, 0, sizeof(sf));
202 grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
204 sf.sf_uc.uc_sigmask = *mask;
205 sf.sf_uc.uc_stack = td->td_sigstk;
206 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
207 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
209 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
210 #ifdef COMPAT_FREEBSD32
214 CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
218 * Allocate and validate space for the signal handler context.
220 if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
221 SIGISMEMBER(psp->ps_sigonstack, sig)) {
222 usfp = (void *)(td->td_sigstk.ss_sp +
223 td->td_sigstk.ss_size - rndfsize);
225 usfp = (void *)(tf->fixreg[1] - rndfsize);
229 * Translate the signal if appropriate (Linux emu ?)
231 if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
232 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
235 * Save the floating-point state, if necessary, then copy it.
240 * Set up the registers to return to sigcode.
242 * r1/sp - sigframe ptr
243 * lr - sig function, dispatched to by blrl in trampoline
245 * r4 - SIGINFO ? &siginfo : exception code
247 * srr0 - trampoline function addr
249 tf->lr = (register_t)catcher;
250 tf->fixreg[1] = (register_t)usfp;
251 tf->fixreg[FIRSTARG] = sig;
252 #ifdef COMPAT_FREEBSD32
253 tf->fixreg[FIRSTARG+2] = (register_t)usfp +
254 ((SV_PROC_FLAG(p, SV_ILP32)) ?
255 offsetof(struct sigframe32, sf_uc) :
256 offsetof(struct sigframe, sf_uc));
258 tf->fixreg[FIRSTARG+2] = (register_t)usfp +
259 offsetof(struct sigframe, sf_uc);
261 if (SIGISMEMBER(psp->ps_siginfo, sig)) {
263 * Signal handler installed with SA_SIGINFO.
265 #ifdef COMPAT_FREEBSD32
266 if (SV_PROC_FLAG(p, SV_ILP32)) {
267 sf32.sf_si = siginfo32;
268 tf->fixreg[FIRSTARG+1] = (register_t)usfp +
269 offsetof(struct sigframe32, sf_si);
270 sf32.sf_si = siginfo32;
273 tf->fixreg[FIRSTARG+1] = (register_t)usfp +
274 offsetof(struct sigframe, sf_si);
275 sf.sf_si = ksi->ksi_info;
276 #ifdef COMPAT_FREEBSD32
280 /* Old FreeBSD-style arguments. */
281 tf->fixreg[FIRSTARG+1] = code;
283 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ?
284 tf->cpu.aim.dar : tf->srr0;
286 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ?
287 tf->cpu.booke.dear : tf->srr0;
290 mtx_unlock(&psp->ps_mtx);
293 tf->srr0 = (register_t)p->p_sysent->sv_sigcode_base;
296 * copy the frame out to userland.
298 if (copyout(sfp, usfp, sfpsize) != 0) {
300 * Process has trashed its stack. Kill it.
302 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
307 CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
308 tf->srr0, tf->fixreg[1]);
311 mtx_lock(&psp->ps_mtx);
315 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
320 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
322 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
323 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
327 error = set_mcontext(td, &uc.uc_mcontext);
331 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
333 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
334 td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
336 return (EJUSTRETURN);
339 #ifdef COMPAT_FREEBSD4
341 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
344 return sys_sigreturn(td, (struct sigreturn_args *)uap);
349 * Construct a PCB from a trapframe. This is called from kdb_trap() where
350 * we want to start a backtrace from the function that caused us to enter
351 * the debugger. We have the context in the trapframe, but base the trace
352 * on the PCB. The PCB doesn't have to be perfect, as long as it contains
353 * enough for a backtrace.
356 makectx(struct trapframe *tf, struct pcb *pcb)
359 pcb->pcb_lr = tf->srr0;
360 pcb->pcb_sp = tf->fixreg[1];
364 * get_mcontext/sendsig helper routine that doesn't touch the
368 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
374 memset(mcp, 0, sizeof(mcontext_t));
376 mcp->mc_vers = _MC_VERSION;
378 memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
379 if (flags & GET_MC_CLEAR_RET) {
386 * This assumes that floating-point context is *not* lazy,
387 * so if the thread has used FP there would have been a
388 * FP-unavailable exception that would have set things up
391 if (pcb->pcb_flags & PCB_FPU) {
392 KASSERT(td == curthread,
393 ("get_mcontext: fp save not curthread"));
397 mcp->mc_flags |= _MC_FP_VALID;
398 memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
399 memcpy(mcp->mc_fpreg, pcb->pcb_fpu.fpr, 32*sizeof(double));
403 * Repeat for Altivec context
406 if (pcb->pcb_flags & PCB_VEC) {
407 KASSERT(td == curthread,
408 ("get_mcontext: fp save not curthread"));
412 mcp->mc_flags |= _MC_AV_VALID;
413 mcp->mc_vscr = pcb->pcb_vec.vscr;
414 mcp->mc_vrsave = pcb->pcb_vec.vrsave;
415 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, const mcontext_t *mcp)
443 struct trapframe *tf;
449 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)) {
461 /* Copy trapframe, preserving TLS pointer across context change */
462 if (SV_PROC_FLAG(td->td_proc, SV_LP64))
463 tls = tf->fixreg[13];
466 memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
467 if (SV_PROC_FLAG(td->td_proc, SV_LP64))
468 tf->fixreg[13] = tls;
473 if (mcp->mc_flags & _MC_FP_VALID) {
474 if ((pcb->pcb_flags & PCB_FPU) != PCB_FPU) {
479 memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
480 memcpy(pcb->pcb_fpu.fpr, mcp->mc_fpreg, 32*sizeof(double));
483 if (mcp->mc_flags & _MC_AV_VALID) {
484 if ((pcb->pcb_flags & PCB_VEC) != PCB_VEC) {
489 pcb->pcb_vec.vscr = mcp->mc_vscr;
490 pcb->pcb_vec.vrsave = mcp->mc_vrsave;
491 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;
507 register_t entry_desc[3];
511 bzero(tf, sizeof *tf);
513 tf->fixreg[1] = -roundup(-stack + 48, 16);
515 tf->fixreg[1] = -roundup(-stack + 8, 16);
519 * Set up arguments for _start():
520 * _start(argc, argv, envp, obj, cleanup, ps_strings);
523 * - obj and cleanup are the auxilliary and termination
524 * vectors. They are fixed up by ld.elf_so.
525 * - ps_strings is a NetBSD extention, and will be
526 * ignored by executables which are strictly
527 * compliant with the SVR4 ABI.
529 * XXX We have to set both regs and retval here due to different
530 * XXX calling convention in trap.c and init_main.c.
533 /* Collect argc from the user stack */
534 argc = fuword((void *)stack);
537 * XXX PG: these get overwritten in the syscall return code.
538 * execve() should return EJUSTRETURN, like it does on NetBSD.
539 * Emulate by setting the syscall return value cells. The
540 * registers still have to be set for init's fork trampoline.
542 td->td_retval[0] = argc;
543 td->td_retval[1] = stack + sizeof(register_t);
544 tf->fixreg[3] = argc;
545 tf->fixreg[4] = stack + sizeof(register_t);
546 tf->fixreg[5] = stack + (2 + argc)*sizeof(register_t);
547 tf->fixreg[6] = 0; /* auxillary vector */
548 tf->fixreg[7] = 0; /* termination vector */
549 tf->fixreg[8] = (register_t)imgp->ps_strings; /* NetBSD extension */
553 * For 64-bit, we need to disentangle the function descriptor
557 * 2. Environment pointer (r11)
560 (void)copyin((void *)imgp->entry_addr, entry_desc, sizeof(entry_desc));
561 tf->srr0 = entry_desc[0] + imgp->reloc_base;
562 tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
563 tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
564 tf->srr1 = PSL_SF | PSL_USERSET | PSL_FE_DFLT;
565 if (mfmsr() & PSL_HV)
568 tf->srr0 = imgp->entry_addr;
569 tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
571 td->td_pcb->pcb_flags = 0;
574 #ifdef COMPAT_FREEBSD32
576 ppc32_setregs(struct thread *td, struct image_params *imgp, u_long stack)
578 struct trapframe *tf;
582 bzero(tf, sizeof *tf);
583 tf->fixreg[1] = -roundup(-stack + 8, 16);
585 argc = fuword32((void *)stack);
587 td->td_retval[0] = argc;
588 td->td_retval[1] = stack + sizeof(uint32_t);
589 tf->fixreg[3] = argc;
590 tf->fixreg[4] = stack + sizeof(uint32_t);
591 tf->fixreg[5] = stack + (2 + argc)*sizeof(uint32_t);
592 tf->fixreg[6] = 0; /* auxillary vector */
593 tf->fixreg[7] = 0; /* termination vector */
594 tf->fixreg[8] = (register_t)imgp->ps_strings; /* NetBSD extension */
596 tf->srr0 = imgp->entry_addr;
597 tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
599 if (mfmsr() & PSL_HV)
601 td->td_pcb->pcb_flags = 0;
606 fill_regs(struct thread *td, struct reg *regs)
608 struct trapframe *tf;
611 memcpy(regs, tf, sizeof(struct reg));
617 fill_dbregs(struct thread *td, struct dbreg *dbregs)
619 /* No debug registers on PowerPC */
624 fill_fpregs(struct thread *td, struct fpreg *fpregs)
630 if ((pcb->pcb_flags & PCB_FPU) == 0)
631 memset(fpregs, 0, sizeof(struct fpreg));
633 memcpy(fpregs, &pcb->pcb_fpu, sizeof(struct fpreg));
639 set_regs(struct thread *td, struct reg *regs)
641 struct trapframe *tf;
644 memcpy(tf, regs, sizeof(struct reg));
650 set_dbregs(struct thread *td, struct dbreg *dbregs)
652 /* No debug registers on PowerPC */
657 set_fpregs(struct thread *td, struct fpreg *fpregs)
663 if ((pcb->pcb_flags & PCB_FPU) == 0)
665 memcpy(&pcb->pcb_fpu, fpregs, sizeof(struct fpreg));
671 #ifdef COMPAT_FREEBSD32
673 set_regs32(struct thread *td, struct reg32 *regs)
675 struct trapframe *tf;
679 for (i = 0; i < 32; i++)
680 tf->fixreg[i] = regs->fixreg[i];
691 fill_regs32(struct thread *td, struct reg32 *regs)
693 struct trapframe *tf;
697 for (i = 0; i < 32; i++)
698 regs->fixreg[i] = tf->fixreg[i];
709 grab_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
714 error = grab_mcontext(td, &mcp64, flags);
718 mcp->mc_vers = mcp64.mc_vers;
719 mcp->mc_flags = mcp64.mc_flags;
720 mcp->mc_onstack = mcp64.mc_onstack;
721 mcp->mc_len = mcp64.mc_len;
722 memcpy(mcp->mc_avec,mcp64.mc_avec,sizeof(mcp64.mc_avec));
723 memcpy(mcp->mc_av,mcp64.mc_av,sizeof(mcp64.mc_av));
724 for (i = 0; i < 42; i++)
725 mcp->mc_frame[i] = mcp64.mc_frame[i];
726 memcpy(mcp->mc_fpreg,mcp64.mc_fpreg,sizeof(mcp64.mc_fpreg));
732 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
736 error = grab_mcontext32(td, mcp, flags);
738 PROC_LOCK(curthread->td_proc);
739 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
740 PROC_UNLOCK(curthread->td_proc);
747 set_mcontext32(struct thread *td, const mcontext32_t *mcp)
752 mcp64.mc_vers = mcp->mc_vers;
753 mcp64.mc_flags = mcp->mc_flags;
754 mcp64.mc_onstack = mcp->mc_onstack;
755 mcp64.mc_len = mcp->mc_len;
756 memcpy(mcp64.mc_avec,mcp->mc_avec,sizeof(mcp64.mc_avec));
757 memcpy(mcp64.mc_av,mcp->mc_av,sizeof(mcp64.mc_av));
758 for (i = 0; i < 42; i++)
759 mcp64.mc_frame[i] = mcp->mc_frame[i];
760 memcpy(mcp64.mc_fpreg,mcp->mc_fpreg,sizeof(mcp64.mc_fpreg));
762 error = set_mcontext(td, &mcp64);
768 #ifdef COMPAT_FREEBSD32
770 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
775 CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
777 if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
778 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
782 error = set_mcontext32(td, &uc.uc_mcontext);
786 kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
788 CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
789 td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
791 return (EJUSTRETURN);
795 * The first two fields of a ucontext_t are the signal mask and the machine
796 * context. The next field is uc_link; we want to avoid destroying the link
797 * when copying out contexts.
799 #define UC32_COPY_SIZE offsetof(ucontext32_t, uc_link)
802 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
807 if (uap->ucp == NULL)
810 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
811 PROC_LOCK(td->td_proc);
812 uc.uc_sigmask = td->td_sigmask;
813 PROC_UNLOCK(td->td_proc);
814 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
820 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
825 if (uap->ucp == NULL)
828 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
830 ret = set_mcontext32(td, &uc.uc_mcontext);
832 kern_sigprocmask(td, SIG_SETMASK,
833 &uc.uc_sigmask, NULL, 0);
837 return (ret == 0 ? EJUSTRETURN : ret);
841 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
846 if (uap->oucp == NULL || uap->ucp == NULL)
849 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
850 PROC_LOCK(td->td_proc);
851 uc.uc_sigmask = td->td_sigmask;
852 PROC_UNLOCK(td->td_proc);
853 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
855 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
857 ret = set_mcontext32(td, &uc.uc_mcontext);
859 kern_sigprocmask(td, SIG_SETMASK,
860 &uc.uc_sigmask, NULL, 0);
865 return (ret == 0 ? EJUSTRETURN : ret);
871 cpu_set_syscall_retval(struct thread *td, int error)
874 struct trapframe *tf;
877 if (error == EJUSTRETURN)
883 if (tf->fixreg[0] == SYS___syscall &&
884 (SV_PROC_FLAG(p, SV_ILP32))) {
885 int code = tf->fixreg[FIRSTARG + 1];
886 if (p->p_sysent->sv_mask)
887 code &= p->p_sysent->sv_mask;
888 fixup = (code != SYS_freebsd6_lseek && code != SYS_lseek) ?
897 * 64-bit return, 32-bit syscall. Fixup byte order
899 tf->fixreg[FIRSTARG] = 0;
900 tf->fixreg[FIRSTARG + 1] = td->td_retval[0];
902 tf->fixreg[FIRSTARG] = td->td_retval[0];
903 tf->fixreg[FIRSTARG + 1] = td->td_retval[1];
905 tf->cr &= ~0x10000000; /* Unset summary overflow */
909 * Set user's pc back to redo the system call.
914 if (p->p_sysent->sv_errsize) {
915 error = (error < p->p_sysent->sv_errsize) ?
916 p->p_sysent->sv_errtbl[error] : -1;
918 tf->fixreg[FIRSTARG] = error;
919 tf->cr |= 0x10000000; /* Set summary overflow */
925 * Threading functions
928 cpu_thread_exit(struct thread *td)
933 cpu_thread_clean(struct thread *td)
938 cpu_thread_alloc(struct thread *td)
942 pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
943 sizeof(struct pcb)) & ~0x2fUL);
945 td->td_frame = (struct trapframe *)pcb - 1;
949 cpu_thread_free(struct thread *td)
954 cpu_set_user_tls(struct thread *td, void *tls_base)
957 if (SV_PROC_FLAG(td->td_proc, SV_LP64))
958 td->td_frame->fixreg[13] = (register_t)tls_base + 0x7010;
960 td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
965 cpu_set_upcall(struct thread *td, struct thread *td0)
968 struct trapframe *tf;
969 struct callframe *cf;
973 /* Copy the upcall pcb */
974 bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
976 /* Create a stack for the new thread */
978 bcopy(td0->td_frame, tf, sizeof(struct trapframe));
979 tf->fixreg[FIRSTARG] = 0;
980 tf->fixreg[FIRSTARG + 1] = 0;
981 tf->cr &= ~0x10000000;
983 /* Set registers for trampoline to user mode. */
984 cf = (struct callframe *)tf - 1;
985 memset(cf, 0, sizeof(struct callframe));
986 cf->cf_func = (register_t)fork_return;
987 cf->cf_arg0 = (register_t)td;
988 cf->cf_arg1 = (register_t)tf;
990 pcb2->pcb_sp = (register_t)cf;
992 pcb2->pcb_lr = ((register_t *)fork_trampoline)[0];
993 pcb2->pcb_toc = ((register_t *)fork_trampoline)[1];
995 pcb2->pcb_lr = (register_t)fork_trampoline;
997 pcb2->pcb_cpu.aim.usr_vsid = 0;
999 /* Setup to release spin count in fork_exit(). */
1000 td->td_md.md_spinlock_count = 1;
1001 td->td_md.md_saved_msr = PSL_KERNSET;
1005 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
1008 struct trapframe *tf;
1012 /* align stack and alloc space for frame ptr and saved LR */
1013 #ifdef __powerpc64__
1014 sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 48) &
1017 sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 8) &
1020 bzero(tf, sizeof(struct trapframe));
1022 tf->fixreg[1] = (register_t)sp;
1023 tf->fixreg[3] = (register_t)arg;
1024 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1025 tf->srr0 = (register_t)entry;
1027 tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
1028 #ifdef __powerpc64__
1029 tf->srr1 &= ~PSL_SF;
1032 tf->srr1 = PSL_USERSET;
1035 #ifdef __powerpc64__
1036 register_t entry_desc[3];
1037 (void)copyin((void *)entry, entry_desc, sizeof(entry_desc));
1038 tf->srr0 = entry_desc[0];
1039 tf->fixreg[2] = entry_desc[1];
1040 tf->fixreg[11] = entry_desc[2];
1041 tf->srr1 = PSL_SF | PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
1045 #ifdef __powerpc64__
1046 if (mfmsr() & PSL_HV)
1049 td->td_pcb->pcb_flags = 0;
1051 td->td_retval[0] = (register_t)entry;
1052 td->td_retval[1] = 0;