Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp246764rwb; Tue, 25 Jul 2023 15:17:45 -0700 (PDT) X-Google-Smtp-Source: APBJJlFy99vp46GnzFZC1fuLsXClm/IqqcJMH+hL3SfMP5sSlMuZ6NpiG1W0UEg4s3dSyd6cE6vc X-Received: by 2002:a17:906:4ca:b0:94e:4489:f24d with SMTP id g10-20020a17090604ca00b0094e4489f24dmr129173eja.61.1690323464750; Tue, 25 Jul 2023 15:17:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690323464; cv=none; d=google.com; s=arc-20160816; b=Fi24mZDfvecip25aTTSK8sUkjL6B8zM40+HJdhnXVL6+QimJFfpyuLEEtd5DOLfFo/ hb35FWudlplMAukNNq1JA4//w5mYnsAXnAm0PVJTzcwYW66tVRcIZ9NohsjlRKtKj+Se hdqwXWFVUwS2OHxBXlfAdr45hFJiXfKXWx27T79dFPkUh+nI3UHBLm9eBxozhzGz75sD mpN0R94xLXGwDjzXmDdpAdPJ1b8LS6HhJLF28iZ6H7uAicL5AAjFPRiTq8fgANndtYRD T2RQqxfU9UY5nJvFWtUxX9jAC1Z4Sn6buTGds/s81twwXra/OS7nxEeG1tzLa5E1gmTV 2xbQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=JRhczmdPScKo2vay9vryElFC45oKYBjwBdCY5Tc9Lgg=; fh=1/sgOwi5AuU8IZymeFKs1Rjnf4QELnrK+C7GntqU2hA=; b=No4TEVVVhnTMVn2gKcYCtGpmHwTOwvvifcY4r7lY/Sha6KAGm5A+cN1NIdC8UmPGz1 S/YHkbk6XKTMOlmRXABWMzUHJWyDPZs2TtZuYnRqc6B3ojckDBro5aMvqr5AUG9MqXUx vNSnUjrnw5faxiAhi13efsIqhbOeXQr4N18qWORvCEFlJj5h1A2FYdpkjJ05ArsVVS8g 9BDxubLOs62dDLRWv39g4pn47cZI0g59zZkbymqFhATBYNXYUGCa9G7z7HxDNqaY02KT gBanEckSuVPbcAwYO3eGLgl9Wt1erVjv7gh7Uti4u3J85Ao/PoexXhjmiqeGdlnIV9ma b84Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=EkGMrIp9; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bg13-20020a170906a04d00b00992d6fa3b33si8530355ejb.440.2023.07.25.15.17.19; Tue, 25 Jul 2023 15:17:44 -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=@google.com header.s=20221208 header.b=EkGMrIp9; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231201AbjGYWDR (ORCPT + 99 others); Tue, 25 Jul 2023 18:03:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231789AbjGYWCc (ORCPT ); Tue, 25 Jul 2023 18:02:32 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E8F76268E for ; Tue, 25 Jul 2023 15:02:05 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-563bcd2cb78so1654250a12.3 for ; Tue, 25 Jul 2023 15:02:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690322518; x=1690927318; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=JRhczmdPScKo2vay9vryElFC45oKYBjwBdCY5Tc9Lgg=; b=EkGMrIp9yZ6eriedZv3ryWGMSfT6CqICnwlCvDzV3GuWJ2Ao7iQH9kXVsSodlZrFX3 VlKVCEZPsXde+TGPcaA8r9eq8RjJmFYXNqAYjmcaxoUOAebhAZI/7Jn6IlUJmB/zVlG4 ebMQaLI95tIPAOhWmVfyJTi/5lh+HUuOX+SY/vVCdEogFH/DWKS7fNXPc8mjsnZ3xOR4 GtuVnNXag6j7gq4wfzCy7PJP+LlFaVlBNpcglp31gbkMPimCQi6+hlN4ym7+uP49SXqX I8EdycDBKkxX7GbuObwGVdLbT9ZS0bHgNJ7THWPi8jmCuD9ip4nnTAuwoDwpJxKs6bJe c83Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690322518; x=1690927318; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JRhczmdPScKo2vay9vryElFC45oKYBjwBdCY5Tc9Lgg=; b=FP4VZ627dLpInuuU0ouqiG2fWwzS8KwIMIVtEmPt4XKTmuELN1bztFzKwC8h8Jhx09 pUw/ilI8yPvaSUHeY/Z0Dt4IKb+FPAjOI9AuxxRGugK7kwk1ziX+JCAlCE4eBXIOqDOf JAoB1eTR7MRHFKhw0S2cWaWAi/kKzEVe++HfueWuuGSmRAHeLObow6Q21PV2YHlzt6XQ B30ewZ6aJ/RWxb6auCw6HWH4GEtPZmmGpxLJA3l0IwDe/gRaL5881A5zZiIVd9jGmbu3 LFBDZ9UWA13/wZcDUMUGTqVqQqudEOQptPbT+uQWqZVPdJYY6fXff6yHIK4lRbKu/lWe wWyQ== X-Gm-Message-State: ABy/qLY3Dj1GSqJCs6Ur+XMorHjBr7/YZMt5sI9GO6aHmsE1e6RGpYnC zEUbWEmC5AwuojG2rndde+APkVdcWxnR X-Received: from afranji.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:47f1]) (user=afranji job=sendgmr) by 2002:a63:3344:0:b0:563:e1ff:aeb0 with SMTP id z65-20020a633344000000b00563e1ffaeb0mr1300pgz.3.1690322518353; Tue, 25 Jul 2023 15:01:58 -0700 (PDT) Date: Tue, 25 Jul 2023 22:00:59 +0000 In-Reply-To: <20230725220132.2310657-1-afranji@google.com> Mime-Version: 1.0 References: <20230725220132.2310657-1-afranji@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230725220132.2310657-7-afranji@google.com> Subject: [PATCH v4 06/28] KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration From: Ryan Afranji To: linux-kselftest@vger.kernel.org Cc: pbonzini@redhat.com, seanjc@google.com, isaku.yamahata@intel.com, sagis@google.com, erdemaktas@google.com, afranji@google.com, runanwang@google.com, shuah@kernel.org, drjones@redhat.com, maz@kernel.org, bgardon@google.com, jmattson@google.com, dmatlack@google.com, peterx@redhat.com, oupton@google.com, ricarkol@google.com, yang.zhong@intel.com, wei.w.wang@intel.com, xiaoyao.li@intel.com, pgonda@google.com, eesposit@redhat.com, borntraeger@de.ibm.com, eric.auger@redhat.com, wangyanan55@huawei.com, aaronlewis@google.com, vkuznets@redhat.com, pshier@google.com, axelrasmussen@google.com, zhenzhong.duan@intel.com, maciej.szmigiero@oracle.com, like.xu@linux.intel.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, ackerleytng@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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: Ackerley Tng This also exercises the KVM_TDX_CAPABILITIES ioctl. Suggested-by: Isaku Yamahata Signed-off-by: Ackerley Tng Change-Id: I967f7b613cd38469379fbbe297725d46b4e3ae4f Signed-off-by: Ryan Afranji --- .../selftests/kvm/lib/x86_64/tdx/tdx_util.c | 69 ++++++++++++++++++- 1 file changed, 66 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c index c90ad29f7733..95c6fb263583 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx/tdx_util.c @@ -27,10 +27,9 @@ static char *tdx_cmd_str[] = { }; #define TDX_MAX_CMD_STR (ARRAY_SIZE(tdx_cmd_str)) -static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) +static int _tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) { struct kvm_tdx_cmd tdx_cmd; - int r; TEST_ASSERT(ioctl_no < TDX_MAX_CMD_STR, "Unknown TDX CMD : %d\n", ioctl_no); @@ -40,11 +39,58 @@ static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) tdx_cmd.flags = flags; tdx_cmd.data = (uint64_t)data; - r = ioctl(fd, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); + return ioctl(fd, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); +} + +static void tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) +{ + int r; + + r = _tdx_ioctl(fd, ioctl_no, flags, data); TEST_ASSERT(r == 0, "%s failed: %d %d", tdx_cmd_str[ioctl_no], r, errno); } +static struct kvm_tdx_capabilities *tdx_read_capabilities(struct kvm_vm *vm) +{ + int i; + int rc = -1; + int nr_cpuid_configs = 4; + struct kvm_tdx_capabilities *tdx_cap = NULL; + + do { + nr_cpuid_configs *= 2; + + tdx_cap = realloc( + tdx_cap, sizeof(*tdx_cap) + + nr_cpuid_configs * sizeof(*tdx_cap->cpuid_configs)); + TEST_ASSERT(tdx_cap != NULL, + "Could not allocate memory for tdx capability nr_cpuid_configs %d\n", + nr_cpuid_configs); + + tdx_cap->nr_cpuid_configs = nr_cpuid_configs; + rc = _tdx_ioctl(vm->fd, KVM_TDX_CAPABILITIES, 0, tdx_cap); + } while (rc < 0 && errno == E2BIG); + + TEST_ASSERT(rc == 0, "KVM_TDX_CAPABILITIES failed: %d %d", + rc, errno); + + pr_debug("tdx_cap: attrs: fixed0 0x%016llx fixed1 0x%016llx\n" + "tdx_cap: xfam fixed0 0x%016llx fixed1 0x%016llx\n", + tdx_cap->attrs_fixed0, tdx_cap->attrs_fixed1, + tdx_cap->xfam_fixed0, tdx_cap->xfam_fixed1); + + for (i = 0; i < tdx_cap->nr_cpuid_configs; i++) { + const struct kvm_tdx_cpuid_config *config = + &tdx_cap->cpuid_configs[i]; + pr_debug("cpuid config[%d]: leaf 0x%x sub_leaf 0x%x eax 0x%08x ebx 0x%08x ecx 0x%08x edx 0x%08x\n", + i, config->leaf, config->sub_leaf, + config->eax, config->ebx, config->ecx, config->edx); + } + + return tdx_cap; +} + #define XFEATURE_LBR 15 #define XFEATURE_CET_U 11 #define XFEATURE_CET_S 12 @@ -85,6 +131,21 @@ static void tdx_apply_cpuid_restrictions(struct kvm_cpuid2 *cpuid_data) } } +static void tdx_check_attributes(struct kvm_vm *vm, uint64_t attributes) +{ + struct kvm_tdx_capabilities *tdx_cap; + + tdx_cap = tdx_read_capabilities(vm); + + /* TDX spec: any bits 0 in attrs_fixed0 must be 0 in attributes */ + ASSERT_EQ(attributes & ~tdx_cap->attrs_fixed0, 0); + + /* TDX spec: any bits 1 in attrs_fixed1 must be 1 in attributes */ + ASSERT_EQ(attributes & tdx_cap->attrs_fixed1, tdx_cap->attrs_fixed1); + + free(tdx_cap); +} + static void tdx_td_init(struct kvm_vm *vm, uint64_t attributes) { const struct kvm_cpuid2 *cpuid; @@ -98,6 +159,8 @@ static void tdx_td_init(struct kvm_vm *vm, uint64_t attributes) memset(init_vm, 0, sizeof(*init_vm)); memcpy(&init_vm->cpuid, cpuid, kvm_cpuid2_size(cpuid->nent)); + tdx_check_attributes(vm, attributes); + init_vm->attributes = attributes; tdx_apply_cpuid_restrictions(&init_vm->cpuid); -- 2.41.0.487.g6d72f3e995-goog