Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp636454rdd; Tue, 9 Jan 2024 15:13:12 -0800 (PST) X-Google-Smtp-Source: AGHT+IGo2/H79rRuuZ+jkbPRzbhvXZijHGgLDLwilLcjP6PLuMi2zkuAa2xQWbl2DbXvjHllQOye X-Received: by 2002:a05:6214:23cc:b0:67f:5784:bf26 with SMTP id hr12-20020a05621423cc00b0067f5784bf26mr289915qvb.47.1704841991947; Tue, 09 Jan 2024 15:13:11 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704841991; cv=none; d=google.com; s=arc-20160816; b=Mg0CWsRkLP5ax4JXwzbycT2cYoT1+KPg+eXeO1/kyZyc1zmoo8JSj/YLIjdcYa9MOe 59HvTFYGIal3gN3/U3LJ5pJlUBL02LPXjEPhrMcijNVgAVSTTe10unHpSr4AZ0SEY8uD Y3WpzhyEygXJ1F9M0WMSW8Revw1+bBsy/Hjp4fAu5TUltapiJRQ5AxmXf/LNkm5yaw9F nnddOMx38sk4/unFZALP/abdPQLXaKSOyOyxC921Ho05NAT6LYWo7GWoAp6rf+yWXrOO Ba+SyGlbBYVTr1iZ0VBwyRsIJ2UvhZ/sKil1MyP+AuRKwL9plA7iTQCcDW+QWNOqIjaT RCgQ== 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=wANFkcf3/mrpI/C7ga3lU3ObqtiYxLFAZO6wmonones=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=FP8gqi7/G7TPSmIZelY0FiboOjcImYB/22dD3VjphXO8zOsN1hQeZbQnOA/hq6lQNU SVbHwOKKCMnR5V7cbTkNeTv870fTXDHmEGy3IeGL2rMCSsTUkppfoLS2GBVcqwSiLwn+ 0DHIrp6cOjJkbkAKnjUf+tLsAL27QNyTMyY/3IIGnAfZYmXA23OOaV+s/CSVRdPCNvk5 2IooB1GKn0Ky6Iwo/IWlZH3vV3k11mq4bdkBhcHwWDO4vSNtTGcgHW/+w5gFvC60++pw 12pxqo4iTbq4PSU6u1UVT0AyQAiy7vwMgS2k7rM/neC+Z5x2W5FL53x3arpwT1dYz+XL pSgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=lg7IKhNr; spf=pass (google.com: domain of linux-kernel+bounces-21516-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21516-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 a20-20020a0ca994000000b006800edeb294si3257524qvb.213.2024.01.09.15.13.11 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 09 Jan 2024 15:13:11 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-21516-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=lg7IKhNr; spf=pass (google.com: domain of linux-kernel+bounces-21516-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-21516-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 A2F9D1C24FE5 for ; Tue, 9 Jan 2024 23:13:11 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6ADFF4CDE1; Tue, 9 Jan 2024 23:03:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lg7IKhNr" 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 613344CB25 for ; Tue, 9 Jan 2024 23:03:47 +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-pf1-f201.google.com with SMTP id d2e1a72fcca58-6d9c7de0620so2747259b3a.1 for ; Tue, 09 Jan 2024 15:03:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1704841426; x=1705446226; 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=wANFkcf3/mrpI/C7ga3lU3ObqtiYxLFAZO6wmonones=; b=lg7IKhNrHY24Mgh2hctYvI6Ypg71cSimjNs3TvcS6+rjsXm88VyScerW2kH/N6VBqn ZUmd582XcPsjj3UJXgKTTz6nMF5v8JxCHeeTixL53nYdhPCsao+dH7m2GWxfAq3toM/+ 1A/lQRoXRkVyBeYlctbKMkQY9N4TIYnU3Suh5xOoxxrvv4ZeeLvR5kawqZQxNwtOeC39 LL+M1cBpLboUR16ZtVhD+SWckFLSr6XKvjHaGW2AZ/W80PrfxCEJqyTN/CSC/MzV2YzC ueFPsisdoZtqYjYEBGKFMO3PaAP9JAYMlZmWqTp/GcfCb1gYIdtXSEAFI8Hif50JNyoO Xkuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704841426; x=1705446226; 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=wANFkcf3/mrpI/C7ga3lU3ObqtiYxLFAZO6wmonones=; b=LaUucB3CMlurahGIQEcUjEJd/IvWvjNBo0+8QSFPjxyhjMGzmJUCDGOlThXHNiK9Uj JUa7hk9OGCTh3BWCX0RRWpyPV1HQ4EJyRjPetstL0LLq6S6p4JVxsNseDuntmbqQ4HcH HOqS+vcECx6BeVc6aNg5NKE/6zRsypof/+8N3fTHDKzVoBv7cX3bVwTFiOlnTJJx0a6T tjhNso5eb4Aa8G/SABrGxNtnC4PyMrY1VtDksRg8r31XC2IWKVPBXqTnUF/5m3ixPtBN nmC8sRrfDaqqSW88VLqr7nBwr3yaxc46uZxUDlQsV0+hlPgGNpxZA9Pt7edF0O9L48L/ qr+g== X-Gm-Message-State: AOJu0YylB8VJBCbSti70LNYoui+HBUtyH+mylU5Fi4SfaBLbrY8sXRUd un4thmHHZN5qJ+ntE6iuS7+bi3VqqvcNZt/Q5g== X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6a00:1896:b0:6da:bf5b:bd4e with SMTP id x22-20020a056a00189600b006dabf5bbd4emr27897pfh.3.1704841426743; Tue, 09 Jan 2024 15:03:46 -0800 (PST) Reply-To: Sean Christopherson Date: Tue, 9 Jan 2024 15:02:47 -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-28-seanjc@google.com> Subject: [PATCH v10 27/29] KVM: selftests: Add a forced emulation variation of KVM_ASM_SAFE() 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 KVM_ASM_SAFE_FEP() to allow forcing emulation on an instruction that might fault. Note, KVM skips RIP past the FEP prefix before injecting an exception, i.e. the fixup needs to be on the instruction itself. Do not check for FEP support, that is firmly the responsibility of whatever code wants to use KVM_ASM_SAFE_FEP(). Sadly, chaining variadic arguments that contain commas doesn't work, thus the unfortunate amount of copy+paste. Signed-off-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 6be365ac2a85..fe891424ff55 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1154,16 +1154,19 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, * r9 = exception vector (non-zero) * r10 = error code */ -#define KVM_ASM_SAFE(insn) \ +#define __KVM_ASM_SAFE(insn, fep) \ "mov $" __stringify(KVM_EXCEPTION_MAGIC) ", %%r9\n\t" \ "lea 1f(%%rip), %%r10\n\t" \ "lea 2f(%%rip), %%r11\n\t" \ - "1: " insn "\n\t" \ + fep "1: " insn "\n\t" \ "xor %%r9, %%r9\n\t" \ "2:\n\t" \ "mov %%r9b, %[vector]\n\t" \ "mov %%r10, %[error_code]\n\t" +#define KVM_ASM_SAFE(insn) __KVM_ASM_SAFE(insn, "") +#define KVM_ASM_SAFE_FEP(insn) __KVM_ASM_SAFE(insn, KVM_FEP) + #define KVM_ASM_SAFE_OUTPUTS(v, ec) [vector] "=qm"(v), [error_code] "=rm"(ec) #define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11" @@ -1190,6 +1193,29 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, vector; \ }) +#define kvm_asm_safe_fep(insn, inputs...) \ +({ \ + uint64_t ign_error_code; \ + uint8_t vector; \ + \ + asm volatile(KVM_ASM_SAFE(insn) \ + : KVM_ASM_SAFE_OUTPUTS(vector, ign_error_code) \ + : inputs \ + : KVM_ASM_SAFE_CLOBBERS); \ + vector; \ +}) + +#define kvm_asm_safe_ec_fep(insn, error_code, inputs...) \ +({ \ + uint8_t vector; \ + \ + asm volatile(KVM_ASM_SAFE_FEP(insn) \ + : KVM_ASM_SAFE_OUTPUTS(vector, error_code) \ + : inputs \ + : KVM_ASM_SAFE_CLOBBERS); \ + vector; \ +}) + static inline uint8_t rdmsr_safe(uint32_t msr, uint64_t *val) { uint64_t error_code; -- 2.43.0.472.g3155946c3a-goog