From nobody Sat Nov 15 06:53:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1755260886; cv=none; d=zohomail.com; s=zohoarc; b=Gxsqx6INkChYZRxQElR7c53KntnZFdgtsXa2yOnbA1sBUBhcyG0NMo5ARhit5Sk6MBjDKM26XIi3pkYTG5M4z/qBjuDhio6ATMor7NtDHX3dKsvtmA9oQkYQY6XZMxIQzK8yDkO3d1YkPeNqrZJZOJENh53vV1ppsUxyQuVXwBM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1755260886; h=Content-Transfer-Encoding:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To:Cc; bh=53dqTOaIfUhQRIFaZNYuW0Bo9AiUeDNRZ/usdqG/HaQ=; b=P9KgHryVl8MXlQNvXFRO2xDsF/+Wus/WK3Gxy4s23VOjXFOpkm+BS0JgZS5Q/fKuNt7zQpMSSi4JS7RfDrujL38kFirFoSK/DF/DVN9NE8r6SAxvwycfvkHcb0jRkRfBcyEahQv7m3mdbv3RjdoLKzDlWKoN1EUuuuL4SdU4xuM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 175526088686325.50270284570331; Fri, 15 Aug 2025 05:28:06 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1umtWc-000153-ER; Fri, 15 Aug 2025 08:27:14 -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 1umtWX-00013q-7D for qemu-devel@nongnu.org; Fri, 15 Aug 2025 08:27:09 -0400 Received: from mail-pf1-x42a.google.com ([2607:f8b0:4864:20::42a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1umtWV-0007XP-DL for qemu-devel@nongnu.org; Fri, 15 Aug 2025 08:27:08 -0400 Received: by mail-pf1-x42a.google.com with SMTP id d2e1a72fcca58-76e39ec6f30so1214958b3a.2 for ; Fri, 15 Aug 2025 05:27:06 -0700 (PDT) Received: from localhost.localdomain ([206.83.105.236]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-76e45292c86sm1053475b3a.48.2025.08.15.05.27.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Aug 2025 05:27:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1755260825; x=1755865625; darn=nongnu.org; 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=53dqTOaIfUhQRIFaZNYuW0Bo9AiUeDNRZ/usdqG/HaQ=; b=OjHMXFIRh2S+PJO4hoYxf9ylXKuECXgEiwtrvWJeCtzs8pK5A46F2dxtR1p6YSYFTM sqVdXXxzG0Lr+RorgtVMQUuZcUrSC1GDvWnUIMNUp0jrOTJ+OJNiWl4Doe3PtR6UPbn0 206V5cWuUMQFv2alumBxZWcTReY1uo6jTwgLl4Mdgecy/+IlNtl73myR6A90910tCfNR rY3Y7P9WrsIqanhx0Wl7Lm1ZTQUDcnLAUDMdpwNw3rv/q60LnQ8UiPaPrbx36pwyDPzA DMENRiUMvWbqILO8tG+jG+rIAArgVyhhjk3D4qEWa+FmjsNdi5bzOHBJir8KhxqTF7t0 Wrwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755260825; x=1755865625; 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=53dqTOaIfUhQRIFaZNYuW0Bo9AiUeDNRZ/usdqG/HaQ=; b=bDVRhjiCWRQpuxqFvHWx6Jty4/77HNFxQfSIOQp/yUu3dqg9xOJUCj1sPPx1P77WkG pdMMiSo7F0LdK4fytTQzLgAGhmJ+UWOND/eNxKpwd9/FNZ3pZLcKhxkpU/XWOnDIszRC r46rS9vCmWFMsN8YNoCtV6GQYEyfOlNunrZlo+7RdvocoUN1gtF+hMRclqUxc2i4EKds z56lLxSHDxN2/qnPCFl6sAXhgZQU/rXic/jotFXQMrAo6CDqIruHWeMIk0KuNQ3IdCmR yzV/JkdbFgCzd8AYr0MP8OfvLBXM6CRHtcqbUv78wrE1i7WfQ9Fdyl6UFofwRqP3+vNO hlsg== X-Gm-Message-State: AOJu0YwK68jrpfxVbI8OJZnLsj5IVwYkHLObDGanrXBgE4+nXDkUWWTX xv0JKYyHtfZXpyRjC8D7Ot69lI7kPKlM/jlpKuKfqaK1AVZ92hMLCRGbB1KFkZrAYnC1QEVCUcR V+vLpQ+U= X-Gm-Gg: ASbGncs8yc/2MGYmd6G4wiQQFs3QieFywFYU0xQkQpBhHBrCuh07P+g7h50RHrEmn5p fX0mSgAHL6rulTxJgOnj/tzWSebLFcfl2YODSiQDch9C03hJslEVYq7VPHPONB4FWcWaRgLvonk ki27EZyxPRFBnSjOngRPbR6+HSzDAch9bqpzSlRokXHnoZE/F050zw6oyX4jTof4hOQVP4FXHtp N+bdvkF0Mbl5MieC/8QdRgGL3x64jMiNPl4Q1PusLrosRIjvvprCmPNR5iejJ6SF49BHyBMi3TW +iNzoOexWVuSHFEZC+NTykTHMb8frH59Fd8RGDodDVDfgaYv0fml15IyzYdrWqpcawKVQ4eEWXD kzTskrN02+wr+A1tzOcymunvpzRsbg1xn9/QIH3fOCI0EzVM= X-Google-Smtp-Source: AGHT+IE68u2gdVFeLxcX8b7YLZODhCu+uuqY7fuOa0IA4VlKlEViMIWWhe2jBz9Gc4qXfcwp68T89w== X-Received: by 2002:a05:6a21:6d99:b0:23f:f729:2e60 with SMTP id adf61e73a8af0-240d2e9f1cemr3407384637.25.1755260824913; Fri, 15 Aug 2025 05:27:04 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Subject: [PATCH 2/7] qemu/atomic: Add atomic16 primitives for xchg, fetch_and, fetch_or Date: Fri, 15 Aug 2025 22:26:48 +1000 Message-ID: <20250815122653.701782-3-richard.henderson@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250815122653.701782-1-richard.henderson@linaro.org> References: <20250815122653.701782-1-richard.henderson@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2607:f8b0:4864:20::42a; envelope-from=richard.henderson@linaro.org; helo=mail-pf1-x42a.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+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1755260888881116600 Content-Type: text/plain; charset="utf-8" Signed-off-by: Richard Henderson Reviewed-by: Peter Maydell --- host/include/aarch64/host/atomic128-cas.h.inc | 57 +++++++++++ host/include/generic/host/atomic128-cas.h.inc | 96 +++++++++++++++++++ 2 files changed, 153 insertions(+) diff --git a/host/include/aarch64/host/atomic128-cas.h.inc b/host/include/a= arch64/host/atomic128-cas.h.inc index 991da4ef54..aec27df182 100644 --- a/host/include/aarch64/host/atomic128-cas.h.inc +++ b/host/include/aarch64/host/atomic128-cas.h.inc @@ -38,6 +38,63 @@ static inline Int128 atomic16_cmpxchg(Int128 *ptr, Int12= 8 cmp, Int128 new) return int128_make128(oldl, oldh); } =20 +static inline Int128 atomic16_xchg(Int128 *ptr, Int128 new) +{ + uint64_t newl =3D int128_getlo(new), newh =3D int128_gethi(new); + uint64_t oldl, oldh; + uint32_t tmp; + + asm("0: ldaxp %[oldl], %[oldh], %[mem]\n\t" + "stlxp %w[tmp], %[newl], %[newh], %[mem]\n\t" + "cbnz %w[tmp], 0b" + : [mem] "+m"(*ptr), [tmp] "=3D&r"(tmp), + [oldl] "=3D&r"(oldl), [oldh] "=3D&r"(oldh) + : [newl] "r"(newl), [newh] "r"(newh) + : "memory"); + + return int128_make128(oldl, oldh); +} + +static inline Int128 atomic16_fetch_and(Int128 *ptr, Int128 new) +{ + uint64_t newl =3D int128_getlo(new), newh =3D int128_gethi(new); + uint64_t oldl, oldh, tmpl, tmph; + uint32_t tmp; + + asm("0: ldaxp %[oldl], %[oldh], %[mem]\n\t" + "and %[tmpl], %[oldl], %[newl]\n\t" + "and %[tmph], %[oldh], %[newh]\n\t" + "stlxp %w[tmp], %[tmpl], %[tmph], %[mem]\n\t" + "cbnz %w[tmp], 0b" + : [mem] "+m"(*ptr), [tmp] "=3D&r"(tmp), + [oldl] "=3D&r"(oldl), [oldh] "=3D&r"(oldh) + : [newl] "r"(newl), [newh] "r"(newh), + [tmpl] "r"(tmpl), [tmph] "r"(tmph) + : "memory"); + + return int128_make128(oldl, oldh); +} + +static inline Int128 atomic16_fetch_or(Int128 *ptr, Int128 new) +{ + uint64_t newl =3D int128_getlo(new), newh =3D int128_gethi(new); + uint64_t oldl, oldh, tmpl, tmph; + uint32_t tmp; + + asm("0: ldaxp %[oldl], %[oldh], %[mem]\n\t" + "orr %[tmpl], %[oldl], %[newl]\n\t" + "orr %[tmph], %[oldh], %[newh]\n\t" + "stlxp %w[tmp], %[tmpl], %[tmph], %[mem]\n\t" + "cbnz %w[tmp], 0b" + : [mem] "+m"(*ptr), [tmp] "=3D&r"(tmp), + [oldl] "=3D&r"(oldl), [oldh] "=3D&r"(oldh) + : [newl] "r"(newl), [newh] "r"(newh), + [tmpl] "r"(tmpl), [tmph] "r"(tmph) + : "memory"); + + return int128_make128(oldl, oldh); +} + # define CONFIG_CMPXCHG128 1 # define HAVE_CMPXCHG128 1 #endif diff --git a/host/include/generic/host/atomic128-cas.h.inc b/host/include/g= eneric/host/atomic128-cas.h.inc index 6b40cc2271..990162c56f 100644 --- a/host/include/generic/host/atomic128-cas.h.inc +++ b/host/include/generic/host/atomic128-cas.h.inc @@ -23,6 +23,51 @@ atomic16_cmpxchg(Int128 *ptr, Int128 cmp, Int128 new) r.i =3D qatomic_cmpxchg__nocheck(ptr_align, c.i, n.i); return r.s; } + +/* + * Since we're looping anyway, use weak compare and swap. + * If the host supports weak, this will eliminate a second loop hidden + * within the atomic operation itself; otherwise the weak parameter is + * ignored. + */ +static inline Int128 ATTRIBUTE_ATOMIC128_OPT +atomic16_xchg(Int128 *ptr, Int128 new) +{ + __int128_t *ptr_align =3D __builtin_assume_aligned(ptr, 16); + Int128 old =3D *ptr_align; + + while (!__atomic_compare_exchange_n(ptr_align, &old, new, true, + __ATOMIC_SEQ_CST, 0)) { + continue; + } + return old; +} + +static inline Int128 ATTRIBUTE_ATOMIC128_OPT +atomic16_fetch_and(Int128 *ptr, Int128 val) +{ + __int128_t *ptr_align =3D __builtin_assume_aligned(ptr, 16); + Int128 old =3D *ptr_align; + + while (!__atomic_compare_exchange_n(ptr_align, &old, old & val, true, + __ATOMIC_SEQ_CST, 0)) { + continue; + } + return old; +} + +static inline Int128 ATTRIBUTE_ATOMIC128_OPT +atomic16_fetch_or(Int128 *ptr, Int128 val) +{ + __int128_t *ptr_align =3D __builtin_assume_aligned(ptr, 16); + Int128 old =3D *ptr_align; + + while (!__atomic_compare_exchange_n(ptr_align, &old, old | val, true, + __ATOMIC_SEQ_CST, 0)) { + continue; + } + return old; +} # define HAVE_CMPXCHG128 1 #elif defined(CONFIG_CMPXCHG128) static inline Int128 ATTRIBUTE_ATOMIC128_OPT @@ -36,6 +81,57 @@ atomic16_cmpxchg(Int128 *ptr, Int128 cmp, Int128 new) r.i =3D __sync_val_compare_and_swap_16(ptr_align, c.i, n.i); return r.s; } + +static inline Int128 ATTRIBUTE_ATOMIC128_OPT +atomic16_xchg(Int128 *ptr, Int128 new) +{ + Int128Aligned *ptr_align =3D __builtin_assume_aligned(ptr, 16); + Int128Alias o, n; + + n.s =3D new; + o.s =3D *ptr_align; + while (1) { + __int128 c =3D __sync_val_compare_and_swap_16(ptr_align, o.i, n.i); + if (c =3D=3D o.i) { + return o.s; + } + o.i =3D c; + } +} + +static inline Int128 ATTRIBUTE_ATOMIC128_OPT +atomic16_fetch_and(Int128 *ptr, Int128 val) +{ + Int128Aligned *ptr_align =3D __builtin_assume_aligned(ptr, 16); + Int128Alias o, v; + + v.s =3D val; + o.s =3D *ptr_align; + while (1) { + __int128 c =3D __sync_val_compare_and_swap_16(ptr_align, o.i, o.i = & v.i); + if (c =3D=3D o.i) { + return o.s; + } + o.i =3D c; + } +} + +static inline Int128 ATTRIBUTE_ATOMIC128_OPT +atomic16_fetch_or(Int128 *ptr, Int128 val) +{ + Int128Aligned *ptr_align =3D __builtin_assume_aligned(ptr, 16); + Int128Alias o, v; + + v.s =3D val; + o.s =3D *ptr_align; + while (1) { + __int128 c =3D __sync_val_compare_and_swap_16(ptr_align, o.i, o.i = | v.i); + if (c =3D=3D o.i) { + return o.s; + } + o.i =3D c; + } +} # define HAVE_CMPXCHG128 1 #else /* Fallback definition that must be optimized away, or error. */ --=20 2.43.0