From nobody Sun Nov 29 02:09:52 2026 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795703632; cv=none; d=zohomail.com; s=zohoarc; b=Bs/CXwq3k3UhAr/uyYY1AhHeG39qYTOcHJjw8hXrmHeb1C8cBupeUcmbPZAwPiFhY2o/IzRA/957Z96yefNlRBfxdPzVW8H7wZxcUgjgXaxNYlTsU6xe84d37/460pLBkwIH/3lZ4ianUzwXxRA3DcGHxjtpTEo3RjvVs4xRNQo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795703632; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Cd5vkrrSWcpZ7F3e4D1cM7AMS3lJGeo3zC6MUWO6l04=; b=GRQKsh889ZOmUszfSrjjCiJjNV7R/+I3UkXwWnwUVXnJcxWzjiBBBakBhAeXVRrbWLtIBNyJrmQ+5qFtgfV5tmWirmteCd1JOCgOZqgrH/2jwbqRf065tql0xHxBaFgi2ctST5ihRpCCQeBu2BpAUiuYxH8kyGu/YiI+bgQQKBc= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1795703632809114.63430263697262; Thu, 26 Nov 2026 06:33:52 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWaXb-0003a0-Hi; Thu, 26 Nov 2026 09:33:39 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWaXQ-0003Dh-K3 for qemu-devel@nongnu.org; Thu, 26 Nov 2026 09:33:31 -0500 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xWaXI-0001Ak-QZ for qemu-devel@nongnu.org; Thu, 26 Nov 2026 09:33:28 -0500 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-4ac95761c85so55127155e9.2 for ; Thu, 26 Nov 2026 06:33:07 -0800 (PST) Received: from noys4.dd1.int.kernkonzept.com (ip-178-026-066-148.vkd85.pools.vodafone-ip.de. [178.26.66.148]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853b2437sm849925415e9.13.2026.11.26.06.33.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Nov 2026 06:33:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795703586; x=1796308386; darn=nongnu.org; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to:content-type; bh=Cd5vkrrSWcpZ7F3e4D1cM7AMS3lJGeo3zC6MUWO6l04=; b=h3OjWx/BD4YGu9+xuQ3YUTvAiWwEsPtnmxu9482gFYkBfAMVNZYFV2SZ+zfgvxIMjw fzM3CXLbmpUGB6WQ3nbw8QIZbU5jywx/kkA1u2clkVrRGM+xvNLoNmYRBLumBEMbue7y cQtmjc+bx0zNF6Dw825OWgs3CWHZpw6OBW0SRA9zH65l7p7ld2E/FU3UoLaUXMXWTdZo Ui5BDWuo9YibII99o5/lmcx90EEI637CUNY4APm1key/EYCC3avFgGyvySyJDNHAhFX+ JxbTF/SID7UABUbw37ZYfmcLZHUFpg2smjvS0d9+88gNbPTpLd5F+IZSryC0U6O903Ca EXfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795703586; x=1796308386; h=content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=Cd5vkrrSWcpZ7F3e4D1cM7AMS3lJGeo3zC6MUWO6l04=; b=Df1ieVDNT9LFDycHb6fh3UTh8Cbt0HHSeqbJQlqLH9JV+vnbImIqJA0qp5HBtlEL7p 4H+s7bMF2UYCqMlreUvqcqYE3bWqXKLjXln3EGrExfCRHdaWip2IXhJs59HqO6uKSJJL qjJVmN0nRs8u5vqT0/bkp0vx5o0zICxKWJxwYmOz9KjwD4y5Sn/Z6w6ajsRqYqyrkxjW yioXMRQeZ/dAT+SjTLw51wgps9Cvhgo1lyQYcAGqCuXWLENrakDzY07Yq0K0BWksD46W lcCr5ZhfFQvUXmQ2KXwe+Og3DmX1LoAhnGEHbYAiE6hfonTEXvfapkN+voZ+my7vtTrG uDvg== X-Gm-Message-State: APuU4Z8hrBwCi/e2NZtZ0TTmy4smGZs2amS58wNldNX7FHBpzM0N8BL0 XZVF/Py1WF8UUsLsfnoJR2d3C9AMG7Gux+MYLaFqhFsrzQWK7YhxMWGYZr/jKzs/ X-Gm-Gg: AZFdTZdueIQ8ajqXEZD+56W4yLrkKNQBsQ0EbWlOl3Rl5ooTrEBEkCCke7J1SQ4NZAV BZtuV9Ib6Oj+iX927HzXLLNRT9ubXtQGci8WjKsHG0JXeV6vaHsFSNRKs59mVvVKLA2hARv1v// eSQ9ICn5KHFKm6+3nb2CxEP4LA1vX+PZZmBILb9wCzIe2Ror2Sc8SLz8gWHuNKBy/3cA9gGxXRw lHpNfTonpKlE9ikLk1TBJQ4ybBPHB+1/5JQZZcNX7R7r6FLkTbEyhiuyhM5Nv2MqP/adi6BVJfp vnKBpvqCpivi2p/6ODTlC7OS+OZgAvjvmsGwhwmNGbqveCmFlL581WFDtvjS5Py3bKlIG3iUxFA IpLTYXNxTTront1i8JGBHHKvUWjkQKXR5qsOu9+V7LfY5PeTnBwB3bItIPXiXABEgA/ingptnug 2pJGS5vzuagl3Snb4oWwtSA4ZZ/5R05yL8mg/vjqoQNAHUqSBEWnT3bjioFRT9Llc8b7WX+5m7c J2Kn2slhBPPIzSPUGj91/9iVbNDoaGQbruESXiDxHGgQLtVrO80u1up4A8F2pr5oykUlzSKSQVH 4UHIEe6tHMrWzA2GovN4RLf6YyGfawmVIiroTx+b01MGIAsBQksJ1g6Rh94gQwLdRqEbytKwgTk 9KMjBjAPros+xHmWrHA2VesgoksTOwUbDHc8xYXRG X-Received: by 2002:a05:600c:1d0a:b0:4a2:59:f568 with SMTP id 5b1f17b1804b1-4bb8880ef6amr458687955e9.14.1795703585903; Thu, 26 Nov 2026 06:33:05 -0800 (PST) From: Frank Mehnert X-Google-Original-From: Frank Mehnert To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, peter.maydell@linaro.org, alex.bennee@linaro.org, Frank Mehnert Subject: [PATCH for-11.2 v2 1/2] target/arm: set the event register on a failed store-exclusive Date: Thu, 26 Nov 2026 15:32:57 +0100 Message-ID: <20261126143258.1056688-2-frank.mehnert@kernkonzept.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261126143258.1056688-1-frank.mehnert@kernkonzept.com> References: <20261126143258.1056688-1-frank.mehnert@kernkonzept.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32a; envelope-from=frank.mehnert@gmail.com; helo=mail-wm1-x32a.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, FREEMAIL_FROM=0.001, 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: qemu development 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 @gmail.com) X-ZM-MESSAGEID: 1795703634789158500 Since WFE may sleep on A-profile, a WFE executed outside an exclusive region halts the vCPU until an event arrives. Lock code commonly branches back to WFE after a failed store-exclusive, for example: 1: wfe ldaxr w1, [x0] cbnz w1, 1b stxr w2, w3, [x0] cbnz w2, 1b The store-exclusive clears exclusive_addr, so the following WFE sleeps. On hardware, the store by another PE that made the store-exclusive fail cleared the global monitor of this PE, and clearing the global monitor sets the Event Register (Arm ARM D1.6.1, R_XRZRK). The WFE then completes immediately. QEMU does not model this, so with interrupts masked and no event stream the vCPU sleeps until another PE executes SEV, which such lock code never does. Set the Event Register when a store-exclusive fails, in both the AArch64 and the AArch32 translator. A failure for another reason causes a spurious wakeup, which the architecture permits. Fixes: 01b223f895a7 ("target/arm: enable WFE sleeping for A-profile") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4938 Reviewed-by: Alex Benn=C3=A9e Signed-off-by: Frank Mehnert --- target/arm/tcg/translate-a64.c | 7 ++++++- target/arm/tcg/translate.c | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/target/arm/tcg/translate-a64.c b/target/arm/tcg/translate-a64.c index 49472f7d97..45f3b3df03 100644 --- a/target/arm/tcg/translate-a64.c +++ b/target/arm/tcg/translate-a64.c @@ -3531,10 +3531,15 @@ static void gen_store_exclusive(DisasContext *s, in= t rd, int rt, int rt2, tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val); } tcg_gen_mov_i64(cpu_reg(s, rd), tmp); - tcg_gen_br(done_label); + tcg_gen_brcondi_i64(TCG_COND_EQ, tmp, 0, done_label); =20 gen_set_label(fail_label); tcg_gen_movi_i64(cpu_reg(s, rd), 1); + /* + * A failing store-exclusive means the global monitor was cleared, whi= ch + * generates a WFE wakeup event. + */ + gen_event_reg(); gen_set_label(done_label); tcg_gen_movi_i64(cpu_exclusive_addr, -1); } diff --git a/target/arm/tcg/translate.c b/target/arm/tcg/translate.c index 442c34ce84..2eef2ceef2 100644 --- a/target/arm/tcg/translate.c +++ b/target/arm/tcg/translate.c @@ -2137,10 +2137,15 @@ static void gen_store_exclusive(DisasContext *s, in= t rd, int rt, int rt2, tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2); } tcg_gen_mov_i32(cpu_R[rd], t0); - tcg_gen_br(done_label); + tcg_gen_brcondi_i32(TCG_COND_EQ, t0, 0, done_label); =20 gen_set_label(fail_label); tcg_gen_movi_i32(cpu_R[rd], 1); + /* + * A failing store-exclusive means the global monitor was cleared, whi= ch + * generates a WFE wakeup event. + */ + gen_event_reg(); gen_set_label(done_label); tcg_gen_movi_i64(cpu_exclusive_addr, -1); } --=20 2.55.0 From nobody Sun Nov 29 02:09:52 2026 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=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1795703638; cv=none; d=zohomail.com; s=zohoarc; b=hmGuLAqUAvj+3ROYMlJIwC7tf3pPqSr84TqEhr5xgO81CUIZ4KKS6YNnk5qXyVrEIE6h76QwW2WVik/QHs6GnWDr0gcGgfk5SjAeKKubGEhztQyd1EnGv4pOj7AWT5gEnLkhs/5KGcvtt38iFN4ZvJwpMfwbZwiAs50fr7uP4Ss= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1795703638; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=hSzWdqovOS7zanPgxojJ0emViJPJpi0/B9Bvq88rSEI=; b=XPhfMr0zKWBXR5gmlQ3gG5wkOEEmJ/AdlBOeZ89ZCmkyHUhJZkyS5/aPGbDtHNrE3FJfQ+/G+ZqbEtrqDKNvrXb70B4YpR4HZOOGrBO1slxELiG+SKJTr1onzEflU1/QfxW7UoPhhd8bGxwi1Vq6lMNXcuyEKKkfxlRYpHongeo= 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 lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17957036383911012.4885288673491; Thu, 26 Nov 2026 06:33:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1xWaXh-0003mJ-Mu; Thu, 26 Nov 2026 09:33:46 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1xWaXQ-0003Dg-JR for qemu-devel@nongnu.org; Thu, 26 Nov 2026 09:33:31 -0500 Received: from mail-wm1-x32b.google.com ([2a00:1450:4864:20::32b]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1xWaXI-0001B2-QY for qemu-devel@nongnu.org; Thu, 26 Nov 2026 09:33:28 -0500 Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-4bb55cda775so44185205e9.0 for ; Thu, 26 Nov 2026 06:33:09 -0800 (PST) Received: from noys4.dd1.int.kernkonzept.com (ip-178-026-066-148.vkd85.pools.vodafone-ip.de. [178.26.66.148]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4bb853b2437sm849925415e9.13.2026.11.26.06.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Nov 2026 06:33:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795703588; x=1796308388; darn=nongnu.org; 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:content-type; bh=hSzWdqovOS7zanPgxojJ0emViJPJpi0/B9Bvq88rSEI=; b=I7YQz6x92Aks03iT1yg8eZLqg8KIctKrP7+SIsm6xd7U2NztmLchDO3f8BHsGZrUk7 g/vae0by+TqbZNTWmQRWxNdbx5DOwZpT8XnLu00R8ruj8bP9u4DFhTT7ne+7KC/7j3wm Go5EcNF/zcz5EIE2KR/QKrJEQqGZHtRvF+VGNhmwbcd8lv0f2FZNH14rpgIIsyNDwEhs 0liouKRi9iY5QN/1qB4RYuLxrGBy+k86lj6xd1OQwHyZ89ShXmYVmaKMUcAGJru5AGTj 6/kn+iclrI1BKVdoFyyvpytoccvmZcu5KAlm1+JULfY+X0u+1xUpaueHdB7FfeFw3vPF 08vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795703588; x=1796308388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=hSzWdqovOS7zanPgxojJ0emViJPJpi0/B9Bvq88rSEI=; b=C3bAqOLTgXFRVZNvPNq/v6Q8oF5kK0jV4GKhO5zXSKs6+CjHmqiUWLOV/Zbz2hZGNs YG2rWKOb3asBUuusoIbT/jGpEmW3tzjYQwiPomUN/YXXzGX63T6GTmNAzesaMGu7c5mm SiYk+myp+yb4FP1I/OJZ6NPEL83DQ65tdZIhvJCsKiqpIvtJGQ2F7nNB3pZ/SGwKDpv+ J/qxeF5ojDJ4s3I5UinZdY7HHPOkbVt5dxhnCt3EeVBDPI9nnG7MtkKjq9OeyOqJaVQM I7krwIE9/fv+73ne8g2jpk+2DqZNYmCibLv2tflVGGBMdtnDvjxEe09ZigS6ASLrpSvU If1Q== X-Gm-Message-State: APuU4Z+4u72vFYQmeBnPpz0KhuodqCE5xQmRrzOy5u/weWKXtONLaTGB YN4zjg+L6w0oLPr3ufAY/iLoyqHDfE0LLFIA755ev4SHH5mh+hPzQDNhRIJFAPGp X-Gm-Gg: AZFdTZfid6h1hAKLAKqsf1/gLwtJ5wpVvBU6YCPyi41+Urcyc+0u1LQUEfi/9U6Q6Rp SAUG8FI5n6Lx/aN7wPi/8QUpGX+XraG638xBrGFk54gReP3WDFPHbGQkeM9GPwAq3PrWYfelOYy 5AgTGbOxoih1GGhl6RbVwFlHczW4WoG//fdyAbj9Y52pjWBRn2XT3LE2+cAfR/20l8n2CwcPafW JAe/DQ3wdu9FM+tm4TWPq0jTN6lukGv6AtZTtmkbVSewKEKEIqYXHEIf/MYLiSlcipF932xuSIj dx19y9pxTrlSn1CjESeXRYm6At6Vk6v7rCkmv4Y1aThf9Swbsj3odZrj3iLi8DvbfwUv5ez32qg /EbT2Xa8FaSqUbMLr1NOCQdCxni6ArtPfTzd1UFndcISy/35NLTDMFXUcKcegta5pR8kWEcnE7a M6ssvPmj4F7ucWRXp27xlU9m8cCCOsnmRlHaSM5JTu/BoQQG8BN54oukiFicRC1auO8pQLNMPCp lyg8W2Iag71LddHNbeWiElFl820P5f0EphHTNXTvYqCZ9DjBh6lKMrtYAoZmH2lSwmYZojIj5Tw GyT6+HeuhAIpretur2cHVwUttF2Y6ghs676+HDrhkjpfGcsWyP8hRZhc9yEXikj16JAo+u5Y9iQ BiY/2fxKKpGWL078SPR3PPKKtLG9YGCUjwDEu5lP3KA== X-Received: by 2002:a05:600c:828e:b0:4a2:526b:d40f with SMTP id 5b1f17b1804b1-4bb88785898mr442947595e9.0.1795703587785; Thu, 26 Nov 2026 06:33:07 -0800 (PST) From: Frank Mehnert X-Google-Original-From: Frank Mehnert To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, peter.maydell@linaro.org, alex.bennee@linaro.org, Frank Mehnert Subject: [PATCH for-11.2 v2 2/2] tests/tcg/aarch64: test WFE after a failed store-exclusive Date: Thu, 26 Nov 2026 15:32:58 +0100 Message-ID: <20261126143258.1056688-3-frank.mehnert@kernkonzept.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261126143258.1056688-1-frank.mehnert@kernkonzept.com> References: <20261126143258.1056688-1-frank.mehnert@kernkonzept.com> 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=lists1p.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::32b; envelope-from=frank.mehnert@gmail.com; helo=mail-wm1-x32b.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, FREEMAIL_FROM=0.001, 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: qemu development 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 @gmail.com) X-ZM-MESSAGEID: 1795703640871158500 Content-Type: text/plain; charset="utf-8" Add a test case for the common spin lock pattern "WFE, load-exclusive, store-exclusive, on failure back to WFE", where the lock is released by a plain store and without SEV. CPU 0 executes LDAXR on the free lock, then CPU 1 takes the lock with a plain store. The following STXR of CPU 0 must fail, and the WFE after it must complete at once because the store of CPU 1 cleared the global monitor of CPU 0, which generates a WFE wakeup event. The virtual timer interrupt, 1s out, ends a WFE that misses the event, so the test fails instead of hanging. The test starts CPU 1 through PSCI and therefore needs two CPUs. Signed-off-by: Frank Mehnert Reviewed-by: Alex Benn=C3=A9e Tested-by: Alex Benn=C3=A9e --- tests/tcg/aarch64/system/meson.build | 2 +- tests/tcg/aarch64/system/wfxt.c | 103 ++++++++++++++++++++++++++- 2 files changed, 103 insertions(+), 2 deletions(-) diff --git a/tests/tcg/aarch64/system/meson.build b/tests/tcg/aarch64/syste= m/meson.build index 8710ebfdad..a3cc77c1a0 100644 --- a/tests/tcg/aarch64/system/meson.build +++ b/tests/tcg/aarch64/system/meson.build @@ -75,7 +75,7 @@ tests +=3D { 'wfxt.c': { 'cflags': cflags, 'qemu_args': ['-M', 'virt,virtualization=3Don,gic-version=3D2', - '-cpu', 'max', '-smp', '1', + '-cpu', 'max', '-smp', '2', '-semihosting-config', 'enable=3Don,arg=3D2', qemu_base_args] }, diff --git a/tests/tcg/aarch64/system/wfxt.c b/tests/tcg/aarch64/system/wfx= t.c index 9d50590a68..2cec6cfbbe 100644 --- a/tests/tcg/aarch64/system/wfxt.c +++ b/tests/tcg/aarch64/system/wfxt.c @@ -1,5 +1,5 @@ /* - * WFIT/WFET timeout test + * WFIT/WFET timeout test, WFE wakeup after a failed store-exclusive * * Copyright (c) 2026 Google LLC * Author: Fuad Tabba @@ -91,6 +91,106 @@ static int test_one(const char *name, void (*wait)(uint= 64_t), uint64_t freq) return 0; } =20 +/* + * At EL2 with virtualization enabled, QEMU's PSCI implementation uses the + * SMC conduit. + */ +#define PSCI_CPU_ON 0xc4000003UL + +static uint64_t psci_cpu_on(uint64_t mpidr, uint64_t entry) +{ + register uint64_t x0 asm("x0") =3D PSCI_CPU_ON; + register uint64_t x1 asm("x1") =3D mpidr; + register uint64_t x2 asm("x2") =3D entry; + register uint64_t x3 asm("x3") =3D 0; + + asm volatile("smc #0" : "+r" (x0) : "r" (x1), "r" (x2), "r" (x3) + : "memory"); + return x0; +} + +static uint32_t lock_word; +static uint32_t lock_step; + +asm(".pushsection .bss\n" + ".balign 16\n" + "secondary_stack:\n" + ".space 4096\n" + "secondary_stack_end:\n" + ".popsection\n" + ".global secondary_entry\n" + "secondary_entry:\n" + "adrp x0, secondary_stack_end\n" + "add x0, x0, :lo12:secondary_stack_end\n" + "mov sp, x0\n" + "b secondary_main\n"); + +/* CPU 1 takes the lock while CPU 0 is between LDAXR and STXR. */ +static void __attribute__((used)) secondary_main(void) +{ + while (__atomic_load_n(&lock_step, __ATOMIC_ACQUIRE) !=3D 1) { + } + __atomic_store_n(&lock_word, 1, __ATOMIC_RELAXED); + __atomic_store_n(&lock_step, 2, __ATOMIC_RELEASE); + + for (;;) { + asm volatile("wfi"); + } +} + +/* + * Spin lock pattern: "LDAXR, STXR, on failure WFE" with a lock released + * by a plain store and without SEV. The store-exclusive fails because + * another CPU took the lock. That store cleared the global monitor of + * this CPU, which generates a WFE wakeup event, so the following WFE + * must complete at once. + */ +static int test_wfe_lock(uint64_t freq) +{ + uint64_t entry, now, elapsed; + uint32_t val, status; + + asm("adrp %0, secondary_entry\n" + "add %0, %0, :lo12:secondary_entry" : "=3Dr" (entry)); + if (psci_cpu_on(1, entry) !=3D 0) { + ml_printf("FAIL: cannot start CPU 1\n"); + return 1; + } + + /* Clear the event register */ + asm volatile("sevl; wfe"); + + asm volatile("ldaxr %w0, [%1]" : "=3Dr" (val) : "r" (&lock_word) + : "memory"); + __atomic_store_n(&lock_step, 1, __ATOMIC_RELEASE); + while (__atomic_load_n(&lock_step, __ATOMIC_ACQUIRE) !=3D 2) { + } + asm volatile("stxr %w0, %w2, [%1]" : "=3D&r" (status) + : "r" (&lock_word), "r" (1) : "memory"); + + if (val !=3D 0 || status =3D=3D 0) { + ml_printf("FAIL: store-exclusive succeeded on a taken lock\n"); + return 1; + } + + /* The virtual timer interrupt, 1s out, wakes the CPU if WFE sleeps */ + now =3D read_sysreg(cntvct_el0); + write_sysreg(cntv_cval_el0, now + freq); + write_sysreg(cntv_ctl_el0, 1); + + asm volatile("wfe"); + + elapsed =3D read_sysreg(cntvct_el0) - now; + write_sysreg(cntv_ctl_el0, 0); + + ml_printf("wfe after failed stxr: woke after %ld ticks\n", elapsed); + if (elapsed > freq / 2) { + ml_printf("FAIL: wfe missed the event of the failed stxr\n"); + return 1; + } + return 0; +} + int main(void) { uint64_t freq; @@ -113,6 +213,7 @@ int main(void) =20 ret =3D test_one("wfit", wfit, freq); ret |=3D test_one("wfet", wfet, freq); + ret |=3D test_wfe_lock(freq); =20 return ret; } --=20 2.55.0