From patchwork Mon Oct 31 18:00:36 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026191 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 F162CECAAA1 for ; Mon, 31 Oct 2022 18:00:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229674AbiJaSAz (ORCPT ); Mon, 31 Oct 2022 14:00:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229651AbiJaSAx (ORCPT ); Mon, 31 Oct 2022 14:00:53 -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 4D48213D1E for ; Mon, 31 Oct 2022 11:00:52 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id v1-20020aa78081000000b005636d8a1947so6021407pff.0 for ; Mon, 31 Oct 2022 11:00:52 -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=idUT8pzYHP+DmrkVsbwLYVLl4f8i7IupWtWnvPGzFF17KI5p12NgqyS5EPdFOcRVaz 0zIsNA7z6z5/t8coxL2CtAN0lkQSPtzCcrd1wnDNnx/VtCIrm2RTZ2fEVNxGcHVYtgkq R0boZ4AeTDw9SJDTLdMUpSR9azMNXeh4qhI6HUgF1HVr8agpql0G2yyKvkg0UpB3W8mQ CeCuB0B1o7odvQrSgAik90cEMxh2dK5ToA+IMWmBFzQ7QeCkMzS1C1Q0s2bFz+5utJdV CvVxIHRzkljWyQLirwC6gF1+mvdFELkmwintV6pbnE2jNwZOmad0GKVNVvSR31LosC6p tMgA== 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=fUNqPvAfue8FOlPokXvRHRX3oZqEdwAlYPJFisDgDrIvUaP6gGt/rok8iN3BrD5Tkb JS9zf9z3+cljIMvTpTO1I4pX6aUZeUNmv9D2PcjpnEONa6iYhq/H8Sceps5GkVGq+JRA saQyPA3SQP5Sx7JPTb0JL5ldfFMHzafd4Au4GAK4UJ5cCQbvbsQdD4Ondo3wCCXA1Hax ULLJYr199kRD6XzI5m+a60AH7JyaJEx/Y2oel6jNz6tJ55d8BRLvKediB5W9UWy1ewV7 RMORI4zuPH+kc3OEqTMsBxM9eDqVDONPc1T5w9/r1t4T5CE7GtxDpb0dj49K3gsxjkgl EzwA== X-Gm-Message-State: ACrzQf2+Gq4Hqai25p11Wn9cm9aaAHZTn6gIBlyOO/6nOhGjQHqfIkKL uyF3jVIbh6MEVJ1+NjD6oTJo+zsSM5KaEg== X-Google-Smtp-Source: AMsMyM5aAtth/wnG4FK4mgKAZVLU69CFPfghmYyZwBHpD7hl5uS3bpX3vKZDdxd/fjKoE/ciXf9lJ1zG2adASQ== 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:158d:b0:56d:59f0:d273 with SMTP id u13-20020a056a00158d00b0056d59f0d273mr8294114pfk.51.1667239251867; Mon, 31 Oct 2022 11:00:51 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:36 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-2-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:37 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026192 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 01320FA3743 for ; Mon, 31 Oct 2022 18:00:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229689AbiJaSA4 (ORCPT ); Mon, 31 Oct 2022 14:00:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229670AbiJaSAz (ORCPT ); Mon, 31 Oct 2022 14:00:55 -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 176AA6423 for ; Mon, 31 Oct 2022 11:00:54 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-36fde8f2cdcso108927777b3.23 for ; Mon, 31 Oct 2022 11:00:54 -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=nk9ehFCSXwebc8wyDFZzqt7A9AUz20ttEVed23o/Jss=; b=hJbpzc+iWNGRBwfvga/XtSVIX+x6JbJbW3hSlyp7HLIqGS5U9JlQtm1/5BJg2578uM fODokw84BQMmBzn/5e9V0ZlN/RQikv4tWb53v7r7z4nDQkUAOeMFpX5hEGuIOSB04HFA UeL9ow7RN+StR1j1IFdnxaPmOMQ0pKvaLbPu/AqucTcRANNww6vyPvknj6bb3zQv5hUZ 6554gyA1RHCNfz9byuhkVOEconB2C/eTO/s6DEOmmGipzolhedu5Bx5IaB+Zh6YQwy2y wPCBqcUKDalsarZWxJpvOmLl/Pdr1/B58exM0VnJzI5Wr97td28vN+k+7Li/yW7S80xR WNtw== 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=nk9ehFCSXwebc8wyDFZzqt7A9AUz20ttEVed23o/Jss=; b=t1AX2VMWtja1qMwsx6Ca8FngVUeIGkce8yQA6KdKyRXp66mNNeO267jI8P+jzS11ht j+tg/mRDtDywLz0sW0wwd/mhxGOXe02ehL9BZCEVyh/9WEfLFOIrt465ZNOWGwVZnnSX c0Pxk8iOZ1VAEa7I993WtcxxKp9CPChxSsaDL+C0k24qz+unpICx6YYcW8nEgHpuCFGW D/aBdkA2rW/QQQrpCaT/71RhXNG22F4PxZkWK/YGfeBJOwGBh8CjTtQ9kYkFqMilkWdQ SvrTXZCvCo4RgfOM4uthApY9ntivpZ9PJR04vBweFbkMHluimKYMMnJHuJbCj8QfxXCP xMIg== X-Gm-Message-State: ACrzQf2Xl4eMBb4bumVngSqugOqHucP/3bVF8puVHrQLtAIkC4s+oF6V fXr/Vw4i7CtdFOfHNBL1AYtUBU4EGVahGw== X-Google-Smtp-Source: AMsMyM5/JH2BDamwirs6YxnvBgOYebPFt83xvI90NhT0MSrgd0W9mWRd0QhDJndyGVOLa59/H5nM1AgHKq16Vw== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a0d:c986:0:b0:325:1b81:9f77 with SMTP id l128-20020a0dc986000000b003251b819f77mr13613230ywd.182.1667239253406; Mon, 31 Oct 2022 11:00:53 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:37 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-3-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026193 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 3F115ECAAA1 for ; Mon, 31 Oct 2022 18:00:59 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229707AbiJaSA6 (ORCPT ); Mon, 31 Oct 2022 14:00:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229677AbiJaSA4 (ORCPT ); Mon, 31 Oct 2022 14:00:56 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 654EA13D2B for ; Mon, 31 Oct 2022 11:00:55 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id q1-20020a17090aa00100b002139a592adbso5750541pjp.1 for ; Mon, 31 Oct 2022 11:00:55 -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=mssMmFyCgRGMuxB7n0IJhZMiRilQUWU+GcvZo19Dlsk=; b=Xa7Zm/XmddVMtPHWm7IbpVr2li6rD76IsuEYJnmjZzrTlKJyECkf7p5oiPhrQHDfe4 Vewwc7Ur9BvHQYzLCFAqSTyRYFUMTMVrZv6MG/1MkdOZpn8IrNRyvrEnv+OxymMaODIx I83yuOl66qphxaA2wsMDuxrFuUjp7H2+jhGjIFQUWXZR0ptdYzFHeeP+WYKBOi4hwq/s bfacIX+5Y8UwwRrMzpukbZCO/858j/H41xqurA1UxraYxMnqeB+MfhI0qggh/ZccYCFm 5uUjVXAN1gXbxOuqhv8GEtUPwOIlY3bCrBBu8p3yWb858oOuHnW6U+R1mSYhn6wMYeb0 wWYA== 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=mssMmFyCgRGMuxB7n0IJhZMiRilQUWU+GcvZo19Dlsk=; b=fKoOxi2EQMj+hUU7SRJiZ8YeRlOhOOmwpBxFZcioXn+T/BZ6nBC/n8y/SuZXXpNB2E DBNwjcSOr3vfExA/shnaIOgSnC57Cj2v+tOh2r1V3fykTz9IxXd8/ZDEUAm3ozWTowDb jGgAfeokY8ubfjmYEkorWch0+mDGTbwCZSEKtrR1k5m/jyQM1Hu3oxHEbAUqNrMl3fYi 6M9XDtBxbFqFx5dhzwLhJItTIoqEP1pJ148+Sv7OD318FxHNVOB/7iCBem5Ex/DRAba+ GYupahGkYYfE+1TEfGgWqCy5cW2mIyBMKdcxk0v/pRktAkfaAkZUU/GI3u78mOmK8Scs Yu/g== X-Gm-Message-State: ACrzQf1dar68GigzHG1FBI82WJaDVG6S/LUfMkVO7oOXNLFiqn9Udbho IE+8o5qIucjP4YkNCgS2LDDdBtM5kV+3aA== X-Google-Smtp-Source: AMsMyM55PpF6WO0Gk6qKIT+ZCLpaOdHW8ymhEja7s89zqIlSkBTYsMrQChHQNLayIpcn1iO2QYzxOdJmXV1kaA== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:aa7:9421:0:b0:56b:b2a8:6822 with SMTP id y1-20020aa79421000000b0056bb2a86822mr15382772pfo.86.1667239255001; Mon, 31 Oct 2022 11:00:55 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:38 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-4-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026194 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 2C510FA3741 for ; Mon, 31 Oct 2022 18:01:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229906AbiJaSBE (ORCPT ); Mon, 31 Oct 2022 14:01:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229757AbiJaSA7 (ORCPT ); Mon, 31 Oct 2022 14:00:59 -0400 Received: from mail-pg1-x54a.google.com (mail-pg1-x54a.google.com [IPv6:2607:f8b0:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A3EE13D1E for ; Mon, 31 Oct 2022 11:00:57 -0700 (PDT) Received: by mail-pg1-x54a.google.com with SMTP id g66-20020a636b45000000b0043a256d3639so6453517pgc.12 for ; Mon, 31 Oct 2022 11:00:57 -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=0Sl8NAAmtS1iDuznP+uILgoMcDYJe1O4bErpznQAKrA=; b=HLFXV3VA8hOVzxdoYnhqyjLJNR/1pZyXecUZBP7ad66r/mtPSonBCxm144jNt7IS8v NWJHHxVcYwLp60LXRB5ED+KtdZmTgXykOzZFbSKQrhPK/YDULe2rw5ZVrMen2Za5+vH9 21Et3DrUWVhIU3jpKAE7XjAFtdob5gRqXASPs8aK2oboWahWpXFjPtQHdbOAybpp/Fuy SGDMkbWg/GPtjLYgO/W0TRrvQD3zAWOAIOGy/ZXyBQB5qlhQTiAhQMXnR3dABcpN38Ga 1YGDJHi/r3YPQb3Lb5iQugDkx3g5zhXYFNdoxVRHH6Dav6Fbzj2mdlas56i321sa0u/8 Czcg== 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=0Sl8NAAmtS1iDuznP+uILgoMcDYJe1O4bErpznQAKrA=; b=J8Moe0uLkCCZAJq4kWJ8prMi11VRvxGcPALECi1sqIf/oWS6Q+D36BnYowD8O04ra2 xmDKvaXusBt7/VzGqo+RwMWE0hx1qjaZsJc9i+vj+NxN5N+mhxZWn0UCxE5SeyPTCKsN i3mFu3N+S9AI/zyF25blx5sRP4wLtTFabnX0dhKrciutN+vFVfW0FL3d3yJKQEjP2Mt+ OPNYBwJZ2+1pEhPrO8WYunvMXxoJEYe74uoBbPBrzJ03VZ0tP9RTm88RitCZw39+jBCT tg007hMCltjFse7f2/j5diGc8GA0W1AVN9bATceF1c1T4DO4L5l/0yh/qkUtB8fW+xd8 yhDw== X-Gm-Message-State: ACrzQf3e/dMym/EsJkNii7IGQoT4RRxXEutTtM2jIhV7G3lb8VpLodgd M3qh1BOBA3GngR/9AjhNf7QK83G9PUzByA== X-Google-Smtp-Source: AMsMyM72ZWvx6Xoj20XoMosuR1N2udg1bnz6hmIxa1C8iZSzdaTpjpYUns6mKVCceGQXikNMMdWhZrt1/TBsRw== 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-20020a17090af29100b0020aeab5cf39mr1263pjb.1.1667239256717; Mon, 31 Oct 2022 11:00:56 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:39 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-5-dmatlack@google.com> Subject: [PATCH v3 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 | 59 +++++++++++++++++++ .../smaller_maxphyaddr_emulation_test.c | 45 ++------------ 2 files changed, 64 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..be0b4e0dd722 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/flds_emulation.h @@ -0,0 +1,59 @@ +/* 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 assert_exit_for_flds_emulation_failure(struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + 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]); +} + +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 f9fdf365dff7..f438a98e8bb7 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,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_EQ(get_ucall(vcpu, NULL), UCALL_DONE); From patchwork Mon Oct 31 18:00:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026195 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 B0A5BECAAA1 for ; Mon, 31 Oct 2022 18:01:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229915AbiJaSBG (ORCPT ); Mon, 31 Oct 2022 14:01:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39708 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229782AbiJaSBB (ORCPT ); Mon, 31 Oct 2022 14:01:01 -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 0C29213D6A for ; Mon, 31 Oct 2022 11:00:59 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 204-20020a2510d5000000b006be7970889cso11046892ybq.21 for ; Mon, 31 Oct 2022 11:00:59 -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=QsGsn2644Bp5TjLD8vDYbI7iPVlGktd20e/ImVP6YpUYAD6GrGDr0W0vYX4aitUEfp lsmQTSa59i5E5w2vT28Qn+F8+PruqdVPa3mLgDvP0XEublpTu7Yx3Y65mC5EQahFUvhV RumQqIaYBcDF3gf1OZ1fZQOO8yyuJ5XGsUJ7rqDLusrvRNeSXNUW+pwtJxOVItDWZail Vv8VzpCo93yKZ6yrC89Lh5bzS9BIh2+qHneRJkIFsWbdpzgcxdzx5g363UqQUADOPxRz zKwO4Zj9g7kenE/wnUYq0i8ua/hgLAXMBhuIrNJ1dnMg495SoIv/1ii9OQNmjMpmo2PN bvRA== 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=jaClFOcVE66nXuldU/6RQ0vYrpZb/EOhLqNAsXhuCA+fhLdURGpEo3gVkZJfBc+1FR GazbAHUJ4jDgc3MZ8MGZOfRL2WJ39vJIaDVzyixc0Y67Dx22mpri83EbE9Hm2Ni4A1bS SYK9KmD8O2MuFmCZ635etYpLcqZld5yvzai+utr1LAxqdFYzAXZK8XgQ/A3Q9bLlcizd TfLM4abJ2OAAGwsAJXXY9sk14vfBPwOUMghNYNseeW58ST+PoCRnTFjB9cdIfdwrGZWN mfN5x3P6znPqG2nlgkOtLXgFRxDedFvLxQVoGfDCPI5px35jAU8psFwtErQ65mCEDoEH cbKA== X-Gm-Message-State: ACrzQf0v4tUqJq1c+m/VR67ugkNrr29O7B5UdepdDEw8v1Q0gxPKP8P3 FeZAlAtUeNxWhrK4S5mnL/MMLKtKSX4zrg== X-Google-Smtp-Source: AMsMyM65v90lchpLHHoOfhHMNUr/rETHiYez1WVC2eisRS+eVtshyGyjiSpooY8nzoEvtUA5lxdiFlz9Djdc4g== 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:1367:b0:6ca:517b:700b with SMTP id bt7-20020a056902136700b006ca517b700bmr4ybb.49.1667239258715; Mon, 31 Oct 2022 11:00:58 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:40 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-6-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026196 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 513A0FA3743 for ; Mon, 31 Oct 2022 18:01:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229889AbiJaSBJ (ORCPT ); Mon, 31 Oct 2022 14:01:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39784 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229779AbiJaSBC (ORCPT ); Mon, 31 Oct 2022 14:01:02 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 79CE713D65 for ; Mon, 31 Oct 2022 11:01:01 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id r10-20020a17090a1bca00b002137a500398so5504567pjr.5 for ; Mon, 31 Oct 2022 11:01: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=aUUVUYLJgnj5nzWDJA+dYUJX2dFbOs68FlBZpKyOozw=; b=P2J16cF793ifwqTCC0EdUFrQ2FRNlTxY/nI8PZx42sfdUlN54Srk6PXTk56bmnrgRw gmz/g5yN6a5QpSh540yg10dV+OGaxFAyvULKwkl2R1bUnTz22Jq+fU+n1pA+OFopfyrd XpP5pOj2ROnVWFSAf2+isXaaQzf9C4+MR4cnONE3Ks682/KmC5vihD3NolJAmuoAyMWl EufmZMsgrAEWq1jy9g+vxTF99uHH54A4hBNMum+Z8auCdoQu8TeMHMREUggvQe31VejH 75jvcWcIFECgrdr8iYhv4b+GW3a5uf7AG061GiKFx/jhNDdKkXOd/88kQ1amJNjCNpT+ UcWw== 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=aUUVUYLJgnj5nzWDJA+dYUJX2dFbOs68FlBZpKyOozw=; b=J/FXBH8L3eKaQyVzpWiRRx5VBH6YR9uqBzIHf01BuwCcIFNCwSocmfHWBMXwSBE3Lc TnkkCjoMXF02WMp5bGtUgvkeB8T3ATqUd6N+UlzNvQDJMwuNOPYolDncaa8c1x1y7jfG oZEAfjklKHqKblq26xgqRcZZ6o23ODVuRTBxNJwCAl99hnwuEiB9oNpn8jhtg63A4GiY 7SOYTyh6xAKyKpTT4yNjnIeSNvw/ZDRToqjI8T61aq+pAw4dJfnqyW6eQR0gtUBZrP7l dnwxIEVnFK6H6FVYM80C5iGaFwgpRMkecrUbx/MvktRENyakpXBl/WRAECB83IFyLNOS daJQ== X-Gm-Message-State: ACrzQf2MJl7yKACllj8vsq23DwJoho0CZW+HRxxkR+te0InaXXFeicL1 wy6NQkYj4jnm8N4iWp48+d9AJ75JGFG0Kg== X-Google-Smtp-Source: AMsMyM7fLiVjT+1I9jOFEUqauPgMViKbSr71pSRXC3LOApPNFLOlAdUynqW6YBqzwvkfyBQNXeqApDxohtSUrg== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:aa7:8c44:0:b0:56c:f21f:5e0e with SMTP id e4-20020aa78c44000000b0056cf21f5e0emr15607974pfd.35.1667239261063; Mon, 31 Oct 2022 11:01:01 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:41 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-7-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026197 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 21F20FA3741 for ; Mon, 31 Oct 2022 18:01:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229955AbiJaSBK (ORCPT ); Mon, 31 Oct 2022 14:01:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39802 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229898AbiJaSBE (ORCPT ); Mon, 31 Oct 2022 14:01:04 -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 062E413D3C for ; Mon, 31 Oct 2022 11:01:03 -0700 (PDT) Received: by mail-pf1-x449.google.com with SMTP id j22-20020aa79296000000b0056d3180c800so2366567pfa.3 for ; Mon, 31 Oct 2022 11:01: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=EumOl5mGgVnCEzDA+UueSGzDQoNJ9RAkypwsXwSdasQ=; b=nmA84ddMcpRP2hFYDqoMp2XhVwDw0uGolYYYJo8yoKEeGUTwIcNkDhfeLAmWW/F6oW oLUUPUeRENd3+vUw22CvKmI9o0MEP8zAPsZ9QmuQI/4YEt80KgXrdz3YIf7Ddf6W2jEY N1DeK1POtHRaWUTOr1DDvz/yuL3vV0eDKCq8H2EDK0YUrxqU4ixGybWBzA7Fxjxrk8Pf 6i/07Qj30aMH2VcbHYR7Jur2RoVXb+h3iRUVtmpoZ7cOYv8KJ1nOnqga9ljYe/K4YiFl I4AbXKP7rcb3GkQOG9/rZzatx9bkzvgIQmzRSzIO2kg+HfwQmAkbf2tMBLeWNHPuwuj5 FoNA== 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=GFRGTej6J4/wk7quB47Q+0eVykAZ+WIxUbhl6lCmKV2BhUgVu5yjf7yBFxTPhoteyj CDdP1jvph9UY5ySQ+luC0n1hHYwaIBRaUDFtg8/2x38bQs1lGNtAhx4jUmTazmEI6zSb eNXAfUq9VSgkraDTMDGuoYd2KMqkSJWOJf3G30xDCaVu//KTMdaZfbl8QFEFWt1zifeZ nNI8mSEO9ujEPA8hKdGAKmBng64D52Vv+nZXZBuRRST/9BIhXRHqm+jUvleNULgmLauQ oc7ARsypHrualIhVk3ut9EACk6/QmIhYP4uX7goPmTWyzZzeqnGoEZ/s4c3I180T1IHr /4ww== X-Gm-Message-State: ACrzQf2DkjCN2tK3L0PGv93ye9/1pu4DyJ2ZjSlSPkeIr3mOSQT1o1a/ lWx1zT7XXPKXEYzEWQUzarJNnW543wi0MA== X-Google-Smtp-Source: AMsMyM5efca/PyxNmqTIb6Ud/6wk5zBYuG9TABpZz5Sadm8w5mD6vosLbMuigQHzECNtMr3fA/F1iLGoEYPiYA== 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:c691:b0:212:fd5f:1ab8 with SMTP id n17-20020a17090ac69100b00212fd5f1ab8mr32667755pjt.11.1667239262566; Mon, 31 Oct 2022 11:01:02 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:42 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-8-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026198 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 A04F9ECAAA1 for ; Mon, 31 Oct 2022 18:01:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230007AbiJaSBN (ORCPT ); Mon, 31 Oct 2022 14:01:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229935AbiJaSBH (ORCPT ); Mon, 31 Oct 2022 14:01:07 -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 C4E6E13D6F for ; Mon, 31 Oct 2022 11:01:05 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 204-20020a250fd5000000b006ccc0e91098so1677984ybp.13 for ; Mon, 31 Oct 2022 11:01: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=KMoIap0sR+1CcUsNo9fMIcDCtwhfe98yh5fTCarrD0c=; b=VLj3zRJLo2Nc3pt/6PLTuz73FGjUXZHI+ss8K4kGwcdY9WYhL/h3+bd7wVTujqbsxK Njc0xZihrFpNQECfQyYwVmTV74B8Hgpb/tbbqO2iR1d7GakKXdBa9T+fMSjpRWNItcBh YcIILblPGHdbp8SEL6gItvb+2mfhZGvRbm8VU7DHSAet99kBSSZ3WBYggp6btxpHIawf mBaXaiwBJNBhjDNmCO9GSKBLLhbjU7vJKmpm6NZ4yKMEB0s52TsbHEi+VIdY8JTB0rcS zJBKaOywg57weYiiRYoo9al80OZSmMbT6G+NJ4NhmktEwyQP3a1M3wMQ4dq4DFRQOO9B F7mA== 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=4gKOWWlvkaL1g2eM/4xBnlX+9P2P3BFe1sn29uKV1WaZ+eh3c2rVkDxWokXpI1k0Ei WS9xszipimrTKX2FyRXIaRW/+3nADPg9dZU0LicIvIOLDVRE+atSvDFVSspFhjKryCeH c0PDTYplDWJkFuGyz7JSi0iqpURFCRhTEzcnrcPPDvcSz15xsoeqvBOZEtPvsSnqym1k SuqImSSNyXzqSNzLGTBN9ZNRk5UiZttGTR75J3LZe5J8F2IJfj5zK5R2FqpBiccCPBVd 2f/mYidhN/e3kD+zloYA+33O5rfboy2+mtbLkT4xUJeMDNWxcICaAG3X5Z8JEtCz1o3/ XcDA== X-Gm-Message-State: ACrzQf1CgCLe+Y3sQiA5bkiPBIL0wtgd7E8JplBGvAUmEUCJsc4kgk2I K++DuwKBk3EpmmI5GQJi+PzTGI2i8KtfhQ== X-Google-Smtp-Source: AMsMyM4U9Rbs0fvhBZ53r4dAC1ISz0ocqdMAMluyOod/5k8dcgP8aQKc2vlbCtT9g8F7baxM7AkAlokX+RUnEA== 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:13c7:b0:695:84d9:c5da with SMTP id y7-20020a05690213c700b0069584d9c5damr13953475ybu.650.1667239264359; Mon, 31 Oct 2022 11:01:04 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:43 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-9-dmatlack@google.com> Subject: [PATCH v3 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 Mon Oct 31 18:00:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Matlack X-Patchwork-Id: 13026199 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 19941FA3741 for ; Mon, 31 Oct 2022 18:01:16 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229890AbiJaSBP (ORCPT ); Mon, 31 Oct 2022 14:01:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39804 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229787AbiJaSBK (ORCPT ); Mon, 31 Oct 2022 14:01:10 -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 EF61513DCE for ; Mon, 31 Oct 2022 11:01:06 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-360a7ff46c3so108769047b3.12 for ; Mon, 31 Oct 2022 11:01: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=fUax8zk38TfX3ZHfYoXO0Z/gTphayQ7H+KL0pW9fwP0=; b=N5RvQXoh/pqNeTNg1QX1HAJwERc2uRU+S5Xfy9zqMQl67B9frO4nT2TNDJ199hCPK9 aDypUp1jwAF0y94w4Yjimn7Ho9uWCb4i1Xa2qHtBH/JSLVa4+w0MESZWpN0KF2x0vaD9 L03LdwOFr3/Jdmrp1hhoC+ukkVuLqA1GOWnxU6Yh6fCxHOh12aGQGs0gxsZfuNS3qnyi brNtgpWmt6NC8mTflVWKMgqIbGohr+CuUBquFD6NHbA3cuWxoC7FxXtxRiMpX2ckfPjI Wt6sWYeO4CvMHGQmwpbwdGdnslXJFVAYdCg7RYHVjJwK2xjBVXa14n+nHd5y3uyPZ7PR glKg== 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=fUax8zk38TfX3ZHfYoXO0Z/gTphayQ7H+KL0pW9fwP0=; b=uoRSZDz7/snnFF7LoNsE+y0mqEbuttPlav76RO3+vKUbzrldSOoH5nqQSjKQQprmrH Bz+7gmJXFWP/bHLB6nambxS+RNwe4+ETtHcYoROGY0pJ6v9cQos8363v1G4tOWdA/RWW fiYMs7cxHbXpwElvLj6/SSLOoP7ARN5E/GXJ3LBiJMg87YW7337aHQMm4ZSoWCvFU1WY DHn16+aGQ+JNkfQs61m/7AHLN9ssL1V8LnGtDKnjQoxe8q9HtLv661wgpV0wQ7OFux1w 7BPQ88audUIch3hyVJODFWwCGxZqPIrD/91xo2djflgfJVXD5ysiC83BE2j+yUnHcsMm d4yA== X-Gm-Message-State: ACrzQf3pcWCCglvbK6p5jJIdlnlOso0uALKBXKCDqTbvSj+W0Hx5V/7I Ay8tZrDJRVL363Gq/uTWbZbaMZMMUKUtAQ== X-Google-Smtp-Source: AMsMyM4uNgM42PUGuS223PfGwHn0V4BfrgzpVwXoNHoT6zI0YA0etvt6X8WnnEDDZiK6Sp04SRrxHftHVn0Amw== X-Received: from dmatlack-n2d-128.c.googlers.com ([fda3:e722:ac3:cc00:20:ed76:c0a8:1309]) (user=dmatlack job=sendgmr) by 2002:a5b:10c:0:b0:6be:28ee:2b86 with SMTP id 12-20020a5b010c000000b006be28ee2b86mr12975643ybx.582.1667239266012; Mon, 31 Oct 2022 11:01:06 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:44 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-10-dmatlack@google.com> Subject: [PATCH v3 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 | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 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 f438a98e8bb7..5934ddc94aa0 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,22 @@ #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 disabled, no instruction emulation is required so flds + * should generate #PF(RSVD). + */ + if (!tdp_enabled) { + GUEST_ASSERT_EQ(vector, PF_VECTOR); + GUEST_ASSERT(error_code & PFERR_RSVD_MASK); + } + GUEST_DONE(); } @@ -32,6 +45,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 +55,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,10 +79,27 @@ 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_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()) { + assert_exit_for_flds_emulation_failure(vcpu); + skip_flds_instruction(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 Mon Oct 31 18:00: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: 13026200 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 E367BFA3741 for ; Mon, 31 Oct 2022 18:01:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229956AbiJaSBS (ORCPT ); Mon, 31 Oct 2022 14:01:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229992AbiJaSBM (ORCPT ); Mon, 31 Oct 2022 14:01:12 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32A7B13DE9 for ; Mon, 31 Oct 2022 11:01:08 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id bw25-20020a056a00409900b0056bdd4f8818so6051527pfb.15 for ; Mon, 31 Oct 2022 11:01:08 -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=9T+DYSzhKuAW5tnRCXGXrMAstKaJApTg9bZvi/cvnBo=; b=qsaIxiFDsOglF9Hk0ta90m/QM33zJVwn00XeSRyjkYXfoK4BiFr1wNS2W71+i9zNyX K69xwONicSjSgYLVFHgK3rbEBZ9FFFp+VzQaTiG1pRH83Vm+qMRxVRWyzvdFGlD8WNMN iZyUVbqH/MOT102y9s+3qF5L8lqTLI+Y1NFeUlGACP7rDR3hC0RnvEuRkO79XwWkL5lZ 73K8sXnc1Dvra6CBtl0l7wFbN/J97R/x3adb+a9dWeILklhCPUDAaSmVQUnbf7qoxNmC sf63iLUkvvyug6UfdOvoZ0t8mHIX/axK4nO3IEKZARHyZzxcXd5cDBV4MBI0/11BhfOj dghQ== 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=9T+DYSzhKuAW5tnRCXGXrMAstKaJApTg9bZvi/cvnBo=; b=Uc5bSgVmBzGyEVyj8rMILPUibRZ5NLL0zcZpAkWaU4ozVIR3/ZLWtvc24J/GL1dsE5 MCF1P7y8JInzNuxGDgk5iJYg6AWSF0mfqZRiDDKsVehoVzGAe2nc5MYKfjBL9FIPZtwq u/vmDTHMRs7G/0hyksmgGyCHCFImJ8WCqOhnro9UgZifE4eMJoTbp43cy7VeiOTDbU7u BwBvTf9vVDepAdJxi9o2OHa6I6p7Ya7nmzahavi9oYI/a3WG7VhREJK+xMnw6mFDmtyx x3uN5OivJcKADblWdnaqh4OD5IiJdOGDtxXVVKxvDT5HjBvMC3x2NgEd9P6c7ftoJRsq U7XQ== X-Gm-Message-State: ACrzQf3UxCTdGTW/ic6R8+AETjmA3Wwo0yrh54QvW2DvC+XQv+dGAmmz cBsXTsRZjmWNr5p8IXirrS7Iw42MKuUBpg== X-Google-Smtp-Source: AMsMyM4UJcYmvu9wjnwgT6xsXHzm5az60L7+I+nacdJ2K9l02XSJW7+dcdSOvbcJ/p9rm9luneI2x61SDD0W9A== 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:1081:b0:20a:fee1:8f69 with SMTP id gj1-20020a17090b108100b0020afee18f69mr682052pjb.0.1667239267351; Mon, 31 Oct 2022 11:01:07 -0700 (PDT) Date: Mon, 31 Oct 2022 11:00:45 -0700 In-Reply-To: <20221031180045.3581757-1-dmatlack@google.com> Mime-Version: 1.0 References: <20221031180045.3581757-1-dmatlack@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221031180045.3581757-11-dmatlack@google.com> Subject: [PATCH v3 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 we exercise KVM_CAP_EXIT_ON_EMULATION_FAILURE 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 | 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 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..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; +}