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