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