]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/trap.c
libevent: Import libevent 2.1.12
[FreeBSD/FreeBSD.git] / sys / riscv / riscv / trap.c
1 /*-
2  * Copyright (c) 2015-2018 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 #include "opt_ddb.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/ktr.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.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 #include <machine/fpe.h>
61 #include <machine/frame.h>
62 #include <machine/pcb.h>
63 #include <machine/pcpu.h>
64
65 #include <machine/resource.h>
66 #include <machine/intr.h>
67
68 #ifdef KDTRACE_HOOKS
69 #include <sys/dtrace_bsd.h>
70 #endif
71
72 #ifdef DDB
73 #include <ddb/ddb.h>
74 #include <ddb/db_sym.h>
75 #endif
76
77 int (*dtrace_invop_jump_addr)(struct trapframe *);
78
79 /* Called from exception.S */
80 void do_trap_supervisor(struct trapframe *);
81 void do_trap_user(struct trapframe *);
82
83 static __inline void
84 call_trapsignal(struct thread *td, int sig, int code, void *addr, int trapno)
85 {
86         ksiginfo_t ksi;
87
88         ksiginfo_init_trap(&ksi);
89         ksi.ksi_signo = sig;
90         ksi.ksi_code = code;
91         ksi.ksi_addr = addr;
92         ksi.ksi_trapno = trapno;
93         trapsignal(td, &ksi);
94 }
95
96 int
97 cpu_fetch_syscall_args(struct thread *td)
98 {
99         struct proc *p;
100         syscallarg_t *ap, *dst_ap;
101         struct syscall_args *sa;
102
103         p = td->td_proc;
104         sa = &td->td_sa;
105         ap = &td->td_frame->tf_a[0];
106         dst_ap = &sa->args[0];
107
108         sa->code = td->td_frame->tf_t[0];
109         sa->original_code = sa->code;
110
111         if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
112                 sa->code = *ap++;
113         } else {
114                 *dst_ap++ = *ap++;
115         }
116
117         if (__predict_false(sa->code >= p->p_sysent->sv_size))
118                 sa->callp = &nosys_sysent;
119         else
120                 sa->callp = &p->p_sysent->sv_table[sa->code];
121
122         KASSERT(sa->callp->sy_narg <= nitems(sa->args),
123             ("Syscall %d takes too many arguments", sa->code));
124
125         memcpy(dst_ap, ap, (NARGREG - 1) * sizeof(*dst_ap));
126
127         td->td_retval[0] = 0;
128         td->td_retval[1] = 0;
129
130         return (0);
131 }
132
133 #include "../../kern/subr_syscall.c"
134
135 static void
136 print_with_symbol(const char *name, uint64_t value)
137 {
138 #ifdef DDB
139         c_db_sym_t sym;
140         db_expr_t sym_value;
141         db_expr_t offset;
142         const char *sym_name;
143 #endif
144
145         printf("%7s: 0x%016lx", name, value);
146
147 #ifdef DDB
148         if (value >= VM_MIN_KERNEL_ADDRESS) {
149                 sym = db_search_symbol(value, DB_STGY_ANY, &offset);
150                 if (sym != C_DB_SYM_NULL) {
151                         db_symbol_values(sym, &sym_name, &sym_value);
152                         if (offset != 0)
153                                 printf(" (%s + 0x%lx)", sym_name, offset);
154                         else
155                                 printf(" (%s)", sym_name);
156                 }
157         }
158 #endif
159         printf("\n");
160 }
161
162 static void
163 dump_regs(struct trapframe *frame)
164 {
165         char name[6];
166         int i;
167
168         for (i = 0; i < nitems(frame->tf_t); i++) {
169                 snprintf(name, sizeof(name), "t[%d]", i);
170                 print_with_symbol(name, frame->tf_t[i]);
171         }
172
173         for (i = 0; i < nitems(frame->tf_s); i++) {
174                 snprintf(name, sizeof(name), "s[%d]", i);
175                 print_with_symbol(name, frame->tf_s[i]);
176         }
177
178         for (i = 0; i < nitems(frame->tf_a); i++) {
179                 snprintf(name, sizeof(name), "a[%d]", i);
180                 print_with_symbol(name, frame->tf_a[i]);
181         }
182
183         print_with_symbol("ra", frame->tf_ra);
184         print_with_symbol("sp", frame->tf_sp);
185         print_with_symbol("gp", frame->tf_gp);
186         print_with_symbol("tp", frame->tf_tp);
187         print_with_symbol("sepc", frame->tf_sepc);
188         printf("sstatus: 0x%016lx\n", frame->tf_sstatus);
189         printf("stval  : 0x%016lx\n", frame->tf_stval);
190 }
191
192 static void
193 ecall_handler(void)
194 {
195         struct thread *td;
196
197         td = curthread;
198
199         syscallenter(td);
200         syscallret(td);
201 }
202
203 static void
204 page_fault_handler(struct trapframe *frame, int usermode)
205 {
206         struct vm_map *map;
207         uint64_t stval;
208         struct thread *td;
209         struct pcb *pcb;
210         vm_prot_t ftype;
211         vm_offset_t va;
212         struct proc *p;
213         int error, sig, ucode;
214 #ifdef KDB
215         bool handled;
216 #endif
217
218 #ifdef KDB
219         if (kdb_active) {
220                 kdb_reenter();
221                 return;
222         }
223 #endif
224
225         td = curthread;
226         p = td->td_proc;
227         pcb = td->td_pcb;
228         stval = frame->tf_stval;
229
230         if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
231             WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
232             "Kernel page fault") != 0)
233                 goto fatal;
234
235         if (usermode) {
236                 if (!VIRT_IS_VALID(stval)) {
237                         call_trapsignal(td, SIGSEGV, SEGV_MAPERR, (void *)stval,
238                             frame->tf_scause & SCAUSE_CODE);
239                         goto done;
240                 }
241                 map = &p->p_vmspace->vm_map;
242         } else {
243                 /*
244                  * Enable interrupts for the duration of the page fault. For
245                  * user faults this was done already in do_trap_user().
246                  */
247                 intr_enable();
248
249                 if (stval >= VM_MIN_KERNEL_ADDRESS) {
250                         map = kernel_map;
251                 } else {
252                         if (pcb->pcb_onfault == 0)
253                                 goto fatal;
254                         map = &p->p_vmspace->vm_map;
255                 }
256         }
257
258         va = trunc_page(stval);
259
260         if (frame->tf_scause == SCAUSE_STORE_PAGE_FAULT) {
261                 ftype = VM_PROT_WRITE;
262         } else if (frame->tf_scause == SCAUSE_INST_PAGE_FAULT) {
263                 ftype = VM_PROT_EXECUTE;
264         } else {
265                 ftype = VM_PROT_READ;
266         }
267
268         if (VIRT_IS_VALID(va) && pmap_fault(map->pmap, va, ftype))
269                 goto done;
270
271         error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
272         if (error != KERN_SUCCESS) {
273                 if (usermode) {
274                         call_trapsignal(td, sig, ucode, (void *)stval,
275                             frame->tf_scause & SCAUSE_CODE);
276                 } else {
277                         if (pcb->pcb_onfault != 0) {
278                                 frame->tf_a[0] = error;
279                                 frame->tf_sepc = pcb->pcb_onfault;
280                                 return;
281                         }
282                         goto fatal;
283                 }
284         }
285
286 done:
287         if (usermode)
288                 userret(td, frame);
289         return;
290
291 fatal:
292         dump_regs(frame);
293 #ifdef KDB
294         if (debugger_on_trap) {
295                 kdb_why = KDB_WHY_TRAP;
296                 handled = kdb_trap(frame->tf_scause & SCAUSE_CODE, 0, frame);
297                 kdb_why = KDB_WHY_UNSET;
298                 if (handled)
299                         return;
300         }
301 #endif
302         panic("Fatal page fault at %#lx: %#lx", frame->tf_sepc, stval);
303 }
304
305 void
306 do_trap_supervisor(struct trapframe *frame)
307 {
308         uint64_t exception;
309
310         /* Ensure we came from supervisor mode, interrupts disabled */
311         KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) ==
312             SSTATUS_SPP, ("Came from S mode with interrupts enabled"));
313
314         KASSERT((csr_read(sstatus) & (SSTATUS_SUM)) == 0,
315             ("Came from S mode with SUM enabled"));
316
317         exception = frame->tf_scause & SCAUSE_CODE;
318         if ((frame->tf_scause & SCAUSE_INTR) != 0) {
319                 /* Interrupt */
320                 riscv_cpu_intr(frame);
321                 return;
322         }
323
324 #ifdef KDTRACE_HOOKS
325         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
326                 return;
327 #endif
328
329         CTR4(KTR_TRAP, "%s: exception=%lu, sepc=%#lx, stval=%#lx", __func__,
330             exception, frame->tf_sepc, frame->tf_stval);
331
332         switch (exception) {
333         case SCAUSE_LOAD_ACCESS_FAULT:
334         case SCAUSE_STORE_ACCESS_FAULT:
335         case SCAUSE_INST_ACCESS_FAULT:
336                 dump_regs(frame);
337                 panic("Memory access exception at %#lx: %#lx",
338                     frame->tf_sepc, frame->tf_stval);
339                 break;
340         case SCAUSE_LOAD_MISALIGNED:
341         case SCAUSE_STORE_MISALIGNED:
342         case SCAUSE_INST_MISALIGNED:
343                 dump_regs(frame);
344                 panic("Misaligned address exception at %#lx: %#lx",
345                     frame->tf_sepc, frame->tf_stval);
346                 break;
347         case SCAUSE_STORE_PAGE_FAULT:
348         case SCAUSE_LOAD_PAGE_FAULT:
349         case SCAUSE_INST_PAGE_FAULT:
350                 page_fault_handler(frame, 0);
351                 break;
352         case SCAUSE_BREAKPOINT:
353 #ifdef KDTRACE_HOOKS
354                 if (dtrace_invop_jump_addr != NULL &&
355                     dtrace_invop_jump_addr(frame) == 0)
356                                 break;
357 #endif
358 #ifdef KDB
359                 kdb_trap(exception, 0, frame);
360 #else
361                 dump_regs(frame);
362                 panic("No debugger in kernel.");
363 #endif
364                 break;
365         case SCAUSE_ILLEGAL_INSTRUCTION:
366                 dump_regs(frame);
367                 panic("Illegal instruction 0x%0*lx at %#lx",
368                     (frame->tf_stval & 0x3) != 0x3 ? 4 : 8,
369                     frame->tf_stval, frame->tf_sepc);
370                 break;
371         default:
372                 dump_regs(frame);
373                 panic("Unknown kernel exception %#lx trap value %#lx",
374                     exception, frame->tf_stval);
375         }
376 }
377
378 void
379 do_trap_user(struct trapframe *frame)
380 {
381         uint64_t exception;
382         struct thread *td;
383         struct pcb *pcb;
384
385         td = curthread;
386         pcb = td->td_pcb;
387
388         KASSERT(td->td_frame == frame,
389             ("%s: td_frame %p != frame %p", __func__, td->td_frame, frame));
390
391         /* Ensure we came from usermode, interrupts disabled */
392         KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
393             ("Came from U mode with interrupts enabled"));
394
395         KASSERT((csr_read(sstatus) & (SSTATUS_SUM)) == 0,
396             ("Came from U mode with SUM enabled"));
397
398         exception = frame->tf_scause & SCAUSE_CODE;
399         if ((frame->tf_scause & SCAUSE_INTR) != 0) {
400                 /* Interrupt */
401                 riscv_cpu_intr(frame);
402                 return;
403         }
404         intr_enable();
405
406         CTR4(KTR_TRAP, "%s: exception=%lu, sepc=%#lx, stval=%#lx", __func__,
407             exception, frame->tf_sepc, frame->tf_stval);
408
409         switch (exception) {
410         case SCAUSE_LOAD_ACCESS_FAULT:
411         case SCAUSE_STORE_ACCESS_FAULT:
412         case SCAUSE_INST_ACCESS_FAULT:
413                 call_trapsignal(td, SIGBUS, BUS_ADRERR, (void *)frame->tf_sepc,
414                     exception);
415                 userret(td, frame);
416                 break;
417         case SCAUSE_LOAD_MISALIGNED:
418         case SCAUSE_STORE_MISALIGNED:
419         case SCAUSE_INST_MISALIGNED:
420                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sepc,
421                     exception);
422                 userret(td, frame);
423                 break;
424         case SCAUSE_STORE_PAGE_FAULT:
425         case SCAUSE_LOAD_PAGE_FAULT:
426         case SCAUSE_INST_PAGE_FAULT:
427                 page_fault_handler(frame, 1);
428                 break;
429         case SCAUSE_ECALL_USER:
430                 frame->tf_sepc += 4;    /* Next instruction */
431                 ecall_handler();
432                 break;
433         case SCAUSE_ILLEGAL_INSTRUCTION:
434                 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
435                         /*
436                          * May be a FPE trap. Enable FPE usage
437                          * for this thread and try again.
438                          */
439                         fpe_state_clear();
440                         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
441                         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
442                         pcb->pcb_fpflags |= PCB_FP_STARTED;
443                         break;
444                 }
445                 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc,
446                     exception);
447                 userret(td, frame);
448                 break;
449         case SCAUSE_BREAKPOINT:
450                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc,
451                     exception);
452                 userret(td, frame);
453                 break;
454         default:
455                 dump_regs(frame);
456                 panic("Unknown userland exception %#lx, trap value %#lx",
457                     exception, frame->tf_stval);
458         }
459 }