From: David Woodhouse A VMM implementing a software privilege model such as Hyper-V VSM needs more than one plane, but kvm_arch_max_planes() returns the vendor ceiling, which is 1 everywhere except SEV-SNP (where VMPLs are guest-architectural), so KVM_CREATE_PLANE rejects every id above zero. Make KVM_CAP_PLANES a VM enable capability taking the number of planes userspace is going to manage, up to KVM_MAX_USER_PLANES (4), which the system fd now reports. It must be set before any vCPU or extra plane exists, so nothing is sized against the old limit, and cannot be changed once set. It also needs a split irqchip, which kvm_arch_max_planes() already requires before it reports more than one plane, so enabling it without one fails instead of silently granting a single plane, and KVM_CREATE_IRQCHIP is refused afterwards. Non-default VM types are rejected: SEV-SNP keeps deriving its count from the supported VMPLs, and protected-state VM types need their own audit first. So is more than one plane without TDP: with shadow paging every plane would write-track the guest page tables, and the per-gfn write-track count is only 16 bits wide. The same count is why the opt-in stops at four planes even with TDP: nested guests still have write-tracked shadow pages, each plane can add its own for a gfn, and four planes is the most that keeps the count below 2^16. KVM_MAX_PLANES stays 16, the bound for plane ids. A VM that does not enable the capability keeps reporting a single plane. Signed-off-by: David Woodhouse --- Documentation/virt/kvm/api.rst | 36 +++++++++++++++++++++ arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 55 +++++++++++++++++++++++++++++++-- include/linux/kvm_host.h | 12 +++++++ virt/kvm/kvm_main.c | 11 ++++--- 5 files changed, 108 insertions(+), 7 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 9aa09d8a39dc..25ce35d5361a 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6834,6 +6834,12 @@ See also: :ref:`KVM_SET_MEMORY_ATTRIBUTES`. Creates a new *plane*, i.e. a separate privilege level for the virtual machine. +The range of valid plane ids is [0, N) where N is the value returned by +KVM_CHECK_EXTENSION(KVM_CAP_PLANES) on the VM file descriptor. Plane 0 is +the VM itself; for VM types where the plane count is not guest-architectural, +ids above 0 only become valid after userspace enables KVM_CAP_PLANES with +the number of planes it is going to manage (see :ref:`KVM_CAP_PLANES`). + Each plane has a numeric id that is used when communicating with KVM. While KVM is currently agnostic to whether low ids are more or less privileged, it is expected that this will not always be the case in the future. For example KVM @@ -9234,6 +9240,36 @@ enabled, cmma can't be enabled anymore and pfmfi and the storage key interpretation are disabled. If cmma has already been enabled or the hpage_2g module parameter is not set to 1, -EINVAL is returned. +.. _KVM_CAP_PLANES: + +7.48 KVM_CAP_PLANES +------------------- + +:Architectures: x86 +:Target: VM +:Parameters: args[0] contains the number of planes to make available + +Opts the VM in to using multiple planes, raising the limit on plane ids +that KVM_CREATE_PLANE accepts to args[0], which must be between 1 and the +value KVM_CHECK_EXTENSION(KVM_CAP_PLANES) returns on the system fd. That +ceiling is currently 4: on x86 every plane can add its own shadow pages for +a gfn, and the per-gfn write-tracking count is only 16 bits wide. + +Planes other than plane 0 only exist when userspace manages them, so by +default a VM reports and accepts a single plane. VM types whose plane +count is guest-architectural (such as SEV-SNP, where it reflects the +supported VMPLs) report that count without this capability, and enabling +it on them fails with -EINVAL. Multiple planes also require two-dimensional +paging: without TDP, enabling more than one plane fails with -EINVAL. + +Planes require a split irqchip. Enable KVM_CAP_SPLIT_IRQCHIP first; +otherwise enabling this capability fails with -EINVAL. Once it is +enabled, KVM_CREATE_IRQCHIP fails as well. + +This capability must be enabled before any vCPU or extra plane has been +created, and the chosen count cannot be changed afterwards; otherwise +-EEXIST is returned. + 8. Other capabilities. ====================== diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index a4912735ad3d..d4628ab99197 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1193,6 +1193,7 @@ struct kvm_arch { bool has_protected_eoi; bool has_protected_pmu; bool pre_fault_allowed; + unsigned int user_max_planes; struct hlist_head *mmu_page_hash; struct list_head active_mmu_pages; struct kvm_possible_nx_huge_pages possible_nx_huge_pages[KVM_NR_MMU_TYPES]; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 29beb5791c15..79ea075eceec 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -320,11 +320,29 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_x86_default_max_planes); unsigned kvm_arch_max_planes(struct kvm *kvm) { + unsigned int vendor_planes; + /* For now, planes are only supported with irqchip=split */ if (!irqchip_split(kvm)) return 1; - return kvm_x86_call(max_planes)(kvm); + vendor_planes = kvm_x86_call(max_planes)(kvm); + + /* + * When the plane count is guest-architectural (SEV-SNP VMPLs), + * the vendor module owns it. Otherwise planes are a software + * construct and only exist when userspace has opted in to the + * number of planes it is going to manage, via KVM_CAP_PLANES. + */ + if (vendor_planes > 1) + return vendor_planes; + + /* + * Lockless readers (KVM_CHECK_EXTENSION) race the KVM_ENABLE_CAP + * store; the value is set once, so a torn-free read is all that + * is needed. + */ + return READ_ONCE(kvm->arch.user_max_planes) ? : 1; } struct kvm_plane *x86_alloc_plane(void) @@ -4146,6 +4164,38 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, mutex_unlock(&kvm->lock); break; } + case KVM_CAP_PLANES: { + mutex_lock(&kvm->lock); + r = -EINVAL; + if (cap->args[0] < 1 || cap->args[0] > KVM_MAX_USER_PLANES) + goto planes_unlock; + /* Planes are only supported with a split irqchip. */ + if (!irqchip_split(kvm)) + goto planes_unlock; + /* + * Only ordinary VMs for now. SEV-SNP VMs get their plane + * count from the architectural VMPL support, and VM types + * with protected state need per-plane audits first. + */ + if (kvm->arch.vm_type != KVM_X86_DEFAULT_VM) + goto planes_unlock; + /* + * Every plane builds its own MMU roots. With shadow paging + * each one would also write-track the guest page tables, and + * the per-gfn write-track count is only 16 bits wide. + */ + if (cap->args[0] > 1 && !tdp_enabled) + goto planes_unlock; + r = -EEXIST; + if (kvm->created_vcpus || kvm->has_planes || + kvm->arch.user_max_planes) + goto planes_unlock; + WRITE_ONCE(kvm->arch.user_max_planes, cap->args[0]); + r = 0; +planes_unlock: + mutex_unlock(&kvm->lock); + break; + } case KVM_CAP_X2APIC_API: r = -EINVAL; if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) @@ -4594,7 +4644,8 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg) goto create_irqchip_unlock; r = -EINVAL; - if (kvm->created_vcpus || kvm->has_planes) + if (kvm->created_vcpus || kvm->has_planes || + kvm->arch.user_max_planes) goto create_irqchip_unlock; r = kvm_pic_init(kvm); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 6922e3063ad8..562489ab444d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1195,6 +1195,18 @@ void kvm_unlock_all_vcpus(struct kvm *kvm); void vcpu_load(struct kvm_vcpu *vcpu); void vcpu_put(struct kvm_vcpu *vcpu); +/* + * The most planes userspace may enable with KVM_ENABLE_CAP(KVM_CAP_PLANES). + * KVM_MAX_PLANES (16, the Hyper-V VTL limit) stays the bound for plane ids + * and per-plane arrays. The opt-in is lower because on x86 each plane can + * shadow a gfn with its own upper-level shadow pages: the per-gfn count of + * those is a bit less than 2^14 per plane (see union kvm_mmu_page_role), + * and the per-gfn write-track count is 16 bits wide, so four planes is the + * most that keeps it from overflowing. The architecture defines 4 SEV-SNP + * VMPLs, so 4 is also the most planes any x86 VM can have. + */ +#define KVM_MAX_USER_PLANES 4 + unsigned kvm_arch_max_planes(struct kvm *kvm); struct kvm_plane *kvm_alloc_plane(void); void kvm_free_plane(struct kvm_plane *plane); diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 62d33f24dd7f..75ceab2d4838 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -5438,10 +5438,11 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) if (kvm) return kvm_arch_max_planes(kvm); /* - * The system fd reports the ceiling a VM may be given; a VM - * fd reports what the VM actually has. + * The system fd reports the ceiling a VM may opt in to via + * KVM_ENABLE_CAP(KVM_CAP_PLANES); a VM fd reports what the + * VM actually has. */ - return KVM_MAX_PLANES; + return KVM_MAX_USER_PLANES; default: break; } @@ -5656,8 +5657,8 @@ static int kvm_vm_ioctl_create_plane(struct kvm *kvm, unsigned id) guard(mutex)(&kvm->lock); /* - * Check the limit under kvm->lock, which KVM_CAP_SPLIT_IRQCHIP (and - * later the KVM_CAP_PLANES opt-in) take when they raise the limit. + * Check the limit under kvm->lock, which KVM_CAP_SPLIT_IRQCHIP and + * the KVM_CAP_PLANES opt-in take when they raise the limit. */ if (id >= kvm_arch_max_planes(kvm) || WARN_ON_ONCE(id >= KVM_MAX_PLANES)) -- 2.50.1 From: Paolo Bonzini Add plane and plane-vCPU wrappers to the selftest library and two tests: plane_test (plane and plane vCPU creation, sibling rules, register state isolation) and plane_x86_test (per-plane FPU, MSRs and LAPIC; MSI delivery to a plane). Planes require a split IRQ chip, so the tests create their VM with vm_create_barebones_irqchip(true). The code is from the planes RFC's selftest work, already adapted to the KVM_CREATE_VCPU on a plane fd shape of the posted series. Signed-off-by: Paolo Bonzini [hmushi: ported onto the posted series on kvm/next] Signed-off-by: Mushahid Hussain [dwmw2: ported to this stack's uAPI: KVM_CAP_PLANES is a VM-level opt-in taking the plane count and must precede vCPU creation, so the tests enable it with 2 planes right after VM creation. The main() probe uses a split-irqchip VM, because kvm_arch_max_planes() reports 1 without one, and skips when the opt-in is refused. Two subtest groups of the later planes series are not carried: the foreign-thread EBUSY test (this kernel does not restrict vCPU ioctls to the vCPU's own thread) and the userspace plane-switch group (this kernel drives plane switches through the Hyper-V VSM hypercalls). vm_create_irqchip() already falls back to a split irqchip when KVM_CREATE_IRQCHIP fails with ENOTTY. The seven remaining subtests (two in plane_test, five in plane_x86_test) pass on this kernel; the MP_STATE one restores the plane vCPU's LAPIC before its INIT MSI, because a plane's APIC map is built from APIC state writes.] (re-done by hand from Mushahid's port) [dwmw2: rebased onto 7.4-rc2: context only in selftests (gmem_ioctl(), vm_types_test, kvm_free_fd())] [dwmw2: review fixes: u32 ids, one KVM_SET_MSRS helper for vCPU and plane vCPU fds, an asserting plane_vcpu_set_msr() for the APICBASE write, apic.h's APIC_SPIV/APIC_IRR, static test functions, no second vcpu_set_cpuid() after vcpu_init_cpuid(), and corrected comments and assertion messages; a u32 plane id for vm_plane_add(), a plane_vcpu_get_msr() helper sharing one KVM_GET_MSRS helper with vcpu_get_msr(), the durable-MSR read-back in plane_vcpu_set_msr(), and a TSC check that orders three reads on one pCPU instead of bounding the elapsed time] Signed-off-by: David Woodhouse --- tools/testing/selftests/kvm/Makefile.kvm | 2 + .../testing/selftests/kvm/include/kvm_util.h | 73 ++++ .../selftests/kvm/include/x86/processor.h | 14 + tools/testing/selftests/kvm/lib/kvm_util.c | 74 +++- .../testing/selftests/kvm/lib/x86/processor.c | 47 ++- tools/testing/selftests/kvm/plane_test.c | 137 ++++++++ .../selftests/kvm/x86/plane_x86_test.c | 331 ++++++++++++++++++ 7 files changed, 667 insertions(+), 11 deletions(-) create mode 100644 tools/testing/selftests/kvm/plane_test.c create mode 100644 tools/testing/selftests/kvm/x86/plane_x86_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index 17a4256e9081..cdc6039d32d2 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -72,6 +72,7 @@ TEST_GEN_PROGS_COMMON += set_memory_region_test TEST_GEN_PROGS_COMMON += memslot_modification_stress_test TEST_GEN_PROGS_COMMON += memslot_perf_test TEST_GEN_PROGS_COMMON += vm_types_test +TEST_GEN_PROGS_COMMON += plane_test # Compiled test targets TEST_GEN_PROGS_x86 = $(TEST_GEN_PROGS_COMMON) @@ -109,6 +110,7 @@ TEST_GEN_PROGS_x86 += x86/nested_tsc_scaling_test TEST_GEN_PROGS_x86 += x86/nested_vmsave_vmload_test TEST_GEN_PROGS_x86 += x86/nested_x2apic_test TEST_GEN_PROGS_x86 += x86/platform_info_test +TEST_GEN_PROGS_x86 += x86/plane_x86_test TEST_GEN_PROGS_x86 += x86/pmu_counters_test TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index e2618877619e..3b8ad6caf757 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -77,6 +77,21 @@ struct kvm_vcpu { u32 dirty_gfns_count; }; +struct kvm_plane { + struct list_head list; + u32 id; + int fd; + struct kvm_vm *vm; +}; + +struct kvm_plane_vcpu { + struct list_head list; + u32 id; + int fd; + struct kvm_vcpu *plane0; + struct kvm_plane *plane; +}; + struct userspace_mem_regions { struct rb_root gpa_tree; struct rb_root hva_tree; @@ -144,10 +159,13 @@ struct kvm_vm { unsigned int va_bits; u64 max_gfn; struct list_head vcpus; + struct list_head planes; + struct list_head plane_vcpus; struct userspace_mem_regions regions; struct sparsebit *vpages_valid; struct sparsebit *vpages_mapped; bool has_irqchip; + bool has_split_irqchip; gpa_t ucall_mmio_addr; gva_t handlers; u32 dirty_ring_size; @@ -419,6 +437,21 @@ do { \ __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, vm); \ }) +static __always_inline void static_assert_is_plane(struct kvm_plane *plane) { } + +#define __plane_ioctl(plane, cmd, arg) \ +({ \ + static_assert_is_plane(plane); \ + kvm_do_ioctl((plane)->fd, cmd, arg); \ +}) + +#define plane_ioctl(plane, cmd, arg) \ +({ \ + int ret = __plane_ioctl(plane, cmd, arg); \ + \ + __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (plane)->vm); \ +}) + static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } #define __vcpu_ioctl(vcpu, cmd, arg) \ @@ -444,6 +477,21 @@ static __always_inline void static_assert_is_vcpu(struct kvm_vcpu *vcpu) { } TEST_ASSERT(!ret, __KVM_IOCTL_ERROR(#cmd, ret)); \ }) +static __always_inline void static_assert_is_plane_vcpu(struct kvm_plane_vcpu *plane_vcpu) { } + +#define __plane_vcpu_ioctl(plane_vcpu, cmd, arg) \ +({ \ + static_assert_is_plane_vcpu(plane_vcpu); \ + kvm_do_ioctl((plane_vcpu)->fd, cmd, arg); \ +}) + +#define plane_vcpu_ioctl(plane_vcpu, cmd, arg) \ +({ \ + int ret = __plane_vcpu_ioctl(plane_vcpu, cmd, arg); \ + \ + __TEST_ASSERT_VM_VCPU_IOCTL(!ret, #cmd, ret, (plane_vcpu)->plane0->vm); \ +}) + /* * Looks up and returns the value corresponding to the capability * (KVM_CAP_*) given by cap. @@ -809,6 +857,17 @@ static inline bool is_smt_on(void) void vm_create_irqchip(struct kvm_vm *vm); +/* + * Record the irqchip mode on the VM so that kvm_vm_restart() recreates + * the same mode instead of trying KVM_CREATE_IRQCHIP again first. + */ +static inline void vm_enable_split_irqchip(struct kvm_vm *vm) +{ + vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); + vm->has_irqchip = true; + vm->has_split_irqchip = true; +} + static inline int __vm_create_guest_memfd(struct kvm_vm *vm, u64 size, u64 flags) { @@ -889,6 +948,8 @@ void vm_mem_region_reload(struct kvm_vm *vm, u32 slot); void vm_mem_region_move(struct kvm_vm *vm, u32 slot, u64 new_gpa); void vm_mem_region_delete(struct kvm_vm *vm, u32 slot); struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, u32 vcpu_id); +struct kvm_plane *vm_plane_add(struct kvm_vm *vm, u32 plane_id); +struct kvm_plane_vcpu *__vm_plane_vcpu_add(struct kvm_vcpu *vcpu, struct kvm_plane *plane); void vm_populate_gva_bitmap(struct kvm_vm *vm); gva_t vm_unused_gva_gap(struct kvm_vm *vm, size_t sz, gva_t min_gva); gva_t vm_alloc(struct kvm_vm *vm, size_t sz, gva_t min_gva); @@ -1236,6 +1297,18 @@ static inline struct kvm_vm *vm_create_barebones(void) return ____vm_create(VM_SHAPE_DEFAULT); } +static inline struct kvm_vm *vm_create_barebones_irqchip(bool split) +{ + struct kvm_vm *vm = vm_create_barebones(); + + if (split) + vm_enable_split_irqchip(vm); + else + vm_create_irqchip(vm); + + return vm; +} + static inline struct kvm_vm *vm_create_barebones_type(unsigned long type) { const struct vm_shape shape = { diff --git a/tools/testing/selftests/kvm/include/x86/processor.h b/tools/testing/selftests/kvm/include/x86/processor.h index 5898a277a2e0..f48c6599732b 100644 --- a/tools/testing/selftests/kvm/include/x86/processor.h +++ b/tools/testing/selftests/kvm/include/x86/processor.h @@ -1211,7 +1211,21 @@ static inline void vcpu_clear_cpuid_feature(struct kvm_vcpu *vcpu, } u64 vcpu_get_msr(struct kvm_vcpu *vcpu, u64 msr_index); +u64 plane_vcpu_get_msr(struct kvm_plane_vcpu *plane_vcpu, u64 msr_index); int _vcpu_set_msr(struct kvm_vcpu *vcpu, u64 msr_index, u64 msr_value); +int _plane_vcpu_set_msr(struct kvm_plane_vcpu *plane_vcpu, u64 msr_index, u64 msr_value); + +#define plane_vcpu_set_msr(plane_vcpu, msr, val) \ +do { \ + u64 r, v = val; \ + \ + TEST_ASSERT_MSR(_plane_vcpu_set_msr(plane_vcpu, msr, v) == 1, \ + "KVM_SET_MSRS failed on %s, value = 0x%lx", msr, #msr, v); \ + if (!is_durable_msr(msr)) \ + break; \ + r = plane_vcpu_get_msr(plane_vcpu, msr); \ + TEST_ASSERT_MSR(r == v, "Set %s to '0x%lx', got back '0x%lx'", msr, #msr, v, r);\ +} while (0) /* * Assert on an MSR access(es) and pretty print the MSR name when possible. diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index d42e201eba76..7e90f3b619ba 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -302,6 +302,8 @@ struct kvm_vm *____vm_create(struct vm_shape shape) TEST_ASSERT(vm != NULL, "Insufficient Memory"); INIT_LIST_HEAD(&vm->vcpus); + INIT_LIST_HEAD(&vm->planes); + INIT_LIST_HEAD(&vm->plane_vcpus); vm->regions.gpa_tree = RB_ROOT; vm->regions.hva_tree = RB_ROOT; hash_init(vm->regions.slot_hash); @@ -632,7 +634,9 @@ void kvm_vm_restart(struct kvm_vm *vmp) struct userspace_mem_region *region; vm_open(vmp); - if (vmp->has_irqchip) + if (vmp->has_split_irqchip) + vm_enable_split_irqchip(vmp); + else if (vmp->has_irqchip) vm_create_irqchip(vmp); hash_for_each(vmp->regions.slot_hash, ctr, region, slot_node) { @@ -864,11 +868,25 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu) void kvm_vm_release(struct kvm_vm *vmp) { + struct kvm_plane_vcpu *plane_vcpu, *tmp_plane_vcpu; + struct kvm_plane *plane, *tmp_plane; struct kvm_vcpu *vcpu, *tmp; + list_for_each_entry_safe(plane_vcpu, tmp_plane_vcpu, &vmp->plane_vcpus, list) { + kvm_close(plane_vcpu->fd); + list_del(&plane_vcpu->list); + free(plane_vcpu); + } + list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list) vm_vcpu_rm(vmp, vcpu); + list_for_each_entry_safe(plane, tmp_plane, &vmp->planes, list) { + kvm_close(plane->fd); + list_del(&plane->list); + free(plane); + } + kvm_free_fd(vmp->fd); kvm_free_fd(vmp->kvm_fd); @@ -1457,6 +1475,51 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, u32 vcpu_id) return vcpu; } +/* + * Creates the plane with the ID given by plane_id in the VM specified by vm. + * No additional setup is done. Returns the plane. + */ +struct kvm_plane *vm_plane_add(struct kvm_vm *vm, u32 plane_id) +{ + struct kvm_plane *plane; + + plane = calloc(1, sizeof(*plane)); + TEST_ASSERT(plane != NULL, "Insufficient Memory"); + + plane->fd = __vm_ioctl(vm, KVM_CREATE_PLANE, (void *)(unsigned long)plane_id); + TEST_ASSERT_VM_VCPU_IOCTL(plane->fd >= 0, KVM_CREATE_PLANE, plane->fd, vm); + plane->vm = vm; + plane->id = plane_id; + + /* Add to linked-list of planes. */ + list_add(&plane->list, &vm->planes); + + return plane; +} + +/* + * Adds @vcpu's sibling in @plane. No additional setup is done. Returns the + * plane vCPU. + */ +struct kvm_plane_vcpu *__vm_plane_vcpu_add(struct kvm_vcpu *vcpu, struct kvm_plane *plane) +{ + struct kvm_plane_vcpu *plane_vcpu; + + plane_vcpu = calloc(1, sizeof(*plane_vcpu)); + TEST_ASSERT(plane_vcpu != NULL, "Insufficient Memory"); + + plane_vcpu->fd = __plane_ioctl(plane, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu->id); + TEST_ASSERT_VM_VCPU_IOCTL(plane_vcpu->fd >= 0, KVM_CREATE_VCPU, plane_vcpu->fd, plane->vm); + plane_vcpu->id = vcpu->id; + plane_vcpu->plane0 = vcpu; + plane_vcpu->plane = plane; + + /* Add to linked-list of extra-plane VCPUs. */ + list_add(&plane_vcpu->list, &plane->vm->plane_vcpus); + + return plane_vcpu; +} + /* * Within the VM specified by @vm, locates the lowest starting guest virtual * address >= @min_gva, that has at least @sz unallocated bytes. A @@ -1750,11 +1813,12 @@ void vm_create_irqchip(struct kvm_vm *vm) * support for KVM_CREATE_IRQCHIP). */ r = __vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL); - if (r && errno == ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)) - vm_enable_cap(vm, KVM_CAP_SPLIT_IRQCHIP, 24); - else - TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm); + if (r && errno == ENOTTY && kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)) { + vm_enable_split_irqchip(vm); + return; + } + TEST_ASSERT_VM_VCPU_IOCTL(!r, KVM_CREATE_IRQCHIP, r, vm); vm->has_irqchip = true; } diff --git a/tools/testing/selftests/kvm/lib/x86/processor.c b/tools/testing/selftests/kvm/lib/x86/processor.c index 8374afd08baf..76b98fcb340d 100644 --- a/tools/testing/selftests/kvm/lib/x86/processor.c +++ b/tools/testing/selftests/kvm/lib/x86/processor.c @@ -1083,34 +1083,69 @@ void vcpu_set_or_clear_cpuid_feature(struct kvm_vcpu *vcpu, vcpu_set_cpuid(vcpu); } -u64 vcpu_get_msr(struct kvm_vcpu *vcpu, u64 msr_index) +/* KVM_GET_MSRS of a single MSR on a vCPU or plane vCPU fd. */ +static int __get_one_msr(int fd, u64 msr_index, u64 *data) { struct { struct kvm_msrs header; struct kvm_msr_entry entry; } buffer = {}; + int r; buffer.header.nmsrs = 1; buffer.entry.index = msr_index; - vcpu_msrs_get(vcpu, &buffer.header); + r = kvm_do_ioctl(fd, KVM_GET_MSRS, &buffer.header); + *data = buffer.entry.data; - return buffer.entry.data; + return r; } -int _vcpu_set_msr(struct kvm_vcpu *vcpu, u64 msr_index, u64 msr_value) +u64 vcpu_get_msr(struct kvm_vcpu *vcpu, u64 msr_index) +{ + u64 data; + int r = __get_one_msr(vcpu->fd, msr_index, &data); + + TEST_ASSERT(r == 1, "KVM_GET_MSRS failed, r: %i (failed on MSR %lx)", + r, msr_index); + + return data; +} + +u64 plane_vcpu_get_msr(struct kvm_plane_vcpu *plane_vcpu, u64 msr_index) +{ + u64 data; + int r = __get_one_msr(plane_vcpu->fd, msr_index, &data); + + TEST_ASSERT(r == 1, "KVM_GET_MSRS(0x%lx) on a plane vCPU: ret %d errno %d", + msr_index, r, errno); + + return data; +} + +/* KVM_SET_MSRS of a single MSR on a vCPU or plane vCPU fd. */ +static int __set_one_msr(int fd, u64 msr_index, u64 msr_value) { struct { struct kvm_msrs header; struct kvm_msr_entry entry; } buffer = {}; - memset(&buffer, 0, sizeof(buffer)); buffer.header.nmsrs = 1; buffer.entry.index = msr_index; buffer.entry.data = msr_value; - return __vcpu_ioctl(vcpu, KVM_SET_MSRS, &buffer.header); + return kvm_do_ioctl(fd, KVM_SET_MSRS, &buffer.header); +} + +int _vcpu_set_msr(struct kvm_vcpu *vcpu, u64 msr_index, u64 msr_value) +{ + return __set_one_msr(vcpu->fd, msr_index, msr_value); +} + +int _plane_vcpu_set_msr(struct kvm_plane_vcpu *plane_vcpu, u64 msr_index, u64 msr_value) +{ + return __set_one_msr(plane_vcpu->fd, msr_index, msr_value); } void vcpu_args_set(struct kvm_vcpu *vcpu, unsigned int num, ...) diff --git a/tools/testing/selftests/kvm/plane_test.c b/tools/testing/selftests/kvm/plane_test.c new file mode 100644 index 000000000000..9464552b8d47 --- /dev/null +++ b/tools/testing/selftests/kvm/plane_test.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Red Hat, Inc. + * + * Test for architecture-neutral VM plane functionality + */ +#include +#include +#include +#include + +#include "test_util.h" + +#include "kvm_util.h" +#include "asm/kvm.h" +#include "linux/kvm.h" + +/* + * Port note (dwmw stack): KVM_CAP_PLANES is a VM-level opt-in that takes + * the plane count and must precede vCPU creation; planes are not + * always-on. The upstream planes series' foreign-thread EBUSY subtest is + * omitted: this kernel does not restrict vCPU ioctls to the vCPU's own + * thread. The assertions below are otherwise unchanged. + */ +#define NR_PLANES 2 + +static void enable_planes(struct kvm_vm *vm) +{ + vm_enable_cap(vm, KVM_CAP_PLANES, NR_PLANES); +} + +static void test_create_plane_errors(int max_planes) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + int planefd, plane_vcpufd; + + /* Planes require an in-kernel (split) IRQ chip. */ + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + vcpu = __vm_vcpu_add(vm, 0); + + planefd = __vm_ioctl(vm, KVM_CREATE_PLANE, (void *)(unsigned long)0); + TEST_ASSERT(planefd == -1 && errno == EEXIST, + "Creating existing plane, expecting EEXIST. ret: %d, errno: %d", + planefd, errno); + + planefd = __vm_ioctl(vm, KVM_CREATE_PLANE, (void *)(unsigned long)max_planes); + TEST_ASSERT(planefd == -1 && errno == EINVAL, + "Creating plane %d, expecting EINVAL. ret: %d, errno: %d", + max_planes, planefd, errno); + + plane_vcpufd = __vm_ioctl(vm, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu->id); + TEST_ASSERT(plane_vcpufd == -1 && errno == EEXIST, + "Creating existing vCPU for plane 0, expecting EEXIST. ret: %d, errno: %d", + plane_vcpufd, errno); + + kvm_vm_free(vm); + ksft_test_result_pass("error conditions\n"); +} + +static void test_create_plane(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct kvm_plane *plane; + int r; + + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + vcpu = __vm_vcpu_add(vm, 0); + + plane = vm_plane_add(vm, 1); + + /* A plane fd takes plane ioctls only; capabilities are a VM matter. */ + r = __plane_ioctl(plane, KVM_CHECK_EXTENSION, (void *)(unsigned long)KVM_CAP_PLANES); + TEST_ASSERT(r == -1 && errno == ENOTTY, + "KVM_CHECK_EXTENSION on a plane fd, expecting ENOTTY. ret: %d, errno: %d", + r, errno); + + r = __vm_ioctl(vm, KVM_CREATE_PLANE, (void *)(unsigned long)1); + TEST_ASSERT(r == -1 && errno == EEXIST, + "Creating existing plane, expecting EEXIST. ret: %d, errno: %d", + r, errno); + + __vm_plane_vcpu_add(vcpu, plane); + + r = __plane_ioctl(plane, KVM_CREATE_VCPU, (void *)(unsigned long)vcpu->id); + TEST_ASSERT(r == -1 && errno == EEXIST, + "Creating vCPU again for plane 1. ret: %d, errno: %d", + r, errno); + + r = __plane_ioctl(plane, KVM_RUN, (void *)(unsigned long)0); + TEST_ASSERT(r == -1 && errno == ENOTTY, + "KVM_RUN on a plane fd, expecting ENOTTY. ret: %d, errno: %d", + r, errno); + + kvm_vm_free(vm); + ksft_test_result_pass("basic planefd and plane_vcpufd operation\n"); +} + +int main(int argc, char *argv[]) +{ + struct kvm_vm *vm; + int cap_planes = 0; + + /* + * Planes require a split irqchip, and the opt-in fails without + * one, so the probe VM gets one first. On architectures without + * KVM_CAP_SPLIT_IRQCHIP the vm_enable_cap() inside + * vm_create_barebones_irqchip() would assert, turning the missing + * feature into a failure instead of a skip. + */ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)); + + vm = vm_create_barebones_irqchip(true); + + if (!__vm_enable_cap(vm, KVM_CAP_PLANES, NR_PLANES)) + cap_planes = vm_check_cap(vm, KVM_CAP_PLANES); + + kvm_vm_free(vm); + TEST_REQUIRE(cap_planes); + + ksft_print_header(); + ksft_set_plan(2); + + pr_info("# KVM_CAP_PLANES: %d\n", cap_planes); + + test_create_plane_errors(cap_planes); + + if (cap_planes > 1) + test_create_plane(); + else + ksft_test_result_skip("plane creation requires KVM_CAP_PLANES > 1\n"); + + ksft_finished(); +} diff --git a/tools/testing/selftests/kvm/x86/plane_x86_test.c b/tools/testing/selftests/kvm/x86/plane_x86_test.c new file mode 100644 index 000000000000..5b9e26a4337b --- /dev/null +++ b/tools/testing/selftests/kvm/x86/plane_x86_test.c @@ -0,0 +1,331 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2025 Red Hat, Inc. + * + * Test for x86-specific VM plane functionality + */ +#include +#include +#include +#include +#include +#include + +#include "test_util.h" + +#include "kvm_util.h" +#include "processor.h" +#include "apic.h" +#include "asm/kvm.h" +#include "linux/kvm.h" + +/* + * Port note (dwmw stack): KVM_CAP_PLANES is a VM-level opt-in that takes + * the plane count and must precede vCPU creation; planes are not + * always-on. The KVM_CAP_PLANES_USERSPACE_SWITCH subtests of the + * upstream planes series (userspace plane switch, lower/not-runnable + * interrupt events, CPUID runtime bits, hugepage mixed attributes) are + * omitted: this kernel drives plane switches through the Hyper-V VSM + * hypercalls and has no userspace-switch capability. The remaining + * subtests' assertions are unchanged. + */ +#define NR_PLANES 2 + +static void enable_planes(struct kvm_vm *vm) +{ + vm_enable_cap(vm, KVM_CAP_PLANES, NR_PLANES); +} + +static void test_plane_regs(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct kvm_plane *plane; + struct kvm_plane_vcpu *plane_vcpu; + + struct kvm_regs regs0, regs1; + + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + vcpu = __vm_vcpu_add(vm, 0); + plane = vm_plane_add(vm, 1); + plane_vcpu = __vm_plane_vcpu_add(vcpu, plane); + + vcpu_ioctl(vcpu, KVM_GET_REGS, ®s0); + plane_vcpu_ioctl(plane_vcpu, KVM_GET_REGS, ®s1); + regs0.rax = 0x12345678; + regs1.rax = 0x87654321; + + vcpu_ioctl(vcpu, KVM_SET_REGS, ®s0); + plane_vcpu_ioctl(plane_vcpu, KVM_SET_REGS, ®s1); + + vcpu_ioctl(vcpu, KVM_GET_REGS, ®s0); + plane_vcpu_ioctl(plane_vcpu, KVM_GET_REGS, ®s1); + TEST_ASSERT_EQ(regs0.rax, 0x12345678); + TEST_ASSERT_EQ(regs1.rax, 0x87654321); + + kvm_vm_free(vm); + ksft_test_result_pass("get/set regs for planes\n"); +} + +/* + * The planes of a VP share one TSC: a plane vCPU created later must take the + * TSC offset of its sibling, not start a new one. Read plane 0, plane 1 and + * plane 0 again from one pCPU: with a shared offset the plane 1 value falls + * between the other two however long the reads take. + */ +static void test_plane_tsc_offset(void) +{ + struct kvm_plane_vcpu *plane_vcpu; + struct kvm_plane *plane; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + u64 tsc0, tsc1, tsc2; + cpu_set_t saved; + + TEST_ASSERT(!sched_getaffinity(0, sizeof(saved), &saved), + "sched_getaffinity failed: errno %d", errno); + pin_self_to_cpu(sched_getcpu()); + + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + vcpu = __vm_vcpu_add(vm, 0); + plane = vm_plane_add(vm, 1); + plane_vcpu = __vm_plane_vcpu_add(vcpu, plane); + + tsc0 = vcpu_get_msr(vcpu, MSR_IA32_TSC); + tsc1 = plane_vcpu_get_msr(plane_vcpu, MSR_IA32_TSC); + tsc2 = vcpu_get_msr(vcpu, MSR_IA32_TSC); + TEST_ASSERT(tsc0 <= tsc1 && tsc1 <= tsc2, + "plane 1 TSC 0x%lx is not between plane 0 TSC reads 0x%lx and 0x%lx", + tsc1, tsc0, tsc2); + + kvm_vm_free(vm); + TEST_ASSERT(!sched_setaffinity(0, sizeof(saved), &saved), + "sched_setaffinity failed: errno %d", errno); + ksft_test_result_pass("plane vCPU shares the TSC offset\n"); +} + +/* + * u32 indexes into struct kvm_xsave's region[]: XSTATE_BV is the first + * field of the XSAVE header at byte 0x200, and XMM0 sits at byte 0xa0 of + * the legacy area. + */ +#define XSTATE_BV_OFFSET (0x200/4) +#define XMM_OFFSET (0xa0/4) + +static void test_plane_fpu_nonshared(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct kvm_plane *plane; + struct kvm_plane_vcpu *plane_vcpu; + + struct kvm_xsave xsave0, xsave1; + + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + + vcpu = __vm_vcpu_add(vm, 0); + vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); + + plane = vm_plane_add(vm, 1); + plane_vcpu = __vm_plane_vcpu_add(vcpu, plane); + + vcpu_ioctl(vcpu, KVM_GET_XSAVE, &xsave0); + xsave0.region[XSTATE_BV_OFFSET] |= XFEATURE_MASK_FP | XFEATURE_MASK_SSE; + xsave0.region[XMM_OFFSET] = 0x12345678; + vcpu_ioctl(vcpu, KVM_SET_XSAVE, &xsave0); + + plane_vcpu_ioctl(plane_vcpu, KVM_GET_XSAVE, &xsave1); + xsave1.region[XSTATE_BV_OFFSET] |= XFEATURE_MASK_FP | XFEATURE_MASK_SSE; + xsave1.region[XMM_OFFSET] = 0x87654321; + plane_vcpu_ioctl(plane_vcpu, KVM_SET_XSAVE, &xsave1); + + memset(&xsave0, 0, sizeof(xsave0)); + vcpu_ioctl(vcpu, KVM_GET_XSAVE, &xsave0); + TEST_ASSERT_EQ(xsave0.region[XMM_OFFSET], 0x12345678); + + memset(&xsave1, 0, sizeof(xsave0)); + plane_vcpu_ioctl(plane_vcpu, KVM_GET_XSAVE, &xsave1); + TEST_ASSERT_EQ(xsave1.region[XMM_OFFSET], 0x87654321); + + kvm_vm_free(vm); + ksft_test_result_pass("get/set FPU not shared across planes\n"); +} + +#define MYVEC 192 + +/* Physical destination mode: destination id in address bits 19:12. */ +#define MAKE_MSI(cpu, vector) ((struct kvm_msi){ \ + .address_lo = APIC_DEFAULT_GPA + (((cpu) & 0xff) << 12), \ + .address_hi = (cpu) & ~0xff, \ + .data = (vector), \ +}) + +static bool has_irr(struct kvm_lapic_state *apic, int vector) +{ + int word = vector >> 5; + int bit_in_word = vector & 31; + int bit = (APIC_IRR + word * 16) * CHAR_BIT + (bit_in_word & 31); + + return apic->regs[bit >> 3] & (1 << (bit & 7)); +} + +static void do_enable_lapic(struct kvm_lapic_state *apic) +{ + *(u32 *)&apic->regs[APIC_SPIV] |= APIC_SPIV_APIC_ENABLED; +} + +static void test_plane_msi(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct kvm_plane *plane; + struct kvm_plane_vcpu *plane_vcpu; + int r; + + struct kvm_msi msi = MAKE_MSI(0, MYVEC); + struct kvm_lapic_state lapic0, lapic1; + + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + + vcpu = __vm_vcpu_add(vm, 0); + vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid()); + + plane = vm_plane_add(vm, 1); + plane_vcpu = __vm_plane_vcpu_add(vcpu, plane); + + vcpu_set_msr(vcpu, MSR_IA32_APICBASE, + APIC_DEFAULT_GPA | MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE); + vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic0); + do_enable_lapic(&lapic0); + vcpu_ioctl(vcpu, KVM_SET_LAPIC, &lapic0); + + plane_vcpu_set_msr(plane_vcpu, MSR_IA32_APICBASE, + APIC_DEFAULT_GPA | MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE); + plane_vcpu_ioctl(plane_vcpu, KVM_GET_LAPIC, &lapic1); + do_enable_lapic(&lapic1); + plane_vcpu_ioctl(plane_vcpu, KVM_SET_LAPIC, &lapic1); + + /* Deliver to plane 1 (via the plane fd); it must land only in plane 1. */ + r = __plane_ioctl(plane, KVM_SIGNAL_MSI, &msi); + TEST_ASSERT(r == 1, + "Delivering interrupt to plane 1. ret: %d, errno: %d", r, errno); + + vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic0); + TEST_ASSERT(!has_irr(&lapic0, MYVEC), "Vector clear in plane 0"); + plane_vcpu_ioctl(plane_vcpu, KVM_GET_LAPIC, &lapic1); + TEST_ASSERT(has_irr(&lapic1, MYVEC), "Vector set in plane 1"); + + /* Deliver to plane 0 (via the vm fd); it must land in plane 0. */ + r = __vm_ioctl(vm, KVM_SIGNAL_MSI, &msi); + TEST_ASSERT(r == 1, + "Delivering interrupt to plane 0. ret: %d, errno: %d", r, errno); + vcpu_ioctl(vcpu, KVM_GET_LAPIC, &lapic0); + TEST_ASSERT(has_irr(&lapic0, MYVEC), "Vector set in plane 0"); + + kvm_vm_free(vm); + ksft_test_result_pass("signal MSI routed per plane\n"); +} + +/* + * MP_STATE is per plane vCPU: a plane vCPU whose id is not the BSP's starts + * uninitialized while its plane-0 sibling runs; setting it does not touch the + * sibling; an INIT signalled to the plane's LAPIC is accepted on the plane fd. + */ +static void test_plane_mp_state(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu0, *vcpu1; + struct kvm_plane *plane; + struct kvm_plane_vcpu *plane_vcpu0, *plane_vcpu1; + struct kvm_mp_state mp_state; + struct kvm_lapic_state lapic; + struct kvm_msi init_msi = MAKE_MSI(1, 0); + int r; + + vm = vm_create_barebones_irqchip(true); + enable_planes(vm); + + vcpu0 = __vm_vcpu_add(vm, 0); + vcpu_init_cpuid(vcpu0, kvm_get_supported_cpuid()); + vcpu1 = __vm_vcpu_add(vm, 1); + vcpu_init_cpuid(vcpu1, kvm_get_supported_cpuid()); + + plane = vm_plane_add(vm, 1); + plane_vcpu0 = __vm_plane_vcpu_add(vcpu0, plane); + plane_vcpu1 = __vm_plane_vcpu_add(vcpu1, plane); + + /* BSP id starts runnable in every plane, the others uninitialized. */ + plane_vcpu_ioctl(plane_vcpu0, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_RUNNABLE); + plane_vcpu_ioctl(plane_vcpu1, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED); + vcpu_ioctl(vcpu1, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED); + + /* Setting plane 1's state leaves plane 0's alone, and vice versa. */ + mp_state.mp_state = KVM_MP_STATE_RUNNABLE; + plane_vcpu_ioctl(plane_vcpu1, KVM_SET_MP_STATE, &mp_state); + vcpu_ioctl(vcpu1, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_UNINITIALIZED); + plane_vcpu_ioctl(plane_vcpu1, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_RUNNABLE); + + mp_state.mp_state = KVM_MP_STATE_HALTED; + vcpu_ioctl(vcpu1, KVM_SET_MP_STATE, &mp_state); + plane_vcpu_ioctl(plane_vcpu1, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_RUNNABLE); + + mp_state.mp_state = KVM_MP_STATE_INIT_RECEIVED; + plane_vcpu_ioctl(plane_vcpu1, KVM_SET_MP_STATE, &mp_state); + plane_vcpu_ioctl(plane_vcpu1, KVM_GET_MP_STATE, &mp_state); + TEST_ASSERT_EQ(mp_state.mp_state, KVM_MP_STATE_INIT_RECEIVED); + + /* + * The plane's APIC map is built from APIC state writes; the reset at + * vCPU creation runs before the plane vCPU joins its plane. Restore + * plane vCPU 1's LAPIC, as a VMM would, so the INIT has a destination. + */ + plane_vcpu_ioctl(plane_vcpu1, KVM_GET_LAPIC, &lapic); + plane_vcpu_ioctl(plane_vcpu1, KVM_SET_LAPIC, &lapic); + + /* An INIT to the plane's LAPIC is accepted on the plane fd. */ + init_msi.data |= APIC_DM_INIT; + r = __plane_ioctl(plane, KVM_SIGNAL_MSI, &init_msi); + TEST_ASSERT(r == 1, "INIT to plane 1 vCPU 1. ret: %d, errno: %d", r, errno); + + kvm_vm_free(vm); + ksft_test_result_pass("MP_STATE per plane vCPU\n"); +} + +int main(int argc, char *argv[]) +{ + /* + * Planes require a split irqchip, and the opt-in fails without + * one, so the probe VM gets one first. + */ + struct kvm_vm *vm = vm_create_barebones_irqchip(true); + int cap_planes = 0; + + if (!__vm_enable_cap(vm, KVM_CAP_PLANES, NR_PLANES)) + cap_planes = vm_check_cap(vm, KVM_CAP_PLANES); + + kvm_vm_free(vm); + TEST_REQUIRE(cap_planes > 1); + + ksft_print_header(); + ksft_set_plan(5); + + pr_info("# KVM_CAP_PLANES: %d\n", cap_planes); + + test_plane_regs(); + test_plane_tsc_offset(); + test_plane_fpu_nonshared(); + test_plane_msi(); + test_plane_mp_state(); + + ksft_finished(); +} -- 2.50.1 From: David Woodhouse Cover KVM_CAP_PLANES argument validation against the system fd's ceiling, its split-irqchip and ordering requirements, plane creation bounds, plane vCPU creation, and per-plane isolation of vCPU register state. Skip on hosts that cannot give a VM more than one plane, such as those without TDP. Signed-off-by: David Woodhouse --- tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/x86/planes_test.c | 137 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/planes_test.c diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index cdc6039d32d2..2926817baad0 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -111,6 +111,7 @@ TEST_GEN_PROGS_x86 += x86/nested_vmsave_vmload_test TEST_GEN_PROGS_x86 += x86/nested_x2apic_test TEST_GEN_PROGS_x86 += x86/platform_info_test TEST_GEN_PROGS_x86 += x86/plane_x86_test +TEST_GEN_PROGS_x86 += x86/planes_test TEST_GEN_PROGS_x86 += x86/pmu_counters_test TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test diff --git a/tools/testing/selftests/kvm/x86/planes_test.c b/tools/testing/selftests/kvm/x86/planes_test.c new file mode 100644 index 000000000000..f7db6cb6807d --- /dev/null +++ b/tools/testing/selftests/kvm/x86/planes_test.c @@ -0,0 +1,137 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Test the KVM_CAP_PLANES opt-in and the plane lifecycle uAPI: plane + * creation bounds, plane vCPU creation, and the independence of + * per-plane vCPU register state. + */ +#include +#include + +#include "kvm_util.h" + +/* + * Whether this host lets a VM have more than one plane: the opt-in also + * fails without two-dimensional paging, which the system fd cannot show. + */ +static bool have_multiple_planes(void) +{ + struct kvm_vm *vm = vm_create_barebones_irqchip(true); + bool ok = !__vm_enable_cap(vm, KVM_CAP_PLANES, 2); + + kvm_vm_free(vm); + return ok; +} + +static void test_enable_cap_bounds(void) +{ + int max_planes = kvm_check_cap(KVM_CAP_PLANES); + struct kvm_vm *vm; + + /* Planes require a split irqchip: without one the opt-in fails. */ + vm = vm_create_barebones(); + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 2), -1); + TEST_ASSERT_EQ(errno, EINVAL); + kvm_vm_free(vm); + + /* + * With one, the system fd's value is the most a VM may be given, + * and a full in-kernel irqchip cannot replace the split one later. + */ + vm = vm_create_barebones_irqchip(true); + + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 0), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + TEST_ASSERT(max_planes > 1 && max_planes <= KVM_MAX_PLANES, + "system fd KVM_CAP_PLANES ceiling %d", max_planes); + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, max_planes + 1), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + vm_enable_cap(vm, KVM_CAP_PLANES, max_planes); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), max_planes); + + /* ENOTTY where KVM_CREATE_IRQCHIP is compiled out (KVM_IOAPIC=n). */ + TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_CREATE_IRQCHIP, NULL), -1); + TEST_ASSERT(errno == EEXIST || errno == ENOTTY, + "KVM_CREATE_IRQCHIP after the opt-in: errno %d", errno); + + kvm_vm_free(vm); +} + +static void test_enable_cap_too_late(void) +{ + struct kvm_vm *vm = vm_create_barebones_irqchip(true); + + __vm_vcpu_add(vm, 0); + + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 2), -1); + TEST_ASSERT_EQ(errno, EEXIST); + + kvm_vm_free(vm); +} + +static void test_plane_lifecycle(void) +{ + struct kvm_vm *vm = vm_create_barebones(); + struct kvm_plane_vcpu *vcpu1; + struct kvm_regs regs0, regs1; + struct kvm_plane *plane; + struct kvm_vcpu *vcpu0; + int r; + + vm_enable_split_irqchip(vm); + + /* Without the opt-in a VM has a single plane. */ + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), 1); + TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_CREATE_PLANE, (void *)1UL), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + vm_enable_cap(vm, KVM_CAP_PLANES, 2); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), 2); + + /* The configured count is a hard bound, and reconfiguration a hard no. */ + TEST_ASSERT_EQ(__vm_ioctl(vm, KVM_CREATE_PLANE, (void *)2UL), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + plane = vm_plane_add(vm, 1); + + TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 3), -1); + TEST_ASSERT_EQ(errno, EEXIST); + + /* A plane fd takes plane ioctls only; capabilities are a VM matter. */ + r = __plane_ioctl(plane, KVM_CHECK_EXTENSION, (void *)KVM_CAP_PLANES); + TEST_ASSERT(r == -1 && errno == ENOTTY, + "KVM_CHECK_EXTENSION on a plane fd, expecting ENOTTY. ret: %d, errno: %d", + r, errno); + + vcpu0 = __vm_vcpu_add(vm, 0); + vcpu1 = __vm_plane_vcpu_add(vcpu0, plane); + + /* Register state is per plane: write plane 1, plane 0 unaffected. */ + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s1); + regs1.rbx = 0x5157ca11; /* arbitrary marker */ + plane_vcpu_ioctl(vcpu1, KVM_SET_REGS, ®s1); + + vcpu_ioctl(vcpu0, KVM_GET_REGS, ®s0); + TEST_ASSERT(regs0.rbx != 0x5157ca11, + "plane 0 RBX changed by plane 1 KVM_SET_REGS"); + + memset(®s1, 0, sizeof(regs1)); + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s1); + TEST_ASSERT_EQ(regs1.rbx, 0x5157ca11); + + kvm_vm_free(vm); +} + +int main(int argc, char *argv[]) +{ + TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES)); + TEST_REQUIRE(kvm_has_cap(KVM_CAP_SPLIT_IRQCHIP)); + TEST_REQUIRE(have_multiple_planes()); + + test_enable_cap_bounds(); + test_enable_cap_too_late(); + test_plane_lifecycle(); + + return 0; +} -- 2.50.1 From: David Woodhouse A VMM doing VSM in userspace has to switch the executing plane when the guest asks for a privilege transition, but a vCPU's plane run state is kernel-internal, set only by the SEV-SNP VMPL switch, plane-0 event delivery, and vCPU creation. A VTL call forwarded to userspace is a dead end. Add ioctls that read and write a plane vCPU's run state as three flags. KVM_PLANE_RUN_STATE_USER_HOLD is userspace policy: the plane is stopped, and only KVM_SET_PLANE_RUN_STATE clears it again. KVM_PLANE_RUN_STATE_SUSPENDED marks a plane suspended by a guest privilege transition; KVM does not set it yet, but userspace can restore it. KVM_PLANE_RUN_STATE_INACTIVE marks a non-zero plane that has not been started, which is how every such plane begins; SUSPENDED implies a started plane, so the two together are refused. A plane with no flag is runnable. KVM_RUN already executes the lowest-numbered runnable plane, so a userspace plane switch is just: clear the target's flags, hold the current plane, re-enter KVM_RUN. A held or suspended plane 0 *stays* stopped. Forcing it runnable on a pending event fits the SEV-SNP model, but under a userspace privilege model plane 0 would preempt the higher VTL on any interrupt and resume mid-hypercall with whatever completion value the shared kvm_run last held; the events stay pending until the VMM switches back. For the same reason KVM_RUN no longer falls back to a held plane 0 when nothing is runnable: it exits with KVM_EXIT_PLANE_EVENT and the new KVM_PLANE_EVENT_HELD cause, through plane 0's normal exit path, so immediate_exit and the kvm_run state userspace reads behave as for any other exit. When KVM makes a plane runnable it clears the two kernel-owned flags, SUSPENDED and INACTIVE, and never the hold. The held exit must not disturb what the shared kvm_run carries for a plane. It neither applies kvm_dirty_regs nor refreshes the kvm_valid_regs area; those belong to the plane that runs next. A completion still pending from plane 0's last exit is harder. Running it first, as any other KVM_RUN would, is right only if kvm_run still holds that exit's data, and KVM cannot tell whether it does: a VMM doing VSM holds plane 0 inside a forwarded VtlCall while VTL1 runs, VTL1's exits reuse the same union, and the completion would retire the VtlCall with VTL1's bytes. Exiting with the completion still armed would instead overwrite completion data the VMM did write. So with a completion pending, a held KVM_RUN fails with -EBUSY and leaves kvm_run alone; the completion runs when plane 0 next runs. The flags round-trip: a VMM that saves them and writes them back, for example on a migration destination, restores the same state, including a plane that was never started. Which runnable plane happens to be executing is not part of the interface. The ioctls are for planes userspace manages, so they return -EINVAL unless the VM enabled more than one plane with KVM_CAP_PLANES. That leaves SEV-SNP VMPL planes, whose count and switching the architecture defines, outside the interface, and it keeps other architectures out: only x86 consumes KVM_REQ_PLANE_RESCHED, and elsewhere a stray request bit would abort every subsequent entry attempt, leaving KVM_RUN spinning at 100% with no way for the VMM to recover the vCPU. Signed-off-by: David Woodhouse --- Documentation/virt/kvm/api.rst | 69 +++++++++++++++++++++ arch/x86/kvm/x86.c | 41 ++++++++++--- include/linux/kvm_host.h | 21 ++++++- include/uapi/linux/kvm.h | 6 ++ virt/kvm/kvm_main.c | 107 ++++++++++++++++++++++++++++++++- 5 files changed, 233 insertions(+), 11 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 25ce35d5361a..4196f3fc5718 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -427,6 +427,9 @@ to be dirty. KVM will not explicitly mark it such. Errors: ======= ============================================================== + EBUSY no plane is runnable, userspace holds plane 0, and plane 0 + still has an operation from its last exit to complete (x86, + see KVM_SET_PLANE_RUN_STATE) EINTR an unmasked signal is pending ENOEXEC the vcpu hasn't been initialized or the guest tried to execute instructions from device memory (arm64) @@ -6847,6 +6850,72 @@ in the future may use the plane id when planes are supported by hardware (as is the case for VMPLs in AMD), or if KVM supports accelerated plane switch operations (as might be the case for Hyper-V VTLs). +.. _KVM_GET_PLANE_RUN_STATE: + +4.148 KVM_GET_PLANE_RUN_STATE / KVM_SET_PLANE_RUN_STATE +-------------------------------------------------------- + +:Capability: KVM_CAP_PLANES +:Architectures: x86 +:Type: vcpu ioctl +:Parameters: pointer to a __u64 of run state flags +:Returns: 0 on success, <0 on error + +Reads or sets the run state flags of a plane's vCPU: + +- KVM_PLANE_RUN_STATE_USER_HOLD (bit 0): userspace has stopped the plane. + Only KVM_SET_PLANE_RUN_STATE sets or clears it. +- KVM_PLANE_RUN_STATE_SUSPENDED (bit 1): the plane is suspended by a + guest privilege transition, and resumes when that transition unwinds. + KVM does not set it yet; userspace can set it to restore a saved state. +- KVM_PLANE_RUN_STATE_INACTIVE (bit 2): the plane has not been started. + KVM sets it on every vCPU of a non-zero plane at creation and clears it + when the plane is made runnable. It is not accepted on plane 0. + KVM_PLANE_RUN_STATE_SUSPENDED implies the plane was started, so setting + both flags fails with -EINVAL. + +A plane with no flag set is runnable. Setting any other bit fails +with -EINVAL. Writing back the flags previously read restores the same +state, so a VMM can save and restore the run state of every plane, for +example across live migration. Which runnable plane executes at a given +moment is not part of this state. + +When a vCPU thread calls KVM_RUN, KVM executes the lowest-numbered +runnable plane, falling back to plane 0. Whenever KVM itself makes a +plane runnable, it clears KVM_PLANE_RUN_STATE_SUSPENDED and +KVM_PLANE_RUN_STATE_INACTIVE but leaves a hold alone. A VMM +implementing a software privilege model (such as Hyper-V VSM) switches +planes by clearing the target plane's flags and setting +KVM_PLANE_RUN_STATE_USER_HOLD on the current one before re-entering +KVM_RUN. Pending events for plane 0 make it runnable again on their own +unless it is held or suspended; a held plane 0 keeps its events pending +until the VMM clears the hold, so a software privilege model can keep a +higher plane running undisturbed. The ioctl is accepted on any plane's +vCPU fd, on VMs that enabled more than one plane with +KVM_ENABLE_CAP(KVM_CAP_PLANES); it fails with -EINVAL on other VMs. +Planes whose count the architecture defines, such as SEV-SNP VMPLs, are +not managed through this interface. + +If no plane is runnable and userspace holds plane 0, KVM_RUN does not +override the hold. It returns to userspace through plane 0 with exit +reason KVM_EXIT_PLANE_EVENT, cause KVM_PLANE_EVENT_HELD and plane 0; +immediate_exit still yields -EINTR, and the kvm_run fields every exit +updates, such as if_flag and apic_base, are filled in. A held KVM_RUN +leaves kvm_dirty_regs and the kvm_valid_regs register area alone: they +are applied to, and refreshed from, the plane that runs next. If plane 0 +still has an operation from its last exit to complete (see the note in +the description of KVM_EXIT_MMIO), KVM_RUN fails with -EBUSY instead, +with or without immediate_exit, and leaves exit_reason and the exit +fields alone. The operation completes when plane 0 next runs, from what +kvm_run holds then, so a VMM that ran another plane in between must put +plane 0's completion data back before releasing it. + +The run state is distinct from KVM_GET/SET_MP_STATE, which describes +the architectural state of a vCPU within its plane. + +A newly created vCPU of plane 0 has no flags set; one of a non-zero +plane has KVM_PLANE_RUN_STATE_INACTIVE. + .. _kvm_run: 5. The kvm_run structure diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 79ea075eceec..e8cefde75838 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -345,6 +345,11 @@ unsigned kvm_arch_max_planes(struct kvm *kvm) return READ_ONCE(kvm->arch.user_max_planes) ? : 1; } +bool kvm_arch_has_user_planes(struct kvm *kvm) +{ + return READ_ONCE(kvm->arch.user_max_planes) > 1; +} + struct kvm_plane *x86_alloc_plane(void) { /* For better type checking, do not return kzalloc() value directly */ @@ -8500,7 +8505,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu) kvm_x86_call(reload_vmsa)(vcpu); } - if (kvm_check_plane0_events(vcpu)) { + if (kvm_check_plane0_events(vcpu) && + !vcpu->common->vcpus[0]->plane_user_hold && + !vcpu->common->vcpus[0]->plane_suspended) { kvm_vcpu_set_plane_runnable(vcpu->common->vcpus[0]); kvm_make_request(KVM_REQ_EVENT, vcpu); @@ -9174,7 +9181,7 @@ static int kvm_x86_vcpu_pre_run(struct kvm_vcpu *vcpu) return 1; } -static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) +static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, bool held) { struct kvm_queued_exception *ex = &vcpu->arch.exception; struct kvm_run *kvm_run = vcpu->run; @@ -9191,6 +9198,25 @@ static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) kvm_load_guest_fpu(vcpu); kvm_vcpu_srcu_read_lock(vcpu); + + /* + * Userspace holds plane 0 and no plane is runnable: return to it + * without touching state in the shared kvm_run. kvm_dirty_regs and + * the sync_regs area are left for the plane that runs next. A + * pending completion of plane 0 may need bytes the exit would + * overwrite, or may find another plane's exit there, so refuse + * instead of running it or exiting. + */ + if (held) { + if (vcpu->arch.complete_userspace_io) + r = -EBUSY; + else if (!kvm_vcpu_wants_to_run(vcpu)) + r = -EINTR; + else + r = kvm_plane_held_exit(vcpu); + goto out; + } + if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { if (!kvm_vcpu_wants_to_run(vcpu)) { r = -EINTR; @@ -9292,7 +9318,8 @@ static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) out: kvm_put_guest_fpu(vcpu); - if (kvm_run->kvm_valid_regs && likely(!vcpu->arch.guest_state_protected)) + if (!held && kvm_run->kvm_valid_regs && + likely(!vcpu->arch.guest_state_protected)) store_regs(vcpu); post_kvm_run_save(vcpu); kvm_vcpu_srcu_read_unlock(vcpu); @@ -9308,14 +9335,12 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu_plane0) int ret; do { - struct kvm_vcpu *vcpu = kvm_vcpu_select_plane(vcpu_plane0); - - if (vcpu == NULL) - return -EINVAL; + bool held; + struct kvm_vcpu *vcpu = kvm_vcpu_select_plane(vcpu_plane0, &held); common->plane_switch = false; - ret = __kvm_arch_vcpu_ioctl_run(vcpu); + ret = __kvm_arch_vcpu_ioctl_run(vcpu, held); if (ret) break; } while (vcpu_plane0->common->plane_switch); diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 562489ab444d..b30982fcd3e7 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -422,6 +422,23 @@ struct kvm_vcpu { u64 plane_requests; enum kvm_vcpu_state plane_state; + /* + * KVM_PLANE_RUN_STATE_USER_HOLD: userspace policy, changed only by + * KVM_SET_PLANE_RUN_STATE. Pending plane-0 events no longer make + * a held plane 0 runnable on their own; the VMM owns the switch. + */ + bool plane_user_hold; + /* + * KVM_PLANE_RUN_STATE_SUSPENDED: the plane is suspended by a guest + * privilege transition and resumes when that transition unwinds. + */ + bool plane_suspended; + /* + * KVM_PLANE_RUN_STATE_INACTIVE: a non-zero plane that has not been + * started. Set at vCPU creation, cleared when the plane is made + * runnable. + */ + bool plane_inactive; /* S390 only */ bool valid_wakeup; @@ -463,7 +480,7 @@ struct kvm_vcpu { void kvm_vcpu_set_plane_runnable(struct kvm_vcpu *vcpu); void kvm_vcpu_set_plane_stopped(struct kvm_vcpu *vcpu); -struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu); +struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu, bool *held); static inline bool kvm_vcpu_wants_to_run(struct kvm_vcpu *vcpu) { @@ -1208,6 +1225,7 @@ void vcpu_put(struct kvm_vcpu *vcpu); #define KVM_MAX_USER_PLANES 4 unsigned kvm_arch_max_planes(struct kvm *kvm); +bool kvm_arch_has_user_planes(struct kvm *kvm); struct kvm_plane *kvm_alloc_plane(void); void kvm_free_plane(struct kvm_plane *plane); @@ -1733,6 +1751,7 @@ void kvm_arch_vcpu_unblocking(struct kvm_vcpu *vcpu); bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu); int kvm_request_create_plane(struct kvm_vcpu *vcpu, unsigned plane, u64 apic_id); +int kvm_plane_held_exit(struct kvm_vcpu *vcpu); #ifndef CONFIG_S390 void __kvm_vcpu_kick(struct kvm_vcpu *vcpu, bool wait); diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index af782b001ea8..abf023ed78c4 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -148,6 +148,7 @@ struct kvm_exit_snp_req_certs { struct kvm_plane_event_exit { #define KVM_PLANE_EVENT_CREATE_VCPU 1 +#define KVM_PLANE_EVENT_HELD 2 __u32 cause; __u32 plane; __u64 extra[8]; @@ -1372,6 +1373,11 @@ struct kvm_s390_keyop { #define KVM_HAS_DEVICE_ATTR _IOW(KVMIO, 0xe3, struct kvm_device_attr) #define KVM_CREATE_PLANE _IO(KVMIO, 0xe4) +#define KVM_GET_PLANE_RUN_STATE _IOR(KVMIO, 0xe5, __u64) +#define KVM_SET_PLANE_RUN_STATE _IOW(KVMIO, 0xe6, __u64) +#define KVM_PLANE_RUN_STATE_USER_HOLD (1ULL << 0) +#define KVM_PLANE_RUN_STATE_SUSPENDED (1ULL << 1) +#define KVM_PLANE_RUN_STATE_INACTIVE (1ULL << 2) /* * ioctls for vcpu fds diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 75ceab2d4838..6a00654490ae 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4121,6 +4121,21 @@ int kvm_request_create_plane(struct kvm_vcpu *vcpu, unsigned plane, u64 apic_id) } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_request_create_plane); +/* + * No plane is runnable and userspace holds plane 0: hand the decision back + * to the VMM instead of running a plane it holds. + */ +int kvm_plane_held_exit(struct kvm_vcpu *vcpu) +{ + vcpu->run->exit_reason = KVM_EXIT_PLANE_EVENT; + memset(&vcpu->run->plane_event, 0, sizeof(vcpu->run->plane_event)); + vcpu->run->plane_event.cause = KVM_PLANE_EVENT_HELD; + vcpu->run->plane_event.plane = 0; + + return 0; +} +EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_plane_held_exit); + #ifndef CONFIG_S390 /* * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode. @@ -4526,6 +4541,7 @@ static int kvm_plane_ioctl_create_vcpu(struct kvm_plane *plane, unsigned long id vcpu->plane = plane; vcpu->plane_level = plane->level; vcpu->plane_state = STOPPED; + vcpu->plane_inactive = plane->level > 0; vcpu->run = vcpu->common->run; kvm_vcpu_init(vcpu, kvm, id); @@ -4767,6 +4783,8 @@ static inline bool kvm_is_vcpu_plane_ioctl(unsigned ioctl) case KVM_GET_SREGS: case KVM_SET_SREGS: case KVM_TRANSLATE: + case KVM_GET_PLANE_RUN_STATE: + case KVM_SET_PLANE_RUN_STATE: return true; default: return kvm_arch_is_vcpu_plane_ioctl(ioctl); @@ -4907,6 +4925,61 @@ static long kvm_vcpu_ioctl(struct file *filp, r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs); break; } + case KVM_GET_PLANE_RUN_STATE: { + u64 flags = 0; + + r = -EINVAL; + if (!kvm_arch_has_user_planes(vcpu->kvm)) + goto out; + if (vcpu->plane_user_hold) + flags |= KVM_PLANE_RUN_STATE_USER_HOLD; + if (vcpu->plane_suspended) + flags |= KVM_PLANE_RUN_STATE_SUSPENDED; + if (vcpu->plane_inactive) + flags |= KVM_PLANE_RUN_STATE_INACTIVE; + r = -EFAULT; + if (copy_to_user(argp, &flags, sizeof(flags))) + goto out; + r = 0; + break; + } + case KVM_SET_PLANE_RUN_STATE: { + u64 flags; + + /* + * KVM_SET_PLANE_RUN_STATE raises KVM_REQ_PLANE_RESCHED, + * which only plane-aware architecture code consumes; on a + * planes-less VM the stuck request would abort every guest + * entry and spin KVM_RUN at 100%. + */ + r = -EINVAL; + if (!kvm_arch_has_user_planes(vcpu->kvm)) + goto out; + r = -EFAULT; + if (copy_from_user(&flags, argp, sizeof(flags))) + goto out; + r = -EINVAL; + if (flags & ~(KVM_PLANE_RUN_STATE_USER_HOLD | + KVM_PLANE_RUN_STATE_SUSPENDED | + KVM_PLANE_RUN_STATE_INACTIVE)) + goto out; + /* Plane 0 runs from creation; it is never inactive. */ + if (!vcpu->plane_level && (flags & KVM_PLANE_RUN_STATE_INACTIVE)) + goto out; + /* A plane that was never started cannot be suspended in a switch. */ + if ((flags & KVM_PLANE_RUN_STATE_INACTIVE) && + (flags & KVM_PLANE_RUN_STATE_SUSPENDED)) + goto out; + vcpu->plane_user_hold = flags & KVM_PLANE_RUN_STATE_USER_HOLD; + vcpu->plane_suspended = flags & KVM_PLANE_RUN_STATE_SUSPENDED; + vcpu->plane_inactive = flags & KVM_PLANE_RUN_STATE_INACTIVE; + if (flags) + kvm_vcpu_set_plane_stopped(vcpu); + else + kvm_vcpu_set_plane_runnable(vcpu); + r = 0; + break; + } case KVM_GET_MP_STATE: { struct kvm_mp_state mp_state; @@ -5149,8 +5222,21 @@ static struct file_operations kvm_plane_fops = { KVM_COMPAT(kvm_plane_ioctl), }; +/* + * Whether userspace manages this VM's planes (KVM_GET/SET_PLANE_RUN_STATE). + * Planes whose count the architecture owns, such as SEV-SNP VMPLs, are not + * part of that interface. + */ +bool __weak kvm_arch_has_user_planes(struct kvm *kvm) +{ + return false; +} + void kvm_vcpu_set_plane_runnable(struct kvm_vcpu *vcpu) { + /* The kernel-owned flags; only KVM_SET_PLANE_RUN_STATE drops a hold. */ + vcpu->plane_inactive = false; + vcpu->plane_suspended = false; vcpu->plane_state = RUNNABLE; vcpu->common->plane_switch = true; kvm_make_request(KVM_REQ_PLANE_RESCHED, vcpu); @@ -5164,12 +5250,18 @@ void kvm_vcpu_set_plane_stopped(struct kvm_vcpu *vcpu) } EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_set_plane_stopped); -struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu) +/* + * Pick the plane KVM_RUN executes. *held is set when nothing is runnable and + * userspace holds plane 0: plane 0 is returned only to carry the exit. + */ +struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu, bool *held) { struct kvm_vcpu_common *common = vcpu->common; struct kvm_vcpu *ret = NULL; unsigned i; + *held = false; + for (i = 0; i < KVM_MAX_PLANES; i++) { if (common->vcpus[i] == NULL) continue; @@ -5181,8 +5273,19 @@ struct kvm_vcpu *kvm_vcpu_select_plane(struct kvm_vcpu *vcpu) } if (ret == NULL) { + /* + * Never force a plane 0 that userspace holds to run: the VMM + * owns releasing it, so KVM_RUN exits to it instead. This + * must not use kvm_vcpu_set_plane_runnable(), which would + * request another plane reschedule. + */ ret = common->vcpus[0]; - ret->plane_state = RUNNABLE; + if (ret->plane_user_hold) { + *held = true; + } else { + ret->plane_suspended = false; + ret->plane_state = RUNNABLE; + } } WRITE_ONCE(common->current_vcpu, ret); -- 2.50.1 From: David Woodhouse Assert that a new plane-1 vCPU starts with KVM_PLANE_RUN_STATE_INACTIVE and plane 0's with no flags, that every valid combination of KVM_PLANE_RUN_STATE_USER_HOLD, KVM_PLANE_RUN_STATE_SUSPENDED and KVM_PLANE_RUN_STATE_INACTIVE reads back as written (so a saved state restores), and that clearing them reads back as runnable. INACTIVE on plane 0, INACTIVE together with SUSPENDED and the lowest undefined bit are refused with EINVAL, and a refused SET leaves an existing hold in place. On a VM that did not enable more than one plane both ioctls fail with EINVAL. With plane 0 held and nothing runnable, KVM_RUN still honours immediate_exit with EINTR, and otherwise exits with KVM_EXIT_PLANE_EVENT and cause KVM_PLANE_EVENT_HELD, with kvm_run's if_flag and apic_base filled in as on any other exit. That exit leaves registers marked in kvm_dirty_regs unconsumed and unrefreshed, and once plane 1 is released they land in plane 1, not plane 0. If plane 0 holds a pending I/O completion, here from a real-mode OUT, a held KVM_RUN fails with EBUSY, with or without immediate_exit, leaves the KVM_EXIT_IO in kvm_run and plane 0's RIP on the OUT, and the OUT retires once plane 0 is released. Signed-off-by: David Woodhouse --- tools/testing/selftests/kvm/x86/planes_test.c | 198 +++++++++++++++++- 1 file changed, 196 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/planes_test.c b/tools/testing/selftests/kvm/x86/planes_test.c index f7db6cb6807d..968f1b099b8e 100644 --- a/tools/testing/selftests/kvm/x86/planes_test.c +++ b/tools/testing/selftests/kvm/x86/planes_test.c @@ -8,6 +8,10 @@ #include #include "kvm_util.h" +#include "processor.h" + +/* Where the run-state test puts plane 0's one instruction. */ +#define CODE_GPA 0x1000 /* * Whether this host lets a VM have more than one plane: the opt-in also @@ -61,12 +65,22 @@ static void test_enable_cap_bounds(void) static void test_enable_cap_too_late(void) { struct kvm_vm *vm = vm_create_barebones_irqchip(true); - - __vm_vcpu_add(vm, 0); + struct kvm_vcpu *vcpu = __vm_vcpu_add(vm, 0); TEST_ASSERT_EQ(__vm_enable_cap(vm, KVM_CAP_PLANES, 2), -1); TEST_ASSERT_EQ(errno, EEXIST); + /* Without more than one user-managed plane there is no run state. */ + { + u64 flags; + + TEST_ASSERT_EQ(__vcpu_ioctl(vcpu, KVM_GET_PLANE_RUN_STATE, &flags), -1); + TEST_ASSERT_EQ(errno, EINVAL); + flags = 0; + TEST_ASSERT_EQ(__vcpu_ioctl(vcpu, KVM_SET_PLANE_RUN_STATE, &flags), -1); + TEST_ASSERT_EQ(errno, EINVAL); + } + kvm_vm_free(vm); } @@ -107,6 +121,186 @@ static void test_plane_lifecycle(void) vcpu0 = __vm_vcpu_add(vm, 0); vcpu1 = __vm_plane_vcpu_add(vcpu0, plane); + /* + * A new plane-1 vCPU is inactive, and plane 0's has no flags. + * Every valid combination reads back as written, so a saved state + * restores; unknown bits, INACTIVE on plane 0 and INACTIVE together + * with SUSPENDED are refused. + */ + { + static const u64 states[] = { + KVM_PLANE_RUN_STATE_USER_HOLD, + KVM_PLANE_RUN_STATE_SUSPENDED, + KVM_PLANE_RUN_STATE_USER_HOLD | + KVM_PLANE_RUN_STATE_SUSPENDED, + KVM_PLANE_RUN_STATE_INACTIVE, + KVM_PLANE_RUN_STATE_USER_HOLD | + KVM_PLANE_RUN_STATE_INACTIVE, + 0, + }; + u64 flags = ~0ULL; + int i; + + plane_vcpu_ioctl(vcpu1, KVM_GET_PLANE_RUN_STATE, &flags); + TEST_ASSERT_EQ(flags, KVM_PLANE_RUN_STATE_INACTIVE); + flags = ~0ULL; + vcpu_ioctl(vcpu0, KVM_GET_PLANE_RUN_STATE, &flags); + TEST_ASSERT_EQ(flags, 0); + + flags = KVM_PLANE_RUN_STATE_INACTIVE; + TEST_ASSERT_EQ(__vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, + &flags), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + for (i = 0; i < ARRAY_SIZE(states); i++) { + flags = states[i]; + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags); + flags = ~0ULL; + plane_vcpu_ioctl(vcpu1, KVM_GET_PLANE_RUN_STATE, &flags); + TEST_ASSERT_EQ(flags, states[i]); + } + + /* A refused SET leaves the hold in place. */ + flags = KVM_PLANE_RUN_STATE_USER_HOLD; + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags); + + flags = KVM_PLANE_RUN_STATE_INACTIVE | KVM_PLANE_RUN_STATE_SUSPENDED; + TEST_ASSERT_EQ(__plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, + &flags), -1); + TEST_ASSERT_EQ(errno, EINVAL); + + /* The lowest bit that no flag defines. */ + flags = ~(u64)(KVM_PLANE_RUN_STATE_USER_HOLD | + KVM_PLANE_RUN_STATE_SUSPENDED | + KVM_PLANE_RUN_STATE_INACTIVE); + flags &= -flags; + TEST_ASSERT_EQ(__plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, + &flags), -1); + TEST_ASSERT_EQ(errno, EINVAL); + flags = ~0ULL; + plane_vcpu_ioctl(vcpu1, KVM_GET_PLANE_RUN_STATE, &flags); + TEST_ASSERT_EQ(flags, KVM_PLANE_RUN_STATE_USER_HOLD); + } + + /* + * Plane 1 is held from above. With plane 0 held too, nothing is + * runnable, and KVM_RUN hands that back to userspace instead of + * running the held plane 0. + */ + { + u64 flags = KVM_PLANE_RUN_STATE_USER_HOLD; + struct kvm_regs regs; + + vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags); + + /* immediate_exit yields EINTR, as on any KVM_RUN. */ + vcpu0->run->immediate_exit = 1; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EINTR); + vcpu0->run->immediate_exit = 0; + + /* The exit fills in kvm_run like any other: poison two fields. */ + vcpu0->run->if_flag = 0xff; + vcpu0->run->apic_base = 0; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0); + TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_PLANE_EVENT); + TEST_ASSERT_EQ(vcpu0->run->plane_event.cause, KVM_PLANE_EVENT_HELD); + TEST_ASSERT_EQ(vcpu0->run->plane_event.plane, 0); + TEST_ASSERT_EQ(vcpu0->run->if_flag, 0); + TEST_ASSERT_EQ(vcpu0->run->apic_base, + vcpu_get_msr(vcpu0, MSR_IA32_APICBASE)); + + /* + * Registers userspace marks dirty are for the plane that runs + * next: the held exit neither consumes nor overwrites them. + */ + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, &vcpu0->run->s.regs.regs); + vcpu0->run->s.regs.regs.rdx = 0x5ca1ab1e; /* arbitrary marker */ + vcpu0->run->kvm_valid_regs = KVM_SYNC_X86_REGS; + vcpu0->run->kvm_dirty_regs = KVM_SYNC_X86_REGS; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0); + TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_PLANE_EVENT); + TEST_ASSERT_EQ(vcpu0->run->kvm_dirty_regs, KVM_SYNC_X86_REGS); + TEST_ASSERT_EQ(vcpu0->run->s.regs.regs.rdx, 0x5ca1ab1e); + + /* + * Released, plane 1 is next and takes them; immediate_exit + * stops it before entry, after the registers are applied. + */ + flags = 0; + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags); + vcpu0->run->immediate_exit = 1; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EINTR); + vcpu0->run->immediate_exit = 0; + TEST_ASSERT_EQ(vcpu0->run->kvm_dirty_regs, 0); + vcpu0->run->kvm_valid_regs = 0; + + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s); + TEST_ASSERT_EQ(regs.rdx, 0x5ca1ab1e); + vcpu_ioctl(vcpu0, KVM_GET_REGS, ®s); + TEST_ASSERT(regs.rdx != 0x5ca1ab1e, + "plane 0 took the registers dirtied while it was held"); + + vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags); + } + + /* + * A completion plane 0 still owes its last exit stays pending while + * plane 0 is held: KVM_RUN fails with EBUSY and leaves the exit in + * kvm_run, and the completion runs once plane 0 runs again. The + * exit is a real-mode OUT at CODE_GPA, which userspace handles. + */ + { + static const u8 out_insn[] = { 0xe6, 0x10 }; /* out %al, $0x10 */ + u64 flags = KVM_PLANE_RUN_STATE_USER_HOLD; + struct kvm_sregs sregs; + struct kvm_regs regs; + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, CODE_GPA, + 1, 1, 0); + memcpy(addr_gpa2hva(vm, CODE_GPA), out_insn, sizeof(out_insn)); + + vcpu_sregs_get(vcpu0, &sregs); + sregs.cs.selector = 0; + sregs.cs.base = 0; + vcpu_sregs_set(vcpu0, &sregs); + vcpu_regs_get(vcpu0, ®s); + regs.rip = CODE_GPA; + vcpu_regs_set(vcpu0, ®s); + + /* Plane 1 stays held, so only plane 0 can run. */ + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags); + TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0); + TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO); + TEST_ASSERT_EQ(vcpu0->run->io.direction, KVM_EXIT_IO_OUT); + TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10); + + vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags); + vcpu0->run->immediate_exit = 1; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EBUSY); + vcpu0->run->immediate_exit = 0; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EBUSY); + TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO); + TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10); + vcpu_regs_get(vcpu0, ®s); + TEST_ASSERT_EQ(regs.rip, CODE_GPA); + + /* Released, plane 0 retires the OUT before immediate_exit. */ + flags = 0; + vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &flags); + vcpu0->run->immediate_exit = 1; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EINTR); + vcpu0->run->immediate_exit = 0; + vcpu_regs_get(vcpu0, ®s); + TEST_ASSERT_EQ(regs.rip, CODE_GPA + sizeof(out_insn)); + + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &flags); + } + /* Register state is per plane: write plane 1, plane 0 unaffected. */ plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s1); regs1.rbx = 0x5157ca11; /* arbitrary marker */ -- 2.50.1 From: David Woodhouse A request raised on one plane's vCPU for a VM-wide reason (TLB flush after a host-side zap, APIC page reload, obsolete-roots release) is consumed by whichever twin runs first, so the other planes keep stale translations. The plane-1 twin of a Windows VSM guest then runs on a zapped root and the guest bugchecks. Mark those three requests per-plane and make kvm_make_vcpu_request() replicate any KVM_REQUEST_PER_PLANE request to every twin of the target vCPU through vcpu_for_each_plane(). Mode and wait live in the shared common and the kick targets the running twin's pCPU, so the existing kick logic already reaches the twins. The x86 kvm_exit and powerpc kvm_check_requests tracepoints logged only the shared request word, so the three requests would vanish from them. OR in the vCPU's plane requests instead; a separate field would say which word a bit came from, but changes the tracepoint format for little gain. Signed-off-by: David Woodhouse --- arch/powerpc/kvm/trace.h | 3 ++- arch/x86/include/asm/kvm_host.h | 6 ++++-- arch/x86/kvm/mmu/mmu.c | 13 ++++++++----- arch/x86/kvm/trace.h | 3 ++- include/linux/kvm_host.h | 3 ++- virt/kvm/kvm_main.c | 20 +++++++++++++++++++- 6 files changed, 37 insertions(+), 11 deletions(-) diff --git a/arch/powerpc/kvm/trace.h b/arch/powerpc/kvm/trace.h index 35c000d918bb..2fc60a4e159f 100644 --- a/arch/powerpc/kvm/trace.h +++ b/arch/powerpc/kvm/trace.h @@ -108,7 +108,8 @@ TRACE_EVENT(kvm_check_requests, TP_fast_assign( __entry->cpu_nr = vcpu->vcpu_id; - __entry->requests = vcpu->common->requests; + __entry->requests = READ_ONCE(vcpu->common->requests) | + READ_ONCE(vcpu->plane_requests); ), TP_printk("vcpu=%x requests=%x", diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index d4628ab99197..596be8ad7154 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -107,7 +107,8 @@ KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_GLOBAL_CLOCK_UPDATE KVM_ARCH_REQ(16) #define KVM_REQ_APIC_PAGE_RELOAD \ - KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) + KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | \ + KVM_REQUEST_PER_PLANE) #define KVM_REQ_HV_CRASH KVM_ARCH_REQ(18) #define KVM_REQ_IOAPIC_EOI_EXIT KVM_ARCH_PLANE_REQ(19) #define KVM_REQ_HV_RESET KVM_ARCH_REQ(20) @@ -125,7 +126,8 @@ #define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \ KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_MMU_FREE_OBSOLETE_ROOTS \ - KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) + KVM_ARCH_REQ_FLAGS(31, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | \ + KVM_REQUEST_PER_PLANE) #define KVM_REQ_HV_TLB_FLUSH \ KVM_ARCH_REQ_FLAGS(32, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_VMSA_PAGE_RELOAD \ diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c index e8eb1e48df36..ef6df08f5a99 100644 --- a/arch/x86/kvm/mmu/mmu.c +++ b/arch/x86/kvm/mmu/mmu.c @@ -6405,11 +6405,14 @@ void kvm_mmu_free_obsolete_roots(struct kvm_vcpu *vcpu) int i; /* - * KVM_REQ_MMU_FREE_OBSOLETE_ROOTS is tracked in the request set - * shared by all of the vCPU's planes, so the plane that consumes - * the request is not necessarily the plane whose root is obsolete. - * Service every plane: the inactive twins cannot be running (all - * planes of a vCPU share one thread), so their MMUs are stable. + * KVM_REQ_MMU_FREE_OBSOLETE_ROOTS fans out to every plane twin, + * but each twin only consumes its own copy when it next runs, and + * a plane that userspace never re-enters would pin its obsolete + * roots indefinitely. Service every plane from whichever twin + * consumes its request first: the inactive twins cannot be + * running (all planes of a vCPU share one thread), so their MMUs + * are stable, and a twin's own later request finds nothing left + * to free. */ vcpu_for_each_plane(vcpu->common, i, v) { __kvm_mmu_free_obsolete_roots(vcpu->kvm, &v->arch.root_mmu); diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h index 7038e596019a..ad901836c08a 100644 --- a/arch/x86/kvm/trace.h +++ b/arch/x86/kvm/trace.h @@ -409,7 +409,8 @@ TRACE_EVENT(name, \ __entry->guest_rip = tracing_kvm_rip_read(vcpu); \ __entry->isa = isa; \ __entry->vcpu_id = vcpu->vcpu_id; \ - __entry->requests = READ_ONCE(vcpu->common->requests); \ + __entry->requests = READ_ONCE(vcpu->common->requests) | \ + READ_ONCE(vcpu->plane_requests); \ kvm_x86_call(get_exit_info)(vcpu, \ &__entry->exit_reason, \ &__entry->info1, \ diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index b30982fcd3e7..6c50c1dc5819 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -168,7 +168,8 @@ static inline bool kvm_is_error_gpa(gpa_t gpa) * Architecture-independent vcpu->requests bit members * Bits 3-7 are reserved for more arch-independent bits. */ -#define KVM_REQ_TLB_FLUSH (0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) +#define KVM_REQ_TLB_FLUSH \ + (0 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP | KVM_REQUEST_PER_PLANE) #define KVM_REQ_VM_DEAD (1 | KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP) #define KVM_REQ_UNBLOCK 2 #define KVM_REQ_DIRTY_RING_SOFT_FULL 3 diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6a00654490ae..c323d067256e 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -237,9 +237,27 @@ static void kvm_make_vcpu_request(struct kvm_vcpu *vcpu, unsigned int req, { int cpu; - if (likely(!(req & KVM_REQUEST_NO_ACTION))) + if (likely(!(req & KVM_REQUEST_NO_ACTION))) { __kvm_make_request(req, vcpu); + /* + * A per-plane request raised on "the vCPU" (TLB flush after + * a host-side zap, obsolete-roots reload, ...) concerns + * every plane: fan it out to the twins, or the plane that + * runs next consumes a bit the others still need. Mode and + * wait live in the common and the kick below targets the + * running twin's pCPU, so it covers the twins too. + */ + if (req & KVM_REQUEST_PER_PLANE) { + struct kvm_vcpu *twin; + int p; + + vcpu_for_each_plane(vcpu->common, p, twin) + if (twin != vcpu) + __kvm_make_request(req, twin); + } + } + if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu)) return; -- 2.50.1 From: David Woodhouse With APICv live on plane-0 vCPUs while their plane-1 twins use software injection, the twins trip the APICv coherency checks: stale vcpu-vs-VM activation state at vcpu_enter_guest(), and timer interrupt injection against a vCPU that is not the running one. Cross-plane event delivery depends on software-injection bookkeeping that hardware acceleration bypasses. Add APICV_INHIBIT_REASON_PLANES, set it when a plane above 0 is created, and include it in the VMX and AVIC required-inhibit masks; __kvm_set_or_clear_apicv_inhibit() silently ignores a reason the vendor mask does not carry. Drop the inhibit again when the KVM_CREATE_PLANE error path destroys the last nonzero plane of a live VM; at VM teardown plane 0 is already gone and the update is skipped. Signed-off-by: David Woodhouse --- arch/x86/kvm/svm/svm.h | 3 ++- arch/x86/kvm/vmx/main.c | 3 ++- arch/x86/kvm/x86.c | 41 +++++++++++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 8 ++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 2b7663722be6..e7fced399e05 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -982,7 +982,8 @@ extern struct kvm_x86_nested_ops svm_nested_ops; BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | \ BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \ BIT(APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED) | \ - BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) \ + BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_TOO_BIG) | \ + BIT(APICV_INHIBIT_REASON_PLANES) \ ) bool __init avic_hardware_setup(void); diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c index 2d84910df724..14d21d02bff4 100644 --- a/arch/x86/kvm/vmx/main.c +++ b/arch/x86/kvm/vmx/main.c @@ -1004,7 +1004,8 @@ static int vt_handle_exit(struct kvm_vcpu *vcpu, BIT(APICV_INHIBIT_REASON_BLOCKIRQ) | \ BIT(APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED) | \ BIT(APICV_INHIBIT_REASON_APIC_ID_MODIFIED) | \ - BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED)) + BIT(APICV_INHIBIT_REASON_APIC_BASE_MODIFIED) | \ + BIT(APICV_INHIBIT_REASON_PLANES)) struct kvm_x86_ops vt_x86_ops __initdata = { .name = KBUILD_MODNAME, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index e8cefde75838..6d3d93e85a65 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -10247,12 +10247,53 @@ int kvm_arch_plane_init(struct kvm *kvm, struct kvm_plane *plane, { mutex_init(&plane->arch.apic_map_lock); + /* + * Cross-plane event delivery (forced VTL entry, twin kicks, NMI + * folding) relies on software injection via KVM_REQ_EVENT; hardware + * APIC virtualization on one twin would bypass the bookkeeping the + * other twin's plane depends on. + */ + if (plane_level > 0) + kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PLANES); + return 0; } void kvm_arch_plane_destroy(struct kvm_plane *plane) { + struct kvm *kvm = plane->kvm; + int i; + kvfree(rcu_dereference_check(plane->arch.apic_map, 1)); + + if (plane->level == 0) + return; + + /* + * Drop the APICv inhibit taken in kvm_arch_plane_init() when the + * last nonzero plane goes away. Only the KVM_CREATE_PLANE error + * path destroys a nonzero plane while the VM is live; at VM + * teardown plane 0 is destroyed first and kvm->planes[0] is + * already NULL, which skips the update (the inhibit machinery + * needs plane 0 and SRCU, both gone by then). + */ + if (!kvm->planes[0]) + return; + + /* + * Reaching here means a nonzero plane died while the VM is live, + * which only the KVM_CREATE_PLANE error path does - under + * kvm->lock. A future kvm_destroy_planes() reordered to tear + * nonzero planes down before plane 0 would get here locklessly; + * catch that instead of silently poking freed APICv state. + */ + lockdep_assert_held(&kvm->lock); + + for (i = 1; i < KVM_MAX_PLANES; i++) + if (kvm->planes[i] && kvm->planes[i] != plane) + return; + + kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PLANES); } int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 498974d38773..fb87ceaa9563 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -580,6 +580,13 @@ enum kvm_apicv_inhibit { APICV_INHIBIT_REASON_APIC_ID_MODIFIED, APICV_INHIBIT_REASON_APIC_BASE_MODIFIED, + /* + * APICv is inhibited when the VM has multiple planes (Hyper-V VTLs). + * Cross-plane event delivery relies on software injection; hardware + * acceleration on one twin would bypass it. + */ + APICV_INHIBIT_REASON_PLANES, + /******************************************************/ /* INHIBITs that are relevant only to the AMD's AVIC. */ /******************************************************/ @@ -637,6 +644,7 @@ enum kvm_apicv_inhibit { __APICV_INHIBIT_REASON(PHYSICAL_ID_ALIASED), \ __APICV_INHIBIT_REASON(APIC_ID_MODIFIED), \ __APICV_INHIBIT_REASON(APIC_BASE_MODIFIED), \ + __APICV_INHIBIT_REASON(PLANES), \ __APICV_INHIBIT_REASON(NESTED), \ __APICV_INHIBIT_REASON(IRQWIN), \ __APICV_INHIBIT_REASON(PIT_REINJ), \ -- 2.50.1 From: David Woodhouse Plane twins share CPUID, so a twin's guest can set the XCR0 bits for a dynamic xfeature such as AMX's XTILEDATA with its own XSETBV. But kvm_check_cpuid() calls fpu_enable_guest_xfd_features() only for the vCPU that KVM_SET_CPUID was issued on, and a twin created afterwards starts with the default fpstate. A twin that then runs AMX code loses its tile data on every fpstate save, because the fpstate it saves into has no room for it. On a host that exposes AMX to the guest, any twin using AMX hits this. Whether the XRSTORS on the next entry can also leave a previous context's tile registers in place for that twin to read is an open question; it has not been verified. Enlarge the fpstate of every twin in kvm_check_cpuid(), and give a twin created after KVM_SET_CPUID the dynamic xfeatures of its plane-0 sibling's fpstate. Only a twin that has not run may have its fpstate enlarged: fpstate_realloc() hands a guest fpstate that is not loaded a fresh image and copies nothing, so a twin that has run would lose its XSAVE state. kvm_set_cpuid() already accepts only identical CPUID once the vCPU has run, but it checked only the vCPU the ioctl was issued on. With plane 0 held while a higher plane runs, KVM_SET_CPUID2 on plane 0 would pass that check and resize the plane that ran. Since the twins share CPUID, take the identical-CPUID path once any twin has run. Signed-off-by: David Woodhouse --- arch/x86/kvm/cpuid.c | 21 +++++++++++++++++++-- arch/x86/kvm/x86.c | 17 +++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index c5fa0551ddd5..971ebd781c6e 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -141,7 +141,9 @@ EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_find_cpuid_entry2); static int kvm_check_cpuid(struct kvm_vcpu *vcpu) { struct kvm_cpuid_entry2 *best; + struct kvm_vcpu *v; u64 xfeatures; + int i, r; /* * The existing code assumes virtual address is 48-bit or 57-bit in the @@ -168,7 +170,18 @@ static int kvm_check_cpuid(struct kvm_vcpu *vcpu) if (!xfeatures) return 0; - return fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, xfeatures); + /* + * The planes share CPUID, so any twin's guest can enable these + * xfeatures in its XCR0: enlarge every twin's fpstate, not just + * this vCPU's. + */ + vcpu_for_each_plane(vcpu->common, i, v) { + r = fpu_enable_guest_xfd_features(&v->arch.guest_fpu, xfeatures); + if (r) + return r; + } + + return 0; } static u32 kvm_apply_cpuid_pv_features_quirk(struct kvm_vcpu *vcpu); @@ -545,8 +558,12 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, * VMMs (e.g. QEMU) reuse vCPU fds for CPU hotplug/unplug and do * KVM_SET_CPUID{,2} again. To support this legacy behavior, check * whether the supplied CPUID data is equal to what's already set. + * Plane twins share CPUID, so this applies once any twin has run. */ - if (!kvm_can_set_cpuid_and_feature_msrs(vcpu)) { + vcpu_for_each_plane(common, i, v) { + if (kvm_can_set_cpuid_and_feature_msrs(v)) + continue; + r = kvm_cpuid_check_equal(vcpu, e2, nent); if (r) goto err; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 6d3d93e85a65..af28e73e4b9b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9771,6 +9771,23 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.hv_root_tdp = INVALID_PAGE; #endif + /* + * A plane twin created after KVM_SET_CPUID shares CPUID that may + * already allow dynamic xfeatures, which KVM_SET_CPUID enabled only + * in the twins that existed at the time. Give it the dynamic + * xfeatures of its plane-0 sibling's fpstate. + */ + if (vcpu->plane_level && vcpu->common->vcpus[0]) { + u64 dyn = vcpu->common->vcpus[0]->arch.guest_fpu.fpstate->xfeatures & + XFEATURE_MASK_USER_DYNAMIC; + + if (dyn) { + r = fpu_enable_guest_xfd_features(&vcpu->arch.guest_fpu, dyn); + if (r) + goto free_guest_fpu; + } + } + /* * A plane twin created after KVM_SET_CPUID shares CPUID that may * already advertise Hyper-V. Allocate the Hyper-V context now so -- 2.50.1 From: David Woodhouse Which per-vCPU state the planes of a vCPU share is policy for the VMM. Hyper-V VSM shares XCR0 between VTLs, and whether it shares DR6 depends on the processor, as HvRegisterVsmCapabilities.Dr6Shared reports; a VMM with a different privilege model may want neither. So far the only knob is the plane count, which leaves nothing to key such sharing on but the opt-in itself. Take a mask of state to share in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES), set once together with the count. Unknown bits, and a non-zero args[2] or args[3], fail with -EINVAL. A new KVM_CAP_PLANES_SHARE reports the supported bits on the system fd and the enabled bits on a VM fd, as KVM_CAP_PLANES reports the ceiling and the live count. No bits are defined yet; the following patches add XCR0 and DR6. Signed-off-by: David Woodhouse --- Documentation/virt/kvm/api.rst | 12 +++++++++++- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/x86.c | 8 ++++++++ arch/x86/kvm/x86.h | 11 +++++++++++ include/uapi/linux/kvm.h | 1 + 5 files changed, 32 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 4196f3fc5718..a6008cb33e99 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -9316,7 +9316,8 @@ hpage_2g module parameter is not set to 1, -EINVAL is returned. :Architectures: x86 :Target: VM -:Parameters: args[0] contains the number of planes to make available +:Parameters: args[0] contains the number of planes to make available, + args[1] a mask of state that the planes of a vCPU share Opts the VM in to using multiple planes, raising the limit on plane ids that KVM_CREATE_PLANE accepts to args[0], which must be between 1 and the @@ -9339,6 +9340,15 @@ This capability must be enabled before any vCPU or extra plane has been created, and the chosen count cannot be changed afterwards; otherwise -EEXIST is returned. +Each plane of a vCPU normally keeps its own copy of the vCPU's register +state, apart from the CPUID model, which the planes always share. args[1] +selects state that the planes of a vCPU share instead, so that a write +through one plane is seen by all of them. KVM_CHECK_EXTENSION +(KVM_CAP_PLANES_SHARE) returns the bits KVM supports on the system fd, and +the bits the VM enabled on a VM fd. Setting an unsupported bit, or a +non-zero args[2] or args[3], fails with -EINVAL. Like the count, the mask +is set once. + 8. Other capabilities. ====================== diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 596be8ad7154..99a671ac2386 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -1195,6 +1195,7 @@ struct kvm_arch { bool has_protected_eoi; bool has_protected_pmu; bool pre_fault_allowed; + u8 plane_share; unsigned int user_max_planes; struct hlist_head *mmu_page_hash; struct list_head active_mmu_pages; diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index af28e73e4b9b..4d70e111eeb0 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -2460,6 +2460,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) case KVM_CAP_VM_TSC_CONTROL: r = kvm_caps.has_tsc_control; break; + case KVM_CAP_PLANES_SHARE: + r = kvm ? READ_ONCE(kvm->arch.plane_share) : KVM_PLANE_SHARE_VALID; + break; case KVM_CAP_X2APIC_API: r = KVM_X2APIC_API_VALID_FLAGS; if (kvm && !irqchip_split(kvm)) @@ -4174,6 +4177,10 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, r = -EINVAL; if (cap->args[0] < 1 || cap->args[0] > KVM_MAX_USER_PLANES) goto planes_unlock; + if (cap->args[1] & ~KVM_PLANE_SHARE_VALID) + goto planes_unlock; + if (cap->args[2] || cap->args[3]) + goto planes_unlock; /* Planes are only supported with a split irqchip. */ if (!irqchip_split(kvm)) goto planes_unlock; @@ -4195,6 +4202,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, if (kvm->created_vcpus || kvm->has_planes || kvm->arch.user_max_planes) goto planes_unlock; + WRITE_ONCE(kvm->arch.plane_share, cap->args[1]); WRITE_ONCE(kvm->arch.user_max_planes, cap->args[0]); r = 0; planes_unlock: diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index fb87ceaa9563..2f51b476dbd1 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -923,4 +923,15 @@ struct kvm_plane *x86_alloc_plane(void); void x86_free_plane(struct kvm_plane *plane); unsigned kvm_x86_default_max_planes(struct kvm *kvm); +/* + * The per-vCPU state that userspace may ask the planes of a vCPU to share, + * in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES). + */ +#define KVM_PLANE_SHARE_VALID 0 + +static inline bool kvm_plane_shares(struct kvm *kvm, u64 state) +{ + return READ_ONCE(kvm->arch.plane_share) & state; +} + #endif diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index abf023ed78c4..aef86ef3976f 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1017,6 +1017,7 @@ struct kvm_enable_cap { #define KVM_CAP_ARM_PMU_V3_STRICT 251 #define KVM_CAP_GUEST_MEMFD_MEMORY_ATTRIBUTES 252 #define KVM_CAP_PLANES 254 +#define KVM_CAP_PLANES_SHARE 256 struct kvm_irq_routing_irqchip { __u32 irqchip; -- 2.50.1 From: David Woodhouse XCR0/XFEM is shared between VTLs (TLFS 15.4). The planes series keeps arch.xcr0 per vCPU, so an XSETBV in one VTL is invisible in the other, and VTL1 reads a stale XFEM: a feature VTL0 enabled (AVX, for the Windows secure kernel) is not enabled in VTL1's XCR0. The register contents themselves are a separate matter, carried by the code that switches VTLs. Add KVM_PLANE_SHARE_XCR0 to the KVM_CAP_PLANES share mask. With it set, propagate the validated value to every plane twin in __kvm_set_xcr(), covering both guest XSETBV and the KVM_SET_XCRS ioctl path, and let a twin created later inherit plane 0's current value. The CPUID dynamic-bits dirty marker lives in the shared common state, so the existing single set covers the twins. Update the planes section of api.rst and the kvm_cpuid_apply_runtime() comment, which still listed XCR0 among the per-plane registers. SEV-SNP VMPLs bank XCR0 per VMSA, and sharing it there would be wrong in two ways: sev_es_sync_from_ghcb() feeds a VMPL's GHCB XCR0 to __kvm_set_xcr(), so a less-privileged VMPL would overwrite its siblings' values (and the MSR-protocol CPUID leaf 0xD sizes would then follow the wrong VMPL); and a pre-launch KVM_SET_XCRS on one twin would change plane 0's VMSA, and with it the launch measurement. An SNP VM cannot set the share mask, since KVM_CAP_PLANES refuses its VM type, so its VMPLs keep XCR0 of their own by construction. The twins' fpstate sizing does not depend on this bit: the planes share CPUID, so any twin's own XSETBV can enable a dynamic xfeature whether or not XCR0 is shared. Signed-off-by: David Woodhouse --- Documentation/virt/kvm/api.rst | 11 ++++++++--- arch/x86/include/uapi/asm/kvm.h | 3 +++ arch/x86/kvm/cpuid.c | 4 +++- arch/x86/kvm/x86.c | 34 +++++++++++++++++++++++++++++++++ arch/x86/kvm/x86.h | 2 +- 5 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index a6008cb33e99..22dc3c793bc4 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -374,8 +374,9 @@ The planes of a vCPU share its CPUID model, set through the plane 0 fd with KVM_SET_CPUID2. The CPUID bits that follow a vCPU's own registers (OSXSAVE from CR4.OSXSAVE, OSPKE from CR4.PKE, the APIC enable, MWAIT from MISC_ENABLE, and the XSAVE area sizes from XCR0 and XSS) reflect the plane -that executes CPUID, since every plane has these registers of its own. -KVM_GET_CPUID2 returns them as plane 0 sees them. +that executes CPUID, since every plane has these registers of its own, +unless the VM asked the planes to share XCR0 with KVM_PLANE_SHARE_XCR0 (see +:ref:`KVM_CAP_PLANES`). KVM_GET_CPUID2 returns them as plane 0 sees them. 4.8 KVM_GET_DIRTY_LOG --------------------- @@ -9347,7 +9348,11 @@ through one plane is seen by all of them. KVM_CHECK_EXTENSION (KVM_CAP_PLANES_SHARE) returns the bits KVM supports on the system fd, and the bits the VM enabled on a VM fd. Setting an unsupported bit, or a non-zero args[2] or args[3], fails with -EINVAL. Like the count, the mask -is set once. +is set once. The bits are: + +- KVM_PLANE_SHARE_XCR0: XCR0, whether written by the guest with XSETBV or + with KVM_SET_XCRS on any plane's vCPU fd. A plane vCPU created later + starts with plane 0's value. 8. Other capabilities. ====================== diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index 00ce265391e1..edec00979acf 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -922,6 +922,9 @@ struct kvm_sev_snp_launch_finish { #define KVM_X2APIC_ENABLE_SUPPRESS_EOI_BROADCAST _BITULL(2) #define KVM_X2APIC_DISABLE_SUPPRESS_EOI_BROADCAST _BITULL(3) +/* KVM_CAP_PLANES args[1]: state the planes of a vCPU share */ +#define KVM_PLANE_SHARE_XCR0 _BITULL(0) + struct kvm_hyperv_eventfd { __u32 conn_id; __s32 fd; diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 971ebd781c6e..aa89d1b6f6c4 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -2155,7 +2155,9 @@ get_out_of_range_cpuid_entry(struct kvm_vcpu *vcpu, u32 *fn_ptr, u32 index) /* * The planes share the CPUID table but not CR4, the APIC enable, MISC_ENABLE - * or XCR0/XSS: the bits that follow those come from the executing plane. + * or XSS, nor XCR0 unless userspace asked for KVM_PLANE_SHARE_XCR0, in which + * case __kvm_set_xcr() keeps it equal across the twins: the bits that follow + * those come from the executing plane. */ static void kvm_cpuid_apply_runtime(struct kvm_vcpu *vcpu, u32 function, u32 index, u32 *eax, u32 *ebx, u32 *ecx, diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index 4d70e111eeb0..ceec10d116a9 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -799,6 +799,31 @@ int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) vcpu->arch.xcr0 = xcr0; + /* + * XCR0/XFEM is shared across VTLs (TLFS 15.4): propagate the new + * value to every plane twin so the other VTLs observe it. This + * covers both guest XSETBV and the KVM_SET_XCRS ioctl path. The + * CPUID dirty marker below lives in the shared common state, so one + * set covers the twins too. + * + * Only planes that userspace asked to share XCR0 do so. SEV-SNP + * VMPLs bank it per VMSA, and an SNP VM cannot set the share mask + * (KVM_CAP_PLANES refuses its VM type), so they never fan out. + * + * Skip the fan-out until this vCPU is published in + * common->vcpus[]: the architectural reset of a twin still being + * created must not clobber a live sibling's XCR0 (the new twin + * inherits the shared value instead, in kvm_arch_vcpu_create()). + */ + if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_XCR0) && vcpu->common && + vcpu->common->vcpus[vcpu->plane_level] == vcpu) { + struct kvm_vcpu *twin; + int i; + + vcpu_for_each_plane(vcpu->common, i, twin) + twin->arch.xcr0 = xcr0; + } + if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) cpuid_set_dirty(vcpu); return 0; @@ -9820,6 +9845,15 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) kvm_vcpu_after_set_cpuid(vcpu); kvm_set_tsc_khz(vcpu, vcpu->kvm->arch.default_tsc_khz); kvm_vcpu_reset(vcpu, false); + /* + * With XCR0 shared, a twin created after its plane-0 sibling has run + * (Hyper-V VSM enables VTL1 at runtime) inherits the current shared + * value; the architectural reset value only stands when no sibling + * exists yet, or when the planes keep XCR0 of their own. + */ + if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_XCR0) && + vcpu->plane_level && vcpu->common->vcpus[0]) + vcpu->arch.xcr0 = vcpu->common->vcpus[0]->arch.xcr0; kvm_init_mmu(vcpu); vcpu_put(vcpu); return 0; diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index 2f51b476dbd1..aa8abd219507 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -927,7 +927,7 @@ unsigned kvm_x86_default_max_planes(struct kvm *kvm); * The per-vCPU state that userspace may ask the planes of a vCPU to share, * in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES). */ -#define KVM_PLANE_SHARE_VALID 0 +#define KVM_PLANE_SHARE_VALID KVM_PLANE_SHARE_XCR0 static inline bool kvm_plane_shares(struct kvm *kvm, u64 state) { -- 2.50.1 From: David Woodhouse The TLFS leaves DR6 sharing between VTLs processor-dependent and tells the guest which way it went in HvRegisterVsmCapabilities.Dr6Shared. KVM cannot know what the VMM advertises there, so make it a share bit and let the VMM keep the two consistent: KVM_PLANE_SHARE_DR6. The twins keep their own arch.dr6, kept equal whenever no plane is in the guest: - after each plane's run in the KVM_RUN plane loop, its DR6 is copied to the twins. By then the exit has synced any DR6 the guest owned in hardware back into arch.dr6 (sync_dirty_debug_regs() on VMX and SVM, under KVM_DEBUGREG_WONT_EXIT); otherwise DR accesses are intercepted and arch.dr6 is the only copy. A VTL switch done in the kernel returns through the same loop; - KVM_SET_DEBUGREGS copies the new value to the twins at once; - a twin created later starts with plane 0's DR6, as for XCR0. Converting every arch.dr6 user to a shared location instead would touch the vendor, nested, SMM and exception code for no gain. Writers audited for the copy-after-run rule: - MOV to DR6 (emulated, or direct under KVM_DEBUGREG_WONT_EXIT), #DB payload delivery (kvm_deliver_exception_payload()), RSM, INIT and nested VMRUN on SVM all write the DR6 of the plane that is running, so the next copy carries them; - a #DB still pending on a plane that is switched away from updates DR6 when that plane next takes it, which matches a debug exception that has not been delivered yet; - host-side debugging (KVM_SET_GUEST_DEBUG) reports in kvm_run->debug.arch.dr6 and leaves arch.dr6 alone; - vCPU ioctls on twins take the shared vCPU mutex, so none of them can observe a plane mid-run. DR0-DR3 and DR7 stay per plane. Signed-off-by: David Woodhouse --- Documentation/virt/kvm/api.rst | 5 +++++ arch/x86/include/uapi/asm/kvm.h | 1 + arch/x86/kvm/regs.c | 1 + arch/x86/kvm/x86.c | 25 +++++++++++++++++++++++++ arch/x86/kvm/x86.h | 4 +++- 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 22dc3c793bc4..351cc7448fe1 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -9353,6 +9353,11 @@ is set once. The bits are: - KVM_PLANE_SHARE_XCR0: XCR0, whether written by the guest with XSETBV or with KVM_SET_XCRS on any plane's vCPU fd. A plane vCPU created later starts with plane 0's value. +- KVM_PLANE_SHARE_DR6: DR6. A guest write, or a debug exception, in one + plane is seen by the others once that plane returns from KVM_RUN or + switches to another plane; a KVM_SET_DEBUGREGS on any plane's vCPU fd is + seen at once. A plane vCPU created later starts with plane 0's value. + DR0-DR3 and DR7 stay per plane. 8. Other capabilities. ====================== diff --git a/arch/x86/include/uapi/asm/kvm.h b/arch/x86/include/uapi/asm/kvm.h index edec00979acf..dbfdc1188c20 100644 --- a/arch/x86/include/uapi/asm/kvm.h +++ b/arch/x86/include/uapi/asm/kvm.h @@ -924,6 +924,7 @@ struct kvm_sev_snp_launch_finish { /* KVM_CAP_PLANES args[1]: state the planes of a vCPU share */ #define KVM_PLANE_SHARE_XCR0 _BITULL(0) +#define KVM_PLANE_SHARE_DR6 _BITULL(1) struct kvm_hyperv_eventfd { __u32 conn_id; diff --git a/arch/x86/kvm/regs.c b/arch/x86/kvm/regs.c index 3621f1a47d37..8a856e478095 100644 --- a/arch/x86/kvm/regs.c +++ b/arch/x86/kvm/regs.c @@ -876,6 +876,7 @@ int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, kvm_update_dr0123(vcpu); vcpu->arch.dr6 = dbgregs->dr6; + kvm_plane_share_dr6(vcpu); vcpu->arch.dr7 = dbgregs->dr7; kvm_update_dr7(vcpu); diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c index ceec10d116a9..7206ee889e9b 100644 --- a/arch/x86/kvm/x86.c +++ b/arch/x86/kvm/x86.c @@ -9362,6 +9362,26 @@ static int __kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, bool held) return r; } +/* + * With KVM_PLANE_SHARE_DR6, every twin holds the same DR6 whenever no plane + * is in the guest. Copy it from @vcpu to its twins after @vcpu changed it: + * after an ioctl write, or after @vcpu ran, by which time its exit has + * synced any guest-owned hardware DR6 back into arch.dr6 + * (sync_dirty_debug_regs() on both vendors). + */ +void kvm_plane_share_dr6(struct kvm_vcpu *vcpu) +{ + struct kvm_vcpu *twin; + int i; + + if (!kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_DR6) || + vcpu->common->vcpus[vcpu->plane_level] != vcpu) + return; + + vcpu_for_each_plane(vcpu->common, i, twin) + twin->arch.dr6 = vcpu->arch.dr6; +} + int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu_plane0) { struct kvm_vcpu_common *common = vcpu_plane0->common; @@ -9374,6 +9394,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu_plane0) common->plane_switch = false; ret = __kvm_arch_vcpu_ioctl_run(vcpu, held); + kvm_plane_share_dr6(vcpu); if (ret) break; } while (vcpu_plane0->common->plane_switch); @@ -9854,6 +9875,10 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_XCR0) && vcpu->plane_level && vcpu->common->vcpus[0]) vcpu->arch.xcr0 = vcpu->common->vcpus[0]->arch.xcr0; + /* Likewise for a shared DR6. */ + if (kvm_plane_shares(vcpu->kvm, KVM_PLANE_SHARE_DR6) && + vcpu->plane_level && vcpu->common->vcpus[0]) + vcpu->arch.dr6 = vcpu->common->vcpus[0]->arch.dr6; kvm_init_mmu(vcpu); vcpu_put(vcpu); return 0; diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h index aa8abd219507..4da224eb1745 100644 --- a/arch/x86/kvm/x86.h +++ b/arch/x86/kvm/x86.h @@ -927,11 +927,13 @@ unsigned kvm_x86_default_max_planes(struct kvm *kvm); * The per-vCPU state that userspace may ask the planes of a vCPU to share, * in args[1] of KVM_ENABLE_CAP(KVM_CAP_PLANES). */ -#define KVM_PLANE_SHARE_VALID KVM_PLANE_SHARE_XCR0 +#define KVM_PLANE_SHARE_VALID (KVM_PLANE_SHARE_XCR0 | KVM_PLANE_SHARE_DR6) static inline bool kvm_plane_shares(struct kvm *kvm, u64 state) { return READ_ONCE(kvm->arch.plane_share) & state; } +void kvm_plane_share_dr6(struct kvm_vcpu *vcpu); + #endif -- 2.50.1 From: David Woodhouse Two checks of how plane twins follow the CPUID they share. A plane vCPU created after KVM_SET_CPUID gets its Hyper-V context in kvm_arch_vcpu_create(), before its vCPU index is assigned, and used to latch a VP index of 0xFFFFFFFF. Create two vCPUs, give them a Hyper-V CPUID, then create their plane-1 twins and check that HV_X64_MSR_VP_INDEX on each twin reads its vCPU index. Once a twin has run, KVM_SET_CPUID2 must accept only the CPUID already set, even on a plane 0 that has never run. Run plane 1 with plane 0 held (a real-mode OUT, then retired), and check that identical CPUID is accepted and that CPUID with XTILE added to leaf 0xD fails with EINVAL. Before kvm_set_cpuid() checked every twin, that call went on to the fpstate code, which failed it with EPERM, or resized the twin that ran if the process had AMX permission. Signed-off-by: David Woodhouse --- tools/testing/selftests/kvm/x86/planes_test.c | 125 +++++++++++++++++- 1 file changed, 122 insertions(+), 3 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/planes_test.c b/tools/testing/selftests/kvm/x86/planes_test.c index 968f1b099b8e..dd3c251f0b7a 100644 --- a/tools/testing/selftests/kvm/x86/planes_test.c +++ b/tools/testing/selftests/kvm/x86/planes_test.c @@ -1,16 +1,19 @@ // SPDX-License-Identifier: GPL-2.0-only /* * Test the KVM_CAP_PLANES opt-in and the plane lifecycle uAPI: plane - * creation bounds, plane vCPU creation, and the independence of - * per-plane vCPU register state. + * creation bounds, plane vCPU creation, the independence of + * per-plane vCPU register state, the Hyper-V VP index of a plane + * vCPU created after KVM_SET_CPUID, and KVM_SET_CPUID2 once a twin + * has run. */ #include #include #include "kvm_util.h" #include "processor.h" +#include "hyperv.h" -/* Where the run-state test puts plane 0's one instruction. */ +/* Where the tests that enter a plane put its one instruction. */ #define CODE_GPA 0x1000 /* @@ -317,6 +320,120 @@ static void test_plane_lifecycle(void) kvm_vm_free(vm); } +/* + * A plane vCPU created after KVM_SET_CPUID gets its Hyper-V context at + * creation, and its default VP index must be its vCPU index like any + * other vCPU's, not the -1 the vCPU carries until it is published. + */ +static void test_late_twin_vp_index(void) +{ + struct kvm_plane_vcpu *twin; + struct kvm_vcpu *vcpu[2]; + struct kvm_plane *plane; + struct kvm_vm *vm; + int i; + + if (!kvm_has_cap(KVM_CAP_SYS_HYPERV_CPUID)) { + print_skip("KVM_CAP_SYS_HYPERV_CPUID not supported"); + return; + } + + vm = vm_create_barebones_irqchip(true); + vm_enable_cap(vm, KVM_CAP_PLANES, 2); + plane = vm_plane_add(vm, 1); + + for (i = 0; i < 2; i++) { + vcpu[i] = __vm_vcpu_add(vm, i); + vcpu_set_hv_cpuid(vcpu[i]); + } + + for (i = 0; i < 2; i++) { + twin = __vm_plane_vcpu_add(vcpu[i], plane); + TEST_ASSERT_EQ(plane_vcpu_get_msr(twin, HV_X64_MSR_VP_INDEX), i); + } + + kvm_vm_free(vm); +} + +/* + * CPUID is shared between plane twins, so once any twin has run, + * KVM_SET_CPUID2 on plane 0 only accepts the CPUID already set, even + * while plane 0 itself has never run. Without that, an enlarged + * dynamic xfeature would resize the fpstate of the twin that ran. + * Plane 1 runs here with plane 0 held: a real-mode OUT at CODE_GPA, + * which is then retired. + */ +static void test_cpuid_after_twin_ran(void) +{ + static const u8 out_insn[] = { 0xe6, 0x10 }; /* out %al, $0x10 */ + u64 hold = KVM_PLANE_RUN_STATE_USER_HOLD, none = 0; + struct kvm_cpuid_entry2 *leaf_d = NULL; + struct kvm_plane_vcpu *vcpu1; + struct kvm_cpuid2 *enlarged; + struct kvm_plane *plane; + struct kvm_sregs sregs; + struct kvm_vcpu *vcpu0; + struct kvm_regs regs; + struct kvm_vm *vm; + u32 i; + + vm = vm_create_barebones_irqchip(true); + vm_enable_cap(vm, KVM_CAP_PLANES, 2); + plane = vm_plane_add(vm, 1); + vcpu0 = __vm_vcpu_add(vm, 0); + vcpu1 = __vm_plane_vcpu_add(vcpu0, plane); + vcpu_init_cpuid(vcpu0, kvm_get_supported_cpuid()); + + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, CODE_GPA, 1, 1, 0); + memcpy(addr_gpa2hva(vm, CODE_GPA), out_insn, sizeof(out_insn)); + + plane_vcpu_ioctl(vcpu1, KVM_GET_SREGS, &sregs); + sregs.cs.selector = 0; + sregs.cs.base = 0; + plane_vcpu_ioctl(vcpu1, KVM_SET_SREGS, &sregs); + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s); + regs.rip = CODE_GPA; + plane_vcpu_ioctl(vcpu1, KVM_SET_REGS, ®s); + + vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &hold); + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &none); + TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0); + TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO); + TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10); + + /* Retire the OUT without entering again. */ + vcpu0->run->immediate_exit = 1; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EINTR); + vcpu0->run->immediate_exit = 0; + + /* Identical CPUID is still accepted. */ + TEST_ASSERT_EQ(__vcpu_ioctl(vcpu0, KVM_SET_CPUID2, vcpu0->cpuid), 0); + + /* + * An enlarged dynamic xfeature is refused by the has-run check + * (EINVAL), before the fpstate code could fail it for lack of + * permission (EPERM). + */ + enlarged = allocate_kvm_cpuid2(vcpu0->cpuid->nent); + memcpy(enlarged, vcpu0->cpuid, kvm_cpuid2_size(vcpu0->cpuid->nent)); + for (i = 0; i < enlarged->nent; i++) { + if (enlarged->entries[i].function == 0xd && + enlarged->entries[i].index == 0) + leaf_d = &enlarged->entries[i]; + } + if (leaf_d) { + leaf_d->eax |= XFEATURE_MASK_XTILE; + TEST_ASSERT_EQ(__vcpu_ioctl(vcpu0, KVM_SET_CPUID2, enlarged), -1); + TEST_ASSERT_EQ(errno, EINVAL); + } else { + print_skip("no CPUID leaf 0xD, enlarged-xfeature check"); + } + free(enlarged); + + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES)); @@ -326,6 +443,8 @@ int main(int argc, char *argv[]) test_enable_cap_bounds(); test_enable_cap_too_late(); test_plane_lifecycle(); + test_late_twin_vp_index(); + test_cpuid_after_twin_ran(); return 0; } -- 2.50.1 From: David Woodhouse Check the KVM_CAP_PLANES share mask: KVM_CAP_PLANES_SHARE reports XCR0|DR6 on the system fd, 0 on a VM fd before the opt-in and the enabled bits after it; an unknown bit and a non-zero args[2] fail with EINVAL, and a second opt-in with another mask fails with EEXIST. Then, with no sharing and with XCR0 and DR6 shared, write XCR0 and DR6 through plane 0 before creating a plane-1 twin, and check that the twin inherits them only when shared; write them through plane 1 and check plane 0; and have plane 1 write DR6 with MOV (real mode, plane 0 held) and check that plane 0 sees the guest's value only when DR6 is shared. Signed-off-by: David Woodhouse --- tools/testing/selftests/kvm/x86/planes_test.c | 159 +++++++++++++++++- 1 file changed, 157 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/kvm/x86/planes_test.c b/tools/testing/selftests/kvm/x86/planes_test.c index dd3c251f0b7a..02242cc7d576 100644 --- a/tools/testing/selftests/kvm/x86/planes_test.c +++ b/tools/testing/selftests/kvm/x86/planes_test.c @@ -3,8 +3,8 @@ * Test the KVM_CAP_PLANES opt-in and the plane lifecycle uAPI: plane * creation bounds, plane vCPU creation, the independence of * per-plane vCPU register state, the Hyper-V VP index of a plane - * vCPU created after KVM_SET_CPUID, and KVM_SET_CPUID2 once a twin - * has run. + * vCPU created after KVM_SET_CPUID, KVM_SET_CPUID2 once a twin has + * run, and the KVM_CAP_PLANES share mask. */ #include #include @@ -434,6 +434,154 @@ static void test_cpuid_after_twin_ran(void) kvm_vm_free(vm); } +static int __enable_planes_share(struct kvm_vm *vm, u64 count, u64 share, u64 arg2) +{ + struct kvm_enable_cap cap = { + .cap = KVM_CAP_PLANES, + .args = { count, share, arg2 }, + }; + + return __vm_ioctl(vm, KVM_ENABLE_CAP, &cap); +} + +/* + * KVM_CAP_PLANES_SHARE reports the supported share bits on the system fd + * and the enabled ones on a VM fd. Unknown bits and a non-zero args[2] + * are refused, and like the count the mask is set once. + */ +static void test_share_opt_in(void) +{ + u64 defined = KVM_PLANE_SHARE_XCR0 | KVM_PLANE_SHARE_DR6; + u64 unknown = ~defined & -~defined; + struct kvm_vm *vm; + + TEST_ASSERT_EQ(kvm_check_cap(KVM_CAP_PLANES_SHARE), defined); + + vm = vm_create_barebones_irqchip(true); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES_SHARE), 0); + TEST_ASSERT_EQ(__enable_planes_share(vm, 2, unknown, 0), -1); + TEST_ASSERT_EQ(errno, EINVAL); + TEST_ASSERT_EQ(__enable_planes_share(vm, 2, 0, 1), -1); + TEST_ASSERT_EQ(errno, EINVAL); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES), 1); + + TEST_ASSERT_EQ(__enable_planes_share(vm, 2, KVM_PLANE_SHARE_DR6, 0), 0); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES_SHARE), KVM_PLANE_SHARE_DR6); + TEST_ASSERT_EQ(__enable_planes_share(vm, 2, KVM_PLANE_SHARE_XCR0, 0), -1); + TEST_ASSERT_EQ(errno, EEXIST); + TEST_ASSERT_EQ(vm_check_cap(vm, KVM_CAP_PLANES_SHARE), KVM_PLANE_SHARE_DR6); + kvm_vm_free(vm); +} + +static u64 get_xcr0(int fd) +{ + struct kvm_xcrs xcrs = {}; + + TEST_ASSERT_EQ(ioctl(fd, KVM_GET_XCRS, &xcrs), 0); + TEST_ASSERT_EQ(xcrs.nr_xcrs, 1); + return xcrs.xcrs[0].value; +} + +static void set_xcr0(int fd, u64 xcr0) +{ + struct kvm_xcrs xcrs = { .nr_xcrs = 1, .xcrs[0] = { .xcr = 0, .value = xcr0 } }; + + TEST_ASSERT_EQ(ioctl(fd, KVM_SET_XCRS, &xcrs), 0); +} + +static u64 get_dr6(int fd) +{ + struct kvm_debugregs dbg; + + TEST_ASSERT_EQ(ioctl(fd, KVM_GET_DEBUGREGS, &dbg), 0); + return dbg.dr6; +} + +static void set_dr6(int fd, u64 dr6) +{ + struct kvm_debugregs dbg; + + TEST_ASSERT_EQ(ioctl(fd, KVM_GET_DEBUGREGS, &dbg), 0); + dbg.dr6 = dr6; + TEST_ASSERT_EQ(ioctl(fd, KVM_SET_DEBUGREGS, &dbg), 0); +} + +/* + * With @share, a write through one plane is seen through the other, and a + * plane vCPU created later starts with plane 0's value; without it each + * plane keeps its own. Checked for XCR0 and DR6 by ioctl, and for DR6 by + * a guest MOV in plane 1 (real mode, at CODE_GPA, with plane 0 held). + */ +static void test_share_state(u64 share) +{ + static const u8 mov_dr6[] = { + 0x66, 0xb8, 0xf4, 0x0f, 0xff, 0xff, /* mov $0xffff0ff4, %eax */ + 0x0f, 0x23, 0xf0, /* mov %eax, %dr6 */ + 0xe6, 0x10, /* out %al, $0x10 */ + }; + bool xcr0_shared = share & KVM_PLANE_SHARE_XCR0; + bool dr6_shared = share & KVM_PLANE_SHARE_DR6; + u64 hold = KVM_PLANE_RUN_STATE_USER_HOLD, none = 0; + struct kvm_plane_vcpu *vcpu1; + struct kvm_plane *plane; + struct kvm_sregs sregs; + struct kvm_vcpu *vcpu0; + struct kvm_regs regs; + struct kvm_vm *vm; + + vm = vm_create_barebones_irqchip(true); + TEST_ASSERT_EQ(__enable_planes_share(vm, 2, share, 0), 0); + plane = vm_plane_add(vm, 1); + vcpu0 = __vm_vcpu_add(vm, 0); + vcpu_init_cpuid(vcpu0, kvm_get_supported_cpuid()); + + /* Plane 0 first, then a plane-1 twin that inherits or not. */ + set_xcr0(vcpu0->fd, XFEATURE_MASK_FP | XFEATURE_MASK_SSE); + set_dr6(vcpu0->fd, 0xffff0ff1); + vcpu1 = __vm_plane_vcpu_add(vcpu0, plane); + TEST_ASSERT_EQ(get_xcr0(vcpu1->fd), xcr0_shared ? + XFEATURE_MASK_FP | XFEATURE_MASK_SSE : XFEATURE_MASK_FP); + TEST_ASSERT_EQ(get_dr6(vcpu1->fd), dr6_shared ? 0xffff0ff1 : 0xffff0ff0); + + /* A write through plane 1. */ + set_xcr0(vcpu1->fd, XFEATURE_MASK_FP); + set_dr6(vcpu1->fd, 0xffff0ff2); + TEST_ASSERT_EQ(get_xcr0(vcpu0->fd), xcr0_shared ? + XFEATURE_MASK_FP : XFEATURE_MASK_FP | XFEATURE_MASK_SSE); + TEST_ASSERT_EQ(get_dr6(vcpu0->fd), dr6_shared ? 0xffff0ff2 : 0xffff0ff1); + + /* A guest write of DR6 in plane 1. */ + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, CODE_GPA, 1, 1, 0); + memcpy(addr_gpa2hva(vm, CODE_GPA), mov_dr6, sizeof(mov_dr6)); + plane_vcpu_ioctl(vcpu1, KVM_GET_SREGS, &sregs); + sregs.cs.selector = 0; + sregs.cs.base = 0; + plane_vcpu_ioctl(vcpu1, KVM_SET_SREGS, &sregs); + plane_vcpu_ioctl(vcpu1, KVM_GET_REGS, ®s); + regs.rip = CODE_GPA; + plane_vcpu_ioctl(vcpu1, KVM_SET_REGS, ®s); + + vcpu_ioctl(vcpu0, KVM_SET_PLANE_RUN_STATE, &hold); + plane_vcpu_ioctl(vcpu1, KVM_SET_PLANE_RUN_STATE, &none); + TEST_ASSERT_EQ(__vcpu_run(vcpu0), 0); + TEST_ASSERT_EQ(vcpu0->run->exit_reason, KVM_EXIT_IO); + TEST_ASSERT_EQ(vcpu0->run->io.port, 0x10); + vcpu0->run->immediate_exit = 1; + TEST_ASSERT_EQ(__vcpu_run(vcpu0), -1); + TEST_ASSERT_EQ(errno, EINTR); + vcpu0->run->immediate_exit = 0; + + TEST_ASSERT(get_dr6(vcpu1->fd) & BIT(2), + "plane 1 DR6 0x%lx lacks the B2 bit the guest wrote", + (unsigned long)get_dr6(vcpu1->fd)); + if (dr6_shared) + TEST_ASSERT_EQ(get_dr6(vcpu0->fd), get_dr6(vcpu1->fd)); + else + TEST_ASSERT_EQ(get_dr6(vcpu0->fd), 0xffff0ff1); + + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { TEST_REQUIRE(kvm_has_cap(KVM_CAP_PLANES)); @@ -445,6 +593,13 @@ int main(int argc, char *argv[]) test_plane_lifecycle(); test_late_twin_vp_index(); test_cpuid_after_twin_ran(); + test_share_opt_in(); + if (kvm_cpu_has(X86_FEATURE_XSAVE)) { + test_share_state(0); + test_share_state(KVM_PLANE_SHARE_XCR0 | KVM_PLANE_SHARE_DR6); + } else { + print_skip("XSAVE not supported, plane state sharing"); + } return 0; } -- 2.50.1