vserver 2.0-rc4
[linux-2.6.git] / arch / x86_64 / mm / fault.c
1 /*
2  *  linux/arch/x86-64/mm/fault.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  *  Copyright (C) 2001,2002 Andi Kleen, SuSE Labs.
6  */
7
8 #include <linux/config.h>
9 #include <linux/signal.h>
10 #include <linux/sched.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/string.h>
14 #include <linux/types.h>
15 #include <linux/ptrace.h>
16 #include <linux/mman.h>
17 #include <linux/mm.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/interrupt.h>
21 #include <linux/init.h>
22 #include <linux/tty.h>
23 #include <linux/vt_kern.h>              /* For unblank_screen() */
24 #include <linux/compiler.h>
25 #include <linux/module.h>
26 #include <linux/kprobes.h>
27
28 #include <asm/system.h>
29 #include <asm/uaccess.h>
30 #include <asm/pgalloc.h>
31 #include <asm/smp.h>
32 #include <asm/tlbflush.h>
33 #include <asm/proto.h>
34 #include <asm/kdebug.h>
35 #include <asm-generic/sections.h>
36 #include <asm/kdebug.h>
37
38 void bust_spinlocks(int yes)
39 {
40         int loglevel_save = console_loglevel;
41         if (yes) {
42                 oops_in_progress = 1;
43         } else {
44 #ifdef CONFIG_VT
45                 unblank_screen();
46 #endif
47                 oops_in_progress = 0;
48                 /*
49                  * OK, the message is on the console.  Now we call printk()
50                  * without oops_in_progress set so that printk will give klogd
51                  * a poke.  Hold onto your hats...
52                  */
53                 console_loglevel = 15;          /* NMI oopser may have shut the console up */
54                 printk(" ");
55                 console_loglevel = loglevel_save;
56         }
57 }
58
59 /* Sometimes the CPU reports invalid exceptions on prefetch.
60    Check that here and ignore.
61    Opcode checker based on code by Richard Brunner */
62 static noinline int is_prefetch(struct pt_regs *regs, unsigned long addr,
63                                 unsigned long error_code)
64
65         unsigned char *instr = (unsigned char *)(regs->rip);
66         int scan_more = 1;
67         int prefetch = 0; 
68         unsigned char *max_instr = instr + 15;
69
70         /* If it was a exec fault ignore */
71         if (error_code & (1<<4))
72                 return 0;
73         
74         /* Code segments in LDT could have a non zero base. Don't check
75            when that's possible */
76         if (regs->cs & (1<<2))
77                 return 0;
78
79         if ((regs->cs & 3) != 0 && regs->rip >= TASK_SIZE)
80                 return 0;
81
82         while (scan_more && instr < max_instr) { 
83                 unsigned char opcode;
84                 unsigned char instr_hi;
85                 unsigned char instr_lo;
86
87                 if (__get_user(opcode, instr))
88                         break; 
89
90                 instr_hi = opcode & 0xf0; 
91                 instr_lo = opcode & 0x0f; 
92                 instr++;
93
94                 switch (instr_hi) { 
95                 case 0x20:
96                 case 0x30:
97                         /* Values 0x26,0x2E,0x36,0x3E are valid x86
98                            prefixes.  In long mode, the CPU will signal
99                            invalid opcode if some of these prefixes are
100                            present so we will never get here anyway */
101                         scan_more = ((instr_lo & 7) == 0x6);
102                         break;
103                         
104                 case 0x40:
105                         /* In AMD64 long mode, 0x40 to 0x4F are valid REX prefixes
106                            Need to figure out under what instruction mode the
107                            instruction was issued ... */
108                         /* Could check the LDT for lm, but for now it's good
109                            enough to assume that long mode only uses well known
110                            segments or kernel. */
111                         scan_more = ((regs->cs & 3) == 0) || (regs->cs == __USER_CS);
112                         break;
113                         
114                 case 0x60:
115                         /* 0x64 thru 0x67 are valid prefixes in all modes. */
116                         scan_more = (instr_lo & 0xC) == 0x4;
117                         break;          
118                 case 0xF0:
119                         /* 0xF0, 0xF2, and 0xF3 are valid prefixes in all modes. */
120                         scan_more = !instr_lo || (instr_lo>>1) == 1;
121                         break;                  
122                 case 0x00:
123                         /* Prefetch instruction is 0x0F0D or 0x0F18 */
124                         scan_more = 0;
125                         if (__get_user(opcode, instr)) 
126                                 break;
127                         prefetch = (instr_lo == 0xF) &&
128                                 (opcode == 0x0D || opcode == 0x18);
129                         break;                  
130                 default:
131                         scan_more = 0;
132                         break;
133                 } 
134         }
135         return prefetch;
136 }
137
138 static int bad_address(void *p) 
139
140         unsigned long dummy;
141         return __get_user(dummy, (unsigned long *)p);
142
143
144 void dump_pagetable(unsigned long address)
145 {
146         pgd_t *pgd;
147         pud_t *pud;
148         pmd_t *pmd;
149         pte_t *pte;
150
151         asm("movq %%cr3,%0" : "=r" (pgd));
152
153         pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK); 
154         pgd += pgd_index(address);
155         printk("PGD %lx ", pgd_val(*pgd));
156         if (bad_address(pgd)) goto bad;
157         if (!pgd_present(*pgd)) goto ret; 
158
159         pud = __pud_offset_k((pud_t *)pgd_page(*pgd), address);
160         if (bad_address(pud)) goto bad;
161         printk("PUD %lx ", pud_val(*pud));
162         if (!pud_present(*pud)) goto ret;
163
164         pmd = pmd_offset(pud, address);
165         if (bad_address(pmd)) goto bad;
166         printk("PMD %lx ", pmd_val(*pmd));
167         if (!pmd_present(*pmd)) goto ret;        
168
169         pte = pte_offset_kernel(pmd, address);
170         if (bad_address(pte)) goto bad;
171         printk("PTE %lx", pte_val(*pte)); 
172 ret:
173         printk("\n");
174         return;
175 bad:
176         printk("BAD\n");
177 }
178
179 static const char errata93_warning[] = 
180 KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
181 KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
182 KERN_ERR "******* Please consider a BIOS update.\n"
183 KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
184
185 /* Workaround for K8 erratum #93 & buggy BIOS.
186    BIOS SMM functions are required to use a specific workaround
187    to avoid corruption of the 64bit RIP register on C stepping K8. 
188    A lot of BIOS that didn't get tested properly miss this. 
189    The OS sees this as a page fault with the upper 32bits of RIP cleared.
190    Try to work around it here.
191    Note we only handle faults in kernel here. */
192
193 static int is_errata93(struct pt_regs *regs, unsigned long address) 
194 {
195         static int warned;
196         if (address != regs->rip)
197                 return 0;
198         if ((address >> 32) != 0) 
199                 return 0;
200         address |= 0xffffffffUL << 32;
201         if ((address >= (u64)_stext && address <= (u64)_etext) || 
202             (address >= MODULES_VADDR && address <= MODULES_END)) { 
203                 if (!warned) {
204                         printk(errata93_warning);               
205                         warned = 1;
206                 }
207                 regs->rip = address;
208                 return 1;
209         }
210         return 0;
211
212
213 int unhandled_signal(struct task_struct *tsk, int sig)
214 {
215         if (tsk->pid == 1)
216                 return 1;
217         /* Warn for strace, but not for gdb */
218         if (!test_ti_thread_flag(tsk->thread_info, TIF_SYSCALL_TRACE) &&
219             (tsk->ptrace & PT_PTRACED))
220                 return 0;
221         return (tsk->sighand->action[sig-1].sa.sa_handler == SIG_IGN) ||
222                 (tsk->sighand->action[sig-1].sa.sa_handler == SIG_DFL);
223 }
224
225 static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
226                                  unsigned long error_code)
227 {
228         oops_begin();
229         printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
230                current->comm, address);
231         dump_pagetable(address);
232         __die("Bad pagetable", regs, error_code);
233         oops_end();
234         do_exit(SIGKILL);
235 }
236
237 /*
238  * Handle a fault on the vmalloc or module mapping area
239  *
240  * This assumes no large pages in there.
241  */
242 static int vmalloc_fault(unsigned long address)
243 {
244         pgd_t *pgd, *pgd_ref;
245         pud_t *pud, *pud_ref;
246         pmd_t *pmd, *pmd_ref;
247         pte_t *pte, *pte_ref;
248
249         /* Copy kernel mappings over when needed. This can also
250            happen within a race in page table update. In the later
251            case just flush. */
252
253         pgd = pgd_offset(current->mm ?: &init_mm, address);
254         pgd_ref = pgd_offset_k(address);
255         if (pgd_none(*pgd_ref))
256                 return -1;
257         if (pgd_none(*pgd))
258                 set_pgd(pgd, *pgd_ref);
259
260         /* Below here mismatches are bugs because these lower tables
261            are shared */
262
263         pud = pud_offset(pgd, address);
264         pud_ref = pud_offset(pgd_ref, address);
265         if (pud_none(*pud_ref))
266                 return -1;
267         if (pud_none(*pud) || pud_page(*pud) != pud_page(*pud_ref))
268                 BUG();
269         pmd = pmd_offset(pud, address);
270         pmd_ref = pmd_offset(pud_ref, address);
271         if (pmd_none(*pmd_ref))
272                 return -1;
273         if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref))
274                 BUG();
275         pte_ref = pte_offset_kernel(pmd_ref, address);
276         if (!pte_present(*pte_ref))
277                 return -1;
278         pte = pte_offset_kernel(pmd, address);
279         /* Don't use pte_page here, because the mappings can point
280            outside mem_map, and the NUMA hash lookup cannot handle
281            that. */
282         if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
283                 BUG();
284         __flush_tlb_all();
285         return 0;
286 }
287
288 int page_fault_trace = 0;
289 int exception_trace = 1;
290
291 /*
292  * This routine handles page faults.  It determines the address,
293  * and the problem, and then passes it off to one of the appropriate
294  * routines.
295  *
296  * error_code:
297  *      bit 0 == 0 means no page found, 1 means protection fault
298  *      bit 1 == 0 means read, 1 means write
299  *      bit 2 == 0 means kernel, 1 means user-mode
300  *      bit 3 == 1 means fault was an instruction fetch
301  */
302 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)
303 {
304         struct task_struct *tsk;
305         struct mm_struct *mm;
306         struct vm_area_struct * vma;
307         unsigned long address;
308         const struct exception_table_entry *fixup;
309         int write;
310         siginfo_t info;
311
312 #ifdef CONFIG_CHECKING
313         { 
314                 unsigned long gs; 
315                 struct x8664_pda *pda = cpu_pda + stack_smp_processor_id(); 
316                 rdmsrl(MSR_GS_BASE, gs); 
317                 if (gs != (unsigned long)pda) { 
318                         wrmsrl(MSR_GS_BASE, pda); 
319                         printk("page_fault: wrong gs %lx expected %p\n", gs, pda);
320                 }
321         }
322 #endif
323
324         /* get the address */
325         __asm__("movq %%cr2,%0":"=r" (address));
326         if (notify_die(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
327                                         SIGSEGV) == NOTIFY_STOP)
328                 return;
329
330         if (likely(regs->eflags & X86_EFLAGS_IF))
331                 local_irq_enable();
332
333         if (unlikely(page_fault_trace))
334                 printk("pagefault rip:%lx rsp:%lx cs:%lu ss:%lu address %lx error %lx\n",
335                        regs->rip,regs->rsp,regs->cs,regs->ss,address,error_code); 
336
337         tsk = current;
338         mm = tsk->mm;
339         info.si_code = SEGV_MAPERR;
340
341
342         /*
343          * We fault-in kernel-space virtual memory on-demand. The
344          * 'reference' page table is init_mm.pgd.
345          *
346          * NOTE! We MUST NOT take any locks for this case. We may
347          * be in an interrupt or a critical region, and should
348          * only copy the information from the master page table,
349          * nothing more.
350          *
351          * This verifies that the fault happens in kernel space
352          * (error_code & 4) == 0, and that the fault was not a
353          * protection error (error_code & 1) == 0.
354          */
355         if (unlikely(address >= TASK_SIZE)) {
356                 if (!(error_code & 5) &&
357                       ((address >= VMALLOC_START && address < VMALLOC_END) ||
358                        (address >= MODULES_VADDR && address < MODULES_END))) {
359                         if (vmalloc_fault(address) < 0)
360                                 goto bad_area_nosemaphore;
361                         return;
362                 }
363                 /*
364                  * Don't take the mm semaphore here. If we fixup a prefetch
365                  * fault we could otherwise deadlock.
366                  */
367                 goto bad_area_nosemaphore;
368         }
369
370         if (unlikely(error_code & (1 << 3)))
371                 pgtable_bad(address, regs, error_code);
372
373         /*
374          * If we're in an interrupt or have no user
375          * context, we must not take the fault..
376          */
377         if (unlikely(in_atomic() || !mm))
378                 goto bad_area_nosemaphore;
379
380  again:
381         /* When running in the kernel we expect faults to occur only to
382          * addresses in user space.  All other faults represent errors in the
383          * kernel and should generate an OOPS.  Unfortunatly, in the case of an
384          * erroneous fault occuring in a code path which already holds mmap_sem
385          * we will deadlock attempting to validate the fault against the
386          * address space.  Luckily the kernel only validly references user
387          * space from well defined areas of code, which are listed in the
388          * exceptions table.
389          *
390          * As the vast majority of faults will be valid we will only perform
391          * the source reference check when there is a possibilty of a deadlock.
392          * Attempt to lock the address space, if we cannot we then validate the
393          * source.  If this is invalid we can skip the address space check,
394          * thus avoiding the deadlock.
395          */
396         if (!down_read_trylock(&mm->mmap_sem)) {
397                 if ((error_code & 4) == 0 &&
398                     !search_exception_tables(regs->rip))
399                         goto bad_area_nosemaphore;
400                 down_read(&mm->mmap_sem);
401         }
402
403         vma = find_vma(mm, address);
404         if (!vma)
405                 goto bad_area;
406         if (likely(vma->vm_start <= address))
407                 goto good_area;
408         if (!(vma->vm_flags & VM_GROWSDOWN))
409                 goto bad_area;
410         if (error_code & 4) {
411                 // XXX: align red zone size with ABI 
412                 if (address + 128 < regs->rsp)
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         info.si_code = SEGV_ACCERR;
423         write = 0;
424         switch (error_code & 3) {
425                 default:        /* 3: write, present */
426                         /* fall through */
427                 case 2:         /* write, not present */
428                         if (!(vma->vm_flags & VM_WRITE))
429                                 goto bad_area;
430                         write++;
431                         break;
432                 case 1:         /* read, present */
433                         goto bad_area;
434                 case 0:         /* read, not present */
435                         if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
436                                 goto bad_area;
437         }
438
439         /*
440          * If for any reason at all we couldn't handle the fault,
441          * make sure we exit gracefully rather than endlessly redo
442          * the fault.
443          */
444         switch (handle_mm_fault(mm, vma, address, write)) {
445         case 1:
446                 tsk->min_flt++;
447                 break;
448         case 2:
449                 tsk->maj_flt++;
450                 break;
451         case 0:
452                 goto do_sigbus;
453         default:
454                 goto out_of_memory;
455         }
456
457         up_read(&mm->mmap_sem);
458         return;
459
460 /*
461  * Something tried to access memory that isn't in our memory map..
462  * Fix it, but check if it's kernel or user first..
463  */
464 bad_area:
465         up_read(&mm->mmap_sem);
466
467 bad_area_nosemaphore:
468
469 #ifdef CONFIG_IA32_EMULATION
470         /* 32bit vsyscall. map on demand. */
471         if (test_thread_flag(TIF_IA32) &&
472             address >= VSYSCALL32_BASE && address < VSYSCALL32_END) {
473                 if (map_syscall32(mm, address) < 0)
474                         goto out_of_memory2;
475                 return;
476         }
477 #endif
478
479         /* User mode accesses just cause a SIGSEGV */
480         if (error_code & 4) {
481                 if (is_prefetch(regs, address, error_code))
482                         return;
483
484                 /* Work around K8 erratum #100 K8 in compat mode
485                    occasionally jumps to illegal addresses >4GB.  We
486                    catch this here in the page fault handler because
487                    these addresses are not reachable. Just detect this
488                    case and return.  Any code segment in LDT is
489                    compatibility mode. */
490                 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
491                     (address >> 32))
492                         return;
493
494                 if (exception_trace && unhandled_signal(tsk, SIGSEGV)) {
495                         printk(
496                        "%s%s[%d]: segfault at %016lx rip %016lx rsp %016lx error %lx\n",
497                                         tsk->pid > 1 ? KERN_INFO : KERN_EMERG,
498                                         tsk->comm, tsk->pid, address, regs->rip,
499                                         regs->rsp, error_code);
500                 }
501        
502                 tsk->thread.cr2 = address;
503                 /* Kernel addresses are always protection faults */
504                 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
505                 tsk->thread.trap_no = 14;
506                 info.si_signo = SIGSEGV;
507                 info.si_errno = 0;
508                 /* info.si_code has been set above */
509                 info.si_addr = (void __user *)address;
510                 force_sig_info(SIGSEGV, &info, tsk);
511                 return;
512         }
513
514 no_context:
515         
516         /* Are we prepared to handle this kernel fault?  */
517         fixup = search_exception_tables(regs->rip);
518         if (fixup) {
519                 regs->rip = fixup->fixup;
520                 return;
521         }
522
523         /* 
524          * Hall of shame of CPU/BIOS bugs.
525          */
526
527         if (is_prefetch(regs, address, error_code))
528                 return;
529
530         if (is_errata93(regs, address))
531                 return; 
532
533 /*
534  * Oops. The kernel tried to access some bad page. We'll have to
535  * terminate things with extreme prejudice.
536  */
537
538         oops_begin(); 
539
540         if (address < PAGE_SIZE)
541                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
542         else
543                 printk(KERN_ALERT "Unable to handle kernel paging request");
544         printk(" at %016lx RIP: \n" KERN_ALERT,address);
545         printk_address(regs->rip);
546         printk("\n");
547         dump_pagetable(address);
548         __die("Oops", regs, error_code);
549         /* Executive summary in case the body of the oops scrolled away */
550         printk(KERN_EMERG "CR2: %016lx\n", address);
551         oops_end(); 
552         do_exit(SIGKILL);
553
554 /*
555  * We ran out of memory, or some other thing happened to us that made
556  * us unable to handle the page fault gracefully.
557  */
558 out_of_memory:
559         up_read(&mm->mmap_sem);
560 out_of_memory2:
561         if (current->pid == 1) { 
562                 yield();
563                 goto again;
564         }
565         printk("VM: killing process %s\n", tsk->comm);
566         if (error_code & 4)
567                 do_exit(SIGKILL);
568         goto no_context;
569
570 do_sigbus:
571         up_read(&mm->mmap_sem);
572
573         /* Kernel mode? Handle exceptions or die */
574         if (!(error_code & 4))
575                 goto no_context;
576
577         tsk->thread.cr2 = address;
578         tsk->thread.error_code = error_code;
579         tsk->thread.trap_no = 14;
580         info.si_signo = SIGBUS;
581         info.si_errno = 0;
582         info.si_code = BUS_ADRERR;
583         info.si_addr = (void __user *)address;
584         force_sig_info(SIGBUS, &info, tsk);
585         return;
586 }