From patchwork Tue Oct 18 21:46: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: 13011083 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 D8E81C4167B for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229978AbiJRVqs (ORCPT ); Tue, 18 Oct 2022 17:46:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53156 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229986AbiJRVqS (ORCPT ); Tue, 18 Oct 2022 17:46:18 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EDCB1B56D6 for ; Tue, 18 Oct 2022 14:46:17 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id lk10-20020a17090b33ca00b0020da9954852so10001533pjb.1 for ; Tue, 18 Oct 2022 14:46:17 -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:message-id:reply-to; bh=6BQYdzWfYe+F6aifR3ZdUK7foQ29QXcSEDEg5el9DVs=; b=VesTJ6dCeAsfNIOlSkXWGtBV3qVDFDPm/Xfg8R1f1sNmFxWzDQIBvrAEVazRBcDvUW C9GRZD9JU5MyvBE6OvapC5zBuibJtmsmK9VvuYUD7y+DFUs96RSZGCk+Zvu31BLz/bxC EhH/rRKJXwORCWNMOn+wC/jFpXcicmwAH8rudIMf4ewYBJQhhLOeeuyQef2apZEhABuT kgJzKlc8E5N9AYoyJfVW5APDFT2R5FikexGqIkPeLiCnFEM6WodOWx0HTjioD6sl2nDV LT3vY2I1fQ1a3p5VGR0mQvEYyXkaZM2Bej4Oessir1+NSFynirJHVQ5KXdpGQ8yJ67PO QY1A== 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:message-id:reply-to; bh=6BQYdzWfYe+F6aifR3ZdUK7foQ29QXcSEDEg5el9DVs=; b=bZ+ZHHdD1u2LOAqUe6+bAIBrSqvO3hyofSRLVIcMjuHjt0jP9cCyS+nbKyDT2xhb6F olhyyrQlkTIF/mSRheYBKDVpyoSAkURP6REyPrFPxsyYHjY+eAHNdfzUUplo+P4DyB4g TChEiu3KZrx2BNPdWpsImg+Gtd/TmYOxQmhHRjYLKK24QS2TATKi/eOnIZrOhQLhGCjj +R9+LPJ6v0dsdJOYysCife+g7u+npp7ZwSOHR5yqQ11K7Se23tvgY53VXxdhWtccUYOF DLrBRIlxsndHo2klRehQHr8KvrP9wMTm84kzK37px3VLdPpmDx03EyB3SMpWpx0KijPi Q/rg== X-Gm-Message-State: ACrzQf1abLqJ/u/LkXdJEzQsVNRKkehGWzJBuHSEDvU7riAIT19LAuMz VFevm4xtuDGbOq+2qmWEnPDUx1y2lmRP+g== X-Google-Smtp-Source: AMsMyM5BgbTUXapzu+3U/jJ6oxHpFMMKRB+/8o3t9eq9+he6yjbGejeoMJsBL+TJtXsTVTbSjGm4gCVLTgWyWA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a17:90a:4811:b0:20a:fee1:8f69 with SMTP id a17-20020a17090a481100b0020afee18f69mr1878699pjh.0.1666129576971; Tue, 18 Oct 2022 14:46:16 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:05 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-2-dmatlack@google.com> Subject: [PATCH v2 1/8] KVM: selftests: Rename emulator_error_test to smaller_maxphyaddr_emulation_test From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Rename emulator_error_test to smaller_maxphyaddr_emulation_test and update the comment at the top of the file to document that this is explicitly a test to validate that KVM emulates instructions in response to an EPT violation when emulating a smaller MAXPHYADDR. Signed-off-by: David Matlack Reviewed-by: Sean Christopherson --- tools/testing/selftests/kvm/.gitignore | 2 +- tools/testing/selftests/kvm/Makefile | 2 +- ...ulator_error_test.c => smaller_maxphyaddr_emulation_test.c} | 3 ++- 3 files changed, 4 insertions(+), 3 deletions(-) rename tools/testing/selftests/kvm/x86_64/{emulator_error_test.c => smaller_maxphyaddr_emulation_test.c} (97%) diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index d625a3f83780..c484ff164000 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -16,7 +16,6 @@ /x86_64/cr4_cpuid_sync_test /x86_64/debug_regs /x86_64/evmcs_test -/x86_64/emulator_error_test /x86_64/fix_hypercall_test /x86_64/get_msr_index_features /x86_64/kvm_clock_test @@ -34,6 +33,7 @@ /x86_64/set_boot_cpu_id /x86_64/set_sregs_test /x86_64/sev_migrate_tests +/x86_64/smaller_maxphyaddr_emulation_test /x86_64/smm_test /x86_64/state_test /x86_64/svm_vmcall_test diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 6448cb9f710f..90c19e1753f7 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -81,7 +81,6 @@ TEST_GEN_PROGS_x86_64 = x86_64/cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test -TEST_GEN_PROGS_x86_64 += x86_64/emulator_error_test TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid @@ -95,6 +94,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/platform_info_test TEST_GEN_PROGS_x86_64 += x86_64/pmu_event_filter_test TEST_GEN_PROGS_x86_64 += x86_64/set_boot_cpu_id TEST_GEN_PROGS_x86_64 += x86_64/set_sregs_test +TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test TEST_GEN_PROGS_x86_64 += x86_64/smm_test TEST_GEN_PROGS_x86_64 += x86_64/state_test TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test diff --git a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c similarity index 97% rename from tools/testing/selftests/kvm/x86_64/emulator_error_test.c rename to tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c index 236e11755ba6..6ed996988a5a 100644 --- a/tools/testing/selftests/kvm/x86_64/emulator_error_test.c +++ b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c @@ -2,7 +2,8 @@ /* * Copyright (C) 2020, Google LLC. * - * Tests for KVM_CAP_EXIT_ON_EMULATION_FAILURE capability. + * Test that KVM emulates instructions in response to EPT violations when + * allow_smaller_maxphyaddr is enabled and guest.MAXPHYADDR < host.MAXPHYADDR. */ #define _GNU_SOURCE /* for program_invocation_short_name */ From patchwork Tue Oct 18 21:46: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: 13011077 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 800B8C4332F for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229767AbiJRVql (ORCPT ); Tue, 18 Oct 2022 17:46:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230000AbiJRVqU (ORCPT ); Tue, 18 Oct 2022 17:46:20 -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 B1C07BB06F for ; Tue, 18 Oct 2022 14:46:19 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-352e29ff8c2so153220447b3.21 for ; Tue, 18 Oct 2022 14:46: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:message-id:reply-to; bh=Y1on+u6Ft+PC3bubHwfG2GbJjTHKJX75lxVXEmZJ9dY=; b=eNkU5Xljty7BP0br6q/YE6EGbNPfcIvGDUy+O6k/29Eej3yUAbxFRrEF/XtDNnfb/L 6ONwBoprwuX5N9JvqQ84oTZJj+3G/jlKHlgCMQ95nlwlsezwgWaVGbWEwjtkwTlI34LC CPCKPjy4ogFSA4d6b8cniAuWadVFXrs7H9uXaLEl4b7qJdAyH9m3B0UKeSMWnTOWhbMl 1eBcqUoy3/4liHrJEMje+MIVUzRjiIK9T0q+ePMBdMPU96mKiIJbDpREIhmRqDQuTZN7 FKm1h9rRb2d513w1yfPREo+WDTObo0Uc6K+VZxNN56yh4RO8+CVuC5EomAjrpxXq+YKu LGsw== 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:message-id:reply-to; bh=Y1on+u6Ft+PC3bubHwfG2GbJjTHKJX75lxVXEmZJ9dY=; b=bCIVAkVNMYzfE4urkaZcfa36ydWkckNjfvlWcpWEhOI0KeJlNeYQ4BlcNdrTHQ5cmx UCvRfY/IVwK48w5bxWNdqOMDUXnPxhtUJ1d0IPcc723U7eoxoAnEZM9+mPyL+fbokVr/ 6eqXSnBoPY73iz61t/BZYqrCdnqQdt9gtTeXOA1xkOWgJKcD90YoV65c2m3cz2EjwbuN Pa/cOoDHa1D/vEJEGywsZXqX7h25vOMiii43RK/i7SYyvil56QD0PZVtschE8+u8kmgT SWMtD8DP0yyzHstTw3Xj/JV/q+VmDnYsmGY8U9Elhst1ijlx2eDj6SlykFBIaOXT0U9s O3pw== X-Gm-Message-State: ACrzQf3/1AR5DqEwoIi93GCPtiq16oQsgVTzXDGLShTFt1Au+SVGE5z3 P8ZP0RVBBo/toGDM15fS+7eR1e7f69RvnQ== X-Google-Smtp-Source: AMsMyM7uSLlJLuZCvuYcSh3Mfz8UXXjXwMtuyrRVubC1F2QHdg2AWjemeZnCIweo/qjd+MPZ2sznDsW1cdczGA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:6f57:0:b0:6be:ff1c:5b42 with SMTP id k84-20020a256f57000000b006beff1c5b42mr4403844ybc.192.1666129579025; Tue, 18 Oct 2022 14:46:19 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:06 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-3-dmatlack@google.com> Subject: [PATCH v2 2/8] KVM: selftests: Explicitly require instructions bytes From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack 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 --- .../smaller_maxphyaddr_emulation_test.c | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c index 6ed996988a5a..c5353ad0e06d 100644 --- a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c +++ b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c @@ -65,30 +65,29 @@ 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); - } - } + flags = run->emulation_failure.flags; + TEST_ASSERT(run->emulation_failure.ndata >= 3 && + flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES, + "run->emulation_failure is missing instruction bytes"); + + 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 do_guest_assert(struct ucall *uc) From patchwork Tue Oct 18 21:46: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: 13011080 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 BA745C4321E for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229963AbiJRVqo (ORCPT ); Tue, 18 Oct 2022 17:46:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53178 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230004AbiJRVqW (ORCPT ); Tue, 18 Oct 2022 17:46:22 -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 54E2DB56D6 for ; Tue, 18 Oct 2022 14:46:21 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-349423f04dbso153137677b3.13 for ; Tue, 18 Oct 2022 14:46:21 -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:message-id:reply-to; bh=HReMimd/H0bznbCp0FcGUwVU95KXf+ukGx45CyBX7Z8=; b=qqDTf++UkzDQHQkKEdFVVu9Ud7rITz8INeDQxoR+L61vdbzKSyLeEuyGwj03V7wrM2 tqq1NuYfiHT+XStV2YIvpTmk2f3O2dOtMu05rdjdmUnVEAGzzxbjYYX+hGS06P9l8nCc XFH7rebz/qxFriJhys7f39PnP97XYqTT9/FEj1k9lGHK6e7n7M4HIHYm4kgCz4khYpgw AewOSakcGmHVFDCYxZobpZDdcLj2qSJGw1ssFlVUJJzqZ+if6mr7ZNxcSdInsgRO1CD2 rMCXvorV0loMpuNTSCFudDlPjwITcS6LuGUi7uUCeizGlY8Vnb9uZ8k4y3wngpyHNRMK a5yg== 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:message-id:reply-to; bh=HReMimd/H0bznbCp0FcGUwVU95KXf+ukGx45CyBX7Z8=; b=2BbY0xVEPUn9EAxpRN5WFcvZdBIzjmFC4gUtVw+q7sCdhTJva8FdzvZYgk7ZpqTK+M WQnx8gdnh9PFP2zU9Mghe14gITmucUF/dCiwh8Eyrbhi+/Zk0/8gbfwH7lNqPldrt39I diWk3wYnys4gOSOozLh4vQ4c0mwDm+7kJPs+K78FJrKU6xOBLuRpyICJzclOV733WUxU BSl7FcuTm7rZdVasb8Q+7g7YczcwhoBDtkMGvNYHN7qTjZGFhio30YT0Qd8D6Gq2Rrg+ ZZ2rQ7iIg9drc4r8uVKI/wRbo5tEqYfOSzaUm2W6Kq75o/RJauTjJb9S3BxJP7q3e1Zo vzUQ== X-Gm-Message-State: ACrzQf2N4SHBvwPYmRocC0nLs8SrGNQ2yO5r3RHSH3bDX/OdmR+gGMyq KZPmx7804wdMWq/j6t7fofliOa8irgY1rg== X-Google-Smtp-Source: AMsMyM7QEL0E/u9r8326E9p5OHlltCrnGwax65vlXOMd31mGPTXlCMuwFbPs+fweX9+aOEgUpM2qdwUCTWWPkQ== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:bd0f:0:b0:6c0:8cb1:d2b4 with SMTP id f15-20020a25bd0f000000b006c08cb1d2b4mr4192425ybk.95.1666129580677; Tue, 18 Oct 2022 14:46:20 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:07 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-4-dmatlack@google.com> Subject: [PATCH v2 3/8] KVM: selftests: Delete dead ucall code From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Delete a bunch of code related to ucall handling from smaller_maxphyaddr_emulation_test. The only thing smaller_maxphyaddr_emulation_test needs to check is that the vCPU exits with UCALL_DONE after the second vcpu_run(). Signed-off-by: David Matlack --- .../smaller_maxphyaddr_emulation_test.c | 54 +------------------ 1 file changed, 2 insertions(+), 52 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c index c5353ad0e06d..d6e71549ca08 100644 --- a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c +++ b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c @@ -90,64 +90,15 @@ static void process_exit_on_emulation_error(struct kvm_vcpu *vcpu) vcpu_regs_set(vcpu, ®s); } -static void do_guest_assert(struct ucall *uc) -{ - REPORT_GUEST_ASSERT(*uc); -} - -static void check_for_guest_assert(struct kvm_vcpu *vcpu) +static void assert_ucall_done(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, - exit_reason_str(run->exit_reason)); - TEST_ASSERT(get_ucall(vcpu, &uc) == UCALL_DONE, "Unexpected ucall command: %lu, expected UCALL_DONE (%d)", 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; @@ -184,8 +135,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"); + assert_ucall_done(vcpu); kvm_vm_free(vm); From patchwork Tue Oct 18 21:46: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: 13011082 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 C9C79C43217 for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229967AbiJRVqq (ORCPT ); Tue, 18 Oct 2022 17:46:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53184 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230009AbiJRVqX (ORCPT ); Tue, 18 Oct 2022 17:46:23 -0400 Received: from mail-pj1-x1049.google.com (mail-pj1-x1049.google.com [IPv6:2607:f8b0:4864:20::1049]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CAF43B56D6 for ; Tue, 18 Oct 2022 14:46:22 -0700 (PDT) Received: by mail-pj1-x1049.google.com with SMTP id pj13-20020a17090b4f4d00b0020b0a13cba4so1550243pjb.0 for ; Tue, 18 Oct 2022 14:46:22 -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:message-id:reply-to; bh=LfWDRjOSGadEASZhwHf3SBCsJTPgC/7zVshmUPjVH5I=; b=fwgdkCGCwDrbXlbDohWAVWvdUlmTxGq32YxAPn36kYs0tvfywan66JQY8Yj6cBmEDf XoewEIfYzCu0CO8Gv/ByMOSQvyXCpAbqrKC+K5jnGOxdt96k77rePuL/BPbMyBvThXs2 ur570ywJYgF1BuXHMo+hEu59VJ78gKR9KMDalGqQ/89POQek1dsn2oX3gX3VFxfgt+DJ WGJEDOX91hCOCQT0gPcTGWuWVCYsK7DiUb8tOPng0YNHHi9RiXHiVmE47kGu8k/acHGR dSCCl6NfX6ZyO6bYO10TLpdEaORnRbUGlg3JXM9dmaqJllnKhLjB2ebSxzFFfxFbnpNK KjSg== 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:message-id:reply-to; bh=LfWDRjOSGadEASZhwHf3SBCsJTPgC/7zVshmUPjVH5I=; b=XTId4GFxIo0GYRVqNciviOXuhoyP0Ofq8eTDMPQz9NL6+HT4Wrgi5Rql6g1y88ZRnO NTJEIfaiLkWH6/WKD8dc8PUhwlGosKcNXdaGTvWo9MEqdiLpTMHFMrFZM3xdmZDpJauM Q5eKk8D1l4x5QXpUU/Zlzd1GAXqvSGRiaDay0wVSBQgVZRM1rXWl/HHwL5tp6/F39HgL p1SaWmOlTczbhF9rKOVAXenw2Q4mCpY/vXQ++1ds4Q2rDYy40LYXwjP3EQ32RUxlYLEt OoHRn4pXGyTgxR8nxLOPesfWqG3PJDFhxNDoaWmS+BGm9lBLUgmmY3E2Wd2NT8QNjG4K XdTA== X-Gm-Message-State: ACrzQf3tUr0fPjL+X6J7980gvRa4D5vKP060lPd51sAJi/KfOXePXMvD JhDkzi/wUbBaSEWduwv7JdPQ7S3gSbK4Pw== X-Google-Smtp-Source: AMsMyM6kRzm0H9DaXA3qmxfKPqrZUMRkTXQ6vrERHyJEQje4IWUK+ro+b+WmmWR3RQXupdM2VLrLO9Ow3SiOPQ== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a05:6a00:15c2:b0:565:bc96:1c75 with SMTP id o2-20020a056a0015c200b00565bc961c75mr5221559pfu.23.1666129582419; Tue, 18 Oct 2022 14:46:22 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:08 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-5-dmatlack@google.com> Subject: [PATCH v2 4/8] KVM: selftests: Move flds instruction emulation failure handling to header From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Move the flds instruction emulation failure handling code to a header so it can be re-used in an upcoming test. No functional change intended. Signed-off-by: David Matlack --- .../selftests/kvm/include/x86_64/processor.h | 9 +++ .../selftests/kvm/x86_64/flds_emulation.h | 67 +++++++++++++++++ .../smaller_maxphyaddr_emulation_test.c | 72 ++----------------- 3 files changed, 81 insertions(+), 67 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/flds_emulation.h diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index e8ca0d8a6a7e..6762bc315464 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -882,4 +882,13 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, #define XSTATE_XTILE_DATA_MASK (1ULL << XSTATE_XTILE_DATA_BIT) #define XFEATURE_XTILE_MASK (XSTATE_XTILE_CFG_MASK | \ XSTATE_XTILE_DATA_MASK) + +/* + * Accessors to get R/M, REG, and Mod bits described in the SDM vol 2, + * figure 2-2 "Table Interpretation of ModR/M Byte (C8H)". + */ +#define GET_RM(insn_byte) ((insn_byte) & 0x7) +#define GET_REG(insn_byte) (((insn_byte) & 0x38) >> 3) +#define GET_MOD(insn_byte) (((insn_byte) & 0xc) >> 6) + #endif /* SELFTEST_KVM_PROCESSOR_H */ diff --git a/tools/testing/selftests/kvm/x86_64/flds_emulation.h b/tools/testing/selftests/kvm/x86_64/flds_emulation.h new file mode 100644 index 000000000000..55bb9272f4d0 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/flds_emulation.h @@ -0,0 +1,67 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_KVM_FLDS_EMULATION_H +#define SELFTEST_KVM_FLDS_EMULATION_H + +#include "processor.h" +#include "kvm_util.h" + +/* + * flds is an instruction that the KVM instruction emulator is known not to + * support. This can be used in guest code along with a mechanism to force + * KVM to emulate the instruction (e.g. by providing an MMIO address) to + * exercise emulation failures. + */ +static inline void flds(uint64_t address) +{ + __asm__ __volatile__("flds (%0)" :: "r"(address)); +} + +static inline void assert_exit_for_flds_emulation_failure(struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + uint8_t *insn_bytes; + uint8_t insn_size; + uint64_t flags; + + TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR, + "Unexpected exit reason: %u (%s)", + run->exit_reason, + exit_reason_str(run->exit_reason)); + + TEST_ASSERT(run->emulation_failure.suberror == KVM_INTERNAL_ERROR_EMULATION, + "Unexpected suberror: %u", + run->emulation_failure.suberror); + + flags = run->emulation_failure.flags; + TEST_ASSERT(run->emulation_failure.ndata >= 3 && + flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES, + "run->emulation_failure is missing instruction bytes"); + + 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(insn_size >= 2 && + insn_bytes[0] == 0xd9 && + GET_REG(insn_bytes[1]) == 0x0 && + GET_MOD(insn_bytes[1]) == 0x0 && + /* Ensure there is no SIB byte. */ + GET_RM(insn_bytes[1]) != 0x4 && + /* Ensure there is no displacement byte. */ + GET_RM(insn_bytes[1]) != 0x5, + "Unexpected instruction. Expected 'flds' (0xd9 /0)"); +} + +static inline void skip_flds_instruction(struct kvm_vcpu *vcpu) +{ + struct kvm_regs regs; + + vcpu_regs_get(vcpu, ®s); + regs.rip += 2; + vcpu_regs_set(vcpu, ®s); +} + +#endif /* !SELFTEST_KVM_FLDS_EMULATION_H */ diff --git a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c index d6e71549ca08..91a85a00b692 100644 --- a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c +++ b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c @@ -8,6 +8,8 @@ #define _GNU_SOURCE /* for program_invocation_short_name */ +#include "flds_emulation.h" + #include "test_util.h" #include "kvm_util.h" #include "vmx.h" @@ -21,75 +23,10 @@ static void guest_code(void) { - __asm__ __volatile__("flds (%[addr])" - :: [addr]"r"(MEM_REGION_GVA)); - + flds(MEM_REGION_GVA); GUEST_DONE(); } -/* - * Accessors to get R/M, REG, and Mod bits described in the SDM vol 2, - * figure 2-2 "Table Interpretation of ModR/M Byte (C8H)". - */ -#define GET_RM(insn_byte) (insn_byte & 0x7) -#define GET_REG(insn_byte) ((insn_byte & 0x38) >> 3) -#define GET_MOD(insn_byte) ((insn_byte & 0xc) >> 6) - -/* Ensure we are dealing with a simple 2-byte flds instruction. */ -static bool is_flds(uint8_t *insn_bytes, uint8_t insn_size) -{ - return insn_size >= 2 && - insn_bytes[0] == 0xd9 && - GET_REG(insn_bytes[1]) == 0x0 && - GET_MOD(insn_bytes[1]) == 0x0 && - /* Ensure there is no SIB byte. */ - GET_RM(insn_bytes[1]) != 0x4 && - /* Ensure there is no displacement byte. */ - GET_RM(insn_bytes[1]) != 0x5; -} - -static void process_exit_on_emulation_error(struct kvm_vcpu *vcpu) -{ - struct kvm_run *run = vcpu->run; - struct kvm_regs regs; - uint8_t *insn_bytes; - uint8_t insn_size; - uint64_t flags; - - TEST_ASSERT(run->exit_reason == KVM_EXIT_INTERNAL_ERROR, - "Unexpected exit reason: %u (%s)", - run->exit_reason, - exit_reason_str(run->exit_reason)); - - TEST_ASSERT(run->emulation_failure.suberror == KVM_INTERNAL_ERROR_EMULATION, - "Unexpected suberror: %u", - run->emulation_failure.suberror); - - flags = run->emulation_failure.flags; - TEST_ASSERT(run->emulation_failure.ndata >= 3 && - flags & KVM_INTERNAL_ERROR_EMULATION_FLAG_INSTRUCTION_BYTES, - "run->emulation_failure is missing instruction bytes"); - - 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 assert_ucall_done(struct kvm_vcpu *vcpu) { struct ucall uc; @@ -133,7 +70,8 @@ int main(int argc, char *argv[]) vm_set_page_table_entry(vm, vcpu, MEM_REGION_GVA, pte | (1ull << 36)); vcpu_run(vcpu); - process_exit_on_emulation_error(vcpu); + assert_exit_for_flds_emulation_failure(vcpu); + skip_flds_instruction(vcpu); vcpu_run(vcpu); assert_ucall_done(vcpu); From patchwork Tue Oct 18 21:46:09 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13011079 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 AB441C43219 for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229506AbiJRVqn (ORCPT ); Tue, 18 Oct 2022 17:46:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230010AbiJRVqZ (ORCPT ); Tue, 18 Oct 2022 17:46:25 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6C35EB56D6 for ; Tue, 18 Oct 2022 14:46:24 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id n1-20020a170902f60100b00179c0a5c51fso10409162plg.7 for ; Tue, 18 Oct 2022 14:46:24 -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:message-id:reply-to; bh=Ki8KREEmOD9pYn9HlzodteOUGs4bQu9Nhvxnlmmz8V4=; b=r92V8W3d9Y9PKpjxnUbnpmJiFvDPElXPqXHXHGAMAuSMYp6MYg2snohMBrGHnumLvd 0APcQWyuoEpss3BLYH9q8SyJZsuInDgiIa/tq7hWuYnqtDnz4xC8jChZDzhXy9xU/txD xOrKTmfA47Y7RybneY08MP5e+gbar5lEHd3lnhuONMfub0jFvpAJ868JdsrwHpre7SmD IavpQmVPY8HsWMZXjaMaW5tz9wVHNDpRn+ZZ2uKkpljrnHX1JS+LvbzQcE4m1VQNlhki 2vkP9kcKpJS0SX6849qwgFEX8dDPSsUOt8lSwM4n8mER7EVKbGg0dJUjFvvg64YD/tv/ bEnQ== 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:message-id:reply-to; bh=Ki8KREEmOD9pYn9HlzodteOUGs4bQu9Nhvxnlmmz8V4=; b=3IBkEcDbkV2CRxxyUTVxH6nQnappcOD1B0zBUzWpNTRvIhTS6mrK9nepXCJjPfQaA9 7q5hRaclMpMzKPiLC5U0NxGPnaOPKmgGynyIYcJIvjSdyr2vpdKZvW14mnNTBa+dDdd5 20n3VqNN52t4Gp0c4T7lmm64JjaSVcD3vvkp7/F1s+rRaXyHBNam+EerUTgz9b02XW9r iaWmcyCQBygy1mCRXr4fRLa9hD7OGlfhtN+fPH8EwGjinriPL9YIJc1Wqc3NapwsQ+Zp hItO6257H9ilGuP5rF5caaAD0hcGy4MfnN6y5L+0Vx4SINNK1mDu0qewdfOGqF3T/rjL pOIw== X-Gm-Message-State: ACrzQf08OdMa7l/AsImHOejV7Ftptm2WHfbfnttEV07vceATgmiQEjXk N+3nplMzkWPI2uWbZS3QHEserCY2QMBU+w== X-Google-Smtp-Source: AMsMyM7iSm9LKqIFtTcsQznRC674C05cbHdZgVWIP+UTTgsRQi4ZGIgbDC3IOyqmHNxq+0UPHyWuHSdcvHpalw== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a17:90b:1d4d:b0:20a:794a:f6e7 with SMTP id ok13-20020a17090b1d4d00b0020a794af6e7mr41878821pjb.151.1666129583995; Tue, 18 Oct 2022 14:46:23 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:09 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-6-dmatlack@google.com> Subject: [PATCH v2 5/8] KVM: x86/mmu: Use BIT{,_ULL}() for PFERR masks From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Use the preferred BIT() and BIT_ULL() to construct the PFERR masks rather than open-coding the bit shifting. No functional change intended. Signed-off-by: David Matlack Reviewed-by: Sean Christopherson --- arch/x86/include/asm/kvm_host.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index aa381ab69a19..1d7605b079bf 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -253,16 +253,16 @@ enum x86_intercept_stage; #define PFERR_GUEST_PAGE_BIT 33 #define PFERR_IMPLICIT_ACCESS_BIT 48 -#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT) -#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT) -#define PFERR_USER_MASK (1U << PFERR_USER_BIT) -#define PFERR_RSVD_MASK (1U << PFERR_RSVD_BIT) -#define PFERR_FETCH_MASK (1U << PFERR_FETCH_BIT) -#define PFERR_PK_MASK (1U << PFERR_PK_BIT) -#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT) -#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT) -#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT) -#define PFERR_IMPLICIT_ACCESS (1ULL << PFERR_IMPLICIT_ACCESS_BIT) +#define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) +#define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT) +#define PFERR_USER_MASK BIT(PFERR_USER_BIT) +#define PFERR_RSVD_MASK BIT(PFERR_RSVD_BIT) +#define PFERR_FETCH_MASK BIT(PFERR_FETCH_BIT) +#define PFERR_PK_MASK BIT(PFERR_PK_BIT) +#define PFERR_SGX_MASK BIT(PFERR_SGX_BIT) +#define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) +#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) +#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \ PFERR_WRITE_MASK | \ From patchwork Tue Oct 18 21:46:10 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13011085 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 02266C4167D for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229999AbiJRVqt (ORCPT ); Tue, 18 Oct 2022 17:46:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53268 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230017AbiJRVq1 (ORCPT ); Tue, 18 Oct 2022 17:46:27 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BE3ABA90F for ; Tue, 18 Oct 2022 14:46:26 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id h4-20020a5b02c4000000b006bc192d672bso14629955ybp.22 for ; Tue, 18 Oct 2022 14:46:26 -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:message-id:reply-to; bh=e6vUZSm5Ysu3Ie0FZjoZ4ACjFfUmR0ukkZmmaTBIo2Y=; b=FF4z/R1ug4N+Ef+I+42aX96gdx/j+18q56mhQMwl6CNxqfgmUDOJ+bu9cE84BODxo/ cbyQg2UxAvdaeWRwU2OM6tFrQynwgLHGoyHqrdotP2kKIb/6Jzz87kTDb262tmRn+baN fjnZaXGfljR4GRSgUB6wfEcBjcy0lzK3lxasgQ5B18cbrx3cnUD4ghUf6EdLvJYpt2Fj MMa8k9lBz1Ou04zi2hBSoI+15jadQpPxEs8x8rbLQbXMhw9LF+q75rBXb68RzQDKQOrU 5aMe04SdEFlm/MF7NyjNE/OucwvZIU08CrbaKSY4DbgEu+4QlhIfTu5xedRuBLxRDOWG Vd6g== 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:message-id:reply-to; bh=e6vUZSm5Ysu3Ie0FZjoZ4ACjFfUmR0ukkZmmaTBIo2Y=; b=dt6MsnWVu2FCPS3X/iG8w9LPYTHSDAFe2QOEcdZNQxG79oY1rzh28s3S+hkSiWP7sz nh49mOVO+/rAhGQTTPguPYv9I5hsaKNXgIRdonx5uG9bmwOo8tRtVma+iyMGZCoADQA7 35jXyA4P+VZURtgpYoMGByrDEqbFn8odyMz3tfpyZZWT9NO04UXHJZXBJ81p2uLwKd67 vrZAB86W6EpHmxieKJnK9xup/e4NJNENzBO7ompAQroXhVbFospX+115HCO+sTGvd5E3 ZU42Lg5JfBKYIixE/teAUEzD9LKihlpdxw5DxbPi4pR5SmFmUATv8nEqmSXcAtBJ5iF0 ZvMA== X-Gm-Message-State: ACrzQf2GurBs/Rfl2FRRkBJ3jOQjvn4bP6ROdo30IAy9B86y4HZLRP7T nV+mIC5Gdzh26vwCW1gCvNsi40/QVBOLQA== X-Google-Smtp-Source: AMsMyM5APhfCJoyAC4iQcOZNhKzzqWq8GTx+OV8PNsoqML4Zy7JCeL9FSIAIm3rHF4dcXljJoZMxPVkLne22zw== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:264a:0:b0:6c0:3e00:f5b0 with SMTP id m71-20020a25264a000000b006c03e00f5b0mr4271819ybm.305.1666129585771; Tue, 18 Oct 2022 14:46:25 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:10 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-7-dmatlack@google.com> Subject: [PATCH v2 6/8] KVM: selftests: Copy KVM PFERR masks into selftests From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Copy KVM's macros for pafe fault error masks into processor.h so they can be used in selftests. Signed-off-by: David Matlack --- .../selftests/kvm/include/x86_64/processor.h | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 6762bc315464..c3ae21237af3 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -891,4 +891,26 @@ void virt_map_level(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, #define GET_REG(insn_byte) (((insn_byte) & 0x38) >> 3) #define GET_MOD(insn_byte) (((insn_byte) & 0xc) >> 6) +#define PFERR_PRESENT_BIT 0 +#define PFERR_WRITE_BIT 1 +#define PFERR_USER_BIT 2 +#define PFERR_RSVD_BIT 3 +#define PFERR_FETCH_BIT 4 +#define PFERR_PK_BIT 5 +#define PFERR_SGX_BIT 15 +#define PFERR_GUEST_FINAL_BIT 32 +#define PFERR_GUEST_PAGE_BIT 33 +#define PFERR_IMPLICIT_ACCESS_BIT 48 + +#define PFERR_PRESENT_MASK BIT(PFERR_PRESENT_BIT) +#define PFERR_WRITE_MASK BIT(PFERR_WRITE_BIT) +#define PFERR_USER_MASK BIT(PFERR_USER_BIT) +#define PFERR_RSVD_MASK BIT(PFERR_RSVD_BIT) +#define PFERR_FETCH_MASK BIT(PFERR_FETCH_BIT) +#define PFERR_PK_MASK BIT(PFERR_PK_BIT) +#define PFERR_SGX_MASK BIT(PFERR_SGX_BIT) +#define PFERR_GUEST_FINAL_MASK BIT_ULL(PFERR_GUEST_FINAL_BIT) +#define PFERR_GUEST_PAGE_MASK BIT_ULL(PFERR_GUEST_PAGE_BIT) +#define PFERR_IMPLICIT_ACCESS BIT_ULL(PFERR_IMPLICIT_ACCESS_BIT) + #endif /* SELFTEST_KVM_PROCESSOR_H */ From patchwork Tue Oct 18 21:46:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13011078 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 6FF96C433FE for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbiJRVqk (ORCPT ); Tue, 18 Oct 2022 17:46:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53276 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230025AbiJRVq2 (ORCPT ); Tue, 18 Oct 2022 17:46:28 -0400 Received: from mail-pl1-x649.google.com (mail-pl1-x649.google.com [IPv6:2607:f8b0:4864:20::649]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0EC4BC58A1 for ; Tue, 18 Oct 2022 14:46:28 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id f18-20020a170902ce9200b0017f9ac4cfb5so10565292plg.22 for ; Tue, 18 Oct 2022 14:46:28 -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:message-id:reply-to; bh=GvhtGfFxm+rsEds9rZdFp4GgpBQe+Q4wuoPZu0+CC8M=; b=SKh7dJq7PYHjULSNJYpDRJ1XSV7cJl8xeLqAYRgiGR1RANuoAC/CETZu6TZyJFGS5L rnn9mbO4a3TqH3wx41sR+cR52f2HVwbwkfT7lcZbyi6+xCpuxN/PpLimPlktFW0V0uNc 0rRMelsSQ3XaMr7hy/6O2lTpTnqmjnT6kn39vql1rqYQEUgzktuf9VgGS4wZxlyYL/pw WrI3cgUxCKoUMM4isIjL+aDkm2ODpiJwvYccx7oqBBm55zCpXKM3doVQSnWOamRMvmTE DoP4G3roOCsdwmg7vsTUyPLkG5M7aMHni2nIDsHC/7PtfPzzIIy0oPX9xUUhUPMQaNas CaTg== 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:message-id:reply-to; bh=GvhtGfFxm+rsEds9rZdFp4GgpBQe+Q4wuoPZu0+CC8M=; b=ekXIVXdXgZRO8Zvqk1Ir6t3Fk2lI2qK24cKnM+g5lhpR6eb7N+2JKs4HbMTgHfYyL6 70na0Y+tQ5whMz1BVT9XiqlTzJ/oKfsluF8spgduyAoV+aJVE4cgwsbaRKzyxu7MhREl ABIhamQr/G2o8P9iXcmPMdzld4Fqz6yUcv4uJkYHPT/7100RS9ZyqlE8Lw6iWlr1DveY mEqcewSioE/eYJh+zgteWa35IKUOZLo7PurN1DIPi1uTPVnK0cmZB9bYmerr/JrUMZzy tlEnMQmNbeLH/qVL9GdEmSH7EsRQoc+1bshINjcIhVM5yyNxHAtzPIA1jT+I2X7Jdwmt NOfw== X-Gm-Message-State: ACrzQf2FMesZIxVh4tmc6AQEFdJqmPfwQXo6pdKXm+MzYTn6ne+mBfb/ XYQbHRbvRcIbsBPiBjRx/TsX/iQkrN3KSQ== X-Google-Smtp-Source: AMsMyM6A52ss9lihnf1n1QSWKmamcIT2+iFT79ZD41SzVfwRyvRP43o742Tz5fkCrNDl0vO/XPJ9O1rNjLNWTg== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a17:90a:5408:b0:20a:d6b1:a2a7 with SMTP id z8-20020a17090a540800b0020ad6b1a2a7mr1873463pjh.2.1666129587322; Tue, 18 Oct 2022 14:46:27 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:11 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-8-dmatlack@google.com> Subject: [PATCH v2 7/8] KVM: selftests: Expect #PF(RSVD) when TDP is disabled From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Change smaller_maxphyaddr_emulation_test to expect a #PF(RSVD), rather than an emulation failure, when TDP is disabled. KVM only needs to emulate instructions to emulate a smaller guest.MAXPHYADDR when TDP is enabled. Fixes: 39bbcc3a4e39 ("selftests: kvm: Allows userspace to handle emulation errors.") Signed-off-by: David Matlack --- .../smaller_maxphyaddr_emulation_test.c | 45 +++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c index 91a85a00b692..afa9e0b3dd8a 100644 --- a/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c +++ b/tools/testing/selftests/kvm/x86_64/smaller_maxphyaddr_emulation_test.c @@ -21,6 +21,12 @@ #define MEM_REGION_SLOT 10 #define MEM_REGION_SIZE PAGE_SIZE +static void guest_page_fault_handler(struct ex_regs *regs) +{ + GUEST_ASSERT(regs->error_code & PFERR_RSVD_MASK); + GUEST_SYNC(PF_VECTOR); +} + static void guest_code(void) { flds(MEM_REGION_GVA); @@ -36,6 +42,23 @@ static void assert_ucall_done(struct kvm_vcpu *vcpu) uc.cmd, UCALL_DONE); } +static void assert_reserved_page_fault(struct kvm_vcpu *vcpu) +{ + struct ucall uc; + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + case UCALL_SYNC: + TEST_ASSERT(uc.args[1] == PF_VECTOR, + "Unexpected UCALL_SYNC: %lu", uc.args[1]); + break; + default: + TEST_FAIL("Unrecognized ucall: %lu\n", uc.cmd); + } +} + int main(int argc, char *argv[]) { struct kvm_vcpu *vcpu; @@ -50,6 +73,9 @@ int main(int argc, char *argv[]) TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR)); vm = vm_create_with_one_vcpu(&vcpu, guest_code); + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vcpu); + vm_install_exception_handler(vm, PF_VECTOR, guest_page_fault_handler); vcpu_set_cpuid_maxphyaddr(vcpu, MAXPHYADDR); @@ -70,10 +96,21 @@ int main(int argc, char *argv[]) vm_set_page_table_entry(vm, vcpu, MEM_REGION_GVA, pte | (1ull << 36)); vcpu_run(vcpu); - assert_exit_for_flds_emulation_failure(vcpu); - skip_flds_instruction(vcpu); - vcpu_run(vcpu); - assert_ucall_done(vcpu); + + /* + * When TDP is enabled, KVM must emulate the flds instruction, which + * results in an emulation failure out to userspace. Otherwise, no + * instruction emulation is required so check that the instruction + * generates #PF(RSVD). + */ + if (kvm_is_tdp_enabled()) { + assert_exit_for_flds_emulation_failure(vcpu); + skip_flds_instruction(vcpu); + vcpu_run(vcpu); + assert_ucall_done(vcpu); + } else { + assert_reserved_page_fault(vcpu); + } kvm_vm_free(vm); From patchwork Tue Oct 18 21:46:12 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13011081 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 E78F6C4167E for ; Tue, 18 Oct 2022 21:46:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229992AbiJRVqt (ORCPT ); Tue, 18 Oct 2022 17:46:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53482 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbiJRVqe (ORCPT ); Tue, 18 Oct 2022 17:46:34 -0400 Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 878E5B56D6 for ; Tue, 18 Oct 2022 14:46:30 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id p5-20020a25bd45000000b006beafa0d110so14478642ybm.1 for ; Tue, 18 Oct 2022 14:46:30 -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:message-id:reply-to; bh=o61Vq7W2J17Qc1HsTAbg5OmkjjMYQQCu/HIoTtKI/Ec=; b=iwBa7eC1TZGR7jM9XhzwjMtcQntC10Y6wR9bAcO6cy2fzs/s1VgZzRp8psDY2pr/zR LL9SCkdtg8dAZPIu0SVWr9iaP8qaPmMZr0Y6DIaR2xf2DQPN9vVRmoEJKUfffWVesazc ojGFBr66X3pZ6Beg3decYu6AaQuu39X8kxt8z9u/iAsQ/akSjf48lImCV1H2zmENU9F2 VKM+vB/1HAwzT4mg+Xy8lQ/xfdHWoTxCW5Txs5BHHPxRYjkki879IVCVIbArdkRQh3b0 Zcis7qta8gAZxnX6W65rkUHDqKoyc/RkjZg0oHPG6p/zvuTs/YjE+iW+AlIsBytROBWh adRA== 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:message-id:reply-to; bh=o61Vq7W2J17Qc1HsTAbg5OmkjjMYQQCu/HIoTtKI/Ec=; b=DzwZxz0RVHvGjNXsswNgk6kI+QiwlPatLIMWX/9LACaW9g45DEw89/0LXrXGJ55fNw x6+gJ5MH/+5w+VsyGkrBp4KurPCE+5B02H7x+KAJrm2lPKYIQn7+yWU9gWSu8YYHbU9E S2rHSeK8r92O04sKhhtclAD3sh85K8qwb1EQFcjT5qjnQsyUiHJA15asHuT6MY4Fp/iZ 3EoVPl2dwak4fvL6Fw7ClQkj8dP+a3uQP3fbKvOFBx4rfs/Qgx3UdXw3LIqWQhiTwMC0 qGpRH6n1Om4zeMBwPhWXfE/YhZef0zfKdQT5QELUsd83/u1B0yalUVxxNH3kOR3YW2Wh oxjg== X-Gm-Message-State: ACrzQf19Cy9Xn/BKNABB7hG9lRHN+LcDa6Np+MqmmySjhzMNUL2PV1P8 y2C7YISuVbTwMiIvyDBMbmfJvk00dkmMuw== X-Google-Smtp-Source: AMsMyM7XcenLIzI1LLn5FVzDM2wY5B7t3Jd36swoAjDoHs6z6NR23yJVi50BLBPnpAWft8r9h+lKhRx5j6eJrg== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:2509:0:b0:6c1:86e1:b8a1 with SMTP id l9-20020a252509000000b006c186e1b8a1mr4539453ybl.350.1666129589184; Tue, 18 Oct 2022 14:46:29 -0700 (PDT) Date: Tue, 18 Oct 2022 14:46:12 -0700 In-Reply-To: <20221018214612.3445074-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221018214612.3445074-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.0.413.g74048e4d9e-goog Message-ID: <20221018214612.3445074-9-dmatlack@google.com> Subject: [PATCH v2 8/8] KVM: selftest: Add a test for KVM_CAP_EXIT_ON_EMULATION_FAILURE From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , Jim Mattson , Peter Xu , Colton Lewis , Aaron Lewis , kvm@vger.kernel.org, David Matlack Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add a selftest to exercise the KVM_CAP_EXIT_ON_EMULATION_FAILURE capability. This capability is also exercised through smaller_maxphyaddr_emulation_test, but that test requires allow_smaller_maxphyaddr=Y, which is off by default on Intel when ept=Y and unconditionally disabled on AMD when npt=Y. This new test ensures we exercise KVM_CAP_EXIT_ON_EMULATION_FAILURE independet of allow_smaller_maxphyaddr. Signed-off-by: David Matlack --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../x86_64/exit_on_emulation_failure_test.c | 42 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index c484ff164000..22807badd510 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -16,6 +16,7 @@ /x86_64/cr4_cpuid_sync_test /x86_64/debug_regs /x86_64/evmcs_test +/x86_64/exit_on_emulation_failure_test /x86_64/fix_hypercall_test /x86_64/get_msr_index_features /x86_64/kvm_clock_test diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 90c19e1753f7..55b6f4efa57c 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -81,6 +81,7 @@ TEST_GEN_PROGS_x86_64 = x86_64/cpuid_test TEST_GEN_PROGS_x86_64 += x86_64/cr4_cpuid_sync_test TEST_GEN_PROGS_x86_64 += x86_64/get_msr_index_features TEST_GEN_PROGS_x86_64 += x86_64/evmcs_test +TEST_GEN_PROGS_x86_64 += x86_64/exit_on_emulation_failure_test TEST_GEN_PROGS_x86_64 += x86_64/fix_hypercall_test TEST_GEN_PROGS_x86_64 += x86_64/hyperv_clock TEST_GEN_PROGS_x86_64 += x86_64/hyperv_cpuid diff --git a/tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c b/tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c new file mode 100644 index 000000000000..8e98ad3259de --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2022, Google LLC. + * + * Test for KVM_CAP_EXIT_ON_EMULATION_FAILURE. + */ + +#define _GNU_SOURCE /* for program_invocation_short_name */ + +#include "flds_emulation.h" + +#include "test_util.h" + +#define MMIO_GPA 0x700000000 +#define MMIO_GVA MMIO_GPA + +static void guest_code(void) +{ + /* Execute flds with an MMIO address to force KVM to emulate it. */ + flds(MMIO_GVA); +} + +int main(int argc, char *argv[]) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + /* Tell stdout not to buffer its content */ + setbuf(stdout, NULL); + + TEST_REQUIRE(kvm_has_cap(KVM_CAP_EXIT_ON_EMULATION_FAILURE)); + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + vm_enable_cap(vm, KVM_CAP_EXIT_ON_EMULATION_FAILURE, 1); + virt_map(vm, MMIO_GVA, MMIO_GPA, 1); + + vcpu_run(vcpu); + assert_exit_for_flds_emulation_failure(vcpu); + + kvm_vm_free(vm); + return 0; +}