From patchwork Fri Feb 24 22:36:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151843 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 82115C64ED8 for ; Fri, 24 Feb 2023 22:37:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229379AbjBXWhJ (ORCPT ); Fri, 24 Feb 2023 17:37:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44128 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229656AbjBXWhH (ORCPT ); Fri, 24 Feb 2023 17:37:07 -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 9CCB56F424 for ; Fri, 24 Feb 2023 14:37:02 -0800 (PST) Received: by mail-pl1-x649.google.com with SMTP id k17-20020a170902d59100b0019abcf45d75so366836plh.8 for ; Fri, 24 Feb 2023 14:37:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=98oyGAb7RcHKqjAwNAgyr6FZh3Kgxofbnet0C96SQlo=; b=ayxF4y7TQAo+UGt7rUOON3JjQmIAFE3tu/dbrAubdUMOL4J4yq7C6I3nnT866uqY2S 60todAkVK8dh6tEm/ouX42F5P8iNnci0heFTv9Q2QCNNBNITFiEG7UQKDUCc+64ofXX+ 7cjGtB1Zoxct9+KUGoe3xeOl48wxhNQfeU+8aLKZOhyZTiYDPeDb+7nowADCgTQAJTdg jEmHgEIQTDvrbddlXX0rm7tSPCUNl0YeHpRlBlh/M33vIFGCDNFZq3o64h7ROrV2mcz+ LdBeK4pP69SnY6m1D7V+lKi6qIb0eWcH7aT8sE5GedZSKaFhhLdbTDh8HtGbTCEgc9YB QW4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=98oyGAb7RcHKqjAwNAgyr6FZh3Kgxofbnet0C96SQlo=; b=H9I+JkGp1px7bkrxxWpZ6APMGqXbKFilO/nCffBee6vD1T62Krs7Bs3pmI3u6r+UsS 9DE769tJtkJMeeg4IYcRGIXXu25yM7zljfY/VSrNAkxRG11sUato4ufLumgpJ0Eo/kzm v8SdDHLfP+Z075/fFcaUPaM9D8aD++YHJ1VUmgh8txv8WFUB8sz+x8p00Ltr9hFdsPVA D/fivMZlOjwrLdnljvSDjs3WZxrN7bZVh8hdbBcrObaUL/rlatgay4EHFRa/aoHXzFJf YxrCaKtA/py3f6M3RdtFAMe2krQd6cc2+FvV0ShiBSOs5Kq5wFcCyYUuFSoV8+e9qWek l+jA== X-Gm-Message-State: AO0yUKV/1Z8DeeV2ID/zKKY7bgTONYJGm7jciFUXj19O4qeWhUddRGb9 IGNF+RAlnRTu8FUe6NNL/q1F+hggH1+i4vnJd8RssfG6OpUX7CwwNNWqhItdjY1VeOM1SQ7K/LP rvWl26lmM/+Dxp5PJkAWvmMUWu3M8hYDCvmC5eNA9cyAvy5aW1Wymidrm+fbSFdCDhqJ0 X-Google-Smtp-Source: AK7set8fvWLgVkhrrdgS/Nk2LrBhZ2ZOJqn3nvkF/akZTEMWMmIHvgy6dNDJm0TRJKYpuZkwc1Pkqy1D7IK8feXu X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:49ca:b0:231:1d90:7b1b with SMTP id l10-20020a17090a49ca00b002311d907b1bmr2302784pjm.2.1677278221945; Fri, 24 Feb 2023 14:37:01 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:00 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-2-aaronlewis@google.com> Subject: [PATCH v3 1/8] KVM: x86: Add kvm_permitted_xcr0() From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add the helper, kvm_permitted_xcr0(), to make it easier to filter the supported XCR0 before using it. No functional changes intended. Signed-off-by: Aaron Lewis Reviewed-by: Mingwei Zhang --- arch/x86/kvm/cpuid.c | 7 ++++++- arch/x86/kvm/cpuid.h | 1 + arch/x86/kvm/x86.c | 4 +--- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 8f8edeaf8177..e1165c196970 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -60,6 +60,11 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted) return ret; } +u64 kvm_permitted_xcr0(void) +{ + return kvm_caps.supported_xcr0 & xstate_get_guest_group_perm(); +} + /* * This one is tied to SSB in the user API, and not * visible in /proc/cpuinfo. @@ -981,7 +986,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) entry->eax = entry->ebx = entry->ecx = 0; break; case 0xd: { - u64 permitted_xcr0 = kvm_caps.supported_xcr0 & xstate_get_guest_group_perm(); + u64 permitted_xcr0 = kvm_permitted_xcr0(); u64 permitted_xss = kvm_caps.supported_xss; entry->eax &= permitted_xcr0; diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h index b1658c0de847..224c25e02748 100644 --- a/arch/x86/kvm/cpuid.h +++ b/arch/x86/kvm/cpuid.h @@ -33,6 +33,7 @@ bool kvm_cpuid(struct kvm_vcpu *vcpu, u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool exact_only); u32 xstate_required_size(u64 xstate_bv, bool compacted); +u64 kvm_permitted_xcr0(void); int cpuid_query_maxphyaddr(struct kvm_vcpu *vcpu); u64 kvm_vcpu_reserved_gpa_bits_raw(struct kvm_vcpu *vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index f706621c35b8..596b234fc100 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -4519,9 +4519,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r = 0; break; case KVM_CAP_XSAVE2: { - u64 guest_perm = xstate_get_guest_group_perm(); - - r = xstate_required_size(kvm_caps.supported_xcr0 & guest_perm, false); + r = xstate_required_size(kvm_permitted_xcr0(), false); if (r < sizeof(struct kvm_xsave)) r = sizeof(struct kvm_xsave); break; From patchwork Fri Feb 24 22:36:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151841 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1BBE9C64ED8 for ; Fri, 24 Feb 2023 22:37:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229615AbjBXWhH (ORCPT ); Fri, 24 Feb 2023 17:37:07 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229460AbjBXWhF (ORCPT ); Fri, 24 Feb 2023 17:37:05 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAE156F42B for ; Fri, 24 Feb 2023 14:37:04 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-536a5a0b6e3so12875697b3.10 for ; Fri, 24 Feb 2023 14:37:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=fAVyG2WHazY43z49qj9fYBID6cROjq1KBIUlLyemaNE=; b=WZgR4mXE0BrXLwYzScTMpcI68LCDELmNUm0RZqeySyrFKaTgc8pIoYDVjA1tMsCczb sU/JP46eDTM6iCN0BFIeyZMLSpQJjg2wSe9hCJoj83LnSJELwYCPaT9FX4tWYJg5YnKl hFS/16lzPOtvaCshS4BCr6UNnXHftDgAvHBM8gX8kn24kro1oyV4gHbE2w8XGbuI6hv/ K1tkNJSE3HtpEuQ201qR8kN50k0wb4men5lHF5Nh87SWNynQisuzilxiidbhJ2SeZJQT oRHTmTls2oPidpNL0qvzbmWqC1quJVJV5cjgx6md2olyi2hGq8OxrEbvKsLjUkRu+d5F Dghw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=fAVyG2WHazY43z49qj9fYBID6cROjq1KBIUlLyemaNE=; b=sniZOxmdAG9iULFWTMkyBsttUIgmGMsthNpV8AMy7w7w+vXNErdefbVA4didY9qWMl LcyZNEc6p6xDdSw0LPo7kLpoQeiXp9fwwGxOkjfmODTjF3L2mS0uSwPgAXThlbENETxy az4P91JBqF1E1GNXBzLkrtZHWCGJGX/IwMK+cFTiVOnQTS1OA+6fUMHsI21LF1BD6mBW Miaj5uLTTtyEN7vIKXdh6st3wQ1aPHa9xFCYWDF4cphyGUle9M5sR+CIuyR1+WiP1uvT YGDitBQydE/x0+Yp8H9RDBBLClbaLKJUqkeZSuGz6U+KcaEvfyd/wJmTMSE/8884GGo9 +R1w== X-Gm-Message-State: AO0yUKWs0s9w4zbM/p1/4OG6Z2V2zh4xvbtHxWd98qqEwYuHNH2pid9s WHZzwpXQm4HxIE3lZVjJvJMgD/WdzXbUFLYMSdEWqPPx/4JqDMX9hEGb08ckAexQpJE4VrZs1HS MGfq5MnwINfzu/4j+BT0lZVfVGDCMjPuQbZFyy4omSX66WBb+Oo2eYqim9O7UJCY+BYJe X-Google-Smtp-Source: AK7set/6uT7Zh24+KjEsJLN7KTypnShr9Z0qh+V8rz6MmQoaBSrqEb0zW1O+AlDRlesxbtZxqwg5W3WZrGYROxrg X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a5b:cc:0:b0:966:b0c5:d7c with SMTP id d12-20020a5b00cc000000b00966b0c50d7cmr4538548ybp.9.1677278223703; Fri, 24 Feb 2023 14:37:03 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:01 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-3-aaronlewis@google.com> Subject: [PATCH v3 2/8] KVM: x86: Clear all supported MPX xfeatures if they are not all set From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Be a good citizen and don't allow any of the supported MPX xfeatures[1] to be set if they can't all be set. That way userspace or a guest doesn't fail if it attempts to set them in XCR0. [1] CPUID.(EAX=0DH,ECX=0):EAX.BNDREGS[bit-3] CPUID.(EAX=0DH,ECX=0):EAX.BNDCSR[bit-4] Suggested-by: Jim Mattson Signed-off-by: Aaron Lewis Reviewed-by: Mingwei Zhang --- arch/x86/kvm/cpuid.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index e1165c196970..b2e7407cd114 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -60,9 +60,22 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted) return ret; } +static u64 sanitize_xcr0(u64 xcr0) +{ + u64 mask; + + mask = XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR; + if ((xcr0 & mask) != mask) + xcr0 &= ~mask; + + return xcr0; +} + u64 kvm_permitted_xcr0(void) { - return kvm_caps.supported_xcr0 & xstate_get_guest_group_perm(); + u64 permitted_xcr0 = kvm_caps.supported_xcr0 & xstate_get_guest_group_perm(); + + return sanitize_xcr0(permitted_xcr0); } /* From patchwork Fri Feb 24 22:36:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151844 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6A241C7EE2F for ; Fri, 24 Feb 2023 22:37:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229656AbjBXWhK (ORCPT ); Fri, 24 Feb 2023 17:37:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229563AbjBXWhH (ORCPT ); Fri, 24 Feb 2023 17:37:07 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DC4E41B2D3 for ; Fri, 24 Feb 2023 14:37:05 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id m9-20020a17090a7f8900b0023769205928so1859066pjl.6 for ; Fri, 24 Feb 2023 14:37:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=UK2Im1BEXAy6ijd16h4AX9V+lLFWq6BP9P20yLZbsYA=; b=Wlo7/9HLpKgw/3Sv1De24kjKRcv1up0BCe2hwc+CMEM0VdJuV3MSbMc5jPekFDjfxj JyWO3DUYJOrEPrl8i60Z8okvees7jFlhgdehTcf4gGZt8yUBdHBiPihD+J2K9qeZZeMT NVtQJ0ZGfM1gQZaynMBCAJxAHAvX7DZXju3u5ajh6AGNDcjFRZYU+ffxIqgxwckAGy+F qLUx7HXfI/RkZj8e2P+So1FhVxwd7F2GZJt5X/9PfTX992GJoFz+Z2665DY4ZdWT5HSq R9TZTUWTjacbD5MtHJqzb6lD46xoi6QCQlUfcVhuucnHEGOd4KXfcEf86kOzWZxx5PEO I5pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=UK2Im1BEXAy6ijd16h4AX9V+lLFWq6BP9P20yLZbsYA=; b=bbW5NoEiUVL6CsQtBEKIdWt9TldTKbEaARmdk6t69WRtXiX3sHLSUSwSi94LqbflXk OWH0iVuZDs1gpMwpHw5/skbCq6Pq4Fmx/kjXLmyEPgGWnxGArDVhalx5bJNZJLaImPn4 ztiIkDn09CnkWyyXPD4vB+Xew/loqH/LlE9rA9XgR0ROA7j2wz9dmIDD81PvqEEgINm2 LQW+lMEgHsc91LkRC62p0i0IwH/bEr2hb5HtpsqvIvWlWRKopqx3nID3pCojJ2cNJYkr kPETSJcyx2wsJvwrD5kcUuEjJaKYlq3GVP6wp0L+kjrrJqqO0GnulPXqnJhvC2644WBL ga5w== X-Gm-Message-State: AO0yUKXubeF2NHFjo8hCItoWI+hlhbv2FJuwyT6cyHpsSQYMX7sZaffL GLgjxjoIfFneKI4n2B8qv+VESrr2hw1O0nXkQItU8YUFv0ADKz0zvL69ANlSluhvd1yM7bWmeit bgilOFwAVZoI6LT5qxeOCAHD+/nfIUqNj5rMBq+OCyq+QfOPlxFouubU9yZcUzzuH6h9Z X-Google-Smtp-Source: AK7set9bXx49vs62zVddXNeWWmvc5NVRdHr8tmDVAsz42P6OZJ4nEN7bNBXowXqRZ+S6//CJUqNqZWbW1LckHTu2 X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:903:3302:b0:19b:20a5:c18a with SMTP id jk2-20020a170903330200b0019b20a5c18amr3545113plb.12.1677278225236; Fri, 24 Feb 2023 14:37:05 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:02 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-4-aaronlewis@google.com> Subject: [PATCH v3 3/8] KVM: x86: Clear all supported AVX-512 xfeatures if they are not all set From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Be a good citizen and don't allow any of the supported AVX-512 xfeatures[1] to be set if they can't all be set. That way userspace or a guest doesn't fail if it attempts to set them in XCR0. [1] CPUID.(EAX=0DH,ECX=0):EAX.OPMASK[bit-5] CPUID.(EAX=0DH,ECX=0):EAX.ZMM_Hi256[bit-6] CPUID.(EAX=0DH,ECX=0):EAX.ZMM_Hi16_ZMM[bit-7] Suggested-by: Jim Mattson Signed-off-by: Aaron Lewis --- arch/x86/kvm/cpuid.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index b2e7407cd114..76379a51a16d 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -68,6 +68,10 @@ static u64 sanitize_xcr0(u64 xcr0) if ((xcr0 & mask) != mask) xcr0 &= ~mask; + mask = XFEATURE_MASK_AVX512; + if ((xcr0 & mask) != mask) + xcr0 &= ~XFEATURE_MASK_AVX512; + return xcr0; } From patchwork Fri Feb 24 22:36:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151845 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77961C6FA8E for ; Fri, 24 Feb 2023 22:37:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229752AbjBXWhM (ORCPT ); Fri, 24 Feb 2023 17:37:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229729AbjBXWhI (ORCPT ); Fri, 24 Feb 2023 17:37:08 -0500 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 764336F42B for ; Fri, 24 Feb 2023 14:37:07 -0800 (PST) Received: by mail-pj1-x1049.google.com with SMTP id fa3-20020a17090af0c300b002377eefb6acso144533pjb.3 for ; Fri, 24 Feb 2023 14:37:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=G4UV7DWOQlYve4HyAdnVWF3PbtUTr/TT861HIpwxpQE=; b=jtmVotXbFj/LbxfLYiIM4VYmb+ZcqFLJ0yf3lwLBE0eOxtEauPeD3PXOr31zoAaYSU QufYDYAvmoHL+GE6QKkSQcPDHNB3EQxyc2RYh4wteVr0RsVov+uxosmKmaqXIEFfAKZZ VBXUVoatCSD8q//cRt4YxUPc/GeuRCCbzLq4gT4Ri+rn/Pn/1Wi4SnaRiuAqhOSrgUl6 QOHg9hcnsgtbjLSAqlV5EAD6mQi4ZqJqroi7zZQEIJ01ppF08Kxz7STXvbWGq7qR+xeL 9GHNNwyWfjQIG8wguAEB3Qx2IV/UHw6MP3fbCVBktEPYzlcV/yohQxVDioKPjgTYLDiP N+yA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=G4UV7DWOQlYve4HyAdnVWF3PbtUTr/TT861HIpwxpQE=; b=on8Ux6JVLD93VLIUwYJsjt50N+6nj8mTpNZtD3DLHjoLAyB6G2h/RpgM2+yVDx2UMY EDcyZ+Vi1iCurI8YpOdsS6cuYe1mY/aJ1OdJRcWWHKiMAFozhYl2INGt2igeBoLSkx7m 2YeVKMqIcoRbzltm+tGYon2K5q16mb43xjLQZYdk/tMb5mxJkKYnSfYYtsGziwbH/0HD 7UBsFqy02m39NkP0m1Hp4e1dr28HePnOwQrNHKF3Tws6X1SYoTrmanoqsISh9M2+OUoP bYnAdy+cAyyQOi5Q8JR/PvU6X8Oach4u4VSE7VWcagwWxiorHbojjkzA7hbM0R8Ryd4C 3fPg== X-Gm-Message-State: AO0yUKV4Tsz+qJcVKGpX5Mxa4z63oqIWCgxnvt8AgyLvE2eZq+NE3bgZ 6Ia8K18AZuK07LJa2Y49K0enOR5WvKI4ocQBV7CQK4gwS9jNds0dOGBjfoNOh/Vh1DeJgGoQXQg 60ocQRCiH1vxTSf34HZIgn4YR7mi/SQvDiwwhyIJCSY/vcBlSmJJVSiAYWH41S7HRpPNm X-Google-Smtp-Source: AK7set/HjDmLwnfI/pb2B6neofTLG2Tregi71la+5NyZ7IwVK0giDCjqjM86iUKt7V4r6+TtltlSlydjff6zRy9X X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:9c2:b0:233:caec:5710 with SMTP id 60-20020a17090a09c200b00233caec5710mr2203143pjo.3.1677278226813; Fri, 24 Feb 2023 14:37:06 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:03 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-5-aaronlewis@google.com> Subject: [PATCH v3 4/8] KVM: x86: Clear AVX-512 xfeatures if SSE or AVX is clear From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org A requirement for setting AVX-512 is to have both SSE and AVX enabled. Add these masks to ensure AVX-512 gets cleared if either SSE or AVX are clear. Signed-off-by: Aaron Lewis --- arch/x86/kvm/cpuid.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 76379a51a16d..1eff76f836a2 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -68,7 +68,7 @@ static u64 sanitize_xcr0(u64 xcr0) if ((xcr0 & mask) != mask) xcr0 &= ~mask; - mask = XFEATURE_MASK_AVX512; + mask = XFEATURE_MASK_AVX512 | XFEATURE_MASK_SSE | XFEATURE_MASK_YMM; if ((xcr0 & mask) != mask) xcr0 &= ~XFEATURE_MASK_AVX512; From patchwork Fri Feb 24 22:36:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151846 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99F81C64ED8 for ; Fri, 24 Feb 2023 22:37:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229757AbjBXWhN (ORCPT ); Fri, 24 Feb 2023 17:37:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229744AbjBXWhK (ORCPT ); Fri, 24 Feb 2023 17:37:10 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 715CE6F431 for ; Fri, 24 Feb 2023 14:37:09 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id h19-20020a63f913000000b00502f20ab936so193790pgi.20 for ; Fri, 24 Feb 2023 14:37:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=T1p1Y6h/HupBXyncuWcOBrNuyTtKtABtkd2Kj481URk=; b=eArPrQQmBXLUycZWg/jDRTdvHlTGr/jgz6tDYQiVVS5gG/GK2qBX6CSPxfZWqBiA21 JFUH8y1SprL5r+5Gb1aSnj3oFuzy4gdUX+KX2GLYftfShtUdyNLMfG+4/j7q4m4vvWdy 9B0E9IbnNPvpwjQpFKdv4NVm6aLrx6TDIKGVtufbn6sYSI1F6M4hOdH4b2fpIHnhGtD+ 8Z4qC7ekTqvqBBMhRGb/w4InCaYbqmdICljtBEc4ROUKy0cOfDiw+gs5JnqxY7JzL7M3 Y/at9y78ZWKJNi5Ny4xWsdbMIqGi4UR/9T1Vn+M2IUzv93z/S62xvOWPXnrd9DZKnHh2 nBkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=T1p1Y6h/HupBXyncuWcOBrNuyTtKtABtkd2Kj481URk=; b=3dNgDbMvGBRLl5N+XYoDBMDR8JolFlXkZMt0t4xheP8zARhPLCO41X5o+N7QBf/rLQ C5E14nYXmtVmprAqGdTrwFsDIADl3V2vbcN5KdxiO5N3Of2bIX/uMX1RSTBOsqstsIIs PN64qqXAj47n7cNMkbdCyB+nN9Nc/mdLJEtit/KKtcEyCN6lX261f1Dn3JS2B1yIUa7Y IgA1sFs0MHXrTmZkoMEhhGjo8vot1hhY6KZi8rsVThI4GtO8wO+ZIhsqXQILxdhVXkRG 7Zn9IzqwzZZ8S6Ko+KXjz/xmz+RKWTSrHWD0U5w7R8BdFionNCM9o4to3kgaYgJhycwf MmrQ== X-Gm-Message-State: AO0yUKU/EFla3/d+yDIgpCLozfdZ4UErSBWrEroCpn+VNE8cIBlLPskH EuIr5sx538fqYyEQwJwwIDMjoGsLGgGbsqKC8/hKi3X35gpMd2b+hFvBVMbx5YOFQDblLY/ze9L rLa6L2pprWzvUQhiuqc8dA3MVOv/vzORN2fdtKqkkGi/z3Z7mEi7cpZdaE5QZ+he64+AG X-Google-Smtp-Source: AK7set9V/hDb9K5AjlgiI9PN0jVBt5G+jniyPbHrkdQOsDX55jLJt1SNUC7MDXEMHDkDdngN/rVbxBKGNcP4R4bL X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:2946:b0:237:9ca5:4d5d with SMTP id x6-20020a17090a294600b002379ca54d5dmr1126291pjf.6.1677278228756; Fri, 24 Feb 2023 14:37:08 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:04 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-6-aaronlewis@google.com> Subject: [PATCH v3 5/8] KVM: x86: Clear all supported AMX xfeatures if they are not all set From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Be a good citizen and don't allow any of the supported AMX xfeatures[1] to be set if they can't all be set. That way userspace or a guest doesn't fail if it attempts to set them in XCR0. [1] CPUID.(EAX=0DH,ECX=0):EAX.XTILE_CFG[bit-17] CPUID.(EAX=0DH,ECX=0):EAX.XTILE_DATA[bit-18] Suggested-by: Sean Christopherson Signed-off-by: Aaron Lewis Reviewed-by: Mingwei Zhang --- arch/x86/kvm/cpuid.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 1eff76f836a2..ac0423508b28 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -72,6 +72,9 @@ static u64 sanitize_xcr0(u64 xcr0) if ((xcr0 & mask) != mask) xcr0 &= ~XFEATURE_MASK_AVX512; + if ((xcr0 & XFEATURE_MASK_XTILE) != XFEATURE_MASK_XTILE) + xcr0 &= ~XFEATURE_MASK_XTILE; + return xcr0; } From patchwork Fri Feb 24 22:36:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151848 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B03B4C7EE23 for ; Fri, 24 Feb 2023 22:37:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229749AbjBXWhQ (ORCPT ); Fri, 24 Feb 2023 17:37:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229744AbjBXWhO (ORCPT ); Fri, 24 Feb 2023 17:37:14 -0500 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D8EB6F431 for ; Fri, 24 Feb 2023 14:37:11 -0800 (PST) Received: by mail-pj1-x104a.google.com with SMTP id m18-20020a17090a7f9200b002375a3cbc9bso141912pjl.9 for ; Fri, 24 Feb 2023 14:37:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Gl/R2pHmzxEtw7c5AzqBzH8DjfqeBWUHfv5NABoXY6w=; b=Y8RkJ8NDJ2nOACFvJWBok7vgyzhvjkkZehsC1eC08ZR7gTAXKVqlU3cbZCjWYjZy/Y wZdG+RSlLRug1WLrkGmJoFW3r3qRs9gf8iwitXOisQSXVcQ3wLiB6w0L+g0u7dF0Q3jx 9U0RBVv81TWHOSdzKE+jVBj0U44xQCawGTvkZS+WhTkGvV9F1mo7GN1O1G79U1nRMUre riByX1CUy9IrphDavMpsjjXVyGdgp5p82QhN1rR0YFPqAc2AUPEZMjs8yLfpLkVV1FHq Wl4dwrf3tNRU4N0tmlPVSscR7YVPMkJF2f4KbdCsDxeNg0gPHNEmFyl3gcGeQQxYRXES 6mcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=Gl/R2pHmzxEtw7c5AzqBzH8DjfqeBWUHfv5NABoXY6w=; b=GOAAg52uIPuj1CupRh4ImfqUWRb4lH9XwmOPOagPu887MjCgXLrzEPse8ndiuPWE/L JpP6SHG0l/MJVnANgiMd3uT/AKmllXIewDrKNnGbHpwuSjwMs4qos2Qxe/nFn7t9nriz v5SVFBTIvs8jsOkKpgUooXEVBcfMYdlnpYBz1qRmbQ47RxVam5P3YAjSuXx6jEYYGtzY BszEpnfQXII01FUXUUGQsm3FUVDzxlogo/kwBqIizQViO/WxBZ2CWzOQO8yZfGBnDMQH 8CcD+z34Ad/Z42Vi61Kq+61ZciVnLETS8Xj2fBYP8TpEnb+P9MJlgu+SCECJxOQGntwi TECA== X-Gm-Message-State: AO0yUKWUO/abJFif+A6cTzr3VOR7/mZBK9L0pdjKhlefyTPn2Bhg8c7R R5vQOJbNHuWjzKww66GokAupdHGfCFhQ7n+MRhrMUKb5KD3iN5y+EPhNUA8fE4gBaGXKWXCnFSA piyr3WJxE0KdNJ1Umw6ufxz4E/9PJ2Z7Bd+jl3oEIpDNIMfF5nEOEJP+Qw5eA9UZgDB7s X-Google-Smtp-Source: AK7set8hyMqQkhkZ/tlRpbYjzSy6F4UE/hZMbvrDqKbGAIpNnD0MgRIHZ+8j9Z4X10DkMYaxC0dEC2K57Yn5QBmC X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:c7d6:b0:233:d64d:4c29 with SMTP id gf22-20020a17090ac7d600b00233d64d4c29mr2285961pjb.4.1677278230270; Fri, 24 Feb 2023 14:37:10 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:05 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-7-aaronlewis@google.com> Subject: [PATCH v3 6/8] KVM: selftests: Hoist XGETBV and XSETBV to make them more accessible From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org The instructions XGETBV and XSETBV are useful to other tests. Move them to processor.h to make them more broadly available. No functional change intended. Reviewed-by: Jim Mattson Signed-off-by: Aaron Lewis Reviewed-by: Mingwei Zhang --- .../selftests/kvm/include/x86_64/processor.h | 18 ++++++++++++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 24 +++---------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 53ffa43c90db..62dc54c8e0c4 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -496,6 +496,24 @@ static inline void set_cr4(uint64_t val) __asm__ __volatile__("mov %0, %%cr4" : : "r" (val) : "memory"); } +static inline u64 xgetbv(u32 index) +{ + u32 eax, edx; + + __asm__ __volatile__("xgetbv;" + : "=a" (eax), "=d" (edx) + : "c" (index)); + return eax | ((u64)edx << 32); +} + +static inline void xsetbv(u32 index, u64 value) +{ + u32 eax = value; + u32 edx = value >> 32; + + __asm__ __volatile__("xsetbv" :: "a" (eax), "d" (edx), "c" (index)); +} + static inline struct desc_ptr get_gdt(void) { struct desc_ptr gdt; diff --git a/tools/testing/selftests/kvm/x86_64/amx_test.c b/tools/testing/selftests/kvm/x86_64/amx_test.c index bd72c6eb3b67..4b733ad21831 100644 --- a/tools/testing/selftests/kvm/x86_64/amx_test.c +++ b/tools/testing/selftests/kvm/x86_64/amx_test.c @@ -68,24 +68,6 @@ struct xtile_info { static struct xtile_info xtile; -static inline u64 __xgetbv(u32 index) -{ - u32 eax, edx; - - asm volatile("xgetbv;" - : "=a" (eax), "=d" (edx) - : "c" (index)); - return eax + ((u64)edx << 32); -} - -static inline void __xsetbv(u32 index, u64 value) -{ - u32 eax = value; - u32 edx = value >> 32; - - asm volatile("xsetbv" :: "a" (eax), "d" (edx), "c" (index)); -} - static inline void __ldtilecfg(void *cfg) { asm volatile(".byte 0xc4,0xe2,0x78,0x49,0x00" @@ -121,7 +103,7 @@ static inline void check_cpuid_xsave(void) static bool check_xsave_supports_xtile(void) { - return __xgetbv(0) & XFEATURE_MASK_XTILE; + return xgetbv(0) & XFEATURE_MASK_XTILE; } static void check_xtile_info(void) @@ -177,9 +159,9 @@ static void init_regs(void) cr4 |= X86_CR4_OSXSAVE; set_cr4(cr4); - xcr0 = __xgetbv(0); + xcr0 = xgetbv(0); xcr0 |= XFEATURE_MASK_XTILE; - __xsetbv(0x0, xcr0); + xsetbv(0x0, xcr0); } static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, From patchwork Fri Feb 24 22:36:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151847 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45615C6FA8E for ; Fri, 24 Feb 2023 22:37:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229760AbjBXWhP (ORCPT ); Fri, 24 Feb 2023 17:37:15 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbjBXWhO (ORCPT ); Fri, 24 Feb 2023 17:37:14 -0500 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 06E226F81E for ; Fri, 24 Feb 2023 14:37:12 -0800 (PST) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-536cad819c7so12990717b3.6 for ; Fri, 24 Feb 2023 14:37:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=qL7Ja+TASVm6K1qCmtdmDAoojZSvdRk42Rau58IW4rM=; b=TERGtr1H3+7mQV/dLjQMGQuZeUczuxuFoQGy3OcPfWRutBwaX35vi+PEU9r86Wg71H eucyBe8upxo/gtB4kK6lHott/xp78/2SqSC7yqOBBq+5bte5KqYcZ4bhIkVYsheXSGCy 2FunVvw59QCydDG1IFoaadW4UwMnyRcVyNGXUbjsaL4Wx08h5plGJRbkYfIgu/hTmWzi 9mEMup4/UGX5ZqEjn49RaSI+cQWNxyUW0weFmvLVa5UATBHlkpiL7EbjQ7Py9P1h0kTX D64Aufs5qeG0FEUs/fC0lyTigTif4e3O6J8poLpMHapNjZoU9afkPW3XoEwPmPSoh91v MWdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=qL7Ja+TASVm6K1qCmtdmDAoojZSvdRk42Rau58IW4rM=; b=4CEi9Z4CRjPkKOHupyTM4P2Y/ywp3/JmS+lCxSLT9mLZaay7S/Qd1Mpj4MRpzmvYrW EzM4fADwKcVNgXJrjQ/rqdnsMpbcKJEUwTXBS8JUe+V7jTmpQWGhvv/3/LXvyXmkbhTe CJc+Luk4pzdCpWwMuRtIhUPT7K3AaorQTQZpSKCmz3SsggOWaa3Dwgk40+rPkOf6g3LG x/clZLktLgRpF3tP9/th6utyjo/N9qVhzNiQ27QoeUTQBb8kuf7m7Qk39UGpJ7FcLtPQ 8McLwG+W+2C8zU/xSDSHxbP/LUqe93LDK7LmBIt7m2z0r/RLdELdgM/PiW8DDvTNO2I5 0Njw== X-Gm-Message-State: AO0yUKUIwRvmNuAoZbDDL+qwlScednwua2gdUWMlJ6h8LJ3SNadWTcMJ UoQdxddsXKlGOe7sSXDw7vgFR8+7i1x6bWiAZDeWQ+4d2mgX/UwqPYgFO0Cg35KuiuUHBt2pFtR 0PcDHqGLn3eWpvsPk+EwhTT3ZvLAAWtgqqEVbLG5Iu7l+sJR9qGpCu+rFVM5LohYnS9s/ X-Google-Smtp-Source: AK7set9OfXRkGcmOUImLWCd8mmvTt+Yp9DLWug2gqb4nOMxfNg7QBMx7XK0pwJhugDE01g6MFniI8A1m595fX6tm X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a81:b705:0:b0:530:a340:bcd7 with SMTP id v5-20020a81b705000000b00530a340bcd7mr5513168ywh.8.1677278232044; Fri, 24 Feb 2023 14:37:12 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:06 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-8-aaronlewis@google.com> Subject: [PATCH v3 7/8] KVM: selftests: Add XFEATURE masks to common code From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add XFEATURE masks to processor.h to make them more broadly available in KVM selftests. Use the names from the kernel's fpu/types.h for consistency, i.e. rename XTILECFG and XTILEDATA to XTILE_CFG and XTILE_DATA respectively. Signed-off-by: Aaron Lewis --- .../selftests/kvm/include/x86_64/processor.h | 17 ++++++++++++++ tools/testing/selftests/kvm/x86_64/amx_test.c | 22 +++++++------------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 62dc54c8e0c4..ebe83cfe521c 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -48,6 +48,23 @@ extern bool host_cpu_is_amd; #define X86_CR4_SMAP (1ul << 21) #define X86_CR4_PKE (1ul << 22) +#define XFEATURE_MASK_FP BIT_ULL(0) +#define XFEATURE_MASK_SSE BIT_ULL(1) +#define XFEATURE_MASK_YMM BIT_ULL(2) +#define XFEATURE_MASK_BNDREGS BIT_ULL(3) +#define XFEATURE_MASK_BNDCSR BIT_ULL(4) +#define XFEATURE_MASK_OPMASK BIT_ULL(5) +#define XFEATURE_MASK_ZMM_Hi256 BIT_ULL(6) +#define XFEATURE_MASK_Hi16_ZMM BIT_ULL(7) +#define XFEATURE_MASK_XTILE_CFG BIT_ULL(17) +#define XFEATURE_MASK_XTILE_DATA BIT_ULL(18) + +#define XFEATURE_MASK_AVX512 (XFEATURE_MASK_OPMASK | \ + XFEATURE_MASK_ZMM_Hi256 | \ + XFEATURE_MASK_Hi16_ZMM) +#define XFEATURE_MASK_XTILE (XFEATURE_MASK_XTILE_DATA | \ + XFEATURE_MASK_XTILE_CFG) + /* Note, these are ordered alphabetically to match kvm_cpuid_entry2. Eww. */ enum cpuid_output_regs { KVM_CPUID_EAX, diff --git a/tools/testing/selftests/kvm/x86_64/amx_test.c b/tools/testing/selftests/kvm/x86_64/amx_test.c index 4b733ad21831..14a7656620d5 100644 --- a/tools/testing/selftests/kvm/x86_64/amx_test.c +++ b/tools/testing/selftests/kvm/x86_64/amx_test.c @@ -33,12 +33,6 @@ #define MAX_TILES 16 #define RESERVED_BYTES 14 -#define XFEATURE_XTILECFG 17 -#define XFEATURE_XTILEDATA 18 -#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 XSAVE_HDR_OFFSET 512 struct xsave_data { @@ -187,14 +181,14 @@ static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, __tilerelease(); GUEST_SYNC(5); /* bit 18 not in the XCOMP_BV after xsavec() */ - set_xstatebv(xsave_data, XFEATURE_MASK_XTILEDATA); - __xsavec(xsave_data, XFEATURE_MASK_XTILEDATA); - GUEST_ASSERT((get_xstatebv(xsave_data) & XFEATURE_MASK_XTILEDATA) == 0); + set_xstatebv(xsave_data, XFEATURE_MASK_XTILE_DATA); + __xsavec(xsave_data, XFEATURE_MASK_XTILE_DATA); + GUEST_ASSERT((get_xstatebv(xsave_data) & XFEATURE_MASK_XTILE_DATA) == 0); /* xfd=0x40000, disable amx tiledata */ - wrmsr(MSR_IA32_XFD, XFEATURE_MASK_XTILEDATA); + wrmsr(MSR_IA32_XFD, XFEATURE_MASK_XTILE_DATA); GUEST_SYNC(6); - GUEST_ASSERT(rdmsr(MSR_IA32_XFD) == XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(rdmsr(MSR_IA32_XFD) == XFEATURE_MASK_XTILE_DATA); set_tilecfg(amx_cfg); __ldtilecfg(amx_cfg); /* Trigger #NM exception */ @@ -206,11 +200,11 @@ static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, void guest_nm_handler(struct ex_regs *regs) { - /* Check if #NM is triggered by XFEATURE_MASK_XTILEDATA */ + /* Check if #NM is triggered by XFEATURE_MASK_XTILE_DATA */ GUEST_SYNC(7); - GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILE_DATA); GUEST_SYNC(8); - GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILEDATA); + GUEST_ASSERT(rdmsr(MSR_IA32_XFD_ERR) == XFEATURE_MASK_XTILE_DATA); /* Clear xfd_err */ wrmsr(MSR_IA32_XFD_ERR, 0); /* xfd=0, enable amx */ From patchwork Fri Feb 24 22:36:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Lewis X-Patchwork-Id: 13151849 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0866FC64ED8 for ; Fri, 24 Feb 2023 22:37:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229768AbjBXWhT (ORCPT ); Fri, 24 Feb 2023 17:37:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44576 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229750AbjBXWhR (ORCPT ); Fri, 24 Feb 2023 17:37:17 -0500 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 600D16F424 for ; Fri, 24 Feb 2023 14:37:14 -0800 (PST) Received: by mail-pf1-x44a.google.com with SMTP id w130-20020a628288000000b005d1f4325e2aso212660pfd.18 for ; Fri, 24 Feb 2023 14:37:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=xLpEAeFTPW1MFSWD7xv6rlX3YaUbF9PKs2AOEdTxWsw=; b=SFw6iz617GhWiNMkRf84ki28hexiMmRsDaKqbzVPlBQP4zjCcEb06c88ty3riEhjn0 f/6FB7XUkDmzqB6P2IhCExdwVEvCp3y3qaKLg3PEMPg3O8uA6Lzap5TuK8auUhyGZLP7 yMOXp+ZtgEOnhbC3k6QP74HMZv3V4OuAJw7u/f2whGjGOp45qEzwfNp8D4waFfoqDLZt puRHy8jP+n1WdQexGdMol21S/9VyzfvQeIMUOg3VhR5gSNUL4PoTBFTvz1Ius1epZ+r7 Xq7X4Uo1l3XMmLLf+RhAKe/2OZLI033l/uBpzjg/fv7+T9k3XAkukaH7KwQOFPGOrHHc 8DQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=xLpEAeFTPW1MFSWD7xv6rlX3YaUbF9PKs2AOEdTxWsw=; b=72Q8Vqc/nWM9B0fU9Ywhsh3LyqiRx6QTYkY1ZfmvGg9lT0Cz/ya9Kx8EpEIZsDdkV1 dIhEdUN7RsWht2Wgi/z9b5dVqJV9cZ4FVRGqz2Jr/LTJguzoFODh16ZI9sfTnD5Y0fj9 sr7VIKn2/vrdhANzqt+DuHcLXNtWAsW50Zr2jk+PIAdpqrGR6XwwM6vIiHTDBjSyZqQ8 byvBz59LqOgK17sboqb1+pX+HC/7tE2cQYjMRzYx33Td4cY4rsAROSH+z7p3FCRSGkTK cvtI6pjMXly4KGaVD+Ke72AaLAKacyCfndWI+vfO9alzoyQTsZC8c2Z+HlaYjRBY5aDB augA== X-Gm-Message-State: AO0yUKWXmr1V5Rjnl0XivUI0KpoUJ3rymt9f2e/cnKjr8r9wwO92XHj2 DQvUg08RUaZuJtC+txTsB67Kimngeq3XKGrpYUoewuNw2On7q9meZ9DNY2X/INydVCcRkPWAza+ 32nPh4d40wELAIn8NvWGt0bsFMkSBkIge8JEw0AmYmFjuXJifK9kbt9T/4OWio/skwxQ3 X-Google-Smtp-Source: AK7set8TzTBS/L2mHE+tmM5wZ0n7eRhVyXCFhkSLCK5B98Dyz9YuxYQjgD2cF0UBnPXjFNtt6poqx+nP2rngGKqe X-Received: from aaronlewis-2.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:519c]) (user=aaronlewis job=sendgmr) by 2002:a17:90a:17ec:b0:237:30ef:e252 with SMTP id q99-20020a17090a17ec00b0023730efe252mr2220850pja.9.1677278233766; Fri, 24 Feb 2023 14:37:13 -0800 (PST) Date: Fri, 24 Feb 2023 22:36:07 +0000 In-Reply-To: <20230224223607.1580880-1-aaronlewis@google.com> Mime-Version: 1.0 References: <20230224223607.1580880-1-aaronlewis@google.com> X-Mailer: git-send-email 2.39.2.637.g21b0678d19-goog Message-ID: <20230224223607.1580880-9-aaronlewis@google.com> Subject: [PATCH v3 8/8] KVM: selftests: Add XCR0 Test From: Aaron Lewis To: kvm@vger.kernel.org Cc: pbonzini@redhat.com, jmattson@google.com, seanjc@google.com, mizhang@google.com, Aaron Lewis Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Check both architectural rules and KVM's own software-defined rules to ensure the supported xfeatures[1] don't violate any of them. The architectural rules[2] and KVM's rules ensure for a given feature, e.g. sse, avx, amx, etc... their associated xfeatures are either all sets or none of them are set, and any dependencies are enabled if needed. [1] EDX:EAX of CPUID.(EAX=0DH,ECX=0) [2] SDM vol 1, 13.3 ENABLING THE XSAVE FEATURE SET AND XSAVE-ENABLED FEATURES Signed-off-by: Aaron Lewis Reviewed-by: Mingwei Zhang --- tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/processor.h | 17 +++ .../selftests/kvm/x86_64/xcr0_cpuid_test.c | 128 ++++++++++++++++++ 3 files changed, 146 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 84a627c43795..18cadc669798 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -105,6 +105,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/vmx_tsc_adjust_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_nested_tsc_scaling_test TEST_GEN_PROGS_x86_64 += x86_64/xapic_ipi_test TEST_GEN_PROGS_x86_64 += x86_64/xapic_state_test +TEST_GEN_PROGS_x86_64 += x86_64/xcr0_cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/xss_msr_test TEST_GEN_PROGS_x86_64 += x86_64/debug_regs TEST_GEN_PROGS_x86_64 += x86_64/tsc_msrs_test diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index ebe83cfe521c..380daa82b023 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -667,6 +667,15 @@ static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature) !this_cpu_has(feature.anti_feature); } +static __always_inline uint64_t this_cpu_supported_xcr0(void) +{ + uint32_t a, b, c, d; + + cpuid(0xd, &a, &b, &c, &d); + + return a | ((uint64_t)d << 32); +} + typedef u32 __attribute__((vector_size(16))) sse128_t; #define __sse128_u union { sse128_t vec; u64 as_u64[2]; u32 as_u32[4]; } #define sse128_lo(x) ({ __sse128_u t; t.vec = x; t.as_u64[0]; }) @@ -1090,6 +1099,14 @@ static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val) return kvm_asm_safe("wrmsr", "a"(val & -1u), "d"(val >> 32), "c"(msr)); } +static inline uint8_t xsetbv_safe(uint32_t index, uint64_t value) +{ + u32 eax = value; + u32 edx = value >> 32; + + return kvm_asm_safe("xsetbv", "a" (eax), "d" (edx), "c" (index)); +} + bool kvm_is_tdp_enabled(void); uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr, diff --git a/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c b/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c new file mode 100644 index 000000000000..7ca0dea3d144 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * XCR0 cpuid test + * + * Copyright (C) 2022, Google LLC. + */ + +#include +#include +#include +#include +#include + +#include "test_util.h" + +#include "kvm_util.h" +#include "processor.h" + +/* Architectural check. */ +#define ASSERT_XFEATURE_DEPENDENCIES(supported_xcr0, xfeatures, dependencies) \ +do { \ + uint64_t __supported = (supported_xcr0) & ((xfeatures) | (dependencies)); \ + \ + GUEST_ASSERT_3((__supported & (xfeatures)) != (xfeatures) || \ + __supported == ((xfeatures) | (dependencies)), \ + __supported, (xfeatures), (dependencies)); \ +} while (0) + +/* + * KVM's own software-defined rules. While not architectural it really + * ought to be true. + */ +#define ASSERT_ALL_OR_NONE_XFEATURE(supported_xcr0, xfeatures) \ +do { \ + uint64_t __supported = (supported_xcr0) & (xfeatures); \ + \ + GUEST_ASSERT_2(!__supported || __supported == (xfeatures), \ + __supported, (xfeatures)); \ +} while (0) + +static void guest_code(void) +{ + uint64_t xcr0_reset; + uint64_t supported_xcr0; + int i, vector; + + set_cr4(get_cr4() | X86_CR4_OSXSAVE); + + xcr0_reset = xgetbv(0); + supported_xcr0 = this_cpu_supported_xcr0(); + + GUEST_ASSERT(xcr0_reset == XFEATURE_MASK_FP); + + /* Check AVX */ + ASSERT_XFEATURE_DEPENDENCIES(supported_xcr0, + XFEATURE_MASK_YMM, + XFEATURE_MASK_SSE); + + /* Check MPX */ + ASSERT_ALL_OR_NONE_XFEATURE(supported_xcr0, + XFEATURE_MASK_BNDREGS | XFEATURE_MASK_BNDCSR); + + /* Check AVX-512 */ + ASSERT_XFEATURE_DEPENDENCIES(supported_xcr0, + XFEATURE_MASK_AVX512, + XFEATURE_MASK_SSE | XFEATURE_MASK_YMM); + ASSERT_ALL_OR_NONE_XFEATURE(supported_xcr0, + XFEATURE_MASK_AVX512); + + /* Check AMX */ + ASSERT_ALL_OR_NONE_XFEATURE(supported_xcr0, + XFEATURE_MASK_XTILE); + + vector = xsetbv_safe(0, supported_xcr0); + GUEST_ASSERT_2(!vector, supported_xcr0, vector); + + for (i = 0; i < 64; i++) { + if (supported_xcr0 & BIT_ULL(i)) + continue; + + vector = xsetbv_safe(0, supported_xcr0 | BIT_ULL(i)); + GUEST_ASSERT_3(vector == GP_VECTOR, supported_xcr0, vector, BIT_ULL(i)); + } + + GUEST_DONE(); +} + +int main(int argc, char *argv[]) +{ + struct kvm_vcpu *vcpu; + struct kvm_run *run; + struct kvm_vm *vm; + struct ucall uc; + + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_XSAVE)); + + /* Tell stdout not to buffer its content */ + setbuf(stdout, NULL); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + run = vcpu->run; + + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vcpu); + + while (1) { + vcpu_run(vcpu); + + TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, + "Unexpected exit reason: %u (%s),\n", + run->exit_reason, + exit_reason_str(run->exit_reason)); + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT_3(uc, "0x%lx 0x%lx 0x%lx"); + break; + case UCALL_DONE: + goto done; + default: + TEST_FAIL("Unknown ucall %lu", uc.cmd); + } + } + +done: + kvm_vm_free(vm); + return 0; +}