fault.c revision 1.1.2.4 1 1.1.2.4 thorpej /* $NetBSD: fault.c,v 1.1.2.4 2002/01/10 19:37:48 thorpej Exp $ */
2 1.1.2.2 lukem
3 1.1.2.2 lukem /*
4 1.1.2.2 lukem * Copyright (c) 1994-1997 Mark Brinicombe.
5 1.1.2.2 lukem * Copyright (c) 1994 Brini.
6 1.1.2.2 lukem * All rights reserved.
7 1.1.2.2 lukem *
8 1.1.2.2 lukem * This code is derived from software written for Brini by Mark Brinicombe
9 1.1.2.2 lukem *
10 1.1.2.2 lukem * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 lukem * modification, are permitted provided that the following conditions
12 1.1.2.2 lukem * are met:
13 1.1.2.2 lukem * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 lukem * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 lukem * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 lukem * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 lukem * documentation and/or other materials provided with the distribution.
18 1.1.2.2 lukem * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 lukem * must display the following acknowledgement:
20 1.1.2.2 lukem * This product includes software developed by Brini.
21 1.1.2.2 lukem * 4. The name of the company nor the name of the author may be used to
22 1.1.2.2 lukem * endorse or promote products derived from this software without specific
23 1.1.2.2 lukem * prior written permission.
24 1.1.2.2 lukem *
25 1.1.2.2 lukem * THIS SOFTWARE IS PROVIDED BY BRINI ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 1.1.2.2 lukem * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 1.1.2.2 lukem * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.1.2.2 lukem * IN NO EVENT SHALL BRINI OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 1.1.2.2 lukem * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 1.1.2.2 lukem * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 1.1.2.2 lukem * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1.2.2 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1.2.2 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1.2.2 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1.2.2 lukem * SUCH DAMAGE.
36 1.1.2.2 lukem *
37 1.1.2.2 lukem * RiscBSD kernel project
38 1.1.2.2 lukem *
39 1.1.2.2 lukem * fault.c
40 1.1.2.2 lukem *
41 1.1.2.2 lukem * Fault handlers
42 1.1.2.2 lukem *
43 1.1.2.2 lukem * Created : 28/11/94
44 1.1.2.2 lukem */
45 1.1.2.2 lukem
46 1.1.2.2 lukem #include "opt_ddb.h"
47 1.1.2.2 lukem #include "opt_pmap_debug.h"
48 1.1.2.2 lukem
49 1.1.2.2 lukem #include <sys/types.h>
50 1.1.2.2 lukem #include <sys/param.h>
51 1.1.2.2 lukem #include <sys/systm.h>
52 1.1.2.2 lukem #include <sys/proc.h>
53 1.1.2.2 lukem #include <sys/user.h>
54 1.1.2.2 lukem #include <sys/kernel.h>
55 1.1.2.2 lukem
56 1.1.2.2 lukem #include <uvm/uvm_extern.h>
57 1.1.2.2 lukem
58 1.1.2.2 lukem #include <machine/frame.h>
59 1.1.2.4 thorpej #include <arm/arm32/katelib.h>
60 1.1.2.2 lukem #include <machine/cpu.h>
61 1.1.2.3 thorpej #include <machine/intr.h>
62 1.1.2.2 lukem #ifdef DDB
63 1.1.2.2 lukem #include <machine/db_machdep.h>
64 1.1.2.2 lukem #endif
65 1.1.2.2 lukem
66 1.1.2.2 lukem #include <arch/arm/arm/disassem.h>
67 1.1.2.4 thorpej #include <arm/arm32/machdep.h>
68 1.1.2.4 thorpej
69 1.1.2.2 lukem int cowfault __P((vaddr_t));
70 1.1.2.2 lukem int fetchuserword __P((u_int address, u_int *location));
71 1.1.2.2 lukem extern char fusubailout[];
72 1.1.2.2 lukem
73 1.1.2.4 thorpej static void report_abort __P((const char *, u_int, u_int, u_int));
74 1.1.2.4 thorpej
75 1.1.2.2 lukem /* Abort code */
76 1.1.2.2 lukem
77 1.1.2.2 lukem /* Define text descriptions of the different aborts */
78 1.1.2.2 lukem
79 1.1.2.2 lukem static const char *aborts[16] = {
80 1.1.2.2 lukem "Write buffer fault",
81 1.1.2.2 lukem "Alignment fault",
82 1.1.2.2 lukem "Write buffer fault",
83 1.1.2.2 lukem "Alignment fault",
84 1.1.2.2 lukem "Bus error (LF section)",
85 1.1.2.2 lukem "Translation fault (section)",
86 1.1.2.2 lukem "Bus error (page)",
87 1.1.2.2 lukem "Translation fault (page)",
88 1.1.2.2 lukem "Bus error (section)",
89 1.1.2.2 lukem "Domain error (section)",
90 1.1.2.2 lukem "Bus error (page)",
91 1.1.2.2 lukem "Domain error (page)",
92 1.1.2.2 lukem "Bus error trans (L1)",
93 1.1.2.2 lukem "Permission error (section)",
94 1.1.2.2 lukem "Bus error trans (L2)",
95 1.1.2.2 lukem "Permission error (page)"
96 1.1.2.2 lukem };
97 1.1.2.2 lukem
98 1.1.2.4 thorpej static void
99 1.1.2.2 lukem report_abort(prefix, fault_status, fault_address, fault_pc)
100 1.1.2.2 lukem const char *prefix;
101 1.1.2.2 lukem u_int fault_status;
102 1.1.2.2 lukem u_int fault_address;
103 1.1.2.2 lukem u_int fault_pc;
104 1.1.2.2 lukem {
105 1.1.2.2 lukem #ifndef DEBUG
106 1.1.2.2 lukem if (prefix == NULL) {
107 1.1.2.2 lukem #endif
108 1.1.2.2 lukem if (prefix)
109 1.1.2.2 lukem printf("%s ", prefix);
110 1.1.2.2 lukem printf("Data abort: '%s' status=%03x address=%08x PC=%08x\n",
111 1.1.2.2 lukem aborts[fault_status & FAULT_TYPE_MASK],
112 1.1.2.2 lukem fault_status & 0xfff, fault_address, fault_pc);
113 1.1.2.2 lukem #ifndef DEBUG
114 1.1.2.2 lukem }
115 1.1.2.2 lukem #endif
116 1.1.2.2 lukem }
117 1.1.2.2 lukem
118 1.1.2.4 thorpej static __volatile int data_abort_expected;
119 1.1.2.4 thorpej static __volatile int data_abort_received;
120 1.1.2.4 thorpej
121 1.1.2.4 thorpej int
122 1.1.2.4 thorpej badaddr_read(void *addr, size_t size, void *rptr)
123 1.1.2.4 thorpej {
124 1.1.2.4 thorpej u_long rcpt;
125 1.1.2.4 thorpej int rv;
126 1.1.2.4 thorpej
127 1.1.2.4 thorpej /* Tell the Data Abort handler that we're expecting one. */
128 1.1.2.4 thorpej data_abort_received = 0;
129 1.1.2.4 thorpej data_abort_expected = 1;
130 1.1.2.4 thorpej
131 1.1.2.4 thorpej cpu_drain_writebuf();
132 1.1.2.4 thorpej
133 1.1.2.4 thorpej /* Read from the test address. */
134 1.1.2.4 thorpej switch (size) {
135 1.1.2.4 thorpej case sizeof(uint8_t):
136 1.1.2.4 thorpej __asm __volatile("ldrb %0, [%1]"
137 1.1.2.4 thorpej : "=r" (rcpt)
138 1.1.2.4 thorpej : "r" (addr));
139 1.1.2.4 thorpej break;
140 1.1.2.4 thorpej
141 1.1.2.4 thorpej case sizeof(uint16_t):
142 1.1.2.4 thorpej __asm __volatile("ldrh %0, [%1]"
143 1.1.2.4 thorpej : "=r" (rcpt)
144 1.1.2.4 thorpej : "r" (addr));
145 1.1.2.4 thorpej break;
146 1.1.2.4 thorpej
147 1.1.2.4 thorpej case sizeof(uint32_t):
148 1.1.2.4 thorpej __asm __volatile("ldr %0, [%1]"
149 1.1.2.4 thorpej : "=r" (rcpt)
150 1.1.2.4 thorpej : "r" (addr));
151 1.1.2.4 thorpej break;
152 1.1.2.4 thorpej
153 1.1.2.4 thorpej default:
154 1.1.2.4 thorpej data_abort_expected = 0;
155 1.1.2.4 thorpej panic("badaddr: invalid size (%lu)\n", (u_long) size);
156 1.1.2.4 thorpej }
157 1.1.2.4 thorpej
158 1.1.2.4 thorpej /* Disallow further Data Aborts. */
159 1.1.2.4 thorpej data_abort_expected = 0;
160 1.1.2.4 thorpej
161 1.1.2.4 thorpej rv = data_abort_received;
162 1.1.2.4 thorpej data_abort_received = 0;
163 1.1.2.4 thorpej
164 1.1.2.4 thorpej /* Copy the data back if no fault occurred. */
165 1.1.2.4 thorpej if (rptr != NULL && rv == 0) {
166 1.1.2.4 thorpej switch (size) {
167 1.1.2.4 thorpej case sizeof(uint8_t):
168 1.1.2.4 thorpej *(uint8_t *) rptr = rcpt;
169 1.1.2.4 thorpej break;
170 1.1.2.4 thorpej
171 1.1.2.4 thorpej case sizeof(uint16_t):
172 1.1.2.4 thorpej *(uint16_t *) rptr = rcpt;
173 1.1.2.4 thorpej break;
174 1.1.2.4 thorpej
175 1.1.2.4 thorpej case sizeof(uint32_t):
176 1.1.2.4 thorpej *(uint32_t *) rptr = rcpt;
177 1.1.2.4 thorpej break;
178 1.1.2.4 thorpej }
179 1.1.2.4 thorpej }
180 1.1.2.4 thorpej
181 1.1.2.4 thorpej /* Return true if the address was invalid. */
182 1.1.2.4 thorpej return (rv);
183 1.1.2.4 thorpej }
184 1.1.2.4 thorpej
185 1.1.2.2 lukem /*
186 1.1.2.2 lukem * void data_abort_handler(trapframe_t *frame)
187 1.1.2.2 lukem *
188 1.1.2.2 lukem * Abort handler called when read/write occurs at an address of
189 1.1.2.2 lukem * a non existent or restricted (access permissions) memory page.
190 1.1.2.2 lukem * We first need to identify the type of page fault.
191 1.1.2.2 lukem */
192 1.1.2.2 lukem
193 1.1.2.2 lukem #define TRAP_CODE ((fault_status & 0x0f) | (fault_address & 0xfffffff0))
194 1.1.2.2 lukem
195 1.1.2.2 lukem void
196 1.1.2.2 lukem data_abort_handler(frame)
197 1.1.2.2 lukem trapframe_t *frame;
198 1.1.2.2 lukem {
199 1.1.2.2 lukem struct proc *p;
200 1.1.2.2 lukem struct pcb *pcb;
201 1.1.2.2 lukem u_int fault_address;
202 1.1.2.2 lukem u_int fault_status;
203 1.1.2.2 lukem u_int fault_pc;
204 1.1.2.2 lukem u_int fault_instruction;
205 1.1.2.2 lukem int fault_code;
206 1.1.2.2 lukem int user;
207 1.1.2.2 lukem int error;
208 1.1.2.2 lukem void *onfault;
209 1.1.2.2 lukem
210 1.1.2.2 lukem /*
211 1.1.2.4 thorpej * If we were expecting a Data Abort, signal that we got
212 1.1.2.4 thorpej * one, adjust the PC to skip the faulting insn, and
213 1.1.2.4 thorpej * return.
214 1.1.2.4 thorpej */
215 1.1.2.4 thorpej if (data_abort_expected) {
216 1.1.2.4 thorpej data_abort_received = 1;
217 1.1.2.4 thorpej frame->tf_pc += INSN_SIZE;
218 1.1.2.4 thorpej return;
219 1.1.2.4 thorpej }
220 1.1.2.4 thorpej
221 1.1.2.4 thorpej /*
222 1.1.2.2 lukem * Must get fault address and status from the CPU before
223 1.1.2.2 lukem * re-enabling interrupts. (Interrupt handlers may take
224 1.1.2.2 lukem * R/M emulation faults.)
225 1.1.2.2 lukem */
226 1.1.2.2 lukem fault_address = cpu_faultaddress();
227 1.1.2.2 lukem fault_status = cpu_faultstatus();
228 1.1.2.2 lukem fault_pc = frame->tf_pc;
229 1.1.2.2 lukem
230 1.1.2.2 lukem /*
231 1.1.2.2 lukem * Enable IRQ's (disabled by CPU on abort) if trapframe
232 1.1.2.2 lukem * shows they were enabled.
233 1.1.2.2 lukem */
234 1.1.2.2 lukem if (!(frame->tf_spsr & I32_bit))
235 1.1.2.2 lukem enable_interrupts(I32_bit);
236 1.1.2.2 lukem
237 1.1.2.2 lukem #ifdef DEBUG
238 1.1.2.2 lukem if ((GetCPSR() & PSR_MODE) != PSR_SVC32_MODE)
239 1.1.2.2 lukem panic("data_abort_handler: not in SVC32 mode");
240 1.1.2.2 lukem #endif
241 1.1.2.2 lukem
242 1.1.2.2 lukem /* Update vmmeter statistics */
243 1.1.2.2 lukem uvmexp.traps++;
244 1.1.2.2 lukem
245 1.1.2.2 lukem /* Extract the fault code from the fault status */
246 1.1.2.2 lukem fault_code = fault_status & FAULT_TYPE_MASK;
247 1.1.2.2 lukem
248 1.1.2.2 lukem /* Get the current proc structure or proc0 if there is none */
249 1.1.2.2 lukem if ((p = curproc) == NULL)
250 1.1.2.2 lukem p = &proc0;
251 1.1.2.2 lukem
252 1.1.2.2 lukem /*
253 1.1.2.2 lukem * can't use curpcb, as it might be NULL; and we have p in
254 1.1.2.2 lukem * a register anyway
255 1.1.2.2 lukem */
256 1.1.2.2 lukem pcb = &p->p_addr->u_pcb;
257 1.1.2.2 lukem
258 1.1.2.2 lukem /* fusubailout is used by [fs]uswintr to avoid page faulting */
259 1.1.2.2 lukem if (pcb->pcb_onfault
260 1.1.2.2 lukem && ((fault_code != FAULT_TRANS_S && fault_code != FAULT_TRANS_P &&
261 1.1.2.2 lukem fault_code != FAULT_PERM_S && fault_code != FAULT_PERM_P)
262 1.1.2.2 lukem || pcb->pcb_onfault == fusubailout)) {
263 1.1.2.2 lukem
264 1.1.2.2 lukem copyfault:
265 1.1.2.2 lukem #ifdef DEBUG
266 1.1.2.2 lukem printf("Using pcb_onfault=%p addr=%08x st=%08x p=%p\n",
267 1.1.2.2 lukem pcb->pcb_onfault, fault_address, fault_status, p);
268 1.1.2.2 lukem #endif
269 1.1.2.2 lukem frame->tf_pc = (u_int)pcb->pcb_onfault;
270 1.1.2.2 lukem if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE)
271 1.1.2.2 lukem panic("Yikes pcb_onfault=%p during USR mode fault\n",
272 1.1.2.2 lukem pcb->pcb_onfault);
273 1.1.2.2 lukem return;
274 1.1.2.2 lukem }
275 1.1.2.2 lukem
276 1.1.2.2 lukem /* More debug stuff */
277 1.1.2.2 lukem
278 1.1.2.2 lukem fault_instruction = ReadWord(fault_pc);
279 1.1.2.2 lukem
280 1.1.2.2 lukem #ifdef PMAP_DEBUG
281 1.1.2.2 lukem if (pmap_debug_level >= 0) {
282 1.1.2.2 lukem report_abort(NULL, fault_status, fault_address, fault_pc);
283 1.1.2.2 lukem printf("Instruction @V%08x = %08x\n",
284 1.1.2.2 lukem fault_pc, fault_instruction);
285 1.1.2.2 lukem }
286 1.1.2.2 lukem #endif
287 1.1.2.2 lukem
288 1.1.2.2 lukem /* Call the cpu specific abort fixup routine */
289 1.1.2.2 lukem error = cpu_dataabt_fixup(frame);
290 1.1.2.2 lukem if (error == ABORT_FIXUP_RETURN)
291 1.1.2.2 lukem return;
292 1.1.2.2 lukem if (error == ABORT_FIXUP_FAILED) {
293 1.1.2.2 lukem printf("pc = 0x%08x, insn = ", fault_pc);
294 1.1.2.2 lukem disassemble(fault_pc);
295 1.1.2.2 lukem panic("data abort fixup failed\n");
296 1.1.2.2 lukem }
297 1.1.2.2 lukem
298 1.1.2.2 lukem #ifdef PMAP_DEBUG
299 1.1.2.2 lukem if (pmap_debug_level >= 0)
300 1.1.2.2 lukem printf("fault in process %p\n", p);
301 1.1.2.2 lukem #endif
302 1.1.2.2 lukem
303 1.1.2.2 lukem #ifdef DEBUG
304 1.1.2.2 lukem /* Is this needed ? */
305 1.1.2.2 lukem if (pcb != curpcb) {
306 1.1.2.2 lukem printf("data_abort: Alert ! pcb(%p) != curpcb(%p)\n",
307 1.1.2.2 lukem pcb, curpcb);
308 1.1.2.2 lukem printf("data_abort: Alert ! proc(%p), curproc(%p)\n",
309 1.1.2.2 lukem p, curproc);
310 1.1.2.2 lukem }
311 1.1.2.2 lukem #endif /* DEBUG */
312 1.1.2.2 lukem
313 1.1.2.2 lukem /* Were we in user mode when the abort occurred ? */
314 1.1.2.2 lukem if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
315 1.1.2.2 lukem /*
316 1.1.2.2 lukem * Note that the fault was from USR mode.
317 1.1.2.2 lukem */
318 1.1.2.2 lukem user = 1;
319 1.1.2.2 lukem p->p_addr->u_pcb.pcb_tf = frame;
320 1.1.2.2 lukem } else
321 1.1.2.2 lukem user = 0;
322 1.1.2.2 lukem
323 1.1.2.2 lukem /* Now act on the fault type */
324 1.1.2.2 lukem switch (fault_code) {
325 1.1.2.2 lukem case FAULT_WRTBUF_0: /* Write Buffer Fault */
326 1.1.2.2 lukem case FAULT_WRTBUF_1: /* Write Buffer Fault */
327 1.1.2.2 lukem /* If this happens forget it no point in continuing */
328 1.1.2.2 lukem
329 1.1.2.2 lukem /* FALLTHROUGH */
330 1.1.2.2 lukem
331 1.1.2.2 lukem case FAULT_ALIGN_0: /* Alignment Fault */
332 1.1.2.2 lukem case FAULT_ALIGN_1: /* Alignment Fault */
333 1.1.2.2 lukem /*
334 1.1.2.2 lukem * Really this should just kill the process.
335 1.1.2.2 lukem * Alignment faults are turned off in the kernel
336 1.1.2.2 lukem * in order to get better performance from shorts with
337 1.1.2.2 lukem * GCC so an alignment fault means somebody has played
338 1.1.2.2 lukem * with the control register in the CPU. Might as well
339 1.1.2.2 lukem * panic as the kernel was not compiled for aligned accesses.
340 1.1.2.2 lukem */
341 1.1.2.2 lukem
342 1.1.2.2 lukem /* FALLTHROUGH */
343 1.1.2.2 lukem
344 1.1.2.2 lukem case FAULT_BUSERR_0: /* Bus Error LF Section */
345 1.1.2.2 lukem case FAULT_BUSERR_1: /* Bus Error Page */
346 1.1.2.2 lukem case FAULT_BUSERR_2: /* Bus Error Section */
347 1.1.2.2 lukem case FAULT_BUSERR_3: /* Bus Error Page */
348 1.1.2.2 lukem /* What will accutally cause a bus error ? */
349 1.1.2.2 lukem /* Real bus errors are not a process problem but hardware */
350 1.1.2.2 lukem
351 1.1.2.2 lukem /* FALLTHROUGH */
352 1.1.2.2 lukem
353 1.1.2.2 lukem case FAULT_DOMAIN_S: /* Section Domain Error Fault */
354 1.1.2.2 lukem case FAULT_DOMAIN_P: /* Page Domain Error Fault*/
355 1.1.2.2 lukem /*
356 1.1.2.2 lukem * Right well we dont use domains, everything is
357 1.1.2.2 lukem * always a client and thus subject to access permissions.
358 1.1.2.2 lukem * If we get a domain error then we have corrupts PTE's
359 1.1.2.2 lukem * so we might as well die !
360 1.1.2.2 lukem * I suppose eventually this should just kill the process
361 1.1.2.2 lukem * who owns the PTE's but if this happens it implies a
362 1.1.2.2 lukem * kernel problem.
363 1.1.2.2 lukem */
364 1.1.2.2 lukem
365 1.1.2.2 lukem /* FALLTHROUGH */
366 1.1.2.2 lukem
367 1.1.2.2 lukem case FAULT_BUSTRNL1: /* Bus Error Trans L1 Fault */
368 1.1.2.2 lukem case FAULT_BUSTRNL2: /* Bus Error Trans L2 Fault */
369 1.1.2.2 lukem /*
370 1.1.2.2 lukem * These faults imply that the PTE is corrupt.
371 1.1.2.2 lukem * Likely to be a kernel fault so we had better stop.
372 1.1.2.2 lukem */
373 1.1.2.2 lukem
374 1.1.2.2 lukem /* FALLTHROUGH */
375 1.1.2.2 lukem
376 1.1.2.2 lukem default :
377 1.1.2.2 lukem /* Are there any combinations I have missed ? */
378 1.1.2.2 lukem report_abort(NULL, fault_status, fault_address, fault_pc);
379 1.1.2.2 lukem
380 1.1.2.2 lukem we_re_toast:
381 1.1.2.2 lukem /*
382 1.1.2.2 lukem * Were are dead, try and provide some debug
383 1.1.2.2 lukem * information before dying.
384 1.1.2.2 lukem */
385 1.1.2.2 lukem #ifdef DDB
386 1.1.2.2 lukem printf("Unhandled trap (frame = %p)\n", frame);
387 1.1.2.2 lukem report_abort(NULL, fault_status, fault_address, fault_pc);
388 1.1.2.2 lukem kdb_trap(-1, frame);
389 1.1.2.2 lukem return;
390 1.1.2.2 lukem #else
391 1.1.2.2 lukem panic("Unhandled trap (frame = %p)", frame);
392 1.1.2.2 lukem #endif /* DDB */
393 1.1.2.2 lukem
394 1.1.2.2 lukem case FAULT_TRANS_P: /* Page Translation Fault */
395 1.1.2.2 lukem case FAULT_PERM_P: /* Page Permission Fault */
396 1.1.2.2 lukem case FAULT_TRANS_S: /* Section Translation Fault */
397 1.1.2.2 lukem case FAULT_PERM_S: /* Section Permission Fault */
398 1.1.2.2 lukem /*
399 1.1.2.2 lukem * Page/section translation/permission fault -- need to fault in
400 1.1.2.2 lukem * the page and possibly the page table page.
401 1.1.2.2 lukem */
402 1.1.2.2 lukem {
403 1.1.2.2 lukem register vaddr_t va;
404 1.1.2.2 lukem register struct vmspace *vm = p->p_vmspace;
405 1.1.2.2 lukem register struct vm_map *map;
406 1.1.2.2 lukem int rv;
407 1.1.2.2 lukem vm_prot_t ftype;
408 1.1.2.2 lukem extern struct vm_map *kernel_map;
409 1.1.2.2 lukem
410 1.1.2.2 lukem va = trunc_page((vaddr_t)fault_address);
411 1.1.2.2 lukem
412 1.1.2.2 lukem #ifdef PMAP_DEBUG
413 1.1.2.2 lukem if (pmap_debug_level >= 0)
414 1.1.2.2 lukem printf("page fault: addr=V%08lx ", va);
415 1.1.2.2 lukem #endif
416 1.1.2.2 lukem
417 1.1.2.2 lukem /*
418 1.1.2.2 lukem * It is only a kernel address space fault iff:
419 1.1.2.2 lukem * 1. user == 0 and
420 1.1.2.2 lukem * 2. pcb_onfault not set or
421 1.1.2.2 lukem * 3. pcb_onfault set but supervisor space fault
422 1.1.2.2 lukem * The last can occur during an exec() copyin where the
423 1.1.2.2 lukem * argument space is lazy-allocated.
424 1.1.2.2 lukem */
425 1.1.2.2 lukem if (!user &&
426 1.1.2.2 lukem (va >= VM_MIN_KERNEL_ADDRESS || va < VM_MIN_ADDRESS)) {
427 1.1.2.2 lukem /* Was the fault due to the FPE/IPKDB ? */
428 1.1.2.2 lukem if ((frame->tf_spsr & PSR_MODE) == PSR_UND32_MODE) {
429 1.1.2.2 lukem report_abort("UND32", fault_status,
430 1.1.2.2 lukem fault_address, fault_pc);
431 1.1.2.2 lukem trapsignal(p, SIGSEGV, TRAP_CODE);
432 1.1.2.2 lukem
433 1.1.2.2 lukem /*
434 1.1.2.2 lukem * Force exit via userret()
435 1.1.2.2 lukem * This is necessary as the FPE is an extension
436 1.1.2.2 lukem * to userland that actually runs in a
437 1.1.2.2 lukem * priveledged mode but uses USR mode
438 1.1.2.2 lukem * permissions for its accesses.
439 1.1.2.2 lukem */
440 1.1.2.2 lukem userret(p);
441 1.1.2.2 lukem return;
442 1.1.2.2 lukem }
443 1.1.2.2 lukem map = kernel_map;
444 1.1.2.2 lukem } else
445 1.1.2.2 lukem map = &vm->vm_map;
446 1.1.2.2 lukem
447 1.1.2.2 lukem #ifdef PMAP_DEBUG
448 1.1.2.2 lukem if (pmap_debug_level >= 0)
449 1.1.2.2 lukem printf("vmmap=%p ", map);
450 1.1.2.2 lukem #endif
451 1.1.2.2 lukem
452 1.1.2.2 lukem if (map == NULL)
453 1.1.2.2 lukem panic("No map for fault address\n");
454 1.1.2.2 lukem
455 1.1.2.2 lukem /*
456 1.1.2.2 lukem * We need to know whether the page should be mapped
457 1.1.2.2 lukem * as R or R/W. The MMU does not give us the info as
458 1.1.2.2 lukem * to whether the fault was caused by a read or a write.
459 1.1.2.2 lukem * This means we need to disassemble the instruction
460 1.1.2.2 lukem * responsible and determine if it was a read or write
461 1.1.2.2 lukem * instruction.
462 1.1.2.2 lukem */
463 1.1.2.2 lukem /* STR instruction ? */
464 1.1.2.2 lukem if ((fault_instruction & 0x0c100000) == 0x04000000)
465 1.1.2.2 lukem ftype = VM_PROT_READ | VM_PROT_WRITE;
466 1.1.2.2 lukem /* STM or CDT instruction ? */
467 1.1.2.2 lukem else if ((fault_instruction & 0x0a100000) == 0x08000000)
468 1.1.2.2 lukem ftype = VM_PROT_READ | VM_PROT_WRITE;
469 1.1.2.2 lukem /* STRH, STRSH or STRSB instruction ? */
470 1.1.2.2 lukem else if ((fault_instruction & 0x0e100090) == 0x00000090)
471 1.1.2.2 lukem ftype = VM_PROT_READ | VM_PROT_WRITE;
472 1.1.2.2 lukem /* SWP instruction ? */
473 1.1.2.2 lukem else if ((fault_instruction & 0x0fb00ff0) == 0x01000090)
474 1.1.2.2 lukem ftype = VM_PROT_READ | VM_PROT_WRITE;
475 1.1.2.2 lukem else
476 1.1.2.2 lukem ftype = VM_PROT_READ;
477 1.1.2.2 lukem
478 1.1.2.2 lukem #ifdef PMAP_DEBUG
479 1.1.2.2 lukem if (pmap_debug_level >= 0)
480 1.1.2.2 lukem printf("fault protection = %d\n", ftype);
481 1.1.2.2 lukem #endif
482 1.1.2.2 lukem
483 1.1.2.2 lukem if ((ftype & VM_PROT_WRITE) ?
484 1.1.2.2 lukem pmap_modified_emulation(map->pmap, va) :
485 1.1.2.2 lukem pmap_handled_emulation(map->pmap, va))
486 1.1.2.2 lukem goto out;
487 1.1.2.2 lukem
488 1.1.2.2 lukem if (current_intr_depth > 0) {
489 1.1.2.2 lukem #ifdef DDB
490 1.1.2.2 lukem printf("Non-emulated page fault with intr_depth > 0\n");
491 1.1.2.2 lukem report_abort(NULL, fault_status, fault_address, fault_pc);
492 1.1.2.2 lukem kdb_trap(-1, frame);
493 1.1.2.2 lukem return;
494 1.1.2.2 lukem #else
495 1.1.2.2 lukem panic("Fault with intr_depth > 0");
496 1.1.2.2 lukem #endif /* DDB */
497 1.1.2.2 lukem }
498 1.1.2.2 lukem
499 1.1.2.2 lukem onfault = pcb->pcb_onfault;
500 1.1.2.2 lukem pcb->pcb_onfault = NULL;
501 1.1.2.2 lukem rv = uvm_fault(map, va, 0, ftype);
502 1.1.2.2 lukem pcb->pcb_onfault = onfault;
503 1.1.2.2 lukem if (rv == 0)
504 1.1.2.2 lukem goto out;
505 1.1.2.2 lukem
506 1.1.2.2 lukem if (user == 0) {
507 1.1.2.2 lukem if (pcb->pcb_onfault)
508 1.1.2.2 lukem goto copyfault;
509 1.1.2.2 lukem printf("[u]vm_fault(%p, %lx, %x, 0) -> %x\n",
510 1.1.2.2 lukem map, va, ftype, rv);
511 1.1.2.2 lukem goto we_re_toast;
512 1.1.2.2 lukem }
513 1.1.2.2 lukem
514 1.1.2.2 lukem report_abort("", fault_status, fault_address, fault_pc);
515 1.1.2.2 lukem if (rv == ENOMEM) {
516 1.1.2.2 lukem printf("UVM: pid %d (%s), uid %d killed: "
517 1.1.2.2 lukem "out of swap\n", p->p_pid, p->p_comm,
518 1.1.2.2 lukem p->p_cred && p->p_ucred ?
519 1.1.2.2 lukem p->p_ucred->cr_uid : -1);
520 1.1.2.2 lukem trapsignal(p, SIGKILL, TRAP_CODE);
521 1.1.2.2 lukem } else
522 1.1.2.2 lukem trapsignal(p, SIGSEGV, TRAP_CODE);
523 1.1.2.2 lukem break;
524 1.1.2.2 lukem }
525 1.1.2.2 lukem }
526 1.1.2.2 lukem
527 1.1.2.2 lukem out:
528 1.1.2.2 lukem /* Call userret() if it was a USR mode fault */
529 1.1.2.2 lukem if (user)
530 1.1.2.2 lukem userret(p);
531 1.1.2.2 lukem }
532 1.1.2.2 lukem
533 1.1.2.2 lukem
534 1.1.2.2 lukem /*
535 1.1.2.2 lukem * void prefetch_abort_handler(trapframe_t *frame)
536 1.1.2.2 lukem *
537 1.1.2.2 lukem * Abort handler called when instruction execution occurs at
538 1.1.2.2 lukem * a non existent or restricted (access permissions) memory page.
539 1.1.2.2 lukem * If the address is invalid and we were in SVC mode then panic as
540 1.1.2.2 lukem * the kernel should never prefetch abort.
541 1.1.2.2 lukem * If the address is invalid and the page is mapped then the user process
542 1.1.2.2 lukem * does no have read permission so send it a signal.
543 1.1.2.2 lukem * Otherwise fault the page in and try again.
544 1.1.2.2 lukem */
545 1.1.2.2 lukem
546 1.1.2.2 lukem extern int kernel_debug;
547 1.1.2.2 lukem
548 1.1.2.2 lukem void
549 1.1.2.2 lukem prefetch_abort_handler(frame)
550 1.1.2.2 lukem trapframe_t *frame;
551 1.1.2.2 lukem {
552 1.1.2.2 lukem register u_int fault_pc;
553 1.1.2.2 lukem register struct proc *p;
554 1.1.2.2 lukem register struct pcb *pcb;
555 1.1.2.2 lukem u_int fault_instruction;
556 1.1.2.2 lukem pt_entry_t *pte;
557 1.1.2.2 lukem int error;
558 1.1.2.2 lukem
559 1.1.2.2 lukem /*
560 1.1.2.2 lukem * Enable IRQ's (disabled by the abort) This always comes
561 1.1.2.2 lukem * from user mode so we know interrupts were not disabled.
562 1.1.2.2 lukem * But we check anyway.
563 1.1.2.2 lukem */
564 1.1.2.2 lukem if (!(frame->tf_spsr & I32_bit))
565 1.1.2.2 lukem enable_interrupts(I32_bit);
566 1.1.2.2 lukem
567 1.1.2.2 lukem #ifdef DEBUG
568 1.1.2.2 lukem if ((GetCPSR() & PSR_MODE) != PSR_SVC32_MODE)
569 1.1.2.2 lukem panic("prefetch_abort_handler: not in SVC32 mode");
570 1.1.2.2 lukem #endif
571 1.1.2.2 lukem
572 1.1.2.2 lukem /* Update vmmeter statistics */
573 1.1.2.2 lukem uvmexp.traps++;
574 1.1.2.2 lukem
575 1.1.2.2 lukem /* Call the cpu specific abort fixup routine */
576 1.1.2.2 lukem error = cpu_prefetchabt_fixup(frame);
577 1.1.2.2 lukem if (error == ABORT_FIXUP_RETURN)
578 1.1.2.2 lukem return;
579 1.1.2.2 lukem if (error == ABORT_FIXUP_FAILED)
580 1.1.2.2 lukem panic("prefetch abort fixup failed\n");
581 1.1.2.2 lukem
582 1.1.2.2 lukem /* Get the current proc structure or proc0 if there is none */
583 1.1.2.2 lukem if ((p = curproc) == 0) {
584 1.1.2.2 lukem p = &proc0;
585 1.1.2.2 lukem #ifdef DEBUG
586 1.1.2.2 lukem printf("Prefetch abort with curproc == 0\n");
587 1.1.2.2 lukem #endif
588 1.1.2.2 lukem }
589 1.1.2.2 lukem
590 1.1.2.2 lukem #ifdef PMAP_DEBUG
591 1.1.2.2 lukem if (pmap_debug_level >= 0)
592 1.1.2.2 lukem printf("prefetch fault in process %p %s\n", p, p->p_comm);
593 1.1.2.2 lukem #endif
594 1.1.2.2 lukem /*
595 1.1.2.2 lukem * can't use curpcb, as it might be NULL; and we have p in a
596 1.1.2.2 lukem * register anyway
597 1.1.2.2 lukem */
598 1.1.2.2 lukem pcb = &p->p_addr->u_pcb;
599 1.1.2.2 lukem if (pcb == 0)
600 1.1.2.2 lukem panic("prefetch_abort_handler: no pcb ... we're toast !\n");
601 1.1.2.2 lukem
602 1.1.2.2 lukem #ifdef DEBUG
603 1.1.2.2 lukem if (pcb != curpcb) {
604 1.1.2.2 lukem printf("data_abort: Alert ! pcb(%p) != curpcb(%p)\n",
605 1.1.2.2 lukem pcb, curpcb);
606 1.1.2.2 lukem printf("data_abort: Alert ! proc(%p), curproc(%p)\n",
607 1.1.2.2 lukem p, curproc);
608 1.1.2.2 lukem }
609 1.1.2.2 lukem #endif /* DEBUG */
610 1.1.2.2 lukem
611 1.1.2.4 thorpej /* Get fault address */
612 1.1.2.4 thorpej fault_pc = frame->tf_pc;
613 1.1.2.2 lukem
614 1.1.2.4 thorpej /* Was the prefectch abort from USR32 mode ? */
615 1.1.2.2 lukem if ((frame->tf_spsr & PSR_MODE) == PSR_USR32_MODE) {
616 1.1.2.2 lukem p->p_addr->u_pcb.pcb_tf = frame;
617 1.1.2.2 lukem } else {
618 1.1.2.2 lukem /*
619 1.1.2.2 lukem * All the kernel code pages are loaded at boot time
620 1.1.2.2 lukem * and do not get paged
621 1.1.2.2 lukem */
622 1.1.2.4 thorpej panic("Prefetch abort in non-USR mode (frame=%p PC=0x%08x)\n",
623 1.1.2.4 thorpej frame, fault_pc);
624 1.1.2.2 lukem }
625 1.1.2.2 lukem
626 1.1.2.2 lukem #ifdef PMAP_DEBUG
627 1.1.2.2 lukem if (pmap_debug_level >= 0)
628 1.1.2.2 lukem printf("prefetch_abort: PC = %08x\n", fault_pc);
629 1.1.2.2 lukem #endif
630 1.1.2.2 lukem /* Ok validate the address, can only execute in USER space */
631 1.1.2.2 lukem if (fault_pc < VM_MIN_ADDRESS || fault_pc >= VM_MAXUSER_ADDRESS) {
632 1.1.2.2 lukem #ifdef DEBUG
633 1.1.2.2 lukem printf("prefetch: pc (%08x) not in user process space\n",
634 1.1.2.2 lukem fault_pc);
635 1.1.2.2 lukem #endif
636 1.1.2.2 lukem trapsignal(p, SIGSEGV, fault_pc);
637 1.1.2.2 lukem userret(p);
638 1.1.2.2 lukem return;
639 1.1.2.2 lukem }
640 1.1.2.2 lukem
641 1.1.2.2 lukem /* Is the page already mapped ? */
642 1.1.2.2 lukem /* This is debugging for rev K SA110 silicon */
643 1.1.2.2 lukem pte = pmap_pte(p->p_vmspace->vm_map.pmap, (vaddr_t)fault_pc);
644 1.1.2.2 lukem if (pte && *pte != 0) {
645 1.1.2.2 lukem if (kernel_debug & 1) {
646 1.1.2.2 lukem printf("prefetch_abort: page is already mapped - pte=%p *pte=%08x\n",
647 1.1.2.2 lukem pte, *pte);
648 1.1.2.2 lukem printf("prefetch_abort: pc=%08x proc=%p process=%s\n", fault_pc, p, p->p_comm);
649 1.1.2.2 lukem printf("prefetch_abort: far=%08x fs=%x\n", cpu_faultaddress(), cpu_faultstatus());
650 1.1.2.2 lukem printf("prefetch_abort: trapframe=%08x\n", (u_int)frame);
651 1.1.2.2 lukem }
652 1.1.2.2 lukem #ifdef DDB
653 1.1.2.2 lukem if (kernel_debug & 2)
654 1.1.2.2 lukem Debugger();
655 1.1.2.2 lukem #endif
656 1.1.2.2 lukem }
657 1.1.2.2 lukem
658 1.1.2.2 lukem /* Ok read the fault address. This will fault the page in for us */
659 1.1.2.2 lukem if (fetchuserword(fault_pc, &fault_instruction) != 0) {
660 1.1.2.2 lukem #ifdef DEBUG
661 1.1.2.2 lukem printf("prefetch: faultin failed for address %08x\n",
662 1.1.2.2 lukem fault_pc);
663 1.1.2.2 lukem #endif
664 1.1.2.2 lukem trapsignal(p, SIGSEGV, fault_pc);
665 1.1.2.2 lukem } else {
666 1.1.2.2 lukem
667 1.1.2.2 lukem #ifdef DIAGNOSTIC
668 1.1.2.2 lukem /* More debug stuff */
669 1.1.2.2 lukem
670 1.1.2.2 lukem #ifdef PMAP_DEBUG
671 1.1.2.2 lukem if (pmap_debug_level >= 0) {
672 1.1.2.2 lukem printf("Instruction @V%08x = %08x\n", fault_pc,
673 1.1.2.2 lukem fault_instruction);
674 1.1.2.2 lukem disassemble(fault_pc);
675 1.1.2.2 lukem printf("return addr=%08x", frame->tf_pc);
676 1.1.2.2 lukem pte = pmap_pte(p->p_vmspace->vm_map.pmap,
677 1.1.2.2 lukem (vaddr_t)fault_pc);
678 1.1.2.2 lukem if (pte)
679 1.1.2.2 lukem printf(" pte=%p *pte=%08x\n", pte, *pte);
680 1.1.2.2 lukem else
681 1.1.2.2 lukem printf("\n");
682 1.1.2.2 lukem
683 1.1.2.2 lukem }
684 1.1.2.2 lukem #endif /* PMAP_DEBUG */
685 1.1.2.2 lukem #endif /* DIAGNOSTIC */
686 1.1.2.2 lukem }
687 1.1.2.2 lukem
688 1.1.2.2 lukem userret(p);
689 1.1.2.2 lukem }
690 1.1.2.2 lukem
691 1.1.2.2 lukem int
692 1.1.2.2 lukem cowfault(va)
693 1.1.2.2 lukem vaddr_t va;
694 1.1.2.2 lukem {
695 1.1.2.2 lukem struct vmspace *vm;
696 1.1.2.2 lukem int error;
697 1.1.2.2 lukem
698 1.1.2.2 lukem if (va >= VM_MAXUSER_ADDRESS)
699 1.1.2.2 lukem return (EFAULT);
700 1.1.2.2 lukem
701 1.1.2.2 lukem /* uvm_fault can't be called from within an interrupt */
702 1.1.2.2 lukem KASSERT(current_intr_depth == 0);
703 1.1.2.2 lukem
704 1.1.2.2 lukem vm = curproc->p_vmspace;
705 1.1.2.2 lukem error = uvm_fault(&vm->vm_map, va, 0, VM_PROT_READ | VM_PROT_WRITE);
706 1.1.2.2 lukem return error;
707 1.1.2.2 lukem }
708 1.1.2.2 lukem
709 1.1.2.2 lukem /* End of fault.c */
710