]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/trap.c
MFV r342532: 5882 Temporary pool names
[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/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/pioctl.h>
37 #include <sys/proc.h>
38 #include <sys/ptrace.h>
39 #include <sys/syscall.h>
40 #include <sys/sysent.h>
41 #ifdef KDB
42 #include <sys/kdb.h>
43 #endif
44
45 #include <vm/vm.h>
46 #include <vm/pmap.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_map.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_extern.h>
51
52 #include <machine/frame.h>
53 #include <machine/pcb.h>
54 #include <machine/pcpu.h>
55 #include <machine/undefined.h>
56
57 #ifdef KDTRACE_HOOKS
58 #include <sys/dtrace_bsd.h>
59 #endif
60
61 #ifdef VFP
62 #include <machine/vfp.h>
63 #endif
64
65 #ifdef KDB
66 #include <machine/db_machdep.h>
67 #endif
68
69 #ifdef DDB
70 #include <ddb/db_output.h>
71 #endif
72
73 extern register_t fsu_intr_fault;
74
75 /* Called from exception.S */
76 void do_el1h_sync(struct thread *, struct trapframe *);
77 void do_el0_sync(struct thread *, struct trapframe *);
78 void do_el0_error(struct trapframe *);
79 void do_serror(struct trapframe *);
80 void unhandled_exception(struct trapframe *);
81
82 static void print_registers(struct trapframe *frame);
83
84 int (*dtrace_invop_jump_addr)(struct trapframe *);
85
86 static __inline void
87 call_trapsignal(struct thread *td, int sig, int code, void *addr)
88 {
89         ksiginfo_t ksi;
90
91         ksiginfo_init_trap(&ksi);
92         ksi.ksi_signo = sig;
93         ksi.ksi_code = code;
94         ksi.ksi_addr = addr;
95         trapsignal(td, &ksi);
96 }
97
98 int
99 cpu_fetch_syscall_args(struct thread *td)
100 {
101         struct proc *p;
102         register_t *ap;
103         struct syscall_args *sa;
104         int nap;
105
106         nap = 8;
107         p = td->td_proc;
108         ap = td->td_frame->tf_x;
109         sa = &td->td_sa;
110
111         sa->code = td->td_frame->tf_x[8];
112
113         if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
114                 sa->code = *ap++;
115                 nap--;
116         }
117
118         if (sa->code >= p->p_sysent->sv_size)
119                 sa->callp = &p->p_sysent->sv_table[0];
120         else
121                 sa->callp = &p->p_sysent->sv_table[sa->code];
122
123         sa->narg = sa->callp->sy_narg;
124         memcpy(sa->args, ap, nap * sizeof(register_t));
125         if (sa->narg > nap)
126                 panic("ARM64TODO: Could we have more than 8 args?");
127
128         td->td_retval[0] = 0;
129         td->td_retval[1] = 0;
130
131         return (0);
132 }
133
134 #include "../../kern/subr_syscall.c"
135
136 static void
137 svc_handler(struct thread *td, struct trapframe *frame)
138 {
139         int error;
140
141         if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
142                 error = syscallenter(td);
143                 syscallret(td, error);
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)
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          * The call to pmap_fault can be dangerous when coming from the
195          * kernel as it may be not be able to lock the pmap to check if
196          * the address is now valid. Because of this we filter the cases
197          * when we are not going to see superpage activity.
198          */
199         if (!lower) {
200                 /*
201                  * We may fault in a DMAP region due to a superpage being
202                  * unmapped when the access took place.
203                  */
204                 if (map == kernel_map && !VIRT_IN_DMAP(far))
205                         goto no_pmap_fault;
206                 /*
207                  * We can also fault in the userspace handling functions,
208                  * e.g. copyin. In these cases we will have set a fault
209                  * handler so we can check if this is set before calling
210                  * pmap_fault.
211                  */
212                 if (map != kernel_map && pcb->pcb_onfault == 0)
213                         goto no_pmap_fault;
214         }
215
216         if (pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
217                 return;
218
219 no_pmap_fault:
220         KASSERT(td->td_md.md_spinlock_count == 0,
221             ("data abort with spinlock held"));
222         if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
223             WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
224                 print_registers(frame);
225                 printf(" far: %16lx\n", far);
226                 printf(" esr:         %.8lx\n", esr);
227                 panic("data abort in critical section or under mutex");
228         }
229
230         va = trunc_page(far);
231         ftype = ((esr >> 6) & 1) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
232
233         /* Fault in the page. */
234         error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
235         if (error != KERN_SUCCESS) {
236                 if (lower) {
237                         sig = SIGSEGV;
238                         if (error == KERN_PROTECTION_FAILURE)
239                                 ucode = SEGV_ACCERR;
240                         else
241                                 ucode = SEGV_MAPERR;
242                         call_trapsignal(td, sig, ucode, (void *)far);
243                 } else {
244                         if (td->td_intr_nesting_level == 0 &&
245                             pcb->pcb_onfault != 0) {
246                                 frame->tf_x[0] = error;
247                                 frame->tf_elr = pcb->pcb_onfault;
248                                 return;
249                         }
250
251                         printf("Fatal data abort:\n");
252                         print_registers(frame);
253                         printf(" far: %16lx\n", far);
254                         printf(" esr:         %.8lx\n", esr);
255
256 #ifdef KDB
257                         if (debugger_on_trap) {
258                                 kdb_why = KDB_WHY_TRAP;
259                                 handled = kdb_trap(ESR_ELx_EXCEPTION(esr), 0,
260                                     frame);
261                                 kdb_why = KDB_WHY_UNSET;
262                                 if (handled)
263                                         return;
264                         }
265 #endif
266                         panic("vm_fault failed: %lx", frame->tf_elr);
267                 }
268         }
269
270         if (lower)
271                 userret(td, frame);
272 }
273
274 static void
275 print_registers(struct trapframe *frame)
276 {
277         u_int reg;
278
279         for (reg = 0; reg < nitems(frame->tf_x); reg++) {
280                 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
281                     frame->tf_x[reg]);
282         }
283         printf("  sp: %16lx\n", frame->tf_sp);
284         printf("  lr: %16lx\n", frame->tf_lr);
285         printf(" elr: %16lx\n", frame->tf_elr);
286         printf("spsr:         %8x\n", frame->tf_spsr);
287 }
288
289 void
290 do_el1h_sync(struct thread *td, struct trapframe *frame)
291 {
292         struct trapframe *oframe;
293         uint32_t exception;
294         uint64_t esr, far;
295
296         /* Read the esr register to get the exception details */
297         esr = frame->tf_esr;
298         exception = ESR_ELx_EXCEPTION(esr);
299
300 #ifdef KDTRACE_HOOKS
301         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
302                 return;
303 #endif
304
305         CTR4(KTR_TRAP,
306             "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
307             esr, frame->tf_elr, frame);
308
309         oframe = td->td_frame;
310
311         switch (exception) {
312         case EXCP_BRK:
313         case EXCP_WATCHPT_EL1:
314         case EXCP_SOFTSTP_EL1:
315                 break;
316         default:
317                 td->td_frame = frame;
318                 break;
319         }
320
321         switch(exception) {
322         case EXCP_FP_SIMD:
323         case EXCP_TRAP_FP:
324 #ifdef VFP
325                 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
326                         vfp_restore_state();
327                 } else
328 #endif
329                 {
330                         print_registers(frame);
331                         printf(" esr:         %.8lx\n", esr);
332                         panic("VFP exception in the kernel");
333                 }
334                 break;
335         case EXCP_INSN_ABORT:
336         case EXCP_DATA_ABORT:
337                 far = READ_SPECIALREG(far_el1);
338                 intr_enable();
339                 data_abort(td, frame, esr, far, 0);
340                 break;
341         case EXCP_BRK:
342 #ifdef KDTRACE_HOOKS
343                 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
344                     dtrace_invop_jump_addr != 0) {
345                         dtrace_invop_jump_addr(frame);
346                         break;
347                 }
348 #endif
349 #ifdef KDB
350                 kdb_trap(exception, 0,
351                     (td->td_frame != NULL) ? td->td_frame : frame);
352 #else
353                 panic("No debugger in kernel.\n");
354 #endif
355                 frame->tf_elr += 4;
356                 break;
357         case EXCP_WATCHPT_EL1:
358         case EXCP_SOFTSTP_EL1:
359 #ifdef KDB
360                 kdb_trap(exception, 0,
361                     (td->td_frame != NULL) ? td->td_frame : frame);
362 #else
363                 panic("No debugger in kernel.\n");
364 #endif
365                 break;
366         case EXCP_UNKNOWN:
367                 if (undef_insn(1, frame))
368                         break;
369                 /* FALLTHROUGH */
370         default:
371                 print_registers(frame);
372                 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
373                     esr);
374         }
375
376         td->td_frame = oframe;
377 }
378
379 void
380 do_el0_sync(struct thread *td, struct trapframe *frame)
381 {
382         pcpu_bp_harden bp_harden;
383         uint32_t exception;
384         uint64_t esr, far;
385
386         /* Check we have a sane environment when entering from userland */
387         KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
388             ("Invalid pcpu address from userland: %p (tpidr %lx)",
389              get_pcpu(), READ_SPECIALREG(tpidr_el1)));
390
391         esr = frame->tf_esr;
392         exception = ESR_ELx_EXCEPTION(esr);
393         switch (exception) {
394         case EXCP_INSN_ABORT_L:
395                 far = READ_SPECIALREG(far_el1);
396
397                 /*
398                  * Userspace may be trying to train the branch predictor to
399                  * attack the kernel. If we are on a CPU affected by this
400                  * call the handler to clear the branch predictor state.
401                  */
402                 if (far > VM_MAXUSER_ADDRESS) {
403                         bp_harden = PCPU_GET(bp_harden);
404                         if (bp_harden != NULL)
405                                 bp_harden();
406                 }
407                 break;
408         case EXCP_UNKNOWN:
409         case EXCP_DATA_ABORT_L:
410         case EXCP_DATA_ABORT:
411                 far = READ_SPECIALREG(far_el1);
412                 break;
413         }
414         intr_enable();
415
416         CTR4(KTR_TRAP,
417             "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
418             frame->tf_elr, frame);
419
420         switch(exception) {
421         case EXCP_FP_SIMD:
422         case EXCP_TRAP_FP:
423 #ifdef VFP
424                 vfp_restore_state();
425 #else
426                 panic("VFP exception in userland");
427 #endif
428                 break;
429         case EXCP_SVC32:
430         case EXCP_SVC64:
431                 svc_handler(td, frame);
432                 break;
433         case EXCP_INSN_ABORT_L:
434         case EXCP_DATA_ABORT_L:
435         case EXCP_DATA_ABORT:
436                 data_abort(td, frame, esr, far, 1);
437                 break;
438         case EXCP_UNKNOWN:
439                 if (!undef_insn(0, frame))
440                         call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
441                 userret(td, frame);
442                 break;
443         case EXCP_SP_ALIGN:
444                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
445                 userret(td, frame);
446                 break;
447         case EXCP_PC_ALIGN:
448                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
449                 userret(td, frame);
450                 break;
451         case EXCP_BRK:
452                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
453                 userret(td, frame);
454                 break;
455         case EXCP_MSR:
456                 call_trapsignal(td, SIGILL, ILL_PRVOPC, (void *)frame->tf_elr); 
457                 userret(td, frame);
458                 break;
459         case EXCP_SOFTSTP_EL0:
460                 td->td_frame->tf_spsr &= ~PSR_SS;
461                 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
462                 WRITE_SPECIALREG(MDSCR_EL1,
463                     READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
464                 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
465                     (void *)frame->tf_elr);
466                 userret(td, frame);
467                 break;
468         default:
469                 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr);
470                 userret(td, frame);
471                 break;
472         }
473
474         KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
475             ("Kernel VFP flags set while entering userspace"));
476         KASSERT(
477             td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
478             ("Kernel VFP state in use when entering userspace"));
479 }
480
481 /*
482  * TODO: We will need to handle these later when we support ARMv8.2 RAS.
483  */
484 void
485 do_serror(struct trapframe *frame)
486 {
487         uint64_t esr, far;
488
489         far = READ_SPECIALREG(far_el1);
490         esr = frame->tf_esr;
491
492         print_registers(frame);
493         printf(" far: %16lx\n", far);
494         printf(" esr:         %.8lx\n", esr);
495         panic("Unhandled System Error");
496 }
497
498 void
499 unhandled_exception(struct trapframe *frame)
500 {
501         uint64_t esr, far;
502
503         far = READ_SPECIALREG(far_el1);
504         esr = frame->tf_esr;
505
506         print_registers(frame);
507         printf(" far: %16lx\n", far);
508         printf(" esr:         %.8lx\n", esr);
509         panic("Unhandled exception");
510 }