From patchwork Wed Jul 7 18:14: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: 12363453 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,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 BF623C07E95 for ; Wed, 7 Jul 2021 18:31:00 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6BB4261CBE for ; Wed, 7 Jul 2021 18:31:00 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6BB4261CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5EEB26B006C; Wed, 7 Jul 2021 14:31:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5C5BE6B0070; Wed, 7 Jul 2021 14:31:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 418416B0071; Wed, 7 Jul 2021 14:31:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0126.hostedemail.com [216.40.44.126]) by kanga.kvack.org (Postfix) with ESMTP id 188276B006C for ; Wed, 7 Jul 2021 14:31:00 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 725738248047 for ; Wed, 7 Jul 2021 18:30:59 +0000 (UTC) X-FDA: 78336633438.20.874A74C Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2063.outbound.protection.outlook.com [40.107.96.63]) by imf05.hostedemail.com (Postfix) with ESMTP id 4FA165007518 for ; Wed, 7 Jul 2021 18:30:53 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=k7Zeaj904LzCcuPINeVrrhvBUcbThSf2tdiHq/jZtJhU2gxjUNLQYKtJbqHm2w0Hz/k81PtvwoHMoWfqxjpOo18N3YnEIV1K8ZDqRsFpl7dIv9o9gmkIuFjKaJAr7YCrvpCd4wWWTVNkw7+M27Macaw2+RNXXTIpFNmd3e2Ue/0KguJjuMtqLNpBEygdxk+25QhZEpb5MQ5ccuqGLiraF8TxBIhwG+HtEg2qvGKvAtUzKJE8BZn+qwuMlBa6JNdbh4QphDjT35rkC2JkIQuHUGgvxWyGamnmcc+fYbnV/oF0ibCXtWghyr0u7znISKk7+SI4oZH9wG1vfbdextwHqQ== 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=dRSFcm/SDSV8jp0c47dbtAeSYKLmz1qsfnLAhRBu7Yk=; b=SUC0lcgOSkPXkfxV+2p8dt6HTqrqWVFEQeQc/X3MKSW/bGRZLG5W9q5m5lBCmZ4lPomxNDotvBWkVCEeQFskXzvl3iQZyjw11Q66/rHmKTcL+8i0onkNIuoC9TOarvaWIyOt2WkmL1lHUXLJ6XaAkEE0H/2amPdSfGiFMq0Q18Mf5R7ZH/shHbQP+onGBLbUEcaLKjnPjrUOiiP6IIeBdfpddx0h9S+83z/Z2ZQ3jr0FgmitR1m/V6dmq6QxZeB1RKnQXp8kLaC+2ilVVB7HWm0Ckab4cryeDw0igGy+uUSV66A/P16msHT4mGRPYPHrUrdI+0mVHPacOHRU/XLKcg== 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=dRSFcm/SDSV8jp0c47dbtAeSYKLmz1qsfnLAhRBu7Yk=; b=j/hyh3R/jjRX1cIJ3+p3h5VaaKXk2QG7yRwNkUARZk/BCoAlMhBs1Ynouy9B018suqlX/6yMMwt7osrz172XW+1oaJ0cb9sUW6IWcHqWrRcek8r0XWtQ3gT0uJbds5YDNdwNi7U0AdDVmon+EIq8+x+tL9NwfHuRlof4UJTcDCQ= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3939.namprd12.prod.outlook.com (2603:10b6:a03:1a5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Wed, 7 Jul 2021 18:15:28 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:28 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 01/36] x86/sev: shorten GHCB terminate macro names Date: Wed, 7 Jul 2021 13:14:31 -0500 Message-Id: <20210707181506.30489-2-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:26 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: f8e73936-c752-4237-eb9c-08d941733301 X-MS-TrafficTypeDiagnostic: BY5PR12MB3939: 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: DtpNK3n86Z3Pa+UgX5M9uL7zMFBmjvk0DR5wF+6IUqd8xOUfABJ+RHPTTHIjUIGjJZdpTr7zE8pTUpx9XmIpjUzwN/NRJpW1ziRt1KtzU7N/QYRx0K5rz55mfH6ZH9g1XOnzRIccN8DgcC5EjxPSWUjKBng6i6/aMwnyZblix1TjrutXGwBmQGQITUhO44WujV+kXpta2FhBq3AdCihEvftc1ycThBP75D80ZAj2ifKPfcwAKavse8pN71+RyrFhyd9WIxFyvznE0Pu93rMlGTVuOhhoqcLLOmBAvbYKhd2MYrNxyLLo4keYJZFdhZp8FIWf2iYgVw2666FBUJwEqZDDVlN1mDPHNYAoXpVcZ/bCG0iyMOq+lMcxOK1FMs/7/FtadVc89Cv1WwnyR1s6iWHbttdpPTCR8v90hBXSZA+FdfoQZ7TNrnLBo7Uw7m4JNCn4Udefh6V8ALfd1qMYXviQUs+84CoNFQlr9e5P9APeqGou5U4M6von+Y82koLDR3NZKrQjjHr//q06hWelcSHX43cjsPph1ChKQMVnwHOQ18VFqe5nL2zUEKf1B9keptZwbOGYPDj/X8NEwrPku00r+iOTcVuM/FCuMaar9Ro7MXebx5dfiRCHpY5wMPSjdxP0JvXf3md+qyDN8yRF1pSiv0M4VX6c3wwAEOgWLwA11ugFguhp5Vtm3C03HNvKsxhrC12OX36aGquxSScD6Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(39860400002)(366004)(376002)(136003)(44832011)(66556008)(38100700002)(38350700002)(66946007)(186003)(66476007)(2906002)(1076003)(26005)(52116002)(8936002)(2616005)(6486002)(7696005)(6666004)(5660300002)(8676002)(956004)(7406005)(7416002)(478600001)(83380400001)(316002)(54906003)(86362001)(36756003)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: WXLJJ6uKQw/9bnkeUbFxIPOpgrcT0pcPD0vj2EMGuSJU0sQDgdsw+gmqVMGGQagrVFyAddVPDgA5S81CFiN7EWo9qJIsaBjtIKVVDnUBx1yKPycAbKrAmCpEf9vP8fV/29M3a5cWSoWuhL71SyOQgGKRnZEftgHrKS28xtuEk7OeaQGyI0uJgdtAZDDif8ulsFYaCdAHfXucKr7EQBibyruZYEbaAkDvXuKHHFsU+JOBSuQV9tjzntNQahR+pC99Am7RYvIFQkYsa6yOPXkIo79VUgWxdyEHcxqUaw1O76Ny4+Nx5wyZuPy0wiUUtwdUl59mRdAhgvDr0G+354L/xmFvSOaRFGj1jtFA1HGyzjX9TqvBsdrR5yHhewidX6p017y88IPPvRz+F3JnqZhUHHHj5pHWdfwx0KEsBk6OZv2YLsxYDN9PuNVZW0lC7hVSBrQufaM45x+zGLXc2I0KUxPw8alFKnE0B0sp8GLDYpWaFFazkqkxhRG6W59ujFeswtcuJ9JK5z3xuNFmVq2lmSz83CrMQMo6pWT41HxjWneNo/+5E6OipLe6RlDw3bFDBHA4O96YJNrxZtXNNtz2JLWm3Lymr5yLsVxU4A5/qHkVkOtwB+UU1PEQngDLwJwFX8k4f2OuQiX1LEhU19rppwu+F68E7UYQoEE/6/0QxGHM+aAyndZlINZ+yyYta53wVs0f8Om2ahiBndsxE42tpfEOeoLNVZOw5rvYb0h+wqyFmo0fdkYKMxTS3JDAUKVo0aCgATyL7s/xQMEY/Fr0QlbLfYX3p2Z3FuyTRuHGatJ9rTRH3D+V0F0Udgm4keiEQDoM229ptr2hVaPom3qG9Rk6bBl28q8j2MM1cuX1F9yUSwDJy5dxX4T2evfhAEyBh4RSQmOsDXsrWmHgu5oI92l+cBhZlUkS4I932OZ0yB/ej1/xw3+Hl4Qiy7SqqM5I4FyA0hzwjk8gv92ZHWNOH4CDFaACaqS0976Hj9yA2/3CzVS1BqNk/9ofbWjy2AwHdpue3T90IAolNGyXWjaXLlW/HUSCw+EiZv+T5RP9CUvYYd3HHtgXhHxeFSyQqiaMStyu+dsv8nRM1H7f88GlZZjmwOWqx3jYbUc8v/2ghj5yQxG4WeCdaZYRZ2ipsU+giMGTPoWCm2WLsoWSTgkh0OHJ5Y2N74lWDqjk7dLwsCfOk2006B9d0HdwSdv5Bk8PGDiKastZ72syKq1Hq2NgS+eL/rJ5v3XjotnAi9GrBsL/KuowK0MUWyNDHUfZl6HLHpRDWMf/e6kRX1TsgqDF1XHZ6ZpYiR3v4L8SZxmYlWsWpaaOrrs6OQ18xk7fVAC7 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: f8e73936-c752-4237-eb9c-08d941733301 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:28.4662 (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: h9DLBySm2FoWO/DwLVxiq52vjewuyBQetJspY2f9M4kZIWV2EezFtdlkByrd4YS+bzxBi3Z3chXsqQ8ozc149Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3939 X-Rspam-User: nil Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="j/hyh3R/"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf05.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.96.63 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 4FA165007518 X-Stat-Signature: ibt8qmdjacph4uerfmzihpajih3a6s9h X-HE-Tag: 1625682653-642997 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Reviewed-by: Venu Busireddy Suggested-by: Borislav Petkov Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/sev.c | 6 +++--- arch/x86/include/asm/sev-common.h | 4 ++-- arch/x86/kernel/sev-shared.c | 2 +- arch/x86/kernel/sev.c | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 670e998fe930..28bcf04c022e 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -122,7 +122,7 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, static bool early_setup_sev_es(void) { if (!sev_es_negotiate_protocol()) - sev_es_terminate(GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED); + sev_es_terminate(GHCB_SEV_ES_PROT_UNSUPPORTED); if (set_page_decrypted((unsigned long)&boot_ghcb_page)) return false; @@ -175,7 +175,7 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) enum es_result result; if (!boot_ghcb && !early_setup_sev_es()) - sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST); + sev_es_terminate(GHCB_SEV_ES_GEN_REQ); vc_ghcb_invalidate(boot_ghcb); result = vc_init_em_ctxt(&ctxt, regs, exit_code); @@ -202,5 +202,5 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) if (result == ES_OK) vc_finish_insn(&ctxt); else if (result != ES_RETRY) - sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST); + sev_es_terminate(GHCB_SEV_ES_GEN_REQ); } diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 629c3df243f0..11b7d9cea775 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -54,8 +54,8 @@ (((((u64)reason_set) & GHCB_MSR_TERM_REASON_SET_MASK) << GHCB_MSR_TERM_REASON_SET_POS) | \ ((((u64)reason_val) & GHCB_MSR_TERM_REASON_MASK) << GHCB_MSR_TERM_REASON_POS)) -#define GHCB_SEV_ES_REASON_GENERAL_REQUEST 0 -#define GHCB_SEV_ES_REASON_PROTOCOL_UNSUPPORTED 1 +#define GHCB_SEV_ES_GEN_REQ 0 +#define GHCB_SEV_ES_PROT_UNSUPPORTED 1 #define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK) diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 9f90f460a28c..114f62fe2529 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -208,7 +208,7 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) fail: /* Terminate the guest */ - sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST); + sev_es_terminate(GHCB_SEV_ES_GEN_REQ); } static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt, diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 87a4b00f028e..66b7f63ad041 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -1429,7 +1429,7 @@ DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication) show_regs(regs); /* Ask hypervisor to sev_es_terminate */ - sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST); + sev_es_terminate(GHCB_SEV_ES_GEN_REQ); /* If that fails and we get here - just panic */ panic("Returned from Terminate-Request to Hypervisor\n"); @@ -1477,7 +1477,7 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs) /* Do initial setup or terminate the guest */ if (unlikely(boot_ghcb == NULL && !sev_es_setup_ghcb())) - sev_es_terminate(GHCB_SEV_ES_REASON_GENERAL_REQUEST); + sev_es_terminate(GHCB_SEV_ES_GEN_REQ); vc_ghcb_invalidate(boot_ghcb); From patchwork Wed Jul 7 18:14: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: 12363449 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,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 E6087C07E95 for ; Wed, 7 Jul 2021 18:30:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 7DAD561CBE for ; Wed, 7 Jul 2021 18:30:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7DAD561CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5C9176B0011; Wed, 7 Jul 2021 14:30:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 578756B005D; Wed, 7 Jul 2021 14:30:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 37CBC6B006C; Wed, 7 Jul 2021 14:30:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0117.hostedemail.com [216.40.44.117]) by kanga.kvack.org (Postfix) with ESMTP id 0A6D06B0011 for ; Wed, 7 Jul 2021 14:30:04 -0400 (EDT) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6826E181C77A0 for ; Wed, 7 Jul 2021 18:30:03 +0000 (UTC) X-FDA: 78336631086.12.73F2125 Received: from NAM02-SN1-obe.outbound.protection.outlook.com (mail-sn1anam02on2088.outbound.protection.outlook.com [40.107.96.88]) by imf19.hostedemail.com (Postfix) with ESMTP id 6DC98B0000BB for ; Wed, 7 Jul 2021 18:30:02 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=govqA+X9+lzVM/cD+0ruYXc1YZ177VDjcRa2ZwrdvCHR7twPw0eYBdTlPRRUgkob+/xeSNvclcVpcUHGrxfQVc9z+DAGBwIZ/5jnGxJ47UaJQ4MoPqXtA9VjKFndouyfFD2z4tZczhR1a+VM5MbR5SkGuJQLyneeY5qmXzDbppGOT1Z4nD0Gd7+A0KDlRd8cR7Of2742HVrA7N0+ihs9fVX1KnptovzvcgxQ4n3iqGK4I0ZWXgRTxREl34i1WUCLGe8cWHsgyD/4XLkjgwjO4qhgZgPeoo7VSO/pImPf5JB+TJmm53KP1dIvqI97HLFM6c1UELEDU6kK3EI9jTrUrQ== 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=C0N9ZErqug4pgk71YQajHqeQ3GpWF4d9XuQBEPLDLKU=; b=SnHu+UARWILRiFARMWOjz9pZGg5/ECx4n3IZ+D4oSe06tL6v9m/IjRRg+5gkOT/iCki834HbaC4a6YMzGejHqfP22j/xHdm1QwGgOS9SrF1dgeLC5iApBzbSvHX6ZcOp0QHjq1KRao7yX3YIkQAUzzkHO9l6kjiJAByLzJn12sY11FDTnGfpBydPfHx7hmfIKrCyHTJcEYTbDxX37AC0TSBucxGoYjE1Y9F+tZCnDMf1sfBRPOubGT/GhDISOW9gjf3k0X8RhUl1HDVkKkM+Tv9KDku9gZJ0yAA8zDCDDRdAVAS9146iAQg+EWl/vuNlnb6gsQhOmFX9lO2VFKckmQ== 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=C0N9ZErqug4pgk71YQajHqeQ3GpWF4d9XuQBEPLDLKU=; b=bax/LEPFLjmqiSaju20PY2z5JRDotTDhJYqd4aFH2omQGvaB6GzMP4w1HCB97kj0UhB+o6VSbI6RymiIKPvmXQOft38Pa1nbcvkroMSOzFT4XrKNXuNDfiP8D3qFK7EIQPBeBvgy1LJhZkKG2S0x0QlOUtyPjpkwh6Idfh6CjF8= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3939.namprd12.prod.outlook.com (2603:10b6:a03:1a5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Wed, 7 Jul 2021 18:15:31 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:31 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 02/36] x86/sev: Save the negotiated GHCB version Date: Wed, 7 Jul 2021 13:14:32 -0500 Message-Id: <20210707181506.30489-3-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:28 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 5cc1bbae-1f48-4f5c-1a4d-08d941733487 X-MS-TrafficTypeDiagnostic: BY5PR12MB3939: 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: oJjmhGIR5F1phAfCKaA2jlwFYPRtpCKep5bzcpfxxQGPAk71Rhl43VI0HJn7ZBiR2n94cKmwnoXi9T8vRAj18zoqiXRUmqF+lU9tsG8hP1p/Kl/XUT3oFTbuLQyZ/SzEl9RMGEuf5ztj//qkSIMNw0XwNRbak/1ACRvWgIxH6ND4CF76+gibm/jG6/WD63yubnhOZ0DA3WoIQWkeLfPV77aRbpD3vHz9HkB1j+NnuIFu87PdKjSSaIcrmhhBALNGPwrGhNqi1D9nGVK2ZuiNyDDVNPM9fuLhshe5LZSvv+wVKQnEwi13aXKiVl/GFo73PfB1TTBrkPKK39hLTrrikv057/bZJPZVBcrqy+StK3uUoA5TzyKii1JmYCVnUbkGg7iI0aW6436SdHU6eUVR2GZ10nlBXw58/59FTuzHEgVXWgrSI1OpqL4FuQklcoJsNl+iLy7H28fuj+jDYM+VrQo83WOnJmxNngVVt5O5hoX9PwrRAUrrLAw5HEizoaprhjmIwAN4LkNJqGfpGLLXUYlr/RRXca+aYWTHV0LhWLa0ZdayUgWqWsubzSXsotUo4H+sIOPnN/q25STzZe7TjKgHrBSHzuxrvM/QKl3It9AwE45+i2arVl1u3JfY0C1SQVWu7wsBdHAWG1Q6+h35hOu6zjQDw7hMS9/LWPa4+4oM2LRKdiEn1PiLioZ4DjoWgbk04DDFZ2vfaxMq0jfwHw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(39860400002)(366004)(376002)(136003)(44832011)(66556008)(38100700002)(38350700002)(66946007)(186003)(66476007)(2906002)(1076003)(26005)(52116002)(8936002)(2616005)(6486002)(7696005)(6666004)(5660300002)(8676002)(956004)(7406005)(7416002)(478600001)(83380400001)(316002)(54906003)(86362001)(36756003)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UPYNYcVq1LQbk402D5wl0W0nZSq1ZkVofeuUEr4cKjk7Zfgu08GsfbrmzeRasfFBi8Cji0799mqKALBMTtMoR520i2ZIv22oV9w+/cOiFkfNExTYDnDngC8ER4n5mY5zxngDJ1hV8OWX8pnWJLiY4ASgo8KLmd2KR/DOiKxuEFFWQCUydi3QFTSmtkJUJDOrESYmnwbyouHpyjbmuZCyH/DvkeNugbGbHSmEDDaP07uiC094CeWOPTa1e+UrTljSGa7UpWW2Xp/Ic6O5rwXKVvOtlEvdEYnpWD76pZYYZ/EE+9g+ozwNtUrxgOP0b/cXll0mWRiPSc+0IbYNqyU8Y3uzzEduTzHcWKvcNbImQtc9546cNECCmSAhPjLuBWPhDMU6+Z93+6TotmH5aNjOEA022GBYaBv6a4HpU3KMTYxw46i0j8AnyDlUeiLi4JiHiXk2HCYKwIXeSKC33qOVRw6AXPQKjwylShOnrbL1NtrPzrAQb0lPkUi/o4mHVyVeyXIwopKWLIKMxqzYRVvdpomfAKbvxjW7o9WCu2m9mruvTw/B1yizhlDNg31hMaZGxqStrRzX6e1+qPjkRmZu5uYqrzWGwcfoEN/ZTSJOHHeqRsXN80v4qZ7o9spPVtv3Dqh/uOzU2YQ2LxB5LpkD6v8qCEHa6Gih1lhffX3dnHFPeNB9JjNmTv/Z/HmNP2RvzKzEiVLeT+YVhTRxVyAYQb9/HW78q0qieI3nyD/Tbl1/e3A5Y6VBCf8M5HdY9pnxuGy6KSY3JoOpaVU+5905YXE2xdSRRQYySbVqULNN8l3XUYhrZRBKMUGB0tSHkorlpUihAvaNxYuAetG+l3VNw7Xwn4Pv95xfzM7UrDIzhDX5Kfo+EDkjEQMk1zZJa1bdMdfVwRjdbJEqfRV5svThnhqLYaGlC14A82aZE/JBqBpMUVacBLviT59P330FciIhchAoIhXTxlptauHQmMRY4U6WJvhYJgB9tGEeZrzYqfuSGh12hu7EvpEqCpvNd+pfyjAsWOiG2fp1JKCQbjj8BB5WzD8ldaBRRdCEu001Q4kPO7sxMlj5UCFGNFRMajDAGnXKAmsjiYHoO1SWQZkjpHcSmuDNl5ibQ01TQYwfM4Xd4dv37IG4+F2lZy81vrl/jvMBut75b3ZJJBv5UDl7qcQ9SL5US4OyWVMXmWJDqX9+4cnowIPLNTAo8nDfN3gh+jWcyE2754BpAiP9KVk85RJja119mw9m6355oKdLOsapsA3tjkS+nBjzw6eofIpk8IVz5/b4Hw+pI72Pp7oRY+zd36/BKMRGh855rIBSD298Wk2VbjhPndrU9Pz6Mq4E X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 5cc1bbae-1f48-4f5c-1a4d-08d941733487 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:31.0498 (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: ktux2SXOBM7PBwWyERbc6E7/DPh0NOFYRQAJiiUyi6eh902/upxbiGyljVW7zsRIAldY1tUuvAZszz4YR2asfg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3939 X-Rspam-User: nil Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="bax/LEPF"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf19.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.96.88 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 6DC98B0000BB X-Stat-Signature: c1rg9qdgua93zgmscz7wzy5cb6wjndax X-HE-Tag: 1625682602-58723 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The SEV-ES guest calls the sev_es_negotiate_protocol() to negotiate the GHCB protocol version before establishing the GHCB. Cache the negotiated GHCB version so that it can be used later. Signed-off-by: Brijesh Singh Signed-off-by: Borislav Petkov --- arch/x86/include/asm/sev.h | 2 +- arch/x86/kernel/sev-shared.c | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index fa5cd05d3b5b..7ec91b1359df 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -12,7 +12,7 @@ #include #include -#define GHCB_PROTO_OUR 0x0001UL +#define GHCB_PROTOCOL_MIN 1ULL #define GHCB_PROTOCOL_MAX 1ULL #define GHCB_DEFAULT_USAGE 0ULL diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 114f62fe2529..19c2306ac02d 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -14,6 +14,15 @@ #define has_cpuflag(f) boot_cpu_has(f) #endif +/* + * Since feature negotiation related variables are set early in the boot + * process they must reside in the .data section so as not to be zeroed + * out when the .bss section is later cleared. + * + * GHCB protocol version negotiated with the hypervisor. + */ +static u16 ghcb_version __section(".data..ro_after_init"); + static bool __init sev_es_check_cpu_features(void) { if (!has_cpuflag(X86_FEATURE_RDRAND)) { @@ -54,10 +63,12 @@ static bool sev_es_negotiate_protocol(void) if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP) return false; - if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTO_OUR || - GHCB_MSR_PROTO_MIN(val) > GHCB_PROTO_OUR) + if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN || + GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX) return false; + ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX); + return true; } @@ -102,7 +113,7 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, enum es_result ret; /* Fill in protocol and format specifiers */ - ghcb->protocol_version = GHCB_PROTOCOL_MAX; + ghcb->protocol_version = ghcb_version; ghcb->ghcb_usage = GHCB_DEFAULT_USAGE; ghcb_set_sw_exit_code(ghcb, exit_code); From patchwork Wed Jul 7 18:14: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: 12363379 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,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 717ACC11F6D for ; Wed, 7 Jul 2021 18:16:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2019C61CCE for ; Wed, 7 Jul 2021 18:16:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2019C61CCE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0AC446B0071; Wed, 7 Jul 2021 14:16:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 083626B0072; Wed, 7 Jul 2021 14:16:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E187C6B0073; Wed, 7 Jul 2021 14:16:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0090.hostedemail.com [216.40.44.90]) by kanga.kvack.org (Postfix) with ESMTP id B8C3D6B0071 for ; Wed, 7 Jul 2021 14:16:42 -0400 (EDT) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 246A8235C3 for ; Wed, 7 Jul 2021 18:16:42 +0000 (UTC) X-FDA: 78336597444.25.597856B Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2046.outbound.protection.outlook.com [40.107.94.46]) by imf26.hostedemail.com (Postfix) with ESMTP id 14A5420019D7 for ; Wed, 7 Jul 2021 18:15:38 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OSII+6LT5/ietR8kfCSwMGqw91bqPg6FnJfECZWTbgGgPhYYN4SL7diLLJgnnbZNBESHBN+50pcY3O/4MjMDFaRvXFt7r7d4DFLC0D1op1bk22ZCqL5MXq7H8hqKjqPTg4VMvyr+Dqsv9mDWirQCMJa31Mt8Hwx53QA9mScMGFE00jiZkZr9w5yQLCetNY8+cI9AhhtvQyFZ4pGVuge+Om8mv/doO0fXV0/4WEvLJIp7jFvEFdLW65WzEa2cralIzYfAagbK5FCOshSqpZ3eY7iHswe836NSY1J6RHZoByey+lZaa430ARck1ZgSgfSTHSguu1ZiSp8veVCoeuJ3qg== 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=RYJ7b2pk2vbOHgVoQcFe8dAUGVd72Tc9+N7EFxpikNs=; b=Wgmk25H6ceki9cT5pGj0S9Gp2o1oZ9W4dPit9GP9Z99GNHKqrg7zTNxYHllhAiLUXbPvFUnM1zpXYB5PZi8eWkhqySrpd8d/UQInFQGj8bbFpeWPnaE6o8hicx9CsTPvB+/muQE2SVMUFY3W61nodMYqRpCEL0H5SiWe+lp5a4VYzbHZfypc3UpDKd/PoxFExzNn2HOpgNuL+AC+FOA/Qc1ua64Lt6YVdszsK3K9T3veymsBU6S9XlD+YESZuWPcYOgMTjPHEyk9FtPO1I69TNJxTtADOJrlmlyxRMDnjRbVPMx12NfjYZK58rBvgcWHiTLpxbv55AwP0LNYH6pCww== 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=RYJ7b2pk2vbOHgVoQcFe8dAUGVd72Tc9+N7EFxpikNs=; b=SVje3Ic/VJyijwc6FJ/+TtU4Z/3j6WNOwiYhcURX2gbJJp1qQoI2fOTcdRfGaDn589a/bCbYK9gEHbD3b+SoI1/fBA8JDnVpayVbwDQnFHWzTOo15yg3W0AQZyR9GnCWBokPzLQhBYau+1idxlAhBFuA4PCACWqROLQHuQQl1AI= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:15:33 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:33 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 03/36] x86/sev: Add support for hypervisor feature VMGEXIT Date: Wed, 7 Jul 2021 13:14:33 -0500 Message-Id: <20210707181506.30489-4-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:31 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 133ea6c2-c9a5-4c3d-5154-08d941733611 X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2331; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: KmMJqZoZyDUelITwBwP41LWNCJXzC0pOA/9ip38BJUhFw70wGlKo7zDO252U1q7MMppDzTm/cDHhWmEpiMvJ31HlXXdznkWINkEZpakoz4EaEZ+05a62Kyrc5Vj7haX/2N9fIO1Jz8m/VO2KE62Qwfu8BahdPWfQleoSSZviOJJy9NEoBtyfw4+68qNlYl2VtXtOIHAeHQW52NMkWVJXSPBH7lqQCpsS9yQbmwuE6+A7UOhkcMseySY27SmlM+d8EMPc9b6trZmEGdExDSN8/YPg7HooZaYWBbCTYu9DaQmtQXVDehv6umRpXM1hdqSPewjACE7nz/V4FTpp4DBkhCpppOYofwDPEaMfIVfQVCcSBhD/OibKOx1zwfLBwuenDm7cXG5Bgj0ZblenFqV6CPSBW1Yftr95HYtIg4IiYOKngO9NggkWxTDYwkB1KZrpV1oeLo2DM3BvPwkp5Qs9SJDjRg2P4XNjscc8Spkm3S+XIMBPVKZmNs9x5qwfUgm0wjr/uXNWMDiEWw1PJWVTVGFMirtvL7LOszQu8uEoy5z8dwVIVWpyN4wsyTbAjFNAkevsElDsihcTLiUnHXf/BYhmaC3VYZRcKhYtqgzYaP9Pj+2E70aR1TYHB6Z3YLLHPOLbWB9/66o1R3BWc8mIsnuqQ8rgq92iSrCEjedCM9uWiwr0TKoNPj5VpJFzy2j52nl2Dra4Qtt+tSMnS9lD4A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(2906002)(1076003)(4326008)(83380400001)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Q3NsmtNbdSnJ4iqgpBh18gPcotE9QQEqap2+hprOLglT9ARvtYomDHWVtR+jyxPzSzO86zULmtFslHT2gVYbHZR/oUVpbEzXmWdnpOuQvRWj8T00DFXyCXThUQZgOdWS4bTYl4D/kqr7suqCp3gHhBirCXPsArlRZq0gdv0/wUtMqqzRB7BfwjfZ2qvubjdeZ2jlwzbKbjRjyoAZuVGV8e3u5q4HoyS3ATZC85i8jBS1TVQv/GELfsDAyRNsOCp6EhjzZySHNp6+sU7ChjZs1jnUltcWujKNNXK8NTn+euLj0ZrnLFRMTjk0bjGjy8RtU7pK8DJXdmdTDILbnRXC4aiRRLZRarRchxs+PkgXMoCl2kpGDvP9bOp3QVZY8NHSNqDniGAP8ZnaCU4mJjunLAG7XTf5v7zH2RXFlX021gC+Je+RIosggMAQUdWoNAjqH5T+A7C8tRRAYCe79sDfYkGUaStCZyIr3ddJXaVyQMXYY27z1t9q2l0xK2GfweflRUvtxzLK8C3K8Cfd4Ep7IfImPMqJnXwB1K1+R3eXoEr+5cN6/1diGmW2uf9wPkYmDpBMukdcK3SNcjpmtLbvKKTc9JvOftn1YIDglP5LoCheDO3SY16YYGYHQZbagCqJc+Q9YR0epGxAlcDG6mkrDnv9MK/qWwyIU2KthufFlAoxPbG54T8k+aahy2/GELXYQgaaYdZ5ylV4pJxNAAJsiukxwqybUOCmxPlQZlPopFq0+SsFZbLGGF5jZeQ8dnaMelTj0KD+b0udmXBDQ9fXVEtzDMTl3IVBXPYoTM42J5gx9fVftkQHq3Nkg2Ef7dEl8W6Dieq3Gn8BhgYg1CTFmxGgBWHcvSKxrQEKvbMg97MnKHq0LqcF4entMrjBU4OVrXrvOvjgnva1SCI3XqHlXkJe7be5mOoBNqcU6Y4Rl/ujfJo25zB02ZJfgdEwBCOQHQ62aJoG5jodlG8aNTktSEymeFShYEigwv14qsyzsdRZnK4X3Eq1oIpjTPCOZlOreuY8F0CBkF1Cb3L10pk8vMY1ikjqAiGquDCBF+Z0RMQyjuwyG7PqcXOslKw6OYWLa06t7NETxEt9aIjHhTwHEyWHpZR0jKZeOhQGNJULuzA5nV72UyBM1W37c3rDSTwpHJoA8YJ9iPwyPlURqWbttKls1o2YKfZnlL5Q2RQ3yyY8tqxERpD0EimHSAhJ2YxGs8b5it+F7cw9GS10fheoc479LccTAeCWtKpD1QV22ZoF9JfRebUhuTjLmbYGzhx/NYEyFCFsoPizn1oY1Brr16N/aIDMf8Cplijdd5VzXULjuzyNVCLaZHuDMVPN6AyV X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 133ea6c2-c9a5-4c3d-5154-08d941733611 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:33.5873 (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: kgf9z06xOEo271VPa0/x0ji7jv3ZBZnzd7HgPzH7Cr7buhaP3mWrfli4KtcWxOH5jiA6i01ZGJCH9E8/Ds9F1Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 X-Rspam-User: nil Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="SVje3Ic/"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf26.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.94.46 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 14A5420019D7 X-Stat-Signature: go9hxkce3reb3t4wp8ujniwjmcqnag8y X-HE-Tag: 1625681738-642148 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Version 2 of GHCB specification introduced advertisement of a features that are supported by the hypervisor. Define the GHCB MSR protocol and NAE for the hypervisor feature request and query the feature during the GHCB protocol negotitation. See the GHCB specification for more details. Version 2 of GHCB specification adds several new NAEs, most of them are optional except the hypervisor feature. Now that hypervisor feature NAE is implemented, so bump the GHCB maximum support protocol version. Reviewed-by: Venu Busireddy Signed-off-by: Brijesh Singh --- arch/x86/include/asm/mem_encrypt.h | 2 ++ arch/x86/include/asm/sev-common.h | 9 +++++++++ arch/x86/include/asm/sev.h | 2 +- arch/x86/include/uapi/asm/svm.h | 2 ++ arch/x86/kernel/sev-shared.c | 23 +++++++++++++++++++++++ arch/x86/kernel/sev.c | 3 +++ 6 files changed, 40 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 9c80c68d75b5..8cc2fd308f65 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -20,6 +20,7 @@ extern u64 sme_me_mask; extern u64 sev_status; +extern u64 sev_hv_features; void sme_encrypt_execute(unsigned long encrypted_kernel_vaddr, unsigned long decrypted_kernel_vaddr, @@ -59,6 +60,7 @@ bool sev_es_active(void); #else /* !CONFIG_AMD_MEM_ENCRYPT */ #define sme_me_mask 0ULL +#define sev_hv_features 0ULL static inline void __init sme_early_encrypt(resource_size_t paddr, unsigned long size) { } diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 11b7d9cea775..23929a3010df 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -45,6 +45,15 @@ (((unsigned long)reg & GHCB_MSR_CPUID_REG_MASK) << GHCB_MSR_CPUID_REG_POS) | \ (((unsigned long)fn) << GHCB_MSR_CPUID_FUNC_POS)) +/* GHCB Hypervisor Feature Request */ +#define GHCB_MSR_HV_FT_REQ 0x080 +#define GHCB_MSR_HV_FT_RESP 0x081 +#define GHCB_MSR_HV_FT_POS 12 +#define GHCB_MSR_HV_FT_MASK GENMASK_ULL(51, 0) + +#define GHCB_MSR_HV_FT_RESP_VAL(v) \ + (((unsigned long)((v) >> GHCB_MSR_HV_FT_POS) & GHCB_MSR_HV_FT_MASK)) + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 7ec91b1359df..134a7c9d91b6 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -13,7 +13,7 @@ #include #define GHCB_PROTOCOL_MIN 1ULL -#define GHCB_PROTOCOL_MAX 1ULL +#define GHCB_PROTOCOL_MAX 2ULL #define GHCB_DEFAULT_USAGE 0ULL #define VMGEXIT() { asm volatile("rep; vmmcall\n\r"); } diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 554f75fe013c..7fbc311e2de1 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -108,6 +108,7 @@ #define SVM_VMGEXIT_AP_JUMP_TABLE 0x80000005 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1 +#define SVM_VMGEXIT_HYPERVISOR_FEATURES 0x8000fffd #define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff #define SVM_EXIT_ERR -1 @@ -215,6 +216,7 @@ { SVM_VMGEXIT_NMI_COMPLETE, "vmgexit_nmi_complete" }, \ { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ + { SVM_VMGEXIT_HYPERVISOR_FEATURES, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" } diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 19c2306ac02d..34821da5f05e 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -23,6 +23,9 @@ */ static u16 ghcb_version __section(".data..ro_after_init"); +/* Bitmap of SEV features supported by the hypervisor */ +u64 sev_hv_features __section(".data..ro_after_init") = 0; + static bool __init sev_es_check_cpu_features(void) { if (!has_cpuflag(X86_FEATURE_RDRAND)) { @@ -51,6 +54,22 @@ static void __noreturn sev_es_terminate(unsigned int reason) asm volatile("hlt\n" : : : "memory"); } +static bool get_hv_features(void) +{ + u64 val; + + sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ); + VMGEXIT(); + + val = sev_es_rd_ghcb_msr(); + if (GHCB_RESP_CODE(val) != GHCB_MSR_HV_FT_RESP) + return false; + + sev_hv_features = GHCB_MSR_HV_FT_RESP_VAL(val); + + return true; +} + static bool sev_es_negotiate_protocol(void) { u64 val; @@ -69,6 +88,10 @@ static bool sev_es_negotiate_protocol(void) ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX); + /* The hypervisor features are available from version 2 onward. */ + if ((ghcb_version >= 2) && !get_hv_features()) + return false; + return true; } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 66b7f63ad041..540b81ac54c9 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -96,6 +96,9 @@ struct ghcb_state { static DEFINE_PER_CPU(struct sev_es_runtime_data*, runtime_data); DEFINE_STATIC_KEY_FALSE(sev_es_enable_key); +/* Bitmap of SEV features supported by the hypervisor */ +EXPORT_SYMBOL(sev_hv_features); + /* Needed in vc_early_forward_exception */ void do_early_exception(struct pt_regs *regs, int trapnr); From patchwork Wed Jul 7 18:14: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: 12363843 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,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 D2A13C07E95 for ; Wed, 7 Jul 2021 18:49:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 34D5D611C2 for ; Wed, 7 Jul 2021 18:49:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 34D5D611C2 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 203E96B0092; Wed, 7 Jul 2021 14:49:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1DB766B0093; Wed, 7 Jul 2021 14:49:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 02DAF6B0095; Wed, 7 Jul 2021 14:49:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0111.hostedemail.com [216.40.44.111]) by kanga.kvack.org (Postfix) with ESMTP id D5A5D6B0092 for ; Wed, 7 Jul 2021 14:49:17 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 40F9A180AD806 for ; Wed, 7 Jul 2021 18:49:17 +0000 (UTC) X-FDA: 78336679554.01.87D8624 Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2042.outbound.protection.outlook.com [40.107.93.42]) by imf14.hostedemail.com (Postfix) with ESMTP id 64FD16001F0A for ; Wed, 7 Jul 2021 18:49:16 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fqxEOjLLFJ0nxma/ApIQAVG757LCr68GfpjpkIdDOXfs8SSoiWbm34JC7eKsG+ePwbgeJuJnVXQI4HYcrfyVW5rbc+MsL4w/XM1Rrn0Seny1xrhkFaBxdWevCA9zX0k6n/Yb98J7/4RnxM3QxpmZqj6lDZ6C54va8V+GBcyNFFfSCxD8OIwmzmIyWhOs1fIl1T2/zds9aRsFXp2LZCjzaClw8quT30diFiWS3FqA7WMkMo9xbpfvHtMtvf58miwDkn6mLOF15ej1jntpGOyVbM/5shwFzTyyJLvEZ2ot7zPf8eNz3YjDESMBnQTHPnzMKWpQxC7yfDztqypdDHxD0w== 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=chVykgN7bZcIuvfIEO5C/ybi4gNYMSU5TQGwpDsi530=; b=kCrFp106JMgVcyldsLANks+s3z7UH/rvPDeMy11jbI1CaLrRvHlOyvhlxFvRkCxP3EqSr25cR5V58DHnjfInlFyQHGarr2rVkqntKoWsKoFtcQpj409aqacX7Lk+g/zU4ESkwOhhXszOwW4xTunmA9kNQvm+ydI37xN6bKKXcX0+Uvr67/T2ZhSJTjSMlxxDoXJDz4RzotK4WPMhznDkMDOb4KTVX0dm4cAxn2ejA5TCwQhO1tsStVx2IYcgcQxHjwjcY0ZO0J9huuAze1m63kkBtlODuWpA6fDfSrwtSF9v9PbuvKjYv+g0JUG6QPM16ueq3RfGiUfd8HjCzoSaKg== 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=chVykgN7bZcIuvfIEO5C/ybi4gNYMSU5TQGwpDsi530=; b=kCdwlzgcrYyrqjAA4mCSt8sMdBpADkDdCQ0mqBQySLgEe/z0v1VieEQFlm+plnIEfLqx9G1hhsTb0wM+UkuRV/svT6rxaQVnCLw3hZeKaHV8BVhbyj3fCCYZPoH1ImCwIPvd/T8bO1GgdpZ9fNYn2uDLAURVAsb+l/SfmMK4i/4= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:15:36 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:36 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 04/36] x86/mm: Add sev_feature_enabled() helper Date: Wed, 7 Jul 2021 13:14:34 -0500 Message-Id: <20210707181506.30489-5-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:33 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7a9a7a80-2fd8-4038-a3d6-08d941733786 X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: 4vL7jpgjaCQiSQjw33IB0m62v+iMoxlxMxb1qjzXbe/ccun64EGdcs6U9fUn5tE/O7sgCQ9U6tzlUX3lrRbMA6laGONLbBaIvnC/JGF7iDf5ZrmFQZN/dKiXEzFYt2sB3l81lSmGvw/aEWM/lkXPn2iUGUzAapEoYaM8vEnH0qf1F6TAXkY9nf9dMDDSjEJBmPPlVVBDc2XcalpN7dVz+z3s2OFUSAhuvCcTQJWy21tX5rypqZIUaOsoVogwA+O1UpcVwaysjRZo8fY48EQ13b4woQHA3hdxgoxuhvqxskYjqEqIb30cLbcIonB0v6MFL6m9nJn0L957rOkqGJ+LUcUZCg6vIWHDlsGkVt+Tf2Gz6xIyyqshT7OuIiqJHXhgRzOhMZOH8Q458zVA2+243JijtfUOIbZSiIMhTs41a3kmyq7bScv0lP0j6vyI46jN6smsNsVWbv84iIKzesZSrcZjTLhgb+JgFWsQC+nWnV/XixC9UnnbS8PhT90PIdZ8EM/NIpguJHfEWLZNB/rms3rmpxtWDvsMET4Mpks03QuLKBBePDtFUNlAiGHUpoyVon1jQg2PxujltczCLOwwksLoSQTzc7nqridVg41qANrgFcPIeFxKdv+1nC0hZr3GVdt3fRrsOyefvkf2ajJR9plRs3JCUQ5HMJ/z5pHc/HDhG3GXSDxvkvPoqeK8YsG+ X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(2906002)(1076003)(4326008)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ZxrjAMQHzds+IZhsfdFM9KBi+LCxGWeo51qFQgjzP+Pq6cyeBYUh1sjdbE+9ogSXD2xL8bBSpfGQ7yeskOFoKTHn/65KTkwjn9dIwixGYuYJpdGnzdwnlLTxC2YP2po9nMtr3wRqCRYCvGHXlmwXY/ofVy4YfSwH/FBtO0K3D3Cj1YeJhFBiYLr94VExfPr0zLGcvCGotLvKmAe8a9Apq9TBNWlp/ieEfJ18bvRSBQJrYyom8nbKDW8IYsOQjLtZT7dYW0u7cnoeJZtUMnVGEr1QRd78S6Uwh2gP22o+wsPoej+riC3fuev9bL46Pqgcde9mBsutN7Uf9bB4ZVVVY687OsoAhRAT+9zp9jARrxjwsfly/4QrpO9BVoFFnB2HCZp0knXLu7eVoxLMZv1vpa6re6xhtidiDrQ+ML+x+ciQ8GfkVoJR3qciWIjE5f3WE/KXdjGUcQ+adL79lu1VBA4ZNWl6ytSOVw7Av9OXP7RIP98Vdg2P4AvLWcHcUiJRcXshnQe/bDobYJu3j7zSB5NimWKjbF9xgEvG5acEySYjBM68aNcSW5n9U2m9S2gM5t2loERl2Nh3zSjxpa6wsqrf3t8TGIOrvzkn1yWPR39Yh2XpX/7kgVmw1ueW118IucYFEfjhO+nHDOtpZES4d2/2WnQE0AW/ylGoYu3iNY3wzp3c2UvGp5++pZkC+QHuTebLpkKbNGIi/gOmLedLhifdK6JJFexTQZoUC8fZk0CKTPbClekzcV4Icqk5o06A2au8lcldMRGzi3azelViuWnwU/OjYnCwc9VmlUfexIEIMTPrFFYoOPRSM8TFes7WFi+SaTiRoyLQ5KBZCuapUJZX5eKL06C0R9Au5GHrYhCP9fSx/cvJdaZKS+0hlYIVxmW3y6o+g1La25wQjf2gcBMGamON9iqTX4nA6uWPnRQ1lAIlCcMe5yuNDeKG96W0Dsruy9PG68Oy/Vu+Vdy2dgYgHfEq1W5c1IkQAOgjUfJgO1NCh1SE5z758CapigU2NgNDCVA7fM1fihZbdm+C6nutrGLhqhrjVmv0TrGWvH++cPPMeBEo34uXO0i2ICXsWsSQGtyt+0xVfEdJ1dk1vPBjmr8SF0AWgyYfqIdchQzH3aVFuaTuuEsSTfjbY5hdOiTe1o5VHa93Wanf+cg6ne3nM2R4Da19ZwR0dS5WGN1FjeM0v3duvjWcHWLckCbsHeI2KxhGXpypnJlSEorBaYBdtu3hSLmJDQD7PmFgsZQHfSNia3yA+3+Pmoxoi+WGj8d/pMPka07TJg2pMrAovxUz1a0plh0iPWCphoGH0JD7EGKdmZ5AA1lfssw9K58+ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7a9a7a80-2fd8-4038-a3d6-08d941733786 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:35.9959 (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: 3bX1NihS9hXEggFpTR94wrRk6HNEfJOsZqKUl0TKH0yvCzMPxXhYnHPvpdd63M31qCTsls4qIVLHYKew5FFCQA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 X-Rspam-User: nil Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=kCdwlzgc; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf14.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.93.42 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 64FD16001F0A X-Stat-Signature: 7yrwj7rygs74q5bw6hbrsh7uk8zkkk3k X-HE-Tag: 1625683756-328508 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The sev_feature_enabled() helper can be used by the guest to query whether the SNP - Secure Nested Paging feature is active. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/mem_encrypt.h | 8 ++++++++ arch/x86/include/asm/msr-index.h | 2 ++ arch/x86/mm/mem_encrypt.c | 14 ++++++++++++++ 3 files changed, 24 insertions(+) diff --git a/arch/x86/include/asm/mem_encrypt.h b/arch/x86/include/asm/mem_encrypt.h index 8cc2fd308f65..fb857f2e72cb 100644 --- a/arch/x86/include/asm/mem_encrypt.h +++ b/arch/x86/include/asm/mem_encrypt.h @@ -16,6 +16,12 @@ #include +enum sev_feature_type { + SEV, + SEV_ES, + SEV_SNP +}; + #ifdef CONFIG_AMD_MEM_ENCRYPT extern u64 sme_me_mask; @@ -54,6 +60,7 @@ void __init sev_es_init_vc_handling(void); bool sme_active(void); bool sev_active(void); bool sev_es_active(void); +bool sev_feature_enabled(unsigned int feature_type); #define __bss_decrypted __section(".bss..decrypted") @@ -87,6 +94,7 @@ static inline int __init early_set_memory_encrypted(unsigned long vaddr, unsigned long size) { return 0; } static inline void mem_encrypt_free_decrypted_mem(void) { } +static bool sev_feature_enabled(unsigned int feature_type) { return false; } #define __bss_decrypted diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index a7c413432b33..37589da0282e 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -481,8 +481,10 @@ #define MSR_AMD64_SEV 0xc0010131 #define MSR_AMD64_SEV_ENABLED_BIT 0 #define MSR_AMD64_SEV_ES_ENABLED_BIT 1 +#define MSR_AMD64_SEV_SNP_ENABLED_BIT 2 #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_VIRT_SPEC_CTRL 0xc001011f diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index ff08dc463634..63e7799a9a86 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -389,6 +389,16 @@ bool noinstr sev_es_active(void) return sev_status & MSR_AMD64_SEV_ES_ENABLED; } +bool sev_feature_enabled(unsigned int type) +{ + switch (type) { + case SEV: return sev_status & MSR_AMD64_SEV_ENABLED; + case SEV_ES: return sev_status & MSR_AMD64_SEV_ES_ENABLED; + case SEV_SNP: return sev_status & MSR_AMD64_SEV_SNP_ENABLED; + default: return false; + } +} + /* Override for DMA direct allocation check - ARCH_HAS_FORCE_DMA_UNENCRYPTED */ bool force_dma_unencrypted(struct device *dev) { @@ -461,6 +471,10 @@ static void print_mem_encrypt_feature_info(void) if (sev_es_active()) pr_cont(" SEV-ES"); + /* Secure Nested Paging */ + if (sev_feature_enabled(SEV_SNP)) + pr_cont(" SEV-SNP"); + pr_cont("\n"); } From patchwork Wed Jul 7 18:14: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: 12363381 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,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 47315C11F66 for ; Wed, 7 Jul 2021 18:16:46 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E196061C83 for ; Wed, 7 Jul 2021 18:16:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E196061C83 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D07E76B0072; Wed, 7 Jul 2021 14:16:45 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDECF6B0073; Wed, 7 Jul 2021 14:16:45 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B59176B0074; Wed, 7 Jul 2021 14:16:45 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0117.hostedemail.com [216.40.44.117]) by kanga.kvack.org (Postfix) with ESMTP id 943266B0072 for ; Wed, 7 Jul 2021 14:16:45 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 066EF22006 for ; Wed, 7 Jul 2021 18:16:45 +0000 (UTC) X-FDA: 78336597570.09.D4F128F Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2040.outbound.protection.outlook.com [40.107.236.40]) by imf17.hostedemail.com (Postfix) with ESMTP id 39467F001F96 for ; Wed, 7 Jul 2021 18:15:41 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=DeOBhNNxA9S+Qw2LL6APrms+CQFCOEyDByiGvBXwCQiq72kLOCw4We4iHOgz4lXczK68qBzgKCQkNbjt8wcPK38Jn75j6DMRDUJfNLbfJWCJmwqhDOzkP1NdvJyt3ep4QyGneWGUGBtK9jqvhEW6Fo8wAbX0dOVcDRA5fv+hVc6y2QUrDnEE+Rx2hrphG6E8dL4rT5Qg2blFd0Alllggf1us1GpfvRNZi5eGZqfGvmpaaOcNmiTmCkgMvJ68Adtjv383GStCunSk/lp2o9f3U/BjwL53FnGGTdXuEd3k8wd6O2MjN8cl9SKb+puN4Z1Z/+Hnc9H81u4Ew+4jJsYwdA== 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=IEw7I2/UqGVZJYfjEijRiGlJViEJd73Y5IVmOCMH3GE=; b=eTKJBxLTcTGMHjyRwke4rYK6ud5218Wpnox+WU8SFDljpoDrKDCl/zUz3jwNsKjIO/q13K67xu8cjOTA4oMmAISvGYt3i5yrrk0seOLrCke6wMpvG5xJNbvI62MtGBKuyQljHVKz29thnDRP8dFDvC+V3/ezjkqNfWxsI6vcpbwxK95LGPusnLqCaPPKJRLSF1JTtnB7Be15NTGCQ3P3j9N8pnE9eeEpHUn4DXrAd1HoH7R8xwpf6C7mLt1BNlt9+2Cpeav0loRbyFD6lRBDvTCEJPT9B1Qgzk4W2xpjYcubKRRMWmtWQJr5QhltVeODaiXETqhxi7TqRly1odbZIQ== 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=IEw7I2/UqGVZJYfjEijRiGlJViEJd73Y5IVmOCMH3GE=; b=P2s3ltLAEzzitrQV5rhv//CR6Iu1d4wYsDo7Ap4oVQQgLh/OVi8/ryKmY5hhAlAIL8+1BU6CGq+cKlXmDsP6V5XYzny/Bn64YDjpMigcbSdY4gx8oZ/+u0UUtAgza6uz0RJ97oiafDzP9ehIfe13d9xF4RAUg+xLeYYQ2CSpqLE= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3939.namprd12.prod.outlook.com (2603:10b6:a03:1a5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Wed, 7 Jul 2021 18:15:39 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:38 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 05/36] x86/sev: Define the Linux specific guest termination reasons Date: Wed, 7 Jul 2021 13:14:35 -0500 Message-Id: <20210707181506.30489-6-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:36 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9680dac4-7a4c-4fb2-81f3-08d94173392f X-MS-TrafficTypeDiagnostic: BY5PR12MB3939: 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: 984wuGON4hl3At0xMj4JIJUB4DAR85Tl6h53Ablv/L0kduUKZV0Kbn6rGyKxQOLUVOunI/Jsp63L5cPYAqShJEHM20Cijv3sI1Go4YOBHUdpgl8MdbqIZ1lLvjypv3DDA0v8uqghyE+8kX1AwmJLoOmxCSfLHSuK2jBgl9WQKZzIw74EZ0PApHFMguGL8SjNi+hy3AHd+uGiijiF7z+PwkuuArZZzSq0waz3X3mLc2YVS1zJ5++WkjaRQcTODWNZKs5yIM9fOeLU366FI/cL5obSEDgmSoXpDM43M3CFZpZ4pWGOq8lR48PesUlfSwSHzDDJ5Gjk3f3HYXQlHUQ89ewGMicpByMPLOs3hf5/VLS9GfhQ4QsLvpNq5mqNWMvs+SdirjI1sx39ssf+4KqMsqGoVK4UF1ZyDYXQdlL1dAO29icDhvvKm789QpgwCvWAufmRb+/tNQmoX+7sZeiwYq7Jw6BNW8MDY3Um6XgYsTOFrEV8az9uDSyaWfBSFP5iZguYxvNKPDVlT7NjrIF7zqdSxr38GbIzd/7fW9kpHDpanV00R+qRWyIo+NtQfhut6zRNkv0q81LStYhVGUL80i+w4IUuFhBAtbIDgWXT6VGSIgQPz7r9f4aALubvgmbzlGu/1s0II/HxaO7K0RRUI87jhcau7li206rRqsFZq8+GSh/2GfWPoOwCCTUFsfTBVWZBbkpaHThfaDJs6bI8Jw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(39860400002)(366004)(376002)(136003)(44832011)(66556008)(38100700002)(38350700002)(66946007)(186003)(66476007)(2906002)(1076003)(26005)(52116002)(8936002)(2616005)(6486002)(7696005)(5660300002)(8676002)(956004)(7406005)(7416002)(478600001)(83380400001)(316002)(54906003)(86362001)(36756003)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wHWNsu9UvDjScKlVBGBNaa77Q1t5ljkGINhEdNxUghGVy/S2GHlGYr4/RK4pQQfmJVgFjyAPxXhAH9u1iwdWaiGFEyK5YQsVzv2aBuldsaN6pkIzAQhQM8GrBdfdVSq9t4/2BkzWWi5IPs8K0L05iB74PoVx1qjzlg8YMDYDvkxhJJyz0BMiWJGn6M2qHYHgU/da9eBm4PJQUhyUdIpAZEYRxnPIEjwHPzbO7k5ZgSXiyQzjFffe9vERqmoxka1+6mVbomz9WclC+O/P0gqWg2fa/oUPMA6AWdzWq3p963VIxK9deS1j0lCyCOpWVIYZgzdoKSrXWfBxiiBehgaIsrfli5Pr4R/I63th6SC9PrfGRnk1VUsiuoj5FDSTE3heTpJTeErZdNHEavFfWJvECcLllR8IBdvh3eMOZfJLpFkTdZPAlyyYI35wKacmi5g33YrA3oFVqpLvU4hdvVI9VYC9MbeFHpm5MTmxbj/cXI1TGLHd2y2P4WVt8zR76nZd6wVS6q63oLhCbSgy415/TiRuosaTdjYPXfTxvjg+nNIzRLP63T46HKn4c5MCkgWbalfLZ6ZHSmMOmhzn6oy4OmKc/tSbV/B+EnmCK0xdK/VqscjK5LF1XinP2Voj6AK8r+sgGvIgJ4LRwf2G7YREjnqrGNTpSiBH1RenvP4xRJsgAcwRm0Xr26jpI1Y6g0OUAytgkCL7Y9fZfENmjh0TL5ifSsbOxyOREv0eVR1et4oMBCDv6paGd9kuU7m4SvhU9pwb4INEeIt0ZezU2a7olbKrsyYWNV3SRI6Q3Wu0z/+z4G78ayVq7sikBP5nbX2OWdOQcY/NMj9BlEh+MwYuvVjfMTYhEkpl2IRSDf1Y57d5BgObOGORUQU9lY3hQ4gVGtEXnTOzFMSyzuwhqaKMLbdplKIonLa9lOEjFVDinOaqb9o8ewCnuOYmAeBzp13ipN4hrVJJGLS1NrYeqEHqFZkkkqdcSOa0tAYOGSTKEfkJlVFExz+JGK3RiD1RRl4f/t4gq1VRQX7LA6s8PwV538kapWz0E+/+Fjkkor8PssQzqbL/sorXnGLL/0ijcd7g3rpS4/kHNtTlqub9f1tUjFqB/nfutX91M5m+CLoDU5rmUSvrKr/euNWeWFui4NXJ/MGv2gMtUuGlWu27dclHTUF+czanF/xS460laux1eywkSvKDoi2VfqGEOJkL2b6Yd/5L5xGKxX+RhhLHdqe+MKVrC0cSLrxkEm+xNhKdqJDEs46VuvsZ4Bb8DGWWuPnO89PK4biHgkHcszF8yxmCPdmNXNa40oPrZNVl9raWxJDnJmEPs0WqD1eGsGtapdHK X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9680dac4-7a4c-4fb2-81f3-08d94173392f X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:38.8024 (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: NOiPTPWu8GXnPMDSgsUhZadwClfb/YtbtLk/FA8/KnRJ2RXRyrVL3N0s68AAwnb1gsqfN9+sAfq9I5Wc11D8+w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3939 X-Rspam-User: nil Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=P2s3ltLA; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf17.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.236.40 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Stat-Signature: fr5wb5j5te417byhsxcnhyid5kq18xse X-Rspamd-Queue-Id: 39467F001F96 X-Rspamd-Server: rspam01 X-HE-Tag: 1625681741-166608 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: GHCB specification defines the reason code for reason set 0. The reason codes defined in the set 0 do not cover all possible causes for a guest to request termination. The reason set 1 to 255 is reserved for the vendor-specific codes. Reseve the reason set 1 for the Linux guest. Define an error codes for reason set 1. While at it, change the sev_es_terminate() to accept the reason set parameter. Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/sev.c | 6 +++--- arch/x86/include/asm/sev-common.h | 8 ++++++++ arch/x86/kernel/sev-shared.c | 11 ++++------- arch/x86/kernel/sev.c | 4 ++-- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 28bcf04c022e..7760959fe96d 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -122,7 +122,7 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, static bool early_setup_sev_es(void) { if (!sev_es_negotiate_protocol()) - sev_es_terminate(GHCB_SEV_ES_PROT_UNSUPPORTED); + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED); if (set_page_decrypted((unsigned long)&boot_ghcb_page)) return false; @@ -175,7 +175,7 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) enum es_result result; if (!boot_ghcb && !early_setup_sev_es()) - sev_es_terminate(GHCB_SEV_ES_GEN_REQ); + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); vc_ghcb_invalidate(boot_ghcb); result = vc_init_em_ctxt(&ctxt, regs, exit_code); @@ -202,5 +202,5 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) if (result == ES_OK) vc_finish_insn(&ctxt); else if (result != ES_RETRY) - sev_es_terminate(GHCB_SEV_ES_GEN_REQ); + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); } diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 23929a3010df..e75e29c05f59 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -63,9 +63,17 @@ (((((u64)reason_set) & GHCB_MSR_TERM_REASON_SET_MASK) << GHCB_MSR_TERM_REASON_SET_POS) | \ ((((u64)reason_val) & GHCB_MSR_TERM_REASON_MASK) << GHCB_MSR_TERM_REASON_POS)) +/* Error code from reason set 0 */ +#define SEV_TERM_SET_GEN 0 #define GHCB_SEV_ES_GEN_REQ 0 #define GHCB_SEV_ES_PROT_UNSUPPORTED 1 #define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK) +/* Linux specific reason codes (used with reason set 1) */ +#define SEV_TERM_SET_LINUX 1 +#define GHCB_TERM_REGISTER 0 /* GHCB GPA registration failure */ +#define GHCB_TERM_PSC 1 /* Page State Change failure */ +#define GHCB_TERM_PVALIDATE 2 /* Pvalidate failure */ + #endif diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 34821da5f05e..c54be2698df0 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -36,15 +36,12 @@ static bool __init sev_es_check_cpu_features(void) return true; } -static void __noreturn sev_es_terminate(unsigned int reason) +static void __noreturn sev_es_terminate(unsigned int set, unsigned int reason) { u64 val = GHCB_MSR_TERM_REQ; - /* - * Tell the hypervisor what went wrong - only reason-set 0 is - * currently supported. - */ - val |= GHCB_SEV_TERM_REASON(0, reason); + /* Tell the hypervisor what went wrong. */ + val |= GHCB_SEV_TERM_REASON(set, reason); /* Request Guest Termination from Hypvervisor */ sev_es_wr_ghcb_msr(val); @@ -242,7 +239,7 @@ void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) fail: /* Terminate the guest */ - sev_es_terminate(GHCB_SEV_ES_GEN_REQ); + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); } static enum es_result vc_insn_string_read(struct es_em_ctxt *ctxt, diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 540b81ac54c9..0245fe986615 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -1432,7 +1432,7 @@ DEFINE_IDTENTRY_VC_KERNEL(exc_vmm_communication) show_regs(regs); /* Ask hypervisor to sev_es_terminate */ - sev_es_terminate(GHCB_SEV_ES_GEN_REQ); + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); /* If that fails and we get here - just panic */ panic("Returned from Terminate-Request to Hypervisor\n"); @@ -1480,7 +1480,7 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs) /* Do initial setup or terminate the guest */ if (unlikely(boot_ghcb == NULL && !sev_es_setup_ghcb())) - sev_es_terminate(GHCB_SEV_ES_GEN_REQ); + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); vc_ghcb_invalidate(boot_ghcb); From patchwork Wed Jul 7 18:14: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: 12363373 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,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 16AE6C07E95 for ; Wed, 7 Jul 2021 18:16:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A5A6661CC8 for ; Wed, 7 Jul 2021 18:16:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A5A6661CC8 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3B8D96B005D; Wed, 7 Jul 2021 14:16:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 390226B006C; Wed, 7 Jul 2021 14:16:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1E2DD6B0070; Wed, 7 Jul 2021 14:16:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0052.hostedemail.com [216.40.44.52]) by kanga.kvack.org (Postfix) with ESMTP id EB6206B005D for ; Wed, 7 Jul 2021 14:16:22 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5E89A1BCC9 for ; Wed, 7 Jul 2021 18:16:22 +0000 (UTC) X-FDA: 78336596604.04.180F58F Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2083.outbound.protection.outlook.com [40.107.236.83]) by imf02.hostedemail.com (Postfix) with ESMTP id 951397001BBC for ; Wed, 7 Jul 2021 18:16:16 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VyXJqZZGjhpxt3ZsZvd7kHnXfnPjbkZUq/p05tbOOCWLtpbypjxZKuFB0LwTnMgt9qQFRHiyCTiQEe+0B1LsBirY3wqWZkTOSXM5Ic8G6ULI/Fsbb7IXtm98c5Wi/rnmKfuVv2vR0H14oMXbcyFgJTe6zw7bmSiEyyw8BiC+YKZpgxMvVywLQyyhEr6oWUHBP2wcRa8TNfzirR6iALV64jitdCUmXwkUFpmwEB8+/lpYid1S5Ia3wnkJdSStvYVrq2ZpHT/VDTTJ1G/pCu7PSIVgG0Gj4ZhgtGC9TnK9tMjdWPNaOF7dogtfTreXEXw9GcXt7ZdKb5va9RCOx2oUTg== 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=RtNhJC0W4p6xNKQnWOKJN8mlkiNi8O2qF5UIBoVBVOw=; b=YfZyVHshPuMYAFWPcuLL39lVIOdp2zOkk3GRF8NkZBlZT/qhbDZ5LhmHqVxysHFVNCdEgPezazXhPfhxce/i5i0fA4YI9fRyvgecR3Lh2sQFujagm49Ype2i1vIQQMD9k4dRLcq15kGnn84TX68WMhfkTt8JSK6GvVoVQLXum2tchbuexHqkN7toLyiPhAY5LwlvtK8cVnNTkRhLVmF5tXxCeIAXhoxRH3TAQZRxbn1avvsof2NRnul2dwA13Mm7HG8snhbcQHubW/THLBPo0nbpDgBn/BgqCByMY+o+NLA/bew6wkZh+rzpZTrsf0Zw/Nq/tlLcyUnYSmtLuLiFyg== 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=RtNhJC0W4p6xNKQnWOKJN8mlkiNi8O2qF5UIBoVBVOw=; b=lEUk/druT9eH4skeR68qo7sABFX7YAuTBqBS1HE94YBtTKXlxIjC8/M1Wv0D0tzTHFZh7kiS3zAhaKmlr18nOYTjp205KnUwTpOMaVopKezR+xM1ipFGYTtLXYihrNVdYrKavQPdLdS3fIB6yr/77P1yrI+tn/ZWPstrwrjvCj0= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3939.namprd12.prod.outlook.com (2603:10b6:a03:1a5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Wed, 7 Jul 2021 18:15:41 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:41 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 06/36] x86/sev: check SEV-SNP features support Date: Wed, 7 Jul 2021 13:14:36 -0500 Message-Id: <20210707181506.30489-7-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:39 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3075f44e-757b-4c3e-c487-08d941733ace X-MS-TrafficTypeDiagnostic: BY5PR12MB3939: 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: wSgaYhYKlprjJJQgh7EtryRyCLCSgHUm8S8QQe0b1h97bp22AWclV4xD9ph9y5q/CdS6xnTMkD1HAoDfGqsL1n9Ui1nEBqYiwrROEX42kMIR48b+FS2uq/0ubdC3XbLc5uMZGG6l2lnqwCrGif1JLnaqfYYdhISb10h0aJUHx41emKS9P0qYlOm+avCgIUh1nhKBkoxkwc2wNUXQcDbLHilmlhKxqAdgY6k5c97x4Zq8VzqDPyJ7gXXdh4/z0caTI8d8HYclqHuiWbzHyUfmonSSJABifQmZUUE6DrWGMKAsAdcf1JaSD4QrubZsAJeBP8gJNYtBbz+6zR/pxpYDFguo216u6KU/OtqEPuS7Uxl4h49S3ZTQRDKU+dnCIn+9kAaVvP3XYcufPd1S9FsMTZw0zq1cd67MmUltDg6Ab0YPppFZoN0utr4V2MM55YVFXtv/WXgV8tXjN2gpDQmndNv7ngyqcaob/Oa6gkJ5ZRnGNLrOaSK6hX6Dm1cS1wHj+r1JtPkPZZixssczcPD6cBI+HzO+0WcGx4yF/d0PQysK7jr4i/cZMKgU7Ag0EE2hCMS8dl8yaRG9BfS2neP6q4HvLUEo8Mu9zX2S8MYeKTEDnvTWv/6TLOsPyb1EpIs7HYGwP/DE7ozWfFzB+wtfl3uzR7zgkA39AINki7R9A6txPJShpbKzppBgMBr6LuysQuqo3SJIKlyWAXPlPwaVhg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(39860400002)(366004)(376002)(136003)(44832011)(66556008)(38100700002)(38350700002)(66946007)(186003)(66476007)(2906002)(1076003)(26005)(52116002)(8936002)(2616005)(6486002)(7696005)(6666004)(5660300002)(8676002)(956004)(7406005)(7416002)(478600001)(83380400001)(316002)(54906003)(86362001)(36756003)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tp7BpiXfDPSUwmkmzMkIRLZdbcXlTl0f0G4p6ElpOHdbg/gHWVlwrdPCMGJQO/F7cnEO3rOSAG0xxe1ynIaC0PIHFi8zSofmzaYxg5RS7o0fO9p9hhqxqlaV92ydK7N2eV25oNEiMAkIn+2AH0/y2zsnohHK6S0F0OMvL3G94RYJrS29TXPCZZt1kFpsLrK7WPsKwDgxTwoB4+UmiIDfTngnuByV8fFu8l78JPddvQTWRW19YMDWbLYI6qSuWLL3nJZdIjeDOl7zmH6qGRv8BR7BvUi3Ng+lJ8ubt6nM09mIJnT+3A99QxFIK1XbnqsOT7EywKGlnUluIYHxFrtrqb5rf+pC1utPus6NXtMC1u39iVf2q5MxqCmqhEZ62jxdrXZKIApPQ7e2vnq4sb3E8fXAR/a0n1UvsPYXWPqFCPyFokEjOxDBVgZ5oZP41T8cvmD9skXwXE2wih1aRnh/YOshNcdPLCN+spY277N/pa2sxf3GN1lFzp12xQNRyazlUeYP1NC6/qA3qdcj2ykUGDIsiSoO0tSaV5AvBMfAzMNFYz4ZiHGASDn5pQC71JpBXIFv6MILa59OM0EausT2ilqlG0WrtZoJ2TnqBk2u+kk5AjU7D7GdzQPysKLZwybfLNWe1Pjqeraqskbm1Q96afhd/9zeg1xJYhBlKAMDM0E8TT2Qf6tsIH1p1u0w8/E2UhPxnYWTkCDr7YccJpQB1eRznbk22kb0qY20akEWkaTCefs9gAvTmXkYVyYUBll8lGmN/vgmBrHTRT7YqSIyPTno15UeRrlNd3E8jOzpRncoRbvFMe4zVkSF20jc96TCFYh6QHpbAjvq3+9gsFRSahpjQh29hyMDyLwPugc15m0r5ACb2TdmtP7I5Kc1dk9/AWimZ1Y/I3yX5ILGR5vNUbz4mgt9AzQeUndySjddK49tiZuW2/jSLzKVLkN5U/A6fHD1yU4BQmfoXRCK7tdIAwL3jWMn5LbORHtOgxNW6fctVYHW8YlsOJl7PRUam4diK9UUGYmQb5PyrXVaMbA33irLrp/B2ljgx8gO4JjXjxWU5GhghPHQoMu8a9OjHxsjvzpp6cakyi+4vhI+rAW+Q1Zti14xaxAklXky9yYYravwW9O/LcMxrfN6romZ+osfOxq+Bs3oKT/YP4dlFJslZ2vhmxKGiZss/I4cE5pBFk8eSLDsqZ6Qfg8lKM46zKYD7MDmUNP+F3yfmBZZyE2RksxSAkAb1T59jnuKs2+XjUOtk4QDVGJcWhKBy4RrxNu1tG/eV8Bq62FjuLXVShr3tLOXd2yEPEh4gLBmhGx7zQ/kdll7DcP1XxLpmabBgedy X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3075f44e-757b-4c3e-c487-08d941733ace X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:41.5388 (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: B8uyJ/Ne8+jJJhDRvsw68E9+fz7+Hmqd+5PGpw22jj/cj/BMNdvtZOYbjj7m3ur9qHvtYvljwUW//flwpjz1Ag== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3939 Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="lEUk/dru"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf02.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.236.83 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam02 X-Rspam-User: nil X-Rspamd-Queue-Id: 951397001BBC X-Stat-Signature: gpwfr8r91cyu87pmkaowznhrtifnwqhr X-HE-Tag: 1625681776-57041 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Version 2 of the GHCB specification added the advertisement of features that are supported by the hypervisor. If hypervisor supports the SEV-SNP then it must set the SEV-SNP features bit to indicate that the base SEV-SNP is supported. Check the SEV-SNP feature while establishing the GHCB, if failed, terminate the guest. Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/sev.c | 26 ++++++++++++++++++++++++-- arch/x86/include/asm/sev-common.h | 3 +++ arch/x86/kernel/sev.c | 8 ++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 7760959fe96d..7be325d9b09f 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -25,6 +25,7 @@ struct ghcb boot_ghcb_page __aligned(PAGE_SIZE); struct ghcb *boot_ghcb; +static u64 msr_sev_status; /* * Copy a version of this function here - insn-eval.c can't be used in @@ -119,11 +120,32 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, /* Include code for early handlers */ #include "../../kernel/sev-shared.c" -static bool early_setup_sev_es(void) +static inline bool sev_snp_enabled(void) +{ + unsigned long low, high; + + if (!msr_sev_status) { + asm volatile("rdmsr\n" + : "=a" (low), "=d" (high) + : "c" (MSR_AMD64_SEV)); + msr_sev_status = (high << 32) | low; + } + + return msr_sev_status & MSR_AMD64_SEV_SNP_ENABLED; +} + +static bool do_early_sev_setup(void) { if (!sev_es_negotiate_protocol()) sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED); + /* + * If SEV-SNP is enabled, then check if the hypervisor supports the SEV-SNP + * features. + */ + if (sev_snp_enabled() && !(sev_hv_features & GHCB_HV_FT_SNP)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + if (set_page_decrypted((unsigned long)&boot_ghcb_page)) return false; @@ -174,7 +196,7 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code) struct es_em_ctxt ctxt; enum es_result result; - if (!boot_ghcb && !early_setup_sev_es()) + if (!boot_ghcb && !do_early_sev_setup()) sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); vc_ghcb_invalidate(boot_ghcb); diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index e75e29c05f59..8abc5eb7ca3d 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -54,6 +54,8 @@ #define GHCB_MSR_HV_FT_RESP_VAL(v) \ (((unsigned long)((v) >> GHCB_MSR_HV_FT_POS) & GHCB_MSR_HV_FT_MASK)) +#define GHCB_HV_FT_SNP BIT_ULL(0) + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf @@ -67,6 +69,7 @@ #define SEV_TERM_SET_GEN 0 #define GHCB_SEV_ES_GEN_REQ 0 #define GHCB_SEV_ES_PROT_UNSUPPORTED 1 +#define GHCB_SNP_UNSUPPORTED 2 #define GHCB_RESP_CODE(v) ((v) & GHCB_MSR_INFO_MASK) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 0245fe986615..504169f1966b 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -665,12 +665,16 @@ static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt) * This function runs on the first #VC exception after the kernel * switched to virtual addresses. */ -static bool __init sev_es_setup_ghcb(void) +static bool __init setup_ghcb(void) { /* First make sure the hypervisor talks a supported protocol. */ if (!sev_es_negotiate_protocol()) return false; + /* If SNP is active, make sure that hypervisor supports the feature. */ + if (sev_feature_enabled(SEV_SNP) && !(sev_hv_features & GHCB_HV_FT_SNP)) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + /* * Clear the boot_ghcb. The first exception comes in before the bss * section is cleared. @@ -1479,7 +1483,7 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs) enum es_result result; /* Do initial setup or terminate the guest */ - if (unlikely(boot_ghcb == NULL && !sev_es_setup_ghcb())) + if (unlikely(boot_ghcb == NULL && !setup_ghcb())) sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ); vc_ghcb_invalidate(boot_ghcb); From patchwork Wed Jul 7 18:14:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363457 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=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE,SPF_PASS, 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 09642C07E95 for ; Wed, 7 Jul 2021 18:31:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A5EA461CBE for ; Wed, 7 Jul 2021 18:31:24 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A5EA461CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 970816B0011; Wed, 7 Jul 2021 14:31:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 947A96B005D; Wed, 7 Jul 2021 14:31:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 773B06B0070; Wed, 7 Jul 2021 14:31:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0167.hostedemail.com [216.40.44.167]) by kanga.kvack.org (Postfix) with ESMTP id 5313E6B0011 for ; Wed, 7 Jul 2021 14:31:24 -0400 (EDT) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id A7D9B183F68C5 for ; Wed, 7 Jul 2021 18:31:23 +0000 (UTC) X-FDA: 78336634446.22.79E4F34 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2068.outbound.protection.outlook.com [40.107.243.68]) by imf10.hostedemail.com (Postfix) with ESMTP id 59CB06001F20 for ; Wed, 7 Jul 2021 18:31:18 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=YfzGshlJcesDkGbjqe2y0oKP04askys0DrXrnads//xPjaAMSXJD11GfB6kgQFSW3ZUoUV8ACVLxQIQbJI08idiGpgAeo8rvUMEZwZUn7S1PClnteknFuLU6156aqTO2Tko/v7+lIODqJaQ6/2SeTA6S6RAi0pXQAu6+D+NgaL8fVM5uust5rsnhJM2Hs+K9TzsRtq29HBfCYqpsQOPXbbPVF+GXOUndAyuySUtE15OOJzLaTGxCNCtiRldA2/4L2ba3lXbtp+SE+ZqRlY0KChWiCLgqODw7B6cHEn8hnAQPA80/PiDWHzh/9dNnJvgDFud0GxuYuH8Q+Pdag5/g6Q== 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=auJREV1mAE69GKyOzop1JjXtKKGgG6Klxx/V2yHFQ3g=; b=OYG+R6gnHDIKl8kwHZ+0i9HSrJ/gbu/2mz1qQ03bZCLgubEoen8uSGIkTAua8WXbdlW9xSM1h94Y4FgCgy5LeoLVNDgIoRE4BZPOAeyvNVixPAkkXi3Walz+f9rMK+xdixUe5gB+g/aL/hWn4pJ32RymWBFZpn4P3kSLfg2S/p4rp/aoe1+CXyiUyX9xYXER+r2qVJLEQGzzE+hlr99+CXquEXxPqXyNvaloOkgoxqflAf3VVsksj5vlAaNtDeaCxluxdicHL0s6w1oGzRv3I8erFGQviIAa2X7Fdd9+dQ/AeF5zKqEtMrBHyOc6KIagcNsmFGt/dGOA2FvSrCxEOA== 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=auJREV1mAE69GKyOzop1JjXtKKGgG6Klxx/V2yHFQ3g=; b=HrSGsT4R0zn7wwqppwYYfywID9vfpjr0B1JNGy9f47RkLCvA2MqGFsiWkaZDkQO5E0k1DYiaYaZEgyNmwDTJ0DVuhKICr7CeAKJnA935jhzFajVSVviSq624K0chhRzXIgL5ts4yLi7OhW2B+e6fUvfZIlRH/ypcbkrpVYcaQLg= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3939.namprd12.prod.outlook.com (2603:10b6:a03:1a5::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.21; Wed, 7 Jul 2021 18:15:45 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:44 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 07/36] x86/sev: Add a helper for the PVALIDATE instruction Date: Wed, 7 Jul 2021 13:14:37 -0500 Message-Id: <20210707181506.30489-8-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:41 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3eb902a3-cd3b-4552-4c5d-08d941733caf X-MS-TrafficTypeDiagnostic: BY5PR12MB3939: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: F7r/4kdHQ0rInqhvejQW7Zp8vNvNMLLfVj7/Jz/Rj+rK4G4w1NbO0Af4kRGdGETs4ZmL9XtzrO58LkZSA9Qj+ldYVUcCv04G7gPXYGG7ZwwSnvuc6xx8LaLhKXUoztdT3blUG/MWIhJxt4rIjQKTQ0yfIA8UIP8RcVti84cZcdpKA8bNeNer0I3tUWRlLvcdrqLacX15T3CcTGl09dRhynbnsFHQ+uto1yi6+q6RkbvhtJRT8IGQ7twKeN9iyH2Hu5YcCwdB+Bmc64b838v9mwID1EMzTomr1yxfPjtA+1wHN66VHZxGltiVs7nnprd6iq8FilveRbl7WAEWc3EJYz2s+wBdn5OTIZ6+Gdt1Iyej8HpXYjWpQ0MXXlww2dPZk8qGYXIHC8pLdj9sdJnIELAJdjNeJQztFuBaT5X/mEVV8U6tncGEge8zL6URcIG3BTUKBUZYOTPMBX2oOl5R6Gc0tQ0MvnnfrMLCJbp6IsGNVmpunAfeV3GVD1iMZdFbMVEVS7VGfojHWuTQ4wiv/buHQk7CnRHB8VbPlxgtFY98hqJI1G6EvouSEjCBGDSqQ7zSumAFjnuaN7D0hCrNUHWMt2sxT3xaqoc3t/sq/ocO6143JjjQtvjdZ3YmncdvACIQuZWCoFRlQdISapyD582ls0AIXQWil6kyMrp+r/NNEcSiQbftKOoVnHLVJ2xG X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(39860400002)(366004)(376002)(136003)(44832011)(66556008)(38100700002)(38350700002)(66946007)(186003)(66476007)(2906002)(1076003)(26005)(52116002)(8936002)(2616005)(6486002)(7696005)(6666004)(5660300002)(8676002)(956004)(7406005)(7416002)(478600001)(83380400001)(316002)(54906003)(86362001)(36756003)(4326008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?zKsC7ogO3PKg30J3O3jNOn6jGQ5R?= =?utf-8?q?kK4iatw9J0sJouB4gyDKppNDo5Ibj6v5gQs2HIgJLmrdS6PJ2RWJgNXyZHVlpO3NZ?= =?utf-8?q?36ar7pdpJJGgxJQCIGxMJqdBZ+K8ch7KhzIloVPfkFr6zFs/nhVemIh1S+fo8ZT+/?= =?utf-8?q?4BhrrFQW0j6JNWSEp9LDoDDx2B/FdL7X52Mw62bePChVXSlKyYWoFF2tPynhcpds0?= =?utf-8?q?Pb0gJviwLCKUHQQs/hXl7SQ1Yy0mtQd2hoW1A18/jrbyFT7PbJDnG5u1EB8pqiAT9?= =?utf-8?q?JKv7/4uD+SJy0yJ2pNZj5JFU9PHzGU+PIPVeTwT1Jrpz8oeM9cMVD9t18/EBWeXUi?= =?utf-8?q?2BnPGoLD8CA1AekkiXRn1CBvbMPFBM4dDCo2l+tKQFpWdcBTF5tBMILOCMB/bBnYm?= =?utf-8?q?25SnUHl8AKB0W2NmJeyJzIf8xFC53UFSyFV6xyjzTHeKD05sNbzU+N89UEcNjGh2j?= =?utf-8?q?pcUfZCYa84gAjuTZHjPLD+Vj0jkG9oEAgeWOcnjhbJpHjnrkXAaRra/rMHpoCTW6C?= =?utf-8?q?/BFgznNPQqnrOJdtMSXKOQ049cgvn72F9+PNUJv0Ffh0Ng9mOz3uknKgrIJRVazZp?= =?utf-8?q?fIEnV+LIBz6Z3qzryrmnh8OcCfCnFfaJZSHIPuZCfdzprALTtfxPH28h1nGv4Jeuw?= =?utf-8?q?NSsM0orD6D2izKgrQHLAoGgP2Ie73tg1hEFLwHgSB+M1VLGpkLJsjv/IEDdUNtrRM?= =?utf-8?q?lOi+OvBTpdFj9N86FWNhgWibeULba9cIxMT1D/XLaHU/Y22M0N1YCMQDBuqmunLW2?= =?utf-8?q?VpAteCwX0JPJn5mAMIWaFQI0RcgJvxOT2INkocY50SFCWhq2Yw75PVDuYQQZVTd/d?= =?utf-8?q?fCWKd0gchmc3NV1wkqRqFKmcGrrdgWev9QD5ullMxtp0QWDp4NZXlWfbMviTjWvcj?= =?utf-8?q?IM2Is6jdd82/81V/L/Y243n8ABcoGaI1LcQSTr8/MW2BrfB7Bx3mG7xSN0WK6gzgm?= =?utf-8?q?EOKfVFI5O2zy2fqJWpsOuT2Oc/N5ly2ZPjLbWI+l1B7z8kJjfTHxK+jfn1bx0pzD5?= =?utf-8?q?YH/kX/5Ytbh2SGE6oklwDoGBtnXG9tNk/rk1IOsSY8/3owYCWuUdo5qHkYyx3yfgb?= =?utf-8?q?zoDznzCxWu5xdXqTDCqkYkIMPsmm36NTkoZHGldRcEgC5MLnf8tooE2KbzDCwA5QN?= =?utf-8?q?lhon2UVjMbD+K1uB6uN48kAGW1EHydu/QtK/jOp/hs95nEV2jjVx9j1KjrscsvUYS?= =?utf-8?q?oKoa6+qCkF8srYKli802q/AcOf2yvQKhygBVFmg0i5/UZoAWYPdpz/Uc0dpuhrSBb?= =?utf-8?q?28zgSVZqEFyxjh8r?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3eb902a3-cd3b-4552-4c5d-08d941733caf X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:44.7699 (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: 6p9PEr6RfVFDsSCfxqGsddjjtc5oD8em6bLTWmjdOC1PC6T4aITahjzBV+Jc09hEAvMPWUIHlRVlT3EFVGgKqg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3939 Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=HrSGsT4R; spf=pass (imf10.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.68 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: fubr3n65utboamj4bhznuqhtsmze5zrf X-Rspamd-Queue-Id: 59CB06001F20 X-Rspam-User: nil X-HE-Tag: 1625682678-4572 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: An SNP-active guest uses the PVALIDATE instruction to validate or rescind the validation of a guest page’s RMP entry. Upon completion, a return code is stored in EAX and rFLAGS bits are set based on the return code. If the instruction completed successfully, the CF indicates if the content of the RMP were changed or not. See AMD APM Volume 3 for additional details. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 134a7c9d91b6..b308815a2c01 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -59,6 +59,9 @@ extern void vc_no_ghcb(void); extern void vc_boot_ghcb(void); extern bool handle_vc_boot_ghcb(struct pt_regs *regs); +/* Software defined (when rFlags.CF = 1) */ +#define PVALIDATE_FAIL_NOUPDATE 255 + #ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key; extern void __sev_es_ist_enter(struct pt_regs *regs); @@ -81,12 +84,30 @@ static __always_inline void sev_es_nmi_complete(void) __sev_es_nmi_complete(); } extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd); +static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) +{ + bool no_rmpupdate; + int rc; + + /* "pvalidate" mnemonic support in binutils 2.36 and newer */ + asm volatile(".byte 0xF2, 0x0F, 0x01, 0xFF\n\t" + CC_SET(c) + : CC_OUT(c) (no_rmpupdate), "=a"(rc) + : "a"(vaddr), "c"(rmp_psize), "d"(validate) + : "memory", "cc"); + + if (no_rmpupdate) + return PVALIDATE_FAIL_NOUPDATE; + + return rc; +} #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; } static inline void sev_es_nmi_complete(void) { } static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } +static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; } #endif #endif From patchwork Wed Jul 7 18:14: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: 12363459 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,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 17769C07E95 for ; Wed, 7 Jul 2021 18:31:43 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AE94261CC9 for ; Wed, 7 Jul 2021 18:31:42 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AE94261CC9 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9E5196B0071; Wed, 7 Jul 2021 14:31:42 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BCC56B0072; Wed, 7 Jul 2021 14:31:42 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80F986B0073; Wed, 7 Jul 2021 14:31:42 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0113.hostedemail.com [216.40.44.113]) by kanga.kvack.org (Postfix) with ESMTP id 5D48B6B0071 for ; Wed, 7 Jul 2021 14:31:42 -0400 (EDT) Received: from smtpin07.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id BC678183F6A32 for ; Wed, 7 Jul 2021 18:31:41 +0000 (UTC) X-FDA: 78336635202.07.64E8755 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2046.outbound.protection.outlook.com [40.107.94.46]) by imf05.hostedemail.com (Postfix) with ESMTP id 2371A50060BE for ; Wed, 7 Jul 2021 18:31:34 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dIzVYkdVHJF+iXUwCyF9pVtDLvGwkdEOFIgVXkB2UDZcEaVt4WQCXQjWByJQmSncqLTd4r6jhb6u6TB1EE2eJpVWytp8xRH09s/C+wyAsPrJjrI1t3MSM1mDWLFIJlUl48qBO7bGsA58Eoa6dVL3CXoNrE41WbOYiZsly1rXy4Pw+BR/Az2KEOBKfY6ShXC9VJwmZWBy2J03dBwS09nNuFepPBBt3fYUHlch2MualrubW4w3+sSSMPjUos6Y5EIySaNIZfmG2vskL94wQ0m0g4CTc1p73Z5SzSOV23rVCV8M+lUhviIW9cflXoI7/rbYp2X2OEAKX7fMYkkeo0GNmw== 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=jvoY8el3yU2so1Jui4tnPlK0mBoD4b0ju1oQd3/iTM8=; b=RXKOj5Q9SzEou/GMuwKekuvUS6kI27SgFTaLpeRvnKZb5lDJoKViRrP1W7SuKQ6CtquJX8otFLLPMGpCDVEcd6oElDnp2py7uXRrvFRro7U7AQrnazEJR4dRmAkY+Aj8TKarDHlqJVv60h4HHcnMGJC83p/rOFADjPutFmdQrwMiWLK1SF3NQiOU8aWYFfmUBLEpwCyrJOuyXXniNlikTIO1pr8ammDwe/WrjSv0CbN76PJwSPMftv7NPOwZM5UPZtFzmg4f937e3EW0y4S35JHp+91w+iE8cQGiDvBZwEqUg2xDf7YfuwIu0JgVXQbMJx7VLZixHitHvcJFOunVOg== 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=jvoY8el3yU2so1Jui4tnPlK0mBoD4b0ju1oQd3/iTM8=; b=2f+xWiifcMSzSI7sgqtVi/Qj2J+Oy+ft/kpnZgKhlGcLVxixuiKwBhOu8hR9AJMFPb/VRd9V4v2+PJlovE4aI6d90qmNRHCyS60iqQ5Gstu+qBdUd/pWrBtx4S9vvktDKfRjb8o5SPXuvat2iX/a1Rl4ujlt2glscybTgej6aSk= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:15:47 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:47 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 08/36] x86/sev: check the vmpl level Date: Wed, 7 Jul 2021 13:14:38 -0500 Message-Id: <20210707181506.30489-9-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:45 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c0fd1a2c-736b-420e-d2d6-08d941733e64 X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: Jd3CPoHSuuMuXk5EQiVPFl3eQ1uJDnGzMJ57R1QczEy/KMmy5ZIvc2cjWi/Jh5RejFPt/ALa7fgnzGHTmlOnhhTwR73Vvqm+4mqMQQGTLtXZxrNQwkglg26vY0978xdyIU710XFfXS4RXPikjwDHNSbFnjfoaVvJUV0lFUT3TRPaWZ0YtspORy3X0cvtP2pG6u4PhL8RfdzJ06ZTkQNlH2b4kezKUd0mmbncdsMK10DVzQr5AjuLs2jg8NnpZO1T8xUumInFilSG69N4rTQl3glk2IqpDIJlkKyryP6i3ScNYW4fObuO6jmIM0pzKYUfbe43eYGF/STBq2m0ox9mLvvExSCYbOxLdz9gWvHZzl4mSf4V2TfX+2kauro601309n0GnxrJBiDiAx7ECAa8g5bvVZ3w/LEoL+xtBBWcianCzcnqRM53h0qHhzzdRvDsaI20HVSDJ0rPdYxkenH9YHcMF7PxWfM+gixsmmjv7X4R8av87cDAlerjiUUzLFtSfZM4yHFkJxNVxwBFTFkZsG27ZJwBXDIUDIGlOrM3yxH4fNegm5KAcSUk61CnjP0uLxoPVnYAbuZ+/BP+qdzWfkKNMPQOqURbYbknuIuURv7N9acYkzDF+PonG9ClfWd7HoYcFPymAwaZ59oXmvrjSBbBiwDSUpkwErGvRFSw/UHJUVNUExx9XUsXlz5ciDr2iIDU4d9/Nd42n3jd4LKj5Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(6666004)(2906002)(1076003)(4326008)(83380400001)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: MwNYl/Km4rkrETT3j8M9Qs7XErV4lLAQjL+ZdVx7fxUiYr+XXDTG70RrolRUJst/3CwnfWnxNMXeqgUeBPxNd20hYb91Uf9PiezO3ziKar0IAh1zJW022qLeQLW2mv4B3At2zomoGdtOLw87q+1GqxvNIcDu1UrnSlVqP/63CjTdiRrc3iM10J3/wIzmu1pInFeT/erN4hfhi10C1rAH9nBRhoE4zPRn4BbtkXnSowgMfYjT5ZG0XNvFgIj+HLwxvpMnR48idxDUyt4VT4ll3+XYG8qnriTrVc6DrGKIi6yu3/32+gZDMKcoPd54R2fWOeeFRaxuedoqeERCXV5FgGAWkgi7l7ra5MSpdcJeupy+uJz3G4bya+5pBsjcVmR/iMLR9G8Fc5J4wA6jkHbBryDVUtKFURF+fq4au2bdADthGEx9uHYZUv5nb+jXUGVNC6F+4dJEbFz1OF+yhK7Dgmsb0vbOSY5uZg4oqPIDNBWCz0r4O65gk6Z6ytudOsuDWGYFHPOLcoJqFXBkzzNMkXX5qbuGnN+AwIVQ3Ov69x+u2nerC/AEUxxSkD4t7aqFeWZwAtBFvsyMiwyHsSBZSsOa51pOry41aWdUj9WIoPGxa7dv7d09gbP72eJSiYP9dxccgWHx7AJcOnT46MkJ9VD+OziWIB4Inos8PzPWBvvA8mx/rAxq8H88AKZgBRKthyfYszVCvdqAonjkdbEX29vo7ayaHeK8+8Ltrk1+ZHBsOsHKHKrlSi7lzTKzOOo6TOiDsyzGlvZ+GVB0tIeHBrHYfihGcoubJtoSWEiy7+kGTYBrkR7VQWOJba4O34CuU7CKmGpJAMpvxaDZSrFlP+zVQrQF8hWYkynUesbULM9cNq8Ehzb0OwKixKnpuR+FuOrGHJxpZiRtRyr1UsIRdk+GCadX/e3g6IV2Dn5ytNqjHxfrBYv3sYJ+RZYiQ7c7OwGmxRMwN0EwlgeBAgzWtKjdCtc+NJsDvib9JpiczeABaE9+z/Qy5CMiHROMvDV41z70iSk4jG5jK/VFg4UEbALpPmYjYmhViP6UfBEJG/bEFkoTGuDSenpOCbrbMBtSYImoztwj/TzTSzvGCwCC+WP0Xm7iBUWXQ8keJb6dhRTidEO95DxM1YeZ6wXcukTDRkZYFiTcdSWWiyS1oeWcKrG83AFX8L+F26ZNjcPqEr5a/q219RljyX2dDPAlEHgrRixU98QryOdM/FhhuN6YmbslvY6sGMZjAXcoQVWfhVCQR21kKG5fWLhs30knT34vvDRyzlIl3pRn8mXB5vZWKStln0spb4XZOeX9U0bmvSqHUdu5bBKarx/yNZN+VPPG X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c0fd1a2c-736b-420e-d2d6-08d941733e64 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:47.5574 (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: x2DVAxP2JAMlPBmvYESZGzn44/nBkVY0uz6PQVUJsCLNqn6OLzfH/3bhRDCoeBO+rfhN/bX2Yomr842Nr0kTPg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 X-Rspam-User: nil Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=2f+xWiif; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf05.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.94.46 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 2371A50060BE X-Stat-Signature: 37aacfnrpws143c3shme1zcioybyxnb4 X-HE-Tag: 1625682694-272383 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Virtual Machine Privilege Level (VMPL) is an optional feature in the SEV-SNP architecture, which allows a guest VM to divide its address space into four levels. The level can be used to provide the hardware isolated abstraction layers with a VM. The VMPL0 is the highest privilege, and VMPL3 is the least privilege. Certain operations must be done by the VMPL0 software, such as: * Validate or invalidate memory range (PVALIDATE instruction) * Allocate VMSA page (RMPADJUST instruction when VMSA=1) The initial SEV-SNP support assumes that the guest kernel is running on VMPL0. Let's add a check to make sure that kernel is running at VMPL0 before continuing the boot. There is no easy method to query the current VMPL level, so use the RMPADJUST instruction to determine whether its booted at the VMPL0. Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/sev.c | 41 ++++++++++++++++++++++++++++--- arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 3 +++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 7be325d9b09f..2f3081e9c78c 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -134,6 +134,36 @@ static inline bool sev_snp_enabled(void) return msr_sev_status & MSR_AMD64_SEV_SNP_ENABLED; } +static bool is_vmpl0(void) +{ + u64 attrs, va; + int err; + + /* + * There is no straightforward way to query the current VMPL level. The + * simplest method is to use the RMPADJUST instruction to change a page + * permission to a VMPL level-1, and if the guest kernel is launched at + * at a level <= 1, then RMPADJUST instruction will return an error. + */ + attrs = 1; + + /* + * Any page aligned virtual address is sufficent to test the VMPL level. + * The boot_ghcb_page is page aligned memory, so lets use for the test. + */ + va = (u64)&boot_ghcb_page; + + /* Instruction mnemonic supported in binutils versions v2.36 and later */ + asm volatile (".byte 0xf3,0x0f,0x01,0xfe\n\t" + : "=a" (err) + : "a" (va), "c" (RMP_PG_SIZE_4K), "d" (attrs) + : "memory", "cc"); + if (err) + return false; + + return true; +} + static bool do_early_sev_setup(void) { if (!sev_es_negotiate_protocol()) @@ -141,10 +171,15 @@ static bool do_early_sev_setup(void) /* * If SEV-SNP is enabled, then check if the hypervisor supports the SEV-SNP - * features. + * features and is launched at VMPL-0 level. */ - if (sev_snp_enabled() && !(sev_hv_features & GHCB_HV_FT_SNP)) - sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + if (sev_snp_enabled()) { + if (!sev_hv_features & GHCB_HV_FT_SNP) + sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED); + + if (!is_vmpl0()) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0); + } if (set_page_decrypted((unsigned long)&boot_ghcb_page)) return false; diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 8abc5eb7ca3d..ea508835ab33 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -78,5 +78,6 @@ #define GHCB_TERM_REGISTER 0 /* GHCB GPA registration failure */ #define GHCB_TERM_PSC 1 /* Page State Change failure */ #define GHCB_TERM_PVALIDATE 2 /* Pvalidate failure */ +#define GHCB_TERM_NOT_VMPL0 3 /* SNP guest is not running at VMPL-0 */ #endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index b308815a2c01..242af1154e49 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -62,6 +62,9 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* Software defined (when rFlags.CF = 1) */ #define PVALIDATE_FAIL_NOUPDATE 255 +/* RMP page size */ +#define RMP_PG_SIZE_4K 0 + #ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key; extern void __sev_es_ist_enter(struct pt_regs *regs); From patchwork Wed Jul 7 18:14: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: 12363849 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,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 36A14C07E95 for ; Wed, 7 Jul 2021 18:50:04 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C50C661CAA for ; Wed, 7 Jul 2021 18:50:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C50C661CAA Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id B2A726B008A; Wed, 7 Jul 2021 14:50:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB3026B0096; Wed, 7 Jul 2021 14:50:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8DE526B0098; Wed, 7 Jul 2021 14:50:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 699366B008A for ; Wed, 7 Jul 2021 14:50:03 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id CD39118209BFB for ; Wed, 7 Jul 2021 18:50:02 +0000 (UTC) X-FDA: 78336681444.09.0F51CF7 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam08on2053.outbound.protection.outlook.com [40.107.100.53]) by imf16.hostedemail.com (Postfix) with ESMTP id AFC6CF00008F for ; Wed, 7 Jul 2021 18:49:58 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZMjbwlr82qJePoaQz0PJIlB3mUgNhDqcZf+AHGzOT2PU+13RQ3AFdIij4yCZvAESXEAQ9zdZfVpYj/uk3Ual3K+6ygSESekj0TwWudIp07lU3t6l7y3t4V8n3OWovD1IlVfmsM+1UeYPOTLt8J5Kalkg7+sOTc4xHJHEFLWEl/slV1mK/Lj289o8gXy+W7QXu9XjroFolwCPwi3YPPLLVTSSZ1aGaic5WI8jCRlKVGEL7IzedIAjl3cWrWLxDSgl4KCARInH0lJzNS/Fl6p39ycRZhaCc505eH8sa8ZmjloAZ9QmHmhuEalLMEbv2Ak/XIj5cHqKX5IacfNLToIj4g== 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=6pjBkcL7QnB0SswiCg0GAFBxc5e0iY64A++7veHCNrA=; b=YAeMtvGKl6rgjeJ7zEfGsztQ8aJZcgwuavOoCKnNFGrLXJDRelMQUV4jS/1J/txDEWr2NxH1vATe1vE/FECuGjNTU9E7h+CBiiF7HytPO4OpCaFci4mYfwigD5H2JQAiZvz8d3DN6pjo33hwXQuTp5tZRmEwXLIWa0vHVUDNgwL6qaLVCdapuZ7aEyc5wQtrBV5dUz4OgrJGhRM1DpNY0Zrl1G+ucqoL1ESUjCxDF1EigqqSzHSwzt4AV9F/U/DNWUvsbbxXfJsNYcGuaeaFTb7jwBmXkNoZqckyPheWVN1jhoBqFPisIyaaIHfD0380V13yzzOa4PoI1l/23LlhgA== 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=6pjBkcL7QnB0SswiCg0GAFBxc5e0iY64A++7veHCNrA=; b=ZuDm7afMGzBpBJV+Hb1JmYNXgLr5xefXZe+7IHQXxakSmuu7vvObVX8VJ2Ny5yX+ySlUHvUHNMXD4mrvAiy3Ap8YWTqijvqJG10YZnI97mnXS8ayRFPiklxgql21tcdwEG6EC49y0/7pOTAiIIuMe+RuP1IknVB7W6E+Rr62ObQ= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:15:50 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:50 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 09/36] x86/compressed: Add helper for validating pages in the decompression stage Date: Wed, 7 Jul 2021 13:14:39 -0500 Message-Id: <20210707181506.30489-10-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:47 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a10179a3-6a95-426d-d60b-08d941733feb X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: kQcHnR6EBIPDcOFtCYbFZ8BwnWDiSpwRXSU46DTjCOYjrcqTCpFU9cCte8VWkEH+Mv9LO4/2EOMaRJjlrNRniW1ab3qvwKBzDx1qZmtqrAGWp60T9ie/jB7yL8IoIwpqzGegXTrQ8rx/gfd0HPKqKZbk/xrVHAWPsjfn0LTEUCMYO2phq4XwOjA5Hi1V/onuvxYc/4PR5ZDuIgVEQLE8xmx98YAaAt8QdifzKNDaWENRBcpgKvswFcgzK/am/VCjSk4V+DInBpE01l3kECRDm0KzIwypvvjCj3oku+pATG/W1mo6EA89JTy3meQMfhKZiAjXItBVUxANp+ZuOS2CnGl8y+AW6a4IWZ0avCRkqzYyNPubQZ42vc4OAU0WVx/VXVgjeZ3/OrsGsqxbSgZxdRPfbWo4/cZEVMg5lrEUKr7coGy/efJ0u/MqBu8T381iPTKyhVqHB5fjOG9Ji1/lNt10SVTnc/3GpDB+tGSWZQoI/G4fRX23nnRmdRgV8rFIf9oDxcXvQzIBD8givaVWy6t0Vk6luDk1wUkMntWmrNmt/ADSYOFcPzF7cPhYZ88GW1CVr3wsWKvFf9wrzTf/ITGYufXH9OWA79v59or+mBEWwSCHBeCgXAUPG1QZ/pAwXbGvesBSt5r5H2GBG1xBVC3WQMFKDXBn8vRDNYXe1LpSFgfeKmhl4d3KKmODmdXruRqC6ASjwVY2E7heBJgTUw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(6666004)(2906002)(1076003)(4326008)(83380400001)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: paPwh1kb7CD/65wPIUzFAIBBHM2KT+dzCYq/Z0SMe4kd/VyGkNCdItRJa0y84Ep4GwbgUj9pPGURdq7wBDAr0IHp9OJyXzkB9PYo7HkEBLB/6aRXc4+C0pAN1GMmhYLeI7qd/leHCJmSm6X/FAf6cGkPT8aPysrdfWW+82+313swuFMCK4+n+IU/lWBJA9CEik4oeLgc5ZBdr3jMR/ZBnJPkYJH2tYdh6Oh+TodWq/dGATSXqT+PNpUrth3GkUCGn6LTLbcXZAeEGkiyBiABXJIFK6e0sTHY7SkbiOM71ZixjTLzVIpCU4x3wD850ObZUpB8jhrBdso6aFpa+BlPY3C2QftrSUHzEpbx7IIB9KUIVKPjQBBErnZWySEIWDhR+G+M6HrsB9oeQklSj7js+KQernvIE9HN7c/aef8+oS5TUZnUQ9ZOt5x0wFjlYYlJEIt5jxsH2fwBSgWMamSm7/s51R8BbZzyekH5qvH5tJ3nXzq2hoHiVDI4tL+o3JQ3+HjgEAsYI3moeqlxYPHZBZVK3rjkmd/z+z6MRfGsDi4DF0tEPz2I2Rym/Ev12bJLgm3wiiDhK5xWCBP1MJG1iQ2pY6F3qZDnKy0RaLCP3Ex19+5CQW/L3LpkxTBZu+iGFSc6GI7DQjXGhRj0E8gOhdW+lgz6ihZRL8Mhis0/Bj2qX2+Q3/dfz5XlVaETJrzIuKH+HWREGo5WlJWNjOca6IVgbOYvDyO+4Qj7/a2hLuO7nS7U7XaN6+IPEf8Fr2ZNqb+SMfDtDyd+J2ykStYSa5AClGObNWafFI+RvPoYWinvWC1S/iZ0rbS0CZaPeNkHzE0fOAi6tZCLC1fYiyNxylAGBfcYRfWeOMorm38qexAtQGnx/tJ6ZWVUyOhDdg040byiaClDoG3Fptp6yXiqmlUg9ZLHeX18oDAF8o34wjIMY4zU3LITvwXbnn94x+qAFDK6BYFCUySUjzPFJB5HgebwpliT4KG6TeKvqHWgL1UKmN4eQpLl8DZDmVb+W4qISj1spwtMSLqQVoh/2Fyva4CmvUAxmAdavQGXLkC33sxohcfk48GCZeQI6gtn1nAjOYhsdKqec2p0wcVgRDPQNnayJRIEb6xA4lBjsOHLbHqodbBLW6IItFEQrLG80zQFtD3LRUlNP8dXKZAWMG1L5IHYOcK35x2Zt0KmJJFhY1h/jVTLmpoU2ORh1P69uCxoyBwzXSgNs3m6QxT2Rqi5vtcd7DrZJxWW50LL/r/ethEeU+0rw57/ii6P+GX+sx/L7CnXGrN7xJgw8DqGkr/Np313P2NSicBN8qSmpqHYA7q4f7N+LH+wEohFcHLTTS9E X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a10179a3-6a95-426d-d60b-08d941733feb X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:50.1399 (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: ZigCn9XvFWKpneyR24v8S10TekK9Dt/ztlhtivwLpVL38QF2cZDh7NPlLKjr5SvijBLRh0hcf3xKErhQMhXeIg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 X-Rspam-User: nil Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ZuDm7afM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf16.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.100.53 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Stat-Signature: 3f11fydsdfght7ct6m3z8dbgiwd5phog X-Rspamd-Queue-Id: AFC6CF00008F X-Rspamd-Server: rspam01 X-HE-Tag: 1625683798-716713 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Many of the integrity guarantees of SEV-SNP are enforced through the Reverse Map Table (RMP). Each RMP entry contains the GPA at which a particular page of DRAM should be mapped. The VMs can request the hypervisor to add pages in the RMP table via the Page State Change VMGEXIT defined in the GHCB specification. Inside each RMP entry is a Validated flag; this flag is automatically cleared to 0 by the CPU hardware when a new RMP entry is created for a guest. Each VM page can be either validated or invalidated, as indicated by the Validated flag in the RMP entry. Memory access to a private page that is not validated generates a #VC. A VM must use PVALIDATE instruction to validate the private page before using it. To maintain the security guarantee of SEV-SNP guests, when transitioning pages from private to shared, the guest must invalidate the pages before asking the hypervisor to change the page state to shared in the RMP table. After the pages are mapped private in the page table, the guest must issue a page state change VMGEXIT to make the pages private in the RMP table and validate it. On boot, BIOS should have validated the entire system memory. During the kernel decompression stage, the VC handler uses the set_memory_decrypted() to make the GHCB page shared (i.e clear encryption attribute). And while exiting from the decompression, it calls the set_page_encrypted() to make the page private. Add sev_snp_set_page_{private,shared}() helper that is used by the set_memory_{decrypt,encrypt}() to change the page state in the RMP table. Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/ident_map_64.c | 17 +++++++++- arch/x86/boot/compressed/misc.h | 6 ++++ arch/x86/boot/compressed/sev.c | 41 +++++++++++++++++++++++++ arch/x86/include/asm/sev-common.h | 17 ++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c index f7213d0943b8..59befc610993 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -274,16 +274,31 @@ static int set_clr_page_flags(struct x86_mapping_info *info, /* * Changing encryption attributes of a page requires to flush it from * the caches. + * + * If the encryption attribute is being cleared, then change the page + * state to shared in the RMP table. */ - if ((set | clr) & _PAGE_ENC) + if ((set | clr) & _PAGE_ENC) { clflush_page(address); + if (clr) + snp_set_page_shared(pte_pfn(*ptep) << PAGE_SHIFT); + } + /* Update PTE */ pte = *ptep; pte = pte_set_flags(pte, set); pte = pte_clear_flags(pte, clr); set_pte(ptep, pte); + /* + * If the encryption attribute is being set, then change the page state to + * private in the RMP entry. The page state must be done after the PTE + * is updated. + */ + if (set & _PAGE_ENC) + snp_set_page_private(pte_pfn(*ptep) << PAGE_SHIFT); + /* Flush TLB after changing encryption attribute */ write_cr3(top_level_pgt); diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 31139256859f..822e0c254b9a 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -121,12 +121,18 @@ void set_sev_encryption_mask(void); #ifdef CONFIG_AMD_MEM_ENCRYPT void sev_es_shutdown_ghcb(void); extern bool sev_es_check_ghcb_fault(unsigned long address); +void snp_set_page_private(unsigned long paddr); +void snp_set_page_shared(unsigned long paddr); + #else static inline void sev_es_shutdown_ghcb(void) { } static inline bool sev_es_check_ghcb_fault(unsigned long address) { return false; } +static inline void snp_set_page_private(unsigned long paddr) { } +static inline void snp_set_page_shared(unsigned long paddr) { } + #endif /* acpi.c */ diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 2f3081e9c78c..f386d45a57b6 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -164,6 +164,47 @@ static bool is_vmpl0(void) return true; } +static void __page_state_change(unsigned long paddr, int op) +{ + u64 val; + + if (!sev_snp_enabled()) + return; + + /* + * If private -> shared then invalidate the page before requesting the + * state change in the RMP table. + */ + if ((op == SNP_PAGE_STATE_SHARED) && pvalidate(paddr, RMP_PG_SIZE_4K, 0)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); + + /* Issue VMGEXIT to change the page state in RMP table. */ + sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op)); + VMGEXIT(); + + /* Read the response of the VMGEXIT. */ + val = sev_es_rd_ghcb_msr(); + if ((GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP) || GHCB_MSR_PSC_RESP_VAL(val)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); + + /* + * Now that page is added in the RMP table, validate it so that it is + * consistent with the RMP entry. + */ + if ((op == SNP_PAGE_STATE_PRIVATE) && pvalidate(paddr, RMP_PG_SIZE_4K, 1)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); +} + +void snp_set_page_private(unsigned long paddr) +{ + __page_state_change(paddr, SNP_PAGE_STATE_PRIVATE); +} + +void snp_set_page_shared(unsigned long paddr) +{ + __page_state_change(paddr, SNP_PAGE_STATE_SHARED); +} + static bool do_early_sev_setup(void) { if (!sev_es_negotiate_protocol()) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index ea508835ab33..aee07d1bb138 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -45,6 +45,23 @@ (((unsigned long)reg & GHCB_MSR_CPUID_REG_MASK) << GHCB_MSR_CPUID_REG_POS) | \ (((unsigned long)fn) << GHCB_MSR_CPUID_FUNC_POS)) +/* SNP Page State Change */ +#define GHCB_MSR_PSC_REQ 0x014 +#define SNP_PAGE_STATE_PRIVATE 1 +#define SNP_PAGE_STATE_SHARED 2 +#define GHCB_MSR_PSC_GFN_POS 12 +#define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) +#define GHCB_MSR_PSC_OP_POS 52 +#define GHCB_MSR_PSC_OP_MASK 0xf +#define GHCB_MSR_PSC_REQ_GFN(gfn, op) \ + (((unsigned long)((op) & GHCB_MSR_PSC_OP_MASK) << GHCB_MSR_PSC_OP_POS) | \ + ((unsigned long)((gfn) & GHCB_MSR_PSC_GFN_MASK) << GHCB_MSR_PSC_GFN_POS) | \ + GHCB_MSR_PSC_REQ) + +#define GHCB_MSR_PSC_RESP 0x015 +#define GHCB_MSR_PSC_ERROR_POS 32 +#define GHCB_MSR_PSC_RESP_VAL(val) ((val) >> GHCB_MSR_PSC_ERROR_POS) + /* GHCB Hypervisor Feature Request */ #define GHCB_MSR_HV_FT_REQ 0x080 #define GHCB_MSR_HV_FT_RESP 0x081 From patchwork Wed Jul 7 18:14: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: 12363371 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,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 51261C07E9C for ; Wed, 7 Jul 2021 18:16:07 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C3EB761CB0 for ; Wed, 7 Jul 2021 18:16:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C3EB761CB0 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 95EC16B0011; Wed, 7 Jul 2021 14:16:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 90EF16B005D; Wed, 7 Jul 2021 14:16:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 73AE36B006C; Wed, 7 Jul 2021 14:16:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0047.hostedemail.com [216.40.44.47]) by kanga.kvack.org (Postfix) with ESMTP id 48BD76B0011 for ; Wed, 7 Jul 2021 14:16:06 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 8987F18369443 for ; Wed, 7 Jul 2021 18:16:05 +0000 (UTC) X-FDA: 78336595890.05.A31CA90 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2076.outbound.protection.outlook.com [40.107.237.76]) by imf09.hostedemail.com (Postfix) with ESMTP id B1B613002E65 for ; Wed, 7 Jul 2021 18:15:56 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=nqg9kVkqOUYCQzn5Qmaa0U1SeG3qk3VZ7ocIoM/adniiaSVVVoscxweDcVb/L6m9iSDkHb3sEUpGN8hRSMKQcj3KfiUfHs0VJ5t2A9vtPZA2pU89bKj64mgViykMaB21eG5Slbvfh+ynt55SbjP4VtKYlqDgWw2jLQcnm3p2j/t/Mc++xDIdMxrf2xtRWAqaBAFi8aXawoLOgtUpdJ8yoI6+PdYK7gHYLMu6Qtf+vVcAcW5D6Su7xxUWKZgHKa+GXQIelJl4OSnwsToMRZU24xxghAlO13YR/g0u4rvsZiP0EREHhqffpCjV2t5BgaAlnFkPlgTqacOdspZR+JC0Lw== 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=6AhqcPsdf05x2f48CtwiBuMds0v4HsWlxYUp+Nw2Y6E=; b=IPfBav54KB//clpLvn2ZT2GZDWu2FO8YpXVfI1knZvN5mdlmS1c77S9sWu61RFEKGnWKkLM6ObUwsQ95Q2RJL9VDRMSulzzf26voqXKrTDkdgYW19thBgSRG1ONWLUBAZjNNDX+NTUQkgj47WIn3UyJf4PCW9wdc7maqndQBCM9huRKf8Mim8X+pHAYZ1gAmcLMnkyBUi6xFjoaNojKjWBzqicDeMGkyarx3/EqBKceg1vPh3EA5msH1MLOgWl1kG7Ach7WDlCdKsilznioFX3Z8JbQ2MKaYI2rjWJy9t2ra8NMs1ZGZs02bCZlNfe0TDl7oszbMb1ysXfqU3PwRkw== 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=6AhqcPsdf05x2f48CtwiBuMds0v4HsWlxYUp+Nw2Y6E=; b=0mluJoapzqTja6VBx3KN9bz7Tyr5r90z5IH+hm2jtPpZai2vBI1A+X97QIRaDkII303nN13ncGUB9CfYN7LtU0DEU2y28S6eF8t43lnC0ASyPSHbkaPV5JQZDoLX3Ki2dfXMiu1RMDOGGKHJp3h9mSDqUmsmBCMVF6jRXFVnjAs= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:15:52 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:52 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 10/36] x86/compressed: Register GHCB memory when SEV-SNP is active Date: Wed, 7 Jul 2021 13:14:40 -0500 Message-Id: <20210707181506.30489-11-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:50 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 87579292-6c40-4a99-2375-08d94173416c X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4A0Flyfz7nbKhdjVyY9B0yI6MA+LcGwGLFd18ZXWYw2SXk7phz0mLAL2gxvphsdLwhoj5G12EeiuFSyjvpSepgnHEAiyyAxVXLonlvzHIoKLEZ3xPcQqPxTgCUfWWW8Hhxboglwzz5bvtrVmE4GIRWLlRQ7hLO1h+lWid/iBEZl7pgMjgLsgBSvQZq64WDJm09IOusxMCnR0aEeAxq9DvBSqg9BoaKumBkxFpOZ9q6Ngf2QYVZGtnM4mfA4fnfRWWCltD8MLDlvvwDc0U9Ohx1yRNpx+aq1gW4ifATFxJC/UOvNTqvC2S9qVrTJYnY2XRBZ+kxh6IBqViksmRQgAlpz+Dpplca473o+cCo7f0lKKQeIAMCrgpuAnBpry2eAI9jHOgAMNc3KImEJpcOfrrTGAblG+tHiBAPbMByzhIB6br1g4+pZtPO5b6fJfF8ArfG52poLowxH9meFe7XmOEHBsWbp1vqBK3eI5bt8OzIJXvLTnCNjnQIsyREgaqnl1c8mmndba7hVaLG+lJU+n8W6KGmN5A+oud3REzvysmFLK693u/K+pZuAbOEfEJmqH6KUzQ3L81W+goF0DTm3kpmQOhVyOL5M2+YXjU/KHLe6NEMpB2EDyOREsycQUQ8ulzpvoPRezcdUM93e6Pp6eaWhRPBgAGiHR+XB0okTUNZgmRpSHKkBzDAOhFENSmle9dSzEt+Fx1x4RlTG+NJQP5g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: UgxNCXmCUgUTMmWMAb9PdrjwG0pwFEUihMQVcJWekRE97zP2gg3Tqij6gJZcCiaibTJCcJbYHsVjGjfP4idKai1atQ//JYk5ln8ag0xdQpkxpYpmWG7RUUy27VG/jY101E1W8td/QTvFSv7JhsEEk5rCxBN9m93a7HkRJIcaSup7K/igJgLiB/umPDzptk+me7vPjVjAv3dtFs63iCxtlQioNyd75yho5/WJGJmfjMcJdGU77MnAvzw6w8vL4HKSez2kePVBPDuanXs1LszGgcd578n+1oXZvHWD7OEpxCNs9VqIIi8R/6RbKYN+bj60ecdsyFpl35PaATvLBN4QsDkZ6Ql/YCZkSD5S4Hntam4r/VcIhaljtTuYRHwRQCDz6c5e2/OEmVtUUg+CZTP0ybTiob4hH5rdBcuWB82OWAtusVtPW6qlqUFE7Kku1E52D6BVSFYCvjn/80hrE7IlE76TcWosCR/RaynuqHTzueL8t7NW07ysjCWIgs2c1vAOYrjbYcF+FzVQ/29uRzlNbaS/p3AJKCu2EpgBZawNtmJaMfvIdCJiZxDFO+hAamgg28Ari6ewYYhqD4F2nDi3kuD/crJ3wsb53UIcrr8zFqp6kryjl8N7HKiF2NZvNziwxWUBmIzDLXS7/q7PSvrC/ugaFva9H2vl1Y5rt1ihCx0QPw/Au7MJhH9iU3xP4WN2yTGVuF98pWrrRGPEYiLXoXExkyf72aEVesnlU4d3HBr2d7QPzNLO0asXD1fT3vNdXsmqEVnAn3A8On5HS4g4/wclU7H8uGqe8pqgIzTdAoeD658gfBQsIxcCAhrbk2n4yeuBpWbMYvi1jcUZFBoPJNVhf7/JRCLjewYLQVpd9cnJCoT4WoG6Eo5ThndvhuDflCX8bFPqjfOI375uymtIG3iigVX64azbr+eKnIvjhiszoRzKKzQWG1drkSQgqQSbek9xfVuAVodenHm4v9UVwXOwE4nVi+6tIRObuy2D99zflRy8bhG3x5Gp+CAdFS/sd2PoucSrm0k9Ph5f2mJM9xbXAkHUL2P1pXWrkxowkOAil4iwGIuUHJLYlQq2vQ6Ki0aVnLlhQAMjwvyGYFdoK9lfUaHhIrgWwbuV9YvdUmUQ4bv5CPjrz2qx1BiRtMwuIJT6ZOiMH0gip2JIdxzV3HX4U8QD11cazQ0pFk1XBuhhyYJRV/n05LmiIdfYnfRCpEwh/uuZfHEUGm7kBXXwaBfR+2aQWEQrxUhzYiGCgpT7PfFc99JW892W9tV6QiG2vNYREYAujuGBKKXEUuI/7VhN4jnMCsUEib1jJTcVUVBoZOX4FUwWnUFNvJ9dAY17 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 87579292-6c40-4a99-2375-08d94173416c X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:52.6415 (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: a+XWePje5+pPwDmRPplcZC8E43QUtkf+UcHH5FSFQ4/IL4JgC3fz66vO5kIcy793tN6lpaVNV/kLonh79H4zdA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=0mluJoap; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf09.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.76 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Stat-Signature: 18pe6fgj1ojmy7q9ogf7scd3p8wcasgf X-Rspamd-Queue-Id: B1B613002E65 X-Rspamd-Server: rspam01 X-HE-Tag: 1625681756-742765 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The SEV-SNP guest is required to perform GHCB GPA registration. This is because the hypervisor may prefer that a guest use a consistent and/or specific GPA for the GHCB associated with a vCPU. For more information, see the GHCB specification. If hypervisor can not work with the guest provided GPA then terminate the guest boot. Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/sev.c | 4 ++++ arch/x86/include/asm/sev-common.h | 11 +++++++++++ arch/x86/kernel/sev-shared.c | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index f386d45a57b6..d4cbadf80838 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -233,6 +233,10 @@ static bool do_early_sev_setup(void) /* Initialize lookup tables for the instruction decoder */ inat_init_tables(); + /* SEV-SNP guest requires the GHCB GPA must be registered */ + if (sev_snp_enabled()) + snp_register_ghcb_early(__pa(&boot_ghcb_page)); + return true; } diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index aee07d1bb138..b19d8d301f5d 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -45,6 +45,17 @@ (((unsigned long)reg & GHCB_MSR_CPUID_REG_MASK) << GHCB_MSR_CPUID_REG_POS) | \ (((unsigned long)fn) << GHCB_MSR_CPUID_FUNC_POS)) +/* GHCB GPA Register */ +#define GHCB_MSR_GPA_REG_REQ 0x012 +#define GHCB_MSR_GPA_REG_VALUE_POS 12 +#define GHCB_MSR_GPA_REG_GFN_MASK GENMASK_ULL(51, 0) +#define GHCB_MSR_GPA_REQ_GFN_VAL(v) \ + (((unsigned long)((v) & GHCB_MSR_GPA_REG_GFN_MASK) << GHCB_MSR_GPA_REG_VALUE_POS)| \ + GHCB_MSR_GPA_REG_REQ) + +#define GHCB_MSR_GPA_REG_RESP 0x013 +#define GHCB_MSR_GPA_REG_RESP_VAL(v) ((v) >> GHCB_MSR_GPA_REG_VALUE_POS) + /* SNP Page State Change */ #define GHCB_MSR_PSC_REQ 0x014 #define SNP_PAGE_STATE_PRIVATE 1 diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index c54be2698df0..be4025f14b4f 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -67,6 +67,22 @@ static bool get_hv_features(void) return true; } +static void snp_register_ghcb_early(unsigned long paddr) +{ + unsigned long pfn = paddr >> PAGE_SHIFT; + u64 val; + + sev_es_wr_ghcb_msr(GHCB_MSR_GPA_REQ_GFN_VAL(pfn)); + VMGEXIT(); + + val = sev_es_rd_ghcb_msr(); + + /* If the response GPA is not ours then abort the guest */ + if ((GHCB_RESP_CODE(val) != GHCB_MSR_GPA_REG_RESP) || + (GHCB_MSR_GPA_REG_RESP_VAL(val) != pfn)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_REGISTER); +} + static bool sev_es_negotiate_protocol(void) { u64 val; From patchwork Wed Jul 7 18:14: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: 12363463 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,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 9966EC07E9B for ; Wed, 7 Jul 2021 18:32:01 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4662C61CC9 for ; Wed, 7 Jul 2021 18:32:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4662C61CC9 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 32DAF6B0075; Wed, 7 Jul 2021 14:32:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2DEA46B0078; Wed, 7 Jul 2021 14:32:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10A586B007B; Wed, 7 Jul 2021 14:32:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0129.hostedemail.com [216.40.44.129]) by kanga.kvack.org (Postfix) with ESMTP id E1D6F6B0075 for ; Wed, 7 Jul 2021 14:32:00 -0400 (EDT) Received: from smtpin02.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 3C47182B177B for ; Wed, 7 Jul 2021 18:32:00 +0000 (UTC) X-FDA: 78336636000.02.D40E2F8 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2083.outbound.protection.outlook.com [40.107.94.83]) by imf21.hostedemail.com (Postfix) with ESMTP id 533A0D002870 for ; Wed, 7 Jul 2021 18:31:57 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=jQ9BNAcAXBVK4604AyGc+OQABXIIDveAe4Nxbrap/T+lpNdeIkiqnz5YQkKQiao/Fi7MDLkugp6l6bUQj9sW3EAl5I4yus3jwunqsOSLhV3Z6OACz/TdAaQnNZot2Te15DittEG6I31flGEQW0wf17h5yG7W1M/bKSVL6h/Q9NiLidxhtkdgZeBGFZt1TTV5RmLHthomtfi2NhcmSBTuxQjikB8k4CbCjkYtD7eE6CJJEPkrngF8WPGP5UaK6bENQrwLvLMtd//J5UrgRnG71kQ5iONs+MQWf6CONOa2pjWMplXccoShNTVOS0GIaD01H4M9kgDzwNsL3+xEPzTOKQ== 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=tKHWGmmrOFM6/pOcwpWAUoepIw5HeaBFOBYCQZOJYOs=; b=aDw5/NrxFm2vspLn7aHzIu0TPpMp7LbU+JS8WsuX4gx057jSuGgiqJnTjsZVSeCSNQBpoKOlhoVRfob3XYIniWy09wYaiWP93eSvGO84eiGMsU7tkBlbO8+ZSnqMDS4ljR2kVOVhIeKbNrQuE84wZpnonyGbGfG6PQMHyNYlLVLysUMZ51+efGFOISKlwEkkqp7cyY9Byyyc7qetJqIpRe5HZGn9L5I3oZInwLykSPl79JRCsZzcr7DamrwcEOZGFOiq68j9H2dRSopyMft5YnFUV6Vie0jYfnIStMGrcJbR5FTRTfgth6K07lNllSMj2GYshAfg/f18sY1wajYEow== 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=tKHWGmmrOFM6/pOcwpWAUoepIw5HeaBFOBYCQZOJYOs=; b=c9jNIflm2vpHVJeTORdzCp1D7D1KeKo/UXAiScJHUQvGZjDM6F4Dmq4zxz1S8GlhQxGBRT4E4YVgEIM875EdNRCrIQeyA0d7ffzBBgWfT8m5TtyiiBKI4dsfRBaMkUrxXvmYiInCPc+D++Xk9GAG+8yNPa7FF5BRMSe6nSusXXk= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:15:55 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:55 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 11/36] x86/sev: Register GHCB memory when SEV-SNP is active Date: Wed, 7 Jul 2021 13:14:41 -0500 Message-Id: <20210707181506.30489-12-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:52 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 78d13c3b-c626-40ef-4186-08d9417342e7 X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: PJhWkP8dMg8F2RVhhvpqAovyXMnrrlbmcjocb6VQy7nrWJVIr0MojNP80yEym0d8cFwFjF5Wn3Ww7p8BDQpCgjf5hnOQR7VqXVS9tOvkOVM50D8uklDk9PR4VxDAIelt1EqWadT7SD+lxWpG+otKDF8wGbqFVH+H33uCyjUDGBNr+N/FUTSee5MlHxe9JWknD+dXqXAI0M7tCl1ami/srunXMEB/SupoLdy6U4+jeL77QSy1V5M1MlTjMTG4PRtj90X6OQ4rGwPWXD61eU9/6yLXeOXZDFf1mhIPAbibOQRnpmJKirOgB5AVFISrjNGLsIl1d+ZcyQHvQCeNLpF8b3r9vxZJil4RjmbRN5ZEUDI24BLsJ7ihXmsBtOc2v0huwHyKyPBaUqLFwoZcmJWNgiDT/F2JDPLJt7okaTubFSM7e0NhH7Xay5yKyerRLrcS52EdjAb4H6HGcTs9yBxgZW0CjZZ/VAfUmgQUO1+BUmCvpGkPOj0seQZ0jGXlkoJJuQRM5ts1M2wqaV2ENu6t+jiwFj7tAwZ7QwKSmc/0g9FjAUfOc4gAwNi8Fko61XZADDInCO0Qb6kHlnVETHKX5waYf54SynNqnacKPQbM+rt22zhcWfgk6j2KCEImGul26jloZLVzovd1QXEESc1lGHpsIBMAhLj/KRqWRNjQoQGtStUuBBU+vbazAKtts6VJCO5Rru9s0K1v+jqhid+JFw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(6666004)(2906002)(1076003)(4326008)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Vz6u+h+ylX4ZhJWP1yBGV8wgsSumBe7oPbIQKSl+MD92hC2vKizU5arQ5CeUcfjfX1VCyVZhNWZZmtxOvYJuUHgnbVq4svhpFNMPrHehQ0nrbGN+AAJiALxOHYlk65hk0Fq5RzUKh25Q3JaLhcczuCBSm1Qw/l0cRAwxMVXiLdZDI/YTrQUjqOHp1ZoEfmjhn3STnaFYsCGkhzXRUos97LACKjvMDeD1TabuHOyrlNx4wrXKh9qK9dCAPfWoRRlfNpVogxFUMH9QiwkNcYvFCGkQ/MZs1+yFCiE6DnJqRnSYUK2GhJOT0/y6dNo07EKd1zX8Pei3ZJRFqxnY+xCO1j9yEFSbgL+KUCSIFv/3gDiQ90unoKbUe8e2uJzGm8aNcH3o0mns8exKH5xoyD8WXzIvXytM/2YrfN45/gMnXKpTpv+cS0Bc0Fmz1ryBQFdAY91kayiNydgGERUMmjg9Pi9nSCDPvrX7AI3o9O5Jt9YegWyOqxfZzyciomyKGukT/YgFKciA1RnHDF02RmM6XNluS21BZtcXDxFNLPeW1qeaOhXZMn5IQnEiGs0S/u3Q3iw5+WYLfrojSn0ob8k2uHvcGAuP1WgHLBJKJY8JnS2Xzs5piZYZZvTU8L5bN3FvSlXvLOXCqqn6xMbPQuGuV9JriIO715iKqejj/cYRaeHEKTwMVHxGPxVIDKwnPO9tLaTAu8m8AgwQ//h8wwVQp9ZZUBXGqp/ziVyGrIRBgGAtSyCahDgvOLWoozeFAGVZ9q+S/uQoBUsgItQhXTbgunOlL5Zzvb52ywmYVmycD1NSjQnUqqIHyO6pQZQRDxSimSxU/OpcBIBVJLDaf/dey80RhMqkxB0IXLzXqrUTCpC0fQPb44bc1LY3JyFk3amUDdMRtqDfdYLTAPYdA6hXa/9pTjURnxR8Sfu1kNQz8LTe4UJuZQdWDnpCLBXCzZqtUxel+nd8hXnw9BVC1AVUMZTK8eIdSMHwQH1aaEQGB1Nbn4f6WlYzNGOELZTe3Zw5E6oVnsvi23Rm4IgxD/u/9hJ7zqlD6MbHk3OD6HpYR2HkGk/RpxJxxlwOJK76F9XeRw8lTEzSOF4gWesxk9bc6d7MRCI/oVs2H5HOGgwWaQcgkhV1FZl1io3U71gKHbRhAZYECN4WN4M6iJDBViNE8Hzazjmun6oNvQab/WUldBPhUHCwXyICO2fJ+WOlLfy2YJyyxEF3D6h3KU4D6MrpadqgdgmNMqlpOd2UWV7IZR4oNF8xialj9QjtmgcXaTE9Bk1AGnC6Kdq2D0nW8dXwDgHqZHBJv5h2LW5U7pTnms/704ifJZCHY8ET9ywgT61Y X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 78d13c3b-c626-40ef-4186-08d9417342e7 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:55.1021 (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: p4+vuxkNtcxu0CLcRzmoAG6blLEH1+i7d3RkAm0azy8DdWj0IDuxR4TuiLby3ClPRU3Xmpfgg9mXeYDYjZ7xpw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=c9jNIflm; spf=pass (imf21.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.94.83 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: ac5m3pnnhwgsf53nk1d5bqc8k4tc9x5w X-Rspamd-Queue-Id: 533A0D002870 X-Rspam-User: nil X-HE-Tag: 1625682717-792853 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The SEV-SNP guest is required to perform GHCB GPA registration. This is because the hypervisor may prefer that a guest use a consistent and/or specific GPA for the GHCB associated with a vCPU. For more information, see the GHCB specification section GHCB GPA Registration. During the boot, init_ghcb() allocates a per-cpu GHCB page. On very first VC exception, the exception handler switch to using the per-cpu GHCB page allocated during the init_ghcb(). The GHCB page must be registered in the current vcpu context. Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev-internal.h | 12 ++++++++++++ arch/x86/kernel/sev.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 arch/x86/kernel/sev-internal.h diff --git a/arch/x86/kernel/sev-internal.h b/arch/x86/kernel/sev-internal.h new file mode 100644 index 000000000000..0fb7324803b4 --- /dev/null +++ b/arch/x86/kernel/sev-internal.h @@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Forward declarations for sev-shared.c + * + * Author: Brijesh Singh + */ + +#ifndef __X86_SEV_INTERNAL_H__ + +static void snp_register_ghcb_early(unsigned long paddr); + +#endif /* __X86_SEV_INTERNAL_H__ */ diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 504169f1966b..c7ff60b55bde 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -31,6 +31,8 @@ #include #include +#include "sev-internal.h" + #define DR7_RESET_VALUE 0x400 /* For early boot hypervisor communication in SEV-ES enabled guests */ @@ -87,6 +89,13 @@ struct sev_es_runtime_data { * is currently unsupported in SEV-ES guests. */ unsigned long dr7; + + /* + * SEV-SNP requires that the GHCB must be registered before using it. + * The flag below will indicate whether the GHCB is registered, if its + * not registered then sev_es_get_ghcb() will perform the registration. + */ + bool snp_ghcb_registered; }; struct ghcb_state { @@ -194,6 +203,17 @@ void noinstr __sev_es_ist_exit(void) this_cpu_write(cpu_tss_rw.x86_tss.ist[IST_INDEX_VC], *(unsigned long *)ist); } +static void snp_register_ghcb(struct sev_es_runtime_data *data, unsigned long paddr) +{ + if (data->snp_ghcb_registered) + return; + + snp_register_ghcb_early(paddr); + + data->snp_ghcb_registered = true; +} + + /* * Nothing shall interrupt this code path while holding the per-CPU * GHCB. The backup GHCB is only for NMIs interrupting this path. @@ -240,6 +260,10 @@ static noinstr struct ghcb *__sev_get_ghcb(struct ghcb_state *state) data->ghcb_active = true; } + /* SEV-SNP guest requires that GHCB must be registered. */ + if (sev_feature_enabled(SEV_SNP)) + snp_register_ghcb(data, __pa(ghcb)); + return ghcb; } @@ -684,6 +708,10 @@ static bool __init setup_ghcb(void) /* Alright - Make the boot-ghcb public */ boot_ghcb = &boot_ghcb_page; + /* SEV-SNP guest requires that GHCB GPA must be registered. */ + if (sev_feature_enabled(SEV_SNP)) + snp_register_ghcb_early(__pa(&boot_ghcb_page)); + return true; } @@ -773,6 +801,7 @@ static void __init init_ghcb(int cpu) data->ghcb_active = false; data->backup_ghcb_active = false; + data->snp_ghcb_registered = false; } void __init sev_es_init_vc_handling(void) From patchwork Wed Jul 7 18:14: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: 12364003 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,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 310C1C07E95 for ; Wed, 7 Jul 2021 19:49:48 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B942961CC1 for ; Wed, 7 Jul 2021 19:49:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B942961CC1 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 971136B0011; Wed, 7 Jul 2021 15:49:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 948406B005D; Wed, 7 Jul 2021 15:49:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 79AEB6B006C; Wed, 7 Jul 2021 15:49:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0056.hostedemail.com [216.40.44.56]) by kanga.kvack.org (Postfix) with ESMTP id 53BC26B0011 for ; Wed, 7 Jul 2021 15:49:47 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id A8EDD8248047 for ; Wed, 7 Jul 2021 19:49:46 +0000 (UTC) X-FDA: 78336831972.24.5AC9611 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2055.outbound.protection.outlook.com [40.107.223.55]) by imf23.hostedemail.com (Postfix) with ESMTP id 744C090000B7 for ; Wed, 7 Jul 2021 19:49:44 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=OOf3LhlV7NfmTWwUTqoAhmXN2p0xS4XGq5u2y/qs5uqa5PTiYn4HurKjXr678OVNr+2Kg3l1/izQ+wmZxG+mRZWMs83EpTti7vsnrtpZgbf+1p/ShCD14z14zw3jMEa5SD0m3IJdJqOEupgsiDORmjPxy2EHs6fyUx0yL/2+ca8QduFbELv6anFNvdXIgA6RxCoL1QLOzSVckYxICmoO9m9jPCim0YK86ToOpYXu39HH+1z7dDuuUSNiR5F9ePfM4hSJ3bqd9LMgrwxtrsHz1vJB7itKJiaverqkECjuV1zuiIDWbyHClwekD6zVNPWMUb0GwC1eEAOEnpoxOvBgjg== 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=MwkUdAZK2Sn+ef6/Ck2d2e4mu8wVrnC14FlkE2s0GU0=; b=fXnnFjcjhnZse6WDMlbD563xgh+Dv8XCKLDYtNK95eaJZ219SLmreADM7DmXvXO5/zT1YYYQ71clDFm7I5rUN37eoklYuNz+LG2vGMy53IPvn9tlx3ATi6bj+GjiFnhz4zZLwsv1Rac+DePN8BChlHMIGDJOVA44RhteTG8e/uypHZ+uOfOEOG3QEfu5vvqTlwpPjN85T0rXhIJllTVz88Omb7DQsxXDWNvIIYi9V+Glt15SvNwxnPyJeB169ptlAbtyakNj046wP6JXkkTr/fMnHFc4LWhhxkcJtFzMMZyrUXwaXEWw/4pCHiubLHs2YxWA17WAtR6NZK47H0sCCw== 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=MwkUdAZK2Sn+ef6/Ck2d2e4mu8wVrnC14FlkE2s0GU0=; b=Wup8KZRaCoTwu+TPixyW1KVp29zWqtVdc+rxzU2oon/ptI2WaAsSw8Smw7nFD600z10lN/cCuKC1q/v18UBI4ChQPznAWo5pEQucZIHOndQpHcn5vXK8f29FVjrrEaHo9PtimpNNPPQV91GSoouFq5EafNjgcVwqG2lOwzfRYDY= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:15:57 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:15:57 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 12/36] x86/sev: Add helper for validating pages in early enc attribute changes Date: Wed, 7 Jul 2021 13:14:42 -0500 Message-Id: <20210707181506.30489-13-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:55 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 80df8925-5c16-45ed-a0dc-08d94173445f X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3044; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Mcoc0iEAuz/24BH1W0hy8MbBaHhREAK5mDy04yvnUdipLJqQG8rf0SJ5eCuDrp8jSQlPcdlfVuuIIPFzrhTb1x23yIZFpqnh6EYsEZoKZg17BU/P0yYKhbJe2KpJbMYvIyUiH+Bj4ZSPUbSs3kgKPMh7ooPiYumXJB6QS/yCNPneBDfqaTJZZnmulHqGTytIGQiu7cRiG4yZ5nwkbj0hzrW0xRsD7CFUGWsgK3xx+HoTm6+ZWzDHDZ9NRdQZec2Wo0hzdP0do/zftW/eBtmSDMr6aw0epiyN8/q/hxHBdvDEOIu8VgiTYLjUGlDGFm9APz+/lmQUAkn5HiJ08N2o4Wid//xkPmrkE+1YaImogcA8qkGIrT7dme6QQ3VFc8faUhNi5t/ghD2t9hWOjm56f4XwJz96nGRgGo955sg3hWmJOFsjT3JVcTsfzHJ+3iJTcHHcSrUWtKuBGL94Rl5fuwe4zezvkraNgck5nzOyAjI1Ki+LZs5zOTeDtt50bgq9LOevsy4gtJnhWv4tabfvMmjUWXMeYxD47rCsJINhce+mPXPlQQ2RguN5wy1kTbFptAQDtjmsq+/ZtRW2vGHQrgoMRVdkFvBWJpzMv0c/QOmJICYrma5Cbqp1ta67qNZMoAe6c723hunssdLNU+yX+RI+jGCqOIbbuvi5+dJf0H9wJ66a2ml8FksvkihlZnv5 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(6666004)(2906002)(1076003)(4326008)(83380400001)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: vF7zTzdt8JW+teLdsxI6MQs26lDRb39V5S+Z1+r8p+O5VBYIWhSLyCaMBkv5EUHmdr5ztWEPhZDA+Wqpo5nf44WTP4E3Do0riZFBFEkVzYYaBJzp9sO1oD8vfagoqkYDbdABQHpa+kSNuBM5yCUmbkoM2fBBEHlv3rNJ+q3p+j5Jh1hSbUAPz4lLHxqV9xd7/nRWa+8F5S9L5QNhJ+XrkMcKmnyF2mT4O2+mv1p29HVuJ67XSJDbUPsB36rVL21+xRRF2QUGM9D58ao6Ydn9/iI3DiCJyH+L46DjW21bKbcZmD8t3yLoRJ7Wmog2V4sJQpNlve8yc98uS9lpUGeDuZU3jvVQOgUBbKFTmQoRjzmHDdAYWeQg1E1SVF21VFcFQf1LMNhOakNd8cndsSv73UCrz4CMO88qRLkx8xSxo6mayUDQfyovr3HzbOMSixDa9Xa4rDOWG6gyaCo/7WPGhcBN5m5XaYFbuIq5ZJV+WB6Mcs3nfpB6ZWd8DMTWrTdyTkfBp85lCrIK5BF10PR/GONT0znC3Ynuy5Fruf/GqqGCgC2JUxWG3cckyknH7WhllBXpO8NtIrZsfGRW3xmcUPZkmvAmB4wtizNpkOG3OLPWssKuCcW1i5ZtS2Lqbz6Ep25dmv96artPIH+tKFW9605S0WIyMyFki+/zJWlXwrzRc739TwWhCgYbTwusZYTGz2jJjmpgPpJQgmWWbGPyvwNy9xfDCON1m5HXPsxkRCcNOM2M0U0aukwY/Lci5tHhtNyjCiaw2paDx0rtl18Nk/BOeVvkBTl/TAavJ9Umpd1nT6wyBYk+zH6aHoOiFD1obr5MJ/OQX1E+bvGVMUUtmtg0ZqxO3BtqRERZkmGG9tMRX5re9+ZnqXKG/JO0ob/yntgwk2S3/DEpOtIWHd/F3QvjqnpjMMFvw/CXFqr9BOqfqtqyLrUPmhSvGPRyLqczXuuUIp/l81lTfTWYa5qGXxQ1Apu46crT5XLuquSqBwJl7zBVE8FjaJx1extAQHyMMxqTjBWCpiqGykXpD4mSWtH3FOBdNMXeVKfofWRamACCYOsOT2wdW9skvq1Pd2pBNs51oC6DhaksGNMxbPMPTL7i1au3Bh8KtSXUFHNg8e1+w1D57oA8HyKSr098PPcVwTOB5ltMBJyB1MMOfI4j7DHdoDBlHOgXiOc/Tg6wjAsfHOLTNT2Ua1NSnw0eqy68zLG4BPEHKSig3GBxqkeBJdnNO9a+DYznAq4XizkLQZzrHebk5aMDC9rIMdrTJ/N3y7O+x1VLTchpO37TT7J2XX6CyogRwoI8P8GFJoKRg0icnm35kJLB6PwHmqiYFAS+ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 80df8925-5c16-45ed-a0dc-08d94173445f X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:15:57.6016 (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: BxtPasek7KL6+YWEFuS6z5pOprWOIRgRPdBk3ytTfcdPZxZRZXq6gduXyEQjNwcdYMiVbM3d7oImj5YY0AXWgQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Wup8KZRa; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf23.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.223.55 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam02 X-Rspam-User: nil X-Rspamd-Queue-Id: 744C090000B7 X-Stat-Signature: rei5qj3u8qerp7tao5n4zusscdyoqwii X-HE-Tag: 1625687384-716050 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The early_set_memory_{encrypt,decrypt}() are used for changing the page from decrypted (shared) to encrypted (private) and vice versa. When SEV-SNP is active, the page state transition needs to go through additional steps. If the page is transitioned from shared to private, then perform the following after the encryption attribute is set in the page table: 1. Issue the page state change VMGEXIT to add the page as a private in the RMP table. 2. Validate the page after its successfully added in the RMP table. To maintain the security guarantees, if the page is transitioned from private to shared, then perform the following before clearing the encryption attribute from the page table. 1. Invalidate the page. 2. Issue the page state change VMGEXIT to make the page shared in the RMP table. The early_set_memory_{encrypt,decrypt} can be called before the GHCB is setup, use the SNP page state MSR protocol VMGEXIT defined in the GHCB specification to request the page state change in the RMP table. While at it, add a helper snp_prep_memory() that can be used outside the sev specific files to change the page state for a specified memory range. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev.h | 10 ++++ arch/x86/kernel/sev.c | 99 ++++++++++++++++++++++++++++++++++++++ arch/x86/mm/mem_encrypt.c | 51 ++++++++++++++++++-- 3 files changed, 156 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 242af1154e49..9a676fb0929d 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -104,6 +104,11 @@ static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) return rc; } +void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, + unsigned int npages); +void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, + unsigned int npages); +void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -111,6 +116,11 @@ static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { ret static inline void sev_es_nmi_complete(void) { } static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; } static inline int pvalidate(unsigned long vaddr, bool rmp_psize, bool validate) { return 0; } +static inline void __init +early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } +static inline void __init +early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } +static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op) { } #endif #endif diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index c7ff60b55bde..62034879fb3f 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -595,6 +595,105 @@ static u64 get_jump_table_addr(void) return ret; } +static void pvalidate_pages(unsigned long vaddr, unsigned int npages, bool validate) +{ + unsigned long vaddr_end; + int rc; + + vaddr = vaddr & PAGE_MASK; + vaddr_end = vaddr + (npages << PAGE_SHIFT); + + while (vaddr < vaddr_end) { + rc = pvalidate(vaddr, RMP_PG_SIZE_4K, validate); + if (WARN(rc, "Failed to validate address 0x%lx ret %d", vaddr, rc)) + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PVALIDATE); + + vaddr = vaddr + PAGE_SIZE; + } +} + +static void __init early_set_page_state(unsigned long paddr, unsigned int npages, int op) +{ + unsigned long paddr_end; + u64 val; + + paddr = paddr & PAGE_MASK; + paddr_end = paddr + (npages << PAGE_SHIFT); + + while (paddr < paddr_end) { + /* + * Use the MSR protocol because this function can be called before the GHCB + * is established. + */ + sev_es_wr_ghcb_msr(GHCB_MSR_PSC_REQ_GFN(paddr >> PAGE_SHIFT, op)); + VMGEXIT(); + + val = sev_es_rd_ghcb_msr(); + + if (WARN(GHCB_RESP_CODE(val) != GHCB_MSR_PSC_RESP, + "Wrong PSC response code: 0x%x\n", + (unsigned int)GHCB_RESP_CODE(val))) + goto e_term; + + if (WARN(GHCB_MSR_PSC_RESP_VAL(val), + "Failed to change page state to '%s' paddr 0x%lx error 0x%llx\n", + op == SNP_PAGE_STATE_PRIVATE ? "private" : "shared", + paddr, GHCB_MSR_PSC_RESP_VAL(val))) + goto e_term; + + paddr = paddr + PAGE_SIZE; + } + + return; + +e_term: + sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_PSC); +} + +void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, + unsigned int npages) +{ + if (!sev_feature_enabled(SEV_SNP)) + return; + + /* Ask hypervisor to add the memory pages in RMP table as a 'private'. */ + early_set_page_state(paddr, npages, SNP_PAGE_STATE_PRIVATE); + + /* Validate the memory pages after they've been added in the RMP table. */ + pvalidate_pages(vaddr, npages, 1); +} + +void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, + unsigned int npages) +{ + if (!sev_feature_enabled(SEV_SNP)) + return; + + /* + * Invalidate the memory pages before they are marked shared in the + * RMP table. + */ + pvalidate_pages(vaddr, npages, 0); + + /* Ask hypervisor to make the memory pages shared in the RMP table. */ + early_set_page_state(paddr, npages, SNP_PAGE_STATE_SHARED); +} + +void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op) +{ + unsigned long vaddr, npages; + + vaddr = (unsigned long)__va(paddr); + npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + + if (op == SNP_PAGE_STATE_PRIVATE) + early_snp_set_memory_private(vaddr, paddr, npages); + else if (op == SNP_PAGE_STATE_SHARED) + early_snp_set_memory_shared(vaddr, paddr, npages); + else + WARN(1, "invalid memory op %d\n", op); +} + int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { u16 startup_cs, startup_ip; diff --git a/arch/x86/mm/mem_encrypt.c b/arch/x86/mm/mem_encrypt.c index 63e7799a9a86..d434376568de 100644 --- a/arch/x86/mm/mem_encrypt.c +++ b/arch/x86/mm/mem_encrypt.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "mm_internal.h" @@ -48,6 +49,34 @@ EXPORT_SYMBOL_GPL(sev_enable_key); /* Buffer used for early in-place encryption by BSP, no locking needed */ static char sme_early_buffer[PAGE_SIZE] __initdata __aligned(PAGE_SIZE); +/* + * When SNP is active, change the page state from private to shared before + * copying the data from the source to destination and restore after the copy. + * This is required because the source address is mapped as decrypted by the + * caller of the routine. + */ +static inline void __init snp_memcpy(void *dst, void *src, size_t sz, + unsigned long paddr, bool decrypt) +{ + unsigned long npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + + if (!sev_feature_enabled(SEV_SNP) || !decrypt) { + memcpy(dst, src, sz); + return; + } + + /* + * With SNP, the paddr needs to be accessed decrypted, mark the page + * shared in the RMP table before copying it. + */ + early_snp_set_memory_shared((unsigned long)__va(paddr), paddr, npages); + + memcpy(dst, src, sz); + + /* Restore the page state after the memcpy. */ + early_snp_set_memory_private((unsigned long)__va(paddr), paddr, npages); +} + /* * This routine does not change the underlying encryption setting of the * page(s) that map this memory. It assumes that eventually the memory is @@ -96,8 +125,8 @@ static void __init __sme_early_enc_dec(resource_size_t paddr, * Use a temporary buffer, of cache-line multiple size, to * avoid data corruption as documented in the APM. */ - memcpy(sme_early_buffer, src, len); - memcpy(dst, sme_early_buffer, len); + snp_memcpy(sme_early_buffer, src, len, paddr, enc); + snp_memcpy(dst, sme_early_buffer, len, paddr, !enc); early_memunmap(dst, len); early_memunmap(src, len); @@ -272,14 +301,28 @@ static void __init __set_clr_pte_enc(pte_t *kpte, int level, bool enc) clflush_cache_range(__va(pa), size); /* Encrypt/decrypt the contents in-place */ - if (enc) + if (enc) { sme_early_encrypt(pa, size); - else + } else { sme_early_decrypt(pa, size); + /* + * ON SNP, the page state in the RMP table must happen + * before the page table updates. + */ + early_snp_set_memory_shared((unsigned long)__va(pa), pa, 1); + } + /* Change the page encryption mask. */ new_pte = pfn_pte(pfn, new_prot); set_pte_atomic(kpte, new_pte); + + /* + * If page is set encrypted in the page table, then update the RMP table to + * add this page as private. + */ + if (enc) + early_snp_set_memory_private((unsigned long)__va(pa), pa, 1); } static int __init early_set_memory_enc_dec(unsigned long vaddr, From patchwork Wed Jul 7 18:14: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: 12363461 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,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 90830C07E95 for ; Wed, 7 Jul 2021 18:31:59 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3E05961CBE for ; Wed, 7 Jul 2021 18:31:59 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3E05961CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2B8FD6B0073; Wed, 7 Jul 2021 14:31:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 268906B0074; Wed, 7 Jul 2021 14:31:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0957E6B0075; Wed, 7 Jul 2021 14:31:59 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0096.hostedemail.com [216.40.44.96]) by kanga.kvack.org (Postfix) with ESMTP id D76A56B0073 for ; Wed, 7 Jul 2021 14:31:58 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 3FDCA250D5 for ; Wed, 7 Jul 2021 18:31:58 +0000 (UTC) X-FDA: 78336635916.23.F963F6A Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2042.outbound.protection.outlook.com [40.107.94.42]) by imf29.hostedemail.com (Postfix) with ESMTP id 8FA189000163 for ; Wed, 7 Jul 2021 18:31:55 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Jiy0TjZ+TYvtmuj44nTHytWco0FcbE0XRvj445aKurJcHnP92YU4x5CCbXNxqbZg/S4PK3NWuMkYelK6tvvbIIqjtQw4yOZlxvV4M4g/+YJ07OzCbun5Q1Cn8NDRdP1PHgDkqIQTFgvAF4rLr5sCaQ+k/dHAti4rGQ+3/KalpIvtpJXuDmGQWw3oWwR/PimligL2wfwEC7A5d60xXVA4m2TTjnwFX4rmvfjcu+yLakitDvmpTh2Lc9np3Fy51KN7HpyWYOmK5Kr6TicFgC0n/ngJOBGN7cQDhgtT3F6WwmyZE0Jr10E5x4IOHP65FF2I9QUdX+qAFsZQ+C8E3DttBw== 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=9/JhxqSka73eZEhSsicofqkCL2gbRrNJ9DD4pCzki9Q=; b=k5FnIuX7rjWtZ7GCr1G5zM8CexVuJ0IIsryUEdlr2B86OTut71Lg0z/6s+1WhJX9qOofxRsGmVnu1zA4eKMoigqCQ1Q+lc3prazCH+1IOzFyN3pplQTrSxhcTv/e5Lye0hx61IJyqdUCiNB1P+1aYmTwP0CIv8Nij0vr+h1CLP8RuZP1ulJfoNQWmoKbRFfqJrY0HCgVjEjRtxehEutpGyP/9nP4Cwy/iZOirw83XWihbO/02WsRqmhlQ+EjyBYVMeFA4XnfvMTu5StjaiB4XetTI9Zd6m+u0ClpaVcGEvUUBIGYw7pX4qsxJ6Iw8rKQK7VJ3asbxeRmz0isCQ9V0w== 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=9/JhxqSka73eZEhSsicofqkCL2gbRrNJ9DD4pCzki9Q=; b=zNMq+2H/zFKKdft81z6cPzhQULA1co9DrKjSIeILuwxHi6/bkytxSxuONGULf1hChSiKhFeydR1i5h4Bd8VVkSkrYrhePPdVVTxHuGtrOJZqwNSGeWM2zn7Pk9zGMY9v6r/IvVKLH2amVXJxPSIUt9s8t4kDT8/+eMnh1JdAtkU= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:00 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:00 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 13/36] x86/kernel: Make the bss.decrypted section shared in RMP table Date: Wed, 7 Jul 2021 13:14:43 -0500 Message-Id: <20210707181506.30489-14-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:15:57 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: bbb67c54-880d-42e9-1bd3-08d941734600 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2958; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: tJpKdITOIhzmD1u++4SQBeGV4QtdNH0EKlii/LpTNn6nX1UHTxnvcW8qhhIxRdcRIwzNfEGpTnp/er259LqJr2PimN5iWq0BSPwlCkit2uLrTSfn7po63prsJ/xlhKBi/TMgYyKkfXcZIUk250GFyUJFULNu6d9eBYqjz2tZbnuIuYbYP8/0ctRm2DofsTZWZWuIJPGf2h5fwOTbh5t2Jv3plIQtumCftrud0gubGLk1dercVolw50OOvwfTiM3YDRhS2bEaaLOj4RDifhtSG1XLg1D6Ncp714YeMM59WtRl1Hrss68vb9/rXUNAbb+uP0oDs9NAzrfEno499GuUoOp+qhyv9LerLA0dlOzIY8p0lbfKw+qunO20mRtlvkdh7oH4HZN3LN27FMMYQ1xpwcfyw6O+do9/z7c3YESX/PEMSyvzHxiL2M2uBNNXxMjMtSPAqRQbffRxZ76PhURuJjCC+gQUEav+U/d8yGfjDch6Ni9yX/KDj+iuw5P0YAKAoGN58S80dd7LJiuIsFxvRM3kD3ndsGB7sDjRIog7P0Ja0k8eYF6Sy3IlWm2hGORvmg4EJxa6SRIjgytkV7f917QBh4PhtXM8saHJ0n+U+Zs7e0NIHyyljLKjTlh95woFqK3gVPoOGlvPqGdvLOWHQZWeGmsz5HiBfsVjVerBGPP23UXuhuUYNi2TrHPLXZjb9HM/stxt7lErm8wXpII+gg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: ajMtQE9AbVQQ+clFVgmOfvc0Z1YHdo3hGpmQ27G6x0nsV8cqjt5jQ1nqv3JiRS4iiqSJb1V6k1wbFTMuwVaGi99+inT9PjO2Kvsajmn9RK7JqOQR2gVN5rl0HtNRPFjLRbySLPZCLH6imKwZmKQRXmcmdZSqt1cmakrYTWsPJEagQN9Otnl4Xjduc/ElqmdHYm7BJoLEfkQLWuWEOofLirmZ7b+xN1fS7x9oHa6YsgzXOB7oMjXV+eLfQkVb1YB0BNuoPu2sBy8k6LFdsKu4b2y/+B7x7OhT0K2i0XI/o2rzpYveUSFVnQX1rhKTF4PueAqVFKEceqak5F2MTCSwMWqkS275J/KObg5QeX7tMGm1Pcu1PUq1jl4wruNUvRVKklcuBwfsTsd1bJtJ1PkCO4aJvsqnI1OHXR7LMSfh5C6ory1ht6OTwLg7BTQm9QhD4HXYznKkU9ZpRSlXoUGwRBFA13A6pXJH2RcX5ATpkJIWaYizUyPyhJI4pX8LfD9ahoysgJeh+vm5y98rChAsw50sE7vT3/NajJ1FXDh+RAth9P/8kr4Sl3M74XCiw/tjFhbNWvUhf0kz2dyAHWaLKbSlKvapuVlL7ndGFKbmeyTNeihPhVyIDToZdnBK+5hovJ1+6EGFeDcR5ZR9h23CJ3Irn6wI8FGaf8kzAtQb5bI0gnCTEjo8C690jHxOVpPVzQeIlQdysDrltLml4tfNf5iWXU3sm+AT8GhugUlMJ4MrKuUs1sqnhkM+wbCIfQOWVIJ65LmF9wgEaCDMsDjvDrpH+XUGfzgpq7g16FeK/y0JfGI0n9Z4AYNF8PfBZSfpjiGXBP8zvgXdUHOF671GerFOwxpwkFpV3vp7pV8YxXLQUc54SWFA+8lAU2Ie43fuxksQV6Aqzwzk/ks0mPwoyhQV3EbNJFLoRL/hpCipTGWwNRdICjgiH13QtFWrbU/e0gDonnInemk5jkKnUV6DFYYpI3HgorIi4h4W9B/Fz43BlPuNjCItOKF7Qca4nek4eHUyZ1BCffQZEIl+IfjEnSYdXzQyHpqO1afItz3h+Cm2IbIdd11FijIfwqm3A7hhDaBdrU6z3m+J4HY8wRCGcIW6WJaaGys5IMm1eFHFZPqxlVK67/xKjP3kkh3Ol9azeF9Pijo0osPBGie8/AHB8ZovX9Fwh7otWT/b83359Dt43YLCyh4LOtXJ9obKjFwuYiWafIBB54ucrYc3he/rzmB9H12yz2jX2xnJaP+CWsZ60K8nqkQkwSwUh22gGEsKWyroPBrVHR+6ZG4KnStJ46isB6kQltCxnBcJQZC0UU9A44fO5Gl1WDGJmrj8qOeA X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: bbb67c54-880d-42e9-1bd3-08d941734600 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:00.3311 (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: Tcf4KLvWm2y2EvYROL4e4u7IE2AQiKcVqrqmmyMaj2GEVwHVO3Y61vJYOxiTXbPDlSr3CaMlQOsWGcJPaec1Ew== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="zNMq+2H/"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf29.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.94.42 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 8FA189000163 X-Stat-Signature: hyqhxz9doaedmrgcxh8tnetx4it6sgkx X-HE-Tag: 1625682715-643957 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The encryption attribute for the bss.decrypted region is cleared in the initial page table build. This is because the section contains the data that need to be shared between the guest and the hypervisor. When SEV-SNP is active, just clearing the encryption attribute in the page table is not enough. The page state need to be updated in the RMP table. Signed-off-by: Brijesh Singh Signed-off-by: Borislav Petkov --- arch/x86/kernel/head64.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index de01903c3735..f4c3e632345a 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -288,7 +288,14 @@ unsigned long __head __startup_64(unsigned long physaddr, if (mem_encrypt_active()) { vaddr = (unsigned long)__start_bss_decrypted; vaddr_end = (unsigned long)__end_bss_decrypted; + for (; vaddr < vaddr_end; vaddr += PMD_SIZE) { + /* + * When SEV-SNP is active then transition the page to shared in the RMP + * table so that it is consistent with the page table attribute change. + */ + early_snp_set_memory_shared(__pa(vaddr), __pa(vaddr), PTRS_PER_PMD); + i = pmd_index(vaddr); pmd[i] -= sme_get_me_mask(); } From patchwork Wed Jul 7 18:14: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: 12363451 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,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 BEC52C07E95 for ; Wed, 7 Jul 2021 18:30:18 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5592561CC9 for ; Wed, 7 Jul 2021 18:30:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5592561CC9 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 41B566B005D; Wed, 7 Jul 2021 14:30:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CB2F6B006C; Wed, 7 Jul 2021 14:30:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D03A6B0070; Wed, 7 Jul 2021 14:30:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id E6ADA6B005D for ; Wed, 7 Jul 2021 14:30:17 -0400 (EDT) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 4841818259C42 for ; Wed, 7 Jul 2021 18:30:17 +0000 (UTC) X-FDA: 78336631674.24.C0B1A6C Received: from NAM11-CO1-obe.outbound.protection.outlook.com (mail-co1nam11on2062.outbound.protection.outlook.com [40.107.220.62]) by imf19.hostedemail.com (Postfix) with ESMTP id 9426AB0001A2 for ; Wed, 7 Jul 2021 18:30:16 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PMpYYW/7hIy1JvaQr/V18OwzkizWzsGSXtbMoj6JUS3zMSQtieOm+yWeOUaP8an8AQTR/fUeH6JAlxx+MUxiZIhBmiZKLiYYnTVwvPLIysIOwzrh1RxQteifxyAHBNtPdikon95E5cvrvLXKdeq7KlcPZ7IhQM3JTSeQJZKjgkHx9vQHgg5oZnhR78mIZl+Gm6gNKn0Hp9i0pnOhFFKDv+LbgnK3nc8ayATYWrs6T3aEfE3hv2b4Pg0+/40TsQBZ6v3t00huGuBh+Zy0IWId5m0rSUbGBlpCq+UzrPzBKL8hSe1/pZH1zcYYPVRooFhxR/odDnIMx/Xx0YrfoZV5wg== 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=+GAj0n/mh4HxIHmd8pu1nVhyIj/Ean9oCwoFOmBwMJ4=; b=QZDNAd8tCQjcm/zLruYyjQa24/Bo1wwKs3o2Dy1Cwwa9DyyKiGoIYGYnQMfttvQMZIlycv++4fVqGSo+u8B3TpLX283CK39HC4zYGxaPHcpLSj2lZprWyb8staYcpsbqyx5ox2a66aD1t2jz+nwvkKYUjmaIJjUff+iG1Ak/X890MWnUezF07v7S8Lbs+6Xd88ojmJ/G4j3UzUucK5dTMLAWZ1Jax6Y/YNbmWKuqGKyEvyXLvGY1OH//bfB2tr3/JWQrG9mtKxU8GdNf5xF0RoxdKSYrBn5IUYBZfqcf3UL2UmukAUmg7+33DJBk7XuoToSpQJDY7mE71UvsKKcpyQ== 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=+GAj0n/mh4HxIHmd8pu1nVhyIj/Ean9oCwoFOmBwMJ4=; b=5RHnVb78UBKrSMlIKp60JbFxI5m0avGO2ETkiNk6cocqafWzILc9vVMufIKvl2Twzgrc2eBG10HSVmITSCAY1T2B/3JNUCOoDUtPJwSymHXOst7DL5ILTTn7rD80IfuXnqbQU9PNr0UNTA4lAlNL/+v/DvLhgKuhWzpSuFV6pAQ= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:03 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:02 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 14/36] x86/kernel: Validate rom memory before accessing when SEV-SNP is active Date: Wed, 7 Jul 2021 13:14:44 -0500 Message-Id: <20210707181506.30489-15-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:00 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: d6c17605-d6ad-47b6-7608-08d941734783 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: a7vv3yFO3nB310cMkrqtD7Tvvoc/7evptrBTo1/FbTeYU+cjI+ctkUAVbPKH6oa2KBiY8UUJGHLNCs6KVcwDadxR7ARF4liJf9PPucoQ3x1movXfYWom6A1y3B+Ed0fst/BYRvPBzuaJssYw8a5z7QWpcODyNd/y63HoUm/xUYMuZWxPC2b5W84AEsUAuBDzzdHdXe8zMIUQStvwIHMAFx/wXLaUj5jSGW8G8mJQagwV72YlozWM79xf0fBe+2d9Jlf9GHSdDey5TSsA9Tz4Lh/+lyOS8zFj0wFprT8SezXMBLEe/WNtR7aY4iPvd0LpRqxsyCV5x3Wm3nlRHOBXpU58oNp1KrG2Sz3O19AMoL1wXAG9ZUA2XeJpE7uLGA2ZW8ctIzy0wX4uwMahd2e4g8NdAWmrVOuaE2OTHYgs34CLSvInw6ykSqnoimuGfeggci+4u/+ChYtSOwfx8YHzyIo5M/vbo1c6i5PxXO9X0/zqibdDG9I2ZHIDdq2euZFpTATjLBgzivsbbRMe+LwwM7+820YfEuhtNbLfrTSpiKBRVVADJ4gly/UVQjGUYs6TGcBXLUQ/hhvcOFqy96gSXMlhpudJ4D938rWckb0HTeGT0QNJi7KHy2SXsS7vbK8GBEbbCwf2h8LdORvnXFAiKjj0VtYxd1fLdjq1WUx+POrCBf/0tPBxLKZagwutpZcVOMDP+13tSKGPCgPLA7So5w== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(15650500001)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: R/yRKhMEE+J12SNMMPueZRiIJbxEv/x0HAQl1rVMoKtd8zfyC6eCH7W94qnbRDs7UsBLQ8W5eJCxIxSaA/+ZSjGzfJ8dEsJdP9qVC1byKK29xcGlFwQ9rpp3Zd4Lrnph131b2+GeQDLU9kvB3OvEUr8inIpKB9Q8pNqRQnZSVUNwJ/LPIsJAGypzXGUpFwajfdaDkVvz/sD8IVhmtD/BjY4wp+1PG/n+KdP/IQ7xXqtaP6uDnfEZ4Jth8KupkBS2H9hj3seh8BkAOmYDdyHcJwiUsV4qPq8HtaUko6USv7lIizds4HzGBoZ2pf+ZXgqdrt1/uYSKtOzU6Pv2Y/DGgvJ5Nl4mWsPdGUJO4kVOT+DLg6l9lKESKEYlYe6k7xnkxjp1u/wSfIY5c+N5tglnyrLkJzUlPuLxYLm+viu1PupL3JvFtfgGXyhYmZmx9/AKnP/3m9B9CXAjo3oL/Niqu0s8GgAbqAMgx+Wu42KIr0WTkK7s7SLFhHU+cmXrMHchgzWi9aX4dRuq205iRc03ZFlHsCAxVIwWeeaV8r6ItbO3/9a17T/IzSwvKVlEyLaxIKa17I2Hvvejdn1/6kV9tS8JOholRSMEwSzqyiNL/ZtD0xMXQ9YUpxwgAzwWZQjT2K0U6oKJ+F2M2YPachqzazjc7vJEbZ7qV7BkMIdSpY9JfxSDeAafHoo8nSyb/ttOWFVww0iXXDVkFGnfumossHb1HeS3wyYepu9soR19iGhUtT31VVdjJsAuPfbLaHeZrMMBGeVbk/ddkyxiTwzYEceHIoVS+hmsHutJVRTqDalygD5N3aNmXxk9AJoL5R/16Umfk2+nyhuRjTNlTbU3poscYacFJ643UN1cPaHfXYbueZRfswe09rLgIJu8jyG3ElnN+OJcHEwHX1O+W6VNamkEICuClH06fYLTf6uj15UkDDv6C9+uPTZdrNqYrAWFuwOQExL7F6lPgJlTFdqcVeQ+nQhMufJDPqvKoVUl4KZDOBAHD8ApODjQYfaPodsf3oBQQ7ggosfE2gh3x2xDkt6R10E7CGiUYAAY8hlOdNvzGRUtqZ6uOtsbuxLGG4fglqkQCtq5WWPl7UR3y4csuDpHiOVcBSJne56ucen4B/Kgjv3JP716GOSF3oWfFvSD6H8V/kGrdZ6vIwvRAs0IdzmJKs81z48LIgwvcXx1CaSaEdleGFb8H8hAWGIzlCKSnz0xpOQjXaxtKD5/nKQsBwpnLA4CKG1JUesVcyXtDm+pF6eqYCfzENMmO8iBJnK0thoFjqYVIPvFWIO0QMDlC9WsdHraw0wuu5MTj7ayOLEA+f8B1wAufWYisZbPw6B6 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: d6c17605-d6ad-47b6-7608-08d941734783 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:02.8167 (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: bTRMRBhlR2P3C9VixrNNYfNEPVCwKtKvR/E+Z1MtOAntDLHJGA34Kfqie/MCJSipK37ub0vfPM877CYW/JsgrQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5RHnVb78; spf=pass (imf19.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.220.62 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9426AB0001A2 X-Rspam-User: nil X-Stat-Signature: c7r4rnouw7mhg1qpzj9zix71xbotfygu X-HE-Tag: 1625682616-977675 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The probe_roms() access the memory range (0xc0000 - 0x10000) to probe various ROMs. The memory range is not part of the E820 system RAM range. The memory range is mapped as private (i.e encrypted) in page table. When SEV-SNP is active, all the private memory must be validated before the access. The ROM range was not part of E820 map, so the guest BIOS did not validate it. An access to invalidated memory will cause a VC exception. The guest does not support handling not-validated VC exception yet, so validate the ROM memory regions before it is accessed. Signed-off-by: Brijesh Singh --- arch/x86/kernel/probe_roms.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/probe_roms.c b/arch/x86/kernel/probe_roms.c index 9e1def3744f2..9c09df86d167 100644 --- a/arch/x86/kernel/probe_roms.c +++ b/arch/x86/kernel/probe_roms.c @@ -21,6 +21,7 @@ #include #include #include +#include static struct resource system_rom_resource = { .name = "System ROM", @@ -197,11 +198,21 @@ static int __init romchecksum(const unsigned char *rom, unsigned long length) void __init probe_roms(void) { - const unsigned char *rom; unsigned long start, length, upper; + const unsigned char *rom; unsigned char c; int i; + /* + * The ROM memory is not part of the E820 system RAM and is not pre-validated + * by the BIOS. The kernel page table maps the ROM region as encrypted memory, + * the SEV-SNP requires the encrypted memory must be validated before the + * access. Validate the ROM before accessing it. + */ + snp_prep_memory(video_rom_resource.start, + ((system_rom_resource.end + 1) - video_rom_resource.start), + SNP_PAGE_STATE_PRIVATE); + /* video rom */ upper = adapter_rom_resources[0].start; for (start = video_rom_resource.start; start < upper; start += 2048) { From patchwork Wed Jul 7 18:14: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: 12363997 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,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 543F0C07E9B for ; Wed, 7 Jul 2021 19:47:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8C37D61CBE for ; Wed, 7 Jul 2021 19:47:31 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8C37D61CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 6B65D6B0011; Wed, 7 Jul 2021 15:47:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 68E0C6B005D; Wed, 7 Jul 2021 15:47:31 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4E1E26B006C; Wed, 7 Jul 2021 15:47:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0189.hostedemail.com [216.40.44.189]) by kanga.kvack.org (Postfix) with ESMTP id 1EB576B0011 for ; Wed, 7 Jul 2021 15:47:31 -0400 (EDT) Received: from smtpin32.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 763CE26805 for ; Wed, 7 Jul 2021 19:47:30 +0000 (UTC) X-FDA: 78336826260.32.703BD90 Received: from NAM11-DM6-obe.outbound.protection.outlook.com (mail-dm6nam11on2048.outbound.protection.outlook.com [40.107.223.48]) by imf25.hostedemail.com (Postfix) with ESMTP id 378AEB000296 for ; Wed, 7 Jul 2021 19:47:29 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dA0yElsDkhD6LI1EkXFuw+ZGYefpnayJv0WFEGvQsIsnxZVgTga7xFh/xLoJN3B+BDGhtUiEPAqxiZk3aNs4R+y37z/mez9VMsKk/a4GvKrtnhLXXRHm34DBhE5U7QikUL9LNsLuYAqU9Tnnam5i7wz95m25i7dRXOsqIC6JN0DIWNQVgJIwA+77BtHRhhHshzyrDthX2WM3jbUfd4O+Vj1zeoFR+FKA5CAs5dv8jzwilrjTUHT2hBvwmSWPEjghz+7+UAl4PPuzfEZ78i7qHahw7eMVanN/vy3XyylwYuJFqhkqymy82zYFMIfngw9r9vfv1M2amtZ10gcJ0I02yQ== 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=2nFihNeDzQrP2uPQ4cYtOE6S4l7eQRdRxyQTHNBaIxI=; b=F6vHwqN+wE6u1vFyygHjn8BPsb/6Oc9ywESmP7LwUj1N82TpxisqBmks5L5iMbsshCuRd+NMex9jbZOI2IzPPCfdHD7i5bXEc/VfMNvVreYK2xwmDLuXkU7MWF8B4/itY9Ilgy4lvOBPhuTcvTItuPIoAfNl3kZyMOhu1rEVNlYANLFuSjmCfm8Dk/PXjeFdu4OAdwpitJXa3oj+a2TYcfxk4nw5Ff1NK6xX4VF3M0kbtKk/74dEWRO75XDnUoct+bq1dApIho9e75QWsqJcfLZLlSUUWk5SQnFnlclVXvkKkP3vjb5rzoMt5a0Hxru6bRoa3sR1B/FRxaSicl0adA== 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=2nFihNeDzQrP2uPQ4cYtOE6S4l7eQRdRxyQTHNBaIxI=; b=jo8VT+BNMVuudRqtvaurdfk9/58ri3Rdyar7WMDPJfhsLYn6jkI+fyN45fk8+L+QLvMrSz5YmlA1xwcXq+FbkpJdgi0SmURGWpT+0VGYdadCOoL8HyKSSNRRGRjCF/yjy2HFOqkSKxd2/yKN8hP+k1rfN/3iM46OciKt9qJM1I8= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:16:05 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:05 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 15/36] x86/mm: Add support to validate memory when changing C-bit Date: Wed, 7 Jul 2021 13:14:45 -0500 Message-Id: <20210707181506.30489-16-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:03 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c5992d06-c860-4c09-4607-08d9417348fe X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: r8vYXOolZ+6Va0SwdFA+ZuyQ7Z0+XwEVqpO5htOZQg8pYP/GUDLrwr/HNAgaABW3Lt4q1MR4MV/Ev9HXC7ynIEd98GbyibaXu62imhBiR5+sfnoJ7tZbSR3FUgZe6SMW9OtCjyYPGSu3IYqyljNPI17yGN8dp2xLBFeyIs1CKadT1aZmvewRJjV+tTT+lGq6LfJAgEYjYE1MbxEC/F70pJUcAmZO3YgabW/q/i1fIVyZqRwqNdiLPkZBl6pJ8uNPs3cnN01PuONIZ1GMbXns5InCiQLGAj8pjU2CfLAeDiqqJt+NBPL0px7N7jP08lphadPJEwNRvB4fASDbMuKfSRLRMmnPenAgkl5SE92LeoYWzSm7Kk6YvC1b+EFoyMWevlNFzDBnOuEd319fM/2yO9H0ge6YdRcZDTPpxHUvocOIRBROKNplxFvfjnFHPKQZun+nVf/fjjo9E4bJKlRQGSRLGhSXM8iGaeC/zrGzX5dKt+iYnZ+cGjSqQf/kRqyiGYZSoc8oK13imOoxLdTfOW+Ddf7PucH9xWztrs9nneQDt87oyHN6115GBQ203+JEjnlNRUR61Vv4RO8IQ5lkTZnrsUxRg6AeJXAWSotMGElgv40l31FPhtsziccaLV3ETxDdAsTgWxij19wPZx4g56/Ot2sBBB5HPtNtYc/UE6lBOaFxj0ty+rijpGsw5xhbdNhDZFThs4+XDmiH15fQgA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(6666004)(2906002)(1076003)(4326008)(83380400001)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(15650500001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: qe6EFsM5PJ5l35naOYGMrZ398Y94yvh5QIK7F7gflgwX87So0Bvi1Oica7ijaZ6Qo8Av5CS/u+yspqbE/FUObMJJuYl/JPFU4YaXw/NwgVKPuyjmSUEigUDWVrrGPxcJaudl9qRh3PeDTQNe5zXeppuMHaYLY/GdNgAbzidSynt7Zh6d2wTELBCYhFBeI0bRRJkR2GylJLVL4WHh479ZQ/IYCXKPpFh+xrHcowsNWFkD/haPjmr3FUZwqLklNa+uPiNwY8ulqDWAX6gvpsVaU/GMayjuNkoMubp0r7Nabsx4lwxi8PyryGx3voMMsURAp9t8gDMJpNCb2Z/4GaDQfxa0ucgh+NRuv/u39Iy5KKMWTB6xS8TR2OkPuAkAekjRJsYOfUO0/A6m7Hu3MffuijaB/UTE96bqIO6rZBen/ft201mQCd2Oy25DBwslFK+lS0fNcnZjUbiAsYSHvE4vS3hpHcqYRp/Ei8YBk3x/5j9I7JC5rWSkC/S6GAqSTZI1Lw92CofWKXOWWOXwUHqy/kS6xHkYvvwtMmrJ76haaaTjHX8bcGEPronoCVt92VX2lY0bHS0GQCehPBkv2SpHBc90yYenjM4mYj9o0pxcLKF+DWJ5IcSgWWb3Nc3w8wwQYINWXyIiYSrS/lhRhUD4Zg0RGMk3U8HReJPN5SGU6JyxSLF6URtRf/4hSCk2xXSW2BCIu6bSUl1CmKgn1POielYX1DfoOMQ2GxulLEX/6taiMcpTATGA4t9tQtzkOn3NOblw0klxaoV7+PGipLnLYQlXgrTeDtw/mJ/r5pIyNSJ3kkiOZEwOwOMl9BhxrnoUe5lrb09zMmn+CplXPTf1ixJCO+07fAQbW84YvkKLMbHw8URTO8wti1xnFMP5DKMWwfu5Q7mAq1t5W8qUf7YZDAV07NRDxFVGXXLFVmiC5n5T36NZ+jlMIqFkoXS4oLEaSyMevujSwKOG9V8rgPohs/W/j/i4iNDZ3dEO4ufG6I99zGlwSkpC+tmzTvftSLdwGS+YuJGhlclg8v2ighPSplsQKDnz/BGU9V6as1rmWEB004A/+KPnt8Xizb+WbYadrJyEu2BU5377Ol7FufrCu/k79CZwQi6QwvSWsyeyQaYoZG8atoui+n/UiTsIHRXKNVpN99fK/Nc7slS/fVY1HzypvvAnGyKcPCY5luH+o0REKerGsvjXft5WF1LO33NtU48didN3b1KTK5ZCn95PovGJuGourqnI4/sktyiEOQajegWuY9Ny9mGvWQaeZpewlOEnjeaOrzJH+A3BC/BGJgjSClN96obxCUOaXe/1mgWUYQky4dApnvhcoSvaNJSy X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: c5992d06-c860-4c09-4607-08d9417348fe X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:05.4042 (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: A2bxoUF212Q4yuGvui9aSz2w4sW/Oj8rvjdZR1dkc2ny7pI1+RxoG0li7ZJHsnp6LpktetYSnPLKUj2sVKi48w== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=jo8VT+BN; spf=pass (imf25.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.223.48 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: 8eeprgqo4f8gtynt3z43ed3skdsqgns8 X-Rspamd-Queue-Id: 378AEB000296 X-Rspam-User: nil X-HE-Tag: 1625687249-296877 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The set_memory_{encrypt,decrypt}() are used for changing the pages from decrypted (shared) to encrypted (private) and vice versa. When SEV-SNP is active, the page state transition needs to go through additional steps. If the page is transitioned from shared to private, then perform the following after the encryption attribute is set in the page table: 1. Issue the page state change VMGEXIT to add the memory region in the RMP table. 2. Validate the memory region after the RMP entry is added. To maintain the security guarantees, if the page is transitioned from private to shared, then perform the following before encryption attribute is removed from the page table: 1. Invalidate the page. 2. Issue the page state change VMGEXIT to remove the page from RMP table. To change the page state in the RMP table, use the Page State Change VMGEXIT defined in the GHCB specification. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-common.h | 24 +++++ arch/x86/include/asm/sev.h | 4 + arch/x86/include/uapi/asm/svm.h | 2 + arch/x86/kernel/sev.c | 160 ++++++++++++++++++++++++++++++ arch/x86/mm/pat/set_memory.c | 15 +++ 5 files changed, 205 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index b19d8d301f5d..2277c8085b13 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -60,6 +60,8 @@ #define GHCB_MSR_PSC_REQ 0x014 #define SNP_PAGE_STATE_PRIVATE 1 #define SNP_PAGE_STATE_SHARED 2 +#define SNP_PAGE_STATE_PSMASH 3 +#define SNP_PAGE_STATE_UNSMASH 4 #define GHCB_MSR_PSC_GFN_POS 12 #define GHCB_MSR_PSC_GFN_MASK GENMASK_ULL(39, 0) #define GHCB_MSR_PSC_OP_POS 52 @@ -84,6 +86,28 @@ #define GHCB_HV_FT_SNP BIT_ULL(0) +/* SNP Page State Change NAE event */ +#define VMGEXIT_PSC_MAX_ENTRY 253 + +struct __packed psc_hdr { + u16 cur_entry; + u16 end_entry; + u32 reserved; +}; + +struct __packed psc_entry { + u64 cur_page : 12, + gfn : 40, + operation : 4, + pagesize : 1, + reserved : 7; +}; + +struct __packed snp_psc_desc { + struct psc_hdr hdr; + struct psc_entry entries[VMGEXIT_PSC_MAX_ENTRY]; +}; + #define GHCB_MSR_TERM_REQ 0x100 #define GHCB_MSR_TERM_REASON_SET_POS 12 #define GHCB_MSR_TERM_REASON_SET_MASK 0xf diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 9a676fb0929d..2385651c810e 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -109,6 +109,8 @@ void __init early_snp_set_memory_private(unsigned long vaddr, unsigned long padd void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages); void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op); +void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); +void snp_set_memory_private(unsigned long vaddr, unsigned int npages); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -121,6 +123,8 @@ early_snp_set_memory_private(unsigned long vaddr, unsigned long paddr, unsigned static inline void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned int npages) { } static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op) { } +static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } +static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } #endif #endif diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 7fbc311e2de1..f7f65febff70 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -108,6 +108,7 @@ #define SVM_VMGEXIT_AP_JUMP_TABLE 0x80000005 #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1 +#define SVM_VMGEXIT_PSC 0x80000010 #define SVM_VMGEXIT_HYPERVISOR_FEATURES 0x8000fffd #define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff @@ -216,6 +217,7 @@ { SVM_VMGEXIT_NMI_COMPLETE, "vmgexit_nmi_complete" }, \ { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ + { SVM_VMGEXIT_PSC, "vmgexit_page_state_change" }, \ { SVM_VMGEXIT_HYPERVISOR_FEATURES, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 62034879fb3f..5fef7fc46282 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -694,6 +694,166 @@ void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op) WARN(1, "invalid memory op %d\n", op); } +static int vmgexit_psc(struct snp_psc_desc *desc) +{ + int cur_entry, end_entry, ret; + struct snp_psc_desc *data; + struct ghcb_state state; + struct ghcb *ghcb; + struct psc_hdr *hdr; + unsigned long flags; + + local_irq_save(flags); + + ghcb = __sev_get_ghcb(&state); + if (unlikely(!ghcb)) + panic("SEV-SNP: Failed to get GHCB\n"); + + /* Copy the input desc into GHCB shared buffer */ + data = (struct snp_psc_desc *)ghcb->shared_buffer; + memcpy(ghcb->shared_buffer, desc, sizeof(*desc)); + + hdr = &data->hdr; + cur_entry = hdr->cur_entry; + end_entry = hdr->end_entry; + + /* + * As per the GHCB specification, the hypervisor can resume the guest + * before processing all the entries. Checks whether all the entries + * are processed. If not, then keep retrying. + * + * The stragtegy here is to wait for the hypervisor to change the page + * state in the RMP table before guest access the memory pages. If the + * page state was not successful, then later memory access will result + * in the crash. + */ + while (hdr->cur_entry <= hdr->end_entry) { + ghcb_set_sw_scratch(ghcb, (u64)__pa(data)); + + ret = sev_es_ghcb_hv_call(ghcb, NULL, SVM_VMGEXIT_PSC, 0, 0); + + /* + * Page State Change VMGEXIT can pass error code through + * exit_info_2. + */ + if (WARN(ret || ghcb->save.sw_exit_info_2, + "SEV-SNP: page state change failed ret=%d exit_info_2=%llx\n", + ret, ghcb->save.sw_exit_info_2)) + return 1; + + /* + * Lets do some sanity check that entry processing is not going + * backward. This will happen only if hypervisor is tricking us. + */ + if (WARN((hdr->end_entry > end_entry) || (cur_entry > hdr->cur_entry), + "SEV-SNP: page state change processing going backward, end_entry " + "(expected %d got %d) cur_entry (expected %d got %d)\n", + end_entry, hdr->end_entry, cur_entry, hdr->cur_entry)) + return 1; + + /* Lets verify that reserved bit is not set in the header*/ + if (WARN(hdr->reserved, "Reserved bit is set in the PSC header\n")) + return 1; + } + + __sev_put_ghcb(&state); + local_irq_restore(flags); + + return 0; +} + +static void __set_page_state(struct snp_psc_desc *data, unsigned long vaddr, + unsigned long vaddr_end, int op) +{ + struct psc_hdr *hdr; + struct psc_entry *e; + unsigned long pfn; + int i; + + hdr = &data->hdr; + e = data->entries; + + memset(data, 0, sizeof(*data)); + i = 0; + + while (vaddr < vaddr_end) { + if (is_vmalloc_addr((void *)vaddr)) + pfn = vmalloc_to_pfn((void *)vaddr); + else + pfn = __pa(vaddr) >> PAGE_SHIFT; + + e->gfn = pfn; + e->operation = op; + hdr->end_entry = i; + + /* + * The GHCB specification provides the flexibility to + * use either 4K or 2MB page size in the RMP table. + * The current SNP support does not keep track of the + * page size used in the RMP table. To avoid the + * overlap request, use the 4K page size in the RMP + * table. + */ + e->pagesize = RMP_PG_SIZE_4K; + + vaddr = vaddr + PAGE_SIZE; + e++; + i++; + } + + /* Terminate the guest on page state change failure. */ + if (vmgexit_psc(data)) + sev_es_terminate(1, GHCB_TERM_PSC); +} + +static void set_page_state(unsigned long vaddr, unsigned int npages, int op) +{ + unsigned long vaddr_end, next_vaddr; + struct snp_psc_desc *desc; + + vaddr = vaddr & PAGE_MASK; + vaddr_end = vaddr + (npages << PAGE_SHIFT); + + desc = kmalloc(sizeof(*desc), GFP_KERNEL_ACCOUNT); + if (!desc) + panic("failed to allocate memory"); + + while (vaddr < vaddr_end) { + /* + * Calculate the last vaddr that can be fit in one + * struct snp_psc_desc. + */ + next_vaddr = min_t(unsigned long, vaddr_end, + (VMGEXIT_PSC_MAX_ENTRY * PAGE_SIZE) + vaddr); + + __set_page_state(desc, vaddr, next_vaddr, op); + + vaddr = next_vaddr; + } + + kfree(desc); +} + +void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) +{ + if (!sev_feature_enabled(SEV_SNP)) + return; + + pvalidate_pages(vaddr, npages, 0); + + set_page_state(vaddr, npages, SNP_PAGE_STATE_SHARED); +} + +void snp_set_memory_private(unsigned long vaddr, unsigned int npages) +{ + if (!sev_feature_enabled(SEV_SNP)) + return; + + set_page_state(vaddr, npages, SNP_PAGE_STATE_PRIVATE); + + pvalidate_pages(vaddr, npages, 1); +} + int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { u16 startup_cs, startup_ip; diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 156cd235659f..d09df2971d30 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "../mm_internal.h" @@ -2009,8 +2010,22 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc) */ cpa_flush(&cpa, !this_cpu_has(X86_FEATURE_SME_COHERENT)); + /* + * To maintain the security gurantees of SEV-SNP guest invalidate the memory + * before clearing the encryption attribute. + */ + if (!enc) + snp_set_memory_shared(addr, numpages); + ret = __change_page_attr_set_clr(&cpa, 1); + /* + * Now that memory is mapped encrypted in the page table, validate it + * so that is consistent with the above page state. + */ + if (!ret && enc) + snp_set_memory_private(addr, numpages); + /* * After changing the encryption attribute, we need to flush TLBs again * in case any speculative TLB caching occurred (but no need to flush From patchwork Wed Jul 7 18:14: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: 12363841 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,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 B400FC07E95 for ; Wed, 7 Jul 2021 18:49:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 4DCF660720 for ; Wed, 7 Jul 2021 18:49:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 4DCF660720 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3D9DE6B008C; Wed, 7 Jul 2021 14:49:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3AFF96B0092; Wed, 7 Jul 2021 14:49:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DBF76B0093; Wed, 7 Jul 2021 14:49:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0104.hostedemail.com [216.40.44.104]) by kanga.kvack.org (Postfix) with ESMTP id F28E96B008C for ; Wed, 7 Jul 2021 14:49:05 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 5D8C7180286D6 for ; Wed, 7 Jul 2021 18:49:05 +0000 (UTC) X-FDA: 78336679050.15.A58699F Received: from NAM10-DM6-obe.outbound.protection.outlook.com (mail-dm6nam10on2042.outbound.protection.outlook.com [40.107.93.42]) by imf08.hostedemail.com (Postfix) with ESMTP id AE66930000BB for ; Wed, 7 Jul 2021 18:48:55 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=VrJinAzbDw6leG9de3XeUW1Fk3fAPcjLTAq4Z61V7r+xqrXWCEwdTw0+HSQVKIIcuJZrmHPiq9ui84Zm7/ZBJ0248SW18Jd1WxRb10ONWlaR3AWR4a/IwhDdXPVWHQohYB6q4zM7ZBsVT4JvNW9b66nqU/EaOc6xTa3sBbSphd8az/RJSqZaHgabzaEVqSMAFQkCU9syEd1r2/IAGvsM00qFbRcuUWFFUOtvv/LtjUyDSxQzqIw8WNDGvEvVDrGpjiVa7ta4cexG+qIXJSednLibJv+Ba5vdPRtnNMFUF/TMpsxXXeKAOAZjS6ZmE6hl6ztaNdBt3CUvo9kefDHtNQ== 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=fcyoz5aUvbLHR2AC0SIalCexZMW2z/HEutwVqkx1aPs=; b=jCsSMuIE0AeN/CqP4Vj9EnHgkUBMUXwsGioRonbp0Bj/rcKPFUGEru3xNwyO/GNr2Sv7onL27OOZAE9gsujsv7a4AxrwmvenICAzuCmkYaXE83m/+aImgdzM8DoG05qB3i7J7gT90SgT5TCcR/PXwpbOLL/nlVXl3ik8OSiHhFTqZKRB2QAY2wGNRV/PZlTLSf6FQ6hjUpXJtBAexTZ2Acdua0rI0DAa5xKKRo2lXQCCQQpehK9wysTLHgCCSiFeQOO4s1YledtspsE0evoUdSL40qzQxGyWmOIYi2FQf0cfrOJaBpPotgFjor/UqVy1SPX3klSmmDMPqi8uC7LVqw== 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=fcyoz5aUvbLHR2AC0SIalCexZMW2z/HEutwVqkx1aPs=; b=LtciZA8iMEl0eG5qIibvgzoNzN7BEDPWUwu1LhIIn7XiNfsFRTfd4jrJXd+Hj3X5TKn72x/xXfq+VbdefKd5BNkeuSZyKIT8rs+wn3q+adZIkNDmp93xEEBmJiUxtWOZ/0cHv67ilxeljhsALCiDm+k3qHvYx7/J96WGtEWeGkA= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB4644.namprd12.prod.outlook.com (2603:10b6:a03:1f9::11) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19; Wed, 7 Jul 2021 18:16:08 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:08 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 16/36] KVM: SVM: define new SEV_FEATURES field in the VMCB Save State Area Date: Wed, 7 Jul 2021 13:14:46 -0500 Message-Id: <20210707181506.30489-17-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:05 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ce648527-c3be-43ca-f235-08d941734a88 X-MS-TrafficTypeDiagnostic: BY5PR12MB4644: 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: nOW+guXPaU4CbIQglm1gr8l6aFQ0J30BHFyjTeXdAR1tHk9GqpnmTgf3HYvhXGXiObt2UQRlCcOMucUk1JSbfrqz1nbVt7Riw7cFJwOYwx2xeSUjSsZdxzzyWAhASw9UmNwW3zqM0NqnbXWCZdiZSzud5HMw7Kv79tXp8l9AvufI5hqHS50Gh1mWP8d/tmg9EmnomlWnNC/JeXjarIfo2md3MLjzVrDQ8V2IbxlgECYhI7QDKra9kN0watHKNfhV68HmFkLMh4diNiH3nUtXr6xJsYA3Zc1JzlG1FoNDC4xll0bHGUCvp/1zBe6GFRN30POpD/J4P8xqxXiGka6N8sGP6IDEKr4THMnN1yWTt1M6ZMFrSrj79PXuQvQtEuoISqJUBPWboU65cc57L4HTnRLc06JkE3l13d/7qlGrP9gbQtutJ4nEPNTIn/XsaBtHseRpe02p7SZkolcXvJ3/FsXIUCq5h3BbpoLrJt1PeoO04cIkNbA6AaT9n8//HCKZck9E2yyVYUvg7tRj4a6Woi8sNlowUnvutU8sBWE5yynZr15VDD4HvgJhDMDMfvb8/E1AZKiOekKX2BGhKhCk0nipSr7SOVR20lULwYRChNcy+sXkKPwPJqfNXoULygC/h6K2kUDuiDLVm+ddppi26FauxIxs5tAJEMeFRxCGzFd1dPNIa3hfnXuNFD9rTqjf8nW4XsFGbAkkvkVUX+6DaQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(136003)(346002)(366004)(396003)(39860400002)(376002)(186003)(6666004)(2906002)(1076003)(4326008)(83380400001)(316002)(6486002)(7406005)(26005)(52116002)(7416002)(66556008)(8936002)(38350700002)(5660300002)(956004)(36756003)(38100700002)(478600001)(66476007)(44832011)(7696005)(2616005)(8676002)(66946007)(86362001)(54906003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: elEEzqqobmjRDWRowTxGWfU4vclYhzMTKwsc582m/kvcL1dzLiCvFwO0fNIwyXpFVBGDxNzlpzFvTj9MuKZSyoCLWb8w5fzTm2RFz6TMYY4/KKV9TIbqZADrTrgrjOcjgta2iea0VdmZ7kfoxpQrkvLNbJjPhmaNWLIfdI7087gYfdHSgIrK0/sAMmNjkStwGe81kGye/ochMS9S7Kc9sVdy622+4Otow+wfJ0Adv5Rz1S3Cq2CQ7EkSfcFOoFbLxc6YKVvlX9HsT6YQGqwgdKGvThXFfdYqLw0mSmZGfsTzpD1ggsNNNF3hBhiUlAc8uaKAlcySJHmUWgv/VwqGbTse+Ont1KjgPe0csGoUejoINkjKbq1f1JvLg2Nl6378639LtTmMJ54ADAmkA6naon8eLEJ2Fm++3SYyNWMXJrePFbG8bvzpFdz+siIioZ19NUC7JXUXCYeBcJ6JefyfbarGd40KpXrJMrl5Akgj3/Suj2VlgTPPOOjR8UXlm2T2U1EHvacbONRV9MZo7/Tm3XVB0xn7HwsynJNjMWkq9KkhjixELoNS1kTGSfYpJabZA5cCOHt2tmhSJVIiWDPdqpGHx068YYJ9tYSGPnfb9d2wFuOFh7fv9xOsiITx7YbEjQLxQkfIsGemYTD0Lr0gFq8U6sRSEv99eLyog72qfcOK6DsXdtQlp4IAhZs0a5AqVB/4Hq25hslhSWQvtZ1FAUIXXgWnL1iOT5uoHmdywiBAunDVagnXI5DNy56yS6HtJzoxmEsR7Dv9MA/L2RdUE+uhtvP+06Rgb+0pCWdmRPzMShAY5SUUZiv2TAXNzqeC67RLExrjapvTK1ZL75YJalVF35h1SQIzWtGGUrKuiS6FSRyGdr2TB+vOYK7SmDQsjrXf9fhFWBfdp0BGSdFWPnZj8/a7/kGsZ2QEIazDmlJ5P5Bcnwn9IL8KxI1AlVeTi8P0DPTA6Yya58zcHznYPyS+uyC+M5o6IYDpj8B79BFMwJ8zfIPDUK2oNI82bVgJpYb/2GvaYEtXZAJ/uEtq+OlyrTmFjxBdDnsw+ObypGfKqLSFTpB7VvXehR5/PFxew8hhXwP7My6YOOQ07q0EPV3vtUoHETAMoHFH4lL+L1CNY09LCv7/eIgAojC1s6kV2ZEgLWuc+2ABANNyDFz1w9SoQ2MMgWWlwDu9QWkOwW3ldjXqdozHTpfTa0FsFAys3S/VH5aEm7Th0K7dfUUieW7qhqHdV5nZJk0R+vRl2nbW8NmF6dQ+QAwoeRYP4lHQ46T2bo1qG+2J0nEJTSykXfoWgbx9bb2nTbi092j/pZxUYnOcB1RMub+HAfd4QX1l X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: ce648527-c3be-43ca-f235-08d941734a88 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:07.9068 (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: WdpX41fwvi2sL+vKOklu6bxRBNYFLmx8TQ69M5uzahzvkHsx+mm5bNLbwvB1FgVfRSsftp/yjgOSqn4uNoamFw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB4644 X-Rspam-User: nil Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=LtciZA8i; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf08.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.93.42 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Stat-Signature: hkadrw5k5ye7n6aix4ws5cht874p93wg X-Rspamd-Queue-Id: AE66930000BB X-Rspamd-Server: rspam01 X-HE-Tag: 1625683735-100445 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The hypervisor uses the SEV_FEATURES field (offset 3B0h) in the Save State Area to control the SEV-SNP guest features such as SNPActive, vTOM, ReflectVC etc. An SEV-SNP guest can read the SEV_FEATURES fields through the SEV_STATUS MSR. While at it, update the dump_vmcb() to log the VMPL level. See APM2 Table 15-34 and B-4 for more details. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/svm.h | 15 +++++++++++++-- arch/x86/kvm/svm/svm.c | 4 ++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 772e60efe243..ff614cdcf628 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -212,6 +212,15 @@ 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) +#define SVM_SEV_FEATURES_VTOM BIT(1) +#define SVM_SEV_FEATURES_REFLECT_VC BIT(2) +#define SVM_SEV_FEATURES_RESTRICTED_INJECTION BIT(3) +#define SVM_SEV_FEATURES_ALTERNATE_INJECTION BIT(4) +#define SVM_SEV_FEATURES_DEBUG_SWAP BIT(5) +#define SVM_SEV_FEATURES_PREVENT_HOST_IBS BIT(6) +#define SVM_SEV_FEATURES_BTB_ISOLATION BIT(7) + struct vmcb_seg { u16 selector; u16 attrib; @@ -230,7 +239,8 @@ struct vmcb_save_area { struct vmcb_seg ldtr; struct vmcb_seg idtr; struct vmcb_seg tr; - u8 reserved_1[43]; + u8 reserved_1[42]; + u8 vmpl; u8 cpl; u8 reserved_2[4]; u64 efer; @@ -295,7 +305,8 @@ struct vmcb_save_area { u64 sw_exit_info_1; u64 sw_exit_info_2; u64 sw_scratch; - u8 reserved_11[56]; + u64 sev_features; + u8 reserved_11[48]; u64 xcr0; u8 valid_bitmap[16]; u64 x87_state_gpa; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index e088086f3de6..293c9e03da5a 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -3184,8 +3184,8 @@ static void dump_vmcb(struct kvm_vcpu *vcpu) "tr:", save01->tr.selector, save01->tr.attrib, save01->tr.limit, save01->tr.base); - pr_err("cpl: %d efer: %016llx\n", - save->cpl, save->efer); + pr_err("vmpl: %d cpl: %d efer: %016llx\n", + save->vmpl, save->cpl, save->efer); pr_err("%-15s %016llx %-13s %016llx\n", "cr0:", save->cr0, "cr2:", save->cr2); pr_err("%-15s %016llx %-13s %016llx\n", From patchwork Wed Jul 7 18:14: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: 12363847 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,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 0774AC07E9C for ; Wed, 7 Jul 2021 18:49:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 8440161CAA for ; Wed, 7 Jul 2021 18:49:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 8440161CAA Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 70CC56B0095; Wed, 7 Jul 2021 14:49:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E5366B0096; Wed, 7 Jul 2021 14:49:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5364C6B0098; Wed, 7 Jul 2021 14:49:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0239.hostedemail.com [216.40.44.239]) by kanga.kvack.org (Postfix) with ESMTP id 328D76B0095 for ; Wed, 7 Jul 2021 14:49:46 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 98D18180ACF16 for ; Wed, 7 Jul 2021 18:49:45 +0000 (UTC) X-FDA: 78336680730.38.8BBFCE7 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2087.outbound.protection.outlook.com [40.107.92.87]) by imf07.hostedemail.com (Postfix) with ESMTP id D2B1E1003694 for ; Wed, 7 Jul 2021 18:49:44 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=hqi+4F38wKxPmVlHnk/mbvHMZ5MaVpDVvIVxYljwfem1PBn242xAnuT7SmGNUDF7x5QC7Icw37txpJAZFCQZV5BYX2WZiD4WnH8/qefDHAvC8OHYsvvW8BAikm/X2Ko1mdA4GVAasPxC77soOUn7cG4lsjyr0eO32tum7ZCl267rN/cSxlEEao76IxkART/sbX0ueH6XAh+MDCrQv5jZmM6bv3bDR0kzCJ0to8JR1eg25dTT8+k0j2w1CKIKqIMFNm4eJqBppRUFlay//FM2DPRfcQplom01G5l3j4fmRzJpvHoUKsa/BVlJuarLV7UliWwwrNvVNPhBpjF2GxJF/A== 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=ar10hDCValtNRg7sDPldEtmq+v3w1Dk8txZ2Tu9WD4s=; b=PtLWSG9Pq92WMR6pwX0Xe2ifvRtqiO8VXfYhK7BUupDRS4CrGiZObtullOZlu3NZmIAzO8miPRi+p/OZl2b6i5I/hn6hReGyXub7I/lBBlfkfyzd2vJqfadE7pwYadcG+OmY9nBD47l4JDgNZfaGBNhiYTnhKmhrPy8SimeHCHnpricD1uLIe8W1RcFqqraT4wv4bcmo6WKOcQ54iKbrv22CjBiJHRlFPcGL1FO+aqMa5cgDx2hGw/wVbHiq6jANTb+I04o4deoY/L22tkImrla+KxsgC+0+zFB4dcQfFKzNheWPKzAioQqMF6YWxEZ8Me7ttsZl4ebFfOGuyKq8hw== 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=ar10hDCValtNRg7sDPldEtmq+v3w1Dk8txZ2Tu9WD4s=; b=Tweh1vGcz7sAmywdfrDqxTfvD8qbxAnUAbUDaWe7Ueu7wdlVvjjtMeGQ0iyA3G3wRFimFaHYyRZiHezLZLCRC+2lUismYzd+nG5zzReGqg9hBNe8CDRIyeGfC7XWw6oBeO6YwN/+XspvH7T8guWcTdcjMssnJMzZlTZ8fRFX2ZQ= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:11 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:10 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 17/36] KVM: SVM: Create a separate mapping for the SEV-ES save area Date: Wed, 7 Jul 2021 13:14:47 -0500 Message-Id: <20210707181506.30489-18-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:08 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 388a9173-a906-4d7c-7102-08d941734c30 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: 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: zYplID/TrRuUAQhOwFguZAKBzy+d8zWgxU8tP/mFjfDrn6U90op6eO5WQium0NXGcwVfgA3aegvGfbvxGmZU83+bt2n6GG5Bc2poeBrLBP3lG7o/9uCrQ+QJxfAD4H5iMadQlm3z9BBRRcKzauYHKDYLkcTvhE2PtJ5THhZdE/me+Q/TIZmsnDYWrDj2kVHhXs0bXRvYImHEXnocJdqflZIuUH5Fo3JMu8mKxoUYnfFPnw0qFvHvdKqkNJDBM+fBlostKqQGc16SPIlS0AvAR7x7II1lm/VEjs31F4hlu3uweF554iELuoEGhTcIeSgvkQhehG82QdUOFruTQuIcWQKl+3++3M1OjcwcCRM0zYSdpZjZkTToIqUvPPBrgS6bWt0S3Dp1Kz4q4J8IDBH684s9XOJJdyFfNnSeZUbx8NkShbXfcJ2x8wLjRD0ADN984iIIWnr9OcYMNMGc1+3BXq78snvYYSRRUPi9jOzUIqeUN625mS67f/KTnEkPA/uwPNlYlJPSw3kt6VzQwrkm0YSxuwI/qm3QIMIEWuqgo6eXmJKwBTTz03QrorKWOmAnSagG8YoskJZ6MbPA7gLC5x7VyE/nmLdQiNzIjra4HDo8rtUP/61T0sO1PoqxVU/Sg3jODLrLHYdjpE4Z67Lxh2Ehso+xx8Uec99Nr5QQ9aPyiChx4iez6rvZGRRPo9FzyFDGJa3Xgq8j6UBpgl4HMA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 6R5MeQIrPiqyGUEA0NJUWXOU3jVKirkDq2Ha9f2sNJw/1jJGBJjXKVDG7vfNbrezxd08c61pMbkBAAn7wi/WLhyYaSNcqSKKGEqUbZHZXKLopx5KsIAprHiAkkVyL6XKzefuX3H14X06pexCzpIJ5LiRuhyv9D142CTVhEGzPi5I5EoQvnloo6ZlBX+7sOlKGlU0Yxg+5yBklBuWqaJ3A6EBSqYYXupMRlAOpskUoqd+kfkQNgMvT6D27zYP5ZdfAPZ//Kfl7xoeYPV320THeODNyWwF8B7qsCJikwkVkvkBToOabjw9tUwucelPmVXPQGZAcxIC60iuxDPvIvXF+hcoBtrhv46dRBnuUmA5rGVP4Nfghy5k+qMRWrL3QpFPiKxbKHaH6gThcvtMtICdJs2uxYwMyv/MuzkDwU2AsRCNIwTtQp/rI8f6/kMrYLuyexFFu+HEywEwEzApUQ7ODnTvmdhA5/5PNcHKLVgxeJRYS1vUQNwfX0hanV68gxYa0ENQwboESCCaHER2RtA7DScqrYlWbbf2QjEmjgT0cpepajzE9dwS4jrFBorCzdIFvCvQp5fW5RayFyD3sjOX4FDZG+lHP7eLG+OVYmqWBS/x39aj0oLwlIswHHanxnG8HhqJ5eMb8GvoW9aKdRye+2gON7u7CcK0oHyk26rA7euxBnFHVmqdNno22rv7TnWgmRH4vhm7rBXhUBFIEVdHIwtSGAnXplb/swA7mh0kJWZPp5E32qC2OgMWi76VSy184pF1CT/a840qJ8hLVeU1OxsV6Ck36eDhMrjZypSq0Peoh6mivQljPh3Vt8VArLlooaC0A0YrQewaZA1B19I1dU6xKe5h4wcKvmick70GX4d2Qk1Tod+/CkEgKL38DbyQJIt6MANH/SarvRdH45PqwDkaC/2W+t6k0MFyiq8fAX44D8rSESRIM80VmNxO5ZDffcLXWGktGsgLrqn/SC+G3hFx9AcPSfK3RVKu6os5pwAPebU+KahBgBQ97rC2G2no3h4/Ua6myxW0g4SaiysuCprrYPHUP1Jn84X9LFsr1Pxuo+wi+wdHV7FxgSvdpUlOTTK+hji2NtwtnqJkCjYegiKkG8TTFdH2Pjv7lZga/J4mHcj8+LfWTttjDsdvlq1k9iEDe0xCCrt0lJbkfGFjv2/SzxrVeoRI0V6ucT59IF0BmK8zFKhY9EPn1M51ESNVijvGl/wkHJRxUdgyLITbKzQFeFpEOwVohxxnjyo8L8li0aViBUQNqJH4xWBugwW0WhpjVpBR1pixtBMmBW3nbX9fww2OXDUg4UHwiZlCjKWcGULF5pIaEwTp4TaBb7Ml X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 388a9173-a906-4d7c-7102-08d941734c30 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:10.7581 (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: 4Ij8C1759YO+I+RlnX/eG4kIfRtmiQ4IhJl1DaclJug1haROuU3v/zmZAjr0df8MVyHSGNehDfhZ8DBANQq2HA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Tweh1vGc; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf07.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.92.87 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D2B1E1003694 X-Stat-Signature: 3b7twmongbhh4m8epiehtffpdyco69zj X-HE-Tag: 1625683784-404036 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Tom Lendacky The save area for SEV-ES/SEV-SNP guests, as used by the hardware, is different from the save area of a non SEV-ES/SEV-SNP guest. This is the first step in defining the multiple save areas to keep them separate and ensuring proper operation amongst the different types of guests. Create an SEV-ES/SEV-SNP save area and adjust usage to the new save area definition where needed. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh --- arch/x86/include/asm/svm.h | 83 +++++++++++++++++++++++++++++--------- arch/x86/kvm/svm/sev.c | 24 +++++------ arch/x86/kvm/svm/svm.h | 2 +- 3 files changed, 77 insertions(+), 32 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index ff614cdcf628..a7fa24ec8ddf 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -228,6 +228,7 @@ struct vmcb_seg { u64 base; } __packed; +/* Save area definition for legacy and SEV-MEM guests */ struct vmcb_save_area { struct vmcb_seg es; struct vmcb_seg cs; @@ -244,8 +245,58 @@ struct vmcb_save_area { u8 cpl; u8 reserved_2[4]; u64 efer; + u8 reserved_3[112]; + u64 cr4; + u64 cr3; + u64 cr0; + u64 dr7; + u64 dr6; + u64 rflags; + u64 rip; + u8 reserved_4[88]; + u64 rsp; + u64 s_cet; + u64 ssp; + u64 isst_addr; + u64 rax; + u64 star; + u64 lstar; + u64 cstar; + u64 sfmask; + u64 kernel_gs_base; + u64 sysenter_cs; + u64 sysenter_esp; + u64 sysenter_eip; + u64 cr2; + u8 reserved_5[32]; + u64 g_pat; + u64 dbgctl; + u64 br_from; + u64 br_to; + u64 last_excp_from; + u64 last_excp_to; + u8 reserved_6[72]; + u32 spec_ctrl; /* Guest version of SPEC_CTRL at 0x2E0 */ +} __packed; + +/* Save area definition for SEV-ES and SEV-SNP guests */ +struct sev_es_save_area { + struct vmcb_seg es; + struct vmcb_seg cs; + struct vmcb_seg ss; + struct vmcb_seg ds; + struct vmcb_seg fs; + struct vmcb_seg gs; + struct vmcb_seg gdtr; + struct vmcb_seg ldtr; + struct vmcb_seg idtr; + struct vmcb_seg tr; + u8 reserved_1[43]; + u8 cpl; + u8 reserved_2[4]; + u64 efer; u8 reserved_3[104]; - u64 xss; /* Valid for SEV-ES only */ + u64 xss; u64 cr4; u64 cr3; u64 cr0; @@ -273,22 +324,14 @@ struct vmcb_save_area { u64 br_to; u64 last_excp_from; u64 last_excp_to; - - /* - * The following part of the save area is valid only for - * SEV-ES guests when referenced through the GHCB or for - * saving to the host save area. - */ - u8 reserved_7[72]; - u32 spec_ctrl; /* Guest version of SPEC_CTRL at 0x2E0 */ - u8 reserved_7b[4]; + u8 reserved_7[80]; u32 pkru; - u8 reserved_7a[20]; - u64 reserved_8; /* rax already available at 0x01f8 */ + u8 reserved_9[20]; + u64 reserved_10; /* rax already available at 0x01f8 */ u64 rcx; u64 rdx; u64 rbx; - u64 reserved_9; /* rsp already available at 0x01d8 */ + u64 reserved_11; /* rsp already available at 0x01d8 */ u64 rbp; u64 rsi; u64 rdi; @@ -300,21 +343,21 @@ struct vmcb_save_area { u64 r13; u64 r14; u64 r15; - u8 reserved_10[16]; + u8 reserved_12[16]; u64 sw_exit_code; u64 sw_exit_info_1; u64 sw_exit_info_2; u64 sw_scratch; u64 sev_features; - u8 reserved_11[48]; + u8 reserved_13[48]; u64 xcr0; u8 valid_bitmap[16]; u64 x87_state_gpa; } __packed; struct ghcb { - struct vmcb_save_area save; - u8 reserved_save[2048 - sizeof(struct vmcb_save_area)]; + struct sev_es_save_area save; + u8 reserved_save[2048 - sizeof(struct sev_es_save_area)]; u8 shared_buffer[2032]; @@ -324,13 +367,15 @@ struct ghcb { } __packed; -#define EXPECTED_VMCB_SAVE_AREA_SIZE 1032 +#define EXPECTED_VMCB_SAVE_AREA_SIZE 740 +#define EXPECTED_SEV_ES_SAVE_AREA_SIZE 1032 #define EXPECTED_VMCB_CONTROL_AREA_SIZE 272 #define EXPECTED_GHCB_SIZE PAGE_SIZE static inline void __unused_size_checks(void) { BUILD_BUG_ON(sizeof(struct vmcb_save_area) != EXPECTED_VMCB_SAVE_AREA_SIZE); + BUILD_BUG_ON(sizeof(struct sev_es_save_area) != EXPECTED_SEV_ES_SAVE_AREA_SIZE); BUILD_BUG_ON(sizeof(struct vmcb_control_area) != EXPECTED_VMCB_CONTROL_AREA_SIZE); BUILD_BUG_ON(sizeof(struct ghcb) != EXPECTED_GHCB_SIZE); } @@ -401,7 +446,7 @@ struct vmcb { /* GHCB Accessor functions */ #define GHCB_BITMAP_IDX(field) \ - (offsetof(struct vmcb_save_area, field) / sizeof(u64)) + (offsetof(struct sev_es_save_area, field) / sizeof(u64)) #define DEFINE_GHCB_ACCESSORS(field) \ static inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \ diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 8d36f0c73071..751a4604a51d 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -552,12 +552,20 @@ static int sev_launch_update_data(struct kvm *kvm, struct kvm_sev_cmd *argp) static int sev_es_sync_vmsa(struct vcpu_svm *svm) { - struct vmcb_save_area *save = &svm->vmcb->save; + struct sev_es_save_area *save = svm->vmsa; /* Check some debug related fields before encrypting the VMSA */ - if (svm->vcpu.guest_debug || (save->dr7 & ~DR7_FIXED_1)) + if (svm->vcpu.guest_debug || (svm->vmcb->save.dr7 & ~DR7_FIXED_1)) return -EINVAL; + /* + * SEV-ES will use a VMSA that is pointed to by the VMCB, not + * the traditional VMSA that is part of the VMCB. Copy the + * traditional VMSA as it has been built so far (in prep + * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state. + */ + memcpy(save, &svm->vmcb->save, sizeof(svm->vmcb->save)); + /* Sync registgers */ save->rax = svm->vcpu.arch.regs[VCPU_REGS_RAX]; save->rbx = svm->vcpu.arch.regs[VCPU_REGS_RBX]; @@ -584,14 +592,6 @@ static int sev_es_sync_vmsa(struct vcpu_svm *svm) save->pkru = svm->vcpu.arch.pkru; save->xss = svm->vcpu.arch.ia32_xss; - /* - * SEV-ES will use a VMSA that is pointed to by the VMCB, not - * the traditional VMSA that is part of the VMCB. Copy the - * traditional VMSA as it has been built so far (in prep - * for LAUNCH_UPDATE_VMSA) to be the initial SEV-ES state. - */ - memcpy(svm->vmsa, save, sizeof(*save)); - return 0; } @@ -2606,7 +2606,7 @@ void sev_es_create_vcpu(struct vcpu_svm *svm) void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu) { struct svm_cpu_data *sd = per_cpu(svm_data, cpu); - struct vmcb_save_area *hostsa; + struct sev_es_save_area *hostsa; /* * As an SEV-ES guest, hardware will restore the host state on VMEXIT, @@ -2616,7 +2616,7 @@ void sev_es_prepare_guest_switch(struct vcpu_svm *svm, unsigned int cpu) vmsave(__sme_page_pa(sd->save_area)); /* XCR0 is restored on VMEXIT, save the current host value */ - hostsa = (struct vmcb_save_area *)(page_address(sd->save_area) + 0x400); + hostsa = (struct sev_es_save_area *)(page_address(sd->save_area) + 0x400); hostsa->xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); /* PKRU is restored on VMEXIT, save the current host value */ diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 2908c6ab5bb4..bb64b7f1b433 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -170,7 +170,7 @@ struct vcpu_svm { } shadow_msr_intercept; /* SEV-ES support */ - struct vmcb_save_area *vmsa; + struct sev_es_save_area *vmsa; struct ghcb *ghcb; struct kvm_host_map ghcb_map; bool received_first_sipi; From patchwork Wed Jul 7 18:14: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: 12363837 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,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 C1126C07E95 for ; Wed, 7 Jul 2021 18:48:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 625B3611C2 for ; Wed, 7 Jul 2021 18:48:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 625B3611C2 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 1DDF96B0089; Wed, 7 Jul 2021 14:48:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B5286B008A; Wed, 7 Jul 2021 14:48:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F23696B008C; Wed, 7 Jul 2021 14:48:31 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0125.hostedemail.com [216.40.44.125]) by kanga.kvack.org (Postfix) with ESMTP id D004C6B0089 for ; Wed, 7 Jul 2021 14:48:31 -0400 (EDT) Received: from smtpin38.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 3EF1D18000DC4 for ; Wed, 7 Jul 2021 18:48:31 +0000 (UTC) X-FDA: 78336677622.38.A35E841 Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2063.outbound.protection.outlook.com [40.107.92.63]) by imf22.hostedemail.com (Postfix) with ESMTP id AC8B019A1 for ; Wed, 7 Jul 2021 18:48:21 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=C1HfDtf/r9QcYP0qt9T/NIUoI44npi2AbIPphWQkynxv+wIvDTbQWZTIIbkLE671mWquSWYZ9/K5Rw8Ch+hL4v0nBADxvjYBcgX5e+olpGZ+y2kBcuqH7YdyDba781MRYjoKrC5tJHmJwI50hOOUl7A7qUhfVIz0+G5p9EoCVKh+MNsd03dar9axWoKIfc06zeXbCOIvD1L8U6BRD2tCSlWI7RhCItYlei4FuOEtOiJ+g3255/jOYXAOxT9Nyv1CRn/xk2FX6i0e7ABDtKg63btPTrflTY3gVybpB8cRFJOulkdjWRJ67pb6ys+CZbiGlZXkDz5pXHkojCdOaExmsw== 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=N2UEm+g7vGiRO+IwbGeOaepODcTaS5c1f0WvKtelg/8=; b=K7RWYnkl2FsmNyBw37U/3Sc7IIZkyaE/o3iv0yQ7jcDTD0SCb46ElCt7kTD/SIZFO0PLxjrlTnwYa56WB7lXi5EFnIHQPE5ofXz30cFGHitqoWyuNN36g4qZCLt8CMCvXEtwecUoT5lwwgQUybyAFvYa7CiuDJ6yW9GQ3XDNFYlo/Mzwb24ehnwX/krVnoPmagXGFL/SqPca/r+aqGqrL5RfgY/r14c1X9a1HoYduOXlqNkY5A/TqXXqaOygcWYMFgskbzP8i0efUBY8r5teZNqGqA4XYsg9o+0xMLUsA9T/lI67o78DIW8EAKcko4z2h+dA3ulqS89ic3B4Qr/Z1Q== 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=N2UEm+g7vGiRO+IwbGeOaepODcTaS5c1f0WvKtelg/8=; b=ZZRIudS7O7mWO+/x4vuc/ZWw8dAnV+5aGLpqk2Vf5gIzTwY9qAX4HpX14wsVktYm5RiFl3XWvLG2AMoviuOL3biFUBk879CL1PsQP8WEJXyo/wkIBHIKBnWp5EmTnn+NtLsif2pYO9xPw/Uadbge4rWYHdBH/EGyq9m5Mem2Rpo= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:13 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:13 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 18/36] KVM: SVM: Create a separate mapping for the GHCB save area Date: Wed, 7 Jul 2021 13:14:48 -0500 Message-Id: <20210707181506.30489-19-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:10 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: cb1e99b7-ed0b-4902-e95a-08d941734db7 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: 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: IY67mWT85vMMeoTFmGl2HXUxpV0peO6/0IgihhmnJiMjAr63egtE3YFb0AtbY6K1AVd4rroKIsADqtXJG+55S6o/6s33qn3UJJQgu9laW+k5wXjLj3o35toQLaS3a+AKy7vuHuwLnOTV4DI70oOtHyddPJSIKsPdgU1cm5uJgbSf4fyNUHkYh50q20Ozirb+OQsuZWgtaWzckI47kaSxup68ThkEREmA2WK/ElFohN4RDTliDh76GQr86gX5Sm/7YHCDliFpZR7zNDK5hJv8eru32tzueTxb9dKXc/ym/aUDJKfmO5TeeDaoyIKBwDty1q0ok7SES6JGH+F9i2wuYDHroa/v22TFACSY90/xrxXPOa3izlx7ZGE98MuniID5m5F8S2LEj+aT3VxBWjDqVttqtiU8eKiOSFnZD7niwDpOXv70bT25/1tkBc8dDXBRLnsT9zTZ4010C1M0Gekz0aVCan/r92MUYWI0oSivOeTeehhghiwfrmdGcuF1eOpKQ60y/lHtBAn9pWCHQ9NI+hgqlU9c+zxft51SVdOfOSlQVCshC+ZFRQyZiNPcOKdhzGSHOgDd5/SuuPj9PWk8zRJH8VPcPN0ij8zu82JFJNWWk4aQC6/dG2b7hj5FOXSQFxQIc1dkN4kSYxEUbbK9z2V9ibAHZAqeO+pTHYGs/Q/+qc4XYvtik/oG92LEtkQ+G26w6kH5JEx4lWaslbWmKA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: YJELrMwPCjJ6sadsuDNX1nbKPjuUMGfup7LXVwEMZHEbhK150KIP3yqy3DTcMSecrWQ8qzt9bLVc8KKXFiQBvvRwl29zXrj/ERrBiBkrb1lZ0jGTTG6s63+W/1VxrjeoIsE8Fh5rtM4X4AjC/a3fT26i1Sz4lfI28SijpaDkiGMvTF3dPKnxWh1BNop4h2PdmaKuJ9eoH2FhXzurJNuYh4YR5jqf490CXkbvutH6QB25lxkyZRJkswXbMvldCp5nT1kQ7B+yJf2OH30m8xZ/lE9ypm9cfmXLA/ZfyE0ZQxxRlsupnPHRKGXZqpb77THY4iNnmZYnzMMhXSkjhTi6w28ja6MR3RSg3MU/PnWubWhRuWFtl0EHnEMT57bHdoAhbSmQMcG7TE+e7VELzWpcmlfTySpc09PTKd6uefuWygIGveYjanqsya9ujv7oD9Idzh/IcWpLdKsMlEPUErpFe1/juQp4jLvvYs4Qo0ZiztxG2aYhcrrQfC/pzZfW+8ciIgzVPe2StXDTzIRiMGa9TqlqGhLvDBY0161SXgi3GIgZjMUSTPW6bviRfVMaNUsQOZd09zGk994/7HB5nrqvGOAgfv5BsdcUzUfB1AIzi5x7rYYW+J6x9JjF2SZWXC5OUgDmDAJwliXNeJKlsvCrf/57C39kGshSE7xjBSx9Leig5DWZLWrBprGZdTUWx3YZ/gROVymhsu2qg1YVr0YFH2HZdRHzEakyeEQdRnkHjjzSoRJDo0HA7Pez4pxqRF54BUmkQtZtSCh+kaBkbwRqSvwnHXJQhn37YhP8ULriCA0Ftg3lOXbvf1nJOFnq6cnRbYCJ7851h7n+2u0jV5W8bSufKHJztFEZ9e9bTl7hwXV5D/ms88X/cuH5Jz3n5cN3ZilJ/eo030Ue19xTTX6ONNAv67q++mgqgqo9lLxO/vXdYPnx2ag22Pv2Vyuhk9yDYCP0dyls05Syvx5icg8M4SpqzEguCiKZsFsl1Ouv/RlAgkTc/3ECd3zoW7QeFz/XT0ozMj+gpG/KUlPzoVk/aFJ/s4Ou00+L1JSMDkrR5/NKLx2sMixeNdwULkqdiL9xj+5LaYID0lXMMIWIIjd+NBS78freVDONzcLh9sZ5lMH/54/Uk82/LnwRYMOWyBTH24Nd6gfg/LTDN/U/lAtZpy0nQMUI69gplIkFR2roVaiehqCbq5ZHbQwqi3TArgb0/F6YRm5jGPLArCqZY07GpvXTankdJq7eNGmBHOrpff4xgzlgsuZTzitAu28x9D4EtxZ8Hop9bOP5Z4Xs6IFn6GTcWa1zzneVNvaXxxueg/tKGHR10/mchwvMUN6/0KLZ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: cb1e99b7-ed0b-4902-e95a-08d941734db7 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:13.2517 (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: 9TLzsh9HK4xmRKIw2p3dSUTM5xObkJaWIQ4lflwhww9JN1DBGKE3frgGDAIJxSXNfzUwlsU+6kbmq6OPBG7JbA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ZZRIudS7; spf=pass (imf22.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.92.63 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: mfu1cx1mdghi7k1p5jo976znsnaj7cwj X-Rspamd-Queue-Id: AC8B019A1 X-Rspam-User: nil X-HE-Tag: 1625683701-192972 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Tom Lendacky The initial implementation of the GHCB spec was based on trying to keep the register state offsets the same relative to the VM save area. However, the save area for SEV-ES has changed within the hardware causing the relation between the SEV-ES save area to change relative to the GHCB save area. This is the second step in defining the multiple save areas to keep them separate and ensuring proper operation amongst the different types of guests. Create a GHCB save area that matches the GHCB specification. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh --- arch/x86/include/asm/svm.h | 48 +++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index a7fa24ec8ddf..f679018685b6 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -355,9 +355,49 @@ struct sev_es_save_area { u64 x87_state_gpa; } __packed; +struct ghcb_save_area { + u8 reserved_1[203]; + u8 cpl; + u8 reserved_2[116]; + u64 xss; + u8 reserved_3[24]; + u64 dr7; + u8 reserved_4[16]; + u64 rip; + u8 reserved_5[88]; + u64 rsp; + u8 reserved_6[24]; + u64 rax; + u8 reserved_7[264]; + u64 rcx; + u64 rdx; + u64 rbx; + u8 reserved_8[8]; + u64 rbp; + u64 rsi; + u64 rdi; + u64 r8; + u64 r9; + u64 r10; + u64 r11; + u64 r12; + u64 r13; + u64 r14; + u64 r15; + u8 reserved_9[16]; + u64 sw_exit_code; + u64 sw_exit_info_1; + u64 sw_exit_info_2; + u64 sw_scratch; + u8 reserved_10[56]; + u64 xcr0; + u8 valid_bitmap[16]; + u64 x87_state_gpa; +} __packed; + struct ghcb { - struct sev_es_save_area save; - u8 reserved_save[2048 - sizeof(struct sev_es_save_area)]; + struct ghcb_save_area save; + u8 reserved_save[2048 - sizeof(struct ghcb_save_area)]; u8 shared_buffer[2032]; @@ -368,6 +408,7 @@ struct ghcb { #define EXPECTED_VMCB_SAVE_AREA_SIZE 740 +#define EXPECTED_GHCB_SAVE_AREA_SIZE 1032 #define EXPECTED_SEV_ES_SAVE_AREA_SIZE 1032 #define EXPECTED_VMCB_CONTROL_AREA_SIZE 272 #define EXPECTED_GHCB_SIZE PAGE_SIZE @@ -375,6 +416,7 @@ struct ghcb { static inline void __unused_size_checks(void) { BUILD_BUG_ON(sizeof(struct vmcb_save_area) != EXPECTED_VMCB_SAVE_AREA_SIZE); + BUILD_BUG_ON(sizeof(struct ghcb_save_area) != EXPECTED_GHCB_SAVE_AREA_SIZE); BUILD_BUG_ON(sizeof(struct sev_es_save_area) != EXPECTED_SEV_ES_SAVE_AREA_SIZE); BUILD_BUG_ON(sizeof(struct vmcb_control_area) != EXPECTED_VMCB_CONTROL_AREA_SIZE); BUILD_BUG_ON(sizeof(struct ghcb) != EXPECTED_GHCB_SIZE); @@ -446,7 +488,7 @@ struct vmcb { /* GHCB Accessor functions */ #define GHCB_BITMAP_IDX(field) \ - (offsetof(struct sev_es_save_area, field) / sizeof(u64)) + (offsetof(struct ghcb_save_area, field) / sizeof(u64)) #define DEFINE_GHCB_ACCESSORS(field) \ static inline bool ghcb_##field##_is_valid(const struct ghcb *ghcb) \ From patchwork Wed Jul 7 18:14: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: 12363469 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,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 CF7CCC07E95 for ; Wed, 7 Jul 2021 18:32:25 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 76F8961CBE for ; Wed, 7 Jul 2021 18:32:25 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 76F8961CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 662856B0080; Wed, 7 Jul 2021 14:32:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 638C96B0081; Wed, 7 Jul 2021 14:32:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 48BA36B0082; Wed, 7 Jul 2021 14:32:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0134.hostedemail.com [216.40.44.134]) by kanga.kvack.org (Postfix) with ESMTP id 222EF6B0080 for ; Wed, 7 Jul 2021 14:32:25 -0400 (EDT) Received: from smtpin37.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 7D480235A9 for ; Wed, 7 Jul 2021 18:32:24 +0000 (UTC) X-FDA: 78336637008.37.A056FF2 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2059.outbound.protection.outlook.com [40.107.94.59]) by imf12.hostedemail.com (Postfix) with ESMTP id B09FE10000B4 for ; Wed, 7 Jul 2021 18:32:19 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mlmiLKHXbqrZBwsy3oHqBXLNUypX3wbEJHCovvSIFvWjf4fi3ufrNzCceA2Wzo5sSdmuUGO5S1Cgs7KMfDbtZdHFR9gNX3EuclD5ZRQbeM3FcEvHXdhlhhyS6aOllA5qQT1fEp7Qj0HzwB68CblPVj4qc5VwniweS0z+lI7w3zeCs7WuN3gfAir5MlXkuCFsDFhiWfo8jn7DaG0z3caJCdHC6F7GsReBhxm+6H5+qsC6p9kTYISz3f7k8hYzTbiO0umScOCIHIOorukMrnCR555pU78hoGtrYfVj7EnsG3z70eH3SnBcLLE4klRPheHUE+6iCZ/UFvID5qCtp3nBpA== 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=EBt8/TPmDL1A3YsvTSKCK8gVuH33bPFiojteShvsYIs=; b=oHiIlk1N5+8I42QkBW+36XUSoZX/f1f95MX4ff8ZhdspuDPc645fdMh3w5nKnqMpaQWnvRET5yyojVc8ETbETpbijmVIp08QN838O+pl6Oy50ITkLG2cbPOrF/yIxtWuV2dd0dCjX63Goz+h+9JOY3hG5NEfvuMqqVEsBk3w74TefX+P9F2pqB9LWwmouzOak10sxK4VZyoA0yuhCbn9ULd+jKFQk5Z8jp49S1pzaWrxAfH7W8PvIjdSqYFW3Fhh+l0rbaImvdRgLc1CqagRMGTmYCtUygOBVFGCl4/GKgBRtdPrc9fgGpSrKskdo+CyIoqbjJysvx061LcmfkvAxw== 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=EBt8/TPmDL1A3YsvTSKCK8gVuH33bPFiojteShvsYIs=; b=3o2BsnyCm83F1JLQAVX5jAVACu6AkmMBeEMPMYljUVX1OknDtnGMFDFcl7O/yBl5FO+qFmPNIGirGulmpb69a8milc3WZa29iRjH5Sx0LJNC8A44b2RUFlyKnQq3c+6TDpIGSK09I5/7H1V2gleLiariX3yzfURvldkp53ZslFI= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:16 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:15 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 19/36] KVM: SVM: Update the SEV-ES save area mapping Date: Wed, 7 Jul 2021 13:14:49 -0500 Message-Id: <20210707181506.30489-20-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:13 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7f7626f0-7dc4-426f-1441-08d941734f36 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: 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: xppIfKs2pOnKfOktzBjj1xJSEnGuPnYKzrLx4DYqBgYbz4T4qmrSQ/fhNmDtfrvQHMkUEY6I8ZpdqNzER9rWcwQEf+IIs1OxwNaY9LA9hxl4Znokhm6oBwTc4YOr8+/0yWzt9Pwgooa7iqmrzLSLZI01MwE3EQeOmhSNfVhdaZi6qeCM6yzmPPHhco/0JhK2mq8m2sx5IVBf0T1uv5EyNurrHBHzNNTCBx7q/o+Fiotr3kYMWlP/vZ+tlnFi6PusnyIqW6UgRiE31G1Dap0gGHsypyejIuPWbXHQmPvNsECrRb0uA1HWE5cXQVdm5u8rl1yNGwKr2uMq4zxXnvWw0TUy/QBN9FXLY/KWSxY0+AD8H7Q9FqreyV4A169By36p27Yu9tsLR1ai3+dKkYXlhNHbSwAsXxBUwj5BfwuiDOfhvazKDUE0mLzz6PKkD0nkcR9vospQUIpaKzLPDHuAyKRKrc3LW2z/BxcVDlCJZ5sxcyAegRxo1sjFlQUnAXB9k5knTa77lSm2G9l1/1FpRsSb19BYzD8a6mqZd1AU/561vrI6KRl8VRQumMwa8FpiJtbJeLCgzibZ04YmxgBV40aB8RXIeoHxwHbYMHYfChEMFOlHmflE/WqUt8KUGY5h41pym8tagle2dzmylPzJm2PAZVP7rMa5MkWORDxMsgQUQsO61O8lgik+CgAVJZa2 X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(15650500001)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: CjuuH2n2QqJtX/wL5NuM/FbuX46iOMvf7bmWRwU0ymJtTOMazz/i7oR4MR46L+RWiudxttra4dy8LtAHybAl+8mbzAw8l0ZsG/o4YvWZmtSpFa7Mg+jCuf80A6pi8lug9ybeOOjeoRs2Boc+jhOEnR0vR5vtEWIqtQp97AInuDzhKfvWu2fKgWHxQRT84EA5TPtVReBeJ6o1yCktuPoN+rTvZT3SDEuaLPR8HBPXQJERvIlrR5919ijrNiQmLnNMQ3UKMwRx9i/pd2m0iMP8hGzbHnXJ7ldJlwn3feDV93N7+f4KYITHWHPdntDRfwfQAirVfOovkqqVtpqj02Hw9W1xrMmQOkG9N8ObXPiMAgoCGTNSqhOOQNIY4gQCRWx7s6WptAYfzs1kFsfNG5z/mzrvhDpEfmnfnGPRYVAesUup5By8X/6dLqaFxIuz7jiPJaYHomMcAmo6JVjgWnq34z95m/prnx3U4B2rIb4xpz/LNarTX6U4vB9M9XrNkthz1vBpqk9Ga6UQrmdKOsbIVr/99qZ4kOEEACikr7WLIIRvpgT8hGE3DvK7bZnJRbcqvL5lHQvkbJ93Jqv2Ea0DJn5nrNvftPcn4/x6L0msUUXSjo0BgW0YlfOkzhgDXIv42Avn9H+y1MuLozHq0JEu1g5xHFIvz663yWhDYw7JoSAiiZEV+eeXA+g5LNJF9ThJuwlpKKoUpz7p4jipiuk+mAx/sYwg9XOy5s9WWxHODO4HfX1FjCtKs3Wrv+LySnmxvrOY4aUUdwEYizVuSEJVqWI3y7aFddWDj+l3MjoDym12o0BZ9iT1sxeYESZlqF+m244ZEXpkpDO0EYJndqOAYpl0F22oLcbe3kuavqoMtN2VD8UfIB3nEQbLRTTWAXYp0wxWr88caDNvph2TaA5dNtzF0EmNOpLObRxvCA9YxOXN06UAdV0HYhnvOq2qqbCZqDm90TYhSEA9FRVrc4257rreFBhw5iN+VWD5mqQMGM4t1iprWP7LbcTCRhWaRri2kGzD7xYn1zF+1wAQ+zrVkn4e9Ojxsj3oKye/HKFTeszLAcHWtNlzRYkkiy/ppKHZqMelgv+hp5ffKzkgquEWb+ag6dxpnAhIiSTfk3CkJFQ16EcZ4yo4AbM25RJcK2KPKQ+rZz5uupmIf+s4VBCR0L5pHVCe2EQCasGCrOE0cQouBfnZgIMoFv1X6IcaBJbIfRzGQcDj+qyeAi0Acr+tf1wFqbltrctJgH1xBQmtudYLTeL75t7c9y6auAoUEsu0K2Z8r28l+s698wa2Wski4InKJXBpx4GZq44d5M7wsA8LycBN+GO47O7sQrXjsdIq X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7f7626f0-7dc4-426f-1441-08d941734f36 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:15.7753 (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: 9cgIoGoN1kIUd02ou6hr2S0erUi/bG7vjBgIecEhZ7SkT6+H265v9j38rvSCyApa+ccEQwTEBmzZYnGwdQqFsg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=3o2BsnyC; spf=pass (imf12.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.94.59 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: spxkt1n4pc3w9qe8a5o8xbmmdro945wm X-Rspamd-Queue-Id: B09FE10000B4 X-Rspam-User: nil X-HE-Tag: 1625682739-625076 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Tom Lendacky This is the final step in defining the multiple save areas to keep them separate and ensuring proper operation amongst the different types of guests. Update the SEV-ES/SEV-SNP save area to match the APM. This save area will be used for the upcoming SEV-SNP AP Creation NAE event support. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh --- arch/x86/include/asm/svm.h | 66 +++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index f679018685b6..5e72faa00cf2 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -291,7 +291,13 @@ struct sev_es_save_area { struct vmcb_seg ldtr; struct vmcb_seg idtr; struct vmcb_seg tr; - u8 reserved_1[43]; + u64 vmpl0_ssp; + u64 vmpl1_ssp; + u64 vmpl2_ssp; + u64 vmpl3_ssp; + u64 u_cet; + u8 reserved_1[2]; + u8 vmpl; u8 cpl; u8 reserved_2[4]; u64 efer; @@ -304,9 +310,19 @@ struct sev_es_save_area { u64 dr6; u64 rflags; u64 rip; - u8 reserved_4[88]; + u64 dr0; + u64 dr1; + u64 dr2; + u64 dr3; + u64 dr0_addr_mask; + u64 dr1_addr_mask; + u64 dr2_addr_mask; + u64 dr3_addr_mask; + u8 reserved_4[24]; u64 rsp; - u8 reserved_5[24]; + u64 s_cet; + u64 ssp; + u64 isst_addr; u64 rax; u64 star; u64 lstar; @@ -317,7 +333,7 @@ struct sev_es_save_area { u64 sysenter_esp; u64 sysenter_eip; u64 cr2; - u8 reserved_6[32]; + u8 reserved_5[32]; u64 g_pat; u64 dbgctl; u64 br_from; @@ -326,12 +342,12 @@ struct sev_es_save_area { u64 last_excp_to; u8 reserved_7[80]; u32 pkru; - u8 reserved_9[20]; - u64 reserved_10; /* rax already available at 0x01f8 */ + u8 reserved_8[20]; + u64 reserved_9; /* rax already available at 0x01f8 */ u64 rcx; u64 rdx; u64 rbx; - u64 reserved_11; /* rsp already available at 0x01d8 */ + u64 reserved_10; /* rsp already available at 0x01d8 */ u64 rbp; u64 rsi; u64 rdi; @@ -343,16 +359,34 @@ struct sev_es_save_area { u64 r13; u64 r14; u64 r15; - u8 reserved_12[16]; - u64 sw_exit_code; - u64 sw_exit_info_1; - u64 sw_exit_info_2; - u64 sw_scratch; + u8 reserved_11[16]; + u64 guest_exit_info_1; + u64 guest_exit_info_2; + u64 guest_exit_int_info; + u64 guest_nrip; u64 sev_features; - u8 reserved_13[48]; + u64 vintr_ctrl; + u64 guest_exit_code; + u64 virtual_tom; + u64 tlb_id; + u64 pcpu_id; + u64 event_inj; u64 xcr0; - u8 valid_bitmap[16]; - u64 x87_state_gpa; + u8 reserved_12[16]; + + /* Floating point area */ + u64 x87_dp; + u32 mxcsr; + u16 x87_ftw; + u16 x87_fsw; + u16 x87_fcw; + u16 x87_fop; + u16 x87_ds; + u16 x87_cs; + u64 x87_rip; + u8 fpreg_x87[80]; + u8 fpreg_xmm[256]; + u8 fpreg_ymm[256]; } __packed; struct ghcb_save_area { @@ -409,7 +443,7 @@ struct ghcb { #define EXPECTED_VMCB_SAVE_AREA_SIZE 740 #define EXPECTED_GHCB_SAVE_AREA_SIZE 1032 -#define EXPECTED_SEV_ES_SAVE_AREA_SIZE 1032 +#define EXPECTED_SEV_ES_SAVE_AREA_SIZE 1648 #define EXPECTED_VMCB_CONTROL_AREA_SIZE 272 #define EXPECTED_GHCB_SIZE PAGE_SIZE From patchwork Wed Jul 7 18:14: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: 12363467 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,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 2B735C07E95 for ; Wed, 7 Jul 2021 18:32:21 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id B0F7D61CBE for ; Wed, 7 Jul 2021 18:32:20 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org B0F7D61CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9BFF26B007D; Wed, 7 Jul 2021 14:32:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 996B66B007E; Wed, 7 Jul 2021 14:32:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7E75C6B0080; Wed, 7 Jul 2021 14:32:20 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id 4F09B6B007D for ; Wed, 7 Jul 2021 14:32:20 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id A64F6182D93D3 for ; Wed, 7 Jul 2021 18:32:19 +0000 (UTC) X-FDA: 78336636798.01.D51F3E7 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (mail-mw2nam10on2059.outbound.protection.outlook.com [40.107.94.59]) by imf12.hostedemail.com (Postfix) with ESMTP id 55CB710000B4 for ; Wed, 7 Jul 2021 18:32:15 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=h3aCogLOrDSFO/VJ5dsJflzLW4TcYfFTZM9vK3Br9jXY+iozJQMiR4Qb+MsNhryQPt5IA2fwvE8z1YNbnKxW0FNS2/kWw34yTT8NL6ESMFIFPutZ1rZCZJN1jMwumzSrCTmx1vunh8CsmS8ID3znDysaH2E++ik82aBVQnqUiJwe3S6FI/Dj6E0tN9yf89uEuBqgxct8HP3bAoeRzpwCGkYaPwta5G2kTS5J3cVfJ/wRd2yizw30LHMJQKl56MvZ02Vw0XAD4+tLMbSy0pflycy/Fd48n3DAO9lKOkm2GqDkWUJrKpUiV6odqPcuVmED0S1E2lkztDiXK337RQJizQ== 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=RRNoZAyYG2JApB2JG/n/p1ihVwaJ2xs/ineeq3NkLBc=; b=cHx0VjGsV3WK2cf9VKtEyjKd7Twdr26PHp1xhDeH2XzUQEG1Mp+L59gt89i+KWjDEiV01Y1g+4VR6yiQQiclKKon+xjUlaN+89l6PpEucs9NJEreYMwIofy1TPAfHMFapLVDgP7dIMLFzeAgK+xmBFcN3MKAK7TFmq+bTO0mDiKrLo8RvHyYo1u4dMXWTPv40tVMA0APECN85ZWM6QGHu18sS7Gs9pHzQHA8BPknOwlAttyfMGsMB4+kRaSTz7aLS+YFOjoeqNXHuEFbamcKl4PiAHfo0C3zP4V/GoClDfKZDDRuve2mtfxiBY3Unv6mkO0Skq3MTV2rUF3+VBZUAQ== 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=RRNoZAyYG2JApB2JG/n/p1ihVwaJ2xs/ineeq3NkLBc=; b=5JjKiRrlwd5L11sQAzsFqwVZHHastVUIRXg7tc8lFWyJsKr86MJTiolUkFpfrcBIfrBlkuLeuWBAlBewsxF6ZJ/bk4V6yw3GFMDVZGUxYUZFKqUmLKfNTUqO6KKDwkV4I54syNmVcozsVgRT86tM2a383UF1Kow+svrHjBQBLgM= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:20 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:20 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 20/36] x86/sev: Use SEV-SNP AP creation to start secondary CPUs Date: Wed, 7 Jul 2021 13:14:50 -0500 Message-Id: <20210707181506.30489-21-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:16 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3bf782e5-16d4-4b2d-c9cf-08d9417351b0 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: 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: M1LaVBZYobolj7X9sz8FK8ibPTOhakR9GQoZXZt1lpDFx5QsZTVU/WS8FESMBu3QY0CXB6R8uQ28bZXV3z6EeRqMOqqpSlw5Zj+C50YlpN4f56TF+/fNWOxINLApZP2R9rnJBwyUrNRtP28DqgpMdHgFUWgMSiscYlh3g98MZElrlL/maBQSFEGfairRteZLomFCgbOeN4VWMBConJ0O5nBGjWXMQdODFGjMoObq3+XOPOu5MM/VcsBbN9lLJ/SxDu8XuZ6MyHMhiw1wqjNc0S4GDkJw4n7FAV7Hb/MGkqub9Mp6MFX2vCvXcFv24l45asO1GoUms/hRtfet8AJnkZ+gOtv02jYPFjGhxkkHedc9K0BF5lXZKWTgHESTjc5MToGA/WODjiq+wg0YrgNZXYshi+nC+RetrDirA5PQOEWlK8gQ4LKckKNcgU33H8MZ0DC21SBekkrVy4YkGtzZAvUvdyfMjkqGNa7PxydeWH57n4myX0nrOjXVcOz74doVNEU0fXAzlk43UwXUpoHG0ElW4DGdsZyVL/fWbvH8QwQ/3VTOOHIWRPN/oeZtjcbvN5RZsYq9ddQQ0bXP7q1jNiCZ91UoRXBIk0dMPERgIK8jAyEmLxCT+K+F6+7p6xrIuxYSAWInNTVhqa15DSrlvtEdmU6SS9tDwUaRMzl53pRuI0ICtrQn1Mrkd5KjNWzZxjLV8pEWQ/MIcF9WLr435Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002)(30864003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: AlKeBJMb8jGBrVHNKLJmyMwW99ycvs0oQ5LQOKT1ApOsn3mEuqmi4eQG2Cp19F1QFdV13opomKJrT/nCZ4XYroiSPd/OnzquY0PZvmUWksAoHYYwXjGSu1MWoMnjW8k59Udgaq+j84k6arzFBuUDCQZ/kyTUSJvog2xDwpIIf/yBKIXyUXuy9/8bpajEWTsfT+chJ3XBp7H09vu2fH0/s0WiudZtj5KNy7VqqDkiKVlZvwudKosYqmzDAIE/zSmwhRmXvFFKxeaa4UX7lndHYFVDGSk/GrKyyxPnoJ14yRC/rPZwY+qXz6A5x3FV4j5S1QH/caADEe9jFEfBXc2ZRiB/MpDPp3v2v0Rwfc7NTYM4qpM8O8AWHtCxomEJKKQx60j/hcKixaaV0NXi5RSzuFCcaJaQdnclBd5wPYpSRUYUZ6ntg7L1NUPbMi/ih3PDOmrmANU16eBUcHDA5mYONXOssrsaE4meEDtNmBNSJpnRZ+xdzaVbG4n7CLB0/Bkxl3rGbj2JZcw2mSkid/ONkucCbDl6hwSDFmrr0UT6bjzr2hp1jDO7NkYl84rdriQob/NWmgHD1Elr1hz8CNV61cLx6m54PcySRFjfdYbIkcnoMjpPlpLkkVzbq64tR5RoCdPcCaR06el6JAcx8TGt+G/7gYv8riyTQSeT9NyeltJHLPobnrSZ7GgEvVZBdMsnVuV/5woannIZd2WGQJMCluokccDlu4SnJ2tlTgCjTsELNj8TutfNkjD8QAdgVyUanrJYCkCBBGwzFxv11tXHyCN74rPqdRD1sIEcQG5saP+HLTmmMlWUwdkcSkuaOE8v26JJQDAh6qb7F3g4yMa4w4lbtN70A3aeXWrXUmnB1NHiLirVbXmuo9p9iMYvfuWVU/ycMRkw+PnTkG8nrOSMLXskl6uXgk27Zd656NQ768Z2AwPZAySLkEGTtv9MePm0pmdXeWOgGcP4Cteb0M+K8iePLW+5152jAkk1jLWjKc8tNyR9KGIP/T7df13iX8InthSho5qUUhy/m5TGurDRmP+YtsUkQVUbSqIdqLYtL9sV7K8rn3IhkGdk98k1gUEuuzT5hTwFdh3+MTbulH560+SvLM6E0hzkShO2X+VRFC/8Vvdp55lo8If083fSp2vljnrm+0VenKa4K0ZZyeBKE/td/pv3FT0OqMSjiGh+9b4LawYwkeQsGABihQK9XfDWXb0VGyIPKiSEVZKO3umQJ4MjPTpQmToDcio7KBgxC1ZrLGw8rgkQpMwzcZZ8dJHgDLPeH7d5/6GziCeaWluhGqw03ZMAdw2WyTSoqlpF009j9BuIlhhw5hmaAH6ALu4k X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3bf782e5-16d4-4b2d-c9cf-08d9417351b0 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:19.9839 (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: wHRKfw2+Dkjdi11KsHyr0bnwL8bDrjAYbaA0UuuLoSympg24veucoP7FolYU2H0Rwl9nkfc0MLm75VCURj2zSA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=5JjKiRrl; spf=pass (imf12.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.94.59 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: frg6qxc1hbfxgkgst8cb1ru1g99ir181 X-Rspamd-Queue-Id: 55CB710000B4 X-Rspam-User: nil X-HE-Tag: 1625682735-490295 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Tom Lendacky To provide a more secure way to start APs under SEV-SNP, use the SEV-SNP AP Creation NAE event. This allows for guest control over the AP register state rather than trusting the hypervisor with the SEV-ES Jump Table address. During native_smp_prepare_cpus(), invoke an SEV-SNP function that, if SEV-SNP is active, will set/override apic->wakeup_secondary_cpu. This will allow the SEV-SNP AP Creation NAE event method to be used to boot the APs. As a result of installing the override when SEV-SNP is active, this method of starting the APs becomes the required method. The override function will fail to start the AP if the hypervisor does not have support for AP creation. Signed-off-by: Tom Lendacky Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev-common.h | 1 + arch/x86/include/asm/sev.h | 6 + arch/x86/include/uapi/asm/svm.h | 5 + arch/x86/kernel/sev.c | 205 ++++++++++++++++++++++++++++++ arch/x86/kernel/smpboot.c | 3 + 5 files changed, 220 insertions(+) diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 2277c8085b13..5da5f5147623 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -85,6 +85,7 @@ (((unsigned long)((v) >> GHCB_MSR_HV_FT_POS) & GHCB_MSR_HV_FT_MASK)) #define GHCB_HV_FT_SNP BIT_ULL(0) +#define GHCB_HV_FT_SNP_AP_CREATION (BIT_ULL(1) | GHCB_HV_FT_SNP) /* SNP Page State Change NAE event */ #define VMGEXIT_PSC_MAX_ENTRY 253 diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index 2385651c810e..f68c9e2c3851 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -65,6 +65,8 @@ extern bool handle_vc_boot_ghcb(struct pt_regs *regs); /* RMP page size */ #define RMP_PG_SIZE_4K 0 +#define RMPADJUST_VMSA_PAGE_BIT BIT(16) + #ifdef CONFIG_AMD_MEM_ENCRYPT extern struct static_key_false sev_es_enable_key; extern void __sev_es_ist_enter(struct pt_regs *regs); @@ -111,6 +113,8 @@ void __init early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op); void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); void snp_set_memory_private(unsigned long vaddr, unsigned int npages); +void snp_set_wakeup_secondary_cpu(void); + #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -125,6 +129,8 @@ early_snp_set_memory_shared(unsigned long vaddr, unsigned long paddr, unsigned i static inline void __init snp_prep_memory(unsigned long paddr, unsigned int sz, int op) { } static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npages) { } static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } +static inline void snp_set_wakeup_secondary_cpu(void) { } + #endif #endif diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index f7f65febff70..997918f0a89a 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -109,6 +109,10 @@ #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1 #define SVM_VMGEXIT_PSC 0x80000010 +#define SVM_VMGEXIT_AP_CREATION 0x80000013 +#define SVM_VMGEXIT_AP_CREATE_ON_INIT 0 +#define SVM_VMGEXIT_AP_CREATE 1 +#define SVM_VMGEXIT_AP_DESTROY 2 #define SVM_VMGEXIT_HYPERVISOR_FEATURES 0x8000fffd #define SVM_VMGEXIT_UNSUPPORTED_EVENT 0x8000ffff @@ -218,6 +222,7 @@ { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ { SVM_VMGEXIT_PSC, "vmgexit_page_state_change" }, \ + { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ { SVM_VMGEXIT_HYPERVISOR_FEATURES, "vmgexit_hypervisor_feature" }, \ { SVM_EXIT_ERR, "invalid_guest_state" } diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 5fef7fc46282..59e0dd04cb02 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -30,6 +31,7 @@ #include #include #include +#include #include "sev-internal.h" @@ -108,6 +110,8 @@ DEFINE_STATIC_KEY_FALSE(sev_es_enable_key); /* Bitmap of SEV features supported by the hypervisor */ EXPORT_SYMBOL(sev_hv_features); +static DEFINE_PER_CPU(struct sev_es_save_area *, snp_vmsa); + /* Needed in vc_early_forward_exception */ void do_early_exception(struct pt_regs *regs, int trapnr); @@ -854,6 +858,207 @@ void snp_set_memory_private(unsigned long vaddr, unsigned int npages) pvalidate_pages(vaddr, npages, 1); } +static int vmsa_rmpadjust(void *va, bool vmsa) +{ + u64 attrs; + int err; + + /* + * The RMPADJUST instruction is used to set or clear the VMSA bit for + * a page. A change to the VMSA bit is only performed when running + * at VMPL0 and is ignored at other VMPL levels. If too low of a target + * VMPL level is specified, the instruction can succeed without changing + * the VMSA bit should the kernel not be in VMPL0. Using a target VMPL + * level of 1 will return a FAIL_PERMISSION error if the kernel is not + * at VMPL0, thus ensuring that the VMSA bit has been properly set when + * no error is returned. + */ + attrs = 1; + if (vmsa) + attrs |= RMPADJUST_VMSA_PAGE_BIT; + + /* Instruction mnemonic supported in binutils versions v2.36 and later */ + asm volatile (".byte 0xf3,0x0f,0x01,0xfe\n\t" + : "=a" (err) + : "a" (va), "c" (RMP_PG_SIZE_4K), "d" (attrs) + : "memory", "cc"); + + return err; +} + +#define __ATTR_BASE (SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK) +#define INIT_CS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_READ_MASK | SVM_SELECTOR_CODE_MASK) +#define INIT_DS_ATTRIBS (__ATTR_BASE | SVM_SELECTOR_WRITE_MASK) + +#define INIT_LDTR_ATTRIBS (SVM_SELECTOR_P_MASK | 2) +#define INIT_TR_ATTRIBS (SVM_SELECTOR_P_MASK | 3) + +static int wakeup_cpu_via_vmgexit(int apic_id, unsigned long start_ip) +{ + struct sev_es_save_area *cur_vmsa, *vmsa; + struct ghcb_state state; + unsigned long flags; + struct ghcb *ghcb; + int cpu, err, ret; + u8 sipi_vector; + u64 cr4; + + if ((sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION) != GHCB_HV_FT_SNP_AP_CREATION) + return -EOPNOTSUPP; + + /* + * Verify the desired start IP against the known trampoline start IP + * to catch any future new trampolines that may be introduced that + * would require a new protected guest entry point. + */ + if (WARN_ONCE(start_ip != real_mode_header->trampoline_start, + "unsupported SEV-SNP start_ip: %lx\n", start_ip)) + return -EINVAL; + + /* Override start_ip with known protected guest start IP */ + start_ip = real_mode_header->sev_es_trampoline_start; + + /* Find the logical CPU for the APIC ID */ + for_each_present_cpu(cpu) { + if (arch_match_cpu_phys_id(cpu, apic_id)) + break; + } + if (cpu >= nr_cpu_ids) + return -EINVAL; + + cur_vmsa = per_cpu(snp_vmsa, cpu); + + /* + * A new VMSA is created each time because there is no guarantee that + * the current VMSA is the kernels or that the vCPU is not running. If + * an attempt was done to use the current VMSA with a running vCPU, a + * #VMEXIT of that vCPU would wipe out all of the settings being done + * here. + */ + vmsa = (struct sev_es_save_area *)get_zeroed_page(GFP_KERNEL); + if (!vmsa) + return -ENOMEM; + + /* CR4 should maintain the MCE value */ + cr4 = native_read_cr4() & ~X86_CR4_MCE; + + /* Set the CS value based on the start_ip converted to a SIPI vector */ + sipi_vector = (start_ip >> 12); + vmsa->cs.base = sipi_vector << 12; + vmsa->cs.limit = 0xffff; + vmsa->cs.attrib = INIT_CS_ATTRIBS; + vmsa->cs.selector = sipi_vector << 8; + + /* Set the RIP value based on start_ip */ + vmsa->rip = start_ip & 0xfff; + + /* Set VMSA entries to the INIT values as documented in the APM */ + vmsa->ds.limit = 0xffff; + vmsa->ds.attrib = INIT_DS_ATTRIBS; + vmsa->es = vmsa->ds; + vmsa->fs = vmsa->ds; + vmsa->gs = vmsa->ds; + vmsa->ss = vmsa->ds; + + vmsa->gdtr.limit = 0xffff; + vmsa->ldtr.limit = 0xffff; + vmsa->ldtr.attrib = INIT_LDTR_ATTRIBS; + vmsa->idtr.limit = 0xffff; + vmsa->tr.limit = 0xffff; + vmsa->tr.attrib = INIT_TR_ATTRIBS; + + vmsa->efer = 0x1000; /* Must set SVME bit */ + vmsa->cr4 = cr4; + vmsa->cr0 = 0x60000010; + vmsa->dr7 = 0x400; + vmsa->dr6 = 0xffff0ff0; + vmsa->rflags = 0x2; + vmsa->g_pat = 0x0007040600070406ULL; + vmsa->xcr0 = 0x1; + vmsa->mxcsr = 0x1f80; + vmsa->x87_ftw = 0x5555; + vmsa->x87_fcw = 0x0040; + + /* + * Set the SNP-specific fields for this VMSA: + * VMPL level + * SEV_FEATURES (matches the SEV STATUS MSR right shifted 2 bits) + */ + vmsa->vmpl = 0; + vmsa->sev_features = sev_status >> 2; + + /* Switch the page over to a VMSA page now that it is initialized */ + ret = vmsa_rmpadjust(vmsa, true); + if (ret) { + pr_err("set VMSA page failed (%u)\n", ret); + free_page((unsigned long)vmsa); + + return -EINVAL; + } + + /* Issue VMGEXIT AP Creation NAE event */ + local_irq_save(flags); + + ghcb = __sev_get_ghcb(&state); + + vc_ghcb_invalidate(ghcb); + ghcb_set_rax(ghcb, vmsa->sev_features); + ghcb_set_sw_exit_code(ghcb, SVM_VMGEXIT_AP_CREATION); + ghcb_set_sw_exit_info_1(ghcb, ((u64)apic_id << 32) | SVM_VMGEXIT_AP_CREATE); + ghcb_set_sw_exit_info_2(ghcb, __pa(vmsa)); + + sev_es_wr_ghcb_msr(__pa(ghcb)); + VMGEXIT(); + + if (!ghcb_sw_exit_info_1_is_valid(ghcb) || + lower_32_bits(ghcb->save.sw_exit_info_1)) { + pr_alert("SNP AP Creation error\n"); + ret = -EINVAL; + } + + __sev_put_ghcb(&state); + + local_irq_restore(flags); + + /* Perform cleanup if there was an error */ + if (ret) { + err = vmsa_rmpadjust(vmsa, false); + if (err) + pr_err("clear VMSA page failed (%u), leaking page\n", err); + else + free_page((unsigned long)vmsa); + + vmsa = NULL; + } + + /* Free up any previous VMSA page */ + if (cur_vmsa) { + err = vmsa_rmpadjust(cur_vmsa, false); + if (err) + pr_err("clear VMSA page failed (%u), leaking page\n", err); + else + free_page((unsigned long)cur_vmsa); + } + + /* Record the current VMSA page */ + per_cpu(snp_vmsa, cpu) = vmsa; + + return ret; +} + +void snp_set_wakeup_secondary_cpu(void) +{ + if (!sev_feature_enabled(SEV_SNP)) + return; + + /* + * Always set this override if SEV-SNP is enabled. This makes it the + * required method to start APs under SEV-SNP. If the hypervisor does + * not support AP creation, then no APs will be started. + */ + apic->wakeup_secondary_cpu = wakeup_cpu_via_vmgexit; +} + int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { u16 startup_cs, startup_ip; diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 9320285a5e29..4fc07006f7f8 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -82,6 +82,7 @@ #include #include #include +#include #ifdef CONFIG_ACPI_CPPC_LIB #include @@ -1377,6 +1378,8 @@ void __init native_smp_prepare_cpus(unsigned int max_cpus) smp_quirk_init_udelay(); speculative_store_bypass_ht_init(); + + snp_set_wakeup_secondary_cpu(); } void arch_thaw_secondary_cpus_begin(void) From patchwork Wed Jul 7 18:14: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: 12363375 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,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 9F10EC07E95 for ; Wed, 7 Jul 2021 18:16:35 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5038761CB0 for ; Wed, 7 Jul 2021 18:16:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5038761CB0 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2809F6B006C; Wed, 7 Jul 2021 14:16:35 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 257756B0070; Wed, 7 Jul 2021 14:16:35 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0AB676B0071; Wed, 7 Jul 2021 14:16:35 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id D62066B006C for ; Wed, 7 Jul 2021 14:16:34 -0400 (EDT) Received: from smtpin40.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 4736723E57 for ; Wed, 7 Jul 2021 18:16:34 +0000 (UTC) X-FDA: 78336597108.40.C6DFA8E Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2045.outbound.protection.outlook.com [40.107.237.45]) by imf17.hostedemail.com (Postfix) with ESMTP id 83621F002C83 for ; Wed, 7 Jul 2021 18:16:27 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QHdhl9ptc/8r1x5fIIkIsryWS7GfKLbKbTs6iVWLP9pNE6+ISTqWtBSpH4NiHiZ6PgoZc64t7YRg7Zr487tuIJKQNl/w0pQNgUUJ6jMaX3IWcNJCxnsiXWGa/GptbI6788JdM6MfSLgYQLzs2sv1rDNytE3nju8GYBmAk1Cn4UkAsXV1ArWWDz3fEfu7jT27ZK34Jvnnesr6w8GcS+KGuawSaCczlZBao8Lt7CkShyQTpD+G4qCmJg6MY1H50q65iLTgiDVNyKhJpAn3bLCnn5RvdyIxOAdl3knJxN8OECvDLfr4o0ThSmNHuEswvYoki0Q9vLHOVIzL86gD7x6NeQ== 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=fx2WDBdjGaBF/g6x3agK+7pQ+o9Ag8h7WcyLfaEPrfQ=; b=GiGE7epdlK9IW5SQWb3Ar4TR88+QZV2vmtu5CL6UiWHFhtKdjc/27yS1BQCdEDbqeteYq1dGNN2GSIFI+H3LFmnovRLirhc9gIn6n6NgIfvP65FpuNFpyNuqJp6u3MqTEeN0RLfuLwBQAAzCkSfZAUsKk5UMoj2e+H61ujfMB2jcYAf+e6YhveuusUg33dgf7+OoezcnH45WwIbZbOembhZxWXld7iF+quRJY9QCh8bpwFQdoFaq052T8xQxQHkqSP25fT+LScpuBKu56MnOf7AzTRmfYjQKxUcHQ2suSufGgWAcMNcSAkeR/txSzYCm5K6/vcWjhF8TImkPZB4feA== 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=fx2WDBdjGaBF/g6x3agK+7pQ+o9Ag8h7WcyLfaEPrfQ=; b=NjOBBCtyjfX4X0LsD7Mdc2VDsL9qsplL74aaFOahWim2QmQZEesyE1UXDMxLg4WzpXehf6gd7O+xIA7CWzTVHLljuDpaIfD+oMevb9/Ry9lHLpmhVHmTWCnzpv0xKtell9fyXxowX4elwwWyaV/AchBSFCUNpbib5VNuhKsSMBw= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:22 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:22 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 21/36] x86/head/64: set up a startup %gs for stack protector Date: Wed, 7 Jul 2021 13:14:51 -0500 Message-Id: <20210707181506.30489-22-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:20 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a99c028e-c169-415f-c418-08d94173533b X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: 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: BtGAYpi3IPs5rPY1/y/t4LiGBmh5rbAIfJG4NHbXRzT5XD/E+n4pGPO6mDXg8dJqTMnUFDhOxGq15wBX36JWXpIy6v9VEsvnWSL59mwQ8LwTuBn+3ufhUvxiUn/xuIBf9MEccUM76pKjOGUf21WnM9NaJyJUoYxJNaZEBK/iT013Ur2cIfoQ8fWKs9nZZuPHivw5P6U4a6G/z9pAUvs3FzGF58AIZ69oDx2m/X5n0Ee8XvIhPr2IX22FYR6nVp0gYVymg41/fP9nUTEaOMkmO6XxCPmzarT5HRnymD+SnVfpp189IBmsxl+G/loWzqivUYKGrjoikaN+DvGtOgcd5/6JiQyjakQOxGvO4fifrlwz9UMDVDOi6wh2jRt04lQKfV7QShs5RmmHeiJMKkarY0J5plTw4yEXZB1lCgF0ruJ444H5CsalossixGtmGmeieTF0mYtEXCCGEup0G2YWvajfK6mc7lERH0kEucB2QeMdAoWRnedCDKquDsKCPmNtG+GJMznBhig5l8APvS+mSkSMbBs7nsn56HkccGf4I1ccZ4H+YJZygCOM2jiwg7uPJZqEj3Gi95dOoHP0gam1jOH50rnHfY3oeBG1AM4VR1r4t589AI3L84+YoHYFYiubVVDPqdod/rC7+jqPpD9p1yPtvc4GVD6fAUSrCAtyjFxXMktU8ty1hABqKTlnGgG7CXcieG9wb4S5lR0cdfZkfg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: N87hNKigKyD5zHjB1BXm9lUrGOXkKm1SPKK8w6EG5HTFOJJ9ulyiSJ5WK7+Q7s46GfJTxehXrBAqk9q354l4DZNIDRLjxcnqG3NnLZRYV6+SeKypQfAQ/EtsKYZ71gdH2ZqskPI95Lp17ZqlR1pIsCewtm6ogxQFtYZbpNSqGrNU1iN2paFL3VnsyqSmvFxl0GTGnC7QK6SCmqsurSpnaxEfZwBfyV5fo7Xz0VfNOW+Xc3LLlDx8MJ8TqJCQc674VLz2w6vbmLkNL9YlUWvk11cIK2L4tpt5V66wpdPNoul++0R/eSdr6dBFJoR5p5/YkqflK4nNIRufok2MeWWwG7Eap+DtuUo8+z69soCAI+74QkQ/pafw2DQADdAxV6f41iFU0vkqxAhJAQnOaO6ckOTe/AFBdgNj/K17pznGV99P+ynu4m9/zDzL9hpz/Anwrw7OjfGSoUjKgx599uuMvtYCOQp7Q6SOCdFR5Hhxztwl5GixP0qDWty1CJPpV8qi5zp4yIcCFCwDSiAbr7gM/Di13ddAPFU2VE2Ci2gAGnSKh4XIqxohWIhrzg21Ci4DVZg04+vX2+M9gM2oPO/cT1t0OZJpbCkhiqZpygGIY8RW5SbgRUdt6iU+jBS6LUoa+Djm+F3ssh0a3P/bt1UKDRWRWzjQ7l5zpszxdy8cC5i/sCI0q/Rw2mnX7wAGG+uk0YKoHqurIrZhtLvoJGsRtGAlHP7/cHmteODhkbOfayp+oPKQKuG6ag1gHdYLBXLy4vkVF/C+fz+5qiDJU8hSJB9qcWZ99Ldp+XAGdaW65RPnTAwP9obIfl64g+EeU/k+mbu6GOFg8bKgdw+Mz+2y13U+PE4DlstPp+xb9FykHIH/20ksHzknXSQ6tNY6JkE2vuXl771eFNZ4QtDAFxd5Gn5CQwHn7DD6OLfvUy9A+I+vNywFU8bwximX443hETU3K3QN18kHCAEjjwkIilxKKGe3KDxi9iaWjqB6/4aJY1Pxy2y1hHdMmjJcpuiKihEErktexSt+eFgPU0ldP2QSbbU3CW7ej34P2rzZ9iSbJCPXqrd1LQXMl8at8L+tFGDspVnA7WumACAQmi31AlOAurdw4t2PPy6t0cmcewBz70gUwg83Ld8G57UsOhEGSFulVPPzxCmCL6tqee6ADUNUk3QpQ5zzCW//idIcjeRKk/Jp5BXxQG4AKoMKP+GuIzu+bAFfGxUCo+8WJ170Ry9/UlWIKM9F5NHnZ0n1j6wx8hEr4B/x6BozK0lu7Ki+0tLi/VO6qqloqTC2OiHT4AeOAu7UGfY2nMpbBCCsnh0sRfpWofCDYtBAKo3TcS0jSwdj X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a99c028e-c169-415f-c418-08d94173533b X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:22.5174 (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: +9S9IF+jTOfvYLIhDNYYsKdOB10i8nOu8Ua0mTsH02HBgyuWahmBl8n/r06L2Bj3xVHsP11iB+0S6SZaqrhcnA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=NjOBBCty; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf17.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 83621F002C83 X-Stat-Signature: z9r8gh9jo5g1aubjeebr1xxd755tuq6k X-HE-Tag: 1625681787-327471 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth As of commit 103a4908ad4d ("x86/head/64: Disable stack protection for head$(BITS).o") kernel/head64.c is compiled with -fno-stack-protector to allow a call to set_bringup_idt_handler(), which would otherwise have stack protection enabled with CONFIG_STACKPROTECTOR_STRONG. While sufficient for that case, this will still cause issues if we attempt to call out to any external functions that were compiled with stack protection enabled that in-turn make stack-protected calls, or if the exception handlers set up by set_bringup_idt_handler() make calls to stack-protected functions. Subsequent patches for SEV-SNP CPUID validation support will introduce both such cases. Attempting to disable stack protection for everything in scope to address that is prohibitive since much of the code, like SEV-ES #VC handler, is shared code that remains in use after boot and could benefit from having stack protection enabled. Attempting to inline calls is brittle and can quickly balloon out to library/helper code where that's not really an option. Instead, set up %gs to point a buffer that stack protector can use for canary values when needed. In doing so, it's likely we can stop using -no-stack-protector for head64.c, but that hasn't been tested yet, and head32.c would need a similar solution to be safe, so that is left as a potential follow-up. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/kernel/head64.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index f4c3e632345a..8615418f98f1 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -74,6 +74,9 @@ static struct desc_struct startup_gdt[GDT_ENTRIES] = { [GDT_ENTRY_KERNEL_DS] = GDT_ENTRY_INIT(0xc093, 0, 0xfffff), }; +/* For use by stack protector code before switching to virtual addresses */ +static char startup_gs_area[64]; + /* * Address needs to be set at runtime because it references the startup_gdt * while the kernel still uses a direct mapping. @@ -598,6 +601,8 @@ void early_setup_idt(void) */ void __head startup_64_setup_env(unsigned long physbase) { + u64 gs_area = (u64)fixup_pointer(startup_gs_area, physbase); + /* Load GDT */ startup_gdt_descr.address = (unsigned long)fixup_pointer(startup_gdt, physbase); native_load_gdt(&startup_gdt_descr); @@ -605,7 +610,18 @@ void __head startup_64_setup_env(unsigned long physbase) /* New GDT is live - reload data segment registers */ asm volatile("movl %%eax, %%ds\n" "movl %%eax, %%ss\n" - "movl %%eax, %%es\n" : : "a"(__KERNEL_DS) : "memory"); + "movl %%eax, %%es\n" + "movl %%eax, %%gs\n" : : "a"(__KERNEL_DS) : "memory"); + + /* + * GCC stack protection needs a place to store canary values. The + * default is %gs:0x28, which is what the kernel currently uses. + * Point GS base to a buffer that can be used for this purpose. + * Note that newer GCCs now allow this location to be configured, + * so if we change from the default in the future we need to ensure + * that this buffer overlaps whatever address ends up being used. + */ + native_wrmsr(MSR_GS_BASE, gs_area, gs_area >> 32); startup_64_load_idt(physbase); } From patchwork Wed Jul 7 18:14: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: 12363377 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,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 DD4C6C07E95 for ; Wed, 7 Jul 2021 18:16:40 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 772BE61C83 for ; Wed, 7 Jul 2021 18:16:40 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 772BE61C83 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 62A8F6B0070; Wed, 7 Jul 2021 14:16:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E5A46B0071; Wed, 7 Jul 2021 14:16:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 42C516B0072; Wed, 7 Jul 2021 14:16:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0030.hostedemail.com [216.40.44.30]) by kanga.kvack.org (Postfix) with ESMTP id 1E26D6B0070 for ; Wed, 7 Jul 2021 14:16:40 -0400 (EDT) Received: from smtpin31.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 71D7980BF4C9 for ; Wed, 7 Jul 2021 18:16:39 +0000 (UTC) X-FDA: 78336597318.31.5E33104 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2045.outbound.protection.outlook.com [40.107.237.45]) by imf17.hostedemail.com (Postfix) with ESMTP id 60FA7F002C83 for ; Wed, 7 Jul 2021 18:16:34 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=eQFg7N+9ZwB0qTppJgBUiGxPOQ15QA4i7y592w/LD1e2aOKAUoW1+jFro9nYO9crW1wJsjfLHrNQ9hCMxaRDozzCFHT5F+FgrzKLzGUyUgJpbz5ruif+EI0f9OwJ7/Id6p3667BbWBCDihgZuBo9AHj8C2sQa0I2skzsk8CAeVCMUe6TYZv6nQChnwQv1VC46/cA/YietEUn1WZI4uV5ILQLZj2eb3rPpVQ4Z+HwlZxIy95ab1k8b7MEHPDGq6bWW4mQKDRvzi6lEgT3uFeHL7BBTDUEiASbOj8HG7H9/UDBZRa70AeFgMU/ZGgR5OgzYnqyvV10I/X15wbP1l1NBA== 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=KiWbdxaVYo6yGflzsIqUHjYzBRDbDTO0jVJIW4bZPI8=; b=ZWGR+zKgjQ20vc6TJ753gQnnpGmm1hLeFhxx3UYl85GL+3SPjlokSivbOQ8D/7uQYV1+VXtE+HMUVX5k5lwevOO2epKW2KhETl6IptcL7KXC1phtUI9FVCu8FKtukR7bNnDqi2MTNS9fwtHkJeUwS+lLrlJovT4eO3tY69RqM04dA35jT3B8NJEalNS2mhVBQGU8IuU9Hdjke+MxBW6WHa3Q0cnD26LW9KVQEWcKBIk/eNwea5ZAJebWv32FWDC8tGSxcmpT2h81EzxzBOACXN/EhEoGKQoac3juyDS46gDXYwbW5ciuhoxfSMtYfN5eer8W3KRwP1FSgezbvAMgIw== 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=KiWbdxaVYo6yGflzsIqUHjYzBRDbDTO0jVJIW4bZPI8=; b=W/kyHlDG+VmeMGDw1FvPpYG9eoo7VO9LdCMyyUsi/oNBBsLpNA2UeoA/XiwT0nD+3NGUHyysx9aF8pBHq0eNUWaxVLmOE9UMFNgdXhlS7pIPCnmGrtw6OC4tND4vkf0ZyPWoM0WvQm7gWQ8pvSwB2g8qNvkvhtiDjqInqHipV3k= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:25 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:25 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 22/36] x86/sev: move MSR-based VMGEXITs for CPUID to helper Date: Wed, 7 Jul 2021 13:14:52 -0500 Message-Id: <20210707181506.30489-23-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:22 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: dc8400c7-ca67-40fe-f7ee-08d9417354b5 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: 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: V9Q+H6YTvJ+WpgOBZPtX79QGQK3/XVl0ZQqcuZgSpQEZ6LM7xii3rNadK/hFiQvPpH1srUUWBcKxKmKRSPw4a3IpItnnbeosA6ktgdtSaXPP7lujQybetZGVC69doNFRvmuTHYwvMUUWSO7VThs+Zta3ardi8j6Xz1YtCSaPZ2cIxN6j1atDpe9UFObOEaQGyrTJER702aegqGHw2q21Iazqt958EegM8+f9BBdmOJp6WPjxKX5p/iA424B3Q7Y4obL2kSxVxKgNMCxCT+rlU2h+ljX/Yf+pCshJPW/0JkCrW29HunPog00vyCtjTE4dW2bHRgWqc8pVZj9QOzCPJcH/8uwSHtfgACeXnpXTR/9xCQJ/ld5rDdNdDvYSwQsH3hA4lFhkz5dnRKHPevbgm9a3nGadLgRDuc0gEI3PPuopYGkeQHW5uqzbvJvLQVx/bMyJOQsC2B47R9jGBZQcAUxs6LefY3llvoQYdUs4i+3lkKdBR0QM6UlB8N81xGLnc9fbfFpB0vwG3Qzo5siGlyKZ2bEtd4ItS0a1WIzC1q9N4sOuKXjLn7CHXnMxTa9olfp3nlvXvx3toubeyEfXZW1yc/aZIzoSaF/PfIWsMf1UeEWALAGA+vQ+S31FwuMHSiKcHW7+OQ/Sr+GJ4Z8m4vAnjL5TB/dfPc3DFAfWGIgwCS1pHsMmx4ljn2OtcAOPIeJSv60n1YCpgU+KxzVh+g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: kdVRYWQb2kgKLOp+05vvYeZjwySuZnoMe0jkRTMuFJEI1RfqJciWbTa18UAZYx8xbWiLHtEcdO+QwZ/HKbooGAZ4qWE465kE4tvXNfmKgnq2APGw9A7DGyc1PyBTeXfLiqN3JMu8BMKVBDYF/4kgKlNCOiG86yxkdMOM/eoB63mY8cNue6MKeMs0D/imH7YthmzOyWSTenvezVfFbAma5lk4EmFIiJTojCx6kV5yPEvKZGrFAaLDm1FdpUP8acITzuHXRnu8BuNuoeeiVnlFboAWKa+S957SvVjhrJ6iC1v8WZQ8nMTab+BHSgMINeAW8ZUT5LI9BxaSP2mJG3i3YRT6hxX3+iM90SNvxe/2+aiSzzxOl+cm7LM0yj8vCMZPW92iFk2+RjUAT+9rZE1XE+gA/lSOJLn5mRgXbFVlkN+enKHQbT1KVe+3/JWYz+nibdNgxI46KzfFZx3YdFlxn+gUgCHDQBbFvUl/9TYTGgwT0+7cFsnfHImuKoudn3IWc3g4QE2B5nYEa/kSh598sPuJw0KGRHmy0aHJeLCVw2AQU27O0sbj4V1QrKVwAB3cRpEJ5MU8qN++1Os3Ed60i5b+JeP5xCyChlYD7XN/Sj1Him1KRjZcg7rLlBLPoyBDXcWUFyjqR8Ub53tijGfp00qCXZV915xVB3N1nmeXcJqpWL7DVB1NRudzxHSTqi96y0L4MOHSXT2+ssdWOoVBlreK6jqcVbr4Brwu8aYuGLl2Klt7cU8h7N9M3ClT9HwkXxzxpVSGWOK1v70sgYXR27/Z785gmtEMf5ScK5a8U838fWqJBxI0AYwU5bXjnZtY/Q49guZiHGGuWMkSjTwFteRDld8sr5l0mmJmE4FqxP+W5QULt9k2GvGfOi7ECYPV/1SolWZ/A25fZscdet8Ryx3E8nwJzO+vyT9cWHXbSs/Ve3HLnTOw40eW4jAnTDIXamzOQPzusgYU6EzNXDn2tOqd1D6fzYIAMHm7gBwGfCi1Zhpnb+ZhVRTtYTortTeIfSFctI8Vfv3/+WbYNwUlxiWrjhElKZfyxPtsUFxRcN39YGorITJ+TRSoQedMlfVUKcTCpD1wK0jS3aDYFmY+DXUkwgehxTDzDTfZLl/Fu80SYrYcV56UWKin0un040BBSZ7niT+HKHvFhBLPv1QF+fMcnZCgIxf8xvYCIAdQO9ga0fZl9R0JCFCpvZsZYaTBmDLHJycYaKDKQYauo5d4cDoAWsHgoK8smmvf9Z48dbDCFYtSP+dj7BfIb96fSYEQa7uE649hyLGKrUThCXCXuHeADlpCpdkmKCOsnSaDRqppGZjqq2xC0Bz0VZ/NPlc3 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: dc8400c7-ca67-40fe-f7ee-08d9417354b5 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:24.9910 (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: VCJOLzf93tOWxj+RW70W3mYWlEN6uqe81sZERIHN5ewF8e+HyEmUPbU3sAzmSNveJWA9mcnUOQ3iul9e1lbBeA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="W/kyHlDG"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf17.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 60FA7F002C83 X-Stat-Signature: sy9bt3buumwo9r6jmnkkoy3nk5dk6jxw X-HE-Tag: 1625681794-543796 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth This code will also be used later for SEV-SNP-validated CPUID code in some cases, so move it to a common helper. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev-shared.c | 84 +++++++++++++++++++++++++----------- 1 file changed, 58 insertions(+), 26 deletions(-) diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index be4025f14b4f..4884de256a49 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -184,6 +184,58 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, return ret; } +static int sev_es_cpuid_msr_proto(u32 func, u32 subfunc, u32 *eax, u32 *ebx, + u32 *ecx, u32 *edx) +{ + u64 val; + + if (eax) { + sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(func, GHCB_CPUID_REQ_EAX)); + VMGEXIT(); + val = sev_es_rd_ghcb_msr(); + + if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) + return -EIO; + + *eax = (val >> 32); + } + + if (ebx) { + sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(func, GHCB_CPUID_REQ_EBX)); + VMGEXIT(); + val = sev_es_rd_ghcb_msr(); + + if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) + return -EIO; + + *ebx = (val >> 32); + } + + if (ecx) { + sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(func, GHCB_CPUID_REQ_ECX)); + VMGEXIT(); + val = sev_es_rd_ghcb_msr(); + + if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) + return -EIO; + + *ecx = (val >> 32); + } + + if (edx) { + sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(func, GHCB_CPUID_REQ_EDX)); + VMGEXIT(); + val = sev_es_rd_ghcb_msr(); + + if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) + return -EIO; + + *edx = (val >> 32); + } + + return 0; +} + /* * Boot VC Handler - This is the first VC handler during boot, there is no GHCB * page yet, so it only supports the MSR based communication with the @@ -192,39 +244,19 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) { unsigned int fn = lower_bits(regs->ax, 32); - unsigned long val; + u32 eax, ebx, ecx, edx; /* Only CPUID is supported via MSR protocol */ if (exit_code != SVM_EXIT_CPUID) goto fail; - sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EAX)); - VMGEXIT(); - val = sev_es_rd_ghcb_msr(); - if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) + if (sev_es_cpuid_msr_proto(fn, 0, &eax, &ebx, &ecx, &edx)) goto fail; - regs->ax = val >> 32; - sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EBX)); - VMGEXIT(); - val = sev_es_rd_ghcb_msr(); - if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) - goto fail; - regs->bx = val >> 32; - - sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_ECX)); - VMGEXIT(); - val = sev_es_rd_ghcb_msr(); - if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) - goto fail; - regs->cx = val >> 32; - - sev_es_wr_ghcb_msr(GHCB_CPUID_REQ(fn, GHCB_CPUID_REQ_EDX)); - VMGEXIT(); - val = sev_es_rd_ghcb_msr(); - if (GHCB_RESP_CODE(val) != GHCB_MSR_CPUID_RESP) - goto fail; - regs->dx = val >> 32; + regs->ax = eax; + regs->bx = ebx; + regs->cx = ecx; + regs->dx = edx; /* * This is a VC handler and the #VC is only raised when SEV-ES is From patchwork Wed Jul 7 18:14: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: 12363383 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,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 B506BC11F67 for ; Wed, 7 Jul 2021 18:16:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5BC6D61CCB for ; Wed, 7 Jul 2021 18:16:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5BC6D61CCB Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7631D6B0073; Wed, 7 Jul 2021 14:16:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 713836B0074; Wed, 7 Jul 2021 14:16:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 516816B0075; Wed, 7 Jul 2021 14:16:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0179.hostedemail.com [216.40.44.179]) by kanga.kvack.org (Postfix) with ESMTP id 270836B0073 for ; Wed, 7 Jul 2021 14:16:46 -0400 (EDT) Received: from smtpin06.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 7612318423887 for ; Wed, 7 Jul 2021 18:16:45 +0000 (UTC) X-FDA: 78336597570.06.2C3F267 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2045.outbound.protection.outlook.com [40.107.237.45]) by imf17.hostedemail.com (Postfix) with ESMTP id 8BAEDF002CAF for ; Wed, 7 Jul 2021 18:16:39 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=f9FNbJYFh71t8bSuyST1dkbNhxLsA31gRZUCqfpEZ/kz99VTsunWMfuoID3qaf2M0kRUIX9Dv+XR57is9EtU9wDyyczFQaWgPfSJAy0kViueB/ORm/2RfylOrrxqYvpwZgP8w4qpjzpMtdmqpskyglVYbXk8COzfPQ494eqFoH8pB97PuvigvTeh9uMBzD/btgsRkwJ/lLUoOmp+beMbQALbgIlb9d+JY1eBN9muvyTPmMzFu8FufS3oUDWu+K2SgzzAgtoyd+3F91xYuJL0gEEdV+4mzB7D+osUnI2XrGpEqXl0r0MUmLVzvnIa9fZDcV/2U0ngtJoRe4vtUitJJw== 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=hBU5NjKVuUubgJqFZUVr/Woemez+mkT4i2hl+Sgn2jg=; b=idvRn870tbm2jaRmmm0O8yc+xcvsGVzMjnb4FiHIvyy+bMZ6FepyrN0YzL1fOfp+PhNGy93LWeV5slBXSP2S8Tmraxt/2jmRbu1BgMeEZ451OiFTgfQZ2zTS1y+SlMAInaSW4Of0uWnIkdyPoH6B2stjz18kGmv3mEuvObX9ngRsJi7JhllZA76INtuvoV4fR4jZmEM0dXcfXvEb+E/l2dFyPPq/SvVfSfJBQB4YLvHncrkx8PoH6RqsNVlMcwG890Pbsx7Evf1N1ezk6UHdwq3BcUsTa6TCcC7vMaD8XHnCQEFHOsyArtiWQDcTdLTIw/aU3IltL23BedTJ4GxCrg== 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=hBU5NjKVuUubgJqFZUVr/Woemez+mkT4i2hl+Sgn2jg=; b=wPdyUSavU9XuskIMzq6jAj9GSQM7SV27ESHy+9dYjbi9jXJjvlk0XBng6vT3YG6sVwQH0xwyo+cwg+wS3XLfR63PvnwlGx+xRpfG9dMVdYH4ytSEELknvmY+fBbF7qktjEBhxCPELsr3YV3h+2V2IjBU8SR/aUF/eBul7sliIf4= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:28 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:27 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 23/36] KVM: x86: move lookup of indexed CPUID leafs to helper Date: Wed, 7 Jul 2021 13:14:53 -0500 Message-Id: <20210707181506.30489-24-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:25 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3611973b-3c3e-422f-543a-08d94173565b X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2150; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: zsFimDqvzw6lvrhqepEGwsowY5lEme8sZXbuJMpEtnD4nyhx6iGUBpSQRrYSpB5uEaFr47v7ZHjIZnfC5lvTt4W2xD1UNf4KjQmi0XrmPBVUypjreJte/Zdt83o7Y9K0t6gC/CFYKMRFbfi4G5M5Zm5tvJI9QUK2hosbfvBQDEC/WdRlvzdp335uB3tAQ0ontI8I5BzPjLRBqAZ5UmYcpgp5SaAaHJay8+RH+bAThLNC4tQxnIVjFE0H1uZ6JPxPz3CiIeezuoyDasAqJ4KQvmW1O/agFDJbLtJLFWNl+8bglrhGZ6T0nr7h4Jy9fCW9lCWTEy5CtlifTOlCgYYnPY6kZ4TigVa1gF4OhOuEkaGb8cuN9PxYGapGfANeacK67xb9e8U8BDmcGcwHBZzMpKgbD4UkPOZftSZOBcpEIZMCRPEjVMJBW6RN9DPgJZGdSFRf4oW3sZhx1xLTI1zFbLSf36QCXbX73VGcW+CU/60j7I1jv5ATJVzPThrS7L8U8eATapZ+SnqpwMMzvwv0EbuOjX8A16zOz6wmI5LU+hr2+ZEkpIRf056jTemurYpe047hyXOY7OjrzLy/CE7d/VAMaD3u+BFVNi5N0km8NfFpmEtAhkuj88bIsKfHN4gNHMUGu/qujoZbnZ682Fn3+kzZJ/Z+w0DCdgCOGnc8XIFqnF1lHlRzpNDn4SRjp0fy8OpxPtDC76wI1kvcCkZ0tg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QTvbYX/w5rlXMROyhh0WxN1eEtT9qLSak5ILBDgsxbb0J/Haui/haENosXbK20xf4jQ3k4B/EuA+e6m60izrSmpsSF82qgtYoiqGfuEIooCE0mMEY72LCpSLD8MEgB8gkh0pq73FhM8Xbgm5RpDbtuzaRuUm0NvsmkJ43nnRSj8b+VsVqupsPquQAqiaTjVpyukQH2HpCwL2NKWjpz25AlTOWOcmlVi8NH+e5tUExyQhLXONQGQBf9iSH+s8ddpLP1YmTDoBkIDvN0mjtLxWxTWymQPoIaqdeMmfVajm+nEo6P2blyqyhCqehNFG3R8kHSvz8w7BIn1agT+xu6LQkKKcJuPBLneRarojnICajVJh07TtxWQw/aM/cfVNBbQygT9zCd7vOvXcuY39g6s136a3YNoe/mPQgc7/CoX3Qrq7MM20Ze7724Z62BXR0kVsQOgTVy3xepV1LCC/v9uO++vogBUfkhW/44UMFHPyJCytQX+rfXAHXelwfKISxGZY0rtCRcY+kz/jgcZeJ1CrcIkRqvmysgyz7M2WrLriDNyENmA3jAR81PSGblLQRLBqHVHD1knSNlciEvyT/fI5w+tNp5CIAYtKoi+PkLttQbY1BSIYHVo8z5y+F+uuXSrJFM3KU2yCARarJ/ofdTZ0T3V8xNHVC2akJpIX327d83uJGVryJ6uHxHuNItocUgLjOfE56NKFZKY0Apu40dC4HQeP0Mi4Oqv6sympKozzOC2ND2Upco+bKpW7UkU2puopFjLaos3kBX0PoDHG7tz31OMyIBYj+wlcF/e4fMiK6TtDOe/X6dT5zgQ9kQl6nezTVGRoH9gmpwu8/w1VDKm4zCsoLIqI2fo1Y0IuHjfBIGBEHHaUdjOFk1X7023VWSWzJf9eTlLAPIbaRhx/AWOoHGfmdyPu+H1njMY6Wdx2suA8YrDtznDDqaw00NUxFdK+rhdnoBEYa/qBSqi874WedveuP1iTnGysVXpd/Pcg+IbtR9SS6l/UNONyyVeeoNH/2LpKxYHp20SMzxfM+2LvLks0HuyPpEAiw+iWJJEPVc1JWRNIPYseicrmxa1sHuM/+I8sGfwciFVwLn8qX8oaq0e3rdxINbTsDT13fz8nYYP1Zn5pLnA/e7SIiBwCr2n1EwNogiu7oKKR8h06//KAg7kSN0zAxLKp57Y+fa2Z7Ykw3XgBEaRQqSuMBuGBa2BqsPD/nJ4b0gcdd+RmzEGS/OJTSsuDbXlqz6vYdyjk9B5fKsQ0RrRhnyPY3+aGMkau1JiKFdkz4NtHEUYKvAPyeGHUV53DM4QQ4J401dwIRHRGPcNI/fgZQad3zLR2Hzka X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3611973b-3c3e-422f-543a-08d94173565b X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:27.7525 (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: mZcr/bQyTEe2dwmNCkmvsWnL7NF+VoDmtY7ERwvDEcp4m3eE9vFE/ofNKGTeHO+UG78fAqoiL0ceaqlNyey4ag== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=wPdyUSav; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf17.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 8BAEDF002CAF X-Stat-Signature: wq8jte11o7bq9bqkwgduhit8jcj4z1rq X-HE-Tag: 1625681799-422694 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth Determining which CPUID leafs have significant ECX/index values is also needed by guest kernel code when doing SEV-SNP-validated CPUID lookups. Move this to common code to keep future updates in sync. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/include/asm/cpuid-indexed.h | 26 ++++++++++++++++++++++++++ arch/x86/kvm/cpuid.c | 17 ++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) create mode 100644 arch/x86/include/asm/cpuid-indexed.h diff --git a/arch/x86/include/asm/cpuid-indexed.h b/arch/x86/include/asm/cpuid-indexed.h new file mode 100644 index 000000000000..f5ab746f5712 --- /dev/null +++ b/arch/x86/include/asm/cpuid-indexed.h @@ -0,0 +1,26 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_X86_CPUID_INDEXED_H +#define _ASM_X86_CPUID_INDEXED_H + +static __always_inline bool cpuid_function_is_indexed(u32 function) +{ + switch (function) { + case 4: + case 7: + case 0xb: + case 0xd: + case 0xf: + case 0x10: + case 0x12: + case 0x14: + case 0x17: + case 0x18: + case 0x1f: + case 0x8000001d: + return true; + } + + return false; +} + +#endif /* _ASM_X86_CPUID_INDEXED_H */ diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index b4da665bb892..be6b226f50e4 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -19,6 +19,7 @@ #include #include #include +#include #include "cpuid.h" #include "lapic.h" #include "mmu.h" @@ -608,22 +609,8 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array, cpuid_count(entry->function, entry->index, &entry->eax, &entry->ebx, &entry->ecx, &entry->edx); - switch (function) { - case 4: - case 7: - case 0xb: - case 0xd: - case 0xf: - case 0x10: - case 0x12: - case 0x14: - case 0x17: - case 0x18: - case 0x1f: - case 0x8000001d: + if (cpuid_function_is_indexed(function)) entry->flags |= KVM_CPUID_FLAG_SIGNIFCANT_INDEX; - break; - } return entry; } From patchwork Wed Jul 7 18:14: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: 12363387 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,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 1C8A3C11F6C for ; Wed, 7 Jul 2021 18:16:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id AE91F61CB0 for ; Wed, 7 Jul 2021 18:16:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org AE91F61CB0 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9FAB26B0075; Wed, 7 Jul 2021 14:16:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9D13E6B0078; Wed, 7 Jul 2021 14:16:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7FCEF6B007B; Wed, 7 Jul 2021 14:16:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0123.hostedemail.com [216.40.44.123]) by kanga.kvack.org (Postfix) with ESMTP id 525086B0075 for ; Wed, 7 Jul 2021 14:16:52 -0400 (EDT) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 9FFE0183C7659 for ; Wed, 7 Jul 2021 18:16:51 +0000 (UTC) X-FDA: 78336597822.15.4072A92 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2045.outbound.protection.outlook.com [40.107.237.45]) by imf17.hostedemail.com (Postfix) with ESMTP id 97895F002C84 for ; Wed, 7 Jul 2021 18:16:45 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=L5Pj2D442YTzlVGryNSzmu8pFB8xhkV9/GmmGDUG0W5vI9e8yXUpu9/BGY290XqO3xo6SCnu7OlOE7WbdyL0wcn+00betxH3tsK6N51kXDsT1xUMS3I4PU7Q7HZ81F2aKNGTe29jgKObtrmVaN05TcI7kjScoEViwypIDTd2yiRHmbXAA0G/CX7z1a5MVpBByWXNIibECNOUehGhDs0OF/6Ugp9R1JHqJGFrXbDLBYfrmYq9R2edJsGr6pgb1lMOB2W/N/6pcU8CGt0xM4FCVm+vQrtifpUOvL8O5+/G0K3WwVvWt1f7JCEcm/wIvT6LAtqAZrhr0aoW/cotbPx+QQ== 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=IPZMrIjJ8mD/qBfBptD/Ox46LRwQeKqAd9aThHVDvOI=; b=eFulPikaeL5ryWd4dZDNDWSEAenvLq4VmVERpvdb3ebXCbmFdIYwkReSVJAJ3XksknB/LbqBz47McrROIn5O/01m9KpxcQcbMZX3X8qjZAlgLyhBK16a3ICYjA86l6tgloaJYhXLgdBx1QhTIK9KkuOouOPIcNuPsGmw1eZH0FHhWdPSsTbjpzzKWu8XabF4p/CNuUBMF6YL2hO0HYPpSVolrmp90X8RUo6mU+VsnoT9RrvGtAx2GEcRrwNeVNBWYEqkdfDcl1sekVW+PAXWVVpCoZ+mT4FgYRGvq8p8+iOPNSFzY0pNsoxZe9/GYtaGjk9s1XKV6n16OdKcbKE47w== 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=IPZMrIjJ8mD/qBfBptD/Ox46LRwQeKqAd9aThHVDvOI=; b=zJwH49CTTjV37zbV6b4hTeP2mWH5lFwd+yj/kDB4c9ICN8fMvOlZ4/WsB6zvT//yJfOOgIEQV0YVaEnB6QbrXM0vcoj8Pe8kM1cFNjP30WLk+LyfuBUXjU72YoSrn4CrM2enAA0FhyadazSQeiCSk6NAJHZT0JlJThkmZ2vf/dg= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB5016.namprd12.prod.outlook.com (2603:10b6:a03:1c5::20) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:30 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:30 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 24/36] x86/compressed/acpi: move EFI config table access to common code Date: Wed, 7 Jul 2021 13:14:54 -0500 Message-Id: <20210707181506.30489-25-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:28 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 40d8fd6a-95f5-427d-da9b-08d9417357d3 X-MS-TrafficTypeDiagnostic: BY5PR12MB5016: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2276; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: FligQslX/nqSLIKKK7St2SjdBqNYPSOCQWP/f5fVXk0alBCLENoZxgex7gDXpxOmehTCTeMYzNUcLB4aHYrXTjMcqc3J3pup7xgfV2lqiczOq9V2YZ27i1p7JsZEsizOH2vuT94MfgveMoZL45G80h9vBYr9TftQl/WVXsYsT/uwvF1nmdYMzm7OPdGgY4E8I8uZuAJwfS02sf16yRVGbxn+9fOFjwHKvGhSA6IKJPi9Muqa4CtM9TV4QOhhw/xoT55UsLC7R5XgbUawGr3MufqTZRvCe8KY9Ll6PUM6DkUSBmf7cJEB0PhsyAFmZmNQq2MPPNa7MmOI1BXHmup4fIY2JwpYUYIueZcr/hseMZkJGG9TsdQxG7js7XYR9iwSFIJLkFdxcfT+2LEmU5XusTl5Mn5yLE9yqFcL1NuNjFkn5tM66sPbZ8q3LCyVcNGVID6uP38TVjAt7leHpxhUwJAjfE47MtXFSGBmDp6bUT9Bl36RloJiHVREHf/CQeUaA6xP74HhDVQ4geZ5rHliPgUZjrTsEczAQN+QuS5VtA1/iqAcMR0L+cM0RtMBVZe6g21laFaf97tAUTGxWu6OnoEmqCG2QuECnrYOs5Aqa7QcwhSf0IiCB6lUT+pfyPYgGtJqEc9n45Ny4VhPDDMOQVVw2kYF3CTCMvdM2VMYPfFiB0Y6g4DRlzUWiveNWEC0+6f2Ysl6qyGwPeWV7T5I6g== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(346002)(396003)(376002)(366004)(39860400002)(136003)(66556008)(66476007)(186003)(7416002)(52116002)(86362001)(26005)(956004)(8936002)(478600001)(7406005)(7696005)(8676002)(83380400001)(5660300002)(2616005)(38350700002)(38100700002)(44832011)(2906002)(1076003)(54906003)(6666004)(66946007)(316002)(4326008)(36756003)(6486002)(30864003);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: V/xY1WuiR5o8Qx+vaGhw3GgS4CCJasBwIfrmSbdPoFSutf79EubvsSezwK/fxIYIlaKmv8JhYHyoSJXsGSOZlcQWf8P2l6wTDVlyrtk1fZ7z0vSf1GTs/0DV9QbilIF04nIMP69JbaL5RmUjj2dHurQT9TznZNeq8Irt4TG2vZXNM3qIa/uLDsL8hV9lJs18/jfIuAXdkO72lV7rFI90dJ49p5CKt9jhlRBkJ3xnxeXdph4pED82ClC6Us5fg/TUAr9+/5v/QfCsXWv5olnmjJeeaLmZurDze+GHlhW1MKJqA6PJVNq3Lry/PhdeAqCp/XtPpE6vCVIUJQLGWAkp2SkMyg2ZhNWYL+R5ltZxZ6u29VpCTSLu/4c/qWYsVbUwEkOWwk04kssttA7pyp5c7pY5WKZ2DisaWXbcDXKiSJpk5bS49newapzEXC8hzSTn0ZSD3iHFlbErMNg1mgyLzlpRbgHEe2o1In3l38xL6E/qrmFaTQh8j+BQK1xXnow6IehlkoDNKI0lLPwyd1hBJd5UQgHG9Dse/wMHGISifG5+0BAMlXMrhkqCUYBnDo0VCLk+m34BFwavBUzjtTI0yW9Kx7Cpv4BB8g3r/xAOZMnuLpSBx4XQPPband2X9xmsmkSyOZGTrsmVcD4pGVY4IXiJ4/Ix9i5Y+pKmPB7RxJmGmG5t+b2n/vR3cQWEXoHpYHqm5Eh4vIhmpesBT+gakdXtu5PkH+ZtPrQP49ZHMNWqxGuURjjNT6mF/sLphGl+kzGTvqYbM+rMdyqATvbYVy1c5E2MTvnfjy78KeVYIAv4dPgBrFpAvmLC3IZBsjziUU46YebJNE3A4o4NfQ3HWdf2ZjfgunyRnOYhIJPZdEyMpPPggFyXvOKbRKiFA7yb6DU9x62KvrqZn5BadMUT/oJ+RPpFjuKgahp9+tKJeWfM7CflrqXcmlY+b8xzARN6ZHlis9jAFJbnc5s6IPOkrPnd9rr4c4tjc9UMMj4K9ddYvTY/NmtTZIW93JvVFw5pbQvl8lMAinnpJvhZC5PDk1tpBzvP+sZnGWf2CdmtbEZnctgfxCfXbk2lQFwAG1Uzs7j3Yu9EfygSScf1wvFm/bJ1pfmn+ptD3MeELXVXzXP0GpmWDpluSSBwAug837XBI839YEFSx5ni+1DI3igB6SPJto3VFmaGoO1DUxwNRIrfdMsKy2FDHOFGY9OM9+do5Tg/f4vMReLtMJpPjsq0CBZlfTjR4yHS/+rSzhxCkQLf/AxUFG8yBdj+d1+SbZKaVodBUcApPqrbO/3Nsm89pPw3+R8vkQRYcUsSclcalP5X1HP6XUlGclwXGQIGlFSN X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 40d8fd6a-95f5-427d-da9b-08d9417357d3 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:30.2530 (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: VFcZq9RuwmVk1/IuYKqJjcS+XUh+DXM7HdmC/wCzB77NB3lQL05pYsm2S8QgO017W2esZn4D6c3nsYLZ8WJrgQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB5016 X-Rspam-User: nil Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=zJwH49CT; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf17.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 97895F002C84 X-Stat-Signature: aemp6uuorj3dehh7q75hhmbpf9pff5wj X-HE-Tag: 1625681805-850810 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth Future patches for SEV-SNP-validated CPUID will also require early parsing of the EFI configuration. Move the related code into a set of helpers that can be re-used for that purpose. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/Makefile | 1 + arch/x86/boot/compressed/acpi.c | 124 +++++--------- arch/x86/boot/compressed/efi-config-table.c | 180 ++++++++++++++++++++ arch/x86/boot/compressed/misc.h | 50 ++++++ 4 files changed, 272 insertions(+), 83 deletions(-) create mode 100644 arch/x86/boot/compressed/efi-config-table.c diff --git a/arch/x86/boot/compressed/Makefile b/arch/x86/boot/compressed/Makefile index 431bf7f846c3..b41aecfda49c 100644 --- a/arch/x86/boot/compressed/Makefile +++ b/arch/x86/boot/compressed/Makefile @@ -100,6 +100,7 @@ endif vmlinux-objs-$(CONFIG_ACPI) += $(obj)/acpi.o vmlinux-objs-$(CONFIG_EFI_MIXED) += $(obj)/efi_thunk_$(BITS).o +vmlinux-objs-$(CONFIG_EFI) += $(obj)/efi-config-table.o efi-obj-$(CONFIG_EFI_STUB) = $(objtree)/drivers/firmware/efi/libstub/lib.a $(obj)/vmlinux: $(vmlinux-objs-y) $(efi-obj-y) FORCE diff --git a/arch/x86/boot/compressed/acpi.c b/arch/x86/boot/compressed/acpi.c index 8bcbcee54aa1..e087dcaf43b3 100644 --- a/arch/x86/boot/compressed/acpi.c +++ b/arch/x86/boot/compressed/acpi.c @@ -24,42 +24,36 @@ struct mem_vector immovable_mem[MAX_NUMNODES*2]; * Search EFI system tables for RSDP. If both ACPI_20_TABLE_GUID and * ACPI_TABLE_GUID are found, take the former, which has more features. */ +#ifdef CONFIG_EFI +static bool +rsdp_find_fn(efi_guid_t guid, unsigned long vendor_table, bool efi_64, + void *opaque) +{ + acpi_physical_address *rsdp_addr = opaque; + + if (!(efi_guidcmp(guid, ACPI_TABLE_GUID))) { + *rsdp_addr = vendor_table; + } else if (!(efi_guidcmp(guid, ACPI_20_TABLE_GUID))) { + *rsdp_addr = vendor_table; + return false; + } + + return true; +} +#endif + static acpi_physical_address -__efi_get_rsdp_addr(unsigned long config_tables, unsigned int nr_tables, +__efi_get_rsdp_addr(unsigned long config_table_pa, unsigned int config_table_len, bool efi_64) { acpi_physical_address rsdp_addr = 0; - #ifdef CONFIG_EFI - int i; - - /* Get EFI tables from systab. */ - for (i = 0; i < nr_tables; i++) { - acpi_physical_address table; - efi_guid_t guid; - - if (efi_64) { - efi_config_table_64_t *tbl = (efi_config_table_64_t *)config_tables + i; - - guid = tbl->guid; - table = tbl->table; - - if (!IS_ENABLED(CONFIG_X86_64) && table >> 32) { - debug_putstr("Error getting RSDP address: EFI config table located above 4GB.\n"); - return 0; - } - } else { - efi_config_table_32_t *tbl = (efi_config_table_32_t *)config_tables + i; - - guid = tbl->guid; - table = tbl->table; - } + int ret; - if (!(efi_guidcmp(guid, ACPI_TABLE_GUID))) - rsdp_addr = table; - else if (!(efi_guidcmp(guid, ACPI_20_TABLE_GUID))) - return table; - } + ret = efi_foreach_conf_entry((void *)config_table_pa, config_table_len, + efi_64, rsdp_find_fn, &rsdp_addr); + if (ret) + debug_putstr("Error getting RSDP address.\n"); #endif return rsdp_addr; } @@ -87,7 +81,9 @@ static acpi_physical_address kexec_get_rsdp_addr(void) efi_system_table_64_t *systab; struct efi_setup_data *esd; struct efi_info *ei; + bool efi_64; char *sig; + int ret; esd = (struct efi_setup_data *)get_kexec_setup_data_addr(); if (!esd) @@ -98,18 +94,16 @@ static acpi_physical_address kexec_get_rsdp_addr(void) return 0; } - ei = &boot_params->efi_info; - sig = (char *)&ei->efi_loader_signature; - if (strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) { + /* Get systab from boot params. */ + ret = efi_bp_get_system_table(boot_params, (unsigned long *)&systab, &efi_64); + if (ret) + error("EFI system table not found in kexec boot_params."); + + if (!efi_64) { debug_putstr("Wrong kexec EFI loader signature.\n"); return 0; } - /* Get systab from boot params. */ - systab = (efi_system_table_64_t *) (ei->efi_systab | ((__u64)ei->efi_systab_hi << 32)); - if (!systab) - error("EFI system table not found in kexec boot_params."); - return __efi_get_rsdp_addr((unsigned long)esd->tables, systab->nr_tables, true); } #else @@ -119,54 +113,18 @@ static acpi_physical_address kexec_get_rsdp_addr(void) { return 0; } static acpi_physical_address efi_get_rsdp_addr(void) { #ifdef CONFIG_EFI - unsigned long systab, config_tables; - unsigned int nr_tables; - struct efi_info *ei; + unsigned long config_table_pa = 0; + unsigned int config_table_len; bool efi_64; - char *sig; - - ei = &boot_params->efi_info; - sig = (char *)&ei->efi_loader_signature; - - if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) { - efi_64 = true; - } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) { - efi_64 = false; - } else { - debug_putstr("Wrong EFI loader signature.\n"); - return 0; - } - - /* Get systab from boot params. */ -#ifdef CONFIG_X86_64 - systab = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32); -#else - if (ei->efi_systab_hi || ei->efi_memmap_hi) { - debug_putstr("Error getting RSDP address: EFI system table located above 4GB.\n"); - return 0; - } - systab = ei->efi_systab; -#endif - if (!systab) - error("EFI system table not found."); - - /* Handle EFI bitness properly */ - if (efi_64) { - efi_system_table_64_t *stbl = (efi_system_table_64_t *)systab; - - config_tables = stbl->tables; - nr_tables = stbl->nr_tables; - } else { - efi_system_table_32_t *stbl = (efi_system_table_32_t *)systab; - - config_tables = stbl->tables; - nr_tables = stbl->nr_tables; - } + int ret; - if (!config_tables) - error("EFI config tables not found."); + ret = efi_bp_get_conf_table(boot_params, &config_table_pa, + &config_table_len, &efi_64); + if (ret || !config_table_pa) + error("EFI config table not found."); - return __efi_get_rsdp_addr(config_tables, nr_tables, efi_64); + return __efi_get_rsdp_addr(config_table_pa, config_table_len, + efi_64); #else return 0; #endif diff --git a/arch/x86/boot/compressed/efi-config-table.c b/arch/x86/boot/compressed/efi-config-table.c new file mode 100644 index 000000000000..d1a34aa7cefd --- /dev/null +++ b/arch/x86/boot/compressed/efi-config-table.c @@ -0,0 +1,180 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Helpers for early access to EFI configuration table + * + * Copyright (C) 2021 Advanced Micro Devices, Inc. + * + * Author: Michael Roth + */ + +#include "misc.h" +#include +#include + +/* Get vendor table address/guid from EFI config table at the given index */ +static int get_vendor_table(void *conf_table, unsigned int idx, + unsigned long *vendor_table_pa, + efi_guid_t *vendor_table_guid, + bool efi_64) +{ + if (efi_64) { + efi_config_table_64_t *table_entry = + (efi_config_table_64_t *)conf_table + idx; + + if (!IS_ENABLED(CONFIG_X86_64) && + table_entry->table >> 32) { + debug_putstr("Error: EFI config table entry located above 4GB.\n"); + return -EINVAL; + } + + *vendor_table_pa = table_entry->table; + *vendor_table_guid = table_entry->guid; + + } else { + efi_config_table_32_t *table_entry = + (efi_config_table_32_t *)conf_table + idx; + + *vendor_table_pa = table_entry->table; + *vendor_table_guid = table_entry->guid; + } + + return 0; +} + +/* + * Iterate through the entries in the EFI configuration table and pass the + * associated GUID/physical address of each entry on the provided callback + * function. + * + * @conf_table: pointer to EFI configuration table + * @conf_table_len: number of entries in EFI configuration table + * @efi_64: true if using 64-bit EFI + * @fn: callback function that returns true if iteration + * should continue + * @opaque: optional caller-provided data structure to pass to + * callback function on each iteration + * + * Returns 0 on success. + */ +int +efi_foreach_conf_entry(void *conf_table, unsigned int conf_table_len, + bool efi_64, bool (*fn)(efi_guid_t vendor_table_guid, + unsigned long vendor_table_pa, + bool efi_64, + void *opaque), + void *opaque) +{ + unsigned int i; + + for (i = 0; i < conf_table_len; i++) { + unsigned long vendor_table_pa; + efi_guid_t vendor_table_guid; + + if (get_vendor_table(conf_table, i, &vendor_table_pa, + &vendor_table_guid, efi_64)) + return -EINVAL; + + if (!fn(vendor_table_guid, vendor_table_pa, efi_64, opaque)) + break; + } + + return 0; +} + +/* + * Given boot_params, retrieve the physical address of EFI system table. + * + * @boot_params: pointer to boot_params + * @sys_table_pa: location to store physical address of system table + * @is_efi_64: location to store whether using 64-bit EFI or not + * + * Returns 0 on success. On error, return params are left unchanged. + */ +int +efi_bp_get_system_table(struct boot_params *boot_params, + unsigned long *sys_table_pa, bool *is_efi_64) +{ + unsigned long sys_table; + struct efi_info *ei; + bool efi_64; + char *sig; + + if (!sys_table_pa || !is_efi_64) + return -EINVAL; + + ei = &boot_params->efi_info; + sig = (char *)&ei->efi_loader_signature; + + if (!strncmp(sig, EFI64_LOADER_SIGNATURE, 4)) { + efi_64 = true; + } else if (!strncmp(sig, EFI32_LOADER_SIGNATURE, 4)) { + efi_64 = false; + } else { + debug_putstr("Wrong EFI loader signature.\n"); + return -ENOENT; + } + + /* Get systab from boot params. */ +#ifdef CONFIG_X86_64 + sys_table = ei->efi_systab | ((__u64)ei->efi_systab_hi << 32); +#else + if (ei->efi_systab_hi || ei->efi_memmap_hi) { + debug_putstr("Error: EFI system table located above 4GB.\n"); + return -EINVAL; + } + sys_table = ei->efi_systab; +#endif + if (!sys_table) { + debug_putstr("EFI system table not found."); + return -ENOENT; + } + + *sys_table_pa = sys_table; + *is_efi_64 = efi_64; + return 0; +} + +/* + * Given boot_params, locate EFI system table from it and return the physical + * address EFI configuration table. + * + * @boot_params: pointer to boot_params + * @conf_table_pa: location to store physical address of config table + * @conf_table_len: location to store number of config table entries + * @is_efi_64: location to store whether using 64-bit EFI or not + * + * Returns 0 on success. On error, return params are left unchanged. + */ +int +efi_bp_get_conf_table(struct boot_params *boot_params, + unsigned long *conf_table_pa, + unsigned int *conf_table_len, + bool *is_efi_64) +{ + unsigned long sys_table_pa = 0; + int ret; + + if (!conf_table_pa || !conf_table_len || !is_efi_64) + return -EINVAL; + + ret = efi_bp_get_system_table(boot_params, &sys_table_pa, is_efi_64); + if (ret) + return ret; + + /* Handle EFI bitness properly */ + if (*is_efi_64) { + efi_system_table_64_t *stbl = + (efi_system_table_64_t *)sys_table_pa; + + *conf_table_pa = stbl->tables; + *conf_table_len = stbl->nr_tables; + } else { + efi_system_table_32_t *stbl = + (efi_system_table_32_t *)sys_table_pa; + + *conf_table_pa = stbl->tables; + *conf_table_len = stbl->nr_tables; + } + + return 0; +} diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 822e0c254b9a..522baf8ff04a 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -174,4 +175,53 @@ void boot_stage2_vc(void); unsigned long sev_verify_cbit(unsigned long cr3); +#ifdef CONFIG_EFI +/* helpers for early EFI config table access */ +int efi_foreach_conf_entry(void *conf_table, unsigned int conf_table_len, + bool efi_64, + bool (*fn)(efi_guid_t guid, + unsigned long vendor_table_pa, + bool efi_64, + void *opaque), + void *opaque); + +int efi_bp_get_system_table(struct boot_params *boot_params, + unsigned long *sys_table_pa, + bool *is_efi_64); + +int efi_bp_get_conf_table(struct boot_params *boot_params, + unsigned long *conf_table_pa, + unsigned int *conf_table_len, + bool *is_efi_64); +#else +static inline int +efi_foreach_conf_entry(void *conf_table, unsigned int conf_table_len, + bool efi_64, + bool (*fn)(efi_guid_t guid, + unsigned long vendor_table_pa, + bool efi_64, + void *opaque), + void *opaque); +{ + return -ENOENT; +} + +static inline int +efi_bp_get_system_table(struct boot_params *boot_params, + unsigned long *sys_table_pa, + bool *is_efi_64) +{ + return -ENOENT; +} + +static inline int +efi_bp_get_conf_table(struct boot_params *boot_params, + unsigned long *conf_table_pa, + unsigned int *conf_table_len, + bool *is_efi_64) +{ + return -ENOENT; +} +#endif /* CONFIG_EFI */ + #endif /* BOOT_COMPRESSED_MISC_H */ From patchwork Wed Jul 7 18:14: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: 12363393 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,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 B4DD4C07E9E for ; Wed, 7 Jul 2021 18:17:47 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6518161C6D for ; Wed, 7 Jul 2021 18:17:47 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6518161C6D Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4C9B26B0011; Wed, 7 Jul 2021 14:17:47 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 49F796B005D; Wed, 7 Jul 2021 14:17:47 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2CC5E6B006C; Wed, 7 Jul 2021 14:17:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0089.hostedemail.com [216.40.44.89]) by kanga.kvack.org (Postfix) with ESMTP id 08DAB6B0011 for ; Wed, 7 Jul 2021 14:17:47 -0400 (EDT) Received: from smtpin08.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 5C3921828EE2E for ; Wed, 7 Jul 2021 18:17:46 +0000 (UTC) X-FDA: 78336600132.08.8E48C2A Received: from NAM04-BN8-obe.outbound.protection.outlook.com (mail-bn8nam08on2056.outbound.protection.outlook.com [40.107.100.56]) by imf10.hostedemail.com (Postfix) with ESMTP id C00336001F15 for ; Wed, 7 Jul 2021 18:16:36 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PF/lgwCRw3TZc+NKvIZYNxgVY7AN9dcGoGXwK+jLI8OxbYumUD/K3xRszTQKEtrY5Q0vEoIqHygteCnzgnHCEtBdhtgrUfdoWMwURSMcafdu8UXZLl8n1ZaezpaNvi7G3rE1Ey9WGQgIKoHAEZhkCtdflkYsmCVfk1MUoSHoWkEbzNNhFCy/N6jaOetmKHGGQNwideIztehOGwrMO13o9gX83LbzwbIJiWOQyfCXgkvqUy+OKn1xeZEmhPRZv6MzlQCo5QfYiD2mQJk7J9roMiR2QvTjerES9vBpg6rhQL5Fuk5kIO35uiH5fG54lN6A+QCJe0LnquQIP141X7lm7Q== 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=GiUqerpB9vjzA1VWp+uR5Yl+kyR8h9OKCLWLo/DJvek=; b=Bj5FGSWSWV/VuD6b68SMLUJ5SltR8mD6Ub6XzlRRaUAQY4j2qshUR8cf4ArE2N8Nn2Q7TbN7bX/E2+7LNOr5G/gziRh8hEzaMFFeAq36q4rE8+nL07/N7kKsCywnuEKCNgu3BJ/+NvVJiWGRx2+08aTXbPf6fEX+bKkEaO4PNms5AbAKumAPXzxIoBrPTapuP/yzU8UhJppEv1/si6D0bmKFvuuPDQAVq7o0Whcd3WfTSwrFX2M+/no61DelABW9coQTmY5l0E2TTKZgLW8PNpKt2iasz2qpTZzYTM4dt1g/U6Qgf/z5wDT+/gD1RdMdAXay34m9hMihb9Nq1rOJug== 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=GiUqerpB9vjzA1VWp+uR5Yl+kyR8h9OKCLWLo/DJvek=; b=dWWovH9GBQMrAiIOwaCL7qorkuYuPLFa+fcEHy6jCRrdxzwj70upBkeWv2KW0djoWwCf2ztrj+i71g8JAL3exkKvjasiHhLj2w+lOidATwbt1qaAx+bNdTDwkTom84DPWNIooFuNJZ9z31G70WQBIscMrdaivRVJCzEIndAU2Kg= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:33 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:32 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 25/36] x86/boot: Add Confidential Computing type to setup_data Date: Wed, 7 Jul 2021 13:14:55 -0500 Message-Id: <20210707181506.30489-26-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 52d29a9f-0353-4d48-64e1-08d941735953 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: TyW23MbKhxUc6nBezNiwIm6LFU0etLhpU0ol1CjPOxyKJWUS4ao7MmBXAcBx7HzxUpj/zj4jFvEKf0P1B0Q2fEULyfsgCCxul9vUmu6TA/YZ0QyfTATtQHnNAaGJ7f8tRkaRlrO6t3cb0zvTonIb+Uqet5mwa2/TeynL/rxiSeEMBbsZ2vMAqYLmrH+fY3p1bAiFkXxsQaY9tp5wx2ELI92CgfNoMjlvMtxkLDtDTMou8JF5MJ40nkpbRtviwLs8eNQqLLL0CZzGA3KQCWoHRLRAWe96i0LLdj8I472qVUUf+fkQ3SyypIvGKVzp02yurQU6CKr8Y3TLFDBZWGQ0yOV0aSOtF/k8at7aySEnj5KLYdQffQpvavDEy11ou1eSI+t/A/oX3emdugxG0bLYxbqZzup54rGQwOgwxSoNqGvOPUsG6Zh0aonlDS3t6v2WgsmcHnXv9oxJTGxeRafk+1qD/DwIlmOUUvNwYtgNntONDAUsnZR346OIsD9aJDFvWo7QGeVGOTVPmHL99bYbntjKqEvBJyEe7iOZ3GO5mQQ/GOGgcK7txRf5VZYc06E/NFOfyO1qpVHlq6z5WZjUA17x5sP3rzni5BtvFvpwxd+DkRvDfQwjI4tx//BjxbI433dHzp1KyPC8AXqz8pu2LK4eALP+wKSZP76CBfmnbfQt2X254f2ZixE+RxG3iarpjbCQy/n43FEOp2bvV8CCOg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: HEAOp9HCNFe5cDkdM2bbRFcacJDzNKREs/9inZO2XYpJAENATJu8V4Mp3IIxVJJXct+0v+BO080/vEvoRvGvsTXpkltIyjwn3DXvGOBXvKqiWxjE0oKFyBDQoRQB0YxiuRKv5LWPC8Jn18AI8JyjI5QqWYL6LEg2/KDvhvbNIju5UqKor9wI2FTN8VDPEsibUZ389Lq2FQGWZAihIxLDbJcI01Tn1McCSVLUAhycEalqtYLMW4KNkjEY0yHEvq6CY+QglmGXTzDrMF9WaXYqh42X72NdlR+3TEEMowMn4YFMax8nByrC/NI1kdl2Q/+0965SMEX/0gdOKa9s+ieSW+dOSMOfi92uikQbg7okwEG8LMLklwh+A113tWvKXF6RqYZ0nIn/ikq2XtfXpBSWH3eXYvUAF8e196GhCo/35MbuA54r2T+LvECOyiq1XN0iNSXVGDLRlM1Pz8yXSIQsabMceEygLChqDAzUADjWrWzxPJtWxTqqNRRPStcvEX485hCuuR8PJEKJNgTpO58Mk492shMQrX62IMaztD2+myoWBCVKjPzIm5qrFq2rajmMEnuVtIWD+/sVlPIlmBV87bWsP/xjjTyoxbWfFufXniLHCrN6HMO3kTAzhJwZk5xvXs0txMs+iSb8lNBamDkBDs1Nf1wJIrlAW3VqT6LYQhOKPXSeFJf7xwsuhU4Xqfg26QqFlxRi9n8werVVbAuk2xidnjju7TDwBrf9L7wz6sJcVL6bmRAMYZ/Ly5sq4W6/s7skWLrl97OxCLUxl2zHPx6yik/LiqoX6qK9zWTzdPXP7vrmSOldBh66esC5imMtL5GByWQtG3hBVYTMIh73TUf9ovd4JMlHlPDGzxvUPp3/0dDv85uwB09Ytpi/Z4pdlpC0LqP1ud2yvbhWG+is3fssuhJiqd2KFeTJyx+mQ7dp6hebdhu/uMyGSPsO4iznGvMcsfqfihNiHFptXs1GlXpevw3HoKD//Jwj+SflvIYRCff8FE0E31J6HeJjDLiwsUaN3/L4qxGTIECrgQYxoIpDlpy8KD43Lee4KZHL2A9FxAdOZulGUEGLw6pFsvRJsKdM+yVFWJLYqZsZtvAdonksn5oM7bzMqUemUn9dIQ8Zi6sFTohIMVBUFztBSrVHxuN8zqNhOWQAeyFlCrTP49EKg/SJGBPwoqA5W7BPrIc5ijQMdkjv/N/NKsiz2SBWkIAHdyKHYZCvfa1QgHDWEbnsPHLzA6MEKxcSTB/agmtIt0Rm1CZ4PeG4CEhV9rA31vVsubpuwY1I5V/5SvsaLAq5KiGAj2FuVIc2u4fLPKj6aHmU8zOr5X1I/IkRJfaP X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 52d29a9f-0353-4d48-64e1-08d941735953 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:32.7066 (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: IwZ9Vikw2AAiJFsspTcYPUiDDdzcwGcH7raiYjCfMkPATyVblQIG7Zfwfltr418KZL92qS1RSTJesDMUAIQBSg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 X-Rspam-User: nil Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=dWWovH9G; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.100.56 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Stat-Signature: o9ppwxhco3srsmdfrasp1kpgpquz9bj5 X-Rspamd-Queue-Id: C00336001F15 X-Rspamd-Server: rspam01 X-HE-Tag: 1625681796-655321 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: While launching the encrypted guests, the hypervisor may need to provide some additional information during the guest boot. When booting under the EFI based BIOS, the EFI configuration table contains an entry for the confidential computing blob that contains the required information. To support booting encrypted guests on non-EFI VM, the hypervisor needs to pass this additional information to the kernel with a different method. For this purpose, introduce SETUP_CC_BLOB type in setup_data to hold the physical address of the confidential computing blob location. The boot loader or hypervisor may choose to use this method instead of EFI configuration table. The CC blob location scanning should give preference to setup_data data over the EFI configuration table. In AMD SEV-SNP, the CC blob contains the address of the secrets and CPUID pages. The secrets page includes information such as a VM to PSP communication key and CPUID page contains PSP filtered CPUID values. Define the AMD SEV confidential computing blob structure. While at it, define the EFI GUID for the confidential computing blob. Signed-off-by: Brijesh Singh --- arch/x86/include/asm/sev.h | 12 ++++++++++++ arch/x86/include/uapi/asm/bootparam.h | 1 + include/linux/efi.h | 1 + 3 files changed, 14 insertions(+) diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index f68c9e2c3851..e41bd55dba5d 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -44,6 +44,18 @@ struct es_em_ctxt { void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code); +/* AMD SEV Confidential computing blob structure */ +#define CC_BLOB_SEV_HDR_MAGIC 0x45444d41 +struct cc_blob_sev_info { + u32 magic; + u16 version; + u16 reserved; + u64 secrets_phys; + u32 secrets_len; + u64 cpuid_phys; + u32 cpuid_len; +}; + static inline u64 lower_bits(u64 val, unsigned int bits) { u64 mask = (1ULL << bits) - 1; diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/asm/bootparam.h index b25d3f82c2f3..1ac5acca72ce 100644 --- a/arch/x86/include/uapi/asm/bootparam.h +++ b/arch/x86/include/uapi/asm/bootparam.h @@ -10,6 +10,7 @@ #define SETUP_EFI 4 #define SETUP_APPLE_PROPERTIES 5 #define SETUP_JAILHOUSE 6 +#define SETUP_CC_BLOB 7 #define SETUP_INDIRECT (1<<31) diff --git a/include/linux/efi.h b/include/linux/efi.h index 6b5d36babfcc..75aeb2a56888 100644 --- a/include/linux/efi.h +++ b/include/linux/efi.h @@ -344,6 +344,7 @@ void efi_native_runtime_setup(void); #define EFI_CERT_SHA256_GUID EFI_GUID(0xc1c41626, 0x504c, 0x4092, 0xac, 0xa9, 0x41, 0xf9, 0x36, 0x93, 0x43, 0x28) #define EFI_CERT_X509_GUID EFI_GUID(0xa5c059a1, 0x94e4, 0x4aa7, 0x87, 0xb5, 0xab, 0x15, 0x5c, 0x2b, 0xf0, 0x72) #define EFI_CERT_X509_SHA256_GUID EFI_GUID(0x3bd2a492, 0x96c0, 0x4079, 0xb4, 0x20, 0xfc, 0xf9, 0x8e, 0xf1, 0x03, 0xed) +#define EFI_CC_BLOB_GUID EFI_GUID(0x067b1f5f, 0xcf26, 0x44c5, 0x85, 0x54, 0x93, 0xd7, 0x77, 0x91, 0x2d, 0x42) /* * This GUID is used to pass to the kernel proper the struct screen_info From patchwork Wed Jul 7 18:14: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: 12363851 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,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 41D03C07E9B for ; Wed, 7 Jul 2021 18:50:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id CB40E61CAA for ; Wed, 7 Jul 2021 18:50:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CB40E61CAA Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 279106B0096; Wed, 7 Jul 2021 14:50:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 228E66B0098; Wed, 7 Jul 2021 14:50:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0044A6B0099; Wed, 7 Jul 2021 14:50:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0209.hostedemail.com [216.40.44.209]) by kanga.kvack.org (Postfix) with ESMTP id C64396B0098 for ; Wed, 7 Jul 2021 14:50:03 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 280F3180CC184 for ; Wed, 7 Jul 2021 18:50:03 +0000 (UTC) X-FDA: 78336681486.27.570098E Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2073.outbound.protection.outlook.com [40.107.237.73]) by imf21.hostedemail.com (Postfix) with ESMTP id 27265D002861 for ; Wed, 7 Jul 2021 18:49:43 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TQrcE6+663OXwP4Hy29ozvoecWLsP3tvt+2LQcreOkq6dFXaO5ino7MUZ4eaJd6DQ54mpMtae1419o335QeGG5UnDyQVyMKP+LpcPyZ7fI4P9TJdL4l2Fv7PjRLD9ENcqV83zKfoHQCJJghDKm18jDyKLsLzxa1U0pnCDGPKdtNv5DtIiYbUKEkhTSGdDXE5THaqn679J5PqZhdRH2KMVLG11+QKTWyTMlfO+c4ADxk3xMyliQACjFDg+xhGApDTQN3awTRg+3VWbnzhoa9TKxLXky0nLt1AqxNgP1rhUhWzAmglLoYJPuQ3OBNDtOCzAxnla2NLmIB+OG5bpk0XCQ== 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=l5Bj4ChvzX1GEpzBwT4c6XJS0kWfJq1wmxQJV3kjYSM=; b=K2olZqbOhvaSxR1wk1GPiy50ohrbvthJ2OIGcyhKf6Tp3wJxAOBMW4lLgny+DaD0BCd11gXIW3374R09e/iGiW9aphwXUvNONXRfO/Ytuu7fmZ/s/90xn+ThrlWRdF8bigksjXla0a4YzEhunSqD/ygBkNRbPtkhfZpXjOMqmJF3vudQcSH+ndXuQmnpvi9Ynmcj1/PkJKAtkQHoh1I0snz69uMq0qwfe1ZfpB2yGBMgszFZyWz49ssq8gEpoAUljwsQCk4XgrOvxQ0r0M6kRl1Nxf1dOrMKHRaQ4ARDG9PnWxOoHfHVPUltfoUyeXOQPSua6/V9zKf8zPh7VP1u2g== 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=l5Bj4ChvzX1GEpzBwT4c6XJS0kWfJq1wmxQJV3kjYSM=; b=OMNIb6eifOxlspazNc4/zb8Jyi1EDv2GH7Jj7VkH6h7FTq2qXGMma7vUbCV2JUmnpIQBey9ETYU6qJlYv1NGgoZ5BpwWoo9GGv1kPQaHVEHTZQzqlxW2zMnETr4iwnFnEp0AUz1hpI+VX65e8DhzXKngeK0AeAT8ZDaxCnRroYQ= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:36 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:35 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 26/36] x86/compressed/64: enable SEV-SNP-validated CPUID in #VC handler Date: Wed, 7 Jul 2021 13:14:56 -0500 Message-Id: <20210707181506.30489-27-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:32 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: bb525b83-f1d6-40c2-8775-08d941735acc X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:372; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RCjjQyzPr/RJjHQbQVx5YViGWvg3rV9e+fv6lZ1sFuY4Mc5UO9sM1Mutwi74AVseBwi0Qm1l00p6Z/2/0hm7s6OFemr8xPOaXbCIs3d6y7K46WifdZqSJIIN2QiLyOpvQ1Ji982B+hF7XwNiI1OoPK/+fFYxtki8rCLUmqBWXJkLZoFOK+D3nSyywEoi4Wnlo50Or/0WrQTmelIBnzzzwwATx5MbyC4sOiKlHPnRH4aCPaNAsgtLYv1IcYFG0q4RaYYmgE6pOBD9wiGH7U7N/JBWJuAllttg+i7IyoSRkYYZ1Q4DZVA5gywCcAOEQ2AQLk2I5isiAtGyBVfTVvMF5lU0GN01QEd/3IZ1j096fDm2QMSTtCpES1VOY+rrljyjL3x4u7X3PGxmbMBFVsLlYpQR90WSfxZ6Vkd8gaZBgrObvV7u+IuqUtfq4MHJLo4vQ67F6HTfs9BNkFg/ZXLk35zHoqW8s6/Qll7ksyQUjkzLMwqVYpLTkFACgD7lk5np/1qjcg92aXRwyk/hI+ABpTtpNTu+DY01zdP+Lxw37fMR7FyHlMr1n2OrO7G583RwOFPIJ7o7+Rd8Oqcz6jvSKzTHkCm+IZLO+iylNiNo+HYygS1r1RZTscFpECcSLA8W7M6kIYVgGhRu5lN5HB0dU4JXP1BTgzhjIYt78kEPSpuY8Rm93j0U2vIG1NTPLMUhxN90ULNxi4U+z9Ls/PMvOw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(15650500001)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(30864003)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Oxs6JPJcVCIfo5/cQ+nx59dp7j3SV0zgOVY3OvoraIFY9dKMHcblMADGzFMff30jEcOjTEjqwh1JbtyeqCFauKh3hI/V2og0r2gLbPUPfF7IHMWNXQ03TwIOh9JadOYk8He9SYYaB+FzzEUb/r1u8mameo3keGmDJyOe7QOq8qVHCbIUxi0IaZhiHlq58CW/4nSN2mJnkRXiVSIS4SHOLlQ+I3UoVtbHqOo5vSNvXS1k07ITroKyZRBC+iFURCQLkGoBf/oEZmg2XH8hwx1Amg0W5I7Q5xIWLMqZJe4esKOcvDFFP+dgre5NFeDnI+VkCegtx+xAkUb7JMz2YP7Y9SDd/xjiVibCijbHht2EURJD80F9BLVmFp13OXhI+rjRmi9BZJj1eh1nErdkTGcOE6WVamBaTAByEZD4awL8/E2ZUBh5z247w4Q308wxL1PJpt5XKQNDq10li2hAJp2EWtxH3jrcQXoA/azj3hdnhmJmF0qBQsVFy0Jb9/d0gsYTwhtrzPACM/FzN09bZhhJyqpd4YDl0Gc+HA99wNSN/N/jmBiySouVqp5Qj+IxLaanJcBGXIj3f9TwIviWc47KUnLiiSjNTwTuHdINCPOP1h33hdoZL5chvaMEdjqjss73r9CTtqerxl9PA8TQo2WhAEAsFQaGNUKIRjtxNmGLf9THrHXbP8BvBdIezcIhlPL+aZO6J90w76Vte+6LdJHcpLWG+NeP8gTJ1T5NvOtt3EMOgNQkd79uTyi8fWQhMa2lS5LRm7FgXmk5I0Z9ko8uMAKuGuq+nLc9PLq0GcJmaTk45bV2G1IFlkt+LcJE7mBn0mMQ/BadCVjqzDwNTc+Hs6ZpRIV7msTAtLm8tWsef21jKaogkvPCezaaXa+Ot0uTUHDpB3Mpgoe16zLMRA0vIrkqfiQ+94k5sKmq2aQ5mz5GphazzGAx8cAS6WM2K6b2dpF2DF1FW9ZgUzQUWeaVt3B4/bBN0xJXrR5pVJoNp+U7uzg60Adtm7vLRSgBbc6zteSz3ZyBpx9FS08Ap5UTRoawp51ZkoJeR3mYytcEjNFe601kRsG8Hx3B1rRyFGQ59EP7iL/9H6YzhqTDE71tXaV5cZH1aCc9WWuWYO6XAsvhfW3z2TAwcqym4PKqFz1VYnLAnfXuIzzOgYuPQn+vSORKvC2dd5ZZGIprJLeHpTt9bslRfneNBV3CzXRBDloaXtssXXyT5q7PJF5zBuKkvsuFD8MnIzirBeNPk2dXM4HqdX+gZUg1emaQLnTQZnkU96xmLTCcfjbGdFuzi0nWgLNDhO/Px8ZxVxm5klEvritNT3ntKwXxF7AXO8qAd/gw X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: bb525b83-f1d6-40c2-8775-08d941735acc X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:35.2762 (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: f1fwLJJImytyH5UohE/b0xPiSdnbmoOawm8tdZP6Moe8Y5javAyqaO96aRw/MXXJdWf7xk5twPt2gB4y19ueRA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=OMNIb6ei; spf=temperror (imf21.hostedemail.com: error in processing during lookup of brijesh.singh@amd.com: DNS error) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: 1q3oyzw6dw1s13n6iukw9n8wh1um9wkw X-Rspamd-Queue-Id: 27265D002861 X-Rspam-User: nil X-HE-Tag: 1625683783-71393 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth CPUID instructions generate a #VC exception for SEV-ES/SEV-SNP guests, for which early handlers are currently set up to handle. In the case of SEV-SNP, guests can use a special location in guest memory address space that has been pre-populated with firmware-validated CPUID information to look up the relevant CPUID values rather than requesting them from hypervisor via a VMGEXIT. Determine the location of the CPUID memory address in advance of any CPUID instructions/exceptions and, when available, use it to handle the CPUID lookup. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/boot/compressed/efi-config-table.c | 44 +++ arch/x86/boot/compressed/head_64.S | 1 + arch/x86/boot/compressed/idt_64.c | 7 +- arch/x86/boot/compressed/misc.h | 10 + arch/x86/boot/compressed/sev.c | 3 + arch/x86/include/asm/sev-common.h | 2 + arch/x86/include/asm/sev.h | 3 + arch/x86/kernel/sev-shared.c | 322 ++++++++++++++++++++ arch/x86/kernel/sev.c | 4 + 9 files changed, 394 insertions(+), 2 deletions(-) diff --git a/arch/x86/boot/compressed/efi-config-table.c b/arch/x86/boot/compressed/efi-config-table.c index d1a34aa7cefd..678fc4236030 100644 --- a/arch/x86/boot/compressed/efi-config-table.c +++ b/arch/x86/boot/compressed/efi-config-table.c @@ -178,3 +178,47 @@ efi_bp_get_conf_table(struct boot_params *boot_params, return 0; } + +/* + * Given boot_params, locate EFI system/config table from it and search for + * physical for the vendor table associated with GUID. + * + * @boot_params: pointer to boot_params + * @guid: GUID of vendor table + * @vendor_table_pa: location to store physical address of vendor table + * + * Returns 0 on success. On error, return params are left unchanged. + */ +int +efi_bp_find_vendor_table(struct boot_params *boot_params, efi_guid_t guid, + unsigned long *vendor_table_pa) +{ + unsigned long conf_table_pa = 0; + unsigned int conf_table_len = 0; + unsigned int i; + bool efi_64; + int ret; + + ret = efi_bp_get_conf_table(boot_params, &conf_table_pa, + &conf_table_len, &efi_64); + if (ret) + return ret; + + for (i = 0; i < conf_table_len; i++) { + unsigned long vendor_table_pa_tmp; + efi_guid_t vendor_table_guid; + int ret; + + if (get_vendor_table((void *)conf_table_pa, i, + &vendor_table_pa_tmp, + &vendor_table_guid, efi_64)) + return -EINVAL; + + if (!efi_guidcmp(guid, vendor_table_guid)) { + *vendor_table_pa = vendor_table_pa_tmp; + return 0; + } + } + + return -ENOENT; +} diff --git a/arch/x86/boot/compressed/head_64.S b/arch/x86/boot/compressed/head_64.S index a2347ded77ea..1c1658693fc9 100644 --- a/arch/x86/boot/compressed/head_64.S +++ b/arch/x86/boot/compressed/head_64.S @@ -441,6 +441,7 @@ SYM_CODE_START(startup_64) .Lon_kernel_cs: pushq %rsi + movq %rsi, %rdi /* real mode address */ call load_stage1_idt popq %rsi diff --git a/arch/x86/boot/compressed/idt_64.c b/arch/x86/boot/compressed/idt_64.c index 9b93567d663a..1f6511a6625d 100644 --- a/arch/x86/boot/compressed/idt_64.c +++ b/arch/x86/boot/compressed/idt_64.c @@ -3,6 +3,7 @@ #include #include #include "misc.h" +#include static void set_idt_entry(int vector, void (*handler)(void)) { @@ -28,13 +29,15 @@ static void load_boot_idt(const struct desc_ptr *dtr) } /* Setup IDT before kernel jumping to .Lrelocated */ -void load_stage1_idt(void) +void load_stage1_idt(void *rmode) { boot_idt_desc.address = (unsigned long)boot_idt; - if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) + if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) { + sev_snp_cpuid_init(rmode); set_idt_entry(X86_TRAP_VC, boot_stage1_vc); + } load_boot_idt(&boot_idt_desc); } diff --git a/arch/x86/boot/compressed/misc.h b/arch/x86/boot/compressed/misc.h index 522baf8ff04a..74c3cf3b982c 100644 --- a/arch/x86/boot/compressed/misc.h +++ b/arch/x86/boot/compressed/misc.h @@ -193,6 +193,10 @@ int efi_bp_get_conf_table(struct boot_params *boot_params, unsigned long *conf_table_pa, unsigned int *conf_table_len, bool *is_efi_64); + +int efi_bp_find_vendor_table(struct boot_params *boot_params, efi_guid_t guid, + unsigned long *vendor_table_pa); + #else static inline int efi_foreach_conf_entry(void *conf_table, unsigned int conf_table_len, @@ -222,6 +226,12 @@ efi_bp_get_conf_table(struct boot_params *boot_params, { return -ENOENT; } + +int efi_bp_find_vendor_table(struct boot_params *boot_params, efi_guid_t guid, + unsigned long *vendor_table_pa); +{ + return -ENOENT; +} #endif /* CONFIG_EFI */ #endif /* BOOT_COMPRESSED_MISC_H */ diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index d4cbadf80838..13a6ce74f320 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include #include "error.h" diff --git a/arch/x86/include/asm/sev-common.h b/arch/x86/include/asm/sev-common.h index 5da5f5147623..e14d24f0950c 100644 --- a/arch/x86/include/asm/sev-common.h +++ b/arch/x86/include/asm/sev-common.h @@ -132,5 +132,7 @@ struct __packed snp_psc_desc { #define GHCB_TERM_PSC 1 /* Page State Change failure */ #define GHCB_TERM_PVALIDATE 2 /* Pvalidate failure */ #define GHCB_TERM_NOT_VMPL0 3 /* SNP guest is not running at VMPL-0 */ +#define GHCB_TERM_CPUID 4 /* CPUID-validation failure */ +#define GHCB_TERM_CPUID_HYP 5 /* CPUID failure during hypervisor fallback */ #endif diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index e41bd55dba5d..e403bd1fcb23 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -11,6 +11,7 @@ #include #include #include +#include #define GHCB_PROTOCOL_MIN 1ULL #define GHCB_PROTOCOL_MAX 2ULL @@ -127,6 +128,7 @@ void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); void snp_set_memory_private(unsigned long vaddr, unsigned int npages); void snp_set_wakeup_secondary_cpu(void); +void sev_snp_cpuid_init(struct boot_params *bp); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -143,6 +145,7 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npage static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } static inline void snp_set_wakeup_secondary_cpu(void) { } +static inline void sev_snp_cpuid_init(struct boot_params *bp) { } #endif #endif diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 4884de256a49..5e0e8e208a8c 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -14,6 +14,25 @@ #define has_cpuflag(f) boot_cpu_has(f) #endif +struct sev_snp_cpuid_fn { + u32 eax_in; + u32 ecx_in; + u64 unused; + u64 unused2; + u32 eax; + u32 ebx; + u32 ecx; + u32 edx; + u64 reserved; +} __packed; + +struct sev_snp_cpuid_info { + u32 count; + u32 reserved1; + u64 reserved2; + struct sev_snp_cpuid_fn fn[0]; +} __packed; + /* * Since feature negotiation related variables are set early in the boot * process they must reside in the .data section so as not to be zeroed @@ -26,6 +45,15 @@ static u16 ghcb_version __section(".data..ro_after_init"); /* Bitmap of SEV features supported by the hypervisor */ u64 sev_hv_features __section(".data..ro_after_init") = 0; +/* + * These are also stored in .data section to avoid the need to re-parse + * boot_params and re-determine CPUID memory range when .bss is cleared. + */ +static int sev_snp_cpuid_enabled __section(".data"); +static unsigned long sev_snp_cpuid_pa __section(".data"); +static unsigned long sev_snp_cpuid_sz __section(".data"); +static const struct sev_snp_cpuid_info *cpuid_info __section(".data"); + static bool __init sev_es_check_cpu_features(void) { if (!has_cpuflag(X86_FEATURE_RDRAND)) { @@ -236,6 +264,171 @@ static int sev_es_cpuid_msr_proto(u32 func, u32 subfunc, u32 *eax, u32 *ebx, return 0; } +static bool sev_snp_cpuid_active(void) +{ + return sev_snp_cpuid_enabled; +} + +static int sev_snp_cpuid_xsave_size(u64 xfeatures_en, u32 base_size, + u32 *xsave_size, bool compacted) +{ + u64 xfeatures_found = 0; + int i; + + *xsave_size = base_size; + + for (i = 0; i < cpuid_info->count; i++) { + const struct sev_snp_cpuid_fn *fn = &cpuid_info->fn[i]; + + if (!(fn->eax_in == 0xd && fn->ecx_in > 1 && fn->ecx_in < 64)) + continue; + if (!(xfeatures_en & (1UL << fn->ecx_in))) + continue; + if (xfeatures_found & (1UL << fn->ecx_in)) + continue; + + xfeatures_found |= (1UL << fn->ecx_in); + if (compacted) + *xsave_size += fn->eax; + else + *xsave_size = max(*xsave_size, fn->eax + fn->ebx); + } + + /* + * Either the guest set unsupported XCR0/XSS bits, or the corresponding + * entries in the CPUID table were not present. This is not a valid + * state to be in. + */ + if (xfeatures_found != (xfeatures_en & ~3ULL)) + return -EINVAL; + + return 0; +} + +static void sev_snp_cpuid_hyp(u32 func, u32 subfunc, u32 *eax, u32 *ebx, + u32 *ecx, u32 *edx) +{ + /* + * Currently MSR protocol is sufficient to handle fallback cases, but + * should that change make sure we terminate rather than grabbing random + * values. Handling can be added in future to use GHCB-page protocol for + * cases that occur late enough in boot that GHCB page is available + */ + if (cpuid_function_is_indexed(func) && subfunc != 0) + sev_es_terminate(1, GHCB_TERM_CPUID_HYP); + + if (sev_es_cpuid_msr_proto(func, 0, eax, ebx, ecx, edx)) + sev_es_terminate(1, GHCB_TERM_CPUID_HYP); +} + +/* + * Returns -EOPNOTSUPP if feature not enabled. Any other return value should be + * treated as fatal by caller since we cannot fall back to hypervisor to fetch + * the values for security reasons (outside of the specific cases handled here) + */ +static int sev_snp_cpuid(u32 func, u32 subfunc, u32 *eax, u32 *ebx, u32 *ecx, + u32 *edx) +{ + bool found = false; + int i; + + if (!sev_snp_cpuid_active()) + return -EOPNOTSUPP; + + if (!cpuid_info) + return -EIO; + + for (i = 0; i < cpuid_info->count; i++) { + const struct sev_snp_cpuid_fn *fn = &cpuid_info->fn[i]; + + if (fn->eax_in != func) + continue; + + if (cpuid_function_is_indexed(func) && fn->ecx_in != subfunc) + continue; + + *eax = fn->eax; + *ebx = fn->ebx; + *ecx = fn->ecx; + *edx = fn->edx; + found = true; + + break; + } + + if (!found) { + *eax = *ebx = *ecx = *edx = 0; + goto out; + } + + if (func == 0x1) { + u32 ebx2, edx2; + + sev_snp_cpuid_hyp(func, subfunc, NULL, &ebx2, NULL, &edx2); + /* initial APIC ID */ + *ebx = (*ebx & 0x00FFFFFF) | (ebx2 & 0xFF000000); + /* APIC enabled bit */ + *edx = (*edx & ~BIT_ULL(9)) | (edx2 & BIT_ULL(9)); + + /* OSXSAVE enabled bit */ + if (native_read_cr4() & X86_CR4_OSXSAVE) + *ecx |= BIT_ULL(27); + } else if (func == 0x7) { + /* OSPKE enabled bit */ + *ecx &= ~BIT_ULL(4); + if (native_read_cr4() & X86_CR4_PKE) + *ecx |= BIT_ULL(4); + } else if (func == 0xB) { + /* extended APIC ID */ + sev_snp_cpuid_hyp(func, 0, NULL, NULL, NULL, edx); + } else if (func == 0xd && (subfunc == 0x0 || subfunc == 0x1)) { + bool compacted = false; + u64 xcr0 = 1, xss = 0; + u32 xsave_size; + + if (native_read_cr4() & X86_CR4_OSXSAVE) + xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); + if (subfunc == 1) { + /* boot/compressed doesn't set XSS so 0 is fine there */ +#ifndef __BOOT_COMPRESSED + if (*eax & 0x8) /* XSAVES */ + if (boot_cpu_has(X86_FEATURE_XSAVES)) + rdmsrl(MSR_IA32_XSS, xss); +#endif + /* + * The PPR and APM aren't clear on what size should be + * encoded in 0xD:0x1:EBX when compaction is not enabled + * by either XSAVEC or XSAVES since SNP-capable hardware + * has the entries fixed as 1. KVM sets it to 0 in this + * case, but to avoid this becoming an issue it's safer + * to simply treat this as unsupported or SNP guests. + */ + if (!(*eax & 0xA)) /* (XSAVEC|XSAVES) */ + return -EINVAL; + + compacted = true; + } + + if (sev_snp_cpuid_xsave_size(xcr0 | xss, *ebx, &xsave_size, + compacted)) + return -EINVAL; + + *ebx = xsave_size; + } else if (func == 0x8000001E) { + u32 ebx2, ecx2; + + /* extended APIC ID */ + sev_snp_cpuid_hyp(func, subfunc, eax, &ebx2, &ecx2, NULL); + /* compute ID */ + *ebx = (*ebx & 0xFFFFFFF00) | (ebx2 & 0x000000FF); + /* node ID */ + *ecx = (*ecx & 0xFFFFFFF00) | (ecx2 & 0x000000FF); + } + +out: + return 0; +} + /* * Boot VC Handler - This is the first VC handler during boot, there is no GHCB * page yet, so it only supports the MSR based communication with the @@ -244,15 +437,25 @@ static int sev_es_cpuid_msr_proto(u32 func, u32 subfunc, u32 *eax, u32 *ebx, void __init do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code) { unsigned int fn = lower_bits(regs->ax, 32); + unsigned int subfn = lower_bits(regs->cx, 32); u32 eax, ebx, ecx, edx; + int ret; /* Only CPUID is supported via MSR protocol */ if (exit_code != SVM_EXIT_CPUID) goto fail; + ret = sev_snp_cpuid(fn, subfn, &eax, &ebx, &ecx, &edx); + if (ret == 0) + goto out; + + if (ret != -EOPNOTSUPP) + goto fail; + if (sev_es_cpuid_msr_proto(fn, 0, &eax, &ebx, &ecx, &edx)) goto fail; +out: regs->ax = eax; regs->bx = ebx; regs->cx = ecx; @@ -552,6 +755,19 @@ static enum es_result vc_handle_cpuid(struct ghcb *ghcb, struct pt_regs *regs = ctxt->regs; u32 cr4 = native_read_cr4(); enum es_result ret; + u32 eax, ebx, ecx, edx; + int cpuid_ret; + + cpuid_ret = sev_snp_cpuid(regs->ax, regs->cx, &eax, &ebx, &ecx, &edx); + if (cpuid_ret == 0) { + regs->ax = eax; + regs->bx = ebx; + regs->cx = ecx; + regs->dx = edx; + return ES_OK; + } + if (cpuid_ret != -EOPNOTSUPP) + return ES_VMM_ERROR; ghcb_set_rax(ghcb, regs->ax); ghcb_set_rcx(ghcb, regs->cx); @@ -603,3 +819,109 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, return ES_OK; } + +#ifdef BOOT_COMPRESSED +static struct setup_data *get_cc_setup_data(struct boot_params *bp) +{ + struct setup_data *hdr = (struct setup_data *)bp->hdr.setup_data; + + while (hdr) { + if (hdr->type == SETUP_CC_BLOB) + return hdr; + hdr = (struct setup_data *)hdr->next; + } + + return NULL; +} + +/* + * For boot/compressed kernel: + * + * 1) Search for CC blob in the following order/precedence: + * - via linux boot protocol / setup_data entry + * - via EFI configuration table + * 2) Return a pointer to the CC blob, NULL otherwise. + */ +static struct cc_blob_sev_info *sev_snp_probe_cc_blob(struct boot_params *bp) +{ + struct cc_blob_sev_info *cc_info = NULL; + struct setup_data_cc { + struct setup_data header; + u32 cc_blob_address; + } *sd; + + /* Try to get CC blob via setup_data */ + sd = (struct setup_data_cc *)get_cc_setup_data(bp); + if (sd) { + cc_info = (struct cc_blob_sev_info *)(unsigned long)sd->cc_blob_address; + goto out_verify; + } + + /* CC blob isn't in setup_data, see if it's in the EFI config table */ + (void)efi_bp_find_vendor_table(bp, EFI_CC_BLOB_GUID, + (unsigned long *)&cc_info); + +out_verify: + /* CC blob should be either valid or not present. Fail otherwise. */ + if (cc_info && cc_info->magic != CC_BLOB_SEV_HDR_MAGIC) + sev_es_terminate(1, GHCB_SNP_UNSUPPORTED); + + return cc_info; +} +#else +/* + * Probing for CC blob for run-time kernel will be enabled in a subsequent + * patch. For now we need to stub this out. + */ +static struct cc_blob_sev_info *sev_snp_probe_cc_blob(struct boot_params *bp) +{ + return NULL; +} +#endif + +/* + * Initial set up of CPUID table when running identity-mapped. + * + * NOTE: Since SEV_SNP feature partly relies on CPUID checks that can't + * happen until we access CPUID page, we skip the check and hope the + * bootloader is providing sane values. Current code relies on all CPUID + * page lookups originating from #VC handler, which at least provides + * indication that SEV-ES is enabled. Subsequent init levels will check for + * SEV_SNP feature once available to also take SEV MSR value into account. + */ +void sev_snp_cpuid_init(struct boot_params *bp) +{ + struct cc_blob_sev_info *cc_info; + + if (!bp) + sev_es_terminate(1, GHCB_TERM_CPUID); + + cc_info = sev_snp_probe_cc_blob(bp); + + if (!cc_info) + return; + + sev_snp_cpuid_pa = cc_info->cpuid_phys; + sev_snp_cpuid_sz = cc_info->cpuid_len; + + /* + * These should always be valid values for SNP, even if guest isn't + * actually configured to use the CPUID table. + */ + if (!sev_snp_cpuid_pa || sev_snp_cpuid_sz < PAGE_SIZE) + sev_es_terminate(1, GHCB_TERM_CPUID); + + cpuid_info = (const struct sev_snp_cpuid_info *)sev_snp_cpuid_pa; + + /* + * We should be able to trust the 'count' value in the CPUID table + * area, but ensure it agrees with CC blob value to be safe. + */ + if (sev_snp_cpuid_sz < (sizeof(struct sev_snp_cpuid_info) + + sizeof(struct sev_snp_cpuid_fn) * + cpuid_info->count)) + sev_es_terminate(1, GHCB_TERM_CPUID); + + if (cpuid_info->count > 0) + sev_snp_cpuid_enabled = 1; +} diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 59e0dd04cb02..04ef5e79fa12 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -32,6 +34,8 @@ #include #include #include +#include +#include #include "sev-internal.h" From patchwork Wed Jul 7 18:14: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: 12363471 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,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 3A915C07E9B for ; Wed, 7 Jul 2021 18:32:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D7BA661CBE for ; Wed, 7 Jul 2021 18:32:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D7BA661CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C7C1B6B0081; Wed, 7 Jul 2021 14:32:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C53536B0082; Wed, 7 Jul 2021 14:32:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A7FEB6B0083; Wed, 7 Jul 2021 14:32:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0233.hostedemail.com [216.40.44.233]) by kanga.kvack.org (Postfix) with ESMTP id 7C8336B0081 for ; Wed, 7 Jul 2021 14:32:26 -0400 (EDT) Received: from smtpin35.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id DF71F181C77A0 for ; Wed, 7 Jul 2021 18:32:25 +0000 (UTC) X-FDA: 78336637050.35.7DBC3FE Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2062.outbound.protection.outlook.com [40.107.243.62]) by imf06.hostedemail.com (Postfix) with ESMTP id 4B921801CF8B for ; Wed, 7 Jul 2021 18:32:20 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=doL+zIZ4yEuqpus/jhB9DkXPkxBjrx9eO5XXhb/Z7A6PcR9IOYt92RYifN6LbXAF1iZF7PiY8I0UzosXje2WUhJorfo+c5dY8/VerrCbdueNqg3vyaWYkS0ITfzXA8OlEFB6ieiK6SbnksFSCYBmi8vm3l4Food6e2xcxmtMD+wf3ewXNxSoG6xORMw8S+DGGrbJU0VtyWwEBx8jmBKYH60IBf8xVap+VfyTbvoXf7mjZV3TjFFvTkNnHPfF4FGFwNduf7UByNcG08/CXzf9uj8bdY+9VHPLZTfl8UgjH3Ld9AE5xEIZAmxuYsm8V2ZONqvvaaA6wzdYyAbk+a4ATw== 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=QkU/foi/ZGV71ZkywRRN3v/j5D2IBhlBMTCjHQgFerI=; b=P3JSK83rFUt/GTmn8SCAnZRuo0lX7jnkY56VTExVUshk0hG79vb4UfShHukbZ91m6n5f1kB1dudSdAhCxJKTGj2twQ8RDS/snKR0ZZTkYY1MCVCjyrtb5zQOVbIpeMm/ALNk+AwVZIX8cigMC00CSmALqTAUJRCv986SdHN52lxnDTx7y/KjBk9LKn59aOpxAaPyQZt74zM1LEfEoBp3A4VB9aK0TYztXrIfp6BoKyxpAlXUQbk1ME1b6vjcgLox99MtnNtYD8kMPIg1+GqROgeC5FbeXkQNHQehwzKvEtRCT0vPg0e22oWwblkiszyJlug13xT1ND24+bFsSNuGHA== 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=QkU/foi/ZGV71ZkywRRN3v/j5D2IBhlBMTCjHQgFerI=; b=pB7AoplmyC8NdNZUATIZufN3Jl9fjnnHd/d8SyMtOZTy+T0LSEGqQ2Esnu2gIblrjH6uV8Iky2ZknuHSQVKP+gBJegEgNZ21POkjt+PlL0Qc2QUsap7aMz13Vmx8w1PUpnwAvPZ+bdnVCiAAM9Mvwi2S6QmyLtduIjkJcnvm2zg= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:38 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:38 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 27/36] x86/boot: add a pointer to Confidential Computing blob in bootparams Date: Wed, 7 Jul 2021 13:14:57 -0500 Message-Id: <20210707181506.30489-28-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:35 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 48e00a4c-ed46-40e5-f25d-08d941735c59 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: oc1brqDg3K/vhDGik7BgBdd/BMFcGfvg+Sh9B31fYnQbeYTz3t+/jdUi+W5hDum0+H8DX7uCXGGFrBGYJ/bkWhGf3MB8C2fs636vq6RWZDcN5MRUQOKWk93DnkxAA87uJ+qMqz3tI7YZIdQhk0DhvYqiMVGYZCKwyKtz4V9r193KZiXolGyG20NwQ5iykXcSi0M5AJVIqH+hnUcYQs/aE/U0viukms8Lg57CnB82h/EYOvTtJhtm5p4W8YVlkBPKjg8FUjhVywIj4JBDJmBuOXuoa3DIJQ2ULfcikQZLC/TRiLa9Chxu7Vz4bnQm0EdhUIk5sZ+Aq4/+dhbSR9ZU7DCmTVUsvdPWGyBVSJeVGVjwzHO9HEJsSGcX3/UI6KZiwY/ELmA/3PO+jZt9mYKoi3zePdHwDJ9gRzVWPaxGfKartSC+FP0CR4U71aXP+YclCaEWSR5q3sazBI+VSo85iVjesjjMTwDaz9r4FDGSKAj7Yv1Oacpwno11AMet20om81jqyOGyhNwEZYDOTomkDeZ9FRF+Zdj3zVmBhbQIWC3W8dBJCF+TopoRBgpzzP1wTT5l2AoJ2d5hEEV4fH83v7LMyxwC5Dk+9sR1HbCbb8z4Ijfb9Yu3vp1nRY0lJLb0k5298Uy1P72CLxdip2aGFj2prPAYRXTOwd2wwJ1hKk60wJ2MK1rAXNh+WQ5iulM3yRQjZI9NbMh/n7uM6/1PfA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: en15AkyelWFpQkmmfaXqVIMLG5dbhbaKJbdZxiC87GOQX6Y3nNNEfBQQgjtTS5ahZof9NrLgjFL3f5yGjSka1wFo7GDGzLkRadZW+D4eAkErKIUwmn2EXfba1SkCd3ddryJQbYFMKRl4DWB2gPwMtdclAwm2VAjwT9N9WmSvLxZJ7mwUw06b3zLcJE98sfwQwCFS5mKc+7y28ARlgYHQOqNXh6qp3AeygvETo+HG1TOkXQa9K5/wnnAVx+C8cNyFkccvKaXw0IYd2vm8rwFUkg/53/QBLBCbN2lv+bOgD088ru26xHmiqpSg5Xza/hu3dROUsbVbwefOihZzudYRJhuqYFTEbqnZjA5K2LaG6Qv4LBZBWLFpQcW/1goZNs09P8fPGEhXAUzAZYSzYxZmj0f0xlZ+cosBlM1O9GVaxPHVISnARCWBTt5X73irj34X9K2YIBhvN7W0eun4cBN8ZJlDoLio+4bwJ5ZtwWmi307WXtZsL9T3Xu6s+W+Z1gG5kxyluewlsQc19aFNqJ91LBlztnjVMGnm+vgvqrqL4ikjGqoFWs+B3Z3mAC5JtdC8tdh9dzBAVDcTCq7mbJt8iXlUD95VoHj4Gjbx097/OM0m3PqUDiNdB8SgN02qLmFvZR5x617yi/PmP/IGp1iwOLH3KX64bllUrLvUAXuH5w1Vtje0z/Pg42IjBieufkogny9i21TnUeYX3zc3n4mKv9iZr0sYLVu6M6hz2OAb03CVD5CJ/4L0IURpAL9tZH0OP1aQz99GXplMR+KPvd6NXOJ7Kz5+LPXzK2fVRWPJLGv69PI9SMqDwHi24iEBzdf689Lj3pEdnXqNp1M6+sanPyiJahJpznYMPhCkrWQdtQ8EGrUoCKfIrbEEHnl5kvkbIo+gmeSZAGL2f/OLtPeLWbuX+NfeNXhGT5F+H4wff0P6rwDHKJjEgeihwxkXHyifQzNNt4Yt/YKxBMBjrZ/A3IRZPARAWkxj2meoL8WzYb0AXYncxyfmM+L9jxNCLLJRroUf52vpaufbnY9B0pRomQhLRB9I3mFCKbAmQoyvDVZknxN2ZJhp9emeg9AE6d1yYKnVaS3yfUq2jARs5GncYFlUbY42aAHTUWdgQW1bGoqmuU1f5uU9miI+Msub4/OM+5TTJOmbYfQayqT32hnJzn5evUoUS9EAMMaR0AtaeiM36mIc8vllV/hUTzhFUT1i0aFlvSisoF/fQxXd+DkOqQh09EfgcOL3r9yN59iu9ficnk3mxRLo1Hay7n2wiNLU7dmKe/gl65jx4cgnpwKSIXUrY7xnunM0N0T9EjMXfXFphEO4h/plPTCjhTaJVQA0 X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 48e00a4c-ed46-40e5-f25d-08d941735c59 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:37.8797 (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: eIxn3/bJvwJZw9uUERcLQbtP/jF72HrkXdW8NscQ2yvEcjM7TgORm6wDkA85KW8RuSZKuYONqDtG5t+ZSbFZnQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 X-Rspam-User: nil Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=pB7Aoplm; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf06.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.62 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 4B921801CF8B X-Stat-Signature: fgokkwjx57bawatswaefe6bs3hqaooid X-HE-Tag: 1625682740-186099 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth The previously defined Confidential Computing blob is provided to the kernel via a setup_data structure or EFI config table entry. Currently these are both checked for by boot/compressed kernel to access the CPUID table address within it for use with SEV-SNP CPUID enforcement. To also enable SEV-SNP CPUID enforcement for the run-time kernel, similar early access to the CPUID table is needed early on while it's still using the identity-mapped page table set up by boot/compressed, where global pointers need to be accessed via fixup_pointer(). This is much of an issue for accessing setup_data, and the EFI config table helper code currently used in boot/compressed *could* be used in this case as well since they both rely on identity-mapping. However, it has some reliance on EFI helpers/string constants that would need to be accessed via fixup_pointer(), and fixing it up while making it shareable between boot/compressed and run-time kernel is fragile and introduces a good bit of uglyness. Instead, this patch adds a boot_params->cc_blob_address pointer that boot/compressed can initialize so that the run-time kernel can access the prelocated CC blob that way instead. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/include/asm/bootparam_utils.h | 1 + arch/x86/include/uapi/asm/bootparam.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/bootparam_utils.h b/arch/x86/include/asm/bootparam_utils.h index 981fe923a59f..53e9b0620d96 100644 --- a/arch/x86/include/asm/bootparam_utils.h +++ b/arch/x86/include/asm/bootparam_utils.h @@ -74,6 +74,7 @@ static void sanitize_boot_params(struct boot_params *boot_params) BOOT_PARAM_PRESERVE(hdr), BOOT_PARAM_PRESERVE(e820_table), BOOT_PARAM_PRESERVE(eddbuf), + BOOT_PARAM_PRESERVE(cc_blob_address), }; memset(&scratch, 0, sizeof(scratch)); diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/asm/bootparam.h index 1ac5acca72ce..bea5cdcdf532 100644 --- a/arch/x86/include/uapi/asm/bootparam.h +++ b/arch/x86/include/uapi/asm/bootparam.h @@ -188,7 +188,8 @@ struct boot_params { __u32 ext_ramdisk_image; /* 0x0c0 */ __u32 ext_ramdisk_size; /* 0x0c4 */ __u32 ext_cmd_line_ptr; /* 0x0c8 */ - __u8 _pad4[116]; /* 0x0cc */ + __u8 _pad4[112]; /* 0x0cc */ + __u32 cc_blob_address; /* 0x13c */ struct edid_info edid_info; /* 0x140 */ struct efi_info efi_info; /* 0x1c0 */ __u32 alt_mem_k; /* 0x1e0 */ From patchwork Wed Jul 7 18:14:58 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363385 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,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 C0A10C07E9E for ; Wed, 7 Jul 2021 18:16:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 6AB3061CC8 for ; Wed, 7 Jul 2021 18:16:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 6AB3061CC8 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 122C76B0074; Wed, 7 Jul 2021 14:16:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0D3396B0075; Wed, 7 Jul 2021 14:16:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DA3B56B0078; Wed, 7 Jul 2021 14:16:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0145.hostedemail.com [216.40.44.145]) by kanga.kvack.org (Postfix) with ESMTP id B38C76B0074 for ; Wed, 7 Jul 2021 14:16:48 -0400 (EDT) Received: from smtpin33.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 1E913183C0409 for ; Wed, 7 Jul 2021 18:16:48 +0000 (UTC) X-FDA: 78336597696.33.4DE2D62 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2074.outbound.protection.outlook.com [40.107.243.74]) by imf02.hostedemail.com (Postfix) with ESMTP id 97FE77002449 for ; Wed, 7 Jul 2021 18:16:43 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mmvPCbmUwu5znVAyNdzCvt0612Ps9R7c16S6DAXM3e24hT1St25ckX2H9MUxxBQEA7B2yrYlP0PXd6zEwcQ2evLCn1DOii73XE1qynun+/hRbuIVSEC1gESg2RmRsKVmlu6aCl5T20i9cbA8BAcf3vwWpnJwWeMnnT6fbaVU5BOdy5t3MZw9oEKvLg9scYE5Trw2bXtdweHfigKnR88T2DVjEUIuIxgq1x+C0kPRvz5z9J89huX7GPtoa0No/UC/3Ff3eCjfU8QqG+ic8C+a+MTfBiMTewUOAF/b3u564yWgAsViILvvXY6OY+S1TR2mCWmT5V7ZVjNTyJCe2elZLw== 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=uIuGp7KBHI64lwmkF6UGH2PkA4+umPxRd0Yii9FMJ5I=; b=YD7UzEsCNbFxjUVXM6sQ6SI53KU+FMhcWck4rGwlDlh6Sm4Tb0IYWHfxUUPXXxrDZvUEYQ7zHpDBOfWAdl8laglqepGX4o+5TQ5qpb5P59tjRgMuqU/mKcvlZC/Qc174/z+00PSVuUhyzp6rycx+ihLNvfKxyMiemKpMPuv6erTdSvmIJWjL1cqg3o0UIv0L0QaHpR+CB3Y/QS3lbaOA0lXifg0Ayo4I89F6YPP4lJgmihMv7tzt8EwzkJPK32oNEV6stYpDtLkeKFUX9+ch4/DCwNSO3NEia4mdqaDh1W+HLxuotT2s3i/T4Q2YNx6k2ZmJ1DiSu64HtkkbQjMYBg== 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=uIuGp7KBHI64lwmkF6UGH2PkA4+umPxRd0Yii9FMJ5I=; b=x7gPR0Qwv88gMM6gEhFLAiJ5clU0HCcVCCQZGbiyXmERfKnbHPW1SWFlESzEeEk8DNmpBOiG3tFxGyhk6/d89nR2+3pQRQedEw+ydYrYlM5AtxM+U3QALP8edUZOkIRTzw5++BSRVdozN3IzOqR/gQNWNmAezz5Lr8vPzCYyxxk= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BYAPR12MB3527.namprd12.prod.outlook.com (2603:10b6:a03:13c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:40 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:40 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 28/36] x86/compressed/64: store Confidential Computing blob address in bootparams Date: Wed, 7 Jul 2021 13:14:58 -0500 Message-Id: <20210707181506.30489-29-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: a6aa2024-a2c1-480e-cea1-08d941735de0 X-MS-TrafficTypeDiagnostic: BYAPR12MB3527: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:260; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: sewOYFtgPvXw9n2on5DnKOII5sU2GWPCau9usWUPbe5CiEEDVSP6g4texRxQR4HmLVPfUCNn2Hi7mVHL+5qZalc/vt/R7DlbbXjL9UKUgRvt+vIg6hmgwIqT3oL9xxGLaeGtyJPxhoQ8hREoh/U0TgXD1ElLoDijEMhujgiBnLsx99QCxirMTG9R0CozxflW0t9yZ4ZXPkRU4872HypsHcqeO9XrWwTI/sO8YeTStqKk4bCsb873K6X3bgv45G2+L1/Ty0vgpCVUHUq8HXhGBq7z1I2VMNQgkQcFSKtV7mo7yZofw+tJ+GqVNb4149MofrEaJLn03yQWo+I0imuxbFTSKWomIjHShHbDtx9XlnqfUEyQTE9iBrW+SBYADEvMYsplSShOXGFOrlTiHTVfp9N24cMdmpHADYAaWHJuG8/EEMY8/jll5RQUd3ZEDJydwa/I1H1E9gHvnWVwjVkjc49L8HDn36csE8bzSmuhdxg75bhTL5z4n17rQd4INl+evzUDQLLoWSe4Jat5PTXTn8HzWddohDhnkCcfEGh5yW++y+8933MrmRzSlbil35JaJhQ0Ha/iCZgHy9NclxFwaN1/3VVGTIQmUzOGYk8zZBPNEV9l1T56eFhHs/M5emGAj4QvsHBhkcdF1FbfdBPF5vBfDRcB52DdHKDuQZgNpLdmpgsS7XI9q3IRmBkVwX6vvXBam0XH1gyWFZpDM+2zBg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(39860400002)(346002)(376002)(396003)(136003)(366004)(26005)(2616005)(5660300002)(478600001)(316002)(44832011)(6486002)(1076003)(66946007)(66556008)(83380400001)(86362001)(186003)(956004)(66476007)(8936002)(8676002)(52116002)(36756003)(54906003)(2906002)(7696005)(38100700002)(38350700002)(4326008)(6666004)(7406005)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 9GaHpQX1uHGkl5nlH44yp0wW6wsVFSM6CWszinH82RaqUgZIYinulM3SapWFwpeMkctKpDIAS0IMFcYRUdqEkOs1Fyl6Z/92Ni9TXcm2vnMibXUXgQWQ89FxYMuPuXs6KzKfXSmWQ0Hgh899J6xTkOeZrD91B6dx9WY+U3mz0Nr0VFBnT8hUlknCahjYy8H/UhhhjgXifONcbEkm5RTw7OOhEVPhncwal3faPy0h20xQ/XVr5WZgv6qiZlbzeyph3FtWlm3/UaI9wIu3/FJdU99tEUJjiBmYJdwOAWR6iCT0a2KlJFdxJpzDjrfkQtmjDXLBfE+kF9K4S/A0GbijxTlf9pRnyRfK2aoHpWJAeAk25GXPr0onaWoWMLEIgZSYDRktqMcawFICBzvPidLUzlXds9ySxztoyXjmLBXRRf0Gmi//lyiIffiEkyON8RAwwgtD1+yRDbaKvYksPb2G4H68AoB/ub4JMDdLoBoAXgS26BBftsj85cAfQNVIG/Icps7NZtN9qyq3jrjnSA1pBo7klxeWVKUEb0i9jTWIZYkiBYIxf2ttLcmEi9T9/e42lPoJWH6zB3acmFS94TOkOzitFNilMAs04stMmnb8zA7bS1ZBh5lBjOWNKMhb5wujStnFm1qoBmHSg9tE45rIYS+k0+tcxbINBLrES7IqaNxfiAfluvU+LwEAM5lqq03euylv1FjIurGWT5Ufl1945TWO8HgBdlXONZVCL/lx88fEHl7Y6Wc5vo9gnCU4OfAhP+0wihnnP/Qfk/Y3mGZYH2es8JWcYfZHFeno264jmQe4+r3k/SHdnxDVwXiFKP1SYpcJ5/C//Z/VJ+7WpReJ+tyCVqJnYI4ckjJh088ElWSmXXwK35ugvADwQRbw1PSKpi0oDDiQjio9NWPwcBEcYaJFVFzaNGV8ECg6NiLyRKV8rkqI8ChL294TvUiDb3DBw7Pv5mDoUtHnV1BPfGcSF8tLPHvtb45DlKd5J01M+U5mFGOZqE4/yGKCmnReDD9UG7Yyc6flE1QRSJGbHFPfVrtzm+ejAKQ0xlJlHMr86eHMNyL//E+bE4MIfWtZ3Bz/xj7K/acKVcdGwXJ2roeYEHZR10awFCVcxYSQjjxp2fK1LBK2dpJIQb6BZ+8smeKfEtLtdq/K8GvTM6ES/2RhGhf0sPWOiljjkL7oGEcSIU6wk6l/zXkXh69ZaYNsoNvSjeXzCoXL2zx1rMGERkBfw6eyRG7cYAm+QGqlHRmbjO/ipjnrouHzjP9FrXGD8tshLvJm0djX8QutpD75RSWTZ3xkOmU/LauboxXv7Kdi2nECzWTkkBqG3EpVdszxesBd X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: a6aa2024-a2c1-480e-cea1-08d941735de0 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:40.3483 (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: Lsxl3ONComGNhuhU3ShqtB3UYIWuiuQggRQ29ZBM4sKPMIUzy+uCG7q8gi1E4+fIFyXP9EMbZftPj12z1hgu8Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3527 X-Rspam-User: nil Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=x7gPR0Qw; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf02.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.74 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 97FE77002449 X-Stat-Signature: h9rcp7t7nfttwk35x3dxkphbrdxwqkcs X-HE-Tag: 1625681803-95596 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth When the Confidential Computing blob is located by the boot/compressed kernel, store a pointer to it in bootparams->cc_blob_address to avoid the need for the run-time kernel to rescan the EFI config table to find it again. Since this function is also shared by the run-time kernel, this patch also adds the logic to make use of bootparams->cc_blob_address when it has been initialized. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev-shared.c | 38 +++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 5e0e8e208a8c..23328727caf4 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -820,7 +820,6 @@ static enum es_result vc_handle_rdtsc(struct ghcb *ghcb, return ES_OK; } -#ifdef BOOT_COMPRESSED static struct setup_data *get_cc_setup_data(struct boot_params *bp) { struct setup_data *hdr = (struct setup_data *)bp->hdr.setup_data; @@ -840,6 +839,16 @@ static struct setup_data *get_cc_setup_data(struct boot_params *bp) * 1) Search for CC blob in the following order/precedence: * - via linux boot protocol / setup_data entry * - via EFI configuration table + * 2) If found, initialize boot_params->cc_blob_address to point to the + * blob so that uncompressed kernel can easily access it during very + * early boot without the need to re-parse EFI config table + * 3) Return a pointer to the CC blob, NULL otherwise. + * + * For run-time/uncompressed kernel: + * + * 1) Search for CC blob in the following order/precedence: + * - via linux boot protocol / setup_data entry + * - via boot_params->cc_blob_address * 2) Return a pointer to the CC blob, NULL otherwise. */ static struct cc_blob_sev_info *sev_snp_probe_cc_blob(struct boot_params *bp) @@ -857,27 +866,34 @@ static struct cc_blob_sev_info *sev_snp_probe_cc_blob(struct boot_params *bp) goto out_verify; } +#ifdef __BOOT_COMPRESSED /* CC blob isn't in setup_data, see if it's in the EFI config table */ (void)efi_bp_find_vendor_table(bp, EFI_CC_BLOB_GUID, (unsigned long *)&cc_info); +#else + /* + * CC blob isn't in setup_data, see if boot kernel passed it via + * boot_params. + */ + if (bp->cc_blob_address) + cc_info = (struct cc_blob_sev_info *)(unsigned long)bp->cc_blob_address; +#endif out_verify: /* CC blob should be either valid or not present. Fail otherwise. */ if (cc_info && cc_info->magic != CC_BLOB_SEV_HDR_MAGIC) sev_es_terminate(1, GHCB_SNP_UNSUPPORTED); +#ifdef __BOOT_COMPRESSED + /* + * Pass run-time kernel a pointer to CC info via boot_params for easier + * access during early boot. + */ + bp->cc_blob_address = (u32)(unsigned long)cc_info; +#endif + return cc_info; } -#else -/* - * Probing for CC blob for run-time kernel will be enabled in a subsequent - * patch. For now we need to stub this out. - */ -static struct cc_blob_sev_info *sev_snp_probe_cc_blob(struct boot_params *bp) -{ - return NULL; -} -#endif /* * Initial set up of CPUID table when running identity-mapped. From patchwork Wed Jul 7 18:14:59 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363455 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,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 40C72C07E9B for ; Wed, 7 Jul 2021 18:31:27 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id D5AE561CBE for ; Wed, 7 Jul 2021 18:31:26 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org D5AE561CBE Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C2FB56B005D; Wed, 7 Jul 2021 14:31:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C07786B0070; Wed, 7 Jul 2021 14:31:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E48F6B0071; Wed, 7 Jul 2021 14:31:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0075.hostedemail.com [216.40.44.75]) by kanga.kvack.org (Postfix) with ESMTP id 7DC0E6B005D for ; Wed, 7 Jul 2021 14:31:26 -0400 (EDT) Received: from smtpin33.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id DD9BB250D5 for ; Wed, 7 Jul 2021 18:31:25 +0000 (UTC) X-FDA: 78336634530.33.7FD1FBA Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2045.outbound.protection.outlook.com [40.107.243.45]) by imf18.hostedemail.com (Postfix) with ESMTP id 8310E40020A9 for ; Wed, 7 Jul 2021 18:31:22 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PML/qSV2+5/fvJNAepZGut+0A47b6bp5OrX4br4+nxiY+aaYuEWFWcZyT8gF1yZ8/a8SUzbdOMqfVp8hpKl4Tr67t3txmlppPwt8RGNUHina3JFT/jY5SYLThw9DhrQC6TDaf4/Sv7q8J4et0oqwpaekCDLiv0I6o9yy4lOA2g/qJ2/KvP5K9sH294Rw9zqj3vIZTFVXP5EhhwyW15JnNKijsIhPZjfKK05G7xelLl4B0bSaT0AzpIjK2UYEb9bC+28ICiCqf/GLM8bY41Nwhc3GFlVCgM1PCfn/qHOn6F0B1fVchLUSgmfrfJ2Xs8+Km/OWMJwcbQ3q1j7Lvcs1GQ== 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=NRNMl3IXvWwOUax7hMfK5EO/9R9ig4gPmMuNhf+5sf4=; b=iLxM1E/6JVjXOU8d7bc/LV0UnoDMx6rloGSnUI8ptCVZouiVLBKY7PJ1W6ckQfl92aX//JQccbmsR5rO0UYpdcfw/JZlbXmeMzkdQAm7rE2xayCbg3bUhvAOzVLWXbXUCRqXyvOWCyYk7ZV8pz0gm0EYUPn4dCZyIRAs/14Nqk1s9rxtucNQuc8K8UMW1amxDx30clbwc5RhmoFJK/EfdXefhhBq5Nosuj+5nJ/+TNwkQ4L25N1Lo1AUwP20BISD65xWkgHBP1ZsEQGQSjykBlWoGU6Qq8SifVvgQfd4Svr3fYUNzqapWa9s5EM5tSvrLTB7fdTFyrN8PReeNqkTAA== 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=NRNMl3IXvWwOUax7hMfK5EO/9R9ig4gPmMuNhf+5sf4=; b=FFre4MGOuW3lT47d7JqPvTU7MCf1KAYJoagbAb10c5uMCVbfYorABZqagFMmFRERUfqGWUHXMjC7HEXt65HYtkBvXidgemdX4R49DgsOKqKxGuzSYJt4w/HDG29OTQlSbSn9PCUvCmu5DtA4+v+hyP7yC9CbW0GzmAG4zcUiRXI= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:43 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:43 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com Subject: [PATCH Part1 RFC v4 29/36] x86/compressed/64: add identity mapping for Confidential Computing blob Date: Wed, 7 Jul 2021 13:14:59 -0500 Message-Id: <20210707181506.30489-30-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:40 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 3b1199ba-bfe4-4e26-6ec2-08d941735f9c X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: oaCn67UmncoOc+Hy9XOkkvj/CvAmCmp5j9gjhEfKqtqzftPfbKt+MJd2hlGc9ItAubTcyF/EP8eWFIjPUb/4E9p752/lVC+8DlEn17auBJ6rQKQU8qR2Z+JK4Br0pdEVtGAFnYjkPdLvtrBK+WKfOeDXDjVFSLZZYhknYokE3jpeF4Fv4cDEXlto4CBAvxjW5Sq9LSy8LKEs/qHdjJaKzNA+UMFV3SJWzxD2ap3mJbAiUjTy6lkTzHiLg3LnJQEWwO/gj0HmUlbp1UoVe7+CmW6YVtgckxToqjglDssqvZprYAAKWJcNzC6eDdMMEJWBEvCV0BxEIBDN//vSKpNnU/Q/JIRePM8inZ4y2+E30nR+zq6s3e/KwQfOgWMlwB4bhguAdyFNqL0WBCbqzpi/fsnEP/LtzV+tge5lHtmMW/T/cEISOZCvEpe6KQS8OYTHn5MuX1YxCxIbEiTF/PM4pUrlH2hpp2rAKPbDQYGh44CU8f37HN4RSusmBZ3EgKXQ6tFcK+ohWI7fNRXIG7doBNqFRg0XiVIk75xXV4u1M8Tql5iZVC/GPmPf3LIeWPcQ45eZjyjMLOmbqciLSy9SOa6QiLHvAVWZhPsziCMzm+uEe4nS6KemkKuDOlgRhJOK/oU/VsRJUN4HDp2R1Vcr1QseCsZ77YPRmifh/L0PbdPfEjQPfNV6BjimUmPAi6kY+aOz9crgQGJghXecU6J0oQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: a39YZzbwmD3s4QobiatcDUkIoQUJZ512OXaJiUSmfCjtYtUiugcvoY49LYF5dYer0Su+IuJmPg13bbCZCWVBOA3vhDX16BUgCwaunr9gFVoyM6Rd0THPNyehDpFzFLYtWupzYh1L8ltwsoatxc67USoRmJ988vv9/tA1kmYbbSDfsHGEqJ19a263mSw0D5o8LuLFiTsn3rGoclre9JUSjl4JWhpYcfIqW8mp6Yqt5xtXixxe5iLqHN7teZyKp/un2cXb+2l0as41SyeW/AY1Dvywmb/5vgxgrWLP6SfuFWtVQE28sIZNPEN+3aV/2g4mDWHUaEKkQTp1zBPEM4x/YHcx5XDY2s0NSTwpHX3R9efaDlb94E4DZp25bfpUaqXAZKDyaH5nJIhGec0dcf2JKMNc473lUQASQMOPw76pYBWZKrB1y/FUJ7if1L/ghmnlpDIO1hqvxeWGXkx8YtnSE1+6eE3G2dSZDLlKIeI3f6QcjtUPqt3LoVGLXkTTMnHyBEejD+GZY9ciH9/w+P0QLt0K+vZgq0lcE/3LaMqRw/0izSu9E+39lNI0BqyXAqQnCmrUnt/eA7awiW7NXIgspCa/3Z/czSuLN4jSEVf9lRD5aSk8Ap1LXQhqNMUH5sKtHm0qGxqzqMfcRUQYHsYx0nWdsdxS4dKzTtYmbYmiM4XfnC5LoOsNZQdpizH63WpWjnU0+Nw6XX5fm9KzBdL8lKrgTeokSqCdzwHjgC5vKBWIFaoAQuaU0nYN3RVr5m55pxZ+r5HDKqCL9YMc7f1Y12wLxYHBCY8ZBpT43bIT1YAA5BKrgYqurIdd5XmyOwSllaiz5oxF+fQ1SXqQFfImXSQVdp+2cEwDYKOLwqu/eDiz7BwUqMCz03sBaD05Yx/w4AteCDGUJPtXIX6lN4u/KM/xpnApzUxWp8s5TCmCY8WdSL8wV40ZLOeN++CVf8nbjiSoS4wOaFy1NTULmqMATKLAf4LSlyB61WEqbdQINIfLu5oJfxT0FqVd+l/wlmDwZJLUIxlKTGW95i/tudVFEprAinQTsdNHW1WU6aNmA+MqsO0zjelTvv0uwZKEZujIw9D9n+lPKLJFYGW3fXAazVhuSs7WmlEP94yxJ3bKvcAqOLX65eTTXbQryy8+nMA3YSUnjGjwaAAZ+rk/id65irKRV00H2b+clN+0pdQx5o3fGSX5y1wU3FEulL0eUjc5dvqymaGCxDmAP7mm4FRvJU29On8HXJ8YNkzzPqjRqPxD2pOsJiuexrklOoCsU02dXs8vDfR35/JTM5kvH68QWEJe1PdCCk+miwl8rhCrjedRRO3H0d0gdMi0edGHKUwx X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3b1199ba-bfe4-4e26-6ec2-08d941735f9c X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:43.2676 (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: 51xZPWlTt+/V+0IsJPh/rOZWEqKMTQc6ePVfKh5L/+z5ZgDjbD7Ew9U/bvWzS1O+mFOA4OsrpoASx3R1T6uy9Q== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=FFre4MGO; spf=pass (imf18.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 8310E40020A9 X-Rspam-User: nil X-Stat-Signature: ekbp6npwecyrqjgsfx49g7gfc4kgyp7u X-HE-Tag: 1625682682-223605 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth The run-time kernel will need to access the Confidential Computing blob very early in boot to access the CPUID table it points to. At that stage of boot it will be relying on the identity-mapped page table set up by boot/compressed kernel, so make sure we have both of them mapped in advance. Signed-off-by: Michael Roth --- arch/x86/boot/compressed/ident_map_64.c | 18 ++++++++++++++++++ arch/x86/boot/compressed/sev.c | 2 +- arch/x86/include/asm/sev.h | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arch/x86/boot/compressed/ident_map_64.c b/arch/x86/boot/compressed/ident_map_64.c index 59befc610993..91e5ab433be4 100644 --- a/arch/x86/boot/compressed/ident_map_64.c +++ b/arch/x86/boot/compressed/ident_map_64.c @@ -37,6 +37,9 @@ #include /* For COMMAND_LINE_SIZE */ #undef _SETUP +#define __BOOT_COMPRESSED +#include /* For sev_snp_active() + ConfidentialComputing blob */ + extern unsigned long get_cmd_line_ptr(void); /* Used by PAGE_KERN* macros: */ @@ -163,6 +166,21 @@ void initialize_identity_maps(void *rmode) cmdline = get_cmd_line_ptr(); add_identity_map(cmdline, cmdline + COMMAND_LINE_SIZE); + /* + * The ConfidentialComputing blob is used very early in uncompressed + * kernel to find CPUID memory to handle cpuid instructions. Make sure + * an identity-mapping exists so they can be accessed after switchover. + */ + if (sev_snp_enabled()) { + struct cc_blob_sev_info *cc_info = + (void *)(unsigned long)boot_params->cc_blob_address; + + add_identity_map((unsigned long)cc_info, + (unsigned long)cc_info + sizeof(*cc_info)); + add_identity_map((unsigned long)cc_info->cpuid_phys, + (unsigned long)cc_info->cpuid_phys + cc_info->cpuid_len); + } + /* Load the new page-table. */ sev_verify_cbit(top_level_pgt); write_cr3(top_level_pgt); diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c index 13a6ce74f320..87080bc4a574 100644 --- a/arch/x86/boot/compressed/sev.c +++ b/arch/x86/boot/compressed/sev.c @@ -123,7 +123,7 @@ static enum es_result vc_read_mem(struct es_em_ctxt *ctxt, /* Include code for early handlers */ #include "../../kernel/sev-shared.c" -static inline bool sev_snp_enabled(void) +bool sev_snp_enabled(void) { unsigned long low, high; diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index e403bd1fcb23..b5715a26361a 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -128,6 +128,10 @@ void snp_set_memory_shared(unsigned long vaddr, unsigned int npages); void snp_set_memory_private(unsigned long vaddr, unsigned int npages); void snp_set_wakeup_secondary_cpu(void); +#ifdef __BOOT_COMPRESSED +bool sev_snp_enabled(void); +#endif + void sev_snp_cpuid_init(struct boot_params *bp); #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } @@ -145,6 +149,10 @@ static inline void snp_set_memory_shared(unsigned long vaddr, unsigned int npage static inline void snp_set_memory_private(unsigned long vaddr, unsigned int npages) { } static inline void snp_set_wakeup_secondary_cpu(void) { } +#ifdef __BOOT_COMPRESSED +static inline bool sev_snp_enabled { return false; } +#endif + static inline void sev_snp_cpuid_init(struct boot_params *bp) { } #endif From patchwork Wed Jul 7 18:15:00 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363561 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,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 D55C9C07E95 for ; Wed, 7 Jul 2021 18:32:56 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 71E1261CC9 for ; Wed, 7 Jul 2021 18:32:56 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 71E1261CC9 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 5C5A26B0083; Wed, 7 Jul 2021 14:32:56 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 59C576B0085; Wed, 7 Jul 2021 14:32:56 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C77F6B0087; Wed, 7 Jul 2021 14:32:56 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0068.hostedemail.com [216.40.44.68]) by kanga.kvack.org (Postfix) with ESMTP id 0C0AF6B0083 for ; Wed, 7 Jul 2021 14:32:56 -0400 (EDT) Received: from smtpin39.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 6C3B318026584 for ; Wed, 7 Jul 2021 18:32:55 +0000 (UTC) X-FDA: 78336638310.39.5C9D0EC Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2065.outbound.protection.outlook.com [40.107.243.65]) by imf01.hostedemail.com (Postfix) with ESMTP id 17FD35007506 for ; Wed, 7 Jul 2021 18:32:38 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=bbmcIyYycTRvJFwERY6PYuXOcOKjHYhTD8aYin/j0o0lykZXqMBrfFLHo8nFPpVoTw0PS1jmUSYKRoyyRJcnjsP0GfkU0FqaVUkgyYvAmGBbMz4zNSPB0Wn+VnqvyZDB/g6wrru6DCfBwGQGbAI24BUV1zS646pTsFaCL/gacGuQzT8beZ91ksfzJetI5BDVnmNoYhtiZfPFs+Rk7pOgpxrtacvrWW6Cr4hvfZgqRXP3v4ijRwA70N5NttxTZ6W6m5pl41oGGuGwM2IBNBl/QJx0pX1unRm15P4WkTzTREOUvJZbH8mYqzIWmUAxz7jGRYdX9zLmbTi1yL7zsAvyhA== 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=DCX9j8VzBE+xI2TTVKjFIAC7uhiRvxIGP2aZiYXM0cA=; b=fQtyIDjK8gSAHPga9MLLt7FEcBIZejQ8u04Lu6IYMt5VH+ghka0QaCrZVZbvbee/nvYC81SdgoWnNSEhvOr8avDujw1YiWXy/Nki0ibcaPxe/KmTNc6Nft338FgxPMSJNef/4DZesrIEWwO/3LtKIpqNNDMLhPwItc3OSMlJ+8YwG4a/0VcF3Nfd5CAN99jcbp2poTJ7j8zDEosUmKMRbvj0PS/cjpvGna2nJdS+Tk4pIXLG7UZjezPTchNzS3+cPdtoJGitLtgvo7ThJepQb3cBbBOT71ZBiA5D8h0LfdBhdIEAuCLG2G7N0+t/2y/rfdDnMrDAMo+tWWztGE7xFg== 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=DCX9j8VzBE+xI2TTVKjFIAC7uhiRvxIGP2aZiYXM0cA=; b=GbtOTZcSrIKQIJavMY8AVoJnMxgUAfJMJdKoe3jsJ1sWOikE3/8MhdYUXn6kjUXYmAvV+ylA98iVlJTH//kCt13LlLIyiEyWhMRQ7y9s0KGGhLlYAqoBJ4Ck7puog85UuebdlwJiMa3Z3KC3k6rXP8JtVRcZdGBsGzk+QCTJ6OU= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:45 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:45 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 30/36] x86/sev: enable SEV-SNP-validated CPUID in #VC handlers Date: Wed, 7 Jul 2021 13:15:00 -0500 Message-Id: <20210707181506.30489-31-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:43 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 010ce0c8-6592-45c1-61f4-08d941736110 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: bmIfSiwnQYoB7W03qR//iVeHdKGPMYeKK+MKz9vkxGA6LRGXNx80LlKYB//z6+TbYRQ66Sp/OBqWBzlIyr9Rt2J6O02v9F5eCmfLZRO7qzU2dOjwrM2jMxQjNsEz8haR53x6x+dryU3UxVhxd1li6gTVPJCl9CGUB6+7g+LvaE+ncBADWLHgCUKLOzj8erx9+wYYVS5ZaiaHPHQlti951yTXB8evthMPP7KQxKziktrECzVCk7x+TC1oEjNdWJMymEcd+/FmunecC6xkkpjJ9TypI58UZ3/WhxJjAbWH6YPtPL2Q3gkVp35XtSAPGrpJm3nkzsRFLPeCK/zfXpAy5X9FmvfnPM+K0UBELkmzAhjNAcrR59O3shwbEwpWZviUWbzzoGGt9tWygTz6SUd2GsfQH1xlM5C/98X41NFMi1T/torrlHpldg2dxEP3UzBFw1locARK1YLIhErpOajwf/4QANOe1MjbWwuMg3wDhxiLjq32nr56Y3/SxNcMTIi781p1Y0sX+Fz1FFoVq00yklkxor0VgkSOk+lp5/aOb+6k3DxcgIiAuEiaa5w/v7+WPeM96CjeL/lUgTLURkHHwunpdFuwYgHuWKSzdrUxlpEyCl5IcP5pxN/9nJ6zNZpitYaf6Lkm3WM1glE9dwd57KYP6PK8lM8o3upXW3gSRnsA9zuTxNnrxCTYS3D2HcgDKFLVWZyEpHHICTBm8vxsWw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(15650500001)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(30864003)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: SXbL1FzXe0BAP4wT7l26VUk+znF17NWavtWpc7ytf/axMIW3D/2GfFYEbSrFIFQAwUsw+3cK7PmFcs8ckG65sW9XW7+9fZs8wSFFGs7dbBWgtEt/OE0WMyZICZzR4Tn9+N2PL80+f6c0JEA/QNA4GjTbf8e2xKUArItu/YPvA+VJ3Wd4hKgC+SqWXEoTN2Qs4/Ru6e2Ki/23UlVakHvTtp0KDckc5NUXVrVN4WcuP04JEW5Orp0CBPpUFiV1gX4vA28sWwLVT1r2P0K4yxrcXS9HdDt5fkjbq1P5YkkBHX8iwG6L2ht62+XIAopeimIU7DPBLkZJYH083Ae+LVtb+dFUgV3WhVWLmvT30H9YwJ+HJIFO0XVi28935XJMWskXXDXxZE1XHJJjkRyYVg1e40lGZXvpeItd0Hah4XkFuYyVOZHZmhUaNhxqkePQc8mzrPysMJFgCkSx0dJCZSaFC+mEku/QwZUZxav2lLZdebtnM9fa74v98RgwuaiaiDvdDrUdKhckdRmCAnDOkqEu/7YaP33Kg6EVZiA49N6whpkd4vBOONRElCZvr3UbmP9FkUlo0aiPNkaM9uTrfPEzVvp2Md71vHssNTETpkl7JqAJpTBf+DcLZRkn4BmaniJExrcB4tPrZ0XcYZ3/8pVtAE/ZDST0upSRMvVcBwqbM1CHscqghRNcjFSSnkT/gxqLosfUW+OXei+BcamPspUYow5TIUgx4AP55V4TK3F0wiFQtYsxd+7uCMlwvvJorL9sf5YTPJPAT8Ymanl62ZfgRorHGBRKJbCxQWdKcHKr3O6jwy6pUKBA+cCrLsMWlr6npU8p7qtcCpBLEALun8ist3d3d/OdDB0if1wrB63+pg3Va4eU6MXbAR0hwV9K0fr4zEappTeB/hpOuqXtD7hu1jesiYfhnu+qYqE0SVGKDAPDssnI1BoJU7RoVD6DQaTJWNHvFgg4Uo4PABTYaZ+IkJHXnVIEduImYntT/Dbo/yg7S6vSmwYrXDUDSLncQ1TRQgWBw/OLOeHWHzT/RRTCRJmQY77pU3Ax7gaNsqypZDZPn2jj8SblqxUGJDO92oWlsQ3aTUxKL8Cu/1CcjygemMnWesH0X95qwZPfpyZt4Dt+T2NxxvH6E9RlqDQa07u63usE9en72U9E5PXme8ygnH7Qbe5B9VuvwIDh4kBPZgDaYruo9WkawGW1m7vU190rdSDEiy6VES/MWrB8FQAy/j93ozQJdgQoYf6jvdeRXi9a7GOckXqZhAm6ZbGkM0AH2rnA6muV8ohsJp2970Y9Qe/3dwwA4yXJz67TD5O1+SN1ZuEc0rG6tTw/gqurldUZ X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 010ce0c8-6592-45c1-61f4-08d941736110 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:45.8032 (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: CH85+XoDjSD3G3RX04gVaK8nuSYgud8UKXInkmVC7D8b4/GL/LVSdlj4ljAHaM1wN7uRcDUsPsfx1TSW6H+l1A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 X-Rspam-User: nil Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=GbtOTZcS; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf01.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.65 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 17FD35007506 X-Stat-Signature: iearxjpn9555e9jt8gjuo7kowfxt3khs X-HE-Tag: 1625682758-143846 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Michael Roth This adds support for utilizing the SEV-SNP-validated CPUID table in the various #VC handler routines used throughout boot/run-time. Mostly this is handled by re-using the CPUID lookup code introduced earlier for the boot/compressed kernel, but at various stages of boot some work needs to be done to ensure the CPUID table is set up and remains accessible throughout. The following init routines are introduced to handle this: sev_snp_cpuid_init(): This sets up access to the CPUID memory range for the #VC handler that gets set up just after entry to startup_64(). Since the code is still using an identity mapping, the existing sev_snp_cpuid_init() used by boot/compressed is used here as well, but annotated as __init so it can be cleaned up later (boot/compressed/sev.c already defines away __init when it pulls in shared SEV code). The boot/compressed kernel handles any necessary lookup of ConfidentialComputing blob from EFI and puts it into boot_params if present, so only boot_params needs to be checked. sev_snp_cpuid_init_virtual(): This is called when the previous identity mapping is gone and the memory used for the CPUID memory range needs to be mapped into the new page table with encryption bit set and accessed via __va(). Since this path is also entered later by APs to set up their initial VC handlers, a function pointer is used to switch them to a handler that doesn't attempt to re-initialize the SNP CPUID feature, as at that point it will have already been set up. sev_snp_cpuid_init_remap_early(): This is called when the previous mapping of CPUID memory range is no longer present. early_memremap() is now available, so use that to create a new one that can be used until memremap() is available. sev_snp_cpuid_init_remap(): This switches away from using early_memremap() to ioremap_encrypted() to map CPUID memory range, otherwise the leak detector will complain. This mapping is what gets used for the remaining life of the guest. Signed-off-by: Michael Roth Signed-off-by: Brijesh Singh --- arch/x86/include/asm/realmode.h | 1 + arch/x86/include/asm/setup.h | 5 +- arch/x86/include/asm/sev.h | 8 +++ arch/x86/kernel/head64.c | 21 ++++++-- arch/x86/kernel/head_64.S | 6 ++- arch/x86/kernel/setup.c | 3 ++ arch/x86/kernel/sev-shared.c | 93 ++++++++++++++++++++++++++++++++- arch/x86/kernel/smpboot.c | 2 + 8 files changed, 129 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/realmode.h b/arch/x86/include/asm/realmode.h index 5db5d083c873..ff0eecee4235 100644 --- a/arch/x86/include/asm/realmode.h +++ b/arch/x86/include/asm/realmode.h @@ -63,6 +63,7 @@ extern unsigned long initial_stack; #ifdef CONFIG_AMD_MEM_ENCRYPT extern unsigned long initial_vc_handler; #endif +extern unsigned long initial_idt_setup; extern unsigned char real_mode_blob[]; extern unsigned char real_mode_relocs[]; diff --git a/arch/x86/include/asm/setup.h b/arch/x86/include/asm/setup.h index a12458a7a8d4..12fc52894ad8 100644 --- a/arch/x86/include/asm/setup.h +++ b/arch/x86/include/asm/setup.h @@ -50,8 +50,9 @@ extern void reserve_standard_io_resources(void); extern void i386_reserve_resources(void); extern unsigned long __startup_64(unsigned long physaddr, struct boot_params *bp); extern unsigned long __startup_secondary_64(void); -extern void startup_64_setup_env(unsigned long physbase); -extern void early_setup_idt(void); +extern void startup_64_setup_env(unsigned long physbase, struct boot_params *bp); +extern void early_setup_idt_common(void *rmode); +extern void __init early_setup_idt(void *rmode); extern void __init do_early_exception(struct pt_regs *regs, int trapnr); #ifdef CONFIG_X86_INTEL_MID diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h index b5715a26361a..6c23e694a109 100644 --- a/arch/x86/include/asm/sev.h +++ b/arch/x86/include/asm/sev.h @@ -133,6 +133,10 @@ bool sev_snp_enabled(void); #endif void sev_snp_cpuid_init(struct boot_params *bp); +#ifndef __BOOT_COMPRESSED +void sev_snp_cpuid_init_virtual(void); +void sev_snp_cpuid_init_remap_early(void); +#endif /* __BOOT_COMPRESSED */ #else static inline void sev_es_ist_enter(struct pt_regs *regs) { } static inline void sev_es_ist_exit(void) { } @@ -154,6 +158,10 @@ static inline bool sev_snp_enabled { return false; } #endif static inline void sev_snp_cpuid_init(struct boot_params *bp) { } +#ifndef __BOOT_COMPRESSED +static inline void sev_snp_cpuid_init_virtual(void) { } +static inline void sev_snp_cpuid_init_remap_early(void) { } +#endif /* __BOOT_COMPRESSED */ #endif #endif diff --git a/arch/x86/kernel/head64.c b/arch/x86/kernel/head64.c index 8615418f98f1..de3b4f1afbfe 100644 --- a/arch/x86/kernel/head64.c +++ b/arch/x86/kernel/head64.c @@ -567,7 +567,7 @@ static void set_bringup_idt_handler(gate_desc *idt, int n, void *handler) } /* This runs while still in the direct mapping */ -static void startup_64_load_idt(unsigned long physbase) +static void startup_64_load_idt(unsigned long physbase, struct boot_params *bp) { struct desc_ptr *desc = fixup_pointer(&bringup_idt_descr, physbase); gate_desc *idt = fixup_pointer(bringup_idt_table, physbase); @@ -577,6 +577,7 @@ static void startup_64_load_idt(unsigned long physbase) void *handler; /* VMM Communication Exception */ + sev_snp_cpuid_init(bp); /* used by #VC handler */ handler = fixup_pointer(vc_no_ghcb, physbase); set_bringup_idt_handler(idt, X86_TRAP_VC, handler); } @@ -585,8 +586,8 @@ static void startup_64_load_idt(unsigned long physbase) native_load_idt(desc); } -/* This is used when running on kernel addresses */ -void early_setup_idt(void) +/* Used for all CPUs */ +void early_setup_idt_common(void *rmode) { /* VMM Communication Exception */ if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) @@ -596,10 +597,20 @@ void early_setup_idt(void) native_load_idt(&bringup_idt_descr); } +/* This is used by boot processor when running on kernel addresses */ +void __init early_setup_idt(void *rmode) +{ + /* SEV-SNP CPUID setup for use by #VC handler */ + if (IS_ENABLED(CONFIG_AMD_MEM_ENCRYPT)) + sev_snp_cpuid_init_virtual(); + + early_setup_idt_common(rmode); +} + /* * Setup boot CPU state needed before kernel switches to virtual addresses. */ -void __head startup_64_setup_env(unsigned long physbase) +void __head startup_64_setup_env(unsigned long physbase, struct boot_params *bp) { u64 gs_area = (u64)fixup_pointer(startup_gs_area, physbase); @@ -623,5 +634,5 @@ void __head startup_64_setup_env(unsigned long physbase) */ native_wrmsr(MSR_GS_BASE, gs_area, gs_area >> 32); - startup_64_load_idt(physbase); + startup_64_load_idt(physbase, bp); } diff --git a/arch/x86/kernel/head_64.S b/arch/x86/kernel/head_64.S index d8b3ebd2bb85..78f35e446498 100644 --- a/arch/x86/kernel/head_64.S +++ b/arch/x86/kernel/head_64.S @@ -218,7 +218,10 @@ SYM_INNER_LABEL(secondary_startup_64_no_verify, SYM_L_GLOBAL) /* Setup and Load IDT */ pushq %rsi - call early_setup_idt + movq %rsi, %rdi + movq initial_idt_setup(%rip), %rax + ANNOTATE_RETPOLINE_SAFE + call *%rax popq %rsi /* Check if nx is implemented */ @@ -341,6 +344,7 @@ SYM_DATA(initial_gs, .quad INIT_PER_CPU_VAR(fixed_percpu_data)) #ifdef CONFIG_AMD_MEM_ENCRYPT SYM_DATA(initial_vc_handler, .quad handle_vc_boot_ghcb) #endif +SYM_DATA(initial_idt_setup, .quad early_setup_idt) /* * The FRAME_SIZE gap is a convention which helps the in-kernel unwinder diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c index 85acd22f8022..5ff264917b5b 100644 --- a/arch/x86/kernel/setup.c +++ b/arch/x86/kernel/setup.c @@ -47,6 +47,7 @@ #include #include #include +#include #include /* @@ -1077,6 +1078,8 @@ void __init setup_arch(char **cmdline_p) init_mem_mapping(); + sev_snp_cpuid_init_remap_early(); + idt_setup_early_pf(); /* diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c index 23328727caf4..dbc5c2600d9d 100644 --- a/arch/x86/kernel/sev-shared.c +++ b/arch/x86/kernel/sev-shared.c @@ -264,7 +264,7 @@ static int sev_es_cpuid_msr_proto(u32 func, u32 subfunc, u32 *eax, u32 *ebx, return 0; } -static bool sev_snp_cpuid_active(void) +static inline bool sev_snp_cpuid_active(void) { return sev_snp_cpuid_enabled; } @@ -905,7 +905,7 @@ static struct cc_blob_sev_info *sev_snp_probe_cc_blob(struct boot_params *bp) * indication that SEV-ES is enabled. Subsequent init levels will check for * SEV_SNP feature once available to also take SEV MSR value into account. */ -void sev_snp_cpuid_init(struct boot_params *bp) +void __init sev_snp_cpuid_init(struct boot_params *bp) { struct cc_blob_sev_info *cc_info; @@ -941,3 +941,92 @@ void sev_snp_cpuid_init(struct boot_params *bp) if (cpuid_info->count > 0) sev_snp_cpuid_enabled = 1; } + +#ifndef __BOOT_COMPRESSED + +static bool __init early_make_pgtable_enc(unsigned long physaddr) +{ + pmdval_t pmd; + + /* early_pmd_flags hasn't been updated with SME bit yet; add it */ + pmd = (physaddr & PMD_MASK) + early_pmd_flags + sme_get_me_mask(); + + return __early_make_pgtable((unsigned long)__va(physaddr), pmd); +} + +/* + * This is called when we switch to virtual kernel addresses, before #PF + * handler is set up. boot_params have already been parsed at this point, + * but CPUID page is no longer identity-mapped so we need to create a + * virtual mapping. + */ +void __init sev_snp_cpuid_init_virtual(void) +{ + /* + * We rely on sev_snp_cpuid_init() to do initial parsing of bootparams + * and initial setup. If that didn't enable the feature then don't try + * to enable it here. + */ + if (!sev_snp_cpuid_active()) + return; + + /* + * Either boot_params/EFI advertised the feature even though SNP isn't + * enabled, or something else went wrong. Bail out. + */ + if (!sev_feature_enabled(SEV_SNP)) + sev_es_terminate(1, GHCB_TERM_CPUID); + + /* If feature is enabled, but we can't map CPUID info, we're hosed */ + if (!early_make_pgtable_enc(sev_snp_cpuid_pa)) + sev_es_terminate(1, GHCB_TERM_CPUID); + + cpuid_info = (const struct sev_snp_cpuid_info *)__va(sev_snp_cpuid_pa); +} + +/* Called after early_ioremap_init() */ +void __init sev_snp_cpuid_init_remap_early(void) +{ + if (!sev_snp_cpuid_active()) + return; + + /* + * This really shouldn't be possible at this point. + */ + if (!sev_feature_enabled(SEV_SNP)) + sev_es_terminate(1, GHCB_TERM_CPUID); + + cpuid_info = early_memremap(sev_snp_cpuid_pa, sev_snp_cpuid_sz); +} + +/* Final switch to run-time mapping */ +static int __init sev_snp_cpuid_init_remap(void) +{ + if (!sev_snp_cpuid_active()) + return 0; + + /* + * This really shouldn't be possible at this point either. + */ + if (!sev_feature_enabled(SEV_SNP)) + sev_es_terminate(1, GHCB_TERM_CPUID); + + /* Clean up earlier mapping. */ + if (cpuid_info) + early_memunmap((void *)cpuid_info, sev_snp_cpuid_sz); + + /* + * We need ioremap_encrypted() to get an encrypted mapping, but this + * is normal RAM so can be accessed directly. + */ + cpuid_info = (__force void *)ioremap_encrypted(sev_snp_cpuid_pa, + sev_snp_cpuid_sz); + if (!cpuid_info) + return -EIO; + + return 0; +} + +arch_initcall(sev_snp_cpuid_init_remap); + +#endif /* __BOOT_COMPRESSED */ diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c index 4fc07006f7f8..d3f4993b89cc 100644 --- a/arch/x86/kernel/smpboot.c +++ b/arch/x86/kernel/smpboot.c @@ -1041,6 +1041,8 @@ static int do_boot_cpu(int apicid, int cpu, struct task_struct *idle, early_gdt_descr.address = (unsigned long)get_cpu_gdt_rw(cpu); initial_code = (unsigned long)start_secondary; initial_stack = idle->thread.sp; + /* don't repeat IDT setup work specific to the BSP */ + initial_idt_setup = (unsigned long)early_setup_idt_common; /* Enable the espfix hack for this CPU */ init_espfix_ap(cpu); From patchwork Wed Jul 7 18:15:01 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12364005 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,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 7D1F2C07E95 for ; Wed, 7 Jul 2021 19:51:19 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 01ADA61CC2 for ; Wed, 7 Jul 2021 19:51:18 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 01ADA61CC2 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id CA39D6B0011; Wed, 7 Jul 2021 15:51:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C53466B005D; Wed, 7 Jul 2021 15:51:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A580B6B006C; Wed, 7 Jul 2021 15:51:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0042.hostedemail.com [216.40.44.42]) by kanga.kvack.org (Postfix) with ESMTP id 79EA66B0011 for ; Wed, 7 Jul 2021 15:51:18 -0400 (EDT) Received: from smtpin33.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id C6DAB1825672D for ; Wed, 7 Jul 2021 19:51:17 +0000 (UTC) X-FDA: 78336835794.33.87C8634 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2086.outbound.protection.outlook.com [40.107.236.86]) by imf10.hostedemail.com (Postfix) with ESMTP id F310A6001AB3 for ; Wed, 7 Jul 2021 19:51:16 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=mYmibSsd1an9rpJ6i4zVE993/QAMWrSPzesCwJwajqWIp/P+ngu9F8HfSHELFQ70usKMkm/jgKHsK1af9+WaRo+l9yTX0gBxObso6qkfQIvh2tm9ACh1AoOJmOfnY31TAI9prF1eYd7lnJNba+NgaEPK6iggusO7NOdI8+nzSpWmChNdnLVVFdu4n1yxkOFznoD3gj4YWxyJWwjS5cbBZFQ31zvGrOHnFrNLDn7QnXCof0nTGuO+Y/J1EXI6NuGYTv5wKAKg3AnkC+cZgS5cErrUNWvAl+4IJOyrufMnTsmrZXUqcbMIpGuZ/bE8aOI+QsKVcq78XVe5bnW2KVKzqA== 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=0B6FiFCaDmM5RZo5rPzgiexy2y5sO1Pr34P5srvJpVg=; b=Xj1nyMCylZAkTyO79kq2fphqMros1v4rapVAh08xZ2ujP1SHSmiR6+tPBVZ9GrrTeXnXGwRNIVWE3lT/eNVYvwbo/UwMgZbQXGp//uaLejRtQDEyJymZHOo4KQ7bp2To4BybEFILosB/f5hTr7Vipncr7Yzd3RnoGRPazNllGBM5d+QTKOS7JBDqITnD3hGEQPbt8Qy6szJQtpFyr8fLmiihy3U6wY/BVqmWo828s9JsQE/cxmoE5vuQK50ihWBETLokRT8kkuVqbPKVdPpRjRrOepK8MBxLR4a+T6e1WizXK1jnYSWC1ggNirMKtZMyqNhxKeaO1XApL5IiFagR7A== 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=0B6FiFCaDmM5RZo5rPzgiexy2y5sO1Pr34P5srvJpVg=; b=uuSpKkHMX2zlER3mgpcXzAh7KoI+YNnygi/zEQyOj62ouJnAKisTmp8eWFwrOG/SH2k6+bWjylzqpKZCPo1XI3t4xmVYPRNUVx6GNyB068b4zvZUuqm6bYjg5Zl7uJCg5j74OBXPbfmIP7cyIvU6nP6a99X37+h1WidXJoXhioc= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:48 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:48 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 31/36] x86/sev: Provide support for SNP guest request NAEs Date: Wed, 7 Jul 2021 13:15:01 -0500 Message-Id: <20210707181506.30489-32-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:46 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 85c1362f-5533-4479-f492-08d941736294 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: tyjYEU5u4AjayshIni8i0raGxh4dY4Sea/xwOo6jRcYsWxl6AV4qo91NQeP5VmioGElgpw/fI63gWd5ekrAhaEvRSsDaTpQhrqAC7sJLCAolEkHNM/TltuFkTRadvxa2luF1BImyWbdDP+3hnAUSazmdDMxUADQI9vgGd9Sh3scK7U+moDXomyLpAnM2825XatAUMbxsQVbu0QzCEZqevaZPc6r3FvCcI73NlJtro7TEe4c0+kKx03kXfKOLVixMy2HpMzq3pHeIlmWhUdBT/d2znH5yDUKM4z2rYtp/gVR3HScMqUx2kgejv8GCCU9/V+EhXZLuQ4mkMkQi828WlxLCnb/hg/FtVklnCs/WbApdv+LpSlm9P19MpYlz3s5ZLOzCl5yPX+sWT9JwC0SeCPfALF4GP45aempSz6ZMGOhzL9snVbI94iXl8ki+0vYYjw+DVafRkv2mOjGeBlBkp8qcVQwUlbNTgZG0fyZZypb2OVeVmqgZVB+uCwwb0740aLuyFnVV7HL24ON299/O6CErRmS96sc37oe/9DXXGqCkYtEYxih5q9lUcycujyuRIO3Co7Ch7xQLY8yrygLwjQjRhn+TKQHaT9pGA2cs2xwN40AdTt1rflrJT4wtgcnf+8z80iABO4osNklauWbB1TdpsR2sGV1YpYLZOWPhL/T2AOXNhU0Vn5aZnV60OjomZq5jUE6K+GwVScmxbpomXg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 5sEiQH5zWHPUb4BKoyI9e9/07O3Oc1gzzD4+AMFTi9jfnUXPcO0+rFCogAVL21aJUebMFF8DkU6QmYz8ycHHNApVj1q+qifzZpJlgwjnCOdkaVujvYh1AO3gU+km/Qna7MJ+czWL/rdfKiacBJNFC7tNZsZB9p5bbVcMXFEYmaODQY/Pmh03R5PaB+hVYzKjQuJUpOfMVTrGvT8x3o4NwVTIqvYsyMpv+bkRLxUPy8/H3FtLu0ztkoMIeyb4vgiIJCEV9a3CtaRUpcAlrggPDocHr8eQb/TD0vqTGlWVzAW9HSGGa2F3jNqyWiHZumX1aBAmIWk/oaTVhBOdqiEpUu4LHNUtbudpw+8t0mwbkObpNxByUGhZCk/4yibIu0ByGU4hWUcZF96at0CfQDiANRyTNf0f/NyigPCvjyabBNlRn5F9iB7XSifGMXXKiQXbyusMeIWgBn8tMtf7YSPrBgnUUrUgtu4V15oDoJd7Us3pAUEeTRXWC2TdHjlCXouH/GZ3RYGj9AhtL0lS3wV/NkChBvavW8wznJcatMHNtQiLVHfIWbmk222KR+STthXCZSYYt31JBD6EVwulULscMFd8w6ooq9iKHvd8rbQ8ccWE42jL6by6QLv+2aw5mDLgOwVtKBj/QJIWJhlebQT2ppLj4cJixK9yt2UrCuW1YH1sIR1ypQ+p9THc2pey9Xc441umsUqgdPbzoln0a1jwT3pV7iGRB1xE1BoUU7BPt4PKcv9zMrO10+O03kBvGmz8FdgTBmcW+uf3Inmmr/ATql1HtL/SKTNIAHneavIA6bMhPRm3/y3o8+BQeQIy2h6TZ5qVOMRMYU5ivsdhjY0wso6XWtVuDbUCIqakzOY2aoiqVVQxka5fXJq297kft4pbyImi3uaY1fCwBVsKjBlV72Q7iE/RI22l/HKmnlWKD7ZN0bRApjIaHK+jRpedPMrM3dU2PVxgvK903mKl8QT+eCLPT7UVsQaEOI1Lg3JaH9HArzNrSv9M1lsQKC8ivwNhtVPVRuYG1b7/G4V5EfsImRmSOKTv2a8FtBJrNCcIzoUlaxYhPymT9LhZIVrz+ytAzfd/KN3RJystfL2Nvj3XaMohdZ5h8FFm4EI0efj99pm5wXx2F5fYcxst1lrT425o5TnZYpQrtX/rfL6INYMSYeZoM75r4zS0rAvpghlYHoYx5bMcp237qxTct7weT9vqz6Y+a/Ws080vsxOSBV9zEkWYthbVnhzApnt4eaRkcudMrxOAaLSRacPCedQqK1d6G4mPaSYct4giCl8lbljnZXffxzRupMuxY/cYh16T2SofXJsuaT7iBmYwosJX5GPD X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 85c1362f-5533-4479-f492-08d941736294 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:48.2618 (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: GO+JN6d5AuR/WVWKlDC2OmzZh+FkGHHRJRBwJY0IVE2mgkvtWVhslo+dy1kuzNPGMiO6wYtVMnwIrCnO3eauKQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 X-Rspam-User: nil Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=uuSpKkHM; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf10.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.236.86 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: F310A6001AB3 X-Stat-Signature: 1ds9p8zh66zg6wy39swcawhapjsdoqgx X-HE-Tag: 1625687476-800859 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Version 2 of GHCB specification provides SNP_GUEST_REQUEST and SNP_EXT_GUEST_REQUEST NAE that can be used by the SNP guest to communicate with the PSP. While at it, add a snp_issue_guest_request() helper that can be used by driver or other subsystem to issue the request to PSP. See SEV-SNP and GHCB spec for more details. Signed-off-by: Brijesh Singh --- arch/x86/include/uapi/asm/svm.h | 4 +++ arch/x86/kernel/sev.c | 57 +++++++++++++++++++++++++++++++++ include/linux/sev-guest.h | 48 +++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 include/linux/sev-guest.h diff --git a/arch/x86/include/uapi/asm/svm.h b/arch/x86/include/uapi/asm/svm.h index 997918f0a89a..9aaf0ab386ef 100644 --- a/arch/x86/include/uapi/asm/svm.h +++ b/arch/x86/include/uapi/asm/svm.h @@ -109,6 +109,8 @@ #define SVM_VMGEXIT_SET_AP_JUMP_TABLE 0 #define SVM_VMGEXIT_GET_AP_JUMP_TABLE 1 #define SVM_VMGEXIT_PSC 0x80000010 +#define SVM_VMGEXIT_GUEST_REQUEST 0x80000011 +#define SVM_VMGEXIT_EXT_GUEST_REQUEST 0x80000012 #define SVM_VMGEXIT_AP_CREATION 0x80000013 #define SVM_VMGEXIT_AP_CREATE_ON_INIT 0 #define SVM_VMGEXIT_AP_CREATE 1 @@ -221,6 +223,8 @@ { SVM_VMGEXIT_NMI_COMPLETE, "vmgexit_nmi_complete" }, \ { SVM_VMGEXIT_AP_HLT_LOOP, "vmgexit_ap_hlt_loop" }, \ { SVM_VMGEXIT_AP_JUMP_TABLE, "vmgexit_ap_jump_table" }, \ + { SVM_VMGEXIT_GUEST_REQUEST, "vmgexit_guest_request" }, \ + { SVM_VMGEXIT_EXT_GUEST_REQUEST, "vmgexit_ext_guest_request" }, \ { SVM_VMGEXIT_PSC, "vmgexit_page_state_change" }, \ { SVM_VMGEXIT_AP_CREATION, "vmgexit_ap_creation" }, \ { SVM_VMGEXIT_HYPERVISOR_FEATURES, "vmgexit_hypervisor_feature" }, \ diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 04ef5e79fa12..b85cab838372 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -2024,3 +2025,59 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs) while (true) halt(); } + +int snp_issue_guest_request(int type, struct snp_guest_request_data *input, unsigned long *fw_err) +{ + struct ghcb_state state; + unsigned long id, flags; + struct ghcb *ghcb; + int ret; + + if (!sev_feature_enabled(SEV_SNP)) + return -ENODEV; + + + local_irq_save(flags); + + ghcb = __sev_get_ghcb(&state); + if (!ghcb) + return -ENODEV; + + vc_ghcb_invalidate(ghcb); + + if (type == GUEST_REQUEST) { + id = SVM_VMGEXIT_GUEST_REQUEST; + } else if (type == EXT_GUEST_REQUEST) { + id = SVM_VMGEXIT_EXT_GUEST_REQUEST; + ghcb_set_rax(ghcb, input->data_gpa); + ghcb_set_rbx(ghcb, input->data_npages); + } else { + ret = -EINVAL; + goto e_put; + } + + + ret = sev_es_ghcb_hv_call(ghcb, NULL, id, input->req_gpa, input->resp_gpa); + if (ret) + goto e_put; + + if (ghcb->save.sw_exit_info_2) { + + /* Number of expected pages are returned in RBX */ + if (id == EXT_GUEST_REQUEST) + input->data_npages = ghcb_get_rbx(ghcb); + + if (fw_err) + *fw_err = ghcb->save.sw_exit_info_2; + + ret = -EIO; + goto e_put; + } + +e_put: + __sev_put_ghcb(&state); + local_irq_restore(flags); + + return ret; +} +EXPORT_SYMBOL_GPL(snp_issue_guest_request); diff --git a/include/linux/sev-guest.h b/include/linux/sev-guest.h new file mode 100644 index 000000000000..24dd17507789 --- /dev/null +++ b/include/linux/sev-guest.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * AMD Secure Encrypted Virtualization (SEV) guest driver interface + * + * Copyright (C) 2021 Advanced Micro Devices, Inc. + * + * Author: Brijesh Singh + * + */ + +#ifndef __LINUX_SEV_GUEST_H_ +#define __LINUX_SEV_GUEST_H_ + +#include + +enum vmgexit_type { + GUEST_REQUEST, + EXT_GUEST_REQUEST, + + GUEST_REQUEST_MAX +}; + +/* + * The error code when the data_npages is too small. The error code + * is defined in the GHCB specification. + */ +#define SNP_GUEST_REQ_INVALID_LEN 0x100000000ULL + +struct snp_guest_request_data { + unsigned long req_gpa; + unsigned long resp_gpa; + unsigned long data_gpa; + unsigned int data_npages; +}; + +#ifdef CONFIG_AMD_MEM_ENCRYPT +int snp_issue_guest_request(int vmgexit_type, struct snp_guest_request_data *input, + unsigned long *fw_err); +#else + +static inline int snp_issue_guest_request(int type, struct snp_guest_request_data *input, + unsigned long *fw_err) +{ + return -ENODEV; +} + +#endif /* CONFIG_AMD_MEM_ENCRYPT */ +#endif /* __LINUX_SEV_GUEST_H__ */ From patchwork Wed Jul 7 18:15:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12364007 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,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 1EF89C07E95 for ; Wed, 7 Jul 2021 19:52:14 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id BA5C661CCA for ; Wed, 7 Jul 2021 19:52:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org BA5C661CCA Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 500BB6B005D; Wed, 7 Jul 2021 15:52:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4B24F6B006C; Wed, 7 Jul 2021 15:52:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2DC7E6B0070; Wed, 7 Jul 2021 15:52:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0223.hostedemail.com [216.40.44.223]) by kanga.kvack.org (Postfix) with ESMTP id 03D836B005D for ; Wed, 7 Jul 2021 15:52:12 -0400 (EDT) Received: from smtpin36.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 5C1ED22ACC for ; Wed, 7 Jul 2021 19:52:12 +0000 (UTC) X-FDA: 78336838104.36.D28A160 Received: from NAM11-BN8-obe.outbound.protection.outlook.com (mail-bn8nam11on2054.outbound.protection.outlook.com [40.107.236.54]) by imf11.hostedemail.com (Postfix) with ESMTP id 9C3C5F0000AC for ; Wed, 7 Jul 2021 19:52:11 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=KGjq8o2xRrrrOHKcAoEq2hIIhCZfnbcc9YFfoapOguKPshRXhuOd5PVRroywGEQ2rTbGJmJA1TOqzvDvktflLjT4Ao8HntuoJ/nQhOxeeDA9KXdEQbg4H4vESMxkNug7AKTWJ6Dsn5hJxTTYatqSlcWbhUANOBihOAVSAn3X2a7JgiE/+aNkh9JlhzjDPr0kZ2BFkK2T/B9lp3HIblUZLHGzISIDNuz7KGvoYaBJovEmOwG6U4QEo/F7SSh1YatOeoH6c9W8Ll1od+jdCGUEVMoA2b1DhDhNtjUbu8L/Bx1Sv5k6M/+JjE29Uoy+Xu4A3bThHjQaM63/8VxZ/Ncn+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=SuBnOuaowbP0FR6tF9Xy3nGftIpOT3sN0TZI5ddzKdE=; b=S1sNRyQ6hptH3LJLEIBzybTbbrem2g/SwioPvp9pyeru/NVHlD66DHRDrIE0ckrwX+m2sB0EPSrixKdGmY/rPW4W010sJgeGdkRxNp5zZdIbSzaltVbzjnfgoXMyhHQtmVAjdSak1Iwux14sOXAW1ZcTXQQcpJgPuffsqOlbz0JHi1+/DNZA+xcXIjB+L5cwJuTIFPiCWB8U5KkjsJNQm9HsiFLvWO5JkHzf3kkjlifDZDn5Qi+J38E3vzjao6xtC74EyZeTXxHFMJSgfkCfmkQG5BBQFTINZIbbQUzBkJodNh8ZX9IaSSgnJ2s7rFCvedoFu1wuvvyDyDzM+ld3Qg== 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=SuBnOuaowbP0FR6tF9Xy3nGftIpOT3sN0TZI5ddzKdE=; b=r9/WFUD23NqBGEbATgFPqBWNo9s8NOwJokbIoptpHGZcXBcB8SDAzTaHlCQxE7HNgLvqN7PF+cKgtAjXEvLQHwZw5IxH6KKPgaPfCNlLg8c0ZKgwXFzQrEIRrJPk4y7YNtPljQmuIcd0CdvVSK4flTifD2sIqMInSprTlPqAB5I= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:50 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:50 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 32/36] x86/sev: Add snp_msg_seqno() helper Date: Wed, 7 Jul 2021 13:15:02 -0500 Message-Id: <20210707181506.30489-33-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:48 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 05944ca3-7fb5-400c-12af-08d941736410 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: nN9G3Kfjr5Wy/RpyBVRbW064AJgLKwyv59wAroKGQZ9Wq+x1FZbXoDmGCmKdrA8rytWCA/FuJE/4M+xmszIAJnMmLMYN9Ft51MkmCkOhdX/85EywzDoDdEkYP1X2UEaqWf12cwEsVexaqJ/0ydcgfSpJzTK68Kibi6DJ1ZqOKE5tubDg/xvYAjz6ca0qki98U9g4vUUK+LieCjEDwRWzLu4wTBoNftKDFLhcRN+jt7x42a605ywXZqjvsVOPd8REFfzPj/AeJ7pAWvu3RUGxbwzhlT/UUT0rnelc+0gbPrIb8KagiURb8QQQ2+R2z23jY411skY1yhHEHVCDkZ0I7NVw4W89zpaNHdF56s9o1dArvZIC3lVtRe3LMtTSc7qd4dRa5Fwni0BvOKBtqiUH73mM2johEztcQIL9ywodny2d/k0yjeP6LSJAtKd4IqSY+tnoCXR8r0O4L7BnKRnEGFaiP3Dn2ZjprxGuo2np469+gOknbxMCR3G/YGNAtnvmyaqzVG/Xw8ciVQSmXGKi8S8bYu3+1n7Y/9Ydb29CJ+qYbG2B+LE9p6ETCx3610mAsDUbrUP9Yr48TfAPXRuLOsJmmmIVuw38WxbHy535iYWkCJ/XtQeGOzG37CXTDOzJYIZeFj5MT/nSzXAIwY/iCsP04tpoLB8a9QkYLXOSznr69TBnaGxMHIkzXgl9rGvxWUuI8khf4a8oNQqTjLU/Hg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: oEsm7nG0WjntBrZOKfwsioDyu09eKL+/ohQlBmeRhI3sldFHjIGvwxN1nSeaE1MwA0z7CvSF4DoVu+kWMalEEeaFhbBIPmbkm0hlLEMrHJWD5uxSqXx07gDUuhNx9BjQSE+hE1CmiXidRSTOQv7LcwDMLka4KczCV/tLBmAsiM4JnupW8S4wu1DYu15f1w9oYiJAjIXdwR9F0rgJvAeQGAoB7Q2KfjizWAvAn1tqXqLBfLRRKn2F1wYkYTOCR2m5nRqv2bvZB3XRh4DVlVbvY1cjvAJTfDdA1vVn2q49KlSiZV52UpnC+xShuwzcLA0Yei6qD5KbhHTuniEBiJSCkAYNTWAwIM5jhFPjb1lAbyNsN7cDRWRncLEn1RN9rsVtHdJ+nVb7PfTzeQnkERIwfYknhZ9h0kDaAmSrEXgpIrzI0Ff1GIOZbKe8JTjQM4/X4wPDEFW64rJAUM7xtu/RP5cxsKvCYNO8TkK4xUSsRxQyfyja7QCCnCLSgq0XXhSdiUzWcAdlmKWnPah/Ot3kb5Tm0RJ5JOyo3a0Udac6wgnbYySnHUFxRAsUdHr8ncG1efToZXmLfKaAHmYpJr/KtlzAFLwyvLD6D2ShUPK4HclHslVaEApBjA0bf6ZPuTkfpM0xx7hFz4JpEECJ7CT61cFMQ2JJnUTB/XC8oj3az2RSBW46mjfB2elgdCi/WxiFEpSsqIfU9pV9cjv04qxnljtwZojh5gC6EiwtAsewNdOWApfW8vo691o5QcsPoyIrT/Uf5zmu5eFQMAFSRxag8k0KWuLYK1VCaKH1CKhhq5Wy8ootxypjjrhEqRtyODSdItDT8zPnRnywkz/BlSdbaJUWn6a0YnCu6WZBx03tuf7s2b9/MF0QmyIrPv7wSbLeJ8swHzh7iMKqlT1JPaXE3lk4X3FsBsa/Bz3fbgvZJ75sB6tFeGC8CT8CS1kM28nA1ohqVdciyHysSv6h8PUwGIKOPz0xuv5yE36d4z5T5KGazrD7Wha1iPWfIqLQWPlQcOoGmjZJma60H5YFHQMBRaVriNAx/nV04eQb2hTK9TdA3J2TuVv5F+iAdwWxw0KE/PjGF8RLyrjE8Z4doxboRqMLkB+Cq0Y7cqmv4VJKwnK809a5RH3r1tcwBh8kG6udHrONu1Q07kMnIvwFo2Mv3aU7w6TmjtEUrpSdpqrPfjE06MzZbW+gC0eB0VdM4bgW72erdONk6uqhwL0iBo4zYZYLryNDkxL/s1NTCg1RZDP292AR5FWqPTEotVWvCJ5ghOcV9iPiy1fkadCmkXo1fRRb3Z7bt6hJQJjh4+WX3gjuHGrE3qRXujZVsRYrmqEM X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 05944ca3-7fb5-400c-12af-08d941736410 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:50.7464 (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: Y8X9pa5JxUsHEABhzvctuGvpr+bOYGxc5n6WeyvG2rrX85qIbQQlYcE8+nMvREjJGniETMaOeG9XPNebWvD8LA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 X-Rspam-User: nil Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b="r9/WFUD2"; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf11.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.236.54 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 9C3C5F0000AC X-Stat-Signature: 6mom7ihrdqtuztf9jnt9qaeh9endw4ux X-HE-Tag: 1625687531-5200 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The SNP guest request message header contains a message count. The message count is used while building the IV. The PSP firmware increments the message count by 1, and expects that next message will be using the incremented count. The snp_msg_seqno() helper will be used by driver to get and message sequence counter used in the request message header, and it will be automatically incremented after the snp_issue_guest_request() is successful. The incremented value is saved in the secrets page so that the kexec'ed kernel knows from where to begin. Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev.c | 79 +++++++++++++++++++++++++++++++++++++++ include/linux/sev-guest.h | 37 ++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index b85cab838372..663cfe96c186 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -51,6 +51,8 @@ static struct ghcb boot_ghcb_page __bss_decrypted __aligned(PAGE_SIZE); */ static struct ghcb __initdata *boot_ghcb; +static u64 snp_secrets_phys; + /* #VC handler runtime per-CPU data */ struct sev_es_runtime_data { struct ghcb ghcb_page; @@ -2026,6 +2028,80 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs) halt(); } +static struct snp_secrets_page_layout *snp_map_secrets_page(void) +{ + u16 __iomem *secrets; + + if (!snp_secrets_phys || !sev_feature_enabled(SEV_SNP)) + return NULL; + + secrets = ioremap_encrypted(snp_secrets_phys, PAGE_SIZE); + if (!secrets) + return NULL; + + return (struct snp_secrets_page_layout *)secrets; +} + +static inline u64 snp_read_msg_seqno(void) +{ + struct snp_secrets_page_layout *layout; + u64 count; + + layout = snp_map_secrets_page(); + if (layout == NULL) + return 0; + + /* Read the current message sequence counter from secrets pages */ + count = readl(&layout->os_area.msg_seqno_0); + + iounmap(layout); + + /* The sequence counter must begin with 1 */ + if (!count) + return 1; + + return count + 1; +} + +u64 snp_msg_seqno(void) +{ + u64 count = snp_read_msg_seqno(); + + if (unlikely(!count)) + return 0; + + /* + * The message sequence counter for the SNP guest request is a + * 64-bit value but the version 2 of GHCB specification defines a + * 32-bit storage for the it. + */ + if (count >= UINT_MAX) + return 0; + + return count; +} +EXPORT_SYMBOL_GPL(snp_msg_seqno); + +static void snp_gen_msg_seqno(void) +{ + struct snp_secrets_page_layout *layout; + u64 count; + + layout = snp_map_secrets_page(); + if (layout == NULL) + return; + + /* + * The counter is also incremented by the PSP, so increment it by 2 + * and save in secrets page. + */ + count = readl(&layout->os_area.msg_seqno_0); + count += 2; + + writel(count, &layout->os_area.msg_seqno_0); + iounmap(layout); +} + int snp_issue_guest_request(int type, struct snp_guest_request_data *input, unsigned long *fw_err) { struct ghcb_state state; @@ -2074,6 +2150,9 @@ int snp_issue_guest_request(int type, struct snp_guest_request_data *input, unsi goto e_put; } + /* The command was successful, increment the sequence counter */ + snp_gen_msg_seqno(); + e_put: __sev_put_ghcb(&state); local_irq_restore(flags); diff --git a/include/linux/sev-guest.h b/include/linux/sev-guest.h index 24dd17507789..5f6c4d634e1f 100644 --- a/include/linux/sev-guest.h +++ b/include/linux/sev-guest.h @@ -20,6 +20,41 @@ enum vmgexit_type { GUEST_REQUEST_MAX }; +/* + * The secrets page contains 96-bytes of reserved field that can be used by + * the guest OS. The guest OS uses the area to save the message sequence + * number for each VMPL level. + * + * See the GHCB spec section Secret page layout for the format for this area. + */ +struct secrets_os_area { + u32 msg_seqno_0; + u32 msg_seqno_1; + u32 msg_seqno_2; + u32 msg_seqno_3; + u64 ap_jump_table_pa; + u8 rsvd[40]; + u8 guest_usage[32]; +} __packed; + +#define VMPCK_KEY_LEN 32 + +/* See the SNP spec secrets page layout section for the structure */ +struct snp_secrets_page_layout { + u32 version; + u32 imiEn : 1, + rsvd1 : 31; + u32 fms; + u32 rsvd2; + u8 gosvw[16]; + u8 vmpck0[VMPCK_KEY_LEN]; + u8 vmpck1[VMPCK_KEY_LEN]; + u8 vmpck2[VMPCK_KEY_LEN]; + u8 vmpck3[VMPCK_KEY_LEN]; + struct secrets_os_area os_area; + u8 rsvd3[3840]; +} __packed; + /* * The error code when the data_npages is too small. The error code * is defined in the GHCB specification. @@ -36,6 +71,7 @@ struct snp_guest_request_data { #ifdef CONFIG_AMD_MEM_ENCRYPT int snp_issue_guest_request(int vmgexit_type, struct snp_guest_request_data *input, unsigned long *fw_err); +u64 snp_msg_seqno(void); #else static inline int snp_issue_guest_request(int type, struct snp_guest_request_data *input, @@ -43,6 +79,7 @@ static inline int snp_issue_guest_request(int type, struct snp_guest_request_dat { return -ENODEV; } +static inline u64 snp_msg_seqno(void) { return 0; } #endif /* CONFIG_AMD_MEM_ENCRYPT */ #endif /* __LINUX_SEV_GUEST_H__ */ From patchwork Wed Jul 7 18:15:03 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363845 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,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 44507C07E95 for ; Wed, 7 Jul 2021 18:49:44 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E9B5D61CB2 for ; Wed, 7 Jul 2021 18:49:43 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E9B5D61CB2 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id D4E1F6B0093; Wed, 7 Jul 2021 14:49:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D24B16B0095; Wed, 7 Jul 2021 14:49:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B507B6B0096; Wed, 7 Jul 2021 14:49:43 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0097.hostedemail.com [216.40.44.97]) by kanga.kvack.org (Postfix) with ESMTP id 941776B0093 for ; Wed, 7 Jul 2021 14:49:43 -0400 (EDT) Received: from smtpin20.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 08CE6274D8 for ; Wed, 7 Jul 2021 18:49:43 +0000 (UTC) X-FDA: 78336680646.20.CDB7C66 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2073.outbound.protection.outlook.com [40.107.237.73]) by imf21.hostedemail.com (Postfix) with ESMTP id B42ACD002843 for ; Wed, 7 Jul 2021 18:49:29 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZEYPkTjpnev5RIH6zVLT20O9VZdfCwHS95tPvPdId2CFE8ZxzKexURo92e+R6cTXFwsfhQFBKt+2+PNPtio/NH15k7KxpoNIeD+BI8yyehevu2xkC80mdmZDC59ixFfr+SxSGhR2IeHN58pX1M/BFrdHSI9Y6mzP03MJutYtOqtOKA9UATZ9EHbsN1MBjcmmq8+k+y126NYIfewZM3EaZwu8iusSr6UulYW1cPIRWN9vY61R0ohWzve0XqfAqgSbxwOUNqeWxi1t73vejVdEDIk5Y00ne/kt+ozx5uoy+34fBZZzx7MPC1z0U0TIkmoX/kGZW4IEdEqZwhoDCQE8ng== 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=1F+SidK1HDpN6741TNeAYv7xj0tMTlfPEyf+prS85qw=; b=mTqWjDDsL3YY2iAV23WcQFN8M+0Gdotyqe9XNUbgef/NKGKIBFG16AOj6f3iobEq90WX3KhlCzLvKREXBHnq8XxXx8N7JKaSqlI08rpgu/r0TD+svHFh6tWinOpRKzh7e9Hq23O8hV4xvQos6OxURF6Taf3jhQq9TFbfho1vrAkAJDdB6DRh6S7ZtNCwZ2lR3EK9ZP/b64rsE+ErtT2b7yrBpHZt89JCi1d6xVPU19C0IWI0QHZRYKHfuWufCoH0x2+ZGzDPVq0O22joZg2CTZVtPIPANBg/JjjRCMHwGC8nZhZoHQXtQ+G5eCxYREOOwOwAr1+xcwSVo2u1UybNRQ== 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=1F+SidK1HDpN6741TNeAYv7xj0tMTlfPEyf+prS85qw=; b=vwT7CdvaaL2AtJzyEowNJUWOa9lnZO8vCcW41+SgaYpgaM/HfY/yZKb86Pes7gXnfz1jnHxw74i27Q82CcwfN3v865ACYPy/t+G8+20nciSpxfKOPmgmQLz8w/88mPIE4aunU4QahGPLjTzd4Thmc/t9DEafaNB3UH5kQG1avag= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:53 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:53 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 33/36] x86/sev: Register SNP guest request platform device Date: Wed, 7 Jul 2021 13:15:03 -0500 Message-Id: <20210707181506.30489-34-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:51 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 890d6e5f-21a7-483b-0e3a-08d941736589 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: 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: yDheqtBnUvDWQClSVVqfv5AqD01RZreWYWFtZvBW6rwnhLnx1XOvFYmHrNOlodZGI2K0Y2W8CS8ml6LGNwamHtkb5ueT5kzCyPge8pYt/2xlUCevZrxxvWO5kf3BCFkx++F24mIFCN83qK2wfbOZ9+2yWZdi6U8yPI0gYTUGJ5doPhpss5WOinljEv4XyMhYYPZ1z08ej6xXbZEijm3x3EvbwL2tNVMHE41JOuXPmZFwGSjVBiuxd+R/LPeBBo7Tn+JWBvSV2/bZBcmdmA0mMxQRZG9P8eatR2/SooDu25Am47MDAldzAonbBh0GE+9CleUxTfHF9OUtAWKoVw4M+jFvINO4VnNoMJi5AAzxo0ziWZ1Le/nDBZ3h1edJqD7gU1QDRjslRu4uPdGSK8vc0i1MN6IIdbqvSq1A19NzxldaJGiGtywvZ7RK3YrC4sFzrI2MAhzQc66mA/wxd1/fdz+SZO5KQ4eUYisLYtpceKQQXdDjsjAvx/aBgOHC1Z5UErnz8VmTxgapipmI5Af4sx23eod7Xt9a6CH4ZZGpXlMsHRjO9zsjqXRKP1xAgN7/IZgvvbBy8mr5rY5JRDrQ4EidBostsIRS7V/PHBMptCWkWg6EGtKuXXSFIVVhg7cL0hjoY43zq8n/L8c3zU2ifsi9Zv+oEaryDNzxFwflp1sHh/+TAImcAWefAQkmZSTeHE+jRVHVV3unqUn9Og6ZGg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(39860400002)(136003)(346002)(376002)(396003)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(38100700002)(52116002)(186003)(6486002)(7696005)(478600001)(86362001)(316002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: nx3CUrdxLNe6yaY5Hc+KuyUA6nW2xKtTmJh8MnURR0nYz0fibS8FOuRj8ciFlV8kp6xTlLLNyOR27CiLx14xY4DA/qaqAom4jArLZZ/MnM6e7xPFf47rrWJJrV3PjLAbD9MrjG4lIIRSy3POF3PeqzKbkiSkNZUb7BiAD7Tjv07m5zQOmSrKsFZhMRO/m+o9EaQzPwiR1j/HDPBBqfC1kaTb6n9O5kNUaMQspRWnHqMiw+6jilXjPODj/2c6nZFXaD3PHwLHfUci6vjrEI88scr4rJJ3KzGKAhDRwKzrEABXHOGVRbTBUNL3iC+ag+PNdSilVAw/EgvN8ytTP973l/cs8hmtAfTshq8smswaCUuAQZXV339XGLGqzjDK6mApL99nyoPt6eyhZuSEf+cF/Iglq3SNTvNpLSvgcONzFc5dk1b6FlCWbDz/5k1yKqgL53V4pPoO7xIQZYbOntpwQgs+rTkGPCvFBZmlrn07JKT1QtkOPtb8/guBt4kw6eZAgc3tCJpmo65OD4Y/OEVJTaFRFjshHIpe/14wwV1bNL9fuk1cihQVNZcw6ZLQJxjiZDlbO53HX8fzvX/q6XArstw45HWOIvH/LqgEHD4lPt7Iu8PwY+WhUmniApAKUgdDO3LkZrkadN5ZiTdFk2RpDgu2pz5JMjwTF2OVKFgqcQXlPWRa/NXidwninr2B899KSPFSO/ab8on19mDDvOrYSssGCxk8V934UV7fvtrFNEKA4wr88OvaSCEZpisHVBSME0NIzJ9tGC0knHYGYLNzliYKfNvuy3szP6//RQ48QcXhFRzGYRA53FHZoHsvFDhyGGA2YXTXMCV9d8MVKlDMIB9NneASGDvzX39RXMS3Tr4ZMygIX0s9u7Zggc3h+5VlxMET3Y5pr6ySX8FWtVwGQ6VHb543u4IY8YKG8YNTO+LMOUdWMKPuLUjGhSYG6KOntIJwdXNLp3umTu1d52fMwiSQGjH21dmLwdvjn/ESc3KqAZP6sKdz20pja1WmYkWEN+AeZsSgYYhjnFO83OsL5udOjSRcHuIU7bH+jUSLfY4By5TaVryemzFso4+JhqZLHHKl7aR4hs1fCd0qFg6bVuHSPfzJw3J9Iu/FKFg+QrHzLaNs0u7q0ogy0HwpYAhgiohM1dK1GLzWFKsLjMsuqlPtdPapCfd3+VbsulnomjW8RrByAywGaLZoJ4fEZyTppYLJVgpqg6L5JdGQA+pIU51po4lh8uclUC+brl8zVbkB55m3GuyxtjFOXv3eOiAQ6L7OuRpYZgxQD7PFloyfz0Oq7QU3iO4vlTtx0oHYnUrEXnpT26cdxBVoJ4OQQNBt X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 890d6e5f-21a7-483b-0e3a-08d941736589 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:53.1980 (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: 5t56lFF2VF5/ka9Gvif3ywC0jgEWPE7AYKS8pkBo3WOZik2f5opfG2rknFYBdfP3nd7RQjBjJiHxv/7NSnv3sA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=vwT7Cdva; spf=pass (imf21.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.73 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com; dmarc=pass (policy=quarantine) header.from=amd.com X-Rspamd-Server: rspam05 X-Stat-Signature: cjst5w5698kxe8n454s4xkfjgbkh4oue X-Rspamd-Queue-Id: B42ACD002843 X-Rspam-User: nil X-HE-Tag: 1625683769-691376 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Version 2 of GHCB specification provides NAEs that can be used by the SNP guest to communicate with the PSP without risk from a malicious hypervisor who wishes to read, alter, drop or replay the messages sent. In order to communicate with the PSP, the guest need to locate the secrets page inserted by the hypervisor during the SEV-SNP guest launch. The secrets page contains the communication keys used to send and receive the encrypted messages between the guest and the PSP. The secrets page is location is passed through the setup_data. Create a platform device that the SNP guest driver can bind to get the platform resources such as encryption key and message id to use to communicate with the PSP. The SNP guest driver can provide userspace interface to get the attestation report, key derivation, extended attestation report etc. Signed-off-by: Brijesh Singh --- arch/x86/kernel/sev.c | 68 +++++++++++++++++++++++++++++++++++++++ include/linux/sev-guest.h | 5 +++ 2 files changed, 73 insertions(+) diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c index 663cfe96c186..566625707132 100644 --- a/arch/x86/kernel/sev.c +++ b/arch/x86/kernel/sev.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include @@ -37,6 +39,7 @@ #include #include #include +#include #include "sev-internal.h" @@ -2160,3 +2163,68 @@ int snp_issue_guest_request(int type, struct snp_guest_request_data *input, unsi return ret; } EXPORT_SYMBOL_GPL(snp_issue_guest_request); + +static struct platform_device guest_req_device = { + .name = "snp-guest", + .id = -1, +}; + +static u64 find_secrets_paddr(void) +{ + u64 pa_data = boot_params.cc_blob_address; + struct cc_blob_sev_info info; + void *map; + + /* + * The CC blob contains the address of the secrets page, check if the + * blob is present. + */ + if (!pa_data) + return 0; + + map = early_memremap(pa_data, sizeof(info)); + memcpy(&info, map, sizeof(info)); + early_memunmap(map, sizeof(info)); + + /* Verify that secrets page address is passed */ + if ((info.secrets_phys && (info.secrets_len == PAGE_SIZE))) + return info.secrets_phys; + + return 0; +} + +static int __init add_snp_guest_request(void) +{ + struct snp_secrets_page_layout *layout; + struct snp_guest_platform_data data; + + if (!sev_feature_enabled(SEV_SNP)) + return -ENODEV; + + snp_secrets_phys = find_secrets_paddr(); + if (!snp_secrets_phys) + return -ENODEV; + + layout = snp_map_secrets_page(); + if (layout == NULL) + return -ENODEV; + + /* + * The secrets page contains three VMPCK that can be used for + * communicating with the PSP. We choose the VMPCK0 to encrypt guest + * messages send and receive by the Linux. Provide the key and + * id through the platform data to the driver. + */ + data.vmpck_id = 0; + memcpy_fromio(data.vmpck, layout->vmpck0, sizeof(data.vmpck)); + + iounmap(layout); + + platform_device_add_data(&guest_req_device, &data, sizeof(data)); + + if (!platform_device_register(&guest_req_device)) + dev_info(&guest_req_device.dev, "secret phys 0x%llx\n", snp_secrets_phys); + + return 0; +} +device_initcall(add_snp_guest_request); diff --git a/include/linux/sev-guest.h b/include/linux/sev-guest.h index 5f6c4d634e1f..d13f2e2cd367 100644 --- a/include/linux/sev-guest.h +++ b/include/linux/sev-guest.h @@ -68,6 +68,11 @@ struct snp_guest_request_data { unsigned int data_npages; }; +struct snp_guest_platform_data { + u8 vmpck_id; + char vmpck[VMPCK_KEY_LEN]; +}; + #ifdef CONFIG_AMD_MEM_ENCRYPT int snp_issue_guest_request(int vmgexit_type, struct snp_guest_request_data *input, unsigned long *fw_err); From patchwork Wed Jul 7 18:15:04 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363853 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,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 C3388C07E9B for ; Wed, 7 Jul 2021 18:50:30 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 538D461CB2 for ; Wed, 7 Jul 2021 18:50:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 538D461CB2 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3AF586B008C; Wed, 7 Jul 2021 14:50:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3612B6B0098; Wed, 7 Jul 2021 14:50:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 115BA6B0099; Wed, 7 Jul 2021 14:50:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id D3F1C6B008C for ; Wed, 7 Jul 2021 14:50:29 -0400 (EDT) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 41ACA18041CE4 for ; Wed, 7 Jul 2021 18:50:29 +0000 (UTC) X-FDA: 78336682578.04.D244750 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2043.outbound.protection.outlook.com [40.107.237.43]) by imf28.hostedemail.com (Postfix) with ESMTP id 3DCF5900009F for ; Wed, 7 Jul 2021 18:50:28 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IqFYSo6znlzYi/XSmwlsKMfH6UiRv5+HtGnPVxEXGc8A+Nyie+V6xcS5VzPM6r0kakFyD1veedRtjTwlp6pgwkD0cOI3aj2PBPtMnilBzpINSDY2iILDLHp4161WKWVt9H2HIF63LnjST/joNuF57ya3xY3AQU1VCQIUeYi9jM+fIz3sCLpqpz9E6zaEwaHoG+fc/f07Agk7sr9dGThHd1TImgc5Q6iOpEEo7hQt+ptYw9Q0qSRIch820jHfQ740Os08DmkHiaXXYocosZQ6RdSGUYzplI0ldXZ7bUI+6IMSzgid5/xYwfHUVqWKs+f6z5wMxAc/yCNUIyQ4A36PRQ== 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=tR23niDP6KqzoZ++CMef5OYnX++6222K6rIuW+vAly4=; b=W2s424oX8KerALJ57Y5H245CiKX6cxWhS8w36zuFhFNVbp9YwJh3GTE4GKG5whpuA9ccT7bHmkvB5xJVjJaC7SKtu1Cfi0fnpDlYdhpuI0TAcKAQyDbjJNWhKc81LUIZqGq4CGfFaaJrQR+d5nMvZ7t1unpCZi4onW+qATTi+wYiYOvtG9fnehJMFTTCOdtNrhXYeGM2LM4ntRErZUuDT4x/yBP3wuVRpys7T0wNHk7LOk0PHcPXjPbVyPZ8wy73KIHKXCA/w7U4UxkkWgvEmlkBL08NITBIngv6VqGgkhlr7nHin8qEEJSxUt8B9UrW5HFPxJ0nrDvBjbm5aVw8OQ== 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=tR23niDP6KqzoZ++CMef5OYnX++6222K6rIuW+vAly4=; b=WVdPB02OB76eQroV759C6fEH3uxQOrG44YQVfdYQzh5Z9zvuSmQd8r2zxGpkQWEbw/TDgnIsUqcYQR/Gw/E8lrQci6UvqpeBKOC5eboRM3vi0cSfQXIuNiKPx7VKauQm4rqQAyb9BNlYGxbU0+Tqcjyz2CBxbbrkKm9LnkhavUI= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BY5PR12MB3683.namprd12.prod.outlook.com (2603:10b6:a03:1a5::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4287.27; Wed, 7 Jul 2021 18:16:56 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:56 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 34/36] virt: Add SEV-SNP guest driver Date: Wed, 7 Jul 2021 13:15:04 -0500 Message-Id: <20210707181506.30489-35-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:53 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 55973e93-e350-47bf-5363-08d941736704 X-MS-TrafficTypeDiagnostic: BY5PR12MB3683: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3044; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: jlITFMYdB7zBQh0R+TQFN4lbBwRgVnp8A9uZdLePyM8dJ4rN7EqAPKc33FoVXYLOyk2AkRGfsP4BvnqYuFEddytA7WgGO9wMVy/VQ3d4sL7OMGdASYqEceMupea5keB58jmwLbGzv9DCsrNcbJ4+ex+BM6v10NKQdi1Il8JzCE+tIk8ECt7e3v6WTWrWypsuEKpsS1bnKQu2sFkJQodTK7N9tb9zXKUBQjn4d5LHvnKU9g3+63Hiov8ds0dd+acRkAReTwMFCfPR4dUvAK/uHtuUFmOAUPWtI2kK5jrVUx2WoGZnJGJm1x4BnwmBSjhZvNP5kMPi22KcZAO7dgsDZ19gBbtUe1CUbG4tL8NtskJMUm4bgY+oNrv+/CZiSmIoqNphGMCwDe+c/dkRtXNmet1N51H/DSW6k5TypGwaTDbr+QjOVWTabGZ+hKXB7myI1TiHf36F5U/hkiFpbZtp+YNwtAdlEWCEE2/m1dGtkalQrDMzMD2RoB/NEF9G47rAFWTZq95YWlGZ2OuJ4Fqz/X1IVyxJI4d/ww3mHCJY/+nG1yjxGgfXCODxi3EZ7wVo1An/swFwdmY8IKhsmWgRQQo7Es/pCZ6oSObGTftLYga1sM1utp3rfWlh0/zBzSSbUsGwTf0hA+6S9BUgdpIFLXQB/1copbmQOl+4eWPQlzyoQecajXDNPoJdOuuwG6RpdEoenytoLZESMqSwMrDPGeI/kUsMyEAzTmJKJPuZtAZNhuDgNLuRUduybYN7R2L0iJvKS56DNlm9xkp9/BVrsjv8jhbx7ol9Gd8j842Tbb0= X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(5660300002)(8936002)(2616005)(44832011)(956004)(38350700002)(26005)(6666004)(7406005)(83380400001)(7416002)(1076003)(66946007)(4326008)(54906003)(66476007)(8676002)(66556008)(2906002)(36756003)(966005)(38100700002)(52116002)(186003)(6486002)(498600001)(7696005)(30864003)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: N523uKV03FMUVjZlLkolFFRZxOsZjwGF+fOLPMS36EPAoam3QQcvTr8Y8MCkXUYtVhMcpPgIGIGu4Gckmqgyo33bjv121sRR5kzdvllZ80+uMuFf7KSce3T9q6bXIRs6psFaoGVW3TzeqlEX0c7Znu4lYN6aiU6+nDZwGgtXdDePqG6OF0EtR7lFaIfp5kpkJLpu84o5HIdluY49S1Xphn/xpqql693jHTxRUuDaISRlCj76okfoZ1CavwRk75YPjCBAlrTXDMWH0ze+FgiOx/XMMVQqLeQ/O0YMSq3LgUKhPi3DnTmYR0AZyp9O558S2lNcRQbC/S0QeRvCiNvu923pNSom6ehEw4MDpNR2XWJBgN0TL/Dz60WM6jV04A0rWlDDLJgF43VerLs1022zKOITBzJBX0ffGQLsR2BN+I3Xbn1PPn202QXHWc/9kDsFircbEkXFrlG9qmTZMIW/WicFxS/qnbOpHI6I/BOrAQdSSWpU2oFAPzNas4f4m915H/ceGM6I14Xn9NNV8E6GR52XAgH385amthwBuc7Xk9Ic/SlUTMavyKqrpjXPn6bWqdrysDHxcFj5bLuRA1fO41FMj8rhABp2mduJkFWjeOPg3Un5XG9c2+MeJthS0CKk4rKugJ17UKL6RcmbXNtAicHgHlSfYqorJso2vVNTMsU1ccNYfI09tJkqbJ63gU3F72M8wrH2Xs/aguPrgZTHtNyAIYSXPm1z1EELfBy8HKO1jYQ02bAronuUCyK1c7nD9kT7w4uzAEsKqVctpyI9FTdtzmK9t/krUlLYE4SKVJAwSnMc44pSW/uFp7Xgj65vbkNScu+YZbI4y28qEDvPGvf4L8Gk6TBlKyagfa9AMI7CpAem3bkebG5TTLOwrWohoiTc9WfvWk8DpSEBG2knuUDogCU43q3+8dHa579gGaJLhe/bsxjrNOdObJZBDLAGlKO2XU/1bxvnqoX/v+Im6bp0BQ9n9BpqLTduBQF5EMEtNiQ85zEI68AF5Vo2DLAghUPht44XeeWQJoheRxHPE8HIQJEAZoblUU9dvw9rME/E2kAjAI8nhhHoAD9iL07xh+onmi/W7B/UXqLlWYSWvUluhBDqF+O+OPQVxlBln+EFEXj7PjazRihqlxpf5CxAKeAb+I2We0A0DjbRMIKjT4GAjFurtPrlQHAY0tQ9TodTOAQCesuREHZz3UqhJhoSW9I1EEvwbxIq/0JT+OS9wN/TXIUnS26F2gGAPo/ahBBx67IlmT6zDwfA2++B7YUeYINpIZ8Nj2r594VU6hGQjWFkbh2jVW62KdKYuJm3ueRgbh/4ivu0MVXSm6vM6p2C X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 55973e93-e350-47bf-5363-08d941736704 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:55.8784 (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: w+l2+4TjKIFoIwL14aXcAu/uN/SYv3YYd/ymhvDVUDWX2SRuL7hEVawZR8bHz38REU47b0nUVPif8DkIqJaMXA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR12MB3683 X-Rspam-User: nil Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=WVdPB02O; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf28.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.237.43 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 3DCF5900009F X-Stat-Signature: u18g83s1ccad5d6yjqo8p79pmgd676cb X-HE-Tag: 1625683828-347080 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: SEV-SNP specification provides the guest a mechanisum to communicate with the PSP without risk from a malicious hypervisor who wishes to read, alter, drop or replay the messages sent. The driver uses snp_issue_guest_request() to issue GHCB SNP_GUEST_REQUEST or SNP_EXT_GUEST_REQUEST NAE events to submit the request to PSP. The PSP requires that all communication should be encrypted using key specified through the platform_data. The userspace can use SNP_GET_REPORT ioctl() to query the guest attestation report. See SEV-SNP spec section Guest Messages for more details. Signed-off-by: Brijesh Singh --- Documentation/virt/coco/sevguest.rst | 69 ++++ drivers/virt/Kconfig | 3 + drivers/virt/Makefile | 1 + drivers/virt/coco/sevguest/Kconfig | 9 + drivers/virt/coco/sevguest/Makefile | 2 + drivers/virt/coco/sevguest/sevguest.c | 449 ++++++++++++++++++++++++++ drivers/virt/coco/sevguest/sevguest.h | 63 ++++ include/uapi/linux/sev-guest.h | 44 +++ 8 files changed, 640 insertions(+) create mode 100644 Documentation/virt/coco/sevguest.rst create mode 100644 drivers/virt/coco/sevguest/Kconfig create mode 100644 drivers/virt/coco/sevguest/Makefile create mode 100644 drivers/virt/coco/sevguest/sevguest.c create mode 100644 drivers/virt/coco/sevguest/sevguest.h create mode 100644 include/uapi/linux/sev-guest.h diff --git a/Documentation/virt/coco/sevguest.rst b/Documentation/virt/coco/sevguest.rst new file mode 100644 index 000000000000..52d5915037ef --- /dev/null +++ b/Documentation/virt/coco/sevguest.rst @@ -0,0 +1,69 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=================================================================== +The Definitive SEV Guest API Documentation +=================================================================== + +1. General description +====================== + +The SEV API is a set of ioctls that are issued to by the guest or +hypervisor to get or set certain aspect of the SEV virtual machine. +The ioctls belong to the following classes: + + - Hypervisor ioctls: These query and set global attributes which affect the + whole SEV firmware. These ioctl is used by platform provision tools. + + - Guest ioctls: These query and set attribute of the SEV virtual machine. + +2. API description +================== + +This section describes ioctls that can be used to query or set SEV guests. +For each ioctl, the following information is provided along with a +description: + + Technology: + which SEV techology provides this ioctl. sev, sev-es, sev-snp or all. + + Type: + hypervisor or guest. The ioctl can be used inside the guest or the + hypervisor. + + Parameters: + what parameters are accepted by the ioctl. + + Returns: + the return value. General error numbers (ENOMEM, EINVAL) + are not detailed, but errors with specific meanings are. + +The guest ioctl should be called to /dev/sev-guest device. The ioctl accepts +struct snp_user_guest_request. The input and output structure is specified +through the req_data and resp_data field respectively. If the ioctl fails +to execute due to the firmware error, then fw_err code will be set. + +:: + struct snp_user_guest_request { + /* Request and response structure address */ + __u64 req_data; + __u64 resp_data; + + /* firmware error code on failure (see psp-sev.h) */ + __u64 fw_err; + }; + +2.1 SNP_GET_REPORT +------------------ + +:Technology: sev-snp +:Type: guest ioctl +:Parameters (in): struct snp_report_req +:Returns (out): struct snp_report_resp on success, -negative on error + +The SNP_GET_REPORT ioctl can be used to query the attestation report from the +SEV-SNP firmware. The ioctl uses the SNP_GUEST_REQUEST (MSG_REPORT_REQ) command +provided by the SEV-SNP firmware to query the attestation report. + +On success, the snp_report_resp.data will contains the report. The report +format is described in the SEV-SNP specification. See the SEV-SNP specification +for further details. diff --git a/drivers/virt/Kconfig b/drivers/virt/Kconfig index 8061e8ef449f..e457e47610d3 100644 --- a/drivers/virt/Kconfig +++ b/drivers/virt/Kconfig @@ -36,4 +36,7 @@ source "drivers/virt/vboxguest/Kconfig" source "drivers/virt/nitro_enclaves/Kconfig" source "drivers/virt/acrn/Kconfig" + +source "drivers/virt/coco/sevguest/Kconfig" + endif diff --git a/drivers/virt/Makefile b/drivers/virt/Makefile index 3e272ea60cd9..9c704a6fdcda 100644 --- a/drivers/virt/Makefile +++ b/drivers/virt/Makefile @@ -8,3 +8,4 @@ obj-y += vboxguest/ obj-$(CONFIG_NITRO_ENCLAVES) += nitro_enclaves/ obj-$(CONFIG_ACRN_HSM) += acrn/ +obj-$(CONFIG_SEV_GUEST) += coco/sevguest/ diff --git a/drivers/virt/coco/sevguest/Kconfig b/drivers/virt/coco/sevguest/Kconfig new file mode 100644 index 000000000000..96190919cca8 --- /dev/null +++ b/drivers/virt/coco/sevguest/Kconfig @@ -0,0 +1,9 @@ +config SEV_GUEST + tristate "AMD SEV Guest driver" + default y + depends on AMD_MEM_ENCRYPT && CRYPTO_AEAD2 + help + The driver can be used by the SEV-SNP guest to communicate with the PSP to + request the attestation report and more. + + If you choose 'M' here, this module will be called sevguest. diff --git a/drivers/virt/coco/sevguest/Makefile b/drivers/virt/coco/sevguest/Makefile new file mode 100644 index 000000000000..b1ffb2b4177b --- /dev/null +++ b/drivers/virt/coco/sevguest/Makefile @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_SEV_GUEST) += sevguest.o diff --git a/drivers/virt/coco/sevguest/sevguest.c b/drivers/virt/coco/sevguest/sevguest.c new file mode 100644 index 000000000000..f3f86f9b5b22 --- /dev/null +++ b/drivers/virt/coco/sevguest/sevguest.c @@ -0,0 +1,449 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * AMD Secure Encrypted Virtualization Nested Paging (SEV-SNP) guest request interface + * + * Copyright (C) 2021 Advanced Micro Devices, Inc. + * + * Author: Brijesh Singh + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sevguest.h" + +#define DEVICE_NAME "sev-guest" +#define AAD_LEN 48 +#define MSG_HDR_VER 1 + +struct snp_guest_crypto { + struct crypto_aead *tfm; + uint8_t *iv, *authtag; + int iv_len, a_len; +}; + +struct snp_guest_dev { + struct device *dev; + struct miscdevice misc; + + struct snp_guest_crypto *crypto; + struct snp_guest_msg *request, *response; +}; + +static u8 vmpck_id; +static DEFINE_MUTEX(snp_cmd_mutex); + +static inline struct snp_guest_dev *to_snp_dev(struct file *file) +{ + struct miscdevice *dev = file->private_data; + + return container_of(dev, struct snp_guest_dev, misc); +} + +static struct snp_guest_crypto *init_crypto(struct snp_guest_dev *snp_dev, uint8_t *key, + size_t keylen) +{ + struct snp_guest_crypto *crypto; + + crypto = kzalloc(sizeof(*crypto), GFP_KERNEL_ACCOUNT); + if (!crypto) + return NULL; + + crypto->tfm = crypto_alloc_aead("gcm(aes)", 0, 0); + if (IS_ERR(crypto->tfm)) + goto e_free; + + if (crypto_aead_setkey(crypto->tfm, key, keylen)) + goto e_free_crypto; + + crypto->iv_len = crypto_aead_ivsize(crypto->tfm); + if (crypto->iv_len < 12) { + dev_err(snp_dev->dev, "IV length is less than 12.\n"); + goto e_free_crypto; + } + + crypto->iv = kmalloc(crypto->iv_len, GFP_KERNEL_ACCOUNT); + if (!crypto->iv) + goto e_free_crypto; + + if (crypto_aead_authsize(crypto->tfm) > MAX_AUTHTAG_LEN) { + if (crypto_aead_setauthsize(crypto->tfm, MAX_AUTHTAG_LEN)) { + dev_err(snp_dev->dev, "failed to set authsize to %d\n", MAX_AUTHTAG_LEN); + goto e_free_crypto; + } + } + + crypto->a_len = crypto_aead_authsize(crypto->tfm); + crypto->authtag = kmalloc(crypto->a_len, GFP_KERNEL_ACCOUNT); + if (!crypto->authtag) + goto e_free_crypto; + + return crypto; + +e_free_crypto: + crypto_free_aead(crypto->tfm); +e_free: + kfree(crypto->iv); + kfree(crypto->authtag); + kfree(crypto); + + return NULL; +} + +static void deinit_crypto(struct snp_guest_crypto *crypto) +{ + crypto_free_aead(crypto->tfm); + kfree(crypto->iv); + kfree(crypto->authtag); + kfree(crypto); +} + +static int enc_dec_message(struct snp_guest_crypto *crypto, struct snp_guest_msg *msg, + uint8_t *src_buf, uint8_t *dst_buf, size_t len, bool enc) +{ + struct snp_guest_msg_hdr *hdr = &msg->hdr; + struct scatterlist src[3], dst[3]; + DECLARE_CRYPTO_WAIT(wait); + struct aead_request *req; + int ret; + + req = aead_request_alloc(crypto->tfm, GFP_KERNEL); + if (!req) + return -ENOMEM; + + /* + * AEAD memory operations: + * +------ AAD -------+------- DATA -----+---- AUTHTAG----+ + * | msg header | plaintext | hdr->authtag | + * | bytes 30h - 5Fh | or | | + * | | cipher | | + * +------------------+------------------+----------------+ + */ + sg_init_table(src, 3); + sg_set_buf(&src[0], &hdr->algo, AAD_LEN); + sg_set_buf(&src[1], src_buf, hdr->msg_sz); + sg_set_buf(&src[2], hdr->authtag, crypto->a_len); + + sg_init_table(dst, 3); + sg_set_buf(&dst[0], &hdr->algo, AAD_LEN); + sg_set_buf(&dst[1], dst_buf, hdr->msg_sz); + sg_set_buf(&dst[2], hdr->authtag, crypto->a_len); + + aead_request_set_ad(req, AAD_LEN); + aead_request_set_tfm(req, crypto->tfm); + aead_request_set_callback(req, 0, crypto_req_done, &wait); + + aead_request_set_crypt(req, src, dst, len, crypto->iv); + ret = crypto_wait_req(enc ? crypto_aead_encrypt(req) : crypto_aead_decrypt(req), &wait); + + aead_request_free(req); + return ret; +} + +static int __enc_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, + void *plaintext, size_t len) +{ + struct snp_guest_crypto *crypto = snp_dev->crypto; + struct snp_guest_msg_hdr *hdr = &msg->hdr; + + memset(crypto->iv, 0, crypto->iv_len); + memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + + return enc_dec_message(crypto, msg, plaintext, msg->payload, len, true); +} + +static int dec_payload(struct snp_guest_dev *snp_dev, struct snp_guest_msg *msg, + void *plaintext, size_t len) +{ + struct snp_guest_crypto *crypto = snp_dev->crypto; + struct snp_guest_msg_hdr *hdr = &msg->hdr; + + /* Build IV with response buffer sequence number */ + memset(crypto->iv, 0, crypto->iv_len); + memcpy(crypto->iv, &hdr->msg_seqno, sizeof(hdr->msg_seqno)); + + return enc_dec_message(crypto, msg, msg->payload, plaintext, len, false); +} + +static int verify_and_dec_payload(struct snp_guest_dev *snp_dev, void *payload, u32 sz) +{ + struct snp_guest_crypto *crypto = snp_dev->crypto; + struct snp_guest_msg *resp = snp_dev->response; + struct snp_guest_msg *req = snp_dev->request; + struct snp_guest_msg_hdr *req_hdr = &req->hdr; + struct snp_guest_msg_hdr *resp_hdr = &resp->hdr; + + dev_dbg(snp_dev->dev, "response [seqno %lld type %d version %d sz %d]\n", + resp_hdr->msg_seqno, resp_hdr->msg_type, resp_hdr->msg_version, resp_hdr->msg_sz); + + /* Verify that the sequence counter is incremented by 1 */ + if (unlikely(resp_hdr->msg_seqno != (req_hdr->msg_seqno + 1))) + return -EBADMSG; + + /* Verify response message type and version number. */ + if ((resp_hdr->msg_type != (req_hdr->msg_type + 1)) || + (resp_hdr->msg_version != req_hdr->msg_version)) + return -EBADMSG; + + /* + * If the message size is greater than our buffer length then return + * an error. + */ + if (unlikely((resp_hdr->msg_sz + crypto->a_len) > sz)) + return -EBADMSG; + + return dec_payload(snp_dev, resp, payload, resp_hdr->msg_sz + crypto->a_len); +} + +static bool enc_payload(struct snp_guest_dev *snp_dev, int version, u8 type, + void *payload, size_t sz) +{ + struct snp_guest_msg *req = snp_dev->request; + struct snp_guest_msg_hdr *hdr = &req->hdr; + + memset(req, 0, sizeof(*req)); + + hdr->algo = SNP_AEAD_AES_256_GCM; + hdr->hdr_version = MSG_HDR_VER; + hdr->hdr_sz = sizeof(*hdr); + hdr->msg_type = type; + hdr->msg_version = version; + hdr->msg_seqno = snp_msg_seqno(); + hdr->msg_vmpck = vmpck_id; + hdr->msg_sz = sz; + + dev_dbg(snp_dev->dev, "request [seqno %lld type %d version %d sz %d]\n", + hdr->msg_seqno, hdr->msg_type, hdr->msg_version, hdr->msg_sz); + + return __enc_payload(snp_dev, req, payload, sz); +} + +static int handle_guest_request(struct snp_guest_dev *snp_dev, int version, u8 type, + void *req_buf, size_t req_sz, void *resp_buf, + u32 resp_sz, __u64 *fw_err) +{ + struct snp_guest_request_data data; + unsigned long err; + int rc; + + memset(snp_dev->response, 0, sizeof(*snp_dev->response)); + + /* Encrypt the userspace provided payload */ + rc = enc_payload(snp_dev, version, type, req_buf, req_sz); + if (rc) + return rc; + + /* Call firmware to process the request */ + data.req_gpa = __pa(snp_dev->request); + data.resp_gpa = __pa(snp_dev->response); + rc = snp_issue_guest_request(GUEST_REQUEST, &data, &err); + + if (fw_err) + *fw_err = err; + + if (rc) + return rc; + + return verify_and_dec_payload(snp_dev, resp_buf, resp_sz); +} + +static int get_report(struct snp_guest_dev *snp_dev, struct snp_user_guest_request *arg) +{ + struct snp_guest_crypto *crypto = snp_dev->crypto; + struct snp_report_resp *resp; + struct snp_report_req req; + int rc, resp_len; + + if (!arg->req_data || !arg->resp_data) + return -EINVAL; + + /* Copy the request payload from the userspace */ + if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) + return -EFAULT; + + /* Message version must be non-zero */ + if (!req.msg_version) + return -EINVAL; + + /* + * The intermediate response buffer is used while decrypting the + * response payload. Make sure that it has enough space to cover the + * authtag. + */ + resp_len = sizeof(resp->data) + crypto->a_len; + resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); + if (!resp) + return -ENOMEM; + + /* Issue the command to get the attestation report */ + rc = handle_guest_request(snp_dev, req.msg_version, SNP_MSG_REPORT_REQ, + &req.user_data, sizeof(req.user_data), resp->data, resp_len, + &arg->fw_err); + if (rc) + goto e_free; + + /* Copy the response payload to userspace */ + if (copy_to_user((void __user *)arg->resp_data, resp, sizeof(*resp))) + rc = -EFAULT; + +e_free: + kfree(resp); + return rc; +} + +static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) +{ + struct snp_guest_dev *snp_dev = to_snp_dev(file); + void __user *argp = (void __user *)arg; + struct snp_user_guest_request input; + int ret = -ENOTTY; + + if (copy_from_user(&input, argp, sizeof(input))) + return -EFAULT; + + mutex_lock(&snp_cmd_mutex); + + switch (ioctl) { + case SNP_GET_REPORT: { + ret = get_report(snp_dev, &input); + break; + } + default: + break; + } + + mutex_unlock(&snp_cmd_mutex); + + if (copy_to_user(argp, &input, sizeof(input))) + return -EFAULT; + + return ret; +} + +static void free_shared_pages(void *buf, size_t sz) +{ + unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + + /* If fail to restore the encryption mask then leak it. */ + if (set_memory_encrypted((unsigned long)buf, npages)) + return; + + __free_pages(virt_to_page(buf), get_order(sz)); +} + +static void *alloc_shared_pages(size_t sz) +{ + unsigned int npages = PAGE_ALIGN(sz) >> PAGE_SHIFT; + struct page *page; + int ret; + + page = alloc_pages(GFP_KERNEL_ACCOUNT, get_order(sz)); + if (IS_ERR(page)) + return NULL; + + ret = set_memory_decrypted((unsigned long)page_address(page), npages); + if (ret) { + __free_pages(page, get_order(sz)); + return NULL; + } + + return page_address(page); +} + +static const struct file_operations snp_guest_fops = { + .owner = THIS_MODULE, + .unlocked_ioctl = snp_guest_ioctl, +}; + +static int __init snp_guest_probe(struct platform_device *pdev) +{ + struct snp_guest_platform_data *data; + struct device *dev = &pdev->dev; + struct snp_guest_dev *snp_dev; + struct miscdevice *misc; + int ret; + + if (!dev->platform_data) + return -ENODEV; + + data = (struct snp_guest_platform_data *)dev->platform_data; + vmpck_id = data->vmpck_id; + + snp_dev = devm_kzalloc(&pdev->dev, sizeof(struct snp_guest_dev), GFP_KERNEL); + if (!snp_dev) + return -ENOMEM; + + platform_set_drvdata(pdev, snp_dev); + snp_dev->dev = dev; + + snp_dev->crypto = init_crypto(snp_dev, data->vmpck, sizeof(data->vmpck)); + if (!snp_dev->crypto) + return -EIO; + + /* Allocate the shared page used for the request and response message. */ + snp_dev->request = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (IS_ERR(snp_dev->request)) { + ret = PTR_ERR(snp_dev->request); + goto e_free_crypto; + } + + snp_dev->response = alloc_shared_pages(sizeof(struct snp_guest_msg)); + if (IS_ERR(snp_dev->response)) { + ret = PTR_ERR(snp_dev->response); + goto e_free_req; + } + + misc = &snp_dev->misc; + misc->minor = MISC_DYNAMIC_MINOR; + misc->name = DEVICE_NAME; + misc->fops = &snp_guest_fops; + + return misc_register(misc); + +e_free_req: + free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); + +e_free_crypto: + deinit_crypto(snp_dev->crypto); + + return ret; +} + +static int __exit snp_guest_remove(struct platform_device *pdev) +{ + struct snp_guest_dev *snp_dev = platform_get_drvdata(pdev); + + free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); + free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); + deinit_crypto(snp_dev->crypto); + misc_deregister(&snp_dev->misc); + + return 0; +} + +static struct platform_driver snp_guest_driver = { + .remove = __exit_p(snp_guest_remove), + .driver = { + .name = "snp-guest", + }, +}; + +module_platform_driver_probe(snp_guest_driver, snp_guest_probe); + +MODULE_AUTHOR("Brijesh Singh "); +MODULE_LICENSE("GPL"); +MODULE_VERSION("1.0.0"); +MODULE_DESCRIPTION("AMD SNP Guest Driver"); diff --git a/drivers/virt/coco/sevguest/sevguest.h b/drivers/virt/coco/sevguest/sevguest.h new file mode 100644 index 000000000000..4cd2f8b81154 --- /dev/null +++ b/drivers/virt/coco/sevguest/sevguest.h @@ -0,0 +1,63 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Copyright (C) 2021 Advanced Micro Devices, Inc. + * + * Author: Brijesh Singh + * + * SEV-SNP API spec is available at https://developer.amd.com/sev + */ + +#ifndef __LINUX_SEVGUEST_H_ +#define __LINUX_SEVGUEST_H_ + +#include + +#define MAX_AUTHTAG_LEN 32 + +/* See SNP spec SNP_GUEST_REQUEST section for the structure */ +enum msg_type { + SNP_MSG_TYPE_INVALID = 0, + SNP_MSG_CPUID_REQ, + SNP_MSG_CPUID_RSP, + SNP_MSG_KEY_REQ, + SNP_MSG_KEY_RSP, + SNP_MSG_REPORT_REQ, + SNP_MSG_REPORT_RSP, + SNP_MSG_EXPORT_REQ, + SNP_MSG_EXPORT_RSP, + SNP_MSG_IMPORT_REQ, + SNP_MSG_IMPORT_RSP, + SNP_MSG_ABSORB_REQ, + SNP_MSG_ABSORB_RSP, + SNP_MSG_VMRK_REQ, + SNP_MSG_VMRK_RSP, + + SNP_MSG_TYPE_MAX +}; + +enum aead_algo { + SNP_AEAD_INVALID, + SNP_AEAD_AES_256_GCM, +}; + +struct snp_guest_msg_hdr { + u8 authtag[MAX_AUTHTAG_LEN]; + u64 msg_seqno; + u8 rsvd1[8]; + u8 algo; + u8 hdr_version; + u16 hdr_sz; + u8 msg_type; + u8 msg_version; + u16 msg_sz; + u32 rsvd2; + u8 msg_vmpck; + u8 rsvd3[35]; +} __packed; + +struct snp_guest_msg { + struct snp_guest_msg_hdr hdr; + u8 payload[4000]; +} __packed; + +#endif /* __LINUX_SNP_GUEST_H__ */ diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h new file mode 100644 index 000000000000..e8cfd15133f3 --- /dev/null +++ b/include/uapi/linux/sev-guest.h @@ -0,0 +1,44 @@ +/* SPDX-License-Identifier: GPL-2.0-only WITH Linux-syscall-note */ +/* + * Userspace interface for AMD SEV and SEV-SNP guest driver. + * + * Copyright (C) 2021 Advanced Micro Devices, Inc. + * + * Author: Brijesh Singh + * + * SEV API specification is available at: https://developer.amd.com/sev/ + */ + +#ifndef __UAPI_LINUX_SEV_GUEST_H_ +#define __UAPI_LINUX_SEV_GUEST_H_ + +#include + +struct snp_report_req { + /* message version number (must be non-zero) */ + __u8 msg_version; + + /* user data that should be included in the report */ + __u8 user_data[64]; +}; + +struct snp_report_resp { + /* response data, see SEV-SNP spec for the format */ + __u8 data[4000]; +}; + +struct snp_user_guest_request { + /* Request and response structure address */ + __u64 req_data; + __u64 resp_data; + + /* firmware error code on failure (see psp-sev.h) */ + __u64 fw_err; +}; + +#define SNP_GUEST_REQ_IOC_TYPE 'S' + +/* Get SNP attestation report */ +#define SNP_GET_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x0, struct snp_user_guest_request) + +#endif /* __UAPI_LINUX_SEV_GUEST_H_ */ From patchwork Wed Jul 7 18:15:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363389 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,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 87B0DC07E9C for ; Wed, 7 Jul 2021 18:17:05 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 15CC461C6D for ; Wed, 7 Jul 2021 18:17:05 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 15CC461C6D Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0120F6B0078; Wed, 7 Jul 2021 14:17:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id F27536B007B; Wed, 7 Jul 2021 14:17:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D7AD56B007D; Wed, 7 Jul 2021 14:17:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0027.hostedemail.com [216.40.44.27]) by kanga.kvack.org (Postfix) with ESMTP id B1E9E6B0078 for ; Wed, 7 Jul 2021 14:17:04 -0400 (EDT) Received: from smtpin28.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 1EFEB181286E8 for ; Wed, 7 Jul 2021 18:17:04 +0000 (UTC) X-FDA: 78336598368.28.0357FED Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2045.outbound.protection.outlook.com [40.107.243.45]) by imf21.hostedemail.com (Postfix) with ESMTP id 857B5D00285B for ; Wed, 7 Jul 2021 18:17:01 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Of4slQiKLwMtGjxmYKnrf1DMlecyWhsSbvPzHBEn7YzWvKhDxMJTUzGWJMkEU4gPCoy4WC78pAG8CspnlcVsxh7u22VZKBm6N3tVWk2Ki9y+Yz/uKUCAf+1aH5Drjl3GIxF35LJfqiOW6p1aB4lgAhAkdqy+LfcQehOP712F0KPchGcwifX34QTQB1bXo7/KhnvPx+VxHv0EcmdIY3jnBcU+ExfFMsqcaGXhSBzgQfVicoH5kZv7P4wUKmk6a0RpryvUh6RGfbZyrUaXx9Bo2r7YhVhrbyJkW2xNn16c2cbJCmVl7OFNLao/nucKyMEiKSFXzgZvokfehx1JxHeZwQ== 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=W/IHMOVem7t7z2OV4kUnI967SHyn8TUBIuL3M5S8ay0=; b=BppHbiDQXPrApQ/vUUQsFlEyu7CRznKvdGP4wvBOdYvhdSQHkuxONKO/ksoVsPwXD4w4cnOVeNZ+5ae0c6TBqyih3NQUPTLvglmiimxAefa6xUbUJShsAhcF2/bUySch5/y3g6uVpygEsZs22BvEhBb3fje2OlRAPlZirpW/8cGq0MhvX0THQlnhhrXya+Xi7rwfvLNsTZvOYDJU4JWfifBOuCPkqk014U4C7vwCXAUx8MaOhrsZVJzVmOzIch+SOmxH8wl9mp4drhWRdsadiEH36015YD33dsbo+2CbvYauj/Rxv3PdooVPTyK6I9IuaF/YLNaxXhEj0xtmX/b9Hw== 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=W/IHMOVem7t7z2OV4kUnI967SHyn8TUBIuL3M5S8ay0=; b=ScNWuFLi1miBzEuFDcjz5x/Frwzoy5kbgu80Hx6liy4J3yYBoRN0tSsFi+8TgVqgUvrLgevn64CInkzieSTLD1fB1NIS82OJaE/SwNjIVcfApkgKIKjTaMg/8TOtmnsUIcfF0IZ1CoDqi2f2mxQuPWAWCx9YBG7ba/qbWnkEtFk= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BYAPR12MB3527.namprd12.prod.outlook.com (2603:10b6:a03:13c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:16:58 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:16:58 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 35/36] virt: sevguest: Add support to derive key Date: Wed, 7 Jul 2021 13:15:05 -0500 Message-Id: <20210707181506.30489-36-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:56 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 2112b32f-7620-48c1-6a87-08d9417368a5 X-MS-TrafficTypeDiagnostic: BYAPR12MB3527: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2582; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: o5ydBn21GoXp2gFKS66M3DU/1eBS5DX4C/yWXkFHrjRLqvCvz5kvcOTwTqCkVHzCE1V//e3WNikES/2w/HF+eUGACHLP8Ast9pbD8lIs9Z0Xs1JR2nouy2Pj9xryWrt3CaeXgqL61d2tUIboYCkWSS1F3YwLXAI9+Sgm/wcx9jm0M9i/rJ30FQ9eAG3EzqxFa66SY/pbr2aekJFr+LaHpYFIUnnWJGSg0ZmJmoCyCFcdnnaU8T5CGBY8iTSvs+SK4Oh4FifUG479rtyXtgOF4kz2TqUphHsud361Do6Enz8tiS/kMjheawiGtQN5Pbf7k3/bH4gDvdoS8ZxJIy7G5P4xKudCiMmC6gBDwt8CZy8ebz8CLRnU40Uqe7BdU3YTiQFjIlbKI8xdep42TkIlziSIjYX+iGUsORg9pkwI3xxI3FKLiqJV4sVB3beyosumceXLnMrpOPX+6d38785nabbQ9uOKYk10PSw26SZbc7kYWuFOnQcdn2lbFmGW2wAPLNnfTJwkX2VI7UOGVGMIjEg0sk728CTzzcD6Eb9YRnp6xc64wIdplCmxBnGuVunr+T4jPuBPVJUDjF+9WF8O6+FCJm30yMnueDLSQS4Te0zgw22iknxOHcxMHbsrBhTnA1n3wi0TqEQcAiVkvOFTdipEc3LoiI3ZBG848/6VA+siuG5CtFZSJXnWvuT9ZRmz+v7RGXmeztbSKAuiG5yuqg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(2616005)(5660300002)(498600001)(44832011)(6486002)(1076003)(66946007)(66556008)(83380400001)(86362001)(186003)(956004)(66476007)(8936002)(8676002)(52116002)(36756003)(54906003)(2906002)(7696005)(38100700002)(38350700002)(4326008)(6666004)(7406005)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: x6joJQOXuDsZ6RYp5TrE4w4WeXP5EmCBEixt/hxI4gKEyXrb9c/nvLVNjYkeqvMsi0CyBOm5Yl8xiHWMJkgpJlc9fpxf44fln2Y2FadDBbZdnDYaNDpO+DB/KoycXVfdgpRJlfRwptAvXhzWtNQIs/x32rDAiFBRjXNPEurq2yuW64Chy2jX7Yl5tDNfshmUf/0Wl8QmtSAUSMMyaId+f9F9AgoT9qhhVYG1RztfLcgN7vM+bw6v03wwpq+zZJqbfmMAFeYHRV0hb2dP29lXaw6yI/rHACF+31os0tGZtLyk5F+5gMufHPUhaKs2SOhfa5OituigBBRSKtl0+586Ft4BknJoWak+glxQqco4BGMf4OlXVu7f/dZJpFF/a2WzVQTYzLybjtGtv9IdgX69Fu6a7LxVf+SkdDYMXAHRb211ZIHcivbMCMC4mXregGfwSL2GApLli5XYY/+31VeDxYEovThiNOeLoac8rWlaWcJ72L7QnOQ3A9KuhFRC0cOng2+FFhlFaNgkubMomKWoogR8otInjebPo4eyIyPMy7TgDJx8rjNIj1CJZw+CnEjDr3YCyGwd+O80GfLqeyCJeJXoJVAWdlZsFbg/lMiZl71a68A8WTlkTUqKtY6twGmhut3/zNd3qJJ6aJIZj8N3d3wfXp1Ay5zEJXsmChXskPI0AFrYX/054heGc9NOf132dlApOBzI8a61PhDjokuY/SP5UNDiiQp3oDmGDr98LpdPqBZJ9bGKbZBXZm7VK3o8W1dqjQHhhqyUXHwqTFDOdnZCGlHOSi8+7dWKs6/OMcwbr3Rm6todb1u89mxkAE+MOLufiHwJXNISxAOldiIlneLdHiJi0RL4TUutwW9aXSvWVYp5E5wdj4xMiQ+oSthTDWSNh6Lii+QcsAHJgcMKTkpckg0DOAx560VVDnDf1e1+4mw3juERPmOUhRDJV8vBrAd7wgkU0j6Xn+4sw2OrHrQTC7yV/CI31E2jwGFB7SiIqpORVXhc271K4YD3+eO9U5gL1PGYjK5hC93vGQcZWH1sTjQ8eZolpyNq10QiOE9eptGLOoL/r0ySZFAxtB/H5Gh6jOIw1rCBv+9UR7G3KBhuAein9P8AqNdfhXYe7VRmgxU73t3Wj54E0z8ucHNkr5PPVIxXInxhGKOWFm1pXavuGXiuMfNt9b00gWq6bc/Ocw9vtwfBNVAbW2epwF2ahxmykV0DES+w1A2uzhUypNjCIyJirPZmZ4UCeYG32RVinjbB1hXDoAfJ/EV9MzktVWpbq/IY1BXRnw84Qc/ffPQX9p6t5rBN68BQHSVM4XZ4oK52UIEP1BqknX+RIYDh X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 2112b32f-7620-48c1-6a87-08d9417368a5 X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:16:58.4460 (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: OvYpKSAjJuph2bQ0VjXhid9EUr/KNOjPeQlkvVBfvGYXZ8RlF5lPxmEbh97FNPmZkSg/60qZ4qTi6lJi91aEPA== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3527 X-Rspam-User: nil Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=ScNWuFLi; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 857B5D00285B X-Stat-Signature: g9bqnte4uorcscmouxwg9u7mrqi76hcy X-HE-Tag: 1625681821-255008 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: The SNP_GET_DERIVED_KEY ioctl interface can be used by the SNP guest to ask the firmware to provide a key derived from a root key. The derived key may be used by the guest for any purposes it choose, such as a sealing key or communicating with the external entities. See SEV-SNP firmware spec for more information. Signed-off-by: Brijesh Singh --- Documentation/virt/coco/sevguest.rst | 18 ++++++++++ drivers/virt/coco/sevguest/sevguest.c | 48 +++++++++++++++++++++++++++ include/uapi/linux/sev-guest.h | 24 ++++++++++++++ 3 files changed, 90 insertions(+) diff --git a/Documentation/virt/coco/sevguest.rst b/Documentation/virt/coco/sevguest.rst index 52d5915037ef..25446670d816 100644 --- a/Documentation/virt/coco/sevguest.rst +++ b/Documentation/virt/coco/sevguest.rst @@ -67,3 +67,21 @@ provided by the SEV-SNP firmware to query the attestation report. On success, the snp_report_resp.data will contains the report. The report format is described in the SEV-SNP specification. See the SEV-SNP specification for further details. + +2.2 SNP_GET_DERIVED_KEY +----------------------- +:Technology: sev-snp +:Type: guest ioctl +:Parameters (in): struct snp_derived_key_req +:Returns (out): struct snp_derived_key_req on success, -negative on error + +The SNP_GET_DERIVED_KEY ioctl can be used to get a key derive from a root key. +The derived key can be used by the guest for any purpose, such as sealing keys +or communicating with external entities. + +The ioctl uses the SNP_GUEST_REQUEST (MSG_KEY_REQ) command provided by the +SEV-SNP firmware to derive the key. See SEV-SNP specification for further details +on the various fileds passed in the key derivation request. + +On success, the snp_derived_key_resp.data will contains the derived key +value. diff --git a/drivers/virt/coco/sevguest/sevguest.c b/drivers/virt/coco/sevguest/sevguest.c index f3f86f9b5b22..2f20db80490a 100644 --- a/drivers/virt/coco/sevguest/sevguest.c +++ b/drivers/virt/coco/sevguest/sevguest.c @@ -304,6 +304,50 @@ static int get_report(struct snp_guest_dev *snp_dev, struct snp_user_guest_reque return rc; } +static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_user_guest_request *arg) +{ + struct snp_guest_crypto *crypto = snp_dev->crypto; + struct snp_derived_key_resp *resp; + struct snp_derived_key_req req; + int rc, resp_len; + + if (!arg->req_data || !arg->resp_data) + return -EINVAL; + + /* Copy the request payload from the userspace */ + if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) + return -EFAULT; + + /* Message version must be non-zero */ + if (!req.msg_version) + return -EINVAL; + + /* + * The intermediate response buffer is used while decrypting the + * response payload. Make sure that it has enough space to cover the + * authtag. + */ + resp_len = sizeof(resp->data) + crypto->a_len; + resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); + if (!resp) + return -ENOMEM; + + /* Issue the command to get the attestation report */ + rc = handle_guest_request(snp_dev, req.msg_version, SNP_MSG_KEY_REQ, + &req.data, sizeof(req.data), resp->data, resp_len, + &arg->fw_err); + if (rc) + goto e_free; + + /* Copy the response payload to userspace */ + if (copy_to_user((void __user *)arg->resp_data, resp, sizeof(*resp))) + rc = -EFAULT; + +e_free: + kfree(resp); + return rc; +} + static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { struct snp_guest_dev *snp_dev = to_snp_dev(file); @@ -321,6 +365,10 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long ret = get_report(snp_dev, &input); break; } + case SNP_GET_DERIVED_KEY: { + ret = get_derived_key(snp_dev, &input); + break; + } default: break; } diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index e8cfd15133f3..621a9167df7a 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -36,9 +36,33 @@ struct snp_user_guest_request { __u64 fw_err; }; +struct __snp_derived_key_req { + __u32 root_key_select; + __u32 rsvd; + __u64 guest_field_select; + __u32 vmpl; + __u32 guest_svn; + __u64 tcb_version; +}; + +struct snp_derived_key_req { + /* message version number (must be non-zero) */ + __u8 msg_version; + + struct __snp_derived_key_req data; +}; + +struct snp_derived_key_resp { + /* response data, see SEV-SNP spec for the format */ + __u8 data[64]; +}; + #define SNP_GUEST_REQ_IOC_TYPE 'S' /* Get SNP attestation report */ #define SNP_GET_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x0, struct snp_user_guest_request) +/* Get a derived key from the root */ +#define SNP_GET_DERIVED_KEY _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x1, struct snp_user_guest_request) + #endif /* __UAPI_LINUX_SEV_GUEST_H_ */ From patchwork Wed Jul 7 18:15:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12363391 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,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 923E0C11F69 for ; Wed, 7 Jul 2021 18:17:06 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 31B1E61C6D for ; Wed, 7 Jul 2021 18:17:06 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 31B1E61C6D Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 20D166B007B; Wed, 7 Jul 2021 14:17:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 16E9D6B007D; Wed, 7 Jul 2021 14:17:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDA376B007E; Wed, 7 Jul 2021 14:17:05 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0116.hostedemail.com [216.40.44.116]) by kanga.kvack.org (Postfix) with ESMTP id C78586B007B for ; Wed, 7 Jul 2021 14:17:05 -0400 (EDT) Received: from smtpin35.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 36059256E5 for ; Wed, 7 Jul 2021 18:17:05 +0000 (UTC) X-FDA: 78336598410.35.13CC171 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2045.outbound.protection.outlook.com [40.107.243.45]) by imf21.hostedemail.com (Postfix) with ESMTP id 293A7D00285B for ; Wed, 7 Jul 2021 18:17:04 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=a8TzpyaouPdKmfY4VaTGBurktzTrtruY+K6Oxb0UUl8DBSckD7mAQkCeUILEOd/G0SRnk/k1D544eEKr5Q+CAfAjMj0LJggCKLrA0wRno2HvJjO6/bgBa2OAe2W02JrGnLiU7Ps7IOJp5k2B4eQuMLgBOx3ciJszTaGtLzA3x3CAEhHOUq8dIVr1dIpE5qkCGOMqrFcJSKZ/0RgyuGAPv7CDzH/paB8qQggayUJlHE08Z8KG9i0f55g5MqwIMb93SCoQBPWy33rLID59atfuluk/aYgzi70YqbrSJBEFXrf861LuDLSb83rxQ4q+H7WjG1FMOaIRLiDhYb9FATUolw== 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=akna8e/oCRqjIlvjGoOzsD7jYm3bBYT+NhbNWSHJbQo=; b=Igwmvv2Q6K58RFbMT0LjyhI9d7gNQzlNVpM8VQyNvmpvI881IzbfxbcDhpR22LaRzGtwXlHlyK8FJaWBcjYc8v4BNl4EZ/y9i0pdEw+eWiw3eC0oNZ9vXQLLYsaUzTH8KYXsBvTwwDO2HRNWEwRa9ryqU0KTMa4nsq5XAJakNsCQymt1CDMRtzb5JH1HhKzXYf0kzfzia0X0bRA/IwsTSvBOZI0Ja1e01BhCnASxOGGFZn2j3CElQG7nf+2m/FNd3gviglwxWbbww2UNtXere2cTLgMeswXwTLzdS+rn85AkYFXKTg6ZXYk3X7weCpTkDeu18nHvHwM9awV3rTR1bA== 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=akna8e/oCRqjIlvjGoOzsD7jYm3bBYT+NhbNWSHJbQo=; b=Eci1ks7z2w9BpaT4R2a5FOBov0w65TWdJqk101eafs4O2OAV+0/DQzLfhs7rKJa/0WhQeDVoAUZSBdSh7SKpVpfg/kbfQAcFM9nAO1QAjqatrcOKdPRj6ABKVqfyUIGYQfDg5FVf2Qi8o7o3oP6QxLNHqCPHsEA/xT4vTtV+iK8= Received: from BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) by BYAPR12MB3527.namprd12.prod.outlook.com (2603:10b6:a03:13c::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.20; Wed, 7 Jul 2021 18:17:01 +0000 Received: from BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed]) by BYAPR12MB2711.namprd12.prod.outlook.com ([fe80::40e3:aade:9549:4bed%7]) with mapi id 15.20.4287.033; Wed, 7 Jul 2021 18:17:01 +0000 From: Brijesh Singh To: x86@kernel.org, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-efi@vger.kernel.org, platform-driver-x86@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 , Dov Murik , Tobin Feldman-Fitzthum , Borislav Petkov , Michael Roth , Vlastimil Babka , tony.luck@intel.com, npmccallum@redhat.com, brijesh.ksingh@gmail.com, Brijesh Singh Subject: [PATCH Part1 RFC v4 36/36] virt: sevguest: Add support to get extended report Date: Wed, 7 Jul 2021 13:15:06 -0500 Message-Id: <20210707181506.30489-37-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210707181506.30489-1-brijesh.singh@amd.com> References: <20210707181506.30489-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) To BYAPR12MB2711.namprd12.prod.outlook.com (2603:10b6:a03:63::10) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA0PR11CA0104.namprd11.prod.outlook.com (2603:10b6:806:d1::19) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4308.19 via Frontend Transport; Wed, 7 Jul 2021 18:16:58 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 199754e2-b797-4682-7f0e-08d941736a2d X-MS-TrafficTypeDiagnostic: BYAPR12MB3527: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:2803; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: EkljqQRdAT3v23NkVkwyjzkSx3nz1AgPCfCE1bYp7iu/ZrjPiL3/BkxbavP5ZJR3vVKYoNbIxSPUAD6D0vwm18u+lk4Bi9hfCFUx7+PwmrP7d8QuP5WmZ5iu3/pkiUK9OILKn9+XlG+5DnS9ukdrp9MPQxgoHeL5jCjZIjXE/zbPyUabh3s81vXvclqyi3f+KnWEKBINfWWosKyU2tRmwf/NJmgSBbyWVURYK1DiMKiUj39zO/aDk0g4X6bnr9bAHiUj7xlVJwgNyikSxpVW0g4/ZSgYPg+OfC1kzjj1ApBkaRi5AR7A/H6WQWh5xZnGIKceSpXuetGSBuN16TxB/o0cxk1dV4WDCGdFeH14hIqToT3V/Am4YXq2gshV9j/XPxJX0R2hiNO2tVo7KuWEkG2iqW1a9iM/nyfenHqieCreb7a5X6zQK7MtO0krRcoAC6ALbyLguZYaTHyV6VdhFiaCzYngaC+GYnQnT57DFIKa9FbKReIJskeiV1rnV+Q2xr/Qu368YQVLEFPiTPDE6gGf80lv0xdN5ZmQIDpjF8CgN2BfRCbQwUsSKRrYF3aUij4IicfJCbyT0bGmuqaYWJQzRhu3g2p2iRIAseBVFyjqbozOKgmUweE5XIz4fBEqelCyyMHVHVe4E3gEDpA98v0ri1iNhk4Z5bu39z//hetraYq4ECFnvO9h+c+CKbpWfqfh4GHN/d8gbS8sENQzOw== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:BYAPR12MB2711.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(26005)(2616005)(5660300002)(498600001)(44832011)(6486002)(1076003)(66946007)(66556008)(83380400001)(86362001)(186003)(956004)(66476007)(8936002)(8676002)(52116002)(36756003)(54906003)(2906002)(7696005)(38100700002)(38350700002)(4326008)(6666004)(7406005)(7416002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: VYpfv6Siey88z3oNh9g4LboVPnlbDUYLRlA/JjIm1v+HSMr4g7H+zvlrbleR1VWWdYZdCGw0Erossxbu07/G+UZ/jhNZaK/lSvtxqs6CuAldsa84pJdnltr9dFQUC9rDrFv/i4zOlhMimJk62s5Dp1PXL1OAn1mn5kzcIZrpRYEXNZWMLul93UIQ9SvDx0ddmFRXxkwDS8LxQrGMDUOl8dfbbaqYQroSe7gw9mZHbyIx9hJ/zysvyQJOYJyIEA1yPRF6DCn/+ZSSmgZbpLoUYmOzjfj8HqbXke9vuci9CV2Aewe8o05CqiBzPOGN4jSjeKqGvBdn0ghhaATcqhKdeGAM2TlhYDzou7lQns5uX7HiRe8u3myyMf4a3EWg6u20TsVpJw8iWRi7niPDLjkIqbPmlyY/Ahwz196iOVsjiZSi80uf9Fs31+CDkIWHKxBc985zfraGE6nPBxiHnDWg7Y3dLiyKgRTvg5w9heSZ/qd7Mm7wN8ImU/R1ghpAFePAxtwckgSfuZdB2nmXJsBpxwwdhNitqlfGLvY8CpRb5x9YpI+mcYAT5LTfRcQjKPktXT3qZiBOpV0KQj0IzKrSqn2HrJmzNMwtWYxvXDLg7LetwAbDHLcjZbzCxwmZwyPwj0XaTrru4q6LOq3xBJLr0cvrhw8PipKCKLywWlrcJo/N6kHP6LLnEypOlZmxCmOEqLGQeOIMQsZJHBpwpCAfeLDNNi1xSTN5SEY8Fz1SYRDCX1G0fX2m+Jw7WLVvPXhSYLk8o0TA/HkfQpEcnJwqI5BJfqVuiszmgVKOZ57E9gIB2nsOJatJxbd7VXE87ed8ag8bQoNdgpOZe6EznrxivR21hcRTofYL7ptaJiHdMqOkw/6pDaQ/2KcCG8bw+tQxpNnvy8NXbuks9d8PX2I254sFC08zFP5KvgyfC2g2BSayQIbhHnpH+W7Zo5Vjb2kkKaAJiUpAoXMrcGSKtDt20xTwHMHnP9WWQmFEo5MF4eCLp6dbjmeJsc/pYBAK88i/KVOnkZ7D+VjDn3UW6xKZn26M9C1E0urct+BVLZp6bgEk1FjMjQmiOd4Q8/87dCaEmBLsioPCpc7rYieyKfW9IYcONcVeLB8blGDhmaSeIQ/yiqHEx8feL/hlC2sleGoC7ApRSGlNj9NPBIsdeMFHto0VApTXo6VnxV0GFjbuNdVL/xZmhmsAXxKBHE0Bpow4tQaJ9+8UcUcEFrPH0DV7bgKIYMM3PverWYiB2WzqCroZW2QNR6izgtKOZzzAbHeSTUoHjqaiQysAZmLYDN6GuvNXZdicUgskj/7X0RQ0J6nt1TCB0XQyX294ui8bZx2F X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 199754e2-b797-4682-7f0e-08d941736a2d X-MS-Exchange-CrossTenant-AuthSource: BYAPR12MB2711.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jul 2021 18:17:01.0205 (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: btnfMS+ieb3RxGDHMvTFPMEenVQBlehu8x2WbytMGF5hAUcsuJMBvQGnkY735uu8Q7be+uyDQD5jQp/ZESusvw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR12MB3527 X-Rspam-User: nil Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=Eci1ks7z; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf21.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.45 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 293A7D00285B X-Stat-Signature: a6fdczwdhebq6rpazuaa8gjw76i6dftd X-HE-Tag: 1625681824-719825 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Version 2 of GHCB specification defines NAE to get the extended guest request. It is similar to the SNP_GET_REPORT ioctl. The main difference is related to the additional data that be returned. The additional data returned is a certificate blob that can be used by the SNP guest user. The certificate blob layout is defined in the GHCB specification. The driver simply treats the blob as a opaque data and copies it to userspace. Signed-off-by: Brijesh Singh --- Documentation/virt/coco/sevguest.rst | 22 +++++ drivers/virt/coco/sevguest/sevguest.c | 126 ++++++++++++++++++++++++++ include/uapi/linux/sev-guest.h | 13 +++ 3 files changed, 161 insertions(+) diff --git a/Documentation/virt/coco/sevguest.rst b/Documentation/virt/coco/sevguest.rst index 25446670d816..7acb8696fca4 100644 --- a/Documentation/virt/coco/sevguest.rst +++ b/Documentation/virt/coco/sevguest.rst @@ -85,3 +85,25 @@ on the various fileds passed in the key derivation request. On success, the snp_derived_key_resp.data will contains the derived key value. + +2.2 SNP_GET_EXT_REPORT +---------------------- +:Technology: sev-snp +:Type: guest ioctl +:Parameters (in/out): struct snp_ext_report_req +:Returns (out): struct snp_report_resp on success, -negative on error + +The SNP_GET_EXT_REPORT ioctl is similar to the SNP_GET_REPORT. The difference is +related to the additional certificate data that is returned with the report. +The certificate data returned is being provided by the hypervisor through the +SNP_SET_EXT_CONFIG. + +The ioctl uses the SNP_GUEST_REQUEST (MSG_REPORT_REQ) command provided by the SEV-SNP +firmware to get the attestation report. + +On success, the snp_ext_report_resp.data will contains the attestation report +and snp_ext_report_req.certs_address will contains the certificate blob. If the +length of the blob is lesser than expected then snp_ext_report_req.certs_len will +be updated with the expected value. + +See GHCB specification for further detail on how to parse the certificate blob. diff --git a/drivers/virt/coco/sevguest/sevguest.c b/drivers/virt/coco/sevguest/sevguest.c index 2f20db80490a..d39667a62002 100644 --- a/drivers/virt/coco/sevguest/sevguest.c +++ b/drivers/virt/coco/sevguest/sevguest.c @@ -39,6 +39,7 @@ struct snp_guest_dev { struct device *dev; struct miscdevice misc; + void *certs_data; struct snp_guest_crypto *crypto; struct snp_guest_msg *request, *response; }; @@ -348,6 +349,117 @@ static int get_derived_key(struct snp_guest_dev *snp_dev, struct snp_user_guest_ return rc; } +static int get_ext_report(struct snp_guest_dev *snp_dev, struct snp_user_guest_request *arg) +{ + struct snp_guest_crypto *crypto = snp_dev->crypto; + struct snp_guest_request_data input = {}; + struct snp_ext_report_req req; + int ret, npages = 0, resp_len; + struct snp_report_resp *resp; + struct snp_report_req *rreq; + unsigned long fw_err = 0; + + if (!arg->req_data || !arg->resp_data) + return -EINVAL; + + /* Copy the request payload from the userspace */ + if (copy_from_user(&req, (void __user *)arg->req_data, sizeof(req))) + return -EFAULT; + + rreq = &req.data; + + /* Message version must be non-zero */ + if (!rreq->msg_version) + return -EINVAL; + + if (req.certs_len) { + if ((req.certs_len > SEV_FW_BLOB_MAX_SIZE) || + !IS_ALIGNED(req.certs_len, PAGE_SIZE)) + return -EINVAL; + } + + if (req.certs_address && req.certs_len) { + if (!access_ok(req.certs_address, req.certs_len)) + return -EFAULT; + + /* + * Initialize the intermediate buffer with all zero's. This buffer + * is used in the guest request message to get the certs blob from + * the host. If host does not supply any certs in it, then we copy + * zeros to indicate that certificate data was not provided. + */ + memset(snp_dev->certs_data, 0, req.certs_len); + + input.data_gpa = __pa(snp_dev->certs_data); + npages = req.certs_len >> PAGE_SHIFT; + } + + /* + * The intermediate response buffer is used while decrypting the + * response payload. Make sure that it has enough space to cover the + * authtag. + */ + resp_len = sizeof(resp->data) + crypto->a_len; + resp = kzalloc(resp_len, GFP_KERNEL_ACCOUNT); + if (!resp) + return -ENOMEM; + + if (copy_from_user(resp, (void __user *)arg->resp_data, sizeof(*resp))) { + ret = -EFAULT; + goto e_free; + } + + /* Encrypt the userspace provided payload */ + ret = enc_payload(snp_dev, rreq->msg_version, SNP_MSG_REPORT_REQ, + &rreq->user_data, sizeof(rreq->user_data)); + if (ret) + goto e_free; + + /* Call firmware to process the request */ + input.req_gpa = __pa(snp_dev->request); + input.resp_gpa = __pa(snp_dev->response); + input.data_npages = npages; + memset(snp_dev->response, 0, sizeof(*snp_dev->response)); + ret = snp_issue_guest_request(EXT_GUEST_REQUEST, &input, &fw_err); + + /* Popogate any firmware error to the userspace */ + arg->fw_err = fw_err; + + /* If certs length is invalid then copy the returned length */ + if (arg->fw_err == SNP_GUEST_REQ_INVALID_LEN) { + req.certs_len = input.data_npages << PAGE_SHIFT; + + if (copy_to_user((void __user *)arg->req_data, &req, sizeof(req))) + ret = -EFAULT; + + goto e_free; + } + + if (ret) + goto e_free; + + /* Decrypt the response payload */ + ret = verify_and_dec_payload(snp_dev, resp->data, resp_len); + if (ret) + goto e_free; + + /* Copy the certificate data blob to userspace */ + if (req.certs_address && + copy_to_user((void __user *)req.certs_address, snp_dev->certs_data, + req.certs_len)) { + ret = -EFAULT; + goto e_free; + } + + /* Copy the response payload to userspace */ + if (copy_to_user((void __user *)arg->resp_data, resp, sizeof(*resp))) + ret = -EFAULT; + +e_free: + kfree(resp); + return ret; +} + static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long arg) { struct snp_guest_dev *snp_dev = to_snp_dev(file); @@ -369,6 +481,10 @@ static long snp_guest_ioctl(struct file *file, unsigned int ioctl, unsigned long ret = get_derived_key(snp_dev, &input); break; } + case SNP_GET_EXT_REPORT: { + ret = get_ext_report(snp_dev, &input); + break; + } default: break; } @@ -454,6 +570,12 @@ static int __init snp_guest_probe(struct platform_device *pdev) goto e_free_req; } + snp_dev->certs_data = alloc_shared_pages(SEV_FW_BLOB_MAX_SIZE); + if (IS_ERR(snp_dev->certs_data)) { + ret = PTR_ERR(snp_dev->certs_data); + goto e_free_resp; + } + misc = &snp_dev->misc; misc->minor = MISC_DYNAMIC_MINOR; misc->name = DEVICE_NAME; @@ -461,6 +583,9 @@ static int __init snp_guest_probe(struct platform_device *pdev) return misc_register(misc); +e_free_resp: + free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); + e_free_req: free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); @@ -476,6 +601,7 @@ static int __exit snp_guest_remove(struct platform_device *pdev) free_shared_pages(snp_dev->request, sizeof(struct snp_guest_msg)); free_shared_pages(snp_dev->response, sizeof(struct snp_guest_msg)); + free_shared_pages(snp_dev->certs_data, SEV_FW_BLOB_MAX_SIZE); deinit_crypto(snp_dev->crypto); misc_deregister(&snp_dev->misc); diff --git a/include/uapi/linux/sev-guest.h b/include/uapi/linux/sev-guest.h index 621a9167df7a..23659215fcfb 100644 --- a/include/uapi/linux/sev-guest.h +++ b/include/uapi/linux/sev-guest.h @@ -57,6 +57,16 @@ struct snp_derived_key_resp { __u8 data[64]; }; +struct snp_ext_report_req { + struct snp_report_req data; + + /* where to copy the certificate blob */ + __u64 certs_address; + + /* length of the certificate blob */ + __u32 certs_len; +}; + #define SNP_GUEST_REQ_IOC_TYPE 'S' /* Get SNP attestation report */ @@ -65,4 +75,7 @@ struct snp_derived_key_resp { /* Get a derived key from the root */ #define SNP_GET_DERIVED_KEY _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x1, struct snp_user_guest_request) +/* Get SNP extended report as defined in the GHCB specification version 2. */ +#define SNP_GET_EXT_REPORT _IOWR(SNP_GUEST_REQ_IOC_TYPE, 0x2, struct snp_user_guest_request) + #endif /* __UAPI_LINUX_SEV_GUEST_H_ */