| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | *  Copyright (C) 1995  Linus Torvalds | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 3 | *  Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs. | 
| Ingo Molnar | f8eeb2e | 2009-02-20 23:13:36 +0100 | [diff] [blame] | 4 | *  Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ | 
| Ingo Molnar | a2bcd47 | 2009-03-29 23:47:48 +0200 | [diff] [blame] | 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, ...		*/ | 
| Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 13 | #include <linux/perf_event.h>		/* perf_sw_event		*/ | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 14 | #include <linux/hugetlb.h>		/* hstate_index_to_shift	*/ | 
| Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 15 | #include <linux/prefetch.h>		/* prefetchw			*/ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 |  | 
| Ingo Molnar | a2bcd47 | 2009-03-29 23:47:48 +0200 | [diff] [blame] | 17 | #include <asm/traps.h>			/* dotraplinkage, ...		*/ | 
|  | 18 | #include <asm/pgalloc.h>		/* pgd_*(), ...			*/ | 
| Vegard Nossum | f856129 | 2008-04-04 00:53:23 +0200 | [diff] [blame] | 19 | #include <asm/kmemcheck.h>		/* kmemcheck_*(), ...		*/ | 
| H. Peter Anvin | fab1167 | 2011-08-15 22:28:56 -0700 | [diff] [blame] | 20 | #include <asm/fixmap.h>			/* VSYSCALL_START		*/ | 
| Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 21 | #include <asm/context_tracking.h>	/* exception_enter(), ...	*/ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 |  | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 23 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 24 | * Page fault error code bits: | 
|  | 25 | * | 
|  | 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 | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 31 | */ | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 32 | enum x86_pf_error_code { | 
|  | 33 |  | 
|  | 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, | 
|  | 39 | }; | 
| Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 40 |  | 
| Ingo Molnar | b814d41 | 2009-02-20 22:32:10 +0100 | [diff] [blame] | 41 | /* | 
| Ingo Molnar | b319eed | 2009-02-22 10:24:18 +0100 | [diff] [blame] | 42 | * Returns 0 if mmiotrace is disabled, or if the fault is not | 
|  | 43 | * handled by mmiotrace: | 
| Ingo Molnar | b814d41 | 2009-02-20 22:32:10 +0100 | [diff] [blame] | 44 | */ | 
| Masami Hiramatsu | 62c9295 | 2009-08-27 13:23:11 -0400 | [diff] [blame] | 45 | static inline int __kprobes | 
|  | 46 | kmmio_fault(struct pt_regs *regs, unsigned long addr) | 
| Pekka Paalanen | 8606978 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 47 | { | 
| Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 48 | if (unlikely(is_kmmio_active())) | 
|  | 49 | if (kmmio_handler(regs, addr) == 1) | 
|  | 50 | return -1; | 
| Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 51 | return 0; | 
| Pekka Paalanen | 8606978 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 52 | } | 
|  | 53 |  | 
| Masami Hiramatsu | 62c9295 | 2009-08-27 13:23:11 -0400 | [diff] [blame] | 54 | static inline int __kprobes notify_page_fault(struct pt_regs *regs) | 
| Anil S Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 55 | { | 
| Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 56 | int ret = 0; | 
| Anil S Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 57 |  | 
| Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 58 | /* kprobe_running() needs smp_processor_id() */ | 
| Ingo Molnar | b180181 | 2009-02-20 22:42:57 +0100 | [diff] [blame] | 59 | if (kprobes_built_in() && !user_mode_vm(regs)) { | 
| Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 60 | preempt_disable(); | 
|  | 61 | if (kprobe_running() && kprobe_fault_handler(regs, 14)) | 
|  | 62 | ret = 1; | 
|  | 63 | preempt_enable(); | 
|  | 64 | } | 
| Anil S Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 65 |  | 
| Christoph Hellwig | 74a0b57 | 2007-10-16 01:24:07 -0700 | [diff] [blame] | 66 | return ret; | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 67 | } | 
| Anil S Keshavamurthy | 1bd858a | 2006-06-26 00:25:25 -0700 | [diff] [blame] | 68 |  | 
| Harvey Harrison | 1dc85be | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 69 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 70 | * Prefetch quirks: | 
| Harvey Harrison | 1dc85be | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 71 | * | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 72 | * 32-bit mode: | 
| Harvey Harrison | 1dc85be | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 73 | * | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 74 | *   Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. | 
|  | 75 | *   Check that here and ignore it. | 
|  | 76 | * | 
|  | 77 | * 64-bit mode: | 
|  | 78 | * | 
|  | 79 | *   Sometimes the CPU reports invalid exceptions on prefetch. | 
|  | 80 | *   Check that here and ignore it. | 
|  | 81 | * | 
|  | 82 | * Opcode checker based on code by Richard Brunner. | 
| Harvey Harrison | 1dc85be | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 83 | */ | 
| Ingo Molnar | 107a036 | 2009-02-20 20:37:05 +0100 | [diff] [blame] | 84 | static inline int | 
|  | 85 | check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr, | 
|  | 86 | unsigned char opcode, int *prefetch) | 
|  | 87 | { | 
|  | 88 | unsigned char instr_hi = opcode & 0xf0; | 
|  | 89 | unsigned char instr_lo = opcode & 0x0f; | 
|  | 90 |  | 
|  | 91 | switch (instr_hi) { | 
|  | 92 | case 0x20: | 
|  | 93 | case 0x30: | 
|  | 94 | /* | 
|  | 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 | 
|  | 99 | */ | 
|  | 100 | return ((instr_lo & 7) == 0x6); | 
|  | 101 | #ifdef CONFIG_X86_64 | 
|  | 102 | case 0x40: | 
|  | 103 | /* | 
|  | 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. | 
|  | 109 | */ | 
| Andy Lutomirski | 318f5a2 | 2011-08-03 09:31:53 -0400 | [diff] [blame] | 110 | return (!user_mode(regs) || user_64bit_mode(regs)); | 
| Ingo Molnar | 107a036 | 2009-02-20 20:37:05 +0100 | [diff] [blame] | 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; | 
|  | 122 |  | 
|  | 123 | *prefetch = (instr_lo == 0xF) && | 
|  | 124 | (opcode == 0x0D || opcode == 0x18); | 
|  | 125 | return 0; | 
|  | 126 | default: | 
|  | 127 | return 0; | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 131 | static int | 
|  | 132 | is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr) | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 133 | { | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 134 | unsigned char *max_instr; | 
| Andi Kleen | ab2bf0c | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 135 | unsigned char *instr; | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 136 | int prefetch = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 |  | 
| Ingo Molnar | 3085354 | 2008-03-27 21:29:09 +0100 | [diff] [blame] | 138 | /* | 
|  | 139 | * If it was a exec (instruction fetch) fault on NX page, then | 
|  | 140 | * do not ignore the fault: | 
|  | 141 | */ | 
| Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 142 | if (error_code & PF_INSTR) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return 0; | 
| Harvey Harrison | 1dc85be | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 144 |  | 
| Ingo Molnar | 107a036 | 2009-02-20 20:37:05 +0100 | [diff] [blame] | 145 | instr = (void *)convert_ip_to_linear(current, regs); | 
| Andi Kleen | f1290ec | 2005-04-16 15:24:59 -0700 | [diff] [blame] | 146 | max_instr = instr + 15; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 |  | 
| Vincent Hanquez | 76381fe | 2005-06-23 00:08:46 -0700 | [diff] [blame] | 148 | if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | return 0; | 
|  | 150 |  | 
| Ingo Molnar | 107a036 | 2009-02-20 20:37:05 +0100 | [diff] [blame] | 151 | while (instr < max_instr) { | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 152 | unsigned char opcode; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 |  | 
| Andi Kleen | ab2bf0c | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 154 | if (probe_kernel_address(instr, opcode)) | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 155 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | instr++; | 
|  | 158 |  | 
| Ingo Molnar | 107a036 | 2009-02-20 20:37:05 +0100 | [diff] [blame] | 159 | if (!check_prefetch_opcode(regs, instr, opcode, &prefetch)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } | 
|  | 162 | return prefetch; | 
|  | 163 | } | 
|  | 164 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 165 | static void | 
|  | 166 | force_sig_info_fault(int si_signo, int si_code, unsigned long address, | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 167 | struct task_struct *tsk, int fault) | 
| Harvey Harrison | c4aba4a | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 168 | { | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 169 | unsigned lsb = 0; | 
| Harvey Harrison | c4aba4a | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 170 | siginfo_t info; | 
|  | 171 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 172 | info.si_signo	= si_signo; | 
|  | 173 | info.si_errno	= 0; | 
|  | 174 | info.si_code	= si_code; | 
|  | 175 | info.si_addr	= (void __user *)address; | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 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; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 181 |  | 
| Harvey Harrison | c4aba4a | 2008-01-30 13:32:35 +0100 | [diff] [blame] | 182 | force_sig_info(si_signo, &info, tsk); | 
|  | 183 | } | 
|  | 184 |  | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 185 | DEFINE_SPINLOCK(pgd_lock); | 
|  | 186 | LIST_HEAD(pgd_list); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 187 |  | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 188 | #ifdef CONFIG_X86_32 | 
|  | 189 | static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) | 
|  | 190 | { | 
|  | 191 | unsigned index = pgd_index(address); | 
|  | 192 | pgd_t *pgd_k; | 
|  | 193 | pud_t *pud, *pud_k; | 
|  | 194 | pmd_t *pmd, *pmd_k; | 
|  | 195 |  | 
|  | 196 | pgd += index; | 
|  | 197 | pgd_k = init_mm.pgd + index; | 
|  | 198 |  | 
|  | 199 | if (!pgd_present(*pgd_k)) | 
|  | 200 | return NULL; | 
|  | 201 |  | 
|  | 202 | /* | 
|  | 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. | 
|  | 206 | */ | 
|  | 207 | pud = pud_offset(pgd, address); | 
|  | 208 | pud_k = pud_offset(pgd_k, address); | 
|  | 209 | if (!pud_present(*pud_k)) | 
|  | 210 | return NULL; | 
|  | 211 |  | 
|  | 212 | pmd = pmd_offset(pud, address); | 
|  | 213 | pmd_k = pmd_offset(pud_k, address); | 
|  | 214 | if (!pmd_present(*pmd_k)) | 
|  | 215 | return NULL; | 
|  | 216 |  | 
| Jeremy Fitzhardinge | b8bcfe9 | 2009-02-17 23:05:19 -0800 | [diff] [blame] | 217 | if (!pmd_present(*pmd)) | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 218 | set_pmd(pmd, *pmd_k); | 
| Jeremy Fitzhardinge | b8bcfe9 | 2009-02-17 23:05:19 -0800 | [diff] [blame] | 219 | else | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 220 | BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 221 |  | 
|  | 222 | return pmd_k; | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 223 | } | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 224 |  | 
|  | 225 | void vmalloc_sync_all(void) | 
|  | 226 | { | 
|  | 227 | unsigned long address; | 
|  | 228 |  | 
|  | 229 | if (SHARED_KERNEL_PMD) | 
|  | 230 | return; | 
|  | 231 |  | 
|  | 232 | for (address = VMALLOC_START & PMD_MASK; | 
|  | 233 | address >= TASK_SIZE && address < FIXADDR_TOP; | 
|  | 234 | address += PMD_SIZE) { | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 235 | struct page *page; | 
|  | 236 |  | 
| Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 237 | spin_lock(&pgd_lock); | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 238 | list_for_each_entry(page, &pgd_list, lru) { | 
| Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 239 | spinlock_t *pgt_lock; | 
| Borislav Petkov | f01f7c5 | 2010-10-19 22:17:37 +0000 | [diff] [blame] | 240 | pmd_t *ret; | 
| Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 241 |  | 
| Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 242 | /* the pgt_lock only for Xen */ | 
| Jeremy Fitzhardinge | 617d34d | 2010-09-21 12:01:51 -0700 | [diff] [blame] | 243 | pgt_lock = &pgd_page_get_mm(page)->page_table_lock; | 
|  | 244 |  | 
|  | 245 | spin_lock(pgt_lock); | 
|  | 246 | ret = vmalloc_sync_one(page_address(page), address); | 
|  | 247 | spin_unlock(pgt_lock); | 
|  | 248 |  | 
|  | 249 | if (!ret) | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 250 | break; | 
|  | 251 | } | 
| Andrea Arcangeli | a79e53d | 2011-02-16 15:45:22 -0800 | [diff] [blame] | 252 | spin_unlock(&pgd_lock); | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 253 | } | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | /* | 
|  | 257 | * 32-bit: | 
|  | 258 | * | 
|  | 259 | *   Handle a fault on the vmalloc or module mapping area | 
|  | 260 | */ | 
| Masami Hiramatsu | 62c9295 | 2009-08-27 13:23:11 -0400 | [diff] [blame] | 261 | static noinline __kprobes int vmalloc_fault(unsigned long address) | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 262 | { | 
|  | 263 | unsigned long pgd_paddr; | 
|  | 264 | pmd_t *pmd_k; | 
|  | 265 | pte_t *pte_k; | 
|  | 266 |  | 
|  | 267 | /* Make sure we are in vmalloc area: */ | 
|  | 268 | if (!(address >= VMALLOC_START && address < VMALLOC_END)) | 
|  | 269 | return -1; | 
|  | 270 |  | 
| Frederic Weisbecker | ebc8827 | 2010-09-27 18:50:51 +0200 | [diff] [blame] | 271 | WARN_ON_ONCE(in_nmi()); | 
|  | 272 |  | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 273 | /* | 
|  | 274 | * Synchronize this task's top level page-table | 
|  | 275 | * with the 'reference' page table. | 
|  | 276 | * | 
|  | 277 | * Do _not_ use "current" here. We might be inside | 
|  | 278 | * an interrupt in the middle of a task switch.. | 
|  | 279 | */ | 
|  | 280 | pgd_paddr = read_cr3(); | 
|  | 281 | pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); | 
|  | 282 | if (!pmd_k) | 
|  | 283 | return -1; | 
|  | 284 |  | 
|  | 285 | pte_k = pte_offset_kernel(pmd_k, address); | 
|  | 286 | if (!pte_present(*pte_k)) | 
|  | 287 | return -1; | 
|  | 288 |  | 
|  | 289 | return 0; | 
|  | 290 | } | 
|  | 291 |  | 
|  | 292 | /* | 
|  | 293 | * Did it hit the DOS screen memory VA from vm86 mode? | 
|  | 294 | */ | 
|  | 295 | static inline void | 
|  | 296 | check_v8086_mode(struct pt_regs *regs, unsigned long address, | 
|  | 297 | struct task_struct *tsk) | 
|  | 298 | { | 
|  | 299 | unsigned long bit; | 
|  | 300 |  | 
|  | 301 | if (!v8086_mode(regs)) | 
|  | 302 | return; | 
|  | 303 |  | 
|  | 304 | bit = (address - 0xA0000) >> PAGE_SHIFT; | 
|  | 305 | if (bit < 32) | 
|  | 306 | tsk->thread.screen_bitmap |= 1 << bit; | 
|  | 307 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 |  | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 309 | static bool low_pfn(unsigned long pfn) | 
|  | 310 | { | 
|  | 311 | return pfn < max_low_pfn; | 
|  | 312 | } | 
|  | 313 |  | 
| Adrian Bunk | cae30f8 | 2008-02-13 23:31:31 +0200 | [diff] [blame] | 314 | static void dump_pagetable(unsigned long address) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | { | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 316 | pgd_t *base = __va(read_cr3()); | 
|  | 317 | pgd_t *pgd = &base[pgd_index(address)]; | 
|  | 318 | pmd_t *pmd; | 
|  | 319 | pte_t *pte; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 320 |  | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 321 | #ifdef CONFIG_X86_PAE | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 322 | printk("*pdpt = %016Lx ", pgd_val(*pgd)); | 
|  | 323 | if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd)) | 
|  | 324 | goto out; | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 325 | #endif | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 326 | pmd = pmd_offset(pud_offset(pgd, address), address); | 
|  | 327 | printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd)); | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 328 |  | 
|  | 329 | /* | 
|  | 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 | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 333 | * it's allocated already: | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 334 | */ | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 335 | if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd)) | 
|  | 336 | goto out; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 337 |  | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 338 | pte = pte_offset_kernel(pmd, address); | 
|  | 339 | printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte)); | 
|  | 340 | out: | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 341 | printk("\n"); | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 342 | } | 
|  | 343 |  | 
|  | 344 | #else /* CONFIG_X86_64: */ | 
|  | 345 |  | 
|  | 346 | void vmalloc_sync_all(void) | 
|  | 347 | { | 
| Haicheng Li | 6afb515 | 2010-05-19 17:42:14 +0800 | [diff] [blame] | 348 | sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END); | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
|  | 351 | /* | 
|  | 352 | * 64-bit: | 
|  | 353 | * | 
|  | 354 | *   Handle a fault on the vmalloc area | 
|  | 355 | * | 
|  | 356 | * This assumes no large pages in there. | 
|  | 357 | */ | 
| Masami Hiramatsu | 62c9295 | 2009-08-27 13:23:11 -0400 | [diff] [blame] | 358 | static noinline __kprobes int vmalloc_fault(unsigned long address) | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 359 | { | 
|  | 360 | pgd_t *pgd, *pgd_ref; | 
|  | 361 | pud_t *pud, *pud_ref; | 
|  | 362 | pmd_t *pmd, *pmd_ref; | 
|  | 363 | pte_t *pte, *pte_ref; | 
|  | 364 |  | 
|  | 365 | /* Make sure we are in vmalloc area: */ | 
|  | 366 | if (!(address >= VMALLOC_START && address < VMALLOC_END)) | 
|  | 367 | return -1; | 
|  | 368 |  | 
| Frederic Weisbecker | ebc8827 | 2010-09-27 18:50:51 +0200 | [diff] [blame] | 369 | WARN_ON_ONCE(in_nmi()); | 
|  | 370 |  | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 371 | /* | 
|  | 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: | 
|  | 375 | */ | 
|  | 376 | pgd = pgd_offset(current->active_mm, address); | 
|  | 377 | pgd_ref = pgd_offset_k(address); | 
|  | 378 | if (pgd_none(*pgd_ref)) | 
|  | 379 | return -1; | 
|  | 380 |  | 
|  | 381 | if (pgd_none(*pgd)) | 
|  | 382 | set_pgd(pgd, *pgd_ref); | 
|  | 383 | else | 
|  | 384 | BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref)); | 
|  | 385 |  | 
|  | 386 | /* | 
|  | 387 | * Below here mismatches are bugs because these lower tables | 
|  | 388 | * are shared: | 
|  | 389 | */ | 
|  | 390 |  | 
|  | 391 | pud = pud_offset(pgd, address); | 
|  | 392 | pud_ref = pud_offset(pgd_ref, address); | 
|  | 393 | if (pud_none(*pud_ref)) | 
|  | 394 | return -1; | 
|  | 395 |  | 
|  | 396 | if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref)) | 
|  | 397 | BUG(); | 
|  | 398 |  | 
|  | 399 | pmd = pmd_offset(pud, address); | 
|  | 400 | pmd_ref = pmd_offset(pud_ref, address); | 
|  | 401 | if (pmd_none(*pmd_ref)) | 
|  | 402 | return -1; | 
|  | 403 |  | 
|  | 404 | if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref)) | 
|  | 405 | BUG(); | 
|  | 406 |  | 
|  | 407 | pte_ref = pte_offset_kernel(pmd_ref, address); | 
|  | 408 | if (!pte_present(*pte_ref)) | 
|  | 409 | return -1; | 
|  | 410 |  | 
|  | 411 | pte = pte_offset_kernel(pmd, address); | 
|  | 412 |  | 
|  | 413 | /* | 
|  | 414 | * Don't use pte_page here, because the mappings can point | 
|  | 415 | * outside mem_map, and the NUMA hash lookup cannot handle | 
|  | 416 | * that: | 
|  | 417 | */ | 
|  | 418 | if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref)) | 
|  | 419 | BUG(); | 
|  | 420 |  | 
|  | 421 | return 0; | 
|  | 422 | } | 
|  | 423 |  | 
| Jan Beulich | e05139f | 2011-09-28 16:56:51 +0100 | [diff] [blame] | 424 | #ifdef CONFIG_CPU_SUP_AMD | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 425 | static const char errata93_warning[] = | 
| Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 426 | KERN_ERR | 
|  | 427 | "******* Your BIOS seems to not contain a fix for K8 errata #93\n" | 
|  | 428 | "******* Working around it, but it may cause SEGVs or burn power.\n" | 
|  | 429 | "******* Please consider a BIOS update.\n" | 
|  | 430 | "******* Disabling USB legacy in the BIOS may also help.\n"; | 
| Jan Beulich | e05139f | 2011-09-28 16:56:51 +0100 | [diff] [blame] | 431 | #endif | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 432 |  | 
|  | 433 | /* | 
|  | 434 | * No vm86 mode in 64-bit mode: | 
|  | 435 | */ | 
|  | 436 | static inline void | 
|  | 437 | check_v8086_mode(struct pt_regs *regs, unsigned long address, | 
|  | 438 | struct task_struct *tsk) | 
|  | 439 | { | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | static int bad_address(void *p) | 
|  | 443 | { | 
|  | 444 | unsigned long dummy; | 
|  | 445 |  | 
|  | 446 | return probe_kernel_address((unsigned long *)p, dummy); | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | static void dump_pagetable(unsigned long address) | 
|  | 450 | { | 
| Akinobu Mita | 087975b | 2009-06-27 15:35:15 +0900 | [diff] [blame] | 451 | pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK); | 
|  | 452 | pgd_t *pgd = base + pgd_index(address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | pud_t *pud; | 
|  | 454 | pmd_t *pmd; | 
|  | 455 | pte_t *pte; | 
|  | 456 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 457 | if (bad_address(pgd)) | 
|  | 458 | goto bad; | 
|  | 459 |  | 
| Jan Beulich | d646bce | 2006-02-03 21:51:47 +0100 | [diff] [blame] | 460 | printk("PGD %lx ", pgd_val(*pgd)); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 461 |  | 
|  | 462 | if (!pgd_present(*pgd)) | 
|  | 463 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 |  | 
| Andi Kleen | d2ae5b5 | 2006-06-26 13:57:56 +0200 | [diff] [blame] | 465 | pud = pud_offset(pgd, address); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 466 | if (bad_address(pud)) | 
|  | 467 | goto bad; | 
|  | 468 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | printk("PUD %lx ", pud_val(*pud)); | 
| Andi Kleen | b536022 | 2008-02-04 16:48:09 +0100 | [diff] [blame] | 470 | if (!pud_present(*pud) || pud_large(*pud)) | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 471 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 |  | 
|  | 473 | pmd = pmd_offset(pud, address); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 474 | if (bad_address(pmd)) | 
|  | 475 | goto bad; | 
|  | 476 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | printk("PMD %lx ", pmd_val(*pmd)); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 478 | if (!pmd_present(*pmd) || pmd_large(*pmd)) | 
|  | 479 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 |  | 
|  | 481 | pte = pte_offset_kernel(pmd, address); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 482 | if (bad_address(pte)) | 
|  | 483 | goto bad; | 
|  | 484 |  | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 485 | printk("PTE %lx", pte_val(*pte)); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 486 | out: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | printk("\n"); | 
|  | 488 | return; | 
|  | 489 | bad: | 
|  | 490 | printk("BAD\n"); | 
|  | 491 | } | 
|  | 492 |  | 
| Ingo Molnar | f2f13a8 | 2009-02-20 22:50:24 +0100 | [diff] [blame] | 493 | #endif /* CONFIG_X86_64 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 495 | /* | 
|  | 496 | * Workaround for K8 erratum #93 & buggy BIOS. | 
|  | 497 | * | 
|  | 498 | * BIOS SMM functions are required to use a specific workaround | 
|  | 499 | * to avoid corruption of the 64bit RIP register on C stepping K8. | 
|  | 500 | * | 
|  | 501 | * A lot of BIOS that didn't get tested properly miss this. | 
|  | 502 | * | 
|  | 503 | * The OS sees this as a page fault with the upper 32bits of RIP cleared. | 
|  | 504 | * Try to work around it here. | 
|  | 505 | * | 
|  | 506 | * Note we only handle faults in kernel here. | 
|  | 507 | * Does nothing on 32-bit. | 
| Harvey Harrison | fdfe8aa | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 508 | */ | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 509 | static int is_errata93(struct pt_regs *regs, unsigned long address) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | { | 
| Jan Beulich | e05139f | 2011-09-28 16:56:51 +0100 | [diff] [blame] | 511 | #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD) | 
|  | 512 | if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD | 
|  | 513 | || boot_cpu_data.x86 != 0xf) | 
|  | 514 | return 0; | 
|  | 515 |  | 
| H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 516 | if (address != regs->ip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | return 0; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 518 |  | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 519 | if ((address >> 32) != 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | return 0; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 521 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | address |= 0xffffffffUL << 32; | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 523 | if ((address >= (u64)_stext && address <= (u64)_etext) || | 
|  | 524 | (address >= MODULES_VADDR && address <= MODULES_END)) { | 
| Ingo Molnar | a454ab3 | 2009-05-03 10:09:03 +0200 | [diff] [blame] | 525 | printk_once(errata93_warning); | 
| H. Peter Anvin | 65ea5b0 | 2008-01-30 13:30:56 +0100 | [diff] [blame] | 526 | regs->ip = address; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | return 1; | 
|  | 528 | } | 
| Harvey Harrison | fdfe8aa | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 529 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | return 0; | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 531 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 |  | 
| Harvey Harrison | 35f3266 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 533 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 534 | * Work around K8 erratum #100 K8 in compat mode occasionally jumps | 
|  | 535 | * to illegal addresses >4GB. | 
|  | 536 | * | 
|  | 537 | * We catch this in the page fault handler because these addresses | 
|  | 538 | * are not reachable. Just detect this case and return.  Any code | 
| Harvey Harrison | 35f3266 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 539 | * segment in LDT is compatibility mode. | 
|  | 540 | */ | 
|  | 541 | static int is_errata100(struct pt_regs *regs, unsigned long address) | 
|  | 542 | { | 
|  | 543 | #ifdef CONFIG_X86_64 | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 544 | if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32)) | 
| Harvey Harrison | 35f3266 | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 545 | return 1; | 
|  | 546 | #endif | 
|  | 547 | return 0; | 
|  | 548 | } | 
|  | 549 |  | 
| Harvey Harrison | 29caf2f | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 550 | static int is_f00f_bug(struct pt_regs *regs, unsigned long address) | 
|  | 551 | { | 
|  | 552 | #ifdef CONFIG_X86_F00F_BUG | 
|  | 553 | unsigned long nr; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 554 |  | 
| Harvey Harrison | 29caf2f | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 555 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 556 | * Pentium F0 0F C7 C8 bug workaround: | 
| Harvey Harrison | 29caf2f | 2008-01-30 13:34:09 +0100 | [diff] [blame] | 557 | */ | 
|  | 558 | if (boot_cpu_data.f00f_bug) { | 
|  | 559 | nr = (address - idt_descr.address) >> 3; | 
|  | 560 |  | 
|  | 561 | if (nr == 6) { | 
|  | 562 | do_invalid_op(regs, 0); | 
|  | 563 | return 1; | 
|  | 564 | } | 
|  | 565 | } | 
|  | 566 | #endif | 
|  | 567 | return 0; | 
|  | 568 | } | 
|  | 569 |  | 
| Ingo Molnar | 8f76614 | 2009-02-20 23:00:29 +0100 | [diff] [blame] | 570 | static const char nx_warning[] = KERN_CRIT | 
|  | 571 | "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n"; | 
|  | 572 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 573 | static void | 
|  | 574 | show_fault_oops(struct pt_regs *regs, unsigned long error_code, | 
|  | 575 | unsigned long address) | 
| Harvey Harrison | b3279c7 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 576 | { | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 577 | if (!oops_may_print()) | 
|  | 578 | return; | 
|  | 579 |  | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 580 | if (error_code & PF_INSTR) { | 
| Harvey Harrison | 93809be | 2008-02-01 17:49:43 +0100 | [diff] [blame] | 581 | unsigned int level; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 582 |  | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 583 | pte_t *pte = lookup_address(address, &level); | 
|  | 584 |  | 
| Ingo Molnar | 8f76614 | 2009-02-20 23:00:29 +0100 | [diff] [blame] | 585 | if (pte && pte_present(*pte) && !pte_exec(*pte)) | 
| Eric W. Biederman | 078de5f | 2012-02-08 07:00:08 -0800 | [diff] [blame] | 586 | printk(nx_warning, from_kuid(&init_user_ns, current_uid())); | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 587 | } | 
| Harvey Harrison | fd40d6e | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 588 |  | 
| Harvey Harrison | 1156e09 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 589 | printk(KERN_ALERT "BUG: unable to handle kernel "); | 
|  | 590 | if (address < PAGE_SIZE) | 
|  | 591 | printk(KERN_CONT "NULL pointer dereference"); | 
|  | 592 | else | 
|  | 593 | printk(KERN_CONT "paging request"); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 594 |  | 
| Vegard Nossum | f294a8c | 2008-07-01 15:38:13 +0200 | [diff] [blame] | 595 | printk(KERN_CONT " at %p\n", (void *) address); | 
| Harvey Harrison | 19f0dda | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 596 | printk(KERN_ALERT "IP:"); | 
| Harvey Harrison | b3279c7 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 597 | printk_address(regs->ip, 1); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 598 |  | 
| Harvey Harrison | b3279c7 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 599 | dump_pagetable(address); | 
|  | 600 | } | 
|  | 601 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 602 | static noinline void | 
|  | 603 | pgtable_bad(struct pt_regs *regs, unsigned long error_code, | 
|  | 604 | unsigned long address) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | { | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 606 | struct task_struct *tsk; | 
|  | 607 | unsigned long flags; | 
|  | 608 | int sig; | 
|  | 609 |  | 
|  | 610 | flags = oops_begin(); | 
|  | 611 | tsk = current; | 
|  | 612 | sig = SIGKILL; | 
| Jan Beulich | 1209140 | 2005-09-12 18:49:24 +0200 | [diff] [blame] | 613 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 615 | tsk->comm, address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | dump_pagetable(address); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 617 |  | 
|  | 618 | tsk->thread.cr2		= address; | 
| Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 619 | tsk->thread.trap_nr	= X86_TRAP_PF; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 620 | tsk->thread.error_code	= error_code; | 
|  | 621 |  | 
| Jan Beulich | 22f5991 | 2008-01-30 13:31:23 +0100 | [diff] [blame] | 622 | if (__die("Bad pagetable", regs, error_code)) | 
| Alexander van Heukelum | 874d93d | 2008-10-22 12:00:09 +0200 | [diff] [blame] | 623 | sig = 0; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 624 |  | 
| Alexander van Heukelum | 874d93d | 2008-10-22 12:00:09 +0200 | [diff] [blame] | 625 | oops_end(flags, regs, sig); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } | 
|  | 627 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 628 | static noinline void | 
|  | 629 | no_context(struct pt_regs *regs, unsigned long error_code, | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 630 | unsigned long address, int signal, int si_code) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 631 | { | 
|  | 632 | struct task_struct *tsk = current; | 
| Ingo Molnar | 1980307 | 2009-01-21 10:39:51 +0100 | [diff] [blame] | 633 | unsigned long *stackend; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 634 | unsigned long flags; | 
|  | 635 | int sig; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 636 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 637 | /* Are we prepared to handle this kernel fault? */ | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 638 | if (fixup_exception(regs)) { | 
|  | 639 | if (current_thread_info()->sig_on_uaccess_error && signal) { | 
| Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 640 | tsk->thread.trap_nr = X86_TRAP_PF; | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 641 | tsk->thread.error_code = error_code | PF_USER; | 
|  | 642 | tsk->thread.cr2 = address; | 
|  | 643 |  | 
|  | 644 | /* XXX: hwpoison faults will set the wrong code. */ | 
|  | 645 | force_sig_info_fault(signal, si_code, address, tsk, 0); | 
|  | 646 | } | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 647 | return; | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 648 | } | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 649 |  | 
|  | 650 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 651 | * 32-bit: | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 652 | * | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 653 | *   Valid to do another page fault here, because if this fault | 
|  | 654 | *   had been triggered by is_prefetch fixup_exception would have | 
|  | 655 | *   handled it. | 
|  | 656 | * | 
|  | 657 | * 64-bit: | 
|  | 658 | * | 
|  | 659 | *   Hall of shame of CPU/BIOS bugs. | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 660 | */ | 
|  | 661 | if (is_prefetch(regs, error_code, address)) | 
|  | 662 | return; | 
|  | 663 |  | 
|  | 664 | if (is_errata93(regs, address)) | 
|  | 665 | return; | 
|  | 666 |  | 
|  | 667 | /* | 
|  | 668 | * Oops. The kernel tried to access some bad page. We'll have to | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 669 | * terminate things with extreme prejudice: | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 670 | */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 671 | flags = oops_begin(); | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 672 |  | 
|  | 673 | show_fault_oops(regs, error_code, address); | 
|  | 674 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 675 | stackend = end_of_stack(tsk); | 
| Jan Beulich | 0e7810b | 2009-11-20 14:00:14 +0000 | [diff] [blame] | 676 | if (tsk != &init_task && *stackend != STACK_END_MAGIC) | 
| Prarit Bhargava | b0f4c4b | 2012-01-26 08:55:34 -0500 | [diff] [blame] | 677 | printk(KERN_EMERG "Thread overran stack, or stack corrupted\n"); | 
| Ingo Molnar | 1980307 | 2009-01-21 10:39:51 +0100 | [diff] [blame] | 678 |  | 
| Ingo Molnar | 1cc9954 | 2009-02-20 23:07:48 +0100 | [diff] [blame] | 679 | tsk->thread.cr2		= address; | 
| Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 680 | tsk->thread.trap_nr	= X86_TRAP_PF; | 
| Ingo Molnar | 1cc9954 | 2009-02-20 23:07:48 +0100 | [diff] [blame] | 681 | tsk->thread.error_code	= error_code; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 682 |  | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 683 | sig = SIGKILL; | 
|  | 684 | if (__die("Oops", regs, error_code)) | 
|  | 685 | sig = 0; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 686 |  | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 687 | /* Executive summary in case the body of the oops scrolled away */ | 
| Prarit Bhargava | b0f4c4b | 2012-01-26 08:55:34 -0500 | [diff] [blame] | 688 | printk(KERN_DEFAULT "CR2: %016lx\n", address); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 689 |  | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 690 | oops_end(flags, regs, sig); | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 691 | } | 
|  | 692 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 693 | /* | 
|  | 694 | * Print out info about fatal segfaults, if the show_unhandled_signals | 
|  | 695 | * sysctl is set: | 
|  | 696 | */ | 
|  | 697 | static inline void | 
|  | 698 | show_signal_msg(struct pt_regs *regs, unsigned long error_code, | 
|  | 699 | unsigned long address, struct task_struct *tsk) | 
|  | 700 | { | 
|  | 701 | if (!unhandled_signal(tsk, SIGSEGV)) | 
|  | 702 | return; | 
|  | 703 |  | 
|  | 704 | if (!printk_ratelimit()) | 
|  | 705 | return; | 
|  | 706 |  | 
| Roland Dreier | a1a08d1 | 2009-07-11 00:10:04 -0700 | [diff] [blame] | 707 | printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx", | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 708 | task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, | 
|  | 709 | tsk->comm, task_pid_nr(tsk), address, | 
|  | 710 | (void *)regs->ip, (void *)regs->sp, error_code); | 
|  | 711 |  | 
|  | 712 | print_vma_addr(KERN_CONT " in ", regs->ip); | 
|  | 713 |  | 
|  | 714 | printk(KERN_CONT "\n"); | 
|  | 715 | } | 
|  | 716 |  | 
|  | 717 | static void | 
|  | 718 | __bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, | 
|  | 719 | unsigned long address, int si_code) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 720 | { | 
|  | 721 | struct task_struct *tsk = current; | 
|  | 722 |  | 
|  | 723 | /* User mode accesses just cause a SIGSEGV */ | 
|  | 724 | if (error_code & PF_USER) { | 
|  | 725 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 726 | * It's possible to have interrupts off here: | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 727 | */ | 
|  | 728 | local_irq_enable(); | 
|  | 729 |  | 
|  | 730 | /* | 
|  | 731 | * Valid to do another page fault here because this one came | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 732 | * from user space: | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 733 | */ | 
|  | 734 | if (is_prefetch(regs, error_code, address)) | 
|  | 735 | return; | 
|  | 736 |  | 
|  | 737 | if (is_errata100(regs, address)) | 
|  | 738 | return; | 
|  | 739 |  | 
| Andy Lutomirski | 3ae3665 | 2011-08-10 11:15:32 -0400 | [diff] [blame] | 740 | #ifdef CONFIG_X86_64 | 
|  | 741 | /* | 
|  | 742 | * Instruction fetch faults in the vsyscall page might need | 
|  | 743 | * emulation. | 
|  | 744 | */ | 
|  | 745 | if (unlikely((error_code & PF_INSTR) && | 
|  | 746 | ((address & ~0xfff) == VSYSCALL_START))) { | 
|  | 747 | if (emulate_vsyscall(regs, address)) | 
|  | 748 | return; | 
|  | 749 | } | 
|  | 750 | #endif | 
|  | 751 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 752 | if (unlikely(show_unhandled_signals)) | 
|  | 753 | show_signal_msg(regs, error_code, address, tsk); | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 754 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 755 | /* Kernel addresses are always protection faults: */ | 
|  | 756 | tsk->thread.cr2		= address; | 
|  | 757 | tsk->thread.error_code	= error_code | (address >= TASK_SIZE); | 
| Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 758 | tsk->thread.trap_nr	= X86_TRAP_PF; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 759 |  | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 760 | force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 761 |  | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 762 | return; | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | if (is_f00f_bug(regs, address)) | 
|  | 766 | return; | 
|  | 767 |  | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 768 | no_context(regs, error_code, address, SIGSEGV, si_code); | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 769 | } | 
|  | 770 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 771 | static noinline void | 
|  | 772 | bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code, | 
|  | 773 | unsigned long address) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 774 | { | 
|  | 775 | __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR); | 
|  | 776 | } | 
|  | 777 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 778 | static void | 
|  | 779 | __bad_area(struct pt_regs *regs, unsigned long error_code, | 
|  | 780 | unsigned long address, int si_code) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 781 | { | 
|  | 782 | struct mm_struct *mm = current->mm; | 
|  | 783 |  | 
|  | 784 | /* | 
|  | 785 | * Something tried to access memory that isn't in our memory map.. | 
|  | 786 | * Fix it, but check if it's kernel or user first.. | 
|  | 787 | */ | 
|  | 788 | up_read(&mm->mmap_sem); | 
|  | 789 |  | 
|  | 790 | __bad_area_nosemaphore(regs, error_code, address, si_code); | 
|  | 791 | } | 
|  | 792 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 793 | static noinline void | 
|  | 794 | bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 795 | { | 
|  | 796 | __bad_area(regs, error_code, address, SEGV_MAPERR); | 
|  | 797 | } | 
|  | 798 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 799 | static noinline void | 
|  | 800 | bad_area_access_error(struct pt_regs *regs, unsigned long error_code, | 
|  | 801 | unsigned long address) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 802 | { | 
|  | 803 | __bad_area(regs, error_code, address, SEGV_ACCERR); | 
|  | 804 | } | 
|  | 805 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 806 | static void | 
| Andi Kleen | a6e04aa | 2009-09-16 11:50:09 +0200 | [diff] [blame] | 807 | do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address, | 
|  | 808 | unsigned int fault) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 809 | { | 
|  | 810 | struct task_struct *tsk = current; | 
|  | 811 | struct mm_struct *mm = tsk->mm; | 
| Andi Kleen | a6e04aa | 2009-09-16 11:50:09 +0200 | [diff] [blame] | 812 | int code = BUS_ADRERR; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 813 |  | 
|  | 814 | up_read(&mm->mmap_sem); | 
|  | 815 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 816 | /* Kernel mode? Handle exceptions or die: */ | 
| Linus Torvalds | 9605456 | 2010-08-13 09:49:20 -0700 | [diff] [blame] | 817 | if (!(error_code & PF_USER)) { | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 818 | no_context(regs, error_code, address, SIGBUS, BUS_ADRERR); | 
| Linus Torvalds | 9605456 | 2010-08-13 09:49:20 -0700 | [diff] [blame] | 819 | return; | 
|  | 820 | } | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 821 |  | 
| Ingo Molnar | cd1b68f | 2009-02-20 23:39:02 +0100 | [diff] [blame] | 822 | /* User-space => ok to do another page fault: */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 823 | if (is_prefetch(regs, error_code, address)) | 
|  | 824 | return; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 825 |  | 
|  | 826 | tsk->thread.cr2		= address; | 
|  | 827 | tsk->thread.error_code	= error_code; | 
| Srikar Dronamraju | 51e7dc7 | 2012-03-12 14:55:55 +0530 | [diff] [blame] | 828 | tsk->thread.trap_nr	= X86_TRAP_PF; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 829 |  | 
| Andi Kleen | a6e04aa | 2009-09-16 11:50:09 +0200 | [diff] [blame] | 830 | #ifdef CONFIG_MEMORY_FAILURE | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 831 | if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) { | 
| Andi Kleen | a6e04aa | 2009-09-16 11:50:09 +0200 | [diff] [blame] | 832 | printk(KERN_ERR | 
|  | 833 | "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n", | 
|  | 834 | tsk->comm, tsk->pid, address); | 
|  | 835 | code = BUS_MCEERR_AR; | 
|  | 836 | } | 
|  | 837 | #endif | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 838 | force_sig_info_fault(SIGBUS, code, address, tsk, fault); | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 839 | } | 
|  | 840 |  | 
| KOSAKI Motohiro | b80ef10 | 2011-05-26 17:12:12 +0900 | [diff] [blame] | 841 | static noinline int | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 842 | mm_fault_error(struct pt_regs *regs, unsigned long error_code, | 
|  | 843 | unsigned long address, unsigned int fault) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 844 | { | 
| KOSAKI Motohiro | b80ef10 | 2011-05-26 17:12:12 +0900 | [diff] [blame] | 845 | /* | 
|  | 846 | * Pagefault was interrupted by SIGKILL. We have no reason to | 
|  | 847 | * continue pagefault. | 
|  | 848 | */ | 
|  | 849 | if (fatal_signal_pending(current)) { | 
|  | 850 | if (!(fault & VM_FAULT_RETRY)) | 
|  | 851 | up_read(¤t->mm->mmap_sem); | 
|  | 852 | if (!(error_code & PF_USER)) | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 853 | no_context(regs, error_code, address, 0, 0); | 
| KOSAKI Motohiro | b80ef10 | 2011-05-26 17:12:12 +0900 | [diff] [blame] | 854 | return 1; | 
|  | 855 | } | 
|  | 856 | if (!(fault & VM_FAULT_ERROR)) | 
|  | 857 | return 0; | 
|  | 858 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 859 | if (fault & VM_FAULT_OOM) { | 
| Andrey Vagin | f862685 | 2011-03-09 15:22:23 -0800 | [diff] [blame] | 860 | /* Kernel mode? Handle exceptions or die: */ | 
|  | 861 | if (!(error_code & PF_USER)) { | 
|  | 862 | up_read(¤t->mm->mmap_sem); | 
| Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 863 | no_context(regs, error_code, address, | 
|  | 864 | SIGSEGV, SEGV_MAPERR); | 
| KOSAKI Motohiro | b80ef10 | 2011-05-26 17:12:12 +0900 | [diff] [blame] | 865 | return 1; | 
| Andrey Vagin | f862685 | 2011-03-09 15:22:23 -0800 | [diff] [blame] | 866 | } | 
|  | 867 |  | 
| David Rientjes | c2d23f9 | 2012-12-12 13:52:10 -0800 | [diff] [blame] | 868 | up_read(¤t->mm->mmap_sem); | 
|  | 869 |  | 
|  | 870 | /* | 
|  | 871 | * We ran out of memory, call the OOM killer, and return the | 
|  | 872 | * userspace (which will retry the fault, or kill us if we got | 
|  | 873 | * oom-killed): | 
|  | 874 | */ | 
|  | 875 | pagefault_out_of_memory(); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 876 | } else { | 
| Andi Kleen | f672b49 | 2010-09-27 22:05:55 +0200 | [diff] [blame] | 877 | if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON| | 
|  | 878 | VM_FAULT_HWPOISON_LARGE)) | 
| Andi Kleen | a6e04aa | 2009-09-16 11:50:09 +0200 | [diff] [blame] | 879 | do_sigbus(regs, error_code, address, fault); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 880 | else | 
|  | 881 | BUG(); | 
|  | 882 | } | 
| KOSAKI Motohiro | b80ef10 | 2011-05-26 17:12:12 +0900 | [diff] [blame] | 883 | return 1; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 884 | } | 
|  | 885 |  | 
| Thomas Gleixner | d8b57bb | 2008-02-06 22:39:43 +0100 | [diff] [blame] | 886 | static int spurious_fault_check(unsigned long error_code, pte_t *pte) | 
|  | 887 | { | 
|  | 888 | if ((error_code & PF_WRITE) && !pte_write(*pte)) | 
|  | 889 | return 0; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 890 |  | 
| Thomas Gleixner | d8b57bb | 2008-02-06 22:39:43 +0100 | [diff] [blame] | 891 | if ((error_code & PF_INSTR) && !pte_exec(*pte)) | 
|  | 892 | return 0; | 
|  | 893 |  | 
|  | 894 | return 1; | 
|  | 895 | } | 
|  | 896 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 898 | * Handle a spurious fault caused by a stale TLB entry. | 
|  | 899 | * | 
|  | 900 | * This allows us to lazily refresh the TLB when increasing the | 
|  | 901 | * permissions of a kernel page (RO -> RW or NX -> X).  Doing it | 
|  | 902 | * eagerly is very expensive since that implies doing a full | 
|  | 903 | * cross-processor TLB flush, even if no stale TLB entries exist | 
|  | 904 | * on other processors. | 
|  | 905 | * | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 906 | * There are no security implications to leaving a stale TLB when | 
|  | 907 | * increasing the permissions on a page. | 
|  | 908 | */ | 
| Masami Hiramatsu | 62c9295 | 2009-08-27 13:23:11 -0400 | [diff] [blame] | 909 | static noinline __kprobes int | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 910 | spurious_fault(unsigned long error_code, unsigned long address) | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 911 | { | 
|  | 912 | pgd_t *pgd; | 
|  | 913 | pud_t *pud; | 
|  | 914 | pmd_t *pmd; | 
|  | 915 | pte_t *pte; | 
| Steven Rostedt | 3c3e569 | 2009-02-19 11:46:36 -0500 | [diff] [blame] | 916 | int ret; | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 917 |  | 
|  | 918 | /* Reserved-bit violation or user access to kernel space? */ | 
|  | 919 | if (error_code & (PF_USER | PF_RSVD)) | 
|  | 920 | return 0; | 
|  | 921 |  | 
|  | 922 | pgd = init_mm.pgd + pgd_index(address); | 
|  | 923 | if (!pgd_present(*pgd)) | 
|  | 924 | return 0; | 
|  | 925 |  | 
|  | 926 | pud = pud_offset(pgd, address); | 
|  | 927 | if (!pud_present(*pud)) | 
|  | 928 | return 0; | 
|  | 929 |  | 
| Thomas Gleixner | d8b57bb | 2008-02-06 22:39:43 +0100 | [diff] [blame] | 930 | if (pud_large(*pud)) | 
|  | 931 | return spurious_fault_check(error_code, (pte_t *) pud); | 
|  | 932 |  | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 933 | pmd = pmd_offset(pud, address); | 
|  | 934 | if (!pmd_present(*pmd)) | 
|  | 935 | return 0; | 
|  | 936 |  | 
| Thomas Gleixner | d8b57bb | 2008-02-06 22:39:43 +0100 | [diff] [blame] | 937 | if (pmd_large(*pmd)) | 
|  | 938 | return spurious_fault_check(error_code, (pte_t *) pmd); | 
|  | 939 |  | 
| Shaohua Li | 660a293 | 2010-07-27 16:06:28 +0800 | [diff] [blame] | 940 | /* | 
|  | 941 | * Note: don't use pte_present() here, since it returns true | 
|  | 942 | * if the _PAGE_PROTNONE bit is set.  However, this aliases the | 
|  | 943 | * _PAGE_GLOBAL bit, which for kernel pages give false positives | 
|  | 944 | * when CONFIG_DEBUG_PAGEALLOC is used. | 
|  | 945 | */ | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 946 | pte = pte_offset_kernel(pmd, address); | 
| Shaohua Li | 660a293 | 2010-07-27 16:06:28 +0800 | [diff] [blame] | 947 | if (!(pte_flags(*pte) & _PAGE_PRESENT)) | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 948 | return 0; | 
|  | 949 |  | 
| Steven Rostedt | 3c3e569 | 2009-02-19 11:46:36 -0500 | [diff] [blame] | 950 | ret = spurious_fault_check(error_code, pte); | 
|  | 951 | if (!ret) | 
|  | 952 | return 0; | 
|  | 953 |  | 
|  | 954 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 955 | * Make sure we have permissions in PMD. | 
|  | 956 | * If not, then there's a bug in the page tables: | 
| Steven Rostedt | 3c3e569 | 2009-02-19 11:46:36 -0500 | [diff] [blame] | 957 | */ | 
|  | 958 | ret = spurious_fault_check(error_code, (pte_t *) pmd); | 
|  | 959 | WARN_ONCE(!ret, "PMD has incorrect permission bits\n"); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 960 |  | 
| Steven Rostedt | 3c3e569 | 2009-02-19 11:46:36 -0500 | [diff] [blame] | 961 | return ret; | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 962 | } | 
|  | 963 |  | 
| Masoud Asgharifard Sharbiani | abd4f75 | 2007-07-22 11:12:28 +0200 | [diff] [blame] | 964 | int show_unhandled_signals = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 966 | static inline int | 
| Michel Lespinasse | 68da336 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 967 | access_error(unsigned long error_code, struct vm_area_struct *vma) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 968 | { | 
| Michel Lespinasse | 68da336 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 969 | if (error_code & PF_WRITE) { | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 970 | /* write, present and write, not present: */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 971 | if (unlikely(!(vma->vm_flags & VM_WRITE))) | 
|  | 972 | return 1; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 973 | return 0; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 974 | } | 
|  | 975 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 976 | /* read, present: */ | 
|  | 977 | if (unlikely(error_code & PF_PROT)) | 
|  | 978 | return 1; | 
|  | 979 |  | 
|  | 980 | /* read, not present: */ | 
|  | 981 | if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))) | 
|  | 982 | return 1; | 
|  | 983 |  | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 984 | return 0; | 
|  | 985 | } | 
|  | 986 |  | 
| Hiroshi Shimamoto | 0973a06 | 2009-02-04 15:24:09 -0800 | [diff] [blame] | 987 | static int fault_in_kernel_space(unsigned long address) | 
|  | 988 | { | 
| Ingo Molnar | d951734 | 2009-02-20 23:32:28 +0100 | [diff] [blame] | 989 | return address >= TASK_SIZE_MAX; | 
| Hiroshi Shimamoto | 0973a06 | 2009-02-04 15:24:09 -0800 | [diff] [blame] | 990 | } | 
|  | 991 |  | 
| H. Peter Anvin | 40d3cd6 | 2012-09-21 12:43:14 -0700 | [diff] [blame] | 992 | static inline bool smap_violation(int error_code, struct pt_regs *regs) | 
|  | 993 | { | 
|  | 994 | if (error_code & PF_USER) | 
|  | 995 | return false; | 
|  | 996 |  | 
|  | 997 | if (!user_mode_vm(regs) && (regs->flags & X86_EFLAGS_AC)) | 
|  | 998 | return false; | 
|  | 999 |  | 
|  | 1000 | return true; | 
|  | 1001 | } | 
|  | 1002 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | /* | 
|  | 1004 | * This routine handles page faults.  It determines the address, | 
|  | 1005 | * and the problem, and then passes it off to one of the appropriate | 
|  | 1006 | * routines. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | */ | 
| Frederic Weisbecker | 6ba3c97 | 2012-07-11 20:26:35 +0200 | [diff] [blame] | 1008 | static void __kprobes | 
|  | 1009 | __do_page_fault(struct pt_regs *regs, unsigned long error_code) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | { | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 1011 | struct vm_area_struct *vma; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1012 | struct task_struct *tsk; | 
|  | 1013 | unsigned long address; | 
|  | 1014 | struct mm_struct *mm; | 
| Harvey Harrison | f8c2ee2 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1015 | int fault; | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1016 | int write = error_code & PF_WRITE; | 
| KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1017 | unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE | | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1018 | (write ? FAULT_FLAG_WRITE : 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 |  | 
| Arjan van de Ven | a9ba9a3 | 2006-03-25 16:30:10 +0100 | [diff] [blame] | 1020 | tsk = current; | 
|  | 1021 | mm = tsk->mm; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1022 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1023 | /* Get the faulting address: */ | 
| Glauber de Oliveira Costa | f51c945 | 2007-07-22 11:12:29 +0200 | [diff] [blame] | 1024 | address = read_cr2(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 |  | 
| Vegard Nossum | f856129 | 2008-04-04 00:53:23 +0200 | [diff] [blame] | 1026 | /* | 
|  | 1027 | * Detect and handle instructions that would cause a page fault for | 
|  | 1028 | * both a tracked kernel page and a userspace page. | 
|  | 1029 | */ | 
|  | 1030 | if (kmemcheck_active(regs)) | 
|  | 1031 | kmemcheck_hide(regs); | 
| Ingo Molnar | 5dfaf90 | 2009-06-16 10:23:32 +0200 | [diff] [blame] | 1032 | prefetchw(&mm->mmap_sem); | 
| Vegard Nossum | f856129 | 2008-04-04 00:53:23 +0200 | [diff] [blame] | 1033 |  | 
| Pekka Paalanen | 0fd0e3d | 2008-05-12 21:20:57 +0200 | [diff] [blame] | 1034 | if (unlikely(kmmio_fault(regs, address))) | 
| Pekka Paalanen | 8606978 | 2008-05-12 21:20:56 +0200 | [diff] [blame] | 1035 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 |  | 
|  | 1037 | /* | 
|  | 1038 | * We fault-in kernel-space virtual memory on-demand. The | 
|  | 1039 | * 'reference' page table is init_mm.pgd. | 
|  | 1040 | * | 
|  | 1041 | * NOTE! We MUST NOT take any locks for this case. We may | 
|  | 1042 | * be in an interrupt or a critical region, and should | 
|  | 1043 | * only copy the information from the master page table, | 
|  | 1044 | * nothing more. | 
|  | 1045 | * | 
|  | 1046 | * This verifies that the fault happens in kernel space | 
|  | 1047 | * (error_code & 4) == 0, and that the fault was not a | 
| Jan Beulich | 8b1bde9 | 2006-01-11 22:42:23 +0100 | [diff] [blame] | 1048 | * protection error (error_code & 9) == 0. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | */ | 
| Hiroshi Shimamoto | 0973a06 | 2009-02-04 15:24:09 -0800 | [diff] [blame] | 1050 | if (unlikely(fault_in_kernel_space(address))) { | 
| Vegard Nossum | f856129 | 2008-04-04 00:53:23 +0200 | [diff] [blame] | 1051 | if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) { | 
|  | 1052 | if (vmalloc_fault(address) >= 0) | 
|  | 1053 | return; | 
|  | 1054 |  | 
|  | 1055 | if (kmemcheck_fault(regs, address, error_code)) | 
|  | 1056 | return; | 
|  | 1057 | } | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 1058 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1059 | /* Can handle a stale RO->RW TLB: */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1060 | if (spurious_fault(error_code, address)) | 
| Jeremy Fitzhardinge | 5b727a3 | 2008-01-30 13:34:11 +0100 | [diff] [blame] | 1061 | return; | 
|  | 1062 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1063 | /* kprobes don't want to hook the spurious faults: */ | 
| Masami Hiramatsu | 9be260a | 2009-02-05 17:12:39 -0500 | [diff] [blame] | 1064 | if (notify_page_fault(regs)) | 
|  | 1065 | return; | 
| Harvey Harrison | f8c2ee2 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1066 | /* | 
|  | 1067 | * Don't take the mm semaphore here. If we fixup a prefetch | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1068 | * fault we could otherwise deadlock: | 
| Harvey Harrison | f8c2ee2 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1069 | */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1070 | bad_area_nosemaphore(regs, error_code, address); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1071 |  | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1072 | return; | 
| Harvey Harrison | f8c2ee2 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1073 | } | 
|  | 1074 |  | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1075 | /* kprobes don't want to hook the spurious faults: */ | 
| Ingo Molnar | f8a6b2b | 2009-02-13 09:44:22 +0100 | [diff] [blame] | 1076 | if (unlikely(notify_page_fault(regs))) | 
| Masami Hiramatsu | 9be260a | 2009-02-05 17:12:39 -0500 | [diff] [blame] | 1077 | return; | 
| Harvey Harrison | f8c2ee2 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1078 | /* | 
| Linus Torvalds | 891cffb | 2008-10-12 13:16:12 -0700 | [diff] [blame] | 1079 | * It's safe to allow irq's after cr2 has been saved and the | 
|  | 1080 | * vmalloc fault has been handled. | 
|  | 1081 | * | 
|  | 1082 | * User-mode registers count as a user access even for any | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1083 | * potential system fault or CPU buglet: | 
| Harvey Harrison | f8c2ee2 | 2008-01-30 13:34:10 +0100 | [diff] [blame] | 1084 | */ | 
| Linus Torvalds | 891cffb | 2008-10-12 13:16:12 -0700 | [diff] [blame] | 1085 | if (user_mode_vm(regs)) { | 
|  | 1086 | local_irq_enable(); | 
|  | 1087 | error_code |= PF_USER; | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1088 | } else { | 
|  | 1089 | if (regs->flags & X86_EFLAGS_IF) | 
|  | 1090 | local_irq_enable(); | 
|  | 1091 | } | 
| Jan Beulich | 8c914cb | 2006-03-25 16:29:40 +0100 | [diff] [blame] | 1092 |  | 
| Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 1093 | if (unlikely(error_code & PF_RSVD)) | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1094 | pgtable_bad(regs, error_code, address); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 |  | 
| H. Peter Anvin | 40d3cd6 | 2012-09-21 12:43:14 -0700 | [diff] [blame] | 1096 | if (static_cpu_has(X86_FEATURE_SMAP)) { | 
|  | 1097 | if (unlikely(smap_violation(error_code, regs))) { | 
|  | 1098 | bad_area_nosemaphore(regs, error_code, address); | 
|  | 1099 | return; | 
|  | 1100 | } | 
|  | 1101 | } | 
|  | 1102 |  | 
| Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1103 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address); | 
| Peter Zijlstra | 7dd1fcc | 2009-03-13 12:21:33 +0100 | [diff] [blame] | 1104 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1106 | * If we're in an interrupt, have no user context or are running | 
|  | 1107 | * in an atomic region then we must not take the fault: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1109 | if (unlikely(in_atomic() || !mm)) { | 
|  | 1110 | bad_area_nosemaphore(regs, error_code, address); | 
|  | 1111 | return; | 
|  | 1112 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 |  | 
| Ingo Molnar | 3a1dfe6 | 2008-10-13 17:49:02 +0200 | [diff] [blame] | 1114 | /* | 
|  | 1115 | * When running in the kernel we expect faults to occur only to | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1116 | * addresses in user space.  All other faults represent errors in | 
|  | 1117 | * the kernel and should generate an OOPS.  Unfortunately, in the | 
|  | 1118 | * case of an erroneous fault occurring in a code path which already | 
|  | 1119 | * holds mmap_sem we will deadlock attempting to validate the fault | 
|  | 1120 | * against the address space.  Luckily the kernel only validly | 
|  | 1121 | * references user space from well defined areas of code, which are | 
|  | 1122 | * listed in the exceptions table. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | * | 
|  | 1124 | * As the vast majority of faults will be valid we will only perform | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1125 | * the source reference check when there is a possibility of a | 
|  | 1126 | * deadlock. Attempt to lock the address space, if we cannot we then | 
|  | 1127 | * validate the source. If this is invalid we can skip the address | 
|  | 1128 | * space check, thus avoiding the deadlock: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1130 | if (unlikely(!down_read_trylock(&mm->mmap_sem))) { | 
| Andi Kleen | 66c5815 | 2006-01-11 22:44:09 +0100 | [diff] [blame] | 1131 | if ((error_code & PF_USER) == 0 && | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1132 | !search_exception_tables(regs->ip)) { | 
|  | 1133 | bad_area_nosemaphore(regs, error_code, address); | 
|  | 1134 | return; | 
|  | 1135 | } | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1136 | retry: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | down_read(&mm->mmap_sem); | 
| Peter Zijlstra | 0100607 | 2009-01-29 16:02:12 +0100 | [diff] [blame] | 1138 | } else { | 
|  | 1139 | /* | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1140 | * The above down_read_trylock() might have succeeded in | 
|  | 1141 | * which case we'll have missed the might_sleep() from | 
|  | 1142 | * down_read(): | 
| Peter Zijlstra | 0100607 | 2009-01-29 16:02:12 +0100 | [diff] [blame] | 1143 | */ | 
|  | 1144 | might_sleep(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | } | 
|  | 1146 |  | 
|  | 1147 | vma = find_vma(mm, address); | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1148 | if (unlikely(!vma)) { | 
|  | 1149 | bad_area(regs, error_code, address); | 
|  | 1150 | return; | 
|  | 1151 | } | 
|  | 1152 | if (likely(vma->vm_start <= address)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | goto good_area; | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1154 | if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) { | 
|  | 1155 | bad_area(regs, error_code, address); | 
|  | 1156 | return; | 
|  | 1157 | } | 
| Harvey Harrison | 33cb524 | 2008-01-30 13:32:19 +0100 | [diff] [blame] | 1158 | if (error_code & PF_USER) { | 
| Harvey Harrison | 6f4d368 | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 1159 | /* | 
|  | 1160 | * Accessing the stack below %sp is always a bug. | 
|  | 1161 | * The large cushion allows instructions like enter | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1162 | * and pusha to work. ("enter $65535, $31" pushes | 
| Harvey Harrison | 6f4d368 | 2008-01-30 13:33:13 +0100 | [diff] [blame] | 1163 | * 32 pointers and then decrements %sp by 65535.) | 
| Chuck Ebbert | 03fdc2c | 2006-06-26 13:59:50 +0200 | [diff] [blame] | 1164 | */ | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1165 | if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) { | 
|  | 1166 | bad_area(regs, error_code, address); | 
|  | 1167 | return; | 
|  | 1168 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | } | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1170 | if (unlikely(expand_stack(vma, address))) { | 
|  | 1171 | bad_area(regs, error_code, address); | 
|  | 1172 | return; | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | /* | 
|  | 1176 | * Ok, we have a good vm_area for this memory access, so | 
|  | 1177 | * we can handle it.. | 
|  | 1178 | */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1179 | good_area: | 
| Michel Lespinasse | 68da336 | 2010-10-26 14:21:58 -0700 | [diff] [blame] | 1180 | if (unlikely(access_error(error_code, vma))) { | 
| Nick Piggin | 92181f1 | 2009-01-20 04:24:26 +0100 | [diff] [blame] | 1181 | bad_area_access_error(regs, error_code, address); | 
|  | 1182 | return; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } | 
|  | 1184 |  | 
|  | 1185 | /* | 
|  | 1186 | * If for any reason at all we couldn't handle the fault, | 
|  | 1187 | * make sure we exit gracefully rather than endlessly redo | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1188 | * the fault: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | */ | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1190 | fault = handle_mm_fault(mm, vma, address, flags); | 
| Ingo Molnar | 2d4a716 | 2009-02-20 19:56:40 +0100 | [diff] [blame] | 1191 |  | 
| KOSAKI Motohiro | b80ef10 | 2011-05-26 17:12:12 +0900 | [diff] [blame] | 1192 | if (unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) { | 
|  | 1193 | if (mm_fault_error(regs, error_code, address, fault)) | 
|  | 1194 | return; | 
| KOSAKI Motohiro | 37b23e0 | 2011-05-24 17:11:30 -0700 | [diff] [blame] | 1195 | } | 
|  | 1196 |  | 
|  | 1197 | /* | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1198 | * Major/minor page fault accounting is only done on the | 
|  | 1199 | * initial attempt. If we go through a retry, it is extremely | 
|  | 1200 | * likely that the page will be found in page cache at that point. | 
|  | 1201 | */ | 
|  | 1202 | if (flags & FAULT_FLAG_ALLOW_RETRY) { | 
|  | 1203 | if (fault & VM_FAULT_MAJOR) { | 
|  | 1204 | tsk->maj_flt++; | 
| Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1205 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1206 | regs, address); | 
|  | 1207 | } else { | 
|  | 1208 | tsk->min_flt++; | 
| Peter Zijlstra | a8b0ca1 | 2011-06-27 14:41:57 +0200 | [diff] [blame] | 1209 | perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1210 | regs, address); | 
|  | 1211 | } | 
|  | 1212 | if (fault & VM_FAULT_RETRY) { | 
|  | 1213 | /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk | 
|  | 1214 | * of starvation. */ | 
|  | 1215 | flags &= ~FAULT_FLAG_ALLOW_RETRY; | 
| Shaohua Li | 45cac65 | 2012-10-08 16:32:19 -0700 | [diff] [blame] | 1216 | flags |= FAULT_FLAG_TRIED; | 
| Michel Lespinasse | d065bd8 | 2010-10-26 14:21:57 -0700 | [diff] [blame] | 1217 | goto retry; | 
|  | 1218 | } | 
| Peter Zijlstra | ac17dc8 | 2009-03-13 12:21:34 +0100 | [diff] [blame] | 1219 | } | 
| Harvey Harrison | d729ab3 | 2008-01-30 13:33:23 +0100 | [diff] [blame] | 1220 |  | 
| Ingo Molnar | 8c938f9 | 2009-02-20 22:12:18 +0100 | [diff] [blame] | 1221 | check_v8086_mode(regs, address, tsk); | 
|  | 1222 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | up_read(&mm->mmap_sem); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | } | 
| Frederic Weisbecker | 6ba3c97 | 2012-07-11 20:26:35 +0200 | [diff] [blame] | 1225 |  | 
|  | 1226 | dotraplinkage void __kprobes | 
|  | 1227 | do_page_fault(struct pt_regs *regs, unsigned long error_code) | 
|  | 1228 | { | 
|  | 1229 | exception_enter(regs); | 
|  | 1230 | __do_page_fault(regs, error_code); | 
|  | 1231 | exception_exit(regs); | 
|  | 1232 | } |