Correctly migrating a guest's view of the system counter is challenging. To date, KVM has utilized value-based interfaces (e.g. guest value for IA32_TSC msr on x86, CNTVCT_EL0 on arm64) for migrating guest counter state. Restoring counters by value is problematic, especially for operators who intend to have the guest's counters account for elapsed time during live migration. Reason being, the guest counters _still_ increment even after calculating an appropriate value to restore a guest's counter on the target machine. Furthermore, maintaining the phase relationship between vCPU counters is impossible with a value-based approach. The only hope for maintaining the phase relationship of counters is to restore them by offset. Introduce a new pair of vcpu ioctls, KVM_GET_SYSTEM_COUNTER_STATE and KVM_SET_SYSTEM_COUNTER_STATE, that aim to do just that. Each implementing architecture will define its own counter state structure, allowing for flexibility with ISAs that may have multiple counters (like arm64). Reviewed-by: Jing Zhang <jingzhangos@xxxxxxxxxx> Reviewed-by: David Matlack <dmatlack@xxxxxxxxxx> Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx> Reviewed-by: Peter Shier <pshier@xxxxxxxxxx> Reviewed-by: Ricardo Koller <ricarkol@xxxxxxxxxx> Signed-off-by: Oliver Upton <oupton@xxxxxxxxxx> --- include/uapi/linux/kvm.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 3fd9a7e9d90c..562650c14e39 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1082,6 +1082,7 @@ struct kvm_ppc_resize_hpt { #define KVM_CAP_SGX_ATTRIBUTE 196 #define KVM_CAP_VM_COPY_ENC_CONTEXT_FROM 197 #define KVM_CAP_PTP_KVM 198 +#define KVM_CAP_SYSTEM_COUNTER_STATE 199 #ifdef KVM_CAP_IRQ_ROUTING @@ -1645,6 +1646,10 @@ struct kvm_xen_vcpu_attr { #define KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_DATA 0x4 #define KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST 0x5 +/* Available with KVM_CAP_SYSTEM_COUNTER_STATE */ +#define KVM_GET_SYSTEM_COUNTER_STATE _IOWR(KVMIO, 0xcc, struct kvm_system_counter_state) +#define KVM_SET_SYSTEM_COUNTER_STATE _IOW(KVMIO, 0xcd, struct kvm_system_counter_state) + /* Secure Encrypted Virtualization command */ enum sev_cmd_id { /* Guest initialization commands */ -- 2.32.0.rc1.229.g3e70b5a671-goog