fedora core 2.6.9-1.11-FC2
[linux-2.6.git] / arch / xen / i386 / mm / fault.c
1 /*
2  *  linux/arch/i386/mm/fault.c
3  *
4  *  Copyright (C) 1995  Linus Torvalds
5  */
6
7 #include <linux/signal.h>
8 #include <linux/sched.h>
9 #include <linux/kernel.h>
10 #include <linux/errno.h>
11 #include <linux/string.h>
12 #include <linux/types.h>
13 #include <linux/ptrace.h>
14 #include <linux/mman.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/smp_lock.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/tty.h>
21 #include <linux/vt_kern.h>              /* For unblank_screen() */
22 #include <linux/highmem.h>
23 #include <linux/module.h>
24 #include <linux/percpu.h>
25
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/desc.h>
29 #include <asm/kdebug.h>
30
31 extern void die(const char *,struct pt_regs *,long);
32
33 DEFINE_PER_CPU(pgd_t *, cur_pgd);
34
35 /*
36  * Unlock any spinlocks which will prevent us from getting the
37  * message out 
38  */
39 void bust_spinlocks(int yes)
40 {
41         int loglevel_save = console_loglevel;
42
43         if (yes) {
44                 oops_in_progress = 1;
45                 return;
46         }
47 #ifdef CONFIG_VT
48         unblank_screen();
49 #endif
50         oops_in_progress = 0;
51         /*
52          * OK, the message is on the console.  Now we call printk()
53          * without oops_in_progress set so that printk will give klogd
54          * a poke.  Hold onto your hats...
55          */
56         console_loglevel = 15;          /* NMI oopser may have shut the console up */
57         printk(" ");
58         console_loglevel = loglevel_save;
59 }
60
61 /*
62  * Return EIP plus the CS segment base.  The segment limit is also
63  * adjusted, clamped to the kernel/user address space (whichever is
64  * appropriate), and returned in *eip_limit.
65  *
66  * The segment is checked, because it might have been changed by another
67  * task between the original faulting instruction and here.
68  *
69  * If CS is no longer a valid code segment, or if EIP is beyond the
70  * limit, or if it is a kernel address when CS is not a kernel segment,
71  * then the returned value will be greater than *eip_limit.
72  * 
73  * This is slow, but is very rarely executed.
74  */
75 static inline unsigned long get_segment_eip(struct pt_regs *regs,
76                                             unsigned long *eip_limit)
77 {
78         unsigned long eip = regs->eip;
79         unsigned seg = regs->xcs & 0xffff;
80         u32 seg_ar, seg_limit, base, *desc;
81
82         /* The standard kernel/user address space limit. */
83         *eip_limit = (seg & 2) ? USER_DS.seg : KERNEL_DS.seg;
84
85         /* Unlikely, but must come before segment checks. */
86         if (unlikely((regs->eflags & VM_MASK) != 0))
87                 return eip + (seg << 4);
88         
89         /* By far the most common cases. */
90         if (likely(seg == __USER_CS || seg == __KERNEL_CS))
91                 return eip;
92
93         /* Check the segment exists, is within the current LDT/GDT size,
94            that kernel/user (ring 0..3) has the appropriate privilege,
95            that it's a code segment, and get the limit. */
96         __asm__ ("larl %3,%0; lsll %3,%1"
97                  : "=&r" (seg_ar), "=r" (seg_limit) : "0" (0), "rm" (seg));
98         if ((~seg_ar & 0x9800) || eip > seg_limit) {
99                 *eip_limit = 0;
100                 return 1;        /* So that returned eip > *eip_limit. */
101         }
102
103         /* Get the GDT/LDT descriptor base. 
104            When you look for races in this code remember that
105            LDT and other horrors are only used in user space. */
106         if (seg & (1<<2)) {
107                 /* Must lock the LDT while reading it. */
108                 down(&current->mm->context.sem);
109                 desc = current->mm->context.ldt;
110                 desc = (void *)desc + (seg & ~7);
111         } else {
112                 /* Must disable preemption while reading the GDT. */
113                 desc = (u32 *)get_cpu_gdt_table(get_cpu());
114                 desc = (void *)desc + (seg & ~7);
115         }
116
117         /* Decode the code segment base from the descriptor */
118         base = get_desc_base((unsigned long *)desc);
119
120         if (seg & (1<<2)) { 
121                 up(&current->mm->context.sem);
122         } else
123                 put_cpu();
124
125         /* Adjust EIP and segment limit, and clamp at the kernel limit.
126            It's legitimate for segments to wrap at 0xffffffff. */
127         seg_limit += base;
128         if (seg_limit < *eip_limit && seg_limit >= base)
129                 *eip_limit = seg_limit;
130         return eip + base;
131 }
132
133 /* 
134  * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
135  * Check that here and ignore it.
136  */
137 static int __is_prefetch(struct pt_regs *regs, unsigned long addr)
138
139         unsigned long limit;
140         unsigned long instr = get_segment_eip (regs, &limit);
141         int scan_more = 1;
142         int prefetch = 0; 
143         int i;
144
145         for (i = 0; scan_more && i < 15; i++) { 
146                 unsigned char opcode;
147                 unsigned char instr_hi;
148                 unsigned char instr_lo;
149
150                 if (instr > limit)
151                         break;
152                 if (__get_user(opcode, (unsigned char *) instr))
153                         break; 
154
155                 instr_hi = opcode & 0xf0; 
156                 instr_lo = opcode & 0x0f; 
157                 instr++;
158
159                 switch (instr_hi) { 
160                 case 0x20:
161                 case 0x30:
162                         /* Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. */
163                         scan_more = ((instr_lo & 7) == 0x6);
164                         break;
165                         
166                 case 0x60:
167                         /* 0x64 thru 0x67 are valid prefixes in all modes. */
168                         scan_more = (instr_lo & 0xC) == 0x4;
169                         break;          
170                 case 0xF0:
171                         /* 0xF0, 0xF2, and 0xF3 are valid prefixes */
172                         scan_more = !instr_lo || (instr_lo>>1) == 1;
173                         break;                  
174                 case 0x00:
175                         /* Prefetch instruction is 0x0F0D or 0x0F18 */
176                         scan_more = 0;
177                         if (instr > limit)
178                                 break;
179                         if (__get_user(opcode, (unsigned char *) instr)) 
180                                 break;
181                         prefetch = (instr_lo == 0xF) &&
182                                 (opcode == 0x0D || opcode == 0x18);
183                         break;                  
184                 default:
185                         scan_more = 0;
186                         break;
187                 } 
188         }
189         return prefetch;
190 }
191
192 static inline int is_prefetch(struct pt_regs *regs, unsigned long addr,
193                               unsigned long error_code)
194 {
195         if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
196                      boot_cpu_data.x86 >= 6)) {
197                 /* Catch an obscure case of prefetch inside an NX page. */
198                 if (nx_enabled && (error_code & 16))
199                         return 0;
200                 return __is_prefetch(regs, addr);
201         }
202         return 0;
203
204
205 fastcall void do_invalid_op(struct pt_regs *, unsigned long);
206
207 /*
208  * This routine handles page faults.  It determines the address,
209  * and the problem, and then passes it off to one of the appropriate
210  * routines.
211  *
212  * error_code:
213  *      bit 0 == 0 means no page found, 1 means protection fault
214  *      bit 1 == 0 means read, 1 means write
215  *      bit 2 == 0 means kernel, 1 means user-mode
216  */
217 fastcall void do_page_fault(struct pt_regs *regs, unsigned long error_code,
218                               unsigned long address)
219 {
220         struct task_struct *tsk;
221         struct mm_struct *mm;
222         struct vm_area_struct * vma;
223         unsigned long page;
224         int write;
225         siginfo_t info;
226
227         /* Set the "privileged fault" bit to something sane. */
228         error_code &= 3;
229         error_code |= (regs->xcs & 2) << 1;
230         if (regs->eflags & X86_EFLAGS_VM)
231                 error_code |= 4;
232
233         if (notify_die(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
234                                         SIGSEGV) == NOTIFY_STOP)
235                 return;
236 #if 0
237         /* It's safe to allow irq's after cr2 has been saved */
238         if (regs->eflags & (X86_EFLAGS_IF|VM_MASK))
239                 local_irq_enable();
240 #endif
241
242         tsk = current;
243
244         info.si_code = SEGV_MAPERR;
245
246         /*
247          * We fault-in kernel-space virtual memory on-demand. The
248          * 'reference' page table is init_mm.pgd.
249          *
250          * NOTE! We MUST NOT take any locks for this case. We may
251          * be in an interrupt or a critical region, and should
252          * only copy the information from the master page table,
253          * nothing more.
254          *
255          * This verifies that the fault happens in kernel space
256          * (error_code & 4) == 0, and that the fault was not a
257          * protection error (error_code & 1) == 0.
258          */
259         if (unlikely(address >= TASK_SIZE)) { 
260                 if (!(error_code & 5))
261                         goto vmalloc_fault;
262                 /* 
263                  * Don't take the mm semaphore here. If we fixup a prefetch
264                  * fault we could otherwise deadlock.
265                  */
266                 goto bad_area_nosemaphore;
267         } 
268
269         mm = tsk->mm;
270
271         /*
272          * If we're in an interrupt, have no user context or are running in an
273          * atomic region then we must not take the fault..
274          */
275         if (in_atomic() || !mm)
276                 goto bad_area_nosemaphore;
277
278         /* When running in the kernel we expect faults to occur only to
279          * addresses in user space.  All other faults represent errors in the
280          * kernel and should generate an OOPS.  Unfortunatly, in the case of an
281          * erroneous fault occuring in a code path which already holds mmap_sem
282          * we will deadlock attempting to validate the fault against the
283          * address space.  Luckily the kernel only validly references user
284          * space from well defined areas of code, which are listed in the
285          * exceptions table.
286          *
287          * As the vast majority of faults will be valid we will only perform
288          * the source reference check when there is a possibilty of a deadlock.
289          * Attempt to lock the address space, if we cannot we then validate the
290          * source.  If this is invalid we can skip the address space check,
291          * thus avoiding the deadlock.
292          */
293         if (!down_read_trylock(&mm->mmap_sem)) {
294                 if ((error_code & 4) == 0 &&
295                     !search_exception_tables(regs->eip))
296                         goto bad_area_nosemaphore;
297                 down_read(&mm->mmap_sem);
298         }
299
300         vma = find_vma(mm, address);
301         if (!vma)
302                 goto bad_area;
303         if (vma->vm_start <= address)
304                 goto good_area;
305         if (!(vma->vm_flags & VM_GROWSDOWN))
306                 goto bad_area;
307         if (error_code & 4) {
308                 /*
309                  * accessing the stack below %esp is always a bug.
310                  * The "+ 32" is there due to some instructions (like
311                  * pusha) doing post-decrement on the stack and that
312                  * doesn't show up until later..
313                  */
314                 if (address + 32 < regs->esp)
315                         goto bad_area;
316         }
317         if (expand_stack(vma, address))
318                 goto bad_area;
319 /*
320  * Ok, we have a good vm_area for this memory access, so
321  * we can handle it..
322  */
323 good_area:
324         info.si_code = SEGV_ACCERR;
325         write = 0;
326         switch (error_code & 3) {
327                 default:        /* 3: write, present */
328 #ifdef TEST_VERIFY_AREA
329                         if (regs->cs == KERNEL_CS)
330                                 printk("WP fault at %08lx\n", regs->eip);
331 #endif
332                         /* fall through */
333                 case 2:         /* write, not present */
334                         if (!(vma->vm_flags & VM_WRITE))
335                                 goto bad_area;
336                         write++;
337                         break;
338                 case 1:         /* read, present */
339                         goto bad_area;
340                 case 0:         /* read, not present */
341                         if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
342                                 goto bad_area;
343         }
344
345  survive:
346         /*
347          * If for any reason at all we couldn't handle the fault,
348          * make sure we exit gracefully rather than endlessly redo
349          * the fault.
350          */
351         switch (handle_mm_fault(mm, vma, address, write)) {
352                 case VM_FAULT_MINOR:
353                         tsk->min_flt++;
354                         break;
355                 case VM_FAULT_MAJOR:
356                         tsk->maj_flt++;
357                         break;
358                 case VM_FAULT_SIGBUS:
359                         goto do_sigbus;
360                 case VM_FAULT_OOM:
361                         goto out_of_memory;
362                 default:
363                         BUG();
364         }
365
366         /*
367          * Did it hit the DOS screen memory VA from vm86 mode?
368          */
369         if (regs->eflags & VM_MASK) {
370                 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
371                 if (bit < 32)
372                         tsk->thread.screen_bitmap |= 1 << bit;
373         }
374         up_read(&mm->mmap_sem);
375         return;
376
377 /*
378  * Something tried to access memory that isn't in our memory map..
379  * Fix it, but check if it's kernel or user first..
380  */
381 bad_area:
382         up_read(&mm->mmap_sem);
383
384 bad_area_nosemaphore:
385         /* User mode accesses just cause a SIGSEGV */
386         if (error_code & 4) {
387                 /* 
388                  * Valid to do another page fault here because this one came 
389                  * from user space.
390                  */
391                 if (is_prefetch(regs, address, error_code))
392                         return;
393
394                 tsk->thread.cr2 = address;
395                 /* Kernel addresses are always protection faults */
396                 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
397                 tsk->thread.trap_no = 14;
398                 info.si_signo = SIGSEGV;
399                 info.si_errno = 0;
400                 /* info.si_code has been set above */
401                 info.si_addr = (void __user *)address;
402                 force_sig_info(SIGSEGV, &info, tsk);
403                 return;
404         }
405
406 #ifdef CONFIG_X86_F00F_BUG
407         /*
408          * Pentium F0 0F C7 C8 bug workaround.
409          */
410         if (boot_cpu_data.f00f_bug) {
411                 unsigned long nr;
412                 
413                 nr = (address - idt_descr.address) >> 3;
414
415                 if (nr == 6) {
416                         do_invalid_op(regs, 0);
417                         return;
418                 }
419         }
420 #endif
421
422 no_context:
423         /* Are we prepared to handle this kernel fault?  */
424         if (fixup_exception(regs))
425                 return;
426
427         /* 
428          * Valid to do another page fault here, because if this fault
429          * had been triggered by is_prefetch fixup_exception would have 
430          * handled it.
431          */
432         if (is_prefetch(regs, address, error_code))
433                 return;
434
435 /*
436  * Oops. The kernel tried to access some bad page. We'll have to
437  * terminate things with extreme prejudice.
438  */
439
440         bust_spinlocks(1);
441
442 #ifdef CONFIG_X86_PAE
443         if (error_code & 16) {
444                 pte_t *pte = lookup_address(address);
445
446                 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
447                         printk(KERN_CRIT "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", current->uid);
448         }
449 #endif
450         if (address < PAGE_SIZE)
451                 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
452         else
453                 printk(KERN_ALERT "Unable to handle kernel paging request");
454         printk(" at virtual address %08lx\n",address);
455         printk(KERN_ALERT " printing eip:\n");
456         printk("%08lx\n", regs->eip);
457         page = ((unsigned long *) per_cpu(cur_pgd, smp_processor_id()))
458             [address >> 22];
459         printk(KERN_ALERT "*pde = ma %08lx pa %08lx\n", page,
460                machine_to_phys(page));
461         /*
462          * We must not directly access the pte in the highpte
463          * case, the page table might be allocated in highmem.
464          * And lets rather not kmap-atomic the pte, just in case
465          * it's allocated already.
466          */
467 #ifndef CONFIG_HIGHPTE
468         if (page & 1) {
469                 page &= PAGE_MASK;
470                 address &= 0x003ff000;
471                 page = machine_to_phys(page);
472                 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
473                 printk(KERN_ALERT "*pte = ma %08lx pa %08lx\n", page,
474                        machine_to_phys(page));
475         }
476 #endif
477         show_trace(NULL, (unsigned long *)&regs[1]);
478         die("Oops", regs, error_code);
479         bust_spinlocks(0);
480         do_exit(SIGKILL);
481
482 /*
483  * We ran out of memory, or some other thing happened to us that made
484  * us unable to handle the page fault gracefully.
485  */
486 out_of_memory:
487         up_read(&mm->mmap_sem);
488         if (tsk->pid == 1) {
489                 yield();
490                 down_read(&mm->mmap_sem);
491                 goto survive;
492         }
493         printk("VM: killing process %s\n", tsk->comm);
494         if (error_code & 4)
495                 do_exit(SIGKILL);
496         goto no_context;
497
498 do_sigbus:
499         up_read(&mm->mmap_sem);
500
501         /* Kernel mode? Handle exceptions or die */
502         if (!(error_code & 4))
503                 goto no_context;
504
505         /* User space => ok to do another page fault */
506         if (is_prefetch(regs, address, error_code))
507                 return;
508
509         tsk->thread.cr2 = address;
510         tsk->thread.error_code = error_code;
511         tsk->thread.trap_no = 14;
512         info.si_signo = SIGBUS;
513         info.si_errno = 0;
514         info.si_code = BUS_ADRERR;
515         info.si_addr = (void __user *)address;
516         force_sig_info(SIGBUS, &info, tsk);
517         return;
518
519 vmalloc_fault:
520         {
521                 /*
522                  * Synchronize this task's top level page-table
523                  * with the 'reference' page table.
524                  *
525                  * Do _not_ use "tsk" here. We might be inside
526                  * an interrupt in the middle of a task switch..
527                  */
528                 int index = pgd_index(address);
529                 pgd_t *pgd, *pgd_k;
530                 pud_t *pud, *pud_k;
531                 pmd_t *pmd, *pmd_k;
532                 pte_t *pte_k;
533
534                 pgd = index + per_cpu(cur_pgd, smp_processor_id());
535                 pgd_k = init_mm.pgd + index;
536
537                 if (!pgd_present(*pgd_k))
538                         goto no_context;
539
540                 /*
541                  * set_pgd(pgd, *pgd_k); here would be useless on PAE
542                  * and redundant with the set_pmd() on non-PAE. As would
543                  * set_pud.
544                  */
545
546                 pud = pud_offset(pgd, address);
547                 pud_k = pud_offset(pgd_k, address);
548                 if (!pud_present(*pud_k))
549                         goto no_context;
550                 
551                 pmd = pmd_offset(pud, address);
552                 pmd_k = pmd_offset(pud_k, address);
553                 if (!pmd_present(*pmd_k))
554                         goto no_context;
555                 set_pmd(pmd, *pmd_k);
556
557                 pte_k = pte_offset_kernel(pmd_k, address);
558                 if (!pte_present(*pte_k))
559                         goto no_context;
560                 return;
561         }
562 }