Received: by 2002:a05:7412:40d:b0:e2:908c:2ebd with SMTP id 13csp349739rdf; Tue, 21 Nov 2023 04:43:22 -0800 (PST) X-Google-Smtp-Source: AGHT+IEyHP8sliZH/ThxnL5YwOjSAXz1Z/XjW1k5QUMxm3+do50+DPKeuhYXJtifdLWYaGimncY7 X-Received: by 2002:a05:6808:2094:b0:3a7:6d64:aa68 with SMTP id s20-20020a056808209400b003a76d64aa68mr13670066oiw.18.1700570601805; Tue, 21 Nov 2023 04:43:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700570601; cv=none; d=google.com; s=arc-20160816; b=L73sp2ljDp6QNYxzHYPQyhSo3WE9XGNs1DMVy7F4QtHqDkhbGC/RecG7TtFhep1ApU 7HukMThLOo4vZQUG547JLO/FQB+jkbr5UcvNhdcmUDWJxzaiZSxDKFZxR72Nzs775GMx BtVgTh1c0G5gjLoQMgLXTmXp+M3frfj9KXW5fLX9kpfzg92UrXGbRLQ+nuXuOQvdwY+d RJ+CoTjI4PKtBjpyjOuCRIyp/ilOrQmW79aobE1/Jm6ngp/C4RgBAbMzCRs/iovzLgdl DKo0VXSicUUjlvU4Uv3p2GbI3UX6awQ/A1UY+tynmMUrkEtwmXlZJZeuww1MTcvo6aWj ALpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=kGrm68QoMUrup/7Rp3nNNkMcX2hX6/h9dwhr7ZWgjH8=; fh=sIMbrHW6XchtO8J++xKMvfnuMh37piioh5cqciNSK/o=; b=DkWef639R9eW4ch+uuXTUZxMQAVqsyGiZs0z1L8NwdL1R2FcmUyExlE5FA9y114GH5 TzDMb2blxd3KeOOPP/632AeigKxkJM3K8TuI4lnT1l74jAdQ8YDlNd4cf/lkEPcoNts2 oN+FC5HvUUPA37KggsKLgYt7FmEPR/SYEhY8dEuJBuloulATCipY3UZuVLTxce2P5th2 2okmiQRHutJe3pngAQMjOQ3MRc9hvsFuSa+y+AoU9SX2rx62zEmc5WwqqEWAUAObWiAA 2kRm0kIjxCH0APHLPZftWP+B9wHIFKWXFV/8ijph8TesipV5+yIyfxWIZzukCqO6Tils aW2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=EJCR4V3I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id n9-20020a634d49000000b005b9b68add89si10022209pgl.84.2023.11.21.04.43.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 04:43:21 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=EJCR4V3I; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 15FC78027E20; Tue, 21 Nov 2023 04:43:09 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232764AbjKUMmp (ORCPT + 99 others); Tue, 21 Nov 2023 07:42:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234466AbjKULzj (ORCPT ); Tue, 21 Nov 2023 06:55:39 -0500 Received: from mail-pl1-x642.google.com (mail-pl1-x642.google.com [IPv6:2607:f8b0:4864:20::642]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BE2918E; Tue, 21 Nov 2023 03:55:35 -0800 (PST) Received: by mail-pl1-x642.google.com with SMTP id d9443c01a7336-1cc9b626a96so40037875ad.2; Tue, 21 Nov 2023 03:55:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700567735; x=1701172535; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kGrm68QoMUrup/7Rp3nNNkMcX2hX6/h9dwhr7ZWgjH8=; b=EJCR4V3I+dsuU7B1YHri5Mr0Ew79yoP5mZnDWFtmoEieWIvBhXLRgzQE6NJrMc/6Yj 8VLJbXtnKL0nnuxoxUubkO5oMoSOZ/3z7LcZUlBEPGgIV9aYyUGhM29peZAt4xBLCQMe dUN+axiqYEssRyELE4KjJpyNeBb9quX6bOG131ng3CXf+paKZUb2oLmUNl1TY3z50cp3 6cFgSRUNTAR4Rtcb+vSWr2d9kUlJhg+LWUoH0FvtO8TofmnvdmXhrkteGZNKoOsj3jcI bVIK5wUZtymwVMGq8VLTcivAv2vi4Urb9pTHXe8OqcC06t6x2kE94AE45LNJP3st8oPj OYUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700567735; x=1701172535; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kGrm68QoMUrup/7Rp3nNNkMcX2hX6/h9dwhr7ZWgjH8=; b=lZ+3tynVBr4EEI8CyznR8hR7H6M1rJGDeyDQ3YcCmjg2hVGGi293jqbLfjfFihGhSY n5HiBfeczRDMe0ZQd9s5c5HsSwxrVHtjO0vHWlcokiM/iXP1f83zukoeUj8IVaARKwRl PLj0I0j1u7IktBKcORk0EW4QZDUFIFCk7F9033/CYMflRqoanF3DW0fq25ahyShBzZqo zgQWQmuDgZNdlxtGliGs6HlsNG9sp/1A+z5p81nqm6p/Ly7ayweYoranZSyoLOaXnWgu 6yQiyunVo57v/zMs1xhQZ/Go0XMKxFWlkUPpRlZAObd16no3/siUkftrVD66OlAa/rTX QoiA== X-Gm-Message-State: AOJu0YwFPX6klCQNTg7syppjXZVmA+r6huIDb3wbNkt4/HJts6O2mSbb j01ht2tnkNg2BZgJQTtvnZ+mfaGo08usS8S7Eno= X-Received: by 2002:a17:902:f7d1:b0:1cf:6e3d:d8e7 with SMTP id h17-20020a170902f7d100b001cf6e3dd8e7mr1445161plw.26.1700567734754; Tue, 21 Nov 2023 03:55:34 -0800 (PST) Received: from localhost.localdomain ([103.7.29.32]) by smtp.gmail.com with ESMTPSA id g10-20020a170902740a00b001cc1dff5b86sm7685431pll.244.2023.11.21.03.55.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Nov 2023 03:55:34 -0800 (PST) From: Jinrong Liang X-Google-Original-From: Jinrong Liang To: Sean Christopherson Cc: Paolo Bonzini , Like Xu , Jim Mattson , Aaron Lewis , Wanpeng Li , Jinrong Liang , Jinrong Liang , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/9] KVM: selftests: Add forced emulation check to fix #UD Date: Tue, 21 Nov 2023 19:54:49 +0800 Message-Id: <20231121115457.76269-2-cloudliang@tencent.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231121115457.76269-1-cloudliang@tencent.com> References: <20231121115457.76269-1-cloudliang@tencent.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 21 Nov 2023 04:43:09 -0800 (PST) Forced emulation uses a magic "prefix" to trigger a #UD, which KVM intercepts. If forced emulation isn't enabled, KVM ignores the magic prefix and reflects the #UD back into the guest. Reported-by: Jinrong Liang Signed-off-by: Sean Christopherson Tested-by: Jinrong Liang --- .../selftests/kvm/x86_64/pmu_counters_test.c | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c index 248ebe8c0577..7d8094a27209 100644 --- a/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c +++ b/tools/testing/selftests/kvm/x86_64/pmu_counters_test.c @@ -325,6 +325,26 @@ __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector, \ "Expected " #insn "(0x%x) to yield 0x%lx, got 0x%lx", \ msr, expected_val, val); +static void guest_test_rdpmc(uint32_t rdpmc_idx, bool expect_success, + uint64_t expected_val) +{ + uint8_t vector; + uint64_t val; + + vector = rdpmc_safe(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); + if (expect_success) + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); + + if (!is_forced_emulation_enabled) + return; + + vector = rdpmc_safe_fep(rdpmc_idx, &val); + GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); + if (expect_success) + GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); +} + static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters, uint8_t nr_counters, uint32_t or_mask) { @@ -367,20 +387,15 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters if (!expect_gp) GUEST_ASSERT_PMC_VALUE(RDMSR, msr, val, expected_val); + /* + * Redo the read tests with RDPMC, which has different indexing + * semantics and additional capabilities. + */ rdpmc_idx = i; if (base_msr == MSR_CORE_PERF_FIXED_CTR0) rdpmc_idx |= INTEL_RDPMC_FIXED; - /* Redo the read tests with RDPMC, and with forced emulation. */ - vector = rdpmc_safe(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); - if (expect_success) - GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); - - vector = rdpmc_safe_fep(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, !expect_success, vector); - if (expect_success) - GUEST_ASSERT_PMC_VALUE(RDPMC, rdpmc_idx, val, expected_val); + guest_test_rdpmc(rdpmc_idx, expect_success, expected_val); /* * KVM doesn't support non-architectural PMUs, i.e. it should @@ -390,11 +405,7 @@ static void guest_rd_wr_counters(uint32_t base_msr, uint8_t nr_possible_counters GUEST_ASSERT(!expect_success || !pmu_has_fast_mode); rdpmc_idx |= INTEL_RDPMC_FAST; - vector = rdpmc_safe(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); - - vector = rdpmc_safe_fep(rdpmc_idx, &val); - GUEST_ASSERT_PMC_MSR_ACCESS(RDPMC, rdpmc_idx, true, vector); + guest_test_rdpmc(rdpmc_idx, false, -1ull); vector = wrmsr_safe(msr, 0); GUEST_ASSERT_PMC_MSR_ACCESS(WRMSR, msr, expect_gp, vector); -- 2.39.3