Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp563657rwb; Thu, 12 Jan 2023 09:21:36 -0800 (PST) X-Google-Smtp-Source: AMrXdXtFGesmpaktSwPdCQfvlUq7Esbdz/voNODYIpOoYjkDNiv5sqpD0SM+/aQfmhuLrsh3+JHW X-Received: by 2002:a17:906:81b:b0:869:a799:1f85 with SMTP id e27-20020a170906081b00b00869a7991f85mr122320ejd.76.1673544096405; Thu, 12 Jan 2023 09:21:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673544096; cv=none; d=google.com; s=arc-20160816; b=O/dmF1FS8W6/1hDFPJ3IsV8nh7HDE6XcN+7O9lgRCx+58xydSpFUu0dUde6KMd8sYR Pk7zdDkKj5pUp8sT/nrEDJmao/EiEdC/Ewdvl7qY7l/dqQw9ElReYwoqkwBuShJhox7b qR2BypuDPlPofkOJbck7B4MmHy0EeQekZtonFrA3A8+9TIE8JdA0ED8H6EAd67qjYSeR p+o+DxVWtZayeZBk1Xbntv3g1fyurfMSX//+r9g/lLCpMqbUBk8Czfp3wLQ7ik/tUxwC xj166FK2zPEwb1yQBsW+D+Dpsl26idNsAHvkYFpi1ecOVS3twdgvFkDSHysLGRIi+N3X xSFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DNXu8v0aQyuFFBhCuLUPCv5TgrN9GxQajXi86eX1Vf0=; b=oGVHMfvYRz0HUOAplaJz1MpTbAPvehOEAkRCW7tR0oxrZLzqbNF++z/tH8ii9WjgyO kwb/WnA+Uf87Bu/t4+pKmEh4uF9FeYts1r2/V0ULOe/H++PoXf1Tch5GLoNt8nKy3Mkg 7rCM/bewKubAPPNShhKcscH5SB6osD2n5q7l44sHeGAAViJTBV3H19RMJbaIlgzfqH9w aG2SzY8IFpwFaIfN++ZTqniEEZecKKm7iRtNfaguM4g0WYxaDDrRGHbzF3TVIvM22qoH zGF+M6ZpqoxoJfV4/juk2JmkYjmQFUznjVVJuH5v2AkQJnTjzvSeo904rBPWb3A+xdxd sEpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="E/ZB6oGl"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id va28-20020a17090711dc00b007b99436026dsi10698040ejb.968.2023.01.12.09.21.24; Thu, 12 Jan 2023 09:21:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="E/ZB6oGl"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241174AbjALQtO (ORCPT + 50 others); Thu, 12 Jan 2023 11:49:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240246AbjALQif (ORCPT ); Thu, 12 Jan 2023 11:38:35 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C22B91C91E; Thu, 12 Jan 2023 08:34:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1673541245; x=1705077245; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=yn42PC0izbUu9yoLkljOnyBooo7nxgVAJduGRhE8SO4=; b=E/ZB6oGl1+UFy4q/uZ8Np2GtrIyI1ZzD+K6RaALdsXzArEgxxRyzXQhZ TgETlZ0scbmk97xpGxmCETBont/x8/kVLR5tSrJQLn4nriFAx8eC27Ke0 yklY1lpM9fht5lzNXhJ4rx2UA90LINB4EjnwKzPKCb6QTtmEM2lz9JlIW yjydIAITtPdxwhDREphWhsVtEzCMWx/P1sQQC788FK0wan5XfOH2oUkqc MdKFzASroreoiKLiLBaap3DrribH+t3AyGlxVbFSl3CYotfj+PE3S2ixK VDZS6VnxNEDsaj/cauDtRyhpzIJ0mbMZCC/6X+GbwGDK8Tp3vlawhF9ew Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="323811910" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="323811910" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:29 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10588"; a="721151818" X-IronPort-AV: E=Sophos;i="5.97,211,1669104000"; d="scan'208";a="721151818" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jan 2023 08:33:29 -0800 From: isaku.yamahata@intel.com To: kvm@vger.kernel.org, linux-kernel@vger.kernel.org Cc: isaku.yamahata@intel.com, isaku.yamahata@gmail.com, Paolo Bonzini , erdemaktas@google.com, Sean Christopherson , Sagi Shahar , David Matlack , Sean Christopherson Subject: [PATCH v11 053/113] KVM: TDX: Add accessors VMX VMCS helpers Date: Thu, 12 Jan 2023 08:32:01 -0800 Message-Id: <4787650143f364c6a4c471c89dba2b5bd590b7c0.1673539699.git.isaku.yamahata@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Sean Christopherson TDX defines SEAMCALL APIs to access TDX control structures corresponding to the VMX VMCS. Introduce helper accessors to hide its SEAMCALL ABI details. Signed-off-by: Isaku Yamahata --- arch/x86/kvm/vmx/tdx.h | 95 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index e909883d60fa..237c8038eb6a 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -57,6 +57,101 @@ static inline struct vcpu_tdx *to_tdx(struct kvm_vcpu *vcpu) return container_of(vcpu, struct vcpu_tdx, vcpu); } +static __always_inline void tdvps_vmcs_check(u32 field, u8 bits) +{ +#define VMCS_ENC_ACCESS_TYPE_MASK 0x1UL +#define VMCS_ENC_ACCESS_TYPE_FULL 0x0UL +#define VMCS_ENC_ACCESS_TYPE_HIGH 0x1UL +#define VMCS_ENC_ACCESS_TYPE(field) ((field) & VMCS_ENC_ACCESS_TYPE_MASK) + + /* TDX is 64bit only. HIGH field isn't supported. */ + BUILD_BUG_ON_MSG(__builtin_constant_p(field) && + VMCS_ENC_ACCESS_TYPE(field) == VMCS_ENC_ACCESS_TYPE_HIGH, + "Read/Write to TD VMCS *_HIGH fields not supported"); + + BUILD_BUG_ON(bits != 16 && bits != 32 && bits != 64); + +#define VMCS_ENC_WIDTH_MASK GENMASK(14, 13) +#define VMCS_ENC_WIDTH_16BIT (0UL << 13) +#define VMCS_ENC_WIDTH_64BIT (1UL << 13) +#define VMCS_ENC_WIDTH_32BIT (2UL << 13) +#define VMCS_ENC_WIDTH_NATURAL (3UL << 13) +#define VMCS_ENC_WIDTH(field) ((field) & VMCS_ENC_WIDTH_MASK) + + /* TDX is 64bit only. i.e. natural width = 64bit. */ + BUILD_BUG_ON_MSG(bits != 64 && __builtin_constant_p(field) && + (VMCS_ENC_WIDTH(field) == VMCS_ENC_WIDTH_64BIT || + VMCS_ENC_WIDTH(field) == VMCS_ENC_WIDTH_NATURAL), + "Invalid TD VMCS access for 64-bit field"); + BUILD_BUG_ON_MSG(bits != 32 && __builtin_constant_p(field) && + VMCS_ENC_WIDTH(field) == VMCS_ENC_WIDTH_32BIT, + "Invalid TD VMCS access for 32-bit field"); + BUILD_BUG_ON_MSG(bits != 16 && __builtin_constant_p(field) && + VMCS_ENC_WIDTH(field) == VMCS_ENC_WIDTH_16BIT, + "Invalid TD VMCS access for 16-bit field"); +} + +static __always_inline void tdvps_state_non_arch_check(u64 field, u8 bits) {} +static __always_inline void tdvps_management_check(u64 field, u8 bits) {} + +#define TDX_BUILD_TDVPS_ACCESSORS(bits, uclass, lclass) \ +static __always_inline u##bits td_##lclass##_read##bits(struct vcpu_tdx *tdx, \ + u32 field) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_rd(tdx->tdvpr_pa, TDVPS_##uclass(field), &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) { \ + pr_err("TDH_VP_RD["#uclass".0x%x] failed: 0x%llx\n", \ + field, err); \ + return 0; \ + } \ + return (u##bits)out.r8; \ +} \ +static __always_inline void td_##lclass##_write##bits(struct vcpu_tdx *tdx, \ + u32 field, u##bits val) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), val, \ + GENMASK_ULL(bits - 1, 0), &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] = 0x%llx failed: 0x%llx\n", \ + field, (u64)val, err); \ +} \ +static __always_inline void td_##lclass##_setbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), bit, bit, &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] |= 0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} \ +static __always_inline void td_##lclass##_clearbit##bits(struct vcpu_tdx *tdx, \ + u32 field, u64 bit) \ +{ \ + struct tdx_module_output out; \ + u64 err; \ + \ + tdvps_##lclass##_check(field, bits); \ + err = tdh_vp_wr(tdx->tdvpr_pa, TDVPS_##uclass(field), 0, bit, &out); \ + if (KVM_BUG_ON(err, tdx->vcpu.kvm)) \ + pr_err("TDH_VP_WR["#uclass".0x%x] &= ~0x%llx failed: 0x%llx\n", \ + field, bit, err); \ +} + +TDX_BUILD_TDVPS_ACCESSORS(16, VMCS, vmcs); +TDX_BUILD_TDVPS_ACCESSORS(32, VMCS, vmcs); +TDX_BUILD_TDVPS_ACCESSORS(64, VMCS, vmcs); + static __always_inline u64 td_tdcs_exec_read64(struct kvm_tdx *kvm_tdx, u32 field) { struct tdx_module_output out; -- 2.25.1