]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/trap.c
MFV r329718: 8520 7198 lzc_rollback_to should support rolling back to origin
[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 static void print_registers(struct trapframe *frame);
80
81 int (*dtrace_invop_jump_addr)(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 = 8;
104         p = td->td_proc;
105         ap = td->td_frame->tf_x;
106         sa = &td->td_sa;
107
108         sa->code = td->td_frame->tf_x[8];
109
110         if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
111                 sa->code = *ap++;
112                 nap--;
113         }
114
115         if (p->p_sysent->sv_mask)
116                 sa->code &= p->p_sysent->sv_mask;
117         if (sa->code >= p->p_sysent->sv_size)
118                 sa->callp = &p->p_sysent->sv_table[0];
119         else
120                 sa->callp = &p->p_sysent->sv_table[sa->code];
121
122         sa->narg = sa->callp->sy_narg;
123         memcpy(sa->args, ap, nap * sizeof(register_t));
124         if (sa->narg > nap)
125                 panic("ARM64TODO: Could we have more than 8 args?");
126
127         td->td_retval[0] = 0;
128         td->td_retval[1] = 0;
129
130         return (0);
131 }
132
133 #include "../../kern/subr_syscall.c"
134
135 static void
136 svc_handler(struct thread *td, struct trapframe *frame)
137 {
138         int error;
139
140         if ((frame->tf_esr & ESR_ELx_ISS_MASK) == 0) {
141                 error = syscallenter(td);
142                 syscallret(td, error);
143         } else {
144                 call_trapsignal(td, SIGILL, ILL_ILLOPN, (void *)frame->tf_elr);
145                 userret(td, frame);
146         }
147 }
148
149 static void
150 data_abort(struct thread *td, struct trapframe *frame, uint64_t esr,
151     uint64_t far, int lower)
152 {
153         struct vm_map *map;
154         struct proc *p;
155         struct pcb *pcb;
156         vm_prot_t ftype;
157         vm_offset_t va;
158         int error, sig, ucode;
159
160         /*
161          * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
162          * and Store-Exclusive instruction usage restrictions", state
163          * of the exclusive monitors after data abort exception is unknown.
164          */
165         clrex();
166
167 #ifdef KDB
168         if (kdb_active) {
169                 kdb_reenter();
170                 return;
171         }
172 #endif
173
174         pcb = td->td_pcb;
175
176         /*
177          * Special case for fuswintr and suswintr. These can't sleep so
178          * handle them early on in the trap handler.
179          */
180         if (__predict_false(pcb->pcb_onfault == (vm_offset_t)&fsu_intr_fault)) {
181                 frame->tf_elr = pcb->pcb_onfault;
182                 return;
183         }
184
185         p = td->td_proc;
186         if (lower)
187                 map = &p->p_vmspace->vm_map;
188         else {
189                 /* The top bit tells us which range to use */
190                 if (far >= VM_MAXUSER_ADDRESS) {
191                         map = kernel_map;
192                 } else {
193                         map = &p->p_vmspace->vm_map;
194                         if (map == NULL)
195                                 map = kernel_map;
196                 }
197         }
198
199         if (pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
200                 return;
201
202         KASSERT(td->td_md.md_spinlock_count == 0,
203             ("data abort with spinlock held"));
204         if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
205             WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
206                 print_registers(frame);
207                 printf(" far: %16lx\n", far);
208                 printf(" esr:         %.8lx\n", esr);
209                 panic("data abort in critical section or under mutex");
210         }
211
212         va = trunc_page(far);
213         ftype = ((esr >> 6) & 1) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
214
215         /* Fault in the page. */
216         error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
217         if (error != KERN_SUCCESS) {
218                 if (lower) {
219                         sig = SIGSEGV;
220                         if (error == KERN_PROTECTION_FAILURE)
221                                 ucode = SEGV_ACCERR;
222                         else
223                                 ucode = SEGV_MAPERR;
224                         call_trapsignal(td, sig, ucode, (void *)far);
225                 } else {
226                         if (td->td_intr_nesting_level == 0 &&
227                             pcb->pcb_onfault != 0) {
228                                 frame->tf_x[0] = error;
229                                 frame->tf_elr = pcb->pcb_onfault;
230                                 return;
231                         }
232
233                         printf("Fatal data abort:\n");
234                         print_registers(frame);
235                         printf(" far: %16lx\n", far);
236                         printf(" esr:         %.8lx\n", esr);
237
238 #ifdef KDB
239                         if (debugger_on_panic || kdb_active)
240                                 if (kdb_trap(ESR_ELx_EXCEPTION(esr), 0, frame))
241                                         return;
242 #endif
243                         panic("vm_fault failed: %lx", frame->tf_elr);
244                 }
245         }
246
247         if (lower)
248                 userret(td, frame);
249 }
250
251 static void
252 print_registers(struct trapframe *frame)
253 {
254         u_int reg;
255
256         for (reg = 0; reg < nitems(frame->tf_x); reg++) {
257                 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
258                     frame->tf_x[reg]);
259         }
260         printf("  sp: %16lx\n", frame->tf_sp);
261         printf("  lr: %16lx\n", frame->tf_lr);
262         printf(" elr: %16lx\n", frame->tf_elr);
263         printf("spsr:         %8x\n", frame->tf_spsr);
264 }
265
266 void
267 do_el1h_sync(struct thread *td, struct trapframe *frame)
268 {
269         struct trapframe *oframe;
270         uint32_t exception;
271         uint64_t esr, far;
272
273         /* Read the esr register to get the exception details */
274         esr = frame->tf_esr;
275         exception = ESR_ELx_EXCEPTION(esr);
276
277 #ifdef KDTRACE_HOOKS
278         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
279                 return;
280 #endif
281
282         CTR4(KTR_TRAP,
283             "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td,
284             esr, frame->tf_elr, frame);
285
286         oframe = td->td_frame;
287
288         switch (exception) {
289         case EXCP_BRK:
290         case EXCP_WATCHPT_EL1:
291         case EXCP_SOFTSTP_EL1:
292                 break;
293         default:
294                 td->td_frame = frame;
295                 break;
296         }
297
298         switch(exception) {
299         case EXCP_FP_SIMD:
300         case EXCP_TRAP_FP:
301 #ifdef VFP
302                 if ((td->td_pcb->pcb_fpflags & PCB_FP_KERN) != 0) {
303                         vfp_restore_state();
304                 } else
305 #endif
306                 {
307                         print_registers(frame);
308                         printf(" esr:         %.8lx\n", esr);
309                         panic("VFP exception in the kernel");
310                 }
311                 break;
312         case EXCP_INSN_ABORT:
313         case EXCP_DATA_ABORT:
314                 far = READ_SPECIALREG(far_el1);
315                 intr_enable();
316                 data_abort(td, frame, esr, far, 0);
317                 break;
318         case EXCP_BRK:
319 #ifdef KDTRACE_HOOKS
320                 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
321                     dtrace_invop_jump_addr != 0) {
322                         dtrace_invop_jump_addr(frame);
323                         break;
324                 }
325 #endif
326                 kdb_trap(exception, 0,
327                     (td->td_frame != NULL) ? td->td_frame : frame);
328                 frame->tf_elr += 4;
329                 break;
330         case EXCP_WATCHPT_EL1:
331         case EXCP_SOFTSTP_EL1:
332 #ifdef KDB
333                 kdb_trap(exception, 0,
334                     (td->td_frame != NULL) ? td->td_frame : frame);
335 #else
336                 panic("No debugger in kernel.\n");
337 #endif
338                 break;
339         case EXCP_UNKNOWN:
340                 if (undef_insn(1, frame))
341                         break;
342                 /* FALLTHROUGH */
343         default:
344                 print_registers(frame);
345                 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
346                     esr);
347         }
348
349         td->td_frame = oframe;
350 }
351
352 void
353 do_el0_sync(struct thread *td, struct trapframe *frame)
354 {
355         pcpu_bp_harden bp_harden;
356         uint32_t exception;
357         uint64_t esr, far;
358
359         /* Check we have a sane environment when entering from userland */
360         KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
361             ("Invalid pcpu address from userland: %p (tpidr %lx)",
362              get_pcpu(), READ_SPECIALREG(tpidr_el1)));
363
364         esr = frame->tf_esr;
365         exception = ESR_ELx_EXCEPTION(esr);
366         switch (exception) {
367         case EXCP_INSN_ABORT_L:
368                 far = READ_SPECIALREG(far_el1);
369
370                 /*
371                  * Userspace may be trying to train the branch predictor to
372                  * attack the kernel. If we are on a CPU affected by this
373                  * call the handler to clear the branch predictor state.
374                  */
375                 if (far > VM_MAXUSER_ADDRESS) {
376                         bp_harden = PCPU_GET(bp_harden);
377                         if (bp_harden != NULL)
378                                 bp_harden();
379                 }
380                 break;
381         case EXCP_UNKNOWN:
382         case EXCP_DATA_ABORT_L:
383         case EXCP_DATA_ABORT:
384                 far = READ_SPECIALREG(far_el1);
385                 break;
386         }
387         intr_enable();
388
389         CTR4(KTR_TRAP,
390             "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p", td, esr,
391             frame->tf_elr, frame);
392
393         switch(exception) {
394         case EXCP_FP_SIMD:
395         case EXCP_TRAP_FP:
396 #ifdef VFP
397                 vfp_restore_state();
398 #else
399                 panic("VFP exception in userland");
400 #endif
401                 break;
402         case EXCP_SVC32:
403         case EXCP_SVC64:
404                 svc_handler(td, frame);
405                 break;
406         case EXCP_INSN_ABORT_L:
407         case EXCP_DATA_ABORT_L:
408         case EXCP_DATA_ABORT:
409                 data_abort(td, frame, esr, far, 1);
410                 break;
411         case EXCP_UNKNOWN:
412                 if (!undef_insn(0, frame))
413                         call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
414                 userret(td, frame);
415                 break;
416         case EXCP_SP_ALIGN:
417                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
418                 userret(td, frame);
419                 break;
420         case EXCP_PC_ALIGN:
421                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
422                 userret(td, frame);
423                 break;
424         case EXCP_BRK:
425                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
426                 userret(td, frame);
427                 break;
428         case EXCP_MSR:
429                 call_trapsignal(td, SIGILL, ILL_PRVOPC, (void *)frame->tf_elr); 
430                 userret(td, frame);
431                 break;
432         case EXCP_SOFTSTP_EL0:
433                 td->td_frame->tf_spsr &= ~PSR_SS;
434                 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
435                 WRITE_SPECIALREG(MDSCR_EL1,
436                     READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
437                 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
438                     (void *)frame->tf_elr);
439                 userret(td, frame);
440                 break;
441         default:
442                 call_trapsignal(td, SIGBUS, BUS_OBJERR, (void *)frame->tf_elr);
443                 userret(td, frame);
444                 break;
445         }
446
447         KASSERT((td->td_pcb->pcb_fpflags & ~PCB_FP_USERMASK) == 0,
448             ("Kernel VFP flags set while entering userspace"));
449         KASSERT(
450             td->td_pcb->pcb_fpusaved == &td->td_pcb->pcb_fpustate,
451             ("Kernel VFP state in use when entering userspace"));
452 }
453
454 void
455 do_el0_error(struct trapframe *frame)
456 {
457
458         panic("ARM64TODO: do_el0_error");
459 }
460