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>
30 #include <linux/perf_counter.h>
32 #include <asm-generic/sections.h>
34 #include <asm/tlbflush.h>
35 #include <asm/pgalloc.h>
36 #include <asm/segment.h>
37 #include <asm/system.h>
38 #include <asm/proto.h>
39 #include <asm/traps.h>
43 * Page fault error code bits:
45 * bit 0 == 0: no page found 1: protection fault
46 * bit 1 == 0: read access 1: write access
47 * bit 2 == 0: kernel-mode access 1: user-mode access
48 * bit 3 == 1: use of reserved bit detected
49 * bit 4 == 1: fault was an instruction fetch
51 enum x86_pf_error_code {
61 * Returns 0 if mmiotrace is disabled, or if the fault is not
62 * handled by mmiotrace:
64 static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
66 if (unlikely(is_kmmio_active()))
67 if (kmmio_handler(regs, addr) == 1)
72 static inline int notify_page_fault(struct pt_regs *regs)
76 /* kprobe_running() needs smp_processor_id() */
77 if (kprobes_built_in() && !user_mode_vm(regs)) {
79 if (kprobe_running() && kprobe_fault_handler(regs, 14))
92 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
93 * Check that here and ignore it.
97 * Sometimes the CPU reports invalid exceptions on prefetch.
98 * Check that here and ignore it.
100 * Opcode checker based on code by Richard Brunner.
103 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
104 unsigned char opcode, int *prefetch)
106 unsigned char instr_hi = opcode & 0xf0;
107 unsigned char instr_lo = opcode & 0x0f;
113 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
114 * In X86_64 long mode, the CPU will signal invalid
115 * opcode if some of these prefixes are present so
116 * X86_64 will never get here anyway
118 return ((instr_lo & 7) == 0x6);
122 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
123 * Need to figure out under what instruction mode the
124 * instruction was issued. Could check the LDT for lm,
125 * but for now it's good enough to assume that long
126 * mode only uses well known segments or kernel.
128 return (!user_mode(regs)) || (regs->cs == __USER_CS);
131 /* 0x64 thru 0x67 are valid prefixes in all modes. */
132 return (instr_lo & 0xC) == 0x4;
134 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
135 return !instr_lo || (instr_lo>>1) == 1;
137 /* Prefetch instruction is 0x0F0D or 0x0F18 */
138 if (probe_kernel_address(instr, opcode))
141 *prefetch = (instr_lo == 0xF) &&
142 (opcode == 0x0D || opcode == 0x18);
150 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
152 unsigned char *max_instr;
153 unsigned char *instr;
157 * If it was a exec (instruction fetch) fault on NX page, then
158 * do not ignore the fault:
160 if (error_code & PF_INSTR)
163 instr = (void *)convert_ip_to_linear(current, regs);
164 max_instr = instr + 15;
166 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
169 while (instr < max_instr) {
170 unsigned char opcode;
172 if (probe_kernel_address(instr, opcode))
177 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
184 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
185 struct task_struct *tsk)
189 info.si_signo = si_signo;
191 info.si_code = si_code;
192 info.si_addr = (void __user *)address;
194 force_sig_info(si_signo, &info, tsk);
197 DEFINE_SPINLOCK(pgd_lock);
201 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
203 unsigned index = pgd_index(address);
209 pgd_k = init_mm.pgd + index;
211 if (!pgd_present(*pgd_k))
215 * set_pgd(pgd, *pgd_k); here would be useless on PAE
216 * and redundant with the set_pmd() on non-PAE. As would
219 pud = pud_offset(pgd, address);
220 pud_k = pud_offset(pgd_k, address);
221 if (!pud_present(*pud_k))
224 pmd = pmd_offset(pud, address);
225 pmd_k = pmd_offset(pud_k, address);
226 if (!pmd_present(*pmd_k))
229 if (!pmd_present(*pmd)) {
230 set_pmd(pmd, *pmd_k);
231 arch_flush_lazy_mmu_mode();
233 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
239 void vmalloc_sync_all(void)
241 unsigned long address;
243 if (SHARED_KERNEL_PMD)
246 for (address = VMALLOC_START & PMD_MASK;
247 address >= TASK_SIZE && address < FIXADDR_TOP;
248 address += PMD_SIZE) {
253 spin_lock_irqsave(&pgd_lock, flags);
254 list_for_each_entry(page, &pgd_list, lru) {
255 if (!vmalloc_sync_one(page_address(page), address))
258 spin_unlock_irqrestore(&pgd_lock, flags);
265 * Handle a fault on the vmalloc or module mapping area
267 static noinline int vmalloc_fault(unsigned long address)
269 unsigned long pgd_paddr;
273 /* Make sure we are in vmalloc area: */
274 if (!(address >= VMALLOC_START && address < VMALLOC_END))
278 * Synchronize this task's top level page-table
279 * with the 'reference' page table.
281 * Do _not_ use "current" here. We might be inside
282 * an interrupt in the middle of a task switch..
284 pgd_paddr = read_cr3();
285 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
289 pte_k = pte_offset_kernel(pmd_k, address);
290 if (!pte_present(*pte_k))
297 * Did it hit the DOS screen memory VA from vm86 mode?
300 check_v8086_mode(struct pt_regs *regs, unsigned long address,
301 struct task_struct *tsk)
305 if (!v8086_mode(regs))
308 bit = (address - 0xA0000) >> PAGE_SHIFT;
310 tsk->thread.screen_bitmap |= 1 << bit;
313 static void dump_pagetable(unsigned long address)
315 __typeof__(pte_val(__pte(0))) page;
318 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
320 #ifdef CONFIG_X86_PAE
321 printk("*pdpt = %016Lx ", page);
322 if ((page >> PAGE_SHIFT) < max_low_pfn
323 && page & _PAGE_PRESENT) {
325 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
326 & (PTRS_PER_PMD - 1)];
327 printk(KERN_CONT "*pde = %016Lx ", page);
331 printk("*pde = %08lx ", page);
335 * We must not directly access the pte in the highpte
336 * case if the page table is located in highmem.
337 * And let's rather not kmap-atomic the pte, just in case
338 * it's allocated already:
340 if ((page >> PAGE_SHIFT) < max_low_pfn
341 && (page & _PAGE_PRESENT)
342 && !(page & _PAGE_PSE)) {
345 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
346 & (PTRS_PER_PTE - 1)];
347 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
353 #else /* CONFIG_X86_64: */
355 void vmalloc_sync_all(void)
357 unsigned long address;
359 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
360 address += PGDIR_SIZE) {
362 const pgd_t *pgd_ref = pgd_offset_k(address);
366 if (pgd_none(*pgd_ref))
369 spin_lock_irqsave(&pgd_lock, flags);
370 list_for_each_entry(page, &pgd_list, lru) {
372 pgd = (pgd_t *)page_address(page) + pgd_index(address);
374 set_pgd(pgd, *pgd_ref);
376 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
378 spin_unlock_irqrestore(&pgd_lock, flags);
385 * Handle a fault on the vmalloc area
387 * This assumes no large pages in there.
389 static noinline int vmalloc_fault(unsigned long address)
391 pgd_t *pgd, *pgd_ref;
392 pud_t *pud, *pud_ref;
393 pmd_t *pmd, *pmd_ref;
394 pte_t *pte, *pte_ref;
396 /* Make sure we are in vmalloc area: */
397 if (!(address >= VMALLOC_START && address < VMALLOC_END))
401 * Copy kernel mappings over when needed. This can also
402 * happen within a race in page table update. In the later
405 pgd = pgd_offset(current->active_mm, address);
406 pgd_ref = pgd_offset_k(address);
407 if (pgd_none(*pgd_ref))
411 set_pgd(pgd, *pgd_ref);
413 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
416 * Below here mismatches are bugs because these lower tables
420 pud = pud_offset(pgd, address);
421 pud_ref = pud_offset(pgd_ref, address);
422 if (pud_none(*pud_ref))
425 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
428 pmd = pmd_offset(pud, address);
429 pmd_ref = pmd_offset(pud_ref, address);
430 if (pmd_none(*pmd_ref))
433 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
436 pte_ref = pte_offset_kernel(pmd_ref, address);
437 if (!pte_present(*pte_ref))
440 pte = pte_offset_kernel(pmd, address);
443 * Don't use pte_page here, because the mappings can point
444 * outside mem_map, and the NUMA hash lookup cannot handle
447 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
453 static const char errata93_warning[] =
454 KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
455 KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
456 KERN_ERR "******* Please consider a BIOS update.\n"
457 KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
460 * No vm86 mode in 64-bit mode:
463 check_v8086_mode(struct pt_regs *regs, unsigned long address,
464 struct task_struct *tsk)
468 static int bad_address(void *p)
472 return probe_kernel_address((unsigned long *)p, dummy);
475 static void dump_pagetable(unsigned long address)
482 pgd = (pgd_t *)read_cr3();
484 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
486 pgd += pgd_index(address);
487 if (bad_address(pgd))
490 printk("PGD %lx ", pgd_val(*pgd));
492 if (!pgd_present(*pgd))
495 pud = pud_offset(pgd, address);
496 if (bad_address(pud))
499 printk("PUD %lx ", pud_val(*pud));
500 if (!pud_present(*pud) || pud_large(*pud))
503 pmd = pmd_offset(pud, address);
504 if (bad_address(pmd))
507 printk("PMD %lx ", pmd_val(*pmd));
508 if (!pmd_present(*pmd) || pmd_large(*pmd))
511 pte = pte_offset_kernel(pmd, address);
512 if (bad_address(pte))
515 printk("PTE %lx", pte_val(*pte));
523 #endif /* CONFIG_X86_64 */
526 * Workaround for K8 erratum #93 & buggy BIOS.
528 * BIOS SMM functions are required to use a specific workaround
529 * to avoid corruption of the 64bit RIP register on C stepping K8.
531 * A lot of BIOS that didn't get tested properly miss this.
533 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
534 * Try to work around it here.
536 * Note we only handle faults in kernel here.
537 * Does nothing on 32-bit.
539 static int is_errata93(struct pt_regs *regs, unsigned long address)
544 if (address != regs->ip)
547 if ((address >> 32) != 0)
550 address |= 0xffffffffUL << 32;
551 if ((address >= (u64)_stext && address <= (u64)_etext) ||
552 (address >= MODULES_VADDR && address <= MODULES_END)) {
554 printk(errata93_warning);
565 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
566 * to illegal addresses >4GB.
568 * We catch this in the page fault handler because these addresses
569 * are not reachable. Just detect this case and return. Any code
570 * segment in LDT is compatibility mode.
572 static int is_errata100(struct pt_regs *regs, unsigned long address)
575 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
581 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
583 #ifdef CONFIG_X86_F00F_BUG
587 * Pentium F0 0F C7 C8 bug workaround:
589 if (boot_cpu_data.f00f_bug) {
590 nr = (address - idt_descr.address) >> 3;
593 do_invalid_op(regs, 0);
601 static const char nx_warning[] = KERN_CRIT
602 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
605 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
606 unsigned long address)
608 if (!oops_may_print())
611 if (error_code & PF_INSTR) {
614 pte_t *pte = lookup_address(address, &level);
616 if (pte && pte_present(*pte) && !pte_exec(*pte))
617 printk(nx_warning, current_uid());
620 printk(KERN_ALERT "BUG: unable to handle kernel ");
621 if (address < PAGE_SIZE)
622 printk(KERN_CONT "NULL pointer dereference");
624 printk(KERN_CONT "paging request");
626 printk(KERN_CONT " at %p\n", (void *) address);
627 printk(KERN_ALERT "IP:");
628 printk_address(regs->ip, 1);
630 dump_pagetable(address);
634 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
635 unsigned long address)
637 struct task_struct *tsk;
641 flags = oops_begin();
645 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
647 dump_pagetable(address);
649 tsk->thread.cr2 = address;
650 tsk->thread.trap_no = 14;
651 tsk->thread.error_code = error_code;
653 if (__die("Bad pagetable", regs, error_code))
656 oops_end(flags, regs, sig);
660 no_context(struct pt_regs *regs, unsigned long error_code,
661 unsigned long address)
663 struct task_struct *tsk = current;
664 unsigned long *stackend;
668 /* Are we prepared to handle this kernel fault? */
669 if (fixup_exception(regs))
675 * Valid to do another page fault here, because if this fault
676 * had been triggered by is_prefetch fixup_exception would have
681 * Hall of shame of CPU/BIOS bugs.
683 if (is_prefetch(regs, error_code, address))
686 if (is_errata93(regs, address))
690 * Oops. The kernel tried to access some bad page. We'll have to
691 * terminate things with extreme prejudice:
693 flags = oops_begin();
695 show_fault_oops(regs, error_code, address);
697 stackend = end_of_stack(tsk);
698 if (*stackend != STACK_END_MAGIC)
699 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
701 tsk->thread.cr2 = address;
702 tsk->thread.trap_no = 14;
703 tsk->thread.error_code = error_code;
706 if (__die("Oops", regs, error_code))
709 /* Executive summary in case the body of the oops scrolled away */
710 printk(KERN_EMERG "CR2: %016lx\n", address);
712 oops_end(flags, regs, sig);
716 * Print out info about fatal segfaults, if the show_unhandled_signals
720 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
721 unsigned long address, struct task_struct *tsk)
723 if (!unhandled_signal(tsk, SIGSEGV))
726 if (!printk_ratelimit())
729 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
730 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
731 tsk->comm, task_pid_nr(tsk), address,
732 (void *)regs->ip, (void *)regs->sp, error_code);
734 print_vma_addr(KERN_CONT " in ", regs->ip);
736 printk(KERN_CONT "\n");
740 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
741 unsigned long address, int si_code)
743 struct task_struct *tsk = current;
745 /* User mode accesses just cause a SIGSEGV */
746 if (error_code & PF_USER) {
748 * It's possible to have interrupts off here:
753 * Valid to do another page fault here because this one came
756 if (is_prefetch(regs, error_code, address))
759 if (is_errata100(regs, address))
762 if (unlikely(show_unhandled_signals))
763 show_signal_msg(regs, error_code, address, tsk);
765 /* Kernel addresses are always protection faults: */
766 tsk->thread.cr2 = address;
767 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
768 tsk->thread.trap_no = 14;
770 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
775 if (is_f00f_bug(regs, address))
778 no_context(regs, error_code, address);
782 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
783 unsigned long address)
785 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
789 __bad_area(struct pt_regs *regs, unsigned long error_code,
790 unsigned long address, int si_code)
792 struct mm_struct *mm = current->mm;
795 * Something tried to access memory that isn't in our memory map..
796 * Fix it, but check if it's kernel or user first..
798 up_read(&mm->mmap_sem);
800 __bad_area_nosemaphore(regs, error_code, address, si_code);
804 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
806 __bad_area(regs, error_code, address, SEGV_MAPERR);
810 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
811 unsigned long address)
813 __bad_area(regs, error_code, address, SEGV_ACCERR);
816 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
818 out_of_memory(struct pt_regs *regs, unsigned long error_code,
819 unsigned long address)
822 * We ran out of memory, call the OOM killer, and return the userspace
823 * (which will retry the fault, or kill us if we got oom-killed):
825 up_read(¤t->mm->mmap_sem);
827 pagefault_out_of_memory();
831 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
833 struct task_struct *tsk = current;
834 struct mm_struct *mm = tsk->mm;
836 up_read(&mm->mmap_sem);
838 /* Kernel mode? Handle exceptions or die: */
839 if (!(error_code & PF_USER))
840 no_context(regs, error_code, address);
842 /* User-space => ok to do another page fault: */
843 if (is_prefetch(regs, error_code, address))
846 tsk->thread.cr2 = address;
847 tsk->thread.error_code = error_code;
848 tsk->thread.trap_no = 14;
850 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
854 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
855 unsigned long address, unsigned int fault)
857 if (fault & VM_FAULT_OOM) {
858 out_of_memory(regs, error_code, address);
860 if (fault & VM_FAULT_SIGBUS)
861 do_sigbus(regs, error_code, address);
867 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
869 if ((error_code & PF_WRITE) && !pte_write(*pte))
872 if ((error_code & PF_INSTR) && !pte_exec(*pte))
879 * Handle a spurious fault caused by a stale TLB entry.
881 * This allows us to lazily refresh the TLB when increasing the
882 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
883 * eagerly is very expensive since that implies doing a full
884 * cross-processor TLB flush, even if no stale TLB entries exist
885 * on other processors.
887 * There are no security implications to leaving a stale TLB when
888 * increasing the permissions on a page.
891 spurious_fault(unsigned long error_code, unsigned long address)
899 /* Reserved-bit violation or user access to kernel space? */
900 if (error_code & (PF_USER | PF_RSVD))
903 pgd = init_mm.pgd + pgd_index(address);
904 if (!pgd_present(*pgd))
907 pud = pud_offset(pgd, address);
908 if (!pud_present(*pud))
912 return spurious_fault_check(error_code, (pte_t *) pud);
914 pmd = pmd_offset(pud, address);
915 if (!pmd_present(*pmd))
919 return spurious_fault_check(error_code, (pte_t *) pmd);
921 pte = pte_offset_kernel(pmd, address);
922 if (!pte_present(*pte))
925 ret = spurious_fault_check(error_code, pte);
930 * Make sure we have permissions in PMD.
931 * If not, then there's a bug in the page tables:
933 ret = spurious_fault_check(error_code, (pte_t *) pmd);
934 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
939 int show_unhandled_signals = 1;
942 access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
945 /* write, present and write, not present: */
946 if (unlikely(!(vma->vm_flags & VM_WRITE)))
952 if (unlikely(error_code & PF_PROT))
955 /* read, not present: */
956 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
962 static int fault_in_kernel_space(unsigned long address)
964 return address >= TASK_SIZE_MAX;
968 * This routine handles page faults. It determines the address,
969 * and the problem, and then passes it off to one of the appropriate
972 dotraplinkage void __kprobes
973 do_page_fault(struct pt_regs *regs, unsigned long error_code)
975 struct vm_area_struct *vma;
976 struct task_struct *tsk;
977 unsigned long address;
978 struct mm_struct *mm;
985 prefetchw(&mm->mmap_sem);
987 /* Get the faulting address: */
988 address = read_cr2();
990 if (unlikely(kmmio_fault(regs, address)))
994 * We fault-in kernel-space virtual memory on-demand. The
995 * 'reference' page table is init_mm.pgd.
997 * NOTE! We MUST NOT take any locks for this case. We may
998 * be in an interrupt or a critical region, and should
999 * only copy the information from the master page table,
1002 * This verifies that the fault happens in kernel space
1003 * (error_code & 4) == 0, and that the fault was not a
1004 * protection error (error_code & 9) == 0.
1006 if (unlikely(fault_in_kernel_space(address))) {
1007 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
1008 vmalloc_fault(address) >= 0)
1011 /* Can handle a stale RO->RW TLB: */
1012 if (spurious_fault(error_code, address))
1015 /* kprobes don't want to hook the spurious faults: */
1016 if (notify_page_fault(regs))
1019 * Don't take the mm semaphore here. If we fixup a prefetch
1020 * fault we could otherwise deadlock:
1022 bad_area_nosemaphore(regs, error_code, address);
1027 /* kprobes don't want to hook the spurious faults: */
1028 if (unlikely(notify_page_fault(regs)))
1031 * It's safe to allow irq's after cr2 has been saved and the
1032 * vmalloc fault has been handled.
1034 * User-mode registers count as a user access even for any
1035 * potential system fault or CPU buglet:
1037 if (user_mode_vm(regs)) {
1039 error_code |= PF_USER;
1041 if (regs->flags & X86_EFLAGS_IF)
1045 if (unlikely(error_code & PF_RSVD))
1046 pgtable_bad(regs, error_code, address);
1048 perf_swcounter_event(PERF_COUNT_PAGE_FAULTS, 1, 0, regs, address);
1051 * If we're in an interrupt, have no user context or are running
1052 * in an atomic region then we must not take the fault:
1054 if (unlikely(in_atomic() || !mm)) {
1055 bad_area_nosemaphore(regs, error_code, address);
1060 * When running in the kernel we expect faults to occur only to
1061 * addresses in user space. All other faults represent errors in
1062 * the kernel and should generate an OOPS. Unfortunately, in the
1063 * case of an erroneous fault occurring in a code path which already
1064 * holds mmap_sem we will deadlock attempting to validate the fault
1065 * against the address space. Luckily the kernel only validly
1066 * references user space from well defined areas of code, which are
1067 * listed in the exceptions table.
1069 * As the vast majority of faults will be valid we will only perform
1070 * the source reference check when there is a possibility of a
1071 * deadlock. Attempt to lock the address space, if we cannot we then
1072 * validate the source. If this is invalid we can skip the address
1073 * space check, thus avoiding the deadlock:
1075 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1076 if ((error_code & PF_USER) == 0 &&
1077 !search_exception_tables(regs->ip)) {
1078 bad_area_nosemaphore(regs, error_code, address);
1081 down_read(&mm->mmap_sem);
1084 * The above down_read_trylock() might have succeeded in
1085 * which case we'll have missed the might_sleep() from
1091 vma = find_vma(mm, address);
1092 if (unlikely(!vma)) {
1093 bad_area(regs, error_code, address);
1096 if (likely(vma->vm_start <= address))
1098 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1099 bad_area(regs, error_code, address);
1102 if (error_code & PF_USER) {
1104 * Accessing the stack below %sp is always a bug.
1105 * The large cushion allows instructions like enter
1106 * and pusha to work. ("enter $65535, $31" pushes
1107 * 32 pointers and then decrements %sp by 65535.)
1109 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1110 bad_area(regs, error_code, address);
1114 if (unlikely(expand_stack(vma, address))) {
1115 bad_area(regs, error_code, address);
1120 * Ok, we have a good vm_area for this memory access, so
1121 * we can handle it..
1124 write = error_code & PF_WRITE;
1126 if (unlikely(access_error(error_code, write, vma))) {
1127 bad_area_access_error(regs, error_code, address);
1132 * If for any reason at all we couldn't handle the fault,
1133 * make sure we exit gracefully rather than endlessly redo
1136 fault = handle_mm_fault(mm, vma, address, write);
1138 if (unlikely(fault & VM_FAULT_ERROR)) {
1139 mm_fault_error(regs, error_code, address, fault);
1143 if (fault & VM_FAULT_MAJOR) {
1145 perf_swcounter_event(PERF_COUNT_PAGE_FAULTS_MAJ, 1, 0,
1149 perf_swcounter_event(PERF_COUNT_PAGE_FAULTS_MIN, 1, 0,
1153 check_v8086_mode(regs, address, tsk);
1155 up_read(&mm->mmap_sem);