]> err.no Git - linux-2.6/blobdiff - arch/x86/mm/fault_64.c
x86: make sure initmem is writable
[linux-2.6] / arch / x86 / mm / fault_64.c
index 0e26230669ca21aabb92a60f134100d8f453f5fc..dd26e680a43166ca9abe0ced6390989edddf4349 100644 (file)
@@ -1,6 +1,4 @@
 /*
- *  linux/arch/x86-64/mm/fault.c
- *
  *  Copyright (C) 1995  Linus Torvalds
  *  Copyright (C) 2001,2002 Andi Kleen, SuSE Labs.
  */
@@ -25,7 +23,6 @@
 #include <linux/kprobes.h>
 #include <linux/uaccess.h>
 #include <linux/kdebug.h>
-#include <linux/kprobes.h>
 
 #include <asm/system.h>
 #include <asm/pgalloc.h>
 #include <asm/proto.h>
 #include <asm-generic/sections.h>
 
-/* Page fault error code bits */
-#define PF_PROT        (1<<0)          /* or no page found */
+/*
+ * Page fault error code bits
+ *     bit 0 == 0 means no page found, 1 means protection fault
+ *     bit 1 == 0 means read, 1 means write
+ *     bit 2 == 0 means kernel, 1 means user-mode
+ *     bit 3 == 1 means use of reserved bit detected
+ *     bit 4 == 1 means fault was an instruction fetch
+ */
+#define PF_PROT                (1<<0)
 #define PF_WRITE       (1<<1)
-#define PF_USER        (1<<2)
-#define PF_RSVD        (1<<3)
+#define PF_USER                (1<<2)
+#define PF_RSVD                (1<<3)
 #define PF_INSTR       (1<<4)
 
-#ifdef CONFIG_KPROBES
 static inline int notify_page_fault(struct pt_regs *regs)
 {
+#ifdef CONFIG_KPROBES
        int ret = 0;
 
        /* kprobe_running() needs smp_processor_id() */
@@ -55,96 +59,128 @@ static inline int notify_page_fault(struct pt_regs *regs)
        }
 
        return ret;
-}
 #else
-static inline int notify_page_fault(struct pt_regs *regs)
-{
        return 0;
-}
 #endif
+}
 
