Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp1567301pxu; Fri, 27 Nov 2020 09:59:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJyf5pzYvUiCCg8GwkfVCTKI7xYyVgzAWhQQia96JNVAcMnkuwqk/ejr4oB5AjcpZoN/VaIx X-Received: by 2002:a17:907:a96:: with SMTP id by22mr1287193ejc.171.1606499988090; Fri, 27 Nov 2020 09:59:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606499988; cv=none; d=google.com; s=arc-20160816; b=nN6JHgH7ah/K/GkLbQgW0urGibju92zth+LvyRrZVwKlXExpVvI3xhyHMvri898mJo H4qn/an13yAmcFbL5AKbNDEAlV72OKdiKLJF2Xr6zyDYpmz1pogAajm/GfuiZzWilP7d qbyIxL5dD/OTbzVo7Q92UzGw23kJ2hSA+ZorRS5qn5mPqVgGs52b2201f7AOE0lxfKT7 d4eAP3Rg+JOCLFRWrzN2aLA/hogDNREeM3PUUuE/sygsqfKQnHF4QubVsOiB//7K2+wI icIIu6jKXchkQE1dfoGvJupkPnGdndaY14hwXdAyk6aU1DMf/mNTST1HLNIzgsXxU+E8 FAgA== 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=S3LKp+3RvK8OZkmGe150smJLkvcUKra31qzmBdRpcwc=; b=B4X37nJa05CaKF0r1Y4pm96xXowTPvGGUyE00IsPLhNy9/nxIyvnOFYux98s/NSIte 27vaQPc2pUJXJoI2Kg1hC31Cdy4adkhhNf/elvGhAfmkhgxSG4pBtwoHhtFGf0RUYPoy MUjMppZy22mHla6QcGJyIGWVuq82xE4cPlL4tBPSxdqMu1/xCzfxs4Z5nj90yBjlz3Ha 4OMzRT49nsiJ8LNN92DjhK0iqDCUPBW6HMRx9tkOMTwppNfblfv0HE8VnI93/qdzQDmI fljjxYouwpM6leinsWjsqsi75NcFC55JhbGRKf5HmGVyjL8mbkfk/qTwYiai777yPQce Rs3w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="HjJkNd/F"; 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 du15si6143872ejc.471.2020.11.27.09.59.25; Fri, 27 Nov 2020 09:59: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="HjJkNd/F"; 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 S1732755AbgK0R5x (ORCPT + 99 others); Fri, 27 Nov 2020 12:57:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37724 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730985AbgK0R5x (ORCPT ); Fri, 27 Nov 2020 12:57:53 -0500 Received: from mail-qk1-x74a.google.com (mail-qk1-x74a.google.com [IPv6:2607:f8b0:4864:20::74a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1A6FC0613D1 for ; Fri, 27 Nov 2020 09:57:52 -0800 (PST) Received: by mail-qk1-x74a.google.com with SMTP id b11so4133587qkk.10 for ; Fri, 27 Nov 2020 09:57:52 -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=S3LKp+3RvK8OZkmGe150smJLkvcUKra31qzmBdRpcwc=; b=HjJkNd/Fcb7hlI25N2/0RZaPOeJLmob9416QnTRAdWKyICh8nlL4N42gT7uYToO16V USLz0UptmPjQlEwvmgho10Ctx+6t/NUOYRkjpaHSs1tXiHAuEyx0ZT9LV8XtNL2aya/Y OPqXGnne4x7klTAiwv2z72/uksfWLkcx0t7tCr0/7WB8rUa9Xd16zdIYNBEhyk5oRYGB M2YonpjfV6Ujm7xik6fhgGm9CIFXiDZNT8bXQvkfBmtRtrpi3/w3rUL+iCDzq+M1sSu9 4uzjdd34FZh4N8WzgTiXMirD6XpfZHgE98Xvtwh1wqemySCXDfdx4ar6Lm+Pr4qNrKES xmIw== 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=S3LKp+3RvK8OZkmGe150smJLkvcUKra31qzmBdRpcwc=; b=Y3HkugRfmKcJK3aIQxTgfJ+7/7H6Tm65VOILC4O0GJ/P0ULo8UXvBcAk5gVukV80gg XjBHyOug76gNMlBoao8pK5ZTAjD1QQ3XQCkj6TGUZkaKBZPYReqahckbWs7AA08BLQPh yslwVMvxJJWesvVsNwgLiiq30bGsuz8OZ7nw09VIkVo1/drmTy08siqx/Q/yGuxAXNcl TG0LmAgAigi7g68hFwDpynECO5PGiObCEA7x9xVaQrU+jGXS0pLD3Y40OU+hz+iQs8sU u8+TCsWVWWJ6uXCm2V2fbhzKTEbAOn000Qa4Ky0OOnqWwo3/7+Ot0RE2TF+k0gbkPZ/w ettg== X-Gm-Message-State: AOAM532be6+O/nUACtd4EmXEmwAXPHvJibxVGBU5+fieOFeLlY5KbVGK 3rSj0H4AJpQU4l8wvIJfCUOlO9Bwd12REA== 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:6214:2b4:: with SMTP id m20mr9656499qvv.34.1606499871986; Fri, 27 Nov 2020 09:57:51 -0800 (PST) Date: Fri, 27 Nov 2020 17:57:27 +0000 In-Reply-To: <20201127175738.1085417-1-jackmanb@google.com> Message-Id: <20201127175738.1085417-3-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 02/13] bpf: x86: Factor out emission of REX byte 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 The JIT case for encoding atomic ops is about to get more complicated. In order to make the review & resulting code easier, let's factor out some shared helpers. Signed-off-by: Brendan Jackman --- arch/x86/net/bpf_jit_comp.c | 39 ++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 94b17bd30e00..a839c1a54276 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -702,6 +702,21 @@ static void emit_modrm_dstoff(u8 **pprog, u32 r1, u32 r2, int off) *pprog = prog; } +/* + * Emit a REX byte if it will be necessary to address these registers + */ +static void maybe_emit_rex(u8 **pprog, u32 reg_rm, u32 reg_reg, bool wide) +{ + u8 *prog = *pprog; + int cnt = 0; + + if (wide) + EMIT1(add_2mod(0x48, reg_rm, reg_reg)); + else if (is_ereg(reg_rm) || is_ereg(reg_reg)) + EMIT1(add_2mod(0x40, reg_rm, reg_reg)); + *pprog = prog; +} + /* LDX: dst_reg = *(u8*)(src_reg + off) */ static void emit_ldx(u8 **pprog, u32 size, u32 dst_reg, u32 src_reg, int off) { @@ -854,10 +869,8 @@ static int do_jit(struct bpf_prog *bpf_prog, int *addrs, u8 *image, case BPF_OR: b2 = 0x09; break; case BPF_XOR: b2 = 0x31; break; } - if (BPF_CLASS(insn->code) == BPF_ALU64) - EMIT1(add_2mod(0x48, dst_reg, src_reg)); - else if (is_ereg(dst_reg) || is_ereg(src_reg)) - EMIT1(add_2mod(0x40, dst_reg, src_reg)); + maybe_emit_rex(&prog, dst_reg, src_reg, + BPF_CLASS(insn->code) == BPF_ALU64); EMIT2(b2, add_2reg(0xC0, dst_reg, src_reg)); break; @@ -1301,20 +1314,16 @@ xadd: emit_modrm_dstoff(&prog, dst_reg, src_reg, insn->off); case BPF_JMP32 | BPF_JSGE | BPF_X: case BPF_JMP32 | BPF_JSLE | BPF_X: /* cmp dst_reg, src_reg */ - if (BPF_CLASS(insn->code) == BPF_JMP) - EMIT1(add_2mod(0x48, dst_reg, src_reg)); - else if (is_ereg(dst_reg) || is_ereg(src_reg)) - EMIT1(add_2mod(0x40, dst_reg, src_reg)); + maybe_emit_rex(&prog, dst_reg, src_reg, + BPF_CLASS(insn->code) == BPF_JMP); EMIT2(0x39, add_2reg(0xC0, dst_reg, src_reg)); goto emit_cond_jmp; case BPF_JMP | BPF_JSET | BPF_X: case BPF_JMP32 | BPF_JSET | BPF_X: /* test dst_reg, src_reg */ - if (BPF_CLASS(insn->code) == BPF_JMP) - EMIT1(add_2mod(0x48, dst_reg, src_reg)); - else if (is_ereg(dst_reg) || is_ereg(src_reg)) - EMIT1(add_2mod(0x40, dst_reg, src_reg)); + maybe_emit_rex(&prog, dst_reg, src_reg, + BPF_CLASS(insn->code) == BPF_JMP); EMIT2(0x85, add_2reg(0xC0, dst_reg, src_reg)); goto emit_cond_jmp; @@ -1350,10 +1359,8 @@ xadd: emit_modrm_dstoff(&prog, dst_reg, src_reg, insn->off); case BPF_JMP32 | BPF_JSLE | BPF_K: /* test dst_reg, dst_reg to save one extra byte */ if (imm32 == 0) { - if (BPF_CLASS(insn->code) == BPF_JMP) - EMIT1(add_2mod(0x48, dst_reg, dst_reg)); - else if (is_ereg(dst_reg)) - EMIT1(add_2mod(0x40, dst_reg, dst_reg)); + maybe_emit_rex(&prog, dst_reg, dst_reg, + BPF_CLASS(insn->code) == BPF_JMP); EMIT2(0x85, add_2reg(0xC0, dst_reg, dst_reg)); goto emit_cond_jmp; } -- 2.29.2.454.gaff20da3a2-goog