ia64/xen-unstable

view linux-2.6.10-xen-sparse/arch/xen/i386/mm/fault.c @ 4182:0cf318b324fb

bitkeeper revision 1.1245.1.1 (42386d63YMsPB-xQ5vhzdFj1gbQ5kg)

added counter for pagefaults

Signed-off-by: michael.fetterman@cl.cam.ac.uk
author rneugeba@wyvis.research.intel-research.net
date Wed Mar 16 17:31:15 2005 +0000 (2005-03-16)
parents 2c4ca5aad6c4
children cf77cd925ef3
line source
1 /*
2 * linux/arch/i386/mm/fault.c
3 *
4 * Copyright (C) 1995 Linus Torvalds
5 */
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>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
28 #include <asm/desc.h>
29 #include <asm/kdebug.h>
31 extern void die(const char *,struct pt_regs *,long);
33 DEFINE_PER_CPU(pgd_t *, cur_pgd);
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;
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 }
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;
82 /* The standard kernel/user address space limit. */
83 *eip_limit = (seg & 2) ? USER_DS.seg : KERNEL_DS.seg;
85 /* Unlikely, but must come before segment checks. */
86 if (unlikely((regs->eflags & VM_MASK) != 0))
87 return eip + (seg << 4);
89 /* By far the most common cases. */
90 if (likely(seg == __USER_CS || seg == __KERNEL_CS))
91 return eip;
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 }
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 }
117 /* Decode the code segment base from the descriptor */
118 base = (desc[0] >> 16) |
119 ((desc[1] & 0xff) << 16) |
120 (desc[1] & 0xff000000);
122 if (seg & (1<<2)) {
123 up(&current->mm->context.sem);
124 } else
125 put_cpu();
127 /* Adjust EIP and segment limit, and clamp at the kernel limit.
128 It's legitimate for segments to wrap at 0xffffffff. */
129 seg_limit += base;
130 if (seg_limit < *eip_limit && seg_limit >= base)
131 *eip_limit = seg_limit;
132 return eip + base;
133 }
135 /*
136 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
137 * Check that here and ignore it.
138 */
139 static int __is_prefetch(struct pt_regs *regs, unsigned long addr)
140 {
141 unsigned long limit;
142 unsigned long instr = get_segment_eip (regs, &limit);
143 int scan_more = 1;
144 int prefetch = 0;
145 int i;
147 for (i = 0; scan_more && i < 15; i++) {
148 unsigned char opcode;
149 unsigned char instr_hi;
150 unsigned char instr_lo;
152 if (instr > limit)
153 break;
154 if (__get_user(opcode, (unsigned char *) instr))
155 break;
157 instr_hi = opcode & 0xf0;
158 instr_lo = opcode & 0x0f;
159 instr++;
161 switch (instr_hi) {
162 case 0x20:
163 case 0x30:
164 /* Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. */
165 scan_more = ((instr_lo & 7) == 0x6);
166 break;
168 case 0x60:
169 /* 0x64 thru 0x67 are valid prefixes in all modes. */
170 scan_more = (instr_lo & 0xC) == 0x4;
171 break;
172 case 0xF0:
173 /* 0xF0, 0xF2, and 0xF3 are valid prefixes */
174 scan_more = !instr_lo || (instr_lo>>1) == 1;
175 break;
176 case 0x00:
177 /* Prefetch instruction is 0x0F0D or 0x0F18 */
178 scan_more = 0;
179 if (instr > limit)
180 break;
181 if (__get_user(opcode, (unsigned char *) instr))
182 break;
183 prefetch = (instr_lo == 0xF) &&
184 (opcode == 0x0D || opcode == 0x18);
185 break;
186 default:
187 scan_more = 0;
188 break;
189 }
190 }
191 return prefetch;
192 }
194 static inline int is_prefetch(struct pt_regs *regs, unsigned long addr,
195 unsigned long error_code)
196 {
197 if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
198 boot_cpu_data.x86 >= 6)) {
199 /* Catch an obscure case of prefetch inside an NX page. */
200 if (nx_enabled && (error_code & 16))
201 return 0;
202 return __is_prefetch(regs, addr);
203 }
204 return 0;
205 }
207 fastcall void do_invalid_op(struct pt_regs *, unsigned long);
209 /*
210 * This routine handles page faults. It determines the address,
211 * and the problem, and then passes it off to one of the appropriate
212 * routines.
213 *
214 * error_code:
215 * bit 0 == 0 means no page found, 1 means protection fault
216 * bit 1 == 0 means read, 1 means write
217 * bit 2 == 0 means kernel, 1 means user-mode
218 */
220 extern unsigned long c_do_page_fault;
221 extern unsigned long c_minor_page_fault;
222 extern unsigned long c_major_page_fault;
224 fastcall void do_page_fault(struct pt_regs *regs, unsigned long error_code,
225 unsigned long address)
226 {
227 struct task_struct *tsk;
228 struct mm_struct *mm;
229 struct vm_area_struct * vma;
230 unsigned long page;
231 int write;
232 siginfo_t info;
234 c_do_page_fault++;
236 /* Set the "privileged fault" bit to something sane. */
237 error_code &= 3;
238 error_code |= (regs->xcs & 2) << 1;
239 if (regs->eflags & X86_EFLAGS_VM)
240 error_code |= 4;
242 #ifdef CONFIG_XEN_BATCH_MODE2
243 /* ensure all updates have completed */
244 flush_page_update_queue();
245 #endif
248 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
249 SIGSEGV) == NOTIFY_STOP)
250 return;
251 #if 0
252 /* It's safe to allow irq's after cr2 has been saved */
253 if (regs->eflags & (X86_EFLAGS_IF|VM_MASK))
254 local_irq_enable();
255 #endif
257 tsk = current;
259 info.si_code = SEGV_MAPERR;
261 /*
262 * We fault-in kernel-space virtual memory on-demand. The
263 * 'reference' page table is init_mm.pgd.
264 *
265 * NOTE! We MUST NOT take any locks for this case. We may
266 * be in an interrupt or a critical region, and should
267 * only copy the information from the master page table,
268 * nothing more.
269 *
270 * This verifies that the fault happens in kernel space
271 * (error_code & 4) == 0, and that the fault was not a
272 * protection error (error_code & 1) == 0.
273 */
274 if (unlikely(address >= TASK_SIZE)) {
275 if (!(error_code & 5))
276 goto vmalloc_fault;
277 /*
278 * Don't take the mm semaphore here. If we fixup a prefetch
279 * fault we could otherwise deadlock.
280 */
281 goto bad_area_nosemaphore;
282 }
284 mm = tsk->mm;
286 /*
287 * If we're in an interrupt, have no user context or are running in an
288 * atomic region then we must not take the fault..
289 */
290 if (in_atomic() || !mm)
291 goto bad_area_nosemaphore;
293 /* When running in the kernel we expect faults to occur only to
294 * addresses in user space. All other faults represent errors in the
295 * kernel and should generate an OOPS. Unfortunatly, in the case of an
296 * erroneous fault occuring in a code path which already holds mmap_sem
297 * we will deadlock attempting to validate the fault against the
298 * address space. Luckily the kernel only validly references user
299 * space from well defined areas of code, which are listed in the
300 * exceptions table.
301 *
302 * As the vast majority of faults will be valid we will only perform
303 * the source reference check when there is a possibilty of a deadlock.
304 * Attempt to lock the address space, if we cannot we then validate the
305 * source. If this is invalid we can skip the address space check,
306 * thus avoiding the deadlock.
307 */
308 if (!down_read_trylock(&mm->mmap_sem)) {
309 if ((error_code & 4) == 0 &&
310 !search_exception_tables(regs->eip))
311 goto bad_area_nosemaphore;
312 down_read(&mm->mmap_sem);
313 }
315 vma = find_vma(mm, address);
316 if (!vma)
317 goto bad_area;
318 if (vma->vm_start <= address)
319 goto good_area;
320 if (!(vma->vm_flags & VM_GROWSDOWN))
321 goto bad_area;
322 if (error_code & 4) {
323 /*
324 * accessing the stack below %esp is always a bug.
325 * The "+ 32" is there due to some instructions (like
326 * pusha) doing post-decrement on the stack and that
327 * doesn't show up until later..
328 */
329 if (address + 32 < regs->esp)
330 goto bad_area;
331 }
332 if (expand_stack(vma, address))
333 goto bad_area;
334 /*
335 * Ok, we have a good vm_area for this memory access, so
336 * we can handle it..
337 */
338 good_area:
339 info.si_code = SEGV_ACCERR;
340 write = 0;
341 switch (error_code & 3) {
342 default: /* 3: write, present */
343 #ifdef TEST_VERIFY_AREA
344 if (regs->cs == KERNEL_CS)
345 printk("WP fault at %08lx\n", regs->eip);
346 #endif
347 /* fall through */
348 case 2: /* write, not present */
349 if (!(vma->vm_flags & VM_WRITE))
350 goto bad_area;
351 write++;
352 break;
353 case 1: /* read, present */
354 goto bad_area;
355 case 0: /* read, not present */
356 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
357 goto bad_area;
358 }
360 survive:
361 /*
362 * If for any reason at all we couldn't handle the fault,
363 * make sure we exit gracefully rather than endlessly redo
364 * the fault.
365 */
366 switch (handle_mm_fault(mm, vma, address, write)) {
367 case VM_FAULT_MINOR:
368 tsk->min_flt++;
369 c_minor_page_fault++;
370 break;
371 case VM_FAULT_MAJOR:
372 tsk->maj_flt++;
373 c_major_page_fault++;
374 break;
375 case VM_FAULT_SIGBUS:
376 goto do_sigbus;
377 case VM_FAULT_OOM:
378 goto out_of_memory;
379 default:
380 BUG();
381 }
383 /*
384 * Did it hit the DOS screen memory VA from vm86 mode?
385 */
386 if (regs->eflags & VM_MASK) {
387 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
388 if (bit < 32)
389 tsk->thread.screen_bitmap |= 1 << bit;
390 }
391 up_read(&mm->mmap_sem);
392 return;
394 /*
395 * Something tried to access memory that isn't in our memory map..
396 * Fix it, but check if it's kernel or user first..
397 */
398 bad_area:
399 up_read(&mm->mmap_sem);
401 bad_area_nosemaphore:
402 /* User mode accesses just cause a SIGSEGV */
403 if (error_code & 4) {
404 /*
405 * Valid to do another page fault here because this one came
406 * from user space.
407 */
408 if (is_prefetch(regs, address, error_code))
409 return;
411 tsk->thread.cr2 = address;
412 /* Kernel addresses are always protection faults */
413 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
414 tsk->thread.trap_no = 14;
415 info.si_signo = SIGSEGV;
416 info.si_errno = 0;
417 /* info.si_code has been set above */
418 info.si_addr = (void __user *)address;
419 force_sig_info(SIGSEGV, &info, tsk);
420 return;
421 }
423 #ifdef CONFIG_X86_F00F_BUG
424 /*
425 * Pentium F0 0F C7 C8 bug workaround.
426 */
427 if (boot_cpu_data.f00f_bug) {
428 unsigned long nr;
430 nr = (address - idt_descr.address) >> 3;
432 if (nr == 6) {
433 do_invalid_op(regs, 0);
434 return;
435 }
436 }
437 #endif
439 no_context:
440 /* Are we prepared to handle this kernel fault? */
441 if (fixup_exception(regs))
442 return;
444 /*
445 * Valid to do another page fault here, because if this fault
446 * had been triggered by is_prefetch fixup_exception would have
447 * handled it.
448 */
449 if (is_prefetch(regs, address, error_code))
450 return;
452 /*
453 * Oops. The kernel tried to access some bad page. We'll have to
454 * terminate things with extreme prejudice.
455 */
457 bust_spinlocks(1);
459 #ifdef CONFIG_X86_PAE
460 if (error_code & 16) {
461 pte_t *pte = lookup_address(address);
463 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
464 printk(KERN_CRIT "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", current->uid);
465 }
466 #endif
467 if (address < PAGE_SIZE)
468 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
469 else
470 printk(KERN_ALERT "Unable to handle kernel paging request");
471 printk(" at virtual address %08lx\n",address);
472 printk(KERN_ALERT " printing eip:\n");
473 printk("%08lx\n", regs->eip);
474 page = ((unsigned long *) per_cpu(cur_pgd, smp_processor_id()))
475 [address >> 22];
476 printk(KERN_ALERT "*pde = ma %08lx pa %08lx\n", page,
477 machine_to_phys(page));
478 /*
479 * We must not directly access the pte in the highpte
480 * case, the page table might be allocated in highmem.
481 * And lets rather not kmap-atomic the pte, just in case
482 * it's allocated already.
483 */
484 #ifndef CONFIG_HIGHPTE
485 if (page & 1) {
486 page &= PAGE_MASK;
487 address &= 0x003ff000;
488 page = machine_to_phys(page);
489 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
490 printk(KERN_ALERT "*pte = ma %08lx pa %08lx\n", page,
491 machine_to_phys(page));
492 }
493 #endif
494 show_trace(NULL, (unsigned long *)&regs[1]);
495 die("Oops", regs, error_code);
496 bust_spinlocks(0);
497 do_exit(SIGKILL);
499 /*
500 * We ran out of memory, or some other thing happened to us that made
501 * us unable to handle the page fault gracefully.
502 */
503 out_of_memory:
504 up_read(&mm->mmap_sem);
505 if (tsk->pid == 1) {
506 yield();
507 down_read(&mm->mmap_sem);
508 goto survive;
509 }
510 printk("VM: killing process %s\n", tsk->comm);
511 if (error_code & 4)
512 do_exit(SIGKILL);
513 goto no_context;
515 do_sigbus:
516 up_read(&mm->mmap_sem);
518 /* Kernel mode? Handle exceptions or die */
519 if (!(error_code & 4))
520 goto no_context;
522 /* User space => ok to do another page fault */
523 if (is_prefetch(regs, address, error_code))
524 return;
526 tsk->thread.cr2 = address;
527 tsk->thread.error_code = error_code;
528 tsk->thread.trap_no = 14;
529 info.si_signo = SIGBUS;
530 info.si_errno = 0;
531 info.si_code = BUS_ADRERR;
532 info.si_addr = (void __user *)address;
533 force_sig_info(SIGBUS, &info, tsk);
534 return;
536 vmalloc_fault:
537 {
538 /*
539 * Synchronize this task's top level page-table
540 * with the 'reference' page table.
541 *
542 * Do _not_ use "tsk" here. We might be inside
543 * an interrupt in the middle of a task switch..
544 */
545 int index = pgd_index(address);
546 pgd_t *pgd, *pgd_k;
547 pmd_t *pmd, *pmd_k;
548 pte_t *pte_k;
550 pgd = index + per_cpu(cur_pgd, smp_processor_id());
551 pgd_k = init_mm.pgd + index;
553 if (!pgd_present(*pgd_k))
554 goto no_context;
556 /*
557 * set_pgd(pgd, *pgd_k); here would be useless on PAE
558 * and redundant with the set_pmd() on non-PAE.
559 */
561 pmd = pmd_offset(pgd, address);
562 pmd_k = pmd_offset(pgd_k, address);
563 if (!pmd_present(*pmd_k))
564 goto no_context;
565 set_pmd(pmd, *pmd_k);
566 xen_flush_page_update_queue(); /* flush PMD update */
568 pte_k = pte_offset_kernel(pmd_k, address);
569 if (!pte_present(*pte_k))
570 goto no_context;
571 return;
572 }
573 }