Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp747090pxb; Thu, 21 Oct 2021 08:44:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxo4+saXBDR5Q58hKx/6GcKXPartE3eMbM4bQskHXWZrqDnD4b1ZND6kpRSIgsvnR6YFpwi X-Received: by 2002:a05:6402:35c5:: with SMTP id z5mr8652051edc.388.1634831047215; Thu, 21 Oct 2021 08:44:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1634831047; cv=none; d=google.com; s=arc-20160816; b=k5LKgS/PXuTp/WFvza9xjD9hY/jJh+6m5DOvKTjdKUNrrAKQ5upXHvL6p8k/QKSnoD aksvx3jAcJhrGPMBR7wWVlloDWP3T+b6noIppJk44wsvPpg4soOlQfxDnVRsxGuU410X WoevXxutXWzYa9qC2pNXdIT0ok4NRnMDKuZR74jUMbNnkXbfWqO7SUsU7t/lQF8hanAi fVqvTS/5cC9cBcRfcW5FiV0vJbDDQhWl81O1h2DuckAPA+tKNkz8uUENdQQ1ohvcsvxK l1Gj5HvI/WnoBpi3s4mO/6N4e6ae/pXLVykkUbsbn1qT/HD+1c2utKRRj72GXHa07+Ky BHTw== 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=rhK6Ol8pQ/kVpYxJ63s7xxGHzN0w2a0M7t+o1RS/RZk=; b=LZfZWkUQnmHWHkM57s9Lkb1Z02YW89duwA0zcHzpYuQbQltk8vtbgE2T/BZZXXsmUn Bn2a5tqhQ3nqdBaBlN5v6mNAIudUPXCssqurNlmBs6+TQJYd0Xrbb5WKrptT9JfDTCfh YpW5TpOOXnUuIGiOn8jD+rR/9lyU3SU+q/SoiEF+GhGx9mIne9WvipXIwJ6xtkw7F4lj AKg26Eq9TjMugUNc3WjSCpOcpNvmg/GAIgZlWhZhDXPjEzKe2Wh6dbahnPlgzLt6izHe nQjz+7i8CQqbj+gAr4F8Ko2mSTotS8znmRKhkgyuCMiTTcVBuyjsPgaobXRFSdN1P8Zl k3Xw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=py0uDYR+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y15si7395597edj.517.2021.10.21.08.43.42; Thu, 21 Oct 2021 08:44:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=py0uDYR+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S232073AbhJUPn4 (ORCPT + 99 others); Thu, 21 Oct 2021 11:43:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56702 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231857AbhJUPnk (ORCPT ); Thu, 21 Oct 2021 11:43:40 -0400 Received: from mail-pl1-x62a.google.com (mail-pl1-x62a.google.com [IPv6:2607:f8b0:4864:20::62a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61DA3C061348; Thu, 21 Oct 2021 08:41:24 -0700 (PDT) Received: by mail-pl1-x62a.google.com with SMTP id y4so733836plb.0; Thu, 21 Oct 2021 08:41:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rhK6Ol8pQ/kVpYxJ63s7xxGHzN0w2a0M7t+o1RS/RZk=; b=py0uDYR+jjcRDLGZQFtOj9SAkWIb58yzG2KAtKxlbFCB8mN8r+ngRLXsYF6IoBbxZb Oum0se4eHP7EI815eBGWySQTMaJWJVpbJvmlC3MTRqvKyoPRgGC9kmfBgM98GSa54FOn n2lfR5yY4FFYDO+MXn6NPAu31KDYZSMwGQT2f7dBAePCv1nLsVrquwh1Lu7vFF3Rz/j9 IyfClt6VRzfLdDcJn4LcpKSzIpJqaiWQeEzY9M5c3HkXYrlwlCI9GEKijtiT/0KNlDg5 vSeboa+mL5AB9vnOvIDCMIR8D/pXe+3FdtYrzO8Mc8Fa7qs+knPqfL1Nz1W260mhIl5p jl4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rhK6Ol8pQ/kVpYxJ63s7xxGHzN0w2a0M7t+o1RS/RZk=; b=nL4xYGj8n3+n1hF63GvnFCooLf+dTQeeXxbkqGjvMT3AU1uQNwx2lbfdFoX6wEOC9p al+wTXuivwvcNk5CIZY9lK4E4mjK1EO+mmBnfRIjk08k2benazSNNSGHlOnjkGYN0chD xYOWCDCWDldkbWbq+troKvT2O+yJtRC1Naz32/ZgO1irHmhrEfpcp7N4/MOohnrdcfqc PnB9tV8wuly8B4Y96TqTY8jJ9eBnX2zoXoqjrcQcACndPiWMl6BpgsyVzgJ5ymfapTpP +lAhA6E0CKvQiKX+A86u152R0u6Hvd+Svj6nuStVysuIvrbEmSYCE8EEaFx85VH1LQXD Pwig== X-Gm-Message-State: AOAM533DaHlEWLXMU5TQ2Uy/5uo+vb6QQprMnu9UVd+nsalLwi3rajb0 avMq56SSGKuwiljtgj5bSmE= X-Received: by 2002:a17:90b:38c6:: with SMTP id nn6mr7477774pjb.246.1634830883793; Thu, 21 Oct 2021 08:41:23 -0700 (PDT) Received: from ubuntu-Virtual-Machine.corp.microsoft.com ([2001:4898:80e8:f:a76d:53a5:b89f:c2a0]) by smtp.gmail.com with ESMTPSA id p9sm6384130pfn.7.2021.10.21.08.41.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 21 Oct 2021 08:41:23 -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, davem@davemloft.net, kuba@kernel.org, gregkh@linuxfoundation.org, arnd@arndb.de, brijesh.singh@amd.com, jroedel@suse.de, Tianyu.Lan@microsoft.com, thomas.lendacky@amd.com, rientjes@google.com, pgonda@google.com, akpm@linux-foundation.org, kirill.shutemov@linux.intel.com, rppt@kernel.org, saravanand@fb.com, aneesh.kumar@linux.ibm.com, hannes@cmpxchg.org, tj@kernel.org, michael.h.kelley@microsoft.com Cc: linux-arch@vger.kernel.org, linux-hyperv@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org, vkuznets@redhat.com, konrad.wilk@oracle.com, hch@lst.de, robin.murphy@arm.com, joro@8bytes.org, parri.andrea@gmail.com, dave.hansen@intel.com Subject: [PATCH V8 5/9] x86/sev-es: Expose sev_es_ghcb_hv_call() to call ghcb hv call out of sev code Date: Thu, 21 Oct 2021 11:41:05 -0400 Message-Id: <20211021154110.3734294-6-ltykernel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20211021154110.3734294-1-ltykernel@gmail.com> References: <20211021154110.3734294-1-ltykernel@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Tianyu Lan Hyper-V needs to call ghcb hv call to write/read MSR in Isolation VM. So expose sev_es_ghcb_hv_call() to call it in the Hyper-V code. Hyper-V Isolation VM is unenlightened guests and run a paravisor in the VMPL0 for communicating and GHCB pages are being allocated and set up by that paravisor. Linux gets ghcb page pa via MSR_AMD64_SEV_ES_GHCB from paravisor and should not change it. Add set_ghcb_msr parameter for sev_es_ghcb_hv_call() and not set ghcb page pa when it's false. Signed-off-by: Tianyu Lan --- arch/x86/include/asm/sev.h | 12 ++++++++++++ arch/x86/kernel/sev-shared.c | 26 +++++++++++++++++--------- arch/x86/kernel/sev.c | 13 +++++++------ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..5b7f7e2b81f7 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -81,12 +81,24 @@ static __always_inline void sev_es_nmi_complete(void) __sev_es_nmi_complete(); } extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); +extern enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, + bool set_ghcb_msr, + struct es_em_ctxt *ctxt, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } static inline void sev_es_nmi_complete(void) { } static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } +static inline enum +es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, + bool set_ghcb_msr, u64 exit_code, + u64 exit_info_1, u64 exit_info_2) +{ + return ES_VMM_ERROR; +} #endif #endif diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index ea9abd69237e..368ed36971e3 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -124,10 +124,9 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt return ES_VMM_ERROR; } -static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, - struct es_em_ctxt *ctxt, - u64 exit_code, u64 exit_info_1, - u64 exit_info_2) +enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, bool set_ghcb_msr, + struct es_em_ctxt *ctxt, u64 exit_code, + u64 exit_info_1, u64 exit_info_2) { /* Fill in protocol and format specifiers */ ghcb->protocol_version = GHCB_PROTOCOL_MAX; @@ -137,7 +136,15 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, ghcb_set_sw_exit_info_1(ghcb, exit_info_1); ghcb_set_sw_exit_info_2(ghcb, exit_info_2); - sev_es_wr_ghcb_msr(__pa(ghcb)); + /* + * Hyper-V unenlightened guests use a paravisor for communicating and + * GHCB pages are being allocated and set up by that paravisor. Linux + * should not change ghcb page pa in such case and so add set_ghcb_msr + * parameter. + */ + if (set_ghcb_msr) + sev_es_wr_ghcb_msr(__pa(ghcb)); + VMGEXIT(); return verify_exception_info(ghcb, ctxt); @@ -417,7 +424,7 @@ static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt) */ sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer); ghcb_set_sw_scratch(ghcb, sw_scratch); - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_IOIO, exit_info_1, exit_info_2); if (ret != ES_OK) return ret; @@ -459,7 +466,8 @@ static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt) ghcb_set_rax(ghcb, rax); - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, + SVM_EXIT_IOIO, exit_info_1, 0); if (ret != ES_OK) return ret; @@ -490,7 +498,7 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb, /* xgetbv will cause #GP - use reset value for xcr0 */ ghcb_set_xcr0(ghcb, 1); - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_CPUID, 0, 0); if (ret != ES_OK) return ret; @@ -515,7 +523,7 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, bool rdtscp = (exit_code == SVM_EXIT_RDTSCP); enum es_result ret; - ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, exit_code, 0, 0); if (ret != ES_OK) return ret; diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index a6895e440bc3..bb62a1d15d6c 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -648,7 +648,8 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) ghcb_set_rdx(ghcb, regs->dx); } - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, exit_info_1, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_MSR, + exit_info_1, 0); if ((ret == ES_OK) && (!exit_info_1)) { regs->ax = ghcb->save.rax; @@ -867,7 +868,7 @@ static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt, ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer)); - return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2); + return sev_es_ghcb_hv_call(ghcb, true, ctxt, exit_code, exit_info_1, exit_info_2); } static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb, @@ -1117,7 +1118,7 @@ static enum es_result vc_handle_dr7_write(struct ghcb *ghcb, /* Using a value of 0 for ExitInfo1 means RAX holds the value */ ghcb_set_rax(ghcb, val); - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_WRITE_DR7, 0, 0); if (ret != ES_OK) return ret; @@ -1147,7 +1148,7 @@ static enum es_result vc_handle_dr7_read(struct ghcb *ghcb, static enum es_result vc_handle_wbinvd(struct ghcb *ghcb, struct es_em_ctxt *ctxt) { - return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0); + return sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_WBINVD, 0, 0); } static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt) @@ -1156,7 +1157,7 @@ static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt ghcb_set_rcx(ghcb, ctxt->regs->cx); - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_RDPMC, 0, 0); if (ret != ES_OK) return ret; @@ -1197,7 +1198,7 @@ static enum es_result vc_handle_vmmcall(struct ghcb *ghcb, if (x86_platform.hyper.sev_es_hcall_prepare) x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs); - ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0); + ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_VMMCALL, 0, 0); if (ret != ES_OK) return ret; -- 2.25.1