From nobody Mon Feb 9 17:21:44 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1627484016; cv=none; d=zohomail.com; s=zohoarc; b=YcxLx8zBh7KGkJUK8siNuWAM1/utxSLYSgHPH5SJoW5lUDhvS24E6AeQ5BbfgOAhp+DngA8/gYIFgwEQj+SAPfKTxN2892ffpsiZ/VgV+Ftaf/9usfIfPzhYV/VCRpGscJTJFJQK0ectmnply9gHu9XtFgaaPI4DttOoGymakDo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1627484016; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Ra70YmOoKyUigRxssc8bdLw9SZJrXLPE0YQ4dcYNGVo=; b=dlF99s5rBgxCbbRrjkrtPLj0vjbxQ3Ggr0PbGvY77yQs8FsjrSqUZ93YLQ0JssO/jxGNoASbQcyQRNyDbgYSbQrc/vdk/0sQ7XArF9Zy3K5SAv0QjgR6X30juEoGz7fjbdSrLCVesHEQuHL4Gf+OFBxDj7zsoNUXMCbP3hRhThA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1627484016020847.5200719760568; Wed, 28 Jul 2021 07:53:36 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.161583.296586 (Exim 4.92) (envelope-from ) id 1m8kvn-0002me-Es; Wed, 28 Jul 2021 14:53:11 +0000 Received: by outflank-mailman (output) from mailman id 161583.296586; Wed, 28 Jul 2021 14:53:11 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvn-0002mT-AQ; Wed, 28 Jul 2021 14:53:11 +0000 Received: by outflank-mailman (input) for mailman id 161583; Wed, 28 Jul 2021 14:53:10 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m8kvm-0002W0-Os for xen-devel@lists.xenproject.org; Wed, 28 Jul 2021 14:53:10 +0000 Received: from mail-pl1-x632.google.com (unknown [2607:f8b0:4864:20::632]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 8ef6e888-024b-47e5-b703-ca2fbe2129c6; Wed, 28 Jul 2021 14:53:08 +0000 (UTC) Received: by mail-pl1-x632.google.com with SMTP id q2so2966178plr.11 for ; Wed, 28 Jul 2021 07:53:08 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:0:3823:141e:6d51:f0ad]) by smtp.gmail.com with ESMTPSA id n134sm277558pfd.89.2021.07.28.07.53.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jul 2021 07:53:07 -0700 (PDT) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 8ef6e888-024b-47e5-b703-ca2fbe2129c6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ra70YmOoKyUigRxssc8bdLw9SZJrXLPE0YQ4dcYNGVo=; b=LmKHehSzb54ioqXz1ez6t1x7O5R86K81v+pIequaacef/tO9BDCzSjcgHS6hGrhQv2 qzARLAfbJyE6sHuqTHz9vyy6kXmEQNBaebh5fDgBK43+147na/gMGQQWfTHnt9bMZGW3 PI/xSNfASsbrpBOIlGaXrQ5YMH8qB1cvo0ozOtA0lboOZTSaOrbYcfC4Zr/MGizHIj4K UQn2qOIx7n+NsKeHbyzbGo6c8gE+9TdBheq0rWZ1PelrbCELqStjq++WLQ17f+9YCPDK xG/Aw0FLnS5tr9GZTkXJZ/H6BWgFhS32PFT431EJh1q4PAKHp3O/cbydxcbUBibmcfx9 Kdig== 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=Ra70YmOoKyUigRxssc8bdLw9SZJrXLPE0YQ4dcYNGVo=; b=gdrbvtuIDzx5Z+LkwIAeM3CsOT68Jfh+0xdn7aw+qRxfoeaLxkg3TLWl+1igpRranW jMHzxqVUthkXpM3bs64pwD7Jwryk08nRGI8pYFBJc6afWMKqNJ2WiCTcPtcwN3ZV2zlH NaVyGf1wzfOGJt30mKTLtBuPrkMkKQKUsqJKskbp41PCNiQ2nIKj3rqzXcuAq63pUiGt zpgwuSEoqj+Sm8GmOZ5+gK+GQDIrBT1Oernx3lV1Pw4iZsRiDkfHsOm5DtISiskFyPHF MVIpSInujZOb1Ay0RQcRzOZtbTnvseE7mucGUbPqkM8A6CeQNEiXJsCrW/lcjWgIjZgD k2cA== X-Gm-Message-State: AOAM53116mdHdNkKoxCfwcNdge/B29RbmcjzH5ubySaHGUTtZpab6a5z vVCwRvZ/vBk1wO+UHRK+I+A= X-Google-Smtp-Source: ABdhPJzsXb8YMbXnD2VQ8NxmDIMRNHMhIgP2MCg4JalE3hBkOx0ET9j7WaB1NV2mPcJTuLhWN1sM8A== X-Received: by 2002:a17:90a:ce02:: with SMTP id f2mr9955691pju.232.1627483988220; Wed, 28 Jul 2021 07:53:08 -0700 (PDT) From: Tianyu Lan To: kys@microsoft.com, haiyangz@microsoft.com, sthemmin@microsoft.com, wei.liu@kernel.org, decui@microsoft.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, x86@kernel.org, hpa@zytor.com, dave.hansen@linux.intel.com, luto@kernel.org, peterz@infradead.org, konrad.wilk@oracle.com, boris.ostrovsky@oracle.com, jgross@suse.com, sstabellini@kernel.org, joro@8bytes.org, will@kernel.org, davem@davemloft.net, kuba@kernel.org, jejb@linux.ibm.com, martin.petersen@oracle.com, arnd@arndb.de, hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com, thomas.lendacky@amd.com, brijesh.singh@amd.com, ardb@kernel.org, Tianyu.Lan@microsoft.com, rientjes@google.com, martin.b.radev@gmail.com, akpm@linux-foundation.org, rppt@kernel.org, kirill.shutemov@linux.intel.com, aneesh.kumar@linux.ibm.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, pgonda@google.com, david@redhat.com, keescook@chromium.org, hannes@cmpxchg.org, sfr@canb.auug.org.au, michael.h.kelley@microsoft.com Cc: iommu@lists.linux-foundation.org, linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, linux-scsi@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, anparri@microsoft.com Subject: [PATCH 01/13] x86/HV: Initialize GHCB page in Isolation VM Date: Wed, 28 Jul 2021 10:52:16 -0400 Message-Id: <20210728145232.285861-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210728145232.285861-1-ltykernel@gmail.com> References: <20210728145232.285861-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1627484018210100005 Content-Type: text/plain; charset="utf-8" From: Tianyu Lan Hyper-V exposes GHCB page via SEV ES GHCB MSR for SNP guest to communicate with hypervisor. Map GHCB page for all cpus to read/write MSR register and submit hvcall request via GHCB. Signed-off-by: Tianyu Lan --- arch/x86/hyperv/hv_init.c | 73 +++++++++++++++++++++++++++++++-- arch/x86/include/asm/mshyperv.h | 2 + include/asm-generic/mshyperv.h | 2 + 3 files changed, 73 insertions(+), 4 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index 4a643a85d570..ee449c076ef4 100644 --- a/arch/x86/hyperv/hv_init.c +++ b/arch/x86/hyperv/hv_init.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,26 @@ static void *hv_hypercall_pg_saved; struct hv_vp_assist_page **hv_vp_assist_page; EXPORT_SYMBOL_GPL(hv_vp_assist_page); =20 +static int hyperv_init_ghcb(void) +{ + u64 ghcb_gpa; + void *ghcb_va; + void **ghcb_base; + + if (!ms_hyperv.ghcb_base) + return -EINVAL; + + rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa); + ghcb_va =3D memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB); + if (!ghcb_va) + return -ENOMEM; + + ghcb_base =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + *ghcb_base =3D ghcb_va; + + return 0; +} + static int hv_cpu_init(unsigned int cpu) { struct hv_vp_assist_page **hvp =3D &hv_vp_assist_page[smp_processor_id()]; @@ -75,6 +96,8 @@ static int hv_cpu_init(unsigned int cpu) wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); } =20 + hyperv_init_ghcb(); + return 0; } =20 @@ -167,6 +190,31 @@ static int hv_cpu_die(unsigned int cpu) { struct hv_reenlightenment_control re_ctrl; unsigned int new_cpu; + unsigned long flags; + void **input_arg; + void *pg; + void **ghcb_va =3D NULL; + + local_irq_save(flags); + input_arg =3D (void **)this_cpu_ptr(hyperv_pcpu_input_arg); + pg =3D *input_arg; + *input_arg =3D NULL; + + if (hv_root_partition) { + void **output_arg; + + output_arg =3D (void **)this_cpu_ptr(hyperv_pcpu_output_arg); + *output_arg =3D NULL; + } + + if (ms_hyperv.ghcb_base) { + ghcb_va =3D (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + if (*ghcb_va) + memunmap(*ghcb_va); + *ghcb_va =3D NULL; + } + + local_irq_restore(flags); =20 hv_common_cpu_die(cpu); =20 @@ -340,9 +388,22 @@ void __init hyperv_init(void) VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, __builtin_return_address(0)); - if (hv_hypercall_pg =3D=3D NULL) { - wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); - goto remove_cpuhp_state; + if (hv_hypercall_pg =3D=3D NULL) + goto clean_guest_os_id; + + if (hv_isolation_type_snp()) { + ms_hyperv.ghcb_base =3D alloc_percpu(void *); + if (!ms_hyperv.ghcb_base) + goto clean_guest_os_id; + + if (hyperv_init_ghcb()) { + free_percpu(ms_hyperv.ghcb_base); + ms_hyperv.ghcb_base =3D NULL; + goto clean_guest_os_id; + } + + /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */ + hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id); } =20 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); @@ -403,7 +464,8 @@ void __init hyperv_init(void) hv_query_ext_cap(0); return; =20 -remove_cpuhp_state: +clean_guest_os_id: + wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); cpuhp_remove_state(cpuhp); free_vp_assist_page: kfree(hv_vp_assist_page); @@ -431,6 +493,9 @@ void hyperv_cleanup(void) */ hv_hypercall_pg =3D NULL; =20 + if (ms_hyperv.ghcb_base) + free_percpu(ms_hyperv.ghcb_base); + /* Reset the hypercall page */ hypercall_msr.as_uint64 =3D 0; wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index adccbc209169..6627cfd2bfba 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -11,6 +11,8 @@ #include #include =20 +DECLARE_STATIC_KEY_FALSE(isolation_type_snp); + typedef int (*hyperv_fill_flush_list_func)( struct hv_guest_mapping_flush_list *flush, void *data); diff --git a/include/asm-generic/mshyperv.h b/include/asm-generic/mshyperv.h index c1ab6a6e72b5..4269f3174e58 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -36,6 +36,7 @@ struct ms_hyperv_info { u32 max_lp_index; u32 isolation_config_a; u32 isolation_config_b; + void __percpu **ghcb_base; }; extern struct ms_hyperv_info ms_hyperv; =20 @@ -237,6 +238,7 @@ bool hv_is_hyperv_initialized(void); bool hv_is_hibernation_supported(void); enum hv_isolation_type hv_get_isolation_type(void); bool hv_is_isolation_supported(void); +bool hv_isolation_type_snp(void); void hyperv_cleanup(void); bool hv_query_ext_cap(u64 cap_query); #else /* CONFIG_HYPERV */ --=20 2.25.1