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