2 * Copyright (C) 1995 Linus Torvalds
5 #include <linux/signal.h>
6 #include <linux/sched.h>
7 #include <linux/kernel.h>
8 #include <linux/errno.h>
9 #include <linux/string.h>
10 #include <linux/types.h>
11 #include <linux/ptrace.h>
12 #include <linux/mman.h>
14 #include <linux/smp.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/tty.h>
18 #include <linux/vt_kern.h> /* For unblank_screen() */
19 #include <linux/highmem.h>
20 #include <linux/bootmem.h> /* for max_low_pfn */
21 #include <linux/vmalloc.h>
22 #include <linux/module.h>
23 #include <linux/kprobes.h>
24 #include <linux/uaccess.h>
25 #include <linux/kdebug.h>
27 #include <asm/system.h>
29 #include <asm/segment.h>
32 * Page fault error code bits
33 * bit 0 == 0 means no page found, 1 means protection fault
34 * bit 1 == 0 means read, 1 means write
35 * bit 2 == 0 means kernel, 1 means user-mode
36 * bit 3 == 1 means use of reserved bit detected
37 * bit 4 == 1 means fault was an instruction fetch
39 #define PF_PROT (1<<0)
40 #define PF_WRITE (1<<1)
41 #define PF_USER (1<<2)
42 #define PF_RSVD (1<<3)
43 #define PF_INSTR (1<<4)
45 extern void die(const char *, struct pt_regs
*, long);
47 static inline int notify_page_fault(struct pt_regs
*regs
)
52 /* kprobe_running() needs smp_processor_id() */
53 if (!user_mode_vm(regs
)) {
55 if (kprobe_running() && kprobe_fault_handler(regs
, 14))
67 * Return EIP plus the CS segment base. The segment limit is also
68 * adjusted, clamped to the kernel/user address space (whichever is
69 * appropriate), and returned in *eip_limit.
71 * The segment is checked, because it might have been changed by another
72 * task between the original faulting instruction and here.
74 * If CS is no longer a valid code segment, or if EIP is beyond the
75 * limit, or if it is a kernel address when CS is not a kernel segment,
76 * then the returned value will be greater than *eip_limit.
78 * This is slow, but is very rarely executed.
80 static inline unsigned long get_segment_eip(struct pt_regs
*regs
,
81 unsigned long *eip_limit
)
83 unsigned long ip
= regs
->ip
;
84 unsigned seg
= regs
->cs
& 0xffff;
85 u32 seg_ar
, seg_limit
, base
, *desc
;
87 /* Unlikely, but must come before segment checks. */
88 if (unlikely(regs
->flags
& VM_MASK
)) {
90 *eip_limit
= base
+ 0xffff;
91 return base
+ (ip
& 0xffff);
94 /* The standard kernel/user address space limit. */
95 *eip_limit
= user_mode(regs
) ? USER_DS
.seg
: KERNEL_DS
.seg
;
97 /* By far the most common cases. */
98 if (likely(SEGMENT_IS_FLAT_CODE(seg
)))
101 /* Check the segment exists, is within the current LDT/GDT size,
102 that kernel/user (ring 0..3) has the appropriate privilege,
103 that it's a code segment, and get the limit. */
104 __asm__ ("larl %3,%0; lsll %3,%1"
105 : "=&r" (seg_ar
), "=r" (seg_limit
) : "0" (0), "rm" (seg
));
106 if ((~seg_ar
& 0x9800) || ip
> seg_limit
) {
108 return 1; /* So that returned ip > *eip_limit. */
111 /* Get the GDT/LDT descriptor base.
112 When you look for races in this code remember that
113 LDT and other horrors are only used in user space. */
115 /* Must lock the LDT while reading it. */
116 mutex_lock(¤t
->mm
->context
.lock
);
117 desc
= current
->mm
->context
.ldt
;
118 desc
= (void *)desc
+ (seg
& ~7);
120 /* Must disable preemption while reading the GDT. */
121 desc
= (u32
*)get_cpu_gdt_table(get_cpu());
122 desc
= (void *)desc
+ (seg
& ~7);
125 /* Decode the code segment base from the descriptor */
126 base
= get_desc_base((struct desc_struct
*)desc
);
129 mutex_unlock(¤t
->mm
->context
.lock
);
133 /* Adjust EIP and segment limit, and clamp at the kernel limit.
134 It's legitimate for segments to wrap at 0xffffffff. */
136 if (seg_limit
< *eip_limit
&& seg_limit
>= base
)
137 *eip_limit
= seg_limit
;
142 * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
143 * Check that here and ignore it.
145 static int __is_prefetch(struct pt_regs
*regs
, unsigned long addr
)
148 unsigned char *instr
= (unsigned char *)get_segment_eip(regs
, &limit
);
153 for (i
= 0; scan_more
&& i
< 15; i
++) {
154 unsigned char opcode
;
155 unsigned char instr_hi
;
156 unsigned char instr_lo
;
158 if (instr
> (unsigned char *)limit
)
160 if (probe_kernel_address(instr
, opcode
))
163 instr_hi
= opcode
& 0xf0;
164 instr_lo
= opcode
& 0x0f;
171 * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
172 * In X86_64 long mode, the CPU will signal invalid
173 * opcode if some of these prefixes are present so
174 * X86_64 will never get here anyway
176 scan_more
= ((instr_lo
& 7) == 0x6);
181 * In AMD64 long mode 0x40..0x4F are valid REX prefixes
182 * Need to figure out under what instruction mode the
183 * instruction was issued. Could check the LDT for lm,
184 * but for now it's good enough to assume that long
185 * mode only uses well known segments or kernel.
187 scan_more
= (!user_mode(regs
)) || (regs
->cs
== __USER_CS
);
191 /* 0x64 thru 0x67 are valid prefixes in all modes. */
192 scan_more
= (instr_lo
& 0xC) == 0x4;
195 /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
196 scan_more
= !instr_lo
|| (instr_lo
>>1) == 1;
199 /* Prefetch instruction is 0x0F0D or 0x0F18 */
201 if (instr
> (unsigned char *)limit
)
203 if (probe_kernel_address(instr
, opcode
))
205 prefetch
= (instr_lo
== 0xF) &&
206 (opcode
== 0x0D || opcode
== 0x18);
216 static inline int is_prefetch(struct pt_regs
*regs
, unsigned long addr
,
217 unsigned long error_code
)
219 if (unlikely(boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
&&
220 boot_cpu_data
.x86
>= 6)) {
221 /* Catch an obscure case of prefetch inside an NX page. */
222 if (nx_enabled
&& (error_code
& 16))
224 return __is_prefetch(regs
, addr
);
229 static noinline
void force_sig_info_fault(int si_signo
, int si_code
,
230 unsigned long address
, struct task_struct
*tsk
)
234 info
.si_signo
= si_signo
;
236 info
.si_code
= si_code
;
237 info
.si_addr
= (void __user
*)address
;
238 force_sig_info(si_signo
, &info
, tsk
);
241 void do_invalid_op(struct pt_regs
*, unsigned long);
243 static inline pmd_t
*vmalloc_sync_one(pgd_t
*pgd
, unsigned long address
)
245 unsigned index
= pgd_index(address
);
251 pgd_k
= init_mm
.pgd
+ index
;
253 if (!pgd_present(*pgd_k
))
257 * set_pgd(pgd, *pgd_k); here would be useless on PAE
258 * and redundant with the set_pmd() on non-PAE. As would
262 pud
= pud_offset(pgd
, address
);
263 pud_k
= pud_offset(pgd_k
, address
);
264 if (!pud_present(*pud_k
))
267 pmd
= pmd_offset(pud
, address
);
268 pmd_k
= pmd_offset(pud_k
, address
);
269 if (!pmd_present(*pmd_k
))
271 if (!pmd_present(*pmd
)) {
272 set_pmd(pmd
, *pmd_k
);
273 arch_flush_lazy_mmu_mode();
275 BUG_ON(pmd_page(*pmd
) != pmd_page(*pmd_k
));
280 * Handle a fault on the vmalloc or module mapping area
282 * This assumes no large pages in there.
284 static inline int vmalloc_fault(unsigned long address
)
286 unsigned long pgd_paddr
;
290 * Synchronize this task's top level page-table
291 * with the 'reference' page table.
293 * Do _not_ use "current" here. We might be inside
294 * an interrupt in the middle of a task switch..
296 pgd_paddr
= read_cr3();
297 pmd_k
= vmalloc_sync_one(__va(pgd_paddr
), address
);
300 pte_k
= pte_offset_kernel(pmd_k
, address
);
301 if (!pte_present(*pte_k
))
306 int show_unhandled_signals
= 1;
309 * This routine handles page faults. It determines the address,
310 * and the problem, and then passes it off to one of the appropriate
313 void __kprobes
do_page_fault(struct pt_regs
*regs
, unsigned long error_code
)
315 struct task_struct
*tsk
;
316 struct mm_struct
*mm
;
317 struct vm_area_struct
*vma
;
318 unsigned long address
;
323 * We can fault from pretty much anywhere, with unknown IRQ state.
325 trace_hardirqs_fixup();
327 /* get the address */
328 address
= read_cr2();
332 si_code
= SEGV_MAPERR
;
335 * We fault-in kernel-space virtual memory on-demand. The
336 * 'reference' page table is init_mm.pgd.
338 * NOTE! We MUST NOT take any locks for this case. We may
339 * be in an interrupt or a critical region, and should
340 * only copy the information from the master page table,
343 * This verifies that the fault happens in kernel space
344 * (error_code & 4) == 0, and that the fault was not a
345 * protection error (error_code & 9) == 0.
347 if (unlikely(address
>= TASK_SIZE
)) {
348 if (!(error_code
& 0x0000000d) && vmalloc_fault(address
) >= 0)
350 if (notify_page_fault(regs
))
353 * Don't take the mm semaphore here. If we fixup a prefetch
354 * fault we could otherwise deadlock.
356 goto bad_area_nosemaphore
;
359 if (notify_page_fault(regs
))
362 /* It's safe to allow irq's after cr2 has been saved and the vmalloc
363 fault has been handled. */
364 if (regs
->flags
& (X86_EFLAGS_IF
|VM_MASK
))
370 * If we're in an interrupt, have no user context or are running in an
371 * atomic region then we must not take the fault.
373 if (in_atomic() || !mm
)
374 goto bad_area_nosemaphore
;
376 /* When running in the kernel we expect faults to occur only to
377 * addresses in user space. All other faults represent errors in the
378 * kernel and should generate an OOPS. Unfortunately, in the case of an
379 * erroneous fault occurring in a code path which already holds mmap_sem
380 * we will deadlock attempting to validate the fault against the
381 * address space. Luckily the kernel only validly references user
382 * space from well defined areas of code, which are listed in the
385 * As the vast majority of faults will be valid we will only perform
386 * the source reference check when there is a possibility of a deadlock.
387 * Attempt to lock the address space, if we cannot we then validate the
388 * source. If this is invalid we can skip the address space check,
389 * thus avoiding the deadlock.
391 if (!down_read_trylock(&mm
->mmap_sem
)) {
392 if ((error_code
& PF_USER
) == 0 &&
393 !search_exception_tables(regs
->ip
))
394 goto bad_area_nosemaphore
;
395 down_read(&mm
->mmap_sem
);
398 vma
= find_vma(mm
, address
);
401 if (vma
->vm_start
<= address
)
403 if (!(vma
->vm_flags
& VM_GROWSDOWN
))
405 if (error_code
& PF_USER
) {
407 * Accessing the stack below %sp is always a bug.
408 * The large cushion allows instructions like enter
409 * and pusha to work. ("enter $65535,$31" pushes
410 * 32 pointers and then decrements %sp by 65535.)
412 if (address
+ 65536 + 32 * sizeof(unsigned long) < regs
->sp
)
415 if (expand_stack(vma
, address
))
418 * Ok, we have a good vm_area for this memory access, so
422 si_code
= SEGV_ACCERR
;
424 switch (error_code
& (PF_PROT
|PF_WRITE
)) {
425 default: /* 3: write, present */
427 case PF_WRITE
: /* write, not present */
428 if (!(vma
->vm_flags
& VM_WRITE
))
432 case PF_PROT
: /* read, present */
434 case 0: /* read, not present */
435 if (!(vma
->vm_flags
& (VM_READ
| VM_EXEC
| VM_WRITE
)))
441 * If for any reason at all we couldn't handle the fault,
442 * make sure we exit gracefully rather than endlessly redo
445 fault
= handle_mm_fault(mm
, vma
, address
, write
);
446 if (unlikely(fault
& VM_FAULT_ERROR
)) {
447 if (fault
& VM_FAULT_OOM
)
449 else if (fault
& VM_FAULT_SIGBUS
)
453 if (fault
& VM_FAULT_MAJOR
)
459 * Did it hit the DOS screen memory VA from vm86 mode?
461 if (regs
->flags
& VM_MASK
) {
462 unsigned long bit
= (address
- 0xA0000) >> PAGE_SHIFT
;
464 tsk
->thread
.screen_bitmap
|= 1 << bit
;
466 up_read(&mm
->mmap_sem
);
470 * Something tried to access memory that isn't in our memory map..
471 * Fix it, but check if it's kernel or user first..
474 up_read(&mm
->mmap_sem
);
476 bad_area_nosemaphore
:
477 /* User mode accesses just cause a SIGSEGV */
478 if (error_code
& PF_USER
) {
480 * It's possible to have interrupts off here.
485 * Valid to do another page fault here because this one came
488 if (is_prefetch(regs
, address
, error_code
))
491 if (show_unhandled_signals
&& unhandled_signal(tsk
, SIGSEGV
) &&
492 printk_ratelimit()) {
493 printk("%s%s[%d]: segfault at %08lx ip %08lx "
494 "sp %08lx error %lx\n",
495 task_pid_nr(tsk
) > 1 ? KERN_INFO
: KERN_EMERG
,
496 tsk
->comm
, task_pid_nr(tsk
), address
, regs
->ip
,
497 regs
->sp
, error_code
);
499 tsk
->thread
.cr2
= address
;
500 /* Kernel addresses are always protection faults */
501 tsk
->thread
.error_code
= error_code
| (address
>= TASK_SIZE
);
502 tsk
->thread
.trap_no
= 14;
503 force_sig_info_fault(SIGSEGV
, si_code
, address
, tsk
);
507 #ifdef CONFIG_X86_F00F_BUG
509 * Pentium F0 0F C7 C8 bug workaround.
511 if (boot_cpu_data
.f00f_bug
) {
514 nr
= (address
- idt_descr
.address
) >> 3;
517 do_invalid_op(regs
, 0);
524 /* Are we prepared to handle this kernel fault? */
525 if (fixup_exception(regs
))
529 * Valid to do another page fault here, because if this fault
530 * had been triggered by is_prefetch fixup_exception would have
533 if (is_prefetch(regs
, address
, error_code
))
537 * Oops. The kernel tried to access some bad page. We'll have to
538 * terminate things with extreme prejudice.
543 if (oops_may_print()) {
544 __typeof__(pte_val(__pte(0))) page
;
546 #ifdef CONFIG_X86_PAE
547 if (error_code
& 16) {
548 pte_t
*pte
= lookup_address(address
);
550 if (pte
&& pte_present(*pte
) && !pte_exec_kernel(*pte
))
551 printk(KERN_CRIT
"kernel tried to execute "
552 "NX-protected page - exploit attempt? "
553 "(uid: %d)\n", current
->uid
);
556 if (address
< PAGE_SIZE
)
557 printk(KERN_ALERT
"BUG: unable to handle kernel NULL "
558 "pointer dereference");
560 printk(KERN_ALERT
"BUG: unable to handle kernel paging"
562 printk(" at virtual address %08lx\n", address
);
563 printk(KERN_ALERT
"printing ip: %08lx ", regs
->ip
);
566 page
= ((__typeof__(page
) *) __va(page
))[address
>> PGDIR_SHIFT
];
567 #ifdef CONFIG_X86_PAE
568 printk("*pdpt = %016Lx ", page
);
569 if ((page
>> PAGE_SHIFT
) < max_low_pfn
570 && page
& _PAGE_PRESENT
) {
572 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PMD_SHIFT
)
573 & (PTRS_PER_PMD
- 1)];
574 printk(KERN_CONT
"*pde = %016Lx ", page
);
578 printk("*pde = %08lx ", page
);
582 * We must not directly access the pte in the highpte
583 * case if the page table is located in highmem.
584 * And let's rather not kmap-atomic the pte, just in case
585 * it's allocated already.
587 if ((page
>> PAGE_SHIFT
) < max_low_pfn
588 && (page
& _PAGE_PRESENT
)
589 && !(page
& _PAGE_PSE
)) {
591 page
= ((__typeof__(page
) *) __va(page
))[(address
>> PAGE_SHIFT
)
592 & (PTRS_PER_PTE
- 1)];
593 printk("*pte = %0*Lx ", sizeof(page
)*2, (u64
)page
);
599 tsk
->thread
.cr2
= address
;
600 tsk
->thread
.trap_no
= 14;
601 tsk
->thread
.error_code
= error_code
;
602 die("Oops", regs
, error_code
);
607 * We ran out of memory, or some other thing happened to us that made
608 * us unable to handle the page fault gracefully.
611 up_read(&mm
->mmap_sem
);
612 if (is_global_init(tsk
)) {
614 down_read(&mm
->mmap_sem
);
617 printk("VM: killing process %s\n", tsk
->comm
);
619 do_group_exit(SIGKILL
);
623 up_read(&mm
->mmap_sem
);
625 /* Kernel mode? Handle exceptions or die */
626 if (!(error_code
& PF_USER
))
629 /* User space => ok to do another page fault */
630 if (is_prefetch(regs
, address
, error_code
))
633 tsk
->thread
.cr2
= address
;
634 tsk
->thread
.error_code
= error_code
;
635 tsk
->thread
.trap_no
= 14;
636 force_sig_info_fault(SIGBUS
, BUS_ADRERR
, address
, tsk
);
639 void vmalloc_sync_all(void)
642 * Note that races in the updates of insync and start aren't
643 * problematic: insync can only get set bits added, and updates to
644 * start are only improving performance (without affecting correctness
647 static DECLARE_BITMAP(insync
, PTRS_PER_PGD
);
648 static unsigned long start
= TASK_SIZE
;
649 unsigned long address
;
651 if (SHARED_KERNEL_PMD
)
654 BUILD_BUG_ON(TASK_SIZE
& ~PGDIR_MASK
);
655 for (address
= start
; address
>= TASK_SIZE
; address
+= PGDIR_SIZE
) {
656 if (!test_bit(pgd_index(address
), insync
)) {
660 spin_lock_irqsave(&pgd_lock
, flags
);
661 for (page
= pgd_list
; page
; page
=
662 (struct page
*)page
->index
)
663 if (!vmalloc_sync_one(page_address(page
),
665 BUG_ON(page
!= pgd_list
);
668 spin_unlock_irqrestore(&pgd_lock
, flags
);
670 set_bit(pgd_index(address
), insync
);
672 if (address
== start
&& test_bit(pgd_index(address
), insync
))
673 start
= address
+ PGDIR_SIZE
;