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>
14 #include <asm/pgtable.h>
15 #include <asm/processor.h>
16 #include <asm/system.h>
17 #include <asm/uaccess.h>
18 #include <asm/kdebug.h>
20 extern void die (char *, struct pt_regs *, long);
23 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
25 /* Hook to register for page fault notifications */
26 int register_page_fault_notifier(struct notifier_block *nb)
28 return atomic_notifier_chain_register(¬ify_page_fault_chain, nb);
31 int unregister_page_fault_notifier(struct notifier_block *nb)
33 return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb);
36 static inline int notify_page_fault(enum die_val val, const char *str,
37 struct pt_regs *regs, long err, int trap, int sig)
39 struct die_args args = {
46 return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args);
49 static inline int notify_page_fault(enum die_val val, const char *str,
50 struct pt_regs *regs, long err, int trap, int sig)
57 * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
58 * (inside region 5, on ia64) and that page is present.
61 mapped_kernel_page_is_present (unsigned long address)
68 pgd = pgd_offset_k(address);
69 if (pgd_none(*pgd) || pgd_bad(*pgd))
72 pud = pud_offset(pgd, address);
73 if (pud_none(*pud) || pud_bad(*pud))
76 pmd = pmd_offset(pud, address);
77 if (pmd_none(*pmd) || pmd_bad(*pmd))
80 ptep = pte_offset_kernel(pmd, address);
85 return pte_present(pte);
89 ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
91 int signal = SIGSEGV, code = SEGV_MAPERR;
92 struct vm_area_struct *vma, *prev_vma;
93 struct mm_struct *mm = current->mm;
97 /* mmap_sem is performance critical.... */
98 prefetchw(&mm->mmap_sem);
101 * If we're in an interrupt or have no user context, we must not take the fault..
103 if (in_atomic() || !mm)
106 #ifdef CONFIG_VIRTUAL_MEM_MAP
108 * If fault is in region 5 and we are in the kernel, we may already
109 * have the mmap_sem (pfn_valid macro is called during mmap). There
110 * is no vma for region 5 addr's anyway, so skip getting the semaphore
111 * and go directly to the exception handling code.
114 if ((REGION_NUMBER(address) == 5) && !user_mode(regs))
119 * This is to handle the kprobes on user space access instructions
121 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, code, TRAP_BRKPT,
122 SIGSEGV) == NOTIFY_STOP)
125 down_read(&mm->mmap_sem);
127 vma = find_vma_prev(mm, address, &prev_vma);
131 /* find_vma_prev() returns vma such that address < vma->vm_end or NULL */
132 if (address < vma->vm_start)
133 goto check_expansion;
138 /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
140 # define VM_READ_BIT 0
141 # define VM_WRITE_BIT 1
142 # define VM_EXEC_BIT 2
144 # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
145 || (1 << VM_EXEC_BIT) != VM_EXEC)
146 # error File is out of sync with <linux/mm.h>. Please update.
149 mask = ( (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
150 | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT)
151 | (((isr >> IA64_ISR_R_BIT) & 1UL) << VM_READ_BIT));
153 if ((vma->vm_flags & mask) != mask)
158 * If for any reason at all we couldn't handle the fault, make
159 * sure we exit gracefully rather than endlessly redo the
162 switch (handle_mm_fault(mm, vma, address, (mask & VM_WRITE) != 0)) {
169 case VM_FAULT_SIGBUS:
171 * We ran out of memory, or some other thing happened
172 * to us that made us unable to handle the page fault
182 up_read(&mm->mmap_sem);
186 if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
187 if (!(vma->vm_flags & VM_GROWSDOWN))
189 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
190 || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
192 if (expand_stack(vma, address))
196 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
197 || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
200 * Since the register backing store is accessed sequentially,
201 * we disallow growing it by more than a page at a time.
203 if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
205 if (expand_upwards(vma, address))
211 up_read(&mm->mmap_sem);
212 #ifdef CONFIG_VIRTUAL_MEM_MAP
215 if ((isr & IA64_ISR_SP)
216 || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
219 * This fault was due to a speculative load or lfetch.fault, set the "ed"
220 * bit in the psr to ensure forward progress. (Target register will get a
221 * NaT for ld.s, lfetch will be canceled.)
223 ia64_psr(regs)->ed = 1;
226 if (user_mode(regs)) {
227 si.si_signo = signal;
230 si.si_addr = (void __user *) address;
232 si.si_flags = __ISR_VALID;
233 force_sig_info(signal, &si, current);
238 if ((isr & IA64_ISR_SP)
239 || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
242 * This fault was due to a speculative load or lfetch.fault, set the "ed"
243 * bit in the psr to ensure forward progress. (Target register will get a
244 * NaT for ld.s, lfetch will be canceled.)
246 ia64_psr(regs)->ed = 1;
251 * Since we have no vma's for region 5, we might get here even if the address is
252 * valid, due to the VHPT walker inserting a non present translation that becomes
253 * stale. If that happens, the non present fault handler already purged the stale
254 * translation, which fixed the problem. So, we check to see if the translation is
255 * valid, and return if it is.
257 if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
260 if (ia64_done_with_exception(regs))
264 * Oops. The kernel tried to access some bad page. We'll have to terminate things
265 * with extreme prejudice.
269 if (address < PAGE_SIZE)
270 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
272 printk(KERN_ALERT "Unable to handle kernel paging request at "
273 "virtual address %016lx\n", address);
274 die("Oops", regs, isr);
280 up_read(&mm->mmap_sem);
281 if (current->pid == 1) {
283 down_read(&mm->mmap_sem);
286 printk(KERN_CRIT "VM: killing process %s\n", current->comm);