2 * Copyright (C) 1995 Linus Torvalds
3 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
5 #include <linux/interrupt.h>
6 #include <linux/mmiotrace.h>
7 #include <linux/bootmem.h>
8 #include <linux/compiler.h>
9 #include <linux/highmem.h>
10 #include <linux/kprobes.h>
11 #include <linux/uaccess.h>
12 #include <linux/vmalloc.h>
13 #include <linux/vt_kern.h>
14 #include <linux/signal.h>
15 #include <linux/kernel.h>
16 #include <linux/ptrace.h>
17 #include <linux/string.h>
18 #include <linux/module.h>
19 #include <linux/kdebug.h>
20 #include <linux/errno.h>
21 #include <linux/magic.h>
22 #include <linux/sched.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/mman.h>
26 #include <linux/tty.h>
27 #include <linux/smp.h>
30 #include <asm-generic/sections.h>
32 #include <asm/tlbflush.h>
33 #include <asm/pgalloc.h>
34 #include <asm/segment.h>
35 #include <asm/system.h>
36 #include <asm/proto.h>
37 #include <asm/traps.h>
41 * Page fault error code bits:
43 * bit 0 == 0: no page found 1: protection fault
44 * bit 1 == 0: read access 1: write access
45 * bit 2 == 0: kernel-mode access 1: user-mode access
46 * bit 3 == 1: use of reserved bit detected
47 * bit 4 == 1: fault was an instruction fetch
49 enum x86_pf_error_code {
59 * (returns 0 if mmiotrace is disabled)
61 static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
63 if (unlikely(is_kmmio_active()))
64 if (kmmio_handler(regs, addr) == 1)
69 static inline int notify_page_fault(struct pt_regs *regs)
73 /* kprobe_running() needs smp_processor_id() */
74 if (kprobes_built_in() && !user_mode_vm(regs)) {
76 if (kprobe_running() && kprobe_fault_handler(regs, 14))
89 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
90 * Check that here and ignore it.
94 * Sometimes the CPU reports invalid exceptions on prefetch.
95 * Check that here and ignore it.
97 * Opcode checker based on code by Richard Brunner.
100 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
101 unsigned char opcode, int *prefetch)
103 unsigned char instr_hi = opcode & 0xf0;
104 unsigned char instr_lo = opcode & 0x0f;
110 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
111 * In X86_64 long mode, the CPU will signal invalid
112 * opcode if some of these prefixes are present so
113 * X86_64 will never get here anyway
115 return ((instr_lo & 7) == 0x6);
119 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
120 * Need to figure out under what instruction mode the
121 * instruction was issued. Could check the LDT for lm,
122 * but for now it's good enough to assume that long
123 * mode only uses well known segments or kernel.
125 return (!user_mode(regs)) || (regs->cs == __USER_CS);
128 /* 0x64 thru 0x67 are valid prefixes in all modes. */
129 return (instr_lo & 0xC) == 0x4;
131 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
132 return !instr_lo || (instr_lo>>1) == 1;
134 /* Prefetch instruction is 0x0F0D or 0x0F18 */
135 if (probe_kernel_address(instr, opcode))
138 *prefetch = (instr_lo == 0xF) &&
139 (opcode == 0x0D || opcode == 0x18);
147 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
149 unsigned char *max_instr;
150 unsigned char *instr;
154 * If it was a exec (instruction fetch) fault on NX page, then
155 * do not ignore the fault:
157 if (error_code & PF_INSTR)
160 instr = (void *)convert_ip_to_linear(current, regs);
161 max_instr = instr + 15;
163 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
166 while (instr < max_instr) {
167 unsigned char opcode;
169 if (probe_kernel_address(instr, opcode))
174 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
181 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
182 struct task_struct *tsk)
186 info.si_signo = si_signo;
188 info.si_code = si_code;
189 info.si_addr = (void __user *)address;
191 force_sig_info(si_signo, &info, tsk);
194 DEFINE_SPINLOCK(pgd_lock);
198 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
200 unsigned index = pgd_index(address);
206 pgd_k = init_mm.pgd + index;
208 if (!pgd_present(*pgd_k))
212 * set_pgd(pgd, *pgd_k); here would be useless on PAE
213 * and redundant with the set_pmd() on non-PAE. As would
216 pud = pud_offset(pgd, address);
217 pud_k = pud_offset(pgd_k, address);
218 if (!pud_present(*pud_k))
221 pmd = pmd_offset(pud, address);
222 pmd_k = pmd_offset(pud_k, address);
223 if (!pmd_present(*pmd_k))
226 if (!pmd_present(*pmd)) {
227 set_pmd(pmd, *pmd_k);
228 arch_flush_lazy_mmu_mode();
230 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
236 void vmalloc_sync_all(void)
238 unsigned long address;
240 if (SHARED_KERNEL_PMD)
243 for (address = VMALLOC_START & PMD_MASK;
244 address >= TASK_SIZE && address < FIXADDR_TOP;
245 address += PMD_SIZE) {
250 spin_lock_irqsave(&pgd_lock, flags);
251 list_for_each_entry(page, &pgd_list, lru) {
252 if (!vmalloc_sync_one(page_address(page), address))
255 spin_unlock_irqrestore(&pgd_lock, flags);
262 * Handle a fault on the vmalloc or module mapping area
264 static noinline int vmalloc_fault(unsigned long address)
266 unsigned long pgd_paddr;
270 /* Make sure we are in vmalloc area: */
271 if (!(address >= VMALLOC_START && address < VMALLOC_END))
275 * Synchronize this task's top level page-table
276 * with the 'reference' page table.
278 * Do _not_ use "current" here. We might be inside
279 * an interrupt in the middle of a task switch..
281 pgd_paddr = read_cr3();
282 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
286 pte_k = pte_offset_kernel(pmd_k, address);
287 if (!pte_present(*pte_k))
294 * Did it hit the DOS screen memory VA from vm86 mode?
297 check_v8086_mode(struct pt_regs *regs, unsigned long address,
298 struct task_struct *tsk)
302 if (!v8086_mode(regs))
305 bit = (address - 0xA0000) >> PAGE_SHIFT;
307 tsk->thread.screen_bitmap |= 1 << bit;
310 static void dump_pagetable(unsigned long address)
312 __typeof__(pte_val(__pte(0))) page;
315 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
317 #ifdef CONFIG_X86_PAE
318 printk("*pdpt = %016Lx ", page);
319 if ((page >> PAGE_SHIFT) < max_low_pfn
320 && page & _PAGE_PRESENT) {
322 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
323 & (PTRS_PER_PMD - 1)];
324 printk(KERN_CONT "*pde = %016Lx ", page);
328 printk("*pde = %08lx ", page);
332 * We must not directly access the pte in the highpte
333 * case if the page table is located in highmem.
334 * And let's rather not kmap-atomic the pte, just in case
335 * it's allocated already:
337 if ((page >> PAGE_SHIFT) < max_low_pfn
338 && (page & _PAGE_PRESENT)
339 && !(page & _PAGE_PSE)) {
342 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
343 & (PTRS_PER_PTE - 1)];
344 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
350 #else /* CONFIG_X86_64: */
352 void vmalloc_sync_all(void)
354 unsigned long address;
356 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
357 address += PGDIR_SIZE) {
359 const pgd_t *pgd_ref = pgd_offset_k(address);
363 if (pgd_none(*pgd_ref))
366 spin_lock_irqsave(&pgd_lock, flags);
367 list_for_each_entry(page, &pgd_list, lru) {
369 pgd = (pgd_t *)page_address(page) + pgd_index(address);
371 set_pgd(pgd, *pgd_ref);
373 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
375 spin_unlock_irqrestore(&pgd_lock, flags);
382 * Handle a fault on the vmalloc area
384 * This assumes no large pages in there.
386 static noinline int vmalloc_fault(unsigned long address)
388 pgd_t *pgd, *pgd_ref;
389 pud_t *pud, *pud_ref;
390 pmd_t *pmd, *pmd_ref;
391 pte_t *pte, *pte_ref;
393 /* Make sure we are in vmalloc area: */
394 if (!(address >= VMALLOC_START && address < VMALLOC_END))
398 * Copy kernel mappings over when needed. This can also
399 * happen within a race in page table update. In the later
402 pgd = pgd_offset(current->active_mm, address);
403 pgd_ref = pgd_offset_k(address);
404 if (pgd_none(*pgd_ref))
408 set_pgd(pgd, *pgd_ref);
410 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
413 * Below here mismatches are bugs because these lower tables
417 pud = pud_offset(pgd, address);
418 pud_ref = pud_offset(pgd_ref, address);
419 if (pud_none(*pud_ref))
422 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
425 pmd = pmd_offset(pud, address);
426 pmd_ref = pmd_offset(pud_ref, address);
427 if (pmd_none(*pmd_ref))
430 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
433 pte_ref = pte_offset_kernel(pmd_ref, address);
434 if (!pte_present(*pte_ref))
437 pte = pte_offset_kernel(pmd, address);
440 * Don't use pte_page here, because the mappings can point
441 * outside mem_map, and the NUMA hash lookup cannot handle
444 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
450 static const char errata93_warning[] =
451 KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
452 KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
453 KERN_ERR "******* Please consider a BIOS update.\n"
454 KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
457 * No vm86 mode in 64-bit mode:
460 check_v8086_mode(struct pt_regs *regs, unsigned long address,
461 struct task_struct *tsk)
465 static int bad_address(void *p)
469 return probe_kernel_address((unsigned long *)p, dummy);
472 static void dump_pagetable(unsigned long address)
479 pgd = (pgd_t *)read_cr3();
481 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
483 pgd += pgd_index(address);
484 if (bad_address(pgd))
487 printk("PGD %lx ", pgd_val(*pgd));
489 if (!pgd_present(*pgd))
492 pud = pud_offset(pgd, address);
493 if (bad_address(pud))
496 printk("PUD %lx ", pud_val(*pud));
497 if (!pud_present(*pud) || pud_large(*pud))
500 pmd = pmd_offset(pud, address);
501 if (bad_address(pmd))
504 printk("PMD %lx ", pmd_val(*pmd));
505 if (!pmd_present(*pmd) || pmd_large(*pmd))
508 pte = pte_offset_kernel(pmd, address);
509 if (bad_address(pte))
512 printk("PTE %lx", pte_val(*pte));
520 #endif /* CONFIG_X86_64 */
523 * Workaround for K8 erratum #93 & buggy BIOS.
525 * BIOS SMM functions are required to use a specific workaround
526 * to avoid corruption of the 64bit RIP register on C stepping K8.
528 * A lot of BIOS that didn't get tested properly miss this.
530 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
531 * Try to work around it here.
533 * Note we only handle faults in kernel here.
534 * Does nothing on 32-bit.
536 static int is_errata93(struct pt_regs *regs, unsigned long address)
541 if (address != regs->ip)
544 if ((address >> 32) != 0)
547 address |= 0xffffffffUL << 32;
548 if ((address >= (u64)_stext && address <= (u64)_etext) ||
549 (address >= MODULES_VADDR && address <= MODULES_END)) {
551 printk(errata93_warning);
562 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
563 * to illegal addresses >4GB.
565 * We catch this in the page fault handler because these addresses
566 * are not reachable. Just detect this case and return. Any code
567 * segment in LDT is compatibility mode.
569 static int is_errata100(struct pt_regs *regs, unsigned long address)
572 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
578 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
580 #ifdef CONFIG_X86_F00F_BUG
584 * Pentium F0 0F C7 C8 bug workaround:
586 if (boot_cpu_data.f00f_bug) {
587 nr = (address - idt_descr.address) >> 3;
590 do_invalid_op(regs, 0);
599 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
600 unsigned long address)
603 if (!oops_may_print())
607 #ifdef CONFIG_X86_PAE
608 if (error_code & PF_INSTR) {
611 pte_t *pte = lookup_address(address, &level);
613 if (pte && pte_present(*pte) && !pte_exec(*pte)) {
614 printk(KERN_CRIT "kernel tried to execute "
615 "NX-protected page - exploit attempt? "
616 "(uid: %d)\n", current_uid());
621 printk(KERN_ALERT "BUG: unable to handle kernel ");
622 if (address < PAGE_SIZE)
623 printk(KERN_CONT "NULL pointer dereference");
625 printk(KERN_CONT "paging request");
627 printk(KERN_CONT " at %p\n", (void *) address);
628 printk(KERN_ALERT "IP:");
629 printk_address(regs->ip, 1);
631 dump_pagetable(address);
635 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
636 unsigned long address)
638 struct task_struct *tsk;
642 flags = oops_begin();
646 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
648 dump_pagetable(address);
650 tsk->thread.cr2 = address;
651 tsk->thread.trap_no = 14;
652 tsk->thread.error_code = error_code;
654 if (__die("Bad pagetable", regs, error_code))
657 oops_end(flags, regs, sig);
661 no_context(struct pt_regs *regs, unsigned long error_code,
662 unsigned long address)
664 struct task_struct *tsk = current;
665 unsigned long *stackend;
672 /* Are we prepared to handle this kernel fault? */
673 if (fixup_exception(regs))
679 * Valid to do another page fault here, because if this fault
680 * had been triggered by is_prefetch fixup_exception would have
685 * Hall of shame of CPU/BIOS bugs.
687 if (is_prefetch(regs, error_code, address))
690 if (is_errata93(regs, address))
694 * Oops. The kernel tried to access some bad page. We'll have to
695 * terminate things with extreme prejudice:
700 flags = oops_begin();
703 show_fault_oops(regs, error_code, address);
705 stackend = end_of_stack(tsk);
706 if (*stackend != STACK_END_MAGIC)
707 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
709 tsk->thread.cr2 = address;
710 tsk->thread.trap_no = 14;
711 tsk->thread.error_code = error_code;
714 die("Oops", regs, error_code);
719 if (__die("Oops", regs, error_code))
722 /* Executive summary in case the body of the oops scrolled away */
723 printk(KERN_EMERG "CR2: %016lx\n", address);
725 oops_end(flags, regs, sig);
730 * Print out info about fatal segfaults, if the show_unhandled_signals
734 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
735 unsigned long address, struct task_struct *tsk)
737 if (!unhandled_signal(tsk, SIGSEGV))
740 if (!printk_ratelimit())
743 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
744 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
745 tsk->comm, task_pid_nr(tsk), address,
746 (void *)regs->ip, (void *)regs->sp, error_code);
748 print_vma_addr(KERN_CONT " in ", regs->ip);
750 printk(KERN_CONT "\n");
754 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
755 unsigned long address, int si_code)
757 struct task_struct *tsk = current;
759 /* User mode accesses just cause a SIGSEGV */
760 if (error_code & PF_USER) {
762 * It's possible to have interrupts off here:
767 * Valid to do another page fault here because this one came
770 if (is_prefetch(regs, error_code, address))
773 if (is_errata100(regs, address))
776 if (unlikely(show_unhandled_signals))
777 show_signal_msg(regs, error_code, address, tsk);
779 /* Kernel addresses are always protection faults: */
780 tsk->thread.cr2 = address;
781 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
782 tsk->thread.trap_no = 14;
784 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
789 if (is_f00f_bug(regs, address))
792 no_context(regs, error_code, address);
796 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
797 unsigned long address)
799 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
803 __bad_area(struct pt_regs *regs, unsigned long error_code,
804 unsigned long address, int si_code)
806 struct mm_struct *mm = current->mm;
809 * Something tried to access memory that isn't in our memory map..
810 * Fix it, but check if it's kernel or user first..
812 up_read(&mm->mmap_sem);
814 __bad_area_nosemaphore(regs, error_code, address, si_code);
818 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
820 __bad_area(regs, error_code, address, SEGV_MAPERR);
824 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
825 unsigned long address)
827 __bad_area(regs, error_code, address, SEGV_ACCERR);
830 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
832 out_of_memory(struct pt_regs *regs, unsigned long error_code,
833 unsigned long address)
836 * We ran out of memory, call the OOM killer, and return the userspace
837 * (which will retry the fault, or kill us if we got oom-killed):
839 up_read(¤t->mm->mmap_sem);
841 pagefault_out_of_memory();
845 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
847 struct task_struct *tsk = current;
848 struct mm_struct *mm = tsk->mm;
850 up_read(&mm->mmap_sem);
852 /* Kernel mode? Handle exceptions or die: */
853 if (!(error_code & PF_USER))
854 no_context(regs, error_code, address);
857 /* User space => ok to do another page fault: */
858 if (is_prefetch(regs, error_code, address))
862 tsk->thread.cr2 = address;
863 tsk->thread.error_code = error_code;
864 tsk->thread.trap_no = 14;
866 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
870 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
871 unsigned long address, unsigned int fault)
873 if (fault & VM_FAULT_OOM) {
874 out_of_memory(regs, error_code, address);
876 if (fault & VM_FAULT_SIGBUS)
877 do_sigbus(regs, error_code, address);
883 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
885 if ((error_code & PF_WRITE) && !pte_write(*pte))
888 if ((error_code & PF_INSTR) && !pte_exec(*pte))
895 * Handle a spurious fault caused by a stale TLB entry.
897 * This allows us to lazily refresh the TLB when increasing the
898 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
899 * eagerly is very expensive since that implies doing a full
900 * cross-processor TLB flush, even if no stale TLB entries exist
901 * on other processors.
903 * There are no security implications to leaving a stale TLB when
904 * increasing the permissions on a page.
907 spurious_fault(unsigned long error_code, unsigned long address)
915 /* Reserved-bit violation or user access to kernel space? */
916 if (error_code & (PF_USER | PF_RSVD))
919 pgd = init_mm.pgd + pgd_index(address);
920 if (!pgd_present(*pgd))
923 pud = pud_offset(pgd, address);
924 if (!pud_present(*pud))
928 return spurious_fault_check(error_code, (pte_t *) pud);
930 pmd = pmd_offset(pud, address);
931 if (!pmd_present(*pmd))
935 return spurious_fault_check(error_code, (pte_t *) pmd);
937 pte = pte_offset_kernel(pmd, address);
938 if (!pte_present(*pte))
941 ret = spurious_fault_check(error_code, pte);
946 * Make sure we have permissions in PMD.
947 * If not, then there's a bug in the page tables:
949 ret = spurious_fault_check(error_code, (pte_t *) pmd);
950 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
955 int show_unhandled_signals = 1;
958 access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
961 /* write, present and write, not present: */
962 if (unlikely(!(vma->vm_flags & VM_WRITE)))
968 if (unlikely(error_code & PF_PROT))
971 /* read, not present: */
972 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
978 static int fault_in_kernel_space(unsigned long address)
981 return address >= TASK_SIZE;
983 return address >= TASK_SIZE64;
988 * This routine handles page faults. It determines the address,
989 * and the problem, and then passes it off to one of the appropriate
995 void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
997 struct vm_area_struct *vma;
998 struct task_struct *tsk;
999 unsigned long address;
1000 struct mm_struct *mm;
1007 prefetchw(&mm->mmap_sem);
1009 /* Get the faulting address: */
1010 address = read_cr2();
1012 if (unlikely(kmmio_fault(regs, address)))
1016 * We fault-in kernel-space virtual memory on-demand. The
1017 * 'reference' page table is init_mm.pgd.
1019 * NOTE! We MUST NOT take any locks for this case. We may
1020 * be in an interrupt or a critical region, and should
1021 * only copy the information from the master page table,
1024 * This verifies that the fault happens in kernel space
1025 * (error_code & 4) == 0, and that the fault was not a
1026 * protection error (error_code & 9) == 0.
1028 if (unlikely(fault_in_kernel_space(address))) {
1029 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
1030 vmalloc_fault(address) >= 0)
1033 /* Can handle a stale RO->RW TLB: */
1034 if (spurious_fault(error_code, address))
1037 /* kprobes don't want to hook the spurious faults: */
1038 if (notify_page_fault(regs))
1041 * Don't take the mm semaphore here. If we fixup a prefetch
1042 * fault we could otherwise deadlock:
1044 bad_area_nosemaphore(regs, error_code, address);
1049 /* kprobes don't want to hook the spurious faults: */
1050 if (unlikely(notify_page_fault(regs)))
1053 * It's safe to allow irq's after cr2 has been saved and the
1054 * vmalloc fault has been handled.
1056 * User-mode registers count as a user access even for any
1057 * potential system fault or CPU buglet:
1059 if (user_mode_vm(regs)) {
1061 error_code |= PF_USER;
1063 if (regs->flags & X86_EFLAGS_IF)
1067 if (unlikely(error_code & PF_RSVD))
1068 pgtable_bad(regs, error_code, address);
1071 * If we're in an interrupt, have no user context or are running
1072 * in an atomic region then we must not take the fault:
1074 if (unlikely(in_atomic() || !mm)) {
1075 bad_area_nosemaphore(regs, error_code, address);
1080 * When running in the kernel we expect faults to occur only to
1081 * addresses in user space. All other faults represent errors in
1082 * the kernel and should generate an OOPS. Unfortunately, in the
1083 * case of an erroneous fault occurring in a code path which already
1084 * holds mmap_sem we will deadlock attempting to validate the fault
1085 * against the address space. Luckily the kernel only validly
1086 * references user space from well defined areas of code, which are
1087 * listed in the exceptions table.
1089 * As the vast majority of faults will be valid we will only perform
1090 * the source reference check when there is a possibility of a
1091 * deadlock. Attempt to lock the address space, if we cannot we then
1092 * validate the source. If this is invalid we can skip the address
1093 * space check, thus avoiding the deadlock:
1095 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1096 if ((error_code & PF_USER) == 0 &&
1097 !search_exception_tables(regs->ip)) {
1098 bad_area_nosemaphore(regs, error_code, address);
1101 down_read(&mm->mmap_sem);
1104 * The above down_read_trylock() might have succeeded in
1105 * which case we'll have missed the might_sleep() from
1111 vma = find_vma(mm, address);
1112 if (unlikely(!vma)) {
1113 bad_area(regs, error_code, address);
1116 if (likely(vma->vm_start <= address))
1118 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1119 bad_area(regs, error_code, address);
1122 if (error_code & PF_USER) {
1124 * Accessing the stack below %sp is always a bug.
1125 * The large cushion allows instructions like enter
1126 * and pusha to work. ("enter $65535, $31" pushes
1127 * 32 pointers and then decrements %sp by 65535.)
1129 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1130 bad_area(regs, error_code, address);
1134 if (unlikely(expand_stack(vma, address))) {
1135 bad_area(regs, error_code, address);
1140 * Ok, we have a good vm_area for this memory access, so
1141 * we can handle it..
1144 write = error_code & PF_WRITE;
1146 if (unlikely(access_error(error_code, write, vma))) {
1147 bad_area_access_error(regs, error_code, address);
1152 * If for any reason at all we couldn't handle the fault,
1153 * make sure we exit gracefully rather than endlessly redo
1156 fault = handle_mm_fault(mm, vma, address, write);
1158 if (unlikely(fault & VM_FAULT_ERROR)) {
1159 mm_fault_error(regs, error_code, address, fault);
1163 if (fault & VM_FAULT_MAJOR)
1168 check_v8086_mode(regs, address, tsk);
1170 up_read(&mm->mmap_sem);