trap.c revision 269752
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: stable/10/sys/powerpc/aim/trap.c 269752 2014-08-09 14:05:01Z markj $");
36
37#include "opt_kdtrace.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/ptrace.h>
47#include <sys/reboot.h>
48#include <sys/syscall.h>
49#include <sys/sysent.h>
50#include <sys/systm.h>
51#include <sys/uio.h>
52#include <sys/signalvar.h>
53#include <sys/vmmeter.h>
54
55#include <security/audit/audit.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/_inttypes.h>
66#include <machine/altivec.h>
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
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
86#ifdef __powerpc64__
87       void	handle_kernel_slb_spill(int, register_t, register_t);
88static int	handle_user_slb_spill(pmap_t pm, vm_offset_t addr);
89extern int	n_slbs;
90#endif
91
92struct powerpc_exception {
93	u_int	vector;
94	char	*name;
95};
96
97#ifdef KDTRACE_HOOKS
98#include <sys/dtrace_bsd.h>
99
100int (*dtrace_invop_jump_addr)(struct trapframe *);
101#endif
102
103static struct powerpc_exception powerpc_exceptions[] = {
104	{ 0x0100, "system reset" },
105	{ 0x0200, "machine check" },
106	{ 0x0300, "data storage interrupt" },
107	{ 0x0380, "data segment exception" },
108	{ 0x0400, "instruction storage interrupt" },
109	{ 0x0480, "instruction segment exception" },
110	{ 0x0500, "external interrupt" },
111	{ 0x0600, "alignment" },
112	{ 0x0700, "program" },
113	{ 0x0800, "floating-point unavailable" },
114	{ 0x0900, "decrementer" },
115	{ 0x0c00, "system call" },
116	{ 0x0d00, "trace" },
117	{ 0x0e00, "floating-point assist" },
118	{ 0x0f00, "performance monitoring" },
119	{ 0x0f20, "altivec unavailable" },
120	{ 0x1000, "instruction tlb miss" },
121	{ 0x1100, "data load tlb miss" },
122	{ 0x1200, "data store tlb miss" },
123	{ 0x1300, "instruction breakpoint" },
124	{ 0x1400, "system management" },
125	{ 0x1600, "altivec assist" },
126	{ 0x1700, "thermal management" },
127	{ 0x2000, "run mode/trace" },
128	{ 0x3000, NULL }
129};
130
131static const char *
132trapname(u_int vector)
133{
134	struct	powerpc_exception *pe;
135
136	for (pe = powerpc_exceptions; pe->vector != 0x3000; pe++) {
137		if (pe->vector == vector)
138			return (pe->name);
139	}
140
141	return ("unknown");
142}
143
144void
145trap(struct trapframe *frame)
146{
147	struct thread	*td;
148	struct proc	*p;
149#ifdef KDTRACE_HOOKS
150	uint32_t inst;
151#endif
152	int		sig, type, user;
153	u_int		ucode;
154	ksiginfo_t	ksi;
155
156	PCPU_INC(cnt.v_trap);
157
158	td = curthread;
159	p = td->td_proc;
160
161	type = ucode = frame->exc;
162	sig = 0;
163	user = frame->srr1 & PSL_PR;
164
165	CTR3(KTR_TRAP, "trap: %s type=%s (%s)", td->td_name,
166	    trapname(type), user ? "user" : "kernel");
167
168#ifdef KDTRACE_HOOKS
169	/*
170	 * A trap can occur while DTrace executes a probe. Before
171	 * executing the probe, DTrace blocks re-scheduling and sets
172	 * a flag in it's per-cpu flags to indicate that it doesn't
173	 * want to fault. On returning from the probe, the no-fault
174	 * flag is cleared and finally re-scheduling is enabled.
175	 *
176	 * If the DTrace kernel module has registered a trap handler,
177	 * call it and if it returns non-zero, assume that it has
178	 * handled the trap and modified the trap frame so that this
179	 * function can return normally.
180	 */
181	/*
182	 * XXXDTRACE: add pid probe handler here (if ever)
183	 */
184	if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, type))
185		return;
186#endif
187
188	if (user) {
189		td->td_pticks = 0;
190		td->td_frame = frame;
191		if (td->td_ucred != p->p_ucred)
192			cred_update_thread(td);
193
194		/* User Mode Traps */
195		switch (type) {
196		case EXC_RUNMODETRC:
197		case EXC_TRC:
198			frame->srr1 &= ~PSL_SE;
199			sig = SIGTRAP;
200			break;
201
202#ifdef __powerpc64__
203		case EXC_ISE:
204		case EXC_DSE:
205			if (handle_user_slb_spill(&p->p_vmspace->vm_pmap,
206			    (type == EXC_ISE) ? frame->srr0 :
207			    frame->cpu.aim.dar) != 0)
208				sig = SIGSEGV;
209			break;
210#endif
211		case EXC_DSI:
212		case EXC_ISI:
213			sig = trap_pfault(frame, 1);
214			break;
215
216		case EXC_SC:
217			syscall(frame);
218			break;
219
220		case EXC_FPU:
221			KASSERT((td->td_pcb->pcb_flags & PCB_FPU) != PCB_FPU,
222			    ("FPU already enabled for thread"));
223			enable_fpu(td);
224			break;
225
226		case EXC_VEC:
227			KASSERT((td->td_pcb->pcb_flags & PCB_VEC) != PCB_VEC,
228			    ("Altivec already enabled for thread"));
229			enable_vec(td);
230			break;
231
232		case EXC_VECAST_G4:
233		case EXC_VECAST_G5:
234			/*
235			 * We get a VPU assist exception for IEEE mode
236			 * vector operations on denormalized floats.
237			 * Emulating this is a giant pain, so for now,
238			 * just switch off IEEE mode and treat them as
239			 * zero.
240			 */
241
242			save_vec(td);
243			td->td_pcb->pcb_vec.vscr |= ALTIVEC_VSCR_NJ;
244			enable_vec(td);
245			break;
246
247		case EXC_ALI:
248			if (fix_unaligned(td, frame) != 0)
249				sig = SIGBUS;
250			else
251				frame->srr0 += 4;
252			break;
253
254		case EXC_PGM:
255			/* Identify the trap reason */
256			if (frame->srr1 & EXC_PGM_TRAP) {
257#ifdef KDTRACE_HOOKS
258				inst = fuword32((const void *)frame->srr0);
259				if (inst == 0x0FFFDDDD && dtrace_pid_probe_ptr != NULL) {
260					struct reg regs;
261					fill_regs(td, &regs);
262					(*dtrace_pid_probe_ptr)(&regs);
263					break;
264				}
265#endif
266 				sig = SIGTRAP;
267			} else {
268				sig = ppc_instr_emulate(frame, td->td_pcb);
269			}
270			break;
271
272		default:
273			trap_fatal(frame);
274		}
275	} else {
276		/* Kernel Mode Traps */
277
278		KASSERT(cold || td->td_ucred != NULL,
279		    ("kernel trap doesn't have ucred"));
280		switch (type) {
281#ifdef KDTRACE_HOOKS
282		case EXC_PGM:
283			if (frame->srr1 & EXC_PGM_TRAP) {
284				if (*(uint32_t *)frame->srr0 == 0x7c810808) {
285					if (dtrace_invop_jump_addr != NULL) {
286						dtrace_invop_jump_addr(frame);
287						return;
288					}
289				}
290			}
291			break;
292#endif
293#ifdef __powerpc64__
294		case EXC_DSE:
295			if ((frame->cpu.aim.dar & SEGMENT_MASK) == USER_ADDR) {
296				__asm __volatile ("slbmte %0, %1" ::
297					"r"(td->td_pcb->pcb_cpu.aim.usr_vsid),
298					"r"(USER_SLB_SLBE));
299				return;
300			}
301			break;
302#endif
303		case EXC_DSI:
304			if (trap_pfault(frame, 0) == 0)
305 				return;
306			break;
307		case EXC_MCHK:
308			if (handle_onfault(frame))
309 				return;
310			break;
311		default:
312			break;
313		}
314		trap_fatal(frame);
315	}
316
317	if (sig != 0) {
318		if (p->p_sysent->sv_transtrap != NULL)
319			sig = (p->p_sysent->sv_transtrap)(sig, type);
320		ksiginfo_init_trap(&ksi);
321		ksi.ksi_signo = sig;
322		ksi.ksi_code = (int) ucode; /* XXX, not POSIX */
323		/* ksi.ksi_addr = ? */
324		ksi.ksi_trapno = type;
325		trapsignal(td, &ksi);
326	}
327
328	userret(td, frame);
329}
330
331static void
332trap_fatal(struct trapframe *frame)
333{
334
335	printtrap(frame->exc, frame, 1, (frame->srr1 & PSL_PR));
336#ifdef KDB
337	if ((debugger_on_panic || kdb_active) &&
338	    kdb_trap(frame->exc, 0, frame))
339		return;
340#endif
341	panic("%s trap", trapname(frame->exc));
342}
343
344static void
345printtrap(u_int vector, struct trapframe *frame, int isfatal, int user)
346{
347
348	printf("\n");
349	printf("%s %s trap:\n", isfatal ? "fatal" : "handled",
350	    user ? "user" : "kernel");
351	printf("\n");
352	printf("   exception       = 0x%x (%s)\n", vector, trapname(vector));
353	switch (vector) {
354	case EXC_DSE:
355	case EXC_DSI:
356		printf("   virtual address = 0x%" PRIxPTR "\n",
357		    frame->cpu.aim.dar);
358		printf("   dsisr           = 0x%" PRIxPTR "\n",
359		    frame->cpu.aim.dsisr);
360		break;
361	case EXC_ISE:
362	case EXC_ISI:
363		printf("   virtual address = 0x%" PRIxPTR "\n", frame->srr0);
364		break;
365	}
366	printf("   srr0            = 0x%" PRIxPTR "\n", frame->srr0);
367	printf("   srr1            = 0x%" PRIxPTR "\n", frame->srr1);
368	printf("   lr              = 0x%" PRIxPTR "\n", frame->lr);
369	printf("   curthread       = %p\n", curthread);
370	if (curthread != NULL)
371		printf("          pid = %d, comm = %s\n",
372		    curthread->td_proc->p_pid, curthread->td_name);
373	printf("\n");
374}
375
376/*
377 * Handles a fatal fault when we have onfault state to recover.  Returns
378 * non-zero if there was onfault recovery state available.
379 */
380static int
381handle_onfault(struct trapframe *frame)
382{
383	struct		thread *td;
384	faultbuf	*fb;
385
386	td = curthread;
387	fb = td->td_pcb->pcb_onfault;
388	if (fb != NULL) {
389		frame->srr0 = (*fb)[0];
390		frame->fixreg[1] = (*fb)[1];
391		frame->fixreg[2] = (*fb)[2];
392		frame->fixreg[3] = 1;
393		frame->cr = (*fb)[3];
394		bcopy(&(*fb)[4], &frame->fixreg[13],
395		    19 * sizeof(register_t));
396		return (1);
397	}
398	return (0);
399}
400
401int
402cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
403{
404	struct proc *p;
405	struct trapframe *frame;
406	caddr_t	params;
407	size_t argsz;
408	int error, n, i;
409
410	p = td->td_proc;
411	frame = td->td_frame;
412
413	sa->code = frame->fixreg[0];
414	params = (caddr_t)(frame->fixreg + FIRSTARG);
415	n = NARGREG;
416
417	if (sa->code == SYS_syscall) {
418		/*
419		 * code is first argument,
420		 * followed by actual args.
421		 */
422		sa->code = *(register_t *) params;
423		params += sizeof(register_t);
424		n -= 1;
425	} else if (sa->code == SYS___syscall) {
426		/*
427		 * Like syscall, but code is a quad,
428		 * so as to maintain quad alignment
429		 * for the rest of the args.
430		 */
431		if (SV_PROC_FLAG(p, SV_ILP32)) {
432			params += sizeof(register_t);
433			sa->code = *(register_t *) params;
434			params += sizeof(register_t);
435			n -= 2;
436		} else {
437			sa->code = *(register_t *) params;
438			params += sizeof(register_t);
439			n -= 1;
440		}
441	}
442
443 	if (p->p_sysent->sv_mask)
444		sa->code &= p->p_sysent->sv_mask;
445	if (sa->code >= p->p_sysent->sv_size)
446		sa->callp = &p->p_sysent->sv_table[0];
447	else
448		sa->callp = &p->p_sysent->sv_table[sa->code];
449
450	sa->narg = sa->callp->sy_narg;
451
452	if (SV_PROC_FLAG(p, SV_ILP32)) {
453		argsz = sizeof(uint32_t);
454
455		for (i = 0; i < n; i++)
456			sa->args[i] = ((u_register_t *)(params))[i] &
457			    0xffffffff;
458	} else {
459		argsz = sizeof(uint64_t);
460
461		for (i = 0; i < n; i++)
462			sa->args[i] = ((u_register_t *)(params))[i];
463	}
464
465	if (sa->narg > n)
466		error = copyin(MOREARGS(frame->fixreg[1]), sa->args + n,
467			       (sa->narg - n) * argsz);
468	else
469		error = 0;
470
471#ifdef __powerpc64__
472	if (SV_PROC_FLAG(p, SV_ILP32) && sa->narg > n) {
473		/* Expand the size of arguments copied from the stack */
474
475		for (i = sa->narg; i >= n; i--)
476			sa->args[i] = ((uint32_t *)(&sa->args[n]))[i-n];
477	}
478#endif
479
480	if (error == 0) {
481		td->td_retval[0] = 0;
482		td->td_retval[1] = frame->fixreg[FIRSTARG + 1];
483	}
484	return (error);
485}
486
487#include "../../kern/subr_syscall.c"
488
489void
490syscall(struct trapframe *frame)
491{
492	struct thread *td;
493	struct syscall_args sa;
494	int error;
495
496	td = curthread;
497	td->td_frame = frame;
498
499#ifdef __powerpc64__
500	/*
501	 * Speculatively restore last user SLB segment, which we know is
502	 * invalid already, since we are likely to do copyin()/copyout().
503	 */
504	__asm __volatile ("slbmte %0, %1; isync" ::
505            "r"(td->td_pcb->pcb_cpu.aim.usr_vsid), "r"(USER_SLB_SLBE));
506#endif
507
508	error = syscallenter(td, &sa);
509	syscallret(td, error, &sa);
510}
511
512#ifdef __powerpc64__
513/* Handle kernel SLB faults -- runs in real mode, all seat belts off */
514void
515handle_kernel_slb_spill(int type, register_t dar, register_t srr0)
516{
517	struct slb *slbcache;
518	uint64_t slbe, slbv;
519	uint64_t esid, addr;
520	int i;
521
522	addr = (type == EXC_ISE) ? srr0 : dar;
523	slbcache = PCPU_GET(slb);
524	esid = (uintptr_t)addr >> ADDR_SR_SHFT;
525	slbe = (esid << SLBE_ESID_SHIFT) | SLBE_VALID;
526
527	/* See if the hardware flushed this somehow (can happen in LPARs) */
528	for (i = 0; i < n_slbs; i++)
529		if (slbcache[i].slbe == (slbe | (uint64_t)i))
530			return;
531
532	/* Not in the map, needs to actually be added */
533	slbv = kernel_va_to_slbv(addr);
534	if (slbcache[USER_SLB_SLOT].slbe == 0) {
535		for (i = 0; i < n_slbs; i++) {
536			if (i == USER_SLB_SLOT)
537				continue;
538			if (!(slbcache[i].slbe & SLBE_VALID))
539				goto fillkernslb;
540		}
541
542		if (i == n_slbs)
543			slbcache[USER_SLB_SLOT].slbe = 1;
544	}
545
546	/* Sacrifice a random SLB entry that is not the user entry */
547	i = mftb() % n_slbs;
548	if (i == USER_SLB_SLOT)
549		i = (i+1) % n_slbs;
550
551fillkernslb:
552	/* Write new entry */
553	slbcache[i].slbv = slbv;
554	slbcache[i].slbe = slbe | (uint64_t)i;
555
556	/* Trap handler will restore from cache on exit */
557}
558
559static int
560handle_user_slb_spill(pmap_t pm, vm_offset_t addr)
561{
562	struct slb *user_entry;
563	uint64_t esid;
564	int i;
565
566	esid = (uintptr_t)addr >> ADDR_SR_SHFT;
567
568	PMAP_LOCK(pm);
569	user_entry = user_va_to_slb_entry(pm, addr);
570
571	if (user_entry == NULL) {
572		/* allocate_vsid auto-spills it */
573		(void)allocate_user_vsid(pm, esid, 0);
574	} else {
575		/*
576		 * Check that another CPU has not already mapped this.
577		 * XXX: Per-thread SLB caches would be better.
578		 */
579		for (i = 0; i < pm->pm_slb_len; i++)
580			if (pm->pm_slb[i] == user_entry)
581				break;
582
583		if (i == pm->pm_slb_len)
584			slb_insert_user(pm, user_entry);
585	}
586	PMAP_UNLOCK(pm);
587
588	return (0);
589}
590#endif
591
592static int
593trap_pfault(struct trapframe *frame, int user)
594{
595	vm_offset_t	eva, va;
596	struct		thread *td;
597	struct		proc *p;
598	vm_map_t	map;
599	vm_prot_t	ftype;
600	int		rv;
601	register_t	user_sr;
602
603	td = curthread;
604	p = td->td_proc;
605	if (frame->exc == EXC_ISI) {
606		eva = frame->srr0;
607		ftype = VM_PROT_EXECUTE;
608		if (frame->srr1 & SRR1_ISI_PFAULT)
609			ftype |= VM_PROT_READ;
610	} else {
611		eva = frame->cpu.aim.dar;
612		if (frame->cpu.aim.dsisr & DSISR_STORE)
613			ftype = VM_PROT_WRITE;
614		else
615			ftype = VM_PROT_READ;
616	}
617
618	if (user) {
619		map = &p->p_vmspace->vm_map;
620	} else {
621		if ((eva >> ADDR_SR_SHFT) == (USER_ADDR >> ADDR_SR_SHFT)) {
622			if (p->p_vmspace == NULL)
623				return (SIGSEGV);
624
625			map = &p->p_vmspace->vm_map;
626
627			user_sr = td->td_pcb->pcb_cpu.aim.usr_segm;
628			eva &= ADDR_PIDX | ADDR_POFF;
629			eva |= user_sr << ADDR_SR_SHFT;
630		} else {
631			map = kernel_map;
632		}
633	}
634	va = trunc_page(eva);
635
636	if (map != kernel_map) {
637		/*
638		 * Keep swapout from messing with us during this
639		 *	critical time.
640		 */
641		PROC_LOCK(p);
642		++p->p_lock;
643		PROC_UNLOCK(p);
644
645		/* Fault in the user page: */
646		rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
647
648		PROC_LOCK(p);
649		--p->p_lock;
650		PROC_UNLOCK(p);
651		/*
652		 * XXXDTRACE: add dtrace_doubletrap_func here?
653		 */
654	} else {
655		/*
656		 * Don't have to worry about process locking or stacks in the
657		 * kernel.
658		 */
659		rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
660	}
661
662	if (rv == KERN_SUCCESS)
663		return (0);
664
665	if (!user && handle_onfault(frame))
666		return (0);
667
668	return (SIGSEGV);
669}
670
671/*
672 * For now, this only deals with the particular unaligned access case
673 * that gcc tends to generate.  Eventually it should handle all of the
674 * possibilities that can happen on a 32-bit PowerPC in big-endian mode.
675 */
676
677static int
678fix_unaligned(struct thread *td, struct trapframe *frame)
679{
680	struct thread	*fputhread;
681	int		indicator, reg;
682	double		*fpr;
683
684	indicator = EXC_ALI_OPCODE_INDICATOR(frame->cpu.aim.dsisr);
685
686	switch (indicator) {
687	case EXC_ALI_LFD:
688	case EXC_ALI_STFD:
689		reg = EXC_ALI_RST(frame->cpu.aim.dsisr);
690		fpr = &td->td_pcb->pcb_fpu.fpr[reg];
691		fputhread = PCPU_GET(fputhread);
692
693		/* Juggle the FPU to ensure that we've initialized
694		 * the FPRs, and that their current state is in
695		 * the PCB.
696		 */
697		if (fputhread != td) {
698			if (fputhread)
699				save_fpu(fputhread);
700			enable_fpu(td);
701		}
702		save_fpu(td);
703
704		if (indicator == EXC_ALI_LFD) {
705			if (copyin((void *)frame->cpu.aim.dar, fpr,
706			    sizeof(double)) != 0)
707				return -1;
708			enable_fpu(td);
709		} else {
710			if (copyout(fpr, (void *)frame->cpu.aim.dar,
711			    sizeof(double)) != 0)
712				return -1;
713		}
714		return 0;
715		break;
716	}
717
718	return -1;
719}
720
721