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