Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp758920pxp; Fri, 11 Mar 2022 14:21:42 -0800 (PST) X-Google-Smtp-Source: ABdhPJxH/AdFRzFd85Iz/Ua0986jQD5ko4MVtsUHV7cZpAo/CCxK6Ren+53pCL9R+HhmDiGzgtkz X-Received: by 2002:a63:ef03:0:b0:374:7286:14d0 with SMTP id u3-20020a63ef03000000b00374728614d0mr10239104pgh.552.1647037302614; Fri, 11 Mar 2022 14:21:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1647037302; cv=none; d=google.com; s=arc-20160816; b=eL/qg908ju+bYs1f/iwUyYhBYEaOBMM9lmseJart2vmDGPXGR+mLmzlVte5ZlpekwC 4fsjt7sh/Rm1M5fZJLxhUIwbBUnP2QwagLUKNeCCzW5KJE3y8Ms2YmHeXBs7EqJjfmad rFQHc7gfPIPDBfUxgK2wrYpPdj/XQpfYGGYC4pd8wfJe+/5XeIqbdWzqc1UvcYjgoVg/ BnX1B9tbE/S0AfczcXaRIujlzRnPC7yPqVHK1eN6KQ9ETsz28AaxlrLMb/Fd5eyYeuy7 djNCNLwWRZtY0t0OKlJqXxQ6PE/m40jFsxozp92UPfuTOkF4PqMIS5/0u0MAqjPcf5m2 mU1A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=7eA6AUUpqrVHrilDK5V9tXlig/uQEFI4tMzjfe19nuQ=; b=Sd3CMRqcuV/tg0Ma37M7gA3qqskyTBLnsxr/IbLBkmbb1axgRxTpdWGPrn+aimxzA/ 7WRP2yp/TCBMNdBHwYwRBhprn8x7UpaSkWtb2iG4jY4Ij75F/U7s1PfQe3To1Xr5Td7H VSuD5akCeM7Z/u37kferrt9LHzis6i0SmLpNRI9yHol3B6j4Vq6O0l3ulKIuBuv5xYqZ DHtE7BBLBdEGVNQ5R5pX6YRgUc5bYomR8BCXsMUWXqvSPi/nLfVc8nd4vIgVVDojTPqq S5PrAUwqAEzVtNqDEgJsMBts2fjronOufCb3M3KJrNM+NxIPzMq4CmOFQ/HySnyRCyHs UJJA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id c32-20020a630d20000000b003729ec91b57si8968571pgl.360.2022.03.11.14.21.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Mar 2022 14:21:42 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D7A902E40A2; Fri, 11 Mar 2022 13:30:27 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343565AbiCJVkN (ORCPT + 99 others); Thu, 10 Mar 2022 16:40:13 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55554 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237075AbiCJVkL (ORCPT ); Thu, 10 Mar 2022 16:40:11 -0500 Received: from vps-vb.mhejs.net (vps-vb.mhejs.net [37.28.154.113]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 29FFABECC3; Thu, 10 Mar 2022 13:39:07 -0800 (PST) Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1nSQUh-0006Ir-2l; Thu, 10 Mar 2022 22:38:47 +0100 From: "Maciej S. Szmigiero" To: Paolo Bonzini Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Tom Lendacky , Brijesh Singh , Jon Grimm , David Kaplan , Boris Ostrovsky , Liam Merwick , kvm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 0/5] nSVM: L1 -> L2 event injection fixes and a self-test Date: Thu, 10 Mar 2022 22:38:36 +0100 Message-Id: X-Mailer: git-send-email 2.35.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 From: "Maciej S. Szmigiero" There are some issues with respect to nSVM L1 -> L2 event injection. First, the next_rip field of a VMCB is *not* an output-only field for a VMRUN. This field value (instead of the saved guest RIP) in used by the CPU for the return address pushed on stack when injecting a software interrupt or INT3 or INTO exception (this was confirmed by AMD). On a VMRUN that does event injection it has similar function as VMX's VM_ENTRY_INSTRUCTION_LEN field, although, in contrast to VMX, it holds an absolute RIP value, not a relative increment. However, KVM seems to treat this field as a unidirectional hint from the CPU to the hypervisor - there seems to be no specific effort to maintain this field consistency for such VMRUN. This is mostly visible with running a nested guest, with L1 trying to inject an event into its L2. In this case, we need to make sure the next_rip field gets synced from vmcb12 to vmcb02. Another issue is that pending L1 -> L2 events are forgotten if there is an intervening L0 VMEXIT during their delivery. We need to make sure they are remembered (including their desired next_rip field value) until they are either re-injected into L2 successfully or returned back to L1 in the EXITINTINFO field upon a nested VMEXIT. A new KVM self-test that checks for the nSVM issues described above is included in this patch series. These issues are SVM-specific - all the use cases described above already work correctly with VMX. This patch set was tested with both Linux and Windows nested guests. KVM: nSVM: Sync next_rip field from vmcb12 to vmcb02 KVM: SVM: Downgrade BUG_ON() to WARN_ON() in svm_inject_irq() KVM: nSVM: Don't forget about L1-injected events KVM: nSVM: Restore next_rip when doing L1 -> L2 event re-injection KVM: selftests: nSVM: Add svm_nested_soft_inject_test arch/x86/kvm/svm/nested.c | 69 +++++++- arch/x86/kvm/svm/svm.c | 60 ++++++- arch/x86/kvm/svm/svm.h | 48 ++++++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/include/x86_64/svm_util.h | 2 + .../kvm/x86_64/svm_nested_soft_inject_test.c | 147 ++++++++++++++++++ 7 files changed, 324 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/svm_nested_soft_inject_test.c