From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 83436C433F5 for ; Tue, 1 Feb 2022 20:17:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D8AD98D0085; Tue, 1 Feb 2022 15:17:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D13728D006D; Tue, 1 Feb 2022 15:17:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B656E8D0085; Tue, 1 Feb 2022 15:17:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay034.a.hostedemail.com [64.99.140.34]) by kanga.kvack.org (Postfix) with ESMTP id 9FC3C8D006D for ; Tue, 1 Feb 2022 15:17:16 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 591B7465 for ; Tue, 1 Feb 2022 20:17:16 +0000 (UTC) X-FDA: 79095320472.01.D0B0697 Received: from mail-lj1-f171.google.com (mail-lj1-f171.google.com [209.85.208.171]) by imf05.hostedemail.com (Postfix) with ESMTP id DD44F100002 for ; Tue, 1 Feb 2022 20:17:15 +0000 (UTC) Received: by mail-lj1-f171.google.com with SMTP id t14so25728311ljh.8 for ; Tue, 01 Feb 2022 12:17:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=c1G+U9mCR+9y3z9D1TzfeiUfdMNDGnmglMpEe2+/2LE=; b=VpWU+oS1r5knoJXzOyILe4moOYE+R2uHT5zRZ2Jk6DhbUyjF27qJ3+INmDQ6fgW21h Tbks8twIhWx83GEmJz8SvyDl28seEQXqJqh3NV9g5rTHkqsOws4rhC3I/N/p7qcc254t dO5dfpzfe+UZ1qwnCXB5UZJSurEkkiBYs4s3D9Zas6b0x9z+x5I85DrD9l2VaSp+bKq7 SMWCvA1VLe6N2jnWSR72sRQz1JDnI/ile1y5D0ika7OBCB6qjHvaKnf83FjnjAl6pUlX yedLGvX4pfjFeVB5yjbxiMrAQ/0ETQPPiMCTCVHV0Dt1fljCfrW3KHBEl8XoGKzF8eOV yQiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=c1G+U9mCR+9y3z9D1TzfeiUfdMNDGnmglMpEe2+/2LE=; b=lRksi6Y5hzYSnLk7rJaGCQ3sNqPh35+FvcEVW9zNTkXPWLCd6JHRnubGKTaMKtg0CI 100rvv3ouNJ7ITs8GSSgMHVaJs4wc3EOQYSB8Y0EnEjRreJYq2DnPoXxqKwotHydnhNT 0g7PX+WV+/oB5vV/+4Np7i6I3xvln9PEp3AYc2WweYWzlAcIW//LMRT2C81nxRFpD0HZ SnyOar2eFn5i4CB0JvvKYh/WJMg+KFz/VPi9jT1Fs7UiuKzRgUa034ubXUzh2DqGf+Jh C9bQZXL2lMos2gCr04qwV+DKrTv/HxoqwDB4PLlz8QSD7TZQiREerhy/TXXHB0jfTOoM f/Mg== X-Gm-Message-State: AOAM530jTK5l1xIrzFM69RUFStdwt9CJEbAMvqrJkvF5sBShIjiGanVD MSTizHJ87NhAZjbBwWBaiWMOT5FWTUsU2ygViUxBmg== X-Google-Smtp-Source: ABdhPJw/gG064uPw6B2Edvi7SJHms7j1vwY6jMCGatl6ahEztehk1/Zegz8WhJ1BfcdyuJ3eUs0emystXh2QL2v0bB8= X-Received: by 2002:a2e:a781:: with SMTP id c1mr16364597ljf.527.1643746633936; Tue, 01 Feb 2022 12:17:13 -0800 (PST) MIME-Version: 1.0 References: <20220128171804.569796-1-brijesh.singh@amd.com> <20220128171804.569796-40-brijesh.singh@amd.com> In-Reply-To: <20220128171804.569796-40-brijesh.singh@amd.com> From: Peter Gonda Date: Tue, 1 Feb 2022 13:17:02 -0700 Message-ID: Subject: Re: [PATCH v9 39/43] x86/sev: Provide support for SNP guest request NAEs To: Brijesh Singh Cc: "the arch/x86 maintainers" , LKML , kvm list , linux-efi@vger.kernel.org, platform-driver-x86@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , "Kirill A . Shutemov" , Andi Kleen , "Dr . David Alan Gilbert" , brijesh.ksingh@gmail.com, Tony Luck , Marc Orr , Sathyanarayanan Kuppuswamy Content-Type: text/plain; charset="UTF-8" X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: DD44F100002 X-Stat-Signature: 5q771wt4mroko5nb3nju7kfj1broodqs Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=VpWU+oS1; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of pgonda@google.com designates 209.85.208.171 as permitted sender) smtp.mailfrom=pgonda@google.com X-Rspam-User: nil X-HE-Tag: 1643746635-789757 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Jan 28, 2022 at 10:19 AM Brijesh Singh wrote: > > Version 2 of GHCB specification provides SNP_GUEST_REQUEST and > SNP_EXT_GUEST_REQUEST NAE that can be used by the SNP guest to communicate > with the PSP. > > While at it, add a snp_issue_guest_request() helper that will be used by > driver or other subsystem to issue the request to PSP. > > See SEV-SNP firmware and GHCB spec for more details. > > Signed-off-by: Brijesh Singh > --- > arch/x86/include/asm/sev-common.h | 3 ++ > arch/x86/include/asm/sev.h | 14 ++++++++ > arch/x86/include/uapi/asm/svm.h | 4 +++ > arch/x86/kernel/sev.c | 55 +++++++++++++++++++++++++++++++ > 4 files changed, 76 insertions(+) > > diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h > index cd769984e929..442614879dad 100644 > --- a/arch/x86/include/asm/sev-common.h > +++ b/arch/x86/include/asm/sev-common.h > @@ -128,6 +128,9 @@ struct snp_psc_desc { > struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; > } __packed; > > +/* Guest message request error code */ > +#define SNP_GUEST_REQ_INVALID_LEN BIT_ULL(32) > + > #define GHCB_MSR_TERM_REQ 0x100 > #define GHCB_MSR_TERM_REASON_SET_POS 12 > #define GHCB_MSR_TERM_REASON_SET_MASK 0xf > diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h > index 219abb4590f2..9830ee1d6ef0 100644 > --- a/arch/x86/include/asm/sev.h > +++ b/arch/x86/include/asm/sev.h > @@ -87,6 +87,14 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); > > #define RMPADJUST_VMSA_PAGE_BIT BIT(16) > > +/* SNP Guest message request */ > +struct snp_req_data { > + unsigned long req_gpa; > + unsigned long resp_gpa; > + unsigned long data_gpa; > + unsigned int data_npages; > +}; > + > #ifdef CONFIG_AMD_MEM_ENCRYPT > extern struct static_key_false sev_es_enable_key; > extern void __sev_es_ist_enter(struct pt_regs *regs); > @@ -154,6 +162,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages); > void snp_set_wakeup_secondary_cpu(void); > bool snp_init(struct boot_params *bp); > void snp_abort(void); > +int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err); > #else > static inline void sev_es_ist_enter(struct pt_regs *regs) { } > static inline void sev_es_ist_exit(void) { } > @@ -173,6 +182,11 @@ static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npag > static inline void snp_set_wakeup_secondary_cpu(void) { } > static inline bool snp_init(struct boot_params *bp) { return false; } > static inline void snp_abort(void) { } > +static inline int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, > + unsigned long *fw_err) > +{ > + return -ENOTTY; > +} > #endif > > #endif > diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h > index 8b4c57baec52..5b8bc2b65a5e 100644 > --- a/arch/x86/include/uapi/asm/svm.h > +++ b/arch/x86/include/uapi/asm/svm.h > @@ -109,6 +109,8 @@ > #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 > #define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1 > #define SVM_VMGEXIT_PSC 0x80000010 > +#define SVM_VMGEXIT_GUEST_REQUEST 0x80000011 > +#define SVM_VMGEXIT_EXT_GUEST_REQUEST 0x80000012 > #define SVM_VMGEXIT_AP_CREATION 0x80000013 > #define SVM_VMGEXIT_AP_CREATE_ON_INIT 0 > #define SVM_VMGEXIT_AP_CREATE 1 > @@ -225,6 +227,8 @@ > { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ > { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ > { SVM_VMGEXIT_PSC, "vmgexit_page_state_change" }, \ > + { SVM_VMGEXIT_GUEST_REQUEST, "vmgexit_guest_request" }, \ > + { SVM_VMGEXIT_EXT_GUEST_REQUEST, "vmgexit_ext_guest_request" }, \ > { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ > { SVM_VMGEXIT_HV_FEATURES, "vmgexit_hypervisor_feature" }, \ > { SVM_EXIT_ERR, "invalid_guest_state" } > diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c > index cb97200bfda7..1d3ac83226fc 100644 > --- a/arch/x86/kernel/sev.c > +++ b/arch/x86/kernel/sev.c > @@ -2122,3 +2122,58 @@ static int __init snp_check_cpuid_table(void) > } > > arch_initcall(snp_check_cpuid_table); > + > +int snp_issue_guest_request(u64 exit_code, struct snp_req_data *input, unsigned long *fw_err) > +{ > + struct ghcb_state state; > + struct es_em_ctxt ctxt; > + unsigned long flags; > + struct ghcb *ghcb; > + int ret; > + > + if (!cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) > + return -ENODEV; > + > + /* > + * __sev_get_ghcb() needs to run with IRQs disabled because it is using > + * a per-CPU GHCB. > + */ > + local_irq_save(flags); > + > + ghcb = __sev_get_ghcb(&state); > + if (!ghcb) { > + ret = -EIO; > + goto e_restore_irq; > + } > + > + vc_ghcb_invalidate(ghcb); > + > + if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST) { > + ghcb_set_rax(ghcb, input->data_gpa); > + ghcb_set_rbx(ghcb, input->data_npages); > + } > + > + ret = sev_es_ghcb_hv_call(ghcb, true, &ctxt, exit_code, input->req_gpa, input->resp_gpa); > + if (ret) > + goto e_put; > + > + if (ghcb->save.sw_exit_info_2) { > + /* Number of expected pages are returned in RBX */ > + if (exit_code == SVM_VMGEXIT_EXT_GUEST_REQUEST && > + ghcb->save.sw_exit_info_2 == SNP_GUEST_REQ_INVALID_LEN) > + input->data_npages = ghcb_get_rbx(ghcb); > + > + if (fw_err) > + *fw_err = ghcb->save.sw_exit_info_2; In the PSP driver we've had a bit of discussion around the fw_err and the return code and that it would be preferable to have fw_err be a required parameter. And then we can easily make sure fw_err is always non-zero when the return code is non-zero. Thoughts about doing the same inside the guest? > + > + ret = -EIO; > + } > + > +e_put: > + __sev_put_ghcb(&state); > +e_restore_irq: > + local_irq_restore(flags); > + > + return ret; > +} > +EXPORT_SYMBOL_GPL(snp_issue_guest_request); > -- > 2.25.1 >