KVM: arm64: Add support for KVM_EXIT_HYPERCALL
authorOliver Upton <oliver.upton@linux.dev>
Tue, 4 Apr 2023 15:40:45 +0000 (15:40 +0000)
committerMarc Zyngier <maz@kernel.org>
Wed, 5 Apr 2023 11:07:41 +0000 (12:07 +0100)
In anticipation of user hypercall filters, add the necessary plumbing to
get SMCCC calls out to userspace. Even though the exit structure has
space for KVM to pass register arguments, let's just avoid it altogether
and let userspace poke at the registers via KVM_GET_ONE_REG.

This deliberately stretches the definition of a 'hypercall' to cover
SMCs from EL1 in addition to the HVCs we know and love. KVM doesn't
support EL1 calls into secure services, but now we can paint that as a
userspace problem and be done with it.

Finally, we need a flag to let userspace know what conduit instruction
was used (i.e. SMC vs. HVC).

Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
Signed-off-by: Marc Zyngier <maz@kernel.org>
Link: https://lore.kernel.org/r/20230404154050.2270077-9-oliver.upton@linux.dev
Documentation/virt/kvm/api.rst
arch/arm64/include/uapi/asm/kvm.h
arch/arm64/kvm/handle_exit.c
arch/arm64/kvm/hypercalls.c

index 9b01e3d0e7577b3d265ea47cc58e20d6db939692..9497792c4ee5661b098c2b41d12d7fb9e8b75673 100644 (file)
@@ -6221,11 +6221,25 @@ to the byte array.
                        __u64 flags;
                } hypercall;
 
-Unused.  This was once used for 'hypercall to userspace'.  To implement
-such functionality, use KVM_EXIT_IO (x86) or KVM_EXIT_MMIO (all except s390).
+
+It is strongly recommended that userspace use ``KVM_EXIT_IO`` (x86) or
+``KVM_EXIT_MMIO`` (all except s390) to implement functionality that
+requires a guest to interact with host userpace.
 
 .. note:: KVM_EXIT_IO is significantly faster than KVM_EXIT_MMIO.
 
+For arm64:
+----------
+
+``nr`` contains the function ID of the guest's SMCCC call. Userspace is
+expected to use the ``KVM_GET_ONE_REG`` ioctl to retrieve the call
+parameters from the vCPU's GPRs.
+
+Definition of ``flags``:
+ - ``KVM_HYPERCALL_EXIT_SMC``: Indicates that the guest used the SMC
+   conduit to initiate the SMCCC call. If this bit is 0 then the guest
+   used the HVC conduit for the SMCCC call.
+
 ::
 
                /* KVM_EXIT_TPR_ACCESS */
index f9672ef1159aafbfc380145371f70e8d516ef017..f86446c5a7e300695eb0aceadf6d2e8fa5dc44fa 100644 (file)
@@ -472,12 +472,16 @@ enum {
 enum kvm_smccc_filter_action {
        KVM_SMCCC_FILTER_HANDLE = 0,
        KVM_SMCCC_FILTER_DENY,
+       KVM_SMCCC_FILTER_FWD_TO_USER,
 
 #ifdef __KERNEL__
        NR_SMCCC_FILTER_ACTIONS
 #endif
 };
 
+/* arm64-specific KVM_EXIT_HYPERCALL flags */
+#define KVM_HYPERCALL_EXIT_SMC (1U << 0)
+
 #endif
 
 #endif /* __ARM_KVM_H__ */
index 68f95dcd41a18c3912272f53ef9ac104d7f394c1..3f43e20c48b615d9cf414c15caca91303d7cb3ea 100644 (file)
@@ -71,7 +71,9 @@ static int handle_smc(struct kvm_vcpu *vcpu)
         * Trap exception, not a Secure Monitor Call exception [...]"
         *
         * We need to advance the PC after the trap, as it would
-        * otherwise return to the same address...
+        * otherwise return to the same address. Furthermore, pre-incrementing
+        * the PC before potentially exiting to userspace maintains the same
+        * abstraction for both SMCs and HVCs.
         */
        kvm_incr_pc(vcpu);
 
index ba7cd84c6668689e2f8bf015b4d0d3c057b47d4c..2db53709bec15aa9cef8bbfa264849ee29a39ce3 100644 (file)
@@ -180,6 +180,19 @@ static u8 kvm_smccc_get_action(struct kvm_vcpu *vcpu, u32 func_id)
        return KVM_SMCCC_FILTER_DENY;
 }
 
+static void kvm_prepare_hypercall_exit(struct kvm_vcpu *vcpu, u32 func_id)
+{
+       u8 ec = ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
+       struct kvm_run *run = vcpu->run;
+
+       run->exit_reason = KVM_EXIT_HYPERCALL;
+       run->hypercall.nr = func_id;
+       run->hypercall.flags = 0;
+
+       if (ec == ESR_ELx_EC_SMC32 || ec == ESR_ELx_EC_SMC64)
+               run->hypercall.flags |= KVM_HYPERCALL_EXIT_SMC;
+}
+
 int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
 {
        struct kvm_smccc_features *smccc_feat = &vcpu->kvm->arch.smccc_feat;
@@ -195,6 +208,9 @@ int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
                break;
        case KVM_SMCCC_FILTER_DENY:
                goto out;
+       case KVM_SMCCC_FILTER_FWD_TO_USER:
+               kvm_prepare_hypercall_exit(vcpu, func_id);
+               return 0;
        default:
                WARN_RATELIMIT(1, "Unhandled SMCCC filter action: %d\n", action);
                goto out;