]> err.no Git - linux-2.6/blob - arch/x86/mm/fault_32.c
bfb0917d699dbc951df6bf964d0cdb1298ccf37c
[linux-2.6] / arch / x86 / mm / fault_32.c
1 /*
2  *  Copyright (C) 1995  Linus Torvalds
3  */
4
5 #include <linux/signal.h>
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/ptrace.h>
12 #include <linux/mman.h>
13 #include <linux/mm.h>
14 #include <linux/smp.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/tty.h>
18 #include <linux/vt_kern.h>              /* For unblank_screen() */
19 #include <linux/highmem.h>
20 #include <linux/bootmem.h>              /* for max_low_pfn */
21 #include <linux/vmalloc.h>
22 #include <linux/module.h>
23 #include <linux/kprobes.h>
24 #include <linux/uaccess.h>
25 #include <linux/kdebug.h>
26
27 #include <asm/system.h>
28 #include <asm/desc.h>
29 #include <asm/segment.h>
30
31 /*
32  * Page fault error code bits
33  *      bit 0 == 0 means no page found, 1 means protection fault
34  *      bit 1 == 0 means read, 1 means write
35  *      bit 2 == 0 means kernel, 1 means user-mode
36  *      bit 3 == 1 means use of reserved bit detected
37  *      bit 4 == 1 means fault was an instruction fetch
38  */
39 #define PF_PROT (1<<0)
40 #define PF_WRITE        (1<<1)
41 #define PF_USER (1<<2)
42 #define PF_RSVD (1<<3)
43 #define PF_INSTR        (1<<4)
44
45 extern void die(const char *, struct pt_regs *, long);
46
47 static inline int notify_page_fault(struct pt_regs *regs)
48 {
49 #ifdef CONFIG_KPROBES
50         int ret = 0;
51
52         /* kprobe_running() needs smp_processor_id() */
53         if (!user_mode_vm(regs)) {
54                 preempt_disable();
55                 if (kprobe_running() && kprobe_fault_handler(regs, 14))
56                         ret = 1;
57                 preempt_enable();
58         }
59
60         return ret;
61 #else
62         return 0;
63 #endif
64 }
65
66 /*
67  * Return EIP plus the CS segment base.  The segment limit is also
68  * adjusted, clamped to the kernel/user address space (whichever is
69  * appropriate), and returned in *eip_limit.
70  *
71  * The segment is checked, because it might have been changed by another
72  * task between the original faulting instruction and here.
73  *
74  * If CS is no longer a valid code segment, or if EIP is beyond the
75  * limit, or if it is a kernel address when CS is not a kernel segment,
76  * then the returned value will be greater than *eip_limit.
77  *
78  * This is slow, but is very rarely executed.
79  */
80 static inline unsigned long get_segment_eip(struct pt_regs *regs,
81                                             unsigned long *eip_limit)
82 {
83         unsigned long ip = regs->ip;
84         unsigned seg = regs->cs & 0xffff;
85         u32 seg_ar, seg_limit, base, *desc;
86
87         /* Unlikely, but must come before segment checks. */
88         if (unlikely(regs->flags & VM_MASK)) {
89                 base = seg << 4;
90                 *eip_limit = base + 0xffff;
91                 return base + (ip & 0xffff);
92         }
93
94         /* The standard kernel/user address space limit. */
95         *eip_limit = user_mode(regs) ? USER_DS.seg : KERNEL_DS.seg;
96
97         /* By far the most common cases. */
98         if (likely(SEGMENT_IS_FLAT_CODE(seg)))
99                 return ip;
100
101         /* Check the segment exists, is within the current LDT/GDT size,
102            that kernel/user (ring 0..3) has the appropriate privilege,
103            that it's a code segment, and get the limit. */
104         __asm__ ("larl %3,%0; lsll %3,%1"
105                  : "=&r" (seg_ar), "=r" (seg_limit) : "0" (0), "rm" (seg));
106         if ((~seg_ar & 0x9800) || ip > seg_limit) {
107                 *eip_limit = 0;
108                 return 1;        /* So that returned ip > *eip_limit. */
109         }
110
111         /* Get the GDT/LDT descriptor base.
112            When you look for races in this code remember that
113            LDT and other horrors are only used in user space. */
114         if (seg & (1<<2)) {
115                 /* Must lock the LDT while reading it. */
116                 mutex_lock(&current->mm->context.lock);
117                 desc = current->mm->context.ldt;
118                 desc = (void *)desc + (seg & ~7);
119         } else {
120                 /* Must disable preemption while reading the GDT. */
121                 desc = (u32 *)get_cpu_gdt_table(get_cpu());
122                 desc = (void *)desc + (seg & ~7);
123         }
124
125         /* Decode the code segment base from the descriptor */
126         base = get_desc_base((struct desc_struct *)desc);
127
128         if (seg & (1<<2))
129                 mutex_unlock(&current->mm->context.lock);
130         else
131                 put_cpu();
132
133         /* Adjust EIP and segment limit, and clamp at the kernel limit.
134            It's legitimate for segments to wrap at 0xffffffff. */
135         seg_limit += base;
136         if (seg_limit < *eip_limit && seg_limit >= base)
137                 *eip_limit = seg_limit;
138         return ip + base;
139 }
140
141 /*
142  * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
143  * Check that here and ignore it.
144  */
145 static int __is_prefetch(struct pt_regs *regs, unsigned long addr)
146 {
147         unsigned long limit;
148         unsigned char *instr = (unsigned char *)get_segment_eip(regs, &limit);
149         int scan_more = 1;
150         int prefetch = 0;
151         int i;
152
153         for (i = 0; scan_more && i < 15; i++) {
154                 unsigned char opcode;
155                 unsigned char instr_hi;
156                 unsigned char instr_lo;
157
158                 if (instr > (unsigned char *)limit)
159                         break;
160                 if (probe_kernel_address(instr, opcode))
161                         break;
162
163                 instr_hi = opcode & 0xf0;
164                 instr_lo = opcode & 0x0f;
165                 instr++;
166
167                 switch (instr_hi) {
168                 case 0x20:
169                 case 0x30:
170                         /*
171                          * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
172                          * In X86_64 long mode, the CPU will signal invalid
173                          * opcode if some of these prefixes are present so
174                          * X86_64 will never get here anyway
175                          */
176                         scan_more = ((instr_lo & 7) == 0x6);
177                         break;
178 #ifdef CONFIG_X86_64
179                 case 0x40:
180                         /*
181                          * In AMD64 long mode 0x40..0x4F are valid REX prefixes
182                          * Need to figure out under what instruction mode the
183                          * instruction was issued. Could check the LDT for lm,
184                          * but for now it's good enough to assume that long
185                          * mode only uses well known segments or kernel.
186                          */
187                         scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
188                         break;
189 #endif
190                 case 0x60:
191                         /* 0x64 thru 0x67 are valid prefixes in all modes. */
192                         scan_more = (instr_lo & 0xC) == 0x4;
193                         break;
194                 case 0xF0:
195                         /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
196                         scan_more = !instr_lo || (instr_lo>>1) == 1;
197                         break;
198                 case 0x00:
199                         /* Prefetch instruction is 0x0F0D or 0x0F18 */
200                         scan_more = 0;
201                         if (instr > (unsigned char *)limit)
202                                 break;
203                         if (probe_kernel_address(instr, opcode))
204                                 break;
205                         prefetch = (instr_lo == 0xF) &&
206                                 (opcode == 0x0D || opcode == 0x18);
207                         break;
208                 default:
209                         scan_more = 0;
210                         break;
211                 }
212         }
213         return prefetch;
214 }
215
216 static inline int is_prefetch(struct pt_regs *regs, unsigned long addr,
217                               unsigned long error_code)
218 {
219         if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
220                      boot_cpu_data.x86 >= 6)) {
221                 /* Catch an obscure case of prefetch inside an NX page. */
222                 if (nx_enabled && (error_code & 16))
223                         return 0;
224                 return __is_prefetch(regs, addr);
225         }
226         return 0;
227 }
228
229 static noinline void force_sig_info_fault(int si_signo, int si_code,
230         unsigned long address, struct task_struct *tsk)
231 {
232         siginfo_t info;
233
234         info.si_signo = si_signo;
235         info.si_errno = 0;
236         info.si_code = si_code;
237         info.si_addr = (void __user *)address;
238         force_sig_info(si_signo, &info, tsk);
239 }
240
241 void do_invalid_op(struct pt_regs *, unsigned long);
242
243 static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address)
244 {
245         unsigned index = pgd_index(address);
246         pgd_t *pgd_k;
247         pud_t *pud, *pud_k;
248         pmd_t *pmd, *pmd_k;
249
250         pgd += index;
251         pgd_k = init_mm.pgd + index;
252
253         if (!pgd_present(*pgd_k))
254                 return NULL;
255
256         /*
257          * set_pgd(pgd, *pgd_k); here would be useless on PAE
258          * and redundant with the set_pmd() on non-PAE. As would
259          * set_pud.
260          */
261
262         pud = pud_offset(pgd, address);
263         pud_k = pud_offset(pgd_k, address);
264         if (!pud_present(*pud_k))
265                 return NULL;
266
267         pmd = pmd_offset(pud, address);
268         pmd_k = pmd_offset(pud_k, address);
269         if (!pmd_present(*pmd_k))
270                 return NULL;
271         if (!pmd_present(*pmd)) {
272                 set_pmd(pmd, *pmd_k);
273                 arch_flush_lazy_mmu_mode();
274         } else
275                 BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k));
276         return pmd_k;
277 }
278
279 /*
280  * Handle a fault on the vmalloc or module mapping area
281  *
282  * This assumes no large pages in there.
283  */
284 static inline int vmalloc_fault(unsigned long address)
285 {
286         unsigned long pgd_paddr;
287         pmd_t *pmd_k;
288         pte_t *pte_k;
289         /*
290          * Synchronize this task's top level page-table
291          * with the 'reference' page table.
292          *
293          * Do _not_ use "current" here. We might be inside
294          * an interrupt in the middle of a task switch..
295          */
296         pgd_paddr = read_cr3();
297         pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
298         if (!pmd_k)
299                 return -1;
300         pte_k = pte_offset_kernel(pmd_k, address);
301         if (!pte_present(*pte_k))
302                 return -1;
303         return 0;
304 }
305
306 int show_unhandled_signals = 1;
307
308 /*
309  * This routine handles page faults.  It determines the address,
310  * and the problem, and then passes it off to one of the appropriate
311  * routines.
312  */
313 void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code)
314 {
315         struct task_struct *tsk;
316         struct mm_struct *mm;
317         struct vm_area_struct *vma;
318         unsigned long address;
319         int write, si_code;
320         int fault;
321
322         /*
323          * We can fault from pretty much anywhere, with unknown IRQ state.
324          */
325         trace_hardirqs_fixup();
326
327         /* get the address */
328         address = read_cr2();
329
330         tsk = current;
331
332         si_code = SEGV_MAPERR;
333
334         /*
335          * We fault-in kernel-space virtual memory on-demand. The
336          * 'reference' page table is init_mm.pgd.
337          *
338          * NOTE! We MUST NOT take any locks for this case. We may
339          * be in an interrupt or a critical region, and should
340          * only copy the information from the master page table,
341          * nothing more.
342          *
343          * This verifies that the fault happens in kernel space
344          * (error_code & 4) == 0, and that the fault was not a
345          * protection error (error_code & 9) == 0.
346          */
347         if (unlikely(address >= TASK_SIZE)) {
348                 if (!(error_code & 0x0000000d) && vmalloc_fault(address) >= 0)
349                         return;
350                 if (notify_page_fault(regs))
351                         return;
352                 /*
353                  * Don't take the mm semaphore here. If we fixup a prefetch
354                  * fault we could otherwise deadlock.
355                  */
356                 goto bad_area_nosemaphore;
357         }
358
359         if (notify_page_fault(regs))
360                 return;
361
362         /* It's safe to allow irq's after cr2 has been saved and the vmalloc
363            fault has been handled. */
364         if (regs->flags & (X86_EFLAGS_IF|VM_MASK))
365                 local_irq_enable();
366
367         mm = tsk->mm;
368
369         /*
370          * If we're in an interrupt, have no user context or are running in an
371          * atomic region then we must not take the fault.
372          */
373         if (in_atomic() || !mm)
374                 goto bad_area_nosemaphore;
375
376         /* When running in the kernel we expect faults to occur only to
377          * addresses in user space.  All other faults represent errors in the
378          * kernel and should generate an OOPS.  Unfortunately, in the case of an
379          * erroneous fault occurring in a code path which already holds mmap_sem
380          * we will deadlock attempting to validate the fault against the
381          * address space.  Luckily the kernel only validly references user
382          * space from well defined areas of code, which are listed in the
383          * exceptions table.
384          *
385          * As the vast majority of faults will be valid we will only perform
386          * the source reference check when there is a possibility of a deadlock.
387          * Attempt to lock the address space, if we cannot we then validate the
388          * source.  If this is invalid we can skip the address space check,
389          * thus avoiding the deadlock.
390          */
391         if (!down_read_trylock(&mm->mmap_sem)) {
392                 if ((error_code & PF_USER) == 0 &&
393                     !search_exception_tables(regs->ip))
394                         goto bad_area_nosemaphore;
395                 down_read(&mm->mmap_sem);
396         }
397
398         vma = find_vma(mm, address);
399         if (!vma)
400                 goto bad_area;
401         if (vma->vm_start <= address)
402                 goto good_area;
403         if (!(vma->vm_flags & VM_GROWSDOWN))
404                 goto bad_area;
405         if (error_code & PF_USER) {
406                 /*
407                  * Accessing the stack below %sp is always a bug.
408                  * The large cushion allows instructions like enter
409                  * and pusha to work.  ("enter $65535,$31" pushes
410                  * 32 pointers and then decrements %sp by 65535.)
411                  */
412                 if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
413                         goto bad_area;
414         }
415         if (expand_stack(vma, address))
416                 goto bad_area;
417 /*
418  * Ok, we have a good vm_area for this memory access, so
419  * we can handle it..
420  */
421 good_area:
422         si_code = SEGV_ACCERR;
423         write = 0;
424         switch (error_code & (PF_PROT|PF_WRITE)) {
425         default:        /* 3: write, present */
426                 /* fall through */
427         case PF_WRITE:          /* write, not present */
428                 if (!(vma->vm_flags & VM_WRITE))
429                         goto bad_area;
430                 write++;
431                 break;
432         case PF_PROT:           /* read, present */
433                 goto bad_area;
434         case 0:                 /* read, not present */
435                 if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
436                         goto bad_area;
437         }
438
439  survive:
440         /*
441          * If for any reason at all we couldn't handle the fault,
442          * make sure we exit gracefully rather than endlessly redo
443          * the fault.
444          */
445         fault = handle_mm_fault(mm, vma, address, write);
446         if (unlikely(fault & VM_FAULT_ERROR)) {
447                 if (fault & VM_FAULT_OOM)
448                         goto out_of_memory;
449                 else if (fault & VM_FAULT_SIGBUS)
450                         goto do_sigbus;
451                 BUG();
452         }
453         if (fault & VM_FAULT_MAJOR)
454                 tsk->maj_flt++;
455         else
456                 tsk->min_flt++;
457
458         /*
459          * Did it hit the DOS screen memory VA from vm86 mode?
460          */
461         if (regs->flags & VM_MASK) {
462                 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
463                 if (bit < 32)
464                         tsk->thread.screen_bitmap |= 1 << bit;
465         }
466         up_read(&mm->mmap_sem);
467         return;
468
469 /*
470  * Something tried to access memory that isn't in our memory map..
471  * Fix it, but check if it's kernel or user first..
472  */
473 bad_area:
474         up_read(&mm->mmap_sem);
475
476 bad_area_nosemaphore:
477         /* User mode accesses just cause a SIGSEGV */
478         if (error_code & PF_USER) {
479                 /*
480                  * It's possible to have interrupts off here.
481                  */
482                 local_irq_enable();
483
484                 /*
485                  * Valid to do another page fault here because this one came
486                  * from user space.
487                  */
488                 if (is_prefetch(regs, address, error_code))
489                         return;
490
491                 if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
492                     printk_ratelimit()) {
493                         printk("%s%s[%d]: segfault at %08lx ip %08lx "
494                             "sp %08lx error %lx\n",
495                             task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
496                             tsk->comm, task_pid_nr(tsk), address, regs->ip,
497                             regs->sp, error_code);
498                 }
499                 tsk->thread.cr2 = address;
500                 /* Kernel addresses are always protection faults */
501                 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
502                 tsk->thread.trap_no = 14;
503                 force_sig_info_fault(SIGSEGV, si_code, address, tsk);
504                 return;
505         }
506
507 #ifdef CONFIG_X86_F00F_BUG
508         /*
509          * Pentium F0 0F C7 C8 bug workaround.
510          */
511         if (boot_cpu_data.f00f_bug) {
512                 unsigned long nr;
513
514                 nr = (address - idt_descr.address) >> 3;
515
516                 if (nr == 6) {
517                         do_invalid_op(regs, 0);
518                         return;
519                 }
520         }
521 #endif
522
523 no_context:
524         /* Are we prepared to handle this kernel fault?  */
525         if (fixup_exception(regs))
526                 return;
527
528         /*
529          * Valid to do another page fault here, because if this fault
530          * had been triggered by is_prefetch fixup_exception would have
531          * handled it.
532          */
533         if (is_prefetch(regs, address, error_code))
534                 return;
535
536 /*
537  * Oops. The kernel tried to access some bad page. We'll have to
538  * terminate things with extreme prejudice.
539  */
540
541         bust_spinlocks(1);
542
543         if (oops_may_print()) {
544                 __typeof__(pte_val(__pte(0))) page;
545
546 #ifdef CONFIG_X86_PAE
547                 if (error_code & 16) {
548                         pte_t *pte = lookup_address(address);
549
550                         if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
551                                 printk(KERN_CRIT "kernel tried to execute "
552                                         "NX-protected page - exploit attempt? "
553                                         "(uid: %d)\n", current->uid);
554                 }
555 #endif
556                 if (address < PAGE_SIZE)
557                         printk(KERN_ALERT "BUG: unable to handle kernel NULL "
558                                         "pointer dereference");
559                 else
560                         printk(KERN_ALERT "BUG: unable to handle kernel paging"
561                                         " request");
562                 printk(" at virtual address %08lx\n", address);
563                 printk(KERN_ALERT "printing ip: %08lx ", regs->ip);
564
565                 page = read_cr3();
566                 page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT];
567 #ifdef CONFIG_X86_PAE
568                 printk("*pdpt = %016Lx ", page);
569                 if ((page >> PAGE_SHIFT) < max_low_pfn
570                     && page & _PAGE_PRESENT) {
571                         page &= PAGE_MASK;
572                         page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT)
573                                                                  & (PTRS_PER_PMD - 1)];
574                         printk(KERN_CONT "*pde = %016Lx ", page);
575                         page &= ~_PAGE_NX;
576                 }
577 #else
578                 printk("*pde = %08lx ", page);
579 #endif
580
581                 /*
582                  * We must not directly access the pte in the highpte
583                  * case if the page table is located in highmem.
584                  * And let's rather not kmap-atomic the pte, just in case
585                  * it's allocated already.
586                  */
587                 if ((page >> PAGE_SHIFT) < max_low_pfn
588                     && (page & _PAGE_PRESENT)
589                     && !(page & _PAGE_PSE)) {
590                         page &= PAGE_MASK;
591                         page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT)
592                                                                  & (PTRS_PER_PTE - 1)];
593                         printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page);
594                 }
595
596                 printk("\n");
597         }
598
599         tsk->thread.cr2 = address;
600         tsk->thread.trap_no = 14;
601         tsk->thread.error_code = error_code;
602         die("Oops", regs, error_code);
603         bust_spinlocks(0);
604         do_exit(SIGKILL);
605
606 /*
607  * We ran out of memory, or some other thing happened to us that made
608  * us unable to handle the page fault gracefully.
609  */
610 out_of_memory:
611         up_read(&mm->mmap_sem);
612         if (is_global_init(tsk)) {
613                 yield();
614                 down_read(&mm->mmap_sem);
615                 goto survive;
616         }
617         printk("VM: killing process %s\n", tsk->comm);
618         if (error_code & 4)
619                 do_group_exit(SIGKILL);
620         goto no_context;
621
622 do_sigbus:
623         up_read(&mm->mmap_sem);
624
625         /* Kernel mode? Handle exceptions or die */
626         if (!(error_code & PF_USER))
627                 goto no_context;
628
629         /* User space => ok to do another page fault */
630         if (is_prefetch(regs, address, error_code))
631                 return;
632
633         tsk->thread.cr2 = address;
634         tsk->thread.error_code = error_code;
635         tsk->thread.trap_no = 14;
636         force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
637 }
638
639 void vmalloc_sync_all(void)
640 {
641         /*
642          * Note that races in the updates of insync and start aren't
643          * problematic: insync can only get set bits added, and updates to
644          * start are only improving performance (without affecting correctness
645          * if undone).
646          */
647         static DECLARE_BITMAP(insync, PTRS_PER_PGD);
648         static unsigned long start = TASK_SIZE;
649         unsigned long address;
650
651         if (SHARED_KERNEL_PMD)
652                 return;
653
654         BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK);
655         for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) {
656                 if (!test_bit(pgd_index(address), insync)) {
657                         unsigned long flags;
658                         struct page *page;
659
660                         spin_lock_irqsave(&pgd_lock, flags);
661                         for (page = pgd_list; page; page =
662                                         (struct page *)page->index)
663                                 if (!vmalloc_sync_one(page_address(page),
664                                                                 address)) {
665                                         BUG_ON(page != pgd_list);
666                                         break;
667                                 }
668                         spin_unlock_irqrestore(&pgd_lock, flags);
669                         if (!page)
670                                 set_bit(pgd_index(address), insync);
671                 }
672                 if (address == start && test_bit(pgd_index(address), insync))
673                         start = address + PGDIR_SIZE;
674         }
675 }