blob: f2d3324d9215208b7788a23e071fedde2122ab4f [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/interrupt.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +01007#include <linux/mmiotrace.h>
8#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/compiler.h>
Harvey Harrisonc61e2112008-01-30 13:34:11 +010010#include <linux/highmem.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070011#include <linux/kprobes.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010012#include <linux/uaccess.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010013#include <linux/vmalloc.h>
14#include <linux/vt_kern.h>
15#include <linux/signal.h>
16#include <linux/kernel.h>
17#include <linux/ptrace.h>
18#include <linux/string.h>
19#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070020#include <linux/kdebug.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010021#include <linux/errno.h>
Eric Sandeen7c9f8862008-04-22 16:38:23 -050022#include <linux/magic.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010023#include <linux/sched.h>
24#include <linux/types.h>
25#include <linux/init.h>
26#include <linux/mman.h>
27#include <linux/tty.h>
28#include <linux/smp.h>
29#include <linux/mm.h>
Peter Zijlstra7dd1fcc2009-03-13 12:21:33 +010030#include <linux/perf_counter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm-generic/sections.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010033
34#include <asm/tlbflush.h>
35#include <asm/pgalloc.h>
36#include <asm/segment.h>
37#include <asm/system.h>
38#include <asm/proto.h>
Jaswinder Singh70ef5642008-07-23 17:36:37 +053039#include <asm/traps.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010040#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Harvey Harrison33cb5242008-01-30 13:32:19 +010042/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010043 * Page fault error code bits:
44 *
45 * bit 0 == 0: no page found 1: protection fault
46 * bit 1 == 0: read access 1: write access
47 * bit 2 == 0: kernel-mode access 1: user-mode access
48 * bit 3 == 1: use of reserved bit detected
49 * bit 4 == 1: fault was an instruction fetch
Harvey Harrison33cb5242008-01-30 13:32:19 +010050 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +010051enum x86_pf_error_code {
52
53 PF_PROT = 1 << 0,
54 PF_WRITE = 1 << 1,
55 PF_USER = 1 << 2,
56 PF_RSVD = 1 << 3,
57 PF_INSTR = 1 << 4,
58};
Andi Kleen66c58152006-01-11 22:44:09 +010059
Ingo Molnarb814d412009-02-20 22:32:10 +010060/*
Ingo Molnarb319eed2009-02-22 10:24:18 +010061 * Returns 0 if mmiotrace is disabled, or if the fault is not
62 * handled by mmiotrace:
Ingo Molnarb814d412009-02-20 22:32:10 +010063 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020064static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020065{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020066 if (unlikely(is_kmmio_active()))
67 if (kmmio_handler(regs, addr) == 1)
68 return -1;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020069 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020070}
71
Christoph Hellwig74a0b572007-10-16 01:24:07 -070072static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070073{
Christoph Hellwig74a0b572007-10-16 01:24:07 -070074 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070075
Christoph Hellwig74a0b572007-10-16 01:24:07 -070076 /* kprobe_running() needs smp_processor_id() */
Ingo Molnarb1801812009-02-20 22:42:57 +010077 if (kprobes_built_in() && !user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070078 preempt_disable();
79 if (kprobe_running() && kprobe_fault_handler(regs, 14))
80 ret = 1;
81 preempt_enable();
82 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070083
Christoph Hellwig74a0b572007-10-16 01:24:07 -070084 return ret;
Harvey Harrison33cb5242008-01-30 13:32:19 +010085}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070086
Harvey Harrison1dc85be2008-01-30 13:32:35 +010087/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010088 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010089 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010090 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010091 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010092 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
93 * Check that here and ignore it.
94 *
95 * 64-bit mode:
96 *
97 * Sometimes the CPU reports invalid exceptions on prefetch.
98 * Check that here and ignore it.
99 *
100 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100101 */
Ingo Molnar107a0362009-02-20 20:37:05 +0100102static inline int
103check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
104 unsigned char opcode, int *prefetch)
105{
106 unsigned char instr_hi = opcode & 0xf0;
107 unsigned char instr_lo = opcode & 0x0f;
108
109 switch (instr_hi) {
110 case 0x20:
111 case 0x30:
112 /*
113 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
114 * In X86_64 long mode, the CPU will signal invalid
115 * opcode if some of these prefixes are present so
116 * X86_64 will never get here anyway
117 */
118 return ((instr_lo & 7) == 0x6);
119#ifdef CONFIG_X86_64
120 case 0x40:
121 /*
122 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
123 * Need to figure out under what instruction mode the
124 * instruction was issued. Could check the LDT for lm,
125 * but for now it's good enough to assume that long
126 * mode only uses well known segments or kernel.
127 */
128 return (!user_mode(regs)) || (regs->cs == __USER_CS);
129#endif
130 case 0x60:
131 /* 0x64 thru 0x67 are valid prefixes in all modes. */
132 return (instr_lo & 0xC) == 0x4;
133 case 0xF0:
134 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
135 return !instr_lo || (instr_lo>>1) == 1;
136 case 0x00:
137 /* Prefetch instruction is 0x0F0D or 0x0F18 */
138 if (probe_kernel_address(instr, opcode))
139 return 0;
140
141 *prefetch = (instr_lo == 0xF) &&
142 (opcode == 0x0D || opcode == 0x18);
143 return 0;
144 default:
145 return 0;
146 }
147}
148
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100149static int
150is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100151{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100152 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100153 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100154 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Ingo Molnar30853542008-03-27 21:29:09 +0100156 /*
157 * If it was a exec (instruction fetch) fault on NX page, then
158 * do not ignore the fault:
159 */
Andi Kleen66c58152006-01-11 22:44:09 +0100160 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100162
Ingo Molnar107a0362009-02-20 20:37:05 +0100163 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700164 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700166 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168
Ingo Molnar107a0362009-02-20 20:37:05 +0100169 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100170 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100172 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 instr++;
176
Ingo Molnar107a0362009-02-20 20:37:05 +0100177 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 return prefetch;
181}
182
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100183static void
184force_sig_info_fault(int si_signo, int si_code, unsigned long address,
185 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100186{
187 siginfo_t info;
188
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100189 info.si_signo = si_signo;
190 info.si_errno = 0;
191 info.si_code = si_code;
192 info.si_addr = (void __user *)address;
193
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100194 force_sig_info(si_signo, &info, tsk);
195}
196
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100197DEFINE_SPINLOCK(pgd_lock);
198LIST_HEAD(pgd_list);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100199
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100200#ifdef CONFIG_X86_32
201static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
202{
203 unsigned index = pgd_index(address);
204 pgd_t *pgd_k;
205 pud_t *pud, *pud_k;
206 pmd_t *pmd, *pmd_k;
207
208 pgd += index;
209 pgd_k = init_mm.pgd + index;
210
211 if (!pgd_present(*pgd_k))
212 return NULL;
213
214 /*
215 * set_pgd(pgd, *pgd_k); here would be useless on PAE
216 * and redundant with the set_pmd() on non-PAE. As would
217 * set_pud.
218 */
219 pud = pud_offset(pgd, address);
220 pud_k = pud_offset(pgd_k, address);
221 if (!pud_present(*pud_k))
222 return NULL;
223
224 pmd = pmd_offset(pud, address);
225 pmd_k = pmd_offset(pud_k, address);
226 if (!pmd_present(*pmd_k))
227 return NULL;
228
229 if (!pmd_present(*pmd)) {
230 set_pmd(pmd, *pmd_k);
231 arch_flush_lazy_mmu_mode();
232 } else {
233 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
234 }
235
236 return pmd_k;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100237}
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100238
239void vmalloc_sync_all(void)
240{
241 unsigned long address;
242
243 if (SHARED_KERNEL_PMD)
244 return;
245
246 for (address = VMALLOC_START & PMD_MASK;
247 address >= TASK_SIZE && address < FIXADDR_TOP;
248 address += PMD_SIZE) {
249
250 unsigned long flags;
251 struct page *page;
252
253 spin_lock_irqsave(&pgd_lock, flags);
254 list_for_each_entry(page, &pgd_list, lru) {
255 if (!vmalloc_sync_one(page_address(page), address))
256 break;
257 }
258 spin_unlock_irqrestore(&pgd_lock, flags);
259 }
260}
261
262/*
263 * 32-bit:
264 *
265 * Handle a fault on the vmalloc or module mapping area
266 */
267static noinline int vmalloc_fault(unsigned long address)
268{
269 unsigned long pgd_paddr;
270 pmd_t *pmd_k;
271 pte_t *pte_k;
272
273 /* Make sure we are in vmalloc area: */
274 if (!(address >= VMALLOC_START && address < VMALLOC_END))
275 return -1;
276
277 /*
278 * Synchronize this task's top level page-table
279 * with the 'reference' page table.
280 *
281 * Do _not_ use "current" here. We might be inside
282 * an interrupt in the middle of a task switch..
283 */
284 pgd_paddr = read_cr3();
285 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
286 if (!pmd_k)
287 return -1;
288
289 pte_k = pte_offset_kernel(pmd_k, address);
290 if (!pte_present(*pte_k))
291 return -1;
292
293 return 0;
294}
295
296/*
297 * Did it hit the DOS screen memory VA from vm86 mode?
298 */
299static inline void
300check_v8086_mode(struct pt_regs *regs, unsigned long address,
301 struct task_struct *tsk)
302{
303 unsigned long bit;
304
305 if (!v8086_mode(regs))
306 return;
307
308 bit = (address - 0xA0000) >> PAGE_SHIFT;
309 if (bit < 32)
310 tsk->thread.screen_bitmap |= 1 << bit;
311}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Adrian Bunkcae30f82008-02-13 23:31:31 +0200313static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Harvey Harrison1156e092008-01-30 13:34:10 +0100315 __typeof__(pte_val(__pte(0))) page;
316
317 page = read_cr3();
318 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100319
Harvey Harrison1156e092008-01-30 13:34:10 +0100320#ifdef CONFIG_X86_PAE
321 printk("*pdpt = %016Lx ", page);
322 if ((page >> PAGE_SHIFT) < max_low_pfn
323 && page & _PAGE_PRESENT) {
324 page &= PAGE_MASK;
325 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100326 & (PTRS_PER_PMD - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100327 printk(KERN_CONT "*pde = %016Lx ", page);
328 page &= ~_PAGE_NX;
329 }
330#else
331 printk("*pde = %08lx ", page);
332#endif
333
334 /*
335 * We must not directly access the pte in the highpte
336 * case if the page table is located in highmem.
337 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100338 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100339 */
340 if ((page >> PAGE_SHIFT) < max_low_pfn
341 && (page & _PAGE_PRESENT)
342 && !(page & _PAGE_PSE)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100343
Harvey Harrison1156e092008-01-30 13:34:10 +0100344 page &= PAGE_MASK;
345 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100346 & (PTRS_PER_PTE - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100347 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
348 }
349
350 printk("\n");
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100351}
352
353#else /* CONFIG_X86_64: */
354
355void vmalloc_sync_all(void)
356{
357 unsigned long address;
358
359 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
360 address += PGDIR_SIZE) {
361
362 const pgd_t *pgd_ref = pgd_offset_k(address);
363 unsigned long flags;
364 struct page *page;
365
366 if (pgd_none(*pgd_ref))
367 continue;
368
369 spin_lock_irqsave(&pgd_lock, flags);
370 list_for_each_entry(page, &pgd_list, lru) {
371 pgd_t *pgd;
372 pgd = (pgd_t *)page_address(page) + pgd_index(address);
373 if (pgd_none(*pgd))
374 set_pgd(pgd, *pgd_ref);
375 else
376 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
377 }
378 spin_unlock_irqrestore(&pgd_lock, flags);
379 }
380}
381
382/*
383 * 64-bit:
384 *
385 * Handle a fault on the vmalloc area
386 *
387 * This assumes no large pages in there.
388 */
389static noinline int vmalloc_fault(unsigned long address)
390{
391 pgd_t *pgd, *pgd_ref;
392 pud_t *pud, *pud_ref;
393 pmd_t *pmd, *pmd_ref;
394 pte_t *pte, *pte_ref;
395
396 /* Make sure we are in vmalloc area: */
397 if (!(address >= VMALLOC_START && address < VMALLOC_END))
398 return -1;
399
400 /*
401 * Copy kernel mappings over when needed. This can also
402 * happen within a race in page table update. In the later
403 * case just flush:
404 */
405 pgd = pgd_offset(current->active_mm, address);
406 pgd_ref = pgd_offset_k(address);
407 if (pgd_none(*pgd_ref))
408 return -1;
409
410 if (pgd_none(*pgd))
411 set_pgd(pgd, *pgd_ref);
412 else
413 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
414
415 /*
416 * Below here mismatches are bugs because these lower tables
417 * are shared:
418 */
419
420 pud = pud_offset(pgd, address);
421 pud_ref = pud_offset(pgd_ref, address);
422 if (pud_none(*pud_ref))
423 return -1;
424
425 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
426 BUG();
427
428 pmd = pmd_offset(pud, address);
429 pmd_ref = pmd_offset(pud_ref, address);
430 if (pmd_none(*pmd_ref))
431 return -1;
432
433 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
434 BUG();
435
436 pte_ref = pte_offset_kernel(pmd_ref, address);
437 if (!pte_present(*pte_ref))
438 return -1;
439
440 pte = pte_offset_kernel(pmd, address);
441
442 /*
443 * Don't use pte_page here, because the mappings can point
444 * outside mem_map, and the NUMA hash lookup cannot handle
445 * that:
446 */
447 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
448 BUG();
449
450 return 0;
451}
452
453static const char errata93_warning[] =
454KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
455KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
456KERN_ERR "******* Please consider a BIOS update.\n"
457KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
458
459/*
460 * No vm86 mode in 64-bit mode:
461 */
462static inline void
463check_v8086_mode(struct pt_regs *regs, unsigned long address,
464 struct task_struct *tsk)
465{
466}
467
468static int bad_address(void *p)
469{
470 unsigned long dummy;
471
472 return probe_kernel_address((unsigned long *)p, dummy);
473}
474
475static void dump_pagetable(unsigned long address)
476{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 pgd_t *pgd;
478 pud_t *pud;
479 pmd_t *pmd;
480 pte_t *pte;
481
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200482 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Harvey Harrison33cb5242008-01-30 13:32:19 +0100484 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 pgd += pgd_index(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100487 if (bad_address(pgd))
488 goto bad;
489
Jan Beulichd646bce2006-02-03 21:51:47 +0100490 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100491
492 if (!pgd_present(*pgd))
493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Andi Kleend2ae5b52006-06-26 13:57:56 +0200495 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100496 if (bad_address(pud))
497 goto bad;
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100500 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100501 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100504 if (bad_address(pmd))
505 goto bad;
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100508 if (!pmd_present(*pmd) || pmd_large(*pmd))
509 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100512 if (bad_address(pte))
513 goto bad;
514
Harvey Harrison33cb5242008-01-30 13:32:19 +0100515 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100516out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 printk("\n");
518 return;
519bad:
520 printk("BAD\n");
521}
522
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100523#endif /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100525/*
526 * Workaround for K8 erratum #93 & buggy BIOS.
527 *
528 * BIOS SMM functions are required to use a specific workaround
529 * to avoid corruption of the 64bit RIP register on C stepping K8.
530 *
531 * A lot of BIOS that didn't get tested properly miss this.
532 *
533 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
534 * Try to work around it here.
535 *
536 * Note we only handle faults in kernel here.
537 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100538 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100539static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100541#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100542 static int once;
543
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100544 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100546
Harvey Harrison33cb5242008-01-30 13:32:19 +0100547 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100551 if ((address >= (u64)_stext && address <= (u64)_etext) ||
552 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100553 if (!once) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100554 printk(errata93_warning);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100555 once = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100557 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 1;
559 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100560#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100562}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Harvey Harrison35f32662008-01-30 13:34:09 +0100564/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100565 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
566 * to illegal addresses >4GB.
567 *
568 * We catch this in the page fault handler because these addresses
569 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100570 * segment in LDT is compatibility mode.
571 */
572static int is_errata100(struct pt_regs *regs, unsigned long address)
573{
574#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100575 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100576 return 1;
577#endif
578 return 0;
579}
580
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100581static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
582{
583#ifdef CONFIG_X86_F00F_BUG
584 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100585
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100586 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100587 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100588 */
589 if (boot_cpu_data.f00f_bug) {
590 nr = (address - idt_descr.address) >> 3;
591
592 if (nr == 6) {
593 do_invalid_op(regs, 0);
594 return 1;
595 }
596 }
597#endif
598 return 0;
599}
600
Ingo Molnar8f766142009-02-20 23:00:29 +0100601static const char nx_warning[] = KERN_CRIT
602"kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
603
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100604static void
605show_fault_oops(struct pt_regs *regs, unsigned long error_code,
606 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100607{
Harvey Harrison1156e092008-01-30 13:34:10 +0100608 if (!oops_may_print())
609 return;
610
Harvey Harrison1156e092008-01-30 13:34:10 +0100611 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100612 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100613
Harvey Harrison1156e092008-01-30 13:34:10 +0100614 pte_t *pte = lookup_address(address, &level);
615
Ingo Molnar8f766142009-02-20 23:00:29 +0100616 if (pte && pte_present(*pte) && !pte_exec(*pte))
617 printk(nx_warning, current_uid());
Harvey Harrison1156e092008-01-30 13:34:10 +0100618 }
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100619
Harvey Harrison1156e092008-01-30 13:34:10 +0100620 printk(KERN_ALERT "BUG: unable to handle kernel ");
621 if (address < PAGE_SIZE)
622 printk(KERN_CONT "NULL pointer dereference");
623 else
624 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100625
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200626 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100627 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100628 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100629
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100630 dump_pagetable(address);
631}
632
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100633static noinline void
634pgtable_bad(struct pt_regs *regs, unsigned long error_code,
635 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100637 struct task_struct *tsk;
638 unsigned long flags;
639 int sig;
640
641 flags = oops_begin();
642 tsk = current;
643 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100646 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100648
649 tsk->thread.cr2 = address;
650 tsk->thread.trap_no = 14;
651 tsk->thread.error_code = error_code;
652
Jan Beulich22f59912008-01-30 13:31:23 +0100653 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200654 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100655
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200656 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100659static noinline void
660no_context(struct pt_regs *regs, unsigned long error_code,
661 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100662{
663 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100664 unsigned long *stackend;
Nick Piggin92181f12009-01-20 04:24:26 +0100665 unsigned long flags;
666 int sig;
Nick Piggin92181f12009-01-20 04:24:26 +0100667
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100668 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100669 if (fixup_exception(regs))
670 return;
671
672 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100673 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100674 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100675 * Valid to do another page fault here, because if this fault
676 * had been triggered by is_prefetch fixup_exception would have
677 * handled it.
678 *
679 * 64-bit:
680 *
681 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100682 */
683 if (is_prefetch(regs, error_code, address))
684 return;
685
686 if (is_errata93(regs, address))
687 return;
688
689 /*
690 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100691 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100692 */
Nick Piggin92181f12009-01-20 04:24:26 +0100693 flags = oops_begin();
Nick Piggin92181f12009-01-20 04:24:26 +0100694
695 show_fault_oops(regs, error_code, address);
696
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100697 stackend = end_of_stack(tsk);
Ingo Molnar19803072009-01-21 10:39:51 +0100698 if (*stackend != STACK_END_MAGIC)
699 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
700
Ingo Molnar1cc99542009-02-20 23:07:48 +0100701 tsk->thread.cr2 = address;
702 tsk->thread.trap_no = 14;
703 tsk->thread.error_code = error_code;
Nick Piggin92181f12009-01-20 04:24:26 +0100704
Nick Piggin92181f12009-01-20 04:24:26 +0100705 sig = SIGKILL;
706 if (__die("Oops", regs, error_code))
707 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100708
Nick Piggin92181f12009-01-20 04:24:26 +0100709 /* Executive summary in case the body of the oops scrolled away */
710 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100711
Nick Piggin92181f12009-01-20 04:24:26 +0100712 oops_end(flags, regs, sig);
Nick Piggin92181f12009-01-20 04:24:26 +0100713}
714
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100715/*
716 * Print out info about fatal segfaults, if the show_unhandled_signals
717 * sysctl is set:
718 */
719static inline void
720show_signal_msg(struct pt_regs *regs, unsigned long error_code,
721 unsigned long address, struct task_struct *tsk)
722{
723 if (!unhandled_signal(tsk, SIGSEGV))
724 return;
725
726 if (!printk_ratelimit())
727 return;
728
729 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
730 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
731 tsk->comm, task_pid_nr(tsk), address,
732 (void *)regs->ip, (void *)regs->sp, error_code);
733
734 print_vma_addr(KERN_CONT " in ", regs->ip);
735
736 printk(KERN_CONT "\n");
737}
738
739static void
740__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
741 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100742{
743 struct task_struct *tsk = current;
744
745 /* User mode accesses just cause a SIGSEGV */
746 if (error_code & PF_USER) {
747 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100748 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100749 */
750 local_irq_enable();
751
752 /*
753 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100754 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100755 */
756 if (is_prefetch(regs, error_code, address))
757 return;
758
759 if (is_errata100(regs, address))
760 return;
761
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100762 if (unlikely(show_unhandled_signals))
763 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100764
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100765 /* Kernel addresses are always protection faults: */
766 tsk->thread.cr2 = address;
767 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
768 tsk->thread.trap_no = 14;
769
Nick Piggin92181f12009-01-20 04:24:26 +0100770 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100771
Nick Piggin92181f12009-01-20 04:24:26 +0100772 return;
773 }
774
775 if (is_f00f_bug(regs, address))
776 return;
777
778 no_context(regs, error_code, address);
779}
780
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100781static noinline void
782bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
783 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100784{
785 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
786}
787
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100788static void
789__bad_area(struct pt_regs *regs, unsigned long error_code,
790 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100791{
792 struct mm_struct *mm = current->mm;
793
794 /*
795 * Something tried to access memory that isn't in our memory map..
796 * Fix it, but check if it's kernel or user first..
797 */
798 up_read(&mm->mmap_sem);
799
800 __bad_area_nosemaphore(regs, error_code, address, si_code);
801}
802
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100803static noinline void
804bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100805{
806 __bad_area(regs, error_code, address, SEGV_MAPERR);
807}
808
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100809static noinline void
810bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
811 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100812{
813 __bad_area(regs, error_code, address, SEGV_ACCERR);
814}
815
816/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100817static void
818out_of_memory(struct pt_regs *regs, unsigned long error_code,
819 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100820{
821 /*
822 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100823 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100824 */
825 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100826
Nick Piggin92181f12009-01-20 04:24:26 +0100827 pagefault_out_of_memory();
828}
829
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100830static void
831do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100832{
833 struct task_struct *tsk = current;
834 struct mm_struct *mm = tsk->mm;
835
836 up_read(&mm->mmap_sem);
837
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100838 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100839 if (!(error_code & PF_USER))
840 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100841
Ingo Molnarcd1b68f2009-02-20 23:39:02 +0100842 /* User-space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100843 if (is_prefetch(regs, error_code, address))
844 return;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100845
846 tsk->thread.cr2 = address;
847 tsk->thread.error_code = error_code;
848 tsk->thread.trap_no = 14;
849
Nick Piggin92181f12009-01-20 04:24:26 +0100850 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
851}
852
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100853static noinline void
854mm_fault_error(struct pt_regs *regs, unsigned long error_code,
855 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100856{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100857 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100858 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100859 } else {
860 if (fault & VM_FAULT_SIGBUS)
861 do_sigbus(regs, error_code, address);
862 else
863 BUG();
864 }
Nick Piggin92181f12009-01-20 04:24:26 +0100865}
866
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100867static int spurious_fault_check(unsigned long error_code, pte_t *pte)
868{
869 if ((error_code & PF_WRITE) && !pte_write(*pte))
870 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100871
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100872 if ((error_code & PF_INSTR) && !pte_exec(*pte))
873 return 0;
874
875 return 1;
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100879 * Handle a spurious fault caused by a stale TLB entry.
880 *
881 * This allows us to lazily refresh the TLB when increasing the
882 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
883 * eagerly is very expensive since that implies doing a full
884 * cross-processor TLB flush, even if no stale TLB entries exist
885 * on other processors.
886 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100887 * There are no security implications to leaving a stale TLB when
888 * increasing the permissions on a page.
889 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100890static noinline int
891spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100892{
893 pgd_t *pgd;
894 pud_t *pud;
895 pmd_t *pmd;
896 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500897 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100898
899 /* Reserved-bit violation or user access to kernel space? */
900 if (error_code & (PF_USER | PF_RSVD))
901 return 0;
902
903 pgd = init_mm.pgd + pgd_index(address);
904 if (!pgd_present(*pgd))
905 return 0;
906
907 pud = pud_offset(pgd, address);
908 if (!pud_present(*pud))
909 return 0;
910
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100911 if (pud_large(*pud))
912 return spurious_fault_check(error_code, (pte_t *) pud);
913
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100914 pmd = pmd_offset(pud, address);
915 if (!pmd_present(*pmd))
916 return 0;
917
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100918 if (pmd_large(*pmd))
919 return spurious_fault_check(error_code, (pte_t *) pmd);
920
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100921 pte = pte_offset_kernel(pmd, address);
922 if (!pte_present(*pte))
923 return 0;
924
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500925 ret = spurious_fault_check(error_code, pte);
926 if (!ret)
927 return 0;
928
929 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100930 * Make sure we have permissions in PMD.
931 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500932 */
933 ret = spurious_fault_check(error_code, (pte_t *) pmd);
934 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100935
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500936 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100937}
938
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200939int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100941static inline int
942access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100943{
944 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100945 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100946 if (unlikely(!(vma->vm_flags & VM_WRITE)))
947 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100948 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100949 }
950
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100951 /* read, present: */
952 if (unlikely(error_code & PF_PROT))
953 return 1;
954
955 /* read, not present: */
956 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
957 return 1;
958
Nick Piggin92181f12009-01-20 04:24:26 +0100959 return 0;
960}
961
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800962static int fault_in_kernel_space(unsigned long address)
963{
Ingo Molnard9517342009-02-20 23:32:28 +0100964 return address >= TASK_SIZE_MAX;
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
968 * This routine handles page faults. It determines the address,
969 * and the problem, and then passes it off to one of the appropriate
970 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Ingo Molnarc3731c62009-02-20 23:22:34 +0100972dotraplinkage void __kprobes
973do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100975 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100976 struct task_struct *tsk;
977 unsigned long address;
978 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100979 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100980 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100982 tsk = current;
983 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100984
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100985 prefetchw(&mm->mmap_sem);
986
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100987 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200988 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200990 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200991 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
993 /*
994 * We fault-in kernel-space virtual memory on-demand. The
995 * 'reference' page table is init_mm.pgd.
996 *
997 * NOTE! We MUST NOT take any locks for this case. We may
998 * be in an interrupt or a critical region, and should
999 * only copy the information from the master page table,
1000 * nothing more.
1001 *
1002 * This verifies that the fault happens in kernel space
1003 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +01001004 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -08001006 if (unlikely(fault_in_kernel_space(address))) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001007 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
1008 vmalloc_fault(address) >= 0)
1009 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001010
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001011 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +01001012 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001013 return;
1014
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001015 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001016 if (notify_page_fault(regs))
1017 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001018 /*
1019 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001020 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001021 */
Nick Piggin92181f12009-01-20 04:24:26 +01001022 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001023
Nick Piggin92181f12009-01-20 04:24:26 +01001024 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001025 }
1026
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001027 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +01001028 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001029 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001030 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -07001031 * It's safe to allow irq's after cr2 has been saved and the
1032 * vmalloc fault has been handled.
1033 *
1034 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001035 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001036 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001037 if (user_mode_vm(regs)) {
1038 local_irq_enable();
1039 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001040 } else {
1041 if (regs->flags & X86_EFLAGS_IF)
1042 local_irq_enable();
1043 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001044
Andi Kleen66c58152006-01-11 22:44:09 +01001045 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001046 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Peter Zijlstra7dd1fcc2009-03-13 12:21:33 +01001048 perf_swcounter_event(PERF_COUNT_PAGE_FAULTS, 1, 0, regs);
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001051 * If we're in an interrupt, have no user context or are running
1052 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 */
Nick Piggin92181f12009-01-20 04:24:26 +01001054 if (unlikely(in_atomic() || !mm)) {
1055 bad_area_nosemaphore(regs, error_code, address);
1056 return;
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001059 /*
1060 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001061 * addresses in user space. All other faults represent errors in
1062 * the kernel and should generate an OOPS. Unfortunately, in the
1063 * case of an erroneous fault occurring in a code path which already
1064 * holds mmap_sem we will deadlock attempting to validate the fault
1065 * against the address space. Luckily the kernel only validly
1066 * references user space from well defined areas of code, which are
1067 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 *
1069 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001070 * the source reference check when there is a possibility of a
1071 * deadlock. Attempt to lock the address space, if we cannot we then
1072 * validate the source. If this is invalid we can skip the address
1073 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 */
Nick Piggin92181f12009-01-20 04:24:26 +01001075 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001076 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001077 !search_exception_tables(regs->ip)) {
1078 bad_area_nosemaphore(regs, error_code, address);
1079 return;
1080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001082 } else {
1083 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001084 * The above down_read_trylock() might have succeeded in
1085 * which case we'll have missed the might_sleep() from
1086 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001087 */
1088 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
1091 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001092 if (unlikely(!vma)) {
1093 bad_area(regs, error_code, address);
1094 return;
1095 }
1096 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001098 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1099 bad_area(regs, error_code, address);
1100 return;
1101 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001102 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001103 /*
1104 * Accessing the stack below %sp is always a bug.
1105 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001106 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001107 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001108 */
Nick Piggin92181f12009-01-20 04:24:26 +01001109 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1110 bad_area(regs, error_code, address);
1111 return;
1112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Nick Piggin92181f12009-01-20 04:24:26 +01001114 if (unlikely(expand_stack(vma, address))) {
1115 bad_area(regs, error_code, address);
1116 return;
1117 }
1118
1119 /*
1120 * Ok, we have a good vm_area for this memory access, so
1121 * we can handle it..
1122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001124 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001125
Nick Piggin92181f12009-01-20 04:24:26 +01001126 if (unlikely(access_error(error_code, write, vma))) {
1127 bad_area_access_error(regs, error_code, address);
1128 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
1130
1131 /*
1132 * If for any reason at all we couldn't handle the fault,
1133 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001134 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Nick Piggin83c54072007-07-19 01:47:05 -07001136 fault = handle_mm_fault(mm, vma, address, write);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001137
Nick Piggin83c54072007-07-19 01:47:05 -07001138 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001139 mm_fault_error(regs, error_code, address, fault);
1140 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001142
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001143 if (fault & VM_FAULT_MAJOR) {
Nick Piggin83c54072007-07-19 01:47:05 -07001144 tsk->maj_flt++;
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001145 perf_swcounter_event(PERF_COUNT_PAGE_FAULTS_MAJ, 1, 0, regs);
1146 } else {
Nick Piggin83c54072007-07-19 01:47:05 -07001147 tsk->min_flt++;
Peter Zijlstraac17dc82009-03-13 12:21:34 +01001148 perf_swcounter_event(PERF_COUNT_PAGE_FAULTS_MIN, 1, 0, regs);
1149 }
Harvey Harrisond729ab32008-01-30 13:33:23 +01001150
Ingo Molnar8c938f92009-02-20 22:12:18 +01001151 check_v8086_mode(regs, address, tsk);
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}