]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/powerpc/powerpc/machdep.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / powerpc / powerpc / 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_ddb.h"
62 #include "opt_kstack_pages.h"
63 #include "opt_msgbuf.h"
64
65 #include <sys/param.h>
66 #include <sys/proc.h>
67 #include <sys/systm.h>
68 #include <sys/bio.h>
69 #include <sys/buf.h>
70 #include <sys/bus.h>
71 #include <sys/cons.h>
72 #include <sys/cpu.h>
73 #include <sys/eventhandler.h>
74 #include <sys/exec.h>
75 #include <sys/imgact.h>
76 #include <sys/kdb.h>
77 #include <sys/kernel.h>
78 #include <sys/ktr.h>
79 #include <sys/linker.h>
80 #include <sys/lock.h>
81 #include <sys/malloc.h>
82 #include <sys/mbuf.h>
83 #include <sys/msgbuf.h>
84 #include <sys/mutex.h>
85 #include <sys/ptrace.h>
86 #include <sys/reboot.h>
87 #include <sys/signalvar.h>
88 #include <sys/sysctl.h>
89 #include <sys/sysent.h>
90 #include <sys/sysproto.h>
91 #include <sys/ucontext.h>
92 #include <sys/uio.h>
93 #include <sys/vmmeter.h>
94 #include <sys/vnode.h>
95
96 #include <net/netisr.h>
97
98 #include <vm/vm.h>
99 #include <vm/vm_extern.h>
100 #include <vm/vm_kern.h>
101 #include <vm/vm_page.h>
102 #include <vm/vm_map.h>
103 #include <vm/vm_object.h>
104 #include <vm/vm_pager.h>
105
106 #include <machine/altivec.h>
107 #include <machine/bat.h>
108 #include <machine/cpu.h>
109 #include <machine/elf.h>
110 #include <machine/fpu.h>
111 #include <machine/md_var.h>
112 #include <machine/metadata.h>
113 #include <machine/mmuvar.h>
114 #include <machine/pcb.h>
115 #include <machine/powerpc.h>
116 #include <machine/reg.h>
117 #include <machine/sigframe.h>
118 #include <machine/trap.h>
119 #include <machine/vmparam.h>
120
121 #include <ddb/ddb.h>
122
123 #include <dev/ofw/openfirm.h>
124
125 #ifdef DDB
126 extern vm_offset_t ksym_start, ksym_end;
127 #endif
128
129 int cold = 1;
130
131 struct          pcpu __pcpu[MAXCPU];
132 struct          trapframe frame0;
133
134 vm_offset_t     kstack0;
135 vm_offset_t     kstack0_phys;
136
137 char            machine[] = "powerpc";
138 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
139
140 static int cacheline_size = CACHELINESIZE;
141 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
142            CTLFLAG_RD, &cacheline_size, 0, "");
143
144 static void     cpu_startup(void *);
145 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_startup, NULL);
146
147 void            powerpc_init(u_int, u_int, u_int, void *);
148
149 int             save_ofw_mapping(void);
150 int             restore_ofw_mapping(void);
151
152 void            install_extint(void (*)(void));
153
154 int             setfault(faultbuf);             /* defined in locore.S */
155
156 static int      grab_mcontext(struct thread *, mcontext_t *, int);
157
158 void            asm_panic(char *);
159
160 long            Maxmem = 0;
161 long            realmem = 0;
162
163 struct pmap     ofw_pmap;
164 extern int      ofmsr;
165
166 struct bat      battable[16];
167
168 struct kva_md_info kmi;
169
170 void setPQL2(int *const size, int *const ways);
171
172 void
173 setPQL2(int *const size, int *const ways)
174 {
175         return;
176 }
177
178 static void
179 powerpc_ofw_shutdown(void *junk, int howto)
180 {
181         if (howto & RB_HALT) {
182                 OF_halt();
183         }
184         OF_reboot();
185 }
186
187 static void
188 cpu_startup(void *dummy)
189 {
190
191         /*
192          * Initialise the decrementer-based clock.
193          */
194         decr_init();
195
196         /*
197          * Good {morning,afternoon,evening,night}.
198          */
199         cpu_setup(PCPU_GET(cpuid));
200
201         /* startrtclock(); */
202 #ifdef PERFMON
203         perfmon_init();
204 #endif
205         printf("real memory  = %ld (%ld MB)\n", ptoa(physmem),
206             ptoa(physmem) / 1048576);
207         realmem = physmem;
208
209         /*
210          * Display any holes after the first chunk of extended memory.
211          */
212         if (bootverbose) {
213                 int indx;
214
215                 printf("Physical memory chunk(s):\n");
216                 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
217                         int size1 = phys_avail[indx + 1] - phys_avail[indx];
218
219                         printf("0x%08x - 0x%08x, %d bytes (%d pages)\n",
220                             phys_avail[indx], phys_avail[indx + 1] - 1, size1,
221                             size1 / PAGE_SIZE);
222                 }
223         }
224
225         vm_ksubmap_init(&kmi);
226
227         printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
228             ptoa(cnt.v_free_count) / 1048576);
229
230         /*
231          * Set up buffers, so they can be used to read disk labels.
232          */
233         bufinit();
234         vm_pager_bufferinit();
235
236         EVENTHANDLER_REGISTER(shutdown_final, powerpc_ofw_shutdown, 0,
237             SHUTDOWN_PRI_LAST);
238
239 #ifdef SMP
240         /*
241          * OK, enough kmem_alloc/malloc state should be up, lets get on with it!
242          */
243         mp_start();                     /* fire up the secondaries */
244         mp_announce();
245 #endif  /* SMP */
246 }
247
248 extern char     kernel_text[], _end[];
249
250 extern void     *trapcode, *trapsize;
251 extern void     *alitrap, *alisize;
252 extern void     *dsitrap, *dsisize;
253 extern void     *decrint, *decrsize;
254 extern void     *extint, *extsize;
255 extern void     *dblow, *dbsize;
256
257 void
258 powerpc_init(u_int startkernel, u_int endkernel, u_int basekernel, void *mdp)
259 {
260         struct          pcpu *pc;
261         vm_offset_t     end, off;
262         void            *kmdp;
263         char            *env;
264
265         end = 0;
266         kmdp = NULL;
267
268         /*
269          * Parse metadata if present and fetch parameters.  Must be done
270          * before console is inited so cninit gets the right value of
271          * boothowto.
272          */
273         if (mdp != NULL) {
274                 preload_metadata = mdp;
275                 kmdp = preload_search_by_type("elf kernel");
276                 if (kmdp != NULL) {
277                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
278                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
279                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
280 #ifdef DDB
281                         ksym_start = MD_FETCH(kmdp, MODINFOMD_SSYM, uintptr_t);
282                         ksym_end = MD_FETCH(kmdp, MODINFOMD_ESYM, uintptr_t);
283 #endif
284                 }
285         }
286
287         /*
288          * Init params/tunables that can be overridden by the loader
289          */
290         init_param1();
291
292         /*
293          * Start initializing proc0 and thread0.
294          */
295         proc_linkup0(&proc0, &thread0);
296         thread0.td_frame = &frame0;
297
298         /*
299          * Set up per-cpu data.
300          */
301         pc = &__pcpu[0];
302         pcpu_init(pc, 0, sizeof(struct pcpu));
303         pc->pc_curthread = &thread0;
304         pc->pc_curpcb = thread0.td_pcb;
305         pc->pc_cpuid = 0;
306
307         __asm __volatile("mtsprg 0, %0" :: "r"(pc));
308
309         mutex_init();
310
311         /*
312          * Initialize the console before printing anything.
313          */
314         cninit();
315
316         /*
317          * Complain if there is no metadata.
318          */
319         if (mdp == NULL || kmdp == NULL) {
320                 printf("powerpc_init: no loader metadata.\n");
321         }
322
323         kdb_init();
324
325         kobj_machdep_init();
326
327         /*
328          * XXX: Initialize the interrupt tables.
329          *      Disable translation in case the vector area
330          *      hasn't been mapped (G5)
331          */
332         mtmsr(mfmsr() & ~(PSL_IR | PSL_DR));
333         isync();
334         bcopy(&trapcode, (void *)EXC_RST,  (size_t)&trapsize);
335         bcopy(&trapcode, (void *)EXC_MCHK, (size_t)&trapsize);
336         bcopy(&dsitrap,  (void *)EXC_DSI,  (size_t)&dsisize);
337         bcopy(&trapcode, (void *)EXC_ISI,  (size_t)&trapsize);
338         bcopy(&trapcode, (void *)EXC_EXI,  (size_t)&trapsize);
339         bcopy(&alitrap,  (void *)EXC_ALI,  (size_t)&alisize);
340         bcopy(&trapcode, (void *)EXC_PGM,  (size_t)&trapsize);
341         bcopy(&trapcode, (void *)EXC_FPU,  (size_t)&trapsize);
342         bcopy(&trapcode, (void *)EXC_DECR, (size_t)&trapsize);
343         bcopy(&trapcode, (void *)EXC_SC,   (size_t)&trapsize);
344         bcopy(&trapcode, (void *)EXC_TRC,  (size_t)&trapsize);
345         bcopy(&trapcode, (void *)EXC_FPA,  (size_t)&trapsize);
346         bcopy(&trapcode, (void *)EXC_VEC,  (size_t)&trapsize);
347         bcopy(&trapcode, (void *)EXC_VECAST, (size_t)&trapsize);
348         bcopy(&trapcode, (void *)EXC_THRM, (size_t)&trapsize);
349         bcopy(&trapcode, (void *)EXC_BPT,  (size_t)&trapsize);
350 #ifdef KDB
351         bcopy(&dblow,    (void *)EXC_RST,  (size_t)&dbsize);
352         bcopy(&dblow,    (void *)EXC_MCHK, (size_t)&dbsize);
353         bcopy(&dblow,   (void *)EXC_PGM,  (size_t)&dbsize);
354         bcopy(&dblow,   (void *)EXC_TRC,  (size_t)&dbsize);
355         bcopy(&dblow,   (void *)EXC_BPT,  (size_t)&dbsize);
356 #endif
357         __syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
358
359         /*
360          * Make sure translation has been enabled
361          */
362         mtmsr(mfmsr() | PSL_IR|PSL_DR|PSL_ME|PSL_RI);
363         isync();
364
365         /*
366          * Initialise virtual memory.
367          */
368         pmap_mmu_install(MMU_TYPE_OEA, 0);              /* XXX temporary */
369         pmap_bootstrap(startkernel, endkernel);
370
371         /*
372          * Initialize params/tunables that are derived from memsize
373          */
374         init_param2(physmem);
375
376         /*
377          * Grab booted kernel's name
378          */
379         env = getenv("kernelname");
380         if (env != NULL) {
381                 strlcpy(kernelname, env, sizeof(kernelname));
382                 freeenv(env);
383         }
384
385         /*
386          * Finish setting up thread0.
387          */
388         thread0.td_kstack = kstack0;
389         thread0.td_pcb = (struct pcb *)
390             (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
391
392         /*
393          * Map and initialise the message buffer.
394          */
395         for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
396                 pmap_kenter((vm_offset_t)msgbufp + off, msgbuf_phys + off);
397         msgbufinit(msgbufp, MSGBUF_SIZE);
398
399 #ifdef KDB
400         if (boothowto & RB_KDB)
401                 kdb_enter_why(KDB_WHY_BOOTFLAGS,
402                     "Boot flags requested debugger");
403 #endif
404 }
405
406 void
407 bzero(void *buf, size_t len)
408 {
409         caddr_t p;
410
411         p = buf;
412
413         while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
414                 *p++ = 0;
415                 len--;
416         }
417
418         while (len >= sizeof(u_long) * 8) {
419                 *(u_long*) p = 0;
420                 *((u_long*) p + 1) = 0;
421                 *((u_long*) p + 2) = 0;
422                 *((u_long*) p + 3) = 0;
423                 len -= sizeof(u_long) * 8;
424                 *((u_long*) p + 4) = 0;
425                 *((u_long*) p + 5) = 0;
426                 *((u_long*) p + 6) = 0;
427                 *((u_long*) p + 7) = 0;
428                 p += sizeof(u_long) * 8;
429         }
430
431         while (len >= sizeof(u_long)) {
432                 *(u_long*) p = 0;
433                 len -= sizeof(u_long);
434                 p += sizeof(u_long);
435         }
436
437         while (len) {
438                 *p++ = 0;
439                 len--;
440         }
441 }
442
443 void
444 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
445 {
446         struct trapframe *tf;
447         struct sigframe *sfp;
448         struct sigacts *psp;
449         struct sigframe sf;
450         struct thread *td;
451         struct proc *p;
452         int oonstack, rndfsize;
453         int sig;
454         int code;
455
456         td = curthread;
457         p = td->td_proc;
458         PROC_LOCK_ASSERT(p, MA_OWNED);
459         sig = ksi->ksi_signo;
460         code = ksi->ksi_code;
461         psp = p->p_sigacts;
462         mtx_assert(&psp->ps_mtx, MA_OWNED);
463         tf = td->td_frame;
464         oonstack = sigonstack(tf->fixreg[1]);
465
466         rndfsize = ((sizeof(sf) + 15) / 16) * 16;
467
468         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
469              catcher, sig);
470
471         /*
472          * Save user context
473          */
474         memset(&sf, 0, sizeof(sf));
475         grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
476         sf.sf_uc.uc_sigmask = *mask;
477         sf.sf_uc.uc_stack = td->td_sigstk;
478         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
479             ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
480
481         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
482
483         /*
484          * Allocate and validate space for the signal handler context.
485          */
486         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
487             SIGISMEMBER(psp->ps_sigonstack, sig)) {
488                 sfp = (struct sigframe *)(td->td_sigstk.ss_sp +
489                    td->td_sigstk.ss_size - rndfsize);
490         } else {
491                 sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
492         }
493
494         /*
495          * Translate the signal if appropriate (Linux emu ?)
496          */
497         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
498                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
499
500         /*
501          * Save the floating-point state, if necessary, then copy it.
502          */
503         /* XXX */
504
505         /*
506          * Set up the registers to return to sigcode.
507          *
508          *   r1/sp - sigframe ptr
509          *   lr    - sig function, dispatched to by blrl in trampoline
510          *   r3    - sig number
511          *   r4    - SIGINFO ? &siginfo : exception code
512          *   r5    - user context
513          *   srr0  - trampoline function addr
514          */
515         tf->lr = (register_t)catcher;
516         tf->fixreg[1] = (register_t)sfp;
517         tf->fixreg[FIRSTARG] = sig;
518         tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
519         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
520                 /*
521                  * Signal handler installed with SA_SIGINFO.
522                  */
523                 tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
524
525                 /*
526                  * Fill siginfo structure.
527                  */
528                 sf.sf_si = ksi->ksi_info;
529                 sf.sf_si.si_signo = sig;
530                 sf.sf_si.si_addr = (void *) ((tf->exc == EXC_DSI) ? 
531                                              tf->dar : tf->srr0);
532         } else {
533                 /* Old FreeBSD-style arguments. */
534                 tf->fixreg[FIRSTARG+1] = code;
535                 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ? 
536                                              tf->dar : tf->srr0;
537         }
538         mtx_unlock(&psp->ps_mtx);
539         PROC_UNLOCK(p);
540
541         tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
542
543         /*
544          * copy the frame out to userland.
545          */
546         if (copyout(&sf, sfp, sizeof(*sfp)) != 0) {
547                 /*
548                  * Process has trashed its stack. Kill it.
549                  */
550                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
551                 PROC_LOCK(p);
552                 sigexit(td, SIGILL);
553         }
554
555         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
556              tf->srr0, tf->fixreg[1]);
557
558         PROC_LOCK(p);
559         mtx_lock(&psp->ps_mtx);
560 }
561
562 int
563 sigreturn(struct thread *td, struct sigreturn_args *uap)
564 {
565         struct proc *p;
566         ucontext_t uc;
567         int error;
568
569         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
570
571         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
572                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
573                 return (EFAULT);
574         }
575
576         error = set_mcontext(td, &uc.uc_mcontext);
577         if (error != 0)
578                 return (error);
579
580         p = td->td_proc;
581         PROC_LOCK(p);
582         td->td_sigmask = uc.uc_sigmask;
583         SIG_CANTMASK(td->td_sigmask);
584         signotify(td);
585         PROC_UNLOCK(p);
586
587         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
588              td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
589
590         return (EJUSTRETURN);
591 }
592
593 #ifdef COMPAT_FREEBSD4
594 int
595 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
596 {
597
598         return sigreturn(td, (struct sigreturn_args *)uap);
599 }
600 #endif
601
602 /*
603  * Construct a PCB from a trapframe. This is called from kdb_trap() where
604  * we want to start a backtrace from the function that caused us to enter
605  * the debugger. We have the context in the trapframe, but base the trace
606  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
607  * enough for a backtrace.
608  */
609 void
610 makectx(struct trapframe *tf, struct pcb *pcb)
611 {
612
613         pcb->pcb_lr = tf->srr0;
614         pcb->pcb_sp = tf->fixreg[1];
615 }
616
617 /*
618  * get_mcontext/sendsig helper routine that doesn't touch the
619  * proc lock
620  */
621 static int
622 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
623 {
624         struct pcb *pcb;
625
626         pcb = td->td_pcb;
627
628         memset(mcp, 0, sizeof(mcontext_t));
629
630         mcp->mc_vers = _MC_VERSION;
631         mcp->mc_flags = 0;
632         memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
633         if (flags & GET_MC_CLEAR_RET) {
634                 mcp->mc_gpr[3] = 0;
635                 mcp->mc_gpr[4] = 0;
636         }
637
638         /*
639          * This assumes that floating-point context is *not* lazy,
640          * so if the thread has used FP there would have been a
641          * FP-unavailable exception that would have set things up
642          * correctly.
643          */
644         if (pcb->pcb_flags & PCB_FPU) {
645                 KASSERT(td == curthread,
646                         ("get_mcontext: fp save not curthread"));
647                 critical_enter();
648                 save_fpu(td);
649                 critical_exit();
650                 mcp->mc_flags |= _MC_FP_VALID;
651                 memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
652                 memcpy(mcp->mc_fpreg, pcb->pcb_fpu.fpr, 32*sizeof(double));
653         }
654
655         /*
656          * Repeat for Altivec context
657          */
658
659         if (pcb->pcb_flags & PCB_VEC) {
660                 KASSERT(td == curthread,
661                         ("get_mcontext: fp save not curthread"));
662                 critical_enter();
663                 save_vec(td);
664                 critical_exit();
665                 mcp->mc_flags |= _MC_AV_VALID;
666                 mcp->mc_vscr  = pcb->pcb_vec.vscr;
667                 mcp->mc_vrsave =  pcb->pcb_vec.vrsave;
668                 memcpy(mcp->mc_avec, pcb->pcb_vec.vr, sizeof(mcp->mc_avec));
669         }
670
671         mcp->mc_len = sizeof(*mcp);
672
673         return (0);
674 }
675
676 int
677 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
678 {
679         int error;
680
681         error = grab_mcontext(td, mcp, flags);
682         if (error == 0) {
683                 PROC_LOCK(curthread->td_proc);
684                 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
685                 PROC_UNLOCK(curthread->td_proc);
686         }
687
688         return (error);
689 }
690
691 int
692 set_mcontext(struct thread *td, const mcontext_t *mcp)
693 {
694         struct pcb *pcb;
695         struct trapframe *tf;
696
697         pcb = td->td_pcb;
698         tf = td->td_frame;
699
700         if (mcp->mc_vers != _MC_VERSION ||
701             mcp->mc_len != sizeof(*mcp))
702                 return (EINVAL);
703
704         /*
705          * Don't let the user set privileged MSR bits
706          */
707         if ((mcp->mc_srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC)) {
708                 return (EINVAL);
709         }
710
711         memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
712
713         if (mcp->mc_flags & _MC_FP_VALID) {
714                 if ((pcb->pcb_flags & PCB_FPU) != PCB_FPU) {
715                         critical_enter();
716                         enable_fpu(td);
717                         critical_exit();
718                 }
719                 memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
720                 memcpy(pcb->pcb_fpu.fpr, mcp->mc_fpreg, 32*sizeof(double));
721         }
722
723         if (mcp->mc_flags & _MC_AV_VALID) {
724                 if ((pcb->pcb_flags & PCB_VEC) != PCB_VEC) {
725                         critical_enter();
726                         enable_vec(td);
727                         critical_exit();
728                 }
729                 pcb->pcb_vec.vscr = mcp->mc_vscr;
730                 pcb->pcb_vec.vrsave = mcp->mc_vrsave;
731                 memcpy(pcb->pcb_vec.vr, mcp->mc_avec, sizeof(mcp->mc_avec));
732         }
733
734
735         return (0);
736 }
737
738 void
739 cpu_boot(int howto)
740 {
741 }
742
743 void
744 cpu_initclocks(void)
745 {
746
747         decr_tc_init();
748 }
749
750 /* Get current clock frequency for the given cpu id. */
751 int
752 cpu_est_clockrate(int cpu_id, uint64_t *rate)
753 {
754
755         return (ENXIO);
756 }
757
758 /*
759  * Shutdown the CPU as much as possible.
760  */
761 void
762 cpu_halt(void)
763 {
764
765         OF_exit();
766 }
767
768 void
769 cpu_idle(void)
770 {
771         /* TODO: Insert code to halt (until next interrupt) */
772
773 #ifdef INVARIANTS
774         if ((mfmsr() & PSL_EE) != PSL_EE) {
775                 struct thread *td = curthread;
776                 printf("td msr %x\n", td->td_md.md_saved_msr);
777                 panic("ints disabled in idleproc!");
778         }
779 #endif
780 }
781
782 /*
783  * Set set up registers on exec.
784  */
785 void
786 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
787 {
788         struct trapframe        *tf;
789         struct ps_strings       arginfo;
790
791         tf = trapframe(td);
792         bzero(tf, sizeof *tf);
793         tf->fixreg[1] = -roundup(-stack + 8, 16);
794
795         /*
796          * XXX Machine-independent code has already copied arguments and
797          * XXX environment to userland.  Get them back here.
798          */
799         (void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
800
801         /*
802          * Set up arguments for _start():
803          *      _start(argc, argv, envp, obj, cleanup, ps_strings);
804          *
805          * Notes:
806          *      - obj and cleanup are the auxilliary and termination
807          *        vectors.  They are fixed up by ld.elf_so.
808          *      - ps_strings is a NetBSD extention, and will be
809          *        ignored by executables which are strictly
810          *        compliant with the SVR4 ABI.
811          *
812          * XXX We have to set both regs and retval here due to different
813          * XXX calling convention in trap.c and init_main.c.
814          */
815         /*
816          * XXX PG: these get overwritten in the syscall return code.
817          * execve() should return EJUSTRETURN, like it does on NetBSD.
818          * Emulate by setting the syscall return value cells. The
819          * registers still have to be set for init's fork trampoline.
820          */
821         td->td_retval[0] = arginfo.ps_nargvstr;
822         td->td_retval[1] = (register_t)arginfo.ps_argvstr;
823         tf->fixreg[3] = arginfo.ps_nargvstr;
824         tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
825         tf->fixreg[5] = (register_t)arginfo.ps_envstr;
826         tf->fixreg[6] = 0;                      /* auxillary vector */
827         tf->fixreg[7] = 0;                      /* termination vector */
828         tf->fixreg[8] = (register_t)PS_STRINGS; /* NetBSD extension */
829
830         tf->srr0 = entry;
831         tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
832         td->td_pcb->pcb_flags = 0;
833 }
834
835 int
836 fill_regs(struct thread *td, struct reg *regs)
837 {
838         struct trapframe *tf;
839
840         tf = td->td_frame;
841         memcpy(regs, tf, sizeof(struct reg));
842
843         return (0);
844 }
845
846 int
847 fill_dbregs(struct thread *td, struct dbreg *dbregs)
848 {
849         /* No debug registers on PowerPC */
850         return (ENOSYS);
851 }
852
853 int
854 fill_fpregs(struct thread *td, struct fpreg *fpregs)
855 {
856         struct pcb *pcb;
857
858         pcb = td->td_pcb;
859
860         if ((pcb->pcb_flags & PCB_FPU) == 0)
861                 memset(fpregs, 0, sizeof(struct fpreg));
862         else
863                 memcpy(fpregs, &pcb->pcb_fpu, sizeof(struct fpreg));
864
865         return (0);
866 }
867
868 int
869 set_regs(struct thread *td, struct reg *regs)
870 {
871         struct trapframe *tf;
872
873         tf = td->td_frame;
874         memcpy(tf, regs, sizeof(struct reg));
875         
876         return (0);
877 }
878
879 int
880 set_dbregs(struct thread *td, struct dbreg *dbregs)
881 {
882         /* No debug registers on PowerPC */
883         return (ENOSYS);
884 }
885
886 int
887 set_fpregs(struct thread *td, struct fpreg *fpregs)
888 {
889         struct pcb *pcb;
890
891         pcb = td->td_pcb;
892         if ((pcb->pcb_flags & PCB_FPU) == 0)
893                 enable_fpu(td);
894         memcpy(&pcb->pcb_fpu, fpregs, sizeof(struct fpreg));
895
896         return (0);
897 }
898
899 int
900 ptrace_set_pc(struct thread *td, unsigned long addr)
901 {
902         struct trapframe *tf;
903
904         tf = td->td_frame;
905         tf->srr0 = (register_t)addr;
906
907         return (0);
908 }
909
910 int
911 ptrace_single_step(struct thread *td)
912 {
913         struct trapframe *tf;
914         
915         tf = td->td_frame;
916         tf->srr1 |= PSL_SE;
917
918         return (0);
919 }
920
921 int
922 ptrace_clear_single_step(struct thread *td)
923 {
924         struct trapframe *tf;
925
926         tf = td->td_frame;
927         tf->srr1 &= ~PSL_SE;
928
929         return (0);
930 }
931
932 /*
933  * Initialise a struct pcpu.
934  */
935 void
936 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
937 {
938
939 }
940
941 void
942 spinlock_enter(void)
943 {
944         struct thread *td;
945
946         td = curthread;
947         if (td->td_md.md_spinlock_count == 0)
948                 td->td_md.md_saved_msr = intr_disable();
949         td->td_md.md_spinlock_count++;
950         critical_enter();
951 }
952
953 void
954 spinlock_exit(void)
955 {
956         struct thread *td;
957
958         td = curthread;
959         critical_exit();
960         td->td_md.md_spinlock_count--;
961         if (td->td_md.md_spinlock_count == 0)
962                 intr_restore(td->td_md.md_saved_msr);
963 }
964
965 /*
966  * kcopy(const void *src, void *dst, size_t len);
967  *
968  * Copy len bytes from src to dst, aborting if we encounter a fatal
969  * page fault.
970  *
971  * kcopy() _must_ save and restore the old fault handler since it is
972  * called by uiomove(), which may be in the path of servicing a non-fatal
973  * page fault.
974  */
975 int
976 kcopy(const void *src, void *dst, size_t len)
977 {
978         struct thread   *td;
979         faultbuf        env, *oldfault;
980         int             rv;
981
982         td = PCPU_GET(curthread);
983         oldfault = td->td_pcb->pcb_onfault;
984         if ((rv = setfault(env)) != 0) {
985                 td->td_pcb->pcb_onfault = oldfault;
986                 return rv;
987         }
988
989         memcpy(dst, src, len);
990
991         td->td_pcb->pcb_onfault = oldfault;
992         return (0);
993 }
994
995 void
996 asm_panic(char *pstr)
997 {
998         panic(pstr);
999 }
1000
1001 int db_trap_glue(struct trapframe *);           /* Called from trap_subr.S */
1002
1003 int
1004 db_trap_glue(struct trapframe *frame)
1005 {
1006         if (!(frame->srr1 & PSL_PR)
1007             && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
1008                 || (frame->exc == EXC_PGM
1009                     && (frame->srr1 & 0x20000))
1010                 || frame->exc == EXC_BPT
1011                 || frame->exc == EXC_DSI)) {
1012                 int type = frame->exc;
1013                 if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
1014                         type = T_BREAKPOINT;
1015                 }
1016                 return (kdb_trap(type, 0, frame));
1017         }
1018
1019         return (0);
1020 }