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