blob: 379beaec6caae6018a98e98692f6f44c9c284705 [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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005#include <linux/interrupt.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +01006#include <linux/mmiotrace.h>
7#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/compiler.h>
Harvey Harrisonc61e2112008-01-30 13:34:11 +01009#include <linux/highmem.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070010#include <linux/kprobes.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010011#include <linux/uaccess.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010012#include <linux/vmalloc.h>
13#include <linux/vt_kern.h>
14#include <linux/signal.h>
15#include <linux/kernel.h>
16#include <linux/ptrace.h>
17#include <linux/string.h>
18#include <linux/module.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070019#include <linux/kdebug.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010020#include <linux/errno.h>
Eric Sandeen7c9f8862008-04-22 16:38:23 -050021#include <linux/magic.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010022#include <linux/sched.h>
23#include <linux/types.h>
24#include <linux/init.h>
25#include <linux/mman.h>
26#include <linux/tty.h>
27#include <linux/smp.h>
28#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm-generic/sections.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010031
32#include <asm/tlbflush.h>
33#include <asm/pgalloc.h>
34#include <asm/segment.h>
35#include <asm/system.h>
36#include <asm/proto.h>
Jaswinder Singh70ef5642008-07-23 17:36:37 +053037#include <asm/traps.h>
Ingo Molnar2d4a7162009-02-20 19:56:40 +010038#include <asm/desc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Harvey Harrison33cb5242008-01-30 13:32:19 +010040/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010041 * Page fault error code bits:
42 *
43 * bit 0 == 0: no page found 1: protection fault
44 * bit 1 == 0: read access 1: write access
45 * bit 2 == 0: kernel-mode access 1: user-mode access
46 * bit 3 == 1: use of reserved bit detected
47 * bit 4 == 1: fault was an instruction fetch
Harvey Harrison33cb5242008-01-30 13:32:19 +010048 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +010049enum x86_pf_error_code {
50
51 PF_PROT = 1 << 0,
52 PF_WRITE = 1 << 1,
53 PF_USER = 1 << 2,
54 PF_RSVD = 1 << 3,
55 PF_INSTR = 1 << 4,
56};
Andi Kleen66c58152006-01-11 22:44:09 +010057
Ingo Molnarb814d412009-02-20 22:32:10 +010058/*
59 * (returns 0 if mmiotrace is disabled)
60 */
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020061static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020062{
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020063 if (unlikely(is_kmmio_active()))
64 if (kmmio_handler(regs, addr) == 1)
65 return -1;
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020066 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020067}
68
Christoph Hellwig74a0b572007-10-16 01:24:07 -070069static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070070{
Christoph Hellwig74a0b572007-10-16 01:24:07 -070071 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070072
Christoph Hellwig74a0b572007-10-16 01:24:07 -070073 /* kprobe_running() needs smp_processor_id() */
Ingo Molnarb1801812009-02-20 22:42:57 +010074 if (kprobes_built_in() && !user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070075 preempt_disable();
76 if (kprobe_running() && kprobe_fault_handler(regs, 14))
77 ret = 1;
78 preempt_enable();
79 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070080
Christoph Hellwig74a0b572007-10-16 01:24:07 -070081 return ret;
Harvey Harrison33cb5242008-01-30 13:32:19 +010082}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070083
Harvey Harrison1dc85be2008-01-30 13:32:35 +010084/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010085 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010086 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010087 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010088 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010089 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
90 * Check that here and ignore it.
91 *
92 * 64-bit mode:
93 *
94 * Sometimes the CPU reports invalid exceptions on prefetch.
95 * Check that here and ignore it.
96 *
97 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +010098 */
Ingo Molnar107a0362009-02-20 20:37:05 +010099static inline int
100check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
101 unsigned char opcode, int *prefetch)
102{
103 unsigned char instr_hi = opcode & 0xf0;
104 unsigned char instr_lo = opcode & 0x0f;
105
106 switch (instr_hi) {
107 case 0x20:
108 case 0x30:
109 /*
110 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
111 * In X86_64 long mode, the CPU will signal invalid
112 * opcode if some of these prefixes are present so
113 * X86_64 will never get here anyway
114 */
115 return ((instr_lo & 7) == 0x6);
116#ifdef CONFIG_X86_64
117 case 0x40:
118 /*
119 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
120 * Need to figure out under what instruction mode the
121 * instruction was issued. Could check the LDT for lm,
122 * but for now it's good enough to assume that long
123 * mode only uses well known segments or kernel.
124 */
125 return (!user_mode(regs)) || (regs->cs == __USER_CS);
126#endif
127 case 0x60:
128 /* 0x64 thru 0x67 are valid prefixes in all modes. */
129 return (instr_lo & 0xC) == 0x4;
130 case 0xF0:
131 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
132 return !instr_lo || (instr_lo>>1) == 1;
133 case 0x00:
134 /* Prefetch instruction is 0x0F0D or 0x0F18 */
135 if (probe_kernel_address(instr, opcode))
136 return 0;
137
138 *prefetch = (instr_lo == 0xF) &&
139 (opcode == 0x0D || opcode == 0x18);
140 return 0;
141 default:
142 return 0;
143 }
144}
145
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100146static int
147is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100148{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100149 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100150 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100151 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Ingo Molnar30853542008-03-27 21:29:09 +0100153 /*
154 * If it was a exec (instruction fetch) fault on NX page, then
155 * do not ignore the fault:
156 */
Andi Kleen66c58152006-01-11 22:44:09 +0100157 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100159
Ingo Molnar107a0362009-02-20 20:37:05 +0100160 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700161 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700163 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 return 0;
165
Ingo Molnar107a0362009-02-20 20:37:05 +0100166 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100167 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100169 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 instr++;
173
Ingo Molnar107a0362009-02-20 20:37:05 +0100174 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177 return prefetch;
178}
179
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100180static void
181force_sig_info_fault(int si_signo, int si_code, unsigned long address,
182 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100183{
184 siginfo_t info;
185
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100186 info.si_signo = si_signo;
187 info.si_errno = 0;
188 info.si_code = si_code;
189 info.si_addr = (void __user *)address;
190
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100191 force_sig_info(si_signo, &info, tsk);
192}
193
Harvey Harrison1156e092008-01-30 13:34:10 +0100194#ifdef CONFIG_X86_64
Harvey Harrison33cb5242008-01-30 13:32:19 +0100195static int bad_address(void *p)
196{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 unsigned long dummy;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100198
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100199 return probe_kernel_address((unsigned long *)p, dummy);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100200}
Harvey Harrison1156e092008-01-30 13:34:10 +0100201#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Adrian Bunkcae30f822008-02-13 23:31:31 +0200203static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Harvey Harrison1156e092008-01-30 13:34:10 +0100205#ifdef CONFIG_X86_32
206 __typeof__(pte_val(__pte(0))) page;
207
208 page = read_cr3();
209 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100210
Harvey Harrison1156e092008-01-30 13:34:10 +0100211#ifdef CONFIG_X86_PAE
212 printk("*pdpt = %016Lx ", page);
213 if ((page >> PAGE_SHIFT) < max_low_pfn
214 && page & _PAGE_PRESENT) {
215 page &= PAGE_MASK;
216 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100217 & (PTRS_PER_PMD - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100218 printk(KERN_CONT "*pde = %016Lx ", page);
219 page &= ~_PAGE_NX;
220 }
221#else
222 printk("*pde = %08lx ", page);
223#endif
224
225 /*
226 * We must not directly access the pte in the highpte
227 * case if the page table is located in highmem.
228 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100229 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100230 */
231 if ((page >> PAGE_SHIFT) < max_low_pfn
232 && (page & _PAGE_PRESENT)
233 && !(page & _PAGE_PSE)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100234
Harvey Harrison1156e092008-01-30 13:34:10 +0100235 page &= PAGE_MASK;
236 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100237 & (PTRS_PER_PTE - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100238 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
239 }
240
241 printk("\n");
242#else /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 pgd_t *pgd;
244 pud_t *pud;
245 pmd_t *pmd;
246 pte_t *pte;
247
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200248 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Harvey Harrison33cb5242008-01-30 13:32:19 +0100250 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 pgd += pgd_index(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100253 if (bad_address(pgd))
254 goto bad;
255
Jan Beulichd646bce2006-02-03 21:51:47 +0100256 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100257
258 if (!pgd_present(*pgd))
259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Andi Kleend2ae5b52006-06-26 13:57:56 +0200261 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100262 if (bad_address(pud))
263 goto bad;
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100266 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100267 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100270 if (bad_address(pmd))
271 goto bad;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100274 if (!pmd_present(*pmd) || pmd_large(*pmd))
275 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100278 if (bad_address(pte))
279 goto bad;
280
Harvey Harrison33cb5242008-01-30 13:32:19 +0100281 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100282out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 printk("\n");
284 return;
285bad:
286 printk("BAD\n");
Harvey Harrison1156e092008-01-30 13:34:10 +0100287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Harvey Harrison1156e092008-01-30 13:34:10 +0100290#ifdef CONFIG_X86_32
291static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
292{
293 unsigned index = pgd_index(address);
294 pgd_t *pgd_k;
295 pud_t *pud, *pud_k;
296 pmd_t *pmd, *pmd_k;
297
298 pgd += index;
299 pgd_k = init_mm.pgd + index;
300
301 if (!pgd_present(*pgd_k))
302 return NULL;
303
304 /*
305 * set_pgd(pgd, *pgd_k); here would be useless on PAE
306 * and redundant with the set_pmd() on non-PAE. As would
307 * set_pud.
308 */
Harvey Harrison1156e092008-01-30 13:34:10 +0100309 pud = pud_offset(pgd, address);
310 pud_k = pud_offset(pgd_k, address);
311 if (!pud_present(*pud_k))
312 return NULL;
313
314 pmd = pmd_offset(pud, address);
315 pmd_k = pmd_offset(pud_k, address);
316 if (!pmd_present(*pmd_k))
317 return NULL;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100318
Harvey Harrison1156e092008-01-30 13:34:10 +0100319 if (!pmd_present(*pmd)) {
320 set_pmd(pmd, *pmd_k);
321 arch_flush_lazy_mmu_mode();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100322 } else {
Harvey Harrison1156e092008-01-30 13:34:10 +0100323 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100324 }
325
Harvey Harrison1156e092008-01-30 13:34:10 +0100326 return pmd_k;
327}
Harvey Harrison1156e092008-01-30 13:34:10 +0100328
Ingo Molnar8c938f92009-02-20 22:12:18 +0100329/*
330 * Did it hit the DOS screen memory VA from vm86 mode?
331 */
332static inline void
333check_v8086_mode(struct pt_regs *regs, unsigned long address,
334 struct task_struct *tsk)
335{
336 unsigned long bit;
337
338 if (!v8086_mode(regs))
339 return;
340
341 bit = (address - 0xA0000) >> PAGE_SHIFT;
342 if (bit < 32)
343 tsk->thread.screen_bitmap |= 1 << bit;
344}
345
346#else /* CONFIG_X86_64: */
347
Harvey Harrison33cb5242008-01-30 13:32:19 +0100348static const char errata93_warning[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
350KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
351KERN_ERR "******* Please consider a BIOS update.\n"
352KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
Ingo Molnar8c938f92009-02-20 22:12:18 +0100353
354/*
355 * No vm86 mode in 64-bit mode:
356 */
357static inline void
358check_v8086_mode(struct pt_regs *regs, unsigned long address,
359 struct task_struct *tsk)
360{
361}
362
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100365/*
366 * Workaround for K8 erratum #93 & buggy BIOS.
367 *
368 * BIOS SMM functions are required to use a specific workaround
369 * to avoid corruption of the 64bit RIP register on C stepping K8.
370 *
371 * A lot of BIOS that didn't get tested properly miss this.
372 *
373 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
374 * Try to work around it here.
375 *
376 * Note we only handle faults in kernel here.
377 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100378 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100379static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100381#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100382 static int once;
383
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100384 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100386
Harvey Harrison33cb5242008-01-30 13:32:19 +0100387 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100391 if ((address >= (u64)_stext && address <= (u64)_etext) ||
392 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100393 if (!once) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100394 printk(errata93_warning);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100395 once = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100397 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return 1;
399 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100400#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100402}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Harvey Harrison35f32662008-01-30 13:34:09 +0100404/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100405 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
406 * to illegal addresses >4GB.
407 *
408 * We catch this in the page fault handler because these addresses
409 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100410 * segment in LDT is compatibility mode.
411 */
412static int is_errata100(struct pt_regs *regs, unsigned long address)
413{
414#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100415 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100416 return 1;
417#endif
418 return 0;
419}
420
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100421static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
422{
423#ifdef CONFIG_X86_F00F_BUG
424 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100425
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100426 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100427 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100428 */
429 if (boot_cpu_data.f00f_bug) {
430 nr = (address - idt_descr.address) >> 3;
431
432 if (nr == 6) {
433 do_invalid_op(regs, 0);
434 return 1;
435 }
436 }
437#endif
438 return 0;
439}
440
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100441static void
442show_fault_oops(struct pt_regs *regs, unsigned long error_code,
443 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100444{
Harvey Harrison1156e092008-01-30 13:34:10 +0100445#ifdef CONFIG_X86_32
446 if (!oops_may_print())
447 return;
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100448#endif
Harvey Harrison1156e092008-01-30 13:34:10 +0100449
450#ifdef CONFIG_X86_PAE
451 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100452 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100453
Harvey Harrison1156e092008-01-30 13:34:10 +0100454 pte_t *pte = lookup_address(address, &level);
455
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100456 if (pte && pte_present(*pte) && !pte_exec(*pte)) {
Harvey Harrison1156e092008-01-30 13:34:10 +0100457 printk(KERN_CRIT "kernel tried to execute "
458 "NX-protected page - exploit attempt? "
David Howells350b4da2008-11-14 10:38:40 +1100459 "(uid: %d)\n", current_uid());
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100460 }
Harvey Harrison1156e092008-01-30 13:34:10 +0100461 }
462#endif
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100463
Harvey Harrison1156e092008-01-30 13:34:10 +0100464 printk(KERN_ALERT "BUG: unable to handle kernel ");
465 if (address < PAGE_SIZE)
466 printk(KERN_CONT "NULL pointer dereference");
467 else
468 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100469
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200470 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100471 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100472 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100473
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100474 dump_pagetable(address);
475}
476
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100477static noinline void
478pgtable_bad(struct pt_regs *regs, unsigned long error_code,
479 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100481 struct task_struct *tsk;
482 unsigned long flags;
483 int sig;
484
485 flags = oops_begin();
486 tsk = current;
487 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100490 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100492
493 tsk->thread.cr2 = address;
494 tsk->thread.trap_no = 14;
495 tsk->thread.error_code = error_code;
496
Jan Beulich22f59912008-01-30 13:31:23 +0100497 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200498 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100499
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200500 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100503static noinline void
504no_context(struct pt_regs *regs, unsigned long error_code,
505 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100506{
507 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100508 unsigned long *stackend;
509
Nick Piggin92181f12009-01-20 04:24:26 +0100510#ifdef CONFIG_X86_64
511 unsigned long flags;
512 int sig;
513#endif
514
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100515 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100516 if (fixup_exception(regs))
517 return;
518
519 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100520 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100521 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100522 * Valid to do another page fault here, because if this fault
523 * had been triggered by is_prefetch fixup_exception would have
524 * handled it.
525 *
526 * 64-bit:
527 *
528 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100529 */
530 if (is_prefetch(regs, error_code, address))
531 return;
532
533 if (is_errata93(regs, address))
534 return;
535
536 /*
537 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100538 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100539 */
540#ifdef CONFIG_X86_32
541 bust_spinlocks(1);
542#else
543 flags = oops_begin();
544#endif
545
546 show_fault_oops(regs, error_code, address);
547
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100548 stackend = end_of_stack(tsk);
Ingo Molnar19803072009-01-21 10:39:51 +0100549 if (*stackend != STACK_END_MAGIC)
550 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
551
Nick Piggin92181f12009-01-20 04:24:26 +0100552 tsk->thread.cr2 = address;
553 tsk->thread.trap_no = 14;
554 tsk->thread.error_code = error_code;
555
556#ifdef CONFIG_X86_32
557 die("Oops", regs, error_code);
558 bust_spinlocks(0);
559 do_exit(SIGKILL);
560#else
561 sig = SIGKILL;
562 if (__die("Oops", regs, error_code))
563 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100564
Nick Piggin92181f12009-01-20 04:24:26 +0100565 /* Executive summary in case the body of the oops scrolled away */
566 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100567
Nick Piggin92181f12009-01-20 04:24:26 +0100568 oops_end(flags, regs, sig);
569#endif
570}
571
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100572/*
573 * Print out info about fatal segfaults, if the show_unhandled_signals
574 * sysctl is set:
575 */
576static inline void
577show_signal_msg(struct pt_regs *regs, unsigned long error_code,
578 unsigned long address, struct task_struct *tsk)
579{
580 if (!unhandled_signal(tsk, SIGSEGV))
581 return;
582
583 if (!printk_ratelimit())
584 return;
585
586 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
587 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
588 tsk->comm, task_pid_nr(tsk), address,
589 (void *)regs->ip, (void *)regs->sp, error_code);
590
591 print_vma_addr(KERN_CONT " in ", regs->ip);
592
593 printk(KERN_CONT "\n");
594}
595
596static void
597__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
598 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100599{
600 struct task_struct *tsk = current;
601
602 /* User mode accesses just cause a SIGSEGV */
603 if (error_code & PF_USER) {
604 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100605 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100606 */
607 local_irq_enable();
608
609 /*
610 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100611 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100612 */
613 if (is_prefetch(regs, error_code, address))
614 return;
615
616 if (is_errata100(regs, address))
617 return;
618
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100619 if (unlikely(show_unhandled_signals))
620 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100621
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100622 /* Kernel addresses are always protection faults: */
623 tsk->thread.cr2 = address;
624 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
625 tsk->thread.trap_no = 14;
626
Nick Piggin92181f12009-01-20 04:24:26 +0100627 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100628
Nick Piggin92181f12009-01-20 04:24:26 +0100629 return;
630 }
631
632 if (is_f00f_bug(regs, address))
633 return;
634
635 no_context(regs, error_code, address);
636}
637
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100638static noinline void
639bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
640 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100641{
642 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
643}
644
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100645static void
646__bad_area(struct pt_regs *regs, unsigned long error_code,
647 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100648{
649 struct mm_struct *mm = current->mm;
650
651 /*
652 * Something tried to access memory that isn't in our memory map..
653 * Fix it, but check if it's kernel or user first..
654 */
655 up_read(&mm->mmap_sem);
656
657 __bad_area_nosemaphore(regs, error_code, address, si_code);
658}
659
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100660static noinline void
661bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100662{
663 __bad_area(regs, error_code, address, SEGV_MAPERR);
664}
665
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100666static noinline void
667bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
668 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100669{
670 __bad_area(regs, error_code, address, SEGV_ACCERR);
671}
672
673/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100674static void
675out_of_memory(struct pt_regs *regs, unsigned long error_code,
676 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100677{
678 /*
679 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100680 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100681 */
682 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100683
Nick Piggin92181f12009-01-20 04:24:26 +0100684 pagefault_out_of_memory();
685}
686
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100687static void
688do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100689{
690 struct task_struct *tsk = current;
691 struct mm_struct *mm = tsk->mm;
692
693 up_read(&mm->mmap_sem);
694
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100695 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100696 if (!(error_code & PF_USER))
697 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100698
Nick Piggin92181f12009-01-20 04:24:26 +0100699#ifdef CONFIG_X86_32
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100700 /* User space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100701 if (is_prefetch(regs, error_code, address))
702 return;
703#endif
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100704
705 tsk->thread.cr2 = address;
706 tsk->thread.error_code = error_code;
707 tsk->thread.trap_no = 14;
708
Nick Piggin92181f12009-01-20 04:24:26 +0100709 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
710}
711
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100712static noinline void
713mm_fault_error(struct pt_regs *regs, unsigned long error_code,
714 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100715{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100716 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100717 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100718 } else {
719 if (fault & VM_FAULT_SIGBUS)
720 do_sigbus(regs, error_code, address);
721 else
722 BUG();
723 }
Nick Piggin92181f12009-01-20 04:24:26 +0100724}
725
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100726static int spurious_fault_check(unsigned long error_code, pte_t *pte)
727{
728 if ((error_code & PF_WRITE) && !pte_write(*pte))
729 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100730
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100731 if ((error_code & PF_INSTR) && !pte_exec(*pte))
732 return 0;
733
734 return 1;
735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100738 * Handle a spurious fault caused by a stale TLB entry.
739 *
740 * This allows us to lazily refresh the TLB when increasing the
741 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
742 * eagerly is very expensive since that implies doing a full
743 * cross-processor TLB flush, even if no stale TLB entries exist
744 * on other processors.
745 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100746 * There are no security implications to leaving a stale TLB when
747 * increasing the permissions on a page.
748 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100749static noinline int
750spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100751{
752 pgd_t *pgd;
753 pud_t *pud;
754 pmd_t *pmd;
755 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500756 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100757
758 /* Reserved-bit violation or user access to kernel space? */
759 if (error_code & (PF_USER | PF_RSVD))
760 return 0;
761
762 pgd = init_mm.pgd + pgd_index(address);
763 if (!pgd_present(*pgd))
764 return 0;
765
766 pud = pud_offset(pgd, address);
767 if (!pud_present(*pud))
768 return 0;
769
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100770 if (pud_large(*pud))
771 return spurious_fault_check(error_code, (pte_t *) pud);
772
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100773 pmd = pmd_offset(pud, address);
774 if (!pmd_present(*pmd))
775 return 0;
776
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100777 if (pmd_large(*pmd))
778 return spurious_fault_check(error_code, (pte_t *) pmd);
779
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100780 pte = pte_offset_kernel(pmd, address);
781 if (!pte_present(*pte))
782 return 0;
783
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500784 ret = spurious_fault_check(error_code, pte);
785 if (!ret)
786 return 0;
787
788 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100789 * Make sure we have permissions in PMD.
790 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500791 */
792 ret = spurious_fault_check(error_code, (pte_t *) pmd);
793 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100794
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500795 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100796}
797
798/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100799 * 32-bit:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100800 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100801 * Handle a fault on the vmalloc or module mapping area
802 *
803 * 64-bit:
804 *
805 * Handle a fault on the vmalloc area
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700806 *
807 * This assumes no large pages in there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
Nick Piggin92181f12009-01-20 04:24:26 +0100809static noinline int vmalloc_fault(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100811#ifdef CONFIG_X86_32
812 unsigned long pgd_paddr;
813 pmd_t *pmd_k;
814 pte_t *pte_k;
Henry Nestlerb29c7012008-05-12 15:44:39 +0200815
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100816 /* Make sure we are in vmalloc area: */
Henry Nestlerb29c7012008-05-12 15:44:39 +0200817 if (!(address >= VMALLOC_START && address < VMALLOC_END))
818 return -1;
819
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100820 /*
821 * Synchronize this task's top level page-table
822 * with the 'reference' page table.
823 *
824 * Do _not_ use "current" here. We might be inside
825 * an interrupt in the middle of a task switch..
826 */
827 pgd_paddr = read_cr3();
828 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
829 if (!pmd_k)
830 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100831
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100832 pte_k = pte_offset_kernel(pmd_k, address);
833 if (!pte_present(*pte_k))
834 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100835
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100836 return 0;
837#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 pgd_t *pgd, *pgd_ref;
839 pud_t *pud, *pud_ref;
840 pmd_t *pmd, *pmd_ref;
841 pte_t *pte, *pte_ref;
842
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100843 /* Make sure we are in vmalloc area: */
Harvey Harrisoncf89ec92008-02-04 16:47:56 +0100844 if (!(address >= VMALLOC_START && address < VMALLOC_END))
845 return -1;
846
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100847 /*
848 * Copy kernel mappings over when needed. This can also
849 * happen within a race in page table update. In the later
850 * case just flush:
851 */
Andi Kleenf313e122009-01-09 12:17:43 -0800852 pgd = pgd_offset(current->active_mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 pgd_ref = pgd_offset_k(address);
854 if (pgd_none(*pgd_ref))
855 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (pgd_none(*pgd))
858 set_pgd(pgd, *pgd_ref);
Jan Beulich8c914cb2006-03-25 16:29:40 +0100859 else
Dave McCracken46a82b22006-09-25 23:31:48 -0700860 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100862 /*
863 * Below here mismatches are bugs because these lower tables
864 * are shared:
865 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 pud = pud_offset(pgd, address);
868 pud_ref = pud_offset(pgd_ref, address);
869 if (pud_none(*pud_ref))
870 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100871
Dave McCracken46a82b22006-09-25 23:31:48 -0700872 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 pmd = pmd_offset(pud, address);
876 pmd_ref = pmd_offset(pud_ref, address);
877 if (pmd_none(*pmd_ref))
878 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
881 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 pte_ref = pte_offset_kernel(pmd_ref, address);
884 if (!pte_present(*pte_ref))
885 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100888
889 /*
890 * Don't use pte_page here, because the mappings can point
891 * outside mem_map, and the NUMA hash lookup cannot handle
892 * that:
893 */
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700894 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return 0;
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100898#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200901int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100903static inline int
904access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100905{
906 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100907 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100908 if (unlikely(!(vma->vm_flags & VM_WRITE)))
909 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100910 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100911 }
912
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100913 /* read, present: */
914 if (unlikely(error_code & PF_PROT))
915 return 1;
916
917 /* read, not present: */
918 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
919 return 1;
920
Nick Piggin92181f12009-01-20 04:24:26 +0100921 return 0;
922}
923
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800924static int fault_in_kernel_space(unsigned long address)
925{
926#ifdef CONFIG_X86_32
927 return address >= TASK_SIZE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100928#else
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800929 return address >= TASK_SIZE64;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100930#endif
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800931}
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
934 * This routine handles page faults. It determines the address,
935 * and the problem, and then passes it off to one of the appropriate
936 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100938#ifdef CONFIG_X86_64
939asmlinkage
940#endif
941void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100943 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100944 struct task_struct *tsk;
945 unsigned long address;
946 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100947 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100948 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100950 tsk = current;
951 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100952
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100953 prefetchw(&mm->mmap_sem);
954
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100955 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200956 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200958 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
962 * We fault-in kernel-space virtual memory on-demand. The
963 * 'reference' page table is init_mm.pgd.
964 *
965 * NOTE! We MUST NOT take any locks for this case. We may
966 * be in an interrupt or a critical region, and should
967 * only copy the information from the master page table,
968 * nothing more.
969 *
970 * This verifies that the fault happens in kernel space
971 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +0100972 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800974 if (unlikely(fault_in_kernel_space(address))) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100975 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
976 vmalloc_fault(address) >= 0)
977 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100978
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100979 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +0100980 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100981 return;
982
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100983 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -0500984 if (notify_page_fault(regs))
985 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100986 /*
987 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100988 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100989 */
Nick Piggin92181f12009-01-20 04:24:26 +0100990 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100991
Nick Piggin92181f12009-01-20 04:24:26 +0100992 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100993 }
994
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100995 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +0100996 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -0500997 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100998 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -0700999 * It's safe to allow irq's after cr2 has been saved and the
1000 * vmalloc fault has been handled.
1001 *
1002 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001003 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001004 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001005 if (user_mode_vm(regs)) {
1006 local_irq_enable();
1007 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001008 } else {
1009 if (regs->flags & X86_EFLAGS_IF)
1010 local_irq_enable();
1011 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001012
Andi Kleen66c58152006-01-11 22:44:09 +01001013 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001014 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001017 * If we're in an interrupt, have no user context or are running
1018 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 */
Nick Piggin92181f12009-01-20 04:24:26 +01001020 if (unlikely(in_atomic() || !mm)) {
1021 bad_area_nosemaphore(regs, error_code, address);
1022 return;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001025 /*
1026 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001027 * addresses in user space. All other faults represent errors in
1028 * the kernel and should generate an OOPS. Unfortunately, in the
1029 * case of an erroneous fault occurring in a code path which already
1030 * holds mmap_sem we will deadlock attempting to validate the fault
1031 * against the address space. Luckily the kernel only validly
1032 * references user space from well defined areas of code, which are
1033 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 *
1035 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001036 * the source reference check when there is a possibility of a
1037 * deadlock. Attempt to lock the address space, if we cannot we then
1038 * validate the source. If this is invalid we can skip the address
1039 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 */
Nick Piggin92181f12009-01-20 04:24:26 +01001041 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001042 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001043 !search_exception_tables(regs->ip)) {
1044 bad_area_nosemaphore(regs, error_code, address);
1045 return;
1046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001048 } else {
1049 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001050 * The above down_read_trylock() might have succeeded in
1051 * which case we'll have missed the might_sleep() from
1052 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001053 */
1054 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056
1057 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001058 if (unlikely(!vma)) {
1059 bad_area(regs, error_code, address);
1060 return;
1061 }
1062 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001064 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1065 bad_area(regs, error_code, address);
1066 return;
1067 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001068 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001069 /*
1070 * Accessing the stack below %sp is always a bug.
1071 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001072 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001073 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001074 */
Nick Piggin92181f12009-01-20 04:24:26 +01001075 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1076 bad_area(regs, error_code, address);
1077 return;
1078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
Nick Piggin92181f12009-01-20 04:24:26 +01001080 if (unlikely(expand_stack(vma, address))) {
1081 bad_area(regs, error_code, address);
1082 return;
1083 }
1084
1085 /*
1086 * Ok, we have a good vm_area for this memory access, so
1087 * we can handle it..
1088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001090 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001091
Nick Piggin92181f12009-01-20 04:24:26 +01001092 if (unlikely(access_error(error_code, write, vma))) {
1093 bad_area_access_error(regs, error_code, address);
1094 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 }
1096
1097 /*
1098 * If for any reason at all we couldn't handle the fault,
1099 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001100 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Nick Piggin83c54072007-07-19 01:47:05 -07001102 fault = handle_mm_fault(mm, vma, address, write);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001103
Nick Piggin83c54072007-07-19 01:47:05 -07001104 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001105 mm_fault_error(regs, error_code, address, fault);
1106 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001108
Nick Piggin83c54072007-07-19 01:47:05 -07001109 if (fault & VM_FAULT_MAJOR)
1110 tsk->maj_flt++;
1111 else
1112 tsk->min_flt++;
Harvey Harrisond729ab32008-01-30 13:33:23 +01001113
Ingo Molnar8c938f92009-02-20 22:12:18 +01001114 check_v8086_mode(regs, address, tsk);
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117}
Andi Kleen9e43e1b2005-11-05 17:25:54 +01001118
Jan Beulich8c914cb2006-03-25 16:29:40 +01001119DEFINE_SPINLOCK(pgd_lock);
Christoph Lameter2bff7382007-05-02 19:27:10 +02001120LIST_HEAD(pgd_list);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001121
1122void vmalloc_sync_all(void)
1123{
Harvey Harrison1156e092008-01-30 13:34:10 +01001124 unsigned long address;
1125
Jan Beulichcc643d42008-08-29 12:53:45 +01001126#ifdef CONFIG_X86_32
Harvey Harrison1156e092008-01-30 13:34:10 +01001127 if (SHARED_KERNEL_PMD)
1128 return;
1129
Jan Beulichcc643d42008-08-29 12:53:45 +01001130 for (address = VMALLOC_START & PMD_MASK;
1131 address >= TASK_SIZE && address < FIXADDR_TOP;
1132 address += PMD_SIZE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001133
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001134 unsigned long flags;
1135 struct page *page;
Harvey Harrison1156e092008-01-30 13:34:10 +01001136
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001137 spin_lock_irqsave(&pgd_lock, flags);
1138 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001139 if (!vmalloc_sync_one(page_address(page), address))
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001140 break;
Harvey Harrison1156e092008-01-30 13:34:10 +01001141 }
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001142 spin_unlock_irqrestore(&pgd_lock, flags);
Harvey Harrison1156e092008-01-30 13:34:10 +01001143 }
1144#else /* CONFIG_X86_64 */
Jan Beulichcc643d42008-08-29 12:53:45 +01001145 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
1146 address += PGDIR_SIZE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001147
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001148 const pgd_t *pgd_ref = pgd_offset_k(address);
1149 unsigned long flags;
1150 struct page *page;
Jan Beulich8c914cb2006-03-25 16:29:40 +01001151
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001152 if (pgd_none(*pgd_ref))
1153 continue;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001154
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001155 spin_lock_irqsave(&pgd_lock, flags);
1156 list_for_each_entry(page, &pgd_list, lru) {
1157 pgd_t *pgd;
1158 pgd = (pgd_t *)page_address(page) + pgd_index(address);
1159 if (pgd_none(*pgd))
1160 set_pgd(pgd, *pgd_ref);
1161 else
1162 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Jan Beulich8c914cb2006-03-25 16:29:40 +01001163 }
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001164 spin_unlock_irqrestore(&pgd_lock, flags);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001165 }
Harvey Harrison1156e092008-01-30 13:34:10 +01001166#endif
Jan Beulich8c914cb2006-03-25 16:29:40 +01001167}