Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp477450pxb; Thu, 14 Jan 2021 10:21:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJyybJ07SG0Oo6BQT9jqOXVvYub181aDvXi4BG6kOWPWuuApZDPyyDIBfMxcg2r9dRGEJQec X-Received: by 2002:aa7:c403:: with SMTP id j3mr6546092edq.217.1610648516244; Thu, 14 Jan 2021 10:21:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610648516; cv=none; d=google.com; s=arc-20160816; b=xFXoBSBi03EJJhS6E6ixlXpBHQctRQofyThyUnNlbTGPh71jPKL2duMkIQxT07wvv5 nxQvYqvMfiQqT1XcUYL+AiYfnJVfpFL4LYqXCsAnahPePFrC1hi9/ZmZl/0TK/iUZFGL 7eRPvy2ynaEPpyo9n7wDicWU/zwYvrdeobdRb/NnOE6Zaac1QEeC2BfO6BD7Zi5gwVfI dq0H84wLz1uvEPOjgg8z6xbxQ2OYfBayXOo2XrNNBoxFeeRM6u48nVIh91l7SgJbi6Qs D0pyyPbf02M+INKRhlp/7xPhxG6i07TdQZBlP1aPBdVwT+XMDyIhGDmhHVqD3radmqv+ dYLQ== 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=ZTGQEWpWAiK+AJl9ZPeXz4p19Gfj2HCZL+15XnqwBus=; b=IYKMn+aMu62j9jKMlNRZ+QP/M8gW0dnZ5fHRzQafYjk3YP0k6eM1gb72qUFC+9KQpB HdgOhADwapHVvPrrVYcLVmIc31JBiKqC941ThuVgynOgQTdTZ0N1TM5c0zI5cgkkfLCE 5s9mFEEIOh9glQid7cfO2qUJYbaCQ5jxiiz9OdlrXnO8STcdiRTDEDZxtT4Mr203cX7g 73wdG8T4tm+VNmgpzBTNNkMxOtFvIeE/FuHDmcslrbfA0najRCmHjXK3Zl0/1dVMhqFo 3Ipc6F5KWtN4emApdXDd8A3bxFLwYZ8sfWbu4C6zIn2Q+SNyQV2+9rAY5HkWJQp4CScU SnBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=vGd4GGM3; 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 l19si554022ejx.163.2021.01.14.10.21.31; Thu, 14 Jan 2021 10:21:56 -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=vGd4GGM3; 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 S1726287AbhANSTb (ORCPT + 99 others); Thu, 14 Jan 2021 13:19:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725950AbhANST2 (ORCPT ); Thu, 14 Jan 2021 13:19:28 -0500 Received: from mail-wm1-x34a.google.com (mail-wm1-x34a.google.com [IPv6:2a00:1450:4864:20::34a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBB47C061786 for ; Thu, 14 Jan 2021 10:18:13 -0800 (PST) Received: by mail-wm1-x34a.google.com with SMTP id f18so2190855wmq.1 for ; Thu, 14 Jan 2021 10:18:13 -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=ZTGQEWpWAiK+AJl9ZPeXz4p19Gfj2HCZL+15XnqwBus=; b=vGd4GGM3w5XE6M/i617DaYJS+kENfz87vmlw2tsz0M5NYCpCKQfWHCFzKe4Tf0j9ES NEIWfdG/VGrTz31tHYltF8nnOHNXQ1IWHPHo017ZgDOthgTqDs8RfnqvvK1xsM/sOfcp t+Jt3oxqal/zmhRI5hrRBwNLU5l92SYVlxIrE9wpWQSo+k9oyXyBo/71BeKm6Z4ZuOAG DSR3u0Tprz7Nj830fQDawnQfRaH7GmdXwjUr5bYcxI0OocpXdQiE4ZrlcercArLhALbh 0r/vCvFR4wJVqPC/xYTgEamEbuONQSJ7OBmPuRLwT07Mwt+sgyU/ABi+hZhgGQuQtEfs INig== 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=ZTGQEWpWAiK+AJl9ZPeXz4p19Gfj2HCZL+15XnqwBus=; b=euKKIGIThO5PoZAyWu7AnWfZ/Pma56Y5Rkm4Hir1z8Lg/6PjvCU9rwDCBF18MIwL2e +RL5DGumDzRDTkfWus8vvJGV+tY4qiWlbrkGN80cJs9PmQAmevg4pSjiCcUbHFj0OhvL F7NLREQGBd2HDRhUxIjAdGF8/QAaRxFYNC+tQKtIADx5gN4by5uc6AAqTFyAj96UmmLt PrvkExhBl23Iy5vzBNG1HYNyj44VrOIN9AwAdgKbLJZbN1uh73cMU7RaR/K+CGqnUPPY uxoVOW5H4TCZpExA3g0yxgkk60aljD38d7e1bMDCwu/3thS95UBmiRb6AcL/Y/SyEv+e bQbQ== X-Gm-Message-State: AOAM530kr+9gXo+hUkMrZBvqkNSc7dHoaG95qow3JQpiKiUNHPdbLFL8 ihbL4AJFRuexoPfeO/DnlwBw1KaCHHF+hQ== 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:a05:600c:2057:: with SMTP id p23mr5028719wmg.109.1610648292636; Thu, 14 Jan 2021 10:18:12 -0800 (PST) Date: Thu, 14 Jan 2021 18:17:43 +0000 In-Reply-To: <20210114181751.768687-1-jackmanb@google.com> Message-Id: <20210114181751.768687-4-jackmanb@google.com> Mime-Version: 1.0 References: <20210114181751.768687-1-jackmanb@google.com> X-Mailer: git-send-email 2.30.0.284.gd98b1dd5eaa7-goog Subject: [PATCH bpf-next v7 03/11] bpf: x86: Factor out a lookup table for some ALU opcodes From: Brendan Jackman To: bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , KP Singh , Florent Revest , linux-kernel@vger.kernel.org, "=?UTF-8?q?Bj=C3=B6rn=20T=C3=B6pel?=" , John Fastabend , Yonghong Song , 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 Acked-by: John Fastabend --- 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