Skip to content

Commit f7fa034

Browse files
committed
arm/arm64: KVM: Clarify KVM_ARM_VCPU_INIT ABI
It is not clear that this ioctl can be called multiple times for a given vcpu. Userspace already does this, so clarify the ABI. Also specify that userspace is expected to always make secondary and subsequent calls to the ioctl with the same parameters for the VCPU as the initial call (which userspace also already does). Add code to check that userspace doesn't violate that ABI in the future, and move the kvm_vcpu_set_target() function which is currently duplicated between the 32-bit and 64-bit versions in guest.c to a common static function in arm.c, shared between both architectures. Acked-by: Marc Zyngier <[email protected]> Signed-off-by: Christoffer Dall <[email protected]>
1 parent b856a59 commit f7fa034

File tree

6 files changed

+48
-54
lines changed

6 files changed

+48
-54
lines changed

Documentation/virtual/kvm/api.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,6 +2453,11 @@ return ENOEXEC for that vcpu.
24532453
Note that because some registers reflect machine topology, all vcpus
24542454
should be created before this ioctl is invoked.
24552455

2456+
Userspace can call this function multiple times for a given vcpu, including
2457+
after the vcpu has been run. This will reset the vcpu to its initial
2458+
state. All calls to this function after the initial call must use the same
2459+
target and same set of feature flags, otherwise EINVAL will be returned.
2460+
24562461
Possible features:
24572462
- KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state.
24582463
Depends on KVM_CAP_ARM_PSCI. If not set, the CPU will be powered on

arch/arm/include/asm/kvm_host.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ struct kvm_vcpu_stat {
150150
u32 halt_wakeup;
151151
};
152152

153-
int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
154-
const struct kvm_vcpu_init *init);
155153
int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
156154
unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
157155
int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);

arch/arm/kvm/arm.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
263263
{
264264
/* Force users to call KVM_ARM_VCPU_INIT */
265265
vcpu->arch.target = -1;
266+
bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
266267

267268
/* Set up the timer */
268269
kvm_timer_vcpu_init(vcpu);
@@ -649,6 +650,48 @@ int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_level,
649650
return -EINVAL;
650651
}
651652

653+
static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
654+
const struct kvm_vcpu_init *init)
655+
{
656+
unsigned int i;
657+
int phys_target = kvm_target_cpu();
658+
659+
if (init->target != phys_target)
660+
return -EINVAL;
661+
662+
/*
663+
* Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
664+
* use the same target.
665+
*/
666+
if (vcpu->arch.target != -1 && vcpu->arch.target != init->target)
667+
return -EINVAL;
668+
669+
/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
670+
for (i = 0; i < sizeof(init->features) * 8; i++) {
671+
bool set = (init->features[i / 32] & (1 << (i % 32)));
672+
673+
if (set && i >= KVM_VCPU_MAX_FEATURES)
674+
return -ENOENT;
675+
676+
/*
677+
* Secondary and subsequent calls to KVM_ARM_VCPU_INIT must
678+
* use the same feature set.
679+
*/
680+
if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES &&
681+
test_bit(i, vcpu->arch.features) != set)
682+
return -EINVAL;
683+
684+
if (set)
685+
set_bit(i, vcpu->arch.features);
686+
}
687+
688+
vcpu->arch.target = phys_target;
689+
690+
/* Now we know what it is, we can reset it. */
691+
return kvm_reset_vcpu(vcpu);
692+
}
693+
694+
652695
static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu,
653696
struct kvm_vcpu_init *init)
654697
{

arch/arm/kvm/guest.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -273,31 +273,6 @@ int __attribute_const__ kvm_target_cpu(void)
273273
}
274274
}
275275

276-
int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
277-
const struct kvm_vcpu_init *init)
278-
{
279-
unsigned int i;
280-
281-
/* We can only cope with guest==host and only on A15/A7 (for now). */
282-
if (init->target != kvm_target_cpu())
283-
return -EINVAL;
284-
285-
vcpu->arch.target = init->target;
286-
bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
287-
288-
/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
289-
for (i = 0; i < sizeof(init->features) * 8; i++) {
290-
if (test_bit(i, (void *)init->features)) {
291-
if (i >= KVM_VCPU_MAX_FEATURES)
292-
return -ENOENT;
293-
set_bit(i, vcpu->arch.features);
294-
}
295-
}
296-
297-
/* Now we know what it is, we can reset it. */
298-
return kvm_reset_vcpu(vcpu);
299-
}
300-
301276
int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
302277
{
303278
int target = kvm_target_cpu();

arch/arm64/include/asm/kvm_host.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ struct kvm_vcpu_stat {
165165
u32 halt_wakeup;
166166
};
167167

168-
int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
169-
const struct kvm_vcpu_init *init);
170168
int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init);
171169
unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu);
172170
int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);

arch/arm64/kvm/guest.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -296,31 +296,6 @@ int __attribute_const__ kvm_target_cpu(void)
296296
return -EINVAL;
297297
}
298298

299-
int kvm_vcpu_set_target(struct kvm_vcpu *vcpu,
300-
const struct kvm_vcpu_init *init)
301-
{
302-
unsigned int i;
303-
int phys_target = kvm_target_cpu();
304-
305-
if (init->target != phys_target)
306-
return -EINVAL;
307-
308-
vcpu->arch.target = phys_target;
309-
bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES);
310-
311-
/* -ENOENT for unknown features, -EINVAL for invalid combinations. */
312-
for (i = 0; i < sizeof(init->features) * 8; i++) {
313-
if (init->features[i / 32] & (1 << (i % 32))) {
314-
if (i >= KVM_VCPU_MAX_FEATURES)
315-
return -ENOENT;
316-
set_bit(i, vcpu->arch.features);
317-
}
318-
}
319-
320-
/* Now we know what it is, we can reset it. */
321-
return kvm_reset_vcpu(vcpu);
322-
}
323-
324299
int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init)
325300
{
326301
int target = kvm_target_cpu();

0 commit comments

Comments
 (0)