Merge branch 'kvm-older-features' into HEAD
[linux-2.6-microblaze.git] / arch / x86 / include / asm / kvm_host.h
index 6dcccb3..f1dfa06 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/cpumask.h>
 #include <linux/irq_work.h>
 #include <linux/irq.h>
+#include <linux/workqueue.h>
 
 #include <linux/kvm.h>
 #include <linux/kvm_para.h>
 #define KVM_REQ_MSR_FILTER_CHANGED     KVM_ARCH_REQ(29)
 #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)
 
 #define CR0_RESERVED_BITS                                               \
        (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
@@ -246,6 +249,7 @@ enum x86_intercept_stage;
 #define PFERR_SGX_BIT 15
 #define PFERR_GUEST_FINAL_BIT 32
 #define PFERR_GUEST_PAGE_BIT 33
+#define PFERR_IMPLICIT_ACCESS_BIT 48
 
 #define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)
 #define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
@@ -256,6 +260,7 @@ enum x86_intercept_stage;
 #define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)
 #define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)
 #define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT)
+#define PFERR_IMPLICIT_ACCESS (1ULL << PFERR_IMPLICIT_ACCESS_BIT)
 
 #define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK |       \
                                 PFERR_WRITE_MASK |             \
@@ -427,13 +432,12 @@ struct kvm_mmu {
        void (*inject_page_fault)(struct kvm_vcpu *vcpu,
                                  struct x86_exception *fault);
        gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
-                           gpa_t gva_or_gpa, u32 access,
+                           gpa_t gva_or_gpa, u64 access,
                            struct x86_exception *exception);
        int (*sync_page)(struct kvm_vcpu *vcpu,
                         struct kvm_mmu_page *sp);
        void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa);
-       hpa_t root_hpa;
-       gpa_t root_pgd;
+       struct kvm_mmu_root_info root;
        union kvm_mmu_role mmu_role;
        u8 root_level;
        u8 shadow_root_level;
@@ -498,6 +502,7 @@ struct kvm_pmc {
        bool intr;
 };
 
