Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp2503661pxu; Mon, 7 Dec 2020 08:13:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJwIFEjsBUg7m1n1GoqyJm+zwKGKy9wnVp0MND+mSsUipg+4gGOzfJ1ILUoyxozWmpdXGbKs X-Received: by 2002:a50:bf4a:: with SMTP id g10mr2017878edk.201.1607357598561; Mon, 07 Dec 2020 08:13:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607357598; cv=none; d=google.com; s=arc-20160816; b=s+P0icBFpWGPqBB0/8CQNw8ouzWl1l95n+lEbn32VCdF/xsZlPQUE7Hj0xptQAh3Ou JYajw73TlNOtru5j/qggWVjRmgB2XpgWwkmNLEAcgQaBLdB9uVhkc+q4dSjhF+qn1Xv0 4b7V1KWPvTz5qmlp3ts69TETo6Ha95QSyxVtWA1Lzv6DqbJTSc3P1gfbTCuXldx7RV/P v0X9fJ5UCTZDozzk5ecPn10lDYDztlo2uiKlklrw4GpB+xkvj2WZiqeClTZZ7xlKFrNs L/CUa8TXWYSjxy1hqQutQfd8vBtMt22Irxqyn2P/sMpGlRK9apKQTZF6Degp79ZmkcGJ koJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=Eh0QAkzEYKTW8rBGiJb5/3buh2wDb2AdES6MqxFZBbE=; b=xaYzYozkooxq7UbD8x9DthjVQcZn/tUirdnVmErsofXoVWEcxAAh1ZUTHm3KxrD0SE SdegK6kiekgCUQFrh43eTUo08KY4LGVVJk7CevY1c9N2CSXFfTPpa+mXvJtus2opdR4v 1ckWGm2bh9lZ6fs+NRHuxTicTuRCJoBz6PS69+d+j2A7kVceyrGpqcv3ZcDv5fT/O4Ni SAI+4n12O+wm1r42LAsz6wFyTLKKboq1DNF6aVibJbYS96vNJZQxCgctAhD8YR3lkNoP Bcm4fx6RDuRCjhW/5w88+aGL/uf070WApXGlSkRBqwOs5LuuqeEILboVY2/8yTWxzmaq kM5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AiLHCRzX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id g10si8563124edy.201.2020.12.07.08.12.52; Mon, 07 Dec 2020 08:13:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AiLHCRzX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727658AbgLGQJm (ORCPT + 99 others); Mon, 7 Dec 2020 11:09:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727377AbgLGQJl (ORCPT ); Mon, 7 Dec 2020 11:09:41 -0500 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2B73C061749 for ; Mon, 7 Dec 2020 08:08:20 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id w8so1579467wrv.18 for ; Mon, 07 Dec 2020 08:08:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=Eh0QAkzEYKTW8rBGiJb5/3buh2wDb2AdES6MqxFZBbE=; b=AiLHCRzXCVOotUmQTXRuenZS+bewnQoSEjfScArGmdvLqkYRL3d/fwJm4E1BEYSNna n6emaTnQHwsLsBBB4HsalQUQX9jK1FtJxn4rFspckbWq3t1K9aK7GwbJ3qCQneLhu/TU ow5JYSN8dGHK5QmCESdMXYOLGzxfciyQcU4iCukuVXmqhG60mIlBShI8afQBUyPgLTKW m3tbmW64mMHyJdWWp/P9+OlNOybIiell+w2hg3O0wxNgKWsRcue6Neo/qGP+z3gxS/Xv JeTCNHHAzni5F6h/e5h0nM0H5ivlzFjgx58e4CY3FLK7rwSBUrKlPXlM+rl6BozmJYR8 Dazw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=Eh0QAkzEYKTW8rBGiJb5/3buh2wDb2AdES6MqxFZBbE=; b=jc2wXKaTa5YtG+hkC/YWvP0VF1nXKcocA6ddvo6RFad9UdWabEuGD2oToihLcDh2+0 wQid1dRJTKpm4fL2tZvtf+rG95atlQh57bZAjYbIBl8Xympu9Oi5NwFvMbyCyXCwLQY+ inTxrNidebSiTf6s0SMx5HiCU4w4+Xeh9dxnh7iRioAHy5Syie/qSZyX8ZaMIph5y+4F AVg4yEkd6db5UEXPCSHr9ETPC6h3ZjADHsRMhOrzdewAf3lreQHlSD0ncblnMRlKSaqJ N9Q04/8w7NBvsqfcR9UriiWN1jJQHutRjbtUCaZRDg5fKqJztMMTxFTE5mHzR+baqZsD ic3A== X-Gm-Message-State: AOAM532ZJv7SH0tfeleaDQpPWxREx1MRU7mdwTcRk6JTmARRtqwBmK33 vb/o/9zXmWzfqkmOvEBCKfphauwShuS7vA== Sender: "jackmanb via sendgmr" X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:cc00:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:a1c:791a:: with SMTP id l26mr54824wme.1.1607357299261; Mon, 07 Dec 2020 08:08:19 -0800 (PST) Date: Mon, 7 Dec 2020 16:07:26 +0000 In-Reply-To: <20201207160734.2345502-1-jackmanb@google.com> Message-Id: <20201207160734.2345502-4-jackmanb@google.com> Mime-Version: 1.0 References: <20201207160734.2345502-1-jackmanb@google.com> X-Mailer: git-send-email 2.29.2.576.ga3fc446d84-goog Subject: [PATCH bpf-next v4 03/11] bpf: x86: Factor out a lookup table for some ALU opcodes From: Brendan Jackman To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Yonghong Song , Daniel Borkmann , KP Singh , Florent Revest , linux-kernel@vger.kernel.org, Jann Horn , Brendan Jackman Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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 7106cfd10ba6..f0c98fd275e5 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.29.2.576.ga3fc446d84-goog