From patchwork Thu Nov 10 01:53:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 13038220 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 E7604C4332F for ; Thu, 10 Nov 2022 01:55:31 +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=roBzduLkN2APFLQWlD+Dxqm/AfzBhRWaB7sOIIqq9zQ=; b=dR01r9egBwy5De gZLZCjN7pnzOuFlzHWP09xCwYtxO7ihd+utXHKnGB39UintQX6398G9MstDvlH8QlWv0RV+Ek/0jg dEjij5pA9Wd1+/h1hR0ogi9HIkVFNEmRUvDBL9//omVFwZvykKAff0lWp1oGQoYILWLFaWm4PU62V pbTDPGvehNn2mt9nzgrzFeKvSJFjD5phlOALamdLwMYbwmgQybyIGysIGtPlBRH7LehtFQ7iYHVIJ DOFivsM915uPJuzM3tBt1ZUXsaA18S28y71p3KAeRs3BdFQy0B8AqNoXNi9G8y5HeLexvXW8VH1Vw RaizwYUwMX9yB+QQ59sg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oswlv-001jag-8s; Thu, 10 Nov 2022 01:54:27 +0000 Received: from out0.migadu.com ([94.23.1.103]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oswlq-001jZ8-Mb for linux-arm-kernel@lists.infradead.org; Thu, 10 Nov 2022 01:54:25 +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=1668045253; 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=D0K3at+tNKaP0/XbZ4HFUAvRHCj+YDnrPrqun7nyIUM=; b=L3Jgqsb2RQ7n2NJlQBeisEZTDbrDv5ycAnr166nq5KUJ/G8ftX5TI388ZxauPxR6teuHwI cT5+4sobg/U8kItzFxpwyVOKmM7rVKLn+5go2Nr2uEOFEtkNPxkWqBkIGq5fYMIJI8OVIK 2jAPmOs29OSkml5yXjsu1qSzli+SOfs= From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Catalin Marinas , Will Deacon Cc: Raghavendra Rao Ananta , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/3] KVM: arm64: Use a generalized accessor for SMCCC args Date: Thu, 10 Nov 2022 01:53:25 +0000 Message-Id: <20221110015327.3389351-2-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_175424_034381_48126CEA X-CRM114-Status: GOOD ( 13.51 ) 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 Avoid the need to introduce yet another accessor for every argument position with a generalized one instead. Align with the SMCCC specification and count arguments starting from 1. No functional change intended. Signed-off-by: Oliver Upton --- arch/arm64/include/asm/kvm_hypercalls.h | 22 ++++++++-------------- arch/arm64/kvm/hypercalls.c | 4 ++-- arch/arm64/kvm/psci.c | 14 +++++++------- arch/arm64/kvm/pvtime.c | 2 +- arch/arm64/kvm/trng.c | 4 ++-- 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/arch/arm64/include/asm/kvm_hypercalls.h b/arch/arm64/include/asm/kvm_hypercalls.h index dfebe8dd8dcd..c30d6ae76000 100644 --- a/arch/arm64/include/asm/kvm_hypercalls.h +++ b/arch/arm64/include/asm/kvm_hypercalls.h @@ -13,20 +13,14 @@ static inline u32 smccc_get_function(struct kvm_vcpu *vcpu) return vcpu_get_reg(vcpu, 0); } -static inline unsigned long smccc_get_arg1(struct kvm_vcpu *vcpu) -{ - return vcpu_get_reg(vcpu, 1); -} - -static inline unsigned long smccc_get_arg2(struct kvm_vcpu *vcpu) -{ - return vcpu_get_reg(vcpu, 2); -} - -static inline unsigned long smccc_get_arg3(struct kvm_vcpu *vcpu) -{ - return vcpu_get_reg(vcpu, 3); -} +#define smccc_get_arg(vcpu, arg) \ +({ \ + u64 __val; \ + \ + BUILD_BUG_ON(arg < 1 || arg > 17); \ + __val = vcpu_get_reg(vcpu, arg); \ + __val; \ +}) static inline void smccc_set_retval(struct kvm_vcpu *vcpu, unsigned long a0, diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 6804075ce57f..62ce45d0d957 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -40,7 +40,7 @@ static void kvm_ptp_get_time(struct kvm_vcpu *vcpu, u64 *val) * (virtual or physical) with the first argument of the SMCCC * call. In case the identifier is not supported, error out. */ - feature = smccc_get_arg1(vcpu); + feature = smccc_get_arg(vcpu, 1); switch (feature) { case KVM_PTP_VIRT_COUNTER: cycles = systime_snapshot.cycles - vcpu_read_sys_reg(vcpu, CNTVOFF_EL2); @@ -136,7 +136,7 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) val[0] = ARM_SMCCC_VERSION_1_1; break; case ARM_SMCCC_ARCH_FEATURES_FUNC_ID: - feature = smccc_get_arg1(vcpu); + feature = smccc_get_arg(vcpu, 1); switch (feature) { case ARM_SMCCC_ARCH_WORKAROUND_1: switch (arm64_get_spectre_v2_state()) { diff --git a/arch/arm64/kvm/psci.c b/arch/arm64/kvm/psci.c index 809710808b25..15a380858ccb 100644 --- a/arch/arm64/kvm/psci.c +++ b/arch/arm64/kvm/psci.c @@ -63,7 +63,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) struct kvm_vcpu *vcpu = NULL; unsigned long cpu_id; - cpu_id = smccc_get_arg1(source_vcpu); + cpu_id = smccc_get_arg(source_vcpu, 1); if (!kvm_psci_valid_affinity(source_vcpu, cpu_id)) return PSCI_RET_INVALID_PARAMS; @@ -84,7 +84,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) reset_state = &vcpu->arch.reset_state; - reset_state->pc = smccc_get_arg2(source_vcpu); + reset_state->pc = smccc_get_arg(source_vcpu, 2); /* Propagate caller endianness */ reset_state->be = kvm_vcpu_is_be(source_vcpu); @@ -93,7 +93,7 @@ static unsigned long kvm_psci_vcpu_on(struct kvm_vcpu *source_vcpu) * NOTE: We always update r0 (or x0) because for PSCI v0.1 * the general purpose registers are undefined upon CPU_ON. */ - reset_state->r0 = smccc_get_arg3(source_vcpu); + reset_state->r0 = smccc_get_arg(source_vcpu, 3); WRITE_ONCE(reset_state->reset, true); kvm_make_request(KVM_REQ_VCPU_RESET, vcpu); @@ -120,8 +120,8 @@ static unsigned long kvm_psci_vcpu_affinity_info(struct kvm_vcpu *vcpu) struct kvm *kvm = vcpu->kvm; struct kvm_vcpu *tmp; - target_affinity = smccc_get_arg1(vcpu); - lowest_affinity_level = smccc_get_arg2(vcpu); + target_affinity = smccc_get_arg(vcpu, 1); + lowest_affinity_level = smccc_get_arg(vcpu, 2); if (!kvm_psci_valid_affinity(vcpu, target_affinity)) return PSCI_RET_INVALID_PARAMS; @@ -317,7 +317,7 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor) val = minor == 0 ? KVM_ARM_PSCI_1_0 : KVM_ARM_PSCI_1_1; break; case PSCI_1_0_FN_PSCI_FEATURES: - arg = smccc_get_arg1(vcpu); + arg = smccc_get_arg(vcpu, 1); val = kvm_psci_check_allowed_function(vcpu, arg); if (val) break; @@ -371,7 +371,7 @@ static int kvm_psci_1_x_call(struct kvm_vcpu *vcpu, u32 minor) fallthrough; case PSCI_1_1_FN64_SYSTEM_RESET2: if (minor >= 1) { - arg = smccc_get_arg1(vcpu); + arg = smccc_get_arg(vcpu, 1); if (arg <= PSCI_1_1_RESET_TYPE_SYSTEM_WARM_RESET || arg >= PSCI_1_1_RESET_TYPE_VENDOR_START) { diff --git a/arch/arm64/kvm/pvtime.c b/arch/arm64/kvm/pvtime.c index 614b2e70b815..587eadf77f29 100644 --- a/arch/arm64/kvm/pvtime.c +++ b/arch/arm64/kvm/pvtime.c @@ -33,7 +33,7 @@ void kvm_update_stolen_time(struct kvm_vcpu *vcpu) long kvm_hypercall_pv_features(struct kvm_vcpu *vcpu) { - u32 feature = smccc_get_arg1(vcpu); + u32 feature = smccc_get_arg(vcpu, 1); long val = SMCCC_RET_NOT_SUPPORTED; switch (feature) { diff --git a/arch/arm64/kvm/trng.c b/arch/arm64/kvm/trng.c index b257d4eace50..953cb0d1b6ff 100644 --- a/arch/arm64/kvm/trng.c +++ b/arch/arm64/kvm/trng.c @@ -23,7 +23,7 @@ static const uuid_t arm_smc_trng_uuid __aligned(4) = UUID_INIT( static int kvm_trng_do_rnd(struct kvm_vcpu *vcpu, int size) { DECLARE_BITMAP(bits, TRNG_MAX_BITS64); - u32 num_bits = smccc_get_arg1(vcpu); + u32 num_bits = smccc_get_arg(vcpu, 1); int i; if (num_bits > 3 * size) { @@ -59,7 +59,7 @@ int kvm_trng_call(struct kvm_vcpu *vcpu) val = ARM_SMCCC_TRNG_VERSION_1_0; break; case ARM_SMCCC_TRNG_FEATURES: - switch (smccc_get_arg1(vcpu)) { + switch (smccc_get_arg(vcpu, 1)) { case ARM_SMCCC_TRNG_VERSION: case ARM_SMCCC_TRNG_FEATURES: case ARM_SMCCC_TRNG_GET_UUID: From patchwork Thu Nov 10 01:53:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oliver Upton X-Patchwork-Id: 13038221 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 67732C433FE for ; Thu, 10 Nov 2022 01:55:32 +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=HZPY5wF6ILBKzssG2g6UCs0pQa4Fva8uMJDFzS/hIVM=; b=WBCqNJxHoPj3X9 k+Cvjh2GQoNzN4LWPE8rwe8981uvwArPTlWsALQnptaCQ2PlTjFPEpCBvkSv6RCoZvP8hyP6Ob69U VR9Tix6sNyWDy4fsA7D6GyxiuBnba5WSBg7I75jpa0DlwN3WhqgzBLy+BgRLTV2hBG0u9xIaJQyOP YL2v1NIHIgPdqgY+hTv6bGiBq0AtCh8hdJwJzHwT/cSjEd7lKrdk6r8STnJ02Dx6W+59AU3HQcPX4 6THtd00QkkzxPZ8LngqeC0Olieai8WoS19Qy4IYQLfy2qN77dgx2fd9AOXCyEr0Rt7VTEkV+ZB/Wp RKoDtbpoIMfG1WSKRA9Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oswm4-001jdf-Hr; Thu, 10 Nov 2022 01:54:36 +0000 Received: from out0.migadu.com ([94.23.1.103]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oswlu-001jaP-Jn for linux-arm-kernel@lists.infradead.org; Thu, 10 Nov 2022 01:54:28 +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=1668045261; 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=e4NKzeDtoFaN8kBUL712bSOeRfxgXb7x1ONm16Q4y+o=; b=r+LeK2mnGqElNPkM/9TRPhVOvTFfQU8Rq0YfxxyXVdeML0jqbULDujWLfU8U1jjDWCD4C2 bVcdvjUU6nvu6U93vhCJgsXxxiFjIGkI2iLdiUr8nLHnlO2oEX2LmV4/c/k87TwZDZINUN A0g7LwBg2Bp+kUYlRQ7hek5somRyq2I= From: Oliver Upton To: Marc Zyngier , James Morse , Alexandru Elisei , Suzuki K Poulose , Oliver Upton , Catalin Marinas , Will Deacon , Paolo Bonzini Cc: Raghavendra Rao Ananta , linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, kvmarm@lists.cs.columbia.edu, linux-kernel@vger.kernel.org, kvm@vger.kernel.org Subject: [RFC PATCH 2/3] KVM: arm64: Allow userspace to trap SMCCC sub-ranges Date: Thu, 10 Nov 2022 01:53:26 +0000 Message-Id: <20221110015327.3389351-3-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_175426_980245_E4407DA9 X-CRM114-Status: GOOD ( 15.65 ) 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 As the SMCCC (and related specifications) march towards an 'everything and the kitchen sink' interface for interacting with a system, it is less likely that KVM will implement every supported feature. Add a capability that allows userspace to trap hypercall ranges, allowing the VMM to mix-and-match between calls handled in userspace vs. KVM. Signed-off-by: Oliver Upton --- arch/arm64/include/asm/kvm_host.h | 5 ++++ arch/arm64/include/uapi/asm/kvm.h | 15 ++++++++++ arch/arm64/kvm/arm.c | 10 +++++++ arch/arm64/kvm/hypercalls.c | 48 +++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 1 + 5 files changed, 79 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e33ed7c09a28..cc3872f1900c 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -52,6 +52,9 @@ #define KVM_HAVE_MMU_RWLOCK +#define KVM_ARM_USER_HYPERCALL_FLAGS \ + GENMASK_ULL(KVM_ARM_USER_HYPERCALL_FLAGS_COUNT - 1, 0) + /* * Mode of operation configurable with kvm-arm.mode early param. * See Documentation/admin-guide/kernel-parameters.txt for more information. @@ -104,11 +107,13 @@ struct kvm_arch_memory_slot { /** * struct kvm_smccc_features: Descriptor of the hypercall services exposed to the guests * + * @user_trap_bmap: Bitmap of SMCCC function ranges trapped to userspace * @std_bmap: Bitmap of standard secure service calls * @std_hyp_bmap: Bitmap of standard hypervisor service calls * @vendor_hyp_bmap: Bitmap of vendor specific hypervisor service calls */ struct kvm_smccc_features { + unsigned long user_trap_bmap; unsigned long std_bmap; unsigned long std_hyp_bmap; unsigned long vendor_hyp_bmap; diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index 316917b98707..07fa3f597e61 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -370,6 +370,21 @@ enum { #endif }; +enum { + KVM_ARM_USER_HYPERCALL_OWNER_ARCH = 0, + KVM_ARM_USER_HYPERCALL_OWNER_CPU = 1, + KVM_ARM_USER_HYPERCALL_OWNER_SIP = 2, + KVM_ARM_USER_HYPERCALL_OWNER_OEM = 3, + KVM_ARM_USER_HYPERCALL_OWNER_STANDARD = 4, + KVM_ARM_USER_HYPERCALL_OWNER_STANDARD_HYP = 5, + KVM_ARM_USER_HYPERCALL_OWNER_VENDOR_HYP = 6, + KVM_ARM_USER_HYPERCALL_OWNER_TRUSTED_APP = 7, + KVM_ARM_USER_HYPERCALL_OWNER_TRUSTED_OS = 8, +#ifdef __KERNEL__ + KVM_ARM_USER_HYPERCALL_FLAGS_COUNT, +#endif +}; + /* Device Control API: ARM VGIC */ #define KVM_DEV_ARM_VGIC_GRP_ADDR 0 #define KVM_DEV_ARM_VGIC_GRP_DIST_REGS 1 diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 6f0b56e7f8c7..6e8a222fc295 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -100,6 +100,13 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = 0; set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags); break; + case KVM_CAP_ARM_USER_HYPERCALLS: + if (cap->args[0] & ~KVM_ARM_USER_HYPERCALL_FLAGS) + return -EINVAL; + + r = 0; + kvm->arch.smccc_feat.user_trap_bmap = cap->args[0]; + break; default: r = -EINVAL; break; @@ -285,6 +292,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_ARM_PTRAUTH_GENERIC: r = system_has_full_ptr_auth(); break; + case KVM_CAP_ARM_USER_HYPERCALLS: + r = KVM_ARM_USER_HYPERCALL_FLAGS; + break; default: r = 0; } diff --git a/arch/arm64/kvm/hypercalls.c b/arch/arm64/kvm/hypercalls.c index 62ce45d0d957..22a23b12201d 100644 --- a/arch/arm64/kvm/hypercalls.c +++ b/arch/arm64/kvm/hypercalls.c @@ -92,6 +92,49 @@ static bool kvm_hvc_call_default_allowed(u32 func_id) } } +static bool kvm_hvc_call_user_trapped(struct kvm_vcpu *vcpu, u32 func_id) +{ + struct kvm *kvm = vcpu->kvm; + unsigned long *bmap = &kvm->arch.smccc_feat.user_trap_bmap; + + switch (ARM_SMCCC_OWNER_NUM(func_id)) { + case ARM_SMCCC_OWNER_ARCH: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_ARCH, bmap); + case ARM_SMCCC_OWNER_CPU: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_CPU, bmap); + case ARM_SMCCC_OWNER_SIP: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_SIP, bmap); + case ARM_SMCCC_OWNER_OEM: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_OEM, bmap); + case ARM_SMCCC_OWNER_STANDARD: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_STANDARD, bmap); + case ARM_SMCCC_OWNER_STANDARD_HYP: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_STANDARD_HYP, bmap); + case ARM_SMCCC_OWNER_VENDOR_HYP: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_VENDOR_HYP, bmap); + case ARM_SMCCC_OWNER_TRUSTED_APP ... ARM_SMCCC_OWNER_TRUSTED_APP_END: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_TRUSTED_APP, bmap); + case ARM_SMCCC_OWNER_TRUSTED_OS ... ARM_SMCCC_OWNER_TRUSTED_OS_END: + return test_bit(KVM_ARM_USER_HYPERCALL_OWNER_TRUSTED_OS, bmap); + default: + return false; + } +} + +static void kvm_hvc_prepare_user_trap(struct kvm_vcpu *vcpu) +{ + struct kvm_run *run = vcpu->run; + + run->exit_reason = KVM_EXIT_HYPERCALL; + run->hypercall.nr = smccc_get_function(vcpu); + run->hypercall.args[0] = smccc_get_arg(vcpu, 1); + run->hypercall.args[1] = smccc_get_arg(vcpu, 2); + run->hypercall.args[2] = smccc_get_arg(vcpu, 3); + run->hypercall.args[3] = smccc_get_arg(vcpu, 4); + run->hypercall.args[4] = smccc_get_arg(vcpu, 5); + run->hypercall.args[5] = smccc_get_arg(vcpu, 6); +} + static bool kvm_hvc_call_allowed(struct kvm_vcpu *vcpu, u32 func_id) { struct kvm_smccc_features *smccc_feat = &vcpu->kvm->arch.smccc_feat; @@ -128,6 +171,11 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu) u32 feature; gpa_t gpa; + if (kvm_hvc_call_user_trapped(vcpu, func_id)) { + kvm_hvc_prepare_user_trap(vcpu); + return 0; + } + if (!kvm_hvc_call_allowed(vcpu, func_id)) goto out; diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 0d5d4419139a..ea6c1983a545 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1178,6 +1178,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_S390_ZPCI_OP 221 #define KVM_CAP_S390_CPU_TOPOLOGY 222 #define KVM_CAP_DIRTY_LOG_RING_ACQ_REL 223 +#define KVM_CAP_ARM_USER_HYPERCALLS 224 #ifdef KVM_CAP_IRQ_ROUTING 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]); +}