2 * MMU fault handling support.
4 * Copyright (C) 1998-2002 Hewlett-Packard Co
5 * David Mosberger-Tang <davidm@hpl.hp.com>
7 #include <linux/sched.h>
8 #include <linux/kernel.h>
10 #include <linux/smp_lock.h>
11 #include <linux/interrupt.h>
12 #include <linux/kprobes.h>
13 #include <linux/vs_memory.h>
15 #include <asm/pgtable.h>
16 #include <asm/processor.h>
17 #include <asm/system.h>
18 #include <asm/uaccess.h>
19 #include <asm/kdebug.h>
21 extern void die (char *, struct pt_regs *, long);
24 * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
25 * (inside region 5, on ia64) and that page is present.
28 mapped_kernel_page_is_present (unsigned long address)
35 pgd = pgd_offset_k(address);
36 if (pgd_none(*pgd) || pgd_bad(*pgd))
39 pud = pud_offset(pgd, address);
40 if (pud_none(*pud) || pud_bad(*pud))
43 pmd = pmd_offset(pud, address);
44 if (pmd_none(*pmd) || pmd_bad(*pmd))
47 ptep = pte_offset_kernel(pmd, address);
52 return pte_present(pte);
56 ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
58 int signal = SIGSEGV, code = SEGV_MAPERR;
59 struct vm_area_struct *vma, *prev_vma;
60 struct mm_struct *mm = current->mm;
65 * If we're in an interrupt or have no user context, we must not take the fault..
67 if (in_atomic() || !mm)
70 #ifdef CONFIG_VIRTUAL_MEM_MAP
72 * If fault is in region 5 and we are in the kernel, we may already
73 * have the mmap_sem (pfn_valid macro is called during mmap). There
74 * is no vma for region 5 addr's anyway, so skip getting the semaphore
75 * and go directly to the exception handling code.
78 if ((REGION_NUMBER(address) == 5) && !user_mode(regs))
83 * This is to handle the kprobes on user space access instructions
85 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, code, TRAP_BRKPT,
86 SIGSEGV) == NOTIFY_STOP)
89 down_read(&mm->mmap_sem);
91 vma = find_vma_prev(mm, address, &prev_vma);
95 /* find_vma_prev() returns vma such that address < vma->vm_end or NULL */
96 if (address < vma->vm_start)
102 /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
104 # define VM_READ_BIT 0
105 # define VM_WRITE_BIT 1
106 # define VM_EXEC_BIT 2
108 # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
109 || (1 << VM_EXEC_BIT) != VM_EXEC)
110 # error File is out of sync with <linux/mm.h>. Please update.
113 mask = ( (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
114 | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT)
115 | (((isr >> IA64_ISR_R_BIT) & 1UL) << VM_READ_BIT));
117 if ((vma->vm_flags & mask) != mask)
122 * If for any reason at all we couldn't handle the fault, make
123 * sure we exit gracefully rather than endlessly redo the
126 switch (handle_mm_fault(mm, vma, address, (mask & VM_WRITE) != 0)) {
133 case VM_FAULT_SIGBUS:
135 * We ran out of memory, or some other thing happened
136 * to us that made us unable to handle the page fault
146 up_read(&mm->mmap_sem);
150 if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
151 if (!(vma->vm_flags & VM_GROWSDOWN))
153 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
154 || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
156 if (expand_stack(vma, address))
160 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
161 || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
164 * Since the register backing store is accessed sequentially,
165 * we disallow growing it by more than a page at a time.
167 if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
169 if (expand_upwards(vma, address))
175 up_read(&mm->mmap_sem);
176 #ifdef CONFIG_VIRTUAL_MEM_MAP
179 if ((isr & IA64_ISR_SP)
180 || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
183 * This fault was due to a speculative load or lfetch.fault, set the "ed"
184 * bit in the psr to ensure forward progress. (Target register will get a
185 * NaT for ld.s, lfetch will be canceled.)
187 ia64_psr(regs)->ed = 1;
190 if (user_mode(regs)) {
191 si.si_signo = signal;
194 si.si_addr = (void __user *) address;
196 si.si_flags = __ISR_VALID;
197 force_sig_info(signal, &si, current);
202 if ((isr & IA64_ISR_SP)
203 || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
206 * This fault was due to a speculative load or lfetch.fault, set the "ed"
207 * bit in the psr to ensure forward progress. (Target register will get a
208 * NaT for ld.s, lfetch will be canceled.)
210 ia64_psr(regs)->ed = 1;
215 * Since we have no vma's for region 5, we might get here even if the address is
216 * valid, due to the VHPT walker inserting a non present translation that becomes
217 * stale. If that happens, the non present fault handler already purged the stale
218 * translation, which fixed the problem. So, we check to see if the translation is
219 * valid, and return if it is.
221 if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
224 if (ia64_done_with_exception(regs))
228 * Oops. The kernel tried to access some bad page. We'll have to terminate things
229 * with extreme prejudice.
233 if (address < PAGE_SIZE)
234 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
236 printk(KERN_ALERT "Unable to handle kernel paging request at "
237 "virtual address %016lx\n", address);
238 die("Oops", regs, isr);
244 up_read(&mm->mmap_sem);
245 if (current->pid == 1) {
247 down_read(&mm->mmap_sem);
250 printk(KERN_CRIT "VM: killing process %s\n", current->comm);