blob: fe99af4b86d9970c3264d6601892cd0364e013d7 [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{
Harvey Harrison33cb5242008-01-30 13:32:19 +010071#ifdef CONFIG_KPROBES
Christoph Hellwig74a0b572007-10-16 01:24:07 -070072 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070073
Christoph Hellwig74a0b572007-10-16 01:24:07 -070074 /* kprobe_running() needs smp_processor_id() */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +010075 if (!user_mode_vm(regs)) {
Christoph Hellwig74a0b572007-10-16 01:24:07 -070076 preempt_disable();
77 if (kprobe_running() && kprobe_fault_handler(regs, 14))
78 ret = 1;
79 preempt_enable();
80 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070081
Christoph Hellwig74a0b572007-10-16 01:24:07 -070082 return ret;
Christoph Hellwig74a0b572007-10-16 01:24:07 -070083#else
Christoph Hellwig74a0b572007-10-16 01:24:07 -070084 return 0;
Christoph Hellwig74a0b572007-10-16 01:24:07 -070085#endif
Harvey Harrison33cb5242008-01-30 13:32:19 +010086}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070087
Harvey Harrison1dc85be2008-01-30 13:32:35 +010088/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010089 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010090 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010091 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010092 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010093 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
94 * Check that here and ignore it.
95 *
96 * 64-bit mode:
97 *
98 * Sometimes the CPU reports invalid exceptions on prefetch.
99 * Check that here and ignore it.
100 *
101 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100102 */
Ingo Molnar107a0362009-02-20 20:37:05 +0100103static inline int
104check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
105 unsigned char opcode, int *prefetch)
106{
107 unsigned char instr_hi = opcode & 0xf0;
108 unsigned char instr_lo = opcode & 0x0f;
109
110 switch (instr_hi) {
111 case 0x20:
112 case 0x30:
113 /*
114 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
115 * In X86_64 long mode, the CPU will signal invalid
116 * opcode if some of these prefixes are present so
117 * X86_64 will never get here anyway
118 */
119 return ((instr_lo & 7) == 0x6);
120#ifdef CONFIG_X86_64
121 case 0x40:
122 /*
123 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
124 * Need to figure out under what instruction mode the
125 * instruction was issued. Could check the LDT for lm,
126 * but for now it's good enough to assume that long
127 * mode only uses well known segments or kernel.
128 */
129 return (!user_mode(regs)) || (regs->cs == __USER_CS);
130#endif
131 case 0x60:
132 /* 0x64 thru 0x67 are valid prefixes in all modes. */
133 return (instr_lo & 0xC) == 0x4;
134 case 0xF0:
135 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
136 return !instr_lo || (instr_lo>>1) == 1;
137 case 0x00:
138 /* Prefetch instruction is 0x0F0D or 0x0F18 */
139 if (probe_kernel_address(instr, opcode))
140 return 0;
141
142 *prefetch = (instr_lo == 0xF) &&
143 (opcode == 0x0D || opcode == 0x18);
144 return 0;
145 default:
146 return 0;
147 }
148}
149
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100150static int
151is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100152{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100153 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100154 unsigned char *instr;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100155 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Ingo Molnar30853542008-03-27 21:29:09 +0100157 /*
158 * If it was a exec (instruction fetch) fault on NX page, then
159 * do not ignore the fault:
160 */
Andi Kleen66c58152006-01-11 22:44:09 +0100161 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100163
Ingo Molnar107a0362009-02-20 20:37:05 +0100164 instr = (void *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700165 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700167 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return 0;
169
Ingo Molnar107a0362009-02-20 20:37:05 +0100170 while (instr < max_instr) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100171 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100173 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 instr++;
177
Ingo Molnar107a0362009-02-20 20:37:05 +0100178 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
181 return prefetch;
182}
183
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100184static void
185force_sig_info_fault(int si_signo, int si_code, unsigned long address,
186 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100187{
188 siginfo_t info;
189
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100190 info.si_signo = si_signo;
191 info.si_errno = 0;
192 info.si_code = si_code;
193 info.si_addr = (void __user *)address;
194
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100195 force_sig_info(si_signo, &info, tsk);
196}
197
Harvey Harrison1156e092008-01-30 13:34:10 +0100198#ifdef CONFIG_X86_64
Harvey Harrison33cb5242008-01-30 13:32:19 +0100199static int bad_address(void *p)
200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned long dummy;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100202
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100203 return probe_kernel_address((unsigned long *)p, dummy);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100204}
Harvey Harrison1156e092008-01-30 13:34:10 +0100205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Adrian Bunkcae30f82008-02-13 23:31:31 +0200207static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Harvey Harrison1156e092008-01-30 13:34:10 +0100209#ifdef CONFIG_X86_32
210 __typeof__(pte_val(__pte(0))) page;
211
212 page = read_cr3();
213 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100214
Harvey Harrison1156e092008-01-30 13:34:10 +0100215#ifdef CONFIG_X86_PAE
216 printk("*pdpt = %016Lx ", page);
217 if ((page >> PAGE_SHIFT) < max_low_pfn
218 && page & _PAGE_PRESENT) {
219 page &= PAGE_MASK;
220 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100221 & (PTRS_PER_PMD - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100222 printk(KERN_CONT "*pde = %016Lx ", page);
223 page &= ~_PAGE_NX;
224 }
225#else
226 printk("*pde = %08lx ", page);
227#endif
228
229 /*
230 * We must not directly access the pte in the highpte
231 * case if the page table is located in highmem.
232 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100233 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100234 */
235 if ((page >> PAGE_SHIFT) < max_low_pfn
236 && (page & _PAGE_PRESENT)
237 && !(page & _PAGE_PSE)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100238
Harvey Harrison1156e092008-01-30 13:34:10 +0100239 page &= PAGE_MASK;
240 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100241 & (PTRS_PER_PTE - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100242 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
243 }
244
245 printk("\n");
246#else /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 pgd_t *pgd;
248 pud_t *pud;
249 pmd_t *pmd;
250 pte_t *pte;
251
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200252 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Harvey Harrison33cb5242008-01-30 13:32:19 +0100254 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 pgd += pgd_index(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100257 if (bad_address(pgd))
258 goto bad;
259
Jan Beulichd646bce2006-02-03 21:51:47 +0100260 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100261
262 if (!pgd_present(*pgd))
263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Andi Kleend2ae5b52006-06-26 13:57:56 +0200265 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100266 if (bad_address(pud))
267 goto bad;
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100270 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100274 if (bad_address(pmd))
275 goto bad;
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100278 if (!pmd_present(*pmd) || pmd_large(*pmd))
279 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100282 if (bad_address(pte))
283 goto bad;
284
Harvey Harrison33cb5242008-01-30 13:32:19 +0100285 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100286out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 printk("\n");
288 return;
289bad:
290 printk("BAD\n");
Harvey Harrison1156e092008-01-30 13:34:10 +0100291#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Harvey Harrison1156e092008-01-30 13:34:10 +0100294#ifdef CONFIG_X86_32
295static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
296{
297 unsigned index = pgd_index(address);
298 pgd_t *pgd_k;
299 pud_t *pud, *pud_k;
300 pmd_t *pmd, *pmd_k;
301
302 pgd += index;
303 pgd_k = init_mm.pgd + index;
304
305 if (!pgd_present(*pgd_k))
306 return NULL;
307
308 /*
309 * set_pgd(pgd, *pgd_k); here would be useless on PAE
310 * and redundant with the set_pmd() on non-PAE. As would
311 * set_pud.
312 */
Harvey Harrison1156e092008-01-30 13:34:10 +0100313 pud = pud_offset(pgd, address);
314 pud_k = pud_offset(pgd_k, address);
315 if (!pud_present(*pud_k))
316 return NULL;
317
318 pmd = pmd_offset(pud, address);
319 pmd_k = pmd_offset(pud_k, address);
320 if (!pmd_present(*pmd_k))
321 return NULL;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100322
Harvey Harrison1156e092008-01-30 13:34:10 +0100323 if (!pmd_present(*pmd)) {
324 set_pmd(pmd, *pmd_k);
325 arch_flush_lazy_mmu_mode();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100326 } else {
Harvey Harrison1156e092008-01-30 13:34:10 +0100327 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100328 }
329
Harvey Harrison1156e092008-01-30 13:34:10 +0100330 return pmd_k;
331}
Harvey Harrison1156e092008-01-30 13:34:10 +0100332
Ingo Molnar8c938f92009-02-20 22:12:18 +0100333/*
334 * Did it hit the DOS screen memory VA from vm86 mode?
335 */
336static inline void
337check_v8086_mode(struct pt_regs *regs, unsigned long address,
338 struct task_struct *tsk)
339{
340 unsigned long bit;
341
342 if (!v8086_mode(regs))
343 return;
344
345 bit = (address - 0xA0000) >> PAGE_SHIFT;
346 if (bit < 32)
347 tsk->thread.screen_bitmap |= 1 << bit;
348}
349
350#else /* CONFIG_X86_64: */
351
Harvey Harrison33cb5242008-01-30 13:32:19 +0100352static const char errata93_warning[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
354KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
355KERN_ERR "******* Please consider a BIOS update.\n"
356KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
Ingo Molnar8c938f92009-02-20 22:12:18 +0100357
358/*
359 * No vm86 mode in 64-bit mode:
360 */
361static inline void
362check_v8086_mode(struct pt_regs *regs, unsigned long address,
363 struct task_struct *tsk)
364{
365}
366
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100369/*
370 * Workaround for K8 erratum #93 & buggy BIOS.
371 *
372 * BIOS SMM functions are required to use a specific workaround
373 * to avoid corruption of the 64bit RIP register on C stepping K8.
374 *
375 * A lot of BIOS that didn't get tested properly miss this.
376 *
377 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
378 * Try to work around it here.
379 *
380 * Note we only handle faults in kernel here.
381 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100382 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100383static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100385#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100386 static int once;
387
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100388 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100390
Harvey Harrison33cb5242008-01-30 13:32:19 +0100391 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100395 if ((address >= (u64)_stext && address <= (u64)_etext) ||
396 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100397 if (!once) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100398 printk(errata93_warning);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100399 once = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100401 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return 1;
403 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Harvey Harrison35f32662008-01-30 13:34:09 +0100408/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100409 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
410 * to illegal addresses >4GB.
411 *
412 * We catch this in the page fault handler because these addresses
413 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100414 * segment in LDT is compatibility mode.
415 */
416static int is_errata100(struct pt_regs *regs, unsigned long address)
417{
418#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100419 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100420 return 1;
421#endif
422 return 0;
423}
424
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100425static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
426{
427#ifdef CONFIG_X86_F00F_BUG
428 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100429
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100430 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100431 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100432 */
433 if (boot_cpu_data.f00f_bug) {
434 nr = (address - idt_descr.address) >> 3;
435
436 if (nr == 6) {
437 do_invalid_op(regs, 0);
438 return 1;
439 }
440 }
441#endif
442 return 0;
443}
444
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100445static void
446show_fault_oops(struct pt_regs *regs, unsigned long error_code,
447 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100448{
Harvey Harrison1156e092008-01-30 13:34:10 +0100449#ifdef CONFIG_X86_32
450 if (!oops_may_print())
451 return;
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100452#endif
Harvey Harrison1156e092008-01-30 13:34:10 +0100453
454#ifdef CONFIG_X86_PAE
455 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100456 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100457
Harvey Harrison1156e092008-01-30 13:34:10 +0100458 pte_t *pte = lookup_address(address, &level);
459
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100460 if (pte && pte_present(*pte) && !pte_exec(*pte)) {
Harvey Harrison1156e092008-01-30 13:34:10 +0100461 printk(KERN_CRIT "kernel tried to execute "
462 "NX-protected page - exploit attempt? "
David Howells350b4da2008-11-14 10:38:40 +1100463 "(uid: %d)\n", current_uid());
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100464 }
Harvey Harrison1156e092008-01-30 13:34:10 +0100465 }
466#endif
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100467
Harvey Harrison1156e092008-01-30 13:34:10 +0100468 printk(KERN_ALERT "BUG: unable to handle kernel ");
469 if (address < PAGE_SIZE)
470 printk(KERN_CONT "NULL pointer dereference");
471 else
472 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100473
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200474 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100475 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100476 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100477
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100478 dump_pagetable(address);
479}
480
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100481static noinline void
482pgtable_bad(struct pt_regs *regs, unsigned long error_code,
483 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100485 struct task_struct *tsk;
486 unsigned long flags;
487 int sig;
488
489 flags = oops_begin();
490 tsk = current;
491 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100494 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100496
497 tsk->thread.cr2 = address;
498 tsk->thread.trap_no = 14;
499 tsk->thread.error_code = error_code;
500
Jan Beulich22f59912008-01-30 13:31:23 +0100501 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200502 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100503
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200504 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100507static noinline void
508no_context(struct pt_regs *regs, unsigned long error_code,
509 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100510{
511 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100512 unsigned long *stackend;
513
Nick Piggin92181f12009-01-20 04:24:26 +0100514#ifdef CONFIG_X86_64
515 unsigned long flags;
516 int sig;
517#endif
518
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100519 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100520 if (fixup_exception(regs))
521 return;
522
523 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100524 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100525 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100526 * Valid to do another page fault here, because if this fault
527 * had been triggered by is_prefetch fixup_exception would have
528 * handled it.
529 *
530 * 64-bit:
531 *
532 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100533 */
534 if (is_prefetch(regs, error_code, address))
535 return;
536
537 if (is_errata93(regs, address))
538 return;
539
540 /*
541 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100542 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100543 */
544#ifdef CONFIG_X86_32
545 bust_spinlocks(1);
546#else
547 flags = oops_begin();
548#endif
549
550 show_fault_oops(regs, error_code, address);
551
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100552 stackend = end_of_stack(tsk);
Ingo Molnar19803072009-01-21 10:39:51 +0100553 if (*stackend != STACK_END_MAGIC)
554 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
555
Nick Piggin92181f12009-01-20 04:24:26 +0100556 tsk->thread.cr2 = address;
557 tsk->thread.trap_no = 14;
558 tsk->thread.error_code = error_code;
559
560#ifdef CONFIG_X86_32
561 die("Oops", regs, error_code);
562 bust_spinlocks(0);
563 do_exit(SIGKILL);
564#else
565 sig = SIGKILL;
566 if (__die("Oops", regs, error_code))
567 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100568
Nick Piggin92181f12009-01-20 04:24:26 +0100569 /* Executive summary in case the body of the oops scrolled away */
570 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100571
Nick Piggin92181f12009-01-20 04:24:26 +0100572 oops_end(flags, regs, sig);
573#endif
574}
575
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100576/*
577 * Print out info about fatal segfaults, if the show_unhandled_signals
578 * sysctl is set:
579 */
580static inline void
581show_signal_msg(struct pt_regs *regs, unsigned long error_code,
582 unsigned long address, struct task_struct *tsk)
583{
584 if (!unhandled_signal(tsk, SIGSEGV))
585 return;
586
587 if (!printk_ratelimit())
588 return;
589
590 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
591 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
592 tsk->comm, task_pid_nr(tsk), address,
593 (void *)regs->ip, (void *)regs->sp, error_code);
594
595 print_vma_addr(KERN_CONT " in ", regs->ip);
596
597 printk(KERN_CONT "\n");
598}
599
600static void
601__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
602 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100603{
604 struct task_struct *tsk = current;
605
606 /* User mode accesses just cause a SIGSEGV */
607 if (error_code & PF_USER) {
608 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100609 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100610 */
611 local_irq_enable();
612
613 /*
614 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100615 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100616 */
617 if (is_prefetch(regs, error_code, address))
618 return;
619
620 if (is_errata100(regs, address))
621 return;
622
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100623 if (unlikely(show_unhandled_signals))
624 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100625
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100626 /* Kernel addresses are always protection faults: */
627 tsk->thread.cr2 = address;
628 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
629 tsk->thread.trap_no = 14;
630
Nick Piggin92181f12009-01-20 04:24:26 +0100631 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100632
Nick Piggin92181f12009-01-20 04:24:26 +0100633 return;
634 }
635
636 if (is_f00f_bug(regs, address))
637 return;
638
639 no_context(regs, error_code, address);
640}
641
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100642static noinline void
643bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
644 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100645{
646 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
647}
648
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100649static void
650__bad_area(struct pt_regs *regs, unsigned long error_code,
651 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100652{
653 struct mm_struct *mm = current->mm;
654
655 /*
656 * Something tried to access memory that isn't in our memory map..
657 * Fix it, but check if it's kernel or user first..
658 */
659 up_read(&mm->mmap_sem);
660
661 __bad_area_nosemaphore(regs, error_code, address, si_code);
662}
663
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100664static noinline void
665bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100666{
667 __bad_area(regs, error_code, address, SEGV_MAPERR);
668}
669
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100670static noinline void
671bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
672 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100673{
674 __bad_area(regs, error_code, address, SEGV_ACCERR);
675}
676
677/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100678static void
679out_of_memory(struct pt_regs *regs, unsigned long error_code,
680 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100681{
682 /*
683 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100684 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100685 */
686 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100687
Nick Piggin92181f12009-01-20 04:24:26 +0100688 pagefault_out_of_memory();
689}
690
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100691static void
692do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100693{
694 struct task_struct *tsk = current;
695 struct mm_struct *mm = tsk->mm;
696
697 up_read(&mm->mmap_sem);
698
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100699 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100700 if (!(error_code & PF_USER))
701 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100702
Nick Piggin92181f12009-01-20 04:24:26 +0100703#ifdef CONFIG_X86_32
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100704 /* User space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100705 if (is_prefetch(regs, error_code, address))
706 return;
707#endif
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100708
709 tsk->thread.cr2 = address;
710 tsk->thread.error_code = error_code;
711 tsk->thread.trap_no = 14;
712
Nick Piggin92181f12009-01-20 04:24:26 +0100713 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
714}
715
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100716static noinline void
717mm_fault_error(struct pt_regs *regs, unsigned long error_code,
718 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100719{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100720 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100721 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100722 } else {
723 if (fault & VM_FAULT_SIGBUS)
724 do_sigbus(regs, error_code, address);
725 else
726 BUG();
727 }
Nick Piggin92181f12009-01-20 04:24:26 +0100728}
729
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100730static int spurious_fault_check(unsigned long error_code, pte_t *pte)
731{
732 if ((error_code & PF_WRITE) && !pte_write(*pte))
733 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100734
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100735 if ((error_code & PF_INSTR) && !pte_exec(*pte))
736 return 0;
737
738 return 1;
739}
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100742 * Handle a spurious fault caused by a stale TLB entry.
743 *
744 * This allows us to lazily refresh the TLB when increasing the
745 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
746 * eagerly is very expensive since that implies doing a full
747 * cross-processor TLB flush, even if no stale TLB entries exist
748 * on other processors.
749 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100750 * There are no security implications to leaving a stale TLB when
751 * increasing the permissions on a page.
752 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100753static noinline int
754spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100755{
756 pgd_t *pgd;
757 pud_t *pud;
758 pmd_t *pmd;
759 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500760 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100761
762 /* Reserved-bit violation or user access to kernel space? */
763 if (error_code & (PF_USER | PF_RSVD))
764 return 0;
765
766 pgd = init_mm.pgd + pgd_index(address);
767 if (!pgd_present(*pgd))
768 return 0;
769
770 pud = pud_offset(pgd, address);
771 if (!pud_present(*pud))
772 return 0;
773
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100774 if (pud_large(*pud))
775 return spurious_fault_check(error_code, (pte_t *) pud);
776
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100777 pmd = pmd_offset(pud, address);
778 if (!pmd_present(*pmd))
779 return 0;
780
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100781 if (pmd_large(*pmd))
782 return spurious_fault_check(error_code, (pte_t *) pmd);
783
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100784 pte = pte_offset_kernel(pmd, address);
785 if (!pte_present(*pte))
786 return 0;
787
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500788 ret = spurious_fault_check(error_code, pte);
789 if (!ret)
790 return 0;
791
792 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100793 * Make sure we have permissions in PMD.
794 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500795 */
796 ret = spurious_fault_check(error_code, (pte_t *) pmd);
797 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100798
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500799 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100800}
801
802/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100803 * 32-bit:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100804 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100805 * Handle a fault on the vmalloc or module mapping area
806 *
807 * 64-bit:
808 *
809 * Handle a fault on the vmalloc area
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700810 *
811 * This assumes no large pages in there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 */
Nick Piggin92181f12009-01-20 04:24:26 +0100813static noinline int vmalloc_fault(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100815#ifdef CONFIG_X86_32
816 unsigned long pgd_paddr;
817 pmd_t *pmd_k;
818 pte_t *pte_k;
Henry Nestlerb29c7012008-05-12 15:44:39 +0200819
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100820 /* Make sure we are in vmalloc area: */
Henry Nestlerb29c7012008-05-12 15:44:39 +0200821 if (!(address >= VMALLOC_START && address < VMALLOC_END))
822 return -1;
823
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100824 /*
825 * Synchronize this task's top level page-table
826 * with the 'reference' page table.
827 *
828 * Do _not_ use "current" here. We might be inside
829 * an interrupt in the middle of a task switch..
830 */
831 pgd_paddr = read_cr3();
832 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
833 if (!pmd_k)
834 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100835
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100836 pte_k = pte_offset_kernel(pmd_k, address);
837 if (!pte_present(*pte_k))
838 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100839
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100840 return 0;
841#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 pgd_t *pgd, *pgd_ref;
843 pud_t *pud, *pud_ref;
844 pmd_t *pmd, *pmd_ref;
845 pte_t *pte, *pte_ref;
846
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100847 /* Make sure we are in vmalloc area: */
Harvey Harrisoncf89ec92008-02-04 16:47:56 +0100848 if (!(address >= VMALLOC_START && address < VMALLOC_END))
849 return -1;
850
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100851 /*
852 * Copy kernel mappings over when needed. This can also
853 * happen within a race in page table update. In the later
854 * case just flush:
855 */
Andi Kleenf313e122009-01-09 12:17:43 -0800856 pgd = pgd_offset(current->active_mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 pgd_ref = pgd_offset_k(address);
858 if (pgd_none(*pgd_ref))
859 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (pgd_none(*pgd))
862 set_pgd(pgd, *pgd_ref);
Jan Beulich8c914cb2006-03-25 16:29:40 +0100863 else
Dave McCracken46a82b22006-09-25 23:31:48 -0700864 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100866 /*
867 * Below here mismatches are bugs because these lower tables
868 * are shared:
869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 pud = pud_offset(pgd, address);
872 pud_ref = pud_offset(pgd_ref, address);
873 if (pud_none(*pud_ref))
874 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100875
Dave McCracken46a82b22006-09-25 23:31:48 -0700876 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 pmd = pmd_offset(pud, address);
880 pmd_ref = pmd_offset(pud_ref, address);
881 if (pmd_none(*pmd_ref))
882 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
885 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 pte_ref = pte_offset_kernel(pmd_ref, address);
888 if (!pte_present(*pte_ref))
889 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100892
893 /*
894 * Don't use pte_page here, because the mappings can point
895 * outside mem_map, and the NUMA hash lookup cannot handle
896 * that:
897 */
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700898 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return 0;
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903}
904
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200905int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100907static inline int
908access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100909{
910 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100911 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100912 if (unlikely(!(vma->vm_flags & VM_WRITE)))
913 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100914 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100915 }
916
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100917 /* read, present: */
918 if (unlikely(error_code & PF_PROT))
919 return 1;
920
921 /* read, not present: */
922 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
923 return 1;
924
Nick Piggin92181f12009-01-20 04:24:26 +0100925 return 0;
926}
927
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800928static int fault_in_kernel_space(unsigned long address)
929{
930#ifdef CONFIG_X86_32
931 return address >= TASK_SIZE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100932#else
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800933 return address >= TASK_SIZE64;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100934#endif
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
938 * This routine handles page faults. It determines the address,
939 * and the problem, and then passes it off to one of the appropriate
940 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100942#ifdef CONFIG_X86_64
943asmlinkage
944#endif
945void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100947 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100948 struct task_struct *tsk;
949 unsigned long address;
950 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100951 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100952 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100954 tsk = current;
955 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100956
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100957 prefetchw(&mm->mmap_sem);
958
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100959 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200960 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200962 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200963 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /*
966 * We fault-in kernel-space virtual memory on-demand. The
967 * 'reference' page table is init_mm.pgd.
968 *
969 * NOTE! We MUST NOT take any locks for this case. We may
970 * be in an interrupt or a critical region, and should
971 * only copy the information from the master page table,
972 * nothing more.
973 *
974 * This verifies that the fault happens in kernel space
975 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +0100976 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800978 if (unlikely(fault_in_kernel_space(address))) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100979 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
980 vmalloc_fault(address) >= 0)
981 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100982
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100983 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +0100984 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100985 return;
986
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100987 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -0500988 if (notify_page_fault(regs))
989 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100990 /*
991 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100992 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100993 */
Nick Piggin92181f12009-01-20 04:24:26 +0100994 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100995
Nick Piggin92181f12009-01-20 04:24:26 +0100996 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100997 }
998
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100999 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +01001000 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -05001001 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001002 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -07001003 * It's safe to allow irq's after cr2 has been saved and the
1004 * vmalloc fault has been handled.
1005 *
1006 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001007 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +01001008 */
Linus Torvalds891cffb2008-10-12 13:16:12 -07001009 if (user_mode_vm(regs)) {
1010 local_irq_enable();
1011 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001012 } else {
1013 if (regs->flags & X86_EFLAGS_IF)
1014 local_irq_enable();
1015 }
Jan Beulich8c914cb2006-03-25 16:29:40 +01001016
Andi Kleen66c58152006-01-11 22:44:09 +01001017 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +01001018 pgtable_bad(regs, error_code, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001021 * If we're in an interrupt, have no user context or are running
1022 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 */
Nick Piggin92181f12009-01-20 04:24:26 +01001024 if (unlikely(in_atomic() || !mm)) {
1025 bad_area_nosemaphore(regs, error_code, address);
1026 return;
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001029 /*
1030 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001031 * addresses in user space. All other faults represent errors in
1032 * the kernel and should generate an OOPS. Unfortunately, in the
1033 * case of an erroneous fault occurring in a code path which already
1034 * holds mmap_sem we will deadlock attempting to validate the fault
1035 * against the address space. Luckily the kernel only validly
1036 * references user space from well defined areas of code, which are
1037 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 *
1039 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001040 * the source reference check when there is a possibility of a
1041 * deadlock. Attempt to lock the address space, if we cannot we then
1042 * validate the source. If this is invalid we can skip the address
1043 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Nick Piggin92181f12009-01-20 04:24:26 +01001045 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001046 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001047 !search_exception_tables(regs->ip)) {
1048 bad_area_nosemaphore(regs, error_code, address);
1049 return;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001052 } else {
1053 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001054 * The above down_read_trylock() might have succeeded in
1055 * which case we'll have missed the might_sleep() from
1056 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001057 */
1058 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060
1061 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001062 if (unlikely(!vma)) {
1063 bad_area(regs, error_code, address);
1064 return;
1065 }
1066 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001068 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1069 bad_area(regs, error_code, address);
1070 return;
1071 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001072 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001073 /*
1074 * Accessing the stack below %sp is always a bug.
1075 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001076 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001077 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001078 */
Nick Piggin92181f12009-01-20 04:24:26 +01001079 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1080 bad_area(regs, error_code, address);
1081 return;
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
Nick Piggin92181f12009-01-20 04:24:26 +01001084 if (unlikely(expand_stack(vma, address))) {
1085 bad_area(regs, error_code, address);
1086 return;
1087 }
1088
1089 /*
1090 * Ok, we have a good vm_area for this memory access, so
1091 * we can handle it..
1092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001094 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001095
Nick Piggin92181f12009-01-20 04:24:26 +01001096 if (unlikely(access_error(error_code, write, vma))) {
1097 bad_area_access_error(regs, error_code, address);
1098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 /*
1102 * If for any reason at all we couldn't handle the fault,
1103 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001104 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Nick Piggin83c54072007-07-19 01:47:05 -07001106 fault = handle_mm_fault(mm, vma, address, write);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001107
Nick Piggin83c54072007-07-19 01:47:05 -07001108 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001109 mm_fault_error(regs, error_code, address, fault);
1110 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001112
Nick Piggin83c54072007-07-19 01:47:05 -07001113 if (fault & VM_FAULT_MAJOR)
1114 tsk->maj_flt++;
1115 else
1116 tsk->min_flt++;
Harvey Harrisond729ab32008-01-30 13:33:23 +01001117
Ingo Molnar8c938f92009-02-20 22:12:18 +01001118 check_v8086_mode(regs, address, tsk);
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
Andi Kleen9e43e1b2005-11-05 17:25:54 +01001122
Jan Beulich8c914cb2006-03-25 16:29:40 +01001123DEFINE_SPINLOCK(pgd_lock);
Christoph Lameter2bff7382007-05-02 19:27:10 +02001124LIST_HEAD(pgd_list);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001125
1126void vmalloc_sync_all(void)
1127{
Harvey Harrison1156e092008-01-30 13:34:10 +01001128 unsigned long address;
1129
Jan Beulichcc643d42008-08-29 12:53:45 +01001130#ifdef CONFIG_X86_32
Harvey Harrison1156e092008-01-30 13:34:10 +01001131 if (SHARED_KERNEL_PMD)
1132 return;
1133
Jan Beulichcc643d42008-08-29 12:53:45 +01001134 for (address = VMALLOC_START & PMD_MASK;
1135 address >= TASK_SIZE && address < FIXADDR_TOP;
1136 address += PMD_SIZE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001137
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001138 unsigned long flags;
1139 struct page *page;
Harvey Harrison1156e092008-01-30 13:34:10 +01001140
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001141 spin_lock_irqsave(&pgd_lock, flags);
1142 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001143 if (!vmalloc_sync_one(page_address(page), address))
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001144 break;
Harvey Harrison1156e092008-01-30 13:34:10 +01001145 }
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001146 spin_unlock_irqrestore(&pgd_lock, flags);
Harvey Harrison1156e092008-01-30 13:34:10 +01001147 }
1148#else /* CONFIG_X86_64 */
Jan Beulichcc643d42008-08-29 12:53:45 +01001149 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
1150 address += PGDIR_SIZE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001151
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001152 const pgd_t *pgd_ref = pgd_offset_k(address);
1153 unsigned long flags;
1154 struct page *page;
Jan Beulich8c914cb2006-03-25 16:29:40 +01001155
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001156 if (pgd_none(*pgd_ref))
1157 continue;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001158
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001159 spin_lock_irqsave(&pgd_lock, flags);
1160 list_for_each_entry(page, &pgd_list, lru) {
1161 pgd_t *pgd;
1162 pgd = (pgd_t *)page_address(page) + pgd_index(address);
1163 if (pgd_none(*pgd))
1164 set_pgd(pgd, *pgd_ref);
1165 else
1166 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Jan Beulich8c914cb2006-03-25 16:29:40 +01001167 }
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001168 spin_unlock_irqrestore(&pgd_lock, flags);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001169 }
Harvey Harrison1156e092008-01-30 13:34:10 +01001170#endif
Jan Beulich8c914cb2006-03-25 16:29:40 +01001171}