blob: cfbb4a7380116dffb8fc9237c94794542b83ce23 [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/*
Ingo Molnarb319eed2009-02-22 10:24:18 +010060 * Returns 0 if mmiotrace is disabled, or if the fault is not
61 * handled by mmiotrace:
Ingo Molnarb814d412009-02-20 22:32:10 +010062 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020063static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020064{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020065 if (unlikely(is_kmmio_active()))
66 if (kmmio_handler(regs, addr) == 1)
67 return -1;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020068 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020069}
70
Christoph Hellwig74a0b572007-10-16 01:24:07 -070071static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070072{
Christoph Hellwig74a0b572007-10-16 01:24:07 -070073 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070074
Christoph Hellwig74a0b572007-10-16 01:24:07 -070075 /* kprobe_running() needs smp_processor_id() */
Ingo Molnarb1801812009-02-20 22:42:57 +010076 if (kprobes_built_in() && !user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070077 preempt_disable();
78 if (kprobe_running() && kprobe_fault_handler(regs, 14))
79 ret = 1;
80 preempt_enable();
81 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070082
Christoph Hellwig74a0b572007-10-16 01:24:07 -070083 return ret;
Harvey Harrison33cb5242008-01-30 13:32:19 +010084}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070085
Harvey Harrison1dc85be2008-01-30 13:32:35 +010086/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010087 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010088 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010089 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010090 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010091 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
92 * Check that here and ignore it.
93 *
94 * 64-bit mode:
95 *
96 * Sometimes the CPU reports invalid exceptions on prefetch.
97 * Check that here and ignore it.
98 *
99 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100100 */
Ingo Molnar107a0362009-02-20 20:37:05 +0100101static inline int
102check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
103 unsigned char opcode, int *prefetch)
104{
105 unsigned char instr_hi = opcode & 0xf0;
106 unsigned char instr_lo = opcode & 0x0f;
107
108 switch (instr_hi) {
109 case 0x20:
110 case 0x30:
111 /*
112 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
113 * In X86_64 long mode, the CPU will signal invalid
114 * opcode if some of these prefixes are present so
115 * X86_64 will never get here anyway
116 */
117 return ((instr_lo & 7) == 0x6);
118#ifdef CONFIG_X86_64
119 case 0x40:
120 /*
121 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
122 * Need to figure out under what instruction mode the
123 * instruction was issued. Could check the LDT for lm,
124 * but for now it's good enough to assume that long
125 * mode only uses well known segments or kernel.
126 */
127 return (!user_mode(regs)) || (regs->cs == __USER_CS);
128#endif
129 case 0x60:
130 /* 0x64 thru 0x67 are valid prefixes in all modes. */
131 return (instr_lo & 0xC) == 0x4;
132 case 0xF0:
133 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
134 return !instr_lo || (instr_lo>>1) == 1;
135 case 0x00:
136 /* Prefetch instruction is 0x0F0D or 0x0F18 */
137 if (probe_kernel_address(instr, opcode))
138 return 0;
139
140 *prefetch = (instr_lo == 0xF) &&
141 (opcode == 0x0D || opcode == 0x18);
142 return 0;
143 default:
144 return 0;
145 }
146}
147
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100148static int
149is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100150{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100151 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100152 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100153 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Ingo Molnar30853542008-03-27 21:29:09 +0100155 /*
156 * If it was a exec (instruction fetch) fault on NX page, then
157 * do not ignore the fault:
158 */
Andi Kleen66c58152006-01-11 22:44:09 +0100159 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100161
Ingo Molnar107a0362009-02-20 20:37:05 +0100162 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700163 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700165 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return 0;
167
Ingo Molnar107a0362009-02-20 20:37:05 +0100168 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100169 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100171 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100172 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 instr++;
175
Ingo Molnar107a0362009-02-20 20:37:05 +0100176 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 return prefetch;
180}
181
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100182static void
183force_sig_info_fault(int si_signo, int si_code, unsigned long address,
184 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100185{
186 siginfo_t info;
187
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100188 info.si_signo = si_signo;
189 info.si_errno = 0;
190 info.si_code = si_code;
191 info.si_addr = (void __user *)address;
192
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100193 force_sig_info(si_signo, &info, tsk);
194}
195
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100196DEFINE_SPINLOCK(pgd_lock);
197LIST_HEAD(pgd_list);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100198
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100199#ifdef CONFIG_X86_32
200static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
201{
202 unsigned index = pgd_index(address);
203 pgd_t *pgd_k;
204 pud_t *pud, *pud_k;
205 pmd_t *pmd, *pmd_k;
206
207 pgd += index;
208 pgd_k = init_mm.pgd + index;
209
210 if (!pgd_present(*pgd_k))
211 return NULL;
212
213 /*
214 * set_pgd(pgd, *pgd_k); here would be useless on PAE
215 * and redundant with the set_pmd() on non-PAE. As would
216 * set_pud.
217 */
218 pud = pud_offset(pgd, address);
219 pud_k = pud_offset(pgd_k, address);
220 if (!pud_present(*pud_k))
221 return NULL;
222
223 pmd = pmd_offset(pud, address);
224 pmd_k = pmd_offset(pud_k, address);
225 if (!pmd_present(*pmd_k))
226 return NULL;
227
Jeremy Fitzhardingeb8bcfe92009-02-17 23:05:19 -0800228 if (!pmd_present(*pmd))
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100229 set_pmd(pmd, *pmd_k);
Jeremy Fitzhardingeb8bcfe92009-02-17 23:05:19 -0800230 else
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100231 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100232
233 return pmd_k;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100234}
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100235
236void vmalloc_sync_all(void)
237{
238 unsigned long address;
239
240 if (SHARED_KERNEL_PMD)
241 return;
242
243 for (address = VMALLOC_START & PMD_MASK;
244 address >= TASK_SIZE && address < FIXADDR_TOP;
245 address += PMD_SIZE) {
246
247 unsigned long flags;
248 struct page *page;
249
250 spin_lock_irqsave(&pgd_lock, flags);
251 list_for_each_entry(page, &pgd_list, lru) {
252 if (!vmalloc_sync_one(page_address(page), address))
253 break;
254 }
255 spin_unlock_irqrestore(&pgd_lock, flags);
256 }
257}
258
259/*
260 * 32-bit:
261 *
262 * Handle a fault on the vmalloc or module mapping area
263 */
264static noinline int vmalloc_fault(unsigned long address)
265{
266 unsigned long pgd_paddr;
267 pmd_t *pmd_k;
268 pte_t *pte_k;
269
270 /* Make sure we are in vmalloc area: */
271 if (!(address >= VMALLOC_START && address < VMALLOC_END))
272 return -1;
273
274 /*
275 * Synchronize this task's top level page-table
276 * with the 'reference' page table.
277 *
278 * Do _not_ use "current" here. We might be inside
279 * an interrupt in the middle of a task switch..
280 */
281 pgd_paddr = read_cr3();
282 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
283 if (!pmd_k)
284 return -1;
285
286 pte_k = pte_offset_kernel(pmd_k, address);
287 if (!pte_present(*pte_k))
288 return -1;
289
290 return 0;
291}
292
293/*
294 * Did it hit the DOS screen memory VA from vm86 mode?
295 */
296static inline void
297check_v8086_mode(struct pt_regs *regs, unsigned long address,
298 struct task_struct *tsk)
299{
300 unsigned long bit;
301
302 if (!v8086_mode(regs))
303 return;
304
305 bit = (address - 0xA0000) >> PAGE_SHIFT;
306 if (bit < 32)
307 tsk->thread.screen_bitmap |= 1 << bit;
308}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Adrian Bunkcae30f822008-02-13 23:31:31 +0200310static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Harvey Harrison1156e092008-01-30 13:34:10 +0100312 __typeof__(pte_val(__pte(0))) page;
313
314 page = read_cr3();
315 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100316
Harvey Harrison1156e092008-01-30 13:34:10 +0100317#ifdef CONFIG_X86_PAE
318 printk("*pdpt = %016Lx ", page);
319 if ((page >> PAGE_SHIFT) < max_low_pfn
320 && page & _PAGE_PRESENT) {
321 page &= PAGE_MASK;
322 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100323 & (PTRS_PER_PMD - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100324 printk(KERN_CONT "*pde = %016Lx ", page);
325 page &= ~_PAGE_NX;
326 }
327#else
328 printk("*pde = %08lx ", page);
329#endif
330
331 /*
332 * We must not directly access the pte in the highpte
333 * case if the page table is located in highmem.
334 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100335 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100336 */
337 if ((page >> PAGE_SHIFT) < max_low_pfn
338 && (page & _PAGE_PRESENT)
339 && !(page & _PAGE_PSE)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100340
Harvey Harrison1156e092008-01-30 13:34:10 +0100341 page &= PAGE_MASK;
342 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100343 & (PTRS_PER_PTE - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100344 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
345 }
346
347 printk("\n");
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100348}
349
350#else /* CONFIG_X86_64: */
351
352void vmalloc_sync_all(void)
353{
354 unsigned long address;
355
356 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
357 address += PGDIR_SIZE) {
358
359 const pgd_t *pgd_ref = pgd_offset_k(address);
360 unsigned long flags;
361 struct page *page;
362
363 if (pgd_none(*pgd_ref))
364 continue;
365
366 spin_lock_irqsave(&pgd_lock, flags);
367 list_for_each_entry(page, &pgd_list, lru) {
368 pgd_t *pgd;
369 pgd = (pgd_t *)page_address(page) + pgd_index(address);
370 if (pgd_none(*pgd))
371 set_pgd(pgd, *pgd_ref);
372 else
373 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
374 }
375 spin_unlock_irqrestore(&pgd_lock, flags);
376 }
377}
378
379/*
380 * 64-bit:
381 *
382 * Handle a fault on the vmalloc area
383 *
384 * This assumes no large pages in there.
385 */
386static noinline int vmalloc_fault(unsigned long address)
387{
388 pgd_t *pgd, *pgd_ref;
389 pud_t *pud, *pud_ref;
390 pmd_t *pmd, *pmd_ref;
391 pte_t *pte, *pte_ref;
392
393 /* Make sure we are in vmalloc area: */
394 if (!(address >= VMALLOC_START && address < VMALLOC_END))
395 return -1;
396
397 /*
398 * Copy kernel mappings over when needed. This can also
399 * happen within a race in page table update. In the later
400 * case just flush:
401 */
402 pgd = pgd_offset(current->active_mm, address);
403 pgd_ref = pgd_offset_k(address);
404 if (pgd_none(*pgd_ref))
405 return -1;
406
407 if (pgd_none(*pgd))
408 set_pgd(pgd, *pgd_ref);
409 else
410 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
411
412 /*
413 * Below here mismatches are bugs because these lower tables
414 * are shared:
415 */
416
417 pud = pud_offset(pgd, address);
418 pud_ref = pud_offset(pgd_ref, address);
419 if (pud_none(*pud_ref))
420 return -1;
421
422 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
423 BUG();
424
425 pmd = pmd_offset(pud, address);
426 pmd_ref = pmd_offset(pud_ref, address);
427 if (pmd_none(*pmd_ref))
428 return -1;
429
430 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
431 BUG();
432
433 pte_ref = pte_offset_kernel(pmd_ref, address);
434 if (!pte_present(*pte_ref))
435 return -1;
436
437 pte = pte_offset_kernel(pmd, address);
438
439 /*
440 * Don't use pte_page here, because the mappings can point
441 * outside mem_map, and the NUMA hash lookup cannot handle
442 * that:
443 */
444 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
445 BUG();
446
447 return 0;
448}
449
450static const char errata93_warning[] =
451KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
452KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
453KERN_ERR "******* Please consider a BIOS update.\n"
454KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
455
456/*
457 * No vm86 mode in 64-bit mode:
458 */
459static inline void
460check_v8086_mode(struct pt_regs *regs, unsigned long address,
461 struct task_struct *tsk)
462{
463}
464
465static int bad_address(void *p)
466{
467 unsigned long dummy;
468
469 return probe_kernel_address((unsigned long *)p, dummy);
470}
471
472static void dump_pagetable(unsigned long address)
473{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 pgd_t *pgd;
475 pud_t *pud;
476 pmd_t *pmd;
477 pte_t *pte;
478
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200479 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Harvey Harrison33cb5242008-01-30 13:32:19 +0100481 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 pgd += pgd_index(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100484 if (bad_address(pgd))
485 goto bad;
486
Jan Beulichd646bce2006-02-03 21:51:47 +0100487 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100488
489 if (!pgd_present(*pgd))
490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Andi Kleend2ae5b52006-06-26 13:57:56 +0200492 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100493 if (bad_address(pud))
494 goto bad;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100497 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100498 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100501 if (bad_address(pmd))
502 goto bad;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100505 if (!pmd_present(*pmd) || pmd_large(*pmd))
506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100509 if (bad_address(pte))
510 goto bad;
511
Harvey Harrison33cb5242008-01-30 13:32:19 +0100512 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100513out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 printk("\n");
515 return;
516bad:
517 printk("BAD\n");
518}
519
Ingo Molnarf2f13a82009-02-20 22:50:24 +0100520#endif /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100522/*
523 * Workaround for K8 erratum #93 & buggy BIOS.
524 *
525 * BIOS SMM functions are required to use a specific workaround
526 * to avoid corruption of the 64bit RIP register on C stepping K8.
527 *
528 * A lot of BIOS that didn't get tested properly miss this.
529 *
530 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
531 * Try to work around it here.
532 *
533 * Note we only handle faults in kernel here.
534 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100535 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100536static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100538#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100539 static int once;
540
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100541 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100543
Harvey Harrison33cb5242008-01-30 13:32:19 +0100544 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100548 if ((address >= (u64)_stext && address <= (u64)_etext) ||
549 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100550 if (!once) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100551 printk(errata93_warning);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100552 once = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100554 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return 1;
556 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100557#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100559}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Harvey Harrison35f32662008-01-30 13:34:09 +0100561/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100562 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
563 * to illegal addresses >4GB.
564 *
565 * We catch this in the page fault handler because these addresses
566 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100567 * segment in LDT is compatibility mode.
568 */
569static int is_errata100(struct pt_regs *regs, unsigned long address)
570{
571#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100572 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100573 return 1;
574#endif
575 return 0;
576}
577
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100578static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
579{
580#ifdef CONFIG_X86_F00F_BUG
581 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100582
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100583 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100584 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100585 */
586 if (boot_cpu_data.f00f_bug) {
587 nr = (address - idt_descr.address) >> 3;
588
589 if (nr == 6) {
590 do_invalid_op(regs, 0);
591 return 1;
592 }
593 }
594#endif
595 return 0;
596}
597
Ingo Molnar8f766142009-02-20 23:00:29 +0100598static const char nx_warning[] = KERN_CRIT
599"kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
600
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100601static void
602show_fault_oops(struct pt_regs *regs, unsigned long error_code,
603 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100604{
Harvey Harrison1156e092008-01-30 13:34:10 +0100605 if (!oops_may_print())
606 return;
607
Harvey Harrison1156e092008-01-30 13:34:10 +0100608 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100609 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100610
Harvey Harrison1156e092008-01-30 13:34:10 +0100611 pte_t *pte = lookup_address(address, &level);
612
Ingo Molnar8f766142009-02-20 23:00:29 +0100613 if (pte && pte_present(*pte) && !pte_exec(*pte))
614 printk(nx_warning, current_uid());
Harvey Harrison1156e092008-01-30 13:34:10 +0100615 }
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100616
Harvey Harrison1156e092008-01-30 13:34:10 +0100617 printk(KERN_ALERT "BUG: unable to handle kernel ");
618 if (address < PAGE_SIZE)
619 printk(KERN_CONT "NULL pointer dereference");
620 else
621 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100622
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200623 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100624 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100625 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100626
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100627 dump_pagetable(address);
628}
629
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100630static noinline void
631pgtable_bad(struct pt_regs *regs, unsigned long error_code,
632 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100634 struct task_struct *tsk;
635 unsigned long flags;
636 int sig;
637
638 flags = oops_begin();
639 tsk = current;
640 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100643 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100645
646 tsk->thread.cr2 = address;
647 tsk->thread.trap_no = 14;
648 tsk->thread.error_code = error_code;
649
Jan Beulich22f59912008-01-30 13:31:23 +0100650 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200651 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100652
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200653 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100656static noinline void
657no_context(struct pt_regs *regs, unsigned long error_code,
658 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100659{
660 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100661 unsigned long *stackend;
Nick Piggin92181f12009-01-20 04:24:26 +0100662 unsigned long flags;
663 int sig;
Nick Piggin92181f12009-01-20 04:24:26 +0100664
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100665 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100666 if (fixup_exception(regs))
667 return;
668
669 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100670 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100671 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100672 * Valid to do another page fault here, because if this fault
673 * had been triggered by is_prefetch fixup_exception would have
674 * handled it.
675 *
676 * 64-bit:
677 *
678 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100679 */
680 if (is_prefetch(regs, error_code, address))
681 return;
682
683 if (is_errata93(regs, address))
684 return;
685
686 /*
687 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100688 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100689 */
Nick Piggin92181f12009-01-20 04:24:26 +0100690 flags = oops_begin();
Nick Piggin92181f12009-01-20 04:24:26 +0100691
692 show_fault_oops(regs, error_code, address);
693
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100694 stackend = end_of_stack(tsk);
Ingo Molnar19803072009-01-21 10:39:51 +0100695 if (*stackend != STACK_END_MAGIC)
696 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
697
Ingo Molnar1cc99542009-02-20 23:07:48 +0100698 tsk->thread.cr2 = address;
699 tsk->thread.trap_no = 14;
700 tsk->thread.error_code = error_code;
Nick Piggin92181f12009-01-20 04:24:26 +0100701
Nick Piggin92181f12009-01-20 04:24:26 +0100702 sig = SIGKILL;
703 if (__die("Oops", regs, error_code))
704 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100705
Nick Piggin92181f12009-01-20 04:24:26 +0100706 /* Executive summary in case the body of the oops scrolled away */
707 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100708
Nick Piggin92181f12009-01-20 04:24:26 +0100709 oops_end(flags, regs, sig);
Nick Piggin92181f12009-01-20 04:24:26 +0100710}
711
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100712/*
713 * Print out info about fatal segfaults, if the show_unhandled_signals
714 * sysctl is set:
715 */
716static inline void
717show_signal_msg(struct pt_regs *regs, unsigned long error_code,
718 unsigned long address, struct task_struct *tsk)
719{
720 if (!unhandled_signal(tsk, SIGSEGV))
721 return;
722
723 if (!printk_ratelimit())
724 return;
725
726 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
727 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
728 tsk->comm, task_pid_nr(tsk), address,
729 (void *)regs->ip, (void *)regs->sp, error_code);
730
731 print_vma_addr(KERN_CONT " in ", regs->ip);
732
733 printk(KERN_CONT "\n");
734}
735
736static void
737__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
738 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100739{
740 struct task_struct *tsk = current;
741
742 /* User mode accesses just cause a SIGSEGV */
743 if (error_code & PF_USER) {
744 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100745 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100746 */
747 local_irq_enable();
748
749 /*
750 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100751 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100752 */
753 if (is_prefetch(regs, error_code, address))
754 return;
755
756 if (is_errata100(regs, address))
757 return;
758
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100759 if (unlikely(show_unhandled_signals))
760 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100761
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100762 /* Kernel addresses are always protection faults: */
763 tsk->thread.cr2 = address;
764 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
765 tsk->thread.trap_no = 14;
766
Nick Piggin92181f12009-01-20 04:24:26 +0100767 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100768
Nick Piggin92181f12009-01-20 04:24:26 +0100769 return;
770 }
771
772 if (is_f00f_bug(regs, address))
773 return;
774
775 no_context(regs, error_code, address);
776}
777
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100778static noinline void
779bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
780 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100781{
782 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
783}
784
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100785static void
786__bad_area(struct pt_regs *regs, unsigned long error_code,
787 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100788{
789 struct mm_struct *mm = current->mm;
790
791 /*
792 * Something tried to access memory that isn't in our memory map..
793 * Fix it, but check if it's kernel or user first..
794 */
795 up_read(&mm->mmap_sem);
796
797 __bad_area_nosemaphore(regs, error_code, address, si_code);
798}
799
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100800static noinline void
801bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100802{
803 __bad_area(regs, error_code, address, SEGV_MAPERR);
804}
805
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100806static noinline void
807bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
808 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100809{
810 __bad_area(regs, error_code, address, SEGV_ACCERR);
811}
812
813/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100814static void
815out_of_memory(struct pt_regs *regs, unsigned long error_code,
816 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100817{
818 /*
819 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100820 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100821 */
822 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100823
Nick Piggin92181f12009-01-20 04:24:26 +0100824 pagefault_out_of_memory();
825}
826
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100827static void
828do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100829{
830 struct task_struct *tsk = current;
831 struct mm_struct *mm = tsk->mm;
832
833 up_read(&mm->mmap_sem);
834
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100835 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100836 if (!(error_code & PF_USER))
837 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100838
Ingo Molnarcd1b68f2009-02-20 23:39:02 +0100839 /* User-space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100840 if (is_prefetch(regs, error_code, address))
841 return;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100842
843 tsk->thread.cr2 = address;
844 tsk->thread.error_code = error_code;
845 tsk->thread.trap_no = 14;
846
Nick Piggin92181f12009-01-20 04:24:26 +0100847 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
848}
849
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100850static noinline void
851mm_fault_error(struct pt_regs *regs, unsigned long error_code,
852 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100853{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100854 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100855 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100856 } else {
857 if (fault & VM_FAULT_SIGBUS)
858 do_sigbus(regs, error_code, address);
859 else
860 BUG();
861 }
Nick Piggin92181f12009-01-20 04:24:26 +0100862}
863
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100864static int spurious_fault_check(unsigned long error_code, pte_t *pte)
865{
866 if ((error_code & PF_WRITE) && !pte_write(*pte))
867 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100868
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100869 if ((error_code & PF_INSTR) && !pte_exec(*pte))
870 return 0;
871
872 return 1;
873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100876 * Handle a spurious fault caused by a stale TLB entry.
877 *
878 * This allows us to lazily refresh the TLB when increasing the
879 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
880 * eagerly is very expensive since that implies doing a full
881 * cross-processor TLB flush, even if no stale TLB entries exist
882 * on other processors.
883 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100884 * There are no security implications to leaving a stale TLB when
885 * increasing the permissions on a page.
886 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100887static noinline int
888spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100889{
890 pgd_t *pgd;
891 pud_t *pud;
892 pmd_t *pmd;
893 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500894 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100895
896 /* Reserved-bit violation or user access to kernel space? */
897 if (error_code & (PF_USER | PF_RSVD))
898 return 0;
899
900 pgd = init_mm.pgd + pgd_index(address);
901 if (!pgd_present(*pgd))
902 return 0;
903
904 pud = pud_offset(pgd, address);
905 if (!pud_present(*pud))
906 return 0;
907
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100908 if (pud_large(*pud))
909 return spurious_fault_check(error_code, (pte_t *) pud);
910
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100911 pmd = pmd_offset(pud, address);
912 if (!pmd_present(*pmd))
913 return 0;
914
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100915 if (pmd_large(*pmd))
916 return spurious_fault_check(error_code, (pte_t *) pmd);
917
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100918 pte = pte_offset_kernel(pmd, address);
919 if (!pte_present(*pte))
920 return 0;
921
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500922 ret = spurious_fault_check(error_code, pte);
923 if (!ret)
924 return 0;
925
926 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100927 * Make sure we have permissions in PMD.
928 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500929 */
930 ret = spurious_fault_check(error_code, (pte_t *) pmd);
931 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100932
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500933 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100934}
935
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200936int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100938static inline int
939access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100940{
941 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100942 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100943 if (unlikely(!(vma->vm_flags & VM_WRITE)))
944 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100945 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100946 }
947
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100948 /* read, present: */
949 if (unlikely(error_code & PF_PROT))
950 return 1;
951
952 /* read, not present: */
953 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
954 return 1;
955
Nick Piggin92181f12009-01-20 04:24:26 +0100956 return 0;
957}
958
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800959static int fault_in_kernel_space(unsigned long address)
960{
Ingo Molnard9517342009-02-20 23:32:28 +0100961 return address >= TASK_SIZE_MAX;
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800962}
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964/*
965 * This routine handles page faults. It determines the address,
966 * and the problem, and then passes it off to one of the appropriate
967 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 */
Ingo Molnarc3731c62009-02-20 23:22:34 +0100969dotraplinkage void __kprobes
970do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100972 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100973 struct task_struct *tsk;
974 unsigned long address;
975 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100976 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100977 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100979 tsk = current;
980 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100981
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100982 prefetchw(&mm->mmap_sem);
983
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100984 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200985 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200987 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200988 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 /*
991 * We fault-in kernel-space virtual memory on-demand. The
992 * 'reference' page table is init_mm.pgd.
993 *
994 * NOTE! We MUST NOT take any locks for this case. We may
995 * be in an interrupt or a critical region, and should
996 * only copy the information from the master page table,
997 * nothing more.
998 *
999 * This verifies that the fault happens in kernel space
1000 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +01001001 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -08001003 if (unlikely(fault_in_kernel_space(address))) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001004 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
1005 vmalloc_fault(address) >= 0)
1006 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001007
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001008 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +01001009 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +01001010 return;
1011
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001012 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001013 if (notify_page_fault(regs))
1014 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001015 /*
1016 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001017 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001018 */
Nick Piggin92181f12009-01-20 04:24:26 +01001019 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001020
Nick Piggin92181f12009-01-20 04:24:26 +01001021 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001022 }
1023
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001024 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +01001025 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001026 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001027 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -07001028 * It's safe to allow irq's after cr2 has been saved and the
1029 * vmalloc fault has been handled.
1030 *
1031 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001032 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001033 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001034 if (user_mode_vm(regs)) {
1035 local_irq_enable();
1036 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001037 } else {
1038 if (regs->flags & X86_EFLAGS_IF)
1039 local_irq_enable();
1040 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001041
Andi Kleen66c58152006-01-11 22:44:09 +01001042 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001043 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001046 * If we're in an interrupt, have no user context or are running
1047 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
Nick Piggin92181f12009-01-20 04:24:26 +01001049 if (unlikely(in_atomic() || !mm)) {
1050 bad_area_nosemaphore(regs, error_code, address);
1051 return;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001054 /*
1055 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001056 * addresses in user space. All other faults represent errors in
1057 * the kernel and should generate an OOPS. Unfortunately, in the
1058 * case of an erroneous fault occurring in a code path which already
1059 * holds mmap_sem we will deadlock attempting to validate the fault
1060 * against the address space. Luckily the kernel only validly
1061 * references user space from well defined areas of code, which are
1062 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 *
1064 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001065 * the source reference check when there is a possibility of a
1066 * deadlock. Attempt to lock the address space, if we cannot we then
1067 * validate the source. If this is invalid we can skip the address
1068 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 */
Nick Piggin92181f12009-01-20 04:24:26 +01001070 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001071 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001072 !search_exception_tables(regs->ip)) {
1073 bad_area_nosemaphore(regs, error_code, address);
1074 return;
1075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001077 } else {
1078 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001079 * The above down_read_trylock() might have succeeded in
1080 * which case we'll have missed the might_sleep() from
1081 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001082 */
1083 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001087 if (unlikely(!vma)) {
1088 bad_area(regs, error_code, address);
1089 return;
1090 }
1091 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001093 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1094 bad_area(regs, error_code, address);
1095 return;
1096 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001097 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001098 /*
1099 * Accessing the stack below %sp is always a bug.
1100 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001101 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001102 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001103 */
Nick Piggin92181f12009-01-20 04:24:26 +01001104 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1105 bad_area(regs, error_code, address);
1106 return;
1107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 }
Nick Piggin92181f12009-01-20 04:24:26 +01001109 if (unlikely(expand_stack(vma, address))) {
1110 bad_area(regs, error_code, address);
1111 return;
1112 }
1113
1114 /*
1115 * Ok, we have a good vm_area for this memory access, so
1116 * we can handle it..
1117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001119 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001120
Nick Piggin92181f12009-01-20 04:24:26 +01001121 if (unlikely(access_error(error_code, write, vma))) {
1122 bad_area_access_error(regs, error_code, address);
1123 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
1126 /*
1127 * If for any reason at all we couldn't handle the fault,
1128 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001129 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
Nick Piggin83c54072007-07-19 01:47:05 -07001131 fault = handle_mm_fault(mm, vma, address, write);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001132
Nick Piggin83c54072007-07-19 01:47:05 -07001133 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001134 mm_fault_error(regs, error_code, address, fault);
1135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001137
Nick Piggin83c54072007-07-19 01:47:05 -07001138 if (fault & VM_FAULT_MAJOR)
1139 tsk->maj_flt++;
1140 else
1141 tsk->min_flt++;
Harvey Harrisond729ab32008-01-30 13:33:23 +01001142
Ingo Molnar8c938f92009-02-20 22:12:18 +01001143 check_v8086_mode(regs, address, tsk);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}