Received: by 2002:a05:7208:9594:b0:7e:5202:c8b4 with SMTP id gs20csp1177761rbb; Mon, 26 Feb 2024 00:47:42 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVXNnIARh4vzjd7wq2I9kjmBtrt2i3Xmc3XNznWVfRXeibBI+8q3oTg0KVSfNA2rgfwpiQIqIRFh9/LC9+ZhzcgVOw2XWyy3sw1pBRNpw== X-Google-Smtp-Source: AGHT+IEMatpC85voNU1tTF0q7i+uiky4wB9OQHtFdFIkuuzfWFPRK/cExBDQ6o5U00uAEZ3nlKXb X-Received: by 2002:a0c:e18b:0:b0:68e:e9d2:3e3 with SMTP id p11-20020a0ce18b000000b0068ee9d203e3mr6281753qvl.46.1708937262231; Mon, 26 Feb 2024 00:47:42 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708937262; cv=pass; d=google.com; s=arc-20160816; b=fff3SY9TE2TWzFpdCTh0eClNpCNFFdYXq2VErLXIDdBFfPdfXVTYUAaQMhbqSF5b26 8OE22IF/7rO8F9bIq+9zp4V2z38g3mP9wXqKCczkWG7Fi2LsXdjgOGXj8xP98uzBC75r xWTNS09qG3dyIXHZai+rAEX4ZbFJj9d7RJA62vNS2mD/r4rowA+VjrJ5bs3osJNW3dT1 WpuJfo/rwJiMjX4Z1jbqxcRpA1y6thhdDHe7JeVuh5Fyk+lA9Iynwn/egM4n5FsrUaUW +PhsVKHb59FRQ4Rx7sPs3ZsUFBqmN3rqZAqQlPRVGhQnoyJO508g9IybrimYUa3yWAqO apPA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=O3Oo94WvVTmEpnVvchVpoh24J9550k3vNfEY2aKEuRQ=; fh=Itbyk7CEvizIrzGEESCqq3I2tZgG1kc/GkVOa3S7Hsg=; b=iJ9kpxqAc7TB9sffS8pYi+83uRaPmw4Rp3BTyqxSRPI/E3jlIPycIpAJaZmiHa+AwN AwFlcQlABSdDrfquGy/vfPw7UUiBOR62Mlw0Vt+DZhR3mM+2lFh/P2J7w2DX6HbgiLrn 4f8XQgpHELyVNTlBde05au7hpqNzN4LdeEtnhvh/Uw6AW+D3DHTPDCFaZ5cxU1C/Ar90 IAQuMrD/JCEA6PyP8iyswRiKU2b2lDOSuMMhjt7/b+RhwxtEyj6bjYfnCT85LoLVN2xQ esNC1jdAIvfc5SAxkHopJHEOZKCe06GXU9rJ1z+9+Xw6ltDblZXKC1ctmvSMk5UwybW7 mxwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EF+SgFz7; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80825-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80825-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id 6-20020a0562140dc600b0068f6008ce6bsi4919093qvt.432.2024.02.26.00.47.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 00:47:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-80825-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=EF+SgFz7; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-80825-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-80825-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id E94B51C2148B for ; Mon, 26 Feb 2024 08:47:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 93070627E0; Mon, 26 Feb 2024 08:28:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="EF+SgFz7" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3731660DCA; Mon, 26 Feb 2024 08:28:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.19 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936106; cv=none; b=jKMTEMqQUhHhZLTA8F7UdamRYEAy8QSoSaNRpYVbGQ4Fa1mfiq3XsZUyGw2xyaK4Zuxv0kQgygZRW+0UYuArxiIvU9lQXIcaQ/Ff6J9H61LwvhmgKwd7clt7vj/NqTE5JlsoQM7tBhiNdSENmWtDDrQlvCFBRuql7o0RrGYLTdI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708936106; c=relaxed/simple; bh=f2K3xma6cqKdxbAmy7RDBYX7WxriqJwDIAHVJ97Ro9w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eHPzV/ga2jRQye/orTE6mvIQzrqWtalHTgfOZgwXE21VMdBA3Dh1vxuXorc86Xwtamit5SLeTUDz33vmrz7qnsY8kIEUZ671A5hWJYm17hrlPufqiokpr+XH3GAeVBq/rtf3G7FpENqFXTieONdCrcodCmcRlOAZyQkfLDPpPS4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=EF+SgFz7; arc=none smtp.client-ip=198.175.65.19 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708936104; x=1740472104; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=f2K3xma6cqKdxbAmy7RDBYX7WxriqJwDIAHVJ97Ro9w=; b=EF+SgFz7nBfY2vGHVofJ/si57Cr4qaeSvjIed5qW9+zZ80HiTysqLhwl zXz3P+BZzX7+UwGYm1EQTHy64ymJDHEW/2cyJdSjDsKEEyT2rQpJfxuzb dhqShygbcRqjGGFqc5pVe/DrgMMYi/0ha8nBtXnaUtpUfMTEuAzLq9oaN e6i+DjPARau2o8AFyqWUvUKDsjJQNiFbT/eLxOydZyRpOsKhRR4ZU8d9T xJn1QKJKwKMAxMITHGsbsdXrMFRrkorQsSxfq8TCRWLAfaTtzr7HBgblt A72/dO1d5fHqoUpiuMnGFFIyucZwMr9dCt7yGuggMuD/OWvg0hTV1qyIA Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10995"; a="3069450" X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="3069450" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:24 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,185,1705392000"; d="scan'208";a="11272338" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 00:28:23 -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 , Kai Huang , chen.bo@intel.com, hang.yuan@intel.com, tina.zhang@intel.com Subject: [PATCH v19 066/130] KVM: TDX: Add accessors VMX VMCS helpers Date: Mon, 26 Feb 2024 00:26:08 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Isaku Yamahata 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 --- v19: - deleted unnecessary stub functions, tdvps_state_non_arch_check() and tdvps_management_check(). --- arch/x86/kvm/vmx/tdx.h | 92 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/arch/x86/kvm/vmx/tdx.h b/arch/x86/kvm/vmx/tdx.h index d3077151252c..8a0d1bfe34a0 100644 --- a/arch/x86/kvm/vmx/tdx.h +++ b/arch/x86/kvm/vmx/tdx.h @@ -58,6 +58,98 @@ 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"); +} + +#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_args 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_args 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_args 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_args 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_args out; -- 2.25.1