-/* Sometimes the CPU reports invalid exceptions on prefetch.
-   Check that here and ignore.
-   Opcode checker based on code by Richard Brunner */
-static noinline int is_prefetch(struct pt_regs *regs, unsigned long addr,
-                               unsigned long error_code)
-{ 
+/*
+ * X86_32
+ * Sometimes AMD Athlon/Opteron CPUs report invalid exceptions on prefetch.
+ * Check that here and ignore it.
+ *
+ * X86_64
+ * Sometimes the CPU reports invalid exceptions on prefetch.
+ * Check that here and ignore it.
+ *
+ * Opcode checker based on code by Richard Brunner
+ */
+static int is_prefetch(struct pt_regs *regs, unsigned long addr,
+                      unsigned long error_code)
+{
        unsigned char *instr;
        int scan_more = 1;
-       int prefetch = 0; 
+       int prefetch = 0;
        unsigned char *max_instr;
 
+#ifdef CONFIG_X86_32
+       if (unlikely(boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
+                    boot_cpu_data.x86 >= 6)) {
+               /* Catch an obscure case of prefetch inside an NX page. */
+               if (nx_enabled && (error_code & PF_INSTR))
+                       return 0;
+       } else {
+               return 0;
+       }
+#else
        /* If it was a exec fault ignore */
        if (error_code & PF_INSTR)
                return 0;
-       
-       instr = (unsigned char __user *)convert_rip_to_linear(current, regs);
+#endif
+
+       instr = (unsigned char *)convert_ip_to_linear(current, regs);
        max_instr = instr + 15;
 
        if (user_mode(regs) && instr >= (unsigned char *)TASK_SIZE)
                return 0;
 
-       while (scan_more && instr < max_instr) { 
+       while (scan_more && instr < max_instr) {
                unsigned char opcode;
                unsigned char instr_hi;
                unsigned char instr_lo;
 
                if (probe_kernel_address(instr, opcode))
-                       break; 
+                       break;
 
-               instr_hi = opcode & 0xf0; 
-               instr_lo = opcode & 0x0f; 
+               instr_hi = opcode & 0xf0;
+               instr_lo = opcode & 0x0f;
                instr++;
 
-               switch (instr_hi) { 
+               switch (instr_hi) {
                case 0x20:
                case 0x30:
-                       /* Values 0x26,0x2E,0x36,0x3E are valid x86
-                          prefixes.  In long mode, the CPU will signal
-                          invalid opcode if some of these prefixes are
-                          present so we will never get here anyway */
+                       /*
+                        * Values 0x26,0x2E,0x36,0x3E are valid x86 prefixes.
+                        * In X86_64 long mode, the CPU will signal invalid
+                        * opcode if some of these prefixes are present so
+                        * X86_64 will never get here anyway
+                        */
                        scan_more = ((instr_lo & 7) == 0x6);
                        break;
-                       
+#ifdef CONFIG_X86_64
                case 0x40:
-                       /* In AMD64 long mode, 0x40 to 0x4F are valid REX prefixes
-                          Need to figure out under what instruction mode the
-                          instruction was issued ... */
-                       /* Could check the LDT for lm, but for now it's good
-                          enough to assume that long mode only uses well known
-                          segments or kernel. */
+                       /*
+                        * In AMD64 long mode 0x40..0x4F are valid REX prefixes
+                        * Need to figure out under what instruction mode the
+                        * instruction was issued. Could check the LDT for lm,
+                        * but for now it's good enough to assume that long
+                        * mode only uses well known segments or kernel.
+                        */
                        scan_more = (!user_mode(regs)) || (regs->cs == __USER_CS);
                        break;
-                       
+#endif
                case 0x60:
                        /* 0x64 thru 0x67 are valid prefixes in all modes. */
                        scan_more = (instr_lo & 0xC) == 0x4;
-                       break;          
+                       break;
                case 0xF0:
-                       /* 0xF0, 0xF2, and 0xF3 are valid prefixes in all modes. */
+                       /* 0xF0, 0xF2, 0xF3 are valid prefixes in all modes. */
                        scan_more = !instr_lo || (instr_lo>>1) == 1;
-                       break;                  
+                       break;
                case 0x00:
                        /* Prefetch instruction is 0x0F0D or 0x0F18 */
                        scan_more = 0;
+
                        if (probe_kernel_address(instr, opcode))
                                break;
                        prefetch = (instr_lo == 0xF) &&
                                (opcode == 0x0D || opcode == 0x18);
-                       break;                  
+                       break;
                default:
                        scan_more = 0;
                        break;
-               } 
+               }
        }
        return prefetch;
 }
 
-static int bad_address(void *p) 
-{ 
+static void force_sig_info_fault(int si_signo, int si_code,
+       unsigned long address, struct task_struct *tsk)
+{
+       siginfo_t info;
+
+       info.si_signo = si_signo;
+       info.si_errno = 0;
+       info.si_code = si_code;
+       info.si_addr = (void __user *)address;
+       force_sig_info(si_signo, &info, tsk);
+}
+
+static int bad_address(void *p)
+{
        unsigned long dummy;
        return probe_kernel_address((unsigned long *)p, dummy);
-} 
+}
 
 void dump_pagetable(unsigned long address)
 {
@@ -155,11 +191,11 @@ void dump_pagetable(unsigned long address)
 
        pgd = (pgd_t *)read_cr3();
 
-       pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK); 
+       pgd = __va((unsigned long)pgd & PHYSICAL_PAGE_MASK);
        pgd += pgd_index(address);
        if (bad_address(pgd)) goto bad;
        printk("PGD %lx ", pgd_val(*pgd));
-       if (!pgd_present(*pgd)) goto ret; 
+       if (!pgd_present(*pgd)) goto ret;
 
        pud = pud_offset(pgd, address);
        if (bad_address(pud)) goto bad;
@@ -173,7 +209,7 @@ void dump_pagetable(unsigned long address)
 
        pte = pte_offset_kernel(pmd, address);
        if (bad_address(pte)) goto bad;
-       printk("PTE %lx", pte_val(*pte)); 
+       printk("PTE %lx", pte_val(*pte));
 ret:
        printk("\n");
        return;
@@ -181,39 +217,44 @@ bad:
        printk("BAD\n");
 }
 
-static const char errata93_warning[] = 
+#ifdef CONFIG_X86_64
+static const char errata93_warning[] =
 KERN_ERR "******* Your BIOS seems to not contain a fix for K8 errata #93\n"
 KERN_ERR "******* Working around it, but it may cause SEGVs or burn power.\n"
 KERN_ERR "******* Please consider a BIOS update.\n"
 KERN_ERR "******* Disabling USB legacy in the BIOS may also help.\n";
+#endif
 
 /* Workaround for K8 erratum #93 & buggy BIOS.
    BIOS SMM functions are required to use a specific workaround
-   to avoid corruption of the 64bit RIP register on C stepping K8. 
-   A lot of BIOS that didn't get tested properly miss this. 
+   to avoid corruption of the 64bit RIP register on C stepping K8.
+   A lot of BIOS that didn't get tested properly miss this.
    The OS sees this as a page fault with the upper 32bits of RIP cleared.
    Try to work around it here.
-   Note we only handle faults in kernel here. */
-
-static int is_errata93(struct pt_regs *regs, unsigned long address) 
+   Note we only handle faults in kernel here.
+   Does nothing for X86_32
+ */
+static int is_errata93(struct pt_regs *regs, unsigned long address)
 {
+#ifdef CONFIG_X86_64
        static int warned;
-       if (address != regs->rip)
+       if (address != regs->ip)
                return 0;
-       if ((address >> 32) != 0) 
+       if ((address >> 32) != 0)
                return 0;
        address |= 0xffffffffUL << 32;
-       if ((address >= (u64)_stext && address <= (u64)_etext) || 
-           (address >= MODULES_VADDR && address <= MODULES_END)) { 
+       if ((address >= (u64)_stext && address <= (u64)_etext) ||
+           (address >= MODULES_VADDR && address <= MODULES_END)) {
                if (!warned) {
-                       printk(errata93_warning);               
+                       printk(errata93_warning);
                        warned = 1;
                }
-               regs->rip = address;
+               regs->ip = address;
                return 1;
        }
+#endif
        return 0;
-} 
+}
 
 static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
                                 unsigned long error_code)
