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