1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/static_call.h>
3 #include <linux/memory.h>
5 #include <asm/text-patching.h>
8 CALL = 0, /* site call */
9 NOP = 1, /* site cond-call */
10 JMP = 2, /* tramp / site tail-call */
11 RET = 3, /* tramp / site cond-tail-call */
15 * data16 data16 xorq %rax, %rax - a single 5 byte instruction that clears %rax
16 * The REX.W cancels the effect of any data16.
18 static const u8 xor5rax[] = { 0x66, 0x66, 0x48, 0x31, 0xc0 };
20 static void __ref __static_call_transform(void *insn, enum insn_type type, void *func)
22 const void *emulate = NULL;
23 int size = CALL_INSN_SIZE;
28 code = text_gen_insn(CALL_INSN_OPCODE, insn, func);
29 if (func == &__static_call_return0) {
41 code = text_gen_insn(JMP32_INSN_OPCODE, insn, func);
45 code = text_gen_insn(RET_INSN_OPCODE, insn, func);
50 if (memcmp(insn, code, size) == 0)
53 if (unlikely(system_state == SYSTEM_BOOTING))
54 return text_poke_early(insn, code, size);
56 text_poke_bp(insn, code, size, emulate);
59 static void __static_call_validate(void *insn, bool tail)
61 u8 opcode = *(u8 *)insn;
64 if (opcode == JMP32_INSN_OPCODE ||
65 opcode == RET_INSN_OPCODE)
68 if (opcode == CALL_INSN_OPCODE ||
69 !memcmp(insn, x86_nops[5], 5) ||
70 !memcmp(insn, xor5rax, 5))
75 * If we ever trigger this, our text is corrupt, we'll probably not live long.
77 WARN_ONCE(1, "unexpected static_call insn opcode 0x%x at %pS\n", opcode, insn);
80 static inline enum insn_type __sc_insn(bool null, bool tail)
83 * Encode the following table without branches:
86 * -----+-------+------
95 void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
97 mutex_lock(&text_mutex);
100 __static_call_validate(tramp, true);
101 __static_call_transform(tramp, __sc_insn(!func, true), func);
104 if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site) {
105 __static_call_validate(site, tail);
106 __static_call_transform(site, __sc_insn(!func, tail), func);
109 mutex_unlock(&text_mutex);
111 EXPORT_SYMBOL_GPL(arch_static_call_transform);