From patchwork Fri Aug 20 15:19:29 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brijesh Singh X-Patchwork-Id: 12449439 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4E663C432BE for ; Fri, 20 Aug 2021 15:22:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id DE6EF61214 for ; Fri, 20 Aug 2021 15:22:23 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org DE6EF61214 Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=amd.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=kvack.org Received: by kanga.kvack.org (Postfix) id 38AB98D0014; Fri, 20 Aug 2021 11:22:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 36A4E8D0012; Fri, 20 Aug 2021 11:22:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1DABD8D0014; Fri, 20 Aug 2021 11:22:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0063.hostedemail.com [216.40.44.63]) by kanga.kvack.org (Postfix) with ESMTP id F1DC18D0012 for ; Fri, 20 Aug 2021 11:22:07 -0400 (EDT) Received: from smtpin01.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B0B422C697 for ; Fri, 20 Aug 2021 15:22:07 +0000 (UTC) X-FDA: 78495824694.01.2C138A6 Received: from NAM12-DM6-obe.outbound.protection.outlook.com (mail-dm6nam12on2043.outbound.protection.outlook.com [40.107.243.43]) by imf13.hostedemail.com (Postfix) with ESMTP id 42025101D0CF for ; Fri, 20 Aug 2021 15:22:07 +0000 (UTC) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=lPuBty+QUf/opknLzZpqEyr3zpP1KkDKZ9wRBGOGy/0Sk0z9fDfV6ADoeYNarVVEpM0CaW1ScHd4xdNBSXBPTiyixsXHQFOqavtrtKmMRfCAV7whnY7vxcSPgAQ2qMeM3t+6txOEv71cVGuBAcPCsWz+dGaJ6CLoDEDU4NuBK1qJuY3uJv48zkX9uAYuEhNvEEt86n1I0Bz2PjvknHWGlgt3KFRuqrk1DIdCrLmYMagfVvZtFV3nWcQB+1kPKDTRekrBo4T6MyPCUlCpnTpVbUTzDVi3S1IeNv+tx0PrW/SI66jVRgLRuuH8LkQzs9f5E2TSvHWtykCyYQ/SvxHTxg== 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=H/+bHTo94EVVINGI1AA9OmGjpus5VFiLwPvPo3mcMXQ=; b=NfRz/jkBI6YEMEcjUzg45MLaNW7/BP685MsCri7YZJwYYHhyFLueP1Rpgxa7b/QvMqzBQHbyQnxcBfA6H9aE7CHCRYlCQjlLvXzYNsFxMefBPnuYHqWDV3H6zNWB6wMjry/OQHgLeNx72LFGF/M7Xfaenz67eGs/5zGCL1N0RTVZoz3QcaCPAx5IOWjKdv8Iy/9BvihbC71l0c0IqKQ2v+4nnFffHw/LPBiUF45u3WctLwx7PAFCGdDt/pLcY2E9G5Ctj+eSRhjPuYyaItMYFme/9NRP/aBOIUZHxxsWSWY8qPuQ2NiDfxcQ+Q+OJSPu3pRMrZ3GhECsRTcnPUSH2g== 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=H/+bHTo94EVVINGI1AA9OmGjpus5VFiLwPvPo3mcMXQ=; b=nGauTfhDo2gdmyPtyQzu5+P/W+Jo8gj0s1LmC94IaQCxJ3s9KQtFQ2PkDTESlKSM0rlUcIIAFCOXiB4Njn1WQjBEeS3dN/+F3XMQhKleWLMkpaAL3i4t6Qa90aHrw3vbcq780H2hH6QX50oGde6qpJYcjlpN3fFdbdaMRt81eao= Received: from SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) by SN6PR12MB2637.namprd12.prod.outlook.com (2603:10b6:805:6b::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4415.21; Fri, 20 Aug 2021 15:22:00 +0000 Received: from SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3]) by SN6PR12MB2718.namprd12.prod.outlook.com ([fe80::78b7:7336:d363:9be3%6]) with mapi id 15.20.4436.019; Fri, 20 Aug 2021 15:22: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 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 , "Kirill A . Shutemov" , Andi Kleen , tony.luck@intel.com, marcorr@google.com, sathyanarayanan.kuppuswamy@linux.intel.com, Brijesh Singh Subject: [PATCH Part1 v5 34/38] x86/sev: Add snp_msg_seqno() helper Date: Fri, 20 Aug 2021 10:19:29 -0500 Message-Id: <20210820151933.22401-35-brijesh.singh@amd.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210820151933.22401-1-brijesh.singh@amd.com> References: <20210820151933.22401-1-brijesh.singh@amd.com> X-ClientProxiedBy: SA9P223CA0002.NAMP223.PROD.OUTLOOK.COM (2603:10b6:806:26::7) To SN6PR12MB2718.namprd12.prod.outlook.com (2603:10b6:805:6f::22) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from sbrijesh-desktop.amd.com (165.204.77.1) by SA9P223CA0002.NAMP223.PROD.OUTLOOK.COM (2603:10b6:806:26::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4436.19 via Frontend Transport; Fri, 20 Aug 2021 15:21:30 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 84a9fb13-c8b9-4cea-8b0f-08d963ee3088 X-MS-TrafficTypeDiagnostic: SN6PR12MB2637: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: TycLKnsnCjYjK31Z873lPi1LDMvTkrfsbSq25mQjVeYJ/hv/hkaIcBdZgYMtm38A5OLvL9pJls6Rj1WFjhV1HqYS1h5ynmfvJuf4f+3UZ/MBTiRkKoeuJHi1CRnfu1I+77Clymn1t73Ns0IYOkl/ljlCr7BQxuUgQZ8A/J84dE3ANRoSx96n2xJ88sXh+fRGwsTYCV8w8QkWgnC1iT5sfsGryVm7dhx3qzsZAlobRY0cYpeIHFoOLQo4pvLmsNLZsyDONx7avo+GEVYRgDjgKLJQ1Blm5Sgp2NxYESQctZLjg4ntTkDWxWpkBoI0EfdXC0zeNEdhQnr7VYQxe7pqw1rl+dHWGMwRDwrYLvrUkOBMrOZwCk6Vz6p096EPdF0iH3VrM6HQnj0dFYlBBC6FF0Vywlhs0qd7hIDQGwxRQ1yi2VCYNajOkO7pxufGO5x0oVAqxMbNQRmV4Ecme9tlY0rhB3TnLzrQOPCMtfykrdjxpl3LookBuCrDQFmUTrMNszih/PtapaflVpn2nm6VVA+ezSaNpaHuB5KllA7VELPiAVVTyVIDr/AOK/N8zIY7lKcLrFFtkP6h/8TjIzyIKYc55nD0uVgjgcXGXR+WUfj31zrbAcejzuZp+tkkNTJnJj466WbUJMkgrukknvwPvo0hj3F+1gG7LCCvSa7d9CMxoUBesRH+djqM413WhETyISrn/GxTKW5zdR/4xULHBQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SN6PR12MB2718.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(366004)(396003)(136003)(346002)(376002)(39860400002)(36756003)(956004)(8936002)(2906002)(4326008)(52116002)(8676002)(83380400001)(2616005)(26005)(86362001)(7416002)(7406005)(186003)(66476007)(44832011)(38100700002)(54906003)(5660300002)(7696005)(38350700002)(1076003)(316002)(6486002)(66946007)(478600001)(66556008);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: QapVGBRQ9/8Kjy5pN2tNvrAE27q4BVH6/w/SDooAT4kkmNOMSrOAlN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bW9UZFM8GNKaTf25rml3lFCtupezdmZwIBMHtr5dPKRYKx3/9ssGyx3ZLb7cdW7Eq2+JOuLVPNc7ZWbdj30G2mqDw6fB6QIgTndCTfAeBgEfpZPEjk1CYdunsDUVpcQwJvsUSyiZPT9WHLgVlFgDRvCRojretRpDUTEPx5+DVBtZs7ybsTN7JQpHxbhOky4GqhRUJlqEAwvTsep6g15AQTbOs X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-Network-Message-Id: 84a9fb13-c8b9-4cea-8b0f-08d963ee3088 X-MS-Exchange-CrossTenant-AuthSource: SN6PR12MB2718.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Aug 2021 15:21:31.8856 (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: uHeRh0fpplqu0oiDL/wnPDaNA3qClCfrOuyyrjh0cAarQF3jtJBk9Fx1rCYj81phl0L0JOlT3lEc9cuFryg+/g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR12MB2637 Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=amd.com header.s=selector1 header.b=nGauTfhD; dmarc=pass (policy=quarantine) header.from=amd.com; spf=pass (imf13.hostedemail.com: domain of brijesh.singh@amd.com designates 40.107.243.43 as permitted sender) smtp.mailfrom=brijesh.singh@amd.com X-Stat-Signature: pk9kdh1j193x4sb4kbm8ooay544zqkoj X-Rspamd-Queue-Id: 42025101D0CF X-Rspamd-Server: rspam05 X-HE-Tag: 1629472927-297156 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 the message sequence counter used in the request message header, and it will be automatically incremented after the 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 319a40fc57ce..f42cd5a8e7bb 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; @@ -2030,6 +2032,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) + 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) + 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; @@ -2077,6 +2153,9 @@ int snp_issue_guest_request(int type, struct snp_guest_request_data *input, unsi ret = -EIO; } + /* The command was successful, increment the sequence counter */ + snp_gen_msg_seqno(); + e_put: __sev_put_ghcb(&state); e_restore_irq: diff --git a/include/linux/sev-guest.h b/include/linux/sev-guest.h index 24dd17507789..16b6af24fda7 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 VMPCK. + * + * 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 for secrets page format */ +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__ */