x86: Fix code patching for paravirt-alternatives on 486
[safe/jmp/linux-2.6] / arch / x86 / kernel / alternative.c
index de240ba..b8ebd0b 100644 (file)
@@ -1,10 +1,11 @@
 #include <linux/module.h>
 #include <linux/sched.h>
-#include <linux/spinlock.h>
+#include <linux/mutex.h>
 #include <linux/list.h>
 #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,31 +229,35 @@ 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;
                if (*ptr > text_end)
                        continue;
-               text_poke(*ptr, ((unsigned char []){0xf0}), 1); /* add lock prefix */
+               /* 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)
 {
        u8 **ptr;
-       char insn[1];
 
        if (noreplace_smp)
                return;
 
-       add_nops(insn, 1);
+       mutex_lock(&text_mutex);
        for (ptr = start; ptr < end; ptr++) {
                if (*ptr < text)
                        continue;
                if (*ptr > text_end)
                        continue;
-               text_poke(*ptr, insn, 1);
+               /* turn lock prefix into DS segment override prefix */
+               text_poke(*ptr, ((unsigned char []){0x3E}), 1);
        };
+       mutex_unlock(&text_mutex);
 }
 
 struct smp_alt_module {
@@ -279,7 +276,7 @@ struct smp_alt_module {
        struct list_head next;
 };
 static LIST_HEAD(smp_alt_modules);
-static DEFINE_SPINLOCK(smp_alt);
+static DEFINE_MUTEX(smp_alt);
 static int smp_mode = 1;       /* protected by smp_alt */
 
 void alternatives_smp_module_add(struct module *mod, char *name,
@@ -312,12 +309,12 @@ void alternatives_smp_module_add(struct module *mod, char *name,
                __func__, smp->locks, smp->locks_end,
                smp->text, smp->text_end, smp->name);
 
-       spin_lock(&smp_alt);
+       mutex_lock(&smp_alt);
        list_add_tail(&smp->next, &smp_alt_modules);
        if (boot_cpu_has(X86_FEATURE_UP))
                alternatives_smp_unlock(smp->locks, smp->locks_end,
                                        smp->text, smp->text_end);
-       spin_unlock(&smp_alt);
+       mutex_unlock(&smp_alt);
 }
 
 void alternatives_smp_module_del(struct module *mod)
@@ -327,17 +324,17 @@ void alternatives_smp_module_del(struct module *mod)
        if (smp_alt_once || noreplace_smp)
                return;
 
-       spin_lock(&smp_alt);
+       mutex_lock(&smp_alt);
        list_for_each_entry(item, &smp_alt_modules, next) {
                if (mod != item->mod)
                        continue;
                list_del(&item->next);
-               spin_unlock(&smp_alt);
+               mutex_unlock(&smp_alt);
                DPRINTK("%s: %s\n", __func__, item->name);
                kfree(item);
                return;
        }
-       spin_unlock(&smp_alt);
+       mutex_unlock(&smp_alt);
 }
 
 void alternatives_smp_switch(int smp)
@@ -359,7 +356,7 @@ void alternatives_smp_switch(int smp)
                return;
        BUG_ON(!smp && (num_online_cpus() > 1));
 
-       spin_lock(&smp_alt);
+       mutex_lock(&smp_alt);
 
        /*
         * Avoid unnecessary switches because it forces JIT based VMs to
@@ -383,7 +380,7 @@ void alternatives_smp_switch(int smp)
                                                mod->text, mod->text_end);
        }
        smp_mode = smp;
-       spin_unlock(&smp_alt);
+       mutex_unlock(&smp_alt);
 }
 
 #endif
@@ -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;
 }