From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-20.4 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 004EBC388F7 for ; Mon, 9 Nov 2020 11:36:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 95E14205CA for ; Mon, 9 Nov 2020 11:36:23 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="iAn2GzCf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729956AbgKILgN (ORCPT ); Mon, 9 Nov 2020 06:36:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35658 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729776AbgKILda (ORCPT ); Mon, 9 Nov 2020 06:33:30 -0500 Received: from mail-wr1-x432.google.com (mail-wr1-x432.google.com [IPv6:2a00:1450:4864:20::432]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4FA39C0613CF for ; Mon, 9 Nov 2020 03:33:30 -0800 (PST) Received: by mail-wr1-x432.google.com with SMTP id d12so6789332wrr.13 for ; Mon, 09 Nov 2020 03:33:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TSwlUknyxMEI+6/IdIE7BFo27ktFepUyPMrayg9jAhU=; b=iAn2GzCfxNArh5CKayH8Vx8icaJ2i4fbykQO1kjXT9qJmzxGyHErFl5pAdrOwyItSF n61YAtEdFpK5GaHftfHCQeRp6ATxUD5xTKpJupnh7JfzXWnKshJWUdT6f7nOvBuZeX6g SGTh0c59Oy/Ar+Y5/KK20aP3f4pCWGqiLNdyaUMbq443GYcVvqwIsQ966yMSdc2E8cID 3Mp9gTu52MoliwQgaKBilPbbycLyb+QisomKBY6VpW2r03L+l2WHjFsB8xxFhUSYzL8X A+z31ZTD4ZKDaaSAqD/uCOOH17EhMAWrV29kZe3P9LTQG8LRF2/gL4x0aWTjBkB3TWgg CJFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TSwlUknyxMEI+6/IdIE7BFo27ktFepUyPMrayg9jAhU=; b=Knn23CZ+nW7ijqdJ1F+zrH7OOLEozmTf1XeflunN66R4LfVHtgj1BLSoox56NuYGx8 5ZDi8DkiA6rFLvSGRG7LIcua+AdQJ3Vs5n7/R4cho3SpDGeB9fpJQpeP5qSsmxcDgRhw oMUaS6UXkwbM6ZfwW3AjA0i+tKauGKT6HQyHEml2rod9zY0AEpbI9y9eOJQOtrixVWwD LPybOxJAvtS68OUsLZfuD/UvdECOpvabNj5iQga23g2b+rxilAhz3ly5PtRaZ2q2leig WK/VmMOaodp4onOlBPmHJJgWYjMIo75BLeAtvQxRxltd/o8DVBPL3O580DDdE2Bg/Efn iXVg== X-Gm-Message-State: AOAM532iqWNQMG3WkSTAxe8XRP2OhlroxHt/ezvW4bo0M6SAxPNV+I3y txlOBG56FNbnw8tHFMjbXqAyl19H6IP0y6+I X-Google-Smtp-Source: ABdhPJxh4X0Ke2rWDYUSFAfru62h5H3hy1NB0X1m0Pp9aNMnCDnQcDcDX1PGxexIachYDmdrHQtBjQ== X-Received: by 2002:a5d:6ca6:: with SMTP id a6mr17360696wra.348.1604921608925; Mon, 09 Nov 2020 03:33:28 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:209d:10b7:c480:3e1f]) by smtp.gmail.com with ESMTPSA id z19sm12507964wmk.12.2020.11.09.03.33.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2020 03:33:28 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Cc: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Marc Zyngier , James Morse , Julien Thierry , Suzuki K Poulose , Catalin Marinas , Will Deacon , Dennis Zhou , Tejun Heo , Christoph Lameter , Mark Rutland , Lorenzo Pieralisi , Quentin Perret , Andrew Scull , Andrew Walbran , kernel-team@android.com, David Brazdil Subject: [PATCH v1 24/24] kvm: arm64: Fix EL2 mode availability checks Date: Mon, 9 Nov 2020 11:32:33 +0000 Message-Id: <20201109113233.9012-25-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109113233.9012-1-dbrazdil@google.com> References: <20201109113233.9012-1-dbrazdil@google.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org With protected nVHE hyp code interception host's PSCI CPU_ON/OFF/SUSPEND SMCs, from the host's perspective new CPUs start booting in EL1 while previously they would have booted in EL2. The kernel logic which keeps track of the mode CPUs were booted in needs to be adjusted to account for this fact. Add a static key enabled if KVM protected nVHE initialization is successful. When the key is enabled, is_hyp_mode_available continues to report `true` because its users either treat it a check whether KVM will be / has been initialized, or whether stub HVCs can be made (eg. hibernate). is_hyp_mode_mismatched is changed to report `false` when the key is enabled. That's because all cores' modes matched at the point of KVM init and KVM will not allow cores not present at init to boot. That said, the function is never used after KVM is initialized. Signed-off-by: David Brazdil --- arch/arm64/include/asm/virt.h | 17 +++++++++++++++++ arch/arm64/kvm/arm.c | 9 ++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/virt.h b/arch/arm64/include/asm/virt.h index 2c3124512c00..8159d6010f4b 100644 --- a/arch/arm64/include/asm/virt.h +++ b/arch/arm64/include/asm/virt.h @@ -66,10 +66,19 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base); void __hyp_reset_vectors(void); DECLARE_STATIC_KEY_FALSE(kvm_protected_mode); +DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); /* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available(void) { + /* + * If KVM protected mode is initialized, all CPUs must have been booted + * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && + static_branch_likely(&kvm_protected_mode_initialized)) + return true; + return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 && __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2); } @@ -77,6 +86,14 @@ static inline bool is_hyp_mode_available(void) /* Check if the bootloader has booted CPUs in different modes */ static inline bool is_hyp_mode_mismatched(void) { + /* + * If KVM protected mode is initialized, all CPUs must have been booted + * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && + static_branch_likely(&kvm_protected_mode_initialized)) + return false; + return __boot_cpu_mode[0] != __boot_cpu_mode[1]; } diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index c09b95cfa00a..9a2329c92a01 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -48,6 +48,7 @@ __asm__(".arch_extension virt"); #endif DEFINE_STATIC_KEY_FALSE(kvm_protected_mode); +DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); @@ -1838,12 +1839,14 @@ int kvm_arch_init(void *opaque) if (err) goto out_hyp; - if (is_kvm_protected_mode()) + if (is_kvm_protected_mode()) { + static_branch_enable(&kvm_protected_mode_initialized); kvm_info("Protected nVHE mode initialized successfully\n"); - else if (in_hyp_mode) + } else if (in_hyp_mode) { kvm_info("VHE mode initialized successfully\n"); - else + } else { kvm_info("Hyp mode initialized successfully\n"); + } return 0; -- 2.29.2.222.g5d2a92d10f8-goog From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6F9E0C388F7 for ; Mon, 9 Nov 2020 11:33:36 +0000 (UTC) Received: from mm01.cs.columbia.edu (mm01.cs.columbia.edu [128.59.11.253]) by mail.kernel.org (Postfix) with ESMTP id E40AE206ED for ; Mon, 9 Nov 2020 11:33:35 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="iAn2GzCf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E40AE206ED Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvmarm-bounces@lists.cs.columbia.edu Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 82CEA4B4DF; Mon, 9 Nov 2020 06:33:35 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Authentication-Results: mm01.cs.columbia.edu (amavisd-new); dkim=softfail (fail, message has been altered) header.i=@google.com Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id cj2vQvDcRmlT; Mon, 9 Nov 2020 06:33:34 -0500 (EST) Received: from mm01.cs.columbia.edu (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 571DA4B4E1; Mon, 9 Nov 2020 06:33:34 -0500 (EST) Received: from localhost (localhost [127.0.0.1]) by mm01.cs.columbia.edu (Postfix) with ESMTP id 5AC004B829 for ; Mon, 9 Nov 2020 06:33:32 -0500 (EST) X-Virus-Scanned: at lists.cs.columbia.edu Received: from mm01.cs.columbia.edu ([127.0.0.1]) by localhost (mm01.cs.columbia.edu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id AsvayeIINIyN for ; Mon, 9 Nov 2020 06:33:31 -0500 (EST) Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by mm01.cs.columbia.edu (Postfix) with ESMTPS id 35AB44B771 for ; Mon, 9 Nov 2020 06:33:30 -0500 (EST) Received: by mail-wr1-f52.google.com with SMTP id d12so6789350wrr.13 for ; Mon, 09 Nov 2020 03:33:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TSwlUknyxMEI+6/IdIE7BFo27ktFepUyPMrayg9jAhU=; b=iAn2GzCfxNArh5CKayH8Vx8icaJ2i4fbykQO1kjXT9qJmzxGyHErFl5pAdrOwyItSF n61YAtEdFpK5GaHftfHCQeRp6ATxUD5xTKpJupnh7JfzXWnKshJWUdT6f7nOvBuZeX6g SGTh0c59Oy/Ar+Y5/KK20aP3f4pCWGqiLNdyaUMbq443GYcVvqwIsQ966yMSdc2E8cID 3Mp9gTu52MoliwQgaKBilPbbycLyb+QisomKBY6VpW2r03L+l2WHjFsB8xxFhUSYzL8X A+z31ZTD4ZKDaaSAqD/uCOOH17EhMAWrV29kZe3P9LTQG8LRF2/gL4x0aWTjBkB3TWgg CJFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TSwlUknyxMEI+6/IdIE7BFo27ktFepUyPMrayg9jAhU=; b=EK3l0naa4DAZOACwnRO0SQ2CeR3zKt/jhBspktjt+M8z46sG6taCgMm8s50lHB1BCw unics2vaUmFBkFEoSw42fOXZsJOhPDTGVH5k0XF2A+nGVC45gp6xr4AkLwsWPhgre/nD Pb66gsTNqRUUekcTZob2tujqtVJ7N83re/6j8pKi4ND6GX9KFyigS95XMzccAYez35// l2D6HSjMLhqqbrnGwi+naMnGe1cVVGIeQWtE5MLu3CP6Gm96WzNiDYkfxl2/izYUgg23 TBX0iNP472nJcAeJrfSNJe93C+Ct7B1MZsnbnnYC1Lue1GgOhKyi/b2wRGWBUh+yQLPk /spw== X-Gm-Message-State: AOAM531NSWLLiAhfLN9bsHUzSVzOJNU4bmYLO9Ia59JaJ6+n6Gyym+B8 NH4MkiUC1YoL32A8HQN9e7cAtJUkSgy/d3e/ X-Google-Smtp-Source: ABdhPJxh4X0Ke2rWDYUSFAfru62h5H3hy1NB0X1m0Pp9aNMnCDnQcDcDX1PGxexIachYDmdrHQtBjQ== X-Received: by 2002:a5d:6ca6:: with SMTP id a6mr17360696wra.348.1604921608925; Mon, 09 Nov 2020 03:33:28 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:209d:10b7:c480:3e1f]) by smtp.gmail.com with ESMTPSA id z19sm12507964wmk.12.2020.11.09.03.33.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2020 03:33:28 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v1 24/24] kvm: arm64: Fix EL2 mode availability checks Date: Mon, 9 Nov 2020 11:32:33 +0000 Message-Id: <20201109113233.9012-25-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109113233.9012-1-dbrazdil@google.com> References: <20201109113233.9012-1-dbrazdil@google.com> MIME-Version: 1.0 Cc: kernel-team@android.com, Lorenzo Pieralisi , Andrew Walbran , Marc Zyngier , linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , Will Deacon X-BeenThere: kvmarm@lists.cs.columbia.edu X-Mailman-Version: 2.1.14 Precedence: list List-Id: Where KVM/ARM decisions are made List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: kvmarm-bounces@lists.cs.columbia.edu Sender: kvmarm-bounces@lists.cs.columbia.edu With protected nVHE hyp code interception host's PSCI CPU_ON/OFF/SUSPEND SMCs, from the host's perspective new CPUs start booting in EL1 while previously they would have booted in EL2. The kernel logic which keeps track of the mode CPUs were booted in needs to be adjusted to account for this fact. Add a static key enabled if KVM protected nVHE initialization is successful. When the key is enabled, is_hyp_mode_available continues to report `true` because its users either treat it a check whether KVM will be / has been initialized, or whether stub HVCs can be made (eg. hibernate). is_hyp_mode_mismatched is changed to report `false` when the key is enabled. That's because all cores' modes matched at the point of KVM init and KVM will not allow cores not present at init to boot. That said, the function is never used after KVM is initialized. Signed-off-by: David Brazdil --- arch/arm64/include/asm/virt.h | 17 +++++++++++++++++ arch/arm64/kvm/arm.c | 9 ++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/virt.h b/arch/arm64/include/asm/virt.h index 2c3124512c00..8159d6010f4b 100644 --- a/arch/arm64/include/asm/virt.h +++ b/arch/arm64/include/asm/virt.h @@ -66,10 +66,19 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base); void __hyp_reset_vectors(void); DECLARE_STATIC_KEY_FALSE(kvm_protected_mode); +DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); /* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available(void) { + /* + * If KVM protected mode is initialized, all CPUs must have been booted + * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && + static_branch_likely(&kvm_protected_mode_initialized)) + return true; + return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 && __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2); } @@ -77,6 +86,14 @@ static inline bool is_hyp_mode_available(void) /* Check if the bootloader has booted CPUs in different modes */ static inline bool is_hyp_mode_mismatched(void) { + /* + * If KVM protected mode is initialized, all CPUs must have been booted + * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && + static_branch_likely(&kvm_protected_mode_initialized)) + return false; + return __boot_cpu_mode[0] != __boot_cpu_mode[1]; } diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index c09b95cfa00a..9a2329c92a01 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -48,6 +48,7 @@ __asm__(".arch_extension virt"); #endif DEFINE_STATIC_KEY_FALSE(kvm_protected_mode); +DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); @@ -1838,12 +1839,14 @@ int kvm_arch_init(void *opaque) if (err) goto out_hyp; - if (is_kvm_protected_mode()) + if (is_kvm_protected_mode()) { + static_branch_enable(&kvm_protected_mode_initialized); kvm_info("Protected nVHE mode initialized successfully\n"); - else if (in_hyp_mode) + } else if (in_hyp_mode) { kvm_info("VHE mode initialized successfully\n"); - else + } else { kvm_info("Hyp mode initialized successfully\n"); + } return 0; -- 2.29.2.222.g5d2a92d10f8-goog _______________________________________________ kvmarm mailing list kvmarm@lists.cs.columbia.edu https://lists.cs.columbia.edu/mailman/listinfo/kvmarm From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 500E1C388F7 for ; Mon, 9 Nov 2020 11:40:42 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id C95CB206B6 for ; Mon, 9 Nov 2020 11:40:41 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="mwfz7m25"; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="iAn2GzCf" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C95CB206B6 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=We9gwdPf9IQU9Wj+DDR/wpY2n2Z468Wyh9HE4MM1F54=; b=mwfz7m25JGkL1IalDA79UrznL ZHFbigVbm+LYfg85pQoPccakUlXh0iJqCJRbvo/yWJM43rp4oo0b/SYSPrDdXlLmdbzNy9Y2fovFc GcpPw9bq0M0+A28YNDHx5f2Oos/K/DI8QGZuKGXENoRE8anEwAMeSIy2XE0oMdiEdobTcZZ+bDM+k G+afmQvWJQkzK74Gfjq5Qg+TLr36pUnC+ehMLtUMER92xb0YX7DguytNp7qrizvVoWNd2yoovrxso /WUlzaM9rquk1T2uifnHGShgLOeSjKqiB8XBkH0OMfvoWNOeDFcRR45Otqf1X5ujDzg7VF1H1iFsA at+fn858A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kc5Wl-0007lz-AV; Mon, 09 Nov 2020 11:40:03 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kc5QR-0004bv-FQ for linux-arm-kernel@lists.infradead.org; Mon, 09 Nov 2020 11:33:40 +0000 Received: by mail-wr1-x42e.google.com with SMTP id l1so3908596wrb.9 for ; Mon, 09 Nov 2020 03:33:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=TSwlUknyxMEI+6/IdIE7BFo27ktFepUyPMrayg9jAhU=; b=iAn2GzCfxNArh5CKayH8Vx8icaJ2i4fbykQO1kjXT9qJmzxGyHErFl5pAdrOwyItSF n61YAtEdFpK5GaHftfHCQeRp6ATxUD5xTKpJupnh7JfzXWnKshJWUdT6f7nOvBuZeX6g SGTh0c59Oy/Ar+Y5/KK20aP3f4pCWGqiLNdyaUMbq443GYcVvqwIsQ966yMSdc2E8cID 3Mp9gTu52MoliwQgaKBilPbbycLyb+QisomKBY6VpW2r03L+l2WHjFsB8xxFhUSYzL8X A+z31ZTD4ZKDaaSAqD/uCOOH17EhMAWrV29kZe3P9LTQG8LRF2/gL4x0aWTjBkB3TWgg CJFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=TSwlUknyxMEI+6/IdIE7BFo27ktFepUyPMrayg9jAhU=; b=njnxIQP83oT8MkvV9cw7ge6ik1E3Tjbm7ZdXQ9r8ZO9eWvrDXyr7YHWtg0Rhm0F3z+ WnI9vx/hHecOpgwg92eXOSXoLRGCgT3u8kLKEnwin9IwoFs/NjTIDuK96Hh+mECMaK1g BsPylUvSgPNefKCQqnF2DlopdOyETaT4fpauzaHJoTvlXWVcWYzAlGyr8/ip7TT2k1M9 b579/6yxoPpUkiU07RtzGpTsofA/Y1huPSUt1NZqw4qqD97ZOSBcuHbSSU6lGnw7KzQx 25JHQGek1Wr2LMD/Zw0Mp0WUXzwbdVIekEyYspFNsAQbnfO+TOjsv9434w6Dix9cwgY/ s7Dw== X-Gm-Message-State: AOAM531+R8ONyMYZ97t8LJWx3j3rgvgmc8cPclJ2a8OBQ3K0TtJz4jTL VjZOjLe8PUqDL0aL8QX+nHxTvg== X-Google-Smtp-Source: ABdhPJxh4X0Ke2rWDYUSFAfru62h5H3hy1NB0X1m0Pp9aNMnCDnQcDcDX1PGxexIachYDmdrHQtBjQ== X-Received: by 2002:a5d:6ca6:: with SMTP id a6mr17360696wra.348.1604921608925; Mon, 09 Nov 2020 03:33:28 -0800 (PST) Received: from localhost ([2a01:4b00:8523:2d03:209d:10b7:c480:3e1f]) by smtp.gmail.com with ESMTPSA id z19sm12507964wmk.12.2020.11.09.03.33.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 09 Nov 2020 03:33:28 -0800 (PST) From: David Brazdil To: kvmarm@lists.cs.columbia.edu Subject: [PATCH v1 24/24] kvm: arm64: Fix EL2 mode availability checks Date: Mon, 9 Nov 2020 11:32:33 +0000 Message-Id: <20201109113233.9012-25-dbrazdil@google.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201109113233.9012-1-dbrazdil@google.com> References: <20201109113233.9012-1-dbrazdil@google.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201109_063331_776319_7DB63D09 X-CRM114-Status: GOOD ( 19.99 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Mark Rutland , kernel-team@android.com, Lorenzo Pieralisi , Andrew Walbran , Suzuki K Poulose , Marc Zyngier , Quentin Perret , linux-kernel@vger.kernel.org, James Morse , linux-arm-kernel@lists.infradead.org, Catalin Marinas , Tejun Heo , Dennis Zhou , Christoph Lameter , David Brazdil , Will Deacon , Julien Thierry , Andrew Scull Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org With protected nVHE hyp code interception host's PSCI CPU_ON/OFF/SUSPEND SMCs, from the host's perspective new CPUs start booting in EL1 while previously they would have booted in EL2. The kernel logic which keeps track of the mode CPUs were booted in needs to be adjusted to account for this fact. Add a static key enabled if KVM protected nVHE initialization is successful. When the key is enabled, is_hyp_mode_available continues to report `true` because its users either treat it a check whether KVM will be / has been initialized, or whether stub HVCs can be made (eg. hibernate). is_hyp_mode_mismatched is changed to report `false` when the key is enabled. That's because all cores' modes matched at the point of KVM init and KVM will not allow cores not present at init to boot. That said, the function is never used after KVM is initialized. Signed-off-by: David Brazdil --- arch/arm64/include/asm/virt.h | 17 +++++++++++++++++ arch/arm64/kvm/arm.c | 9 ++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/arch/arm64/include/asm/virt.h b/arch/arm64/include/asm/virt.h index 2c3124512c00..8159d6010f4b 100644 --- a/arch/arm64/include/asm/virt.h +++ b/arch/arm64/include/asm/virt.h @@ -66,10 +66,19 @@ void __hyp_set_vectors(phys_addr_t phys_vector_base); void __hyp_reset_vectors(void); DECLARE_STATIC_KEY_FALSE(kvm_protected_mode); +DECLARE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); /* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available(void) { + /* + * If KVM protected mode is initialized, all CPUs must have been booted + * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && + static_branch_likely(&kvm_protected_mode_initialized)) + return true; + return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 && __boot_cpu_mode[1] == BOOT_CPU_MODE_EL2); } @@ -77,6 +86,14 @@ static inline bool is_hyp_mode_available(void) /* Check if the bootloader has booted CPUs in different modes */ static inline bool is_hyp_mode_mismatched(void) { + /* + * If KVM protected mode is initialized, all CPUs must have been booted + * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. + */ + if (IS_ENABLED(CONFIG_KVM) && + static_branch_likely(&kvm_protected_mode_initialized)) + return false; + return __boot_cpu_mode[0] != __boot_cpu_mode[1]; } diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index c09b95cfa00a..9a2329c92a01 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -48,6 +48,7 @@ __asm__(".arch_extension virt"); #endif DEFINE_STATIC_KEY_FALSE(kvm_protected_mode); +DEFINE_STATIC_KEY_FALSE(kvm_protected_mode_initialized); DECLARE_KVM_HYP_PER_CPU(unsigned long, kvm_hyp_vector); @@ -1838,12 +1839,14 @@ int kvm_arch_init(void *opaque) if (err) goto out_hyp; - if (is_kvm_protected_mode()) + if (is_kvm_protected_mode()) { + static_branch_enable(&kvm_protected_mode_initialized); kvm_info("Protected nVHE mode initialized successfully\n"); - else if (in_hyp_mode) + } else if (in_hyp_mode) { kvm_info("VHE mode initialized successfully\n"); - else + } else { kvm_info("Hyp mode initialized successfully\n"); + } return 0; -- 2.29.2.222.g5d2a92d10f8-goog _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel