1/*	$NetBSD: undefined.c,v 1.22 2003/11/29 22:21:29 bjh21 Exp $	*/
2
3/*-
4 * Copyright (c) 2001 Ben Harris.
5 * Copyright (c) 1995 Mark Brinicombe.
6 * Copyright (c) 1995 Brini.
7 * All rights reserved.
8 *
9 * This code is derived from software written for Brini by Mark Brinicombe
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by Brini.
22 * 4. The name of the company nor the name of the author may be used to
23 *    endorse or promote products derived from this software without specific
24 *    prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
27 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * RiscBSD kernel project
39 *
40 * undefined.c
41 *
42 * Fault handler
43 *
44 * Created      : 06/01/95
45 */
46
47
48#include "opt_ddb.h"
49
50#include <sys/cdefs.h>
51__FBSDID("$FreeBSD: stable/11/sys/arm/arm/undefined.c 331722 2018-03-29 02:50:57Z eadler $");
52
53#include <sys/param.h>
54#include <sys/malloc.h>
55#include <sys/queue.h>
56#include <sys/signal.h>
57#include <sys/systm.h>
58#include <sys/proc.h>
59#include <sys/syslog.h>
60#include <sys/vmmeter.h>
61#include <sys/lock.h>
62#include <sys/mutex.h>
63#include <sys/signalvar.h>
64#include <sys/ptrace.h>
65#include <sys/vmmeter.h>
66#ifdef KDB
67#include <sys/kdb.h>
68#endif
69
70#include <vm/vm.h>
71#include <vm/vm_extern.h>
72
73#include <machine/armreg.h>
74#include <machine/asm.h>
75#include <machine/cpu.h>
76#include <machine/frame.h>
77#include <machine/undefined.h>
78#include <machine/trap.h>
79
80#include <machine/disassem.h>
81
82#ifdef DDB
83#include <ddb/db_output.h>
84#endif
85
86#ifdef KDB
87#include <machine/db_machdep.h>
88#endif
89
90#define	ARM_COPROC_INSN(insn)	(((insn) & (1 << 27)) != 0)
91#define	ARM_VFP_INSN(insn)	((((insn) & 0xfe000000) == 0xf2000000) || \
92    (((insn) & 0xff100000) == 0xf4000000))
93#define	ARM_COPROC(insn)	(((insn) >> 8) & 0xf)
94
95#define	THUMB_32BIT_INSN(insn)	((insn) >= 0xe800)
96#define	THUMB_COPROC_INSN(insn)	(((insn) & (3 << 26)) == (3 << 26))
97#define	THUMB_COPROC_UNDEFINED(insn) (((insn) & 0x3e << 20) == 0)
98#define	THUMB_VFP_INSN(insn)	(((insn) & (3 << 24)) == (3 << 24))
99#define	THUMB_COPROC(insn)	(((insn) >> 8) & 0xf)
100
101#define	COPROC_VFP	10
102
103static int gdb_trapper(u_int, u_int, struct trapframe *, int);
104
105LIST_HEAD(, undefined_handler) undefined_handlers[MAX_COPROCS];
106
107
108void *
109install_coproc_handler(int coproc, undef_handler_t handler)
110{
111	struct undefined_handler *uh;
112
113	KASSERT(coproc >= 0 && coproc < MAX_COPROCS, ("bad coproc"));
114	KASSERT(handler != NULL, ("handler is NULL")); /* Used to be legal. */
115
116	/* XXX: M_TEMP??? */
117	uh = malloc(sizeof(*uh), M_TEMP, M_WAITOK);
118	uh->uh_handler = handler;
119	install_coproc_handler_static(coproc, uh);
120	return uh;
121}
122
123void
124install_coproc_handler_static(int coproc, struct undefined_handler *uh)
125{
126
127	LIST_INSERT_HEAD(&undefined_handlers[coproc], uh, uh_link);
128}
129
130void
131remove_coproc_handler(void *cookie)
132{
133	struct undefined_handler *uh = cookie;
134
135	LIST_REMOVE(uh, uh_link);
136	free(uh, M_TEMP);
137}
138
139
140static int
141gdb_trapper(u_int addr, u_int insn, struct trapframe *frame, int code)
142{
143	struct thread *td;
144	ksiginfo_t ksi;
145
146	td = (curthread == NULL) ? &thread0 : curthread;
147
148	if (insn == GDB_BREAKPOINT || insn == GDB5_BREAKPOINT) {
149		if (code == FAULT_USER) {
150			ksiginfo_init_trap(&ksi);
151			ksi.ksi_signo = SIGTRAP;
152			ksi.ksi_code = TRAP_BRKPT;
153			ksi.ksi_addr = (u_int32_t *)addr;
154			trapsignal(td, &ksi);
155			return 0;
156		}
157#if 0
158#ifdef KGDB
159		return !kgdb_trap(T_BREAKPOINT, frame);
160#endif
161#endif
162	}
163	return 1;
164}
165
166static struct undefined_handler gdb_uh;
167
168void
169undefined_init(void)
170{
171	int loop;
172
173	/* Not actually necessary -- the initialiser is just NULL */
174	for (loop = 0; loop < MAX_COPROCS; ++loop)
175		LIST_INIT(&undefined_handlers[loop]);
176
177	/* Install handler for GDB breakpoints */
178	gdb_uh.uh_handler = gdb_trapper;
179	install_coproc_handler_static(0, &gdb_uh);
180}
181
182
183void
184undefinedinstruction(struct trapframe *frame)
185{
186	struct thread *td;
187	u_int fault_pc;
188	int fault_instruction;
189	int fault_code;
190	int coprocessor;
191	struct undefined_handler *uh;
192	int error;
193#ifdef VERBOSE_ARM32
194	int s;
195#endif
196	ksiginfo_t ksi;
197
198	/* Enable interrupts if they were enabled before the exception. */
199	if (__predict_true(frame->tf_spsr & PSR_I) == 0)
200		enable_interrupts(PSR_I);
201	if (__predict_true(frame->tf_spsr & PSR_F) == 0)
202		enable_interrupts(PSR_F);
203
204	PCPU_INC(cnt.v_trap);
205
206	fault_pc = frame->tf_pc;
207
208	/*
209	 * Get the current thread/proc structure or thread0/proc0 if there is
210	 * none.
211	 */
212	td = curthread == NULL ? &thread0 : curthread;
213
214	coprocessor = 0;
215	if ((frame->tf_spsr & PSR_T) == 0) {
216		/*
217		 * Make sure the program counter is correctly aligned so we
218		 * don't take an alignment fault trying to read the opcode.
219		 */
220		if (__predict_false((fault_pc & 3) != 0)) {
221			ksiginfo_init_trap(&ksi);
222			ksi.ksi_signo = SIGILL;
223			ksi.ksi_code = ILL_ILLADR;
224			ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
225			trapsignal(td, &ksi);
226			userret(td, frame);
227			return;
228		}
229
230		/*
231		 * Should use fuword() here .. but in the interests of
232		 * squeezing every bit of speed we will just use ReadWord().
233		 * We know the instruction can be read as was just executed
234		 * so this will never fail unless the kernel is screwed up
235		 * in which case it does not really matter does it ?
236		 */
237
238		fault_instruction = *(u_int32_t *)fault_pc;
239
240		/* Check for coprocessor instruction */
241
242		/*
243		 * According to the datasheets you only need to look at bit
244		 * 27 of the instruction to tell the difference between and
245		 * undefined instruction and a coprocessor instruction
246		 * following an undefined instruction trap.
247		 */
248
249		if (ARM_COPROC_INSN(fault_instruction))
250			coprocessor = ARM_COPROC(fault_instruction);
251		else {          /* check for special instructions */
252			if (ARM_VFP_INSN(fault_instruction))
253				coprocessor = COPROC_VFP; /* vfp / simd */
254		}
255	} else {
256#if __ARM_ARCH >= 7
257		fault_instruction = *(uint16_t *)fault_pc;
258		if (THUMB_32BIT_INSN(fault_instruction)) {
259			fault_instruction <<= 16;
260			fault_instruction |= *(uint16_t *)(fault_pc + 2);
261
262			/*
263			 * Is it a Coprocessor, Advanced SIMD, or
264			 * Floating-point instruction.
265			 */
266			if (THUMB_COPROC_INSN(fault_instruction)) {
267				if (THUMB_COPROC_UNDEFINED(fault_instruction)) {
268					/* undefined insn */
269				} else if (THUMB_VFP_INSN(fault_instruction))
270					coprocessor = COPROC_VFP;
271				else
272					coprocessor =
273					    THUMB_COPROC(fault_instruction);
274			}
275		}
276#else
277		/*
278		 * No support for Thumb-2 on this cpu
279		 */
280		ksiginfo_init_trap(&ksi);
281		ksi.ksi_signo = SIGILL;
282		ksi.ksi_code = ILL_ILLADR;
283		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
284		trapsignal(td, &ksi);
285		userret(td, frame);
286		return;
287#endif
288	}
289
290	if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
291		/*
292		 * Modify the fault_code to reflect the USR/SVC state at
293		 * time of fault.
294		 */
295		fault_code = FAULT_USER;
296		td->td_frame = frame;
297	} else
298		fault_code = 0;
299
300	/* OK this is were we do something about the instruction. */
301	LIST_FOREACH(uh, &undefined_handlers[coprocessor], uh_link)
302	    if (uh->uh_handler(fault_pc, fault_instruction, frame,
303			       fault_code) == 0)
304		    break;
305
306	if (fault_code & FAULT_USER) {
307		/* TODO: No support for ptrace from Thumb-2 */
308		if ((frame->tf_spsr & PSR_T) == 0 &&
309		    fault_instruction == PTRACE_BREAKPOINT) {
310			PROC_LOCK(td->td_proc);
311			_PHOLD(td->td_proc);
312			error = ptrace_clear_single_step(td);
313			_PRELE(td->td_proc);
314			PROC_UNLOCK(td->td_proc);
315			if (error != 0) {
316				ksiginfo_init_trap(&ksi);
317				ksi.ksi_signo = SIGILL;
318				ksi.ksi_code = ILL_ILLOPC;
319				ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
320				trapsignal(td, &ksi);
321			}
322			return;
323		}
324	}
325
326	if (uh == NULL && (fault_code & FAULT_USER)) {
327		/* Fault has not been handled */
328		ksiginfo_init_trap(&ksi);
329		ksi.ksi_signo = SIGILL;
330		ksi.ksi_code = ILL_ILLOPC;
331		ksi.ksi_addr = (u_int32_t *)(intptr_t) fault_pc;
332		trapsignal(td, &ksi);
333	}
334
335	if ((fault_code & FAULT_USER) == 0) {
336		if (fault_instruction == KERNEL_BREAKPOINT) {
337#ifdef KDB
338			kdb_trap(T_BREAKPOINT, 0, frame);
339#else
340			printf("No debugger in kernel.\n");
341#endif
342			return;
343		}
344		else
345			panic("Undefined instruction in kernel.\n");
346	}
347
348	userret(td, frame);
349}
350