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