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