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/magic.h> /* STACK_END_MAGIC */
7 #include <linux/sched.h> /* test_thread_flag(), ... */
8 #include <linux/kdebug.h> /* oops_begin/end, ... */
9 #include <linux/module.h> /* search_exception_table */
10 #include <linux/bootmem.h> /* max_low_pfn */
11 #include <linux/kprobes.h> /* __kprobes, ... */
12 #include <linux/mmiotrace.h> /* kmmio_handler, ... */
14 #include <asm/traps.h> /* dotraplinkage, ... */
15 #include <asm/pgalloc.h> /* pgd_*(), ... */
18 * Page fault error code bits:
20 * bit 0 == 0: no page found 1: protection fault
21 * bit 1 == 0: read access 1: write access
22 * bit 2 == 0: kernel-mode access 1: user-mode access
23 * bit 3 == 1: use of reserved bit detected
24 * bit 4 == 1: fault was an instruction fetch
26 enum x86_pf_error_code {
36 * Returns 0 if mmiotrace is disabled, or if the fault is not
37 * handled by mmiotrace:
39 static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
41 if (unlikely(is_kmmio_active()))
42 if (kmmio_handler(regs, addr) == 1)
47 static inline int notify_page_fault(struct pt_regs *regs)
51 /* kprobe_running() needs smp_processor_id() */
52 if (kprobes_built_in() && !user_mode_vm(regs)) {
54 if (kprobe_running() && kprobe_fault_handler(regs, 14))
67 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
68 * Check that here and ignore it.
72 * Sometimes the CPU reports invalid exceptions on prefetch.
73 * Check that here and ignore it.
75 * Opcode checker based on code by Richard Brunner.
78 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
79 unsigned char opcode, int *prefetch)
81 unsigned char instr_hi = opcode & 0xf0;
82 unsigned char instr_lo = opcode & 0x0f;
88 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
89 * In X86_64 long mode, the CPU will signal invalid
90 * opcode if some of these prefixes are present so
91 * X86_64 will never get here anyway
93 return ((instr_lo & 7) == 0x6);
97 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
98 * Need to figure out under what instruction mode the
99 * instruction was issued. Could check the LDT for lm,
100 * but for now it's good enough to assume that long
101 * mode only uses well known segments or kernel.
103 return (!user_mode(regs)) || (regs->cs == __USER_CS);
106 /* 0x64 thru 0x67 are valid prefixes in all modes. */
107 return (instr_lo & 0xC) == 0x4;
109 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
110 return !instr_lo || (instr_lo>>1) == 1;
112 /* Prefetch instruction is 0x0F0D or 0x0F18 */
113 if (probe_kernel_address(instr, opcode))
116 *prefetch = (instr_lo == 0xF) &&
117 (opcode == 0x0D || opcode == 0x18);
125 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
127 unsigned char *max_instr;
128 unsigned char *instr;
132 * If it was a exec (instruction fetch) fault on NX page, then
133 * do not ignore the fault:
135 if (error_code & PF_INSTR)
138 instr = (void *)convert_ip_to_linear(current, regs);
139 max_instr = instr + 15;
141 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
144 while (instr < max_instr) {
145 unsigned char opcode;
147 if (probe_kernel_address(instr, opcode))
152 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
159 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
160 struct task_struct *tsk)
164 info.si_signo = si_signo;
166 info.si_code = si_code;
167 info.si_addr = (void __user *)address;
169 force_sig_info(si_signo, &info, tsk);
172 DEFINE_SPINLOCK(pgd_lock);
176 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
178 unsigned index = pgd_index(address);
184 pgd_k = init_mm.pgd + index;
186 if (!pgd_present(*pgd_k))
190 * set_pgd(pgd, *pgd_k); here would be useless on PAE
191 * and redundant with the set_pmd() on non-PAE. As would
194 pud = pud_offset(pgd, address);
195 pud_k = pud_offset(pgd_k, address);
196 if (!pud_present(*pud_k))
199 pmd = pmd_offset(pud, address);
200 pmd_k = pmd_offset(pud_k, address);
201 if (!pmd_present(*pmd_k))
204 if (!pmd_present(*pmd)) {
205 set_pmd(pmd, *pmd_k);
206 arch_flush_lazy_mmu_mode();
208 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
214 void vmalloc_sync_all(void)
216 unsigned long address;
218 if (SHARED_KERNEL_PMD)
221 for (address = VMALLOC_START & PMD_MASK;
222 address >= TASK_SIZE && address < FIXADDR_TOP;
223 address += PMD_SIZE) {
228 spin_lock_irqsave(&pgd_lock, flags);
229 list_for_each_entry(page, &pgd_list, lru) {
230 if (!vmalloc_sync_one(page_address(page), address))
233 spin_unlock_irqrestore(&pgd_lock, flags);
240 * Handle a fault on the vmalloc or module mapping area
242 static noinline int vmalloc_fault(unsigned long address)
244 unsigned long pgd_paddr;
248 /* Make sure we are in vmalloc area: */
249 if (!(address >= VMALLOC_START && address < VMALLOC_END))
253 * Synchronize this task's top level page-table
254 * with the 'reference' page table.
256 * Do _not_ use "current" here. We might be inside
257 * an interrupt in the middle of a task switch..
259 pgd_paddr = read_cr3();
260 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
264 pte_k = pte_offset_kernel(pmd_k, address);
265 if (!pte_present(*pte_k))
272 * Did it hit the DOS screen memory VA from vm86 mode?
275 check_v8086_mode(struct pt_regs *regs, unsigned long address,
276 struct task_struct *tsk)
280 if (!v8086_mode(regs))
283 bit = (address - 0xA0000) >> PAGE_SHIFT;
285 tsk->thread.screen_bitmap |= 1 << bit;
288 static void dump_pagetable(unsigned long address)
290 __typeof__(pte_val(__pte(0))) page;
293 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
295 #ifdef CONFIG_X86_PAE
296 printk("*pdpt = %016Lx ", page);
297 if ((page >> PAGE_SHIFT) < max_low_pfn
298 && page & _PAGE_PRESENT) {
300 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
301 & (PTRS_PER_PMD - 1)];
302 printk(KERN_CONT "*pde = %016Lx ", page);
306 printk("*pde = %08lx ", page);
310 * We must not directly access the pte in the highpte
311 * case if the page table is located in highmem.
312 * And let's rather not kmap-atomic the pte, just in case
313 * it's allocated already:
315 if ((page >> PAGE_SHIFT) < max_low_pfn
316 && (page & _PAGE_PRESENT)
317 && !(page & _PAGE_PSE)) {
320 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
321 & (PTRS_PER_PTE - 1)];
322 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
328 #else /* CONFIG_X86_64: */
330 void vmalloc_sync_all(void)
332 unsigned long address;
334 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
335 address += PGDIR_SIZE) {
337 const pgd_t *pgd_ref = pgd_offset_k(address);
341 if (pgd_none(*pgd_ref))
344 spin_lock_irqsave(&pgd_lock, flags);
345 list_for_each_entry(page, &pgd_list, lru) {
347 pgd = (pgd_t *)page_address(page) + pgd_index(address);
349 set_pgd(pgd, *pgd_ref);
351 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
353 spin_unlock_irqrestore(&pgd_lock, flags);
360 * Handle a fault on the vmalloc area
362 * This assumes no large pages in there.
364 static noinline int vmalloc_fault(unsigned long address)
366 pgd_t *pgd, *pgd_ref;
367 pud_t *pud, *pud_ref;
368 pmd_t *pmd, *pmd_ref;
369 pte_t *pte, *pte_ref;
371 /* Make sure we are in vmalloc area: */
372 if (!(address >= VMALLOC_START && address < VMALLOC_END))
376 * Copy kernel mappings over when needed. This can also
377 * happen within a race in page table update. In the later
380 pgd = pgd_offset(current->active_mm, address);
381 pgd_ref = pgd_offset_k(address);
382 if (pgd_none(*pgd_ref))
386 set_pgd(pgd, *pgd_ref);
388 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
391 * Below here mismatches are bugs because these lower tables
395 pud = pud_offset(pgd, address);
396 pud_ref = pud_offset(pgd_ref, address);
397 if (pud_none(*pud_ref))
400 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
403 pmd = pmd_offset(pud, address);
404 pmd_ref = pmd_offset(pud_ref, address);
405 if (pmd_none(*pmd_ref))
408 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
411 pte_ref = pte_offset_kernel(pmd_ref, address);
412 if (!pte_present(*pte_ref))
415 pte = pte_offset_kernel(pmd, address);
418 * Don't use pte_page here, because the mappings can point
419 * outside mem_map, and the NUMA hash lookup cannot handle
422 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
428 static const char errata93_warning[] =
429 KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
430 KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
431 KERN_ERR "******* Please consider a BIOS update.\n"
432 KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
435 * No vm86 mode in 64-bit mode:
438 check_v8086_mode(struct pt_regs *regs, unsigned long address,
439 struct task_struct *tsk)
443 static int bad_address(void *p)
447 return probe_kernel_address((unsigned long *)p, dummy);
450 static void dump_pagetable(unsigned long address)
457 pgd = (pgd_t *)read_cr3();
459 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
461 pgd += pgd_index(address);
462 if (bad_address(pgd))
465 printk("PGD %lx ", pgd_val(*pgd));
467 if (!pgd_present(*pgd))
470 pud = pud_offset(pgd, address);
471 if (bad_address(pud))
474 printk("PUD %lx ", pud_val(*pud));
475 if (!pud_present(*pud) || pud_large(*pud))
478 pmd = pmd_offset(pud, address);
479 if (bad_address(pmd))
482 printk("PMD %lx ", pmd_val(*pmd));
483 if (!pmd_present(*pmd) || pmd_large(*pmd))
486 pte = pte_offset_kernel(pmd, address);
487 if (bad_address(pte))
490 printk("PTE %lx", pte_val(*pte));
498 #endif /* CONFIG_X86_64 */
501 * Workaround for K8 erratum #93 & buggy BIOS.
503 * BIOS SMM functions are required to use a specific workaround
504 * to avoid corruption of the 64bit RIP register on C stepping K8.
506 * A lot of BIOS that didn't get tested properly miss this.
508 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
509 * Try to work around it here.
511 * Note we only handle faults in kernel here.
512 * Does nothing on 32-bit.
514 static int is_errata93(struct pt_regs *regs, unsigned long address)
519 if (address != regs->ip)
522 if ((address >> 32) != 0)
525 address |= 0xffffffffUL << 32;
526 if ((address >= (u64)_stext && address <= (u64)_etext) ||
527 (address >= MODULES_VADDR && address <= MODULES_END)) {
529 printk(errata93_warning);
540 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
541 * to illegal addresses >4GB.
543 * We catch this in the page fault handler because these addresses
544 * are not reachable. Just detect this case and return. Any code
545 * segment in LDT is compatibility mode.
547 static int is_errata100(struct pt_regs *regs, unsigned long address)
550 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
556 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
558 #ifdef CONFIG_X86_F00F_BUG
562 * Pentium F0 0F C7 C8 bug workaround:
564 if (boot_cpu_data.f00f_bug) {
565 nr = (address - idt_descr.address) >> 3;
568 do_invalid_op(regs, 0);
576 static const char nx_warning[] = KERN_CRIT
577 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
580 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
581 unsigned long address)
583 if (!oops_may_print())
586 if (error_code & PF_INSTR) {
589 pte_t *pte = lookup_address(address, &level);
591 if (pte && pte_present(*pte) && !pte_exec(*pte))
592 printk(nx_warning, current_uid());
595 printk(KERN_ALERT "BUG: unable to handle kernel ");
596 if (address < PAGE_SIZE)
597 printk(KERN_CONT "NULL pointer dereference");
599 printk(KERN_CONT "paging request");
601 printk(KERN_CONT " at %p\n", (void *) address);
602 printk(KERN_ALERT "IP:");
603 printk_address(regs->ip, 1);
605 dump_pagetable(address);
609 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
610 unsigned long address)
612 struct task_struct *tsk;
616 flags = oops_begin();
620 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
622 dump_pagetable(address);
624 tsk->thread.cr2 = address;
625 tsk->thread.trap_no = 14;
626 tsk->thread.error_code = error_code;
628 if (__die("Bad pagetable", regs, error_code))
631 oops_end(flags, regs, sig);
635 no_context(struct pt_regs *regs, unsigned long error_code,
636 unsigned long address)
638 struct task_struct *tsk = current;
639 unsigned long *stackend;
643 /* Are we prepared to handle this kernel fault? */
644 if (fixup_exception(regs))
650 * Valid to do another page fault here, because if this fault
651 * had been triggered by is_prefetch fixup_exception would have
656 * Hall of shame of CPU/BIOS bugs.
658 if (is_prefetch(regs, error_code, address))
661 if (is_errata93(regs, address))
665 * Oops. The kernel tried to access some bad page. We'll have to
666 * terminate things with extreme prejudice:
668 flags = oops_begin();
670 show_fault_oops(regs, error_code, address);
672 stackend = end_of_stack(tsk);
673 if (*stackend != STACK_END_MAGIC)
674 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
676 tsk->thread.cr2 = address;
677 tsk->thread.trap_no = 14;
678 tsk->thread.error_code = error_code;
681 if (__die("Oops", regs, error_code))
684 /* Executive summary in case the body of the oops scrolled away */
685 printk(KERN_EMERG "CR2: %016lx\n", address);
687 oops_end(flags, regs, sig);
691 * Print out info about fatal segfaults, if the show_unhandled_signals
695 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
696 unsigned long address, struct task_struct *tsk)
698 if (!unhandled_signal(tsk, SIGSEGV))
701 if (!printk_ratelimit())
704 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
705 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
706 tsk->comm, task_pid_nr(tsk), address,
707 (void *)regs->ip, (void *)regs->sp, error_code);
709 print_vma_addr(KERN_CONT " in ", regs->ip);
711 printk(KERN_CONT "\n");
715 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
716 unsigned long address, int si_code)
718 struct task_struct *tsk = current;
720 /* User mode accesses just cause a SIGSEGV */
721 if (error_code & PF_USER) {
723 * It's possible to have interrupts off here:
728 * Valid to do another page fault here because this one came
731 if (is_prefetch(regs, error_code, address))
734 if (is_errata100(regs, address))
737 if (unlikely(show_unhandled_signals))
738 show_signal_msg(regs, error_code, address, tsk);
740 /* Kernel addresses are always protection faults: */
741 tsk->thread.cr2 = address;
742 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
743 tsk->thread.trap_no = 14;
745 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
750 if (is_f00f_bug(regs, address))
753 no_context(regs, error_code, address);
757 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
758 unsigned long address)
760 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
764 __bad_area(struct pt_regs *regs, unsigned long error_code,
765 unsigned long address, int si_code)
767 struct mm_struct *mm = current->mm;
770 * Something tried to access memory that isn't in our memory map..
771 * Fix it, but check if it's kernel or user first..
773 up_read(&mm->mmap_sem);
775 __bad_area_nosemaphore(regs, error_code, address, si_code);
779 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
781 __bad_area(regs, error_code, address, SEGV_MAPERR);
785 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
786 unsigned long address)
788 __bad_area(regs, error_code, address, SEGV_ACCERR);
791 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
793 out_of_memory(struct pt_regs *regs, unsigned long error_code,
794 unsigned long address)
797 * We ran out of memory, call the OOM killer, and return the userspace
798 * (which will retry the fault, or kill us if we got oom-killed):
800 up_read(¤t->mm->mmap_sem);
802 pagefault_out_of_memory();
806 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
808 struct task_struct *tsk = current;
809 struct mm_struct *mm = tsk->mm;
811 up_read(&mm->mmap_sem);
813 /* Kernel mode? Handle exceptions or die: */
814 if (!(error_code & PF_USER))
815 no_context(regs, error_code, address);
817 /* User-space => ok to do another page fault: */
818 if (is_prefetch(regs, error_code, address))
821 tsk->thread.cr2 = address;
822 tsk->thread.error_code = error_code;
823 tsk->thread.trap_no = 14;
825 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
829 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
830 unsigned long address, unsigned int fault)
832 if (fault & VM_FAULT_OOM) {
833 out_of_memory(regs, error_code, address);
835 if (fault & VM_FAULT_SIGBUS)
836 do_sigbus(regs, error_code, address);
842 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
844 if ((error_code & PF_WRITE) && !pte_write(*pte))
847 if ((error_code & PF_INSTR) && !pte_exec(*pte))
854 * Handle a spurious fault caused by a stale TLB entry.
856 * This allows us to lazily refresh the TLB when increasing the
857 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
858 * eagerly is very expensive since that implies doing a full
859 * cross-processor TLB flush, even if no stale TLB entries exist
860 * on other processors.
862 * There are no security implications to leaving a stale TLB when
863 * increasing the permissions on a page.
866 spurious_fault(unsigned long error_code, unsigned long address)
874 /* Reserved-bit violation or user access to kernel space? */
875 if (error_code & (PF_USER | PF_RSVD))
878 pgd = init_mm.pgd + pgd_index(address);
879 if (!pgd_present(*pgd))
882 pud = pud_offset(pgd, address);
883 if (!pud_present(*pud))
887 return spurious_fault_check(error_code, (pte_t *) pud);
889 pmd = pmd_offset(pud, address);
890 if (!pmd_present(*pmd))
894 return spurious_fault_check(error_code, (pte_t *) pmd);
896 pte = pte_offset_kernel(pmd, address);
897 if (!pte_present(*pte))
900 ret = spurious_fault_check(error_code, pte);
905 * Make sure we have permissions in PMD.
906 * If not, then there's a bug in the page tables:
908 ret = spurious_fault_check(error_code, (pte_t *) pmd);
909 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
914 int show_unhandled_signals = 1;
917 access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
920 /* write, present and write, not present: */
921 if (unlikely(!(vma->vm_flags & VM_WRITE)))
927 if (unlikely(error_code & PF_PROT))
930 /* read, not present: */
931 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
937 static int fault_in_kernel_space(unsigned long address)
939 return address >= TASK_SIZE_MAX;
943 * This routine handles page faults. It determines the address,
944 * and the problem, and then passes it off to one of the appropriate
947 dotraplinkage void __kprobes
948 do_page_fault(struct pt_regs *regs, unsigned long error_code)
950 struct vm_area_struct *vma;
951 struct task_struct *tsk;
952 unsigned long address;
953 struct mm_struct *mm;
960 prefetchw(&mm->mmap_sem);
962 /* Get the faulting address: */
963 address = read_cr2();
965 if (unlikely(kmmio_fault(regs, address)))
969 * We fault-in kernel-space virtual memory on-demand. The
970 * 'reference' page table is init_mm.pgd.
972 * NOTE! We MUST NOT take any locks for this case. We may
973 * be in an interrupt or a critical region, and should
974 * only copy the information from the master page table,
977 * This verifies that the fault happens in kernel space
978 * (error_code & 4) == 0, and that the fault was not a
979 * protection error (error_code & 9) == 0.
981 if (unlikely(fault_in_kernel_space(address))) {
982 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
983 vmalloc_fault(address) >= 0)
986 /* Can handle a stale RO->RW TLB: */
987 if (spurious_fault(error_code, address))
990 /* kprobes don't want to hook the spurious faults: */
991 if (notify_page_fault(regs))
994 * Don't take the mm semaphore here. If we fixup a prefetch
995 * fault we could otherwise deadlock:
997 bad_area_nosemaphore(regs, error_code, address);
1002 /* kprobes don't want to hook the spurious faults: */
1003 if (unlikely(notify_page_fault(regs)))
1006 * It's safe to allow irq's after cr2 has been saved and the
1007 * vmalloc fault has been handled.
1009 * User-mode registers count as a user access even for any
1010 * potential system fault or CPU buglet:
1012 if (user_mode_vm(regs)) {
1014 error_code |= PF_USER;
1016 if (regs->flags & X86_EFLAGS_IF)
1020 if (unlikely(error_code & PF_RSVD))
1021 pgtable_bad(regs, error_code, address);
1024 * If we're in an interrupt, have no user context or are running
1025 * in an atomic region then we must not take the fault:
1027 if (unlikely(in_atomic() || !mm)) {
1028 bad_area_nosemaphore(regs, error_code, address);
1033 * When running in the kernel we expect faults to occur only to
1034 * addresses in user space. All other faults represent errors in
1035 * the kernel and should generate an OOPS. Unfortunately, in the
1036 * case of an erroneous fault occurring in a code path which already
1037 * holds mmap_sem we will deadlock attempting to validate the fault
1038 * against the address space. Luckily the kernel only validly
1039 * references user space from well defined areas of code, which are
1040 * listed in the exceptions table.
1042 * As the vast majority of faults will be valid we will only perform
1043 * the source reference check when there is a possibility of a
1044 * deadlock. Attempt to lock the address space, if we cannot we then
1045 * validate the source. If this is invalid we can skip the address
1046 * space check, thus avoiding the deadlock:
1048 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1049 if ((error_code & PF_USER) == 0 &&
1050 !search_exception_tables(regs->ip)) {
1051 bad_area_nosemaphore(regs, error_code, address);
1054 down_read(&mm->mmap_sem);
1057 * The above down_read_trylock() might have succeeded in
1058 * which case we'll have missed the might_sleep() from
1064 vma = find_vma(mm, address);
1065 if (unlikely(!vma)) {
1066 bad_area(regs, error_code, address);
1069 if (likely(vma->vm_start <= address))
1071 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1072 bad_area(regs, error_code, address);
1075 if (error_code & PF_USER) {
1077 * Accessing the stack below %sp is always a bug.
1078 * The large cushion allows instructions like enter
1079 * and pusha to work. ("enter $65535, $31" pushes
1080 * 32 pointers and then decrements %sp by 65535.)
1082 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1083 bad_area(regs, error_code, address);
1087 if (unlikely(expand_stack(vma, address))) {
1088 bad_area(regs, error_code, address);
1093 * Ok, we have a good vm_area for this memory access, so
1094 * we can handle it..
1097 write = error_code & PF_WRITE;
1099 if (unlikely(access_error(error_code, write, vma))) {
1100 bad_area_access_error(regs, error_code, address);
1105 * If for any reason at all we couldn't handle the fault,
1106 * make sure we exit gracefully rather than endlessly redo
1109 fault = handle_mm_fault(mm, vma, address, write);
1111 if (unlikely(fault & VM_FAULT_ERROR)) {
1112 mm_fault_error(regs, error_code, address, fault);
1116 if (fault & VM_FAULT_MAJOR)
1121 check_v8086_mode(regs, address, tsk);
1123 up_read(&mm->mmap_sem);