From patchwork Sun Apr 21 18:01:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13637415 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 5FAD2C4345F for ; Sun, 21 Apr 2024 18:03:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E93036B008A; Sun, 21 Apr 2024 14:03:50 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E1B6C6B008C; Sun, 21 Apr 2024 14:03:50 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C6E6F6B0092; Sun, 21 Apr 2024 14:03:50 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id A44A16B008A for ; Sun, 21 Apr 2024 14:03:50 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 23350A08C0 for ; Sun, 21 Apr 2024 18:03:50 +0000 (UTC) X-FDA: 82034312220.03.3B354C7 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2060.outbound.protection.outlook.com [40.107.223.60]) by imf14.hostedemail.com (Postfix) with ESMTP id 450C0100007 for ; Sun, 21 Apr 2024 18:03:47 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JvRezQBa; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.60 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1713722627; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Vbdpkwf3WqWD2bfPQccxax6mE79R6wo8R9w46YmoB1c=; b=yU9/sw0PUrCbJXdJpxizvSzBk9ltSCwjx4W5cUSK0sRQGXS9XEB/Mvc7S6tfxvRjOndpCK eJNN1UhPz7ME52J5hh2tXikwq7FpLiqzIHFMiaKp47L4+5M2ffwRu8+CwV0Ax36aw/8htf V1Olo+8i3HTZVzTW9dF029a74/BSdSQ= ARC-Authentication-Results: i=2; imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=JvRezQBa; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf14.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.223.60 as permitted sender) smtp.mailfrom=Michael.Roth@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com ARC-Seal: i=2; s=arc-20220608; d=hostedemail.com; t=1713722627; a=rsa-sha256; cv=pass; b=aCmxVmZOtU80pNaoUfycslGBfWgEsApcPGaxWusKs1Stf3BuuAOVRiXPsNJJG4dzAB/Oud 0Z55zaLnkb8YKOtDZJ36G5k+NfhIkzfaRY0M6SWUgEfHxfu/ZJguWi3lAARcWBDCVU5gn+ PjVTLU6pgRuDr8HzjBH25uYtZ1R38bs= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OWMZxIKKCOTyjKlMfmtFlVAhQRPa+lqPTugnaczSdFJfR0KHafuWDe/r9AnD8us+uxxUQva13zZg1n/vFt6VFl3jYcDHhQjEEZeuz8gHBP4i/+OrSshyesye2rdEB3SYOdvosmH92V7soQ+pJ01XVL94I2KeXuaZxAr/k5NuoNepG3F5/djh2C1GpVwGpqzclV3wazLpV5F+kxCGWF+qg4uxSyjPSGZIxzUuyyWZpB7Q09bGnYEvnwXsdYZfKYYsgWM8m0oxoF7fsJmGebvPP4dmI+YEztRkXJGJsmQPK7J7KkBhQY4MzGjl4faTk3PlWnp/D8U6VYr4mshr2TmweA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=Vbdpkwf3WqWD2bfPQccxax6mE79R6wo8R9w46YmoB1c=; b=SRXC3mpifNL6gN2QZS/KHNRTwT8BMtmgFDDkF+LayjFeTMbtzVYgMs+iK9xlxdHe8hvvGZeMQlmP8+7S8OJuZamIMhkniNmGWluH77G5P+ckwtznPHye2IZ6oaFP//WUeUs/Hvhoo6CCaZ1Gkd+GVvc2u+QaSp1wbcAGb0AZ+MW9T7e9WpQclJZzbf5KPMtlNfNoov76UWJgUztajH7b1d1XyvyADpS3sq+x0WTh35RIV9fHyKTqaoorQh9/kwCg0lPdLc3prG+VAb2PWR5RXHPZ4y8yL7JVu3CGLunfusSB8JATc3wyvoAhU1GBMnwVGUZisrUmIb6XA+bzNN0VAw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Vbdpkwf3WqWD2bfPQccxax6mE79R6wo8R9w46YmoB1c=; b=JvRezQBaMPaWbdn4phF41P8uG8BugrQItXisyfdBeHGjsX/EgkVUsKZl8Ddf7uE0/erDs4ECBabR0CzSHrH7regxcU6CHPko1ac/7Ze0rEh082LVk7Ufad/GTqSDy8EtHLu8BAo4dmSjSEPNoAgJKUeIRJn30joYaqpEW/F1TL8= Received: from BL6PEPF00013E0E.NAMP222.PROD.OUTLOOK.COM (2603:10b6:22e:400:0:1001:0:f) by SA1PR12MB7248.namprd12.prod.outlook.com (2603:10b6:806:2be::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7472.44; Sun, 21 Apr 2024 18:03:43 +0000 Received: from MN1PEPF0000ECD8.namprd02.prod.outlook.com (2a01:111:f403:f902::2) by BL6PEPF00013E0E.outlook.office365.com (2603:1036:903:4::4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7495.26 via Frontend Transport; Sun, 21 Apr 2024 18:03:43 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by MN1PEPF0000ECD8.mail.protection.outlook.com (10.167.242.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7519.19 via Frontend Transport; Sun, 21 Apr 2024 18:03:43 +0000 Received: from localhost (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Sun, 21 Apr 2024 13:03:42 -0500 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH v14 10/22] KVM: SEV: Add support to handle Page State Change VMGEXIT Date: Sun, 21 Apr 2024 13:01:10 -0500 Message-ID: <20240421180122.1650812-11-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240421180122.1650812-1-michael.roth@amd.com> References: <20240421180122.1650812-1-michael.roth@amd.com> MIME-Version: 1.0 X-Originating-IP: [10.180.168.240] X-ClientProxiedBy: SATLEXMB03.amd.com (10.181.40.144) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MN1PEPF0000ECD8:EE_|SA1PR12MB7248:EE_ X-MS-Office365-Filtering-Correlation-Id: 06924529-0962-46e8-e5c1-08dc622d61d3 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 9Mz+RDQ/eZS4yQYkTQY3DfIPzAWcH6JSWdQex/i+0YpBx9ulgiyD0lkw47o0ytsj14sY34po9GX88Mzbe5v9GMlbGA3kVk5qMigXkxHkm5UPzTIh8UGV4cj4P2AK/uDJqSrF24/giWzqLIT7hMgzq0dEpKNpId46GGIJmfl9+5AYMKKnVcFCC9zaMnFvdSNr/H+x8B3jrZ7Ldt2LwBxYqotPmsCHyMYSgk2c9bK+m9oVGUad7JVAfpc48ZeQe1Xtb/Sgskotne5J4DYfruh9RyZXhtIVLBfIJ9kMBpbn7uskh5cuV+NLeRAxNMwK91a24vI/H45S34fiy3gNf0w+Qb4uN/YQ+IIgwo7IEzaIeeNfSvH2OXZPIvkKi+Bwn2SLwjzSUWSbfWn4Ae7KMaJouz5KU6CGua5BW1ywQIvx5vbwcU9prqjKQc3pcwroljCx4TVgec6iuvZaxujfQS4D/AVSlhM+Zk7dRmvOBlOijEGwHoomno92WQIBhW8/YhfNbPKDQ/DfRtTr/2qWfDERmPQtx7IeGvwzttRSQEFOfkQHzgeEoq8FMYQsV+T3e2EQiM/Lmy4//R1YdX2xcdFHZ8zYe1OKszvTYU/O/g6ALS8pXGauuejMPk08Fcpg5nAXZodS3fUUKsJcj3AZsI2qN4epO53cu4ZIChqyNuFP1T7fQHMwx8zfh71v5XMerofKZ+kEJgZaFfi5HQgABKJxaNE4o9v/I1m8iSVfl3lg+4g3IJ01GIhp0bTl0MFH+nK3z/Bq25H4u8AiivPJK9QZE2FGWV4aF4K0qZdk4Optn/pmI+hU0QlsQ0uXdzEwZoa1TMHUQch+Bz3UNFXNqMjq/rIdgE6Bqbh7R8YBNcZRjXaDFemPDpKEgD7fF9U5gceL/MTPKY/THIHd+sJsb2/HAm3M9v1V3/OXJrwqDoCtIJ7HrwW5j+KeNk01zKTsInkFbWgIYPU1AiDwnBcY1MpVgip2vGu64Cb05eG9cw5RQFfVclkg2LVtdrwfUsR46v2+w/UQnKciWG0Pf9CgFvjRXDDCtavliayHk+hwiiJAp4hMnNJdTZY5Rlz+Os2xPVQM+AnP/+/3I0aVpLPaoYRvOs8gFofZrGr9tqe29hEVGZ/d2bgqT7CpBaaHrbzIc1kLCY8unPvpDBqMBaZJaEwlrDacNBrD6uFhEJ9k1gLeM7rJ/tmhpFBxeXdDAQtR7WvmIDAwRUAZVMd44U+DguFsH3kd47y+w6WDjBp1wxbQiPt4GTIpWOIkMWJyzg3hFr4Zpasj6Rtbkv5PYCdjQshax3qB7gZ939R7twbeNk0Db+G/fj4IwOHWlq9/dld4826z/xd2u76DLNigmf5mOulQ4GxIa7X3MrQdEz6rk8KVurc= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230031)(376005)(1800799015)(7416005)(36860700004)(82310400014);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Apr 2024 18:03:43.3626 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 06924529-0962-46e8-e5c1-08dc622d61d3 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d;Ip=[165.204.84.17];Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: MN1PEPF0000ECD8.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR12MB7248 X-Rspamd-Queue-Id: 450C0100007 X-Stat-Signature: t8x7peop3hgbwo456ib7oysfd6sip85a X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1713722627-862792 X-HE-Meta: U2FsdGVkX1/GQxHTLrIlIkriiWZHowBmmwq0hmUXNUg+ObU+CKYWTczFrtNAnJi4blCwL9zvrBebCbXss2y7fVFubJZeMxn/D5ns5ckYwZvYELJj775we1UmWos40q5CQTrq3AB14DC3zGShKFswKEZGwkabg2zjyR0aknAnEGmJ4XB73RO4pIAaoUQJeCfhbGu8NM362IsACTCWcWP+us6Km32XrWJQrbHYx9UROMimfORe9MlKXhcEr2V2jU0IxywhIVS0MnJVGaWhrMKufdPZpJIsf/VJ2cFTibO+RbzjUTVFgwuhctwQZAggBeP4drY04MDWOVjeZcU6XfAG/w7YoqVEv3lqtPg+ijLsjOknEXGmQWXLdrUaoeueINY0dNlh/4M+Aztmfl2xx/BuruyX8/DmlL6PBsfShc1obJsRFOtZUbOfERb14XnIu5hlDW2expWSjP+ponU7xsQzxXC97FkGTvM5APtLoQ1ZDGtTGSxKpuY3bSNnwqHM+uPST+XcCKt90ZHJmFRbO7Fff6+Pz9fKTnqLIXTFGRUw/ogg0AsPDowm4njx/xJXGiCVmdnKjSyRkHU3FXDYejd/wDA14RXh+KTrh9bb45t+tp3JIjwdh/vivpwPxSbBj0/ZjvJ13vsRVbelVBuNLRGHIB75Af6iIC3Yik20l0yqM1R9/yHfX50S1o43f8W6Fz6xsm3L5FM752WCMhAFavrakCl4nt3vGSftIhHPl+W9D/nuB5JV2MPo8ta7d//WzlHExlKcuQcxWhTIISBnahMneZ7eHrSeQkw9GUR3oA4tW89+qvoY44afRJp3KBT0QNMkG1NxjH7ffxYIFk1cAtfFOO1fIkUS/eKC+hn0TXfRrmuDQi/dfS/oWrhrl0NU/BMsJNiEHMi+4eKY9bclI+trBzHySV3FRaeVMgnJi6wC9vJq9nZ8Fc8VAB2JLpVE5WPdrX+2i45AbQPgVYzgzPE +P+dWoid SdSOFn1R8/h8piTC4gtfjKyDhSTHlsWk6B7Jzh7Zz0sc31DNXM+FPLgJtooHkTmHAGxpVvmgh5ymCOAjaWahR0pkt+bwDFRW9p5SpuddlTKcDohFNQdf69eGg6rdd9hJd1kexWkyE7AWg+tti+kolc/aP2PSyszhs7QHawDp7IoWoIORznPeUbAL0W+H7TRdGBPkl6Coq+buw0+aGA4PHi0CbEjB/3Unk3Y/bZPOonUniVfNBDvMnBq/5UZCqtxqYEPEYGtS9zzrjcBYhbAIyoE4AhQ820axli5DnD7wm5XnymZyXZac7Wafrs4+mjpsB2qque/aCzhbmZ88= 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: List-Subscribe: List-Unsubscribe: From: Brijesh Singh SEV-SNP VMs can ask the hypervisor to change the page state in the RMP table to be private or shared using the Page State Change NAE event as defined in the GHCB specification version 2. Forward these requests to userspace as KVM_EXIT_VMGEXITs, similar to how it is done for requests that don't use a GHCB page. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- Documentation/virt/kvm/api.rst | 14 ++++++++++++++ arch/x86/kvm/svm/sev.c | 16 ++++++++++++++++ include/uapi/linux/kvm.h | 5 +++++ 3 files changed, 35 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 4a7a2945bc78..85099198a10f 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -7065,6 +7065,7 @@ values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. /* KVM_EXIT_VMGEXIT */ struct kvm_user_vmgexit { #define KVM_USER_VMGEXIT_PSC_MSR 1 + #define KVM_USER_VMGEXIT_PSC 2 __u32 type; /* KVM_USER_VMGEXIT_* type */ union { struct { @@ -7074,9 +7075,14 @@ values in kvm_run even if the corresponding bit in kvm_dirty_regs is not set. __u8 op; __u32 ret; } psc_msr; + struct { + __u64 shared_gpa; + __u64 ret; + } psc; }; }; + If exit reason is KVM_EXIT_VMGEXIT then it indicates that an SEV-SNP guest has issued a VMGEXIT instruction (as documented by the AMD Architecture Programmer's Manual (APM)) to the hypervisor that needs to be serviced by @@ -7094,6 +7100,14 @@ update the private/shared state of the GPA using the corresponding KVM_SET_MEMORY_ATTRIBUTES ioctl. The 'ret' field is to be set to 0 by userpace on success, or some non-zero value on failure. +For the KVM_USER_VMGEXIT_PSC type, the psc union type is used. The kernel +will supply the GPA of the Page State Structure defined in the GHCB spec. +Userspace will process this structure as defined by the GHCB, and issue +KVM_SET_MEMORY_ATTRIBUTES ioctls to set the GPAs therein to the expected +private/shared state. Userspace will return a value in 'ret' that is in +agreement with the GHCB-defined return values that the guest will expect +in the SW_EXITINFO2 field of the GHCB in response to these requests. + 6. Capabilities that can be enabled on vCPUs ============================================ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index f6f54a889fde..e9519af8f14c 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3275,6 +3275,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_JUMP_TABLE: case SVM_VMGEXIT_UNSUPPORTED_EVENT: case SVM_VMGEXIT_HV_FEATURES: + case SVM_VMGEXIT_PSC: break; default: reason = GHCB_ERR_INVALID_EVENT; @@ -3493,6 +3494,15 @@ static int snp_begin_psc_msr(struct kvm_vcpu *vcpu, u64 ghcb_msr) return 0; /* forward request to userspace */ } +static int snp_complete_psc(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + ghcb_set_sw_exit_info_2(svm->sev_es.ghcb, vcpu->run->vmgexit.psc.ret); + + return 1; /* resume guest */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3730,6 +3740,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; + case SVM_VMGEXIT_PSC: + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.type = KVM_USER_VMGEXIT_PSC; + vcpu->run->vmgexit.psc.shared_gpa = svm->sev_es.sw_scratch; + vcpu->arch.complete_userspace_io = snp_complete_psc; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 54b81e46a9fa..e33c48bfbd67 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -137,6 +137,7 @@ struct kvm_xen_exit { struct kvm_user_vmgexit { #define KVM_USER_VMGEXIT_PSC_MSR 1 +#define KVM_USER_VMGEXIT_PSC 2 __u32 type; /* KVM_USER_VMGEXIT_* type */ union { struct { @@ -146,6 +147,10 @@ struct kvm_user_vmgexit { __u8 op; __u32 ret; } psc_msr; + struct { + __u64 shared_gpa; + __u64 ret; + } psc; }; };