xref: /freebsd/sys/powerpc/powerpc/machdep.c (revision 6fd05b64b5b65dd4ba9b86482a0634a5f0b96c29)
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 	bcopy(&trapcode, (void *)EXC_RST,  (size_t)&trapsize);
313 	bcopy(&trapcode, (void *)EXC_MCHK, (size_t)&trapsize);
314 	bcopy(&dsitrap,  (void *)EXC_DSI,  (size_t)&dsisize);
315 	bcopy(&trapcode, (void *)EXC_ISI,  (size_t)&trapsize);
316 	bcopy(&trapcode, (void *)EXC_EXI,  (size_t)&trapsize);
317 	bcopy(&trapcode, (void *)EXC_ALI,  (size_t)&trapsize);
318 	bcopy(&trapcode, (void *)EXC_PGM,  (size_t)&trapsize);
319 	bcopy(&trapcode, (void *)EXC_FPU,  (size_t)&trapsize);
320 	bcopy(&trapcode, (void *)EXC_DECR, (size_t)&trapsize);
321 	bcopy(&trapcode, (void *)EXC_SC,   (size_t)&trapsize);
322 	bcopy(&trapcode, (void *)EXC_TRC,  (size_t)&trapsize);
323 	bcopy(&trapcode, (void *)EXC_FPA,  (size_t)&trapsize);
324 	bcopy(&trapcode, (void *)EXC_THRM, (size_t)&trapsize);
325 	bcopy(&trapcode, (void *)EXC_BPT,  (size_t)&trapsize);
326 #ifdef KDB
327 	bcopy(&dblow,	 (void *)EXC_RST,  (size_t)&dbsize);
328 	bcopy(&dblow,	 (void *)EXC_MCHK, (size_t)&dbsize);
329 	bcopy(&dblow,   (void *)EXC_PGM,  (size_t)&dbsize);
330 	bcopy(&dblow,   (void *)EXC_TRC,  (size_t)&dbsize);
331 	bcopy(&dblow,   (void *)EXC_BPT,  (size_t)&dbsize);
332 #endif
333 	__syncicache(EXC_RSVD, EXC_LAST - EXC_RSVD);
334 
335 	/*
336 	 * Make sure translation has been enabled
337 	 */
338 	mtmsr(mfmsr() | PSL_IR|PSL_DR|PSL_ME|PSL_RI);
339 
340 	/*
341 	 * Initialise virtual memory.
342 	 */
343 	pmap_bootstrap(startkernel, endkernel);
344 
345 	/*
346 	 * Initialize params/tunables that are derived from memsize
347 	 */
348 	init_param2(physmem);
349 
350 	/*
351 	 * Finish setting up thread0.
352 	 */
353 	thread0.td_kstack = kstack0;
354 	thread0.td_pcb = (struct pcb *)
355 	    (thread0.td_kstack + KSTACK_PAGES * PAGE_SIZE) - 1;
356 
357 	/*
358 	 * Map and initialise the message buffer.
359 	 */
360 	for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
361 		pmap_kenter((vm_offset_t)msgbufp + off, msgbuf_phys + off);
362 	msgbufinit(msgbufp, MSGBUF_SIZE);
363 
364 #ifdef KDB
365 	if (boothowto & RB_KDB)
366 		kdb_enter("Boot flags requested debugger");
367 #endif
368 }
369 
370 void
371 bzero(void *buf, size_t len)
372 {
373 	caddr_t	p;
374 
375 	p = buf;
376 
377 	while (((vm_offset_t) p & (sizeof(u_long) - 1)) && len) {
378 		*p++ = 0;
379 		len--;
380 	}
381 
382 	while (len >= sizeof(u_long) * 8) {
383 		*(u_long*) p = 0;
384 		*((u_long*) p + 1) = 0;
385 		*((u_long*) p + 2) = 0;
386 		*((u_long*) p + 3) = 0;
387 		len -= sizeof(u_long) * 8;
388 		*((u_long*) p + 4) = 0;
389 		*((u_long*) p + 5) = 0;
390 		*((u_long*) p + 6) = 0;
391 		*((u_long*) p + 7) = 0;
392 		p += sizeof(u_long) * 8;
393 	}
394 
395 	while (len >= sizeof(u_long)) {
396 		*(u_long*) p = 0;
397 		len -= sizeof(u_long);
398 		p += sizeof(u_long);
399 	}
400 
401 	while (len) {
402 		*p++ = 0;
403 		len--;
404 	}
405 }
406 
407 void
408 sendsig(sig_t catcher, int sig, sigset_t *mask, u_long code)
409 {
410 	struct trapframe *tf;
411 	struct sigframe *sfp;
412 	struct sigacts *psp;
413 	struct sigframe sf;
414 	struct thread *td;
415 	struct proc *p;
416 	int oonstack, rndfsize;
417 
418 	td = curthread;
419 	p = td->td_proc;
420 	PROC_LOCK_ASSERT(p, MA_OWNED);
421 	psp = p->p_sigacts;
422 	mtx_assert(&psp->ps_mtx, MA_OWNED);
423 	tf = td->td_frame;
424 	oonstack = sigonstack(tf->fixreg[1]);
425 
426 	rndfsize = ((sizeof(sf) + 15) / 16) * 16;
427 
428 	CTR4(KTR_SIG, "sendsig: td=%p (%s) catcher=%p sig=%d", td, p->p_comm,
429 	     catcher, sig);
430 
431 	/*
432 	 * Save user context
433 	 */
434 	memset(&sf, 0, sizeof(sf));
435 	grab_mcontext(td, &sf.sf_uc.uc_mcontext, 0);
436 	sf.sf_uc.uc_sigmask = *mask;
437 	sf.sf_uc.uc_stack = td->td_sigstk;
438 	sf.sf_uc.uc_stack.ss_flags = (td->td_pflags & TDP_ALTSTACK)
439 	    ? ((oonstack) ? SS_ONSTACK : 0) : SS_DISABLE;
440 
441 	sf.sf_uc.uc_mcontext.mc_onstack = (oonstack) ? 1 : 0;
442 
443 	/*
444 	 * Allocate and validate space for the signal handler context.
445 	 */
446 	if ((td->td_pflags & TDP_ALTSTACK) != 0 && !oonstack &&
447 	    SIGISMEMBER(psp->ps_sigonstack, sig)) {
448 		sfp = (struct sigframe *)((caddr_t)td->td_sigstk.ss_sp +
449 		   td->td_sigstk.ss_size - rndfsize);
450 	} else {
451 		sfp = (struct sigframe *)(tf->fixreg[1] - rndfsize);
452 	}
453 
454 	/*
455 	 * Translate the signal if appropriate (Linux emu ?)
456 	 */
457 	if (p->p_sysent->sv_sigtbl && sig <= p->p_sysent->sv_sigsize)
458 		sig = p->p_sysent->sv_sigtbl[_SIG_IDX(sig)];
459 
460 	/*
461 	 * Save the floating-point state, if necessary, then copy it.
462 	 */
463 	/* XXX */
464 
465 	/*
466 	 * Set up the registers to return to sigcode.
467 	 *
468 	 *   r1/sp - sigframe ptr
469 	 *   lr    - sig function, dispatched to by blrl in trampoline
470 	 *   r3    - sig number
471 	 *   r4    - SIGINFO ? &siginfo : exception code
472 	 *   r5    - user context
473 	 *   srr0  - trampoline function addr
474 	 */
475 	tf->lr = (register_t)catcher;
476 	tf->fixreg[1] = (register_t)sfp;
477 	tf->fixreg[FIRSTARG] = sig;
478 	tf->fixreg[FIRSTARG+2] = (register_t)&sfp->sf_uc;
479 	if (SIGISMEMBER(psp->ps_siginfo, sig)) {
480 		/*
481 		 * Signal handler installed with SA_SIGINFO.
482 		 */
483 		tf->fixreg[FIRSTARG+1] = (register_t)&sfp->sf_si;
484 
485 		/*
486 		 * Fill siginfo structure.
487 		 */
488 		sf.sf_si.si_signo = sig;
489 		sf.sf_si.si_code = code;
490 		sf.sf_si.si_addr = (void *)tf->srr0;
491 	} else {
492 		/* Old FreeBSD-style arguments. */
493 		tf->fixreg[FIRSTARG+1] = code;
494 	}
495 	mtx_unlock(&psp->ps_mtx);
496 	PROC_UNLOCK(p);
497 
498 	tf->srr0 = (register_t)(PS_STRINGS - *(p->p_sysent->sv_szsigcode));
499 
500 	/*
501 	 * copy the frame out to userland.
502 	 */
503 	if (copyout((caddr_t)&sf, (caddr_t)sfp, sizeof(sf)) != 0) {
504 		/*
505 		 * Process has trashed its stack. Kill it.
506 		 */
507 		CTR2(KTR_SIG, "sendsig: sigexit td=%p sfp=%p", td, sfp);
508 		PROC_LOCK(p);
509 		sigexit(td, SIGILL);
510 	}
511 
512 	CTR3(KTR_SIG, "sendsig: return td=%p pc=%#x sp=%#x", td,
513 	     tf->srr0, tf->fixreg[1]);
514 
515 	PROC_LOCK(p);
516 	mtx_lock(&psp->ps_mtx);
517 }
518 
519 /*
520  * Build siginfo_t for SA thread
521  */
522 void
523 cpu_thread_siginfo(int sig, u_long code, siginfo_t *si)
524 {
525 	struct proc *p;
526 	struct thread *td;
527 
528 	td = curthread;
529 	p = td->td_proc;
530 	PROC_LOCK_ASSERT(p, MA_OWNED);
531 
532 	bzero(si, sizeof(*si));
533 	si->si_signo = sig;
534 	si->si_code = code;
535 	/* XXXKSE fill other fields */
536 }
537 
538 int
539 sigreturn(struct thread *td, struct sigreturn_args *uap)
540 {
541 	struct proc *p;
542 	ucontext_t uc;
543 	int error;
544 
545 	CTR2(KTR_SIG, "sigreturn: td=%p ucp=%p", td, uap->sigcntxp);
546 
547 	if (copyin(uap->sigcntxp, &uc, sizeof(uc)) != 0) {
548 		CTR1(KTR_SIG, "sigreturn: efault td=%p", td);
549 		return (EFAULT);
550 	}
551 
552 	error = set_mcontext(td, &uc.uc_mcontext);
553 	if (error != 0)
554 		return (error);
555 
556 	p = td->td_proc;
557 	PROC_LOCK(p);
558 	td->td_sigmask = uc.uc_sigmask;
559 	SIG_CANTMASK(td->td_sigmask);
560 	signotify(td);
561 	PROC_UNLOCK(p);
562 
563 	CTR3(KTR_SIG, "sigreturn: return td=%p pc=%#x sp=%#x",
564 	     td, uc.uc_mcontext.mc_srr0, uc.uc_mcontext.mc_gpr[1]);
565 
566 	return (EJUSTRETURN);
567 }
568 
569 #ifdef COMPAT_FREEBSD4
570 int
571 freebsd4_sigreturn(struct thread *td, struct freebsd4_sigreturn_args *uap)
572 {
573 
574 	return sigreturn(td, (struct sigreturn_args *)uap);
575 }
576 #endif
577 
578 /*
579  * Construct a PCB from a trapframe. This is called from kdb_trap() where
580  * we want to start a backtrace from the function that caused us to enter
581  * the debugger. We have the context in the trapframe, but base the trace
582  * on the PCB. The PCB doesn't have to be perfect, as long as it contains
583  * enough for a backtrace.
584  */
585 void
586 makectx(struct trapframe *tf, struct pcb *pcb)
587 {
588 
589 	pcb->pcb_lr = tf->srr0;
590 	pcb->pcb_sp = tf->fixreg[1];
591 }
592 
593 /*
594  * get_mcontext/sendsig helper routine that doesn't touch the
595  * proc lock
596  */
597 static int
598 grab_mcontext(struct thread *td, mcontext_t *mcp, int flags)
599 {
600 	struct pcb *pcb;
601 
602 	pcb = td->td_pcb;
603 
604 	memset(mcp, 0, sizeof(mcontext_t));
605 
606 	mcp->mc_vers = _MC_VERSION;
607 	mcp->mc_flags = 0;
608 	memcpy(&mcp->mc_frame, td->td_frame, sizeof(struct trapframe));
609 	if (flags & GET_MC_CLEAR_RET) {
610 		mcp->mc_gpr[3] = 0;
611 		mcp->mc_gpr[4] = 0;
612 	}
613 
614 	/*
615 	 * This assumes that floating-point context is *not* lazy,
616 	 * so if the thread has used FP there would have been a
617 	 * FP-unavailable exception that would have set things up
618 	 * correctly.
619 	 */
620 	if (pcb->pcb_flags & PCB_FPU) {
621 		KASSERT(td == curthread,
622 			("get_mcontext: fp save not curthread"));
623 		critical_enter();
624 		save_fpu(td);
625 		critical_exit();
626 		mcp->mc_flags |= _MC_FP_VALID;
627 		memcpy(&mcp->mc_fpscr, &pcb->pcb_fpu.fpscr, sizeof(double));
628 		memcpy(mcp->mc_fpreg, pcb->pcb_fpu.fpr, 32*sizeof(double));
629 	}
630 
631 	/* XXX Altivec context ? */
632 
633 	mcp->mc_len = sizeof(*mcp);
634 
635 	return (0);
636 }
637 
638 int
639 get_mcontext(struct thread *td, mcontext_t *mcp, int flags)
640 {
641 	int error;
642 
643 	error = grab_mcontext(td, mcp, flags);
644 	if (error == 0) {
645 		PROC_LOCK(curthread->td_proc);
646 		mcp->mc_onstack = sigonstack(td->td_frame->fixreg[1]);
647 		PROC_UNLOCK(curthread->td_proc);
648 	}
649 
650 	return (error);
651 }
652 
653 int
654 set_mcontext(struct thread *td, const mcontext_t *mcp)
655 {
656 	struct pcb *pcb;
657 	struct trapframe *tf;
658 
659 	pcb = td->td_pcb;
660 	tf = td->td_frame;
661 
662 	if (mcp->mc_vers != _MC_VERSION ||
663 	    mcp->mc_len != sizeof(*mcp))
664 		return (EINVAL);
665 
666 	/*
667 	 * Don't let the user set privileged MSR bits
668 	 */
669 	if ((mcp->mc_srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC)) {
670 		return (EINVAL);
671 	}
672 
673 	memcpy(tf, mcp->mc_frame, sizeof(mcp->mc_frame));
674 
675 	if (mcp->mc_flags & _MC_FP_VALID) {
676 		if ((pcb->pcb_flags & PCB_FPU) != PCB_FPU) {
677 			critical_enter();
678 			enable_fpu(td);
679 			critical_exit();
680 		}
681 		memcpy(&pcb->pcb_fpu.fpscr, &mcp->mc_fpscr, sizeof(double));
682 		memcpy(pcb->pcb_fpu.fpr, mcp->mc_fpreg, 32*sizeof(double));
683 	}
684 
685 	/* XXX Altivec context? */
686 
687 	return (0);
688 }
689 
690 void
691 cpu_boot(int howto)
692 {
693 }
694 
695 /*
696  * Shutdown the CPU as much as possible.
697  */
698 void
699 cpu_halt(void)
700 {
701 
702 	OF_exit();
703 }
704 
705 void
706 cpu_idle(void)
707 {
708 	/* Insert code to halt (until next interrupt) for the idle loop */
709 }
710 
711 /*
712  * Set set up registers on exec.
713  */
714 void
715 exec_setregs(struct thread *td, u_long entry, u_long stack, u_long ps_strings)
716 {
717 	struct trapframe	*tf;
718 	struct ps_strings	arginfo;
719 
720 	tf = trapframe(td);
721 	bzero(tf, sizeof *tf);
722 	tf->fixreg[1] = -roundup(-stack + 8, 16);
723 
724 	/*
725 	 * XXX Machine-independent code has already copied arguments and
726 	 * XXX environment to userland.  Get them back here.
727 	 */
728 	(void)copyin((char *)PS_STRINGS, &arginfo, sizeof(arginfo));
729 
730 	/*
731 	 * Set up arguments for _start():
732 	 *	_start(argc, argv, envp, obj, cleanup, ps_strings);
733 	 *
734 	 * Notes:
735 	 *	- obj and cleanup are the auxilliary and termination
736 	 *	  vectors.  They are fixed up by ld.elf_so.
737 	 *	- ps_strings is a NetBSD extention, and will be
738 	 * 	  ignored by executables which are strictly
739 	 *	  compliant with the SVR4 ABI.
740 	 *
741 	 * XXX We have to set both regs and retval here due to different
742 	 * XXX calling convention in trap.c and init_main.c.
743 	 */
744         /*
745          * XXX PG: these get overwritten in the syscall return code.
746          * execve() should return EJUSTRETURN, like it does on NetBSD.
747          * Emulate by setting the syscall return value cells. The
748          * registers still have to be set for init's fork trampoline.
749          */
750         td->td_retval[0] = arginfo.ps_nargvstr;
751         td->td_retval[1] = (register_t)arginfo.ps_argvstr;
752 	tf->fixreg[3] = arginfo.ps_nargvstr;
753 	tf->fixreg[4] = (register_t)arginfo.ps_argvstr;
754 	tf->fixreg[5] = (register_t)arginfo.ps_envstr;
755 	tf->fixreg[6] = 0;			/* auxillary vector */
756 	tf->fixreg[7] = 0;			/* termination vector */
757 	tf->fixreg[8] = (register_t)PS_STRINGS;	/* NetBSD extension */
758 
759 	tf->srr0 = entry;
760 	tf->srr1 = PSL_MBO | PSL_USERSET | PSL_FE_DFLT;
761 	td->td_pcb->pcb_flags = 0;
762 }
763 
764 /* XXX: dummy {fill,set}_[fp]regs */
765 int
766 fill_regs(struct thread *td, struct reg *regs)
767 {
768 
769 	return (ENOSYS);
770 }
771 
772 int
773 fill_dbregs(struct thread *td, struct dbreg *dbregs)
774 {
775 
776 	return (ENOSYS);
777 }
778 
779 int
780 fill_fpregs(struct thread *td, struct fpreg *fpregs)
781 {
782 
783 	return (ENOSYS);
784 }
785 
786 int
787 set_regs(struct thread *td, struct reg *regs)
788 {
789 
790 	return (ENOSYS);
791 }
792 
793 int
794 set_dbregs(struct thread *td, struct dbreg *dbregs)
795 {
796 
797 	return (ENOSYS);
798 }
799 
800 int
801 set_fpregs(struct thread *td, struct fpreg *fpregs)
802 {
803 
804 	return (ENOSYS);
805 }
806 
807 int
808 ptrace_set_pc(struct thread *td, unsigned long addr)
809 {
810 
811 	/* XXX: coming soon... */
812 	return (ENOSYS);
813 }
814 
815 int
816 ptrace_single_step(struct thread *td)
817 {
818 
819 	/* XXX: coming soon... */
820 	return (ENOSYS);
821 }
822 
823 int
824 ptrace_clear_single_step(struct thread *td)
825 {
826 
827 	/* XXX: coming soon... */
828 	return (ENOSYS);
829 }
830 
831 /*
832  * Initialise a struct pcpu.
833  */
834 void
835 cpu_pcpu_init(struct pcpu *pcpu, int cpuid, size_t sz)
836 {
837 
838 }
839 
840 /*
841  * kcopy(const void *src, void *dst, size_t len);
842  *
843  * Copy len bytes from src to dst, aborting if we encounter a fatal
844  * page fault.
845  *
846  * kcopy() _must_ save and restore the old fault handler since it is
847  * called by uiomove(), which may be in the path of servicing a non-fatal
848  * page fault.
849  */
850 int
851 kcopy(const void *src, void *dst, size_t len)
852 {
853 	struct thread	*td;
854 	faultbuf	env, *oldfault;
855 	int		rv;
856 
857 	td = PCPU_GET(curthread);
858 	oldfault = td->td_pcb->pcb_onfault;
859 	if ((rv = setfault(env)) != 0) {
860 		td->td_pcb->pcb_onfault = oldfault;
861 		return rv;
862 	}
863 
864 	memcpy(dst, src, len);
865 
866 	td->td_pcb->pcb_onfault = oldfault;
867 	return (0);
868 }
869 
870 void
871 asm_panic(char *pstr)
872 {
873 	panic(pstr);
874 }
875 
876 int db_trap_glue(struct trapframe *);		/* Called from trap_subr.S */
877 
878 int
879 db_trap_glue(struct trapframe *frame)
880 {
881 	if (!(frame->srr1 & PSL_PR)
882 	    && (frame->exc == EXC_TRC || frame->exc == EXC_RUNMODETRC
883 		|| (frame->exc == EXC_PGM
884 		    && (frame->srr1 & 0x20000))
885 		|| frame->exc == EXC_BPT)) {
886 		int type = frame->exc;
887 		if (type == EXC_PGM && (frame->srr1 & 0x20000)) {
888 			type = T_BREAKPOINT;
889 		}
890 		return (kdb_trap(type, 0, frame));
891 	}
892 
893 	return (0);
894 }
895