blob: 3aaeffcfd67a6d9ae3b3e89514fed55c31e2ea4d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1995 Linus Torvalds
Ingo Molnar2d4a7162009-02-20 19:56:40 +01003 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
Ingo Molnarf8eeb2e2009-02-20 23:13:36 +01004 * Copyright (C) 2008-2009, Red Hat Inc., Ingo Molnar
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Ingo Molnara2bcd472009-03-29 23:47:48 +02006#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 Molnarcdd6c482009-09-21 12:02:48 +020013#include <linux/perf_event.h> /* perf_sw_event */
Andi Kleenf672b492010-09-27 22:05:55 +020014#include <linux/hugetlb.h> /* hstate_index_to_shift */
Linus Torvalds268bb0c2011-05-20 12:50:29 -070015#include <linux/prefetch.h> /* prefetchw */
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010016#include <linux/context_tracking.h> /* exception_enter(), ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Ingo Molnara2bcd472009-03-29 23:47:48 +020018#include <asm/traps.h> /* dotraplinkage, ... */
19#include <asm/pgalloc.h> /* pgd_*(), ... */
Vegard Nossumf8561292008-04-04 00:53:23 +020020#include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
H. Peter Anvinfab11672011-08-15 22:28:56 -070021#include <asm/fixmap.h> /* VSYSCALL_START */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Harvey Harrison33cb5242008-01-30 13:32:19 +010023/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010024 * 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
Harvey Harrison33cb5242008-01-30 13:32:19 +010031 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +010032enum 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};
Andi Kleen66c58152006-01-11 22:44:09 +010040
Ingo Molnarb814d412009-02-20 22:32:10 +010041/*
Ingo Molnarb319eed2009-02-22 10:24:18 +010042 * Returns 0 if mmiotrace is disabled, or if the fault is not
43 * handled by mmiotrace:
Ingo Molnarb814d412009-02-20 22:32:10 +010044 */
Masami Hiramatsu62c92952009-08-27 13:23:11 -040045static inline int __kprobes
46kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020047{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020048 if (unlikely(is_kmmio_active()))
49 if (kmmio_handler(regs, addr) == 1)
50 return -1;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020051 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020052}
53
Masami Hiramatsu62c92952009-08-27 13:23:11 -040054static inline int __kprobes notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070055{
Christoph Hellwig74a0b572007-10-16 01:24:07 -070056 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070057
Christoph Hellwig74a0b572007-10-16 01:24:07 -070058 /* kprobe_running() needs smp_processor_id() */
Ingo Molnarb1801812009-02-20 22:42:57 +010059 if (kprobes_built_in() && !user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070060 preempt_disable();
61 if (kprobe_running() && kprobe_fault_handler(regs, 14))
62 ret = 1;
63 preempt_enable();
64 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070065
Christoph Hellwig74a0b572007-10-16 01:24:07 -070066 return ret;
Harvey Harrison33cb5242008-01-30 13:32:19 +010067}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070068
Harvey Harrison1dc85be2008-01-30 13:32:35 +010069/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010070 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010071 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010072 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010073 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010074 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
75 * Check that here and ignore it.
76 *
77 * 64-bit mode:
78 *
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.
Harvey Harrison1dc85be2008-01-30 13:32:35 +010083 */
Ingo Molnar107a0362009-02-20 20:37:05 +010084static inline int
85check_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 */
Andy Lutomirski318f5a22011-08-03 09:31:53 -0400110 return (!user_mode(regs) || user_64bit_mode(regs));
Ingo Molnar107a0362009-02-20 20:37:05 +0100111#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
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100131static int
132is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100133{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100134 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100135 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100136 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Ingo Molnar30853542008-03-27 21:29:09 +0100138 /*
139 * If it was a exec (instruction fetch) fault on NX page, then
140 * do not ignore the fault:
141 */
Andi Kleen66c58152006-01-11 22:44:09 +0100142 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100144
Ingo Molnar107a0362009-02-20 20:37:05 +0100145 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700146 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700148 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
150
Ingo Molnar107a0362009-02-20 20:37:05 +0100151 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100152 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100154 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 instr++;
158
Ingo Molnar107a0362009-02-20 20:37:05 +0100159 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162 return prefetch;
163}
164
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100165static void
166force_sig_info_fault(int si_signo, int si_code, unsigned long address,
Andi Kleenf672b492010-09-27 22:05:55 +0200167 struct task_struct *tsk, int fault)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100168{
Andi Kleenf672b492010-09-27 22:05:55 +0200169 unsigned lsb = 0;
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100170 siginfo_t info;
171
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100172 info.si_signo = si_signo;
173 info.si_errno = 0;
174 info.si_code = si_code;
175 info.si_addr = (void __user *)address;
Andi Kleenf672b492010-09-27 22:05:55 +0200176 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;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100181
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100182 force_sig_info(si_signo, &info, tsk);
183}
184
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100185DEFINE_SPINLOCK(pgd_lock);
186LIST_HEAD(pgd_list);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100187
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100188#ifdef CONFIG_X86_32
189static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
190{
191 unsigned index = pgd_index(address);
192 pgd_t *pgd_k;
193 pud_t *pud, *pud_k;
194 pmd_t *pmd, *pmd_k;
195
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
Jeremy Fitzhardingeb8bcfe92009-02-17 23:05:19 -0800217 if (!pmd_present(*pmd))
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100218 set_pmd(pmd, *pmd_k);
Jeremy Fitzhardingeb8bcfe92009-02-17 23:05:19 -0800219 else
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100220 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100221
222 return pmd_k;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100223}
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100224
225void 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) {
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100235 struct page *page;
236
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800237 spin_lock(&pgd_lock);
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100238 list_for_each_entry(page, &pgd_list, lru) {
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700239 spinlock_t *pgt_lock;
Borislav Petkovf01f7c52010-10-19 22:17:37 +0000240 pmd_t *ret;
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700241
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800242 /* the pgt_lock only for Xen */
Jeremy Fitzhardinge617d34d2010-09-21 12:01:51 -0700243 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)
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100250 break;
251 }
Andrea Arcangelia79e53d2011-02-16 15:45:22 -0800252 spin_unlock(&pgd_lock);
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100253 }
254}
255
256/*
257 * 32-bit:
258 *
259 * Handle a fault on the vmalloc or module mapping area
260 */
Masami Hiramatsu62c92952009-08-27 13:23:11 -0400261static noinline __kprobes int vmalloc_fault(unsigned long address)
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100262{
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
Frederic Weisbeckerebc88272010-09-27 18:50:51 +0200271 WARN_ON_ONCE(in_nmi());
272
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100273 /*
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 */
295static inline void
296check_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;
307}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Akinobu Mita087975b2009-06-27 15:35:15 +0900309static bool low_pfn(unsigned long pfn)
310{
311 return pfn < max_low_pfn;
312}
313
Adrian Bunkcae30f822008-02-13 23:31:31 +0200314static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Akinobu Mita087975b2009-06-27 15:35:15 +0900316 pgd_t *base = __va(read_cr3());
317 pgd_t *pgd = &base[pgd_index(address)];
318 pmd_t *pmd;
319 pte_t *pte;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100320
Harvey Harrison1156e092008-01-30 13:34:10 +0100321#ifdef CONFIG_X86_PAE
Akinobu Mita087975b2009-06-27 15:35:15 +0900322 printk("*pdpt = %016Lx ", pgd_val(*pgd));
323 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
324 goto out;
Harvey Harrison1156e092008-01-30 13:34:10 +0100325#endif
Akinobu Mita087975b2009-06-27 15:35:15 +0900326 pmd = pmd_offset(pud_offset(pgd, address), address);
327 printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
Harvey Harrison1156e092008-01-30 13:34:10 +0100328
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
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100333 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100334 */
Akinobu Mita087975b2009-06-27 15:35:15 +0900335 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
336 goto out;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100337
Akinobu Mita087975b2009-06-27 15:35:15 +0900338 pte = pte_offset_kernel(pmd, address);
339 printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
340out:
Harvey Harrison1156e092008-01-30 13:34:10 +0100341 printk("\n");
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100342}
343
344#else /* CONFIG_X86_64: */
345
346void vmalloc_sync_all(void)
347{
Haicheng Li6afb5152010-05-19 17:42:14 +0800348 sync_global_pgds(VMALLOC_START & PGDIR_MASK, VMALLOC_END);
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100349}
350
351/*
352 * 64-bit:
353 *
354 * Handle a fault on the vmalloc area
355 *
356 * This assumes no large pages in there.
357 */
Masami Hiramatsu62c92952009-08-27 13:23:11 -0400358static noinline __kprobes int vmalloc_fault(unsigned long address)
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100359{
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
Frederic Weisbeckerebc88272010-09-27 18:50:51 +0200369 WARN_ON_ONCE(in_nmi());
370
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100371 /*
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
Samu Kallio1160c272013-03-23 09:36:35 -0400381 if (pgd_none(*pgd)) {
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100382 set_pgd(pgd, *pgd_ref);
Samu Kallio1160c272013-03-23 09:36:35 -0400383 arch_flush_lazy_mmu_mode();
384 } else {
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100385 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Samu Kallio1160c272013-03-23 09:36:35 -0400386 }
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100387
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
Jan Beuliche05139f2011-09-28 16:56:51 +0100426#ifdef CONFIG_CPU_SUP_AMD
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100427static const char errata93_warning[] =
Joe Perchesad361c92009-07-06 13:05:40 -0700428KERN_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";
Jan Beuliche05139f2011-09-28 16:56:51 +0100433#endif
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100434
435/*
436 * No vm86 mode in 64-bit mode:
437 */
438static inline void
439check_v8086_mode(struct pt_regs *regs, unsigned long address,
440 struct task_struct *tsk)
441{
442}
443
444static int bad_address(void *p)
445{
446 unsigned long dummy;
447
448 return probe_kernel_address((unsigned long *)p, dummy);
449}
450
451static void dump_pagetable(unsigned long address)
452{
Akinobu Mita087975b2009-06-27 15:35:15 +0900453 pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
454 pgd_t *pgd = base + pgd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 pud_t *pud;
456 pmd_t *pmd;
457 pte_t *pte;
458
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100459 if (bad_address(pgd))
460 goto bad;
461
Jan Beulichd646bce2006-02-03 21:51:47 +0100462 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100463
464 if (!pgd_present(*pgd))
465 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Andi Kleend2ae5b52006-06-26 13:57:56 +0200467 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100468 if (bad_address(pud))
469 goto bad;
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100472 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100473 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100476 if (bad_address(pmd))
477 goto bad;
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100480 if (!pmd_present(*pmd) || pmd_large(*pmd))
481 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100484 if (bad_address(pte))
485 goto bad;
486
Harvey Harrison33cb5242008-01-30 13:32:19 +0100487 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100488out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 printk("\n");
490 return;
491bad:
492 printk("BAD\n");
493}
494
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100495#endif /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100497/*
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.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100510 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100511static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Jan Beuliche05139f2011-09-28 16:56:51 +0100513#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
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100518 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100520
Harvey Harrison33cb5242008-01-30 13:32:19 +0100521 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100525 if ((address >= (u64)_stext && address <= (u64)_etext) ||
526 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnara454ab32009-05-03 10:09:03 +0200527 printk_once(errata93_warning);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100528 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return 1;
530 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100531#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Harvey Harrison35f32662008-01-30 13:34:09 +0100535/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100536 * 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
Harvey Harrison35f32662008-01-30 13:34:09 +0100541 * segment in LDT is compatibility mode.
542 */
543static int is_errata100(struct pt_regs *regs, unsigned long address)
544{
545#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100546 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100547 return 1;
548#endif
549 return 0;
550}
551
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100552static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
553{
554#ifdef CONFIG_X86_F00F_BUG
555 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100556
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100557 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100558 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100559 */
Borislav Petkove2604b42013-03-20 15:07:24 +0100560 if (boot_cpu_has_bug(X86_BUG_F00F)) {
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100561 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
Ingo Molnar8f766142009-02-20 23:00:29 +0100572static const char nx_warning[] = KERN_CRIT
573"kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
574
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100575static void
576show_fault_oops(struct pt_regs *regs, unsigned long error_code,
577 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100578{
Harvey Harrison1156e092008-01-30 13:34:10 +0100579 if (!oops_may_print())
580 return;
581
Harvey Harrison1156e092008-01-30 13:34:10 +0100582 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100583 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100584
Harvey Harrison1156e092008-01-30 13:34:10 +0100585 pte_t *pte = lookup_address(address, &level);
586
Ingo Molnar8f766142009-02-20 23:00:29 +0100587 if (pte && pte_present(*pte) && !pte_exec(*pte))
Eric W. Biederman078de5f2012-02-08 07:00:08 -0800588 printk(nx_warning, from_kuid(&init_user_ns, current_uid()));
Harvey Harrison1156e092008-01-30 13:34:10 +0100589 }
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100590
Harvey Harrison1156e092008-01-30 13:34:10 +0100591 printk(KERN_ALERT "BUG: unable to handle kernel ");
592 if (address < PAGE_SIZE)
593 printk(KERN_CONT "NULL pointer dereference");
594 else
595 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100596
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200597 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100598 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100599 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100600
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100601 dump_pagetable(address);
602}
603
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100604static noinline void
605pgtable_bad(struct pt_regs *regs, unsigned long error_code,
606 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100608 struct task_struct *tsk;
609 unsigned long flags;
610 int sig;
611
612 flags = oops_begin();
613 tsk = current;
614 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100617 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100619
620 tsk->thread.cr2 = address;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530621 tsk->thread.trap_nr = X86_TRAP_PF;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100622 tsk->thread.error_code = error_code;
623
Jan Beulich22f59912008-01-30 13:31:23 +0100624 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200625 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100626
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200627 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100630static noinline void
631no_context(struct pt_regs *regs, unsigned long error_code,
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800632 unsigned long address, int signal, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100633{
634 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100635 unsigned long *stackend;
Nick Piggin92181f12009-01-20 04:24:26 +0100636 unsigned long flags;
637 int sig;
Nick Piggin92181f12009-01-20 04:24:26 +0100638
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100639 /* Are we prepared to handle this kernel fault? */
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800640 if (fixup_exception(regs)) {
641 if (current_thread_info()->sig_on_uaccess_error && signal) {
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530642 tsk->thread.trap_nr = X86_TRAP_PF;
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800643 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 }
Nick Piggin92181f12009-01-20 04:24:26 +0100649 return;
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800650 }
Nick Piggin92181f12009-01-20 04:24:26 +0100651
652 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100653 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100654 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100655 * 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:
660 *
661 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100662 */
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
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100671 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100672 */
Nick Piggin92181f12009-01-20 04:24:26 +0100673 flags = oops_begin();
Nick Piggin92181f12009-01-20 04:24:26 +0100674
675 show_fault_oops(regs, error_code, address);
676
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100677 stackend = end_of_stack(tsk);
Jan Beulich0e7810b2009-11-20 14:00:14 +0000678 if (tsk != &init_task && *stackend != STACK_END_MAGIC)
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500679 printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
Ingo Molnar19803072009-01-21 10:39:51 +0100680
Ingo Molnar1cc99542009-02-20 23:07:48 +0100681 tsk->thread.cr2 = address;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530682 tsk->thread.trap_nr = X86_TRAP_PF;
Ingo Molnar1cc99542009-02-20 23:07:48 +0100683 tsk->thread.error_code = error_code;
Nick Piggin92181f12009-01-20 04:24:26 +0100684
Nick Piggin92181f12009-01-20 04:24:26 +0100685 sig = SIGKILL;
686 if (__die("Oops", regs, error_code))
687 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100688
Nick Piggin92181f12009-01-20 04:24:26 +0100689 /* Executive summary in case the body of the oops scrolled away */
Prarit Bhargavab0f4c4b2012-01-26 08:55:34 -0500690 printk(KERN_DEFAULT "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100691
Nick Piggin92181f12009-01-20 04:24:26 +0100692 oops_end(flags, regs, sig);
Nick Piggin92181f12009-01-20 04:24:26 +0100693}
694
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100695/*
696 * Print out info about fatal segfaults, if the show_unhandled_signals
697 * sysctl is set:
698 */
699static inline void
700show_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
Roland Dreiera1a08d12009-07-11 00:10:04 -0700709 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100710 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
719static void
720__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
721 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100722{
723 struct task_struct *tsk = current;
724
725 /* User mode accesses just cause a SIGSEGV */
726 if (error_code & PF_USER) {
727 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100728 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100729 */
730 local_irq_enable();
731
732 /*
733 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100734 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100735 */
736 if (is_prefetch(regs, error_code, address))
737 return;
738
739 if (is_errata100(regs, address))
740 return;
741
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400742#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
Kees Cooke575a862013-02-07 09:44:13 -0800753 /* Kernel addresses are always protection faults: */
754 if (address >= TASK_SIZE)
755 error_code |= PF_PROT;
Andy Lutomirski3ae36652011-08-10 11:15:32 -0400756
Kees Cooke575a862013-02-07 09:44:13 -0800757 if (likely(show_unhandled_signals))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100758 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100759
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100760 tsk->thread.cr2 = address;
Kees Cooke575a862013-02-07 09:44:13 -0800761 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530762 tsk->thread.trap_nr = X86_TRAP_PF;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100763
Andi Kleenf672b492010-09-27 22:05:55 +0200764 force_sig_info_fault(SIGSEGV, si_code, address, tsk, 0);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100765
Nick Piggin92181f12009-01-20 04:24:26 +0100766 return;
767 }
768
769 if (is_f00f_bug(regs, address))
770 return;
771
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800772 no_context(regs, error_code, address, SIGSEGV, si_code);
Nick Piggin92181f12009-01-20 04:24:26 +0100773}
774
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100775static noinline void
776bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
777 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100778{
779 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
780}
781
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100782static void
783__bad_area(struct pt_regs *regs, unsigned long error_code,
784 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100785{
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
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100797static noinline void
798bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100799{
800 __bad_area(regs, error_code, address, SEGV_MAPERR);
801}
802
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100803static noinline void
804bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
805 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100806{
807 __bad_area(regs, error_code, address, SEGV_ACCERR);
808}
809
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100810static void
Andi Kleena6e04aa2009-09-16 11:50:09 +0200811do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
812 unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100813{
814 struct task_struct *tsk = current;
815 struct mm_struct *mm = tsk->mm;
Andi Kleena6e04aa2009-09-16 11:50:09 +0200816 int code = BUS_ADRERR;
Nick Piggin92181f12009-01-20 04:24:26 +0100817
818 up_read(&mm->mmap_sem);
819
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100820 /* Kernel mode? Handle exceptions or die: */
Linus Torvalds96054562010-08-13 09:49:20 -0700821 if (!(error_code & PF_USER)) {
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800822 no_context(regs, error_code, address, SIGBUS, BUS_ADRERR);
Linus Torvalds96054562010-08-13 09:49:20 -0700823 return;
824 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100825
Ingo Molnarcd1b68f2009-02-20 23:39:02 +0100826 /* User-space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100827 if (is_prefetch(regs, error_code, address))
828 return;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100829
830 tsk->thread.cr2 = address;
831 tsk->thread.error_code = error_code;
Srikar Dronamraju51e7dc72012-03-12 14:55:55 +0530832 tsk->thread.trap_nr = X86_TRAP_PF;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100833
Andi Kleena6e04aa2009-09-16 11:50:09 +0200834#ifdef CONFIG_MEMORY_FAILURE
Andi Kleenf672b492010-09-27 22:05:55 +0200835 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
Andi Kleena6e04aa2009-09-16 11:50:09 +0200836 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
Andi Kleenf672b492010-09-27 22:05:55 +0200842 force_sig_info_fault(SIGBUS, code, address, tsk, fault);
Nick Piggin92181f12009-01-20 04:24:26 +0100843}
844
Johannes Weiner3a13c4d2013-09-12 15:13:40 -0700845static noinline void
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100846mm_fault_error(struct pt_regs *regs, unsigned long error_code,
847 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100848{
Johannes Weiner3a13c4d2013-09-12 15:13:40 -0700849 if (fatal_signal_pending(current) && !(error_code & PF_USER)) {
850 up_read(&current->mm->mmap_sem);
851 no_context(regs, error_code, address, 0, 0);
852 return;
KOSAKI Motohirob80ef102011-05-26 17:12:12 +0900853 }
KOSAKI Motohirob80ef102011-05-26 17:12:12 +0900854
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100855 if (fault & VM_FAULT_OOM) {
Andrey Vaginf8626852011-03-09 15:22:23 -0800856 /* Kernel mode? Handle exceptions or die: */
857 if (!(error_code & PF_USER)) {
858 up_read(&current->mm->mmap_sem);
Andy Lutomirski4fc34902011-11-07 16:33:40 -0800859 no_context(regs, error_code, address,
860 SIGSEGV, SEGV_MAPERR);
Johannes Weiner3a13c4d2013-09-12 15:13:40 -0700861 return;
Andrey Vaginf8626852011-03-09 15:22:23 -0800862 }
863
David Rientjesc2d23f92012-12-12 13:52:10 -0800864 up_read(&current->mm->mmap_sem);
865
866 /*
867 * We ran out of memory, call the OOM killer, and return the
868 * userspace (which will retry the fault, or kill us if we got
869 * oom-killed):
870 */
871 pagefault_out_of_memory();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100872 } else {
Andi Kleenf672b492010-09-27 22:05:55 +0200873 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
874 VM_FAULT_HWPOISON_LARGE))
Andi Kleena6e04aa2009-09-16 11:50:09 +0200875 do_sigbus(regs, error_code, address, fault);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100876 else
877 BUG();
878 }
Nick Piggin92181f12009-01-20 04:24:26 +0100879}
880
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100881static int spurious_fault_check(unsigned long error_code, pte_t *pte)
882{
883 if ((error_code & PF_WRITE) && !pte_write(*pte))
884 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100885
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100886 if ((error_code & PF_INSTR) && !pte_exec(*pte))
887 return 0;
888
889 return 1;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100893 * Handle a spurious fault caused by a stale TLB entry.
894 *
895 * This allows us to lazily refresh the TLB when increasing the
896 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
897 * eagerly is very expensive since that implies doing a full
898 * cross-processor TLB flush, even if no stale TLB entries exist
899 * on other processors.
900 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100901 * There are no security implications to leaving a stale TLB when
902 * increasing the permissions on a page.
903 */
Masami Hiramatsu62c92952009-08-27 13:23:11 -0400904static noinline __kprobes int
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100905spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100906{
907 pgd_t *pgd;
908 pud_t *pud;
909 pmd_t *pmd;
910 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500911 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100912
913 /* Reserved-bit violation or user access to kernel space? */
914 if (error_code & (PF_USER | PF_RSVD))
915 return 0;
916
917 pgd = init_mm.pgd + pgd_index(address);
918 if (!pgd_present(*pgd))
919 return 0;
920
921 pud = pud_offset(pgd, address);
922 if (!pud_present(*pud))
923 return 0;
924
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100925 if (pud_large(*pud))
926 return spurious_fault_check(error_code, (pte_t *) pud);
927
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100928 pmd = pmd_offset(pud, address);
929 if (!pmd_present(*pmd))
930 return 0;
931
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100932 if (pmd_large(*pmd))
933 return spurious_fault_check(error_code, (pte_t *) pmd);
934
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100935 pte = pte_offset_kernel(pmd, address);
Andrea Arcangeli954f8572013-02-22 15:11:49 -0800936 if (!pte_present(*pte))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100937 return 0;
938
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500939 ret = spurious_fault_check(error_code, pte);
940 if (!ret)
941 return 0;
942
943 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100944 * Make sure we have permissions in PMD.
945 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500946 */
947 ret = spurious_fault_check(error_code, (pte_t *) pmd);
948 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100949
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500950 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100951}
952
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200953int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100955static inline int
Michel Lespinasse68da3362010-10-26 14:21:58 -0700956access_error(unsigned long error_code, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100957{
Michel Lespinasse68da3362010-10-26 14:21:58 -0700958 if (error_code & PF_WRITE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100959 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100960 if (unlikely(!(vma->vm_flags & VM_WRITE)))
961 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100962 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100963 }
964
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100965 /* read, present: */
966 if (unlikely(error_code & PF_PROT))
967 return 1;
968
969 /* read, not present: */
970 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
971 return 1;
972
Nick Piggin92181f12009-01-20 04:24:26 +0100973 return 0;
974}
975
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800976static int fault_in_kernel_space(unsigned long address)
977{
Ingo Molnard9517342009-02-20 23:32:28 +0100978 return address >= TASK_SIZE_MAX;
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800979}
980
H. Peter Anvin40d3cd62012-09-21 12:43:14 -0700981static inline bool smap_violation(int error_code, struct pt_regs *regs)
982{
983 if (error_code & PF_USER)
984 return false;
985
986 if (!user_mode_vm(regs) && (regs->flags & X86_EFLAGS_AC))
987 return false;
988
989 return true;
990}
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992/*
993 * This routine handles page faults. It determines the address,
994 * and the problem, and then passes it off to one of the appropriate
995 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 */
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +0200997static void __kprobes
998__do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
Harvey Harrison33cb5242008-01-30 13:32:19 +01001000 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001001 struct task_struct *tsk;
1002 unsigned long address;
1003 struct mm_struct *mm;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001004 int fault;
Johannes Weiner759496b2013-09-12 15:13:39 -07001005 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Arjan van de Vena9ba9a32006-03-25 16:30:10 +01001007 tsk = current;
1008 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001009
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001010 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +02001011 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Vegard Nossumf8561292008-04-04 00:53:23 +02001013 /*
1014 * Detect and handle instructions that would cause a page fault for
1015 * both a tracked kernel page and a userspace page.
1016 */
1017 if (kmemcheck_active(regs))
1018 kmemcheck_hide(regs);
Ingo Molnar5dfaf902009-06-16 10:23:32 +02001019 prefetchw(&mm->mmap_sem);
Vegard Nossumf8561292008-04-04 00:53:23 +02001020
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +02001021 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +02001022 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /*
1025 * We fault-in kernel-space virtual memory on-demand. The
1026 * 'reference' page table is init_mm.pgd.
1027 *
1028 * NOTE! We MUST NOT take any locks for this case. We may
1029 * be in an interrupt or a critical region, and should
1030 * only copy the information from the master page table,
1031 * nothing more.
1032 *
1033 * This verifies that the fault happens in kernel space
1034 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +01001035 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -08001037 if (unlikely(fault_in_kernel_space(address))) {
Vegard Nossumf8561292008-04-04 00:53:23 +02001038 if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
1039 if (vmalloc_fault(address) >= 0)
1040 return;
1041
1042 if (kmemcheck_fault(regs, address, error_code))
1043 return;
1044 }
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001045
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001046 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +01001047 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001048 return;
1049
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001050 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001051 if (notify_page_fault(regs))
1052 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001053 /*
1054 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001055 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001056 */
Nick Piggin92181f12009-01-20 04:24:26 +01001057 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001058
Nick Piggin92181f12009-01-20 04:24:26 +01001059 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001060 }
1061
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001062 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +01001063 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001064 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001065 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -07001066 * It's safe to allow irq's after cr2 has been saved and the
1067 * vmalloc fault has been handled.
1068 *
1069 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001070 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001071 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001072 if (user_mode_vm(regs)) {
1073 local_irq_enable();
1074 error_code |= PF_USER;
Johannes Weiner759496b2013-09-12 15:13:39 -07001075 flags |= FAULT_FLAG_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001076 } else {
1077 if (regs->flags & X86_EFLAGS_IF)
1078 local_irq_enable();
1079 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001080
Andi Kleen66c58152006-01-11 22:44:09 +01001081 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001082 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
H. Peter Anvin40d3cd62012-09-21 12:43:14 -07001084 if (static_cpu_has(X86_FEATURE_SMAP)) {
1085 if (unlikely(smap_violation(error_code, regs))) {
1086 bad_area_nosemaphore(regs, error_code, address);
1087 return;
1088 }
1089 }
1090
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001091 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, address);
Peter Zijlstra7dd1fcc2009-03-13 12:21:33 +01001092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001094 * If we're in an interrupt, have no user context or are running
1095 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 */
Nick Piggin92181f12009-01-20 04:24:26 +01001097 if (unlikely(in_atomic() || !mm)) {
1098 bad_area_nosemaphore(regs, error_code, address);
1099 return;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Johannes Weiner759496b2013-09-12 15:13:39 -07001102 if (error_code & PF_WRITE)
1103 flags |= FAULT_FLAG_WRITE;
1104
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001105 /*
1106 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001107 * addresses in user space. All other faults represent errors in
1108 * the kernel and should generate an OOPS. Unfortunately, in the
1109 * case of an erroneous fault occurring in a code path which already
1110 * holds mmap_sem we will deadlock attempting to validate the fault
1111 * against the address space. Luckily the kernel only validly
1112 * references user space from well defined areas of code, which are
1113 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
1115 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001116 * the source reference check when there is a possibility of a
1117 * deadlock. Attempt to lock the address space, if we cannot we then
1118 * validate the source. If this is invalid we can skip the address
1119 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 */
Nick Piggin92181f12009-01-20 04:24:26 +01001121 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001122 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001123 !search_exception_tables(regs->ip)) {
1124 bad_area_nosemaphore(regs, error_code, address);
1125 return;
1126 }
Michel Lespinassed065bd82010-10-26 14:21:57 -07001127retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001129 } else {
1130 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001131 * The above down_read_trylock() might have succeeded in
1132 * which case we'll have missed the might_sleep() from
1133 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001134 */
1135 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
1137
1138 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001139 if (unlikely(!vma)) {
1140 bad_area(regs, error_code, address);
1141 return;
1142 }
1143 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001145 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1146 bad_area(regs, error_code, address);
1147 return;
1148 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001149 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001150 /*
1151 * Accessing the stack below %sp is always a bug.
1152 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001153 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001154 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001155 */
Nick Piggin92181f12009-01-20 04:24:26 +01001156 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1157 bad_area(regs, error_code, address);
1158 return;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Nick Piggin92181f12009-01-20 04:24:26 +01001161 if (unlikely(expand_stack(vma, address))) {
1162 bad_area(regs, error_code, address);
1163 return;
1164 }
1165
1166 /*
1167 * Ok, we have a good vm_area for this memory access, so
1168 * we can handle it..
1169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170good_area:
Michel Lespinasse68da3362010-10-26 14:21:58 -07001171 if (unlikely(access_error(error_code, vma))) {
Nick Piggin92181f12009-01-20 04:24:26 +01001172 bad_area_access_error(regs, error_code, address);
1173 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175
1176 /*
1177 * If for any reason at all we couldn't handle the fault,
1178 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001179 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Michel Lespinassed065bd82010-10-26 14:21:57 -07001181 fault = handle_mm_fault(mm, vma, address, flags);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001182
Johannes Weiner3a13c4d2013-09-12 15:13:40 -07001183 /*
1184 * If we need to retry but a fatal signal is pending, handle the
1185 * signal first. We do not need to release the mmap_sem because it
1186 * would already be released in __lock_page_or_retry in mm/filemap.c.
1187 */
1188 if (unlikely((fault & VM_FAULT_RETRY) && fatal_signal_pending(current)))
1189 return;
1190
1191 if (unlikely(fault & VM_FAULT_ERROR)) {
1192 mm_fault_error(regs, error_code, address, fault);
1193 return;
KOSAKI Motohiro37b23e02011-05-24 17:11:30 -07001194 }
1195
1196 /*
Michel Lespinassed065bd82010-10-26 14:21:57 -07001197 * Major/minor page fault accounting is only done on the
1198 * initial attempt. If we go through a retry, it is extremely
1199 * likely that the page will be found in page cache at that point.
1200 */
1201 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1202 if (fault & VM_FAULT_MAJOR) {
1203 tsk->maj_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001204 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1,
Michel Lespinassed065bd82010-10-26 14:21:57 -07001205 regs, address);
1206 } else {
1207 tsk->min_flt++;
Peter Zijlstraa8b0ca12011-06-27 14:41:57 +02001208 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1,
Michel Lespinassed065bd82010-10-26 14:21:57 -07001209 regs, address);
1210 }
1211 if (fault & VM_FAULT_RETRY) {
1212 /* Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk
1213 * of starvation. */
1214 flags &= ~FAULT_FLAG_ALLOW_RETRY;
Shaohua Li45cac652012-10-08 16:32:19 -07001215 flags |= FAULT_FLAG_TRIED;
Michel Lespinassed065bd82010-10-26 14:21:57 -07001216 goto retry;
1217 }
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001218 }
Harvey Harrisond729ab32008-01-30 13:33:23 +01001219
Ingo Molnar8c938f92009-02-20 22:12:18 +01001220 check_v8086_mode(regs, address, tsk);
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +02001224
1225dotraplinkage void __kprobes
1226do_page_fault(struct pt_regs *regs, unsigned long error_code)
1227{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +01001228 enum ctx_state prev_state;
1229
1230 prev_state = exception_enter();
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +02001231 __do_page_fault(regs, error_code);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +01001232 exception_exit(prev_state);
Frederic Weisbecker6ba3c972012-07-11 20:26:35 +02001233}