From patchwork Tue Apr 25 19:31:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 676848 Delivered-To: patch@linaro.org Received: by 2002:a5d:4a41:0:0:0:0:0 with SMTP id v1csp2882025wrs; Tue, 25 Apr 2023 12:47:07 -0700 (PDT) X-Google-Smtp-Source: AKy350YxC+4RERHhdBbC9od0Z+tyCwsQ61vJZqgxxUuQ/T9f+Srw9GABFzzHxeRT3WrkTkrzqRFO X-Received: by 2002:a05:6214:1c43:b0:5e0:ad80:6846 with SMTP id if3-20020a0562141c4300b005e0ad806846mr36781588qvb.0.1682452027693; Tue, 25 Apr 2023 12:47:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682452027; cv=none; d=google.com; s=arc-20160816; b=ADRItsXQB12R6NXWb3w7VFM3WuSeUCSMhCEMZ+B5CRso3/e+u6fVSRiR49cdIsZiOH 7qDf8Qg97HhaxWATJjFjpSylW7JnMLFjQXF/jv1Fv/aIPEauLxXmSBZ8KW6XPd2Hyt0f TIK6RW3PbLOCHRrrDOTHQP5G7M4A9cb3NCrKk9MfmptfQpsHT3hIlKh070FDpKCgvPgQ oUdpDG54MbFaxpixRoXCzuO5OX6MKcpvFGUs5vtj0HvRyPuU2veqgUCMapLh+mzno4sK Na/obKHL2rKGwHzrlKQnzaqgfqa8p+5H8Q8wplR3yciecprGDFUpdgJYhcwJlr8ZLqw+ pffQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=i9vVtuysfBMlh/Al6RBdJn0LgfXo4dvluqytL552K7Y=; b=oOvjNi0ImztteQmbZqclzj0pOL3pvRxZFTIlsgXl/hbgBJSB98RycLarToEzUNWJ6Y 4ilTnjyzMoNjU7ghrwxzgBXSIUUs1ABlCEhma2/+Ki3BRLrA8oMY10Ga6QlA0hCfhChy w2vmUNr1N0MfDPp6CRHtBfo6xnQWtaHIznovwsg2KJjF5vZwiXNI4++YL+VisXZMTi2b ll7Sj5S8d5rClwONb02G9W33SIBzcKuNzLkWZYVPhie1SaLCGe0QljAOB/im7boNxJeN 8B0DUPp+ykPkQNAesk+bvZEQtr0OKinqbI3HwdGH1mkCeaJGJuHY5wAeQ9Gd5v2nwag7 e7yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="PCi/E1kZ"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id g9-20020a0562140ac900b005ef6bbe3d20si9594416qvi.207.2023.04.25.12.47.07 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 25 Apr 2023 12:47:07 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="PCi/E1kZ"; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1prOQh-0003WD-Ob; Tue, 25 Apr 2023 15:34:24 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1prOQd-0003TI-CP for qemu-devel@nongnu.org; Tue, 25 Apr 2023 15:34:19 -0400 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1prOQM-0004gm-Ad for qemu-devel@nongnu.org; Tue, 25 Apr 2023 15:34:18 -0400 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2a8a5f6771fso60410491fa.3 for ; Tue, 25 Apr 2023 12:34:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1682451240; x=1685043240; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=i9vVtuysfBMlh/Al6RBdJn0LgfXo4dvluqytL552K7Y=; b=PCi/E1kZuNO3EyT8b7f1NyxkbDbJFkR+o7G4elBMfWhEkoUsIdpCqxE8ojpbPG4MP2 hMlGYKkrtHEDRE+EQSSTNV23/NGMW9y7nizrsclrrrlY/PHegtGEojV/vIS/gMHJvspo x+bb2e4IWxF9pa+7d4P5LJRr3hhjvVXmHmqrPs+CGkwB5gpAo83vtbPnbJ8JpeRhCs/u kevUlkmKLRXVPrtXzScaAyF8DZVK/4uMlp624vJLxvCbptxUpsN5by47efV4JOy6mrnv hOJJ2mvIJzTE1KCt1BR6mg2vZk9U+neSITG/iKbawxjPma8N87QGD1T6UIzk7Xxiyy44 7/Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682451240; x=1685043240; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=i9vVtuysfBMlh/Al6RBdJn0LgfXo4dvluqytL552K7Y=; b=AdtpzeHrYsujS247POQj/PPuGoviZEMfj3NDZIty0SZL8W4avvFi2M5y9ti1BPt0vE IecmWjucRpD5FiUaHuiHHF9I9ET95g4PS/Dz0G1vn7dbQ5Jg4M02ooNfKm9Vgjzv77cv pfQPcdvn8RgQiTgffgcnhMVrPA/0tdPoz4vuE0I6mFeKPAnGnT3N4E5PGP2JIuC8ekgO mdffBLEZCXkLUYd69PMsMdrzUtfxd+LaYSgzEhjussDUYAXRROJpn/Ny3CB1GLJLpPmg xuDJw8HadoTq2C6GNWaNl3Jhwx0Alum9+8G0ARGrztYBYq+Hdzys1Y3Y1D+8sZUMCimQ sdLg== X-Gm-Message-State: AAQBX9dTyGEvMEoeraPdhoOPddw+gLh2euK032hIspx4lJZPSwAfQX2q QtykDArLQyEW33ROvcmBXbd8kV545f+vPc/Fgm2VQQ== X-Received: by 2002:a2e:8046:0:b0:2a9:f59a:a2ae with SMTP id p6-20020a2e8046000000b002a9f59aa2aemr3832658ljg.18.1682451240718; Tue, 25 Apr 2023 12:34:00 -0700 (PDT) Received: from stoup.. ([91.209.212.61]) by smtp.gmail.com with ESMTPSA id z23-20020a2e8857000000b002a8c271de33sm2160484ljj.67.2023.04.25.12.33.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Apr 2023 12:34:00 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, qemu-s390x@nongnu.org, qemu-riscv@nongnu.org, qemu-ppc@nongnu.org, git@xen0n.name, jiaxun.yang@flygoat.com, philmd@linaro.org Subject: [PATCH v3 16/57] accel/tcg: Add aarch64 specific support in ldst_atomicity Date: Tue, 25 Apr 2023 20:31:05 +0100 Message-Id: <20230425193146.2106111-17-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230425193146.2106111-1-richard.henderson@linaro.org> References: <20230425193146.2106111-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::232; envelope-from=richard.henderson@linaro.org; helo=mail-lj1-x232.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org We have code in atomic128.h noting that through GCC 8, there was no support for atomic operations on __uint128. This has been fixed in GCC 10. But we can still improve over any basic compare-and-swap loop using the ldxp/stxp instructions. Signed-off-by: Richard Henderson --- accel/tcg/ldst_atomicity.c.inc | 60 ++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/accel/tcg/ldst_atomicity.c.inc b/accel/tcg/ldst_atomicity.c.inc index 874fde6937..cf4a0e4a6e 100644 --- a/accel/tcg/ldst_atomicity.c.inc +++ b/accel/tcg/ldst_atomicity.c.inc @@ -247,7 +247,22 @@ static Int128 load_atomic16_or_exit(CPUArchState *env, uintptr_t ra, void *pv) * In system mode all guest pages are writable, and for user-only * we have just checked writability. Try cmpxchg. */ -#if defined(CONFIG_CMPXCHG128) +#if defined(__aarch64__) + /* We can do better than cmpxchg for AArch64. */ + { + uint64_t l, h; + uint32_t fail; + + /* The load must be paired with the store to guarantee not tearing. */ + asm("0: ldxp %0, %1, %3\n\t" + "stxp %w2, %0, %1, %3\n\t" + "cbnz %w2, 0b" + : "=&r"(l), "=&r"(h), "=&r"(fail) : "Q"(*p)); + + qemu_build_assert(!HOST_BIG_ENDIAN); + return int128_make128(l, h); + } +#elif defined(CONFIG_CMPXCHG128) /* Swap 0 with 0, with the side-effect of returning the old value. */ { Int128Alias r; @@ -743,7 +758,22 @@ store_atomic16(void *pv, Int128Alias val) return; } #endif -#if defined(CONFIG_CMPXCHG128) +#if defined(__aarch64__) + /* We can do better than cmpxchg for AArch64. */ + { + uint64_t l, h, t; + + qemu_build_assert(!HOST_BIG_ENDIAN); + l = int128_getlo(val.s); + h = int128_gethi(val.s); + + asm("0: ldxp %0, xzr, %1\n\t" + "stxp %w0, %2, %3, %1\n\t" + "cbnz %w0, 0b" + : "=&r"(t), "=Q"(*(__uint128_t *)pv) : "r"(l), "r"(h)); + return; + } +#elif defined(CONFIG_CMPXCHG128) { __uint128_t *pu = __builtin_assume_aligned(pv, 16); __uint128_t o; @@ -841,7 +871,31 @@ static void store_atom_insert_al8(uint64_t *p, uint64_t val, uint64_t msk) static void ATTRIBUTE_ATOMIC128_OPT store_atom_insert_al16(Int128 *ps, Int128Alias val, Int128Alias msk) { -#if defined(CONFIG_ATOMIC128) +#if defined(__aarch64__) + /* + * GCC only implements __sync* primitives for int128 on aarch64. + * We can do better without the barriers, and integrating the + * arithmetic into the load-exclusive/store-conditional pair. + */ + uint64_t tl, th, vl, vh, ml, mh; + uint32_t fail; + + qemu_build_assert(!HOST_BIG_ENDIAN); + vl = int128_getlo(val.s); + vh = int128_gethi(val.s); + ml = int128_getlo(msk.s); + mh = int128_gethi(msk.s); + + asm("0: ldxp %[l], %[h], %[mem]\n\t" + "bic %[l], %[l], %[ml]\n\t" + "bic %[h], %[h], %[mh]\n\t" + "orr %[l], %[l], %[vl]\n\t" + "orr %[h], %[h], %[vh]\n\t" + "stxp %w[f], %[l], %[h], %[mem]\n\t" + "cbnz %w[f], 0b\n" + : [mem] "+Q"(*ps), [f] "=&r"(fail), [l] "=&r"(tl), [h] "=&r"(th) + : [vl] "r"(vl), [vh] "r"(vh), [ml] "r"(ml), [mh] "r"(mh)); +#elif defined(CONFIG_ATOMIC128) __uint128_t *pu, old, new; /* With CONFIG_ATOMIC128, we can avoid the memory barriers. */