Import 2.3.41pre2
[davej-history.git] / arch / sparc / mm / fault.c
blobd9981e68b6a5bcb0053b2cb0fccb7f5feddbadcd
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)
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 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;
349 pgd_t *pgdp;
350 pte_t *ptep;
352 if (text_fault) {
353 address = regs->pc;
354 } else if (!write &&
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)
361 write = 1;
365 pgdp = sun4c_pgd_offset(mm, address);
366 ptep = sun4c_pte_offset((pmd_t *) pgdp, address);
368 if (pgd_val(*pgdp)) {
369 if (write) {
370 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
371 == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
372 unsigned long flags;
374 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
375 _SUN4C_PAGE_MODIFIED |
376 _SUN4C_PAGE_VALID |
377 _SUN4C_PAGE_DIRTY);
379 save_and_cli(flags);
380 if (sun4c_get_segmap(address) != invalid_segment) {
381 sun4c_put_pte(address, pte_val(*ptep));
382 restore_flags(flags);
383 return;
385 restore_flags(flags);
387 } else {
388 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
389 == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
390 unsigned long flags;
392 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
393 _SUN4C_PAGE_VALID);
395 save_and_cli(flags);
396 if (sun4c_get_segmap(address) != invalid_segment) {
397 sun4c_put_pte(address, pte_val(*ptep));
398 restore_flags(flags);
399 return;
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,
416 *ptep);
417 else
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;
427 siginfo_t info;
429 info.si_code = SEGV_MAPERR;
431 #if 0
432 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
433 tsk->pid, write, address);
434 #endif
435 down(&mm->mmap_sem);
436 vma = find_vma(mm, address);
437 if(!vma)
438 goto bad_area;
439 if(vma->vm_start <= address)
440 goto good_area;
441 if(!(vma->vm_flags & VM_GROWSDOWN))
442 goto bad_area;
443 if(expand_stack(vma, address))
444 goto bad_area;
445 good_area:
446 info.si_code = SEGV_ACCERR;
447 if(write) {
448 if(!(vma->vm_flags & VM_WRITE))
449 goto bad_area;
450 } else {
451 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
452 goto bad_area;
454 if (!handle_mm_fault(current, vma, address, write))
455 goto do_sigbus;
456 up(&mm->mmap_sem);
457 return;
458 bad_area:
459 up(&mm->mmap_sem);
460 #if 0
461 printk("Window whee %s [%d]: segfaults at %08lx\n",
462 tsk->comm, tsk->pid, address);
463 #endif
464 info.si_signo = SIGSEGV;
465 info.si_errno = 0;
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;
469 info.si_trapno = 0;
470 force_sig_info (SIGSEGV, &info, tsk);
471 return;
473 do_sigbus:
474 up(&mm->mmap_sem);
475 info.si_signo = SIGBUS;
476 info.si_errno = 0;
477 info.si_code = BUS_ADRERR;
478 info.si_addr = (void *)address;
479 info.si_trapno = 0;
480 force_sig_info (SIGBUS, &info, tsk);
483 void window_overflow_fault(void)
485 unsigned long sp;
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)
502 unsigned long sp;
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);