]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/trap.c
Upgrade to Unbound 1.5.9.
[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
56 #ifdef KDTRACE_HOOKS
57 #include <sys/dtrace_bsd.h>
58 #endif
59
60 #ifdef VFP
61 #include <machine/vfp.h>
62 #endif
63
64 #ifdef KDB
65 #include <machine/db_machdep.h>
66 #endif
67
68 #ifdef DDB
69 #include <ddb/db_output.h>
70 #endif
71
72 extern register_t fsu_intr_fault;
73
74 /* Called from exception.S */
75 void do_el1h_sync(struct trapframe *);
76 void do_el0_sync(struct trapframe *);
77 void do_el0_error(struct trapframe *);
78 static void print_registers(struct trapframe *frame);
79
80 int (*dtrace_invop_jump_addr)(struct trapframe *);
81
82 static __inline void
83 call_trapsignal(struct thread *td, int sig, int code, void *addr)
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         trapsignal(td, &ksi);
92 }
93
94 int
95 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
96 {
97         struct proc *p;
98         register_t *ap;
99         int nap;
100
101         nap = 8;
102         p = td->td_proc;
103         ap = td->td_frame->tf_x;
104
105         sa->code = td->td_frame->tf_x[8];
106
107         if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
108                 sa->code = *ap++;
109                 nap--;
110         }
111
112         if (p->p_sysent->sv_mask)
113                 sa->code &= p->p_sysent->sv_mask;
114         if (sa->code >= p->p_sysent->sv_size)
115                 sa->callp = &p->p_sysent->sv_table[0];
116         else
117                 sa->callp = &p->p_sysent->sv_table[sa->code];
118
119         sa->narg = sa->callp->sy_narg;
120         memcpy(sa->args, ap, nap * sizeof(register_t));
121         if (sa->narg > nap)
122                 panic("ARM64TODO: Could we have more than 8 args?");
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 svc_handler(struct trapframe *frame)
134 {
135         struct syscall_args sa;
136         struct thread *td;
137         int error;
138
139         td = curthread;
140
141         error = syscallenter(td, &sa);
142         syscallret(td, error, &sa);
143 }
144
145 static void
146 data_abort(struct trapframe *frame, uint64_t esr, uint64_t far, int lower)
147 {
148         struct vm_map *map;
149         struct thread *td;
150         struct proc *p;
151         struct pcb *pcb;
152         vm_prot_t ftype;
153         vm_offset_t va;
154         int error, sig, ucode;
155
156         /*
157          * According to the ARMv8-A rev. A.g, B2.10.5 "Load-Exclusive
158          * and Store-Exclusive instruction usage restrictions", state
159          * of the exclusive monitors after data abort exception is unknown.
160          */
161         clrex();
162
163 #ifdef KDB
164         if (kdb_active) {
165                 kdb_reenter();
166                 return;
167         }
168 #endif
169
170         td = curthread;
171         pcb = td->td_pcb;
172
173         /*
174          * Special case for fuswintr and suswintr. These can't sleep so
175          * handle them early on in the trap handler.
176          */
177         if (__predict_false(pcb->pcb_onfault == (vm_offset_t)&fsu_intr_fault)) {
178                 frame->tf_elr = pcb->pcb_onfault;
179                 return;
180         }
181
182         p = td->td_proc;
183         if (lower)
184                 map = &p->p_vmspace->vm_map;
185         else {
186                 /* The top bit tells us which range to use */
187                 if ((far >> 63) == 1) {
188                         map = kernel_map;
189                 } else {
190                         map = &p->p_vmspace->vm_map;
191                         if (map == NULL)
192                                 map = kernel_map;
193                 }
194         }
195
196         if (pmap_fault(map->pmap, esr, far) == KERN_SUCCESS)
197                 return;
198
199         KASSERT(td->td_md.md_spinlock_count == 0,
200             ("data abort with spinlock held"));
201         if (td->td_critnest != 0 || WITNESS_CHECK(WARN_SLEEPOK |
202             WARN_GIANTOK, NULL, "Kernel page fault") != 0) {
203                 print_registers(frame);
204                 printf(" far: %16lx\n", far);
205                 printf(" esr:         %.8lx\n", esr);
206                 panic("data abort in critical section or under mutex");
207         }
208
209         va = trunc_page(far);
210         ftype = ((esr >> 6) & 1) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
211
212         /* Fault in the page. */
213         error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
214         if (error != KERN_SUCCESS) {
215                 if (lower) {
216                         sig = SIGSEGV;
217                         if (error == KERN_PROTECTION_FAILURE)
218                                 ucode = SEGV_ACCERR;
219                         else
220                                 ucode = SEGV_MAPERR;
221                         call_trapsignal(td, sig, ucode, (void *)far);
222                 } else {
223                         if (td->td_intr_nesting_level == 0 &&
224                             pcb->pcb_onfault != 0) {
225                                 frame->tf_x[0] = error;
226                                 frame->tf_elr = pcb->pcb_onfault;
227                                 return;
228                         }
229
230                         printf("Fatal data abort:\n");
231                         print_registers(frame);
232                         printf(" far: %16lx\n", far);
233                         printf(" esr:         %.8lx\n", esr);
234
235 #ifdef KDB
236                         if (debugger_on_panic || kdb_active)
237                                 if (kdb_trap(ESR_ELx_EXCEPTION(esr), 0, frame))
238                                         return;
239 #endif
240                         panic("vm_fault failed: %lx", frame->tf_elr);
241                 }
242         }
243
244         if (lower)
245                 userret(td, frame);
246 }
247
248 static void
249 print_registers(struct trapframe *frame)
250 {
251         u_int reg;
252
253         for (reg = 0; reg < 31; reg++) {
254                 printf(" %sx%d: %16lx\n", (reg < 10) ? " " : "", reg,
255                     frame->tf_x[reg]);
256         }
257         printf("  sp: %16lx\n", frame->tf_sp);
258         printf("  lr: %16lx\n", frame->tf_lr);
259         printf(" elr: %16lx\n", frame->tf_elr);
260         printf("spsr: %16lx\n", frame->tf_spsr);
261 }
262
263 void
264 do_el1h_sync(struct trapframe *frame)
265 {
266         uint32_t exception;
267         uint64_t esr, far;
268
269         /* Read the esr register to get the exception details */
270         esr = READ_SPECIALREG(esr_el1);
271         exception = ESR_ELx_EXCEPTION(esr);
272
273 #ifdef KDTRACE_HOOKS
274         if (dtrace_trap_func != NULL && (*dtrace_trap_func)(frame, exception))
275                 return;
276 #endif
277
278         CTR4(KTR_TRAP,
279             "do_el1_sync: curthread: %p, esr %lx, elr: %lx, frame: %p",
280             curthread, esr, frame->tf_elr, frame);
281
282         switch(exception) {
283         case EXCP_FP_SIMD:
284         case EXCP_TRAP_FP:
285                 print_registers(frame);
286                 printf(" esr:         %.8lx\n", esr);
287                 panic("VFP exception in the kernel");
288         case EXCP_INSN_ABORT:
289         case EXCP_DATA_ABORT:
290                 far = READ_SPECIALREG(far_el1);
291                 intr_enable();
292                 data_abort(frame, esr, far, 0);
293                 break;
294         case EXCP_BRK:
295 #ifdef KDTRACE_HOOKS
296                 if ((esr & ESR_ELx_ISS_MASK) == 0x40d && \
297                     dtrace_invop_jump_addr != 0) {
298                         dtrace_invop_jump_addr(frame);
299                         break;
300                 }
301 #endif
302                 /* FALLTHROUGH */
303         case EXCP_WATCHPT_EL1:
304         case EXCP_SOFTSTP_EL1:
305 #ifdef KDB
306                 kdb_trap(exception, 0, frame);
307 #else
308                 panic("No debugger in kernel.\n");
309 #endif
310                 break;
311         default:
312                 print_registers(frame);
313                 panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
314                     esr);
315         }
316 }
317
318 /*
319  * The attempted execution of an instruction bit pattern that has no allocated
320  * instruction results in an exception with an unknown reason.
321  */
322 static void
323 el0_excp_unknown(struct trapframe *frame, uint64_t far)
324 {
325         struct thread *td;
326
327         td = curthread;
328         call_trapsignal(td, SIGILL, ILL_ILLTRP, (void *)far);
329         userret(td, frame);
330 }
331
332 void
333 do_el0_sync(struct trapframe *frame)
334 {
335         struct thread *td;
336         uint32_t exception;
337         uint64_t esr, far;
338
339         /* Check we have a sane environment when entering from userland */
340         KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
341             ("Invalid pcpu address from userland: %p (tpidr %lx)",
342              get_pcpu(), READ_SPECIALREG(tpidr_el1)));
343
344         td = curthread;
345         td->td_frame = frame;
346
347         esr = READ_SPECIALREG(esr_el1);
348         exception = ESR_ELx_EXCEPTION(esr);
349         switch (exception) {
350         case EXCP_UNKNOWN:
351         case EXCP_INSN_ABORT_L:
352         case EXCP_DATA_ABORT_L:
353         case EXCP_DATA_ABORT:
354                 far = READ_SPECIALREG(far_el1);
355         }
356         intr_enable();
357
358         CTR4(KTR_TRAP,
359             "do_el0_sync: curthread: %p, esr %lx, elr: %lx, frame: %p",
360             curthread, esr, frame->tf_elr, frame);
361
362         switch(exception) {
363         case EXCP_FP_SIMD:
364         case EXCP_TRAP_FP:
365 #ifdef VFP
366                 vfp_restore_state();
367 #else
368                 panic("VFP exception in userland");
369 #endif
370                 break;
371         case EXCP_SVC:
372                 svc_handler(frame);
373                 break;
374         case EXCP_INSN_ABORT_L:
375         case EXCP_DATA_ABORT_L:
376         case EXCP_DATA_ABORT:
377                 data_abort(frame, esr, far, 1);
378                 break;
379         case EXCP_UNKNOWN:
380                 el0_excp_unknown(frame, far);
381                 break;
382         case EXCP_SP_ALIGN:
383                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_sp);
384                 userret(td, frame);
385                 break;
386         case EXCP_PC_ALIGN:
387                 call_trapsignal(td, SIGBUS, BUS_ADRALN, (void *)frame->tf_elr);
388                 userret(td, frame);
389                 break;
390         case EXCP_BRK:
391                 call_trapsignal(td, SIGTRAP, TRAP_BRKPT, (void *)frame->tf_elr);
392                 userret(td, frame);
393                 break;
394         case EXCP_SOFTSTP_EL0:
395                 td->td_frame->tf_spsr &= ~PSR_SS;
396                 td->td_pcb->pcb_flags &= ~PCB_SINGLE_STEP;
397                 WRITE_SPECIALREG(MDSCR_EL1,
398                     READ_SPECIALREG(MDSCR_EL1) & ~DBG_MDSCR_SS);
399                 call_trapsignal(td, SIGTRAP, TRAP_TRACE,
400                     (void *)frame->tf_elr);
401                 userret(td, frame);
402                 break;
403         default:
404                 print_registers(frame);
405                 panic("Unknown userland exception %x esr_el1 %lx\n", exception,
406                     esr);
407         }
408 }
409
410 void
411 do_el0_error(struct trapframe *frame)
412 {
413
414         panic("ARM64TODO: do_el0_error");
415 }
416