blob: 3fa09ba3845f3dafd06e80439e8214785d297f39 [file] [log] [blame]
Adrian Bunk88278ca2008-05-19 16:53:02 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * fault.c: Page fault handlers for the Sparc.
3 *
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 */
8
9#include <asm/head.h>
10
11#include <linux/string.h>
12#include <linux/types.h>
13#include <linux/sched.h>
14#include <linux/ptrace.h>
15#include <linux/mman.h>
16#include <linux/threads.h>
17#include <linux/kernel.h>
18#include <linux/signal.h>
19#include <linux/mm.h>
20#include <linux/smp.h>
David S. Millera084b662010-01-20 03:04:14 -080021#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070024#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/page.h>
28#include <asm/pgtable.h>
29#include <asm/memreg.h>
30#include <asm/openprom.h>
31#include <asm/oplib.h>
32#include <asm/smp.h>
33#include <asm/traps.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/uaccess.h>
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036extern int prom_node_root;
37
38/* At boot time we determine these two values necessary for setting
39 * up the segment maps and page table entries (pte's).
40 */
41
42int num_segmaps, num_contexts;
43int invalid_segment;
44
45/* various Virtual Address Cache parameters we find at boot time... */
46
47int vac_size, vac_linesize, vac_do_hw_vac_flushes;
48int vac_entries_per_context, vac_entries_per_segment;
49int vac_entries_per_page;
50
David S. Miller9f2b2a52008-05-01 22:28:17 -070051/* Return how much physical memory we have. */
52unsigned long probe_memory(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
David S. Miller9f2b2a52008-05-01 22:28:17 -070054 unsigned long total = 0;
55 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
David S. Miller9f2b2a52008-05-01 22:28:17 -070057 for (i = 0; sp_banks[i].num_bytes; i++)
58 total += sp_banks[i].num_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return total;
61}
62
63extern void sun4c_complete_all_stores(void);
64
65/* Whee, a level 15 NMI interrupt memory error. Let's have fun... */
66asmlinkage void sparc_lvl15_nmi(struct pt_regs *regs, unsigned long serr,
67 unsigned long svaddr, unsigned long aerr,
68 unsigned long avaddr)
69{
70 sun4c_complete_all_stores();
71 printk("FAULT: NMI received\n");
72 printk("SREGS: Synchronous Error %08lx\n", serr);
73 printk(" Synchronous Vaddr %08lx\n", svaddr);
74 printk(" Asynchronous Error %08lx\n", aerr);
75 printk(" Asynchronous Vaddr %08lx\n", avaddr);
76 if (sun4c_memerr_reg)
77 printk(" Memory Parity Error %08lx\n", *sun4c_memerr_reg);
78 printk("REGISTER DUMP:\n");
79 show_regs(regs);
80 prom_halt();
81}
82
83static void unhandled_fault(unsigned long, struct task_struct *,
84 struct pt_regs *) __attribute__ ((noreturn));
85
86static void unhandled_fault(unsigned long address, struct task_struct *tsk,
87 struct pt_regs *regs)
88{
89 if((unsigned long) address < PAGE_SIZE) {
90 printk(KERN_ALERT
91 "Unable to handle kernel NULL pointer dereference\n");
92 } else {
93 printk(KERN_ALERT "Unable to handle kernel paging request "
94 "at virtual address %08lx\n", address);
95 }
96 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %08lx\n",
97 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
98 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %08lx\n",
99 (tsk->mm ? (unsigned long) tsk->mm->pgd :
100 (unsigned long) tsk->active_mm->pgd));
101 die_if_kernel("Oops", regs);
102}
103
104asmlinkage int lookup_fault(unsigned long pc, unsigned long ret_pc,
105 unsigned long address)
106{
107 struct pt_regs regs;
108 unsigned long g2;
109 unsigned int insn;
110 int i;
111
112 i = search_extables_range(ret_pc, &g2);
113 switch (i) {
114 case 3:
115 /* load & store will be handled by fixup */
116 return 3;
117
118 case 1:
119 /* store will be handled by fixup, load will bump out */
120 /* for _to_ macros */
121 insn = *((unsigned int *) pc);
122 if ((insn >> 21) & 1)
123 return 1;
124 break;
125
126 case 2:
127 /* load will be handled by fixup, store will bump out */
128 /* for _from_ macros */
129 insn = *((unsigned int *) pc);
130 if (!((insn >> 21) & 1) || ((insn>>19)&0x3f) == 15)
131 return 2;
132 break;
133
134 default:
135 break;
136 };
137
138 memset(&regs, 0, sizeof (regs));
139 regs.pc = pc;
140 regs.npc = pc + 4;
141 __asm__ __volatile__(
142 "rd %%psr, %0\n\t"
143 "nop\n\t"
144 "nop\n\t"
145 "nop\n" : "=r" (regs.psr));
146 unhandled_fault(address, current, &regs);
147
148 /* Not reached */
149 return 0;
150}
151
152extern unsigned long safe_compute_effective_address(struct pt_regs *,
153 unsigned int);
154
155static unsigned long compute_si_addr(struct pt_regs *regs, int text_fault)
156{
157 unsigned int insn;
158
159 if (text_fault)
160 return regs->pc;
161
162 if (regs->psr & PSR_PS) {
163 insn = *(unsigned int *) regs->pc;
164 } else {
165 __get_user(insn, (unsigned int *) regs->pc);
166 }
167
168 return safe_compute_effective_address(regs, insn);
169}
170
171asmlinkage void do_sparc_fault(struct pt_regs *regs, int text_fault, int write,
172 unsigned long address)
173{
174 struct vm_area_struct *vma;
175 struct task_struct *tsk = current;
176 struct mm_struct *mm = tsk->mm;
177 unsigned int fixup;
178 unsigned long g2;
179 siginfo_t info;
180 int from_user = !(regs->psr & PSR_PS);
Nick Piggin83c54072007-07-19 01:47:05 -0700181 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if(text_fault)
184 address = regs->pc;
185
186 /*
187 * We fault-in kernel-space virtual memory on-demand. The
188 * 'reference' page table is init_mm.pgd.
189 *
190 * NOTE! We MUST NOT take any locks for this case. We may
191 * be in an interrupt or a critical region, and should
192 * only copy the information from the master page table,
193 * nothing more.
194 */
Adrian Bunk5110bd22008-08-31 20:59:37 -0700195 if (!ARCH_SUN4C && address >= TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 goto vmalloc_fault;
197
198 info.si_code = SEGV_MAPERR;
199
200 /*
201 * If we're in an interrupt or have no user
202 * context, we must not take the fault..
203 */
204 if (in_atomic() || !mm)
205 goto no_context;
206
David S. Millera084b662010-01-20 03:04:14 -0800207 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 down_read(&mm->mmap_sem);
210
211 /*
212 * The kernel referencing a bad kernel pointer can lock up
213 * a sun4c machine completely, so we must attempt recovery.
214 */
215 if(!from_user && address >= PAGE_OFFSET)
216 goto bad_area;
217
218 vma = find_vma(mm, address);
219 if(!vma)
220 goto bad_area;
221 if(vma->vm_start <= address)
222 goto good_area;
223 if(!(vma->vm_flags & VM_GROWSDOWN))
224 goto bad_area;
225 if(expand_stack(vma, address))
226 goto bad_area;
227 /*
228 * Ok, we have a good vm_area for this memory access, so
229 * we can handle it..
230 */
231good_area:
232 info.si_code = SEGV_ACCERR;
233 if(write) {
234 if(!(vma->vm_flags & VM_WRITE))
235 goto bad_area;
236 } else {
237 /* Allow reads even for write-only mappings */
238 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
239 goto bad_area;
240 }
241
242 /*
243 * If for any reason at all we couldn't handle the fault,
244 * make sure we exit gracefully rather than endlessly redo
245 * the fault.
246 */
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700247 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
Nick Piggin83c54072007-07-19 01:47:05 -0700248 if (unlikely(fault & VM_FAULT_ERROR)) {
249 if (fault & VM_FAULT_OOM)
250 goto out_of_memory;
251 else if (fault & VM_FAULT_SIGBUS)
252 goto do_sigbus;
253 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
David S. Millera084b662010-01-20 03:04:14 -0800255 if (fault & VM_FAULT_MAJOR) {
Nick Piggin83c54072007-07-19 01:47:05 -0700256 current->maj_flt++;
David S. Millera084b662010-01-20 03:04:14 -0800257 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
258 regs, address);
259 } else {
Nick Piggin83c54072007-07-19 01:47:05 -0700260 current->min_flt++;
David S. Millera084b662010-01-20 03:04:14 -0800261 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
262 regs, address);
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 up_read(&mm->mmap_sem);
265 return;
266
267 /*
268 * Something tried to access memory that isn't in our memory map..
269 * Fix it, but check if it's kernel or user first..
270 */
271bad_area:
272 up_read(&mm->mmap_sem);
273
274bad_area_nosemaphore:
275 /* User mode accesses just cause a SIGSEGV */
276 if(from_user) {
277#if 0
278 printk("Fault whee %s [%d]: segfaults at %08lx pc=%08lx\n",
279 tsk->comm, tsk->pid, address, regs->pc);
280#endif
281 info.si_signo = SIGSEGV;
282 info.si_errno = 0;
283 /* info.si_code set above to make clear whether
284 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
285 info.si_addr = (void __user *)compute_si_addr(regs, text_fault);
286 info.si_trapno = 0;
287 force_sig_info (SIGSEGV, &info, tsk);
288 return;
289 }
290
291 /* Is this in ex_table? */
292no_context:
293 g2 = regs->u_regs[UREG_G2];
Sam Ravnborg01571412009-01-06 12:52:41 -0800294 if (!from_user) {
295 fixup = search_extables_range(regs->pc, &g2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (fixup > 10) { /* Values below are reserved for other things */
297 extern const unsigned __memset_start[];
298 extern const unsigned __memset_end[];
299 extern const unsigned __csum_partial_copy_start[];
300 extern const unsigned __csum_partial_copy_end[];
301
302#ifdef DEBUG_EXCEPTIONS
303 printk("Exception: PC<%08lx> faddr<%08lx>\n", regs->pc, address);
304 printk("EX_TABLE: insn<%08lx> fixup<%08x> g2<%08lx>\n",
305 regs->pc, fixup, g2);
306#endif
307 if ((regs->pc >= (unsigned long)__memset_start &&
308 regs->pc < (unsigned long)__memset_end) ||
309 (regs->pc >= (unsigned long)__csum_partial_copy_start &&
310 regs->pc < (unsigned long)__csum_partial_copy_end)) {
311 regs->u_regs[UREG_I4] = address;
312 regs->u_regs[UREG_I5] = regs->pc;
313 }
314 regs->u_regs[UREG_G2] = g2;
315 regs->pc = fixup;
316 regs->npc = regs->pc + 4;
317 return;
318 }
319 }
320
321 unhandled_fault (address, tsk, regs);
322 do_exit(SIGKILL);
323
324/*
325 * We ran out of memory, or some other thing happened to us that made
326 * us unable to handle the page fault gracefully.
327 */
328out_of_memory:
329 up_read(&mm->mmap_sem);
David S. Millera923c282009-08-02 19:17:15 -0700330 if (from_user) {
331 pagefault_out_of_memory();
332 return;
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto no_context;
335
336do_sigbus:
337 up_read(&mm->mmap_sem);
338 info.si_signo = SIGBUS;
339 info.si_errno = 0;
340 info.si_code = BUS_ADRERR;
341 info.si_addr = (void __user *) compute_si_addr(regs, text_fault);
342 info.si_trapno = 0;
343 force_sig_info (SIGBUS, &info, tsk);
344 if (!from_user)
345 goto no_context;
346
347vmalloc_fault:
348 {
349 /*
350 * Synchronize this task's top level page-table
351 * with the 'reference' page table.
352 */
353 int offset = pgd_index(address);
354 pgd_t *pgd, *pgd_k;
355 pmd_t *pmd, *pmd_k;
356
357 pgd = tsk->active_mm->pgd + offset;
358 pgd_k = init_mm.pgd + offset;
359
360 if (!pgd_present(*pgd)) {
361 if (!pgd_present(*pgd_k))
362 goto bad_area_nosemaphore;
363 pgd_val(*pgd) = pgd_val(*pgd_k);
364 return;
365 }
366
367 pmd = pmd_offset(pgd, address);
368 pmd_k = pmd_offset(pgd_k, address);
369
370 if (pmd_present(*pmd) || !pmd_present(*pmd_k))
371 goto bad_area_nosemaphore;
372 *pmd = *pmd_k;
373 return;
374 }
375}
376
377asmlinkage void do_sun4c_fault(struct pt_regs *regs, int text_fault, int write,
378 unsigned long address)
379{
380 extern void sun4c_update_mmu_cache(struct vm_area_struct *,
Russell King4b3073e2009-12-18 16:40:18 +0000381 unsigned long,pte_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 extern pte_t *sun4c_pte_offset_kernel(pmd_t *,unsigned long);
383 struct task_struct *tsk = current;
384 struct mm_struct *mm = tsk->mm;
385 pgd_t *pgdp;
386 pte_t *ptep;
387
388 if (text_fault) {
389 address = regs->pc;
390 } else if (!write &&
391 !(regs->psr & PSR_PS)) {
392 unsigned int insn, __user *ip;
393
394 ip = (unsigned int __user *)regs->pc;
395 if (!get_user(insn, ip)) {
396 if ((insn & 0xc1680000) == 0xc0680000)
397 write = 1;
398 }
399 }
400
401 if (!mm) {
402 /* We are oopsing. */
403 do_sparc_fault(regs, text_fault, write, address);
404 BUG(); /* P3 Oops already, you bitch */
405 }
406
407 pgdp = pgd_offset(mm, address);
408 ptep = sun4c_pte_offset_kernel((pmd_t *) pgdp, address);
409
410 if (pgd_val(*pgdp)) {
411 if (write) {
412 if ((pte_val(*ptep) & (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT))
413 == (_SUN4C_PAGE_WRITE|_SUN4C_PAGE_PRESENT)) {
414 unsigned long flags;
415
416 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
417 _SUN4C_PAGE_MODIFIED |
418 _SUN4C_PAGE_VALID |
419 _SUN4C_PAGE_DIRTY);
420
421 local_irq_save(flags);
422 if (sun4c_get_segmap(address) != invalid_segment) {
423 sun4c_put_pte(address, pte_val(*ptep));
424 local_irq_restore(flags);
425 return;
426 }
427 local_irq_restore(flags);
428 }
429 } else {
430 if ((pte_val(*ptep) & (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT))
431 == (_SUN4C_PAGE_READ|_SUN4C_PAGE_PRESENT)) {
432 unsigned long flags;
433
434 *ptep = __pte(pte_val(*ptep) | _SUN4C_PAGE_ACCESSED |
435 _SUN4C_PAGE_VALID);
436
437 local_irq_save(flags);
438 if (sun4c_get_segmap(address) != invalid_segment) {
439 sun4c_put_pte(address, pte_val(*ptep));
440 local_irq_restore(flags);
441 return;
442 }
443 local_irq_restore(flags);
444 }
445 }
446 }
447
448 /* This conditional is 'interesting'. */
449 if (pgd_val(*pgdp) && !(write && !(pte_val(*ptep) & _SUN4C_PAGE_WRITE))
450 && (pte_val(*ptep) & _SUN4C_PAGE_VALID))
451 /* Note: It is safe to not grab the MMAP semaphore here because
452 * we know that update_mmu_cache() will not sleep for
453 * any reason (at least not in the current implementation)
454 * and therefore there is no danger of another thread getting
455 * on the CPU and doing a shrink_mmap() on this vma.
456 */
457 sun4c_update_mmu_cache (find_vma(current->mm, address), address,
Russell King4b3073e2009-12-18 16:40:18 +0000458 ptep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 else
460 do_sparc_fault(regs, text_fault, write, address);
461}
462
463/* This always deals with user addresses. */
Adrian Bunk50215d62008-06-05 11:41:51 -0700464static void force_user_fault(unsigned long address, int write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 struct vm_area_struct *vma;
467 struct task_struct *tsk = current;
468 struct mm_struct *mm = tsk->mm;
469 siginfo_t info;
470
471 info.si_code = SEGV_MAPERR;
472
473#if 0
474 printk("wf<pid=%d,wr=%d,addr=%08lx>\n",
475 tsk->pid, write, address);
476#endif
477 down_read(&mm->mmap_sem);
478 vma = find_vma(mm, address);
479 if(!vma)
480 goto bad_area;
481 if(vma->vm_start <= address)
482 goto good_area;
483 if(!(vma->vm_flags & VM_GROWSDOWN))
484 goto bad_area;
485 if(expand_stack(vma, address))
486 goto bad_area;
487good_area:
488 info.si_code = SEGV_ACCERR;
489 if(write) {
490 if(!(vma->vm_flags & VM_WRITE))
491 goto bad_area;
492 } else {
493 if(!(vma->vm_flags & (VM_READ | VM_EXEC)))
494 goto bad_area;
495 }
Linus Torvaldsd06063c2009-04-10 09:01:23 -0700496 switch (handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 case VM_FAULT_SIGBUS:
498 case VM_FAULT_OOM:
499 goto do_sigbus;
500 }
501 up_read(&mm->mmap_sem);
502 return;
503bad_area:
504 up_read(&mm->mmap_sem);
505#if 0
506 printk("Window whee %s [%d]: segfaults at %08lx\n",
507 tsk->comm, tsk->pid, address);
508#endif
509 info.si_signo = SIGSEGV;
510 info.si_errno = 0;
511 /* info.si_code set above to make clear whether
512 this was a SEGV_MAPERR or SEGV_ACCERR fault. */
513 info.si_addr = (void __user *) address;
514 info.si_trapno = 0;
515 force_sig_info (SIGSEGV, &info, tsk);
516 return;
517
518do_sigbus:
519 up_read(&mm->mmap_sem);
520 info.si_signo = SIGBUS;
521 info.si_errno = 0;
522 info.si_code = BUS_ADRERR;
523 info.si_addr = (void __user *) address;
524 info.si_trapno = 0;
525 force_sig_info (SIGBUS, &info, tsk);
526}
527
528void window_overflow_fault(void)
529{
530 unsigned long sp;
531
532 sp = current_thread_info()->rwbuf_stkptrs[0];
533 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
534 force_user_fault(sp + 0x38, 1);
535 force_user_fault(sp, 1);
536}
537
538void window_underflow_fault(unsigned long sp)
539{
540 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
541 force_user_fault(sp + 0x38, 0);
542 force_user_fault(sp, 0);
543}
544
545void window_ret_fault(struct pt_regs *regs)
546{
547 unsigned long sp;
548
549 sp = regs->u_regs[UREG_FP];
550 if(((sp + 0x38) & PAGE_MASK) != (sp & PAGE_MASK))
551 force_user_fault(sp + 0x38, 0);
552 force_user_fault(sp, 0);
553}