From patchwork Tue Jun 20 12:44:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 694498 Delivered-To: patch@linaro.org Received: by 2002:adf:e885:0:0:0:0:0 with SMTP id d5csp285556wrm; Tue, 20 Jun 2023 05:45:38 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5/rQjDi0TJil5sYTYXcafmFBsZA6QjwLWCUaTNVM8X9x3LC5DkvQcIk/Rc9VHDq4vUWaMf X-Received: by 2002:a1f:5e48:0:b0:473:4e4d:8a8d with SMTP id s69-20020a1f5e48000000b004734e4d8a8dmr360332vkb.11.1687265137709; Tue, 20 Jun 2023 05:45:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1687265137; cv=none; d=google.com; s=arc-20160816; b=WVEMLsx1u5MYEfYGlAFIbsCvH71cb95Fd8oyQAjE5gfn/QJ8zEbtxAuGuoeMOgATa1 kyDtYjqrvxG07afrPYiiDRvvqBRXc5Sok1SdHOJCEW9bF8W31GngvaNt1IUocSQPXtCH AoIRYxQETWqm9gFeCHtDHaN9FasjRboT8qPwH0z3kJCKlVlnPsPkx8sUVi1yQ8/5BqcU U5V4dCFF6SD2ALLuYAf6B61vOqoqLkcKbkMHK/2dgUSJhHRuVyO68/vV6Hj1DflEKYgL +stxyWYKK0e526+RzUYdZ9rm1oK3R0z8EOdQCCRQ1+6n6Typ3zEwh2447UGFaU/iREZs aJkg== 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=2Yo25OAxUD/lm8WzBJYAD5QUNlo/s/nznK8oA1gE1bM=; b=h/ube+tnJqodCKNZNQG6UmZCIY4T3Z0/m1bV+kfj8zPeqgWPQGcebSVmtVtjSl7Hw8 wi4D/1DmpM5qg8lvC3Tbz3D82SLnRhodItREcQau9+CuLx/PugopPO85sDglimGhgvB1 z0R8gWwWdYvJusaL3t895vpn62lyLBqNUg4vvF5Buq1Ry4SNQzwBKNplC9NbouQ2T6MF ovJXuAs36/SJM9/pA/W5cTQo9bohz8/9WAVD+knPLBu44aldhOTQ234yfJcJh8Up++j6 gauyjUMaxqJQrIwtOAdvBQo0Ql+GFHm1jZsYe88J3Vxrtqe/tuYiMOXjJKP2qM1I7WK6 hZ/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Cf+YaOJb; 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 pi8-20020a05620a378800b0075cd3df8118si764202qkn.603.2023.06.20.05.45.37 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 20 Jun 2023 05:45:37 -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=Cf+YaOJb; 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 1qBaim-0001UL-S1; Tue, 20 Jun 2023 08:44:32 -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 1qBail-0001SO-Fp for qemu-devel@nongnu.org; Tue, 20 Jun 2023 08:44:31 -0400 Received: from mail-ed1-x52d.google.com ([2a00:1450:4864:20::52d]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qBaii-0001t1-Cm for qemu-devel@nongnu.org; Tue, 20 Jun 2023 08:44:31 -0400 Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-51a4d215e09so2908235a12.3 for ; Tue, 20 Jun 2023 05:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1687265065; x=1689857065; 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=2Yo25OAxUD/lm8WzBJYAD5QUNlo/s/nznK8oA1gE1bM=; b=Cf+YaOJbUJ5t7rUAZgg3tMFR839BlT8L+24WV00smEOPAxuDB7BtSjcyImn7/gg5dZ RiC1WYIVZCjXeeq3uPEd6/ubZ8LTDAJYsa3cKGBFCPxXM3WiVGKbIkPZyVn6aWhlBqaP nZ2+Prb1Zp/Ffjdb9m+J0X1dh+iWrE186iOXhH2k3au/8Akay1+yZclw339JMxmJJWz3 bkHWPJYjn5raRzydyxARBFE/iGJ48KupffX9smGdmbrhcku5jnQrib8+HYOaPibMHLUx Ct7FWH4le2uev+PZuu3SGZWjhPP5CJ9kH/PedbeIOctCZ+DByyshO+NVZwQMUgxs3XPT fWBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687265065; x=1689857065; 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=2Yo25OAxUD/lm8WzBJYAD5QUNlo/s/nznK8oA1gE1bM=; b=VSEocrjZGII1XOJ+SfB2p+Q3Rj6SKoiH0Jux+R2a6KConEkSn1sJmby9MNRlvPcYQc /ZvmaUDzaCdkYH9QEcaBu16T/zFjNR+G/RR7Nu/TmG+j1Jz8TZvVb9IPNiqY5Cq1Kowt BGnOoVlDTX8BQChWj+ZQEzFgUuznjnAhgohsgN4BZdfMidnue4cPoCOZKwboI9AYpKZc Y5+ae2XjTXTCbtYM13tc9C36S0n2MaYB9cLgXKdeeEUrIirF6nodxbO3g5sODmBjR2Yr rCNzlWtk4z2Omi3R4/rlXl0h7WYFJQ03hyjQ44RSxbHCDAAOzmiHm6L9TxoU4/V9MRFK CVmA== X-Gm-Message-State: AC+VfDzrMluTzYyN7AqZF7r1tbt1fgGvvAQlgpTrRXltOLAIb1/o+7/v L+SaNAeRGzAZ4L4TbFP/mUikAWxfR0mR3F7Bm8Al4Bc7 X-Received: by 2002:a05:6402:3449:b0:518:6c09:847f with SMTP id l9-20020a056402344900b005186c09847fmr7677687edc.6.1687265065215; Tue, 20 Jun 2023 05:44:25 -0700 (PDT) Received: from stoup.lan ([176.176.183.29]) by smtp.gmail.com with ESMTPSA id w9-20020aa7dcc9000000b0051a313a66e8sm1142541edu.45.2023.06.20.05.44.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 20 Jun 2023 05:44:25 -0700 (PDT) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Peter Maydell Subject: [PATCH v5 05/20] target/arm: Introduce ARMSecuritySpace Date: Tue, 20 Jun 2023 14:44:03 +0200 Message-Id: <20230620124418.805717-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230620124418.805717-1-richard.henderson@linaro.org> References: <20230620124418.805717-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::52d; envelope-from=richard.henderson@linaro.org; helo=mail-ed1-x52d.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 Introduce both the enumeration and functions to retrieve the current state, and state outside of EL3. Reviewed-by: Peter Maydell Signed-off-by: Richard Henderson --- target/arm/cpu.h | 89 ++++++++++++++++++++++++++++++++++----------- target/arm/helper.c | 60 ++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+), 22 deletions(-) diff --git a/target/arm/cpu.h b/target/arm/cpu.h index c9c87b515d..125e53b83f 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -2414,25 +2414,53 @@ static inline int arm_feature(CPUARMState *env, int feature) void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp); -#if !defined(CONFIG_USER_ONLY) /* + * ARM v9 security states. + * The ordering of the enumeration corresponds to the low 2 bits + * of the GPI value, and (except for Root) the concat of NSE:NS. + */ + +typedef enum ARMSecuritySpace { + ARMSS_Secure = 0, + ARMSS_NonSecure = 1, + ARMSS_Root = 2, + ARMSS_Realm = 3, +} ARMSecuritySpace; + +/* Return true if @space is secure, in the pre-v9 sense. */ +static inline bool arm_space_is_secure(ARMSecuritySpace space) +{ + return space == ARMSS_Secure || space == ARMSS_Root; +} + +/* Return the ARMSecuritySpace for @secure, assuming !RME or EL[0-2]. */ +static inline ARMSecuritySpace arm_secure_to_space(bool secure) +{ + return secure ? ARMSS_Secure : ARMSS_NonSecure; +} + +#if !defined(CONFIG_USER_ONLY) +/** + * arm_security_space_below_el3: + * @env: cpu context + * + * Return the security space of exception levels below EL3, following + * an exception return to those levels. Unlike arm_security_space, + * this doesn't care about the current EL. + */ +ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env); + +/** + * arm_is_secure_below_el3: + * @env: cpu context + * * Return true if exception levels below EL3 are in secure state, - * or would be following an exception return to that level. - * Unlike arm_is_secure() (which is always a question about the - * _current_ state of the CPU) this doesn't care about the current - * EL or mode. + * or would be following an exception return to those levels. */ static inline bool arm_is_secure_below_el3(CPUARMState *env) { - assert(!arm_feature(env, ARM_FEATURE_M)); - if (arm_feature(env, ARM_FEATURE_EL3)) { - return !(env->cp15.scr_el3 & SCR_NS); - } else { - /* If EL3 is not supported then the secure state is implementation - * defined, in which case QEMU defaults to non-secure. - */ - return false; - } + ARMSecuritySpace ss = arm_security_space_below_el3(env); + return ss == ARMSS_Secure; } /* Return true if the CPU is AArch64 EL3 or AArch32 Mon */ @@ -2452,16 +2480,23 @@ static inline bool arm_is_el3_or_mon(CPUARMState *env) return false; } -/* Return true if the processor is in secure state */ +/** + * arm_security_space: + * @env: cpu context + * + * Return the current security space of the cpu. + */ +ARMSecuritySpace arm_security_space(CPUARMState *env); + +/** + * arm_is_secure: + * @env: cpu context + * + * Return true if the processor is in secure state. + */ static inline bool arm_is_secure(CPUARMState *env) { - if (arm_feature(env, ARM_FEATURE_M)) { - return env->v7m.secure; - } - if (arm_is_el3_or_mon(env)) { - return true; - } - return arm_is_secure_below_el3(env); + return arm_space_is_secure(arm_security_space(env)); } /* @@ -2480,11 +2515,21 @@ static inline bool arm_is_el2_enabled(CPUARMState *env) } #else +static inline ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env) +{ + return ARMSS_NonSecure; +} + static inline bool arm_is_secure_below_el3(CPUARMState *env) { return false; } +static inline ARMSecuritySpace arm_security_space(CPUARMState *env) +{ + return ARMSS_NonSecure; +} + static inline bool arm_is_secure(CPUARMState *env) { return false; diff --git a/target/arm/helper.c b/target/arm/helper.c index 006447dde8..f68923d73b 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -12136,3 +12136,63 @@ void aarch64_sve_change_el(CPUARMState *env, int old_el, } } #endif + +#ifndef CONFIG_USER_ONLY +ARMSecuritySpace arm_security_space(CPUARMState *env) +{ + if (arm_feature(env, ARM_FEATURE_M)) { + return arm_secure_to_space(env->v7m.secure); + } + + /* + * If EL3 is not supported then the secure state is implementation + * defined, in which case QEMU defaults to non-secure. + */ + if (!arm_feature(env, ARM_FEATURE_EL3)) { + return ARMSS_NonSecure; + } + + /* Check for AArch64 EL3 or AArch32 Mon. */ + if (is_a64(env)) { + if (extract32(env->pstate, 2, 2) == 3) { + if (cpu_isar_feature(aa64_rme, env_archcpu(env))) { + return ARMSS_Root; + } else { + return ARMSS_Secure; + } + } + } else { + if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { + return ARMSS_Secure; + } + } + + return arm_security_space_below_el3(env); +} + +ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env) +{ + assert(!arm_feature(env, ARM_FEATURE_M)); + + /* + * If EL3 is not supported then the secure state is implementation + * defined, in which case QEMU defaults to non-secure. + */ + if (!arm_feature(env, ARM_FEATURE_EL3)) { + return ARMSS_NonSecure; + } + + /* + * Note NSE cannot be set without RME, and NSE & !NS is Reserved. + * Ignoring NSE when !NS retains consistency without having to + * modify other predicates. + */ + if (!(env->cp15.scr_el3 & SCR_NS)) { + return ARMSS_Secure; + } else if (env->cp15.scr_el3 & SCR_NSE) { + return ARMSS_Realm; + } else { + return ARMSS_NonSecure; + } +} +#endif /* !CONFIG_USER_ONLY */