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