1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* This file contains a simple exception handler. Exceptions in user
* processes are converted to signals. Exceptions in a kernel task cause
* a panic.
*/
#include "kernel/kernel.h"
#include "arch_proto.h"
#include <signal.h>
#include <string.h>
#include <assert.h>
#include "kernel/proc.h"
#include "kernel/proto.h"
#include <machine/vm.h>
struct ex_s {
char *msg;
int signum;
};
static struct ex_s ex_data[] = {
{ "Reset", 0},
{ "Undefined instruction", SIGILL},
{ "Supervisor call", 0},
{ "Prefetch Abort", SIGILL},
{ "Data Abort", SIGSEGV},
{ "Hypervisor call", 0},
{ "Interrupt", 0},
{ "Fast Interrupt", 0},
};
static void inkernel_disaster(struct proc *saved_proc,
reg_t *saved_lr, struct ex_s *ep, int is_nested);
extern int catch_pagefaults;
static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc);
static void pagefault( struct proc *pr,
reg_t *saved_lr,
int is_nested,
u32_t pagefault_addr,
u32_t pagefault_status)
{
int in_physcopy = 0, in_memset = 0;
message m_pagefault;
int err;
in_physcopy = (*saved_lr > (vir_bytes) phys_copy) &&
(*saved_lr < (vir_bytes) phys_copy_fault);
in_memset = (*saved_lr > (vir_bytes) phys_memset) &&
(*saved_lr < (vir_bytes) memset_fault);
if((is_nested || iskernelp(pr)) &&
catch_pagefaults && (in_physcopy || in_memset)) {
if (is_nested) {
if(in_physcopy) {
assert(!in_memset);
*saved_lr = (reg_t) phys_copy_fault_in_kernel;
} else {
*saved_lr = (reg_t) memset_fault_in_kernel;
}
}
else {
pr->p_reg.pc = (reg_t) phys_copy_fault;
pr->p_reg.retreg = pagefault_addr;
}
return;
}
if(is_nested) {
printf("pagefault in kernel at pc 0x%lx address 0x%lx\n",
*saved_lr, pagefault_addr);
inkernel_disaster(pr, saved_lr, NULL, is_nested);
}
/* VM can't handle page faults. */
if(pr->p_endpoint == VM_PROC_NR) {
/* Page fault we can't / don't want to
* handle.
*/
printf("pagefault for VM on CPU %d, "
"pc = 0x%x, addr = 0x%x, flags = 0x%x, is_nested %d\n",
cpuid, pr->p_reg.pc, pagefault_addr, pagefault_status,
is_nested);
proc_stacktrace(pr);
printf("pc of pagefault: 0x%lx\n", pr->p_reg.pc);
panic("pagefault in VM");
return;
}
/* Don't schedule this process until pagefault is handled. */
RTS_SET(pr, RTS_PAGEFAULT);
/* tell Vm about the pagefault */
m_pagefault.m_source = pr->p_endpoint;
m_pagefault.m_type = VM_PAGEFAULT;
m_pagefault.VPF_ADDR = pagefault_addr;
m_pagefault.VPF_FLAGS = pagefault_status;
if ((err = mini_send(pr, VM_PROC_NR,
&m_pagefault, FROM_KERNEL))) {
panic("WARNING: pagefault: mini_send returned %d\n", err);
}
return;
}
static void
data_abort(int is_nested, struct proc *pr, reg_t *saved_lr,
struct ex_s *ep, u32_t dfar, u32_t dfsr)
{
/* Extract fault status bit [0:3, 10] from DFSR */
u32_t fs = dfsr & 0x0F;
fs |= ((dfsr >> 6) & 0x10);
/* Translation and permission faults are handled as pagefaults. */
if (is_trans_fault(fs) || is_perm_fault(fs)) {
pagefault(pr, saved_lr, is_nested, dfar, dfsr);
} else if (!is_nested) {
/* A user process caused some other kind of data abort. */
int signum = SIGSEGV;
if (is_align_fault(fs)) {
signum = SIGBUS;
} else {
printf("KERNEL: unknown data abort by proc %d sending "
"SIGSEGV (dfar=0x%lx dfsr=0x%lx fs=0x%lx)\n",
proc_nr(pr), dfar, dfsr, fs);
}
cause_sig(proc_nr(pr), signum);
} else { /* is_nested */
printf("KERNEL: inkernel data abort - disaster (dfar=0x%lx "
"dfsr=0x%lx fs=0x%lx)\n", dfar, dfsr, fs);
inkernel_disaster(pr, saved_lr, ep, is_nested);
}
}
static void inkernel_disaster(struct proc *saved_proc,
reg_t *saved_lr, struct ex_s *ep,
int is_nested)
{
#if USE_SYSDEBUG
if(ep)
printf("\n%s\n", ep->msg);
printf("cpu %d is_nested = %d ", cpuid, is_nested);
if (saved_proc) {
printf("scheduled was: process %d (%s), ", saved_proc->p_endpoint, saved_proc->p_name);
printf("pc = 0x%x\n", (unsigned) saved_proc->p_reg.pc);
proc_stacktrace(saved_proc);
panic("Unhandled kernel exception");
}
/* in an early stage of boot process we don't have processes yet */
panic("exception in kernel while booting, no saved_proc yet");
#endif /* USE_SYSDEBUG */
}
void exception_handler(int is_nested, reg_t *saved_lr, int vector)
{
/* An exception or unexpected interrupt has occurred. */
struct ex_s *ep;
struct proc *saved_proc;
saved_proc = get_cpulocal_var(proc_ptr);
ep = &ex_data[vector];
assert((vir_bytes) saved_lr >= kinfo.vir_kern_start);
/*
* handle special cases for nested problems as they might be tricky or filter
* them out quickly if the traps are not nested
*/
if (is_nested) {
/*
* if a problem occurred while copying a message from userspace because
* of a wrong pointer supplied by userland, handle it the only way we
* can handle it ...
*/
if (((void*)*saved_lr >= (void*)copy_msg_to_user &&
(void*)*saved_lr <= (void*)__copy_msg_to_user_end) ||
((void*)*saved_lr >= (void*)copy_msg_from_user &&
(void*)*saved_lr <= (void*)__copy_msg_from_user_end)) {
switch(vector) {
/* these error are expected */
case DATA_ABORT_VECTOR:
*saved_lr = (reg_t) __user_copy_msg_pointer_failure;
return;
default:
panic("Copy involving a user pointer failed unexpectedly!");
}
}
}
if (vector == DATA_ABORT_VECTOR) {
data_abort(is_nested, saved_proc, saved_lr, ep, read_dfar(), read_dfsr());
return;
}
if (!is_nested && vector == PREFETCH_ABORT_VECTOR) {
static int warned = FALSE;
reg_t ifar = read_ifar(), ifsr = read_ifsr();
/* The saved_lr is the instruction we're going to execute after
* the fault is handled; IFAR is the address that pagefaulted
* while fetching the instruction. As far as we know the two
* should be the same, if not this assumption will lead to very
* hard to debug problems (instruction executing being off by one)
* and this assumption needs re-examining.
*
* UPDATE: at least qemu-linaro does in fact sometimes generate faults
* with LR and IFAR differing by as many as 64 bytes. While the page
* fault resolution code below handles this case just fine, the cause
* of this behavior is unknown. We have not yet seen the same on
* actual hardware, which is why we warn about this problem once.
*/
if (*saved_lr != ifar && !warned) {
printf("KERNEL: prefetch abort with differing IFAR and LR\n");
printf("KERNEL: IFSR %"PRIx32" IFAR %"PRIx32" LR %"PRIx32" in "
"%s/%d\n", ifsr, ifar, *saved_lr, saved_proc->p_name,
saved_proc->p_endpoint);
warned = TRUE;
}
pagefault(saved_proc, saved_lr, is_nested, ifar, ifsr);
return;
}
/* If an exception occurs while running a process, the is_nested variable
* will be zero. Exceptions in interrupt handlers or system traps will make
* is_nested non-zero.
*/
if (is_nested == 0 && ! iskernelp(saved_proc)) {
cause_sig(proc_nr(saved_proc), ep->signum);
return;
}
/* Exception in system code. This is not supposed to happen. */
inkernel_disaster(saved_proc, saved_lr, ep, is_nested);
panic("return from inkernel_disaster");
}
#if USE_SYSDEBUG
/*===========================================================================*
* proc_stacktrace_execute *
*===========================================================================*/
static void proc_stacktrace_execute(struct proc *whichproc, reg_t v_bp, reg_t pc)
{
printf("%-8.8s %6d 0x%lx \n",
whichproc->p_name, whichproc->p_endpoint, pc);
}
#endif
void proc_stacktrace(struct proc *whichproc)
{
#if USE_SYSDEBUG
proc_stacktrace_execute(whichproc, whichproc->p_reg.fp, whichproc->p_reg.pc);
#endif /* USE_SYSDEBUG */
}
void enable_fpu_exception(void)
{
}
void disable_fpu_exception(void)
{
}
|