From patchwork Thu Jan 30 18:23:09 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 860886 Delivered-To: patch@linaro.org Received: by 2002:adf:fb05:0:b0:385:e875:8a9e with SMTP id c5csp329102wrr; Thu, 30 Jan 2025 10:26:55 -0800 (PST) X-Forwarded-Encrypted: i=2; AJvYcCU/N5sjALNnOe2FZus8FUYnCTgxAmFrhMlqTouvXhKHtKOcNbTgpCywxGNwl0eEIhDZRNQqIQ==@linaro.org X-Google-Smtp-Source: AGHT+IEDKNJYeqsz4a+YZIPvj+kf4vYULf92rgmIE6wVgEQ9MQH3p7tlFavyq6gtrFRpW4NOwlxc X-Received: by 2002:a05:620a:6084:b0:7b1:5763:4ba2 with SMTP id af79cd13be357-7bffcd927aemr1430519585a.40.1738261615311; Thu, 30 Jan 2025 10:26:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1738261615; cv=none; d=google.com; s=arc-20240605; b=hAo6iZ8MSmu6bcUonX0jffETWOg+yvky9fFybh7zLj1EXCEJzp0OKwnBOSytmUOB6m L1UhgLVpmwKbBFW5pCMNDCvn41kFBDXrBT1XZ5ol10hpl41LcACDr0lMPmK2XuPoIesO a6MiexBstwiBrM3e+tnPWGRLmtaQ6NddRVFxtIH9guLExS+noJ/GGbSAKg/kMKk52Kry s0B7Ydnh7iSAXbEBn1+Pp6Pd+2NxBjGd7+TvWL6ucm+5s3P81Q16Ki6WHj6IE7iJVapu QArCRRG4p3tCgee/vEhDHGiTYds+OU95Umb41jFi4T9JoKWWOyJu0hFl/LXzwHzpwuAC ervw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; 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=hmP8UEFFJb/lmaE2ZgdfHHn0pCkyFx+RjhYIX8J7P0Q=; fh=THYFgWh0gpAk/DFISrKT0zoA978C0O0tjyWcI5viY6E=; b=IGgq88MYw3FNkvURsJIv54GL9m15+XScUreB2bfRgeRROzbgsksuH4/HuHVHIBmH3N Z08Dd/IEAEEPfEKFj8RMEePbUXXA5JElu0hiO1gju+7dz9ij+Zk8abiU5A0QYngSKmb0 qbucfptk9pb1/RmGDZ+N5wKxC58P4SUINvioLYwnFgDfo2XV8+PKkJGz9kvGhElWfQ4b haZNUJS+BSs2qhXh5pCUkkIxgR/Ua7F4Zj9fq0cCwv85nT4Q/ZQgyxIhSR3EMZViJhqs 686hz2wCl6bGe+nwWXjZ/l+P8bkI3n1S0ADjfVZklzaHWH8aGfH3WF47lER0Z0VpSztY CGjQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=wjHx+KfK; 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; dara=neutral header.i=@linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id af79cd13be357-7c00a8bf557si210529285a.72.2025.01.30.10.26.55 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Thu, 30 Jan 2025 10:26:55 -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=wjHx+KfK; 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; dara=neutral header.i=@linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tdZCR-0003y0-Bl; Thu, 30 Jan 2025 13:23:35 -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 1tdZCL-0003u9-Ft for qemu-devel@nongnu.org; Thu, 30 Jan 2025 13:23:30 -0500 Received: from mail-wr1-x436.google.com ([2a00:1450:4864:20::436]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tdZCJ-0000Yc-Iw for qemu-devel@nongnu.org; Thu, 30 Jan 2025 13:23:29 -0500 Received: by mail-wr1-x436.google.com with SMTP id ffacd0b85a97d-385deda28b3so667132f8f.0 for ; Thu, 30 Jan 2025 10:23:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1738261406; x=1738866206; 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; bh=hmP8UEFFJb/lmaE2ZgdfHHn0pCkyFx+RjhYIX8J7P0Q=; b=wjHx+KfK1qan9SGD7R9+UubvVr431lMZ4Hud71zAbFwVyevdmabmeK+Azx0G3WfFrn +NP3vLgvdM6cqBQKSUcYrqOSpmAa2yse8C02z7OUxfwBSMQ4nt7pAYq+inbep81QPph+ eiT+93EPZuoxCwmX9d9oD7xuhXeo1junleMMdzAaxb3Cgk/FBJn+EiRMO6x1jYW0oHX/ OHv69DtYSQVn0Okak6p9x5JS4U+Mj4ToBVv2Lh3nHNz52kDx53qpW/0dZ9hkvYI/1Zh0 9OcA6U46dwx+J4IF8GCD9gStXAMBsL+jt2EII4Vazt7WEOBmnIIcHgrOUbSNc1+DIAmK kW7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738261406; x=1738866206; 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=hmP8UEFFJb/lmaE2ZgdfHHn0pCkyFx+RjhYIX8J7P0Q=; b=QJUheJuh+BLPq4D6yI+w4svkeIuhA7/Ta8vZA802b44CkeW754o+8OdTkEmtLopozW aHqpRAMWG6muzTLueoYmFLPjU3QM2rqQ41yjdx4g+78tfiK7EaJffuVYPo2BjsAFWj3h nyBzKKaOeFhJOkEQ2d7yex0W/8CTN6laOngATG3dKcwmG8P08w1JxT1TXPK31It1y3MD KSwbGxNHyIShGgglwM6bGRO8bxyf7k55M5vPNrjH5Bx7TyzxS7HPN1DXdtndDtkbB/rI UkTUtdcYFUAi2POHPPM2rrTAHlTbTQic+S2E9d0NnVC+FPEG4LBm4TdZ8KPDODi4c1hg JT2w== X-Forwarded-Encrypted: i=1; AJvYcCXJNxLIWW9d3I4EURBEho5qOEwjApgIbOdvcacAtSTTM5mHgb93FNr8VsXauM2y3pnw4rzMkT1INK3X@nongnu.org X-Gm-Message-State: AOJu0Yx+sAP7YE0RXcoyelOR/kiivSzugCEJv/obWColiDdWoMnhirh8 iQC3XMubBmxbGvWGT5afPdKAg2LLPaBliTHaNu14LujMbLxIhAzdGvaHqGgPtMWhZDh4ZeC9+BN L X-Gm-Gg: ASbGnctLk9wTwAH0D5C5fZXWpB9IQD9K2bOVIYB5SBAe7wlgPqJ4ZjVMLhrrV+MkG2l QprywMgKoCl8WbjUz6qW2bIFgpt3BHLDo0XvjGLZUo8qu0pszkMtMqSE8mMRSIwKygCyDpvhCZV zP8D5YDWKPckzW3j0lONgxa6k92o1/CSmz5j2g18nRLOoHIffl+UPz1YtJqxFUFww3Qo9dAWl+j wwCmeRbYfECQl3ZlpAAqXn9SJgP1uFTkb0YPGxq2PowAILUjMnukt0+pXUb0+ibdOrj9tq6UESG ma087jYMV8zDEi3dQIBFaw== X-Received: by 2002:a05:6000:dd2:b0:385:f0c9:4b66 with SMTP id ffacd0b85a97d-38c51969feemr6698796f8f.33.1738261405982; Thu, 30 Jan 2025 10:23:25 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38c5c101599sm2634426f8f.23.2025.01.30.10.23.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jan 2025 10:23:25 -0800 (PST) From: Peter Maydell To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: =?utf-8?q?Alex_Benn=C3=A9e?= Subject: [PATCH 14/14] target/arm: Correct errors in WFI/WFE trapping Date: Thu, 30 Jan 2025 18:23:09 +0000 Message-Id: <20250130182309.717346-15-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20250130182309.717346-1-peter.maydell@linaro.org> References: <20250130182309.717346-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::436; envelope-from=peter.maydell@linaro.org; helo=mail-wr1-x436.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 The code for WFI/WFE trapping has several errors: * it wasn't using arm_sctlr(), so it would look at SCTLR_EL1 even if the CPU was in the EL2&0 translation regime * it was raising UNDEF, not Monitor Trap, for traps to AArch32 EL3 because of SCR.{TWE,TWI} * it was not honouring SCR.{TWE,TWI} when running in AArch32 at EL3 not in Monitor mode * it checked SCR.{TWE,TWI} even on v7 CPUs which don't have those bits Fix these bugs. Cc: qemu-stable@nongnu.org Fixes: b1eced713d99 ("target-arm: Add WFx instruction trap support") Signed-off-by: Peter Maydell --- target/arm/tcg/op_helper.c | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/target/arm/tcg/op_helper.c b/target/arm/tcg/op_helper.c index 2230351a8f4..02c375d196d 100644 --- a/target/arm/tcg/op_helper.c +++ b/target/arm/tcg/op_helper.c @@ -313,15 +313,19 @@ void HELPER(check_bxj_trap)(CPUARMState *env, uint32_t rm) } #ifndef CONFIG_USER_ONLY -/* Function checks whether WFx (WFI/WFE) instructions are set up to be trapped. +/* + * Function checks whether WFx (WFI/WFE) instructions are set up to be trapped. * The function returns the target EL (1-3) if the instruction is to be trapped; * otherwise it returns 0 indicating it is not trapped. + * For a trap, *excp is updated with the EXCP_* trap type to use. */ -static inline int check_wfx_trap(CPUARMState *env, bool is_wfe) +static inline int check_wfx_trap(CPUARMState *env, bool is_wfe, uint32_t *excp) { int cur_el = arm_current_el(env); uint64_t mask; + *excp = EXCP_UDEF; + if (arm_feature(env, ARM_FEATURE_M)) { /* M profile cores can never trap WFI/WFE. */ return 0; @@ -331,18 +335,9 @@ static inline int check_wfx_trap(CPUARMState *env, bool is_wfe) * WFx instructions being trapped to EL1. These trap bits don't exist in v7. */ if (cur_el < 1 && arm_feature(env, ARM_FEATURE_V8)) { - int target_el; - mask = is_wfe ? SCTLR_nTWE : SCTLR_nTWI; - if (arm_is_secure_below_el3(env) && !arm_el_is_aa64(env, 3)) { - /* Secure EL0 and Secure PL1 is at EL3 */ - target_el = 3; - } else { - target_el = 1; - } - - if (!(env->cp15.sctlr_el[target_el] & mask)) { - return target_el; + if (!(arm_sctlr(env, cur_el) & mask)) { + return exception_target_el(env); } } @@ -358,9 +353,12 @@ static inline int check_wfx_trap(CPUARMState *env, bool is_wfe) } /* We are not trapping to EL1 or EL2; trap to EL3 if SCR_EL3 requires it */ - if (cur_el < 3) { + if (arm_feature(env, ARM_FEATURE_V8) && !arm_is_el3_or_mon(env)) { mask = (is_wfe) ? SCR_TWE : SCR_TWI; if (env->cp15.scr_el3 & mask) { + if (!arm_el_is_aa64(env, 3)) { + *excp = EXCP_MON_TRAP; + } return 3; } } @@ -383,7 +381,8 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len) return; #else CPUState *cs = env_cpu(env); - int target_el = check_wfx_trap(env, false); + uint32_t excp; + int target_el = check_wfx_trap(env, false, &excp); if (cpu_has_work(cs)) { /* Don't bother to go into our "low power state" if @@ -399,7 +398,7 @@ void HELPER(wfi)(CPUARMState *env, uint32_t insn_len) env->regs[15] -= insn_len; } - raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, insn_len == 2), + raise_exception(env, excp, syn_wfx(1, 0xe, 0, insn_len == 2), target_el); } @@ -424,7 +423,8 @@ void HELPER(wfit)(CPUARMState *env, uint64_t timeout) #else ARMCPU *cpu = env_archcpu(env); CPUState *cs = env_cpu(env); - int target_el = check_wfx_trap(env, false); + uint32_t excp; + int target_el = check_wfx_trap(env, false, &excp); /* The WFIT should time out when CNTVCT_EL0 >= the specified value. */ uint64_t cntval = gt_get_countervalue(env); uint64_t offset = gt_virt_cnt_offset(env); @@ -441,8 +441,7 @@ void HELPER(wfit)(CPUARMState *env, uint64_t timeout) if (target_el) { env->pc -= 4; - raise_exception(env, EXCP_UDEF, syn_wfx(1, 0xe, 0, false), - target_el); + raise_exception(env, excp, syn_wfx(1, 0xe, 0, false), target_el); } if (uadd64_overflow(timeout, offset, &nexttick)) {