xref: /freebsd/sys/riscv/riscv/trap.c (revision 6ec8bf9f)
128029b68SRuslan Bukin /*-
2b977d819SRuslan Bukin  * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
328029b68SRuslan Bukin  * All rights reserved.
428029b68SRuslan Bukin  *
528029b68SRuslan Bukin  * Portions of this software were developed by SRI International and the
628029b68SRuslan Bukin  * University of Cambridge Computer Laboratory under DARPA/AFRL contract
728029b68SRuslan Bukin  * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
828029b68SRuslan Bukin  *
928029b68SRuslan Bukin  * Portions of this software were developed by the University of Cambridge
1028029b68SRuslan Bukin  * Computer Laboratory as part of the CTSRD Project, with support from the
1128029b68SRuslan Bukin  * UK Higher Education Innovation Fund (HEIF).
1228029b68SRuslan Bukin  *
1328029b68SRuslan Bukin  * Redistribution and use in source and binary forms, with or without
1428029b68SRuslan Bukin  * modification, are permitted provided that the following conditions
1528029b68SRuslan Bukin  * are met:
1628029b68SRuslan Bukin  * 1. Redistributions of source code must retain the above copyright
1728029b68SRuslan Bukin  *    notice, this list of conditions and the following disclaimer.
1828029b68SRuslan Bukin  * 2. Redistributions in binary form must reproduce the above copyright
1928029b68SRuslan Bukin  *    notice, this list of conditions and the following disclaimer in the
2028029b68SRuslan Bukin  *    documentation and/or other materials provided with the distribution.
2128029b68SRuslan Bukin  *
2228029b68SRuslan Bukin  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2328029b68SRuslan Bukin  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2428029b68SRuslan Bukin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2528029b68SRuslan Bukin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2628029b68SRuslan Bukin  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2728029b68SRuslan Bukin  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2828029b68SRuslan Bukin  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2928029b68SRuslan Bukin  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3028029b68SRuslan Bukin  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3128029b68SRuslan Bukin  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3228029b68SRuslan Bukin  * SUCH DAMAGE.
3328029b68SRuslan Bukin  */
3428029b68SRuslan Bukin 
3528029b68SRuslan Bukin #include <sys/cdefs.h>
361c776124SChristos Margiolis #include "opt_ddb.h"
371c776124SChristos Margiolis 
3828029b68SRuslan Bukin #include <sys/param.h>
3928029b68SRuslan Bukin #include <sys/systm.h>
4028029b68SRuslan Bukin #include <sys/kernel.h>
41daec9284SConrad Meyer #include <sys/ktr.h>
4228029b68SRuslan Bukin #include <sys/lock.h>
4328029b68SRuslan Bukin #include <sys/mutex.h>
4428029b68SRuslan Bukin #include <sys/bus.h>
4528029b68SRuslan Bukin #include <sys/proc.h>
4628029b68SRuslan Bukin #include <sys/ptrace.h>
4728029b68SRuslan Bukin #include <sys/syscall.h>
4828029b68SRuslan Bukin #include <sys/sysent.h>
49d52d6d7cSRuslan Bukin #ifdef KDB
50d52d6d7cSRuslan Bukin #include <sys/kdb.h>
51d52d6d7cSRuslan Bukin #endif
5228029b68SRuslan Bukin 
5328029b68SRuslan Bukin #include <vm/vm.h>
5428029b68SRuslan Bukin #include <vm/pmap.h>
5528029b68SRuslan Bukin #include <vm/vm_kern.h>
5628029b68SRuslan Bukin #include <vm/vm_map.h>
5728029b68SRuslan Bukin #include <vm/vm_param.h>
5828029b68SRuslan Bukin #include <vm/vm_extern.h>
5928029b68SRuslan Bukin 
60232d0b87SJohn Baldwin #include <machine/fpe.h>
6128029b68SRuslan Bukin #include <machine/frame.h>
6228029b68SRuslan Bukin #include <machine/pcb.h>
6328029b68SRuslan Bukin #include <machine/pcpu.h>
6428029b68SRuslan Bukin 
6528029b68SRuslan Bukin #include <machine/resource.h>
6628029b68SRuslan Bukin #include <machine/intr.h>
6728029b68SRuslan Bukin 
68fed1ca4bSRuslan Bukin #ifdef KDTRACE_HOOKS
69fed1ca4bSRuslan Bukin #include <sys/dtrace_bsd.h>
70fed1ca4bSRuslan Bukin #endif
71fed1ca4bSRuslan Bukin 
721c776124SChristos Margiolis #ifdef DDB
731c776124SChristos Margiolis #include <ddb/ddb.h>
741c776124SChristos Margiolis #include <ddb/db_sym.h>
751c776124SChristos Margiolis #endif
761c776124SChristos Margiolis 
77*6ec8bf9fSJessica Clarke void intr_irq_handler(struct trapframe *tf);
78*6ec8bf9fSJessica Clarke 
79fed1ca4bSRuslan Bukin int (*dtrace_invop_jump_addr)(struct trapframe *);
80fed1ca4bSRuslan Bukin 
8128029b68SRuslan Bukin /* Called from exception.S */
8228029b68SRuslan Bukin void do_trap_supervisor(struct trapframe *);
8328029b68SRuslan Bukin void do_trap_user(struct trapframe *);
8428029b68SRuslan Bukin 
8528029b68SRuslan Bukin static __inline void
call_trapsignal(struct thread * td,int sig,int code,void * addr,int trapno)86c294dddbSJohn Baldwin call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
8728029b68SRuslan Bukin {
8828029b68SRuslan Bukin 	ksiginfo_t ksi;
8928029b68SRuslan Bukin 
9028029b68SRuslan Bukin 	ksiginfo_init_trap(&ksi);
9128029b68SRuslan Bukin 	ksi.ksi_signo = sig;
9228029b68SRuslan Bukin 	ksi.ksi_code = code;
9328029b68SRuslan Bukin 	ksi.ksi_addr = addr;
94c294dddbSJohn Baldwin 	ksi.ksi_trapno = trapno;
9528029b68SRuslan Bukin 	trapsignal(td, &ksi);
9628029b68SRuslan Bukin }
9728029b68SRuslan Bukin 
9828029b68SRuslan Bukin int
cpu_fetch_syscall_args(struct thread * td)992d88da2fSKonstantin Belousov cpu_fetch_syscall_args(struct thread *td)
10028029b68SRuslan Bukin {
10128029b68SRuslan Bukin 	struct proc *p;
102b1ad6a90SBrooks Davis 	syscallarg_t *ap, *dst_ap;
1032d88da2fSKonstantin Belousov 	struct syscall_args *sa;
10428029b68SRuslan Bukin 
10528029b68SRuslan Bukin 	p = td->td_proc;
1062d88da2fSKonstantin Belousov 	sa = &td->td_sa;
10728029b68SRuslan Bukin 	ap = &td->td_frame->tf_a[0];
108f7265157SEdward Tomasz Napierala 	dst_ap = &sa->args[0];
10928029b68SRuslan Bukin 
11028029b68SRuslan Bukin 	sa->code = td->td_frame->tf_t[0];
111cf98bc28SDavid Chisnall 	sa->original_code = sa->code;
11228029b68SRuslan Bukin 
113f7265157SEdward Tomasz Napierala 	if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
11428029b68SRuslan Bukin 		sa->code = *ap++;
115f7265157SEdward Tomasz Napierala 	} else {
116f7265157SEdward Tomasz Napierala 		*dst_ap++ = *ap++;
11728029b68SRuslan Bukin 	}
11828029b68SRuslan Bukin 
119f7265157SEdward Tomasz Napierala 	if (__predict_false(sa->code >= p->p_sysent->sv_size))
12039024a89SKonstantin Belousov 		sa->callp = &nosys_sysent;
12128029b68SRuslan Bukin 	else
12228029b68SRuslan Bukin 		sa->callp = &p->p_sysent->sv_table[sa->code];
12328029b68SRuslan Bukin 
124f7265157SEdward Tomasz Napierala 	KASSERT(sa->callp->sy_narg <= nitems(sa->args),
125f7265157SEdward Tomasz Napierala 	    ("Syscall %d takes too many arguments", sa->code));
126f7265157SEdward Tomasz Napierala 
127b1ad6a90SBrooks Davis 	memcpy(dst_ap, ap, (NARGREG - 1) * sizeof(*dst_ap));
12828029b68SRuslan Bukin 
12928029b68SRuslan Bukin 	td->td_retval[0] = 0;
13028029b68SRuslan Bukin 	td->td_retval[1] = 0;
13128029b68SRuslan Bukin 
13228029b68SRuslan Bukin 	return (0);
13328029b68SRuslan Bukin }
13428029b68SRuslan Bukin 
13528029b68SRuslan Bukin #include "../../kern/subr_syscall.c"
13628029b68SRuslan Bukin 
13728029b68SRuslan Bukin static void
print_with_symbol(const char * name,uint64_t value)1381c776124SChristos Margiolis print_with_symbol(const char *name, uint64_t value)
1391c776124SChristos Margiolis {
1401c776124SChristos Margiolis #ifdef DDB
1411c776124SChristos Margiolis 	c_db_sym_t sym;
1421c776124SChristos Margiolis 	db_expr_t sym_value;
1431c776124SChristos Margiolis 	db_expr_t offset;
1441c776124SChristos Margiolis 	const char *sym_name;
1451c776124SChristos Margiolis #endif
1461c776124SChristos Margiolis 
1471c776124SChristos Margiolis 	printf("%7s: 0x%016lx", name, value);
1481c776124SChristos Margiolis 
1491c776124SChristos Margiolis #ifdef DDB
1501c776124SChristos Margiolis 	if (value >= VM_MIN_KERNEL_ADDRESS) {
1511c776124SChristos Margiolis 		sym = db_search_symbol(value, DB_STGY_ANY, &offset);
1521c776124SChristos Margiolis 		if (sym != C_DB_SYM_NULL) {
1531c776124SChristos Margiolis 			db_symbol_values(sym, &sym_name, &sym_value);
154ba675bb9SJohn Baldwin 			if (offset != 0)
1551c776124SChristos Margiolis 				printf(" (%s + 0x%lx)", sym_name, offset);
156ba675bb9SJohn Baldwin 			else
157ba675bb9SJohn Baldwin 				printf(" (%s)", sym_name);
1581c776124SChristos Margiolis 		}
1591c776124SChristos Margiolis 	}
1601c776124SChristos Margiolis #endif
1611c776124SChristos Margiolis 	printf("\n");
1621c776124SChristos Margiolis }
1631c776124SChristos Margiolis 
1641c776124SChristos Margiolis static void
dump_regs(struct trapframe * frame)16528029b68SRuslan Bukin dump_regs(struct trapframe *frame)
16628029b68SRuslan Bukin {
1671c776124SChristos Margiolis 	char name[6];
16828029b68SRuslan Bukin 	int i;
16928029b68SRuslan Bukin 
1701c776124SChristos Margiolis 	for (i = 0; i < nitems(frame->tf_t); i++) {
1711c776124SChristos Margiolis 		snprintf(name, sizeof(name), "t[%d]", i);
1721c776124SChristos Margiolis 		print_with_symbol(name, frame->tf_t[i]);
1731c776124SChristos Margiolis 	}
17428029b68SRuslan Bukin 
1751c776124SChristos Margiolis 	for (i = 0; i < nitems(frame->tf_s); i++) {
1761c776124SChristos Margiolis 		snprintf(name, sizeof(name), "s[%d]", i);
1771c776124SChristos Margiolis 		print_with_symbol(name, frame->tf_s[i]);
1781c776124SChristos Margiolis 	}
17928029b68SRuslan Bukin 
1801c776124SChristos Margiolis 	for (i = 0; i < nitems(frame->tf_a); i++) {
1811c776124SChristos Margiolis 		snprintf(name, sizeof(name), "a[%d]", i);
1821c776124SChristos Margiolis 		print_with_symbol(name, frame->tf_a[i]);
1831c776124SChristos Margiolis 	}
18428029b68SRuslan Bukin 
1851c776124SChristos Margiolis 	print_with_symbol("ra", frame->tf_ra);
1861c776124SChristos Margiolis 	print_with_symbol("sp", frame->tf_sp);
1871c776124SChristos Margiolis 	print_with_symbol("gp", frame->tf_gp);
1881c776124SChristos Margiolis 	print_with_symbol("tp", frame->tf_tp);
1891c776124SChristos Margiolis 	print_with_symbol("sepc", frame->tf_sepc);
1901c776124SChristos Margiolis 	printf("sstatus: 0x%016lx\n", frame->tf_sstatus);
1913b5fc5eeSJohn Baldwin 	printf("stval  : 0x%016lx\n", frame->tf_stval);
19228029b68SRuslan Bukin }
19328029b68SRuslan Bukin 
19428029b68SRuslan Bukin static void
ecall_handler(void)1955319fa1bSEdward Tomasz Napierala ecall_handler(void)
19628029b68SRuslan Bukin {
19728029b68SRuslan Bukin 	struct thread *td;
19828029b68SRuslan Bukin 
19928029b68SRuslan Bukin 	td = curthread;
200f1577619SEdward Tomasz Napierala 
201c18ca749SJohn Baldwin 	syscallenter(td);
202c18ca749SJohn Baldwin 	syscallret(td);
20328029b68SRuslan Bukin }
20428029b68SRuslan Bukin 
20528029b68SRuslan Bukin static void
page_fault_handler(struct trapframe * frame,int usermode)206da8944d9SJessica Clarke page_fault_handler(struct trapframe *frame, int usermode)
20728029b68SRuslan Bukin {
20828029b68SRuslan Bukin 	struct vm_map *map;
20953941c0aSMark Johnston 	uint64_t stval;
21028029b68SRuslan Bukin 	struct thread *td;
21128029b68SRuslan Bukin 	struct pcb *pcb;
21228029b68SRuslan Bukin 	vm_prot_t ftype;
21328029b68SRuslan Bukin 	vm_offset_t va;
21428029b68SRuslan Bukin 	struct proc *p;
2153ec68206SMark Johnston 	int error, sig, ucode;
2165a28499fSMitchell Horne #ifdef KDB
2175a28499fSMitchell Horne 	bool handled;
2185a28499fSMitchell Horne #endif
21928029b68SRuslan Bukin 
220d52d6d7cSRuslan Bukin #ifdef KDB
221d52d6d7cSRuslan Bukin 	if (kdb_active) {
222d52d6d7cSRuslan Bukin 		kdb_reenter();
223d52d6d7cSRuslan Bukin 		return;
224d52d6d7cSRuslan Bukin 	}
225d52d6d7cSRuslan Bukin #endif
226d52d6d7cSRuslan Bukin 
22728029b68SRuslan Bukin 	td = curthread;
2283ec68206SMark Johnston 	p = td->td_proc;
22928029b68SRuslan Bukin 	pcb = td->td_pcb;
23053941c0aSMark Johnston 	stval = frame->tf_stval;
23128029b68SRuslan Bukin 
2323ec68206SMark Johnston 	if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
2333ec68206SMark Johnston 	    WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
2343ec68206SMark Johnston 	    "Kernel page fault") != 0)
2353ec68206SMark Johnston 		goto fatal;
23628029b68SRuslan Bukin 
237958a0943SMitchell Horne 	if (usermode) {
2384a9f2f8bSMitchell Horne 		if (!VIRT_IS_VALID(stval)) {
2394a9f2f8bSMitchell Horne 			call_trapsignal(td, SIGSEGV, SEGV_MAPERR, (void *)stval,
2404a9f2f8bSMitchell Horne 			    frame->tf_scause & SCAUSE_CODE);
2414a9f2f8bSMitchell Horne 			goto done;
2424a9f2f8bSMitchell Horne 		}
243eb81812fSMitchell Horne 		map = &p->p_vmspace->vm_map;
244958a0943SMitchell Horne 	} else {
245958a0943SMitchell Horne 		/*
246958a0943SMitchell Horne 		 * Enable interrupts for the duration of the page fault. For
247958a0943SMitchell Horne 		 * user faults this was done already in do_trap_user().
248958a0943SMitchell Horne 		 */
249958a0943SMitchell Horne 		intr_enable();
250958a0943SMitchell Horne 
251828ea49dSMark Johnston 		if (stval >= VM_MIN_KERNEL_ADDRESS) {
25228029b68SRuslan Bukin 			map = kernel_map;
253958a0943SMitchell Horne 		} else {
2543ec68206SMark Johnston 			if (pcb->pcb_onfault == 0)
2553ec68206SMark Johnston 				goto fatal;
256eb81812fSMitchell Horne 			map = &p->p_vmspace->vm_map;
25728029b68SRuslan Bukin 		}
258958a0943SMitchell Horne 	}
25928029b68SRuslan Bukin 
26053941c0aSMark Johnston 	va = trunc_page(stval);
26128029b68SRuslan Bukin 
262cb8e0678SMitchell Horne 	if (frame->tf_scause == SCAUSE_STORE_PAGE_FAULT) {
263d198cb6dSJohn Baldwin 		ftype = VM_PROT_WRITE;
264cb8e0678SMitchell Horne 	} else if (frame->tf_scause == SCAUSE_INST_PAGE_FAULT) {
265d198cb6dSJohn Baldwin 		ftype = VM_PROT_EXECUTE;
26628029b68SRuslan Bukin 	} else {
267d198cb6dSJohn Baldwin 		ftype = VM_PROT_READ;
26828029b68SRuslan Bukin 	}
26928029b68SRuslan Bukin 
270828ea49dSMark Johnston 	if (VIRT_IS_VALID(va) && pmap_fault(map->pmap, va, ftype))
271b977d819SRuslan Bukin 		goto done;
272b977d819SRuslan Bukin 
273df08823dSKonstantin Belousov 	error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
27428029b68SRuslan Bukin 	if (error != KERN_SUCCESS) {
2753ec68206SMark Johnston 		if (usermode) {
276c294dddbSJohn Baldwin 			call_trapsignal(td, sig, ucode, (void *)stval,
277cb8e0678SMitchell Horne 			    frame->tf_scause & SCAUSE_CODE);
27828029b68SRuslan Bukin 		} else {
2793ec68206SMark Johnston 			if (pcb->pcb_onfault != 0) {
28028029b68SRuslan Bukin 				frame->tf_a[0] = error;
28128029b68SRuslan Bukin 				frame->tf_sepc = pcb->pcb_onfault;
28228029b68SRuslan Bukin 				return;
28328029b68SRuslan Bukin 			}
2843ec68206SMark Johnston 			goto fatal;
28528029b68SRuslan Bukin 		}
28628029b68SRuslan Bukin 	}
28728029b68SRuslan Bukin 
288b977d819SRuslan Bukin done:
2893ec68206SMark Johnston 	if (usermode)
29028029b68SRuslan Bukin 		userret(td, frame);
2913ec68206SMark Johnston 	return;
2923ec68206SMark Johnston 
2933ec68206SMark Johnston fatal:
2943ec68206SMark Johnston 	dump_regs(frame);
2955a28499fSMitchell Horne #ifdef KDB
2965a28499fSMitchell Horne 	if (debugger_on_trap) {
2975a28499fSMitchell Horne 		kdb_why = KDB_WHY_TRAP;
2985a28499fSMitchell Horne 		handled = kdb_trap(frame->tf_scause & SCAUSE_CODE, 0, frame);
2995a28499fSMitchell Horne 		kdb_why = KDB_WHY_UNSET;
3005a28499fSMitchell Horne 		if (handled)
3015a28499fSMitchell Horne 			return;
3025a28499fSMitchell Horne 	}
3035a28499fSMitchell Horne #endif
304ff79f35bSJohn Baldwin 	panic("Fatal page fault at %#lx: %#lx", frame->tf_sepc, stval);
30528029b68SRuslan Bukin }
30628029b68SRuslan Bukin 
30728029b68SRuslan Bukin void
do_trap_supervisor(struct trapframe * frame)30828029b68SRuslan Bukin do_trap_supervisor(struct trapframe *frame)
30928029b68SRuslan Bukin {
31028029b68SRuslan Bukin 	uint64_t exception;
31198f50c44SRuslan Bukin 
31298f50c44SRuslan Bukin 	/* Ensure we came from supervisor mode, interrupts disabled */
313ac2b208dSJohn Baldwin 	KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) ==
314ac2b208dSJohn Baldwin 	    SSTATUS_SPP, ("Came from S mode with interrupts enabled"));
31528029b68SRuslan Bukin 
3166a3a6fe3SJohn Baldwin 	KASSERT((csr_read(sstatus) & (SSTATUS_SUM)) == 0,
3176a3a6fe3SJohn Baldwin 	    ("Came from S mode with SUM enabled"));
3186a3a6fe3SJohn Baldwin 
319cb8e0678SMitchell Horne 	exception = frame->tf_scause & SCAUSE_CODE;
320cb8e0678SMitchell Horne 	if ((frame->tf_scause & SCAUSE_INTR) != 0) {
32128029b68SRuslan Bukin 		/* Interrupt */
322*6ec8bf9fSJessica Clarke 		intr_irq_handler(frame);
32328029b68SRuslan Bukin 		return;
32428029b68SRuslan Bukin 	}
32528029b68SRuslan Bukin 
326fed1ca4bSRuslan Bukin #ifdef KDTRACE_HOOKS
327fed1ca4bSRuslan Bukin 	if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
328fed1ca4bSRuslan Bukin 		return;
329fed1ca4bSRuslan Bukin #endif
330fed1ca4bSRuslan Bukin 
331ff79f35bSJohn Baldwin 	CTR4(KTR_TRAP, "%s: exception=%lu, sepc=%#lx, stval=%#lx", __func__,
33277562321SMitchell Horne 	    exception, frame->tf_sepc, frame->tf_stval);
33328029b68SRuslan Bukin 
33428029b68SRuslan Bukin 	switch (exception) {
335cb8e0678SMitchell Horne 	case SCAUSE_LOAD_ACCESS_FAULT:
336cb8e0678SMitchell Horne 	case SCAUSE_STORE_ACCESS_FAULT:
337cb8e0678SMitchell Horne 	case SCAUSE_INST_ACCESS_FAULT:
3380d3aa0fbSKristof Provost 		dump_regs(frame);
339ff79f35bSJohn Baldwin 		panic("Memory access exception at %#lx: %#lx",
340ff79f35bSJohn Baldwin 		    frame->tf_sepc, frame->tf_stval);
3410d3aa0fbSKristof Provost 		break;
3429b4cbaa9SMitchell Horne 	case SCAUSE_LOAD_MISALIGNED:
3439b4cbaa9SMitchell Horne 	case SCAUSE_STORE_MISALIGNED:
3449b4cbaa9SMitchell Horne 	case SCAUSE_INST_MISALIGNED:
3459b4cbaa9SMitchell Horne 		dump_regs(frame);
346ff79f35bSJohn Baldwin 		panic("Misaligned address exception at %#lx: %#lx",
3479b4cbaa9SMitchell Horne 		    frame->tf_sepc, frame->tf_stval);
3489b4cbaa9SMitchell Horne 		break;
349cb8e0678SMitchell Horne 	case SCAUSE_STORE_PAGE_FAULT:
350cb8e0678SMitchell Horne 	case SCAUSE_LOAD_PAGE_FAULT:
351cb8e0678SMitchell Horne 	case SCAUSE_INST_PAGE_FAULT:
352da8944d9SJessica Clarke 		page_fault_handler(frame, 0);
35328029b68SRuslan Bukin 		break;
354cb8e0678SMitchell Horne 	case SCAUSE_BREAKPOINT:
355fed1ca4bSRuslan Bukin #ifdef KDTRACE_HOOKS
356d75038a0SRuslan Bukin 		if (dtrace_invop_jump_addr != NULL &&
357d75038a0SRuslan Bukin 		    dtrace_invop_jump_addr(frame) == 0)
358fed1ca4bSRuslan Bukin 				break;
359fed1ca4bSRuslan Bukin #endif
360e1b6d4a9SRuslan Bukin #ifdef KDB
361e1b6d4a9SRuslan Bukin 		kdb_trap(exception, 0, frame);
362e1b6d4a9SRuslan Bukin #else
363e1b6d4a9SRuslan Bukin 		dump_regs(frame);
364ff79f35bSJohn Baldwin 		panic("No debugger in kernel.");
365e1b6d4a9SRuslan Bukin #endif
366d52d6d7cSRuslan Bukin 		break;
367cb8e0678SMitchell Horne 	case SCAUSE_ILLEGAL_INSTRUCTION:
368e1b6d4a9SRuslan Bukin 		dump_regs(frame);
369ff79f35bSJohn Baldwin 		panic("Illegal instruction 0x%0*lx at %#lx",
370ff79f35bSJohn Baldwin 		    (frame->tf_stval & 0x3) != 0x3 ? 4 : 8,
371ff79f35bSJohn Baldwin 		    frame->tf_stval, frame->tf_sepc);
372e1b6d4a9SRuslan Bukin 		break;
37328029b68SRuslan Bukin 	default:
37428029b68SRuslan Bukin 		dump_regs(frame);
375ff79f35bSJohn Baldwin 		panic("Unknown kernel exception %#lx trap value %#lx",
37653941c0aSMark Johnston 		    exception, frame->tf_stval);
37728029b68SRuslan Bukin 	}
37828029b68SRuslan Bukin }
37928029b68SRuslan Bukin 
38028029b68SRuslan Bukin void
do_trap_user(struct trapframe * frame)38128029b68SRuslan Bukin do_trap_user(struct trapframe *frame)
38228029b68SRuslan Bukin {
38328029b68SRuslan Bukin 	uint64_t exception;
384e1b6d4a9SRuslan Bukin 	struct thread *td;
3857804dd52SRuslan Bukin 	struct pcb *pcb;
386e1b6d4a9SRuslan Bukin 
387e1b6d4a9SRuslan Bukin 	td = curthread;
3887804dd52SRuslan Bukin 	pcb = td->td_pcb;
38928029b68SRuslan Bukin 
3905319fa1bSEdward Tomasz Napierala 	KASSERT(td->td_frame == frame,
3915319fa1bSEdward Tomasz Napierala 	    ("%s: td_frame %p != frame %p", __func__, td->td_frame, frame));
3925319fa1bSEdward Tomasz Napierala 
39398f50c44SRuslan Bukin 	/* Ensure we came from usermode, interrupts disabled */
394ac2b208dSJohn Baldwin 	KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
395ac2b208dSJohn Baldwin 	    ("Came from U mode with interrupts enabled"));
39698f50c44SRuslan Bukin 
3976a3a6fe3SJohn Baldwin 	KASSERT((csr_read(sstatus) & (SSTATUS_SUM)) == 0,
3986a3a6fe3SJohn Baldwin 	    ("Came from U mode with SUM enabled"));
3996a3a6fe3SJohn Baldwin 
400cb8e0678SMitchell Horne 	exception = frame->tf_scause & SCAUSE_CODE;
401cb8e0678SMitchell Horne 	if ((frame->tf_scause & SCAUSE_INTR) != 0) {
40228029b68SRuslan Bukin 		/* Interrupt */
403*6ec8bf9fSJessica Clarke 		intr_irq_handler(frame);
40428029b68SRuslan Bukin 		return;
40528029b68SRuslan Bukin 	}
406958a0943SMitchell Horne 	intr_enable();
40728029b68SRuslan Bukin 
408ff79f35bSJohn Baldwin 	CTR4(KTR_TRAP, "%s: exception=%lu, sepc=%#lx, stval=%#lx", __func__,
40977562321SMitchell Horne 	    exception, frame->tf_sepc, frame->tf_stval);
41028029b68SRuslan Bukin 
41128029b68SRuslan Bukin 	switch (exception) {
412cb8e0678SMitchell Horne 	case SCAUSE_LOAD_ACCESS_FAULT:
413cb8e0678SMitchell Horne 	case SCAUSE_STORE_ACCESS_FAULT:
414cb8e0678SMitchell Horne 	case SCAUSE_INST_ACCESS_FAULT:
41575f02277SKristof Provost 		call_trapsignal(td, SIGBUS, BUS_ADRERR, (void *)frame->tf_sepc,
41675f02277SKristof Provost 		    exception);
41775f02277SKristof Provost 		userret(td, frame);
41875f02277SKristof Provost 		break;
4199b4cbaa9SMitchell Horne 	case SCAUSE_LOAD_MISALIGNED:
4209b4cbaa9SMitchell Horne 	case SCAUSE_STORE_MISALIGNED:
4219b4cbaa9SMitchell Horne 	case SCAUSE_INST_MISALIGNED:
4229b4cbaa9SMitchell Horne 		call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sepc,
4239b4cbaa9SMitchell Horne 		    exception);
4249b4cbaa9SMitchell Horne 		userret(td, frame);
4259b4cbaa9SMitchell Horne 		break;
426cb8e0678SMitchell Horne 	case SCAUSE_STORE_PAGE_FAULT:
427cb8e0678SMitchell Horne 	case SCAUSE_LOAD_PAGE_FAULT:
428cb8e0678SMitchell Horne 	case SCAUSE_INST_PAGE_FAULT:
429da8944d9SJessica Clarke 		page_fault_handler(frame, 1);
43028029b68SRuslan Bukin 		break;
431cb8e0678SMitchell Horne 	case SCAUSE_ECALL_USER:
43228029b68SRuslan Bukin 		frame->tf_sepc += 4;	/* Next instruction */
4335319fa1bSEdward Tomasz Napierala 		ecall_handler();
43428029b68SRuslan Bukin 		break;
435cb8e0678SMitchell Horne 	case SCAUSE_ILLEGAL_INSTRUCTION:
4367804dd52SRuslan Bukin 		if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
4377804dd52SRuslan Bukin 			/*
4387804dd52SRuslan Bukin 			 * May be a FPE trap. Enable FPE usage
4397804dd52SRuslan Bukin 			 * for this thread and try again.
4407804dd52SRuslan Bukin 			 */
441232d0b87SJohn Baldwin 			fpe_state_clear();
442232d0b87SJohn Baldwin 			frame->tf_sstatus &= ~SSTATUS_FS_MASK;
443232d0b87SJohn Baldwin 			frame->tf_sstatus |= SSTATUS_FS_CLEAN;
4447804dd52SRuslan Bukin 			pcb->pcb_fpflags |= PCB_FP_STARTED;
4457804dd52SRuslan Bukin 			break;
4467804dd52SRuslan Bukin 		}
447c294dddbSJohn Baldwin 		call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc,
448c294dddbSJohn Baldwin 		    exception);
449e1b6d4a9SRuslan Bukin 		userret(td, frame);
450e1b6d4a9SRuslan Bukin 		break;
451cb8e0678SMitchell Horne 	case SCAUSE_BREAKPOINT:
452c294dddbSJohn Baldwin 		call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc,
453c294dddbSJohn Baldwin 		    exception);
454e1b6d4a9SRuslan Bukin 		userret(td, frame);
455e1b6d4a9SRuslan Bukin 		break;
45628029b68SRuslan Bukin 	default:
45728029b68SRuslan Bukin 		dump_regs(frame);
458ff79f35bSJohn Baldwin 		panic("Unknown userland exception %#lx, trap value %#lx",
45953941c0aSMark Johnston 		    exception, frame->tf_stval);
46028029b68SRuslan Bukin 	}
46128029b68SRuslan Bukin }
462