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