From patchwork Mon Feb 27 23:01:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Henderson X-Patchwork-Id: 657142 Delivered-To: patch@linaro.org Received: by 2002:adf:a3c6:0:0:0:0:0 with SMTP id m6csp2616028wrb; Mon, 27 Feb 2023 15:02:20 -0800 (PST) X-Google-Smtp-Source: AK7set/UpapcgVPOFLUYLZDjF2H+SqR49osu47mHlhAjyUu2/YOXwLza91mtb+heCr0eq35lKvy3 X-Received: by 2002:a05:6214:d8d:b0:56e:a7d9:b132 with SMTP id e13-20020a0562140d8d00b0056ea7d9b132mr1665740qve.22.1677538940467; Mon, 27 Feb 2023 15:02:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677538940; cv=none; d=google.com; s=arc-20160816; b=SOP0DLQoUNNX9/NfHW8kvcDparSyoCojGY/xssyTVBSl1w7EIRrAPFANT3HmeH402T LyTL9UqWxh2ichfug3XcIJBX2vSCXfL3mGt908N3Fg033RnM70KzJ5AKV2ddAujWEDLF 1ALxnZQyc2QSM22Fka8zUWIyUEfrGjj60KYYvtfuzUmDPT7TO220Wsy4Ox4pEG2nXZfw Q3F8BVCA7K0Kli3A/1nECRuQEX8ldpa36tUFhHfE2AOLyQlGCqNzZ7Ss0nd25oAWu1lT Jv8h8PXDlZbgyZY3WMr2cnPEtfoEBaqPDaX9a2SYfTc1o9Hqjr9sgbNF8V20xfZbHQ+x T5Eg== 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=Z2H/Gk1sBZb1zXekpLJvx4pEHghvfBzjstzPZ4R0ZjE=; b=Tp+OjXodHvxO5eOp02QPdCqE+ZN9XmCrFsbps9y1d0Rc9iXfIyQauThvA9WXzh9l+o julTFco7CmCRlpyycDhFPFTk33Wu/3Fl1CWx/dNHEiUMFG0y/YXnBAwSefLwL9vfXKhN 9AwYCmKTDMWRpYZ+RDmXwMXz6N+IqWxpJ5DMz7RKFjkU4xiJpV3p/CwzcYwWCzt04kQZ TYKQX1FmC7qkZBBHFNFZozUtyi1CRtl80Oz6k2NrYNffSQOP8/dHNmhSii8OWUZNvuTC VlgiSN3jv2IJTumxLkp6tbBlbVE0BUzM717DOZXvR/8TiFgAxy1EanFwnS/dbz1DyCQL ilXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Mnn2xqoV; 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 c13-20020a0cca0d000000b005722cefa697si7539525qvk.305.2023.02.27.15.02.20 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 27 Feb 2023 15:02:20 -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=Mnn2xqoV; 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 1pWmV3-0007l9-Qb; Mon, 27 Feb 2023 18:01:45 -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 1pWmUz-0007fY-3w for qemu-devel@nongnu.org; Mon, 27 Feb 2023 18:01:37 -0500 Received: from mail-pj1-x102f.google.com ([2607:f8b0:4864:20::102f]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pWmUx-0000zp-6V for qemu-devel@nongnu.org; Mon, 27 Feb 2023 18:01:36 -0500 Received: by mail-pj1-x102f.google.com with SMTP id oj5so3878164pjb.5 for ; Mon, 27 Feb 2023 15:01:34 -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:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Z2H/Gk1sBZb1zXekpLJvx4pEHghvfBzjstzPZ4R0ZjE=; b=Mnn2xqoVUyaEtMsfWEBI/Avs2nY20c7q7zcAoC2/VFE0M6/XgkoOBLtjHfbGAbmwhZ 9J4E5kG4dXYK5g8KmHZUiz8lfvvC2Rgm7Cpq/kNFYjjhSu69y0XRddB76MEGXbd48IFw q4P3DqC8TizVDjx3LjvwaWewmakwTdR8uyYJ1+s+JmJ6MPxOL7jOqcBNa9fnbdKcpdwI NWfouqldQYlGYY0wftYUYTiB7HQ+3/Ri3eX5/tHG20f0X8fJ0JOOA+D1NYaqOuxmwy3c 9y/OLOxcs3i1hpJ/pVaiQl2uwX7tFrbN8x8NZW19eS0ru2B5tLA0KYoHV/jYlT0JeKVF 7x0Q== 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:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Z2H/Gk1sBZb1zXekpLJvx4pEHghvfBzjstzPZ4R0ZjE=; b=xiByrsxXfQQlv9T9dSMDoMngIKXy2vWYJJy+vqkctfqJB4GQ8/swcJWZeP4WFg2LDW k3S5mVKhKYcNXucWgYQ9X+63b6T9+te/miUYRtVMWFMPDBXv8UdzyHo2Ars2u5006Egy a/sxRpIIB2HHbIX5SotV6tQ/Xe70scl5abrkkbjykmGNUSEc1l7HwxC0kig2xCB+rgb1 qnCyq8Ch9n6UTmMQGVvX/gfLC3/rK1+AeUk1NZR79TtUbxwKVd4oYB0kyHOvji5S1np5 ITKhxnv42zQsib1+J7aa3jXdQXdJF/7JlLi7awOT5ypX934/40mTpJhhjW5bUl/zzNVE nKuQ== X-Gm-Message-State: AO0yUKVnT+gpVCS1cTB3x6MGEpFDd2ynBgHF3G/wGIjBIVGD8LsVhFus K7V9rV9fWoZlK77PAFJrQ1e0t/+jDgu7ixSxHc8= X-Received: by 2002:a17:90b:3b4b:b0:234:56aa:f176 with SMTP id ot11-20020a17090b3b4b00b0023456aaf176mr9561468pjb.24.1677538893487; Mon, 27 Feb 2023 15:01:33 -0800 (PST) Received: from stoup.. (rrcs-173-198-77-218.west.biz.rr.com. [173.198.77.218]) by smtp.gmail.com with ESMTPSA id u2-20020a637902000000b0050301745a5dsm4406130pgc.50.2023.02.27.15.01.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Feb 2023 15:01:33 -0800 (PST) From: Richard Henderson To: qemu-devel@nongnu.org Cc: qemu-arm@nongnu.org, Peter Maydell Subject: [PATCH for-8.0 v4 05/21] target/arm: Introduce ARMSecuritySpace Date: Mon, 27 Feb 2023 13:01:06 -1000 Message-Id: <20230227230122.816702-6-richard.henderson@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230227230122.816702-1-richard.henderson@linaro.org> References: <20230227230122.816702-1-richard.henderson@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2607:f8b0:4864:20::102f; envelope-from=richard.henderson@linaro.org; helo=mail-pj1-x102f.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=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 8d18d98350..203a3e0046 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -2409,25 +2409,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 */ @@ -2447,16 +2475,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)); } /* @@ -2475,11 +2510,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 6e9dcb17c4..af71caa7c7 100644 --- a/target/arm/helper.c +++ b/target/arm/helper.c @@ -12155,3 +12155,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 */