From patchwork Tue Jul 18 03:22:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121746 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1483482vqt; Mon, 17 Jul 2023 20:32:59 -0700 (PDT) X-Google-Smtp-Source: APBJJlGdb0Q02R4n4TR3jQDZzudERsvzRvQ3nLOUrsgN8r/xMdDmMtPp2MoUCOSfY0q0IcOKNkS5 X-Received: by 2002:a17:906:6493:b0:975:63f4:46 with SMTP id e19-20020a170906649300b0097563f40046mr12028449ejm.57.1689651179421; Mon, 17 Jul 2023 20:32:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651179; cv=none; d=google.com; s=arc-20160816; b=bsKoprimYz4UW3mfx02RkO57dD9tJK3mnx0te2WPXNADuvXFH4cquS9G4SM+cBoyzD yi5wPNVN9RrZqihvcxYir5l2sJ/y838S6D1UDdBnRZQ69Q+qDvegTNA6HiGY4I60hJst aoZ8dUev/hqiE6+Hp5wpLTvgEKXHeRUBxOhDwTHPHAKwelNwIHtlnsG/eNjjPsky+/Tv GzEDL2YccvoN57eLmMj2Li3tBYjScDeMebOzCPWP6KapUlmn3HicAWUi6tMwjc8VQFTS W0TSIdPXAMoOnEmQdUhcz63SkUSH++lBG93D4Kg3dB8WGTvr5ZcSxojFMHyA5kRNx1Kh FqMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4OH2nJxZTXPnwyWDhRKyI4KLZM7zBv8h2VCX3NFTe4M=; fh=0q4Ral/ZvWHifErBr5uVVFNUVQsTG1/TVmQRaCWii40=; b=Ak3Bxp2zyFetQ/djPluY+/SeyQPMv9ttSp2M9U5BgqfFIw9SGxsdOwwpYEp0zxe3C6 nS2g+7uiLZi/mBWO33bd32bJULzoolxLmeHRW9bm3dyhG/fUjbowHMAVpKR5hQKgbe+l OiVTGuhbpEJldqDxUNH63QV5dtp7QI5sNTFbwwVon+Tb0vWZQpSAdUcFjvo1XY2VdVCf MxIUiWzAhMuJa2LZ43dLT4f72pXd9Dr2q9e1+vYjrlX+fHNHPlqtrGAORIcc0sktddUU 5t6KT3Cn0ZfBujuMaCSAMHgx0Y7H3Y1a6TjtZBm0UzdRWCuT435l5ZCnk5RjM6sDa7bK lDKA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=WicSNvr2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t17-20020a1709067c1100b0098e4aef078esi513115ejo.778.2023.07.17.20.32.36; Mon, 17 Jul 2023 20:32:59 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=WicSNvr2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230177AbjGRDXR (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60998 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230144AbjGRDXN (ORCPT ); Mon, 17 Jul 2023 23:23:13 -0400 Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 961F910D1; Mon, 17 Jul 2023 20:23:11 -0700 (PDT) Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-262fa79e97fso2527285a91.2; Mon, 17 Jul 2023 20:23:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650591; x=1692242591; 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=4OH2nJxZTXPnwyWDhRKyI4KLZM7zBv8h2VCX3NFTe4M=; b=WicSNvr2SEHT5wggpdXQuAdZEuqxZHgUCz6kyls3VFj+KAT7nn/VLRqIacMxHjclhg 9NI/u3peLuNPwgXIyI1aH0MbjKPrar9AMA2AIq7j9w7WXjY/ojt2iVyl/+Db0m0X3vtm Sm12160J7woPrs9gC6sZ0yAVty5iRk+kW07t30YJf4EMy4hPU55x4rqInXjvP5psTu+Q R7PM6S6DfDGBbt1xDmQKuZCs243Ma0kqYfwGx7OdhO3LIifxrOED9wVrAIaz/nKdasd1 AvJpp/MCx5IarqXrWWpWuwETbbKgBkL7ix/Q3EncxqBOTXYyPoTk1GxPid1IO+WdZh5m nFZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650591; x=1692242591; 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=4OH2nJxZTXPnwyWDhRKyI4KLZM7zBv8h2VCX3NFTe4M=; b=d0Ii05NgVl6bf04m2xPLdUsfz0ag/YZnUKMvvg+vtXj44F9qFpdQYsDJUYVGcO1zbk /j3BtXjGzwpqK+/xyTShypVx/qNRdqLc4B+OEZmtitF2HDdPbVv4ChSWTtlXySiAoB/U 4hciRoZt806uBDJ30cTm49QnnAKrO6+unx+B1NCXqDuoL745TTPqhx08AdSuTHb9uIbC 3Mn84ll/R1F1ty9H0/P8g0wWUphKXLQsvdufKb65/XoBv6C13lwvaunhyhGf2o2ylaeu tW9eiUQ6JtEwkbVFVAWe8qLWjzQX7J7utoQdoe8mtxyqmJLNWzrkNgoekPCcWj5voDmO aTDA== X-Gm-Message-State: ABy/qLaOxnl73JJI8dwpLztNnc7yiLnv3zGadAx4BKcOjIjq0DvOe0Hw BwUKUlDr1NuMNL0dolIlhe4= X-Received: by 2002:a17:90a:4144:b0:262:d6e9:208b with SMTP id m4-20020a17090a414400b00262d6e9208bmr9895592pjg.4.1689650590986; Mon, 17 Jul 2023 20:23:10 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:10 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com Subject: [PATCH V3 1/9] x86/hyperv: Add sev-snp enlightened guest static key Date: Mon, 17 Jul 2023 23:22:55 -0400 Message-Id: <20230718032304.136888-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727675025510700 X-GMAIL-MSGID: 1771727675025510700 From: Tianyu Lan Introduce static key isolation_type_en_snp for enlightened sev-snp guest check. Signed-off-by: Tianyu Lan Reviewed-by: Michael Kelley --- arch/x86/hyperv/ivm.c | 11 +++++++++++ arch/x86/include/asm/mshyperv.h | 3 +++ arch/x86/kernel/cpu/mshyperv.c | 9 +++++++-- drivers/hv/hv_common.c | 6 ++++++ include/asm-generic/mshyperv.h | 12 +++++++++--- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 14f46ad2ca64..b2b5cb19fac9 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -413,3 +413,14 @@ bool hv_isolation_type_snp(void) { return static_branch_unlikely(&isolation_type_snp); } + +DEFINE_STATIC_KEY_FALSE(isolation_type_en_snp); +/* + * hv_isolation_type_en_snp - Check system runs in the AMD SEV-SNP based + * isolation enlightened VM. + */ +bool hv_isolation_type_en_snp(void) +{ + return static_branch_unlikely(&isolation_type_en_snp); +} + diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index 88d9ef98e087..2fa38e9f6207 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -26,6 +26,7 @@ union hv_ghcb; DECLARE_STATIC_KEY_FALSE(isolation_type_snp); +DECLARE_STATIC_KEY_FALSE(isolation_type_en_snp); typedef int (*hyperv_fill_flush_list_func)( struct hv_guest_mapping_flush_list *flush, @@ -45,6 +46,8 @@ extern void *hv_hypercall_pg; extern u64 hv_current_partition_id; +extern bool hv_isolation_type_en_snp(void); + extern union hv_ghcb * __percpu *hv_ghcb_pg; int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index c7969e806c64..5398fb2f4d39 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -402,8 +402,12 @@ static void __init ms_hyperv_init_platform(void) pr_info("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n", ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b); - if (hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP) + + if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) { + static_branch_enable(&isolation_type_en_snp); + } else if (hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP) { static_branch_enable(&isolation_type_snp); + } } if (hv_max_functions_eax >= HYPERV_CPUID_NESTED_FEATURES) { @@ -473,7 +477,8 @@ static void __init ms_hyperv_init_platform(void) #if IS_ENABLED(CONFIG_HYPERV) if ((hv_get_isolation_type() == HV_ISOLATION_TYPE_VBS) || - (hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP)) + ((hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP) && + ms_hyperv.paravisor_present)) hv_vtom_init(); /* * Setup the hook to get control post apic initialization. diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c index 542a1d53b303..4b4aa53c34c2 100644 --- a/drivers/hv/hv_common.c +++ b/drivers/hv/hv_common.c @@ -502,6 +502,12 @@ bool __weak hv_isolation_type_snp(void) } EXPORT_SYMBOL_GPL(hv_isolation_type_snp); +bool __weak hv_isolation_type_en_snp(void) +{ + return false; +} +EXPORT_SYMBOL_GPL(hv_isolation_type_en_snp); + void __weak hv_setup_vmbus_handler(void (*handler)(void)) { } diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 402a8c1c202d..6b5c41f90398 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -36,15 +36,21 @@ struct ms_hyperv_info { u32 nested_features; u32 max_vp_index; u32 max_lp_index; - u32 isolation_config_a; + union { + u32 isolation_config_a; + struct { + u32 paravisor_present : 1; + u32 reserved_a1 : 31; + }; + }; union { u32 isolation_config_b; struct { u32 cvm_type : 4; - u32 reserved1 : 1; + u32 reserved_b1 : 1; u32 shared_gpa_boundary_active : 1; u32 shared_gpa_boundary_bits : 6; - u32 reserved2 : 20; + u32 reserved_b2 : 20; }; }; u64 shared_gpa_boundary; From patchwork Tue Jul 18 03:22:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1483049vqt; Mon, 17 Jul 2023 20:31:28 -0700 (PDT) X-Google-Smtp-Source: APBJJlFfCfgRO0LXvGlWCKRXPSWxhjh897b1KtGL6BSRFs1fdVCEuFbD6BXcux4/diDTz1SGeCXR X-Received: by 2002:a05:6a00:a1b:b0:682:9162:7218 with SMTP id p27-20020a056a000a1b00b0068291627218mr17851290pfh.15.1689651088418; Mon, 17 Jul 2023 20:31:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651088; cv=none; d=google.com; s=arc-20160816; b=lx7ESMBbLLykDTy93txpl9POzgL57yslqgrJ7JMEHvi9jObtLSjBJwGrARYnJhgXqi 9O87qBJ76ew5eHCoyhx2Z1Fggf108byq1ABMO7tcpeIKYtZK/Pczze4Xb9LQjcQRR479 d6THIWvhXX3ppyR4OZ1WcqkrJklt1XQ3SIVWxyL65g0GRN8cCEUuUC4j14YrnkbtZiJ0 qb2YhqevmxfEQbc/tU5oo8JlGc5cxljG31orJmsb0CFLADFpjAPA4v+JGETIFyaiZ9SH U4Kizm8yzs3ZXTVF5k3phEJjLi62In3PUKkBfvzyDjjOuiYecEhdX+xNwgjor8LRv4rk Xleg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=EzmXN52+TQQdhtLMBjsRB9gT+s4o48H638S+GTEwYko=; fh=0q4Ral/ZvWHifErBr5uVVFNUVQsTG1/TVmQRaCWii40=; b=tvtDgSXwRJugN2oW1YuD8vn/EiimJNRmWDO8NNHugo7yrdsJt4GimfolpiuxyRECO1 q49DI/NvbhhZ4Gm/Rql36THkbb+Ruh1/71gwF4VBz+GmCzZ9ZZZGV/kCB1SvT3hXBXdU ZjB39Vz2O7xwmzddJIdx4ozn8KauDGel0lHC3Tm8o4tXPN5KJQkpfRdk20YniBRUoFF4 slerbIvv6uzsxmFAGPgbh7TEErx9uXiFxoICmnwyFXAD3unTrFMt+6D2PcXZayua5rqo cz2at4sF6prXxpDEDUzVTeneUTxkSwlXGuoju0Uq01r8tz1MBiBkZ9v03xXEUKf0mn6u x8+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=MaaO9u2t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r124-20020a632b82000000b0055785a37147si799716pgr.590.2023.07.17.20.31.15; Mon, 17 Jul 2023 20:31:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=MaaO9u2t; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231178AbjGRDXc (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229790AbjGRDXQ (ORCPT ); Mon, 17 Jul 2023 23:23:16 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6938810D1; Mon, 17 Jul 2023 20:23:13 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id 98e67ed59e1d1-263121cd04eso2776001a91.2; Mon, 17 Jul 2023 20:23:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650593; x=1692242593; 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=EzmXN52+TQQdhtLMBjsRB9gT+s4o48H638S+GTEwYko=; b=MaaO9u2t7kLY11kzlBYjIwd+kNfrSwJGyynCDbxa20rdBog+n+riq87clrqVsCNdUA YD3+/9nmcNgkxvoJfLgHY9UHyLHH2P14oP+xhjM5faTFPfCJBukMcGxKGAjlDVmDCqhm W4jqKxGnEaom5bsVHzLHsEoZj/TDQV+zaEFeqJ8iSTDTlzHYy8w+xT85Mz8R6Z7Mb5fM HyUkBOYr+M55iUrJtH+fr0HQpYC5sPxzNegSQWY9phL1EVq8MQUz+1f81Go081L3UKBC 7QA8596vyZ2NuskITIHPpxfnzz2ntFRSPfEMuTVj7it7BwP+Hqv42O3zS43391cMq3iQ +YbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650593; x=1692242593; 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=EzmXN52+TQQdhtLMBjsRB9gT+s4o48H638S+GTEwYko=; b=Wpepm970oGCzz2r2iqaOnQGBHs1Eh5UqunJrxVE9kItZdo1uEK8g6Dc8WhH88R0Q7u pFvmMS4TndXZGYDY5U9/OZV2IEt8soIqMPXt3dSzXVlJ4KlvHAF53HrU6+yhrn0YSXD4 bmbf8eqvJUV1FP65bU5MoAwnyHCR/lSZWHOM4eo6WsvZvnEmGEOInUeTNmyHZ6vZlyZe +t2d7FPptBeLhTnnF6ZbygAJpKNPQ2IcGLo3mmM9CGZOMJIOnPultL8RBECB4GzRXJ6S IB9lWqYbiZ/sFdJu6yiWcT8SEVG+1e5l91d8wJFESWVWhp62epE+xk5G9f8VuAYYSMiD HjnA== X-Gm-Message-State: ABy/qLbndJYFYRSExckzhMUObWNq/9N2VDVifOaWJhw0LB38Yl1xpBdk KklzYK7Rt0Vsux93FJojIE0= X-Received: by 2002:a17:90b:1c0e:b0:25d:eca9:1621 with SMTP id oc14-20020a17090b1c0e00b0025deca91621mr11396604pjb.6.1689650592786; Mon, 17 Jul 2023 20:23:12 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:12 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com Subject: [PATCH V3 2/9] x86/hyperv: Set Virtual Trust Level in VMBus init message Date: Mon, 17 Jul 2023 23:22:56 -0400 Message-Id: <20230718032304.136888-3-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727580103434109 X-GMAIL-MSGID: 1771727580103434109 From: Tianyu Lan SEV-SNP guests on Hyper-V can run at multiple Virtual Trust Levels (VTL). During boot, get the VTL at which we're running using the GET_VP_REGISTERs hypercall, and save the value for future use. Then during VMBus initialization, set the VTL with the saved value as required in the VMBus init message. Signed-off-by: Tianyu Lan Reviewed-by: Michael Kelley --- * Change since v2: Update the change log. --- arch/x86/hyperv/hv_init.c | 36 ++++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 7 ++++++ drivers/hv/connection.c | 1 + include/asm-generic/mshyperv.h | 1 + include/linux/hyperv.h | 4 ++-- 5 files changed, 47 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 6c04b52f139b..1ba367a9686e 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -378,6 +378,40 @@ static void __init hv_get_partition_id(void) local_irq_restore(flags); } +static u8 __init get_vtl(void) +{ + u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS; + struct hv_get_vp_registers_input *input; + struct hv_get_vp_registers_output *output; + u64 vtl = 0; + u64 ret; + unsigned long flags; + + local_irq_save(flags); + input = *this_cpu_ptr(hyperv_pcpu_input_arg); + output = (struct hv_get_vp_registers_output *)input; + if (!input) { + local_irq_restore(flags); + goto done; + } + + memset(input, 0, struct_size(input, element, 1)); + input->header.partitionid = HV_PARTITION_ID_SELF; + input->header.vpindex = HV_VP_INDEX_SELF; + input->header.inputvtl = 0; + input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS; + + ret = hv_do_hypercall(control, input, output); + if (hv_result_success(ret)) + vtl = output->as64.low & HV_X64_VTL_MASK; + else + pr_err("Hyper-V: failed to get VTL! %lld", ret); + local_irq_restore(flags); + +done: + return vtl; +} + /* * This function is to be invoked early in the boot sequence after the * hypervisor has been detected. @@ -506,6 +540,8 @@ void __init hyperv_init(void) /* Query the VMs extended capability once, so that it can be cached. */ hv_query_ext_cap(0); + /* Find the VTL */ + ms_hyperv.vtl = get_vtl(); return; clean_guest_os_id: diff --git a/arch/x86/include/asm/hyperv-tlfs.h b/arch/x86/include/asm/hyperv-tlfs.h index cea95dcd27c2..4bf0b315b0ce 100644 --- a/arch/x86/include/asm/hyperv-tlfs.h +++ b/arch/x86/include/asm/hyperv-tlfs.h @@ -301,6 +301,13 @@ enum hv_isolation_type { #define HV_X64_MSR_TIME_REF_COUNT HV_REGISTER_TIME_REF_COUNT #define HV_X64_MSR_REFERENCE_TSC HV_REGISTER_REFERENCE_TSC +/* + * Registers are only accessible via HVCALL_GET_VP_REGISTERS hvcall and + * there is not associated MSR address. + */ +#define HV_X64_REGISTER_VSM_VP_STATUS 0x000D0003 +#define HV_X64_VTL_MASK GENMASK(3, 0) + /* Hyper-V memory host visibility */ enum hv_mem_host_visibility { VMBUS_PAGE_NOT_VISIBLE = 0, diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index 5978e9dbc286..02b54f85dc60 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -98,6 +98,7 @@ int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version) */ if (version >= VERSION_WIN10_V5) { msg->msg_sint = VMBUS_MESSAGE_SINT; + msg->msg_vtl = ms_hyperv.vtl; vmbus_connection.msg_conn_id = VMBUS_MESSAGE_CONNECTION_ID_4; } else { msg->interrupt_page = virt_to_phys(vmbus_connection.int_page); diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index 6b5c41f90398..f73a044ecaa7 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -54,6 +54,7 @@ struct ms_hyperv_info { }; }; u64 shared_gpa_boundary; + u8 vtl; }; extern struct ms_hyperv_info ms_hyperv; extern bool hv_nested; diff --git a/include/linux/hyperv.h b/include/linux/hyperv.h index bfbc37ce223b..1f2bfec4abde 100644 --- a/include/linux/hyperv.h +++ b/include/linux/hyperv.h @@ -665,8 +665,8 @@ struct vmbus_channel_initiate_contact { u64 interrupt_page; struct { u8 msg_sint; - u8 padding1[3]; - u32 padding2; + u8 msg_vtl; + u8 reserved[6]; }; }; u64 monitor_page1; From patchwork Tue Jul 18 03:22:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121740 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1481307vqt; Mon, 17 Jul 2023 20:25:40 -0700 (PDT) X-Google-Smtp-Source: APBJJlFkKkno1O+8bAgA61flhGwb7Q0PGjYFWBDM321EFfPG+ZeW2drr1HmgZCPHnE/m7vuBfojc X-Received: by 2002:a17:902:868a:b0:1b8:1335:b775 with SMTP id g10-20020a170902868a00b001b81335b775mr12520130plo.0.1689650740016; Mon, 17 Jul 2023 20:25:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689650740; cv=none; d=google.com; s=arc-20160816; b=I3T+4Zs2vs1ogpoqnoVPdjU1LUNa6tGZc7sWic6aCEzdKmp1vW7XTHFZiR2AXg9nvC q7c/kSeVhs9hB2R2LDV0Y/YRYkppft1mRUsyUXMqRxZo18Gr9QoQMQOEnCr9wiaD69wS j7PWIiMXi02gDGi/F1cffYgZxU1WP/vWBubiwf/xS+4NJVZRfS2FCOE1pqd+St2uIMjE wgtbTXe1mM28XgbpOV+GE/iq0MZBjECCZ2W3ZclyBOS2tbU7XurxztXg5ghT6Tp2I1Pl 36c2eXkBTP9PvPJyxj6jNfqXX5LCTvhjB+c5divla7GjDf2VETg4ErCU58BS70mOol0A 3mpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=hJBdL12ty40lO6lWuXeIofQP11zU97c0ieetMiyqTqA=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=lNc2QYv/w0SdXzlDjvOncPNv9yv3sFpPaSfyboNHKEKgFifvHJ5+uumITFVC9QPVqm DZGis+k4ALuaVhSl70qSeFkCm4KuBrz00AjpJ9hK7zPj/EEEjSVBYTW1cta0Og6iqU5V a5/pjPGq/iFCmh5/Tyjz1nt5hBtHBQCAfBF037GqTYeeHIFRq5bSyRmhnjohOXLYh8nm v12mF59B7wlH8DT0LRKhUw40gxkWbUFjmloiF3Pg1IjP5bNm4v0ZjsYDDzUiRbHQ+QMg PAbJkiXWboxirwyo//Rzf/+orgRhDIuW2jPCnaEr9cEBAs4aEpJr+XOyJG2icy8TsP+z PSww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=G2OpQEpH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y23-20020a17090264d700b001b8c99b9b7asi877065pli.299.2023.07.17.20.25.27; Mon, 17 Jul 2023 20:25:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=G2OpQEpH; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231205AbjGRDXi (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229852AbjGRDXQ (ORCPT ); Mon, 17 Jul 2023 23:23:16 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 071C810DF; Mon, 17 Jul 2023 20:23:15 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-55bac17b442so3946028a12.3; Mon, 17 Jul 2023 20:23:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650594; x=1692242594; 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=hJBdL12ty40lO6lWuXeIofQP11zU97c0ieetMiyqTqA=; b=G2OpQEpHahGv794djr3EpTFtQzRrPzwIkV2s5d4wkJMqkx/f+PTDDHW3TY4l1NQDIm jI1yj+qMvgU5TKcY0hZieVLTTZpFbeabs3vi+xKt9C3OTe/N3Cfas9jLXLR91zBVX568 u2nuFlMlI66+br4R/vF/AeyveRzlPBru1G11xqHPi0WfZsw9DCTrprd98OtUmAZKf4Dv 9+Jg2NvgVztQV8X9B4RpCVzlOzCxTj6hLJT+WpbFVU88a29p7AYQArQqSWeTuEZNLx/4 fT54sRlCRVglV4UoBleyXQffSPm6dJ5YJjPdahGk58dLmajDSc7htUQAFRGV36GWJNaG KKdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650594; x=1692242594; 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=hJBdL12ty40lO6lWuXeIofQP11zU97c0ieetMiyqTqA=; b=DN4jpCQI9pYvHIKlkZQuvF/HgS0cGHBpqpptNYbWHppYR5CyvEIfJGCzOO0pXKoUla L3yLQhoso4UTrhgGzAGEsTihySDhoKvLK+aCejmi8amWfHuCG8K1KvkUIOEQmn6UHFJW P7M2NXeOsyQrYhm5I4BJg/pG/N5X8787+K/tIfH+G4cP9iW6VZo5NjdyHOOaSKMnIKEw sMEiQ9lTNBOrQboGsfTLrRQuWOxjwD71tSwEafr7NQqK4RGDLYWeiM//RUwkfdCiC0hn HHNjYnnYL7fw5CQYydWZAS68uS4HB1KDLkBmCW+RrsWAy7JmFnFVMbyUEtIRwBpKfXEp RHEw== X-Gm-Message-State: ABy/qLZqADaQ7kjcr+gn4xW4uoitAUhO/1we/PRhbqIRqRabT8Mw5/Iq PWZDb7FqZbI+0HDELNvc1tM= X-Received: by 2002:a17:90b:1488:b0:262:f06d:c0fc with SMTP id js8-20020a17090b148800b00262f06dc0fcmr12522774pjb.7.1689650594382; Mon, 17 Jul 2023 20:23:14 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:13 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V3 3/9] x86/hyperv: Mark Hyper-V vp assist page unencrypted in SEV-SNP enlightened guest Date: Mon, 17 Jul 2023 23:22:57 -0400 Message-Id: <20230718032304.136888-4-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727214270572472 X-GMAIL-MSGID: 1771727214270572472 From: Tianyu Lan hv vp assist page needs to be shared between SEV-SNP guest and Hyper-V. So mark the page unencrypted in the SEV-SNP guest. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- arch/x86/hyperv/hv_init.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 1ba367a9686e..b004370d3b01 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -106,8 +107,21 @@ static int hv_cpu_init(unsigned int cpu) * in hv_cpu_die(), otherwise a CPU may not be stopped in the * case of CPU offlining and the VM will hang. */ - if (!*hvp) + if (!*hvp) { *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO); + + /* + * Hyper-V should never specify a VM that is a Confidential + * VM and also running in the root partition. Root partition + * is blocked to run in Confidential VM. So only decrypt assist + * page in non-root partition here. + */ + if (*hvp && hv_isolation_type_en_snp()) { + WARN_ON_ONCE(set_memory_decrypted((unsigned long)(*hvp), 1)); + memset(*hvp, 0, PAGE_SIZE); + } + } + if (*hvp) msr.pfn = vmalloc_to_pfn(*hvp); From patchwork Tue Jul 18 03:22:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121741 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1481375vqt; Mon, 17 Jul 2023 20:25:56 -0700 (PDT) X-Google-Smtp-Source: APBJJlFQG9f8Y41PODHO61sqyqz8m2RDPp01c9AJNLAc8/QlLdtrCrHdxVstqG0vlpCRUCeew/H4 X-Received: by 2002:a17:902:d2c5:b0:1b6:92f0:b6f5 with SMTP id n5-20020a170902d2c500b001b692f0b6f5mr13720208plc.14.1689650756588; Mon, 17 Jul 2023 20:25:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689650756; cv=none; d=google.com; s=arc-20160816; b=D+CWVyAFA+n4FA3Ras9qrxEq5TFrvyVzmQQNjs0n9dVjEo+h7Dr0IUBAISAU6Jqhz3 1KnNTe0t9JHC18D+MMTngH8NLOTiNYmWJWfcWktkKCn7KKHWrCJbe0Eh+R6VT7Nj7U90 Pig/PSoQlHUyLU/HzqRUWZTLlRe5rQFpkuI9aUPqzP4DwCT+1EGAXSKRG4C5u2qo2xgg RFwFzdg9PpWw5tYQRiOk9KVrteeHnrSu+16FwdN0bzyWqpfxPeKIkzc8bfxsBr7YpfYn pHs1ziRVcM3dNS0mK6ZMRWDR2S5YAYOoAxN3UJbYiLSxl86b2Rar3jLQ0o22uPkYBcGf hozQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0mqb14LBzHQf18FY7xszQiIf830qOZYbv6/JECgr54c=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=ngkAN38qL0iVs+iJhqNGyyICCfDmYg6nB1eodbZjSK9aTxZMoR/pmgtE7UxGgW52no eHZX/c5KwQmIxOhBdn3BsBMHwp1TVngcuKmSqQ/XM5YQWQA9n5LsXkNuEXTaZIYFe4ct sHjYGF3oS7YPbLCRViM7DcPbY0t3M6ujrkjygYosdNyejckrHDGJLnDSB88c9c21t18+ AMWiIACXWciIuIshXGdCCuW4cSrMfA9QGdUFVSZiAIpbEP4i1TnSu4fv+2jsDFs1gyXs 1H4z73zgHZpDOXy+m5qR7lCqgzmDfIARZ3VvL2niG/xQhabydEolpheC1co3XIVhOK5E zf9w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=nvzVeuEu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i9-20020a655b89000000b0053fee209655si809326pgr.664.2023.07.17.20.25.43; Mon, 17 Jul 2023 20:25:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=nvzVeuEu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229669AbjGRDXn (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230447AbjGRDXR (ORCPT ); Mon, 17 Jul 2023 23:23:17 -0400 Received: from mail-pg1-x532.google.com (mail-pg1-x532.google.com [IPv6:2607:f8b0:4864:20::532]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B55410E2; Mon, 17 Jul 2023 20:23:16 -0700 (PDT) Received: by mail-pg1-x532.google.com with SMTP id 41be03b00d2f7-55ba5bb0bf3so3199574a12.1; Mon, 17 Jul 2023 20:23:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650596; x=1692242596; 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=0mqb14LBzHQf18FY7xszQiIf830qOZYbv6/JECgr54c=; b=nvzVeuEupIKD66g2NAcLB3TMdCqMRB2Q+kPHig08uUKxEqN5w9Q+0sNRjhLdcnFr4V gVkQs39UCHfUKOxXm6o3q/pR0b2X8qYmrKV8PQosnaRLcH5f0DUZDsT9Fj5tOHxn2w6S yY1jHnsaYzQc4WbuTcwRrQHn9l4ys/Tvil7CwacRWEf4zX91dqXhmeq/inJADEnzxlWm UIV1BZ0fyDcHCnrfieHm85HcDRv5YOUTW4we3xBIIBbE63E7dVngtIAZL5W2QTGNpP3M bw4bH/WsqZPDaeM9tXOt0LbNfHGAsnlvdyn1aqrdflLOjm5SGLwKATfRGyGCOS0C7MTe bAVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650596; x=1692242596; 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=0mqb14LBzHQf18FY7xszQiIf830qOZYbv6/JECgr54c=; b=jNwFkwLYHiIfMSC7gZLQj3Zd2bscBHd5BmLbJcYrncetoEqELKL/Cbiv44KD6LqoPw WagWmU8YHBvNRDqFhNGyFeEA0GD+qLJFira7fD1UugIgRmbtDbpDI/jb05LCPDJhqGs2 HPivDH2Jl67k0v1K+wKDr6LzzPr1TJMIoTt8kcuzWEw7btfzzg5TuCA6KJxnBDQsPUnM M6VoLgVwi7NAYd2NTRPmPERGTBjkB71EfK8VNa4a64w01v8uIVxkvcFseUfICSMXKjGh 0g9AcAEL8NB88cN5+tsoIgzRCqnWNEt7BNJSI7vxa3WxGWe4HejVK9WD2a/23wFWcDiV zluA== X-Gm-Message-State: ABy/qLY7yfkRfZnyUCCoTCMvKz9p7McqgnKw+x6bgawGtG0u47tnDBDw VYXmjZFjtjx9N3TfAxUKV1pnAxum6Rz8cw== X-Received: by 2002:a17:90b:1095:b0:263:3567:f99 with SMTP id gj21-20020a17090b109500b0026335670f99mr14755117pjb.15.1689650595674; Mon, 17 Jul 2023 20:23:15 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:15 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V3 4/9] drivers: hv: Mark percpu hvcall input arg page unencrypted in SEV-SNP enlightened guest Date: Mon, 17 Jul 2023 23:22:58 -0400 Message-Id: <20230718032304.136888-5-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727231533559376 X-GMAIL-MSGID: 1771727231533559376 From: Tianyu Lan Hypervisor needs to access input arg, VMBus synic event and message pages. Mark these pages unencrypted in the SEV-SNP guest and free them only if they have been marked encrypted successfully. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- drivers/hv/hv.c | 57 +++++++++++++++++++++++++++++++++++++++--- drivers/hv/hv_common.c | 13 ++++++++++ 2 files changed, 67 insertions(+), 3 deletions(-) diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index de6708dbe0df..ec6e35a0d9bf 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "hyperv_vmbus.h" /* The one and only */ @@ -78,7 +79,7 @@ int hv_post_message(union hv_connection_id connection_id, int hv_synic_alloc(void) { - int cpu; + int cpu, ret = -ENOMEM; struct hv_per_cpu_context *hv_cpu; /* @@ -123,26 +124,76 @@ int hv_synic_alloc(void) goto err; } } + + if (hv_isolation_type_en_snp()) { + ret = set_memory_decrypted((unsigned long) + hv_cpu->synic_message_page, 1); + if (ret) { + pr_err("Failed to decrypt SYNIC msg page: %d\n", ret); + hv_cpu->synic_message_page = NULL; + + /* + * Free the event page here so that hv_synic_free() + * won't later try to re-encrypt it. + */ + free_page((unsigned long)hv_cpu->synic_event_page); + hv_cpu->synic_event_page = NULL; + goto err; + } + + ret = set_memory_decrypted((unsigned long) + hv_cpu->synic_event_page, 1); + if (ret) { + pr_err("Failed to decrypt SYNIC event page: %d\n", ret); + hv_cpu->synic_event_page = NULL; + goto err; + } + + memset(hv_cpu->synic_message_page, 0, PAGE_SIZE); + memset(hv_cpu->synic_event_page, 0, PAGE_SIZE); + } } return 0; + err: /* * Any memory allocations that succeeded will be freed when * the caller cleans up by calling hv_synic_free() */ - return -ENOMEM; + return ret; } void hv_synic_free(void) { - int cpu; + int cpu, ret; for_each_present_cpu(cpu) { struct hv_per_cpu_context *hv_cpu = per_cpu_ptr(hv_context.cpu_context, cpu); + /* It's better to leak the page if the encryption fails. */ + if (hv_isolation_type_en_snp()) { + if (hv_cpu->synic_message_page) { + ret = set_memory_encrypted((unsigned long) + hv_cpu->synic_message_page, 1); + if (ret) { + pr_err("Failed to encrypt SYNIC msg page: %d\n", ret); + hv_cpu->synic_message_page = NULL; + } + } + + if (hv_cpu->synic_event_page) { + ret = set_memory_encrypted((unsigned long) + hv_cpu->synic_event_page, 1); + if (ret) { + pr_err("Failed to encrypt SYNIC event page: %d\n", ret); + hv_cpu->synic_event_page = NULL; + } + } + } + free_page((unsigned long)hv_cpu->synic_event_page); free_page((unsigned long)hv_cpu->synic_message_page); } diff --git a/drivers/hv/hv_common.c b/drivers/hv/hv_common.c index 4b4aa53c34c2..2d43ba2bc925 100644 --- a/drivers/hv/hv_common.c +++ b/drivers/hv/hv_common.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -359,6 +360,7 @@ int hv_common_cpu_init(unsigned int cpu) u64 msr_vp_index; gfp_t flags; int pgcount = hv_root_partition ? 2 : 1; + int ret; /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL; @@ -378,6 +380,17 @@ int hv_common_cpu_init(unsigned int cpu) outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg); *outputarg = (char *)(*inputarg) + HV_HYP_PAGE_SIZE; } + + if (hv_isolation_type_en_snp()) { + ret = set_memory_decrypted((unsigned long)*inputarg, pgcount); + if (ret) { + kfree(*inputarg); + *inputarg = NULL; + return ret; + } + + memset(*inputarg, 0x00, pgcount * PAGE_SIZE); + } } msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX); From patchwork Tue Jul 18 03:22:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121750 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1484347vqt; Mon, 17 Jul 2023 20:36:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlGtjvTF/XM1MjtmIvhAR1MLubKZxwSaprbUrB3XuepkgB7phDgkCf5DF0uO4uxqia7lTm65 X-Received: by 2002:a17:902:7c14:b0:1b8:3786:334d with SMTP id x20-20020a1709027c1400b001b83786334dmr3083191pll.18.1689651370078; Mon, 17 Jul 2023 20:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651370; cv=none; d=google.com; s=arc-20160816; b=hJaDzV4qgBfPnmlyY/0omTK4AIaOGwaPESbRfa/UxYuFPV/k23ERgIdLVMpgmfOzSx Y013gMYEENBFPT1IbNxu9zBSA7hwFpuivQKoQmzh2Up9bdzXpyRFqypFLZ/b6rWRdMCW /r6ZJheSZjkJu8Y1R3ahl5BAWarkh7mxSvGi18IoNvZAVWbRZefe9pMmubbXBfktkUzI +jUwP7VLNHKRaUGc7zOMcxqVMPU9zHFZ4gyZw5caBSC9wKRJGQEiaWA5MYyDQWQdYLgJ 2Q7+nOiZQyT7r8aE28bzQuKmwcm9zfo+upT2gCxsbkCTYnfT+HCW/AF9pxne4thMaeUu cBQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lGDTbc8t+NaU1OP3xKzemfdpSc0H/y8ZWu0f/3/zAAY=; fh=0q4Ral/ZvWHifErBr5uVVFNUVQsTG1/TVmQRaCWii40=; b=SBpG94kz+yoillyddfeZFcewBP10VjSTRrmKRvI3hOFN0Y7eX4giMOmSn8pg7v0UF7 W/d7L0s4zs8xvlg+RsSJpek0XIhK6XLL37N+vNMxlM6JsvYsNKVA2pMo6LSy+jNM52Em KM0TnhHibuJ2SRc58DUxhqHaX7EaTSC9Mcssv4ZKivJTjJ3L746iF9+IG6uP5Ayp307d WhIBO5XGSN+mVQSlFUHaILrcIRbAMm/ZDhRmaJQX2XQvJ6kMUXoP/U1uDSlX0siHJqE3 XFhKhhFUFcd7XmKyUYHh2hVz2eVtq+PJA+6KJNHkO9OvUiozmS3TgdHlkdk6lRy8AL2q r9Ww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=pt2jGLoF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a15-20020a170902710f00b001bb053dc952si919686pll.147.2023.07.17.20.35.56; Mon, 17 Jul 2023 20:36:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=pt2jGLoF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231221AbjGRDXw (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230306AbjGRDXa (ORCPT ); Mon, 17 Jul 2023 23:23:30 -0400 Received: from mail-ot1-x333.google.com (mail-ot1-x333.google.com [IPv6:2607:f8b0:4864:20::333]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0B14310F0; Mon, 17 Jul 2023 20:23:18 -0700 (PDT) Received: by mail-ot1-x333.google.com with SMTP id 46e09a7af769-6b9c942eb18so2219974a34.3; Mon, 17 Jul 2023 20:23:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650598; x=1692242598; 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=lGDTbc8t+NaU1OP3xKzemfdpSc0H/y8ZWu0f/3/zAAY=; b=pt2jGLoFHP0lMXD+QOuJ/WB4g5pOP4OGFpgO8L8I+Ro2vkMGheu2h3qOi9xX5VINxq 130pK6ZDVMkJf8TJpTzgSTXkQpDxs90FVCh6JWKxG4OHmHdMmCifl40yk1TB3Yqk7JlY rYSHwOuFLqe+HBNWWD3IhmwqoIqO0mMppXOtV6zkX5drNZ3h1N+ddz6L+N36JiZUvyuI LUC6NYHUf9a4Op9rwdTaHt5K1R+DDbkCF5+ZYZjuR1pxhkGxZjEU2gKIXASFzWU0mTnJ 2PXwq9uFlECDjceWTOMFkc3gqTOw5hDG5xUD544tDLMKL4EaEquaR+T/QYjH01DHYUWT cBiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650598; x=1692242598; 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=lGDTbc8t+NaU1OP3xKzemfdpSc0H/y8ZWu0f/3/zAAY=; b=Y18CwSB5u7UBstmTMgM+HnRbXige3GVsNtDzXITcev05jyWHvnUzIVOV+rjYQ2twew OlW/VfZP/Sw7rqQLKgiCJvE9yCjEqSfaixwSTYXri0W8No1rPTsh+KEmzgSDQ03QQ8do 7vR0UArWxOgwiifA1lbaRHygeu7eg0BKUeu73Rx9yjMMa1SWOuPLSCyukRgH7HR15ZYC nxZazAC1CJiS67LCNk/6BTV4M8pM1tdidFTd0xsgJJGdSuzNPTTjbCAcftfbZjGssXhB MM5cIN72FjkUh2pL0/TragMlPyA6yfmMooOyA6mD+5ocggWR/rJHx5OIqkAprTS6WUcY HXOw== X-Gm-Message-State: ABy/qLbcO120yknKNDxwMW8MMn1r7pJOTBk5bl45rzTcb10bMJdneqbB U1AHuzSMMkEfV+hsYkm2VZgGofRrntLNjg== X-Received: by 2002:a05:6358:2815:b0:134:d559:259a with SMTP id k21-20020a056358281500b00134d559259amr14262083rwb.17.1689650597056; Mon, 17 Jul 2023 20:23:17 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:16 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com Subject: [PATCH V3 5/9] x86/hyperv: Use vmmcall to implement Hyper-V hypercall in sev-snp enlightened guest Date: Mon, 17 Jul 2023 23:22:59 -0400 Message-Id: <20230718032304.136888-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727875427423666 X-GMAIL-MSGID: 1771727875427423666 From: Tianyu Lan In sev-snp enlightened guest, Hyper-V hypercall needs to use vmmcall to trigger vmexit and notify hypervisor to handle hypercall request. Signed-off-by: Tianyu Lan --- arch/x86/include/asm/mshyperv.h | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index 2fa38e9f6207..025eda129d99 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -64,12 +64,12 @@ static inline u64 hv_do_hypercall(u64 control, void *input, void *output) if (!hv_hypercall_pg) return U64_MAX; - __asm__ __volatile__("mov %4, %%r8\n" - CALL_NOSPEC + __asm__ __volatile__("mov %[output], %%r8\n" + ALTERNATIVE("vmmcall", CALL_NOSPEC, X86_FEATURE_SEV_ES) : "=a" (hv_status), ASM_CALL_CONSTRAINT, - "+c" (control), "+d" (input_address) - : "r" (output_address), - THUNK_TARGET(hv_hypercall_pg) + "+c" (control), "+d" (input_address) + : [output] "r" (output_address), + THUNK_TARGET(hv_hypercall_pg) : "cc", "memory", "r8", "r9", "r10", "r11"); #else u32 input_address_hi = upper_32_bits(input_address); @@ -105,7 +105,8 @@ static inline u64 _hv_do_fast_hypercall8(u64 control, u64 input1) #ifdef CONFIG_X86_64 { - __asm__ __volatile__(CALL_NOSPEC + __asm__ __volatile__("mov %[thunk_target], %%r8\n" + ALTERNATIVE("vmmcall", CALL_NOSPEC, X86_FEATURE_SEV_ES) : "=a" (hv_status), ASM_CALL_CONSTRAINT, "+c" (control), "+d" (input1) : THUNK_TARGET(hv_hypercall_pg) @@ -150,13 +151,13 @@ static inline u64 _hv_do_fast_hypercall16(u64 control, u64 input1, u64 input2) #ifdef CONFIG_X86_64 { - __asm__ __volatile__("mov %4, %%r8\n" - CALL_NOSPEC - : "=a" (hv_status), ASM_CALL_CONSTRAINT, - "+c" (control), "+d" (input1) - : "r" (input2), - THUNK_TARGET(hv_hypercall_pg) - : "cc", "r8", "r9", "r10", "r11"); + __asm__ __volatile__("mov %[output], %%r8\n" + ALTERNATIVE("vmmcall", CALL_NOSPEC, X86_FEATURE_SEV_ES) + : "=a" (hv_status), ASM_CALL_CONSTRAINT, + "+c" (control), "+d" (input1) + : [output] "r" (input2), + THUNK_TARGET(hv_hypercall_pg) + : "cc", "r8", "r9", "r10", "r11"); } #else { From patchwork Tue Jul 18 03:23:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121747 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1483504vqt; Mon, 17 Jul 2023 20:33:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlEhw0lyMci2fcpnUAFErf33/+kB0fpxCxGdBKwTT95TAv2kcDFZXmUHOQ4zGaSkZ0hVCHtg X-Received: by 2002:a17:90a:c90d:b0:25e:ad19:5f46 with SMTP id v13-20020a17090ac90d00b0025ead195f46mr11000069pjt.12.1689651182526; Mon, 17 Jul 2023 20:33:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651182; cv=none; d=google.com; s=arc-20160816; b=lyY+duN3a6UHGEAqHp5u7IkpN5LW6THrCU43gCm3sSukA7mnIpkgy6nG6gPoG8duwN nFK98RVwj0w943wGncwUPRLGnmpGVGH9IGyLezEP2RgltUAE8kuQ4BMVArH3Bo4hoIeu e4UkFe6D+2AwjiPDYvlm6qbsS3sunLtuz8swlrRrajYpqvRtIaWYhn0Lf94AQ9NzsJMl XS3AUTmNLQBF6qQOphAkgjd/WCzAhiI+AuQCRvz44GIi19R+tN9J70xOVu9PchDOZQhT ugWqgnwqricYzMmtEDJd2McC1m03OORfChNLpmC23cQL3yIaZkFQFxW+1dXmflzw1Wff 5F6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=lxyPRx65GPTmM7WIGpMjts/akUWiTXqh+ZFAndUPdXI=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=mZOlfz69uENiZ3yEyStUxkWh98BJeGICxJAIfRbBr28efXuUUtl2CCnrOyigciSdRE 6uQo7m9rIgRiPSGHbX+DhU382Xi4+lpRZEh5/PcF29t1qjml2AYMmU3fwuayId7A1gn8 zQ5oGVuZ6pfaZVayfQ2TE98mKuAXwIRH1vAFmxf1pxnqHcGVJjGOhxX9rXaG7KrXB9ED RnRjGO6OGon6zwkncFYF/0k1ND+nu4gDpdfxlBZL1puv+ssQ4g4xczP7RAdkBa/lQzT3 F0HckO+NXCC7Ti3KdgaS0r8eIYCO/WPMtkta0+5gcxqqedYGLntSKTmsG2+8Thl2MKop uxtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="Z9ih0aA/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r124-20020a632b82000000b0055785a37147si799716pgr.590.2023.07.17.20.32.49; Mon, 17 Jul 2023 20:33:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="Z9ih0aA/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231142AbjGRDX4 (ORCPT + 99 others); Mon, 17 Jul 2023 23:23:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32916 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231156AbjGRDXa (ORCPT ); Mon, 17 Jul 2023 23:23:30 -0400 Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5BF9E10F3; Mon, 17 Jul 2023 20:23:19 -0700 (PDT) Received: by mail-ot1-x32a.google.com with SMTP id 46e09a7af769-6b91ad1f9c1so3467040a34.3; Mon, 17 Jul 2023 20:23:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650598; x=1692242598; 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=lxyPRx65GPTmM7WIGpMjts/akUWiTXqh+ZFAndUPdXI=; b=Z9ih0aA/PzLKRelAHQmbTO80qK68YQO4IevqyDH0xG02u8MrrOrsvshZKQeV0bfe+J UYc4pemypZweDyZngoa8pcj2wriAWBzCJbJcEi1O9+UYZolqFz3hmu0RWRNpy/7gBjpC oJRazKaJ/RoJwllA01phZbgTbLaQ77BQOkl/q21B6gklmc4kpzqfJiHw+Amvy//ewZgZ /+v66iPHoPjyhLLF16YfSTuQ1Mtf6mVsVquwleWz0+E5C/IAFF276MPcPnpTMEPLPQGx llkuQfYaNk3eZkAr/4X47GWex904s6gtfGqpc5eqKdmivaTkyT0f7XlqnceMJKT1j7ym +mwA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650598; x=1692242598; 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=lxyPRx65GPTmM7WIGpMjts/akUWiTXqh+ZFAndUPdXI=; b=cZmRmmsxWIFd5yDxoVSCu1SYuABtYD6aZF7jFXSMH1z7bukA/FUgCWOwPFhLmy4IT4 9SIEcK1M3880LVmoa3nL4L0weMO3oXX/p5sccPoBkBR/z9fL6L82jXCxQ52koZogjOId /fbBExiA6PN3RsgathqWKFYcAezpkxHjq7MrAlb4Xya5PTsJNeTwboO86lclJfVgNwf2 gSEMcmcjXdyQqmBnYP18mWAEAnovnl/VLCnbFbqJYaWo1isRisvuJFYD4gIsWwpDlw6j A9dyN6w82p6vyLmKWN9FDCK6OExjI8vu72CSNtHCDH9W0Krx2tsks+bg0Bvk588AvrDt G7yA== X-Gm-Message-State: ABy/qLZjTJRn1Nbt7EkLqK7tnd44k9Pg0bqsfatkoY9D5nNIFVtH2eC3 8oZoRX/YaO04bIlIeG0CKkA= X-Received: by 2002:a05:6808:148d:b0:398:34da:daad with SMTP id e13-20020a056808148d00b0039834dadaadmr15016242oiw.51.1689650598402; Mon, 17 Jul 2023 20:23:18 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:18 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V3 6/9] clocksource: hyper-v: Mark hyperv tsc page unencrypted in sev-snp enlightened guest Date: Mon, 17 Jul 2023 23:23:00 -0400 Message-Id: <20230718032304.136888-7-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727678296777964 X-GMAIL-MSGID: 1771727678296777964 From: Tianyu Lan Hyper-V tsc page is shared with hypervisor and mark the page unencrypted in sev-snp enlightened guest when it's used. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- drivers/clocksource/hyperv_timer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/clocksource/hyperv_timer.c b/drivers/clocksource/hyperv_timer.c index e56307a81f4d..8ff7cd4e20bb 100644 --- a/drivers/clocksource/hyperv_timer.c +++ b/drivers/clocksource/hyperv_timer.c @@ -390,7 +390,7 @@ static __always_inline u64 read_hv_clock_msr(void) static union { struct ms_hyperv_tsc_page page; u8 reserved[PAGE_SIZE]; -} tsc_pg __aligned(PAGE_SIZE); +} tsc_pg __bss_decrypted __aligned(PAGE_SIZE); static struct ms_hyperv_tsc_page *tsc_page = &tsc_pg.page; static unsigned long tsc_pfn; From patchwork Tue Jul 18 03:23:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121743 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1482734vqt; Mon, 17 Jul 2023 20:30:38 -0700 (PDT) X-Google-Smtp-Source: APBJJlGgkt5RjGcFIJSGXQzVFDG9AzamMLtOSYlTlxKfNDrI9iptSVS5nOd1xaO7ijwfledpxoJM X-Received: by 2002:a05:6a21:999f:b0:126:af02:444e with SMTP id ve31-20020a056a21999f00b00126af02444emr18529244pzb.8.1689651038486; Mon, 17 Jul 2023 20:30:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651038; cv=none; d=google.com; s=arc-20160816; b=hGmbbP9Cuj/gk3GOEesyCQXlZkGrsHLgDIDSgpky/7A/p3+NoaaCQMNosawb5iomU8 h+BFNyvK7EkkFHDDId4lAabk3fBiWOCw3rW4MPGnM6hRje+q89RO1Nc/RE9L+/5KurNR k8g7NvSFqsynZC/A1zk2biymuZFzkMB2yCKrjGOVi1+y5WkuyPs37txw63aJA9gOmqnE LtaRM4uCIh2WndMJHIr0o0P9zFqcv8raf3Gs1oZT0aehbb+n3YY52bTnsfbVYGIMm8DC /RUvf8jr0IQg0Y78uDfczqB+axhyyszpH+P35gvlP0wJqm56djcitzLP/SuA0r9+g8kg BXNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=vTr/U5pjToUGm5/dOth80Sxi8/cWirKNMTqOirinpDc=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=HaWPwCN9ygnFC8z0PWlE56M+DeCkycdUW5o8VVsaW0M+QNH0/vYFgs9k7eYEfDiHEx 36RbgQVDb+/duj4YzAFMatvzEtclZGhGbWPwEGxwazjRltUmdRkCq4ZfMXVZQn91SR1G r9fKm6LIR5IC2A6+FCCz0EnW2aSQc+v7Y5nTH/fA72ENyaqEgHiiRsDDfW8+zHAWcLTN Zuk4aPr4BLlnuzYlHrzmtTVtonkWjhUQszqwXtCMf9Am/sjfUKxVyx9C0/4pbIKOVGfL L9Ni6GX98m6NURsa1xngZ+K8T4D07Jp+yv1u6hDIIgmqDS7tsvrrNOY9NZNvlrMA9p68 8juw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="Sa0g/M0N"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id o1-20020a170903300100b001b665d8d63asi902854pla.356.2023.07.17.20.30.25; Mon, 17 Jul 2023 20:30:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b="Sa0g/M0N"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230013AbjGRDYL (ORCPT + 99 others); Mon, 17 Jul 2023 23:24:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33220 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231199AbjGRDXh (ORCPT ); Mon, 17 Jul 2023 23:23:37 -0400 Received: from mail-oi1-x235.google.com (mail-oi1-x235.google.com [IPv6:2607:f8b0:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1F3E7170B; Mon, 17 Jul 2023 20:23:20 -0700 (PDT) Received: by mail-oi1-x235.google.com with SMTP id 5614622812f47-3a3c78ede4bso3912688b6e.2; Mon, 17 Jul 2023 20:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650600; x=1692242600; 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=vTr/U5pjToUGm5/dOth80Sxi8/cWirKNMTqOirinpDc=; b=Sa0g/M0N0nnZVlCVfBTBHOApjjhUK3i/Qmow17A913aTK3wKo/PdVrsvd2AzfA/+rL Z2wvQgwyTvoNWQWGXjvnII6VbYGq8WCcaI402x3vHL/jmNFYjSSTefNgfj9bnbMNAHVP aRbfXhDLkEcPxCpShrUxkr1E+MjeqyEBB7+upJSvF7DcykXHlUB/OYe/gb6a4JFBvmZT Kakm6gcY4SG37IeVRbJfTr6xzKBbarj6Ze/G+MUlQF3kgFVHLylGmyBvC2EocU4O6hgi t7mOZ8crCqjeEA5Nxb/wf80ZISIixWOqcMoyh0HI2dsfZhN+6OQVzEBvtckmVP82UlLw +CFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650600; x=1692242600; 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=vTr/U5pjToUGm5/dOth80Sxi8/cWirKNMTqOirinpDc=; b=JNPNFLP4XrmEo3r2OIOKK9KuBNXkXrbb/yHvyt4OM8wNeJJ+YqLlRNE/E1dap2DXX+ uyUxlWTFYnca3/jCB/XWA3Khdez3X+61k2XajZgMbfVuhHl51jU9vEj6899ye5BVHEB4 +k5HWCqpH5nAv6wyh5eoF9Jkj1mX8ZPaAEvx/how8OVTh4m3hguobUA4NXi5GsplJnRL M7ClnQ8yieWzOtT/kICuP1xykXZQSvCdzHWXTp6drxChZDLtm1ck/O5B+kqDYz9gyocB h9dCa0EbE/8GfZhJkYPDpfDOeMMPm6Jy0BUugk0I+cmB4FLv7fWKb7u0pfzQCdx7ScL0 3FZg== X-Gm-Message-State: ABy/qLbJcP3PQKhJffgYsddLK4PYk5YT9BZqgVBv7x08yZESPWjVacef xVviAvSiwcDjkFewBENKluYEASeMN2bFaA== X-Received: by 2002:a05:6808:1384:b0:3a4:1f76:bdfb with SMTP id c4-20020a056808138400b003a41f76bdfbmr15725583oiw.14.1689650600108; Mon, 17 Jul 2023 20:23:20 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:19 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V3 7/9] x86/hyperv: Initialize cpu and memory for SEV-SNP enlightened guest Date: Mon, 17 Jul 2023 23:23:01 -0400 Message-Id: <20230718032304.136888-8-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727527195831366 X-GMAIL-MSGID: 1771727527195831366 From: Tianyu Lan Hyper-V enlightened guest doesn't have boot loader support. Boot Linux kernel directly from hypervisor with data (kernel image, initrd and parameter page) and memory for boot up that is initialized via AMD SEV PSP protocol (Please reference Section 4.5 Launching a Guest of [1]). Kernel needs to read processor and memory info from EN_SEV_ SNP_PROCESSOR/MEM_INFO_ADDR address which are populated by Hyper-V. The data is prepared by hypervisor via SNP_ LAUNCH_UPDATE with page type SNP_PAGE_TYPE_UNMEASURED and Initialize smp cpu related ops, validate system memory and add them into e820 table. [1]: https://www.amd.com/system/files/TechDocs/56860.pdf Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- Change since v2: * Update change log. --- arch/x86/hyperv/ivm.c | 93 +++++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 17 ++++++ arch/x86/kernel/cpu/mshyperv.c | 3 ++ 3 files changed, 113 insertions(+) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index b2b5cb19fac9..ede47c8264e0 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -18,6 +18,11 @@ #include #include #include +#include +#include +#include +#include +#include #ifdef CONFIG_AMD_MEM_ENCRYPT @@ -58,6 +63,8 @@ union hv_ghcb { static u16 hv_ghcb_version __ro_after_init; +static u32 processor_count; + u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size) { union hv_ghcb *hv_ghcb; @@ -357,6 +364,92 @@ static bool hv_is_private_mmio(u64 addr) return false; } +static __init void hv_snp_get_smp_config(unsigned int early) +{ + /* + * The "early" parameter can be true only if old-style AMD + * Opteron NUMA detection is enabled, which should never be + * the case for an SEV-SNP guest. See CONFIG_AMD_NUMA. + * For safety, just do nothing if "early" is true. + */ + if (early) + return; + + /* + * There is no firmware and ACPI MADT table support in + * in the Hyper-V SEV-SNP enlightened guest. Set smp + * related config variable here. + */ + while (num_processors < processor_count) { + early_per_cpu(x86_cpu_to_apicid, num_processors) = num_processors; + early_per_cpu(x86_bios_cpu_apicid, num_processors) = num_processors; + physid_set(num_processors, phys_cpu_present_map); + set_cpu_possible(num_processors, true); + set_cpu_present(num_processors, true); + num_processors++; + } +} + +__init void hv_sev_init_mem_and_cpu(void) +{ + struct memory_map_entry *entry; + struct e820_entry *e820_entry; + u64 e820_end; + u64 ram_end; + u64 page; + + /* + * Hyper-V enlightened snp guest boots kernel + * directly without bootloader. So roms, bios + * regions and reserve resources are not available. + * Set these callback to NULL. + */ + x86_platform.legacy.rtc = 0; + x86_platform.legacy.reserve_bios_regions = 0; + x86_platform.set_wallclock = set_rtc_noop; + x86_platform.get_wallclock = get_rtc_noop; + x86_init.resources.probe_roms = x86_init_noop; + x86_init.resources.reserve_resources = x86_init_noop; + x86_init.mpparse.find_smp_config = x86_init_noop; + x86_init.mpparse.get_smp_config = hv_snp_get_smp_config; + + /* + * Hyper-V SEV-SNP enlightened guest doesn't support ioapic + * and legacy APIC page read/write. Switch to hv apic here. + */ + disable_ioapic_support(); + + /* Get processor and mem info. */ + processor_count = *(u32 *)__va(EN_SEV_SNP_PROCESSOR_INFO_ADDR); + entry = (struct memory_map_entry *)__va(EN_SEV_SNP_MEM_INFO_ADDR); + + /* + * There is no bootloader/EFI firmware in the SEV SNP guest. + * E820 table in the memory just describes memory for kernel, + * ACPI table, cmdline, boot params and ramdisk. The dynamic + * data(e.g, vcpu number and the rest memory layout) needs to + * be read from EN_SEV_SNP_PROCESSOR_INFO_ADDR. + */ + for (; entry->numpages != 0; entry++) { + e820_entry = &e820_table->entries[ + e820_table->nr_entries - 1]; + e820_end = e820_entry->addr + e820_entry->size; + ram_end = (entry->starting_gpn + + entry->numpages) * PAGE_SIZE; + + if (e820_end < entry->starting_gpn * PAGE_SIZE) + e820_end = entry->starting_gpn * PAGE_SIZE; + + if (e820_end < ram_end) { + pr_info("Hyper-V: add e820 entry [mem %#018Lx-%#018Lx]\n", e820_end, ram_end - 1); + e820__range_add(e820_end, ram_end - e820_end, + E820_TYPE_RAM); + for (page = e820_end; page < ram_end; page += PAGE_SIZE) + pvalidate((unsigned long)__va(page), RMP_PG_SIZE_4K, true); + } + } +} + void __init hv_vtom_init(void) { /* diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index 025eda129d99..e57df590846a 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -50,6 +50,21 @@ extern bool hv_isolation_type_en_snp(void); extern union hv_ghcb * __percpu *hv_ghcb_pg; +/* + * Hyper-V puts processor and memory layout info + * to this address in SEV-SNP enlightened guest. + */ +#define EN_SEV_SNP_PROCESSOR_INFO_ADDR 0x802000 +#define EN_SEV_SNP_MEM_INFO_ADDR 0x802018 + +struct memory_map_entry { + u64 starting_gpn; + u64 numpages; + u16 type; + u16 flags; + u32 reserved; +}; + int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); @@ -234,12 +249,14 @@ void hv_ghcb_msr_read(u64 msr, u64 *value); bool hv_ghcb_negotiate_protocol(void); void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason); void hv_vtom_init(void); +void hv_sev_init_mem_and_cpu(void); #else static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} static inline bool hv_ghcb_negotiate_protocol(void) { return false; } static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {} static inline void hv_vtom_init(void) {} +static inline void hv_sev_init_mem_and_cpu(void) {} #endif extern bool hv_isolation_type_snp(void); diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 5398fb2f4d39..d3bb921ee7fe 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -529,6 +529,9 @@ static void __init ms_hyperv_init_platform(void) if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT)) mark_tsc_unstable("running on Hyper-V"); + if (hv_isolation_type_en_snp()) + hv_sev_init_mem_and_cpu(); + hardlockup_detector_disable(); } From patchwork Tue Jul 18 03:23:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121748 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1483537vqt; Mon, 17 Jul 2023 20:33:10 -0700 (PDT) X-Google-Smtp-Source: APBJJlGxh2FooSl0HyF0KzII5IeLfyeLHyUmhXpLVbIia8SIo6mxmG5MYQO45a6WuBNmc3wUthUz X-Received: by 2002:a17:902:f547:b0:1b8:3cb8:7926 with SMTP id h7-20020a170902f54700b001b83cb87926mr16462203plf.23.1689651190360; Mon, 17 Jul 2023 20:33:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651190; cv=none; d=google.com; s=arc-20160816; b=dNKTwof7zEJelhwrNRRSHPqqQPOW7AsO76jfLjfd9buQ+r1pY0qsSfxUqLnqYHZ3sN peFWeEKVWdr+Ve8xoZmK0vgZkPSYgc/YuREdsY5S2NomFbYHJLzGE0CU8WOGhlGk3ovf zjJr0cvfg3Ac/LCPkRoRpTtG94MN+rCMRgmbHybpROJEaeNkjItsOLTZyhb6NAs2h5hj BZexmN36tPssfZzJ1HHxZr2MW5iZpXzQJIld+KHiXKZWCWmtFIGksH+ydAnl5rxR+i6R X/fwz2pLG6BqRDT0K5E8zNEjH0LTidRb16SjTmqbblk6OQnk7zKxeCoushMybSePrYtL BURA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=bSMrDEwxkhYrlz2QgyYImrZKuicJf1LqTrgFv+twk64=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=nxtm/1su7oyARcoi36PVIyN654GgJyir6L0y7VSYrF45eICVdv1gHg2uFNmX5ONOK3 1T2KgDiR3YTnTQ+dmmAfSpjRcqKSYuBblLCWFWIAZZjqc+4F3FQXKtrxzNqmcIxk2Gif gECgqPMXv6mvvAO6YGUu4tTkUFP3UNhnUVpKV3HCPwzESncd4BelJ7KEyRPYEkg5NAWt ukpy5KrQF8FVeq122/FSJnwc1ZLRxzvhdANEPasZKywWz99LO8qzrCAZw5bkd+Xt76+p ecx8BSLdiM44yOYJ0isH49V8gUQPtsEV8rJhrAGHSR6ghapg+sAfk4Km54iqWZTiPSt+ nPGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=PIBfVH4l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u17-20020a170902e5d100b001b9e3a18270si943274plf.420.2023.07.17.20.32.57; Mon, 17 Jul 2023 20:33:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=PIBfVH4l; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231270AbjGRDYT (ORCPT + 99 others); Mon, 17 Jul 2023 23:24:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33120 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231207AbjGRDXq (ORCPT ); Mon, 17 Jul 2023 23:23:46 -0400 Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 85B78171B; Mon, 17 Jul 2023 20:23:22 -0700 (PDT) Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-5634d8d1db0so3225640eaf.0; Mon, 17 Jul 2023 20:23:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650601; x=1692242601; 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=bSMrDEwxkhYrlz2QgyYImrZKuicJf1LqTrgFv+twk64=; b=PIBfVH4lx4su5LTNvllbFqSeHldg6NPwn0x1M9u78QkqGGgoCrJUuTKhbiBmLanV2u Z6xU5Pah2k+8BJYh88dh+wAN5FeQVK2yMK3ASi4KlH7n4MBBYfhamIbC7/r28vT6nA7J 5jp1p+JbC++/p0TmzVTUgMZjPBDrqLtHdNxHDtp15n07Mr0aHFcEmQJ3pyP9Ix23UaLK bKb7Lj0d5OzVPAZFgIAX9QY+bTDQhVOl1hQfesyeh+hbIuXTry1LL4dqKxoA3hgcbp+g k/nlAPoHdJV85JEbUhnJDVouKVzQ0DrB9y8oX8+5hbyytEhtfaGUqfRtso6uk4+zBoZP gzEQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650601; x=1692242601; 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=bSMrDEwxkhYrlz2QgyYImrZKuicJf1LqTrgFv+twk64=; b=faVJr7d9uCOdCZ1tKVViy7AhCFDO56PEhhwdgaiazlpzlpbJykg5p9Vl+bzAcxJm60 IPFm2hFmEdC1tYRi5elkOBLHFI64ylnHKUydaR634J2wFddvM5tlUrpWlJHtQmszacbo M2C2WWE6UMPvK5YW/UQtFPZCBAd870RWoMdxh606VXVh21/MHOAiZ8I0tU5kb48y0bRB TCJ7Dny5pXILhivwRKG/2IlkxYM2R7DElcD7/sYx37yhMl8rgRlH18GqXUbetrySGwnO gsL20m88pKrejKYtOC6JOeRQ2DePocn2Mvy7SHHLyRW/I9zdB9ZgLvK79d+wr0HbR7Jd wU7g== X-Gm-Message-State: ABy/qLYaYVoeDwdR/5V0nh6/0J1vVuwxtlJ6nedlZd93MycVdoFgZKP2 ROW5fta73DUVI08IKeC0hUg= X-Received: by 2002:aca:bb0b:0:b0:3a3:78dc:8c4c with SMTP id l11-20020acabb0b000000b003a378dc8c4cmr12856863oif.46.1689650601176; Mon, 17 Jul 2023 20:23:21 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:20 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V3 8/9] x86/hyperv: Add smp support for SEV-SNP guest Date: Mon, 17 Jul 2023 23:23:02 -0400 Message-Id: <20230718032304.136888-9-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727686664214474 X-GMAIL-MSGID: 1771727686664214474 From: Tianyu Lan In the AMD SEV-SNP guest, AP needs to be started up via sev es save area and Hyper-V requires to call HVCALL_START_VP hypercall to pass the gpa of sev es save area with AP's vp index and VTL(Virtual trust level) parameters. Override wakeup_secondary_cpu_64 callback with hv_snp_boot_ap. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- arch/x86/hyperv/ivm.c | 95 +++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 9 +++ arch/x86/kernel/cpu/mshyperv.c | 13 ++++- include/asm-generic/hyperv-tlfs.h | 1 + 4 files changed, 116 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index ede47c8264e0..2eda4e69849d 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -23,11 +23,15 @@ #include #include #include +#include #ifdef CONFIG_AMD_MEM_ENCRYPT #define GHCB_USAGE_HYPERV_CALL 1 +static u8 ap_start_input_arg[PAGE_SIZE] __bss_decrypted __aligned(PAGE_SIZE); +static u8 ap_start_stack[PAGE_SIZE] __aligned(PAGE_SIZE); + union hv_ghcb { struct ghcb ghcb; struct { @@ -450,6 +454,97 @@ __init void hv_sev_init_mem_and_cpu(void) } } +#define hv_populate_vmcb_seg(seg, gdtr_base) \ +do { \ + if (seg.selector) { \ + seg.base = 0; \ + seg.limit = HV_AP_SEGMENT_LIMIT; \ + seg.attrib = *(u16 *)(gdtr_base + seg.selector + 5); \ + seg.attrib = (seg.attrib & 0xFF) | ((seg.attrib >> 4) & 0xF00); \ + } \ +} while (0) \ + +int hv_snp_boot_ap(int cpu, unsigned long start_ip) +{ + struct sev_es_save_area *vmsa = (struct sev_es_save_area *) + __get_free_page(GFP_KERNEL | __GFP_ZERO); + struct desc_ptr gdtr; + u64 ret, rmp_adjust, retry = 5; + struct hv_enable_vp_vtl *start_vp_input; + unsigned long flags; + + native_store_gdt(&gdtr); + + vmsa->gdtr.base = gdtr.address; + vmsa->gdtr.limit = gdtr.size; + + asm volatile("movl %%es, %%eax;" : "=a" (vmsa->es.selector)); + hv_populate_vmcb_seg(vmsa->es, vmsa->gdtr.base); + + asm volatile("movl %%cs, %%eax;" : "=a" (vmsa->cs.selector)); + hv_populate_vmcb_seg(vmsa->cs, vmsa->gdtr.base); + + asm volatile("movl %%ss, %%eax;" : "=a" (vmsa->ss.selector)); + hv_populate_vmcb_seg(vmsa->ss, vmsa->gdtr.base); + + asm volatile("movl %%ds, %%eax;" : "=a" (vmsa->ds.selector)); + hv_populate_vmcb_seg(vmsa->ds, vmsa->gdtr.base); + + vmsa->efer = native_read_msr(MSR_EFER); + + asm volatile("movq %%cr4, %%rax;" : "=a" (vmsa->cr4)); + asm volatile("movq %%cr3, %%rax;" : "=a" (vmsa->cr3)); + asm volatile("movq %%cr0, %%rax;" : "=a" (vmsa->cr0)); + + vmsa->xcr0 = 1; + vmsa->g_pat = HV_AP_INIT_GPAT_DEFAULT; + vmsa->rip = (u64)secondary_startup_64_no_verify; + vmsa->rsp = (u64)&ap_start_stack[PAGE_SIZE]; + + /* + * Set the SNP-specific fields for this VMSA: + * VMPL level + * SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits) + */ + vmsa->vmpl = 0; + vmsa->sev_features = sev_status >> 2; + + /* + * Running at VMPL0 allows the kernel to change the VMSA bit for a page + * using the RMPADJUST instruction. However, for the instruction to + * succeed it must target the permissions of a lesser privileged + * (higher numbered) VMPL level, so use VMPL1 (refer to the RMPADJUST + * instruction in the AMD64 APM Volume 3). + */ + rmp_adjust = RMPADJUST_VMSA_PAGE_BIT | 1; + ret = rmpadjust((unsigned long)vmsa, RMP_PG_SIZE_4K, + rmp_adjust); + if (ret != 0) { + pr_err("RMPADJUST(%llx) failed: %llx\n", (u64)vmsa, ret); + return ret; + } + + local_irq_save(flags); + start_vp_input = + (struct hv_enable_vp_vtl *)ap_start_input_arg; + memset(start_vp_input, 0, sizeof(*start_vp_input)); + start_vp_input->partition_id = -1; + start_vp_input->vp_index = cpu; + start_vp_input->target_vtl.target_vtl = ms_hyperv.vtl; + *(u64 *)&start_vp_input->vp_context = __pa(vmsa) | 1; + + do { + ret = hv_do_hypercall(HVCALL_START_VP, + start_vp_input, NULL); + } while (hv_result(ret) == HV_STATUS_TIME_OUT && retry--); + + local_irq_restore(flags); + + if (!hv_result_success(ret)) + pr_err("HvCallStartVirtualProcessor failed: %llx\n", ret); + return ret; +} + void __init hv_vtom_init(void) { /* diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index e57df590846a..c5a3c29fad01 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -65,6 +65,13 @@ struct memory_map_entry { u32 reserved; }; +/* + * DEFAULT INIT GPAT and SEGMENT LIMIT value in struct VMSA + * to start AP in enlightened SEV guest. + */ +#define HV_AP_INIT_GPAT_DEFAULT 0x0007040600070406ULL +#define HV_AP_SEGMENT_LIMIT 0xffffffff + int hv_call_deposit_pages(int node, u64 partition_id, u32 num_pages); int hv_call_add_logical_proc(int node, u32 lp_index, u32 acpi_id); int hv_call_create_vp(int node, u64 partition_id, u32 vp_index, u32 flags); @@ -250,6 +257,7 @@ bool hv_ghcb_negotiate_protocol(void); void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason); void hv_vtom_init(void); void hv_sev_init_mem_and_cpu(void); +int hv_snp_boot_ap(int cpu, unsigned long start_ip); #else static inline void hv_ghcb_msr_write(u64 msr, u64 value) {} static inline void hv_ghcb_msr_read(u64 msr, u64 *value) {} @@ -257,6 +265,7 @@ static inline bool hv_ghcb_negotiate_protocol(void) { return false; } static inline void hv_ghcb_terminate(unsigned int set, unsigned int reason) {} static inline void hv_vtom_init(void) {} static inline void hv_sev_init_mem_and_cpu(void) {} +static int hv_snp_boot_ap(int cpu, unsigned long start_ip) {} #endif extern bool hv_isolation_type_snp(void); diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index d3bb921ee7fe..8e1d9ed6a1e0 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -295,6 +295,16 @@ static void __init hv_smp_prepare_cpus(unsigned int max_cpus) native_smp_prepare_cpus(max_cpus); + /* + * Override wakeup_secondary_cpu_64 callback for SEV-SNP + * enlightened guest. + */ + if (hv_isolation_type_en_snp()) + apic->wakeup_secondary_cpu_64 = hv_snp_boot_ap; + + if (!hv_root_partition) + return; + #ifdef CONFIG_X86_64 for_each_present_cpu(i) { if (i == 0) @@ -502,8 +512,7 @@ static void __init ms_hyperv_init_platform(void) # ifdef CONFIG_SMP smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu; - if (hv_root_partition) - smp_ops.smp_prepare_cpus = hv_smp_prepare_cpus; + smp_ops.smp_prepare_cpus = hv_smp_prepare_cpus; # endif /* diff --git a/include/asm-generic/hyperv-tlfs.h b/include/asm-generic/hyperv-tlfs.h index f4e4cc4f965f..fdac4a1714ec 100644 --- a/include/asm-generic/hyperv-tlfs.h +++ b/include/asm-generic/hyperv-tlfs.h @@ -223,6 +223,7 @@ enum HV_GENERIC_SET_FORMAT { #define HV_STATUS_INVALID_PORT_ID 17 #define HV_STATUS_INVALID_CONNECTION_ID 18 #define HV_STATUS_INSUFFICIENT_BUFFERS 19 +#define HV_STATUS_TIME_OUT 120 #define HV_STATUS_VTL_ALREADY_ENABLED 134 /* From patchwork Tue Jul 18 03:23:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tianyu Lan X-Patchwork-Id: 121744 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c923:0:b0:3e4:2afc:c1 with SMTP id j3csp1482872vqt; Mon, 17 Jul 2023 20:30:57 -0700 (PDT) X-Google-Smtp-Source: APBJJlESBwDIos+1XU44L4CVJQRkInhKxNcZlFP9YlHExsYGj/QbSu5wsuAXqrQ8DcKql582VLfP X-Received: by 2002:a17:90b:224a:b0:263:1d18:886a with SMTP id hk10-20020a17090b224a00b002631d18886amr13324385pjb.1.1689651057546; Mon, 17 Jul 2023 20:30:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689651057; cv=none; d=google.com; s=arc-20160816; b=obObekaFLaYMJNZPGz18erNKY2vERWNC/ccfywfq+pEvL8U4EBIbRoctsuNJ9mK5Q2 uPm7kN+rhenG7DKdgrYsuXYaONLKTUtp6QDGruWJ5AAgmqZTP7i7GFhPAMris1hS9qu1 MV4itOiE+jhL73NT1YLWRYt5JSX7zxNP1eSNXM7bH0+ZiiBLUjfWG2huQwz7twVmz526 jMYaLtc9NLbvq80dOiARr0/WUPE4xBUp9b3l1h1mQpKMvGCX2dRFJsppiwK4TrOdP0Mh OnuPdULyGbIvlmTG2tN9Ry8OoP6rc/Xwe7rAKOmvK+xrnRocH8J6aXGXTnZYpxoDl/CW 8mpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=0I/GN1swVut2u8gwwzCTgr8mdA5BefsHiacF2Dh1Kwk=; fh=TxLVtSsj0Yk+bstPG6ly04YaPbvjOFEmGNSQ8sZkNQc=; b=HXK8NJcXOitIZ4Gn2SP4YTstqtX2eqxaUEkncUfbrJ9C+eC9KkYF1Hq609JIPtDir9 6AVIcf8qjoEmXvzsaxRKUZitm2b9iGjOOUScZrCxTj1QXFQaN+f9t2KnyhpeqkIJMO4l RRlv10vL3YHAu/BiRfxp5pfn1VZgYQT5bYhDShbOx36Mb4YgY+WSKHUlO6zc8//AZrWk yj2OOT9KfrDS3WgBfxfTUjU5Tn/brF5r2DR951Y5idju5jyXIEy8aLfuiTOj39TU6DuW ED+T5znvWAEjtZp9iXNJ+DF33vNcEzSbaKKQx8s89nOatvbyb/0UsNlTlgqn8/Ic4f34 t3aw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Xb4B6R3y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id fr14-20020a17090ae2ce00b0025979e8c246si900891pjb.70.2023.07.17.20.30.44; Mon, 17 Jul 2023 20:30:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=Xb4B6R3y; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230094AbjGRDYt (ORCPT + 99 others); Mon, 17 Jul 2023 23:24:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33600 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231229AbjGRDYA (ORCPT ); Mon, 17 Jul 2023 23:24:00 -0400 Received: from mail-oi1-x231.google.com (mail-oi1-x231.google.com [IPv6:2607:f8b0:4864:20::231]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 18538172B; Mon, 17 Jul 2023 20:23:22 -0700 (PDT) Received: by mail-oi1-x231.google.com with SMTP id 5614622812f47-3a426e70575so2813334b6e.0; Mon, 17 Jul 2023 20:23:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689650602; x=1692242602; 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=0I/GN1swVut2u8gwwzCTgr8mdA5BefsHiacF2Dh1Kwk=; b=Xb4B6R3ynym2umsUFxuGCu0WcTP+WZwAHDaNB8j84egirjurOsbBigr1Itn8O1ZkU0 jTMsLxxogjG6KtbyeVEZ2nbAarYn7CZURH08pmB3LYfXLk3UKASx1PwFfNJqDquKS5qL B2jxGQeXj9EO/a1FtDb/2mdHkFy9usUNCBUyA9BCy1x0+/bFCvSj4GjENlwoqe0J9M8m zdyl/6+Q8HgB6NGZBWBj01uHxYn6GJ1IRdXtRr+lHd7AC1b7qPpCKWYHR8QHGgEkBjlK lylWiLkEfil0sXZejoJWePv5gJOeGP5KiJxJuN9lx1F3oaQ9zC3s6+D1S1NOm7/8MPdV paaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689650602; x=1692242602; 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=0I/GN1swVut2u8gwwzCTgr8mdA5BefsHiacF2Dh1Kwk=; b=XmUN7X6bp8asqXojQ2sKiBSSETcc+eqNo2NOZiGzAOiWmCCxat7+sltKVfsN5XegNp /ZFMRMnNf6Do8YgAbEtF+6eMX6XILvr7BdVOYQT0BOUfIpubx8KFfww8QymQ/s85FAnQ 9nFgAGeZCB3YVSGduIDLiaoAiC0koObqWIfJH5xFav5OKLjOygNepXbkBgdXSqMB3doD Neov4ce/IVVFFy8hiw32DC2SNwVAh3fL0QozNkKLLYGUDjuotG1OSMiwNtPuJdB6t7ji COXtzYo7XqmPzLr/BoM4RZ6SMNNLIsFOXuNs3sIwTh6F2puKqfgUYHZEAv/K69J3ngUc cbtg== X-Gm-Message-State: ABy/qLa8S7Sgpf+tI1mxPaN6UFt/2YxctWw20IDO/MI7vYUlBGGLGJc3 vwUvPKMfTd275CbIYCPjKlQ= X-Received: by 2002:a05:6808:13cf:b0:3a3:820e:2f05 with SMTP id d15-20020a05680813cf00b003a3820e2f05mr14716706oiw.1.1689650602206; Mon, 17 Jul 2023 20:23:22 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:37:c5e9:2003:6c97:8057]) by smtp.gmail.com with ESMTPSA id s92-20020a17090a2f6500b00263f41a655esm504040pjd.43.2023.07.17.20.23.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Jul 2023 20:23:21 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, daniel.lezcano@linaro.org, arnd@arndb.de, michael.h.kelley@microsoft.com Cc: Tianyu Lan , linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, vkuznets@redhat.com, Michael Kelley Subject: [PATCH V3 9/9] x86/hyperv: Add hyperv-specific handling for VMMCALL under SEV-ES Date: Mon, 17 Jul 2023 23:23:03 -0400 Message-Id: <20230718032304.136888-10-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230718032304.136888-1-ltykernel@gmail.com> References: <20230718032304.136888-1-ltykernel@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771727546951004957 X-GMAIL-MSGID: 1771727546951004957 From: Tianyu Lan Add Hyperv-specific handling for faults caused by VMMCALL instructions. Reviewed-by: Michael Kelley Signed-off-by: Tianyu Lan --- arch/x86/kernel/cpu/mshyperv.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/arch/x86/kernel/cpu/mshyperv.c b/arch/x86/kernel/cpu/mshyperv.c index 8e1d9ed6a1e0..ba9a3a65f664 100644 --- a/arch/x86/kernel/cpu/mshyperv.c +++ b/arch/x86/kernel/cpu/mshyperv.c @@ -32,6 +32,7 @@ #include #include #include +#include /* Is Linux running as the root partition? */ bool hv_root_partition; @@ -577,6 +578,20 @@ static bool __init ms_hyperv_msi_ext_dest_id(void) return eax & HYPERV_VS_PROPERTIES_EAX_EXTENDED_IOAPIC_RTE; } +static void hv_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs) +{ + /* RAX and CPL are already in the GHCB */ + ghcb_set_rcx(ghcb, regs->cx); + ghcb_set_rdx(ghcb, regs->dx); + ghcb_set_r8(ghcb, regs->r8); +} + +static bool hv_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs) +{ + /* No checking of the return state needed */ + return true; +} + const __initconst struct hypervisor_x86 x86_hyper_ms_hyperv = { .name = "Microsoft Hyper-V", .detect = ms_hyperv_platform, @@ -584,4 +599,6 @@ const __initconst struct hypervisor_x86 x86_hyper_ms_hyperv = { .init.x2apic_available = ms_hyperv_x2apic_available, .init.msi_ext_dest_id = ms_hyperv_msi_ext_dest_id, .init.init_platform = ms_hyperv_init_platform, + .runtime.sev_es_hcall_prepare = hv_sev_es_hcall_prepare, + .runtime.sev_es_hcall_finish = hv_sev_es_hcall_finish, };