ia64/xen-unstable

view linux-2.6-xen-sparse/arch/xen/i386/mm/fault.c @ 6283:7f9b024a509e

Actually make suspending SMP domUs work: the previous commit didn't
bring the other vcpus up correctly.

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