Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp598154pxu; Thu, 3 Dec 2020 08:07:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJw+0fVGN4JWhzrRsZJDYe6SUOTVOGUT9ssbOhGt56EB/f9jATrMzS7q+uGmq4tUfDIo3pIF X-Received: by 2002:a17:906:2806:: with SMTP id r6mr2940465ejc.130.1607011638501; Thu, 03 Dec 2020 08:07:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607011638; cv=none; d=google.com; s=arc-20160816; b=qUnqE5aiz4FMi1P4HjFOL+e3QK/3O4vU/Hdjs2DaIn1eBIuVqNgNOdgdj8LYVB9q80 0K0+0mdI6ctkb4DvNeei2Gam10fED2QInusSZmC+jd4+g4ClOiC/Ud8RB/szBR1LoO7A UHVTWW8xZIJboHpFUsfqoVfl7NG9XmBzKf3APIDYcn0g+51As27S7jb1Qbh728zMZn/A 4fMioXogIY2bcIMQ4hEM/WJGQjh/djlpPnpG0iYtLHjyTJ+mtVoJwVqL6RKfvDP8ZBI7 /yRCSCgQc5XVsZCVtyiij2sbnkrd6c9VcwAoYnXtDNdl0eqcgoyOfNYbBP7zMJgUOmB5 6yuA== 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=2Q2lne0lXLlvS3rxE7b9vuMB9y2K2LQZIZMl35a4JXk=; b=ytydNxOZ5w9GzrmSL+GePe4lxq76/mu2lIlfC2gp2ZPqBj5pC646RIGkS9lMsz7Neu /d16cNKX+5Bzyyh7ejzofoLyO/O0ASLx76SGfD3x41uso1uARdatI11+CEbQ/pUXwhOu gaghgyqhYnMlXKA/u1xwlWUuWSUqLhRBg/XsJWgIPSpvsU5WAO5nDFKyNE0ad+uh416A PdXRj3F748RoZawhfELGdqJnyX9KEOH0a14cswGb0k0Nq0+DW/ANlCNjt9WmpYtVFI6q jgw97mSYwwMB/3z0ONrRswL7srPKlE4ylwdr04mR702vDeTq3T2xjCyw25XWLAwYN9sT J0fw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="vcJ4+79/"; 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 p1si1240435ejw.42.2020.12.03.08.06.54; Thu, 03 Dec 2020 08:07: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="vcJ4+79/"; 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 S2436644AbgLCQEc (ORCPT + 99 others); Thu, 3 Dec 2020 11:04:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54818 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2436587AbgLCQEb (ORCPT ); Thu, 3 Dec 2020 11:04:31 -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 9C24CC08E85E for ; Thu, 3 Dec 2020 08:03:19 -0800 (PST) Received: by mail-wr1-x449.google.com with SMTP id z13so1374734wrm.19 for ; Thu, 03 Dec 2020 08:03:19 -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=2Q2lne0lXLlvS3rxE7b9vuMB9y2K2LQZIZMl35a4JXk=; b=vcJ4+79//CdUXs9AdX9SPf05uB/G63aUP5cG4KzyXo/tvvxbbWt6n+3Qx73s7O3Dpb v1L95XWtfWCpKts52CEIq8d2OwA88kBC67aOerfydzROVWbBDykc1fntIekGZgB/OjKX sR3DEEv/H6mFORpM0vcEOlh3GxtOvqkMfy/89+7ORcFWEBgSAHd8U0pWVWjwYSsmym14 +FAYpqcpU5vR/l8ZboEngQxDOMgFyrXSemfzZloks/lv/qLV0QZxN7kjkm/Hz18U3ugR ffZn/FTZkJVMZ2GVSfcGAsWqtz8S4AfL15VPP2ma04FHcF249DECceWDLAGs5TuAId8+ UdBQ== 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=2Q2lne0lXLlvS3rxE7b9vuMB9y2K2LQZIZMl35a4JXk=; b=lQDCBy+PjdrljTFZAzA6BQHo9a5Q6QTG1rDZO5vRZSi+k55Q+nHzZOnfJijfdiTT5D kCE1xn3NZScVER4JPIN3qiyy2PvQy7gjdUpAPxU1s+VnfPtBQLNgaz0U7VMy/yTmgW82 83mcV3xU7wogV0zrqrali3cUdY7oTOcFcqAESdtyKWDFL+2NjyTdlmxMJHugdssYeuwC 2Oqsg1Mk8fBTd1O6k30SN+jhfRZgk14lb6O7iXfF9PFBE1qwsXa8oMv+Y/eV+TekA6TB ejv2BGizxY2+/3UU9WoB6NVShRPmxrkIFTnGI6saA+S9l/OQu4Zu9uN0RlR02KpBtC3S Eniw== X-Gm-Message-State: AOAM532At4aPr8egjSMq3EgsYl5+rc4uEGc0HWbblztyV+WzAwIqu/N+ elskQQ/WcVsCUwNBrdiGIMYEHbh2vJ2TJg== Sender: "jackmanb via sendgmr" X-Received: from beeg.c.googlers.com ([fda3:e722:ac3:10:28:9cb1:c0a8:11db]) (user=jackmanb job=sendgmr) by 2002:a7b:c8c8:: with SMTP id f8mr1117092wml.0.1607011397822; Thu, 03 Dec 2020 08:03:17 -0800 (PST) Date: Thu, 3 Dec 2020 16:02:35 +0000 In-Reply-To: <20201203160245.1014867-1-jackmanb@google.com> Message-Id: <20201203160245.1014867-5-jackmanb@google.com> Mime-Version: 1.0 References: <20201203160245.1014867-1-jackmanb@google.com> X-Mailer: git-send-email 2.29.2.454.gaff20da3a2-goog Subject: [PATCH bpf-next v3 04/14] 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. Change-Id: Ia6888f9fa65da6225c33b530ea16911bf2f70750 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 171ce539f6b9..ee7905051ee9 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_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; @@ -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