From patchwork Thu Feb 16 02:57:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 653961 Delivered-To: patch@linaro.org Received: by 2002:adf:f90f:0:0:0:0:0 with SMTP id b15csp118528wrr; Wed, 15 Feb 2023 18:59:17 -0800 (PST) X-Google-Smtp-Source: AK7set/DA0UVZHN37J78dc5YpGZUSmtgZQ5yoDCu8bwiwAqcV2IxZ6vWVaym/UDVkHtTuKx8OxrO X-Received: by 2002:a05:6214:d87:b0:56e:a96a:2be1 with SMTP id e7-20020a0562140d8700b0056ea96a2be1mr7407962qve.48.1676516357251; Wed, 15 Feb 2023 18:59:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676516357; cv=none; d=google.com; s=arc-20160816; b=m2xlQ0AZmWuh6+IbTeVeb1ochrJCMMwC9yxSMGFjcr+rRAar0EJlqsEnGnRCjPbd4G Emx6FL/sl/+F6tkrkJ5JOEEu1vJsj4Y0nEtanDH804gADaU33G2tB5oTw6WVknGw6Jni qARYV/A2OfsXY8lXIiX5/XoVyrqXc57c8DrxugDLPzVyAK8wNo2Ex3MvsplBRXH+NlTf xceqyQ24TGhUUYjz7fHQ/HGx9bTGUoyLNFvmr0S/OHE1RI3oQjx3NCJM6rKOUkVW78Ub 6jOZUUd4DxuXBuO3a0L3TT487YxEJAmD3nJmv9UkoTEA00H3sjSUZzNFyotaRhi2OzfH 63/g== 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:to:from :dkim-signature; bh=eD5cSh7HSVjqFOmUj2rwI/Hu7f1XIWpv7Fy8Vo5nqrw=; b=DER3lN+zddkPkyytfD3Cx7n1QpHV0V9zBZ60xycC4/LiDvJ0dfJkLtbH6IfXNLb+1B xICRBuLJX2yo6IbZUOc6tJF1h39iBgnurTi7/yD0kY9skhpQiAgIc0Qz6SV9zEVgUcr4 92182Fh4EXoZPCEuida0MfUpBfben8kF0M/WwQKwu7ZGxl54kdONv86bdDIepBPuNTVH 0mh71UqqhPhF3qgQvtmSAs0cezc7n1bT7HUUrL1PlqE0FOl3JCbMrctA4FWRIUpRWgHr 5WloXdpI1kYz44Xg5XJEDUnquMUarU7Sp9aWJ2auUY2ocgiO9HQnHWqyQItTvgWTvi+Q QONw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="oYtqP8/5"; 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 p3-20020a0ccb83000000b0056e99162fa1si97631qvk.313.2023.02.15.18.59.17 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Wed, 15 Feb 2023 18:59:17 -0800 (PST) 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="oYtqP8/5"; 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 1pSUTJ-0003Vp-Ed; Wed, 15 Feb 2023 21:58:09 -0500 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 1pSUTH-0003Uo-4H for qemu-devel@nongnu.org; Wed, 15 Feb 2023 21:58:07 -0500 Received: from mail-pl1-x62d.google.com ([2607:f8b0:4864:20::62d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pSUTF-0005lJ-7F for qemu-devel@nongnu.org; Wed, 15 Feb 2023 21:58:06 -0500 Received: by mail-pl1-x62d.google.com with SMTP id h4so675404pll.9 for ; Wed, 15 Feb 2023 18:58:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=eD5cSh7HSVjqFOmUj2rwI/Hu7f1XIWpv7Fy8Vo5nqrw=; b=oYtqP8/53Zwe6DqXgrW1XU2bfkwhCD4w5AxIN6niIhig74b7HA0pGRWHgcNxCCLsZQ dAS2Grx5gK1sdBfV16ap3Qiw3Wjp98ealMziVaiDIeJ12yTdbMCDJQTyxuDN+7xC91eY xsviaNpiT/O0RphwR8xH+fbBWgUfFIQb1LQNvODBprDEEOGreBHtjqjZ/HGtkI8du/pZ 7CjaZKTUxaoTkh/IKwDkCzwv8Ox2Mrp8SNh9KWue0hTyFNK6A4qgFTb57zTo6RlQ4Cr2 aqkcd8vLV95ZA4L4gHn+YGic9pK8NMrxhFZCaIDv3WZrCnZVFBcNuYmztVEZ+vYP/fVN /PHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eD5cSh7HSVjqFOmUj2rwI/Hu7f1XIWpv7Fy8Vo5nqrw=; b=myCFDak6xQy8s+XZY4OOb5OhR7vfUFVkPSKv2GreR3xqBXOhUVptYKjtAfHXqoKleA ZaKy/SbyTFw8qEX0uWmgv6gMdlEm2Glfe7wWr6c2Q+mXMWVoRbxRtgQJ14i+j6tuv8Kj BTvpS3NjYiNaVD+ENaGsKIkYOYsBONR4Ztd9ga6AB0SJcXR4dAaMBaYD5Etqa8DM4GQK I0jufx+nY3a7tMbmbVx+ZXBK5glfhWT9x6IcYfy4dpGBVX0L/BERhra+wfpavWy9hnrY k4Ah6FbKG8bfT9OF+spFsj+0hUnrAMujZjk3MpNAnrevABNctPRlitlJRjqf4znq1cki xI1Q== X-Gm-Message-State: AO0yUKVP21adMfCUQmb1pMHVBDlLZKmdo8b/+BwZ3ikkZBvTz5MK+8hV +x/ZvYqujFbPol+2LGPuU9d7bhe1r56UsUdwKJo= X-Received: by 2002:a05:6a20:69a8:b0:c7:13be:6df0 with SMTP id t40-20020a056a2069a800b000c713be6df0mr1162019pzk.11.1676516283854; Wed, 15 Feb 2023 18:58:03 -0800 (PST) Received: from stoup.. (rrcs-74-87-59-234.west.biz.rr.com. [74.87.59.234]) by smtp.gmail.com with ESMTPSA id e14-20020a62aa0e000000b005a816b7c3e8sm89655pff.24.2023.02.15.18.58.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Feb 2023 18:58:03 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH v2 16/30] accel/tcg: Add aarch64 specific support in ldst_atomicity Date: Wed, 15 Feb 2023 16:57:25 -1000 Message-Id: <20230216025739.1211680-17-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230216025739.1211680-1-richard.henderson@linaro.org> References: <20230216025739.1211680-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::62d; envelope-from=richard.henderson@linaro.org; helo=mail-pl1-x62d.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 autolearn=ham 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 07982e021d..9a95ac327d 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; @@ -740,7 +755,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; @@ -838,7 +868,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. */