From patchwork Wed Jun 2 14:10:21 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294703 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84FC0C4708F for ; Wed, 2 Jun 2021 14:12:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6E3D3613EE for ; Wed, 2 Jun 2021 14:12:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230145AbhFBOOD (ORCPT ); Wed, 2 Jun 2021 10:14:03 -0400 Received: from mail-dm3nam07on2077.outbound.protection.outlook.com ([40.107.95.77]:45953 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230204AbhFBONe (ORCPT ); Wed, 2 Jun 2021 10:13:34 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KzKrpe5TwxP4JQFyBdcBGGvDvsIoYvalSN7uOgfv5XYzeCau+cX3t86HvSoAcO3vI/nQfCMBSR22VZL85/jT5EDDQ7VuJWV3sXDHM+XfslmpFUpcdI+Wi/1bCzSmEvxYi7D2ed3onKd6J8Sg31ClLMnPHOFkZ42VwjLJUGI7QYnqf5Q45ZFol4kOkfBgFk1i+kkAYoOJERosnir1sDnd6sm2npeO5AN4fQyekXRPHt+uq8y1hHQoInMwjaIJc56L5Wr6kXBWowPlWu8MP1dURg+LQL8ZGpvD5MK38LmNwl0l0S4fxYa1TifcsRt/XnEM43/UPG5FHbhWX+I1O8bDAg== 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-SenderADCheck; bh=vA9Y7FTLsl3X3KXsPSydxPwo+ZSKbBDJMa3NZmmwxmI=; b=kwCY2Km1qEOpqGmLAySR+S0N5EZWD6CfN4XN6jmWs0g75ytdnZgtpbtaYkhI0fT3e/bMAxb83YUDU3Wyu6jWPPh23AAUiafLqeZQd+By3wHqPLo2Q5iEl7e+Y2QnSN3qmRQuARdr+ZtiUVav2P79stQBKcLNB+J3IGasXau1/OwAfzZ33bzFD84GUKdayxY3kTtzkPEVCJSyw4XRVPUNG829yCW933T4Q3j6Lgj1nox2H/y6LXRMeCtrO+R/sJSAYF7Lv17qenFLfy10BUycX6fK56Ncz49oIUxkwkkOENh+QGzZM414H4M2T9lAGLwz3i7rYK1VFiFZ9Q6Eh1AD2g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=vA9Y7FTLsl3X3KXsPSydxPwo+ZSKbBDJMa3NZmmwxmI=; b=2GouCPbDU5yVrZUytDaADmGSJOcD6v00kCxjh5zLr+EIg+lDZNe5kcNGIVV+7mkQOuo2hG5xOOOK4VEHiFQGjV1NFj7fygCfoPHM7y3M6D8Ni67qDSjGWCjQxkrUW4TqubqDrzc4jsXZbEjQOhq/2GzhI3q18U7y7eCca6Y1lpA= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:30 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:30 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 01/37] KVM: SVM: Add support to handle AP reset MSR protocol Date: Wed, 2 Jun 2021 09:10:21 -0500 Message-Id: <20210602141057.27107-2-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:29 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 47b0fc7a-3848-4e22-0bbc-08d925d0516f X-MS-TrafficTypeDiagnostic: SA0PR12MB4495: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: HAdQCQYuW9bo+ta1Dw6hsrXKWyk1kMhpx2riMl7lVsdWaAH9Oy6hG3QiGDZTHYJ8o6u08b5Sc1IqfqbDfdDN5ok3MQ/RLirldHAdsfa/EQ0rJma68aIM5JLDXAm2OLBneWdBkukTNP1hDLwE8AI7MuEvBvAx+QZkF4zZ5a1qs930uxUuwW77vIhLVIWZZJ3h6R0kJYm5mSo8mH7CB3UY3ZGkeKk9BlGWeD1eFGSnOSQlNiycAxHJQxlum/hu0HxPZo9FB+meGNNUGv7ejQ8ZLRIsUOAGe3rvZMSBhADVUeWugIyzkKg46MJzFPlpONfNIWA5cpZQXBXZVe0+hJU4CHVXYo1/h+tLBWoPejo2n4Xiv60AjoyJoa1SLBFN4vBxuClYaP1WIGb2LyVhiygjes8VSjha8lXmYZ6RSup34kQKbjC+Lf/rmKTHM+vyZZDs2NEvcTwPQogQehKz/XTrnvbxWgT5qxIRhbb34CE/+/oz9zN8KB3JL1kepSbE6Wru3qklUSCeRA7Ns02Wn8SBGMOI9sxSp5JbczMtOXLg+P96oGwSxlBhTjq730csNcX6clN5mJTYRQkr7kb6qprAfztDOE5SCePw4FsUdRe1E32Zm6hqMQ4Pm0kEdKkIVO9eRY8NGo4D77EZ4Ct5zZgPUw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(396003)(346002)(366004)(39860400002)(1076003)(54906003)(44832011)(8676002)(2616005)(956004)(478600001)(6486002)(316002)(86362001)(4326008)(7416002)(6666004)(38100700002)(7696005)(38350700002)(52116002)(16526019)(26005)(186003)(2906002)(5660300002)(66946007)(8936002)(83380400001)(36756003)(66556008)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: VOC/OmfkogO4b/NPmeECsMq6EP7hA04zC14KYZc6jaRwrAMfUQYyS3y9DyYkgGcy/abwkDD3jJ6Zy6+aVnQJvVs9Ky9QDbjrNUtfcqJi6R5hqtb7tBG3DsxtDHIkOx1Efh+exn7+2qmHyMss8J0KEKB+y5pwEqqljEEpNdfuKkvrUoJf+5IJ+i1Smoj2kf9FOribUAxLGeBJjfKJGFIaUGfQ8BBQzVtYe7NvD+rDm4lJZqeeCMU0Zf+TNYqQPSnGekjmhL2xVvLLYbhf7sMm/e4xlDyJ/E4sw8A/qXwuowsy+V0oaFCEfOI9M0ZRgWo2Jv1rxACuEaY+2ilp4piZV5ZBf1dy7Wk5cpusm+/aWEKW4ypjLXPD6Yc/cjq+bCxPBVy+yKipstHIb8vONA3bHUwvAKBChjTNCA07txIylLouVdIV23hjFho/iLWVmuCquBn1wwUJsTeSlWWmHkYvLmv9ErP85FQJpfKk1AiA4f0JbTdwZ3jVz7jWhfeOj1z/tKawvWgieLUzl4NZXgE3gtZPWNPjPSQzh5EfzwpyDFiJigy451s1sssSVGcvHiyS/Yi4nov+6pTlPv9YWP/w3Mj7j3BTofYrqcPzZIqphwZYMMq22m1scOns501WXEPmuKK08rPoG+fQTo+uXoVUFe/pHf8GeesBEYXKDF4WXQGCV2S94RKhZEZpO3BU3rPBsesxpXf3gPg0sDXLQAK36f8wcwQ1tU9WSVbtSjnxUAAbvPWmf1K+6TTeIzjgUZfwXOJlNCpMOQZBkspvkiMZAa6UKXwBBwlxGNlOVdMyVilxeVnIyfzIK4FYUfDiZN59vpnTz5R/cq4edXoIVr7xMXWmPPj7pV5wavxboOMJkRCcKSyjT7fe5PFx/Tek/7DXaZYsI1WSUjXKu7v8ba5lO5ufVdsw9VPmkbSC4IbWtefwtGWWqaEitcVpybK/umKT9N3qPhUQmo9/fJM63Wy4NlPynWY9nRLoUzKiQM/JHl5ToZiE543Ml+Rh770VoBAi+8cBl8TRf6IPgoB7Ltjf7WMdqXxDcv6S+Z8jSWPalCVy2+0FtBsZJ0dDedVsHdyXYl/RGYiOrNtB0p9IrO0fAY56vXzda1Vc1GBjshtblW5XB3qGTMWbc23TJEs2A3mCPPk5KH29I2UGq+aMHdqlT9MnTLUDizYmdlke/yh+DLyLYIi2jho1mTxMoE+hTx+CYg3KBrt6SawTcp8RzH/7mMMDS8mRbsbGA9mFSlZ+sThRQNGDzjzATQdpTTnwIwi40ZAO7dtYLc6bI+vnskRijyx4J5M2YUOeaIZHZhDHc4MIRtphpPpyaHHy7ri9ohTu X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 47b0fc7a-3848-4e22-0bbc-08d925d0516f X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:30.0972 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: I1Wfg8N+dm3UZe6n+jcIv3kRDM7hgWmaNaK014CFhnCE/NDd1yJLN3cjxhWRVt1G9MDOVISHyw3lAuHlnUNCwQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Tom Lendacky Add support for AP Reset Hold being invoked using the GHCB MSR protocol, available in version 2 of the GHCB specification. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-common.h | 6 ++++ arch/x86/kvm/svm/sev.c | 56 ++++++++++++++++++++++++++----- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 47aa57bf654a..97137f1a567b 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -45,6 +45,12 @@ (((unsigned long)reg & GHCB_MSR_CPUID_REG_MASK) << GHCB_MSR_CPUID_REG_POS) | \ (((unsigned long)fn) << GHCB_MSR_CPUID_FUNC_POS)) +/* AP Reset Hold */ +#define GHCB_MSR_AP_RESET_HOLD_REQ 0x006 +#define GHCB_MSR_AP_RESET_HOLD_RESP 0x007 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_POS 12 +#define GHCB_MSR_AP_RESET_HOLD_RESULT_MASK GENMASK_ULL(51, 0) + /* GHCB Hypervisor Feature Request */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d93a1c368b61..7d0b98dbe523 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -57,6 +57,10 @@ module_param_named(sev_es, sev_es_enabled, bool, 0444); #define sev_es_enabled false #endif /* CONFIG_KVM_AMD_SEV */ +#define AP_RESET_HOLD_NONE 0 +#define AP_RESET_HOLD_NAE_EVENT 1 +#define AP_RESET_HOLD_MSR_PROTO 2 + static u8 sev_enc_bit; static DECLARE_RWSEM(sev_deactivate_lock); static DEFINE_MUTEX(sev_bitmap_lock); @@ -2199,6 +2203,9 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) void sev_es_unmap_ghcb(struct vcpu_svm *svm) { + /* Clear any indication that the vCPU is in a type of AP Reset Hold */ + svm->ap_reset_hold_type = AP_RESET_HOLD_NONE; + if (!svm->ghcb) return; @@ -2404,6 +2411,22 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_AP_RESET_HOLD_REQ: + svm->ap_reset_hold_type = AP_RESET_HOLD_MSR_PROTO; + ret = kvm_emulate_ap_reset_hold(&svm->vcpu); + + /* + * Preset the result to a non-SIPI return and then only set + * the result to non-zero when delivering a SIPI. + */ + set_ghcb_msr_bits(svm, 0, + GHCB_MSR_AP_RESET_HOLD_RESULT_MASK, + GHCB_MSR_AP_RESET_HOLD_RESULT_POS); + + set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2491,6 +2514,7 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = svm_invoke_exit_handler(vcpu, SVM_EXIT_IRET); break; case SVM_VMGEXIT_AP_HLT_LOOP: + svm->ap_reset_hold_type = AP_RESET_HOLD_NAE_EVENT; ret = kvm_emulate_ap_reset_hold(vcpu); break; case SVM_VMGEXIT_AP_JUMP_TABLE: { @@ -2628,13 +2652,29 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) return; } - /* - * Subsequent SIPI: Return from an AP Reset Hold VMGEXIT, where - * the guest will set the CS and RIP. Set SW_EXIT_INFO_2 to a - * non-zero value. - */ - if (!svm->ghcb) - return; + /* Subsequent SIPI */ + switch (svm->ap_reset_hold_type) { + case AP_RESET_HOLD_NAE_EVENT: + /* + * Return from an AP Reset Hold VMGEXIT, where the guest will + * set the CS and RIP. Set SW_EXIT_INFO_2 to a non-zero value. + */ + ghcb_set_sw_exit_info_2(svm->ghcb, 1); + break; + case AP_RESET_HOLD_MSR_PROTO: + /* + * Return from an AP Reset Hold VMGEXIT, where the guest will + * set the CS and RIP. Set GHCB data field to a non-zero value. + */ + set_ghcb_msr_bits(svm, 1, + GHCB_MSR_AP_RESET_HOLD_RESULT_MASK, + GHCB_MSR_AP_RESET_HOLD_RESULT_POS); - ghcb_set_sw_exit_info_2(svm->ghcb, 1); + set_ghcb_msr_bits(svm, GHCB_MSR_AP_RESET_HOLD_RESP, + GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + default: + break; + } } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 0b89aee51b74..ad12ca26b2d8 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -174,6 +174,7 @@ struct vcpu_svm { struct ghcb *ghcb; struct kvm_host_map ghcb_map; bool received_first_sipi; + unsigned int ap_reset_hold_type; /* SEV-ES scratch area support */ void *ghcb_sa; From patchwork Wed Jun 2 14:10:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294609 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BA197C47083 for ; Wed, 2 Jun 2021 14:11:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A65E9613EE for ; Wed, 2 Jun 2021 14:11:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230153AbhFBONl (ORCPT ); Wed, 2 Jun 2021 10:13:41 -0400 Received: from mail-dm3nam07on2058.outbound.protection.outlook.com ([40.107.95.58]:30496 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230389AbhFBONT (ORCPT ); Wed, 2 Jun 2021 10:13:19 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=W6lIHJW7UA1kXsGyRlx6cQSDjrln0hNrc+SnCg/vX6LpocvY4rojJktQbfB8uwHZiX5sf1PQ3toF7GkyPPZ5jZHNirGNskV6scH+PSHUZqI0gf0TX4QhyaxBBH7drtu1ORvOWitL8WtqrSoDpfIL5hhjNXeQqGPnmqewhygp37nfepcMQYqFIpUs2KD5vPl7UM8TMSxv1VUAWAObfbsEvOfXARCu4T6HTKtNmuPrnxAP81xDOh83gUme3cEiDJkKjeOgsUO6bfv3/xMZKQha/mPUrFQ6yUhKG6GWfrXq4wf1BcKdM3EeXKdJDPzUeB+TPhQB/buUl4nCC2zMpGpY7Q== 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-SenderADCheck; bh=LbkcD3u6VPMFxYdkbjP4GT3Z766pfi8iMLV8g8cvHI4=; b=a+u936oQUEO2meQfDT06ysJnRsyGMCLFpyqnFBcTLnQ41c53nettQIJMKcGKv2ln7m8RBFBowCiNqG2ioZrI2JStdL/sjkXMMLLdfz3OywZbGig+kI9NgUlOmmHEhguC/RjbTzUs2T06tktohlk6POeXeO50xF+IaBFJhRZIv/XASdAYENyo1IwNCZSgft+evEycUmp1EQDVsP4eXpPk0fPDsa29XPbx2osUUImTzNQjrZHCV4p/c0wA2fFfFG5P1c++WCWBtuIa73wnSu39KLnmxccS230HI1iwEv//VLw3i+OvQeIpDqBxjuCexbPMflDqlrYvE2sJ7MNFWQY05Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=LbkcD3u6VPMFxYdkbjP4GT3Z766pfi8iMLV8g8cvHI4=; b=RQwSnBaDL+OUsO8ZSQi9otoshZ6yZtSkje4YgHHBF1lytUReB61S/YxDO0G/jZvxhwMg6IeHypaVhuV9KIX3m5875+4zQ0TIfn6zNoa2bXRwlWjm/yJYPaVHzqMLgEj6lAsy3EvMOg38ofkpE/4NoeSfiaOFmUOMpSUl2OIL75E= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:31 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:31 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 02/37] KVM: SVM: Provide the Hypervisor Feature support VMGEXIT Date: Wed, 2 Jun 2021 09:10:22 -0500 Message-Id: <20210602141057.27107-3-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: b5535477-2af6-4d80-260b-08d925d05223 X-MS-TrafficTypeDiagnostic: SA0PR12MB4495: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2887; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tVTr0QlReaqqHVltv5poe2/Nq/e9GRk4pAxSvhN2xQnIA77z0cZH9wIKvvBA5wIdnnc8bMZmjUWmQKfgjYaAGro3HTlrK8ic0hPKgI5xNOY8mHKURiIb8yf9Z3Q/z/h4kN+UJDNjSxcPqLUtMWnYUzWtndD9KaPDoUaFWxAK7lWNPDIPyaDfkvD9jbvTMNikl227hgDha93OSvcB59daE420IKXlepjxK26635Z1VT3xQFdbXXXYzmJbRLluc633V6yqgJitKg8wJo9B0OWW485j0Og6IpJhnYAzi7KbeUdBrZEjH2m4UiXTdl/P3aWrIax8Bulnex4M1omCz3rOzgm9QbQ4m1N5wsqnQpoIWDzkA5lEer0bkozgWZOLq1KMSR6J8qTkxCxkMvGFgOv5RG/MMES24HP+XEzgrRlNhM+EcIB8jyMdYNiHT0N0F0hZda0JeFjDEu0xkQzRLIkaV/gj9GOqNJauDIPb3TG3M0AvFFSvkDSR8SpSaDUXXY00Sc/94MawIwi5aOW1vY9CYu+iIJUe7vKf9HI3b4MY+RO+GwYDqV51Y8ZcCeRltuQajgqMZFUm5vrjt4wVfzMwRuJC0OKUKeYEKZPotWlOQqIOApYGkujrW0GndahKEhYRSlGb7u15K4IgTwqJ1ADljg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(396003)(346002)(366004)(39860400002)(1076003)(54906003)(44832011)(8676002)(2616005)(956004)(478600001)(6486002)(316002)(86362001)(4326008)(7416002)(6666004)(38100700002)(7696005)(38350700002)(52116002)(16526019)(26005)(186003)(2906002)(5660300002)(66946007)(8936002)(83380400001)(36756003)(66556008)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: xgOTqBHal5rCIEAhf7l70kH1DDkGOKa2qy5oKEr9QTXYFlQUq1IVJcOCQe/NeAWVOX2fZh9NalHl1NOOh+f195Azj8iEBMKXzCgWt3lSRDhtpEaIdEMOU9bXdHCDB9QxSxb/OzgzNgcgZ4bZ1cFBfPpHmZdMjxYqyX+Xf9fHX5Ijd49wrW6iSusEzaM2gLz/fEvAzLow4oq35Ts/wtaBkrOgpgMQVo5t8KtnKB6bg14J/fUmWsvsoSJDNQq8vVdP0ntD8bL1lPN4CnAwBYlD0ZCcsk5LS3DcSyjms9kO68lTlMPjUzkczbhJHAVqemdaYHDtwGE6cFijj22OxonRNMXbDMyX27zdf3pmaMxPHxRBeBD3olLUOQ0+6+pfsMXnQ6nX7nLUT2q638KsPUHHWt2kRO2mC2+OM6SA4gbl1tXGyZjEbqLSPPQJGDpkevbh5H3JP/KRYMBHWYgAkiNyxTos9Azznoman7SfQMD9Xem4Q86uhOi/ykWTitGdxSbkkwg+9f3U3hJ+vNgTABQTnx4safeGT/fxa3/Bq+7TcmOzD0d7ntTUKaBfZFU7cG3jGuVJwBIsn27A4DDynwcooum60ACcAmZcLlr9vbzlDmfVSP+6EXeUHgVKuOac8tJ56Khc4D61URNjowgrh/PknJNcvEeizTpviDL8RtYGX24kmFUWnzC/82to8VuQZXWlBydLx3OCsxd9zK83l8s/0BMuCzgqnkIXih9k+XqGrNzPT7ILloxWU2miHJaBVixu2uXnQwUyjdvUXx2QHu00NcRpWBEycvO2X7Jm4QGPv9BiZm8ylHbiHENWUhiRlAx9b+nQi3SoCCvSSsDYerOWcrcxb6bGFTFaoymqhm6lYCz1rT0X/Cq1TO4SmZKI9OrJPJoAr4FcjnUodM/eutmIGI21fIIt7ZPDrUDFiseoHC3id6fk+u+nWSMGQpEHz9V2h5a/1RRd2d2YXaAyJfuEjKZGlBSGnwyeKnnnKe1L+9Wv5SbKy1/VtQozfilvZx0IGkp0CXl+e7KDYNRXhen05DuJ21vNoN6V5/1NJ4Ig+pZshpLxdMHWxfC+JZtycseKqov9kTOOBpFEaOY/7ZqjccObKhYm9r9wDqMwsGuCHxveId9ji5sLRdotksu1Jlq/owPR9pfRv+Zw98JTmWpo90RFi0At8Khe0x/u/R/IiTLDCU2ccWefmUMWMTQataXfzqf4Xav3OTZsrt2mYlZZ4Bkfht20aWZ515c4T19MRb1xQa0PZGEChmLH4r7ni9m9uWypdram+m2zzOHcBj95/PL2qrRrsHiLZSuN+pl8vBO8FzuKc1WfmQx1LW/mGFai X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: b5535477-2af6-4d80-260b-08d925d05223 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:31.2715 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: dFb5UJIsU8d9Gy2CWu8EUFGeWkM+aN59t7MstfjuV47lMrnALVmh6EbtRpAG70OZXQX4/iA/UMFSE7tIPu97xw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Version 2 of the GHCB specification introduced advertisement of features that are supported by the Hypervisor. Now that KVM supports version 2 of the GHCB specification, bump the maximum supported protocol version. Signed-off-by: Brijesh Singh --- arch/x86/include/uapi/asm/svm.h | 2 ++ arch/x86/kvm/svm/sev.c | 14 ++++++++++++++ arch/x86/kvm/svm/svm.h | 3 ++- 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index bd64f2b98ac7..de977dd167ca 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -114,6 +114,7 @@ #define SVM_VMGEXIT_AP_CREATE_ON_INIT 0 #define SVM_VMGEXIT_AP_CREATE 1 #define SVM_VMGEXIT_AP_DESTROY 2 +#define SVM_VMGEXIT_HV_FT 0x8000fffd #define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff #define SVM_EXIT_ERR -1 @@ -224,6 +225,7 @@ { SVM_VMGEXIT_PSC, "vmgexit_page_state_change" }, \ { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ { SVM_VMGEXIT_GUEST_REQUEST, "vmgexit_guest_request" }, \ + { SVM_VMGEXIT_HV_FT, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" } diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 7d0b98dbe523..b8505710c36b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2173,6 +2173,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: case SVM_VMGEXIT_UNSUPPORTED_EVENT: + case SVM_VMGEXIT_HV_FT: break; default: goto vmgexit_err; @@ -2427,6 +2428,13 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; + case GHCB_MSR_HV_FT_REQ: { + set_ghcb_msr_bits(svm, GHCB_HV_FT_SUPPORTED, + GHCB_MSR_HV_FT_MASK, GHCB_MSR_HV_FT_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_HV_FT_RESP, + GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2542,6 +2550,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_HV_FT: { + ghcb_set_sw_exit_info_2(ghcb, GHCB_HV_FT_SUPPORTED); + + ret = 1; + break; + } case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ad12ca26b2d8..5f874168551b 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -527,9 +527,10 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); /* sev.c */ -#define GHCB_VERSION_MAX 1ULL +#define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL +#define GHCB_HV_FT_SUPPORTED 0 extern unsigned int max_sev_asid; From patchwork Wed Jun 2 14:10:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294607 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 54E21C47092 for ; Wed, 2 Jun 2021 14:11:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3DCE7613EE for ; Wed, 2 Jun 2021 14:11:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230409AbhFBONk (ORCPT ); Wed, 2 Jun 2021 10:13:40 -0400 Received: from mail-bn8nam11on2088.outbound.protection.outlook.com ([40.107.236.88]:32416 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230475AbhFBONU (ORCPT ); Wed, 2 Jun 2021 10:13:20 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OngY6rbT0wypgMLP8aysX4zw1vmiqV6M+fl80cwoEnIpTX/Zm4pfDtGukZzouGCNoA7Zo97OnzbRU9MzJbIsj1gCZLrd9x18dVYvF0FwgkJQMAzuBz2qKVSHw5fKF+x0jk5VpYKMFzlJj5lVseLj+ivaYmPz3DpffRMYcwe8TnhyDNd4PKQUa4S6qwMgt3BBVgL1hChXdA1mTNxZy6eQnVFNl+WbpUKk4cbV/eOHsZ0pmZYkUOVWFfQkMD1CY60Or1L6Ag2A+DwwDgtbLuvBiT9fEDmBIxsvilWR68JLEfUbORaSj4bEiAH+4oLycY+mDR4A7pnXrac1mENFRWo/Ag== 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-SenderADCheck; bh=b7OcioCq+8o2r70L9N0hO5CldAYJzBAXRKvcHyOO4Vk=; b=WwlM16I8VM6iLv+PfMlal21ZXShZq3AtGaB1SxxMZvGUihdqeeG0V4Tgq+auz9qjczd8H3XVYfvhmBXFAsmJF1O9UhnoZrG5yPnhNpFponUw1hSoi5qpVfJYTKnFO9YhwLfH5v2HGcAlVI711mvs63Yarnd3Wn5yJoNN8dbJgO0sDV30KjyAXhvsRnPFbwvlmFDrF3VeR88aXfLpd+Zk9XtuJPkgfXmkWo0hNe80NPFldHoJZd8k0bgwi78myHk6dUXMH0349SkzbVw+nfMjFrbZ9L/1JMrmd4UwFw5LQJ00xxoVWhwSPpOhssSSjhoRJQFe4jKNbk83B8AqjIka8Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=b7OcioCq+8o2r70L9N0hO5CldAYJzBAXRKvcHyOO4Vk=; b=D41kZAffHmqIGfImSfUIeJbQTurmH3OeO/rPeRJO2sFRST6KHYzWf2y8Wm5JILIlEHY/vvqQCvXvvaeFX1rCp6BgWK5dfGtaHOWBWE528td14Ypv0yZTjvkclP4V3b+3rNJjev2Lg6dh3WrPO5aW0Yp0PQEfFT99vH5a1SSnrnE= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:34 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:34 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 03/37] x86/cpufeatures: Add SEV-SNP CPU feature Date: Wed, 2 Jun 2021 09:10:23 -0500 Message-Id: <20210602141057.27107-4-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:31 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9a896f7b-c3de-4879-a4d4-08d925d052cd X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: XFtAUz7dNHfIKvIYaty1za8+kv4PhI8I8/sR6eF80kVnTgqE0WZpU8ImGjIFVTConJO82RokHNdD7EXyBkFW+0iS4C2shKdDvQlXoEUm5r3BeFQLbhkBaak7zPRBGoTVqYVDOAUunZhRPxjO/dO6IuQ19S5In8ohw6aWvxRf/O4H6D3gugIVDASvRAMfQtIRz3iiOBJBEcw8UMukdmDcUXRYo+bfKU/cYyUMKGp4z1x1fnxgwPPpxJkuX/A7W4CsAIvAbC0//GPajue63mGcRKBMVy5zTdL0+4GJ71JMJoUCKKTwnwOWeshPTOJvelmCwKtFqECy+Cg4erj+bFZ8eQhlxQDew+P4spp8Kjc4OwH0M9wP0vGSQCBqUCBtus8FOMYWiFE1Wnl6RiBIi5sqBjSTeiPYiAKwYhjv20mdtdojGJHS7YhcvvNnHEfbEBk6U8B4LteP1ktN8la6b5LNnFm2EltSgzfX3Rp2yM/gWs4ToYjGK6P9/77TdoVJ1Snjh73DUPW/xe8ZcIphfutMVMfcgyc9LPeDItj/TjeYxhZbTlqaI/aseYRVpwbwFmdciCOaHknnLmJ9bP4WfM+P45rIDv7Nokqvod3ELB7XRUo/LKNvUtuSx0l5hWuit8YxLkefapfUdMHxW7HQHpkxLw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: vtvrakr1FW6s83Go0DSwwGeyF3mRP+p+2hgnPQkvF+EXJ/QDKydOip7vXJ0AV/n2j54AqcLt/xnuVd1vlGlv2ZLXPj6sIQot+t6mpoJn68IPaWU7Bun6oL2MSwlmcGT2XNkmTtW5REiVxMcob/aXqcKdtguDKi4FjgEnIFGQVXBOxSqSCCrCK0VyZQmXqa0D4EosJQCIfVOjwti4hWTQUVTr53EgzaDggVvBi7RwGqkn7rLQ4B6W4oinVHfwrQpUb/8iYI2vSQoYF83Vl+4q4rlBwQPzRqC1WslBy6CTzN5GMnuPknLSzvuC15s4+f9GiLn4HYteA2LcuVhrnT7ckIIBjccEwI/emoquUvitIzlRG2DZ8YchqZTyXPbzcJG51zP/lkmuHmEp9aL4bm5hFfYpEYfIq/8TBizPHVbjeZwRX2nB2S1OjTTAHC0u3KWrumCvU+pJgNZEI4XPieBDM1XFCrrMB6//fDUIkFvfJI3Zs2TmoXqXjRsbtpy+yKcdUeSlv8LRI/0aLYYO70mW7gIl6lsYsFfmMLiGRlxLNEvNbTMbOONdNg6SVoLb4WhFhUoSDOMnYtMA8dC7+3EA4U6T8GCeibnbYbosTLgHaBr37NbGSJufSd16Yva3/CvUdx1rtPAoHs6yQbiE/Vvb+5sPeH+hV//tS9SEp5PhqiADVUzxP4E4mTxuQ1dHMDLoIyJX+chp1+qrVDD/ao7v6xpEE7VMm1K12Bk8hjS0RqlQFN90FtjRcdRmWtnEhsDf6CDlgbBFvrJ5Zj3LR5EvRZ2tbqAsppTQ9biZco/GEkfXTIws1M9aZ1NLhr6zRDzEyKSvpiXoRnTc/HCmUgWCIdosYectMEpM5VjjKK93qwC6KOGmw6t4ErEX/Gq382JjPigIOdgMyPvMAHgK5kCoXUI+niUARNELqpM246/cVH0gaLdBHLu6VqDAYo9YggYb/US3QvCUIqAQDCwURVsQuzkhR4EPP2eELIFuIv3jNqLHKoNEgVykBMuyUouB3ZE5enf7uzgGs4h3i9DgIWM0TKscTEICyt50kevA2oFMHQ/9z18Xx1B20lAabTBGYVwWZM0rmuKiYBSwYroWfrsvWNKn7gGKxjjOU6FzqKHD5095f3H28cJsDuSMUB4TexdCjUgoH1vCtNxbeoUEKJhiIw4Q0HpIUDwjJ6IKGZ77BC39KsKhCnP03249wc2+99dQ0ovI+aWJT0HR4onZD1mLOPmdyKiTj5/oL7m7kcx3omc5F49pc2OeUiECqMR7TS8vKPZDpAWEzZ4uCnDyG8Z0X946MLFFknihwN33+P5hzecuvNE8udyYPwjhub69va0q X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9a896f7b-c3de-4879-a4d4-08d925d052cd X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:32.4178 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: AB/Mf7eSNU/c9AgfTggRoFxD5RW8XWWy3pJDshAYsb6GKIu9Uy+IAd1AeZ4bdgXxEEFnpj+IIgY2nsw3k7Di4A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Add CPU feature detection for Secure Encrypted Virtualization with Secure Nested Paging. This feature adds a strong memory integrity protection to help prevent malicious hypervisor-based attacks like data replay, memory re-mapping, and more. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/cpufeatures.h | 1 + arch/x86/kernel/cpu/amd.c | 3 ++- tools/arch/x86/include/asm/cpufeatures.h | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/cpufeatures.h b/arch/x86/include/asm/cpufeatures.h index ac37830ae941..433d00323b36 100644 --- a/arch/x86/include/asm/cpufeatures.h +++ b/arch/x86/include/asm/cpufeatures.h @@ -397,6 +397,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ /* diff --git a/arch/x86/kernel/cpu/amd.c b/arch/x86/kernel/cpu/amd.c index 0adb0341cd7c..19567f976996 100644 --- a/arch/x86/kernel/cpu/amd.c +++ b/arch/x86/kernel/cpu/amd.c @@ -586,7 +586,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) * If BIOS has not enabled SME then don't advertise the * SME feature (set in scattered.c). * For SEV: If BIOS has not enabled SEV then don't advertise the - * SEV and SEV_ES feature (set in scattered.c). + * SEV, SEV_ES and SEV_SNP feature. * * In all cases, since support for SME and SEV requires long mode, * don't advertise the feature under CONFIG_X86_32. @@ -618,6 +618,7 @@ static void early_detect_mem_encrypt(struct cpuinfo_x86 *c) clear_sev: setup_clear_cpu_cap(X86_FEATURE_SEV); setup_clear_cpu_cap(X86_FEATURE_SEV_ES); + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); } } diff --git a/tools/arch/x86/include/asm/cpufeatures.h b/tools/arch/x86/include/asm/cpufeatures.h index cc96e26d69f7..e78ac4011ec8 100644 --- a/tools/arch/x86/include/asm/cpufeatures.h +++ b/tools/arch/x86/include/asm/cpufeatures.h @@ -390,6 +390,7 @@ #define X86_FEATURE_SEV (19*32+ 1) /* AMD Secure Encrypted Virtualization */ #define X86_FEATURE_VM_PAGE_FLUSH (19*32+ 2) /* "" VM Page Flush MSR is supported */ #define X86_FEATURE_SEV_ES (19*32+ 3) /* AMD Secure Encrypted Virtualization - Encrypted State */ +#define X86_FEATURE_SEV_SNP (19*32+4) /* AMD Secure Encrypted Virtualization - Secure Nested Paging */ #define X86_FEATURE_SME_COHERENT (19*32+10) /* "" AMD hardware-enforced cache coherency */ /* From patchwork Wed Jun 2 14:10:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294701 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 327B5C47083 for ; Wed, 2 Jun 2021 14:12:11 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 108176140C for ; Wed, 2 Jun 2021 14:12:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231363AbhFBONu (ORCPT ); Wed, 2 Jun 2021 10:13:50 -0400 Received: from mail-bn8nam11on2066.outbound.protection.outlook.com ([40.107.236.66]:15585 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230029AbhFBON0 (ORCPT ); Wed, 2 Jun 2021 10:13:26 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZjUs7zkZyX+mUjz95dZd6sRgSx1Ea6kv1v8iRGEvX9WuB2IwYM0oHGwQ/rP1DLeECNUa/yjWEb64tBYQxngIWPsBiNsktMldAmOT8hwdfmAOfbUW2YgcndfBNzdDhye9Kk/BMC36j+MpEq/bPVYUALg2i+n18PkZ1jvKwhwX2I+ITsIuP9V9BbwzPl+n/v5GFTTDqNEimv9uQnRhrvUxuJEsofbbPyHRAZjMu7vOjVbgIkPV99mYs/rxacEFiPLPVCDsfmrXD0RIO3nJHs7XdttfTkObqQcJLFFPQhRJNHl0C1FVPX76phNx5vJlxHZc559s4lvmbE1iJBmD3Lpwkg== 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-SenderADCheck; bh=1UphxcpY5uChYPHuOjoorgCa1mz8ved+dqYE3LBHz70=; b=fvtcJjR3tD316KOLsgkuOxYiWBoumYB3DRA7NtHfG5gvGmyfnVbMZJg+uR3xwcSdylWAEkUX7Dihz2s1gsEfci5o5sIDxlRCr/ML03iMzzZ974y0IEbTqp5/x8Twi4kUGE4/GAnCCToXoASKgUucy221NHwCJp1VvuxXP8NE0AIcvsqRoBZd0l153ofL79hOCg7yonlqJ/KF/72xro68pdqIz4EZz9Mf55cXEwE8wGAuRPUo2F7FRNV0bBAkYuHbbBkyww0tfTggpNdoEodkDJS0sAFpstV1V0TuQDE6EbnXo1Hqg4Uuda/OTsnMs0DHwFHq/IV6sJlL5cdkLfaD3w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=1UphxcpY5uChYPHuOjoorgCa1mz8ved+dqYE3LBHz70=; b=3YaLkSTOm2nHMb+lmDc2TvCsa7Cuja5HBxLF9X4XhJTEK25hXBhT1UIhu3e4hDm1KTBl0TDKZ2jGPXoTo+vUdlSmh4nvWqmC2RQi/t0+6wScVsTQv1fviOy8eXSmLIlMqm2K5/iyl/oT13RKfsFhJ/wzfINXuaNQ1cMa5XxzG3Y= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:34 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:34 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 04/37] x86/sev: Add the host SEV-SNP initialization support Date: Wed, 2 Jun 2021 09:10:24 -0500 Message-Id: <20210602141057.27107-5-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:32 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 025ba9c4-ed41-4ca3-e2fc-08d925d05371 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jYrpWFsqujSvcHKdrDhKkOwUbeKCOkNCvA2kc4LQUoV9m4uCBO4j0LP1m4YDO3dkXIsMk+SUxcfNnXfVncoqUFffnptxs91c4IDfwWvfZja03HEWZnB6PFXeXGhdhXqx9/Eo72SzHr4ZD9wkwLR94coS12dk41AYW3jY8/3UBL/ZXEPtOKxl/GFYQfYl7+9EJpF5+g6VR2+PRKaRPJfpFqiehK2UNRpjpOZEgvMeCBMZttE85C5tJrQtPgGkSH2Af8tLinI1Qpg98xduW04Upbh/92gu4mafAJT43vfeGXrXMzxuq+k2TKoLSa2T6gQeCG8MgMUCWyFGzVwyYqqTamB4bjTUV5g6KQogrCHdgPcfKRAp2lCK4nxZgZdKdoMSDBxetK9IqK2FWb0c+3rFxFk1ePTj/z93tfqt8fnqi9tH39OKg50IbsBnGvO+89brVNCO6HxDVfYiaukn5g9+FUkMK8PulKFQpZks5/YjWpxg3tnBDgh+4VV7Bw5KU4f0+xT3MfrtQX61qn63B6NvkDA2L4rMsZ63ek6aU8EmYOSX3Li/vdKdgpaNCdSXE4ruo1cRRW0bQ1h+417m0tJcNH4qn+1k1++uZkI47BjpQOHwnz/ljIskHSaPwhRnltuFXHZYJN5mOzsSa7Key1/h8A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: tedJwENNpupSH40iWmDpWNExfA+E7/8RAygnFGj953xk/XEIBGoGTRuHFm7qwQ5WUWLaI8wUSufC8zaeIMkW64FgKEqz+nhFbTsSAZLTz85tDQEK+HKDu8s7nX0IIGNPEHXZh+kSVU7tNlcXY6Zyqh5pC7jeGgmN/YZKbkIJFhqRYSWCkPaYGhbUd+CXreKdtQnnMMyUPU2827vUlvlpcVaojwrtE4c+RYgZinGWSJ5cf2J4O1DfpO9mi84UAQT1pVzNY9dYoxVpet/bwk1GIYyT7qxrggzbeBk8xFDUs+glWVGzBdEK5uuRHtSxbrYMIKQ91GAj9OJklNhZG9Qb/Z8eoox72QAtP4pmoZ0m5e3rGzYIFXEm8g6N6vXeIUFLWLBg1AiyQGbqPRxzRQFGWJsfqdZ/X+EhuucNLxwdDz9FyCoB+7K2XDEZxUdPrQ0i107C/7ywXbE3P37ycb19ukY8JlXQmA7bq6WKx4LG/OtyutIgGDyWwZXevueJk/H2iry3PwjPLXIpCXnKILEomyFEo4oyoGZpA/IesqdxYOat57Uct+AzyAWUKLGWcgpce+Tke62IMo/2lkjHW6ENsAGfYcoqXpedstHmZqeg1BM7rkpBzuarVnpOk5DHN3H8NMunpmfaV5CFBrF516BBYiJtO7zk5DMM405xl44Ck1c97ybkgfAvh4g/xXiRq+yi4aHeb8z+0wBaaTJrCL5czMX37EogjZmQISdj1d3kGgWeeJCpDaWQjPTZvDRwH0N38yY3Scg1XHuHeVJ+qt4xf6mrdzK+MXPyiMqH6zoZIwMvQZ/yqwp4ywiwwkiTksF+gnFeiIRF++FG0Q9+SMrCjFjy12ilsScJC4nqcz7/wKSwUOg4ejmxzXgfIS4idwZu5yZdLSlWqy41rK1ouwo/gRW1RH/gzS98/FV1Bj5fNEmItkJEwip+0UQc08g4yaR8nx/6YYr/d7VIzaVMB8cL5QR6X53pHHmXR/CFdfthvKytxmMp46UpNrHl/s3kEgDeaiG2FsbwA4p6KbzWaQ4kj7SkfMG4rKaBHBLnnghHaTmkeyAeP8j9wQcZWgYfrxjUo41UjEmNNiCQbxVvsiSdP51qaejRmEQeBQYASIpXS36U24HMwelS/FNx+NijzOhAiSlkQgnXsVZcHEMVrBW+IqHP2l6A7nHbov7aOh9EAXyzixshgwun3mjgqHMkKFIqYBHk1XhKy4X5WWfdfdK3Ofv3SWtIiIPJJYfDv7qabGHIZ/54YHNaHoTEgdp3nrqx8R2Dd7uXJAUIvuhzTO9KaKULZP8xUzQYzxetbBSomI2kjJss6YJwrHk60OUgMCK0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 025ba9c4-ed41-4ca3-e2fc-08d925d05371 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:33.5242 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: s8LadkHFVD+9sZs4VRGO9LXFTQ5LjhfHaSecUDL8jH4hsTg6yoGYcVCVD+3twKh8oihWJYV00Z1CQrK8E6z1NQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The memory integrity guarantees of SEV-SNP are enforced through a new structure called the Reverse Map Table (RMP). The RMP is a single data structure shared across the system that contains one entry for every 4K page of DRAM that may be used by SEV-SNP VMs. The goal of RMP is to track the owner of each page of memory. Pages of memory can be owned by the hypervisor, owned by a specific VM or owned by the AMD-SP. See APM2 section 15.36.3 for more detail on RMP. The RMP table is used to enforce access control to memory. The table itself is not directly writable by the software. New CPU instructions (RMPUPDATE, PVALIDATE, RMPADJUST) are used to manipulate the RMP entries. Based on the platform configuration, the BIOS reserves the memory used for the RMP table. The start and end address of the RMP table must be queried by reading the RMP_BASE and RMP_END MSRs. If the RMP_BASE and RMP_END are not set then disable the SEV-SNP feature. The SEV-SNP feature is enabled only after the RMP table is successfully initialized. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/disabled-features.h | 8 +- arch/x86/include/asm/msr-index.h | 6 ++ arch/x86/kernel/sev.c | 113 +++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/disabled-features.h b/arch/x86/include/asm/disabled-features.h index b7dd944dc867..0d5c8d08185c 100644 --- a/arch/x86/include/asm/disabled-features.h +++ b/arch/x86/include/asm/disabled-features.h @@ -68,6 +68,12 @@ # define DISABLE_SGX (1 << (X86_FEATURE_SGX & 31)) #endif +#ifdef CONFIG_AMD_MEM_ENCRYPT +# define DISABLE_SEV_SNP 0 +#else +# define DISABLE_SEV_SNP (1 << (X86_FEATURE_SEV_SNP & 31)) +#endif + /* * Make sure to add features to the correct mask */ @@ -91,7 +97,7 @@ DISABLE_ENQCMD) #define DISABLED_MASK17 0 #define DISABLED_MASK18 0 -#define DISABLED_MASK19 0 +#define DISABLED_MASK19 (DISABLE_SEV_SNP) #define DISABLED_MASK_CHECK BUILD_BUG_ON_ZERO(NCAPINTS != 20) #endif /* _ASM_X86_DISABLED_FEATURES_H */ diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 69ce50fa3565..e8d45929010a 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -485,6 +485,8 @@ #define MSR_AMD64_SEV_ENABLED BIT_ULL(MSR_AMD64_SEV_ENABLED_BIT) #define MSR_AMD64_SEV_ES_ENABLED BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT) #define MSR_AMD64_SEV_SNP_ENABLED BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT) +#define MSR_AMD64_RMP_BASE 0xc0010132 +#define MSR_AMD64_RMP_END 0xc0010133 #define MSR_AMD64_VIRT_SPEC_CTRL 0xc001011f @@ -542,6 +544,10 @@ #define MSR_AMD64_SYSCFG 0xc0010010 #define MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT 23 #define MSR_AMD64_SYSCFG_MEM_ENCRYPT BIT_ULL(MSR_AMD64_SYSCFG_MEM_ENCRYPT_BIT) +#define MSR_AMD64_SYSCFG_SNP_EN_BIT 24 +#define MSR_AMD64_SYSCFG_SNP_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_EN_BIT) +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT 25 +#define MSR_AMD64_SYSCFG_SNP_VMPL_EN BIT_ULL(MSR_AMD64_SYSCFG_SNP_VMPL_EN_BIT) #define MSR_K8_INT_PENDING_MSG 0xc0010055 /* C1E active bits in int pending message */ #define K8_INTP_C1E_ACTIVE_MASK 0x18000000 diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 8aae1166f52e..172497d6cbb9 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include #include #include @@ -38,6 +40,7 @@ #include #include #include /* For struct boot_params */ +#include #include "sev-internal.h" @@ -54,6 +57,9 @@ static struct ghcb __initdata *boot_ghcb; static unsigned long snp_secrets_phys; +static unsigned long rmptable_start __ro_after_init; +static unsigned long rmptable_end __ro_after_init; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -2085,3 +2091,110 @@ unsigned long snp_issue_guest_request(int type, struct snp_guest_request_data *i return ret; } EXPORT_SYMBOL_GPL(snp_issue_guest_request); + +#undef pr_fmt +#define pr_fmt(fmt) "SEV-SNP: " fmt + +static int __snp_enable(unsigned int cpu) +{ + u64 val; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return 0; + + rdmsrl(MSR_AMD64_SYSCFG, val); + + val |= MSR_AMD64_SYSCFG_SNP_EN; + val |= MSR_AMD64_SYSCFG_SNP_VMPL_EN; + + wrmsrl(MSR_AMD64_SYSCFG, val); + + return 0; +} + +static __init void snp_enable(void *arg) +{ + __snp_enable(smp_processor_id()); +} + +static __init int __snp_rmptable_init(void) +{ + u64 rmp_base, rmp_end; + unsigned long sz; + void *start; + u64 val; + + rdmsrl(MSR_AMD64_RMP_BASE, rmp_base); + rdmsrl(MSR_AMD64_RMP_END, rmp_end); + + if (!rmp_base || !rmp_end) { + pr_info("Memory for the RMP table has not been reserved by BIOS\n"); + return 1; + } + + sz = rmp_end - rmp_base + 1; + + start = memremap(rmp_base, sz, MEMREMAP_WB); + if (!start) { + pr_err("Failed to map RMP table 0x%llx-0x%llx\n", rmp_base, rmp_end); + return 1; + } + + /* + * Check if SEV-SNP is already enabled, this can happen if we are coming from + * kexec boot. + */ + rdmsrl(MSR_AMD64_SYSCFG, val); + if (val & MSR_AMD64_SYSCFG_SNP_EN) + goto skip_enable; + + /* Initialize the RMP table to zero */ + memset(start, 0, sz); + + /* Flush the caches to ensure that data is written before SNP is enabled. */ + wbinvd_on_all_cpus(); + + /* Enable SNP on all CPUs. */ + on_each_cpu(snp_enable, NULL, 1); + +skip_enable: + rmptable_start = (unsigned long)start; + rmptable_end = rmptable_start + sz; + + pr_info("RMP table physical address 0x%016llx - 0x%016llx\n", rmp_base, rmp_end); + + return 0; +} + +static int __init snp_rmptable_init(void) +{ + if (!boot_cpu_has(X86_FEATURE_SEV_SNP)) + return 0; + + /* + * The SEV-SNP support requires that IOMMU must be enabled, and is not + * configured in the passthrough mode. + */ + if (no_iommu || iommu_default_passthrough()) { + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + pr_err("IOMMU is either disabled or configured in passthrough mode.\n"); + return 0; + } + + if (__snp_rmptable_init()) { + setup_clear_cpu_cap(X86_FEATURE_SEV_SNP); + return 1; + } + + cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/rmptable_init:online", __snp_enable, NULL); + + return 0; +} + +/* + * This must be called after the PCI subsystem. This is because before enabling + * the SNP feature we need to ensure that IOMMU is not configured in the + * passthrough mode. The iommu_default_passthrough() is used for checking the + * passthough state, and it is available after subsys_initcall(). + */ +fs_initcall(snp_rmptable_init); From patchwork Wed Jun 2 14:10:25 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294705 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C705EC47097 for ; Wed, 2 Jun 2021 14:12:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B3102613F3 for ; Wed, 2 Jun 2021 14:12:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230446AbhFBOOF (ORCPT ); Wed, 2 Jun 2021 10:14:05 -0400 Received: from mail-bn8nam11on2062.outbound.protection.outlook.com ([40.107.236.62]:57888 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230234AbhFBONh (ORCPT ); Wed, 2 Jun 2021 10:13:37 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AlSZLEjeg7eDOwUbgqJ0Bp0tPyZwXNsw1F4vK/gCQycbxyhEb78T4NoZD8SINKYqPCWc+0mi5fjtn8qVXc8SvhtoTRumqcZJ8bKAYIdbU4lVA/nObCE2AFPbV36lYn7VzTp275MT6l2mNzC61PodNtSxOl722RkkFYr5flv7/PZE16trTJ18rwB/siDB5JaU/LLdUukqOU3GWRqHsLzxOTMfs79+Sh4R2waJBxYrraINXd3AEWXz96TtFxovFBzi9fLaOI5zNxXYMA/kAatrYZJoAPTNCPqu36Prz52GaH2n4GCDZjV96JBys8lY5REaocf/lEJSLFrTMcpIG8/BWQ== 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-SenderADCheck; bh=o5FeCbgJvCPDvNZucTG6C/Be5DjljDLwjfU1yTbGz3c=; b=TY2unl9Bn0+gYOR5KBXrA78lSQxlA2K6KUgv7Vgg1iJbWO1Jmqu9ViVlGAdKzjpKPZRXQ/JjjsEWxHufCybRiNbTTIQCkcaILtkH5LEopt7M1471IYR1MaXnIXJUCV+kzJy1OnAGXn6kr63rdtrz/dYQbDHYFoBXMqxlFgpUDdN5hKN1koTohjREzqLq2t/pFXPWnAma5qAbiyA2+ywK+EYQ6EALkXqUtcFFyy+s8Ak3WWRoThIuAarMPc5wVxpjHl0sCmaWVEI0VIl5ByeS3jrDgK3xlSJPuq7QAM23RUwRsFMRYhY5UG3KV1Tmf35UalV28rUpNj/Sbo2aytjiZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=o5FeCbgJvCPDvNZucTG6C/Be5DjljDLwjfU1yTbGz3c=; b=L1orI7FmTdpKRbgmDwBg96RjF/tICP0FErx2qc91HbJb1LTCoesCE7jNnnFzQ+eiXI6L21uggrZMstojC07lQoxBqC8iTQBlpdJcsiYI2MmP3twIyQgulXd+IVAqGAfPX1D/BrOrfWDAgUllNo4oI+3hC5YUY31IvokhH4btaoM= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:35 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:35 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 05/37] x86/sev: Add RMP entry lookup helpers Date: Wed, 2 Jun 2021 09:10:25 -0500 Message-Id: <20210602141057.27107-6-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:33 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3d93bc58-4741-45b7-ecaf-08d925d0541b X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cwEXj2G1id13+xvZBTNiYGMkxdPHBwII5WrimSoKIo539E+Z/paSSL1aQdKq6NIX/7ivLU3R4o1CjXBDitypLB4CX4pigEbHy/H0qJ5wSJU24AOasbr6HfMz/rvhE3dKo9GHBtd2pLc/dtOhTJ+JcVeBfbQUSRDIDpOC+nBuLFcFm77luuD1+2JDyFvgaTr4VO14/J3JvCU8hiDXo5dUj0MVIuNdO4WIcvm9A9tFjSiIyBPdBgG5dV2Wd/GbMC42rgio+uzltI0wpVlPvfeevRVvyWF8X1dShnmBna8ulr/eQiZrDNtslcAZVOFwftCd1LQy/Td5GxbErZB6qC/Bj4edncC1e3FsTViCx6tRIp4V3pehi6BwvucqMYwSmHt71+fDJFGFN/2qxf8l1kb7iulT82BYz7M1NPbh34ZyZiyBRUAl71yd7TbjOyrZ2cIJn5dgQ0lDmDNeMIdTaRpQjTi5ultzF3rtU8H+lS6iQMEoB8gp/QzaswqedbBR8HQx+v01dLvp3arSgrQY6nAR6NTdnr+zM433B6k2m9U6B5/0dk6D17Tmm+G9CyndeAULHm+p2GUkxqRH1YnqEgilS9DT11AGIjSuIkYs6ctMs5sJUNo7495ZPL05nH+RntEXAGys8lIfmyrAFTeSny/TCtXVw+wnez0BFTRQnDaq08o5sk9l7xwG4MlYUXER1/H7FssGrrIdNu3/odFj7fs3k9oa+HFGXt7EYN72rmyfoHG7/KJJ95zKNuAPh3c+ih1kJ+PPPMWCJm0+bp1NHfb+qg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(966005)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: zBSld+TuNf6/haO0tZT1K/xqBnmj+ca7s7QDmKbOtkHl+g3UHbO1aCIl4QqPQJ5RzrhVuY+WP96cUmKtigpmtajn+OQt3Nn9e0W2rDEbGJGkj3m1RrcNVrPaovzVI8rne+Ocoy4TwEasUrF9uQ2gSOyENAknHRrdWdXzLj+hOYuKNlXUjUJhk5uxwSAlrkg7Kiofe+GoRWnevQE4bY5PbXcsJNmLDnBvuOkTrp/XuFc0uh8mCvMPaWEkh4aCFbG2vQYL07QxNLQTz7DMRNfL+cIjiFUHWS0BtkAXeQSf+Uauo3o0Hc5hfK1J8MW7V05hAXgZCyeHvj7qZcuxcw41+8Q111bIGrauS6wbvAKmbj2O5YbUp8CJ50nbN0uUdl4nEfLzNDjqKSBlnDW8kaVp2kWGTt2qBpttiuB0t5YR3SK5jSgzsKeIfYT4XnB2iaGxFe+L9E6V2YfOMG0b2QlVhiMou1BD7xmpNd2/wrmr3Gfhkqrh9DrYnjf2UYFNSUtrvX1cX1Gf8FidjnNkMzoZL0U28EeP6ELL7DZWc24Jxe8M9/apRe+qFsJtF7/kZD3p8oAyqLKbCJE0mquqRjG5Q268nhSUfz7qvDHU2mb8bgRRAxTSs4j4GD0+3KjbS3T5Tnjbruu6tCQPQ1DwN92ItkFnCmuHhzzxEYhbLqyxLKqFDLAkjihN2TRbgahDI3WDAzdrMgGd5MxqaA/GKy/erszbI5HkWgv/Sjb1QMx/hiVpOJOZcOFpgRUAZ9fGkV9aU7ofe7Ty/8qM9MrHhgatrSIpBBgkpfTqeSgwmmqsZAKS/Dy2dJa/kBfDMGWGH6M5Dm1FaJxpfF/GcjMTk2ErSMUW8vsAwt+X8AHxxAN8JWqvgrU/eelRiJT2vIbv/gub2fznWA/c+uf94UC5MDXtFiT/eNR5y5siS2ZOFT6rCbkvd/nZGXVt8HsNDhrcjJWhJOzrhRzE6m9WDVahYSFo08VZUaR0hKf4Pnm76PGyFDcdIjZabKp/7UeYiyL1CS2cROnebfspErFDAd48xTKB6g7y4l2X4b437bpM8P/PLrayO0GBg+qg5jnioL8q+xTJks56Nn3JrtIr4WxsrXJggGUv6obWzMUYSXtNyoM1pAV3dYiTD+UIfRzWiLL9xu8PYyzc2N77sKHKDjAjj6JW7AQLCnDm6Yon6dQ43xBkycc038KuVjK5Hwd2PSeQcOSjvwpQeJwpNT+n/LgWW6/HuG8GUtiGK6KFmsBwa2pMet+iD6J2wHbYkRu9NBnZJ7pqqZvUPR5Mxl7e+lQ+jag8LYatkfULBEMUWKdq/iBal67UoZ65WJfGSJNaXWTifK1q X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3d93bc58-4741-45b7-ecaf-08d925d0541b X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:34.6126 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 9i+wQwB0Dmb4NtK6Foir73OXN7Ze5RP3r4mfUgC0lUK86sCg7kDLx2CX7qtOW16FQSlIfS2x9hg9nHT+EKc1wQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The snp_lookup_page_in_rmptable() can be used by the host to read the RMP entry for a given page. The RMP entry format is documented in AMD PPR, see https://bugzilla.kernel.org/attachment.cgi?id=296015. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev.h | 4 +-- arch/x86/kernel/sev.c | 28 +++++++++++++++++++++ include/linux/sev.h | 51 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 include/linux/sev.h diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index da2f757cd9bc..2764a438dbeb 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -9,6 +9,7 @@ #define __ASM_ENCRYPTED_STATE_H #include +#include #include #include @@ -74,9 +75,6 @@ struct cc_blob_sev_info { /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 -/* RMP page size */ -#define RMP_PG_SIZE_4K 0 - /* Memory opertion for snp_prep_memory() */ enum snp_mem_op { MEMORY_PRIVATE, diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 172497d6cbb9..51676ab1a321 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -46,6 +46,10 @@ #define DR7_RESET_VALUE 0x400 +#define RMPTABLE_ENTRIES_OFFSET 0x4000 +#define RMPENTRY_SHIFT 8 +#define rmptable_page_offset(x) (RMPTABLE_ENTRIES_OFFSET + (((unsigned long)x) >> RMPENTRY_SHIFT)) + /* For early boot hypervisor communication in SEV-ES enabled guests */ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE); @@ -2198,3 +2202,27 @@ static int __init snp_rmptable_init(void) * passthough state, and it is available after subsys_initcall(). */ fs_initcall(snp_rmptable_init); + +struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level) +{ + unsigned long phys = page_to_pfn(page) << PAGE_SHIFT; + struct rmpentry *entry, *large_entry; + unsigned long vaddr; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return NULL; + + vaddr = rmptable_start + rmptable_page_offset(phys); + if (unlikely(vaddr > rmptable_end)) + return NULL; + + entry = (struct rmpentry *)vaddr; + + /* Read a large RMP entry to get the correct page level used in RMP entry. */ + vaddr = rmptable_start + rmptable_page_offset(phys & PMD_MASK); + large_entry = (struct rmpentry *)vaddr; + *level = RMP_TO_X86_PG_LEVEL(rmpentry_pagesize(large_entry)); + + return entry; +} +EXPORT_SYMBOL_GPL(snp_lookup_page_in_rmptable); diff --git a/include/linux/sev.h b/include/linux/sev.h new file mode 100644 index 000000000000..83c89e999999 --- /dev/null +++ b/include/linux/sev.h @@ -0,0 +1,51 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * AMD Secure Encrypted Virtualization + * + * Author: Brijesh Singh + */ + +#ifndef __LINUX_SEV_H +#define __LINUX_SEV_H + +struct __packed rmpentry { + union { + struct { + u64 assigned : 1, + pagesize : 1, + immutable : 1, + rsvd1 : 9, + gpa : 39, + asid : 10, + vmsa : 1, + validated : 1, + rsvd2 : 1; + } info; + u64 low; + }; + u64 high; +}; + +#define rmpentry_assigned(x) ((x)->info.assigned) +#define rmpentry_pagesize(x) ((x)->info.pagesize) +#define rmpentry_vmsa(x) ((x)->info.vmsa) +#define rmpentry_asid(x) ((x)->info.asid) +#define rmpentry_validated(x) ((x)->info.validated) +#define rmpentry_gpa(x) ((unsigned long)(x)->info.gpa) +#define rmpentry_immutable(x) ((x)->info.immutable) + +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 + +#define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) + +#ifdef CONFIG_AMD_MEM_ENCRYPT +struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level); +#else +static inline struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level) +{ + return NULL; +} + +#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_H */ From patchwork Wed Jun 2 14:10:26 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294709 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E376BC4708F for ; Wed, 2 Jun 2021 14:12:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CDA2E613EE for ; Wed, 2 Jun 2021 14:12:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230272AbhFBOOi (ORCPT ); Wed, 2 Jun 2021 10:14:38 -0400 Received: from mail-bn8nam11on2088.outbound.protection.outlook.com ([40.107.236.88]:32416 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230359AbhFBONj (ORCPT ); Wed, 2 Jun 2021 10:13:39 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZD4KV6sga9O3iiXNC5qMp52yANHwPombGqFEdq0glQkE5Etu/Nhf04sIaz3cwTqKRcTMA025HgbzOWCk11bY4Eizye2rl4uE53nAurMuvjCvbOU/7znJ+HJ6SkxYU2ezScCBfSCmqoGPvTq/fLndU2pzajRg5RNNtDR1q8JTF22iZqIxg1Baz1RvRRvpEIytS9IowAEas8b+pAgyzfC+mYh7URuLF6DLLIsPOj2AXWiZxSBmxcbnT8mqslbB26yM0vw/YpE3+wHpnqzmECZewmkZGICp44OHTHODnK7AWpGFMtWr1I3zS/0qPYlkpQpgMDfCySwzEGGZdfuXH00hcg== 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-SenderADCheck; bh=ZEZwO4zibzF6XLvhJ7uKSuP2o00KS+r4CawKQPuRyXY=; b=O+w2aEu1DtCvVGu4UGCAcqQK+gfjbuZ73wgLDDPRCo+ApXJuy1A973xXJ3OU2RtwlINE8VCDtOyhvCU8d+PnTZHbH2CpySimzaNPo7IfZTLpBlGutoxpBbRi0DOOvWM/twac5MflXjJ1yC5VZ1z1DWNqCQXDqX/t7WKvPikh46tmJGFdNgPrxOSUlus5WBAtYMtdJ1mccxtYL7CNwWl9Fncedsv9B4P/OCMpVxn6PbCHbD9lJjio74nyOnfM+ox7hH5hONRXWLNS9RgsiSO0X8k4Np9pX9DwA3WhSLXv4RS+MtGMeB1DR1V2iT65F8n2Ozd6gGVqf5qHsCb0MI7GBw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=ZEZwO4zibzF6XLvhJ7uKSuP2o00KS+r4CawKQPuRyXY=; b=jcHpo5VJwxfteSnrCEHR7H582g9PFyUdAf1iZRtfcsNechPrBNJfFRwrcJ5CfLnMorzH9NwdaKXpOGt2j/wPylFi/wLIx9gb3ewazaoPD05pXwv4tjK14qfqMhxCde6KXpjMqea/dSKN3P/hfgWEnpol+R85UWoXbig79K+9gYI= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:35 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:35 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 06/37] x86/sev: Add helper functions for RMPUPDATE and PSMASH instruction Date: Wed, 2 Jun 2021 09:10:26 -0500 Message-Id: <20210602141057.27107-7-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:34 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d4f81cf1-c079-443a-e46b-08d925d054cd X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: n0PdiE+PI8sRpNXh37+n8vC1R5eI2biR3DO2ggUDUtwGNB9DesdcZwj+gkIT1QXxXMAeAswmI8COZyMQd+9KZ6PTD0oHPLyejtsEHDDV5F3kUtluwDxhSLKxtIHNKRvD02HzinmT3N6drXZJ1b5Yw2vqeD95GxG9dg7TqTFy641OafXZ8EZ7B+H12Q5oa/t+QhREcyT92QLtYFT5gU7UgO+lHwwUQ8/XERVjQv0C4uJXWVDksBFwSUfcWnV1v9Nc95RmATRPLcmKyPDRFYDu0Z0wyDR4eglqgTCVQ00gCr7mH/e+wg1f6/55dCmqxZ3gUXcPBaHzKU5odQvZ76yTu6FNf8nRSTDwWMuwM0PMXyhmP2W/DOL12viu69g7n5XbZvNmZHiev8lSpVu92tsuVVqbN+R/j6VV2hSgVXSlYwEkzgzRLtfDCkWCJtnbXspmwBH5VJsmifcS1T0ELLvohRj1g8I/qaG7SHMokOM7egUAYOpyO/AGOK74dL42q48yYbqCAcc2AvNTtTo4LsDFHw6r0OdpqDW8t3oGnKbNQqKX6X7lmh9Ut1a0ddp43S1252evYTiUZFMGqtUKGxbrgbx+UtXENMkIodLh5oUrh7onjs1vhCJBEE3ZyYlHqXUt7LQGTenakGs+3WotDwIDLQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: MSvZ2usGucY5aialo4Ut3ykid2ymAuBy8EOAPR4I0524oQR9/bJuVVX8Vb6VNeb5gnjRjlwaWwWojMVDxAFWQ8/Lf6rO52oxJFA9iVDZD920DbxCiaUVg0HBICysRHu3/OkcS6sr9zneVJDxyD0RlHH203WNZVKZtI7PO/u8nS7dI/f+JB1me5QXY7UYNBa1qoAknXP2u4F/z79pd56vFHeOwJNdcie1G3tmP1WC/7fGnbNU8xxNCjALYMldxU+sjJ5k+evnw/iA4owLkrNxpBXQCOqlTZz0wejortZhSVqKVliko2/U79MITU4W6dr2mH3hMO+UVaVeUfMSmxQEIsdf0V+HfYd/p7+WhBotaN/PldRiAuPPvkLjSKF0fA1GYjce4lg6dvP0z6koDMvJBN2fINil9QdO4frASou63411AfdIbfE9LtdEejOAOJ7RVzHYqrd0jFr3mzL11pz5kFypnoD6PQyAQ2VQmm4jdqZByDB6a19D40zl/hbu2yLCalKg6+uyqpftlJ9mpYCx1d0xE2/j1uDeVc1fjLFM72qsscZJOrHzX7TAdkfMPSVqbRNyQCn042PgcVDSXoy2LqEL5nwH8NmJYokCSgqZQ/moSMwzlhKYKLFMl54iD+SAs0kbvGJ15nToOzPOe1fvNLmqw50F2szFT5gYEHnByr5r9omp72QsVrsXDAXDYpq5jqcT1VIIWLMhrYkVKyYMH8vP85aF+l12IeNSmFwKKza9yHbzRsxBUyLjudh6OXEB1kZ+G9G3QnlwmarUTr3kOGPCnnyzaWUOPfzpMD8IXYB38vWGJvZX8Wk5T3I6JLIx4V9AG9zE6YM/g3/3otbiOSvCUQ7DLKOq/m8gL23QQoxOin7dpCd37XIi/p8bEbkMQGEsLBuJ019aQnp3NjPSOa5dBqq6WVGHjifZSyhgH4+zclbIxBkL1tTtAAwHCie3zWgcAos6uWVRpWK8po6tQKFWZZV4ljaIRvNCpmm1dKULo0GCJ9J2cHknrwwk4BcPH5oH9rfmSAHQLCNc2HLLiIR8du7vBN/js0NY6aMDA8J75KV3ZEVxV8KN1t+HxfCpobLeUDU2TpvsEBNSqd9BffqXwljzkW755c9nmcL476hdPShnRHFm3wlgAZunaRAvhwtRV8V9I/hFtXUkiPN4mNxpgcjyY6V2/vb0fqoz8r9XnzzlD06JWoEZy2P2OdwHu7Qrx//BZ4sJZgujTxL08EP1bzYYTGbEglGmvkoywwb5ZPcNKtgfZHj5mD5GPAW6MNT3qcC4bAWEYAsvIm6fdosew4ElWqUgJfUJpBMotS46oSwRJPuS3GZubJHFDmhX X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d4f81cf1-c079-443a-e46b-08d925d054cd X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:35.7429 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JteIB1gYKxV7PXgV08DHYqzZ5pxH34wYuYMsIMy3l8Hhvz49Fx+XG2LoNgab+OKdUCcoZ/HyjeVVVKe70BMXwQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The RMPUPDATE instruction writes a new RMP entry in the RMP Table. The hypervisor will use the instruction to add pages to the RMP table. See APM3 for details on the instruction operations. The PSMASH instruction expands a 2MB RMP entry into a corresponding set of contiguous 4KB-Page RMP entries. The hypervisor will use this instruction to adjust the RMP entry without invalidating the previous RMP entry. Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev.c | 42 ++++++++++++++++++++++++++++++++++++++++++ include/linux/sev.h | 20 ++++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 51676ab1a321..9727df945fb1 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2226,3 +2226,45 @@ struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level) return entry; } EXPORT_SYMBOL_GPL(snp_lookup_page_in_rmptable); + +int psmash(struct page *page) +{ + unsigned long spa = page_to_pfn(page) << PAGE_SHIFT; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + /* Retry if another processor is modifying the RMP entry. */ + do { + /* Binutils version 2.36 supports the PSMASH mnemonic. */ + asm volatile(".byte 0xF3, 0x0F, 0x01, 0xFF" + : "=a"(ret) + : "a"(spa) + : "memory", "cc"); + } while (ret == FAIL_INUSE); + + return ret; +} +EXPORT_SYMBOL_GPL(psmash); + +int rmpupdate(struct page *page, struct rmpupdate *val) +{ + unsigned long spa = page_to_pfn(page) << PAGE_SHIFT; + int ret; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENXIO; + + /* Retry if another processor is modifying the RMP entry. */ + do { + /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ + asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFE" + : "=a"(ret) + : "a"(spa), "c"((unsigned long)val) + : "memory", "cc"); + } while (ret == FAIL_INUSE); + + return ret; +} +EXPORT_SYMBOL_GPL(rmpupdate); diff --git a/include/linux/sev.h b/include/linux/sev.h index 83c89e999999..bcd4d75d87c8 100644 --- a/include/linux/sev.h +++ b/include/linux/sev.h @@ -39,13 +39,33 @@ struct __packed rmpentry { #define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +struct rmpupdate { + u64 gpa; + u8 assigned; + u8 pagesize; + u8 immutable; + u8 rsvd; + u32 asid; +} __packed; + + +/* + * The psmash() and rmpupdate() returns FAIL_INUSE when another processor is + * modifying the RMP entry. + */ +#define FAIL_INUSE 3 + #ifdef CONFIG_AMD_MEM_ENCRYPT struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level); +int psmash(struct page *page); +int rmpupdate(struct page *page, struct rmpupdate *e); #else static inline struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level) { return NULL; } +static inline int psmash(struct page *page) { return -ENXIO; } +static inline int rmpupdate(struct page *page, struct rmpupdate *e) { return -ENXIO; } #endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __LINUX_SEV_H */ From patchwork Wed Jun 2 14:10:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294715 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BA58C47092 for ; Wed, 2 Jun 2021 14:13:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8183B61400 for ; Wed, 2 Jun 2021 14:13:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231315AbhFBOOk (ORCPT ); Wed, 2 Jun 2021 10:14:40 -0400 Received: from mail-bn8nam11on2066.outbound.protection.outlook.com ([40.107.236.66]:15585 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231357AbhFBONu (ORCPT ); Wed, 2 Jun 2021 10:13:50 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=B4jjmz0WyiuySqtL/qsRnRExj6eaotEfY1ckI8Xr59CXdB/SW3gn9K1K0AfW3kPo/k7TCcB25IIghkgJU7SWbU12EWAlFt1I5niL+LNWyCKHUBRyPj90z+Sp1wunYd6MoLHFw9XpD0Y3qeAAB7gmh0YEuSa6Z+ORDdxp4yL4eJoMQQ3ewLQFUHA/DwQgUONV/KYgUpVIDJeagwqM3rFhuXWtQeGTpziSb904wKd+up6Y2aJycxwTksFCAmAXWsk6kHzeGpcQ79dfT/mgrhtBR/Q+J17pDKZTUfvwSRLpydlq6G/wkjo6GR4FCea3YbHqA6CjtO9rVdGX/euhsAp9gw== 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-SenderADCheck; bh=zvD91IGs9k4Jvq6g5vcETVczA3gK6OHl4yM/9fIXIJo=; b=MKAL7VFYuLEauwnIhAS/nsMO0BTrVIq78Dd/tLyPVKdGwITQINYZcA7GbNEpIZMxqRKWFv0yYO3Zs0eYgoNHjEEvA6p4ncAWLo2azMYObIaTaDrPyJ0+Ut/iih9geDINdQApb6lRzV0so8SiqW4s7A5FaX9koYorw0jtLbFRH8yn0vzHx39RW0VJ4XwAVbfsnH1auKkMcHmD+0xTrP1uWlEpuxTEGwkW2mJSr3WAq39z0/k/UcZVjcCEzfS+i2Vjsumi06BFLWIUEj9qjW7ekLm++O78yN7Vi41t1XDlmKc4A2j9eSEWyme+bJUM2p91IrX+B/49pW8zQwzUXnwYKQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=zvD91IGs9k4Jvq6g5vcETVczA3gK6OHl4yM/9fIXIJo=; b=0ICNBZF0YKrmwfRsgzeqqftlFMaGaEyMlhNi8C3GRy5HGHCMgrAudYvucWKJr83iO9D3Zd/yncJ7ZQfrB/czrguiA6MuGBu7o8JXfPVLyNiAvXEO0OteUOCVlap3CM5Y+3/rV8CSlBOpCE9dN5F23WwRjgbC7TJlpY0jMrPAuZI= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:37 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:37 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 07/37] x86/sev: Split the physmap when adding the page in RMP table Date: Wed, 2 Jun 2021 09:10:27 -0500 Message-Id: <20210602141057.27107-8-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7da2ab06-6e82-44e5-f150-08d925d05587 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tIUfJ3EXUOWDlMEdYAfLX3Ww0rRlDsurS+gKAc/rY2YfioQGOu435Buq57JvXqWYFC4Pq6OAM7DbeI0zJLu/eMt+tYYuXjbKq+iijU6gLLnQtiw/ekPEfd74SRyWX6Bi+SBUHXz38VDiWUsBCd3XvTE32q4zv5VmUw96Elb8lRw7ryLjSnV6GljQqPSMtv78sjHN/xOAnTBfc5t8nlkRqs+qMw9XKE7dRR0EkF1X3MZSqiY5X7yi07CHqxjmyPTH2gduU2svehkyCvo2tDhsVUdZyaCbFABbUO6qnAS0nRrmtKHw3WIeh9pSmPikNg5bu7hZr+chT8GwAxj0N/Y/FLHHdjcvCMkVd3esFkqSuwnA470s8tWVXFKHuLXXibqRgTdoquvFZR+lHTDDznQSQiMX/ywwIcvemuJeeIw9D0Xq0qTNfqhvLSJm4YciGpDIW4pTWWZDqZwmYvqpS9aox0EmQ7iQkLiEQ6RzVRm+bnVLwfs97M6PlVRcQGR1l1SV6XN9//D7XWvnY5mbOo6ITI82eSakZv5DyJvPplO3FaHqeF5Y6KBjVoY1te5VEuYan+3i7kWMoYd07b0zoaen/tZi/AIw0JWrQNGvjgh3q2qVBS8qk20YPNJukp3DnByIRc1IV4zkMmSonb/fhvH6SA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: jB/qEaJppH0RXZsN5vH93CgF5ZHxvkuOTEAbL/CRqRSLX5dykJs8oSNk5caxhT4hs6vKUiyiEexSE4TbAr+S+V3+0oHkvmwq2j7AihKHQ+qgALPL1zSgsXTK4cTxBsM/RH20Q229zDF50TMqlol4y8kE0//o1Ivi1hS+Iw7Nct8rdPkyazUeqcH01Ub53TLPs9uaQc5XH9WMtaxuzKjl1AGlQk8Cp5cD/3qyWUxtabjz221dE2GfQBcl06jpB6hJ697GcxYU0zwllHRRRu81FYjz2Gt2XpXW7sQZ6eicslhl0l5YQJK/x2/vpasf+9h5MXBsGxp6qy/pV8uerQYvfk1oKMDthYP7nGRxezwY+YoMndGToQDIMBlDIS149EU7dOLlJN9w8dF1zJyK7YAuVU1w6aKGFa0yF60rwXpQ3PT1RcKYi/nPm4Gd/PkfSj5pEsDSsd15XcfgJf3cbajkXDE4SBVYX8tug3U3UqFqzVJskUR3wrICkyWqYA9XP73j2Lm98rvypUGFoLVzhwdxsl6WGGe4PmUaJ3j1krradnIlGkr+wlzN0FvYvBuV64IN6AWWIZY3He/deohNJhwBHRpj1BI7iq3ts26OzKrXqF04SVvD2QRodlKoE1EualbVXBy+VClYCsrChTDIGZSR3GR+w2h1VplTTcSV5x5YjJ1L4GFQG6WWKW2IlGkbaon5hE9yBXlihYMVbrDwupNUPMsGojNnNtFewoqJjknwEReu521uWIAHIDqqX5ZNSaWDzc2Pi3ylyCYjZsWmjyDzcWUe4SEdozKN0esXRpVbVxiFFzIxMVuWWI+Ndb6X6kHf49x0PQEfhyxqUCGtgmS9j2gy9Z41hTZwK1qqAVvme/qHT5iIvFMKxj+BKJOAzG6yZ9ESdL0Y0B4OQuQN0dG9dylHS5rUCw94U3qiVj3UC7VAMsd8j+PHM6hyFDgwGpCD/IKF3Nuo6TFk36oRsCZiwvUXaIv4Xoc9NGOQNcXF3JP3pnPbg0xBGVRtRxLn2x2rC4XUkYNWJcseMe4ywFAvQ2s0p4uhwt8NbS8LCtBHv4XYqXyYMuIPV0t+P3L6WQPdlCYn/Lz4rsmuA0obaEAYJxE40v/YC0+BVPdnnCrBIDkLE+Ffwcf2YLC8eW5+pcdCTD2yPorxesbWUKNEitlLw7gkakkEtBLT2AbGIU0+bNfjto13jLlPnmFdnrVuTG3B2V+i8vfvb1UU1Xp2oIlSRLCsZMIDkISHN+cjVkQslPtYIjf9o0rVPtXCtcrMAAi2r41PgvCD60BCf6igxoDiaKaWo0F5tODBslO8foT65Zabr5hyMHyNtcDC6u7IAmrE X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7da2ab06-6e82-44e5-f150-08d925d05587 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:37.0402 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: tthya9+tYbJ95i1fOViX6hsv2cfxwobmcQCx60IdWENmLIPqVA3C6HbfbHskkDFqBMWkDV4mwDtXuhsw4ivx3g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The integrity guarantee of SEV-SNP is enforced through the RMP table. The RMP is used in conjuntion with standard x86 and IOMMU page tables to enforce memory restrictions and page access rights. The RMP is indexed by system physical address, and is checked at the end of CPU and IOMMU table walks. The RMP check is enforced as soon as SEV-SNP is enabled globally in the system. Not every memory access requires an RMP check. In particular, the read accesses from the hypervisor do not require RMP checks because the data confidentiality is already protected via memory encryption. When hardware encounters an RMP checks failure, it raise a page-fault exception. The RMP bit in fault error code can be used to determine if the fault was due to an RMP checks failure. A write from the hypervisor goes through the RMP checks. When the hypervisor writes to pages, hardware checks to ensures that the assigned bit in the RMP is zero (i.e page is shared). If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0. If one of entry does not have assigned bit 0 then hardware will raise an RMP violation. To resolve it, split the page table entry leading to target page into 4K. This poses a challenge in the Linux memory model. The Linux kernel creates a direct mapping of all the physical memory -- referred to as the physmap. The physmap may contain a valid mapping of guest owned pages. During the page table walk, the host access may get into the situation where one of the pages within the large page is owned by the guest (i.e assigned bit is set in RMP). A write to a non-guest within the large page will raise an RMP violation. Call set_memory_4k() to split the physmap before adding the page in the RMP table. This ensures that the pages added in the RMP table are used as 4K in the physmap. Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 9727df945fb1..278be03c64f0 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -2256,6 +2256,12 @@ int rmpupdate(struct page *page, struct rmpupdate *val) if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) return -ENXIO; + ret = set_memory_4k((unsigned long)page_to_virt(page), 1); + if (ret) { + pr_err("Failed to split physical address 0x%lx (%d)\n", spa, ret); + return ret; + } + /* Retry if another processor is modifying the RMP entry. */ do { /* Binutils version 2.36 supports the RMPUPDATE mnemonic. */ From patchwork Wed Jun 2 14:10:28 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294719 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8FCF2C47093 for ; Wed, 2 Jun 2021 14:13:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7B11B613EE for ; Wed, 2 Jun 2021 14:13:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231468AbhFBOOu (ORCPT ); Wed, 2 Jun 2021 10:14:50 -0400 Received: from mail-bn8nam11on2062.outbound.protection.outlook.com ([40.107.236.62]:57888 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230339AbhFBOOF (ORCPT ); Wed, 2 Jun 2021 10:14:05 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MZgYXN/Lj0XALBG3S+65bj987vTg+T2kKTwysMB2Kvr3xabymAxe400zq415VAoXnnHfO+vh9BOH/9JYPOISWOUK/bEaEbCwsLG47fpxuemLIf//cMhhvtwAxY6rI6KoYIKdDgt5WUJAmWTOnX9j5vafqHYP33+RMpaHdIm+qRfkqodRMh/JqDaMwf2K2GMd+TykAhkj4LqUxrUiNihTRrhpMljXpVE0trpruDkIqlvVuOtjeaW57qp7wAzk0QzQAz20aUXBDexJxZp3L9Jl2c9+e5Zi3t9pbdJNY1/FZpwATkWRmBzOQWv1lRrWwPt3F5cdQ8kW/uqtEVIA2S7tKQ== 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-SenderADCheck; bh=9IpMdpyuUJv/iUkp9gcvE2Lk8D0OpiJS+R9aTNr+fEg=; b=jB7787Hz3nC1fXqM/37ZPu+Ro1snPMbA3ksFDKGZqfJL+wz8RS0UOT2T5argJM9NBlM7HPQRmllVaq2lUpjhdj35YCiLg0ElD+VPfQqyrZYgGjpJW3SUK9SrPqH4xybkJ+BWE6OX+mGLAGM27uAGpz0eRgmMZXmjEmSi6RbSPgfTV/3ZcKIb9dpj5J2aob5ImHiDftkwGTrei85gbNNpd+2JAeQQnaIbaFwpx1d50jJU85emSgIKoJeDo26Jon60WCwixqXCDtRsLA2z11w/nK+wsphcYdc0WnQtve+azrWNP//ZoIwe7JyOaHuC72dG7nZEBR9G/TsyZCEpzrX8Tg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=9IpMdpyuUJv/iUkp9gcvE2Lk8D0OpiJS+R9aTNr+fEg=; b=RlX/SHk5OkmUhbExDlGOt01JC6/U8XU8awjQRRcFZfBh5Uz1fxJALPG/p+RcN/Ohlznxq8td7Pjf3ROS5n/ZTa71OnOfeB5YE2ZWHIz/6/dFsVJs0sCJc1wfYRkJQlieqFKETkiMxIQEdaF5PE87tzQuUmL9pTPp6gAmP2ZqeYQ= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:38 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:38 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 08/37] x86/traps: Define RMP violation #PF error code Date: Wed, 2 Jun 2021 09:10:28 -0500 Message-Id: <20210602141057.27107-9-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:37 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8dffd91c-765d-484c-c5dd-08d925d05640 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sQKtCYhTiSge3QVvuuqTx1HlCjHuZsIjtZ/qyPtMbDZqjfv8KCjKfb0aad+4PkyHuNINvuurhm7hO7GgpxqjuyW+ctvnv78YELLMztCSl7GxtV7ZH1E+8FKV/g5WO7IuRvx4AVAB4sWTfMamWBBZ+iOImU1JKJDnhzPxOVTBxZ+E2R+AgcW1h+p7K3FWzYpG9nN4Blr9zT64OERFbr7AYYPtfq9FSpwD7TI4i3Ld2h3ZENEUgHJtncgOVGto507lSY7MuDJAaSJjujEkfhE/u5B+QbTjWlSHX0iXUKAl8sgCFlRES+JWVvc2v8e/i/c9m5M1H2Ty4IPQ9p8YVwLN5sTeqy5JueKVF2QszkCV8Tx8DLPoZQxwAU0YPEwEVkd0khwlkj4JJ6dHKCwSB7sw53R0HuIK5wJegHTDctAsEGP/vNjOiN0iDDCP7qZb6PJZ72OqVqHtCxLtfAZMXmxdIpfv87X3RmsTUtb3R7GuCE+ZurjZXX2wQMKO6vIJCvnWyWC/Shy9f/dyRVRj1+DuPKt2vBLLb1UMYm7LGf/NmOLolNnWwuJDFcOeEk82n6QJ8HRFRtfZonTQ6EtA5slct7l3ZElHa74gZFXC+OgkiQxyPq8fAkENiHt+cWynXiWGiQWF2OTBT/nhVRGF4A3vZw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: i2Or2EdAogM0p5gpcI8lSGF8lcivZdtk5vouUt4/fYBXVZg0EwP5PgTU3XulSQgvkDMFU4d95MTrVF8DqfZqJ+96Gf9vdt3FbM4ppCZx4FpZ60qY5twf61J/p4WutOY8KzXgLsYoWGqLLcGSXPXplAaEJawsE6dGfIOUClYf38skhp4skEpTIWM7kKooaJ1nV25QLBFrCeaKHZ3/lLXskRfKQ1NYbTPlpRwGjH9f7daoL684F4og8aGQGVWPuoVQmMqMiJGWVJWBEgCSLYyWpGj0lyY4rVBztZAXT8Gaq+aR5bAYxEO+W/a677cEeGr3+3C0U0EMgniEl07Ia2pOVmlXRSlkOT0IaCqnEcGPzLsPUwGa7lWKZ8rt6pXIV4ObSyh7WjY53b5QFUBp+TvQcJmAv90neZG/KZBzQjnIhDSeRxDMYFLsMUafqzPH9cYtCbBrGhMmHX1WicN8GpHTDnLYwYomVMxZNgc6YLjQmdZAY0iAxZJz9clA5daKG4NlzrOdyw9+uwQjXmwJuaCmuqwjxaksphYov3cuL4z4v/A28z9nKAKoAEzO6F1mTRMZHORN6v1caapaoXQRUcv/TSJGXtAB3vaPy/dAe0oGGRqXbVsKQBl+afCAhUVjSybGQeMOXf/lrHcumWDITl4Zqd+uSVV/ye2WiOQwPOTAgOyeyFEJgYu8TUkfK0ZhdcxibsQu4TjDymjzXEn4B+HYhX3EoMPN3T7pZYxC+DtrmreP73PyN+uAtwhECBMXjWGE/7FYO1GhY3vcicGyFG/aWAQ/5xCyfeSSdX+YwajF/UwBXxTgR1m5kpC5E3SVdNSb0WUA645qdtkbLQj9ea1ru/uuvIeXzdLDCxxof6dL1c6+v2Wie0aGEBF2WdWvNU/0R6zPqdq8WH+VHvB20T4/yQJTp2Oo4M2kcL9XOtdciRtKGum+CfXNKgHtyn8NVZ1PeZKeXEZcTqAE5n7eRNkqpP+XZZ2NdL864cQdGnP4EK7/nH8WiJDDHPoRF5kJNXQrkTV33TX2VHo4N0jNG16js81f0NTdCZlQdUypWByLXH2v0nf/ifW4CTRuK8FjQoHi6GNoNgUGnYBPOLqrUg1T8Tj9hqzYen0eZw5Frj7wHOJZsYYGvd/WB4XUU+kmwzLjugRsrciOWo9u8wcla0r9Pe6rE/ZFsiiFHpBlTLxSX9wpY3Km1W9R9drECWgEgYxp0io1ei07G3WpJWH85SsTblfYn4F49Von61Z13aY5sZslynMyxTZlHsEZW1jVpdCuH3+v2TJNupIgiiKjxcnycFdT2LVO7QJQUJn1JjcwgTHSJVc4CcBFFashLwOLF/xX X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8dffd91c-765d-484c-c5dd-08d925d05640 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:38.2485 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: xhLtxmARJ9QMoF6boZyr5Bt/x4BlsmHXcLqQEB9M0udihBlLeXN1QEWJMRspvtzvKENnXzomwJbtIotFECgozA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Bit 31 in the page fault-error bit will be set when processor encounters an RMP violation. While at it, use the BIT() macro. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/trap_pf.h | 18 +++++++++++------- arch/x86/mm/fault.c | 1 + 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/trap_pf.h b/arch/x86/include/asm/trap_pf.h index 10b1de500ab1..29f678701753 100644 --- a/arch/x86/include/asm/trap_pf.h +++ b/arch/x86/include/asm/trap_pf.h @@ -2,6 +2,8 @@ #ifndef _ASM_X86_TRAP_PF_H #define _ASM_X86_TRAP_PF_H +#include /* BIT() macro */ + /* * Page fault error code bits: * @@ -12,15 +14,17 @@ * bit 4 == 1: fault was an instruction fetch * bit 5 == 1: protection keys block access * bit 15 == 1: SGX MMU page-fault + * bit 31 == 1: fault was an RMP violation */ enum x86_pf_error_code { - X86_PF_PROT = 1 << 0, - X86_PF_WRITE = 1 << 1, - X86_PF_USER = 1 << 2, - X86_PF_RSVD = 1 << 3, - X86_PF_INSTR = 1 << 4, - X86_PF_PK = 1 << 5, - X86_PF_SGX = 1 << 15, + X86_PF_PROT = BIT(0), + X86_PF_WRITE = BIT(1), + X86_PF_USER = BIT(2), + X86_PF_RSVD = BIT(3), + X86_PF_INSTR = BIT(4), + X86_PF_PK = BIT(5), + X86_PF_SGX = BIT(15), + X86_PF_RMP = BIT(31), }; #endif /* _ASM_X86_TRAP_PF_H */ diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 1c548ad00752..2715240c757e 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -545,6 +545,7 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad !(error_code & X86_PF_PROT) ? "not-present page" : (error_code & X86_PF_RSVD) ? "reserved bit violation" : (error_code & X86_PF_PK) ? "protection keys violation" : + (error_code & X86_PF_RMP) ? "rmp violation" : "permissions violation"); if (!(error_code & X86_PF_USER) && user_mode(regs)) { From patchwork Wed Jun 2 14:10:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294725 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 40CFDC47083 for ; Wed, 2 Jun 2021 14:13:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 2B5AD613F3 for ; Wed, 2 Jun 2021 14:13:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230333AbhFBOP3 (ORCPT ); Wed, 2 Jun 2021 10:15:29 -0400 Received: from mail-bn8nam11on2088.outbound.protection.outlook.com ([40.107.236.88]:32416 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231627AbhFBOOK (ORCPT ); Wed, 2 Jun 2021 10:14:10 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=cqtacyGbzahgZkDEbHsOWNePCQGPBYmqrsekjXTYgwgf2fkfTFoJxGM4JYYv4tip1fYN5ceIgG4DciBr4XUML4ETdoxjwVoM9aOhcFRzgxyyaZXguNu0M0zGE4siwcwCJrNJutEL8eh6BDKzY5vX/E11gu1QReq89he1kCh369q0L6B+mEjk1nHTLzqPvDlHwWH6nsZzLp0PmfQNdpzF4OpgrPikoS1369wFcoJuEV+IkkAR83yiqwUOZ++T+RAeAdNGMocnN6k/rAMI+XM3G5nWHbmWLp9X1W6j8bYfmGSCjQa/FjKdhFNLRPnE6+znDcM3yAbJVi169Jo1HvtBlw== 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-SenderADCheck; bh=UgzOEPHPOpw/L+40HOMnWYeNM0P9/hPteowwlWxGbZc=; b=ihReDO2C69FHIWSqoSRF9+8qqrWGn8RCsYzq5W+OB3ILPkdJi+VyCmn6962vn8hCL4NJv9wpfyBNewpfVqEgo9+l4W+cxkeg1KkEH0YIA6PL30UdBVsr/4/MBr4AECHVKZ+tDiwI2Gnbkc8LiTBpY/7Ra03bTLP2/B8tev9Z/cZb+CiCcyYkDJELURmEN/uFV7onbo83JDPpwdNRONs2sfI50V6JqStNgKlTKgFKM6KiDZK0CfZTCTvPD94qrb/hJOCl4Qoj/PYoZQovQyYwm+G8hif9cJn9882iOSbChkZj2k2yke7W3RGeDy4P/1g1B66/w6wn7IUv7NgXkBCcUQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=UgzOEPHPOpw/L+40HOMnWYeNM0P9/hPteowwlWxGbZc=; b=DUwO0SuYKBIZ8mKCmUbmbrMpJiKWweWgFVNGov66S6f7VZtL/p03fP/wFmdc0NXE+RTGyzGKATZkuO6tfX/VPT8WloWRwPRc7aGgbqy2JC9ALVSuU/y1ZPrAH22C0qUhnv+IaxAntTeHjHALraichMuirzFAZtYSJJR+J/p5T1M= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:39 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:39 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 09/37] x86/fault: Add support to dump RMP entry on fault Date: Wed, 2 Jun 2021 09:10:29 -0500 Message-Id: <20210602141057.27107-10-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6fa51aa9-0fb4-4e27-95f7-08d925d056fb X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tXdiSR3M74oQUNaaro04ssXRaaJmXPui4fYtThq8mkCpeQEBosxzKl41ZVYJaQ5EzJjag65dos+HaZE+AusJh7KkaGAppWBa/k/OX+trzd22PkhuPUVnZhfMUBxfxGjcPorbzXoIy4SUCAGNr1LYdL95AokOzEF16v+QBr1vHhKqIaK/mCzLXhfsH0pDw1auukM/r2NXo7DOqgVmr+hCcbfTSbsFbaKbX/6sX9KQfveUff3Ml5R0+3TJTXXdTkMblqpSo06N3sWzQ0G0Q+XiUFey0K+4VJsBoSitFWduOt4+N2sBbu3qZYc/97sO5AYaH+blrR7YM5bf3VNWSKohJbPRsJvvGuJZ+SwydY/FDYomSCBGSgBvCc2YyewGpPuNMfxDVhOTNzOPl941YMmYLN/pbUXT+oreimAGH03odVWVK+QSDhrt1mn93g9QB7TxQ6fvfjI6T9YwdcS2ADp20BUwp6PdK1FQ32rkr4tvsMRJQcz47j5genpI2LoT1pIGGy3AojCYgsZeI4Q7h8Nw3gFdxvb7WiQJ4niw/NtY79E/1tL0eHu4GxocJMj4XWywlp9+cUFdJ2QVgPqwG0HqAtC20EmZR4mHvfxESyEhjb0xy1zn0klm1V1FaBYgqUc6N3flbMFBo0aOQycVBv/57w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: XpRjVdqJRwyGFuvgLvDxejMzPo6YifoEpjHuk/3SZqtOXGtqmUiiArZUc0ljYMbuwM2iHX1VOVcL7FvQXMELojZmshw2gGZQCWr48tuu5J6a3C2VH/SRhW9i7bwfHUd9emOBJLpzCZ9OjfDfV/FsQBIpGI5raVKHXla+9dOSc3noZkDVHbQ7NVUzqq6D56ZnClngSCxfCpXPh+5Zz6Du0I1hNNhGMzm3aW3TYVS3CmBfY2le38wddPqkKC5IXBzecqwyOsAbmDz5yu2ZT3Oh9TYDt8FHSRcCMKTOiwyK1n18kjr08S2hH8/XWSaZKifbzmSO8qWwDo4/S6943xn1y0ZyCar5UnGcLQgYhJnYmgyuepV3RQajAW0aVpU6Of8WaS5eT4NFVXy83nNiEkgiRecIVk+n7Dy4tmOB0LY5qIZ8tMhoegrNMBIksoingHDJxW4+NZp5VHaFGzJaVkLqX9ZI2vxN/cY1jPgkAYszXvpge20BiUVdk+0oS+9PqF2rgt+Hhz6HpGkucacRRF2S5C8juozz5EdOf8g1hsGnqGSGJU3CJsbr+7l0ipZpnDUhScNP1+8Z+AqDUp0hNDOuNPy1tGEEw/hWkNPb7q8ToS56PcjSxJHm2nCnDMGr9bz1OopYplia9h0o+vuJKmeDWzLrOHWZd0eOZtqy3ypjhFqhUfTPHV05UF4adKTlUQRSkG3a4FVX4FmI4449aioYMbfXetqjIFCXHRDFjp6FUDi8GnE8yT5+RzWkgBa+GgrwoH6GNyjGonI9ZQGkg9nrS7XvCPVOUfY4mBL3ETNQauByaAdVfG0ICU4PTLLK/N7h055SszwIX+G8JoDkKccpkDBheN1HgCd5oYm5RuQ/4pd4S8QFPZU735mu+Qk2Z1GkCc20oh1TO9fYxtw/Lh6IuJDudCiwF54rb2yLi4gZuUFkisG+8YgUgM6ITyA0yhBbPw6WrHtrGuKXk7K3qnkcjuuzGoywLD8Jr7s8xdayi6+8ncaW1vUmmL0udYn0Yz2nQMbt2ILgWJwjV8Mn2+qPK8qeVetWSpuINwKluJc8xM0WkjAoUC7k9EvHPFRLLsHh4AarkG8x3bWRln4xZVgf8oi1l8flkwqx7bPiPGyQKqvxuyM+bZx7fidKJSIpLLAwKDAnoo4YkkfDuzqBoh/eyl1AFEH2D+zXl3jH0XYkJa0ZGyUc0tdqtWKP5vamyycYBhzjo6Sh1+fuV3dLFwFlxdqXQYDfFNSX9+Wd6rQgU0qdtzMDNIWneEdAzIFEqlNH19JiwgDIKPy3WOQ8RPQQ8bTxLD9i5ri/Gs2ahaApCVYUx/T61QeyvGAnhIcNA4rO X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6fa51aa9-0fb4-4e27-95f7-08d925d056fb X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:39.4818 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +pqCGAhLWOaNIeqS1H6qN6ccUctjYWphwHCf2/ikk7K05q3PoM6ELa8diHX/4h/1GLJM6i00eVsUH9saUEc6cQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is enabled globally, a write from the host goes through the RMP check. If the hardware encounters the check failure, then it raises the #PF (with RMP set). Dump the RMP table to help the debug. Signed-off-by: Brijesh Singh --- arch/x86/mm/fault.c | 78 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index 2715240c757e..e6deedf27d78 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -19,6 +19,7 @@ #include /* faulthandler_disabled() */ #include /* efi_crash_gracefully_on_page_fault()*/ #include +#include /* snp_lookup_page_in_rmptable() */ #include /* boot_cpu_has, ... */ #include /* dotraplinkage, ... */ @@ -502,6 +503,80 @@ static void show_ldttss(const struct desc_ptr *gdt, const char *name, u16 index) name, index, addr, (desc.limit0 | (desc.limit1 << 16))); } +static void dump_rmpentry(unsigned long address) +{ + struct rmpentry *e; + unsigned long pfn; + pgd_t *pgd; + pte_t *pte; + int level; + + pgd = __va(read_cr3_pa()); + pgd += pgd_index(address); + + pte = lookup_address_in_pgd(pgd, address, &level); + if (unlikely(!pte)) + return; + + switch (level) { + case PG_LEVEL_4K: { + pfn = pte_pfn(*pte); + break; + } + case PG_LEVEL_2M: { + pfn = pmd_pfn(*(pmd_t *)pte); + break; + } + case PG_LEVEL_1G: { + pfn = pud_pfn(*(pud_t *)pte); + break; + } + case PG_LEVEL_512G: { + pfn = p4d_pfn(*(p4d_t *)pte); + break; + } + default: + return; + } + + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &level); + if (unlikely(!e)) + return; + + /* + * If the RMP entry at the faulting address was not assigned, then dump may + * not provide any useful debug information. Iterate through the entire 2MB + * region, and dump the RMP entries if one of the bit in the RMP entry is set. + */ + if (rmpentry_assigned(e)) { + pr_alert("RMPEntry paddr 0x%lx [assigned=%d immutable=%d pagesize=%d gpa=0x%lx" + " asid=%d vmsa=%d validated=%d]\n", pfn << PAGE_SHIFT, + rmpentry_assigned(e), rmpentry_immutable(e), rmpentry_pagesize(e), + rmpentry_gpa(e), rmpentry_asid(e), rmpentry_vmsa(e), + rmpentry_validated(e)); + + pr_alert("RMPEntry paddr 0x%lx %016llx %016llx\n", pfn << PAGE_SHIFT, + e->high, e->low); + } else { + unsigned long pfn_end; + + pfn = pfn & ~0x1ff; + pfn_end = pfn + PTRS_PER_PMD; + + while (pfn < pfn_end) { + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &level); + + if (unlikely(!e)) + return; + + if (e->low || e->high) + pr_alert("RMPEntry paddr 0x%lx: %016llx %016llx\n", + pfn << PAGE_SHIFT, e->high, e->low); + pfn++; + } + } +} + static void show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long address) { @@ -578,6 +653,9 @@ show_fault_oops(struct pt_regs *regs, unsigned long error_code, unsigned long ad } dump_pagetable(address); + + if (error_code & X86_PF_RMP) + dump_rmpentry(address); } static noinline void From patchwork Wed Jun 2 14:10:30 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294727 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77D5FC47083 for ; Wed, 2 Jun 2021 14:14:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 53759613EA for ; Wed, 2 Jun 2021 14:14:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232140AbhFBOPv (ORCPT ); Wed, 2 Jun 2021 10:15:51 -0400 Received: from mail-bn8nam11on2066.outbound.protection.outlook.com ([40.107.236.66]:15585 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231737AbhFBOOW (ORCPT ); Wed, 2 Jun 2021 10:14:22 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=afBM8j+71HRxON9/Ck6T0eqAIzdGEVDEUH11aJXytP2+xLcESiWd0EfQmqcyVns9hSXMlnFtbRFeJ+hEi4qag0sXdrkuIcopA/Lpes8qnRLhr/mc0eo1qvKkPpL8qi4iGxtVABM/R2qdvEQB7AAEwMnkO+xmU2J3N0y+9BEJid4FPjOvyGDzLwU4qD+bFKYD4LBBPysQplu4sAAbKEYSBGezWnZDc3LPgIqmC9sKs27Uljj1VuuHapXLZpEDxhx1yyrqyzC+pKZLC/w4kSib7PVodMkPFALtNuV0MSF1tTLl0nN4PK1k2xVwOtNmJhloj3TUlmf2mKtfXEEy/r7LPg== 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-SenderADCheck; bh=2X+kv6FsknAp8BL9+Ji1NgfORFMy5n4Qe+gj50/+jl4=; b=oBMdJwAwH/rC+v2yWMI1BrCsGJxcsYR4sJ3qRHYK7uP9gpiGvihsYNX2dkauwH0NRkjkPrU/UPbondKF6dM7tdX5zstK89JqBjCqC3yZH+3tJgcaoXWNlFuhhX5FJCRLNZrR3iAsPyiURxCs6mIhJ2Gkm3ooALhRn7fYoqXCXYXCcwZKLK/qMFow30ZBo0MYFDdGAJZRtkmiIp0FUoKm/sdEGAbMQH10vlvblbul5ogfOfaxRib8zFsFJWzZcVKhU4I1aJaqT1WH21rmlRWO9AyjncdynGjdKxQe3AjOvpUsE7mhoEXZN5fKNhK1YJNO6sNnJ+ZnH8RRag4yDcFLEg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=2X+kv6FsknAp8BL9+Ji1NgfORFMy5n4Qe+gj50/+jl4=; b=plcpTrbWIPIRebQMAp4nsDnBFhHYjVlsIDUyjds5MO8mAHqqHVdZoQOgV3S2+o6gCNijrzCDKokioVV5ocyr028AH9q2FWiGjkBhCojdQzk1BhbY1z0hFMMi1ixUNYKA0SjEBhvnU9DCYW8AcGqMsdwukJwOSPmmWJqb6MYzFBs= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:40 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:40 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 10/37] x86/fault: Add support to handle the RMP fault for user address Date: Wed, 2 Jun 2021 09:10:30 -0500 Message-Id: <20210602141057.27107-11-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:39 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 74149feb-0f04-4535-fd10-08d925d057c1 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:332; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ThKKFvZDfhnAZyuTxdGd+Oie8mk6udTo3a7XIbZtQDrHbsslUVTT9I6QIvSnyjgGSUgWLCKSudZ0q3KNBQC6dNfcpSk4M84xf8R41AmEf2BmQOtW3WnvEKBgfs7MMDWYi0k3zvAkPzDXBxocQjGyqmmFepeX0w96KhezFLf0jLlCR1zCNKPK8fDXpN//zcWkvV3y3dRjP9a7TDrNVCNL8/vmgfL3ySBbmIVB+Xf7Gv+GtJX4yCm6fvEHRptZXFgHk9WvUg8/7oJizT9rIeia8Ds+UNzE/ZnaOP5Gjdzk3dEhqifPtkfxg/5LjSHq57QJTCvYrWeGYtMooSaOUkCYRLBsmO2wOA2IT+YJg+hJprJ5UpmT72he3aG2L5JGTPOID83hJ600hFpCqqNI+f0WIx/fruiLFjdE5WMLs1+fc7JoTGjuLNJmq1gzSBy6pRCw35BR/X9i8uvxofkObzUYn7WHljqb7Rc8/72rI8paDtzcSKXkJdvW9DqZpS/rouFjsBZTr/zjvT4xQIja2PaJlccRz07d0yHgoCzacfh7Pl5XfQLwDfE+t+5IRuZB4Ju83fIrIe9tg6uaFBsxptB/9qzQWTE4gb9B0ZOEty5/PHbRqkxzwc2hzRGurx+0TbxVtN33S3H0b/lkvXY6hSjU+A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: WcVQRwKt4miCTYwlCmnvCmaKbP3ivwfPHA2WCcOf3tk2Zttl+KBSe706BmASXMsnfeD4kkzXeX3+zpDLUQ/MfdoFed6nJF+hYVVXtS9HbRzYG4qlSh2yW3dvk2M4qfYma56oVCEu7BULBtVG3Bt6JxJxTPx2d/efDkqVLV+y7+Bp2TyJduqPv7zKrN9UBLkuQrlHNJECRSJAZYoMHkdjOqqxyB0HE0rVA319qfY9pAeD372y4PvqEMMs3FUi1q8/klikSlp4N01J3kF2UZ2houSab/4vM+X9nIoy//XDbGr29b6TFPt3BvIqlHchwdrd91vY7Rmp8jUjW0CGFcQ/1UTYpqB79EYMMu8wcPAg/xyzHOqqQMrOUmQeHxG3lHUyjtvC0nP5zj/b56KR37h5iu1x7b/5TQuKxheuKF7yatym+fA9kEwc8E1uZZhO/5svWENK69MdKsiQBpZXHr4HJyE2/mVheqQXp7/Y6fwqTmHrPq2/edvCSmlBcSaYCa0XSbwDuz5STiHItXgcRwVKdeJ2RVodSU/M4Cxf9coxIIa+t73Hh0gDGCYpX40exP7blwEsmPgwLIjudYHdR+Jp4BWXPjoIq7GDdvjZeFkfFFCxdRthTmxKh5uD7V8wH6daZV4eUTrjCRpN0gMnEFC0xL1LHNiDpv1+8WY5JjXm8yqb18eGdkGkpuHmLTomtQDco6YzH3E3+e3WaaW+arkUmKSxFx///+KjPz+Jk5BZ17+MBFZo1ttkgNj4GPJjglmrX/7aAPsJWvHeQrxUhDonEnhIdl9ch4m5ttXegHipgrXmyyYmwr/qKwKLJbcZGoVAFp08CuPHQF6RUgoIf1zpZAXpidNYcGo20EUhFls2tlKwJsiddIjWPnmKnW4DD7irzyWbOrbzg0oOWMSYoh+9NuCRNwuJMF7rujbIvp+yOzy7P2c6hANMrq3xWRbHS86JW//C/pNdVYUnNoMSU5rGAwP6A/VpKupf7m6AGGLulVLGt5S4iUxsQJzB9VzQlAN3U/T5Y006k/pi5KPxzloPbZoYjJeKzzwGNBOK/4Pa/fOGVSTOCji24R/N2HJhkIf1F0HjqH9P0DeS4TuI3pWKjY/OQoFuTiGl6YOn6Q8q2q7xuQ2IYjLHxxpFknzk7Ly06NrqjtmZ4p66CZH7rFKWp6yA2mTmSEN12n/qzs0ijFOr7tvRtU4X10NkPPiUk1/FvqaapEm6T+kgK8dDOGO6pPcawyCU02/kOr4CpEqGxP2nhGi616Cd6KE+3u0525yhhTfgDLoqxRP34cGKX49SD4TdKoCB41FCgpG7m2Ribt0RCMemy1Qq6VLa6BN+WNgR X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 74149feb-0f04-4535-fd10-08d925d057c1 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:40.7091 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: oFLsu7jjufmnNA4w6zacb0oynqwY2RDpZUvVZu5YTpepcKEoGA9LVu9sP7kGbNA7uY0bh1pMfnGgZ1YPQqmnrg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is enabled globally, a write from the host goes through the RMP check. When the host writes to pages, hardware checks the following conditions at the end of page walk: 1. Assigned bit in the RMP table is zero (i.e page is shared). 2. If the page table entry that gives the sPA indicates that the target page size is a large page, then all RMP entries for the 4KB constituting pages of the target must have the assigned bit 0. 3. Immutable bit in the RMP table is not zero. The hardware will raise page fault if one of the above conditions is not met. Try resolving the fault instead of taking fault again and again. If the host attempts to write to the guest private memory then send the SIGBUG signal to kill the process. If the page level between the host and RMP entry does not match, then split the address to keep the RMP and host page levels in sync. Signed-off-by: Brijesh Singh --- arch/x86/mm/fault.c | 69 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/mm.h | 6 +++- mm/memory.c | 13 +++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) diff --git a/arch/x86/mm/fault.c b/arch/x86/mm/fault.c index e6deedf27d78..f90d203a2e8f 100644 --- a/arch/x86/mm/fault.c +++ b/arch/x86/mm/fault.c @@ -1280,6 +1280,58 @@ do_kern_addr_fault(struct pt_regs *regs, unsigned long hw_error_code, } NOKPROBE_SYMBOL(do_kern_addr_fault); +#define RMP_FAULT_RETRY 0 +#define RMP_FAULT_KILL 1 +#define RMP_FAULT_PAGE_SPLIT 2 + +static inline size_t pages_per_hpage(int level) +{ + return page_level_size(level) / PAGE_SIZE; +} + +static int handle_user_rmp_page_fault(unsigned long hw_error_code, unsigned long address) +{ + unsigned long pfn, mask; + int rmp_level, level; + struct rmpentry *e; + pte_t *pte; + + if (unlikely(!cpu_feature_enabled(X86_FEATURE_SEV_SNP))) + return RMP_FAULT_KILL; + + /* Get the native page level */ + pte = lookup_address_in_mm(current->mm, address, &level); + if (unlikely(!pte)) + return RMP_FAULT_KILL; + + pfn = pte_pfn(*pte); + if (level > PG_LEVEL_4K) { + mask = pages_per_hpage(level) - pages_per_hpage(level - 1); + pfn |= (address >> PAGE_SHIFT) & mask; + } + + /* Get the page level from the RMP entry. */ + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level); + if (!e) + return RMP_FAULT_KILL; + + /* + * Check if the RMP violation is due to the guest private page access. We can + * not resolve this RMP fault, ask to kill the guest. + */ + if (rmpentry_assigned(e)) + return RMP_FAULT_KILL; + + /* + * Its a guest shared page, and the backing page level is higher than the RMP + * page level, request to split the page. + */ + if (level > rmp_level) + return RMP_FAULT_PAGE_SPLIT; + + return RMP_FAULT_RETRY; +} + /* * Handle faults in the user portion of the address space. Nothing in here * should check X86_PF_USER without a specific justification: for almost @@ -1297,6 +1349,7 @@ void do_user_addr_fault(struct pt_regs *regs, struct task_struct *tsk; struct mm_struct *mm; vm_fault_t fault; + int ret; unsigned int flags = FAULT_FLAG_DEFAULT; tsk = current; @@ -1377,6 +1430,22 @@ void do_user_addr_fault(struct pt_regs *regs, if (error_code & X86_PF_INSTR) flags |= FAULT_FLAG_INSTRUCTION; + /* + * If its an RMP violation, try resolving it. + */ + if (error_code & X86_PF_RMP) { + ret = handle_user_rmp_page_fault(error_code, address); + if (ret == RMP_FAULT_PAGE_SPLIT) { + flags |= FAULT_FLAG_PAGE_SPLIT; + } else if (ret == RMP_FAULT_KILL) { + fault |= VM_FAULT_SIGBUS; + do_sigbus(regs, error_code, address, fault); + return; + } else { + return; + } + } + #ifdef CONFIG_X86_64 /* * Faults in the vsyscall page might need emulation. The diff --git a/include/linux/mm.h b/include/linux/mm.h index 322ec61d0da7..211dfe5d3b1d 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -450,6 +450,8 @@ extern pgprot_t protection_map[16]; * @FAULT_FLAG_REMOTE: The fault is not for current task/mm. * @FAULT_FLAG_INSTRUCTION: The fault was during an instruction fetch. * @FAULT_FLAG_INTERRUPTIBLE: The fault can be interrupted by non-fatal signals. + * @FAULT_FLAG_PAGE_SPLIT: The fault was due page size mismatch, split the + * region to smaller page size and retry. * * About @FAULT_FLAG_ALLOW_RETRY and @FAULT_FLAG_TRIED: we can specify * whether we would allow page faults to retry by specifying these two @@ -481,6 +483,7 @@ enum fault_flag { FAULT_FLAG_REMOTE = 1 << 7, FAULT_FLAG_INSTRUCTION = 1 << 8, FAULT_FLAG_INTERRUPTIBLE = 1 << 9, + FAULT_FLAG_PAGE_SPLIT = 1 << 10, }; /* @@ -520,7 +523,8 @@ static inline bool fault_flag_allow_retry_first(enum fault_flag flags) { FAULT_FLAG_USER, "USER" }, \ { FAULT_FLAG_REMOTE, "REMOTE" }, \ { FAULT_FLAG_INSTRUCTION, "INSTRUCTION" }, \ - { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" } + { FAULT_FLAG_INTERRUPTIBLE, "INTERRUPTIBLE" }, \ + { FAULT_FLAG_PAGE_SPLIT, "PAGESPLIT" } /* * vm_fault is filled by the pagefault handler and passed to the vma's diff --git a/mm/memory.c b/mm/memory.c index 730daa00952b..aef261d94e33 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -4407,6 +4407,15 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf) return 0; } +static int handle_split_page_fault(struct vm_fault *vmf) +{ + if (!IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) + return VM_FAULT_SIGBUS; + + __split_huge_pmd(vmf->vma, vmf->pmd, vmf->address, false, NULL); + return 0; +} + /* * By the time we get here, we already hold the mm semaphore * @@ -4484,6 +4493,10 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, pmd_migration_entry_wait(mm, vmf.pmd); return 0; } + + if (flags & FAULT_FLAG_PAGE_SPLIT) + return handle_split_page_fault(&vmf); + if (pmd_trans_huge(orig_pmd) || pmd_devmap(orig_pmd)) { if (pmd_protnone(orig_pmd) && vma_is_accessible(vma)) return do_huge_pmd_numa_page(&vmf, orig_pmd); From patchwork Wed Jun 2 14:10:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294737 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 79304C4708F for ; Wed, 2 Jun 2021 14:14:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 606C961242 for ; Wed, 2 Jun 2021 14:14:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230029AbhFBOQb (ORCPT ); Wed, 2 Jun 2021 10:16:31 -0400 Received: from mail-bn8nam11on2062.outbound.protection.outlook.com ([40.107.236.62]:57888 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230208AbhFBOOu (ORCPT ); Wed, 2 Jun 2021 10:14:50 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bXf1qq8cRQFPniCwQwb3KkyoleLTqBeEL1UEj22pb/KoBJVMTcIarQ2jf13uoSqUspPRIs8LyijQ46Eur3rxx8IRlEZGe8iqX9NedCxNheUtbNfdMAIg0Ocp8TDhZoam3paXZyhja9iOMsPa8fqx4A7hOPceAOQtNCTr1mU7I4A4d9u948eEeughX7587XNi8uygNqRN0Z+7gwZTDLiC00ysJkzAP3OfyYw+icNq1MVjvi8EV9owURL703MxztLWTgqG3ulMxfXRVzsNM/1NVOxZf9OqTOL3tqtI6m1Rb7632Uh/RtbLuXtK5XQhOulaTsYTxR51esvniTql/8qvTw== 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-SenderADCheck; bh=g2Z7JSlGWPerJZfQq8QU8d7hr+48kKTAD1KSOj4Bnmo=; b=jvl5TSGmxFcqjHtB0n7lotOZkt4mF35MWxzlKnznxlpExe9zPiNTDyv/zBsmhv5AiBhP+8qY7ducpvVchZCug/8YyM23RaTwuKvkygwwL01TqEdnMPAJzwJKhDnN0sYbyRoSeWkm0dAnV3wVan49FKrOfAI9H8MhtYSK1PYicj2L+tC7u1sMyd3RCGBLQSbVBYDJhekM0+ykLzFr+X89e10+TEw2fRZvesUBXZy39xtfAlqzbpmccEDzG6bF7O9KqCJ/d1S9Q5WzcBlxTAeecQp77TswIcV3lQZMFBbu/3oj7vTscQDZcCNFWvjRBhtB6vLOxg/ScWOBP9zAWCCAGQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=g2Z7JSlGWPerJZfQq8QU8d7hr+48kKTAD1KSOj4Bnmo=; b=Gmysjav6szr5xnBlkSfYKZIWOCw0Px16oi7rX5qY1y8z71bRhc959loo6pVLEDTHHBTZHRY391ob5n/HG9rfSUdPfpuNSleuWWt9ja+J1a7wCKVwEHNZmV4bOQQuA3HgT7oMMJNQg7XDzZr4gAwvJsGgWGjqTL+pENJDvUtvkG8= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:42 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:42 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 11/37] crypto:ccp: Define the SEV-SNP commands Date: Wed, 2 Jun 2021 09:10:31 -0500 Message-Id: <20210602141057.27107-12-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:41 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 46a7b252-9342-48bd-2619-08d925d0589b X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GB+UiVaTdeLxSzzr1ZXayEJwuViagFtAxnDk2T/Q4PIZzXJk9Fx598bZvgMCE54OzRkAaGB3DhcgU17gEazldkQitWnFWKAPeCm0SfUxIgrROWO2VCC5B8pUcOIR+AXw2Weq0BZQuWMBX2SyATv675HVyE/XXtv7xMTkSUAxg0Dp8H91y7ZGOxMBF9YnFIkifaNMMDK/P/Qcn+bfcwK7R2idAEQ1A/dv9HzUkNM4LrnbPannmheVgAKQCAdTHkug64/MbMIyuDidZOQoUntcWjwAWcKTbb4+z9g11o7NljWLhk24H8jy6Wb+u9H3dSHXJvshwIPQ5VExNOLFUPI+Ctlavb9pz/2YSOymDvKmz9BoymjAvX6LhA7wbzrZfslYIq4sflTna5C/xXWjZxRGDdO/EJF8giXFxkgwb8lc1UFnSrKVeX5RKvD8A9FZQ8EVO7ijTQfuDH+M/OYdukRJoDGU+Z1iLkrxpBjh6QAKaJzORANv78vx6ALmMjMD93BEEe1fE4Cgk0JPok0m4HNMUE9DiXhK+3npX7Fse6SQwTvF9dXxAXIpOTf3XA1hBYNyz/B5A5DScp0LuSXD47xqb4qdMDPq/pvtb3HptNY0nUKN14ycp85FuPEIprr6cG889xuPaBikvAqM2wAmdsjO5w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(30864003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 9GP75woSh5w9vJdmeTg/7rHFX1LE/dK97yO3o5nu48oMmy5I7DVPKaOKL4zm/Z/hBnL0fu7CUjAYc44p002J6EgMQxISo0JPM3lyCF0/HWWz37GGOslQu4Z2sEhL95yggH92EfuBY1EkimTtOuFVliTMW3x46YpdJQLrvXO5FTi/BW+GB68iucirrxuF3QS8Bcz0rCDn5UEbcVsHfhVCIbfM3Zi1QUdqr8QHE8+h1+dWgociYilRgGZ7i9yzmtr666pZig+kmar+qgSTwmJ/jZM0iM0zDGlPXsOUr3CV5XOisRPSmRnwkNXVbU7CoTbE6zoIq42i55ZItufMF+R8g1ah7hR9UWeRbmLxyh3SCaCn0KhG3wOGboK2arG1XLr/ESzh4BV4rYqgOBOArmGHM2xw/Pvi2WbYo34Ozx7FOJSNqpg2JuCyXBeysXg22HYFKQBxWw8q3vjJGFCNyVAvB8VcSTdSCQMR8hFcMaJ3SBvI2HWmtMUwuz7zM69jUZzA1MuaQO5Z8k6ZZscVvKgRtXvGB2viyVPIHgijVyqTC9whd/p/5o5t4JfM7JkCZH0lJ8VPSI4I93ARNvfeHoq8gR4VqEpv2DRB2UNIDguWa3YsmyuYUBu7rgxGoh9i2yQyCU+XQ2K0nEoFyAZQ+roEWEL7GKJiLjLAiy54PbKXe/xyWyASfbc9ymIS9cZ3QCN/5C0RRVETPIPztaIcoOm5rlOq131bO7TYFChIZW7k2gzS99ZFk5mPVLYXMN/oChGnSM5ek53C2HVPNIGaB9KsOyKL02Ka5d8K2GUEM3qndbV4txTf6wFPWKQipii57Dkw7ePI8BShXghwdbw96waydqso6yMCYTAfk1lbkCqhOj+ySHLSoqHumhNblzChUZnx4b0bE9xAP2pQLf2cZ9cIKyaQoBuQ0k1z7AYNu3EpfKRtqR4Dt+WxYzwAafirc/aSvLTLkOUmcNRUgIFndTwgA2vIGhh780tgGA3JxXemAyT3opVxWKvINy0VNd6uDReMDj7NFp6Swhek4NUnzyKdHL/ok8dQQoGOargMdUFRFEFuf1j2xXQ0F8my2r56A/FwESMelhpFnVrsRpBjcLtli3z1iO/sU3OWAq5tAPC0OsHIRcIbUeOVTNYu+Cayx7TTsvD6t4+5LlzClNVcoWxbBfYgp/RRol0pS2uY54VOQutz2VtFsgw7KVV3axeIQqrzC/nsIcNxPLFAvZ8Z321rWYKG2OtWH2sdVE80JDye73s1L66tVmDEZsYx26J6jzUqWaYc6M/ZZ45A6Km5bZZSjc+ujsGtwksVeLO5AkgHsMRq6p56vBSv9bXsz8/31y8i X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 46a7b252-9342-48bd-2619-08d925d0589b X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:42.2152 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: FYwWJsiW1lfJfO0HyNYUhEenfLnXzYtOlKDZXb7NTjl5k9m69XKWpJsBbpUFsGOZAFpw+wUJjGDxxl2ikE2I9A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org AMD introduced the next generation of SEV called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware security protection. Define the commands and structures used to communicate with the AMD-SP when creating and managing the SEV-SNP guests. The SEV-SNP firmware spec is available at developer.amd.com/sev. Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 16 ++- include/linux/psp-sev.h | 222 +++++++++++++++++++++++++++++++++++ include/uapi/linux/psp-sev.h | 44 +++++++ 3 files changed, 281 insertions(+), 1 deletion(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 3506b2050fb8..0331d4cea7da 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -130,7 +130,21 @@ static int sev_cmd_buffer_len(int cmd) case SEV_CMD_DOWNLOAD_FIRMWARE: return sizeof(struct sev_data_download_firmware); case SEV_CMD_GET_ID: return sizeof(struct sev_data_get_id); case SEV_CMD_ATTESTATION_REPORT: return sizeof(struct sev_data_attestation_report); - case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); + case SEV_CMD_SEND_CANCEL: return sizeof(struct sev_data_send_cancel); + case SEV_CMD_SNP_GCTX_CREATE: return sizeof(struct sev_data_snp_gctx_create); + case SEV_CMD_SNP_LAUNCH_START: return sizeof(struct sev_data_snp_launch_start); + case SEV_CMD_SNP_LAUNCH_UPDATE: return sizeof(struct sev_data_snp_launch_update); + case SEV_CMD_SNP_ACTIVATE: return sizeof(struct sev_data_snp_activate); + case SEV_CMD_SNP_DECOMMISSION: return sizeof(struct sev_data_snp_decommission); + case SEV_CMD_SNP_PAGE_RECLAIM: return sizeof(struct sev_data_snp_page_reclaim); + case SEV_CMD_SNP_GUEST_STATUS: return sizeof(struct sev_data_snp_guest_status); + case SEV_CMD_SNP_LAUNCH_FINISH: return sizeof(struct sev_data_snp_launch_finish); + case SEV_CMD_SNP_DBG_DECRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_DBG_ENCRYPT: return sizeof(struct sev_data_snp_dbg); + case SEV_CMD_SNP_PAGE_UNSMASH: return sizeof(struct sev_data_snp_page_unsmash); + case SEV_CMD_SNP_PLATFORM_STATUS: return sizeof(struct sev_data_snp_platform_status_buf); + case SEV_CMD_SNP_GUEST_REQUEST: return sizeof(struct sev_data_snp_guest_request); + case SEV_CMD_SNP_CONFIG: return sizeof(struct sev_data_snp_config); default: return 0; } diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index d48a7192e881..c3755099ab55 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -85,6 +85,34 @@ enum sev_cmd { SEV_CMD_DBG_DECRYPT = 0x060, SEV_CMD_DBG_ENCRYPT = 0x061, + /* SNP specific commands */ + SEV_CMD_SNP_INIT = 0x81, + SEV_CMD_SNP_SHUTDOWN = 0x82, + SEV_CMD_SNP_PLATFORM_STATUS = 0x83, + SEV_CMD_SNP_DF_FLUSH = 0x84, + SEV_CMD_SNP_INIT_EX = 0x85, + SEV_CMD_SNP_DECOMMISSION = 0x90, + SEV_CMD_SNP_ACTIVATE = 0x91, + SEV_CMD_SNP_GUEST_STATUS = 0x92, + SEV_CMD_SNP_GCTX_CREATE = 0x93, + SEV_CMD_SNP_GUEST_REQUEST = 0x94, + SEV_CMD_SNP_ACTIVATE_EX = 0x95, + SEV_CMD_SNP_LAUNCH_START = 0xA0, + SEV_CMD_SNP_LAUNCH_UPDATE = 0xA1, + SEV_CMD_SNP_LAUNCH_FINISH = 0xA2, + SEV_CMD_SNP_DBG_DECRYPT = 0xB0, + SEV_CMD_SNP_DBG_ENCRYPT = 0xB1, + SEV_CMD_SNP_PAGE_SWAP_OUT = 0xC0, + SEV_CMD_SNP_PAGE_SWAP_IN = 0xC1, + SEV_CMD_SNP_PAGE_MOVE = 0xC2, + SEV_CMD_SNP_PAGE_MD_INIT = 0xC3, + SEV_CMD_SNP_PAGE_MD_RECLAIM = 0xC4, + SEV_CMD_SNP_PAGE_RO_RECLAIM = 0xC5, + SEV_CMD_SNP_PAGE_RO_RESTORE = 0xC6, + SEV_CMD_SNP_PAGE_RECLAIM = 0xC7, + SEV_CMD_SNP_PAGE_UNSMASH = 0xC8, + SEV_CMD_SNP_CONFIG = 0xC9, + SEV_CMD_MAX, }; @@ -510,6 +538,200 @@ struct sev_data_attestation_report { u32 len; /* In/Out */ } __packed; +/** + * struct sev_data_snp_platform_status_buf - SNP_PLATFORM_STATUS command params + * + * @address: physical address where the status should be copied + */ +struct sev_data_snp_platform_status_buf { + u64 status_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_download_firmware - SNP_DOWNLOAD_FIRMWARE command params + * + * @address: physical address of firmware image + * @len: len of the firmware image + */ +struct sev_data_snp_download_firmware { + u64 address; /* In */ + u32 len; /* In */ +} __packed; + +/** + * struct sev_data_snp_gctx_create - SNP_GCTX_CREATE command params + * + * @gctx_paddr: system physical address of the page donated to firmware by + * the hypervisor to contain the guest context. + */ +struct sev_data_snp_gctx_create { + u64 gctx_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_activate - SNP_ACTIVATE command params + * + * @gctx_paddr: system physical address guest context page + * @asid: ASID to bind to the guest + */ +struct sev_data_snp_activate { + u64 gctx_paddr; /* In */ + u32 asid; /* In */ +} __packed; + +/** + * struct sev_data_snp_decommission - SNP_DECOMMISSION command params + * + * @address: system physical address guest context page + */ +struct sev_data_snp_decommission { + u64 gctx_paddr; /* In */ +} __packed; + +/** + * struct sev_data_snp_launch_start - SNP_LAUNCH_START command params + * + * @gctx_addr: system physical address of guest context page + * @policy: guest policy + * @ma_gctx_addr: system physical address of migration agent + * @imi_en: launch flow is launching an IMI for the purpose of + * guest-assisted migration. + * @ma_en: the guest is associated with a migration agent + */ +struct sev_data_snp_launch_start { + u64 gctx_paddr; /* In */ + u64 policy; /* In */ + u64 ma_gctx_paddr; /* In */ + u32 ma_en:1; /* In */ + u32 imi_en:1; /* In */ + u32 rsvd:30; + u8 gosvw[16]; /* In */ +} __packed; + +/* SNP support page type */ +enum { + SNP_PAGE_TYPE_NORMAL = 0x1, + SNP_PAGE_TYPE_VMSA = 0x2, + SNP_PAGE_TYPE_ZERO = 0x3, + SNP_PAGE_TYPE_UNMEASURED = 0x4, + SNP_PAGE_TYPE_SECRET = 0x5, + SNP_PAGE_TYPE_CPUID = 0x6, + + SNP_PAGE_TYPE_MAX +}; + +/** + * struct sev_data_snp_launch_update - SNP_LAUNCH_UPDATE command params + * + * @gctx_addr: system physical address of guest context page + * @imi_page: indicates that this page is part of the IMI of the guest + * @page_type: encoded page type + * @page_size: page size 0 indicates 4K and 1 indicates 2MB page + * @address: system physical address of destination page to encrypt + * @vmpl3_perms: VMPL permission mask for VMPL3 + * @vmpl2_perms: VMPL permission mask for VMPL2 + * @vmpl1_perms: VMPL permission mask for VMPL1 + */ +struct sev_data_snp_launch_update { + u64 gctx_paddr; /* In */ + u32 page_size:1; /* In */ + u32 page_type:3; /* In */ + u32 imi_page:1; /* In */ + u32 rsvd:27; + u32 rsvd2; + u64 address; /* In */ + u32 rsvd3:8; + u32 vmpl3_perms:8; /* In */ + u32 vmpl2_perms:8; /* In */ + u32 vmpl1_perms:8; /* In */ + u32 rsvd4; +} __packed; + +/** + * struct sev_data_snp_launch_finish - SNP_LAUNCH_FINISH command params + * + * @gctx_addr: system pphysical address of guest context page + */ +struct sev_data_snp_launch_finish { + u64 gctx_paddr; + u64 id_block_paddr; + u64 id_auth_paddr; + u8 id_block_en:1; + u8 auth_key_en:1; + u64 rsvd:62; + u8 host_data[32]; +} __packed; + +/** + * struct sev_data_snp_guest_status - SNP_GUEST_STATUS command params + * + * @gctx_paddr: system physical address of guest context page + * @address: system physical address of guest status page + */ +struct sev_data_snp_guest_status { + u64 gctx_paddr; + u64 address; +} __packed; + +/** + * struct sev_data_snp_page_reclaim - SNP_PAGE_RECLAIM command params + * + * @paddr: system physical address of page to be claimed. The BIT0 indicate + * the page size. 0h indicates 4 kB and 1h indicates 2 MB page. + */ +struct sev_data_snp_page_reclaim { + u64 paddr; +} __packed; + +/** + * struct sev_data_snp_page_unsmash - SNP_PAGE_UNMASH command params + * + * @paddr: system physical address of page to be unmashed. The BIT0 indicate + * the page size. 0h indicates 4 kB and 1h indicates 2 MB page. + */ +struct sev_data_snp_page_unsmash { + u64 paddr; +} __packed; + +/** + * struct sev_data_dbg - DBG_ENCRYPT/DBG_DECRYPT command parameters + * + * @handle: handle of the VM to perform debug operation + * @src_addr: source address of data to operate on + * @dst_addr: destination address of data to operate on + * @len: len of data to operate on + */ +struct sev_data_snp_dbg { + u64 gctx_paddr; /* In */ + u64 src_addr; /* In */ + u64 dst_addr; /* In */ + u32 len; /* In */ +} __packed; + +/** + * struct sev_snp_guest_request - SNP_GUEST_REQUEST command params + * + * @gctx_paddr: system physical address of guest context page + * @req_paddr: system physical address of request page + * @res_paddr: system physical address of response page + */ +struct sev_data_snp_guest_request { + u64 gctx_paddr; /* In */ + u64 req_paddr; /* In */ + u64 res_paddr; /* In */ +} __packed; + +/** + * struuct sev_data_snp_init - SNP_INIT_EX structure + * + * @init_rmp: indicate that the RMP should be initialized. + */ +struct sev_data_snp_init_ex { + u32 init_rmp:1; + u32 rsvd:31; + u8 rsvd1[60]; +} __packed; + #ifdef CONFIG_CRYPTO_DEV_SP_PSP /** diff --git a/include/uapi/linux/psp-sev.h b/include/uapi/linux/psp-sev.h index 91b4c63d5cbf..b7207629eb90 100644 --- a/include/uapi/linux/psp-sev.h +++ b/include/uapi/linux/psp-sev.h @@ -28,6 +28,8 @@ enum { SEV_PEK_CERT_IMPORT, SEV_GET_ID, /* This command is deprecated, use SEV_GET_ID2 */ SEV_GET_ID2, + SNP_PLATFORM_STATUS = 255, + SNP_CONFIG, SEV_MAX, }; @@ -61,6 +63,13 @@ typedef enum { SEV_RET_INVALID_PARAM, SEV_RET_RESOURCE_LIMIT, SEV_RET_SECURE_DATA_INVALID, + SEV_RET_INVALID_PAGE_SIZE, + SEV_RET_INVALID_PAGE_STATE, + SEV_RET_INVALID_MDATA_ENTRY, + SEV_RET_INVALID_PAGE_OWNER, + SEV_RET_INVALID_PAGE_AEAD_OFLOW, + SEV_RET_RMP_INIT_REQUIRED, + SEV_RET_MAX, } sev_ret_code; @@ -147,6 +156,41 @@ struct sev_user_data_get_id2 { __u32 length; /* In/Out */ } __packed; +/** + * struct sev_data_snp_platform_status - Platform status + * + * @major: API major version + * @minor: API minor version + * @state: current platform state + * @build: firmware build id for the API version + * @guest_count: the number of guest currently managed by the firmware + * @tcb_version: current TCB version + */ +struct sev_user_snp_status { + __u8 api_major; /* Out */ + __u8 api_minor; /* Out */ + __u8 state; /* Out */ + __u8 rsvd; + __u32 build_id; /* Out */ + __u32 rsvd1; + __u32 guest_count; /* Out */ + __u64 tcb_version; /* Out */ + __u64 rsvd2; +} __packed; + +/** + * struct sev_data_snp_config - system wide configuration value for SNP. + * + * @reported_tcb: The TCB version to report in the guest attestation report. + * @mask_chip_id: Indicates that the CHID_ID field in the attestation report + * will always be zero. + */ +struct sev_data_snp_config { + __u64 reported_tcb; /* In */ + __u32 mask_chip_id; /* In */ + __u8 rsvd[52]; +} __packed; + /** * struct sev_issue_cmd - SEV ioctl parameters * From patchwork Wed Jun 2 14:10:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294745 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5FFC8C4708F for ; Wed, 2 Jun 2021 14:15:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 495AE613EE for ; Wed, 2 Jun 2021 14:15:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230204AbhFBORY (ORCPT ); Wed, 2 Jun 2021 10:17:24 -0400 Received: from mail-bn8nam11on2046.outbound.protection.outlook.com ([40.107.236.46]:14177 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232086AbhFBOPW (ORCPT ); Wed, 2 Jun 2021 10:15:22 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=m5dM77ilCsrnsKmWcP0aCRLj0G4TkE5YCg/Y12ziFyjAvMMMVGwSOGCc2L1BRnoM7LrUL2lvTEzViOKHSOUHOrwnniI5vwOuNRQc4Q/pMxsSjX7jgbTMwPWlhYYCHLGHOOoVtTzGP7vdYKcr5hc6cJ2z7XBx5Ix9Q2wCNFkyk+gU/tIu1NovDyqk8RC95hQQkkdIb3kQmpYMJnxcvE9wCCs1EcFfZSaKQB6+vUq9Ce5YM651Uqv3yCZ58DeDau6EeizcIhpoh8XQGcnkDKAkWW90va/RuKvDih9aQLFPiesqlYX8UeOywYJ869TPxSvcZTduX/0S6C8cXMGjsuxwtw== 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-SenderADCheck; bh=UuTtlJI+WhUL7P1v0QZxB9ntK8AC1kuUHWv7DNtgqec=; b=eG6Qz3huKK0qlmPrFcoFrtWGYaudvGz5H7+kVdMpMJj0oNsbowciBl8FbYyAZehHZe+CxKl27FjbLAQE7P4V1y+gSPo5/rSyEXs5rp0PUS/PmuwIJGuVSKQSGCo01L2vK0OGh2lHd4DScVmn2gsyVQfs0PzBnapbQYIKsc9AsgNLkM7dYPuws5hFFE+iTxamKUhQF+4FwurWbz5ANycsH20Z1N4Z8UPmZmdMiVat+OcB8EvEhupEQ8VJgU5TwsR5bOXI06/7IYgvCJ9ui5LFH0+k7uY8QLToKKRzVVulHmV+C4gEqV9U6rRDyNO4Pq/XZ8H4ZC16+N6uKaVnu1PQpw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=UuTtlJI+WhUL7P1v0QZxB9ntK8AC1kuUHWv7DNtgqec=; b=FHwo2nRsY1QSriOYA2ZMFRGHq0uJY7gfGbzO6A156oKqDBKZaVkNdYK0dm2wXGPS+I+qbp9GHs/2vc5wvclV1mnbLLJYYYY+RJCrnVSteZUgy2b9q01VotGNDksqE7AQF5Nb1AtF3I5WueyGrhG48GWydUMW578erYLbdZg8yMY= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:43 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:43 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 12/37] crypto: ccp: Add support to initialize the AMD-SP for SEV-SNP Date: Wed, 2 Jun 2021 09:10:32 -0500 Message-Id: <20210602141057.27107-13-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:42 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f65a89c6-c2a9-456d-077c-08d925d05957 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 3rZUSmjsuzZufBudDfjbA20/8tKgwP9gs3i2330q4jSzlfUkX3IZK7WfMKNNLt+bCXZeRNtuOU+JZq9iiqW9yRHkPwEWW64c15hj8Jg7oagz97NgDNGZAwUJZMaoVY9GrOwxLtKxg/xh2mA5fqd7xgGOltFZ8XA6wYBawOn2+HUKv3RFivGyorf7fU9Oh97cRw0paXnUzIITpCIhK1w+cl/Ii0ErpUuFpSlmaohR/iG6obplMcwv26ee/9qA606vhBJ6V4mG0QKIH8BZDwEYeEYtX4XYVvH4YPNSSm9QexvLVeb/g8HkpJZQ3zzotBqHO7VtanDQaBYSffOyp5eRbK97/putz6CDRJgNfRTQox+CFkc4S2mZZ2wOiNOS4e7JR62rOnO3evfpJ2aiyeNCsqrANQGNtRzDDfB3qyCwSWb1PjGJW3I26grtL8Gg4c/VVle7l4SnSYIC5wwy92KD+7TndyL6CxF0SjNOkW07iXZqgISdGnViOJZ0EMF9VV0tPLpm39hZyHqIEgb/yV2rKqE43dHlFjTozLY3DGxkYkYFX2w6gis/YLcT+dOlQN4Z4DFuVGATIAL2h0HTLgfprhhlK1geKJC8Yv58hUfAo1cwIjPMQ4hCoEkrFqCupRt9zizzPel7T7F679TsiyH3fykfzTL9VGQe2vQXTFLxwWpMlPffKB641zKWS+B6TdC0 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007)(134885004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: N0zHONSA6PE6B23xhflEql0rXZUFzO4GNi6lh8Ir9R88Qn8aOVmXBze7uEK6w6nm++mpKwklz9t0kI6dyXvsC9KHJQ+77rZKwIKrIH82WVhqXe6ZmW/uZPVpJou2I0Maszygt7jwNnIfLVHQnXU6PJLJL/0sRx5XNy3HCb42m27IHTzlh8YjbMhzhMEmz7BnGEVi2sY8svPV/7AOBh2GlZWBKnPGpoiqmI5pHaUPkdP8bKNe+qDzhjwbE/l3maT3Aftlamjv9i1smFcS5aw0FRZo2aH1cqU1Apu5KFIdzvVgaBvJLEvkne06cFhgTxm6ZNMzAgM3AkgTeRL0oLi0V33LVLIhAUOAJBwew5f2lWtSpn4vU9hlKKp0WTtC9KWUKBrxmupYcznvSErghcgDvjnJO6+9UXOo/dp6QUetoK0e5TvJz+vE6yOT6Pus4aV304kqB5cVCLT7H7o8cxodZqwqfFjmYHo2b6H3TPrnAdc/MzQDLk48GTtJPQZmawb2fK0fCdVPq0qwTz8wxltdvdQulT8mcA/vyN1hJVi3yMsQ0L1bgCG4RtiRFZpQqn7z89F//iL7CeJNPqLMx0Hczh0v18iAYTVDc12qHX/7vOWjoBEJcwmgUdY9AuUzIPC77SBqm36oqVjL6Z69YcrJHwmkM0JR54PK0q3GIG44LF8RYStVL14LX4hVUNGt2w4tjtzkWvr4v6Qqo8roFc/zHexiH6x+9uwjqRMZBhvHpwtp+b0FE+XJR6zClvl04LzVbMA2SJPdO8ppBQG1krPgYM9fCVyDf6KOJrDg5/TRN5wbGqui5C9L425+O4rwXXqNoWf3RAhSOWzjee9fCfDIZPVL9enAtSIghhHrIvKrij+89OPUX0t0oNO7tXAOMa6W4Ks0Aqh6iTUAq5qnWVIAnY5qYwuGHH4S/d2Kh9qlebfSuMR2Kd8eajBGvAuIkpsiWNtar4i07Jd9aiRfyxNI3XyDaGOgADhKrcCGUWzHRzEJb9OGr5nSQrj6TPiIjSfmXSdfG6DEi9B51mq3+Pc/DxLAO7/okd7WaLuYoe7WgegfmrJwaHSn70Qh/5D1KcEm7ljHlsgc7aCzMHBRpDa0oUYtwL3GNrzubCg6y9Ra2AoSeyvTaSzEk5FP2AYFnC/3keCl+o+0tYOyi3DfixZIl3a/dKIEkh9qkitRCOS8lms+7N9Lc3SjvOHSGV02/SDGKRWl+bUxrKyXDtirxQolPNuao8lkZ9xKAI2hOa+uDyIGWP2wIYmAn2+y5p9yM3K5ASKmbB2wR2DznSzUDtEXO8KOJazpmE6HS3QdjlRY2hv3A0a5bL6JLMikpz2EQQwQ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f65a89c6-c2a9-456d-077c-08d925d05957 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:43.3815 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: e9whaJ3936iNvAMi3R5uxGEQct6Z/ybFANuAl0ksS2ih62Ix/2xm+pxxtcTuFQCg05XiJjTeqIPMZTrQFENSsQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Before SNP VMs can be launched, the platform must be appropriately configured and initialized. Platform initialization is accomplished via the SNP_INIT command. Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 111 +++++++++++++++++++++++++++++++++-- drivers/crypto/ccp/sev-dev.h | 2 + include/linux/psp-sev.h | 16 +++++ 3 files changed, 124 insertions(+), 5 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 0331d4cea7da..2203167dbc2e 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -591,6 +591,92 @@ static int sev_update_firmware(struct device *dev) return ret; } +static void snp_set_hsave_pa(void *arg) +{ + wrmsrl(MSR_VM_HSAVE_PA, 0); +} + +static int __sev_snp_init_locked(int *error) +{ + struct psp_device *psp = psp_master; + struct sev_device *sev; + int rc = 0; + + if (!psp || !psp->sev_data) + return -ENODEV; + + sev = psp->sev_data; + + if (sev->snp_inited) + return 0; + + /* SNP_INIT requires the MSR_VM_HSAVE_PA must be set to 0h across all cores. */ + on_each_cpu(snp_set_hsave_pa, NULL, 1); + + /* Prepare for first SEV guest launch after INIT */ + wbinvd_on_all_cpus(); + + /* Issue the SNP_INIT firmware command. */ + rc = __sev_do_cmd_locked(SEV_CMD_SNP_INIT, NULL, error); + if (rc) + return rc; + + sev->snp_inited = true; + dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + + return rc; +} + +int sev_snp_init(int *error) +{ + int rc; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return -ENODEV; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_init_locked(error); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} +EXPORT_SYMBOL_GPL(sev_snp_init); + +static int __sev_snp_shutdown_locked(int *error) +{ + struct sev_device *sev = psp_master->sev_data; + int ret; + + if (!sev->snp_inited) + return 0; + + /* SHUTDOWN requires the DF_FLUSH */ + wbinvd_on_all_cpus(); + __sev_do_cmd_locked(SEV_CMD_SNP_DF_FLUSH, NULL, NULL); + + ret = __sev_do_cmd_locked(SEV_CMD_SNP_SHUTDOWN, NULL, error); + if (ret) { + dev_err(sev->dev, "SEV-SNP firmware shutdown failed\n"); + return ret; + } + + sev->snp_inited = false; + dev_dbg(sev->dev, "SEV-SNP firmware shutdown\n"); + + return ret; +} + +static int sev_snp_shutdown(int *error) +{ + int rc; + + mutex_lock(&sev_cmd_mutex); + rc = __sev_snp_shutdown_locked(NULL); + mutex_unlock(&sev_cmd_mutex); + + return rc; +} + static int sev_ioctl_do_pek_import(struct sev_issue_cmd *argp, bool writable) { struct sev_device *sev = psp_master->sev_data; @@ -1095,6 +1181,21 @@ void sev_pci_init(void) "SEV: TMR allocation failed, SEV-ES support unavailable\n"); } + /* + * If boot CPU supports the SNP, then let first attempt to initialize + * the SNP firmware. + */ + if (cpu_feature_enabled(X86_FEATURE_SEV_SNP)) { + rc = sev_snp_init(&error); + if (rc) { + /* + * If we failed to INIT SNP then don't abort the probe. + * Continue to initialize the legacy SEV firmware. + */ + dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error); + } + } + /* Initialize the platform */ rc = sev_platform_init(&error); if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { @@ -1109,13 +1210,11 @@ void sev_pci_init(void) rc = sev_platform_init(&error); } - if (rc) { + if (rc) dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error); - return; - } - dev_info(sev->dev, "SEV API:%d.%d build:%d\n", sev->api_major, - sev->api_minor, sev->build); + dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_inited ? + "-SNP" : "", sev->api_major, sev->api_minor, sev->build); return; @@ -1138,4 +1237,6 @@ void sev_pci_exit(void) get_order(SEV_ES_TMR_SIZE)); sev_es_tmr = NULL; } + + sev_snp_shutdown(NULL); } diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 666c21eb81ab..186ad20cbd24 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -52,6 +52,8 @@ struct sev_device { u8 build; void *cmd_buf; + + bool snp_inited; }; int sev_dev_init(struct psp_device *psp); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index c3755099ab55..1b53e8782250 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -748,6 +748,20 @@ struct sev_data_snp_init_ex { */ int sev_platform_init(int *error); +/** + * sev_snp_init - perform SEV SNP_INIT command + * + * @error: SEV command return code + * + * Returns: + * 0 if the SEV successfully processed the command + * -%ENODEV if the SEV device is not available + * -%ENOTSUPP if the SEV does not support SEV + * -%ETIMEDOUT if the SEV command timed out + * -%EIO if the SEV returned a non-zero return code + */ +int sev_snp_init(int *error); + /** * sev_platform_status - perform SEV PLATFORM_STATUS command * @@ -855,6 +869,8 @@ sev_platform_status(struct sev_user_data_status *status, int *error) { return -E static inline int sev_platform_init(int *error) { return -ENODEV; } +static inline int sev_snp_init(int *error) { return -ENODEV; } + static inline int sev_guest_deactivate(struct sev_data_deactivate *data, int *error) { return -ENODEV; } From patchwork Wed Jun 2 14:10:33 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294717 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C37C0C47096 for ; Wed, 2 Jun 2021 14:13:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id ACABB613EE for ; Wed, 2 Jun 2021 14:13:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231913AbhFBOOp (ORCPT ); Wed, 2 Jun 2021 10:14:45 -0400 Received: from mail-dm3nam07on2077.outbound.protection.outlook.com ([40.107.95.77]:45953 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230093AbhFBOOD (ORCPT ); Wed, 2 Jun 2021 10:14:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=FGDPsyS4ZxhEg4hiaiascxQ7QpmbXicT3WNikyRfsDS4iONCTQZtucYrdO8/lO8i2hsx0vFsSKcZD4JWX/UxjWtwuxCW3JxuE266gh5sCoK0mEWdRgHuDdmhOvPjz6Qje2JaVnGLqVv/NVmZpvtzf3Yvyd8MW5LvoA9c6UlmYylIElgLt5/36Le5EQnr1lE0pRPaKu0dyfehxCIrscVE4uNwk9/b+Wl2JNNGLnh9mheKWqRBBEunk4H94Z48uQgslp5EmieErNvNN13xRs6olAT3/xQu7rSuSLNnsF8Ds5xF6K3ZcsGpd+JC7TW0RzfKuYQijxWxH6Zq3m7YoLf/9w== 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-SenderADCheck; bh=SA+GvkxqcIKuAmhsi0VrzFsAQZ2+ZzX/nHKGnHU+H0I=; b=AGk7TZj7dC0JHWr7fCIYSKRBd+vQXM1AmA7ewuhZpnJXvn0mgP43bGeOSfkwYoDqlH7S4yztmFEA+wmWqepY7stLy+Inrqcp00Ms3BA3U6OCipWcRD5bsdK/7NKwc/YM6axTLAqyLm1R6dADfk8MuYR1AGYm45S1oigmRQxE4A8a0XvZasVpXhYMPZTov7svIJv/m49LbYrtKF8iOmN62vbojPgKCQ4V/Xt8OzLMOXEK6HoJKZitqmlwyhrtmrE9lGAqcFGmVZyhRZG4Q5l/tUd9s3WKAxgo+ASUREx+OB+7Rz63WUSAkoi+s8022B/xNHpdiGrHpHiRlsyePdKatA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=SA+GvkxqcIKuAmhsi0VrzFsAQZ2+ZzX/nHKGnHU+H0I=; b=KlcbnCxUeLYZTOu7fnt0wy5vx7WkxbMADWMQb2H4pKJPb6S2nOyJNdsx130ulc31nfn0tI3autdQSv+EFq18XmVCexqfc+G9JmO47GFB9WHHRWeQrfxFDClu4kikPrvh3GU0HpQ1fMFoBHns8yfMm1mB3SzL71pFwZ+KXxD0fbk= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:45 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:45 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 13/37] crypto: ccp: Shutdown SNP firmware on kexec Date: Wed, 2 Jun 2021 09:10:33 -0500 Message-Id: <20210602141057.27107-14-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:43 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f35e2d1b-f209-41d8-a08b-08d925d059fe X-MS-TrafficTypeDiagnostic: SA0PR12MB4495: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5516; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: H8NFa4KWEyWqKdCBxPsG/ScuhA0vo5Ur628Ztj9I+dBZGoU0avUG/u3kO8hDlfVyRE5IyH2eVmsKVj6ZFl/3o6PbB58pYIfl4qSZjlOtd9hcPHRyulztbfdL/i4ISaWg+ZynqTm21nFOSKv6/FTUfm0VIyZDCEotKXGVWIm3PASNVL09oQ21Bn/MuFtZjgBmAXkBjqkpIy8cBdF0tv1ejl6rccmJAZxyCPrltLA9qWgLEQxa6o/rOSOO0oV0xVwNDUlcfEAGj/RTUKhKxlY6InOeR0sBYBQfqVAj+C2FEwHV8PlGDqSpSBMiLYWMTfiRbxjWrQWuHxy9b29PwJ0A5GMFN520EWhMj1UMe1l49hfQNrLTzEboym3UMwEbDWdO/JrAiBylWLuD9UQQ41NvLqR1xhYyJCvz8qG+wDe7zcCWaEi+4SacoW9fF6YwTOiKM9RMjf7CDUtYstw2WKC4QS3ohRHAzRcdOp9mxa1BN/jhodsANHhBXpDQyiNHoqdYOEetAEBF0kXDnV8lNELYUE7to+xOXluDg+WTmYYe+VBFPZnAgGkjPqYNRm0aot3HkYFQ8FutVGMmN1eUUKRs+GwkELbbIFN0VyW/U5uX3bvK6w7EOIXyDQdjHZrr01R0VZPnb5n13j1TVbeVmlVl9g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(396003)(346002)(366004)(39860400002)(1076003)(54906003)(44832011)(8676002)(2616005)(956004)(478600001)(6486002)(316002)(86362001)(4326008)(7416002)(6666004)(38100700002)(7696005)(38350700002)(52116002)(16526019)(26005)(186003)(2906002)(5660300002)(66946007)(8936002)(83380400001)(36756003)(66556008)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 6dNlb4UavJy88d2ZFHiT3FrNwBVEHXwSQF34vEkeK/9oWtwHfsVzEuERVJ9NylKGVZF6b4B3pEknP4C/rEIXYJvUsK8SK77Qi9u7gZYErV0s9mrd9E4aTyBqVT5UkbUwutGbfUgEoelLSpwE1a5Ub1QlLuRrB+JN4eZI/eIbRz3BMd24TKdeTuIYfgl67vZlyK/mwXHVpQ8laUb7tFAG2sfPu+A1JH1GkNAvu14RmmNpYxDOV4zy7CMy0shFvE+ixdRhAzh81PoOS96WqQbNirRmPwbT8Gh4bygb1pxNO13HKN4ad13sCgUf5i1FuHI8SS6lsTbl7coN6jsrexsbFi3Ize6ONxU1ovDu0dsTC+m9idbaYkw/nYjUsd/b29saEe1H9cV8bOTl3vFsY+ec1hWgwrpmCkzndeQEnx2hVv0C3TgKgcnWZIeOzMcXOFm1BMAPRUcR8SmvlQT/De/oSsJXE6XTXJE6cqKRgCFbBbLODl/buohtXWbVcERQ3e4ZzcT+xz02mg0kyh0R7GPB5qhLCiND3sPGHY/+ZjkvCx4pUw386k9dQninzrAdi0Mu9Cj4JECSIPoF+U9BsTV00eGIQnsg2yMHzHraYYjpvPqVYtMsHjeVdiuN145gUI+lLVsudko5j2A3ogZMvr2wbLfxPiLGeI67Gth9Y/yOZ5YynTmDuMdurHiI1M9pCdGeVQY3FdFLmNYMCdMh+MLFUdNN3weVi7/afrL3WDGDg0btI3IM+jXxRs58jwpieDlXEckBUFmFq1ux2XOOPVtaNbUDqRSNCvDXhY6jyheUvNXm85j0/qhGxT+z0LbwwfEMOsn8dflRPItFgrjU5/9tjnCc9EJqsR8rHMsl7jWX7X8UGVAiwHTx/DJG0EKs1bWQFr7N/TJArYSOs4zqd6Jf1XsO5NFfHJ6AMtrKsPilBwJA9aM5myKiO52E8aX8DNl3A2LByEyaafEgD2RlhQvIqSRS7rEqli0yYVhyOH0GVThNucAKqyGZcBVvW4tGk0/lgn+gRdY4Thj8m+fqv365GKGEIjpsaW9mPXvoRH34MVgF9taQY4XJTMaEZoyybY7kyJllViNvLn1ulMLT+jaHzoEhszXUntf0UjjE/cIQqQaCwLyNXeNA+9E0+EoaenUxgMMcCJtFI8RqOuV9q8a+Mx0kUHBYAZMvtaN2nhmBYNS/R8ac/5IjHeDk4lVIj3qrF47iGBDRw3vhRXqDAvwQRPYEg25mS/eWry0RzTUEZSXeFNVRa89YtXldtzkTOLxQk3o77gydl634o3KYhQaq9u8zBfgKo+KgtM57eQlOGI6FzKdcUMkqZtoX25rXi69n X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f35e2d1b-f209-41d8-a08b-08d925d059fe X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:44.4799 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: I/NlmFy8BklacN89fQZrdQhTBPvkx9g67AzV2T3zqD7FnKuoqH1Fm5M4Jzluo0SExPM89Lo3CYN7jhfg3EPT5Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When the kernel is getting ready to kexec, it calls the device_shutdown() to allow drivers to cleanup before the kexec. If SEV firmware is initialized then shut it down before kexec'ing the new kernel. Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 53 +++++++++++++++++------------------- drivers/crypto/ccp/sp-pci.c | 12 ++++++++ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index 2203167dbc2e..b225face37b1 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -310,6 +310,9 @@ static int __sev_platform_shutdown_locked(int *error) struct sev_device *sev = psp_master->sev_data; int ret; + if (sev->state == SEV_STATE_UNINIT) + return 0; + ret = __sev_do_cmd_locked(SEV_CMD_SHUTDOWN, NULL, error); if (ret) return ret; @@ -1115,6 +1118,22 @@ int sev_dev_init(struct psp_device *psp) return ret; } +static void sev_firmware_shutdown(struct sev_device *sev) +{ + sev_platform_shutdown(NULL); + + if (sev_es_tmr) { + /* The TMR area was encrypted, flush it from the cache */ + wbinvd_on_all_cpus(); + + free_pages((unsigned long)sev_es_tmr, + get_order(SEV_ES_TMR_SIZE)); + sev_es_tmr = NULL; + } + + sev_snp_shutdown(NULL); +} + void sev_dev_destroy(struct psp_device *psp) { struct sev_device *sev = psp->sev_data; @@ -1122,6 +1141,8 @@ void sev_dev_destroy(struct psp_device *psp) if (!sev) return; + sev_firmware_shutdown(sev); + if (sev->misc) kref_put(&misc_dev->refcount, sev_exit); @@ -1152,21 +1173,6 @@ void sev_pci_init(void) if (sev_get_api_version()) goto err; - /* - * If platform is not in UNINIT state then firmware upgrade and/or - * platform INIT command will fail. These command require UNINIT state. - * - * In a normal boot we should never run into case where the firmware - * is not in UNINIT state on boot. But in case of kexec boot, a reboot - * may not go through a typical shutdown sequence and may leave the - * firmware in INIT or WORKING state. - */ - - if (sev->state != SEV_STATE_UNINIT) { - sev_platform_shutdown(NULL); - sev->state = SEV_STATE_UNINIT; - } - if (sev_version_greater_or_equal(0, 15) && sev_update_firmware(sev->dev) == 0) sev_get_api_version(); @@ -1224,19 +1230,10 @@ void sev_pci_init(void) void sev_pci_exit(void) { - if (!psp_master->sev_data) - return; - - sev_platform_shutdown(NULL); - - if (sev_es_tmr) { - /* The TMR area was encrypted, flush it from the cache */ - wbinvd_on_all_cpus(); + struct sev_device *sev = psp_master->sev_data; - free_pages((unsigned long)sev_es_tmr, - get_order(SEV_ES_TMR_SIZE)); - sev_es_tmr = NULL; - } + if (!sev) + return; - sev_snp_shutdown(NULL); + sev_firmware_shutdown(sev); } diff --git a/drivers/crypto/ccp/sp-pci.c b/drivers/crypto/ccp/sp-pci.c index f468594ef8af..fb1b499bf04d 100644 --- a/drivers/crypto/ccp/sp-pci.c +++ b/drivers/crypto/ccp/sp-pci.c @@ -239,6 +239,17 @@ static int sp_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) return ret; } +static void sp_pci_shutdown(struct pci_dev *pdev) +{ + struct device *dev = &pdev->dev; + struct sp_device *sp = dev_get_drvdata(dev); + + if (!sp) + return; + + sp_destroy(sp); +} + static void sp_pci_remove(struct pci_dev *pdev) { struct device *dev = &pdev->dev; @@ -369,6 +380,7 @@ static struct pci_driver sp_pci_driver = { .id_table = sp_pci_table, .probe = sp_pci_probe, .remove = sp_pci_remove, + .shutdown = sp_pci_shutdown, .driver.pm = &sp_pci_pm_ops, }; From patchwork Wed Jun 2 14:10:34 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294721 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57A19C47083 for ; Wed, 2 Jun 2021 14:13:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 3CD93613F4 for ; Wed, 2 Jun 2021 14:13:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232019AbhFBOPH (ORCPT ); Wed, 2 Jun 2021 10:15:07 -0400 Received: from mail-dm3nam07on2058.outbound.protection.outlook.com ([40.107.95.58]:30496 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231577AbhFBOOJ (ORCPT ); Wed, 2 Jun 2021 10:14:09 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IlrsWkbIRI5WR73zbkyQdUSqA1DxyWADxEKiONXPzUikgAZhk10nF3HiaDGI1LfXkVVCaR7yaFIcii+EXsnE7+4HF4RzZyd2cqOibem3rRvXYrqrDr4/DmM26c6BcrzO3UVkJLgnuxbH5Q1RoxcWcatwi9E6KQ4UjHRs6ixwlafx17oIy7T4T2j6cjHyj588njh1u6HpNdloRFd6Zinjw6fBmHe6fJ6BNwiIA7KduXhgtcfx6VDm9FlqLPNEgAC8CytzHJquYSicStgBtfongXnvgqgYdALYbM+tGkPUry7woGdYJ+OoytLg7bGcNhp6ozZsUTpnoXrUWzAdQ/0u/g== 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-SenderADCheck; bh=yCVBTJ4FkMf/LVJ8T6ab3ZBuJBaCUbSHHcaR8JsMZ44=; b=SlHc5CSL2xnizRhyYV3Xit/CamOV6ftEjy6Yyc86WnaZl2yILY43VK9v7TPx0QA/tkJ6frd9dNgJzX9O2SeY1yVzSBBBuMm/EcMaXac0WDUlzCSYHvs0k0F/Y75KmKv7owTxqsX7GFGH2QFXuWZxgc1T7SG9JBHuEaut5mpmUAxe+KjVfP813fzYkH3+L96viTk671+mgX2c/W92PT99Y05GZkMArVqTDi1Ukj0XWL4R3kBt/4cEDRb70n7vDkYFIWYzTPnl1Bm2XNkweysaYOcRqwWpDvaltqC0UykTAoXNGcnxqUi4cINRt1GOgm6VOAmt2cGd46YCB81uH0Fnlw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=yCVBTJ4FkMf/LVJ8T6ab3ZBuJBaCUbSHHcaR8JsMZ44=; b=ZXp6XVQYooibXtonB/puQ/5gJyKtCB3T9+r0671zW37MmVfc412LiBd6IXn5kzipUXTDqI1GurIj7+Je82RnfWISHveq8ila9fsZ22WytsWnWj5jAX0XC9GnvAnNEeErsD02ZGEZGgeDl3eeGLa9cSK0dc6C3MbV7uTtjqUXXS8= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:45 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:45 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 14/37] crypto:ccp: Provide APIs to issue SEV-SNP commands Date: Wed, 2 Jun 2021 09:10:34 -0500 Message-Id: <20210602141057.27107-15-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:44 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 99290dbf-bee0-415c-1b7f-08d925d05aa2 X-MS-TrafficTypeDiagnostic: SA0PR12MB4495: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 2iiSJzswINHcWpY18ijCQO2JcTsmPWeRT+m7JOEc4x/KTdmwxL1d/crWE7+RQKExIsHq+oV0CBFjKbF4ecu82GFUT1Xz/1VPGdGHnUUPUNG6Zbwd+khqZi2E8SdFp3mWsltM16gDzSGU26otYk7YUuUn8XNqM/cc6PznXSxWpoEQvqtq69gAzR7cMrUGwguFfMbB0WiwZk6+nEhcaOAXtak3g4EAdWPh2ibhQeao73FoVjk9MWG0vvjOEbizemukHwNQa4b+DdQ22jVl1z88s22vYygelLMefRGqA41WPoEKlEQGqLAfQAm6roGYiJuxXDJeG/vUOtrstRJP38WpOF5EBSQwFyq4cQ5Zu8tOOvdy+k3X45hkay9pD9JWW6MTNgiXUCaB5VvzQJRgCwR4id7+aZANap+XgWzUP2uDUH0KzG3lVhSjxvAo0I+jK6d3QqE2qemFEVwGGXkpwK2eHwcoX05+HfyTS2/QLR7MBm6XUX4u82qxTWq6tLaCarna5DceQMJVmufpPLOf6mY25RPOH6GJnFM0cPACpAkHUUnbu7As0D5vZTUgJwrAzOTxGAA428WMC4eL7S542FmM+lq1KDnL/Hbe086tEXIaEp0chvkBfqTiPGDL59Iv72p2Ud4vU98stmDpR6M5a9uO/zhZ3vrOfUEQPnHFfrF31BtG5okaRbHc8tMVhU38Ny6K X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(396003)(346002)(366004)(39860400002)(1076003)(54906003)(44832011)(8676002)(2616005)(956004)(478600001)(6486002)(316002)(86362001)(4326008)(7416002)(6666004)(38100700002)(7696005)(38350700002)(52116002)(16526019)(26005)(186003)(2906002)(5660300002)(66946007)(8936002)(36756003)(66556008)(66476007)(134885004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: SduIdjJww+g3wrgRqbbatnozuZ9wsshODX5es3yuPxgOgkiH3RWAm66I95XB6Dk3Q91FWqT/kdEVjglsWh4joSCBD7dRSnGH0ZT3bcclsQHBEkMOHHCtNVatvmLg5bVyDpgXfRoB+EsA0TdQ1FjZ7GtsZC/Vn20zrxIxDP1LwmirlRUP2rcMPyIMjXvGSu6FBjiNCUizjVCal+Ch+1nc0816t6RXhsA6r23QrCqDCRkTjWAF1M0XCViisPv1X68UWKI9aaNZtgVW2L84t5YDrELkV2Nf6G7myP88ZOAn5MZjvqVw2rYEqAUgZKeHtHKUoDZMNkyD/A1pqeEpAWHWEVAtn/jPAvUuCtW/iRzG3ey9Z5lkIrhWBvWeUqy5w1x6yqFYBgEt6PHOWZo2o04ov4R9imDnhtj02FKpSlpM0b6yzyh80dV9gVy3Fm183+6FEv6Yhlq63w6o2BwZsAWtv9T50RuqesGJrxWfzIFMm9QDpGMO7JQDYuiiX07xdnfZFLRVkpDMO9j0uyjeodFBqN6p2YfCEb9Cs4XK1b4ekCRALkRyO6aDG+7tC3dL3JzK3j+nkDrlA8XL4bB+1XNR7mOqMz6ZWUNvlkPS9qzch1aEpb1ZyOIWtUh/NZyqi0lVQA+7++o0rTb69ysbEixpkvXgft6nEx86NDeYtahMtd1fUeKsNSiQy+rJRzCn4hPhedqTMI5KPmwVDyhuR8TuMgeGsat647O+CSDEDloepwqijiwUTT1mPfrgWte6Up1DpzwrYaZFR2eusxQCtZ7ALPet8yAacCG26OgNqz5Mj0OcXbwuizMXUE3pujax+OqaguC9H9reuRZXHkQJuJ5frqGxKjh9jNEYJISAaSZ4Km/3cHTaBR2kMhLWbTUd9xUiiRFeJacf1JexvQrZ0mEB/VxFvrPANqGy7z274U1aD5RgeVn2CpXwItk4FYgxJqs7GM3Lh7tESlPu/JSfTWX+n/wM4JZl6Pte8NWarHhgDxYjs5cKqAG7PVVllp7OWKr5e11rAptWnPLKewhMwZvpL5l7iCzORxqDOYPb3fqE/kCOrGVeziClnIU3i9U3ioYx4VqQDjpxh13r+FM+LG9er76Nm/pVFGIIBVN1YHfVczRvCnBGDpJpxLTHmKxEIOpEEFfv2Lv5rP6iDYaAWgiqRerdJBH+9MbQ+6gC+XDh5Oa2pr3F++Yf+JqS769OkCWAMQJ+8BU5iNiJiaftx09ewHjyjEE8jJ1X+FTMb/cGakiCVBLnW4VR7UesRUUPr/8n1I1+A0JMg042NhBKJVpxgDIieXhBcJ7vGWFUYTQkyYn8Mla4KLjTsLiex7NmAwEG X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 99290dbf-bee0-415c-1b7f-08d925d05aa2 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:45.5573 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: UCMficEVXTd0Zx4QMYOHqU+i3lubG2CbkQ6owAQoXDwa7xTFe2gWmCqArgE1CyZfnilznBi1OKql4LbYKbJBTg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Provide the APIs for the hypervisor to manage an SEV-SNP guest. The commands for SEV-SNP is defined in the SEV-SNP firmware specification. Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 24 ++++++++++++ include/linux/psp-sev.h | 74 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index b225face37b1..def2996111db 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -1014,6 +1014,30 @@ int sev_guest_df_flush(int *error) } EXPORT_SYMBOL_GPL(sev_guest_df_flush); +int snp_guest_decommission(struct sev_data_snp_decommission *data, int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_DECOMMISSION, data, error); +} +EXPORT_SYMBOL_GPL(snp_guest_decommission); + +int snp_guest_df_flush(int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_DF_FLUSH, NULL, error); +} +EXPORT_SYMBOL_GPL(snp_guest_df_flush); + +int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, data, error); +} +EXPORT_SYMBOL_GPL(snp_guest_page_reclaim); + +int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) +{ + return sev_do_cmd(SEV_CMD_SNP_DBG_DECRYPT, data, error); +} +EXPORT_SYMBOL_GPL(snp_guest_dbg_decrypt); + static void sev_exit(struct kref *ref) { misc_deregister(&misc_dev->misc); diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 1b53e8782250..63ef766cbd7a 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -860,6 +860,65 @@ int sev_guest_df_flush(int *error); */ int sev_guest_decommission(struct sev_data_decommission *data, int *error); +/** + * snp_guest_df_flush - perform SNP DF_FLUSH command + * + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int snp_guest_df_flush(int *error); + +/** + * snp_guest_decommission - perform SNP_DECOMMISSION command + * + * @decommission: sev_data_decommission structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int snp_guest_decommission(struct sev_data_snp_decommission *data, int *error); + +/** + * snp_guest_page_reclaim - perform SNP_PAGE_RECLAIM command + * + * @decommission: sev_snp_page_reclaim structure to be processed + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error); + +/** + * snp_guest_dbg_decrypt - perform SEV SNP_DBG_DECRYPT command + * + * @sev_ret: sev command return code + * + * Returns: + * 0 if the sev successfully processed the command + * -%ENODEV if the sev device is not available + * -%ENOTSUPP if the sev does not support SEV + * -%ETIMEDOUT if the sev command timed out + * -%EIO if the sev returned a non-zero return code + */ +int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error); + + void *psp_copy_user_blob(u64 uaddr, u32 len); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -887,6 +946,21 @@ sev_issue_cmd_external_user(struct file *filep, unsigned int id, void *data, int static inline void *psp_copy_user_blob(u64 __user uaddr, u32 len) { return ERR_PTR(-EINVAL); } +static inline int +snp_guest_decommission(struct sev_data_snp_decommission *data, int *error) { return -ENODEV; } + +static inline int snp_guest_df_flush(int *error) { return -ENODEV; } + +static inline int snp_guest_page_reclaim(struct sev_data_snp_page_reclaim *data, int *error) +{ + return -ENODEV; +} + +static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error) +{ + return -ENODEV; +} + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Wed Jun 2 14:10:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294735 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C4CFC4708F for ; Wed, 2 Jun 2021 14:14:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 16760613EA for ; Wed, 2 Jun 2021 14:14:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232294AbhFBOQX (ORCPT ); Wed, 2 Jun 2021 10:16:23 -0400 Received: from mail-dm3nam07on2058.outbound.protection.outlook.com ([40.107.95.58]:6980 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230031AbhFBOOj (ORCPT ); Wed, 2 Jun 2021 10:14:39 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C9HbEJwFF8DwElwcAq0dM8mQCrIOp0iGFvYm3MlLTgWmlP7wJXduTmzeuct1ogL0vH4C802dG6s05zvjIMSCoD/NYFBxcFsGtKSbTg7+nZvZzr5nkr1QFwbI2ViAyFqZD9Dym2XpIQqrOFY3wEMSo68iVyDMWpj6M+so1ejbTsUJ2jRe5FzAsIWwURPS+Ot6jkiUA8Ek3zDjzlOphAjEe7A4rbvd367ZkJSeuxwbuy2u4dmWvQ48tio1LHfpPDAWHdLfYTER4PcQqm26UNNiBCYafa6+0+I5EbTgcg/2jz6IG4Qnx/imiPurFNtlXj2hnIXhMLDukF/Jf+Xqxb2ByA== 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-SenderADCheck; bh=MOlQ2aCoAvg7b5HxXGUyXDe7t2qSxR8TcFCpfqVN4UI=; b=S6oL2SuU0sB2uMlPZO3g1/6E89O2NXZlzvEqT/2VroH/YFXRxugJ5qiWHVherDBVZtj1pacbMCP69V0uz/VhcgbJSf21x9ZW5nGfw8Bq1usBJW8QwRScgJX3MOBaqbOXdCDPolq/XdCGzRbat1XayZoKdOL1ATIXPG83Zl7He3Aod/Tax4qtKHj9bWeP8BIrkH3PpfwzRKhJCoNQUUjua9HWdyCK3eSoxPi/2c4YWt+qFPCRToda8xTTJcxgNd5PrMUxhki42NnCk37vLJdtMbUCXNvbvpkj12b1vKk2VtU990+nCsrWyf2VOZxan07lqMNQEm1j8TZPO229/xpX7A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=MOlQ2aCoAvg7b5HxXGUyXDe7t2qSxR8TcFCpfqVN4UI=; b=Tca6a5YZQ/zGz9QQNMeJnHB1ZKx0aKN8POWLXfarhldOkNNf7ZYu6vTrqAbaCl7ITTA5b0M1loVOPhDcGrxEgBPOU3QkWNYdi7NxvpLbt/TusKeAD+tcOILrZP25rABYwJUlY23SSrp6iV+wc4UK9dZMc1oWgoAYMA3v9he+11I= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4495.namprd12.prod.outlook.com (2603:10b6:806:70::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:47 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:47 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 15/37] crypto: ccp: Handle the legacy TMR allocation when SNP is enabled Date: Wed, 2 Jun 2021 09:10:35 -0500 Message-Id: <20210602141057.27107-16-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:45 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 151b1f33-4e24-4cfe-77b9-08d925d05b4a X-MS-TrafficTypeDiagnostic: SA0PR12MB4495: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Lx8WJvuuPwyatsYg0v2aLfUvDN+u/Krc90HGMTaLEFbDNrgDlzLPMuaAhd+RFdC8QwGPeS/pB+dIYm8AjADNyDlP3E7X4hj5Oo/YkBmDbTBtGjVxEur6J/kuO4TSne1o6HrTt9zffFylfPG/OB744p4AueecqliE6lODlSea8/o5wugBXXUNX/Mf0vD0M3wzbwqIM2uqF+vEj5FJgMoWG2eWUVysfaC29MtrFF5B6fxHKlXrCtZHo/jnYMsPjIUglQ5ke6MaK3p2Hym0oR2vLemcLk4sh1mwuREHDxfxrolgxGJkffiaKXwCCYYXoR3pd3PZL8swuVeVtAOUgV2UjqYvhs0b4nZHT160SZEh/fFGSzHyU38XuBjdRZcIETKJ9cT6NfhcZFmKfHxMRCMR+rX57HNRfOpPKgtJiwS9fkIdb1uTVtAGGuJDJwFwoNQ71Yoclz79q6ulu6D1kEnJRwTdaE5uy6tMRdtB6H/tSVnnv8TkY97+uT3tTm2vlgQJM4ZZcfrlb+cDtiKjVsqlmliQ2mmgpF9Qj7ml/hIgqm0/MLf/NSVKgMjza5dlzJPLsLQ8lTvvVw3FHhV9yksl48eVJOi5bIGwGo3SB4kPikH0TBkq/XalY5WcLBPvzPdgeUdHtk7etQ6XO2H9oZpnDg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(376002)(396003)(346002)(366004)(39860400002)(1076003)(54906003)(44832011)(8676002)(2616005)(956004)(478600001)(6486002)(316002)(86362001)(4326008)(7416002)(6666004)(38100700002)(7696005)(38350700002)(52116002)(16526019)(26005)(186003)(2906002)(5660300002)(66946007)(8936002)(83380400001)(36756003)(66556008)(66476007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Pxy3rbvJzGkgF2Tv0M0axEYe/6JZbRQSUTQJSsenhsEIzV+KoK/aaD91wF/Bp7xEv2CgO3tjAde2w6J0xAM09tEBLyBf3JUBEjk+SOGBkBl76kWYoHBXgkN8Qz+o2xxQbSfCa1BhmkO/KXCoK1IxvC0xY4QUTvKiN/xgVZqbQTPxY+eMAxYlavn3IHTXkZUAdwC5HMfw7woxGG4k5pBaIjlyz75VF5G23TXXBic9YK09dMkUTe+UOsNYoRjozlhZiHmcOxLpHwXyCH08jmaacO8lcHQOqrMlTnnq//No4ea23e5PYnEy8YG2TmmlHqIoruat6VoOWzx8l0MvOdZQyqb1BO5KP6j+6k0+pUv/ZQagUUJFt8YI652lGMSOQGuO/IMiLL2KRxLRsFiylHbvT1sM1RL4JLuYJ72r6hX6Zz8nkWmWL7HckYWlC2Ri+QoFz/ggbBu4YjZzG8GinzY1BJxaLE2SgDzyjHsX7B5UllvquDTLUoZO0a0Z24sQG0QkGm2sw/gHLtraNTrNCcLJ4D7eTlkz14LBkA7YT/EGNA6HRin+RH4DmD1qC4fMhzX+g6SaztzxC+prwefJpQh+l9p64wbNnjcawl4PqQGuIIjNPFvFnoJMvGyY4ZG9xJeiIoATl5hujiqsUWX+6g7cFoum7yXs8wwKVSK8EysVrBjEvJjWhxc9W4uWNJSK5ksZ5yEGjkm5Lqdglig/KuJHWb1IOfnB9jiQZNeuFek9MXwm3NRhdSsYNHcxCYG40izlC4cNRJYsEbM28e9r+6GDt4do3EtrBu5DkWmCdg8Wt1MdYNO/a/WTNOMO1NtTnXJxHc4Q/Men+KsRPTgCODWrOg8M6HtxSogqKjIs1Hc0vcxJyXcbtGzs5Ch6cR+XfwyMaRJ9Zuz6AJqtbLUmAhRMSR3T0d7ewURtupldIyaRLvcRqlul2N9qUonk6eIVWrE5Hqyn62Bvo+rEHDnUYJ3fXWHi0A8gXEgwfwmp5+/mrPLedClRMryA/sIro9It00q0LDYzSehxVBfnKVe+SrSxftlaEhagHeFwQZp1DfYaDVw72hsRjBuQxE+qH/qRk3Sjve+kfn3lsgT5xsTHo3NTZYKntKKykR+s4+BZMZRWK4OMzTS4B8+Ovs5p1PM9pKtQRPZs/C9Gk0eZ0RbhuX9xyrZ59yLZWVgeT701jgPMNEGQRxqKjhx/rYmWeQRX/midPEY+2NX+7PHtwqHrQbZo0DAnkRiXVzpajR0fnH82/+EepYIX+rNIW1xarYIOYRCUzs7jdw76paG2Kf/SBgwmZuQCEjfzSCjPs3kQv8iJ2EQdtgmFA0gDqB4ud/Rpmxap X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 151b1f33-4e24-4cfe-77b9-08d925d05b4a X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:46.8535 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: o7rOshOIz0W37jhJfeifaYPjIGnOAnXxuZHHdm3sbYX5mOnIki97ut2pEJOph3gHjCuPMYdo0jmTMxqX6tzZGg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4495 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The behavior and requirement for the SEV-legacy command is altered when the SNP firmware is in the INIT state. See SEV-SNP firmware specification for more details. When SNP is INIT state, all the SEV-legacy commands that cause the firmware to write memory must be in the firmware state. The TMR memory is allocated by the host but updated by the firmware, so, it must be in the firmware state. Additionally, the TMR memory must be a 2MB aligned instead of the 1MB, and the TMR length need to be 2MB instead of 1MB. The helper __snp_{alloc,free}_firmware_pages() can be used for allocating and freeing the memory used by the firmware. While at it, provide API that can be used by others to allocate a page that can be used by the firmware. The immediate user for this API will be the KVM driver. The KVM driver to need to allocate a firmware context page during the guest creation. The context page need to be updated by the firmware. See the SEV-SNP specification for further details. Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 142 +++++++++++++++++++++++++++++++---- include/linux/psp-sev.h | 11 +++ 2 files changed, 140 insertions(+), 13 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index def2996111db..cbf77cbf1887 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -54,6 +54,14 @@ static int psp_timeout; #define SEV_ES_TMR_SIZE (1024 * 1024) static void *sev_es_tmr; +/* When SEV-SNP is enabled the TMR need to be 2MB aligned and 2MB size. */ +#define SEV_SNP_ES_TMR_SIZE (2 * 1024 * 1024) + +static size_t sev_es_tmr_size = SEV_ES_TMR_SIZE; + +static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret); +static int sev_do_cmd(int cmd, void *data, int *psp_ret); + static inline bool sev_version_greater_or_equal(u8 maj, u8 min) { struct sev_device *sev = psp_master->sev_data; @@ -151,6 +159,112 @@ static int sev_cmd_buffer_len(int cmd) return 0; } +static int snp_reclaim_page(struct page *page, bool locked) +{ + struct sev_data_snp_page_reclaim data = {}; + int ret, err; + + data.paddr = page_to_pfn(page) << PAGE_SHIFT; + + if (locked) + ret = __sev_do_cmd_locked(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + else + ret = sev_do_cmd(SEV_CMD_SNP_PAGE_RECLAIM, &data, &err); + + return ret; +} + +static int snp_set_rmptable_state(unsigned long paddr, int npages, + struct rmpupdate *val, bool locked, bool need_reclaim) +{ + unsigned long pfn = __sme_clr(paddr) >> PAGE_SHIFT; + unsigned long pfn_end = pfn + npages; + struct psp_device *psp = psp_master; + struct sev_device *sev; + int rc; + + if (!psp || !psp->sev_data) + return 0; + + /* If SEV-SNP is initialized then add the page in RMP table. */ + sev = psp->sev_data; + if (!sev->snp_inited) + return 0; + + while (pfn < pfn_end) { + if (need_reclaim) + if (snp_reclaim_page(pfn_to_page(pfn), locked)) + return -EFAULT; + + rc = rmpupdate(pfn_to_page(pfn), val); + if (rc) + return rc; + + pfn++; + } + + return 0; +} + +static struct page *__snp_alloc_firmware_pages(gfp_t gfp_mask, int order) +{ + struct rmpupdate val = {}; + unsigned long paddr; + struct page *page; + + page = alloc_pages(gfp_mask, order); + if (!page) + return NULL; + + val.assigned = 1; + val.immutable = 1; + paddr = __pa((unsigned long)page_address(page)); + + if (snp_set_rmptable_state(paddr, 1 << order, &val, false, true)) { + pr_warn("Failed to set page state (leaking it)\n"); + return NULL; + } + + return page; +} + +void *snp_alloc_firmware_page(gfp_t gfp_mask) +{ + struct page *page; + + page = __snp_alloc_firmware_pages(gfp_mask, 0); + + return page ? page_address(page) : NULL; +} +EXPORT_SYMBOL_GPL(snp_alloc_firmware_page); + +static void __snp_free_firmware_pages(struct page *page, int order) +{ + struct rmpupdate val = {}; + unsigned long paddr; + + if (!page) + return; + + paddr = __pa((unsigned long)page_address(page)); + + if (snp_set_rmptable_state(paddr, 1 << order, &val, false, true)) { + pr_warn("Failed to set page state (leaking it)\n"); + return; + } + + __free_pages(page, order); +} + +void snp_free_firmware_page(void *addr) +{ + if (!addr) + return; + + __snp_free_firmware_pages(virt_to_page(addr), 0); +} +EXPORT_SYMBOL(snp_free_firmware_page); + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { struct psp_device *psp = psp_master; @@ -273,7 +387,7 @@ static int __sev_platform_init_locked(int *error) data.flags |= SEV_INIT_FLAGS_SEV_ES; data.tmr_address = tmr_pa; - data.tmr_len = SEV_ES_TMR_SIZE; + data.tmr_len = sev_es_tmr_size; } rc = __sev_do_cmd_locked(SEV_CMD_INIT, &data, error); @@ -627,6 +741,8 @@ static int __sev_snp_init_locked(int *error) sev->snp_inited = true; dev_dbg(sev->dev, "SEV-SNP firmware initialized\n"); + sev_es_tmr_size = SEV_SNP_ES_TMR_SIZE; + return rc; } @@ -1150,8 +1266,8 @@ static void sev_firmware_shutdown(struct sev_device *sev) /* The TMR area was encrypted, flush it from the cache */ wbinvd_on_all_cpus(); - free_pages((unsigned long)sev_es_tmr, - get_order(SEV_ES_TMR_SIZE)); + + __snp_free_firmware_pages(virt_to_page(sev_es_tmr), get_order(sev_es_tmr_size)); sev_es_tmr = NULL; } @@ -1201,16 +1317,6 @@ void sev_pci_init(void) sev_update_firmware(sev->dev) == 0) sev_get_api_version(); - /* Obtain the TMR memory area for SEV-ES use */ - tmr_page = alloc_pages(GFP_KERNEL, get_order(SEV_ES_TMR_SIZE)); - if (tmr_page) { - sev_es_tmr = page_address(tmr_page); - } else { - sev_es_tmr = NULL; - dev_warn(sev->dev, - "SEV: TMR allocation failed, SEV-ES support unavailable\n"); - } - /* * If boot CPU supports the SNP, then let first attempt to initialize * the SNP firmware. @@ -1226,6 +1332,16 @@ void sev_pci_init(void) } } + /* Obtain the TMR memory area for SEV-ES use */ + tmr_page = __snp_alloc_firmware_pages(GFP_KERNEL, get_order(sev_es_tmr_size)); + if (tmr_page) { + sev_es_tmr = page_address(tmr_page); + } else { + sev_es_tmr = NULL; + dev_warn(sev->dev, + "SEV: TMR allocation failed, SEV-ES support unavailable\n"); + } + /* Initialize the platform */ rc = sev_platform_init(&error); if (rc && (error == SEV_RET_SECURE_DATA_INVALID)) { diff --git a/include/linux/psp-sev.h b/include/linux/psp-sev.h index 63ef766cbd7a..b72a74f6a4e9 100644 --- a/include/linux/psp-sev.h +++ b/include/linux/psp-sev.h @@ -12,6 +12,8 @@ #ifndef __PSP_SEV_H__ #define __PSP_SEV_H__ +#include + #include #ifdef CONFIG_X86 @@ -920,6 +922,8 @@ int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *error); void *psp_copy_user_blob(u64 uaddr, u32 len); +void *snp_alloc_firmware_page(gfp_t mask); +void snp_free_firmware_page(void *addr); #else /* !CONFIG_CRYPTO_DEV_SP_PSP */ @@ -961,6 +965,13 @@ static inline int snp_guest_dbg_decrypt(struct sev_data_snp_dbg *data, int *erro return -ENODEV; } +static inline void *snp_alloc_firmware_page(gfp_t mask) +{ + return NULL; +} + +static inline void snp_free_firmware_page(void *addr) { } + #endif /* CONFIG_CRYPTO_DEV_SP_PSP */ #endif /* __PSP_SEV_H__ */ From patchwork Wed Jun 2 14:10:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294749 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9F8A7C4708F for ; Wed, 2 Jun 2021 14:15:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 89371613EA for ; Wed, 2 Jun 2021 14:15:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232114AbhFBORe (ORCPT ); Wed, 2 Jun 2021 10:17:34 -0400 Received: from mail-bn8nam11on2088.outbound.protection.outlook.com ([40.107.236.88]:32416 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S229762AbhFBOP3 (ORCPT ); Wed, 2 Jun 2021 10:15:29 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UrM/kOYUDZ0XvALZIodZI8oLynEdwRrVcyC+WtJNj2MIjWlYqVTPNKQTmoNk2U0DXjoWE2tF3mHLZMcXgXiEiTx7SEOIdt/VTul3WIoGLmDXiOUqL7fXYRcmZu08Gykt/QyUYIoZ2+APycH4olDdWjk/XEi9vHli6aUX9NqsxE6fagwTi6Ivyr+EJexB1kcTv7/B5HPSF6Tw4NxhvqipP4lSDOhCK7hV/5MUtaV2uYNLTpgMXKA+9VBngrCJjRvvSgvSwf8Pt16onxn2P29erTEUZt7yuKfdYxVUKPRv4ZMlVkt8j67sk+oe9Th6w2ZtxeAoexu3fu8AGs6TCD/O6Q== 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-SenderADCheck; bh=u4/a7g6CsJ5AEPBd2g0WE1mD86STTW1l6r7T2FpRne4=; b=nFnOlbAnvcOteE6LvKP4fElFsSNZBgacJjsf6BTmcqQwd0gp6rPjd0UXj4bJ2/1fFs2nWAdkTHZTFjM/9MdytRyG8nMX5r/RP5M5/dQrm3t/OTDohK1WJY6ytm1aEyEDcNJmrwyJgwL/UO3SE5AQrrZaptaqpviyUwqpbYjSIf15hMzX6v/21ZnBJpkTqUr/GJ1ntlVuVutV3BJxwaxUW21Hj9KI1PKLehooOFOmca6KZ5EgimsRvqlX7e1imWE/O+vndELF0Qo2d9Q7MOKGisVOk3RiDL5eqUiA3UsrKuoipcQO9hBECL1085Ha9+/94rIIwQGAwp69E8ukZoglfg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=u4/a7g6CsJ5AEPBd2g0WE1mD86STTW1l6r7T2FpRne4=; b=Ch4lArar0/NGFQOu+YiaYwe6HEtl7YQvj4uP/0Dfy5PaqP2sQDgdLlZdQHxDjyymtg6mlsAdrdCTI137KH4BndJmrDeru6GATNJg97jIecsm7+wLLgPoXu//BzobpINTV9bLUTLGzgTTlDBzcC5fkYvFCVaXkkPK/a7tNtIUxRo= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:48 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:48 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 16/37] crypto: ccp: Handle the legacy SEV command when SNP is enabled Date: Wed, 2 Jun 2021 09:10:36 -0500 Message-Id: <20210602141057.27107-17-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:47 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e5c2487b-3e8a-4e6c-dedf-08d925d05c4d X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:1468; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: hjiH7v6f9ql0TVy3l/BWVdOm8+6uwmAUwZIOea1BS+toqHagxEAezciLSGthiJCPRENhqls/UIvjaZfr0UPXeN6/gldAxR6/56bIgwV7dvsbsT+P84uAdvLe6sCUJC5Wr6lxGIOqun3tfHm+CKsx/mgC0f+mcXuXNxwZF4CG+xehc/JfzjkFl3a+GlTuxdOsOKCGueGgdWpUU+2w5W5tiLSJbtU+VmZHhzurC6kSTeItfO82M50BCSoLRfcHHxuM9xFJL2WODSAd//c3ZEHKvTCsvl6PASmBS30VucIg8ryFQNbcA2y+LD1ASUFV5j3zrg5bVnM1Tvjtrl/16lusF4HNNpnW/b/Qyl2++M8VWmla1iWlz6HOUxLYNoOVLl1BcZ0KVN6CmCdqdVLAyyQJWiZT+KbkmRhMi6wG7imMhi9ZGxopfV+i2Nwt+O/e8Hoz2MJ3lELEVU1CMFrEZzQVniWzaI9NtBWuygPXK1oDCcwGY+MkEX4SoGHKpwLq/xSTlmoXaMN2LEi+DIK4ug/Q0Ku2+hEBHzk5TZZPYoh8dnd+Vhx6lEALIsIx8qZ80UGEVZYKdHlaLi8BBWpH03nOdOaPC9AlAPERZ1/W8LQGCStx03oVFVPuHHfopvOUUxjKRJa89o6FQB8HgHalW2Ioog== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(30864003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: TJcHCSA3gV+d9wG5XeeQKYtx2bjDw6/9dt8c+IfzH5ALYAqBTyjG5sSXpOg6o91B6fYIDJbRhhCPurJABSQSkq1Gl83vkEsQS7djRcwldXg/a7+VNRc9Pix/ucNmrfxQGXocOQpEiomZw347Vyo2D3krKKlOB/2VMd8XxA/zEQ60VWAfQOFdxqa5ifDh2DTqXdq8qI+yDehChsI6iDLFpbZ+Kh3DMacMNPB15MA0zh16cd/fxBBSAq6Bg/9FmforIzOZvc1Ls/84fFmKhPd/G86vGkxHUmztdnWmp0Cun1z1dmvuM4kWqy43aQC+WmYqX21T8plGwFZjqgQ95ZDRICRmq9mesCyWDT1v9d9+JbkfCD1o75Sd8QS0wAqybx1dCDG4WRUmu9G0+Qqk94ObdDSIy+iJ+ggKAMk0BH6pYtaTEh7wt7wVhrpviOhgAs7XHzJuIuKEjCOdhezcijthQH55QHjkItjwiURpv4VidD1H3uYPo3JODgEUsYm+qPHezeHPmQqhb7z+DqUlsMRg+f2Zl3sDLC4YAaL/UDO9OUzZTYQJ3DdfXPdtvx2mkrZL+qMx2cOFbTDVezXMsRJtvQAZLhAWbgFTZhCBb9JbwV7nAttGMwkpwBxX+5GcaqZHeXg4ZHBLnvPMMVI/5Dlc1U+ftKWTc/q9gR6EPmUCwTixAOK0bH4MIvoPFR09rBV27Y0s3Js6lZ2vwqKCDJunLpVfcWUSOLy2uHTHwxn/R5d4E/yGSBWGXe6COdCSGQO8+TIUs6NhbvnH9HSGltxwWEfLXm34bgfUS/v3UNNzaMm/p6rZlazgbyiSWEmVRqTuvEnakg+atWrdnzoyhi3ctTBG4MOW6xUjwVBAEzVYbMiSzWoqBs6z0CbSWrUXKUtfcBgSx3yTAzZGGMywpXGJ9cdXp/rvfeqy+FQS85OhuMTlGXcsUdC8wC5p1Onn0i8joMne975BlAlfvm14q02sORPvrG/N0IoyKAs0ZANCLLBVMDhIUpJgMZsKzfaNuHVFQvAyUVGPUCESTw9W5ao6dZp/bApAE8fDd12Mv8hrpFqp92YNU7AfTDC0Km68embKhy4swCvYpmh/JUHBP8/vBl2AFFP7NP44GchOg4Il94WLPClFD4fHROLMKTQ2mZUGlQYu3bQPcmOKnUiBphJjp/DVD6PnvJULrBnfnyB23RevebERMwmwEFDDWM5VMm94sD9bvlK2XeeuDN0YDQTDTK5fvVcokmuneym54NKy2rycm6Xc3pcAPd7EPoIKLa+bUoNROspsHW/86mBtJbzaV8st34YnU6Iv1kSw0Yw7Rir5tKUAZ937c2m4DBuaPhRl X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e5c2487b-3e8a-4e6c-dedf-08d925d05c4d X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:48.3547 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /ZgAVMh1VDDh4v5PNy1J19e8rHj3U/LqNBUk421DmDI9WY0IURVhQBiiq3c9B1ZCEmgIw9pH4KvT44FCCEyz5g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The behavior of the SEV-legacy commands is altered when the SNP firmware is in the INIT state. When SNP is in INIT state, all the SEV-legacy commands that cause the firmware to write to memory must be in the firmware state before issuing the command.. A command buffer may contains a system physical address that the firmware may write to. There are two cases that need to be handled: 1) system physical address points to a guest memory 2) system physical address points to a host memory To handle the case #1, map_firmware_writeable() helper simply changes the page state in the RMP table before and after the command is sent to the firmware. For the case #2, the map_firmware_writeable() replaces the host system physical memory with a pre-allocated firmware page, and after the command completes, the unmap_firmware_writeable() copies the content from pre-allocated firmware page to original host system physical. The unmap_firmware_writeable() calls a __sev_do_cmd_locked() to clear the immutable bit from the memory page. To support the nested calling, a separate command buffer is required. Allocate a backup command buffer and keep reference count of it. If a nested call is detected then use the backup cmd_buf to complete the command submission. Signed-off-by: Brijesh Singh --- drivers/crypto/ccp/sev-dev.c | 348 ++++++++++++++++++++++++++++++++++- drivers/crypto/ccp/sev-dev.h | 12 ++ 2 files changed, 350 insertions(+), 10 deletions(-) diff --git a/drivers/crypto/ccp/sev-dev.c b/drivers/crypto/ccp/sev-dev.c index cbf77cbf1887..c886d76ae31d 100644 --- a/drivers/crypto/ccp/sev-dev.c +++ b/drivers/crypto/ccp/sev-dev.c @@ -265,12 +265,299 @@ void snp_free_firmware_page(void *addr) } EXPORT_SYMBOL(snp_free_firmware_page); +static int alloc_snp_host_map(struct sev_device *sev) +{ + struct page *page; + int i; + + for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) { + struct snp_host_map *map = &sev->snp_host_map[i]; + + memset(map, 0, sizeof(*map)); + + page = __snp_alloc_firmware_pages(GFP_KERNEL_ACCOUNT, + get_order(SEV_FW_BLOB_MAX_SIZE)); + if (!page) + return -ENOMEM; + + map->host = page_address(page); + } + + return 0; +} + +static void free_snp_host_map(struct sev_device *sev) +{ + int i; + + for (i = 0; i < MAX_SNP_HOST_MAP_BUFS; i++) { + struct snp_host_map *map = &sev->snp_host_map[i]; + + if (map->host) { + __snp_free_firmware_pages(virt_to_page(map->host), + get_order(SEV_FW_BLOB_MAX_SIZE)); + memset(map, 0, sizeof(*map)); + } + } +} + +static int map_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map) +{ + unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT; + int ret; + + map->active = false; + + if (!paddr || !len) + return 0; + + map->paddr = *paddr; + map->len = len; + + /* If paddr points to a guest memory then change the page state to firmwware. */ + if (guest) { + struct rmpupdate val = {}; + + val.immutable = true; + val.assigned = true; + ret = snp_set_rmptable_state(*paddr, npages, &val, true, false); + if (ret) + return ret; + + goto done; + } + + if (unlikely(!map->host)) + return -EINVAL; + + /* Check if the pre-allocated buffer can be used to fullfil the request. */ + if (unlikely(len > SEV_FW_BLOB_MAX_SIZE)) + return -EINVAL; + + /* Set the paddr to use an intermediate firmware buffer */ + *paddr = __psp_pa(map->host); + +done: + map->active = true; + return 0; +} + +static int unmap_firmware_writeable(u64 *paddr, u32 len, bool guest, struct snp_host_map *map) +{ + unsigned int npages = PAGE_ALIGN(len) >> PAGE_SHIFT; + int ret; + + if (!map->active) + return 0; + + /* If paddr points to a guest memory then restore the page state to hypervisor. */ + if (guest) { + struct rmpupdate val = {}; + + ret = snp_set_rmptable_state(*paddr, npages, &val, true, true); + if (ret) + return ret; + + goto done; + } + + /* Copy the response data firmware buffer to the callers buffer. */ + memcpy(__va(__sme_clr(map->paddr)), map->host, min_t(size_t, len, map->len)); + *paddr = map->paddr; + +done: + map->active = false; + return 0; +} + +static bool sev_legacy_cmd_buf_writable(int cmd) +{ + switch (cmd) { + case SEV_CMD_PLATFORM_STATUS: + case SEV_CMD_GUEST_STATUS: + case SEV_CMD_LAUNCH_START: + case SEV_CMD_RECEIVE_START: + case SEV_CMD_LAUNCH_MEASURE: + case SEV_CMD_SEND_START: + case SEV_CMD_SEND_UPDATE_DATA: + case SEV_CMD_SEND_UPDATE_VMSA: + case SEV_CMD_PEK_CSR: + case SEV_CMD_PDH_CERT_EXPORT: + case SEV_CMD_GET_ID: + case SEV_CMD_ATTESTATION_REPORT: + return true; + default: + return false; + } +} + +#define prep_buffer(name, addr, len, guest, map) \ + func(&((typeof(name *))cmd_buf)->addr, ((typeof(name *))cmd_buf)->len, guest, map) + +static int __snp_cmd_buf_copy(int cmd, void *cmd_buf, bool to_fw, int fw_err) +{ + int (*func)(u64 *paddr, u32 len, bool guest, struct snp_host_map *map); + struct sev_device *sev = psp_master->sev_data; + struct rmpupdate val = {}; + bool from_fw = !to_fw; + int ret; + + /* + * After the command is completed, change the command buffer memory to + * hypervisor state. + * + * The immutable bit is automatically cleared by the firmware, so + * no not need to reclaim the page. + */ + if (from_fw && sev_legacy_cmd_buf_writable(cmd)) { + ret = snp_set_rmptable_state(__pa(cmd_buf), 1, &val, true, false); + if (ret) + return ret; + + /* No need to go further if firmware failed to execute command. */ + if (fw_err) + return 0; + } + + if (to_fw) + func = map_firmware_writeable; + else + func = unmap_firmware_writeable; + + /* + * A command buffer may contains a system physical address. If the address + * points to a host memory then use an intermediate firmware page otherwise + * change the page state in the RMP table. + */ + switch (cmd) { + case SEV_CMD_PDH_CERT_EXPORT: + if (prep_buffer(struct sev_data_pdh_cert_export, pdh_cert_address, + pdh_cert_len, false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_pdh_cert_export, cert_chain_address, + cert_chain_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_GET_ID: + if (prep_buffer(struct sev_data_get_id, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_PEK_CSR: + if (prep_buffer(struct sev_data_pek_csr, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_DATA: + if (prep_buffer(struct sev_data_launch_update_data, address, len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_VMSA: + if (prep_buffer(struct sev_data_launch_update_vmsa, address, len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_MEASURE: + if (prep_buffer(struct sev_data_launch_measure, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_LAUNCH_UPDATE_SECRET: + if (prep_buffer(struct sev_data_launch_secret, guest_address, guest_len, + true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_DBG_DECRYPT: + if (prep_buffer(struct sev_data_dbg, dst_addr, len, false, + &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_DBG_ENCRYPT: + if (prep_buffer(struct sev_data_dbg, dst_addr, len, true, + &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_ATTESTATION_REPORT: + if (prep_buffer(struct sev_data_attestation_report, address, len, + false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_SEND_START: + if (prep_buffer(struct sev_data_send_start, session_address, + session_len, false, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_SEND_UPDATE_DATA: + if (prep_buffer(struct sev_data_send_update_data, hdr_address, hdr_len, + false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_send_update_data, trans_address, + trans_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_SEND_UPDATE_VMSA: + if (prep_buffer(struct sev_data_send_update_vmsa, hdr_address, hdr_len, + false, &sev->snp_host_map[0])) + goto err; + if (prep_buffer(struct sev_data_send_update_vmsa, trans_address, + trans_len, false, &sev->snp_host_map[1])) + goto err; + break; + case SEV_CMD_RECEIVE_UPDATE_DATA: + if (prep_buffer(struct sev_data_receive_update_data, guest_address, + guest_len, true, &sev->snp_host_map[0])) + goto err; + break; + case SEV_CMD_RECEIVE_UPDATE_VMSA: + if (prep_buffer(struct sev_data_receive_update_vmsa, guest_address, + guest_len, true, &sev->snp_host_map[0])) + goto err; + break; + default: + break; + } + + /* The command buffer need to be in the firmware state. */ + if (to_fw && sev_legacy_cmd_buf_writable(cmd)) { + val.assigned = true; + val.immutable = true; + ret = snp_set_rmptable_state(__pa(cmd_buf), 1, &val, true, false); + if (ret) + return ret; + } + + return 0; + +err: + return -EINVAL; +} + +static inline bool need_firmware_copy(int cmd) +{ + struct sev_device *sev = psp_master->sev_data; + + /* After SNP is INIT'ed, the behavior of legacy SEV command is changed. */ + return ((cmd < SEV_CMD_SNP_INIT) && sev->snp_inited) ? true : false; +} + +static int snp_aware_copy_to_firmware(int cmd, void *data) +{ + return __snp_cmd_buf_copy(cmd, data, true, 0); +} + +static int snp_aware_copy_from_firmware(int cmd, void *data, int fw_err) +{ + return __snp_cmd_buf_copy(cmd, data, false, fw_err); +} + static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) { struct psp_device *psp = psp_master; struct sev_device *sev; unsigned int phys_lsb, phys_msb; unsigned int reg, ret = 0; + void *cmd_buf; int buf_len; if (!psp || !psp->sev_data) @@ -290,12 +577,26 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) * work for some memory, e.g. vmalloc'd addresses, and @data may not be * physically contiguous. */ - if (data) - memcpy(sev->cmd_buf, data, buf_len); + if (data) { + if (unlikely(sev->cmd_buf_active > 2)) + return -EBUSY; + + cmd_buf = sev->cmd_buf_active ? sev->cmd_buf_backup : sev->cmd_buf; + + memcpy(cmd_buf, data, buf_len); + sev->cmd_buf_active++; + + /* + * The behavior of the SEV-legacy commands is altered when the + * SNP firmware is in the INIT state. + */ + if (need_firmware_copy(cmd) && snp_aware_copy_to_firmware(cmd, sev->cmd_buf)) + return -EFAULT; + } /* Get the physical address of the command buffer */ - phys_lsb = data ? lower_32_bits(__psp_pa(sev->cmd_buf)) : 0; - phys_msb = data ? upper_32_bits(__psp_pa(sev->cmd_buf)) : 0; + phys_lsb = data ? lower_32_bits(__psp_pa(cmd_buf)) : 0; + phys_msb = data ? upper_32_bits(__psp_pa(cmd_buf)) : 0; dev_dbg(sev->dev, "sev command id %#x buffer 0x%08x%08x timeout %us\n", cmd, phys_msb, phys_lsb, psp_timeout); @@ -336,15 +637,24 @@ static int __sev_do_cmd_locked(int cmd, void *data, int *psp_ret) ret = -EIO; } - print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, - buf_len, false); - /* * Copy potential output from the PSP back to data. Do this even on * failure in case the caller wants to glean something from the error. */ - if (data) - memcpy(data, sev->cmd_buf, buf_len); + if (data) { + /* + * Restore the page state after the command completes. + */ + if (need_firmware_copy(cmd) && + snp_aware_copy_from_firmware(cmd, cmd_buf, ret)) + return -EFAULT; + + memcpy(data, cmd_buf, buf_len); + sev->cmd_buf_active--; + } + + print_hex_dump_debug("(out): ", DUMP_PREFIX_OFFSET, 16, 2, data, + buf_len, false); return ret; } @@ -1216,10 +1526,12 @@ int sev_dev_init(struct psp_device *psp) if (!sev) goto e_err; - sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 0); + sev->cmd_buf = (void *)devm_get_free_pages(dev, GFP_KERNEL, 1); if (!sev->cmd_buf) goto e_sev; + sev->cmd_buf_backup = (uint8_t *)sev->cmd_buf + PAGE_SIZE; + psp->sev_data = sev; sev->dev = dev; @@ -1271,6 +1583,12 @@ static void sev_firmware_shutdown(struct sev_device *sev) sev_es_tmr = NULL; } + /* + * The host map need to clear the immutable bit so it must be free'd before the + * SNP firmware shutdown. + */ + free_snp_host_map(sev); + sev_snp_shutdown(NULL); } @@ -1330,6 +1648,14 @@ void sev_pci_init(void) */ dev_err(sev->dev, "SEV-SNP: failed to INIT error %#x\n", error); } + + /* + * Allocate the intermediate buffers used for the legacy command handling. + */ + if (alloc_snp_host_map(sev)) { + dev_notice(sev->dev, "Failed to alloc host map (disabling legacy SEV)\n"); + goto skip_legacy; + } } /* Obtain the TMR memory area for SEV-ES use */ @@ -1359,12 +1685,14 @@ void sev_pci_init(void) if (rc) dev_err(sev->dev, "SEV: failed to INIT error %#x\n", error); +skip_legacy: dev_info(sev->dev, "SEV%s API:%d.%d build:%d\n", sev->snp_inited ? "-SNP" : "", sev->api_major, sev->api_minor, sev->build); return; err: + free_snp_host_map(sev); psp_master->sev_data = NULL; } diff --git a/drivers/crypto/ccp/sev-dev.h b/drivers/crypto/ccp/sev-dev.h index 186ad20cbd24..fe5d7a3ebace 100644 --- a/drivers/crypto/ccp/sev-dev.h +++ b/drivers/crypto/ccp/sev-dev.h @@ -29,11 +29,20 @@ #define SEV_CMDRESP_CMD_SHIFT 16 #define SEV_CMDRESP_IOC BIT(0) +#define MAX_SNP_HOST_MAP_BUFS 2 + struct sev_misc_dev { struct kref refcount; struct miscdevice misc; }; +struct snp_host_map { + u64 paddr; + u32 len; + void *host; + bool active; +}; + struct sev_device { struct device *dev; struct psp_device *psp; @@ -52,8 +61,11 @@ struct sev_device { u8 build; void *cmd_buf; + void *cmd_buf_backup; + int cmd_buf_active; bool snp_inited; + struct snp_host_map snp_host_map[MAX_SNP_HOST_MAP_BUFS]; }; int sev_dev_init(struct psp_device *psp); From patchwork Wed Jun 2 14:10:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294753 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0781BC47083 for ; Wed, 2 Jun 2021 14:16:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DB19D613D8 for ; Wed, 2 Jun 2021 14:16:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230406AbhFBORx (ORCPT ); Wed, 2 Jun 2021 10:17:53 -0400 Received: from mail-bn8nam11on2066.outbound.protection.outlook.com ([40.107.236.66]:15585 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231713AbhFBOPv (ORCPT ); Wed, 2 Jun 2021 10:15:51 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kq2cpsQolvTBKrwRP0PhrwYCfMFwJIjRNao0OrODSJZ4MIHk3l6QiNTpymoyaWPk9b8C/xw0RUXcbWlxzOLo14qjQE2KD6syN9x1z/X/5UeRWcthYXFfRCB1YOaQUkpsD9lvBUTicY8uU/OgU7hmi+xsX7zRQreRFz17ONeXoHg3Eo/xTf1D2c34erfPtcGwduC5UrLRq8jJqHs16+mxovzbvPQyDwoPgXh7V1qqEIWIKFiq+R/l9nyY5979XyxrJwtz9tzV3YBv4hTAM+st0Ycsj9GPGTCrj55GF6tfX3OHNAgxF20UMzQ92LYDXIfxoxjX4Y4r7C4eNLn70BSf5A== 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-SenderADCheck; bh=ZXWvmF41owDiMt6To+AsmIAEf1i35/5Hhllt7y5Df9I=; b=l4TzTCLL8KqLrHnWe/IGiwihbES5WD5+qJWkmoYg6zvE2st7J4uTvVS7nHM/PmD6kSlJusjJ8ROq3InFWebtDBn+czeatKtQS8Xk8Oy0bp/4cdeYFbtyptQwC2o1yYTeyfbuqApwoT+/8+TQz6tCVfCKxgChIgvLItotopaKR1VkoJiv5zo4YgSnlSSmD77Ch52Ct/Hhoxqr5FqajGojpyFY4NfnihUZJ/gOrqKm+lwJw9cmvwtjuYWWmyUS+iY91r9JlHgnUgiHsuTuM8jYuPxuSCwHghYDxGf9vdtJ3pUKE2DEXwZz+hBzftxwCTABWaGIj22XUEJWQx+WbrA0EA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=ZXWvmF41owDiMt6To+AsmIAEf1i35/5Hhllt7y5Df9I=; b=KXF2d5E0S+ilm5h2Vi7U6kNMVm2zrwq8m+835/2Rv+AvxORCPz7YGsQQR5isukbL68+pdlQAkPDBSiXzrjvP3XbwkxYtHUvNO8KOaRc+6NJqajITlIa1a7WrI589lVubb2fq+h4klQtv2UUb8647R43Jk2J3FlvZGQBvu3dvDlE= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:50 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:49 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 17/37] KVM: SVM: make AVIC backing, VMSA and VMCB memory allocation SNP safe Date: Wed, 2 Jun 2021 09:10:37 -0500 Message-Id: <20210602141057.27107-18-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:48 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0feb9bc1-80db-456f-b5fe-08d925d05d1f X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 7DITOTE3nnQdwdszN0TNWkm6M6g0AUbJ4T6K5oU9wWzlPBwf+uMrNy2Wr6Rg7ujr58uR9b0YK8cNQLchxjQYi7LBE99pz7kOnzGJ1hJj/MxFJBJrWoPP9uUa+6RQ15y6Qz35kY4VzBJMpo+WMyPrpGG8fbqFpr+zXQpHvJhxXfY1UXcsRnxWw7/BwySy9M2Cka0H/X5kUSua1S4uTiOU+qDJDvz2f+lWCxa8hYv7vDhZVt8lG+TcnGy3wle8M1ybPHpbsYxtoiMX62rM1cggzmf9pwzMCCyTOv3k5ja3T7UEaG+P/NsRBZqRJx+vUOA/C7T9LRnPeIYx0YX94jh8WxLvZCkR2oVipFB6+8UnLkCgYabTXE145evR52K/otJThOjYBXyxru/li1agU1dzbPP3kcQE5jovVEDvYRT6o27xWbhNmT8xYLuubSWO/ILwpthCRArGd8gfopFCKJDx0VBxYVCx2e3yTtu0snSqjlc/v1lJcy/DRo4KLr2FBwjwOiLRknmLDqSL+HwqTMyxCJ94g3nAaDawEgBZipzFZezbazp7luF4DlNSLa9JdewkySE0CwbUfZK2ydu7B9VjYbaeU08ermlVDnDn1C1QcNhaz7fOAMxDSzJIGK+PYO0w1IX4Qt0vB7jPvG90WVYgKQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 0RMpvbeFhrIseM8VC391Z/WRdeZkeNWxvQIZhI36Dr4TK6QuV4NbUn5NLgpD805UqBoQLtMOIE9A7/uFOZL/rUyEVmt6/XFfpIOKUFvJYo6p1ZUiX9BOMeZzLSD3u/QAfJQZV3nJ9nospUUzKQGy0/lZ7Jgf9G81zZYbyiKIY5rdNxbduu6xHArCB/PXz33gQObeI/IxIGdkKmsn8U21IUUKZFlV0APbgD2s+o6LJHRMix0ZbLsPIO8EZhRUba4LD8xeDsr8xdt5H1njpMcDD49I11naV2uywDmv3mBV0UAnUcMB5aePIM8rNRJ6PAMmmF2LpAyw1zep4eee1AZbCZvtEcgTQVtZdrYKMpyxe0ZIrMJVdpqDT5UmsET3H+4B4tG0JVCPmi28UGfGGurZ+CpH4L0mfXFao84aw4zTFi1U1AM3fo/xCoPluQ5fU0/2JWbTRV89s0hJ3j1S+WCJBYAVcGngQRiRTbu0T0jiLNNcqdrbo6WRhMgdzzHjdDa6F4n25/D5sU72E5TfMURMPR5M+UvYcKsxFJ9Fy9uWJlx7N+aZT54sFB/vkYbi49jWkC2j0NfVWdgz7QppM3weM3M3Op/lBIdaT70rrbJvpOKoy4yoy6ARS2WuvRfJXOWtyDid+6UBiDy+Vv4xxKBCjfM7tPbT5/OSrjEMFreR067OUrpIRJCI0G2QssMXyddLM5aMUEbTi/fcMqoLtRsHxBJV/Rfa8+HuKUQ615RAJQoXN3KZpfIHz7pdwxmu/YV0ke5bRYmPeKgDCL1VSjEFUQr262cYlcKhwYyNn0B5qPMXxWij7JM0Iv9jlJltKpe3lXY6rTGZri2bc//hqkY/J3ksDVR9Aqcn8EWN/KFe3mjf0+spsuEiXuD32rHRd2128EdqS/+y3Yso5FyfdBWTmhK/8x7w4aC1GWN5W6CsOM2qnFc6rttx5JHmoW8UY90ebME8G9/Tf60FWouzpToZ4Hq1w4lNF94mY7UEGly6rPKX0qRfjkN5IdagH/H+xR7Nr1A7rE1ALSOZlhiR+5xopx72TIe23OqgQR1L80Yqcho9F+YmWQXYIGHgTJmCBY4Kdfz9xfmgF2ZYZ03EsgHXv3INDIMNH2sr1Et7Z+1JqNiZsWMSZHN4DmtTMQYyEohoH50lKj708h5O/jtLHOmplCXPuhliFCyu0z+HW1BCxuQ/pSuU+qri9/P/iGOBwFNF8DHiyVbAQ/uor5RnLNTFB4zyGC9pqfBH+K7caFpADNnyVMirMoCs9zLdvjSYumDYgngsMdBxVxk32sL646gbmvPJ7fVtfrzDP5PHg7AabYwQgo35+/v1n6oDLubSiTth X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0feb9bc1-80db-456f-b5fe-08d925d05d1f X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:49.7499 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: suaUDRvA7At25DUzPln6YYamWfk6dVpesAALGE0cn7k/t5D8erwWrybZqio0K339d7i0Sqv1SDkLwkw3Jjxxkg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is globally enabled on a system, the VMRUN instruction performs additional security checks on AVIC backing, VMSA, and VMCB page. On a successful VMRUN, these pages are marked "in-use" by the hardware in the RMP entry, and any attempt to modify the RMP entry for these pages will result in page-fault (RMP violation check). While performing the RMP check, hardware will try to create a 2MB TLB entry for the large page accesses. When it does this, it first reads the RMP for the base of 2MB region and verifies that all this memory is safe. If AVIC backing, VMSA, and VMCB memory happen to be the base of 2MB region, then RMP check will fail because of the "in-use" marking for the base entry of this 2MB region. e.g. 1. A VMCB was allocated on 2MB-aligned address. 2. The VMRUN instruction marks this RMP entry as "in-use". 3. Another process allocated some other page of memory that happened to be within the same 2MB region. 4. That process tried to write its page using physmap. If the physmap entry in step #4 uses a large (1G/2M) page, then the hardware will attempt to create a 2M TLB entry. The hardware will find that the "in-use" bit is set in the RMP entry (because it was a VMCB page) and will cause an RMP violation check. See APM2 section 15.36.12 for more information on VMRUN checks when SEV-SNP is globally active. A generic allocator can return a page which are 2M aligned and will not be safe to be used when SEV-SNP is globally enabled. Add a snp_safe_alloc_page() helper that can be used for allocating the SNP safe memory. The helper allocated 2 pages and splits them into order-1 allocation. It frees one page and keeps one of the page which is not 2M aligned. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/lapic.c | 5 ++++- arch/x86/kvm/svm/sev.c | 27 +++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 16 ++++++++++++++-- arch/x86/kvm/svm/svm.h | 1 + 5 files changed, 47 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 55efbacfc244..188110ab2c02 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1383,6 +1383,7 @@ struct kvm_x86_ops { int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err); void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); + void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c index c0ebef560bd1..d4c77f66d7d5 100644 --- a/arch/x86/kvm/lapic.c +++ b/arch/x86/kvm/lapic.c @@ -2441,7 +2441,10 @@ int kvm_create_lapic(struct kvm_vcpu *vcpu, int timer_advance_ns) vcpu->arch.apic = apic; - apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); + if (kvm_x86_ops.alloc_apic_backing_page) + apic->regs = kvm_x86_ops.alloc_apic_backing_page(vcpu); + else + apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT); if (!apic->regs) { printk(KERN_ERR "malloc apic regs error for vcpu %x\n", vcpu->vcpu_id); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index b8505710c36b..411ed72f63af 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2692,3 +2692,30 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) break; } } + +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) +{ + unsigned long pfn; + struct page *p; + + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + return alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + + p = alloc_pages(GFP_KERNEL_ACCOUNT | __GFP_ZERO, 1); + if (!p) + return NULL; + + /* split the page order */ + split_page(p, 1); + + /* Find a non-2M aligned page */ + pfn = page_to_pfn(p); + if (IS_ALIGNED(__pfn_to_phys(pfn), PMD_SIZE)) { + pfn++; + __free_page(p); + } else { + __free_page(pfn_to_page(pfn + 1)); + } + + return pfn_to_page(pfn); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2acf187a3100..a7adf6ca1713 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1336,7 +1336,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm = to_svm(vcpu); err = -ENOMEM; - vmcb01_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmcb01_page = snp_safe_alloc_page(vcpu); if (!vmcb01_page) goto out; @@ -1345,7 +1345,7 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) * SEV-ES guests require a separate VMSA page used to contain * the encrypted register state of the guest. */ - vmsa_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO); + vmsa_page = snp_safe_alloc_page(vcpu); if (!vmsa_page) goto error_free_vmcb_page; @@ -4439,6 +4439,16 @@ static int svm_vm_init(struct kvm *kvm) return 0; } +static void *svm_alloc_apic_backing_page(struct kvm_vcpu *vcpu) +{ + struct page *page = snp_safe_alloc_page(vcpu); + + if (!page) + return NULL; + + return page_address(page); +} + static struct kvm_x86_ops svm_x86_ops __initdata = { .hardware_unsetup = svm_hardware_teardown, .hardware_enable = svm_hardware_enable, @@ -4564,6 +4574,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .complete_emulated_msr = svm_complete_emulated_msr, .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, + + .alloc_apic_backing_page = svm_alloc_apic_backing_page, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 5f874168551b..1175edb02d33 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -554,6 +554,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu); void sev_es_unmap_ghcb(struct vcpu_svm *svm); +struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); /* vmenter.S */ From patchwork Wed Jun 2 14:10:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294761 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 95192C47083 for ; Wed, 2 Jun 2021 14:16:40 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7AAD8610A1 for ; Wed, 2 Jun 2021 14:16:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231846AbhFBOSW (ORCPT ); Wed, 2 Jun 2021 10:18:22 -0400 Received: from mail-bn8nam11on2064.outbound.protection.outlook.com ([40.107.236.64]:63776 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232290AbhFBOQX (ORCPT ); Wed, 2 Jun 2021 10:16:23 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EzzSOEFYcdAry6Xrlte+LX5ydxpxCGZyADobAVhcuBROMbd4rbEs8zRbDkSN/SZ6Wjy8rOa+LPuUimc4vejwa40D+7wKkyxXkSSswJyJlZxtDftyPaNfCpGZKJhOJpJ4H5zEWKYg8IDNirEdjbLBsrSqP3Zf9TmLiwYFtZJ+QJsC6+0xmZOxP9YLp05dLhZAca/RCvXPDqWY1uaKJiVuQl/IRi1gPNLFKNraoYDDg1OSkrvg8lltt8ocUYQ4Lksnk+ILPWQhH6INnub9+vDyZCkrDmVIlqcZp4S6HIepxn/Gwy0OJljnwVl34iTYgiA60WDcHEBIHRqECnmwBonYwg== 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-SenderADCheck; bh=MvMlsfQpaPzh8sw3rRnK68ziGc8ZpmAC5O4Vs8R5wkI=; b=A7a/sKg4wdxYjeamHDfNsImli97s24KL2Z2M8rgUz6K7CEZ2RHvMB3HPZq68qfF77a0fZWLnQ35Of2tNn71Z8qdfweQUHEpz2Gu4Qhcxkun1byfmsnb2EJdro3GS/Ml6KQbPdZ2bJ1AgOZ50/JQ9x6DZKyr6pOI7dJhklsPr3OZ7gX29RkUewAAIqyH2VLGZ+p75uMv3E/idNyu/bzP2XxXoDDZGnlne9RUyVhbHdKW7h1E+FsXU2V2LTvSkPDzlZblILtrKrgLVnIzVHMoS/0YgnIG0MNxORP25CINpsHcOirB861blxGpJxAsAPFXCH2RuUlmtjHnOyW/OlxxrnA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=MvMlsfQpaPzh8sw3rRnK68ziGc8ZpmAC5O4Vs8R5wkI=; b=xE/oXC7qX3/4QG1Mo/zr0jnxXrwLkdP2Xt0fCfyPt70Q+RFYml8wf16UIk4EaCKOhBOjxzZ2S+fZg1G8sJnLJaOlSjANVBTe0HfR/Da06hTYe8+CVN2iOHdhj/vUU0goD+z6aZDVjoNAWOTGcCKsP2eyooRzYbKowIVJMPGh2Cc= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:51 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:51 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 18/37] KVM: SVM: Add initial SEV-SNP support Date: Wed, 2 Jun 2021 09:10:38 -0500 Message-Id: <20210602141057.27107-19-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:50 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 88c61faf-c630-4e37-97fd-08d925d05dd2 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2733; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: BEoGdOUP8U7wdTG6CBHx0qxDsbziAIVtFXWYclc5rdDJXB/GeL5cs1vOHEcNiKxHvs5jZTawhrQ+fHeh9ABKwAtuXQsNoxMor0aP6sperAXjrA9Hq5uwXtjI06PMoQh0A3+VyazbsjrxJlR7N2Om1pxHNX4bXw6xjU03lUlqZYmWPvzlhQxgxpI33WDuabdHR5FDTy+7Y8AOP4C+tlPFtkwGW78FXsP0R+kl2R67DnORIt5Ph4PtFM/Q41nrkl8wupOa5NOJFEm7S5uD2JMp3UQCwxdt8h36/lh66LU+uGlZn3oEPMRe9Go1vjAFjpwGTlSX5BFpRh+gXjyGxveAW3j33O08zeXmmlyUL6GLQylA/S2J/0BXtYoYXzK6y+BifgQ9r5iE84sOdtHsHDvui7qCvdE2URW9gSX7qBRRgWZW3ZMukQsKSQkV9YOs7sl6Q5zmaektVuIaPYZXhu2//IMB9rBWzQuRNi6xNrPNl80FXWP1r8NICBRbn4JlS1WDOUSIQnRRZvgIkrUjriQGhN9ywWLPFE8Ru1Lnoa7fqQ4a1H08BrjCLzhpV7aq14J99PTW0IxQ74hUxu57nKSPIE9DShBkEFTwp4nlTYqRN+BI3O1NSVA1njfEkCo7Wcyc8VYW55hJ9usBYsx4GCzhbA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 5/r50BPDtdw/wh15i1PXkyFGFLRKBTJM/rgL7+mVkcUEFHLSYMNn5BcT1S8soZcfZ1MMEsI1HmR38hifnmbIw3GEkocs+wkFZbryrginGLj1EeZoCe3NAmfut2P3Qw0BEEhJwpRp8lja32TrGsTByS0Lsxly31XwekLXNUAB1vRWFw6ZiyEelUO0xasoQobUo3mo2NVNvnNucI+++/QxP49WLrAoHHJZKZooSaXVlim8FarozUELHHSeLnjOTTH0pth6bmIU2tFdR7EXBSPLOhLwPs2Y7mgkdJ2QkPBrQDIcqRmW7jZg4DHVdTKqV4emoZUOvJLPwIQL5JFGKN2y1ZFFOCVBpDsy5niVBv+cR8o4zTxxo8FGqSOjsCxAanA9bY3lYaK1jNqiMaqpEUcIEMHRI1OTLH+gJtaTINi1cRtoxdOpKArnG+n29EzRQ4ksnw6b5h0XnRGBuoTIq0oWE88Od3owBdR8FzYmNNM6Q7yetrpI6vKdcZboL7bvstFWExNmL49hiq4+lYxx9sZ+Z0KLo+e+ERAGxCwKy/UCf5N2p13VkRMxIySLGkiXIsiuiue07vlcJhyWKUAbw1/tCkL1xiogQLuMfccyFUvcEvNroQrlwLbaRTihd3B5h0+erTYCjTRl3FiCYz6JahoTzk9jdD1S8LFRSJfsolYw70jVybohbARhy6fTwc9hHaJ6DrXntMLJp8pwzGo+jgGd+L4NhfYSl8bbhfVObLF58KdUpdn/1Poq6yVHcsGv7ieg09mFlZctXtnF+G7Gg1NPY9x6Jd01q1j05xa0GcwIyyfYTJ9REdrDl5pigHRXw15G4xfMV97ObNTbOh2x3CohI6bZznzNl1/S6CHAILl88rSxDC/f34oGdSjvKcSZuN+rZDoUpSlj9+Mr/qlZ65rftScP1tYKcwrIXJh58+SkIhGq4K2ctRLlVPMnwNxMO2E8vE2Y9VQy7FVYZDFqTjEDPbpALZpFBdT58qtF+YyzX4XTf20C1Fem96Mswp/nNd7SmcWKE/lUIw+cqBmyQzomMtna+MEd9NYSOqd2d53qwvmeThZWz7DE5ZsWSeFO+BBJQr07KHEIiPQ0gevvU2c+P1o5Kfm01FIn9ifoFNqeXB93LNefByp/P+HhcguL/axSufxmLZNlEO61KpQz3a4CNsi9anEIiMbIrJsWlB0FhzWEOH+cFyc/0VEAzDTf/jn74NGff2x6T9Ggo157iGObrQUtBFO94R0VIJYrKvGI5q81MsFnVeNaNwrab8pJPFGeDlY/LPI4B3e7UTTj4UW9RJmjiGiItKDIgwMZH77m2IYxlDo4gGm1K1w1AYZwyQ5e X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 88c61faf-c630-4e37-97fd-08d925d05dd2 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:50.9592 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: paiV6HYMitQDkiT2DbmL1osiozvqsTX4zZXYJrGzlnW5SWGqq8JmqkAIUxRAVP6LvVoZS/ZHAvpwr/NiztVhQA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The next generation of SEV is called SEV-SNP (Secure Nested Paging). SEV-SNP builds upon existing SEV and SEV-ES functionality while adding new hardware based security protection. SEV-SNP adds strong memory encryption integrity protection to help prevent malicious hypervisor-based attacks such as data replay, memory re-mapping, and more, to create an isolated execution environment. The SNP feature can be enabled in the KVM by passing the sev-snp module parameter. Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 18 ++++++++++++++++++ arch/x86/kvm/svm/svm.h | 12 ++++++++++++ 2 files changed, 30 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 411ed72f63af..abca2b9dee83 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -52,9 +52,14 @@ module_param_named(sev, sev_enabled, bool, 0444); /* enable/disable SEV-ES support */ static bool sev_es_enabled = true; module_param_named(sev_es, sev_es_enabled, bool, 0444); + +/* enable/disable SEV-SNP support */ +static bool sev_snp_enabled = true; +module_param_named(sev_snp, sev_snp_enabled, bool, 0444); #else #define sev_enabled false #define sev_es_enabled false +#define sev_snp_enabled false #endif /* CONFIG_KVM_AMD_SEV */ #define AP_RESET_HOLD_NONE 0 @@ -1825,6 +1830,7 @@ void __init sev_hardware_setup(void) { #ifdef CONFIG_KVM_AMD_SEV unsigned int eax, ebx, ecx, edx, sev_asid_count, sev_es_asid_count; + bool sev_snp_supported = false; bool sev_es_supported = false; bool sev_supported = false; @@ -1888,9 +1894,21 @@ void __init sev_hardware_setup(void) pr_info("SEV-ES supported: %u ASIDs\n", sev_es_asid_count); sev_es_supported = true; + /* SEV-SNP support requested? */ + if (!sev_snp_enabled) + goto out; + + /* Is SEV-SNP enabled? */ + if (!cpu_feature_enabled(X86_FEATURE_SEV_SNP)) + goto out; + + pr_info("SEV-SNP supported: %u ASIDs\n", min_sev_asid - 1); + sev_snp_supported = true; + out: sev_enabled = sev_supported; sev_es_enabled = sev_es_supported; + sev_snp_enabled = sev_snp_supported; #endif } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 1175edb02d33..b9ea99f8579e 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -58,6 +58,7 @@ enum { struct kvm_sev_info { bool active; /* SEV enabled guest */ bool es_active; /* SEV-ES enabled guest */ + bool snp_active; /* SEV-SNP enabled guest */ unsigned int asid; /* ASID used for this guest */ unsigned int handle; /* SEV firmware handle */ int fd; /* SEV device fd */ @@ -232,6 +233,17 @@ static inline bool sev_es_guest(struct kvm *kvm) #endif } +static inline bool sev_snp_guest(struct kvm *kvm) +{ +#ifdef CONFIG_KVM_AMD_SEV + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + + return sev_es_guest(kvm) && sev->snp_active; +#else + return false; +#endif +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Wed Jun 2 14:10:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294763 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DFC30C47099 for ; Wed, 2 Jun 2021 14:16:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C86276108D for ; Wed, 2 Jun 2021 14:16:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231899AbhFBOSe (ORCPT ); Wed, 2 Jun 2021 10:18:34 -0400 Received: from mail-bn8nam11on2062.outbound.protection.outlook.com ([40.107.236.62]:57888 "EHLO NAM11-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230359AbhFBOQc (ORCPT ); Wed, 2 Jun 2021 10:16:32 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h2nZyrcD5hcAreqAPvHB1U/p6X9N58MLhSPk4+iWO/UrMN+c10D6QKhgxMTSfIxdgLyqhju5xI8eh2wRG4VCOD3oaF4dNi4YalzmWl/BfRyfmME1Qylu5cUXV/rOvmMvcnk3kLbdVCmItXyKuMeb59akbY3ihaYqsBLJo+MsgE6/qq71HgGHvzY8ggLWykd/pfH/3W2qPfh4PfOlg/GIXNsJyWwdVR47/3X3xH7qJ3GugzBB268NgarHy0b1h1MlW8A773FxCmjDdVulXjaCDXJnVRpKYT+GQo1XyQFKxvrVmEgJFz882Ve+QvHr88oPH/IgKs0LGC01g0ghACRWKA== 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-SenderADCheck; bh=MFpeNdFjUsH7IwLUh37P7htqdthGpK5Be5lUjePfE7w=; b=DofFlMQxfzf1CNqxytVhQ5vCAPyJFWpFNrkOfOtb7TA/3HEUZcs/GETnNz6pgoO1l/Y81vapjXw0i7kHo4gS+Lw533yyBahJC/+LXOkdkTL1iwXkr5zZcTrkpAaogW7HQ/60C9t8WxJ5Vkl++bQ6Nbpufm3OlBlquQ3F9BnPARzQolR0A53F7Elm2hh0SNfHY0ekmREstS8lgq8HzDpAQSAaMGja2lIAEs9kxjOJfQcnsvk/gbeloFxQ4aNanBtj13f25mrfuPAH88Jo+MJoOmwxQuBSI4MfJz+RiJ4cNrV0oujF9d5d/ryFmuRlb0x0ywI8EBSJarCodca5mUAn2w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=MFpeNdFjUsH7IwLUh37P7htqdthGpK5Be5lUjePfE7w=; b=avgADYZCNNN39eN5TGJCXzkElXsCWWtff2afbZ2hC2O7BBiAMVrI5+0MOC6WtdWxcoKMUQ6hO58B9JQTrabPAiaj8yxRbkHnKaKZpsJTa66S6WnsLQY8zl1qNVlptRCxfYnY6LXI+f8aTW+Vh3uMw8ztUIFdtHFKejEpjBK5xQ4= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN1PR12MB2368.namprd12.prod.outlook.com (2603:10b6:802:32::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20; Wed, 2 Jun 2021 14:11:52 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:52 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 19/37] KVM: SVM: Add KVM_SNP_INIT command Date: Wed, 2 Jun 2021 09:10:39 -0500 Message-Id: <20210602141057.27107-20-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:51 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 53bec168-42cc-46a3-c2f7-08d925d05e90 X-MS-TrafficTypeDiagnostic: SN1PR12MB2368: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Rp60OCQucj1PsDZ4YxsW7LgWxJH8O5dtMG6A4Z1pRa3qmSrHOJoFY+8IIEyHm08k407+V29uryuMyoWoePoO+D4nuW/sB8C7xT/eA5KzYEXooO0Dq4XnRfve7qvXVATE+TnYY1R2TGv/QoGTGWgoK5kFY+Kg66KLL5SBSD3WRqjwFsj0uwIDEsNcksJp4WKP3gVEGYhMzBuIB8LPfG3I3x/gaw0MRolmv/8f2HSM7lF0OuaQfu/m1El6R3l3IjjMt/wwb9aQ9Y+8WP/tqHrjZZozJG4Nif5hBiI+WDVhy1d2F+HNZ6rUQVKWY5fICdJmyhLshtm4r4NDRd+LB/BdcOZTckjXhU/73b0+vdPLW0xoXMyZlxazhHH9POCEuShJ6JrB1q+nuhzHa7b1d8CkfYu7zsJPme6wlkOXB8K0c1eA4uVX+GejUHLQKnN3kvtrN33ljfag0mCHFXx/2coayCdznoXTTa3v1T5No2EhpLAIR56goStHZ6gfWhbPzWaoeygLhnMEic0KoNfhxgwuxI5Xrdpz/4p75iD3g3JEte3vU2NoPQmE5LugFvEobC27Eq/IBVHoBA1pwjAmc31K3C1+cTO7sJiF9sTvgQNXMbBPkgQuFSxN53fY2a4tjPRwoViD60Eh0P8mzTtV2QiOhw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(396003)(376002)(366004)(346002)(39860400002)(5660300002)(86362001)(6486002)(52116002)(7696005)(44832011)(38350700002)(38100700002)(956004)(2616005)(1076003)(8676002)(7416002)(8936002)(478600001)(186003)(316002)(4326008)(16526019)(26005)(66556008)(66476007)(2906002)(36756003)(54906003)(83380400001)(6666004)(66946007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: Ayao1/BVLSzGZbmgJ5E4B4/JMpbHXKHKkz0qDfI4GHUo+qr8e4dwL957lBVTK5OMdq9zqcJn7ExV5B3tvZwyLXoFS6DIPZq+juNsFSmD1d2Nb/aIn5Xiw8OeSSq3qmjOOasOJPxs0Q8q+Gp4NfTWHwArB17bosYGTyuA08SgzGKE+oPPJiFnCXEOEmWyKEJez+GEf4qQ/zq990/t5te6+lWd49ZxJtZPouDBZKKmZMcQEtQFJtx2GRod32NDSQqWo0sV4JRT3CmgAA001Hw9GLte5x3lf6jhyLkMZv6XxAbSwBKyO13qghksPIGmu0TlDFYjFMLryaX904WW3zyCllbj/ukZ6tprCbAtMflE9sxQIMu2y/3wk2e6FCbKUQdaX04uTEiLWQakHE+vcT2WUurEr0QpLELvc6miFMGiIdvFPSeW7ApRl7khs6c1HzANwTTl2KINbV6VjisT79gG6a9nKZUk3Kk5cy2FvV1K2zCw5J122WUGsETX1QymqqNRHmCxOd83XWpQ5dgjg1/7t+e0BMAJTI6KVfVW0Rtd2O0TxGZVrsEozYbIRRLVGJCsC+AyqScCTvy4X23TkBihvVyFvSYghNvq5k+zznPMQwCCK+lzqSSyoKIhYkznO5FMYh6OIJMYYZ5aGl9ImIW3prp4KrfOTqctFBWQFwUb9PJx/eWQiA4xgy9TO1RyRCcbW3SRWW/AAvi4+Z3xBMJiKYWbVetC9x2o56SA2oSy3iT/e3yJNMJA6p+CxS4LGMtA549maZjX3b8uBS9sYf5J0un2NChK/qaSyuCa2orc7jzkghwtkLxQigd6zWC8nxgyNv11l5hDLnHN77NZRuM35aC6wlFlDVNsBi2eCW9SC4fQpoeYvwOPef4vv1HhM50hYi3By+qrnC7WhJ66OELipU4t7yWaSMUXrdytd/tl0G8N4ZKo2PKnbrOtbVLrUsqf5MGIryFKtqfe9/zLrqkhRhV8E/Uv+BZZcj8rqLpC0b9ymqKL/oYhUHUTBbmGCGrHNrWFWwc2miAgxEOquMyqmyCAPyC2i/U3H2WNc11Smtx97qcXhf1l/jwae8HOnhjs0kTPp0joB7SIQt30KSl75zrtGHKwBqxQW/js92zxpoI+qezIYmyS37faj6oQOlF9+TStWIo89U3FjOV79j3FLXD2QXeecDXFT2pOhf7s32g8enITzIrNIHGTU3ux8bzWVuDymSA9rDD3bb8mWEpr6fvF7cJXhtIZdIPUPbiMnzkwDUy499a6XxyzGlTZEJkSlqOF0f58aEcEI4qoLSe+w6531P62s1FpLopNNwgt+Ow0u4EjadN5oG3wfS9ppbeD X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 53bec168-42cc-46a3-c2f7-08d925d05e90 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:52.3254 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: HHN1+VOK/nFwzPm1Uc4wrP5YGEmosM/PrdFmfG0Nsv/cbsfXPox/CM8jDsgvJ6kG9oCd/3D/IVr7xPJ+R3ZwQg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN1PR12MB2368 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The KVM_SNP_INIT command is used by the hypervisor to initialize the SEV-SNP platform context. In a typical workflow, this command should be the first command issued. When creating SEV-SNP guest, the VMM must use this command instead of the KVM_SEV_INIT or KVM_SEV_ES_INIT. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm/sev.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 3 +++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index b6f358d6b975..65407b6d35a0 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -212,6 +212,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_NESTED_CTL_SEV_ENABLE BIT(1) #define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) +#define SVM_SEV_FEATURES_SNP_ACTIVE BIT(0) + struct vmcb_seg { u16 selector; u16 attrib; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index abca2b9dee83..0cd0078baf75 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -230,8 +230,9 @@ static void sev_unbind_asid(struct kvm *kvm, unsigned int handle) static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) { + bool es_active = (argp->id == KVM_SEV_ES_INIT || argp->id == KVM_SEV_SNP_INIT); struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; - bool es_active = argp->id == KVM_SEV_ES_INIT; + bool snp_active = argp->id == KVM_SEV_SNP_INIT; int asid, ret; if (kvm->created_vcpus) @@ -242,12 +243,16 @@ static int sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; sev->es_active = es_active; + sev->snp_active = snp_active; asid = sev_asid_new(sev); if (asid < 0) goto e_no_asid; sev->asid = asid; - ret = sev_platform_init(&argp->error); + if (snp_active) + ret = sev_snp_init(&argp->error); + else + ret = sev_platform_init(&argp->error); if (ret) goto e_free; @@ -591,6 +596,9 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; + if (sev_snp_guest(svm->vcpu.kvm)) + save->sev_features |= SVM_SEV_FEATURES_SNP_ACTIVE; + return 0; } @@ -1523,6 +1531,12 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) } switch (sev_cmd.id) { + case KVM_SEV_SNP_INIT: + if (!sev_snp_enabled) { + r = -ENOTTY; + goto out; + } + fallthrough; case KVM_SEV_ES_INIT: if (!sev_es_enabled) { r = -ENOTTY; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 3fd9a7e9d90c..f3a86d3a8f04 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1678,6 +1678,9 @@ enum sev_cmd_id { /* Guest Migration Extension */ KVM_SEV_SEND_CANCEL, + /* SNP specific commands */ + KVM_SEV_SNP_INIT = 255, + KVM_SEV_NR_MAX, }; From patchwork Wed Jun 2 14:10:40 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294711 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id DF56FC47083 for ; Wed, 2 Jun 2021 14:12:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C4656613F3 for ; Wed, 2 Jun 2021 14:12:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231204AbhFBOOj (ORCPT ); Wed, 2 Jun 2021 10:14:39 -0400 Received: from mail-dm6nam12on2047.outbound.protection.outlook.com ([40.107.243.47]:30304 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230209AbhFBONj (ORCPT ); Wed, 2 Jun 2021 10:13:39 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YNk8mMrR+DDbFp6HEuL24gxa5TIijYnlm8pVL7ela8DAKRCGGQIxP4yqumKAP2m5KzEhH+eX5DDx+e1GVQmZZoJRWy77w8fJyys2wKdp4S062QLh9GTqTjncofNrSYD3lyr8Vz7SAcqoN0AomifmR8umvN0k57EICK6yBbNcBonILEjgwO6x3jnT5kH+mm7/xIwxFbwiDLmDI4FWIhK7GbXXbtCLuvYbX0FFpHjEMwIHoAtjxsAodGCDXmxGWWUct2NHNp5VadQbbolyurrRZkhlkOup5Z2ZEUWNixKxv3htG3f9Gm6qn4znN/r9SYNwpslij8bI2mJFx1KombaFzQ== 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-SenderADCheck; bh=54QajzVC4b5Be8qChUfvrQpUNx0sYf93+mCe253wOFA=; b=hCoaG1J9JnUXv+zng7l1M3d152L0oTXoTQjnKH2cVyqeKOZoy/vNF8W+1etklAWVrpM3ZCN8wkVAWkXqXWCt8VZ7IhJiG0Aqai61BEZFWdPOgDvKyA/4FsLd735Bl8KhtW+8tFQCXP161FPgyYlVDpjxMq2eqOTELkduY9y31/u+JI0D3N7rZKddhJRtDJgfh0+BztyLziG1jDZL12/JW/8T/jejzoTw4Jtx7ce2oRGGjZoxp47pMWyyJjM8HenYSJXxMgV9lBzoV3neadkciqPBh+XC49ASMy7Ii7a4mRqsXO0IF7L1l0kw2IuTRF8pKkuFA/WtgYqjBOAy1ncsqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=54QajzVC4b5Be8qChUfvrQpUNx0sYf93+mCe253wOFA=; b=48qtUK52LJglh7kgwblA40xvvSRdwf2g47289Mkz2CDv0G1aS1kqjrsU7RHRjCebxfxbCTO8yMD3L/u7c3TShiSVWPQnDHkY2e6gZy04FXs426ouawj+/xQIcYi5EpDwheCZOFxb8Qnzbjv/3Z5Mgt4/eeHQS5AXnFTPia/Ja+k= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:11:54 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:54 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 20/37] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_START command Date: Wed, 2 Jun 2021 09:10:40 -0500 Message-Id: <20210602141057.27107-21-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:52 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ef970233-27e1-4925-96ee-08d925d05f8f X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: udYJAtl+u/4E3EcdI6C5fIkaTgWfbJgzZOwJCYlXQfR6Gcs5cDEspvMZ0qeVOGhFZ811qdiq9jeRZFz/INe2G9LFRIXVsunuc0XaOv2i6023ShVqfmpUxt7j4yGT7rDRZuBSLigys3JW8KjFJK/ha7Bz1YAOmTVe84kwHwk/Ho8/N7+WSakCh2aZfOcm+8CKq92DtsQf6w/sWFcS2R7bTzxUIrOmf5flwQfTy1zpR5nmGZFb1ZaL5kmk0THMZSE0O0UiyvbImWBAXX2pAKYm7h+57FDczT1hNN3yEtr8NUJ0adauInhWxG2TT1Hoee5WtNwPSayMIGS/jMbsOSnEaT+ejPCSmkCp2UR/6mZRfbfxmsuCrSSrgmzAleXiTz6w/lyFaOBy4rgL+Yq6RsI5D1snSn+ODbWWQVe0YputK1pPAM4KvhuTUPmWZHmlTnm8iuGNBDJHoSnzLocZWtp52/WDMIZ0yLNVGYdmu+IvTC3H/h+Lt39LOf2kmrp9tmqRFFol06oxyAk7PzJDY48IIu324j551+QWeV4JLm/WUAYw5k7FF6Hem7a+xdYEWqTaHn6zg2GquhNZ/DkovoEUVeYsQZ2rhOMQi5yqKALtZ3+Lmc3uUoA6eDrzURsywYHzr6zJ9CPaJPDXWMWvkZ1HYg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: iY1Qw3SpK//AAB//5j59/vizbjUCvVQBbB66mpepL0t6q9/nr6GbDnBzAWGd/yHmq8eWVFMoSkHiE85dwsXUPQ5Qke+kjz+v2fOvS6ke3QEUPbYfUPEkaqno1NSEQkFJcT9ek2KW83bxG0tJXCNShgdh8q5xPW5rQOmZUMFoCw2ZM1xJIXOnY5uFKdp+RAjtMgI7s9fXJeQ5mnF3lv5JD8rXdFU/4J5QLyPw4owN+ku02nofHrVO8GlmjZYuoHhBt66Zx7foDHlL5jeZVJFNfOfgXls1AbEE8R+kNZ7R3br7y3sxMc114DPNYQTP2d+JUse/LpxmCub8/+1XQ3R3UXi6i7Q1DK/txGy9qWBiXod9NuG4ReDap+M/IsHqWuCtzvroHtwv5szEEPA+vs+5safIhZKtkvggJEZS2e1FmiP6ds0JlTG7JovVg49sCNJRc9MI/ciNPy+SIDsEZO8XZfH3WJsVKapCGF4GCm0JNLHibET3m+fz0GmuU9a29xDqMLR+w5B0jP0Wm2iaBg3ti+YFsYJK3Btu98PWXDQgQnBr4+5NVDgnefXWwA2F9Hxi1dpB++8nMMRjF5QQ54lA1yigXI/zFdyZEqOsF4s+wvfhREesu5qpI3qS8py755GMja1aXc7w1j0wbrBuTHl+2fSfhkbYyF9wsBkXfgM5bUJz1hCnWL/Wrqj9Snl6jEdB/h0VDrFJZgjlCwdy1DTRFkr36LNtYXkpPIYl9WwI2DcUv0a9QdmE6woqtAtewCISgw0TYL+iQaaCFEBu1bP5kh5V8fLpqZyNdV7e17fY/7DFpH5MOOE9Jn9BIDvmV8I+u3XT9IL8k3ACN1UkxrWnzzKHmLK666BK4fVlBgTkBLizL1x2Q2EjZIqQ/IhEz78qa/4AjYwvZ5GxU0/muC6nQxxWhI094ReIzxaXcCFHRcaN8YolnCTjGUIUGQTxkjqdlEygJ7xnzOxgculRp//cvOxXFeMAhrRhBmMM7oL43IBReVlRxWduQERCOW7wxECFFrxEpFbQe1hRwg2Wrv9ciD9GIMMz9nhM/+3MZBIzxR7Nt8vthsPbhmAe7Tz4t3nrQoZ5tB/73m7AtT0ClgPyBJZ9Mf0/OyhYnCsY0PZ6I+FQzu3QrcEVMO9dGycgc8j956y/XBRVkBtpGJ3GxrWRzQn8xVhW7pwt6Lw3UwtO/HGlhpD8l1sMggVGHwUyngtKv9WuSygKAPgixlOqPT5sZEKeqsNMUzOTUlP8InknRxM8SkTFY1j1tdGT/CrazyDrblV9g1ZIFbclu1Ko9pVggfp7mVxeqNIhdsd9lFVPytB6N56zw3AGVZ6J7WodmaKk X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef970233-27e1-4925-96ee-08d925d05f8f X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:53.9425 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 1FmwpMY5zn7ZXWMgejevRclXU0b1frGqfxWJlL1TOruoSAFMZ3h5khK85/h2funEMQN3V/Ft++stOhlYW/UEAg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org KVM_SEV_SNP_LAUNCH_START begins the launch process for an SEV-SNP guest. The command initializes a cryptographic digest context used to construct the measurement of the guest. If the guest is expected to be migrated, the command also binds a migration agent (MA) to the guest. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 132 ++++++++++++++++++++++++++++++++++++++- arch/x86/kvm/svm/svm.h | 1 + include/uapi/linux/kvm.h | 9 +++ 3 files changed, 141 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 0cd0078baf75..dac71bdedac4 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -20,6 +20,7 @@ #include #include +#include #include "x86.h" #include "svm.h" @@ -75,6 +76,8 @@ static unsigned long sev_me_mask; static unsigned long *sev_asid_bitmap; static unsigned long *sev_reclaim_asid_bitmap; +static int snp_decommission_context(struct kvm *kvm); + struct enc_region { struct list_head list; unsigned long npages; @@ -1508,6 +1511,100 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) return sev_issue_cmd(kvm, SEV_CMD_RECEIVE_FINISH, &data, &argp->error); } +static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct sev_data_snp_gctx_create data = {}; + void *context; + int rc; + + /* Allocate memory for context page */ + context = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!context) + return NULL; + + data.gctx_paddr = __psp_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); + if (rc) { + snp_free_firmware_page(context); + return NULL; + } + + return context; +} + +static int snp_bind_asid(struct kvm *kvm, int *error) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_activate data = {}; + int asid = sev_get_asid(kvm); + int ret, retry_count = 0; + + /* Activate ASID on the given context */ + data.gctx_paddr = __psp_pa(sev->snp_context); + data.asid = asid; +again: + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_ACTIVATE, &data, error); + + /* Check if the DF_FLUSH is required, and try again */ + if (ret && (*error == SEV_RET_DFFLUSH_REQUIRED) && (!retry_count)) { + /* Guard DEACTIVATE against WBINVD/DF_FLUSH used in ASID recycling */ + down_read(&sev_deactivate_lock); + wbinvd_on_all_cpus(); + ret = snp_guest_df_flush(error); + up_read(&sev_deactivate_lock); + + if (ret) + return ret; + + /* only one retry */ + retry_count = 1; + + goto again; + } + + return ret; +} + +static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_start start = {}; + struct kvm_sev_snp_launch_start params; + int rc; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + /* Initialize the guest context */ + sev->snp_context = snp_context_create(kvm, argp); + if (!sev->snp_context) + return -ENOTTY; + + /* Issue the LAUNCH_START command */ + start.gctx_paddr = __psp_pa(sev->snp_context); + start.policy = params.policy; + memcpy(start.gosvw, params.gosvw, sizeof(params.gosvw)); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_START, &start, &argp->error); + if (rc) + goto e_free_context; + + /* Bind ASID to this guest */ + sev->fd = argp->sev_fd; + rc = snp_bind_asid(kvm, &argp->error); + if (rc) + goto e_free_context; + + return 0; + +e_free_context: + snp_decommission_context(kvm); + + return rc; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1597,6 +1694,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_RECEIVE_FINISH: r = sev_receive_finish(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_START: + r = snp_launch_start(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -1790,6 +1890,28 @@ int svm_vm_copy_asid_from(struct kvm *kvm, unsigned int source_fd) return ret; } +static int snp_decommission_context(struct kvm *kvm) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_decommission data = {}; + int ret; + + /* If context is not created then do nothing */ + if (!sev->snp_context) + return 0; + + data.gctx_paddr = __sme_pa(sev->snp_context); + ret = snp_guest_decommission(&data, NULL); + if (ret) + return ret; + + /* free the context page now */ + snp_free_firmware_page(sev->snp_context); + sev->snp_context = NULL; + + return 0; +} + void sev_vm_destroy(struct kvm *kvm) { struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; @@ -1828,7 +1950,15 @@ void sev_vm_destroy(struct kvm *kvm) mutex_unlock(&kvm->lock); - sev_unbind_asid(kvm, sev->handle); + if (sev_snp_guest(kvm)) { + if (snp_decommission_context(kvm)) { + pr_err("Failed to free SNP guest context, leaking asid!\n"); + return; + } + } else { + sev_unbind_asid(kvm, sev->handle); + } + sev_asid_free(sev); } diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index b9ea99f8579e..bc5582b44356 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -67,6 +67,7 @@ struct kvm_sev_info { u64 ap_jump_table; /* SEV-ES AP Jump Table address */ struct kvm *enc_context_owner; /* Owner of copied encryption context */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ + void *snp_context; /* SNP guest context page */ }; struct kvm_svm { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index f3a86d3a8f04..56ab5576741e 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1680,6 +1680,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT = 255, + KVM_SEV_SNP_LAUNCH_START, KVM_SEV_NR_MAX, }; @@ -1777,6 +1778,14 @@ struct kvm_sev_receive_update_data { __u32 trans_len; }; +struct kvm_sev_snp_launch_start { + __u64 policy; + __u64 ma_uaddr; + __u8 ma_en; + __u8 imi_en; + __u8 gosvw[16]; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Wed Jun 2 14:10:41 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294713 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5A2DFC47093 for ; Wed, 2 Jun 2021 14:13:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 42C08613EE for ; Wed, 2 Jun 2021 14:13:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231352AbhFBOOl (ORCPT ); Wed, 2 Jun 2021 10:14:41 -0400 Received: from mail-dm6nam10on2044.outbound.protection.outlook.com ([40.107.93.44]:27560 "EHLO NAM10-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231392AbhFBONw (ORCPT ); Wed, 2 Jun 2021 10:13:52 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KdLexQJkiOqvgEwBX7TUOBmizO5ThHMgpiyT5MJv8TmbtNX5swrgBESPtBWEfFmFdvVdNU4ogBtQvFmHj7impVH+A5dKNTd6NY3K1ARq8pgZBYViUUI89CoeGd1JCJmGxUpU2k5YIVpc23ZCHnYSf7RT3D043Y8K/C973OU2yEM9yg+XD1wIRF2w7caAUzJ4mUIjdRpN/d37/JUbHlIU8rozHoVl6Vj3TxThp7HSIro85xjfzSZiUA2XTaX7UBYHBisDEyJD41oJDyWQn9ho8m7ayQvXqwfY9HM3KFaBYyM4KeO5PS8YNJg7wCPp4WlWH4g0yq7fGV9vnslpzuj+Yw== 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-SenderADCheck; bh=Fj30EHwlZ9k9PEPjfuYC35sYctmpeoUpUlo67lH5/OU=; b=RYcf4GDtjcFEJmxU1RbsaFeZNyglIl7d3LtXcRikKwYPeSwdczOpqcQ46Ysgrcjcu55Zm8g5IVdVMOp285MsBMR7Kw78zjdp0FgHlNPvr1/t9JU3sPLFi8f3dkajpS9kUQExJUsXQ1cvMPviRIigA8jPUSDhJJKnd0KrqKPa0B3R81Qdt3Vp8ZhF4zV36/5rp7DkTwXSLi5+uzy4MgCOzwuzYijIn45OWpPIAD6Cg2benhz0loU08lUv5OxbRTJJASYIsn6yTgZkIbR7Uo1B2aSd2JeK4YXxL++AwRzvJlRZtohOzodnTpZ02OmWAatcLOtidYfGeKfqsJj2nOQTAg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=Fj30EHwlZ9k9PEPjfuYC35sYctmpeoUpUlo67lH5/OU=; b=yo7ysLAelDAojnk99Ov1k9PuzNwW9csWwhOSzohSFhWZGX3lVqq5ycehnmMMOpMtu+ihDJWIa0Wfcd8hj1BPxhTAL1ovIc0URYDeiNTgG6RBLXmMt0SYh+M2jSDTCRKOYUc3aXGP82AFq4Q46tm39C4hQXYv4DTgwzZw9a4e+dA= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:11:55 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:55 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 21/37] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_UPDATE command Date: Wed, 2 Jun 2021 09:10:41 -0500 Message-Id: <20210602141057.27107-22-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:54 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 37222725-8ed4-4af0-8471-08d925d06057 X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4941; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dTmoTivpCjx9ud+u29BpLTPIULWnsMovs9+ygRLrJGTmR9fLW6GqGxTygsvQzvg2TAr5tXmnvWIYNMffGaJfapOrz7lthprdMXg5ahHxHIKBiw7XRwdDtK+4ZHcxZtO91NIAfJeefEQkAhbWc4uCW6RQ+RaoBDcwikRimcPoqtQg34R/eREuunFhi30gEYW2Rlhjp+/Com7s7kz1IsdhgNi/hFJs9Vek14qVD+mPxfRzLifiemKktp2dES03AFhpSLD5l+sk2ihOrU+jkrG89DLWppkKS0sBCCcZAGWILih+C8SxYjMNqMy1lIZxvvaCpzRPNcQ3Ib8EsmoWOZOjhLmbshDeUb1AXTv5S9IHfLzSWq/uUNeZzpVagZNVzI292SYtsXyesxTHiFvmU7ZyRw7k6NEHYeqOy5afwp/yQPnoPumsQGrgMbfFeEPW0+Y21JWWJNUyFAoLN1OcO1fofIDUNfqhU4hwB746f7Su0dPaEjnt4qV8SV8N6rghKCo1EbcMXqbmcD/tvpT+LCjHODYhTtjtugJo8gcOQ/YejhXRX/R8SE09k0JrK0+7VnJSbvmnY5jide8Fiw46u0R4UzzK6R8qoYM0tI7dVrx3K6jbzBlXna0zIh7H3Zg2gct/Ps56m3ZpxBCttD/cbrC6wQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: H+xF+Gr2Fhm1ZzZ8jv86zVfSLKR4HE/1I91WUFgHiCKvzP9ls7O9dvBSmgZ5rbVtauAtwzD9tHXDUXTv3H8HsG2cckIMqyCGjKi2M547aNY6d3NcfXU82fiz8DLQBogvysyBNxZf1YnAuOuqPOz4Z0WoC6z023mnLC0KBeI1INKYxzthYhUJdB0+Yk9UEzabvwL7Vt1QLe+Jipme1qwEs/cmfbOQxguwc6rwBaf/oLuUz8Ze8tQKckyvSECr4p9kLjOkge2fCM6gBQ+ymcGzijVfFPpBpyrTTtjtWyqc9LUTI6PDzPxPtRod8oS5AcVWOJgxXEK5h/NzwjaQ2aHLilnU8Xug+obP+tbdyi+UpweYI07SZYRM06mvPKeMJsjKwow34d8iMbaBMrfQzAFSVJJMesxx31ebNxjOjwmVzNykn/OLThb2MLXU9/aotdtELEreM6KRS+U5tOQ2HW5sj8Iy4Dt9mqvxoBzJEhaVQgwm/M1YV+jSG8XS+FRm4Jfp5Fw/3j2ICucgkxfqrES/kEChdO8RRLJv4fKNtvC9qZxQIbdrgs+xSG2xuHkN6twjaxeyskVm5bZsY0pCZ81CmxRGlktdgcG9kFiTA0i97aji3qN7M9q7vscsIw/tdpCl7s1hWvyXsYNn0dILHMLV9Gqr43SXES92Ttx8+8rwCWd++pCfz88KxxCfggnp94is9C7k51hNhFb7uk/JADzcFCitvyILvsDM1s9HT0lt9MVuQfOOmnKU48ZERbhDXBBor+Ay2tudJ4ep+dDyl1QpKa4TVoBiqYFvsIuaDvOjyrNlB8oE2AV/f0bcsKh1J2nj5jQSF0WBvG/bWWu6JnMFa1Uh+ZVy+qqbZdCd7DGV2Mic8Ecqg1/Tdg8kOsSoLDj0B5WqHfahN7fONMfZWzwJ6VyqQbLI6i3WuZw7S1OsZeXZPq0sq23f/2GrZ4aDDHl7BiaNDgGgyBwNbasnMuleoKLdF2ogZadTSogNxFNHOgiK4b7PAmevJFAS89/1KBdEI0ZYghSuPSDUX12IX7AQ6Ujm34DzEw4qVQsI/gA9qhytTthOIxOGFPTOHyNycPawPyULmGSMisNoiBLS7H3HkY8ZkJJ8EsanhiLw1EHubhp/431WyT8RTZyvMlS4yXNy3bX9B952Bn0k552E3Ef52QGcKQs0/MJ4YIHEAfXWF3Zkk0kM8V+pFoGXABLAWaiSyzbnytH0z2BRJyBggd8pgRSBSoxIJBEnwro+hC8E2+YOaFHP7r7NhMZDCnV5t1f5TgmY7OR41tHDMlTUAdZQn3woMs2Qte2su1PisO5JEwDCxCk46S+0Lzlt+XLH44sn X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 37222725-8ed4-4af0-8471-08d925d06057 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:55.1698 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: kuF5wglqHlcQc7AWgBs+wNnvCWnQeAspPQs6spmELInT7xYWVA9MrUveioVLarLO/G7To8uLpTTpEJ5wKvZF0w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The KVM_SEV_SNP_LAUNCH_UPDATE command can be used to insert data into the guest's memory. The data is encrypted with the cryptographic context created with the KVM_SEV_SNP_LAUNCH_START. In addition to the inserting data, it can insert a two special pages into the guests memory: the secrets page and the CPUID page. For more information see the SEV-SNP specification. Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 139 +++++++++++++++++++++++++++++++++++++++ include/linux/sev.h | 2 + include/uapi/linux/kvm.h | 18 +++++ 3 files changed, 159 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index dac71bdedac4..dc9343ecca14 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -1605,6 +1606,141 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) return rc; } +static struct kvm_memory_slot *hva_to_memslot(struct kvm *kvm, unsigned long hva) +{ + struct kvm_memslots *slots = kvm_memslots(kvm); + struct kvm_memory_slot *memslot; + + kvm_for_each_memslot(memslot, slots) { + if (hva >= memslot->userspace_addr && + hva < memslot->userspace_addr + (memslot->npages << PAGE_SHIFT)) + return memslot; + } + + return NULL; +} + +static bool hva_to_gpa(struct kvm *kvm, unsigned long hva, gpa_t *gpa) +{ + struct kvm_memory_slot *memslot; + gpa_t gpa_offset; + + memslot = hva_to_memslot(kvm, hva); + if (!memslot) + return false; + + gpa_offset = hva - memslot->userspace_addr; + *gpa = ((memslot->base_gfn << PAGE_SHIFT) + gpa_offset); + + return true; +} + +static int snp_page_reclaim(struct page *page, int rmppage_size) +{ + struct sev_data_snp_page_reclaim data = {}; + struct rmpupdate e = {}; + int rc, err; + + data.paddr = __sme_page_pa(page) | rmppage_size; + rc = snp_guest_page_reclaim(&data, &err); + if (rc) + return rc; + + return rmpupdate(page, &e); +} + +static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + unsigned long npages, vaddr, vaddr_end, i, next_vaddr; + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update data = {}; + struct kvm_sev_snp_launch_update params; + int *error = &argp->error; + struct kvm_vcpu *vcpu; + struct page **inpages; + struct rmpupdate e; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + data.gctx_paddr = __psp_pa(sev->snp_context); + + /* Lock the user memory. */ + inpages = sev_pin_memory(kvm, params.uaddr, params.len, &npages, 1); + if (!inpages) + return -ENOMEM; + + vcpu = kvm_get_vcpu(kvm, 0); + vaddr = params.uaddr; + vaddr_end = vaddr + params.len; + + for (i = 0; vaddr < vaddr_end; vaddr = next_vaddr, i++) { + unsigned long psize, pmask; + int level = PG_LEVEL_4K; + gpa_t gpa; + + if (!hva_to_gpa(kvm, vaddr, &gpa)) { + ret = -EINVAL; + goto e_unpin; + } + + psize = page_level_size(level); + pmask = page_level_mask(level); + gpa = gpa & pmask; + + /* Transition the page state to pre-guest */ + memset(&e, 0, sizeof(e)); + e.assigned = 1; + e.gpa = gpa; + e.asid = sev_get_asid(kvm); + e.immutable = true; + e.pagesize = X86_TO_RMP_PG_LEVEL(level); + ret = rmpupdate(inpages[i], &e); + if (ret) { + ret = -EFAULT; + goto e_unpin; + } + + data.address = __sme_page_pa(inpages[i]); + data.page_size = e.pagesize; + data.page_type = params.page_type; + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, &data, error); + if (ret) { + snp_page_reclaim(inpages[i], e.pagesize); + goto e_unpin; + } + + next_vaddr = (vaddr & pmask) + psize; + } + +e_unpin: + /* Content of memory is updated, mark pages dirty */ + memset(&e, 0, sizeof(e)); + for (i = 0; i < npages; i++) { + set_page_dirty_lock(inpages[i]); + mark_page_accessed(inpages[i]); + + /* + * If its an error, then update RMP entry to change page ownership + * to the hypervisor. + */ + if (ret) + rmpupdate(inpages[i], &e); + } + + /* Unlock the user pages */ + sev_unpin_memory(kvm, inpages, npages); + + return ret; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1697,6 +1833,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_START: r = snp_launch_start(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_UPDATE: + r = snp_launch_update(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; diff --git a/include/linux/sev.h b/include/linux/sev.h index bcd4d75d87c8..82e804a2ee0d 100644 --- a/include/linux/sev.h +++ b/include/linux/sev.h @@ -36,8 +36,10 @@ struct __packed rmpentry { /* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMP_PG_SIZE_2M 1 #define RMP_TO_X86_PG_LEVEL(level) (((level) == RMP_PG_SIZE_4K) ? PG_LEVEL_4K : PG_LEVEL_2M) +#define X86_TO_RMP_PG_LEVEL(level) (((level) == PG_LEVEL_4K) ? RMP_PG_SIZE_4K : RMP_PG_SIZE_2M) struct rmpupdate { u64 gpa; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 56ab5576741e..8890d5a340be 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1681,6 +1681,7 @@ enum sev_cmd_id { /* SNP specific commands */ KVM_SEV_SNP_INIT = 255, KVM_SEV_SNP_LAUNCH_START, + KVM_SEV_SNP_LAUNCH_UPDATE, KVM_SEV_NR_MAX, }; @@ -1786,6 +1787,23 @@ struct kvm_sev_snp_launch_start { __u8 gosvw[16]; }; +#define KVM_SEV_SNP_PAGE_TYPE_NORMAL 0x1 +#define KVM_SEV_SNP_PAGE_TYPE_VMSA 0x2 +#define KVM_SEV_SNP_PAGE_TYPE_ZERO 0x3 +#define KVM_SEV_SNP_PAGE_TYPE_UNMEASURED 0x4 +#define KVM_SEV_SNP_PAGE_TYPE_SECRETS 0x5 +#define KVM_SEV_SNP_PAGE_TYPE_CPUID 0x6 + +struct kvm_sev_snp_launch_update { + __u64 uaddr; + __u32 len; + __u8 imi_page; + __u8 page_type; + __u8 vmpl3_perms; + __u8 vmpl2_perms; + __u8 vmpl1_perms; +}; + #define KVM_DEV_ASSIGN_ENABLE_IOMMU (1 << 0) #define KVM_DEV_ASSIGN_PCI_2_3 (1 << 1) #define KVM_DEV_ASSIGN_MASK_INTX (1 << 2) From patchwork Wed Jun 2 14:10:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294723 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F3DB9C4708F for ; Wed, 2 Jun 2021 14:13:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DEC12613F3 for ; Wed, 2 Jun 2021 14:13:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232031AbhFBOPJ (ORCPT ); Wed, 2 Jun 2021 10:15:09 -0400 Received: from mail-dm6nam12on2047.outbound.protection.outlook.com ([40.107.243.47]:30304 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231618AbhFBOOK (ORCPT ); Wed, 2 Jun 2021 10:14:10 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a6moAQP4uLmV+lHrtETHP+z+SVUQoH9R5VOPmG7sGMolc2iKUAtrwKvDjfeQSa1WPY8qd6A/4bQUXvWx/raKjEf4g9oz8S2sncX3sI2Kx9WkTFD8sGenL8epl1RG3/1LEbkwy/6meGr9Ak32YUo59bB8fUpJ/BNzV2utfqKU688OwdNw+TGad0GO5czVxV55Lb7Sf/m7ka4+I2TTrcE/N82hMtz52qX2DcETWXAQNogYFrq2GWWcmIBKBKEnmtfx7lj9KtpP65OmVuxFK+QH091rL9jVqtyrnTcJD+Qq1MhrDmly6Tm8IvxtFB117rZgZwXB77cnFvqSZj4NcUYS2w== 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-SenderADCheck; bh=wLzPWTu/lzqc1c2enHjemO8mERzlZ61QbqhAo6RqLwI=; b=hgzIsyS3j9MVGJSvSgzB64m6+32w54P9sfnthZreTW6uIcrbfYjdg5zXn7H/p8UH2swNjqT9OceQSSQUqrlIW3q/fsgz6YuskTDm/h8RwsJIm7ZTNDlbzFtsof/ytk9eKYdDxpoN9w3UdrNx/TeJ/6blk90HD3CUHeaDWNo10MDdKwOLN84FYdHGY9scSs2iWZwG3m00kzeRcj37Z0XvKLU0g85wnUbUy+GQ/H7HvIPGFZ1t0oVmhUc2BkpF3meqF6IkoowX6aUy4xOAqRVQU9Q10lXKBv994qkbRoX4vKi0bzuRg7NlI8NDyhpqgdqNoWoLG0+Yt1S0lZuB9waLVA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=wLzPWTu/lzqc1c2enHjemO8mERzlZ61QbqhAo6RqLwI=; b=LL7Vnowl/OF6MPWWw3ZIyMH+hjdpnS42x6Y1fyF7m4NqAYj3L6k/d7/kM2llk0TUfO/aRth29UgzNrTI0wjmve8pVFooBk/L7r05X+PNGYEs7aUvILep5v7xRCy20ZU1SMSKbcO7871LZEagNyclXOi2F4+dkkMMZm/x9q9GCck= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:11:57 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:57 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 22/37] KVM: SVM: Reclaim the guest pages when SEV-SNP VM terminates Date: Wed, 2 Jun 2021 09:10:42 -0500 Message-Id: <20210602141057.27107-23-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:55 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e954e55b-3a07-44a9-9c3e-08d925d0616c X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: npGq16S65B9X2/+7SXjsE1UxZCMl18Zm1xGQYNjBkZ/j5HHjeH5baz/ix2WEQwdguOVYW4Y3yvMA9K+4o/DLkqxxoJRFfGDfm+g5xBD8l+dZ8Gd5/Ny4kkBiAdKI1QBHQvdRZ30QebflT9AzmXQSeushGS4/aplUerzPH61fDSirNOfTrCPaTGmvCWRPrPLqvNX2w0iHp8fSGXwhffTtpd+M5MWvzNR/S89oY/en8B7Rs4yJ5Occ7iYmf9iQpPORzK8foHQxVGacLO1rWM1c2azB85L64SI41bNDioR25UjD1Szw4TN3YW5EYuMGfA/l1EjAuFK4XNcbmdEApc8GY+ZqugjqKHe0Kyo9Bc9MCdtTBzZZpHn15h/9M0hzCRRWtF6c5t4VUCnxM67KYfD8D+KphYKDdWZS2YFE76W2kaNrul5eMu7hEx6muvyC2oO6rZcwFiOjKRyMZ4I6vvKtihLE0gpk/aNffcmTeaaTX/nG/FfEqY6FP80v75wA2AnuKGki7VsAiNYaiDtDDvm1jKDORpOc/AyCAeGoOhthHgwx1sG0qZCt14QcnGPKaiOMriaUGleddpQPiovFNCAkB326ohubWD8jQMVvObnQ1LMsSJd+TcPm33u7F+GRst1k1qXjEBv0OMskGpLtAnKnzg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: JIxappckPtM37e+a+/PINahscU3GX54Bnyn4Sia4OS68XRXvHCQ8EXOq0kVIQ//vjG7MWO6NYvLcSMY3Ctr4atseJ5JoUnfGCDUm12CphfUsHFFgxMPT1RXH1BvZ+v1utwTmGygtI1Ek99Ru1GZZRYRI8DGUKip5pKeYcH3vwNT0wWU5bY3epss9a/rj5dReloA+0A2u1MsTSRNvBNSy+O4C8KnCsnICa45lktLrai4zPETChrsfJdOJGxWuuLS4D6AuO6OLR85Kjv/WW0rX4gBocoqpNc07MyYMynSHIaAki1Af9BDq5tUlvqZP95E0JsJrsKLq7giUoOz0f0ic4HdgGSTTYTpJ7Mxms+i8prd8Hl8nzdg8tzkTp9m7DgyYpeLQvexXEXjBCyRTDwFkUDH41mvzpNvQVVqP2WAEqL1T69Qt/b64BuzsRTgIh0s6OxQuB/AfRLgIce8oQlbd/6AW3H90zM497EV3iUjQARZ81MHZQ28GOo4U1BcweNt56eUyP5Hpr5joLecTYq3CjS6gjPGfz2Ok5HBOM4lSFEPtYwWoIL90B+AyKZO4WDM+x9jNnlLoXzOfyIMuuWiaDCwgyWUQRLfD0FvnWc6uHxllWwgOVp75imw0XVBUW2G92woriwd54BSAmukA5ypjbxhmGEVykgU6+WWghxcwRDh87+DyjI6o2+Op+y2HACRACQHZEbDburSwdADHgu4YsUnwEvpDNUtg4hJ8LJDt6C8D82tpEHMOX4yapyWlrT0fdTgE1ghaP080k8Y0od1P3gNGuVmmqYkdZmWd2/u7YyT3au0c2caJq7KrQyTLHLmTqT1KyNlPXlRlF0GR5PN9n470UxVNLqyJulKLhMtAYIYZVn/Gb3nTnOinN9auxni8i+9lZ5nyfHgSlcmSppMVc8Ei2+8qa1AWLAocWSQ6k4tnVuWLri1UvVYPKIDl1/Nu1aI0OjGC69YFN37lVAPa1A5QClhPUCwPZ8bAJkCtuUYum1FGStX3tyjpl/IfjEFm+Fb7buN/lPvr5ATiFl3whxQu0cmWa0KIx7O/2hvVpxxCvm06qFPOvInInXIvL2h8EVp1bslnddjq0+vr0Cj0+Y+TNt+K1C+llXuiyHbOl4wFfPycMCf2kShEq0JWWnahwo9fo152ak9mZUv2wCkPydO+MaN3Mq9FC1DcFwmqAUYI7/VniaDjvMuvGJa8rFIu5KiLDP6Nj+oAvkfRd/9Vuxiqo3T9AzfngKVpsjomTwcpMyKTWGhbmQ+Q7x0KPeFkWjzAGqhnwH2YyESnD2iMKC2eX03A54pV2/GZjJQeypLaTYnDpg916no4OYTcpTrm X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e954e55b-3a07-44a9-9c3e-08d925d0616c X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:57.0267 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: XKjIPjHm9+ORAb0b1gchE0jziKzDutohu6+f7LfVjs7HCC9E4IW2VB77Ki4kFfcQgldvO51InrPfH/Hj6Rpiug== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The guest pages of the SEV-SNP VM maybe added as a private page in the RMP entry (assigned bit is set). The guest private pages must be transitioned to the hypervisor state before its freed. Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index dc9343ecca14..6b7c8287eada 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1918,6 +1918,45 @@ find_enc_region(struct kvm *kvm, struct kvm_enc_region *range) static void __unregister_enc_region_locked(struct kvm *kvm, struct enc_region *region) { + struct rmpupdate val = {}; + unsigned long i, pfn; + struct rmpentry *e; + int level, rc; + + /* + * The guest memory pages are assigned in the RMP table. Unassign it + * before releasing the memory. + */ + if (sev_snp_guest(kvm)) { + for (i = 0; i < region->npages; i++) { + pfn = page_to_pfn(region->pages[i]); + + if (need_resched()) + schedule(); + + e = snp_lookup_page_in_rmptable(region->pages[i], &level); + if (unlikely(!e)) + continue; + + /* If its not a guest assigned page then skip it. */ + if (!rmpentry_assigned(e)) + continue; + + /* Is the page part of a 2MB RMP entry? */ + if (level == PG_LEVEL_2M) { + val.pagesize = RMP_PG_SIZE_2M; + pfn &= ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + } else { + val.pagesize = RMP_PG_SIZE_4K; + } + + /* Transition the page to hypervisor owned. */ + rc = rmpupdate(pfn_to_page(pfn), &val); + if (rc) + pr_err("Failed to release pfn 0x%lx ret=%d\n", pfn, rc); + } + } + sev_unpin_memory(kvm, region->pages, region->npages); list_del(®ion->list); kfree(region); From patchwork Wed Jun 2 14:10:43 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294731 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 14679C4708F for ; Wed, 2 Jun 2021 14:14:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EEBAA613EA for ; Wed, 2 Jun 2021 14:14:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232163AbhFBOPz (ORCPT ); Wed, 2 Jun 2021 10:15:55 -0400 Received: from mail-dm6nam10on2044.outbound.protection.outlook.com ([40.107.93.44]:27560 "EHLO NAM10-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231777AbhFBOOY (ORCPT ); Wed, 2 Jun 2021 10:14:24 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HMtY2njHM4urBzQ+5Ya7YDp/uYJmVPLkwjS6zHGgZrm19gGZU/ohDaWfZLl9TD2c4hlV0TtwEBMSS8V9EuN3jG4J2nmonE2+bDs9LzslEmDrcsnVpfuLfmUYdScHQUbPEW4NQqf7On7UyCxA4T7vHimCBgIrD8Vx1whY8WvNobUygt4vgmc3f/t58MBRHMkYTwTn+e0LoethgoLzeNZRgW7p+G3mb3eU+mBa07l7UznHKLaWGbDJ705ccAHOzLLRnfiP+Gv1Po5mRLzhUeR4ZWkR+Z6dXjX7qRa0zN/5Mwdq01TmSS5PVdAPJCImYeHNuKTrylqFnL2Rh1iDeshjew== 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-SenderADCheck; bh=VzLt/1RgVgPzPJ0od+Ix1HpNL7dG2JcRammV/qLXJzo=; b=T3LWlL3LTCso8LE+6SFAm9T2Cba/0ILF0hh3VM5K98FJK7QSVmGl4Qxwi1vbuVyeI9bxKXuE2cpAybz/x4TxZml144vWRx9bzXjLlTATFs16CWJSOADIsvdiIFgG/5Xjg+JymlHq4MonxcN2i5LcgoMo5YL3QemxZ7e5zY8lMXKukvPvrWBhgwTHDt7x3WGN09c9r3W01bqZOm7E/ToowRA19oqVy7wN3OJVxYX+22Obp6QI17+KzF00IjfDKUTw2fB8zsyItYOomUao3XQAdKr6XAeAmuJDutyvr4OpeQChmhalKcpdGwOzZvmIkBVvWAWAEogiFO3a9BDZD2aJJQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=VzLt/1RgVgPzPJ0od+Ix1HpNL7dG2JcRammV/qLXJzo=; b=yYvYy7vR9Hd39ijEqiy15OtjlvpPF5oewnpmNOeMympuiA0xvncpYO5zkNrFihNCGyd1MeVCSPz7VxK/Af5Mbg5DzHqEuCSDRIAP6+VoYS72dPRuSXfI+cU43bpsIAxmhIWsACzrWvCTSCn0ygM9TE26x0zbxvNgF7cwhSHQU9c= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:11:58 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:58 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 23/37] KVM: SVM: Add KVM_SEV_SNP_LAUNCH_FINISH command Date: Wed, 2 Jun 2021 09:10:43 -0500 Message-Id: <20210602141057.27107-24-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:57 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 0102e53c-3ac2-406c-a02d-08d925d0622a X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: xlajeET/HUnvhSEIptjzWU1t6epxzjA/pNomjk2Aj26s0JUxPaA52OHOG+e1xkgk9FY6tKglPaLuCWG6CNVBjq32lEWnFZNWVhZ1WZyGwxauTu3QRl9kKA1WKhbpSmRMOmH0guoNDz48f0jWvSlUn2NfdybYSnCERvBuy7kOYD7SEuLCrqKfTXq+zUOIhG113qp6+dMrtbzGtuDdutKSgAusC+7VpsH80GLew6+Lk+580pIG7XYr3dFRaS3sc0w2tLHx8PlA6a4W6brnFS1pz2kFTTBC2kslmAnW9fbYIqvxpKw/+aWDLCsxNqo0Jb9rtF0atNc/Tk9DLosUyasY/QrkvLIHIq28vMdx+hJXqRw1VQaPW+aQOpiOj+icXMSDbhbCXti77Ql021LU9sXbD70h13s8teSxf7gtKLQkXULS0ZiK2pTKeMl57X+DVol6etzOCnkgKc4PZQrC41I4WiGN29Fp7tRk0vBv0JaAR68yZ4NhZCh7To5tkLQe4uBVJarl1BJDbGZx3Q2YCPJBns3HJ4JfxrTRzJJJTc3+9HdEOQ6ZZi+ABmDQKesOiFXxkOP9WkiuMEc/sajomK/+jkNl3n3F7H38PTPtUh7R3hWd62cuLRXFBq8YpeBJd1h0Dh0sDD+8MgcDqN6hC8cthg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: L9tJ7Pzpvy3on8C0+c7M02CKDax2oKJxVx9bMwG/qPV2jVyFOr9ofYPJdIWp/txYSAaT2CpIUEC67SnX6joqmEEpNirKMK+n+ro/uyv3WQFaGfjO9iT7xwTArftNVWDemLB38F8cwrhOYQI3tFUmGJXT+GP1/CEy1sW1UMxg5ufR1jyzXrjO7xiXvcpubVdLS0JE0w0GGO5PERdHhqxL5b4NbfiiDlnqBDmBiyQHuerdZmUQC05bAf6vk2pyESHfQ2Pxcku5ZyPwtHOsg40iI2VOoXNe73TOJcHTyO0vsS7kf39f/v5rd3U/ctt0DuITb+7hxvAOryCcg5z/GcTIcxOqfutlgaXdnOK/4eldUYmSt8bAdpVN4xu6W3dSTyAdoVInZlg24PTTGcDQhi/CIWrbHfQDFvrDx81tLNDrk4QgZwV6tiexiOiuv+XN90ss8fjF9ZuwCxMQQl7g7wN2HjMHF7BYD/6VwWYvVl3i4MIu8yjboFZLlMgzPKafRwYIyYFlUqJEVld5jLLjXgtqdgEykQ51XbvFFYyqME2MmfwbUXw2/TSji3B/pn8cDsvaI+rKxBwh96d4cP/Yc3mbdLQxRNt7qtNtK15Scfmmo1TvkeHNuDwNqk+TXkFq1FZ8feNrYgtnKb0miib3wRijk5LpKfNexdapB8eBxiiCxeK6eNhQWN/6QH4zMQ+fMSWM1slftTyiBRHY4KrHh+Cmx8fL6MFP5TkEeffVDyO23rg1z87/JrlfNv4gX3eXYLN/nvqccLxxYJsRUaToF6q/0DTRIYiIMDlE0jq6zwdrtUNjqcMrwfPUpOz1R0wPc7MD/Z0Go+q2VwvOVyJpXk2EsTGG487fUow+zm1LRr5QjTgb0rjiHEiomsTt9CNqo/zH4yr6jHxMQsVqxKJEwtPpAVm2t7jr1zcW5MYonftubX53hJUCoNz3ydBlNKnaXCTBh3XaynyqEeKTnySopfF4slFSR2Iv0PFE/YJCZyib5z8MerW9qRwu91hm01bl8/PSkHP7Axygp7FRwRyvhStm0NqZeGiBd8wNAU46NlerpaNX0N23UrRNdPcEQO7dDEDgKUOfNzTPJrafhrrlQ6IM+KZucJLID89n6oqb12oMauM/nnmlobaDetpnEtOtHpGlS5pV2utyiXZSltOxUTYPO+nIhHMAWZpDyW++JmvbPFaTI8U67JSupou9h1Ftq49bujK8XShiU4s+WBr0KBvbDoXrQfOYHoyRvjyZj1FcmW25sfV1lmRERDKdjszeETKuyziMo9X9Zo90jil5DTqxgVA/VxHKo/cJpy5DUjXPqrqtvJ3Y03d9opP5vZch7P2J X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 0102e53c-3ac2-406c-a02d-08d925d0622a X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:58.1850 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4yEB/AEbpofqiO8pvlH/iQVYfkZNGDALyvy93QmboTOuwwGJb1+WooLKZrbCjWEG+qkAwN2pugfY7fFZNxGtyw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The KVM_SEV_SNP_LAUNCH_FINISH finalize the cryptographic digest and stores it as the measurement of the guest at launch. While finalizing the launch flow, it also issues the LAUNCH_UPDATE command to encrypt the VMSA pages. Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 125 +++++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 13 ++++ 2 files changed, 138 insertions(+) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6b7c8287eada..856a6cf99a61 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -1741,6 +1741,111 @@ static int snp_launch_update(struct kvm *kvm, struct kvm_sev_cmd *argp) return ret; } +static int snp_launch_update_vmsa(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_update data = {}; + int i, ret; + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.page_type = SNP_PAGE_TYPE_VMSA; + + for (i = 0; i < kvm->created_vcpus; i++) { + struct vcpu_svm *svm = to_svm(kvm->vcpus[i]); + struct rmpupdate e = {}; + + /* Perform some pre-encryption checks against the VMSA */ + ret = sev_es_sync_vmsa(svm); + if (ret) + return ret; + + /* Transition the VMSA page to a firmware state. */ + e.assigned = 1; + e.immutable = 1; + e.asid = sev->asid; + e.gpa = -1; + e.pagesize = RMP_PG_SIZE_4K; + ret = rmpupdate(virt_to_page(svm->vmsa), &e); + if (ret) + return ret; + + /* Issue the SNP command to encrypt the VMSA */ + data.address = __sme_pa(svm->vmsa); + ret = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_LAUNCH_UPDATE, + &data, &argp->error); + if (ret) { + snp_page_reclaim(virt_to_page(svm->vmsa), RMP_PG_SIZE_4K); + return ret; + } + + svm->vcpu.arch.guest_state_protected = true; + } + + return 0; +} + +static int snp_launch_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) +{ + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; + struct sev_data_snp_launch_finish *data; + void *id_block = NULL, *id_auth = NULL; + struct kvm_sev_snp_launch_finish params; + int ret; + + if (!sev_snp_guest(kvm)) + return -ENOTTY; + + if (!sev->snp_context) + return -EINVAL; + + if (copy_from_user(¶ms, (void __user *)(uintptr_t)argp->data, sizeof(params))) + return -EFAULT; + + /* Measure all vCPUs using LAUNCH_UPDATE before we finalize the launch flow. */ + ret = snp_launch_update_vmsa(kvm, argp); + if (ret) + return ret; + + data = kzalloc(sizeof(*data), GFP_KERNEL_ACCOUNT); + if (!data) + return -ENOMEM; + + if (params.id_block_en) { + id_block = psp_copy_user_blob(params.id_block_uaddr, KVM_SEV_SNP_ID_BLOCK_SIZE); + if (IS_ERR(id_block)) { + ret = PTR_ERR(id_block); + goto e_free; + } + + data->id_block_en = 1; + data->id_block_paddr = __sme_pa(id_block); + } + + if (params.auth_key_en) { + id_auth = psp_copy_user_blob(params.id_auth_uaddr, KVM_SEV_SNP_ID_AUTH_SIZE); + if (IS_ERR(id_auth)) { + ret = PTR_ERR(id_auth); + goto e_free_id_block; + } + + data->auth_key_en = 1; + data->id_auth_paddr = __sme_pa(id_auth); + } + + data->gctx_paddr = __psp_pa(sev->snp_context); + ret = sev_issue_cmd(kvm, SEV_CMD_SNP_LAUNCH_FINISH, data, &argp->error); + + kfree(id_auth); + +e_free_id_block: + kfree(id_block); + +e_free: + kfree(data); + + return ret; +} + int svm_mem_enc_op(struct kvm *kvm, void __user *argp) { struct kvm_sev_cmd sev_cmd; @@ -1836,6 +1941,9 @@ int svm_mem_enc_op(struct kvm *kvm, void __user *argp) case KVM_SEV_SNP_LAUNCH_UPDATE: r = snp_launch_update(kvm, &sev_cmd); break; + case KVM_SEV_SNP_LAUNCH_FINISH: + r = snp_launch_finish(kvm, &sev_cmd); + break; default: r = -EINVAL; goto out; @@ -2324,8 +2432,25 @@ void sev_free_vcpu(struct kvm_vcpu *vcpu) if (vcpu->arch.guest_state_protected) sev_flush_guest_memory(svm, svm->vmsa, PAGE_SIZE); + + /* + * If its an SNP guest, then VMSA was added in the RMP entry as a guest owned page. + * Transition the page to hyperivosr state before releasing it back to the system. + */ + if (sev_snp_guest(vcpu->kvm)) { + struct rmpupdate e = {}; + int rc; + + rc = rmpupdate(virt_to_page(svm->vmsa), &e); + if (rc) { + pr_err("Failed to release SNP guest VMSA page (rc %d), leaking it\n", rc); + goto skip_vmsa_free; + } + } + __free_page(virt_to_page(svm->vmsa)); +skip_vmsa_free: if (svm->ghcb_sa_free) kfree(svm->ghcb_sa); } diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 8890d5a340be..8db12055b8b9 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1682,6 +1682,7 @@ enum sev_cmd_id { KVM_SEV_SNP_INIT = 255, KVM_SEV_SNP_LAUNCH_START, KVM_SEV_SNP_LAUNCH_UPDATE, + KVM_SEV_SNP_LAUNCH_FINISH, KVM_SEV_NR_MAX, }; @@ -1693,6 +1694,18 @@ struct kvm_sev_cmd { __u32 sev_fd; }; +#define KVM_SEV_SNP_ID_BLOCK_SIZE 96 +#define KVM_SEV_SNP_ID_AUTH_SIZE 4096 +#define KVM_SEV_SNP_FINISH_DATA_SIZE 32 + +struct kvm_sev_snp_launch_finish { + __u64 id_block_uaddr; + __u64 id_auth_uaddr; + __u8 id_block_en; + __u8 auth_key_en; + __u8 host_data[KVM_SEV_SNP_FINISH_DATA_SIZE]; +}; + struct kvm_sev_launch_start { __u32 handle; __u32 policy; From patchwork Wed Jun 2 14:10:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294741 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0D843C4708F for ; Wed, 2 Jun 2021 14:15:23 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id EEB9D613F6 for ; Wed, 2 Jun 2021 14:15:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231865AbhFBORE (ORCPT ); Wed, 2 Jun 2021 10:17:04 -0400 Received: from mail-dm6nam12on2058.outbound.protection.outlook.com ([40.107.243.58]:12928 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231994AbhFBOPD (ORCPT ); Wed, 2 Jun 2021 10:15:03 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=blHPFbEERjgERwZcxiIKtzeylxFTwhNZMt4dJ5L9yU59AKbTOmmI5rA1R/8hnhGPRwZWdNZ3BzTEboWlKQt+3tgr+rnqVWrnkcD2gWFVfhtGMQcblMuja+jSQiHKgEnIuUtm4wDLmpy49f+Dq9AHCvkxL/uvPhXCPZ3615wAytIHdxnk6oBut19gnp3b87X+Vhmvdlo1HInI9JhqHzBgzSGThhCi//FkSahu6lj2nYD7LFZEl4d/3goMmeHL0YjbQF5NRJ4385rpF2nEzrjXTVxBh+vMuVe8RRxge9rSsHpbnu3cPcC/YbqiPY5tJzxkeELFggvDfW0WOGRR1QfZbA== 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-SenderADCheck; bh=Ctf2ZME0dCzsd2GPR85K3/wNdSfar94NNCdQynI6vjY=; b=m6unJd3J8DuMx+RYNI1hm1xPu6pCGJAiytyvUkVkT5YaucLVSSOZjcujh0oZuCt+okgaRY5gciJuB2VPiZDNvWcogeEt31hugqARDVGP1hhzKNohQjNceOOMH/ZV5u+8quXdycu7Jr2Sz+FV5TqOJgfz8FskjthdUgxC8T1cVBtelEICisX4Q1A6FdUHFM4Omwoda/ix34m//iXlF5PYrKdpFUNFvU3UnWrA6Uu24m3dKrGZA72bvsMph6iSH83/Yi0YouvL49BA24UQU4Etmd6Ldg+K0NRhq6RPR1Q0Yn/TmjmuvOGkEqfQPUcDA4x9QwJ2gerkzjtlHkW7jhZZcA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=Ctf2ZME0dCzsd2GPR85K3/wNdSfar94NNCdQynI6vjY=; b=TR57PGWJmxjuEQ+Z01+FWtbwJoiZPEe8v6+o5Qrs9hX/0GeoFn4dSAHFe9MNgErBAHAjB+4SiEeCTDXAimf+Rrc9Qm2/Pb8d0FiY1J+f9aZOBNyX4zyiwtt0PSTrAfllueJiTI+xf/xb7OSKAeUgck6G7buwndtCPeQ4q34Ac4Y= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:11:59 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:11:59 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 24/37] KVM: X86: Add kvm_x86_ops to get the max page level for the TDP Date: Wed, 2 Jun 2021 09:10:44 -0500 Message-Id: <20210602141057.27107-25-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:58 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 02bc59a9-9ed3-4db4-44b7-08d925d062e7 X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: CJdz4BdUbH6aeN100rx81FQ86UFRd2LkMAbOJmY5FqErvJC10UoIy2x1IxwkZ/9yTdRptE9I315LhtxlS4v7pElQhh8SXmM4klZ59LXXXV0PyH63KFHMvitEBUA8R4YCQpclTx5B8G4qJFMQHX2OJDznyQeD2mBp8DAnCRJNb8cjtoEQvJbthhauxCVn86ox17ffVRd+Ymw/w5P71hK9a1AjWYOsRztVVJWhcTO/WfRFHrtO3Av6VzlQNMQVmLg0GdfMdjvjO/+biFX4GCZOK3jJdj0305pUtVjntwCfjDmFUW01etHltR1Cwfap6VYw/32zqYjSbsEH8OlcUizgTW6QMRRUIoy9s1daXhcHkAnMo/J6NMKJBXyVH4Mj8Lh9wliwOUptkbcSyjWWrIChI8vrhZblDEJ147vMbY9wg/zRdvPFMaQP9yAt+017MCjUAkJWOGFhdoujCDmAHLJ4b1sQS4opk3esapvq7TMJlfkKfAQfIcdzHw1KQ2935tZPgw7dnoJVayzfrQ6nO9TXQPAodYoLr8/1UDbi4TJEDJrNQa9QmAkh7jJskC+UAWjZ+YIhNxp5JC+TmMYG+f5QaQ0gYq3xixhzNGndyOgXB18RVHzCZYYjNbx5mbWNhiUfhQ0q9nv2DxktwbccfBbpXT2kCmOftnvm1kimNLfl0OI= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002)(15583001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 21Cq4EYlOJVA2gCTxwAQBtrMpQ62h2gIxuew0tnUhJmk9M8EBbsF7Imb/HWPZZNkp/0Tk36sJWS2+QMXLzqIFmaTdhdPROf1avEcUQZMDW/nmtfJrxvrd4H/mvotAGUarYd1lbo+J5F7fxhRgVpwCTOZESemajMy9AxvGFlnqreONJl//gxMLNy2DAOzfKq2qXjYHPDLPghMbtmxbeNht8hWhnwf3j7iyu9tumUPDi7SBR5dct+weBpm1hXT/b5J7UI9UBEYtM60njJquls349N0jlSwp7FByWo5/iD0YpiEhDp9BZMp0tKpXQ27ukZb6PObpqVW2NiQ22PZ/52azQ2zEyVeW+V3psUnNw8Z4aaYRKt/Qp06QeMyxcEN4XPDmO8vSkXX+Hdj9DiyV89R59qXgORp88buhySCL+AqAxGST+lQ2bId6ts1Rf7NDQVKDRcCy5E4SUKRQOWenrnSC+ua7x/40mB66SChHAnWJ4F1lZ3GvOm/UVD3CXfkTrv7sMvle8pn/PyF/nlY+4zFJdgzjUm44Pz68O7LnwAhRKI/iDxmYGGeFY+sLIG0y0pSor1oopRbrMSq52VJwtnV+joNoIH/sL9GBtfpmLBb2eWOxZo6hfydUlByL8ac3ytrcweSMGPOK5qZmwzmTOL2XUY5HOw3Gtm4pmR83kDHLX00NSyAMLS1HqIqtPOz7duL6VDiGvhk2Z74WurHwtz8tGn1f95iWGkUWFfcJq6+P9B8mnRSrv3nAZvpiOjKNkHH3y9hxd190UK4qCDOaKxrNyNNg4aMlWv4eKBoo49lImjKiA3zQg+zDh84YUAxMyrJ2SqAjkd6P4VSGMKwHk6gW2Xa7i6y6SzI3w9Vg0d/Er0b0mqJrGaLDdut79BTJkwfnFT08FyjoGbK6ahdK8AGTXRhaiAhR3EWUn7DBro7ZViLiMxe3GQ3jfpp5UTLzolUPwLaIO0UVrVE9omzrt6Yj1O+IZOlBD3u0owqDL/17ABNvMBkKv7bjfrpAg9pYaqorR8zmUPhWt2j3zlhXYEdpkLuJXBdP8wYJdUADh+IEe6dMSVh1AXgat3ng8ARkYMrNrDhvFFoUldHZhrSP8TUrD8GoF2l6p1etcEN3vNTSiVRupBJqshhfsy1eYundg7kPnWmQWojCihLXfRx5xwx6ZlUfFsWtnip5/VYjhQxKlSfgVHnlf109ixoXPheN3/RjXYuhac+BzHHHH5ZEM0ed3ZmEyF/5joIJVMiLhGxetPBMAAbGHB7kO7E9sRRQOZDrciTEYHQ16XuTzZgutVaq+lkYkCQRtLQbmC8BPaBesIuObPF7I2/PogEqvoKTQql X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 02bc59a9-9ed3-4db4-44b7-08d925d062e7 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:11:59.4053 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: jp0j+92dG90ws8sU/tQ4rCz7uv+dikIfte4bVarrfJeiwfv9psP6stmGQ4nFbRxeGaR/ztFyCtgf6E2NZTI0Aw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When running an SEV-SNP VM, the sPA used to index the RMP entry is obtained through the TDP translation (gva->gpa->spa). The TDP page level is checked against the page level programmed in the RMP entry. If the page level does not match, then it will cause a nested page fault with the RMP bit set to indicate the RMP violation. To keep the TDP and RMP page level's in sync, the KVM fault handle kvm_handle_page_fault() will call get_tdp_max_page_level() to get the maximum allowed page level so that it can limit the TDP level. In the case of SEV-SNP guest, the get_tdp_max_page_level() will consult the RMP table to compute the maximum allowed page level for a given GPA. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/mmu/mmu.c | 6 ++++-- arch/x86/kvm/svm/sev.c | 20 ++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 1 + arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/vmx/vmx.c | 8 ++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 188110ab2c02..cd2e19e1d323 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1384,6 +1384,7 @@ struct kvm_x86_ops { void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); + int (*get_tdp_max_page_level)(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 0144c40d09c7..7991ffae7b31 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3781,11 +3781,13 @@ static int direct_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, static int nonpaging_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault) { + int max_level = kvm_x86_ops.get_tdp_max_page_level(vcpu, gpa, PG_LEVEL_2M); + pgprintk("%s: gva %lx error %x\n", __func__, gpa, error_code); /* This path builds a PAE pagetable, we can map 2mb pages at maximum. */ return direct_page_fault(vcpu, gpa & PAGE_MASK, error_code, prefault, - PG_LEVEL_2M, false); + max_level, false); } int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code, @@ -3826,7 +3828,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, { int max_level; - for (max_level = KVM_MAX_HUGEPAGE_LEVEL; + for (max_level = kvm_x86_ops.get_tdp_max_page_level(vcpu, gpa, KVM_MAX_HUGEPAGE_LEVEL); max_level > PG_LEVEL_4K; max_level--) { int page_num = KVM_PAGES_PER_HPAGE(max_level); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 856a6cf99a61..6b0c230c5f37 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3184,3 +3184,23 @@ struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu) return pfn_to_page(pfn); } + +int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level) +{ + struct rmpentry *e; + kvm_pfn_t pfn; + int level; + + if (!sev_snp_guest(vcpu->kvm)) + return max_level; + + pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); + if (is_error_noslot_pfn(pfn)) + return max_level; + + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &level); + if (unlikely(!e)) + return max_level; + + return min_t(uint32_t, level, max_level); +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index a7adf6ca1713..2632eae52aa3 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4576,6 +4576,7 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .vcpu_deliver_sipi_vector = svm_vcpu_deliver_sipi_vector, .alloc_apic_backing_page = svm_alloc_apic_backing_page, + .get_tdp_max_page_level = sev_get_tdp_max_page_level, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index bc5582b44356..32abcbd774d0 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -568,6 +568,7 @@ void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu); void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); +int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); /* vmenter.S */ diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 4bceb5ca3a89..fbc9034edf16 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7612,6 +7612,12 @@ static bool vmx_check_apicv_inhibit_reasons(ulong bit) return supported & BIT(bit); } + +static int vmx_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level) +{ + return max_level; +} + static struct kvm_x86_ops vmx_x86_ops __initdata = { .hardware_unsetup = hardware_unsetup, @@ -7742,6 +7748,8 @@ static struct kvm_x86_ops vmx_x86_ops __initdata = { .complete_emulated_msr = kvm_complete_insn_gp, .vcpu_deliver_sipi_vector = kvm_vcpu_deliver_sipi_vector, + + .get_tdp_max_page_level = vmx_get_tdp_max_page_level, }; static __init void vmx_setup_user_return_msrs(void) From patchwork Wed Jun 2 14:10:45 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294743 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 598E2C47092 for ; Wed, 2 Jun 2021 14:15:29 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4568661242 for ; Wed, 2 Jun 2021 14:15:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232027AbhFBORK (ORCPT ); Wed, 2 Jun 2021 10:17:10 -0400 Received: from mail-dm6nam12on2047.outbound.protection.outlook.com ([40.107.243.47]:30304 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232025AbhFBOPJ (ORCPT ); Wed, 2 Jun 2021 10:15:09 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Art7wraoDaqeAqMgiqBcDNlWm14HnU+hoE7DGcAmGlBA4Ff1ZFSH4NtlvgZSlOm2StJHtN9oNyzH4mIZ3rhdnkMdXYaeepTkl7QkswW69Whe7cpDYEkEoLdm6PMpDXWLABTspZA5bPmqLhXzV48xqnVttOlmeYzw2TRIqD0niI8PAJwHXNeAYd2rLHm/V1Y7ojiVGmVMk7BjEzk/7kKTXjtjNRgB2EAmiFlMg3drs4BBiZfb7HB6xGhMzp/kU57FzKOZbHBP+VAyZCUb/NODMZ6ye5lkGZFZDGSLjTAkM2K17U7Clzy3cL1VSOfaSnYSqkFlaylxmwNtfl94ST/85w== 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-SenderADCheck; bh=v5fkC3xFRR9pL5wKKPvCx+W+UqhkeLVogvMDzAl06Y4=; b=NTpPqR7xcS5REFAIAASFCcftPuKGGTDOEzookQjtxAMEl19KCsGRNCiStceKc1woK494979+8oOlStut4z+hEZi4y6298vMZj1R3Qrpt1MxbJ4thjVy6gQ6RkWaie+nXaxSODgFEfJXUAenTSdFAjM6W+VyaZ/W32gl28vI09xBdjn60rD7387r9N0jcCJPcTkhq/MtbeXr+Xv1V5WwYgrPLQ9fABSaGBwnoaq3YP+M7IKnCTv1Jb/aZqHouSUT0g6/tTJXo2yriIPtZvjVPhZu/dErsu9ZiK+W9WXrXp1HiQAVRg2IkFDw0WSgDq6JiIaL1gyincfZYiUq2nhsP7A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=v5fkC3xFRR9pL5wKKPvCx+W+UqhkeLVogvMDzAl06Y4=; b=izPf25QUEskrCDiP4GRL6CTU5PgSbJwW1QP4H93m2NxHRHftn8FIfXHF/aGr8w0q498EAOgRqWvZUB0ym9YxjBFO4CSxn2yvePcFadNB/nVFX7GjixBVdeXNL5/dTo4SZQ/NM4F6CL1IHfKFgmthcXoyl+DQZ8gbNqxC3kXtK6E= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:12:01 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:01 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 25/37] KVM: X86: Introduce kvm_mmu_map_tdp_page() for use by SEV Date: Wed, 2 Jun 2021 09:10:45 -0500 Message-Id: <20210602141057.27107-26-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:11:59 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3d5f1e87-fa01-400d-0055-08d925d06391 X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8273; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EJRSFYg09H95Dzg/B9PnbGw7fD0db1V98XtuCo79Sw5a4uhfzROMRqYI46d5WJzrP6v05RyKYK8yfm+GoHiRb/DgbGQHytsNSA2La26R7J0v+4YFt4ZrRzFC0FAjj19KTHb+CmW0/k1EdnaME9jprlqoutiXEw14vGRpfor0ohHGUEYvqqCHFRzSF2cE+KH/pEGZsD/+h4dYUbcxg+WWWY1tLgKeguKfYpHK7cmWMjtsFzOhIAg8vMDz8mc8rhgxM0dY7g9nFlvrWmn/5jQVi9gHp++abeahbqCwfIuySG5UI9ZJq6mPDkbel3ss48Mzulk26tHstMJokf5RyQQQuBHyJbYHchQcjT9JXviOfoUy+UV/400KmjcS8Ta6tSG0VLMbTZrQob/WZafmbOB0njjlASc3cVvB4bR7v41EzlVud+N6NU/ms02thn3lxId9vVFc7JH1BX+KQZTmhmpI3fpL7BiytxYeYe5WiTwIDQSYDWvAWSVIFOm5WnDI4n1CKkUxXDLHP0/aAgs2o6ucTNapaJU7hYTtcB6PlOKtW1Beih/owJXGnV05HYNhvAiN/8k1LQogx89AI22RLuiWzpyu0NNWTP2puSN1h7xtc09HR6a8z3PQskyreCgqPRGH6uVT1aG8fEL6x1FzPbtaLQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: adJ5PMZfXgw5n1+KQdL7qyk+dCbssm1N3BreEqOi3DFCHQFeN9HGyji15AjvEo4DAgMAJ4SJBjdj3+fsA3dMRgesRAQcXVDx8bixtuqjCLrNOeR/ji1bT46Fvp1F5AgKSL2ydzD4hlw9MfAeErMAABKrPrCAWEVUm2mSbCnwR+v+Ax7CHVMETwBngsQthuzG8xDVRu9D9WidPW8aQ+3bDaa6YLYSv6qkt1xnRbUsNBxNktN2uPfY+Paxdvl1pHWxIpCBdoRIq6cSbdgaVMNgjGN7I2XVhdpPeKY7z/zJvYXZ7PYsZwuyH00FgYW6f6nDl0IVLpD9pIGjxIVDChywK1Tle3W+R8eQRBcR07trPMFcuIO8l9Pvs+mjOd0JswqKrZJVjd2caYwxCOY6S3RvvH624isfJnOxIWRA9uwNStdJPtomAXdK1The1rFyePbxAXaJqCRgQ0gNWLil0XLtRcnM4rzktZOXgO6eSdrL+uaEqZYgSEnV6kANL9PFV1u87COQoqwd5V4C0qAXco2MsP2Zo+jybgGKCdzKnoipBvUGEYFeSuQcjN4gvK1KFIDJNSwcyfQi9xXUX7qUaq0Y2EobfVx6TsUveWpTf2kiP847xfQShePYLJfUWkLgpmOUW0Cc/ZnP7xalQCmeHcT3A0U7y2n9XYQ9bTWGgl09mjuYmyBxSGZuhVM3RZ/7ty1WMkvFaYmJRufvuJQZxbtOYZczk/i5H7sZuG94BGB1BluujCQEIevEBdUHu+mkt9Tt9egKcv28070mG32IykVZ/GXmVQjnF9/AkFDqCqNJCifyBTrZ/b3Z5tiTBUFzfv7pkYytmEk/sYcrAhFPfewNvFhvjARTibh9Xi4XJfgESY7n+nFB3o+ZibBIJDVmUbMng2oh+SlApqLWDkgJQWhMmOdu3HIkVxBf53F7ePcyE33x+7h/TDfraFsKn+oLIOTsUytaL+7QliHJO3B9lOwrUWqkmkOfA7DA+f1T+70V9nQO5QAzH3hyytb2OObAPtarNbIpxDtOjBGyn8HbcG9BvccnzXW0cL5XFLGucDcMFjTVeTELOW0eLU6dT/2n0r/Odm+oRMMD45r8cCaZ0rL5hWBiKCUJxtq9iJQJ6IOB6EHPVFyRA8+zmLfTZEs8rPALONzOvmcJASNc1M/8dzmmBbGI50lpshODc4e2HwA+RHcFTl9WujfLkXPgMQO+932v9a+DqgElLAlk8LBGSuCTq3uzkepHuY/kC6WHgdKt4Lc8ztj+pzJKh1vAXq6fUfwU75phA/VBGDnTI1oZlulafomYga/H5I4KslsvQh6WVElHq5rPmJ5oOt1w/xJUbDke X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3d5f1e87-fa01-400d-0055-08d925d06391 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:00.7286 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /u1gjgtONsnUEdEe96xsGtiZ5n0q63g8/1r9vc6iY3vrZWnp1JUhmZnOHRqdMNoWfu4zDgOCB/wKtm3lwht8WQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Introduce a helper to directly fault-in a TDP page without going through the full page fault path. This allows SEV-SNP to build the netsted page table while handling the page state change VMGEXIT. A guest may issue a page state change VMGEXIT before accessing the page. Create a fault so that VMGEXIT handler can get the TDP page level and keep the TDP and RMP page level in sync. Signed-off-by: Brijesh Singh --- arch/x86/kvm/mmu.h | 2 ++ arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 88d0ed5225a4..005ce139c97d 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -114,6 +114,8 @@ static inline void kvm_mmu_load_pgd(struct kvm_vcpu *vcpu) int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault); +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level); + static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err, bool prefault) { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 7991ffae7b31..df8923fb664f 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3842,6 +3842,26 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, max_level, true); } +int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level) +{ + int r; + + /* + * Loop on the page fault path to handle the case where an mmu_notifier + * invalidation triggers RET_PF_RETRY. In the normal page fault path, + * KVM needs to resume the guest in case the invalidation changed any + * of the page fault properties, i.e. the gpa or error code. For this + * path, the gpa and error code are fixed by the caller, and the caller + * expects failure if and only if the page fault can't be fixed. + */ + do { + r = direct_page_fault(vcpu, gpa, error_code, false, max_level, true); + } while (r == RET_PF_RETRY); + + return r; +} +EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); + static void nonpaging_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context) { From patchwork Wed Jun 2 14:10:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294751 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60807C4708F for ; Wed, 2 Jun 2021 14:16:03 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 4D9B5613EE for ; Wed, 2 Jun 2021 14:16:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231157AbhFBORp (ORCPT ); Wed, 2 Jun 2021 10:17:45 -0400 Received: from mail-dm6nam12on2070.outbound.protection.outlook.com ([40.107.243.70]:49033 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231694AbhFBOPn (ORCPT ); Wed, 2 Jun 2021 10:15:43 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Vhj7V1Yerck7avwwPVslEbgGGVx9J4DmaVfeo3LTPL/ai5nbcvzx+XqKFhYZ+Q8HQ/012RYRu96/nSTl7Do46pURi4VPu1/YhN4MAeac9uqVu8687FXY6GNN/1lEq9DULwcHTqRv1nJ/6QeGRTryCXNj7+aEjmqUzC/vc/RZglWSM8vVZkwgmdD8knXOVTJcNXpk2uI78G1pYAcsjPHbAgcHJvQkklt6Zw80wtHlJxD/ilf6OU+Bg2OiF+/RtbS69/V5DY7tOgMinr5SK9huaCYxTDQYW6vm53xJmZ1viQWX/NDZAhAULJ4AwmJ1k5c1VIxRZWElIbe4weS+NmR/dw== 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-SenderADCheck; bh=7T/72aP4MqlEHsa4nqhnFTAbPsOy6kaWC23U4dSfW6o=; b=khMH1nsdwIxR6ua8yCMK7rNHC0wDzhhrWHrZQeYQLGvuX0Tk8pe9F/7b0brpepdAMMbbmrgUCzCcHU6dsauH6TiMB5/45yeSBYV8+35lFq5G29v+4Y0Pzzq/zLWSHIvoUGQs4dpLypD1poFOi3HU2+z4aXSFfyVkP/kJ+EaAtytQOets6piO1jmHNnwCJb2SSQs3BrsqgnLXEXdg8Pfq5xW+mJj9y9fakhlLRjp9ckAbVcQgXhKlNzUVHN/4svm0snZw16poiaZyB9LCkUG7ogzHfBdblimo93Fwga5eJ6A+uUrfzl1VrbRjU7zQMxozKZLqtRc74V4ubqspQVWCtA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=7T/72aP4MqlEHsa4nqhnFTAbPsOy6kaWC23U4dSfW6o=; b=c11byFiTTDiDICuLKYDqLGwyP0Tu5zWnF6TnMeHXjq8ogQl4iJXQf4Qr6m5g9e5UNAzTr+oQ9uTIuMV4+RpTF10ohsnGW9iZksaJnYlgasZFWuGJAta2K2nVfxuGy6yvLVZ1Y+9n4/V63M/C2bJawxt6lajuBu0egHfeEG/UIxQ= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:12:02 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:02 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 26/37] KVM: X86: Introduce kvm_mmu_get_tdp_walk() for SEV-SNP use Date: Wed, 2 Jun 2021 09:10:46 -0500 Message-Id: <20210602141057.27107-27-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:01 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8ee46e39-6774-47e4-c8bf-08d925d0647a X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 6MdempsK8iG04LR1WMt6mF13uvVQpoyrtUM1NENiE3o9o9bphpHL58afDaczoow0cngBSJXCv8hSP0zWkGsj+SXLr3ZJrapcxU83W3F91pYO34BT0fgdPeBLfGrkLvmMNEaUNKQAMyabjUkAzWK5ympOiDhZvHBeMx8VIk891PIaRPwJSCjqhS0kk5RVefxSluN1zoalUIPJw9o3hYebDM0PoUtZOlsytfwPkFkpymYeeIH+/G0MMotb0gyVR21Vk/cCHMLWQolGx0NhEvAYVfFyNO7r9iNkrN3XHqRRKdcetFT65CO6MoyMzaO6TY0S8TSf0P2jj4G/M1SiSkRqBmrjbJjcwoiaUgRO31D/GE/+xFS7qjdEuQY2YzgP47QodaeOAPqVa5+rSAqZ1vgmb/78Y7zjfRSCt7e/2H9mw1DjykMeW/7kbJ6Z6smp9Rc0n2e9cN3KQy04L0XumJKzB2+jJEoBr/+A8ediSTg96Z41WmDUNlKwK5KN+idvoES4zbi4rwwDdfUSXrl6U3eal6jW3zjO8mBlCZNupN5dX/qV16hDmIAnUhdEDYohpp4BgPqq7Ihnvngj45w6X0lD5StTS9l/K4GbfLGbWm0wXqwJkkCURq2d7HrfMxcGOwPyDX6VMs4T8by+rHvXdgRhGw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: j0oCJpLRKJijMMoQ/ktEIWazTxAnfn63y23AYNuY+4qbLaZBG80raoq8lgSBjMyH8u0jWaRfhoEjxrOckaXIG0ATZ955o8gUmoiY3b3AOcUMo1zuNk2rn+5XF7BFg1r3xVMgckSV0VFErUw4NZ1OIEC6JPCFzOYZcFTMP3I8d90IZMA6qAgkzVOU+3MfwNnWyUJAov6vDkMjgeNm1l1NTiFaDCmvsJk58uLZwkKeVe5ijic91nD5Wxsy9pVZAPiUqqpEQ0nGEJhn/Lgg4NxZoxs8e6oxMkycxLhKEc6Qg7SwynwoqEF5n1qx5gvZdgzPDOgNuxC93FFnza6SeoWH7H9V2mZj5x23dfXFYb7paToeWPJRgby8peV9nVncWV3gvG2iBVfrEUSeVclBRduTFlPn1rBIA3FoZa91GRIqH331Ij0t3gGRzxBAT1rgszSzIRyOqbyZNgoq7lZQm73KPEFaUhIl1Hxc+/RVdbFVPG2DYhdfmrNzex5XD6jpv5/Ye1jLiAMWV50iE1gJcO7JUqcSkaH9UyiYpqgWzYkJ1PkPrRhrSsC/4syNtoaS1LnnNCaTiywzG1TtV4GT4V1vivtNzooTKaU1qvRwqfXAWCzU8dxQtW5/PW5Wgwef64M7Awqhft3MtM+/CzCJuQ1s5X2hllqNvFcmetHK3qJiMU9+1f9Wi+y4Tp+mlHEBkJFgib8cPa6uwaYzSjjjim6g8V+SBlyfY5rXgxmSM3n+O2fOh5mII0WPsimj4Q6NyfQh82+3pEg3kJdp9x3Ofn2O9X3Z59kZup54fgdgrQx/VFnYoIn7qgDjiyOUVdS4Fye7sldR48fHlPJM/yLToEg5eKT+3mfTtjAtXGzeGFjXP/X3eBuunD/rildia3HrlQ2/gKMI6L+DGBc4xQ06QwAGvijGlJID+3HUqAvjhGWfEWjdz1Cy+GIw0fkfmNgFo5pUzl2uh+GkOVZe7pQ6ZFx8Ae9AQo9PG67W2JnaTUp/5fnYFQMucUFnzs4VYEaZwzVzj3T/VWRfs4PyAub2fbTOyuOpt2I4BftEnWq2lY7ER01kxzaFNDkDd5aoutiJcMGClnxib9mGwNMWBjwTt3fxPOpytgY/nKREY7x366AtDeH3hYdkTmq9LLKV4037/Xd0r7RqKorMzCcANkVkheWXPOgEY7cTqWz/fB7fp2mPQa56MkU5JLem9Rz1e1SpeaStCTnYWDPO8SF8iHn/9Yse5G4uIxfrAVIJ3vKMGx1zZvKWaN672F12Aa/8B3knRiN7AUIJ0kKxLAesRlEeMdmnQtmvJRAsgGMUSUvLJWV0fR74EIrOZTX097wEw+Topl/z X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8ee46e39-6774-47e4-c8bf-08d925d0647a X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:02.2947 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: EmmnNDUwYLF6qydD+UsGYbcLeT7rvGpcon2SOilhBd6IRgA6uG3dG46FybZ8W0xGfE50AiJh3+A7HvPXDmNhqQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The SEV-SNP VMs may call the page state change VMGEXIT to add the GPA as private or shared in the RMP table. The page state change VMGEXIT will contain the RMP page level to be used in the RMP entry. If the page level between the TDP and RMP does not match then, it will result in nested-page-fault (RMP violation). The SEV-SNP VMGEXIT handler will use the kvm_mmu_get_tdp_walk() to get the current page-level in the TDP for the given GPA and calculate a workable page level. If a GPA is mapped as a 4K-page in the TDP, but the guest requested to add the GPA as a 2M in the RMP entry then the 2M request will be broken into 4K-pages to keep the RMP and TDP page-levels in sync. Signed-off-by: Brijesh Singh --- arch/x86/kvm/mmu.h | 1 + arch/x86/kvm/mmu/mmu.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 005ce139c97d..147e76ab1536 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -115,6 +115,7 @@ int kvm_tdp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, bool prefault); int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int max_level); +bool kvm_mmu_get_tdp_walk(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t *pfn, int *level); static inline int kvm_mmu_do_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err, bool prefault) diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index df8923fb664f..4abc0dc49d55 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -3862,6 +3862,35 @@ int kvm_mmu_map_tdp_page(struct kvm_vcpu *vcpu, gpa_t gpa, u32 error_code, int m } EXPORT_SYMBOL_GPL(kvm_mmu_map_tdp_page); +bool kvm_mmu_get_tdp_walk(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t *pfn, int *level) +{ + u64 sptes[PT64_ROOT_MAX_LEVEL + 1]; + int leaf, root; + + if (is_tdp_mmu_root(vcpu->kvm, vcpu->arch.mmu->root_hpa)) + leaf = kvm_tdp_mmu_get_walk(vcpu, gpa, sptes, &root); + else + leaf = get_walk(vcpu, gpa, sptes, &root); + + if (unlikely(leaf < 0)) + return false; + + /* Check if the leaf SPTE is present */ + if (!is_shadow_present_pte(sptes[leaf])) + return false; + + *pfn = spte_to_pfn(sptes[leaf]); + if (leaf > PG_LEVEL_4K) { + u64 page_mask = KVM_PAGES_PER_HPAGE(leaf) - KVM_PAGES_PER_HPAGE(leaf - 1); + *pfn |= (gpa_to_gfn(gpa) & page_mask); + } + + *level = leaf; + + return true; +} +EXPORT_SYMBOL_GPL(kvm_mmu_get_tdp_walk); + static void nonpaging_init_context(struct kvm_vcpu *vcpu, struct kvm_mmu *context) { From patchwork Wed Jun 2 14:10:47 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294757 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C1D7C47096 for ; Wed, 2 Jun 2021 14:16:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0F974613EE for ; Wed, 2 Jun 2021 14:16:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231694AbhFBORz (ORCPT ); Wed, 2 Jun 2021 10:17:55 -0400 Received: from mail-dm6nam10on2044.outbound.protection.outlook.com ([40.107.93.44]:27560 "EHLO NAM10-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232143AbhFBOPw (ORCPT ); Wed, 2 Jun 2021 10:15:52 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VPdqkmGf8Bv6R2gQPjtFOzawv6nzj9cOxKg5uMWJoQyXKDVhdy6ZDSq7KWVwk8zXEr1Slm5uOkTpMgkabYd7LB2rHfDEEF74bJ1kYVJikQUfd7dX/ymipiEnfu61RjLEdPIqiDhiL9eR0I2won6537OwlOGTZUcKt91RiIeD/wiHmhSYPI3VhNrVcIytJqfaagFZz4DeyfuPk91MNaqW5wuKZu2j4BSGRptN9ywExCa9pJOBkO4v65kyKxGa5skh0rh+I2080HfJvdGiDX1oDMo8cgNeRpKKs8Qmw0E3rbtw8LyLkWtEK3AQl4dnZVkfDzQw8NG3w3TLAWfmMwR7vg== 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-SenderADCheck; bh=zuQ6jECD770ybRkBmH8s+erhZW7p02EbJr+4zvD24f0=; b=JHHAdHokNMFPCUrxXryCDj1I15RmKV+E0TgOQf2ox0vz83dJhQLV/ZgyJasPetlrsjgFoYQpcvN9ct0TGqx0AjDCNxr+EyH46Kd+l51SWoAv6Fo6vBMztA9Yb3vcYjxdepPfF/7LruPy7rpXwaVuh3MBiBq9945wsRZPN2KM9P7t3o8/uwIQhjUC1JeTKf1nHmj30inNnl1nP21lsBjBF8d2PwXHpDNBFPAY4E8mxD3eQG7n6mj1Rm+LWXshgqtyuKaBEtBulitMwExR/b6LYq3qQbY1bn+h46LaArTuV/C18fQ96ifIEPNQqKGQAkPjsuGU6ZSIzddUKeoShKrqMA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=zuQ6jECD770ybRkBmH8s+erhZW7p02EbJr+4zvD24f0=; b=VYVq/0NvSi0FF1Jf6xl1hotB9jfCpf+9JnBXnGS1hYc+rouWaDfQIN/e1mq5tA3nmot+oStQSlZNXAXC2+nnpir3Zll5+V9xjaWEhOUngCom3/PxNG+7n0M2x1sTRfkbPv7MzQAtmWHelXlEM8i72sUqqJFZjAfTlP8OcwL3FWU= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:12:03 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:03 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 27/37] KVM: X86: Define new RMP check related #NPF error bits Date: Wed, 2 Jun 2021 09:10:47 -0500 Message-Id: <20210602141057.27107-28-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:02 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 909b0bb4-8862-4a60-e981-08d925d0654e X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3513; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: JLSwBGon0GZqzEXT9VEkPA6MqHMnvRyHbOm7Abgt/r6h48MPmwj8601n2Q/GezcEF7Ze1g43HPCJhV8xeSLPBPjy2/+0m+36qP4OKonUAABBTMsMsMaRg/HRFvTg/WxsOtf2UtWU/e1oRCcfg7CKq+0JuCIil+Lik/xT7Wfm3ALEzbsWEyAgra9yHyg4X62E+d9NxEjAg1BtyiK92hVcPCcKvDnLuG9fw1s0mxltN7t0lkBVUhjsPVVpRSmb0Nm5xOt9jM3m0nJHvPpdbGtTDsrmssAXo759zaVHcyjw1RPlAT24Igu+QCYuEIh2FeTW/3BpFqFyM1nThrrTgn0Bj+8VM7LUfs4DA0D8rRHegcyRYB8hDOtvWrOQGkQiu5UvyIHfaGkLqOv3RYq6pNYK6950mX3uBVrrvFGiS6qkrzLJchvKzLpURvoZu8UhoR8pvHU0y936YrWVZ6vNWZzikQo4xMhpae7C6p8d6f1ZW+hzc3fIz6SplM3d7X5JkAi91i3E5VCyOAhJcIMF19kozMAqIvwhf4zuRjCzjZfCyHjnE5OLfvyUkVqop86syC5v/xIhTicw7m45QiKDfsnQK+g6qlnmgUaNjdbbO1loR0gIVMvWosFhoPSKy2Vl0edlt2h//gdIv53vphvaFcSmcQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: k5CMy81fzO4vzgRZ6B1aRtVer7W+71KfcB6wNY5CDuQ2rFaU0wTVWDiBEiSDuVRzr3xFkM0u2nS56221TWZ5LLzyG1CO1Siuqpd+cPSJHSfZTxm5Y0Z6Th2Kp+4mgQaPJXpeg8MFPmD5y6w/I4LaAN/wrranrNxvUBJKzEUvFGl/a/yaPe2uXYFwjMhj7lj1+Mgl9+ojNv93SBlJZl09pdgqvVz4zsbE2T9e4MsTtQUGYfda5K935vUMkyNmiOxEoiZAXSc4nPtHDK6/8DHizddLQ91lIvJpM0RVCG2tJKRyzOxqO+wUvogTHyRUN2kNuBZDR+j7DAZFPUtJLmyn260r/eDtWzXPoEnGidHndfwbRFQF/Ch999M/CJDgmYKDy5IQicLsRf5x7CzVYA/MiskAqQWvFXCU9u7uGBb9pUi9YY/jxi5b6UttyLKM/gxVRQ9QBbo8AZ3R0n2U3M3ukmozVmDlL9ige3+SoY4yD8h6G8xIUVHaF+ZgIbn5deQy7mtWNuypuxmiS6LjG5k7yy2s093mZ0Dckj/DXtp9uKnWmD7HnlthCXUlgq5L0JENPHIBtmrlfwqmb/ZiXY/UpfQxAtQta2MLX/nBg5uX4sVDfFOzfKNRmnlOPPwCyJArXCalrFbDqpphNUKm9s9u2jeDv/YLHIfd/dLST/dy/4u8SPAI+r6QMAYOvKidQ8V2W2qQIGitu8M5uZXfKVguDqoXgN9T9v6dHC92bFpL7IPdRUm5fU91GNoYSs1InWih2HNT71Zo16GIOkHeftBYw0+JV8zhUxpGHzkvYKsDbHpkzFMAfqXHa7TPup/9urZbrYYwyT8tHtU6VyjnQn6dCzGkjggw25yeCI773UB6/2Y8ICNB4fVQFo/xTwlgvmzAjvYHxIOIpNPg6dXytrAzXOiO5qj+zVXjJKY+X2trOQx/kK5GtgXcKNL1YwM79hTuycNGTssGZYfqbWBCA/ooxZeM25gvG0O9dtPjqjRIx/eD8khTpLHWZlCp8rsdoF4V5TpkdtVNR70w/YdrkJeaeJcEY1dhe1hq094AMX7uvJUd6f8J2wdgEBEVL1DF1yO4mMpqbi18OQS5pItCEo8pZvQ0ITtmJ2Ce6aqRmbksd1954h7bO9MQX0Br7jbeLO0tvsbjDOe8JrsEqVGMTO3t4RmnVb3PxuAKCikI/YQN8BdNU2f87Mwe6t7vXptzOLd6PZD2Y/fIkuc84cOA+ITf5CVpThBXdSiDGj0ZCAASj+eve3u9ArNnjkyeTuZdizOX3aCEI4YJmoGm6aAJ9Za3oEt8AkMhDQGwJp0L/F3FympGaVps1Q6s/zDvfrLNCjS4 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 909b0bb4-8862-4a60-e981-08d925d0654e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:03.5519 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: +2EnkUdcbBZc9Pr94GXBMBJwbvkbHO6dGSm4T86jgcoBNBt74cGoKMaat1cHIl/xKymbI9NS/yXBH424Yvbhzg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org When SEV-SNP is enabled globally, the hardware places restrictions on all memory accesses based on the RMP entry, whether the hyperviso or a VM, performs the accesses. When hardware encounters an RMP access violation during a guest access, it will cause a #VMEXIT(NPF). See APM2 section 16.36.10 for more details. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index cd2e19e1d323..59185b6bc82a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -239,8 +239,12 @@ enum x86_intercept_stage; #define PFERR_FETCH_BIT 4 #define PFERR_PK_BIT 5 #define PFERR_SGX_BIT 15 +#define PFERR_GUEST_RMP_BIT 31 #define PFERR_GUEST_FINAL_BIT 32 #define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_GUEST_ENC_BIT 34 +#define PFERR_GUEST_SIZEM_BIT 35 +#define PFERR_GUEST_VMPL_BIT 36 #define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT) #define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT) @@ -251,6 +255,10 @@ enum x86_intercept_stage; #define PFERR_SGX_MASK (1U << PFERR_SGX_BIT) #define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT) #define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) +#define PFERR_GUEST_RMP_MASK (1ULL << PFERR_GUEST_RMP_BIT) +#define PFERR_GUEST_ENC_MASK (1ULL << PFERR_GUEST_ENC_BIT) +#define PFERR_GUEST_SIZEM_MASK (1ULL << PFERR_GUEST_SIZEM_BIT) +#define PFERR_GUEST_VMPL_MASK (1ULL << PFERR_GUEST_VMPL_BIT) #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ From patchwork Wed Jun 2 14:10:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294765 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id F2DE8C47093 for ; Wed, 2 Jun 2021 14:16:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DEA3D6108D for ; Wed, 2 Jun 2021 14:16:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232259AbhFBOSY (ORCPT ); Wed, 2 Jun 2021 10:18:24 -0400 Received: from mail-dm6nam12on2061.outbound.protection.outlook.com ([40.107.243.61]:58433 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230257AbhFBOQX (ORCPT ); Wed, 2 Jun 2021 10:16:23 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=EfONGEfo6OsqRI3NGRKZJhQsS1HU5UYZ8Ji7ek6CXkQ0Bts3NPtzDFjkGuBFYXqGMlM0tE6k+yKqEQUy5D+oeIs85EXo9vSO0DRlXoSgH30DmTx7Xhct+SvxITZMwO98u0cpyQwodMs2tkUaWw918zJyiXm7BP5JnvCxFk2+ZEpIuBNd/jH08wG1fUyt0Y27gwCqw/h0jLjfyt04XNyxnIUnBlvFYFmtGmTKaTMa+PWv0KFde1pI8o5uZ3z7d2GPkxxwlUs538++l5Yfj3irbTOj75P3NzymavMbvDkDtAzfjiXi/NvRkXGbp3qURzFg7Rdv4Az9xsT2ni59bZdo+g== 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-SenderADCheck; bh=zJsHY8n2NA4exO9t0dViCB784NHvK8fDVYNzRX7Qd/g=; b=CaAyxM//HZ3AngugDdNBRQ9zIgHhCNaS05Qa5FDRSw7n9rw3aifJGiJM9TlVRFyQefWSJuIUy0tUq42JUlKFcK37ordtxaN7pD8w/egV10eJGG1fM9fFmy9T/yPMsEXwyBtX7RokbknATUsCgnWZqHOKpiFcx7vPyhnSnU40WWuvI5BpeL/Z3xxYa0GdAfiIA+7cK+xISsqpAnO+poyiTdaO2TBHa5NcEFt7gwTWBOiX27C1UzkwLyqE2qUy6kKqoCZmPmKqPm/78TFjabMHtGwc6SjwZgqtn+nyL54O7+vF392mLAKzkDSXGGu/F0v2tEekOZG8WHbRclM6HFNUuw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=zJsHY8n2NA4exO9t0dViCB784NHvK8fDVYNzRX7Qd/g=; b=b5BCIJtgBOh8YAtgGwBvA0VFs8CKKDBEAYHHyEyPtI7XnoUTxu9/fhToH1HXhD9XNDOFUHqieODH9PUe5GJ8uUracWwZifTrjVDm+6Nab9gvWot5xiTx/owLCw6MZueg9X0+ZYK43Hvmwo0oehRpOOOmZJeHMrIhZ7gSFUcapdw= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:12:04 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:04 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 28/37] KVM: X86: update page-fault trace to log the 64-bit error code Date: Wed, 2 Jun 2021 09:10:48 -0500 Message-Id: <20210602141057.27107-29-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:03 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e63e3224-3870-4c3f-9adf-08d925d0660e X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5797; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 1c9QTLjnVUrRfH/F1be6xcMgyxZqOLp34CwZTYLq0oC2jsCYiByiYPkQxTGPPKopXfQI1tKUp7sClFta8Ran2vB3JwpvzI7eYGsPQQd0NT+jBFUnSgvYaZoUU5K7XTuwXhsNkQqGN1GAE/XrNhdlrvTvF5ZO7k9MuS7CLk52X/1rPFJ1VXONgN6IN6LX6jkD57aHs2qbBzNw6tB4YusKCh/0zuZ3rbmpg7EfXUspJiSVBEhwmh3pvXmBFnAuhLYU5zBGHk19q7JhA0PVAWu2Gk9IsC7vafS3PJn+/LSBgp/ydhMAoVd6N+RTKNOvYLCgX/t74efpyfNKYc2x3ER2jScWpu0dH6lXmFUpCZ6kxxSC+BK+7CMGHk4BSEW4PBmBuiTzMHY66YHRkmSvFEtzFG1qELakF7AHwB4BkTuuggitybqSl0RTDB22FkFpg5znl9IB7jEsylhorAKnOWJ3ux+1tfxgcWplmDgvAOW7eRAtSzp6p/CpNzSKznEOkHsjztUFfiVm9AEoEFpj5SlhYTBxiZDnKk7AdsJWaaAvmj7ZIOPjRMr7m/WEcCkEWmvuTwH667KQDHlmPqP7IXgnHWjr4qNeCLYcszyB2e7TIa8N8bUob8clwiPzEyBx9Uew1ocE2H9ehQqKvX7l+wg9Cw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(956004)(8936002)(8676002)(36756003)(498600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: WxhEby/+l2J3Ss0rTer7mWWFjJoVgJlYIJYiI/4McG38U1Ydo2KQC/tCaYw6Ypr7+t+qoGkfxwRIyiTe8j73iLDF0snl1+lZmMwmdEXDDgTiETyKW+U4f8WI5p3Q9/8cmGZdGB6iAaqBRDTHm80sCCmiVoUr2uGCef1P76+eFrsjoDHpemWMQmT9rIR3vNTDAR6oxfHw0ld9DQ3EvdYiTt5akEi5DsnY66Ehweqszk9SvG4Jgq00LBrKQOeb4R4NmScaYlNQoweSXT7ivL8mqgZu93FBDth5ejVmOaAA0xMpFkURLNI+LC5n/QP2ZW2jIgKiiRqQuIEAYEdm8SmmlqT9yF8Rd4kq0bDhpHTfXcDFSHMWUvzYosT1iounPob3ui3yuFsjaIsFrmOozh1TVIZzNajVRCaJOc+EjqFdHyoLX7mE1QR5qMVtH/rcNgFWfGo03tarb12lkZDQLiIBwbLmRb4nW9VWlYrcl0GamcuQQ8h0LSboj08bA9r9m3CsTXJZa0kSPhRwfdOtBmDUT49rHsvuR34jxkbdiVkgaL727Y19KVzv77874weLAoemG061WJudiD2Hr+93XZmtgd+VPeOVWPpqK4zhKe9exibgj264w8a5wvcfLdh0115lwj1ycZT7wfD/iSe0UxFdzUZGnFf1JP1WMiEvTSD8h8+DW9wgqAHs+ucgK1Qi9+b6hW4l3fY8y61Yzz1q7sxzgFCjXICoNwnuW0ntaBl2cJyG6+46Ch3ezYLe6QQCpBaDFgDKjbCxi6HyuOeKzy3u+McF94I2dUUSwuUjaKiBTeOLodkCoB/PJmY1PzOKWzIYK4PWxwCHnLQXovajMWzTUT2urm/dX1yJhRgBJOW3ZfcBq9iCLfxTpKJH8llv2q0iAbvasIpP2MTsgXwS5913HHNNmG18yi10fuyiV2gxHCeeh/OcBdSwnj000+qJ+JrMoFeGXSPq0pccOPqy0F1pWtOztUsEYpw8Gh99gArsYNtF+L6l8Wai18gM7vQDhMf9VuODK3h8TNYW1yLZcOIrSgMhAh/K6HGOOq782SVkf+4FO8N4qqJyVReigudGEBZAQC+O0dXc0PfL6sap7D9B6surPExe9uymjJMxEFoNMfQsm6wJpqSopmnbDLktckzsHBkbMMFo2ihPzNIfwDZbiN03oOUYLAE6bHpbXHExF91TiNZCascVFLh5eWZgjrn+Afk6Upzuz1g9gx4r06xcfkpBHvu1A14AcI1UGcug4c+o0L4+fQpFJU6UPk99DVg0L00rM0umlVFtv+ErisJr1DGer794psV3jfhCp4gKeROu6ji7yEA7UoSt3m/VIbeE X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: e63e3224-3870-4c3f-9adf-08d925d0660e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:04.7213 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 3Z9wGdBLA2bwMY/D/pYG7rSsD8AnHPrgfDnzwD/Uuidu4R+FjKYR5Hn9UED0qGuQ1Gd6Zw/3b7jgX+nM06zRbA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The page-fault error code is a 64-bit value, but the trace prints only the lower 32-bits. Some of the SEV-SNP RMP fault error codes are available in the upper 32-bits. Signed-off-by: Brijesh Singh --- arch/x86/kvm/trace.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index a61c015870e3..78cbf53bf412 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -365,12 +365,12 @@ TRACE_EVENT(kvm_inj_exception, * Tracepoint for page fault. */ TRACE_EVENT(kvm_page_fault, - TP_PROTO(unsigned long fault_address, unsigned int error_code), + TP_PROTO(unsigned long fault_address, u64 error_code), TP_ARGS(fault_address, error_code), TP_STRUCT__entry( __field( unsigned long, fault_address ) - __field( unsigned int, error_code ) + __field( u64, error_code ) ), TP_fast_assign( @@ -378,7 +378,7 @@ TRACE_EVENT(kvm_page_fault, __entry->error_code = error_code; ), - TP_printk("address %lx error_code %x", + TP_printk("address %lx error_code %llx", __entry->fault_address, __entry->error_code) ); From patchwork Wed Jun 2 14:10:49 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294855 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D5ABC47083 for ; Wed, 2 Jun 2021 14:17:13 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5F944613BA for ; Wed, 2 Jun 2021 14:17:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231623AbhFBOSy (ORCPT ); Wed, 2 Jun 2021 10:18:54 -0400 Received: from mail-dm6nam12on2079.outbound.protection.outlook.com ([40.107.243.79]:15104 "EHLO NAM12-DM6-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231653AbhFBOQx (ORCPT ); Wed, 2 Jun 2021 10:16:53 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bI1VX0wUunEp5GZ/OephPMlHO67cBJp1GJRJ6/yLdXVcBqll0gYHkdZWG1l0PTaoTn/DqYg2kHai/eusrH73ldrzkFtJpahKCV9uCusTIkijJ4AULkGDbI6f+5u8/+raJnZ0zokRWJ2SfncVH/NRsiX7ylIecm4RYudfSPO8GZiJDiVbR8Daw80UtJ2p8SXfR/YPUR7b74KR3fAonNHNMuyKHO9mdTKlgAGex9WN9yUdvYqzLTvNFbjHLqxemNfHwqQFbwhvgcJNI05kLB2y2aEIT4oCg5n93Ykfp1ny+T3OdyyvrJHPjDuYGaWZ3dRH/L3Jr59zBfTd4txdqD7EmQ== 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-SenderADCheck; bh=KooHv0mllq5V/tjmM2u3XNXV0Y9fxysjExz0F1f2TiU=; b=SI9MqEwndRY1nWAizzZKtc5omI+HmOd84wHIJNeGWY+0EVe8SIqnRqKRCDxRHdY3ZvV5Mitj3bXF0T4ijMKtylYudoszwPLvM7qOLf11TJm48sxBME0hmAGejWuTmlM7IbNbAwjxMHlWWFzK1PJOw6WGGXMnntrlOetvv6SR7M0MOLCVbcmGX5VNHajS/CCh+GZGKt/uTfBC9jMvxsJzK9kt94j1R+5thUri4vXdZ/ZbY0VrxRpYtLeKm69VLHdzL4AFiOSzddMiFDjaxZW+vSVggCryzanRdeDT8BWtsxtCzRqKG2sANBPqXKMHd3ntN2Nw9EoyxcTrahFeId6szA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=KooHv0mllq5V/tjmM2u3XNXV0Y9fxysjExz0F1f2TiU=; b=oUViDX+5dylzk4O0uyEan1OshcOazB4Vj7nty6TP+38WJ8EoguGyPGcCRSxBgWRvIcq5wXCOaLa2UtOgUkX83vRjOlHJrXIxIWRsIVQ76mZQ1Ahusu9jeY8t96UeDKY/XDIKDIANdMZ3YeTJcEFeGZq7JejZGheSUEB8jcq0cVo= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4592.namprd12.prod.outlook.com (2603:10b6:806:9b::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.22; Wed, 2 Jun 2021 14:12:06 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:06 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 29/37] KVM: SVM: Add support to handle GHCB GPA register VMGEXIT Date: Wed, 2 Jun 2021 09:10:49 -0500 Message-Id: <20210602141057.27107-30-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:04 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 36cdc39e-1f27-4e69-cb92-08d925d066b8 X-MS-TrafficTypeDiagnostic: SA0PR12MB4592: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7219; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F4L4x/cu/I+t2iyzrDwJUwIMWZITBNQoFvj/Uq7c2Xno7n6tb+1i7+4r8W7n1h5tHB6xrQBkePfHn2JfPII+naGQdsr8+VGe2aDpi3N7Jp7AU/5rTZ+XToGo6eeCJKleKVtTL9kR3A4B7STtg3DwCNDXH6Dab27rf33J1IUoxiW0VP8X3yY+y1uHMdaR1PTdzuCtkzUuoIT9qS17EazGXfSEuknZdd9eVJaGVl9djdxTn+/mv2w2mrl4fqlrYKV6iJnl/kzEXSrvboN7mdvj9WGJVcuI6f8yI0F62ZcQsKzUhSB59NIa9ZdYEIHqU3c+HraYTZ3Ef6svXQa6F03gHtLL9+6evh6LN91fuA2XUXO3eZa8Pwclh3CTe2bVSGyQZEhv2CIpfsbVTIHP5bWLRBs/s8N8ax5QfS5jdHdeOOE48FvYoY8mSBoN0e5GJxH6CHuFNk3ozKvFkzkFTeAmBru3sai8fQDKoONpwAF+In1SSwgwL03Zjs5avQgDSaxT0MJsqrni0GfTZPahmQR0kEtTD9yLP88UorPnyWtAfyyIBxlJVXBMgit58+lPqFpjsoAOiNL6/IPLSAyM156kY4QhWYnaYzskgy2i15GTQ8mUXBCsVNurw6pQUpo0jI4vv7t3RiHx5yFlHdpQfF3QBA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(396003)(346002)(136003)(39860400002)(366004)(376002)(956004)(8936002)(8676002)(36756003)(478600001)(54906003)(83380400001)(1076003)(44832011)(2616005)(6666004)(38350700002)(38100700002)(5660300002)(66476007)(66556008)(2906002)(66946007)(4326008)(26005)(86362001)(186003)(7696005)(52116002)(16526019)(6486002)(7416002)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: vd4PGtCPOErTv6d5hXfCjzV6v0sgEBskZ3THYXtwkm426tQ4hP8O04fFTK9llJCJVXom1koT98zPk+4hD5o8i/3sDFNCwcLAzizQou+FncBjHKYrbVT10xKly0qg0RX1fGopIgA10YpBfYIhZSdAd9UivbIJtzOxX5webNdKXG8lszadHb/gXSWUwOtr30aLCa2/CSD1Lyi0TgsHTFXswuzQuSbUss+1safA5hVLySj79ExDaqJHVw8fqxtkJ7J5EJBlmUcm8m/KaW54dBr9Q1q+KzJvJiz6jRYiWL2uBSthUw/1VZFO4SbLxgWmzr2k7ico5+7190jxHi/U+fjfFoRv4I7kHNIHKaSYFPsTHEogRX7qETicaFTDTkbIo3XPqtV+BJ7HrM4dztxJmThy9aR20stPWx/HIi+FfsTO0v+Z1efwTblZ+/IMMhtfvI3vTqDRFW5mCFxs1sBM9OXRwPVCzgHSDhijc2gAg6n6YLF4GTBoyZvdpM9daKCp3BhBfTtFVSZM5aRyavEvdwPGOYGR3kwZ1mgdZdIh8Ac7YAyRCtx5YRwxNNTKtMTHz4mmteN3iQpFrl2reAAZwzawV7c6T1GR4z1bj6Njzx1GyP0vxKV5WOXbUMok7w0gloOO6B8A4wXsq6PgqdU3CVYU3RWEu3JjQkAAVE/Kyr1fyrppFBrW6vg5fl9Ic+XDo1IZy0qzKS05LlqKfHR5kxfG792XtnEkxRAnMDTUXp5ODOT5MNYXl/ioQOv1Inw7KbngLGdod7YrpzVQLLb5spGdR53EYgl0t8LxIn8XExQn2QaH8TA+5X1m64XRG00j+zjQ3HwYTi7fpAPfFzokS7F+papmNjiHc/5B+D7gHK4bw3sEIWnJye5xGN+u+fdQK4S2FJydChEGZzlD2KWJPCRcHhgfhpac9GU7GEhs30+Nr6iw2TokSAQK09bNdpVniMN/r8Hd1xu3QmYTJz+faggEt+sB+xIPRmah/fXf7X4ioSBMYOLfOygV/Rgn7IeJdzLgJNCtsTJwhFMbdGdWLBmPd5b3KUC70HbS8DQvQkGvyMbTrrpXpYSLfRaO6gnNBh56Vesq4zftHdUjF0Hco5gEdRGyUt01C3p0r3pZCSEnVFMSzGv6NpM32ggSQzo0UbOPijV/TBRxLpvQLmysgyFMBLwy7PVGWz8KSglf5WpRknblfRr/PEzBjTPAkwr/vv54oWCztrh+8vrZbKHJNl4xmSZBsWtedAfen9Ldgh+7UP7UO6ay6183LL6cbti4FlyaMdBJofO+MHFI+S9w3gcpho62TFcOelj5IQyLmGIKfM1xRyLd4xLeNtLyY+nRRyps X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 36cdc39e-1f27-4e69-cb92-08d925d066b8 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:05.8546 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: e/Vsmnb7QF2/EC8QAAUB5NEhYfgZvVA78Wly7y9MNbbIt1FRM17iqEVIg9GMhvI9OVZRxa0aLTJJXhf031dSJw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4592 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org SEV-SNP guests are required to perform a GHCB GPA registration (see section 2.5.2 in GHCB specification). Before using a GHCB GPA for a vCPU the first time, a guest must register the vCPU GHCB GPA. If hypervisor can work with the guest requested GPA then it must respond back with the same GPA otherwise return -1. On VMEXIT, Verify that GHCB GPA matches with the registered value. If a mismatch is detected then abort the guest. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-common.h | 1 + arch/x86/kvm/svm/sev.c | 25 +++++++++++++++++++++++++ arch/x86/kvm/svm/svm.h | 7 +++++++ 3 files changed, 33 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 97137f1a567b..e7c6ce2ce45e 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -93,6 +93,7 @@ GHCB_MSR_GPA_REG_REQ) #define GHCB_MSR_GPA_REG_RESP 0x013 +#define GHCB_MSR_GPA_REG_ERROR GENMASK_ULL(51, 0) #define GHCB_MSR_GPA_REG_RESP_VAL(v) ((v) >> GHCB_MSR_GPA_REG_VALUE_POS) /* SNP Page State Change NAE event */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 6b0c230c5f37..81c0fc883261 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2900,6 +2900,25 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_MASK, GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_GPA_REG_REQ: { + kvm_pfn_t pfn; + u64 gfn; + + gfn = get_ghcb_msr_bits(svm, GHCB_MSR_GPA_REG_GFN_MASK, + GHCB_MSR_GPA_REG_VALUE_POS); + + pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn); + if (is_error_noslot_pfn(pfn)) + gfn = GHCB_MSR_GPA_REG_ERROR; + else + svm->ghcb_registered_gpa = gfn_to_gpa(gfn); + + set_ghcb_msr_bits(svm, gfn, GHCB_MSR_GPA_REG_GFN_MASK, + GHCB_MSR_GPA_REG_VALUE_POS); + set_ghcb_msr_bits(svm, GHCB_MSR_GPA_REG_RESP, GHCB_MSR_INFO_MASK, + GHCB_MSR_INFO_POS); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; @@ -2948,6 +2967,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) return -EINVAL; } + /* SEV-SNP guest requires that the GHCB GPA must be registered */ + if (sev_snp_guest(svm->vcpu.kvm) && !ghcb_gpa_is_registered(svm, ghcb_gpa)) { + vcpu_unimpl(&svm->vcpu, "vmgexit: GHCB GPA [%#llx] is not registered.\n", ghcb_gpa); + return -EINVAL; + } + svm->ghcb = svm->ghcb_map.hva; ghcb = svm->ghcb_map.hva; diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 32abcbd774d0..af4cce39b30f 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -185,6 +185,8 @@ struct vcpu_svm { bool ghcb_sa_free; bool guest_state_loaded; + + u64 ghcb_registered_gpa; }; struct svm_cpu_data { @@ -245,6 +247,11 @@ static inline bool sev_snp_guest(struct kvm *kvm) #endif } +static inline bool ghcb_gpa_is_registered(struct vcpu_svm *svm, u64 val) +{ + return svm->ghcb_registered_gpa == val; +} + static inline void vmcb_mark_all_dirty(struct vmcb *vmcb) { vmcb->control.clean = 0; From patchwork Wed Jun 2 14:10:50 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294729 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 01F9DC47093 for ; Wed, 2 Jun 2021 14:14:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id DD21361242 for ; Wed, 2 Jun 2021 14:14:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232146AbhFBOPv (ORCPT ); Wed, 2 Jun 2021 10:15:51 -0400 Received: from mail-dm3nam07on2076.outbound.protection.outlook.com ([40.107.95.76]:3297 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231765AbhFBOOX (ORCPT ); Wed, 2 Jun 2021 10:14:23 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f/ONCllHpZe2SZUtiv0PdBI24gECKimv8M1GRzD5LRMdXo/I0eTE6tzSpgYxvLdkv/K4iuwlFYcMEgZvEgpm4FgzaNeO24o6qFmGAnqULblJq2mqqtft6VLxldrR9roemUVnCFv0veNS4qcXZ7mbZazW1H4wPvgx7/eK+2xSiMJbz9IW4gWD24Vu3BhbASfTIBRemYfDd5UF+bT9ElASdEZd8ZtAJgZcz7P9BLRacqbUxZEZWze/EWZKK70OM6CPbCwT85/RmyunFlPZp+j2HzcHeLviVVWngPm7P+72I4wxIiXuz24409G3LVRqBV3WyEe0H6qgpUo6lv97+fIyRA== 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-SenderADCheck; bh=rVtWgC7hpGHszr+GQ7wUzkkLKdRLKB2IIVX9lHAB/tc=; b=Hx9scSvcSITHGPj453PhRhGZySCBjFYaLPGyDRgoZ3sfa//0/sar4YjHPBxWo9sOnGlg8CuW3Bob3rbH5ewB8kYN5LlMxRf/NTsh9TGqdL10n0d7QaPUoMsDUPQNWMgr4nA/LK3ujXY+L9CXmN3q1BkSNd7ICT0q7MLhX0lZDlrE2aW3fZvn2W0F2Sphcs3hh63+OwWcTL4k1K3ppsnPhowJ6lihjuf6ddWrdJ1W1O1GTbAsrcPswSN4hGWVz1BO8YZOhm7oH8a+I9gXnN0Td3m3mvPx2hnwUz47K+pst/ErX/bwDmjfI3tdayRbuwECe8om4m7+VWKOX7TwiOizcQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=rVtWgC7hpGHszr+GQ7wUzkkLKdRLKB2IIVX9lHAB/tc=; b=Rydro723hNpC7EPHNsG3fvBcAtZylRd0Z5A4VVnwkeUUWm/Kh/fPxttUFmibCFmYeAf6Qrs2oyJqUfttrwCeIJV51x8XXK+sXHGn/OqFHP9BPzpv+ZndwdceT6HAqPxRGOmn+0toc3glBa8R5zS73k3A1+hmSMjLEOVuRa0r7A4= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:37 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:37 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 30/37] KVM: SVM: Add support to handle MSR based Page State Change VMGEXIT Date: Wed, 2 Jun 2021 09:10:50 -0500 Message-Id: <20210602141057.27107-31-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:06 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c0319594-6256-4db4-7d92-08d925d0676f X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:8882; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: kpCSXD9DbZ6BrJgltix/JyRFdbOzkHkhFgTvYb9xSP77LVSqrijCbI9y4ZzWlaodTrx6BvqsNiPjCeWomVlq9/GAaIi1RwnC53g0C0FLoOCWZMb2yxykBYUwyP+k+JDvVFSADJtwZAxGGUyrnmXDSd6D2xOBV4heNzsCanPKPcSGImEUpn6mPLrsAqbGUp5nclmo1N7lEg6z9OlQcpnYiWEn7AGFMxOBZdyPFapQDHNCpAuzh90/ghKp+DjjqtUlEPHmdgcC9usMMFyrC64ElAqXJBPWRRgv+wxJfHtA/ta1b1VTCmlvw5pTW3OAfWQf9jjdzjc+13ZiAREBwiR3qRw/0uzhcOE/XIfjqKICj8NSdc11TZaZdrqJXejXSKVOSxPH+1X8xKkD4ju/oW365x6Zg/xZLKMhQWq8nu1zD14oBGzE7GatQ5x+Hcg0GuHtlSKAZqnlpCItfE0aDjdGEod3JH2t+3bX4knwTd2ovKqGFsiUygl+Gr3lGj/16fjGDPhI3wMUWbSNs3eYX2z5vnsBUH40Ll9u0rMjYu44U7NYgTGf+5ofjk2uLsvKcOhZwbvpbDm06or6PskbzKwYgo+gIhqvH8QpzVr6boSScwWRzyHI7o/7OzbvN54YkzFFNgrQIywADjauIsPZPGQGUg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: 9cUbvnfuRL7/+TeXrjCU7HYsRogvTNxAZHfYDPhnkS2MgYq11jg8GXbQNOzQ1VvlSO/bkk1PEcvPYDlT2N9STB9dTl6J4C5mdkilbt0qsrGQQj5byYyDjAa7CII8VkVLw1BsRp2IC79+yolUC4MS3oVV8RX26qE6bVsqgJjbZ458dxIl/tIuFkAeG2mB03FfxK3KSQMhmgCSw2tx+3Q7iWkUFf9BuX5Wl2lyNGx5+qrl4RL8500q5oZqZ3QqZ3OsoOvbOILUmV8nZ7WH+PvVTUUo5GpYC0JElYCYUnH22BuERkV5YErfxTpAXFu5DF5rHrH2CIeMGf9SbXxpotNxp3TiD4PDx9VwyD1FJE0GCNDuMNeFwi3M9sqAPl2W051tMB5VZt/kHeEDoj0PjqngpkWNjpGYEXkws+CEq+iXP6N1rocpdIfAhrnx8DZOuACJTdF/nWg6j0RlNULbSWTSuhCMhaE/GC1ZTm/MFpnyRZyXKJsypmQOG280u4TUhbpaXN+GuzctSOSveeTO98vie9VGsY4xxxzrCQp0YT+DEOW1VVa84A2Jxi9/F13E05dvMm6MpTSKrkgVtPJ+eqb901e1gCBpJob64Gc2UYup9/Jbusw8npbaTnjSGyZpVVhe1+kJYB7Eq90JJSj+nw8hI84OoupvNdtp/YtiGQOmkeiPzIU7519avQiJKL7Etct7dIq2RQ7FOlho2mDhBdW1V3G+2D4LIsSOfHo58MLWTDuKzy41N39rlM9MXBh/WwmktsqJtaO04RALjCOKy7BnW8a17YdkhuV7jCM/FU+Q4rcdWAlFMSANWczkJdH1HYMmevrvGhWX21KTDTZwn6DMyMfdhSlEGHO+7ltXtygRYqbSvWDb2ZTXIAKTV041/F00Ab4hHCxfe/zY76M8Xv3MC4yyCfZYJf37Kfq1LCZDztZENAqvIdmoZvF+kQEW32Hjs4rybzleUV0YphKRHQ8MfkEf7YPnDcAFDmReCcrN4mCEFX3WH/EbfRbPCYjFEAjudtQkv8Cf8MbuEvAAKOtr6NBIG3u5DtPAVmCzFfvxneYSa7QD1cI1KBs8TgDx283KGQCrcJzqCEnzQxv1MDnpckwytwHkZgqkTWLUCMbZkRZz/YUmqZpKAcsXL9aWgCi/z9Vw1vsoN3nS6L7bkHH+z97GQpcKLcCb9sgYMW5B477yfTUyutCFJHW+Pdak4o9Hgt2ic01xpy7xjvp6gFfDI2sKh/wodKkry3U4W1MsO3nl6aRv8FQZZA6OqP/Ys1jkc3O+5FMuiORxW79kf3f86az4xQ7sIOO7PnVn2tDTwvrfuXHBUGWIfV3REUmuQF14 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c0319594-6256-4db4-7d92-08d925d0676f X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:07.3937 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 4av2C5RhpCDbYWQVtRqNRLJma9PB2t7bPh20tsmgdBOR4591hmC/RmupUWykzB4ddlhENNbFJy/JBV8KF2I67g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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. Before changing the page state in the RMP entry, we lookup the page in the TDP to make sure that there is a valid mapping for it. If the mapping exist then try to find a workable page level between the TDP and RMP for the page. If the page is not mapped in the TDP, then create a fault such that it gets mapped before we change the page state in the RMP entry. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-common.h | 3 + arch/x86/kvm/svm/sev.c | 141 ++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index e7c6ce2ce45e..ed417340ed42 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -82,6 +82,9 @@ #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_RSVD_POS 12 +#define GHCB_MSR_PSC_RSVD_MASK GENMASK_ULL(19, 0) #define GHCB_MSR_PSC_RESP_VAL(val) ((val) >> GHCB_MSR_PSC_ERROR_POS) /* GHCB GPA Register */ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 81c0fc883261..dac7042464be 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -28,6 +28,7 @@ #include "svm_ops.h" #include "cpuid.h" #include "trace.h" +#include "mmu.h" #define __ex(x) __kvm_handle_fault_on_reboot(x) @@ -2821,6 +2822,127 @@ static void set_ghcb_msr(struct vcpu_svm *svm, u64 value) svm->vmcb->control.ghcb_gpa = value; } +static int snp_rmptable_psmash(struct kvm_vcpu *vcpu, kvm_pfn_t pfn) +{ + pfn = pfn & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + + return psmash(pfn_to_page(pfn)); +} + +static int snp_make_page_shared(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level) +{ + struct rmpupdate val; + int rc, rmp_level; + struct rmpentry *e; + + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level); + if (!e) + return -EINVAL; + + if (!rmpentry_assigned(e)) + return 0; + + /* Log if the entry is validated */ + if (rmpentry_validated(e)) + pr_warn_ratelimited("Remove RMP entry for a validated gpa 0x%llx\n", gpa); + + /* + * Is the page part of an existing 2M RMP entry ? Split the 2MB into multiple + * of 4K-page before making the memory shared. + */ + if ((level == PG_LEVEL_4K) && (rmp_level == PG_LEVEL_2M)) { + rc = snp_rmptable_psmash(vcpu, pfn); + if (rc) + return rc; + } + + memset(&val, 0, sizeof(val)); + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + return rmpupdate(pfn_to_page(pfn), &val); +} + +static int snp_make_page_private(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level) +{ + struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info; + struct rmpupdate val; + struct rmpentry *e; + int rmp_level; + + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rmp_level); + if (!e) + return -EINVAL; + + /* Log if the entry is validated */ + if (rmpentry_validated(e)) + pr_warn_ratelimited("Asked to make a pre-validated gpa %llx private\n", gpa); + + memset(&val, 0, sizeof(val)); + val.gpa = gpa; + val.asid = sev->asid; + val.pagesize = X86_TO_RMP_PG_LEVEL(level); + val.assigned = true; + + return rmpupdate(pfn_to_page(pfn), &val); +} + +static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t gpa, int level) +{ + struct kvm *kvm = vcpu->kvm; + int rc, tdp_level; + kvm_pfn_t pfn; + gpa_t gpa_end; + + gpa_end = gpa + page_level_size(level); + + while (gpa < gpa_end) { + /* + * Get the pfn and level for the gpa from the nested page table. + * + * If the TDP walk failed, then its safe to say that we don't have a valid + * mapping for the gpa in the nested page table. Create a fault to map the + * page is nested page table. + */ + if (!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &tdp_level)) { + pfn = kvm_mmu_map_tdp_page(vcpu, gpa, PFERR_USER_MASK, level); + if (is_error_noslot_pfn(pfn)) + goto out; + + if (!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &tdp_level)) + goto out; + } + + /* Adjust the level so that we don't go higher than the backing page level */ + level = min_t(size_t, level, tdp_level); + + write_lock(&kvm->mmu_lock); + + switch (op) { + case SNP_PAGE_STATE_SHARED: + rc = snp_make_page_shared(vcpu, gpa, pfn, level); + break; + case SNP_PAGE_STATE_PRIVATE: + rc = snp_make_page_private(vcpu, gpa, pfn, level); + break; + default: + rc = -EINVAL; + break; + } + + write_unlock(&kvm->mmu_lock); + + if (rc) { + pr_err_ratelimited("Error op %d gpa %llx pfn %llx level %d rc %d\n", + op, gpa, pfn, level, rc); + goto out; + } + + gpa = gpa + page_level_size(level); + } + +out: + return rc; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -2919,6 +3041,25 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) GHCB_MSR_INFO_POS); break; } + case GHCB_MSR_PSC_REQ: { + gfn_t gfn; + int ret; + u8 op; + + gfn = get_ghcb_msr_bits(svm, GHCB_MSR_PSC_GFN_MASK, GHCB_MSR_PSC_GFN_POS); + op = get_ghcb_msr_bits(svm, GHCB_MSR_PSC_OP_MASK, GHCB_MSR_PSC_OP_POS); + + ret = __snp_handle_page_state_change(vcpu, op, gfn_to_gpa(gfn), PG_LEVEL_4K); + + /* If failed to change the state then spec requires to return all F's */ + if (ret) + ret = -1; + + set_ghcb_msr_bits(svm, ret, 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); + break; + } case GHCB_MSR_TERM_REQ: { u64 reason_set, reason_code; From patchwork Wed Jun 2 14:10:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294733 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 39908C47083 for ; Wed, 2 Jun 2021 14:14:26 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1EDC4613D8 for ; Wed, 2 Jun 2021 14:14:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232209AbhFBOQG (ORCPT ); Wed, 2 Jun 2021 10:16:06 -0400 Received: from mail-dm3nam07on2052.outbound.protection.outlook.com ([40.107.95.52]:43296 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231871AbhFBOO3 (ORCPT ); Wed, 2 Jun 2021 10:14:29 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kbKGPIVfaC5LF9APWueZ0MUBeFighZF9luMdIpVDVTyJLBX6whOhmfl6s8MWhUyTTvK2GH/j/w+WrF6WaiE6WbJMfb4slUS7zQ7OFK5sSk3Rvn228i8cKwdsk2YUmXJz/l4eDBFpxPHO3ttSFknpUW+b1hPsfEfDz8RvkJxc+4npBhTTIgx6dPHxJGQJsM72omNzhXbBMqY8jMz3CrP/DccQiqrgUWzoX2nJ19aPJwDfzkKd+FD0gmhRqqAfw9GFXj+gFJTf9w/cjVCSeddkex6avFIjmTnS4rvRA02dba/1z4OhdxYpojw7LD+Pi3RKSDDJbiC3j5xhuNAfHWB2+w== 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-SenderADCheck; bh=TR/OilF+kLj/6CoYEbWq+e3F5tqZ+4LrJ8NSmaTNCg0=; b=RPJsSt1yLMhR7IzaKoXH9Qv+ktKhZG7Ii1jf/MLlYfRk6LJj/9ViCNfEbXluwVDTWs5TA7wn1oIVR6F9LapVfjoWmM5ckfdqBW5amruRYpWtEPk6oKUWp+nLANg62leSb51a/ViHTbKeS1eO0IMRQ44bZ9kYZ2nqkBCOwYF22YXgixLVBt2sIwUnTI1+mVD5OFR8dCUYk3hypfxJgkPvTYz4q2Vlr1vLXGl91gve1/yCQf5hTLmT/Nt0qUq9K/u2Yevh45pddoEO9KwYme6aHPiwk83opdRlboWrxA57Rb+54Z0YsROdRiG/oAzIBrzLzRI4pVZsmV134uUy5h8ROw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=TR/OilF+kLj/6CoYEbWq+e3F5tqZ+4LrJ8NSmaTNCg0=; b=0ix3C8QX0TGCHmZzQGYHQ0Ay6jl+yA887jCae1IT3iMCbHkY9BkT29Qab/DPPwGtJSomaB7AqifICeQPxcF1kbKbrYauepUjKN+q04R2xGlOO+Ywzl7LH9ejfFhMDrnXyiFpUjPwVLjjdbAr8OYrE+Qkdp15SNintLO+9zwBfSI= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:38 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:38 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 31/37] KVM: SVM: Add support to handle Page State Change VMGEXIT Date: Wed, 2 Jun 2021 09:10:51 -0500 Message-Id: <20210602141057.27107-32-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:07 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7144cafc-4990-4b35-ea78-08d925d0685e X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6790; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: vH939Gcn2vPH8DCoyoFfHGFLB4u4Hq3nnbNgYmsrjSotu1KCxX8z6KB/zWyJqnH/btqqKcL74tPs+MagRHBVsH35MFVCiSNKNyZ778sRtVZO9/lbFU6We3WZEawAITCA35hyRkYxgqAqH8E3nQp7JHMmAnKavI48EvUw9QGrQ+1wtSu0IEIdhqAEOaSpjEC2LnIK2gPMwRB3cQK1S4jlo4zJt44H4yRJwhqg1BoQgFkiJSa146S9+YDGqN1z9SGqtceOQBj68/TAqSciUAkYrZ11+emZ11d0kmJM5auCd7jKV8AOkBiIccRsCyhoBVnssoOlsXoYWlCr0ChrAIOF1vbAECO/yWCPFk8T2DxVgF3dDBjtZ1/VT7T39MFqb4s0OVhG/KxBtDiUJkxx2HIpCYF1yLAVCSclgARgA1GzKy9dhxmOUdyUP/76YxzebYIZhdED7bt744OUGD+1lPyv2sz7KKKyHHD/c2WDk4fhMmVRXWRhsYKZMumJo6znoI91gWSWpR6FP2l6bcRHDMclilzRXVlEMvg7o1nw5WXYaCK7eeeYomovJNcvab7jKJrRsft58e4J8ydUq60Fn1PNAWqi/vfhoglDW+HUIDeC4c5wR5FB7QpUdQS4KzR0s+LJN1rrq+iNc2ZdzIMtxn9BLw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: H7kFAsywpYqvWDy6+4TXxu8fTUkRQcbCDy/7EsMAtLApa/5z0eJgBDrWqzvlb3+p75i9gS9kV6xeJ8L/AkaUjZ3lLX6UlcIJUDq5rbqqvyUcIRJgXY7QokDmlgQB6haF4sU0bWdL2Uvkkw+3AIR8HGv5n0OYomNnxRlnnEf9lIfoL8t1o68j694D2dxJgg24iKPccnMVVJNbMqn7gIHhLEvgga+/AsqgZWjZt8j5Dcg4ZHIUOdSLFOkL5E180aoBn80UN/uzkaeefAMQYna8/bVPoFT3EbF22++DvvxSxOABtpv5HlM6OH06k5g9BYdCd3e/4QxM9dUJ2OEPgDrY+i/2wVBkiZ4Wr//4VTh4DTk/d/o2FNAL5gZW5cy7hqPVc6WXFGZlh3dRalpCg265s03CGtQeMqU/S1SpSzLZs3vXl5vTI5200BU+pmxKn3wDZnPIUlKpwn/rOWAn7GdlW7z8xzwSpBCHE/SHE/KFMB8cBbqbkSpO5Fz+wCIINh0FVHB305CeUm/CXCCpDo3kh8KEp+FXyVOEGMK7r6sO2r/ZBkfhHy2ebM8kDNgm/ze4EP/ukSVgnxmySYZAr8R3k9/5uhJCsrcGyR8AWl6rdZ2Ko2ZBXa2UAVQpuWU7DF3IzBD3Y4Z+Pzp4UyLq6o0mM/gWKlhSawc8ZAz50oM/b6sULpvoe8UQNe/DIdQ/4tiMt9+gqPaNXc9rEjijygqFqshTSI446IDBVB3tOLCl2DFeBP9rm3kQ+bhXFFKXD+FucJHmbklSVuuNi+DgDE3tFGerqBwl/6fRXbvKCBLJDFKzKt0X3yLzHyom15pYRNKzMZ5i4SjiRIOoigcAvPL3sCzGFbCt96EGPe2Qu+/j6/DumU2uKZhSZQ4Gfu/AiMCL1gvXXrzqF2NZmVyueX/gaqRLL//Xc/9K55xmd+pUyP+MMI0JZg+8tTcPsAOV51Q0LKpTfCIYyd19s5V95Sa4lcBeEv3zp9CZb0PMrR5sbKAD+OgSkrmNMp/YAB1Vloruxs5DzvJefO47sxDkhhHHGAYZbBUnwhlbcWCevFPYj/jE/Ag8NSZLyMu1N3jYwXU+EjZFDQu9AsZIuvy3utmgQpA/v4L/Xqbq5DX+YbZDld7hnqhD80+Kuvk+FdIie3mVfmJaC7qtgNrPfFuezlSFvQSLkIKr3wTDH7+oxFtRGEVvFYvYIdmE1DtKbBkOM/mffErbz1l8QXPVv3Re+2Sw2rEmKxBQ/GoopBAqErpUtw6UCQCo4COv7YUyr3xfpOGlcTvo+eCPCtJsIh1NJBpLTMS1GEHtNnDEuVlNhiybFfkQwhXe64CEYLLN462x31R6 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7144cafc-4990-4b35-ea78-08d925d0685e X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:08.6770 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wGK+CUL86aD8LHciSYB/CQiwOrErd40Rf18dbpw/jGQb4pb5PIxSAXz/oOIBTCEAyVaJ0LWdvNfFrOQoEHLw6g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org 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 section 4.1.6. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-common.h | 7 +++ arch/x86/kvm/svm/sev.c | 80 ++++++++++++++++++++++++++++++- include/linux/sev.h | 3 ++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index ed417340ed42..aeaf0ff3f2c7 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -102,6 +102,13 @@ /* SNP Page State Change NAE event */ #define VMGEXIT_PSC_MAX_ENTRY 253 +/* The page state change hdr structure in not valid */ +#define PSC_INVALID_HDR 1 +/* The hdr.cur_entry or hdr.end_entry is not valid */ +#define PSC_INVALID_ENTRY 2 +/* Page state change encountered undefined error */ +#define PSC_UNDEF_ERR 3 + struct __packed snp_page_state_header { u16 cur_entry; u16 end_entry; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index dac7042464be..ddcbae37de4f 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2640,6 +2640,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_FT: + case SVM_VMGEXIT_PSC: break; default: goto vmgexit_err; @@ -2888,7 +2889,8 @@ static int snp_make_page_private(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t gpa, int level) { struct kvm *kvm = vcpu->kvm; - int rc, tdp_level; + int rc = PSC_UNDEF_ERR; + int tdp_level; kvm_pfn_t pfn; gpa_t gpa_end; @@ -2923,8 +2925,11 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t g case SNP_PAGE_STATE_PRIVATE: rc = snp_make_page_private(vcpu, gpa, pfn, level); break; + case SNP_PAGE_STATE_PSMASH: + case SNP_PAGE_STATE_UNSMASH: + /* TODO: Add support to handle it */ default: - rc = -EINVAL; + rc = PSC_INVALID_ENTRY; break; } @@ -2943,6 +2948,68 @@ static int __snp_handle_page_state_change(struct kvm_vcpu *vcpu, int op, gpa_t g return rc; } +static inline unsigned long map_to_psc_vmgexit_code(int rc) +{ + switch (rc) { + case PSC_INVALID_HDR: + return ((1ul << 32) | 1); + case PSC_INVALID_ENTRY: + return ((1ul << 32) | 2); + case RMPUPDATE_FAIL_OVERLAP: + return ((3ul << 32) | 2); + default: return (4ul << 32); + } +} + +static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm, struct ghcb *ghcb) +{ + struct snp_page_state_entry *entry; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct snp_page_state_change *info; + int level, op, rc = PSC_UNDEF_ERR; + gpa_t gpa; + + if (!sev_snp_guest(vcpu->kvm)) + goto out; + + if (!setup_vmgexit_scratch(svm, true, sizeof(ghcb->save.sw_scratch))) { + pr_err("vmgexit: scratch area is not setup.\n"); + rc = PSC_INVALID_HDR; + goto out; + } + + info = (struct snp_page_state_change *)svm->ghcb_sa; + entry = &info->entry[info->header.cur_entry]; + + if ((info->header.cur_entry >= VMGEXIT_PSC_MAX_ENTRY) || + (info->header.end_entry >= VMGEXIT_PSC_MAX_ENTRY) || + (info->header.cur_entry > info->header.end_entry)) { + rc = PSC_INVALID_ENTRY; + goto out; + } + + while (info->header.cur_entry <= info->header.end_entry) { + entry = &info->entry[info->header.cur_entry]; + gpa = gfn_to_gpa(entry->gfn); + level = RMP_TO_X86_PG_LEVEL(entry->pagesize); + op = entry->operation; + + if (!IS_ALIGNED(gpa, page_level_size(level))) { + rc = PSC_INVALID_ENTRY; + goto out; + } + + rc = __snp_handle_page_state_change(vcpu, op, gpa, level); + if (rc) + goto out; + + info->header.cur_entry++; + } + +out: + return rc ? map_to_psc_vmgexit_code(rc) : 0; +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3187,6 +3254,15 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_PSC: { + unsigned long rc; + + ret = 1; + + rc = snp_handle_page_state_change(svm, ghcb); + ghcb_set_sw_exit_info_2(ghcb, rc); + 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/linux/sev.h b/include/linux/sev.h index 82e804a2ee0d..d96900b52aa5 100644 --- a/include/linux/sev.h +++ b/include/linux/sev.h @@ -57,6 +57,9 @@ struct rmpupdate { */ #define FAIL_INUSE 3 +/* RMUPDATE detected 4K page and 2MB page overlap. */ +#define RMPUPDATE_FAIL_OVERLAP 7 + #ifdef CONFIG_AMD_MEM_ENCRYPT struct rmpentry *snp_lookup_page_in_rmptable(struct page *page, int *level); int psmash(struct page *page); From patchwork Wed Jun 2 14:10:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294739 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E0332C47092 for ; Wed, 2 Jun 2021 14:15:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C5105613F4 for ; Wed, 2 Jun 2021 14:15:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231847AbhFBOQ4 (ORCPT ); Wed, 2 Jun 2021 10:16:56 -0400 Received: from mail-dm3nam07on2088.outbound.protection.outlook.com ([40.107.95.88]:21280 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231548AbhFBOO7 (ORCPT ); Wed, 2 Jun 2021 10:14:59 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RMwu/gcNpjuLadBNkTEq0R08Dj8uy397nP71/jytuK+Mb3qScox6k6J/E7wD2ihYj9ietwXydw0V5ZsihL/7tRrDuCCh6Q3vhrlybKQ26urXTGzNkYydaxUFnQld4QUuMAeSHf1sbHAc+fcS7BNxZTi39s2i5a5+Rl0+u5W76GWW7+NTkQxp4UpJ8bKILXwogiDg/kZArC9sgUmjK12uxDkX3gvGmX/O17+kqxE10U8gYh+XQY15S8bN7IXM6PlhXQxURKcAxSKTfox5YTU5B7hfxkZFO3/Bo1KPMM/IhOU+/RWlKGJM5RhMzWYUIcY1CVtcYUSmSIfWtktabZgbAw== 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-SenderADCheck; bh=vDsLkHL5aTZaasn23+ED9nFSFLBcECCwr0UJ8Ws509Y=; b=FFxa4L/cXUaCHPpICYb+4Ph7wtq01SeBm5l62ydH9tzH3ZeS1RNY7K+Hw8ek0ZP2mCEcgL25VBSEPvTkKar7z3VkJ8zBtr1ILFWhZw5jrOWdD2gBdA1xAiX6yJ3I3Gx27QE6KBq1oM9h03a1SaU6PrRtlzrfMA3+kb3pk7oXiVABqIEpFT843yL7WrixGyutyGO/S5035osjkEMeponp+BbUlUcBBsCTJW+J5KIBYjojhGR6cyf5F7hsnxMDFgioa3kdj/9hyqdDOZB31IAicFBk+gSq5cyZZMvdmDoSeSSJ6rWtJr2ifpGAnqgtIWqtwg/GaUTdxYZfv2qldGKfOg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=vDsLkHL5aTZaasn23+ED9nFSFLBcECCwr0UJ8Ws509Y=; b=Q4u1pwJIbI33IvoivrmoPtzdGPcnIXhCxKwcPrr33cILqKIeyUtvM7CigevBUgC0kbZguy1On2NAfWNMeHTFPqhWAYEdhMKL1wmfk6SvX55Ee67ZWl4gNJneqGT02BmHXVKgIwjT5eqp8Yj53OUcYacRQRN4sHzEhZY7IrjvX4M= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:38 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:38 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 32/37] KVM: Add arch hooks to track the host write to guest memory Date: Wed, 2 Jun 2021 09:10:52 -0500 Message-Id: <20210602141057.27107-33-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:09 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 183e5096-4d91-4492-519a-08d925d06925 X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:499; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fq/y53872CXo38W0Q6VlJ24Rugq8nDmFB45crsAielaZXEXi+ZDws8om9FML7UhoDZK+TTaD5cLnwUSgsfX/poC/IhL1H8II1qKLDTBYmiYWxHfGT4XstnTWAavZZNXGQoFzIbqV7qrM2AI3Bn4see5E81cCHQ5YkzafqdSJCDs+8aAT1/IvgtSD6so4SZ7F4C4sHI4gTZ0xm8wm+4rcuT87KKJBYQ2t4/41IYYO8yLyshYzSaq1LPf+0HuqiinOJ3qOU33ad1Ysi1sRbPL+w55nGwoIDnjq23/zHP8vM1CIcDsaf1chKrvtocvSyO4jplOy8/YwXYVJvbM+gQ4GwOF7AuowqVLWnJdJsFgUoBIklo1et2rTVQ2T1FfQhIDTEgu8v2VpCzOxaL0ba8bD7uzND+R6nulwhizqvPVo5o/lCfzNY+U2AXmQaywXQ02KMeF8ZLSURZ8HLNLSN/yEHhq5mZuDTlwBCEVEZ7HNKmMTutcW++Rg9sKSZ/RvPi9f2TqWe4d3Z/ZL+DmKWBf8uQhpBzm55B6K+fyhcOs6tZmXpZEO+AN9bfKm2w2LutFlgnsG86DSDMev8y21CqSA5Ie32bz5yMZ5I2WSZwxIEsw3tojpvlJoHoWfrOGu50JU/YmUE4fR8F8cDlMzyNFLTwTUa52G+OmxNjyM5FJW3U5/GfAb6X4Cf8Fxu2hmkPzi X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(30864003)(6486002)(54906003)(36756003)(309714004);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: tXNI+YrVgG5dti+ZK/+XCi958g6yQE7OLKQZLFcc/ES42JFg5YWBnmh4OJ52+ocE1eGOSFXC1lc7W34xxPHVtT7TzfMaQTeydcUWqQuJCP+GnADSb4nlFlvfhMEjM3lfmuSdc8CScEg0SAHsHzTCCa5/c62IEcJ/1K8i4UuAOyN44NSEUQOoj2XQoOpzwUumpMU/jd2TFwPQE1jPTo0nu7lgMEmPFKixJGSKVz6VodRhe2J5+7bTr3MIrPyUu9FBNNj12ixNPBOY45Ai4eW8PdzRg0uihvcT1gHL6SYB5m5v11AARAsclYoFYT+ZQuB17OYGY49NRsKj2D55bZ7CbeCsv4h7vTOWQcQv7OzZ2iN/oAPE9hjr1FnFP0ziIj4QgEJA9fJImjLam9SLs1a0a7KzYcdnDZMWjWt9QV+Hs1FdteWoPf+1TpXPvEPcLqUH4uEQKvBd6poIvUHwZYDkJrpvBRSDtLVfy/gJcq6UKTwbimgN96nwrZuie9ZWJJVs5XRls0zZ8KzM4Ge+2+wQwNriyg0iR7fVY5RDF+Dbgoxa6Ti1kDD8uZM3k2N9vC0WCZXQbXx4amVc8jIiariTF2SExNfMpGZngZBLZBcUhOX1cmwps+SvluFPHf1qQL3Yw546SwGWzy8c65ha62GC5Ph96xlGEmFznEEIZ0H42IUWgsebp+Wciei7Er1cksJ1WsXNuJWwG2nUdpdMA/3TGklosXRLmyI2hzz1heqGIcmOwezJUgVxOteVTuklPzN3t8TuGgFYx7ohuz2FJ3z3kG1qZa/lwWpSpBj+wcN7pI11zewSpy8x/RsCHgZVILekuf/6P/sssGQeWbKDrhXBb7AlIEh4ZX37D3i/EZ9KSLFiAElr+WgS+EuhXL/5h6fIb2R3/pSt4yTrmdoh/ZmFxfN1C7vXORH6IcFjW3zng+vvRDw36ImFgmv6wsBgJUpQoOLrrKSon26PSJcyVOHdSSPc7y6WR4857owDooslRnAEEpEaDqm8MvZf69NrPNyIRrmNjrCWU/kL3kviBZlmvsJ/KpPRFUkNpR0NSYGeFnyv+WUP1qt8jRkoRs9uG/GaCyt/DAghGGJ4RJEnRg+QBCcTrLaoYaaZK/J9GQP7quJDbfL77Tois0Ut1krWK/wECLyU9pv+XxszmYBOIm+qL6bXx56GU5SNuUZjuQhOLMDIZpE3aWKbpq8oqZiM8+iFUbaInG57efl2gta2Pz/swTlWk0WevqpQkIy2E+BJoNPkv1Jjk6kKuyQriYKgThRC8Joqc7mv6lFJ9qNFopq6llU/HbcsWR/cyGK+2Kouv/5adNK6eRi70ZfxCvGZofZ1 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 183e5096-4d91-4492-519a-08d925d06925 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:09.9173 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: VKmS2xQ+wfrE6KhpXPeIVku/xu6f6bEVolqP63Jrr1oz9Eavo04eTRHgOcUbDVdjy1pgVvwJjvxr3V59ILldsA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The kvm_write_guest{_page} and kvm_vcpu_write_guest{_page} are used by the hypevisor to write to the guest memory. The kvm_vcpu_map() and kvm_map_gfn() are used by the hypervisor to map the guest memory and and access it later. When SEV-SNP is enabled in the guest VM, the guest memory pages can either be a private or shared. A write from the hypervisor goes through the RMP checks. If hardware sees that hypervisor is attempting to write to a guest private page, then it triggers an RMP violation (i.e, #PF with RMP bit set). Enhance the KVM guest write helpers to invoke an architecture specific hooks (kvm_arch_write_gfn_{begin,end}) to track the write access from the hypervisor. When SEV-SNP is enabled, the guest uses the PAGE_STATE vmgexit to ask the hypervisor to change the page state from shared to private or vice versa. While changing the page state to private, use the kvm_host_write_track_is_active() to check whether the page is being tracked for the host write access (i.e either mapped or kvm_write_guest is in progress). If its tracked, then do not change the page state. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 6 +++ arch/x86/kvm/svm/sev.c | 51 +++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 + arch/x86/kvm/svm/svm.h | 1 + arch/x86/kvm/x86.c | 78 +++++++++++++++++++++++++++++++++ include/linux/kvm_host.h | 3 ++ virt/kvm/kvm_main.c | 21 +++++++-- 7 files changed, 159 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 59185b6bc82a..678992e9966a 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -865,10 +865,13 @@ struct kvm_lpage_info { int disallow_lpage; }; +bool kvm_host_write_track_is_active(struct kvm *kvm, gfn_t gfn); + struct kvm_arch_memory_slot { struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES]; struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1]; unsigned short *gfn_track[KVM_PAGE_TRACK_MAX]; + unsigned short *host_write_track[KVM_PAGE_TRACK_MAX]; }; /* @@ -1393,6 +1396,9 @@ struct kvm_x86_ops { void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector); void *(*alloc_apic_backing_page)(struct kvm_vcpu *vcpu); int (*get_tdp_max_page_level)(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); + + void (*write_page_begin)(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); + void (*write_page_end)(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index ddcbae37de4f..d30419e91288 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2862,6 +2862,19 @@ static int snp_make_page_shared(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, return rmpupdate(pfn_to_page(pfn), &val); } +static inline bool kvm_host_write_track_gpa_range_is_active(struct kvm *kvm, + gpa_t start, gpa_t end) +{ + while (start < end) { + if (kvm_host_write_track_is_active(kvm, gpa_to_gfn(start))) + return 1; + + start += PAGE_SIZE; + } + + return false; +} + static int snp_make_page_private(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level) { struct kvm_sev_info *sev = &to_kvm_svm(vcpu->kvm)->sev_info; @@ -2873,6 +2886,14 @@ static int snp_make_page_private(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn if (!e) return -EINVAL; + /* + * If the GPA is tracked for the write access then do not change the + * page state from shared to private. + */ + if (kvm_host_write_track_gpa_range_is_active(vcpu->kvm, + gpa, gpa + page_level_size(level))) + return -EBUSY; + /* Log if the entry is validated */ if (rmpentry_validated(e)) pr_warn_ratelimited("Asked to make a pre-validated gpa %llx private\n", gpa); @@ -3446,3 +3467,33 @@ int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level) return min_t(uint32_t, level, max_level); } + +void sev_snp_write_page_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn) +{ + struct rmpentry *e; + int level, rc; + kvm_pfn_t pfn; + + if (!sev_snp_guest(kvm)) + return; + + pfn = gfn_to_pfn(kvm, gfn); + if (is_error_noslot_pfn(pfn)) + return; + + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &level); + if (unlikely(!e)) + return; + + /* + * A hypervisor should never write to the guest private page. A write to the + * guest private will cause an RMP violation. If the guest page is private, + * then make it shared. + */ + if (rmpentry_assigned(e)) { + pr_err("SEV-SNP: write to guest private gfn %llx\n", gfn); + rc = snp_make_page_shared(kvm_get_vcpu(kvm, 0), + gfn << PAGE_SHIFT, pfn, PG_LEVEL_4K); + BUG_ON(rc != 0); + } +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 2632eae52aa3..4ff6fc86dd18 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4577,6 +4577,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .alloc_apic_backing_page = svm_alloc_apic_backing_page, .get_tdp_max_page_level = sev_get_tdp_max_page_level, + + .write_page_begin = sev_snp_write_page_begin, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index af4cce39b30f..e0276ad8a1ae 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -576,6 +576,7 @@ void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu); void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); +void sev_snp_write_page_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); /* vmenter.S */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index bbc4e04e67ad..1398b8021982 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9076,6 +9076,48 @@ void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); } +static void update_gfn_track(struct kvm_memory_slot *slot, gfn_t gfn, + enum kvm_page_track_mode mode, short count) +{ + int index, val; + + index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K); + + val = slot->arch.host_write_track[mode][index]; + + if (WARN_ON(val + count < 0 || val + count > USHRT_MAX)) + return; + + slot->arch.host_write_track[mode][index] += count; +} + +bool kvm_host_write_track_is_active(struct kvm *kvm, gfn_t gfn) +{ + struct kvm_memory_slot *slot; + int index; + + slot = gfn_to_memslot(kvm, gfn); + if (!slot) + return false; + + index = gfn_to_index(gfn, slot->base_gfn, PG_LEVEL_4K); + return !!READ_ONCE(slot->arch.host_write_track[KVM_PAGE_TRACK_WRITE][index]); +} +EXPORT_SYMBOL_GPL(kvm_host_write_track_is_active); + +void kvm_arch_write_gfn_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn) +{ + update_gfn_track(slot, gfn, KVM_PAGE_TRACK_WRITE, 1); + + if (kvm_x86_ops.write_page_begin) + kvm_x86_ops.write_page_begin(kvm, slot, gfn); +} + +void kvm_arch_write_gfn_end(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn) +{ + update_gfn_track(slot, gfn, KVM_PAGE_TRACK_WRITE, -1); +} + void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) { if (!lapic_in_kernel(vcpu)) @@ -10896,6 +10938,36 @@ void kvm_arch_destroy_vm(struct kvm *kvm) kvm_hv_destroy_vm(kvm); } +static void kvm_write_page_track_free_memslot(struct kvm_memory_slot *slot) +{ + int i; + + for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) { + kvfree(slot->arch.host_write_track[i]); + slot->arch.host_write_track[i] = NULL; + } +} + +static int kvm_write_page_track_create_memslot(struct kvm_memory_slot *slot, + unsigned long npages) +{ + int i; + + for (i = 0; i < KVM_PAGE_TRACK_MAX; i++) { + slot->arch.host_write_track[i] = + kvcalloc(npages, sizeof(*slot->arch.host_write_track[i]), + GFP_KERNEL_ACCOUNT); + if (!slot->arch.host_write_track[i]) + goto track_free; + } + + return 0; + +track_free: + kvm_write_page_track_free_memslot(slot); + return -ENOMEM; +} + void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) { int i; @@ -10969,8 +11041,14 @@ static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, if (kvm_page_track_create_memslot(slot, npages)) goto out_free; + if (kvm_write_page_track_create_memslot(slot, npages)) + goto e_free_page_track; + return 0; +e_free_page_track: + kvm_page_track_free_memslot(slot); + out_free: for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { kvfree(slot->arch.rmap[i]); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 2f34487e21f2..f22e22cd2179 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1550,6 +1550,9 @@ static inline long kvm_arch_vcpu_async_ioctl(struct file *filp, void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, unsigned long start, unsigned long end); +void kvm_arch_write_gfn_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); +void kvm_arch_write_gfn_end(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); + #ifdef CONFIG_HAVE_KVM_VCPU_RUN_PID_CHANGE int kvm_arch_vcpu_run_pid_change(struct kvm_vcpu *vcpu); #else diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6b4feb92dc79..bc805c15d0de 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -160,6 +160,14 @@ __weak void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, { } +__weak void kvm_arch_write_gfn_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn) +{ +} + +__weak void kvm_arch_write_gfn_end(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn) +{ +} + bool kvm_is_zone_device_pfn(kvm_pfn_t pfn) { /* @@ -2309,7 +2317,8 @@ static void kvm_cache_gfn_to_pfn(struct kvm_memory_slot *slot, gfn_t gfn, cache->generation = gen; } -static int __kvm_map_gfn(struct kvm_memslots *slots, gfn_t gfn, +static int __kvm_map_gfn(struct kvm *kvm, + struct kvm_memslots *slots, gfn_t gfn, struct kvm_host_map *map, struct gfn_to_pfn_cache *cache, bool atomic) @@ -2361,20 +2370,22 @@ static int __kvm_map_gfn(struct kvm_memslots *slots, gfn_t gfn, map->pfn = pfn; map->gfn = gfn; + kvm_arch_write_gfn_begin(kvm, slot, map->gfn); + return 0; } int kvm_map_gfn(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map, struct gfn_to_pfn_cache *cache, bool atomic) { - return __kvm_map_gfn(kvm_memslots(vcpu->kvm), gfn, map, + return __kvm_map_gfn(vcpu->kvm, kvm_memslots(vcpu->kvm), gfn, map, cache, atomic); } EXPORT_SYMBOL_GPL(kvm_map_gfn); int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map) { - return __kvm_map_gfn(kvm_vcpu_memslots(vcpu), gfn, map, + return __kvm_map_gfn(vcpu->kvm, kvm_vcpu_memslots(vcpu), gfn, map, NULL, false); } EXPORT_SYMBOL_GPL(kvm_vcpu_map); @@ -2412,6 +2423,8 @@ static void __kvm_unmap_gfn(struct kvm *kvm, else kvm_release_pfn(map->pfn, dirty, NULL); + kvm_arch_write_gfn_end(kvm, memslot, map->gfn); + map->hva = NULL; map->page = NULL; } @@ -2612,7 +2625,9 @@ static int __kvm_write_guest_page(struct kvm *kvm, addr = gfn_to_hva_memslot(memslot, gfn); if (kvm_is_error_hva(addr)) return -EFAULT; + kvm_arch_write_gfn_begin(kvm, memslot, gfn); r = __copy_to_user((void __user *)addr + offset, data, len); + kvm_arch_write_gfn_end(kvm, memslot, gfn); if (r) return -EFAULT; mark_page_dirty_in_slot(kvm, memslot, gfn); From patchwork Wed Jun 2 14:10:53 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294747 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E10FFC47083 for ; Wed, 2 Jun 2021 14:15:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id CB0F0613EE for ; Wed, 2 Jun 2021 14:15:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231618AbhFBORd (ORCPT ); Wed, 2 Jun 2021 10:17:33 -0400 Received: from mail-bn8nam12on2053.outbound.protection.outlook.com ([40.107.237.53]:10945 "EHLO NAM12-BN8-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231906AbhFBOPa (ORCPT ); Wed, 2 Jun 2021 10:15:30 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=NL5ebzugTnu1zo5h4aZi6PAuNEWELYbz2g/2SQYVOrk7SSR5JI0RBPoKcQmDdL0mfmTEVzL6nlEm3Slp/eq1PX48bKnvYZgeza7Mprjtn5HE+mFqUV1IQTAl+i8gVYG4qp63eYRG1f6bIVFnn2VdQA2cT7VQhFkFAC6owioyr4o0APphbQYudJwM9ICDhrk0IXM++EwEjEEUwPnUIc3+N/284tKR10an6LIO/Ko2auMZb0GbjCeGJY/ZNxVEPdJt29ovrYVtlBQx9wLVrtUhqt5/S3MSP46H5MR6ZTUn6fg8L4aAr+LTbJJ8HijBNPCSrybaCjeQ373wRTVS451gcQ== 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-SenderADCheck; bh=amc345V0ABYnWmTuzWs5XId5+/6yv4cUgrvfJJyi0mk=; b=kzxKmexNo/NYdQ130Z4WZNUbKd7Kl3vrpEyUqSkQJiVEbGITy+pkyNzsA0qn1JDi2sSravUDp3dazf+ODlnSbw+dOIRKrz4NRhx+yveTCP2aj/pZo+BamzkTAb3Rn13aCMmJxWKF/uHnx9Oyc3DY5DT5UaXVftvwI77BJPgEtUHobl5l4a4TDAtnbUb7tjC2R+4NGCEIgHbxG/sxM0n3jgTB+YqCDtGzMKSIvnIkL7/o0vhpUi2FgHIYP0yDKZMcHHVQcTZ7bNLrCXmSak3Rgvfa+ZWhWethqkTmibO+o6ysapLVIy9G8B1Klq/+fuikNz1PUpdOyrO4QglSoKmklQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=amc345V0ABYnWmTuzWs5XId5+/6yv4cUgrvfJJyi0mk=; b=aaWDW4R1GPn46l0IHHVVXwMAX15jBo80y2X30dOEmgArgyikU0iTLmFTLMl1FsL8W55xbipYeIclGsb+Tl9kW9BXTly5suQMggWKgBzqd95yRq9xjc7iglL7ILyWfdHg9KgK02STvNffkndsAmhBZSyd1hM0oFMdjrDhMXY7dLs= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:39 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:39 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 33/37] KVM: X86: Export the kvm_zap_gfn_range() for the SNP use Date: Wed, 2 Jun 2021 09:10:53 -0500 Message-Id: <20210602141057.27107-34-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:10 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 809a9246-b86a-4dbf-295d-08d925d069ee X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UEbKIGTWZvDIY4UrzbuASNP51O3jU5QlTTAAdKyHKTTZoVU5wMqsfE1AfPi94hXgcuR0+9gtfAPREExC63Tx9mRZ6qjF9qcOjVVR2WgZE7wUPWsNIATkwQ6yhZuCvYvQ6k30ueNAx5lqbDul8Mf2GZ6miwHpjl/FLhAWrO/EZDmgh+I+HwZX15k3ju2p9KYnLDABfAN2nTgOuLQOdhlQr+4iT/cAfnbSv6qZTBNOVyyZtSmVFmGIJa3PZ4EG2DQS1euitDiOeZinvwb+Huchb6tqMoPzouh9wqvY46IQkO06dStK5xacE1glsnrq0K3Enf1PTKGa8JyUODWYTOGOF29y0hBBvUbDerCFXBEEXqv0C6AJLeh1rSuzBmsqRgExYEq+cksiEMAeuUCSRuMmueQCq8GA7uhYvrt9ZC4QVjXgfL/64V07iOiX+7eCKoyA8fPgnwIs17Xcsy+jqzHkcz0LzbCtIDhAHb6tGgIw+08C2nRgo27P/xW8PPtSX4x1VybeW9x8OjBWjan/wvcox+RcIWCNSFxMXO+NAqEygYtCxJF1SC10z4PqlGP/mEDnuL2eCeleVq8uDklaepQuCDAubD9OCa00H0nKYvym5KalAxcMUqYIPSpH80aVS1ShIF525JHID+qRvBRwYq7JyQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: bstt14rqnUMAcyEdFCScnwLh+Moxevg6CBWKOAXmuxkdDOjb8lTvfr4qLWepcoyTvBkKAGs9/c0FG7CElslRtI4c7UZWPu4kQh/S89bRcfui9eZ1PGSVTO8GWP+zK0xEVkl3Yoni45e1I3/5UwwEtl8beyYUxxVtv4pWTT+IlCoftqDLrcV6dyKTrE1XD5HatN1gpTbBNPrdCeFvOEnLt1947jHcKcTqcxaH+UaXbNQx/DJ0+Yl58S6vgCBNL5LBEPPIsiQ6velkHQbY+CHoXZgMj8EQK9s7bu5v4lKcU38k3fo0I0DEGHDv197sqd47Sa3azG+CLdgDZieWzpPIyjxeRFLonC2kC8cPqynFVSNJlfR4W9R8A9Jk5mFDeor6zP1fdZsc3/qYcgIGucm6YRvelCa8fLxXianqdbNgjA3Sc/CtRO8ydGAVLYOCchmioy4ofH01XM4J0H9b2r6U30+FFeLQ2A5VQ5/g8jw6RYyZzW9aiVomvOlmvxznzniz7WksOqLmBnGN73lDEVr4FCxU2s/e+DIth89Iv/oe8Iih2Gb+kZmmY5WEt/YwCdZfVSx1U06xOd2cqJgjUMffZqWPwSyG+r0IU4C3j6HSpxa0wwrB0DsMloN8hLlu2lPVuoKIaokMuvlQx5ZFng8ankYOFZRi4cHwmrf20nNH1mF0v1BJ2rHJBXhW4cS2ao7QZH5CbdddDQjYAaEaphszoodliiNP8aWpsMAwieB3NBs/ge89MLTQiLrvEeheykbuAAl8hJsMUHU5RtWxakbi9mA86WDFFYqrVofGoeMjlXLp9fXEjjo536gEVVskZX/2xy5McESMuQW2mvC0Bq9+0AVoxVeJo0Kr03A0lYp74kc2wWXsrUigzwNrO6KuAAVNx0IkvcWwdT46wEIz6fux6HH9OmmSMiPNJoMcpN2a1/aPzmFS/MIQel4iC0LXcXU6riYKGLSpnhgGEBxkfxyKsKvat6o9KowCYeJhjT3nEKAMnNPygObM/JqMxEAamJBKnsM4JrUxRLZ2kM7mXDZL44gLDO8ckZ3LnmDpnP6N7KAL9a0CV+eZinwtkk1MydNODQlW5wSyDkX+8ds1oXWRRatxAoQgiLjkuxn2LNFDJ9fgOPoVJhdeqX0/beoQDy+Sm5OMMwUvzmLSLt9bastWcxMfrpQ0QtQSP+cRU4M7kj2Dvk6dDc94Bm02mrH7t1TT772NozMPvSryngbbyQ6umMQRgn6YTk+9xzfz8bjNn4fRLj8Mb97uJ9ASjUOkUFYvqYrhrRnZgBGEc6KspSaMQgzJjqzqJLCi4UUtQ5yeFfTMkqD8JeyQhVXr0vpH0wlH X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 809a9246-b86a-4dbf-295d-08d925d069ee X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:11.6073 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: vmBKTPoW/KyDJwI3VH6Udw3D/ipsW4FRm0eFjZBkXybxm7XbxhpYEASvHDJN4FS9RnEZRijcdJ6LD00YXeJF+w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org While resolving the RMP page fault, we may run into cases where the page level between the RMP entry and TDP does not match and the 2M RMP entry must be split into 4K RMP entries. Or a 2M TDP page need to be broken into multiple of 4K pages. To keep the RMP and TDP page level in sync, we will zap the gfn range after splitting the pages in the RMP entry. The zap should force the TDP to gets rebuilt with the new page level. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 2 ++ arch/x86/kvm/mmu.h | 2 -- arch/x86/kvm/mmu/mmu.c | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 678992e9966a..46323af09995 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1490,6 +1490,8 @@ void kvm_mmu_zap_all(struct kvm *kvm); void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm, u64 gen); unsigned long kvm_mmu_calculate_default_mmu_pages(struct kvm *kvm); void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned long kvm_nr_mmu_pages); +void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); + int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3); bool pdptrs_changed(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h index 147e76ab1536..eec62011bb2e 100644 --- a/arch/x86/kvm/mmu.h +++ b/arch/x86/kvm/mmu.h @@ -228,8 +228,6 @@ static inline u8 permission_fault(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, return -(u32)fault & errcode; } -void kvm_zap_gfn_range(struct kvm *kvm, gfn_t gfn_start, gfn_t gfn_end); - int kvm_arch_write_log_dirty(struct kvm_vcpu *vcpu); int kvm_mmu_post_init_vm(struct kvm *kvm); diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index 4abc0dc49d55..e60f54455cdc 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5657,6 +5657,7 @@ static bool kvm_mmu_zap_collapsible_spte(struct kvm *kvm, return need_tlb_flush; } +EXPORT_SYMBOL_GPL(kvm_zap_gfn_range); void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm, const struct kvm_memory_slot *memslot) From patchwork Wed Jun 2 14:10:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294755 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A9E7C4708F for ; Wed, 2 Jun 2021 14:16:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E6B21613EA for ; Wed, 2 Jun 2021 14:16:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232145AbhFBORy (ORCPT ); Wed, 2 Jun 2021 10:17:54 -0400 Received: from mail-dm3nam07on2076.outbound.protection.outlook.com ([40.107.95.76]:3297 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S231732AbhFBOPv (ORCPT ); Wed, 2 Jun 2021 10:15:51 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jFcKEhWCuaSBCNJo0VrkYdCOpxSgT56yQMpim9jIvemXFDg6yJA7JC3aH3JNWdErX6rUlE159Pzm3aerOdxkiYIGL0g62jHeY8XbJduhJl2nw1iQdXzf1RgTE9XO9E1rjIOBhxYjSueIDKGt/ACOJOjRBITBwB9aznH3AXQl4dPTGbCb89BdSBTqFrpeGhPeFhIdCbYPA97JYIT4K/hPQFHqyc7aZI5CpA5NFL2vXV4qjqyQHj4WnRcpGxBSkFrdNcrkCsRILzw4QGmrM/kfYPv95VQMoLaNlNZRy+bTw7sYGkSFrsmHtWR0+r6rLQA5gtWeY3BdVkV5TxBzYEebXg== 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-SenderADCheck; bh=2Tl9aw51wwJjspRUscIWdOdkee0xpKTLD8cxKdNcEOo=; b=StZpQQZHgJvHvqcV0n4NdrjmCrGq4oO/A0gvLeUkSeOflwNnvWw0bKQgHSYmhgHrG4BUoxiyVn96hqpwKswdKUDX+LWNC56qJO+O6Lp0O/8/4RSLxRjxxknGmhumJJ1I1xb/kkchDkA+MfLFJJeHGsKBh2QLSUWkABM0pJUO/5T2wt3iQcPA2CTBBVn0jNbP62O49Sq4s9AdfIJ7gMkvJds5WonxFldTLXfwSXFz5DThWYgLsYZSIVfy3PleIgvGxw4bzv1YTH+2STdaHvuR0vYSy95z39tJbW5UIpPHS20v7Nx1WpS3YhyJwD0+9APcSH6dfCoSRp3So39hAc2LgQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=2Tl9aw51wwJjspRUscIWdOdkee0xpKTLD8cxKdNcEOo=; b=r+gT8peIdi5kcgnP7INPYGxe9cE+4TR0IjBnX6Ru/Q+girvn3VJcknyuPkoH591S/nw6K8CDtq2iUkFc581Umqfg63kgfUjcHT7uPJiNB0icAuOBRmwvSQ+Cd6BdPyyFyvjF8cmXf8lHglC6Nm0+1xePbgaSNze3XZbxqcyoCVs= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:39 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:39 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 34/37] KVM: SVM: Add support to handle the RMP nested page fault Date: Wed, 2 Jun 2021 09:10:54 -0500 Message-Id: <20210602141057.27107-35-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:11 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5d1c9718-3550-4016-5d59-08d925d06ae5 X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:7691; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sy9CXmzqxENbH9AIfmkNt6Ijvz/C6DTdAc8BcNqUiEj+ZJ58BiwSyY9ZblNtRenEgZ/gCh+xqVMoflXohw0PL587X04HcGnQFJTVfl7sXtFqEW8cXd08N3osjAz/nE131W9gNSEx6cAAfyhEQWJMBo6iXQK6yvQTNGPd+VVRhJUlPiJ38rWMhfNAW0vB0pod09/95a037cj2mCel3qsJsC3cMFpVpmDQdMyPLXfDHVTjK6N2DCm60ik8UCTFrvydeYKr+0lv3MulGvgxzFkFzH+1rPJao2/nJIY3T7W0YPNUjVyfbmMimTxeLkC1aU7obRI0N6VaRY/B0SjBIFOspfaL1TWWt2zNLOTOLZ1G9VSCiIv3eKHe8m+J+Ca7QULKRSD56Swoz+0IXGq7L/mrOQxCaOAJNXfGgNkAaJdqQVJiUXJkfb2odjUYlgTotWxv91/qk3DAQBo0cXBTU83uRdm7vafWEO8A3uxGQxl61BEuIursj5WBx8a1F0CgtHVoaV1f11rQN4vswXNyiboavv8DSojp0ph4U+nNOb2TD/yhNOjUN1/jylEdeWJkCDn2n9BHsDiGsV3B4PEarErlQSOttNL5TonZQNz0DX/ygkgfTt6907cGWmTKWaElyzDdpU7gd1K9w7X0LzqdR4FWSg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: MLbFr/A9BpellT10XMQxzT9FaZCFFd3yEPoUiXgLW27IdSRviwWCLpUtstzfsX2jOgyrW0l3cyozCaIkvsuaP3NgctHH6EKhBS1PJkpZZ1/ln3oZj4hB8bgKNZuA5jETZbDlYNcWV7xEj6siKXs+QhN4KKlNLCRuvm38Ym9wiz2pYwHjnx0aeZp976JRis/07FsotKnOWswk1JNV2592HxpZ+IPm1hUG7uLmBA41DkdmsWZh3dvL1GWnvmBFPMmdD1c2YrmHVRVM8JVEA9HBai0CL8V4DRKE7VTS/hThOSEA8XfmHDzv3+iZ60IgeDLDAsqHi+dB6pa/eWSFQUswa4+bNOKeLkUPm00fqeTEcfuZMz+hJrsgv2A2UTLmDzh+Hb7GhSm0mywM7cZkI5KziS8mjcbaHmEKTuXRYI2QcIYeB4lyLrxHAIonW+NTINkBmefvTYvb9fQku3fgO38z1eKFt/eilViv9amQJI3GhzPnBbEpYeWX62+qOKAo8RCXb7CVyWBLKrZrnu4hJmZDHRXdtu708btVitMYBnodntMkGEvdSRPJbaRykN/B+Xm3AXGjc3IFZjm3si2EvSUQJj5MKSsdt9aoHEQ77/cVUwQiRlQPiWplpsV+LWfomRyAuGdeyWb0eFJfmVLiARQ1ZoUoT3M71m7zFgsJc5ETgpmJzBWXrExPQ+VhVa3s0VehtZmj+i2NMVvVVepKNKFghrt21OdUHDdmU36ALebjsIag0pzvhXej9Z1/KEJGU0EU+WQUv6LCXunfpOPTXancMDES+wNYbPARRxhH3wWfIjSc5EsfrQQa6rNtMlF2eL2q8jSr2EjCWiHNqeAQ94LZS5+c5NEciMRq/IvDS6WEuRQNuq3gPi4ZQ8Wi8plAP2kjDj3Jc4I9DA4GVaq7YKJWeV+rfl+QmooNxE3QeZmmSOVkfz045YrFutNULOXh1XnAMtYK2zttTa2XCsIjQUjkUV/cZURW1NSdrkJ6h1614asDwzr+JdbppcI7FgPxE8+/Hdq0mB+QzVSfZtHdCClUtB24h+m6haV0NLZ88DU6tww1LvzkPmY5+yeCZkcQEPCDbdzVjSNmKNYjRgg7D/MEPiU5wCeS/5Qgf0CYWzu656mahE2MAHfxYoeWDLqpu3DtwGY9J27w2H3+WL7UL76yzZb5hHgDZxH0n5/JfzzEHvwyHbXrEJt9LMTcvax+vkVVql7tfXtLGhoy10cVzELwNahydzP9ZS+W87gy0+z+cyUaU/57h8GtDjDK/OP7b46/VyXNhtGb4hXDaOImqv38DYXlPGusHZJRjygSd8vtiDH3DajMAQBOwRRiW3DhkhMR X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5d1c9718-3550-4016-5d59-08d925d06ae5 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:12.8276 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: pKVslC/S8XA58FlDTdv95LX1y6UMeeBDxRA6hnoc2Rdq7mpuzdQE93FiVv2d8U2OnvfRhJe6FYqNqL7rKPNMMw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Follow the recommendation from APM2 section 15.36.10 and 15.36.11 to resolve the RMP violation encountered during the NPT table walk. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 3 ++ arch/x86/kvm/mmu/mmu.c | 20 ++++++++++++ arch/x86/kvm/svm/sev.c | 57 +++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/svm/svm.h | 2 ++ 5 files changed, 84 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 46323af09995..117e2e08d7ed 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1399,6 +1399,9 @@ struct kvm_x86_ops { void (*write_page_begin)(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); void (*write_page_end)(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); + + int (*handle_rmp_page_fault)(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, + int level, u64 error_code); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e60f54455cdc..b6a676ba1862 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -5096,6 +5096,18 @@ static void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa, write_unlock(&vcpu->kvm->mmu_lock); } +static int handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, u64 error_code) +{ + kvm_pfn_t pfn; + int level; + + if (unlikely(!kvm_mmu_get_tdp_walk(vcpu, gpa, &pfn, &level))) + return RET_PF_RETRY; + + kvm_x86_ops.handle_rmp_page_fault(vcpu, gpa, pfn, level, error_code); + return RET_PF_RETRY; +} + int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, void *insn, int insn_len) { @@ -5112,6 +5124,14 @@ int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code, goto emulate; } + if (unlikely(error_code & PFERR_GUEST_RMP_MASK)) { + r = handle_rmp_page_fault(vcpu, cr2_or_gpa, error_code); + if (r == RET_PF_RETRY) + return 1; + else + return r; + } + if (r == RET_PF_INVALID) { r = kvm_mmu_do_page_fault(vcpu, cr2_or_gpa, lower_32_bits(error_code), false); diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index d30419e91288..5b033d4c3b92 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3497,3 +3497,60 @@ void sev_snp_write_page_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn BUG_ON(rc != 0); } } + +int snp_handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, + int level, u64 error_code) +{ + struct rmpentry *e; + int rlevel, rc = 0; + bool private; + gfn_t gfn; + + e = snp_lookup_page_in_rmptable(pfn_to_page(pfn), &rlevel); + if (!e) + return 1; + + private = !!(error_code & PFERR_GUEST_ENC_MASK); + + /* + * See APM section 15.36.11 on how to handle the RMP fault for the large pages. + * + * npt rmp access action + * -------------------------------------------------- + * 4k 2M C=1 psmash + * x x C=1 if page is not private then add a new RMP entry + * x x C=0 if page is private then make it shared + * 2M 4k C=x zap + */ + if ((error_code & PFERR_GUEST_SIZEM_MASK) || + ((level == PG_LEVEL_4K) && (rlevel == PG_LEVEL_2M) && private)) { + rc = snp_rmptable_psmash(vcpu, pfn); + goto zap_gfn; + } + + /* + * If it's a private access, and the page is not assigned in the RMP table, create a + * new private RMP entry. + */ + if (!rmpentry_assigned(e) && private) { + rc = snp_make_page_private(vcpu, gpa, pfn, PG_LEVEL_4K); + goto zap_gfn; + } + + /* + * If it's a shared access, then make the page shared in the RMP table. + */ + if (rmpentry_assigned(e) && !private) + rc = snp_make_page_shared(vcpu, gpa, pfn, PG_LEVEL_4K); + +zap_gfn: + /* + * Now that we have updated the RMP pagesize, zap the existing rmaps for + * large entry ranges so that nested page table gets rebuilt with the updated RMP + * pagesize. + */ + gfn = gpa_to_gfn(gpa) & ~(KVM_PAGES_PER_HPAGE(PG_LEVEL_2M) - 1); + kvm_zap_gfn_range(vcpu->kvm, gfn, gfn + 512); + + return 0; +} diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 4ff6fc86dd18..32e35d396508 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -4579,6 +4579,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .get_tdp_max_page_level = sev_get_tdp_max_page_level, .write_page_begin = sev_snp_write_page_begin, + + .handle_rmp_page_fault = snp_handle_rmp_page_fault, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index e0276ad8a1ae..ccdaaa4e1fb1 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -577,6 +577,8 @@ void sev_es_unmap_ghcb(struct vcpu_svm *svm); struct page *snp_safe_alloc_page(struct kvm_vcpu *vcpu); int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); void sev_snp_write_page_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); +int snp_handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, + int level, u64 error_code); /* vmenter.S */ From patchwork Wed Jun 2 14:10:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294759 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 87A6DC4709C for ; Wed, 2 Jun 2021 14:16:21 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6C17C613EA for ; Wed, 2 Jun 2021 14:16:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231732AbhFBOSB (ORCPT ); Wed, 2 Jun 2021 10:18:01 -0400 Received: from mail-dm3nam07on2052.outbound.protection.outlook.com ([40.107.95.52]:43296 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232200AbhFBOQF (ORCPT ); Wed, 2 Jun 2021 10:16:05 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DXrreSyN4URmVXvj9DNKWnuq2WyrOcDM49TajfnGkwZW4KO52xnhlyZnyKKdkbYh8bxb+BDPa84MvGRpJiX6Np3NTwOqs9pHFREuvvEG0kfkjSo4O5TQNWUAWWnJp93oVOG0fN//pPv4MWSC7txSgmKXX/yNHIw5QsBxGQzB1HjoJRPV+TQlRflmeVvCyCnpJqHEDiijLKMLhvOBWeJQXJuFIvd9+IDl1MQ784fx8GAYOzV82ew6RbtK5IjZD6hCXEoW6235FKWBLWF8fChJcyT/nRYU7rXa610Eh4IBD0zpQDJJu4SH4fazxicZ8ptZgV87rnikQpKp004Bp4Fn8w== 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-SenderADCheck; bh=xLJXcd9y5+Qgm0yIvuFvVVGuI40kkmVzZ3F9gN7qF84=; b=iWspw2xKm6by2HQrvVLM94fRuxFneLOImUtFOvxqaEicOErEYC/C67eOEPxrkNt3L5wRBQOx5r52v290jYyTBc93qvleA3XWbrTHbb4IdEgwdxByxuQ46IjFuWkmtfxNjbV4OhNgalluufmbFNWA1rw67gk6190yoWVKIKR0TcjEobAWjYT8CGMO9JW2/9XyGzF0mss8CR+J/SDmungnhL1l9LkGwQTO/X2JRPNMZwTLyIK4zm8SIcBYy4J5rryI7XYBbbR8D4EVKIijzaqJD/43N6ot4DeLDfcjtH9i5SPH5EJQT1UwCJLJ3gPe2Q/6MF5TZqzffvitE7M/uGEQNA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=xLJXcd9y5+Qgm0yIvuFvVVGuI40kkmVzZ3F9gN7qF84=; b=z9RbPumaTMn4LClVk1AliefdRZ2pVc5iZu280GENkSojr9XT44exgFU7TCl6EJOAh70O9AZJ5FWprJ3CErj6v1Xjl7sl8TLFvInkijxpZFp4L+rmGA5pPoIXBMDBsKNNteGUjrY2TTPq5eKHs5YK9FL4i4zdQ7v41Wb59CgcK1k= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:40 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:40 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 35/37] KVM: SVM: Provide support for SNP_GUEST_REQUEST NAE event Date: Wed, 2 Jun 2021 09:10:55 -0500 Message-Id: <20210602141057.27107-36-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:13 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 928cb5a8-7659-4746-8e08-08d925d06b8f X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FtdBakzDA1pUtHqZVpLs+lr/pSi5dXWot5UvBHOZGNjzAo7l+OS4wTzs/KKg/qX7X8Zm0hnjn6r6tXSgFBrkwcmAWtHfm1DfyW6gcW27Kk/hgPBFLtJkXsj9tMMHamHS2lSIr2NQ/4iDu9AiDr+FlC/Rg23pe0aJTPK2qQlvTm5QB+uh9s8obPWRMDX0qzm41xRx6RQTySuds0kr9nntJBia3HuS7wNla86mCeth46XcSMukguWfe1Exl2QXsItx122E+qpKJvM0NgXwEt5DA3Sxl3ZCVgikNcrtTfajLCOeHrJdflt10Yvrbc5tqrDv3RiL1+22iCjIwNqbx7V7nfaVW6cHFLSjD7AueWL3/h6wETHnf/kQF1O2iutCrGuhdaT4Grupt5l48t0bVd/Y0XO7MB+FULP1lGIIQatC96BnfiyWgP/bs5vn2rVoVY6hUYaQ8492qIstKas4A0RAWOCJetdRFnZrOJ/lzleeD8oLBwLfehPa9XnIZ3+maK/ArFu3B7gxrrOHmbFCGCzhQCyz0l9jCPu/oeP7Fl5bxGL53gbYTjXT1yjXr9VssIjak1MG9PH28IjTyBrL/zGsxVM6qomxH4dsSc5euAK0T3jQRObq/5RdFU+6MGS0jol+GDy/MAtjU+O3pTaquBu8Kg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: zFxqQVm1BkrAHCAkdibcy/pT1hcZPIeg8dVph93YC5ONL5fZnJ7UUMGgmTfv2U+5Fhq2WXTTKSQXhNlb+nNOAaEn9X2EuN0HnUeFociqrEEHEqjwc9cNtUB7BuZOaC56PG058sfn7g8SAF75qZY99o8h+RFXw0w/+mBBiFi2gbyO6BqTaZmg8h7hHni4mRSku1Tfm+XKZB/jSHMiB085zQZLIWf6z6S9iua+FEH0IUZOOIvZp1T7nMuM1OBnAlT0kvbtJ3GcKGOSHqha1Hqn9SU1OEUlCZsa0Bt8bmYoTODON0ooWOKDKdOseqK1oPeuaPUm2Zg7syCgfI9OHyarkNtuUKyn09oXWWj5b1TiN7tim/A1Y/WBcGkMG/OHRKdjxPw0GLkAd6HCTYiRRkAuXKeg50RzptEkF9mIQJsB65s9HCBCn8TSN8lE4RaUL/qEds9NuOWysMBSRZNpIRBy3o2f543lC7skNtKo4aVj1zLx+haOku7qUcYqqgmkxv712dOMf51R/cKwgrDCoZgS/zDvxcRKGdssK/WwsMjaFLmO4xvjYWDqpS4UPUbHND/f+TujAgkfpHATuphqFISxQvsxR8MJliQ/4pz/aihZTvg3q8q/SQgFjcBKnvSLOzomm9gn3zt7WSeXw3md6mM3q5XSrBKVPLazyX0iQkffcyK0ezXwvehlf3+eYS2TjQNnfPxa8YX48TQUiLjvYKnHenvE7AsM4P5YY1D166y1gUMDupKN9BJTFt7SDaCd6F9KP99gf1bte+23Mrn0/N+a7Iv/0V7g4YdK7+UWdfecoPRPgcysBaOK3wBDjppRNK6D+XeRq30VhxjDnYBGU4z+X59HSjEhT+R8EN1QqYJ7kicbi9IC/TB8SDpkAbb04vdcEZf84Bs4RkHU+kfqsNnZ2vyhL2PrS70V+oBadVWusXzq+G5E6TxNIIfAWFC2bmqTivvJyd9TXniCGcUpb6n2Nb5wMog6/2LvyaHnjAh0szlMG6DRvfX5l4QufqXmfTQT2qdxQbxjvLDcb7qNnoqyyLYmHV6pQQ8PXAK0h//JHWLXkZkwYWDbtl/aVUKWuI21nH6668WDnPJqSw2BkNJ6BuCvVQXfo5MnQyaDaPk6FeMf0Y58LfoeGCPvdRjZnxNxxy6tufYHJ5zSifsor6kTviJfHbmuIgaEGRz6X3Q5aW3j2tVaFaKTDGq9ukXusWw0vL4OcFa/1X+fgdhiyVjB12etILupb7LFdfurcjzYJQb0pJecGEK7jwyxiPaH0s+eCk/H/SN6B6qxbnxfmQRgYe/FouDA2pDxEVUpxU8r0XumfaBUJZzWzgk591HqPhkm X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 928cb5a8-7659-4746-8e08-08d925d06b8f X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:13.9680 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: cMWXx808hVBKwjNY11hwaFNvsGOqJoAzVbooWegpHFAcjgnp5t3ufeDVtINsolMlO8103O7o1DNS+6kIclhZ1g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Version 2 of GHCB specification added the support SNP Guest Request Message NAE event. The event allows for an SEV-SNP guest to make request to the SEV-SNP firmware through hypervisor using the SNP_GUEST_REQUEST API define in the SEV-SNP firmware specification. The SNP_GUEST_REQUEST requires two unique pages, one page for the request and one page for the response. The response page need to be in the firmware state. The GHCB specification says that both the pages need to be in the hypervisor state but before executing the SEV-SNP command the response page need to be in the firmware state. In order to minimize the page state transition during the command handling, pre-allocate a firmware page on guest creation. Use the pre-allocated firmware page to complete the command execution and copy the result in the guest response page. Ratelimit the handling of SNP_GUEST_REQUEST NAE to avoid the possibility of a guest creating a denial of service attack aginst the SNP firmware. Now that KVM supports all the VMGEXIT NAEs required for the base SEV-SNP feature, set the hypervisor feature to advertise it. Signed-off-by: Brijesh Singh --- arch/x86/kvm/svm/sev.c | 101 +++++++++++++++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.h | 5 +- 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5b033d4c3b92..5718e2e07788 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -1515,6 +1516,7 @@ static int sev_receive_finish(struct kvm *kvm, struct kvm_sev_cmd *argp) static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) { + struct kvm_sev_info *sev = &to_kvm_svm(kvm)->sev_info; struct sev_data_snp_gctx_create data = {}; void *context; int rc; @@ -1524,14 +1526,24 @@ static void *snp_context_create(struct kvm *kvm, struct kvm_sev_cmd *argp) if (!context) return NULL; - data.gctx_paddr = __psp_pa(context); - rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); - if (rc) { + /* Allocate a firmware buffer used during the guest command handling. */ + sev->snp_resp_page = snp_alloc_firmware_page(GFP_KERNEL_ACCOUNT); + if (!sev->snp_resp_page) { snp_free_firmware_page(context); return NULL; } + data.gctx_paddr = __psp_pa(context); + rc = __sev_issue_cmd(argp->sev_fd, SEV_CMD_SNP_GCTX_CREATE, &data, &argp->error); + if (rc) + goto e_free; + return context; + +e_free: + snp_free_firmware_page(context); + snp_free_firmware_page(sev->snp_resp_page); + return NULL; } static int snp_bind_asid(struct kvm *kvm, int *error) @@ -1599,6 +1611,9 @@ static int snp_launch_start(struct kvm *kvm, struct kvm_sev_cmd *argp) if (rc) goto e_free_context; + /* Used for rate limiting SNP guest message request, use the default settings */ + ratelimit_default_init(&sev->snp_guest_msg_rs); + return 0; e_free_context: @@ -2196,6 +2211,9 @@ static int snp_decommission_context(struct kvm *kvm) snp_free_firmware_page(sev->snp_context); sev->snp_context = NULL; + /* Free the response page. */ + snp_free_firmware_page(sev->snp_resp_page); + return 0; } @@ -2641,6 +2659,7 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) case SVM_VMGEXIT_UNSUPPORTED_EVENT: case SVM_VMGEXIT_HV_FT: case SVM_VMGEXIT_PSC: + case SVM_VMGEXIT_GUEST_REQUEST: break; default: goto vmgexit_err; @@ -3031,6 +3050,76 @@ static unsigned long snp_handle_page_state_change(struct vcpu_svm *svm, struct g return rc ? map_to_psc_vmgexit_code(rc) : 0; } +static void snp_handle_guest_request(struct vcpu_svm *svm, struct ghcb *ghcb, + gpa_t req_gpa, gpa_t resp_gpa) +{ + struct sev_data_snp_guest_request data = {}; + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm *kvm = vcpu->kvm; + kvm_pfn_t req_pfn, resp_pfn; + struct kvm_sev_info *sev; + int rc, err = 0; + + if (!sev_snp_guest(vcpu->kvm)) { + rc = -ENODEV; + goto e_fail; + } + + sev = &to_kvm_svm(kvm)->sev_info; + + if (!__ratelimit(&sev->snp_guest_msg_rs)) { + pr_info_ratelimited("svm: too many guest message requests\n"); + rc = -EAGAIN; + goto e_fail; + } + + if (!IS_ALIGNED(req_gpa, PAGE_SIZE) || !IS_ALIGNED(resp_gpa, PAGE_SIZE)) { + pr_err_ratelimited("svm: guest request (%#llx) or response (%#llx) is not page aligned\n", + req_gpa, resp_gpa); + rc = -EINVAL; + goto e_fail; + } + + req_pfn = gfn_to_pfn(kvm, gpa_to_gfn(req_gpa)); + if (is_error_noslot_pfn(req_pfn)) { + pr_err_ratelimited("svm: guest request invalid gpa=%#llx\n", req_gpa); + rc = -EINVAL; + goto e_fail; + } + + resp_pfn = gfn_to_pfn(kvm, gpa_to_gfn(resp_gpa)); + if (is_error_noslot_pfn(resp_pfn)) { + pr_err_ratelimited("svm: guest response invalid gpa=%#llx\n", resp_gpa); + rc = -EINVAL; + goto e_fail; + } + + data.gctx_paddr = __psp_pa(sev->snp_context); + data.req_paddr = __sme_set(req_pfn << PAGE_SHIFT); + data.res_paddr = __psp_pa(sev->snp_resp_page); + + mutex_lock(&kvm->lock); + + rc = sev_issue_cmd(kvm, SEV_CMD_SNP_GUEST_REQUEST, &data, &err); + if (rc) { + mutex_unlock(&kvm->lock); + + /* If we have a firmware error code then use it. */ + if (err) + rc = err; + + goto e_fail; + } + + /* Copy the response after the firmware returns success. */ + rc = kvm_write_guest(kvm, resp_gpa, sev->snp_resp_page, PAGE_SIZE); + + mutex_unlock(&kvm->lock); + +e_fail: + ghcb_set_sw_exit_info_2(ghcb, rc); +} + static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) { struct vmcb_control_area *control = &svm->vmcb->control; @@ -3284,6 +3373,12 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ghcb_set_sw_exit_info_2(ghcb, rc); break; } + case SVM_VMGEXIT_GUEST_REQUEST: { + snp_handle_guest_request(svm, ghcb, control->exit_info_1, control->exit_info_2); + + ret = 1; + break; + } case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index ccdaaa4e1fb1..d4efcda3070d 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -68,6 +69,8 @@ struct kvm_sev_info { struct kvm *enc_context_owner; /* Owner of copied encryption context */ struct misc_cg *misc_cg; /* For misc cgroup accounting */ void *snp_context; /* SNP guest context page */ + void *snp_resp_page; /* SNP guest response page */ + struct ratelimit_state snp_guest_msg_rs; /* Rate limit the SNP guest message */ }; struct kvm_svm { @@ -550,7 +553,7 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); #define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED 0 +#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP extern unsigned int max_sev_asid; From patchwork Wed Jun 2 14:10:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294767 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 301F2C4708F for ; Wed, 2 Jun 2021 14:16:59 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1BB4961242 for ; Wed, 2 Jun 2021 14:16:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231593AbhFBOSk (ORCPT ); Wed, 2 Jun 2021 10:18:40 -0400 Received: from mail-dm3nam07on2052.outbound.protection.outlook.com ([40.107.95.52]:27104 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S230428AbhFBOQj (ORCPT ); Wed, 2 Jun 2021 10:16:39 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Hi05moGjJF7sjNfbuyqOfxQs97Fmnnxy1C0vVqYLDLW5GTO69BnJ4vjDiYk81Gi/paoVsWYQoL27DTAGGZZgp2sO77tCd9ks2ach1fo8geNt1HFWpY2EgbIjlzlz5ro+lgIDOsz8uZcSSz9dVh+5YGrSCHoaoU6Wsy6GnITzPNNpVr7R0waRgINgqD7oXvgUSNVMxPiurrucu5pGjmyqi4pUMcSc/zw66U1q6xpe+znh8G1EnqqM5DkrVIMja9IPpsnsLuBO/vnKCo8PQCJzvoEZuqOM3lJ0Vi+IvAkDtUI1ZJtW8rhdJXCyqm+TeLZJFjTbxmef8X3hRh73QJ8T0g== 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-SenderADCheck; bh=bPIvRwnNwHdEha3n72i7CJC71O88LkOUXxlGm7zAFkQ=; b=mGge8w/zGpQVRUkjzknnVZFYKu+uT/sUdti/8eBRhn0qjgbobiIW1oXKbPaDh4NRdZy/Hl85ZCJ2GAcZjZt/2zCxrwJ6SalKfRfZVI0hYDNNVVKJxIMKLFSOROoQdVV28zAzPBaiWSdPhPSQYOBLacyaQGkJZjhD9oKa4s606UKAox9ddcam2CEHP8vcEMdDdR+BqRlTJpdimCnGIbFbBOy1aQeGqm/CeXA6CW0l9Lg08NF1evg3n0uqFteRCJCpuEXTXb0DtdbCcNTyV6iV6yboXZTvYEmdYHVRl1tHZwtat0UkReDxr3rO/+kNnzw64eAQTIlcwkEnUESP8XscQw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=bPIvRwnNwHdEha3n72i7CJC71O88LkOUXxlGm7zAFkQ=; b=YdP0bwDJbYf9WOaM9VRVtd/1Lt6IhbrslL5syRtmUDU/CmBaMZ/4Bj5Nn055hTlAUp6YaMz581/ndLGfJvvMKeTueeENNat5rS7zfjk98NVQLXItR8XNisIvcS/mDK9EjhXQuJH4fLzuR9wpT8qagcAsV+ZadV4uqBoRDKpyr30= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:41 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:41 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov Subject: [PATCH Part2 RFC v3 36/37] KVM: SVM: Use a VMSA physical address variable for populating VMCB Date: Wed, 2 Jun 2021 09:10:56 -0500 Message-Id: <20210602141057.27107-37-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:14 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 094a8188-f328-48c7-f338-08d925d06c33 X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3826; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: W+Y3OI4oA2E4uIZQPKG8+w8L7TE6scjBXj0uZkKba3jyGTU81j+2ZC9dvTKCF70l3wVCSU29KAtWi/2VwA7nLjMxEsQIJuWAI7TjFtxq7oyJgDeV/ExvY+N0wIigu0hzSVlb1jvqwNyexHkwDvgE/33KY/8OeuyBQxBPEyA5uKhFWflm5qLXdO2kM7gisjIh0jO8aOMmpTBwHCGdH0a5/fD9pU/ispRLvjARdKu1aPEXBKn3YNN1QAg29kiMbGk4IezVY0tCh9uZAEkeh8vDquR343VInqdL4dv+NzD2z57G1nZ/Yoa4DbpqW3+9Ab5Y0up9n4o1X2rdmL/pYfthDMzQjGBniWvacqgkkAgl455Fxni3jnqDpiBFu7+//LZe/xJ2lAi4QsEtzfhMhcY7EIUaTPsYHcxeHcQNrKUWy+TJTlnpGGiM8YVl0Odd2hhbxF+E4sCVETzFgbY3bV4/AyYFt2kkCRpFOmg6aWGCobwkwS5M4anY3KfERZExthao6X1CHjo0/R7kEikLI/qJ1WYJBGiT7uUa+jrg8X2LT42TAwBD0f8QLcSctsmLqAsk18jGw9+Fhl7WJp5sn4aC07Wq1nHPmsdr2f5jBxQ7tQgHB+eBdgT6ENTNpUH+j+JoQDNyieAPh2VngthfJH4fww== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: XoneEl81jzW+xpqfSMvmQc9NPyrCIyx8WIvgnK/4JiVXPMrMYJwp3r5+EpbuPrbReS7UZ0jLcIobAWEBpy21Q+qgabhW/RahZp6XF4q0Hg3nZZRbbtOvCpYZS3UZSGmA99NeO61cqQ9rPjZTDt0U00hBM4pZOfXGce2GsenEYx5GMpjizrH3YQcV2uF1j7eNZ59OWilyW3zyqYvLQD/eBM/uOzfcWuVS1De2vqbm6vQI6Qr0j3bpZdQTHlQLUCrshvTGhyh/CvXUz4ask2qFXdFviSWCGok+XO9KoOAVgQXAbmI9HRrFbjPmWuZNtrsAlWg81YVb1AtQc+SyX2A8YAr3aY0p4elO+edhkvH/wLpuTu10G+1X08hfVMLassIyGSy2QLVWIXz8oer3mk61pn4Qki3p1gNt04n3+LbzmUBnAxVh1r1xwpTKLh4g75hTBF4cDyIFlKYJNfcDJVhCWd50AIjEjLriW4VrUgei3u67IuOQvhw6F4Eof6cSaTUae/aE7bxtkUCLzg2lKr3G6HQX7YlV9CwcKDOkUvWTBd6Cg71jvNGOde0cz/cJd0uDkYiMu2RDE/8hy/5+us7Vd/dlnDF7lL/BukQu0inHxbJ/gxrR2eqjT0zhjW/HLGY1EbiqC5HTQ0cwHXzFWBI41EotoLPH3w+ULTWrFBEiySVO1hFLuGNcqfhuNvQ30rOV0LqWZl9mrElJrp4DvgzBP+IeB43CzImK5iqGKceym022xNaCg+lfI2IMy81/t5IZU4iM4S8YadG7bm0fLiMMLXcgz0sK6I3RfVcXIkLfqlLFiHkZhNcwLcxQBa42V/LBnVIzNCd1xOG31cVXUecJ170otAHuQpRnYlbMpAF2tpObjUuA415v8uudcU/QfvAQ2zhaQsmnemNWNAc21Ojyha4oQAYsQwwHbtnPLXprnmdTNdEsXbe2UN6KzgiG/cE+zL19C1NMjv1r/bOJcVsSHBqkOCE4CmPTH8IZ84xFmaVd1Luim6kM+WQ8ycfLe2fesmB7EwTUVa8FHjzgyquIHVsPE+F2t6pDol10qxOHVm7bu2UN6XELTwZdbDy79OzOdhSsPgUZ+/N9N27yXOab2lmb+n09lZ/613EowZgnz/lxYYJ2GZQUTp3f4QUUJpq5Ln8FVYC8xpq0/DEnrieo5mr40ULAjNUUviLsNbYRzdYQZp69fB6ueEivyZYp0ikeT3i3bUVf1gs7fTs4xJKrTgOTaddcbFGiwXlBll4YCMS4Nbz+He3J3X03QdviGTUVxG6WeoyNgkBEOp0G1du2VYZMWC9hJG8XCvdmrXR62nm92z00LwadxcvKwyBmrZ9W X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 094a8188-f328-48c7-f338-08d925d06c33 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:15.0763 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: jzjZDUR2BUltO9e6yHECiWCVLRpdQv7uZ4VkH/M/4qUiAEITyY+RJt2y1jTCS45jwzi8/+97XMTmHq36VlXswQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Tom Lendacky In preparation to support SEV-SNP AP Creation, use a variable that holds the VMSA physical address rather than converting the virtual address. This will allow SEV-SNP AP Creation to set the new physical address that will be used should the vCPU reset path be taken. Signed-off-by: Tom Lendacky --- arch/x86/kvm/svm/sev.c | 5 ++--- arch/x86/kvm/svm/svm.c | 9 ++++++++- arch/x86/kvm/svm/svm.h | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 5718e2e07788..047f4dbde99b 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -3409,10 +3409,9 @@ void sev_es_init_vmcb(struct vcpu_svm *svm) /* * An SEV-ES guest requires a VMSA area that is a separate from the - * VMCB page. Do not include the encryption mask on the VMSA physical - * address since hardware will access it using the guest key. + * VMCB page. */ - svm->vmcb->control.vmsa_pa = __pa(svm->vmsa); + svm->vmcb->control.vmsa_pa = svm->vmsa_pa; /* Can't intercept CR register access, HV can't modify CR registers */ svm_clr_intercept(svm, INTERCEPT_CR0_READ); diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 32e35d396508..74bc635c9608 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1379,9 +1379,16 @@ static int svm_create_vcpu(struct kvm_vcpu *vcpu) svm->vmcb01.ptr = page_address(vmcb01_page); svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT); - if (vmsa_page) + if (vmsa_page) { svm->vmsa = page_address(vmsa_page); + /* + * Do not include the encryption mask on the VMSA physical + * address since hardware will access it using the guest key. + */ + svm->vmsa_pa = __pa(svm->vmsa); + } + svm->guest_state_loaded = false; svm_switch_vmcb(svm, &svm->vmcb01); diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index d4efcda3070d..52fd3cf30ad9 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -176,6 +176,7 @@ struct vcpu_svm { /* SEV-ES support */ struct sev_es_save_area *vmsa; + hpa_t vmsa_pa; struct ghcb *ghcb; struct kvm_host_map ghcb_map; bool received_first_sipi; From patchwork Wed Jun 2 14:10:57 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12294857 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D300C47092 for ; Wed, 2 Jun 2021 14:17:16 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 71D4C610A1 for ; Wed, 2 Jun 2021 14:17:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231974AbhFBOS5 (ORCPT ); Wed, 2 Jun 2021 10:18:57 -0400 Received: from mail-dm3nam07on2088.outbound.protection.outlook.com ([40.107.95.88]:21280 "EHLO NAM02-DM3-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S232331AbhFBOQ4 (ORCPT ); Wed, 2 Jun 2021 10:16:56 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Tcxmq0MXypIPh+bbvaOvG+Li4Mmt067NTJ+VgIN6X8pqC3sBaK1guTqqgrkv/5z17FyBInAsFfyz3vDoQ/VatAV3GQpkEY5zDaK1OkYzQcoBnFsbTaP0NJ1CQhAjHtaa6EQRY+C/u6CccNymnEAN9S9K7T7Cq3hWTWauHgs7GrnIU9zLTRTbV1Una6sLoCs8CF9DdLM3PySwpp/YGrcXzVgAdFgXGXYCygmQyzP2XKS2Ejq/tMrHzS7iBofiq5f6XhpciDdedVlfBCU6clnmXq3Jdyg9rJnNfzouhr3KsvIhx9sd2/q5JkhNHAf+eowD4ySUcY6sQNuRSFrrMu9pXg== 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-SenderADCheck; bh=RDVOLUpBzPLb9c3Q4JiKWyJakwdXbSVPLg+04RAfmfY=; b=KoRSXi8C3nwCFI8OalKFQHm6J7EAntyh2KJs3Y6hbeygrbariZ07M/7iLk6FTx1bQV5Xj6xsOrLaFWx1Jc86Vkxth5KbyGso+tPv3I1wxWD33T8PWwgRrUCrFyirG/UttwJAXMxFxLKm5AhymNlaAv/Y4PcL/WLFyPSAH14lC7kltPXJ2+byS8aEQxc58vimYPre7rCDhwu1GIOsVy+zZj3eCxOPp87T7dFooop4Xy1BNU+VgR1t5HfE618kPAtuiDnl8T8ZDBKGN0np72wsQHYrXj14PorlE4x12Qk+PQV1GYWSKO6iYA5c2YL8Pt/iv/RHp8diYRX7ELlN/b0+EA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; 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=RDVOLUpBzPLb9c3Q4JiKWyJakwdXbSVPLg+04RAfmfY=; b=AW42gtYpZEAxp6PgLs7OyE/ZjwX+NrNXqH0A5+1oKaNrv1pHUgoX3wBAWqwTaOgwYWR3Bct+5QBr156Ls9VH6Ym+kohcJYcFTP2VPfgpkPQ1zqIXDzqmzC/xxgvVOwqPoSiky+pGFI8yL3OKpMSPSj0r713AHGuagZWHwnvK9rc= Authentication-Results: kernel.org; dkim=none (message not signed) header.d=none;kernel.org; dmarc=none action=none header.from=amd.com; Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SA0PR12MB4574.namprd12.prod.outlook.com (2603:10b6:806:94::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4173.20; Wed, 2 Jun 2021 14:12:42 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::9898:5b48:a062:db94%6]) with mapi id 15.20.4173.030; Wed, 2 Jun 2021 14:12:42 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-coco@lists.linux.dev, linux-mm@kvack.org, linux-crypto@vger.kernel.org Cc: Thomas Gleixner , Ingo Molnar , Joerg Roedel , Tom Lendacky , "H. Peter Anvin" , Ard Biesheuvel , Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Andy Lutomirski , Dave Hansen , Sergio Lopez , Peter Gonda , Peter Zijlstra , Srinivas Pandruvada , David Rientjes , tony.luck@intel.com, npmccallum@redhat.com, Borislav Petkov , Brijesh Singh Subject: [PATCH Part2 RFC v3 37/37] KVM: SVM: Support SEV-SNP AP Creation NAE event Date: Wed, 2 Jun 2021 09:10:57 -0500 Message-Id: <20210602141057.27107-38-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210602141057.27107-1-brijesh.singh@amd.com> References: <20210602141057.27107-1-brijesh.singh@amd.com> X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0056.namprd11.prod.outlook.com (2603:10b6:806:d0::31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4195.20 via Frontend Transport; Wed, 2 Jun 2021 14:12:15 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9e41fe9e-8cbd-43ba-686d-08d925d06ceb X-MS-TrafficTypeDiagnostic: SA0PR12MB4574: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3173; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: ty6/MjGAFJnUHJUZHz6RofM6ByFDpvzKo/U2lLwa7ShqmKiqJwc6WKLF8qTwCeyqncef1sxjud1UF32XTxY/h1Ii3EUeBU96SnbJivIi50Se7bTmylXrVAUb0Qxwdkx5Ve82cRJPutrXbcIgE7qoBZZXIBlGkDv+Cr/Osib1EefxAOfdRqILyQ6x8JQbLJIWIyKG5ckQPU3oLdY4Q0sugbSMpPzbcesRxW8tZqfi/BvFQwpSuzFpHYa+MHXd1CB2F3h0TjN8YgJm5jHAWkr3unm9eJTblcDWYqLpsb0Vxdri8gkvAS8iEQi/RllEDV1y6Y84GGqUmty9TZ0FmkWCPErUJxT0cB6P58Thjq7/r+eL35QfiT8X1lLiVAQptmvd1mM8PKmlXEd1RB0HnGZfeRo9gKTZAXyn92EtkMH2Li6b7j/DooTcHCfWAlvkNGqe/IH7z/KuxNKmxY+WKfdYQlxRnhrkqmwjJLvPMrepwB4vY6DXwCRm6zAtIW1mg8c6sj5/k6WeoL0UoKw9UNQ5YWbvH/sAgGz5bvEdsYz4fcrFbmTsq7Yo3NO6V8XkTlbnz1qt/hVAC2umlEGcEz6KSkxyO+ABeHo6OqYgdKRH3Dw9zsCmpX612nuIdnl+8haQsWOho5p3mF1/2urOMUJxLQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(366004)(376002)(396003)(136003)(39860400002)(86362001)(8936002)(7696005)(52116002)(956004)(478600001)(2616005)(66476007)(66946007)(2906002)(26005)(186003)(16526019)(7416002)(4326008)(1076003)(316002)(6666004)(38100700002)(38350700002)(66556008)(44832011)(83380400001)(8676002)(5660300002)(30864003)(6486002)(54906003)(36756003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: FZGJxxgmMc1JEUL/qvozXPA9uYIPyKTO6omTlmGE/rg/dS/MVaWN1Da7Z/aKRHYvwbNY3fa2Y2GrdPuwuLN+unVOjbj22TFkV5RiYoqiNpbBBjP9USl8m/TEinOOoQJqJ3J02wOa/BIgbebutLbmxXexmm518gBCLyk1XrtIYtYcyROFROvbyYeYO53rxN55D7PlXVmgvfI/m/Qi5ONqeTu0zZ9nmpzw4PKvEi8acotE2gTe8uMbIJmB/9IrYIHiJl3mP1ySvdJ6pRBknAqHx4JSSwfbTjVrTVxiIv05ITKjmZNeSOthdQeer7P1xf7KD7De7jalE2AlQOqP+6nhFBgj7Nfm6MpY54vwhXrcEBBnPOTyTnlLFzsKSafMlBYpptglaB7G5hbRIJJktHajPGXXuhp4WMo+fBaWdQg9IqWhk/c6uIVmlyAAWcjAh60nyl1HQTHSFyh4lETN0HpnZf/Orls8b4S2dmnmqdKgvjp1D04vdkKlPiq7x7wd8KHJevLDRFpXn3KyoxHF87Rzzqzr5tpHNvdUxOBaC6oyTFm8ak4IlUph9qCcYXwLKLcNREubCGSjW6Qt6emQQrj0xWWjPhk9MMNbZ3kWy8rhnLqdogJWRe8RnrQ87S4cPX+Fy8SypH6QR69m/o/CX0W10F3R7DMR4sdyUbmayfk9qODo+mfaeIu30BB/M3Y6Dzbc/J/GZ+AYtClvcbAUDImGW20eGRABK9TdqagE1jLMool7NBeYmtv5Yr/AxbZYDNKaB/fO5boIwQwvArsmYhMZiVoyz8Hu15Iqm9MtQlw8LRqtUAOiAHDDV86/it6jScscK0QeGbww3lCH1N2rraG5EPAqZ3Hx5wDU12KkmYpV4ypZW8DmwtWr2oEDyHqs2k8SWhBEu4oRFVBRvvlIJkwSVBWPFOiD2k6ZWG91+IOzmr56aAXWJaS9z/I+cn9THqvaWerWwm+Z7dRi6f3qDOdRysmUYvP4Zo9eVBLNxIHKsqAhfrLDcANPqkehcslp3TAh3jUJgwAeYo5yrnD6RKVbUVGM4Doc6XIzAkLq6az0uTMsQB/GUxBh6CyhS4jKykcZkAPLknQ5H7pU3kaJ3GHpuXo9mCBRJbo+JY5lbJ9pxV3CVybK4+t8ciGWc/dvEyOuZ9Mi+ZJ3+xhRTie9J2v2H/oS2Hvn7hEbap+XZUXe6co+mXZdyMAO5HzC1yzwJFlkXmyGP6sGDs74Ij0Z4qtlAwewF2ZuSPOv2mTxMrjKpw+moo0aDHdQbpESknFTZVUbnioL9ZETG0lGAQ9LnVHxbBx9cH1KO3JWQWaPue9UuxJPtcWjzmagTh1WXzu+NeHJ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9e41fe9e-8cbd-43ba-686d-08d925d06ceb X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 02 Jun 2021 14:12:16.2626 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: lwm3lgLmk4Xqq0FEHRSqG3T7Tfcowksul+G28cbKz5BGUxq1avFYSl1h3vvWajcLkKQ9YnCS6F/sQsaOa3RnNQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA0PR12MB4574 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Tom Lendacky Add support for the SEV-SNP AP Creation NAE event. This allows SEV-SNP guests to create and start APs on their own. A new event, KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, is created and used so as to avoid updating the VMSA pointer while the vCPU is running. For CREATE The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID. The GPA is saved in the svm struct of the target vCPU, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to the vCPU and then the vCPU is kicked. For CREATE_ON_INIT: The guest supplies the GPA of the VMSA to be used for the vCPU with the specified APIC ID the next time an INIT is performed. The GPA is saved in the svm struct of the target vCPU. For DESTROY: The guest indicates it wishes to stop the vCPU. The GPA is cleared from the svm struct, the KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event is added to vCPU and then the vCPU is kicked. The KVM_REQ_UPDATE_PROTECTED_GUEST_STATE event handler will be invoked as a result of the event or as a result of an INIT. The handler sets the vCPU to the KVM_MP_STATE_UNINITIALIZED state, so that any errors will leave the vCPU as not runnable. Any previous VMSA pages that were installed as part of an SEV-SNP AP Creation NAE event are un-pinned. If a new VMSA is to be installed, the VMSA guest page is pinned and set as the VMSA in the vCPU VMCB and the vCPU state is set to KVM_MP_STATE_RUNNABLE. If a new VMSA is not to be installed, the VMSA is cleared in the vCPU VMCB and the vCPU state is left as KVM_MP_STATE_UNINITIALIZED to prevent it from being run. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh --- arch/x86/include/asm/kvm_host.h | 3 + arch/x86/include/asm/svm.h | 6 ++ arch/x86/kvm/svm/sev.c | 128 ++++++++++++++++++++++++++++++++ arch/x86/kvm/svm/svm.c | 7 +- arch/x86/kvm/svm/svm.h | 8 +- arch/x86/kvm/x86.c | 11 ++- 6 files changed, 160 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 117e2e08d7ed..881e05b3f74e 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -91,6 +91,7 @@ #define KVM_REQ_MSR_FILTER_CHANGED KVM_ARCH_REQ(29) #define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \ KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_UPDATE_PROTECTED_GUEST_STATE KVM_ARCH_REQ(31) #define CR0_RESERVED_BITS \ (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \ @@ -1402,6 +1403,8 @@ struct kvm_x86_ops { int (*handle_rmp_page_fault)(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level, u64 error_code); + + void (*update_protected_guest_state)(struct kvm_vcpu *vcpu); }; struct kvm_x86_nested_ops { diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 65407b6d35a0..82242d871a88 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -213,6 +213,12 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_NESTED_CTL_SEV_ES_ENABLE BIT(2) #define SVM_SEV_FEATURES_SNP_ACTIVE BIT(0) +#define SVM_SEV_FEATURES_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEATURES_ALTERNATE_INJECTION BIT(4) + +#define SVM_SEV_FEATURES_INT_INJ_MODES \ + (SVM_SEV_FEATURES_RESTRICTED_INJECTION | \ + SVM_SEV_FEATURES_ALTERNATE_INJECTION) struct vmcb_seg { u16 selector; diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 047f4dbde99b..05292985c49d 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -2653,6 +2653,10 @@ static int sev_es_validate_vmgexit(struct vcpu_svm *svm) if (!ghcb_sw_scratch_is_valid(ghcb)) goto vmgexit_err; break; + case SVM_VMGEXIT_AP_CREATION: + if (!ghcb_rax_is_valid(ghcb)) + goto vmgexit_err; + break; case SVM_VMGEXIT_NMI_COMPLETE: case SVM_VMGEXIT_AP_HLT_LOOP: case SVM_VMGEXIT_AP_JUMP_TABLE: @@ -3260,6 +3264,123 @@ static int sev_handle_vmgexit_msr_protocol(struct vcpu_svm *svm) return ret; } +void sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu) +{ + struct vcpu_svm *svm = to_svm(vcpu); + kvm_pfn_t pfn; + + mutex_lock(&svm->snp_vmsa_mutex); + + vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; + + /* Clear use of the VMSA in the sev_es_init_vmcb() path */ + svm->vmsa_pa = 0; + + /* Clear use of the VMSA from the VMCB */ + svm->vmcb->control.vmsa_pa = 0; + + /* Un-pin previous VMSA */ + if (svm->snp_vmsa_pfn) { + kvm_release_pfn_dirty(svm->snp_vmsa_pfn); + svm->snp_vmsa_pfn = 0; + } + + if (svm->snp_vmsa_gpa) { + /* Validate that the GPA is page aligned */ + if (!PAGE_ALIGNED(svm->snp_vmsa_gpa)) + goto e_unlock; + + /* + * The VMSA is referenced by thy hypervisor physical address, + * so retrieve the PFN and pin it. + */ + pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(svm->snp_vmsa_gpa)); + if (is_error_pfn(pfn)) + goto e_unlock; + + svm->snp_vmsa_pfn = pfn; + + /* Use the new VMSA in the sev_es_init_vmcb() path */ + svm->vmsa_pa = pfn_to_hpa(pfn); + svm->vmcb->control.vmsa_pa = svm->vmsa_pa; + + vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; + } else { + vcpu->arch.pv.pv_unhalted = false; + vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; + } + +e_unlock: + mutex_unlock(&svm->snp_vmsa_mutex); +} + +static void sev_snp_ap_creation(struct vcpu_svm *svm) +{ + struct kvm_vcpu *vcpu = &svm->vcpu; + struct kvm_vcpu *target_vcpu; + struct vcpu_svm *target_svm; + unsigned int request; + unsigned int apic_id; + u64 sev_features; + u64 int_inj_mode; + bool kick; + + request = lower_32_bits(svm->vmcb->control.exit_info_1); + apic_id = upper_32_bits(svm->vmcb->control.exit_info_1); + + /* Validate the APIC ID */ + target_vcpu = kvm_get_vcpu_by_id(vcpu->kvm, apic_id); + if (!target_vcpu) + return; + + target_svm = to_svm(target_vcpu); + + /* + * Restricted Injection mode and Alternate Injection mode are + * not supported. + */ + sev_features = vcpu->arch.regs[VCPU_REGS_RAX]; + int_inj_mode = sev_features & SVM_SEV_FEATURES_INT_INJ_MODES; + + kick = true; + + mutex_lock(&target_svm->snp_vmsa_mutex); + + target_svm->snp_vmsa_gpa = 0; + target_svm->snp_vmsa_update_on_init = false; + + switch (request) { + case SVM_VMGEXIT_AP_CREATE_ON_INIT: + if (!int_inj_mode) { + target_svm->snp_vmsa_update_on_init = true; + kick = false; + } + fallthrough; + case SVM_VMGEXIT_AP_CREATE: + if (!int_inj_mode) { + target_svm->snp_vmsa_gpa = svm->vmcb->control.exit_info_2; + break; + } + + vcpu_unimpl(vcpu, "vmgexit: invalid AP injection mode [%#llx] from guest\n", + int_inj_mode); + break; + case SVM_VMGEXIT_AP_DESTROY: + break; + default: + vcpu_unimpl(vcpu, "vmgexit: invalid AP creation request [%#x] from guest\n", + request); + break; + } + + mutex_unlock(&target_svm->snp_vmsa_mutex); + + if (kick) { + kvm_make_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, target_vcpu); + kvm_vcpu_kick(target_vcpu); + } +} + int sev_handle_vmgexit(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -3379,6 +3500,11 @@ int sev_handle_vmgexit(struct kvm_vcpu *vcpu) ret = 1; break; } + case SVM_VMGEXIT_AP_CREATION: + sev_snp_ap_creation(svm); + + ret = 1; + break; case SVM_VMGEXIT_UNSUPPORTED_EVENT: vcpu_unimpl(vcpu, "vmgexit: unsupported event - exit_info_1=%#llx, exit_info_2=%#llx\n", @@ -3453,6 +3579,8 @@ void sev_es_create_vcpu(struct vcpu_svm *svm) set_ghcb_msr(svm, GHCB_MSR_SEV_INFO(GHCB_VERSION_MAX, GHCB_VERSION_MIN, sev_enc_bit)); + + mutex_init(&svm->snp_vmsa_mutex); } void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 74bc635c9608..078a569c85a8 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1304,7 +1304,10 @@ static void svm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) svm->spec_ctrl = 0; svm->virt_spec_ctrl = 0; - if (!init_event) { + if (init_event && svm->snp_vmsa_update_on_init) { + svm->snp_vmsa_update_on_init = false; + sev_snp_update_protected_guest_state(vcpu); + } else { vcpu->arch.apic_base = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE; if (kvm_vcpu_is_reset_bsp(vcpu)) @@ -4588,6 +4591,8 @@ static struct kvm_x86_ops svm_x86_ops __initdata = { .write_page_begin = sev_snp_write_page_begin, .handle_rmp_page_fault = snp_handle_rmp_page_fault, + + .update_protected_guest_state = sev_snp_update_protected_guest_state, }; static struct kvm_x86_init_ops svm_init_ops __initdata = { diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 52fd3cf30ad9..abd3f3cec7cf 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -191,6 +191,11 @@ struct vcpu_svm { bool guest_state_loaded; u64 ghcb_registered_gpa; + + struct mutex snp_vmsa_mutex; + gpa_t snp_vmsa_gpa; + kvm_pfn_t snp_vmsa_pfn; + bool snp_vmsa_update_on_init; /* SEV-SNP AP Creation on INIT-SIPI */ }; struct svm_cpu_data { @@ -554,7 +559,7 @@ void svm_vcpu_unblocking(struct kvm_vcpu *vcpu); #define GHCB_VERSION_MAX 2ULL #define GHCB_VERSION_MIN 1ULL -#define GHCB_HV_FT_SUPPORTED GHCB_HV_FT_SNP +#define GHCB_HV_FT_SUPPORTED (GHCB_HV_FT_SNP | GHCB_HV_FT_SNP_AP_CREATION) extern unsigned int max_sev_asid; @@ -583,6 +588,7 @@ int sev_get_tdp_max_page_level(struct kvm_vcpu *vcpu, gpa_t gpa, int max_level); void sev_snp_write_page_begin(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn); int snp_handle_rmp_page_fault(struct kvm_vcpu *vcpu, gpa_t gpa, kvm_pfn_t pfn, int level, u64 error_code); +void sev_snp_update_protected_guest_state(struct kvm_vcpu *vcpu); /* vmenter.S */ diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 1398b8021982..e9fd59913bc2 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9279,6 +9279,14 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) if (kvm_check_request(KVM_REQ_UPDATE_CPU_DIRTY_LOGGING, vcpu)) static_call(kvm_x86_update_cpu_dirty_logging)(vcpu); + + if (kvm_check_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) { + kvm_x86_ops.update_protected_guest_state(vcpu); + if (vcpu->arch.mp_state != KVM_MP_STATE_RUNNABLE) { + r = 1; + goto out; + } + } } if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win || @@ -11236,7 +11244,8 @@ static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) if (!list_empty_careful(&vcpu->async_pf.done)) return true; - if (kvm_apic_has_events(vcpu)) + if (kvm_apic_has_events(vcpu) || + kvm_test_request(KVM_REQ_UPDATE_PROTECTED_GUEST_STATE, vcpu)) return true; if (vcpu->arch.pv.pv_unhalted)