Received: by 2002:a05:6a10:9afc:0:0:0:0 with SMTP id t28csp2278542pxm; Fri, 4 Mar 2022 12:59:11 -0800 (PST) X-Google-Smtp-Source: ABdhPJwiE938NysPOCNH0cUHnuN0SZkV4BbBqb5bfjTIIwZ4LH0TMZ1a3imzXLCho+ThdgvSmAs3 X-Received: by 2002:a17:902:780f:b0:14f:d765:b6f1 with SMTP id p15-20020a170902780f00b0014fd765b6f1mr260331pll.85.1646427551393; Fri, 04 Mar 2022 12:59:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1646427551; cv=none; d=google.com; s=arc-20160816; b=1KJoaw0oCcvymlj8FQBpkM464WA09Z1T8koZLc/+0b/HoO0+LXv1q1uFBVhr7P0ATH wpD6wG0WKB125PLh1HKpi5nk4+6SWBYQGivwoV9u4GcPux/WVJXY3JeuUoNNSboq7xGT VeSkZAIJQrWT1D9kqVjQlL93DQgAQ6SqWm5+Art2X4wg4mzyeuTonhOaS0ealKSoMdxS U4d7mtV7GPLvDtR2LkDqfkDJjpHV0N9GWlWIYHRv+aHDc93uJ+f7vzpV/c3UYVbrtpvL JDX4cD8ujW0VTbp78hTFeLcW7Iecd4ySYWPx4od6h2djs99VApNkWyv859Fo+7uJxMJa 29Qw== 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=mMknfJhBzdgcU7MdvGqZdxKkt540wOgFYDOn8Tc+/cU=; b=IWm5tmf9+QX162vO+DiMZxdUOJLi51cVDbxZ1GnE7lSbtdUyEnL1Ro63YMHv7Z4qoC qYKnPqTlUmDE6e+FrKvO1rp/PPchLQhb1ZyWzmOTcYsIA4TNLPfPEVu5QOfFB8DXj6UY hnPmDm0U+K3TT+D5ywJYKKh/BIrpVruJKD6XYsXuBChtrXhYbQ73AOLU0BMSpRHuJoA0 jqdIWrMfX8MnVpMo+pVdMLqXuv9qFjFcS/8nlMHzCgMsXQDnInWVL0ByaAmhqOMo7oaa Fit9kO9DJaWAIavRwtShYZjJf5ba7lukiSuP/iB3ZfvLrW/WShv327RysdfeBe4ssQax a8kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="HF1XjB/A"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id z69-20020a633348000000b0036fe3b212cfsi5743845pgz.602.2022.03.04.12.59.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 12:59:11 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b="HF1XjB/A"; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 0565C24F86F; Fri, 4 Mar 2022 12:09:08 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230419AbiCDUHm (ORCPT + 99 others); Fri, 4 Mar 2022 15:07:42 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53084 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230401AbiCDUHc (ORCPT ); Fri, 4 Mar 2022 15:07:32 -0500 Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 415CEA7756; Fri, 4 Mar 2022 12:01:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1646424109; x=1677960109; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=pH5YDceO6tTdr5IZ6o3dwukz5Ln1m9AUbdFUHciWGhM=; b=HF1XjB/A3rmE9G6OoCzQ+h06F4cFNMmmYsItBv2JbFH5bYobiITXajJU 3m4ryL3snT+XYcIYjy4fWdMqg8wrXhd9UADVCxGAn5nOKmTCt/WSLsylI idPozNI1lRlOZqvZbmCngGAlkR975khhKm1KpiBQD0djj5dHn0AYIrLQq 5M5VGIsOGCmw+fKq6CE6gvfgacrLMRL9xIInnGgi2SnSnp+RG2p/2glDo Djn6Cy5KVnscbXnNwmZdEbfaD7GS3RtVhOItTd8fpdsnZ7+L1UPbjQF34 llXNwDrnml7Qe+jBUXwEZBhaPaUQgP1KRWXjmkdNKzEgtGbOgBGHzD5S7 A==; X-IronPort-AV: E=McAfee;i="6200,9189,10276"; a="253983411" X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="253983411" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:15 -0800 X-IronPort-AV: E=Sophos;i="5.90,156,1643702400"; d="scan'208";a="552344265" Received: from ls.sc.intel.com (HELO localhost) ([143.183.96.54]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Mar 2022 11:50:15 -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 , Jim Mattson , erdemaktas@google.com, Connor Kuehl , Sean Christopherson Subject: [RFC PATCH v5 026/104] KVM: TDX: x86: Add vm ioctl to get TDX systemwide parameters Date: Fri, 4 Mar 2022 11:48:42 -0800 Message-Id: <5ff08ce32be458581afe59caa05d813d0e4a1ef0.1646422845.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=-2.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable 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: Isaku Yamahata Implement a VM-scoped subcomment to get system-wide parameters. Although this is system-wide parameters not per-VM, this subcomand is VM-scoped because - Device model needs TDX system-wide parameters after creating KVM VM. - This subcommands requires to initialize TDX module. For lazy initialization of the TDX module, vm-scope ioctl is better. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/include/uapi/asm/kvm.h | 22 ++++++++++++++ arch/x86/kvm/vmx/tdx.c | 41 +++++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 22 ++++++++++++++ 3 files changed, 85 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 2ad61caf4e0b..70f9be4ea575 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -530,6 +530,8 @@ struct kvm_pmu_event_filter { /* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id { + KVM_TDX_CAPABILITIES = 0, + KVM_TDX_CMD_NR_MAX, }; @@ -539,4 +541,24 @@ struct kvm_tdx_cmd { __u64 data; }; +struct kvm_tdx_cpuid_config { + __u32 leaf; + __u32 sub_leaf; + __u32 eax; + __u32 ebx; + __u32 ecx; + __u32 edx; +}; + +struct kvm_tdx_capabilities { + __u64 attrs_fixed0; + __u64 attrs_fixed1; + __u64 xfam_fixed0; + __u64 xfam_fixed1; + + __u32 nr_cpuid_configs; + __u32 padding; + struct kvm_tdx_cpuid_config cpuid_configs[0]; +}; + #endif /* _ASM_X86_KVM_H */ diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c index 8c67444d052a..20b45bb0b032 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -349,6 +349,44 @@ int tdx_vm_init(struct kvm *kvm) return ret; } +static int tdx_capabilities(struct kvm *kvm, struct kvm_tdx_cmd *cmd) +{ + struct kvm_tdx_capabilities __user *user_caps; + struct kvm_tdx_capabilities caps; + + BUILD_BUG_ON(sizeof(struct kvm_tdx_cpuid_config) != + sizeof(struct tdx_cpuid_config)); + + WARN_ON(cmd->id != KVM_TDX_CAPABILITIES); + if (cmd->metadata) + return -EINVAL; + + user_caps = (void __user *)cmd->data; + if (copy_from_user(&caps, user_caps, sizeof(caps))) + return -EFAULT; + + if (caps.nr_cpuid_configs < tdx_caps.nr_cpuid_configs) + return -E2BIG; + + caps = (struct kvm_tdx_capabilities) { + .attrs_fixed0 = tdx_caps.attrs_fixed0, + .attrs_fixed1 = tdx_caps.attrs_fixed1, + .xfam_fixed0 = tdx_caps.xfam_fixed0, + .xfam_fixed1 = tdx_caps.xfam_fixed1, + .nr_cpuid_configs = tdx_caps.nr_cpuid_configs, + .padding = 0, + }; + + if (copy_to_user(user_caps, &caps, sizeof(caps))) + return -EFAULT; + if (copy_to_user(user_caps->cpuid_configs, &tdx_caps.cpuid_configs, + tdx_caps.nr_cpuid_configs * + sizeof(struct tdx_cpuid_config))) + return -EFAULT; + + return 0; +} + int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) { struct kvm_tdx_cmd tdx_cmd; @@ -360,6 +398,9 @@ int tdx_vm_ioctl(struct kvm *kvm, void __user *argp) mutex_lock(&kvm->lock); switch (tdx_cmd.id) { + case KVM_TDX_CAPABILITIES: + r = tdx_capabilities(kvm, &tdx_cmd); + break; default: r = -EINVAL; goto out; diff --git a/tools/arch/x86/include/uapi/asm/kvm.h b/tools/arch/x86/include/uapi/asm/kvm.h index 2ad61caf4e0b..70f9be4ea575 100644 --- a/tools/arch/x86/include/uapi/asm/kvm.h +++ b/tools/arch/x86/include/uapi/asm/kvm.h @@ -530,6 +530,8 @@ struct kvm_pmu_event_filter { /* Trust Domain eXtension sub-ioctl() commands. */ enum kvm_tdx_cmd_id { + KVM_TDX_CAPABILITIES = 0, + KVM_TDX_CMD_NR_MAX, }; @@ -539,4 +541,24 @@ struct kvm_tdx_cmd { __u64 data; }; +struct kvm_tdx_cpuid_config { + __u32 leaf; + __u32 sub_leaf; + __u32 eax; + __u32 ebx; + __u32 ecx; + __u32 edx; +}; + +struct kvm_tdx_capabilities { + __u64 attrs_fixed0; + __u64 attrs_fixed1; + __u64 xfam_fixed0; + __u64 xfam_fixed1; + + __u32 nr_cpuid_configs; + __u32 padding; + struct kvm_tdx_cpuid_config cpuid_configs[0]; +}; + #endif /* _ASM_X86_KVM_H */ -- 2.25.1