Commit 02a6dfa8 authored by Menglong Dong's avatar Menglong Dong Committed by Alexei Starovoitov

bpf, x86: save/restore regs with BPF_DW size

As we already reserve 8 byte in the stack for each reg, it is ok to
store/restore the regs in BPF_DW size. This will make the code in
save_regs()/restore_regs() simpler.
Signed-off-by: default avatarMenglong Dong <imagedong@tencent.com>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/r/20230713040738.1789742-2-imagedong@tencent.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 0a5550b1
...@@ -1860,57 +1860,34 @@ st: if (is_imm8(insn->off)) ...@@ -1860,57 +1860,34 @@ st: if (is_imm8(insn->off))
static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_regs, static void save_regs(const struct btf_func_model *m, u8 **prog, int nr_regs,
int stack_size) int stack_size)
{ {
int i, j, arg_size; int i;
bool next_same_struct = false;
/* Store function arguments to stack. /* Store function arguments to stack.
* For a function that accepts two pointers the sequence will be: * For a function that accepts two pointers the sequence will be:
* mov QWORD PTR [rbp-0x10],rdi * mov QWORD PTR [rbp-0x10],rdi
* mov QWORD PTR [rbp-0x8],rsi * mov QWORD PTR [rbp-0x8],rsi
*/ */
for (i = 0, j = 0; i < min(nr_regs, 6); i++) { for (i = 0; i < min(nr_regs, 6); i++)
/* The arg_size is at most 16 bytes, enforced by the verifier. */ emit_stx(prog, BPF_DW, BPF_REG_FP,
arg_size = m->arg_size[j];
if (arg_size > 8) {
arg_size = 8;
next_same_struct = !next_same_struct;
}
emit_stx(prog, bytes_to_bpf_size(arg_size),
BPF_REG_FP,
i == 5 ? X86_REG_R9 : BPF_REG_1 + i, i == 5 ? X86_REG_R9 : BPF_REG_1 + i,
-(stack_size - i * 8)); -(stack_size - i * 8));
j = next_same_struct ? j : j + 1;
}
} }
static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_regs, static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_regs,
int stack_size) int stack_size)
{ {
int i, j, arg_size; int i;
bool next_same_struct = false;
/* Restore function arguments from stack. /* Restore function arguments from stack.
* For a function that accepts two pointers the sequence will be: * For a function that accepts two pointers the sequence will be:
* EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10] * EMIT4(0x48, 0x8B, 0x7D, 0xF0); mov rdi,QWORD PTR [rbp-0x10]
* EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8] * EMIT4(0x48, 0x8B, 0x75, 0xF8); mov rsi,QWORD PTR [rbp-0x8]
*/ */
for (i = 0, j = 0; i < min(nr_regs, 6); i++) { for (i = 0; i < min(nr_regs, 6); i++)
/* The arg_size is at most 16 bytes, enforced by the verifier. */ emit_ldx(prog, BPF_DW,
arg_size = m->arg_size[j];
if (arg_size > 8) {
arg_size = 8;
next_same_struct = !next_same_struct;
}
emit_ldx(prog, bytes_to_bpf_size(arg_size),
i == 5 ? X86_REG_R9 : BPF_REG_1 + i, i == 5 ? X86_REG_R9 : BPF_REG_1 + i,
BPF_REG_FP, BPF_REG_FP,
-(stack_size - i * 8)); -(stack_size - i * 8));
j = next_same_struct ? j : j + 1;
}
} }
static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog, static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment