xref: /minix3/minix/kernel/arch/earm/exception.c (revision 0dd719f1bd28ca1fd3474cf2d18d9e041c5ec028)
1433d6423SLionel Sambuc /* This file contains a simple exception handler.  Exceptions in user
2433d6423SLionel Sambuc  * processes are converted to signals. Exceptions in a kernel task cause
3433d6423SLionel Sambuc  * a panic.
4433d6423SLionel Sambuc  */
5433d6423SLionel Sambuc 
6433d6423SLionel Sambuc #include "kernel/kernel.h"
7433d6423SLionel Sambuc #include "arch_proto.h"
8433d6423SLionel Sambuc #include <signal.h>
9433d6423SLionel Sambuc #include <string.h>
10433d6423SLionel Sambuc #include <assert.h>
11433d6423SLionel Sambuc #include "kernel/proc.h"
12433d6423SLionel Sambuc #include "kernel/proto.h"
13433d6423SLionel Sambuc #include <machine/vm.h>
14433d6423SLionel Sambuc 
15433d6423SLionel Sambuc struct ex_s {
16433d6423SLionel Sambuc 	char *msg;
17433d6423SLionel Sambuc 	int signum;
18433d6423SLionel Sambuc };
19433d6423SLionel Sambuc 
20433d6423SLionel Sambuc static struct ex_s ex_data[] = {
21433d6423SLionel Sambuc 	{ "Reset", 0},
22433d6423SLionel Sambuc 	{ "Undefined instruction", SIGILL},
23433d6423SLionel Sambuc 	{ "Supervisor call", 0},
24433d6423SLionel Sambuc 	{ "Prefetch Abort", SIGILL},
25433d6423SLionel Sambuc 	{ "Data Abort", SIGSEGV},
26433d6423SLionel Sambuc 	{ "Hypervisor call", 0},
27433d6423SLionel Sambuc 	{ "Interrupt", 0},
28433d6423SLionel Sambuc 	{ "Fast Interrupt", 0},
29433d6423SLionel Sambuc };
30433d6423SLionel Sambuc 
31433d6423SLionel Sambuc static void inkernel_disaster(struct proc *saved_proc,
32433d6423SLionel Sambuc 	reg_t *saved_lr, struct ex_s *ep, int is_nested);
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc extern int catch_pagefaults;
35433d6423SLionel Sambuc 
36433d6423SLionel Sambuc static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc);
37433d6423SLionel Sambuc 
pagefault(struct proc * pr,reg_t * saved_lr,int is_nested,u32_t pagefault_addr,u32_t pagefault_status)38433d6423SLionel Sambuc static void pagefault( struct proc *pr,
39433d6423SLionel Sambuc 			reg_t *saved_lr,
40433d6423SLionel Sambuc 			int is_nested,
41433d6423SLionel Sambuc 			u32_t pagefault_addr,
42433d6423SLionel Sambuc 			u32_t pagefault_status)
43433d6423SLionel Sambuc {
44433d6423SLionel Sambuc 	int in_physcopy = 0, in_memset = 0;
45433d6423SLionel Sambuc 
46433d6423SLionel Sambuc 	message m_pagefault;
47433d6423SLionel Sambuc 	int err;
48433d6423SLionel Sambuc 
49433d6423SLionel Sambuc 	in_physcopy = (*saved_lr > (vir_bytes) phys_copy) &&
50433d6423SLionel Sambuc 	   (*saved_lr < (vir_bytes) phys_copy_fault);
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc 	in_memset = (*saved_lr > (vir_bytes) phys_memset) &&
53433d6423SLionel Sambuc 	   (*saved_lr < (vir_bytes) memset_fault);
54433d6423SLionel Sambuc 
55433d6423SLionel Sambuc 	if((is_nested || iskernelp(pr)) &&
56433d6423SLionel Sambuc 		catch_pagefaults && (in_physcopy || in_memset)) {
57433d6423SLionel Sambuc 		if (is_nested) {
58433d6423SLionel Sambuc 			if(in_physcopy) {
59433d6423SLionel Sambuc 				assert(!in_memset);
60433d6423SLionel Sambuc 				*saved_lr = (reg_t) phys_copy_fault_in_kernel;
61433d6423SLionel Sambuc 			} else {
62433d6423SLionel Sambuc 				*saved_lr = (reg_t) memset_fault_in_kernel;
63433d6423SLionel Sambuc 			}
64433d6423SLionel Sambuc 		}
65433d6423SLionel Sambuc 		else {
66433d6423SLionel Sambuc 			pr->p_reg.pc = (reg_t) phys_copy_fault;
67433d6423SLionel Sambuc 			pr->p_reg.retreg = pagefault_addr;
68433d6423SLionel Sambuc 		}
69433d6423SLionel Sambuc 
70433d6423SLionel Sambuc 		return;
71433d6423SLionel Sambuc 	}
72433d6423SLionel Sambuc 
73433d6423SLionel Sambuc 	if(is_nested) {
74433d6423SLionel Sambuc 		printf("pagefault in kernel at pc 0x%lx address 0x%lx\n",
75433d6423SLionel Sambuc 			*saved_lr, pagefault_addr);
76433d6423SLionel Sambuc 		inkernel_disaster(pr, saved_lr, NULL, is_nested);
77433d6423SLionel Sambuc 	}
78433d6423SLionel Sambuc 
79433d6423SLionel Sambuc 	/* VM can't handle page faults. */
80433d6423SLionel Sambuc 	if(pr->p_endpoint == VM_PROC_NR) {
81433d6423SLionel Sambuc 		/* Page fault we can't / don't want to
82433d6423SLionel Sambuc 		 * handle.
83433d6423SLionel Sambuc 		 */
84433d6423SLionel Sambuc 		printf("pagefault for VM on CPU %d, "
85433d6423SLionel Sambuc 			"pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n",
86433d6423SLionel Sambuc 			cpuid, pr->p_reg.pc, pagefault_addr, pagefault_status,
87433d6423SLionel Sambuc 			is_nested);
88433d6423SLionel Sambuc 		proc_stacktrace(pr);
89433d6423SLionel Sambuc 		printf("pc of pagefault: 0x%lx\n", pr->p_reg.pc);
90433d6423SLionel Sambuc 		panic("pagefault in VM");
91433d6423SLionel Sambuc 
92433d6423SLionel Sambuc 		return;
93433d6423SLionel Sambuc 	}
94433d6423SLionel Sambuc 
95433d6423SLionel Sambuc 	/* Don't schedule this process until pagefault is handled. */
96433d6423SLionel Sambuc 	RTS_SET(pr, RTS_PAGEFAULT);
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc 	/* tell Vm about the pagefault */
99433d6423SLionel Sambuc 	m_pagefault.m_source = pr->p_endpoint;
100433d6423SLionel Sambuc 	m_pagefault.m_type   = VM_PAGEFAULT;
101433d6423SLionel Sambuc 	m_pagefault.VPF_ADDR = pagefault_addr;
102433d6423SLionel Sambuc 	m_pagefault.VPF_FLAGS = pagefault_status;
103433d6423SLionel Sambuc 
104433d6423SLionel Sambuc 	if ((err = mini_send(pr, VM_PROC_NR,
105433d6423SLionel Sambuc 					&m_pagefault, FROM_KERNEL))) {
106433d6423SLionel Sambuc 		panic("WARNING: pagefault: mini_send returned %d\n", err);
107433d6423SLionel Sambuc 	}
108433d6423SLionel Sambuc 
109433d6423SLionel Sambuc 	return;
110433d6423SLionel Sambuc }
111433d6423SLionel Sambuc 
1127c3424c2SArne Welzel static void
data_abort(int is_nested,struct proc * pr,reg_t * saved_lr,struct ex_s * ep,u32_t dfar,u32_t dfsr)1137c3424c2SArne Welzel data_abort(int is_nested, struct proc *pr, reg_t *saved_lr,
1147c3424c2SArne Welzel 		       struct ex_s *ep, u32_t dfar, u32_t dfsr)
1157c3424c2SArne Welzel {
1167c3424c2SArne Welzel 	/* Extract fault status bit [0:3, 10] from DFSR */
1177c3424c2SArne Welzel 	u32_t fs = dfsr & 0x0F;
1187c3424c2SArne Welzel 	fs |= ((dfsr >> 6) & 0x10);
119*0dd719f1SArne Welzel 
120*0dd719f1SArne Welzel 	 /* Translation and permission faults are handled as pagefaults. */
121*0dd719f1SArne Welzel 	if (is_trans_fault(fs) || is_perm_fault(fs)) {
122*0dd719f1SArne Welzel 		pagefault(pr, saved_lr, is_nested, dfar, dfsr);
123*0dd719f1SArne Welzel 	} else if (!is_nested) {
124*0dd719f1SArne Welzel 		/* A user process caused some other kind of data abort. */
125*0dd719f1SArne Welzel 		int signum = SIGSEGV;
126*0dd719f1SArne Welzel 
127*0dd719f1SArne Welzel 		if (is_align_fault(fs)) {
128*0dd719f1SArne Welzel 			signum = SIGBUS;
129*0dd719f1SArne Welzel 		} else {
130*0dd719f1SArne Welzel 			printf("KERNEL: unknown data abort by proc %d sending "
131*0dd719f1SArne Welzel 			       "SIGSEGV (dfar=0x%lx dfsr=0x%lx fs=0x%lx)\n",
132*0dd719f1SArne Welzel 			       proc_nr(pr), dfar, dfsr, fs);
133*0dd719f1SArne Welzel 		}
134*0dd719f1SArne Welzel 		cause_sig(proc_nr(pr), signum);
135*0dd719f1SArne Welzel 	} else { /* is_nested */
136*0dd719f1SArne Welzel 		printf("KERNEL: inkernel data abort - disaster (dfar=0x%lx "
137*0dd719f1SArne Welzel 		       "dfsr=0x%lx fs=0x%lx)\n", dfar, dfsr, fs);
1387c3424c2SArne Welzel 		inkernel_disaster(pr, saved_lr, ep, is_nested);
1397c3424c2SArne Welzel 	}
1407c3424c2SArne Welzel }
1417c3424c2SArne Welzel 
inkernel_disaster(struct proc * saved_proc,reg_t * saved_lr,struct ex_s * ep,int is_nested)142433d6423SLionel Sambuc static void inkernel_disaster(struct proc *saved_proc,
143433d6423SLionel Sambuc 	reg_t *saved_lr, struct ex_s *ep,
144433d6423SLionel Sambuc 	int is_nested)
145433d6423SLionel Sambuc {
146433d6423SLionel Sambuc #if USE_SYSDEBUG
147433d6423SLionel Sambuc   if(ep)
148433d6423SLionel Sambuc 	printf("\n%s\n", ep->msg);
149433d6423SLionel Sambuc 
150433d6423SLionel Sambuc   printf("cpu %d is_nested = %d ", cpuid, is_nested);
151433d6423SLionel Sambuc 
152433d6423SLionel Sambuc   if (saved_proc) {
153433d6423SLionel Sambuc 	  printf("scheduled was: process %d (%s), ", saved_proc->p_endpoint, saved_proc->p_name);
154433d6423SLionel Sambuc 	  printf("pc = 0x%x\n", (unsigned) saved_proc->p_reg.pc);
155433d6423SLionel Sambuc 	  proc_stacktrace(saved_proc);
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc 	  panic("Unhandled kernel exception");
158433d6423SLionel Sambuc   }
159433d6423SLionel Sambuc 
160433d6423SLionel Sambuc   /* in an early stage of boot process we don't have processes yet */
161433d6423SLionel Sambuc   panic("exception in kernel while booting, no saved_proc yet");
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc #endif /* USE_SYSDEBUG */
164433d6423SLionel Sambuc }
165433d6423SLionel Sambuc 
exception_handler(int is_nested,reg_t * saved_lr,int vector)166433d6423SLionel Sambuc void exception_handler(int is_nested, reg_t *saved_lr, int vector)
167433d6423SLionel Sambuc {
168433d6423SLionel Sambuc /* An exception or unexpected interrupt has occurred. */
169433d6423SLionel Sambuc   struct ex_s *ep;
170433d6423SLionel Sambuc   struct proc *saved_proc;
171433d6423SLionel Sambuc 
172433d6423SLionel Sambuc   saved_proc = get_cpulocal_var(proc_ptr);
173433d6423SLionel Sambuc 
174433d6423SLionel Sambuc   ep = &ex_data[vector];
175433d6423SLionel Sambuc 
176433d6423SLionel Sambuc   assert((vir_bytes) saved_lr >= kinfo.vir_kern_start);
177433d6423SLionel Sambuc 
178433d6423SLionel Sambuc   /*
179433d6423SLionel Sambuc    * handle special cases for nested problems as they might be tricky or filter
180433d6423SLionel Sambuc    * them out quickly if the traps are not nested
181433d6423SLionel Sambuc    */
182433d6423SLionel Sambuc   if (is_nested) {
183433d6423SLionel Sambuc 	/*
184433d6423SLionel Sambuc 	 * if a problem occurred while copying a message from userspace because
185433d6423SLionel Sambuc 	 * of a wrong pointer supplied by userland, handle it the only way we
186433d6423SLionel Sambuc 	 * can handle it ...
187433d6423SLionel Sambuc 	 */
188433d6423SLionel Sambuc 	if (((void*)*saved_lr >= (void*)copy_msg_to_user &&
189433d6423SLionel Sambuc 			(void*)*saved_lr <= (void*)__copy_msg_to_user_end) ||
190433d6423SLionel Sambuc 			((void*)*saved_lr >= (void*)copy_msg_from_user &&
191433d6423SLionel Sambuc 			(void*)*saved_lr <= (void*)__copy_msg_from_user_end)) {
192433d6423SLionel Sambuc 		switch(vector) {
193433d6423SLionel Sambuc 		/* these error are expected */
194433d6423SLionel Sambuc 		case DATA_ABORT_VECTOR:
195433d6423SLionel Sambuc 			*saved_lr = (reg_t) __user_copy_msg_pointer_failure;
196433d6423SLionel Sambuc 			return;
197433d6423SLionel Sambuc 		default:
198433d6423SLionel Sambuc 			panic("Copy involving a user pointer failed unexpectedly!");
199433d6423SLionel Sambuc 		}
200433d6423SLionel Sambuc 	}
201433d6423SLionel Sambuc   }
202433d6423SLionel Sambuc 
203433d6423SLionel Sambuc   if (vector == DATA_ABORT_VECTOR) {
2047c3424c2SArne Welzel 	data_abort(is_nested, saved_proc, saved_lr, ep, read_dfar(), read_dfsr());
205433d6423SLionel Sambuc 	return;
206433d6423SLionel Sambuc   }
207433d6423SLionel Sambuc 
208433d6423SLionel Sambuc   if (!is_nested && vector == PREFETCH_ABORT_VECTOR) {
209a3975fbcSDavid van Moolenbroek 	static int warned = FALSE;
210433d6423SLionel Sambuc 	reg_t ifar = read_ifar(), ifsr = read_ifsr();
211433d6423SLionel Sambuc 
212433d6423SLionel Sambuc 	/* The saved_lr is the instruction we're going to execute after
213433d6423SLionel Sambuc 	 * the fault is handled; IFAR is the address that pagefaulted
214433d6423SLionel Sambuc 	 * while fetching the instruction. As far as we know the two
215433d6423SLionel Sambuc 	 * should be the same, if not this assumption will lead to very
216433d6423SLionel Sambuc 	 * hard to debug problems (instruction executing being off by one)
217a3975fbcSDavid van Moolenbroek 	 * and this assumption needs re-examining.
218a3975fbcSDavid van Moolenbroek 	 *
219a3975fbcSDavid van Moolenbroek 	 * UPDATE: at least qemu-linaro does in fact sometimes generate faults
220a3975fbcSDavid van Moolenbroek 	 * with LR and IFAR differing by as many as 64 bytes.  While the page
221a3975fbcSDavid van Moolenbroek 	 * fault resolution code below handles this case just fine, the cause
222a3975fbcSDavid van Moolenbroek 	 * of this behavior is unknown.  We have not yet seen the same on
223a3975fbcSDavid van Moolenbroek 	 * actual hardware, which is why we warn about this problem once.
224433d6423SLionel Sambuc 	 */
225a3975fbcSDavid van Moolenbroek 	if (*saved_lr != ifar && !warned) {
226a3975fbcSDavid van Moolenbroek 		printf("KERNEL: prefetch abort with differing IFAR and LR\n");
227a3975fbcSDavid van Moolenbroek 		printf("KERNEL: IFSR %"PRIx32" IFAR %"PRIx32" LR %"PRIx32" in "
228a3975fbcSDavid van Moolenbroek 		    "%s/%d\n", ifsr, ifar, *saved_lr, saved_proc->p_name,
229a3975fbcSDavid van Moolenbroek 		    saved_proc->p_endpoint);
230a3975fbcSDavid van Moolenbroek 		warned = TRUE;
231a3975fbcSDavid van Moolenbroek 	}
232433d6423SLionel Sambuc 	pagefault(saved_proc, saved_lr, is_nested, ifar, ifsr);
233433d6423SLionel Sambuc 	return;
234433d6423SLionel Sambuc   }
235433d6423SLionel Sambuc 
236433d6423SLionel Sambuc   /* If an exception occurs while running a process, the is_nested variable
237433d6423SLionel Sambuc    * will be zero. Exceptions in interrupt handlers or system traps will make
238433d6423SLionel Sambuc    * is_nested non-zero.
239433d6423SLionel Sambuc    */
240433d6423SLionel Sambuc   if (is_nested == 0 && ! iskernelp(saved_proc)) {
241433d6423SLionel Sambuc 	cause_sig(proc_nr(saved_proc), ep->signum);
242433d6423SLionel Sambuc 	return;
243433d6423SLionel Sambuc   }
244433d6423SLionel Sambuc 
245433d6423SLionel Sambuc   /* Exception in system code. This is not supposed to happen. */
246433d6423SLionel Sambuc   inkernel_disaster(saved_proc, saved_lr, ep, is_nested);
247433d6423SLionel Sambuc 
248433d6423SLionel Sambuc   panic("return from inkernel_disaster");
249433d6423SLionel Sambuc }
250433d6423SLionel Sambuc 
251433d6423SLionel Sambuc #if USE_SYSDEBUG
252433d6423SLionel Sambuc /*===========================================================================*
253433d6423SLionel Sambuc  *				proc_stacktrace_execute			     *
254433d6423SLionel Sambuc  *===========================================================================*/
proc_stacktrace_execute(struct proc * whichproc,reg_t v_bp,reg_t pc)255433d6423SLionel Sambuc static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc)
256433d6423SLionel Sambuc {
257433d6423SLionel Sambuc 	printf("%-8.8s %6d 0x%lx \n",
258433d6423SLionel Sambuc 		whichproc->p_name, whichproc->p_endpoint, pc);
259433d6423SLionel Sambuc }
260433d6423SLionel Sambuc #endif
261433d6423SLionel Sambuc 
proc_stacktrace(struct proc * whichproc)262433d6423SLionel Sambuc void proc_stacktrace(struct proc *whichproc)
263433d6423SLionel Sambuc {
264433d6423SLionel Sambuc #if USE_SYSDEBUG
265433d6423SLionel Sambuc 	proc_stacktrace_execute(whichproc, whichproc->p_reg.fp, whichproc->p_reg.pc);
266433d6423SLionel Sambuc #endif /* USE_SYSDEBUG */
267433d6423SLionel Sambuc }
268433d6423SLionel Sambuc 
enable_fpu_exception(void)269433d6423SLionel Sambuc void enable_fpu_exception(void)
270433d6423SLionel Sambuc {
271433d6423SLionel Sambuc }
272433d6423SLionel Sambuc 
disable_fpu_exception(void)273433d6423SLionel Sambuc void disable_fpu_exception(void)
274433d6423SLionel Sambuc {
275433d6423SLionel Sambuc }
276