x86: clean up text_poke()
authorMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Thu, 24 Apr 2008 15:03:33 +0000 (11:03 -0400)
committerIngo Molnar <mingo@elte.hu>
Fri, 25 Apr 2008 17:54:07 +0000 (19:54 +0200)
Clean up the codepath, remove alignment restrictions and do sanity
checking of the end result, to make sure we patched the right site.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
arch/x86/kernel/alternative.c

index 7ab3a9774763b35d3ce866083c04f22a0c6d3b47..60299f61843fc9c13a701704edff8567baf3e08e 100644 (file)
@@ -511,31 +511,29 @@ 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;
 
-       BUG_ON(len > sizeof(long));
-       BUG_ON((((long)addr + len - 1) & ~(sizeof(long) - 1))
-               - ((long)addr & ~(sizeof(long) - 1)));
-       if (core_kernel_text((unsigned long)addr)) {
-               struct page *pages[2] = { virt_to_page(addr),
-                       virt_to_page(addr + PAGE_SIZE) };
-               if (!pages[1])
-                       nr_pages = 1;
-               vaddr = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL);
-               BUG_ON(!vaddr);
-               local_irq_save(flags);
-               memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
-               local_irq_restore(flags);
-               vunmap(vaddr);
+       if (!core_kernel_text((unsigned long)addr)) {
+               pages[0] = vmalloc_to_page(addr);
+               pages[1] = vmalloc_to_page(addr + PAGE_SIZE);
        } else {
-               /*
-                * modules are in vmalloc'ed memory, always writable.
-                */
-               local_irq_save(flags);
-               memcpy(addr, opcode, len);
-               local_irq_restore(flags);
+               pages[0] = virt_to_page(addr);
+               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);
+       memcpy(&vaddr[(unsigned long)addr & ~PAGE_MASK], opcode, len);
+       local_irq_restore(flags);
+       vunmap(vaddr);
        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]);
        return addr;
 }