2020-08-18 13:57:44 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/static_call.h>
|
|
|
|
#include <linux/memory.h>
|
|
|
|
#include <linux/bug.h>
|
|
|
|
#include <asm/text-patching.h>
|
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
enum insn_type {
|
|
|
|
CALL = 0, /* site call */
|
|
|
|
NOP = 1, /* site cond-call */
|
|
|
|
JMP = 2, /* tramp / site tail-call */
|
|
|
|
RET = 3, /* tramp / site cond-tail-call */
|
|
|
|
};
|
|
|
|
|
|
|
|
static void __static_call_transform(void *insn, enum insn_type type, void *func)
|
2020-08-18 13:57:44 +00:00
|
|
|
{
|
2020-08-18 13:57:48 +00:00
|
|
|
int size = CALL_INSN_SIZE;
|
|
|
|
const void *code;
|
2020-08-18 13:57:44 +00:00
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
switch (type) {
|
|
|
|
case CALL:
|
|
|
|
code = text_gen_insn(CALL_INSN_OPCODE, insn, func);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case NOP:
|
|
|
|
code = ideal_nops[NOP_ATOMIC5];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JMP:
|
|
|
|
code = text_gen_insn(JMP32_INSN_OPCODE, insn, func);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RET:
|
|
|
|
code = text_gen_insn(RET_INSN_OPCODE, insn, func);
|
|
|
|
size = RET_INSN_SIZE;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-18 13:57:44 +00:00
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
if (memcmp(insn, code, size) == 0)
|
2020-08-18 13:57:44 +00:00
|
|
|
return;
|
|
|
|
|
2020-08-18 13:57:48 +00:00
|
|
|
text_poke_bp(insn, code, size, NULL);
|
2020-08-18 13:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void arch_static_call_transform(void *site, void *tramp, void *func)
|
|
|
|
{
|
|
|
|
mutex_lock(&text_mutex);
|
|
|
|
|
|
|
|
if (tramp)
|
2020-08-18 13:57:48 +00:00
|
|
|
__static_call_transform(tramp, func ? JMP : RET, func);
|
2020-08-18 13:57:44 +00:00
|
|
|
|
2020-08-18 13:57:45 +00:00
|
|
|
if (IS_ENABLED(CONFIG_HAVE_STATIC_CALL_INLINE) && site)
|
2020-08-18 13:57:48 +00:00
|
|
|
__static_call_transform(site, func ? CALL : NOP, func);
|
2020-08-18 13:57:45 +00:00
|
|
|
|
2020-08-18 13:57:44 +00:00
|
|
|
mutex_unlock(&text_mutex);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(arch_static_call_transform);
|