From patchwork Thu Nov 10 01:53:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 13038222 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 455B3C4332F for ; Thu, 10 Nov 2022 01:55:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=82IbI9RFhr/+qscnHHHLTmyNu/FuZx/6TznGlT1Q/wE=; b=kI8XJlQJVlh9j2 kIRT59CtLY68Yoddey3dbMJprFDAN1XWwBwTEptQmCRzB9Cn9EcPxzIU+S3gKMYOYsgoBHp2ZY1ZI 5FG3KnR/6W9w9dHpw5rfs4zaJn/Me4kNCuotDPSvxyKfDKnlLTrTJ/KSqMCQQHdjXBR4CRy/pywgV yHrxl6hQMU66Z0WUEiyjCTtkRCDNVDeKmdv/9mV2WLNlzZGWcaO5hAexZ9kX7m/nEWKbZPanftaKf y5DG4PwDiH/EHhmJBtyCgyuEVIpHGWGc1sSo7+79h5ikPYwvIO9YGa7uMFFqL0drEQVOB+ksQefNy +wErsKbHhVREvoVRmr1w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oswmE-001jiX-MW; Thu, 10 Nov 2022 01:54:46 +0000 Received: from out0.migadu.com ([2001:41d0:2:267::]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oswm4-001jcS-N5 for linux-arm-kernel@lists.infradead.org; Thu, 10 Nov 2022 01:54:38 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1668045268; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UUsD3eBCKbdoGj1PTEkb6Ygy4UgyIiHbIOlpThhPpiY=; b=PO+ypaaJeM3qPl0fWuF2uvP/fo/sqnO82JCtIdUg25hsgiQyKcdBDlRzweSXh+qscVOKlI Q3Q3zySTlqmrXCBsQ3So4vz5HJAZ7xJA/EYr0Swvi0jsLbLcp5dIfK3cVQoVJ9FM6d/LLI 9FEG79SWTqEsViN5pBalcdS0DyxZDBQ= From: Oliver Upton To: Paolo Bonzini , Shuah Khan , Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton Cc: Raghavendra Rao Ananta , linux-kernel@vger.kernel.org, kvm@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu Subject: [RFC PATCH 3/3] KVM: selftests: Test user hypercalls Date: Thu, 10 Nov 2022 01:53:27 +0000 Message-Id: <20221110015327.3389351-4-oliver.upton@linux.dev> In-Reply-To: <20221110015327.3389351-1-oliver.upton@linux.dev> References: <20221110015327.3389351-1-oliver.upton@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20221109_175437_210137_81ED960E X-CRM114-Status: GOOD ( 13.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Signed-off-by: Oliver Upton --- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 1 + .../selftests/kvm/aarch64/user_hypercalls.c | 130 ++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 tools/testing/selftests/kvm/aarch64/user_hypercalls.c diff --git a/tools/testing/selftests/kvm/.gitignore b/tools/testing/selftests/kvm/.gitignore index 2f0d705db9db..4f45e987985f 100644 --- a/tools/testing/selftests/kvm/.gitignore +++ b/tools/testing/selftests/kvm/.gitignore @@ -5,6 +5,7 @@ /aarch64/get-reg-list /aarch64/hypercalls /aarch64/psci_test +/aarch64/user_hypercalls /aarch64/vcpu_width_config /aarch64/vgic_init /aarch64/vgic_irq diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 0172eb6cb6ee..8ac1988ea669 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -153,6 +153,7 @@ TEST_GEN_PROGS_aarch64 += aarch64/debug-exceptions TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/hypercalls TEST_GEN_PROGS_aarch64 += aarch64/psci_test +TEST_GEN_PROGS_aarch64 += aarch64/user_hypercalls TEST_GEN_PROGS_aarch64 += aarch64/vcpu_width_config TEST_GEN_PROGS_aarch64 += aarch64/vgic_init TEST_GEN_PROGS_aarch64 += aarch64/vgic_irq diff --git a/tools/testing/selftests/kvm/aarch64/user_hypercalls.c b/tools/testing/selftests/kvm/aarch64/user_hypercalls.c new file mode 100644 index 000000000000..94ac821c5474 --- /dev/null +++ b/tools/testing/selftests/kvm/aarch64/user_hypercalls.c @@ -0,0 +1,130 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include + +#include "kvm_util.h" +#include "processor.h" +#include "test_util.h" + +struct test_case { + uint64_t cap; + uint32_t function; + uint64_t args[6]; +}; + +#define TEST_OWNER(name) \ +{ \ + .cap = BIT_ULL(KVM_ARM_USER_HYPERCALL_OWNER_ ## name), \ + .function = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_32, \ + ARM_SMCCC_OWNER_ ## name, \ + 0), \ + .args = { \ + __LINE__, \ + __LINE__ + 1, \ + __LINE__ + 2, \ + __LINE__ + 3, \ + __LINE__ + 4, \ + __LINE__ + 5, \ + }, \ +}, \ +{ \ + .cap = BIT_ULL(KVM_ARM_USER_HYPERCALL_OWNER_ ## name), \ + .function = ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \ + ARM_SMCCC_SMC_64, \ + ARM_SMCCC_OWNER_ ## name, \ + 0), \ + .args = { \ + __LINE__, \ + __LINE__ + 1, \ + __LINE__ + 2, \ + __LINE__ + 3, \ + __LINE__ + 4, \ + __LINE__ + 5, \ + }, \ +} \ + +static struct test_case test_cases[] = { + TEST_OWNER(ARCH), + TEST_OWNER(CPU), + TEST_OWNER(SIP), + TEST_OWNER(OEM), + TEST_OWNER(STANDARD), + TEST_OWNER(STANDARD_HYP), + TEST_OWNER(VENDOR_HYP), + TEST_OWNER(TRUSTED_APP), + TEST_OWNER(TRUSTED_OS), +}; + +static void guest_main(const struct test_case *test) +{ + struct arm_smccc_res unused; + + smccc_hvc(test->function, test->args[0], test->args[1], test->args[2], + test->args[3], test->args[4], test->args[5], 0, &unused); + + GUEST_ASSERT(0); +} + +static void handle_hvc(const struct test_case *test, struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + + TEST_ASSERT(run->hypercall.nr == test->function, + "unexpected function ID: %llx (expected %x)", + run->hypercall.nr, test->function); + + TEST_ASSERT(!memcmp(&run->hypercall.args, &test->args, sizeof(test->args)), + "unexpected hypercall arguments"); +} + +static void handle_ucall(struct kvm_vcpu *vcpu) +{ + struct ucall uc; + + switch (get_ucall(vcpu, &uc)) { + case UCALL_ABORT: + REPORT_GUEST_ASSERT(uc); + break; + default: + TEST_FAIL("unhandled ucall: %lu", uc.cmd); + } +} + +static void run_test(const struct test_case *test) +{ + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + + vm = vm_create_with_one_vcpu(&vcpu, guest_main); + vm_enable_cap(vm, KVM_CAP_ARM_USER_HYPERCALLS, test->cap); + ucall_init(vm, NULL); + + vcpu_args_set(vcpu, 1, test); + + vcpu_run(vcpu); + switch (vcpu->run->exit_reason) { + case KVM_EXIT_HYPERCALL: + handle_hvc(test, vcpu); + break; + case KVM_EXIT_MMIO: + handle_ucall(vcpu); + break; + default: + TEST_FAIL("unhandled exit reason: %u (%s)", vcpu->run->exit_reason, + exit_reason_str(vcpu->run->exit_reason)); + } + + ucall_uninit(vm); + kvm_vm_free(vm); +} + +int main(void) +{ + int i; + + TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_USER_HYPERCALLS)); + + for (i = 0; i < ARRAY_SIZE(test_cases); i++) + run_test(&test_cases[i]); +}