blob: e9a086a1a9ff9e20ac2bac10ab782ed436769974 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1995 Linus Torvalds
3 * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs.
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/signal.h>
7#include <linux/sched.h>
8#include <linux/kernel.h>
9#include <linux/errno.h>
10#include <linux/string.h>
11#include <linux/types.h>
12#include <linux/ptrace.h>
13#include <linux/mman.h>
14#include <linux/mm.h>
15#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/interrupt.h>
17#include <linux/init.h>
18#include <linux/tty.h>
19#include <linux/vt_kern.h> /* For unblank_screen() */
20#include <linux/compiler.h>
Harvey Harrisonc61e2112008-01-30 13:34:11 +010021#include <linux/highmem.h>
22#include <linux/bootmem.h> /* for max_low_pfn */
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070023#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
Prasanna S Panchamukhi0f2fbdc2005-09-06 15:19:28 -070025#include <linux/kprobes.h>
Andi Kleenab2bf0c2006-12-07 02:14:06 +010026#include <linux/uaccess.h>
Christoph Hellwig1eeb66a2007-05-08 00:27:03 -070027#include <linux/kdebug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/system.h>
Harvey Harrisonc61e2112008-01-30 13:34:11 +010030#include <asm/desc.h>
31#include <asm/segment.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/pgalloc.h>
33#include <asm/smp.h>
34#include <asm/tlbflush.h>
35#include <asm/proto.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <asm-generic/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Harvey Harrison33cb5242008-01-30 13:32:19 +010038/*
39 * Page fault error code bits
40 * bit 0 == 0 means no page found, 1 means protection fault
41 * bit 1 == 0 means read, 1 means write
42 * bit 2 == 0 means kernel, 1 means user-mode
43 * bit 3 == 1 means use of reserved bit detected
44 * bit 4 == 1 means fault was an instruction fetch
45 */
Ingo Molnar8a19da72008-01-30 13:32:53 +010046#define PF_PROT (1<<0)
Andi Kleen66c58152006-01-11 22:44:09 +010047#define PF_WRITE (1<<1)
Ingo Molnar8a19da72008-01-30 13:32:53 +010048#define PF_USER (1<<2)
49#define PF_RSVD (1<<3)
Andi Kleen66c58152006-01-11 22:44:09 +010050#define PF_INSTR (1<<4)
51
Pekka Paalanen10c43d22008-05-12 21:20:57 +020052#ifdef CONFIG_MMIOTRACE_HOOKS
53static pf_handler_func mmiotrace_pf_handler; /* protected by RCU */
54static DEFINE_SPINLOCK(mmiotrace_handler_lock);
Pekka Paalanen86069782008-05-12 21:20:56 +020055
Pekka Paalanen10c43d22008-05-12 21:20:57 +020056int mmiotrace_register_pf(pf_handler_func new_pfh)
Pekka Paalanen86069782008-05-12 21:20:56 +020057{
Pekka Paalanen10c43d22008-05-12 21:20:57 +020058 int ret = 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020059 unsigned long flags;
Pekka Paalanen10c43d22008-05-12 21:20:57 +020060 spin_lock_irqsave(&mmiotrace_handler_lock, flags);
61 if (mmiotrace_pf_handler)
62 ret = -EBUSY;
63 else
64 mmiotrace_pf_handler = new_pfh;
65 spin_unlock_irqrestore(&mmiotrace_handler_lock, flags);
66 return ret;
Pekka Paalanen86069782008-05-12 21:20:56 +020067}
Pekka Paalanen10c43d22008-05-12 21:20:57 +020068EXPORT_SYMBOL_GPL(mmiotrace_register_pf);
Pekka Paalanen86069782008-05-12 21:20:56 +020069
70/**
Pekka Paalanen10c43d22008-05-12 21:20:57 +020071 * mmiotrace_unregister_pf:
Pekka Paalanen86069782008-05-12 21:20:56 +020072 * The caller must ensure @old_pfh is not in use anymore before freeing it.
Pekka Paalanen10c43d22008-05-12 21:20:57 +020073 * This function does not guarantee it. The handler function pointer is
74 * protected by RCU, so you can do this by e.g. calling synchronize_rcu().
Pekka Paalanen86069782008-05-12 21:20:56 +020075 */
Pekka Paalanen10c43d22008-05-12 21:20:57 +020076int mmiotrace_unregister_pf(pf_handler_func old_pfh)
Pekka Paalanen86069782008-05-12 21:20:56 +020077{
Pekka Paalanen10c43d22008-05-12 21:20:57 +020078 int ret = 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020079 unsigned long flags;
Pekka Paalanen10c43d22008-05-12 21:20:57 +020080 spin_lock_irqsave(&mmiotrace_handler_lock, flags);
81 if (mmiotrace_pf_handler != old_pfh)
82 ret = -EPERM;
83 else
84 mmiotrace_pf_handler = NULL;
85 spin_unlock_irqrestore(&mmiotrace_handler_lock, flags);
86 return ret;
Pekka Paalanen86069782008-05-12 21:20:56 +020087}
Pekka Paalanen10c43d22008-05-12 21:20:57 +020088EXPORT_SYMBOL_GPL(mmiotrace_unregister_pf);
89#endif /* CONFIG_MMIOTRACE_HOOKS */
Pekka Paalanen86069782008-05-12 21:20:56 +020090
91/* returns non-zero if do_page_fault() should return */
Pekka Paalanen10c43d22008-05-12 21:20:57 +020092static inline int call_mmiotrace(struct pt_regs *regs,
93 unsigned long error_code,
94 unsigned long address)
Pekka Paalanen86069782008-05-12 21:20:56 +020095{
Pekka Paalanen10c43d22008-05-12 21:20:57 +020096#ifdef CONFIG_MMIOTRACE_HOOKS
Pekka Paalanen86069782008-05-12 21:20:56 +020097 int ret = 0;
Pekka Paalanen86069782008-05-12 21:20:56 +020098 rcu_read_lock();
Pekka Paalanen10c43d22008-05-12 21:20:57 +020099 if (mmiotrace_pf_handler)
100 ret = mmiotrace_pf_handler(regs, error_code, address);
Pekka Paalanen86069782008-05-12 21:20:56 +0200101 rcu_read_unlock();
102 return ret;
103#else
104 return 0;
105#endif
106}
107
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700108static inline int notify_page_fault(struct pt_regs *regs)
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -0700109{
Harvey Harrison33cb5242008-01-30 13:32:19 +0100110#ifdef CONFIG_KPROBES
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700111 int ret = 0;
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -0700112
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700113 /* kprobe_running() needs smp_processor_id() */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100114#ifdef CONFIG_X86_32
115 if (!user_mode_vm(regs)) {
116#else
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700117 if (!user_mode(regs)) {
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100118#endif
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700119 preempt_disable();
120 if (kprobe_running() && kprobe_fault_handler(regs, 14))
121 ret = 1;
122 preempt_enable();
123 }
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -0700124
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700125 return ret;
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700126#else
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700127 return 0;
Christoph Hellwig74a0b572007-10-16 01:24:07 -0700128#endif
Harvey Harrison33cb5242008-01-30 13:32:19 +0100129}
Anil S Keshavamurthy1bd858a2006-06-26 00:25:25 -0700130
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100131/*
132 * X86_32
133 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
134 * Check that here and ignore it.
135 *
136 * X86_64
137 * Sometimes the CPU reports invalid exceptions on prefetch.
138 * Check that here and ignore it.
139 *
140 * Opcode checker based on code by Richard Brunner
141 */
142static int is_prefetch(struct pt_regs *regs, unsigned long addr,
143 unsigned long error_code)
Harvey Harrison33cb5242008-01-30 13:32:19 +0100144{
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100145 unsigned char *instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int scan_more = 1;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100147 int prefetch = 0;
Andi Kleenf1290ec2005-04-16 15:24:59 -0700148 unsigned char *max_instr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ingo Molnar30853542008-03-27 21:29:09 +0100150 /*
151 * If it was a exec (instruction fetch) fault on NX page, then
152 * do not ignore the fault:
153 */
Andi Kleen66c58152006-01-11 22:44:09 +0100154 if (error_code & PF_INSTR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return 0;
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100156
Harvey Harrisonf2857ce2008-01-30 13:33:12 +0100157 instr = (unsigned char *)convert_ip_to_linear(current, regs);
Andi Kleenf1290ec2005-04-16 15:24:59 -0700158 max_instr = instr + 15;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700160 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return 0;
162
Harvey Harrison33cb5242008-01-30 13:32:19 +0100163 while (scan_more && instr < max_instr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 unsigned char opcode;
165 unsigned char instr_hi;
166 unsigned char instr_lo;
167
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100168 if (probe_kernel_address(instr, opcode))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100169 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Harvey Harrison33cb5242008-01-30 13:32:19 +0100171 instr_hi = opcode & 0xf0;
172 instr_lo = opcode & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 instr++;
174
Harvey Harrison33cb5242008-01-30 13:32:19 +0100175 switch (instr_hi) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 case 0x20:
177 case 0x30:
Harvey Harrison33cb5242008-01-30 13:32:19 +0100178 /*
179 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
180 * In X86_64 long mode, the CPU will signal invalid
181 * opcode if some of these prefixes are present so
182 * X86_64 will never get here anyway
183 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 scan_more = ((instr_lo & 7) == 0x6);
185 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100186#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 case 0x40:
Harvey Harrison33cb5242008-01-30 13:32:19 +0100188 /*
189 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
190 * Need to figure out under what instruction mode the
191 * instruction was issued. Could check the LDT for lm,
192 * but for now it's good enough to assume that long
193 * mode only uses well known segments or kernel.
194 */
Vincent Hanquez76381fe2005-06-23 00:08:46 -0700195 scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 case 0x60:
199 /* 0x64 thru 0x67 are valid prefixes in all modes. */
200 scan_more = (instr_lo & 0xC) == 0x4;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 case 0xF0:
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100203 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 scan_more = !instr_lo || (instr_lo>>1) == 1;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 case 0x00:
207 /* Prefetch instruction is 0x0F0D or 0x0F18 */
208 scan_more = 0;
Harvey Harrisonf2857ce2008-01-30 13:33:12 +0100209
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100210 if (probe_kernel_address(instr, opcode))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
212 prefetch = (instr_lo == 0xF) &&
213 (opcode == 0x0D || opcode == 0x18);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100214 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 default:
216 scan_more = 0;
217 break;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220 return prefetch;
221}
222
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100223static void force_sig_info_fault(int si_signo, int si_code,
224 unsigned long address, struct task_struct *tsk)
225{
226 siginfo_t info;
227
228 info.si_signo = si_signo;
229 info.si_errno = 0;
230 info.si_code = si_code;
231 info.si_addr = (void __user *)address;
232 force_sig_info(si_signo, &info, tsk);
233}
234
Harvey Harrison1156e092008-01-30 13:34:10 +0100235#ifdef CONFIG_X86_64
Harvey Harrison33cb5242008-01-30 13:32:19 +0100236static int bad_address(void *p)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 unsigned long dummy;
Andi Kleenab2bf0c2006-12-07 02:14:06 +0100239 return probe_kernel_address((unsigned long *)p, dummy);
Harvey Harrison33cb5242008-01-30 13:32:19 +0100240}
Harvey Harrison1156e092008-01-30 13:34:10 +0100241#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Adrian Bunkcae30f82008-02-13 23:31:31 +0200243static void dump_pagetable(unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Harvey Harrison1156e092008-01-30 13:34:10 +0100245#ifdef CONFIG_X86_32
246 __typeof__(pte_val(__pte(0))) page;
247
248 page = read_cr3();
249 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
250#ifdef CONFIG_X86_PAE
251 printk("*pdpt = %016Lx ", page);
252 if ((page >> PAGE_SHIFT) < max_low_pfn
253 && page & _PAGE_PRESENT) {
254 page &= PAGE_MASK;
255 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
256 & (PTRS_PER_PMD - 1)];
257 printk(KERN_CONT "*pde = %016Lx ", page);
258 page &= ~_PAGE_NX;
259 }
260#else
261 printk("*pde = %08lx ", page);
262#endif
263
264 /*
265 * We must not directly access the pte in the highpte
266 * case if the page table is located in highmem.
267 * And let's rather not kmap-atomic the pte, just in case
268 * it's allocated already.
269 */
270 if ((page >> PAGE_SHIFT) < max_low_pfn
271 && (page & _PAGE_PRESENT)
272 && !(page & _PAGE_PSE)) {
273 page &= PAGE_MASK;
274 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
275 & (PTRS_PER_PTE - 1)];
276 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
277 }
278
279 printk("\n");
280#else /* CONFIG_X86_64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 pgd_t *pgd;
282 pud_t *pud;
283 pmd_t *pmd;
284 pte_t *pte;
285
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200286 pgd = (pgd_t *)read_cr3();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Harvey Harrison33cb5242008-01-30 13:32:19 +0100288 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 pgd += pgd_index(address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (bad_address(pgd)) goto bad;
Jan Beulichd646bce2006-02-03 21:51:47 +0100291 printk("PGD %lx ", pgd_val(*pgd));
Harvey Harrison33cb5242008-01-30 13:32:19 +0100292 if (!pgd_present(*pgd)) goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Andi Kleend2ae5b52006-06-26 13:57:56 +0200294 pud = pud_offset(pgd, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 if (bad_address(pud)) goto bad;
296 printk("PUD %lx ", pud_val(*pud));
Andi Kleenb5360222008-02-04 16:48:09 +0100297 if (!pud_present(*pud) || pud_large(*pud))
298 goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 pmd = pmd_offset(pud, address);
301 if (bad_address(pmd)) goto bad;
302 printk("PMD %lx ", pmd_val(*pmd));
Jan Beulichb1992df2007-10-19 20:35:03 +0200303 if (!pmd_present(*pmd) || pmd_large(*pmd)) goto ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 pte = pte_offset_kernel(pmd, address);
306 if (bad_address(pte)) goto bad;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100307 printk("PTE %lx", pte_val(*pte));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308ret:
309 printk("\n");
310 return;
311bad:
312 printk("BAD\n");
Harvey Harrison1156e092008-01-30 13:34:10 +0100313#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
Harvey Harrison1156e092008-01-30 13:34:10 +0100316#ifdef CONFIG_X86_32
317static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
318{
319 unsigned index = pgd_index(address);
320 pgd_t *pgd_k;
321 pud_t *pud, *pud_k;
322 pmd_t *pmd, *pmd_k;
323
324 pgd += index;
325 pgd_k = init_mm.pgd + index;
326
327 if (!pgd_present(*pgd_k))
328 return NULL;
329
330 /*
331 * set_pgd(pgd, *pgd_k); here would be useless on PAE
332 * and redundant with the set_pmd() on non-PAE. As would
333 * set_pud.
334 */
335
336 pud = pud_offset(pgd, address);
337 pud_k = pud_offset(pgd_k, address);
338 if (!pud_present(*pud_k))
339 return NULL;
340
341 pmd = pmd_offset(pud, address);
342 pmd_k = pmd_offset(pud_k, address);
343 if (!pmd_present(*pmd_k))
344 return NULL;
345 if (!pmd_present(*pmd)) {
346 set_pmd(pmd, *pmd_k);
347 arch_flush_lazy_mmu_mode();
348 } else
349 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
350 return pmd_k;
351}
352#endif
353
Harvey Harrison1dc85be2008-01-30 13:32:35 +0100354#ifdef CONFIG_X86_64
Harvey Harrison33cb5242008-01-30 13:32:19 +0100355static const char errata93_warning[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
357KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
358KERN_ERR "******* Please consider a BIOS update.\n"
359KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100360#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/* Workaround for K8 erratum #93 & buggy BIOS.
363 BIOS SMM functions are required to use a specific workaround
Harvey Harrison33cb5242008-01-30 13:32:19 +0100364 to avoid corruption of the 64bit RIP register on C stepping K8.
365 A lot of BIOS that didn't get tested properly miss this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 The OS sees this as a page fault with the upper 32bits of RIP cleared.
367 Try to work around it here.
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100368 Note we only handle faults in kernel here.
369 Does nothing for X86_32
370 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100371static int is_errata93(struct pt_regs *regs, unsigned long address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100373#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 static int warned;
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100375 if (address != regs->ip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100377 if ((address >> 32) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return 0;
379 address |= 0xffffffffUL << 32;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100380 if ((address >= (u64)_stext && address <= (u64)_etext) ||
381 (address >= MODULES_VADDR && address <= MODULES_END)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (!warned) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100383 printk(errata93_warning);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 warned = 1;
385 }
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100386 regs->ip = address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 1;
388 }
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100389#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100391}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Harvey Harrison35f32662008-01-30 13:34:09 +0100393/*
394 * Work around K8 erratum #100 K8 in compat mode occasionally jumps to illegal
395 * addresses >4GB. We catch this in the page fault handler because these
396 * addresses are not reachable. Just detect this case and return. Any code
397 * segment in LDT is compatibility mode.
398 */
399static int is_errata100(struct pt_regs *regs, unsigned long address)
400{
401#ifdef CONFIG_X86_64
402 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
403 (address >> 32))
404 return 1;
405#endif
406 return 0;
407}
408
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100409void do_invalid_op(struct pt_regs *, unsigned long);
410
411static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
412{
413#ifdef CONFIG_X86_F00F_BUG
414 unsigned long nr;
415 /*
416 * Pentium F0 0F C7 C8 bug workaround.
417 */
418 if (boot_cpu_data.f00f_bug) {
419 nr = (address - idt_descr.address) >> 3;
420
421 if (nr == 6) {
422 do_invalid_op(regs, 0);
423 return 1;
424 }
425 }
426#endif
427 return 0;
428}
429
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100430static void show_fault_oops(struct pt_regs *regs, unsigned long error_code,
431 unsigned long address)
432{
Harvey Harrison1156e092008-01-30 13:34:10 +0100433#ifdef CONFIG_X86_32
434 if (!oops_may_print())
435 return;
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100436#endif
Harvey Harrison1156e092008-01-30 13:34:10 +0100437
438#ifdef CONFIG_X86_PAE
439 if (error_code & PF_INSTR) {
Harvey Harrison93809be2008-02-01 17:49:43 +0100440 unsigned int level;
Harvey Harrison1156e092008-01-30 13:34:10 +0100441 pte_t *pte = lookup_address(address, &level);
442
443 if (pte && pte_present(*pte) && !pte_exec(*pte))
444 printk(KERN_CRIT "kernel tried to execute "
445 "NX-protected page - exploit attempt? "
446 "(uid: %d)\n", current->uid);
447 }
448#endif
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100449
Harvey Harrison1156e092008-01-30 13:34:10 +0100450 printk(KERN_ALERT "BUG: unable to handle kernel ");
451 if (address < PAGE_SIZE)
452 printk(KERN_CONT "NULL pointer dereference");
453 else
454 printk(KERN_CONT "paging request");
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100455#ifdef CONFIG_X86_32
Harvey Harrison1156e092008-01-30 13:34:10 +0100456 printk(KERN_CONT " at %08lx\n", address);
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100457#else
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100458 printk(KERN_CONT " at %016lx\n", address);
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100459#endif
Harvey Harrison19f0dda2008-01-30 13:34:10 +0100460 printk(KERN_ALERT "IP:");
Harvey Harrisonb3279c72008-01-30 13:34:10 +0100461 printk_address(regs->ip, 1);
462 dump_pagetable(address);
463}
464
Harvey Harrison1156e092008-01-30 13:34:10 +0100465#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
467 unsigned long error_code)
468{
Jan Beulich12091402005-09-12 18:49:24 +0200469 unsigned long flags = oops_begin();
Jan Beulich6e3f3612006-01-11 22:42:14 +0100470 struct task_struct *tsk;
Jan Beulich12091402005-09-12 18:49:24 +0200471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
473 current->comm, address);
474 dump_pagetable(address);
Jan Beulich6e3f3612006-01-11 22:42:14 +0100475 tsk = current;
476 tsk->thread.cr2 = address;
477 tsk->thread.trap_no = 14;
478 tsk->thread.error_code = error_code;
Jan Beulich22f59912008-01-30 13:31:23 +0100479 if (__die("Bad pagetable", regs, error_code))
480 regs = NULL;
481 oops_end(flags, regs, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
Harvey Harrison1156e092008-01-30 13:34:10 +0100483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100485static int spurious_fault_check(unsigned long error_code, pte_t *pte)
486{
487 if ((error_code & PF_WRITE) && !pte_write(*pte))
488 return 0;
489 if ((error_code & PF_INSTR) && !pte_exec(*pte))
490 return 0;
491
492 return 1;
493}
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495/*
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100496 * Handle a spurious fault caused by a stale TLB entry. This allows
497 * us to lazily refresh the TLB when increasing the permissions of a
498 * kernel page (RO -> RW or NX -> X). Doing it eagerly is very
499 * expensive since that implies doing a full cross-processor TLB
500 * flush, even if no stale TLB entries exist on other processors.
501 * There are no security implications to leaving a stale TLB when
502 * increasing the permissions on a page.
503 */
504static int spurious_fault(unsigned long address,
505 unsigned long error_code)
506{
507 pgd_t *pgd;
508 pud_t *pud;
509 pmd_t *pmd;
510 pte_t *pte;
511
512 /* Reserved-bit violation or user access to kernel space? */
513 if (error_code & (PF_USER | PF_RSVD))
514 return 0;
515
516 pgd = init_mm.pgd + pgd_index(address);
517 if (!pgd_present(*pgd))
518 return 0;
519
520 pud = pud_offset(pgd, address);
521 if (!pud_present(*pud))
522 return 0;
523
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100524 if (pud_large(*pud))
525 return spurious_fault_check(error_code, (pte_t *) pud);
526
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100527 pmd = pmd_offset(pud, address);
528 if (!pmd_present(*pmd))
529 return 0;
530
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100531 if (pmd_large(*pmd))
532 return spurious_fault_check(error_code, (pte_t *) pmd);
533
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100534 pte = pte_offset_kernel(pmd, address);
535 if (!pte_present(*pte))
536 return 0;
537
Thomas Gleixnerd8b57bb2008-02-06 22:39:43 +0100538 return spurious_fault_check(error_code, pte);
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100539}
540
541/*
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100542 * X86_32
543 * Handle a fault on the vmalloc or module mapping area
544 *
545 * X86_64
Andi Kleenf95190b2006-01-11 22:44:00 +0100546 * Handle a fault on the vmalloc area
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700547 *
548 * This assumes no large pages in there.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 */
550static int vmalloc_fault(unsigned long address)
551{
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100552#ifdef CONFIG_X86_32
553 unsigned long pgd_paddr;
554 pmd_t *pmd_k;
555 pte_t *pte_k;
556 /*
557 * Synchronize this task's top level page-table
558 * with the 'reference' page table.
559 *
560 * Do _not_ use "current" here. We might be inside
561 * an interrupt in the middle of a task switch..
562 */
563 pgd_paddr = read_cr3();
564 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
565 if (!pmd_k)
566 return -1;
567 pte_k = pte_offset_kernel(pmd_k, address);
568 if (!pte_present(*pte_k))
569 return -1;
570 return 0;
571#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 pgd_t *pgd, *pgd_ref;
573 pud_t *pud, *pud_ref;
574 pmd_t *pmd, *pmd_ref;
575 pte_t *pte, *pte_ref;
576
Harvey Harrisoncf89ec92008-02-04 16:47:56 +0100577 /* Make sure we are in vmalloc area */
578 if (!(address >= VMALLOC_START && address < VMALLOC_END))
579 return -1;
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* Copy kernel mappings over when needed. This can also
582 happen within a race in page table update. In the later
583 case just flush. */
584
585 pgd = pgd_offset(current->mm ?: &init_mm, address);
586 pgd_ref = pgd_offset_k(address);
587 if (pgd_none(*pgd_ref))
588 return -1;
589 if (pgd_none(*pgd))
590 set_pgd(pgd, *pgd_ref);
Jan Beulich8c914cb2006-03-25 16:29:40 +0100591 else
Dave McCracken46a82b22006-09-25 23:31:48 -0700592 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Below here mismatches are bugs because these lower tables
595 are shared */
596
597 pud = pud_offset(pgd, address);
598 pud_ref = pud_offset(pgd_ref, address);
599 if (pud_none(*pud_ref))
600 return -1;
Dave McCracken46a82b22006-09-25 23:31:48 -0700601 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 BUG();
603 pmd = pmd_offset(pud, address);
604 pmd_ref = pmd_offset(pud_ref, address);
605 if (pmd_none(*pmd_ref))
606 return -1;
607 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
608 BUG();
609 pte_ref = pte_offset_kernel(pmd_ref, address);
610 if (!pte_present(*pte_ref))
611 return -1;
612 pte = pte_offset_kernel(pmd, address);
Andi Kleen3b9ba4d2005-05-16 21:53:31 -0700613 /* Don't use pte_page here, because the mappings can point
614 outside mem_map, and the NUMA hash lookup cannot handle
615 that. */
616 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return 0;
Harvey Harrisonfdfe8aa2008-01-30 13:33:13 +0100619#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200622int show_unhandled_signals = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624/*
625 * This routine handles page faults. It determines the address,
626 * and the problem, and then passes it off to one of the appropriate
627 * routines.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100629#ifdef CONFIG_X86_64
630asmlinkage
631#endif
632void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct task_struct *tsk;
635 struct mm_struct *mm;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100636 struct vm_area_struct *vma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 unsigned long address;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100638 int write, si_code;
639 int fault;
640#ifdef CONFIG_X86_64
Jan Beulich12091402005-09-12 18:49:24 +0200641 unsigned long flags;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Peter Zijlstra143a5d32007-10-25 14:01:10 +0200644 /*
645 * We can fault from pretty much anywhere, with unknown IRQ state.
646 */
647 trace_hardirqs_fixup();
648
Arjan van de Vena9ba9a32006-03-25 16:30:10 +0100649 tsk = current;
650 mm = tsk->mm;
651 prefetchw(&mm->mmap_sem);
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /* get the address */
Glauber de Oliveira Costaf51c9452007-07-22 11:12:29 +0200654 address = read_cr2();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100656 si_code = SEGV_MAPERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Harvey Harrison608566b2008-01-30 13:33:12 +0100658 if (notify_page_fault(regs))
659 return;
Pekka Paalanen10c43d22008-05-12 21:20:57 +0200660 if (call_mmiotrace(regs, error_code, address))
Pekka Paalanen86069782008-05-12 21:20:56 +0200661 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /*
664 * We fault-in kernel-space virtual memory on-demand. The
665 * 'reference' page table is init_mm.pgd.
666 *
667 * NOTE! We MUST NOT take any locks for this case. We may
668 * be in an interrupt or a critical region, and should
669 * only copy the information from the master page table,
670 * nothing more.
671 *
672 * This verifies that the fault happens in kernel space
673 * (error_code & 4) == 0, and that the fault was not a
Jan Beulich8b1bde92006-01-11 22:42:23 +0100674 * protection error (error_code & 9) == 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100676#ifdef CONFIG_X86_32
677 if (unlikely(address >= TASK_SIZE)) {
Harvey Harrisoncf89ec92008-02-04 16:47:56 +0100678#else
679 if (unlikely(address >= TASK_SIZE64)) {
680#endif
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100681 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
682 vmalloc_fault(address) >= 0)
683 return;
Jeremy Fitzhardinge5b727a32008-01-30 13:34:11 +0100684
685 /* Can handle a stale RO->RW TLB */
686 if (spurious_fault(address, error_code))
687 return;
688
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100689 /*
690 * Don't take the mm semaphore here. If we fixup a prefetch
691 * fault we could otherwise deadlock.
692 */
693 goto bad_area_nosemaphore;
694 }
695
Harvey Harrisoncf89ec92008-02-04 16:47:56 +0100696
697#ifdef CONFIG_X86_32
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100698 /* It's safe to allow irq's after cr2 has been saved and the vmalloc
699 fault has been handled. */
gorcunov@gmail.com6b6891f2008-03-28 17:56:57 +0300700 if (regs->flags & (X86_EFLAGS_IF | X86_VM_MASK))
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100701 local_irq_enable();
702
703 /*
704 * If we're in an interrupt, have no user context or are running in an
705 * atomic region then we must not take the fault.
706 */
707 if (in_atomic() || !mm)
708 goto bad_area_nosemaphore;
709#else /* CONFIG_X86_64 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100710 if (likely(regs->flags & X86_EFLAGS_IF))
Jan Beulich8c914cb2006-03-25 16:29:40 +0100711 local_irq_enable();
712
Andi Kleen66c58152006-01-11 22:44:09 +0100713 if (unlikely(error_code & PF_RSVD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 pgtable_bad(address, regs, error_code);
715
716 /*
Harvey Harrison33cb5242008-01-30 13:32:19 +0100717 * If we're in an interrupt, have no user context or are running in an
718 * atomic region then we must not take the fault.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720 if (unlikely(in_atomic() || !mm))
721 goto bad_area_nosemaphore;
722
Linus Torvaldsdbe3ed12007-09-19 11:37:14 -0700723 /*
724 * User-mode registers count as a user access even for any
725 * potential system fault or CPU buglet.
726 */
727 if (user_mode_vm(regs))
728 error_code |= PF_USER;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100729again:
730#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 /* When running in the kernel we expect faults to occur only to
732 * addresses in user space. All other faults represent errors in the
Simon Arlott676b1852007-10-20 01:25:36 +0200733 * kernel and should generate an OOPS. Unfortunately, in the case of an
Adrian Bunk80f72282006-06-30 18:27:16 +0200734 * erroneous fault occurring in a code path which already holds mmap_sem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * we will deadlock attempting to validate the fault against the
736 * address space. Luckily the kernel only validly references user
737 * space from well defined areas of code, which are listed in the
738 * exceptions table.
739 *
740 * As the vast majority of faults will be valid we will only perform
Simon Arlott676b1852007-10-20 01:25:36 +0200741 * the source reference check when there is a possibility of a deadlock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * Attempt to lock the address space, if we cannot we then validate the
743 * source. If this is invalid we can skip the address space check,
744 * thus avoiding the deadlock.
745 */
746 if (!down_read_trylock(&mm->mmap_sem)) {
Andi Kleen66c58152006-01-11 22:44:09 +0100747 if ((error_code & PF_USER) == 0 &&
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100748 !search_exception_tables(regs->ip))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto bad_area_nosemaphore;
750 down_read(&mm->mmap_sem);
751 }
752
753 vma = find_vma(mm, address);
754 if (!vma)
755 goto bad_area;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100756 if (vma->vm_start <= address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto good_area;
758 if (!(vma->vm_flags & VM_GROWSDOWN))
759 goto bad_area;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100760 if (error_code & PF_USER) {
Harvey Harrison6f4d3682008-01-30 13:33:13 +0100761 /*
762 * Accessing the stack below %sp is always a bug.
763 * The large cushion allows instructions like enter
764 * and pusha to work. ("enter $65535,$31" pushes
765 * 32 pointers and then decrements %sp by 65535.)
Chuck Ebbert03fdc2c2006-06-26 13:59:50 +0200766 */
H. Peter Anvin65ea5b02008-01-30 13:30:56 +0100767 if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto bad_area;
769 }
770 if (expand_stack(vma, address))
771 goto bad_area;
772/*
773 * Ok, we have a good vm_area for this memory access, so
774 * we can handle it..
775 */
776good_area:
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100777 si_code = SEGV_ACCERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 write = 0;
Andi Kleen66c58152006-01-11 22:44:09 +0100779 switch (error_code & (PF_PROT|PF_WRITE)) {
Harvey Harrison33cb5242008-01-30 13:32:19 +0100780 default: /* 3: write, present */
781 /* fall through */
782 case PF_WRITE: /* write, not present */
783 if (!(vma->vm_flags & VM_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 goto bad_area;
Harvey Harrison33cb5242008-01-30 13:32:19 +0100785 write++;
786 break;
787 case PF_PROT: /* read, present */
788 goto bad_area;
789 case 0: /* read, not present */
790 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
791 goto bad_area;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100794#ifdef CONFIG_X86_32
795survive:
796#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
798 * If for any reason at all we couldn't handle the fault,
799 * make sure we exit gracefully rather than endlessly redo
800 * the fault.
801 */
Nick Piggin83c54072007-07-19 01:47:05 -0700802 fault = handle_mm_fault(mm, vma, address, write);
803 if (unlikely(fault & VM_FAULT_ERROR)) {
804 if (fault & VM_FAULT_OOM)
805 goto out_of_memory;
806 else if (fault & VM_FAULT_SIGBUS)
807 goto do_sigbus;
808 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
Nick Piggin83c54072007-07-19 01:47:05 -0700810 if (fault & VM_FAULT_MAJOR)
811 tsk->maj_flt++;
812 else
813 tsk->min_flt++;
Harvey Harrisond729ab32008-01-30 13:33:23 +0100814
815#ifdef CONFIG_X86_32
816 /*
817 * Did it hit the DOS screen memory VA from vm86 mode?
818 */
819 if (v8086_mode(regs)) {
820 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
821 if (bit < 32)
822 tsk->thread.screen_bitmap |= 1 << bit;
823 }
824#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 up_read(&mm->mmap_sem);
826 return;
827
828/*
829 * Something tried to access memory that isn't in our memory map..
830 * Fix it, but check if it's kernel or user first..
831 */
832bad_area:
833 up_read(&mm->mmap_sem);
834
835bad_area_nosemaphore:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* User mode accesses just cause a SIGSEGV */
Andi Kleen66c58152006-01-11 22:44:09 +0100837 if (error_code & PF_USER) {
Steven Rostedte5e3c842007-06-06 23:34:04 -0400838 /*
839 * It's possible to have interrupts off here.
840 */
841 local_irq_enable();
842
Harvey Harrison1156e092008-01-30 13:34:10 +0100843 /*
844 * Valid to do another page fault here because this one came
845 * from user space.
846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (is_prefetch(regs, address, error_code))
848 return;
849
Harvey Harrison35f32662008-01-30 13:34:09 +0100850 if (is_errata100(regs, address))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return;
852
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200853 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
854 printk_ratelimit()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 printk(
Harvey Harrison6f4d3682008-01-30 13:33:13 +0100856#ifdef CONFIG_X86_32
Harvey Harrisonedcd8112008-01-30 13:33:16 +0100857 "%s%s[%d]: segfault at %lx ip %08lx sp %08lx error %lx",
Harvey Harrison6f4d3682008-01-30 13:33:13 +0100858#else
Andi Kleen03252912008-01-30 13:33:18 +0100859 "%s%s[%d]: segfault at %lx ip %lx sp %lx error %lx",
Harvey Harrison6f4d3682008-01-30 13:33:13 +0100860#endif
861 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
862 tsk->comm, task_pid_nr(tsk), address, regs->ip,
863 regs->sp, error_code);
Andi Kleen03252912008-01-30 13:33:18 +0100864 print_vma_addr(" in ", regs->ip);
865 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Harvey Harrison33cb5242008-01-30 13:32:19 +0100867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 tsk->thread.cr2 = address;
869 /* Kernel addresses are always protection faults */
870 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
871 tsk->thread.trap_no = 14;
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100872 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return;
874 }
875
Harvey Harrison29caf2f2008-01-30 13:34:09 +0100876 if (is_f00f_bug(regs, address))
877 return;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879no_context:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Are we prepared to handle this kernel fault? */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100881 if (fixup_exception(regs))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Harvey Harrison33cb5242008-01-30 13:32:19 +0100884 /*
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100885 * X86_32
886 * Valid to do another page fault here, because if this fault
887 * had been triggered by is_prefetch fixup_exception would have
888 * handled it.
889 *
890 * X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 * Hall of shame of CPU/BIOS bugs.
892 */
Harvey Harrison33cb5242008-01-30 13:32:19 +0100893 if (is_prefetch(regs, address, error_code))
894 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if (is_errata93(regs, address))
Harvey Harrison33cb5242008-01-30 13:32:19 +0100897 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899/*
900 * Oops. The kernel tried to access some bad page. We'll have to
901 * terminate things with extreme prejudice.
902 */
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100903#ifdef CONFIG_X86_32
904 bust_spinlocks(1);
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100905#else
906 flags = oops_begin();
907#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100909 show_fault_oops(regs, error_code, address);
910
911 tsk->thread.cr2 = address;
912 tsk->thread.trap_no = 14;
913 tsk->thread.error_code = error_code;
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100914
915#ifdef CONFIG_X86_32
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100916 die("Oops", regs, error_code);
917 bust_spinlocks(0);
918 do_exit(SIGKILL);
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100919#else
Jan Beulich22f59912008-01-30 13:31:23 +0100920 if (__die("Oops", regs, error_code))
921 regs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 /* Executive summary in case the body of the oops scrolled away */
923 printk(KERN_EMERG "CR2: %016lx\n", address);
Jan Beulich22f59912008-01-30 13:31:23 +0100924 oops_end(flags, regs, SIGKILL);
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927/*
928 * We ran out of memory, or some other thing happened to us that made
929 * us unable to handle the page fault gracefully.
930 */
931out_of_memory:
932 up_read(&mm->mmap_sem);
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100933 if (is_global_init(tsk)) {
934 yield();
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100935#ifdef CONFIG_X86_32
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100936 down_read(&mm->mmap_sem);
937 goto survive;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100938#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto again;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100940#endif
Harvey Harrisonfd40d6e2008-01-30 13:34:11 +0100941 }
942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 printk("VM: killing process %s\n", tsk->comm);
Harvey Harrison318aa292008-01-30 13:32:59 +0100944 if (error_code & PF_USER)
Will Schmidt021daae2007-07-21 17:11:17 +0200945 do_group_exit(SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 goto no_context;
947
948do_sigbus:
949 up_read(&mm->mmap_sem);
950
951 /* Kernel mode? Handle exceptions or die */
Andi Kleen66c58152006-01-11 22:44:09 +0100952 if (!(error_code & PF_USER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 goto no_context;
Harvey Harrisonf8c2ee22008-01-30 13:34:10 +0100954#ifdef CONFIG_X86_32
955 /* User space => ok to do another page fault */
956 if (is_prefetch(regs, address, error_code))
957 return;
958#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 tsk->thread.cr2 = address;
960 tsk->thread.error_code = error_code;
961 tsk->thread.trap_no = 14;
Harvey Harrisonc4aba4a2008-01-30 13:32:35 +0100962 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
Andi Kleen9e43e1b2005-11-05 17:25:54 +0100964
Jan Beulich8c914cb2006-03-25 16:29:40 +0100965DEFINE_SPINLOCK(pgd_lock);
Christoph Lameter2bff7382007-05-02 19:27:10 +0200966LIST_HEAD(pgd_list);
Jan Beulich8c914cb2006-03-25 16:29:40 +0100967
968void vmalloc_sync_all(void)
969{
Harvey Harrison1156e092008-01-30 13:34:10 +0100970#ifdef CONFIG_X86_32
971 /*
972 * Note that races in the updates of insync and start aren't
973 * problematic: insync can only get set bits added, and updates to
974 * start are only improving performance (without affecting correctness
975 * if undone).
976 */
977 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
978 static unsigned long start = TASK_SIZE;
979 unsigned long address;
980
981 if (SHARED_KERNEL_PMD)
982 return;
983
984 BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
985 for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
986 if (!test_bit(pgd_index(address), insync)) {
987 unsigned long flags;
988 struct page *page;
989
990 spin_lock_irqsave(&pgd_lock, flags);
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100991 list_for_each_entry(page, &pgd_list, lru) {
Harvey Harrison1156e092008-01-30 13:34:10 +0100992 if (!vmalloc_sync_one(page_address(page),
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100993 address))
Harvey Harrison1156e092008-01-30 13:34:10 +0100994 break;
Jeremy Fitzhardingee3ed9102008-01-30 13:34:11 +0100995 }
Harvey Harrison1156e092008-01-30 13:34:10 +0100996 spin_unlock_irqrestore(&pgd_lock, flags);
997 if (!page)
998 set_bit(pgd_index(address), insync);
999 }
1000 if (address == start && test_bit(pgd_index(address), insync))
1001 start = address + PGDIR_SIZE;
1002 }
1003#else /* CONFIG_X86_64 */
Harvey Harrison6f4d3682008-01-30 13:33:13 +01001004 /*
1005 * Note that races in the updates of insync and start aren't
1006 * problematic: insync can only get set bits added, and updates to
1007 * start are only improving performance (without affecting correctness
1008 * if undone).
1009 */
Jan Beulich8c914cb2006-03-25 16:29:40 +01001010 static DECLARE_BITMAP(insync, PTRS_PER_PGD);
1011 static unsigned long start = VMALLOC_START & PGDIR_MASK;
1012 unsigned long address;
1013
1014 for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) {
1015 if (!test_bit(pgd_index(address), insync)) {
1016 const pgd_t *pgd_ref = pgd_offset_k(address);
Ingo Molnar58d5d0d2008-02-06 22:39:45 +01001017 unsigned long flags;
Jan Beulich8c914cb2006-03-25 16:29:40 +01001018 struct page *page;
1019
1020 if (pgd_none(*pgd_ref))
1021 continue;
Ingo Molnar58d5d0d2008-02-06 22:39:45 +01001022 spin_lock_irqsave(&pgd_lock, flags);
Christoph Lameter2bff7382007-05-02 19:27:10 +02001023 list_for_each_entry(page, &pgd_list, lru) {
Jan Beulich8c914cb2006-03-25 16:29:40 +01001024 pgd_t *pgd;
1025 pgd = (pgd_t *)page_address(page) + pgd_index(address);
1026 if (pgd_none(*pgd))
1027 set_pgd(pgd, *pgd_ref);
1028 else
Dave McCracken46a82b22006-09-25 23:31:48 -07001029 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
Jan Beulich8c914cb2006-03-25 16:29:40 +01001030 }
Ingo Molnar58d5d0d2008-02-06 22:39:45 +01001031 spin_unlock_irqrestore(&pgd_lock, flags);
Jan Beulich8c914cb2006-03-25 16:29:40 +01001032 set_bit(pgd_index(address), insync);
1033 }
1034 if (address == start)
1035 start = address + PGDIR_SIZE;
1036 }
Harvey Harrison1156e092008-01-30 13:34:10 +01001037#endif
Jan Beulich8c914cb2006-03-25 16:29:40 +01001038}