From patchwork Fri Feb 10 00:31:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 55157 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp660837wrn; Thu, 9 Feb 2023 16:34:17 -0800 (PST) X-Google-Smtp-Source: AK7set8KfvdLzzViHmkLvuCYfFzDYlE2QT6nEDjVuh7d8RHGcBYhZ6tkRFCZRM1cJKYfml6SvAU3 X-Received: by 2002:a05:6a00:4399:b0:5a8:4c8a:2ce4 with SMTP id bt25-20020a056a00439900b005a84c8a2ce4mr5184921pfb.3.1675989257219; Thu, 09 Feb 2023 16:34:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675989257; cv=none; d=google.com; s=arc-20160816; b=ZQH6eYFnd8YokszR/GS2s8OVQb5ydySa5zVdLTdrV0f71zINR6Og2fZReJz0AeKPyf 2+XU+3ssIhyRySMjTMpWGWBCvO7Itxmho/7BZyv5tgYFMnEFpBYNRrP5ntthPvSiJbgj nFKm73Cu7BIG3tTLfn7JVkxnZHNDVORw0qnMUq33kVQzkBOue5ugaIzSTh7yvmQwCFIA pWN4Yo05clx8kSBKRjmeHskyG+CjJmxE7V1gVWBCz7CKcYnFTgHDvMwXKDh1WQhPji/a Al38CT/CkxJJPKMBOIQctxTCsTcJe4xma9eGQ/fqicnb1oWySr3wK8T0BhDi2ube4lc6 YgBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:reply-to:dkim-signature; bh=ErxDS8Mt98Bh2G42hGdy1a7/D5r7Doi1oWDwj8D/bzs=; b=ErNW7bcC0CEAqFMlVz78OAeZa4uVNLP4r3OHZycklwmPtkAJsKgM4fs8UhevHsx08a jhgFEJEGx+P3XbpLPPC7j8lYSDDQ1A0Bq9drYVA0Bc971IxxYhk/rMf9ktjHX8k0Wmmf tQ4+8ZKra4CgRcxZpgFgJtfSeqFkEjfMJRDa2602qq2PiTpHIRL2dyEHdUVVjsaDotCA 8AdNsnxa8Oh4qvx4xm4rAkaSZvqS0OlML0nnto8rnDZe2o7jcAimEWb1A5qS/7oLrA8j EhbGAbh7bhaR6SJ322QkhRUDGHf+vPDyvvA8dERwpuTlW+v04v/u0Mp5QbZe4ZwxzvrZ 40xQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=USF0vWnw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w189-20020a6262c6000000b0058d9b9c045asi3049335pfb.84.2023.02.09.16.34.04; Thu, 09 Feb 2023 16:34:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=USF0vWnw; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230502AbjBJAdS (ORCPT + 99 others); Thu, 9 Feb 2023 19:33:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53166 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230183AbjBJAcs (ORCPT ); Thu, 9 Feb 2023 19:32:48 -0500 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 86B5D6F8DA for ; Thu, 9 Feb 2023 16:32:12 -0800 (PST) Received: by mail-pg1-x54a.google.com with SMTP id o19-20020a63fb13000000b004fb5e56e652so235283pgh.9 for ; Thu, 09 Feb 2023 16:32: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:reply-to:from:to:cc:subject:date:message-id:reply-to; bh=ErxDS8Mt98Bh2G42hGdy1a7/D5r7Doi1oWDwj8D/bzs=; b=USF0vWnw8XD6xBLgfR077GlxM3M8cLOgPzJzS6EyFsuX6Tz8IHUkS8VPCkzqBTmHvN Ykf+xRwHERwEpqaivb5knRZXK7+fGnzrSTi1XrZyxqR6Q60BkOI8CM6b8JSh8DB/R0pH 4H+7XX84Ax3fgVPq9kk8MqBvtc17sRz60GvWG7PKDZvy+cW3DHPNk8LGk+rXZ8EdP3MA 1LvirwcUYAWpFeiz8VOvBXd7t1XiX98omtxZerYmngSI8lgMBQDUa3czCiFm3tSiQEYb GrAkqew/EPp8L8AGnDCCHBmFWmectV5S3uIvrtJ0HkEuAQGhWqPNYAlIN4dHYb12rJRX cqIg== 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:reply-to:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ErxDS8Mt98Bh2G42hGdy1a7/D5r7Doi1oWDwj8D/bzs=; b=kZP06VtT65xj/DEjpDceuA7hLa8dXDrYl9nqrOy/22DGWHJo3c24rj16yQsfD6uvoQ ZHbKmafCvyIVYxRZPFWpdJ7kbvfF+sz5PNwQC9LBCsO52fUCPp5ovyGS2QBcSBDD7ZYu PgWnLmaikGdMJ5V3bGT1z6n0pyCPJTv6cd8vnvN5BZtB+2DX/CY+YdbtHRmYhIzlQEMI lrN8e1lsy30X6gD438mGHe49QRzMtVBRjFYxnbYe/gDJ2pfvWimd5lV0ABgYF6Piudu9 ULW+Pji7ZSJEOahXprTkCqH7Pfnpb9UhfPNYtjZHjbBjJ47jq8cuHxzXRHYwZTsiN5hO SfLw== X-Gm-Message-State: AO0yUKXWwU/hCExkFUXyItAda5MNLsO+TCOgk/kTO0oUEALFaJffjOix h1Y/KgHutXZQz3iF/dEHOQlsiS1aElA= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90a:6b06:b0:22c:266c:2c45 with SMTP id v6-20020a17090a6b0600b0022c266c2c45mr1763828pjj.49.1675989131403; Thu, 09 Feb 2023 16:32:11 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 10 Feb 2023 00:31:39 +0000 In-Reply-To: <20230210003148.2646712-1-seanjc@google.com> Mime-Version: 1.0 References: <20230210003148.2646712-1-seanjc@google.com> X-Mailer: git-send-email 2.39.1.581.gbfd45094c4-goog Message-ID: <20230210003148.2646712-13-seanjc@google.com> Subject: [PATCH v2 12/21] KVM: selftests: Verify KVM preserves userspace writes to "durable" MSRs From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Like Xu X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1757402110950730256?= X-GMAIL-MSGID: =?utf-8?q?1757402110950730256?= Assert that KVM provides "read what you wrote" semantics for all "durable" MSRs (for lack of a better name). The extra coverage is cheap from a runtime performance perspective, and verifying the behavior in the common helper avoids gratuitous copy+paste in individual tests. Note, this affects all tests that set MSRs from userspace! Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 26c8e202a956..52260f6c2465 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -945,12 +945,27 @@ do { \ } \ } while (0) +/* + * Returns true if KVM should return the last written value when reading an MSR + * from userspace, e.g. the MSR isn't a command MSR, doesn't emulate state that + * is changing, etc. This is NOT an exhaustive list! The intent is to filter + * out MSRs that are not durable _and_ that a selftest wants to write. + */ +static inline bool is_durable_msr(uint32_t msr) +{ + return msr != MSR_IA32_TSC; +} + #define vcpu_set_msr(vcpu, msr, val) \ do { \ - uint64_t v = val; \ + uint64_t r, v = val; \ \ TEST_ASSERT_MSR(_vcpu_set_msr(vcpu, msr, v) == 1, \ "KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v); \ + if (!is_durable_msr(msr)) \ + break; \ + r = vcpu_get_msr(vcpu, msr); \ + TEST_ASSERT_MSR(r == v, "Set %s to '0x%lx', got back '0x%lx'", msr, #msr, v, r);\ } while (0) void kvm_get_cpu_address_width(unsigned int *pa_bits, unsigned int *va_bits);