@@ -228,9 +269,9 @@ static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
        tsk->thread.cr2 = address;
        tsk->thread.trap_no = 14;
        tsk->thread.error_code = error_code;
-       __die("Bad pagetable", regs, error_code);
-       oops_end(flags);
-       do_exit(SIGKILL);
+       if (__die("Bad pagetable", regs, error_code))
+               regs = NULL;
+       oops_end(flags, regs, SIGKILL);
 }
 
 /*
@@ -240,6 +281,26 @@ static noinline void pgtable_bad(unsigned long address, struct pt_regs *regs,
  */
 static int vmalloc_fault(unsigned long address)
 {
+#ifdef CONFIG_X86_32
+       unsigned long pgd_paddr;
+       pmd_t *pmd_k;
+       pte_t *pte_k;
+       /*
+        * Synchronize this task's top level page-table
+        * with the 'reference' page table.
+        *
+        * Do _not_ use "current" here. We might be inside
+        * an interrupt in the middle of a task switch..
+        */
+       pgd_paddr = read_cr3();
+       pmd_k = vmalloc_sync_one(__va(pgd_paddr), address);
+       if (!pmd_k)
+               return -1;
+       pte_k = pte_offset_kernel(pmd_k, address);
+       if (!pte_present(*pte_k))
+               return -1;
+       return 0;
+#else
        pgd_t *pgd, *pgd_ref;
        pud_t *pud, *pud_ref;
        pmd_t *pmd, *pmd_ref;
@@ -283,6 +344,7 @@ static int vmalloc_fault(unsigned long address)
        if (!pte_present(*pte) || pte_pfn(*pte) != pte_pfn(*pte_ref))
                BUG();
        return 0;
+#endif
 }
 
 int show_unhandled_signals = 1;
