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