Merge tag 'ux500-dt-for-v3.13-2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6.git] / arch / x86 / mm / fault.c
blob3aaeffcfd67a6d9ae3b3e89514fed55c31e2ea4d
1 /*
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
5 */
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, ... */
13 #include <linux/perf_event.h> /* perf_sw_event */
14 #include <linux/hugetlb.h> /* hstate_index_to_shift */
15 #include <linux/prefetch.h> /* prefetchw */
16 #include <linux/context_tracking.h> /* exception_enter(), ... */
18 #include <asm/traps.h> /* dotraplinkage, ... */
19 #include <asm/pgalloc.h> /* pgd_*(), ... */
20 #include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
21 #include <asm/fixmap.h> /* VSYSCALL_START */
24 * Page fault error code bits:
26 * bit 0 == 0: no page found 1: protection fault
27 * bit 1 == 0: read access 1: write access
28 * bit 2 == 0: kernel-mode access 1: user-mode access
29 * bit 3 == 1: use of reserved bit detected
30 * bit 4 == 1: fault was an instruction fetch
32 enum x86_pf_error_code {
34 PF_PROT = 1 << 0,
35 PF_WRITE = 1 << 1,
36 PF_USER = 1 << 2,
37 PF_RSVD = 1 << 3,
38 PF_INSTR = 1 << 4,
42 * Returns 0 if mmiotrace is disabled, or if the fault is not
43 * handled by mmiotrace:
45 static inline int __kprobes
46 kmmio_fault(struct pt_regs *regs, unsigned long addr)
48 if (unlikely(is_kmmio_active()))
49 if (kmmio_handler(regs, addr) == 1)
50 return -1;
51 return 0;
54 static inline int __kprobes notify_page_fault(struct pt_regs *regs)
56 int ret = 0;
58 /* kprobe_running() needs smp_processor_id() */
59 if (kprobes_built_in() && !user_mode_vm(regs)) {
60 preempt_disable();
61 if (kprobe_running() && kprobe_fault_handler(regs, 14))
62 ret = 1;
63 preempt_enable();
66 return ret;
70 * Prefetch quirks:
72 * 32-bit mode:
74 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
75 * Check that here and ignore it.
77 * 64-bit mode:
79 * Sometimes the CPU reports invalid exceptions on prefetch.
80 * Check that here and ignore it.
82 * Opcode checker based on code by Richard Brunner.
84 static inline int
85 check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
86 unsigned char opcode, int *prefetch)
88 unsigned char instr_hi = opcode & 0xf0;
89 unsigned char instr_lo = opcode & 0x0f;
91 switch (instr_hi) {
92 case 0x20:
93 case 0x30:
95 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
96 * In X86_64 long mode, the CPU will signal invalid
97 * opcode if some of these prefixes are present so
98 * X86_64 will never get here anyway
100 return ((instr_lo & 7) == 0x6);
101 #ifdef CONFIG_X86_64
102 case 0x40:
104 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
105 * Need to figure out under what instruction mode the
106 * instruction was issued. Could check the LDT for lm,
107 * but for now it's good enough to assume that long
108 * mode only uses well known segments or kernel.
110 return (!user_mode(regs) || user_64bit_mode(regs));
111 #endif
112 case 0x60:
113 /* 0x64 thru 0x67 are valid prefixes in all modes. */
114 return (instr_lo & 0xC) == 0x4;
115 case 0xF0:
116 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
117 return !instr_lo || (instr_lo>>1) == 1;
118 case 0x00:
119 /* Prefetch instruction is 0x0F0D or 0x0F18 */
120 if (probe_kernel_address(instr, opcode))
121 return 0;
123 *prefetch = (instr_lo == 0xF) &&
124 (opcode == 0x0D || opcode == 0x18);
125 return 0;
126 default:
127 return 0;
131 static int
132 is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
134 unsigned char *max_instr;
135 unsigned char *instr;
136 int prefetch = 0;
139 * If it was a exec (instruction fetch) fault on NX page, then
140 * do not ignore the fault:
142 if (error_code & PF_INSTR)
143 return 0;
145 instr = (void *)convert_ip_to_linear(current, regs);
146 max_instr = instr + 15;
148 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
149 return 0;
151 while (instr < max_instr) {
152 unsigned char opcode;
154 if (probe_kernel_address(instr, opcode))
155 break;
157 instr++;
159 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
160 break;
162 return prefetch;
165 static void
166 force_sig_info_fault(int si_signo, int si_code, unsigned long address,
167 struct task_struct *tsk, int fault)
169 unsigned lsb = 0;
170 siginfo_t info;
172 info.si_signo = si_signo;
173 info.si_errno = 0;
174 info.si_code = si_code;
175 info.si_addr = (void __user *)address;
176 if (fault & VM_FAULT_HWPOISON_LARGE)
177 lsb = hstate_index_to_shift(VM_FAULT_GET_HINDEX(fault));
178 if (fault & VM_FAULT_HWPOISON)
179 lsb = PAGE_SHIFT;
180 info.si_addr_lsb = lsb;
182 force_sig_info(si_signo, &info, tsk);
185 DEFINE_SPINLOCK(pgd_lock);
186 LIST_HEAD(pgd_list);
188 #ifdef CONFIG_X86_32
189 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
191 unsigned index = pgd_index(address);
192 pgd_t *pgd_k;
193 pud_t *pud, *pud_k;
194 pmd_t *pmd, *pmd_k;
196 pgd += index;
197 pgd_k = init_mm.pgd + index;
199 if (!pgd_present(*pgd_k))
200 return NULL;
203 * set_pgd(pgd, *pgd_k); here would be useless on PAE
204 * and redundant with the set_pmd() on non-PAE. As would
205 * set_pud.
207 pud = pud_offset(pgd, address);
208 pud_k = pud_offset(pgd_k, address);
209 if (!pud_present(*pud_k))
210 return NULL;
212 pmd = pmd_offset(pud, address);
213 pmd_k = pmd_offset(pud_k, address);
214 if (!pmd_present(*pmd_k))
215 return NULL;
217 if (!pmd_present(*pmd))
218 set_pmd(pmd, *pmd_k);
219 else
220 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
222 return pmd_k;
225 void vmalloc_sync_all(void)
227 unsigned long address;
229 if (SHARED_KERNEL_PMD)
230 return;
232 for (address = VMALLOC_START & PMD_MASK;
233 address >= TASK_SIZE && address < FIXADDR_TOP;
234 address += PMD_SIZE) {
235 struct page *page;
237 spin_lock(&pgd_lock);
238 list_for_each_entry(page, &pgd_list, lru) {
239 spinlock_t *pgt_lock;
240 pmd_t *ret;
242 /* the pgt_lock only for Xen */
243 pgt_lock = &pgd_page_get_mm(page)->page_table_lock;
245 spin_lock(pgt_lock);
246 ret = vmalloc_sync_one(page_address(page), address);
247 spin_unlock(pgt_lock);
249 if (!ret)
250 break;
252 spin_unlock(&pgd_lock);
257 * 32-bit:
259 * Handle a fault on the vmalloc or module mapping area
261 static noinline __kprobes int vmalloc_fault(unsigned long address)
263 unsigned long pgd_paddr;
264 pmd_t *pmd_k;
265 pte_t *pte_k;
267 /* Make sure we are in vmalloc area: */
268 if (!(address >= VMALLOC_START && address < VMALLOC_END))
269 return -1;
271 WARN_ON_ONCE(in_nmi());
274 * Synchronize this task's top level page-table
275 * with the 'reference' page table.
277 * Do _not_ use "current" here. We might be inside
278 * an interrupt in the middle of a task switch..
280 pgd_paddr = read_cr3();
281 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
282 if (!pmd_k)
283 return -1;
285 pte_k = pte_offset_kernel(pmd_k, address);
286 if (!pte_present(*pte_k))
287 return -1;
289 return 0;
293 * Did it hit the DOS screen memory VA from vm86 mode?
295 static inline void
296 check_v8086_mode(struct pt_regs *regs, unsigned long address,
297 struct task_struct *tsk)
299 unsigned long bit;
301 if (!v8086_mode(regs))
302 return;
304 bit = (address - 0xA0000) >> PAGE_SHIFT;
305 if (bit < 32)
306 tsk->thread.screen_bitmap |= 1 << bit;
309 static bool low_pfn(unsigned long pfn)
311 return pfn < max_low_pfn;
314 static void dump_pagetable(unsigned long address)
316 pgd_t *base = __va(read_cr3());
317 pgd_t *pgd = &base[pgd_index(address)];
318 pmd_t *pmd;
319 pte_t *pte;
321 #ifdef CONFIG_X86_PAE
322 printk("*pdpt = %016Lx ", pgd_val(*pgd));
323 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
324 goto out;
325 #endif
326 pmd = pmd_offset(pud_offset(pgd, address), address);
327 printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
330 * We must not directly access the pte in the highpte
331 * case if the page table is located in highmem.
332 * And let's rather not kmap-atomic the pte, just in case
333 * it's allocated already:
335 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
336 goto out;
338 pte = pte_offset_kernel(pmd, address);
339 printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
340 out:
341 printk("\n");
344 #else /* CONFIG_X86_64: */
346 void vmalloc_sync_all(void)
348 sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
352 * 64-bit:
354 * Handle a fault on the vmalloc area
356 * This assumes no large pages in there.
358 static noinline __kprobes int vmalloc_fault(unsigned long address)
360 pgd_t *pgd, *pgd_ref;
361 pud_t *pud, *pud_ref;
362 pmd_t *pmd, *pmd_ref;
363 pte_t *pte, *pte_ref;
365 /* Make sure we are in vmalloc area: */
366 if (!(address >= VMALLOC_START && address < VMALLOC_END))
367 return -1;
369 WARN_ON_ONCE(in_nmi());
372 * Copy kernel mappings over when needed. This can also
373 * happen within a race in page table update. In the later
374 * case just flush:
376 pgd = pgd_offset(current->active_mm, address);
377 pgd_ref = pgd_offset_k(address);
378 if (pgd_none(*pgd_ref))
379 return -1;
381 if (pgd_none(*pgd)) {
382 set_pgd(pgd, *pgd_ref);
383 arch_flush_lazy_mmu_mode();
384 } else {
385 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
389 * Below here mismatches are bugs because these lower tables
390 * are shared:
393 pud = pud_offset(pgd, address);
394 pud_ref = pud_offset(pgd_ref, address);
395 if (pud_none(*pud_ref))
396 return -1;
398 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
399 BUG();
401 pmd = pmd_offset(pud, address);
402 pmd_ref = pmd_offset(pud_ref, address);
403 if (pmd_none(*pmd_ref))
404 return -1;
406 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
407 BUG();
409 pte_ref = pte_offset_kernel(pmd_ref, address);
410 if (!pte_present(*pte_ref))
411 return -1;
413 pte = pte_offset_kernel(pmd, address);
416 * Don't use pte_page here, because the mappings can point
417 * outside mem_map, and the NUMA hash lookup cannot handle
418 * that:
420 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
421 BUG();
423 return 0;
426 #ifdef CONFIG_CPU_SUP_AMD
427 static const char errata93_warning[] =
428 KERN_ERR
429 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
430 "******* Working around it, but it may cause SEGVs or burn power.\n"
431 "******* Please consider a BIOS update.\n"
432 "******* Disabling USB legacy in the BIOS may also help.\n";
433 #endif
436 * No vm86 mode in 64-bit mode:
438 static inline void
439 check_v8086_mode(struct pt_regs *regs, unsigned long address,
440 struct task_struct *tsk)
444 static int bad_address(void *p)
446 unsigned long dummy;
448 return probe_kernel_address((unsigned long *)p, dummy);
451 static void dump_pagetable(unsigned long address)
453 pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
454 pgd_t *pgd = base + pgd_index(address);
455 pud_t *pud;
456 pmd_t *pmd;
457 pte_t *pte;
459 if (bad_address(pgd))
460 goto bad;
462 printk("PGD %lx ", pgd_val(*pgd));
464 if (!pgd_present(*pgd))
465 goto out;
467 pud = pud_offset(pgd, address);
468 if (bad_address(pud))
469 goto bad;
471 printk("PUD %lx ", pud_val(*pud));
472 if (!pud_present(*pud) || pud_large(*pud))
473 goto out;
475 pmd = pmd_offset(pud, address);
476 if (bad_address(pmd))
477 goto bad;
479 printk("PMD %lx ", pmd_val(*pmd));
480 if (!pmd_present(*pmd) || pmd_large(*pmd))
481 goto out;
483 pte = pte_offset_kernel(pmd, address);
484 if (bad_address(pte))
485 goto bad;
487 printk("PTE %lx", pte_val(*pte));
488 out:
489 printk("\n");
490 return;
491 bad:
492 printk("BAD\n");
495 #endif /* CONFIG_X86_64 */
498 * Workaround for K8 erratum #93 & buggy BIOS.
500 * BIOS SMM functions are required to use a specific workaround
501 * to avoid corruption of the 64bit RIP register on C stepping K8.
503 * A lot of BIOS that didn't get tested properly miss this.
505 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
506 * Try to work around it here.
508 * Note we only handle faults in kernel here.
509 * Does nothing on 32-bit.
511 static int is_errata93(struct pt_regs *regs, unsigned long address)
513 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
514 if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD
515 || boot_cpu_data.x86 != 0xf)
516 return 0;
518 if (address != regs->ip)
519 return 0;
521 if ((address >> 32) != 0)
522 return 0;
524 address |= 0xffffffffUL << 32;
525 if ((address >= (u64)_stext && address <= (u64)_etext) ||
526 (address >= MODULES_VADDR && address <= MODULES_END)) {
527 printk_once(errata93_warning);
528 regs->ip = address;
529 return 1;
531 #endif
532 return 0;
536 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
537 * to illegal addresses >4GB.
539 * We catch this in the page fault handler because these addresses
540 * are not reachable. Just detect this case and return. Any code
541 * segment in LDT is compatibility mode.
543 static int is_errata100(struct pt_regs *regs, unsigned long address)
545 #ifdef CONFIG_X86_64
546 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
547 return 1;
548 #endif
549 return 0;
552 static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
554 #ifdef CONFIG_X86_F00F_BUG
555 unsigned long nr;
558 * Pentium F0 0F C7 C8 bug workaround:
560 if (boot_cpu_has_bug(X86_BUG_F00F)) {
561 nr = (address - idt_descr.address) >> 3;
563 if (nr == 6) {
564 do_invalid_op(regs, 0);
565 return 1;
568 #endif
569 return 0;
572 static const char nx_warning[] = KERN_CRIT
573 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
575 static void
576 show_fault_oops(struct pt_regs *regs, unsigned long error_code,
577 unsigned long address)
579 if (!oops_may_print())
580 return;
582 if (error_code & PF_INSTR) {
583 unsigned int level;
585 pte_t *pte = lookup_address(address, &level);
587 if (pte && pte_present(*pte) && !pte_exec(*pte))
588 printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
591 printk(KERN_ALERT "BUG: unable to handle kernel ");
592 if (address < PAGE_SIZE)
593 printk(KERN_CONT "NULL pointer dereference");
594 else
595 printk(KERN_CONT "paging request");
597 printk(KERN_CONT " at %p\n", (void *) address);
598 printk(KERN_ALERT "IP:");
599 printk_address(regs->ip, 1);
601 dump_pagetable(address);
604 static noinline void
605 pgtable_bad(struct pt_regs *regs, unsigned long error_code,
606 unsigned long address)
608 struct task_struct *tsk;
609 unsigned long flags;
610 int sig;
612 flags = oops_begin();
613 tsk = current;
614 sig = SIGKILL;
616 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
617 tsk->comm, address);
618 dump_pagetable(address);
620 tsk->thread.cr2 = address;
621 tsk->thread.trap_nr = X86_TRAP_PF;
622 tsk->thread.error_code = error_code;
624 if (__die("Bad pagetable", regs, error_code))
625 sig = 0;
627 oops_end(flags, regs, sig);
630 static noinline void
631 no_context(struct pt_regs *regs, unsigned long error_code,
632 unsigned long address, int signal, int si_code)
634 struct task_struct *tsk = current;
635 unsigned long *stackend;
636 unsigned long flags;
637 int sig;
639 /* Are we prepared to handle this kernel fault? */
640 if (fixup_exception(regs)) {
641 if (current_thread_info()->sig_on_uaccess_error && signal) {
642 tsk->thread.trap_nr = X86_TRAP_PF;
643 tsk->thread.error_code = error_code | PF_USER;
644 tsk->thread.cr2 = address;
646 /* XXX: hwpoison faults will set the wrong code. */
647 force_sig_info_fault(signal, si_code, address, tsk, 0);
649 return;
653 * 32-bit:
655 * Valid to do another page fault here, because if this fault
656 * had been triggered by is_prefetch fixup_exception would have
657 * handled it.
659 * 64-bit:
661 * Hall of shame of CPU/BIOS bugs.
663 if (is_prefetch(regs, error_code, address))
664 return;
666 if (is_errata93(regs, address))
667 return;
670 * Oops. The kernel tried to access some bad page. We'll have to
671 * terminate things with extreme prejudice:
673 flags = oops_begin();
675 show_fault_oops(regs, error_code, address);
677 stackend = end_of_stack(tsk);
678 if (tsk != &init_task && *stackend != STACK_END_MAGIC)
679 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
681 tsk->thread.cr2 = address;
682 tsk->thread.trap_nr = X86_TRAP_PF;
683 tsk->thread.error_code = error_code;
685 sig = SIGKILL;
686 if (__die("Oops", regs, error_code))
687 sig = 0;
689 /* Executive summary in case the body of the oops scrolled away */
690 printk(KERN_DEFAULT "CR2: %016lx\n", address);
692 oops_end(flags, regs, sig);
696 * Print out info about fatal segfaults, if the show_unhandled_signals
697 * sysctl is set:
699 static inline void
700 show_signal_msg(struct pt_regs *regs, unsigned long error_code,
701 unsigned long address, struct task_struct *tsk)
703 if (!unhandled_signal(tsk, SIGSEGV))
704 return;
706 if (!printk_ratelimit())
707 return;
709 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
710 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
711 tsk->comm, task_pid_nr(tsk), address,
712 (void *)regs->ip, (void *)regs->sp, error_code);
714 print_vma_addr(KERN_CONT " in ", regs->ip);
716 printk(KERN_CONT "\n");
719 static void
720 __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
721 unsigned long address, int si_code)
723 struct task_struct *tsk = current;
725 /* User mode accesses just cause a SIGSEGV */
726 if (error_code & PF_USER) {
728 * It's possible to have interrupts off here:
730 local_irq_enable();
733 * Valid to do another page fault here because this one came
734 * from user space:
736 if (is_prefetch(regs, error_code, address))
737 return;
739 if (is_errata100(regs, address))
740 return;
742 #ifdef CONFIG_X86_64
744 * Instruction fetch faults in the vsyscall page might need
745 * emulation.
747 if (unlikely((error_code & PF_INSTR) &&
748 ((address & ~0xfff) == VSYSCALL_START))) {
749 if (emulate_vsyscall(regs, address))
750 return;
752 #endif
753 /* Kernel addresses are always protection faults: */
754 if (address >= TASK_SIZE)
755 error_code |= PF_PROT;
757 if (likely(show_unhandled_signals))
758 show_signal_msg(regs, error_code, address, tsk);
760 tsk->thread.cr2 = address;
761 tsk->thread.error_code = error_code;
762 tsk->thread.trap_nr = X86_TRAP_PF;
764 force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
766 return;
769 if (is_f00f_bug(regs, address))
770 return;
772 no_context(regs, error_code, address, SIGSEGV, si_code);
775 static noinline void
776 bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
777 unsigned long address)
779 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
782 static void
783 __bad_area(struct pt_regs *regs, unsigned long error_code,
784 unsigned long address, int si_code)
786 struct mm_struct *mm = current->mm;
789 * Something tried to access memory that isn't in our memory map..
790 * Fix it, but check if it's kernel or user first..
792 up_read(&mm->mmap_sem);
794 __bad_area_nosemaphore(regs, error_code, address, si_code);
797 static noinline void
798 bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
800 __bad_area(regs, error_code, address, SEGV_MAPERR);
803 static noinline void
804 bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
805 unsigned long address)
807 __bad_area(regs, error_code, address, SEGV_ACCERR);
810 static void
811 do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
812 unsigned int fault)
814 struct task_struct *tsk = current;
815 struct mm_struct *mm = tsk->mm;
816 int code = BUS_ADRERR;
818 up_read(&mm->mmap_sem);
820 /* Kernel mode? Handle exceptions or die: */
821 if (!(error_code & PF_USER)) {
822 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
823 return;
826 /* User-space => ok to do another page fault: */
827 if (is_prefetch(regs, error_code, address))
828 return;
830 tsk->thread.cr2 = address;
831 tsk->thread.error_code = error_code;
832 tsk->thread.trap_nr = X86_TRAP_PF;
834 #ifdef CONFIG_MEMORY_FAILURE
835 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
836 printk(KERN_ERR
837 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
838 tsk->comm, tsk->pid, address);
839 code = BUS_MCEERR_AR;
841 #endif
842 force_sig_info_fault(SIGBUS, code, address, tsk, fault);
845 static noinline void
846 mm_fault_error(struct pt_regs *regs, unsigned long error_code,
847 unsigned long address, unsigned int fault)
849 if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
850 up_read(&current->mm->mmap_sem);
851 no_context(regs, error_code, address, 0, 0);
852 return;
855 if (fault & VM_FAULT_OOM) {
856 /* Kernel mode? Handle exceptions or die: */
857 if (!(error_code & PF_USER)) {
858 up_read(&current->mm->mmap_sem);
859 no_context(regs, error_code, address,
860 SIGSEGV, SEGV_MAPERR);
861 return;
864 up_read(&current->mm->mmap_sem);
867 * We ran out of memory, call the OOM killer, and return the
868 * userspace (which will retry the fault, or kill us if we got
869 * oom-killed):
871 pagefault_out_of_memory();
872 } else {
873 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
874 VM_FAULT_HWPOISON_LARGE))
875 do_sigbus(regs, error_code, address, fault);
876 else
877 BUG();
881 static int spurious_fault_check(unsigned long error_code, pte_t *pte)
883 if ((error_code & PF_WRITE) && !pte_write(*pte))
884 return 0;
886 if ((error_code & PF_INSTR) && !pte_exec(*pte))
887 return 0;
889 return 1;
893 * Handle a spurious fault caused by a stale TLB entry.
895 * This allows us to lazily refresh the TLB when increasing the
896 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
897 * eagerly is very expensive since that implies doing a full
898 * cross-processor TLB flush, even if no stale TLB entries exist
899 * on other processors.
901 * There are no security implications to leaving a stale TLB when
902 * increasing the permissions on a page.
904 static noinline __kprobes int
905 spurious_fault(unsigned long error_code, unsigned long address)
907 pgd_t *pgd;
908 pud_t *pud;
909 pmd_t *pmd;
910 pte_t *pte;
911 int ret;
913 /* Reserved-bit violation or user access to kernel space? */
914 if (error_code & (PF_USER | PF_RSVD))
915 return 0;
917 pgd = init_mm.pgd + pgd_index(address);
918 if (!pgd_present(*pgd))
919 return 0;
921 pud = pud_offset(pgd, address);
922 if (!pud_present(*pud))
923 return 0;
925 if (pud_large(*pud))
926 return spurious_fault_check(error_code, (pte_t *) pud);
928 pmd = pmd_offset(pud, address);
929 if (!pmd_present(*pmd))
930 return 0;
932 if (pmd_large(*pmd))
933 return spurious_fault_check(error_code, (pte_t *) pmd);
935 pte = pte_offset_kernel(pmd, address);
936 if (!pte_present(*pte))
937 return 0;
939 ret = spurious_fault_check(error_code, pte);
940 if (!ret)
941 return 0;
944 * Make sure we have permissions in PMD.
945 * If not, then there's a bug in the page tables:
947 ret = spurious_fault_check(error_code, (pte_t *) pmd);
948 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
950 return ret;
953 int show_unhandled_signals = 1;
955 static inline int
956 access_error(unsigned long error_code, struct vm_area_struct *vma)
958 if (error_code & PF_WRITE) {
959 /* write, present and write, not present: */
960 if (unlikely(!(vma->vm_flags & VM_WRITE)))
961 return 1;
962 return 0;
965 /* read, present: */
966 if (unlikely(error_code & PF_PROT))
967 return 1;
969 /* read, not present: */
970 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
971 return 1;
973 return 0;
976 static int fault_in_kernel_space(unsigned long address)
978 return address >= TASK_SIZE_MAX;
981 static inline bool smap_violation(int error_code, struct pt_regs *regs)
983 if (error_code & PF_USER)
984 return false;
986 if (!user_mode_vm(regs) && (regs->flags & X86_EFLAGS_AC))
987 return false;
989 return true;
993 * This routine handles page faults. It determines the address,
994 * and the problem, and then passes it off to one of the appropriate
995 * routines.
997 static void __kprobes
998 __do_page_fault(struct pt_regs *regs, unsigned long error_code)
1000 struct vm_area_struct *vma;
1001 struct task_struct *tsk;
1002 unsigned long address;
1003 struct mm_struct *mm;
1004 int fault;
1005 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
1007 tsk = current;
1008 mm = tsk->mm;
1010 /* Get the faulting address: */
1011 address = read_cr2();
1014 * Detect and handle instructions that would cause a page fault for
1015 * both a tracked kernel page and a userspace page.
1017 if (kmemcheck_active(regs))
1018 kmemcheck_hide(regs);
1019 prefetchw(&mm->mmap_sem);
1021 if (unlikely(kmmio_fault(regs, address)))
1022 return;
1025 * We fault-in kernel-space virtual memory on-demand. The
1026 * 'reference' page table is init_mm.pgd.
1028 * NOTE! We MUST NOT take any locks for this case. We may
1029 * be in an interrupt or a critical region, and should
1030 * only copy the information from the master page table,
1031 * nothing more.
1033 * This verifies that the fault happens in kernel space
1034 * (error_code & 4) == 0, and that the fault was not a
1035 * protection error (error_code & 9) == 0.
1037 if (unlikely(fault_in_kernel_space(address))) {
1038 if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1039 if (vmalloc_fault(address) >= 0)
1040 return;
1042 if (kmemcheck_fault(regs, address, error_code))
1043 return;
1046 /* Can handle a stale RO->RW TLB: */
1047 if (spurious_fault(error_code, address))
1048 return;
1050 /* kprobes don't want to hook the spurious faults: */
1051 if (notify_page_fault(regs))
1052 return;
1054 * Don't take the mm semaphore here. If we fixup a prefetch
1055 * fault we could otherwise deadlock:
1057 bad_area_nosemaphore(regs, error_code, address);
1059 return;
1062 /* kprobes don't want to hook the spurious faults: */
1063 if (unlikely(notify_page_fault(regs)))
1064 return;
1066 * It's safe to allow irq's after cr2 has been saved and the
1067 * vmalloc fault has been handled.
1069 * User-mode registers count as a user access even for any
1070 * potential system fault or CPU buglet:
1072 if (user_mode_vm(regs)) {
1073 local_irq_enable();
1074 error_code |= PF_USER;
1075 flags |= FAULT_FLAG_USER;
1076 } else {
1077 if (regs->flags & X86_EFLAGS_IF)
1078 local_irq_enable();
1081 if (unlikely(error_code & PF_RSVD))
1082 pgtable_bad(regs, error_code, address);
1084 if (static_cpu_has(X86_FEATURE_SMAP)) {
1085 if (unlikely(smap_violation(error_code, regs))) {
1086 bad_area_nosemaphore(regs, error_code, address);
1087 return;
1091 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
1094 * If we're in an interrupt, have no user context or are running
1095 * in an atomic region then we must not take the fault:
1097 if (unlikely(in_atomic() || !mm)) {
1098 bad_area_nosemaphore(regs, error_code, address);
1099 return;
1102 if (error_code & PF_WRITE)
1103 flags |= FAULT_FLAG_WRITE;
1106 * When running in the kernel we expect faults to occur only to
1107 * addresses in user space. All other faults represent errors in
1108 * the kernel and should generate an OOPS. Unfortunately, in the
1109 * case of an erroneous fault occurring in a code path which already
1110 * holds mmap_sem we will deadlock attempting to validate the fault
1111 * against the address space. Luckily the kernel only validly
1112 * references user space from well defined areas of code, which are
1113 * listed in the exceptions table.
1115 * As the vast majority of faults will be valid we will only perform
1116 * the source reference check when there is a possibility of a
1117 * deadlock. Attempt to lock the address space, if we cannot we then
1118 * validate the source. If this is invalid we can skip the address
1119 * space check, thus avoiding the deadlock:
1121 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
1122 if ((error_code & PF_USER) == 0 &&
1123 !search_exception_tables(regs->ip)) {
1124 bad_area_nosemaphore(regs, error_code, address);
1125 return;
1127 retry:
1128 down_read(&mm->mmap_sem);
1129 } else {
1131 * The above down_read_trylock() might have succeeded in
1132 * which case we'll have missed the might_sleep() from
1133 * down_read():
1135 might_sleep();
1138 vma = find_vma(mm, address);
1139 if (unlikely(!vma)) {
1140 bad_area(regs, error_code, address);
1141 return;
1143 if (likely(vma->vm_start <= address))
1144 goto good_area;
1145 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1146 bad_area(regs, error_code, address);
1147 return;
1149 if (error_code & PF_USER) {
1151 * Accessing the stack below %sp is always a bug.
1152 * The large cushion allows instructions like enter
1153 * and pusha to work. ("enter $65535, $31" pushes
1154 * 32 pointers and then decrements %sp by 65535.)
1156 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1157 bad_area(regs, error_code, address);
1158 return;
1161 if (unlikely(expand_stack(vma, address))) {
1162 bad_area(regs, error_code, address);
1163 return;
1167 * Ok, we have a good vm_area for this memory access, so
1168 * we can handle it..
1170 good_area:
1171 if (unlikely(access_error(error_code, vma))) {
1172 bad_area_access_error(regs, error_code, address);
1173 return;
1177 * If for any reason at all we couldn't handle the fault,
1178 * make sure we exit gracefully rather than endlessly redo
1179 * the fault:
1181 fault = handle_mm_fault(mm, vma, address, flags);
1184 * If we need to retry but a fatal signal is pending, handle the
1185 * signal first. We do not need to release the mmap_sem because it
1186 * would already be released in __lock_page_or_retry in mm/filemap.c.
1188 if (unlikely((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)))
1189 return;
1191 if (unlikely(fault & VM_FAULT_ERROR)) {
1192 mm_fault_error(regs, error_code, address, fault);
1193 return;
1197 * Major/minor page fault accounting is only done on the
1198 * initial attempt. If we go through a retry, it is extremely
1199 * likely that the page will be found in page cache at that point.
1201 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1202 if (fault & VM_FAULT_MAJOR) {
1203 tsk->maj_flt++;
1204 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
1205 regs, address);
1206 } else {
1207 tsk->min_flt++;
1208 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
1209 regs, address);
1211 if (fault & VM_FAULT_RETRY) {
1212 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1213 * of starvation. */
1214 flags &= ~FAULT_FLAG_ALLOW_RETRY;
1215 flags |= FAULT_FLAG_TRIED;
1216 goto retry;
1220 check_v8086_mode(regs, address, tsk);
1222 up_read(&mm->mmap_sem);
1225 dotraplinkage void __kprobes
1226 do_page_fault(struct pt_regs *regs, unsigned long error_code)
1228 enum ctx_state prev_state;
1230 prev_state = exception_enter();
1231 __do_page_fault(regs, error_code);
1232 exception_exit(prev_state);