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