From patchwork Thu Oct 3 23:43:27 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821673 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8DC221D0B91 for ; Thu, 3 Oct 2024 23:43:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999024; cv=none; b=Y1UMgduWfI0Z/Mzev+Q/8zW06HufUYjmcYPJ+KMwI1De3vysxj2sRrz6/H+dkqHCP5d1wV6uA3AXrUusiTDJ2vzH72+I7XkVFrWs9vcY1tm11FB7hRa8BUeGp4HrzM9P/ZBAMQwFR+mldJNZo6wuKeka9bSVZT96dQbK706JI6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999024; c=relaxed/simple; bh=dCT8bXCAqzzFJVCOs7D2QLba2AuX/LxeOumRe2jwP14=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=cfNusvtWESDnNzpvUSAdvkkztqRB9EngwmnCqekd38SIu7cU/bNqWnqIQ1YcqzjnQbV04tHVs+cML8itdXsLSM2fiw9/A9BVDOUnrqg6atBXGmROJutZve1h3jgVX6OOmSgo9MWdUuWaTeBrED9Jn3M5P1lAYE0g6lgHZqbOnKU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=j/DfmBlF; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="j/DfmBlF" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-20b0c92182fso14251535ad.1 for ; Thu, 03 Oct 2024 16:43:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999023; x=1728603823; 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=6V2q4amWCHB6J/4atN9JkqFjKadmMrC6jiYsYmH9GeA=; b=j/DfmBlF0XueeTAUmTobLtoKu+JuPt3/lsv58+IYI2kNwnKRXj85Qmei653K0dtRNf colbLIEw0mms1FjK9K/MxrF8oeaHfmvxzUWqwNdJsJLyu0Poff5pgEy6RU5cp/LfKwbi qQqDqGIdRmLWlB3OmywPAvK1bYQ0ceKcbI+YwL7iXgrkUGfdps7nCQOcygJw17KNqHnm zytZ1H6lhiR4zd7F+Y0WJLorGOjAnX/YQ5ZurmyDC2swCdQAiK++iOuIA5I13ZoKi9Sc 3GiCpmN0aelB2i4OvVb3Gde2mKCsLejr9dYySL+cpe2OdOYs1sAtkJp4Pm7QHrjwirak BbfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999023; x=1728603823; 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=6V2q4amWCHB6J/4atN9JkqFjKadmMrC6jiYsYmH9GeA=; b=m0ziQMygk9hUL8Yewtw6+HVlnPIAAAYTMIxNQzXDLRiqSSJ8t6OHDfF6xfEzbySThF uUg9A/eWFxyk9N5/0piRtddm70EW6g17B9nTAsZBpE1fCs9ta7Qdt8UrdC2g5zl9VdBT zzJEhGCM6u3KivJOKRp5EcHHqhoyj11e5DYetwinnce3Z7RNx+6OvtQ4FMQXs2aiL3Sq 6hWkNHeDduvv2idMSD+oSdyRKcWECRIxhbWkMVLCbwSSSRIr9wiqQunX6Gr8U7YkBCRl 8PhQ/4YM3m18ftj8mWLghHX3/9veX/XAk1NyCqctu3qPzngHq145gTrmF7KsAm6xnNLd FPag== X-Gm-Message-State: AOJu0Yzc4rdnTDP7VCb8rhRUBbBmjV9iBPBlt1GCzf+71b54/VSx97Hz iPNTxRLLibZFLx8NFV8YF3FyspSEW2ClqR/dGzgpH2og+fTF7oE97RlR4OFn174k3+MqihcA74F 5VQ== X-Google-Smtp-Source: AGHT+IFaPFq1x4myrDRmgES6KGTmO/IX8Xo50bZDsQhg0x5tM5p7famkM2K8G8qgEWxTrXonLa0y6KUkJmc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:e80c:b0:20b:9df1:54a3 with SMTP id d9443c01a7336-20bff1dfc9fmr8035ad.8.1727999022648; Thu, 03 Oct 2024 16:43:42 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:27 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-2-seanjc@google.com> Subject: [PATCH 01/11] KVM: selftests: Fix out-of-bounds reads in CPUID test's array lookups From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov When looking for a "mangled", i.e. dynamic, CPUID entry, terminate the walk based on the number of array _entries_, not the size in bytes of the array. Iterating based on the total size of the array can result in false passes, e.g. if the random data beyond the array happens to match a CPUID entry's function and index. Fixes: fb18d053b7f8 ("selftest: kvm: x86: test KVM_GET_CPUID2 and guest visible CPUIDs against KVM_GET_SUPPORTED_CPUID") Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/cpuid_test.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/cpuid_test.c b/tools/testing/selftests/kvm/x86_64/cpuid_test.c index 8c579ce714e9..fec03b11b059 100644 --- a/tools/testing/selftests/kvm/x86_64/cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/cpuid_test.c @@ -60,7 +60,7 @@ static bool is_cpuid_mangled(const struct kvm_cpuid_entry2 *entrie) { int i; - for (i = 0; i < sizeof(mangled_cpuids); i++) { + for (i = 0; i < ARRAY_SIZE(mangled_cpuids); i++) { if (mangled_cpuids[i].function == entrie->function && mangled_cpuids[i].index == entrie->index) return true; From patchwork Thu Oct 3 23:43:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821674 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6CD6C1D0E38 for ; Thu, 3 Oct 2024 23:43:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999027; cv=none; b=ErxIEC10q4QYcbLhtmOFEGeA1qeWpjMxu2jaHetH2q6jYhtHk2bemYMvLPelI3SSTnn95bPYvZwF99x8i2HVMjzdSwCNfbL3DiamR4VBb0iD98pkQ2WAlgbC0aFcNzc4+CgLEetI+n5Ej9Qxbx3g8mARjHrygQjpA7i4Kb1ANhs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999027; c=relaxed/simple; bh=WciSfpUtADL53emwfW+NrL/3Wm17IVfAZmunZlqHwnU=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aq3Aro7DraAlv/pAAQqnc9qL3VKVPGq22/AlpmzHIE/m2FUotO5VoNBp/2xHa2wKCIhuKImFKLn2PjQvzQnzuUBdz1ZZKrFPL4bEhp0KVsEcaDjycvTt+qVps5E9SYg3KdixhRRSaVbVgC2XxOCDMRZaKHHq0hdkStsgHKw91NE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=u5hU6+iO; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="u5hU6+iO" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e29b4f8837so20306307b3.0 for ; Thu, 03 Oct 2024 16:43:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999024; x=1728603824; 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=KgYrDB8FMNlYemVzZTKJ7rr9scMDlTxRH27Xvf6zHWk=; b=u5hU6+iObg+a+jfwQtRHfoaLs2uU4Y0FgzRGObj4WiEebRvq9D5nJy1Xj5VcUjcBaQ GooYy5lJqlVUqdGKdGz9hdr78/096iRaMMkQRO0GNuybGRpPRGsbWeQS9IiXiIhUgtGH M30XP2k1n0dz8qhlt1CLam2X6sQR8JjP+VWlxMS09tr9IuP9dvFF7u3Da7IvOKv0FFph HyALqIiqVBuIhrJfrpHVVw3jJhTIKt2VS9CcRED08M1zUISLqG8Wbbk6IzAnkBlyF5T7 Ym1q3UAzJ0BS+GB4JF2EyqhDtvQpsZ1bI3vFberJ4QFc2fgu/VpyjiDaoLNk3F9ukkg5 LoxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999024; x=1728603824; 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=KgYrDB8FMNlYemVzZTKJ7rr9scMDlTxRH27Xvf6zHWk=; b=PH6WFwozwqSs0wqi1d0tC5ZRjmBS/QxJFvXusJhUTMS/1T5DHUVk6vO04et5dhr2Ej R9WHqMD53Q1b5KtI/bhqCsSqgGdD36+tPc+YzKzlX9C9CkMjnKUggLG1obOFx1lmLHAT EM9WId+n5lWZDsh6wmq0toxIGW3ETDG1cMG79vpWFXILJXhgkjh0F1v7ROJwHFNKS2T6 lY8NSzlxEbUlfwb4o3hlzle647tEzsJAp6jnplQoWXo9K9HcS6USkSVYbbIKTaomAfTn h3xEIQuTnOnA7OKqjpAKgG37bz2h48qC3zPNSiuVx3bS3TR9rXqm04O7m3LyOZByx24m vJNQ== X-Gm-Message-State: AOJu0YyDVNfWhmCpCaKYGtFw6SAucFUNhqfzpnQgIrlqE8Am3vmktP2q esSbJAS6PNNQRXbs7iCCnUJ/WdHiQ0IqUZOIhGBRz5Ri0X9CSTdbJNcntJ3cwTgxrbpaQs/BtkY KEw== X-Google-Smtp-Source: AGHT+IHaEIz62SUDBPFamT7iUN294iPXv9WrqsT0+w4WLCmg7tFXE882wAH7QsU3DqZOyCv/lJrcMqX4V+o= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:7202:b0:6d9:d865:46c7 with SMTP id 00721157ae682-6e2b531f429mr1481757b3.2.1727999024502; Thu, 03 Oct 2024 16:43:44 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:28 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-3-seanjc@google.com> Subject: [PATCH 02/11] KVM: selftests: Precisely mask off dynamic fields in CPUID test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov When comparing vCPU CPUID entries against KVM's supported CPUID, mask off only the dynamic fields/bits instead of skipping the entire entry. Precisely masking bits isn't meaningfully more difficult than skipping entire entries, and will be necessary to maintain test coverage when a future commit enables OSXSAVE by default, i.e. makes one bit in all of CPUID.0x1 dynamic. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- .../testing/selftests/kvm/x86_64/cpuid_test.c | 61 +++++++++++-------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/cpuid_test.c b/tools/testing/selftests/kvm/x86_64/cpuid_test.c index fec03b11b059..f7fdcef5fa59 100644 --- a/tools/testing/selftests/kvm/x86_64/cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/cpuid_test.c @@ -12,17 +12,16 @@ #include "kvm_util.h" #include "processor.h" -/* CPUIDs known to differ */ -struct { - u32 function; - u32 index; -} mangled_cpuids[] = { - /* - * These entries depend on the vCPU's XCR0 register and IA32_XSS MSR, - * which are not controlled for by this test. - */ - {.function = 0xd, .index = 0}, - {.function = 0xd, .index = 1}, +struct cpuid_mask { + union { + struct { + u32 eax; + u32 ebx; + u32 ecx; + u32 edx; + }; + u32 regs[4]; + }; }; static void test_guest_cpuids(struct kvm_cpuid2 *guest_cpuid) @@ -56,17 +55,23 @@ static void guest_main(struct kvm_cpuid2 *guest_cpuid) GUEST_DONE(); } -static bool is_cpuid_mangled(const struct kvm_cpuid_entry2 *entrie) +static struct cpuid_mask get_const_cpuid_mask(const struct kvm_cpuid_entry2 *entry) { - int i; + struct cpuid_mask mask; - for (i = 0; i < ARRAY_SIZE(mangled_cpuids); i++) { - if (mangled_cpuids[i].function == entrie->function && - mangled_cpuids[i].index == entrie->index) - return true; + memset(&mask, 0xff, sizeof(mask)); + + switch (entry->function) { + case 0xd: + /* + * CPUID.0xD.{0,1}.EBX enumerate XSAVE size based on the current + * XCR0 and IA32_XSS MSR values. + */ + if (entry->index < 2) + mask.ebx = 0; + break; } - - return false; + return mask; } static void compare_cpuids(const struct kvm_cpuid2 *cpuid1, @@ -79,6 +84,8 @@ static void compare_cpuids(const struct kvm_cpuid2 *cpuid1, "CPUID nent mismatch: %d vs. %d", cpuid1->nent, cpuid2->nent); for (i = 0; i < cpuid1->nent; i++) { + struct cpuid_mask mask; + e1 = &cpuid1->entries[i]; e2 = &cpuid2->entries[i]; @@ -88,15 +95,19 @@ static void compare_cpuids(const struct kvm_cpuid2 *cpuid1, i, e1->function, e1->index, e1->flags, e2->function, e2->index, e2->flags); - if (is_cpuid_mangled(e1)) - continue; + /* Mask off dynamic bits, e.g. OSXSAVE, when comparing entries. */ + mask = get_const_cpuid_mask(e1); - TEST_ASSERT(e1->eax == e2->eax && e1->ebx == e2->ebx && - e1->ecx == e2->ecx && e1->edx == e2->edx, + TEST_ASSERT((e1->eax & mask.eax) == (e2->eax & mask.eax) && + (e1->ebx & mask.ebx) == (e2->ebx & mask.ebx) && + (e1->ecx & mask.ecx) == (e2->ecx & mask.ecx) && + (e1->edx & mask.edx) == (e2->edx & mask.edx), "CPUID 0x%x.%x differ: 0x%x:0x%x:0x%x:0x%x vs 0x%x:0x%x:0x%x:0x%x", e1->function, e1->index, - e1->eax, e1->ebx, e1->ecx, e1->edx, - e2->eax, e2->ebx, e2->ecx, e2->edx); + e1->eax & mask.eax, e1->ebx & mask.ebx, + e1->ecx & mask.ecx, e1->edx & mask.edx, + e2->eax & mask.eax, e2->ebx & mask.ebx, + e2->ecx & mask.ecx, e2->edx & mask.edx); } } From patchwork Thu Oct 3 23:43:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821675 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6A9BD1D14EF for ; Thu, 3 Oct 2024 23:43:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999030; cv=none; b=NEpmMyuxPi5YqZ+gJ9q0Oub7r8ZSCKKA7NdjhxKc/XT7lxb8RdgKXZG6ZFE1ELLBU8rWhozm4Xt6dbh2vcBxCkxH2JxYhz5osVRTImoDBullq8H5GoDnp646jnllJO3PqD5Jo5cr3eZo1hpnoghzQljbpvD8NZwbwQ+z8/3CIa8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999030; c=relaxed/simple; bh=PtrRlu6468I2zXi5sMQUl9RRClXhgN/bYQ8V5FPjqhM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iJugVAeiqF5d86FeFgocS7JU/kl2TwGGXm8LUOF3EDCv14MPYq47lQfZko94EsAiaUQRMGQ0XxGb5ktt+zH+nTi6e5eaqr3cxm2A30VnfrTf3VUid218WuiWPFKqsdcMgba2N8h2NBUmad6FasTGT5SzXS/xMx5lFh7Lpgsafj0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=PmgE+5dL; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="PmgE+5dL" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e230808455so19636997b3.0 for ; Thu, 03 Oct 2024 16:43:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999026; x=1728603826; 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=49GBsflx4kJ26M5BBQeAer4waxjgInZgGBve4oDuDFo=; b=PmgE+5dL+VQF7FqUAKqT9hxg3bWfje1imoD5wJGLHHCBM01er+cXfzy4k9mNcAsyrD GTjN0fqlcz+Y2CwSbSlTL/zL63Q6GVq027QfBQImcE9sCYPd8o4OvXOXhsZg+Rsoex8M a7pRtaBMJWw7z924qPGrjI/P9YeRl+xUZTC7yrdsCmHbbfZsn0Km99crSJXys3yRp2Hz KADohJPwd16DLHtVldDG2RXVhX6dc939RE+okFSNl/QqtxvEuidLmptRCRR2Y+3MA6o8 9ABpu5hx22Q+8MJIP3LZv+CErjc8RunTOIw3v5VTsKNz4WrBKOOOqzmnWd6+U5/bjf4H GlOQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999026; x=1728603826; 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=49GBsflx4kJ26M5BBQeAer4waxjgInZgGBve4oDuDFo=; b=V4uemwoYyHpPvXb+9SvKao494OWzWpC5OgDtILoMhDER7hD9TDlBuWYh/TCmV4BwTI biOqqFbyoQTasmIejbIbPGF3HFaNT1qy+MXB0m2blrmqEdlzd6t5A1ixK0PNGNNuwb6c 4Pi89O01WQCaWxX1OeePrNeJg3Ro7JB0+mD12Jt/+HXurK02ESJuKZHqHft9YqkzmdGh wcmIhqORYN60XQ3CTkcHK5bzETDJsbJiPs5M/cW0XM3yTer6A62ff3p/2U45hFwdICbV hHMwGlzgW15oDZGv4mWSlg4GzyI719MEpgl7NjNf9z7Lcr34h87VNeN0AkMY3D0kJ0fL 3Aow== X-Gm-Message-State: AOJu0YwFWZ5AcYzZig+n2adCyzrn02PJrTVYLbhHEaHQ2E4Gxci07JjX wbFLS7gbNRKfLsBKz4CqcBj+CuxHJu5DZxGWftiihmWUXRUYwVSelnrtBAIY/BSPOEkhctnlTGs ZvQ== X-Google-Smtp-Source: AGHT+IHXH8suqly6HdzHCQ+5EeC4WB/t3O9O2a59kE4ofo5bIvTb741Hm2G2KACsRGD7rnXfUY27G4xAUaE= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:938a:0:b0:e0b:d729:ff8d with SMTP id 3f1490d57ef6-e28936e8489mr1022276.5.1727999026390; Thu, 03 Oct 2024 16:43:46 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:29 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-4-seanjc@google.com> Subject: [PATCH 03/11] KVM: selftests: Mask off OSPKE and OSXSAVE when comparing CPUID entries From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Mask off OSPKE and OSXSAVE, which are toggled based on corresponding CR4 enabling bits, when comparing vCPU CPUID against KVM's supported CPUID. This will allow setting OSXSAVE by default when creating vCPUs, without causing test failures (KVM doesn't enumerate OSXSAVE=1). Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/cpuid_test.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/cpuid_test.c b/tools/testing/selftests/kvm/x86_64/cpuid_test.c index f7fdcef5fa59..7b3fda6842bc 100644 --- a/tools/testing/selftests/kvm/x86_64/cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/cpuid_test.c @@ -62,6 +62,12 @@ static struct cpuid_mask get_const_cpuid_mask(const struct kvm_cpuid_entry2 *ent memset(&mask, 0xff, sizeof(mask)); switch (entry->function) { + case 0x1: + mask.regs[X86_FEATURE_OSXSAVE.reg] &= ~BIT(X86_FEATURE_OSXSAVE.bit); + break; + case 0x7: + mask.regs[X86_FEATURE_OSPKE.reg] &= ~BIT(X86_FEATURE_OSPKE.bit); + break; case 0xd: /* * CPUID.0xD.{0,1}.EBX enumerate XSAVE size based on the current From patchwork Thu Oct 3 23:43:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821676 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1048B1D1758 for ; Thu, 3 Oct 2024 23:43:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999030; cv=none; b=f/42qyvZuuh6IRRa6kTQB2dqfJOd/98KO5V+LLeHXAdaGToved+TlvIJ1HE6+7SQdfTKnyDdosy5SpZV5lg9bVP6uYL9WI7xnYzSG1/aVfrc76roie0k1oxNt4E5DTuQYzjPzxwK1cSCVHl1LEw2h8Q9n1qkwRIyehON3G9hNY8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999030; c=relaxed/simple; bh=xhRtPkgV9Q/H720xNEs74p9Eh3oxK2vk3t5Rc0CtUR8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Y+j8sqdLtJtw+GspGw8OCOvY50mk7DTGy+nISPkrFJsxf/0t10DRou4w9nViUx3FLN8AdDAkZSXe9m3pl6KQFGAVSj5bYOHKmkrFcRzznTAy+Qcq5Ykd6DFFI9nluGm3hmIWaTZ0XTmfFQTrZ70iLgguDkLJ9Pi/bxge4+1zTTw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=CE0E4BAG; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="CE0E4BAG" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6d7124939beso26503687b3.2 for ; Thu, 03 Oct 2024 16:43:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999028; x=1728603828; 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=tfyRHkNYv2lBXqcBCE/BM0O93uGXVshEfgAZDAs1C7Y=; b=CE0E4BAGxZ1xJ/sXqMQxPm9zFG3FezML2GR8owJPTJUy+zqpNUnfeoRFQg2oUR7qUE H5hrtGlPfPkn8/7lM8RKK5lqEZBqTiAQLr8Fm37Ez2sem4wb37x7ABtIXIpQrcFuh7by TvkTFLGZ6cQUuKQVs0U2bMPnrvDplsNOTdq2NISCE1q0BYl9ACJq7lyr5WfpSWkcxRQS zLHHfPyKCsBpVRYFnEOCJONXjdakPXTZMGE7Bvca7V0sI4BTELDT3HjPAhxmNj3I4vff IcaZCL0fvnF9Hpm4zT1YIUc0e/PVVM7oiST4Pg7UQxgChSdmk0cvzbwG9ksYA7TMBUAt B++g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999028; x=1728603828; 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=tfyRHkNYv2lBXqcBCE/BM0O93uGXVshEfgAZDAs1C7Y=; b=LUhJ5J4M1BkPjwa0VfmsidYdOyj3rfKLTTepEg3+B6W9nXy790rVY29ZZHbvh58cxS ERrMERfAYkcHzA9f/f8mMH7pPeT2M6WCFVhkktFxULuFHCkXjVr6WZ8Gghi+tSfc3vMT leM2mR2o4w07Jm6bkWc5Q5q5C3A+5WPBteI5M1OE6aQ/cihc4t1wM58owsqZomnkheLC qenXRs3fqT8K4oEOjddYuOEFhLXwEdDDH9AhfqRf84eIfZWPz7Ss8wK3UorJnMWOPtqH I5Qh6EY8+5iM61oc3/xR9n74CfbnuTnEMbE9u2wvc85s01EFNqNoMrODhI5W9uVC/pZ3 xS7Q== X-Gm-Message-State: AOJu0Yy/wmaEDWFFFrfCwDyJ+ZKT9dbUdaJqDHpnjQP+dddsWF5yR5pO Zv3SKUi3gSBmv4mc+LqSMhZC7qxRtC+LFfwXIyOQxGSyGK1XFW1GLWDEQGRF2TEIvlAZLBR+jk3 MBA== X-Google-Smtp-Source: AGHT+IGfWN/im2Wj2GcFD7QkzpCS0bnI6+78rBQmlk913hbGcIabj1x9PvHCC5Bd0/hV07nZoZuOXxqSaCA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:ecd:b0:6e2:a129:161a with SMTP id 00721157ae682-6e2c6fbefb5mr202067b3.2.1727999028278; Thu, 03 Oct 2024 16:43:48 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:30 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-5-seanjc@google.com> Subject: [PATCH 04/11] KVM: selftests: Rework OSXSAVE CR4=>CPUID test to play nice with AVX insns From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Rework the CR4/CPUID sync test to clear CR4.OSXSAVE, do CPUID, and restore CR4.OSXSAVE in assembly, so that there is zero chance of AVX instructions being executed while CR4.OSXSAVE is disabled. This will allow enabling CR4.OSXSAVE by default for selftests vCPUs as a general means of playing nice with AVX instructions. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- .../kvm/x86_64/cr4_cpuid_sync_test.c | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c b/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c index 624dc725e14d..da818afb7031 100644 --- a/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +++ b/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c @@ -19,15 +19,14 @@ #include "kvm_util.h" #include "processor.h" -static inline bool cr4_cpuid_is_sync(void) -{ - uint64_t cr4 = get_cr4(); - - return (this_cpu_has(X86_FEATURE_OSXSAVE) == !!(cr4 & X86_CR4_OSXSAVE)); -} +#define MAGIC_HYPERCALL_PORT 0x80 static void guest_code(void) { + u32 regs[4] = { + [KVM_CPUID_EAX] = X86_FEATURE_OSXSAVE.function, + [KVM_CPUID_ECX] = X86_FEATURE_OSXSAVE.index, + }; uint64_t cr4; /* turn on CR4.OSXSAVE */ @@ -36,13 +35,29 @@ static void guest_code(void) set_cr4(cr4); /* verify CR4.OSXSAVE == CPUID.OSXSAVE */ - GUEST_ASSERT(cr4_cpuid_is_sync()); + GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE)); - /* notify hypervisor to change CR4 */ - GUEST_SYNC(0); + /* + * Notify hypervisor to clear CR4.0SXSAVE, do CPUID and save output, + * and then restore CR4. Do this all in assembly to ensure no AVX + * instructions are executed while OSXSAVE=0. + */ + asm volatile ( + "out %%al, $" __stringify(MAGIC_HYPERCALL_PORT) "\n\t" + "cpuid\n\t" + "mov %%rdi, %%cr4\n\t" + : "+a" (regs[KVM_CPUID_EAX]), + "=b" (regs[KVM_CPUID_EBX]), + "+c" (regs[KVM_CPUID_ECX]), + "=d" (regs[KVM_CPUID_EDX]) + : "D" (get_cr4()) + ); - /* check again */ - GUEST_ASSERT(cr4_cpuid_is_sync()); + /* Verify KVM cleared OSXSAVE in CPUID when it was cleared in CR4. */ + GUEST_ASSERT(!(regs[X86_FEATURE_OSXSAVE.reg] & BIT(X86_FEATURE_OSXSAVE.bit))); + + /* Verify restoring CR4 also restored OSXSAVE in CPUID. */ + GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE)); GUEST_DONE(); } @@ -62,13 +77,16 @@ int main(int argc, char *argv[]) vcpu_run(vcpu); TEST_ASSERT_KVM_EXIT_REASON(vcpu, KVM_EXIT_IO); - switch (get_ucall(vcpu, &uc)) { - case UCALL_SYNC: + if (vcpu->run->io.port == MAGIC_HYPERCALL_PORT && + vcpu->run->io.direction == KVM_EXIT_IO_OUT) { /* emulate hypervisor clearing CR4.OSXSAVE */ vcpu_sregs_get(vcpu, &sregs); sregs.cr4 &= ~X86_CR4_OSXSAVE; vcpu_sregs_set(vcpu, &sregs); - break; + continue; + } + + switch (get_ucall(vcpu, &uc)) { case UCALL_ABORT: REPORT_GUEST_ASSERT(uc); break; From patchwork Thu Oct 3 23:43:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821677 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 231231D1F4F for ; Thu, 3 Oct 2024 23:43:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999032; cv=none; b=aT3O8sW8D+iknDKEdyr6ns2/8Z5LB5EUeLtymtpX7a7t7xz9Tmm2zzDtBUyokYyfLisDzJYCVRXA+OQ9YasyJYW4zcnyYRVj1Ers64aYTE8ZXKZkqPQ6pQFALn+tV4m0wqG7/vn4cMCsmDCsrF/nhybD+w3lXpQEypvd5A12YCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999032; c=relaxed/simple; bh=gX+o8XXJM50peceuHqmdcWWs3o/nxzR2aXn9TDSmehw=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=g8w0VAW3qacQdMefeUlLtiSB56uOdkyYPGesy2mrgCbOCqPVsbbKyHzrg60TJVZ9v3b2tQC6+n4wstUQ17Vx/DbPoQjbQ0cxG/sOpGaZFBwiUIHkcydrF5ZDUJwoj4D0DYonSm0T957ECRCz0F3no121Yd/1MZrbGwR6dYK6rLc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=M1ELjmWc; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="M1ELjmWc" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e2590f5bc1so23167077b3.2 for ; Thu, 03 Oct 2024 16:43:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999030; x=1728603830; 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=gk2ltXEs/6rBeGKFiAGP6lmYm5gM6rfWM1zcP38vcLM=; b=M1ELjmWcrKGIHv6dlzNem/9r8nXGOLAHgM6FCnhWkfQo6U8luc77hyyf7n3pNvXMgc MWcjAI1hOU7VnsMdJp8rqgkytznwecYJg4jhLqYwGffMoW7z4yGU+uF2tGe6Mm6YRcMQ A1xSzSFywBifDCISZ3soisQTn3GeJlp08WXOBM3rdClJvm8r5NZKYOIBiUYMVLb5CH91 5rIC3ZYdSssn16JPfz9wievtg+pfYIOz5pdw7UxMLu2RXrkSO6upVeczRtTkzbEnmfDP Ik3oRmrXnpiI3/HbKJooZN/asg1LqEFGtF5YuO79jtSasQet5r29/0Vyd+IsGnTds+Iz lf4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999030; x=1728603830; 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=gk2ltXEs/6rBeGKFiAGP6lmYm5gM6rfWM1zcP38vcLM=; b=QBecvl5m5JZa8FvF7hYbp9KVVoSEC9iRz472Om8oMXRHFWwovipcM6QrVf700ldNLq AC+WRQIfmt1wD8VGHv8Iz7JfQTDrVlesdrcGseHta4NayolkUtpyjKDkCzJCb5mpNt4o 8ZUF1I0an/BzSKruY2YHxTQIu9sXc5l7E2qOIJT/YE/3s+jFfwN5FmEhhjtGQCR6mrPI zXwmOxXYKF8B70tkriax4ZJQBZG5FP4VrOdpfMxhCvgo8bMLeUHCtP7VebX1dbRQeRxZ m1TsqInwPhkdrJtvIoKChLeWOrU5H/R0VW2YHtYgXdUNPvu2XoOuEM/QrA8Homjx2hnN AhQw== X-Gm-Message-State: AOJu0Yz4P/GqjXxMLcet9fBTGEveUQqRCpKY9lA+3PAIrDrMp1Opj02h wUl+zuRuYwUvYDqlpBoVtKwDhB/ku5GpTVg5HJ76zJrCa5ZP7eOEQVFe4tb6jmh7hymifQ0A+E7 GvA== X-Google-Smtp-Source: AGHT+IH1mRDr/LJ1+AGep1hMpxSXBKZFso+u/Vj+xIKpeT7s6R8CKDq8kLEN6DeZJyO2znluPHRoe2V7e78= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:361d:0:b0:e22:5bdf:39c1 with SMTP id 3f1490d57ef6-e289394938fmr459276.10.1727999030121; Thu, 03 Oct 2024 16:43:50 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:31 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-6-seanjc@google.com> Subject: [PATCH 05/11] KVM: selftests: Configure XCR0 to max supported value by default From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov To play nice with compilers generating AVX instructions, set CR4.OSXSAVE and configure XCR0 by default when creating selftests vCPUs. Some distros have switched gcc to '-march=x86-64-v3' by default, and while it's hard to find a CPU which doesn't support AVX today, many KVM selftests fail with ==== Test Assertion Failure ==== lib/x86_64/processor.c:570: Unhandled exception in guest pid=72747 tid=72747 errno=4 - Interrupted system call Unhandled exception '0x6' at guest RIP '0x4104f7' due to selftests not enabling AVX by default for the guest. The failure is easy to reproduce elsewhere with: $ make clean && CFLAGS='-march=x86-64-v3' make -j && ./x86_64/kvm_pv_test E.g. gcc-13 with -march=x86-64-v3 compiles this chunk from selftests' kvm_fixup_exception(): regs->rip = regs->r11; regs->r9 = regs->vector; regs->r10 = regs->error_code; into this monstronsity (which is clever, but oof): 405313: c4 e1 f9 6e c8 vmovq %rax,%xmm1 405318: 48 89 68 08 mov %rbp,0x8(%rax) 40531c: 48 89 e8 mov %rbp,%rax 40531f: c4 c3 f1 22 c4 01 vpinsrq $0x1,%r12,%xmm1,%xmm0 405325: 49 89 6d 38 mov %rbp,0x38(%r13) 405329: c5 fa 7f 45 00 vmovdqu %xmm0,0x0(%rbp) Alternatively, KVM selftests could explicitly restrict the compiler to -march=x86-64-v2, but odds are very good that punting on AVX enabling will simply result in tests that "need" AVX doing their own thing, e.g. there are already three or so additional cleanups that can be done on top. Reported-by: Vitaly Kuznetsov Closes: https://lore.kernel.org/all/20240920154422.2890096-1-vkuznets@redhat.com Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 5 ++++ .../selftests/kvm/lib/x86_64/processor.c | 24 +++++++++++++++++++ .../selftests/kvm/x86_64/xcr0_cpuid_test.c | 6 ++--- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index e247f99e0473..645200e95f89 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1049,6 +1049,11 @@ static inline void vcpu_set_cpuid(struct kvm_vcpu *vcpu) vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid); } +static inline void vcpu_get_cpuid(struct kvm_vcpu *vcpu) +{ + vcpu_ioctl(vcpu, KVM_GET_CPUID2, vcpu->cpuid); +} + void vcpu_set_cpuid_property(struct kvm_vcpu *vcpu, struct kvm_x86_cpu_property property, uint32_t value); diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 974bcd2df6d7..636b29ba8985 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -506,6 +506,8 @@ static void vcpu_init_sregs(struct kvm_vm *vm, struct kvm_vcpu *vcpu) sregs.cr0 = X86_CR0_PE | X86_CR0_NE | X86_CR0_PG; sregs.cr4 |= X86_CR4_PAE | X86_CR4_OSFXSR; + if (kvm_cpu_has(X86_FEATURE_XSAVE)) + sregs.cr4 |= X86_CR4_OSXSAVE; sregs.efer |= (EFER_LME | EFER_LMA | EFER_NX); kvm_seg_set_unusable(&sregs.ldt); @@ -519,6 +521,20 @@ static void vcpu_init_sregs(struct kvm_vm *vm, struct kvm_vcpu *vcpu) vcpu_sregs_set(vcpu, &sregs); } +static void vcpu_init_xcrs(struct kvm_vm *vm, struct kvm_vcpu *vcpu) +{ + struct kvm_xcrs xcrs = { + .nr_xcrs = 1, + .xcrs[0].xcr = 0, + .xcrs[0].value = kvm_cpu_supported_xcr0(), + }; + + if (!kvm_cpu_has(X86_FEATURE_XSAVE)) + return; + + vcpu_xcrs_set(vcpu, &xcrs); +} + static void set_idt_entry(struct kvm_vm *vm, int vector, unsigned long addr, int dpl, unsigned short selector) { @@ -675,6 +691,7 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) vcpu = __vm_vcpu_add(vm, vcpu_id); vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); vcpu_init_sregs(vm, vcpu); + vcpu_init_xcrs(vm, vcpu); /* Setup guest general purpose registers */ vcpu_regs_get(vcpu, ®s); @@ -686,6 +703,13 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id) mp_state.mp_state = 0; vcpu_mp_state_set(vcpu, &mp_state); + /* + * Refresh CPUID after setting SREGS and XCR0, so that KVM's "runtime" + * updates to guest CPUID, e.g. for OSXSAVE and XSAVE state size, are + * reflected into selftests' vCPU CPUID cache, i.e. so that the cache + * is consistent with vCPU state. + */ + vcpu_get_cpuid(vcpu); return vcpu; } diff --git a/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c b/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c index 95ce192d0753..a4aecdc77da5 100644 --- a/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c @@ -48,16 +48,16 @@ do { \ static void guest_code(void) { - uint64_t xcr0_reset; + uint64_t initial_xcr0; uint64_t supported_xcr0; int i, vector; set_cr4(get_cr4() | X86_CR4_OSXSAVE); - xcr0_reset = xgetbv(0); + initial_xcr0 = xgetbv(0); supported_xcr0 = this_cpu_supported_xcr0(); - GUEST_ASSERT(xcr0_reset == XFEATURE_MASK_FP); + GUEST_ASSERT(initial_xcr0 == supported_xcr0); /* Check AVX */ ASSERT_XFEATURE_DEPENDENCIES(supported_xcr0, From patchwork Thu Oct 3 23:43:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821678 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B6CEE1D221B for ; Thu, 3 Oct 2024 23:43:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999034; cv=none; b=P6DjTsVrQ7O/IL7q9yBzghllay+FOP1ORf6ZAIxSKhaffyUTZigBPMwe4HgT6MJvrv1Lb2vJa5vJERdlNccNbCSHdjOH+EZMEqCzFtLpA1wD1g76/UDtXp1dUt+lB2njlj/z0rEXEBfhNfq0CtcLYhKn32z2sdaqXvJT0I+vSuE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999034; c=relaxed/simple; bh=YWaZS/REFKJMLevlqrMIo/CR/NX3I+PwQJV5TvOBW+s=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HZIF0R8fWgfTotEkyYFaCdRAjTdUbJRLQGTx/DFozs02knrufIDLdOew9kjfPJGn+wSPKQUUaaVJIzR1AgR7YpyAY8XSFs/yfQ4fRqQxtLsNGLN+utovhFilvBD/5lUOHoN6x7hwlJmXggrskij5OQUNLGfr9ysb02Cr2yc0zlg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tQMSKbCT; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tQMSKbCT" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7d4f9974c64so1055198a12.1 for ; Thu, 03 Oct 2024 16:43:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999032; x=1728603832; 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=xgBcuvGk3o3ZhJ5eerG862wxsCgl+dqWJVsjJCQrLX8=; b=tQMSKbCTN833z8PFmSr0bP6Be9Ipa2H0X8P+Nfmi+zYAJM6Iduuok3T9oHynd6H/Tw iGu2Mq+3sUWbtl5XYOOa6ybifHd/wOi/tCmvbCjjBbtwQ7bQHuwBcYPM6L/01IolCExF CatAu7x/VpsrRu1Gj0a0nDrO3FsFuAIfTl0KO8L+h04vdcEK4MgMPD3dvSTejB/9KwhA qB4yTZSOtnHpk49Slm6OkZWSWCYnFRManXCh7rMf9Zw3y5+PJASirc+MDNIw6lBHy1h6 rOE1gsaoeeRyPWhFhpSQ280QsTRfq3KwGHNsc6GA2t50ZQZNXYoES8JaAbKwDvd5UP/S VwrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999032; x=1728603832; 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=xgBcuvGk3o3ZhJ5eerG862wxsCgl+dqWJVsjJCQrLX8=; b=K3m1dkOvNBqjGDN/I9BqmM/Doo/G0YslgCoXrWYZvrCevCdHd3D+ObtDwz2bXm6e4x 3z6sEaSFv9loBuFxJU4zwPsina7lg/wfLAK0VsGcJKFERxUxKBZUfFX2fND+CWtLfPWw fblaYfeQ2XLgVLaylZx12RHOfVi/qc/vPe0vcIc+qfeAjdG05Ga3qNBinQuO49zww74K hMT8qT5jSYpwQxUIRpQinn7qJYL1tgzYT/NLboRUxXrj+N8whJojwbkPnSx1zh0KYTfa asl6t6cnGulX7HSrl8xKVpfgYS/jzd1PfoKzkeEP0Tu0r7mjHB4xEQvidZLM5MUjMHc6 piaQ== X-Gm-Message-State: AOJu0YzT9cwnzOe4dSh68BMthquK+y0AsSoTjlIl+CWwACY9hZyb7JfV rENq/72O1krNdncJY+2sA37wmzSw4FghsK8PWX+nIWZSrmfLglc5wodb8QX5YFLFzV0tXP3A4Sw 7Kw== X-Google-Smtp-Source: AGHT+IFXq9RCb6WRkwMAtMcPJ9IUSeE+59BJekmESI3c/ddtcupPuWgRFNALH1TsU96CuFHtqHa2A7atpFQ= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a63:8c07:0:b0:7db:16b4:7a2a with SMTP id 41be03b00d2f7-7e9e59a8976mr1289a12.2.1727999031876; Thu, 03 Oct 2024 16:43:51 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:32 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-7-seanjc@google.com> Subject: [PATCH 06/11] KVM: selftests: Verify XCR0 can be "downgraded" and "upgraded" From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Now that KVM selftests enable all supported XCR0 features by default, add a testcase to the XCR0 vs. CPUID test to verify that the guest can disable everything except the legacy FPU in XCR0, and then re-enable the full feature set, which is kinda sorta what the test did before XCR0 was setup by default. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c b/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c index a4aecdc77da5..c8a5c5e51661 100644 --- a/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c +++ b/tools/testing/selftests/kvm/x86_64/xcr0_cpuid_test.c @@ -79,6 +79,11 @@ static void guest_code(void) ASSERT_ALL_OR_NONE_XFEATURE(supported_xcr0, XFEATURE_MASK_XTILE); + vector = xsetbv_safe(0, XFEATURE_MASK_FP); + __GUEST_ASSERT(!vector, + "Expected success on XSETBV(FP), got vector '0x%x'", + vector); + vector = xsetbv_safe(0, supported_xcr0); __GUEST_ASSERT(!vector, "Expected success on XSETBV(0x%lx), got vector '0x%x'", From patchwork Thu Oct 3 23:43:33 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821679 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4883C1D2700 for ; Thu, 3 Oct 2024 23:43:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999035; cv=none; b=rF8zmHfGHNA5Qrl+0M4kcBXvHfSmCzz8C4Crm9xFKPtTdAiqjgwWHCmDe06sKQp5gM5jTZi+FetlgfBb1lJYQLdVPXStUg0AmmsaX3dCC3n7kkI130UOavE+ahCYlM0ScNBBJ83CIguU6BhWLTtuEJI+vEwp3A0Kay2DU6cIiis= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999035; c=relaxed/simple; bh=gl0Wr8APCYJH9gb2faxHbvqjAXiNHTO8/OWXX9MH4KA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=DslmBPC+ofA4uvLDFwt2IL882RUa+cbPfyZ9Pc5V29bvPQDiJPQkw/8Fo0lhtL9YQ3J9w/MyEuB8i7HZAJwpA08EkfMxp3DAN7jPDoCFlEALSosXwKKpOe/MM+aMdk3dY0d5TvJfiAnu9coG2kXs0DncOdO8rKP5OCAvXwrsuJA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ZkK6Yrrz; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ZkK6Yrrz" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2e148d4550bso1600312a91.1 for ; Thu, 03 Oct 2024 16:43:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999034; x=1728603834; 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=pDkQ3m0IH+ik1cLm4fl5hS775lZs/1BFcA2J8KjREsM=; b=ZkK6Yrrzcn5MfWOaz2j/G79z3a0O7N77tqsV2gqSQ1IEHGO8QRlz/rYiix7RbeJY6e srOmBqDMWy/cgBiBmBQzjA6kaLO4TI33Hj8UBvI9UCCyulD9dH1EGF3pcIra2/j0q6y0 Ex3tzHTwzItdIQgPC1+weYLCW3WmOUdo/BSt5vNhNFWy4nSHQ8NT+olCJGAtTmhuYE5B uMP+KAWXPQA78QW4T6cgc0Q4gkySjOTzSVc8Bg2gsywbstg3hTHum/pyxNcKm8j+fV4L 111GihK+l2POzkKIADWCbw0FYZiSK10h939Ae4dA9eTVy5Y4sNNZDysnuwNyEgz2MXkU xa8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999034; x=1728603834; 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=pDkQ3m0IH+ik1cLm4fl5hS775lZs/1BFcA2J8KjREsM=; b=RgZkEaUAWO6PAQmqkzJw7oqEBPRMUB/fyyTRuxxDcUPfNfdfCsfgusIUKE5H2VKZye CUXWXK80T6eKEKxN5f47MZDJ17D6ruDGZ32fdvjUr62af5cSQAhMBUSGcqN8Qd6KGQZx 5sn40O/fjZ6Wh6kFelhAX7lm9865W0fTCcv95pC9Mu9nN+WPP259ViX7vBoAgw0lLV76 PMa+8e+XdMW3gDQMAtPgHN4iiefCdswiuwvIJeFZDLBVkwa3MpRqC6izbNhy0sJ55MCA IQR4n9ZK5FzRurgTtX/s9ARAKRVqqfF6c5P0YzYSVvkEa4pna6KvLgLs2YEyn5/UmAI+ I92w== X-Gm-Message-State: AOJu0Yz3UTFyEgHVLu1Mn6zfdT/k3ulEA337HNcTNMR1IhSqKqACmlMt ixQqc59F/XAJ9C4ZBKYuRE7J3/buVKLkgScjew1+ryzn3/lSLEx3OY2gZpoaGi1NGa2OSX2uZeU a1g== X-Google-Smtp-Source: AGHT+IEPUf60YVy3+qxeYEMLExtz6vIBv7c6bd/YO+J15uqPf7aKwNWa1u5iohopHmwKZq52HVRfPFla+t4= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:156:b0:2e0:876c:8cb6 with SMTP id 98e67ed59e1d1-2e1e620c428mr2878a91.2.1727999033692; Thu, 03 Oct 2024 16:43:53 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:33 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-8-seanjc@google.com> Subject: [PATCH 07/11] KVM: selftests: Drop manual CR4.OSXSAVE enabling from CR4/CPUID sync test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Now that CR4.OSXSAVE is enabled by default, drop the manual enabling from CR4/CPUID sync test and instead assert that CR4.OSXSAVE is enabled. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c b/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c index da818afb7031..28cc66454601 100644 --- a/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c +++ b/tools/testing/selftests/kvm/x86_64/cr4_cpuid_sync_test.c @@ -27,12 +27,9 @@ static void guest_code(void) [KVM_CPUID_EAX] = X86_FEATURE_OSXSAVE.function, [KVM_CPUID_ECX] = X86_FEATURE_OSXSAVE.index, }; - uint64_t cr4; - /* turn on CR4.OSXSAVE */ - cr4 = get_cr4(); - cr4 |= X86_CR4_OSXSAVE; - set_cr4(cr4); + /* CR4.OSXSAVE should be enabled by default (for selftests vCPUs). */ + GUEST_ASSERT(get_cr4() & X86_CR4_OSXSAVE); /* verify CR4.OSXSAVE == CPUID.OSXSAVE */ GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE)); From patchwork Thu Oct 3 23:43:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821680 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 657441D2793 for ; Thu, 3 Oct 2024 23:43:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999037; cv=none; b=iVJqJwEL4yOBdfFapHKHg+UdihW7oZnVJpDlI7FntntURgRLNuTexfi9UY1ICvp3TD0AKxTRNl2hRor5+5TGO+GGn7cDAu+kLeqARlmJcFn9UclAfUUCvsAXWhLC1EElGiBkHsqS/yGFmwm0gaES9C4TdG+KYRd8o3UnykXANEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999037; c=relaxed/simple; bh=V7rKuwPrSY+SRxJi0K7vLggqsnLA/DoeF2YXdfDToSY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bdeENhur5CJi6RwTUNa5dG/Miu7kIjCj4N9irjjJZYZ8/H0VBX7RheI/486b0seOUxq+Flb8p4j6rL2fk+g0VisietaFykVbD9lKT3cfQYo32ieSaovImhfNDdgUjIA2bid1LJxkiInyj1i7B60zH9Rra3aLM7k2ztlSaUA6DLg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=l2yQJtIB; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="l2yQJtIB" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6ddbcc96984so23911907b3.2 for ; Thu, 03 Oct 2024 16:43:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999035; x=1728603835; 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=cYdreTQ2dGWT694xFw76MeJwXKyqFrydL4ES/jVoY04=; b=l2yQJtIB38Znd+mhBEM7C0hGpm/tqgYVtznuP/a3WnRKKT+e6oR5VlTiaBM4OZWw48 o3q9FqDgV/hDqNJUNqO/FuT9xKH/DQkrhGTNUBYLIsTcBTaTz4kEsrB9PriagXNeAWqT UIXG4QRTRu/cUIGp9UUFAtLXuxY3CeayzuA+t3AOgPmICPYAXCOXjpTG5LE9qMUHHT2N NaO7RfDGXV75r+T1wBPog7Rnnp77iRIWAl6d2ck5q+1dyMi6cBmjt47I+j55TEzrW7Y/ PxVK/TzBxq+oIKa8xgiEtYf5zogMQnjXGEjOGhXs2bkyBp9Mla8CSX/OBlIA54W38RlA AaWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999035; x=1728603835; 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=cYdreTQ2dGWT694xFw76MeJwXKyqFrydL4ES/jVoY04=; b=R6sGirtfVufaqBVckJcbG4Ka61ERfqZzycwYcTEpTm2kBYejrTZyAD5O2eN7EA8h7i hD52ue7XjwvzGNoLZLh9bPnzwHbRWeYoDmgui+llKIPXBHRPwzeg9TN9WiKNB2WOJOs0 bzFAbXhFCPB0yNP6cJPJb+bLIDcIwm43rlFgJS/dhYd9nZlugYBtyYww9LBFR3XmNOrv 49nnRfMDfjbdC6dnN86EiVLl1rsqerhgJIonJacG9aWXDHp0J7DRGoVaVhrk3WvkKzzQ evINi6DelByvPsfUW7ZsV0wMhv7kGkzFGBmQXXfN2D7skeZmYb3N10Iy4OcH4gZvRatP SdIQ== X-Gm-Message-State: AOJu0Ywouz05Tk7zEDocVlMSPoCQIZYPtf0ZMhsMAUEC9z4CTrKlY/KA 92/W8W449o4nSIwWXtYYkVn63Rvtkkh6CE2miJ6mABifymXxbyuIJNCJviAql1JCQpR2GAhN12G p+A== X-Google-Smtp-Source: AGHT+IEd91DInd0W/wFySOQh235Qh88p94i5fGTLkUED1r7q1hClizhaqe0VKQW4C1DefHLLlGROsyVnmok= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:690c:6a0c:b0:6dd:bc07:2850 with SMTP id 00721157ae682-6e2c7295006mr136787b3.6.1727999035387; Thu, 03 Oct 2024 16:43:55 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:34 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-9-seanjc@google.com> Subject: [PATCH 08/11] KVM: selftests: Drop manual XCR0 configuration from AMX test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Now that CR4.OSXSAVE and XCR0 are setup by default, drop the manual enabling of OXSAVE and XTILE from the AMX test. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/amx_test.c | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/amx_test.c b/tools/testing/selftests/kvm/x86_64/amx_test.c index 903940c54d2d..f4ce5a185a7d 100644 --- a/tools/testing/selftests/kvm/x86_64/amx_test.c +++ b/tools/testing/selftests/kvm/x86_64/amx_test.c @@ -86,6 +86,8 @@ static inline void __xsavec(struct xstate *xstate, uint64_t rfbm) static void check_xtile_info(void) { + GUEST_ASSERT((xgetbv(0) & XFEATURE_MASK_XTILE) == XFEATURE_MASK_XTILE); + GUEST_ASSERT(this_cpu_has_p(X86_PROPERTY_XSTATE_MAX_SIZE_XCR0)); GUEST_ASSERT(this_cpu_property(X86_PROPERTY_XSTATE_MAX_SIZE_XCR0) <= XSAVE_SIZE); @@ -122,29 +124,12 @@ static void set_tilecfg(struct tile_config *cfg) } } -static void init_regs(void) -{ - uint64_t cr4, xcr0; - - GUEST_ASSERT(this_cpu_has(X86_FEATURE_XSAVE)); - - /* turn on CR4.OSXSAVE */ - cr4 = get_cr4(); - cr4 |= X86_CR4_OSXSAVE; - set_cr4(cr4); - GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE)); - - xcr0 = xgetbv(0); - xcr0 |= XFEATURE_MASK_XTILE; - xsetbv(0x0, xcr0); - GUEST_ASSERT((xgetbv(0) & XFEATURE_MASK_XTILE) == XFEATURE_MASK_XTILE); -} - static void __attribute__((__flatten__)) guest_code(struct tile_config *amx_cfg, struct tile_data *tiledata, struct xstate *xstate) { - init_regs(); + GUEST_ASSERT(this_cpu_has(X86_FEATURE_XSAVE) && + this_cpu_has(X86_FEATURE_OSXSAVE)); check_xtile_info(); GUEST_SYNC(1); From patchwork Thu Oct 3 23:43:35 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821681 Received: from mail-pg1-f201.google.com (mail-pg1-f201.google.com [209.85.215.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C9C91D2B04 for ; Thu, 3 Oct 2024 23:43:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999039; cv=none; b=pOJFpwbv7YQDAWFMG24j8u3IYttoKsPZrBbGP46nThavpldVKSv1SbU66qniBbjzKioUadnlO+MJf4pad6mt42FG6n+TOUezbcZf0IPrr/tMduWYH0tHuTqY4JxMVwOBmAh1ikucADhTyJon8iapl7nX4moNsnucOKndENuoKLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999039; c=relaxed/simple; bh=xqBlrWuS6MCxk0gww17oourN4jowUONsWvqCnk9wPn4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=T/ydsmNs8wz0B+e7HKa1Raal4Iu5i1P6ZLvbEw/qFVmGdedz3xcT1wuqwYZfLfy2tvRXir6QcZu2JoT4ULYavV51jP4NRD5m3wqb+5PZ19fA5dW+qH1Og2Or9I7V610ozRXzBFElW79x3xWumTyhahgdznvd8xCNKAEI5U2EDQk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gnj2GkHD; arc=none smtp.client-ip=209.85.215.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gnj2GkHD" Received: by mail-pg1-f201.google.com with SMTP id 41be03b00d2f7-7e6cc094c2fso2792232a12.1 for ; Thu, 03 Oct 2024 16:43:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999037; x=1728603837; 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=lHVxAupwVdLj9JS6H09y9gDY/v8cy7swDNQclaU5Fj0=; b=gnj2GkHD8H7lDTYPvn9kCqFhDvtxpsIaGjxuYiU9RxwrELTxOjmbGcSLg0lI3OLt5L Ul9h+sC8EK9sVF4hA0f1BiHB1Mmy8h7+CeEzHKOkyl1I9Eb7e9E+shTIQoLXZeNSt6l1 fdOmPQ3mcZfdo5HGiIwbGIeetjrOJnwy9hh1fLlS05YBeGE2dwJFdm3AWQTsmARXlnBq Bogd0xGiLGRoFKJIlj7qpHfTOEhD4Eo6YZF3Q3hw5+Ao9tpEmmJpld8iF9KazCwSFiTe itkM3KiaX+xjwSzNYci0LcGcIuOK87kgNcraXB4kt60/O9bmXVJfPL0spN8Ei5l7i8Ns abNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999037; x=1728603837; 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=lHVxAupwVdLj9JS6H09y9gDY/v8cy7swDNQclaU5Fj0=; b=ZZMiMKRlLFxCxed5/lxy6WTU02Nm3irlUXrUo3AlLqBaCmSrE7ecJWHnmzNebtNkST e4jVaLS46Gn7AG+3D3ihsba74QO6Dl9qbsudk8Q/XuNFfoMLdCMu0IlmEC8qmkWY8Uub pwnhrMiElyZq1vHsgVbWNlkvFC52Yb0/NQN/iwQ8n0vDeu5Gs79OqmKxyZEyREKayKOf 9HS9n68E4QCBziEukglbIxos/Vj7ttbhleQiDBIICsIXoV/QloJ9q6j/2ZPVPOWLmZ9k syFbVOTaZACPwZnnLhi5oh4Kbiw76qjtX8LJroBtUoHOMu5ly9P7M4eCzcN0tQm2yApc mKHQ== X-Gm-Message-State: AOJu0Yx5huOuOIu/cbhWoCrB7pz8v1krMpDj3/sfGfN3C7CgZTISp3s4 bUkZT4CJglE1ERuXWEM0AaJnhSrqREFmns8ZWrf7DMUIgn1v+Ds/UnhiXe/iI5xbVnf68YRxecs W4Q== X-Google-Smtp-Source: AGHT+IGKroOpNriNfmU45AkA6nSG4g4BolcTa1/5RFMDjU2V8x+RltKhqqMU4YNPO1VDq4XZodpXpHwhZw0= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:17ca:b0:2e0:8e0f:7bfd with SMTP id 98e67ed59e1d1-2e1b393e2eemr25111a91.2.1727999037305; Thu, 03 Oct 2024 16:43:57 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:35 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-10-seanjc@google.com> Subject: [PATCH 09/11] KVM: selftests: Drop manual XCR0 configuration from state test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Now that CR4.OSXSAVE and XCR0 are setup by default, drop the manual enabling from the state test, which is fully redundant with the default behavior. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/state_test.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/state_test.c b/tools/testing/selftests/kvm/x86_64/state_test.c index 1c756db329e5..141b7fc0c965 100644 --- a/tools/testing/selftests/kvm/x86_64/state_test.c +++ b/tools/testing/selftests/kvm/x86_64/state_test.c @@ -145,11 +145,6 @@ static void __attribute__((__flatten__)) guest_code(void *arg) memset(buffer, 0xcc, sizeof(buffer)); - set_cr4(get_cr4() | X86_CR4_OSXSAVE); - GUEST_ASSERT(this_cpu_has(X86_FEATURE_OSXSAVE)); - - xsetbv(0, xgetbv(0) | supported_xcr0); - /* * Modify state for all supported xfeatures to take them out of * their "init" state, i.e. to make them show up in XSTATE_BV. From patchwork Thu Oct 3 23:43:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821682 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07E071D2B2A for ; Thu, 3 Oct 2024 23:43:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999041; cv=none; b=TuhnB9buHV7WIDi/QfDE/RYlHxHyBIqmHaVgY7Y6pCTWkJ1qgHo88j9ROdbLEtYW/AzdphuAEB0Ntxsg9SOu8m4pTD7dg//JrgqoxBZwwNOhEY3NL1sMSb2yACVbMY4SdtcynadXc7GK7ClcLV48EV9dBw1T5KFjFzXwLtCrhVw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999041; c=relaxed/simple; bh=BC7/PgFAhuyr7N0SeAVV58DtND5eOdQFwYoP8EUI7lI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=N0DOo2fCBx6MZRdqKFN/owBewa+oFSgPK54r0xEWx/ZZ4d6RJT/rReKzxJ3ByP/F75wmcfhTGnujDCbWxwnITL1eBTPzSRuLit6Pe6trJHR0IJrR+m3+5LvvpPVpOW0AhE0Y+RJZtLFp3VZyLf9Y3z5eZ+TOQwvq23qhOamdH7U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AeSBXKvj; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AeSBXKvj" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71ddfd240d2so650707b3a.3 for ; Thu, 03 Oct 2024 16:43:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999039; x=1728603839; 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=miF0NnrKs/LVUqtwEsnwKwGdVp/l7Rk6nLP0b8R2e6Q=; b=AeSBXKvjerqysEKNtTqIN57t0U5a620aWths1DPeJfPv3lCKzSps0UzOVNf8PTzNFF dFUsljWf+7Om+jHk+5p0BWFJzVrJSgIJ/pj23LVtPGKfIsaGUTC6C0vDFQq8ONX89GkB Xg8mxehdZVMuy6ofMJgX8Lqkdmh/UR5hV7Mw0BW1LedP2zArTe+iEDdFiMJUbIWpdmUf yNT9eprgzcAk5pYzZhEWxU/hEsDFJhW8zo/uPpqLgWpVvu4v/FxNcFKTqMXhsCDb2VGI TyNy6a/4Ksr/0DABLjMiGToMn4dZ94LcML767Zn+MzdYBQOBljCu2sad+YHqeydOy9x8 +xdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999039; x=1728603839; 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=miF0NnrKs/LVUqtwEsnwKwGdVp/l7Rk6nLP0b8R2e6Q=; b=kcaBZr2QCB/CR8yPdZMwUf9p7iCYsUziZtlbBgvq7qAyVo//OuYikOhmfGcQL31ur/ ZIRK73AB4HkV7PBucjRjDklVjzfqoYF41GbIyUTB+AZQmut/e0N8RVSkau+yayi4NxNO 24lbYvhtjKJXmuLIZhXI9ZCXmNSEKimEerKJo/fZ+WUQoFMIMKWJgBb1+5fawwLH+u3z 9fjrlN/voOS2NdMIvW8e6sOIGxn+Wi5GhslrqfPTFoom3i9zhP7T+fR11DkfRJABG0bc zru8RD+pOIzTINCiZQry6OODNctbhYdtDj6y0z/ne9S9ZrUdcqBSfWr3QxFeVvSxXeR6 hXBA== X-Gm-Message-State: AOJu0YwcDxtPPbt9GnhOpHfshQdBTnjvWTpGnpeaEkRYQ+EMrp/Kdzct S1yQZ3F3HHxRgfXXAz7Zp2ZS/8gN8LBgIb+bZfcF5MDWU5xmwKIJIBM1D6jAYRP75jNLwQbB8A3 KAQ== X-Google-Smtp-Source: AGHT+IFxJ85cxWVOAwm7Y/0i5309oldZWB8K0r+VZOLEUf2bRY9vmsB1cfaYlvtoaXQ+SY+jIkaIglV0w3g= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a62:e815:0:b0:717:91ba:b156 with SMTP id d2e1a72fcca58-71de2455097mr8131b3a.3.1727999039232; Thu, 03 Oct 2024 16:43:59 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:36 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-11-seanjc@google.com> Subject: [PATCH 10/11] KVM: selftests: Drop manual XCR0 configuration from SEV smoke test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Now that CR4.OSXSAVE and XCR0 are setup by default, drop the manual enabling from the SEV smoke test that validates FPU state can be transferred into the VMSA. In guest_code_xsave(), explicitly set the Requested-Feature Bitmask (RFBM) to exactly XFEATURE_MASK_X87_AVX instead of relying on the host side of things to enable only X87_AVX features in guest XCR0. I.e. match the RFBM for the host XSAVE. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/x86_64/sev_smoke_test.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index 2e9197eb1652..965fc362dee3 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -41,8 +41,8 @@ static void guest_sev_code(void) /* Stash state passed via VMSA before any compiled code runs. */ extern void guest_code_xsave(void); asm("guest_code_xsave:\n" - "mov $-1, %eax\n" - "mov $-1, %edx\n" + "mov $" __stringify(XFEATURE_MASK_X87_AVX) ", %eax\n" + "xor %edx, %edx\n" "xsave (%rdi)\n" "jmp guest_sev_es_code"); @@ -70,12 +70,6 @@ static void test_sync_vmsa(uint32_t policy) double x87val = M_PI; struct kvm_xsave __attribute__((aligned(64))) xsave = { 0 }; - struct kvm_sregs sregs; - struct kvm_xcrs xcrs = { - .nr_xcrs = 1, - .xcrs[0].xcr = 0, - .xcrs[0].value = XFEATURE_MASK_X87_AVX, - }; vm = vm_sev_create_with_one_vcpu(KVM_X86_SEV_ES_VM, guest_code_xsave, &vcpu); gva = vm_vaddr_alloc_shared(vm, PAGE_SIZE, KVM_UTIL_MIN_VADDR, @@ -84,11 +78,6 @@ static void test_sync_vmsa(uint32_t policy) vcpu_args_set(vcpu, 1, gva); - vcpu_sregs_get(vcpu, &sregs); - sregs.cr4 |= X86_CR4_OSFXSR | X86_CR4_OSXSAVE; - vcpu_sregs_set(vcpu, &sregs); - - vcpu_xcrs_set(vcpu, &xcrs); asm("fninit\n" "vpcmpeqb %%ymm4, %%ymm4, %%ymm4\n" "fldl %3\n" From patchwork Thu Oct 3 23:43:37 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 13821683 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C08531D2F4F for ; Thu, 3 Oct 2024 23:44:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999043; cv=none; b=aEaK93G6YKfyt2g1ljBYnkev4sOiAjhc+CgeVuXxqQpPseouXKpLWLjrYuMAIDTt6xX59LCN0cJm1or0OuQBId5h0OoKaDS/VPvvxPsDpSYuDd5CzgkdmDEHAO248zARArMF2JsAbbcpSmVlBCwQKAb86bwk3l7t8E4djGVGtGA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1727999043; c=relaxed/simple; bh=WxcrHJSR5dRPO5GeLxqc3vQBEoOmpv6JaJwJcRuT2ac=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SuVein7lFrcPluzUN7bo47wPMNwhoquAntrrbwqXaVSflrfl5Gdz3VH33Nge0G/syIK3m9K6c7ME8qc7X0jNW4joRz+vRf55CduOxsLgNX54hfVelRYQDXYTlKjmDTbmrzHhJLVd4nnQr8+TXR5V9hhAfgC0gjWRHCNNSMSJhOI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=aYuke+Ur; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="aYuke+Ur" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e287876794aso2252296276.0 for ; Thu, 03 Oct 2024 16:44:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1727999041; x=1728603841; 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=CDMdVHdl900NNV0/6Aj888t96l8f72iUXOQpGAshdmU=; b=aYuke+UrNk/s/VLRBH1UvMXOMX3/SWIMYIw32is8Cx4UxIIDkU7pXYkQCFNltm7lT9 Adec3dlN90znQ4QdElDKNpGpsbaQf4TLWeXj29qgVgIkqBqrZavWC6H2iIKV//qso3rw 0CpoJKKUHECMwarorxQ1mcys6EAK2vzyK77sgk0AzqvsAJoI0t7lmINtSwn3/20lK5j3 muhLpgcuk19Zw9tjwOPRf3M3wSyBi4LOgNxifu/3Jv+kg+AM9Y9pGQ4bq7pTe+roV0dZ Gi3lXD3bEqnsW3XjW+uqZNoW+hJhu5bOpGmnkdXVty0QB6qWtIBLI1JKqkuvO3rSV4jI AYzw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1727999041; x=1728603841; 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=CDMdVHdl900NNV0/6Aj888t96l8f72iUXOQpGAshdmU=; b=tizW0m5hypokjg2GnBuwLtcxW3yUwj3JO2/aJFFxeFdoUgK6fDs8JRIRReHQcend5W +Sk1aZ627QN15yv4867u7xW2NjD3sNlp3hI/GHpWrQFrdYFEMyQSo5yyFyWc5a5rR6S9 k4bItDMas0Gg8d+PRJaJWgF81HJk1MYS/Ym9NqtXa2Lf9ixd6PK8rvzKowf/JkxSxw/x s/tZAPDVoDNJtQppbJxrlfWGk/Nna0K0tANDXK9n0YENspSBJk4ADKgUaceTysDT0pSG R+KMxW9n4G7oBwJE9/ECk9TLDI7ThyHC0XVnJ+AVsUAo9OkZ/6vGXFMInBNhKOCJgcHw 5v1A== X-Gm-Message-State: AOJu0YzweZgHcjoM+qgqilSdz967p9IrMfq+wZ5yh+foal/V+0n366LE voQgink0X5Mys2wqErhs6dq0M+AjAEk3nTcTiaO2oAHhDF0SiomOCktQ5npDxEgPoNKLRWSyJ0C Qgg== X-Google-Smtp-Source: AGHT+IHoFWU1x0IMqBwd4GgvSk5uqMLFn7or/VSYh3NJinoehg83VQkjefevxGf34pZsKQ4xF4gjbZVNPQ8= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:74c6:0:b0:e1f:eaf1:2254 with SMTP id 3f1490d57ef6-e289393b7aamr1220276.8.1727999040874; Thu, 03 Oct 2024 16:44:00 -0700 (PDT) Reply-To: Sean Christopherson Date: Thu, 3 Oct 2024 16:43:37 -0700 In-Reply-To: <20241003234337.273364-1-seanjc@google.com> Precedence: bulk X-Mailing-List: kvm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241003234337.273364-1-seanjc@google.com> X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241003234337.273364-12-seanjc@google.com> Subject: [PATCH 11/11] KVM: selftests: Ensure KVM supports AVX for SEV-ES VMSA FPU test From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vitaly Kuznetsov Verify that KVM's supported XCR0 includes AVX (and earlier features) when running the SEV-ES VMSA XSAVE test. In practice, the issue will likely never pop up, since KVM support for AVX predates KVM support for SEV-ES, but checking for KVM support makes the requirement more obvious. Signed-off-by: Sean Christopherson Reviewed-by: Vitaly Kuznetsov --- tools/testing/selftests/kvm/x86_64/sev_smoke_test.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c index 965fc362dee3..ae77698e6e97 100644 --- a/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c +++ b/tools/testing/selftests/kvm/x86_64/sev_smoke_test.c @@ -181,6 +181,8 @@ static void test_sev_es_shutdown(void) int main(int argc, char *argv[]) { + const u64 xf_mask = XFEATURE_MASK_X87_AVX; + TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_SEV)); test_sev(guest_sev_code, SEV_POLICY_NO_DBG); @@ -193,7 +195,7 @@ int main(int argc, char *argv[]) test_sev_es_shutdown(); if (kvm_has_cap(KVM_CAP_XCRS) && - (xgetbv(0) & XFEATURE_MASK_X87_AVX) == XFEATURE_MASK_X87_AVX) { + (xgetbv(0) & kvm_cpu_supported_xcr0() & xf_mask) == xf_mask) { test_sync_vmsa(0); test_sync_vmsa(SEV_POLICY_NO_DBG); }