Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp636643rdd; Tue, 9 Jan 2024 15:13:40 -0800 (PST) X-Google-Smtp-Source: AGHT+IHQhqhr6cuFKCnJ8A0kgmtQYPTRsq958u5oXn/9ZsKbPwGzv5P6IJ+DOyeEoLxWrNqLaY8s X-Received: by 2002:a05:622a:118b:b0:429:9798:cb8a with SMTP id m11-20020a05622a118b00b004299798cb8amr167315qtk.111.1704842019765; Tue, 09 Jan 2024 15:13:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704842019; cv=none; d=google.com; s=arc-20160816; b=ZnesJOl+Mb+eRrPE8fqqVdYK5mvvmnxD8qzoAuXeLHp7WgOE0tsH8bUEKGyQzIePO7 h9I1D18+QqLVZIDQ3P6RTvtr12xePYGemBLW1Wen0elOx2rD8iEx3B1ILqVW5/RuN0B2 nBPu+8nOyJktzGeWI0Uc3a48FWBwQAe2UZz+g/n2dp0e89sIW7FOVp9iUgQH3DDMUS49 aqrMiLk3kJP9UDZSIEjg2iciN1usZPynHqyBvTXMqD5eVV813IGgE5qimxqdulbnhMJy R3d994AQseImzaMc6uwKkldnLCWwAyeAUmWuRfMgVCQoBbpbWeVfRy0EdPlJc47Enk4U u3JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=cc:to:from:subject:message-id:references:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:in-reply-to:date :reply-to:dkim-signature; bh=08OoRr/gbTB9xGNdl1frY+t6wa0iZxEBlExGG+2stds=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=TWHXRZ4CxgeiOrR+nof0gzk9623Gha74h73+pNtPjOaFlMlzwvO5MkvvNWHp26po3L mUZRHd+p8SAyt5vBcIWNtN6GquIs35g5rG2H0iBOkE4pLCtBU91GJYM2h0zW+Qm8zAVz 2C4keFABF5Ee+g6qIRZ/ovzcCLZydoPe5pw165nOhrZBmybPXcm4kKjWVa0hiVkoYAIa jb1xja6ONlSgcbtrKQlvv9udBy6x9HB4rcFn7djQ0RCYYYlA7loNzyBxBwJyhAQES1DF g/lXF1h9cce05TJVIUlwrObB2LtIKhGQYAxibjNVD8Wnl9d3UlvrL/yFnzbuM1Cu1KW9 L+Yg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ST4c0qte; spf=pass (google.com: domain of linux-kernel+bounces-21517-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21517-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id h18-20020ac85852000000b004299b199763si3054764qth.98.2024.01.09.15.13.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:13:39 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21517-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=ST4c0qte; spf=pass (google.com: domain of linux-kernel+bounces-21517-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21517-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 858941C2162D for ; Tue, 9 Jan 2024 23:13:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 5EA144CE1F; Tue, 9 Jan 2024 23:03:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ST4c0qte" Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 41AFC4CB5B for ; Tue, 9 Jan 2024 23:03:49 +0000 (UTC) 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 Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-28bd4766346so1866247a91.3 for ; Tue, 09 Jan 2024 15:03:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841428; x=1705446228; 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=08OoRr/gbTB9xGNdl1frY+t6wa0iZxEBlExGG+2stds=; b=ST4c0qteUlWOfrfQ0hqN9mQanQILL4cysEKvUDw6TSJZSe74lt3s20RpQIZSurzrJV VS2f6bQM4PI8xQsc8NwO1n4tsbdcPYbjpb0O3+Ot/NXsSFW94HW8OJYXI6TMgdNn3WE3 v0QcQfDch20WguMX+3uwt4cfpNBy2NdF0adZ5ebaQQYbil5SoSGGbvpTd9cl9LLEAUev HjwY3LrFKTDx96Gr0tglVP7G4QbBBKcop6TbMgvpjNnfrARJ2Q/5fDV6iqzPnuoW/srU xzY8tOnyoFJvUZXmH+OSSztM+MzzbBNJUmVwyl4dj31St+GpT4y512qp149kKUGaiDYT r3Xg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841428; x=1705446228; 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=08OoRr/gbTB9xGNdl1frY+t6wa0iZxEBlExGG+2stds=; b=h13eYAip+R76whLOI5Sny54Pozxa/QJP4xvckWKEFLag3K/pXsOqMLDIqeY2azVr5O 8agbIaYwqiofd0dqO1O+yUpV601Wd1zg4uuWoPvBDZ4kMp+poIK7eIdEeLEUo1apjny8 drhsgGaqFmlQ1jlyZK4/i5xB7ZMMK2K59iDsbdBP/gQZnHyd3tvcTvT4VeG6o9yT6qaI Bwbr9T3yJ0bv/PnAOSsULiZ8EWa5s5Aos6w7h1FH7/Psl5ccUItvRL8NxXevbNP+GyRs ZdbdiwKp2aUc4sV3bAC1ULwKaM+dLJJoHn95q0Ux/OjdWa/EVSHEe03F/EAx9Bqj4eUh d6hQ== X-Gm-Message-State: AOJu0YysVuJyGXfjicYDsGfIIrvQKl/E19yS5FuJNQRqgXG+EGISF2EB F6H9Y4Ase8xXK1xcxRr0gVNWQYSeR7uBAwSq0Q== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:4aca:b0:28d:28f5:9b5e with SMTP id mh10-20020a17090b4aca00b0028d28f59b5emr407pjb.0.1704841428700; Tue, 09 Jan 2024 15:03:48 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:48 -0800 In-Reply-To: <20240109230250.424295-1-seanjc@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20240109230250.424295-1-seanjc@google.com> X-Mailer: git-send-email 2.43.0.472.g3155946c3a-goog Message-ID: <20240109230250.424295-29-seanjc@google.com> Subject: [PATCH v10 28/29] 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" 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.472.g3155946c3a-goog