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 */
14 static void __ref __static_call_transform(void *insn, enum insn_type type, void *func)
16 int size = CALL_INSN_SIZE;
21 code = text_gen_insn(CALL_INSN_OPCODE, insn, func);
25 code = ideal_nops[NOP_ATOMIC5];
29 code = text_gen_insn(JMP32_INSN_OPCODE, insn, func);
33 code = text_gen_insn(RET_INSN_OPCODE, insn, func);
38 if (memcmp(insn, code, size) == 0)
41 if (unlikely(system_state == SYSTEM_BOOTING))
42 return text_poke_early(insn, code, size);
44 text_poke_bp(insn, code, size, NULL);
47 static void __static_call_validate(void *insn, bool tail)
49 u8 opcode = *(u8 *)insn;
52 if (opcode == JMP32_INSN_OPCODE ||
53 opcode == RET_INSN_OPCODE)
56 if (opcode == CALL_INSN_OPCODE ||
57 !memcmp(insn, ideal_nops[NOP_ATOMIC5], 5))
62 * If we ever trigger this, our text is corrupt, we'll probably not live long.
64 WARN_ONCE(1, "unexpected static_call insn opcode 0x%x at %pS\n", opcode, insn);
67 static inline enum insn_type __sc_insn(bool null, bool tail)
70 * Encode the following table without branches:
73 * -----+-------+------
82 void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
84 mutex_lock(&text_mutex);
87 __static_call_validate(tramp, true);
88 __static_call_transform(tramp, __sc_insn(!func, true), func);
91 if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site) {
92 __static_call_validate(site, tail);
93 __static_call_transform(site, __sc_insn(!func, tail), func);
96 mutex_unlock(&text_mutex);
98 EXPORT_SYMBOL_GPL(arch_static_call_transform);