1 /* This file contains a simple exception handler. Exceptions in user
2 * processes are converted to signals. Exceptions in a kernel task cause
6 #include "kernel/kernel.h"
7 #include "arch_proto.h"
11 #include "kernel/proc.h"
12 #include "kernel/proto.h"
13 #include <machine/vm.h>
20 static struct ex_s ex_data
[] = {
22 { "Undefined instruction", SIGILL
},
23 { "Supervisor call", 0},
24 { "Prefetch Abort", SIGILL
},
25 { "Data Abort", SIGSEGV
},
26 { "Hypervisor call", 0},
28 { "Fast Interrupt", 0},
31 static void inkernel_disaster(struct proc
*saved_proc
,
32 reg_t
*saved_lr
, struct ex_s
*ep
, int is_nested
);
34 extern int catch_pagefaults
;
36 static void proc_stacktrace_execute(struct proc
*whichproc
, reg_t v_bp
, reg_t pc
);
38 static void pagefault( struct proc
*pr
,
42 u32_t pagefault_status
)
44 int in_physcopy
= 0, in_memset
= 0;
49 in_physcopy
= (*saved_lr
> (vir_bytes
) phys_copy
) &&
50 (*saved_lr
< (vir_bytes
) phys_copy_fault
);
52 in_memset
= (*saved_lr
> (vir_bytes
) phys_memset
) &&
53 (*saved_lr
< (vir_bytes
) memset_fault
);
55 if((is_nested
|| iskernelp(pr
)) &&
56 catch_pagefaults
&& (in_physcopy
|| in_memset
)) {
60 *saved_lr
= (reg_t
) phys_copy_fault_in_kernel
;
62 *saved_lr
= (reg_t
) memset_fault_in_kernel
;
66 pr
->p_reg
.pc
= (reg_t
) phys_copy_fault
;
67 pr
->p_reg
.retreg
= pagefault_addr
;
74 printf("pagefault in kernel at pc 0x%lx address 0x%lx\n",
75 *saved_lr
, pagefault_addr
);
76 inkernel_disaster(pr
, saved_lr
, NULL
, is_nested
);
79 /* VM can't handle page faults. */
80 if(pr
->p_endpoint
== VM_PROC_NR
) {
81 /* Page fault we can't / don't want to
84 printf("pagefault for VM on CPU %d, "
85 "pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n",
86 cpuid
, pr
->p_reg
.pc
, pagefault_addr
, pagefault_status
,
89 printf("pc of pagefault: 0x%lx\n", pr
->p_reg
.pc
);
90 panic("pagefault in VM");
95 /* Don't schedule this process until pagefault is handled. */
96 RTS_SET(pr
, RTS_PAGEFAULT
);
98 /* tell Vm about the pagefault */
99 m_pagefault
.m_source
= pr
->p_endpoint
;
100 m_pagefault
.m_type
= VM_PAGEFAULT
;
101 m_pagefault
.VPF_ADDR
= pagefault_addr
;
102 m_pagefault
.VPF_FLAGS
= pagefault_status
;
104 if ((err
= mini_send(pr
, VM_PROC_NR
,
105 &m_pagefault
, FROM_KERNEL
))) {
106 panic("WARNING: pagefault: mini_send returned %d\n", err
);
113 data_abort(int is_nested
, struct proc
*pr
, reg_t
*saved_lr
,
114 struct ex_s
*ep
, u32_t dfar
, u32_t dfsr
)
116 /* Extract fault status bit [0:3, 10] from DFSR */
117 u32_t fs
= dfsr
& 0x0F;
118 fs
|= ((dfsr
>> 6) & 0x10);
120 /* Translation and permission faults are handled as pagefaults. */
121 if (is_trans_fault(fs
) || is_perm_fault(fs
)) {
122 pagefault(pr
, saved_lr
, is_nested
, dfar
, dfsr
);
123 } else if (!is_nested
) {
124 /* A user process caused some other kind of data abort. */
125 int signum
= SIGSEGV
;
127 if (is_align_fault(fs
)) {
130 printf("KERNEL: unknown data abort by proc %d sending "
131 "SIGSEGV (dfar=0x%lx dfsr=0x%lx fs=0x%lx)\n",
132 proc_nr(pr
), dfar
, dfsr
, fs
);
134 cause_sig(proc_nr(pr
), signum
);
135 } else { /* is_nested */
136 printf("KERNEL: inkernel data abort - disaster (dfar=0x%lx "
137 "dfsr=0x%lx fs=0x%lx)\n", dfar
, dfsr
, fs
);
138 inkernel_disaster(pr
, saved_lr
, ep
, is_nested
);
142 static void inkernel_disaster(struct proc
*saved_proc
,
143 reg_t
*saved_lr
, struct ex_s
*ep
,
148 printf("\n%s\n", ep
->msg
);
150 printf("cpu %d is_nested = %d ", cpuid
, is_nested
);
153 printf("scheduled was: process %d (%s), ", saved_proc
->p_endpoint
, saved_proc
->p_name
);
154 printf("pc = 0x%x\n", (unsigned) saved_proc
->p_reg
.pc
);
155 proc_stacktrace(saved_proc
);
157 panic("Unhandled kernel exception");
160 /* in an early stage of boot process we don't have processes yet */
161 panic("exception in kernel while booting, no saved_proc yet");
163 #endif /* USE_SYSDEBUG */
166 void exception_handler(int is_nested
, reg_t
*saved_lr
, int vector
)
168 /* An exception or unexpected interrupt has occurred. */
170 struct proc
*saved_proc
;
172 saved_proc
= get_cpulocal_var(proc_ptr
);
174 ep
= &ex_data
[vector
];
176 assert((vir_bytes
) saved_lr
>= kinfo
.vir_kern_start
);
179 * handle special cases for nested problems as they might be tricky or filter
180 * them out quickly if the traps are not nested
184 * if a problem occurred while copying a message from userspace because
185 * of a wrong pointer supplied by userland, handle it the only way we
188 if (((void*)*saved_lr
>= (void*)copy_msg_to_user
&&
189 (void*)*saved_lr
<= (void*)__copy_msg_to_user_end
) ||
190 ((void*)*saved_lr
>= (void*)copy_msg_from_user
&&
191 (void*)*saved_lr
<= (void*)__copy_msg_from_user_end
)) {
193 /* these error are expected */
194 case DATA_ABORT_VECTOR
:
195 *saved_lr
= (reg_t
) __user_copy_msg_pointer_failure
;
198 panic("Copy involving a user pointer failed unexpectedly!");
203 if (vector
== DATA_ABORT_VECTOR
) {
204 data_abort(is_nested
, saved_proc
, saved_lr
, ep
, read_dfar(), read_dfsr());
208 if (!is_nested
&& vector
== PREFETCH_ABORT_VECTOR
) {
209 static int warned
= FALSE
;
210 reg_t ifar
= read_ifar(), ifsr
= read_ifsr();
212 /* The saved_lr is the instruction we're going to execute after
213 * the fault is handled; IFAR is the address that pagefaulted
214 * while fetching the instruction. As far as we know the two
215 * should be the same, if not this assumption will lead to very
216 * hard to debug problems (instruction executing being off by one)
217 * and this assumption needs re-examining.
219 * UPDATE: at least qemu-linaro does in fact sometimes generate faults
220 * with LR and IFAR differing by as many as 64 bytes. While the page
221 * fault resolution code below handles this case just fine, the cause
222 * of this behavior is unknown. We have not yet seen the same on
223 * actual hardware, which is why we warn about this problem once.
225 if (*saved_lr
!= ifar
&& !warned
) {
226 printf("KERNEL: prefetch abort with differing IFAR and LR\n");
227 printf("KERNEL: IFSR %"PRIx32
" IFAR %"PRIx32
" LR %"PRIx32
" in "
228 "%s/%d\n", ifsr
, ifar
, *saved_lr
, saved_proc
->p_name
,
229 saved_proc
->p_endpoint
);
232 pagefault(saved_proc
, saved_lr
, is_nested
, ifar
, ifsr
);
236 /* If an exception occurs while running a process, the is_nested variable
237 * will be zero. Exceptions in interrupt handlers or system traps will make
238 * is_nested non-zero.
240 if (is_nested
== 0 && ! iskernelp(saved_proc
)) {
241 cause_sig(proc_nr(saved_proc
), ep
->signum
);
245 /* Exception in system code. This is not supposed to happen. */
246 inkernel_disaster(saved_proc
, saved_lr
, ep
, is_nested
);
248 panic("return from inkernel_disaster");
252 /*===========================================================================*
253 * proc_stacktrace_execute *
254 *===========================================================================*/
255 static void proc_stacktrace_execute(struct proc
*whichproc
, reg_t v_bp
, reg_t pc
)
257 printf("%-8.8s %6d 0x%lx \n",
258 whichproc
->p_name
, whichproc
->p_endpoint
, pc
);
262 void proc_stacktrace(struct proc
*whichproc
)
265 proc_stacktrace_execute(whichproc
, whichproc
->p_reg
.fp
, whichproc
->p_reg
.pc
);
266 #endif /* USE_SYSDEBUG */
269 void enable_fpu_exception(void)
273 void disable_fpu_exception(void)