From patchwork Tue Dec 12 20:46:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sagi Shahar X-Patchwork-Id: 177559 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:bcd1:0:b0:403:3b70:6f57 with SMTP id r17csp7993276vqy; Tue, 12 Dec 2023 12:47:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IEq/Fm5vmvLeOmHbbeS56Lu6mOTlenkQl3sYV0Ibgad+Np+hYA7+GdqDkHQDODrpNmO4MxE X-Received: by 2002:a05:6a20:3d81:b0:190:c314:35f8 with SMTP id s1-20020a056a203d8100b00190c31435f8mr3123256pzi.46.1702414049922; Tue, 12 Dec 2023 12:47:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702414049; cv=none; d=google.com; s=arc-20160816; b=K/p7Yb71EPb18U0aPx9x31mE9xF5zX2ayQhBiPfd/Zw9yefp4DV6jk3uIOPEj2xY5y 70EG3KqgcVrVooZB6THYpVICtIak8OP3v5lWS4Z9RpLokxNYo7PCNpDzSekYaxjdXvCv d3Gg4KOAqruQOhXbj1CbRRf+hYm/ftIkPbdwzydnZX6RI4LyAsnA2X4uqYjApqqbS0Gi CjbHXRz3ktaQYxzyROuWG3tVo+roSg21orRkSusawbgCCfuyqHtB5beH3kSjRdfFVGpH qyZRNV2n77TGaeAQ/yZD5rmjRTNuAwTrC67l+fkPXYEAkBESP2QImLDl5Vy+YPBvd3A3 /MkQ== 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=63FW+3XX5/e8zea7VCMPF1eNiMssQ00z9r2TQiyi8pA=; fh=Y5ua58y+ybdOPwRkMynUdu+Hv1LrozBumh8skcuOhBI=; b=Ho4XCUkYd2vvll1qXKBq9QvlKwt12NifUCa5UozSq1VAo9NmMjRIsn2GjQ5H6kI0vg cnN2qNlLKqE72EKAImxbWEvdQYgvhlZDnE5CqY+2quHAAkj/xcrpNsCSev3e3gE4ngwg 0yqdKPaYBeKUjpWFZqJhth9ajsTAyBfgnoRi82ZnYWhhCkFpFCh6NquM7Mrpm5NA2Ad3 b+5I2el6jZpzM2tFzmsx8hTdeZKb+YdTWhcrlzPuAz+TUJKkGLvPOAUih55FPUkD/RUR xCZgxJlW4UKyR+07AxFx++aFfsu2YUOhso5DnUAQ/F2a3eTlyB2IPBLAuBbJpv8/8W/S zLtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mTsUbYaw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id kk10-20020a17090b4a0a00b00286f189a050si8543727pjb.22.2023.12.12.12.47.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Dec 2023 12:47:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=mTsUbYaw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D2F4E8047061; Tue, 12 Dec 2023 12:47:28 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1377241AbjLLUrP (ORCPT + 99 others); Tue, 12 Dec 2023 15:47:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54632 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1377254AbjLLUrE (ORCPT ); Tue, 12 Dec 2023 15:47:04 -0500 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 179FBF2 for ; Tue, 12 Dec 2023 12:47:09 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id d9443c01a7336-1d09a64eaebso54431485ad.3 for ; Tue, 12 Dec 2023 12:47:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1702414028; x=1703018828; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=63FW+3XX5/e8zea7VCMPF1eNiMssQ00z9r2TQiyi8pA=; b=mTsUbYawaFu4uUJg03yc2JUrbrvQKJW6LxsvxthD45xTSefLhQWOZ1UxgTIpGAXlG3 GEMQ1AM38NOiiWZm6HIsTjovI16+xDY/WznpeoE7nZHifj/luV+t8mryxjNBdox4u/ad r8Yq3PK87taTRxvaWQxVpaCouM3H+lW9ZzGsuON6GoR9CZnilQkstLkgA4fCUULQJWyY GUwOiKBvEb1wU3OeHIJdqGkd+ITSOptTcASBf3mHbXyDgSnLxs28N7eemEzJRkoreLFB DuMP+hTEaJbQONDLv/Bo+T7rQwPOWM2sjnxnxq1JWsRiUTilpRPujNSMnv+Ki9aN4nmU FxNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702414028; x=1703018828; 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=63FW+3XX5/e8zea7VCMPF1eNiMssQ00z9r2TQiyi8pA=; b=b+wAZT21bIfwKOR2rL1d+aZt36U9VnlbaFMN988xnSHGJi+l2uIcbFsHOsO/NcYB3i 2gfunpL1ixetm+Z2c+t4qGdVQYmEr4YOM/+NOaYwb305x1fkD326WroGsEdiOCVV8Ugj /5roSRcUj+33/93Z+amQnFjSOdDurIeDFg5aELHJezfy0wOyRORaTZBo98NybiqyGF5R 8ZvQJJFa/gVU1PTF1Fptu5YEzl4RGCWbX7w0WrbgzjVw7kmPCrPvxTBgiVuO8mgRE16P iekAp7GMatxmTJrTNgdEugZrsz5oL5ezR5aliu6YrzHCEGz9FFbB+Y/sQ5JdRD7qNQTM gHFg== X-Gm-Message-State: AOJu0YzUvWpPwJofMJcLATYdIrBa6yhBSeMg8i1bGY1h2IUeeiMMhxpv JYxY9OUhDTpvpLjOu9kle2hKys8aag== X-Received: from sagi.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:241b]) (user=sagis job=sendgmr) by 2002:a17:902:e74e:b0:1d0:820a:cf0d with SMTP id p14-20020a170902e74e00b001d0820acf0dmr51718plf.6.1702414028261; Tue, 12 Dec 2023 12:47:08 -0800 (PST) Date: Tue, 12 Dec 2023 12:46:21 -0800 In-Reply-To: <20231212204647.2170650-1-sagis@google.com> Mime-Version: 1.0 References: <20231212204647.2170650-1-sagis@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20231212204647.2170650-7-sagis@google.com> Subject: [RFC PATCH v5 06/29] KVM: selftests: TDX: Use KVM_TDX_CAPABILITIES to validate TDs' attribute configuration From: Sagi Shahar To: linux-kselftest@vger.kernel.org, Ackerley Tng , Ryan Afranji , Erdem Aktas , Sagi Shahar , Isaku Yamahata Cc: Sean Christopherson , Paolo Bonzini , Shuah Khan , Peter Gonda , Haibo Xu , Chao Peng , Vishal Annapurve , Roger Wang , Vipin Sharma , jmattson@google.com, dmatlack@google.com, linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-mm@kvack.org 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_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL 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 X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 12 Dec 2023 12:47:29 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785110515081733113 X-GMAIL-MSGID: 1785110515081733113 From: Ackerley Tng This also exercises the KVM_TDX_CAPABILITIES ioctl. Suggested-by: Isaku Yamahata Signed-off-by: Ackerley Tng Signed-off-by: Ryan Afranji Signed-off-by: Sagi Shahar Reviewed-by: Binbin Wu --- .../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 9b69c733ce01..6b995c3f6153 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_MASK_CET (XFEATURE_MASK_CET_USER | XFEATURE_MASK_CET_KERNEL) static void tdx_apply_cpuid_restrictions(struct kvm_cpuid2 *cpuid_data) @@ -78,6 +124,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 */ + TEST_ASSERT_EQ(attributes & ~tdx_cap->attrs_fixed0, 0); + + /* TDX spec: any bits 1 in attrs_fixed1 must be 1 in attributes */ + TEST_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; @@ -91,6 +152,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);