]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/trap.c
Add 'contrib/pnglite/' from commit 'a70c2a23d0d84dfc63a1d9413a7f4aaede7313aa'
[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/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, int trapno)
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         ksi.ksi_trapno = trapno;
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, *dst_ap;
100         struct syscall_args *sa;
101
102         p = td->td_proc;
103         sa = &td->td_sa;
104         ap = &td->td_frame->tf_a[0];
105         dst_ap = &sa->args[0];
106
107         sa->code = td->td_frame->tf_t[0];
108
109         if (__predict_false(sa->code == SYS_syscall || sa->code == SYS___syscall)) {
110                 sa->code = *ap++;
111         } else {
112                 *dst_ap++ = *ap++;
113         }
114
115         if (__predict_false(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         KASSERT(sa->callp->sy_narg <= nitems(sa->args),
121             ("Syscall %d takes too many arguments", sa->code));
122
123         memcpy(dst_ap, ap, (NARGREG - 1) * sizeof(register_t));
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 = nitems(frame->tf_t);
140         for (i = 0; i < n; i++)
141                 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
142
143         n = nitems(frame->tf_s);
144         for (i = 0; i < n; i++)
145                 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
146
147         n = nitems(frame->tf_a);
148         for (i = 0; i < n; i++)
149                 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
150
151         printf("ra == 0x%016lx\n", frame->tf_ra);
152         printf("sp == 0x%016lx\n", frame->tf_sp);
153         printf("gp == 0x%016lx\n", frame->tf_gp);
154         printf("tp == 0x%016lx\n", frame->tf_tp);
155
156         printf("sepc == 0x%016lx\n", frame->tf_sepc);
157         printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
158 }
159
160 static void
161 ecall_handler(void)
162 {
163         struct thread *td;
164
165         td = curthread;
166
167         syscallenter(td);
168         syscallret(td);
169 }
170
171 static void
172 page_fault_handler(struct trapframe *frame, int usermode)
173 {
174         struct vm_map *map;
175         uint64_t stval;
176         struct thread *td;
177         struct pcb *pcb;
178         vm_prot_t ftype;
179         vm_offset_t va;
180         struct proc *p;
181         int error, sig, ucode;
182 #ifdef KDB
183         bool handled;
184 #endif
185
186 #ifdef KDB
187         if (kdb_active) {
188                 kdb_reenter();
189                 return;
190         }
191 #endif
192
193         td = curthread;
194         p = td->td_proc;
195         pcb = td->td_pcb;
196         stval = frame->tf_stval;
197
198         if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
199             WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
200             "Kernel page fault") != 0)
201                 goto fatal;
202
203         if (usermode) {
204                 map = &td->td_proc->p_vmspace->vm_map;
205         } else {
206                 /*
207                  * Enable interrupts for the duration of the page fault. For
208                  * user faults this was done already in do_trap_user().
209                  */
210                 intr_enable();
211
212                 if (stval >= VM_MAX_USER_ADDRESS) {
213                         map = kernel_map;
214                 } else {
215                         if (pcb->pcb_onfault == 0)
216                                 goto fatal;
217                         map = &td->td_proc->p_vmspace->vm_map;
218                 }
219         }
220
221         va = trunc_page(stval);
222
223         if (frame->tf_scause == SCAUSE_STORE_PAGE_FAULT) {
224                 ftype = VM_PROT_WRITE;
225         } else if (frame->tf_scause == SCAUSE_INST_PAGE_FAULT) {
226                 ftype = VM_PROT_EXECUTE;
227         } else {
228                 ftype = VM_PROT_READ;
229         }
230
231         if (pmap_fault_fixup(map->pmap, va, ftype))
232                 goto done;
233
234         error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
235         if (error != KERN_SUCCESS) {
236                 if (usermode) {
237                         call_trapsignal(td, sig, ucode, (void *)stval,
238                             frame->tf_scause & SCAUSE_CODE);
239                 } else {
240                         if (pcb->pcb_onfault != 0) {
241                                 frame->tf_a[0] = error;
242                                 frame->tf_sepc = pcb->pcb_onfault;
243                                 return;
244                         }
245                         goto fatal;
246                 }
247         }
248
249 done:
250         if (usermode)
251                 userret(td, frame);
252         return;
253
254 fatal:
255         dump_regs(frame);
256 #ifdef KDB
257         if (debugger_on_trap) {
258                 kdb_why = KDB_WHY_TRAP;
259                 handled = kdb_trap(frame->tf_scause & SCAUSE_CODE, 0, frame);
260                 kdb_why = KDB_WHY_UNSET;
261                 if (handled)
262                         return;
263         }
264 #endif
265         panic("Fatal page fault at %#lx: %#016lx", frame->tf_sepc, stval);
266 }
267
268 void
269 do_trap_supervisor(struct trapframe *frame)
270 {
271         uint64_t exception;
272
273         /* Ensure we came from supervisor mode, interrupts disabled */
274         KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) ==
275             SSTATUS_SPP, ("Came from S mode with interrupts enabled"));
276
277         exception = frame->tf_scause & SCAUSE_CODE;
278         if ((frame->tf_scause & SCAUSE_INTR) != 0) {
279                 /* Interrupt */
280                 riscv_cpu_intr(frame);
281                 return;
282         }
283
284 #ifdef KDTRACE_HOOKS
285         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
286                 return;
287 #endif
288
289         CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
290             curthread, frame->tf_sepc, frame);
291
292         switch (exception) {
293         case SCAUSE_LOAD_ACCESS_FAULT:
294         case SCAUSE_STORE_ACCESS_FAULT:
295         case SCAUSE_INST_ACCESS_FAULT:
296                 dump_regs(frame);
297                 panic("Memory access exception at 0x%016lx\n", frame->tf_sepc);
298                 break;
299         case SCAUSE_STORE_PAGE_FAULT:
300         case SCAUSE_LOAD_PAGE_FAULT:
301         case SCAUSE_INST_PAGE_FAULT:
302                 page_fault_handler(frame, 0);
303                 break;
304         case SCAUSE_BREAKPOINT:
305 #ifdef KDTRACE_HOOKS
306                 if (dtrace_invop_jump_addr != NULL &&
307                     dtrace_invop_jump_addr(frame) == 0)
308                                 break;
309 #endif
310 #ifdef KDB
311                 kdb_trap(exception, 0, frame);
312 #else
313                 dump_regs(frame);
314                 panic("No debugger in kernel.\n");
315 #endif
316                 break;
317         case SCAUSE_ILLEGAL_INSTRUCTION:
318                 dump_regs(frame);
319                 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
320                 break;
321         default:
322                 dump_regs(frame);
323                 panic("Unknown kernel exception %lx trap value %lx\n",
324                     exception, frame->tf_stval);
325         }
326 }
327
328 void
329 do_trap_user(struct trapframe *frame)
330 {
331         uint64_t exception;
332         struct thread *td;
333         struct pcb *pcb;
334
335         td = curthread;
336         pcb = td->td_pcb;
337
338         KASSERT(td->td_frame == frame,
339             ("%s: td_frame %p != frame %p", __func__, td->td_frame, frame));
340
341         /* Ensure we came from usermode, interrupts disabled */
342         KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
343             ("Came from U mode with interrupts enabled"));
344
345         exception = frame->tf_scause & SCAUSE_CODE;
346         if ((frame->tf_scause & SCAUSE_INTR) != 0) {
347                 /* Interrupt */
348                 riscv_cpu_intr(frame);
349                 return;
350         }
351         intr_enable();
352
353         CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
354             curthread, frame->tf_sepc, frame);
355
356         switch (exception) {
357         case SCAUSE_LOAD_ACCESS_FAULT:
358         case SCAUSE_STORE_ACCESS_FAULT:
359         case SCAUSE_INST_ACCESS_FAULT:
360                 call_trapsignal(td, SIGBUS, BUS_ADRERR, (void *)frame->tf_sepc,
361                     exception);
362                 userret(td, frame);
363                 break;
364         case SCAUSE_STORE_PAGE_FAULT:
365         case SCAUSE_LOAD_PAGE_FAULT:
366         case SCAUSE_INST_PAGE_FAULT:
367                 page_fault_handler(frame, 1);
368                 break;
369         case SCAUSE_ECALL_USER:
370                 frame->tf_sepc += 4;    /* Next instruction */
371                 ecall_handler();
372                 break;
373         case SCAUSE_ILLEGAL_INSTRUCTION:
374 #ifdef FPE
375                 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
376                         /*
377                          * May be a FPE trap. Enable FPE usage
378                          * for this thread and try again.
379                          */
380                         fpe_state_clear();
381                         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
382                         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
383                         pcb->pcb_fpflags |= PCB_FP_STARTED;
384                         break;
385                 }
386 #endif
387                 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc,
388                     exception);
389                 userret(td, frame);
390                 break;
391         case SCAUSE_BREAKPOINT:
392                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc,
393                     exception);
394                 userret(td, frame);
395                 break;
396         default:
397                 dump_regs(frame);
398                 panic("Unknown userland exception %lx, trap value %lx\n",
399                     exception, frame->tf_stval);
400         }
401 }