Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1817896pxu; Fri, 27 Nov 2020 16:33:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJzcPBw9cLnrCBbfPsyIbfjR0cF9AdvYSW8PVnEtSnynabcKLur3sEXy5vjd0EEY5dax1LPp X-Received: by 2002:a17:907:262d:: with SMTP id aq13mr10588718ejc.484.1606523628095; Fri, 27 Nov 2020 16:33:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606523628; cv=none; d=google.com; s=arc-20160816; b=aX3kIxGe1AqTq6YAoR8PlxxH/k41CLy1B0s9XNaulpHZuoaUx0i3DQmhPIL7gB9mj6 MII2aZ5tdD+bHCvlTju4Hut4iATF4aq7ZzDDl/6okBZTOf720hg7DfqSToO0IiXCgiIh ZabY/WY4kP74BvA+spgGUF5iYrt4O2icQViGiRODAprDCQ0lDuF6StYwnrGlhu126WDv X1+mA1RIFbhSTYu5Ap+8bLTtfXHdoxlcNtumGxyJa1/o8gR7gzh1qeptaF871BTe34Mv /xiiVjmvrYm8G8A6/Sqbj/aUJglWMVvQzy+Ba++e7KZU7X0yN7oBLF3A67uwCWfg59qe CVew== 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=H22q+ouf7u9LzglpF3czxswAZ/tYvPaf24TLGDwbSdY=; b=MEPVNDZjMKrm0PHYZMFYYqaGcL8qPVj7lCLTtY38iq5Wt1Kc1W4q25Xthtjd5dDsUl FWrXgOHxyUol4Ap/z5ekSyzW25U5EJ76q5kzFTLJ4pAocomcM/bSExf4hFIHO+iN72A4 L4CN3XbqxPYSf7sqvq0XDIGEMQeRb87Ckx+d+aAVHcBbK7sJEcAb3rAtupD7HVthIeyV RUOU5Sn9OJDGSt2FQENaQpXiffza9wqGke8o0t5f+L9zHhAnuNl9oGNhlXdoy4a7kjm/ npFed75+8DruUkXuIq7e8RurrdxTnsZ14aQVO5CT87L/xIHKjz8H6hXmRABekQ4fz9Wy mBPg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=q7oY8MHB; 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 c27si3624563edj.80.2020.11.27.16.33.25; Fri, 27 Nov 2020 16:33:48 -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=q7oY8MHB; 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 S1732866AbgK0R57 (ORCPT + 99 others); Fri, 27 Nov 2020 12:57:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37744 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730985AbgK0R56 (ORCPT ); Fri, 27 Nov 2020 12:57:58 -0500 Received: from mail-wr1-x44a.google.com (mail-wr1-x44a.google.com [IPv6:2a00:1450:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D172EC0613D1 for ; Fri, 27 Nov 2020 09:57:57 -0800 (PST) Received: by mail-wr1-x44a.google.com with SMTP id g5so3771599wrp.5 for ; Fri, 27 Nov 2020 09:57:57 -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=H22q+ouf7u9LzglpF3czxswAZ/tYvPaf24TLGDwbSdY=; b=q7oY8MHBuEHwS6KdXZOjzfRIdqqQ9Gq5GgEfXpJ3lBShIXyDo2xtCpN5S+0k4mqqJv BP9FV9uH44XTe0Pkm5Deh9ldWyDwefxmMlDwSiRqpZknx2J2vHuUpTTJY8xPyfxwKTqf 1yohpDY3jSIg13YPnnrKMQNwSeap0PQrQoBiVOCEH+byznGShV6BsBJ2zlemsleoKOoC EcpHSVCj4+2zuuhnoX9K+8Fsmf9uJVOdYeGslR7uGvnQfNFkdZ5jqW327+1QtxbpgnMI PStcypAW1hJ0odoCypA2OdL+yL2M3UwM8ZbgI8T/yH148rMN4gL5I3YbZvxRO8FXIvzA KOMQ== 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=H22q+ouf7u9LzglpF3czxswAZ/tYvPaf24TLGDwbSdY=; b=swxevfIPTkFI6X7esKblxwa4EuTAPFiBeNPxJpBCjyeSmKiqvNFUNCt0QFzx2p/4N5 HV34TYaRYMjRJzI9/6/lfmM4VWgrwLiAeshpaUXe93W1X0XamBCzY/qQ+oEswNDEdx5k nIMhxxFHqqDgqyRJ03WTJSoIPzGb4plCUiisNtKqqKGtQGj5/Umq3WyLCqlRPQ866Fox aRPblyDSkFP7Cdnn9XSlTJ/jnJfUKQo/y9Zq9AIFNcL+1ezZdc8+zb+WeyBU0qZR3Cit OZE15hDBRy6tx6YN0Tv8msNNKiX+z3CHTyK9ktTa5OfurHWAiK2yWjp+q7RPWDTNfZOf FiIQ== X-Gm-Message-State: AOAM530D76llfUnZlLCQn/dNi/IszCzKEgjWypavXKKqHIAG3I6m0YL8 CYmKCs9ArR4agjpBu0oHIwitHevBGHexOg== 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:61c2:: with SMTP id v185mr10546867wmb.152.1606499876503; Fri, 27 Nov 2020 09:57:56 -0800 (PST) Date: Fri, 27 Nov 2020 17:57:29 +0000 In-Reply-To: <20201127175738.1085417-1-jackmanb@google.com> Message-Id: <20201127175738.1085417-5-jackmanb@google.com> Mime-Version: 1.0 References: <20201127175738.1085417-1-jackmanb@google.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH v2 bpf-next 04/13] 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 49dea0c1a130..9ecee9d018ac 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 */ @@ -878,15 +890,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_rex(&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; @@ -1063,12 +1069,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 @@ -1102,11 +1103,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.454.gaff20da3a2-goog