Skip to content

Commit d824dff

Browse files
ouptonMarc Zyngier
authored andcommitted
KVM: arm64: Add support for KVM_EXIT_HYPERCALL
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
1 parent fb88707 commit d824dff

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

Documentation/virt/kvm/api.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6221,11 +6221,25 @@ to the byte array.
62216221
__u64 flags;
62226222
} hypercall;
62236223

6224-
Unused. This was once used for 'hypercall to userspace'. To implement
6225-
such functionality, use KVM_EXIT_IO (x86) or KVM_EXIT_MMIO (all except s390).
6224+
6225+
It is strongly recommended that userspace use ``KVM_EXIT_IO`` (x86) or
6226+
``KVM_EXIT_MMIO`` (all except s390) to implement functionality that
6227+
requires a guest to interact with host userpace.
62266228

62276229
.. note:: KVM_EXIT_IO is significantly faster than KVM_EXIT_MMIO.
62286230

6231+
For arm64:
6232+
----------
6233+
6234+
``nr`` contains the function ID of the guest's SMCCC call. Userspace is
6235+
expected to use the ``KVM_GET_ONE_REG`` ioctl to retrieve the call
6236+
parameters from the vCPU's GPRs.
6237+
6238+
Definition of ``flags``:
6239+
- ``KVM_HYPERCALL_EXIT_SMC``: Indicates that the guest used the SMC
6240+
conduit to initiate the SMCCC call. If this bit is 0 then the guest
6241+
used the HVC conduit for the SMCCC call.
6242+
62296243
::
62306244

62316245
/* KVM_EXIT_TPR_ACCESS */

arch/arm64/include/uapi/asm/kvm.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,16 @@ enum {
472472
enum kvm_smccc_filter_action {
473473
KVM_SMCCC_FILTER_HANDLE = 0,
474474
KVM_SMCCC_FILTER_DENY,
475+
KVM_SMCCC_FILTER_FWD_TO_USER,
475476

476477
#ifdef __KERNEL__
477478
NR_SMCCC_FILTER_ACTIONS
478479
#endif
479480
};
480481

482+
/* arm64-specific KVM_EXIT_HYPERCALL flags */
483+
#define KVM_HYPERCALL_EXIT_SMC (1U << 0)
484+
481485
#endif
482486

483487
#endif /* __ARM_KVM_H__ */

arch/arm64/kvm/handle_exit.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ static int handle_smc(struct kvm_vcpu *vcpu)
7171
* Trap exception, not a Secure Monitor Call exception [...]"
7272
*
7373
* We need to advance the PC after the trap, as it would
74-
* otherwise return to the same address...
74+
* otherwise return to the same address. Furthermore, pre-incrementing
75+
* the PC before potentially exiting to userspace maintains the same
76+
* abstraction for both SMCs and HVCs.
7577
*/
7678
kvm_incr_pc(vcpu);
7779

arch/arm64/kvm/hypercalls.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ static u8 kvm_smccc_get_action(struct kvm_vcpu *vcpu, u32 func_id)
180180
return KVM_SMCCC_FILTER_DENY;
181181
}
182182

183+
static void kvm_prepare_hypercall_exit(struct kvm_vcpu *vcpu, u32 func_id)
184+
{
185+
u8 ec = ESR_ELx_EC(kvm_vcpu_get_esr(vcpu));
186+
struct kvm_run *run = vcpu->run;
187+
188+
run->exit_reason = KVM_EXIT_HYPERCALL;
189+
run->hypercall.nr = func_id;
190+
run->hypercall.flags = 0;
191+
192+
if (ec == ESR_ELx_EC_SMC32 || ec == ESR_ELx_EC_SMC64)
193+
run->hypercall.flags |= KVM_HYPERCALL_EXIT_SMC;
194+
}
195+
183196
int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
184197
{
185198
struct kvm_smccc_features *smccc_feat = &vcpu->kvm->arch.smccc_feat;
@@ -195,6 +208,9 @@ int kvm_smccc_call_handler(struct kvm_vcpu *vcpu)
195208
break;
196209
case KVM_SMCCC_FILTER_DENY:
197210
goto out;
211+
case KVM_SMCCC_FILTER_FWD_TO_USER:
212+
kvm_prepare_hypercall_exit(vcpu, func_id);
213+
return 0;
198214
default:
199215
WARN_RATELIMIT(1, "Unhandled SMCCC filter action: %d\n", action);
200216
goto out;

0 commit comments

Comments
 (0)