1c61e211dSHarvey Harrison /* 2c61e211dSHarvey Harrison * Copyright (C) 1995 Linus Torvalds 3c61e211dSHarvey Harrison * Copyright (C) 2001,2002 Andi Kleen, SuSE Labs. 4c61e211dSHarvey Harrison */ 5c61e211dSHarvey Harrison 6c61e211dSHarvey Harrison #include <linux/signal.h> 7c61e211dSHarvey Harrison #include <linux/sched.h> 8c61e211dSHarvey Harrison #include <linux/kernel.h> 9c61e211dSHarvey Harrison #include <linux/errno.h> 10c61e211dSHarvey Harrison #include <linux/string.h> 11c61e211dSHarvey Harrison #include <linux/types.h> 12c61e211dSHarvey Harrison #include <linux/ptrace.h> 13c61e211dSHarvey Harrison #include <linux/mman.h> 14c61e211dSHarvey Harrison #include <linux/mm.h> 15c61e211dSHarvey Harrison #include <linux/smp.h> 16c61e211dSHarvey Harrison #include <linux/interrupt.h> 17c61e211dSHarvey Harrison #include <linux/init.h> 18c61e211dSHarvey Harrison #include <linux/tty.h> 19c61e211dSHarvey Harrison #include <linux/vt_kern.h> /* For unblank_screen() */ 20c61e211dSHarvey Harrison #include <linux/compiler.h> 21c61e211dSHarvey Harrison #include <linux/highmem.h> 22c61e211dSHarvey Harrison #include <linux/bootmem.h> /* for max_low_pfn */ 23c61e211dSHarvey Harrison #include <linux/vmalloc.h> 24c61e211dSHarvey Harrison #include <linux/module.h> 25c61e211dSHarvey Harrison #include <linux/kprobes.h> 26c61e211dSHarvey Harrison #include <linux/uaccess.h> 27c61e211dSHarvey Harrison #include <linux/kdebug.h> 28c61e211dSHarvey Harrison 29c61e211dSHarvey Harrison #include <asm/system.h> 30c61e211dSHarvey Harrison #include <asm/desc.h> 31c61e211dSHarvey Harrison #include <asm/segment.h> 32c61e211dSHarvey Harrison #include <asm/pgalloc.h> 33c61e211dSHarvey Harrison #include <asm/smp.h> 34c61e211dSHarvey Harrison #include <asm/tlbflush.h> 35c61e211dSHarvey Harrison #include <asm/proto.h> 36c61e211dSHarvey Harrison #include <asm-generic/sections.h> 37c61e211dSHarvey Harrison 38c61e211dSHarvey Harrison /* 39c61e211dSHarvey Harrison * Page fault error code bits 40c61e211dSHarvey Harrison * bit 0 == 0 means no page found, 1 means protection fault 41c61e211dSHarvey Harrison * bit 1 == 0 means read, 1 means write 42c61e211dSHarvey Harrison * bit 2 == 0 means kernel, 1 means user-mode 43c61e211dSHarvey Harrison * bit 3 == 1 means use of reserved bit detected 44c61e211dSHarvey Harrison * bit 4 == 1 means fault was an instruction fetch 45c61e211dSHarvey Harrison */ 46c61e211dSHarvey Harrison #define PF_PROT (1<<0) 47c61e211dSHarvey Harrison #define PF_WRITE (1<<1) 48c61e211dSHarvey Harrison #define PF_USER (1<<2) 49c61e211dSHarvey Harrison #define PF_RSVD (1<<3) 50c61e211dSHarvey Harrison #define PF_INSTR (1<<4) 51c61e211dSHarvey Harrison 52c61e211dSHarvey Harrison static inline int notify_page_fault(struct pt_regs *regs) 53c61e211dSHarvey Harrison { 54c61e211dSHarvey Harrison #ifdef CONFIG_KPROBES 55c61e211dSHarvey Harrison int ret = 0; 56c61e211dSHarvey Harrison 57c61e211dSHarvey Harrison /* kprobe_running() needs smp_processor_id() */ 58c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 59c61e211dSHarvey Harrison if (!user_mode_vm(regs)) { 60c61e211dSHarvey Harrison #else 61c61e211dSHarvey Harrison if (!user_mode(regs)) { 62c61e211dSHarvey Harrison #endif 63c61e211dSHarvey Harrison preempt_disable(); 64c61e211dSHarvey Harrison if (kprobe_running() && kprobe_fault_handler(regs, 14)) 65c61e211dSHarvey Harrison ret = 1; 66c61e211dSHarvey Harrison preempt_enable(); 67c61e211dSHarvey Harrison } 68c61e211dSHarvey Harrison 69c61e211dSHarvey Harrison return ret; 70c61e211dSHarvey Harrison #else 71c61e211dSHarvey Harrison return 0; 72c61e211dSHarvey Harrison #endif 73c61e211dSHarvey Harrison } 74c61e211dSHarvey Harrison 75c61e211dSHarvey Harrison /* 76c61e211dSHarvey Harrison * X86_32 77c61e211dSHarvey Harrison * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch. 78c61e211dSHarvey Harrison * Check that here and ignore it. 79c61e211dSHarvey Harrison * 80c61e211dSHarvey Harrison * X86_64 81c61e211dSHarvey Harrison * Sometimes the CPU reports invalid exceptions on prefetch. 82c61e211dSHarvey Harrison * Check that here and ignore it. 83c61e211dSHarvey Harrison * 84c61e211dSHarvey Harrison * Opcode checker based on code by Richard Brunner 85c61e211dSHarvey Harrison */ 86c61e211dSHarvey Harrison static int is_prefetch(struct pt_regs *regs, unsigned long addr, 87c61e211dSHarvey Harrison unsigned long error_code) 88c61e211dSHarvey Harrison { 89c61e211dSHarvey Harrison unsigned char *instr; 90c61e211dSHarvey Harrison int scan_more = 1; 91c61e211dSHarvey Harrison int prefetch = 0; 92c61e211dSHarvey Harrison unsigned char *max_instr; 93c61e211dSHarvey Harrison 94c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 95b406ac61SHarvey Harrison if (!(__supported_pte_mask & _PAGE_NX)) 96c61e211dSHarvey Harrison return 0; 97c61e211dSHarvey Harrison #endif 98b406ac61SHarvey Harrison 99c61e211dSHarvey Harrison /* If it was a exec fault on NX page, ignore */ 100c61e211dSHarvey Harrison if (error_code & PF_INSTR) 101c61e211dSHarvey Harrison return 0; 102c61e211dSHarvey Harrison 103c61e211dSHarvey Harrison instr = (unsigned char *)convert_ip_to_linear(current, regs); 104c61e211dSHarvey Harrison max_instr = instr + 15; 105c61e211dSHarvey Harrison 106c61e211dSHarvey Harrison if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE) 107c61e211dSHarvey Harrison return 0; 108c61e211dSHarvey Harrison 109c61e211dSHarvey Harrison while (scan_more && instr < max_instr) { 110c61e211dSHarvey Harrison unsigned char opcode; 111c61e211dSHarvey Harrison unsigned char instr_hi; 112c61e211dSHarvey Harrison unsigned char instr_lo; 113c61e211dSHarvey Harrison 114c61e211dSHarvey Harrison if (probe_kernel_address(instr, opcode)) 115c61e211dSHarvey Harrison break; 116c61e211dSHarvey Harrison 117c61e211dSHarvey Harrison instr_hi = opcode & 0xf0; 118c61e211dSHarvey Harrison instr_lo = opcode & 0x0f; 119c61e211dSHarvey Harrison instr++; 120c61e211dSHarvey Harrison 121c61e211dSHarvey Harrison switch (instr_hi) { 122c61e211dSHarvey Harrison case 0x20: 123c61e211dSHarvey Harrison case 0x30: 124c61e211dSHarvey Harrison /* 125c61e211dSHarvey Harrison * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes. 126c61e211dSHarvey Harrison * In X86_64 long mode, the CPU will signal invalid 127c61e211dSHarvey Harrison * opcode if some of these prefixes are present so 128c61e211dSHarvey Harrison * X86_64 will never get here anyway 129c61e211dSHarvey Harrison */ 130c61e211dSHarvey Harrison scan_more = ((instr_lo & 7) == 0x6); 131c61e211dSHarvey Harrison break; 132c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 133c61e211dSHarvey Harrison case 0x40: 134c61e211dSHarvey Harrison /* 135c61e211dSHarvey Harrison * In AMD64 long mode 0x40..0x4F are valid REX prefixes 136c61e211dSHarvey Harrison * Need to figure out under what instruction mode the 137c61e211dSHarvey Harrison * instruction was issued. Could check the LDT for lm, 138c61e211dSHarvey Harrison * but for now it's good enough to assume that long 139c61e211dSHarvey Harrison * mode only uses well known segments or kernel. 140c61e211dSHarvey Harrison */ 141c61e211dSHarvey Harrison scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS); 142c61e211dSHarvey Harrison break; 143c61e211dSHarvey Harrison #endif 144c61e211dSHarvey Harrison case 0x60: 145c61e211dSHarvey Harrison /* 0x64 thru 0x67 are valid prefixes in all modes. */ 146c61e211dSHarvey Harrison scan_more = (instr_lo & 0xC) == 0x4; 147c61e211dSHarvey Harrison break; 148c61e211dSHarvey Harrison case 0xF0: 149c61e211dSHarvey Harrison /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */ 150c61e211dSHarvey Harrison scan_more = !instr_lo || (instr_lo>>1) == 1; 151c61e211dSHarvey Harrison break; 152c61e211dSHarvey Harrison case 0x00: 153c61e211dSHarvey Harrison /* Prefetch instruction is 0x0F0D or 0x0F18 */ 154c61e211dSHarvey Harrison scan_more = 0; 155c61e211dSHarvey Harrison 156c61e211dSHarvey Harrison if (probe_kernel_address(instr, opcode)) 157c61e211dSHarvey Harrison break; 158c61e211dSHarvey Harrison prefetch = (instr_lo == 0xF) && 159c61e211dSHarvey Harrison (opcode == 0x0D || opcode == 0x18); 160c61e211dSHarvey Harrison break; 161c61e211dSHarvey Harrison default: 162c61e211dSHarvey Harrison scan_more = 0; 163c61e211dSHarvey Harrison break; 164c61e211dSHarvey Harrison } 165c61e211dSHarvey Harrison } 166c61e211dSHarvey Harrison return prefetch; 167c61e211dSHarvey Harrison } 168c61e211dSHarvey Harrison 169c61e211dSHarvey Harrison static void force_sig_info_fault(int si_signo, int si_code, 170c61e211dSHarvey Harrison unsigned long address, struct task_struct *tsk) 171c61e211dSHarvey Harrison { 172c61e211dSHarvey Harrison siginfo_t info; 173c61e211dSHarvey Harrison 174c61e211dSHarvey Harrison info.si_signo = si_signo; 175c61e211dSHarvey Harrison info.si_errno = 0; 176c61e211dSHarvey Harrison info.si_code = si_code; 177c61e211dSHarvey Harrison info.si_addr = (void __user *)address; 178c61e211dSHarvey Harrison force_sig_info(si_signo, &info, tsk); 179c61e211dSHarvey Harrison } 180c61e211dSHarvey Harrison 181c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 182c61e211dSHarvey Harrison static int bad_address(void *p) 183c61e211dSHarvey Harrison { 184c61e211dSHarvey Harrison unsigned long dummy; 185c61e211dSHarvey Harrison return probe_kernel_address((unsigned long *)p, dummy); 186c61e211dSHarvey Harrison } 187c61e211dSHarvey Harrison #endif 188c61e211dSHarvey Harrison 189*cae30f82SAdrian Bunk static void dump_pagetable(unsigned long address) 190c61e211dSHarvey Harrison { 191c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 192c61e211dSHarvey Harrison __typeof__(pte_val(__pte(0))) page; 193c61e211dSHarvey Harrison 194c61e211dSHarvey Harrison page = read_cr3(); 195c61e211dSHarvey Harrison page = ((__typeof__(page) *) __va(page))[address >> PGDIR_SHIFT]; 196c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE 197c61e211dSHarvey Harrison printk("*pdpt = %016Lx ", page); 198c61e211dSHarvey Harrison if ((page >> PAGE_SHIFT) < max_low_pfn 199c61e211dSHarvey Harrison && page & _PAGE_PRESENT) { 200c61e211dSHarvey Harrison page &= PAGE_MASK; 201c61e211dSHarvey Harrison page = ((__typeof__(page) *) __va(page))[(address >> PMD_SHIFT) 202c61e211dSHarvey Harrison & (PTRS_PER_PMD - 1)]; 203c61e211dSHarvey Harrison printk(KERN_CONT "*pde = %016Lx ", page); 204c61e211dSHarvey Harrison page &= ~_PAGE_NX; 205c61e211dSHarvey Harrison } 206c61e211dSHarvey Harrison #else 207c61e211dSHarvey Harrison printk("*pde = %08lx ", page); 208c61e211dSHarvey Harrison #endif 209c61e211dSHarvey Harrison 210c61e211dSHarvey Harrison /* 211c61e211dSHarvey Harrison * We must not directly access the pte in the highpte 212c61e211dSHarvey Harrison * case if the page table is located in highmem. 213c61e211dSHarvey Harrison * And let's rather not kmap-atomic the pte, just in case 214c61e211dSHarvey Harrison * it's allocated already. 215c61e211dSHarvey Harrison */ 216c61e211dSHarvey Harrison if ((page >> PAGE_SHIFT) < max_low_pfn 217c61e211dSHarvey Harrison && (page & _PAGE_PRESENT) 218c61e211dSHarvey Harrison && !(page & _PAGE_PSE)) { 219c61e211dSHarvey Harrison page &= PAGE_MASK; 220c61e211dSHarvey Harrison page = ((__typeof__(page) *) __va(page))[(address >> PAGE_SHIFT) 221c61e211dSHarvey Harrison & (PTRS_PER_PTE - 1)]; 222c61e211dSHarvey Harrison printk("*pte = %0*Lx ", sizeof(page)*2, (u64)page); 223c61e211dSHarvey Harrison } 224c61e211dSHarvey Harrison 225c61e211dSHarvey Harrison printk("\n"); 226c61e211dSHarvey Harrison #else /* CONFIG_X86_64 */ 227c61e211dSHarvey Harrison pgd_t *pgd; 228c61e211dSHarvey Harrison pud_t *pud; 229c61e211dSHarvey Harrison pmd_t *pmd; 230c61e211dSHarvey Harrison pte_t *pte; 231c61e211dSHarvey Harrison 232c61e211dSHarvey Harrison pgd = (pgd_t *)read_cr3(); 233c61e211dSHarvey Harrison 234c61e211dSHarvey Harrison pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK); 235c61e211dSHarvey Harrison pgd += pgd_index(address); 236c61e211dSHarvey Harrison if (bad_address(pgd)) goto bad; 237c61e211dSHarvey Harrison printk("PGD %lx ", pgd_val(*pgd)); 238c61e211dSHarvey Harrison if (!pgd_present(*pgd)) goto ret; 239c61e211dSHarvey Harrison 240c61e211dSHarvey Harrison pud = pud_offset(pgd, address); 241c61e211dSHarvey Harrison if (bad_address(pud)) goto bad; 242c61e211dSHarvey Harrison printk("PUD %lx ", pud_val(*pud)); 243b5360222SAndi Kleen if (!pud_present(*pud) || pud_large(*pud)) 244b5360222SAndi Kleen goto ret; 245c61e211dSHarvey Harrison 246c61e211dSHarvey Harrison pmd = pmd_offset(pud, address); 247c61e211dSHarvey Harrison if (bad_address(pmd)) goto bad; 248c61e211dSHarvey Harrison printk("PMD %lx ", pmd_val(*pmd)); 249c61e211dSHarvey Harrison if (!pmd_present(*pmd) || pmd_large(*pmd)) goto ret; 250c61e211dSHarvey Harrison 251c61e211dSHarvey Harrison pte = pte_offset_kernel(pmd, address); 252c61e211dSHarvey Harrison if (bad_address(pte)) goto bad; 253c61e211dSHarvey Harrison printk("PTE %lx", pte_val(*pte)); 254c61e211dSHarvey Harrison ret: 255c61e211dSHarvey Harrison printk("\n"); 256c61e211dSHarvey Harrison return; 257c61e211dSHarvey Harrison bad: 258c61e211dSHarvey Harrison printk("BAD\n"); 259c61e211dSHarvey Harrison #endif 260c61e211dSHarvey Harrison } 261c61e211dSHarvey Harrison 262c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 263c61e211dSHarvey Harrison static inline pmd_t *vmalloc_sync_one(pgd_t *pgd, unsigned long address) 264c61e211dSHarvey Harrison { 265c61e211dSHarvey Harrison unsigned index = pgd_index(address); 266c61e211dSHarvey Harrison pgd_t *pgd_k; 267c61e211dSHarvey Harrison pud_t *pud, *pud_k; 268c61e211dSHarvey Harrison pmd_t *pmd, *pmd_k; 269c61e211dSHarvey Harrison 270c61e211dSHarvey Harrison pgd += index; 271c61e211dSHarvey Harrison pgd_k = init_mm.pgd + index; 272c61e211dSHarvey Harrison 273c61e211dSHarvey Harrison if (!pgd_present(*pgd_k)) 274c61e211dSHarvey Harrison return NULL; 275c61e211dSHarvey Harrison 276c61e211dSHarvey Harrison /* 277c61e211dSHarvey Harrison * set_pgd(pgd, *pgd_k); here would be useless on PAE 278c61e211dSHarvey Harrison * and redundant with the set_pmd() on non-PAE. As would 279c61e211dSHarvey Harrison * set_pud. 280c61e211dSHarvey Harrison */ 281c61e211dSHarvey Harrison 282c61e211dSHarvey Harrison pud = pud_offset(pgd, address); 283c61e211dSHarvey Harrison pud_k = pud_offset(pgd_k, address); 284c61e211dSHarvey Harrison if (!pud_present(*pud_k)) 285c61e211dSHarvey Harrison return NULL; 286c61e211dSHarvey Harrison 287c61e211dSHarvey Harrison pmd = pmd_offset(pud, address); 288c61e211dSHarvey Harrison pmd_k = pmd_offset(pud_k, address); 289c61e211dSHarvey Harrison if (!pmd_present(*pmd_k)) 290c61e211dSHarvey Harrison return NULL; 291c61e211dSHarvey Harrison if (!pmd_present(*pmd)) { 292c61e211dSHarvey Harrison set_pmd(pmd, *pmd_k); 293c61e211dSHarvey Harrison arch_flush_lazy_mmu_mode(); 294c61e211dSHarvey Harrison } else 295c61e211dSHarvey Harrison BUG_ON(pmd_page(*pmd) != pmd_page(*pmd_k)); 296c61e211dSHarvey Harrison return pmd_k; 297c61e211dSHarvey Harrison } 298c61e211dSHarvey Harrison #endif 299c61e211dSHarvey Harrison 300c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 301c61e211dSHarvey Harrison static const char errata93_warning[] = 302c61e211dSHarvey Harrison KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n" 303c61e211dSHarvey Harrison KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n" 304c61e211dSHarvey Harrison KERN_ERR "******* Please consider a BIOS update.\n" 305c61e211dSHarvey Harrison KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n"; 306c61e211dSHarvey Harrison #endif 307c61e211dSHarvey Harrison 308c61e211dSHarvey Harrison /* Workaround for K8 erratum #93 & buggy BIOS. 309c61e211dSHarvey Harrison BIOS SMM functions are required to use a specific workaround 310c61e211dSHarvey Harrison to avoid corruption of the 64bit RIP register on C stepping K8. 311c61e211dSHarvey Harrison A lot of BIOS that didn't get tested properly miss this. 312c61e211dSHarvey Harrison The OS sees this as a page fault with the upper 32bits of RIP cleared. 313c61e211dSHarvey Harrison Try to work around it here. 314c61e211dSHarvey Harrison Note we only handle faults in kernel here. 315c61e211dSHarvey Harrison Does nothing for X86_32 316c61e211dSHarvey Harrison */ 317c61e211dSHarvey Harrison static int is_errata93(struct pt_regs *regs, unsigned long address) 318c61e211dSHarvey Harrison { 319c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 320c61e211dSHarvey Harrison static int warned; 321c61e211dSHarvey Harrison if (address != regs->ip) 322c61e211dSHarvey Harrison return 0; 323c61e211dSHarvey Harrison if ((address >> 32) != 0) 324c61e211dSHarvey Harrison return 0; 325c61e211dSHarvey Harrison address |= 0xffffffffUL << 32; 326c61e211dSHarvey Harrison if ((address >= (u64)_stext && address <= (u64)_etext) || 327c61e211dSHarvey Harrison (address >= MODULES_VADDR && address <= MODULES_END)) { 328c61e211dSHarvey Harrison if (!warned) { 329c61e211dSHarvey Harrison printk(errata93_warning); 330c61e211dSHarvey Harrison warned = 1; 331c61e211dSHarvey Harrison } 332c61e211dSHarvey Harrison regs->ip = address; 333c61e211dSHarvey Harrison return 1; 334c61e211dSHarvey Harrison } 335c61e211dSHarvey Harrison #endif 336c61e211dSHarvey Harrison return 0; 337c61e211dSHarvey Harrison } 338c61e211dSHarvey Harrison 339c61e211dSHarvey Harrison /* 340c61e211dSHarvey Harrison * Work around K8 erratum #100 K8 in compat mode occasionally jumps to illegal 341c61e211dSHarvey Harrison * addresses >4GB. We catch this in the page fault handler because these 342c61e211dSHarvey Harrison * addresses are not reachable. Just detect this case and return. Any code 343c61e211dSHarvey Harrison * segment in LDT is compatibility mode. 344c61e211dSHarvey Harrison */ 345c61e211dSHarvey Harrison static int is_errata100(struct pt_regs *regs, unsigned long address) 346c61e211dSHarvey Harrison { 347c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 348c61e211dSHarvey Harrison if ((regs->cs == __USER32_CS || (regs->cs & (1<<2))) && 349c61e211dSHarvey Harrison (address >> 32)) 350c61e211dSHarvey Harrison return 1; 351c61e211dSHarvey Harrison #endif 352c61e211dSHarvey Harrison return 0; 353c61e211dSHarvey Harrison } 354c61e211dSHarvey Harrison 355c61e211dSHarvey Harrison void do_invalid_op(struct pt_regs *, unsigned long); 356c61e211dSHarvey Harrison 357c61e211dSHarvey Harrison static int is_f00f_bug(struct pt_regs *regs, unsigned long address) 358c61e211dSHarvey Harrison { 359c61e211dSHarvey Harrison #ifdef CONFIG_X86_F00F_BUG 360c61e211dSHarvey Harrison unsigned long nr; 361c61e211dSHarvey Harrison /* 362c61e211dSHarvey Harrison * Pentium F0 0F C7 C8 bug workaround. 363c61e211dSHarvey Harrison */ 364c61e211dSHarvey Harrison if (boot_cpu_data.f00f_bug) { 365c61e211dSHarvey Harrison nr = (address - idt_descr.address) >> 3; 366c61e211dSHarvey Harrison 367c61e211dSHarvey Harrison if (nr == 6) { 368c61e211dSHarvey Harrison do_invalid_op(regs, 0); 369c61e211dSHarvey Harrison return 1; 370c61e211dSHarvey Harrison } 371c61e211dSHarvey Harrison } 372c61e211dSHarvey Harrison #endif 373c61e211dSHarvey Harrison return 0; 374c61e211dSHarvey Harrison } 375c61e211dSHarvey Harrison 376c61e211dSHarvey Harrison static void show_fault_oops(struct pt_regs *regs, unsigned long error_code, 377c61e211dSHarvey Harrison unsigned long address) 378c61e211dSHarvey Harrison { 379c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 380c61e211dSHarvey Harrison if (!oops_may_print()) 381c61e211dSHarvey Harrison return; 382fd40d6e3SHarvey Harrison #endif 383c61e211dSHarvey Harrison 384c61e211dSHarvey Harrison #ifdef CONFIG_X86_PAE 385c61e211dSHarvey Harrison if (error_code & PF_INSTR) { 38693809be8SHarvey Harrison unsigned int level; 387c61e211dSHarvey Harrison pte_t *pte = lookup_address(address, &level); 388c61e211dSHarvey Harrison 389c61e211dSHarvey Harrison if (pte && pte_present(*pte) && !pte_exec(*pte)) 390c61e211dSHarvey Harrison printk(KERN_CRIT "kernel tried to execute " 391c61e211dSHarvey Harrison "NX-protected page - exploit attempt? " 392c61e211dSHarvey Harrison "(uid: %d)\n", current->uid); 393c61e211dSHarvey Harrison } 394c61e211dSHarvey Harrison #endif 395fd40d6e3SHarvey Harrison 396c61e211dSHarvey Harrison printk(KERN_ALERT "BUG: unable to handle kernel "); 397c61e211dSHarvey Harrison if (address < PAGE_SIZE) 398c61e211dSHarvey Harrison printk(KERN_CONT "NULL pointer dereference"); 399c61e211dSHarvey Harrison else 400c61e211dSHarvey Harrison printk(KERN_CONT "paging request"); 401fd40d6e3SHarvey Harrison #ifdef CONFIG_X86_32 402c61e211dSHarvey Harrison printk(KERN_CONT " at %08lx\n", address); 403fd40d6e3SHarvey Harrison #else 404c61e211dSHarvey Harrison printk(KERN_CONT " at %016lx\n", address); 405fd40d6e3SHarvey Harrison #endif 406c61e211dSHarvey Harrison printk(KERN_ALERT "IP:"); 407c61e211dSHarvey Harrison printk_address(regs->ip, 1); 408c61e211dSHarvey Harrison dump_pagetable(address); 409c61e211dSHarvey Harrison } 410c61e211dSHarvey Harrison 411c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 412c61e211dSHarvey Harrison static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs, 413c61e211dSHarvey Harrison unsigned long error_code) 414c61e211dSHarvey Harrison { 415c61e211dSHarvey Harrison unsigned long flags = oops_begin(); 416c61e211dSHarvey Harrison struct task_struct *tsk; 417c61e211dSHarvey Harrison 418c61e211dSHarvey Harrison printk(KERN_ALERT "%s: Corrupted page table at address %lx\n", 419c61e211dSHarvey Harrison current->comm, address); 420c61e211dSHarvey Harrison dump_pagetable(address); 421c61e211dSHarvey Harrison tsk = current; 422c61e211dSHarvey Harrison tsk->thread.cr2 = address; 423c61e211dSHarvey Harrison tsk->thread.trap_no = 14; 424c61e211dSHarvey Harrison tsk->thread.error_code = error_code; 425c61e211dSHarvey Harrison if (__die("Bad pagetable", regs, error_code)) 426c61e211dSHarvey Harrison regs = NULL; 427c61e211dSHarvey Harrison oops_end(flags, regs, SIGKILL); 428c61e211dSHarvey Harrison } 429c61e211dSHarvey Harrison #endif 430c61e211dSHarvey Harrison 431d8b57bb7SThomas Gleixner static int spurious_fault_check(unsigned long error_code, pte_t *pte) 432d8b57bb7SThomas Gleixner { 433d8b57bb7SThomas Gleixner if ((error_code & PF_WRITE) && !pte_write(*pte)) 434d8b57bb7SThomas Gleixner return 0; 435d8b57bb7SThomas Gleixner if ((error_code & PF_INSTR) && !pte_exec(*pte)) 436d8b57bb7SThomas Gleixner return 0; 437d8b57bb7SThomas Gleixner 438d8b57bb7SThomas Gleixner return 1; 439d8b57bb7SThomas Gleixner } 440d8b57bb7SThomas Gleixner 441c61e211dSHarvey Harrison /* 4425b727a3bSJeremy Fitzhardinge * Handle a spurious fault caused by a stale TLB entry. This allows 4435b727a3bSJeremy Fitzhardinge * us to lazily refresh the TLB when increasing the permissions of a 4445b727a3bSJeremy Fitzhardinge * kernel page (RO -> RW or NX -> X). Doing it eagerly is very 4455b727a3bSJeremy Fitzhardinge * expensive since that implies doing a full cross-processor TLB 4465b727a3bSJeremy Fitzhardinge * flush, even if no stale TLB entries exist on other processors. 4475b727a3bSJeremy Fitzhardinge * There are no security implications to leaving a stale TLB when 4485b727a3bSJeremy Fitzhardinge * increasing the permissions on a page. 4495b727a3bSJeremy Fitzhardinge */ 4505b727a3bSJeremy Fitzhardinge static int spurious_fault(unsigned long address, 4515b727a3bSJeremy Fitzhardinge unsigned long error_code) 4525b727a3bSJeremy Fitzhardinge { 4535b727a3bSJeremy Fitzhardinge pgd_t *pgd; 4545b727a3bSJeremy Fitzhardinge pud_t *pud; 4555b727a3bSJeremy Fitzhardinge pmd_t *pmd; 4565b727a3bSJeremy Fitzhardinge pte_t *pte; 4575b727a3bSJeremy Fitzhardinge 4585b727a3bSJeremy Fitzhardinge /* Reserved-bit violation or user access to kernel space? */ 4595b727a3bSJeremy Fitzhardinge if (error_code & (PF_USER | PF_RSVD)) 4605b727a3bSJeremy Fitzhardinge return 0; 4615b727a3bSJeremy Fitzhardinge 4625b727a3bSJeremy Fitzhardinge pgd = init_mm.pgd + pgd_index(address); 4635b727a3bSJeremy Fitzhardinge if (!pgd_present(*pgd)) 4645b727a3bSJeremy Fitzhardinge return 0; 4655b727a3bSJeremy Fitzhardinge 4665b727a3bSJeremy Fitzhardinge pud = pud_offset(pgd, address); 4675b727a3bSJeremy Fitzhardinge if (!pud_present(*pud)) 4685b727a3bSJeremy Fitzhardinge return 0; 4695b727a3bSJeremy Fitzhardinge 470d8b57bb7SThomas Gleixner if (pud_large(*pud)) 471d8b57bb7SThomas Gleixner return spurious_fault_check(error_code, (pte_t *) pud); 472d8b57bb7SThomas Gleixner 4735b727a3bSJeremy Fitzhardinge pmd = pmd_offset(pud, address); 4745b727a3bSJeremy Fitzhardinge if (!pmd_present(*pmd)) 4755b727a3bSJeremy Fitzhardinge return 0; 4765b727a3bSJeremy Fitzhardinge 477d8b57bb7SThomas Gleixner if (pmd_large(*pmd)) 478d8b57bb7SThomas Gleixner return spurious_fault_check(error_code, (pte_t *) pmd); 479d8b57bb7SThomas Gleixner 4805b727a3bSJeremy Fitzhardinge pte = pte_offset_kernel(pmd, address); 4815b727a3bSJeremy Fitzhardinge if (!pte_present(*pte)) 4825b727a3bSJeremy Fitzhardinge return 0; 4835b727a3bSJeremy Fitzhardinge 484d8b57bb7SThomas Gleixner return spurious_fault_check(error_code, pte); 4855b727a3bSJeremy Fitzhardinge } 4865b727a3bSJeremy Fitzhardinge 4875b727a3bSJeremy Fitzhardinge /* 488c61e211dSHarvey Harrison * X86_32 489c61e211dSHarvey Harrison * Handle a fault on the vmalloc or module mapping area 490c61e211dSHarvey Harrison * 491c61e211dSHarvey Harrison * X86_64 492c61e211dSHarvey Harrison * Handle a fault on the vmalloc area 493c61e211dSHarvey Harrison * 494c61e211dSHarvey Harrison * This assumes no large pages in there. 495c61e211dSHarvey Harrison */ 496c61e211dSHarvey Harrison static int vmalloc_fault(unsigned long address) 497c61e211dSHarvey Harrison { 498c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 499c61e211dSHarvey Harrison unsigned long pgd_paddr; 500c61e211dSHarvey Harrison pmd_t *pmd_k; 501c61e211dSHarvey Harrison pte_t *pte_k; 502c61e211dSHarvey Harrison /* 503c61e211dSHarvey Harrison * Synchronize this task's top level page-table 504c61e211dSHarvey Harrison * with the 'reference' page table. 505c61e211dSHarvey Harrison * 506c61e211dSHarvey Harrison * Do _not_ use "current" here. We might be inside 507c61e211dSHarvey Harrison * an interrupt in the middle of a task switch.. 508c61e211dSHarvey Harrison */ 509c61e211dSHarvey Harrison pgd_paddr = read_cr3(); 510c61e211dSHarvey Harrison pmd_k = vmalloc_sync_one(__va(pgd_paddr), address); 511c61e211dSHarvey Harrison if (!pmd_k) 512c61e211dSHarvey Harrison return -1; 513c61e211dSHarvey Harrison pte_k = pte_offset_kernel(pmd_k, address); 514c61e211dSHarvey Harrison if (!pte_present(*pte_k)) 515c61e211dSHarvey Harrison return -1; 516c61e211dSHarvey Harrison return 0; 517c61e211dSHarvey Harrison #else 518c61e211dSHarvey Harrison pgd_t *pgd, *pgd_ref; 519c61e211dSHarvey Harrison pud_t *pud, *pud_ref; 520c61e211dSHarvey Harrison pmd_t *pmd, *pmd_ref; 521c61e211dSHarvey Harrison pte_t *pte, *pte_ref; 522c61e211dSHarvey Harrison 523cf89ec92SHarvey Harrison /* Make sure we are in vmalloc area */ 524cf89ec92SHarvey Harrison if (!(address >= VMALLOC_START && address < VMALLOC_END)) 525cf89ec92SHarvey Harrison return -1; 526cf89ec92SHarvey Harrison 527c61e211dSHarvey Harrison /* Copy kernel mappings over when needed. This can also 528c61e211dSHarvey Harrison happen within a race in page table update. In the later 529c61e211dSHarvey Harrison case just flush. */ 530c61e211dSHarvey Harrison 531c61e211dSHarvey Harrison pgd = pgd_offset(current->mm ?: &init_mm, address); 532c61e211dSHarvey Harrison pgd_ref = pgd_offset_k(address); 533c61e211dSHarvey Harrison if (pgd_none(*pgd_ref)) 534c61e211dSHarvey Harrison return -1; 535c61e211dSHarvey Harrison if (pgd_none(*pgd)) 536c61e211dSHarvey Harrison set_pgd(pgd, *pgd_ref); 537c61e211dSHarvey Harrison else 538c61e211dSHarvey Harrison BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref)); 539c61e211dSHarvey Harrison 540c61e211dSHarvey Harrison /* Below here mismatches are bugs because these lower tables 541c61e211dSHarvey Harrison are shared */ 542c61e211dSHarvey Harrison 543c61e211dSHarvey Harrison pud = pud_offset(pgd, address); 544c61e211dSHarvey Harrison pud_ref = pud_offset(pgd_ref, address); 545c61e211dSHarvey Harrison if (pud_none(*pud_ref)) 546c61e211dSHarvey Harrison return -1; 547c61e211dSHarvey Harrison if (pud_none(*pud) || pud_page_vaddr(*pud) != pud_page_vaddr(*pud_ref)) 548c61e211dSHarvey Harrison BUG(); 549c61e211dSHarvey Harrison pmd = pmd_offset(pud, address); 550c61e211dSHarvey Harrison pmd_ref = pmd_offset(pud_ref, address); 551c61e211dSHarvey Harrison if (pmd_none(*pmd_ref)) 552c61e211dSHarvey Harrison return -1; 553c61e211dSHarvey Harrison if (pmd_none(*pmd) || pmd_page(*pmd) != pmd_page(*pmd_ref)) 554c61e211dSHarvey Harrison BUG(); 555c61e211dSHarvey Harrison pte_ref = pte_offset_kernel(pmd_ref, address); 556c61e211dSHarvey Harrison if (!pte_present(*pte_ref)) 557c61e211dSHarvey Harrison return -1; 558c61e211dSHarvey Harrison pte = pte_offset_kernel(pmd, address); 559c61e211dSHarvey Harrison /* Don't use pte_page here, because the mappings can point 560c61e211dSHarvey Harrison outside mem_map, and the NUMA hash lookup cannot handle 561c61e211dSHarvey Harrison that. */ 562c61e211dSHarvey Harrison if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref)) 563c61e211dSHarvey Harrison BUG(); 564c61e211dSHarvey Harrison return 0; 565c61e211dSHarvey Harrison #endif 566c61e211dSHarvey Harrison } 567c61e211dSHarvey Harrison 568c61e211dSHarvey Harrison int show_unhandled_signals = 1; 569c61e211dSHarvey Harrison 570c61e211dSHarvey Harrison /* 571c61e211dSHarvey Harrison * This routine handles page faults. It determines the address, 572c61e211dSHarvey Harrison * and the problem, and then passes it off to one of the appropriate 573c61e211dSHarvey Harrison * routines. 574c61e211dSHarvey Harrison */ 575c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 576c61e211dSHarvey Harrison asmlinkage 577c61e211dSHarvey Harrison #endif 578c61e211dSHarvey Harrison void __kprobes do_page_fault(struct pt_regs *regs, unsigned long error_code) 579c61e211dSHarvey Harrison { 580c61e211dSHarvey Harrison struct task_struct *tsk; 581c61e211dSHarvey Harrison struct mm_struct *mm; 582c61e211dSHarvey Harrison struct vm_area_struct *vma; 583c61e211dSHarvey Harrison unsigned long address; 584c61e211dSHarvey Harrison int write, si_code; 585c61e211dSHarvey Harrison int fault; 586c61e211dSHarvey Harrison #ifdef CONFIG_X86_64 587c61e211dSHarvey Harrison unsigned long flags; 588c61e211dSHarvey Harrison #endif 589c61e211dSHarvey Harrison 590c61e211dSHarvey Harrison /* 591c61e211dSHarvey Harrison * We can fault from pretty much anywhere, with unknown IRQ state. 592c61e211dSHarvey Harrison */ 593c61e211dSHarvey Harrison trace_hardirqs_fixup(); 594c61e211dSHarvey Harrison 595c61e211dSHarvey Harrison tsk = current; 596c61e211dSHarvey Harrison mm = tsk->mm; 597c61e211dSHarvey Harrison prefetchw(&mm->mmap_sem); 598c61e211dSHarvey Harrison 599c61e211dSHarvey Harrison /* get the address */ 600c61e211dSHarvey Harrison address = read_cr2(); 601c61e211dSHarvey Harrison 602c61e211dSHarvey Harrison si_code = SEGV_MAPERR; 603c61e211dSHarvey Harrison 604c61e211dSHarvey Harrison if (notify_page_fault(regs)) 605c61e211dSHarvey Harrison return; 606c61e211dSHarvey Harrison 607c61e211dSHarvey Harrison /* 608c61e211dSHarvey Harrison * We fault-in kernel-space virtual memory on-demand. The 609c61e211dSHarvey Harrison * 'reference' page table is init_mm.pgd. 610c61e211dSHarvey Harrison * 611c61e211dSHarvey Harrison * NOTE! We MUST NOT take any locks for this case. We may 612c61e211dSHarvey Harrison * be in an interrupt or a critical region, and should 613c61e211dSHarvey Harrison * only copy the information from the master page table, 614c61e211dSHarvey Harrison * nothing more. 615c61e211dSHarvey Harrison * 616c61e211dSHarvey Harrison * This verifies that the fault happens in kernel space 617c61e211dSHarvey Harrison * (error_code & 4) == 0, and that the fault was not a 618c61e211dSHarvey Harrison * protection error (error_code & 9) == 0. 619c61e211dSHarvey Harrison */ 620c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 621c61e211dSHarvey Harrison if (unlikely(address >= TASK_SIZE)) { 622cf89ec92SHarvey Harrison #else 623cf89ec92SHarvey Harrison if (unlikely(address >= TASK_SIZE64)) { 624cf89ec92SHarvey Harrison #endif 625c61e211dSHarvey Harrison if (!(error_code & (PF_RSVD|PF_USER|PF_PROT)) && 626c61e211dSHarvey Harrison vmalloc_fault(address) >= 0) 627c61e211dSHarvey Harrison return; 6285b727a3bSJeremy Fitzhardinge 6295b727a3bSJeremy Fitzhardinge /* Can handle a stale RO->RW TLB */ 6305b727a3bSJeremy Fitzhardinge if (spurious_fault(address, error_code)) 6315b727a3bSJeremy Fitzhardinge return; 6325b727a3bSJeremy Fitzhardinge 633c61e211dSHarvey Harrison /* 634c61e211dSHarvey Harrison * Don't take the mm semaphore here. If we fixup a prefetch 635c61e211dSHarvey Harrison * fault we could otherwise deadlock. 636c61e211dSHarvey Harrison */ 637c61e211dSHarvey Harrison goto bad_area_nosemaphore; 638c61e211dSHarvey Harrison } 639c61e211dSHarvey Harrison 640cf89ec92SHarvey Harrison 641cf89ec92SHarvey Harrison #ifdef CONFIG_X86_32 642c61e211dSHarvey Harrison /* It's safe to allow irq's after cr2 has been saved and the vmalloc 643c61e211dSHarvey Harrison fault has been handled. */ 644c61e211dSHarvey Harrison if (regs->flags & (X86_EFLAGS_IF|VM_MASK)) 645c61e211dSHarvey Harrison local_irq_enable(); 646c61e211dSHarvey Harrison 647c61e211dSHarvey Harrison /* 648c61e211dSHarvey Harrison * If we're in an interrupt, have no user context or are running in an 649c61e211dSHarvey Harrison * atomic region then we must not take the fault. 650c61e211dSHarvey Harrison */ 651c61e211dSHarvey Harrison if (in_atomic() || !mm) 652c61e211dSHarvey Harrison goto bad_area_nosemaphore; 653c61e211dSHarvey Harrison #else /* CONFIG_X86_64 */ 654c61e211dSHarvey Harrison if (likely(regs->flags & X86_EFLAGS_IF)) 655c61e211dSHarvey Harrison local_irq_enable(); 656c61e211dSHarvey Harrison 657c61e211dSHarvey Harrison if (unlikely(error_code & PF_RSVD)) 658c61e211dSHarvey Harrison pgtable_bad(address, regs, error_code); 659c61e211dSHarvey Harrison 660c61e211dSHarvey Harrison /* 661c61e211dSHarvey Harrison * If we're in an interrupt, have no user context or are running in an 662c61e211dSHarvey Harrison * atomic region then we must not take the fault. 663c61e211dSHarvey Harrison */ 664c61e211dSHarvey Harrison if (unlikely(in_atomic() || !mm)) 665c61e211dSHarvey Harrison goto bad_area_nosemaphore; 666c61e211dSHarvey Harrison 667c61e211dSHarvey Harrison /* 668c61e211dSHarvey Harrison * User-mode registers count as a user access even for any 669c61e211dSHarvey Harrison * potential system fault or CPU buglet. 670c61e211dSHarvey Harrison */ 671c61e211dSHarvey Harrison if (user_mode_vm(regs)) 672c61e211dSHarvey Harrison error_code |= PF_USER; 673c61e211dSHarvey Harrison again: 674c61e211dSHarvey Harrison #endif 675c61e211dSHarvey Harrison /* When running in the kernel we expect faults to occur only to 676c61e211dSHarvey Harrison * addresses in user space. All other faults represent errors in the 677c61e211dSHarvey Harrison * kernel and should generate an OOPS. Unfortunately, in the case of an 678c61e211dSHarvey Harrison * erroneous fault occurring in a code path which already holds mmap_sem 679c61e211dSHarvey Harrison * we will deadlock attempting to validate the fault against the 680c61e211dSHarvey Harrison * address space. Luckily the kernel only validly references user 681c61e211dSHarvey Harrison * space from well defined areas of code, which are listed in the 682c61e211dSHarvey Harrison * exceptions table. 683c61e211dSHarvey Harrison * 684c61e211dSHarvey Harrison * As the vast majority of faults will be valid we will only perform 685c61e211dSHarvey Harrison * the source reference check when there is a possibility of a deadlock. 686c61e211dSHarvey Harrison * Attempt to lock the address space, if we cannot we then validate the 687c61e211dSHarvey Harrison * source. If this is invalid we can skip the address space check, 688c61e211dSHarvey Harrison * thus avoiding the deadlock. 689c61e211dSHarvey Harrison */ 690c61e211dSHarvey Harrison if (!down_read_trylock(&mm->mmap_sem)) { 691c61e211dSHarvey Harrison if ((error_code & PF_USER) == 0 && 692c61e211dSHarvey Harrison !search_exception_tables(regs->ip)) 693c61e211dSHarvey Harrison goto bad_area_nosemaphore; 694c61e211dSHarvey Harrison down_read(&mm->mmap_sem); 695c61e211dSHarvey Harrison } 696c61e211dSHarvey Harrison 697c61e211dSHarvey Harrison vma = find_vma(mm, address); 698c61e211dSHarvey Harrison if (!vma) 699c61e211dSHarvey Harrison goto bad_area; 700c61e211dSHarvey Harrison if (vma->vm_start <= address) 701c61e211dSHarvey Harrison goto good_area; 702c61e211dSHarvey Harrison if (!(vma->vm_flags & VM_GROWSDOWN)) 703c61e211dSHarvey Harrison goto bad_area; 704c61e211dSHarvey Harrison if (error_code & PF_USER) { 705c61e211dSHarvey Harrison /* 706c61e211dSHarvey Harrison * Accessing the stack below %sp is always a bug. 707c61e211dSHarvey Harrison * The large cushion allows instructions like enter 708c61e211dSHarvey Harrison * and pusha to work. ("enter $65535,$31" pushes 709c61e211dSHarvey Harrison * 32 pointers and then decrements %sp by 65535.) 710c61e211dSHarvey Harrison */ 711c61e211dSHarvey Harrison if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp) 712c61e211dSHarvey Harrison goto bad_area; 713c61e211dSHarvey Harrison } 714c61e211dSHarvey Harrison if (expand_stack(vma, address)) 715c61e211dSHarvey Harrison goto bad_area; 716c61e211dSHarvey Harrison /* 717c61e211dSHarvey Harrison * Ok, we have a good vm_area for this memory access, so 718c61e211dSHarvey Harrison * we can handle it.. 719c61e211dSHarvey Harrison */ 720c61e211dSHarvey Harrison good_area: 721c61e211dSHarvey Harrison si_code = SEGV_ACCERR; 722c61e211dSHarvey Harrison write = 0; 723c61e211dSHarvey Harrison switch (error_code & (PF_PROT|PF_WRITE)) { 724c61e211dSHarvey Harrison default: /* 3: write, present */ 725c61e211dSHarvey Harrison /* fall through */ 726c61e211dSHarvey Harrison case PF_WRITE: /* write, not present */ 727c61e211dSHarvey Harrison if (!(vma->vm_flags & VM_WRITE)) 728c61e211dSHarvey Harrison goto bad_area; 729c61e211dSHarvey Harrison write++; 730c61e211dSHarvey Harrison break; 731c61e211dSHarvey Harrison case PF_PROT: /* read, present */ 732c61e211dSHarvey Harrison goto bad_area; 733c61e211dSHarvey Harrison case 0: /* read, not present */ 734c61e211dSHarvey Harrison if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE))) 735c61e211dSHarvey Harrison goto bad_area; 736c61e211dSHarvey Harrison } 737c61e211dSHarvey Harrison 738c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 739c61e211dSHarvey Harrison survive: 740c61e211dSHarvey Harrison #endif 741c61e211dSHarvey Harrison /* 742c61e211dSHarvey Harrison * If for any reason at all we couldn't handle the fault, 743c61e211dSHarvey Harrison * make sure we exit gracefully rather than endlessly redo 744c61e211dSHarvey Harrison * the fault. 745c61e211dSHarvey Harrison */ 746c61e211dSHarvey Harrison fault = handle_mm_fault(mm, vma, address, write); 747c61e211dSHarvey Harrison if (unlikely(fault & VM_FAULT_ERROR)) { 748c61e211dSHarvey Harrison if (fault & VM_FAULT_OOM) 749c61e211dSHarvey Harrison goto out_of_memory; 750c61e211dSHarvey Harrison else if (fault & VM_FAULT_SIGBUS) 751c61e211dSHarvey Harrison goto do_sigbus; 752c61e211dSHarvey Harrison BUG(); 753c61e211dSHarvey Harrison } 754c61e211dSHarvey Harrison if (fault & VM_FAULT_MAJOR) 755c61e211dSHarvey Harrison tsk->maj_flt++; 756c61e211dSHarvey Harrison else 757c61e211dSHarvey Harrison tsk->min_flt++; 758c61e211dSHarvey Harrison 759c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 760c61e211dSHarvey Harrison /* 761c61e211dSHarvey Harrison * Did it hit the DOS screen memory VA from vm86 mode? 762c61e211dSHarvey Harrison */ 763c61e211dSHarvey Harrison if (v8086_mode(regs)) { 764c61e211dSHarvey Harrison unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT; 765c61e211dSHarvey Harrison if (bit < 32) 766c61e211dSHarvey Harrison tsk->thread.screen_bitmap |= 1 << bit; 767c61e211dSHarvey Harrison } 768c61e211dSHarvey Harrison #endif 769c61e211dSHarvey Harrison up_read(&mm->mmap_sem); 770c61e211dSHarvey Harrison return; 771c61e211dSHarvey Harrison 772c61e211dSHarvey Harrison /* 773c61e211dSHarvey Harrison * Something tried to access memory that isn't in our memory map.. 774c61e211dSHarvey Harrison * Fix it, but check if it's kernel or user first.. 775c61e211dSHarvey Harrison */ 776c61e211dSHarvey Harrison bad_area: 777c61e211dSHarvey Harrison up_read(&mm->mmap_sem); 778c61e211dSHarvey Harrison 779c61e211dSHarvey Harrison bad_area_nosemaphore: 780c61e211dSHarvey Harrison /* User mode accesses just cause a SIGSEGV */ 781c61e211dSHarvey Harrison if (error_code & PF_USER) { 782c61e211dSHarvey Harrison /* 783c61e211dSHarvey Harrison * It's possible to have interrupts off here. 784c61e211dSHarvey Harrison */ 785c61e211dSHarvey Harrison local_irq_enable(); 786c61e211dSHarvey Harrison 787c61e211dSHarvey Harrison /* 788c61e211dSHarvey Harrison * Valid to do another page fault here because this one came 789c61e211dSHarvey Harrison * from user space. 790c61e211dSHarvey Harrison */ 791c61e211dSHarvey Harrison if (is_prefetch(regs, address, error_code)) 792c61e211dSHarvey Harrison return; 793c61e211dSHarvey Harrison 794c61e211dSHarvey Harrison if (is_errata100(regs, address)) 795c61e211dSHarvey Harrison return; 796c61e211dSHarvey Harrison 797c61e211dSHarvey Harrison if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) && 798c61e211dSHarvey Harrison printk_ratelimit()) { 799c61e211dSHarvey Harrison printk( 800c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 801c61e211dSHarvey Harrison "%s%s[%d]: segfault at %lx ip %08lx sp %08lx error %lx", 802c61e211dSHarvey Harrison #else 803c61e211dSHarvey Harrison "%s%s[%d]: segfault at %lx ip %lx sp %lx error %lx", 804c61e211dSHarvey Harrison #endif 805c61e211dSHarvey Harrison task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG, 806c61e211dSHarvey Harrison tsk->comm, task_pid_nr(tsk), address, regs->ip, 807c61e211dSHarvey Harrison regs->sp, error_code); 808c61e211dSHarvey Harrison print_vma_addr(" in ", regs->ip); 809c61e211dSHarvey Harrison printk("\n"); 810c61e211dSHarvey Harrison } 811c61e211dSHarvey Harrison 812c61e211dSHarvey Harrison tsk->thread.cr2 = address; 813c61e211dSHarvey Harrison /* Kernel addresses are always protection faults */ 814c61e211dSHarvey Harrison tsk->thread.error_code = error_code | (address >= TASK_SIZE); 815c61e211dSHarvey Harrison tsk->thread.trap_no = 14; 816c61e211dSHarvey Harrison force_sig_info_fault(SIGSEGV, si_code, address, tsk); 817c61e211dSHarvey Harrison return; 818c61e211dSHarvey Harrison } 819c61e211dSHarvey Harrison 820c61e211dSHarvey Harrison if (is_f00f_bug(regs, address)) 821c61e211dSHarvey Harrison return; 822c61e211dSHarvey Harrison 823c61e211dSHarvey Harrison no_context: 824c61e211dSHarvey Harrison /* Are we prepared to handle this kernel fault? */ 825c61e211dSHarvey Harrison if (fixup_exception(regs)) 826c61e211dSHarvey Harrison return; 827c61e211dSHarvey Harrison 828c61e211dSHarvey Harrison /* 829c61e211dSHarvey Harrison * X86_32 830c61e211dSHarvey Harrison * Valid to do another page fault here, because if this fault 831c61e211dSHarvey Harrison * had been triggered by is_prefetch fixup_exception would have 832c61e211dSHarvey Harrison * handled it. 833c61e211dSHarvey Harrison * 834c61e211dSHarvey Harrison * X86_64 835c61e211dSHarvey Harrison * Hall of shame of CPU/BIOS bugs. 836c61e211dSHarvey Harrison */ 837c61e211dSHarvey Harrison if (is_prefetch(regs, address, error_code)) 838c61e211dSHarvey Harrison return; 839c61e211dSHarvey Harrison 840c61e211dSHarvey Harrison if (is_errata93(regs, address)) 841c61e211dSHarvey Harrison return; 842c61e211dSHarvey Harrison 843c61e211dSHarvey Harrison /* 844c61e211dSHarvey Harrison * Oops. The kernel tried to access some bad page. We'll have to 845c61e211dSHarvey Harrison * terminate things with extreme prejudice. 846c61e211dSHarvey Harrison */ 847c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 848c61e211dSHarvey Harrison bust_spinlocks(1); 849fd40d6e3SHarvey Harrison #else 850fd40d6e3SHarvey Harrison flags = oops_begin(); 851fd40d6e3SHarvey Harrison #endif 852c61e211dSHarvey Harrison 853c61e211dSHarvey Harrison show_fault_oops(regs, error_code, address); 854c61e211dSHarvey Harrison 855c61e211dSHarvey Harrison tsk->thread.cr2 = address; 856c61e211dSHarvey Harrison tsk->thread.trap_no = 14; 857c61e211dSHarvey Harrison tsk->thread.error_code = error_code; 858fd40d6e3SHarvey Harrison 859fd40d6e3SHarvey Harrison #ifdef CONFIG_X86_32 860c61e211dSHarvey Harrison die("Oops", regs, error_code); 861c61e211dSHarvey Harrison bust_spinlocks(0); 862c61e211dSHarvey Harrison do_exit(SIGKILL); 863fd40d6e3SHarvey Harrison #else 864c61e211dSHarvey Harrison if (__die("Oops", regs, error_code)) 865c61e211dSHarvey Harrison regs = NULL; 866c61e211dSHarvey Harrison /* Executive summary in case the body of the oops scrolled away */ 867c61e211dSHarvey Harrison printk(KERN_EMERG "CR2: %016lx\n", address); 868c61e211dSHarvey Harrison oops_end(flags, regs, SIGKILL); 869c61e211dSHarvey Harrison #endif 870c61e211dSHarvey Harrison 871c61e211dSHarvey Harrison /* 872c61e211dSHarvey Harrison * We ran out of memory, or some other thing happened to us that made 873c61e211dSHarvey Harrison * us unable to handle the page fault gracefully. 874c61e211dSHarvey Harrison */ 875c61e211dSHarvey Harrison out_of_memory: 876c61e211dSHarvey Harrison up_read(&mm->mmap_sem); 877c61e211dSHarvey Harrison if (is_global_init(tsk)) { 878c61e211dSHarvey Harrison yield(); 879fd40d6e3SHarvey Harrison #ifdef CONFIG_X86_32 880c61e211dSHarvey Harrison down_read(&mm->mmap_sem); 881c61e211dSHarvey Harrison goto survive; 882c61e211dSHarvey Harrison #else 883c61e211dSHarvey Harrison goto again; 884c61e211dSHarvey Harrison #endif 885fd40d6e3SHarvey Harrison } 886fd40d6e3SHarvey Harrison 887c61e211dSHarvey Harrison printk("VM: killing process %s\n", tsk->comm); 888c61e211dSHarvey Harrison if (error_code & PF_USER) 889c61e211dSHarvey Harrison do_group_exit(SIGKILL); 890c61e211dSHarvey Harrison goto no_context; 891c61e211dSHarvey Harrison 892c61e211dSHarvey Harrison do_sigbus: 893c61e211dSHarvey Harrison up_read(&mm->mmap_sem); 894c61e211dSHarvey Harrison 895c61e211dSHarvey Harrison /* Kernel mode? Handle exceptions or die */ 896c61e211dSHarvey Harrison if (!(error_code & PF_USER)) 897c61e211dSHarvey Harrison goto no_context; 898c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 899c61e211dSHarvey Harrison /* User space => ok to do another page fault */ 900c61e211dSHarvey Harrison if (is_prefetch(regs, address, error_code)) 901c61e211dSHarvey Harrison return; 902c61e211dSHarvey Harrison #endif 903c61e211dSHarvey Harrison tsk->thread.cr2 = address; 904c61e211dSHarvey Harrison tsk->thread.error_code = error_code; 905c61e211dSHarvey Harrison tsk->thread.trap_no = 14; 906c61e211dSHarvey Harrison force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk); 907c61e211dSHarvey Harrison } 908c61e211dSHarvey Harrison 909c61e211dSHarvey Harrison DEFINE_SPINLOCK(pgd_lock); 910c61e211dSHarvey Harrison LIST_HEAD(pgd_list); 911c61e211dSHarvey Harrison 912c61e211dSHarvey Harrison void vmalloc_sync_all(void) 913c61e211dSHarvey Harrison { 914c61e211dSHarvey Harrison #ifdef CONFIG_X86_32 915c61e211dSHarvey Harrison /* 916c61e211dSHarvey Harrison * Note that races in the updates of insync and start aren't 917c61e211dSHarvey Harrison * problematic: insync can only get set bits added, and updates to 918c61e211dSHarvey Harrison * start are only improving performance (without affecting correctness 919c61e211dSHarvey Harrison * if undone). 920c61e211dSHarvey Harrison */ 921c61e211dSHarvey Harrison static DECLARE_BITMAP(insync, PTRS_PER_PGD); 922c61e211dSHarvey Harrison static unsigned long start = TASK_SIZE; 923c61e211dSHarvey Harrison unsigned long address; 924c61e211dSHarvey Harrison 925c61e211dSHarvey Harrison if (SHARED_KERNEL_PMD) 926c61e211dSHarvey Harrison return; 927c61e211dSHarvey Harrison 928c61e211dSHarvey Harrison BUILD_BUG_ON(TASK_SIZE & ~PGDIR_MASK); 929c61e211dSHarvey Harrison for (address = start; address >= TASK_SIZE; address += PGDIR_SIZE) { 930c61e211dSHarvey Harrison if (!test_bit(pgd_index(address), insync)) { 931c61e211dSHarvey Harrison unsigned long flags; 932c61e211dSHarvey Harrison struct page *page; 933c61e211dSHarvey Harrison 934c61e211dSHarvey Harrison spin_lock_irqsave(&pgd_lock, flags); 935e3ed910dSJeremy Fitzhardinge list_for_each_entry(page, &pgd_list, lru) { 936c61e211dSHarvey Harrison if (!vmalloc_sync_one(page_address(page), 937e3ed910dSJeremy Fitzhardinge address)) 938c61e211dSHarvey Harrison break; 939c61e211dSHarvey Harrison } 940c61e211dSHarvey Harrison spin_unlock_irqrestore(&pgd_lock, flags); 941c61e211dSHarvey Harrison if (!page) 942c61e211dSHarvey Harrison set_bit(pgd_index(address), insync); 943c61e211dSHarvey Harrison } 944c61e211dSHarvey Harrison if (address == start && test_bit(pgd_index(address), insync)) 945c61e211dSHarvey Harrison start = address + PGDIR_SIZE; 946c61e211dSHarvey Harrison } 947c61e211dSHarvey Harrison #else /* CONFIG_X86_64 */ 948c61e211dSHarvey Harrison /* 949c61e211dSHarvey Harrison * Note that races in the updates of insync and start aren't 950c61e211dSHarvey Harrison * problematic: insync can only get set bits added, and updates to 951c61e211dSHarvey Harrison * start are only improving performance (without affecting correctness 952c61e211dSHarvey Harrison * if undone). 953c61e211dSHarvey Harrison */ 954c61e211dSHarvey Harrison static DECLARE_BITMAP(insync, PTRS_PER_PGD); 955c61e211dSHarvey Harrison static unsigned long start = VMALLOC_START & PGDIR_MASK; 956c61e211dSHarvey Harrison unsigned long address; 957c61e211dSHarvey Harrison 958c61e211dSHarvey Harrison for (address = start; address <= VMALLOC_END; address += PGDIR_SIZE) { 959c61e211dSHarvey Harrison if (!test_bit(pgd_index(address), insync)) { 960c61e211dSHarvey Harrison const pgd_t *pgd_ref = pgd_offset_k(address); 96158d5d0d8SIngo Molnar unsigned long flags; 962c61e211dSHarvey Harrison struct page *page; 963c61e211dSHarvey Harrison 964c61e211dSHarvey Harrison if (pgd_none(*pgd_ref)) 965c61e211dSHarvey Harrison continue; 96658d5d0d8SIngo Molnar spin_lock_irqsave(&pgd_lock, flags); 967c61e211dSHarvey Harrison list_for_each_entry(page, &pgd_list, lru) { 968c61e211dSHarvey Harrison pgd_t *pgd; 969c61e211dSHarvey Harrison pgd = (pgd_t *)page_address(page) + pgd_index(address); 970c61e211dSHarvey Harrison if (pgd_none(*pgd)) 971c61e211dSHarvey Harrison set_pgd(pgd, *pgd_ref); 972c61e211dSHarvey Harrison else 973c61e211dSHarvey Harrison BUG_ON(pgd_page_vaddr(*pgd) != pgd_page_vaddr(*pgd_ref)); 974c61e211dSHarvey Harrison } 97558d5d0d8SIngo Molnar spin_unlock_irqrestore(&pgd_lock, flags); 976c61e211dSHarvey Harrison set_bit(pgd_index(address), insync); 977c61e211dSHarvey Harrison } 978c61e211dSHarvey Harrison if (address == start) 979c61e211dSHarvey Harrison start = address + PGDIR_SIZE; 980c61e211dSHarvey Harrison } 981c61e211dSHarvey Harrison /* Check that there is no need to do the same for the modules area. */ 982c61e211dSHarvey Harrison BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL)); 983c61e211dSHarvey Harrison BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) == 984c61e211dSHarvey Harrison (__START_KERNEL & PGDIR_MASK))); 985c61e211dSHarvey Harrison #endif 986c61e211dSHarvey Harrison } 987