x86/static_call: Add out-of-line static call implementation
[linux-2.6-microblaze.git] / arch / x86 / kernel / static_call.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/static_call.h>
3 #include <linux/memory.h>
4 #include <linux/bug.h>
5 #include <asm/text-patching.h>
6
7 static void __static_call_transform(void *insn, u8 opcode, void *func)
8 {
9         const void *code = text_gen_insn(opcode, insn, func);
10
11         if (WARN_ONCE(*(u8 *)insn != opcode,
12                       "unexpected static call insn opcode 0x%x at %pS\n",
13                       opcode, insn))
14                 return;
15
16         if (memcmp(insn, code, CALL_INSN_SIZE) == 0)
17                 return;
18
19         text_poke_bp(insn, code, CALL_INSN_SIZE, NULL);
20 }
21
22 void arch_static_call_transform(void *site, void *tramp, void *func)
23 {
24         mutex_lock(&text_mutex);
25
26         if (tramp)
27                 __static_call_transform(tramp, JMP32_INSN_OPCODE, func);
28
29         mutex_unlock(&text_mutex);
30 }
31 EXPORT_SYMBOL_GPL(arch_static_call_transform);