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