When KVM_CAP_ADJUST_CLOCK is passed to KVM_CHECK_EXTENSION, it returns the
 set of bits that KVM can return in struct kvm_clock_data's flag member.
 
-The only flag defined now is KVM_CLOCK_TSC_STABLE.  If set, the returned
-value is the exact kvmclock value seen by all VCPUs at the instant
-when KVM_GET_CLOCK was called.  If clear, the returned value is simply
-CLOCK_MONOTONIC plus a constant offset; the offset can be modified
-with KVM_SET_CLOCK.  KVM will try to make all VCPUs follow this clock,
-but the exact value read by each VCPU could differ, because the host
-TSC is not stable.
+The following flags are defined:
+
+KVM_CLOCK_TSC_STABLE
+  If set, the returned value is the exact kvmclock
+  value seen by all VCPUs at the instant when KVM_GET_CLOCK was called.
+  If clear, the returned value is simply CLOCK_MONOTONIC plus a constant
+  offset; the offset can be modified with KVM_SET_CLOCK.  KVM will try
+  to make all VCPUs follow this clock, but the exact value read by each
+  VCPU could differ, because the host TSC is not stable.
+
+KVM_CLOCK_REALTIME
+  If set, the `realtime` field in the kvm_clock_data
+  structure is populated with the value of the host's real time
+  clocksource at the instant when KVM_GET_CLOCK was called. If clear,
+  the `realtime` field does not contain a value.
+
+KVM_CLOCK_HOST_TSC
+  If set, the `host_tsc` field in the kvm_clock_data
+  structure is populated with the value of the host's timestamp counter (TSC)
+  at the instant when KVM_GET_CLOCK was called. If clear, the `host_tsc` field
+  does not contain a value.
 
 ::
 
   struct kvm_clock_data {
        __u64 clock;  /* kvmclock current value */
        __u32 flags;
-       __u32 pad[9];
+       __u32 pad0;
+       __u64 realtime;
+       __u64 host_tsc;
+       __u32 pad[4];
   };
 
 
 In conjunction with KVM_GET_CLOCK, it is used to ensure monotonicity on scenarios
 such as migration.
 
+The following flags can be passed:
+
+KVM_CLOCK_REALTIME
+  If set, KVM will compare the value of the `realtime` field
+  with the value of the host's real time clocksource at the instant when
+  KVM_SET_CLOCK was called. The difference in elapsed time is added to the final
+  kvmclock value that will be provided to guests.
+
+Other flags returned by ``KVM_GET_CLOCK`` are accepted but ignored.
+
 ::
 
   struct kvm_clock_data {
        __u64 clock;  /* kvmclock current value */
        __u32 flags;
-       __u32 pad[9];
+       __u32 pad0;
+       __u64 realtime;
+       __u64 host_tsc;
+       __u32 pad[4];
   };
 
 
 
        struct pvclock_vcpu_time_info hv_clock;
        unsigned long flags;
 
+       data->flags = 0;
        spin_lock_irqsave(&ka->pvclock_gtod_sync_lock, flags);
        if (!ka->use_master_clock) {
                spin_unlock_irqrestore(&ka->pvclock_gtod_sync_lock, flags);
        get_cpu();
 
        if (__this_cpu_read(cpu_tsc_khz)) {
+#ifdef CONFIG_X86_64
+               struct timespec64 ts;
+
+               if (kvm_get_walltime_and_clockread(&ts, &data->host_tsc)) {
+                       data->realtime = ts.tv_nsec + NSEC_PER_SEC * ts.tv_sec;
+                       data->flags |= KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC;
+               } else
+#endif
+               data->host_tsc = rdtsc();
+
                kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
                                   &hv_clock.tsc_shift,
                                   &hv_clock.tsc_to_system_mul);
-               data->clock = __pvclock_read_cycles(&hv_clock, rdtsc());
+               data->clock = __pvclock_read_cycles(&hv_clock, data->host_tsc);
        } else {
                data->clock = get_kvmclock_base_ns() + ka->kvmclock_offset;
        }
 {
        struct kvm_clock_data data;
 
-       /*
-        * Zero flags as it's accessed RMW, leave everything else uninitialized
-        * as clock is always written and no other fields are consumed.
-        */
-       data.flags = 0;
-
        get_kvmclock(kvm, &data);
        return data.clock;
 }
                r = KVM_SYNC_X86_VALID_FIELDS;
                break;
        case KVM_CAP_ADJUST_CLOCK:
-               r = KVM_CLOCK_TSC_STABLE;
+               r = KVM_CLOCK_VALID_FLAGS;
                break;
        case KVM_CAP_X86_DISABLE_EXITS:
                r |=  KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
 {
        struct kvm_arch *ka = &kvm->arch;
        struct kvm_clock_data data;
-       u64 now_ns;
+       u64 now_raw_ns;
 
        if (copy_from_user(&data, argp, sizeof(data)))
                return -EFAULT;
 
-       if (data.flags)
+       /*
+        * Only KVM_CLOCK_REALTIME is used, but allow passing the
+        * result of KVM_GET_CLOCK back to KVM_SET_CLOCK.
+        */
+       if (data.flags & ~KVM_CLOCK_VALID_FLAGS)
                return -EINVAL;
 
        kvm_hv_invalidate_tsc_page(kvm);
         * is slightly ahead) here we risk going negative on unsigned
         * 'system_time' when 'data.clock' is very small.
         */
-       if (kvm->arch.use_master_clock)
-               now_ns = ka->master_kernel_ns;
+       if (data.flags & KVM_CLOCK_REALTIME) {
+               u64 now_real_ns = ktime_get_real_ns();
+
+               /*
+                * Avoid stepping the kvmclock backwards.
+                */
+               if (now_real_ns > data.realtime)
+                       data.clock += now_real_ns - data.realtime;
+       }
+
+       if (ka->use_master_clock)
+               now_raw_ns = ka->master_kernel_ns;
        else
-               now_ns = get_kvmclock_base_ns();
-       ka->kvmclock_offset = data.clock - now_ns;
+               now_raw_ns = get_kvmclock_base_ns();
+       ka->kvmclock_offset = data.clock - now_raw_ns;
        kvm_end_pvclock_update(kvm);
        return 0;
 }