From patchwork Thu Sep 29 20:47:05 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12994610 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CE7F0C433FE for ; Thu, 29 Sep 2022 20:47:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229615AbiI2UrT (ORCPT ); Thu, 29 Sep 2022 16:47:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33982 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbiI2UrR (ORCPT ); Thu, 29 Sep 2022 16:47:17 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B72EF153111 for ; Thu, 29 Sep 2022 13:47:16 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id n9-20020a170902d2c900b001782ad97c7aso1723724plc.8 for ; Thu, 29 Sep 2022 13:47:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=ehn7f9oYjeKdJef39Xo4xVYoRftFjfIcprmKivEos50=; b=sdQTGo3Zyf4/v7/Ep9gB6zG08PK3T1mf9q5pzLqxg/zUpHiB4rf0mz5Y3UtIXhwN33 mnUXa49AUCRUz+7p3Cz+aFZsN8v2PpPcr9hW9CLsDQ3ZAOt6nHxzKUsKdv8k17Bw+V0H 0aJVEtafzjCvf4U2eDZ59gMyqgGUUEYLOke5uQd5zLnylqewV6B+mcPnftG7qqSzj39i x9mugM99cWYswS+DynTJvTXduLFrG2M4hyeACo4JcISB81aJA3WU6rug1zsEsuCB92/g 23o191TthVU0kQOPhe1AU1h2TmORYICCVA+W4ZY+ok9Ovw67i9zzbv6Uosdy/58G6cEQ yZTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=ehn7f9oYjeKdJef39Xo4xVYoRftFjfIcprmKivEos50=; b=x62Dj44lwfoay2NoMU8OC11VdKHs/5IVWKkfXI7dKFITrI7brpyhdVk3uFDugMIzy0 0O2YWSKTw4Hy/IQvL/kr3heIdxvn/h0dl+rkmzY4SYg8pYAPaRsiv3MTH6Z0KZBM2cNo GOMVEiEZF6DW/xD5Y9uFMV7Hi8TIhsPPpKSPIzTLMwzRbQpEsWV/iwbZoYzpFKQjiYTw UTnqcNOznSuvTPI/PMHpd62/8ucranHwiQVQSSNeJEZDV6skhdo8HgWy0JstJT3hFA92 6JAxOp9m0iUX8PSThEqszeUyE4UlGlrGRKMV1TjHuP1L7MZFbJINZQjw71MbSEOraUso NzbQ== X-Gm-Message-State: ACrzQf0qLuyNumMMyVPwX1WwwDROquWInENvpegfqH4H38WMn4lxnu4g eMecd4pcE1/6Z152oYT58LnGcAxReeSrhQ== X-Google-Smtp-Source: AMsMyM74wjVxCiWgJuMZPHq5qb+JKY+VTdhbKP7fh6WXauTmHFwWfc+zUVGHkep25d1vtMSRdatwhH1aod3OxQ== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a17:903:2302:b0:178:286c:86a8 with SMTP id d2-20020a170903230200b00178286c86a8mr5058980plh.172.1664484436263; Thu, 29 Sep 2022 13:47:16 -0700 (PDT) Date: Thu, 29 Sep 2022 13:47:05 -0700 In-Reply-To: <20220929204708.2548375-1-dmatlack@google.com> Mime-Version: 1.0 References: <20220929204708.2548375-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20220929204708.2548375-2-dmatlack@google.com> Subject: [PATCH 1/4] KVM: selftests: Use MMIO to trigger emulation in emulator_error_test From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Colton Lewis , Peter Xu , Jim Mattson , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Use MMIO to force KVM to emulate the flds isntruction in emulator_error_test, rather than relying on KVM_CAP_SMALLER_MAXPHYADDR. KVM_CAP_SMALLER_MAXPHYADDR is not enabled by default when TDP is enabled. So developers that run all selftests against KVM in its default configuration do not get test coverage of KVM_CAP_EXIT_ON_EMULATION_FAILURE. When TDP is disabled, KVM_CAP_SMALLER_MAXPHYADDR is enabled by default, but emulator_error_test actually fails because KVM does not need to emulate flds. i.e. The test fails to induce and emulation failure. Fixes: 39bbcc3a4e39 ("selftests: kvm: Allows userspace to handle emulation errors.") Signed-off-by: David Matlack --- .../kvm/x86_64/emulator_error_test.c | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c index 236e11755ba6..2dff57991d31 100644 --- a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c +++ b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c @@ -11,18 +11,12 @@ #include "kvm_util.h" #include "vmx.h" -#define MAXPHYADDR 36 - -#define MEM_REGION_GVA 0x0000123456789000 -#define MEM_REGION_GPA 0x0000000700000000 -#define MEM_REGION_SLOT 10 -#define MEM_REGION_SIZE PAGE_SIZE +#define MMIO_REGION_GPA 0x700000000 +#define MMIO_REGION_GVA 0x700000000 static void guest_code(void) { - __asm__ __volatile__("flds (%[addr])" - :: [addr]"r"(MEM_REGION_GVA)); - + __asm__ __volatile__("flds (%[addr])" :: [addr]"r"(MMIO_REGION_GVA)); GUEST_DONE(); } @@ -152,34 +146,24 @@ int main(int argc, char *argv[]) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; - uint64_t gpa, pte; - uint64_t *hva; int rc; /* Tell stdout not to buffer its content */ setbuf(stdout, NULL); - TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR)); - vm = vm_create_with_one_vcpu(&vcpu, guest_code); - vcpu_set_cpuid_maxphyaddr(vcpu, MAXPHYADDR); - rc = kvm_check_cap(KVM_CAP_EXIT_ON_EMULATION_FAILURE); TEST_ASSERT(rc, "KVM_CAP_EXIT_ON_EMULATION_FAILURE is unavailable"); vm_enable_cap(vm, KVM_CAP_EXIT_ON_EMULATION_FAILURE, 1); - vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, - MEM_REGION_GPA, MEM_REGION_SLOT, - MEM_REGION_SIZE / PAGE_SIZE, 0); - gpa = vm_phy_pages_alloc(vm, MEM_REGION_SIZE / PAGE_SIZE, - MEM_REGION_GPA, MEM_REGION_SLOT); - TEST_ASSERT(gpa == MEM_REGION_GPA, "Failed vm_phy_pages_alloc\n"); - virt_map(vm, MEM_REGION_GVA, MEM_REGION_GPA, 1); - hva = addr_gpa2hva(vm, MEM_REGION_GPA); - memset(hva, 0, PAGE_SIZE); - pte = vm_get_page_table_entry(vm, vcpu, MEM_REGION_GVA); - vm_set_page_table_entry(vm, vcpu, MEM_REGION_GVA, pte | (1ull << 36)); + /* + * Create a virtual mapping so the guest can access MMIO_REGION_GPA. + * MMIO_REGION_GPA is not mapped by a memslot so KVM will treat the + * access as MMIO and attempt to emulate the flds instruction, which + * will then generate an emulation error. + */ + virt_map(vm, MMIO_REGION_GVA, MMIO_REGION_GPA, 1); vcpu_run(vcpu); process_exit_on_emulation_error(vcpu); From patchwork Thu Sep 29 20:47:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12994611 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E07BC433F5 for ; Thu, 29 Sep 2022 20:47:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229726AbiI2UrX (ORCPT ); Thu, 29 Sep 2022 16:47:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34036 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229650AbiI2UrT (ORCPT ); Thu, 29 Sep 2022 16:47:19 -0400 Received: from mail-yw1-x1149.google.com (mail-yw1-x1149.google.com [IPv6:2607:f8b0:4864:20::1149]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 119A615312A for ; Thu, 29 Sep 2022 13:47:19 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-348608c1cd3so24792207b3.10 for ; Thu, 29 Sep 2022 13:47:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=WdZO+QIKb9m1gAu11nH6SdOXw3nLX+qDN1D4O/5fVL0=; b=aL/OFNgk6ZbFlCVjZCLjGnI6qXi6HZGPFaWGCNPxiNm3aEk0A2sz+E3NaHjt50hzGS UWV9M3HUVDFbUjInMftB/OvV3bka/047A3ee91QT1hcpoB1nZ5lKydoGt/zsCfI55MkL UdaJIjF7HmpVrWz0sCbSaw2viOAhoQFBDWjGp/Mw2F6tPXssTXIOHKsyiQHNDxbcLyHG 4JjQ/ErhdE88pgELD7osYmOGqBZJkmoz/CZJi8qu5OKZUNsXGUWzNZ6kdlWPLJtdWj6S OmATwqfhaCgu2WEwT9wlI5xrAaG9j9Q7aG78jAu7DB2IRJvxYAnbn5q+X4EiXi91js9Y jiPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=WdZO+QIKb9m1gAu11nH6SdOXw3nLX+qDN1D4O/5fVL0=; b=SWTLAzsstZmOn0iOKhOxDLs7nFvyU8FCYnOsh7fGLgZY7BNAXMQOY8+Nqw9AW1C/0p +3UVFHJkSI8pEIDwlZJCGO3jmwZW2ifF7D9Vy3WHKVzvwRfu7EYcgGbESpPY+j4xIGvG 3oYmh+Id68wsQUq+oBZSbXwOcv2mLElHQtEZOQkcMj6ArQQkNRagwgaSGb7NOHK1z4K4 pTHC0NCMIZu0VfkFQlgOkZupMpMv78xHl8HCqtHqvWL//ruUl9su+ofTLSkEaF8OUI2O pQukz94OVZJwZkREMtX0bEUZbaGjFjJJaeSG+nO2hQSlqfqp3hdAZmd6uuF0YhmG19Wq yL3A== X-Gm-Message-State: ACrzQf1u5tlqidYIyGj9xd6a+4oZ2jDCTLkTi3xDVvz+wS/9By3h2q+N VTcVO977JRjpnLvQUvY1G5fPlmHuj+4j3Q== X-Google-Smtp-Source: AMsMyM5ooLGI519TR8RVoAzXlzJ0LlYJeZJ2lcMw5I66WIgvkWi+AZayc33tT0MdSs493AAlJf+l6bueV7tcWg== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a81:11cc:0:b0:345:6c7c:e6db with SMTP id 195-20020a8111cc000000b003456c7ce6dbmr5278953ywr.44.1664484438375; Thu, 29 Sep 2022 13:47:18 -0700 (PDT) Date: Thu, 29 Sep 2022 13:47:06 -0700 In-Reply-To: <20220929204708.2548375-1-dmatlack@google.com> Mime-Version: 1.0 References: <20220929204708.2548375-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20220929204708.2548375-3-dmatlack@google.com> Subject: [PATCH 2/4] KVM: selftests: Delete dead ucall code from emulator_error_test From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Colton Lewis , Peter Xu , Jim Mattson , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Delete a bunch of code related to ucall handling from emulator_error_test. The only thing emulator_error_test needs to check is that the vCPU exits with UCALL_DONE after the second vcpu_run(). No functional change intended. Signed-off-by: David Matlack --- .../kvm/x86_64/emulator_error_test.c | 46 +------------------ 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c index 2dff57991d31..52ff1eb772e9 100644 --- a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c +++ b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c @@ -84,28 +84,11 @@ static void process_exit_on_emulation_error(struct kvm_vcpu *vcpu) } } -static void do_guest_assert(struct ucall *uc) -{ - REPORT_GUEST_ASSERT(*uc); -} - -static void check_for_guest_assert(struct kvm_vcpu *vcpu) -{ - struct ucall uc; - - if (vcpu->run->exit_reason == KVM_EXIT_IO && - get_ucall(vcpu, &uc) == UCALL_ABORT) { - do_guest_assert(&uc); - } -} - static void process_ucall_done(struct kvm_vcpu *vcpu) { struct kvm_run *run = vcpu->run; struct ucall uc; - check_for_guest_assert(vcpu); - TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, "Unexpected exit reason: %u (%s)", run->exit_reason, @@ -116,32 +99,6 @@ static void process_ucall_done(struct kvm_vcpu *vcpu) uc.cmd, UCALL_DONE); } -static uint64_t process_ucall(struct kvm_vcpu *vcpu) -{ - struct kvm_run *run = vcpu->run; - struct ucall uc; - - TEST_ASSERT(run->exit_reason == KVM_EXIT_IO, - "Unexpected exit reason: %u (%s)", - run->exit_reason, - exit_reason_str(run->exit_reason)); - - switch (get_ucall(vcpu, &uc)) { - case UCALL_SYNC: - break; - case UCALL_ABORT: - do_guest_assert(&uc); - break; - case UCALL_DONE: - process_ucall_done(vcpu); - break; - default: - TEST_ASSERT(false, "Unexpected ucall"); - } - - return uc.cmd; -} - int main(int argc, char *argv[]) { struct kvm_vcpu *vcpu; @@ -168,8 +125,7 @@ int main(int argc, char *argv[]) vcpu_run(vcpu); process_exit_on_emulation_error(vcpu); vcpu_run(vcpu); - - TEST_ASSERT(process_ucall(vcpu) == UCALL_DONE, "Expected UCALL_DONE"); + process_ucall_done(vcpu); kvm_vm_free(vm); From patchwork Thu Sep 29 20:47:07 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12994612 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBD5BC433F5 for ; Thu, 29 Sep 2022 20:47:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229745AbiI2Ur2 (ORCPT ); Thu, 29 Sep 2022 16:47:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34154 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229462AbiI2UrZ (ORCPT ); Thu, 29 Sep 2022 16:47:25 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 065FD15313C for ; Thu, 29 Sep 2022 13:47:21 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-349423f04dbso24598657b3.13 for ; Thu, 29 Sep 2022 13:47:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=m9o9aC44IY/im9QAS+qaues+I53Dzb9AHDDpW5u1v5c=; b=HgRgZwUsL+vm/dX90nmv82OSQcUtARJ36y37S8UdTMx2HdsiLkwxuWRWxmfiOqrHE+ nLQuF5MhBOwOHQ9f7VlMQ0v4C+Y/JKmmMmOjnwFLsie2dRS/q855wl8JxN4stoNl238b tZjmBVvqY4hc25JrI6jOJ6eiK6keccGoq8DWgnMDQvc3Th3SFi8nJbeXVCRF7CVREYCj eptalcWmah9dev+gC8CPcS3i51rCxI+2SOTmRnVe1holvaCB0AQb1cRgrvfsTRB33Ivx m0/D9w2kKULKM7XGqIiZOWQWjHA7yB35D+nr1uE3w/zsWUDGXDaqaw+aczasb5pbxv0F y5Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=m9o9aC44IY/im9QAS+qaues+I53Dzb9AHDDpW5u1v5c=; b=rc81kP3Y1j5LiS3/8UNl6tqtPoWlf/ntoxxqCDaprxwizadAKKddNO9YRmurroK8rj BVbB6X1E87cFiYwA9ymfkTh8I/NZm+t+MRNTbP9SZ9hOGDXbvq3eF1Fb7hma3D/kMz8+ eO0Kdw8XNRUx9PtNLGA0/uuh587H7MzXs1MjABVmjZuFjP4KHhx/wE4kerJ1ELB6BCe5 o6X4PNRdbioaywVFMGvSmYoHxPsJM5yUCsyOxw9ZnwlEquW+X+jblhdxFyGr5p+/tiJC XFeTaGZ5HibSaQpSt1TvOZ5I4HWo5FXubT+StMS+9VD80x+WjTsxwl5wx1rU2kGNA9nG ASpA== X-Gm-Message-State: ACrzQf11r0q2jwar2RKPAExKru9wh9o7pYUmpGPfWhfUfm3U9ECAV0Oi VDTl7IsVF/ePQbhc2M2KDkvV0hAyo1DYqw== X-Google-Smtp-Source: AMsMyM7okvM2d9TGUnzuQXESO/VvZPGMOsOoqNODiZF7b6EaR7I7m6WBi/liP0Oia/8cL9wxIMPnv/u5JwRZZA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:97c5:0:b0:6b0:7306:8a76 with SMTP id j5-20020a2597c5000000b006b073068a76mr5079490ybo.400.1664484440252; Thu, 29 Sep 2022 13:47:20 -0700 (PDT) Date: Thu, 29 Sep 2022 13:47:07 -0700 In-Reply-To: <20220929204708.2548375-1-dmatlack@google.com> Mime-Version: 1.0 References: <20220929204708.2548375-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20220929204708.2548375-4-dmatlack@google.com> Subject: [PATCH 3/4] KVM: selftests: Skip emulator_error_test if KVM_CAP_EXIT_ON_EMULATION_FAILURE not available From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Colton Lewis , Peter Xu , Jim Mattson , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Skip emulator_error_test if KVM_CAP_EXIT_ON_EMULATION_FAILURE is not available rather than failing. This makes emulator_error_test skip on older kernels and also deletes a net 2 lines of code from the test. Signed-off-by: David Matlack --- tools/testing/selftests/kvm/x86_64/emulator_error_test.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c index 52ff1eb772e9..4b06c9eefe7d 100644 --- a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c +++ b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c @@ -103,15 +103,13 @@ int main(int argc, char *argv[]) { struct kvm_vcpu *vcpu; struct kvm_vm *vm; - int rc; /* Tell stdout not to buffer its content */ setbuf(stdout, NULL); - vm = vm_create_with_one_vcpu(&vcpu, guest_code); + TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_ON_EMULATION_FAILURE)); - rc = kvm_check_cap(KVM_CAP_EXIT_ON_EMULATION_FAILURE); - TEST_ASSERT(rc, "KVM_CAP_EXIT_ON_EMULATION_FAILURE is unavailable"); + vm = vm_create_with_one_vcpu(&vcpu, guest_code); vm_enable_cap(vm, KVM_CAP_EXIT_ON_EMULATION_FAILURE, 1); /* From patchwork Thu Sep 29 20:47:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 12994613 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2740BC4332F for ; Thu, 29 Sep 2022 20:47:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229708AbiI2Ura (ORCPT ); Thu, 29 Sep 2022 16:47:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34148 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229650AbiI2UrZ (ORCPT ); Thu, 29 Sep 2022 16:47:25 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2F10315311A for ; Thu, 29 Sep 2022 13:47:23 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-349f88710b2so24594557b3.20 for ; Thu, 29 Sep 2022 13:47:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=YiFgZ5G7GtRwDSuIWSYMu3WaEUvY8gkRcfs0C8X2dC4=; b=Twv8NbxwW3VrmxSP2xmUcpaTwuIM94wXpUwFPjCKwEns4q4h0FpniwLtjiQErsZ6OB s1S/MkXYNb2w4VZWNmP3uWRxCkk1kGF4pOviep4fi+EHvw26in/8wcASGKr2FnTHU0wQ dCknia8quBBEr8qT/G6Q6/EYPCNsIGh1N3A/WsqSuYXzjfq9UWfc2/msvMyeB4cYc3ZJ 0hV2wbp1+3il7/KhzOIH1sqmLZSk5q7bf6nKcLStPlyyjGmGYZvc8h5i7pEyhjJoQ5W7 HyZ6tbeG8bFz1PfsmDhos1yA5Td/Ev0aNE2wbM8veD1ehZE19N8xF9RBikNLXLE0Re58 Y9GQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=YiFgZ5G7GtRwDSuIWSYMu3WaEUvY8gkRcfs0C8X2dC4=; b=RZUfJtfgzKK292WmyOjz4OXsO5cEhKPZFqbWFgoCx2PDwZOQBKeNYVW+aTSWzHxmAm GSTojF3NJnp4Jbanza6DXbZip2qQijGg28v3K+p46S1Oh2Ec65l6oK3zfhOnV9i6T4Iy Zsx61Der05kFIPgYsHk7PT6TuErNFRmiP70c1u7L+5p45tGNs2C24xM8Zd25w3WVD8G6 llgzfGSCVzGCkybzc00bkonkdYEgFlx6Vk+09eGXLFYKfFb9MCs5ypuP0MG0P1UYSh/P FYw6l1U5h4YOtJJykJ5U+kyM78xW6A6cUIdVVOvej2+gTt7xhhbg9yTAbitjbdcvrX15 +XoA== X-Gm-Message-State: ACrzQf0UaRd+NlTkUQdcg9s6j3Y69C0/QnZQbdln/OD1xXOsKUoFt6sw EP4FgxTrYYUbMzezpJDCQSPWvbEQu8IVmA== X-Google-Smtp-Source: AMsMyM51bkM4dx91NWb7MIofyA5OoYmlv+BmNFz+YVumEZX3XFjN79rsnSxOrnf7oGuD947i50YJwP8h4fTVEg== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a5b:e91:0:b0:69a:ddb:9f9f with SMTP id z17-20020a5b0e91000000b0069a0ddb9f9fmr5655772ybr.295.1664484442482; Thu, 29 Sep 2022 13:47:22 -0700 (PDT) Date: Thu, 29 Sep 2022 13:47:08 -0700 In-Reply-To: <20220929204708.2548375-1-dmatlack@google.com> Mime-Version: 1.0 References: <20220929204708.2548375-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.rc1.362.ged0d419d3c-goog Message-ID: <20220929204708.2548375-5-dmatlack@google.com> Subject: [PATCH 4/4] KVM: selftests: Explicitly require instructions bytes in emulator_error_test From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Colton Lewis , Peter Xu , Jim Mattson , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Explicitly require instruction bytes to be available in run->emulation_failure by asserting that they are present. Note that the test already requires the instruction bytes to be present because that's the only way the test will advance the RIP past the flds and get to GUEST_DONE(). Signed-off-by: David Matlack --- .../kvm/x86_64/emulator_error_test.c | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c index 4b06c9eefe7d..37ecd880a7c1 100644 --- a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c +++ b/tools/testing/selftests/kvm/x86_64/emulator_error_test.c @@ -58,30 +58,32 @@ static void process_exit_on_emulation_error(struct kvm_vcpu *vcpu) "Unexpected suberror: %u", run->emulation_failure.suberror); - if (run->emulation_failure.ndata >= 1) { - flags = run->emulation_failure.flags; - if ((flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES) && - run->emulation_failure.ndata >= 3) { - insn_size = run->emulation_failure.insn_size; - insn_bytes = run->emulation_failure.insn_bytes; - - TEST_ASSERT(insn_size <= 15 && insn_size > 0, - "Unexpected instruction size: %u", - insn_size); - - TEST_ASSERT(is_flds(insn_bytes, insn_size), - "Unexpected instruction. Expected 'flds' (0xd9 /0)"); - - /* - * If is_flds() succeeded then the instruction bytes - * contained an flds instruction that is 2-bytes in - * length (ie: no prefix, no SIB, no displacement). - */ - vcpu_regs_get(vcpu, ®s); - regs.rip += 2; - vcpu_regs_set(vcpu, ®s); - } - } + TEST_ASSERT(run->emulation_failure.ndata >= 3, + "Unexpected emulation_failure.ndata: %d", + run->emulation_failure.ndata); + + flags = run->emulation_failure.flags; + TEST_ASSERT(flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES, + "Missing instruction bytes in emulation_failure."); + + insn_size = run->emulation_failure.insn_size; + insn_bytes = run->emulation_failure.insn_bytes; + + TEST_ASSERT(insn_size <= 15 && insn_size > 0, + "Unexpected instruction size: %u", + insn_size); + + TEST_ASSERT(is_flds(insn_bytes, insn_size), + "Unexpected instruction. Expected 'flds' (0xd9 /0)"); + + /* + * If is_flds() succeeded then the instruction bytes contained an flds + * instruction that is 2-bytes in length (ie: no prefix, no SIB, no + * displacement). + */ + vcpu_regs_get(vcpu, ®s); + regs.rip += 2; + vcpu_regs_set(vcpu, ®s); } static void process_ucall_done(struct kvm_vcpu *vcpu)