blob: 351d679bf977e7f54507b9833716b2413b01a97f [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
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020058static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
Pekka Paalanen86069782008-05-12 21:20:56 +020059{
Pekka Paalanenfd3fdf12008-10-24 20:08:11 +030060#ifdef CONFIG_MMIOTRACE
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020061 if (unlikely(is_kmmio_active()))
62 if (kmmio_handler(regs, addr) == 1)
63 return -1;
Pekka Paalanen86069782008-05-12 21:20:56 +020064#endif
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +020065 return 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020066}
67
Christoph Hellwig74a0b572007-10-16 01:24:07 -070068static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070069{
Harvey Harrison33cb5242008-01-30 13:32:19 +010070#ifdef CONFIG_KPROBES
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() */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +010074 if (!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;
Christoph Hellwig74a0b572007-10-16 01:24:07 -070082#else
Christoph Hellwig74a0b572007-10-16 01:24:07 -070083 return 0;
Christoph Hellwig74a0b572007-10-16 01:24:07 -070084#endif
Harvey Harrison33cb5242008-01-30 13:32:19 +010085}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -070086
Harvey Harrison1dc85be2008-01-30 13:32:35 +010087/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +010088 * Prefetch quirks:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010089 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010090 * 32-bit mode:
Harvey Harrison1dc85be2008-01-30 13:32:35 +010091 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +010092 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
93 * Check that here and ignore it.
94 *
95 * 64-bit mode:
96 *
97 * Sometimes the CPU reports invalid exceptions on prefetch.
98 * Check that here and ignore it.
99 *
100 * Opcode checker based on code by Richard Brunner.
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100101 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100102static int
103is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100104{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100105 unsigned char *max_instr;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100106 unsigned char *instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 int scan_more = 1;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100108 int prefetch = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Ingo Molnar30853542008-03-27 21:29:09 +0100110 /*
111 * If it was a exec (instruction fetch) fault on NX page, then
112 * do not ignore the fault:
113 */
Andi Kleen66c58152006-01-11 22:44:09 +0100114 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100116
Harvey Harrisonf2857ce2008-01-30 13:33:12 +0100117 instr = (unsigned char *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700118 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700120 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122
Harvey Harrison33cb5242008-01-30 13:32:19 +0100123 while (scan_more && instr < max_instr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 unsigned char instr_hi;
125 unsigned char instr_lo;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100126 unsigned char opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100128 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100129 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Harvey Harrison33cb5242008-01-30 13:32:19 +0100131 instr_hi = opcode & 0xf0;
132 instr_lo = opcode & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 instr++;
134
Harvey Harrison33cb5242008-01-30 13:32:19 +0100135 switch (instr_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 case 0x20:
137 case 0x30:
Harvey Harrison33cb5242008-01-30 13:32:19 +0100138 /*
139 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
140 * In X86_64 long mode, the CPU will signal invalid
141 * opcode if some of these prefixes are present so
142 * X86_64 will never get here anyway
143 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 scan_more = ((instr_lo & 7) == 0x6);
145 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100146#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 case 0x40:
Harvey Harrison33cb5242008-01-30 13:32:19 +0100148 /*
149 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
150 * Need to figure out under what instruction mode the
151 * instruction was issued. Could check the LDT for lm,
152 * but for now it's good enough to assume that long
153 * mode only uses well known segments or kernel.
154 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700155 scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 case 0x60:
159 /* 0x64 thru 0x67 are valid prefixes in all modes. */
160 scan_more = (instr_lo & 0xC) == 0x4;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100161 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 case 0xF0:
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100163 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 scan_more = !instr_lo || (instr_lo>>1) == 1;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100165 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 case 0x00:
167 /* Prefetch instruction is 0x0F0D or 0x0F18 */
168 scan_more = 0;
Harvey Harrisonf2857ce2008-01-30 13:33:12 +0100169
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100170 if (probe_kernel_address(instr, opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 break;
172 prefetch = (instr_lo == 0xF) &&
173 (opcode == 0x0D || opcode == 0x18);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100174 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 default:
176 scan_more = 0;
177 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 return prefetch;
181}
182
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100183static void
184force_sig_info_fault(int si_signo, int si_code, unsigned long address,
185 struct task_struct *tsk)
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100186{
187 siginfo_t info;
188
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100189 info.si_signo = si_signo;
190 info.si_errno = 0;
191 info.si_code = si_code;
192 info.si_addr = (void __user *)address;
193
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100194 force_sig_info(si_signo, &info, tsk);
195}
196
Harvey Harrison1156e092008-01-30 13:34:10 +0100197#ifdef CONFIG_X86_64
Harvey Harrison33cb5242008-01-30 13:32:19 +0100198static int bad_address(void *p)
199{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 unsigned long dummy;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100201
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100202 return probe_kernel_address((unsigned long *)p, dummy);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100203}
Harvey Harrison1156e092008-01-30 13:34:10 +0100204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Adrian Bunkcae30f82008-02-13 23:31:31 +0200206static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Harvey Harrison1156e092008-01-30 13:34:10 +0100208#ifdef CONFIG_X86_32
209 __typeof__(pte_val(__pte(0))) page;
210
211 page = read_cr3();
212 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100213
Harvey Harrison1156e092008-01-30 13:34:10 +0100214#ifdef CONFIG_X86_PAE
215 printk("*pdpt = %016Lx ", page);
216 if ((page >> PAGE_SHIFT) < max_low_pfn
217 && page & _PAGE_PRESENT) {
218 page &= PAGE_MASK;
219 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100220 & (PTRS_PER_PMD - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100221 printk(KERN_CONT "*pde = %016Lx ", page);
222 page &= ~_PAGE_NX;
223 }
224#else
225 printk("*pde = %08lx ", page);
226#endif
227
228 /*
229 * We must not directly access the pte in the highpte
230 * case if the page table is located in highmem.
231 * And let's rather not kmap-atomic the pte, just in case
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100232 * it's allocated already:
Harvey Harrison1156e092008-01-30 13:34:10 +0100233 */
234 if ((page >> PAGE_SHIFT) < max_low_pfn
235 && (page & _PAGE_PRESENT)
236 && !(page & _PAGE_PSE)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100237
Harvey Harrison1156e092008-01-30 13:34:10 +0100238 page &= PAGE_MASK;
239 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100240 & (PTRS_PER_PTE - 1)];
Harvey Harrison1156e092008-01-30 13:34:10 +0100241 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
242 }
243
244 printk("\n");
245#else /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 pgd_t *pgd;
247 pud_t *pud;
248 pmd_t *pmd;
249 pte_t *pte;
250
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200251 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Harvey Harrison33cb5242008-01-30 13:32:19 +0100253 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 pgd += pgd_index(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100256 if (bad_address(pgd))
257 goto bad;
258
Jan Beulichd646bce2006-02-03 21:51:47 +0100259 printk("PGD %lx ", pgd_val(*pgd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100260
261 if (!pgd_present(*pgd))
262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Andi Kleend2ae5b52006-06-26 13:57:56 +0200264 pud = pud_offset(pgd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100265 if (bad_address(pud))
266 goto bad;
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100269 if (!pud_present(*pud) || pud_large(*pud))
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 pmd = pmd_offset(pud, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100273 if (bad_address(pmd))
274 goto bad;
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 printk("PMD %lx ", pmd_val(*pmd));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100277 if (!pmd_present(*pmd) || pmd_large(*pmd))
278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100281 if (bad_address(pte))
282 goto bad;
283
Harvey Harrison33cb5242008-01-30 13:32:19 +0100284 printk("PTE %lx", pte_val(*pte));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100285out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 printk("\n");
287 return;
288bad:
289 printk("BAD\n");
Harvey Harrison1156e092008-01-30 13:34:10 +0100290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Harvey Harrison1156e092008-01-30 13:34:10 +0100293#ifdef CONFIG_X86_32
294static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
295{
296 unsigned index = pgd_index(address);
297 pgd_t *pgd_k;
298 pud_t *pud, *pud_k;
299 pmd_t *pmd, *pmd_k;
300
301 pgd += index;
302 pgd_k = init_mm.pgd + index;
303
304 if (!pgd_present(*pgd_k))
305 return NULL;
306
307 /*
308 * set_pgd(pgd, *pgd_k); here would be useless on PAE
309 * and redundant with the set_pmd() on non-PAE. As would
310 * set_pud.
311 */
Harvey Harrison1156e092008-01-30 13:34:10 +0100312 pud = pud_offset(pgd, address);
313 pud_k = pud_offset(pgd_k, address);
314 if (!pud_present(*pud_k))
315 return NULL;
316
317 pmd = pmd_offset(pud, address);
318 pmd_k = pmd_offset(pud_k, address);
319 if (!pmd_present(*pmd_k))
320 return NULL;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100321
Harvey Harrison1156e092008-01-30 13:34:10 +0100322 if (!pmd_present(*pmd)) {
323 set_pmd(pmd, *pmd_k);
324 arch_flush_lazy_mmu_mode();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100325 } else {
Harvey Harrison1156e092008-01-30 13:34:10 +0100326 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100327 }
328
Harvey Harrison1156e092008-01-30 13:34:10 +0100329 return pmd_k;
330}
331#endif
332
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100333#ifdef CONFIG_X86_64
Harvey Harrison33cb5242008-01-30 13:32:19 +0100334static const char errata93_warning[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
336KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
337KERN_ERR "******* Please consider a BIOS update.\n"
338KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100341/*
342 * Workaround for K8 erratum #93 & buggy BIOS.
343 *
344 * BIOS SMM functions are required to use a specific workaround
345 * to avoid corruption of the 64bit RIP register on C stepping K8.
346 *
347 * A lot of BIOS that didn't get tested properly miss this.
348 *
349 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
350 * Try to work around it here.
351 *
352 * Note we only handle faults in kernel here.
353 * Does nothing on 32-bit.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100354 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100355static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100357#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100358 static int once;
359
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100360 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100362
Harvey Harrison33cb5242008-01-30 13:32:19 +0100363 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100367 if ((address >= (u64)_stext && address <= (u64)_etext) ||
368 (address >= MODULES_VADDR && address <= MODULES_END)) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100369 if (!once) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100370 printk(errata93_warning);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100371 once = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100373 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 return 1;
375 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100376#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Harvey Harrison35f32662008-01-30 13:34:09 +0100380/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100381 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
382 * to illegal addresses >4GB.
383 *
384 * We catch this in the page fault handler because these addresses
385 * are not reachable. Just detect this case and return. Any code
Harvey Harrison35f32662008-01-30 13:34:09 +0100386 * segment in LDT is compatibility mode.
387 */
388static int is_errata100(struct pt_regs *regs, unsigned long address)
389{
390#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100391 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
Harvey Harrison35f32662008-01-30 13:34:09 +0100392 return 1;
393#endif
394 return 0;
395}
396
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100397static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
398{
399#ifdef CONFIG_X86_F00F_BUG
400 unsigned long nr;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100401
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100402 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100403 * Pentium F0 0F C7 C8 bug workaround:
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100404 */
405 if (boot_cpu_data.f00f_bug) {
406 nr = (address - idt_descr.address) >> 3;
407
408 if (nr == 6) {
409 do_invalid_op(regs, 0);
410 return 1;
411 }
412 }
413#endif
414 return 0;
415}
416
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100417static void
418show_fault_oops(struct pt_regs *regs, unsigned long error_code,
419 unsigned long address)
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100420{
Harvey Harrison1156e092008-01-30 13:34:10 +0100421#ifdef CONFIG_X86_32
422 if (!oops_may_print())
423 return;
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100424#endif
Harvey Harrison1156e092008-01-30 13:34:10 +0100425
426#ifdef CONFIG_X86_PAE
427 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100428 unsigned int level;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100429
Harvey Harrison1156e092008-01-30 13:34:10 +0100430 pte_t *pte = lookup_address(address, &level);
431
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100432 if (pte && pte_present(*pte) && !pte_exec(*pte)) {
Harvey Harrison1156e092008-01-30 13:34:10 +0100433 printk(KERN_CRIT "kernel tried to execute "
434 "NX-protected page - exploit attempt? "
David Howells350b4da2008-11-14 10:38:40 +1100435 "(uid: %d)\n", current_uid());
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100436 }
Harvey Harrison1156e092008-01-30 13:34:10 +0100437 }
438#endif
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100439
Harvey Harrison1156e092008-01-30 13:34:10 +0100440 printk(KERN_ALERT "BUG: unable to handle kernel ");
441 if (address < PAGE_SIZE)
442 printk(KERN_CONT "NULL pointer dereference");
443 else
444 printk(KERN_CONT "paging request");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100445
Vegard Nossumf294a8c2008-07-01 15:38:13 +0200446 printk(KERN_CONT " at %p\n", (void *) address);
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100447 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100448 printk_address(regs->ip, 1);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100449
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100450 dump_pagetable(address);
451}
452
Harvey Harrison1156e092008-01-30 13:34:10 +0100453#ifdef CONFIG_X86_64
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100454static noinline void
455pgtable_bad(struct pt_regs *regs, unsigned long error_code,
456 unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100458 struct task_struct *tsk;
459 unsigned long flags;
460 int sig;
461
462 flags = oops_begin();
463 tsk = current;
464 sig = SIGKILL;
Jan Beulich12091402005-09-12 18:49:24 +0200465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
Nick Piggin92181f12009-01-20 04:24:26 +0100467 tsk->comm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dump_pagetable(address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100469
470 tsk->thread.cr2 = address;
471 tsk->thread.trap_no = 14;
472 tsk->thread.error_code = error_code;
473
Jan Beulich22f59912008-01-30 13:31:23 +0100474 if (__die("Bad pagetable", regs, error_code))
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200475 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100476
Alexander van Heukelum874d93d2008-10-22 12:00:09 +0200477 oops_end(flags, regs, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
Harvey Harrison1156e092008-01-30 13:34:10 +0100479#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100481static noinline void
482no_context(struct pt_regs *regs, unsigned long error_code,
483 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100484{
485 struct task_struct *tsk = current;
Ingo Molnar19803072009-01-21 10:39:51 +0100486 unsigned long *stackend;
487
Nick Piggin92181f12009-01-20 04:24:26 +0100488#ifdef CONFIG_X86_64
489 unsigned long flags;
490 int sig;
491#endif
492
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100493 /* Are we prepared to handle this kernel fault? */
Nick Piggin92181f12009-01-20 04:24:26 +0100494 if (fixup_exception(regs))
495 return;
496
497 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100498 * 32-bit:
Nick Piggin92181f12009-01-20 04:24:26 +0100499 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100500 * Valid to do another page fault here, because if this fault
501 * had been triggered by is_prefetch fixup_exception would have
502 * handled it.
503 *
504 * 64-bit:
505 *
506 * Hall of shame of CPU/BIOS bugs.
Nick Piggin92181f12009-01-20 04:24:26 +0100507 */
508 if (is_prefetch(regs, error_code, address))
509 return;
510
511 if (is_errata93(regs, address))
512 return;
513
514 /*
515 * Oops. The kernel tried to access some bad page. We'll have to
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100516 * terminate things with extreme prejudice:
Nick Piggin92181f12009-01-20 04:24:26 +0100517 */
518#ifdef CONFIG_X86_32
519 bust_spinlocks(1);
520#else
521 flags = oops_begin();
522#endif
523
524 show_fault_oops(regs, error_code, address);
525
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100526 stackend = end_of_stack(tsk);
Ingo Molnar19803072009-01-21 10:39:51 +0100527 if (*stackend != STACK_END_MAGIC)
528 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
529
Nick Piggin92181f12009-01-20 04:24:26 +0100530 tsk->thread.cr2 = address;
531 tsk->thread.trap_no = 14;
532 tsk->thread.error_code = error_code;
533
534#ifdef CONFIG_X86_32
535 die("Oops", regs, error_code);
536 bust_spinlocks(0);
537 do_exit(SIGKILL);
538#else
539 sig = SIGKILL;
540 if (__die("Oops", regs, error_code))
541 sig = 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100542
Nick Piggin92181f12009-01-20 04:24:26 +0100543 /* Executive summary in case the body of the oops scrolled away */
544 printk(KERN_EMERG "CR2: %016lx\n", address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100545
Nick Piggin92181f12009-01-20 04:24:26 +0100546 oops_end(flags, regs, sig);
547#endif
548}
549
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100550/*
551 * Print out info about fatal segfaults, if the show_unhandled_signals
552 * sysctl is set:
553 */
554static inline void
555show_signal_msg(struct pt_regs *regs, unsigned long error_code,
556 unsigned long address, struct task_struct *tsk)
557{
558 if (!unhandled_signal(tsk, SIGSEGV))
559 return;
560
561 if (!printk_ratelimit())
562 return;
563
564 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
565 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
566 tsk->comm, task_pid_nr(tsk), address,
567 (void *)regs->ip, (void *)regs->sp, error_code);
568
569 print_vma_addr(KERN_CONT " in ", regs->ip);
570
571 printk(KERN_CONT "\n");
572}
573
574static void
575__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
576 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100577{
578 struct task_struct *tsk = current;
579
580 /* User mode accesses just cause a SIGSEGV */
581 if (error_code & PF_USER) {
582 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100583 * It's possible to have interrupts off here:
Nick Piggin92181f12009-01-20 04:24:26 +0100584 */
585 local_irq_enable();
586
587 /*
588 * Valid to do another page fault here because this one came
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100589 * from user space:
Nick Piggin92181f12009-01-20 04:24:26 +0100590 */
591 if (is_prefetch(regs, error_code, address))
592 return;
593
594 if (is_errata100(regs, address))
595 return;
596
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100597 if (unlikely(show_unhandled_signals))
598 show_signal_msg(regs, error_code, address, tsk);
Nick Piggin92181f12009-01-20 04:24:26 +0100599
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100600 /* Kernel addresses are always protection faults: */
601 tsk->thread.cr2 = address;
602 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
603 tsk->thread.trap_no = 14;
604
Nick Piggin92181f12009-01-20 04:24:26 +0100605 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100606
Nick Piggin92181f12009-01-20 04:24:26 +0100607 return;
608 }
609
610 if (is_f00f_bug(regs, address))
611 return;
612
613 no_context(regs, error_code, address);
614}
615
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100616static noinline void
617bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
618 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100619{
620 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
621}
622
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100623static void
624__bad_area(struct pt_regs *regs, unsigned long error_code,
625 unsigned long address, int si_code)
Nick Piggin92181f12009-01-20 04:24:26 +0100626{
627 struct mm_struct *mm = current->mm;
628
629 /*
630 * Something tried to access memory that isn't in our memory map..
631 * Fix it, but check if it's kernel or user first..
632 */
633 up_read(&mm->mmap_sem);
634
635 __bad_area_nosemaphore(regs, error_code, address, si_code);
636}
637
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100638static noinline void
639bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100640{
641 __bad_area(regs, error_code, address, SEGV_MAPERR);
642}
643
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100644static noinline void
645bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
646 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100647{
648 __bad_area(regs, error_code, address, SEGV_ACCERR);
649}
650
651/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100652static void
653out_of_memory(struct pt_regs *regs, unsigned long error_code,
654 unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100655{
656 /*
657 * We ran out of memory, call the OOM killer, and return the userspace
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100658 * (which will retry the fault, or kill us if we got oom-killed):
Nick Piggin92181f12009-01-20 04:24:26 +0100659 */
660 up_read(&current->mm->mmap_sem);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100661
Nick Piggin92181f12009-01-20 04:24:26 +0100662 pagefault_out_of_memory();
663}
664
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100665static void
666do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
Nick Piggin92181f12009-01-20 04:24:26 +0100667{
668 struct task_struct *tsk = current;
669 struct mm_struct *mm = tsk->mm;
670
671 up_read(&mm->mmap_sem);
672
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100673 /* Kernel mode? Handle exceptions or die: */
Nick Piggin92181f12009-01-20 04:24:26 +0100674 if (!(error_code & PF_USER))
675 no_context(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100676
Nick Piggin92181f12009-01-20 04:24:26 +0100677#ifdef CONFIG_X86_32
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100678 /* User space => ok to do another page fault: */
Nick Piggin92181f12009-01-20 04:24:26 +0100679 if (is_prefetch(regs, error_code, address))
680 return;
681#endif
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100682
683 tsk->thread.cr2 = address;
684 tsk->thread.error_code = error_code;
685 tsk->thread.trap_no = 14;
686
Nick Piggin92181f12009-01-20 04:24:26 +0100687 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
688}
689
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100690static noinline void
691mm_fault_error(struct pt_regs *regs, unsigned long error_code,
692 unsigned long address, unsigned int fault)
Nick Piggin92181f12009-01-20 04:24:26 +0100693{
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100694 if (fault & VM_FAULT_OOM) {
Nick Piggin92181f12009-01-20 04:24:26 +0100695 out_of_memory(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100696 } else {
697 if (fault & VM_FAULT_SIGBUS)
698 do_sigbus(regs, error_code, address);
699 else
700 BUG();
701 }
Nick Piggin92181f12009-01-20 04:24:26 +0100702}
703
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100704static int spurious_fault_check(unsigned long error_code, pte_t *pte)
705{
706 if ((error_code & PF_WRITE) && !pte_write(*pte))
707 return 0;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100708
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100709 if ((error_code & PF_INSTR) && !pte_exec(*pte))
710 return 0;
711
712 return 1;
713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100716 * Handle a spurious fault caused by a stale TLB entry.
717 *
718 * This allows us to lazily refresh the TLB when increasing the
719 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
720 * eagerly is very expensive since that implies doing a full
721 * cross-processor TLB flush, even if no stale TLB entries exist
722 * on other processors.
723 *
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100724 * There are no security implications to leaving a stale TLB when
725 * increasing the permissions on a page.
726 */
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100727static noinline int
728spurious_fault(unsigned long error_code, unsigned long address)
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100729{
730 pgd_t *pgd;
731 pud_t *pud;
732 pmd_t *pmd;
733 pte_t *pte;
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500734 int ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100735
736 /* Reserved-bit violation or user access to kernel space? */
737 if (error_code & (PF_USER | PF_RSVD))
738 return 0;
739
740 pgd = init_mm.pgd + pgd_index(address);
741 if (!pgd_present(*pgd))
742 return 0;
743
744 pud = pud_offset(pgd, address);
745 if (!pud_present(*pud))
746 return 0;
747
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100748 if (pud_large(*pud))
749 return spurious_fault_check(error_code, (pte_t *) pud);
750
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100751 pmd = pmd_offset(pud, address);
752 if (!pmd_present(*pmd))
753 return 0;
754
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100755 if (pmd_large(*pmd))
756 return spurious_fault_check(error_code, (pte_t *) pmd);
757
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100758 pte = pte_offset_kernel(pmd, address);
759 if (!pte_present(*pte))
760 return 0;
761
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500762 ret = spurious_fault_check(error_code, pte);
763 if (!ret)
764 return 0;
765
766 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100767 * Make sure we have permissions in PMD.
768 * If not, then there's a bug in the page tables:
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500769 */
770 ret = spurious_fault_check(error_code, (pte_t *) pmd);
771 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100772
Steven Rostedt3c3e5692009-02-19 11:46:36 -0500773 return ret;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100774}
775
776/*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100777 * 32-bit:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100778 *
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100779 * Handle a fault on the vmalloc or module mapping area
780 *
781 * 64-bit:
782 *
783 * Handle a fault on the vmalloc area
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700784 *
785 * This assumes no large pages in there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 */
Nick Piggin92181f12009-01-20 04:24:26 +0100787static noinline int vmalloc_fault(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100789#ifdef CONFIG_X86_32
790 unsigned long pgd_paddr;
791 pmd_t *pmd_k;
792 pte_t *pte_k;
Henry Nestlerb29c7012008-05-12 15:44:39 +0200793
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100794 /* Make sure we are in vmalloc area: */
Henry Nestlerb29c7012008-05-12 15:44:39 +0200795 if (!(address >= VMALLOC_START && address < VMALLOC_END))
796 return -1;
797
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100798 /*
799 * Synchronize this task's top level page-table
800 * with the 'reference' page table.
801 *
802 * Do _not_ use "current" here. We might be inside
803 * an interrupt in the middle of a task switch..
804 */
805 pgd_paddr = read_cr3();
806 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
807 if (!pmd_k)
808 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100809
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100810 pte_k = pte_offset_kernel(pmd_k, address);
811 if (!pte_present(*pte_k))
812 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100813
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100814 return 0;
815#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 pgd_t *pgd, *pgd_ref;
817 pud_t *pud, *pud_ref;
818 pmd_t *pmd, *pmd_ref;
819 pte_t *pte, *pte_ref;
820
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100821 /* Make sure we are in vmalloc area: */
Harvey Harrisoncf89ec92008-02-04 16:47:56 +0100822 if (!(address >= VMALLOC_START && address < VMALLOC_END))
823 return -1;
824
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100825 /*
826 * Copy kernel mappings over when needed. This can also
827 * happen within a race in page table update. In the later
828 * case just flush:
829 */
Andi Kleenf313e122009-01-09 12:17:43 -0800830 pgd = pgd_offset(current->active_mm, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 pgd_ref = pgd_offset_k(address);
832 if (pgd_none(*pgd_ref))
833 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 if (pgd_none(*pgd))
836 set_pgd(pgd, *pgd_ref);
Jan Beulich8c914cb2006-03-25 16:29:40 +0100837 else
Dave McCracken46a82b22006-09-25 23:31:48 -0700838 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100840 /*
841 * Below here mismatches are bugs because these lower tables
842 * are shared:
843 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 pud = pud_offset(pgd, address);
846 pud_ref = pud_offset(pgd_ref, address);
847 if (pud_none(*pud_ref))
848 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100849
Dave McCracken46a82b22006-09-25 23:31:48 -0700850 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 pmd = pmd_offset(pud, address);
854 pmd_ref = pmd_offset(pud_ref, address);
855 if (pmd_none(*pmd_ref))
856 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
859 BUG();
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 pte_ref = pte_offset_kernel(pmd_ref, address);
862 if (!pte_present(*pte_ref))
863 return -1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 pte = pte_offset_kernel(pmd, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100866
867 /*
868 * Don't use pte_page here, because the mappings can point
869 * outside mem_map, and the NUMA hash lookup cannot handle
870 * that:
871 */
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700872 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_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 return 0;
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100876#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200879int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100881static inline int
882access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
Nick Piggin92181f12009-01-20 04:24:26 +0100883{
884 if (write) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100885 /* write, present and write, not present: */
Nick Piggin92181f12009-01-20 04:24:26 +0100886 if (unlikely(!(vma->vm_flags & VM_WRITE)))
887 return 1;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100888 return 0;
Nick Piggin92181f12009-01-20 04:24:26 +0100889 }
890
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100891 /* read, present: */
892 if (unlikely(error_code & PF_PROT))
893 return 1;
894
895 /* read, not present: */
896 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
897 return 1;
898
Nick Piggin92181f12009-01-20 04:24:26 +0100899 return 0;
900}
901
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800902static int fault_in_kernel_space(unsigned long address)
903{
904#ifdef CONFIG_X86_32
905 return address >= TASK_SIZE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100906#else
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800907 return address >= TASK_SIZE64;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100908#endif
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800909}
910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911/*
912 * This routine handles page faults. It determines the address,
913 * and the problem, and then passes it off to one of the appropriate
914 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100916#ifdef CONFIG_X86_64
917asmlinkage
918#endif
919void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100921 struct vm_area_struct *vma;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100922 struct task_struct *tsk;
923 unsigned long address;
924 struct mm_struct *mm;
Nick Piggin92181f12009-01-20 04:24:26 +0100925 int write;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100926 int fault;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100928 tsk = current;
929 mm = tsk->mm;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100930
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100931 prefetchw(&mm->mmap_sem);
932
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100933 /* Get the faulting address: */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200934 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Pekka Paalanen0fd0e3d2008-05-12 21:20:57 +0200936 if (unlikely(kmmio_fault(regs, address)))
Pekka Paalanen86069782008-05-12 21:20:56 +0200937 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /*
940 * We fault-in kernel-space virtual memory on-demand. The
941 * 'reference' page table is init_mm.pgd.
942 *
943 * NOTE! We MUST NOT take any locks for this case. We may
944 * be in an interrupt or a critical region, and should
945 * only copy the information from the master page table,
946 * nothing more.
947 *
948 * This verifies that the fault happens in kernel space
949 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +0100950 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Hiroshi Shimamoto0973a062009-02-04 15:24:09 -0800952 if (unlikely(fault_in_kernel_space(address))) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100953 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
954 vmalloc_fault(address) >= 0)
955 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100956
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100957 /* Can handle a stale RO->RW TLB: */
Nick Piggin92181f12009-01-20 04:24:26 +0100958 if (spurious_fault(error_code, address))
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100959 return;
960
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100961 /* kprobes don't want to hook the spurious faults: */
Masami Hiramatsu9be260a2009-02-05 17:12:39 -0500962 if (notify_page_fault(regs))
963 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100964 /*
965 * Don't take the mm semaphore here. If we fixup a prefetch
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100966 * fault we could otherwise deadlock:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100967 */
Nick Piggin92181f12009-01-20 04:24:26 +0100968 bad_area_nosemaphore(regs, error_code, address);
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100969
Nick Piggin92181f12009-01-20 04:24:26 +0100970 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100971 }
972
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100973 /* kprobes don't want to hook the spurious faults: */
Ingo Molnarf8a6b2b2009-02-13 09:44:22 +0100974 if (unlikely(notify_page_fault(regs)))
Masami Hiramatsu9be260a2009-02-05 17:12:39 -0500975 return;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100976 /*
Linus Torvalds891cffb2008-10-12 13:16:12 -0700977 * It's safe to allow irq's after cr2 has been saved and the
978 * vmalloc fault has been handled.
979 *
980 * User-mode registers count as a user access even for any
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100981 * potential system fault or CPU buglet:
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100982 */
Linus Torvalds891cffb2008-10-12 13:16:12 -0700983 if (user_mode_vm(regs)) {
984 local_irq_enable();
985 error_code |= PF_USER;
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100986 } else {
987 if (regs->flags & X86_EFLAGS_IF)
988 local_irq_enable();
989 }
Jan Beulich8c914cb2006-03-25 16:29:40 +0100990
Linus Torvalds891cffb2008-10-12 13:16:12 -0700991#ifdef CONFIG_X86_64
Andi Kleen66c58152006-01-11 22:44:09 +0100992 if (unlikely(error_code & PF_RSVD))
Nick Piggin92181f12009-01-20 04:24:26 +0100993 pgtable_bad(regs, error_code, address);
Linus Torvalds891cffb2008-10-12 13:16:12 -0700994#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +0100997 * If we're in an interrupt, have no user context or are running
998 * in an atomic region then we must not take the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 */
Nick Piggin92181f12009-01-20 04:24:26 +01001000 if (unlikely(in_atomic() || !mm)) {
1001 bad_area_nosemaphore(regs, error_code, address);
1002 return;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Ingo Molnar3a1dfe62008-10-13 17:49:02 +02001005 /*
1006 * When running in the kernel we expect faults to occur only to
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001007 * addresses in user space. All other faults represent errors in
1008 * the kernel and should generate an OOPS. Unfortunately, in the
1009 * case of an erroneous fault occurring in a code path which already
1010 * holds mmap_sem we will deadlock attempting to validate the fault
1011 * against the address space. Luckily the kernel only validly
1012 * references user space from well defined areas of code, which are
1013 * listed in the exceptions table.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 *
1015 * As the vast majority of faults will be valid we will only perform
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001016 * the source reference check when there is a possibility of a
1017 * deadlock. Attempt to lock the address space, if we cannot we then
1018 * validate the source. If this is invalid we can skip the address
1019 * space check, thus avoiding the deadlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 */
Nick Piggin92181f12009-01-20 04:24:26 +01001021 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
Andi Kleen66c58152006-01-11 22:44:09 +01001022 if ((error_code & PF_USER) == 0 &&
Nick Piggin92181f12009-01-20 04:24:26 +01001023 !search_exception_tables(regs->ip)) {
1024 bad_area_nosemaphore(regs, error_code, address);
1025 return;
1026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 down_read(&mm->mmap_sem);
Peter Zijlstra01006072009-01-29 16:02:12 +01001028 } else {
1029 /*
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001030 * The above down_read_trylock() might have succeeded in
1031 * which case we'll have missed the might_sleep() from
1032 * down_read():
Peter Zijlstra01006072009-01-29 16:02:12 +01001033 */
1034 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036
1037 vma = find_vma(mm, address);
Nick Piggin92181f12009-01-20 04:24:26 +01001038 if (unlikely(!vma)) {
1039 bad_area(regs, error_code, address);
1040 return;
1041 }
1042 if (likely(vma->vm_start <= address))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto good_area;
Nick Piggin92181f12009-01-20 04:24:26 +01001044 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1045 bad_area(regs, error_code, address);
1046 return;
1047 }
Harvey Harrison33cb5242008-01-30 13:32:19 +01001048 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001049 /*
1050 * Accessing the stack below %sp is always a bug.
1051 * The large cushion allows instructions like enter
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001052 * and pusha to work. ("enter $65535, $31" pushes
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001053 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +02001054 */
Nick Piggin92181f12009-01-20 04:24:26 +01001055 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1056 bad_area(regs, error_code, address);
1057 return;
1058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
Nick Piggin92181f12009-01-20 04:24:26 +01001060 if (unlikely(expand_stack(vma, address))) {
1061 bad_area(regs, error_code, address);
1062 return;
1063 }
1064
1065 /*
1066 * Ok, we have a good vm_area for this memory access, so
1067 * we can handle it..
1068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069good_area:
Nick Piggin92181f12009-01-20 04:24:26 +01001070 write = error_code & PF_WRITE;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001071
Nick Piggin92181f12009-01-20 04:24:26 +01001072 if (unlikely(access_error(error_code, write, vma))) {
1073 bad_area_access_error(regs, error_code, address);
1074 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 /*
1078 * If for any reason at all we couldn't handle the fault,
1079 * make sure we exit gracefully rather than endlessly redo
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001080 * the fault:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
Nick Piggin83c54072007-07-19 01:47:05 -07001082 fault = handle_mm_fault(mm, vma, address, write);
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001083
Nick Piggin83c54072007-07-19 01:47:05 -07001084 if (unlikely(fault & VM_FAULT_ERROR)) {
Nick Piggin92181f12009-01-20 04:24:26 +01001085 mm_fault_error(regs, error_code, address, fault);
1086 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001088
Nick Piggin83c54072007-07-19 01:47:05 -07001089 if (fault & VM_FAULT_MAJOR)
1090 tsk->maj_flt++;
1091 else
1092 tsk->min_flt++;
Harvey Harrisond729ab32008-01-30 13:33:23 +01001093
1094#ifdef CONFIG_X86_32
1095 /*
1096 * Did it hit the DOS screen memory VA from vm86 mode?
1097 */
1098 if (v8086_mode(regs)) {
1099 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
1100 if (bit < 32)
1101 tsk->thread.screen_bitmap |= 1 << bit;
1102 }
1103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 up_read(&mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105}
Andi Kleen9e43e1b2005-11-05 17:25:54 +01001106
Jan Beulich8c914cb2006-03-25 16:29:40 +01001107DEFINE_SPINLOCK(pgd_lock);
Christoph Lameter2bff7382007-05-02 19:27:10 +02001108LIST_HEAD(pgd_list);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001109
1110void vmalloc_sync_all(void)
1111{
Harvey Harrison1156e092008-01-30 13:34:10 +01001112 unsigned long address;
1113
Jan Beulichcc643d42008-08-29 12:53:45 +01001114#ifdef CONFIG_X86_32
Harvey Harrison1156e092008-01-30 13:34:10 +01001115 if (SHARED_KERNEL_PMD)
1116 return;
1117
Jan Beulichcc643d42008-08-29 12:53:45 +01001118 for (address = VMALLOC_START & PMD_MASK;
1119 address >= TASK_SIZE && address < FIXADDR_TOP;
1120 address += PMD_SIZE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001121
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001122 unsigned long flags;
1123 struct page *page;
Harvey Harrison1156e092008-01-30 13:34:10 +01001124
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001125 spin_lock_irqsave(&pgd_lock, flags);
1126 list_for_each_entry(page, &pgd_list, lru) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001127 if (!vmalloc_sync_one(page_address(page), address))
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001128 break;
Harvey Harrison1156e092008-01-30 13:34:10 +01001129 }
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001130 spin_unlock_irqrestore(&pgd_lock, flags);
Harvey Harrison1156e092008-01-30 13:34:10 +01001131 }
1132#else /* CONFIG_X86_64 */
Jan Beulichcc643d42008-08-29 12:53:45 +01001133 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
1134 address += PGDIR_SIZE) {
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001135
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001136 const pgd_t *pgd_ref = pgd_offset_k(address);
1137 unsigned long flags;
1138 struct page *page;
Jan Beulich8c914cb2006-03-25 16:29:40 +01001139
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001140 if (pgd_none(*pgd_ref))
1141 continue;
Ingo Molnar2d4a7162009-02-20 19:56:40 +01001142
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001143 spin_lock_irqsave(&pgd_lock, flags);
1144 list_for_each_entry(page, &pgd_list, lru) {
1145 pgd_t *pgd;
1146 pgd = (pgd_t *)page_address(page) + pgd_index(address);
1147 if (pgd_none(*pgd))
1148 set_pgd(pgd, *pgd_ref);
1149 else
1150 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Jan Beulich8c914cb2006-03-25 16:29:40 +01001151 }
Jeremy Fitzhardinge67350a52008-06-25 00:19:11 -04001152 spin_unlock_irqrestore(&pgd_lock, flags);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001153 }
Harvey Harrison1156e092008-01-30 13:34:10 +01001154#endif
Jan Beulich8c914cb2006-03-25 16:29:40 +01001155}