2 * Copyright (C) 1995 Linus Torvalds
3 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
4 * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
6 #include <linux/interrupt.h>
7 #include <linux/mmiotrace.h>
8 #include <linux/bootmem.h>
9 #include <linux/compiler.h>
10 #include <linux/highmem.h>
11 #include <linux/kprobes.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
14 #include <linux/vt_kern.h>
15 #include <linux/signal.h>
16 #include <linux/kernel.h>
17 #include <linux/ptrace.h>
18 #include <linux/string.h>
19 #include <linux/module.h>
20 #include <linux/kdebug.h>
21 #include <linux/errno.h>
22 #include <linux/magic.h>
23 #include <linux/sched.h>
24 #include <linux/types.h>
25 #include <linux/init.h>
26 #include <linux/mman.h>
27 #include <linux/tty.h>
28 #include <linux/smp.h>
31 #include <asm-generic/sections.h>
33 #include <asm/tlbflush.h>
34 #include <asm/pgalloc.h>
35 #include <asm/segment.h>
36 #include <asm/system.h>
37 #include <asm/proto.h>
38 #include <asm/traps.h>
42 * Page fault error code bits:
44 * bit 0 == 0: no page found 1: protection fault
45 * bit 1 == 0: read access 1: write access
46 * bit 2 == 0: kernel-mode access 1: user-mode access
47 * bit 3 == 1: use of reserved bit detected
48 * bit 4 == 1: fault was an instruction fetch
50 enum x86_pf_error_code {
60 * (returns 0 if mmiotrace is disabled)
62 static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
64 if (unlikely(is_kmmio_active()))
65 if (kmmio_handler(regs, addr) == 1)
70 static inline int notify_page_fault(struct pt_regs *regs)
74 /* kprobe_running() needs smp_processor_id() */
75 if (kprobes_built_in() && !user_mode_vm(regs)) {
77 if (kprobe_running() && kprobe_fault_handler(regs, 14))
90 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
91 * Check that here and ignore it.
95 * Sometimes the CPU reports invalid exceptions on prefetch.
96 * Check that here and ignore it.
98 * Opcode checker based on code by Richard Brunner.
101 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
102 unsigned char opcode, int *prefetch)
104 unsigned char instr_hi = opcode & 0xf0;
105 unsigned char instr_lo = opcode & 0x0f;
111 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
112 * In X86_64 long mode, the CPU will signal invalid
113 * opcode if some of these prefixes are present so
114 * X86_64 will never get here anyway
116 return ((instr_lo & 7) == 0x6);
120 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
121 * Need to figure out under what instruction mode the
122 * instruction was issued. Could check the LDT for lm,
123 * but for now it's good enough to assume that long
124 * mode only uses well known segments or kernel.
126 return (!user_mode(regs)) || (regs->cs == __USER_CS);
129 /* 0x64 thru 0x67 are valid prefixes in all modes. */
130 return (instr_lo & 0xC) == 0x4;
132 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
133 return !instr_lo || (instr_lo>>1) == 1;
135 /* Prefetch instruction is 0x0F0D or 0x0F18 */
136 if (probe_kernel_address(instr, opcode))
139 *prefetch = (instr_lo == 0xF) &&
140 (opcode == 0x0D || opcode == 0x18);
148 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
150 unsigned char *max_instr;
151 unsigned char *instr;
155 * If it was a exec (instruction fetch) fault on NX page, then
156 * do not ignore the fault:
158 if (error_code & PF_INSTR)
161 instr = (void *)convert_ip_to_linear(current, regs);
162 max_instr = instr + 15;
164 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
167 while (instr < max_instr) {
168 unsigned char opcode;
170 if (probe_kernel_address(instr, opcode))
175 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
182 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
183 struct task_struct *tsk)
187 info.si_signo = si_signo;
189 info.si_code = si_code;
190 info.si_addr = (void __user *)address;
192 force_sig_info(si_signo, &info, tsk);
195 DEFINE_SPINLOCK(pgd_lock);
199 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
201 unsigned index = pgd_index(address);
207 pgd_k = init_mm.pgd + index;
209 if (!pgd_present(*pgd_k))
213 * set_pgd(pgd, *pgd_k); here would be useless on PAE
214 * and redundant with the set_pmd() on non-PAE. As would
217 pud = pud_offset(pgd, address);
218 pud_k = pud_offset(pgd_k, address);
219 if (!pud_present(*pud_k))
222 pmd = pmd_offset(pud, address);
223 pmd_k = pmd_offset(pud_k, address);
224 if (!pmd_present(*pmd_k))
227 if (!pmd_present(*pmd)) {
228 set_pmd(pmd, *pmd_k);
229 arch_flush_lazy_mmu_mode();
231 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
237 void vmalloc_sync_all(void)
239 unsigned long address;
241 if (SHARED_KERNEL_PMD)
244 for (address = VMALLOC_START & PMD_MASK;
245 address >= TASK_SIZE && address < FIXADDR_TOP;
246 address += PMD_SIZE) {
251 spin_lock_irqsave(&pgd_lock, flags);
252 list_for_each_entry(page, &pgd_list, lru) {
253 if (!vmalloc_sync_one(page_address(page), address))
256 spin_unlock_irqrestore(&pgd_lock, flags);
263 * Handle a fault on the vmalloc or module mapping area
265 static noinline int vmalloc_fault(unsigned long address)
267 unsigned long pgd_paddr;
271 /* Make sure we are in vmalloc area: */
272 if (!(address >= VMALLOC_START && address < VMALLOC_END))
276 * Synchronize this task's top level page-table
277 * with the 'reference' page table.
279 * Do _not_ use "current" here. We might be inside
280 * an interrupt in the middle of a task switch..
282 pgd_paddr = read_cr3();
283 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
287 pte_k = pte_offset_kernel(pmd_k, address);
288 if (!pte_present(*pte_k))
295 * Did it hit the DOS screen memory VA from vm86 mode?
298 check_v8086_mode(struct pt_regs *regs, unsigned long address,
299 struct task_struct *tsk)
303 if (!v8086_mode(regs))
306 bit = (address - 0xA0000) >> PAGE_SHIFT;
308 tsk->thread.screen_bitmap |= 1 << bit;
311 static void dump_pagetable(unsigned long address)
313 __typeof__(pte_val(__pte(0))) page;
316 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
318 #ifdef CONFIG_X86_PAE
319 printk("*pdpt = %016Lx ", page);
320 if ((page >> PAGE_SHIFT) < max_low_pfn
321 && page & _PAGE_PRESENT) {
323 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
324 & (PTRS_PER_PMD - 1)];
325 printk(KERN_CONT "*pde = %016Lx ", page);
329 printk("*pde = %08lx ", page);
333 * We must not directly access the pte in the highpte
334 * case if the page table is located in highmem.
335 * And let's rather not kmap-atomic the pte, just in case
336 * it's allocated already:
338 if ((page >> PAGE_SHIFT) < max_low_pfn
339 && (page & _PAGE_PRESENT)
340 && !(page & _PAGE_PSE)) {
343 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
344 & (PTRS_PER_PTE - 1)];
345 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
351 #else /* CONFIG_X86_64: */
353 void vmalloc_sync_all(void)
355 unsigned long address;
357 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
358 address += PGDIR_SIZE) {
360 const pgd_t *pgd_ref = pgd_offset_k(address);
364 if (pgd_none(*pgd_ref))
367 spin_lock_irqsave(&pgd_lock, flags);
368 list_for_each_entry(page, &pgd_list, lru) {
370 pgd = (pgd_t *)page_address(page) + pgd_index(address);
372 set_pgd(pgd, *pgd_ref);
374 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
376 spin_unlock_irqrestore(&pgd_lock, flags);
383 * Handle a fault on the vmalloc area
385 * This assumes no large pages in there.
387 static noinline int vmalloc_fault(unsigned long address)
389 pgd_t *pgd, *pgd_ref;
390 pud_t *pud, *pud_ref;
391 pmd_t *pmd, *pmd_ref;
392 pte_t *pte, *pte_ref;
394 /* Make sure we are in vmalloc area: */
395 if (!(address >= VMALLOC_START && address < VMALLOC_END))
399 * Copy kernel mappings over when needed. This can also
400 * happen within a race in page table update. In the later
403 pgd = pgd_offset(current->active_mm, address);
404 pgd_ref = pgd_offset_k(address);
405 if (pgd_none(*pgd_ref))
409 set_pgd(pgd, *pgd_ref);
411 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
414 * Below here mismatches are bugs because these lower tables
418 pud = pud_offset(pgd, address);
419 pud_ref = pud_offset(pgd_ref, address);
420 if (pud_none(*pud_ref))
423 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
426 pmd = pmd_offset(pud, address);
427 pmd_ref = pmd_offset(pud_ref, address);
428 if (pmd_none(*pmd_ref))
431 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
434 pte_ref = pte_offset_kernel(pmd_ref, address);
435 if (!pte_present(*pte_ref))
438 pte = pte_offset_kernel(pmd, address);
441 * Don't use pte_page here, because the mappings can point
442 * outside mem_map, and the NUMA hash lookup cannot handle
445 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
451 static const char errata93_warning[] =
452 KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
453 KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
454 KERN_ERR "******* Please consider a BIOS update.\n"
455 KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
458 * No vm86 mode in 64-bit mode:
461 check_v8086_mode(struct pt_regs *regs, unsigned long address,
462 struct task_struct *tsk)
466 static int bad_address(void *p)
470 return probe_kernel_address((unsigned long *)p, dummy);
473 static void dump_pagetable(unsigned long address)
480 pgd = (pgd_t *)read_cr3();
482 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
484 pgd += pgd_index(address);
485 if (bad_address(pgd))
488 printk("PGD %lx ", pgd_val(*pgd));
490 if (!pgd_present(*pgd))
493 pud = pud_offset(pgd, address);
494 if (bad_address(pud))
497 printk("PUD %lx ", pud_val(*pud));
498 if (!pud_present(*pud) || pud_large(*pud))
501 pmd = pmd_offset(pud, address);
502 if (bad_address(pmd))
505 printk("PMD %lx ", pmd_val(*pmd));
506 if (!pmd_present(*pmd) || pmd_large(*pmd))
509 pte = pte_offset_kernel(pmd, address);
510 if (bad_address(pte))
513 printk("PTE %lx", pte_val(*pte));
521 #endif /* CONFIG_X86_64 */
524 * Workaround for K8 erratum #93 & buggy BIOS.
526 * BIOS SMM functions are required to use a specific workaround
527 * to avoid corruption of the 64bit RIP register on C stepping K8.
529 * A lot of BIOS that didn't get tested properly miss this.
531 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
532 * Try to work around it here.
534 * Note we only handle faults in kernel here.
535 * Does nothing on 32-bit.
537 static int is_errata93(struct pt_regs *regs, unsigned long address)
542 if (address != regs->ip)
545 if ((address >> 32) != 0)
548 address |= 0xffffffffUL << 32;
549 if ((address >= (u64)_stext && address <= (u64)_etext) ||
550 (address >= MODULES_VADDR && address <= MODULES_END)) {
552 printk(errata93_warning);
563 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
564 * to illegal addresses >4GB.
566 * We catch this in the page fault handler because these addresses
567 * are not reachable. Just detect this case and return. Any code
568 * segment in LDT is compatibility mode.
570 static int is_errata100(struct pt_regs *regs, unsigned long address)
573 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
579 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
581 #ifdef CONFIG_X86_F00F_BUG
585 * Pentium F0 0F C7 C8 bug workaround:
587 if (boot_cpu_data.f00f_bug) {
588 nr = (address - idt_descr.address) >> 3;
591 do_invalid_op(regs, 0);
599 static const char nx_warning[] = KERN_CRIT
600 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
603 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
604 unsigned long address)
606 if (!oops_may_print())
609 if (error_code & PF_INSTR) {
612 pte_t *pte = lookup_address(address, &level);
614 if (pte && pte_present(*pte) && !pte_exec(*pte))
615 printk(nx_warning, current_uid());
618 printk(KERN_ALERT "BUG: unable to handle kernel ");
619 if (address < PAGE_SIZE)
620 printk(KERN_CONT "NULL pointer dereference");
622 printk(KERN_CONT "paging request");
624 printk(KERN_CONT " at %p\n", (void *) address);
625 printk(KERN_ALERT "IP:");
626 printk_address(regs->ip, 1);
628 dump_pagetable(address);
632 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
633 unsigned long address)
635 struct task_struct *tsk;
639 flags = oops_begin();
643 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
645 dump_pagetable(address);
647 tsk->thread.cr2 = address;
648 tsk->thread.trap_no = 14;
649 tsk->thread.error_code = error_code;
651 if (__die("Bad pagetable", regs, error_code))
654 oops_end(flags, regs, sig);
658 no_context(struct pt_regs *regs, unsigned long error_code,
659 unsigned long address)
661 struct task_struct *tsk = current;
662 unsigned long *stackend;
666 /* Are we prepared to handle this kernel fault? */
667 if (fixup_exception(regs))
673 * Valid to do another page fault here, because if this fault
674 * had been triggered by is_prefetch fixup_exception would have
679 * Hall of shame of CPU/BIOS bugs.
681 if (is_prefetch(regs, error_code, address))
684 if (is_errata93(regs, address))
688 * Oops. The kernel tried to access some bad page. We'll have to
689 * terminate things with extreme prejudice:
691 flags = oops_begin();
693 show_fault_oops(regs, error_code, address);
695 stackend = end_of_stack(tsk);
696 if (*stackend != STACK_END_MAGIC)
697 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
699 tsk->thread.cr2 = address;
700 tsk->thread.trap_no = 14;
701 tsk->thread.error_code = error_code;
704 if (__die("Oops", regs, error_code))
707 /* Executive summary in case the body of the oops scrolled away */
708 printk(KERN_EMERG "CR2: %016lx\n", address);
710 oops_end(flags, regs, sig);
714 * Print out info about fatal segfaults, if the show_unhandled_signals
718 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
719 unsigned long address, struct task_struct *tsk)
721 if (!unhandled_signal(tsk, SIGSEGV))
724 if (!printk_ratelimit())
727 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
728 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
729 tsk->comm, task_pid_nr(tsk), address,
730 (void *)regs->ip, (void *)regs->sp, error_code);
732 print_vma_addr(KERN_CONT " in ", regs->ip);
734 printk(KERN_CONT "\n");
738 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
739 unsigned long address, int si_code)
741 struct task_struct *tsk = current;
743 /* User mode accesses just cause a SIGSEGV */
744 if (error_code & PF_USER) {
746 * It's possible to have interrupts off here:
751 * Valid to do another page fault here because this one came
754 if (is_prefetch(regs, error_code, address))
757 if (is_errata100(regs, address))
760 if (unlikely(show_unhandled_signals))
761 show_signal_msg(regs, error_code, address, tsk);
763 /* Kernel addresses are always protection faults: */
764 tsk->thread.cr2 = address;
765 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
766 tsk->thread.trap_no = 14;
768 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
773 if (is_f00f_bug(regs, address))
776 no_context(regs, error_code, address);
780 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
781 unsigned long address)
783 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
787 __bad_area(struct pt_regs *regs, unsigned long error_code,
788 unsigned long address, int si_code)
790 struct mm_struct *mm = current->mm;
793 * Something tried to access memory that isn't in our memory map..
794 * Fix it, but check if it's kernel or user first..
796 up_read(&mm->mmap_sem);
798 __bad_area_nosemaphore(regs, error_code, address, si_code);
802 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
804 __bad_area(regs, error_code, address, SEGV_MAPERR);
808 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
809 unsigned long address)
811 __bad_area(regs, error_code, address, SEGV_ACCERR);
814 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
816 out_of_memory(struct pt_regs *regs, unsigned long error_code,
817 unsigned long address)
820 * We ran out of memory, call the OOM killer, and return the userspace
821 * (which will retry the fault, or kill us if we got oom-killed):
823 up_read(¤t->mm->mmap_sem);
825 pagefault_out_of_memory();
829 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
831 struct task_struct *tsk = current;
832 struct mm_struct *mm = tsk->mm;
834 up_read(&mm->mmap_sem);
836 /* Kernel mode? Handle exceptions or die: */
837 if (!(error_code & PF_USER))
838 no_context(regs, error_code, address);
840 /* User-space => ok to do another page fault: */
841 if (is_prefetch(regs, error_code, address))
844 tsk->thread.cr2 = address;
845 tsk->thread.error_code = error_code;
846 tsk->thread.trap_no = 14;
848 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
852 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
853 unsigned long address, unsigned int fault)
855 if (fault & VM_FAULT_OOM) {
856 out_of_memory(regs, error_code, address);
858 if (fault & VM_FAULT_SIGBUS)
859 do_sigbus(regs, error_code, address);
865 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
867 if ((error_code & PF_WRITE) && !pte_write(*pte))
870 if ((error_code & PF_INSTR) && !pte_exec(*pte))
877 * Handle a spurious fault caused by a stale TLB entry.
879 * This allows us to lazily refresh the TLB when increasing the
880 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
881 * eagerly is very expensive since that implies doing a full
882 * cross-processor TLB flush, even if no stale TLB entries exist
883 * on other processors.
885 * There are no security implications to leaving a stale TLB when
886 * increasing the permissions on a page.
889 spurious_fault(unsigned long error_code, unsigned long address)
897 /* Reserved-bit violation or user access to kernel space? */
898 if (error_code & (PF_USER | PF_RSVD))
901 pgd = init_mm.pgd + pgd_index(address);
902 if (!pgd_present(*pgd))
905 pud = pud_offset(pgd, address);
906 if (!pud_present(*pud))
910 return spurious_fault_check(error_code, (pte_t *) pud);
912 pmd = pmd_offset(pud, address);
913 if (!pmd_present(*pmd))
917 return spurious_fault_check(error_code, (pte_t *) pmd);
919 pte = pte_offset_kernel(pmd, address);
920 if (!pte_present(*pte))
923 ret = spurious_fault_check(error_code, pte);
928 * Make sure we have permissions in PMD.
929 * If not, then there's a bug in the page tables:
931 ret = spurious_fault_check(error_code, (pte_t *) pmd);
932 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
937 int show_unhandled_signals = 1;
940 access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
943 /* write, present and write, not present: */
944 if (unlikely(!(vma->vm_flags & VM_WRITE)))
950 if (unlikely(error_code & PF_PROT))
953 /* read, not present: */
954 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
960 static int fault_in_kernel_space(unsigned long address)
962 return address >= TASK_SIZE_MAX;
966 * This routine handles page faults. It determines the address,
967 * and the problem, and then passes it off to one of the appropriate
970 dotraplinkage void __kprobes
971 do_page_fault(struct pt_regs *regs, unsigned long error_code)
973 struct vm_area_struct *vma;
974 struct task_struct *tsk;
975 unsigned long address;
976 struct mm_struct *mm;
983 prefetchw(&mm->mmap_sem);
985 /* Get the faulting address: */
986 address = read_cr2();
988 if (unlikely(kmmio_fault(regs, address)))
992 * We fault-in kernel-space virtual memory on-demand. The
993 * 'reference' page table is init_mm.pgd.
995 * NOTE! We MUST NOT take any locks for this case. We may
996 * be in an interrupt or a critical region, and should
997 * only copy the information from the master page table,
1000 * This verifies that the fault happens in kernel space
1001 * (error_code & 4) == 0, and that the fault was not a
1002 * protection error (error_code & 9) == 0.
1004 if (unlikely(fault_in_kernel_space(address))) {
1005 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
1006 vmalloc_fault(address) >= 0)
1009 /* Can handle a stale RO->RW TLB: */
1010 if (spurious_fault(error_code, address))
1013 /* kprobes don't want to hook the spurious faults: */
1014 if (notify_page_fault(regs))
1017 * Don't take the mm semaphore here. If we fixup a prefetch
1018 * fault we could otherwise deadlock:
1020 bad_area_nosemaphore(regs, error_code, address);
1025 /* kprobes don't want to hook the spurious faults: */
1026 if (unlikely(notify_page_fault(regs)))
1029 * It's safe to allow irq's after cr2 has been saved and the
1030 * vmalloc fault has been handled.
1032 * User-mode registers count as a user access even for any
1033 * potential system fault or CPU buglet:
1035 if (user_mode_vm(regs)) {
1037 error_code |= PF_USER;
1039 if (regs->flags & X86_EFLAGS_IF)
1043 if (unlikely(error_code & PF_RSVD))
1044 pgtable_bad(regs, error_code, address);
1047 * If we're in an interrupt, have no user context or are running
1048 * in an atomic region then we must not take the fault:
1050 if (unlikely(in_atomic() || !mm)) {
1051 bad_area_nosemaphore(regs, error_code, address);
1056 * When running in the kernel we expect faults to occur only to
1057 * addresses in user space. All other faults represent errors in
1058 * the kernel and should generate an OOPS. Unfortunately, in the
1059 * case of an erroneous fault occurring in a code path which already
1060 * holds mmap_sem we will deadlock attempting to validate the fault
1061 * against the address space. Luckily the kernel only validly
1062 * references user space from well defined areas of code, which are
1063 * listed in the exceptions table.
1065 * As the vast majority of faults will be valid we will only perform
1066 * the source reference check when there is a possibility of a
1067 * deadlock. Attempt to lock the address space, if we cannot we then
1068 * validate the source. If this is invalid we can skip the address
1069 * space check, thus avoiding the deadlock:
1071 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1072 if ((error_code & PF_USER) == 0 &&
1073 !search_exception_tables(regs->ip)) {
1074 bad_area_nosemaphore(regs, error_code, address);
1077 down_read(&mm->mmap_sem);
1080 * The above down_read_trylock() might have succeeded in
1081 * which case we'll have missed the might_sleep() from
1087 vma = find_vma(mm, address);
1088 if (unlikely(!vma)) {
1089 bad_area(regs, error_code, address);
1092 if (likely(vma->vm_start <= address))
1094 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1095 bad_area(regs, error_code, address);
1098 if (error_code & PF_USER) {
1100 * Accessing the stack below %sp is always a bug.
1101 * The large cushion allows instructions like enter
1102 * and pusha to work. ("enter $65535, $31" pushes
1103 * 32 pointers and then decrements %sp by 65535.)
1105 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1106 bad_area(regs, error_code, address);
1110 if (unlikely(expand_stack(vma, address))) {
1111 bad_area(regs, error_code, address);
1116 * Ok, we have a good vm_area for this memory access, so
1117 * we can handle it..
1120 write = error_code & PF_WRITE;
1122 if (unlikely(access_error(error_code, write, vma))) {
1123 bad_area_access_error(regs, error_code, address);
1128 * If for any reason at all we couldn't handle the fault,
1129 * make sure we exit gracefully rather than endlessly redo
1132 fault = handle_mm_fault(mm, vma, address, write);
1134 if (unlikely(fault & VM_FAULT_ERROR)) {
1135 mm_fault_error(regs, error_code, address, fault);
1139 if (fault & VM_FAULT_MAJOR)
1144 check_v8086_mode(regs, address, tsk);
1146 up_read(&mm->mmap_sem);