blob: 4a52e79d515f1e11e6eff56975341f0ec7ef90d1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $Id: fault.c,v 1.59 2002/02/09 19:49:31 davem Exp $
2 * arch/sparc64/mm/fault.c: Page fault handlers for the 64-bit Sparc.
3 *
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
6 */
7
8#include <asm/head.h>
9
10#include <linux/string.h>
11#include <linux/types.h>
12#include <linux/sched.h>
13#include <linux/ptrace.h>
14#include <linux/mman.h>
15#include <linux/signal.h>
16#include <linux/mm.h>
17#include <linux/module.h>
18#include <linux/smp_lock.h>
19#include <linux/init.h>
20#include <linux/interrupt.h>
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070021#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <asm/page.h>
24#include <asm/pgtable.h>
25#include <asm/openprom.h>
26#include <asm/oplib.h>
27#include <asm/uaccess.h>
28#include <asm/asi.h>
29#include <asm/lsu.h>
30#include <asm/sections.h>
31#include <asm/kdebug.h>
32
33#define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
34
35extern struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
36
37/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 * To debug kernel to catch accesses to certain virtual/physical addresses.
39 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
40 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
41 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
42 * watched. This is only useful on a single cpu machine for now. After the watchpoint
43 * is detected, the process causing it will be killed, thus preventing an infinite loop.
44 */
45void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
46{
47 unsigned long lsubits;
48
49 __asm__ __volatile__("ldxa [%%g0] %1, %0"
50 : "=r" (lsubits)
51 : "i" (ASI_LSU_CONTROL));
52 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
53 LSU_CONTROL_PR | LSU_CONTROL_VR |
54 LSU_CONTROL_PW | LSU_CONTROL_VW);
55
56 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
57 "membar #Sync"
58 : /* no outputs */
59 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
60 "i" (ASI_DMMU));
61
62 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
63 if (flags & VM_READ)
64 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
65 if (flags & VM_WRITE)
66 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
67 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
68 "membar #Sync"
69 : /* no outputs */
70 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
71 : "memory");
72}
73
74/* Nice, simple, prom library does all the sweating for us. ;) */
75unsigned long __init prom_probe_memory (void)
76{
77 register struct linux_mlist_p1275 *mlist;
78 register unsigned long bytes, base_paddr, tally;
79 register int i;
80
81 i = 0;
82 mlist = *prom_meminfo()->p1275_available;
83 bytes = tally = mlist->num_bytes;
84 base_paddr = mlist->start_adr;
85
86 sp_banks[0].base_addr = base_paddr;
87 sp_banks[0].num_bytes = bytes;
88
89 while (mlist->theres_more != (void *) 0) {
90 i++;
91 mlist = mlist->theres_more;
92 bytes = mlist->num_bytes;
93 tally += bytes;
94 if (i >= SPARC_PHYS_BANKS-1) {
95 printk ("The machine has more banks than "
96 "this kernel can support\n"
97 "Increase the SPARC_PHYS_BANKS "
98 "setting (currently %d)\n",
99 SPARC_PHYS_BANKS);
100 i = SPARC_PHYS_BANKS-1;
101 break;
102 }
103
104 sp_banks[i].base_addr = mlist->start_adr;
105 sp_banks[i].num_bytes = mlist->num_bytes;
106 }
107
108 i++;
109 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
110 sp_banks[i].num_bytes = 0;
111
112 /* Now mask all bank sizes on a page boundary, it is all we can
113 * use anyways.
114 */
115 for (i = 0; sp_banks[i].num_bytes != 0; i++)
116 sp_banks[i].num_bytes &= PAGE_MASK;
117
118 return tally;
119}
120
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700121static void __kprobes unhandled_fault(unsigned long address,
122 struct task_struct *tsk,
123 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 if ((unsigned long) address < PAGE_SIZE) {
126 printk(KERN_ALERT "Unable to handle kernel NULL "
127 "pointer dereference\n");
128 } else {
129 printk(KERN_ALERT "Unable to handle kernel paging request "
130 "at virtual address %016lx\n", (unsigned long)address);
131 }
132 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
133 (tsk->mm ?
134 CTX_HWBITS(tsk->mm->context) :
135 CTX_HWBITS(tsk->active_mm->context)));
136 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
137 (tsk->mm ? (unsigned long) tsk->mm->pgd :
138 (unsigned long) tsk->active_mm->pgd));
139 if (notify_die(DIE_GPF, "general protection fault", regs,
140 0, 0, SIGSEGV) == NOTIFY_STOP)
141 return;
142 die_if_kernel("Oops", regs);
143}
144
145static void bad_kernel_pc(struct pt_regs *regs)
146{
147 unsigned long *ksp;
148
149 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
150 regs->tpc);
151 __asm__("mov %%sp, %0" : "=r" (ksp));
152 show_stack(current, ksp);
153 unhandled_fault(regs->tpc, current, regs);
154}
155
156/*
157 * We now make sure that mmap_sem is held in all paths that call
158 * this. Additionally, to prevent kswapd from ripping ptes from
159 * under us, raise interrupts around the time that we look at the
160 * pte, kswapd will have to wait to get his smp ipi response from
161 * us. This saves us having to get page_table_lock.
162 */
163static unsigned int get_user_insn(unsigned long tpc)
164{
165 pgd_t *pgdp = pgd_offset(current->mm, tpc);
166 pud_t *pudp;
167 pmd_t *pmdp;
168 pte_t *ptep, pte;
169 unsigned long pa;
170 u32 insn = 0;
171 unsigned long pstate;
172
173 if (pgd_none(*pgdp))
174 goto outret;
175 pudp = pud_offset(pgdp, tpc);
176 if (pud_none(*pudp))
177 goto outret;
178 pmdp = pmd_offset(pudp, tpc);
179 if (pmd_none(*pmdp))
180 goto outret;
181
182 /* This disables preemption for us as well. */
183 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
184 __asm__ __volatile__("wrpr %0, %1, %%pstate"
185 : : "r" (pstate), "i" (PSTATE_IE));
186 ptep = pte_offset_map(pmdp, tpc);
187 pte = *ptep;
188 if (!pte_present(pte))
189 goto out;
190
191 pa = (pte_val(pte) & _PAGE_PADDR);
192 pa += (tpc & ~PAGE_MASK);
193
194 /* Use phys bypass so we don't pollute dtlb/dcache. */
195 __asm__ __volatile__("lduwa [%1] %2, %0"
196 : "=r" (insn)
197 : "r" (pa), "i" (ASI_PHYS_USE_EC));
198
199out:
200 pte_unmap(ptep);
201 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
202outret:
203 return insn;
204}
205
206extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
207
208static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
209 unsigned int insn, int fault_code)
210{
211 siginfo_t info;
212
213 info.si_code = code;
214 info.si_signo = sig;
215 info.si_errno = 0;
216 if (fault_code & FAULT_CODE_ITLB)
217 info.si_addr = (void __user *) regs->tpc;
218 else
219 info.si_addr = (void __user *)
220 compute_effective_address(regs, insn, 0);
221 info.si_trapno = 0;
222 force_sig_info(sig, &info, current);
223}
224
225extern int handle_ldf_stq(u32, struct pt_regs *);
226extern int handle_ld_nf(u32, struct pt_regs *);
227
228static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
229{
230 if (!insn) {
231 if (!regs->tpc || (regs->tpc & 0x3))
232 return 0;
233 if (regs->tstate & TSTATE_PRIV) {
234 insn = *(unsigned int *) regs->tpc;
235 } else {
236 insn = get_user_insn(regs->tpc);
237 }
238 }
239 return insn;
240}
241
242static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
243 unsigned int insn, unsigned long address)
244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned char asi = ASI_P;
246
247 if ((!insn) && (regs->tstate & TSTATE_PRIV))
248 goto cannot_handle;
249
250 /* If user insn could be read (thus insn is zero), that
251 * is fine. We will just gun down the process with a signal
252 * in that case.
253 */
254
255 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
256 (insn & 0xc0800000) == 0xc0800000) {
257 if (insn & 0x2000)
258 asi = (regs->tstate >> 24);
259 else
260 asi = (insn >> 5);
261 if ((asi & 0xf2) == 0x82) {
262 if (insn & 0x1000000) {
263 handle_ldf_stq(insn, regs);
264 } else {
265 /* This was a non-faulting load. Just clear the
266 * destination register(s) and continue with the next
267 * instruction. -jj
268 */
269 handle_ld_nf(insn, regs);
270 }
271 return;
272 }
273 }
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Is this in ex_table? */
276 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700277 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
280 if (insn & 0x2000)
281 asi = (regs->tstate >> 24);
282 else
283 asi = (insn >> 5);
284 }
285
286 /* Look in asi.h: All _S asis have LS bit set */
287 if ((asi & 0x1) &&
David S. Miller8cf14af2005-09-28 20:21:11 -0700288 (entry = search_exception_tables(regs->tpc))) {
289 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return;
292 }
293 } else {
294 /* The si_code was set to make clear whether
295 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
296 */
297 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
298 return;
299 }
300
301cannot_handle:
302 unhandled_fault (address, current, regs);
303}
304
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700305asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 struct mm_struct *mm = current->mm;
308 struct vm_area_struct *vma;
309 unsigned int insn = 0;
310 int si_code, fault_code;
311 unsigned long address;
312
313 fault_code = get_thread_fault_code();
314
315 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
316 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
317 return;
318
319 si_code = SEGV_MAPERR;
320 address = current_thread_info()->fault_address;
321
322 if ((fault_code & FAULT_CODE_ITLB) &&
323 (fault_code & FAULT_CODE_DTLB))
324 BUG();
325
326 if (regs->tstate & TSTATE_PRIV) {
327 unsigned long tpc = regs->tpc;
328
329 /* Sanity check the PC. */
330 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
331 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
332 /* Valid, no problems... */
333 } else {
334 bad_kernel_pc(regs);
335 return;
336 }
337 }
338
339 /*
340 * If we're in an interrupt or have no user
341 * context, we must not take the fault..
342 */
343 if (in_atomic() || !mm)
344 goto intr_or_no_mm;
345
346 if (test_thread_flag(TIF_32BIT)) {
347 if (!(regs->tstate & TSTATE_PRIV))
348 regs->tpc &= 0xffffffff;
349 address &= 0xffffffff;
350 }
351
352 if (!down_read_trylock(&mm->mmap_sem)) {
353 if ((regs->tstate & TSTATE_PRIV) &&
354 !search_exception_tables(regs->tpc)) {
355 insn = get_fault_insn(regs, insn);
356 goto handle_kernel_fault;
357 }
358 down_read(&mm->mmap_sem);
359 }
360
361 vma = find_vma(mm, address);
362 if (!vma)
363 goto bad_area;
364
365 /* Pure DTLB misses do not tell us whether the fault causing
366 * load/store/atomic was a write or not, it only says that there
367 * was no match. So in such a case we (carefully) read the
368 * instruction to try and figure this out. It's an optimization
369 * so it's ok if we can't do this.
370 *
371 * Special hack, window spill/fill knows the exact fault type.
372 */
373 if (((fault_code &
374 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
375 (vma->vm_flags & VM_WRITE) != 0) {
376 insn = get_fault_insn(regs, 0);
377 if (!insn)
378 goto continue_fault;
379 if ((insn & 0xc0200000) == 0xc0200000 &&
380 (insn & 0x1780000) != 0x1680000) {
381 /* Don't bother updating thread struct value,
382 * because update_mmu_cache only cares which tlb
383 * the access came from.
384 */
385 fault_code |= FAULT_CODE_WRITE;
386 }
387 }
388continue_fault:
389
390 if (vma->vm_start <= address)
391 goto good_area;
392 if (!(vma->vm_flags & VM_GROWSDOWN))
393 goto bad_area;
394 if (!(fault_code & FAULT_CODE_WRITE)) {
395 /* Non-faulting loads shouldn't expand stack. */
396 insn = get_fault_insn(regs, insn);
397 if ((insn & 0xc0800000) == 0xc0800000) {
398 unsigned char asi;
399
400 if (insn & 0x2000)
401 asi = (regs->tstate >> 24);
402 else
403 asi = (insn >> 5);
404 if ((asi & 0xf2) == 0x82)
405 goto bad_area;
406 }
407 }
408 if (expand_stack(vma, address))
409 goto bad_area;
410 /*
411 * Ok, we have a good vm_area for this memory access, so
412 * we can handle it..
413 */
414good_area:
415 si_code = SEGV_ACCERR;
416
417 /* If we took a ITLB miss on a non-executable page, catch
418 * that here.
419 */
420 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
421 BUG_ON(address != regs->tpc);
422 BUG_ON(regs->tstate & TSTATE_PRIV);
423 goto bad_area;
424 }
425
426 if (fault_code & FAULT_CODE_WRITE) {
427 if (!(vma->vm_flags & VM_WRITE))
428 goto bad_area;
429
430 /* Spitfire has an icache which does not snoop
431 * processor stores. Later processors do...
432 */
433 if (tlb_type == spitfire &&
434 (vma->vm_flags & VM_EXEC) != 0 &&
435 vma->vm_file != NULL)
436 set_thread_fault_code(fault_code |
437 FAULT_CODE_BLKCOMMIT);
438 } else {
439 /* Allow reads even for write-only mappings */
440 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
441 goto bad_area;
442 }
443
444 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
445 case VM_FAULT_MINOR:
446 current->min_flt++;
447 break;
448 case VM_FAULT_MAJOR:
449 current->maj_flt++;
450 break;
451 case VM_FAULT_SIGBUS:
452 goto do_sigbus;
453 case VM_FAULT_OOM:
454 goto out_of_memory;
455 default:
456 BUG();
457 }
458
459 up_read(&mm->mmap_sem);
David S. Millerefdc1e22005-09-28 21:06:47 -0700460 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * Something tried to access memory that isn't in our memory map..
464 * Fix it, but check if it's kernel or user first..
465 */
466bad_area:
467 insn = get_fault_insn(regs, insn);
468 up_read(&mm->mmap_sem);
469
470handle_kernel_fault:
471 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700472 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474/*
475 * We ran out of memory, or some other thing happened to us that made
476 * us unable to handle the page fault gracefully.
477 */
478out_of_memory:
479 insn = get_fault_insn(regs, insn);
480 up_read(&mm->mmap_sem);
481 printk("VM: killing process %s\n", current->comm);
482 if (!(regs->tstate & TSTATE_PRIV))
483 do_exit(SIGKILL);
484 goto handle_kernel_fault;
485
486intr_or_no_mm:
487 insn = get_fault_insn(regs, 0);
488 goto handle_kernel_fault;
489
490do_sigbus:
491 insn = get_fault_insn(regs, insn);
492 up_read(&mm->mmap_sem);
493
494 /*
495 * Send a sigbus, regardless of whether we were in kernel
496 * or user mode.
497 */
498 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
499
500 /* Kernel mode? Handle exceptions or die */
501 if (regs->tstate & TSTATE_PRIV)
502 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}