3e36614627396a32c693e1ac2e2bd846587efc19
2 * Copyright (C) 1995 Linus Torvalds
3 * Copyright (C) 2001, 2002 Andi Kleen, SuSE Labs.
5 #include <linux/interrupt.h>
6 #include <linux/mmiotrace.h>
7 #include <linux/bootmem.h>
8 #include <linux/compiler.h>
9 #include <linux/highmem.h>
10 #include <linux/kprobes.h>
11 #include <linux/uaccess.h>
12 #include <linux/vmalloc.h>
13 #include <linux/vt_kern.h>
14 #include <linux/signal.h>
15 #include <linux/kernel.h>
16 #include <linux/ptrace.h>
17 #include <linux/string.h>
18 #include <linux/module.h>
19 #include <linux/kdebug.h>
20 #include <linux/errno.h>
21 #include <linux/magic.h>
22 #include <linux/sched.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/mman.h>
26 #include <linux/tty.h>
27 #include <linux/smp.h>
30 #include <asm-generic/sections.h>
32 #include <asm/tlbflush.h>
33 #include <asm/pgalloc.h>
34 #include <asm/segment.h>
35 #include <asm/system.h>
36 #include <asm/proto.h>
37 #include <asm/traps.h>
41 * Page fault error code bits:
43 * bit 0 == 0: no page found 1: protection fault
44 * bit 1 == 0: read access 1: write access
45 * bit 2 == 0: kernel-mode access 1: user-mode access
46 * bit 3 == 1: use of reserved bit detected
47 * bit 4 == 1: fault was an instruction fetch
49 enum x86_pf_error_code
{
58 static inline int kmmio_fault(struct pt_regs
*regs
, unsigned long addr
)
60 #ifdef CONFIG_MMIOTRACE
61 if (unlikely(is_kmmio_active()))
62 if (kmmio_handler(regs
, addr
) == 1)
68 static inline int notify_page_fault(struct pt_regs
*regs
)
73 /* kprobe_running() needs smp_processor_id() */
74 if (!user_mode_vm(regs
)) {
76 if (kprobe_running() && kprobe_fault_handler(regs
, 14))
92 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
93 * Check that here and ignore it.
97 * Sometimes the CPU reports invalid exceptions on prefetch.
98 * Check that here and ignore it.
100 * Opcode checker based on code by Richard Brunner.
103 check_prefetch_opcode(struct pt_regs
*regs
, unsigned char *instr
,
104 unsigned char opcode
, int *prefetch
)
106 unsigned char instr_hi
= opcode
& 0xf0;
107 unsigned char instr_lo
= opcode
& 0x0f;
113 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
114 * In X86_64 long mode, the CPU will signal invalid
115 * opcode if some of these prefixes are present so
116 * X86_64 will never get here anyway
118 return ((instr_lo
& 7) == 0x6);
122 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
123 * Need to figure out under what instruction mode the
124 * instruction was issued. Could check the LDT for lm,
125 * but for now it's good enough to assume that long
126 * mode only uses well known segments or kernel.
128 return (!user_mode(regs
)) || (regs
->cs
== __USER_CS
);
131 /* 0x64 thru 0x67 are valid prefixes in all modes. */
132 return (instr_lo
& 0xC) == 0x4;
134 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
135 return !instr_lo
|| (instr_lo
>>1) == 1;
137 /* Prefetch instruction is 0x0F0D or 0x0F18 */
138 if (probe_kernel_address(instr
, opcode
))
141 *prefetch
= (instr_lo
== 0xF) &&
142 (opcode
== 0x0D || opcode
== 0x18);
150 is_prefetch(struct pt_regs
*regs
, unsigned long error_code
, unsigned long addr
)
152 unsigned char *max_instr
;
153 unsigned char *instr
;
157 * If it was a exec (instruction fetch) fault on NX page, then
158 * do not ignore the fault:
160 if (error_code
& PF_INSTR
)
163 instr
= (void *)convert_ip_to_linear(current
, regs
);
164 max_instr
= instr
+ 15;
166 if (user_mode(regs
) && instr
>= (unsigned char *)TASK_SIZE
)
169 while (instr
< max_instr
) {
170 unsigned char opcode
;
172 if (probe_kernel_address(instr
, opcode
))
177 if (!check_prefetch_opcode(regs
, instr
, opcode
, &prefetch
))
184 force_sig_info_fault(int si_signo
, int si_code
, unsigned long address
,
185 struct task_struct
*tsk
)
189 info
.si_signo
= si_signo
;
191 info
.si_code
= si_code
;
192 info
.si_addr
= (void __user
*)address
;
194 force_sig_info(si_signo
, &info
, tsk
);
198 static int bad_address(void *p
)
202 return probe_kernel_address((unsigned long *)p
, dummy
);
206 static void dump_pagetable(unsigned long address
)
209 __typeof__(pte_val(__pte(0))) page
;
212 page
= ((__typeof__(page
) *) __va(page
))[address
>> PGDIR_SHIFT
];
214 #ifdef CONFIG_X86_PAE
215 printk("*pdpt = %016Lx ", page
);
216 if ((page
>> PAGE_SHIFT
) < max_low_pfn
217 && page
& _PAGE_PRESENT
) {
219 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PMD_SHIFT
)
220 & (PTRS_PER_PMD
- 1)];
221 printk(KERN_CONT
"*pde = %016Lx ", page
);
225 printk("*pde = %08lx ", page
);
229 * We must not directly access the pte in the highpte
230 * case if the page table is located in highmem.
231 * And let's rather not kmap-atomic the pte, just in case
232 * it's allocated already:
234 if ((page
>> PAGE_SHIFT
) < max_low_pfn
235 && (page
& _PAGE_PRESENT
)
236 && !(page
& _PAGE_PSE
)) {
239 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PAGE_SHIFT
)
240 & (PTRS_PER_PTE
- 1)];
241 printk("*pte = %0*Lx ", sizeof(page
)*2, (u64
)page
);
245 #else /* CONFIG_X86_64 */
251 pgd
= (pgd_t
*)read_cr3();
253 pgd
= __va((unsigned long)pgd
& PHYSICAL_PAGE_MASK
);
255 pgd
+= pgd_index(address
);
256 if (bad_address(pgd
))
259 printk("PGD %lx ", pgd_val(*pgd
));
261 if (!pgd_present(*pgd
))
264 pud
= pud_offset(pgd
, address
);
265 if (bad_address(pud
))
268 printk("PUD %lx ", pud_val(*pud
));
269 if (!pud_present(*pud
) || pud_large(*pud
))
272 pmd
= pmd_offset(pud
, address
);
273 if (bad_address(pmd
))
276 printk("PMD %lx ", pmd_val(*pmd
));
277 if (!pmd_present(*pmd
) || pmd_large(*pmd
))
280 pte
= pte_offset_kernel(pmd
, address
);
281 if (bad_address(pte
))
284 printk("PTE %lx", pte_val(*pte
));
294 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
296 unsigned index
= pgd_index(address
);
302 pgd_k
= init_mm
.pgd
+ index
;
304 if (!pgd_present(*pgd_k
))
308 * set_pgd(pgd, *pgd_k); here would be useless on PAE
309 * and redundant with the set_pmd() on non-PAE. As would
312 pud
= pud_offset(pgd
, address
);
313 pud_k
= pud_offset(pgd_k
, address
);
314 if (!pud_present(*pud_k
))
317 pmd
= pmd_offset(pud
, address
);
318 pmd_k
= pmd_offset(pud_k
, address
);
319 if (!pmd_present(*pmd_k
))
322 if (!pmd_present(*pmd
)) {
323 set_pmd(pmd
, *pmd_k
);
324 arch_flush_lazy_mmu_mode();
326 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
333 * Did it hit the DOS screen memory VA from vm86 mode?
336 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
337 struct task_struct
*tsk
)
341 if (!v8086_mode(regs
))
344 bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
346 tsk
->thread
.screen_bitmap
|= 1 << bit
;
349 #else /* CONFIG_X86_64: */
351 static const char errata93_warning
[] =
352 KERN_ERR
"******* Your BIOS seems to not contain a fix for K8 errata #93\n"
353 KERN_ERR
"******* Working around it, but it may cause SEGVs or burn power.\n"
354 KERN_ERR
"******* Please consider a BIOS update.\n"
355 KERN_ERR
"******* Disabling USB legacy in the BIOS may also help.\n";
358 * No vm86 mode in 64-bit mode:
361 check_v8086_mode(struct pt_regs
*regs
, unsigned long address
,
362 struct task_struct
*tsk
)
369 * Workaround for K8 erratum #93 & buggy BIOS.
371 * BIOS SMM functions are required to use a specific workaround
372 * to avoid corruption of the 64bit RIP register on C stepping K8.
374 * A lot of BIOS that didn't get tested properly miss this.
376 * The OS sees this as a page fault with the upper 32bits of RIP cleared.
377 * Try to work around it here.
379 * Note we only handle faults in kernel here.
380 * Does nothing on 32-bit.
382 static int is_errata93(struct pt_regs
*regs
, unsigned long address
)
387 if (address
!= regs
->ip
)
390 if ((address
>> 32) != 0)
393 address
|= 0xffffffffUL
<< 32;
394 if ((address
>= (u64
)_stext
&& address
<= (u64
)_etext
) ||
395 (address
>= MODULES_VADDR
&& address
<= MODULES_END
)) {
397 printk(errata93_warning
);
408 * Work around K8 erratum #100 K8 in compat mode occasionally jumps
409 * to illegal addresses >4GB.
411 * We catch this in the page fault handler because these addresses
412 * are not reachable. Just detect this case and return. Any code
413 * segment in LDT is compatibility mode.
415 static int is_errata100(struct pt_regs
*regs
, unsigned long address
)
418 if ((regs
->cs
== __USER32_CS
|| (regs
->cs
& (1<<2))) && (address
>> 32))
424 static int is_f00f_bug(struct pt_regs
*regs
, unsigned long address
)
426 #ifdef CONFIG_X86_F00F_BUG
430 * Pentium F0 0F C7 C8 bug workaround:
432 if (boot_cpu_data
.f00f_bug
) {
433 nr
= (address
- idt_descr
.address
) >> 3;
436 do_invalid_op(regs
, 0);
445 show_fault_oops(struct pt_regs
*regs
, unsigned long error_code
,
446 unsigned long address
)
449 if (!oops_may_print())
453 #ifdef CONFIG_X86_PAE
454 if (error_code
& PF_INSTR
) {
457 pte_t
*pte
= lookup_address(address
, &level
);
459 if (pte
&& pte_present(*pte
) && !pte_exec(*pte
)) {
460 printk(KERN_CRIT
"kernel tried to execute "
461 "NX-protected page - exploit attempt? "
462 "(uid: %d)\n", current_uid());
467 printk(KERN_ALERT
"BUG: unable to handle kernel ");
468 if (address
< PAGE_SIZE
)
469 printk(KERN_CONT
"NULL pointer dereference");
471 printk(KERN_CONT
"paging request");
473 printk(KERN_CONT
" at %p\n", (void *) address
);
474 printk(KERN_ALERT
"IP:");
475 printk_address(regs
->ip
, 1);
477 dump_pagetable(address
);
481 pgtable_bad(struct pt_regs
*regs
, unsigned long error_code
,
482 unsigned long address
)
484 struct task_struct
*tsk
;
488 flags
= oops_begin();
492 printk(KERN_ALERT
"%s: Corrupted page table at address %lx\n",
494 dump_pagetable(address
);
496 tsk
->thread
.cr2
= address
;
497 tsk
->thread
.trap_no
= 14;
498 tsk
->thread
.error_code
= error_code
;
500 if (__die("Bad pagetable", regs
, error_code
))
503 oops_end(flags
, regs
, sig
);
507 no_context(struct pt_regs
*regs
, unsigned long error_code
,
508 unsigned long address
)
510 struct task_struct
*tsk
= current
;
511 unsigned long *stackend
;
518 /* Are we prepared to handle this kernel fault? */
519 if (fixup_exception(regs
))
525 * Valid to do another page fault here, because if this fault
526 * had been triggered by is_prefetch fixup_exception would have
531 * Hall of shame of CPU/BIOS bugs.
533 if (is_prefetch(regs
, error_code
, address
))
536 if (is_errata93(regs
, address
))
540 * Oops. The kernel tried to access some bad page. We'll have to
541 * terminate things with extreme prejudice:
546 flags
= oops_begin();
549 show_fault_oops(regs
, error_code
, address
);
551 stackend
= end_of_stack(tsk
);
552 if (*stackend
!= STACK_END_MAGIC
)
553 printk(KERN_ALERT
"Thread overran stack, or stack corrupted\n");
555 tsk
->thread
.cr2
= address
;
556 tsk
->thread
.trap_no
= 14;
557 tsk
->thread
.error_code
= error_code
;
560 die("Oops", regs
, error_code
);
565 if (__die("Oops", regs
, error_code
))
568 /* Executive summary in case the body of the oops scrolled away */
569 printk(KERN_EMERG
"CR2: %016lx\n", address
);
571 oops_end(flags
, regs
, sig
);
576 * Print out info about fatal segfaults, if the show_unhandled_signals
580 show_signal_msg(struct pt_regs
*regs
, unsigned long error_code
,
581 unsigned long address
, struct task_struct
*tsk
)
583 if (!unhandled_signal(tsk
, SIGSEGV
))
586 if (!printk_ratelimit())
589 printk(KERN_CONT
"%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
590 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
591 tsk
->comm
, task_pid_nr(tsk
), address
,
592 (void *)regs
->ip
, (void *)regs
->sp
, error_code
);
594 print_vma_addr(KERN_CONT
" in ", regs
->ip
);
596 printk(KERN_CONT
"\n");
600 __bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
601 unsigned long address
, int si_code
)
603 struct task_struct
*tsk
= current
;
605 /* User mode accesses just cause a SIGSEGV */
606 if (error_code
& PF_USER
) {
608 * It's possible to have interrupts off here:
613 * Valid to do another page fault here because this one came
616 if (is_prefetch(regs
, error_code
, address
))
619 if (is_errata100(regs
, address
))
622 if (unlikely(show_unhandled_signals
))
623 show_signal_msg(regs
, error_code
, address
, tsk
);
625 /* Kernel addresses are always protection faults: */
626 tsk
->thread
.cr2
= address
;
627 tsk
->thread
.error_code
= error_code
| (address
>= TASK_SIZE
);
628 tsk
->thread
.trap_no
= 14;
630 force_sig_info_fault(SIGSEGV
, si_code
, address
, tsk
);
635 if (is_f00f_bug(regs
, address
))
638 no_context(regs
, error_code
, address
);
642 bad_area_nosemaphore(struct pt_regs
*regs
, unsigned long error_code
,
643 unsigned long address
)
645 __bad_area_nosemaphore(regs
, error_code
, address
, SEGV_MAPERR
);
649 __bad_area(struct pt_regs
*regs
, unsigned long error_code
,
650 unsigned long address
, int si_code
)
652 struct mm_struct
*mm
= current
->mm
;
655 * Something tried to access memory that isn't in our memory map..
656 * Fix it, but check if it's kernel or user first..
658 up_read(&mm
->mmap_sem
);
660 __bad_area_nosemaphore(regs
, error_code
, address
, si_code
);
664 bad_area(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
666 __bad_area(regs
, error_code
, address
, SEGV_MAPERR
);
670 bad_area_access_error(struct pt_regs
*regs
, unsigned long error_code
,
671 unsigned long address
)
673 __bad_area(regs
, error_code
, address
, SEGV_ACCERR
);
676 /* TODO: fixup for "mm-invoke-oom-killer-from-page-fault.patch" */
678 out_of_memory(struct pt_regs
*regs
, unsigned long error_code
,
679 unsigned long address
)
682 * We ran out of memory, call the OOM killer, and return the userspace
683 * (which will retry the fault, or kill us if we got oom-killed):
685 up_read(¤t
->mm
->mmap_sem
);
687 pagefault_out_of_memory();
691 do_sigbus(struct pt_regs
*regs
, unsigned long error_code
, unsigned long address
)
693 struct task_struct
*tsk
= current
;
694 struct mm_struct
*mm
= tsk
->mm
;
696 up_read(&mm
->mmap_sem
);
698 /* Kernel mode? Handle exceptions or die: */
699 if (!(error_code
& PF_USER
))
700 no_context(regs
, error_code
, address
);
703 /* User space => ok to do another page fault: */
704 if (is_prefetch(regs
, error_code
, address
))
708 tsk
->thread
.cr2
= address
;
709 tsk
->thread
.error_code
= error_code
;
710 tsk
->thread
.trap_no
= 14;
712 force_sig_info_fault(SIGBUS
, BUS_ADRERR
, address
, tsk
);
716 mm_fault_error(struct pt_regs
*regs
, unsigned long error_code
,
717 unsigned long address
, unsigned int fault
)
719 if (fault
& VM_FAULT_OOM
) {
720 out_of_memory(regs
, error_code
, address
);
722 if (fault
& VM_FAULT_SIGBUS
)
723 do_sigbus(regs
, error_code
, address
);
729 static int spurious_fault_check(unsigned long error_code
, pte_t
*pte
)
731 if ((error_code
& PF_WRITE
) && !pte_write(*pte
))
734 if ((error_code
& PF_INSTR
) && !pte_exec(*pte
))
741 * Handle a spurious fault caused by a stale TLB entry.
743 * This allows us to lazily refresh the TLB when increasing the
744 * permissions of a kernel page (RO -> RW or NX -> X). Doing it
745 * eagerly is very expensive since that implies doing a full
746 * cross-processor TLB flush, even if no stale TLB entries exist
747 * on other processors.
749 * There are no security implications to leaving a stale TLB when
750 * increasing the permissions on a page.
753 spurious_fault(unsigned long error_code
, unsigned long address
)
761 /* Reserved-bit violation or user access to kernel space? */
762 if (error_code
& (PF_USER
| PF_RSVD
))
765 pgd
= init_mm
.pgd
+ pgd_index(address
);
766 if (!pgd_present(*pgd
))
769 pud
= pud_offset(pgd
, address
);
770 if (!pud_present(*pud
))
774 return spurious_fault_check(error_code
, (pte_t
*) pud
);
776 pmd
= pmd_offset(pud
, address
);
777 if (!pmd_present(*pmd
))
781 return spurious_fault_check(error_code
, (pte_t
*) pmd
);
783 pte
= pte_offset_kernel(pmd
, address
);
784 if (!pte_present(*pte
))
787 ret
= spurious_fault_check(error_code
, pte
);
792 * Make sure we have permissions in PMD.
793 * If not, then there's a bug in the page tables:
795 ret
= spurious_fault_check(error_code
, (pte_t
*) pmd
);
796 WARN_ONCE(!ret
, "PMD has incorrect permission bits\n");
804 * Handle a fault on the vmalloc or module mapping area
808 * Handle a fault on the vmalloc area
810 * This assumes no large pages in there.
812 static noinline
int vmalloc_fault(unsigned long address
)
815 unsigned long pgd_paddr
;
819 /* Make sure we are in vmalloc area: */
820 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
824 * Synchronize this task's top level page-table
825 * with the 'reference' page table.
827 * Do _not_ use "current" here. We might be inside
828 * an interrupt in the middle of a task switch..
830 pgd_paddr
= read_cr3();
831 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
835 pte_k
= pte_offset_kernel(pmd_k
, address
);
836 if (!pte_present(*pte_k
))
841 pgd_t
*pgd
, *pgd_ref
;
842 pud_t
*pud
, *pud_ref
;
843 pmd_t
*pmd
, *pmd_ref
;
844 pte_t
*pte
, *pte_ref
;
846 /* Make sure we are in vmalloc area: */
847 if (!(address
>= VMALLOC_START
&& address
< VMALLOC_END
))
851 * Copy kernel mappings over when needed. This can also
852 * happen within a race in page table update. In the later
855 pgd
= pgd_offset(current
->active_mm
, address
);
856 pgd_ref
= pgd_offset_k(address
);
857 if (pgd_none(*pgd_ref
))
861 set_pgd(pgd
, *pgd_ref
);
863 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
866 * Below here mismatches are bugs because these lower tables
870 pud
= pud_offset(pgd
, address
);
871 pud_ref
= pud_offset(pgd_ref
, address
);
872 if (pud_none(*pud_ref
))
875 if (pud_none(*pud
) || pud_page_vaddr(*pud
) != pud_page_vaddr(*pud_ref
))
878 pmd
= pmd_offset(pud
, address
);
879 pmd_ref
= pmd_offset(pud_ref
, address
);
880 if (pmd_none(*pmd_ref
))
883 if (pmd_none(*pmd
) || pmd_page(*pmd
) != pmd_page(*pmd_ref
))
886 pte_ref
= pte_offset_kernel(pmd_ref
, address
);
887 if (!pte_present(*pte_ref
))
890 pte
= pte_offset_kernel(pmd
, address
);
893 * Don't use pte_page here, because the mappings can point
894 * outside mem_map, and the NUMA hash lookup cannot handle
897 if (!pte_present(*pte
) || pte_pfn(*pte
) != pte_pfn(*pte_ref
))
904 int show_unhandled_signals
= 1;
907 access_error(unsigned long error_code
, int write
, struct vm_area_struct
*vma
)
910 /* write, present and write, not present: */
911 if (unlikely(!(vma
->vm_flags
& VM_WRITE
)))
917 if (unlikely(error_code
& PF_PROT
))
920 /* read, not present: */
921 if (unlikely(!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
))))
927 static int fault_in_kernel_space(unsigned long address
)
930 return address
>= TASK_SIZE
;
932 return address
>= TASK_SIZE64
;
937 * This routine handles page faults. It determines the address,
938 * and the problem, and then passes it off to one of the appropriate
944 void __kprobes
do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
946 struct vm_area_struct
*vma
;
947 struct task_struct
*tsk
;
948 unsigned long address
;
949 struct mm_struct
*mm
;
956 prefetchw(&mm
->mmap_sem
);
958 /* Get the faulting address: */
959 address
= read_cr2();
961 if (unlikely(kmmio_fault(regs
, address
)))
965 * We fault-in kernel-space virtual memory on-demand. The
966 * 'reference' page table is init_mm.pgd.
968 * NOTE! We MUST NOT take any locks for this case. We may
969 * be in an interrupt or a critical region, and should
970 * only copy the information from the master page table,
973 * This verifies that the fault happens in kernel space
974 * (error_code & 4) == 0, and that the fault was not a
975 * protection error (error_code & 9) == 0.
977 if (unlikely(fault_in_kernel_space(address
))) {
978 if (!(error_code
& (PF_RSVD
|PF_USER
|PF_PROT
)) &&
979 vmalloc_fault(address
) >= 0)
982 /* Can handle a stale RO->RW TLB: */
983 if (spurious_fault(error_code
, address
))
986 /* kprobes don't want to hook the spurious faults: */
987 if (notify_page_fault(regs
))
990 * Don't take the mm semaphore here. If we fixup a prefetch
991 * fault we could otherwise deadlock:
993 bad_area_nosemaphore(regs
, error_code
, address
);
998 /* kprobes don't want to hook the spurious faults: */
999 if (unlikely(notify_page_fault(regs
)))
1002 * It's safe to allow irq's after cr2 has been saved and the
1003 * vmalloc fault has been handled.
1005 * User-mode registers count as a user access even for any
1006 * potential system fault or CPU buglet:
1008 if (user_mode_vm(regs
)) {
1010 error_code
|= PF_USER
;
1012 if (regs
->flags
& X86_EFLAGS_IF
)
1016 if (unlikely(error_code
& PF_RSVD
))
1017 pgtable_bad(regs
, error_code
, address
);
1020 * If we're in an interrupt, have no user context or are running
1021 * in an atomic region then we must not take the fault:
1023 if (unlikely(in_atomic() || !mm
)) {
1024 bad_area_nosemaphore(regs
, error_code
, address
);
1029 * When running in the kernel we expect faults to occur only to
1030 * addresses in user space. All other faults represent errors in
1031 * the kernel and should generate an OOPS. Unfortunately, in the
1032 * case of an erroneous fault occurring in a code path which already
1033 * holds mmap_sem we will deadlock attempting to validate the fault
1034 * against the address space. Luckily the kernel only validly
1035 * references user space from well defined areas of code, which are
1036 * listed in the exceptions table.
1038 * As the vast majority of faults will be valid we will only perform
1039 * the source reference check when there is a possibility of a
1040 * deadlock. Attempt to lock the address space, if we cannot we then
1041 * validate the source. If this is invalid we can skip the address
1042 * space check, thus avoiding the deadlock:
1044 if (unlikely(!down_read_trylock(&mm
->mmap_sem
))) {
1045 if ((error_code
& PF_USER
) == 0 &&
1046 !search_exception_tables(regs
->ip
)) {
1047 bad_area_nosemaphore(regs
, error_code
, address
);
1050 down_read(&mm
->mmap_sem
);
1053 * The above down_read_trylock() might have succeeded in
1054 * which case we'll have missed the might_sleep() from
1060 vma
= find_vma(mm
, address
);
1061 if (unlikely(!vma
)) {
1062 bad_area(regs
, error_code
, address
);
1065 if (likely(vma
->vm_start
<= address
))
1067 if (unlikely(!(vma
->vm_flags
& VM_GROWSDOWN
))) {
1068 bad_area(regs
, error_code
, address
);
1071 if (error_code
& PF_USER
) {
1073 * Accessing the stack below %sp is always a bug.
1074 * The large cushion allows instructions like enter
1075 * and pusha to work. ("enter $65535, $31" pushes
1076 * 32 pointers and then decrements %sp by 65535.)
1078 if (unlikely(address
+ 65536 + 32 * sizeof(unsigned long) < regs
->sp
)) {
1079 bad_area(regs
, error_code
, address
);
1083 if (unlikely(expand_stack(vma
, address
))) {
1084 bad_area(regs
, error_code
, address
);
1089 * Ok, we have a good vm_area for this memory access, so
1090 * we can handle it..
1093 write
= error_code
& PF_WRITE
;
1095 if (unlikely(access_error(error_code
, write
, vma
))) {
1096 bad_area_access_error(regs
, error_code
, address
);
1101 * If for any reason at all we couldn't handle the fault,
1102 * make sure we exit gracefully rather than endlessly redo
1105 fault
= handle_mm_fault(mm
, vma
, address
, write
);
1107 if (unlikely(fault
& VM_FAULT_ERROR
)) {
1108 mm_fault_error(regs
, error_code
, address
, fault
);
1112 if (fault
& VM_FAULT_MAJOR
)
1117 check_v8086_mode(regs
, address
, tsk
);
1119 up_read(&mm
->mmap_sem
);
1122 DEFINE_SPINLOCK(pgd_lock
);
1123 LIST_HEAD(pgd_list
);
1125 void vmalloc_sync_all(void)
1127 unsigned long address
;
1129 #ifdef CONFIG_X86_32
1130 if (SHARED_KERNEL_PMD
)
1133 for (address
= VMALLOC_START
& PMD_MASK
;
1134 address
>= TASK_SIZE
&& address
< FIXADDR_TOP
;
1135 address
+= PMD_SIZE
) {
1137 unsigned long flags
;
1140 spin_lock_irqsave(&pgd_lock
, flags
);
1141 list_for_each_entry(page
, &pgd_list
, lru
) {
1142 if (!vmalloc_sync_one(page_address(page
), address
))
1145 spin_unlock_irqrestore(&pgd_lock
, flags
);
1147 #else /* CONFIG_X86_64 */
1148 for (address
= VMALLOC_START
& PGDIR_MASK
; address
<= VMALLOC_END
;
1149 address
+= PGDIR_SIZE
) {
1151 const pgd_t
*pgd_ref
= pgd_offset_k(address
);
1152 unsigned long flags
;
1155 if (pgd_none(*pgd_ref
))
1158 spin_lock_irqsave(&pgd_lock
, flags
);
1159 list_for_each_entry(page
, &pgd_list
, lru
) {
1161 pgd
= (pgd_t
*)page_address(page
) + pgd_index(address
);
1163 set_pgd(pgd
, *pgd_ref
);
1165 BUG_ON(pgd_page_vaddr(*pgd
) != pgd_page_vaddr(*pgd_ref
));
1167 spin_unlock_irqrestore(&pgd_lock
, flags
);
This page took 0.056974 seconds and 4 git commands to generate.