]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/powerpc/exec_machdep.c
uma: Make uma_zone_set_maxcache() work better with small limits
[FreeBSD/FreeBSD.git] / sys / powerpc / powerpc / exec_machdep.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause AND BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5  * Copyright (C) 1995, 1996 TooLs GmbH.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by TooLs GmbH.
19  * 4. The name of TooLs GmbH may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 /*-
34  * Copyright (C) 2001 Benno Rice
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  *
46  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
47  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
51  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
52  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
53  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
54  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
55  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
56  *      $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
57  */
58
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
61
62 #include "opt_fpu_emu.h"
63
64 #include <sys/param.h>
65 #include <sys/proc.h>
66 #include <sys/systm.h>
67 #include <sys/bio.h>
68 #include <sys/buf.h>
69 #include <sys/bus.h>
70 #include <sys/cons.h>
71 #include <sys/cpu.h>
72 #include <sys/exec.h>
73 #include <sys/imgact.h>
74 #include <sys/kernel.h>
75 #include <sys/ktr.h>
76 #include <sys/lock.h>
77 #include <sys/malloc.h>
78 #include <sys/mutex.h>
79 #include <sys/signalvar.h>
80 #include <sys/syscallsubr.h>
81 #include <sys/syscall.h>
82 #include <sys/sysent.h>
83 #include <sys/sysproto.h>
84 #include <sys/ucontext.h>
85 #include <sys/uio.h>
86
87 #include <machine/altivec.h>
88 #include <machine/cpu.h>
89 #include <machine/elf.h>
90 #include <machine/fpu.h>
91 #include <machine/pcb.h>
92 #include <machine/reg.h>
93 #include <machine/sigframe.h>
94 #include <machine/trap.h>
95 #include <machine/vmparam.h>
96
97 #include <vm/pmap.h>
98
99 #ifdef FPU_EMU
100 #include <powerpc/fpu/fpu_extern.h>
101 #endif
102
103 #ifdef COMPAT_FREEBSD32
104 #include <compat/freebsd32/freebsd32_signal.h>
105 #include <compat/freebsd32/freebsd32_util.h>
106 #include <compat/freebsd32/freebsd32_proto.h>
107
108 typedef struct __ucontext32 {
109         sigset_t                uc_sigmask;
110         mcontext32_t            uc_mcontext;
111         uint32_t                uc_link;
112         struct sigaltstack32    uc_stack;
113         uint32_t                uc_flags;
114         uint32_t                __spare__[4];
115 } ucontext32_t;
116
117 struct sigframe32 {
118         ucontext32_t            sf_uc;
119         struct siginfo32        sf_si;
120 };
121
122 static int      grab_mcontext32(struct thread *td, mcontext32_t *, int flags);
123 #endif
124
125 static int      grab_mcontext(struct thread *, mcontext_t *, int);
126
127 static void     cleanup_power_extras(struct thread *);
128
129 #ifdef __powerpc64__
130 extern struct sysentvec elf64_freebsd_sysvec_v2;
131 #endif
132
133 void
134 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
135 {
136         struct trapframe *tf;
137         struct sigacts *psp;
138         struct sigframe sf;
139         struct thread *td;
140         struct proc *p;
141         #ifdef COMPAT_FREEBSD32
142         struct siginfo32 siginfo32;
143         struct sigframe32 sf32;
144         #endif
145         size_t sfpsize;
146         caddr_t sfp, usfp;
147         register_t sp;
148         int oonstack, rndfsize;
149         int sig;
150         int code;
151
152         td = curthread;
153         p = td->td_proc;
154         PROC_LOCK_ASSERT(p, MA_OWNED);
155
156         psp = p->p_sigacts;
157         mtx_assert(&psp->ps_mtx, MA_OWNED);
158         tf = td->td_frame;
159
160         /*
161          * Fill siginfo structure.
162          */
163         ksi->ksi_info.si_signo = ksi->ksi_signo;
164         ksi->ksi_info.si_addr =
165             (void *)((tf->exc == EXC_DSI || tf->exc == EXC_DSE) ? 
166             tf->dar : tf->srr0);
167
168         #ifdef COMPAT_FREEBSD32
169         if (SV_PROC_FLAG(p, SV_ILP32)) {
170                 siginfo_to_siginfo32(&ksi->ksi_info, &siginfo32);
171                 sig = siginfo32.si_signo;
172                 code = siginfo32.si_code;
173                 sfp = (caddr_t)&sf32;
174                 sfpsize = sizeof(sf32);
175                 rndfsize = roundup(sizeof(sf32), 16);
176                 sp = (uint32_t)tf->fixreg[1];
177                 oonstack = sigonstack(sp);
178
179                 /*
180                  * Save user context
181                  */
182
183                 memset(&sf32, 0, sizeof(sf32));
184                 grab_mcontext32(td, &sf32.sf_uc.uc_mcontext, 0);
185
186                 sf32.sf_uc.uc_sigmask = *mask;
187                 sf32.sf_uc.uc_stack.ss_sp = (uintptr_t)td->td_sigstk.ss_sp;
188                 sf32.sf_uc.uc_stack.ss_size = (uint32_t)td->td_sigstk.ss_size;
189                 sf32.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
190                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
191
192                 sf32.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
193         } else {
194         #endif
195                 sig = ksi->ksi_signo;
196                 code = ksi->ksi_code;
197                 sfp = (caddr_t)&sf;
198                 sfpsize = sizeof(sf);
199                 #ifdef __powerpc64__
200                 /*
201                  * 64-bit PPC defines a 288 byte scratch region
202                  * below the stack.
203                  */
204                 rndfsize = 288 + roundup(sizeof(sf), 48);
205                 #else
206                 rndfsize = roundup(sizeof(sf), 16);
207                 #endif
208                 sp = tf->fixreg[1];
209                 oonstack = sigonstack(sp);
210
211                 /*
212                  * Save user context
213                  */
214
215                 memset(&sf, 0, sizeof(sf));
216                 grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
217
218                 sf.sf_uc.uc_sigmask = *mask;
219                 sf.sf_uc.uc_stack = td->td_sigstk;
220                 sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
221                     ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
222
223                 sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
224         #ifdef COMPAT_FREEBSD32
225         }
226         #endif
227
228         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
229              catcher, sig);
230
231         /*
232          * Allocate and validate space for the signal handler context.
233          */
234         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
235             SIGISMEMBER(psp->ps_sigonstack, sig)) {
236                 usfp = (void *)(((uintptr_t)td->td_sigstk.ss_sp +
237                    td->td_sigstk.ss_size - rndfsize) & ~0xFul);
238         } else {
239                 usfp = (void *)((sp - rndfsize) & ~0xFul);
240         }
241
242         /* 
243          * Set Floating Point facility to "Ignore Exceptions Mode" so signal
244          * handler can run. 
245          */
246         if (td->td_pcb->pcb_flags & PCB_FPU)
247                 tf->srr1 = tf->srr1 & ~(PSL_FE0 | PSL_FE1);
248
249
250         /*
251          * Set up the registers to return to sigcode.
252          *
253          *   r1/sp - sigframe ptr
254          *   lr    - sig function, dispatched to by blrl in trampoline
255          *   r3    - sig number
256          *   r4    - SIGINFO ? &siginfo : exception code
257          *   r5    - user context
258          *   srr0  - trampoline function addr
259          */
260         tf->lr = (register_t)catcher;
261         tf->fixreg[1] = (register_t)usfp;
262         tf->fixreg[FIRSTARG] = sig;
263         #ifdef COMPAT_FREEBSD32
264         tf->fixreg[FIRSTARG+2] = (register_t)usfp +
265             ((SV_PROC_FLAG(p, SV_ILP32)) ?
266             offsetof(struct sigframe32, sf_uc) :
267             offsetof(struct sigframe, sf_uc));
268         #else
269         tf->fixreg[FIRSTARG+2] = (register_t)usfp +
270             offsetof(struct sigframe, sf_uc);
271         #endif
272         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
273                 /*
274                  * Signal handler installed with SA_SIGINFO.
275                  */
276                 #ifdef COMPAT_FREEBSD32
277                 if (SV_PROC_FLAG(p, SV_ILP32)) {
278                         sf32.sf_si = siginfo32;
279                         tf->fixreg[FIRSTARG+1] = (register_t)usfp +
280                             offsetof(struct sigframe32, sf_si);
281                         sf32.sf_si = siginfo32;
282                 } else  {
283                 #endif
284                         tf->fixreg[FIRSTARG+1] = (register_t)usfp +
285                             offsetof(struct sigframe, sf_si);
286                         sf.sf_si = ksi->ksi_info;
287                 #ifdef COMPAT_FREEBSD32
288                 }
289                 #endif
290         } else {
291                 /* Old FreeBSD-style arguments. */
292                 tf->fixreg[FIRSTARG+1] = code;
293                 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ? 
294                     tf->dar : tf->srr0;
295         }
296         mtx_unlock(&psp->ps_mtx);
297         PROC_UNLOCK(p);
298
299         tf->srr0 = (register_t)p->p_sysent->sv_sigcode_base;
300
301         /*
302          * copy the frame out to userland.
303          */
304         if (copyout(sfp, usfp, sfpsize) != 0) {
305                 /*
306                  * Process has trashed its stack. Kill it.
307                  */
308                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
309                 PROC_LOCK(p);
310                 sigexit(td, SIGILL);
311         }
312
313         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
314              tf->srr0, tf->fixreg[1]);
315
316         PROC_LOCK(p);
317         mtx_lock(&psp->ps_mtx);
318 }
319
320 int
321 sys_sigreturn(struct thread *td, struct sigreturn_args *uap)
322 {
323         ucontext_t uc;
324         int error;
325
326         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
327
328         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
329                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
330                 return (EFAULT);
331         }
332
333         error = set_mcontext(td, &uc.uc_mcontext);
334         if (error != 0)
335                 return (error);
336
337         /* 
338          * Save FPU state if needed. User may have changed it on
339          * signal handler 
340          */ 
341         if (uc.uc_mcontext.mc_srr1 & PSL_FP)
342                 save_fpu(td);
343
344         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
345
346         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
347              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
348
349         return (EJUSTRETURN);
350 }
351
352 #ifdef COMPAT_FREEBSD4
353 int
354 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
355 {
356
357         return sys_sigreturn(td, (struct sigreturn_args *)uap);
358 }
359 #endif
360
361 /*
362  * Construct a PCB from a trapframe. This is called from kdb_trap() where
363  * we want to start a backtrace from the function that caused us to enter
364  * the debugger. We have the context in the trapframe, but base the trace
365  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
366  * enough for a backtrace.
367  */
368 void
369 makectx(struct trapframe *tf, struct pcb *pcb)
370 {
371
372         pcb->pcb_lr = tf->srr0;
373         pcb->pcb_sp = tf->fixreg[1];
374 }
375
376 /*
377  * get_mcontext/sendsig helper routine that doesn't touch the
378  * proc lock
379  */
380 static int
381 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
382 {
383         struct pcb *pcb;
384         int i;
385
386         pcb = td->td_pcb;
387
388         memset(mcp, 0, sizeof(mcontext_t));
389
390         mcp->mc_vers = _MC_VERSION;
391         mcp->mc_flags = 0;
392         memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
393         if (flags & GET_MC_CLEAR_RET) {
394                 mcp->mc_gpr[3] = 0;
395                 mcp->mc_gpr[4] = 0;
396         }
397
398         /*
399          * This assumes that floating-point context is *not* lazy,
400          * so if the thread has used FP there would have been a
401          * FP-unavailable exception that would have set things up
402          * correctly.
403          */
404         if (pcb->pcb_flags & PCB_FPREGS) {
405                 if (pcb->pcb_flags & PCB_FPU) {
406                         KASSERT(td == curthread,
407                                 ("get_mcontext: fp save not curthread"));
408                         critical_enter();
409                         save_fpu(td);
410                         critical_exit();
411                 }
412                 mcp->mc_flags |= _MC_FP_VALID;
413                 memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
414                 for (i = 0; i < 32; i++)
415                         memcpy(&mcp->mc_fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
416                             sizeof(double));
417         }
418
419         if (pcb->pcb_flags & PCB_VSX) {
420                 for (i = 0; i < 32; i++)
421                         memcpy(&mcp->mc_vsxfpreg[i],
422                             &pcb->pcb_fpu.fpr[i].vsr[2], sizeof(double));
423         }
424
425         /*
426          * Repeat for Altivec context
427          */
428
429         if (pcb->pcb_flags & PCB_VEC) {
430                 KASSERT(td == curthread,
431                         ("get_mcontext: fp save not curthread"));
432                 critical_enter();
433                 save_vec(td);
434                 critical_exit();
435                 mcp->mc_flags |= _MC_AV_VALID;
436                 mcp->mc_vscr  = pcb->pcb_vec.vscr;
437                 mcp->mc_vrsave =  pcb->pcb_vec.vrsave;
438                 memcpy(mcp->mc_avec, pcb->pcb_vec.vr, sizeof(mcp->mc_avec));
439         }
440
441         mcp->mc_len = sizeof(*mcp);
442
443         return (0);
444 }
445
446 int
447 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
448 {
449         int error;
450
451         error = grab_mcontext(td, mcp, flags);
452         if (error == 0) {
453                 PROC_LOCK(curthread->td_proc);
454                 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
455                 PROC_UNLOCK(curthread->td_proc);
456         }
457
458         return (error);
459 }
460
461 int
462 set_mcontext(struct thread *td, mcontext_t *mcp)
463 {
464         struct pcb *pcb;
465         struct trapframe *tf;
466         register_t tls;
467         int i;
468
469         pcb = td->td_pcb;
470         tf = td->td_frame;
471
472         if (mcp->mc_vers != _MC_VERSION || mcp->mc_len != sizeof(*mcp))
473                 return (EINVAL);
474
475         /*
476          * Don't let the user change privileged MSR bits.
477          *
478          * psl_userstatic is used here to mask off any bits that can
479          * legitimately vary between user contexts (Floating point
480          * exception control and any facilities that we are using the
481          * "enable on first use" pattern with.)
482          *
483          * All other bits are required to match psl_userset(32).
484          *
485          * Remember to update the platform cpu_init code when implementing
486          * support for a new conditional facility!
487          */
488         if ((mcp->mc_srr1 & psl_userstatic) != (tf->srr1 & psl_userstatic)) {
489                 return (EINVAL);
490         }
491
492         /* Copy trapframe, preserving TLS pointer across context change */
493         if (SV_PROC_FLAG(td->td_proc, SV_LP64))
494                 tls = tf->fixreg[13];
495         else
496                 tls = tf->fixreg[2];
497         memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
498         if (SV_PROC_FLAG(td->td_proc, SV_LP64))
499                 tf->fixreg[13] = tls;
500         else
501                 tf->fixreg[2] = tls;
502
503         /*
504          * Force the FPU back off to ensure the new context will not bypass
505          * the enable_fpu() setup code accidentally.
506          *
507          * This prevents an issue where a process that uses floating point
508          * inside a signal handler could end up in a state where the MSR
509          * did not match pcb_flags.
510          *
511          * Additionally, ensure VSX is disabled as well, as it is illegal
512          * to leave it turned on when FP or VEC are off.
513          */
514         tf->srr1 &= ~(PSL_FP | PSL_VSX);
515         pcb->pcb_flags &= ~(PCB_FPU | PCB_VSX);
516
517         if (mcp->mc_flags & _MC_FP_VALID) {
518                 /* enable_fpu() will happen lazily on a fault */
519                 pcb->pcb_flags |= PCB_FPREGS;
520                 memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
521                 bzero(pcb->pcb_fpu.fpr, sizeof(pcb->pcb_fpu.fpr));
522                 for (i = 0; i < 32; i++) {
523                         memcpy(&pcb->pcb_fpu.fpr[i].fpr, &mcp->mc_fpreg[i],
524                             sizeof(double));
525                         memcpy(&pcb->pcb_fpu.fpr[i].vsr[2],
526                             &mcp->mc_vsxfpreg[i], sizeof(double));
527                 }
528         }
529
530         if (mcp->mc_flags & _MC_AV_VALID) {
531                 if ((pcb->pcb_flags & PCB_VEC) != PCB_VEC) {
532                         critical_enter();
533                         enable_vec(td);
534                         critical_exit();
535                 }
536                 pcb->pcb_vec.vscr = mcp->mc_vscr;
537                 pcb->pcb_vec.vrsave = mcp->mc_vrsave;
538                 memcpy(pcb->pcb_vec.vr, mcp->mc_avec, sizeof(mcp->mc_avec));
539         } else {
540                 tf->srr1 &= ~PSL_VEC;
541                 pcb->pcb_flags &= ~PCB_VEC;
542         }
543
544         return (0);
545 }
546
547 /*
548  * Clean up extra POWER state.  Some per-process registers and states are not
549  * managed by the MSR, so must be cleaned up explicitly on thread exit.
550  *
551  * Currently this includes:
552  * DSCR -- Data stream control register (PowerISA 2.06+)
553  * FSCR -- Facility Status and Control Register (PowerISA 2.07+)
554  */
555 static void
556 cleanup_power_extras(struct thread *td)
557 {
558         uint32_t pcb_flags;
559
560         if (td != curthread)
561                 return;
562
563         pcb_flags = td->td_pcb->pcb_flags;
564         /* Clean up registers not managed by MSR. */
565         if (pcb_flags & PCB_CFSCR)
566                 mtspr(SPR_FSCR, 0);
567         if (pcb_flags & PCB_CDSCR) 
568                 mtspr(SPR_DSCRP, 0);
569
570         cleanup_fpscr();
571 }
572
573 /*
574  * Set set up registers on exec.
575  */
576 void
577 exec_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
578 {
579         struct trapframe        *tf;
580         register_t              argc;
581
582         tf = trapframe(td);
583         bzero(tf, sizeof *tf);
584         #ifdef __powerpc64__
585         tf->fixreg[1] = -roundup(-stack + 48, 16);
586         #else
587         tf->fixreg[1] = -roundup(-stack + 8, 16);
588         #endif
589
590         /*
591          * Set up arguments for _start():
592          *      _start(argc, argv, envp, obj, cleanup, ps_strings);
593          *
594          * Notes:
595          *      - obj and cleanup are the auxilliary and termination
596          *        vectors.  They are fixed up by ld.elf_so.
597          *      - ps_strings is a NetBSD extention, and will be
598          *        ignored by executables which are strictly
599          *        compliant with the SVR4 ABI.
600          */
601
602         /* Collect argc from the user stack */
603         argc = fuword((void *)stack);
604
605         tf->fixreg[3] = argc;
606         tf->fixreg[4] = stack + sizeof(register_t);
607         tf->fixreg[5] = stack + (2 + argc)*sizeof(register_t);
608         tf->fixreg[6] = 0;                              /* auxillary vector */
609         tf->fixreg[7] = 0;                              /* termination vector */
610         tf->fixreg[8] = (register_t)imgp->ps_strings;   /* NetBSD extension */
611
612         tf->srr0 = imgp->entry_addr;
613         #ifdef __powerpc64__
614         tf->fixreg[12] = imgp->entry_addr;
615         #endif
616         tf->srr1 = psl_userset | PSL_FE_DFLT;
617         cleanup_power_extras(td);
618         td->td_pcb->pcb_flags = 0;
619 }
620
621 #ifdef COMPAT_FREEBSD32
622 void
623 ppc32_setregs(struct thread *td, struct image_params *imgp, uintptr_t stack)
624 {
625         struct trapframe        *tf;
626         uint32_t                argc;
627
628         tf = trapframe(td);
629         bzero(tf, sizeof *tf);
630         tf->fixreg[1] = -roundup(-stack + 8, 16);
631
632         argc = fuword32((void *)stack);
633
634         tf->fixreg[3] = argc;
635         tf->fixreg[4] = stack + sizeof(uint32_t);
636         tf->fixreg[5] = stack + (2 + argc)*sizeof(uint32_t);
637         tf->fixreg[6] = 0;                              /* auxillary vector */
638         tf->fixreg[7] = 0;                              /* termination vector */
639         tf->fixreg[8] = (register_t)imgp->ps_strings;   /* NetBSD extension */
640
641         tf->srr0 = imgp->entry_addr;
642         tf->srr1 = psl_userset32 | PSL_FE_DFLT;
643         cleanup_power_extras(td);
644         td->td_pcb->pcb_flags = 0;
645 }
646 #endif
647
648 int
649 fill_regs(struct thread *td, struct reg *regs)
650 {
651         struct trapframe *tf;
652
653         tf = td->td_frame;
654         memcpy(regs, tf, sizeof(struct reg));
655
656         return (0);
657 }
658
659 int
660 fill_dbregs(struct thread *td, struct dbreg *dbregs)
661 {
662         /* No debug registers on PowerPC */
663         return (ENOSYS);
664 }
665
666 int
667 fill_fpregs(struct thread *td, struct fpreg *fpregs)
668 {
669         struct pcb *pcb;
670         int i;
671
672         pcb = td->td_pcb;
673
674         if ((pcb->pcb_flags & PCB_FPREGS) == 0)
675                 memset(fpregs, 0, sizeof(struct fpreg));
676         else {
677                 memcpy(&fpregs->fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
678                 for (i = 0; i < 32; i++)
679                         memcpy(&fpregs->fpreg[i], &pcb->pcb_fpu.fpr[i].fpr,
680                             sizeof(double));
681         }
682
683         return (0);
684 }
685
686 int
687 set_regs(struct thread *td, struct reg *regs)
688 {
689         struct trapframe *tf;
690
691         tf = td->td_frame;
692         memcpy(tf, regs, sizeof(struct reg));
693
694         return (0);
695 }
696
697 int
698 set_dbregs(struct thread *td, struct dbreg *dbregs)
699 {
700         /* No debug registers on PowerPC */
701         return (ENOSYS);
702 }
703
704 int
705 set_fpregs(struct thread *td, struct fpreg *fpregs)
706 {
707         struct pcb *pcb;
708         int i;
709
710         pcb = td->td_pcb;
711         pcb->pcb_flags |= PCB_FPREGS;
712         memcpy(&pcb->pcb_fpu.fpscr, &fpregs->fpscr, sizeof(double));
713         for (i = 0; i < 32; i++) {
714                 memcpy(&pcb->pcb_fpu.fpr[i].fpr, &fpregs->fpreg[i],
715                     sizeof(double));
716         }
717
718         return (0);
719 }
720
721 #ifdef COMPAT_FREEBSD32
722 int
723 set_regs32(struct thread *td, struct reg32 *regs)
724 {
725         struct trapframe *tf;
726         int i;
727
728         tf = td->td_frame;
729         for (i = 0; i < 32; i++)
730                 tf->fixreg[i] = regs->fixreg[i];
731         tf->lr = regs->lr;
732         tf->cr = regs->cr;
733         tf->xer = regs->xer;
734         tf->ctr = regs->ctr;
735         tf->srr0 = regs->pc;
736
737         return (0);
738 }
739
740 int
741 fill_regs32(struct thread *td, struct reg32 *regs)
742 {
743         struct trapframe *tf;
744         int i;
745
746         tf = td->td_frame;
747         for (i = 0; i < 32; i++)
748                 regs->fixreg[i] = tf->fixreg[i];
749         regs->lr = tf->lr;
750         regs->cr = tf->cr;
751         regs->xer = tf->xer;
752         regs->ctr = tf->ctr;
753         regs->pc = tf->srr0;
754
755         return (0);
756 }
757
758 static int
759 grab_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
760 {
761         mcontext_t mcp64;
762         int i, error;
763
764         error = grab_mcontext(td, &mcp64, flags);
765         if (error != 0)
766                 return (error);
767
768         mcp->mc_vers = mcp64.mc_vers;
769         mcp->mc_flags = mcp64.mc_flags;
770         mcp->mc_onstack = mcp64.mc_onstack;
771         mcp->mc_len = mcp64.mc_len;
772         memcpy(mcp->mc_avec,mcp64.mc_avec,sizeof(mcp64.mc_avec));
773         memcpy(mcp->mc_av,mcp64.mc_av,sizeof(mcp64.mc_av));
774         for (i = 0; i < 42; i++)
775                 mcp->mc_frame[i] = mcp64.mc_frame[i];
776         memcpy(mcp->mc_fpreg,mcp64.mc_fpreg,sizeof(mcp64.mc_fpreg));
777         memcpy(mcp->mc_vsxfpreg,mcp64.mc_vsxfpreg,sizeof(mcp64.mc_vsxfpreg));
778
779         return (0);
780 }
781
782 static int
783 get_mcontext32(struct thread *td, mcontext32_t *mcp, int flags)
784 {
785         int error;
786
787         error = grab_mcontext32(td, mcp, flags);
788         if (error == 0) {
789                 PROC_LOCK(curthread->td_proc);
790                 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
791                 PROC_UNLOCK(curthread->td_proc);
792         }
793
794         return (error);
795 }
796
797 static int
798 set_mcontext32(struct thread *td, mcontext32_t *mcp)
799 {
800         mcontext_t mcp64;
801         int i, error;
802
803         mcp64.mc_vers = mcp->mc_vers;
804         mcp64.mc_flags = mcp->mc_flags;
805         mcp64.mc_onstack = mcp->mc_onstack;
806         mcp64.mc_len = mcp->mc_len;
807         memcpy(mcp64.mc_avec,mcp->mc_avec,sizeof(mcp64.mc_avec));
808         memcpy(mcp64.mc_av,mcp->mc_av,sizeof(mcp64.mc_av));
809         for (i = 0; i < 42; i++)
810                 mcp64.mc_frame[i] = mcp->mc_frame[i];
811         mcp64.mc_srr1 |= (td->td_frame->srr1 & 0xFFFFFFFF00000000ULL);
812         memcpy(mcp64.mc_fpreg,mcp->mc_fpreg,sizeof(mcp64.mc_fpreg));
813         memcpy(mcp64.mc_vsxfpreg,mcp->mc_vsxfpreg,sizeof(mcp64.mc_vsxfpreg));
814
815         error = set_mcontext(td, &mcp64);
816
817         return (error);
818 }
819 #endif
820
821 #ifdef COMPAT_FREEBSD32
822 int
823 freebsd32_sigreturn(struct thread *td, struct freebsd32_sigreturn_args *uap)
824 {
825         ucontext32_t uc;
826         int error;
827
828         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
829
830         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
831                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
832                 return (EFAULT);
833         }
834
835         error = set_mcontext32(td, &uc.uc_mcontext);
836         if (error != 0)
837                 return (error);
838
839         /*
840          * Save FPU state if needed. User may have changed it on
841          * signal handler
842          */
843         if (uc.uc_mcontext.mc_srr1 & PSL_FP)
844                 save_fpu(td);
845
846         kern_sigprocmask(td, SIG_SETMASK, &uc.uc_sigmask, NULL, 0);
847
848         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
849              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
850
851         return (EJUSTRETURN);
852 }
853
854 /*
855  * The first two fields of a ucontext_t are the signal mask and the machine
856  * context.  The next field is uc_link; we want to avoid destroying the link
857  * when copying out contexts.
858  */
859 #define UC32_COPY_SIZE  offsetof(ucontext32_t, uc_link)
860
861 int
862 freebsd32_getcontext(struct thread *td, struct freebsd32_getcontext_args *uap)
863 {
864         ucontext32_t uc;
865         int ret;
866
867         if (uap->ucp == NULL)
868                 ret = EINVAL;
869         else {
870                 bzero(&uc, sizeof(uc));
871                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
872                 PROC_LOCK(td->td_proc);
873                 uc.uc_sigmask = td->td_sigmask;
874                 PROC_UNLOCK(td->td_proc);
875                 ret = copyout(&uc, uap->ucp, UC32_COPY_SIZE);
876         }
877         return (ret);
878 }
879
880 int
881 freebsd32_setcontext(struct thread *td, struct freebsd32_setcontext_args *uap)
882 {
883         ucontext32_t uc;
884         int ret;        
885
886         if (uap->ucp == NULL)
887                 ret = EINVAL;
888         else {
889                 ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
890                 if (ret == 0) {
891                         ret = set_mcontext32(td, &uc.uc_mcontext);
892                         if (ret == 0) {
893                                 kern_sigprocmask(td, SIG_SETMASK,
894                                     &uc.uc_sigmask, NULL, 0);
895                         }
896                 }
897         }
898         return (ret == 0 ? EJUSTRETURN : ret);
899 }
900
901 int
902 freebsd32_swapcontext(struct thread *td, struct freebsd32_swapcontext_args *uap)
903 {
904         ucontext32_t uc;
905         int ret;
906
907         if (uap->oucp == NULL || uap->ucp == NULL)
908                 ret = EINVAL;
909         else {
910                 bzero(&uc, sizeof(uc));
911                 get_mcontext32(td, &uc.uc_mcontext, GET_MC_CLEAR_RET);
912                 PROC_LOCK(td->td_proc);
913                 uc.uc_sigmask = td->td_sigmask;
914                 PROC_UNLOCK(td->td_proc);
915                 ret = copyout(&uc, uap->oucp, UC32_COPY_SIZE);
916                 if (ret == 0) {
917                         ret = copyin(uap->ucp, &uc, UC32_COPY_SIZE);
918                         if (ret == 0) {
919                                 ret = set_mcontext32(td, &uc.uc_mcontext);
920                                 if (ret == 0) {
921                                         kern_sigprocmask(td, SIG_SETMASK,
922                                             &uc.uc_sigmask, NULL, 0);
923                                 }
924                         }
925                 }
926         }
927         return (ret == 0 ? EJUSTRETURN : ret);
928 }
929
930 #endif
931
932 void
933 cpu_set_syscall_retval(struct thread *td, int error)
934 {
935         struct proc *p;
936         struct trapframe *tf;
937         int fixup;
938
939         if (error == EJUSTRETURN)
940                 return;
941
942         p = td->td_proc;
943         tf = td->td_frame;
944
945         if (tf->fixreg[0] == SYS___syscall &&
946             (SV_PROC_FLAG(p, SV_ILP32))) {
947                 int code = tf->fixreg[FIRSTARG + 1];
948                 fixup = (
949 #if defined(COMPAT_FREEBSD6) && defined(SYS_freebsd6_lseek)
950                     code != SYS_freebsd6_lseek &&
951 #endif
952                     code != SYS_lseek) ?  1 : 0;
953         } else
954                 fixup = 0;
955
956         switch (error) {
957         case 0:
958                 if (fixup) {
959                         /*
960                          * 64-bit return, 32-bit syscall. Fixup byte order
961                          */
962                         tf->fixreg[FIRSTARG] = 0;
963                         tf->fixreg[FIRSTARG + 1] = td->td_retval[0];
964                 } else {
965                         tf->fixreg[FIRSTARG] = td->td_retval[0];
966                         tf->fixreg[FIRSTARG + 1] = td->td_retval[1];
967                 }
968                 tf->cr &= ~0x10000000;          /* Unset summary overflow */
969                 break;
970         case ERESTART:
971                 /*
972                  * Set user's pc back to redo the system call.
973                  */
974                 tf->srr0 -= 4;
975                 break;
976         default:
977                 tf->fixreg[FIRSTARG] = error;
978                 tf->cr |= 0x10000000;           /* Set summary overflow */
979                 break;
980         }
981 }
982
983 /*
984  * Threading functions
985  */
986 void
987 cpu_thread_exit(struct thread *td)
988 {
989         cleanup_power_extras(td);
990 }
991
992 void
993 cpu_thread_clean(struct thread *td)
994 {
995 }
996
997 void
998 cpu_thread_alloc(struct thread *td)
999 {
1000         struct pcb *pcb;
1001
1002         pcb = (struct pcb *)((td->td_kstack + td->td_kstack_pages * PAGE_SIZE -
1003             sizeof(struct pcb)) & ~0x2fUL);
1004         td->td_pcb = pcb;
1005         td->td_frame = (struct trapframe *)pcb - 1;
1006 }
1007
1008 void
1009 cpu_thread_free(struct thread *td)
1010 {
1011 }
1012
1013 int
1014 cpu_set_user_tls(struct thread *td, void *tls_base)
1015 {
1016
1017         if (SV_PROC_FLAG(td->td_proc, SV_LP64))
1018                 td->td_frame->fixreg[13] = (register_t)tls_base + 0x7010;
1019         else
1020                 td->td_frame->fixreg[2] = (register_t)tls_base + 0x7008;
1021         return (0);
1022 }
1023
1024 void
1025 cpu_copy_thread(struct thread *td, struct thread *td0)
1026 {
1027         struct pcb *pcb2;
1028         struct trapframe *tf;
1029         struct callframe *cf;
1030
1031         pcb2 = td->td_pcb;
1032
1033         /* Copy the upcall pcb */
1034         bcopy(td0->td_pcb, pcb2, sizeof(*pcb2));
1035
1036         /* Create a stack for the new thread */
1037         tf = td->td_frame;
1038         bcopy(td0->td_frame, tf, sizeof(struct trapframe));
1039         tf->fixreg[FIRSTARG] = 0;
1040         tf->fixreg[FIRSTARG + 1] = 0;
1041         tf->cr &= ~0x10000000;
1042
1043         /* Set registers for trampoline to user mode. */
1044         cf = (struct callframe *)tf - 1;
1045         memset(cf, 0, sizeof(struct callframe));
1046         cf->cf_func = (register_t)fork_return;
1047         cf->cf_arg0 = (register_t)td;
1048         cf->cf_arg1 = (register_t)tf;
1049
1050         pcb2->pcb_sp = (register_t)cf;
1051         #if defined(__powerpc64__) && (!defined(_CALL_ELF) || _CALL_ELF == 1)
1052         pcb2->pcb_lr = ((register_t *)fork_trampoline)[0];
1053         pcb2->pcb_toc = ((register_t *)fork_trampoline)[1];
1054         #else
1055         pcb2->pcb_lr = (register_t)fork_trampoline;
1056         pcb2->pcb_context[0] = pcb2->pcb_lr;
1057         #endif
1058         pcb2->pcb_cpu.aim.usr_vsid = 0;
1059 #ifdef __SPE__
1060         pcb2->pcb_vec.vscr = SPEFSCR_FINVE | SPEFSCR_FDBZE |
1061             SPEFSCR_FUNFE | SPEFSCR_FOVFE;
1062 #endif
1063
1064         /* Setup to release spin count in fork_exit(). */
1065         td->td_md.md_spinlock_count = 1;
1066         td->td_md.md_saved_msr = psl_kernset;
1067 }
1068
1069 void
1070 cpu_set_upcall(struct thread *td, void (*entry)(void *), void *arg,
1071     stack_t *stack)
1072 {
1073         struct trapframe *tf;
1074         uintptr_t sp;
1075
1076         tf = td->td_frame;
1077         /* align stack and alloc space for frame ptr and saved LR */
1078         #ifdef __powerpc64__
1079         sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 48) &
1080             ~0x1f;
1081         #else
1082         sp = ((uintptr_t)stack->ss_sp + stack->ss_size - 8) &
1083             ~0x1f;
1084         #endif
1085         bzero(tf, sizeof(struct trapframe));
1086
1087         tf->fixreg[1] = (register_t)sp;
1088         tf->fixreg[3] = (register_t)arg;
1089         if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1090                 tf->srr0 = (register_t)entry;
1091                 #ifdef __powerpc64__
1092                 tf->srr1 = psl_userset32 | PSL_FE_DFLT;
1093                 #else
1094                 tf->srr1 = psl_userset | PSL_FE_DFLT;
1095                 #endif
1096         } else {
1097             #ifdef __powerpc64__
1098                 if (td->td_proc->p_sysent == &elf64_freebsd_sysvec_v2) {
1099                         tf->srr0 = (register_t)entry;
1100                         /* ELFv2 ABI requires that the global entry point be in r12. */
1101                         tf->fixreg[12] = (register_t)entry;
1102                 }
1103                 else {
1104                         register_t entry_desc[3];
1105                         (void)copyin((void *)entry, entry_desc, sizeof(entry_desc));
1106                         tf->srr0 = entry_desc[0];
1107                         tf->fixreg[2] = entry_desc[1];
1108                         tf->fixreg[11] = entry_desc[2];
1109                 }
1110                 tf->srr1 = psl_userset | PSL_FE_DFLT;
1111             #endif
1112         }
1113
1114         td->td_pcb->pcb_flags = 0;
1115 #ifdef __SPE__
1116         td->td_pcb->pcb_vec.vscr = SPEFSCR_FINVE | SPEFSCR_FDBZE |
1117             SPEFSCR_FUNFE | SPEFSCR_FOVFE;
1118 #endif
1119
1120         td->td_retval[0] = (register_t)entry;
1121         td->td_retval[1] = 0;
1122 }
1123
1124 static int
1125 emulate_mfspr(int spr, int reg, struct trapframe *frame){
1126         struct thread *td;
1127
1128         td = curthread;
1129
1130         if (spr == SPR_DSCR || spr == SPR_DSCRP) {
1131                 if (!(cpu_features2 & PPC_FEATURE2_DSCR))
1132                         return (SIGILL);
1133                 // If DSCR was never set, get the default DSCR
1134                 if ((td->td_pcb->pcb_flags & PCB_CDSCR) == 0)
1135                         td->td_pcb->pcb_dscr = mfspr(SPR_DSCRP);
1136
1137                 frame->fixreg[reg] = td->td_pcb->pcb_dscr;
1138                 frame->srr0 += 4;
1139                 return (0);
1140         } else
1141                 return (SIGILL);
1142 }
1143
1144 static int
1145 emulate_mtspr(int spr, int reg, struct trapframe *frame){
1146         struct thread *td;
1147
1148         td = curthread;
1149
1150         if (spr == SPR_DSCR || spr == SPR_DSCRP) {
1151                 if (!(cpu_features2 & PPC_FEATURE2_DSCR))
1152                         return (SIGILL);
1153                 td->td_pcb->pcb_flags |= PCB_CDSCR;
1154                 td->td_pcb->pcb_dscr = frame->fixreg[reg];
1155                 mtspr(SPR_DSCRP, frame->fixreg[reg]);
1156                 frame->srr0 += 4;
1157                 return (0);
1158         } else
1159                 return (SIGILL);
1160 }
1161
1162 #define XFX 0xFC0007FF
1163 int
1164 ppc_instr_emulate(struct trapframe *frame, struct thread *td)
1165 {
1166         struct pcb *pcb;
1167         uint32_t instr;
1168         int reg, sig;
1169         int rs, spr;
1170
1171         instr = fuword32((void *)frame->srr0);
1172         sig = SIGILL;
1173
1174         if ((instr & 0xfc1fffff) == 0x7c1f42a6) {       /* mfpvr */
1175                 reg = (instr & ~0xfc1fffff) >> 21;
1176                 frame->fixreg[reg] = mfpvr();
1177                 frame->srr0 += 4;
1178                 return (0);
1179         } else if ((instr & XFX) == 0x7c0002a6) {       /* mfspr */
1180                 rs = (instr &  0x3e00000) >> 21;
1181                 spr = (instr & 0x1ff800) >> 16;
1182                 return emulate_mfspr(spr, rs, frame);
1183         } else if ((instr & XFX) == 0x7c0003a6) {       /* mtspr */
1184                 rs = (instr &  0x3e00000) >> 21;
1185                 spr = (instr & 0x1ff800) >> 16;
1186                 return emulate_mtspr(spr, rs, frame);
1187         } else if ((instr & 0xfc000ffe) == 0x7c0004ac) {        /* various sync */
1188                 powerpc_sync(); /* Do a heavy-weight sync */
1189                 frame->srr0 += 4;
1190                 return (0);
1191         }
1192
1193         pcb = td->td_pcb;
1194 #ifdef FPU_EMU
1195         if (!(pcb->pcb_flags & PCB_FPREGS)) {
1196                 bzero(&pcb->pcb_fpu, sizeof(pcb->pcb_fpu));
1197                 pcb->pcb_flags |= PCB_FPREGS;
1198         } else if (pcb->pcb_flags & PCB_FPU)
1199                 save_fpu(td);
1200         sig = fpu_emulate(frame, &pcb->pcb_fpu);
1201         if ((sig == 0 || sig == SIGFPE) && pcb->pcb_flags & PCB_FPU)
1202                 enable_fpu(td);
1203 #endif
1204         if (sig == SIGILL) {
1205                 if (pcb->pcb_lastill != frame->srr0) {
1206                         /* Allow a second chance, in case of cache sync issues. */
1207                         sig = 0;
1208                         pmap_sync_icache(PCPU_GET(curpmap), frame->srr0, 4);
1209                         pcb->pcb_lastill = frame->srr0;
1210                 }
1211         }
1212
1213         return (sig);
1214 }