blob: 071eee6041473d7f5da261f07aaad322067bb828 [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Ingo Molnara2bcd472009-03-29 23:47:48 +020015#include <asm/traps.h> /* dotraplinkage, ... */
16#include <asm/pgalloc.h> /* pgd_*(), ... */
Vegard Nossumf8561292008-04-04 00:53:23 +020017#include <asm/kmemcheck.h> /* kmemcheck_*(), ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Harvey Harrison33cb5242008-01-30 13:32:19 +010019/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010020 * Page fault error code bits:
21 *
22 * bit 0 == 0: no page found 1: protection fault
23 * bit 1 == 0: read access 1: write access
24 * bit 2 == 0: kernel-mode access 1: user-mode access
25 * bit 3 == 1: use of reserved bit detected
26 * bit 4 == 1: fault was an instruction fetch
Harvey Harrison33cb5242008-01-30 13:32:19 +010027 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +010028enum x86_pf_error_code {
29
30 PF_PROT = 1 << 0,
31 PF_WRITE = 1 << 1,
32 PF_USER = 1 << 2,
33 PF_RSVD = 1 << 3,
34 PF_INSTR = 1 << 4,
35};
Andi Kleen66c58152006-01-11 22:44:09 +010036
Ingo Molnarb814d412009-02-20 22:32:10 +010037/*
Ingo Molnarb319eed2009-02-22 10:24:18 +010038 * Returns 0 if mmiotrace is disabled, or if the fault is not
39 * handled by mmiotrace:
Ingo Molnarb814d412009-02-20 22:32:10 +010040 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020041static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020042{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020043 if (unlikely(is_kmmio_active()))
44 if (kmmio_handler(regs, addr) == 1)
45 return -1;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020046 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020047}
48
Christoph Hellwig74a0b572007-10-16 01:24:07 -070049static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070050{
Christoph Hellwig74a0b572007-10-16 01:24:07 -070051 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070052
Christoph Hellwig74a0b572007-10-16 01:24:07 -070053 /* kprobe_running() needs smp_processor_id() */
Ingo Molnarb1801812009-02-20 22:42:57 +010054 if (kprobes_built_in() && !user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070055 preempt_disable();
56 if (kprobe_running() && kprobe_fault_handler(regs, 14))
57 ret = 1;
58 preempt_enable();
59 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070060
Christoph Hellwig74a0b572007-10-16 01:24:07 -070061 return ret;
Harvey Harrison33cb5242008-01-30 13:32:19 +010062}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070063
Harvey Harrison1dc85be2008-01-30 13:32:35 +010064/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010065 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010066 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010067 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010068 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010069 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
70 * Check that here and ignore it.
71 *
72 * 64-bit mode:
73 *
74 * Sometimes the CPU reports invalid exceptions on prefetch.
75 * Check that here and ignore it.
76 *
77 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +010078 */
Ingo Molnar107a0362009-02-20 20:37:05 +010079static inline int
80check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
81 unsigned char opcode, int *prefetch)
82{
83 unsigned char instr_hi = opcode & 0xf0;
84 unsigned char instr_lo = opcode & 0x0f;
85
86 switch (instr_hi) {
87 case 0x20:
88 case 0x30:
89 /*
90 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
91 * In X86_64 long mode, the CPU will signal invalid
92 * opcode if some of these prefixes are present so
93 * X86_64 will never get here anyway
94 */
95 return ((instr_lo & 7) == 0x6);
96#ifdef CONFIG_X86_64
97 case 0x40:
98 /*
99 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
100 * Need to figure out under what instruction mode the
101 * instruction was issued. Could check the LDT for lm,
102 * but for now it's good enough to assume that long
103 * mode only uses well known segments or kernel.
104 */
105 return (!user_mode(regs)) || (regs->cs == __USER_CS);
106#endif
107 case 0x60:
108 /* 0x64 thru 0x67 are valid prefixes in all modes. */
109 return (instr_lo & 0xC) == 0x4;
110 case 0xF0:
111 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
112 return !instr_lo || (instr_lo>>1) == 1;
113 case 0x00:
114 /* Prefetch instruction is 0x0F0D or 0x0F18 */
115 if (probe_kernel_address(instr, opcode))
116 return 0;
117
118 *prefetch = (instr_lo == 0xF) &&
119 (opcode == 0x0D || opcode == 0x18);
120 return 0;
121 default:
122 return 0;
123 }
124}
125
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100126static int
127is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100128{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100129 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100130 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100131 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Ingo Molnar30853542008-03-27 21:29:09 +0100133 /*
134 * If it was a exec (instruction fetch) fault on NX page, then
135 * do not ignore the fault:
136 */
Andi Kleen66c58152006-01-11 22:44:09 +0100137 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100139
Ingo Molnar107a0362009-02-20 20:37:05 +0100140 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700141 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700143 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return 0;
145
Ingo Molnar107a0362009-02-20 20:37:05 +0100146 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100147 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100149 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100150 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 instr++;
153
Ingo Molnar107a0362009-02-20 20:37:05 +0100154 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 }
157 return prefetch;
158}
159
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100160static void
161force_sig_info_fault(int si_signo, int si_code, unsigned long address,
162 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100163{
164 siginfo_t info;
165
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100166 info.si_signo = si_signo;
167 info.si_errno = 0;
168 info.si_code = si_code;
169 info.si_addr = (void __user *)address;
Andi Kleena6e04aa2009-09-16 11:50:09 +0200170 info.si_addr_lsb = si_code == BUS_MCEERR_AR ? PAGE_SHIFT : 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100171
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100172 force_sig_info(si_signo, &info, tsk);
173}
174
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100175DEFINE_SPINLOCK(pgd_lock);
176LIST_HEAD(pgd_list);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100177
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100178#ifdef CONFIG_X86_32
179static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
180{
181 unsigned index = pgd_index(address);
182 pgd_t *pgd_k;
183 pud_t *pud, *pud_k;
184 pmd_t *pmd, *pmd_k;
185
186 pgd += index;
187 pgd_k = init_mm.pgd + index;
188
189 if (!pgd_present(*pgd_k))
190 return NULL;
191
192 /*
193 * set_pgd(pgd, *pgd_k); here would be useless on PAE
194 * and redundant with the set_pmd() on non-PAE. As would
195 * set_pud.
196 */
197 pud = pud_offset(pgd, address);
198 pud_k = pud_offset(pgd_k, address);
199 if (!pud_present(*pud_k))
200 return NULL;
201
202 pmd = pmd_offset(pud, address);
203 pmd_k = pmd_offset(pud_k, address);
204 if (!pmd_present(*pmd_k))
205 return NULL;
206
Jeremy Fitzhardingeb8bcfe92009-02-17 23:05:19 -0800207 if (!pmd_present(*pmd))
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100208 set_pmd(pmd, *pmd_k);
Jeremy Fitzhardingeb8bcfe92009-02-17 23:05:19 -0800209 else
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100210 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100211
212 return pmd_k;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100213}
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100214
215void vmalloc_sync_all(void)
216{
217 unsigned long address;
218
219 if (SHARED_KERNEL_PMD)
220 return;
221
222 for (address = VMALLOC_START & PMD_MASK;
223 address >= TASK_SIZE && address < FIXADDR_TOP;
224 address += PMD_SIZE) {
225
226 unsigned long flags;
227 struct page *page;
228
229 spin_lock_irqsave(&pgd_lock, flags);
230 list_for_each_entry(page, &pgd_list, lru) {
231 if (!vmalloc_sync_one(page_address(page), address))
232 break;
233 }
234 spin_unlock_irqrestore(&pgd_lock, flags);
235 }
236}
237
238/*
239 * 32-bit:
240 *
241 * Handle a fault on the vmalloc or module mapping area
242 */
243static noinline int vmalloc_fault(unsigned long address)
244{
245 unsigned long pgd_paddr;
246 pmd_t *pmd_k;
247 pte_t *pte_k;
248
249 /* Make sure we are in vmalloc area: */
250 if (!(address >= VMALLOC_START && address < VMALLOC_END))
251 return -1;
252
253 /*
254 * Synchronize this task's top level page-table
255 * with the 'reference' page table.
256 *
257 * Do _not_ use "current" here. We might be inside
258 * an interrupt in the middle of a task switch..
259 */
260 pgd_paddr = read_cr3();
261 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
262 if (!pmd_k)
263 return -1;
264
265 pte_k = pte_offset_kernel(pmd_k, address);
266 if (!pte_present(*pte_k))
267 return -1;
268
269 return 0;
270}
271
272/*
273 * Did it hit the DOS screen memory VA from vm86 mode?
274 */
275static inline void
276check_v8086_mode(struct pt_regs *regs, unsigned long address,
277 struct task_struct *tsk)
278{
279 unsigned long bit;
280
281 if (!v8086_mode(regs))
282 return;
283
284 bit = (address - 0xA0000) >> PAGE_SHIFT;
285 if (bit < 32)
286 tsk->thread.screen_bitmap |= 1 << bit;
287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Akinobu Mita087975b2009-06-27 15:35:15 +0900289static bool low_pfn(unsigned long pfn)
290{
291 return pfn < max_low_pfn;
292}
293
Adrian Bunkcae30f822008-02-13 23:31:31 +0200294static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Akinobu Mita087975b2009-06-27 15:35:15 +0900296 pgd_t *base = __va(read_cr3());
297 pgd_t *pgd = &base[pgd_index(address)];
298 pmd_t *pmd;
299 pte_t *pte;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100300
Harvey Harrison1156e092008-01-30 13:34:10 +0100301#ifdef CONFIG_X86_PAE
Akinobu Mita087975b2009-06-27 15:35:15 +0900302 printk("*pdpt = %016Lx ", pgd_val(*pgd));
303 if (!low_pfn(pgd_val(*pgd) >> PAGE_SHIFT) || !pgd_present(*pgd))
304 goto out;
Harvey Harrison1156e092008-01-30 13:34:10 +0100305#endif
Akinobu Mita087975b2009-06-27 15:35:15 +0900306 pmd = pmd_offset(pud_offset(pgd, address), address);
307 printk(KERN_CONT "*pde = %0*Lx ", sizeof(*pmd) * 2, (u64)pmd_val(*pmd));
Harvey Harrison1156e092008-01-30 13:34:10 +0100308
309 /*
310 * We must not directly access the pte in the highpte
311 * case if the page table is located in highmem.
312 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100313 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100314 */
Akinobu Mita087975b2009-06-27 15:35:15 +0900315 if (!low_pfn(pmd_pfn(*pmd)) || !pmd_present(*pmd) || pmd_large(*pmd))
316 goto out;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100317
Akinobu Mita087975b2009-06-27 15:35:15 +0900318 pte = pte_offset_kernel(pmd, address);
319 printk("*pte = %0*Lx ", sizeof(*pte) * 2, (u64)pte_val(*pte));
320out:
Harvey Harrison1156e092008-01-30 13:34:10 +0100321 printk("\n");
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100322}
323
324#else /* CONFIG_X86_64: */
325
326void vmalloc_sync_all(void)
327{
328 unsigned long address;
329
330 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
331 address += PGDIR_SIZE) {
332
333 const pgd_t *pgd_ref = pgd_offset_k(address);
334 unsigned long flags;
335 struct page *page;
336
337 if (pgd_none(*pgd_ref))
338 continue;
339
340 spin_lock_irqsave(&pgd_lock, flags);
341 list_for_each_entry(page, &pgd_list, lru) {
342 pgd_t *pgd;
343 pgd = (pgd_t *)page_address(page) + pgd_index(address);
344 if (pgd_none(*pgd))
345 set_pgd(pgd, *pgd_ref);
346 else
347 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
348 }
349 spin_unlock_irqrestore(&pgd_lock, flags);
350 }
351}
352
353/*
354 * 64-bit:
355 *
356 * Handle a fault on the vmalloc area
357 *
358 * This assumes no large pages in there.
359 */
360static noinline int vmalloc_fault(unsigned long address)
361{
362 pgd_t *pgd, *pgd_ref;
363 pud_t *pud, *pud_ref;
364 pmd_t *pmd, *pmd_ref;
365 pte_t *pte, *pte_ref;
366
367 /* Make sure we are in vmalloc area: */
368 if (!(address >= VMALLOC_START && address < VMALLOC_END))
369 return -1;
370
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
381 if (pgd_none(*pgd))
382 set_pgd(pgd, *pgd_ref);
383 else
384 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
385
386 /*
387 * Below here mismatches are bugs because these lower tables
388 * are shared:
389 */
390
391 pud = pud_offset(pgd, address);
392 pud_ref = pud_offset(pgd_ref, address);
393 if (pud_none(*pud_ref))
394 return -1;
395
396 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
397 BUG();
398
399 pmd = pmd_offset(pud, address);
400 pmd_ref = pmd_offset(pud_ref, address);
401 if (pmd_none(*pmd_ref))
402 return -1;
403
404 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
405 BUG();
406
407 pte_ref = pte_offset_kernel(pmd_ref, address);
408 if (!pte_present(*pte_ref))
409 return -1;
410
411 pte = pte_offset_kernel(pmd, address);
412
413 /*
414 * Don't use pte_page here, because the mappings can point
415 * outside mem_map, and the NUMA hash lookup cannot handle
416 * that:
417 */
418 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
419 BUG();
420
421 return 0;
422}
423
424static const char errata93_warning[] =
Joe Perchesad361c92009-07-06 13:05:40 -0700425KERN_ERR
426"******* Your BIOS seems to not contain a fix for K8 errata #93\n"
427"******* Working around it, but it may cause SEGVs or burn power.\n"
428"******* Please consider a BIOS update.\n"
429"******* Disabling USB legacy in the BIOS may also help.\n";
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100430
431/*
432 * No vm86 mode in 64-bit mode:
433 */
434static inline void
435check_v8086_mode(struct pt_regs *regs, unsigned long address,
436 struct task_struct *tsk)
437{
438}
439
440static int bad_address(void *p)
441{
442 unsigned long dummy;
443
444 return probe_kernel_address((unsigned long *)p, dummy);
445}
446
447static void dump_pagetable(unsigned long address)
448{
Akinobu Mita087975b2009-06-27 15:35:15 +0900449 pgd_t *base = __va(read_cr3() & PHYSICAL_PAGE_MASK);
450 pgd_t *pgd = base + pgd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 pud_t *pud;
452 pmd_t *pmd;
453 pte_t *pte;
454
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100455 if (bad_address(pgd))
456 goto bad;
457
Jan Beulichd646bce2006-02-03 21:51:47 +0100458 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100459
460 if (!pgd_present(*pgd))
461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Andi Kleend2ae5b52006-06-26 13:57:56 +0200463 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100464 if (bad_address(pud))
465 goto bad;
466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100468 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100469 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100472 if (bad_address(pmd))
473 goto bad;
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100476 if (!pmd_present(*pmd) || pmd_large(*pmd))
477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100480 if (bad_address(pte))
481 goto bad;
482
Harvey Harrison33cb5242008-01-30 13:32:19 +0100483 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100484out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 printk("\n");
486 return;
487bad:
488 printk("BAD\n");
489}
490
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100491#endif /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100493/*
494 * Workaround for K8 erratum #93 & buggy BIOS.
495 *
496 * BIOS SMM functions are required to use a specific workaround
497 * to avoid corruption of the 64bit RIP register on C stepping K8.
498 *
499 * A lot of BIOS that didn't get tested properly miss this.
500 *
501 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
502 * Try to work around it here.
503 *
504 * Note we only handle faults in kernel here.
505 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100506 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100507static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100509#ifdef CONFIG_X86_64
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100510 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100512
Harvey Harrison33cb5242008-01-30 13:32:19 +0100513 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100517 if ((address >= (u64)_stext && address <= (u64)_etext) ||
518 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnara454ab32009-05-03 10:09:03 +0200519 printk_once(errata93_warning);
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100520 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 1;
522 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100523#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100525}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Harvey Harrison35f32662008-01-30 13:34:09 +0100527/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100528 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
529 * to illegal addresses >4GB.
530 *
531 * We catch this in the page fault handler because these addresses
532 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100533 * segment in LDT is compatibility mode.
534 */
535static int is_errata100(struct pt_regs *regs, unsigned long address)
536{
537#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100538 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100539 return 1;
540#endif
541 return 0;
542}
543
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100544static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
545{
546#ifdef CONFIG_X86_F00F_BUG
547 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100548
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100549 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100550 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100551 */
552 if (boot_cpu_data.f00f_bug) {
553 nr = (address - idt_descr.address) >> 3;
554
555 if (nr == 6) {
556 do_invalid_op(regs, 0);
557 return 1;
558 }
559 }
560#endif
561 return 0;
562}
563
Ingo Molnar8f766142009-02-20 23:00:29 +0100564static const char nx_warning[] = KERN_CRIT
565"kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
566
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100567static void
568show_fault_oops(struct pt_regs *regs, unsigned long error_code,
569 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100570{
Harvey Harrison1156e092008-01-30 13:34:10 +0100571 if (!oops_may_print())
572 return;
573
Harvey Harrison1156e092008-01-30 13:34:10 +0100574 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100575 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100576
Harvey Harrison1156e092008-01-30 13:34:10 +0100577 pte_t *pte = lookup_address(address, &level);
578
Ingo Molnar8f766142009-02-20 23:00:29 +0100579 if (pte && pte_present(*pte) && !pte_exec(*pte))
580 printk(nx_warning, current_uid());
Harvey Harrison1156e092008-01-30 13:34:10 +0100581 }
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100582
Harvey Harrison1156e092008-01-30 13:34:10 +0100583 printk(KERN_ALERT "BUG: unable to handle kernel ");
584 if (address < PAGE_SIZE)
585 printk(KERN_CONT "NULL pointer dereference");
586 else
587 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100588
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200589 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100590 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100591 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100592
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100593 dump_pagetable(address);
594}
595
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100596static noinline void
597pgtable_bad(struct pt_regs *regs, unsigned long error_code,
598 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100600 struct task_struct *tsk;
601 unsigned long flags;
602 int sig;
603
604 flags = oops_begin();
605 tsk = current;
606 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100609 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100611
612 tsk->thread.cr2 = address;
613 tsk->thread.trap_no = 14;
614 tsk->thread.error_code = error_code;
615
Jan Beulich22f59912008-01-30 13:31:23 +0100616 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200617 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100618
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200619 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100622static noinline void
623no_context(struct pt_regs *regs, unsigned long error_code,
624 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100625{
626 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100627 unsigned long *stackend;
Nick Piggin92181f12009-01-20 04:24:26 +0100628 unsigned long flags;
629 int sig;
Nick Piggin92181f12009-01-20 04:24:26 +0100630
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100631 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100632 if (fixup_exception(regs))
633 return;
634
635 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100636 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100637 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100638 * Valid to do another page fault here, because if this fault
639 * had been triggered by is_prefetch fixup_exception would have
640 * handled it.
641 *
642 * 64-bit:
643 *
644 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100645 */
646 if (is_prefetch(regs, error_code, address))
647 return;
648
649 if (is_errata93(regs, address))
650 return;
651
652 /*
653 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100654 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100655 */
Nick Piggin92181f12009-01-20 04:24:26 +0100656 flags = oops_begin();
Nick Piggin92181f12009-01-20 04:24:26 +0100657
658 show_fault_oops(regs, error_code, address);
659
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100660 stackend = end_of_stack(tsk);
Jan Beulich0e7810b2009-11-20 14:00:14 +0000661 if (tsk != &init_task && *stackend != STACK_END_MAGIC)
Ingo Molnar19803072009-01-21 10:39:51 +0100662 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
663
Ingo Molnar1cc99542009-02-20 23:07:48 +0100664 tsk->thread.cr2 = address;
665 tsk->thread.trap_no = 14;
666 tsk->thread.error_code = error_code;
Nick Piggin92181f12009-01-20 04:24:26 +0100667
Nick Piggin92181f12009-01-20 04:24:26 +0100668 sig = SIGKILL;
669 if (__die("Oops", regs, error_code))
670 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100671
Nick Piggin92181f12009-01-20 04:24:26 +0100672 /* Executive summary in case the body of the oops scrolled away */
673 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100674
Nick Piggin92181f12009-01-20 04:24:26 +0100675 oops_end(flags, regs, sig);
Nick Piggin92181f12009-01-20 04:24:26 +0100676}
677
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100678/*
679 * Print out info about fatal segfaults, if the show_unhandled_signals
680 * sysctl is set:
681 */
682static inline void
683show_signal_msg(struct pt_regs *regs, unsigned long error_code,
684 unsigned long address, struct task_struct *tsk)
685{
686 if (!unhandled_signal(tsk, SIGSEGV))
687 return;
688
689 if (!printk_ratelimit())
690 return;
691
Roland Dreiera1a08d12009-07-11 00:10:04 -0700692 printk("%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100693 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
694 tsk->comm, task_pid_nr(tsk), address,
695 (void *)regs->ip, (void *)regs->sp, error_code);
696
697 print_vma_addr(KERN_CONT " in ", regs->ip);
698
699 printk(KERN_CONT "\n");
700}
701
702static void
703__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
704 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100705{
706 struct task_struct *tsk = current;
707
708 /* User mode accesses just cause a SIGSEGV */
709 if (error_code & PF_USER) {
710 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100711 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100712 */
713 local_irq_enable();
714
715 /*
716 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100717 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100718 */
719 if (is_prefetch(regs, error_code, address))
720 return;
721
722 if (is_errata100(regs, address))
723 return;
724
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100725 if (unlikely(show_unhandled_signals))
726 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100727
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100728 /* Kernel addresses are always protection faults: */
729 tsk->thread.cr2 = address;
730 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
731 tsk->thread.trap_no = 14;
732
Nick Piggin92181f12009-01-20 04:24:26 +0100733 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100734
Nick Piggin92181f12009-01-20 04:24:26 +0100735 return;
736 }
737
738 if (is_f00f_bug(regs, address))
739 return;
740
741 no_context(regs, error_code, address);
742}
743
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100744static noinline void
745bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
746 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100747{
748 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
749}
750
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100751static void
752__bad_area(struct pt_regs *regs, unsigned long error_code,
753 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100754{
755 struct mm_struct *mm = current->mm;
756
757 /*
758 * Something tried to access memory that isn't in our memory map..
759 * Fix it, but check if it's kernel or user first..
760 */
761 up_read(&mm->mmap_sem);
762
763 __bad_area_nosemaphore(regs, error_code, address, si_code);
764}
765
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100766static noinline void
767bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100768{
769 __bad_area(regs, error_code, address, SEGV_MAPERR);
770}
771
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100772static noinline void
773bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
774 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100775{
776 __bad_area(regs, error_code, address, SEGV_ACCERR);
777}
778
779/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100780static void
781out_of_memory(struct pt_regs *regs, unsigned long error_code,
782 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100783{
784 /*
785 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100786 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100787 */
788 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100789
Nick Piggin92181f12009-01-20 04:24:26 +0100790 pagefault_out_of_memory();
791}
792
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100793static void
Andi Kleena6e04aa2009-09-16 11:50:09 +0200794do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address,
795 unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100796{
797 struct task_struct *tsk = current;
798 struct mm_struct *mm = tsk->mm;
Andi Kleena6e04aa2009-09-16 11:50:09 +0200799 int code = BUS_ADRERR;
Nick Piggin92181f12009-01-20 04:24:26 +0100800
801 up_read(&mm->mmap_sem);
802
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100803 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100804 if (!(error_code & PF_USER))
805 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100806
Ingo Molnarcd1b68f2009-02-20 23:39:02 +0100807 /* User-space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100808 if (is_prefetch(regs, error_code, address))
809 return;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100810
811 tsk->thread.cr2 = address;
812 tsk->thread.error_code = error_code;
813 tsk->thread.trap_no = 14;
814
Andi Kleena6e04aa2009-09-16 11:50:09 +0200815#ifdef CONFIG_MEMORY_FAILURE
816 if (fault & VM_FAULT_HWPOISON) {
817 printk(KERN_ERR
818 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
819 tsk->comm, tsk->pid, address);
820 code = BUS_MCEERR_AR;
821 }
822#endif
823 force_sig_info_fault(SIGBUS, code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100824}
825
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100826static noinline void
827mm_fault_error(struct pt_regs *regs, unsigned long error_code,
828 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100829{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100830 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100831 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100832 } else {
Andi Kleena6e04aa2009-09-16 11:50:09 +0200833 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON))
834 do_sigbus(regs, error_code, address, fault);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100835 else
836 BUG();
837 }
Nick Piggin92181f12009-01-20 04:24:26 +0100838}
839
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100840static int spurious_fault_check(unsigned long error_code, pte_t *pte)
841{
842 if ((error_code & PF_WRITE) && !pte_write(*pte))
843 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100844
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100845 if ((error_code & PF_INSTR) && !pte_exec(*pte))
846 return 0;
847
848 return 1;
849}
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100852 * Handle a spurious fault caused by a stale TLB entry.
853 *
854 * This allows us to lazily refresh the TLB when increasing the
855 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
856 * eagerly is very expensive since that implies doing a full
857 * cross-processor TLB flush, even if no stale TLB entries exist
858 * on other processors.
859 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100860 * There are no security implications to leaving a stale TLB when
861 * increasing the permissions on a page.
862 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100863static noinline int
864spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100865{
866 pgd_t *pgd;
867 pud_t *pud;
868 pmd_t *pmd;
869 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500870 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100871
872 /* Reserved-bit violation or user access to kernel space? */
873 if (error_code & (PF_USER | PF_RSVD))
874 return 0;
875
876 pgd = init_mm.pgd + pgd_index(address);
877 if (!pgd_present(*pgd))
878 return 0;
879
880 pud = pud_offset(pgd, address);
881 if (!pud_present(*pud))
882 return 0;
883
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100884 if (pud_large(*pud))
885 return spurious_fault_check(error_code, (pte_t *) pud);
886
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100887 pmd = pmd_offset(pud, address);
888 if (!pmd_present(*pmd))
889 return 0;
890
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100891 if (pmd_large(*pmd))
892 return spurious_fault_check(error_code, (pte_t *) pmd);
893
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100894 pte = pte_offset_kernel(pmd, address);
895 if (!pte_present(*pte))
896 return 0;
897
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500898 ret = spurious_fault_check(error_code, pte);
899 if (!ret)
900 return 0;
901
902 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100903 * Make sure we have permissions in PMD.
904 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500905 */
906 ret = spurious_fault_check(error_code, (pte_t *) pmd);
907 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100908
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500909 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100910}
911
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200912int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100914static inline int
915access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100916{
917 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100918 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100919 if (unlikely(!(vma->vm_flags & VM_WRITE)))
920 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100921 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100922 }
923
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100924 /* read, present: */
925 if (unlikely(error_code & PF_PROT))
926 return 1;
927
928 /* read, not present: */
929 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
930 return 1;
931
Nick Piggin92181f12009-01-20 04:24:26 +0100932 return 0;
933}
934
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800935static int fault_in_kernel_space(unsigned long address)
936{
Ingo Molnard9517342009-02-20 23:32:28 +0100937 return address >= TASK_SIZE_MAX;
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800938}
939
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940/*
941 * This routine handles page faults. It determines the address,
942 * and the problem, and then passes it off to one of the appropriate
943 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 */
Ingo Molnarc3731c62009-02-20 23:22:34 +0100945dotraplinkage void __kprobes
946do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100948 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100949 struct task_struct *tsk;
950 unsigned long address;
951 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100952 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100953 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100955 tsk = current;
956 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100957
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100958 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200959 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Vegard Nossumf8561292008-04-04 00:53:23 +0200961 /*
962 * Detect and handle instructions that would cause a page fault for
963 * both a tracked kernel page and a userspace page.
964 */
965 if (kmemcheck_active(regs))
966 kmemcheck_hide(regs);
Ingo Molnar5dfaf902009-06-16 10:23:32 +0200967 prefetchw(&mm->mmap_sem);
Vegard Nossumf8561292008-04-04 00:53:23 +0200968
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200969 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200970 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /*
973 * We fault-in kernel-space virtual memory on-demand. The
974 * 'reference' page table is init_mm.pgd.
975 *
976 * NOTE! We MUST NOT take any locks for this case. We may
977 * be in an interrupt or a critical region, and should
978 * only copy the information from the master page table,
979 * nothing more.
980 *
981 * This verifies that the fault happens in kernel space
982 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +0100983 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800985 if (unlikely(fault_in_kernel_space(address))) {
Vegard Nossumf8561292008-04-04 00:53:23 +0200986 if (!(error_code & (PF_RSVD | PF_USER | PF_PROT))) {
987 if (vmalloc_fault(address) >= 0)
988 return;
989
990 if (kmemcheck_fault(regs, address, error_code))
991 return;
992 }
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100993
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100994 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +0100995 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100996 return;
997
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100998 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -0500999 if (notify_page_fault(regs))
1000 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001001 /*
1002 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001003 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001004 */
Nick Piggin92181f12009-01-20 04:24:26 +01001005 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001006
Nick Piggin92181f12009-01-20 04:24:26 +01001007 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001008 }
1009
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001010 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +01001011 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001012 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001013 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -07001014 * It's safe to allow irq's after cr2 has been saved and the
1015 * vmalloc fault has been handled.
1016 *
1017 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001018 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001019 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001020 if (user_mode_vm(regs)) {
1021 local_irq_enable();
1022 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001023 } else {
1024 if (regs->flags & X86_EFLAGS_IF)
1025 local_irq_enable();
1026 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001027
Andi Kleen66c58152006-01-11 22:44:09 +01001028 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001029 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001031 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, 0, regs, address);
Peter Zijlstra7dd1fcc2009-03-13 12:21:33 +01001032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001034 * If we're in an interrupt, have no user context or are running
1035 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 */
Nick Piggin92181f12009-01-20 04:24:26 +01001037 if (unlikely(in_atomic() || !mm)) {
1038 bad_area_nosemaphore(regs, error_code, address);
1039 return;
1040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001042 /*
1043 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001044 * addresses in user space. All other faults represent errors in
1045 * the kernel and should generate an OOPS. Unfortunately, in the
1046 * case of an erroneous fault occurring in a code path which already
1047 * holds mmap_sem we will deadlock attempting to validate the fault
1048 * against the address space. Luckily the kernel only validly
1049 * references user space from well defined areas of code, which are
1050 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 *
1052 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001053 * the source reference check when there is a possibility of a
1054 * deadlock. Attempt to lock the address space, if we cannot we then
1055 * validate the source. If this is invalid we can skip the address
1056 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
Nick Piggin92181f12009-01-20 04:24:26 +01001058 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001059 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001060 !search_exception_tables(regs->ip)) {
1061 bad_area_nosemaphore(regs, error_code, address);
1062 return;
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001065 } else {
1066 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001067 * The above down_read_trylock() might have succeeded in
1068 * which case we'll have missed the might_sleep() from
1069 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001070 */
1071 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073
1074 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001075 if (unlikely(!vma)) {
1076 bad_area(regs, error_code, address);
1077 return;
1078 }
1079 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001081 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1082 bad_area(regs, error_code, address);
1083 return;
1084 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001085 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001086 /*
1087 * Accessing the stack below %sp is always a bug.
1088 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001089 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001090 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001091 */
Nick Piggin92181f12009-01-20 04:24:26 +01001092 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1093 bad_area(regs, error_code, address);
1094 return;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
Nick Piggin92181f12009-01-20 04:24:26 +01001097 if (unlikely(expand_stack(vma, address))) {
1098 bad_area(regs, error_code, address);
1099 return;
1100 }
1101
1102 /*
1103 * Ok, we have a good vm_area for this memory access, so
1104 * we can handle it..
1105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001107 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001108
Nick Piggin92181f12009-01-20 04:24:26 +01001109 if (unlikely(access_error(error_code, write, vma))) {
1110 bad_area_access_error(regs, error_code, address);
1111 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 }
1113
1114 /*
1115 * If for any reason at all we couldn't handle the fault,
1116 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001117 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 */
Linus Torvaldsd06063c2009-04-10 09:01:23 -07001119 fault = handle_mm_fault(mm, vma, address, write ? FAULT_FLAG_WRITE : 0);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001120
Nick Piggin83c54072007-07-19 01:47:05 -07001121 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001122 mm_fault_error(regs, error_code, address, fault);
1123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001125
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001126 if (fault & VM_FAULT_MAJOR) {
Nick Piggin83c54072007-07-19 01:47:05 -07001127 tsk->maj_flt++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001128 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, 0,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001129 regs, address);
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001130 } else {
Nick Piggin83c54072007-07-19 01:47:05 -07001131 tsk->min_flt++;
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001132 perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, 0,
Peter Zijlstra78f13e92009-04-08 15:01:33 +02001133 regs, address);
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001134 }
Harvey Harrisond729ab32008-01-30 13:33:23 +01001135
Ingo Molnar8c938f92009-02-20 22:12:18 +01001136 check_v8086_mode(regs, address, tsk);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}