x86: Fix code patching for paravirt-alternatives on 486
[safe/jmp/linux-2.6] / arch / x86 / kernel / alternative.c
index 7ead11f..b8ebd0b 100644 (file)
@@ -5,6 +5,7 @@
 #include <linux/kprobes.h>
 #include <linux/mm.h>
 #include <linux/vmalloc.h>
+#include <linux/memory.h>
 #include <asm/alternative.h>
 #include <asm/sections.h>
 #include <asm/pgtable.h>
@@ -12,7 +13,9 @@
 #include <asm/nmi.h>
 #include <asm/vsyscall.h>
 #include <asm/cacheflush.h>
+#include <asm/tlbflush.h>
 #include <asm/io.h>
+#include <asm/fixmap.h>
 
 #define MAX_PATCH_LEN (255-1)
 
@@ -145,35 +148,25 @@ static const unsigned char *const p6_nops[ASM_NOP_MAX+1] = {
 extern char __vsyscall_0;
 const unsigned char *const *find_nop_table(void)
 {
-       return boot_cpu_data.x86_vendor != X86_VENDOR_INTEL ||
-              boot_cpu_data.x86 < 6 ? k8_nops : p6_nops;
+       if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
+           boot_cpu_has(X86_FEATURE_NOPL))
+               return p6_nops;
+       else
+               return k8_nops;
 }
 
 #else /* CONFIG_X86_64 */
 
-static const struct nop {
-       int cpuid;
-       const unsigned char *const *noptable;
-} noptypes[] = {
-       { X86_FEATURE_K8, k8_nops },
-       { X86_FEATURE_K7, k7_nops },
-       { X86_FEATURE_P4, p6_nops },
-       { X86_FEATURE_P3, p6_nops },
-       { -1, NULL }
-};
-
 const unsigned char *const *find_nop_table(void)
 {
-       const unsigned char *const *noptable = intel_nops;
-       int i;
-
-       for (i = 0; noptypes[i].cpuid >= 0; i++) {
-               if (boot_cpu_has(noptypes[i].cpuid)) {
-                       noptable = noptypes[i].noptable;
-                       break;
-               }
-       }
-       return noptable;
+       if (boot_cpu_has(X86_FEATURE_K8))
+               return k8_nops;
+       else if (boot_cpu_has(X86_FEATURE_K7))
+               return k7_nops;
+       else if (boot_cpu_has(X86_FEATURE_NOPL))
+               return p6_nops;
+       else
+               return intel_nops;
 }
 
 #endif /* CONFIG_X86_64 */
@@ -236,6 +229,7 @@ static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
 {
        u8 **ptr;
 
+       mutex_lock(&text_mutex);
        for (ptr = start; ptr < end; ptr++) {
                if (*ptr < text)
                        continue;
@@ -244,6 +238,7 @@ static void alternatives_smp_lock(u8 **start, u8 **end, u8 *text, u8 *text_end)
                /* turn DS segment override prefix into lock prefix */
                text_poke(*ptr, ((unsigned char []){0xf0}), 1);
        };
+       mutex_unlock(&text_mutex);
 }
 
 static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end)
@@ -253,6 +248,7 @@ static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end
        if (noreplace_smp)
                return;
 
+       mutex_lock(&text_mutex);
        for (ptr = start; ptr < end; ptr++) {
                if (*ptr < text)
                        continue;
@@ -261,6 +257,7 @@ static void alternatives_smp_unlock(u8 **start, u8 **end, u8 *text, u8 *text_end
                /* turn lock prefix into DS segment override prefix */
                text_poke(*ptr, ((unsigned char []){0x3E}), 1);
        };
+       mutex_unlock(&text_mutex);
 }
 
 struct smp_alt_module {
@@ -424,9 +421,17 @@ void __init alternative_instructions(void)
           that might execute the to be patched code.
           Other CPUs are not running. */
        stop_nmi();
-#ifdef CONFIG_X86_MCE
-       stop_mce();
-#endif
+
+       /*
+        * Don't stop machine check exceptions while patching.
+        * MCEs only happen when something got corrupted and in this
+        * case we must do something about the corruption.
+        * Ignoring it is worse than a unlikely patching race.
+        * Also machine checks tend to be broadcast and if one CPU
+        * goes into machine check the others follow quickly, so we don't
+        * expect a machine check to cause undue problems during to code
+        * patching.
+        */
 
        apply_alternatives(__alt_instructions, __alt_instructions_end);
 
@@ -454,7 +459,7 @@ void __init alternative_instructions(void)
                                            _text, _etext);
 
                /* Only switch to UP mode if we don't immediately boot others */
-               if (num_possible_cpus() == 1 || setup_max_cpus <= 1)
+               if (num_present_cpus() == 1 || setup_max_cpus <= 1)
                        alternatives_smp_switch(0);
        }
 #endif
@@ -466,9 +471,6 @@ void __init alternative_instructions(void)
                                (unsigned long)__smp_locks_end);
 
        restart_nmi();
-#ifdef CONFIG_X86_MCE
-       restart_mce();
-#endif
 }
 
 /**
@@ -488,8 +490,8 @@ void *text_poke_early(void *addr, const void *opcode, size_t len)
        unsigned long flags;
        local_irq_save(flags);
        memcpy(addr, opcode, len);
-       local_irq_restore(flags);
        sync_core();
+       local_irq_restore(flags);
        /* Could also do a CLFLUSH here to speed up CPU recovery; but
           that causes hangs on some VIA CPUs. */
        return addr;
@@ -505,12 +507,13 @@ void *text_poke_early(void *addr, const void *opcode, size_t len)
  * It means the size must be writable atomically and the address must be aligned
  * in a way that permits an atomic write. It also makes sure we fit on a single
  * page.
+ *
+ * Note: Must be called under text_mutex.
  */
 void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
 {
        unsigned long flags;
        char *vaddr;
-       int nr_pages = 2;
        struct page *pages[2];
        int i;
 
@@ -523,18 +526,21 @@ void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
                pages[1] = virt_to_page(addr + PAGE_SIZE);
        }
        BUG_ON(!pages[0]);
-       if (!pages[1])
-               nr_pages = 1;
-       vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
-       BUG_ON(!vaddr);
        local_irq_save(flags);
+       set_fixmap(FIX_TEXT_POKE0, page_to_phys(pages[0]));
+       if (pages[1])
+               set_fixmap(FIX_TEXT_POKE1, page_to_phys(pages[1]));
+       vaddr = (char *)fix_to_virt(FIX_TEXT_POKE0);
        memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
-       local_irq_restore(flags);
-       vunmap(vaddr);
+       clear_fixmap(FIX_TEXT_POKE0);
+       if (pages[1])
+               clear_fixmap(FIX_TEXT_POKE1);
+       local_flush_tlb();
        sync_core();
        /* Could also do a CLFLUSH here to speed up CPU recovery; but
           that causes hangs on some VIA CPUs. */
        for (i = 0; i < len; i++)
                BUG_ON(((char *)addr)[i] != ((char *)opcode)[i]);
+       local_irq_restore(flags);
        return addr;
 }