[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v12 06/60] i386/hvm: Set Xen vCPU ID in KVM
From: |
David Woodhouse |
Subject: |
[PATCH v12 06/60] i386/hvm: Set Xen vCPU ID in KVM |
Date: |
Mon, 20 Feb 2023 20:46:42 +0000 |
From: David Woodhouse <dwmw@amazon.co.uk>
There are (at least) three different vCPU ID number spaces. One is the
internal KVM vCPU index, based purely on which vCPU was chronologically
created in the kernel first. If userspace threads are all spawned and
create their KVM vCPUs in essentially random order, then the KVM indices
are basically random too.
The second number space is the APIC ID space, which is consistent and
useful for referencing vCPUs. MSIs will specify the target vCPU using
the APIC ID, for example, and the KVM Xen APIs also take an APIC ID
from userspace whenever a vCPU needs to be specified (as opposed to
just using the appropriate vCPU fd).
The third number space is not normally relevant to the kernel, and is
the ACPI/MADT/Xen CPU number which corresponds to cs->cpu_index. But
Xen timer hypercalls use it, and Xen timer hypercalls *really* want
to be accelerated in the kernel rather than handled in userspace, so
the kernel needs to be told.
Signed-off-by: David Woodhouse <dwmw@amazon.co.uk>
Reviewed-by: Paul Durrant <paul@xen.org>
---
target/i386/kvm/kvm.c | 5 +++++
target/i386/kvm/xen-emu.c | 28 ++++++++++++++++++++++++++++
target/i386/kvm/xen-emu.h | 1 +
3 files changed, 34 insertions(+)
diff --git a/target/i386/kvm/kvm.c b/target/i386/kvm/kvm.c
index 2b3daabf7b..165fa5232d 100644
--- a/target/i386/kvm/kvm.c
+++ b/target/i386/kvm/kvm.c
@@ -1869,6 +1869,11 @@ int kvm_arch_init_vcpu(CPUState *cs)
}
}
+ r = kvm_xen_init_vcpu(cs);
+ if (r) {
+ return r;
+ }
+
kvm_base += 0x100;
#else /* CONFIG_XEN_EMU */
/* This should never happen as kvm_arch_init() would have died first.
*/
diff --git a/target/i386/kvm/xen-emu.c b/target/i386/kvm/xen-emu.c
index 34d5bc1bc9..4883b95d9d 100644
--- a/target/i386/kvm/xen-emu.c
+++ b/target/i386/kvm/xen-emu.c
@@ -52,6 +52,34 @@ int kvm_xen_init(KVMState *s, uint32_t hypercall_msr)
return 0;
}
+int kvm_xen_init_vcpu(CPUState *cs)
+{
+ int err;
+
+ /*
+ * The kernel needs to know the Xen/ACPI vCPU ID because that's
+ * what the guest uses in hypercalls such as timers. It doesn't
+ * match the APIC ID which is generally used for talking to the
+ * kernel about vCPUs. And if vCPU threads race with creating
+ * their KVM vCPUs out of order, it doesn't necessarily match
+ * with the kernel's internal vCPU indices either.
+ */
+ if (kvm_xen_has_cap(EVTCHN_SEND)) {
+ struct kvm_xen_vcpu_attr va = {
+ .type = KVM_XEN_VCPU_ATTR_TYPE_VCPU_ID,
+ .u.vcpu_id = cs->cpu_index,
+ };
+ err = kvm_vcpu_ioctl(cs, KVM_XEN_VCPU_SET_ATTR, &va);
+ if (err) {
+ error_report("kvm: Failed to set Xen vCPU ID attribute: %s",
+ strerror(-err));
+ return err;
+ }
+ }
+
+ return 0;
+}
+
uint32_t kvm_xen_get_caps(void)
{
return kvm_state->xen_caps;
diff --git a/target/i386/kvm/xen-emu.h b/target/i386/kvm/xen-emu.h
index 2101df0182..d62f1d8ed8 100644
--- a/target/i386/kvm/xen-emu.h
+++ b/target/i386/kvm/xen-emu.h
@@ -24,5 +24,6 @@
#define XEN_VERSION(maj, min) ((maj) << 16 | (min))
int kvm_xen_init(KVMState *s, uint32_t hypercall_msr);
+int kvm_xen_init_vcpu(CPUState *cs);
#endif /* QEMU_I386_KVM_XEN_EMU_H */
--
2.39.0
- [PATCH v12 15/60] i386/xen: add pc_machine_kvm_type to initialize XEN_EMULATE mode, (continued)
- [PATCH v12 15/60] i386/xen: add pc_machine_kvm_type to initialize XEN_EMULATE mode, David Woodhouse, 2023/02/20
- [PATCH v12 25/60] i386/xen: implement HVMOP_set_evtchn_upcall_vector, David Woodhouse, 2023/02/20
- [PATCH v12 59/60] i386/xen: Document Xen HVM emulation, David Woodhouse, 2023/02/20
- [PATCH v12 18/60] i386/xen: implement XENMEM_add_to_physmap_batch, David Woodhouse, 2023/02/20
- [PATCH v12 43/60] hw/xen: Add xen_gnttab device for grant table emulation, David Woodhouse, 2023/02/20
- [PATCH v12 12/60] i386/xen: Implement SCHEDOP_poll and SCHEDOP_yield, David Woodhouse, 2023/02/20
- [PATCH v12 13/60] hw/xen: Add xen_overlay device for emulating shared xenheap pages, David Woodhouse, 2023/02/20
- [PATCH v12 50/60] hw/xen: Add backend implementation of interdomain event channel support, David Woodhouse, 2023/02/20
- [PATCH v12 27/60] hw/xen: Add xen_evtchn device for event channel emulation, David Woodhouse, 2023/02/20
- [PATCH v12 51/60] hw/xen: Add xen_xenstore device for xenstore emulation, David Woodhouse, 2023/02/20
- [PATCH v12 06/60] i386/hvm: Set Xen vCPU ID in KVM,
David Woodhouse <=
- [PATCH v12 55/60] hw/xen: Implement emulated PIRQ hypercall support, David Woodhouse, 2023/02/20
- [PATCH v12 24/60] i386/xen: implement HYPERVISOR_event_channel_op, David Woodhouse, 2023/02/20
- [PATCH v12 56/60] hw/xen: Support GSI mapping to PIRQ, David Woodhouse, 2023/02/20
- [PATCH v12 09/60] i386/xen: handle guest hypercalls, David Woodhouse, 2023/02/20
- [PATCH v12 32/60] hw/xen: Implement EVTCHNOP_bind_virq, David Woodhouse, 2023/02/20
- [PATCH v12 21/60] i386/xen: handle VCPUOP_register_vcpu_info, David Woodhouse, 2023/02/20
- [PATCH v12 20/60] i386/xen: implement HYPERVISOR_vcpu_op, David Woodhouse, 2023/02/20
- [PATCH v12 01/60] include: import Xen public headers to hw/xen/interface, David Woodhouse, 2023/02/20
- [PATCH v12 16/60] i386/xen: manage and save/restore Xen guest long_mode setting, David Woodhouse, 2023/02/20
- [PATCH v12 36/60] hw/xen: Implement EVTCHNOP_bind_interdomain, David Woodhouse, 2023/02/20