From patchwork Sat Jul 29 00:36:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sean Christopherson X-Patchwork-Id: 127929 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:918b:0:b0:3e4:2afc:c1 with SMTP id s11csp788610vqg; Fri, 28 Jul 2023 18:27:12 -0700 (PDT) X-Google-Smtp-Source: APBJJlFA8oZ1CYtD/k+CKy8+yw5a2J/XwIAqODyv90yKOo8nDXWYU9OsAtINCf6SkbNBP2ClwbY7 X-Received: by 2002:a05:6a21:27a4:b0:134:8b50:47cd with SMTP id rn36-20020a056a2127a400b001348b5047cdmr2857891pzb.9.1690594032098; Fri, 28 Jul 2023 18:27:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690594032; cv=none; d=google.com; s=arc-20160816; b=UqKtYUFmD9HZLo7OQBiNFpt6jU1ulufAa9XHdw6wy7Qj1eVyxHjorJhtTw06qTwkFD m5uPkzhFN2zcDUoxp/XgysnIk/BKKA8/8l2l6VRAH6YgT6wcsC8J9KTt18yThG4jsjow VjyADym2HJ3dLoc4Y+4YZQBKyfpaA09EB0YEgNQfFuPQe6aAaYnpiWNNXt+n4QAqr1WF 5AZGlTc9RPpUhs+rIfkoBPMFxTHmmt8P5PAqELB+wck/GMlherS6KKooqZhu8YI+jOsO y/NfgIuFN+EolyDm0jDL5gzx18n/jaBNanPB6budSWWUNgnFRhYvpnihuX6mvjvMpwJk IvkQ== 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=jjZUurEgx8+1WTff2yy2mIJojJc3waWamxg7HHxYGIg=; fh=Ctt0ydInMKwei8YFBFbw7rU4p8H0G/T51ZI/KkO6Md4=; b=TOzAzLMeQju3B1wXZbtrKn1e9cl+Nxx7uV2q1knTRKLrSTVhnJBM9+T3g38SSZqy1/ sBj64EzakczqzXHhkllWjsJEVVZ/FngtqdgmR6Ba4eXpe9TurRqNOfBKpk2wH46UBAEN avN7YJhziQi+/c3LY8GzAQiqRindoPwFdBDpHSyFH9OCQJ32dFAcbuWHw+JsFMiJfPGz NqMQ/lbV6j1AXt4Oe1Cp4YE8/TLm4Za2buDostY5sDZ1qRHRytE4sSbXcF12UPCU1KDy M4YgNaUqqrxMAhbeAaUIFdRR4l+Xo3FynLWpJM9sJ3MFvVX+ipqLWcO9PT5lrAFwCiBc EoDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=noxRP7Dy; 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 h3-20020a056a00230300b006791029dbedsi2164418pfh.157.2023.07.28.18.26.59; Fri, 28 Jul 2023 18:27:12 -0700 (PDT) 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=20221208 header.b=noxRP7Dy; 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 S229914AbjG2AhD (ORCPT + 99 others); Fri, 28 Jul 2023 20:37:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232904AbjG2Agx (ORCPT ); Fri, 28 Jul 2023 20:36:53 -0400 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 734D035A0 for ; Fri, 28 Jul 2023 17:36:52 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id 41be03b00d2f7-5641bc8eba6so874262a12.3 for ; Fri, 28 Jul 2023 17:36:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690591012; x=1691195812; 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=jjZUurEgx8+1WTff2yy2mIJojJc3waWamxg7HHxYGIg=; b=noxRP7DyXhyaM98FK0Vfbiwdsje2k4Fme8Ej+Lzw3561F5KC+8wOpq147aZ9YkCJA4 yEvYhRIL0tZYxlGfadJ3T/3jLoEbUzrRLhc+Tpjjx3RoDw70wdOdllOhL6e3s9wXinRE stWTzC1jd5xpvinZznJG2aox3Q/eCKj8CKm8HHcFpRAqbyrWnYRMbc+qOK3bW954IHkN khHcPSsUb+qRbsTCBupFaUGuJ2ouREB28Jz6YQlS/pAikuObTfZ9xuQkb79Ykk1rItdc /QIdsA7sAOkNbdhQEtraNqyeGeOjcd1g1go1jvyob4nqQ1d1fjXGQ7+U7YBmsRRKafg/ r2oA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690591012; x=1691195812; 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=jjZUurEgx8+1WTff2yy2mIJojJc3waWamxg7HHxYGIg=; b=kOYz4KQcfp8efRIJGOx6pYvTr94NGJO7CGrSSHVLvy+Mz+RKHqnfPENfGR3hWJzDQK LggTmaF6OU5fMtvqRUJmQSRJ9rnQA9SvYaOy20NLPQgK4zNfKxe6ujB1sdKmah7S2z9e NMOl1tVth4EaS39PH+F27lDqq4AkynIlCToxr0tQjEW8fNxX6Fd9QOu7oEmZDTsPHaH+ duiwtFOTMKnHLx6jkfArYXToI92kkvi18pp4OGJWQ8Mnq9PJrEtoU+J1MSgft3axRs+s 3WvRdsV8uYQ8X6YPHycNa/ELINVZzUgCVdvticdbSfBJHawN+8lzN4HxpUttxv3pa6yb 20Ag== X-Gm-Message-State: ABy/qLbem2gxRHy6xIlG9aNBtmPg0kN4J8pNRlPJr/q0StdFo3EgEJJM BIxcICwDxWc1XQtMJixF+KyFaBhKWWw= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:902:f990:b0:1b8:80c9:a98e with SMTP id ky16-20020a170902f99000b001b880c9a98emr10492plb.13.1690591012012; Fri, 28 Jul 2023 17:36:52 -0700 (PDT) Reply-To: Sean Christopherson Date: Fri, 28 Jul 2023 17:36:12 -0700 In-Reply-To: <20230729003643.1053367-1-seanjc@google.com> Mime-Version: 1.0 References: <20230729003643.1053367-1-seanjc@google.com> X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230729003643.1053367-4-seanjc@google.com> Subject: [PATCH v4 03/34] KVM: selftests: Add a shameful hack to preserve/clobber GPRs across ucall From: Sean Christopherson To: Paolo Bonzini , Marc Zyngier , Oliver Upton , Christian Borntraeger , Janosch Frank , Claudio Imbrenda Cc: kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-kernel@vger.kernel.org, Sean Christopherson , Thomas Huth , " =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= " , Aaron Lewis 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,T_SCC_BODY_TEXT_LINE,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: INBOX X-GMAIL-THRID: 1772716327385915919 X-GMAIL-MSGID: 1772716327385915919 Preserve or clobber all GPRs (except RIP and RSP, as they're saved and restored via the VMCS) when performing a ucall on x86 to fudge around a horrific long-standing bug in selftests' nested VMX support where L2's GPRs are not preserved across a nested VM-Exit. I.e. if a test triggers a nested VM-Exit to L1 in response to a ucall, e.g. GUEST_SYNC(), then L2's GPR state can be corrupted. The issues manifests as an unexpected #GP in clear_bit() when running the hyperv_evmcs test due to RBX being used to track the ucall object, and RBX being clobbered by the nested VM-Exit. The problematic hyperv_evmcs testcase is where L0 (test's host userspace) injects an NMI in response to GUEST_SYNC(8) from L2, but the bug could "randomly" manifest in any test that induces a nested VM-Exit from L0. The bug hasn't caused failures in the past due to sheer dumb luck. The obvious fix is to rework the nVMX helpers to save/restore L2 GPRs across VM-Exit and VM-Enter, but that is a much bigger task and carries its own risks, e.g. nSVM does save/restore GPRs, but not in a thread-safe manner, and there is a _lot_ of cleanup that can be done to unify code for doing VM-Enter on nVMX, nSVM, and eVMCS. Signed-off-by: Sean Christopherson --- .../testing/selftests/kvm/lib/x86_64/ucall.c | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/x86_64/ucall.c b/tools/testing/selftests/kvm/lib/x86_64/ucall.c index 4d41dc63cc9e..a53df3ece2f8 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/ucall.c +++ b/tools/testing/selftests/kvm/lib/x86_64/ucall.c @@ -14,8 +14,36 @@ void ucall_arch_init(struct kvm_vm *vm, vm_paddr_t mmio_gpa) void ucall_arch_do_ucall(vm_vaddr_t uc) { - asm volatile("in %[port], %%al" - : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory"); + /* + * FIXME: Revert this hack (the entire commit that added it) once nVMX + * preserves L2 GPRs across a nested VM-Exit. If a ucall from L2, e.g. + * to do a GUEST_SYNC(), lands the vCPU in L1, any and all GPRs can be + * clobbered by L1. Save and restore non-volatile GPRs (clobbering RBP + * in particular is problematic) along with RDX and RDI (which are + * inputs), and clobber volatile GPRs. *sigh* + */ +#define HORRIFIC_L2_UCALL_CLOBBER_HACK \ + "rcx", "rsi", "r8", "r9", "r10", "r11" + + asm volatile("push %%rbp\n\t" + "push %%r15\n\t" + "push %%r14\n\t" + "push %%r13\n\t" + "push %%r12\n\t" + "push %%rbx\n\t" + "push %%rdx\n\t" + "push %%rdi\n\t" + "in %[port], %%al\n\t" + "pop %%rdi\n\t" + "pop %%rdx\n\t" + "pop %%rbx\n\t" + "pop %%r12\n\t" + "pop %%r13\n\t" + "pop %%r14\n\t" + "pop %%r15\n\t" + "pop %%rbp\n\t" + : : [port] "d" (UCALL_PIO_PORT), "D" (uc) : "rax", "memory", + HORRIFIC_L2_UCALL_CLOBBER_HACK); } void *ucall_arch_get_ucall(struct kvm_vcpu *vcpu)