import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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/signal.h>
9 #include <linux/sched.h>
10 #include <linux/kernel.h>
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
14 #include <linux/ptrace.h>
15 #include <linux/mman.h>
16 #include <linux/mm.h>
17 #include <linux/smp.h>
18 #include <linux/smp_lock.h>
19 #include <linux/interrupt.h>
20 #include <linux/init.h>
21 #include <linux/tty.h>
22 #include <linux/vt_kern.h>              /* For unblank_screen() */
23 #include <linux/compiler.h>
24
25 #include <asm/system.h>
26 #include <asm/uaccess.h>
27 #include <asm/pgalloc.h>
28 #include <asm/hardirq.h>
29 #include <asm/smp.h>
30 #include <asm/proto.h>
31 #include <asm/kdebug.h>
32
33 extern spinlock_t console_lock, timerlist_lock;
34
35 void bust_spinlocks(int yes)
36 {
37         spin_lock_init(&timerlist_lock);
38         if (yes) {
39                 oops_in_progress = 1;
40 #ifdef CONFIG_SMP
41                 global_irq_lock = 0;    /* Many serial drivers do __global_cli() */
42 #endif
43         } else {
44         int loglevel_save = console_loglevel;
45 #ifdef CONFIG_VT
46                 unblank_screen();
47 #endif
48                 oops_in_progress = 0;
49                 /*
50                  * OK, the message is on the console.  Now we call printk()
51                  * without oops_in_progress set so that printk will give klogd
52                  * a poke.  Hold onto your hats...
53                  */
54                 console_loglevel = 15;          /* NMI oopser may have shut the console up */
55                 printk(" ");
56                 console_loglevel = loglevel_save;
57         }
58 }
59
60 static int bad_address(void *p) 
61
62         unsigned long dummy;
63         return __get_user(dummy, (unsigned long *)p);
64
65
66 void dump_pagetable(unsigned long address)
67 {
68         pml4_t *pml4;
69         asm("movq %%cr3,%0" : "=r" (pml4));
70
71         pml4 = __va((unsigned long)pml4 & PHYSICAL_PAGE_MASK); 
72         pml4 += pml4_index(address);
73         printk("PML4 %lx ", pml4_val(*pml4));
74         if (bad_address(pml4)) goto bad;
75         if (!pml4_present(*pml4)) goto ret; 
76
77         pgd_t *pgd = __pgd_offset_k((pgd_t *)pml4_page(*pml4), address); 
78         if (bad_address(pgd)) goto bad;
79         printk("PGD %lx ", pgd_val(*pgd)); 
80         if (!pgd_present(*pgd)) goto ret;
81
82         pmd_t *pmd = pmd_offset(pgd, address); 
83         if (bad_address(pmd)) goto bad;
84         printk("PMD %lx ", pmd_val(*pmd));
85         if (!pmd_present(*pmd)) goto ret;        
86
87         pte_t *pte = pte_offset(pmd, address);
88         if (bad_address(pte)) goto bad;
89         printk("PTE %lx", pte_val(*pte)); 
90 ret:
91         printk("\n");
92         return;
93 bad:
94         printk("BAD\n");
95 }
96
97 /* Sometimes the CPU reports invalid exceptions on prefetch.
98    Check that here and ignore.
99    Opcode checker based on code by Richard Brunner */
100 static int is_prefetch(struct pt_regs *regs, unsigned long addr)
101
102         unsigned char *instr = (unsigned char *)(regs->rip);
103         int scan_more = 1;
104         int prefetch = 0; 
105         unsigned char *max_instr = instr + 15;
106
107         /* Avoid recursive faults for this common case */
108         if (regs->rip == addr)
109                 return 0; 
110
111         if (regs->cs & (1<<2))
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 (__get_user(opcode, instr))
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                         /* Values 0x26,0x2E,0x36,0x3E are valid x86
130                            prefixes.  In long mode, the CPU will signal
131                            invalid opcode if some of these prefixes are
132                            present so we will never get here anyway */
133                         scan_more = ((instr_lo & 7) == 0x6);
134                         break;
135                         
136                 case 0x40:
137                         /* In AMD64 long mode, 0x40 to 0x4F are valid REX prefixes
138                            Need to figure out under what instruction mode the
139                            instruction was issued ... */
140                         /* Could check the LDT for lm, but for now it's good
141                            enough to assume that long mode only uses well known
142                            segments or kernel. */
143                         scan_more = ((regs->cs & 3) == 0) || (regs->cs == __USER_CS);
144                         break;
145                         
146                 case 0x60:
147                         /* 0x64 thru 0x67 are valid prefixes in all modes. */
148                         scan_more = (instr_lo & 0xC) == 0x4;
149                         break;          
150                 case 0xF0:
151                         /* 0xF0, 0xF2, and 0xF3 are valid prefixes in all modes. */
152                         scan_more = !instr_lo || (instr_lo>>1) == 1;
153                         break;                  
154                 case 0x00:
155                         /* Prefetch instruction is 0x0F0D or 0x0F18 */
156                         scan_more = 0;
157                         if (__get_user(opcode, instr)) 
158                                 break;
159                         prefetch = (instr_lo == 0xF) &&
160                                 (opcode == 0x0D || opcode == 0x18);
161                         break;                  
162                 default:
163                         scan_more = 0;
164                         break;
165                 } 
166         }
167
168 #if 0
169         if (prefetch)
170                 printk("%s: prefetch caused page fault at %lx/%lx\n", current->comm,
171                        regs->rip, addr);
172 #endif
173         return prefetch;
174 }
175
176 int page_fault_trace; 
177 int exception_trace = 1;
178
179 /*
180  * This routine handles page faults.  It determines the address,
181  * and the problem, and then passes it off to one of the appropriate
182  * routines.
183  *
184  * error_code:
185  *      bit 0 == 0 means no page found, 1 means protection fault
186  *      bit 1 == 0 means read, 1 means write
187  *      bit 2 == 0 means kernel, 1 means user-mode
188  *      bit 3 == 1 means fault was an instruction fetch
189  */
190 asmlinkage void do_page_fault(struct pt_regs *regs, unsigned long error_code)
191 {
192         struct task_struct *tsk;
193         struct mm_struct *mm;
194         struct vm_area_struct * vma;
195         unsigned long address;
196         unsigned long fixup;
197         int write;
198         siginfo_t info;
199
200         /* get the address */
201         __asm__("movq %%cr2,%0":"=r" (address));
202
203         if (regs->eflags & X86_EFLAGS_IF)
204                 __sti();
205
206 #ifdef CONFIG_CHECKING
207         if (page_fault_trace) 
208                 printk("pagefault rip:%lx rsp:%lx cs:%lu ss:%lu address %lx error %lx\n",
209                        regs->rip,regs->rsp,regs->cs,regs->ss,address,error_code); 
210
211
212         { 
213                 unsigned long gs; 
214                 struct x8664_pda *pda = cpu_pda + safe_smp_processor_id(); 
215                 rdmsrl(MSR_GS_BASE, gs); 
216                 if (gs != (unsigned long)pda) { 
217                         wrmsrl(MSR_GS_BASE, pda); 
218                         printk("page_fault: wrong gs %lx expected %p\n", gs, pda);
219                 }
220         }
221 #endif
222                         
223         tsk = current;
224         mm = tsk->mm;
225         info.si_code = SEGV_MAPERR;
226
227         /* 5 => page not present and from supervisor mode */
228         if (unlikely(!(error_code & 5) &&
229                      ((address >= VMALLOC_START && address <= VMALLOC_END) ||
230                       (address >= MODULES_VADDR && address <= MODULES_END))))
231                 goto vmalloc_fault;
232   
233         /*
234          * If we're in an interrupt or have no user
235          * context, we must not take the fault..
236          */
237         if (in_interrupt() || !mm)
238                 goto bad_area_nosemaphore;
239
240         /* 
241          * Work around K8 errata #100. See the K8 specification update for 
242          * details. Any code segment in LDT is compatibility mode.
243          */
244         if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) &&
245                 (address >> 32))
246                 return;
247
248 again:
249         down_read(&mm->mmap_sem);
250
251         vma = find_vma(mm, address);
252         if (!vma)
253                 goto bad_area;
254         if (vma->vm_start <= address)
255                 goto good_area;
256         if (!(vma->vm_flags & VM_GROWSDOWN))
257                 goto bad_area;
258         if (error_code & 4) {
259                 // XXX: align red zone size with ABI 
260                 if (address + 128 < regs->rsp)
261                         goto bad_area;
262         }
263         if (expand_stack(vma, address))
264                 goto bad_area;
265 /*
266  * Ok, we have a good vm_area for this memory access, so
267  * we can handle it..
268  */
269 good_area:
270         info.si_code = SEGV_ACCERR;
271         write = 0;
272         switch (error_code & 3) {
273                 default:        /* 3: write, present */
274                         /* fall through */
275                 case 2:         /* write, not present */
276                         if (!(vma->vm_flags & VM_WRITE))
277                                 goto bad_area;
278                         write++;
279                         break;
280                 case 1:         /* read, present */
281                         goto bad_area;
282                 case 0:         /* read, not present */
283                         if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
284                                 goto bad_area;
285         }
286
287         /*
288          * If for any reason at all we couldn't handle the fault,
289          * make sure we exit gracefully rather than endlessly redo
290          * the fault.
291          */
292         switch (handle_mm_fault(mm, vma, address, write)) {
293         case 1:
294                 tsk->min_flt++;
295                 break;
296         case 2:
297                 tsk->maj_flt++;
298                 break;
299         case 0:
300                 goto do_sigbus;
301         default:
302                 goto out_of_memory;
303         }
304
305         up_read(&mm->mmap_sem);
306         return;
307
308 /*
309  * Something tried to access memory that isn't in our memory map..
310  * Fix it, but check if it's kernel or user first..
311  */
312 bad_area:
313         up_read(&mm->mmap_sem);
314
315 bad_area_nosemaphore:
316         /* User mode accesses just cause a SIGSEGV */
317         if (error_code & 4) {
318                 if (is_prefetch(regs, address))
319                         return;
320
321                 if (exception_trace && !(tsk->ptrace & PT_PTRACED) && 
322                     (tsk->sig->action[SIGSEGV-1].sa.sa_handler == SIG_IGN ||
323                     (tsk->sig->action[SIGSEGV-1].sa.sa_handler == SIG_DFL)))
324                         printk(KERN_INFO 
325                        "%s[%d]: segfault at %016lx rip %016lx rsp %016lx error %lx\n",
326                                         tsk->comm, tsk->pid, address, regs->rip,
327                                         regs->rsp, error_code);
328         
329                 tsk->thread.cr2 = address;
330                 /* Kernel addresses are always protection faults */
331                 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
332                 tsk->thread.trap_no = 14;
333                 info.si_signo = SIGSEGV;
334                 info.si_errno = 0;
335                 /* info.si_code has been set above */
336                 info.si_addr = (void *)address;
337                 force_sig_info(SIGSEGV, &info, tsk);
338                 return;
339         }
340
341 no_context:
342         
343         /* Are we prepared to handle this kernel fault?  */
344         if ((fixup = search_exception_table(regs->rip)) != 0) {
345                 regs->rip = fixup;
346                 if (0 && exception_trace) 
347                 printk(KERN_ERR 
348                        "%s: fixed kernel exception at %lx address %lx err:%ld\n", 
349                        tsk->comm, regs->rip, address, error_code);
350                 return;
351         }
352
353         if (is_prefetch(regs, address))
354                 return;
355
356 /*
357  * Oops. The kernel tried to access some bad page. We'll have to
358  * terminate things with extreme prejudice.
359  */
360
361         unsigned long flags; 
362         prepare_die(&flags);
363         if (address < PAGE_SIZE)
364                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
365         else
366                 printk(KERN_ALERT "Unable to handle kernel paging request");
367         printk(KERN_ALERT " at %016lx RIP: ", address); 
368         printk_address(regs->rip);
369         dump_pagetable(address);
370         __die("Oops", regs, error_code);
371         /* Executive summary in case the oops scrolled away */
372         printk(KERN_EMERG "CR2: %016lx\n", address);
373         exit_die(flags);
374         do_exit(SIGKILL);
375
376 /*
377  * We ran out of memory, or some other thing happened to us that made
378  * us unable to handle the page fault gracefully.
379  */
380 out_of_memory:
381         up_read(&mm->mmap_sem);
382         if (current->pid == 1) { 
383                 tsk->policy |= SCHED_YIELD;
384                 schedule();
385                 goto again;
386         }
387         printk("VM: killing process %s\n", tsk->comm);
388         if (error_code & 4)
389                 do_exit(SIGKILL);
390         goto no_context;
391
392 do_sigbus:
393         up_read(&mm->mmap_sem);
394
395         /* Kernel mode? Handle exceptions or die */
396         if (!(error_code & 4))
397                 goto no_context;
398                 
399         if (is_prefetch(regs, address))
400                 return;
401
402         tsk->thread.cr2 = address;
403         tsk->thread.error_code = error_code;
404         tsk->thread.trap_no = 14;
405         info.si_signo = SIGBUS;
406         info.si_errno = 0;
407         info.si_code = BUS_ADRERR;
408         info.si_addr = (void *)address;
409         force_sig_info(SIGBUS, &info, tsk);
410         return;
411
412
413 vmalloc_fault:
414         {
415                 pgd_t *pgd;
416                 pmd_t *pmd;
417                 pte_t *pte; 
418
419                 /* 
420                  * x86-64 has the same kernel 3rd level pages for all CPUs.
421                  * But for vmalloc/modules the TLB synchronization works lazily,
422                  * so it can happen that we get a page fault for something
423                  * that is really already in the page table. Just check if it
424                  * is really there and when yes flush the local TLB. 
425                  */
426 #if 0
427                 printk("vmalloc fault %lx index %lu\n",address,pml4_index(address));
428                 dump_pagetable(address);
429 #endif
430
431                 pgd = pgd_offset_k(address);
432                 if (pgd != current_pgd_offset_k(address)) 
433                         goto bad_area_nosemaphore;       
434                 if (!pgd_present(*pgd))
435                         goto bad_area_nosemaphore;
436                 pmd = pmd_offset(pgd, address);
437                 if (!pmd_present(*pmd))
438                         goto bad_area_nosemaphore;
439                 pte = pte_offset(pmd, address); 
440                 if (!pte_present(*pte))
441                         goto bad_area_nosemaphore; 
442
443                 __flush_tlb_all();              
444                 return;
445         }
446 }