From patchwork Tue May 17 15:31:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 12852681 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 7AAECC433EF for ; Tue, 17 May 2022 15:32:03 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.331093.554559 (Exim 4.92) (envelope-from ) id 1nqzAp-0005go-5P; Tue, 17 May 2022 15:31:47 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 331093.554559; Tue, 17 May 2022 15:31:47 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nqzAp-0005gh-0z; Tue, 17 May 2022 15:31:47 +0000 Received: by outflank-mailman (input) for mailman id 331093; Tue, 17 May 2022 15:31:45 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nqzAn-0005fb-AO for xen-devel@lists.xenproject.org; Tue, 17 May 2022 15:31:45 +0000 Received: from esa2.hc3370-68.iphmx.com (esa2.hc3370-68.iphmx.com [216.71.145.153]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 73b8a127-d5f6-11ec-bd2c-47488cf2e6aa; Tue, 17 May 2022 17:31:43 +0200 (CEST) Received: from mail-dm6nam08lp2042.outbound.protection.outlook.com (HELO NAM04-DM6-obe.outbound.protection.outlook.com) ([104.47.73.42]) by ob1.hc3370-68.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 17 May 2022 11:31:40 -0400 Received: from DS7PR03MB5608.namprd03.prod.outlook.com (2603:10b6:5:2c9::18) by BYAPR03MB4662.namprd03.prod.outlook.com (2603:10b6:a03:138::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Tue, 17 May 2022 15:31:37 +0000 Received: from DS7PR03MB5608.namprd03.prod.outlook.com ([fe80::5df3:95ce:4dfd:134e]) by DS7PR03MB5608.namprd03.prod.outlook.com ([fe80::5df3:95ce:4dfd:134e%5]) with mapi id 15.20.5273.014; Tue, 17 May 2022 15:31:37 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 73b8a127-d5f6-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1652801503; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=E0WrwYD300/uBgj9TRjUZK4gWeZhaQHIkA6kYkkRO7U=; b=ONNpPHEh+F+1VHnMXr6wHMMkxhyvCMIABXRbeB8ZWmSUe0vcbnUQXzGA 5+Y4YeUXwqCYELZazhEM4vcu2Tyxib/7tDeRq6+qzXl41C/c7/rstbS62 YqJGDnV9lOyS+DSxttcf6VIq8Z7+DSN7exrwjR/fOKFYcbr6clSC2VZ6c o=; X-IronPort-RemoteIP: 104.47.73.42 X-IronPort-MID: 71519856 X-IronPort-Reputation: None X-IronPort-Listener: OutboundMail X-IronPort-SenderGroup: RELAY_O365 X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:0ucnKK74BMT2PetWZHoDAAxRtEzGchMFZxGqfqrLsTDasY5as4F+v jNJCmjSa66JN2rwe4t2ao3g8k8O657cyt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1vZjXOIi3rqpsL0ABggDdBauI5fehzFr2I9odBVys twri2knqsSKSmFsDX25tDOWR0vvFGzu2AenekaiGEaeZcCaYVWsZcU8CpuYds99RrBmcEa+d RVfYHhDK48SyLYU5mZhBgj/DWUZAV8Iv/cKXJy+PB80FBt7QVEJgUjtYkid0w7heMAoql/lp r525tT5cFzp7ctHMRA7cc6MLyK4z/2MGTx2Fz7GyWVKIg3f1TwlrXQ3JIZoMmXRb1g9upBpH 2GaiITiVIP X-IronPort-AV: E=Sophos;i="5.91,233,1647316800"; d="scan'208";a="71519856" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=I7HUTINmUzRI39zzwUwcOCfy52++HaaJ+r17RQytoc78NkmlHosH8miHH2RjyKXizDUqTJgVUxYkl9OUn2dvQtA1c+pEzgvqZu3OU1gPWMM0v77fBbhO9AV3FQhr+olDExd3QYQKRue2bo02utswZCgaZal+uTw06bQ6QynTFp/gCv3sneB6zfXTOrNxTdmz6mfgfApTKs+097b2tEebEzx5eJuwlrBqReZ9UxJS3Pq9dpPMuEZXdOdSm4SgptiHxgqu4Pr6HvrUfydBkVhYTrnKEtQvW8UjbOMA5kuJNdb/vo05vvDB7ngRb4CirKLM6nYM68xW4ALU8xq88Za2ww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=q1YXIq3wwhUERf7uVRHsHYT1E5p+r61LTwXkwzyRWfU=; b=B+NK8Z8YvBGG6x4hYLVJIq0hyTkO+evUDvOcICcWUZzeldzv0hvWKPiJhHe2C7NtwxZ+kplkcoICeKRLwHT/TldbRD4wRCx1wZbzkdWETTAQjyoPEY1tr9XcmwBBUTjCjtvE8LWxH/lZGJxVWv7/lFPi7Iivprl3rN39U2RjCbpRSZnJYiegV/yx4bCVdIVO/AV8YomY3I/g/3DzF785s/lPGVN4rzR0GFdIT/7ZngZzKmqJkkYg7EMepsmINHrWA+1GvMzwear2nIOz9zGO5sLR+FEyEobEOfKVLzcXcKJFTqCb35pQi7qFOzXsZvWac+rwfl/LgWS/t/O2uv+aSw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.onmicrosoft.com; s=selector2-citrix-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=q1YXIq3wwhUERf7uVRHsHYT1E5p+r61LTwXkwzyRWfU=; b=dC6VOu4PIg1vzdLAcegH7nWdDQWNzrnmqAab3EgN5OjdT3LyGzWfq00xjTLHMdUDVuzU6Npb8uLGTKgnHFagn4i8TE9liDKBAlvDcMfwtBmVNNh826EayyU6ls5N3Es9t0TN1N/JCWhuTbLIundWFTfhCDyVxjLxajjq71EBB3M= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com; From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper , Wei Liu Subject: [PATCH v6 1/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests on top of SPEC_CTRL Date: Tue, 17 May 2022 17:31:25 +0200 Message-Id: <20220517153127.40276-2-roger.pau@citrix.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220517153127.40276-1-roger.pau@citrix.com> References: <20220517153127.40276-1-roger.pau@citrix.com> X-ClientProxiedBy: LO2P123CA0077.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:138::10) To DS7PR03MB5608.namprd03.prod.outlook.com (2603:10b6:5:2c9::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 31f322d8-2972-4ce7-f783-08da381a5535 X-MS-TrafficTypeDiagnostic: BYAPR03MB4662:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cxrbkII93kiVd3/tnbHXGscxtmm2PpmB0GFSeGVVxyUAnnMLjdDFUQz40TrmjDfCoUCCdiT0et9lzpcnAJwg7nbE79msKyoNKn+wHDwWu/X3aWzQfWK/jHT0yBiKdi7DeC68saB1UTAqHti8p0SUvsMK6nNBziG3e+5ThvSN3OApuuBBP+mtKbhfHHmyQO+WNETJ28D3z1zvrn+25jH1LKN5oeE+u+xpbfceFUnYpnhijCrnhzuvv5BHXsveSdkOoAEo2fgFPppUOET/UqlslPiPVwg3HzUSlBBH6uF1Qsg3fswrz2QcbNlCXCtefv7T/WYLIIv8Mizb2DBF+fpVawmjU4GvHNk94Jc7Wg9qFHc6qFUjd4NoEhT80EkTnIk45ycW1ggA1uLG68bxYxpcrp95CciEuHLCBO6vAfjxqG5wrhJ8PhJKQezwl00F5bZPAzmLHEi2vMc5MncB/1naxtMiQaFTPkWhiunuzln4N9WSqVJ5vkrBDdBkhnFK0Gsc+UgmpF4R3LotMkf2B/4z9GOKSptN/wO+B6nBOW1pUfh0RZq7zT+WZqve/8pO9Som3YxDuVw16M8KfpADo4taXa8+J35nZA0IxlVND/KaVv9j/Myni2JBZ86du9vY3PN0p+hglxQ6hwj4StYOsYGgtQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR03MB5608.namprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(36756003)(6506007)(186003)(82960400001)(1076003)(508600001)(54906003)(6916009)(6486002)(26005)(6512007)(2906002)(66946007)(66476007)(8676002)(4326008)(66556008)(6666004)(2616005)(5660300002)(8936002)(83380400001)(316002)(86362001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?ftaQsFNdk7ooZSiWhxEB9LDGBY2/?= =?utf-8?q?aMpPz/TbWFDwUZNwhGEKkTY34ac1l2iGEXEEaPOCo0YM2QAdOgqt/r+XPv+3VX2OQ?= =?utf-8?q?olBjT/3PCyIbxbT2CjGpsuxh+yz3c60LZCVelZraDfD33X7WRSJ7j4zefEnymiT2I?= =?utf-8?q?rBveDTw0tSCYvk8YTD33OgkNCkt9sp3dod/X9GaSFj3oI8goCmYPD5vwScdxwI6i8?= =?utf-8?q?fbQ6wsno2yDW2hDzs/91ejK5JbnoAK6piZKBBthWHohiB0c/oMTFvcUPO6ZoaKXeC?= =?utf-8?q?Fnqk8QbV48SMEdW5Y1eaV108OtXnXUhQpLneoANy3ZRMAel/g14Y0Zggx2OiR1Hn5?= =?utf-8?q?zr3le48USEG7CuEr5AX83zhapHf2mtks4fJB4toV/UL/DxY8mBC+DPCNRoKEyZ2/O?= =?utf-8?q?zlTspfE7S4HM7VCXscIJpP53xNM4sg25cAz0TefZW+4izm76MWZyLZX3Kvm3BoCde?= =?utf-8?q?InkZThDPP8BN9IV4UhL/idQkuYVSTmAEZhgUwiVBN8/Ly9PnCJrpBacWcS55sk2IK?= =?utf-8?q?Ecz1vdeFJy8Bd38JtQQjZzYVmpHeEBzshxRoX/xdJD5e/flW0kbQ/sEbKPxjSi3Uj?= =?utf-8?q?sKXCATSwIkpSO9mI51CjopG4ZbGGA6LM7+asivt+tgnDvbqRDIh3awNb8cVwxbsKe?= =?utf-8?q?in2y6WeUehrNHzn5JA0tt2B5vTP5IPOQrLFJ7zQVaVVAOzgxqjJT4XEHkQBbZlhBr?= =?utf-8?q?nV9j/uCDxjvJEfqsYSZWYYd3kVGl6nVFVeXZMxTGdN78stq4V9t2nysP3kejzOEK7?= =?utf-8?q?39/EzlqWfiPvLT8/r1ht9qK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FrYISSGhN3eTlceVnL2XK3Fjb7V75vHBSxsBnm+PP8CU=3D?= X-OriginatorOrg: citrix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 31f322d8-2972-4ce7-f783-08da381a5535 X-MS-Exchange-CrossTenant-AuthSource: DS7PR03MB5608.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 15:31:37.7657 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 335836de-42ef-43a2-b145-348c2ee9ca5b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: WTEborTCrfivrne5mPF34Uj1knyvI/bAjsSP7zMZ0c620xWqRth7ZDoKt30ZRKiytGcd0EWHQP+Jkq+rkRyjNQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR03MB4662 Use the logic to set shadow SPEC_CTRL values in order to implement support for VIRT_SPEC_CTRL (signaled by VIRT_SSBD CPUID flag) for HVM guests. This includes using the spec_ctrl vCPU MSR variable to store the guest set value of VIRT_SPEC_CTRL.SSBD, which will be OR'ed with any SPEC_CTRL values being set by the guest. On hardware having SPEC_CTRL VIRT_SPEC_CTRL will not be offered by default to guests. VIRT_SPEC_CTRL will only be part of the max CPUID policy so it can be enabled for compatibility purposes. Use '!' to annotate the feature in order to express that the presence of the bit is not directly tied to its value in the host policy. Suggested-by: Andrew Cooper Signed-off-by: Roger Pau Monné Reviewed-by: Jan Beulich --- Changes since v3: - Use '!' to annotate the feature. Changes since v2: - Reword reasoning for using '!s'. - Trim comment about only setting SSBD bit in spec_ctrl.raw. Changes since v1: - Only expose VIRT_SSBD if AMD_SSBD is available on the host. - Revert change to msr-sc= command line option documentation. - Only set or clear the SSBD bit of spec_ctrl. --- xen/arch/x86/cpuid.c | 7 +++++++ xen/arch/x86/hvm/hvm.c | 1 + xen/arch/x86/include/asm/msr.h | 4 ++++ xen/arch/x86/msr.c | 18 ++++++++++++++++++ xen/arch/x86/spec_ctrl.c | 3 ++- xen/include/public/arch-x86/cpufeatureset.h | 2 +- 6 files changed, 33 insertions(+), 2 deletions(-) diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c index 7e0b395698..979dcf8164 100644 --- a/xen/arch/x86/cpuid.c +++ b/xen/arch/x86/cpuid.c @@ -550,6 +550,13 @@ static void __init calculate_hvm_max_policy(void) __clear_bit(X86_FEATURE_IBRSB, hvm_featureset); __clear_bit(X86_FEATURE_IBRS, hvm_featureset); } + else if ( boot_cpu_has(X86_FEATURE_AMD_SSBD) ) + /* + * If SPEC_CTRL.SSBD is available VIRT_SPEC_CTRL.SSBD can be exposed + * and implemented using the former. Expose in the max policy only as + * the preference is for guests to use SPEC_CTRL.SSBD if available. + */ + __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset); /* * With VT-x, some features are only supported by Xen if dedicated diff --git a/xen/arch/x86/hvm/hvm.c b/xen/arch/x86/hvm/hvm.c index 5b16fb4cd8..db8f95ef7c 100644 --- a/xen/arch/x86/hvm/hvm.c +++ b/xen/arch/x86/hvm/hvm.c @@ -1334,6 +1334,7 @@ static const uint32_t msrs_to_send[] = { MSR_INTEL_MISC_FEATURES_ENABLES, MSR_IA32_BNDCFGS, MSR_IA32_XSS, + MSR_VIRT_SPEC_CTRL, MSR_AMD64_DR0_ADDRESS_MASK, MSR_AMD64_DR1_ADDRESS_MASK, MSR_AMD64_DR2_ADDRESS_MASK, diff --git a/xen/arch/x86/include/asm/msr.h b/xen/arch/x86/include/asm/msr.h index ce4fe51afe..ab6fbb5051 100644 --- a/xen/arch/x86/include/asm/msr.h +++ b/xen/arch/x86/include/asm/msr.h @@ -291,6 +291,7 @@ struct vcpu_msrs { /* * 0x00000048 - MSR_SPEC_CTRL + * 0xc001011f - MSR_VIRT_SPEC_CTRL (if X86_FEATURE_AMD_SSBD) * * For PV guests, this holds the guest kernel value. It is accessed on * every entry/exit path. @@ -306,6 +307,9 @@ struct vcpu_msrs * We must clear/restore Xen's value before/after VMRUN to avoid unduly * influencing the guest. In order to support "behind the guest's back" * protections, we load this value (commonly 0) before VMRUN. + * + * Once of such "behind the guest's back" usages is setting SPEC_CTRL.SSBD + * if the guest sets VIRT_SPEC_CTRL.SSBD. */ struct { uint32_t raw; diff --git a/xen/arch/x86/msr.c b/xen/arch/x86/msr.c index 01a15857b7..72c175fd8b 100644 --- a/xen/arch/x86/msr.c +++ b/xen/arch/x86/msr.c @@ -381,6 +381,13 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) ? K8_HWCR_TSC_FREQ_SEL : 0; break; + case MSR_VIRT_SPEC_CTRL: + if ( !cp->extd.virt_ssbd ) + goto gp_fault; + + *val = msrs->spec_ctrl.raw & SPEC_CTRL_SSBD; + break; + case MSR_AMD64_DE_CFG: if ( !(cp->x86_vendor & (X86_VENDOR_AMD | X86_VENDOR_HYGON)) ) goto gp_fault; @@ -666,6 +673,17 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) wrmsr_tsc_aux(val); break; + case MSR_VIRT_SPEC_CTRL: + if ( !cp->extd.virt_ssbd ) + goto gp_fault; + + /* Only supports SSBD bit, the rest are ignored. */ + if ( val & SPEC_CTRL_SSBD ) + msrs->spec_ctrl.raw |= SPEC_CTRL_SSBD; + else + msrs->spec_ctrl.raw &= ~SPEC_CTRL_SSBD; + break; + case MSR_AMD64_DE_CFG: /* * OpenBSD 6.7 will panic if writing to DE_CFG triggers a #GP: diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 1408e4c7ab..f338bfe292 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -402,12 +402,13 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps) * mitigation support for guests. */ #ifdef CONFIG_HVM - printk(" Support for HVM VMs:%s%s%s%s%s\n", + printk(" Support for HVM VMs:%s%s%s%s%s%s\n", (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) || boot_cpu_has(X86_FEATURE_SC_RSB_HVM) || boot_cpu_has(X86_FEATURE_MD_CLEAR) || opt_eager_fpu) ? "" : " None", boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ? " MSR_SPEC_CTRL" : "", + boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ? " MSR_VIRT_SPEC_CTRL" : "", boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ? " RSB" : "", opt_eager_fpu ? " EAGER_FPU" : "", boot_cpu_has(X86_FEATURE_MD_CLEAR) ? " MD_CLEAR" : ""); diff --git a/xen/include/public/arch-x86/cpufeatureset.h b/xen/include/public/arch-x86/cpufeatureset.h index 9cee4b439e..5aa3c82fc6 100644 --- a/xen/include/public/arch-x86/cpufeatureset.h +++ b/xen/include/public/arch-x86/cpufeatureset.h @@ -265,7 +265,7 @@ XEN_CPUFEATURE(IBRS_SAME_MODE, 8*32+19) /*S IBRS provides same-mode protection XEN_CPUFEATURE(NO_LMSL, 8*32+20) /*S EFER.LMSLE no longer supported. */ XEN_CPUFEATURE(AMD_PPIN, 8*32+23) /* Protected Processor Inventory Number */ XEN_CPUFEATURE(AMD_SSBD, 8*32+24) /*S MSR_SPEC_CTRL.SSBD available */ -XEN_CPUFEATURE(VIRT_SSBD, 8*32+25) /* MSR_VIRT_SPEC_CTRL.SSBD */ +XEN_CPUFEATURE(VIRT_SSBD, 8*32+25) /*! MSR_VIRT_SPEC_CTRL.SSBD */ XEN_CPUFEATURE(SSB_NO, 8*32+26) /*A Hardware not vulnerable to SSB */ XEN_CPUFEATURE(PSFD, 8*32+28) /*S MSR_SPEC_CTRL.PSFD */ From patchwork Tue May 17 15:31:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 12852682 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 536E7C433F5 for ; Tue, 17 May 2022 15:32:03 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.331094.554570 (Exim 4.92) (envelope-from ) id 1nqzAr-0005yI-Er; Tue, 17 May 2022 15:31:49 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 331094.554570; Tue, 17 May 2022 15:31:49 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nqzAr-0005yB-B7; Tue, 17 May 2022 15:31:49 +0000 Received: by outflank-mailman (input) for mailman id 331094; Tue, 17 May 2022 15:31:47 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nqzAp-0005fb-K5 for xen-devel@lists.xenproject.org; Tue, 17 May 2022 15:31:47 +0000 Received: from esa2.hc3370-68.iphmx.com (esa2.hc3370-68.iphmx.com [216.71.145.153]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 75c812ec-d5f6-11ec-bd2c-47488cf2e6aa; Tue, 17 May 2022 17:31:45 +0200 (CEST) Received: from mail-dm6nam08lp2041.outbound.protection.outlook.com (HELO NAM04-DM6-obe.outbound.protection.outlook.com) ([104.47.73.41]) by ob1.hc3370-68.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 17 May 2022 11:31:44 -0400 Received: from DS7PR03MB5608.namprd03.prod.outlook.com (2603:10b6:5:2c9::18) by BYAPR03MB4662.namprd03.prod.outlook.com (2603:10b6:a03:138::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Tue, 17 May 2022 15:31:42 +0000 Received: from DS7PR03MB5608.namprd03.prod.outlook.com ([fe80::5df3:95ce:4dfd:134e]) by DS7PR03MB5608.namprd03.prod.outlook.com ([fe80::5df3:95ce:4dfd:134e%5]) with mapi id 15.20.5273.014; Tue, 17 May 2022 15:31:42 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 75c812ec-d5f6-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1652801505; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=/Pb4PtMbn80iE/cJjzQcmJPSFXHeH+Bckcrs+eHVMPg=; b=MLi1E5fjmFjZC3WWYVs0TY89eLM29YFYxcEzsBuVnCjYdxYt38Wtnl0D lqXucVzEYuvOsImIJs7gfqTdaptQplu5Bd7u7I2MNsPrUvyrz3So92MAC TuYPZqzgDQpg5xfORT+LD3Adlrooza6LwCpnhbCqUEpJEJrhv4XNCKb85 8=; X-IronPort-RemoteIP: 104.47.73.41 X-IronPort-MID: 71519888 X-IronPort-Reputation: None X-IronPort-Listener: OutboundMail X-IronPort-SenderGroup: RELAY_O365 X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:eAaCYq7/m/PZdAMnZ3VTugxRtE3GchMFZxGqfqrLsTDasY5as4F+v mccX2iHPvqKNjP2KdBxOYq+8hgPsMTXyYBnSgJv+y88Hi5G8cbLO4+Ufxz6V8+wwmwvb67FA +E2MISowBUcFyeEzvuVGuG96yE6j8lkf5KkYAL+EnkZqTRMFWFw0HqPp8Zj2tQy2YXgXFvX0 T/Pi5a31GGNimYc3l08s8pvmDs31BglkGpF1rCWTakjU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c+CUlpAZwyHsMceg72w 36CNUZqFg3dL5xUUtcPpZ3fSLlMB26ffrzWFjiUmjPJeUgB0/njaLRzfEc2NyKEaZ4vqfa3q 6xGm9liQ== X-IronPort-AV: E=Sophos;i="5.91,233,1647316800"; d="scan'208";a="71519888" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=R7/ZcZR3ZZg02XHwTDOJVLVufYN84HYhWhZb7wIbHKec9ZwlWfURDl0YLujOoPdDdUMfR4kGSVAifR+st3QIXTx9KuO7V6krDtuPDLbcwOSrZiUcoZAZxWaagAHk4pGWHZdRnCBZxNQLeJhG6BC8o2LNMeolXasD+QaEwy/vWCN10xc2uRTUXZ/iLpSGCXwPp8Rb3vJs+AB38h3mSnRp2gngQtREIN7CCiGdw5fuHGZIQC43glmGER07beatM3buaHQIDHuH7rK0OMF1g3+JFjKst/Lk/KOCkaVATTaqTMKnH/VjbKk4xY9CgIXfh6ZhZ+BpJKNJUusr7O9ZU+9s5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=XxGtLGB+qBMQmowNUxyabNIKT+0uN2seU/MaxvqgRh0=; b=imoxos4EN7rOUJb9BiUkwIkXdZUTRAWTXPWHztwGRzWOV3fDnSJ7guPTk9Qv2TX3c78Z7mbBB3gv/HkGebB4ok6m/E6A2d+6WhQqk30HIcibRONKUid51zT8LVP3qthMIy+42rSoYV86vcJCs9dS0OeSem1lYcizhfyR3RZcf2oTrwaeLjuBFMG9e0YCQXmY1RuRHYkpCaxQMNCh3N1qslYZEGV2FOfsann3rmxNTIUBKfnNfzRrV95cgl6pSemvlK9PgOaHUecel2ODO38x+wpTxVbzsmWnS4PfZY+3wnNuQXA0z+s/eA8HpEIwAadvrvhFH4mDaqNaPF5p8ywagQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.onmicrosoft.com; s=selector2-citrix-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=XxGtLGB+qBMQmowNUxyabNIKT+0uN2seU/MaxvqgRh0=; b=MbrnFGfTk7Kvxi53z7tsegmrXGRAITRaesh3R0O7SEdLOPUOUBJ50D41neYTt1HUxoPxXXa89nloRz/KEh5i8DQ0stYuidM2iiA1Pqu84vtdjXzKmPYO0hv6GDC0XCdl+Z8Js3tMbd29fEQbVHgtygPO2Cfklqy1dVyREJZp/EI= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com; From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Jan Beulich , Andrew Cooper , Wei Liu Subject: [PATCH v6 2/3] amd/msr: allow passthrough of VIRT_SPEC_CTRL for HVM guests Date: Tue, 17 May 2022 17:31:26 +0200 Message-Id: <20220517153127.40276-3-roger.pau@citrix.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220517153127.40276-1-roger.pau@citrix.com> References: <20220517153127.40276-1-roger.pau@citrix.com> X-ClientProxiedBy: LO4P123CA0374.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:18e::19) To DS7PR03MB5608.namprd03.prod.outlook.com (2603:10b6:5:2c9::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 148d567b-bcd2-4a17-4eb4-08da381a582f X-MS-TrafficTypeDiagnostic: BYAPR03MB4662:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: UF3XLor2UABqBubKjKbAyhsTKBJe3N+ZvHwPleFbrv034dHicPsMqki7ifKj5ulwczTyQeijvHPiEDey2N11RoJkpQ3IhR3KUw883wj41ZprwINbWo5elVkh4xCVQDxaaWQEuX75jzKYAM7DFw0VJAYq0CInyEOYGNfCPjGdT9HYN/qxirLwJk/EXoALZsTJvRcUCJpnKNugPwb5ECH8zFSVv8M6SFLrf2XrEPoLuSsQKfaTp2MuxuHbRv3c0i5Tl2awv+sWQStzV1AkenN3c1WLerDITlTxFh367LMcaNa9gVVd8bQnD8z6pkhdejAJJvSZaAJSQ6ynDzSS16oCfqoBiXgGUvKWvkxY9CX+jyYG2hP2lJUGyfYmz89NEWHYsUbRdILSjRvp+iwxwuXS6XeVbuzg+2LCwM9EGR3x6JB29cqwuIBqR6kX/0dOGucPRF4sO/0hDQIujunAnTnLzQjCqtGweDHFTBAEEzhSH0EXz9ilfa2ZPHbjNouJU5hHOH/1hQIkypR/mCpeG+zp3vR/EIhQKsnVHe+VtvUV8p8bDvW2EadHrnEQ1t9vn1UZBcv80Q5JaYbV1cwUep9uEeZ40B8gNpnabWTIMzyofKDbzg3SQiFmDmYYJSj2OO7i8EBJmw4sD1aySuUMI+WwJg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR03MB5608.namprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(36756003)(6506007)(186003)(82960400001)(1076003)(508600001)(54906003)(6916009)(6486002)(26005)(6512007)(2906002)(66946007)(66476007)(8676002)(4326008)(66556008)(6666004)(2616005)(5660300002)(8936002)(83380400001)(316002)(86362001)(38100700002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?lkGmkE9p74xoFjpFiDTiaq5TIFhJ?= =?utf-8?q?ncv5b1Sak7ZkS4L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8P5meij9KMhAmQQBzEskL+LbjWKakhB8+qf3cPWFT4eden?= =?utf-8?q?2n3FERBCZOy/hhFuywKwCplvht72rt844PWKRzEIx5FhHF/QthVthXlsjqj5kpuUn?= =?utf-8?q?VvUnip0kJQNNYrf+CuJuq2dMcTV93QWx/rM5ANRtODQ9wKg6i4VFlkQLtfxiMdACY?= =?utf-8?q?/aQYBEtY/aHB9ugqAC9a8v6kl6MwNzMlhATqbCqRrR/T4fUhAHDmS4aR5Sx8MaSi2?= =?utf-8?q?eERDOJPZtAEaHERD22uelRjAsUfx6pM/9ntbOFGpYAp4PUVYKLH2SIOwFpvLFafXA?= =?utf-8?q?GKWQ0FzbNP2DdRP3qjVlsWmzNMbAaAXXY1jSClLFDNis9fgfuHXdlBRB+Rhdn4Ra5?= =?utf-8?q?0suEc4S3Kox76eNpEkWCdpZsUkBw6GLV1oOZu5I8yFi5loGMx3f/DvFS0ilJ69T5d?= =?utf-8?q?7SJ8F7bLySwE92E1tGHtp6d1MNAeg7b2lfmf0yNchaHl3btB0uAAaXyQpBu3DbK0P?= =?utf-8?q?f27zeRvG1RbK27tN39ZLVzbVxALAHjDM1o3KsP6jDwNr5G9V9oPMIfSxJ09qbZEy1?= =?utf-8?q?j197zKK9/feAyPBZ4Zv4xYuoTDYuhm1wZI9A9ttr3MXhYx3RVG9tuWuhCFHtzXBxu?= =?utf-8?q?Ov32vZ5YrYGIHoEqazwq0OfhVegSwA/KAaFk2HyebE+KdaS/ZcKQJoA7kdR33w2ey?= =?utf-8?q?SEpq31zjaSVVVbMRTlfNTP1R7/Gzw8XZqcsqOJ3BaGRfPg9NfjldVCKq31crZfisZ?= =?utf-8?q?/zDRGgaV/0KK9axTO1oiplLeIcawVYekfymBcmhgfeJVvOVgRfG84=3D?= X-OriginatorOrg: citrix.com X-MS-Exchange-CrossTenant-Network-Message-Id: 148d567b-bcd2-4a17-4eb4-08da381a582f X-MS-Exchange-CrossTenant-AuthSource: DS7PR03MB5608.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 15:31:42.7865 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 335836de-42ef-43a2-b145-348c2ee9ca5b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: JsK8HTOPgYuuLkwSnSdGvvmYlDCmhgDJrVkF+3RfwTKqmZQONgH7AzrhpPq+ymBogEPzYCTahhz5Tks/7oGqkg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR03MB4662 Allow HVM guests access to MSR_VIRT_SPEC_CTRL if the platform Xen is running on has support for it. This requires adding logic in the vm{entry,exit} paths for SVM in order to context switch between the hypervisor value and the guest one. The added handlers for context switch will also be used for the legacy SSBD support. Introduce a new synthetic feature leaf (X86_FEATURE_VIRT_SC_MSR_HVM) to signal whether VIRT_SPEC_CTRL needs to be handled on guest vm{entry,exit}. Suggested-by: Andrew Cooper Signed-off-by: Roger Pau Monné Reviewed-by: Jan Beulich --- Changes since v5: - Add comment regarding VIRT_SSBD exposure. - Store guest value in 'val' local variable in vmentry_virt_spec_ctrl. Changes since v4: - Fix exposing in the max policy. Changes since v3: - Always trap write accesses to VIRT_SPEC_CTRL in order to cache the guest setting. - Do not use the 'S' annotation for the VIRT_SSBD feature. Changes since v2: - Reword part of the commit message regarding annotation change. - Fix MSR intercept. - Add handling of VIRT_SPEC_CTRL to guest_{rd,wr}msr when using VIRT_SSBD also. Changes since v1: - Introduce virt_spec_ctrl vCPU field. - Context switch VIRT_SPEC_CTRL on vmentry/vmexit separately from SPEC_CTRL. --- xen/arch/x86/cpuid.c | 14 +++++++++++ xen/arch/x86/hvm/svm/entry.S | 8 ++++++ xen/arch/x86/hvm/svm/svm.c | 35 ++++++++++++++++++++++++++ xen/arch/x86/include/asm/cpufeatures.h | 1 + xen/arch/x86/include/asm/msr.h | 10 ++++++++ xen/arch/x86/msr.c | 16 +++++++++--- xen/arch/x86/spec_ctrl.c | 9 ++++++- 7 files changed, 88 insertions(+), 5 deletions(-) diff --git a/xen/arch/x86/cpuid.c b/xen/arch/x86/cpuid.c index 979dcf8164..a4a366ad84 100644 --- a/xen/arch/x86/cpuid.c +++ b/xen/arch/x86/cpuid.c @@ -541,6 +541,13 @@ static void __init calculate_hvm_max_policy(void) raw_cpuid_policy.basic.sep ) __set_bit(X86_FEATURE_SEP, hvm_featureset); + /* + * VIRT_SSBD is exposed in the default policy as a result of + * VIRT_SC_MSR_HVM being set, it also needs exposing in the max policy. + */ + if ( boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) ) + __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset); + /* * If Xen isn't virtualising MSR_SPEC_CTRL for HVM guests (functional * availability, or admin choice), hide the feature. @@ -597,6 +604,13 @@ static void __init calculate_hvm_def_policy(void) guest_common_feature_adjustments(hvm_featureset); guest_common_default_feature_adjustments(hvm_featureset); + /* + * Only expose VIRT_SSBD if AMD_SSBD is not available, and thus + * VIRT_SC_MSR_HVM is set. + */ + if ( boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) ) + __set_bit(X86_FEATURE_VIRT_SSBD, hvm_featureset); + sanitise_featureset(hvm_featureset); cpuid_featureset_to_policy(hvm_featureset, p); recalculate_xstate(p); diff --git a/xen/arch/x86/hvm/svm/entry.S b/xen/arch/x86/hvm/svm/entry.S index 4ae55a2ef6..2f63a2e3c6 100644 --- a/xen/arch/x86/hvm/svm/entry.S +++ b/xen/arch/x86/hvm/svm/entry.S @@ -19,6 +19,8 @@ .file "svm/entry.S" +#include + #include #include @@ -57,6 +59,9 @@ __UNLIKELY_END(nsvm_hap) clgi + ALTERNATIVE "", STR(call vmentry_virt_spec_ctrl), \ + X86_FEATURE_VIRT_SC_MSR_HVM + /* WARNING! `ret`, `call *`, `jmp *` not safe beyond this point. */ /* SPEC_CTRL_EXIT_TO_SVM Req: b=curr %rsp=regs/cpuinfo, Clob: acd */ .macro svm_vmentry_spec_ctrl @@ -114,6 +119,9 @@ __UNLIKELY_END(nsvm_hap) ALTERNATIVE "", svm_vmexit_spec_ctrl, X86_FEATURE_SC_MSR_HVM /* WARNING! `ret`, `call *`, `jmp *` not safe before this point. */ + ALTERNATIVE "", STR(call vmexit_virt_spec_ctrl), \ + X86_FEATURE_VIRT_SC_MSR_HVM + stgi GLOBAL(svm_stgi_label) mov %rsp,%rdi diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index 2455835eda..c4bdeaff52 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -52,6 +52,7 @@ #include #include #include +#include #include #include #include @@ -610,6 +611,16 @@ static void cf_check svm_cpuid_policy_changed(struct vcpu *v) svm_intercept_msr(v, MSR_SPEC_CTRL, cp->extd.ibrs ? MSR_INTERCEPT_NONE : MSR_INTERCEPT_RW); + /* + * Always trap write accesses to VIRT_SPEC_CTRL in order to cache the guest + * setting and avoid having to perform a rdmsr on vmexit to get the guest + * setting even if VIRT_SSBD is offered to Xen itself. + */ + svm_intercept_msr(v, MSR_VIRT_SPEC_CTRL, + cp->extd.virt_ssbd && cpu_has_virt_ssbd && + !cpu_has_amd_ssbd ? + MSR_INTERCEPT_WRITE : MSR_INTERCEPT_RW); + /* Give access to MSR_PRED_CMD if the guest has been told about it. */ svm_intercept_msr(v, MSR_PRED_CMD, cp->extd.ibpb ? MSR_INTERCEPT_NONE : MSR_INTERCEPT_RW); @@ -3105,6 +3116,30 @@ void svm_vmexit_handler(struct cpu_user_regs *regs) vmcb_set_vintr(vmcb, intr); } +/* Called with GIF=0. */ +void vmexit_virt_spec_ctrl(void) +{ + unsigned int val = opt_ssbd ? SPEC_CTRL_SSBD : 0; + + if ( val == current->arch.msrs->virt_spec_ctrl.raw ) + return; + + if ( cpu_has_virt_ssbd ) + wrmsr(MSR_VIRT_SPEC_CTRL, val, 0); +} + +/* Called with GIF=0. */ +void vmentry_virt_spec_ctrl(void) +{ + unsigned int val = current->arch.msrs->virt_spec_ctrl.raw; + + if ( val == (opt_ssbd ? SPEC_CTRL_SSBD : 0) ) + return; + + if ( cpu_has_virt_ssbd ) + wrmsr(MSR_VIRT_SPEC_CTRL, val, 0); +} + /* * Local variables: * mode: C diff --git a/xen/arch/x86/include/asm/cpufeatures.h b/xen/arch/x86/include/asm/cpufeatures.h index 7413febd7a..2240547b64 100644 --- a/xen/arch/x86/include/asm/cpufeatures.h +++ b/xen/arch/x86/include/asm/cpufeatures.h @@ -40,6 +40,7 @@ XEN_CPUFEATURE(SC_VERW_HVM, X86_SYNTH(24)) /* VERW used by Xen for HVM */ XEN_CPUFEATURE(SC_VERW_IDLE, X86_SYNTH(25)) /* VERW used by Xen for idle */ XEN_CPUFEATURE(XEN_SHSTK, X86_SYNTH(26)) /* Xen uses CET Shadow Stacks */ XEN_CPUFEATURE(XEN_IBT, X86_SYNTH(27)) /* Xen uses CET Indirect Branch Tracking */ +XEN_CPUFEATURE(VIRT_SC_MSR_HVM, X86_SYNTH(28)) /* MSR_VIRT_SPEC_CTRL exposed to HVM */ /* Bug words follow the synthetic words. */ #define X86_NR_BUG 1 diff --git a/xen/arch/x86/include/asm/msr.h b/xen/arch/x86/include/asm/msr.h index ab6fbb5051..de18e90b2e 100644 --- a/xen/arch/x86/include/asm/msr.h +++ b/xen/arch/x86/include/asm/msr.h @@ -375,6 +375,16 @@ struct vcpu_msrs */ uint32_t tsc_aux; + /* + * 0xc001011f - MSR_VIRT_SPEC_CTRL (if !X86_FEATURE_AMD_SSBD) + * + * AMD only. Guest selected value, context switched on guest VM + * entry/exit. + */ + struct { + uint32_t raw; + } virt_spec_ctrl; + /* * 0xc00110{27,19-1b} MSR_AMD64_DR{0-3}_ADDRESS_MASK * diff --git a/xen/arch/x86/msr.c b/xen/arch/x86/msr.c index 72c175fd8b..a1e268eea9 100644 --- a/xen/arch/x86/msr.c +++ b/xen/arch/x86/msr.c @@ -385,7 +385,10 @@ int guest_rdmsr(struct vcpu *v, uint32_t msr, uint64_t *val) if ( !cp->extd.virt_ssbd ) goto gp_fault; - *val = msrs->spec_ctrl.raw & SPEC_CTRL_SSBD; + if ( cpu_has_amd_ssbd ) + *val = msrs->spec_ctrl.raw & SPEC_CTRL_SSBD; + else + *val = msrs->virt_spec_ctrl.raw; break; case MSR_AMD64_DE_CFG: @@ -678,10 +681,15 @@ int guest_wrmsr(struct vcpu *v, uint32_t msr, uint64_t val) goto gp_fault; /* Only supports SSBD bit, the rest are ignored. */ - if ( val & SPEC_CTRL_SSBD ) - msrs->spec_ctrl.raw |= SPEC_CTRL_SSBD; + if ( cpu_has_amd_ssbd ) + { + if ( val & SPEC_CTRL_SSBD ) + msrs->spec_ctrl.raw |= SPEC_CTRL_SSBD; + else + msrs->spec_ctrl.raw &= ~SPEC_CTRL_SSBD; + } else - msrs->spec_ctrl.raw &= ~SPEC_CTRL_SSBD; + msrs->virt_spec_ctrl.raw = val & SPEC_CTRL_SSBD; break; case MSR_AMD64_DE_CFG: diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index f338bfe292..0d5ec877d1 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -406,9 +406,12 @@ static void __init print_details(enum ind_thunk thunk, uint64_t caps) (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) || boot_cpu_has(X86_FEATURE_SC_RSB_HVM) || boot_cpu_has(X86_FEATURE_MD_CLEAR) || + boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM) || opt_eager_fpu) ? "" : " None", boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ? " MSR_SPEC_CTRL" : "", - boot_cpu_has(X86_FEATURE_SC_MSR_HVM) ? " MSR_VIRT_SPEC_CTRL" : "", + (boot_cpu_has(X86_FEATURE_SC_MSR_HVM) || + boot_cpu_has(X86_FEATURE_VIRT_SC_MSR_HVM)) ? " MSR_VIRT_SPEC_CTRL" + : "", boot_cpu_has(X86_FEATURE_SC_RSB_HVM) ? " RSB" : "", opt_eager_fpu ? " EAGER_FPU" : "", boot_cpu_has(X86_FEATURE_MD_CLEAR) ? " MD_CLEAR" : ""); @@ -1069,6 +1072,10 @@ void __init init_speculation_mitigations(void) setup_force_cpu_cap(X86_FEATURE_SC_MSR_HVM); } + /* Support VIRT_SPEC_CTRL.SSBD if AMD_SSBD is not available. */ + if ( opt_msr_sc_hvm && !cpu_has_amd_ssbd && cpu_has_virt_ssbd ) + setup_force_cpu_cap(X86_FEATURE_VIRT_SC_MSR_HVM); + /* If we have IBRS available, see whether we should use it. */ if ( has_spec_ctrl && ibrs ) default_xen_spec_ctrl |= SPEC_CTRL_IBRS; From patchwork Tue May 17 15:31:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Roger_Pau_Monn=C3=A9?= X-Patchwork-Id: 12852683 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id DFABBC433F5 for ; Tue, 17 May 2022 15:32:11 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.331095.554581 (Exim 4.92) (envelope-from ) id 1nqzAx-0006Mq-VA; Tue, 17 May 2022 15:31:55 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 331095.554581; Tue, 17 May 2022 15:31:55 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nqzAx-0006Me-Rb; Tue, 17 May 2022 15:31:55 +0000 Received: by outflank-mailman (input) for mailman id 331095; Tue, 17 May 2022 15:31:54 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1nqzAw-0005fb-EW for xen-devel@lists.xenproject.org; Tue, 17 May 2022 15:31:54 +0000 Received: from esa3.hc3370-68.iphmx.com (esa3.hc3370-68.iphmx.com [216.71.145.155]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 78f11c72-d5f6-11ec-bd2c-47488cf2e6aa; Tue, 17 May 2022 17:31:52 +0200 (CEST) Received: from mail-dm3nam07lp2040.outbound.protection.outlook.com (HELO NAM02-DM3-obe.outbound.protection.outlook.com) ([104.47.56.40]) by ob1.hc3370-68.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 17 May 2022 11:31:49 -0400 Received: from DS7PR03MB5608.namprd03.prod.outlook.com (2603:10b6:5:2c9::18) by DM6PR03MB4617.namprd03.prod.outlook.com (2603:10b6:5:15e::10) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.5250.18; Tue, 17 May 2022 15:31:47 +0000 Received: from DS7PR03MB5608.namprd03.prod.outlook.com ([fe80::5df3:95ce:4dfd:134e]) by DS7PR03MB5608.namprd03.prod.outlook.com ([fe80::5df3:95ce:4dfd:134e%5]) with mapi id 15.20.5273.014; Tue, 17 May 2022 15:31:47 +0000 X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 78f11c72-d5f6-11ec-bd2c-47488cf2e6aa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1652801512; h=from:to:cc:subject:date:message-id:in-reply-to: references:content-transfer-encoding:mime-version; bh=00JQhhZ8kYD+yNYOJkhm3v0AevYhxWIvLZIvUqoZXkk=; b=VxvVtsWdDbudukOq07nh/9IVYOFeXCq9IW/aBnKl4TkeStZoUh9b+GhV hy7NVxAcVvEQFv6bKkKga18YKwyF9pJYOYZQLMSixUFEBCwXWsFLjqk5o ZzAAz4EbpFz1dtUWiiw6xzuZl48SuE0yqh5bEPmh75Cj0YdIQfFC6V34h k=; X-IronPort-RemoteIP: 104.47.56.40 X-IronPort-MID: 71507225 X-IronPort-Reputation: None X-IronPort-Listener: OutboundMail X-IronPort-SenderGroup: RELAY_O365 X-IronPort-MailFlowPolicy: $RELAYED IronPort-Data: A9a23:yNwSnKMfn9mCM63vrR2GlsFynXyQoLVcMsEvi/4bfWQNrUol0mZWz DROC2jQOaneYzOhfI9yPYy28EJUvsPdx9FiSgto+SlhQUwRpJueD7x1DKtR0wB+jCHnZBg6h ynLQoCYdKjYdleF+lH1dOKJQUBUjclkfJKlYAL/En03FFYMpBsJ00o5wbZk29Ew27BVPivW0 T/Mi5yHULOa82Yc3lI8s8pvfzs24ZweEBtB1rAPTagjUG32zhH5P7pGTU2FFFPqQ5E8IwKPb 72rIIdVXI/u10xF5tuNyt4Xe6CRK1LYFVDmZnF+A8BOjvXez8CbP2lS2Pc0MC9qZzu1c99Zz c18uZaQaS0VMbDnyMA3eFpFNDtnMvgTkFPHCSDXXc276WTjKyGp79A1SUY8MMsf5/p9BnxI+ boAMjcRYxufhuWwhrWmVu1rgcdlJ87uVG8dkig4kXeFUrB7H9afG80m5vcBtNs0rtpJEvvEI dIQdBJkbQjaYg0JMVASYH47tLjx2yGgKWcFwL6TjfQs3TnO0x5N67iuDPjwSPeMb+QOoUnN8 woq+Ey8WHn2Lue32TeDt36hmOLLtSf6Q54JUq218OZwh1+ezXBVDwcZPXObrP+jmwiBUtRQA 0UO/2wlqq1a3EesS9zgWRyksTiBtxgVVNdKO/037hmXzajZ6BrfAXILJhZDYtE7sM49RRQxy 0SE2djuAFRHr7m9WX+bsLCOoluP1TM9KGYDYWoISFAD6ty7/4Ur1EqTFpBkDbK/icDzFXfo2 TeWoSMihrIVy8kWy6G8+lOBiDWpznTUcjMICszsdjrNxmtEiESNPuRENXCzAS58Ebuk IronPort-HdrOrdr: A9a23:twXZsK9c9012LkxurRFuk+FRdb1zdoMgy1knxilNoENuH/Bwxv rFoB1E73TJYW4qKQkdcKO7SdK9qBLnhNZICOwqUYtKMzOW3FdAQLsC0WKm+UyYJ8SczJ8X6U 4DSdkYNDSYNzET4qjHCUuDYrAdKbK8gcOVbJLlvhJQpHZRGsNdBmlCajqzIwlTfk1rFJA5HJ 2T6o5svDy7Y0kaacy9Gz0sQ/XDj8ejruOqXTc2QzocrCWehzKh77D3VzKC2A0Fbj9JybA+tU DYjg3C4Lm5uf3T8G6R64aT1eUYpDLS8KoDOCW+sLlUFtwqsHfqWG1VYczNgNnympDs1L9lqq iIn/5qBbUI15qYRBDJnfKq4Xir7N9m0Q6f9XaIxXTkusD3XzQ8Fo5Igp9YaALQ7w46sMh7y7 8j5RPvi3N7N2K0oM3G3am9a/iqrDvFnVMy1eoIy3BPW4oXb7Fc6YQZ4UNOCZ8FWCb38pouHu ViBNzVoK8+SyLSU1nJ+m10hNC8VHU6GRmLBkAEp8yOyjBT2HR01VERysATlmoJsJg9V55H7e LZNbkArsA5cuYGKaZmQOsRS8q+DWLABRrKLWKJOFziULoKPnrcwqSHkondJNvaC6Dg4KFC5q gpCmkoylLaU3ieePGz4A== X-IronPort-AV: E=Sophos;i="5.91,233,1647316800"; d="scan'208";a="71507225" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ZhM7r9n6DueQZITqxA6LGl6DF/ywayMDJ930+5NfBmCtq9wnPttY2gE2AtJFRfsXBNWoF5nBYi2eo3J3MMLLMUpqGHItCzELeCKiNcjLM3JDXRFjP9Ylh7MpccDa/0sjqR/gG286r26NyPgugXdYFPyw4w2h5xsgphCebwW0K30RllSKs93fwApYPo/JUoO9WUKQGkTpo6HYw6ovbSXXL4WCdiHDmI5JwzdbOvCrixgHwCONJ8rrhlNh4N9svIyqhLMTnGoKEPzowvcn9bmqvnjmWm0+Pm2Nwt7H/aGdqeuUb00x0KcFuigFe0hKbgmdJbyoireCqI92wgXvdk3CRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=soyEvu+ROxQR6hPSOZ3YsH4mdXeUwWBiN8q89AyTdo8=; b=jhlSAewZpRC3DFcOBVccQaIz0p+ZwWyPEznxoghnKA8HwMSx/u8wyT1qoxdBFhIko84UqrJI+lBU/dxBWg58V85pUivgRtrzWNeBwKuLsaz5SULipzO5+BFYgkW/4jH5hfjNJr86qKi19S5heSs5Be6jrfji7BUNoa6TB1P1SzJEAQ89MvTPd2fo5R3SWsKdpdXDT7iYekI2v3YEczIosDKrqvfkG08z9aYLyT6iyh5zMqfnxuIit5+K+Nn/2+MJgkKvrUIsuvRL8d5f5hdeNC0zKO7GBWUkwyIZmoYlSbJKFxWyWqeVQbpjkgm3iUCStBu1E+mOqt+MDv0UOON0AQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=citrix.com; dmarc=pass action=none header.from=citrix.com; dkim=pass header.d=citrix.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=citrix.onmicrosoft.com; s=selector2-citrix-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=soyEvu+ROxQR6hPSOZ3YsH4mdXeUwWBiN8q89AyTdo8=; b=wfyQVX65A/KmR7h1+g1aOTOtGD6JK2vCwXRXaxXQ2OCOkDwNCIrArgCkqA7sElXFWBOe0SHLILHSPjtnNx9E4p3TcDnQQthEfa6MsnUFfTqRsLcliV23KGJC+jFXk5f14TtgOehcnxcVT3Fuc1JDBHcVLnvCJvEzWhscc/hKCJE= Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=citrix.com; From: Roger Pau Monne To: xen-devel@lists.xenproject.org Cc: Roger Pau Monne , Henry Wang , Community Manager , Jan Beulich , Andrew Cooper , Wei Liu Subject: [PATCH v6 3/3] amd/msr: implement VIRT_SPEC_CTRL for HVM guests using legacy SSBD Date: Tue, 17 May 2022 17:31:27 +0200 Message-Id: <20220517153127.40276-4-roger.pau@citrix.com> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220517153127.40276-1-roger.pau@citrix.com> References: <20220517153127.40276-1-roger.pau@citrix.com> X-ClientProxiedBy: LO2P265CA0410.GBRP265.PROD.OUTLOOK.COM (2603:10a6:600:a0::14) To DS7PR03MB5608.namprd03.prod.outlook.com (2603:10b6:5:2c9::18) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: ed0661ea-dadb-4440-8266-08da381a5b2f X-MS-TrafficTypeDiagnostic: DM6PR03MB4617:EE_ X-Microsoft-Antispam-PRVS: X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4R9Jb6It8dM1LQvggvhwKDniMPQJflxVCsqy0mkGDO3HbqV3OEglsVHT+4MeFamaLfyaKHDpwxQNNKzASINe1SquRDOMpeO2TLpotkBwW0kAxuo2M/1vNXBEqMtISzRD1W9hRkrMQzJW0gKMZjhQWQWv5jOKMqsPvSqBflYGIUg1RwN1MbvQx8fWFUijSfg496kBIFP5vARYIKVlsaIPG7A1/qZEjFDnP3B3QxXMoaqd656AnnDDHVLgTs0FK6Lku8fV0ImAE7EAreFtGU6V9ONFW2UR359K1uSqFy8vVMT1ZqHTCy0jr6SDQ4o/zAgW+danJvdd+iA9G2ava7mUXPttPOQEZlIi5ostZyZ7qUvQvqMUlDyEPWDhtZv7F+8quQAHAf1aPWyTdxoiXmNNpRlgDPKyi1zitL0GkUtKYhY7y0IeVtLyW7t4GckcEpWptn4ChVolcPD89g4y1kYCL5dYfQL7x/L9Zl7KkSKrdg7oNLv5FnKXQH6weTraIia0Vq7k7C4mS3zQV44FdKPp3wLhQff67Raz0VP7vG9EO4mRREWgmOkwZeAhsjrjJ/S0VTfzWDJbLYVpgYu23QdqQ7g8KYk3n2VpQ6xbN3mhQGCoX9Bf5ESzN8KXe7oPvanr0laguCR6T8fplCaj1WCviMIetyTPI5InlyTfUchdKqmluEZfZcQg11JnNk5aWWHoiQ9xnZYVoM144XJ60aYSXZ6Zh4oaXFTb7r/nYwZPvw0BajIZqmgbOg6VgqbUgWFaS4q9xls/4uji8MxRs4X+Ow== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DS7PR03MB5608.namprd03.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230001)(4636009)(366004)(4326008)(2616005)(8676002)(82960400001)(186003)(6916009)(66946007)(66556008)(1076003)(66476007)(2906002)(5660300002)(36756003)(38100700002)(6506007)(6512007)(508600001)(966005)(6486002)(86362001)(316002)(54906003)(83380400001)(6666004)(8936002)(26005);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?utf-8?q?9S1BlL9JpI9If6ZeAEYviGtlC2vl?= =?utf-8?q?wkt6ZipLOr2vMCdiHakGuoI5X4MjE4Wce5RgwbjpUA3tw7B4YutRa7Yz9l0wpdG6X?= =?utf-8?q?7N5r7FKK6Og1poh5WeXfjCxB9jfUVvEpsnzQYZZf+CkRhwennoOl/VR8t6xqBF0TO?= =?utf-8?q?JfIjhBqfDzqZDzzDTcD+QnJQA0E7L53lN+7LJPozTaA2xkKH+WOZVvuBXWrj+Kkdc?= =?utf-8?q?UZbr5woAXliywFO6FHx2pKi26qV/eq+zJ3+1KtEqoQo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orThULBWpf?= =?utf-8?q?9rrXiKV6kuU0G+Ks89IWjUuQuDdiERH6Jbcc+CTOvtYrt6bIM+b829Y+hQgexmXxb?= =?utf-8?q?Lr200znKM0GlE/oTS55SkBndOpXOGqwwW2ClAocCEsuQX3RkphGCqbYfctCt+pttK?= =?utf-8?q?diCYzexqA1GyVqHfEcZ5JMfTHrMJlLdow/rKZto0CT0oJ2Rzx77nZ3a0un08yqIVB?= =?utf-8?q?MOFrAcHXcdfV7k5meet5UbnD3e8GxESWWtc4hLefiNXkmGlJrx6G+F8lcrch4k3NI?= =?utf-8?q?3HXVCQlq+wztfYDvEM8BrhHwryu80Vwbj1KOtxZeorE/nCsz3Le5BQ6WdNiTcbV7h?= =?utf-8?q?SXThervynOBD4BaSZo2oK2XjKzpeLUuKQmZzLMAW/rf12EEN5sEMKaUK+nwvjV2hF?= =?utf-8?q?uT3yCRECCBp1uJcxI0lB9uZ3gWjVWlHLCiskq/gBqHS5nJ+dYyBkucU95oSpm1AzA?= =?utf-8?q?fRPgHe0h4X646cu5SjOkL3CDG9ThrENUsLujdi47Nk2TVYVWJQAyztn4gqFq3AOeS?= =?utf-8?q?86Qb6tJS/Pf5U5RFCo8zB2bQwfcnyNDy3bdbMHO2x6yFqQNxxINt4=3D?= X-OriginatorOrg: citrix.com X-MS-Exchange-CrossTenant-Network-Message-Id: ed0661ea-dadb-4440-8266-08da381a5b2f X-MS-Exchange-CrossTenant-AuthSource: DS7PR03MB5608.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 17 May 2022 15:31:47.8679 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 335836de-42ef-43a2-b145-348c2ee9ca5b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Y4T/DzqXGLGOjtrQ066aC2jw0oIHfRqgWFygLrOxHck265sEQvwgLEHId5rWcWe7lMrJUJG0jThxuZkwmy/v9g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR03MB4617 Expose VIRT_SSBD to guests if the hardware supports setting SSBD in the LS_CFG MSR (a.k.a. non-architectural way). Different AMD CPU families use different bits in LS_CFG, so exposing VIRT_SPEC_CTRL.SSBD allows for an unified way of exposing SSBD support to guests on AMD hardware that's compatible migration wise, regardless of what underlying mechanism is used to set SSBD. Note that on AMD Family 17h and Hygon Family 18h processors the value of SSBD in LS_CFG is shared between threads on the same core, so there's extra logic in order to synchronize the value and have SSBD set as long as one of the threads in the core requires it to be set. Such logic also requires extra storage for each thread state, which is allocated at initialization time. Do the context switching of the SSBD selection in LS_CFG between hypervisor and guest in the same handler that's already used to switch the value of VIRT_SPEC_CTRL. Suggested-by: Andrew Cooper Signed-off-by: Roger Pau Monné Reviewed-by: Jan Beulich Acked-by: Henry Wang --- Changes since v5: - Fix one codding style issue. Changes since v4: - Slightly change usage of val/opt_ssbd in vm{exit,entry}_virt_spec_ctrl. - Pull opt_ssbd outside of the for loop in amd_setup_legacy_ssbd(). - Fix indentation. - Remove ASSERTs/BUG_ONs from GIF=0 context. Changes since v3: - Align ssbd per-core struct to a cache line. - Open code a simple spinlock to avoid playing tricks with the lock detector. - s/ssbd_core/ssbd_ls_cfg/. - Fix log message wording. - Fix define name and remove comment. - Also handle Hygon processors (Fam18h). - Add changelog entry. Changes since v2: - Fix codding style issues. - Use AMD_ZEN1_MAX_SOCKETS to define the max number of possible sockets in Zen1 systems. Changes since v1: - Report legacy SSBD support using a global variable. - Use ro_after_init for ssbd_max_cores. - Handle boot_cpu_data.x86_num_siblings < 1. - Add comment regarding _irqsave usage in amd_set_legacy_ssbd. --- CHANGELOG.md | 3 + xen/arch/x86/cpu/amd.c | 121 ++++++++++++++++++++++++++++----- xen/arch/x86/hvm/svm/svm.c | 4 ++ xen/arch/x86/include/asm/amd.h | 4 ++ xen/arch/x86/spec_ctrl.c | 4 +- 5 files changed, 118 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a7755d7b0..9a007e2513 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed / support downgraded - dropped support for the (x86-only) "vesa-mtrr" and "vesa-remap" command line options +### Added + - Support VIRT_SSBD feature for HVM guests on AMD. + ## [4.16.0](https://xenbits.xen.org/gitweb/?p=xen.git;a=shortlog;h=staging) - 2021-12-02 ### Removed diff --git a/xen/arch/x86/cpu/amd.c b/xen/arch/x86/cpu/amd.c index 4999f8be2b..5f9e734e84 100644 --- a/xen/arch/x86/cpu/amd.c +++ b/xen/arch/x86/cpu/amd.c @@ -48,6 +48,7 @@ boolean_param("allow_unsafe", opt_allow_unsafe); /* Signal whether the ACPI C1E quirk is required. */ bool __read_mostly amd_acpi_c1e_quirk; +bool __ro_after_init amd_legacy_ssbd; static inline int rdmsr_amd_safe(unsigned int msr, unsigned int *lo, unsigned int *hi) @@ -685,23 +686,10 @@ void amd_init_lfence(struct cpuinfo_x86 *c) * Refer to the AMD Speculative Store Bypass whitepaper: * https://developer.amd.com/wp-content/resources/124441_AMD64_SpeculativeStoreBypassDisable_Whitepaper_final.pdf */ -void amd_init_ssbd(const struct cpuinfo_x86 *c) +static bool set_legacy_ssbd(const struct cpuinfo_x86 *c, bool enable) { int bit = -1; - if (cpu_has_ssb_no) - return; - - if (cpu_has_amd_ssbd) { - /* Handled by common MSR_SPEC_CTRL logic */ - return; - } - - if (cpu_has_virt_ssbd) { - wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0); - return; - } - switch (c->x86) { case 0x15: bit = 54; break; case 0x16: bit = 33; break; @@ -715,20 +703,119 @@ void amd_init_ssbd(const struct cpuinfo_x86 *c) if (rdmsr_safe(MSR_AMD64_LS_CFG, val) || ({ val &= ~mask; - if (opt_ssbd) + if (enable) val |= mask; false; }) || wrmsr_safe(MSR_AMD64_LS_CFG, val) || ({ rdmsrl(MSR_AMD64_LS_CFG, val); - (val & mask) != (opt_ssbd * mask); + (val & mask) != (enable * mask); })) bit = -1; } - if (bit < 0) + return bit >= 0; +} + +void amd_init_ssbd(const struct cpuinfo_x86 *c) +{ + if (cpu_has_ssb_no) + return; + + if (cpu_has_amd_ssbd) { + /* Handled by common MSR_SPEC_CTRL logic */ + return; + } + + if (cpu_has_virt_ssbd) { + wrmsrl(MSR_VIRT_SPEC_CTRL, opt_ssbd ? SPEC_CTRL_SSBD : 0); + return; + } + + if (!set_legacy_ssbd(c, opt_ssbd)) { printk_once(XENLOG_ERR "No SSBD controls available\n"); + if (amd_legacy_ssbd) + panic("CPU feature mismatch: no legacy SSBD\n"); + } else if (c == &boot_cpu_data) + amd_legacy_ssbd = true; +} + +static struct ssbd_ls_cfg { + bool locked; + unsigned int count; +} __cacheline_aligned *ssbd_ls_cfg; +static unsigned int __ro_after_init ssbd_max_cores; +#define AMD_FAM17H_MAX_SOCKETS 2 + +bool __init amd_setup_legacy_ssbd(void) +{ + unsigned int i; + + if ((boot_cpu_data.x86 != 0x17 && boot_cpu_data.x86 != 0x18) || + boot_cpu_data.x86_num_siblings <= 1) + return true; + + /* + * One could be forgiven for thinking that c->x86_max_cores is the + * correct value to use here. + * + * However, that value is derived from the current configuration, and + * c->cpu_core_id is sparse on all but the top end CPUs. Derive + * max_cpus from ApicIdCoreIdSize which will cover any sparseness. + */ + if (boot_cpu_data.extended_cpuid_level >= 0x80000008) { + ssbd_max_cores = 1u << MASK_EXTR(cpuid_ecx(0x80000008), 0xf000); + ssbd_max_cores /= boot_cpu_data.x86_num_siblings; + } + if (!ssbd_max_cores) + return false; + + ssbd_ls_cfg = xzalloc_array(struct ssbd_ls_cfg, + ssbd_max_cores * AMD_FAM17H_MAX_SOCKETS); + if (!ssbd_ls_cfg) + return false; + + if (opt_ssbd) + for (i = 0; i < ssbd_max_cores * AMD_FAM17H_MAX_SOCKETS; i++) + /* Set initial state, applies to any (hotplug) CPU. */ + ssbd_ls_cfg[i].count = boot_cpu_data.x86_num_siblings; + + return true; +} + +/* + * Executed from GIF==0 context: avoid using BUG/ASSERT or other functionality + * that relies on exceptions as those are not expected to run in GIF==0 + * context. + */ +void amd_set_legacy_ssbd(bool enable) +{ + const struct cpuinfo_x86 *c = ¤t_cpu_data; + struct ssbd_ls_cfg *status; + + if ((c->x86 != 0x17 && c->x86 != 0x18) || c->x86_num_siblings <= 1) { + set_legacy_ssbd(c, enable); + return; + } + + status = &ssbd_ls_cfg[c->phys_proc_id * ssbd_max_cores + + c->cpu_core_id]; + + /* + * Open code a very simple spinlock: this function is used with GIF==0 + * and different IF values, so would trigger the checklock detector. + * Instead of trying to workaround the detector, use a very simple lock + * implementation: it's better to reduce the amount of code executed + * with GIF==0. + */ + while (test_and_set_bool(status->locked)) + cpu_relax(); + status->count += enable ? 1 : -1; + if (enable ? status->count == 1 : !status->count) + set_legacy_ssbd(c, enable); + barrier(); + write_atomic(&status->locked, false); } void __init detect_zen2_null_seg_behaviour(void) diff --git a/xen/arch/x86/hvm/svm/svm.c b/xen/arch/x86/hvm/svm/svm.c index c4bdeaff52..3cc5fcdc44 100644 --- a/xen/arch/x86/hvm/svm/svm.c +++ b/xen/arch/x86/hvm/svm/svm.c @@ -3126,6 +3126,8 @@ void vmexit_virt_spec_ctrl(void) if ( cpu_has_virt_ssbd ) wrmsr(MSR_VIRT_SPEC_CTRL, val, 0); + else + amd_set_legacy_ssbd(val); } /* Called with GIF=0. */ @@ -3138,6 +3140,8 @@ void vmentry_virt_spec_ctrl(void) if ( cpu_has_virt_ssbd ) wrmsr(MSR_VIRT_SPEC_CTRL, val, 0); + else + amd_set_legacy_ssbd(val); } /* diff --git a/xen/arch/x86/include/asm/amd.h b/xen/arch/x86/include/asm/amd.h index a82382e6bf..6a42f68542 100644 --- a/xen/arch/x86/include/asm/amd.h +++ b/xen/arch/x86/include/asm/amd.h @@ -151,4 +151,8 @@ void check_enable_amd_mmconf_dmi(void); extern bool amd_acpi_c1e_quirk; void amd_check_disable_c1e(unsigned int port, u8 value); +extern bool amd_legacy_ssbd; +bool amd_setup_legacy_ssbd(void); +void amd_set_legacy_ssbd(bool enable); + #endif /* __AMD_H__ */ diff --git a/xen/arch/x86/spec_ctrl.c b/xen/arch/x86/spec_ctrl.c index 0d5ec877d1..495e6f9405 100644 --- a/xen/arch/x86/spec_ctrl.c +++ b/xen/arch/x86/spec_ctrl.c @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -1073,7 +1074,8 @@ void __init init_speculation_mitigations(void) } /* Support VIRT_SPEC_CTRL.SSBD if AMD_SSBD is not available. */ - if ( opt_msr_sc_hvm && !cpu_has_amd_ssbd && cpu_has_virt_ssbd ) + if ( opt_msr_sc_hvm && !cpu_has_amd_ssbd && + (cpu_has_virt_ssbd || (amd_legacy_ssbd && amd_setup_legacy_ssbd())) ) setup_force_cpu_cap(X86_FEATURE_VIRT_SC_MSR_HVM); /* If we have IBRS available, see whether we should use it. */