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