direct-io.hg

view linux-2.6-xen-sparse/arch/xen/i386/mm/fault.c @ 7745:b9e8654c3f10

Fix Linux fault.c indentation.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Thu Nov 10 19:43:56 2005 +0100 (2005-11-10)
parents 5aa93e3c2a22
children 36ab34f1c31e
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 = get_desc_base((unsigned long *)desc);
120 if (seg & (1<<2)) {
121 up(&current->mm->context.sem);
122 } else
123 put_cpu();
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 }
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;
145 for (i = 0; scan_more && i < 15; i++) {
146 unsigned char opcode;
147 unsigned char instr_hi;
148 unsigned char instr_lo;
150 if (instr > limit)
151 break;
152 if (__get_user(opcode, (unsigned char *) instr))
153 break;
155 instr_hi = opcode & 0xf0;
156 instr_lo = opcode & 0x0f;
157 instr++;
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;
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 }
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 }
205 fastcall void do_invalid_op(struct pt_regs *, unsigned long);
207 #ifdef CONFIG_X86_PAE
208 static void dump_fault_path(unsigned long address)
209 {
210 unsigned long *p, page;
211 unsigned long mfn;
213 preempt_disable();
214 page = __pa(per_cpu(cur_pgd, smp_processor_id()));
215 preempt_enable();
217 p = (unsigned long *)__va(page);
218 p += (address >> 30) * 2;
219 printk(KERN_ALERT "%08lx -> *pde = %08lx:%08lx\n", page, p[1], p[0]);
220 if (p[0] & 1) {
221 mfn = (p[0] >> PAGE_SHIFT) | ((p[1] & 0x7) << 20);
222 page = mfn_to_pfn(mfn) << PAGE_SHIFT;
223 p = (unsigned long *)__va(page);
224 address &= 0x3fffffff;
225 p += (address >> 21) * 2;
226 printk(KERN_ALERT "%08lx -> *pme = %08lx:%08lx\n",
227 page, p[1], p[0]);
228 #ifndef CONFIG_HIGHPTE
229 if (p[0] & 1) {
230 mfn = (p[0] >> PAGE_SHIFT) | ((p[1] & 0x7) << 20);
231 page = mfn_to_pfn(mfn) << PAGE_SHIFT;
232 p = (unsigned long *) __va(page);
233 address &= 0x001fffff;
234 p += (address >> 12) * 2;
235 printk(KERN_ALERT "%08lx -> *pte = %08lx:%08lx\n",
236 page, p[1], p[0]);
237 }
238 #endif
239 }
240 }
241 #else
242 static void dump_fault_path(unsigned long address)
243 {
244 unsigned long page;
246 preempt_disable();
247 page = ((unsigned long *) per_cpu(cur_pgd, smp_processor_id()))
248 [address >> 22];
249 preempt_enable();
251 page = ((unsigned long *) per_cpu(cur_pgd, get_cpu()))
252 [address >> 22];
253 printk(KERN_ALERT "*pde = ma %08lx pa %08lx\n", page,
254 machine_to_phys(page));
255 /*
256 * We must not directly access the pte in the highpte
257 * case, the page table might be allocated in highmem.
258 * And lets rather not kmap-atomic the pte, just in case
259 * it's allocated already.
260 */
261 #ifndef CONFIG_HIGHPTE
262 if (page & 1) {
263 page &= PAGE_MASK;
264 address &= 0x003ff000;
265 page = machine_to_phys(page);
266 page = ((unsigned long *) __va(page))[address >> PAGE_SHIFT];
267 printk(KERN_ALERT "*pte = ma %08lx pa %08lx\n", page,
268 machine_to_phys(page));
269 }
270 #endif
271 }
272 #endif
275 /*
276 * This routine handles page faults. It determines the address,
277 * and the problem, and then passes it off to one of the appropriate
278 * routines.
279 *
280 * error_code:
281 * bit 0 == 0 means no page found, 1 means protection fault
282 * bit 1 == 0 means read, 1 means write
283 * bit 2 == 0 means kernel, 1 means user-mode
284 */
285 fastcall void do_page_fault(struct pt_regs *regs, unsigned long error_code)
286 {
287 struct task_struct *tsk;
288 struct mm_struct *mm;
289 struct vm_area_struct * vma;
290 unsigned long address;
291 int write;
292 siginfo_t info;
294 address = HYPERVISOR_shared_info->vcpu_data[
295 smp_processor_id()].arch.cr2;
297 /* Set the "privileged fault" bit to something sane. */
298 error_code &= ~4;
299 error_code |= (regs->xcs & 2) << 1;
300 if (regs->eflags & X86_EFLAGS_VM)
301 error_code |= 4;
303 if (notify_die(DIE_PAGE_FAULT, "page fault", regs, error_code, 14,
304 SIGSEGV) == NOTIFY_STOP)
305 return;
306 /* It's safe to allow irq's after cr2 has been saved */
307 if (regs->eflags & (X86_EFLAGS_IF|VM_MASK))
308 local_irq_enable();
310 tsk = current;
312 info.si_code = SEGV_MAPERR;
314 /*
315 * We fault-in kernel-space virtual memory on-demand. The
316 * 'reference' page table is init_mm.pgd.
317 *
318 * NOTE! We MUST NOT take any locks for this case. We may
319 * be in an interrupt or a critical region, and should
320 * only copy the information from the master page table,
321 * nothing more.
322 *
323 * This verifies that the fault happens in kernel space
324 * (error_code & 4) == 0, and that the fault was not a
325 * protection error (error_code & 1) == 0.
326 */
327 if (unlikely(address >= TASK_SIZE)) {
328 if (!(error_code & 5))
329 goto vmalloc_fault;
330 /*
331 * Don't take the mm semaphore here. If we fixup a prefetch
332 * fault we could otherwise deadlock.
333 */
334 goto bad_area_nosemaphore;
335 }
337 mm = tsk->mm;
339 /*
340 * If we're in an interrupt, have no user context or are running in an
341 * atomic region then we must not take the fault..
342 */
343 if (in_atomic() || !mm)
344 goto bad_area_nosemaphore;
346 /* When running in the kernel we expect faults to occur only to
347 * addresses in user space. All other faults represent errors in the
348 * kernel and should generate an OOPS. Unfortunatly, in the case of an
349 * erroneous fault occuring in a code path which already holds mmap_sem
350 * we will deadlock attempting to validate the fault against the
351 * address space. Luckily the kernel only validly references user
352 * space from well defined areas of code, which are listed in the
353 * exceptions table.
354 *
355 * As the vast majority of faults will be valid we will only perform
356 * the source reference check when there is a possibilty of a deadlock.
357 * Attempt to lock the address space, if we cannot we then validate the
358 * source. If this is invalid we can skip the address space check,
359 * thus avoiding the deadlock.
360 */
361 if (!down_read_trylock(&mm->mmap_sem)) {
362 if ((error_code & 4) == 0 &&
363 !search_exception_tables(regs->eip))
364 goto bad_area_nosemaphore;
365 down_read(&mm->mmap_sem);
366 }
368 vma = find_vma(mm, address);
369 if (!vma)
370 goto bad_area;
371 if (vma->vm_start <= address)
372 goto good_area;
373 if (!(vma->vm_flags & VM_GROWSDOWN))
374 goto bad_area;
375 if (error_code & 4) {
376 /*
377 * accessing the stack below %esp is always a bug.
378 * The "+ 32" is there due to some instructions (like
379 * pusha) doing post-decrement on the stack and that
380 * doesn't show up until later..
381 */
382 if (address + 32 < regs->esp)
383 goto bad_area;
384 }
385 if (expand_stack(vma, address))
386 goto bad_area;
387 /*
388 * Ok, we have a good vm_area for this memory access, so
389 * we can handle it..
390 */
391 good_area:
392 info.si_code = SEGV_ACCERR;
393 write = 0;
394 switch (error_code & 3) {
395 default: /* 3: write, present */
396 #ifdef TEST_VERIFY_AREA
397 if (regs->cs == KERNEL_CS)
398 printk("WP fault at %08lx\n", regs->eip);
399 #endif
400 /* fall through */
401 case 2: /* write, not present */
402 if (!(vma->vm_flags & VM_WRITE))
403 goto bad_area;
404 write++;
405 break;
406 case 1: /* read, present */
407 goto bad_area;
408 case 0: /* read, not present */
409 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
410 goto bad_area;
411 }
413 survive:
414 /*
415 * If for any reason at all we couldn't handle the fault,
416 * make sure we exit gracefully rather than endlessly redo
417 * the fault.
418 */
419 switch (handle_mm_fault(mm, vma, address, write)) {
420 case VM_FAULT_MINOR:
421 tsk->min_flt++;
422 break;
423 case VM_FAULT_MAJOR:
424 tsk->maj_flt++;
425 break;
426 case VM_FAULT_SIGBUS:
427 goto do_sigbus;
428 case VM_FAULT_OOM:
429 goto out_of_memory;
430 default:
431 BUG();
432 }
434 /*
435 * Did it hit the DOS screen memory VA from vm86 mode?
436 */
437 if (regs->eflags & VM_MASK) {
438 unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
439 if (bit < 32)
440 tsk->thread.screen_bitmap |= 1 << bit;
441 }
442 up_read(&mm->mmap_sem);
443 return;
445 /*
446 * Something tried to access memory that isn't in our memory map..
447 * Fix it, but check if it's kernel or user first..
448 */
449 bad_area:
450 up_read(&mm->mmap_sem);
452 bad_area_nosemaphore:
453 /* User mode accesses just cause a SIGSEGV */
454 if (error_code & 4) {
455 /*
456 * Valid to do another page fault here because this one came
457 * from user space.
458 */
459 if (is_prefetch(regs, address, error_code))
460 return;
462 tsk->thread.cr2 = address;
463 /* Kernel addresses are always protection faults */
464 tsk->thread.error_code = error_code | (address >= TASK_SIZE);
465 tsk->thread.trap_no = 14;
466 info.si_signo = SIGSEGV;
467 info.si_errno = 0;
468 /* info.si_code has been set above */
469 info.si_addr = (void __user *)address;
470 force_sig_info(SIGSEGV, &info, tsk);
471 return;
472 }
474 #ifdef CONFIG_X86_F00F_BUG
475 /*
476 * Pentium F0 0F C7 C8 bug workaround.
477 */
478 if (boot_cpu_data.f00f_bug) {
479 unsigned long nr;
481 nr = (address - idt_descr.address) >> 3;
483 if (nr == 6) {
484 do_invalid_op(regs, 0);
485 return;
486 }
487 }
488 #endif
490 no_context:
491 /* Are we prepared to handle this kernel fault? */
492 if (fixup_exception(regs))
493 return;
495 /*
496 * Valid to do another page fault here, because if this fault
497 * had been triggered by is_prefetch fixup_exception would have
498 * handled it.
499 */
500 if (is_prefetch(regs, address, error_code))
501 return;
503 /*
504 * Oops. The kernel tried to access some bad page. We'll have to
505 * terminate things with extreme prejudice.
506 */
508 bust_spinlocks(1);
510 #ifdef CONFIG_X86_PAE
511 if (error_code & 16) {
512 pte_t *pte = lookup_address(address);
514 if (pte && pte_present(*pte) && !pte_exec_kernel(*pte))
515 printk(KERN_CRIT "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n", current->uid);
516 }
517 #endif
518 if (address < PAGE_SIZE)
519 printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
520 else
521 printk(KERN_ALERT "Unable to handle kernel paging request");
522 printk(" at virtual address %08lx\n",address);
523 printk(KERN_ALERT " printing eip:\n");
524 printk("%08lx\n", regs->eip);
525 dump_fault_path(address);
526 die("Oops", regs, error_code);
527 bust_spinlocks(0);
528 do_exit(SIGKILL);
530 /*
531 * We ran out of memory, or some other thing happened to us that made
532 * us unable to handle the page fault gracefully.
533 */
534 out_of_memory:
535 up_read(&mm->mmap_sem);
536 if (tsk->pid == 1) {
537 yield();
538 down_read(&mm->mmap_sem);
539 goto survive;
540 }
541 printk("VM: killing process %s\n", tsk->comm);
542 if (error_code & 4)
543 do_exit(SIGKILL);
544 goto no_context;
546 do_sigbus:
547 up_read(&mm->mmap_sem);
549 /* Kernel mode? Handle exceptions or die */
550 if (!(error_code & 4))
551 goto no_context;
553 /* User space => ok to do another page fault */
554 if (is_prefetch(regs, address, error_code))
555 return;
557 tsk->thread.cr2 = address;
558 tsk->thread.error_code = error_code;
559 tsk->thread.trap_no = 14;
560 info.si_signo = SIGBUS;
561 info.si_errno = 0;
562 info.si_code = BUS_ADRERR;
563 info.si_addr = (void __user *)address;
564 force_sig_info(SIGBUS, &info, tsk);
565 return;
567 vmalloc_fault:
568 {
569 /*
570 * Synchronize this task's top level page-table
571 * with the 'reference' page table.
572 *
573 * Do _not_ use "tsk" here. We might be inside
574 * an interrupt in the middle of a task switch..
575 */
576 int index = pgd_index(address);
577 pgd_t *pgd, *pgd_k;
578 pud_t *pud, *pud_k;
579 pmd_t *pmd, *pmd_k;
580 pte_t *pte_k;
582 preempt_disable();
583 pgd = index + per_cpu(cur_pgd, smp_processor_id());
584 preempt_enable();
585 pgd_k = init_mm.pgd + index;
587 if (!pgd_present(*pgd_k))
588 goto no_context;
590 /*
591 * set_pgd(pgd, *pgd_k); here would be useless on PAE
592 * and redundant with the set_pmd() on non-PAE. As would
593 * set_pud.
594 */
596 pud = pud_offset(pgd, address);
597 pud_k = pud_offset(pgd_k, address);
598 if (!pud_present(*pud_k))
599 goto no_context;
601 pmd = pmd_offset(pud, address);
602 pmd_k = pmd_offset(pud_k, address);
603 if (!pmd_present(*pmd_k))
604 goto no_context;
605 #ifndef CONFIG_XEN
606 set_pmd(pmd, *pmd_k);
607 #else
608 /*
609 * When running on Xen we must launder *pmd_k through
610 * pmd_val() to ensure that _PAGE_PRESENT is correctly set.
611 */
612 set_pmd(pmd, __pmd(pmd_val(*pmd_k)));
613 #endif
615 pte_k = pte_offset_kernel(pmd_k, address);
616 if (!pte_present(*pte_k))
617 goto no_context;
618 return;
619 }
620 }