Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp597596pxu; Thu, 3 Dec 2020 08:06:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJx7wPefMdCwnoDDNDV8m5yxOoknXxMBdogt6ia8JGWmbNojGPFBRJiS8xFTdOsEdd5qCJqX X-Received: by 2002:a05:6402:1d12:: with SMTP id dg18mr3553039edb.238.1607011595123; Thu, 03 Dec 2020 08:06:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607011595; cv=none; d=google.com; s=arc-20160816; b=vQSsn9RdJPv95lzmVCJ26w8FDRvrhw9OFijpr9RN4SXWcnQqKrkz6BahSinZJv0hZI 8TBdW208KKM0+JYFGKWD7IrPK6w50PyWVTBjeO+DD15VYiSTdVoLjonvrujFCXZntJRw RRgCm/q4he3/SAo9+HplPAOpGvhwYlJjIELy4+YKuE1VtSHsgFwtSG37OsQnMcS2W4kP pi0ljxrklrhSSbgGZwDFCdlG14HQxgsywuA6ATwdTZdZnn66Bhmmal5+6alSxnBgVkS7 CBAfTKA0WjUvG0m0f2Yu2hkMWg4TM+rz4Och5k9pJjetV7vyAfpjA4mCcCsthY7vM9x4 oXXQ== 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=0ZiV8JtnOj2y1hxnSkofHvyMEv1ZFrFbx4Eyr3aUv3w=; b=09AI+NO8YNaAiaORnvA3gEdZU5EvvteWiLAUijyOjILYQb7k7xf8k0kyQHUYNku2Ui fZkdWadPtwbUrg3iLMDb90jaiUEDbxnvKJD35WgFQX3Fmkv5T1vBDlhDke/+eZJjjRzI HtQiqaSOYLt31j50rVBBR5/qeU1m4qMDZznLcDgFhnAedwnVNW3b4ecU6LQDkEbi5jWP WHmWf9oe85lEsC2oT+evVbOk+acNxhYalUZ/wy5giYUJBWdL/784B4OKgHUzyjGVaXaY dPaHr/3SSHBRVYF3jdmowgdwgmZoRolafRbhEvDsb5UpEXVtfj8Z/BGHQEAaeQANDyoG CWTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=HVBol0Jz; 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 q4si1563463eji.89.2020.12.03.08.06.09; Thu, 03 Dec 2020 08:06:35 -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=HVBol0Jz; 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 S1731059AbgLCQEN (ORCPT + 99 others); Thu, 3 Dec 2020 11:04:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54760 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726415AbgLCQEM (ORCPT ); Thu, 3 Dec 2020 11:04:12 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 23A03C061A54 for ; Thu, 3 Dec 2020 08:03:14 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id 202so2337822qkl.9 for ; Thu, 03 Dec 2020 08:03:14 -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=0ZiV8JtnOj2y1hxnSkofHvyMEv1ZFrFbx4Eyr3aUv3w=; b=HVBol0JzcgJH7Ul5vN3bvsiqNzXpZyUkasuPalD/BEGlMr+FHMyaGuzmvPL+skOydT 25mz/ZMUxaDqB7YNd3tQRDJShKzUFtuX5wck/+0hczVpQZpbvw/CMHABSiRwb5fnn8Ld LHhGVPBWZus7QYmdIJwU+irg8dLVkVOUDfY4dyUSUVt9+rr5/Il4+zIsK8ARs8JotmUW WLBzgq8n2gghPMODS36wyRukW3Hy/f2SwiartK7KSx+JZS6EE49M/5a4YJxsKXo6adcw O7hu1p567RFpkzTUpOBFCP+LdEMqbhSJYGYa+jJ9FcBiHOv52Ro6blztV8vBS/fAoGqE fscQ== 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=0ZiV8JtnOj2y1hxnSkofHvyMEv1ZFrFbx4Eyr3aUv3w=; b=mQkHvb+Cr3NnnPZrQXzTYx5Z3kBKn/k++dBGsOyqyv9X83XdV3nJ0QIVaoSyX2j3NG swWx3IkdUFnlWKKMeEK8bwyNBMwG/Yynv2Xqm0AZBasNHNnsnVPEuQFFTbDTfuNewsi1 CBUZg+ECZToK+ryPuOsgK3x32/Av+qbS9qBRuBqD2JiAKCooSlIjdh/WaUmcMwvJcVlj bLAxmWk7W1ijUz04wVpC52oRfW3gUcJmruZaLdrZGnbUAuKqP0dNYkMEy/HJNJikErXI B9JFbwEzu63GRpsLiWdqlON0c88WozNGq/HEA2tDbNbnJ7d0YwxHu9shJzukLdohJS9h csxw== X-Gm-Message-State: AOAM5335ZDbt8GKV69ZL1JQntapzjJ+0fsxTgIfJLz0f7UaPgWXMY/SQ hI1yxmVsslc5BwnEKlY3r9oQE5ENKt1QgA== 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:ad4:5106:: with SMTP id g6mr3804556qvp.1.1607011393249; Thu, 03 Dec 2020 08:03:13 -0800 (PST) Date: Thu, 3 Dec 2020 16:02:33 +0000 In-Reply-To: <20201203160245.1014867-1-jackmanb@google.com> Message-Id: <20201203160245.1014867-3-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 02/14] 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 Change-Id: I66dbd5ad0bf6f820901fb73d6b2c6a63e00483b1 --- 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 cc818ed7c2b9..7106cfd10ba6 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -702,6 +702,21 @@ static void emit_insn_suffix(u8 **pprog, u32 ptr_reg, u32 val_reg, int off) *pprog = prog; } +/* + * Emit a REX byte if it will be necessary to address these registers + */ +static void maybe_emit_mod(u8 **pprog, u32 dst_reg, u32 src_reg, bool is64) +{ + u8 *prog = *pprog; + int cnt = 0; + + if (is64) + 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)); + *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_mod(&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_mod(&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_mod(&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_mod(&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