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