Received: by 2002:a05:6358:45e:b0:b5:b6eb:e1f9 with SMTP id 30csp155057rwe; Wed, 31 Aug 2022 18:35:22 -0700 (PDT) X-Google-Smtp-Source: AA6agR5MWjC8ERzZD5l8lHgr6kLsPZTh0XDuFvNWoahIIoodjQLGPOGaxwKBSe0dGsqfRtT2CEMB X-Received: by 2002:a17:90b:3b4d:b0:1fe:4d47:76ed with SMTP id ot13-20020a17090b3b4d00b001fe4d4776edmr210763pjb.150.1661996122730; Wed, 31 Aug 2022 18:35:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1661996122; cv=none; d=google.com; s=arc-20160816; b=krkOw5+zcSIgw67B00g0+BcQ+N29k/PTF75OM2QM0jcSBkkpCOJrtEEvhylALN8CGI /r7w3qwL1i4pSOIcJREckBEp7Zm/02h5gN9IB55nnYx9iHt8IPn/YXchvhsRoME/uXLK sgBw8elgkpHtDYMYjypbAx6OU2XoajMsXVKJuV4ycEYPSZcJWaSQ+ZodlWfeRH6gPaFt 14TeLVYANjrd6g05vOZYGqcPq8I/rogfJbgsYxcBBR/xIdbHJL6ruIuSMxncOlsdVfYK n0wXRqDcPbVpEuso6YUd3k+ThN+2LD1mKjJyncNfXZWKpoyo5zMDfBS8+hB7vqdGMR69 RMbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=+0+HN/s3K0NpAKgv9NYnh6iIwfvsfyIVk74B+Fut/KA=; b=fKI38oeFT2TGBqJXqhjYU7kbgVybtfKkbPoCF4yqqeVo8OtrjrZfTAKyrwGhFU318K Mnvwq+7teppzmmoJ5YrYr4rXFK2IU5En/dZty3NkHasX61TwpWjv9gRqF29/2Ojsf8bK G8RG6Z98WutBPbyEx5MIbVmJFsRC/6O5VOT4fmbJjk9hFv334it5lh9mia+ytlLA3+/d yfo1oMZUfmldASpUrSzbtAaCvZVyPDXlLgz1kjXv1A2hhj1MuBAF0y9AMyQt9v+slyjE DFKQWAJAmQAj8Rvl2MYlFoSLS527uAX9EoSzkjcwA5LHm11yTJQ6X2HsgVpg9KcbXozl 2QGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=MkbNQwUF; 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=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g130-20020a636b88000000b0041b27847a0dsi5883921pgc.618.2022.08.31.18.35.12; Wed, 31 Aug 2022 18:35:22 -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=@gmail.com header.s=20210112 header.b=MkbNQwUF; 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=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231308AbiIABWu (ORCPT + 99 others); Wed, 31 Aug 2022 21:22:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232069AbiIABWs (ORCPT ); Wed, 31 Aug 2022 21:22:48 -0400 Received: from mail-pl1-x62f.google.com (mail-pl1-x62f.google.com [IPv6:2607:f8b0:4864:20::62f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5F875136B28; Wed, 31 Aug 2022 18:22:47 -0700 (PDT) Received: by mail-pl1-x62f.google.com with SMTP id m2so15719756pls.4; Wed, 31 Aug 2022 18:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc; bh=+0+HN/s3K0NpAKgv9NYnh6iIwfvsfyIVk74B+Fut/KA=; b=MkbNQwUFQQkP11Q9RtytsGbyDyXzFBP7LCHqeZAailwaE79eaqZ7E45hmx9+iqEROB 5wUqNnEIEgkJPcJHdITxhEnflFwaBCrWwvwxtP1P9cSqIU/px2OPPnKReJHTEp3GNTTi e82q7oBauXVi00CpeuiDPwHCVGy/Pbr9ZICorZ7kxUxTXcCItfDMH8YOpre8+dvAAQ7h uZq7r+HiHQmEPxFlBjZlD5VvFie6XBdWsjxaUTIQteDxJLmLOxbbuVgKDO/9UCmn7WNR g51pQI1ndppnYAVBrwhy4TmQXgAofi90zAoQebdsc2FfpmTmkjmcYYxhXIwWm4+ljobf BEYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=+0+HN/s3K0NpAKgv9NYnh6iIwfvsfyIVk74B+Fut/KA=; b=UebzIThmpuw22jq0dQxPGcgg7hzJC9OUG11PeVrNqe25t8pNarfdbT4alyMb8eVUkR YwMjycRYUWKalQG9dFsxxDrz6IAUeTNWr0EakZb4GdJ3JaXhrl9t9/liZ1sps/rrcNRB txY/GnnlHrtac6f48DBxw0vu+vS/m2Z0FhA1g2weww4AyREDt9uW2IsQx6hKuCuOcQKI CRmoP1D1EeE4oII6Ur5bbs7TmXM7tge4cZ4hnibD4ycG+nYlegZhQsmvfXLsAMuvzYmp v68w3Mn+kJDIp84MeofNrMdu84cpfZrG7wfX3R+Wj+MdGWqtZgIimP6hbQVAfUcZ/Y2G r8/A== X-Gm-Message-State: ACgBeo15dD6KiN8n4Atp87kqBB2Uqo91lcPCNnMeJ1uPs51dughsnIpR eqU/o3cka549HVxgmZaeMDo= X-Received: by 2002:a17:902:d4c4:b0:170:9fdb:4a2a with SMTP id o4-20020a170902d4c400b001709fdb4a2amr27456600plg.137.1661995366819; Wed, 31 Aug 2022 18:22:46 -0700 (PDT) Received: from localhost ([192.55.55.51]) by smtp.gmail.com with ESMTPSA id c9-20020a170902d48900b001755ac7dd0asm307368plg.290.2022.08.31.18.22.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 31 Aug 2022 18:22:46 -0700 (PDT) Date: Wed, 31 Aug 2022 18:22:45 -0700 From: Isaku Yamahata To: Sagi Shahar Cc: linux-kselftest@vger.kernel.org, Paolo Bonzini , Sean Christopherson , Isaku Yamahata , Erdem Aktas , Ryan Afranji , Roger Wang , Shuah Khan , Andrew Jones , Marc Zyngier , Ben Gardon , Jim Mattson , David Matlack , Peter Xu , Oliver Upton , Ricardo Koller , Yang Zhong , Wei Wang , Xiaoyao Li , Peter Gonda , Marc Orr , Emanuele Giuseppe Esposito , Christian Borntraeger , Eric Auger , Yanan Wang , Aaron Lewis , Vitaly Kuznetsov , Peter Shier , Axel Rasmussen , Zhenzhong Duan , "Maciej S . Szmigiero" , Like Xu , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, isaku.yamahata@gmail.com Subject: Re: [RFC PATCH v2 02/17] KVM: selftest: Add helper functions to create TDX VMs Message-ID: <20220901012245.GE2711697@ls.amr.corp.intel.com> References: <20220830222000.709028-1-sagis@google.com> <20220830222000.709028-3-sagis@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20220830222000.709028-3-sagis@google.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,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 On Tue, Aug 30, 2022 at 10:19:45PM +0000, Sagi Shahar wrote: ... > diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c b/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c > new file mode 100644 > index 000000000000..72bf2ff24a29 > --- /dev/null > +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c > @@ -0,0 +1,338 @@ > +// SPDX-License-Identifier: GPL-2.0 > +#include > +#include "asm/kvm.h" > +#include "tdx.h" > +#include > +#include > +#include "processor.h" > +#include > + > +char *tdx_cmd_str[] = { > + "KVM_TDX_CAPABILITIES", > + "KVM_TDX_INIT_VM", > + "KVM_TDX_INIT_VCPU", > + "KVM_TDX_INIT_MEM_REGION", > + "KVM_TDX_FINALIZE_VM" > +}; > + > +#define TDX_MAX_CMD_STR (ARRAY_SIZE(tdx_cmd_str)) > +#define EIGHT_INT3_INSTRUCTIONS 0xCCCCCCCCCCCCCCCC > + > +#define XFEATURE_LBR 15 > +#define XFEATURE_XTILECFG 17 > +#define XFEATURE_XTILEDATA 18 > +#define XFEATURE_MASK_LBR (1 << XFEATURE_LBR) > +#define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG) > +#define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA) > +#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) > + > + > +static void 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); > + > + memset(&tdx_cmd, 0x0, sizeof(tdx_cmd)); > + tdx_cmd.id = ioctl_no; > + tdx_cmd.flags = flags; > + tdx_cmd.data = (uint64_t)data; > + r = ioctl(fd, KVM_MEMORY_ENCRYPT_OP, &tdx_cmd); > + TEST_ASSERT(r == 0, "%s failed: %d %d", tdx_cmd_str[ioctl_no], r, > + errno); > +} > + > +static struct tdx_cpuid_data get_tdx_cpuid_data(struct kvm_vm *vm) > +{ > + static struct tdx_cpuid_data cpuid_data; > + int ret, i; > + > + if (cpuid_data.cpuid.nent) > + return cpuid_data; > + > + memset(&cpuid_data, 0, sizeof(cpuid_data)); > + cpuid_data.cpuid.nent = KVM_MAX_CPUID_ENTRIES; > + ret = ioctl(vm->kvm_fd, KVM_GET_SUPPORTED_CPUID, &cpuid_data); > + if (ret) { > + TEST_FAIL("KVM_GET_SUPPORTED_CPUID failed %d %d\n", > + ret, errno); > + cpuid_data.cpuid.nent = 0; > + return cpuid_data; > + } > + > + for (i = 0; i < KVM_MAX_CPUID_ENTRIES; i++) { > + struct kvm_cpuid_entry2 *e = &cpuid_data.entries[i]; > + > + /* TDX doesn't support LBR and AMX features yet. > + * Disable those bits from the XCR0 register. > + */ > + if (e->function == 0xd && (e->index == 0)) { > + e->eax &= ~XFEATURE_MASK_LBR; > + e->eax &= ~XFEATURE_MASK_XTILE; > + } > + } > + > + return cpuid_data; > +} CET also needs adjust. How about the followings? diff --git a/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c b/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c index 1c3e47006cd2..123db9b76f82 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c +++ b/tools/testing/selftests/kvm/lib/x86_64/tdx_lib.c @@ -25,7 +25,7 @@ char *tdx_cmd_str[] = { #define XFEATURE_MASK_XTILECFG (1 << XFEATURE_XTILECFG) #define XFEATURE_MASK_XTILEDATA (1 << XFEATURE_XTILEDATA) #define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILECFG | XFEATURE_MASK_XTILEDATA) - +#define XFEATURE_MASK_CET ((1 << 11) | (1 << 12)) static int __tdx_ioctl(int fd, int ioctl_no, uint32_t flags, void *data) { @@ -72,12 +72,26 @@ static struct tdx_cpuid_data get_tdx_cpuid_data(struct kvm_vm *vm) for (i = 0; i < KVM_MAX_CPUID_ENTRIES; i++) { struct kvm_cpuid_entry2 *e = &cpuid_data.entries[i]; - /* TDX doesn't support LBR and AMX features yet. + /* TDX doesn't support LBR yet. * Disable those bits from the XCR0 register. */ if (e->function == 0xd && (e->index == 0)) { e->eax &= ~XFEATURE_MASK_LBR; - e->eax &= ~XFEATURE_MASK_XTILE; + + /* + * TDX modules requires both CET_{U, S} to be set even + * if only one is supported. + */ + if (e->eax & XFEATURE_MASK_CET) { + e->eax |= XFEATURE_MASK_CET; + } + /* + * TDX module requires both XTILE_{CFG, DATA} to be set. + * Both bits are required for AMX to be functional. + */ + if ((e->eax & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE) { + e->eax &= ~XFEATURE_MASK_XTILE; + } } } -- Isaku Yamahata