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 extern register_t fsu_intr_fault;
78 /* Called from exception.S */
79 void do_trap_supervisor(struct trapframe *);
80 void do_trap_user(struct trapframe *);
83 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
87 ksiginfo_init_trap(&ksi);
91 ksi.ksi_trapno = trapno;
96 cpu_fetch_syscall_args(struct thread *td)
100 struct syscall_args *sa;
106 ap = &td->td_frame->tf_a[0];
108 sa->code = td->td_frame->tf_t[0];
110 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
115 if (sa->code >= p->p_sysent->sv_size)
116 sa->callp = &p->p_sysent->sv_table[0];
118 sa->callp = &p->p_sysent->sv_table[sa->code];
120 sa->narg = sa->callp->sy_narg;
121 memcpy(sa->args, ap, nap * sizeof(register_t));
123 panic("TODO: Could we have more then %d args?", NARGREG);
125 td->td_retval[0] = 0;
126 td->td_retval[1] = 0;
131 #include "../../kern/subr_syscall.c"
134 dump_regs(struct trapframe *frame)
139 n = (sizeof(frame->tf_t) / sizeof(frame->tf_t[0]));
140 for (i = 0; i < n; i++)
141 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
143 n = (sizeof(frame->tf_s) / sizeof(frame->tf_s[0]));
144 for (i = 0; i < n; i++)
145 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
147 n = (sizeof(frame->tf_a) / sizeof(frame->tf_a[0]));
148 for (i = 0; i < n; i++)
149 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
151 printf("ra == 0x%016lx\n", frame->tf_ra);
152 printf("sp == 0x%016lx\n", frame->tf_sp);
153 printf("gp == 0x%016lx\n", frame->tf_gp);
154 printf("tp == 0x%016lx\n", frame->tf_tp);
156 printf("sepc == 0x%016lx\n", frame->tf_sepc);
157 printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
161 svc_handler(struct trapframe *frame)
166 td->td_frame = frame;
173 data_abort(struct trapframe *frame, int usermode)
182 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 map = &td->td_proc->p_vmspace->vm_map;
203 else if (stval >= VM_MAX_USER_ADDRESS)
206 if (pcb->pcb_onfault == 0)
208 map = &td->td_proc->p_vmspace->vm_map;
211 va = trunc_page(stval);
213 if ((frame->tf_scause == EXCP_FAULT_STORE) ||
214 (frame->tf_scause == EXCP_STORE_PAGE_FAULT)) {
215 ftype = VM_PROT_WRITE;
216 } else if (frame->tf_scause == EXCP_INST_PAGE_FAULT) {
217 ftype = VM_PROT_EXECUTE;
219 ftype = VM_PROT_READ;
222 if (pmap_fault_fixup(map->pmap, va, ftype))
225 error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
226 if (error != KERN_SUCCESS) {
228 call_trapsignal(td, sig, ucode, (void *)stval,
229 frame->tf_scause & EXCP_MASK);
231 if (pcb->pcb_onfault != 0) {
232 frame->tf_a[0] = error;
233 frame->tf_sepc = pcb->pcb_onfault;
247 panic("Fatal page fault at %#lx: %#016lx", frame->tf_sepc, stval);
251 do_trap_supervisor(struct trapframe *frame)
255 /* Ensure we came from supervisor mode, interrupts disabled */
256 KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) ==
257 SSTATUS_SPP, ("Came from S mode with interrupts enabled"));
259 exception = frame->tf_scause & EXCP_MASK;
260 if (frame->tf_scause & EXCP_INTR) {
262 riscv_cpu_intr(frame);
267 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
271 CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
272 curthread, frame->tf_sepc, frame);
275 case EXCP_FAULT_LOAD:
276 case EXCP_FAULT_STORE:
277 case EXCP_FAULT_FETCH:
278 case EXCP_STORE_PAGE_FAULT:
279 case EXCP_LOAD_PAGE_FAULT:
280 data_abort(frame, 0);
282 case EXCP_BREAKPOINT:
284 if (dtrace_invop_jump_addr != NULL &&
285 dtrace_invop_jump_addr(frame) == 0)
289 kdb_trap(exception, 0, frame);
292 panic("No debugger in kernel.\n");
295 case EXCP_ILLEGAL_INSTRUCTION:
297 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
301 panic("Unknown kernel exception %x trap value %lx\n",
302 exception, frame->tf_stval);
307 do_trap_user(struct trapframe *frame)
314 td->td_frame = frame;
317 /* Ensure we came from usermode, interrupts disabled */
318 KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
319 ("Came from U mode with interrupts enabled"));
321 exception = frame->tf_scause & EXCP_MASK;
322 if (frame->tf_scause & EXCP_INTR) {
324 riscv_cpu_intr(frame);
328 CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
329 curthread, frame->tf_sepc, frame);
332 case EXCP_FAULT_LOAD:
333 case EXCP_FAULT_STORE:
334 case EXCP_FAULT_FETCH:
335 case EXCP_STORE_PAGE_FAULT:
336 case EXCP_LOAD_PAGE_FAULT:
337 case EXCP_INST_PAGE_FAULT:
338 data_abort(frame, 1);
340 case EXCP_USER_ECALL:
341 frame->tf_sepc += 4; /* Next instruction */
344 case EXCP_ILLEGAL_INSTRUCTION:
346 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
348 * May be a FPE trap. Enable FPE usage
349 * for this thread and try again.
352 frame->tf_sstatus &= ~SSTATUS_FS_MASK;
353 frame->tf_sstatus |= SSTATUS_FS_CLEAN;
354 pcb->pcb_fpflags |= PCB_FP_STARTED;
358 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc,
362 case EXCP_BREAKPOINT:
363 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc,
369 panic("Unknown userland exception %x, trap value %lx\n",
370 exception, frame->tf_stval);