xref: /freebsd/sys/riscv/riscv/trap.c (revision 2a01feab)
1 /*-
2  * Copyright (c) 2015-2017 Ruslan Bukin <br@bsdpad.com>
3  * All rights reserved.
4  *
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.
8  *
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).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/pioctl.h>
44 #include <sys/bus.h>
45 #include <sys/proc.h>
46 #include <sys/ptrace.h>
47 #include <sys/syscall.h>
48 #include <sys/sysent.h>
49 #ifdef KDB
50 #include <sys/kdb.h>
51 #endif
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.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>
59 
60 #ifdef FPE
61 #include <machine/fpe.h>
62 #endif
63 #include <machine/frame.h>
64 #include <machine/pcb.h>
65 #include <machine/pcpu.h>
66 
67 #include <machine/resource.h>
68 #include <machine/intr.h>
69 
70 #ifdef KDTRACE_HOOKS
71 #include <sys/dtrace_bsd.h>
72 #endif
73 
74 int (*dtrace_invop_jump_addr)(struct trapframe *);
75 
76 extern register_t fsu_intr_fault;
77 
78 /* Called from exception.S */
79 void do_trap_supervisor(struct trapframe *);
80 void do_trap_user(struct trapframe *);
81 
82 static __inline void
83 call_trapsignal(struct thread *td, int sig, int code, void *addr)
84 {
85 	ksiginfo_t ksi;
86 
87 	ksiginfo_init_trap(&ksi);
88 	ksi.ksi_signo = sig;
89 	ksi.ksi_code = code;
90 	ksi.ksi_addr = addr;
91 	trapsignal(td, &ksi);
92 }
93 
94 int
95 cpu_fetch_syscall_args(struct thread *td)
96 {
97 	struct proc *p;
98 	register_t *ap;
99 	struct syscall_args *sa;
100 	int nap;
101 
102 	nap = NARGREG;
103 	p = td->td_proc;
104 	sa = &td->td_sa;
105 	ap = &td->td_frame->tf_a[0];
106 
107 	sa->code = td->td_frame->tf_t[0];
108 
109 	if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
110 		sa->code = *ap++;
111 		nap--;
112 	}
113 
114 	if (p->p_sysent->sv_mask)
115 		sa->code &= p->p_sysent->sv_mask;
116 	if (sa->code >= p->p_sysent->sv_size)
117 		sa->callp = &p->p_sysent->sv_table[0];
118 	else
119 		sa->callp = &p->p_sysent->sv_table[sa->code];
120 
121 	sa->narg = sa->callp->sy_narg;
122 	memcpy(sa->args, ap, nap * sizeof(register_t));
123 	if (sa->narg > nap)
124 		panic("TODO: Could we have more then %d args?", NARGREG);
125 
126 	td->td_retval[0] = 0;
127 	td->td_retval[1] = 0;
128 
129 	return (0);
130 }
131 
132 #include "../../kern/subr_syscall.c"
133 
134 static void
135 dump_regs(struct trapframe *frame)
136 {
137 	int n;
138 	int i;
139 
140 	n = (sizeof(frame->tf_t) / sizeof(frame->tf_t[0]));
141 	for (i = 0; i < n; i++)
142 		printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
143 
144 	n = (sizeof(frame->tf_s) / sizeof(frame->tf_s[0]));
145 	for (i = 0; i < n; i++)
146 		printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
147 
148 	n = (sizeof(frame->tf_a) / sizeof(frame->tf_a[0]));
149 	for (i = 0; i < n; i++)
150 		printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
151 
152 	printf("sepc == 0x%016lx\n", frame->tf_sepc);
153 	printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
154 }
155 
156 static void
157 svc_handler(struct trapframe *frame)
158 {
159 	struct thread *td;
160 	int error;
161 
162 	td = curthread;
163 	td->td_frame = frame;
164 
165 	error = syscallenter(td);
166 	syscallret(td, error);
167 }
168 
169 static void
170 data_abort(struct trapframe *frame, int lower)
171 {
172 	struct vm_map *map;
173 	uint64_t sbadaddr;
174 	struct thread *td;
175 	struct pcb *pcb;
176 	vm_prot_t ftype;
177 	vm_offset_t va;
178 	struct proc *p;
179 	int ucode;
180 	int error;
181 	int sig;
182 
183 #ifdef KDB
184 	if (kdb_active) {
185 		kdb_reenter();
186 		return;
187 	}
188 #endif
189 
190 	td = curthread;
191 	pcb = td->td_pcb;
192 	sbadaddr = frame->tf_sbadaddr;
193 
194 	p = td->td_proc;
195 
196 	if (lower)
197 		map = &td->td_proc->p_vmspace->vm_map;
198 	else {
199 		/* The top bit tells us which range to use */
200 		if ((sbadaddr >> 63) == 1)
201 			map = kernel_map;
202 		else
203 			map = &td->td_proc->p_vmspace->vm_map;
204 	}
205 
206 	va = trunc_page(sbadaddr);
207 
208 	if ((frame->tf_scause == EXCP_FAULT_STORE) ||
209 	    (frame->tf_scause == EXCP_STORE_PAGE_FAULT)) {
210 		ftype = (VM_PROT_READ | VM_PROT_WRITE);
211 	} else {
212 		ftype = (VM_PROT_READ);
213 	}
214 
215 	if (map != kernel_map) {
216 		/*
217 		 * Keep swapout from messing with us during this
218 		 *	critical time.
219 		 */
220 		PROC_LOCK(p);
221 		++p->p_lock;
222 		PROC_UNLOCK(p);
223 
224 		/* Fault in the user page: */
225 		error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
226 
227 		PROC_LOCK(p);
228 		--p->p_lock;
229 		PROC_UNLOCK(p);
230 	} else {
231 		/*
232 		 * Don't have to worry about process locking or stacks in the
233 		 * kernel.
234 		 */
235 		error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
236 	}
237 
238 	if (error != KERN_SUCCESS) {
239 		if (lower) {
240 			sig = SIGSEGV;
241 			if (error == KERN_PROTECTION_FAILURE)
242 				ucode = SEGV_ACCERR;
243 			else
244 				ucode = SEGV_MAPERR;
245 			call_trapsignal(td, sig, ucode, (void *)sbadaddr);
246 		} else {
247 			if (td->td_intr_nesting_level == 0 &&
248 			    pcb->pcb_onfault != 0) {
249 				frame->tf_a[0] = error;
250 				frame->tf_sepc = pcb->pcb_onfault;
251 				return;
252 			}
253 			dump_regs(frame);
254 			panic("vm_fault failed: %lx, va 0x%016lx",
255 				frame->tf_sepc, sbadaddr);
256 		}
257 	}
258 
259 	if (lower)
260 		userret(td, frame);
261 }
262 
263 void
264 do_trap_supervisor(struct trapframe *frame)
265 {
266 	uint64_t exception;
267 	uint64_t sstatus;
268 
269 	/* Ensure we came from supervisor mode, interrupts disabled */
270 	__asm __volatile("csrr %0, sstatus" : "=&r" (sstatus));
271 	KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == SSTATUS_SPP,
272 			("We must came from S mode with interrupts disabled"));
273 
274 	exception = (frame->tf_scause & EXCP_MASK);
275 	if (frame->tf_scause & EXCP_INTR) {
276 		/* Interrupt */
277 		riscv_cpu_intr(frame);
278 		return;
279 	}
280 
281 #ifdef KDTRACE_HOOKS
282 	if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
283 		return;
284 #endif
285 
286 	CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
287 	    curthread, frame->tf_sepc, frame);
288 
289 	switch(exception) {
290 	case EXCP_FAULT_LOAD:
291 	case EXCP_FAULT_STORE:
292 	case EXCP_FAULT_FETCH:
293 	case EXCP_STORE_PAGE_FAULT:
294 	case EXCP_LOAD_PAGE_FAULT:
295 		data_abort(frame, 0);
296 		break;
297 	case EXCP_BREAKPOINT:
298 #ifdef KDTRACE_HOOKS
299 		if (dtrace_invop_jump_addr != 0) {
300 			dtrace_invop_jump_addr(frame);
301 			break;
302 		}
303 #endif
304 #ifdef KDB
305 		kdb_trap(exception, 0, frame);
306 #else
307 		dump_regs(frame);
308 		panic("No debugger in kernel.\n");
309 #endif
310 		break;
311 	case EXCP_ILLEGAL_INSTRUCTION:
312 		dump_regs(frame);
313 		panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
314 		break;
315 	default:
316 		dump_regs(frame);
317 		panic("Unknown kernel exception %x badaddr %lx\n",
318 			exception, frame->tf_sbadaddr);
319 	}
320 }
321 
322 void
323 do_trap_user(struct trapframe *frame)
324 {
325 	uint64_t exception;
326 	struct thread *td;
327 	uint64_t sstatus;
328 	struct pcb *pcb;
329 
330 	td = curthread;
331 	td->td_frame = frame;
332 	pcb = td->td_pcb;
333 
334 	/* Ensure we came from usermode, interrupts disabled */
335 	__asm __volatile("csrr %0, sstatus" : "=&r" (sstatus));
336 	KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
337 			("We must came from U mode with interrupts disabled"));
338 
339 	exception = (frame->tf_scause & EXCP_MASK);
340 	if (frame->tf_scause & EXCP_INTR) {
341 		/* Interrupt */
342 		riscv_cpu_intr(frame);
343 		return;
344 	}
345 
346 	CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
347 	    curthread, frame->tf_sepc, frame);
348 
349 	switch(exception) {
350 	case EXCP_FAULT_LOAD:
351 	case EXCP_FAULT_STORE:
352 	case EXCP_FAULT_FETCH:
353 	case EXCP_STORE_PAGE_FAULT:
354 	case EXCP_LOAD_PAGE_FAULT:
355 	case EXCP_INST_PAGE_FAULT:
356 		data_abort(frame, 1);
357 		break;
358 	case EXCP_USER_ECALL:
359 		frame->tf_sepc += 4;	/* Next instruction */
360 		svc_handler(frame);
361 		break;
362 	case EXCP_ILLEGAL_INSTRUCTION:
363 #ifdef FPE
364 		if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
365 			/*
366 			 * May be a FPE trap. Enable FPE usage
367 			 * for this thread and try again.
368 			 */
369 			fpe_state_clear();
370 			frame->tf_sstatus &= ~SSTATUS_FS_MASK;
371 			frame->tf_sstatus |= SSTATUS_FS_CLEAN;
372 			pcb->pcb_fpflags |= PCB_FP_STARTED;
373 			break;
374 		}
375 #endif
376 		call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc);
377 		userret(td, frame);
378 		break;
379 	case EXCP_BREAKPOINT:
380 		call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc);
381 		userret(td, frame);
382 		break;
383 	default:
384 		dump_regs(frame);
385 		panic("Unknown userland exception %x, badaddr %lx\n",
386 			exception, frame->tf_sbadaddr);
387 	}
388 }
389