upgrade to linux 2.6.10-1.12_FC2
[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         pml4_t *pml4;
147         pgd_t *pgd;
148         pmd_t *pmd;
149         pte_t *pte;
150
151         asm("movq %%cr3,%0" : "=r" (pml4));
152
153         pml4 = __va((unsigned long)pml4 & PHYSICAL_PAGE_MASK); 
154         pml4 += pml4_index(address);
155         printk("PML4 %lx ", pml4_val(*pml4));
156         if (bad_address(pml4)) goto bad;
157         if (!pml4_present(*pml4)) goto ret; 
158
159         pgd = __pgd_offset_k((pgd_t *)pml4_page(*pml4), address);
160         if (bad_address(pgd)) goto bad;
161         printk("PGD %lx ", pgd_val(*pgd)); 
162         if (!pgd_present(*pgd)) goto ret;
163
164         pmd = pmd_offset(pgd, 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         /* Warn for strace, but not for gdb */
216         if (!test_ti_thread_flag(tsk->thread_info, TIF_SYSCALL_TRACE) &&
217             (tsk->ptrace & PT_PTRACED))
218                 return 0;
219         return (tsk->sighand->action[sig-1].sa.sa_handler == SIG_IGN) ||
220                 (tsk->sighand->action[sig-1].sa.sa_handler == SIG_DFL);
221 }
222
223 static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
224                                  unsigned long error_code)
225 {
226         oops_begin();
227         printk(KERN_ALERT "%s: Corrupted page table at address %lx\n",
228                current->comm, address);
229         dump_pagetable(address);
230         __die("Bad pagetable", regs, error_code);
231         oops_end();
232         do_exit(SIGKILL);
233 }
234
235 int page_fault_trace; 
236 int exception_trace = 1;
237
238 /*
239  * This routine handles page faults.  It determines the address,
240  * and the problem, and then passes it off to one of the appropriate
241  * routines.
242  *
243  * error_code:
244  *      bit 0 == 0 means no page found, 1 means protection fault
245  *      bit 1 == 0 means read, 1 means write
246  *      bit 2 == 0 means kernel, 1 means user-mode
247  *      bit 3 == 1 means fault was an instruction fetch
248  */
249 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)
250 {
251         struct task_struct *tsk;
252         struct mm_struct *mm;
253         struct vm_area_struct * vma;
254         unsigned long address;
255         const struct exception_table_entry *fixup;
256         int write;
257         siginfo_t info;
258
259 #ifdef CONFIG_CHECKING
260         { 
261                 unsigned long gs; 
262                 struct x8664_pda *pda = cpu_pda + stack_smp_processor_id(); 
263                 rdmsrl(MSR_GS_BASE, gs); 
264                 if (gs != (unsigned long)pda) { 
265                         wrmsrl(MSR_GS_BASE, pda); 
266                         printk("page_fault: wrong gs %lx expected %p\n", gs, pda);
267                 }
268         }
269 #endif
270
271         /* get the address */
272         __asm__("movq %%cr2,%0":"=r" (address));
273         if (notify_die(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
274                                         SIGSEGV) == NOTIFY_STOP)
275                 return;
276
277         if (likely(regs->eflags & X86_EFLAGS_IF))
278                 local_irq_enable();
279
280         if (unlikely(page_fault_trace))
281                 printk("pagefault rip:%lx rsp:%lx cs:%lu ss:%lu address %lx error %lx\n",
282                        regs->rip,regs->rsp,regs->cs,regs->ss,address,error_code); 
283
284         tsk = current;
285         mm = tsk->mm;
286         info.si_code = SEGV_MAPERR;
287
288
289         /*
290          * We fault-in kernel-space virtual memory on-demand. The
291          * 'reference' page table is init_mm.pgd.
292          *
293          * NOTE! We MUST NOT take any locks for this case. We may
294          * be in an interrupt or a critical region, and should
295          * only copy the information from the master page table,
296          * nothing more.
297          *
298          * This verifies that the fault happens in kernel space
299          * (error_code & 4) == 0, and that the fault was not a
300          * protection error (error_code & 1) == 0.
301          */
302         if (unlikely(address >= TASK_SIZE)) {
303                 if (!(error_code & 5))
304                         goto vmalloc_fault;
305                 /*
306                  * Don't take the mm semaphore here. If we fixup a prefetch
307                  * fault we could otherwise deadlock.
308                  */
309                 goto bad_area_nosemaphore;
310         }
311
312         if (unlikely(error_code & (1 << 3)))
313                 goto page_table_corruption;
314
315         /*
316          * If we're in an interrupt or have no user
317          * context, we must not take the fault..
318          */
319         if (unlikely(in_atomic() || !mm))
320                 goto bad_area_nosemaphore;
321
322  again:
323         /* When running in the kernel we expect faults to occur only to
324          * addresses in user space.  All other faults represent errors in the
325          * kernel and should generate an OOPS.  Unfortunatly, in the case of an
326          * erroneous fault occuring in a code path which already holds mmap_sem
327          * we will deadlock attempting to validate the fault against the
328          * address space.  Luckily the kernel only validly references user
329          * space from well defined areas of code, which are listed in the
330          * exceptions table.
331          *
332          * As the vast majority of faults will be valid we will only perform
333          * the source reference check when there is a possibilty of a deadlock.
334          * Attempt to lock the address space, if we cannot we then validate the
335          * source.  If this is invalid we can skip the address space check,
336          * thus avoiding the deadlock.
337          */
338         if (!down_read_trylock(&mm->mmap_sem)) {
339                 if ((error_code & 4) == 0 &&
340                     !search_exception_tables(regs->rip))
341                         goto bad_area_nosemaphore;
342                 down_read(&mm->mmap_sem);
343         }
344
345         vma = find_vma(mm, address);
346         if (!vma)
347                 goto bad_area;
348         if (likely(vma->vm_start <= address))
349                 goto good_area;
350         if (!(vma->vm_flags & VM_GROWSDOWN))
351                 goto bad_area;
352         if (error_code & 4) {
353                 // XXX: align red zone size with ABI 
354                 if (address + 128 < regs->rsp)
355                         goto bad_area;
356         }
357         if (expand_stack(vma, address))
358                 goto bad_area;
359 /*
360  * Ok, we have a good vm_area for this memory access, so
361  * we can handle it..
362  */
363 good_area:
364         info.si_code = SEGV_ACCERR;
365         write = 0;
366         switch (error_code & 3) {
367                 default:        /* 3: write, present */
368                         /* fall through */
369                 case 2:         /* write, not present */
370                         if (!(vma->vm_flags & VM_WRITE))
371                                 goto bad_area;
372                         write++;
373                         break;
374                 case 1:         /* read, present */
375                         goto bad_area;
376                 case 0:         /* read, not present */
377                         if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
378                                 goto bad_area;
379         }
380
381         /*
382          * If for any reason at all we couldn't handle the fault,
383          * make sure we exit gracefully rather than endlessly redo
384          * the fault.
385          */
386         switch (handle_mm_fault(mm, vma, address, write)) {
387         case 1:
388                 tsk->min_flt++;
389                 break;
390         case 2:
391                 tsk->maj_flt++;
392                 break;
393         case 0:
394                 goto do_sigbus;
395         default:
396                 goto out_of_memory;
397         }
398
399         up_read(&mm->mmap_sem);
400         return;
401
402 /*
403  * Something tried to access memory that isn't in our memory map..
404  * Fix it, but check if it's kernel or user first..
405  */
406 bad_area:
407         up_read(&mm->mmap_sem);
408
409 bad_area_nosemaphore:
410
411 #ifdef CONFIG_IA32_EMULATION
412         /* 32bit vsyscall. map on demand. */
413         if (test_thread_flag(TIF_IA32) &&
414             address >= VSYSCALL32_BASE && address < VSYSCALL32_END) {
415                 if (map_syscall32(mm, address) < 0)
416                         goto out_of_memory2;
417                 return;
418         }
419 #endif
420
421         /* User mode accesses just cause a SIGSEGV */
422         if (error_code & 4) {
423                 if (is_prefetch(regs, address, error_code))
424                         return;
425
426                 /* Work around K8 erratum #100 K8 in compat mode
427                    occasionally jumps to illegal addresses >4GB.  We
428                    catch this here in the page fault handler because
429                    these addresses are not reachable. Just detect this
430                    case and return.  Any code segment in LDT is
431                    compatibility mode. */
432                 if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
433                     (address >> 32))
434                         return;
435
436                 if (exception_trace && unhandled_signal(tsk, SIGSEGV)) {
437                         printk(KERN_INFO
438                        "%s[%d]: segfault at %016lx rip %016lx rsp %016lx error %lx\n",
439                                         tsk->comm, tsk->pid, address, regs->rip,
440                                         regs->rsp, error_code);
441                 }
442        
443                 tsk->thread.cr2 = address;
444                 /* Kernel addresses are always protection faults */
445                 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
446                 tsk->thread.trap_no = 14;
447                 info.si_signo = SIGSEGV;
448                 info.si_errno = 0;
449                 /* info.si_code has been set above */
450                 info.si_addr = (void __user *)address;
451                 force_sig_info(SIGSEGV, &info, tsk);
452                 return;
453         }
454
455 no_context:
456         
457         /* Are we prepared to handle this kernel fault?  */
458         fixup = search_exception_tables(regs->rip);
459         if (fixup) {
460                 regs->rip = fixup->fixup;
461                 return;
462         }
463
464         /* 
465          * Hall of shame of CPU/BIOS bugs.
466          */
467
468         if (is_prefetch(regs, address, error_code))
469                 return;
470
471         if (is_errata93(regs, address))
472                 return; 
473
474 /*
475  * Oops. The kernel tried to access some bad page. We'll have to
476  * terminate things with extreme prejudice.
477  */
478
479         oops_begin(); 
480
481         if (address < PAGE_SIZE)
482                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
483         else
484                 printk(KERN_ALERT "Unable to handle kernel paging request");
485         printk(" at %016lx RIP: \n" KERN_ALERT,address);
486         printk_address(regs->rip);
487         printk("\n");
488         dump_pagetable(address);
489         __die("Oops", regs, error_code);
490         /* Executive summary in case the body of the oops scrolled away */
491         printk(KERN_EMERG "CR2: %016lx\n", address);
492         try_crashdump(regs);
493         oops_end(); 
494         do_exit(SIGKILL);
495
496 /*
497  * We ran out of memory, or some other thing happened to us that made
498  * us unable to handle the page fault gracefully.
499  */
500 out_of_memory:
501         up_read(&mm->mmap_sem);
502 out_of_memory2:
503         if (current->pid == 1) { 
504                 yield();
505                 goto again;
506         }
507         printk("VM: killing process %s\n", tsk->comm);
508         if (error_code & 4)
509                 do_exit(SIGKILL);
510         goto no_context;
511
512 do_sigbus:
513         up_read(&mm->mmap_sem);
514
515         /* Kernel mode? Handle exceptions or die */
516         if (!(error_code & 4))
517                 goto no_context;
518
519         tsk->thread.cr2 = address;
520         tsk->thread.error_code = error_code;
521         tsk->thread.trap_no = 14;
522         info.si_signo = SIGBUS;
523         info.si_errno = 0;
524         info.si_code = BUS_ADRERR;
525         info.si_addr = (void __user *)address;
526         force_sig_info(SIGBUS, &info, tsk);
527         return;
528
529 vmalloc_fault:
530         {
531                 pgd_t *pgd;
532                 pmd_t *pmd;
533                 pte_t *pte; 
534
535                 /*
536                  * x86-64 has the same kernel 3rd level pages for all CPUs.
537                  * But for vmalloc/modules the TLB synchronization works lazily,
538                  * so it can happen that we get a page fault for something
539                  * that is really already in the page table. Just check if it
540                  * is really there and when yes flush the local TLB. 
541                  */
542                 pgd = pgd_offset_k(address);
543                 if (!pgd_present(*pgd))
544                         goto bad_area_nosemaphore;
545                 pmd = pmd_offset(pgd, address);
546                 if (!pmd_present(*pmd))
547                         goto bad_area_nosemaphore;
548                 pte = pte_offset_kernel(pmd, address); 
549                 if (!pte_present(*pte))
550                         goto bad_area_nosemaphore;
551
552                 __flush_tlb_all();              
553                 return;
554         }
555
556 page_table_corruption:
557         pgtable_bad(address, regs, error_code);
558 }