linux/arch/powerpc/kernel/static_call.c
Christophe Leroy f50b45626e powerpc/static_call: Implement inline static calls
Implement inline static calls:
- Put a 'bl' to the destination function ('b' if tail call)
- Put a 'nop' when the destination function is NULL ('blr' if tail call)
- Put a 'li r3,0' when the destination is the RET0 function and not
a tail call.

If the destination is too far (over the 32Mb limit), go via the
trampoline.

Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com>
Link: https://patch.msgid.link/3dbd0b2ba577c942729235d0211d04a406653d81.1733245362.git.christophe.leroy@csgroup.eu
2025-02-26 21:09:43 +05:30

65 lines
1.7 KiB
C

// SPDX-License-Identifier: GPL-2.0
#include <linux/memory.h>
#include <linux/static_call.h>
#include <asm/text-patching.h>
void arch_static_call_transform(void *site, void *tramp, void *func, bool tail)
{
int err;
bool is_ret0 = (func == __static_call_return0);
unsigned long _tramp = (unsigned long)tramp;
unsigned long _func = (unsigned long)func;
unsigned long _ret0 = _tramp + PPC_SCT_RET0;
bool is_short = is_offset_in_branch_range((long)func - (long)(site ? : tramp));
mutex_lock(&text_mutex);
if (site && tail) {
if (!func)
err = patch_instruction(site, ppc_inst(PPC_RAW_BLR()));
else if (is_ret0)
err = patch_branch(site, _ret0, 0);
else if (is_short)
err = patch_branch(site, _func, 0);
else if (tramp)
err = patch_branch(site, _tramp, 0);
else
err = 0;
} else if (site) {
if (!func)
err = patch_instruction(site, ppc_inst(PPC_RAW_NOP()));
else if (is_ret0)
err = patch_instruction(site, ppc_inst(PPC_RAW_LI(_R3, 0)));
else if (is_short)
err = patch_branch(site, _func, BRANCH_SET_LINK);
else if (tramp)
err = patch_branch(site, _tramp, BRANCH_SET_LINK);
else
err = 0;
} else if (tramp) {
if (func && !is_short) {
err = patch_ulong(tramp + PPC_SCT_DATA, _func);
if (err)
goto out;
}
if (!func)
err = patch_instruction(tramp, ppc_inst(PPC_RAW_BLR()));
else if (is_ret0)
err = patch_branch(tramp, _ret0, 0);
else if (is_short)
err = patch_branch(tramp, _func, 0);
else
err = patch_instruction(tramp, ppc_inst(PPC_RAW_NOP()));
} else {
err = 0;
}
out:
mutex_unlock(&text_mutex);
if (err)
panic("%s: patching failed %pS at %pS\n", __func__, func, tramp);
}
EXPORT_SYMBOL_GPL(arch_static_call_transform);