Received: by 2002:a05:7412:b10a:b0:f3:1519:9f41 with SMTP id az10csp1405856rdb; Fri, 1 Dec 2023 16:06:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IG0zlJxP0PNN4uFqnap6P6sGW6AQxj1FHrqMIc7qVQ4p0FwpnTt2am5EnMnI9Ub4dPDcSLz X-Received: by 2002:a17:90a:4cc7:b0:286:6cc1:2cac with SMTP id k65-20020a17090a4cc700b002866cc12cacmr226942pjh.54.1701475607719; Fri, 01 Dec 2023 16:06:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701475607; cv=none; d=google.com; s=arc-20160816; b=QfCt3E2YnWeqTGJv5Aftjq+4/eJNJy48omsllfh0oESDqtcT0/BwiZ89V+e1FPI1JZ LCTrgwRHztbud4YOTcAL/Ura+VtEBB5LpNXolQPbYlpCz7ev/yatXtWgsSupN2KbiaHL 446gkvrkOCEXi55nUcbdwsRplq+xu225/4euzC5F+qItQMKHH5CaTjp9efHeFn8iO+D9 KiOUEzsDzqxCaufxfKJdfa9LJtB1jQqr8dF4p5+6EY3jwZpipkEG+G3BvXR4fDj23KFv vETDfvCmCj7KP9z8LhKhTNrohdW9VBUqbVcsHDg5+/63+hDF595U6WInbkG2Gto1zjSA N2Rw== 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=sn5K0TUz/ND3xDsLXUagKvVZbAXVyeU2LAAk3nLknAQ=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=CxE0hU8vtoS2L6JtRr72PhL03+hxUA3oEdBYis+RCAEeYTb9mRqdmnuk6uY9IudK5w nvTXuYQ2yGVsX8hdxdJNKOQ8sy1DmzYrWFbkoGi/yrWYqrlNqTUo7OUqTO3j/gW3y1YL 290v9jTMYJErmxKj7Cg8cB8gWZ6gsRTbkJVitwmVWWJ9t1QXO/g1srSCAKswS57yovUu CE34M6/RT9hBmvO0y8D++5Tf22RdRYiTAei5kzQmOAuoOf/MembcTYv51C1mHgXYfvAU 2/O7ff7t//xdwYUfLtUcDmcHojo/5n16CHPkF8dwAJhhCmvNhPl4uc2m25UA3Ah8z69W VKPw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=je2wEPOp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id i10-20020a17090a974a00b0026b365096e5si6364059pjw.53.2023.12.01.16.06.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Dec 2023 16:06:47 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=je2wEPOp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 5E13781896CC; Fri, 1 Dec 2023 16:06:46 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1442159AbjLBAGb (ORCPT + 99 others); Fri, 1 Dec 2023 19:06:31 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42602 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1442124AbjLBAFl (ORCPT ); Fri, 1 Dec 2023 19:05:41 -0500 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AAD2F199F for ; Fri, 1 Dec 2023 16:05:13 -0800 (PST) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5c87663a873so39602697b3.2 for ; Fri, 01 Dec 2023 16:05:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1701475513; x=1702080313; 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=sn5K0TUz/ND3xDsLXUagKvVZbAXVyeU2LAAk3nLknAQ=; b=je2wEPOphLZAZjfRFJNa2KF5G1I4U0vFNbnTo2yoPrB2K8cpdwT9P/oOWhZTPaw28n uQhAAY9AC9BJWydnxGtq5mx6FTmg/V5USlEPqI0eejjscvipmMKPFGt03BHraLShbQoP BTEE3n+cOLBPLNd7mpE69gTUf7op+PrbpomO94yyvcqrN9Kl7OsGikkkfOMsnCu2XKoh au55mTq38RQENeuktx7odaQq9lh3R+ZZ82eG3Mgh4MRh9C2t0WB358rDnibvG8jgt25g 0VRtVNx9YzSmVRmnp4M/OMojZmyoWBmLeDsAl1W0av6WKqF9gygN8wv2xg3NfuXG3bwt RdPg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701475513; x=1702080313; 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=sn5K0TUz/ND3xDsLXUagKvVZbAXVyeU2LAAk3nLknAQ=; b=EsD3CZeEoU00PV7bY3mQ+FcPbQ1gCCtUXumttlR4eKdlqE4G1oqO8ZKiR1sbKZXvzR Bqq/FFEsbPGzt1H19jE0Ghkjycb3MBGJhqvj2AS/eeJeGpG7zAbmluI2ZMMTZkIMfqbZ WV3YKzw/1BKQn1WYdOaGVZY2sQUWTxs3IqBrnsKRFt+M+cj9YgzWk+QUEe30e7Vtzy68 ijCt0Vziin0zoOdeSAiB/HrKj5sZWT4U9QZOpxhZB2zsgo7VgPoP1xAf//dosy67HU0L zpOeZcyhE6ir6F15/dmUAWt3qtmU/lza90po+WECQ0vTo1Hc6YD5G/hoK8J9wULTP685 8eZw== X-Gm-Message-State: AOJu0YxFkalzKG0D3IifnWQoOUlxys1FgbNdVMHoqNiICCWfJcGBXMNT B/ZsMrZRH0rRGs/O/cAd1lcZ1jEI1pc= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a25:d6d1:0:b0:db7:dce9:76d3 with SMTP id n200-20020a25d6d1000000b00db7dce976d3mr11991ybg.9.1701475512974; Fri, 01 Dec 2023 16:05:12 -0800 (PST) Reply-To: Sean Christopherson Date: Fri, 1 Dec 2023 16:04:16 -0800 In-Reply-To: <20231202000417.922113-1-seanjc@google.com> Mime-Version: 1.0 References: <20231202000417.922113-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.rc2.451.g8631bc7472-goog Message-ID: <20231202000417.922113-28-seanjc@google.com> Subject: [PATCH v9 27/28] KVM: selftests: Add helpers for safe and safe+forced RDMSR, RDPMC, and XGETBV From: Sean Christopherson To: Sean Christopherson , Paolo Bonzini Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Kan Liang , Dapeng Mi , Jim Mattson , Jinrong Liang , Aaron Lewis , Like Xu Content-Type: text/plain; charset="UTF-8" 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_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=ham 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-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Fri, 01 Dec 2023 16:06:46 -0800 (PST) Add helpers for safe and safe-with-forced-emulations versions of RDMSR, RDPMC, and XGETBV. Use macro shenanigans to eliminate the rather large amount of boilerplate needed to get values in and out of registers. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index fe891424ff55..abac816f6594 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1216,21 +1216,35 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, vector; \ }) -static inline uint8_t rdmsr_safe(uint32_t msr, uint64_t *val) -{ - uint64_t error_code; - uint8_t vector; - uint32_t a, d; - - asm volatile(KVM_ASM_SAFE("rdmsr") - : "=a"(a), "=d"(d), KVM_ASM_SAFE_OUTPUTS(vector, error_code) - : "c"(msr) - : KVM_ASM_SAFE_CLOBBERS); - - *val = (uint64_t)a | ((uint64_t)d << 32); - return vector; +#define BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \ +static inline uint8_t insn##_safe ##_fep(uint32_t idx, uint64_t *val) \ +{ \ + uint64_t error_code; \ + uint8_t vector; \ + uint32_t a, d; \ + \ + asm volatile(KVM_ASM_SAFE##_FEP(#insn) \ + : "=a"(a), "=d"(d), \ + KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ + : "c"(idx) \ + : KVM_ASM_SAFE_CLOBBERS); \ + \ + *val = (uint64_t)a | ((uint64_t)d << 32); \ + return vector; \ } +/* + * Generate {insn}_safe() and {insn}_safe_fep() helpers for instructions that + * use ECX as in input index, and EDX:EAX as a 64-bit output. + */ +#define BUILD_READ_U64_SAFE_HELPERS(insn) \ + BUILD_READ_U64_SAFE_HELPER(insn, , ) \ + BUILD_READ_U64_SAFE_HELPER(insn, _fep, _FEP) \ + +BUILD_READ_U64_SAFE_HELPERS(rdmsr) +BUILD_READ_U64_SAFE_HELPERS(rdpmc) +BUILD_READ_U64_SAFE_HELPERS(xgetbv) + static inline uint8_t wrmsr_safe(uint32_t msr, uint64_t val) { return kvm_asm_safe("wrmsr", "a"(val & -1u), "d"(val >> 32), "c"(msr)); -- 2.43.0.rc2.451.g8631bc7472-goog