+#define KVM_PMC_MAX_FIXED      3
 struct kvm_pmu {
        unsigned nr_arch_gp_counters;
        unsigned nr_arch_fixed_counters;
@@ -509,9 +514,10 @@ struct kvm_pmu {
        u64 global_ctrl_mask;
        u64 global_ovf_ctrl_mask;
        u64 reserved_bits;
+       u64 raw_event_mask;
        u8 version;
        struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
-       struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
+       struct kvm_pmc fixed_counters[KVM_PMC_MAX_FIXED];
        struct irq_work irq_work;
        DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
        DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
@@ -601,16 +607,21 @@ struct kvm_vcpu_hv {
 struct kvm_vcpu_xen {
        u64 hypercall_rip;
        u32 current_runstate;
-       bool vcpu_info_set;
-       bool vcpu_time_info_set;
-       bool runstate_set;
-       struct gfn_to_hva_cache vcpu_info_cache;
-       struct gfn_to_hva_cache vcpu_time_info_cache;
-       struct gfn_to_hva_cache runstate_cache;
+       u8 upcall_vector;
+       struct gfn_to_pfn_cache vcpu_info_cache;
+       struct gfn_to_pfn_cache vcpu_time_info_cache;
+       struct gfn_to_pfn_cache runstate_cache;
        u64 last_steal;
        u64 runstate_entry_time;
        u64 runstate_times[4];
        unsigned long evtchn_pending_sel;
+       u32 vcpu_id; /* The Xen / ACPI vCPU ID */
+       u32 timer_virq;
+       u64 timer_expires; /* In guest epoch */
+       atomic_t timer_pending;
+       struct hrtimer timer;
+       int poll_evtchn;
+       struct timer_list poll_timer;
 };
 
 struct kvm_vcpu_arch {
@@ -703,7 +714,6 @@ struct kvm_vcpu_arch {
        struct fpu_guest guest_fpu;
 
        u64 xcr0;
-       u64 guest_supported_xcr0;
 
        struct kvm_pio_request pio;
        void *pio_data;
@@ -748,8 +758,7 @@ struct kvm_vcpu_arch {
        gpa_t time;
        struct pvclock_vcpu_time_info hv_clock;
        unsigned int hw_tsc_khz;
-       struct gfn_to_hva_cache pv_time;
-       bool pv_time_enabled;
+       struct gfn_to_pfn_cache pv_time;
        /* set guest stopped flag in pvclock flags field */
        bool pvclock_set_guest_stopped_request;
 
@@ -969,12 +978,10 @@ enum hv_tsc_page_status {
        HV_TSC_PAGE_UNSET = 0,
        /* TSC page MSR was written by the guest, update pending */
        HV_TSC_PAGE_GUEST_CHANGED,
-       /* TSC page MSR was written by KVM userspace, update pending */
+       /* TSC page update was triggered from the host side */
        HV_TSC_PAGE_HOST_CHANGED,
        /* TSC page was properly set up and is currently active  */
        HV_TSC_PAGE_SET,
-       /* TSC page is currently being updated and therefore is inactive */
-       HV_TSC_PAGE_UPDATING,
        /* TSC page was set up with an inaccessible GPA */
        HV_TSC_PAGE_BROKEN,
 };
@@ -1021,9 +1028,12 @@ struct msr_bitmap_range {
 
 /* Xen emulation context */
 struct kvm_xen {
+       u32 xen_version;
        bool long_mode;
        u8 upcall_vector;
        struct gfn_to_pfn_cache shinfo_cache;
+       struct idr evtchn_ports;
+       unsigned long poll_mask[BITS_TO_LONGS(KVM_MAX_VCPUS)];
 };
 
 enum kvm_irqchip_mode {
@@ -1038,14 +1048,17 @@ struct kvm_x86_msr_filter {
        struct msr_bitmap_range ranges[16];
 };
 
-#define APICV_INHIBIT_REASON_DISABLE    0
-#define APICV_INHIBIT_REASON_HYPERV     1
-#define APICV_INHIBIT_REASON_NESTED     2
-#define APICV_INHIBIT_REASON_IRQWIN     3
-#define APICV_INHIBIT_REASON_PIT_REINJ  4
-#define APICV_INHIBIT_REASON_X2APIC    5
-#define APICV_INHIBIT_REASON_BLOCKIRQ  6
-#define APICV_INHIBIT_REASON_ABSENT    7
+enum kvm_apicv_inhibit {
+       APICV_INHIBIT_REASON_DISABLE,
+       APICV_INHIBIT_REASON_HYPERV,
+       APICV_INHIBIT_REASON_NESTED,
+       APICV_INHIBIT_REASON_IRQWIN,
+       APICV_INHIBIT_REASON_PIT_REINJ,
+       APICV_INHIBIT_REASON_X2APIC,
+       APICV_INHIBIT_REASON_BLOCKIRQ,
+       APICV_INHIBIT_REASON_ABSENT,
+       APICV_INHIBIT_REASON_SEV,
+};
 
 struct kvm_arch {
        unsigned long n_used_mmu_pages;
@@ -1113,6 +1126,8 @@ struct kvm_arch {
        u64 cur_tsc_generation;
        int nr_vcpus_matched_tsc;
 
+       u32 default_tsc_khz;
+
        seqcount_raw_spinlock_t pvclock_sc;
        bool use_master_clock;
        u64 master_kernel_ns;
@@ -1128,10 +1143,6 @@ struct kvm_arch {
        struct kvm_hv hyperv;
        struct kvm_xen xen;
 
-       #ifdef CONFIG_KVM_MMU_AUDIT
-       int audit_point;
-       #endif
-
        bool backwards_tsc_observed;
        bool boot_vcpu_runs_old_kvmclock;
        u32 bsp_vcpu_id;
@@ -1151,6 +1162,7 @@ struct kvm_arch {
        bool exception_payload_enabled;
 
        bool bus_lock_detection_enabled;
+       bool enable_pmu;
        /*
         * If exit_on_emulation_error is set, and the in-kernel instruction
         * emulator fails to emulate an instruction, allow userspace
@@ -1220,6 +1232,7 @@ struct kvm_arch {
         * the thread holds the MMU lock in write mode.
         */
        spinlock_t tdp_mmu_pages_lock;
+       struct workqueue_struct *tdp_mmu_zap_wq;
 #endif /* CONFIG_X86_64 */
 
        /*
@@ -1318,7 +1331,6 @@ struct kvm_x86_ops {
        int (*hardware_enable)(void);
        void (*hardware_disable)(void);
        void (*hardware_unsetup)(void);
-       bool (*cpu_has_accelerated_tpr)(void);
        bool (*has_emulated_msr)(struct kvm *kvm, u32 index);
        void (*vcpu_after_set_cpuid)(struct kvm_vcpu *vcpu);
 
@@ -1331,7 +1343,7 @@ struct kvm_x86_ops {
        void (*vcpu_free)(struct kvm_vcpu *vcpu);
        void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
 
-       void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
+       void (*prepare_switch_to_guest)(struct kvm_vcpu *vcpu);
        void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
        void (*vcpu_put)(struct kvm_vcpu *vcpu);
 
@@ -1361,8 +1373,8 @@ struct kvm_x86_ops {
        void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
        bool (*get_if_flag)(struct kvm_vcpu *vcpu);
 
-       void (*tlb_flush_all)(struct kvm_vcpu *vcpu);
-       void (*tlb_flush_current)(struct kvm_vcpu *vcpu);
+       void (*flush_tlb_all)(struct kvm_vcpu *vcpu);
+       void (*flush_tlb_current)(struct kvm_vcpu *vcpu);
        int  (*tlb_remote_flush)(struct kvm *kvm);
        int  (*tlb_remote_flush_with_range)(struct kvm *kvm,
                        struct kvm_tlb_range *range);
@@ -1373,16 +1385,16 @@ struct kvm_x86_ops {
         * Can potentially get non-canonical addresses through INVLPGs, which
         * the implementation may choose to ignore if appropriate.
         */
-       void (*tlb_flush_gva)(struct kvm_vcpu *vcpu, gva_t addr);
+       void (*flush_tlb_gva)(struct kvm_vcpu *vcpu, gva_t addr);
 
        /*
         * Flush any TLB entries created by the guest.  Like tlb_flush_gva(),
         * does not need to flush GPA->HPA mappings.
         */
-       void (*tlb_flush_guest)(struct kvm_vcpu *vcpu);
+       void (*flush_tlb_guest)(struct kvm_vcpu *vcpu);
 
        int (*vcpu_pre_run)(struct kvm_vcpu *vcpu);
-       enum exit_fastpath_completion (*run)(struct kvm_vcpu *vcpu);
+       enum exit_fastpath_completion (*vcpu_run)(struct kvm_vcpu *vcpu);
        int (*handle_exit)(struct kvm_vcpu *vcpu,
                enum exit_fastpath_completion exit_fastpath);
        int (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
@@ -1391,8 +1403,8 @@ struct kvm_x86_ops {
        u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
        void (*patch_hypercall)(struct kvm_vcpu *vcpu,
                                unsigned char *hypercall_addr);
-       void (*set_irq)(struct kvm_vcpu *vcpu);
-       void (*set_nmi)(struct kvm_vcpu *vcpu);
+       void (*inject_irq)(struct kvm_vcpu *vcpu);
+       void (*inject_nmi)(struct kvm_vcpu *vcpu);
        void (*queue_exception)(struct kvm_vcpu *vcpu);
        void (*cancel_injection)(struct kvm_vcpu *vcpu);
        int (*interrupt_allowed)(struct kvm_vcpu *vcpu, bool for_injection);
@@ -1402,7 +1414,7 @@ struct kvm_x86_ops {
        void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
        void (*enable_irq_window)(struct kvm_vcpu *vcpu);
        void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
-       bool (*check_apicv_inhibit_reasons)(ulong bit);
+       bool (*check_apicv_inhibit_reasons)(enum kvm_apicv_inhibit reason);
        void (*refresh_apicv_exec_ctrl)(struct kvm_vcpu *vcpu);
        void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr);
        void (*hwapic_isr_update)(struct kvm_vcpu *vcpu, int isr);
@@ -1459,9 +1471,9 @@ struct kvm_x86_ops {
        void (*vcpu_blocking)(struct kvm_vcpu *vcpu);
        void (*vcpu_unblocking)(struct kvm_vcpu *vcpu);
 
-       int (*update_pi_irte)(struct kvm *kvm, unsigned int host_irq,
+       int (*pi_update_irte)(struct kvm *kvm, unsigned int host_irq,
                              uint32_t guest_irq, bool set);
-       void (*start_assignment)(struct kvm *kvm);
+       void (*pi_start_assignment)(struct kvm *kvm);
        void (*apicv_post_state_restore)(struct kvm_vcpu *vcpu);
        bool (*dy_apicv_has_pending_interrupt)(struct kvm_vcpu *vcpu);
 
@@ -1476,9 +1488,9 @@ struct kvm_x86_ops {
        int (*leave_smm)(struct kvm_vcpu *vcpu, const char *smstate);
        void (*enable_smi_window)(struct kvm_vcpu *vcpu);
 
-       int (*mem_enc_op)(struct kvm *kvm, void __user *argp);
-       int (*mem_enc_reg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
-       int (*mem_enc_unreg_region)(struct kvm *kvm, struct kvm_enc_region *argp);
+       int (*mem_enc_ioctl)(struct kvm *kvm, void __user *argp);
+       int (*mem_enc_register_region)(struct kvm *kvm, struct kvm_enc_region *argp);
+       int (*mem_enc_unregister_region)(struct kvm *kvm, struct kvm_enc_region *argp);
        int (*vm_copy_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
        int (*vm_move_enc_context_from)(struct kvm *kvm, unsigned int source_fd);
 
@@ -1495,6 +1507,11 @@ struct kvm_x86_ops {
        int (*complete_emulated_msr)(struct kvm_vcpu *vcpu, int err);
 
        void (*vcpu_deliver_sipi_vector)(struct kvm_vcpu *vcpu, u8 vector);
+
+       /*
+        * Returns vCPU specific APICv inhibit reasons
+        */
+       unsigned long (*vcpu_get_apicv_inhibit_reasons)(struct kvm_vcpu *vcpu);
 };
 
 struct kvm_x86_nested_ops {
@@ -1541,15 +1558,22 @@ extern struct kvm_x86_ops kvm_x86_ops;
 
 #define KVM_X86_OP(func) \
        DECLARE_STATIC_CALL(kvm_x86_##func, *(((struct kvm_x86_ops *)0)->func));
-#define KVM_X86_OP_NULL KVM_X86_OP
+#define KVM_X86_OP_OPTIONAL KVM_X86_OP
+#define KVM_X86_OP_OPTIONAL_RET0 KVM_X86_OP
 #include <asm/kvm-x86-ops.h>
 
 static inline void kvm_ops_static_call_update(void)
 {
-#define KVM_X86_OP(func) \
+#define __KVM_X86_OP(func) \
        static_call_update(kvm_x86_##func, kvm_x86_ops.func);
-#define KVM_X86_OP_NULL KVM_X86_OP
+#define KVM_X86_OP(func) \
+       WARN_ON(!kvm_x86_ops.func); __KVM_X86_OP(func)
+#define KVM_X86_OP_OPTIONAL __KVM_X86_OP
+#define KVM_X86_OP_OPTIONAL_RET0(func) \
+       static_call_update(kvm_x86_##func, (void *)kvm_x86_ops.func ? : \
+                                          (void *)__static_call_return0);
 #include <asm/kvm-x86-ops.h>
+#undef __KVM_X86_OP
 }
 
 #define __KVM_HAVE_ARCH_VM_ALLOC
@@ -1574,12 +1598,13 @@ static inline int kvm_arch_flush_remote_tlb(struct kvm *kvm)
 #define kvm_arch_pmi_in_guest(vcpu) \
        ((vcpu) && (vcpu)->arch.handling_intr_from_guest)
 
-int kvm_mmu_module_init(void);
-void kvm_mmu_module_exit(void);
+void kvm_mmu_x86_module_init(void);
+int kvm_mmu_vendor_module_init(void);
+void kvm_mmu_vendor_module_exit(void);
 
 void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
 int kvm_mmu_create(struct kvm_vcpu *vcpu);
-void kvm_mmu_init_vm(struct kvm *kvm);
+int kvm_mmu_init_vm(struct kvm *kvm);
 void kvm_mmu_uninit_vm(struct kvm *kvm);
 
 void kvm_mmu_after_set_cpuid(struct kvm_vcpu *vcpu);
@@ -1587,6 +1612,13 @@ void kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
 void kvm_mmu_slot_remove_write_access(struct kvm *kvm,
                                      const struct kvm_memory_slot *memslot,
                                      int start_level);
+void kvm_mmu_slot_try_split_huge_pages(struct kvm *kvm,
+                                      const struct kvm_memory_slot *memslot,
+                                      int target_level);
+void kvm_mmu_try_split_huge_pages(struct kvm *kvm,
+                                 const struct kvm_memory_slot *memslot,
+                                 u64 start, u64 end,
+                                 int target_level);
 void kvm_mmu_zap_collapsible_sptes(struct kvm *kvm,
                                   const struct kvm_memory_slot *memslot);
 void kvm_mmu_slot_leaf_clear_dirty(struct kvm *kvm,
@@ -1724,7 +1756,6 @@ int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
 void kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
-void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
 int kvm_emulate_xsetbv(struct kvm_vcpu *vcpu);
 
 int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
@@ -1769,9 +1800,9 @@ void kvm_inject_nmi(struct kvm_vcpu *vcpu);
 void kvm_update_dr7(struct kvm_vcpu *vcpu);
 
 int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn);
-void kvm_mmu_free_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
+void kvm_mmu_free_roots(struct kvm *kvm, struct kvm_mmu *mmu,
                        ulong roots_to_free);
-void kvm_mmu_free_guest_mode_roots(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu);
+void kvm_mmu_free_guest_mode_roots(struct kvm *kvm, struct kvm_mmu *mmu);
 gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
                              struct x86_exception *exception);
 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
@@ -1782,12 +1813,24 @@ gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
                                struct x86_exception *exception);
 
 bool kvm_apicv_activated(struct kvm *kvm);
+bool kvm_vcpu_apicv_activated(struct kvm_vcpu *vcpu);
 void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu);
-void kvm_request_apicv_update(struct kvm *kvm, bool activate,
-                             unsigned long bit);
+void __kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
+                                     enum kvm_apicv_inhibit reason, bool set);
+void kvm_set_or_clear_apicv_inhibit(struct kvm *kvm,
+                                   enum kvm_apicv_inhibit reason, bool set);
+
+static inline void kvm_set_apicv_inhibit(struct kvm *kvm,
+                                        enum kvm_apicv_inhibit reason)
+{
+       kvm_set_or_clear_apicv_inhibit(kvm, reason, true);
+}
 
-void __kvm_request_apicv_update(struct kvm *kvm, bool activate,
-                               unsigned long bit);
+static inline void kvm_clear_apicv_inhibit(struct kvm *kvm,
+                                          enum kvm_apicv_inhibit reason)
+{
+       kvm_set_or_clear_apicv_inhibit(kvm, reason, false);
+}
 
 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
 
@@ -1878,7 +1921,7 @@ static inline bool kvm_is_supported_user_return_msr(u32 msr)
        return kvm_find_user_return_msr(msr) >= 0;
 }
 
-u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc, u64 ratio);
+u64 kvm_scale_tsc(u64 tsc, u64 ratio);
 u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc);
 u64 kvm_calc_nested_tsc_offset(u64 l1_offset, u64 l2_offset, u64 l2_multiplier);
 u64 kvm_calc_nested_tsc_multiplier(u64 l1_multiplier, u64 l2_multiplier);
@@ -1955,4 +1998,12 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages);
 #define KVM_CLOCK_VALID_FLAGS                                          \
        (KVM_CLOCK_TSC_STABLE | KVM_CLOCK_REALTIME | KVM_CLOCK_HOST_TSC)
 
+#define KVM_X86_VALID_QUIRKS                   \
+       (KVM_X86_QUIRK_LINT0_REENABLED |        \
+        KVM_X86_QUIRK_CD_NW_CLEARED |          \
+        KVM_X86_QUIRK_LAPIC_MMIO_HOLE |        \
+        KVM_X86_QUIRK_OUT_7E_INC_RIP |         \
+        KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT |   \
+        KVM_X86_QUIRK_FIX_HYPERCALL_INSN)
+
 #endif /* _ASM_X86_KVM_HOST_H */