aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel/alternative.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/kernel/alternative.c')
-rw-r--r--arch/x86/kernel/alternative.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/arch/x86/kernel/alternative.c b/arch/x86/kernel/alternative.c
index e63b80e5861c..c41f13c15e8f 100644
--- a/arch/x86/kernel/alternative.c
+++ b/arch/x86/kernel/alternative.c
@@ -7,6 +7,7 @@
7#include <linux/mm.h> 7#include <linux/mm.h>
8#include <linux/vmalloc.h> 8#include <linux/vmalloc.h>
9#include <linux/memory.h> 9#include <linux/memory.h>
10#include <linux/stop_machine.h>
10#include <asm/alternative.h> 11#include <asm/alternative.h>
11#include <asm/sections.h> 12#include <asm/sections.h>
12#include <asm/pgtable.h> 13#include <asm/pgtable.h>
@@ -570,3 +571,62 @@ void *__kprobes text_poke(void *addr, const void *opcode, size_t len)
570 local_irq_restore(flags); 571 local_irq_restore(flags);
571 return addr; 572 return addr;
572} 573}
574
575/*
576 * Cross-modifying kernel text with stop_machine().
577 * This code originally comes from immediate value.
578 */
579static atomic_t stop_machine_first;
580static int wrote_text;
581
582struct text_poke_params {
583 void *addr;
584 const void *opcode;
585 size_t len;
586};
587
588static int __kprobes stop_machine_text_poke(void *data)
589{
590 struct text_poke_params *tpp = data;
591
592 if (atomic_dec_and_test(&stop_machine_first)) {
593 text_poke(tpp->addr, tpp->opcode, tpp->len);
594 smp_wmb(); /* Make sure other cpus see that this has run */
595 wrote_text = 1;
596 } else {
597 while (!wrote_text)
598 smp_rmb();
599 sync_core();
600 }
601
602 flush_icache_range((unsigned long)tpp->addr,
603 (unsigned long)tpp->addr + tpp->len);
604 return 0;
605}
606
607/**
608 * text_poke_smp - Update instructions on a live kernel on SMP
609 * @addr: address to modify
610 * @opcode: source of the copy
611 * @len: length to copy
612 *
613 * Modify multi-byte instruction by using stop_machine() on SMP. This allows
614 * user to poke/set multi-byte text on SMP. Only non-NMI/MCE code modifying
615 * should be allowed, since stop_machine() does _not_ protect code against
616 * NMI and MCE.
617 *
618 * Note: Must be called under get_online_cpus() and text_mutex.
619 */
620void *__kprobes text_poke_smp(void *addr, const void *opcode, size_t len)
621{
622 struct text_poke_params tpp;
623
624 tpp.addr = addr;
625 tpp.opcode = opcode;
626 tpp.len = len;
627 atomic_set(&stop_machine_first, 1);
628 wrote_text = 0;
629 stop_machine(stop_machine_text_poke, (void *)&tpp, NULL);
630 return addr;
631}
632