Skip to content

Commit

Permalink
bpf, arm64: Impelment bpf_arch_text_poke() for arm64
Browse files Browse the repository at this point in the history
Impelment bpf_arch_text_poke() for arm64, so bpf trampoline code can use
it to replace nop with jump, or replace jump with nop.

Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
Acked-by: Song Liu <songliubraving@fb.com>
  • Loading branch information
Xu Kuohai authored and Kernel Patches Daemon committed Apr 14, 2022
1 parent a96585b commit 265026e
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions arch/arm64/net/bpf_jit_comp.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <linux/bitfield.h>
#include <linux/bpf.h>
#include <linux/memory.h>
#include <linux/filter.h>
#include <linux/printk.h>
#include <linux/slab.h>
Expand All @@ -18,6 +19,7 @@
#include <asm/cacheflush.h>
#include <asm/debug-monitors.h>
#include <asm/insn.h>
#include <asm/patching.h>
#include <asm/set_memory.h>

#include "bpf_jit.h"
Expand Down Expand Up @@ -1529,3 +1531,53 @@ void bpf_jit_free_exec(void *addr)
{
return vfree(addr);
}

static int gen_branch_or_nop(enum aarch64_insn_branch_type type, void *ip,
void *addr, u32 *insn)
{
if (!addr)
*insn = aarch64_insn_gen_nop();
else
*insn = aarch64_insn_gen_branch_imm((unsigned long)ip,
(unsigned long)addr,
type);

return *insn != AARCH64_BREAK_FAULT ? 0 : -EFAULT;
}

int bpf_arch_text_poke(void *ip, enum bpf_text_poke_type poke_type,
void *old_addr, void *new_addr)
{
int ret;
u32 old_insn;
u32 new_insn;
u32 replaced;
enum aarch64_insn_branch_type branch_type;

if (poke_type == BPF_MOD_CALL)
branch_type = AARCH64_INSN_BRANCH_LINK;
else
branch_type = AARCH64_INSN_BRANCH_NOLINK;

if (gen_branch_or_nop(branch_type, ip, old_addr, &old_insn) < 0)
return -EFAULT;

if (gen_branch_or_nop(branch_type, ip, new_addr, &new_insn) < 0)
return -EFAULT;

mutex_lock(&text_mutex);
if (aarch64_insn_read(ip, &replaced)) {
ret = -EFAULT;
goto out;
}

if (replaced != old_insn) {
ret = -EFAULT;
goto out;
}

ret = aarch64_insn_patch_text_nosync((void *)ip, new_insn);
out:
mutex_unlock(&text_mutex);
return ret;
}

0 comments on commit 265026e

Please sign in to comment.