blob: 9bb07d331c3beabd9b2328650bdf0634b532b84a [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm-generic/sections.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010032
33#include <asm/tlbflush.h>
34#include <asm/pgalloc.h>
35#include <asm/segment.h>
36#include <asm/system.h>
37#include <asm/proto.h>
Jaswinder Singh70ef5642008-07-23 17:36:37 +053038#include <asm/traps.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010039#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Harvey Harrison33cb5242008-01-30 13:32:19 +010041/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010042 * Page fault error code bits:
43 *
44 * bit 0 == 0: no page found 1: protection fault
45 * bit 1 == 0: read access 1: write access
46 * bit 2 == 0: kernel-mode access 1: user-mode access
47 * bit 3 == 1: use of reserved bit detected
48 * bit 4 == 1: fault was an instruction fetch
Harvey Harrison33cb5242008-01-30 13:32:19 +010049 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +010050enum x86_pf_error_code {
51
52 PF_PROT = 1 << 0,
53 PF_WRITE = 1 << 1,
54 PF_USER = 1 << 2,
55 PF_RSVD = 1 << 3,
56 PF_INSTR = 1 << 4,
57};
Andi Kleen66c58152006-01-11 22:44:09 +010058
Ingo Molnarb814d412009-02-20 22:32:10 +010059/*
60 * (returns 0 if mmiotrace is disabled)
61 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020062static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020063{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020064 if (unlikely(is_kmmio_active()))
65 if (kmmio_handler(regs, addr) == 1)
66 return -1;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020067 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020068}
69
Christoph Hellwig74a0b572007-10-16 01:24:07 -070070static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070071{
Christoph Hellwig74a0b572007-10-16 01:24:07 -070072 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070073
Christoph Hellwig74a0b572007-10-16 01:24:07 -070074 /* kprobe_running() needs smp_processor_id() */
Ingo Molnarb1801812009-02-20 22:42:57 +010075 if (kprobes_built_in() && !user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070076 preempt_disable();
77 if (kprobe_running() && kprobe_fault_handler(regs, 14))
78 ret = 1;
79 preempt_enable();
80 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070081
Christoph Hellwig74a0b572007-10-16 01:24:07 -070082 return ret;
Harvey Harrison33cb5242008-01-30 13:32:19 +010083}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070084
Harvey Harrison1dc85be2008-01-30 13:32:35 +010085/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010086 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010087 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010088 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010089 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010090 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
91 * Check that here and ignore it.
92 *
93 * 64-bit mode:
94 *
95 * Sometimes the CPU reports invalid exceptions on prefetch.
96 * Check that here and ignore it.
97 *
98 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +010099 */
Ingo Molnar107a0362009-02-20 20:37:05 +0100100static inline int
101check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
102 unsigned char opcode, int *prefetch)
103{
104 unsigned char instr_hi = opcode & 0xf0;
105 unsigned char instr_lo = opcode & 0x0f;
106
107 switch (instr_hi) {
108 case 0x20:
109 case 0x30:
110 /*
111 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
112 * In X86_64 long mode, the CPU will signal invalid
113 * opcode if some of these prefixes are present so
114 * X86_64 will never get here anyway
115 */
116 return ((instr_lo & 7) == 0x6);
117#ifdef CONFIG_X86_64
118 case 0x40:
119 /*
120 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
121 * Need to figure out under what instruction mode the
122 * instruction was issued. Could check the LDT for lm,
123 * but for now it's good enough to assume that long
124 * mode only uses well known segments or kernel.
125 */
126 return (!user_mode(regs)) || (regs->cs == __USER_CS);
127#endif
128 case 0x60:
129 /* 0x64 thru 0x67 are valid prefixes in all modes. */
130 return (instr_lo & 0xC) == 0x4;
131 case 0xF0:
132 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
133 return !instr_lo || (instr_lo>>1) == 1;
134 case 0x00:
135 /* Prefetch instruction is 0x0F0D or 0x0F18 */
136 if (probe_kernel_address(instr, opcode))
137 return 0;
138
139 *prefetch = (instr_lo == 0xF) &&
140 (opcode == 0x0D || opcode == 0x18);
141 return 0;
142 default:
143 return 0;
144 }
145}
146
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100147static int
148is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100149{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100150 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100151 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100152 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ingo Molnar30853542008-03-27 21:29:09 +0100154 /*
155 * If it was a exec (instruction fetch) fault on NX page, then
156 * do not ignore the fault:
157 */
Andi Kleen66c58152006-01-11 22:44:09 +0100158 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100160
Ingo Molnar107a0362009-02-20 20:37:05 +0100161 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700162 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700164 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0;
166
Ingo Molnar107a0362009-02-20 20:37:05 +0100167 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100168 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100170 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100171 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 instr++;
174
Ingo Molnar107a0362009-02-20 20:37:05 +0100175 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 return prefetch;
179}
180
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100181static void
182force_sig_info_fault(int si_signo, int si_code, unsigned long address,
183 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100184{
185 siginfo_t info;
186
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100187 info.si_signo = si_signo;
188 info.si_errno = 0;
189 info.si_code = si_code;
190 info.si_addr = (void __user *)address;
191
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100192 force_sig_info(si_signo, &info, tsk);
193}
194
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100195DEFINE_SPINLOCK(pgd_lock);
196LIST_HEAD(pgd_list);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100197
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100198#ifdef CONFIG_X86_32
199static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
200{
201 unsigned index = pgd_index(address);
202 pgd_t *pgd_k;
203 pud_t *pud, *pud_k;
204 pmd_t *pmd, *pmd_k;
205
206 pgd += index;
207 pgd_k = init_mm.pgd + index;
208
209 if (!pgd_present(*pgd_k))
210 return NULL;
211
212 /*
213 * set_pgd(pgd, *pgd_k); here would be useless on PAE
214 * and redundant with the set_pmd() on non-PAE. As would
215 * set_pud.
216 */
217 pud = pud_offset(pgd, address);
218 pud_k = pud_offset(pgd_k, address);
219 if (!pud_present(*pud_k))
220 return NULL;
221
222 pmd = pmd_offset(pud, address);
223 pmd_k = pmd_offset(pud_k, address);
224 if (!pmd_present(*pmd_k))
225 return NULL;
226
227 if (!pmd_present(*pmd)) {
228 set_pmd(pmd, *pmd_k);
229 arch_flush_lazy_mmu_mode();
230 } else {
231 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
232 }
233
234 return pmd_k;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100235}
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100236
237void vmalloc_sync_all(void)
238{
239 unsigned long address;
240
241 if (SHARED_KERNEL_PMD)
242 return;
243
244 for (address = VMALLOC_START & PMD_MASK;
245 address >= TASK_SIZE && address < FIXADDR_TOP;
246 address += PMD_SIZE) {
247
248 unsigned long flags;
249 struct page *page;
250
251 spin_lock_irqsave(&pgd_lock, flags);
252 list_for_each_entry(page, &pgd_list, lru) {
253 if (!vmalloc_sync_one(page_address(page), address))
254 break;
255 }
256 spin_unlock_irqrestore(&pgd_lock, flags);
257 }
258}
259
260/*
261 * 32-bit:
262 *
263 * Handle a fault on the vmalloc or module mapping area
264 */
265static noinline int vmalloc_fault(unsigned long address)
266{
267 unsigned long pgd_paddr;
268 pmd_t *pmd_k;
269 pte_t *pte_k;
270
271 /* Make sure we are in vmalloc area: */
272 if (!(address >= VMALLOC_START && address < VMALLOC_END))
273 return -1;
274
275 /*
276 * Synchronize this task's top level page-table
277 * with the 'reference' page table.
278 *
279 * Do _not_ use "current" here. We might be inside
280 * an interrupt in the middle of a task switch..
281 */
282 pgd_paddr = read_cr3();
283 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
284 if (!pmd_k)
285 return -1;
286
287 pte_k = pte_offset_kernel(pmd_k, address);
288 if (!pte_present(*pte_k))
289 return -1;
290
291 return 0;
292}
293
294/*
295 * Did it hit the DOS screen memory VA from vm86 mode?
296 */
297static inline void
298check_v8086_mode(struct pt_regs *regs, unsigned long address,
299 struct task_struct *tsk)
300{
301 unsigned long bit;
302
303 if (!v8086_mode(regs))
304 return;
305
306 bit = (address - 0xA0000) >> PAGE_SHIFT;
307 if (bit < 32)
308 tsk->thread.screen_bitmap |= 1 << bit;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Adrian Bunkcae30f82008-02-13 23:31:31 +0200311static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Harvey Harrison1156e092008-01-30 13:34:10 +0100313 __typeof__(pte_val(__pte(0))) page;
314
315 page = read_cr3();
316 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100317
Harvey Harrison1156e092008-01-30 13:34:10 +0100318#ifdef CONFIG_X86_PAE
319 printk("*pdpt = %016Lx ", page);
320 if ((page >> PAGE_SHIFT) < max_low_pfn
321 && page & _PAGE_PRESENT) {
322 page &= PAGE_MASK;
323 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100324 & (PTRS_PER_PMD - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100325 printk(KERN_CONT "*pde = %016Lx ", page);
326 page &= ~_PAGE_NX;
327 }
328#else
329 printk("*pde = %08lx ", page);
330#endif
331
332 /*
333 * We must not directly access the pte in the highpte
334 * case if the page table is located in highmem.
335 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100336 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100337 */
338 if ((page >> PAGE_SHIFT) < max_low_pfn
339 && (page & _PAGE_PRESENT)
340 && !(page & _PAGE_PSE)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100341
Harvey Harrison1156e092008-01-30 13:34:10 +0100342 page &= PAGE_MASK;
343 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100344 & (PTRS_PER_PTE - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100345 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
346 }
347
348 printk("\n");
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100349}
350
351#else /* CONFIG_X86_64: */
352
353void vmalloc_sync_all(void)
354{
355 unsigned long address;
356
357 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
358 address += PGDIR_SIZE) {
359
360 const pgd_t *pgd_ref = pgd_offset_k(address);
361 unsigned long flags;
362 struct page *page;
363
364 if (pgd_none(*pgd_ref))
365 continue;
366
367 spin_lock_irqsave(&pgd_lock, flags);
368 list_for_each_entry(page, &pgd_list, lru) {
369 pgd_t *pgd;
370 pgd = (pgd_t *)page_address(page) + pgd_index(address);
371 if (pgd_none(*pgd))
372 set_pgd(pgd, *pgd_ref);
373 else
374 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
375 }
376 spin_unlock_irqrestore(&pgd_lock, flags);
377 }
378}
379
380/*
381 * 64-bit:
382 *
383 * Handle a fault on the vmalloc area
384 *
385 * This assumes no large pages in there.
386 */
387static noinline int vmalloc_fault(unsigned long address)
388{
389 pgd_t *pgd, *pgd_ref;
390 pud_t *pud, *pud_ref;
391 pmd_t *pmd, *pmd_ref;
392 pte_t *pte, *pte_ref;
393
394 /* Make sure we are in vmalloc area: */
395 if (!(address >= VMALLOC_START && address < VMALLOC_END))
396 return -1;
397
398 /*
399 * Copy kernel mappings over when needed. This can also
400 * happen within a race in page table update. In the later
401 * case just flush:
402 */
403 pgd = pgd_offset(current->active_mm, address);
404 pgd_ref = pgd_offset_k(address);
405 if (pgd_none(*pgd_ref))
406 return -1;
407
408 if (pgd_none(*pgd))
409 set_pgd(pgd, *pgd_ref);
410 else
411 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
412
413 /*
414 * Below here mismatches are bugs because these lower tables
415 * are shared:
416 */
417
418 pud = pud_offset(pgd, address);
419 pud_ref = pud_offset(pgd_ref, address);
420 if (pud_none(*pud_ref))
421 return -1;
422
423 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
424 BUG();
425
426 pmd = pmd_offset(pud, address);
427 pmd_ref = pmd_offset(pud_ref, address);
428 if (pmd_none(*pmd_ref))
429 return -1;
430
431 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
432 BUG();
433
434 pte_ref = pte_offset_kernel(pmd_ref, address);
435 if (!pte_present(*pte_ref))
436 return -1;
437
438 pte = pte_offset_kernel(pmd, address);
439
440 /*
441 * Don't use pte_page here, because the mappings can point
442 * outside mem_map, and the NUMA hash lookup cannot handle
443 * that:
444 */
445 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
446 BUG();
447
448 return 0;
449}
450
451static const char errata93_warning[] =
452KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
453KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
454KERN_ERR "******* Please consider a BIOS update.\n"
455KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
456
457/*
458 * No vm86 mode in 64-bit mode:
459 */
460static inline void
461check_v8086_mode(struct pt_regs *regs, unsigned long address,
462 struct task_struct *tsk)
463{
464}
465
466static int bad_address(void *p)
467{
468 unsigned long dummy;
469
470 return probe_kernel_address((unsigned long *)p, dummy);
471}
472
473static void dump_pagetable(unsigned long address)
474{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 pgd_t *pgd;
476 pud_t *pud;
477 pmd_t *pmd;
478 pte_t *pte;
479
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200480 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Harvey Harrison33cb5242008-01-30 13:32:19 +0100482 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 pgd += pgd_index(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100485 if (bad_address(pgd))
486 goto bad;
487
Jan Beulichd646bce2006-02-03 21:51:47 +0100488 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100489
490 if (!pgd_present(*pgd))
491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Andi Kleend2ae5b52006-06-26 13:57:56 +0200493 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100494 if (bad_address(pud))
495 goto bad;
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100498 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100499 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100502 if (bad_address(pmd))
503 goto bad;
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100506 if (!pmd_present(*pmd) || pmd_large(*pmd))
507 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100510 if (bad_address(pte))
511 goto bad;
512
Harvey Harrison33cb5242008-01-30 13:32:19 +0100513 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100514out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 printk("\n");
516 return;
517bad:
518 printk("BAD\n");
519}
520
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100521#endif /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100523/*
524 * Workaround for K8 erratum #93 & buggy BIOS.
525 *
526 * BIOS SMM functions are required to use a specific workaround
527 * to avoid corruption of the 64bit RIP register on C stepping K8.
528 *
529 * A lot of BIOS that didn't get tested properly miss this.
530 *
531 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
532 * Try to work around it here.
533 *
534 * Note we only handle faults in kernel here.
535 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100536 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100537static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100539#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100540 static int once;
541
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100542 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100544
Harvey Harrison33cb5242008-01-30 13:32:19 +0100545 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100549 if ((address >= (u64)_stext && address <= (u64)_etext) ||
550 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100551 if (!once) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100552 printk(errata93_warning);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100553 once = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100555 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return 1;
557 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100558#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100560}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Harvey Harrison35f32662008-01-30 13:34:09 +0100562/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100563 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
564 * to illegal addresses >4GB.
565 *
566 * We catch this in the page fault handler because these addresses
567 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100568 * segment in LDT is compatibility mode.
569 */
570static int is_errata100(struct pt_regs *regs, unsigned long address)
571{
572#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100573 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100574 return 1;
575#endif
576 return 0;
577}
578
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100579static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
580{
581#ifdef CONFIG_X86_F00F_BUG
582 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100583
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100584 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100585 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100586 */
587 if (boot_cpu_data.f00f_bug) {
588 nr = (address - idt_descr.address) >> 3;
589
590 if (nr == 6) {
591 do_invalid_op(regs, 0);
592 return 1;
593 }
594 }
595#endif
596 return 0;
597}
598
Ingo Molnar8f766142009-02-20 23:00:29 +0100599static const char nx_warning[] = KERN_CRIT
600"kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
601
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100602static void
603show_fault_oops(struct pt_regs *regs, unsigned long error_code,
604 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100605{
Harvey Harrison1156e092008-01-30 13:34:10 +0100606 if (!oops_may_print())
607 return;
608
Harvey Harrison1156e092008-01-30 13:34:10 +0100609 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100610 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100611
Harvey Harrison1156e092008-01-30 13:34:10 +0100612 pte_t *pte = lookup_address(address, &level);
613
Ingo Molnar8f766142009-02-20 23:00:29 +0100614 if (pte && pte_present(*pte) && !pte_exec(*pte))
615 printk(nx_warning, current_uid());
Harvey Harrison1156e092008-01-30 13:34:10 +0100616 }
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100617
Harvey Harrison1156e092008-01-30 13:34:10 +0100618 printk(KERN_ALERT "BUG: unable to handle kernel ");
619 if (address < PAGE_SIZE)
620 printk(KERN_CONT "NULL pointer dereference");
621 else
622 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100623
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200624 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100625 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100626 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100627
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100628 dump_pagetable(address);
629}
630
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100631static noinline void
632pgtable_bad(struct pt_regs *regs, unsigned long error_code,
633 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100635 struct task_struct *tsk;
636 unsigned long flags;
637 int sig;
638
639 flags = oops_begin();
640 tsk = current;
641 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100644 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100646
647 tsk->thread.cr2 = address;
648 tsk->thread.trap_no = 14;
649 tsk->thread.error_code = error_code;
650
Jan Beulich22f59912008-01-30 13:31:23 +0100651 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200652 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100653
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200654 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100657static noinline void
658no_context(struct pt_regs *regs, unsigned long error_code,
659 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100660{
661 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100662 unsigned long *stackend;
Nick Piggin92181f12009-01-20 04:24:26 +0100663 unsigned long flags;
664 int sig;
Nick Piggin92181f12009-01-20 04:24:26 +0100665
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100666 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100667 if (fixup_exception(regs))
668 return;
669
670 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100671 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100672 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100673 * Valid to do another page fault here, because if this fault
674 * had been triggered by is_prefetch fixup_exception would have
675 * handled it.
676 *
677 * 64-bit:
678 *
679 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100680 */
681 if (is_prefetch(regs, error_code, address))
682 return;
683
684 if (is_errata93(regs, address))
685 return;
686
687 /*
688 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100689 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100690 */
Nick Piggin92181f12009-01-20 04:24:26 +0100691 flags = oops_begin();
Nick Piggin92181f12009-01-20 04:24:26 +0100692
693 show_fault_oops(regs, error_code, address);
694
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100695 stackend = end_of_stack(tsk);
Ingo Molnar19803072009-01-21 10:39:51 +0100696 if (*stackend != STACK_END_MAGIC)
697 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
698
Ingo Molnar1cc99542009-02-20 23:07:48 +0100699 tsk->thread.cr2 = address;
700 tsk->thread.trap_no = 14;
701 tsk->thread.error_code = error_code;
Nick Piggin92181f12009-01-20 04:24:26 +0100702
Nick Piggin92181f12009-01-20 04:24:26 +0100703 sig = SIGKILL;
704 if (__die("Oops", regs, error_code))
705 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100706
Nick Piggin92181f12009-01-20 04:24:26 +0100707 /* Executive summary in case the body of the oops scrolled away */
708 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100709
Nick Piggin92181f12009-01-20 04:24:26 +0100710 oops_end(flags, regs, sig);
Nick Piggin92181f12009-01-20 04:24:26 +0100711}
712
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100713/*
714 * Print out info about fatal segfaults, if the show_unhandled_signals
715 * sysctl is set:
716 */
717static inline void
718show_signal_msg(struct pt_regs *regs, unsigned long error_code,
719 unsigned long address, struct task_struct *tsk)
720{
721 if (!unhandled_signal(tsk, SIGSEGV))
722 return;
723
724 if (!printk_ratelimit())
725 return;
726
727 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
728 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
729 tsk->comm, task_pid_nr(tsk), address,
730 (void *)regs->ip, (void *)regs->sp, error_code);
731
732 print_vma_addr(KERN_CONT " in ", regs->ip);
733
734 printk(KERN_CONT "\n");
735}
736
737static void
738__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
739 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100740{
741 struct task_struct *tsk = current;
742
743 /* User mode accesses just cause a SIGSEGV */
744 if (error_code & PF_USER) {
745 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100746 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100747 */
748 local_irq_enable();
749
750 /*
751 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100752 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100753 */
754 if (is_prefetch(regs, error_code, address))
755 return;
756
757 if (is_errata100(regs, address))
758 return;
759
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100760 if (unlikely(show_unhandled_signals))
761 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100762
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100763 /* Kernel addresses are always protection faults: */
764 tsk->thread.cr2 = address;
765 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
766 tsk->thread.trap_no = 14;
767
Nick Piggin92181f12009-01-20 04:24:26 +0100768 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100769
Nick Piggin92181f12009-01-20 04:24:26 +0100770 return;
771 }
772
773 if (is_f00f_bug(regs, address))
774 return;
775
776 no_context(regs, error_code, address);
777}
778
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100779static noinline void
780bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
781 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100782{
783 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
784}
785
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100786static void
787__bad_area(struct pt_regs *regs, unsigned long error_code,
788 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100789{
790 struct mm_struct *mm = current->mm;
791
792 /*
793 * Something tried to access memory that isn't in our memory map..
794 * Fix it, but check if it's kernel or user first..
795 */
796 up_read(&mm->mmap_sem);
797
798 __bad_area_nosemaphore(regs, error_code, address, si_code);
799}
800
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100801static noinline void
802bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100803{
804 __bad_area(regs, error_code, address, SEGV_MAPERR);
805}
806
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100807static noinline void
808bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
809 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100810{
811 __bad_area(regs, error_code, address, SEGV_ACCERR);
812}
813
814/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100815static void
816out_of_memory(struct pt_regs *regs, unsigned long error_code,
817 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100818{
819 /*
820 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100821 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100822 */
823 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100824
Nick Piggin92181f12009-01-20 04:24:26 +0100825 pagefault_out_of_memory();
826}
827
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100828static void
829do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100830{
831 struct task_struct *tsk = current;
832 struct mm_struct *mm = tsk->mm;
833
834 up_read(&mm->mmap_sem);
835
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100836 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100837 if (!(error_code & PF_USER))
838 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100839
Ingo Molnarcd1b68f2009-02-20 23:39:02 +0100840 /* User-space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100841 if (is_prefetch(regs, error_code, address))
842 return;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100843
844 tsk->thread.cr2 = address;
845 tsk->thread.error_code = error_code;
846 tsk->thread.trap_no = 14;
847
Nick Piggin92181f12009-01-20 04:24:26 +0100848 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
849}
850
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100851static noinline void
852mm_fault_error(struct pt_regs *regs, unsigned long error_code,
853 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100854{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100855 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100856 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100857 } else {
858 if (fault & VM_FAULT_SIGBUS)
859 do_sigbus(regs, error_code, address);
860 else
861 BUG();
862 }
Nick Piggin92181f12009-01-20 04:24:26 +0100863}
864
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100865static int spurious_fault_check(unsigned long error_code, pte_t *pte)
866{
867 if ((error_code & PF_WRITE) && !pte_write(*pte))
868 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100869
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100870 if ((error_code & PF_INSTR) && !pte_exec(*pte))
871 return 0;
872
873 return 1;
874}
875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100877 * Handle a spurious fault caused by a stale TLB entry.
878 *
879 * This allows us to lazily refresh the TLB when increasing the
880 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
881 * eagerly is very expensive since that implies doing a full
882 * cross-processor TLB flush, even if no stale TLB entries exist
883 * on other processors.
884 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100885 * There are no security implications to leaving a stale TLB when
886 * increasing the permissions on a page.
887 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100888static noinline int
889spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100890{
891 pgd_t *pgd;
892 pud_t *pud;
893 pmd_t *pmd;
894 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500895 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100896
897 /* Reserved-bit violation or user access to kernel space? */
898 if (error_code & (PF_USER | PF_RSVD))
899 return 0;
900
901 pgd = init_mm.pgd + pgd_index(address);
902 if (!pgd_present(*pgd))
903 return 0;
904
905 pud = pud_offset(pgd, address);
906 if (!pud_present(*pud))
907 return 0;
908
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100909 if (pud_large(*pud))
910 return spurious_fault_check(error_code, (pte_t *) pud);
911
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100912 pmd = pmd_offset(pud, address);
913 if (!pmd_present(*pmd))
914 return 0;
915
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100916 if (pmd_large(*pmd))
917 return spurious_fault_check(error_code, (pte_t *) pmd);
918
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100919 pte = pte_offset_kernel(pmd, address);
920 if (!pte_present(*pte))
921 return 0;
922
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500923 ret = spurious_fault_check(error_code, pte);
924 if (!ret)
925 return 0;
926
927 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100928 * Make sure we have permissions in PMD.
929 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500930 */
931 ret = spurious_fault_check(error_code, (pte_t *) pmd);
932 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100933
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500934 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100935}
936
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200937int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100939static inline int
940access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100941{
942 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100943 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100944 if (unlikely(!(vma->vm_flags & VM_WRITE)))
945 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100946 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100947 }
948
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100949 /* read, present: */
950 if (unlikely(error_code & PF_PROT))
951 return 1;
952
953 /* read, not present: */
954 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
955 return 1;
956
Nick Piggin92181f12009-01-20 04:24:26 +0100957 return 0;
958}
959
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800960static int fault_in_kernel_space(unsigned long address)
961{
Ingo Molnard9517342009-02-20 23:32:28 +0100962 return address >= TASK_SIZE_MAX;
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800963}
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/*
966 * This routine handles page faults. It determines the address,
967 * and the problem, and then passes it off to one of the appropriate
968 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Ingo Molnarc3731c62009-02-20 23:22:34 +0100970dotraplinkage void __kprobes
971do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100973 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100974 struct task_struct *tsk;
975 unsigned long address;
976 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100977 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100978 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100980 tsk = current;
981 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100982
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100983 prefetchw(&mm->mmap_sem);
984
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100985 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200986 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200988 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200989 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 /*
992 * We fault-in kernel-space virtual memory on-demand. The
993 * 'reference' page table is init_mm.pgd.
994 *
995 * NOTE! We MUST NOT take any locks for this case. We may
996 * be in an interrupt or a critical region, and should
997 * only copy the information from the master page table,
998 * nothing more.
999 *
1000 * This verifies that the fault happens in kernel space
1001 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +01001002 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -08001004 if (unlikely(fault_in_kernel_space(address))) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001005 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
1006 vmalloc_fault(address) >= 0)
1007 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001008
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001009 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +01001010 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001011 return;
1012
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001013 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001014 if (notify_page_fault(regs))
1015 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001016 /*
1017 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001018 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001019 */
Nick Piggin92181f12009-01-20 04:24:26 +01001020 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001021
Nick Piggin92181f12009-01-20 04:24:26 +01001022 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001023 }
1024
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001025 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +01001026 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001027 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001028 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -07001029 * It's safe to allow irq's after cr2 has been saved and the
1030 * vmalloc fault has been handled.
1031 *
1032 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001033 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001034 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001035 if (user_mode_vm(regs)) {
1036 local_irq_enable();
1037 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001038 } else {
1039 if (regs->flags & X86_EFLAGS_IF)
1040 local_irq_enable();
1041 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001042
Andi Kleen66c58152006-01-11 22:44:09 +01001043 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001044 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001047 * If we're in an interrupt, have no user context or are running
1048 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 */
Nick Piggin92181f12009-01-20 04:24:26 +01001050 if (unlikely(in_atomic() || !mm)) {
1051 bad_area_nosemaphore(regs, error_code, address);
1052 return;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001055 /*
1056 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001057 * addresses in user space. All other faults represent errors in
1058 * the kernel and should generate an OOPS. Unfortunately, in the
1059 * case of an erroneous fault occurring in a code path which already
1060 * holds mmap_sem we will deadlock attempting to validate the fault
1061 * against the address space. Luckily the kernel only validly
1062 * references user space from well defined areas of code, which are
1063 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 *
1065 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001066 * the source reference check when there is a possibility of a
1067 * deadlock. Attempt to lock the address space, if we cannot we then
1068 * validate the source. If this is invalid we can skip the address
1069 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
Nick Piggin92181f12009-01-20 04:24:26 +01001071 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001072 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001073 !search_exception_tables(regs->ip)) {
1074 bad_area_nosemaphore(regs, error_code, address);
1075 return;
1076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001078 } else {
1079 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001080 * The above down_read_trylock() might have succeeded in
1081 * which case we'll have missed the might_sleep() from
1082 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001083 */
1084 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
1086
1087 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001088 if (unlikely(!vma)) {
1089 bad_area(regs, error_code, address);
1090 return;
1091 }
1092 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001094 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1095 bad_area(regs, error_code, address);
1096 return;
1097 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001098 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001099 /*
1100 * Accessing the stack below %sp is always a bug.
1101 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001102 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001103 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001104 */
Nick Piggin92181f12009-01-20 04:24:26 +01001105 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1106 bad_area(regs, error_code, address);
1107 return;
1108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Nick Piggin92181f12009-01-20 04:24:26 +01001110 if (unlikely(expand_stack(vma, address))) {
1111 bad_area(regs, error_code, address);
1112 return;
1113 }
1114
1115 /*
1116 * Ok, we have a good vm_area for this memory access, so
1117 * we can handle it..
1118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001120 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001121
Nick Piggin92181f12009-01-20 04:24:26 +01001122 if (unlikely(access_error(error_code, write, vma))) {
1123 bad_area_access_error(regs, error_code, address);
1124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126
1127 /*
1128 * If for any reason at all we couldn't handle the fault,
1129 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001130 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 */
Nick Piggin83c54072007-07-19 01:47:05 -07001132 fault = handle_mm_fault(mm, vma, address, write);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001133
Nick Piggin83c54072007-07-19 01:47:05 -07001134 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001135 mm_fault_error(regs, error_code, address, fault);
1136 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001138
Nick Piggin83c54072007-07-19 01:47:05 -07001139 if (fault & VM_FAULT_MAJOR)
1140 tsk->maj_flt++;
1141 else
1142 tsk->min_flt++;
Harvey Harrisond729ab32008-01-30 13:33:23 +01001143
Ingo Molnar8c938f92009-02-20 22:12:18 +01001144 check_v8086_mode(regs, address, tsk);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147}