]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/trap.c
Implement pci_enable_msi() and pci_disable_msi() in the LinuxKPI.
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / trap.c
1 /*-
2  * Copyright (c) 2014 Andrew Turner
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/ktr.h>
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 #include <sys/pioctl.h>
38 #include <sys/proc.h>
39 #include <sys/ptrace.h>
40 #include <sys/syscall.h>
41 #include <sys/sysent.h>
42 #ifdef KDB
43 #include <sys/kdb.h>
44 #endif
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48 #include <vm/vm_kern.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_param.h>
51 #include <vm/vm_extern.h>
52
53 #include <machine/frame.h>
54 #include <machine/pcb.h>
55 #include <machine/pcpu.h>
56 #include <machine/undefined.h>
57
58 #ifdef KDTRACE_HOOKS
59 #include <sys/dtrace_bsd.h>
60 #endif
61
62 #ifdef VFP
63 #include <machine/vfp.h>
64 #endif
65
66 #ifdef KDB
67 #include <machine/db_machdep.h>
68 #endif
69
70 #ifdef DDB
71 #include <ddb/db_output.h>
72 #endif
73
74 extern register_t fsu_intr_fault;
75
76 /* Called from exception.S */
77 void do_el1h_sync(struct thread *, struct trapframe *);
78 void do_el0_sync(struct thread *, struct trapframe *);
79 void do_el0_error(struct trapframe *);
80 void do_serror(struct trapframe *);
81 void unhandled_exception(struct trapframe *);
82
83 static void print_registers(struct trapframe *frame);
84
85 int (*dtrace_invop_jump_addr)(struct trapframe *);
86
87 static __inline void
88 call_trapsignal(struct thread *td, int sig, int code, void *addr)
89 {
90         ksiginfo_t ksi;
91
92         ksiginfo_init_trap(&ksi);
93         ksi.ksi_signo = sig;
94         ksi.ksi_code = code;
95         ksi.ksi_addr = addr;
96         trapsignal(td, &ksi);
97 }
98
99 int
100 cpu_fetch_syscall_args(struct thread *td)
101 {
102         struct proc *p;
103         register_t *ap;
104         struct syscall_args *sa;
105         int nap;
106
107         nap = 8;
108         p = td->td_proc;
109         ap = td->td_frame->tf_x;
110         sa = &td->td_sa;
111
112         sa->code = td->td_frame->tf_x[8];
113
114         if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
115                 sa->code = *ap++;
116                 nap--;
117         }
118
119         if (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         sa->narg = sa->callp->sy_narg;
125         memcpy(sa->args, ap, nap * sizeof(register_t));
126         if (sa->narg > nap)
127                 panic("ARM64TODO: Could we have more than 8 args?");
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 svc_handler(struct thread *td, struct trapframe *frame)
139 {
140
141         if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
142                 syscallenter(td);
143                 syscallret(td);
144         } else {
145                 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr);
146                 userret(td, frame);
147         }
148 }
149
150 static void
151 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
152     uint64_t far, int lower, int exec)
153 {
154         struct vm_map *map;
155         struct proc *p;
156         struct pcb *pcb;
157         vm_prot_t ftype;
158         vm_offset_t va;
159         int error, sig, ucode;
160 #ifdef KDB
161         bool handled;
162 #endif
163
164         /*
165          * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
166          * and Store-Exclusive instruction usage restrictions", state
167          * of the exclusive monitors after data abort exception is unknown.
168          */
169         clrex();
170
171 #ifdef KDB
172         if (kdb_active) {
173                 kdb_reenter();
174                 return;
175         }
176 #endif
177
178         pcb = td->td_pcb;
179         p = td->td_proc;
180         if (lower)
181                 map = &p->p_vmspace->vm_map;
182         else {
183                 /* The top bit tells us which range to use */
184                 if (far >= VM_MAXUSER_ADDRESS) {
185                         map = kernel_map;
186                 } else {
187                         map = &p->p_vmspace->vm_map;
188                         if (map == NULL)
189                                 map = kernel_map;
190                 }
191         }
192
193         /*
194          * Try to handle translation, access flag, and permission faults.
195          * Translation faults may occur as a result of the required
196          * break-before-make sequence used when promoting or demoting
197          * superpages.  Such faults must not occur while holding the pmap lock,
198          * or pmap_fault() will recurse on that lock.
199          */
200         if ((lower || map == kernel_map || pcb->pcb_onfault != 0) &&
201             pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
202                 return;
203
204         KASSERT(td->td_md.md_spinlock_count == 0,
205             ("data abort with spinlock held"));
206         if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
207             WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
208                 print_registers(frame);
209                 printf(" far: %16lx\n", far);
210                 printf(" esr:         %.8lx\n", esr);
211                 panic("data abort in critical section or under mutex");
212         }
213
214         va = trunc_page(far);
215         if (exec)
216                 ftype = VM_PROT_EXECUTE;
217         else
218                 ftype = (esr & ISS_DATA_WnR) == 0 ? VM_PROT_READ :
219                     VM_PROT_READ | VM_PROT_WRITE;
220
221         /* Fault in the page. */
222         error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
223         if (error != KERN_SUCCESS) {
224                 if (lower) {
225                         sig = SIGSEGV;
226                         if (error == KERN_PROTECTION_FAILURE)
227                                 ucode = SEGV_ACCERR;
228                         else
229                                 ucode = SEGV_MAPERR;
230                         call_trapsignal(td, sig, ucode, (void *)far);
231                 } else {
232                         if (td->td_intr_nesting_level == 0 &&
233                             pcb->pcb_onfault != 0) {
234                                 frame->tf_x[0] = error;
235                                 frame->tf_elr = pcb->pcb_onfault;
236                                 return;
237                         }
238
239                         printf("Fatal data abort:\n");
240                         print_registers(frame);
241                         printf(" far: %16lx\n", far);
242                         printf(" esr:         %.8lx\n", esr);
243
244 #ifdef KDB
245                         if (debugger_on_trap) {
246                                 kdb_why = KDB_WHY_TRAP;
247                                 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
248                                     frame);
249                                 kdb_why = KDB_WHY_UNSET;
250                                 if (handled)
251                                         return;
252                         }
253 #endif
254                         panic("vm_fault failed: %lx", frame->tf_elr);
255                 }
256         }
257
258         if (lower)
259                 userret(td, frame);
260 }
261
262 static void
263 print_registers(struct trapframe *frame)
264 {
265         u_int reg;
266
267         for (reg = 0; reg < nitems(frame->tf_x); reg++) {
268                 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
269                     frame->tf_x[reg]);
270         }
271         printf("  sp: %16lx\n", frame->tf_sp);
272         printf("  lr: %16lx\n", frame->tf_lr);
273         printf(" elr: %16lx\n", frame->tf_elr);
274         printf("spsr:         %8x\n", frame->tf_spsr);
275 }
276
277 void
278 do_el1h_sync(struct thread *td, struct trapframe *frame)
279 {
280         struct trapframe *oframe;
281         uint32_t exception;
282         uint64_t esr, far;
283
284         /* Read the esr register to get the exception details */
285         esr = frame->tf_esr;
286         exception = ESR_ELx_EXCEPTION(esr);
287
288 #ifdef KDTRACE_HOOKS
289         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
290                 return;
291 #endif
292
293         CTR4(KTR_TRAP,
294             "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
295             esr, frame->tf_elr, frame);
296
297         oframe = td->td_frame;
298
299         switch (exception) {
300         case EXCP_BRK:
301         case EXCP_WATCHPT_EL1:
302         case EXCP_SOFTSTP_EL1:
303                 break;
304         default:
305                 td->td_frame = frame;
306                 break;
307         }
308
309         switch(exception) {
310         case EXCP_FP_SIMD:
311         case EXCP_TRAP_FP:
312 #ifdef VFP
313                 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
314                         vfp_restore_state();
315                 } else
316 #endif
317                 {
318                         print_registers(frame);
319                         printf(" esr:         %.8lx\n", esr);
320                         panic("VFP exception in the kernel");
321                 }
322                 break;
323         case EXCP_INSN_ABORT:
324         case EXCP_DATA_ABORT:
325                 far = READ_SPECIALREG(far_el1);
326                 intr_enable();
327                 data_abort(td, frame, esr, far, 0,
328                     exception == EXCP_INSN_ABORT);
329                 break;
330         case EXCP_BRK:
331 #ifdef KDTRACE_HOOKS
332                 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
333                     dtrace_invop_jump_addr != 0) {
334                         dtrace_invop_jump_addr(frame);
335                         break;
336                 }
337 #endif
338 #ifdef KDB
339                 kdb_trap(exception, 0,
340                     (td->td_frame != NULL) ? td->td_frame : frame);
341 #else
342                 panic("No debugger in kernel.\n");
343 #endif
344                 frame->tf_elr += 4;
345                 break;
346         case EXCP_WATCHPT_EL1:
347         case EXCP_SOFTSTP_EL1:
348 #ifdef KDB
349                 kdb_trap(exception, 0,
350                     (td->td_frame != NULL) ? td->td_frame : frame);
351 #else
352                 panic("No debugger in kernel.\n");
353 #endif
354                 break;
355         case EXCP_UNKNOWN:
356                 if (undef_insn(1, frame))
357                         break;
358                 /* FALLTHROUGH */
359         default:
360                 print_registers(frame);
361                 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
362                     esr);
363         }
364
365         td->td_frame = oframe;
366 }
367
368 void
369 do_el0_sync(struct thread *td, struct trapframe *frame)
370 {
371         pcpu_bp_harden bp_harden;
372         uint32_t exception;
373         uint64_t esr, far;
374
375         /* Check we have a sane environment when entering from userland */
376         KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
377             ("Invalid pcpu address from userland: %p (tpidr %lx)",
378              get_pcpu(), READ_SPECIALREG(tpidr_el1)));
379
380         esr = frame->tf_esr;
381         exception = ESR_ELx_EXCEPTION(esr);
382         switch (exception) {
383         case EXCP_INSN_ABORT_L:
384                 far = READ_SPECIALREG(far_el1);
385
386                 /*
387                  * Userspace may be trying to train the branch predictor to
388                  * attack the kernel. If we are on a CPU affected by this
389                  * call the handler to clear the branch predictor state.
390                  */
391                 if (far > VM_MAXUSER_ADDRESS) {
392                         bp_harden = PCPU_GET(bp_harden);
393                         if (bp_harden != NULL)
394                                 bp_harden();
395                 }
396                 break;
397         case EXCP_UNKNOWN:
398         case EXCP_DATA_ABORT_L:
399         case EXCP_DATA_ABORT:
400                 far = READ_SPECIALREG(far_el1);
401                 break;
402         }
403         intr_enable();
404
405         CTR4(KTR_TRAP,
406             "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
407             frame->tf_elr, frame);
408
409         switch(exception) {
410         case EXCP_FP_SIMD:
411         case EXCP_TRAP_FP:
412 #ifdef VFP
413                 vfp_restore_state();
414 #else
415                 panic("VFP exception in userland");
416 #endif
417                 break;
418         case EXCP_SVC32:
419         case EXCP_SVC64:
420                 svc_handler(td, frame);
421                 break;
422         case EXCP_INSN_ABORT_L:
423         case EXCP_DATA_ABORT_L:
424         case EXCP_DATA_ABORT:
425                 data_abort(td, frame, esr, far, 1,
426                     exception == EXCP_INSN_ABORT_L);
427                 break;
428         case EXCP_UNKNOWN:
429                 if (!undef_insn(0, frame))
430                         call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
431                 userret(td, frame);
432                 break;
433         case EXCP_SP_ALIGN:
434                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
435                 userret(td, frame);
436                 break;
437         case EXCP_PC_ALIGN:
438                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
439                 userret(td, frame);
440                 break;
441         case EXCP_BRK:
442                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
443                 userret(td, frame);
444                 break;
445         case EXCP_MSR:
446                 call_trapsignal(td, SIGILL, ILL_PRVOPC, (void *)frame->tf_elr); 
447                 userret(td, frame);
448                 break;
449         case EXCP_SOFTSTP_EL0:
450                 td->td_frame->tf_spsr &= ~PSR_SS;
451                 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
452                 WRITE_SPECIALREG(MDSCR_EL1,
453                     READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
454                 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
455                     (void *)frame->tf_elr);
456                 userret(td, frame);
457                 break;
458         default:
459                 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr);
460                 userret(td, frame);
461                 break;
462         }
463
464         KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
465             ("Kernel VFP flags set while entering userspace"));
466         KASSERT(
467             td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
468             ("Kernel VFP state in use when entering userspace"));
469 }
470
471 /*
472  * TODO: We will need to handle these later when we support ARMv8.2 RAS.
473  */
474 void
475 do_serror(struct trapframe *frame)
476 {
477         uint64_t esr, far;
478
479         far = READ_SPECIALREG(far_el1);
480         esr = frame->tf_esr;
481
482         print_registers(frame);
483         printf(" far: %16lx\n", far);
484         printf(" esr:         %.8lx\n", esr);
485         panic("Unhandled System Error");
486 }
487
488 void
489 unhandled_exception(struct trapframe *frame)
490 {
491         uint64_t esr, far;
492
493         far = READ_SPECIALREG(far_el1);
494         esr = frame->tf_esr;
495
496         print_registers(frame);
497         printf(" far: %16lx\n", far);
498         printf(" esr:         %.8lx\n", esr);
499         panic("Unhandled exception");
500 }