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