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>
23 #include <asm/pgtable.h>
24 #include <asm/openprom.h>
25 #include <asm/oplib.h>
26 #include <asm/uaccess.h>
29 #include <asm/sections.h>
30 #include <asm/kdebug.h>
32 #define ELEMENTS(arr) (sizeof (arr)/sizeof (arr[0]))
34 extern struct sparc_phys_banks sp_banks[SPARC_PHYS_BANKS];
37 * To debug kernel during syscall entry.
39 void syscall_trace_entry(struct pt_regs *regs)
41 printk("scall entry: %s[%d]/cpu%d: %d\n", current->comm, current->pid, smp_processor_id(), (int) regs->u_regs[UREG_G1]);
45 * To debug kernel during syscall exit.
47 void syscall_trace_exit(struct pt_regs *regs)
49 printk("scall exit: %s[%d]/cpu%d: %d\n", current->comm, current->pid, smp_processor_id(), (int) regs->u_regs[UREG_G1]);
53 * To debug kernel to catch accesses to certain virtual/physical addresses.
54 * Mode = 0 selects physical watchpoints, mode = 1 selects virtual watchpoints.
55 * flags = VM_READ watches memread accesses, flags = VM_WRITE watches memwrite accesses.
56 * Caller passes in a 64bit aligned addr, with mask set to the bytes that need to be
57 * watched. This is only useful on a single cpu machine for now. After the watchpoint
58 * is detected, the process causing it will be killed, thus preventing an infinite loop.
60 void set_brkpt(unsigned long addr, unsigned char mask, int flags, int mode)
62 unsigned long lsubits;
64 __asm__ __volatile__("ldxa [%%g0] %1, %0"
66 : "i" (ASI_LSU_CONTROL));
67 lsubits &= ~(LSU_CONTROL_PM | LSU_CONTROL_VM |
68 LSU_CONTROL_PR | LSU_CONTROL_VR |
69 LSU_CONTROL_PW | LSU_CONTROL_VW);
71 __asm__ __volatile__("stxa %0, [%1] %2\n\t"
74 : "r" (addr), "r" (mode ? VIRT_WATCHPOINT : PHYS_WATCHPOINT),
77 lsubits |= ((unsigned long)mask << (mode ? 25 : 33));
79 lsubits |= (mode ? LSU_CONTROL_VR : LSU_CONTROL_PR);
81 lsubits |= (mode ? LSU_CONTROL_VW : LSU_CONTROL_PW);
82 __asm__ __volatile__("stxa %0, [%%g0] %1\n\t"
85 : "r" (lsubits), "i" (ASI_LSU_CONTROL)
89 /* Nice, simple, prom library does all the sweating for us. ;) */
90 unsigned long __init prom_probe_memory (void)
92 register struct linux_mlist_p1275 *mlist;
93 register unsigned long bytes, base_paddr, tally;
97 mlist = *prom_meminfo()->p1275_available;
98 bytes = tally = mlist->num_bytes;
99 base_paddr = mlist->start_adr;
101 sp_banks[0].base_addr = base_paddr;
102 sp_banks[0].num_bytes = bytes;
104 while (mlist->theres_more != (void *) 0) {
106 mlist = mlist->theres_more;
107 bytes = mlist->num_bytes;
109 if (i >= SPARC_PHYS_BANKS-1) {
110 printk ("The machine has more banks than "
111 "this kernel can support\n"
112 "Increase the SPARC_PHYS_BANKS "
113 "setting (currently %d)\n",
115 i = SPARC_PHYS_BANKS-1;
119 sp_banks[i].base_addr = mlist->start_adr;
120 sp_banks[i].num_bytes = mlist->num_bytes;
124 sp_banks[i].base_addr = 0xdeadbeefbeefdeadUL;
125 sp_banks[i].num_bytes = 0;
127 /* Now mask all bank sizes on a page boundary, it is all we can
130 for (i = 0; sp_banks[i].num_bytes != 0; i++)
131 sp_banks[i].num_bytes &= PAGE_MASK;
136 static void unhandled_fault(unsigned long address, struct task_struct *tsk,
137 struct pt_regs *regs)
139 if ((unsigned long) address < PAGE_SIZE) {
140 printk(KERN_ALERT "Unable to handle kernel NULL "
141 "pointer dereference\n");
143 printk(KERN_ALERT "Unable to handle kernel paging request "
144 "at virtual address %016lx\n", (unsigned long)address);
146 printk(KERN_ALERT "tsk->{mm,active_mm}->context = %016lx\n",
147 (tsk->mm ? tsk->mm->context : tsk->active_mm->context));
148 printk(KERN_ALERT "tsk->{mm,active_mm}->pgd = %016lx\n",
149 (tsk->mm ? (unsigned long) tsk->mm->pgd :
150 (unsigned long) tsk->active_mm->pgd));
151 if (notify_die(DIE_GPF, "general protection fault", regs,
152 0, 0, SIGSEGV) == NOTIFY_STOP)
154 die_if_kernel("Oops", regs);
157 static void bad_kernel_pc(struct pt_regs *regs)
161 printk(KERN_CRIT "OOPS: Bogus kernel PC [%016lx] in fault handler\n",
163 __asm__("mov %%sp, %0" : "=r" (ksp));
164 show_stack(current, ksp);
165 unhandled_fault(regs->tpc, current, regs);
169 * We now make sure that mmap_sem is held in all paths that call
170 * this. Additionally, to prevent kswapd from ripping ptes from
171 * under us, raise interrupts around the time that we look at the
172 * pte, kswapd will have to wait to get his smp ipi response from
173 * us. This saves us having to get page_table_lock.
175 static unsigned int get_user_insn(unsigned long tpc)
177 pgd_t *pgdp = pgd_offset(current->mm, tpc);
182 unsigned long pstate;
186 pmdp = pmd_offset(pgdp, tpc);
190 /* This disables preemption for us as well. */
191 __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
192 __asm__ __volatile__("wrpr %0, %1, %%pstate"
193 : : "r" (pstate), "i" (PSTATE_IE));
194 ptep = pte_offset_map(pmdp, tpc);
196 if (!pte_present(pte))
199 pa = (pte_val(pte) & _PAGE_PADDR);
200 pa += (tpc & ~PAGE_MASK);
202 /* Use phys bypass so we don't pollute dtlb/dcache. */
203 __asm__ __volatile__("lduwa [%1] %2, %0"
205 : "r" (pa), "i" (ASI_PHYS_USE_EC));
209 __asm__ __volatile__("wrpr %0, 0x0, %%pstate" : : "r" (pstate));
214 extern unsigned long compute_effective_address(struct pt_regs *, unsigned int, unsigned int);
216 static void do_fault_siginfo(int code, int sig, struct pt_regs *regs,
217 unsigned int insn, int fault_code)
224 if (fault_code & FAULT_CODE_ITLB)
225 info.si_addr = (void __user *) regs->tpc;
227 info.si_addr = (void __user *)
228 compute_effective_address(regs, insn, 0);
230 force_sig_info(sig, &info, current);
233 extern int handle_ldf_stq(u32, struct pt_regs *);
234 extern int handle_ld_nf(u32, struct pt_regs *);
236 static unsigned int get_fault_insn(struct pt_regs *regs, unsigned int insn)
239 if (!regs->tpc || (regs->tpc & 0x3))
241 if (regs->tstate & TSTATE_PRIV) {
242 insn = *(unsigned int *) regs->tpc;
244 insn = get_user_insn(regs->tpc);
250 static void do_kernel_fault(struct pt_regs *regs, int si_code, int fault_code,
251 unsigned int insn, unsigned long address)
254 unsigned char asi = ASI_P;
256 if ((!insn) && (regs->tstate & TSTATE_PRIV))
259 /* If user insn could be read (thus insn is zero), that
260 * is fine. We will just gun down the process with a signal
264 if (!(fault_code & (FAULT_CODE_WRITE|FAULT_CODE_ITLB)) &&
265 (insn & 0xc0800000) == 0xc0800000) {
267 asi = (regs->tstate >> 24);
270 if ((asi & 0xf2) == 0x82) {
271 if (insn & 0x1000000) {
272 handle_ldf_stq(insn, regs);
274 /* This was a non-faulting load. Just clear the
275 * destination register(s) and continue with the next
278 handle_ld_nf(insn, regs);
284 g2 = regs->u_regs[UREG_G2];
286 /* Is this in ex_table? */
287 if (regs->tstate & TSTATE_PRIV) {
290 if (asi == ASI_P && (insn & 0xc0800000) == 0xc0800000) {
292 asi = (regs->tstate >> 24);
297 /* Look in asi.h: All _S asis have LS bit set */
299 (fixup = search_extables_range(regs->tpc, &g2))) {
301 regs->tnpc = regs->tpc + 4;
302 regs->u_regs[UREG_G2] = g2;
306 /* The si_code was set to make clear whether
307 * this was a SEGV_MAPERR or SEGV_ACCERR fault.
309 do_fault_siginfo(si_code, SIGSEGV, regs, insn, fault_code);
314 unhandled_fault (address, current, regs);
317 asmlinkage void do_sparc64_fault(struct pt_regs *regs)
319 struct mm_struct *mm = current->mm;
320 struct vm_area_struct *vma;
321 unsigned int insn = 0;
322 int si_code, fault_code;
323 unsigned long address;
325 fault_code = get_thread_fault_code();
327 if (notify_die(DIE_PAGE_FAULT, "page_fault", regs,
328 fault_code, 0, SIGSEGV) == NOTIFY_STOP)
331 si_code = SEGV_MAPERR;
332 address = current_thread_info()->fault_address;
334 if ((fault_code & FAULT_CODE_ITLB) &&
335 (fault_code & FAULT_CODE_DTLB))
338 if (regs->tstate & TSTATE_PRIV) {
339 unsigned long tpc = regs->tpc;
341 /* Sanity check the PC. */
342 if ((tpc >= KERNBASE && tpc < (unsigned long) _etext) ||
343 (tpc >= MODULES_VADDR && tpc < MODULES_END)) {
344 /* Valid, no problems... */
352 * If we're in an interrupt or have no user
353 * context, we must not take the fault..
355 if (in_atomic() || !mm)
358 if (test_thread_flag(TIF_32BIT)) {
359 if (!(regs->tstate & TSTATE_PRIV))
360 regs->tpc &= 0xffffffff;
361 address &= 0xffffffff;
364 if (!down_read_trylock(&mm->mmap_sem)) {
365 if ((regs->tstate & TSTATE_PRIV) &&
366 !search_exception_tables(regs->tpc)) {
367 insn = get_fault_insn(regs, insn);
368 goto handle_kernel_fault;
370 down_read(&mm->mmap_sem);
373 vma = find_vma(mm, address);
377 /* Pure DTLB misses do not tell us whether the fault causing
378 * load/store/atomic was a write or not, it only says that there
379 * was no match. So in such a case we (carefully) read the
380 * instruction to try and figure this out. It's an optimization
381 * so it's ok if we can't do this.
383 * Special hack, window spill/fill knows the exact fault type.
386 (FAULT_CODE_DTLB | FAULT_CODE_WRITE | FAULT_CODE_WINFIXUP)) == FAULT_CODE_DTLB) &&
387 (vma->vm_flags & VM_WRITE) != 0) {
388 insn = get_fault_insn(regs, 0);
391 if ((insn & 0xc0200000) == 0xc0200000 &&
392 (insn & 0x1780000) != 0x1680000) {
393 /* Don't bother updating thread struct value,
394 * because update_mmu_cache only cares which tlb
395 * the access came from.
397 fault_code |= FAULT_CODE_WRITE;
402 if (vma->vm_start <= address)
404 if (!(vma->vm_flags & VM_GROWSDOWN))
406 if (!(fault_code & FAULT_CODE_WRITE)) {
407 /* Non-faulting loads shouldn't expand stack. */
408 insn = get_fault_insn(regs, insn);
409 if ((insn & 0xc0800000) == 0xc0800000) {
413 asi = (regs->tstate >> 24);
416 if ((asi & 0xf2) == 0x82)
420 if (expand_stack(vma, address))
423 * Ok, we have a good vm_area for this memory access, so
427 si_code = SEGV_ACCERR;
429 /* If we took a ITLB miss on a non-executable page, catch
432 if ((fault_code & FAULT_CODE_ITLB) && !(vma->vm_flags & VM_EXEC)) {
433 BUG_ON(address != regs->tpc);
434 BUG_ON(regs->tstate & TSTATE_PRIV);
438 if (fault_code & FAULT_CODE_WRITE) {
439 if (!(vma->vm_flags & VM_WRITE))
442 /* Spitfire has an icache which does not snoop
443 * processor stores. Later processors do...
445 if (tlb_type == spitfire &&
446 (vma->vm_flags & VM_EXEC) != 0 &&
447 vma->vm_file != NULL)
448 set_thread_fault_code(fault_code |
449 FAULT_CODE_BLKCOMMIT);
451 /* Allow reads even for write-only mappings */
452 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
456 switch (handle_mm_fault(mm, vma, address, (fault_code & FAULT_CODE_WRITE))) {
463 case VM_FAULT_SIGBUS:
471 up_read(&mm->mmap_sem);
475 * Something tried to access memory that isn't in our memory map..
476 * Fix it, but check if it's kernel or user first..
479 insn = get_fault_insn(regs, insn);
480 up_read(&mm->mmap_sem);
483 do_kernel_fault(regs, si_code, fault_code, insn, address);
488 * We ran out of memory, or some other thing happened to us that made
489 * us unable to handle the page fault gracefully.
492 insn = get_fault_insn(regs, insn);
493 up_read(&mm->mmap_sem);
494 printk("VM: killing process %s\n", current->comm);
495 if (!(regs->tstate & TSTATE_PRIV))
497 goto handle_kernel_fault;
500 insn = get_fault_insn(regs, 0);
501 goto handle_kernel_fault;
504 insn = get_fault_insn(regs, insn);
505 up_read(&mm->mmap_sem);
508 * Send a sigbus, regardless of whether we were in kernel
511 do_fault_siginfo(BUS_ADRERR, SIGBUS, regs, insn, fault_code);
513 /* Kernel mode? Handle exceptions or die */
514 if (regs->tstate & TSTATE_PRIV)
515 goto handle_kernel_fault;
518 /* These values are no longer needed, clear them. */
519 set_thread_fault_code(0);
520 current_thread_info()->fault_address = 0;