From: Brendan Jackman <jackmanb@google.com> To: bpf@vger.kernel.org Cc: "Alexei Starovoitov" <ast@kernel.org>, "Daniel Borkmann" <daniel@iogearbox.net>, "Andrii Nakryiko" <andrii.nakryiko@gmail.com>, "KP Singh" <kpsingh@chromium.org>, "Florent Revest" <revest@chromium.org>, linux-kernel@vger.kernel.org, "Björn Töpel" <bjorn.topel@gmail.com>, "John Fastabend" <john.fastabend@gmail.com>, "Yonghong Song" <yhs@fb.com>, "Brendan Jackman" <jackmanb@google.com> Subject: [PATCH bpf-next v7 03/11] bpf: x86: Factor out a lookup table for some ALU opcodes Date: Thu, 14 Jan 2021 18:17:43 +0000 Message-ID: <20210114181751.768687-4-jackmanb@google.com> (raw) In-Reply-To: <20210114181751.768687-1-jackmanb@google.com> A later commit will need to lookup a subset of these opcodes. To avoid duplicating code, pull out a table. The shift opcodes won't be needed by that later commit, but they're already duplicated, so fold them into the table anyway. Signed-off-by: Brendan Jackman <jackmanb@google.com> Acked-by: John Fastabend <john.fastabend@gmail.com> --- arch/x86/net/bpf_jit_comp.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index f15c93275a18..93f32e0ba0ef 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -205,6 +205,18 @@ static u8 add_2reg(u8 byte, u32 dst_reg, u32 src_reg) return byte + reg2hex[dst_reg] + (reg2hex[src_reg] << 3); } +/* Some 1-byte opcodes for binary ALU operations */ +static u8 simple_alu_opcodes[] = { + [BPF_ADD] = 0x01, + [BPF_SUB] = 0x29, + [BPF_AND] = 0x21, + [BPF_OR] = 0x09, + [BPF_XOR] = 0x31, + [BPF_LSH] = 0xE0, + [BPF_RSH] = 0xE8, + [BPF_ARSH] = 0xF8, +}; + static void jit_fill_hole(void *area, unsigned int size) { /* Fill whole space with INT3 instructions */ @@ -862,15 +874,9 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, case BPF_ALU64 | BPF_AND | BPF_X: case BPF_ALU64 | BPF_OR | BPF_X: case BPF_ALU64 | BPF_XOR | BPF_X: - switch (BPF_OP(insn->code)) { - case BPF_ADD: b2 = 0x01; break; - case BPF_SUB: b2 = 0x29; break; - case BPF_AND: b2 = 0x21; break; - case BPF_OR: b2 = 0x09; break; - case BPF_XOR: b2 = 0x31; break; - } maybe_emit_mod(&prog, dst_reg, src_reg, BPF_CLASS(insn->code) == BPF_ALU64); + b2 = simple_alu_opcodes[BPF_OP(insn->code)]; EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg)); break; @@ -1050,12 +1056,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, else if (is_ereg(dst_reg)) EMIT1(add_1mod(0x40, dst_reg)); - switch (BPF_OP(insn->code)) { - case BPF_LSH: b3 = 0xE0; break; - case BPF_RSH: b3 = 0xE8; break; - case BPF_ARSH: b3 = 0xF8; break; - } - + b3 = simple_alu_opcodes[BPF_OP(insn->code)]; if (imm32 == 1) EMIT2(0xD1, add_1reg(b3, dst_reg)); else @@ -1089,11 +1090,7 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, else if (is_ereg(dst_reg)) EMIT1(add_1mod(0x40, dst_reg)); - switch (BPF_OP(insn->code)) { - case BPF_LSH: b3 = 0xE0; break; - case BPF_RSH: b3 = 0xE8; break; - case BPF_ARSH: b3 = 0xF8; break; - } + b3 = simple_alu_opcodes[BPF_OP(insn->code)]; EMIT2(0xD3, add_1reg(b3, dst_reg)); if (src_reg != BPF_REG_4) -- 2.30.0.284.gd98b1dd5eaa7-goog
next prev parent reply index Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-01-14 18:17 [PATCH bpf-next v7 00/11] Atomics for eBPF Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 01/11] bpf: x86: Factor out emission of ModR/M for *(reg + off) Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 02/11] bpf: x86: Factor out emission of REX byte Brendan Jackman 2021-01-14 18:17 ` Brendan Jackman [this message] 2021-01-14 18:17 ` [PATCH bpf-next v7 04/11] bpf: Rename BPF_XADD and prepare to encode other atomics in .imm Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 05/11] bpf: Move BPF_STX reserved field check into BPF_STX verifier code Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 06/11] bpf: Add BPF_FETCH field / create atomic_fetch_add instruction Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 07/11] bpf: Add instructions for atomic_[cmp]xchg Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 08/11] bpf: Pull out a macro for interpreting atomic ALU operations Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 09/11] bpf: Add bitwise atomic instructions Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 10/11] bpf: Add tests for new BPF atomic operations Brendan Jackman 2021-01-14 18:17 ` [PATCH bpf-next v7 11/11] bpf: Document new atomic instructions Brendan Jackman 2021-01-15 3:20 ` [PATCH bpf-next v7 00/11] Atomics for eBPF patchwork-bot+netdevbpf 2021-01-15 3:23 ` Alexei Starovoitov
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20210114181751.768687-4-jackmanb@google.com \ --to=jackmanb@google.com \ --cc=andrii.nakryiko@gmail.com \ --cc=ast@kernel.org \ --cc=bjorn.topel@gmail.com \ --cc=bpf@vger.kernel.org \ --cc=daniel@iogearbox.net \ --cc=john.fastabend@gmail.com \ --cc=kpsingh@chromium.org \ --cc=linux-kernel@vger.kernel.org \ --cc=revest@chromium.org \ --cc=yhs@fb.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
BPF Archive on lore.kernel.org Archives are clonable: git clone --mirror https://lore.kernel.org/bpf/0 bpf/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 bpf bpf/ https://lore.kernel.org/bpf \ bpf@vger.kernel.org public-inbox-index bpf Example config snippet for mirrors Newsgroup available over NNTP: nntp://nntp.lore.kernel.org/org.kernel.vger.bpf AGPL code for this site: git clone https://public-inbox.org/public-inbox.git