]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/powerpc/booke/machdep.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[FreeBSD/FreeBSD.git] / sys / powerpc / booke / machdep.c
1 /*-
2  * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19  * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 /*-
28  * Copyright (C) 2001 Benno Rice
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  *
40  * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
41  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
43  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
45  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
46  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  * $NetBSD: machdep.c,v 1.74.2.1 2000/11/01 16:13:48 tv Exp $
51  */
52 /*-
53  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
54  * Copyright (C) 1995, 1996 TooLs GmbH.
55  * All rights reserved.
56  *
57  * Redistribution and use in source and binary forms, with or without
58  * modification, are permitted provided that the following conditions
59  * are met:
60  * 1. Redistributions of source code must retain the above copyright
61  *    notice, this list of conditions and the following disclaimer.
62  * 2. Redistributions in binary form must reproduce the above copyright
63  *    notice, this list of conditions and the following disclaimer in the
64  *    documentation and/or other materials provided with the distribution.
65  * 3. All advertising materials mentioning features or use of this software
66  *    must display the following acknowledgement:
67  *      This product includes software developed by TooLs GmbH.
68  * 4. The name of TooLs GmbH may not be used to endorse or promote products
69  *    derived from this software without specific prior written permission.
70  *
71  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
72  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
73  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
74  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
75  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
76  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
77  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
78  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
79  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
80  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
81  */
82
83 #include <sys/cdefs.h>
84 __FBSDID("$FreeBSD$");
85
86 #include "opt_compat.h"
87 #include "opt_kstack_pages.h"
88
89 #include <sys/cdefs.h>
90 #include <sys/types.h>
91
92 #include <sys/param.h>
93 #include <sys/proc.h>
94 #include <sys/systm.h>
95 #include <sys/time.h>
96 #include <sys/bio.h>
97 #include <sys/buf.h>
98 #include <sys/bus.h>
99 #include <sys/cons.h>
100 #include <sys/cpu.h>
101 #include <sys/kdb.h>
102 #include <sys/kernel.h>
103 #include <sys/lock.h>
104 #include <sys/mutex.h>
105 #include <sys/sysctl.h>
106 #include <sys/exec.h>
107 #include <sys/ktr.h>
108 #include <sys/sysproto.h>
109 #include <sys/signalvar.h>
110 #include <sys/sysent.h>
111 #include <sys/imgact.h>
112 #include <sys/msgbuf.h>
113 #include <sys/ptrace.h>
114
115 #include <vm/vm.h>
116 #include <vm/pmap.h>
117 #include <vm/vm_page.h>
118 #include <vm/vm_object.h>
119 #include <vm/vm_pager.h>
120
121 #include <machine/cpu.h>
122 #include <machine/kdb.h>
123 #include <machine/reg.h>
124 #include <machine/vmparam.h>
125 #include <machine/spr.h>
126 #include <machine/hid.h>
127 #include <machine/psl.h>
128 #include <machine/trap.h>
129 #include <machine/md_var.h>
130 #include <machine/mmuvar.h>
131 #include <machine/pmap.h>
132 #include <machine/sigframe.h>
133 #include <machine/metadata.h>
134 #include <machine/bootinfo.h>
135 #include <machine/powerpc.h>
136
137 #include <sys/linker.h>
138 #include <sys/reboot.h>
139
140 #ifdef  DEBUG
141 #define debugf(fmt, args...) printf(fmt, ##args)
142 #else
143 #define debugf(fmt, args...)
144 #endif
145
146 extern unsigned char kernel_text[];
147 extern unsigned char _etext[];
148 extern unsigned char _edata[];
149 extern unsigned char __bss_start[];
150 extern unsigned char __sbss_start[];
151 extern unsigned char __sbss_end[];
152 extern unsigned char _end[];
153
154 extern struct mem_region availmem_regions[];
155 extern int availmem_regions_sz;
156
157 extern void dcache_enable(void);
158 extern void dcache_inval(void);
159 extern void icache_enable(void);
160 extern void icache_inval(void);
161
162 struct kva_md_info kmi;
163 struct pcpu __pcpu[MAXCPU];
164 struct trapframe frame0;
165 int cold = 1;
166 long realmem = 0;
167 long Maxmem = 0;
168
169 struct bootinfo *bootinfo;
170
171 char machine[] = "powerpc";
172 SYSCTL_STRING(_hw, HW_MACHINE, machine, CTLFLAG_RD, machine, 0, "");
173
174 int cacheline_size = 32;
175
176 SYSCTL_INT(_machdep, CPU_CACHELINE, cacheline_size,
177            CTLFLAG_RD, &cacheline_size, 0, "");
178
179 static void cpu_e500_startup(void *);
180 SYSINIT(cpu, SI_SUB_CPU, SI_ORDER_FIRST, cpu_e500_startup, NULL);
181
182 void print_kernel_section_addr(void);
183 void dump_bootinfo(void);
184 void dump_kenv(void);
185 u_int e500_init(u_int32_t, u_int32_t, void *);
186
187 static void
188 cpu_e500_startup(void *dummy)
189 {
190         int indx, size;
191
192         /* Initialise the decrementer-based clock. */
193         decr_init();
194
195         /* Good {morning,afternoon,evening,night}. */
196         cpu_setup(PCPU_GET(cpuid));
197
198         printf("real memory  = %ld (%ld MB)\n", ptoa(physmem),
199             ptoa(physmem) / 1048576);
200         realmem = physmem;
201
202         /* Display any holes after the first chunk of extended memory. */
203         if (bootverbose) {
204                 printf("Physical memory chunk(s):\n");
205                 for (indx = 0; phys_avail[indx + 1] != 0; indx += 2) {
206                         size = phys_avail[indx + 1] - phys_avail[indx];
207                         printf("0x%08x - 0x%08x, %d bytes (%d pages)\n",
208                             phys_avail[indx], phys_avail[indx + 1] - 1, size,
209                             size / PAGE_SIZE);
210                 }
211         }
212
213         vm_ksubmap_init(&kmi);
214
215         printf("avail memory = %ld (%ld MB)\n", ptoa(cnt.v_free_count),
216                         ptoa(cnt.v_free_count) / 1048576);
217
218         /* Set up buffers, so they can be used to read disk labels. */
219         bufinit();
220         vm_pager_bufferinit();
221 }
222
223 static char *
224 kenv_next(char *cp)
225 {
226
227         if (cp != NULL) {
228                 while (*cp != 0)
229                         cp++;
230                 cp++;
231                 if (*cp == 0)
232                         cp = NULL;
233         }
234         return (cp);
235 }
236
237 void
238 dump_kenv(void)
239 {
240         int len;
241         char *cp;
242
243         debugf("loader passed (static) kenv:\n");
244         if (kern_envp == NULL) {
245                 debugf(" no env, null ptr\n");
246                 return;
247         }
248         debugf(" kern_envp = 0x%08x\n", (u_int32_t)kern_envp);
249
250         len = 0;
251         for (cp = kern_envp; cp != NULL; cp = kenv_next(cp))
252                 debugf(" %x %s\n", (u_int32_t)cp, cp);
253 }
254
255 void
256 dump_bootinfo(void)
257 {
258         struct bi_mem_region *mr;
259         struct bi_eth_addr *eth;
260         int i, j;
261
262         debugf("bootinfo:\n");
263         if (bootinfo == NULL) {
264                 debugf(" no bootinfo, null ptr\n");
265                 return;
266         }
267
268         debugf(" version = 0x%08x\n", bootinfo->bi_version);
269         debugf(" ccsrbar = 0x%08x\n", bootinfo->bi_bar_base);
270         debugf(" cpu_clk = 0x%08x\n", bootinfo->bi_cpu_clk);
271         debugf(" bus_clk = 0x%08x\n", bootinfo->bi_bus_clk);
272
273         debugf(" mem regions:\n");
274         mr = (struct bi_mem_region *)bootinfo->bi_data;
275         for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++)
276                 debugf("    #%d, base = 0x%08x, size = 0x%08x\n", i,
277                     mr->mem_base, mr->mem_size);
278
279         debugf(" eth addresses:\n");
280         eth = (struct bi_eth_addr *)mr;
281         for (i = 0; i < bootinfo->bi_eth_addr_no; i++, eth++) {
282                 debugf("    #%d, addr = ", i);
283                 for (j = 0; j < 6; j++)
284                         debugf("%02x ", eth->mac_addr[j]);
285                 debugf("\n");
286         }
287 }
288
289 void
290 print_kernel_section_addr(void)
291 {
292
293         debugf("kernel image addresses:\n");
294         debugf(" kernel_text    = 0x%08x\n", (u_int32_t)kernel_text);
295         debugf(" _etext (sdata) = 0x%08x\n", (u_int32_t)_etext);
296         debugf(" _edata         = 0x%08x\n", (u_int32_t)_edata);
297         debugf(" __sbss_start   = 0x%08x\n", (u_int32_t)__sbss_start);
298         debugf(" __sbss_end     = 0x%08x\n", (u_int32_t)__sbss_end);
299         debugf(" __sbss_start   = 0x%08x\n", (u_int32_t)__bss_start);
300         debugf(" _end           = 0x%08x\n", (u_int32_t)_end);
301 }
302
303 struct bi_mem_region *
304 bootinfo_mr(void)
305 {
306
307         return((struct bi_mem_region *)bootinfo->bi_data);
308 }
309
310 struct bi_eth_addr *
311 bootinfo_eth(void)
312 {
313         struct bi_mem_region *mr;
314         struct bi_eth_addr *eth;
315         int i;
316
317         /* Advance to the eth section */
318         mr = bootinfo_mr();
319         for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++)
320                 ;
321
322         eth = (struct bi_eth_addr *)mr;
323         return (eth);
324 }
325
326 u_int
327 e500_init(u_int32_t startkernel, u_int32_t endkernel, void *mdp)
328 {
329         struct pcpu *pc;
330         void *kmdp;
331         vm_offset_t end;
332         struct bi_mem_region *mr;
333         uint32_t csr;
334         int i;
335
336         kmdp = NULL;
337
338         end = endkernel;
339
340         /*
341          * Parse metadata and fetch parameters. This must be done as the first
342          * step as we need bootinfo data to at least init the console
343          */
344         if (mdp != NULL) {
345                 preload_metadata = mdp;
346                 kmdp = preload_search_by_type("elf kernel");
347                 if (kmdp != NULL) {
348                         bootinfo = (struct bootinfo *)preload_search_info(kmdp,
349                             MODINFO_METADATA|MODINFOMD_BOOTINFO);
350
351                         boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
352                         kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
353                         end = MD_FETCH(kmdp, MODINFOMD_KERNEND, vm_offset_t);
354                 }
355         } else {
356                 /*
357                  * We should scream but how? - without CCSR bar (in bootinfo) 
358                  * cannot even output anything...
359                  */
360
361                  /*
362                   * FIXME add return value and handle in the locore so we can
363                   * return to the loader maybe? (this seems not very easy to
364                   * restore everything as the TLB have all been reprogrammed
365                   * in the locore etc...)
366                   */
367                 while(1);
368         }
369
370         /* Initialize memory regions table */
371         mr = bootinfo_mr();
372         for (i = 0; i < bootinfo->bi_mem_reg_no; i++, mr++) {
373                 if (i == MEM_REGIONS)
374                         break;
375                 availmem_regions[i].mr_start = mr->mem_base;
376                 availmem_regions[i].mr_size = mr->mem_size;
377         }
378         availmem_regions_sz = i;
379
380         /* Initialize TLB1 handling */
381         tlb1_init(bootinfo->bi_bar_base);
382
383         /*
384          * Time Base and Decrementer are updated every 8 CCB bus clocks.
385          * HID0[SEL_TBCLK] = 0
386          */
387         decr_config(bootinfo->bi_bus_clk/8);
388
389         /* Init params/tunables that can be overridden by the loader. */
390         init_param1();
391
392         /* Start initializing proc0 and thread0. */
393         proc_linkup(&proc0, &thread0);
394         thread0.td_frame = &frame0;
395
396         /* Set up per-cpu data and store the pointer in SPR general 0. */
397         pc = &__pcpu[0];
398         pcpu_init(pc, 0, sizeof(struct pcpu));
399         pc->pc_curthread = &thread0;
400         pc->pc_cpuid = 0;
401         __asm __volatile("mtsprg 0, %0" :: "r"(pc));
402
403         /* Initialize system mutexes. */
404         mutex_init();
405
406         /* Initialize the console before printing anything. */
407         cninit();
408
409         /* Print out some debug info... */
410         debugf("e500_init: console initialized\n");
411         debugf(" arg1 startkernel = 0x%08x\n", startkernel);
412         debugf(" arg2 endkernel = 0x%08x\n", endkernel);
413         debugf(" arg3 midp = 0x%08x\n", (u_int32_t)mdp);
414         debugf(" end = 0x%08x\n", (u_int32_t)end);
415         debugf(" boothowto = 0x%08x\n", boothowto);
416         debugf(" kernel ccsrbar = 0x%08x\n", CCSRBAR_VA);
417         debugf(" MSR = 0x%08x\n", mfmsr());
418         dump_bootinfo();
419         print_kernel_section_addr();
420         dump_kenv();
421         //tlb1_print_entries();
422         //tlb1_print_tlbentries();
423
424         kdb_init();
425
426 #ifdef KDB
427         if (boothowto & RB_KDB)
428                 kdb_enter(KDB_WHY_BOOTFLAGS, "Boot flags requested debugger");
429 #endif
430         kobj_machdep_init();
431
432         /* Initialise virtual memory. */
433         pmap_mmu_install(MMU_TYPE_BOOKE, 0);
434         pmap_bootstrap(startkernel, end);
435         debugf("MSR = 0x%08x\n", mfmsr());
436         //tlb1_print_entries();
437         //tlb1_print_tlbentries();
438
439         /* Initialize params/tunables that are derived from memsize. */
440         init_param2(physmem);
441
442         /* Finish setting up thread0. */
443         thread0.td_pcb = (struct pcb *)
444             ((thread0.td_kstack + thread0.td_kstack_pages * PAGE_SIZE -
445             sizeof(struct pcb)) & ~15);
446         bzero((void *)thread0.td_pcb, sizeof(struct pcb));
447         pc->pc_curpcb = thread0.td_pcb;
448
449         /* Initialise the message buffer. */
450         msgbufinit(msgbufp, MSGBUF_SIZE);
451
452         /* Enable Machine Check interrupt. */
453         mtmsr(mfmsr() | PSL_ME);
454         isync();
455
456         /* Enable D-cache if applicable */
457         csr = mfspr(SPR_L1CSR0);
458         if ((csr & L1CSR0_DCE) == 0) {
459                 dcache_inval();
460                 dcache_enable();
461         }
462         csr = mfspr(SPR_L1CSR0);
463         if ((boothowto & RB_VERBOSE) != 0 || (csr & L1CSR0_DCE) == 0)
464                 printf("L1 D-cache %sabled\n",
465                     (csr & L1CSR0_DCE) ? "en" : "dis");
466
467         /* Enable L1 I-cache if applicable. */
468         csr = mfspr(SPR_L1CSR1);
469         if ((csr & L1CSR1_ICE) == 0) {
470                 icache_inval();
471                 icache_enable();
472         }
473         csr = mfspr(SPR_L1CSR1);
474         if ((boothowto & RB_VERBOSE) != 0 || (csr & L1CSR1_ICE) == 0)
475                 printf("L1 I-cache %sabled\n",
476                     (csr & L1CSR1_ICE) ? "en" : "dis");
477
478         debugf("e500_init: SP = 0x%08x\n", ((uintptr_t)thread0.td_pcb - 16) & ~15);
479         debugf("e500_init: e\n");
480
481         return (((uintptr_t)thread0.td_pcb - 16) & ~15);
482 }
483
484 /* Initialise a struct pcpu. */
485 void
486 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
487 {
488
489 }
490
491 /* Set set up registers on exec. */
492 void
493 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
494 {
495         struct trapframe *tf;
496         struct ps_strings arginfo;
497
498         tf = trapframe(td);
499         bzero(tf, sizeof *tf);
500         tf->fixreg[1] = -roundup(-stack + 8, 16);
501
502         /*
503          * XXX Machine-independent code has already copied arguments and
504          * XXX environment to userland.  Get them back here.
505          */
506         (void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
507
508         /*
509          * Set up arguments for _start():
510          *      _start(argc, argv, envp, obj, cleanup, ps_strings);
511          *
512          * Notes:
513          *      - obj and cleanup are the auxilliary and termination
514          *        vectors.  They are fixed up by ld.elf_so.
515          *      - ps_strings is a NetBSD extention, and will be
516          *        ignored by executables which are strictly
517          *        compliant with the SVR4 ABI.
518          *
519          * XXX We have to set both regs and retval here due to different
520          * XXX calling convention in trap.c and init_main.c.
521          */
522         /*
523          * XXX PG: these get overwritten in the syscall return code.
524          * execve() should return EJUSTRETURN, like it does on NetBSD.
525          * Emulate by setting the syscall return value cells. The
526          * registers still have to be set for init's fork trampoline.
527          */
528         td->td_retval[0] = arginfo.ps_nargvstr;
529         td->td_retval[1] = (register_t)arginfo.ps_argvstr;
530         tf->fixreg[3] = arginfo.ps_nargvstr;
531         tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
532         tf->fixreg[5] = (register_t)arginfo.ps_envstr;
533         tf->fixreg[6] = 0;                      /* auxillary vector */
534         tf->fixreg[7] = 0;                      /* termination vector */
535         tf->fixreg[8] = (register_t)PS_STRINGS; /* NetBSD extension */
536
537         tf->srr0 = entry;
538         tf->srr1 = PSL_USERSET;
539         td->td_pcb->pcb_flags = 0;
540 }
541
542 int
543 fill_regs(struct thread *td, struct reg *regs)
544 {
545         struct trapframe *tf;
546
547         tf = td->td_frame;
548         memcpy(regs, tf, sizeof(struct reg));
549
550         return (0);
551 }
552
553 int
554 fill_fpregs(struct thread *td, struct fpreg *fpregs)
555 {
556
557         return (0);
558 }
559
560 /* Get current clock frequency for the given cpu id. */
561 int
562 cpu_est_clockrate(int cpu_id, uint64_t *rate)
563 {
564
565         return (ENXIO);
566 }
567
568 /*
569  * Construct a PCB from a trapframe. This is called from kdb_trap() where
570  * we want to start a backtrace from the function that caused us to enter
571  * the debugger. We have the context in the trapframe, but base the trace
572  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
573  * enough for a backtrace.
574  */
575 void
576 makectx(struct trapframe *tf, struct pcb *pcb)
577 {
578
579         pcb->pcb_lr = tf->srr0;
580         pcb->pcb_sp = tf->fixreg[1];
581 }
582
583 /*
584  * get_mcontext/sendsig helper routine that doesn't touch the
585  * proc lock.
586  */
587 static int
588 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
589 {
590         struct pcb *pcb;
591
592         pcb = td->td_pcb;
593         memset(mcp, 0, sizeof(mcontext_t));
594
595         mcp->mc_vers = _MC_VERSION;
596         mcp->mc_flags = 0;
597         memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
598         if (flags & GET_MC_CLEAR_RET) {
599                 mcp->mc_gpr[3] = 0;
600                 mcp->mc_gpr[4] = 0;
601         }
602
603         /* XXX Altivec context ? */
604
605         mcp->mc_len = sizeof(*mcp);
606         return (0);
607 }
608
609 int
610 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
611 {
612         int error;
613
614         error = grab_mcontext(td, mcp, flags);
615         if (error == 0) {
616                 PROC_LOCK(curthread->td_proc);
617                 mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
618                 PROC_UNLOCK(curthread->td_proc);
619         }
620
621         return (error);
622 }
623
624 int
625 set_mcontext(struct thread *td, const mcontext_t *mcp)
626 {
627         struct pcb *pcb;
628         struct trapframe *tf;
629
630         pcb = td->td_pcb;
631         tf = td->td_frame;
632
633         if (mcp->mc_vers != _MC_VERSION || mcp->mc_len != sizeof(*mcp))
634                 return (EINVAL);
635
636         memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
637
638         /* XXX Altivec context? */
639
640         return (0);
641 }
642
643 int
644 sigreturn(struct thread *td, struct sigreturn_args *uap)
645 {
646         struct proc *p;
647         ucontext_t uc;
648         int error;
649
650         CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
651
652         if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
653                 CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
654                 return (EFAULT);
655         }
656
657         error = set_mcontext(td, &uc.uc_mcontext);
658         if (error != 0)
659                 return (error);
660
661         p = td->td_proc;
662         PROC_LOCK(p);
663         td->td_sigmask = uc.uc_sigmask;
664         SIG_CANTMASK(td->td_sigmask);
665         signotify(td);
666         PROC_UNLOCK(p);
667
668         CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
669             td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
670
671         return (EJUSTRETURN);
672 }
673
674 #ifdef COMPAT_FREEBSD4
675 int
676 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
677 {
678
679         return sigreturn(td, (struct sigreturn_args *)uap);
680 }
681 #endif
682
683 /*
684  * cpu_idle
685  *
686  * Set Wait state enable.
687  */
688 void
689 cpu_idle (int busy)
690 {
691         register_t msr;
692
693         msr = mfmsr();
694 #ifdef INVARIANTS
695         if ((msr & PSL_EE) != PSL_EE) {
696                 struct thread *td = curthread;
697                 printf("td msr %x\n", td->td_md.md_saved_msr);
698                 panic("ints disabled in idleproc!");
699         }
700 #endif
701 #if 0
702         /*
703          * Freescale E500 core RM section 6.4.1
704          */
705         msr = msr | PSL_WE;
706
707         __asm__("       msync;"
708                 "       mtmsr   %0;"
709                 "       isync;"
710                 "loop:  b       loop" :
711                 /* no output */ :
712                 "r" (msr));
713 #endif
714 }
715
716 int
717 cpu_idle_wakeup(int cpu)
718 {
719
720         return (0);
721 }
722
723 void
724 spinlock_enter(void)
725 {
726         struct thread *td;
727
728         td = curthread;
729         if (td->td_md.md_spinlock_count == 0)
730                 td->td_md.md_saved_msr = intr_disable();
731         td->td_md.md_spinlock_count++;
732         critical_enter();
733 }
734
735 void
736 spinlock_exit(void)
737 {
738         struct thread *td;
739
740         td = curthread;
741         critical_exit();
742         td->td_md.md_spinlock_count--;
743         if (td->td_md.md_spinlock_count == 0)
744                 intr_restore(td->td_md.md_saved_msr);
745 }
746
747 /* Shutdown the CPU as much as possible. */
748 void
749 cpu_halt(void)
750 {
751
752         mtmsr(mfmsr() & ~(PSL_CE | PSL_EE | PSL_ME | PSL_DE));
753         while (1);
754 }
755
756 int
757 set_regs(struct thread *td, struct reg *regs)
758 {
759         struct trapframe *tf;
760
761         tf = td->td_frame;
762         memcpy(tf, regs, sizeof(struct reg));
763         return (0);
764 }
765
766 int
767 fill_dbregs(struct thread *td, struct dbreg *dbregs)
768 {
769
770         /* No debug registers on PowerPC */
771         return (ENOSYS);
772 }
773
774 int
775 set_dbregs(struct thread *td, struct dbreg *dbregs)
776 {
777
778         /* No debug registers on PowerPC */
779         return (ENOSYS);
780 }
781
782 int
783 set_fpregs(struct thread *td, struct fpreg *fpregs)
784 {
785
786         return (0);
787 }
788
789 int
790 ptrace_set_pc(struct thread *td, unsigned long addr)
791 {
792         struct trapframe *tf;
793
794         tf = td->td_frame;
795         tf->srr0 = (register_t)addr;
796
797         return (0);
798 }
799
800 int
801 ptrace_single_step(struct thread *td)
802 {
803         struct trapframe *tf;
804         u_int reg;
805
806         reg = mfspr(SPR_DBCR0);
807         reg |= DBCR0_IC | DBCR0_IDM;
808         mtspr(SPR_DBCR0, reg);
809
810         tf = td->td_frame;
811         tf->srr1 |= PSL_DE;
812         return (0);
813 }
814
815 int
816 ptrace_clear_single_step(struct thread *td)
817 {
818         struct trapframe *tf;
819
820         tf = td->td_frame;
821         tf->srr1 &= ~PSL_DE;
822         return (0);
823 }
824
825 void
826 kdb_cpu_clear_singlestep(void)
827 {
828         register_t r;
829
830         r = mfspr(SPR_DBCR0);
831         mtspr(SPR_DBCR0, r & ~DBCR0_IC);
832         kdb_frame->srr1 &= ~PSL_DE;
833 }
834
835 void
836 kdb_cpu_set_singlestep(void)
837 {
838         register_t r;
839
840         r = mfspr(SPR_DBCR0);
841         mtspr(SPR_DBCR0, r | DBCR0_IC | DBCR0_IDM);
842         kdb_frame->srr1 |= PSL_DE;
843 }
844
845 void
846 sendsig(sig_t catcher, ksiginfo_t *ksi, sigset_t *mask)
847 {
848         struct trapframe *tf;
849         struct sigframe *sfp;
850         struct sigacts *psp;
851         struct sigframe sf;
852         struct thread *td;
853         struct proc *p;
854         int oonstack, rndfsize;
855         int sig, code;
856
857         td = curthread;
858         p = td->td_proc;
859         PROC_LOCK_ASSERT(p, MA_OWNED);
860         sig = ksi->ksi_signo;
861         code = ksi->ksi_code;
862         psp = p->p_sigacts;
863         mtx_assert(&psp->ps_mtx, MA_OWNED);
864         tf = td->td_frame;
865         oonstack = sigonstack(tf->fixreg[1]);
866
867         rndfsize = ((sizeof(sf) + 15) / 16) * 16;
868
869         CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
870             catcher, sig);
871
872         /*
873          * Save user context
874          */
875         memset(&sf, 0, sizeof(sf));
876         grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
877         sf.sf_uc.uc_sigmask = *mask;
878         sf.sf_uc.uc_stack = td->td_sigstk;
879         sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
880                 ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
881
882         sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
883
884         /*
885          * Allocate and validate space for the signal handler context.
886          */
887         if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
888             SIGISMEMBER(psp->ps_sigonstack, sig)) {
889                 sfp = (struct sigframe *)((caddr_t)td->td_sigstk.ss_sp +
890                     td->td_sigstk.ss_size - rndfsize);
891         } else {
892                 sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
893         }
894
895         /*
896          * Translate the signal if appropriate (Linux emu ?)
897          */
898         if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
899                 sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
900
901         /*
902          * Save the floating-point state, if necessary, then copy it.
903          */
904         /* XXX */
905
906         /*
907          * Set up the registers to return to sigcode.
908          *
909          *   r1/sp - sigframe ptr
910          *   lr    - sig function, dispatched to by blrl in trampoline
911          *   r3    - sig number
912          *   r4    - SIGINFO ? &siginfo : exception code
913          *   r5    - user context
914          *   srr0  - trampoline function addr
915          */
916         tf->lr = (register_t)catcher;
917         tf->fixreg[1] = (register_t)sfp;
918         tf->fixreg[FIRSTARG] = sig;
919         tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
920         if (SIGISMEMBER(psp->ps_siginfo, sig)) {
921                 /*
922                  * Signal handler installed with SA_SIGINFO.
923                  */
924                 tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
925
926                 /*
927                  * Fill siginfo structure.
928                  */
929                 sf.sf_si = ksi->ksi_info;
930                 sf.sf_si.si_signo = sig;
931                 sf.sf_si.si_addr = (void *) ((tf->exc == EXC_DSI) ?
932                     tf->cpu.booke.dear : tf->srr0);
933         } else {
934                 /* Old FreeBSD-style arguments. */
935                 tf->fixreg[FIRSTARG+1] = code;
936                 tf->fixreg[FIRSTARG+3] = (tf->exc == EXC_DSI) ?
937                     tf->cpu.booke.dear : tf->srr0;
938         }
939         mtx_unlock(&psp->ps_mtx);
940         PROC_UNLOCK(p);
941
942         tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
943
944         /*
945          * copy the frame out to userland.
946          */
947         if (copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf)) != 0) {
948                 /*
949                  * Process has trashed its stack. Kill it.
950                  */
951                 CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
952                 PROC_LOCK(p);
953                 sigexit(td, SIGILL);
954         }
955
956         CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
957             tf->srr0, tf->fixreg[1]);
958
959         PROC_LOCK(p);
960         mtx_lock(&psp->ps_mtx);
961 }
962
963 void
964 bzero(void *buf, size_t len)
965 {
966         caddr_t p;
967
968         p = buf;
969
970         while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
971                 *p++ = 0;
972                 len--;
973         }
974
975         while (len >= sizeof(u_long) * 8) {
976                 *(u_long*) p = 0;
977                 *((u_long*) p + 1) = 0;
978                 *((u_long*) p + 2) = 0;
979                 *((u_long*) p + 3) = 0;
980                 len -= sizeof(u_long) * 8;
981                 *((u_long*) p + 4) = 0;
982                 *((u_long*) p + 5) = 0;
983                 *((u_long*) p + 6) = 0;
984                 *((u_long*) p + 7) = 0;
985                 p += sizeof(u_long) * 8;
986         }
987
988         while (len >= sizeof(u_long)) {
989                 *(u_long*) p = 0;
990                 len -= sizeof(u_long);
991                 p += sizeof(u_long);
992         }
993
994         while (len) {
995                 *p++ = 0;
996                 len--;
997         }
998 }
999
1000 /*
1001  * XXX what is the better/proper place for this routine?
1002  */
1003 int
1004 mem_valid(vm_offset_t addr, int len)
1005 {
1006
1007         return (1);
1008 }