]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/exec_machdep.c
Merge ^/head r279163 through r279308.
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / exec_machdep.c
1 /*-
2  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
3  * Copyright (C) 1995, 1996 TooLs GmbH.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
19  *
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.
30  */
31 /*-
32  * Copyright (C) 2001 Benno Rice
33  * All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
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.
43  *
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 $
55  */
56
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59
60 #include "opt_compat.h"
61 #include "opt_fpu_emu.h"
62
63 #include <sys/param.h>
64 #include <sys/proc.h>
65 #include <sys/systm.h>
66 #include <sys/bio.h>
67 #include <sys/buf.h>
68 #include <sys/bus.h>
69 #include <sys/cons.h>
70 #include <sys/cpu.h>
71 #include <sys/exec.h>
72 #include <sys/imgact.h>
73 #include <sys/kernel.h>
74 #include <sys/ktr.h>
75 #include <sys/lock.h>
76 #include <sys/malloc.h>
77 #include <sys/mutex.h>
78 #include <sys/signalvar.h>
79 #include <sys/syscallsubr.h>
80 #include <sys/syscall.h>
81 #include <sys/sysent.h>
82 #include <sys/sysproto.h>
83 #include <sys/ucontext.h>
84 #include <sys/uio.h>
85
86 #include <machine/altivec.h>
87 #include <machine/cpu.h>
88 #include <machine/elf.h>
89 #include <machine/fpu.h>
90 #include <machine/pcb.h>
91 #include <machine/reg.h>
92 #include <machine/sigframe.h>
93 #include <machine/trap.h>
94 #include <machine/vmparam.h>
95
96 #ifdef FPU_EMU
97 #include <powerpc/fpu/fpu_extern.h>
98 #endif
99
100 #ifdef COMPAT_FREEBSD32
101 #include <compat/freebsd32/freebsd32_signal.h>
102 #include <compat/freebsd32/freebsd32_util.h>
103 #include <compat/freebsd32/freebsd32_proto.h>
104
105 typedef struct __ucontext32 {
106         sigset_t                uc_sigmask;
107         mcontext32_t            uc_mcontext;
108         uint32_t                uc_link;
109         struct sigaltstack32    uc_stack;
110         uint32_t                uc_flags;
111         uint32_t                __spare__[4];
112 } ucontext32_t;
113
114 struct sigframe32 {
115         ucontext32_t            sf_uc;
116         struct siginfo32        sf_si;
117 };
118
119 static int      grab_mcontext32(struct thread *td, mcontext32_t *, int flags);
120 #endif
121
122 static int      grab_mcontext(struct thread *, mcontext_t *, int);
123
124 void
125 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
126 {
127         struct trapframe *tf;
128         struct sigacts *psp;
129         struct sigframe sf;
130         struct thread *td;
131         struct proc *p;
132         #ifdef COMPAT_FREEBSD32
133         struct siginfo32 siginfo32;
134         struct sigframe32 sf32;
135         #endif
136         size_t sfpsize;
137         caddr_t sfp, usfp;
138         int oonstack, rndfsize;
139         int sig;
140         int code;
141
142         td = curthread;
143         p = td->td_proc;
144         PROC_LOCK_ASSERT(p, MA_OWNED);
145
146         psp = p->p_sigacts;
147         mtx_assert(&psp->ps_mtx, MA_OWNED);
148         tf = td->td_frame;
149         oonstack = sigonstack(tf->fixreg[1]);
150
151         /*
152          * Fill siginfo structure.
153          */
154         ksi->ksi_info.si_signo = ksi->ksi_signo;
155         #ifdef AIM
156         ksi->ksi_info.si_addr = (void *)((tf->exc == EXC_DSI) ? 
157             tf->cpu.aim.dar : tf->srr0);
158         #else
159         ksi->ksi_info.si_addr = (void *)((tf->exc == EXC_DSI) ? 
160             tf->cpu.booke.dear : tf->srr0);
161         #endif
162
163         #ifdef COMPAT_FREEBSD32
164         if (SV_PROC_FLAG(p, SV_ILP32)) {
165                 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo32);
166                 sig = siginfo32.si_signo;
167                 code = siginfo32.si_code;
168                 sfp = (caddr_t)&sf32;
169                 sfpsize = sizeof(sf32);
170                 rndfsize = ((sizeof(sf32) + 15) / 16) * 16;
171
172                 /*
173                  * Save user context
174                  */
175
176                 memset(&sf32, 0, sizeof(sf32));
177                 grab_mcontext32(td, &sf32.sf_uc.uc_mcontext, 0);
178
179                 sf32.sf_uc.uc_sigmask = *mask;
180                 sf32.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
181                 sf32.sf_uc.uc_stack.ss_size = (uint32_t)td->td_sigstk.ss_size;
182                 sf32.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
183                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
184
185                 sf32.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
186         } else {
187         #endif
188                 sig = ksi->ksi_signo;
189                 code = ksi->ksi_code;
190                 sfp = (caddr_t)&sf;
191                 sfpsize = sizeof(sf);
192                 #ifdef __powerpc64__
193                 /*
194                  * 64-bit PPC defines a 288 byte scratch region
195                  * below the stack.
196                  */
197                 rndfsize = 288 + ((sizeof(sf) + 47) / 48) * 48;
198                 #else
199                 rndfsize = ((sizeof(sf) + 15) / 16) * 16;
200                 #endif
201
202                 /*
203                  * Save user context
204                  */
205
206                 memset(&sf, 0, sizeof(sf));
207                 grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
208
209                 sf.sf_uc.uc_sigmask = *mask;
210                 sf.sf_uc.uc_stack = td->td_sigstk;
211                 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
212                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
213
214                 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
215         #ifdef COMPAT_FREEBSD32
216         }
217         #endif
218
219         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
220              catcher, sig);
221
222         /*
223          * Allocate and validate space for the signal handler context.
224          */
225         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
226             SIGISMEMBER(psp->ps_sigonstack, sig)) {
227                 usfp = (void *)(td->td_sigstk.ss_sp +
228                    td->td_sigstk.ss_size - rndfsize);
229         } else {
230                 usfp = (void *)(tf->fixreg[1] - rndfsize);
231         }
232
233         /*
234          * Translate the signal if appropriate (Linux emu ?)
235          */
236         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
237                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
238
239         /*
240          * Save the floating-point state, if necessary, then copy it.
241          */
242         /* XXX */
243
244         /*
245          * Set up the registers to return to sigcode.
246          *
247          *   r1/sp - sigframe ptr
248          *   lr    - sig function, dispatched to by blrl in trampoline
249          *   r3    - sig number
250          *   r4    - SIGINFO ? &siginfo : exception code
251          *   r5    - user context
252          *   srr0  - trampoline function addr
253          */
254         tf->lr = (register_t)catcher;
255         tf->fixreg[1] = (register_t)usfp;
256         tf->fixreg[FIRSTARG] = sig;
257         #ifdef COMPAT_FREEBSD32
258         tf->fixreg[FIRSTARG+2] = (register_t)usfp +
259             ((SV_PROC_FLAG(p, SV_ILP32)) ?
260             offsetof(struct sigframe32, sf_uc) :
261             offsetof(struct sigframe, sf_uc));
262         #else
263         tf->fixreg[FIRSTARG+2] = (register_t)usfp +
264             offsetof(struct sigframe, sf_uc);
265         #endif
266         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
267                 /*
268                  * Signal handler installed with SA_SIGINFO.
269                  */
270                 #ifdef COMPAT_FREEBSD32
271                 if (SV_PROC_FLAG(p, SV_ILP32)) {
272                         sf32.sf_si = siginfo32;
273                         tf->fixreg[FIRSTARG+1] = (register_t)usfp +
274                             offsetof(struct sigframe32, sf_si);
275                         sf32.sf_si = siginfo32;
276                 } else  {
277                 #endif
278                         tf->fixreg[FIRSTARG+1] = (register_t)usfp +
279                             offsetof(struct sigframe, sf_si);
280                         sf.sf_si = ksi->ksi_info;
281                 #ifdef COMPAT_FREEBSD32
282                 }
283                 #endif
284         } else {
285                 /* Old FreeBSD-style arguments. */
286                 tf->fixreg[FIRSTARG+1] = code;
287                 #ifdef AIM
288                 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ? 
289                     tf->cpu.aim.dar : tf->srr0;
290                 #else
291                 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ? 
292                     tf->cpu.booke.dear : tf->srr0;
293                 #endif
294         }
295         mtx_unlock(&psp->ps_mtx);
296         PROC_UNLOCK(p);
297
298         tf->srr0 = (register_t)p->p_sysent->sv_sigcode_base;
299
300         /*
301          * copy the frame out to userland.
302          */
303         if (copyout(sfp, usfp, sfpsize) != 0) {
304                 /*
305                  * Process has trashed its stack. Kill it.
306                  */
307                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
308                 PROC_LOCK(p);
309                 sigexit(td, SIGILL);
310         }
311
312         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
313              tf->srr0, tf->fixreg[1]);
314
315         PROC_LOCK(p);
316         mtx_lock(&psp->ps_mtx);
317 }
318
319 int
320 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
321 {
322         ucontext_t uc;
323         int error;
324
325         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
326
327         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
328                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
329                 return (EFAULT);
330         }
331
332         error = set_mcontext(td, &uc.uc_mcontext);
333         if (error != 0)
334                 return (error);
335
336         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
337
338         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
339              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
340
341         return (EJUSTRETURN);
342 }
343
344 #ifdef COMPAT_FREEBSD4
345 int
346 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
347 {
348
349         return sys_sigreturn(td, (struct sigreturn_args *)uap);
350 }
351 #endif
352
353 /*
354  * Construct a PCB from a trapframe. This is called from kdb_trap() where
355  * we want to start a backtrace from the function that caused us to enter
356  * the debugger. We have the context in the trapframe, but base the trace
357  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
358  * enough for a backtrace.
359  */
360 void
361 makectx(struct trapframe *tf, struct pcb *pcb)
362 {
363
364         pcb->pcb_lr = tf->srr0;
365         pcb->pcb_sp = tf->fixreg[1];
366 }
367
368 /*
369  * get_mcontext/sendsig helper routine that doesn't touch the
370  * proc lock
371  */
372 static int
373 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
374 {
375         struct pcb *pcb;
376         int i;
377
378         pcb = td->td_pcb;
379
380         memset(mcp, 0, sizeof(mcontext_t));
381
382         mcp->mc_vers = _MC_VERSION;
383         mcp->mc_flags = 0;
384         memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
385         if (flags & GET_MC_CLEAR_RET) {
386                 mcp->mc_gpr[3] = 0;
387                 mcp->mc_gpr[4] = 0;
388         }
389
390         /*
391          * This assumes that floating-point context is *not* lazy,
392          * so if the thread has used FP there would have been a
393          * FP-unavailable exception that would have set things up
394          * correctly.
395          */
396         if (pcb->pcb_flags & PCB_FPREGS) {
397                 if (pcb->pcb_flags & PCB_FPU) {
398                         KASSERT(td == curthread,
399                                 ("get_mcontext: fp save not curthread"));
400                         critical_enter();
401                         save_fpu(td);
402                         critical_exit();
403                 }
404                 mcp->mc_flags |= _MC_FP_VALID;
405                 memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
406                 memcpy(mcp->mc_fpreg, pcb->pcb_fpu.fpr, 32*sizeof(double));
407                 for (i = 0; i < 32; i++)
408                         memcpy(&mcp->mc_fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
409                             sizeof(double));
410         }
411
412         /*
413          * Repeat for Altivec context
414          */
415
416         if (pcb->pcb_flags & PCB_VEC) {
417                 KASSERT(td == curthread,
418                         ("get_mcontext: fp save not curthread"));
419                 critical_enter();
420                 save_vec(td);
421                 critical_exit();
422                 mcp->mc_flags |= _MC_AV_VALID;
423                 mcp->mc_vscr  = pcb->pcb_vec.vscr;
424                 mcp->mc_vrsave =  pcb->pcb_vec.vrsave;
425                 memcpy(mcp->mc_avec, pcb->pcb_vec.vr, sizeof(mcp->mc_avec));
426         }
427
428         /* XXX VSX context */
429
430         mcp->mc_len = sizeof(*mcp);
431
432         return (0);
433 }
434
435 int
436 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
437 {
438         int error;
439
440         error = grab_mcontext(td, mcp, flags);
441         if (error == 0) {
442                 PROC_LOCK(curthread->td_proc);
443                 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
444                 PROC_UNLOCK(curthread->td_proc);
445         }
446
447         return (error);
448 }
449
450 int
451 set_mcontext(struct thread *td, mcontext_t *mcp)
452 {
453         struct pcb *pcb;
454         struct trapframe *tf;
455         register_t tls;
456         int i;
457
458         pcb = td->td_pcb;
459         tf = td->td_frame;
460
461         if (mcp->mc_vers != _MC_VERSION || mcp->mc_len != sizeof(*mcp))
462                 return (EINVAL);
463
464         /*
465          * Don't let the user set privileged MSR bits
466          */
467         if ((mcp->mc_srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC)) {
468                 return (EINVAL);
469         }
470
471         /* Copy trapframe, preserving TLS pointer across context change */
472         if (SV_PROC_FLAG(td->td_proc, SV_LP64))
473                 tls = tf->fixreg[13];
474         else
475                 tls = tf->fixreg[2];
476         memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
477         if (SV_PROC_FLAG(td->td_proc, SV_LP64))
478                 tf->fixreg[13] = tls;
479         else
480                 tf->fixreg[2] = tls;
481
482         if (mcp->mc_flags & _MC_FP_VALID) {
483                 /* enable_fpu() will happen lazily on a fault */
484                 pcb->pcb_flags |= PCB_FPREGS;
485                 memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
486                 bzero(pcb->pcb_fpu.fpr, sizeof(pcb->pcb_fpu.fpr));
487                 for (i = 0; i < 32; i++)
488                         memcpy(&pcb->pcb_fpu.fpr[i].fpr, &mcp->mc_fpreg[i],
489                             sizeof(double));
490         }
491
492         if (mcp->mc_flags & _MC_AV_VALID) {
493                 if ((pcb->pcb_flags & PCB_VEC) != PCB_VEC) {
494                         critical_enter();
495                         enable_vec(td);
496                         critical_exit();
497                 }
498                 pcb->pcb_vec.vscr = mcp->mc_vscr;
499                 pcb->pcb_vec.vrsave = mcp->mc_vrsave;
500                 memcpy(pcb->pcb_vec.vr, mcp->mc_avec, sizeof(mcp->mc_avec));
501         }
502
503         /* XXX VSX context */
504
505         return (0);
506 }
507
508 /*
509  * Set set up registers on exec.
510  */
511 void
512 exec_setregs(struct thread *td, struct image_params *imgp, u_long stack)
513 {
514         struct trapframe        *tf;
515         register_t              argc;
516         #ifdef __powerpc64__
517         register_t              entry_desc[3];
518         #endif
519
520         tf = trapframe(td);
521         bzero(tf, sizeof *tf);
522         #ifdef __powerpc64__
523         tf->fixreg[1] = -roundup(-stack + 48, 16);
524         #else
525         tf->fixreg[1] = -roundup(-stack + 8, 16);
526         #endif
527
528         /*
529          * Set up arguments for _start():
530          *      _start(argc, argv, envp, obj, cleanup, ps_strings);
531          *
532          * Notes:
533          *      - obj and cleanup are the auxilliary and termination
534          *        vectors.  They are fixed up by ld.elf_so.
535          *      - ps_strings is a NetBSD extention, and will be
536          *        ignored by executables which are strictly
537          *        compliant with the SVR4 ABI.
538          *
539          * XXX We have to set both regs and retval here due to different
540          * XXX calling convention in trap.c and init_main.c.
541          */
542
543         /* Collect argc from the user stack */
544         argc = fuword((void *)stack);
545
546         /*
547          * XXX PG: these get overwritten in the syscall return code.
548          * execve() should return EJUSTRETURN, like it does on NetBSD.
549          * Emulate by setting the syscall return value cells. The
550          * registers still have to be set for init's fork trampoline.
551          */
552         td->td_retval[0] = argc;
553         td->td_retval[1] = stack + sizeof(register_t);
554         tf->fixreg[3] = argc;
555         tf->fixreg[4] = stack + sizeof(register_t);
556         tf->fixreg[5] = stack + (2 + argc)*sizeof(register_t);
557         tf->fixreg[6] = 0;                              /* auxillary vector */
558         tf->fixreg[7] = 0;                              /* termination vector */
559         tf->fixreg[8] = (register_t)imgp->ps_strings;   /* NetBSD extension */
560
561         #ifdef __powerpc64__
562         /*
563          * For 64-bit, we need to disentangle the function descriptor
564          * 
565          * 0. entry point
566          * 1. TOC value (r2)
567          * 2. Environment pointer (r11)
568          */
569
570         (void)copyin((void *)imgp->entry_addr, entry_desc, sizeof(entry_desc));
571         tf->srr0 = entry_desc[0] + imgp->reloc_base;
572         tf->fixreg[2] = entry_desc[1] + imgp->reloc_base;
573         tf->fixreg[11] = entry_desc[2] + imgp->reloc_base;
574         tf->srr1 = PSL_SF | PSL_USERSET | PSL_FE_DFLT;
575         if (mfmsr() & PSL_HV)
576                 tf->srr1 |= PSL_HV;
577         #else
578         tf->srr0 = imgp->entry_addr;
579         tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
580         #endif
581         td->td_pcb->pcb_flags = 0;
582 }
583
584 #ifdef COMPAT_FREEBSD32
585 void
586 ppc32_setregs(struct thread *td, struct image_params *imgp, u_long stack)
587 {
588         struct trapframe        *tf;
589         uint32_t                argc;
590
591         tf = trapframe(td);
592         bzero(tf, sizeof *tf);
593         tf->fixreg[1] = -roundup(-stack + 8, 16);
594
595         argc = fuword32((void *)stack);
596
597         td->td_retval[0] = argc;
598         td->td_retval[1] = stack + sizeof(uint32_t);
599         tf->fixreg[3] = argc;
600         tf->fixreg[4] = stack + sizeof(uint32_t);
601         tf->fixreg[5] = stack + (2 + argc)*sizeof(uint32_t);
602         tf->fixreg[6] = 0;                              /* auxillary vector */
603         tf->fixreg[7] = 0;                              /* termination vector */
604         tf->fixreg[8] = (register_t)imgp->ps_strings;   /* NetBSD extension */
605
606         tf->srr0 = imgp->entry_addr;
607         tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
608         tf->srr1 &= ~PSL_SF;
609         if (mfmsr() & PSL_HV)
610                 tf->srr1 |= PSL_HV;
611         td->td_pcb->pcb_flags = 0;
612 }
613 #endif
614
615 int
616 fill_regs(struct thread *td, struct reg *regs)
617 {
618         struct trapframe *tf;
619
620         tf = td->td_frame;
621         memcpy(regs, tf, sizeof(struct reg));
622
623         return (0);
624 }
625
626 int
627 fill_dbregs(struct thread *td, struct dbreg *dbregs)
628 {
629         /* No debug registers on PowerPC */
630         return (ENOSYS);
631 }
632
633 int
634 fill_fpregs(struct thread *td, struct fpreg *fpregs)
635 {
636         struct pcb *pcb;
637
638         pcb = td->td_pcb;
639
640         if ((pcb->pcb_flags & PCB_FPREGS) == 0)
641                 memset(fpregs, 0, sizeof(struct fpreg));
642         else
643                 memcpy(fpregs, &pcb->pcb_fpu, sizeof(struct fpreg));
644
645         return (0);
646 }
647
648 int
649 set_regs(struct thread *td, struct reg *regs)
650 {
651         struct trapframe *tf;
652
653         tf = td->td_frame;
654         memcpy(tf, regs, sizeof(struct reg));
655         
656         return (0);
657 }
658
659 int
660 set_dbregs(struct thread *td, struct dbreg *dbregs)
661 {
662         /* No debug registers on PowerPC */
663         return (ENOSYS);
664 }
665
666 int
667 set_fpregs(struct thread *td, struct fpreg *fpregs)
668 {
669         struct pcb *pcb;
670
671         pcb = td->td_pcb;
672         pcb->pcb_flags |= PCB_FPREGS;
673         memcpy(&pcb->pcb_fpu, fpregs, sizeof(struct fpreg));
674
675         return (0);
676 }
677
678 #ifdef COMPAT_FREEBSD32
679 int
680 set_regs32(struct thread *td, struct reg32 *regs)
681 {
682         struct trapframe *tf;
683         int i;
684
685         tf = td->td_frame;
686         for (i = 0; i < 32; i++)
687                 tf->fixreg[i] = regs->fixreg[i];
688         tf->lr = regs->lr;
689         tf->cr = regs->cr;
690         tf->xer = regs->xer;
691         tf->ctr = regs->ctr;
692         tf->srr0 = regs->pc;
693
694         return (0);
695 }
696
697 int
698 fill_regs32(struct thread *td, struct reg32 *regs)
699 {
700         struct trapframe *tf;
701         int i;
702
703         tf = td->td_frame;
704         for (i = 0; i < 32; i++)
705                 regs->fixreg[i] = tf->fixreg[i];
706         regs->lr = tf->lr;
707         regs->cr = tf->cr;
708         regs->xer = tf->xer;
709         regs->ctr = tf->ctr;
710         regs->pc = tf->srr0;
711
712         return (0);
713 }
714
715 static int
716 grab_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
717 {
718         mcontext_t mcp64;
719         int i, error;
720
721         error = grab_mcontext(td, &mcp64, flags);
722         if (error != 0)
723                 return (error);
724         
725         mcp->mc_vers = mcp64.mc_vers;
726         mcp->mc_flags = mcp64.mc_flags;
727         mcp->mc_onstack = mcp64.mc_onstack;
728         mcp->mc_len = mcp64.mc_len;
729         memcpy(mcp->mc_avec,mcp64.mc_avec,sizeof(mcp64.mc_avec));
730         memcpy(mcp->mc_av,mcp64.mc_av,sizeof(mcp64.mc_av));
731         for (i = 0; i < 42; i++)
732                 mcp->mc_frame[i] = mcp64.mc_frame[i];
733         memcpy(mcp->mc_fpreg,mcp64.mc_fpreg,sizeof(mcp64.mc_fpreg));
734
735         return (0);
736 }
737
738 static int
739 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
740 {
741         int error;
742
743         error = grab_mcontext32(td, mcp, flags);
744         if (error == 0) {
745                 PROC_LOCK(curthread->td_proc);
746                 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
747                 PROC_UNLOCK(curthread->td_proc);
748         }
749
750         return (error);
751 }
752
753 static int
754 set_mcontext32(struct thread *td, mcontext32_t *mcp)
755 {
756         mcontext_t mcp64;
757         int i, error;
758
759         mcp64.mc_vers = mcp->mc_vers;
760         mcp64.mc_flags = mcp->mc_flags;
761         mcp64.mc_onstack = mcp->mc_onstack;
762         mcp64.mc_len = mcp->mc_len;
763         memcpy(mcp64.mc_avec,mcp->mc_avec,sizeof(mcp64.mc_avec));
764         memcpy(mcp64.mc_av,mcp->mc_av,sizeof(mcp64.mc_av));
765         for (i = 0; i < 42; i++)
766                 mcp64.mc_frame[i] = mcp->mc_frame[i];
767         mcp64.mc_srr1 |= (td->td_frame->srr1 & 0xFFFFFFFF00000000ULL);
768         memcpy(mcp64.mc_fpreg,mcp->mc_fpreg,sizeof(mcp64.mc_fpreg));
769
770         error = set_mcontext(td, &mcp64);
771
772         return (error);
773 }
774 #endif
775
776 #ifdef COMPAT_FREEBSD32
777 int
778 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
779 {
780         ucontext32_t uc;
781         int error;
782
783         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
784
785         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
786                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
787                 return (EFAULT);
788         }
789
790         error = set_mcontext32(td, &uc.uc_mcontext);
791         if (error != 0)
792                 return (error);
793
794         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
795
796         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
797              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
798
799         return (EJUSTRETURN);
800 }
801
802 /*
803  * The first two fields of a ucontext_t are the signal mask and the machine
804  * context.  The next field is uc_link; we want to avoid destroying the link
805  * when copying out contexts.
806  */
807 #define UC32_COPY_SIZE  offsetof(ucontext32_t, uc_link)
808
809 int
810 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
811 {
812         ucontext32_t uc;
813         int ret;
814
815         if (uap->ucp == NULL)
816                 ret = EINVAL;
817         else {
818                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
819                 PROC_LOCK(td->td_proc);
820                 uc.uc_sigmask = td->td_sigmask;
821                 PROC_UNLOCK(td->td_proc);
822                 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
823         }
824         return (ret);
825 }
826
827 int
828 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
829 {
830         ucontext32_t uc;
831         int ret;        
832
833         if (uap->ucp == NULL)
834                 ret = EINVAL;
835         else {
836                 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
837                 if (ret == 0) {
838                         ret = set_mcontext32(td, &uc.uc_mcontext);
839                         if (ret == 0) {
840                                 kern_sigprocmask(td, SIG_SETMASK,
841                                     &uc.uc_sigmask, NULL, 0);
842                         }
843                 }
844         }
845         return (ret == 0 ? EJUSTRETURN : ret);
846 }
847
848 int
849 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
850 {
851         ucontext32_t uc;
852         int ret;
853
854         if (uap->oucp == NULL || uap->ucp == NULL)
855                 ret = EINVAL;
856         else {
857                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
858                 PROC_LOCK(td->td_proc);
859                 uc.uc_sigmask = td->td_sigmask;
860                 PROC_UNLOCK(td->td_proc);
861                 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
862                 if (ret == 0) {
863                         ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
864                         if (ret == 0) {
865                                 ret = set_mcontext32(td, &uc.uc_mcontext);
866                                 if (ret == 0) {
867                                         kern_sigprocmask(td, SIG_SETMASK,
868                                             &uc.uc_sigmask, NULL, 0);
869                                 }
870                         }
871                 }
872         }
873         return (ret == 0 ? EJUSTRETURN : ret);
874 }
875
876 #endif
877
878 void
879 cpu_set_syscall_retval(struct thread *td, int error)
880 {
881         struct proc *p;
882         struct trapframe *tf;
883         int fixup;
884
885         if (error == EJUSTRETURN)
886                 return;
887
888         p = td->td_proc;
889         tf = td->td_frame;
890
891         if (tf->fixreg[0] == SYS___syscall &&
892             (SV_PROC_FLAG(p, SV_ILP32))) {
893                 int code = tf->fixreg[FIRSTARG + 1];
894                 if (p->p_sysent->sv_mask)
895                         code &= p->p_sysent->sv_mask;
896                 fixup = (code != SYS_freebsd6_lseek && code != SYS_lseek) ?
897                     1 : 0;
898         } else
899                 fixup = 0;
900
901         switch (error) {
902         case 0:
903                 if (fixup) {
904                         /*
905                          * 64-bit return, 32-bit syscall. Fixup byte order
906                          */
907                         tf->fixreg[FIRSTARG] = 0;
908                         tf->fixreg[FIRSTARG + 1] = td->td_retval[0];
909                 } else {
910                         tf->fixreg[FIRSTARG] = td->td_retval[0];
911                         tf->fixreg[FIRSTARG + 1] = td->td_retval[1];
912                 }
913                 tf->cr &= ~0x10000000;          /* Unset summary overflow */
914                 break;
915         case ERESTART:
916                 /*
917                  * Set user's pc back to redo the system call.
918                  */
919                 tf->srr0 -= 4;
920                 break;
921         default:
922                 if (p->p_sysent->sv_errsize) {
923                         error = (error < p->p_sysent->sv_errsize) ?
924                             p->p_sysent->sv_errtbl[error] : -1;
925                 }
926                 tf->fixreg[FIRSTARG] = error;
927                 tf->cr |= 0x10000000;           /* Set summary overflow */
928                 break;
929         }
930 }
931
932 /*
933  * Threading functions
934  */
935 void
936 cpu_thread_exit(struct thread *td)
937 {
938 }
939
940 void
941 cpu_thread_clean(struct thread *td)
942 {
943 }
944
945 void
946 cpu_thread_alloc(struct thread *td)
947 {
948         struct pcb *pcb;
949
950         pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
951             sizeof(struct pcb)) & ~0x2fUL);
952         td->td_pcb = pcb;
953         td->td_frame = (struct trapframe *)pcb - 1;
954 }
955
956 void
957 cpu_thread_free(struct thread *td)
958 {
959 }
960
961 int
962 cpu_set_user_tls(struct thread *td, void *tls_base)
963 {
964
965         if (SV_PROC_FLAG(td->td_proc, SV_LP64))
966                 td->td_frame->fixreg[13] = (register_t)tls_base + 0x7010;
967         else
968                 td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
969         return (0);
970 }
971
972 void
973 cpu_set_upcall(struct thread *td, struct thread *td0)
974 {
975         struct pcb *pcb2;
976         struct trapframe *tf;
977         struct callframe *cf;
978
979         pcb2 = td->td_pcb;
980
981         /* Copy the upcall pcb */
982         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
983
984         /* Create a stack for the new thread */
985         tf = td->td_frame;
986         bcopy(td0->td_frame, tf, sizeof(struct trapframe));
987         tf->fixreg[FIRSTARG] = 0;
988         tf->fixreg[FIRSTARG + 1] = 0;
989         tf->cr &= ~0x10000000;
990
991         /* Set registers for trampoline to user mode. */
992         cf = (struct callframe *)tf - 1;
993         memset(cf, 0, sizeof(struct callframe));
994         cf->cf_func = (register_t)fork_return;
995         cf->cf_arg0 = (register_t)td;
996         cf->cf_arg1 = (register_t)tf;
997
998         pcb2->pcb_sp = (register_t)cf;
999         #ifdef __powerpc64__
1000         pcb2->pcb_lr = ((register_t *)fork_trampoline)[0];
1001         pcb2->pcb_toc = ((register_t *)fork_trampoline)[1];
1002         #else
1003         pcb2->pcb_lr = (register_t)fork_trampoline;
1004         #endif
1005         pcb2->pcb_cpu.aim.usr_vsid = 0;
1006
1007         /* Setup to release spin count in fork_exit(). */
1008         td->td_md.md_spinlock_count = 1;
1009         td->td_md.md_saved_msr = PSL_KERNSET;
1010 }
1011
1012 void
1013 cpu_set_upcall_kse(struct thread *td, void (*entry)(void *), void *arg,
1014         stack_t *stack)
1015 {
1016         struct trapframe *tf;
1017         uintptr_t sp;
1018
1019         tf = td->td_frame;
1020         /* align stack and alloc space for frame ptr and saved LR */
1021         #ifdef __powerpc64__
1022         sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 48) &
1023             ~0x1f;
1024         #else
1025         sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 8) &
1026             ~0x1f;
1027         #endif
1028         bzero(tf, sizeof(struct trapframe));
1029
1030         tf->fixreg[1] = (register_t)sp;
1031         tf->fixreg[3] = (register_t)arg;
1032         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1033                 tf->srr0 = (register_t)entry;
1034                 tf->srr1 = PSL_USERSET | PSL_FE_DFLT;
1035                 #ifdef __powerpc64__
1036                 tf->srr1 &= ~PSL_SF;
1037                 #endif
1038         } else {
1039             #ifdef __powerpc64__
1040                 register_t entry_desc[3];
1041                 (void)copyin((void *)entry, entry_desc, sizeof(entry_desc));
1042                 tf->srr0 = entry_desc[0];
1043                 tf->fixreg[2] = entry_desc[1];
1044                 tf->fixreg[11] = entry_desc[2];
1045                 tf->srr1 = PSL_SF | PSL_USERSET | PSL_FE_DFLT;
1046             #endif
1047         }
1048
1049         #ifdef __powerpc64__
1050         if (mfmsr() & PSL_HV)
1051                 tf->srr1 |= PSL_HV;
1052         #endif
1053         td->td_pcb->pcb_flags = 0;
1054
1055         td->td_retval[0] = (register_t)entry;
1056         td->td_retval[1] = 0;
1057 }
1058
1059 int
1060 ppc_instr_emulate(struct trapframe *frame, struct pcb *pcb)
1061 {
1062         uint32_t instr;
1063         int reg, sig;
1064
1065         instr = fuword32((void *)frame->srr0);
1066         sig = SIGILL;
1067
1068         if ((instr & 0xfc1fffff) == 0x7c1f42a6) {       /* mfpvr */
1069                 reg = (instr & ~0xfc1fffff) >> 21;
1070                 frame->fixreg[reg] = mfpvr();
1071                 frame->srr0 += 4;
1072                 return (0);
1073         }
1074
1075         if ((instr & 0xfc000ffe) == 0x7c0004ac) {       /* various sync */
1076                 powerpc_sync(); /* Do a heavy-weight sync */
1077                 frame->srr0 += 4;
1078                 return (0);
1079         }
1080
1081 #ifdef FPU_EMU
1082         if (!(pcb->pcb_flags & PCB_FPREGS)) {
1083                 bzero(&pcb->pcb_fpu, sizeof(pcb->pcb_fpu));
1084                 pcb->pcb_flags |= PCB_FPREGS;
1085         }
1086         sig = fpu_emulate(frame, (struct fpreg *)&pcb->pcb_fpu);
1087 #endif
1088
1089         return (sig);
1090 }
1091