From patchwork Sat Nov 5 04:56:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 13032828 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 9D62DC433FE for ; Sat, 5 Nov 2022 04:57:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229542AbiKEE5P (ORCPT ); Sat, 5 Nov 2022 00:57:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47016 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229582AbiKEE5M (ORCPT ); Sat, 5 Nov 2022 00:57:12 -0400 Received: from mail-pl1-x64a.google.com (mail-pl1-x64a.google.com [IPv6:2607:f8b0:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F16EA31EF3 for ; Fri, 4 Nov 2022 21:57:11 -0700 (PDT) Received: by mail-pl1-x64a.google.com with SMTP id o7-20020a170902d4c700b001868cdac9adso4933656plg.13 for ; Fri, 04 Nov 2022 21:57: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=BXjcYNaRFsRiTOYj/VSeDrYfnk14qW8dp0T0QhmC01U=; b=VEy7ANYLdY2iq6bTxxUDZH6+XSU2Nro3Rt2zHGVMA4QIhELj/IrEYMVaEzZGGn3ZuK /aVN+XLpm2LmvYFvB2Gd10hGP/dFIqYq2yj+yM9Gndo6RtIm08NiZCTylamQCEAUGEos KHPhMs/vHmbr50DuZRCNLnCw0oCUd7D4i0lsFgypH9fASd2wgsXDKOa3p1Ze7zQ7lI05 G5o9g23jHCJmOenZVwLG2uLqyO8mKbn1UZW6eXIb+/c0Y7bpFnNrqHaliPBbJL7u8fBY LF44uC5ML5UeFa6fZChqUid+ocL/63nXT436SgeFZcuIMiYYwh4bRNYoRzNT3A/7jpGO DLGA== 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=BXjcYNaRFsRiTOYj/VSeDrYfnk14qW8dp0T0QhmC01U=; b=U6ZkmLsNFkux5m9UWglHCzTPR3qmZ0NR7pvMYCeIlDbAmBUKB6IgbI40oyRjdR9ICG lE1rVHx/pMV5zbnFAkv2H74sdAay50iBYBDhoK6TPZQmwuIEfib08Yy/qp8CxOoCVVOY w1NCcs7AkdOngiJqrmpBbwqzOzOfelWzoaX0lKL+/y918ly1dq4k1d8KI2ruaR1MeYOe 7reVXPDRXxsAuQRQfpdDIHl1RgsYNEZtgWeOExnJrNiQLSZSyhcAUpNrNIPwmzJ+x6lg K8li49hizG1LK5sRZ4Ubst6Z1tAHv2nBGtejt6hYaP1vVjD+j3ozp/zlJHxMwqhWJfSO bs2w== X-Gm-Message-State: ACrzQf2mZJ9XEyjBMJL6V+piv/VVZd4vi04D6l+TVRXkRJq9ouguWXMJ FG1jQrZJUKUShws8lJYSLfzP1GH+bFxb X-Google-Smtp-Source: AMsMyM74KD9sv5Y/7eP8tBJe72q8a1/ftv2vvR5j5K3J2QbUWaHpU3xhQYXxycxvJJyfgjd96owmfq9yHGCO X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a17:902:e883:b0:186:99e3:c079 with SMTP id w3-20020a170902e88300b0018699e3c079mr433741plg.149.1667624231359; Fri, 04 Nov 2022 21:57:11 -0700 (PDT) Date: Fri, 4 Nov 2022 21:56:59 -0700 In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221105045704.2315186-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221105045704.2315186-2-vipinsh@google.com> Subject: [PATCH 1/6] KVM: x86: hyper-v: Use common code for hypercall userspace exit From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Remove duplicate code to exit to userspace for hyper-v hypercalls and use a common place to exit. Signed-off-by: Vipin Sharma Suggested-by: Sean Christopherson --- arch/x86/kvm/hyperv.c | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index 0adf4a437e85..db2f96a0ae23 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -2270,14 +2270,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) ret = HV_STATUS_INVALID_HYPERCALL_INPUT; break; } - vcpu->run->exit_reason = KVM_EXIT_HYPERV; - vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; - vcpu->run->hyperv.u.hcall.input = hc.param; - vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; - vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; - vcpu->arch.complete_userspace_io = - kvm_hv_hypercall_complete_userspace; - return 0; + goto hypercall_userspace_exit; case HVCALL_FLUSH_VIRTUAL_ADDRESS_LIST: if (unlikely(hc.var_cnt)) { ret = HV_STATUS_INVALID_HYPERCALL_INPUT; @@ -2336,14 +2329,7 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) ret = HV_STATUS_OPERATION_DENIED; break; } - vcpu->run->exit_reason = KVM_EXIT_HYPERV; - vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; - vcpu->run->hyperv.u.hcall.input = hc.param; - vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; - vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; - vcpu->arch.complete_userspace_io = - kvm_hv_hypercall_complete_userspace; - return 0; + goto hypercall_userspace_exit; } default: ret = HV_STATUS_INVALID_HYPERCALL_CODE; @@ -2352,6 +2338,15 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) hypercall_complete: return kvm_hv_hypercall_complete(vcpu, ret); + +hypercall_userspace_exit: + vcpu->run->exit_reason = KVM_EXIT_HYPERV; + vcpu->run->hyperv.type = KVM_EXIT_HYPERV_HCALL; + vcpu->run->hyperv.u.hcall.input = hc.param; + vcpu->run->hyperv.u.hcall.params[0] = hc.ingpa; + vcpu->run->hyperv.u.hcall.params[1] = hc.outgpa; + vcpu->arch.complete_userspace_io = kvm_hv_hypercall_complete_userspace; + return 0; } void kvm_hv_init_vm(struct kvm *kvm) From patchwork Sat Nov 5 04:57:00 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 13032829 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 BB9DAC4332F for ; Sat, 5 Nov 2022 04:57:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229615AbiKEE5Q (ORCPT ); Sat, 5 Nov 2022 00:57:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47044 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229588AbiKEE5O (ORCPT ); Sat, 5 Nov 2022 00:57:14 -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 E2D6D317E6 for ; Fri, 4 Nov 2022 21:57:13 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id b142-20020a253494000000b006ca86d5f40fso6641449yba.19 for ; Fri, 04 Nov 2022 21:57: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=BW+rPCZyrgfGjBQ3NJWBgC70ZM2E4N7Rxm+O8srguEk=; b=pAlbE6bcgnNjnuoJ+rX9hnRtuqI6cTg40qoUAFHN13LuRDcL3E92YcRjLGNk/oYc1n lMjbz30yCvufkdfqXQA3/fxMYImVQZbw8fw0LLfvujVII+d/gJtZGk5OZx3Y6xX2UlKX clwas/NpZLJ4598nf/qr4z3AU+hMroJYTuPVhS9X4Wn2eWqc7fHYGbaHKeSCL2ATiJNu 8TZHj0l1n2OEFVQXolbTx96GQC5QgT9/DCwhp9kQQSokd2g6V9quOCvzJSUgR82+Y64R w+kULRNopwY+6Gwt94hupB3kAPy6suRLjrZ8REtS5eZPl30kqte/ubZfadv4N4iqV7V5 Sdpw== 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=BW+rPCZyrgfGjBQ3NJWBgC70ZM2E4N7Rxm+O8srguEk=; b=hoQzh4HAiuCX+UO5aeSDEe9/vxPxRtgbubiThSjw+rp2di0TH6IousnxqUqnASKz2x cTKfNbQQ4Eol2bnKZtmjHsH4u29J+6y9oHpLryAyQbj8I3Sc5Purp8nH5AcheAkpTdo5 PoBJp9mbRbn4aEhTGsr/QDscR1NfAtimF4vgHB6YqVlRwTjlCqo3ou2k0KNVeeVpHF3Z G2vZMTCKpj2iuxRum+VMs4o+KtzdIC/DmLmzBrG0zKcnzVKp2sRXiKQQOsk0QFi0FAeK q7bBsPhmgHYjScZGT6NEelOOCKvvypbMzU9vwbqzjrk6s/oiZZnSablaOyZ32HYgWiCB C9cQ== X-Gm-Message-State: ACrzQf0g+Zq6JSWifJG4UkFOZtQ62xCIiaCtXCYiVHmXrTNYS9AKI+ee odwMi312WYU/LJMCbdrfcizorwOy6K6v X-Google-Smtp-Source: AMsMyM78gyHl/Ehhg39fKDPk6sd67w5nGZMpxmuBesaLcrfMLc4OytNZQthO+brQuFxPwEupOLckpKjKYoRe X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a5b:a0f:0:b0:691:6fea:deda with SMTP id k15-20020a5b0a0f000000b006916feadedamr37985774ybq.377.1667624233167; Fri, 04 Nov 2022 21:57:13 -0700 (PDT) Date: Fri, 4 Nov 2022 21:57:00 -0700 In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221105045704.2315186-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221105045704.2315186-3-vipinsh@google.com> Subject: [PATCH 2/6] KVM: x86: hyper-v: Add extended hypercall support in Hyper-v From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Add support for extended hypercall in Hyper-v. Hyper-v TLFS 6.0b describes hypercalls above call code 0x8000 as extended hypercalls. A Hyper-v hypervisor's guest VM finds availability of extended hypercalls via CPUID.0x40000003.EBX BIT(20). If the bit is set then the guest can call extended hypercalls. All extended hypercalls will exit to userspace by default. This allows for easy support of future hypercalls without being dependent on KVM releases. If there will be need to process the hypercall in KVM instead of userspace then KVM can create a capability which userspace can query to know which hypercalls can be handled by the KVM and enable handling of those hypercalls. Signed-off-by: Vipin Sharma --- arch/x86/kvm/hyperv.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/arch/x86/kvm/hyperv.c b/arch/x86/kvm/hyperv.c index db2f96a0ae23..85c6100f4451 100644 --- a/arch/x86/kvm/hyperv.c +++ b/arch/x86/kvm/hyperv.c @@ -40,6 +40,12 @@ #define KVM_HV_MAX_SPARSE_VCPU_SET_BITS DIV_ROUND_UP(KVM_MAX_VCPUS, 64) +/* + * The TLFS carves out 64 possible extended hypercalls, numbered sequentially + * after the base capabilities extended hypercall. + */ +#define HV_EXT_CALL_MAX (HV_EXT_CALL_QUERY_CAPABILITIES + 64) + static void stimer_mark_pending(struct kvm_vcpu_hv_stimer *stimer, bool vcpu_kick); @@ -2178,6 +2184,9 @@ static bool hv_check_hypercall_access(struct kvm_vcpu_hv *hv_vcpu, u16 code) case HVCALL_SEND_IPI: return hv_vcpu->cpuid_cache.enlightenments_eax & HV_X64_CLUSTER_IPI_RECOMMENDED; + case HV_EXT_CALL_QUERY_CAPABILITIES ... HV_EXT_CALL_MAX: + return hv_vcpu->cpuid_cache.features_ebx & + HV_ENABLE_EXTENDED_HYPERCALLS; default: break; } @@ -2331,6 +2340,12 @@ int kvm_hv_hypercall(struct kvm_vcpu *vcpu) } goto hypercall_userspace_exit; } + case HV_EXT_CALL_QUERY_CAPABILITIES ... HV_EXT_CALL_MAX: + if (unlikely(hc.fast)) { + ret = HV_STATUS_INVALID_PARAMETER; + break; + } + goto hypercall_userspace_exit; default: ret = HV_STATUS_INVALID_HYPERCALL_CODE; break; @@ -2489,6 +2504,7 @@ int kvm_get_hv_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid2 *cpuid, ent->ebx |= HV_POST_MESSAGES; ent->ebx |= HV_SIGNAL_EVENTS; + ent->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS; ent->edx |= HV_X64_HYPERCALL_XMM_INPUT_AVAILABLE; ent->edx |= HV_FEATURE_FREQUENCY_MSRS_AVAILABLE; From patchwork Sat Nov 5 04:57:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 13032830 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 DEC3FC433FE for ; Sat, 5 Nov 2022 04:57:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229595AbiKEE5V (ORCPT ); Sat, 5 Nov 2022 00:57:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229607AbiKEE5P (ORCPT ); Sat, 5 Nov 2022 00:57:15 -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 1B2F231DEE for ; Fri, 4 Nov 2022 21:57:15 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id a8-20020a621a08000000b0056e3c6c0746so3322328pfa.20 for ; Fri, 04 Nov 2022 21:57:15 -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=n6Upwu++k4u4pvTQ5ggpu28fN9WErU6ZbFJtDquZjUo=; b=gwunFD8XRkB8tadYDha1qv/kRbDMr1XGQO+PmaHs5uPp80MzHt+AYtQBb1FEzg2RAR irHZokc2miBdZR7YaSYRWxBK9nEUsVNpZAOBvsupFTUytJOg0+ds2NYQoQyerq34hC+Y p1PkX1fGoXWBXXMPOx0mOyo/iwgTfnPgZzICMEnTkZu0mWKshtik31He5oszFlNyrz43 e7QeYPXeLdVF52hlBSFEjC/RJ3015l9aQEm4B7lVy9hj/5RtnywsGszEMFAL6k32tKvw jceiWseFi+j95U9168l+lnNladYXUm0KgO269XNo2V/8aVxzeNpZrKZFTqCmuY9a3kQ9 e2eQ== 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=n6Upwu++k4u4pvTQ5ggpu28fN9WErU6ZbFJtDquZjUo=; b=CW+A361yJK7miJu4/Mg9m2eWWXfaYcJ3V6WBZiwtOyCn4bAvSipJWInvH0HCRxlYz2 ng3XGQXGaYLQxgUwajvOoLyyrqKbD+/pghe3N7jvRB+xW8sN07SO5nP8cTWiSpH3MEcA jF+DAiWi+VSIeuJDUIlOVBvHG4s27ByQAPF6AGWZ5DM/56oTGVEf/7F5I/Tf3KZ10zdC ILDt9UymbemlkI9snqDgk0/VZpO6IfCkj4E/aWJvfnSmR/RXHe/KnWdYDehWwiKggnSN D/WmE0uOKynFShmGm+Wuq9c9O1H77armhPcdKePiiOd0oi/1Xs8tfBUW/1CmGrhQhYAT cVvg== X-Gm-Message-State: ACrzQf0eVlHhtcl3CyGyFT3M8ZshC5Y48MvMfMTsb5Y5RKPF+FEDuznh 5+c5UFLyQDKJKE/a0TTZ3rPPx8G4fVtp X-Google-Smtp-Source: AMsMyM6GijZVfmsYGrKZhmiG6jO7MByuyeJQVUsCsQrrtEvCpSi5e5egOzX0GKLixjyllbP207KuvjtNSk9y X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a05:6a00:2409:b0:54e:a3ad:d32d with SMTP id z9-20020a056a00240900b0054ea3add32dmr38883061pfh.70.1667624234662; Fri, 04 Nov 2022 21:57:14 -0700 (PDT) Date: Fri, 4 Nov 2022 21:57:01 -0700 In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221105045704.2315186-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221105045704.2315186-4-vipinsh@google.com> Subject: [PATCH 3/6] KVM: selftests: Test Hyper-V extended hypercall enablement From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Test Extended hypercall CPUID enablement Signed-off-by: Vipin Sharma --- tools/testing/selftests/kvm/include/x86_64/hyperv.h | 4 ++++ tools/testing/selftests/kvm/x86_64/hyperv_features.c | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h index b66910702c0a..075fd29071a6 100644 --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h @@ -110,6 +110,7 @@ #define HV_ACCESS_STATS BIT(8) #define HV_DEBUGGING BIT(11) #define HV_CPU_MANAGEMENT BIT(12) +#define HV_ENABLE_EXTENDED_HYPERCALLS BIT(20) #define HV_ISOLATION BIT(22) /* HYPERV_CPUID_FEATURES.EDX */ @@ -164,6 +165,9 @@ #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af #define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0 +/* Extended hypercalls */ +#define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001 + #define HV_FLUSH_ALL_PROCESSORS BIT(0) #define HV_FLUSH_ALL_VIRTUAL_ADDRESS_SPACES BIT(1) #define HV_FLUSH_NON_GLOBAL_MAPPINGS_ONLY BIT(2) diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c index 05b32e550a80..6b443ce456b6 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c @@ -602,6 +602,15 @@ static void guest_test_hcalls_access(void) hcall->expect = HV_STATUS_SUCCESS; break; case 19: + hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES; + hcall->expect = HV_STATUS_ACCESS_DENIED; + break; + case 20: + feat->ebx |= HV_ENABLE_EXTENDED_HYPERCALLS; + hcall->control = HV_EXT_CALL_QUERY_CAPABILITIES | HV_HYPERCALL_FAST_BIT; + hcall->expect = HV_STATUS_INVALID_PARAMETER; + break; + case 21: kvm_vm_free(vm); return; } From patchwork Sat Nov 5 04:57:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 13032831 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 6314BC4332F for ; Sat, 5 Nov 2022 04:57:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229678AbiKEE5a (ORCPT ); Sat, 5 Nov 2022 00:57:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47140 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229629AbiKEE5S (ORCPT ); Sat, 5 Nov 2022 00:57:18 -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 0FF2B32BBF for ; Fri, 4 Nov 2022 21:57:17 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id r10-20020a17090a1bca00b002137a500398so3110490pjr.5 for ; Fri, 04 Nov 2022 21:57:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gZsOXIQ/ETlFCBETTe/elKVE4lpjamgAUlI2bZ+SvJc=; b=jB5l9Hayd7t9U2kjL83ltyjH3Fv+WtodRQcAlUj5Es6ZLiwVw+6vA0eIgG4QChDpWm qp97gIUIxTuhE8CWduUzsHWOHOaOc0cDlf58T7tROsRAsdnWkInlARWh8Q7AI3SF+UOf XIDyiMmkY9dKUGQuS0BWS6DlZ6s2LhcRrdf3AxedTUAp9uyDF8HyV7cPWu76Tc62OWgE +r333jvm8DKwfwdSxuekRtSS1+lkpnMhLgp9Re9QXOozWKUs8BUraLGvXaClX120fr+w iJm8sdJwJif+9C4KMMhykA94EhrQ4CEwxShXIwEqo6OQdiOR62eTPe43L1+UURPdKGE8 lhLw== 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=gZsOXIQ/ETlFCBETTe/elKVE4lpjamgAUlI2bZ+SvJc=; b=pWxiYu6edgFhE53BEWUsf/zc21hUQaTxRylI6mcAQp2vcSf8fUBzZU1JcNIsDKrxZX KlErpctB9fy/2EbgixYkJSZgOOR4uBEwo2qeGsB89W5H8CKJUYIW5318NejbKuKqu35v KvGWCCaDYcsohh6bwHwqegSTIZS8APvJ/SupU2SJQwK6inV4NNaL9SHLUUI1mOFCjF5/ 0VJx/nCENHEVT4oiurrHoF+aDP/dIAre4lHiY+tg7dTCXE8y1NT86k1zWwX9uo48Vz9T PlsTcqrpSXXL180eZU7n9qwsSqW2Gz1fJcJ2nrBDoYVcMoUqBOgMlfLKmeDdWuPB5M6R REPw== X-Gm-Message-State: ACrzQf3puKKejofaIfW9YmZd1vGeIbw8RHaZtSqnFCTpZJtf1lNoPTSU tqTOKl7e1Mf/xgWN43N0OzenhigTOLH6 X-Google-Smtp-Source: AMsMyM6GW/7X0x4391P7gdBKlTG0oU4AiAlfthpj/AvnhYObrCwIFZxCcrHcQ/ggbAshvUS6jsR/1GvPBvbd X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a05:6a00:218a:b0:56d:1bb6:af4f with SMTP id h10-20020a056a00218a00b0056d1bb6af4fmr36690480pfi.11.1667624236628; Fri, 04 Nov 2022 21:57:16 -0700 (PDT) Date: Fri, 4 Nov 2022 21:57:02 -0700 In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221105045704.2315186-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221105045704.2315186-5-vipinsh@google.com> Subject: [PATCH 4/6] KVM: selftests: Make Hyper-V guest OS ID common From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Make guest OS ID calculation common to all hyperv tests and similar to hv_generate_guest_id(). Signed-off-by: Vipin Sharma --- tools/testing/selftests/kvm/include/x86_64/hyperv.h | 10 ++++++++++ tools/testing/selftests/kvm/x86_64/hyperv_clock.c | 2 +- tools/testing/selftests/kvm/x86_64/hyperv_features.c | 6 ++---- tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h index 075fd29071a6..9d8c325af1d9 100644 --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h @@ -9,6 +9,10 @@ #ifndef SELFTEST_KVM_HYPERV_H #define SELFTEST_KVM_HYPERV_H +#include + +#define HV_LINUX_VENDOR_ID 0x8100 + #define HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS 0x40000000 #define HYPERV_CPUID_INTERFACE 0x40000001 #define HYPERV_CPUID_VERSION 0x40000002 @@ -189,4 +193,10 @@ /* hypercall options */ #define HV_HYPERCALL_FAST_BIT BIT(16) +static inline uint64_t hv_linux_guest_id(void) +{ + return ((uint64_t)HV_LINUX_VENDOR_ID << 48) | + ((uint64_t)LINUX_VERSION_CODE << 16); +} + #endif /* !SELFTEST_KVM_HYPERV_H */ diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c index d576bc8ce823..f9112c5dc3f7 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_clock.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_clock.c @@ -104,7 +104,7 @@ static void guest_main(struct ms_hyperv_tsc_page *tsc_page, vm_paddr_t tsc_page_ /* Set Guest OS id to enable Hyper-V emulation */ GUEST_SYNC(1); - wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48); + wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id()); GUEST_SYNC(2); check_tsc_msr_rdtsc(); diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c index 6b443ce456b6..b5a42cf1ad9d 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c @@ -13,8 +13,6 @@ #include "processor.h" #include "hyperv.h" -#define LINUX_OS_ID ((u64)0x8100 << 48) - static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address, vm_vaddr_t output_address, uint64_t *hv_status) { @@ -71,7 +69,7 @@ static void guest_hcall(vm_vaddr_t pgs_gpa, struct hcall_data *hcall) GUEST_ASSERT(hcall->control); - wrmsr(HV_X64_MSR_GUEST_OS_ID, LINUX_OS_ID); + wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id()); wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa); if (!(hcall->control & HV_HYPERCALL_FAST_BIT)) { @@ -169,7 +167,7 @@ static void guest_test_msrs_access(void) */ msr->idx = HV_X64_MSR_GUEST_OS_ID; msr->write = 1; - msr->write_val = LINUX_OS_ID; + msr->write_val = hv_linux_guest_id(); msr->available = 1; break; case 3: diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c index a380ad7bb9b3..2c13a144b04c 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_svm_test.c @@ -69,7 +69,7 @@ static void __attribute__((__flatten__)) guest_code(struct svm_test_data *svm) GUEST_SYNC(1); - wrmsr(HV_X64_MSR_GUEST_OS_ID, (u64)0x8100 << 48); + wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id()); GUEST_ASSERT(svm->vmcb_gpa); /* Prepare for L2 execution. */ From patchwork Sat Nov 5 04:57:03 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 13032832 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 29037C4332F for ; Sat, 5 Nov 2022 04:57:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229501AbiKEE5c (ORCPT ); Sat, 5 Nov 2022 00:57:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229648AbiKEE52 (ORCPT ); Sat, 5 Nov 2022 00:57:28 -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 083D732BA4 for ; Fri, 4 Nov 2022 21:57:19 -0700 (PDT) Received: by mail-yw1-x1149.google.com with SMTP id 00721157ae682-368036d93abso62914077b3.18 for ; Fri, 04 Nov 2022 21:57:18 -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=WSg0IO7Xl+tozOh4xkbrWeBiwBnKrWj4O0/zH/r779g=; b=NOQmhkl231fjcgvdKUOzvyqky4m6MkeFoOb9YJMy3HeukRVol0YITwYGZxrAlAi1cj mFEKmkYeBHVPyFal3aNYHbnNAyvjJOzq/WYv39vZcBU6tYStPLObeOHlDcCEJ+78d3Ov DwYNm/O5Wce+rPgWTe/K57ztOSfjOabLgjFtrJURD7fGF4BqpahbuVQ4bYQZgJ+OZVK4 aIuToHu+DtIPXdheLe9xhldM9vXJlgsCj52tIJLpFdzQklrBCGg2lvY8FSkPZ8ziUT/x HxSDQgc/j2X2x/o+6bFoK893dTYDyG1lZTrP6nVyHjJbDsL1zveXVc7qTbQf2j3dqVSv oklg== 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=WSg0IO7Xl+tozOh4xkbrWeBiwBnKrWj4O0/zH/r779g=; b=Hf2Rj2IFvC2aegxffNeoVkA11JX8hN7The5vVWTenfsxmumA+Bxel/fnKRY7EZ8zF1 H5XhJTEVfhJkM/WjJYEuYXLO3+q8AhIH8mR0e6Vg8T/s5QC58zp31oNYvS+ZySwbKeW7 selxLypkgRrPKMkZETp/O/KzffKFzYyApXwaR9GVYFOizfxlnjVLwGnQcJG/STBCR9C8 kn7dS8cgAGloKUF7ffDGnyqnV6/t/+ZGaxCrNniDW4ZS61YYieXF4629KsFEUnoXjAtZ 5vYCXMWoYVrA0u4OcpbMSDC3LhXDgaFbc/MuGRXwOkKRwiK7i5ISO6KRx96ItQatcgmb Wv3Q== X-Gm-Message-State: ACrzQf2G2rU8lcnLCQuBRymFz8ZOAuI7uwIbDXGi1mBtMX5nYa7FU8me PBsSj5T+pEAljJdWnhiLbvAx16VFFxhZ X-Google-Smtp-Source: AMsMyM5FC5Vm+MC97SQrJN35M2k6kPcLK1kmaXUEWtZaQkudGpQKB4ob+eFNH4JK9+gWCshHCMMHgChwmzpN X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a81:1d95:0:b0:370:744a:5a07 with SMTP id d143-20020a811d95000000b00370744a5a07mr401209ywd.334.1667624238317; Fri, 04 Nov 2022 21:57:18 -0700 (PDT) Date: Fri, 4 Nov 2022 21:57:03 -0700 In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221105045704.2315186-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221105045704.2315186-6-vipinsh@google.com> Subject: [PATCH 5/6] KVM: selftests: Move hypercall() to hyper.h From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org hypercall() can be used by other hyperv tests, move it to hyperv.h. Signed-off-by: Vipin Sharma --- .../selftests/kvm/include/x86_64/hyperv.h | 17 +++++++++++++++++ .../selftests/kvm/x86_64/hyperv_features.c | 17 ----------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/kvm/include/x86_64/hyperv.h b/tools/testing/selftests/kvm/include/x86_64/hyperv.h index 9d8c325af1d9..87d8d9e444f7 100644 --- a/tools/testing/selftests/kvm/include/x86_64/hyperv.h +++ b/tools/testing/selftests/kvm/include/x86_64/hyperv.h @@ -199,4 +199,21 @@ static inline uint64_t hv_linux_guest_id(void) ((uint64_t)LINUX_VERSION_CODE << 16); } +static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address, + vm_vaddr_t output_address, uint64_t *hv_status) +{ + uint8_t vector; + + /* Note both the hypercall and the "asm safe" clobber r9-r11. */ + asm volatile("mov %[output_address], %%r8\n\t" + KVM_ASM_SAFE("vmcall") + : "=a" (*hv_status), + "+c" (control), "+d" (input_address), + KVM_ASM_SAFE_OUTPUTS(vector) + : [output_address] "r"(output_address), + "a" (-EFAULT) + : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS); + return vector; +} + #endif /* !SELFTEST_KVM_HYPERV_H */ diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_features.c b/tools/testing/selftests/kvm/x86_64/hyperv_features.c index b5a42cf1ad9d..31b22ee07dfb 100644 --- a/tools/testing/selftests/kvm/x86_64/hyperv_features.c +++ b/tools/testing/selftests/kvm/x86_64/hyperv_features.c @@ -13,23 +13,6 @@ #include "processor.h" #include "hyperv.h" -static inline uint8_t hypercall(u64 control, vm_vaddr_t input_address, - vm_vaddr_t output_address, uint64_t *hv_status) -{ - uint8_t vector; - - /* Note both the hypercall and the "asm safe" clobber r9-r11. */ - asm volatile("mov %[output_address], %%r8\n\t" - KVM_ASM_SAFE("vmcall") - : "=a" (*hv_status), - "+c" (control), "+d" (input_address), - KVM_ASM_SAFE_OUTPUTS(vector) - : [output_address] "r"(output_address), - "a" (-EFAULT) - : "cc", "memory", "r8", KVM_ASM_SAFE_CLOBBERS); - return vector; -} - struct msr_data { uint32_t idx; bool available; From patchwork Sat Nov 5 04:57:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vipin Sharma X-Patchwork-Id: 13032833 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 8A12EC433FE for ; Sat, 5 Nov 2022 04:57:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229635AbiKEE5h (ORCPT ); Sat, 5 Nov 2022 00:57:37 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229682AbiKEE5a (ORCPT ); Sat, 5 Nov 2022 00:57:30 -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 C8DC73FBA4 for ; Fri, 4 Nov 2022 21:57:20 -0700 (PDT) Received: by mail-yb1-xb4a.google.com with SMTP id o2-20020a5b0502000000b006cade5e7c07so6741934ybp.10 for ; Fri, 04 Nov 2022 21:57:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=X3X2UKpD6zQllEjbrRaWm6/Z/akGwGuBaRDivMqgr8Q=; b=VIDKnr7oA7dLSPu4w5AAFufkeugCkvAQRahHYHsXU5b9ZNq9wL+25O2wkVs8e4PuC/ TIJU6QavnK5/6WXiX/z9atzSeOYxxN2Nahv0m5k7qcjby/QI2LB0B60VwSJuUofBe8em QZwLj+BchqaqON0RTYkdrbnmiWpM1oP2EzCivxaSYZCgA87PmcETZGVQ3ccF2fMpcAHr cWZeisWkQyc0Bk7fNgQz6qQKy2M19Ta0hRC7betzNIFuIaGk36E2vtSNpwnC3DqcfDDu Dwzt+dxRSAdJmPdzr4nc0zgX4yVx5+c9+sFuFsL6dSSHjP6Azofe0euBAVXD71zZyScL XXTA== 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=X3X2UKpD6zQllEjbrRaWm6/Z/akGwGuBaRDivMqgr8Q=; b=efZFXT4Fb4EfvVyZ8E4gXbiPPxI803mtdBD6YJSVrpqdrIBWIu8ydPA8ktnMShxF2I BLAKtq3Q6SGPQX+XCyTHWCbOKoWX1uyJup4+/K6uk2H4KelELkkt5uKM4J2b/342yB5w lEYU53jUoE+bExajotsBCmR8IKMWgVgjjMdWCcgIaz5RMFsalY84lmThKPlqOZ4Yrrmn V8iJlX8bz+yHJ+RU/a7uCqeNgFWFLcQuZF6BJpBBTB6nF/lkUDxb5Ar67hYVk7MbY+J2 dgQ0qqmWkZvGIc59bFve9ZKbGXahazdF0KJLAJCfDmxP1komKS93OobQY4JFAWFFWOA5 vDvA== X-Gm-Message-State: ACrzQf0rXGZdX7aWpeTP50UCKxo9kglHqtzYCRxuJJepLw2Cq4hidyIT hzPaXq7YQRVEIOWeMh9v+65ia4/YX5iH X-Google-Smtp-Source: AMsMyM7Flg5aR1zClpNW2xshNq/VsxhK9frK2C/zEKLM3NFx5tw9w95ZDnLjFUdUdwDsyTvZZzdGAIe0MWDb X-Received: from vipin.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:479f]) (user=vipinsh job=sendgmr) by 2002:a25:af12:0:b0:6cb:b5d7:d64d with SMTP id a18-20020a25af12000000b006cbb5d7d64dmr38476719ybh.510.1667624240120; Fri, 04 Nov 2022 21:57:20 -0700 (PDT) Date: Fri, 4 Nov 2022 21:57:04 -0700 In-Reply-To: <20221105045704.2315186-1-vipinsh@google.com> Mime-Version: 1.0 References: <20221105045704.2315186-1-vipinsh@google.com> X-Mailer: git-send-email 2.38.1.273.g43a17bfeac-goog Message-ID: <20221105045704.2315186-7-vipinsh@google.com> Subject: [PATCH 6/6] KVM: selftests: Test Hyper-V extended hypercall exit to userspace From: Vipin Sharma To: seanjc@google.com, pbonzini@redhat.com, vkuznets@redhat.com Cc: dmatlack@google.com, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Vipin Sharma Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org Hyper-V extended hypercalls by default exit to userspace. Verify userspace gets the call, update the result and then guest verifies result it received. Signed-off-by: Vipin Sharma --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../kvm/x86_64/hyperv_extended_hcalls.c | 90 +++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 2f0d705db9db..ffe06dd1cc6e 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -24,6 +24,7 @@ /x86_64/kvm_pv_test /x86_64/hyperv_clock /x86_64/hyperv_cpuid +/x86_64/hyperv_extended_hcalls /x86_64/hyperv_features /x86_64/hyperv_svm_test /x86_64/max_vcpuid_cap_test diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 0172eb6cb6ee..366345099363 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -85,6 +85,7 @@ 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 +TEST_GEN_PROGS_x86_64 += x86_64/hyperv_extended_hcalls TEST_GEN_PROGS_x86_64 += x86_64/hyperv_features TEST_GEN_PROGS_x86_64 += x86_64/hyperv_svm_test TEST_GEN_PROGS_x86_64 += x86_64/kvm_clock_test diff --git a/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c new file mode 100644 index 000000000000..d378877235d4 --- /dev/null +++ b/tools/testing/selftests/kvm/x86_64/hyperv_extended_hcalls.c @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test Hyper-V extended hypercalls + * + * Copyright 2020 Google LLC + * Author: Vipin Sharma + */ + +#include "kvm_util.h" +#include "processor.h" +#include "hyperv.h" + +/* Any value is fine */ +#define EXT_CAPABILITIES 0xbull + +static void guest_code(vm_vaddr_t pgs_gpa, vm_vaddr_t output_pg_gva) +{ + uint64_t res, vector; + uint64_t *output_gva; + + wrmsr(HV_X64_MSR_GUEST_OS_ID, hv_linux_guest_id()); + wrmsr(HV_X64_MSR_HYPERCALL, pgs_gpa); + + output_gva = (uint64_t *)output_pg_gva; + + vector = hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, pgs_gpa, + pgs_gpa + 4096, &res); + + GUEST_ASSERT_1(!vector, vector); + GUEST_ASSERT_2(res == HV_STATUS_SUCCESS, res, HV_STATUS_SUCCESS); + + /* TLFS states output will be a uint64_t value */ + GUEST_ASSERT_2(*output_gva == EXT_CAPABILITIES, *output_gva, + EXT_CAPABILITIES); + + GUEST_DONE(); +} + +static void guest_extended_hcall_test(void) +{ + struct kvm_vcpu *vcpu; + struct kvm_run *run; + struct kvm_vm *vm; + struct ucall uc; + vm_vaddr_t hcall_page; + uint64_t *outval; + + vm = vm_create_with_one_vcpu(&vcpu, guest_code); + run = vcpu->run; + vcpu_enable_cap(vcpu, KVM_CAP_HYPERV_ENFORCE_CPUID, 1); + vcpu_set_hv_cpuid(vcpu); + + /* Hypercall input/output */ + hcall_page = vm_vaddr_alloc_pages(vm, 2); + memset(addr_gva2hva(vm, hcall_page), 0x0, 2 * getpagesize()); + vcpu_args_set(vcpu, 2, addr_gva2gpa(vm, hcall_page), hcall_page + 4096); + + vcpu_run(vcpu); + + TEST_ASSERT((run->exit_reason == KVM_EXIT_HYPERV), + "unexpected exit reason: %u (%s)", run->exit_reason, + exit_reason_str(run->exit_reason)); + + outval = addr_gpa2hva(vm, run->hyperv.u.hcall.params[1]); + *outval = EXT_CAPABILITIES; + run->hyperv.u.hcall.result = HV_STATUS_SUCCESS; + + vcpu_run(vcpu); + + 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_ABORT: + REPORT_GUEST_ASSERT_2(uc, "arg1 = %ld, arg2 = %ld"); + break; + case UCALL_DONE: + break; + default: + TEST_FAIL("Unhandled ucall: %ld", uc.cmd); + } + + kvm_vm_free(vm); +} + +int main(void) +{ + guest_extended_hcall_test(); +}