Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2682000rwd; Sun, 28 May 2023 22:06:34 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5mLgwrjt7A5IrOHmA4DjHNOeTZvwh/iJgCUfbMV0XUKrgWurU4jBEEBUWGmfw/iymVdanH X-Received: by 2002:a05:6a20:7fa4:b0:104:2b4e:412d with SMTP id d36-20020a056a207fa400b001042b4e412dmr8759298pzj.6.1685336794025; Sun, 28 May 2023 22:06:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685336794; cv=none; d=google.com; s=arc-20160816; b=zK2Pw93t6X5uRs+gmBgIKtYVaYy90IV8x1OSk0YoBy0nliyZJYd+/uwVzFkty2lg1z ZtSWHtSqMBahf6LYqrHQZX7aZvBs4RlPcReKfbNvqh+Wx0Jduh51coVKB7K/32tQnB7B 5uuB5dorWPDf68oCvINNIqyE8nRIidQUrIDmGZNlZbfnyqT+rawYDYKpqgLoVP5fliUM 1QydATkS5SDdUI9cj3dRvA5QHkuTwQKgGGcPlb+YvOS4mmZq12HL63lsSU9X1Avd7SLj 5RJG9JVW/0ZTQKBhcLhQdO5APFs/AJ8FCPOHsVvTmWGpsLMd+vBmS/c+IR/QVP18/1Ll UoMQ== 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=loS5tgB9Cb71J1bGZWFyIrmRJ7G/SIGrSsJ/lHnMQvQ=; b=N9GW+r8SsvpX4non5t6ZHADxAKb6FFtyOF7p5Eps7j5gquSDekIKPwqmkf7toeH6ch 7MxVM/qzIuXUzdoOHsuafGBkzB//1Na21Z8O0KNptgzukC+o1cFiGxl9ah22JYTwCQh9 DQgq0kI9rO7GqEoaxWU1rEAAJhfpBsYOqRwXISBE0TDmwOdNDsikyh7PiYph9qqOdnUq Pmbys8ZDqkFzxKYHmM2GbJ+AoPp53xwlKVKITnylc64ZYuN5ybOjgnA07MLsdFoZOpje NfjF0Jzp1H8KQFnJ7iEWTzAUjrJInckCGAB6pdsmXPeEHlzsvTFUwJxwrXUYo0j+bnz3 7WOg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=ZBrDFFEn; 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 i184-20020a6387c1000000b00513af1fa0b5si8450781pge.797.2023.05.28.22.06.21; Sun, 28 May 2023 22:06:33 -0700 (PDT) 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=ZBrDFFEn; 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 S231674AbjE2EWW (ORCPT + 99 others); Mon, 29 May 2023 00:22:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231455AbjE2EU4 (ORCPT ); Mon, 29 May 2023 00:20:56 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DA426C2; Sun, 28 May 2023 21:20:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1685334054; x=1716870054; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Hn7shvW1d+qYgrt4lzN8XBeqmtZHjhyPvGqncbK5er8=; b=ZBrDFFEnQ6nv4s7kaUPO5f71Xli+aMizRAgZmnAxJzEpgUkgzZR7juOo foDTpOnUVht2sE3W/n2pS+o9JUjNUBnFRimbH5c1Y42n3cXjrPOOR7dkl 6qfVVk0WW2PEvuSOXatxRFdwzHZT3nBOb1GgXi30FDE9x8PIkDdrF1nZ1 gR4B6JHlyidd42yZI7BNgx1wQvCjdXpl6jglPJoV0tdieqLWwZ9p7JC/B mbYYUV7YvU+pAQdjG6e/8377o2iyZVCpoKAlCDVnzlQSIOJYMJsS8iOAl 6DzzqlyJQWPKJNkZ8WBGzFXK25xKazrhW2uv2S1lumI5XEKDNHz4N8svq Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="418094322" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="418094322" Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:20:53 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10724"; a="683419337" X-IronPort-AV: E=Sophos;i="6.00,200,1681196400"; d="scan'208";a="683419337" Received: from ls.sc.intel.com (HELO localhost) ([172.25.112.31]) by orsmga006-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 May 2023 21:20:52 -0700 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 , Kai Huang , Zhi Wang , chen.bo@intel.com, Sean Christopherson Subject: [PATCH v14 017/113] KVM: TDX: x86: Add ioctl to get TDX systemwide parameters Date: Sun, 28 May 2023 21:18:59 -0700 Message-Id: <49cdbc21f2cc74faa8fa365ba5091f34c86fec76.1685333727.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.6 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,T_SCC_BODY_TEXT_LINE 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 Implement an ioctl to get system-wide parameters for TDX. Although the function is systemwide, vm scoped mem_enc ioctl works for userspace VMM like qemu and device scoped version is not define, re-use vm scoped mem_enc. Signed-off-by: Sean Christopherson Signed-off-by: Isaku Yamahata --- arch/x86/include/uapi/asm/kvm.h | 20 +++++++++++ arch/x86/kvm/vmx/tdx.c | 45 +++++++++++++++++++++++++ tools/arch/x86/include/uapi/asm/kvm.h | 48 +++++++++++++++++++++++++++ 3 files changed, 113 insertions(+) diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 4eb345c3e5ce..bf522e8e461b 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -593,4 +593,24 @@ struct kvm_tdx_cmd { __u64 unused; }; +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 54e5422ea73a..cf320459574f 100644 --- a/arch/x86/kvm/vmx/tdx.c +++ b/arch/x86/kvm/vmx/tdx.c @@ -16,6 +16,48 @@ offsetof(struct tdsysinfo_struct, cpuid_configs)) \ / sizeof(struct tdx_cpuid_config)) +static int tdx_get_capabilities(struct kvm_tdx_cmd *cmd) +{ + struct kvm_tdx_capabilities __user *user_caps; + const struct tdsysinfo_struct *tdsysinfo; + struct kvm_tdx_capabilities caps; + + BUILD_BUG_ON(sizeof(struct kvm_tdx_cpuid_config) != + sizeof(struct tdx_cpuid_config)); + + if (cmd->flags) + return -EINVAL; + + tdsysinfo = tdx_get_sysinfo(); + if (!tdsysinfo) + return -ENOTSUPP; + + user_caps = (void __user *)cmd->data; + if (copy_from_user(&caps, user_caps, sizeof(caps))) + return -EFAULT; + + if (caps.nr_cpuid_configs < tdsysinfo->num_cpuid_config) + return -E2BIG; + + caps = (struct kvm_tdx_capabilities) { + .attrs_fixed0 = tdsysinfo->attributes_fixed0, + .attrs_fixed1 = tdsysinfo->attributes_fixed1, + .xfam_fixed0 = tdsysinfo->xfam_fixed0, + .xfam_fixed1 = tdsysinfo->xfam_fixed1, + .nr_cpuid_configs = tdsysinfo->num_cpuid_config, + .padding = 0, + }; + + if (copy_to_user(user_caps, &caps, sizeof(caps))) + return -EFAULT; + if (copy_to_user(user_caps->cpuid_configs, &tdsysinfo->cpuid_configs, + tdsysinfo->num_cpuid_config * + 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; @@ -29,6 +71,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_get_capabilities(&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 3e92f1057b51..bf522e8e461b 100644 --- a/tools/arch/x86/include/uapi/asm/kvm.h +++ b/tools/arch/x86/include/uapi/asm/kvm.h @@ -565,4 +565,52 @@ struct kvm_pmu_event_filter { #define KVM_X86_DEFAULT_VM 0 #define KVM_X86_PROTECTED_VM 1 +/* Trust Domain eXtension sub-ioctl() commands. */ +enum kvm_tdx_cmd_id { + KVM_TDX_CAPABILITIES = 0, + + KVM_TDX_CMD_NR_MAX, +}; + +struct kvm_tdx_cmd { + /* enum kvm_tdx_cmd_id */ + __u32 id; + /* flags for sub-commend. If sub-command doesn't use this, set zero. */ + __u32 flags; + /* + * data for each sub-command. An immediate or a pointer to the actual + * data in process virtual address. If sub-command doesn't use it, + * set zero. + */ + __u64 data; + /* + * Auxiliary error code. The sub-command may return TDX SEAMCALL + * status code in addition to -Exxx. + * Defined for consistency with struct kvm_sev_cmd. + */ + __u64 error; + /* Reserved: Defined for consistency with struct kvm_sev_cmd. */ + __u64 unused; +}; + +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