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 ATOMIC_NOTIFIER_HEAD(notify_page_fault_chain);
26 /* Hook to register for page fault notifications */
27 int register_page_fault_notifier(struct notifier_block *nb)
29 return atomic_notifier_chain_register(¬ify_page_fault_chain, nb);
32 int unregister_page_fault_notifier(struct notifier_block *nb)
34 return atomic_notifier_chain_unregister(¬ify_page_fault_chain, nb);
37 static inline int notify_page_fault(enum die_val val, const char *str,
38 struct pt_regs *regs, long err, int trap, int sig)
40 struct die_args args = {
47 return atomic_notifier_call_chain(¬ify_page_fault_chain, val, &args);
50 static inline int notify_page_fault(enum die_val val, const char *str,
51 struct pt_regs *regs, long err, int trap, int sig)
58 * Return TRUE if ADDRESS points at a page in the kernel's mapped segment
59 * (inside region 5, on ia64) and that page is present.
62 mapped_kernel_page_is_present (unsigned long address)
69 pgd = pgd_offset_k(address);
70 if (pgd_none(*pgd) || pgd_bad(*pgd))
73 pud = pud_offset(pgd, address);
74 if (pud_none(*pud) || pud_bad(*pud))
77 pmd = pmd_offset(pud, address);
78 if (pmd_none(*pmd) || pmd_bad(*pmd))
81 ptep = pte_offset_kernel(pmd, address);
86 return pte_present(pte);
90 ia64_do_page_fault (unsigned long address, unsigned long isr, struct pt_regs *regs)
92 int signal = SIGSEGV, code = SEGV_MAPERR;
93 struct vm_area_struct *vma, *prev_vma;
94 struct mm_struct *mm = current->mm;
98 /* mmap_sem is performance critical.... */
99 prefetchw(&mm->mmap_sem);
102 * If we're in an interrupt or have no user context, we must not take the fault..
104 if (in_atomic() || !mm)
107 #ifdef CONFIG_VIRTUAL_MEM_MAP
109 * If fault is in region 5 and we are in the kernel, we may already
110 * have the mmap_sem (pfn_valid macro is called during mmap). There
111 * is no vma for region 5 addr's anyway, so skip getting the semaphore
112 * and go directly to the exception handling code.
115 if ((REGION_NUMBER(address) == 5) && !user_mode(regs))
120 * This is to handle the kprobes on user space access instructions
122 if (notify_page_fault(DIE_PAGE_FAULT, "page fault", regs, code, TRAP_BRKPT,
123 SIGSEGV) == NOTIFY_STOP)
126 down_read(&mm->mmap_sem);
128 vma = find_vma_prev(mm, address, &prev_vma);
132 /* find_vma_prev() returns vma such that address < vma->vm_end or NULL */
133 if (address < vma->vm_start)
134 goto check_expansion;
139 /* OK, we've got a good vm_area for this memory area. Check the access permissions: */
141 # define VM_READ_BIT 0
142 # define VM_WRITE_BIT 1
143 # define VM_EXEC_BIT 2
145 # if (((1 << VM_READ_BIT) != VM_READ || (1 << VM_WRITE_BIT) != VM_WRITE) \
146 || (1 << VM_EXEC_BIT) != VM_EXEC)
147 # error File is out of sync with <linux/mm.h>. Please update.
150 if (((isr >> IA64_ISR_R_BIT) & 1UL) && (!(vma->vm_flags & (VM_READ | VM_WRITE))))
153 mask = ( (((isr >> IA64_ISR_X_BIT) & 1UL) << VM_EXEC_BIT)
154 | (((isr >> IA64_ISR_W_BIT) & 1UL) << VM_WRITE_BIT));
156 if ((vma->vm_flags & mask) != mask)
161 * If for any reason at all we couldn't handle the fault, make
162 * sure we exit gracefully rather than endlessly redo the
165 switch (handle_mm_fault(mm, vma, address, (mask & VM_WRITE) != 0)) {
172 case VM_FAULT_SIGBUS:
174 * We ran out of memory, or some other thing happened
175 * to us that made us unable to handle the page fault
185 up_read(&mm->mmap_sem);
189 if (!(prev_vma && (prev_vma->vm_flags & VM_GROWSUP) && (address == prev_vma->vm_end))) {
190 if (!(vma->vm_flags & VM_GROWSDOWN))
192 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
193 || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
195 if (expand_stack(vma, address))
199 if (REGION_NUMBER(address) != REGION_NUMBER(vma->vm_start)
200 || REGION_OFFSET(address) >= RGN_MAP_LIMIT)
203 * Since the register backing store is accessed sequentially,
204 * we disallow growing it by more than a page at a time.
206 if (address > vma->vm_end + PAGE_SIZE - sizeof(long))
208 if (expand_upwards(vma, address))
214 up_read(&mm->mmap_sem);
215 #ifdef CONFIG_VIRTUAL_MEM_MAP
218 if ((isr & IA64_ISR_SP)
219 || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
222 * This fault was due to a speculative load or lfetch.fault, set the "ed"
223 * bit in the psr to ensure forward progress. (Target register will get a
224 * NaT for ld.s, lfetch will be canceled.)
226 ia64_psr(regs)->ed = 1;
229 if (user_mode(regs)) {
230 si.si_signo = signal;
233 si.si_addr = (void __user *) address;
235 si.si_flags = __ISR_VALID;
236 force_sig_info(signal, &si, current);
241 if ((isr & IA64_ISR_SP)
242 || ((isr & IA64_ISR_NA) && (isr & IA64_ISR_CODE_MASK) == IA64_ISR_CODE_LFETCH))
245 * This fault was due to a speculative load or lfetch.fault, set the "ed"
246 * bit in the psr to ensure forward progress. (Target register will get a
247 * NaT for ld.s, lfetch will be canceled.)
249 ia64_psr(regs)->ed = 1;
254 * Since we have no vma's for region 5, we might get here even if the address is
255 * valid, due to the VHPT walker inserting a non present translation that becomes
256 * stale. If that happens, the non present fault handler already purged the stale
257 * translation, which fixed the problem. So, we check to see if the translation is
258 * valid, and return if it is.
260 if (REGION_NUMBER(address) == 5 && mapped_kernel_page_is_present(address))
263 if (ia64_done_with_exception(regs))
267 * Oops. The kernel tried to access some bad page. We'll have to terminate things
268 * with extreme prejudice.
272 if (address < PAGE_SIZE)
273 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference (address %016lx)\n", address);
275 printk(KERN_ALERT "Unable to handle kernel paging request at "
276 "virtual address %016lx\n", address);
277 die("Oops", regs, isr);
283 up_read(&mm->mmap_sem);
284 if (is_init(current)) {
286 down_read(&mm->mmap_sem);
289 printk(KERN_CRIT "VM: killing process %s\n", current->comm);