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: 15886 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp818424wru; Fri, 4 Nov 2022 22:27:01 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6CXLrUpOsPqw4ZJ1JWqfk6W9CzgX1ue2CIEppsSKCHYpgSiBdkjSbjydbbe/CxZkBXzKMX X-Received: by 2002:a50:a406:0:b0:463:4fe5:67d1 with SMTP id u6-20020a50a406000000b004634fe567d1mr30695404edb.151.1667626021320; Fri, 04 Nov 2022 22:27:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1667626021; cv=none; d=google.com; s=arc-20160816; b=Vzx6K7fBfTTro6KVCBBn620CfSZNG9dZHOzfiqOH6FWOkUWwLLKtGXLgAQcjq4IKC8 9Qy0gQjXUvVwGwl9g9Q4S7sDXAr6IcLjfEHmTsLRi5LqTbeYb5Kc7fk+Uu0fIrj2RPZB ByOkWSjCp74ZPV6Culq/k30bBBuZdC+i86ILsDNXaCbBcRIH2mW4UD81J0CJzRAVDX8/ YSkLMO0eoyikCKJlsTsIFRDVOvpqJa2PpFJ2sXBAiwbGI7Xq8cnofTfhkO5dpfgOp1Yw 2i3fFyKL4il1LBGJgIwJ8O1o3b2QqJG3FKAOlX0V2X+i66oDlAPkgIIQdz83aUuoJgNJ NIoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=X3X2UKpD6zQllEjbrRaWm6/Z/akGwGuBaRDivMqgr8Q=; b=GO1VTOcMBNU53ZmPu6HVu3AubVu48aoY9v6VLfUmG7nU2dFgjyoPj8JXasFeOcz3R4 SRLGk1lfjCBIcEzxkjDmGsSUYGi1JcFhfHDxrB3Fd/8fek6CPeVtp/NlZb21jKpFULyT EN/gIRLD2V4XVBcqOJ1dFJsv9LfMOSmxBWxFsN70IjmoznzXqT0wj7/cKBJyjuhBMWJF DukAOWNVYqfruWkh8ul57nppk40Pd6159/uiBfksZKit8bfPlbGRiqOWmARFsXhtF+jW MW/8xK7SJJ78CtLYxSqSt639tbxR3km7U+0FdENzQUOhEmydElj08LFgHzarWq7mNN/O NR+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=VIDKnr7o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id bx25-20020a0564020b5900b004636ec90769si1625572edb.33.2022.11.04.22.26.24; Fri, 04 Nov 2022 22:27:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=VIDKnr7o; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229677AbiKEE5i (ORCPT + 99 others); Sat, 5 Nov 2022 00:57:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47644 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229688AbiKEE5a (ORCPT ); Sat, 5 Nov 2022 00:57:30 -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 C7C703FBA3 for ; Fri, 4 Nov 2022 21:57:20 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 130-20020a251188000000b006be249d0a70so6671207ybr.2 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=YZ52WhFmpmJAi6pl+6pz4rgNSUZwowp7XP0M/47wIGaiVPguQGMjy5GejkUXCdCCgr Cr4D3vNiieG16xJ4lGeAprvXgjDP9NhC3xeQBO6bcWy1STBvR7dIu9JR5Vn67AZS6V73 1auEN1I7BmxIgZeHV5YiixscpAr4+dEEq5G9JmNbL69+0YN34KR1c0zBmc7YjIf1mptA 7r6rXGeykgLfqRHZEbxxvQuUtpPnghdMdyzpDtQAfkawm1ob3EaUzhvdCBSnGCbz9+0J ZGICQQS7KiD635wLyXvSOev04Fuvib1dgyKFWud0ONQ7dBoXfwOwu4YCf+OvQpm5/pWZ AYjQ== X-Gm-Message-State: ACrzQf1AFBuWbanSQXdIU6uU3xlGXRqYBNGFXxmXkmf2oW4Dc+dut5J6 rBXnPzx7sEpae0w1JQh23SIc+FGY0rKU 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 X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1748632622621229890?= X-GMAIL-MSGID: =?utf-8?q?1748632622621229890?= 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(); +}