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