Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[cascardo/linux.git] / arch / x86 / kvm / x86.c
index 292e6ca..d21bce5 100644 (file)
@@ -1193,20 +1193,37 @@ void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
        elapsed = ns - kvm->arch.last_tsc_nsec;
 
        if (vcpu->arch.virtual_tsc_khz) {
+               int faulted = 0;
+
                /* n.b - signed multiplication and division required */
                usdiff = data - kvm->arch.last_tsc_write;
 #ifdef CONFIG_X86_64
                usdiff = (usdiff * 1000) / vcpu->arch.virtual_tsc_khz;
 #else
                /* do_div() only does unsigned */
-               asm("idivl %2; xor %%edx, %%edx"
-               : "=A"(usdiff)
-               : "A"(usdiff * 1000), "rm"(vcpu->arch.virtual_tsc_khz));
+               asm("1: idivl %[divisor]\n"
+                   "2: xor %%edx, %%edx\n"
+                   "   movl $0, %[faulted]\n"
+                   "3:\n"
+                   ".section .fixup,\"ax\"\n"
+                   "4: movl $1, %[faulted]\n"
+                   "   jmp  3b\n"
+                   ".previous\n"
+
+               _ASM_EXTABLE(1b, 4b)
+
+               : "=A"(usdiff), [faulted] "=r" (faulted)
+               : "A"(usdiff * 1000), [divisor] "rm"(vcpu->arch.virtual_tsc_khz));
+
 #endif
                do_div(elapsed, 1000);
                usdiff -= elapsed;
                if (usdiff < 0)
                        usdiff = -usdiff;
+
+               /* idivl overflow => difference is larger than USEC_PER_SEC */
+               if (faulted)
+                       usdiff = USEC_PER_SEC;
        } else
                usdiff = USEC_PER_SEC; /* disable TSC match window below */
 
@@ -1587,6 +1604,30 @@ static int kvm_guest_time_update(struct kvm_vcpu *v)
        return 0;
 }
 
+/*
+ * kvmclock updates which are isolated to a given vcpu, such as
+ * vcpu->cpu migration, should not allow system_timestamp from
+ * the rest of the vcpus to remain static. Otherwise ntp frequency
+ * correction applies to one vcpu's system_timestamp but not
+ * the others.
+ *
+ * So in those cases, request a kvmclock update for all vcpus.
+ * The worst case for a remote vcpu to update its kvmclock
+ * is then bounded by maximum nohz sleep latency.
+ */
+
+static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
+{
+       int i;
+       struct kvm *kvm = v->kvm;
+       struct kvm_vcpu *vcpu;
+
+       kvm_for_each_vcpu(i, vcpu, kvm) {
+               set_bit(KVM_REQ_CLOCK_UPDATE, &vcpu->requests);
+               kvm_vcpu_kick(vcpu);
+       }
+}
+
 static bool msr_mtrr_valid(unsigned msr)
 {
        switch (msr) {
@@ -1984,7 +2025,7 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                kvmclock_reset(vcpu);
 
                vcpu->arch.time = data;
-               kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+               kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
 
                /* we verify if the enable bit is set... */
                if (!(data & 1))
@@ -2701,7 +2742,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
                 * kvmclock on vcpu->cpu migration
                 */
                if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
-                       kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
+                       kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
                if (vcpu->cpu != cpu)
                        kvm_migrate_timers(vcpu);
                vcpu->cpu = cpu;
@@ -5238,7 +5279,13 @@ static void kvm_set_mmio_spte_mask(void)
         * Set the reserved bits and the present bit of an paging-structure
         * entry to generate page fault with PFER.RSV = 1.
         */
-       mask = ((1ull << (62 - maxphyaddr + 1)) - 1) << maxphyaddr;
+        /* Mask the reserved physical address bits. */
+       mask = ((1ull << (51 - maxphyaddr + 1)) - 1) << maxphyaddr;
+
+       /* Bit 62 is always reserved for 32bit host. */
+       mask |= 0x3ull << 62;
+
+       /* Set the present bit. */
        mask |= 1ull;
 
 #ifdef CONFIG_X86_64
@@ -5498,13 +5545,6 @@ static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
        char instruction[3];
        unsigned long rip = kvm_rip_read(vcpu);
 
-       /*
-        * Blow out the MMU to ensure that no other VCPU has an active mapping
-        * to ensure that the updated hypercall appears atomically across all
-        * VCPUs.
-        */
-       kvm_mmu_zap_all(vcpu->kvm);
-
        kvm_x86_ops->patch_hypercall(vcpu, instruction);
 
        return emulator_write_emulated(ctxt, rip, instruction, 3, NULL);
@@ -5702,6 +5742,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
                        __kvm_migrate_timers(vcpu);
                if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
                        kvm_gen_update_masterclock(vcpu->kvm);
+               if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
+                       kvm_gen_kvmclock_update(vcpu);
                if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
                        r = kvm_guest_time_update(vcpu);
                        if (unlikely(r))
@@ -6812,6 +6854,7 @@ int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
                return -EINVAL;
 
        INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
+       INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
        INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
 
        /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
@@ -7040,22 +7083,18 @@ void kvm_arch_commit_memory_region(struct kvm *kvm,
         * If memory slot is created, or moved, we need to clear all
         * mmio sptes.
         */
-       if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
-               kvm_mmu_zap_mmio_sptes(kvm);
-               kvm_reload_remote_mmus(kvm);
-       }
+       kvm_mmu_invalidate_mmio_sptes(kvm);
 }
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
-       kvm_mmu_zap_all(kvm);
-       kvm_reload_remote_mmus(kvm);
+       kvm_mmu_invalidate_zap_all_pages(kvm);
 }
 
 void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
                                   struct kvm_memory_slot *slot)
 {
-       kvm_arch_flush_shadow_all(kvm);
+       kvm_mmu_invalidate_zap_all_pages(kvm);
 }
 
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
@@ -7263,3 +7302,4 @@ EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
 EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
+EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);