1 /* $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.
4 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
5 * Copyright (C) 1997, 1999 Jakub Jelinek (jj@ultra.linux.cz)
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>
17 #include <linux/module.h>
18 #include <linux/smp_lock.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kprobes.h>
24 #include <asm/pgtable.h>
25 #include <asm/openprom.h>
26 #include <asm/oplib.h>
27 #include <asm/uaccess.h>
30 #include <asm/sections.h>
31 #include <asm/kdebug.h>
34 * To debug kernel to catch accesses to certain virtual/physical addresses.
35 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
36 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
37 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
38 * watched. This is only useful on a single cpu machine for now. After the watchpoint
39 * is detected, the process causing it will be killed, thus preventing an infinite loop.
41 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
43 unsigned long lsubits;
45 __asm__ __volatile__("ldxa [%%g0] %1, %0"
47 : "i" (ASI_LSU_CONTROL));
48 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
49 LSU_CONTROL_PR | LSU_CONTROL_VR |
50 LSU_CONTROL_PW | LSU_CONTROL_VW);
52 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
55 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
58 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
60 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
62 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
63 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
66 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
70 static void __kprobes unhandled_fault(unsigned long address,
71 struct task_struct *tsk,
74 if ((unsigned long) address < PAGE_SIZE) {
75 printk(KERN_ALERT "Unable to handle kernel NULL "
76 "pointer dereference\n");
78 printk(KERN_ALERT "Unable to handle kernel paging request "
79 "at virtual address %016lx\n", (unsigned long)address);
81 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
83 CTX_HWBITS(tsk->mm->context) :
84 CTX_HWBITS(tsk->active_mm->context)));
85 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
86 (tsk->mm ? (unsigned long) tsk->mm->pgd :
87 (unsigned long) tsk->active_mm->pgd));
88 if (notify_die(DIE_GPF, "general protection fault", regs,
89 0, 0, SIGSEGV) == NOTIFY_STOP)
91 die_if_kernel("Oops", regs);
94 static void bad_kernel_pc(struct pt_regs *regs)
98 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
100 __asm__("mov %%sp, %0" : "=r" (ksp));
101 show_stack(current, ksp);
102 unhandled_fault(regs->tpc, current, regs);
106 * We now make sure that mmap_sem is held in all paths that call
107 * this. Additionally, to prevent kswapd from ripping ptes from
108 * under us, raise interrupts around the time that we look at the
109 * pte, kswapd will have to wait to get his smp ipi response from
110 * us. vmtruncate likewise. This saves us having to get pte lock.
112 static unsigned int get_user_insn(unsigned long tpc)
114 pgd_t *pgdp = pgd_offset(current->mm, tpc);
120 unsigned long pstate;
124 pudp = pud_offset(pgdp, tpc);
127 pmdp = pmd_offset(pudp, tpc);
131 /* This disables preemption for us as well. */
132 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
133 __asm__ __volatile__("wrpr %0, %1, %%pstate"
134 : : "r" (pstate), "i" (PSTATE_IE));
135 ptep = pte_offset_map(pmdp, tpc);
137 if (!pte_present(pte))
140 pa = (pte_val(pte) & _PAGE_PADDR);
141 pa += (tpc & ~PAGE_MASK);
143 /* Use phys bypass so we don't pollute dtlb/dcache. */
144 __asm__ __volatile__("lduwa [%1] %2, %0"
146 : "r" (pa), "i" (ASI_PHYS_USE_EC));
150 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
155 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
157 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
158 unsigned int insn, int fault_code)
165 if (fault_code & FAULT_CODE_ITLB)
166 info.si_addr = (void __user *) regs->tpc;
168 info.si_addr = (void __user *)
169 compute_effective_address(regs, insn, 0);
171 force_sig_info(sig, &info, current);
174 extern int handle_ldf_stq(u32, struct pt_regs *);
175 extern int handle_ld_nf(u32, struct pt_regs *);
177 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
180 if (!regs->tpc || (regs->tpc & 0x3))
182 if (regs->tstate & TSTATE_PRIV) {
183 insn = *(unsigned int *) regs->tpc;
185 insn = get_user_insn(regs->tpc);
191 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
192 unsigned int insn, unsigned long address)
194 unsigned char asi = ASI_P;
196 if ((!insn) && (regs->tstate & TSTATE_PRIV))
199 /* If user insn could be read (thus insn is zero), that
200 * is fine. We will just gun down the process with a signal
204 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
205 (insn & 0xc0800000) == 0xc0800000) {
207 asi = (regs->tstate >> 24);
210 if ((asi & 0xf2) == 0x82) {
211 if (insn & 0x1000000) {
212 handle_ldf_stq(insn, regs);
214 /* This was a non-faulting load. Just clear the
215 * destination register(s) and continue with the next
218 handle_ld_nf(insn, regs);
224 /* Is this in ex_table? */
225 if (regs->tstate & TSTATE_PRIV) {
226 const struct exception_table_entry *entry;
228 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
230 asi = (regs->tstate >> 24);
235 /* Look in asi.h: All _S asis have LS bit set */
237 (entry = search_exception_tables(regs->tpc))) {
238 regs->tpc = entry->fixup;
239 regs->tnpc = regs->tpc + 4;
243 /* The si_code was set to make clear whether
244 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
246 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
251 unhandled_fault (address, current, regs);
254 asmlinkage void __kprobes do_sparc64_fault(struct pt_regs *regs)
256 struct mm_struct *mm = current->mm;
257 struct vm_area_struct *vma;
258 unsigned int insn = 0;
259 int si_code, fault_code;
260 unsigned long address;
262 fault_code = get_thread_fault_code();
264 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
265 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
268 si_code = SEGV_MAPERR;
269 address = current_thread_info()->fault_address;
271 if ((fault_code & FAULT_CODE_ITLB) &&
272 (fault_code & FAULT_CODE_DTLB))
275 if (regs->tstate & TSTATE_PRIV) {
276 unsigned long tpc = regs->tpc;
278 /* Sanity check the PC. */
279 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
280 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
281 /* Valid, no problems... */
289 * If we're in an interrupt or have no user
290 * context, we must not take the fault..
292 if (in_atomic() || !mm)
295 if (test_thread_flag(TIF_32BIT)) {
296 if (!(regs->tstate & TSTATE_PRIV))
297 regs->tpc &= 0xffffffff;
298 address &= 0xffffffff;
301 if (!down_read_trylock(&mm->mmap_sem)) {
302 if ((regs->tstate & TSTATE_PRIV) &&
303 !search_exception_tables(regs->tpc)) {
304 insn = get_fault_insn(regs, insn);
305 goto handle_kernel_fault;
307 down_read(&mm->mmap_sem);
310 vma = find_vma(mm, address);
314 /* Pure DTLB misses do not tell us whether the fault causing
315 * load/store/atomic was a write or not, it only says that there
316 * was no match. So in such a case we (carefully) read the
317 * instruction to try and figure this out. It's an optimization
318 * so it's ok if we can't do this.
320 * Special hack, window spill/fill knows the exact fault type.
323 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
324 (vma->vm_flags & VM_WRITE) != 0) {
325 insn = get_fault_insn(regs, 0);
328 if ((insn & 0xc0200000) == 0xc0200000 &&
329 (insn & 0x1780000) != 0x1680000) {
330 /* Don't bother updating thread struct value,
331 * because update_mmu_cache only cares which tlb
332 * the access came from.
334 fault_code |= FAULT_CODE_WRITE;
339 if (vma->vm_start <= address)
341 if (!(vma->vm_flags & VM_GROWSDOWN))
343 if (!(fault_code & FAULT_CODE_WRITE)) {
344 /* Non-faulting loads shouldn't expand stack. */
345 insn = get_fault_insn(regs, insn);
346 if ((insn & 0xc0800000) == 0xc0800000) {
350 asi = (regs->tstate >> 24);
353 if ((asi & 0xf2) == 0x82)
357 if (expand_stack(vma, address))
360 * Ok, we have a good vm_area for this memory access, so
364 si_code = SEGV_ACCERR;
366 /* If we took a ITLB miss on a non-executable page, catch
369 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
370 BUG_ON(address != regs->tpc);
371 BUG_ON(regs->tstate & TSTATE_PRIV);
375 if (fault_code & FAULT_CODE_WRITE) {
376 if (!(vma->vm_flags & VM_WRITE))
379 /* Spitfire has an icache which does not snoop
380 * processor stores. Later processors do...
382 if (tlb_type == spitfire &&
383 (vma->vm_flags & VM_EXEC) != 0 &&
384 vma->vm_file != NULL)
385 set_thread_fault_code(fault_code |
386 FAULT_CODE_BLKCOMMIT);
388 /* Allow reads even for write-only mappings */
389 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
393 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
400 case VM_FAULT_SIGBUS:
408 up_read(&mm->mmap_sem);
412 * Something tried to access memory that isn't in our memory map..
413 * Fix it, but check if it's kernel or user first..
416 insn = get_fault_insn(regs, insn);
417 up_read(&mm->mmap_sem);
420 do_kernel_fault(regs, si_code, fault_code, insn, address);
424 * We ran out of memory, or some other thing happened to us that made
425 * us unable to handle the page fault gracefully.
428 insn = get_fault_insn(regs, insn);
429 up_read(&mm->mmap_sem);
430 printk("VM: killing process %s\n", current->comm);
431 if (!(regs->tstate & TSTATE_PRIV))
433 goto handle_kernel_fault;
436 insn = get_fault_insn(regs, 0);
437 goto handle_kernel_fault;
440 insn = get_fault_insn(regs, insn);
441 up_read(&mm->mmap_sem);
444 * Send a sigbus, regardless of whether we were in kernel
447 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
449 /* Kernel mode? Handle exceptions or die */
450 if (regs->tstate & TSTATE_PRIV)
451 goto handle_kernel_fault;