2 * Copyright (c) 2015-2018 Ruslan Bukin <br@bsdpad.com>
5 * Portions of this software were developed by SRI International and the
6 * University of Cambridge Computer Laboratory under DARPA/AFRL contract
7 * FA8750-10-C-0237 ("CTSRD"), as part of the DARPA CRASH research programme.
9 * Portions of this software were developed by the University of Cambridge
10 * Computer Laboratory as part of the CTSRD Project, with support from the
11 * UK Higher Education Innovation Fund (HEIF).
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
43 #include <sys/mutex.h>
46 #include <sys/ptrace.h>
47 #include <sys/syscall.h>
48 #include <sys/sysent.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_param.h>
58 #include <vm/vm_extern.h>
61 #include <machine/fpe.h>
63 #include <machine/frame.h>
64 #include <machine/pcb.h>
65 #include <machine/pcpu.h>
67 #include <machine/resource.h>
68 #include <machine/intr.h>
71 #include <sys/dtrace_bsd.h>
74 int (*dtrace_invop_jump_addr)(struct trapframe *);
76 /* Called from exception.S */
77 void do_trap_supervisor(struct trapframe *);
78 void do_trap_user(struct trapframe *);
81 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
85 ksiginfo_init_trap(&ksi);
89 ksi.ksi_trapno = trapno;
94 cpu_fetch_syscall_args(struct thread *td)
97 register_t *ap, *dst_ap;
98 struct syscall_args *sa;
102 ap = &td->td_frame->tf_a[0];
103 dst_ap = &sa->args[0];
105 sa->code = td->td_frame->tf_t[0];
107 if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
113 if (__predict_false(sa->code >= p->p_sysent->sv_size))
114 sa->callp = &p->p_sysent->sv_table[0];
116 sa->callp = &p->p_sysent->sv_table[sa->code];
118 KASSERT(sa->callp->sy_narg <= nitems(sa->args),
119 ("Syscall %d takes too many arguments", sa->code));
121 memcpy(dst_ap, ap, (NARGREG - 1) * sizeof(register_t));
123 td->td_retval[0] = 0;
124 td->td_retval[1] = 0;
129 #include "../../kern/subr_syscall.c"
132 dump_regs(struct trapframe *frame)
137 n = nitems(frame->tf_t);
138 for (i = 0; i < n; i++)
139 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
141 n = nitems(frame->tf_s);
142 for (i = 0; i < n; i++)
143 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
145 n = nitems(frame->tf_a);
146 for (i = 0; i < n; i++)
147 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
149 printf("ra == 0x%016lx\n", frame->tf_ra);
150 printf("sp == 0x%016lx\n", frame->tf_sp);
151 printf("gp == 0x%016lx\n", frame->tf_gp);
152 printf("tp == 0x%016lx\n", frame->tf_tp);
154 printf("sepc == 0x%016lx\n", frame->tf_sepc);
155 printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
170 page_fault_handler(struct trapframe *frame, int usermode)
179 int error, sig, ucode;
194 stval = frame->tf_stval;
196 if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
197 WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
198 "Kernel page fault") != 0)
202 if (!VIRT_IS_VALID(stval)) {
203 call_trapsignal(td, SIGSEGV, SEGV_MAPERR, (void *)stval,
204 frame->tf_scause & SCAUSE_CODE);
207 map = &td->td_proc->p_vmspace->vm_map;
210 * Enable interrupts for the duration of the page fault. For
211 * user faults this was done already in do_trap_user().
215 if (!VIRT_IS_VALID(stval))
218 if (stval >= VM_MAX_USER_ADDRESS) {
221 if (pcb->pcb_onfault == 0)
223 map = &td->td_proc->p_vmspace->vm_map;
227 va = trunc_page(stval);
229 if (frame->tf_scause == SCAUSE_STORE_PAGE_FAULT) {
230 ftype = VM_PROT_WRITE;
231 } else if (frame->tf_scause == SCAUSE_INST_PAGE_FAULT) {
232 ftype = VM_PROT_EXECUTE;
234 ftype = VM_PROT_READ;
237 if (pmap_fault(map->pmap, va, ftype))
240 error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
241 if (error != KERN_SUCCESS) {
243 call_trapsignal(td, sig, ucode, (void *)stval,
244 frame->tf_scause & SCAUSE_CODE);
246 if (pcb->pcb_onfault != 0) {
247 frame->tf_a[0] = error;
248 frame->tf_sepc = pcb->pcb_onfault;
263 if (debugger_on_trap) {
264 kdb_why = KDB_WHY_TRAP;
265 handled = kdb_trap(frame->tf_scause & SCAUSE_CODE, 0, frame);
266 kdb_why = KDB_WHY_UNSET;
271 panic("Fatal page fault at %#lx: %#016lx", frame->tf_sepc, stval);
275 do_trap_supervisor(struct trapframe *frame)
279 /* Ensure we came from supervisor mode, interrupts disabled */
280 KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) ==
281 SSTATUS_SPP, ("Came from S mode with interrupts enabled"));
283 KASSERT((csr_read(sstatus) & (SSTATUS_SUM)) == 0,
284 ("Came from S mode with SUM enabled"));
286 exception = frame->tf_scause & SCAUSE_CODE;
287 if ((frame->tf_scause & SCAUSE_INTR) != 0) {
289 riscv_cpu_intr(frame);
294 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
298 CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
299 curthread, frame->tf_sepc, frame);
302 case SCAUSE_LOAD_ACCESS_FAULT:
303 case SCAUSE_STORE_ACCESS_FAULT:
304 case SCAUSE_INST_ACCESS_FAULT:
306 panic("Memory access exception at 0x%016lx\n", frame->tf_sepc);
308 case SCAUSE_STORE_PAGE_FAULT:
309 case SCAUSE_LOAD_PAGE_FAULT:
310 case SCAUSE_INST_PAGE_FAULT:
311 page_fault_handler(frame, 0);
313 case SCAUSE_BREAKPOINT:
315 if (dtrace_invop_jump_addr != NULL &&
316 dtrace_invop_jump_addr(frame) == 0)
320 kdb_trap(exception, 0, frame);
323 panic("No debugger in kernel.\n");
326 case SCAUSE_ILLEGAL_INSTRUCTION:
328 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
332 panic("Unknown kernel exception %lx trap value %lx\n",
333 exception, frame->tf_stval);
338 do_trap_user(struct trapframe *frame)
347 KASSERT(td->td_frame == frame,
348 ("%s: td_frame %p != frame %p", __func__, td->td_frame, frame));
350 /* Ensure we came from usermode, interrupts disabled */
351 KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
352 ("Came from U mode with interrupts enabled"));
354 KASSERT((csr_read(sstatus) & (SSTATUS_SUM)) == 0,
355 ("Came from U mode with SUM enabled"));
357 exception = frame->tf_scause & SCAUSE_CODE;
358 if ((frame->tf_scause & SCAUSE_INTR) != 0) {
360 riscv_cpu_intr(frame);
365 CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
366 curthread, frame->tf_sepc, frame);
369 case SCAUSE_LOAD_ACCESS_FAULT:
370 case SCAUSE_STORE_ACCESS_FAULT:
371 case SCAUSE_INST_ACCESS_FAULT:
372 call_trapsignal(td, SIGBUS, BUS_ADRERR, (void *)frame->tf_sepc,
376 case SCAUSE_STORE_PAGE_FAULT:
377 case SCAUSE_LOAD_PAGE_FAULT:
378 case SCAUSE_INST_PAGE_FAULT:
379 page_fault_handler(frame, 1);
381 case SCAUSE_ECALL_USER:
382 frame->tf_sepc += 4; /* Next instruction */
385 case SCAUSE_ILLEGAL_INSTRUCTION:
387 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
389 * May be a FPE trap. Enable FPE usage
390 * for this thread and try again.
393 frame->tf_sstatus &= ~SSTATUS_FS_MASK;
394 frame->tf_sstatus |= SSTATUS_FS_CLEAN;
395 pcb->pcb_fpflags |= PCB_FP_STARTED;
399 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc,
403 case SCAUSE_BREAKPOINT:
404 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc,
410 panic("Unknown userland exception %lx, trap value %lx\n",
411 exception, frame->tf_stval);