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 * cs cs cs xorl %eax, %eax - a single 5 byte instruction that clears %[er]ax
17 static const u8 xor5rax
[] = { 0x2e, 0x2e, 0x2e, 0x31, 0xc0 };
19 static void __ref
__static_call_transform(void *insn
, enum insn_type type
, void *func
)
21 const void *emulate
= NULL
;
22 int size
= CALL_INSN_SIZE
;
27 code
= text_gen_insn(CALL_INSN_OPCODE
, insn
, func
);
28 if (func
== &__static_call_return0
) {
40 code
= text_gen_insn(JMP32_INSN_OPCODE
, insn
, func
);
44 code
= text_gen_insn(RET_INSN_OPCODE
, insn
, func
);
49 if (memcmp(insn
, code
, size
) == 0)
52 if (unlikely(system_state
== SYSTEM_BOOTING
))
53 return text_poke_early(insn
, code
, size
);
55 text_poke_bp(insn
, code
, size
, emulate
);
58 static void __static_call_validate(void *insn
, bool tail
)
60 u8 opcode
= *(u8
*)insn
;
63 if (opcode
== JMP32_INSN_OPCODE
||
64 opcode
== RET_INSN_OPCODE
)
67 if (opcode
== CALL_INSN_OPCODE
||
68 !memcmp(insn
, x86_nops
[5], 5) ||
69 !memcmp(insn
, xor5rax
, 5))
74 * If we ever trigger this, our text is corrupt, we'll probably not live long.
76 WARN_ONCE(1, "unexpected static_call insn opcode 0x%x at %pS\n", opcode
, insn
);
79 static inline enum insn_type
__sc_insn(bool null
, bool tail
)
82 * Encode the following table without branches:
85 * -----+-------+------
94 void arch_static_call_transform(void *site
, void *tramp
, void *func
, bool tail
)
96 mutex_lock(&text_mutex
);
99 __static_call_validate(tramp
, true);
100 __static_call_transform(tramp
, __sc_insn(!func
, true), func
);
103 if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE
) && site
) {
104 __static_call_validate(site
, tail
);
105 __static_call_transform(site
, __sc_insn(!func
, tail
), func
);
108 mutex_unlock(&text_mutex
);
110 EXPORT_SYMBOL_GPL(arch_static_call_transform
);