xref: /freebsd/sys/arm64/arm64/trap.c (revision 5b9c547c)
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_extern.h>
50 
51 #include <machine/frame.h>
52 #include <machine/pcb.h>
53 #include <machine/pcpu.h>
54 #include <machine/vmparam.h>
55 
56 #ifdef VFP
57 #include <machine/vfp.h>
58 #endif
59 
60 #ifdef KDB
61 #include <machine/db_machdep.h>
62 #endif
63 
64 #ifdef DDB
65 #include <ddb/db_output.h>
66 #endif
67 
68 extern register_t fsu_intr_fault;
69 
70 /* Called from exception.S */
71 void do_el1h_sync(struct trapframe *);
72 void do_el0_sync(struct trapframe *);
73 void do_el0_error(struct trapframe *);
74 
75 static __inline void
76 call_trapsignal(struct thread *td, int sig, u_long code)
77 {
78 	ksiginfo_t ksi;
79 
80 	ksiginfo_init_trap(&ksi);
81 	ksi.ksi_signo = sig;
82 	ksi.ksi_code = (int)code;
83 	trapsignal(td, &ksi);
84 }
85 
86 int
87 cpu_fetch_syscall_args(struct thread *td, struct syscall_args *sa)
88 {
89 	struct proc *p;
90 	register_t *ap;
91 	int nap;
92 
93 	nap = 8;
94 	p = td->td_proc;
95 	ap = td->td_frame->tf_x;
96 
97 	sa->code = td->td_frame->tf_x[8];
98 
99 	if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
100 		sa->code = *ap++;
101 		nap--;
102 	}
103 
104 	if (p->p_sysent->sv_mask)
105 		sa->code &= p->p_sysent->sv_mask;
106 	if (sa->code >= p->p_sysent->sv_size)
107 		sa->callp = &p->p_sysent->sv_table[0];
108 	else
109 		sa->callp = &p->p_sysent->sv_table[sa->code];
110 
111 	sa->narg = sa->callp->sy_narg;
112 	memcpy(sa->args, ap, nap * sizeof(register_t));
113 	if (sa->narg > nap)
114 		panic("TODO: Could we have more then 8 args?");
115 
116 	td->td_retval[0] = 0;
117 	td->td_retval[1] = 0;
118 
119 	return (0);
120 }
121 
122 #include "../../kern/subr_syscall.c"
123 
124 static void
125 svc_handler(struct trapframe *frame)
126 {
127 	struct syscall_args sa;
128 	struct thread *td;
129 	int error;
130 
131 	td = curthread;
132 	td->td_frame = frame;
133 
134 	error = syscallenter(td, &sa);
135 	syscallret(td, error, &sa);
136 }
137 
138 static void
139 data_abort(struct trapframe *frame, uint64_t esr, int lower)
140 {
141 	struct vm_map *map;
142 	struct thread *td;
143 	struct proc *p;
144 	struct pcb *pcb;
145 	vm_prot_t ftype;
146 	vm_offset_t va;
147 	uint64_t far;
148 	int error, sig;
149 
150 	td = curthread;
151 	pcb = td->td_pcb;
152 
153 	/*
154 	 * Special case for fuswintr and suswintr. These can't sleep so
155 	 * handle them early on in the trap handler.
156 	 */
157 	if (__predict_false(pcb->pcb_onfault == (vm_offset_t)&fsu_intr_fault)) {
158 		frame->tf_elr = pcb->pcb_onfault;
159 		return;
160 	}
161 
162 	far = READ_SPECIALREG(far_el1);
163 	p = td->td_proc;
164 
165 	if (lower)
166 		map = &td->td_proc->p_vmspace->vm_map;
167 	else {
168 		/* The top bit tells us which range to use */
169 		if ((far >> 63) == 1)
170 			map = kernel_map;
171 		else
172 			map = &td->td_proc->p_vmspace->vm_map;
173 	}
174 
175 	va = trunc_page(far);
176 	ftype = ((esr >> 6) & 1) ? VM_PROT_READ | VM_PROT_WRITE : VM_PROT_READ;
177 
178 	if (map != kernel_map) {
179 		/*
180 		 * Keep swapout from messing with us during this
181 		 *	critical time.
182 		 */
183 		PROC_LOCK(p);
184 		++p->p_lock;
185 		PROC_UNLOCK(p);
186 
187 		/* Fault in the user page: */
188 		error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
189 
190 		PROC_LOCK(p);
191 		--p->p_lock;
192 		PROC_UNLOCK(p);
193 	} else {
194 		/*
195 		 * Don't have to worry about process locking or stacks in the
196 		 * kernel.
197 		 */
198 		error = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
199 	}
200 
201 	if (error != 0) {
202 		if (lower) {
203 			if (error == ENOMEM)
204 				sig = SIGKILL;
205 			else
206 				sig = SIGSEGV;
207 			call_trapsignal(td, sig, 0);
208 		} else {
209 			if (td->td_intr_nesting_level == 0 &&
210 			    pcb->pcb_onfault != 0) {
211 				frame->tf_x[0] = error;
212 				frame->tf_elr = pcb->pcb_onfault;
213 				return;
214 			}
215 			panic("vm_fault failed: %lx", frame->tf_elr);
216 		}
217 	}
218 
219 	if (lower)
220 		userret(td, frame);
221 }
222 
223 void
224 do_el1h_sync(struct trapframe *frame)
225 {
226 	uint32_t exception;
227 	uint64_t esr;
228 
229 	/* Read the esr register to get the exception details */
230 	esr = READ_SPECIALREG(esr_el1);
231 	exception = ESR_ELx_EXCEPTION(esr);
232 
233 	/*
234 	 * Sanity check we are in an exception er can handle. The IL bit
235 	 * is used to indicate the instruction length, except in a few
236 	 * exceptions described in the ARMv8 ARM.
237 	 *
238 	 * It is unclear in some cases if the bit is implementation defined.
239 	 * The Foundation Model and QEMU disagree on if the IL bit should
240 	 * be set when we are in a data fault from the same EL and the ISV
241 	 * bit (bit 24) is also set.
242 	 */
243 	KASSERT((esr & ESR_ELx_IL) == ESR_ELx_IL ||
244 	    (exception == EXCP_DATA_ABORT && ((esr & ISS_DATA_ISV) == 0)),
245 	    ("Invalid instruction length in exception"));
246 
247 	switch(exception) {
248 	case EXCP_FP_SIMD:
249 	case EXCP_TRAP_FP:
250 		panic("VFP exception in the kernel");
251 	case EXCP_DATA_ABORT:
252 		data_abort(frame, esr, 0);
253 		break;
254 	case EXCP_BRK:
255 	case EXCP_WATCHPT_EL1:
256 	case EXCP_SOFTSTP_EL1:
257 #ifdef KDB
258 		kdb_trap(exception, 0, frame);
259 #else
260 		panic("No debugger in kernel.\n");
261 #endif
262 		break;
263 	default:
264 		panic("Unknown kernel exception %x esr_el1 %lx\n", exception,
265 		    esr);
266 	}
267 }
268 
269 void
270 do_el0_sync(struct trapframe *frame)
271 {
272 	uint32_t exception;
273 	uint64_t esr;
274 
275 	/* Check we have a sane environment when entering from userland */
276 	KASSERT((uintptr_t)get_pcpu() >= VM_MIN_KERNEL_ADDRESS,
277 	    ("Invalid pcpu address from userland: %p (tpidr %lx)",
278 	     get_pcpu(), READ_SPECIALREG(tpidr_el1)));
279 
280 	esr = READ_SPECIALREG(esr_el1);
281 	exception = ESR_ELx_EXCEPTION(esr);
282 
283 	switch(exception) {
284 	case EXCP_FP_SIMD:
285 	case EXCP_TRAP_FP:
286 #ifdef VFP
287 		vfp_restore_state();
288 #else
289 		panic("VFP exception in userland");
290 #endif
291 		break;
292 	case EXCP_SVC:
293 		svc_handler(frame);
294 		break;
295 	case EXCP_INSN_ABORT_L:
296 	case EXCP_DATA_ABORT_L:
297 		data_abort(frame, esr, 1);
298 		break;
299 	default:
300 		panic("Unknown userland exception %x esr_el1 %lx\n", exception,
301 		    esr);
302 	}
303 }
304 
305 void
306 do_el0_error(struct trapframe *frame)
307 {
308 
309 	panic("do_el0_error");
310 }
311 
312