From nobody Sat Feb 7 08:36:37 2026 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 3F702E80A8A for ; Thu, 28 Sep 2023 00:20:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230000AbjI1AUX (ORCPT ); Wed, 27 Sep 2023 20:20:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229956AbjI1AUM (ORCPT ); Wed, 27 Sep 2023 20:20:12 -0400 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 54D60126 for ; Wed, 27 Sep 2023 17:20:10 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-59c12d31d04so240600377b3.1 for ; Wed, 27 Sep 2023 17:20:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695860409; x=1696465209; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=apwOiyl24ZAzh9ksVSTWSskudG0FdMibqgJzxKGTzAQ=; b=Zze5OchnMhY/xpYjX8HB/FriVX6nu+cRHKXB4vZY7gxJR3iPIPmqih7gNJ4BXITsmA ceEJtB+pIiM2mmIU6ErKSr65I4vDLO1zY7uxe0vE9L2YMttnem4Ib3Jo3z2M7Ju+pPz8 YD6Zq2x56OMgXe/GKQfXOsNEFXWgMjVxZD5xz5DLZs7liJIKDUY8Uas7E8OguTdZRLLW SWptk3wTbS1K/mmQwdQb9iMOAytZ5Cnjak5CQu/CgZ9MDf0JiO+I+zrMCYBYyucb1XLM vA+715O0al/OePUImkKVPLPJULnRZj1WbmYUuiPAU88J8LG9CvfXiJ53yrymyuObbysT BAQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695860409; x=1696465209; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=apwOiyl24ZAzh9ksVSTWSskudG0FdMibqgJzxKGTzAQ=; b=LKxsAUWcRxnZ1VGLiVuo+IIl+5H3t+W7YnQEXRHc+qRngEtWWoRpduO7IwNJ1Evl74 Rrk63k1Ec2059/I+iIuFZZkW7P3i6VlgL/oTSsao+Ph3LRMqL64Jecp5Kcb6IsWXs69S leuziFfWTimADWn708fUwHdwQ/ba78IXIds7MQccEjVQUcXa1n9oCPAZ/zk389Q1QmZo 7cpy1Jje6cI54405MFTpg0kmDMDCtLohG5y/zdL4SvsTG7SzgEDnj6bqPVEaFkxY3zOi uoSIF5kdi0TpKIyst2IIBbg1swRvveSG9Cz4UdHu3xnEeNFmDKSMbnB0zz9I1QXEuUtc ZxJA== X-Gm-Message-State: AOJu0Yz+I+r+kE30cjM6Gm7BRf7pnj3Ok2V+i1G738hU7xetZUXrunzA GHCJHJrdk59wbCPSh3bdfEZ8y6Uf56g= X-Google-Smtp-Source: AGHT+IFjrvJ2npDBahfPzbZd3vwTKZ6Cm9AffU0wGAefuUEwJ5h8tV0U/szmpVbKu6qLaPMbte7BjNX86SI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a81:b65e:0:b0:586:a58d:2e24 with SMTP id h30-20020a81b65e000000b00586a58d2e24mr63304ywk.5.1695860409338; Wed, 27 Sep 2023 17:20:09 -0700 (PDT) Reply-To: Sean Christopherson Date: Wed, 27 Sep 2023 17:19:56 -0700 In-Reply-To: <20230928001956.924301-1-seanjc@google.com> Mime-Version: 1.0 References: <20230928001956.924301-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.582.g8ccd20d70d-goog Message-ID: <20230928001956.924301-6-seanjc@google.com> Subject: [PATCH 5/5] KVM: selftests: Force load all supported XSAVE state in state test From: Sean Christopherson To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, Sean Christopherson , Paolo Bonzini , Shuah Khan , Nathan Chancellor , Nick Desaulniers Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, llvm@lists.linux.dev, Tyler Stachecki , Leonardo Bras Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend x86's state to forcefully load *all* host-supported xfeatures by modifying xstate_bv in the saved state. Stuffing xstate_bv ensures that the selftest is verifying KVM's full ABI regardless of whether or not the guest code is successful in getting various xfeatures out of their INIT state, e.g. see the disaster that is/was MPX. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 9 +++++++++ tools/testing/selftests/kvm/x86_64/state_test.c | 14 ++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools= /testing/selftests/kvm/include/x86_64/processor.h index 6f66861175ad..25bc61dac5fb 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -922,6 +922,15 @@ static inline bool kvm_pmu_has(struct kvm_x86_pmu_feat= ure feature) !kvm_cpu_has(feature.anti_feature); } =20 +static __always_inline uint64_t kvm_cpu_supported_xcr0(void) +{ + if (!kvm_cpu_has_p(X86_PROPERTY_SUPPORTED_XCR0_LO)) + return 0; + + return kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_LO) | + ((uint64_t)kvm_cpu_property(X86_PROPERTY_SUPPORTED_XCR0_HI) << 32); +} + static inline size_t kvm_cpuid2_size(int nr_entries) { return sizeof(struct kvm_cpuid2) + diff --git a/tools/testing/selftests/kvm/x86_64/state_test.c b/tools/testin= g/selftests/kvm/x86_64/state_test.c index 115b2cdf9279..88b58aab7207 100644 --- a/tools/testing/selftests/kvm/x86_64/state_test.c +++ b/tools/testing/selftests/kvm/x86_64/state_test.c @@ -230,6 +230,7 @@ static void __attribute__((__flatten__)) guest_code(voi= d *arg) =20 int main(int argc, char *argv[]) { + uint64_t *xstate_bv, saved_xstate_bv; vm_vaddr_t nested_gva =3D 0; struct kvm_cpuid2 empty_cpuid =3D {}; struct kvm_regs regs1, regs2; @@ -294,12 +295,25 @@ int main(int argc, char *argv[]) * allow KVM_SET_XSAVE regardless of guest CPUID. Manually * load only XSAVE state, MSRs in particular have a much more * convoluted ABI. + * + * Load two versions of XSAVE state: one with the actual guest + * XSAVE state, and one with all supported features forced "on" + * in xstate_bv, e.g. to ensure that KVM allows loading all + * supported features, even if something goes awry in saving + * the original snapshot. */ + xstate_bv =3D (void *)&((uint8_t *)state->xsave->region)[512]; + saved_xstate_bv =3D *xstate_bv; + vcpuN =3D __vm_vcpu_add(vm, vcpu->id + 1); vcpu_xsave_set(vcpuN, state->xsave); + *xstate_bv =3D kvm_cpu_supported_xcr0(); + vcpu_xsave_set(vcpuN, state->xsave); =20 vcpu_init_cpuid(vcpuN, &empty_cpuid); vcpu_xsave_set(vcpuN, state->xsave); + *xstate_bv =3D saved_xstate_bv; + vcpu_xsave_set(vcpuN, state->xsave); =20 kvm_x86_state_cleanup(state); =20 --=20 2.42.0.582.g8ccd20d70d-goog