@@ -297,12 +359,11 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
 {
        struct task_struct *tsk;
        struct mm_struct *mm;
-       struct vm_area_struct * vma;
+       struct vm_area_struct *vma;
        unsigned long address;
-       const struct exception_table_entry *fixup;
        int write, fault;
        unsigned long flags;
-       siginfo_t info;
+       int si_code;
 
        /*
         * We can fault from pretty much anywhere, with unknown IRQ state.
@@ -316,8 +377,10 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
        /* get the address */
        address = read_cr2();
 
-       info.si_code = SEGV_MAPERR;
+       si_code = SEGV_MAPERR;
 
+       if (notify_page_fault(regs))
+               return;
 
        /*
         * We fault-in kernel-space virtual memory on-demand. The
@@ -343,8 +406,6 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
                        if (vmalloc_fault(address) >= 0)
                                return;
                }
-               if (notify_page_fault(regs))
-                       return;
                /*
                 * Don't take the mm semaphore here. If we fixup a prefetch
                 * fault we could otherwise deadlock.
@@ -352,18 +413,15 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
                goto bad_area_nosemaphore;
        }
 
-       if (notify_page_fault(regs))
-               return;
-
-       if (likely(regs->eflags & X86_EFLAGS_IF))
+       if (likely(regs->flags & X86_EFLAGS_IF))
                local_irq_enable();
 
        if (unlikely(error_code & PF_RSVD))
                pgtable_bad(address, regs, error_code);
 
        /*
-        * If we're in an interrupt or have no user
-        * context, we must not take the fault..
+        * If we're in an interrupt, have no user context or are running in an
+        * atomic region then we must not take the fault.
         */
        if (unlikely(in_atomic() || !mm))
                goto bad_area_nosemaphore;
@@ -393,7 +451,7 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
         */
        if (!down_read_trylock(&mm->mmap_sem)) {
                if ((error_code & PF_USER) == 0 &&
-                   !search_exception_tables(regs->rip))
+                   !search_exception_tables(regs->ip))
                        goto bad_area_nosemaphore;
                down_read(&mm->mmap_sem);
        }
@@ -405,11 +463,14 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
                goto good_area;
        if (!(vma->vm_flags & VM_GROWSDOWN))
                goto bad_area;
-       if (error_code & 4) {
-               /* Allow userspace just enough access below the stack pointer
-                * to let the 'enter' instruction work.
+       if (error_code & PF_USER) {
+               /*
+                * Accessing the stack below %sp is always a bug.
+                * The large cushion allows instructions like enter
+                * and pusha to work.  ("enter $65535,$31" pushes
+                * 32 pointers and then decrements %sp by 65535.)
                 */
-               if (address + 65536 + 32 * sizeof(unsigned long) < regs->rsp)
+               if (address + 65536 + 32 * sizeof(unsigned long) < regs->sp)
                        goto bad_area;
        }
        if (expand_stack(vma, address))
@@ -419,21 +480,21 @@ asmlinkage void __kprobes do_page_fault(struct pt_regs *regs,
  * we can handle it..
  */
 good_area:
-       info.si_code = SEGV_ACCERR;
+       si_code = SEGV_ACCERR;
        write = 0;
        switch (error_code & (PF_PROT|PF_WRITE)) {
-               default:        /* 3: write, present */
-                       /* fall through */
-               case PF_WRITE:          /* write, not present */
-                       if (!(vma->vm_flags & VM_WRITE))
-                               goto bad_area;
-                       write++;
-                       break;
-               case PF_PROT:           /* read, present */
+       default:        /* 3: write, present */
+               /* fall through */
+       case PF_WRITE:          /* write, not present */
+               if (!(vma->vm_flags & VM_WRITE))
+                       goto bad_area;
+               write++;
+               break;
+       case PF_PROT:           /* read, present */
+               goto bad_area;
+       case 0:                 /* read, not present */
+               if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
                        goto bad_area;
-               case 0:                 /* read, not present */
-                       if (!(vma->vm_flags & (VM_READ | VM_EXEC | VM_WRITE)))
-                               goto bad_area;
        }
 
        /*
@@ -453,6 +514,17 @@ good_area:
                tsk->maj_flt++;
        else
                tsk->min_flt++;
+
+#ifdef CONFIG_X86_32
+       /*
+        * Did it hit the DOS screen memory VA from vm86 mode?
+        */
+       if (v8086_mode(regs)) {
+               unsigned long bit = (address - 0xA0000) >> PAGE_SHIFT;
+               if (bit < 32)
+                       tsk->thread.screen_bitmap |= 1 << bit;
+       }
+#endif
        up_read(&mm->mmap_sem);
        return;
 
@@ -488,42 +560,41 @@ bad_area_nosemaphore:
                if (show_unhandled_signals && unhandled_signal(tsk, SIGSEGV) &&
                    printk_ratelimit()) {
                        printk(
-                      "%s%s[%d]: segfault at %lx rip %lx rsp %lx error %lx\n",
-                                       tsk->pid > 1 ? KERN_INFO : KERN_EMERG,
-                                       tsk->comm, tsk->pid, address, regs->rip,
-                                       regs->rsp, error_code);
+#ifdef CONFIG_X86_32
+                       "%s%s[%d]: segfault at %lx ip %08lx sp %08lx error %lx",
+#else
+                       "%s%s[%d]: segfault at %lx ip %lx sp %lx error %lx",
+#endif
+                       task_pid_nr(tsk) > 1 ? KERN_INFO : KERN_EMERG,
+                       tsk->comm, task_pid_nr(tsk), address, regs->ip,
+                       regs->sp, error_code);
+                       print_vma_addr(" in ", regs->ip);
+                       printk("\n");
                }
