2 * Copyright (c) 2015-2016 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>
42 #include <sys/mutex.h>
43 #include <sys/pioctl.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>
60 #include <machine/frame.h>
61 #include <machine/pcb.h>
62 #include <machine/pcpu.h>
64 #include <machine/resource.h>
65 #include <machine/intr.h>
68 #include <sys/dtrace_bsd.h>
71 int (*dtrace_invop_jump_addr)(struct trapframe *);
73 extern register_t fsu_intr_fault;
75 /* Called from exception.S */
76 void do_trap_supervisor(struct trapframe *);
77 void do_trap_user(struct trapframe *);
80 call_trapsignal(struct thread *td, int sig, int code, void *addr)
84 ksiginfo_init_trap(&ksi);
92 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
100 ap = &td->td_frame->tf_a[0];
102 sa->code = td->td_frame->tf_t[0];
104 if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
109 if (p->p_sysent->sv_mask)
110 sa->code &= p->p_sysent->sv_mask;
111 if (sa->code >= p->p_sysent->sv_size)
112 sa->callp = &p->p_sysent->sv_table[0];
114 sa->callp = &p->p_sysent->sv_table[sa->code];
116 sa->narg = sa->callp->sy_narg;
117 memcpy(sa->args, ap, nap * sizeof(register_t));
119 panic("TODO: Could we have more then %d args?", NARGREG);
121 td->td_retval[0] = 0;
122 td->td_retval[1] = 0;
127 #include "../../kern/subr_syscall.c"
130 dump_regs(struct trapframe *frame)
135 n = (sizeof(frame->tf_t) / sizeof(frame->tf_t[0]));
136 for (i = 0; i < n; i++)
137 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
139 n = (sizeof(frame->tf_s) / sizeof(frame->tf_s[0]));
140 for (i = 0; i < n; i++)
141 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
143 n = (sizeof(frame->tf_a) / sizeof(frame->tf_a[0]));
144 for (i = 0; i < n; i++)
145 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
147 printf("sepc == 0x%016lx\n", frame->tf_sepc);
148 printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
152 svc_handler(struct trapframe *frame)
154 struct syscall_args sa;
159 td->td_frame = frame;
161 error = syscallenter(td, &sa);
162 syscallret(td, error, &sa);
166 data_abort(struct trapframe *frame, int lower)
190 * Special case for fuswintr and suswintr. These can't sleep so
191 * handle them early on in the trap handler.
193 if (__predict_false(pcb->pcb_onfault == (vm_offset_t)&fsu_intr_fault)) {
194 frame->tf_sepc = pcb->pcb_onfault;
198 sbadaddr = frame->tf_sbadaddr;
203 map = &td->td_proc->p_vmspace->vm_map;
205 /* The top bit tells us which range to use */
206 if ((sbadaddr >> 63) == 1)
209 map = &td->td_proc->p_vmspace->vm_map;
212 va = trunc_page(sbadaddr);
214 if (frame->tf_scause == EXCP_FAULT_STORE) {
215 ftype = (VM_PROT_READ | VM_PROT_WRITE);
217 ftype = (VM_PROT_READ);
220 if (map != kernel_map) {
222 * Keep swapout from messing with us during this
229 /* Fault in the user page: */
230 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
237 * Don't have to worry about process locking or stacks in the
240 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
243 if (error != KERN_SUCCESS) {
246 if (error == KERN_PROTECTION_FAILURE)
250 call_trapsignal(td, sig, ucode, (void *)sbadaddr);
252 if (td->td_intr_nesting_level == 0 &&
253 pcb->pcb_onfault != 0) {
254 frame->tf_a[0] = error;
255 frame->tf_sepc = pcb->pcb_onfault;
259 panic("vm_fault failed: %lx, va 0x%016lx",
260 frame->tf_sepc, sbadaddr);
269 do_trap_supervisor(struct trapframe *frame)
274 /* Ensure we came from supervisor mode, interrupts disabled */
275 __asm __volatile("csrr %0, sstatus" : "=&r" (sstatus));
276 KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == SSTATUS_SPP,
277 ("We must came from S mode with interrupts disabled"));
279 exception = (frame->tf_scause & EXCP_MASK);
280 if (frame->tf_scause & EXCP_INTR) {
282 riscv_cpu_intr(frame);
287 if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
291 CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
292 curthread, frame->tf_sepc, frame);
295 case EXCP_FAULT_LOAD:
296 case EXCP_FAULT_STORE:
297 case EXCP_FAULT_FETCH:
298 data_abort(frame, 0);
300 case EXCP_BREAKPOINT:
302 if (dtrace_invop_jump_addr != 0) {
303 dtrace_invop_jump_addr(frame);
308 kdb_trap(exception, 0, frame);
311 panic("No debugger in kernel.\n");
314 case EXCP_ILLEGAL_INSTRUCTION:
316 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
320 panic("Unknown kernel exception %x badaddr %lx\n",
321 exception, frame->tf_sbadaddr);
326 do_trap_user(struct trapframe *frame)
334 td->td_frame = frame;
337 /* Ensure we came from usermode, interrupts disabled */
338 __asm __volatile("csrr %0, sstatus" : "=&r" (sstatus));
339 KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
340 ("We must came from U mode with interrupts disabled"));
342 exception = (frame->tf_scause & EXCP_MASK);
343 if (frame->tf_scause & EXCP_INTR) {
345 riscv_cpu_intr(frame);
349 CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
350 curthread, frame->tf_sepc, frame);
353 case EXCP_FAULT_LOAD:
354 case EXCP_FAULT_STORE:
355 case EXCP_FAULT_FETCH:
356 data_abort(frame, 1);
358 case EXCP_USER_ECALL:
359 frame->tf_sepc += 4; /* Next instruction */
362 case EXCP_ILLEGAL_INSTRUCTION:
364 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
366 * May be a FPE trap. Enable FPE usage
367 * for this thread and try again.
369 frame->tf_sstatus |= SSTATUS_FS_INITIAL;
370 pcb->pcb_fpflags |= PCB_FP_STARTED;
374 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc);
377 case EXCP_BREAKPOINT:
378 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc);
383 panic("Unknown userland exception %x badaddr %lx\n",
384 exception, frame->tf_sbadaddr);