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