blob: 6e002aacb961832113ba30d522d437460ee20612 [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>
David S. Miller7a1ac522006-03-16 02:02:32 -080032#include <asm/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * To debug kernel to catch accesses to certain virtual/physical addresses.
36 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
37 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
38 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
39 * watched. This is only useful on a single cpu machine for now. After the watchpoint
40 * is detected, the process causing it will be killed, thus preventing an infinite loop.
41 */
42void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
43{
44 unsigned long lsubits;
45
46 __asm__ __volatile__("ldxa [%%g0] %1, %0"
47 : "=r" (lsubits)
48 : "i" (ASI_LSU_CONTROL));
49 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
50 LSU_CONTROL_PR | LSU_CONTROL_VR |
51 LSU_CONTROL_PW | LSU_CONTROL_VW);
52
53 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
54 "membar #Sync"
55 : /* no outputs */
56 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
57 "i" (ASI_DMMU));
58
59 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
60 if (flags & VM_READ)
61 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
62 if (flags & VM_WRITE)
63 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
64 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
65 "membar #Sync"
66 : /* no outputs */
67 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
68 : "memory");
69}
70
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -070071static void __kprobes unhandled_fault(unsigned long address,
72 struct task_struct *tsk,
73 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
75 if ((unsigned long) address < PAGE_SIZE) {
76 printk(KERN_ALERT "Unable to handle kernel NULL "
77 "pointer dereference\n");
78 } else {
79 printk(KERN_ALERT "Unable to handle kernel paging request "
80 "at virtual address %016lx\n", (unsigned long)address);
81 }
82 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
83 (tsk->mm ?
84 CTX_HWBITS(tsk->mm->context) :
85 CTX_HWBITS(tsk->active_mm->context)));
86 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
87 (tsk->mm ? (unsigned long) tsk->mm->pgd :
88 (unsigned long) tsk->active_mm->pgd));
89 if (notify_die(DIE_GPF, "general protection fault", regs,
90 0, 0, SIGSEGV) == NOTIFY_STOP)
91 return;
92 die_if_kernel("Oops", regs);
93}
94
David S. Millerbf941d62006-02-13 18:07:45 -080095static void bad_kernel_pc(struct pt_regs *regs, unsigned long vaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 unsigned long *ksp;
98
99 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
100 regs->tpc);
David S. Millerbf941d62006-02-13 18:07:45 -0800101 printk(KERN_CRIT "OOPS: Fault was to vaddr[%lx]\n", vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 __asm__("mov %%sp, %0" : "=r" (ksp));
103 show_stack(current, ksp);
104 unhandled_fault(regs->tpc, current, regs);
105}
106
107/*
108 * We now make sure that mmap_sem is held in all paths that call
109 * this. Additionally, to prevent kswapd from ripping ptes from
110 * under us, raise interrupts around the time that we look at the
111 * pte, kswapd will have to wait to get his smp ipi response from
Hugh Dickinsda160542005-11-08 10:00:55 -0800112 * us. vmtruncate likewise. This saves us having to get pte lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114static unsigned int get_user_insn(unsigned long tpc)
115{
116 pgd_t *pgdp = pgd_offset(current->mm, tpc);
117 pud_t *pudp;
118 pmd_t *pmdp;
119 pte_t *ptep, pte;
120 unsigned long pa;
121 u32 insn = 0;
122 unsigned long pstate;
123
124 if (pgd_none(*pgdp))
125 goto outret;
126 pudp = pud_offset(pgdp, tpc);
127 if (pud_none(*pudp))
128 goto outret;
129 pmdp = pmd_offset(pudp, tpc);
130 if (pmd_none(*pmdp))
131 goto outret;
132
133 /* This disables preemption for us as well. */
134 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
135 __asm__ __volatile__("wrpr %0, %1, %%pstate"
136 : : "r" (pstate), "i" (PSTATE_IE));
137 ptep = pte_offset_map(pmdp, tpc);
138 pte = *ptep;
139 if (!pte_present(pte))
140 goto out;
141
David S. Millerc4bce902006-02-11 21:57:54 -0800142 pa = (pte_pfn(pte) << PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 pa += (tpc & ~PAGE_MASK);
144
145 /* Use phys bypass so we don't pollute dtlb/dcache. */
146 __asm__ __volatile__("lduwa [%1] %2, %0"
147 : "=r" (insn)
148 : "r" (pa), "i" (ASI_PHYS_USE_EC));
149
150out:
151 pte_unmap(ptep);
152 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
153outret:
154 return insn;
155}
156
157extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
158
159static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
160 unsigned int insn, int fault_code)
161{
162 siginfo_t info;
163
164 info.si_code = code;
165 info.si_signo = sig;
166 info.si_errno = 0;
167 if (fault_code & FAULT_CODE_ITLB)
168 info.si_addr = (void __user *) regs->tpc;
169 else
170 info.si_addr = (void __user *)
171 compute_effective_address(regs, insn, 0);
172 info.si_trapno = 0;
173 force_sig_info(sig, &info, current);
174}
175
176extern int handle_ldf_stq(u32, struct pt_regs *);
177extern int handle_ld_nf(u32, struct pt_regs *);
178
179static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
180{
181 if (!insn) {
182 if (!regs->tpc || (regs->tpc & 0x3))
183 return 0;
184 if (regs->tstate & TSTATE_PRIV) {
185 insn = *(unsigned int *) regs->tpc;
186 } else {
187 insn = get_user_insn(regs->tpc);
188 }
189 }
190 return insn;
191}
192
193static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
194 unsigned int insn, unsigned long address)
195{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned char asi = ASI_P;
197
198 if ((!insn) && (regs->tstate & TSTATE_PRIV))
199 goto cannot_handle;
200
201 /* If user insn could be read (thus insn is zero), that
202 * is fine. We will just gun down the process with a signal
203 * in that case.
204 */
205
206 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
207 (insn & 0xc0800000) == 0xc0800000) {
208 if (insn & 0x2000)
209 asi = (regs->tstate >> 24);
210 else
211 asi = (insn >> 5);
212 if ((asi & 0xf2) == 0x82) {
213 if (insn & 0x1000000) {
214 handle_ldf_stq(insn, regs);
215 } else {
216 /* This was a non-faulting load. Just clear the
217 * destination register(s) and continue with the next
218 * instruction. -jj
219 */
220 handle_ld_nf(insn, regs);
221 }
222 return;
223 }
224 }
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Is this in ex_table? */
227 if (regs->tstate & TSTATE_PRIV) {
David S. Miller8cf14af2005-09-28 20:21:11 -0700228 const struct exception_table_entry *entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
231 if (insn & 0x2000)
232 asi = (regs->tstate >> 24);
233 else
234 asi = (insn >> 5);
235 }
236
237 /* Look in asi.h: All _S asis have LS bit set */
238 if ((asi & 0x1) &&
David S. Miller8cf14af2005-09-28 20:21:11 -0700239 (entry = search_exception_tables(regs->tpc))) {
240 regs->tpc = entry->fixup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 regs->tnpc = regs->tpc + 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return;
243 }
244 } else {
245 /* The si_code was set to make clear whether
246 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
247 */
248 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
249 return;
250 }
251
252cannot_handle:
253 unhandled_fault (address, current, regs);
254}
255
Prasanna S Panchamukhi05e14cb2005-09-06 15:19:30 -0700256asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 struct mm_struct *mm = current->mm;
259 struct vm_area_struct *vma;
260 unsigned int insn = 0;
261 int si_code, fault_code;
David S. Miller7a1ac522006-03-16 02:02:32 -0800262 unsigned long address, mm_rss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 fault_code = get_thread_fault_code();
265
266 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
267 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
268 return;
269
270 si_code = SEGV_MAPERR;
271 address = current_thread_info()->fault_address;
272
273 if ((fault_code & FAULT_CODE_ITLB) &&
274 (fault_code & FAULT_CODE_DTLB))
275 BUG();
276
277 if (regs->tstate & TSTATE_PRIV) {
278 unsigned long tpc = regs->tpc;
279
280 /* Sanity check the PC. */
281 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
282 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
283 /* Valid, no problems... */
284 } else {
David S. Millerbf941d62006-02-13 18:07:45 -0800285 bad_kernel_pc(regs, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return;
287 }
288 }
289
290 /*
291 * If we're in an interrupt or have no user
292 * context, we must not take the fault..
293 */
294 if (in_atomic() || !mm)
295 goto intr_or_no_mm;
296
297 if (test_thread_flag(TIF_32BIT)) {
298 if (!(regs->tstate & TSTATE_PRIV))
299 regs->tpc &= 0xffffffff;
300 address &= 0xffffffff;
301 }
302
303 if (!down_read_trylock(&mm->mmap_sem)) {
304 if ((regs->tstate & TSTATE_PRIV) &&
305 !search_exception_tables(regs->tpc)) {
306 insn = get_fault_insn(regs, insn);
307 goto handle_kernel_fault;
308 }
309 down_read(&mm->mmap_sem);
310 }
311
312 vma = find_vma(mm, address);
313 if (!vma)
314 goto bad_area;
315
316 /* Pure DTLB misses do not tell us whether the fault causing
317 * load/store/atomic was a write or not, it only says that there
318 * was no match. So in such a case we (carefully) read the
319 * instruction to try and figure this out. It's an optimization
320 * so it's ok if we can't do this.
321 *
322 * Special hack, window spill/fill knows the exact fault type.
323 */
324 if (((fault_code &
325 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
326 (vma->vm_flags & VM_WRITE) != 0) {
327 insn = get_fault_insn(regs, 0);
328 if (!insn)
329 goto continue_fault;
David S. Miller73c50a22006-03-28 13:32:24 -0800330 /* All loads, stores and atomics have bits 30 and 31 both set
331 * in the instruction. Bit 21 is set in all stores, but we
332 * have to avoid prefetches which also have bit 21 set.
333 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if ((insn & 0xc0200000) == 0xc0200000 &&
David S. Miller73c50a22006-03-28 13:32:24 -0800335 (insn & 0x01780000) != 0x01680000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /* Don't bother updating thread struct value,
337 * because update_mmu_cache only cares which tlb
338 * the access came from.
339 */
340 fault_code |= FAULT_CODE_WRITE;
341 }
342 }
343continue_fault:
344
345 if (vma->vm_start <= address)
346 goto good_area;
347 if (!(vma->vm_flags & VM_GROWSDOWN))
348 goto bad_area;
349 if (!(fault_code & FAULT_CODE_WRITE)) {
350 /* Non-faulting loads shouldn't expand stack. */
351 insn = get_fault_insn(regs, insn);
352 if ((insn & 0xc0800000) == 0xc0800000) {
353 unsigned char asi;
354
355 if (insn & 0x2000)
356 asi = (regs->tstate >> 24);
357 else
358 asi = (insn >> 5);
359 if ((asi & 0xf2) == 0x82)
360 goto bad_area;
361 }
362 }
363 if (expand_stack(vma, address))
364 goto bad_area;
365 /*
366 * Ok, we have a good vm_area for this memory access, so
367 * we can handle it..
368 */
369good_area:
370 si_code = SEGV_ACCERR;
371
372 /* If we took a ITLB miss on a non-executable page, catch
373 * that here.
374 */
375 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
376 BUG_ON(address != regs->tpc);
377 BUG_ON(regs->tstate & TSTATE_PRIV);
378 goto bad_area;
379 }
380
381 if (fault_code & FAULT_CODE_WRITE) {
382 if (!(vma->vm_flags & VM_WRITE))
383 goto bad_area;
384
385 /* Spitfire has an icache which does not snoop
386 * processor stores. Later processors do...
387 */
388 if (tlb_type == spitfire &&
389 (vma->vm_flags & VM_EXEC) != 0 &&
390 vma->vm_file != NULL)
391 set_thread_fault_code(fault_code |
392 FAULT_CODE_BLKCOMMIT);
393 } else {
394 /* Allow reads even for write-only mappings */
395 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
396 goto bad_area;
397 }
398
399 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
400 case VM_FAULT_MINOR:
401 current->min_flt++;
402 break;
403 case VM_FAULT_MAJOR:
404 current->maj_flt++;
405 break;
406 case VM_FAULT_SIGBUS:
407 goto do_sigbus;
408 case VM_FAULT_OOM:
409 goto out_of_memory;
410 default:
411 BUG();
412 }
413
414 up_read(&mm->mmap_sem);
David S. Miller7a1ac522006-03-16 02:02:32 -0800415
416 mm_rss = get_mm_rss(mm);
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800417#ifdef CONFIG_HUGETLB_PAGE
418 mm_rss -= (mm->context.huge_pte_count * (HPAGE_SIZE / PAGE_SIZE));
419#endif
David S. Miller7bebd832006-03-27 01:07:55 -0800420 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800421 mm->context.tsb_block[MM_TSB_BASE].tsb_rss_limit))
422 tsb_grow(mm, MM_TSB_BASE, mm_rss);
423#ifdef CONFIG_HUGETLB_PAGE
424 mm_rss = mm->context.huge_pte_count;
David S. Miller7bebd832006-03-27 01:07:55 -0800425 if (unlikely(mm_rss >
David S. Millerdcc1e8d2006-03-22 00:49:59 -0800426 mm->context.tsb_block[MM_TSB_HUGE].tsb_rss_limit))
427 tsb_grow(mm, MM_TSB_HUGE, mm_rss);
428#endif
David S. Millerefdc1e22005-09-28 21:06:47 -0700429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /*
432 * Something tried to access memory that isn't in our memory map..
433 * Fix it, but check if it's kernel or user first..
434 */
435bad_area:
436 insn = get_fault_insn(regs, insn);
437 up_read(&mm->mmap_sem);
438
439handle_kernel_fault:
440 do_kernel_fault(regs, si_code, fault_code, insn, address);
David S. Millerefdc1e22005-09-28 21:06:47 -0700441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443/*
444 * We ran out of memory, or some other thing happened to us that made
445 * us unable to handle the page fault gracefully.
446 */
447out_of_memory:
448 insn = get_fault_insn(regs, insn);
449 up_read(&mm->mmap_sem);
450 printk("VM: killing process %s\n", current->comm);
451 if (!(regs->tstate & TSTATE_PRIV))
452 do_exit(SIGKILL);
453 goto handle_kernel_fault;
454
455intr_or_no_mm:
456 insn = get_fault_insn(regs, 0);
457 goto handle_kernel_fault;
458
459do_sigbus:
460 insn = get_fault_insn(regs, insn);
461 up_read(&mm->mmap_sem);
462
463 /*
464 * Send a sigbus, regardless of whether we were in kernel
465 * or user mode.
466 */
467 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
468
469 /* Kernel mode? Handle exceptions or die */
470 if (regs->tstate & TSTATE_PRIV)
471 goto handle_kernel_fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}