From patchwork Wed Nov 2 18:46:45 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028879 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 0EDDEC433FE for ; Wed, 2 Nov 2022 18:47:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231502AbiKBSrD (ORCPT ); Wed, 2 Nov 2022 14:47:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41996 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231441AbiKBSrA (ORCPT ); Wed, 2 Nov 2022 14:47:00 -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 4335C2CE27 for ; Wed, 2 Nov 2022 11:47:00 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id p66-20020a257445000000b006ca0ba7608fso16940285ybc.7 for ; Wed, 02 Nov 2022 11:47:00 -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=b7uq7+6FJUtCM5yZaRMCgjXsjZDgsouXY7vk22+ZXIs=; b=XDgeZyKDTKKipX+i2aGyHNi3s+8pnkpnMI12cgPv4ojpprE+Fg9YnzSmNNsXX8HfJj iQbxfEndlSN9abt6doem7fwJJXekYak3qgfa1e23bo7K9164k5zp7hw1o60qcSIVJ2w5 drYOm4FQw/MYk0vW8prIJdruby48uiOoct5SUhO7BjVnv3fPIL/h5KidH0sDSufi9qzW DpKq7TkYLamfrhWnqBMaBIHpEeY/o280to+UOlNzldbdEpoWBRgZHLGKJS+Sk0rLiOz7 ZRGobOy+GEWhz0thGXVb31D7vBrs3byLm/297DioRssWpXwKLAWCIyi89HRJVJim6mxZ BR6w== 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=b7uq7+6FJUtCM5yZaRMCgjXsjZDgsouXY7vk22+ZXIs=; b=TTnSdC7GlkyGu24Yr0+rYgyUiM9c3em5/x/A3Hq7RfghtfYD5DGM3d7orP9JRVmCoR W3LAkWh8CoLr541Q7R3x4uMHzoRQXZ5L8mQF4hVt0CfrX1evhRpKNUjxfBL+2qaCLt77 rtPQSGYeR7vWncS7ROxtNCOrlNZ3HvpQQGpW6mZ+GOCxFk3YacjrjEPOrT7phpPTw2Pb RyexEYkZiieF92JCSp9n98zv4Vq+u26hYp4fChxDXva8SfJ7ij7B1hINylHw+Zkv6k3D Qwk1lC6a6D7rq4lUFKcn45G1Fh2Cj9p8jOL3ZluzXKGOqfHKi5Hzh1SRJjSPgBjEAvuN TrXA== X-Gm-Message-State: ACrzQf0d/Vpi8Nl26Yz3OyNf7BCJ7dJUaw+ORjefPDDvXWtJeClXgSra Zj1Nmlojxd4gMCOFJ1impwROgXFIyyg+iw== X-Google-Smtp-Source: AMsMyM6ztd9xyXefhdqKbXs4TQ/5pf47M9ZV5C6tp0cuCubpRbzjdpMp5DlM5P9ogxJwG548M2iggV6hX5AAZA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a5b:f4e:0:b0:6bc:9f5a:96f0 with SMTP id y14-20020a5b0f4e000000b006bc9f5a96f0mr177191ybr.10.1667414819125; Wed, 02 Nov 2022 11:46:59 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:45 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-2-dmatlack@google.com> Subject: [PATCH v4 01/10] KVM: selftests: Rename emulator_error_test to smaller_maxphyaddr_emulation_test From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org 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 2f0d705db9db..053e5d34cd03 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -17,7 +17,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 @@ -36,6 +35,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 0172eb6cb6ee..ab133b731a2d 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 @@ -96,6 +95,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 Wed Nov 2 18:46:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028880 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 150F4C4332F for ; Wed, 2 Nov 2022 18:47:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231639AbiKBSrF (ORCPT ); Wed, 2 Nov 2022 14:47:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42010 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231336AbiKBSrC (ORCPT ); Wed, 2 Nov 2022 14:47:02 -0400 Received: from mail-pf1-x449.google.com (mail-pf1-x449.google.com [IPv6:2607:f8b0:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90B0A2CE27 for ; Wed, 2 Nov 2022 11:47:01 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id k11-20020aa792cb000000b00558674e8e7fso9475840pfa.6 for ; Wed, 02 Nov 2022 11:47:01 -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=ql4D3kiOZzzmaaxOdFm8FO+vhwnQsfC/UdEU/ar8O0w=; b=aNUkXBxNmbQdSOVWv/Iru8y2NflwpO3ygzFS+IJT5/Cf8/2z63W9e3jYx+G37tLmuR uPOhSWPTbC2R7qdSaC14LMM7IvbB7ygmTGx6qBlJi7RUGTqucbqwElkWrgUFczYdLxRL rSeeO+qJRkWJ9KMDIl/9lQky6rdAJVXS6BO6TCldqcHYGfZgQq6QdS8mvOrUsWBpUIkd Bf4X/NRvFKb3/JNPPWRfXs631Uh1uhJ6hHASPauf3Ufvq7ggVIXra2yCgt9GLT9JCZGR xIVL4tiQukJwdNXGN4YL4E00JVdzV6HNi0oZjwvaALhAndPrKKIVN+3mdEeo7n4YKlFj xneQ== 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=ql4D3kiOZzzmaaxOdFm8FO+vhwnQsfC/UdEU/ar8O0w=; b=tBr3UL7CQDts91NcsF+tt8W7RIu5OOvXEMZCy06zZTfWnORCfektGQhnDsjdsOZs8J LnD3lVzXyl9EQlm6M7RSq3JooVdmNGekgxEloJKErzmZ/QWHZK8HsduStpxk4acXFgqm Q4zIU1ko6SdEVv24V7DiH+San11WK6QEMvN54zFLGrWonX+/a0codLT+9yI1/nRgc1rp 0N8L7MdWNU4AmGiHfqdW2oi/GbyUEwuz3hHpMIjm70NiJJISmzXs0fU/FxwB1yu7hkF0 0TpuDcbtXRyEN5YvJmKpplcrkhsHnhvFrI9NOmiMAs3DUsLfwvb+gaVVdyqJoMd9Hs/J 5jgg== X-Gm-Message-State: ACrzQf2NXp+EQROJ6sArFpd6+LMU9lzIqnuokcloWO/3UlcqltPCSqQh +jhA/yZOlLseoeI1vv0y96IyxoRek+7hWA== X-Google-Smtp-Source: AMsMyM5q5qyLQVIN/MGOhr7mcX1yfMtLR66i3kecMb/7rU9e44+PG++MAUT2C4Z1Um9dIO4ffifVTmdnv8tL7g== 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:230d:b0:53d:c198:6ad7 with SMTP id h13-20020a056a00230d00b0053dc1986ad7mr26953339pfh.67.1667414820988; Wed, 02 Nov 2022 11:47:00 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:46 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-3-dmatlack@google.com> Subject: [PATCH v4 02/10] KVM: selftests: Explicitly require instructions bytes From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hard-code the flds instruction and assert the exact instruction bytes are present in run->emulation_failure. 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(). Note that KVM does not necessarily return exactly 2 bytes in run->emulation_failure since it may not know the exact instruction length in all cases. So just assert that run->emulation_failure.insn_size is at least 2. Signed-off-by: David Matlack Reviewed-by: Sean Christopherson --- .../smaller_maxphyaddr_emulation_test.c | 68 ++++++------------- 1 file changed, 20 insertions(+), 48 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..d92cd4139f6d 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 @@ -19,41 +19,20 @@ #define MEM_REGION_SLOT 10 #define MEM_REGION_SIZE PAGE_SIZE +#define FLDS_MEM_EAX ".byte 0xd9, 0x00" + static void guest_code(void) { - __asm__ __volatile__("flds (%[addr])" - :: [addr]"r"(MEM_REGION_GVA)); + __asm__ __volatile__(FLDS_MEM_EAX :: "a"(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, @@ -65,30 +44,23 @@ 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"); + + TEST_ASSERT(run->emulation_failure.insn_size >= 2, + "Expected a 2-byte opcode for 'flds', got %d bytes", + run->emulation_failure.insn_size); + + insn_bytes = run->emulation_failure.insn_bytes; + TEST_ASSERT(insn_bytes[0] == 0xd9 && insn_bytes[1] == 0, + "Expected 'flds [eax]', opcode '0xd9 0x00', got opcode 0x%02x 0x%02x\n", + insn_bytes[0], insn_bytes[1]); + + vcpu_regs_get(vcpu, ®s); + regs.rip += 2; + vcpu_regs_set(vcpu, ®s); } static void do_guest_assert(struct ucall *uc) From patchwork Wed Nov 2 18:46:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028881 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 8B106C4167D for ; Wed, 2 Nov 2022 18:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231683AbiKBSrH (ORCPT ); Wed, 2 Nov 2022 14:47:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42024 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231520AbiKBSrD (ORCPT ); Wed, 2 Nov 2022 14:47:03 -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 2B89D2CE27 for ; Wed, 2 Nov 2022 11:47:03 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-36fc0644f51so165660277b3.17 for ; Wed, 02 Nov 2022 11:47:03 -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=cMB6TX5uDf1QAJ7HvbUaBRObClM1MNgowri3IAfpFFY=; b=VauRlSFVm86d5oJHtfg4zSldCKdbxKThcr5I1twnfjAgsrppOQ0DbeQRgCa/HXam/Q F94c+F7Ek6v10tbSmme1HDxuMaM0ZK78uhE/DR/xGFMUaCw/Z3UxYop+jaliu3xydblO oSFTWwMXjoDH9V2WzB5BH/xcZ2FpjaN0B0MI9A6+Jb0++jVvTgfi/Hz1hAZYasr1rfjs VvZQpGkagO18C2IaWWMUja/grz3WSJrwkbV3CvvY6GtUiL+G2CoSsReyqq/1IH/mgWHA PF0lWD28fv6JcdmQLusZJ6ZHpzINCaPDT5KYGWg0EKdyeS9DJN1xh4gHKkboNl+QVPnL UkYA== 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=cMB6TX5uDf1QAJ7HvbUaBRObClM1MNgowri3IAfpFFY=; b=68vDa4xgTw01vfHkMQWNEFG6GuQ00kl66NdCO2UH/gkw2oZDZDgisV2NQxSvZT54vU xPiEawBu9vDZ5F4ZRx1DDPtF3QrfRa4loUObfbzrt7/yoFtlkSaLUxFGfeWle3lTOXKm 0V1ZQFz2gHjYcHypl9srOT2yTb9v1+MjWEPE51qtb25bc+xwNZsgzIbGGZ1JEIX7OebY Je2vUjawpcmT1mHRW0IGl3mPISggs3UeXUQ5uEWN5w4Saf0N/bXoxJq4a0FjZmNdLDcn O0Hf/SoN7wU/z8TpSndNcj1/XrktlVHtl/MSF41sE3+kLdSQwgaWFlRRImwCTYYvUT2i jAuw== X-Gm-Message-State: ACrzQf0zl16TE0e2Y2QF+7BVTjdhDr91ByGh31y07tEyU0DEhR+PAlmF v6PckK9Z25A0dyuA8LnodN8CKB2HBseTKw== X-Google-Smtp-Source: AMsMyM4+47uBszbYBydrPC4GTCo0jqqlsf8OemUUFXinQvbfOQDsvqzYf9lbWqCVoc2BTk+jDzfNgYPmPZ20bQ== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a81:6f88:0:b0:35f:df0e:3a7a with SMTP id k130-20020a816f88000000b0035fdf0e3a7amr25239243ywc.416.1667414822529; Wed, 02 Nov 2022 11:47:02 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:47 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-4-dmatlack@google.com> Subject: [PATCH v4 03/10] KVM: selftests: Delete dead ucall code From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , 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 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 Reviewed-by: Sean Christopherson --- .../smaller_maxphyaddr_emulation_test.c | 61 +------------------ 1 file changed, 1 insertion(+), 60 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 d92cd4139f6d..f9fdf365dff7 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 @@ -63,64 +63,6 @@ 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) -{ - 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; @@ -157,8 +99,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_EQ(get_ucall(vcpu, NULL), UCALL_DONE); kvm_vm_free(vm); From patchwork Wed Nov 2 18:46:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028882 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 9DD30C4332F for ; Wed, 2 Nov 2022 18:47:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231689AbiKBSrJ (ORCPT ); Wed, 2 Nov 2022 14:47:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231666AbiKBSrF (ORCPT ); Wed, 2 Nov 2022 14:47:05 -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 31A582F66B for ; Wed, 2 Nov 2022 11:47:05 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id y6-20020a25b9c6000000b006c1c6161716so17028118ybj.8 for ; Wed, 02 Nov 2022 11:47:05 -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=rZlY20pisID3UMdvyEPOsDdyNuMTsJMxwMVwMHhsyEA=; b=KNbO5OFWGRx1Sf9wqORfzfux0wAUYpDYYqoWVzcDfEHuZCvzhZJhCo4vmrbBjVxkvY VNGAf8BNUlbiZjjclnqE4NI1QkXMeEyNylOdjhzfk1Uv0RDRVG+qHyTXD/oz8WWxwAz1 6O3PWRsf3jF7iBm5p2IacbAm33P5mTBDMrWLnzrHA0ur2JxjfFj/bEV2mEUvVnk48lbX Sa+z/ag63DF2d3I1cPm+QHuNMZwTPvnnO0/mgRN66FEywFzB7q2xT1A0mdCVBKzw0Yv6 8qh2XilcDj2Fs1/rgRZtnm2kb4iG74TuVY6rv50E7xTp2vZrhg2GYw9eRrxSG07aqNGY 03LQ== 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=rZlY20pisID3UMdvyEPOsDdyNuMTsJMxwMVwMHhsyEA=; b=1ZUSqHxhi7meVpo5095B4Ng4Dp5XvpHSzOO4DRyAqRASm2h3cgkxhS5MusZbZC/xcS vzQEdcg3XYw4hSJA6Ldwm0VwV+73fXTXRywJrGjDSvjY7hIhbxXUObIDBcx4YPz2fxJw WBLVU/vU0E2OKfn2ExmchQzc71UnEse0t04fK5RMq+i3GgCM15XqxhJrRS0ygOs2Kxb3 pb1ELMIlWqp8JgNnKxM2yoWChmh6Eq6MzzHeHyrngxF3csy7G2xoTbAq9qvv/FPurCtS pa43khIcCTsBdLLe0vhWTgLROhXsllkrp72zT+Q5Q4DdjFJl33e4LlynURdzvCAFBUXz ykyw== X-Gm-Message-State: ACrzQf09aS5TTwrW1KNCl7zFdeiMdpHEzTOHghu3mh4jyJwvA1DSskLW RxWvut9moKeaw5KgO+z1a3LnZmWw7xXpFg== X-Google-Smtp-Source: AMsMyM6QT10t0FAYm56F0kG0IqnbQ/spL61Nwk7MXdKMTo7EfSDMgIZDdtM1jM4PphXGFDEkYqwgxikHQ3hM/w== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:25d7:0:b0:6cb:77ee:61a0 with SMTP id l206-20020a2525d7000000b006cb77ee61a0mr193123ybl.498.1667414823936; Wed, 02 Nov 2022 11:47:03 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:48 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-5-dmatlack@google.com> Subject: [PATCH v4 04/10] KVM: selftests: Move flds instruction emulation failure handling to header From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org 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/x86_64/flds_emulation.h | 55 +++++++++++++++++++ .../smaller_maxphyaddr_emulation_test.c | 44 ++------------- 2 files changed, 59 insertions(+), 40 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86_64/flds_emulation.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..e43a7df25f2c --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/flds_emulation.h @@ -0,0 +1,55 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SELFTEST_KVM_FLDS_EMULATION_H +#define SELFTEST_KVM_FLDS_EMULATION_H + +#include "kvm_util.h" + +#define FLDS_MEM_EAX ".byte 0xd9, 0x00" + +/* + * 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_MEM_EAX :: "a"(address)); +} + +static inline void handle_flds_emulation_failure_exit(struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + struct kvm_regs regs; + uint8_t *insn_bytes; + 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"); + + TEST_ASSERT(run->emulation_failure.insn_size >= 2, + "Expected a 2-byte opcode for 'flds', got %d bytes", + run->emulation_failure.insn_size); + + insn_bytes = run->emulation_failure.insn_bytes; + TEST_ASSERT(insn_bytes[0] == 0xd9 && insn_bytes[1] == 0, + "Expected 'flds [eax]', opcode '0xd9 0x00', got opcode 0x%02x 0x%02x\n", + insn_bytes[0], insn_bytes[1]); + + 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 f9fdf365dff7..9d0e555ea630 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" @@ -19,50 +21,12 @@ #define MEM_REGION_SLOT 10 #define MEM_REGION_SIZE PAGE_SIZE -#define FLDS_MEM_EAX ".byte 0xd9, 0x00" - static void guest_code(void) { - __asm__ __volatile__(FLDS_MEM_EAX :: "a"(MEM_REGION_GVA)); - + flds(MEM_REGION_GVA); GUEST_DONE(); } -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; - 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"); - - TEST_ASSERT(run->emulation_failure.insn_size >= 2, - "Expected a 2-byte opcode for 'flds', got %d bytes", - run->emulation_failure.insn_size); - - insn_bytes = run->emulation_failure.insn_bytes; - TEST_ASSERT(insn_bytes[0] == 0xd9 && insn_bytes[1] == 0, - "Expected 'flds [eax]', opcode '0xd9 0x00', got opcode 0x%02x 0x%02x\n", - insn_bytes[0], insn_bytes[1]); - - vcpu_regs_get(vcpu, ®s); - regs.rip += 2; - vcpu_regs_set(vcpu, ®s); -} - int main(int argc, char *argv[]) { struct kvm_vcpu *vcpu; @@ -97,7 +61,7 @@ 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); + handle_flds_emulation_failure_exit(vcpu); vcpu_run(vcpu); ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE); From patchwork Wed Nov 2 18:46:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028883 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 5116CC4332F for ; Wed, 2 Nov 2022 18:47:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231702AbiKBSrN (ORCPT ); Wed, 2 Nov 2022 14:47:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231681AbiKBSrH (ORCPT ); Wed, 2 Nov 2022 14:47:07 -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 C954F2FC36 for ; Wed, 2 Nov 2022 11:47:06 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id t9-20020a5b03c9000000b006cff5077dc9so1884624ybp.3 for ; Wed, 02 Nov 2022 11:47:06 -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=3fJGVLvuux7ZjXhWn/U9v5+1PBfr8qV0mPVrMinacoE=; b=tUUyu5SD3bD2emAIVs5TbHN4UbJlKx4cIjvc9WE6Dx7OleokmsFQE0dlBCTRBec/HB I1nHljLKNFf7UbeVg2g4tNLCxaU275XQKlUTZF7vveoUHMNZ24Zjj4wNuFIJf6TYbmOn qIn7ESBQK0x+sbXBpPRJ5+sAc1e7rEdY6x+XG1OnN4TNgHATusz01eicl6zNoE7lXExL kkAf1hbporckbz3+HcWThq/iFfWZ9GmMwPsjSuVMSlHogmY9WKB/HcCqJDa+IyyMbIMr Vf2TysbM7ssiiJ5h/NqzlHB6l/rH09+wBV0WobW/dgwRj3lnLBb4tRVraVh4Mz3uzqef fJUA== 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=3fJGVLvuux7ZjXhWn/U9v5+1PBfr8qV0mPVrMinacoE=; b=1bio9FHdYef4CfywTWrYpnQTSsR1oA2TTxJY4UP9m5w1rm+3HzYNXnoCH+U8o7WP3v /rZTLyvy1Ls+ttp9Zj/Rc8EsZyghLaIrbgVqwpFj1Q6DJPdg/Jb/YL/0520j5Pfb2KCL z2vOXJAMpDRZFRShQ7IppIk0tHn+427CJQt/OELRQSWLbofK8erMva25LdtERqThCDBg goJF+73ahOEjstyeQ3fCQvvuTRggpVMaDowcjynFZafi3FvyWy+9HAp7hXng6rMPJtWP ucjrrDyhWoLUo6RuQdhwkbPqPdtMzZwCLSLzjuA89ELMWburcTa5+XGvkRdCjzr3+LX7 uq+g== X-Gm-Message-State: ACrzQf3tBjTEGzcZzGFScJYyv9JNxQu6OjRo4CTO1n3htMJKwksyUnuM UdL2nDaVdfrBPyxAufvXSZDxTKg+mOk54g== X-Google-Smtp-Source: AMsMyM4ALx73ySVgxvPN4T4NGOYv0Xh/sR4+BRuBQBMXmwmEMYFgZfYYrR3S692+nQLNSyBBaBTNU9huY+O5UQ== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:d0d1:0:b0:6cb:3ae4:7de2 with SMTP id h200-20020a25d0d1000000b006cb3ae47de2mr25494210ybg.505.1667414826096; Wed, 02 Nov 2022 11:47:06 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:49 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-6-dmatlack@google.com> Subject: [PATCH v4 05/10] KVM: x86/mmu: Use BIT{,_ULL}() for PFERR masks From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org 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 415113dea951..716f165cfa10 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -256,16 +256,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 Wed Nov 2 18:46:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028884 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 E132BC4332F for ; Wed, 2 Nov 2022 18:47:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231680AbiKBSrP (ORCPT ); Wed, 2 Nov 2022 14:47:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42124 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231694AbiKBSrI (ORCPT ); Wed, 2 Nov 2022 14:47:08 -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 EE3462CE27 for ; Wed, 2 Nov 2022 11:47:07 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-36fc0644f51so165662597b3.17 for ; Wed, 02 Nov 2022 11:47:07 -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=3kR62HSIiTQCzwGmQgs5HY6YJRVWbPr3ykoc+jji2yw=; b=q5DQoPzrYWPMyKqVo1p7fe42p1gFesUoA0SbdaAzEHPMfWMQYOsQtlQUppP9XvLEQ/ tZ0H8d2Azp17sw6x/LWkhKmx770K+fHmREw+0gPtt+zV2jUV/icvoL5tVVND7cgQ1TsM uG/oldWXppmgBts7lzQaFL+QnHf5V6Yh3llW+NhQhNWBpw3W7SnXjTQy4XYODmtHD/K3 nCfYHNy2o8FT6rSG1l/To4WShg7NPGtwDGSrRJjb9QX403y9Z5sJHSlcY2OPf3rx2meE eD7+HqgcqC6NfT117EUE+ujUcqZKv+4YX8UOaAkWPfOAUuod+Hh3skEOHXvFmEMdKpY/ YXAQ== 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=3kR62HSIiTQCzwGmQgs5HY6YJRVWbPr3ykoc+jji2yw=; b=flx9Kz6/rug8TAZgFXu4lTybdiUpSMneRTnNujaqxr0UtSHj/MVilHTrAbJVUpN4xW 4+tb4thjLF4Dn6vix/bi2k3YaZXiBut51+UhJsexAew7GKOIaeT4ktwnJ9KwcGT0ugmp bB0P/houeN+gTscMxK32rSZAcF7L8HrCyxcwszmWxbCOes37dxplbmFnaZeiqJT9afJr U/u4un/jpIlfc9ev5GKf+lhXu81Oylke44zVduTn71rgr5Icm1MwPYCd83jkANjLIX4c PxktsaGf5NHJCvVeYrxbytuGk/0+igCs917Xbrdg+cZnzMPoO70UY3oIkxG3NKPUVm7a drlg== X-Gm-Message-State: ACrzQf0MyP23blP0qX8VD1tGjD2pztQtXgLZVKxOo9q98J102RFWI40d 4FOzXI+OZSz3gyw5MBrIAw7y6mDvfEC2lg== X-Google-Smtp-Source: AMsMyM4CTD6h0hiYzEZTXKWUZW2tL3Y5DhoZKaapI828Ce/Kz9P/BgOTzVbndv8TZmPXKfphxjQ6Q6P9Aqz9EA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a05:6902:348:b0:6be:b67e:8c24 with SMTP id e8-20020a056902034800b006beb67e8c24mr26065847ybs.112.1667414827708; Wed, 02 Nov 2022 11:47:07 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:50 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-7-dmatlack@google.com> Subject: [PATCH v4 06/10] KVM: selftests: Copy KVM PFERR masks into selftests From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Copy KVM's macros for page fault error masks into processor.h so they can be used in selftests. Signed-off-by: David Matlack Reviewed-by: Sean Christopherson --- .../selftests/kvm/include/x86_64/processor.h | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index e8ca0d8a6a7e..f7249cb27e0d 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -882,4 +882,27 @@ 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) + +#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 Wed Nov 2 18:46:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028885 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 53378C4332F for ; Wed, 2 Nov 2022 18:47:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231707AbiKBSrT (ORCPT ); Wed, 2 Nov 2022 14:47:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231666AbiKBSrK (ORCPT ); Wed, 2 Nov 2022 14:47:10 -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 B4D5F2F67C for ; Wed, 2 Nov 2022 11:47:09 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id e8-20020a5b0cc8000000b006bca0fa3ab6so16982454ybr.0 for ; Wed, 02 Nov 2022 11:47:09 -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=EumOl5mGgVnCEzDA+UueSGzDQoNJ9RAkypwsXwSdasQ=; b=UZJ4hHhdrFkLZ870xr/rjRoaNrd6lIUolSt2myKZViZ/GGgQJ/pbadILfT8PMgjEmu ZYJWMBFgnYN1VAS1sLdD1zns2vEJ7+9ZGXFVeb557F/DY1ywKHMoyiUM5NbF4Qf0vWV0 HWQGgu6owiFBTWoUE5C8Xs10LQnj5RtBCy/8pYbOiom4+jomeGFSih3281H71Xj7S1CY D03bNpNLbV1lB7R/ldNZ8/3BuLzXQIaL2U2XmlK0oMiMBUyuxlinHckz1iU6srG3GjKh w2zFicVm0sILsMZZH9FSCLtgKgywo4OjhLDhwc4g+PEgq1k0SfpdqxDIGTaNTZ81gLoW VJgQ== 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=EumOl5mGgVnCEzDA+UueSGzDQoNJ9RAkypwsXwSdasQ=; b=7SZtV8zFE2msxO4kQBt42Jdl7/AtDYJdECtttvvO9nm/rbCxu4Qc1pzMnIWoUkRZmK 6cfy1/XXLvyEPpDVT2Y68LV7xhaOfboGlVpWkLk/BQehxtPg6KUyL6cTwCXs6PogskYm rhg1rNR59rO8sWY5Zae+xUarfmHk57n72679Pi5hdT1iKuruWPFlUNtKwv2KNGIKX88T Kwk05dAjKJzdWytmUe7nIupYc6QOXRlDZw59ufCvUB42ttrH8x6kPMPZGIKnBDCKr1za qz59lhI9kHXvkpKJDTxzFzA9AJlhHRXFSgbe7fOjf227PPDpTMOnxy6tUYq/js7FoxWE GAKQ== X-Gm-Message-State: ACrzQf2lBxHElFP4SnYRw33rOx+FTof36bXn7eXDNIJdlnr5jea+KI2G 093CRHOkWB6haxnNMU7bwvLleiNrUqiFXQ== X-Google-Smtp-Source: AMsMyM5ppov+wqEqcSKl5tRhi1ODfgtFQA4ohSDwa8CL7qUIHi6UlCTVjKAHH7wax6oVG0rPEXTX5URrinBHZA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:d491:0:b0:6ca:ef3c:d5f6 with SMTP id m139-20020a25d491000000b006caef3cd5f6mr23347532ybf.343.1667414829036; Wed, 02 Nov 2022 11:47:09 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:51 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-8-dmatlack@google.com> Subject: [PATCH v4 07/10] KVM: selftests: Avoid JMP in non-faulting path of KVM_ASM_SAFE() From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Sean Christopherson Clear R9 in the non-faulting path of KVM_ASM_SAFE() and fall through to to a common load of "vector" to effectively load "vector" with '0' to reduce the code footprint of the asm blob, to reduce the runtime overhead of the non-faulting path (when "vector" is stored in a register), and so that additional output constraints that are valid if and only if a fault occur are loaded even in the non-faulting case. A future patch will add a 64-bit output for the error code, and if its output is not explicitly loaded with _something_, the user of the asm blob can end up technically consuming uninitialized data. Using a common path to load the output constraints will allow using an existing scratch register, e.g. r10, to hold the error code in the faulting path, while also guaranteeing the error code is initialized with deterministic data in the non-faulting patch (r10 is loaded with the RIP of to-be-executed instruction). Consuming the error code when a fault doesn't occur would obviously be a test bug, but there's no guarantee the compiler will detect uninitialized consumption. And conversely, it's theoretically possible that the compiler might throw a false positive on uninitialized data, e.g. if the compiler can't determine that the non-faulting path won't touch the error code. Alternatively, the error code could be explicitly loaded in the non-faulting path, but loading a 64-bit memory|register output operand with an explicitl value requires a sign-extended "MOV imm32, r/m64", which isn't exactly straightforward and has a largish code footprint. And loading the error code with what is effectively garbage (from a scratch register) avoids having to choose an arbitrary value for the non-faulting case. Opportunistically remove a rogue asterisk in the block comment. Signed-off-by: Sean Christopherson Signed-off-by: David Matlack --- tools/testing/selftests/kvm/include/x86_64/processor.h | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index f7249cb27e0d..9efe80d52389 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -764,7 +764,7 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, * for recursive faults when accessing memory in the handler. The downside to * using registers is that it restricts what registers can be used by the actual * instruction. But, selftests are 64-bit only, making register* pressure a - * minor concern. Use r9-r11 as they are volatile, i.e. don't need* to be saved + * minor concern. Use r9-r11 as they are volatile, i.e. don't need to be saved * by the callee, and except for r11 are not implicit parameters to any * instructions. Ideally, fixup would use r8-r10 and thus avoid implicit * parameters entirely, but Hyper-V's hypercall ABI uses r8 and testing Hyper-V @@ -786,11 +786,9 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, "lea 1f(%%rip), %%r10\n\t" \ "lea 2f(%%rip), %%r11\n\t" \ "1: " insn "\n\t" \ - "movb $0, %[vector]\n\t" \ - "jmp 3f\n\t" \ + "xor %%r9, %%r9\n\t" \ "2:\n\t" \ - "mov %%r9b, %[vector]\n\t" \ - "3:\n\t" + "mov %%r9b, %[vector]\n\t" #define KVM_ASM_SAFE_OUTPUTS(v) [vector] "=qm"(v) #define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11" From patchwork Wed Nov 2 18:46:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028886 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 4E37AC433FE for ; Wed, 2 Nov 2022 18:47:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231697AbiKBSrV (ORCPT ); Wed, 2 Nov 2022 14:47:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231678AbiKBSrN (ORCPT ); Wed, 2 Nov 2022 14:47:13 -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 B5B6D2FFD8 for ; Wed, 2 Nov 2022 11:47:11 -0700 (PDT) Received: by mail-pl1-x649.google.com with SMTP id e13-20020a17090301cd00b001871e6f8714so6713782plh.14 for ; Wed, 02 Nov 2022 11:47:11 -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=KMoIap0sR+1CcUsNo9fMIcDCtwhfe98yh5fTCarrD0c=; b=k1yXSjshfABPqBWzkk2re+5NTx0r1/omNxYPIhvfkrWPWOXQ3FDs8tlXd0BS10PfBn sIs/Hi3l8f6BNYAm0REVTPHyrmG9K7BzizWj7vjmVWwyUdSpJ3jse/1wzbTuYuz346fA odG+5EaTWzlr8ZbCQA34/E/aQoFtQ5fsxGYXp+OUKV4vuVHfIg+MSfLquVktJihbS6m8 /IXUNoe8XbV7njQf5AOmZzjox2KDVvFzIsaxo/RD728X41FYQpqh3nDVRURNPDLUR34b L2qlQhINf0RH3gnunJd8nBJznViQZZh5hXHrNszL5J2v2bsPCQ2iuOkTKKS8+aHndIcP BAbQ== 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=KMoIap0sR+1CcUsNo9fMIcDCtwhfe98yh5fTCarrD0c=; b=yx45q6V6PGLh1v1wLGp9XGNwjAeTEq6O0fadpfUgfpRdQd7zzi59nyn2siO0IL7U5H VdfN7mRqJWvCW09Ak+0eyHT5vpqLkc7bpmStDdXscpjpGYr4LtW7K4fl+8IZR4W1DZNe RVrjJhgbV9xQ8UKvyX5w2P3yqO+lFrIVCpAz943cViIBWhGAqPC6L5xIUt6UhI9PIQzr Pmjh+7s+mWcLiKxJlDbekK7PaWfAum7YEYWAdXQJw0fFS0m3QEHFEsNWzpceDoLwj658 MEw9Yv/eYnyh5WV8t854oUH3xownU5YKhCqNhThnQ7sdowiDJ4JIZ1AxSZ0TgWDdsvN6 h1sg== X-Gm-Message-State: ACrzQf1E7fJD5WTsfA4JBvWn9PaugngWM5+eV4elKC5VyvHRBLKqQlac Gcd3CbVafoyFLVCaB3HluuRVlwtDZ4/6vA== X-Google-Smtp-Source: AMsMyM48FymbReh5snaAz2jmylT9nC4FzX0GZg+//beH/EXpya9DpGzRz3RTzXCaxExeLJ4ck1uJogvMSA7pJA== 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:f291:b0:20a:eab5:cf39 with SMTP id fs17-20020a17090af29100b0020aeab5cf39mr79473pjb.1.1667414830700; Wed, 02 Nov 2022 11:47:10 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:52 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-9-dmatlack@google.com> Subject: [PATCH v4 08/10] KVM: selftests: Provide error code as a KVM_ASM_SAFE() output From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org From: Sean Christopherson Provide the error code on a fault in KVM_ASM_SAFE(), e.g. to allow tests to assert that #PF generates the correct error code without needing to manually install a #PF handler. Use r10 as the scratch register for the error code, as it's already clobbered by the asm blob (loaded with the RIP of the to-be-executed instruction). Deliberately load the output "error_code" even in the non-faulting path so that error_code is always initialized with deterministic data (the aforementioned RIP), i.e to ensure a selftest won't end up with uninitialized consumption regardless of how KVM_ASM_SAFE() is used. Don't clear r10 in the non-faulting case and instead load error code with the RIP (see above). The error code is valid if and only if an exception occurs, and '0' isn't necessarily a better "invalid" value, e.g. '0' could result in false passes for a buggy test. Signed-off-by: Sean Christopherson Signed-off-by: David Matlack --- .../selftests/kvm/include/x86_64/processor.h | 39 +++++++++++++------ .../selftests/kvm/lib/x86_64/processor.c | 1 + .../selftests/kvm/x86_64/hyperv_features.c | 3 +- 3 files changed, 30 insertions(+), 13 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/processor.h b/tools/testing/selftests/kvm/include/x86_64/processor.h index 9efe80d52389..33b0f19e502c 100644 --- a/tools/testing/selftests/kvm/include/x86_64/processor.h +++ b/tools/testing/selftests/kvm/include/x86_64/processor.h @@ -780,6 +780,7 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, * * REGISTER OUTPUTS: * r9 = exception vector (non-zero) + * r10 = error code */ #define KVM_ASM_SAFE(insn) \ "mov $" __stringify(KVM_EXCEPTION_MAGIC) ", %%r9\n\t" \ @@ -788,29 +789,43 @@ void vm_install_exception_handler(struct kvm_vm *vm, int vector, "1: " insn "\n\t" \ "xor %%r9, %%r9\n\t" \ "2:\n\t" \ - "mov %%r9b, %[vector]\n\t" + "mov %%r9b, %[vector]\n\t" \ + "mov %%r10, %[error_code]\n\t" -#define KVM_ASM_SAFE_OUTPUTS(v) [vector] "=qm"(v) +#define KVM_ASM_SAFE_OUTPUTS(v, ec) [vector] "=qm"(v), [error_code] "=rm"(ec) #define KVM_ASM_SAFE_CLOBBERS "r9", "r10", "r11" -#define kvm_asm_safe(insn, inputs...) \ -({ \ - uint8_t vector; \ - \ - asm volatile(KVM_ASM_SAFE(insn) \ - : KVM_ASM_SAFE_OUTPUTS(vector) \ - : inputs \ - : KVM_ASM_SAFE_CLOBBERS); \ - vector; \ +#define kvm_asm_safe(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(insn, error_code, inputs...) \ +({ \ + uint8_t vector; \ + \ + asm volatile(KVM_ASM_SAFE(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; uint8_t vector; uint32_t a, d; asm volatile(KVM_ASM_SAFE("rdmsr") - : "=a"(a), "=d"(d), KVM_ASM_SAFE_OUTPUTS(vector) + : "=a"(a), "=d"(d), KVM_ASM_SAFE_OUTPUTS(vector, error_code) : "c"(msr) : KVM_ASM_SAFE_CLOBBERS); diff --git a/tools/testing/selftests/kvm/lib/x86_64/processor.c b/tools/testing/selftests/kvm/lib/x86_64/processor.c index 39c4409ef56a..fc6c724e0d24 100644 --- a/tools/testing/selftests/kvm/lib/x86_64/processor.c +++ b/tools/testing/selftests/kvm/lib/x86_64/processor.c @@ -1116,6 +1116,7 @@ static bool kvm_fixup_exception(struct ex_regs *regs) regs->rip = regs->r11; regs->r9 = regs->vector; + regs->r10 = regs->error_code; return true; } diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c index 05b32e550a80..2b6d455acf8a 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c @@ -18,6 +18,7 @@ static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address, vm_vaddr_t output_address, uint64_t *hv_status) { + uint64_t error_code; uint8_t vector; /* Note both the hypercall and the "asm safe" clobber r9-r11. */ @@ -25,7 +26,7 @@ static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address, KVM_ASM_SAFE("vmcall") : "=a" (*hv_status), "+c" (control), "+d" (input_address), - KVM_ASM_SAFE_OUTPUTS(vector) + KVM_ASM_SAFE_OUTPUTS(vector, error_code) : [output_address] "r"(output_address), "a" (-EFAULT) : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS); From patchwork Wed Nov 2 18:46:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028887 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 6F1FDC43217 for ; Wed, 2 Nov 2022 18:47:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231750AbiKBSrd (ORCPT ); Wed, 2 Nov 2022 14:47:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42504 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231696AbiKBSrS (ORCPT ); Wed, 2 Nov 2022 14:47:18 -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 43C5B2FFE6 for ; Wed, 2 Nov 2022 11:47:13 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-36810cfa61fso164448247b3.6 for ; Wed, 02 Nov 2022 11:47:13 -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=tgC3vVAXJf1HfFnPnIA3qYa9J9PvuIsGWvmyOnENyL8=; b=JdPVYbvGmrT6AIY9WsQkOGcffjK6ZQKiB6a7Ftdk9nfu2exh7TOl1eF1c61rq1a1Wv 2ZPcL3ecWhrhStfF1UCD08OJUh1mMaZGGrdY5UE1CF2Ainbb8xid0bOzU5EWqLV/Vkny F9lkfunRcJYMLYvdfmS17oKvKlfu6AAgV2pdi3p3fC1VWMVjw4E9hM+iqC2bcNtaEntE AZzlerQBqK3smN9LHqysu1Ma/VAYzLNCpFzbFAI8EAsB++6B9vNU4J+dhiijzJ6xK9u4 xZ/eWpnrQD/pd5q3df0hDHLJf1JyPBtnBnXI0FavLPHb4XoMinPV9xD8SDGM2fBLNzH+ lvzg== 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=tgC3vVAXJf1HfFnPnIA3qYa9J9PvuIsGWvmyOnENyL8=; b=ggQmunwnSC26MNF85VZzau40b0doCsFRA9CbtUzZv+lCFovMeMVfEq5XCIcuPwZn5p BQmVCQLvXnCI6HlCdHauNGXveKawzpHC/qWcnax4Ht71KB3YofMyUUGmuNRJaUv3XMMV lAzfNPSDcVHa+VBqOlWsuvy6VpnVfl5ONG+LgR0WNnqfYN4aSJv+XXlg3dLrhndWQe8e oXHpKvJjKW8jR+wqg2fUh+d3LoKpUrQDSzvyOdPFfBnaZT0b6Kz6U+JIObnr1ISbRwea +kIqYFSbE0hdgdUQeDXbxeiCKxKtE76Y1OuhCsAClu4PuoiLgLZZtnlVzFcB9oO5p2Jt rJdg== X-Gm-Message-State: ACrzQf19OZuoS6f+P5ny3JYrT9eX9sAWlsUgHC1d5A6DrQQluK/Q1RD7 7UjmJYlRoTfJdMP0niUrLRgBOH1VNXhhgg== X-Google-Smtp-Source: AMsMyM6Cadio/K70nPcxiUijaio/rnmWE5a7GQmQudQfr2KyPu4sSVnym27O7cmyzagmc3xS6gGC1sOlzECJyA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a25:8089:0:b0:6c9:b209:d922 with SMTP id n9-20020a258089000000b006c9b209d922mr25375362ybk.396.1667414832588; Wed, 02 Nov 2022 11:47:12 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:53 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-10-dmatlack@google.com> Subject: [PATCH v4 09/10] KVM: selftests: Expect #PF(RSVD) when TDP is disabled From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org 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 | 51 +++++++++++++++++-- 1 file changed, 46 insertions(+), 5 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 9d0e555ea630..ea1e7ae37e85 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,9 +21,28 @@ #define MEM_REGION_SLOT 10 #define MEM_REGION_SIZE PAGE_SIZE -static void guest_code(void) +static void guest_code(bool tdp_enabled) { - flds(MEM_REGION_GVA); + uint64_t error_code; + uint64_t vector; + + vector = kvm_asm_safe_ec(FLDS_MEM_EAX, error_code, "a"(MEM_REGION_GVA)); + + /* + * When TDP is enabled, the flds instruction will trigger an emulation + * failure, exit to userspace, and then the selftest skips the + * instruction. + * + * When TDP is disabled, no instruction emulation is required so flds + * should generate #PF(RSVD). + */ + if (tdp_enabled) { + GUEST_ASSERT(!vector); + } else { + GUEST_ASSERT_EQ(vector, PF_VECTOR); + GUEST_ASSERT(error_code & PFERR_RSVD_MASK); + } + GUEST_DONE(); } @@ -32,6 +51,7 @@ int main(int argc, char *argv[]) struct kvm_vcpu *vcpu; struct kvm_vm *vm; uint64_t gpa, pte; + struct ucall uc; uint64_t *hva; int rc; @@ -41,6 +61,10 @@ int main(int argc, char *argv[]) TEST_REQUIRE(kvm_has_cap(KVM_CAP_SMALLER_MAXPHYADDR)); vm = vm_create_with_one_vcpu(&vcpu, guest_code); + vcpu_args_set(vcpu, 1, kvm_is_tdp_enabled()); + + vm_init_descriptor_tables(vm); + vcpu_init_descriptor_tables(vcpu); vcpu_set_cpuid_maxphyaddr(vcpu, MAXPHYADDR); @@ -61,9 +85,26 @@ int main(int argc, char *argv[]) vm_set_page_table_entry(vm, vcpu, MEM_REGION_GVA, pte | (1ull << 36)); vcpu_run(vcpu); - handle_flds_emulation_failure_exit(vcpu); - vcpu_run(vcpu); - ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE); + + /* + * When TDP is enabled, KVM must emulate the flds instruction, which + * results in an emulation failure out to userspace since KVM does not + * know how to emulate flds. + */ + if (kvm_is_tdp_enabled()) { + handle_flds_emulation_failure_exit(vcpu); + vcpu_run(vcpu); + } + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unrecognized ucall: %lu\n", uc.cmd); + } kvm_vm_free(vm); From patchwork Wed Nov 2 18:46:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13028888 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 7281CC43219 for ; Wed, 2 Nov 2022 18:47:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231519AbiKBSre (ORCPT ); Wed, 2 Nov 2022 14:47:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42130 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231709AbiKBSrT (ORCPT ); Wed, 2 Nov 2022 14:47:19 -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 82F122FFF0 for ; Wed, 2 Nov 2022 11:47:14 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-3697bd55974so166607767b3.15 for ; Wed, 02 Nov 2022 11:47:14 -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=nQsYYcvMuiXeimF3iWphBO1Sb/hb6+CelzMx1eKIPSU=; b=pw9ymKA5S+Jx9QOYGRBTqhoQ3BHm/jqVqpUFdB6f3MXDLBe+NWv48150aokyYA/QY5 d9woU8yJZ2UvMNiVZdK8bSGccAhD9cpn/NKf6H9aBHC6ICTybIW8pLbMZ2j5Wz9AsqMg m6xZGYnwV5zX2AkwYawsNt+RTJ7ujLPNaFDCM2Amt0PVsjznpFYlc1c+s8QKUm1FWJEo r1QkLIR17F0t1ZnVuHMh4LW1oU4+yrXnZIgKlBs3NDw9Mw6mbQlWaiDTygj6FGZzAsXi zmV41q7J3DlwhzU8Quc5yNBxYBvV0MP5wYNHJFM98RGoYzY20ZJqdqn3EmdQ8c+LoT17 XrTA== 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=nQsYYcvMuiXeimF3iWphBO1Sb/hb6+CelzMx1eKIPSU=; b=wYBiSQfrCkP8d/7HwuBA6jiQdAGs3J0NwgHq0RNEFamFRmU6rwEFNCpB/FZnnmTxrp EO6AQz33qbDjILjUiMrJ2jETVi5cPJdOK/NBXXy4nf5E6L/EddfajyT4bXRCjfGBosv4 UhSQjRxEuSIaEwIzsFroKST/Q4MOwcIX69ECntG3UVZRQHLGPQGwRTdh9YdvkbKZkkkC uczHHa1JaRp6TqdbYAYnz4FpCGYXNb0orYFO3b9d6PF+1VJjc76cJYSKVvNtm7NwPBGS e0OEBWBTOYOU6UGNJqJBcXTlDRc4NJPrPFPAtnVrxaBE8NuXMK1ICOQ2XxCpKa3j5vE+ Dqjg== X-Gm-Message-State: ACrzQf3YHKEA0vCvQLNjXS1QbGL9/e2rU47lbVsRjKGgcysrdDdgxvde GcVTQBN577LPGyCfwyOInQfwqqmnNWJ5GA== X-Google-Smtp-Source: AMsMyM43IUGOPge1t3uHBUGFvgiuKeHmYSedOK6angXrFPIk+JUSfWbqcPJ+DZGNAH4FoO9M8EeZkCunUFxbrA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a0d:c5c4:0:b0:34a:7ab0:7b29 with SMTP id h187-20020a0dc5c4000000b0034a7ab07b29mr25379869ywd.294.1667414833843; Wed, 02 Nov 2022 11:47:13 -0700 (PDT) Date: Wed, 2 Nov 2022 11:46:54 -0700 In-Reply-To: <20221102184654.282799-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221102184654.282799-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221102184654.282799-11-dmatlack@google.com> Subject: [PATCH v4 10/10] KVM: selftests: Add a test for KVM_CAP_EXIT_ON_EMULATION_FAILURE From: David Matlack To: Paolo Bonzini Cc: Sean Christopherson , David Matlack , Peter Xu , Jim Mattson , Yang Zhong , Wei Wang , Colton Lewis , Vitaly Kuznetsov , Vipin Sharma , Aaron Lewis , kvm@vger.kernel.org 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 that KVM_CAP_EXIT_ON_EMULATION_FAILURE is exercised independent 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 | 45 +++++++++++++++++++ 3 files changed, 47 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 053e5d34cd03..bef984e4c39d 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -17,6 +17,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 ab133b731a2d..11a6104e6547 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..37c61f712fd5 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/exit_on_emulation_failure_test.c @@ -0,0 +1,45 @@ +// 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); + GUEST_DONE(); +} + +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); + handle_flds_emulation_failure_exit(vcpu); + vcpu_run(vcpu); + ASSERT_EQ(get_ucall(vcpu, NULL), UCALL_DONE); + + kvm_vm_free(vm); + return 0; +}