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