From patchwork Wed Dec 14 19:40:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Roth X-Patchwork-Id: 13073460 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 D8D5EC4332F for ; Wed, 14 Dec 2022 20:01:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 835768E0008; Wed, 14 Dec 2022 15:01:34 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E4898E0002; Wed, 14 Dec 2022 15:01:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65EAA8E0008; Wed, 14 Dec 2022 15:01:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 53C008E0002 for ; Wed, 14 Dec 2022 15:01:34 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EB8DB120F8A for ; Wed, 14 Dec 2022 20:01:33 +0000 (UTC) X-FDA: 80241981666.20.F60075A Received: from NAM12-MW2-obe.outbound.protection.outlook.com (mail-mw2nam12on2054.outbound.protection.outlook.com [40.107.244.54]) by imf08.hostedemail.com (Postfix) with ESMTP id 06F0816000E for ; Wed, 14 Dec 2022 20:01:30 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=y2yuMNXd; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.54 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=1671048091; 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=GlVqa2qmkaaiQMe4N6o7Bdn+VWWRYcbCRi5kfE+AEcs=; b=NInnIm5WM4qDNKAEZY5qsIz1y2xuVccJNUdgnkVZspUMTZZEWTjBr4xc5we+XsL4MR4JE4 ejlgnFor+y1Zgtqiqcisp+WnH749+Lrpd1ghzAGQ44DCp+EvaqraSGugLfdrVYZaPm5C5m wRysGAMJtIes057tTvGVuAAJVF9D4BU= ARC-Authentication-Results: i=2; imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=y2yuMNXd; arc=pass ("microsoft.com:s=arcselector9901:i=1"); spf=pass (imf08.hostedemail.com: domain of Michael.Roth@amd.com designates 40.107.244.54 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=1671048091; a=rsa-sha256; cv=pass; b=537tTa1YQBEbBE2VFMpu5Dzq4fy6m6ySs3FU3ZT24k0A+iL05xxKIuu4IyX7Fct59DqmNr CQLNhTEA3KXj4zqu9ZNFp1uSRvYh16q6ARUAuuZJgLHhEokicvfF6Lba8PNDW6nKcvwt/9 y5MD5czocdMjWocomXVgm5v4XfZQBSM= ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=et/pSCXO1Pu7zUui0Dt88nBwf8QcIBM0mH7v1AL/XONJc26rvBcDEzKpTmk+4ppNhb4lhnSFmtd3ROCDdr0AMYRs4czsdEIejZbl+f+erE+lgTxWLMR8WxJL1eshOD1k4BmAkuZajdscsAc06lyb2pJ1zQi8Wz7ivrCVOHGTXEzVwNvUCwhF4I4u1A9dzQs34hEFHa7DYyccxfdKcxUMOz9sl7v/J73jkHAngWEzTB5ghZGoDLy7kFmvwz0yZkqV6RtE+HRR15bAsGMjxFempjQ/YNhF1KtuaZzgvuNfC9j5nqzSm/OBMcSHKXZGoDSTVG0yJSknQnMd5r5pscIPcQ== 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=GlVqa2qmkaaiQMe4N6o7Bdn+VWWRYcbCRi5kfE+AEcs=; b=VOFO1kFg9cIn1nbmsYnyptDGWujJMbH9CddmAWGDCzTNHashUJP63+E0IIkzon0S9h/sth0Laz6IMSjAoXMypNJ5LxKQhtZTkavGXaKBQjV6cszccK+dL21I3zLL9hCfA+Vxa8PnIzlw0kwNTGo1zvSfx9hUBu8pCvdcdYWW9q0OgShHTjL0Ul6NTdV9Nfe7PG7VLKCcoD5DcGdMvDeeM+7JbEahpIZOxLRMv35tqSp4ByiMxjyZ5Vg9KsPT3x0LRm2BbU6Us29WmwyC1tv/dEF5MX9n8hgNX77CpHqDgu3LLNpreN3faAdTKiSwusp+duPJlWBF8K++sDKMq0tcoA== 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 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=GlVqa2qmkaaiQMe4N6o7Bdn+VWWRYcbCRi5kfE+AEcs=; b=y2yuMNXdA8MtNSwuQ+OFJ6s+1acdCDTG8U1Ve+cHOduen30yoNZAlsOhM7PIVvsalhclr+xgju3bljR1NSFgSX9eXVg9s8ZF0xu2XBERtknFNNzflA1eiePWtoPcdenG26TvIHc18w++L+D6DWM2sxtygZgl6rTi/RoXyt9cEdI= Received: from DS7PR05CA0094.namprd05.prod.outlook.com (2603:10b6:8:56::11) by MN0PR12MB6223.namprd12.prod.outlook.com (2603:10b6:208:3c1::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5880.19; Wed, 14 Dec 2022 20:01:27 +0000 Received: from CY4PEPF0000C971.namprd02.prod.outlook.com (2603:10b6:8:56:cafe::cd) by DS7PR05CA0094.outlook.office365.com (2603:10b6:8:56::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5924.6 via Frontend Transport; Wed, 14 Dec 2022 20:01:26 +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 CY4PEPF0000C971.mail.protection.outlook.com (10.167.242.9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.5924.10 via Frontend Transport; Wed, 14 Dec 2022 20:01:26 +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.2375.34; Wed, 14 Dec 2022 14:01:25 -0600 From: Michael Roth To: CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Brijesh Singh Subject: [PATCH RFC v7 47/64] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Date: Wed, 14 Dec 2022 13:40:39 -0600 Message-ID: <20221214194056.161492-48-michael.roth@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221214194056.161492-1-michael.roth@amd.com> References: <20221214194056.161492-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: CY4PEPF0000C971:EE_|MN0PR12MB6223:EE_ X-MS-Office365-Filtering-Correlation-Id: 8663c683-ff99-467f-5989-08dade0dfbdf X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ZveNpknXwjrnDQoLsSKWEAhz/NYf2kdEvJ1IE3GEcX8Hf8i0u8He+dteQrFLohtLbQztcQVYbyppdEUF79rupi20ct4PlotG4MQxaFaUCXeAfLPKoHfZ+fJ5S8Osw9b63Q/Q7KWAhQAMSTLf0c/QTw9vr2h5xmHX+QJ6ln2ax0hVmgw392Jz5erztSez7g8OAS+AW0gJYrBhgAV6YFJVnKjxKmUloq4QtGiXDKwkTcIAcrh0t3L/MA3g36LG+iqyJMz0EIij0Fhg1qqepZRVv+aYnNkhxsotn0udiwWSzG2JGkK6J7OspFsWfTs36O2z44NDnRpuOuxmS4Gzl6o/p9H/7JgDwSpIw6mkIA/ru0BRTefqvwqsk0OqdXa3revYQP7Wh9ky9YjUafq751G4tnT3+hNoRoI6GdiiYv/pWfIah0uUK0j/O5iSSko8u3+l2C77FcPB1PnsXy66cyRncxANdbsev7cD898kUrhUTR7b/IQXxg45KxcLbI9ATWhMahL/jhcrxA9x9sBAD5xlE9rRMNTuw219IFa3sJOfbTrSkYAJpqbK3Iik+wYuMpHpEvLxAXYvNSqM6g9bilC38FC/PRtEn4Rp0VtcfMOetQu8/UJJInxzqvKX49F+jLff882RdyFYAPjXAy7UbA1eSM+iT7X+Jcpi6aoqoIlyO+A1SCisQnrj9zWps1nJXJWRwFNxcLZM7WwZm1APylQr+Sn4nI1HEqGjaPtcb9zsxRE= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:CA;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:SATLEXMB04.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(376002)(346002)(396003)(136003)(39860400002)(451199015)(40470700004)(46966006)(36840700001)(36756003)(40460700003)(316002)(44832011)(8676002)(7406005)(8936002)(7416002)(41300700001)(5660300002)(16526019)(426003)(47076005)(356005)(70586007)(2616005)(186003)(70206006)(336012)(82310400005)(1076003)(4326008)(83380400001)(26005)(40480700001)(36860700001)(54906003)(6916009)(478600001)(86362001)(6666004)(82740400003)(81166007)(2906002)(36900700001);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 14 Dec 2022 20:01:26.6912 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 8663c683-ff99-467f-5989-08dade0dfbdf 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: CY4PEPF0000C971.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN0PR12MB6223 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 06F0816000E X-Rspam-User: X-Stat-Signature: be7depx87zatwpk9xbufcmrrobfb81xc X-HE-Tag: 1671048090-24113 X-HE-Meta: U2FsdGVkX19guoWTMOJXxEGm8CFfnAK3PWfvLgBkwuCdFxwOIVAKiXAF0fWd8OSV01ctVFJRgFXZiV87Serme4SJuZKo708tJ9ioSppLzIH1y9Ii66YvBCZ1oIMSG0NyXh8XN6CLTcgFuXOFK9g6nHMbZC+XKT/NmNhK+mN62lkl9dRQiPb0nLKQBUxfo+nl1Z+ol6+b2tgTD4Od28K2+vlLpIfwx7lI5Z2/BEFDS06aRhgxuKExuiAP8pcu7JeeD1PWmyzF3xj7f2iP8nbLTiXMC6wU8zlI6LDyfQDk/9zS5LL1ZnvZKkzuFTEJ0ICBq7cAykdt098fJytIIk10kKAbGGkm1dAXb6gkoPopJGpna+cJpt5d2uBTEXd9kOUQNUpahxcZ1lPkEqidekMhsS/lDxfjxYRZA5CMmEatVA0rtANXUPmgpxK6Aop0JMAFv3e5LUrjKtH745UJ2+LZIGhF4nD64kCuzHXpNZCzFLxVc0WokhQjh/9Vu1FNn68SDuWw9FNAL5AyffqZBZPIM2/W9U+Y4f6/84fstSFhXTngO+8O9Qhjzi/qTrx94FAu7JdsH60z2Hl5yrMlm9TrG/pv/p5N+37baY9kWX3VDFtqFwFPC+mciwkpOEHIHMUGVc/GT20cnhiHR2Zmcw2UKFy83DkE8w/X+q+7cgf6j8qegwayojVr7nM50TA/0j2ZsdQOB/dUU8C/w1d8MtnOFMqWMwKSNC2iA6gIX3g8Y15/Zj2b3vbEPNypQdmSILsvRr0nenfL3s/4jUOCIFfkXOPXkLQMtOKy3Ln4O8x+BamIXSZbVvOEAe4743gGu7wuH9jqnKKA5M3uNOq1OR4EQDuTuHhLNC5CeZDj+46GWHLTsEPprjikclK78il24JxUbFrttqnVHY/xzJKhuFN4C0wuFQWHiY3uK7Qgx+ZPA35fAO3RkF9EkbmRaNP0dnlDrbf+DVFtpn3XEg5fhH7 J2hRM5W5 +ucUD 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: 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 MSR protocol as defined in the GHCB specification. Forward these requests to userspace via KVM_EXIT_VMGEXIT so the VMM can issue the KVM ioctls to update the page state accordingly. Co-developed-by: Michael Roth Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh Signed-off-by: Ashish Kalra --- arch/x86/include/asm/sev-common.h | 9 ++++++++ arch/x86/kvm/svm/sev.c | 25 +++++++++++++++++++++++ arch/x86/kvm/trace.h | 34 +++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 1 + 4 files changed, 69 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 0a9055cdfae2..ee38f7408470 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -93,6 +93,10 @@ enum psc_op { }; #define GHCB_MSR_PSC_REQ 0x014 +#define GHCB_MSR_PSC_GFN_POS 12 +#define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) +#define GHCB_MSR_PSC_OP_POS 52 +#define GHCB_MSR_PSC_OP_MASK 0xf #define GHCB_MSR_PSC_REQ_GFN(gfn, op) \ /* GHCBData[55:52] */ \ (((u64)((op) & 0xf) << 52) | \ @@ -102,6 +106,11 @@ enum psc_op { GHCB_MSR_PSC_REQ) #define GHCB_MSR_PSC_RESP 0x015 +#define GHCB_MSR_PSC_ERROR_POS 32 +#define GHCB_MSR_PSC_ERROR_MASK GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_ERROR GENMASK_ULL(31, 0) +#define GHCB_MSR_PSC_RSVD_POS 12 +#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0) #define GHCB_MSR_PSC_RESP_VAL(val) \ /* GHCBData[63:32] */ \ (((u64)(val) & GENMASK_ULL(63, 32)) >> 32) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d7b467b620aa..d7988629073b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -29,6 +29,7 @@ #include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #ifndef CONFIG_KVM_AMD_SEV /* @@ -3350,6 +3351,23 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +/* + * TODO: need to get the value set by userspace in vcpu->run->vmgexit.ghcb_msr + * and process that here accordingly. + */ +static int snp_complete_psc_msr_protocol(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + + set_ghcb_msr_bits(svm, 0, + GHCB_MSR_PSC_ERROR_MASK, GHCB_MSR_PSC_ERROR_POS); + + set_ghcb_msr_bits(svm, 0, GHCB_MSR_PSC_RSVD_MASK, GHCB_MSR_PSC_RSVD_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_PSC_RESP, GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); + + return 1; /* resume */ +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3450,6 +3468,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: + vcpu->run->exit_reason = KVM_EXIT_VMGEXIT; + vcpu->run->vmgexit.ghcb_msr = control->ghcb_gpa; + vcpu->arch.complete_userspace_io = snp_complete_psc_msr_protocol; + + ret = -1; + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 83843379813e..65861d2d086c 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -7,6 +7,7 @@ #include #include #include +#include #undef TRACE_SYSTEM #define TRACE_SYSTEM kvm @@ -1831,6 +1832,39 @@ TRACE_EVENT(kvm_vmgexit_msr_protocol_exit, __entry->vcpu_id, __entry->ghcb_gpa, __entry->result) ); +/* + * Tracepoint for the SEV-SNP page state change processing + */ +#define psc_operation \ + {SNP_PAGE_STATE_PRIVATE, "private"}, \ + {SNP_PAGE_STATE_SHARED, "shared"} \ + +TRACE_EVENT(kvm_snp_psc, + TP_PROTO(unsigned int vcpu_id, u64 pfn, u64 gpa, u8 op, int level), + TP_ARGS(vcpu_id, pfn, gpa, op, level), + + TP_STRUCT__entry( + __field(int, vcpu_id) + __field(u64, pfn) + __field(u64, gpa) + __field(u8, op) + __field(int, level) + ), + + TP_fast_assign( + __entry->vcpu_id = vcpu_id; + __entry->pfn = pfn; + __entry->gpa = gpa; + __entry->op = op; + __entry->level = level; + ), + + TP_printk("vcpu %u, pfn %llx, gpa %llx, op %s, level %d", + __entry->vcpu_id, __entry->pfn, __entry->gpa, + __print_symbolic(__entry->op, psc_operation), + __entry->level) +); + #endif /* _TRACE_KVM_H */ #undef TRACE_INCLUDE_PATH diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 732f9cbbadb5..08dd1ef7e136 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -13481,6 +13481,7 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_exit); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_enter); EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_vmgexit_msr_protocol_exit); +EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_snp_psc); static int __init kvm_x86_init(void) {