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