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