From patchwork Fri Nov 10 02:13:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 163745 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b129:0:b0:403:3b70:6f57 with SMTP id q9csp837938vqs; Thu, 9 Nov 2023 18:16:42 -0800 (PST) X-Google-Smtp-Source: AGHT+IHpyUsvyP9VB1JvGhCJBDMJTVrYFFjShP0ak4Rx7Eb5mNBVCYipuJCT/Kt9vr2mkZTYRUjh X-Received: by 2002:a05:6a00:230d:b0:6c3:871a:7f0f with SMTP id h13-20020a056a00230d00b006c3871a7f0fmr7034408pfh.5.1699582602033; Thu, 09 Nov 2023 18:16:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699582602; cv=none; d=google.com; s=arc-20160816; b=DNOw/K6VVG/4WcI4xY2TW/MXxLJMme+ZQoxF5D3wDXWw5AI/w6u8d3YO74WD9rJKFg 8isMw+7HIlxzE/b1q/DtO9MwIq2effJ2UBog8FjU5xIpZmspsZNksCk6TJpdvfBCDpSI iD8SAn3vvjdEy4QOI/C5iNNG0xuQVJ6oMfh79tACnsj2hfhOPpMlqzsK8D3mECUdyGsT izCBP+zbbhQltbgjN9lnXNOwGEb8YYCzSn05qZhArK6Kkn7l+pil3oFDl0jbf6bPHM7v 2W2NQ8/OAg2j1oFQwlzREzftqoorwMuOx56cvJQcX88HRKQ+z6XRJev803cL6ZOunyPy 8pxw== 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=vea+L5qfDMmA4c3KSY8Nr6t7qY/kdDKuIJUR/t/IBFw=; fh=gm96CM+cG0KJxn7vnhn0c1c98ILNP1HHUrKfrGecw3M=; b=OuszIIGYxOBFp/E72weYk8lHaLNpMz3Uf4pFU1TM8274uOwy5DF3i0LQh8tq+qthcD /x1IjGTC0wt3vJLcNHzlpDjU0nFRQ2cYd8m6CMWyeo5VEEKijU7/zWr9QRws9ZpRceMR uHwMdSThQycg+vXp63oOUlyDEPOH7vqtwC+u2np8sggnb8qS8Mnim3qZzFfnnutt+skW ewPImNNMDmocpI808JJKD+7KGoj/o0uT6jhBjP8H693or/2pg089yMQl24WsRtmZu2kn oTDOYYdnJsUKlRhjJ0NUQVpYJEpDhabka+/ohKrD5Dy+8QN+qq9RzkfdYmjXtlayje1Q HyYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=Zc2J2ZH1; 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 snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id e6-20020a62ee06000000b0068fba6a7375si15387196pfi.321.2023.11.09.18.16.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 09 Nov 2023 18:16:42 -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=Zc2J2ZH1; 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 926D583B2029; Thu, 9 Nov 2023 18:16:00 -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 S1345889AbjKJCPd (ORCPT + 30 others); Thu, 9 Nov 2023 21:15:33 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45162 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345899AbjKJCOy (ORCPT ); Thu, 9 Nov 2023 21:14:54 -0500 Received: from mail-pg1-x549.google.com (mail-pg1-x549.google.com [IPv6:2607:f8b0:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7108F47AD for ; Thu, 9 Nov 2023 18:13:59 -0800 (PST) Received: by mail-pg1-x549.google.com with SMTP id 41be03b00d2f7-5be154fe98bso867802a12.0 for ; Thu, 09 Nov 2023 18:13:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1699582439; x=1700187239; 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=vea+L5qfDMmA4c3KSY8Nr6t7qY/kdDKuIJUR/t/IBFw=; b=Zc2J2ZH1qpKtA1YEdrNCiRvIMuwRxfoHgTwcPWn6GdAniCPnEFXZYuT+lylNfxezPY 9s3YSiPHhGcQ4ml89co19KcXHbeAgivCWDg0V42e+8ALaRoB4bLnhtO3tj6Do2wjisCH ekq0B5+KGYHTY46WxDg7Luh6YXjCt/EU9h0mevUGGggziJxy0WbUTjSqy/vxY87TBXJZ vPrSasPQ9RaaoaI9Hq8S8/O3s4OejvdReeCRY6+hiRaxQZvBZo/MtsG0Ba26zyrXyLCe mFqvAHS8OnZfmEfulbl7ETTQpfbd0FFLpkqqkr2IOLf8VstII/0L5+bO5xrXQF/k+V8G RQkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699582439; x=1700187239; 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=vea+L5qfDMmA4c3KSY8Nr6t7qY/kdDKuIJUR/t/IBFw=; b=GTgubo2Phdj5RKJpvbp5lXPqQcbXCV0PeszWsdVyrldRTOzzIIk6CZpk2XtTOgmD0m 6hKaDBQaIl8VFjWy4+s9HuDcIf/ZN7DDxFZLA1Uo+YuCk3QR67XN7s81UFmGHqG092xS 9xfQBnCWnako/tqGRilM/k4iardkhYMQ1icciNIW2wphKIxUnIikI4zKYtK56DVqdP2B klOO7fWivXS4IdWElMHMPqZL4OKM/fOAOy6ZnCk40YhKaRU8U8Bque0L2nF56/0hwTN4 8OIPLS3ESs+96N3jsyaj2BsFAtDLpRxRg/3gzanaAtvk9CIvqvzGfRW8LWIeEgP2VbTm G3Mg== X-Gm-Message-State: AOJu0Yx8GEd7rcBI813D77NYApnOGKNBEd9t6CzH62MOZNwXfYHy/gZp J9WjfYeqHBrbxPQlm+sgrrbXKSBrCbI= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:90b:2291:b0:280:47ba:7685 with SMTP id kx17-20020a17090b229100b0028047ba7685mr339413pjb.0.1699582438954; Thu, 09 Nov 2023 18:13:58 -0800 (PST) Reply-To: Sean Christopherson Date: Thu, 9 Nov 2023 18:13:04 -0800 In-Reply-To: <20231110021306.1269082-1-seanjc@google.com> Mime-Version: 1.0 References: <20231110021306.1269082-1-seanjc@google.com> X-Mailer: git-send-email 2.42.0.869.gea05f2083d-goog Message-ID: <20231110021306.1269082-25-seanjc@google.com> Subject: [PATCH v8 24/26] 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 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]); Thu, 09 Nov 2023 18:16:00 -0800 (PST) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1782141526681668395 X-GMAIL-MSGID: 1782141526681668395 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 e5c383bd313b..e83b136ca15b 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -1153,16 +1153,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" @@ -1189,6 +1192,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;