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