]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/trap.c
MFV r348555: 9690 metaslab of vdev with no space maps was flushed during removal
[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 <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/pioctl.h>
45 #include <sys/bus.h>
46 #include <sys/proc.h>
47 #include <sys/ptrace.h>
48 #include <sys/syscall.h>
49 #include <sys/sysent.h>
50 #ifdef KDB
51 #include <sys/kdb.h>
52 #endif
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_param.h>
59 #include <vm/vm_extern.h>
60
61 #ifdef FPE
62 #include <machine/fpe.h>
63 #endif
64 #include <machine/frame.h>
65 #include <machine/pcb.h>
66 #include <machine/pcpu.h>
67
68 #include <machine/resource.h>
69 #include <machine/intr.h>
70
71 #ifdef KDTRACE_HOOKS
72 #include <sys/dtrace_bsd.h>
73 #endif
74
75 int (*dtrace_invop_jump_addr)(struct trapframe *);
76
77 extern register_t fsu_intr_fault;
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)
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         trapsignal(td, &ksi);
93 }
94
95 int
96 cpu_fetch_syscall_args(struct thread *td)
97 {
98         struct proc *p;
99         register_t *ap;
100         struct syscall_args *sa;
101         int nap;
102
103         nap = NARGREG;
104         p = td->td_proc;
105         sa = &td->td_sa;
106         ap = &td->td_frame->tf_a[0];
107
108         sa->code = td->td_frame->tf_t[0];
109
110         if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
111                 sa->code = *ap++;
112                 nap--;
113         }
114
115         if (sa->code >= p->p_sysent->sv_size)
116                 sa->callp = &p->p_sysent->sv_table[0];
117         else
118                 sa->callp = &p->p_sysent->sv_table[sa->code];
119
120         sa->narg = sa->callp->sy_narg;
121         memcpy(sa->args, ap, nap * sizeof(register_t));
122         if (sa->narg > nap)
123                 panic("TODO: Could we have more then %d args?", NARGREG);
124
125         td->td_retval[0] = 0;
126         td->td_retval[1] = 0;
127
128         return (0);
129 }
130
131 #include "../../kern/subr_syscall.c"
132
133 static void
134 dump_regs(struct trapframe *frame)
135 {
136         int n;
137         int i;
138
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]);
142
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]);
146
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]);
150
151         printf("sepc == 0x%016lx\n", frame->tf_sepc);
152         printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
153 }
154
155 static void
156 svc_handler(struct trapframe *frame)
157 {
158         struct thread *td;
159         int error;
160
161         td = curthread;
162         td->td_frame = frame;
163
164         error = syscallenter(td);
165         syscallret(td, error);
166 }
167
168 static void
169 data_abort(struct trapframe *frame, int usermode)
170 {
171         struct vm_map *map;
172         uint64_t stval;
173         struct thread *td;
174         struct pcb *pcb;
175         vm_prot_t ftype;
176         vm_offset_t va;
177         struct proc *p;
178         int error, sig, ucode;
179
180 #ifdef KDB
181         if (kdb_active) {
182                 kdb_reenter();
183                 return;
184         }
185 #endif
186
187         td = curthread;
188         p = td->td_proc;
189         pcb = td->td_pcb;
190         stval = frame->tf_stval;
191
192         if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
193             WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
194             "Kernel page fault") != 0)
195                 goto fatal;
196
197         if (usermode)
198                 map = &td->td_proc->p_vmspace->vm_map;
199         else if (stval >= VM_MAX_USER_ADDRESS)
200                 map = kernel_map;
201         else {
202                 if (pcb->pcb_onfault == 0)
203                         goto fatal;
204                 map = &td->td_proc->p_vmspace->vm_map;
205         }
206
207         va = trunc_page(stval);
208
209         if ((frame->tf_scause == EXCP_FAULT_STORE) ||
210             (frame->tf_scause == EXCP_STORE_PAGE_FAULT)) {
211                 ftype = VM_PROT_WRITE;
212         } else if (frame->tf_scause == EXCP_INST_PAGE_FAULT) {
213                 ftype = VM_PROT_EXECUTE;
214         } else {
215                 ftype = VM_PROT_READ;
216         }
217
218         if (pmap_fault_fixup(map->pmap, va, ftype))
219                 goto done;
220
221         if (map != kernel_map) {
222                 /*
223                  * Keep swapout from messing with us during this
224                  *      critical time.
225                  */
226                 PROC_LOCK(p);
227                 ++p->p_lock;
228                 PROC_UNLOCK(p);
229
230                 /* Fault in the user page: */
231                 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
232
233                 PROC_LOCK(p);
234                 --p->p_lock;
235                 PROC_UNLOCK(p);
236         } else {
237                 /*
238                  * Don't have to worry about process locking or stacks in the
239                  * kernel.
240                  */
241                 error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
242         }
243
244         if (error != KERN_SUCCESS) {
245                 if (usermode) {
246                         sig = SIGSEGV;
247                         if (error == KERN_PROTECTION_FAILURE)
248                                 ucode = SEGV_ACCERR;
249                         else
250                                 ucode = SEGV_MAPERR;
251                         call_trapsignal(td, sig, ucode, (void *)stval);
252                 } else {
253                         if (pcb->pcb_onfault != 0) {
254                                 frame->tf_a[0] = error;
255                                 frame->tf_sepc = pcb->pcb_onfault;
256                                 return;
257                         }
258                         goto fatal;
259                 }
260         }
261
262 done:
263         if (usermode)
264                 userret(td, frame);
265         return;
266
267 fatal:
268         dump_regs(frame);
269         panic("Fatal page fault at %#lx: %#016lx", frame->tf_sepc, stval);
270 }
271
272 void
273 do_trap_supervisor(struct trapframe *frame)
274 {
275         uint64_t exception;
276         uint64_t sstatus;
277
278         /* Ensure we came from supervisor mode, interrupts disabled */
279         __asm __volatile("csrr %0, sstatus" : "=&r" (sstatus));
280         KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == SSTATUS_SPP,
281                         ("We must came from S mode with interrupts disabled"));
282
283         exception = (frame->tf_scause & EXCP_MASK);
284         if (frame->tf_scause & EXCP_INTR) {
285                 /* Interrupt */
286                 riscv_cpu_intr(frame);
287                 return;
288         }
289
290 #ifdef KDTRACE_HOOKS
291         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
292                 return;
293 #endif
294
295         CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
296             curthread, frame->tf_sepc, frame);
297
298         switch(exception) {
299         case EXCP_FAULT_LOAD:
300         case EXCP_FAULT_STORE:
301         case EXCP_FAULT_FETCH:
302         case EXCP_STORE_PAGE_FAULT:
303         case EXCP_LOAD_PAGE_FAULT:
304                 data_abort(frame, 0);
305                 break;
306         case EXCP_BREAKPOINT:
307 #ifdef KDTRACE_HOOKS
308                 if (dtrace_invop_jump_addr != 0) {
309                         dtrace_invop_jump_addr(frame);
310                         break;
311                 }
312 #endif
313 #ifdef KDB
314                 kdb_trap(exception, 0, frame);
315 #else
316                 dump_regs(frame);
317                 panic("No debugger in kernel.\n");
318 #endif
319                 break;
320         case EXCP_ILLEGAL_INSTRUCTION:
321                 dump_regs(frame);
322                 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
323                 break;
324         default:
325                 dump_regs(frame);
326                 panic("Unknown kernel exception %x trap value %lx\n",
327                     exception, frame->tf_stval);
328         }
329 }
330
331 void
332 do_trap_user(struct trapframe *frame)
333 {
334         uint64_t exception;
335         struct thread *td;
336         uint64_t sstatus;
337         struct pcb *pcb;
338
339         td = curthread;
340         td->td_frame = frame;
341         pcb = td->td_pcb;
342
343         /* Ensure we came from usermode, interrupts disabled */
344         __asm __volatile("csrr %0, sstatus" : "=&r" (sstatus));
345         KASSERT((sstatus & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
346                         ("We must came from U mode with interrupts disabled"));
347
348         exception = (frame->tf_scause & EXCP_MASK);
349         if (frame->tf_scause & EXCP_INTR) {
350                 /* Interrupt */
351                 riscv_cpu_intr(frame);
352                 return;
353         }
354
355         CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
356             curthread, frame->tf_sepc, frame);
357
358         switch(exception) {
359         case EXCP_FAULT_LOAD:
360         case EXCP_FAULT_STORE:
361         case EXCP_FAULT_FETCH:
362         case EXCP_STORE_PAGE_FAULT:
363         case EXCP_LOAD_PAGE_FAULT:
364         case EXCP_INST_PAGE_FAULT:
365                 data_abort(frame, 1);
366                 break;
367         case EXCP_USER_ECALL:
368                 frame->tf_sepc += 4;    /* Next instruction */
369                 svc_handler(frame);
370                 break;
371         case EXCP_ILLEGAL_INSTRUCTION:
372 #ifdef FPE
373                 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
374                         /*
375                          * May be a FPE trap. Enable FPE usage
376                          * for this thread and try again.
377                          */
378                         fpe_state_clear();
379                         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
380                         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
381                         pcb->pcb_fpflags |= PCB_FP_STARTED;
382                         break;
383                 }
384 #endif
385                 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc);
386                 userret(td, frame);
387                 break;
388         case EXCP_BREAKPOINT:
389                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc);
390                 userret(td, frame);
391                 break;
392         default:
393                 dump_regs(frame);
394                 panic("Unknown userland exception %x, trap value %lx\n",
395                     exception, frame->tf_stval);
396         }
397 }