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