6 #include <linux/magic.h>
7 #include <linux/sched.h>
9 #include <linux/module.h>
13 #include <linux/perf_event.h>
15 #include <linux/prefetch.h>
17 #include <asm/traps.h>
18 #include <asm/pgalloc.h>
20 #include <asm/fixmap.h>
59 if (kprobes_built_in() && !user_mode_vm(regs)) {
85 check_prefetch_opcode(
struct pt_regs *regs,
unsigned char *
instr,
88 unsigned char instr_hi = opcode & 0xf0;
89 unsigned char instr_lo = opcode & 0x0f;
100 return ((instr_lo & 7) == 0x6);
110 return (!
user_mode(regs) || user_64bit_mode(regs));
114 return (instr_lo & 0xC) == 0x4;
117 return !instr_lo || (instr_lo>>1) == 1;
123 *prefetch = (instr_lo == 0xF) &&
124 (opcode == 0x0D || opcode == 0x18);
134 unsigned char *max_instr;
135 unsigned char *
instr;
146 max_instr = instr + 15;
151 while (instr < max_instr) {
159 if (!check_prefetch_opcode(regs, instr, opcode, &prefetch))
175 info.si_addr = (
void __user *)address;
176 if (fault & VM_FAULT_HWPOISON_LARGE)
178 if (fault & VM_FAULT_HWPOISON)
180 info.si_addr_lsb =
lsb;
189 static inline pmd_t *vmalloc_sync_one(
pgd_t *pgd,
unsigned long address)
237 spin_lock(&pgd_lock);
247 spin_unlock(pgt_lock);
252 spin_unlock(&pgd_lock);
280 pgd_paddr = read_cr3();
281 pmd_k = vmalloc_sync_one(
__va(pgd_paddr), address);
296 check_v8086_mode(
struct pt_regs *regs,
unsigned long address,
301 if (!v8086_mode(regs))
309 static bool low_pfn(
unsigned long pfn)
314 static void dump_pagetable(
unsigned long address)
321 #ifdef CONFIG_X86_PAE
360 pgd_t *pgd, *pgd_ref;
361 pud_t *pud, *pud_ref;
424 #ifdef CONFIG_CPU_SUP_AMD
425 static const char errata93_warning[] =
427 "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
428 "******* Working around it, but it may cause SEGVs or burn power.\n"
429 "******* Please consider a BIOS update.\n"
430 "******* Disabling USB legacy in the BIOS may also help.\n";
437 check_v8086_mode(
struct pt_regs *regs,
unsigned long address,
442 static int bad_address(
void *
p)
449 static void dump_pagetable(
unsigned long address)
457 if (bad_address(pgd))
466 if (bad_address(pud))
474 if (bad_address(pmd))
482 if (bad_address(pte))
509 static int is_errata93(
struct pt_regs *regs,
unsigned long address)
511 #if defined(CONFIG_X86_64) && defined(CONFIG_CPU_SUP_AMD)
516 if (address != regs->ip)
519 if ((address >> 32) != 0)
522 address |= 0xffffffff
UL << 32;
523 if ((address >= (
u64)_stext && address <= (
u64)_etext) ||
541 static int is_errata100(
struct pt_regs *regs,
unsigned long address)
544 if ((regs->
cs ==
__USER32_CS || (regs->
cs & (1<<2))) && (address >> 32))
550 static int is_f00f_bug(
struct pt_regs *regs,
unsigned long address)
552 #ifdef CONFIG_X86_F00F_BUG
570 static const char nx_warning[] =
KERN_CRIT
571 "kernel tried to execute NX-protected page - exploit attempt? (uid: %d)\n";
574 show_fault_oops(
struct pt_regs *regs,
unsigned long error_code,
575 unsigned long address)
580 if (error_code & PF_INSTR) {
599 dump_pagetable(address);
603 pgtable_bad(
struct pt_regs *regs,
unsigned long error_code,
604 unsigned long address)
616 dump_pagetable(address);
622 if (
__die(
"Bad pagetable", regs, error_code))
629 no_context(
struct pt_regs *regs,
unsigned long error_code,
630 unsigned long address,
int signal,
int si_code)
633 unsigned long *stackend;
645 force_sig_info_fault(signal, si_code, address, tsk, 0);
661 if (is_prefetch(regs, error_code, address))
664 if (is_errata93(regs, address))
673 show_fault_oops(regs, error_code, address);
684 if (
__die(
"Oops", regs, error_code))
698 show_signal_msg(
struct pt_regs *regs,
unsigned long error_code,
704 if (!printk_ratelimit())
707 printk(
"%s%s[%d]: segfault at %lx ip %p sp %p error %lx",
709 tsk->
comm, task_pid_nr(tsk), address,
710 (
void *)regs->ip, (
void *)regs->
sp, error_code);
718 __bad_area_nosemaphore(
struct pt_regs *regs,
unsigned long error_code,
719 unsigned long address,
int si_code)
734 if (is_prefetch(regs, error_code, address))
737 if (is_errata100(regs, address))
745 if (
unlikely((error_code & PF_INSTR) &&
753 show_signal_msg(regs, error_code, address, tsk);
760 force_sig_info_fault(
SIGSEGV, si_code, address, tsk, 0);
765 if (is_f00f_bug(regs, address))
768 no_context(regs, error_code, address,
SIGSEGV, si_code);
772 bad_area_nosemaphore(
struct pt_regs *regs,
unsigned long error_code,
773 unsigned long address)
775 __bad_area_nosemaphore(regs, error_code, address,
SEGV_MAPERR);
779 __bad_area(
struct pt_regs *regs,
unsigned long error_code,
780 unsigned long address,
int si_code)
790 __bad_area_nosemaphore(regs, error_code, address, si_code);
794 bad_area(
struct pt_regs *regs,
unsigned long error_code,
unsigned long address)
796 __bad_area(regs, error_code, address,
SEGV_MAPERR);
800 bad_area_access_error(
struct pt_regs *regs,
unsigned long error_code,
801 unsigned long address)
803 __bad_area(regs, error_code, address,
SEGV_ACCERR);
808 out_of_memory(
struct pt_regs *regs,
unsigned long error_code,
809 unsigned long address)
821 do_sigbus(
struct pt_regs *regs,
unsigned long error_code,
unsigned long address,
831 if (!(error_code & PF_USER)) {
837 if (is_prefetch(regs, error_code, address))
844 #ifdef CONFIG_MEMORY_FAILURE
845 if (fault & (VM_FAULT_HWPOISON|VM_FAULT_HWPOISON_LARGE)) {
847 "MCE: Killing %s:%d due to hardware memory corruption fault at %lx\n",
852 force_sig_info_fault(
SIGBUS, code, address, tsk, fault);
856 mm_fault_error(
struct pt_regs *regs,
unsigned long error_code,
857 unsigned long address,
unsigned int fault)
863 if (fatal_signal_pending(
current)) {
864 if (!(fault & VM_FAULT_RETRY))
866 if (!(error_code & PF_USER))
867 no_context(regs, error_code, address, 0, 0);
870 if (!(fault & VM_FAULT_ERROR))
873 if (fault & VM_FAULT_OOM) {
875 if (!(error_code & PF_USER)) {
877 no_context(regs, error_code, address,
882 out_of_memory(regs, error_code, address);
884 if (fault & (VM_FAULT_SIGBUS|VM_FAULT_HWPOISON|
885 VM_FAULT_HWPOISON_LARGE))
886 do_sigbus(regs, error_code, address, fault);
893 static int spurious_fault_check(
unsigned long error_code,
pte_t *pte)
898 if ((error_code & PF_INSTR) && !
pte_exec(*pte))
917 spurious_fault(
unsigned long error_code,
unsigned long address)
926 if (error_code & (PF_USER |
PF_RSVD))
938 return spurious_fault_check(error_code, (
pte_t *) pud);
945 return spurious_fault_check(error_code, (
pte_t *) pmd);
957 ret = spurious_fault_check(error_code, pte);
965 ret = spurious_fault_check(error_code, (
pte_t *) pmd);
966 WARN_ONCE(!ret,
"PMD has incorrect permission bits\n");
974 access_error(
unsigned long error_code,
struct vm_area_struct *vma)
976 if (error_code & PF_WRITE) {
994 static int fault_in_kernel_space(
unsigned long address)
999 static inline bool smap_violation(
int error_code,
struct pt_regs *regs)
1001 if (error_code & PF_USER)
1016 __do_page_fault(
struct pt_regs *regs,
unsigned long error_code)
1024 unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE |
1025 (write ? FAULT_FLAG_WRITE : 0);
1031 address = read_cr2();
1041 if (
unlikely(kmmio_fault(regs, address)))
1057 if (
unlikely(fault_in_kernel_space(address))) {
1059 if (vmalloc_fault(address) >= 0)
1067 if (spurious_fault(error_code, address))
1071 if (notify_page_fault(regs))
1077 bad_area_nosemaphore(regs, error_code, address);
1083 if (
unlikely(notify_page_fault(regs)))
1092 if (user_mode_vm(regs)) {
1101 pgtable_bad(regs, error_code, address);
1104 if (
unlikely(smap_violation(error_code, regs))) {
1105 bad_area_nosemaphore(regs, error_code, address);
1117 bad_area_nosemaphore(regs, error_code, address);
1138 if ((error_code & PF_USER) == 0 &&
1140 bad_area_nosemaphore(regs, error_code, address);
1156 bad_area(regs, error_code, address);
1162 bad_area(regs, error_code, address);
1165 if (error_code & PF_USER) {
1172 if (
unlikely(address + 65536 + 32 *
sizeof(
unsigned long) < regs->
sp)) {
1173 bad_area(regs, error_code, address);
1178 bad_area(regs, error_code, address);
1187 if (
unlikely(access_error(error_code, vma))) {
1188 bad_area_access_error(regs, error_code, address);
1199 if (
unlikely(fault & (VM_FAULT_RETRY|VM_FAULT_ERROR))) {
1200 if (mm_fault_error(regs, error_code, address, fault))
1209 if (flags & FAULT_FLAG_ALLOW_RETRY) {
1210 if (fault & VM_FAULT_MAJOR) {
1219 if (fault & VM_FAULT_RETRY) {
1222 flags &= ~FAULT_FLAG_ALLOW_RETRY;
1223 flags |= FAULT_FLAG_TRIED;
1228 check_v8086_mode(regs, address, tsk);
1236 exception_enter(regs);
1237 __do_page_fault(regs, error_code);
1238 exception_exit(regs);