trap.c revision 97398
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 * $NetBSD: trap.c,v 1.58 2002/03/04 04:07:35 dbj Exp $
32 */
33
34#ifndef lint
35static const char rcsid[] =
36  "$FreeBSD: head/sys/powerpc/aim/trap.c 97398 2002-05-28 12:28:22Z benno $";
37#endif /* not lint */
38
39#include "opt_ddb.h"
40#include "opt_ktrace.h"
41
42#include <sys/param.h>
43#include <sys/proc.h>
44#include <sys/ktr.h>
45#include <sys/lock.h>
46#include <sys/mutex.h>
47#include <sys/pioctl.h>
48#include <sys/reboot.h>
49#include <sys/syscall.h>
50#include <sys/sysent.h>
51#include <sys/systm.h>
52#include <sys/uio.h>
53#include <sys/user.h>
54#ifdef KTRACE
55#include <sys/ktrace.h>
56#endif
57#include <sys/vmmeter.h>
58
59#include <vm/vm.h>
60#include <vm/pmap.h>
61#include <vm/vm_extern.h>
62#include <vm/vm_param.h>
63#include <vm/vm_kern.h>
64#include <vm/vm_map.h>
65#include <vm/vm_page.h>
66
67#include <machine/cpu.h>
68#include <machine/db_machdep.h>
69#include <machine/fpu.h>
70#include <machine/frame.h>
71#include <machine/pcb.h>
72#include <machine/pmap.h>
73#include <machine/psl.h>
74#include <machine/trap.h>
75#include <machine/spr.h>
76#include <machine/sr.h>
77
78#ifndef MULTIPROCESSOR
79extern int intr_depth;
80#endif
81
82void		trap(struct trapframe *);
83
84static void	trap_fatal(struct trapframe *frame);
85static void	printtrap(u_int vector, struct trapframe *frame, int isfatal,
86		    int user);
87static int	trap_pfault(struct trapframe *frame, int user);
88static int	fix_unaligned(struct thread *td, struct trapframe *frame);
89static int	handle_onfault(struct trapframe *frame);
90static void	syscall(struct trapframe *frame);
91
92static __inline void	setusr(u_int);
93
94int	setfault(faultbuf);		/* defined in locore.S */
95
96/* Why are these not defined in a header? */
97int	badaddr(void *, size_t);
98int	badaddr_read(void *, size_t, int *);
99
100#ifdef	WITNESS
101extern char	*syscallnames[];
102#endif
103
104struct powerpc_exception {
105	u_int	vector;
106	char	*name;
107};
108
109static struct powerpc_exception powerpc_exceptions[] = {
110	{ 0x0100, "system reset" },
111	{ 0x0200, "machine check" },
112	{ 0x0300, "data storage interrupt" },
113	{ 0x0400, "instruction storage interrupt" },
114	{ 0x0500, "external interrupt" },
115	{ 0x0600, "alignment" },
116	{ 0x0700, "program" },
117	{ 0x0800, "floating-point unavailable" },
118	{ 0x0900, "decrementer" },
119	{ 0x0c00, "system call" },
120	{ 0x0d00, "trace" },
121	{ 0x0e00, "floating-point assist" },
122	{ 0x0f00, "performance monitoring" },
123	{ 0x0f20, "altivec unavailable" },
124	{ 0x1000, "instruction tlb miss" },
125	{ 0x1100, "data load tlb miss" },
126	{ 0x1200, "data store tlb miss" },
127	{ 0x1300, "instruction breakpoint" },
128	{ 0x1400, "system management" },
129	{ 0x1600, "altivec assist" },
130	{ 0x1700, "thermal management" },
131	{ 0x2000, "run mode/trace" },
132	{ 0x3000, NULL }
133};
134
135static const char *
136trapname(u_int vector)
137{
138	struct	powerpc_exception *pe;
139
140	for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) {
141		if (pe->vector == vector)
142			return (pe->name);
143	}
144
145	return ("unknown");
146}
147
148void
149trap(struct trapframe *frame)
150{
151	struct thread	*td, *fputhread;
152	struct proc	*p;
153	int		sig, type, user;
154	u_int		sticks, ucode;
155
156	atomic_add_int(&cnt.v_trap, 1);
157
158	td = PCPU_GET(curthread);
159	p = td->td_proc;
160
161	type = ucode = frame->exc;
162	sig = 0;
163	user = frame->srr1 & PSL_PR;
164	sticks = 0;
165
166	CTR3(KTR_TRAP, "trap: %s type=%s (%s)", p->p_comm,
167	    trapname(type), user ? "user" : "kernel");
168
169	if (user) {
170		sticks = td->td_kse->ke_sticks;
171		td->td_frame = frame;
172		if (td->td_ucred != p->p_ucred)
173			cred_update_thread(td);
174
175		/* User Mode Traps */
176		switch (type) {
177		case EXC_RUNMODETRC:
178		case EXC_TRC:
179			frame->srr1 &= ~PSL_SE;
180			sig = SIGTRAP;
181			break;
182
183		case EXC_DSI:
184		case EXC_ISI:
185			sig = trap_pfault(frame, 1);
186			break;
187
188		case EXC_SC:
189			syscall(frame);
190			break;
191
192		case EXC_FPU:
193			if ((fputhread = PCPU_GET(fputhread)) != NULL) {
194				save_fpu(fputhread);
195			}
196			PCPU_SET(fputhread, td);
197			td->td_pcb->pcb_fpcpu = PCPU_GET(cpuid);
198			enable_fpu(td);
199			frame->srr1 |= PSL_FP;
200			break;
201
202#ifdef	ALTIVEC
203		case EXC_VEC:
204			if ((vecthread = PCPU_GET(vecthread)) != NULL) {
205				KASSERT(vecthread != td,
206				    ("altivec already enabled"));
207				save_vec(vecthread);
208			}
209			PCPU_SET(vecthread, td);
210			td->td_pcb->pcb_veccpu = PCPU_GET(cpuid);
211			enable_vec(td);
212			frame->srr1 |= PSL_VEC;
213			break;
214#endif /* ALTIVEC */
215
216		case EXC_ALI:
217			if (fix_unaligned(td, frame) != 0)
218				sig = SIGBUS;
219			else
220				frame->srr0 += 4;
221			break;
222
223		case EXC_PGM:
224			/* XXX temporarily */
225			/* XXX: Magic Number? */
226			if (frame->srr1 & 0x0002000)
227				sig = SIGTRAP;
228 			else
229				sig = SIGILL;
230			break;
231
232		default:
233			trap_fatal(frame);
234		}
235	} else {
236		/* Kernel Mode Traps */
237
238		KASSERT(cold || td->td_ucred != NULL,
239		    ("kernel trap doesn't have ucred"));
240		switch (type) {
241		case EXC_DSI:
242			if (trap_pfault(frame, 0) == 0)
243 				return;
244			break;
245		case EXC_MCHK:
246			if (handle_onfault(frame))
247 				return;
248			break;
249		default:
250			trap_fatal(frame);
251		}
252	}
253
254	if (td != PCPU_GET(fputhread) ||
255	    td->td_pcb->pcb_fpcpu != PCPU_GET(cpuid))
256		frame->srr1 &= ~PSL_FP;
257
258#ifdef	ALTIVEC
259	if (td != PCPU_GET(vecthread) ||
260	    td->td_pcb->pcb_veccpu != PCPU_GET(cpuid))
261		frame->srr1 &= ~PSL_VEC;
262#endif /* ALTIVEC */
263
264	if (sig != 0) {
265		if (p->p_sysent->sv_transtrap != NULL)
266			sig = (p->p_sysent->sv_transtrap)(sig, type);
267		trapsignal(p, sig, ucode);
268	}
269
270	userret(td, frame, sticks);
271	mtx_assert(&Giant, MA_NOTOWNED);
272#ifdef	DIAGNOSTIC
273	cred_free_thread(td);
274#endif	/* DIAGNOSTIC */
275}
276
277static void
278trap_fatal(struct trapframe *frame)
279{
280
281	printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
282#ifdef DDB
283	if ((debugger_on_panic || db_active) && kdb_trap(frame->exc, 0, frame))
284		return;
285#endif
286	panic("%s trap", trapname(frame->exc));
287}
288
289static void
290printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
291{
292
293	printf("\n");
294	printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
295	    user ? "user" : "kernel");
296	printf("\n");
297	printf("   exception       = 0x%x (%s)\n", vector >> 8,
298	    trapname(vector));
299	switch (vector) {
300	case EXC_DSI:
301		printf("   virtual address = 0x%x\n", frame->dar);
302		break;
303	case EXC_ISI:
304		printf("   virtual address = 0x%x\n", frame->srr0);
305		break;
306	}
307	printf("   srr0            = 0x%x\n", frame->srr0);
308	printf("   srr1            = 0x%x\n", frame->srr1);
309	printf("   curthread       = %p\n", curthread);
310	if (curthread != NULL)
311		printf("          pid = %d, comm = %s\n",
312		    curthread->td_proc->p_pid, curthread->td_proc->p_comm);
313	printf("\n");
314}
315
316/*
317 * Handles a fatal fault when we have onfault state to recover.  Returns
318 * non-zero if there was onfault recovery state available.
319 */
320static int
321handle_onfault(struct trapframe *frame)
322{
323	struct		thread *td;
324	faultbuf	*fb;
325
326	td = curthread;
327	fb = td->td_pcb->pcb_onfault;
328	if (fb != NULL) {
329		frame->srr0 = (*fb)[0];
330		frame->fixreg[1] = (*fb)[1];
331		frame->fixreg[2] = (*fb)[2];
332		frame->cr = (*fb)[3];
333		bcopy(&(*fb)[4], &frame->fixreg[13],
334		    19 * sizeof(register_t));
335		return (1);
336	}
337	return (0);
338}
339
340void
341syscall(struct trapframe *frame)
342{
343	caddr_t		params;
344	struct		sysent *callp;
345	struct		thread *td;
346	struct		proc *p;
347	int		error, n;
348	size_t		narg;
349	register_t	args[10];
350	u_int		code;
351
352	td = PCPU_GET(curthread);
353	p = td->td_proc;
354
355	atomic_add_int(&cnt.v_syscall, 1);
356
357	code = frame->fixreg[0];
358	params = (caddr_t)(frame->fixreg + FIRSTARG);
359	n = NARGREG;
360
361	if (p->p_sysent->sv_prepsyscall) {
362		/*
363		 * The prep code is MP aware.
364		 */
365		(*p->p_sysent->sv_prepsyscall)(frame, args, &code, &params);
366	} else if (code == SYS_syscall) {
367		/*
368		 * code is first argument,
369		 * followed by actual args.
370		 */
371		code = *params++;
372		n -= 1;
373	} else if (code == SYS___syscall) {
374		/*
375		 * Like syscall, but code is a quad,
376		 * so as to maintain quad alignment
377		 * for the rest of the args.
378		 */
379		params++;
380		code = *params++;
381		n -= 2;
382	}
383
384 	if (p->p_sysent->sv_mask)
385 		code &= p->p_sysent->sv_mask;
386
387 	if (code >= p->p_sysent->sv_size)
388 		callp = &p->p_sysent->sv_table[0];
389  	else
390 		callp = &p->p_sysent->sv_table[code];
391
392	narg = callp->sy_narg & SYF_ARGMASK;
393
394	if (narg > n * sizeof(register_t)) {
395		bcopy(params, args, n * sizeof(register_t));
396		error = copyin(MOREARGS(frame->fixreg[1]), args + n,
397			narg - n * sizeof(register_t));
398		if (error) {
399#ifdef	KTRACE
400			/* Can't get all the arguments! */
401			if (KTRPOINT(p, KTR_SYSCALL))
402				ktrsyscall(p->p_tracep, code, narg, args);
403#endif
404			goto bad;
405		}
406		params = (caddr_t)args;
407	}
408
409	/*
410	 * Try to run the syscall without Giant if the syscall is MP safe.
411	 */
412	if ((callp->sy_narg & SYF_MPSAFE) == 0)
413		mtx_lock(&Giant);
414
415#ifdef	KTRACE
416	if (KTRPOINT(p, KTR_SYSCALL))
417		ktrsyscall(p->p_tracep, code, narg, params);
418#endif
419	td->td_retval[0] = 0;
420	td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
421
422	STOPEVENT(p, S_SCE, narg);
423
424	error = (*callp->sy_call)(td, params);
425	switch (error) {
426	case 0:
427		frame->fixreg[FIRSTARG] = td->td_retval[0];
428		frame->fixreg[FIRSTARG + 1] = td->td_retval[1];
429		/* XXX: Magic number */
430		frame->cr &= ~0x10000000;
431		break;
432	case ERESTART:
433		/*
434		 * Set user's pc back to redo the system call.
435		 */
436		frame->srr0 -= 4;
437		break;
438	case EJUSTRETURN:
439		/* nothing to do */
440		break;
441	default:
442bad:
443		if (p->p_sysent->sv_errsize) {
444			if (error >= p->p_sysent->sv_errsize)
445				error = -1;	/* XXX */
446			else
447				error = p->p_sysent->sv_errtbl[error];
448		}
449		frame->fixreg[FIRSTARG] = error;
450		/* XXX: Magic number: Carry Flag Equivalent? */
451		frame->cr |= 0x10000000;
452		break;
453	}
454
455
456#ifdef	KTRACE
457	if (KTRPOINT(p, KTR_SYSRET))
458		ktrsysret(p->p_tracep, code, error, td->td_retval[0]);
459#endif
460
461	if ((callp->sy_narg & SYF_MPSAFE) == 0)
462		mtx_unlock(&Giant);
463
464	/*
465	 * Does the comment in the i386 code about errno apply here?
466	 */
467	STOPEVENT(p, S_SCX, code);
468
469#ifdef WITNESS
470	if (witness_list(td)) {
471		panic("system call %s returning with mutex(s) held\n",
472		    syscallnames[code]);
473	}
474#endif
475	mtx_assert(&sched_lock, MA_NOTOWNED);
476	mtx_assert(&Giant, MA_NOTOWNED);
477}
478
479static int
480trap_pfault(struct trapframe *frame, int user)
481{
482	vm_offset_t	eva, va;
483	struct		thread *td;
484	struct		proc *p;
485	vm_map_t	map;
486	vm_prot_t	ftype;
487	int		rv;
488	u_int		user_sr;
489
490	td = curthread;
491	p = td->td_proc;
492	if (frame->exc == EXC_ISI) {
493		eva = frame->srr0;
494		ftype = VM_PROT_READ | VM_PROT_EXECUTE;
495	} else {
496		eva = frame->dar;
497		if (frame->dsisr & DSISR_STORE)
498			ftype = VM_PROT_READ | VM_PROT_WRITE;
499		else
500			ftype = VM_PROT_READ;
501	}
502
503	if (user) {
504		map = &p->p_vmspace->vm_map;
505	} else {
506		if ((eva >> ADDR_SR_SHFT) == USER_SR) {
507			if (p->p_vmspace == NULL)
508				return (SIGSEGV);
509
510			__asm ("mfsr %0, %1"
511			    : "=r"(user_sr)
512			    : "K"(USER_SR));
513			eva &= ADDR_PIDX | ADDR_POFF;
514			eva |= user_sr << ADDR_SR_SHFT;
515			map = &p->p_vmspace->vm_map;
516		} else {
517			map = kernel_map;
518		}
519	}
520	va = trunc_page(eva);
521
522	mtx_lock(&Giant);
523	if (map != kernel_map) {
524		/*
525		 * Keep swapout from messing with us during this
526		 *	critical time.
527		 */
528		PROC_LOCK(p);
529		++p->p_lock;
530		PROC_UNLOCK(p);
531
532		/* Fault in the user page: */
533		rv = vm_fault(map, va, ftype,
534		      (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
535					      : VM_FAULT_NORMAL);
536
537		PROC_LOCK(p);
538		--p->p_lock;
539		PROC_UNLOCK(p);
540	} else {
541		/*
542		 * Don't have to worry about process locking or stacks in the
543		 * kernel.
544		 */
545		rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
546	}
547	mtx_unlock(&Giant);
548
549	if (rv == KERN_SUCCESS)
550		return (0);
551
552	if (!user && handle_onfault(frame))
553		return (0);
554
555	return (SIGSEGV);
556}
557
558static __inline void
559setusr(u_int content)
560{
561	__asm __volatile ("isync; mtsr %0,%1; isync"
562		      :: "n"(USER_SR), "r"(content));
563}
564
565int
566badaddr(void *addr, size_t size)
567{
568	return (badaddr_read(addr, size, NULL));
569}
570
571int
572badaddr_read(void *addr, size_t size, int *rptr)
573{
574	struct thread	*td;
575	faultbuf	env;
576	int		x;
577
578	/* Get rid of any stale machine checks that have been waiting.  */
579	__asm __volatile ("sync; isync");
580
581	td = PCPU_GET(curthread);
582
583	if (setfault(env)) {
584		td->td_pcb->pcb_onfault = 0;
585		__asm __volatile ("sync");
586		return 1;
587	}
588
589	__asm __volatile ("sync");
590
591	switch (size) {
592	case 1:
593		x = *(volatile int8_t *)addr;
594		break;
595	case 2:
596		x = *(volatile int16_t *)addr;
597		break;
598	case 4:
599		x = *(volatile int32_t *)addr;
600		break;
601	default:
602		panic("badaddr: invalid size (%d)", size);
603	}
604
605	/* Make sure we took the machine check, if we caused one. */
606	__asm __volatile ("sync; isync");
607
608	td->td_pcb->pcb_onfault = 0;
609	__asm __volatile ("sync");	/* To be sure. */
610
611	/* Use the value to avoid reorder. */
612	if (rptr)
613		*rptr = x;
614
615	return (0);
616}
617
618/*
619 * For now, this only deals with the particular unaligned access case
620 * that gcc tends to generate.  Eventually it should handle all of the
621 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
622 */
623
624static int
625fix_unaligned(struct thread *td, struct trapframe *frame)
626{
627	struct thread	*fputhread;
628	int		indicator, reg;
629	double		*fpr;
630
631	indicator = EXC_ALI_OPCODE_INDICATOR(frame->dsisr);
632
633	switch (indicator) {
634	case EXC_ALI_LFD:
635	case EXC_ALI_STFD:
636		reg = EXC_ALI_RST(frame->dsisr);
637		fpr = &td->td_pcb->pcb_fpu.fpr[reg];
638		fputhread = PCPU_GET(fputhread);
639
640		/* Juggle the FPU to ensure that we've initialized
641		 * the FPRs, and that their current state is in
642		 * the PCB.
643		 */
644		if (fputhread != td) {
645			if (fputhread)
646				save_fpu(fputhread);
647			enable_fpu(td);
648		}
649		save_fpu(td);
650
651		if (indicator == EXC_ALI_LFD) {
652			if (copyin((void *)frame->dar, fpr,
653			    sizeof(double)) != 0)
654				return -1;
655			enable_fpu(td);
656		} else {
657			if (copyout(fpr, (void *)frame->dar,
658			    sizeof(double)) != 0)
659				return -1;
660		}
661		return 0;
662		break;
663	}
664
665	return -1;
666}
667