1 /* $Id: fault.c,v 1.113 2000/01/21 11:38:47 jj Exp $
2 * fault.c: Page fault handlers for the Sparc.
4 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1996 Eddie C. Dost (ecd@skynet.be)
6 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/threads.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
19 #include <linux/smp.h>
20 #include <linux/smp_lock.h>
21 #include <linux/interrupt.h>
23 #include <asm/system.h>
24 #include <asm/segment.h>
26 #include <asm/pgtable.h>
27 #include <asm/memreg.h>
28 #include <asm/openprom.h>
29 #include <asm/oplib.h>
31 #include <asm/traps.h>
32 #include <asm/kdebug.h>
33 #include <asm/uaccess.h>
35 #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
37 extern struct sparc_phys_banks sp_banks
[SPARC_PHYS_BANKS
];
38 extern int prom_node_root
;
40 struct linux_romvec
*romvec
;
42 /* At boot time we determine these two values necessary for setting
43 * up the segment maps and page table entries (pte's).
46 int num_segmaps
, num_contexts
;
49 /* various Virtual Address Cache parameters we find at boot time... */
51 int vac_size
, vac_linesize
, vac_do_hw_vac_flushes
;
52 int vac_entries_per_context
, vac_entries_per_segment
;
53 int vac_entries_per_page
;
55 /* Nice, simple, prom library does all the sweating for us. ;) */
56 int prom_probe_memory (void)
58 register struct linux_mlist_v0
*mlist
;
59 register unsigned long bytes
, base_paddr
, tally
;
63 mlist
= *prom_meminfo()->v0_available
;
64 bytes
= tally
= mlist
->num_bytes
;
65 base_paddr
= (unsigned long) mlist
->start_adr
;
67 sp_banks
[0].base_addr
= base_paddr
;
68 sp_banks
[0].num_bytes
= bytes
;
70 while (mlist
->theres_more
!= (void *) 0){
72 mlist
= mlist
->theres_more
;
73 bytes
= mlist
->num_bytes
;
75 if (i
>= SPARC_PHYS_BANKS
-1) {
76 printk ("The machine has more banks than "
77 "this kernel can support\n"
78 "Increase the SPARC_PHYS_BANKS "
79 "setting (currently %d)\n",
81 i
= SPARC_PHYS_BANKS
-1;
85 sp_banks
[i
].base_addr
= (unsigned long) mlist
->start_adr
;
86 sp_banks
[i
].num_bytes
= mlist
->num_bytes
;
90 sp_banks
[i
].base_addr
= 0xdeadbeef;
91 sp_banks
[i
].num_bytes
= 0;
93 /* Now mask all bank sizes on a page boundary, it is all we can
96 for(i
=0; sp_banks
[i
].num_bytes
!= 0; i
++)
97 sp_banks
[i
].num_bytes
&= PAGE_MASK
;
102 /* Traverse the memory lists in the prom to see how much physical we
110 total
= prom_probe_memory();
112 /* Oh man, much nicer, keep the dirt in promlib. */
116 extern void sun4c_complete_all_stores(void);
118 /* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
119 asmlinkage
void sparc_lvl15_nmi(struct pt_regs
*regs
, unsigned long serr
,
120 unsigned long svaddr
, unsigned long aerr
,
121 unsigned long avaddr
)
123 sun4c_complete_all_stores();
124 printk("FAULT: NMI received\n");
125 printk("SREGS: Synchronous Error %08lx\n", serr
);
126 printk(" Synchronous Vaddr %08lx\n", svaddr
);
127 printk(" Asynchronous Error %08lx\n", aerr
);
128 printk(" Asynchronous Vaddr %08lx\n", avaddr
);
129 if (sun4c_memerr_reg
)
130 printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg
);
131 printk("REGISTER DUMP:\n");
136 static void unhandled_fault(unsigned long, struct task_struct
*,
137 struct pt_regs
*) __attribute__ ((noreturn
));
139 static void unhandled_fault(unsigned long address
, struct task_struct
*tsk
,
140 struct pt_regs
*regs
)
142 if((unsigned long) address
< PAGE_SIZE
) {
143 printk(KERN_ALERT
"Unable to handle kernel NULL "
144 "pointer dereference");
146 printk(KERN_ALERT
"Unable to handle kernel paging request "
147 "at virtual address %08lx\n", address
);
149 printk(KERN_ALERT
"tsk->{mm,active_mm}->context = %08lx\n",
150 (tsk
->mm
? tsk
->mm
->context
: tsk
->active_mm
->context
));
151 printk(KERN_ALERT
"tsk->{mm,active_mm}->pgd = %08lx\n",
152 (tsk
->mm
? (unsigned long) tsk
->mm
->pgd
:
153 (unsigned long) tsk
->active_mm
->pgd
));
154 die_if_kernel("Oops", regs
);
157 asmlinkage
int lookup_fault(unsigned long pc
, unsigned long ret_pc
,
158 unsigned long address
)
165 i
= search_exception_table (ret_pc
, &g2
);
167 /* load & store will be handled by fixup */
169 /* store will be handled by fixup, load will bump out */
170 /* for _to_ macros */
171 case 1: insn
= (unsigned)pc
; if ((insn
>> 21) & 1) return 1; break;
172 /* load will be handled by fixup, store will bump out */
173 /* for _from_ macros */
174 case 2: insn
= (unsigned)pc
;
175 if (!((insn
>> 21) & 1) || ((insn
>>19)&0x3f) == 15) return 2;
179 memset (®s
, 0, sizeof (regs
));
182 __asm__
__volatile__ ("
186 nop" : "=r" (regs
.psr
));
187 unhandled_fault (address
, current
, ®s
);
192 asmlinkage
void do_sparc_fault(struct pt_regs
*regs
, int text_fault
, int write
,
193 unsigned long address
)
195 struct vm_area_struct
*vma
;
196 struct task_struct
*tsk
= current
;
197 struct mm_struct
*mm
= tsk
->mm
;
201 int from_user
= !(regs
->psr
& PSR_PS
);
203 info
.si_code
= SEGV_MAPERR
;
208 * If we're in an interrupt or have no user
209 * context, we must not take the fault..
211 if (in_interrupt() || !mm
)
217 * The kernel referencing a bad kernel pointer can lock up
218 * a sun4c machine completely, so we must attempt recovery.
220 if(!from_user
&& address
>= PAGE_OFFSET
)
223 vma
= find_vma(mm
, address
);
226 if(vma
->vm_start
<= address
)
228 if(!(vma
->vm_flags
& VM_GROWSDOWN
))
230 if(expand_stack(vma
, address
))
233 * Ok, we have a good vm_area for this memory access, so
237 info
.si_code
= SEGV_ACCERR
;
239 if(!(vma
->vm_flags
& VM_WRITE
))
242 /* Allow reads even for write-only mappings */
243 if(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
248 * If for any reason at all we couldn't handle the fault,
249 * make sure we exit gracefully rather than endlessly redo
253 int fault
= handle_mm_fault(tsk
, vma
, address
, write
);
263 * Something tried to access memory that isn't in our memory map..
264 * Fix it, but check if it's kernel or user first..
269 /* User mode accesses just cause a SIGSEGV */
272 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
273 tsk
->comm
, tsk
->pid
, address
, regs
->pc
);
275 info
.si_signo
= SIGSEGV
;
277 /* info.si_code set above to make clear whether
278 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
279 info
.si_addr
= (void *)address
;
281 force_sig_info (SIGSEGV
, &info
, tsk
);
285 /* Is this in ex_table? */
287 g2
= regs
->u_regs
[UREG_G2
];
288 if (!from_user
&& (fixup
= search_exception_table (regs
->pc
, &g2
))) {
289 if (fixup
> 10) { /* Values below are reserved for other things */
290 extern const unsigned __memset_start
[];
291 extern const unsigned __memset_end
[];
292 extern const unsigned __csum_partial_copy_start
[];
293 extern const unsigned __csum_partial_copy_end
[];
295 #ifdef DEBUG_EXCEPTIONS
296 printk("Exception: PC<%08lx> faddr<%08lx>\n", regs
->pc
, address
);
297 printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
298 regs
->pc
, fixup
, g2
);
300 if ((regs
->pc
>= (unsigned long)__memset_start
&&
301 regs
->pc
< (unsigned long)__memset_end
) ||
302 (regs
->pc
>= (unsigned long)__csum_partial_copy_start
&&
303 regs
->pc
< (unsigned long)__csum_partial_copy_end
)) {
304 regs
->u_regs
[UREG_I4
] = address
;
305 regs
->u_regs
[UREG_I5
] = regs
->pc
;
307 regs
->u_regs
[UREG_G2
] = g2
;
309 regs
->npc
= regs
->pc
+ 4;
314 unhandled_fault (address
, tsk
, regs
);
318 * We ran out of memory, or some other thing happened to us that made
319 * us unable to handle the page fault gracefully.
323 printk("VM: killing process %s\n", tsk
->comm
);
330 info
.si_signo
= SIGBUS
;
332 info
.si_code
= BUS_ADRERR
;
333 info
.si_addr
= (void *)address
;
335 force_sig_info (SIGBUS
, &info
, tsk
);
340 asmlinkage
void do_sun4c_fault(struct pt_regs
*regs
, int text_fault
, int write
,
341 unsigned long address
)
343 extern void sun4c_update_mmu_cache(struct vm_area_struct
*,
344 unsigned long,pte_t
);
345 extern pgd_t
*sun4c_pgd_offset(struct mm_struct
*,unsigned long);
346 extern pte_t
*sun4c_pte_offset(pmd_t
*,unsigned long);
347 struct task_struct
*tsk
= current
;
348 struct mm_struct
*mm
= tsk
->mm
;
355 !(regs
->psr
& PSR_PS
)) {
356 unsigned int insn
, *ip
;
358 ip
= (unsigned int *)regs
->pc
;
359 if (! get_user(insn
, ip
)) {
360 if ((insn
& 0xc1680000) == 0xc0680000)
365 pgdp
= sun4c_pgd_offset(mm
, address
);
366 ptep
= sun4c_pte_offset((pmd_t
*) pgdp
, address
);
368 if (pgd_val(*pgdp
)) {
370 if ((pte_val(*ptep
) & (_SUN4C_PAGE_WRITE
|_SUN4C_PAGE_PRESENT
))
371 == (_SUN4C_PAGE_WRITE
|_SUN4C_PAGE_PRESENT
)) {
374 *ptep
= __pte(pte_val(*ptep
) | _SUN4C_PAGE_ACCESSED
|
375 _SUN4C_PAGE_MODIFIED
|
380 if (sun4c_get_segmap(address
) != invalid_segment
) {
381 sun4c_put_pte(address
, pte_val(*ptep
));
382 restore_flags(flags
);
385 restore_flags(flags
);
388 if ((pte_val(*ptep
) & (_SUN4C_PAGE_READ
|_SUN4C_PAGE_PRESENT
))
389 == (_SUN4C_PAGE_READ
|_SUN4C_PAGE_PRESENT
)) {
392 *ptep
= __pte(pte_val(*ptep
) | _SUN4C_PAGE_ACCESSED
|
396 if (sun4c_get_segmap(address
) != invalid_segment
) {
397 sun4c_put_pte(address
, pte_val(*ptep
));
398 restore_flags(flags
);
401 restore_flags(flags
);
406 /* This conditional is 'interesting'. */
407 if (pgd_val(*pgdp
) && !(write
&& !(pte_val(*ptep
) & _SUN4C_PAGE_WRITE
))
408 && (pte_val(*ptep
) & _SUN4C_PAGE_VALID
))
409 /* Note: It is safe to not grab the MMAP semaphore here because
410 * we know that update_mmu_cache() will not sleep for
411 * any reason (at least not in the current implementation)
412 * and therefore there is no danger of another thread getting
413 * on the CPU and doing a shrink_mmap() on this vma.
415 sun4c_update_mmu_cache (find_vma(current
->mm
, address
), address
,
418 do_sparc_fault(regs
, text_fault
, write
, address
);
421 /* This always deals with user addresses. */
422 inline void force_user_fault(unsigned long address
, int write
)
424 struct vm_area_struct
*vma
;
425 struct task_struct
*tsk
= current
;
426 struct mm_struct
*mm
= tsk
->mm
;
429 info
.si_code
= SEGV_MAPERR
;
432 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
433 tsk
->pid
, write
, address
);
436 vma
= find_vma(mm
, address
);
439 if(vma
->vm_start
<= address
)
441 if(!(vma
->vm_flags
& VM_GROWSDOWN
))
443 if(expand_stack(vma
, address
))
446 info
.si_code
= SEGV_ACCERR
;
448 if(!(vma
->vm_flags
& VM_WRITE
))
451 if(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
)))
454 if (!handle_mm_fault(current
, vma
, address
, write
))
461 printk("Window whee %s [%d]: segfaults at %08lx\n",
462 tsk
->comm
, tsk
->pid
, address
);
464 info
.si_signo
= SIGSEGV
;
466 /* info.si_code set above to make clear whether
467 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
468 info
.si_addr
= (void *)address
;
470 force_sig_info (SIGSEGV
, &info
, tsk
);
475 info
.si_signo
= SIGBUS
;
477 info
.si_code
= BUS_ADRERR
;
478 info
.si_addr
= (void *)address
;
480 force_sig_info (SIGBUS
, &info
, tsk
);
483 void window_overflow_fault(void)
487 sp
= current
->thread
.rwbuf_stkptrs
[0];
488 if(((sp
+ 0x38) & PAGE_MASK
) != (sp
& PAGE_MASK
))
489 force_user_fault(sp
+ 0x38, 1);
490 force_user_fault(sp
, 1);
493 void window_underflow_fault(unsigned long sp
)
495 if(((sp
+ 0x38) & PAGE_MASK
) != (sp
& PAGE_MASK
))
496 force_user_fault(sp
+ 0x38, 0);
497 force_user_fault(sp
, 0);
500 void window_ret_fault(struct pt_regs
*regs
)
504 sp
= regs
->u_regs
[UREG_FP
];
505 if(((sp
+ 0x38) & PAGE_MASK
) != (sp
& PAGE_MASK
))
506 force_user_fault(sp
+ 0x38, 0);
507 force_user_fault(sp
, 0);