x86, mm, kprobes: fault.c, simplify notify_page_fault()
[deliverable/linux.git] / arch / x86 / mm / fault.c
CommitLineData
1da177e4 1/*
1da177e4 2 * Copyright (C) 1995 Linus Torvalds
2d4a7167 3 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
1da177e4 4 */
1da177e4 5#include <linux/interrupt.h>
2d4a7167
IM
6#include <linux/mmiotrace.h>
7#include <linux/bootmem.h>
1da177e4 8#include <linux/compiler.h>
c61e211d 9#include <linux/highmem.h>
0f2fbdcb 10#include <linux/kprobes.h>
ab2bf0c1 11#include <linux/uaccess.h>
2d4a7167
IM
12#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>
1eeb66a1 19#include <linux/kdebug.h>
2d4a7167 20#include <linux/errno.h>
7c9f8861 21#include <linux/magic.h>
2d4a7167
IM
22#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>
29
30#include <asm-generic/sections.h>
1da177e4 31
1da177e4 32#include <asm/tlbflush.h>
2d4a7167
IM
33#include <asm/pgalloc.h>
34#include <asm/segment.h>
35#include <asm/system.h>
1da177e4 36#include <asm/proto.h>
70ef5641 37#include <asm/traps.h>
2d4a7167 38#include <asm/desc.h>
1da177e4 39
33cb5243 40/*
2d4a7167
IM
41 * 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
33cb5243 48 */
2d4a7167
IM
49enum 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};
66c58156 57
b814d41f
IM
58/*
59 * (returns 0 if mmiotrace is disabled)
60 */
0fd0e3da 61static inline int kmmio_fault(struct pt_regs *regs, unsigned long addr)
86069782 62{
0fd0e3da
PP
63 if (unlikely(is_kmmio_active()))
64 if (kmmio_handler(regs, addr) == 1)
65 return -1;
0fd0e3da 66 return 0;
86069782
PP
67}
68
74a0b576 69static inline int notify_page_fault(struct pt_regs *regs)
1bd858a5 70{
74a0b576
CH
71 int ret = 0;
72
73 /* kprobe_running() needs smp_processor_id() */
b1801812 74 if (kprobes_built_in() && !user_mode_vm(regs)) {
74a0b576
CH
75 preempt_disable();
76 if (kprobe_running() && kprobe_fault_handler(regs, 14))
77 ret = 1;
78 preempt_enable();
79 }
1bd858a5 80
74a0b576 81 return ret;
33cb5243 82}
1bd858a5 83
1dc85be0 84/*
2d4a7167
IM
85 * Prefetch quirks:
86 *
87 * 32-bit mode:
88 *
89 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
90 * Check that here and ignore it.
1dc85be0 91 *
2d4a7167 92 * 64-bit mode:
1dc85be0 93 *
2d4a7167
IM
94 * Sometimes the CPU reports invalid exceptions on prefetch.
95 * Check that here and ignore it.
96 *
97 * Opcode checker based on code by Richard Brunner.
1dc85be0 98 */
107a0367
IM
99static inline int
100check_prefetch_opcode(struct pt_regs *regs, unsigned char *instr,
101 unsigned char opcode, int *prefetch)
102{
103 unsigned char instr_hi = opcode & 0xf0;
104 unsigned char instr_lo = opcode & 0x0f;
105
106 switch (instr_hi) {
107 case 0x20:
108 case 0x30:
109 /*
110 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
111 * In X86_64 long mode, the CPU will signal invalid
112 * opcode if some of these prefixes are present so
113 * X86_64 will never get here anyway
114 */
115 return ((instr_lo & 7) == 0x6);
116#ifdef CONFIG_X86_64
117 case 0x40:
118 /*
119 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
120 * Need to figure out under what instruction mode the
121 * instruction was issued. Could check the LDT for lm,
122 * but for now it's good enough to assume that long
123 * mode only uses well known segments or kernel.
124 */
125 return (!user_mode(regs)) || (regs->cs == __USER_CS);
126#endif
127 case 0x60:
128 /* 0x64 thru 0x67 are valid prefixes in all modes. */
129 return (instr_lo & 0xC) == 0x4;
130 case 0xF0:
131 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
132 return !instr_lo || (instr_lo>>1) == 1;
133 case 0x00:
134 /* Prefetch instruction is 0x0F0D or 0x0F18 */
135 if (probe_kernel_address(instr, opcode))
136 return 0;
137
138 *prefetch = (instr_lo == 0xF) &&
139 (opcode == 0x0D || opcode == 0x18);
140 return 0;
141 default:
142 return 0;
143 }
144}
145
2d4a7167
IM
146static int
147is_prefetch(struct pt_regs *regs, unsigned long error_code, unsigned long addr)
33cb5243 148{
2d4a7167 149 unsigned char *max_instr;
ab2bf0c1 150 unsigned char *instr;
33cb5243 151 int prefetch = 0;
1da177e4 152
3085354d
IM
153 /*
154 * If it was a exec (instruction fetch) fault on NX page, then
155 * do not ignore the fault:
156 */
66c58156 157 if (error_code & PF_INSTR)
1da177e4 158 return 0;
1dc85be0 159
107a0367 160 instr = (void *)convert_ip_to_linear(current, regs);
f1290ec9 161 max_instr = instr + 15;
1da177e4 162
76381fee 163 if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
1da177e4
LT
164 return 0;
165
107a0367 166 while (instr < max_instr) {
2d4a7167 167 unsigned char opcode;
1da177e4 168
ab2bf0c1 169 if (probe_kernel_address(instr, opcode))
33cb5243 170 break;
1da177e4 171
1da177e4
LT
172 instr++;
173
107a0367 174 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
1da177e4 175 break;
1da177e4
LT
176 }
177 return prefetch;
178}
179
2d4a7167
IM
180static void
181force_sig_info_fault(int si_signo, int si_code, unsigned long address,
182 struct task_struct *tsk)
c4aba4a8
HH
183{
184 siginfo_t info;
185
2d4a7167
IM
186 info.si_signo = si_signo;
187 info.si_errno = 0;
188 info.si_code = si_code;
189 info.si_addr = (void __user *)address;
190
c4aba4a8
HH
191 force_sig_info(si_signo, &info, tsk);
192}
193
1156e098 194#ifdef CONFIG_X86_64
33cb5243
HH
195static int bad_address(void *p)
196{
1da177e4 197 unsigned long dummy;
2d4a7167 198
ab2bf0c1 199 return probe_kernel_address((unsigned long *)p, dummy);
33cb5243 200}
1156e098 201#endif
1da177e4 202
cae30f82 203static void dump_pagetable(unsigned long address)
1da177e4 204{
1156e098
HH
205#ifdef CONFIG_X86_32
206 __typeof__(pte_val(__pte(0))) page;
207
208 page = read_cr3();
209 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
2d4a7167 210
1156e098
HH
211#ifdef CONFIG_X86_PAE
212 printk("*pdpt = %016Lx ", page);
213 if ((page >> PAGE_SHIFT) < max_low_pfn
214 && page & _PAGE_PRESENT) {
215 page &= PAGE_MASK;
216 page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
2d4a7167 217 & (PTRS_PER_PMD - 1)];
1156e098
HH
218 printk(KERN_CONT "*pde = %016Lx ", page);
219 page &= ~_PAGE_NX;
220 }
221#else
222 printk("*pde = %08lx ", page);
223#endif
224
225 /*
226 * We must not directly access the pte in the highpte
227 * case if the page table is located in highmem.
228 * And let's rather not kmap-atomic the pte, just in case
2d4a7167 229 * it's allocated already:
1156e098
HH
230 */
231 if ((page >> PAGE_SHIFT) < max_low_pfn
232 && (page & _PAGE_PRESENT)
233 && !(page & _PAGE_PSE)) {
2d4a7167 234
1156e098
HH
235 page &= PAGE_MASK;
236 page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
2d4a7167 237 & (PTRS_PER_PTE - 1)];
1156e098
HH
238 printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
239 }
240
241 printk("\n");
242#else /* CONFIG_X86_64 */
1da177e4
LT
243 pgd_t *pgd;
244 pud_t *pud;
245 pmd_t *pmd;
246 pte_t *pte;
247
f51c9452 248 pgd = (pgd_t *)read_cr3();
1da177e4 249
33cb5243 250 pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
2d4a7167 251
1da177e4 252 pgd += pgd_index(address);
2d4a7167
IM
253 if (bad_address(pgd))
254 goto bad;
255
d646bce4 256 printk("PGD %lx ", pgd_val(*pgd));
2d4a7167
IM
257
258 if (!pgd_present(*pgd))
259 goto out;
1da177e4 260
d2ae5b5f 261 pud = pud_offset(pgd, address);
2d4a7167
IM
262 if (bad_address(pud))
263 goto bad;
264
1da177e4 265 printk("PUD %lx ", pud_val(*pud));
b5360222 266 if (!pud_present(*pud) || pud_large(*pud))
2d4a7167 267 goto out;
1da177e4
LT
268
269 pmd = pmd_offset(pud, address);
2d4a7167
IM
270 if (bad_address(pmd))
271 goto bad;
272
1da177e4 273 printk("PMD %lx ", pmd_val(*pmd));
2d4a7167
IM
274 if (!pmd_present(*pmd) || pmd_large(*pmd))
275 goto out;
1da177e4
LT
276
277 pte = pte_offset_kernel(pmd, address);
2d4a7167
IM
278 if (bad_address(pte))
279 goto bad;
280
33cb5243 281 printk("PTE %lx", pte_val(*pte));
2d4a7167 282out:
1da177e4
LT
283 printk("\n");
284 return;
285bad:
286 printk("BAD\n");
1156e098
HH
287#endif
288}
289
290#ifdef CONFIG_X86_32
291static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
292{
293 unsigned index = pgd_index(address);
294 pgd_t *pgd_k;
295 pud_t *pud, *pud_k;
296 pmd_t *pmd, *pmd_k;
297
298 pgd += index;
299 pgd_k = init_mm.pgd + index;
300
301 if (!pgd_present(*pgd_k))
302 return NULL;
303
304 /*
305 * set_pgd(pgd, *pgd_k); here would be useless on PAE
306 * and redundant with the set_pmd() on non-PAE. As would
307 * set_pud.
308 */
1156e098
HH
309 pud = pud_offset(pgd, address);
310 pud_k = pud_offset(pgd_k, address);
311 if (!pud_present(*pud_k))
312 return NULL;
313
314 pmd = pmd_offset(pud, address);
315 pmd_k = pmd_offset(pud_k, address);
316 if (!pmd_present(*pmd_k))
317 return NULL;
2d4a7167 318
1156e098
HH
319 if (!pmd_present(*pmd)) {
320 set_pmd(pmd, *pmd_k);
321 arch_flush_lazy_mmu_mode();
2d4a7167 322 } else {
1156e098 323 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
2d4a7167
IM
324 }
325
1156e098 326 return pmd_k;
1da177e4
LT
327}
328
8c938f9f
IM
329/*
330 * Did it hit the DOS screen memory VA from vm86 mode?
331 */
332static inline void
333check_v8086_mode(struct pt_regs *regs, unsigned long address,
334 struct task_struct *tsk)
335{
336 unsigned long bit;
337
338 if (!v8086_mode(regs))
339 return;
340
341 bit = (address - 0xA0000) >> PAGE_SHIFT;
342 if (bit < 32)
343 tsk->thread.screen_bitmap |= 1 << bit;
344}
345
346#else /* CONFIG_X86_64: */
347
33cb5243 348static const char errata93_warning[] =
1da177e4
LT
349KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
350KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
351KERN_ERR "******* Please consider a BIOS update.\n"
352KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
8c938f9f
IM
353
354/*
355 * No vm86 mode in 64-bit mode:
356 */
357static inline void
358check_v8086_mode(struct pt_regs *regs, unsigned long address,
359 struct task_struct *tsk)
360{
361}
362
fdfe8aa8 363#endif
1da177e4 364
2d4a7167
IM
365/*
366 * Workaround for K8 erratum #93 & buggy BIOS.
367 *
368 * BIOS SMM functions are required to use a specific workaround
369 * to avoid corruption of the 64bit RIP register on C stepping K8.
370 *
371 * A lot of BIOS that didn't get tested properly miss this.
372 *
373 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
374 * Try to work around it here.
375 *
376 * Note we only handle faults in kernel here.
377 * Does nothing on 32-bit.
fdfe8aa8 378 */
33cb5243 379static int is_errata93(struct pt_regs *regs, unsigned long address)
1da177e4 380{
fdfe8aa8 381#ifdef CONFIG_X86_64
2d4a7167
IM
382 static int once;
383
65ea5b03 384 if (address != regs->ip)
1da177e4 385 return 0;
2d4a7167 386
33cb5243 387 if ((address >> 32) != 0)
1da177e4 388 return 0;
2d4a7167 389
1da177e4 390 address |= 0xffffffffUL << 32;
33cb5243
HH
391 if ((address >= (u64)_stext && address <= (u64)_etext) ||
392 (address >= MODULES_VADDR && address <= MODULES_END)) {
2d4a7167 393 if (!once) {
33cb5243 394 printk(errata93_warning);
2d4a7167 395 once = 1;
1da177e4 396 }
65ea5b03 397 regs->ip = address;
1da177e4
LT
398 return 1;
399 }
fdfe8aa8 400#endif
1da177e4 401 return 0;
33cb5243 402}
1da177e4 403
35f3266f 404/*
2d4a7167
IM
405 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
406 * to illegal addresses >4GB.
407 *
408 * We catch this in the page fault handler because these addresses
409 * are not reachable. Just detect this case and return. Any code
35f3266f
HH
410 * segment in LDT is compatibility mode.
411 */
412static int is_errata100(struct pt_regs *regs, unsigned long address)
413{
414#ifdef CONFIG_X86_64
2d4a7167 415 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && (address >> 32))
35f3266f
HH
416 return 1;
417#endif
418 return 0;
419}
420
29caf2f9
HH
421static int is_f00f_bug(struct pt_regs *regs, unsigned long address)
422{
423#ifdef CONFIG_X86_F00F_BUG
424 unsigned long nr;
2d4a7167 425
29caf2f9 426 /*
2d4a7167 427 * Pentium F0 0F C7 C8 bug workaround:
29caf2f9
HH
428 */
429 if (boot_cpu_data.f00f_bug) {
430 nr = (address - idt_descr.address) >> 3;
431
432 if (nr == 6) {
433 do_invalid_op(regs, 0);
434 return 1;
435 }
436 }
437#endif
438 return 0;
439}
440
2d4a7167
IM
441static void
442show_fault_oops(struct pt_regs *regs, unsigned long error_code,
443 unsigned long address)
b3279c7f 444{
1156e098
HH
445#ifdef CONFIG_X86_32
446 if (!oops_may_print())
447 return;
fd40d6e3 448#endif
1156e098
HH
449
450#ifdef CONFIG_X86_PAE
451 if (error_code & PF_INSTR) {
93809be8 452 unsigned int level;
2d4a7167 453
1156e098
HH
454 pte_t *pte = lookup_address(address, &level);
455
2d4a7167 456 if (pte && pte_present(*pte) && !pte_exec(*pte)) {
1156e098
HH
457 printk(KERN_CRIT "kernel tried to execute "
458 "NX-protected page - exploit attempt? "
350b4da7 459 "(uid: %d)\n", current_uid());
2d4a7167 460 }
1156e098
HH
461 }
462#endif
1156e098 463
19f0dda9 464 printk(KERN_ALERT "BUG: unable to handle kernel ");
b3279c7f 465 if (address < PAGE_SIZE)
19f0dda9 466 printk(KERN_CONT "NULL pointer dereference");
b3279c7f 467 else
19f0dda9 468 printk(KERN_CONT "paging request");
2d4a7167 469
f294a8ce 470 printk(KERN_CONT " at %p\n", (void *) address);
19f0dda9 471 printk(KERN_ALERT "IP:");
b3279c7f 472 printk_address(regs->ip, 1);
2d4a7167 473
b3279c7f
HH
474 dump_pagetable(address);
475}
476
2d4a7167
IM
477static noinline void
478pgtable_bad(struct pt_regs *regs, unsigned long error_code,
479 unsigned long address)
1da177e4 480{
2d4a7167
IM
481 struct task_struct *tsk;
482 unsigned long flags;
483 int sig;
484
485 flags = oops_begin();
486 tsk = current;
487 sig = SIGKILL;
1209140c 488
1da177e4 489 printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
92181f19 490 tsk->comm, address);
1da177e4 491 dump_pagetable(address);
2d4a7167
IM
492
493 tsk->thread.cr2 = address;
494 tsk->thread.trap_no = 14;
495 tsk->thread.error_code = error_code;
496
22f5991c 497 if (__die("Bad pagetable", regs, error_code))
874d93d1 498 sig = 0;
2d4a7167 499
874d93d1 500 oops_end(flags, regs, sig);
1da177e4
LT
501}
502
2d4a7167
IM
503static noinline void
504no_context(struct pt_regs *regs, unsigned long error_code,
505 unsigned long address)
92181f19
NP
506{
507 struct task_struct *tsk = current;
19803078
IM
508 unsigned long *stackend;
509
92181f19
NP
510#ifdef CONFIG_X86_64
511 unsigned long flags;
512 int sig;
513#endif
514
2d4a7167 515 /* Are we prepared to handle this kernel fault? */
92181f19
NP
516 if (fixup_exception(regs))
517 return;
518
519 /*
2d4a7167
IM
520 * 32-bit:
521 *
522 * Valid to do another page fault here, because if this fault
523 * had been triggered by is_prefetch fixup_exception would have
524 * handled it.
525 *
526 * 64-bit:
92181f19 527 *
2d4a7167 528 * Hall of shame of CPU/BIOS bugs.
92181f19
NP
529 */
530 if (is_prefetch(regs, error_code, address))
531 return;
532
533 if (is_errata93(regs, address))
534 return;
535
536 /*
537 * Oops. The kernel tried to access some bad page. We'll have to
2d4a7167 538 * terminate things with extreme prejudice:
92181f19
NP
539 */
540#ifdef CONFIG_X86_32
541 bust_spinlocks(1);
542#else
543 flags = oops_begin();
544#endif
545
546 show_fault_oops(regs, error_code, address);
547
2d4a7167 548 stackend = end_of_stack(tsk);
19803078
IM
549 if (*stackend != STACK_END_MAGIC)
550 printk(KERN_ALERT "Thread overran stack, or stack corrupted\n");
551
92181f19
NP
552 tsk->thread.cr2 = address;
553 tsk->thread.trap_no = 14;
554 tsk->thread.error_code = error_code;
555
556#ifdef CONFIG_X86_32
557 die("Oops", regs, error_code);
558 bust_spinlocks(0);
559 do_exit(SIGKILL);
560#else
561 sig = SIGKILL;
562 if (__die("Oops", regs, error_code))
563 sig = 0;
2d4a7167 564
92181f19
NP
565 /* Executive summary in case the body of the oops scrolled away */
566 printk(KERN_EMERG "CR2: %016lx\n", address);
2d4a7167 567
92181f19
NP
568 oops_end(flags, regs, sig);
569#endif
570}
571
2d4a7167
IM
572/*
573 * Print out info about fatal segfaults, if the show_unhandled_signals
574 * sysctl is set:
575 */
576static inline void
577show_signal_msg(struct pt_regs *regs, unsigned long error_code,
578 unsigned long address, struct task_struct *tsk)
579{
580 if (!unhandled_signal(tsk, SIGSEGV))
581 return;
582
583 if (!printk_ratelimit())
584 return;
585
586 printk(KERN_CONT "%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
587 task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
588 tsk->comm, task_pid_nr(tsk), address,
589 (void *)regs->ip, (void *)regs->sp, error_code);
590
591 print_vma_addr(KERN_CONT " in ", regs->ip);
592
593 printk(KERN_CONT "\n");
594}
595
596static void
597__bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
598 unsigned long address, int si_code)
92181f19
NP
599{
600 struct task_struct *tsk = current;
601
602 /* User mode accesses just cause a SIGSEGV */
603 if (error_code & PF_USER) {
604 /*
2d4a7167 605 * It's possible to have interrupts off here:
92181f19
NP
606 */
607 local_irq_enable();
608
609 /*
610 * Valid to do another page fault here because this one came
2d4a7167 611 * from user space:
92181f19
NP
612 */
613 if (is_prefetch(regs, error_code, address))
614 return;
615
616 if (is_errata100(regs, address))
617 return;
618
2d4a7167
IM
619 if (unlikely(show_unhandled_signals))
620 show_signal_msg(regs, error_code, address, tsk);
621
622 /* Kernel addresses are always protection faults: */
623 tsk->thread.cr2 = address;
624 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
625 tsk->thread.trap_no = 14;
92181f19 626
92181f19 627 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
2d4a7167 628
92181f19
NP
629 return;
630 }
631
632 if (is_f00f_bug(regs, address))
633 return;
634
635 no_context(regs, error_code, address);
636}
637
2d4a7167
IM
638static noinline void
639bad_area_nosemaphore(struct pt_regs *regs, unsigned long error_code,
640 unsigned long address)
92181f19
NP
641{
642 __bad_area_nosemaphore(regs, error_code, address, SEGV_MAPERR);
643}
644
2d4a7167
IM
645static void
646__bad_area(struct pt_regs *regs, unsigned long error_code,
647 unsigned long address, int si_code)
92181f19
NP
648{
649 struct mm_struct *mm = current->mm;
650
651 /*
652 * Something tried to access memory that isn't in our memory map..
653 * Fix it, but check if it's kernel or user first..
654 */
655 up_read(&mm->mmap_sem);
656
657 __bad_area_nosemaphore(regs, error_code, address, si_code);
658}
659
2d4a7167
IM
660static noinline void
661bad_area(struct pt_regs *regs, unsigned long error_code, unsigned long address)
92181f19
NP
662{
663 __bad_area(regs, error_code, address, SEGV_MAPERR);
664}
665
2d4a7167
IM
666static noinline void
667bad_area_access_error(struct pt_regs *regs, unsigned long error_code,
668 unsigned long address)
92181f19
NP
669{
670 __bad_area(regs, error_code, address, SEGV_ACCERR);
671}
672
673/* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
2d4a7167
IM
674static void
675out_of_memory(struct pt_regs *regs, unsigned long error_code,
676 unsigned long address)
92181f19
NP
677{
678 /*
679 * We ran out of memory, call the OOM killer, and return the userspace
2d4a7167 680 * (which will retry the fault, or kill us if we got oom-killed):
92181f19
NP
681 */
682 up_read(&current->mm->mmap_sem);
2d4a7167 683
92181f19
NP
684 pagefault_out_of_memory();
685}
686
2d4a7167
IM
687static void
688do_sigbus(struct pt_regs *regs, unsigned long error_code, unsigned long address)
92181f19
NP
689{
690 struct task_struct *tsk = current;
691 struct mm_struct *mm = tsk->mm;
692
693 up_read(&mm->mmap_sem);
694
2d4a7167 695 /* Kernel mode? Handle exceptions or die: */
92181f19
NP
696 if (!(error_code & PF_USER))
697 no_context(regs, error_code, address);
2d4a7167 698
92181f19 699#ifdef CONFIG_X86_32
2d4a7167 700 /* User space => ok to do another page fault: */
92181f19
NP
701 if (is_prefetch(regs, error_code, address))
702 return;
703#endif
2d4a7167
IM
704
705 tsk->thread.cr2 = address;
706 tsk->thread.error_code = error_code;
707 tsk->thread.trap_no = 14;
708
92181f19
NP
709 force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
710}
711
2d4a7167
IM
712static noinline void
713mm_fault_error(struct pt_regs *regs, unsigned long error_code,
714 unsigned long address, unsigned int fault)
92181f19 715{
2d4a7167 716 if (fault & VM_FAULT_OOM) {
92181f19 717 out_of_memory(regs, error_code, address);
2d4a7167
IM
718 } else {
719 if (fault & VM_FAULT_SIGBUS)
720 do_sigbus(regs, error_code, address);
721 else
722 BUG();
723 }
92181f19
NP
724}
725
d8b57bb7
TG
726static int spurious_fault_check(unsigned long error_code, pte_t *pte)
727{
728 if ((error_code & PF_WRITE) && !pte_write(*pte))
729 return 0;
2d4a7167 730
d8b57bb7
TG
731 if ((error_code & PF_INSTR) && !pte_exec(*pte))
732 return 0;
733
734 return 1;
735}
736
5b727a3b 737/*
2d4a7167
IM
738 * Handle a spurious fault caused by a stale TLB entry.
739 *
740 * This allows us to lazily refresh the TLB when increasing the
741 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
742 * eagerly is very expensive since that implies doing a full
743 * cross-processor TLB flush, even if no stale TLB entries exist
744 * on other processors.
745 *
5b727a3b
JF
746 * There are no security implications to leaving a stale TLB when
747 * increasing the permissions on a page.
748 */
2d4a7167
IM
749static noinline int
750spurious_fault(unsigned long error_code, unsigned long address)
5b727a3b
JF
751{
752 pgd_t *pgd;
753 pud_t *pud;
754 pmd_t *pmd;
755 pte_t *pte;
3c3e5694 756 int ret;
5b727a3b
JF
757
758 /* Reserved-bit violation or user access to kernel space? */
759 if (error_code & (PF_USER | PF_RSVD))
760 return 0;
761
762 pgd = init_mm.pgd + pgd_index(address);
763 if (!pgd_present(*pgd))
764 return 0;
765
766 pud = pud_offset(pgd, address);
767 if (!pud_present(*pud))
768 return 0;
769
d8b57bb7
TG
770 if (pud_large(*pud))
771 return spurious_fault_check(error_code, (pte_t *) pud);
772
5b727a3b
JF
773 pmd = pmd_offset(pud, address);
774 if (!pmd_present(*pmd))
775 return 0;
776
d8b57bb7
TG
777 if (pmd_large(*pmd))
778 return spurious_fault_check(error_code, (pte_t *) pmd);
779
5b727a3b
JF
780 pte = pte_offset_kernel(pmd, address);
781 if (!pte_present(*pte))
782 return 0;
783
3c3e5694
SR
784 ret = spurious_fault_check(error_code, pte);
785 if (!ret)
786 return 0;
787
788 /*
2d4a7167
IM
789 * Make sure we have permissions in PMD.
790 * If not, then there's a bug in the page tables:
3c3e5694
SR
791 */
792 ret = spurious_fault_check(error_code, (pte_t *) pmd);
793 WARN_ONCE(!ret, "PMD has incorrect permission bits\n");
2d4a7167 794
3c3e5694 795 return ret;
5b727a3b
JF
796}
797
1da177e4 798/*
2d4a7167
IM
799 * 32-bit:
800 *
801 * Handle a fault on the vmalloc or module mapping area
f8c2ee22 802 *
2d4a7167
IM
803 * 64-bit:
804 *
805 * Handle a fault on the vmalloc area
3b9ba4d5
AK
806 *
807 * This assumes no large pages in there.
1da177e4 808 */
92181f19 809static noinline int vmalloc_fault(unsigned long address)
1da177e4 810{
fdfe8aa8
HH
811#ifdef CONFIG_X86_32
812 unsigned long pgd_paddr;
813 pmd_t *pmd_k;
814 pte_t *pte_k;
b29c701d 815
2d4a7167 816 /* Make sure we are in vmalloc area: */
b29c701d
HN
817 if (!(address >= VMALLOC_START && address < VMALLOC_END))
818 return -1;
819
fdfe8aa8
HH
820 /*
821 * Synchronize this task's top level page-table
822 * with the 'reference' page table.
823 *
824 * Do _not_ use "current" here. We might be inside
825 * an interrupt in the middle of a task switch..
826 */
827 pgd_paddr = read_cr3();
828 pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
829 if (!pmd_k)
830 return -1;
2d4a7167 831
fdfe8aa8
HH
832 pte_k = pte_offset_kernel(pmd_k, address);
833 if (!pte_present(*pte_k))
834 return -1;
2d4a7167 835
fdfe8aa8
HH
836 return 0;
837#else
1da177e4
LT
838 pgd_t *pgd, *pgd_ref;
839 pud_t *pud, *pud_ref;
840 pmd_t *pmd, *pmd_ref;
841 pte_t *pte, *pte_ref;
842
2d4a7167 843 /* Make sure we are in vmalloc area: */
cf89ec92
HH
844 if (!(address >= VMALLOC_START && address < VMALLOC_END))
845 return -1;
846
2d4a7167
IM
847 /*
848 * Copy kernel mappings over when needed. This can also
849 * happen within a race in page table update. In the later
850 * case just flush:
851 */
f313e123 852 pgd = pgd_offset(current->active_mm, address);
1da177e4
LT
853 pgd_ref = pgd_offset_k(address);
854 if (pgd_none(*pgd_ref))
855 return -1;
2d4a7167 856
1da177e4
LT
857 if (pgd_none(*pgd))
858 set_pgd(pgd, *pgd_ref);
8c914cb7 859 else
46a82b2d 860 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
1da177e4 861
2d4a7167
IM
862 /*
863 * Below here mismatches are bugs because these lower tables
864 * are shared:
865 */
1da177e4
LT
866
867 pud = pud_offset(pgd, address);
868 pud_ref = pud_offset(pgd_ref, address);
869 if (pud_none(*pud_ref))
870 return -1;
2d4a7167 871
46a82b2d 872 if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref))
1da177e4 873 BUG();
2d4a7167 874
1da177e4
LT
875 pmd = pmd_offset(pud, address);
876 pmd_ref = pmd_offset(pud_ref, address);
877 if (pmd_none(*pmd_ref))
878 return -1;
2d4a7167 879
1da177e4
LT
880 if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
881 BUG();
2d4a7167 882
1da177e4
LT
883 pte_ref = pte_offset_kernel(pmd_ref, address);
884 if (!pte_present(*pte_ref))
885 return -1;
2d4a7167 886
1da177e4 887 pte = pte_offset_kernel(pmd, address);
2d4a7167
IM
888
889 /*
890 * Don't use pte_page here, because the mappings can point
891 * outside mem_map, and the NUMA hash lookup cannot handle
892 * that:
893 */
3b9ba4d5 894 if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
1da177e4 895 BUG();
2d4a7167 896
1da177e4 897 return 0;
fdfe8aa8 898#endif
1da177e4
LT
899}
900
abd4f750 901int show_unhandled_signals = 1;
1da177e4 902
2d4a7167
IM
903static inline int
904access_error(unsigned long error_code, int write, struct vm_area_struct *vma)
92181f19
NP
905{
906 if (write) {
2d4a7167 907 /* write, present and write, not present: */
92181f19
NP
908 if (unlikely(!(vma->vm_flags & VM_WRITE)))
909 return 1;
2d4a7167 910 return 0;
92181f19
NP
911 }
912
2d4a7167
IM
913 /* read, present: */
914 if (unlikely(error_code & PF_PROT))
915 return 1;
916
917 /* read, not present: */
918 if (unlikely(!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))))
919 return 1;
920
92181f19
NP
921 return 0;
922}
923
0973a06c
HS
924static int fault_in_kernel_space(unsigned long address)
925{
926#ifdef CONFIG_X86_32
927 return address >= TASK_SIZE;
2d4a7167 928#else
0973a06c 929 return address >= TASK_SIZE64;
2d4a7167 930#endif
0973a06c
HS
931}
932
1da177e4
LT
933/*
934 * This routine handles page faults. It determines the address,
935 * and the problem, and then passes it off to one of the appropriate
936 * routines.
1da177e4 937 */
f8c2ee22
HH
938#ifdef CONFIG_X86_64
939asmlinkage
940#endif
941void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
1da177e4 942{
2d4a7167 943 struct vm_area_struct *vma;
1da177e4 944 struct task_struct *tsk;
2d4a7167 945 unsigned long address;
1da177e4 946 struct mm_struct *mm;
92181f19 947 int write;
f8c2ee22 948 int fault;
1da177e4 949
a9ba9a3b
AV
950 tsk = current;
951 mm = tsk->mm;
2d4a7167 952
a9ba9a3b
AV
953 prefetchw(&mm->mmap_sem);
954
2d4a7167 955 /* Get the faulting address: */
f51c9452 956 address = read_cr2();
1da177e4 957
0fd0e3da 958 if (unlikely(kmmio_fault(regs, address)))
86069782 959 return;
1da177e4
LT
960
961 /*
962 * We fault-in kernel-space virtual memory on-demand. The
963 * 'reference' page table is init_mm.pgd.
964 *
965 * NOTE! We MUST NOT take any locks for this case. We may
966 * be in an interrupt or a critical region, and should
967 * only copy the information from the master page table,
968 * nothing more.
969 *
970 * This verifies that the fault happens in kernel space
971 * (error_code & 4) == 0, and that the fault was not a
8b1bde93 972 * protection error (error_code & 9) == 0.
1da177e4 973 */
0973a06c 974 if (unlikely(fault_in_kernel_space(address))) {
f8c2ee22
HH
975 if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) &&
976 vmalloc_fault(address) >= 0)
977 return;
5b727a3b 978
2d4a7167 979 /* Can handle a stale RO->RW TLB: */
92181f19 980 if (spurious_fault(error_code, address))
5b727a3b
JF
981 return;
982
2d4a7167 983 /* kprobes don't want to hook the spurious faults: */
9be260a6
MH
984 if (notify_page_fault(regs))
985 return;
f8c2ee22
HH
986 /*
987 * Don't take the mm semaphore here. If we fixup a prefetch
2d4a7167 988 * fault we could otherwise deadlock:
f8c2ee22 989 */
92181f19 990 bad_area_nosemaphore(regs, error_code, address);
2d4a7167 991
92181f19 992 return;
f8c2ee22
HH
993 }
994
2d4a7167 995 /* kprobes don't want to hook the spurious faults: */
f8a6b2b9 996 if (unlikely(notify_page_fault(regs)))
9be260a6 997 return;
f8c2ee22 998 /*
891cffbd
LT
999 * It's safe to allow irq's after cr2 has been saved and the
1000 * vmalloc fault has been handled.
1001 *
1002 * User-mode registers count as a user access even for any
2d4a7167 1003 * potential system fault or CPU buglet:
f8c2ee22 1004 */
891cffbd
LT
1005 if (user_mode_vm(regs)) {
1006 local_irq_enable();
1007 error_code |= PF_USER;
2d4a7167
IM
1008 } else {
1009 if (regs->flags & X86_EFLAGS_IF)
1010 local_irq_enable();
1011 }
8c914cb7 1012
66c58156 1013 if (unlikely(error_code & PF_RSVD))
92181f19 1014 pgtable_bad(regs, error_code, address);
1da177e4
LT
1015
1016 /*
2d4a7167
IM
1017 * If we're in an interrupt, have no user context or are running
1018 * in an atomic region then we must not take the fault:
1da177e4 1019 */
92181f19
NP
1020 if (unlikely(in_atomic() || !mm)) {
1021 bad_area_nosemaphore(regs, error_code, address);
1022 return;
1023 }
1da177e4 1024
3a1dfe6e
IM
1025 /*
1026 * When running in the kernel we expect faults to occur only to
2d4a7167
IM
1027 * addresses in user space. All other faults represent errors in
1028 * the kernel and should generate an OOPS. Unfortunately, in the
1029 * case of an erroneous fault occurring in a code path which already
1030 * holds mmap_sem we will deadlock attempting to validate the fault
1031 * against the address space. Luckily the kernel only validly
1032 * references user space from well defined areas of code, which are
1033 * listed in the exceptions table.
1da177e4
LT
1034 *
1035 * As the vast majority of faults will be valid we will only perform
2d4a7167
IM
1036 * the source reference check when there is a possibility of a
1037 * deadlock. Attempt to lock the address space, if we cannot we then
1038 * validate the source. If this is invalid we can skip the address
1039 * space check, thus avoiding the deadlock:
1da177e4 1040 */
92181f19 1041 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
66c58156 1042 if ((error_code & PF_USER) == 0 &&
92181f19
NP
1043 !search_exception_tables(regs->ip)) {
1044 bad_area_nosemaphore(regs, error_code, address);
1045 return;
1046 }
1da177e4 1047 down_read(&mm->mmap_sem);
01006074
PZ
1048 } else {
1049 /*
2d4a7167
IM
1050 * The above down_read_trylock() might have succeeded in
1051 * which case we'll have missed the might_sleep() from
1052 * down_read():
01006074
PZ
1053 */
1054 might_sleep();
1da177e4
LT
1055 }
1056
1057 vma = find_vma(mm, address);
92181f19
NP
1058 if (unlikely(!vma)) {
1059 bad_area(regs, error_code, address);
1060 return;
1061 }
1062 if (likely(vma->vm_start <= address))
1da177e4 1063 goto good_area;
92181f19
NP
1064 if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) {
1065 bad_area(regs, error_code, address);
1066 return;
1067 }
33cb5243 1068 if (error_code & PF_USER) {
6f4d368e
HH
1069 /*
1070 * Accessing the stack below %sp is always a bug.
1071 * The large cushion allows instructions like enter
2d4a7167 1072 * and pusha to work. ("enter $65535, $31" pushes
6f4d368e 1073 * 32 pointers and then decrements %sp by 65535.)
03fdc2c2 1074 */
92181f19
NP
1075 if (unlikely(address + 65536 + 32 * sizeof(unsigned long) < regs->sp)) {
1076 bad_area(regs, error_code, address);
1077 return;
1078 }
1da177e4 1079 }
92181f19
NP
1080 if (unlikely(expand_stack(vma, address))) {
1081 bad_area(regs, error_code, address);
1082 return;
1083 }
1084
1085 /*
1086 * Ok, we have a good vm_area for this memory access, so
1087 * we can handle it..
1088 */
1da177e4 1089good_area:
92181f19 1090 write = error_code & PF_WRITE;
2d4a7167 1091
92181f19
NP
1092 if (unlikely(access_error(error_code, write, vma))) {
1093 bad_area_access_error(regs, error_code, address);
1094 return;
1da177e4
LT
1095 }
1096
1097 /*
1098 * If for any reason at all we couldn't handle the fault,
1099 * make sure we exit gracefully rather than endlessly redo
2d4a7167 1100 * the fault:
1da177e4 1101 */
83c54070 1102 fault = handle_mm_fault(mm, vma, address, write);
2d4a7167 1103
83c54070 1104 if (unlikely(fault & VM_FAULT_ERROR)) {
92181f19
NP
1105 mm_fault_error(regs, error_code, address, fault);
1106 return;
1da177e4 1107 }
2d4a7167 1108
83c54070
NP
1109 if (fault & VM_FAULT_MAJOR)
1110 tsk->maj_flt++;
1111 else
1112 tsk->min_flt++;
d729ab35 1113
8c938f9f
IM
1114 check_v8086_mode(regs, address, tsk);
1115
1da177e4 1116 up_read(&mm->mmap_sem);
1da177e4 1117}
9e43e1b7 1118
8c914cb7 1119DEFINE_SPINLOCK(pgd_lock);
2bff7383 1120LIST_HEAD(pgd_list);
8c914cb7
JB
1121
1122void vmalloc_sync_all(void)
1123{
1156e098
HH
1124 unsigned long address;
1125
cc643d46 1126#ifdef CONFIG_X86_32
1156e098
HH
1127 if (SHARED_KERNEL_PMD)
1128 return;
1129
cc643d46
JB
1130 for (address = VMALLOC_START & PMD_MASK;
1131 address >= TASK_SIZE && address < FIXADDR_TOP;
1132 address += PMD_SIZE) {
2d4a7167 1133
67350a5c
JF
1134 unsigned long flags;
1135 struct page *page;
1136
1137 spin_lock_irqsave(&pgd_lock, flags);
1138 list_for_each_entry(page, &pgd_list, lru) {
2d4a7167 1139 if (!vmalloc_sync_one(page_address(page), address))
67350a5c 1140 break;
1156e098 1141 }
67350a5c 1142 spin_unlock_irqrestore(&pgd_lock, flags);
1156e098
HH
1143 }
1144#else /* CONFIG_X86_64 */
cc643d46
JB
1145 for (address = VMALLOC_START & PGDIR_MASK; address <= VMALLOC_END;
1146 address += PGDIR_SIZE) {
2d4a7167 1147
67350a5c
JF
1148 const pgd_t *pgd_ref = pgd_offset_k(address);
1149 unsigned long flags;
1150 struct page *page;
1151
1152 if (pgd_none(*pgd_ref))
1153 continue;
2d4a7167 1154
67350a5c
JF
1155 spin_lock_irqsave(&pgd_lock, flags);
1156 list_for_each_entry(page, &pgd_list, lru) {
1157 pgd_t *pgd;
1158 pgd = (pgd_t *)page_address(page) + pgd_index(address);
1159 if (pgd_none(*pgd))
1160 set_pgd(pgd, *pgd_ref);
1161 else
1162 BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref));
8c914cb7 1163 }
67350a5c 1164 spin_unlock_irqrestore(&pgd_lock, flags);
8c914cb7 1165 }
1156e098 1166#endif
8c914cb7 1167}
This page took 0.520197 seconds and 5 git commands to generate.