On Mon, Dec 17, 2012 at 01:30:49PM +0800, Yang Zhang wrote:
> From: Yang Zhang <yang.z.zh...@intel.com>
> 
> Virtual interrupt delivery avoids KVM to inject vAPIC interrupts
> manually, which is fully taken care of by the hardware. This needs
> some special awareness into existing interrupr injection path:
> 
> - for pending interrupt, instead of direct injection, we may need
>   update architecture specific indicators before resuming to guest.
> 
> - A pending interrupt, which is masked by ISR, should be also
>   considered in above update action, since hardware will decide
>   when to inject it at right time. Current has_interrupt and
>   get_interrupt only returns a valid vector from injection p.o.v.
> 
> Signed-off-by: Kevin Tian <kevin.t...@intel.com>
> Signed-off-by: Yang Zhang <yang.z.zh...@intel.com>
> ---
>  arch/ia64/kvm/lapic.h           |    6 ++
>  arch/x86/include/asm/kvm_host.h |    6 ++
>  arch/x86/include/asm/vmx.h      |   11 +++
>  arch/x86/kvm/irq.c              |   56 +++++++++++++-
>  arch/x86/kvm/lapic.c            |   65 ++++++++++-------
>  arch/x86/kvm/lapic.h            |   28 ++++++-
>  arch/x86/kvm/svm.c              |   24 ++++++
>  arch/x86/kvm/vmx.c              |  154 
> ++++++++++++++++++++++++++++++++++++++-
>  arch/x86/kvm/x86.c              |   11 ++-
>  include/linux/kvm_host.h        |    2 +
>  virt/kvm/ioapic.c               |   36 +++++++++
>  virt/kvm/ioapic.h               |    1 +
>  virt/kvm/irq_comm.c             |   20 +++++
>  13 files changed, 379 insertions(+), 41 deletions(-)
> 
> diff --git a/arch/ia64/kvm/lapic.h b/arch/ia64/kvm/lapic.h
> index c5f92a9..cb59eb4 100644
> --- a/arch/ia64/kvm/lapic.h
> +++ b/arch/ia64/kvm/lapic.h
> @@ -27,4 +27,10 @@ int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct 
> kvm_lapic_irq *irq);
>  #define kvm_apic_present(x) (true)
>  #define kvm_lapic_enabled(x) (true)
>  
> +static inline void kvm_update_eoi_exitmap(struct kvm *kvm,
> +                                     struct kvm_lapic_irq *irq)
> +{
> +     /* IA64 has no apicv supporting, do nothing here */
> +}
> +
>  #endif
> diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
> index c431b33..b63a144 100644
> --- a/arch/x86/include/asm/kvm_host.h
> +++ b/arch/x86/include/asm/kvm_host.h
> @@ -697,6 +697,11 @@ 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);
> +     int (*has_virtual_interrupt_delivery)(struct kvm_vcpu *vcpu);
> +     void (*update_apic_irq)(struct kvm_vcpu *vcpu, int max_irr);
> +     void (*update_eoi_exitmap)(struct kvm *kvm, struct kvm_lapic_irq *irq);
> +     void (*reset_eoi_exitmap)(struct kvm_vcpu *vcpu);
> +     void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu);
>       int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
>       int (*get_tdp_level)(void);
>       u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
> @@ -991,6 +996,7 @@ int kvm_age_hva(struct kvm *kvm, unsigned long hva);
>  int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
>  void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
>  int cpuid_maxphyaddr(struct kvm_vcpu *vcpu);
> +int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v);
>  int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
>  int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
>  int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
> diff --git a/arch/x86/include/asm/vmx.h b/arch/x86/include/asm/vmx.h
> index 44c3f7e..d1ab331 100644
> --- a/arch/x86/include/asm/vmx.h
> +++ b/arch/x86/include/asm/vmx.h
> @@ -62,6 +62,7 @@
>  #define EXIT_REASON_MCE_DURING_VMENTRY  41
>  #define EXIT_REASON_TPR_BELOW_THRESHOLD 43
>  #define EXIT_REASON_APIC_ACCESS         44
> +#define EXIT_REASON_EOI_INDUCED         45
>  #define EXIT_REASON_EPT_VIOLATION       48
>  #define EXIT_REASON_EPT_MISCONFIG       49
>  #define EXIT_REASON_WBINVD              54
> @@ -143,6 +144,7 @@
>  #define SECONDARY_EXEC_WBINVD_EXITING                0x00000040
>  #define SECONDARY_EXEC_UNRESTRICTED_GUEST    0x00000080
>  #define SECONDARY_EXEC_APIC_REGISTER_VIRT       0x00000100
> +#define SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY    0x00000200
>  #define SECONDARY_EXEC_PAUSE_LOOP_EXITING    0x00000400
>  #define SECONDARY_EXEC_ENABLE_INVPCID                0x00001000
>  
> @@ -180,6 +182,7 @@ enum vmcs_field {
>       GUEST_GS_SELECTOR               = 0x0000080a,
>       GUEST_LDTR_SELECTOR             = 0x0000080c,
>       GUEST_TR_SELECTOR               = 0x0000080e,
> +     GUEST_INTR_STATUS               = 0x00000810,
>       HOST_ES_SELECTOR                = 0x00000c00,
>       HOST_CS_SELECTOR                = 0x00000c02,
>       HOST_SS_SELECTOR                = 0x00000c04,
> @@ -207,6 +210,14 @@ enum vmcs_field {
>       APIC_ACCESS_ADDR_HIGH           = 0x00002015,
>       EPT_POINTER                     = 0x0000201a,
>       EPT_POINTER_HIGH                = 0x0000201b,
> +     EOI_EXIT_BITMAP0                = 0x0000201c,
> +     EOI_EXIT_BITMAP0_HIGH           = 0x0000201d,
> +     EOI_EXIT_BITMAP1                = 0x0000201e,
> +     EOI_EXIT_BITMAP1_HIGH           = 0x0000201f,
> +     EOI_EXIT_BITMAP2                = 0x00002020,
> +     EOI_EXIT_BITMAP2_HIGH           = 0x00002021,
> +     EOI_EXIT_BITMAP3                = 0x00002022,
> +     EOI_EXIT_BITMAP3_HIGH           = 0x00002023,
>       GUEST_PHYSICAL_ADDRESS          = 0x00002400,
>       GUEST_PHYSICAL_ADDRESS_HIGH     = 0x00002401,
>       VMCS_LINK_POINTER               = 0x00002800,
> diff --git a/arch/x86/kvm/irq.c b/arch/x86/kvm/irq.c
> index b111aee..e113440 100644
> --- a/arch/x86/kvm/irq.c
> +++ b/arch/x86/kvm/irq.c
> @@ -38,6 +38,38 @@ int kvm_cpu_has_pending_timer(struct kvm_vcpu *vcpu)
>  EXPORT_SYMBOL(kvm_cpu_has_pending_timer);
>  
>  /*
> + * check if there is pending interrupt from
> + * non-APIC source without intack.
> + */
> +static int kvm_cpu_has_extint(struct kvm_vcpu *v)
> +{
> +     if (kvm_apic_accept_pic_intr(v))
> +             return pic_irqchip(v->kvm)->output;     /* PIC */
> +     else
> +             return 0;
> +}
> +
> +/*
> + * check if there is injectable interrupt:
> + * when virtual interrupt delivery enabled,
> + * interrupt from apic will handled by hardware,
> + * we don't need to check it here.
> + */
> +int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v)
> +{
> +     if (!irqchip_in_kernel(v->kvm))
> +             return v->arch.interrupt.pending;
> +
> +     if (kvm_cpu_has_extint(v))
> +             return 1;
> +
> +     if (kvm_apic_vid_enabled(v))
> +             return 0;
> +
> +     return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
> +}
> +
> +/*
>   * check if there is pending interrupt without
>   * intack.
>   */
> @@ -46,27 +78,41 @@ int kvm_cpu_has_interrupt(struct kvm_vcpu *v)
>       if (!irqchip_in_kernel(v->kvm))
>               return v->arch.interrupt.pending;
>  
> -     if (kvm_apic_accept_pic_intr(v) && pic_irqchip(v->kvm)->output)
> -             return pic_irqchip(v->kvm)->output;     /* PIC */
> +     if (kvm_cpu_has_extint(v))
> +             return 1;
>  
>       return kvm_apic_has_interrupt(v) != -1; /* LAPIC */
>  }
>  EXPORT_SYMBOL_GPL(kvm_cpu_has_interrupt);
>  
>  /*
> + * Read pending interrupt(from non-APIC source)
> + * vector and intack.
> + */
> +static int kvm_cpu_get_extint(struct kvm_vcpu *v)
> +{
> +     if (kvm_cpu_has_extint(v))
> +             return kvm_pic_read_irq(v->kvm); /* PIC */
> +     return -1;
> +}
> +
> +/*
>   * Read pending interrupt vector and intack.
>   */
>  int kvm_cpu_get_interrupt(struct kvm_vcpu *v)
>  {
> +     int vector;
> +
>       if (!irqchip_in_kernel(v->kvm))
>               return v->arch.interrupt.nr;
>  
> -     if (kvm_apic_accept_pic_intr(v) && pic_irqchip(v->kvm)->output)
> -             return kvm_pic_read_irq(v->kvm);        /* PIC */
> +     vector = kvm_cpu_get_extint(v);
> +
> +     if (kvm_apic_vid_enabled(v) || vector != -1)
> +             return vector;                  /* PIC */
>  
>       return kvm_get_apic_interrupt(v);       /* APIC */
>  }
> -EXPORT_SYMBOL_GPL(kvm_cpu_get_interrupt);
>  
>  void kvm_inject_pending_timer_irqs(struct kvm_vcpu *vcpu)
>  {
> diff --git a/arch/x86/kvm/lapic.c b/arch/x86/kvm/lapic.c
> index 0664c13..61971bd 100644
> --- a/arch/x86/kvm/lapic.c
> +++ b/arch/x86/kvm/lapic.c
> @@ -133,6 +133,12 @@ static inline int apic_enabled(struct kvm_lapic *apic)
>       return kvm_apic_sw_enabled(apic) &&     kvm_apic_hw_enabled(apic);
>  }
>  
> +bool kvm_apic_present(struct kvm_vcpu *vcpu)
> +{
> +     return kvm_vcpu_has_lapic(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
> +}
> +EXPORT_SYMBOL_GPL(kvm_apic_present);
> +
>  #define LVT_MASK     \
>       (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
>  
> @@ -150,23 +156,6 @@ static inline int kvm_apic_id(struct kvm_lapic *apic)
>       return (kvm_apic_get_reg(apic, APIC_ID) >> 24) & 0xff;
>  }
>  
> -static inline u16 apic_cluster_id(struct kvm_apic_map *map, u32 ldr)
> -{
> -     u16 cid;
> -     ldr >>= 32 - map->ldr_bits;
> -     cid = (ldr >> map->cid_shift) & map->cid_mask;
> -
> -     BUG_ON(cid >= ARRAY_SIZE(map->logical_map));
> -
> -     return cid;
> -}
> -
> -static inline u16 apic_logical_id(struct kvm_apic_map *map, u32 ldr)
> -{
> -     ldr >>= (32 - map->ldr_bits);
> -     return ldr & map->lid_mask;
> -}
> -
>  static void recalculate_apic_map(struct kvm *kvm)
>  {
>       struct kvm_apic_map *new, *old = NULL;
> @@ -236,12 +225,14 @@ static inline void kvm_apic_set_id(struct kvm_lapic 
> *apic, u8 id)
>  {
>       apic_set_reg(apic, APIC_ID, id << 24);
>       recalculate_apic_map(apic->vcpu->kvm);
> +     ioapic_update_eoi_exitmap(apic->vcpu->kvm);
>  }
>  
>  static inline void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
>  {
>       apic_set_reg(apic, APIC_LDR, id);
>       recalculate_apic_map(apic->vcpu->kvm);
> +     ioapic_update_eoi_exitmap(apic->vcpu->kvm);
>  }
>  
>  static inline int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
> @@ -740,6 +731,19 @@ int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct 
> kvm_vcpu *vcpu2)
>       return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
>  }
>  
> +static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
> +{
> +     if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
> +         kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
> +             int trigger_mode;
> +             if (apic_test_vector(vector, apic->regs + APIC_TMR))
> +                     trigger_mode = IOAPIC_LEVEL_TRIG;
> +             else
> +                     trigger_mode = IOAPIC_EDGE_TRIG;
> +             kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
> +     }
> +}
> +
>  static int apic_set_eoi(struct kvm_lapic *apic)
>  {
>       int vector = apic_find_highest_isr(apic);
> @@ -756,19 +760,26 @@ static int apic_set_eoi(struct kvm_lapic *apic)
>       apic_clear_isr(vector, apic);
>       apic_update_ppr(apic);
>  
> -     if (!(kvm_apic_get_reg(apic, APIC_SPIV) & APIC_SPIV_DIRECTED_EOI) &&
> -         kvm_ioapic_handles_vector(apic->vcpu->kvm, vector)) {
> -             int trigger_mode;
> -             if (apic_test_vector(vector, apic->regs + APIC_TMR))
> -                     trigger_mode = IOAPIC_LEVEL_TRIG;
> -             else
> -                     trigger_mode = IOAPIC_EDGE_TRIG;
> -             kvm_ioapic_update_eoi(apic->vcpu->kvm, vector, trigger_mode);
> -     }
> +     kvm_ioapic_send_eoi(apic, vector);
>       kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
>       return vector;
>  }
>  
> +/*
> + * this interface assumes a trap-like exit, which has already finished
> + * desired side effect including vISR and vPPR update.
> + */
> +void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector)
> +{
> +     struct kvm_lapic *apic = vcpu->arch.apic;
> +
> +     trace_kvm_eoi(apic, vector);
> +
> +     kvm_ioapic_send_eoi(apic, vector);
> +     kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
> +}
> +EXPORT_SYMBOL_GPL(kvm_apic_set_eoi_accelerated);
> +
>  static void apic_send_ipi(struct kvm_lapic *apic)
>  {
>       u32 icr_low = kvm_apic_get_reg(apic, APIC_ICR);
> @@ -1071,6 +1082,7 @@ static int apic_reg_write(struct kvm_lapic *apic, u32 
> reg, u32 val)
>               if (!apic_x2apic_mode(apic)) {
>                       apic_set_reg(apic, APIC_DFR, val | 0x0FFFFFFF);
>                       recalculate_apic_map(apic->vcpu->kvm);
> +                     ioapic_update_eoi_exitmap(apic->vcpu->kvm);
>               } else
>                       ret = 1;
>               break;
> @@ -1318,6 +1330,7 @@ void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 
> value)
>               else
>                       static_key_slow_inc(&apic_hw_disabled.key);
>               recalculate_apic_map(vcpu->kvm);
> +             ioapic_update_eoi_exitmap(apic->vcpu->kvm);
>       }
>  
>       if (!kvm_vcpu_is_bsp(apic->vcpu))
> diff --git a/arch/x86/kvm/lapic.h b/arch/x86/kvm/lapic.h
> index 9a8ee22..9e3f888 100644
> --- a/arch/x86/kvm/lapic.h
> +++ b/arch/x86/kvm/lapic.h
> @@ -39,6 +39,7 @@ void kvm_free_lapic(struct kvm_vcpu *vcpu);
>  int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu);
>  int kvm_apic_accept_pic_intr(struct kvm_vcpu *vcpu);
>  int kvm_get_apic_interrupt(struct kvm_vcpu *vcpu);
> +int kvm_apic_get_highest_irr(struct kvm_vcpu *vcpu);
>  void kvm_lapic_reset(struct kvm_vcpu *vcpu);
>  u64 kvm_lapic_get_cr8(struct kvm_vcpu *vcpu);
>  void kvm_lapic_set_tpr(struct kvm_vcpu *vcpu, unsigned long cr8);
> @@ -65,6 +66,7 @@ u64 kvm_get_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu);
>  void kvm_set_lapic_tscdeadline_msr(struct kvm_vcpu *vcpu, u64 data);
>  
>  void kvm_apic_write_nodecode(struct kvm_vcpu *vcpu, u32 offset);
> +void kvm_apic_set_eoi_accelerated(struct kvm_vcpu *vcpu, int vector);
>  
>  void kvm_lapic_set_vapic_addr(struct kvm_vcpu *vcpu, gpa_t vapic_addr);
>  void kvm_lapic_sync_from_vapic(struct kvm_vcpu *vcpu);
> @@ -75,6 +77,7 @@ int kvm_x2apic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 
> *data);
>  
>  int kvm_hv_vapic_msr_write(struct kvm_vcpu *vcpu, u32 msr, u64 data);
>  int kvm_hv_vapic_msr_read(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
> +bool kvm_apic_present(struct kvm_vcpu *vcpu);
>  
>  static inline bool kvm_hv_vapic_assist_page_enabled(struct kvm_vcpu *vcpu)
>  {
> @@ -116,14 +119,31 @@ static inline int kvm_apic_sw_enabled(struct kvm_lapic 
> *apic)
>       return APIC_SPIV_APIC_ENABLED;
>  }
>  
> -static inline bool kvm_apic_present(struct kvm_vcpu *vcpu)
> +static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
>  {
> -     return kvm_vcpu_has_lapic(vcpu) && kvm_apic_hw_enabled(vcpu->arch.apic);
> +     return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
>  }
>  
> -static inline int kvm_lapic_enabled(struct kvm_vcpu *vcpu)
> +static inline bool kvm_apic_vid_enabled(struct kvm_vcpu *vcpu)
>  {
> -     return kvm_apic_present(vcpu) && kvm_apic_sw_enabled(vcpu->arch.apic);
> +     return kvm_x86_ops->has_virtual_interrupt_delivery(vcpu);
> +}
> +
> +static inline u16 apic_cluster_id(struct kvm_apic_map *map, u32 ldr)
> +{
> +     u16 cid;
> +     ldr >>= 32 - map->ldr_bits;
> +     cid = (ldr >> map->cid_shift) & map->cid_mask;
> +
> +     BUG_ON(cid >= ARRAY_SIZE(map->logical_map));
> +
> +     return cid;
> +}
> +
> +static inline u16 apic_logical_id(struct kvm_apic_map *map, u32 ldr)
> +{
> +     ldr >>= (32 - map->ldr_bits);
> +     return ldr & map->lid_mask;
>  }
>  
>  #endif
> diff --git a/arch/x86/kvm/svm.c b/arch/x86/kvm/svm.c
> index d29d3cd..951f1ec 100644
> --- a/arch/x86/kvm/svm.c
> +++ b/arch/x86/kvm/svm.c
> @@ -3571,6 +3571,26 @@ static void update_cr8_intercept(struct kvm_vcpu 
> *vcpu, int tpr, int irr)
>               set_cr_intercept(svm, INTERCEPT_CR8_WRITE);
>  }
>  
> +static int svm_has_virtual_interrupt_delivery(struct kvm_vcpu *vcpu)
> +{
> +     return 0;
> +}
> +
> +static void svm_update_eoi_exitmap(struct kvm *kvm, struct kvm_lapic_irq 
> *irq)
> +{
> +     return ;
> +}
> +
> +static void svm_reset_eoi_exitmap(struct kvm_vcpu *vcpu)
> +{
> +     return ;
> +}
> +
> +static void svm_load_eoi_exitmap(struct kvm_vcpu *vcpu)
> +{
> +     return ;
> +}
> +
>  static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
>  {
>       struct vcpu_svm *svm = to_svm(vcpu);
> @@ -4290,6 +4310,10 @@ static struct kvm_x86_ops svm_x86_ops = {
>       .enable_nmi_window = enable_nmi_window,
>       .enable_irq_window = enable_irq_window,
>       .update_cr8_intercept = update_cr8_intercept,
> +     .has_virtual_interrupt_delivery = svm_has_virtual_interrupt_delivery,
> +     .update_eoi_exitmap = svm_update_eoi_exitmap,
> +     .reset_eoi_exitmap = svm_reset_eoi_exitmap,
> +     .load_eoi_exitmap = svm_load_eoi_exitmap,
>  
>       .set_tss_addr = svm_set_tss_addr,
>       .get_tdp_level = get_npt_level,
> diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
> index 7ed26ec..be66c3e 100644
> --- a/arch/x86/kvm/vmx.c
> +++ b/arch/x86/kvm/vmx.c
> @@ -433,6 +433,8 @@ struct vcpu_vmx {
>  
>       bool rdtscp_enabled;
>  
> +     unsigned long eoi_exit_bitmap[4];
> +
>       /* Support for a guest hypervisor (nested VMX) */
>       struct nested_vmx nested;
>  };
> @@ -771,6 +773,12 @@ static inline bool cpu_has_vmx_apic_register_virt(void)
>               SECONDARY_EXEC_APIC_REGISTER_VIRT;
>  }
>  
> +static inline bool cpu_has_vmx_virtual_intr_delivery(void)
> +{
> +     return vmcs_config.cpu_based_2nd_exec_ctrl &
> +             SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
> +}
> +
>  static inline bool cpu_has_vmx_flexpriority(void)
>  {
>       return cpu_has_vmx_tpr_shadow() &&
> @@ -2549,7 +2557,8 @@ static __init int setup_vmcs_config(struct vmcs_config 
> *vmcs_conf)
>                       SECONDARY_EXEC_PAUSE_LOOP_EXITING |
>                       SECONDARY_EXEC_RDTSCP |
>                       SECONDARY_EXEC_ENABLE_INVPCID |
> -                     SECONDARY_EXEC_APIC_REGISTER_VIRT;
> +                     SECONDARY_EXEC_APIC_REGISTER_VIRT |
> +                     SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY;
>               if (adjust_vmx_controls(min2, opt2,
>                                       MSR_IA32_VMX_PROCBASED_CTLS2,
>                                       &_cpu_based_2nd_exec_control) < 0)
> @@ -2563,7 +2572,8 @@ static __init int setup_vmcs_config(struct vmcs_config 
> *vmcs_conf)
>  
>       if (!(_cpu_based_exec_control & CPU_BASED_TPR_SHADOW))
>               _cpu_based_2nd_exec_control &= ~(
> -                             SECONDARY_EXEC_APIC_REGISTER_VIRT);
> +                             SECONDARY_EXEC_APIC_REGISTER_VIRT |
> +                             SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
>  
>       if (_cpu_based_2nd_exec_control & SECONDARY_EXEC_ENABLE_EPT) {
>               /* CR3 accesses and invlpg don't need to cause VM Exits when EPT
> @@ -2762,9 +2772,15 @@ static __init int hardware_setup(void)
>       if (!cpu_has_vmx_ple())
>               ple_gap = 0;
>  
> -     if (!cpu_has_vmx_apic_register_virt())
> +     if (!cpu_has_vmx_apic_register_virt() ||
> +                             !cpu_has_vmx_virtual_intr_delivery())
>               enable_apicv_reg_vid = 0;
>  
> +     if (enable_apicv_reg_vid)
> +             kvm_x86_ops->update_cr8_intercept = NULL;
> +     else
> +             kvm_x86_ops->update_apic_irq = NULL;
> +
>       if (nested)
>               nested_vmx_setup_ctls_msrs();
>  
> @@ -3880,7 +3896,8 @@ static u32 vmx_secondary_exec_control(struct vcpu_vmx 
> *vmx)
>       if (!ple_gap)
>               exec_control &= ~SECONDARY_EXEC_PAUSE_LOOP_EXITING;
>       if (!enable_apicv_reg_vid)
> -             exec_control &= ~SECONDARY_EXEC_APIC_REGISTER_VIRT;
> +             exec_control &= ~(SECONDARY_EXEC_APIC_REGISTER_VIRT |
> +                               SECONDARY_EXEC_VIRTUAL_INTR_DELIVERY);
>       return exec_control;
>  }
>  
> @@ -3925,6 +3942,15 @@ static int vmx_vcpu_setup(struct vcpu_vmx *vmx)
>                               vmx_secondary_exec_control(vmx));
>       }
>  
> +     if (enable_apicv_reg_vid) {
> +             vmcs_write64(EOI_EXIT_BITMAP0, 0);
> +             vmcs_write64(EOI_EXIT_BITMAP1, 0);
> +             vmcs_write64(EOI_EXIT_BITMAP2, 0);
> +             vmcs_write64(EOI_EXIT_BITMAP3, 0);
> +
> +             vmcs_write16(GUEST_INTR_STATUS, 0);
> +     }

AFAICS SVI should be regenerated on migration. Consider:

1. vintr delivery, sets SVI = vector = RVI.
2. clears RVI.
3. migration.
4. RVI properly set from VIRR on entry.
5. SVI = 0.
6. EOI -> EOI virtualization with SVI = 0.

Could hook into kvm_apic_post_state_restore() to do that (set highest
index of bit set in VISR).

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to