From nobody Sat Feb 7 08:44:38 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=1625672818; cv=none; d=zohomail.com; s=zohoarc; b=f3tNHsLk8fhe9fzihy9us8j9Ru8xGUhu40IBpDp3rdxgv3nsEz0pS5TOEvNRrK4unha9KaieZkq5X7GLcD0Fdi5UxyZ1X/ET2C4nqrPxG+50ANxXh+44ocj1ulsMaS5yD9SOpfcsL1z0szhM4T3wbWfdRIw4vM+UB4f308Cd1OE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625672818; 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=R5OLGAMwYXeSrSgg628gKxmY1CTczZlgXFfhQZlHpg0=; b=R6gZyHOPKPKSbypsckIj48olEAPGFeR0YsQ6Ecdvv6oC6z9YhVb/rsuNX80rxYW2u7tMdVE5EfJq/IbAL/zt3Popy2BDS8L8SIcSoBx013LoH2nVgHG9ZRf7g6Uu51rP4LOQw/z5U+3rRXieoyhWsKJ5SnOrDC/OuKTzyaZLeLE= 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 1625672818390891.0753241252564; Wed, 7 Jul 2021 08:46:58 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.152486.281718 (Exim 4.92) (envelope-from ) id 1m19l7-000356-AL; Wed, 07 Jul 2021 15:46:45 +0000 Received: by outflank-mailman (output) from mailman id 152486.281718; Wed, 07 Jul 2021 15:46:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19l7-00034v-5t; Wed, 07 Jul 2021 15:46:45 +0000 Received: by outflank-mailman (input) for mailman id 152486; Wed, 07 Jul 2021 15:46:44 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m19l6-0002ed-3w for xen-devel@lists.xenproject.org; Wed, 07 Jul 2021 15:46:44 +0000 Received: from mail-pf1-x42e.google.com (unknown [2607:f8b0:4864:20::42e]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id f952742f-823b-431e-b5fa-5d6e02d6d4af; Wed, 07 Jul 2021 15:46:42 +0000 (UTC) Received: by mail-pf1-x42e.google.com with SMTP id f20so2555667pfa.1 for ; Wed, 07 Jul 2021 08:46:41 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:38:6b47:cf3e:bbf2:d229]) by smtp.gmail.com with ESMTPSA id q18sm23093560pgj.8.2021.07.07.08.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 08:46:41 -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: f952742f-823b-431e-b5fa-5d6e02d6d4af 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=R5OLGAMwYXeSrSgg628gKxmY1CTczZlgXFfhQZlHpg0=; b=tgnrux6t6SkoTnavPrn5fh2qP8KaJe6YyAcLF8Zt4MPOaOlMQnjeAPQtLYHcXHGF0l RB/sKa5anmBAiy1Yvr2Y+jbSOIsZKj02IHcz79CtMxEELmbdvgejBFoU8P/inGk7Ski+ UB5Fbm6qJvsCU7DXOgoODC5/LhmKJ2RHM0qWVcBm5px6hqJ6rLFhYCJMaghMJN7A4+Ae pn3qiH84GvkyWF7QU5yE4BQr7v0t0dCF2LiTlW/fuYW0dRdIKXqx8OiOcW0SB0gvFoEe 6fqsHE83UrjkzL8JL9iT4cPKy7cfHYkLm0LgcRdpfqv0E36dKdrKn5jfgHF+5+w1Jqdf JEIA== 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=R5OLGAMwYXeSrSgg628gKxmY1CTczZlgXFfhQZlHpg0=; b=qM84QtTHgvqVQiVO5CFo0ahEow3uDPtUtElJ9uiDEeqTyg0CjZ2bzybXuvxk9STrrM ObYFSrZVh/9UaZQeRsZVE1k9kBqlDKSKNBqLKiEqQ2LgzpyUkHLg+ZnzU5sH97AX2RcQ eymrwVVMa0gW9DzKYWGXK69wD4a46shHcjRH4tbHx8v3mhNnAjTEFF4rx0c4qlGR9qUp X5IqudVYWYL/Za02zTDpfI4zlMf7G7Aw8TcoCQoN+g14mk6x0lJn4fOFVcEyHSlwzAVD CHCPLQcl8r/hvSWBgGs+NDdNox2Y9ro2fuMHXnLGYLNKs/z13rgPFwerSu8YNw4eJXok S1ew== X-Gm-Message-State: AOAM533QMFIDo0g2b2uxTNz9vGB2Auny3BECo5Bf/2ohTE1d9bQCBrnq 1HVrMcpFJFM24YPkh7cHhIg= X-Google-Smtp-Source: ABdhPJzuj8kyKMtNb+EskaT+9TtDKmzwnLiT5Tz7koSTfL0Lib9zH4bi1+4ZTW8ZC1YP+6PSa+4BSA== X-Received: by 2002:a62:fb13:0:b029:309:8d89:46b2 with SMTP id x19-20020a62fb130000b02903098d8946b2mr25766588pfm.67.1625672801313; Wed, 07 Jul 2021 08:46:41 -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, kirill.shutemov@linux.intel.com, akpm@linux-foundation.org, rppt@kernel.org, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, ardb@kernel.org, robh@kernel.org, nramas@linux.microsoft.com, pgonda@google.com, martin.b.radev@gmail.com, david@redhat.com, krish.sadhukhan@oracle.com, saravanand@fb.com, xen-devel@lists.xenproject.org, keescook@chromium.org, rientjes@google.com, hannes@cmpxchg.org, 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, brijesh.singh@amd.com, anparri@microsoft.com Subject: [Resend RFC PATCH V4 01/13] x86/HV: Initialize GHCB page in Isolation VM Date: Wed, 7 Jul 2021 11:46:15 -0400 Message-Id: <20210707154629.3977369-2-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20210707154629.3977369-1-ltykernel@gmail.com> References: <20210707154629.3977369-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1625672820569100003 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 | 64 ++++++++++++++++++++++++++++++--- arch/x86/include/asm/mshyperv.h | 2 ++ include/asm-generic/mshyperv.h | 2 ++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/arch/x86/hyperv/hv_init.c b/arch/x86/hyperv/hv_init.c index b756b2866deb..e058f72eeee5 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 @@ -54,6 +55,26 @@ EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg); u32 hv_max_vp_index; EXPORT_SYMBOL_GPL(hv_max_vp_index); =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) { u64 msr_vp_index; @@ -106,6 +127,8 @@ static int hv_cpu_init(unsigned int cpu) wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); } =20 + hyperv_init_ghcb(); + return 0; } =20 @@ -201,6 +224,7 @@ static int hv_cpu_die(unsigned int cpu) unsigned long flags; void **input_arg; void *pg; + void **ghcb_va =3D NULL; =20 local_irq_save(flags); input_arg =3D (void **)this_cpu_ptr(hyperv_pcpu_input_arg); @@ -214,6 +238,13 @@ static int hv_cpu_die(unsigned int cpu) *output_arg =3D NULL; } =20 + 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 free_pages((unsigned long)pg, hv_root_partition ? 1 : 0); @@ -410,9 +441,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); @@ -473,7 +517,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); @@ -502,6 +547,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); @@ -586,6 +634,14 @@ bool hv_is_isolation_supported(void) } EXPORT_SYMBOL_GPL(hv_is_isolation_supported); =20 +DEFINE_STATIC_KEY_FALSE(isolation_type_snp); + +bool hv_isolation_type_snp(void) +{ + return static_branch_unlikely(&isolation_type_snp); +} +EXPORT_SYMBOL_GPL(hv_isolation_type_snp); + /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx= */ bool hv_query_ext_cap(u64 cap_query) { diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyper= v.h index 67ff0d637e55..aeacca7c4da8 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 9a000ba2bb75..3ae56a29594f 100644 --- a/include/asm-generic/mshyperv.h +++ b/include/asm-generic/mshyperv.h @@ -35,6 +35,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 @@ -224,6 +225,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