]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/riscv/riscv/trap.c
riscv: Add memmmap so we can mmap /dev/mem
[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;
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         memcpy(sa->args, ap, nap * sizeof(register_t));
121         if (sa->callp->sy_narg > nap)
122                 panic("TODO: Could we have more then %d args?", NARGREG);
123
124         td->td_retval[0] = 0;
125         td->td_retval[1] = 0;
126
127         return (0);
128 }
129
130 #include "../../kern/subr_syscall.c"
131
132 static void
133 dump_regs(struct trapframe *frame)
134 {
135         int n;
136         int i;
137
138         n = nitems(frame->tf_t);
139         for (i = 0; i < n; i++)
140                 printf("t[%d] == 0x%016lx\n", i, frame->tf_t[i]);
141
142         n = nitems(frame->tf_s);
143         for (i = 0; i < n; i++)
144                 printf("s[%d] == 0x%016lx\n", i, frame->tf_s[i]);
145
146         n = nitems(frame->tf_a);
147         for (i = 0; i < n; i++)
148                 printf("a[%d] == 0x%016lx\n", i, frame->tf_a[i]);
149
150         printf("ra == 0x%016lx\n", frame->tf_ra);
151         printf("sp == 0x%016lx\n", frame->tf_sp);
152         printf("gp == 0x%016lx\n", frame->tf_gp);
153         printf("tp == 0x%016lx\n", frame->tf_tp);
154
155         printf("sepc == 0x%016lx\n", frame->tf_sepc);
156         printf("sstatus == 0x%016lx\n", frame->tf_sstatus);
157 }
158
159 static void
160 svc_handler(struct trapframe *frame)
161 {
162         struct thread *td;
163
164         td = curthread;
165         td->td_frame = frame;
166
167         syscallenter(td);
168         syscallret(td);
169 }
170
171 static void
172 data_abort(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
183 #ifdef KDB
184         if (kdb_active) {
185                 kdb_reenter();
186                 return;
187         }
188 #endif
189
190         td = curthread;
191         p = td->td_proc;
192         pcb = td->td_pcb;
193         stval = frame->tf_stval;
194
195         if (td->td_critnest != 0 || td->td_intr_nesting_level != 0 ||
196             WITNESS_CHECK(WARN_SLEEPOK | WARN_GIANTOK, NULL,
197             "Kernel page fault") != 0)
198                 goto fatal;
199
200         if (usermode) {
201                 map = &td->td_proc->p_vmspace->vm_map;
202         } else {
203                 /*
204                  * Enable interrupts for the duration of the page fault. For
205                  * user faults this was done already in do_trap_user().
206                  */
207                 intr_enable();
208
209                 if (stval >= VM_MAX_USER_ADDRESS) {
210                         map = kernel_map;
211                 } else {
212                         if (pcb->pcb_onfault == 0)
213                                 goto fatal;
214                         map = &td->td_proc->p_vmspace->vm_map;
215                 }
216         }
217
218         va = trunc_page(stval);
219
220         if ((frame->tf_scause == EXCP_FAULT_STORE) ||
221             (frame->tf_scause == EXCP_STORE_PAGE_FAULT)) {
222                 ftype = VM_PROT_WRITE;
223         } else if (frame->tf_scause == EXCP_INST_PAGE_FAULT) {
224                 ftype = VM_PROT_EXECUTE;
225         } else {
226                 ftype = VM_PROT_READ;
227         }
228
229         if (pmap_fault_fixup(map->pmap, va, ftype))
230                 goto done;
231
232         error = vm_fault_trap(map, va, ftype, VM_FAULT_NORMAL, &sig, &ucode);
233         if (error != KERN_SUCCESS) {
234                 if (usermode) {
235                         call_trapsignal(td, sig, ucode, (void *)stval,
236                             frame->tf_scause & EXCP_MASK);
237                 } else {
238                         if (pcb->pcb_onfault != 0) {
239                                 frame->tf_a[0] = error;
240                                 frame->tf_sepc = pcb->pcb_onfault;
241                                 return;
242                         }
243                         goto fatal;
244                 }
245         }
246
247 done:
248         if (usermode)
249                 userret(td, frame);
250         return;
251
252 fatal:
253         dump_regs(frame);
254         panic("Fatal page fault at %#lx: %#016lx", frame->tf_sepc, stval);
255 }
256
257 void
258 do_trap_supervisor(struct trapframe *frame)
259 {
260         uint64_t exception;
261
262         /* Ensure we came from supervisor mode, interrupts disabled */
263         KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) ==
264             SSTATUS_SPP, ("Came from S mode with interrupts enabled"));
265
266         exception = frame->tf_scause & EXCP_MASK;
267         if (frame->tf_scause & EXCP_INTR) {
268                 /* Interrupt */
269                 riscv_cpu_intr(frame);
270                 return;
271         }
272
273 #ifdef KDTRACE_HOOKS
274         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
275                 return;
276 #endif
277
278         CTR3(KTR_TRAP, "do_trap_supervisor: curthread: %p, sepc: %lx, frame: %p",
279             curthread, frame->tf_sepc, frame);
280
281         switch (exception) {
282         case EXCP_FAULT_LOAD:
283         case EXCP_FAULT_STORE:
284         case EXCP_FAULT_FETCH:
285                 dump_regs(frame);
286                 panic("Memory access exception at 0x%016lx\n", frame->tf_sepc);
287                 break;
288         case EXCP_STORE_PAGE_FAULT:
289         case EXCP_LOAD_PAGE_FAULT:
290                 data_abort(frame, 0);
291                 break;
292         case EXCP_BREAKPOINT:
293 #ifdef KDTRACE_HOOKS
294                 if (dtrace_invop_jump_addr != NULL &&
295                     dtrace_invop_jump_addr(frame) == 0)
296                                 break;
297 #endif
298 #ifdef KDB
299                 kdb_trap(exception, 0, frame);
300 #else
301                 dump_regs(frame);
302                 panic("No debugger in kernel.\n");
303 #endif
304                 break;
305         case EXCP_ILLEGAL_INSTRUCTION:
306                 dump_regs(frame);
307                 panic("Illegal instruction at 0x%016lx\n", frame->tf_sepc);
308                 break;
309         default:
310                 dump_regs(frame);
311                 panic("Unknown kernel exception %lx trap value %lx\n",
312                     exception, frame->tf_stval);
313         }
314 }
315
316 void
317 do_trap_user(struct trapframe *frame)
318 {
319         uint64_t exception;
320         struct thread *td;
321         struct pcb *pcb;
322
323         td = curthread;
324         td->td_frame = frame;
325         pcb = td->td_pcb;
326
327         /* Ensure we came from usermode, interrupts disabled */
328         KASSERT((csr_read(sstatus) & (SSTATUS_SPP | SSTATUS_SIE)) == 0,
329             ("Came from U mode with interrupts enabled"));
330
331         exception = frame->tf_scause & EXCP_MASK;
332         if (frame->tf_scause & EXCP_INTR) {
333                 /* Interrupt */
334                 riscv_cpu_intr(frame);
335                 return;
336         }
337         intr_enable();
338
339         CTR3(KTR_TRAP, "do_trap_user: curthread: %p, sepc: %lx, frame: %p",
340             curthread, frame->tf_sepc, frame);
341
342         switch (exception) {
343         case EXCP_FAULT_LOAD:
344         case EXCP_FAULT_STORE:
345         case EXCP_FAULT_FETCH:
346         case EXCP_STORE_PAGE_FAULT:
347         case EXCP_LOAD_PAGE_FAULT:
348         case EXCP_INST_PAGE_FAULT:
349                 data_abort(frame, 1);
350                 break;
351         case EXCP_USER_ECALL:
352                 frame->tf_sepc += 4;    /* Next instruction */
353                 svc_handler(frame);
354                 break;
355         case EXCP_ILLEGAL_INSTRUCTION:
356 #ifdef FPE
357                 if ((pcb->pcb_fpflags & PCB_FP_STARTED) == 0) {
358                         /*
359                          * May be a FPE trap. Enable FPE usage
360                          * for this thread and try again.
361                          */
362                         fpe_state_clear();
363                         frame->tf_sstatus &= ~SSTATUS_FS_MASK;
364                         frame->tf_sstatus |= SSTATUS_FS_CLEAN;
365                         pcb->pcb_fpflags |= PCB_FP_STARTED;
366                         break;
367                 }
368 #endif
369                 call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)frame->tf_sepc,
370                     exception);
371                 userret(td, frame);
372                 break;
373         case EXCP_BREAKPOINT:
374                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_sepc,
375                     exception);
376                 userret(td, frame);
377                 break;
378         default:
379                 dump_regs(frame);
380                 panic("Unknown userland exception %lx, trap value %lx\n",
381                     exception, frame->tf_stval);
382         }
383 }