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