Import 2.3.47pre1
[davej-history.git] / arch / sparc / mm / fault.c
blob909c464476872c1b9db12627830ec3dad1b9e645
1 /* $Id: fault.c,v 1.114 2000/02/14 04:52:36 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)
7 */
9 #include <asm/head.h>
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>
18 #include <linux/mm.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>
25 #include <asm/page.h>
26 #include <asm/pgtable.h>
27 #include <asm/memreg.h>
28 #include <asm/openprom.h>
29 #include <asm/oplib.h>
30 #include <asm/smp.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;
47 int invalid_segment;
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;
60 register int i;
62 i = 0;
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){
71 i++;
72 mlist = mlist->theres_more;
73 bytes = mlist->num_bytes;
74 tally += 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",
80 SPARC_PHYS_BANKS);
81 i = SPARC_PHYS_BANKS-1;
82 break;
85 sp_banks[i].base_addr = (unsigned long) mlist->start_adr;
86 sp_banks[i].num_bytes = mlist->num_bytes;
89 i++;
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
94 * use anyways.
96 for(i=0; sp_banks[i].num_bytes != 0; i++)
97 sp_banks[i].num_bytes &= PAGE_MASK;
99 return tally;
102 /* Traverse the memory lists in the prom to see how much physical we
103 * have.
105 unsigned long
106 probe_memory(void)
108 int total;
110 total = prom_probe_memory();
112 /* Oh man, much nicer, keep the dirt in promlib. */
113 return total;
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");
132 show_regs(regs);
133 prom_halt();
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");
145 } else {
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)
160 unsigned long g2;
161 int i;
162 unsigned insn;
163 struct pt_regs regs;
165 i = search_exception_table (ret_pc, &g2);
166 switch (i) {
167 /* load & store will be handled by fixup */
168 case 3: return 3;
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;
176 break;
177 default: break;
179 memset (&regs, 0, sizeof (regs));
180 regs.pc = pc;
181 regs.npc = pc + 4;
182 __asm__ __volatile__ ("
183 rd %%psr, %0
186 nop" : "=r" (regs.psr));
187 unhandled_fault (address, current, &regs);
188 /* Not reached */
189 return 0;
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;
198 unsigned int fixup;
199 unsigned long g2;
200 siginfo_t info;
201 int from_user = !(regs->psr & PSR_PS);
203 info.si_code = SEGV_MAPERR;
204 if(text_fault)
205 address = regs->pc;
208 * If we're in an interrupt or have no user
209 * context, we must not take the fault..
211 if (in_interrupt() || !mm)
212 goto no_context;
214 down(&mm->mmap_sem);
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)
221 goto bad_area;
223 vma = find_vma(mm, address);
224 if(!vma)
225 goto bad_area;
226 if(vma->vm_start <= address)
227 goto good_area;
228 if(!(vma->vm_flags & VM_GROWSDOWN))
229 goto bad_area;
230 if(expand_stack(vma, address))
231 goto bad_area;
233 * Ok, we have a good vm_area for this memory access, so
234 * we can handle it..
236 good_area:
237 info.si_code = SEGV_ACCERR;
238 if(write) {
239 if(!(vma->vm_flags & VM_WRITE))
240 goto bad_area;
241 } else {
242 /* Allow reads even for write-only mappings */
243 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
244 goto bad_area;
248 * If for any reason at all we couldn't handle the fault,
249 * make sure we exit gracefully rather than endlessly redo
250 * the fault.
253 int fault = handle_mm_fault(tsk, vma, address, write);
254 if (fault < 0)
255 goto out_of_memory;
256 if (!fault)
257 goto do_sigbus;
259 up(&mm->mmap_sem);
260 return;
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..
266 bad_area:
267 up(&mm->mmap_sem);
269 /* User mode accesses just cause a SIGSEGV */
270 if(from_user) {
271 #if 0
272 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
273 tsk->comm, tsk->pid, address, regs->pc);
274 #endif
275 info.si_signo = SIGSEGV;
276 info.si_errno = 0;
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;
280 info.si_trapno = 0;
281 force_sig_info (SIGSEGV, &info, tsk);
282 return;
285 /* Is this in ex_table? */
286 no_context:
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);
299 #endif
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;
308 regs->pc = fixup;
309 regs->npc = regs->pc + 4;
310 return;
314 unhandled_fault (address, tsk, regs);
315 do_exit(SIGKILL);
318 * We ran out of memory, or some other thing happened to us that made
319 * us unable to handle the page fault gracefully.
321 out_of_memory:
322 up(&mm->mmap_sem);
323 printk("VM: killing process %s\n", tsk->comm);
324 if (from_user)
325 do_exit(SIGKILL);
326 goto no_context;
328 do_sigbus:
329 up(&mm->mmap_sem);
330 info.si_signo = SIGBUS;
331 info.si_errno = 0;
332 info.si_code = BUS_ADRERR;
333 info.si_addr = (void *)address;
334 info.si_trapno = 0;
335 force_sig_info (SIGBUS, &info, tsk);
336 if (!from_user)
337 goto no_context;
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 pte_t *sun4c_pte_offset(pmd_t *,unsigned long);
346 struct task_struct *tsk = current;
347 struct mm_struct *mm = tsk->mm;
348 pgd_t *pgdp;
349 pte_t *ptep;
351 if (text_fault) {
352 address = regs->pc;
353 } else if (!write &&
354 !(regs->psr & PSR_PS)) {
355 unsigned int insn, *ip;
357 ip = (unsigned int *)regs->pc;
358 if (! get_user(insn, ip)) {
359 if ((insn & 0xc1680000) == 0xc0680000)
360 write = 1;
364 pgdp = pgd_offset(mm, address);
365 ptep = sun4c_pte_offset((pmd_t *) pgdp, address);
367 if (pgd_val(*pgdp)) {
368 if (write) {
369 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
370 == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
371 unsigned long flags;
373 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
374 _SUN4C_PAGE_MODIFIED |
375 _SUN4C_PAGE_VALID |
376 _SUN4C_PAGE_DIRTY);
378 save_and_cli(flags);
379 if (sun4c_get_segmap(address) != invalid_segment) {
380 sun4c_put_pte(address, pte_val(*ptep));
381 restore_flags(flags);
382 return;
384 restore_flags(flags);
386 } else {
387 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
388 == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
389 unsigned long flags;
391 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
392 _SUN4C_PAGE_VALID);
394 save_and_cli(flags);
395 if (sun4c_get_segmap(address) != invalid_segment) {
396 sun4c_put_pte(address, pte_val(*ptep));
397 restore_flags(flags);
398 return;
400 restore_flags(flags);
405 /* This conditional is 'interesting'. */
406 if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
407 && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
408 /* Note: It is safe to not grab the MMAP semaphore here because
409 * we know that update_mmu_cache() will not sleep for
410 * any reason (at least not in the current implementation)
411 * and therefore there is no danger of another thread getting
412 * on the CPU and doing a shrink_mmap() on this vma.
414 sun4c_update_mmu_cache (find_vma(current->mm, address), address,
415 *ptep);
416 else
417 do_sparc_fault(regs, text_fault, write, address);
420 /* This always deals with user addresses. */
421 inline void force_user_fault(unsigned long address, int write)
423 struct vm_area_struct *vma;
424 struct task_struct *tsk = current;
425 struct mm_struct *mm = tsk->mm;
426 siginfo_t info;
428 info.si_code = SEGV_MAPERR;
430 #if 0
431 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
432 tsk->pid, write, address);
433 #endif
434 down(&mm->mmap_sem);
435 vma = find_vma(mm, address);
436 if(!vma)
437 goto bad_area;
438 if(vma->vm_start <= address)
439 goto good_area;
440 if(!(vma->vm_flags & VM_GROWSDOWN))
441 goto bad_area;
442 if(expand_stack(vma, address))
443 goto bad_area;
444 good_area:
445 info.si_code = SEGV_ACCERR;
446 if(write) {
447 if(!(vma->vm_flags & VM_WRITE))
448 goto bad_area;
449 } else {
450 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
451 goto bad_area;
453 if (!handle_mm_fault(current, vma, address, write))
454 goto do_sigbus;
455 up(&mm->mmap_sem);
456 return;
457 bad_area:
458 up(&mm->mmap_sem);
459 #if 0
460 printk("Window whee %s [%d]: segfaults at %08lx\n",
461 tsk->comm, tsk->pid, address);
462 #endif
463 info.si_signo = SIGSEGV;
464 info.si_errno = 0;
465 /* info.si_code set above to make clear whether
466 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
467 info.si_addr = (void *)address;
468 info.si_trapno = 0;
469 force_sig_info (SIGSEGV, &info, tsk);
470 return;
472 do_sigbus:
473 up(&mm->mmap_sem);
474 info.si_signo = SIGBUS;
475 info.si_errno = 0;
476 info.si_code = BUS_ADRERR;
477 info.si_addr = (void *)address;
478 info.si_trapno = 0;
479 force_sig_info (SIGBUS, &info, tsk);
482 void window_overflow_fault(void)
484 unsigned long sp;
486 sp = current->thread.rwbuf_stkptrs[0];
487 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
488 force_user_fault(sp + 0x38, 1);
489 force_user_fault(sp, 1);
492 void window_underflow_fault(unsigned long sp)
494 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
495 force_user_fault(sp + 0x38, 0);
496 force_user_fault(sp, 0);
499 void window_ret_fault(struct pt_regs *regs)
501 unsigned long sp;
503 sp = regs->u_regs[UREG_FP];
504 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
505 force_user_fault(sp + 0x38, 0);
506 force_user_fault(sp, 0);