trap.c revision 151316
1/*-
2 * Copyright (c) 2001, Jake Burkholder
3 * Copyright (C) 1994, David Greenman
4 * Copyright (c) 1990, 1993
5 *      The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * the University of Utah, and William Jolitz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *      This product includes software developed by the University of
21 *      California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *      from: @(#)trap.c        7.4 (Berkeley) 5/13/91
39 * 	from: FreeBSD: src/sys/i386/i386/trap.c,v 1.197 2001/07/19
40 * $FreeBSD: head/sys/sparc64/sparc64/trap.c 151316 2005-10-14 12:43:47Z davidxu $
41 */
42
43#include "opt_ddb.h"
44#include "opt_ktr.h"
45#include "opt_ktrace.h"
46
47#include <sys/param.h>
48#include <sys/kdb.h>
49#include <sys/kernel.h>
50#include <sys/bus.h>
51#include <sys/interrupt.h>
52#include <sys/ktr.h>
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <sys/systm.h>
56#include <sys/pioctl.h>
57#include <sys/ptrace.h>
58#include <sys/proc.h>
59#include <sys/smp.h>
60#include <sys/signalvar.h>
61#include <sys/syscall.h>
62#include <sys/sysctl.h>
63#include <sys/sysent.h>
64#include <sys/vmmeter.h>
65#ifdef KTRACE
66#include <sys/uio.h>
67#include <sys/ktrace.h>
68#endif
69
70#include <vm/vm.h>
71#include <vm/pmap.h>
72#include <vm/vm_extern.h>
73#include <vm/vm_param.h>
74#include <vm/vm_kern.h>
75#include <vm/vm_map.h>
76#include <vm/vm_page.h>
77
78#include <machine/clock.h>
79#include <machine/cpu.h>
80#include <machine/frame.h>
81#include <machine/intr_machdep.h>
82#include <machine/pcb.h>
83#include <machine/smp.h>
84#include <machine/trap.h>
85#include <machine/tstate.h>
86#include <machine/tte.h>
87#include <machine/tlb.h>
88#include <machine/tsb.h>
89#include <machine/watch.h>
90
91void trap(struct trapframe *tf);
92void syscall(struct trapframe *tf);
93
94static int trap_pfault(struct thread *td, struct trapframe *tf);
95
96extern char copy_fault[];
97extern char copy_nofault_begin[];
98extern char copy_nofault_end[];
99
100extern char fs_fault[];
101extern char fs_nofault_begin[];
102extern char fs_nofault_end[];
103extern char fs_nofault_intr_begin[];
104extern char fs_nofault_intr_end[];
105
106extern char fas_fault[];
107extern char fas_nofault_begin[];
108extern char fas_nofault_end[];
109
110extern char *syscallnames[];
111
112const char *trap_msg[] = {
113	"reserved",
114	"instruction access exception",
115	"instruction access error",
116	"instruction access protection",
117	"illtrap instruction",
118	"illegal instruction",
119	"privileged opcode",
120	"floating point disabled",
121	"floating point exception ieee 754",
122	"floating point exception other",
123	"tag overflow",
124	"division by zero",
125	"data access exception",
126	"data access error",
127	"data access protection",
128	"memory address not aligned",
129	"privileged action",
130	"async data error",
131	"trap instruction 16",
132	"trap instruction 17",
133	"trap instruction 18",
134	"trap instruction 19",
135	"trap instruction 20",
136	"trap instruction 21",
137	"trap instruction 22",
138	"trap instruction 23",
139	"trap instruction 24",
140	"trap instruction 25",
141	"trap instruction 26",
142	"trap instruction 27",
143	"trap instruction 28",
144	"trap instruction 29",
145	"trap instruction 30",
146	"trap instruction 31",
147	"fast instruction access mmu miss",
148	"fast data access mmu miss",
149	"interrupt",
150	"physical address watchpoint",
151	"virtual address watchpoint",
152	"corrected ecc error",
153	"spill",
154	"fill",
155	"fill",
156	"breakpoint",
157	"clean window",
158	"range check",
159	"fix alignment",
160	"integer overflow",
161	"syscall",
162	"restore physical watchpoint",
163	"restore virtual watchpoint",
164	"kernel stack fault",
165};
166
167const int trap_sig[] = {
168	SIGILL,			/* reserved */
169	SIGILL,			/* instruction access exception */
170	SIGILL,			/* instruction access error */
171	SIGILL,			/* instruction access protection */
172	SIGILL,			/* illtrap instruction */
173	SIGILL,			/* illegal instruction */
174	SIGBUS,			/* privileged opcode */
175	SIGFPE,			/* floating point disabled */
176	SIGFPE,			/* floating point exception ieee 754 */
177	SIGFPE,			/* floating point exception other */
178	SIGEMT,			/* tag overflow */
179	SIGFPE,			/* division by zero */
180	SIGILL,			/* data access exception */
181	SIGILL,			/* data access error */
182	SIGBUS,			/* data access protection */
183	SIGBUS,			/* memory address not aligned */
184	SIGBUS,			/* privileged action */
185	SIGBUS,			/* async data error */
186	SIGILL,			/* trap instruction 16 */
187	SIGILL,			/* trap instruction 17 */
188	SIGILL,			/* trap instruction 18 */
189	SIGILL,			/* trap instruction 19 */
190	SIGILL,			/* trap instruction 20 */
191	SIGILL,			/* trap instruction 21 */
192	SIGILL,			/* trap instruction 22 */
193	SIGILL,			/* trap instruction 23 */
194	SIGILL,			/* trap instruction 24 */
195	SIGILL,			/* trap instruction 25 */
196	SIGILL,			/* trap instruction 26 */
197	SIGILL,			/* trap instruction 27 */
198	SIGILL,			/* trap instruction 28 */
199	SIGILL,			/* trap instruction 29 */
200	SIGILL,			/* trap instruction 30 */
201	SIGILL,			/* trap instruction 31 */
202	SIGSEGV,		/* fast instruction access mmu miss */
203	SIGSEGV,		/* fast data access mmu miss */
204	-1,			/* interrupt */
205	-1,			/* physical address watchpoint */
206	-1,			/* virtual address watchpoint */
207	-1,			/* corrected ecc error */
208	SIGILL,			/* spill */
209	SIGILL,			/* fill */
210	SIGILL,			/* fill */
211	SIGTRAP,		/* breakpoint */
212	SIGILL,			/* clean window */
213	SIGILL,			/* range check */
214	SIGILL,			/* fix alignment */
215	SIGILL,			/* integer overflow */
216	SIGSYS,			/* syscall */
217	-1,			/* restore physical watchpoint */
218	-1,			/* restore virtual watchpoint */
219	-1,			/* kernel stack fault */
220};
221
222CTASSERT(sizeof(struct trapframe) == 256);
223
224int debugger_on_signal = 0;
225SYSCTL_INT(_debug, OID_AUTO, debugger_on_signal, CTLFLAG_RW,
226    &debugger_on_signal, 0, "");
227
228void
229trap(struct trapframe *tf)
230{
231	struct thread *td;
232	struct proc *p;
233	u_int sticks;
234	int error;
235	int sig;
236	ksiginfo_t ksi;
237
238	td = PCPU_GET(curthread);
239
240	CTR4(KTR_TRAP, "trap: %p type=%s (%s) pil=%#lx", td,
241	    trap_msg[tf->tf_type & ~T_KERNEL],
242	    (TRAPF_USERMODE(tf) ? "user" : "kernel"), rdpr(pil));
243
244	PCPU_LAZY_INC(cnt.v_trap);
245
246	if ((tf->tf_tstate & TSTATE_PRIV) == 0) {
247		KASSERT(td != NULL, ("trap: curthread NULL"));
248		KASSERT(td->td_proc != NULL, ("trap: curproc NULL"));
249
250		p = td->td_proc;
251		sticks = td->td_sticks;
252		td->td_frame = tf;
253		if (td->td_ucred != p->p_ucred)
254			cred_update_thread(td);
255
256		switch (tf->tf_type) {
257		case T_DATA_MISS:
258		case T_DATA_PROTECTION:
259		case T_INSTRUCTION_MISS:
260			sig = trap_pfault(td, tf);
261			break;
262		case T_FILL:
263			sig = rwindow_load(td, tf, 2);
264			break;
265		case T_FILL_RET:
266			sig = rwindow_load(td, tf, 1);
267			break;
268		case T_SPILL:
269			sig = rwindow_save(td);
270			break;
271		default:
272			if (tf->tf_type < 0 || tf->tf_type >= T_MAX ||
273			    trap_sig[tf->tf_type] == -1)
274				panic("trap: bad trap type");
275			sig = trap_sig[tf->tf_type];
276			break;
277		}
278
279		if (sig != 0) {
280			/* Translate fault for emulators. */
281			if (p->p_sysent->sv_transtrap != NULL) {
282				sig = p->p_sysent->sv_transtrap(sig,
283				    tf->tf_type);
284			}
285			if (debugger_on_signal &&
286			    (sig == 4 || sig == 10 || sig == 11))
287				kdb_enter("trapsig");
288			ksiginfo_init_trap(&ksi);
289			ksi.ksi_signo = sig;
290			ksi.ksi_code = (int)tf->tf_type; /* XXX not POSIX */
291			/* ksi.ksi_addr = ? */
292			ksi.ksi_trap = (int)tf->tf_type;
293			trapsignal(td, &ksi);
294		}
295
296		userret(td, tf, sticks);
297		mtx_assert(&Giant, MA_NOTOWNED);
298 	} else {
299		KASSERT((tf->tf_type & T_KERNEL) != 0,
300		    ("trap: kernel trap isn't"));
301
302#ifdef KDB
303		if (kdb_active) {
304			kdb_reenter();
305			return;
306		}
307#endif
308
309		switch (tf->tf_type & ~T_KERNEL) {
310#ifdef KDB
311		case T_BREAKPOINT:
312		case T_KSTACK_FAULT:
313			error = (kdb_trap(tf->tf_type, 0, tf) == 0);
314			TF_DONE(tf);
315			break;
316#ifdef notyet
317		case T_PA_WATCHPOINT:
318		case T_VA_WATCHPOINT:
319			error = db_watch_trap(tf);
320			break;
321#endif
322#endif
323		case T_DATA_MISS:
324		case T_DATA_PROTECTION:
325		case T_INSTRUCTION_MISS:
326			error = trap_pfault(td, tf);
327			break;
328		case T_DATA_EXCEPTION:
329		case T_MEM_ADDRESS_NOT_ALIGNED:
330			if ((tf->tf_sfsr & MMU_SFSR_FV) != 0 &&
331			    MMU_SFSR_GET_ASI(tf->tf_sfsr) == ASI_AIUP) {
332				if (tf->tf_tpc >= (u_long)copy_nofault_begin &&
333				    tf->tf_tpc <= (u_long)copy_nofault_end) {
334					tf->tf_tpc = (u_long)copy_fault;
335					tf->tf_tnpc = tf->tf_tpc + 4;
336					error = 0;
337					break;
338				}
339				if (tf->tf_tpc >= (u_long)fs_nofault_begin &&
340				    tf->tf_tpc <= (u_long)fs_nofault_end) {
341					tf->tf_tpc = (u_long)fs_fault;
342					tf->tf_tnpc = tf->tf_tpc + 4;
343					error = 0;
344					break;
345				}
346			}
347			error = 1;
348			break;
349		case T_DATA_ERROR:
350			/*
351			 * handle PCI poke/peek as per UltraSPARC IIi
352			 * User's Manual 16.2.1.
353			 */
354#define MEMBARSYNC_INST	((u_int32_t)0x8143e040)
355			if (tf->tf_tpc > (u_long)fas_nofault_begin &&
356			    tf->tf_tpc < (u_long)fas_nofault_end &&
357			    *(u_int32_t *)tf->tf_tpc == MEMBARSYNC_INST &&
358			    ((u_int32_t *)tf->tf_tpc)[-2] == MEMBARSYNC_INST) {
359				cache_flush();
360				cache_enable();
361				tf->tf_tpc = (u_long)fas_fault;
362				tf->tf_tnpc = tf->tf_tpc + 4;
363				error = 0;
364				break;
365			}
366#undef MEMBARSYNC_INST
367			error = 1;
368			break;
369		default:
370			error = 1;
371			break;
372		}
373
374		if (error != 0)
375			panic("trap: %s", trap_msg[tf->tf_type & ~T_KERNEL]);
376	}
377	CTR1(KTR_TRAP, "trap: td=%p return", td);
378}
379
380static int
381trap_pfault(struct thread *td, struct trapframe *tf)
382{
383	struct vmspace *vm;
384	struct pcb *pcb;
385	struct proc *p;
386	vm_offset_t va;
387	vm_prot_t prot;
388	u_long ctx;
389	int flags;
390	int type;
391	int rv;
392
393	if (td == NULL)
394		return (-1);
395	KASSERT(td->td_pcb != NULL, ("trap_pfault: pcb NULL"));
396	KASSERT(td->td_proc != NULL, ("trap_pfault: curproc NULL"));
397	KASSERT(td->td_proc->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
398
399	p = td->td_proc;
400
401	rv = KERN_SUCCESS;
402	ctx = TLB_TAR_CTX(tf->tf_tar);
403	pcb = td->td_pcb;
404	type = tf->tf_type & ~T_KERNEL;
405	va = TLB_TAR_VA(tf->tf_tar);
406
407	CTR4(KTR_TRAP, "trap_pfault: td=%p pm_ctx=%#lx va=%#lx ctx=%#lx",
408	    td, p->p_vmspace->vm_pmap.pm_context[PCPU_GET(cpuid)], va, ctx);
409
410	if (type == T_DATA_PROTECTION) {
411		prot = VM_PROT_WRITE;
412		flags = VM_FAULT_DIRTY;
413	} else {
414		if (type == T_DATA_MISS)
415			prot = VM_PROT_READ;
416		else
417			prot = VM_PROT_READ | VM_PROT_EXECUTE;
418		flags = VM_FAULT_NORMAL;
419	}
420
421	if (ctx != TLB_CTX_KERNEL) {
422		if ((tf->tf_tstate & TSTATE_PRIV) != 0 &&
423		    (tf->tf_tpc >= (u_long)fs_nofault_intr_begin &&
424		     tf->tf_tpc <= (u_long)fs_nofault_intr_end)) {
425			tf->tf_tpc = (u_long)fs_fault;
426			tf->tf_tnpc = tf->tf_tpc + 4;
427			return (0);
428		}
429
430		/*
431		 * This is a fault on non-kernel virtual memory.
432		 */
433		vm = p->p_vmspace;
434
435		/*
436		 * Keep swapout from messing with us during this
437		 * critical time.
438		 */
439		PROC_LOCK(p);
440		++p->p_lock;
441		PROC_UNLOCK(p);
442
443		/* Fault in the user page. */
444		rv = vm_fault(&vm->vm_map, va, prot, flags);
445
446		/*
447		 * Now the process can be swapped again.
448		 */
449		PROC_LOCK(p);
450		--p->p_lock;
451		PROC_UNLOCK(p);
452	} else {
453		/*
454		 * This is a fault on kernel virtual memory.  Attempts to
455		 * access kernel memory from user mode cause privileged
456		 * action traps, not page fault.
457		 */
458		KASSERT(tf->tf_tstate & TSTATE_PRIV,
459		    ("trap_pfault: fault on nucleus context from user mode"));
460
461		/*
462		 * Don't have to worry about process locking or stacks in the
463		 * kernel.
464		 */
465		rv = vm_fault(kernel_map, va, prot, VM_FAULT_NORMAL);
466	}
467
468	CTR3(KTR_TRAP, "trap_pfault: return td=%p va=%#lx rv=%d",
469	    td, va, rv);
470	if (rv == KERN_SUCCESS)
471		return (0);
472	if (ctx != TLB_CTX_KERNEL && (tf->tf_tstate & TSTATE_PRIV) != 0) {
473		if (tf->tf_tpc >= (u_long)fs_nofault_begin &&
474		    tf->tf_tpc <= (u_long)fs_nofault_end) {
475			tf->tf_tpc = (u_long)fs_fault;
476			tf->tf_tnpc = tf->tf_tpc + 4;
477			return (0);
478		}
479		if (tf->tf_tpc >= (u_long)copy_nofault_begin &&
480		    tf->tf_tpc <= (u_long)copy_nofault_end) {
481			tf->tf_tpc = (u_long)copy_fault;
482			tf->tf_tnpc = tf->tf_tpc + 4;
483			return (0);
484		}
485	}
486	return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
487}
488
489/* Maximum number of arguments that can be passed via the out registers. */
490#define	REG_MAXARGS	6
491
492/*
493 * Syscall handler. The arguments to the syscall are passed in the o registers
494 * by the caller, and are saved in the trap frame. The syscall number is passed
495 * in %g1 (and also saved in the trap frame).
496 */
497void
498syscall(struct trapframe *tf)
499{
500	struct sysent *callp;
501	struct thread *td;
502	register_t args[8];
503	register_t *argp;
504	struct proc *p;
505	u_int sticks;
506	u_long code;
507	u_long tpc;
508	int reg;
509	int regcnt;
510	int narg;
511	int error;
512
513	td = PCPU_GET(curthread);
514	KASSERT(td != NULL, ("trap: curthread NULL"));
515	KASSERT(td->td_proc != NULL, ("trap: curproc NULL"));
516
517	p = td->td_proc;
518
519	PCPU_LAZY_INC(cnt.v_syscall);
520
521	narg = 0;
522	error = 0;
523	reg = 0;
524	regcnt = REG_MAXARGS;
525
526	sticks = td->td_sticks;
527	td->td_frame = tf;
528	if (td->td_ucred != p->p_ucred)
529		cred_update_thread(td);
530	if (p->p_flag & P_SA)
531		thread_user_enter(td);
532	code = tf->tf_global[1];
533
534	/*
535	 * For syscalls, we don't want to retry the faulting instruction
536	 * (usually), instead we need to advance one instruction.
537	 */
538	tpc = tf->tf_tpc;
539	TF_DONE(tf);
540
541	if (p->p_sysent->sv_prepsyscall) {
542		/*
543		 * The prep code is MP aware.
544		 */
545#if 0
546		(*p->p_sysent->sv_prepsyscall)(tf, args, &code, &params);
547#endif
548	} else 	if (code == SYS_syscall || code == SYS___syscall) {
549		code = tf->tf_out[reg++];
550		regcnt--;
551	}
552
553 	if (p->p_sysent->sv_mask)
554 		code &= p->p_sysent->sv_mask;
555
556 	if (code >= p->p_sysent->sv_size)
557 		callp = &p->p_sysent->sv_table[0];
558  	else
559 		callp = &p->p_sysent->sv_table[code];
560
561	narg = callp->sy_narg & SYF_ARGMASK;
562
563	if (narg <= regcnt) {
564		argp = &tf->tf_out[reg];
565		error = 0;
566	} else {
567		KASSERT(narg <= sizeof(args) / sizeof(args[0]),
568		    ("Too many syscall arguments!"));
569		argp = args;
570		bcopy(&tf->tf_out[reg], args, sizeof(args[0]) * regcnt);
571		error = copyin((void *)(tf->tf_out[6] + SPOFF +
572		    offsetof(struct frame, fr_pad[6])),
573		    &args[regcnt], (narg - regcnt) * sizeof(args[0]));
574	}
575
576	CTR5(KTR_SYSC, "syscall: td=%p %s(%#lx, %#lx, %#lx)", td,
577	    syscallnames[code], argp[0], argp[1], argp[2]);
578
579	/*
580	 * Try to run the syscall without the MP lock if the syscall
581	 * is MP safe.
582	 */
583	if ((callp->sy_narg & SYF_MPSAFE) == 0)
584		mtx_lock(&Giant);
585
586#ifdef KTRACE
587	if (KTRPOINT(td, KTR_SYSCALL))
588		ktrsyscall(code, narg, argp);
589#endif
590	if (error == 0) {
591		td->td_retval[0] = 0;
592		td->td_retval[1] = 0;
593
594		STOPEVENT(p, S_SCE, narg);	/* MP aware */
595
596		PTRACESTOP_SC(p, td, S_PT_SCE);
597
598		error = (*callp->sy_call)(td, argp);
599
600		CTR5(KTR_SYSC, "syscall: p=%p error=%d %s return %#lx %#lx ", p,
601		    error, syscallnames[code], td->td_retval[0],
602		    td->td_retval[1]);
603	}
604
605	/*
606	 * MP SAFE (we may or may not have the MP lock at this point)
607	 */
608	switch (error) {
609	case 0:
610		tf->tf_out[0] = td->td_retval[0];
611		tf->tf_out[1] = td->td_retval[1];
612		tf->tf_tstate &= ~TSTATE_XCC_C;
613		break;
614
615	case ERESTART:
616		/*
617		 * Undo the tpc advancement we have done above, we want to
618		 * reexecute the system call.
619		 */
620		tf->tf_tpc = tpc;
621		tf->tf_tnpc -= 4;
622		break;
623
624	case EJUSTRETURN:
625		break;
626
627	default:
628 		if (p->p_sysent->sv_errsize) {
629 			if (error >= p->p_sysent->sv_errsize)
630  				error = -1;	/* XXX */
631   			else
632  				error = p->p_sysent->sv_errtbl[error];
633		}
634		tf->tf_out[0] = error;
635		tf->tf_tstate |= TSTATE_XCC_C;
636		break;
637	}
638
639	/*
640	 * Release Giant if we had to get it.  Don't use mtx_owned(),
641	 * we want to catch broken syscalls.
642	 */
643	if ((callp->sy_narg & SYF_MPSAFE) == 0)
644		mtx_unlock(&Giant);
645
646	/*
647	 * Handle reschedule and other end-of-syscall issues
648	 */
649	userret(td, tf, sticks);
650
651#ifdef KTRACE
652	if (KTRPOINT(td, KTR_SYSRET))
653		ktrsysret(code, error, td->td_retval[0]);
654#endif
655	/*
656	 * This works because errno is findable through the
657	 * register set.  If we ever support an emulation where this
658	 * is not the case, this code will need to be revisited.
659	 */
660	STOPEVENT(p, S_SCX, code);
661
662	PTRACESTOP_SC(p, td, S_PT_SCX);
663
664	WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
665	    (code >= 0 && code < SYS_MAXSYSCALL) ? syscallnames[code] : "???");
666	mtx_assert(&sched_lock, MA_NOTOWNED);
667	mtx_assert(&Giant, MA_NOTOWNED);
668}
669