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