-       
+
                tsk->thread.cr2 = address;
                /* Kernel addresses are always protection faults */
                tsk->thread.error_code = error_code | (address >= TASK_SIZE);
                tsk->thread.trap_no = 14;
-               info.si_signo = SIGSEGV;
-               info.si_errno = 0;
-               /* info.si_code has been set above */
-               info.si_addr = (void __user *)address;
-               force_sig_info(SIGSEGV, &info, tsk);
+
+               force_sig_info_fault(SIGSEGV, si_code, address, tsk);
                return;
        }
 
 no_context:
-       
        /* Are we prepared to handle this kernel fault?  */
-       fixup = search_exception_tables(regs->rip);
-       if (fixup) {
-               regs->rip = fixup->fixup;
+       if (fixup_exception(regs))
                return;
-       }
 
-       /* 
+       /*
         * Hall of shame of CPU/BIOS bugs.
         */
 
-       if (is_prefetch(regs, address, error_code))
-               return;
+       if (is_prefetch(regs, address, error_code))
+               return;
 
        if (is_errata93(regs, address))
-               return; 
+               return;
 
 /*
  * Oops. The kernel tried to access some bad page. We'll have to
@@ -536,17 +607,17 @@ no_context:
                printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference");
        else
                printk(KERN_ALERT "Unable to handle kernel paging request");
-       printk(" at %016lx RIP: \n" KERN_ALERT,address);
-       printk_address(regs->rip);
+       printk(" at %016lx RIP: \n" KERN_ALERT, address);
+       printk_address(regs->ip, 1);
        dump_pagetable(address);
        tsk->thread.cr2 = address;
        tsk->thread.trap_no = 14;
        tsk->thread.error_code = error_code;
-       __die("Oops", regs, error_code);
+       if (__die("Oops", regs, error_code))
+               regs = NULL;
        /* Executive summary in case the body of the oops scrolled away */
        printk(KERN_EMERG "CR2: %016lx\n", address);
-       oops_end(flags);
-       do_exit(SIGKILL);
+       oops_end(flags, regs, SIGKILL);
 
 /*
  * We ran out of memory, or some other thing happened to us that made
@@ -559,7 +630,7 @@ out_of_memory:
                goto again;
        }
        printk("VM: killing process %s\n", tsk->comm);
-       if (error_code & 4)
+       if (error_code & PF_USER)
                do_group_exit(SIGKILL);
        goto no_context;
 
@@ -573,11 +644,7 @@ do_sigbus:
        tsk->thread.cr2 = address;
        tsk->thread.error_code = error_code;
        tsk->thread.trap_no = 14;
-       info.si_signo = SIGBUS;
-       info.si_errno = 0;
-       info.si_code = BUS_ADRERR;
-       info.si_addr = (void __user *)address;
-       force_sig_info(SIGBUS, &info, tsk);
+       force_sig_info_fault(SIGBUS, BUS_ADRERR, address, tsk);
        return;
 }
 
@@ -586,10 +653,12 @@ LIST_HEAD(pgd_list);
 
 void vmalloc_sync_all(void)
 {
-       /* Note that races in the updates of insync and start aren't 
-          problematic:
-          insync can only get set bits added, and updates to start are only
-          improving performance (without affecting correctness if undone). */
+       /*
+        * Note that races in the updates of insync and start aren't
+        * problematic: insync can only get set bits added, and updates to
+        * start are only improving performance (without affecting correctness
+        * if undone).
+        */
        static DECLARE_BITMAP(insync, PTRS_PER_PGD);
        static unsigned long start = VMALLOC_START & PGDIR_MASK;
        unsigned long address;
@@ -618,6 +687,6 @@ void vmalloc_sync_all(void)
        }
        /* Check that there is no need to do the same for the modules area. */
        BUILD_BUG_ON(!(MODULES_VADDR > __START_KERNEL));
-       BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) == 
+       BUILD_BUG_ON(!(((MODULES_END - 1) & PGDIR_MASK) ==
                                (__START_KERNEL & PGDIR_MASK)));
 }