Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <marc.zyngier@arm.com>
Signed-off-by: Christoffer Dall <christoffer.dall@linaro.org>

+48 -54
+5
Documentation/virtual/kvm/api.txt
··· 2453 2453 Note that because some registers reflect machine topology, all vcpus 2454 2454 should be created before this ioctl is invoked. 2455 2455 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 + 2456 2461 Possible features: 2457 2462 - KVM_ARM_VCPU_POWER_OFF: Starts the CPU in a power-off state. 2458 2463 Depends on KVM_CAP_ARM_PSCI. If not set, the CPU will be powered on
-2
arch/arm/include/asm/kvm_host.h
··· 150 150 u32 halt_wakeup; 151 151 }; 152 152 153 - int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 154 - const struct kvm_vcpu_init *init); 155 153 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init); 156 154 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu); 157 155 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
+43
arch/arm/kvm/arm.c
··· 263 263 { 264 264 /* Force users to call KVM_ARM_VCPU_INIT */ 265 265 vcpu->arch.target = -1; 266 + bitmap_zero(vcpu->arch.features, KVM_VCPU_MAX_FEATURES); 266 267 267 268 /* Set up the timer */ 268 269 kvm_timer_vcpu_init(vcpu); ··· 649 648 650 649 return -EINVAL; 651 650 } 651 + 652 + static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 653 + const struct kvm_vcpu_init *init) 654 + { 655 + unsigned int i; 656 + int phys_target = kvm_target_cpu(); 657 + 658 + if (init->target != phys_target) 659 + return -EINVAL; 660 + 661 + /* 662 + * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must 663 + * use the same target. 664 + */ 665 + if (vcpu->arch.target != -1 && vcpu->arch.target != init->target) 666 + return -EINVAL; 667 + 668 + /* -ENOENT for unknown features, -EINVAL for invalid combinations. */ 669 + for (i = 0; i < sizeof(init->features) * 8; i++) { 670 + bool set = (init->features[i / 32] & (1 << (i % 32))); 671 + 672 + if (set && i >= KVM_VCPU_MAX_FEATURES) 673 + return -ENOENT; 674 + 675 + /* 676 + * Secondary and subsequent calls to KVM_ARM_VCPU_INIT must 677 + * use the same feature set. 678 + */ 679 + if (vcpu->arch.target != -1 && i < KVM_VCPU_MAX_FEATURES && 680 + test_bit(i, vcpu->arch.features) != set) 681 + return -EINVAL; 682 + 683 + if (set) 684 + set_bit(i, vcpu->arch.features); 685 + } 686 + 687 + vcpu->arch.target = phys_target; 688 + 689 + /* Now we know what it is, we can reset it. */ 690 + return kvm_reset_vcpu(vcpu); 691 + } 692 + 652 693 653 694 static int kvm_arch_vcpu_ioctl_vcpu_init(struct kvm_vcpu *vcpu, 654 695 struct kvm_vcpu_init *init)
-25
arch/arm/kvm/guest.c
··· 273 273 } 274 274 } 275 275 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 - 301 276 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init) 302 277 { 303 278 int target = kvm_target_cpu();
-2
arch/arm64/include/asm/kvm_host.h
··· 165 165 u32 halt_wakeup; 166 166 }; 167 167 168 - int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, 169 - const struct kvm_vcpu_init *init); 170 168 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init); 171 169 unsigned long kvm_arm_num_regs(struct kvm_vcpu *vcpu); 172 170 int kvm_arm_copy_reg_indices(struct kvm_vcpu *vcpu, u64 __user *indices);
-25
arch/arm64/kvm/guest.c
··· 296 296 return -EINVAL; 297 297 } 298 298 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 - 324 299 int kvm_vcpu_preferred_target(struct kvm_vcpu_init *init) 325 300 { 326 301 int target = kvm_target_cpu();