KVM
Macros | Enumerations | Functions | Variables
lapic.c File Reference
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/smp.h>
#include <linux/hrtimer.h>
#include <linux/io.h>
#include <linux/export.h>
#include <linux/math64.h>
#include <linux/slab.h>
#include <asm/processor.h>
#include <asm/mce.h>
#include <asm/msr.h>
#include <asm/page.h>
#include <asm/current.h>
#include <asm/apicdef.h>
#include <asm/delay.h>
#include <linux/atomic.h>
#include <linux/jump_label.h>
#include "kvm_cache_regs.h"
#include "irq.h"
#include "ioapic.h"
#include "trace.h"
#include "x86.h"
#include "xen.h"
#include "cpuid.h"
#include "hyperv.h"
#include "smm.h"
Include dependency graph for lapic.c:

Go to the source code of this file.

Macros

#define pr_fmt(fmt)   KBUILD_MODNAME ": " fmt
 
#define mod_64(x, y)   ((x) - (y) * div64_u64(x, y))
 
#define APIC_VERSION   0x14UL
 
#define LAPIC_MMIO_LENGTH   (1 << 12)
 
#define MAX_APIC_VECTOR   256
 
#define APIC_VECTORS_PER_REG   32
 
#define LAPIC_TIMER_ADVANCE_ADJUST_MIN   100 /* clock cycles */
 
#define LAPIC_TIMER_ADVANCE_ADJUST_MAX   10000 /* clock cycles */
 
#define LAPIC_TIMER_ADVANCE_NS_INIT   1000
 
#define LAPIC_TIMER_ADVANCE_NS_MAX   5000
 
#define LAPIC_TIMER_ADVANCE_ADJUST_STEP   8
 
#define LVT_MASK    (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)
 
#define LINT_MASK
 
#define APIC_REG_MASK(reg)   (1ull << ((reg) >> 4))
 
#define APIC_REGS_MASK(first, count)    (APIC_REG_MASK(first) * ((1ull << (count)) - 1))
 

Enumerations

enum  { CLEAN , UPDATE_IN_PROGRESS , DIRTY }
 

Functions

static int kvm_lapic_msr_read (struct kvm_lapic *apic, u32 reg, u64 *data)
 
static int kvm_lapic_msr_write (struct kvm_lapic *apic, u32 reg, u64 data)
 
static void __kvm_lapic_set_reg (char *regs, int reg_off, u32 val)
 
static void kvm_lapic_set_reg (struct kvm_lapic *apic, int reg_off, u32 val)
 
static __always_inline u64 __kvm_lapic_get_reg64 (char *regs, int reg)
 
static __always_inline u64 kvm_lapic_get_reg64 (struct kvm_lapic *apic, int reg)
 
static __always_inline void __kvm_lapic_set_reg64 (char *regs, int reg, u64 val)
 
static __always_inline void kvm_lapic_set_reg64 (struct kvm_lapic *apic, int reg, u64 val)
 
static int apic_test_vector (int vec, void *bitmap)
 
bool kvm_apic_pending_eoi (struct kvm_vcpu *vcpu, int vector)
 
static int __apic_test_and_set_vector (int vec, void *bitmap)
 
static int __apic_test_and_clear_vector (int vec, void *bitmap)
 
__read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE (apic_hw_disabled, HZ)
 
__read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE (apic_sw_disabled, HZ)
 
static int apic_enabled (struct kvm_lapic *apic)
 
static u32 kvm_x2apic_id (struct kvm_lapic *apic)
 
static bool kvm_can_post_timer_interrupt (struct kvm_vcpu *vcpu)
 
bool kvm_can_use_hv_timer (struct kvm_vcpu *vcpu)
 
static bool kvm_use_posted_timer_interrupt (struct kvm_vcpu *vcpu)
 
static u32 kvm_apic_calc_x2apic_ldr (u32 id)
 
static bool kvm_apic_map_get_logical_dest (struct kvm_apic_map *map, u32 dest_id, struct kvm_lapic ***cluster, u16 *mask)
 
static void kvm_apic_map_free (struct rcu_head *rcu)
 
static int kvm_recalculate_phys_map (struct kvm_apic_map *new, struct kvm_vcpu *vcpu, bool *xapic_id_mismatch)
 
static void kvm_recalculate_logical_map (struct kvm_apic_map *new, struct kvm_vcpu *vcpu)
 
void kvm_recalculate_apic_map (struct kvm *kvm)
 
static void apic_set_spiv (struct kvm_lapic *apic, u32 val)
 
static void kvm_apic_set_xapic_id (struct kvm_lapic *apic, u8 id)
 
static void kvm_apic_set_ldr (struct kvm_lapic *apic, u32 id)
 
static void kvm_apic_set_dfr (struct kvm_lapic *apic, u32 val)
 
static void kvm_apic_set_x2apic_id (struct kvm_lapic *apic, u32 id)
 
static int apic_lvt_enabled (struct kvm_lapic *apic, int lvt_type)
 
static int apic_lvtt_oneshot (struct kvm_lapic *apic)
 
static int apic_lvtt_period (struct kvm_lapic *apic)
 
static int apic_lvtt_tscdeadline (struct kvm_lapic *apic)
 
static int apic_lvt_nmi_mode (u32 lvt_val)
 
static bool kvm_lapic_lvt_supported (struct kvm_lapic *apic, int lvt_index)
 
static int kvm_apic_calc_nr_lvt_entries (struct kvm_vcpu *vcpu)
 
void kvm_apic_set_version (struct kvm_vcpu *vcpu)
 
void kvm_apic_after_set_mcg_cap (struct kvm_vcpu *vcpu)
 
static int find_highest_vector (void *bitmap)
 
static u8 count_vectors (void *bitmap)
 
bool __kvm_apic_update_irr (u32 *pir, void *regs, int *max_irr)
 
 EXPORT_SYMBOL_GPL (__kvm_apic_update_irr)
 
bool kvm_apic_update_irr (struct kvm_vcpu *vcpu, u32 *pir, int *max_irr)
 
 EXPORT_SYMBOL_GPL (kvm_apic_update_irr)
 
static int apic_search_irr (struct kvm_lapic *apic)
 
static int apic_find_highest_irr (struct kvm_lapic *apic)
 
static void apic_clear_irr (int vec, struct kvm_lapic *apic)
 
void kvm_apic_clear_irr (struct kvm_vcpu *vcpu, int vec)
 
 EXPORT_SYMBOL_GPL (kvm_apic_clear_irr)
 
static void apic_set_isr (int vec, struct kvm_lapic *apic)
 
static int apic_find_highest_isr (struct kvm_lapic *apic)
 
static void apic_clear_isr (int vec, struct kvm_lapic *apic)
 
int kvm_lapic_find_highest_irr (struct kvm_vcpu *vcpu)
 
 EXPORT_SYMBOL_GPL (kvm_lapic_find_highest_irr)
 
static int __apic_accept_irq (struct kvm_lapic *apic, int delivery_mode, int vector, int level, int trig_mode, struct dest_map *dest_map)
 
int kvm_apic_set_irq (struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, struct dest_map *dest_map)
 
static int __pv_send_ipi (unsigned long *ipi_bitmap, struct kvm_apic_map *map, struct kvm_lapic_irq *irq, u32 min)
 
int kvm_pv_send_ipi (struct kvm *kvm, unsigned long ipi_bitmap_low, unsigned long ipi_bitmap_high, u32 min, unsigned long icr, int op_64_bit)
 
static int pv_eoi_put_user (struct kvm_vcpu *vcpu, u8 val)
 
static int pv_eoi_get_user (struct kvm_vcpu *vcpu, u8 *val)
 
static bool pv_eoi_enabled (struct kvm_vcpu *vcpu)
 
static void pv_eoi_set_pending (struct kvm_vcpu *vcpu)
 
static bool pv_eoi_test_and_clr_pending (struct kvm_vcpu *vcpu)
 
static int apic_has_interrupt_for_ppr (struct kvm_lapic *apic, u32 ppr)
 
static bool __apic_update_ppr (struct kvm_lapic *apic, u32 *new_ppr)
 
static void apic_update_ppr (struct kvm_lapic *apic)
 
void kvm_apic_update_ppr (struct kvm_vcpu *vcpu)
 
 EXPORT_SYMBOL_GPL (kvm_apic_update_ppr)
 
static void apic_set_tpr (struct kvm_lapic *apic, u32 tpr)
 
static bool kvm_apic_broadcast (struct kvm_lapic *apic, u32 mda)
 
static bool kvm_apic_match_physical_addr (struct kvm_lapic *apic, u32 mda)
 
static bool kvm_apic_match_logical_addr (struct kvm_lapic *apic, u32 mda)
 
static u32 kvm_apic_mda (struct kvm_vcpu *vcpu, unsigned int dest_id, struct kvm_lapic *source, struct kvm_lapic *target)
 
bool kvm_apic_match_dest (struct kvm_vcpu *vcpu, struct kvm_lapic *source, int shorthand, unsigned int dest, int dest_mode)
 
 EXPORT_SYMBOL_GPL (kvm_apic_match_dest)
 
int kvm_vector_to_index (u32 vector, u32 dest_vcpus, const unsigned long *bitmap, u32 bitmap_size)
 
static void kvm_apic_disabled_lapic_found (struct kvm *kvm)
 
static bool kvm_apic_is_broadcast_dest (struct kvm *kvm, struct kvm_lapic **src, struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
 
static bool kvm_apic_map_get_dest_lapic (struct kvm *kvm, struct kvm_lapic **src, struct kvm_lapic_irq *irq, struct kvm_apic_map *map, struct kvm_lapic ***dst, unsigned long *bitmap)
 
bool kvm_irq_delivery_to_apic_fast (struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, int *r, struct dest_map *dest_map)
 
bool kvm_intr_is_single_vcpu_fast (struct kvm *kvm, struct kvm_lapic_irq *irq, struct kvm_vcpu **dest_vcpu)
 
void kvm_bitmap_or_dest_vcpus (struct kvm *kvm, struct kvm_lapic_irq *irq, unsigned long *vcpu_bitmap)
 
int kvm_apic_compare_prio (struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
 
static bool kvm_ioapic_handles_vector (struct kvm_lapic *apic, int vector)
 
static void kvm_ioapic_send_eoi (struct kvm_lapic *apic, int vector)
 
static int apic_set_eoi (struct kvm_lapic *apic)
 
void kvm_apic_set_eoi_accelerated (struct kvm_vcpu *vcpu, int vector)
 
 EXPORT_SYMBOL_GPL (kvm_apic_set_eoi_accelerated)
 
void kvm_apic_send_ipi (struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
 
 EXPORT_SYMBOL_GPL (kvm_apic_send_ipi)
 
static u32 apic_get_tmcct (struct kvm_lapic *apic)
 
static void __report_tpr_access (struct kvm_lapic *apic, bool write)
 
static void report_tpr_access (struct kvm_lapic *apic, bool write)
 
static u32 __apic_read (struct kvm_lapic *apic, unsigned int offset)
 
static struct kvm_lapicto_lapic (struct kvm_io_device *dev)
 
u64 kvm_lapic_readable_reg_mask (struct kvm_lapic *apic)
 
 EXPORT_SYMBOL_GPL (kvm_lapic_readable_reg_mask)
 
static int kvm_lapic_reg_read (struct kvm_lapic *apic, u32 offset, int len, void *data)
 
static int apic_mmio_in_range (struct kvm_lapic *apic, gpa_t addr)
 
static int apic_mmio_read (struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t address, int len, void *data)
 
static void update_divide_count (struct kvm_lapic *apic)
 
static void limit_periodic_timer_frequency (struct kvm_lapic *apic)
 
static void cancel_hv_timer (struct kvm_lapic *apic)
 
static void cancel_apic_timer (struct kvm_lapic *apic)
 
static void apic_update_lvtt (struct kvm_lapic *apic)
 
static bool lapic_timer_int_injected (struct kvm_vcpu *vcpu)
 
static void __wait_lapic_expire (struct kvm_vcpu *vcpu, u64 guest_cycles)
 
static void adjust_lapic_timer_advance (struct kvm_vcpu *vcpu, s64 advance_expire_delta)
 
static void __kvm_wait_lapic_expire (struct kvm_vcpu *vcpu)
 
void kvm_wait_lapic_expire (struct kvm_vcpu *vcpu)
 
 EXPORT_SYMBOL_GPL (kvm_wait_lapic_expire)
 
static void kvm_apic_inject_pending_timer_irqs (struct kvm_lapic *apic)
 
static void apic_timer_expired (struct kvm_lapic *apic, bool from_timer_fn)
 
static void start_sw_tscdeadline (struct kvm_lapic *apic)
 
static u64 tmict_to_ns (struct kvm_lapic *apic, u32 tmict)
 
static void update_target_expiration (struct kvm_lapic *apic, uint32_t old_divisor)
 
static bool set_target_expiration (struct kvm_lapic *apic, u32 count_reg)
 
static void advance_periodic_target_expiration (struct kvm_lapic *apic)
 
static void start_sw_period (struct kvm_lapic *apic)
 
bool kvm_lapic_hv_timer_in_use (struct kvm_vcpu *vcpu)
 
static bool start_hv_timer (struct kvm_lapic *apic)
 
static void start_sw_timer (struct kvm_lapic *apic)
 
static void restart_apic_timer (struct kvm_lapic *apic)
 
void kvm_lapic_expired_hv_timer (struct kvm_vcpu *vcpu)
 
 EXPORT_SYMBOL_GPL (kvm_lapic_expired_hv_timer)
 
void kvm_lapic_switch_to_hv_timer (struct kvm_vcpu *vcpu)
 
void kvm_lapic_switch_to_sw_timer (struct kvm_vcpu *vcpu)
 
void kvm_lapic_restart_hv_timer (struct kvm_vcpu *vcpu)
 
static void __start_apic_timer (struct kvm_lapic *apic, u32 count_reg)
 
static void start_apic_timer (struct kvm_lapic *apic)
 
static void apic_manage_nmi_watchdog (struct kvm_lapic *apic, u32 lvt0_val)
 
static int get_lvt_index (u32 reg)
 
static int kvm_lapic_reg_write (struct kvm_lapic *apic, u32 reg, u32 val)
 
static int apic_mmio_write (struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t address, int len, const void *data)
 
void kvm_lapic_set_eoi (struct kvm_vcpu *vcpu)
 
 EXPORT_SYMBOL_GPL (kvm_lapic_set_eoi)
 
void kvm_apic_write_nodecode (struct kvm_vcpu *vcpu, u32 offset)
 
 EXPORT_SYMBOL_GPL (kvm_apic_write_nodecode)
 
void kvm_free_lapic (struct kvm_vcpu *vcpu)
 
u64 kvm_get_lapic_tscdeadline_msr (struct kvm_vcpu *vcpu)
 
void kvm_set_lapic_tscdeadline_msr (struct kvm_vcpu *vcpu, u64 data)
 
void kvm_lapic_set_tpr (struct kvm_vcpu *vcpu, unsigned long cr8)
 
u64 kvm_lapic_get_cr8 (struct kvm_vcpu *vcpu)
 
void kvm_lapic_set_base (struct kvm_vcpu *vcpu, u64 value)
 
void kvm_apic_update_apicv (struct kvm_vcpu *vcpu)
 
int kvm_alloc_apic_access_page (struct kvm *kvm)
 
 EXPORT_SYMBOL_GPL (kvm_alloc_apic_access_page)
 
void kvm_inhibit_apic_access_page (struct kvm_vcpu *vcpu)
 
void kvm_lapic_reset (struct kvm_vcpu *vcpu, bool init_event)
 
static bool lapic_is_periodic (struct kvm_lapic *apic)
 
int apic_has_pending_timer (struct kvm_vcpu *vcpu)
 
int kvm_apic_local_deliver (struct kvm_lapic *apic, int lvt_type)
 
void kvm_apic_nmi_wd_deliver (struct kvm_vcpu *vcpu)
 
static enum hrtimer_restart apic_timer_fn (struct hrtimer *data)
 
int kvm_create_lapic (struct kvm_vcpu *vcpu, int timer_advance_ns)
 
int kvm_apic_has_interrupt (struct kvm_vcpu *vcpu)
 
 EXPORT_SYMBOL_GPL (kvm_apic_has_interrupt)
 
int kvm_apic_accept_pic_intr (struct kvm_vcpu *vcpu)
 
void kvm_inject_apic_timer_irqs (struct kvm_vcpu *vcpu)
 
int kvm_get_apic_interrupt (struct kvm_vcpu *vcpu)
 
static int kvm_apic_state_fixup (struct kvm_vcpu *vcpu, struct kvm_lapic_state *s, bool set)
 
int kvm_apic_get_state (struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
 
int kvm_apic_set_state (struct kvm_vcpu *vcpu, struct kvm_lapic_state *s)
 
void __kvm_migrate_apic_timer (struct kvm_vcpu *vcpu)
 
static void apic_sync_pv_eoi_from_guest (struct kvm_vcpu *vcpu, struct kvm_lapic *apic)
 
void kvm_lapic_sync_from_vapic (struct kvm_vcpu *vcpu)
 
static void apic_sync_pv_eoi_to_guest (struct kvm_vcpu *vcpu, struct kvm_lapic *apic)
 
void kvm_lapic_sync_to_vapic (struct kvm_vcpu *vcpu)
 
int kvm_lapic_set_vapic_addr (struct kvm_vcpu *vcpu, gpa_t vapic_addr)
 
int kvm_x2apic_icr_write (struct kvm_lapic *apic, u64 data)
 
int kvm_x2apic_msr_write (struct kvm_vcpu *vcpu, u32 msr, u64 data)
 
int kvm_x2apic_msr_read (struct kvm_vcpu *vcpu, u32 msr, u64 *data)
 
int kvm_hv_vapic_msr_write (struct kvm_vcpu *vcpu, u32 reg, u64 data)
 
int kvm_hv_vapic_msr_read (struct kvm_vcpu *vcpu, u32 reg, u64 *data)
 
int kvm_lapic_set_pv_eoi (struct kvm_vcpu *vcpu, u64 data, unsigned long len)
 
int kvm_apic_accept_events (struct kvm_vcpu *vcpu)
 
void kvm_lapic_exit (void)
 

Variables

static bool lapic_timer_advance_dynamic __read_mostly
 
static const unsigned int apic_lvt_mask [KVM_APIC_MAX_NR_LVT_ENTRIES]
 
static const struct kvm_io_device_ops apic_mmio_ops
 

Macro Definition Documentation

◆ APIC_REG_MASK

#define APIC_REG_MASK (   reg)    (1ull << ((reg) >> 4))

Definition at line 1603 of file lapic.c.

◆ APIC_REGS_MASK

#define APIC_REGS_MASK (   first,
  count 
)     (APIC_REG_MASK(first) * ((1ull << (count)) - 1))

Definition at line 1604 of file lapic.c.

◆ APIC_VECTORS_PER_REG

#define APIC_VECTORS_PER_REG   32

Definition at line 60 of file lapic.c.

◆ APIC_VERSION

#define APIC_VERSION   0x14UL

Definition at line 56 of file lapic.c.

◆ LAPIC_MMIO_LENGTH

#define LAPIC_MMIO_LENGTH   (1 << 12)

Definition at line 57 of file lapic.c.

◆ LAPIC_TIMER_ADVANCE_ADJUST_MAX

#define LAPIC_TIMER_ADVANCE_ADJUST_MAX   10000 /* clock cycles */

Definition at line 64 of file lapic.c.

◆ LAPIC_TIMER_ADVANCE_ADJUST_MIN

#define LAPIC_TIMER_ADVANCE_ADJUST_MIN   100 /* clock cycles */

Definition at line 63 of file lapic.c.

◆ LAPIC_TIMER_ADVANCE_ADJUST_STEP

#define LAPIC_TIMER_ADVANCE_ADJUST_STEP   8

Definition at line 68 of file lapic.c.

◆ LAPIC_TIMER_ADVANCE_NS_INIT

#define LAPIC_TIMER_ADVANCE_NS_INIT   1000

Definition at line 65 of file lapic.c.

◆ LAPIC_TIMER_ADVANCE_NS_MAX

#define LAPIC_TIMER_ADVANCE_NS_MAX   5000

Definition at line 66 of file lapic.c.

◆ LINT_MASK

#define LINT_MASK
Value:
(LVT_MASK | APIC_MODE_MASK | APIC_INPUT_POLARITY | \
APIC_LVT_REMOTE_IRR | APIC_LVT_LEVEL_TRIGGER)
#define LVT_MASK
Definition: lapic.c:136

Definition at line 139 of file lapic.c.

◆ LVT_MASK

#define LVT_MASK    (APIC_LVT_MASKED | APIC_SEND_PENDING | APIC_VECTOR_MASK)

Definition at line 136 of file lapic.c.

◆ MAX_APIC_VECTOR

#define MAX_APIC_VECTOR   256

Definition at line 59 of file lapic.c.

◆ mod_64

#define mod_64 (   x,
 
)    ((x) - (y) * div64_u64(x, y))

Definition at line 50 of file lapic.c.

◆ pr_fmt

#define pr_fmt (   fmt)    KBUILD_MODNAME ": " fmt

Definition at line 18 of file lapic.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
CLEAN 
UPDATE_IN_PROGRESS 
DIRTY 

Definition at line 368 of file lapic.c.

368  {
369  CLEAN,
371  DIRTY
372 };
@ CLEAN
Definition: lapic.c:369
@ DIRTY
Definition: lapic.c:371
@ UPDATE_IN_PROGRESS
Definition: lapic.c:370

Function Documentation

◆ __apic_accept_irq()

static int __apic_accept_irq ( struct kvm_lapic apic,
int  delivery_mode,
int  vector,
int  level,
int  trig_mode,
struct dest_map dest_map 
)
static

Definition at line 1291 of file lapic.c.

1294 {
1295  int result = 0;
1296  struct kvm_vcpu *vcpu = apic->vcpu;
1297 
1298  trace_kvm_apic_accept_irq(vcpu->vcpu_id, delivery_mode,
1299  trig_mode, vector);
1300  switch (delivery_mode) {
1301  case APIC_DM_LOWEST:
1302  vcpu->arch.apic_arb_prio++;
1303  fallthrough;
1304  case APIC_DM_FIXED:
1305  if (unlikely(trig_mode && !level))
1306  break;
1307 
1308  /* FIXME add logic for vcpu on reset */
1309  if (unlikely(!apic_enabled(apic)))
1310  break;
1311 
1312  result = 1;
1313 
1314  if (dest_map) {
1315  __set_bit(vcpu->vcpu_id, dest_map->map);
1316  dest_map->vectors[vcpu->vcpu_id] = vector;
1317  }
1318 
1319  if (apic_test_vector(vector, apic->regs + APIC_TMR) != !!trig_mode) {
1320  if (trig_mode)
1321  kvm_lapic_set_vector(vector,
1322  apic->regs + APIC_TMR);
1323  else
1324  kvm_lapic_clear_vector(vector,
1325  apic->regs + APIC_TMR);
1326  }
1327 
1328  static_call(kvm_x86_deliver_interrupt)(apic, delivery_mode,
1329  trig_mode, vector);
1330  break;
1331 
1332  case APIC_DM_REMRD:
1333  result = 1;
1334  vcpu->arch.pv.pv_unhalted = 1;
1335  kvm_make_request(KVM_REQ_EVENT, vcpu);
1336  kvm_vcpu_kick(vcpu);
1337  break;
1338 
1339  case APIC_DM_SMI:
1340  if (!kvm_inject_smi(vcpu)) {
1341  kvm_vcpu_kick(vcpu);
1342  result = 1;
1343  }
1344  break;
1345 
1346  case APIC_DM_NMI:
1347  result = 1;
1348  kvm_inject_nmi(vcpu);
1349  kvm_vcpu_kick(vcpu);
1350  break;
1351 
1352  case APIC_DM_INIT:
1353  if (!trig_mode || level) {
1354  result = 1;
1355  /* assumes that there are only KVM_APIC_INIT/SIPI */
1356  apic->pending_events = (1UL << KVM_APIC_INIT);
1357  kvm_make_request(KVM_REQ_EVENT, vcpu);
1358  kvm_vcpu_kick(vcpu);
1359  }
1360  break;
1361 
1362  case APIC_DM_STARTUP:
1363  result = 1;
1364  apic->sipi_vector = vector;
1365  /* make sure sipi_vector is visible for the receiver */
1366  smp_wmb();
1367  set_bit(KVM_APIC_SIPI, &apic->pending_events);
1368  kvm_make_request(KVM_REQ_EVENT, vcpu);
1369  kvm_vcpu_kick(vcpu);
1370  break;
1371 
1372  case APIC_DM_EXTINT:
1373  /*
1374  * Should only be called by kvm_apic_local_deliver() with LVT0,
1375  * before NMI watchdog was enabled. Already handled by
1376  * kvm_apic_accept_pic_intr().
1377  */
1378  break;
1379 
1380  default:
1381  printk(KERN_ERR "TODO: unsupported delivery mode %x\n",
1382  delivery_mode);
1383  break;
1384  }
1385  return result;
1386 }
void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
Definition: kvm_main.c:3931
static int apic_enabled(struct kvm_lapic *apic)
Definition: lapic.c:131
static int apic_test_vector(int vec, void *bitmap)
Definition: lapic.c:105
#define KVM_APIC_INIT
Definition: lapic.h:12
static void kvm_lapic_clear_vector(int vec, void *bitmap)
Definition: lapic.h:154
static void kvm_lapic_set_vector(int vec, void *bitmap)
Definition: lapic.h:159
#define KVM_APIC_SIPI
Definition: lapic.h:13
static int kvm_inject_smi(struct kvm_vcpu *vcpu)
Definition: smm.h:159
u8 vectors[KVM_MAX_VCPU_IDS]
Definition: ioapic.h:48
struct kvm_vcpu * vcpu
Definition: lapic.h:64
unsigned int sipi_vector
Definition: lapic.h:82
unsigned long pending_events
Definition: lapic.h:81
void * regs
Definition: lapic.h:78
void kvm_inject_nmi(struct kvm_vcpu *vcpu)
Definition: x86.c:818
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __apic_read()

static u32 __apic_read ( struct kvm_lapic apic,
unsigned int  offset 
)
static

Definition at line 1566 of file lapic.c.

1567 {
1568  u32 val = 0;
1569 
1570  if (offset >= LAPIC_MMIO_LENGTH)
1571  return 0;
1572 
1573  switch (offset) {
1574  case APIC_ARBPRI:
1575  break;
1576 
1577  case APIC_TMCCT: /* Timer CCR */
1578  if (apic_lvtt_tscdeadline(apic))
1579  return 0;
1580 
1581  val = apic_get_tmcct(apic);
1582  break;
1583  case APIC_PROCPRI:
1584  apic_update_ppr(apic);
1585  val = kvm_lapic_get_reg(apic, offset);
1586  break;
1587  case APIC_TASKPRI:
1588  report_tpr_access(apic, false);
1589  fallthrough;
1590  default:
1591  val = kvm_lapic_get_reg(apic, offset);
1592  break;
1593  }
1594 
1595  return val;
1596 }
#define LAPIC_MMIO_LENGTH
Definition: lapic.c:57
static u32 apic_get_tmcct(struct kvm_lapic *apic)
Definition: lapic.c:1529
static void apic_update_ppr(struct kvm_lapic *apic)
Definition: lapic.c:966
static void report_tpr_access(struct kvm_lapic *apic, bool write)
Definition: lapic.c:1560
static int apic_lvtt_tscdeadline(struct kvm_lapic *apic)
Definition: lapic.c:553
static u32 kvm_lapic_get_reg(struct kvm_lapic *apic, int reg_off)
Definition: lapic.h:179
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __apic_test_and_clear_vector()

static int __apic_test_and_clear_vector ( int  vec,
void *  bitmap 
)
inlinestatic

Definition at line 123 of file lapic.c.

124 {
125  return __test_and_clear_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
126 }
#define VEC_POS(v)
Definition: lapic.h:151
#define REG_POS(v)
Definition: lapic.h:152
Here is the caller graph for this function:

◆ __apic_test_and_set_vector()

static int __apic_test_and_set_vector ( int  vec,
void *  bitmap 
)
inlinestatic

Definition at line 118 of file lapic.c.

119 {
120  return __test_and_set_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
121 }
Here is the caller graph for this function:

◆ __apic_update_ppr()

static bool __apic_update_ppr ( struct kvm_lapic apic,
u32 *  new_ppr 
)
static

Definition at line 944 of file lapic.c.

945 {
946  u32 tpr, isrv, ppr, old_ppr;
947  int isr;
948 
949  old_ppr = kvm_lapic_get_reg(apic, APIC_PROCPRI);
950  tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI);
951  isr = apic_find_highest_isr(apic);
952  isrv = (isr != -1) ? isr : 0;
953 
954  if ((tpr & 0xf0) >= (isrv & 0xf0))
955  ppr = tpr & 0xff;
956  else
957  ppr = isrv & 0xf0;
958 
959  *new_ppr = ppr;
960  if (old_ppr != ppr)
961  kvm_lapic_set_reg(apic, APIC_PROCPRI, ppr);
962 
963  return ppr < old_ppr;
964 }
static int apic_find_highest_isr(struct kvm_lapic *apic)
Definition: lapic.c:768
static void kvm_lapic_set_reg(struct kvm_lapic *apic, int reg_off, u32 val)
Definition: lapic.c:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __kvm_apic_update_irr()

bool __kvm_apic_update_irr ( u32 *  pir,
void *  regs,
int *  max_irr 
)

Definition at line 654 of file lapic.c.

655 {
656  u32 i, vec;
657  u32 pir_val, irr_val, prev_irr_val;
658  int max_updated_irr;
659 
660  max_updated_irr = -1;
661  *max_irr = -1;
662 
663  for (i = vec = 0; i <= 7; i++, vec += 32) {
664  u32 *p_irr = (u32 *)(regs + APIC_IRR + i * 0x10);
665 
666  irr_val = *p_irr;
667  pir_val = READ_ONCE(pir[i]);
668 
669  if (pir_val) {
670  pir_val = xchg(&pir[i], 0);
671 
672  prev_irr_val = irr_val;
673  do {
674  irr_val = prev_irr_val | pir_val;
675  } while (prev_irr_val != irr_val &&
676  !try_cmpxchg(p_irr, &prev_irr_val, irr_val));
677 
678  if (prev_irr_val != irr_val)
679  max_updated_irr = __fls(irr_val ^ prev_irr_val) + vec;
680  }
681  if (irr_val)
682  *max_irr = __fls(irr_val) + vec;
683  }
684 
685  return ((max_updated_irr != -1) &&
686  (max_updated_irr == *max_irr));
687 }
u32 pir[8]
Definition: posted_intr.h:0
Here is the caller graph for this function:

◆ __kvm_lapic_get_reg64()

static __always_inline u64 __kvm_lapic_get_reg64 ( char *  regs,
int  reg 
)
static

Definition at line 82 of file lapic.c.

83 {
84  BUILD_BUG_ON(reg != APIC_ICR);
85  return *((u64 *) (regs + reg));
86 }
Here is the caller graph for this function:

◆ __kvm_lapic_set_reg()

static void __kvm_lapic_set_reg ( char *  regs,
int  reg_off,
u32  val 
)
inlinestatic

Definition at line 72 of file lapic.c.

73 {
74  *((u32 *) (regs + reg_off)) = val;
75 }
Here is the caller graph for this function:

◆ __kvm_lapic_set_reg64()

static __always_inline void __kvm_lapic_set_reg64 ( char *  regs,
int  reg,
u64  val 
)
static

Definition at line 93 of file lapic.c.

94 {
95  BUILD_BUG_ON(reg != APIC_ICR);
96  *((u64 *) (regs + reg)) = val;
97 }
Here is the caller graph for this function:

◆ __kvm_migrate_apic_timer()

void __kvm_migrate_apic_timer ( struct kvm_vcpu *  vcpu)

Definition at line 3029 of file lapic.c.

3030 {
3031  struct hrtimer *timer;
3032 
3033  if (!lapic_in_kernel(vcpu) ||
3035  return;
3036 
3037  timer = &vcpu->arch.apic->lapic_timer.timer;
3038  if (hrtimer_cancel(timer))
3039  hrtimer_start_expires(timer, HRTIMER_MODE_ABS_HARD);
3040 }
static bool kvm_can_post_timer_interrupt(struct kvm_vcpu *vcpu)
Definition: lapic.c:148
static bool lapic_in_kernel(struct kvm_vcpu *vcpu)
Definition: lapic.h:186
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __kvm_wait_lapic_expire()

static void __kvm_wait_lapic_expire ( struct kvm_vcpu *  vcpu)
static

Definition at line 1844 of file lapic.c.

1845 {
1846  struct kvm_lapic *apic = vcpu->arch.apic;
1847  u64 guest_tsc, tsc_deadline;
1848 
1849  tsc_deadline = apic->lapic_timer.expired_tscdeadline;
1850  apic->lapic_timer.expired_tscdeadline = 0;
1851  guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1852  trace_kvm_wait_lapic_expire(vcpu->vcpu_id, guest_tsc - tsc_deadline);
1853 
1854  if (lapic_timer_advance_dynamic) {
1855  adjust_lapic_timer_advance(vcpu, guest_tsc - tsc_deadline);
1856  /*
1857  * If the timer fired early, reread the TSC to account for the
1858  * overhead of the above adjustment to avoid waiting longer
1859  * than is necessary.
1860  */
1861  if (guest_tsc < tsc_deadline)
1862  guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1863  }
1864 
1865  if (guest_tsc < tsc_deadline)
1866  __wait_lapic_expire(vcpu, tsc_deadline - guest_tsc);
1867 }
static void __wait_lapic_expire(struct kvm_vcpu *vcpu, u64 guest_cycles)
Definition: lapic.c:1795
static void adjust_lapic_timer_advance(struct kvm_vcpu *vcpu, s64 advance_expire_delta)
Definition: lapic.c:1815
struct kvm_timer lapic_timer
Definition: lapic.h:62
u64 expired_tscdeadline
Definition: lapic.h:53
u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
Definition: x86.c:2583
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __pv_send_ipi()

static int __pv_send_ipi ( unsigned long *  ipi_bitmap,
struct kvm_apic_map *  map,
struct kvm_lapic_irq *  irq,
u32  min 
)
static

Definition at line 832 of file lapic.c.

834 {
835  int i, count = 0;
836  struct kvm_vcpu *vcpu;
837 
838  if (min > map->max_apic_id)
839  return 0;
840 
841  for_each_set_bit(i, ipi_bitmap,
842  min((u32)BITS_PER_LONG, (map->max_apic_id - min + 1))) {
843  if (map->phys_map[min + i]) {
844  vcpu = map->phys_map[min + i]->vcpu;
845  count += kvm_apic_set_irq(vcpu, irq, NULL);
846  }
847  }
848 
849  return count;
850 }
int kvm_apic_set_irq(struct kvm_vcpu *vcpu, struct kvm_lapic_irq *irq, struct dest_map *dest_map)
Definition: lapic.c:823
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __report_tpr_access()

static void __report_tpr_access ( struct kvm_lapic apic,
bool  write 
)
static

Definition at line 1550 of file lapic.c.

1551 {
1552  struct kvm_vcpu *vcpu = apic->vcpu;
1553  struct kvm_run *run = vcpu->run;
1554 
1555  kvm_make_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu);
1556  run->tpr_access.rip = kvm_rip_read(vcpu);
1557  run->tpr_access.is_write = write;
1558 }
static unsigned long kvm_rip_read(struct kvm_vcpu *vcpu)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __start_apic_timer()

static void __start_apic_timer ( struct kvm_lapic apic,
u32  count_reg 
)
static

Definition at line 2216 of file lapic.c.

2217 {
2218  atomic_set(&apic->lapic_timer.pending, 0);
2219 
2220  if ((apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
2221  && !set_target_expiration(apic, count_reg))
2222  return;
2223 
2224  restart_apic_timer(apic);
2225 }
static bool set_target_expiration(struct kvm_lapic *apic, u32 count_reg)
Definition: lapic.c:1992
static int apic_lvtt_period(struct kvm_lapic *apic)
Definition: lapic.c:548
static void restart_apic_timer(struct kvm_lapic *apic)
Definition: lapic.c:2158
static int apic_lvtt_oneshot(struct kvm_lapic *apic)
Definition: lapic.c:543
atomic_t pending
Definition: lapic.h:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ __wait_lapic_expire()

static void __wait_lapic_expire ( struct kvm_vcpu *  vcpu,
u64  guest_cycles 
)
inlinestatic

Definition at line 1795 of file lapic.c.

1796 {
1797  u64 timer_advance_ns = vcpu->arch.apic->lapic_timer.timer_advance_ns;
1798 
1799  /*
1800  * If the guest TSC is running at a different ratio than the host, then
1801  * convert the delay to nanoseconds to achieve an accurate delay. Note
1802  * that __delay() uses delay_tsc whenever the hardware has TSC, thus
1803  * always for VMX enabled hardware.
1804  */
1805  if (vcpu->arch.tsc_scaling_ratio == kvm_caps.default_tsc_scaling_ratio) {
1806  __delay(min(guest_cycles,
1807  nsec_to_cycles(vcpu, timer_advance_ns)));
1808  } else {
1809  u64 delay_ns = guest_cycles * 1000000ULL;
1810  do_div(delay_ns, vcpu->arch.virtual_tsc_khz);
1811  ndelay(min_t(u32, delay_ns, timer_advance_ns));
1812  }
1813 }
Definition: x86.h:12
u64 default_tsc_scaling_ratio
Definition: x86.h:22
static u64 nsec_to_cycles(struct kvm_vcpu *vcpu, u64 nsec)
Definition: x86.h:389
Here is the call graph for this function:
Here is the caller graph for this function:

◆ adjust_lapic_timer_advance()

static void adjust_lapic_timer_advance ( struct kvm_vcpu *  vcpu,
s64  advance_expire_delta 
)
inlinestatic

Definition at line 1815 of file lapic.c.

1817 {
1818  struct kvm_lapic *apic = vcpu->arch.apic;
1819  u32 timer_advance_ns = apic->lapic_timer.timer_advance_ns;
1820  u64 ns;
1821 
1822  /* Do not adjust for tiny fluctuations or large random spikes. */
1823  if (abs(advance_expire_delta) > LAPIC_TIMER_ADVANCE_ADJUST_MAX ||
1824  abs(advance_expire_delta) < LAPIC_TIMER_ADVANCE_ADJUST_MIN)
1825  return;
1826 
1827  /* too early */
1828  if (advance_expire_delta < 0) {
1829  ns = -advance_expire_delta * 1000000ULL;
1830  do_div(ns, vcpu->arch.virtual_tsc_khz);
1831  timer_advance_ns -= ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
1832  } else {
1833  /* too late */
1834  ns = advance_expire_delta * 1000000ULL;
1835  do_div(ns, vcpu->arch.virtual_tsc_khz);
1836  timer_advance_ns += ns/LAPIC_TIMER_ADVANCE_ADJUST_STEP;
1837  }
1838 
1839  if (unlikely(timer_advance_ns > LAPIC_TIMER_ADVANCE_NS_MAX))
1840  timer_advance_ns = LAPIC_TIMER_ADVANCE_NS_INIT;
1841  apic->lapic_timer.timer_advance_ns = timer_advance_ns;
1842 }
#define LAPIC_TIMER_ADVANCE_NS_MAX
Definition: lapic.c:66
#define LAPIC_TIMER_ADVANCE_ADJUST_MIN
Definition: lapic.c:63
#define LAPIC_TIMER_ADVANCE_ADJUST_STEP
Definition: lapic.c:68
#define LAPIC_TIMER_ADVANCE_NS_INIT
Definition: lapic.c:65
#define LAPIC_TIMER_ADVANCE_ADJUST_MAX
Definition: lapic.c:64
u32 timer_advance_ns
Definition: lapic.h:54
Here is the caller graph for this function:

◆ advance_periodic_target_expiration()

static void advance_periodic_target_expiration ( struct kvm_lapic apic)
static

Definition at line 2042 of file lapic.c.

2043 {
2044  ktime_t now = ktime_get();
2045  u64 tscl = rdtsc();
2046  ktime_t delta;
2047 
2048  /*
2049  * Synchronize both deadlines to the same time source or
2050  * differences in the periods (caused by differences in the
2051  * underlying clocks or numerical approximation errors) will
2052  * cause the two to drift apart over time as the errors
2053  * accumulate.
2054  */
2056  ktime_add_ns(apic->lapic_timer.target_expiration,
2057  apic->lapic_timer.period);
2058  delta = ktime_sub(apic->lapic_timer.target_expiration, now);
2059  apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
2060  nsec_to_cycles(apic->vcpu, delta);
2061 }
ktime_t target_expiration
Definition: lapic.h:49
s64 period
Definition: lapic.h:48
u64 tscdeadline
Definition: lapic.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_clear_irr()

static void apic_clear_irr ( int  vec,
struct kvm_lapic apic 
)
inlinestatic

Definition at line 723 of file lapic.c.

724 {
725  if (unlikely(apic->apicv_active)) {
726  /* need to update RVI */
727  kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
728  static_call_cond(kvm_x86_hwapic_irr_update)(apic->vcpu,
729  apic_find_highest_irr(apic));
730  } else {
731  apic->irr_pending = false;
732  kvm_lapic_clear_vector(vec, apic->regs + APIC_IRR);
733  if (apic_search_irr(apic) != -1)
734  apic->irr_pending = true;
735  }
736 }
static int apic_search_irr(struct kvm_lapic *apic)
Definition: lapic.c:701
static int apic_find_highest_irr(struct kvm_lapic *apic)
Definition: lapic.c:706
bool apicv_active
Definition: lapic.h:65
bool irr_pending
Definition: lapic.h:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_clear_isr()

static void apic_clear_isr ( int  vec,
struct kvm_lapic apic 
)
inlinestatic

Definition at line 787 of file lapic.c.

788 {
789  if (!__apic_test_and_clear_vector(vec, apic->regs + APIC_ISR))
790  return;
791 
792  /*
793  * We do get here for APIC virtualization enabled if the guest
794  * uses the Hyper-V APIC enlightenment. In this case we may need
795  * to trigger a new interrupt delivery by writing the SVI field;
796  * on the other hand isr_count and highest_isr_cache are unused
797  * and must be left alone.
798  */
799  if (unlikely(apic->apicv_active))
800  static_call_cond(kvm_x86_hwapic_isr_update)(apic_find_highest_isr(apic));
801  else {
802  --apic->isr_count;
803  BUG_ON(apic->isr_count < 0);
804  apic->highest_isr_cache = -1;
805  }
806 }
static int __apic_test_and_clear_vector(int vec, void *bitmap)
Definition: lapic.c:123
int highest_isr_cache
Definition: lapic.h:72
s16 isr_count
Definition: lapic.h:70
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_enabled()

static int apic_enabled ( struct kvm_lapic apic)
inlinestatic

Definition at line 131 of file lapic.c.

132 {
133  return kvm_apic_sw_enabled(apic) && kvm_apic_hw_enabled(apic);
134 }
static bool kvm_apic_hw_enabled(struct kvm_lapic *apic)
Definition: lapic.h:195
static bool kvm_apic_sw_enabled(struct kvm_lapic *apic)
Definition: lapic.h:204
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_find_highest_irr()

static int apic_find_highest_irr ( struct kvm_lapic apic)
inlinestatic

Definition at line 706 of file lapic.c.

707 {
708  int result;
709 
710  /*
711  * Note that irr_pending is just a hint. It will be always
712  * true with virtual interrupt delivery enabled.
713  */
714  if (!apic->irr_pending)
715  return -1;
716 
717  result = apic_search_irr(apic);
718  ASSERT(result == -1 || result >= 16);
719 
720  return result;
721 }
#define ASSERT(x)
Definition: ioapic.h:101
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_find_highest_isr()

static int apic_find_highest_isr ( struct kvm_lapic apic)
inlinestatic

Definition at line 768 of file lapic.c.

769 {
770  int result;
771 
772  /*
773  * Note that isr_count is always 1, and highest_isr_cache
774  * is always -1, with APIC virtualization enabled.
775  */
776  if (!apic->isr_count)
777  return -1;
778  if (likely(apic->highest_isr_cache != -1))
779  return apic->highest_isr_cache;
780 
781  result = find_highest_vector(apic->regs + APIC_ISR);
782  ASSERT(result == -1 || result >= 16);
783 
784  return result;
785 }
static int find_highest_vector(void *bitmap)
Definition: lapic.c:625
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_get_tmcct()

static u32 apic_get_tmcct ( struct kvm_lapic apic)
static

Definition at line 1529 of file lapic.c.

1530 {
1531  ktime_t remaining, now;
1532  s64 ns;
1533 
1534  ASSERT(apic != NULL);
1535 
1536  /* if initial count is 0, current count should also be 0 */
1537  if (kvm_lapic_get_reg(apic, APIC_TMICT) == 0 ||
1538  apic->lapic_timer.period == 0)
1539  return 0;
1540 
1541  now = ktime_get();
1542  remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1543  if (ktime_to_ns(remaining) < 0)
1544  remaining = 0;
1545 
1546  ns = mod_64(ktime_to_ns(remaining), apic->lapic_timer.period);
1547  return div64_u64(ns, (APIC_BUS_CYCLE_NS * apic->divide_count));
1548 }
#define mod_64(x, y)
Definition: lapic.c:50
#define APIC_BUS_CYCLE_NS
Definition: lapic.h:19
u32 divide_count
Definition: lapic.h:63
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_has_interrupt_for_ppr()

static int apic_has_interrupt_for_ppr ( struct kvm_lapic apic,
u32  ppr 
)
static

Definition at line 932 of file lapic.c.

933 {
934  int highest_irr;
935  if (kvm_x86_ops.sync_pir_to_irr)
936  highest_irr = static_call(kvm_x86_sync_pir_to_irr)(apic->vcpu);
937  else
938  highest_irr = apic_find_highest_irr(apic);
939  if (highest_irr == -1 || (highest_irr & 0xF0) <= ppr)
940  return -1;
941  return highest_irr;
942 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_has_pending_timer()

int apic_has_pending_timer ( struct kvm_vcpu *  vcpu)

Definition at line 2752 of file lapic.c.

2753 {
2754  struct kvm_lapic *apic = vcpu->arch.apic;
2755 
2756  if (apic_enabled(apic) && apic_lvt_enabled(apic, APIC_LVTT))
2757  return atomic_read(&apic->lapic_timer.pending);
2758 
2759  return 0;
2760 }
static int apic_lvt_enabled(struct kvm_lapic *apic, int lvt_type)
Definition: lapic.c:538
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_lvt_enabled()

static int apic_lvt_enabled ( struct kvm_lapic apic,
int  lvt_type 
)
inlinestatic

Definition at line 538 of file lapic.c.

539 {
540  return !(kvm_lapic_get_reg(apic, lvt_type) & APIC_LVT_MASKED);
541 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_lvt_nmi_mode()

static int apic_lvt_nmi_mode ( u32  lvt_val)
inlinestatic

Definition at line 558 of file lapic.c.

559 {
560  return (lvt_val & (APIC_MODE_MASK | APIC_LVT_MASKED)) == APIC_DM_NMI;
561 }
Here is the caller graph for this function:

◆ apic_lvtt_oneshot()

static int apic_lvtt_oneshot ( struct kvm_lapic apic)
inlinestatic

Definition at line 543 of file lapic.c.

544 {
545  return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_ONESHOT;
546 }
u32 timer_mode
Definition: lapic.h:50
Here is the caller graph for this function:

◆ apic_lvtt_period()

static int apic_lvtt_period ( struct kvm_lapic apic)
inlinestatic

Definition at line 548 of file lapic.c.

549 {
550  return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_PERIODIC;
551 }
Here is the caller graph for this function:

◆ apic_lvtt_tscdeadline()

static int apic_lvtt_tscdeadline ( struct kvm_lapic apic)
inlinestatic

Definition at line 553 of file lapic.c.

554 {
555  return apic->lapic_timer.timer_mode == APIC_LVT_TIMER_TSCDEADLINE;
556 }
Here is the caller graph for this function:

◆ apic_manage_nmi_watchdog()

static void apic_manage_nmi_watchdog ( struct kvm_lapic apic,
u32  lvt0_val 
)
static

Definition at line 2232 of file lapic.c.

2233 {
2234  bool lvt0_in_nmi_mode = apic_lvt_nmi_mode(lvt0_val);
2235 
2236  if (apic->lvt0_in_nmi_mode != lvt0_in_nmi_mode) {
2237  apic->lvt0_in_nmi_mode = lvt0_in_nmi_mode;
2238  if (lvt0_in_nmi_mode) {
2239  atomic_inc(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
2240  } else
2241  atomic_dec(&apic->vcpu->kvm->arch.vapics_in_nmi_mode);
2242  }
2243 }
static int apic_lvt_nmi_mode(u32 lvt_val)
Definition: lapic.c:558
bool lvt0_in_nmi_mode
Definition: lapic.h:68
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_mmio_in_range()

static int apic_mmio_in_range ( struct kvm_lapic apic,
gpa_t  addr 
)
static

Definition at line 1682 of file lapic.c.

1683 {
1684  return addr >= apic->base_address &&
1685  addr < apic->base_address + LAPIC_MMIO_LENGTH;
1686 }
unsigned long base_address
Definition: lapic.h:60
Here is the caller graph for this function:

◆ apic_mmio_read()

static int apic_mmio_read ( struct kvm_vcpu *  vcpu,
struct kvm_io_device this,
gpa_t  address,
int  len,
void *  data 
)
static

Definition at line 1688 of file lapic.c.

1690 {
1691  struct kvm_lapic *apic = to_lapic(this);
1692  u32 offset = address - apic->base_address;
1693 
1694  if (!apic_mmio_in_range(apic, address))
1695  return -EOPNOTSUPP;
1696 
1697  if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
1698  if (!kvm_check_has_quirk(vcpu->kvm,
1699  KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
1700  return -EOPNOTSUPP;
1701 
1702  memset(data, 0xff, len);
1703  return 0;
1704  }
1705 
1706  kvm_lapic_reg_read(apic, offset, len, data);
1707 
1708  return 0;
1709 }
static int apic_mmio_in_range(struct kvm_lapic *apic, gpa_t addr)
Definition: lapic.c:1682
static struct kvm_lapic * to_lapic(struct kvm_io_device *dev)
Definition: lapic.c:1598
static int kvm_lapic_reg_read(struct kvm_lapic *apic, u32 offset, int len, void *data)
Definition: lapic.c:1645
static int apic_x2apic_mode(struct kvm_lapic *apic)
Definition: lapic.h:221
static bool kvm_check_has_quirk(struct kvm *kvm, u64 quirk)
Definition: x86.h:288
Here is the call graph for this function:

◆ apic_mmio_write()

static int apic_mmio_write ( struct kvm_vcpu *  vcpu,
struct kvm_io_device this,
gpa_t  address,
int  len,
const void *  data 
)
static

Definition at line 2406 of file lapic.c.

2408 {
2409  struct kvm_lapic *apic = to_lapic(this);
2410  unsigned int offset = address - apic->base_address;
2411  u32 val;
2412 
2413  if (!apic_mmio_in_range(apic, address))
2414  return -EOPNOTSUPP;
2415 
2416  if (!kvm_apic_hw_enabled(apic) || apic_x2apic_mode(apic)) {
2417  if (!kvm_check_has_quirk(vcpu->kvm,
2418  KVM_X86_QUIRK_LAPIC_MMIO_HOLE))
2419  return -EOPNOTSUPP;
2420 
2421  return 0;
2422  }
2423 
2424  /*
2425  * APIC register must be aligned on 128-bits boundary.
2426  * 32/64/128 bits registers must be accessed thru 32 bits.
2427  * Refer SDM 8.4.1
2428  */
2429  if (len != 4 || (offset & 0xf))
2430  return 0;
2431 
2432  val = *(u32*)data;
2433 
2434  kvm_lapic_reg_write(apic, offset & 0xff0, val);
2435 
2436  return 0;
2437 }
static int kvm_lapic_reg_write(struct kvm_lapic *apic, u32 reg, u32 val)
Definition: lapic.c:2255
Here is the call graph for this function:

◆ apic_search_irr()

static int apic_search_irr ( struct kvm_lapic apic)
inlinestatic

Definition at line 701 of file lapic.c.

702 {
703  return find_highest_vector(apic->regs + APIC_IRR);
704 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_set_eoi()

static int apic_set_eoi ( struct kvm_lapic apic)
static

Definition at line 1465 of file lapic.c.

1466 {
1467  int vector = apic_find_highest_isr(apic);
1468 
1469  trace_kvm_eoi(apic, vector);
1470 
1471  /*
1472  * Not every write EOI will has corresponding ISR,
1473  * one example is when Kernel check timer on setup_IO_APIC
1474  */
1475  if (vector == -1)
1476  return vector;
1477 
1478  apic_clear_isr(vector, apic);
1479  apic_update_ppr(apic);
1480 
1481  if (kvm_hv_synic_has_vector(apic->vcpu, vector))
1482  kvm_hv_synic_send_eoi(apic->vcpu, vector);
1483 
1484  kvm_ioapic_send_eoi(apic, vector);
1485  kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1486  return vector;
1487 }
void kvm_hv_synic_send_eoi(struct kvm_vcpu *vcpu, int vector)
Definition: hyperv.c:511
static bool kvm_hv_synic_has_vector(struct kvm_vcpu *vcpu, int vector)
Definition: hyperv.h:290
static void kvm_ioapic_send_eoi(struct kvm_lapic *apic, int vector)
Definition: lapic.c:1442
static void apic_clear_isr(int vec, struct kvm_lapic *apic)
Definition: lapic.c:787
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_set_isr()

static void apic_set_isr ( int  vec,
struct kvm_lapic apic 
)
inlinestatic

Definition at line 744 of file lapic.c.

745 {
746  if (__apic_test_and_set_vector(vec, apic->regs + APIC_ISR))
747  return;
748 
749  /*
750  * With APIC virtualization enabled, all caching is disabled
751  * because the processor can modify ISR under the hood. Instead
752  * just set SVI.
753  */
754  if (unlikely(apic->apicv_active))
755  static_call_cond(kvm_x86_hwapic_isr_update)(vec);
756  else {
757  ++apic->isr_count;
758  BUG_ON(apic->isr_count > MAX_APIC_VECTOR);
759  /*
760  * ISR (in service register) bit is set when injecting an interrupt.
761  * The highest vector is injected. Thus the latest bit set matches
762  * the highest bit in ISR.
763  */
764  apic->highest_isr_cache = vec;
765  }
766 }
#define MAX_APIC_VECTOR
Definition: lapic.c:59
static int __apic_test_and_set_vector(int vec, void *bitmap)
Definition: lapic.c:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_set_spiv()

static void apic_set_spiv ( struct kvm_lapic apic,
u32  val 
)
inlinestatic

Definition at line 486 of file lapic.c.

487 {
488  bool enabled = val & APIC_SPIV_APIC_ENABLED;
489 
490  kvm_lapic_set_reg(apic, APIC_SPIV, val);
491 
492  if (enabled != apic->sw_enabled) {
493  apic->sw_enabled = enabled;
494  if (enabled)
495  static_branch_slow_dec_deferred(&apic_sw_disabled);
496  else
497  static_branch_inc(&apic_sw_disabled.key);
498 
499  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
500  }
501 
502  /* Check if there are APF page ready requests pending */
503  if (enabled) {
504  kvm_make_request(KVM_REQ_APF_READY, apic->vcpu);
506  }
507 }
struct static_key_false_deferred apic_sw_disabled
bool sw_enabled
Definition: lapic.h:66
static void kvm_xen_sw_enable_lapic(struct kvm_vcpu *vcpu)
Definition: xen.h:118
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_set_tpr()

static void apic_set_tpr ( struct kvm_lapic apic,
u32  tpr 
)
static

Definition at line 981 of file lapic.c.

982 {
983  kvm_lapic_set_reg(apic, APIC_TASKPRI, tpr);
984  apic_update_ppr(apic);
985 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_sync_pv_eoi_from_guest()

static void apic_sync_pv_eoi_from_guest ( struct kvm_vcpu *  vcpu,
struct kvm_lapic apic 
)
static

Definition at line 3049 of file lapic.c.

3051 {
3052  int vector;
3053  /*
3054  * PV EOI state is derived from KVM_APIC_PV_EOI_PENDING in host
3055  * and KVM_PV_EOI_ENABLED in guest memory as follows:
3056  *
3057  * KVM_APIC_PV_EOI_PENDING is unset:
3058  * -> host disabled PV EOI.
3059  * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is set:
3060  * -> host enabled PV EOI, guest did not execute EOI yet.
3061  * KVM_APIC_PV_EOI_PENDING is set, KVM_PV_EOI_ENABLED is unset:
3062  * -> host enabled PV EOI, guest executed EOI.
3063  */
3064  BUG_ON(!pv_eoi_enabled(vcpu));
3065 
3066  if (pv_eoi_test_and_clr_pending(vcpu))
3067  return;
3068  vector = apic_set_eoi(apic);
3069  trace_kvm_pv_eoi(apic, vector);
3070 }
static bool pv_eoi_enabled(struct kvm_vcpu *vcpu)
Definition: lapic.c:897
static bool pv_eoi_test_and_clr_pending(struct kvm_vcpu *vcpu)
Definition: lapic.c:910
static int apic_set_eoi(struct kvm_lapic *apic)
Definition: lapic.c:1465
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_sync_pv_eoi_to_guest()

static void apic_sync_pv_eoi_to_guest ( struct kvm_vcpu *  vcpu,
struct kvm_lapic apic 
)
static

Definition at line 3095 of file lapic.c.

3097 {
3098  if (!pv_eoi_enabled(vcpu) ||
3099  /* IRR set or many bits in ISR: could be nested. */
3100  apic->irr_pending ||
3101  /* Cache not set: could be safe but we don't bother. */
3102  apic->highest_isr_cache == -1 ||
3103  /* Need EOI to update ioapic. */
3105  /*
3106  * PV EOI was disabled by apic_sync_pv_eoi_from_guest
3107  * so we need not do anything here.
3108  */
3109  return;
3110  }
3111 
3112  pv_eoi_set_pending(apic->vcpu);
3113 }
static void pv_eoi_set_pending(struct kvm_vcpu *vcpu)
Definition: lapic.c:902
static bool kvm_ioapic_handles_vector(struct kvm_lapic *apic, int vector)
Definition: lapic.c:1437
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_test_vector()

static int apic_test_vector ( int  vec,
void *  bitmap 
)
inlinestatic

Definition at line 105 of file lapic.c.

106 {
107  return test_bit(VEC_POS(vec), (bitmap) + REG_POS(vec));
108 }
Here is the caller graph for this function:

◆ apic_timer_expired()

static void apic_timer_expired ( struct kvm_lapic apic,
bool  from_timer_fn 
)
static

Definition at line 1892 of file lapic.c.

1893 {
1894  struct kvm_vcpu *vcpu = apic->vcpu;
1895  struct kvm_timer *ktimer = &apic->lapic_timer;
1896 
1897  if (atomic_read(&apic->lapic_timer.pending))
1898  return;
1899 
1900  if (apic_lvtt_tscdeadline(apic) || ktimer->hv_timer_in_use)
1901  ktimer->expired_tscdeadline = ktimer->tscdeadline;
1902 
1903  if (!from_timer_fn && apic->apicv_active) {
1904  WARN_ON(kvm_get_running_vcpu() != vcpu);
1906  return;
1907  }
1908 
1909  if (kvm_use_posted_timer_interrupt(apic->vcpu)) {
1910  /*
1911  * Ensure the guest's timer has truly expired before posting an
1912  * interrupt. Open code the relevant checks to avoid querying
1913  * lapic_timer_int_injected(), which will be false since the
1914  * interrupt isn't yet injected. Waiting until after injecting
1915  * is not an option since that won't help a posted interrupt.
1916  */
1917  if (vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
1918  vcpu->arch.apic->lapic_timer.timer_advance_ns)
1921  return;
1922  }
1923 
1924  atomic_inc(&apic->lapic_timer.pending);
1925  kvm_make_request(KVM_REQ_UNBLOCK, vcpu);
1926  if (from_timer_fn)
1927  kvm_vcpu_kick(vcpu);
1928 }
struct kvm_vcpu * kvm_get_running_vcpu(void)
Definition: kvm_main.c:6338
static void kvm_apic_inject_pending_timer_irqs(struct kvm_lapic *apic)
Definition: lapic.c:1879
static void __kvm_wait_lapic_expire(struct kvm_vcpu *vcpu)
Definition: lapic.c:1844
static bool kvm_use_posted_timer_interrupt(struct kvm_vcpu *vcpu)
Definition: lapic.c:161
bool hv_timer_in_use
Definition: lapic.h:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_timer_fn()

static enum hrtimer_restart apic_timer_fn ( struct hrtimer *  data)
static

Definition at line 2782 of file lapic.c.

2796 {
2797  struct kvm_timer *ktimer = container_of(data, struct kvm_timer, timer);
2798  struct kvm_lapic *apic = container_of(ktimer, struct kvm_lapic, lapic_timer);
2799 
2800  apic_timer_expired(apic, true);
2801 
2802  if (lapic_is_periodic(apic)) {
2804  hrtimer_add_expires_ns(&ktimer->timer, ktimer->period);
2805  return HRTIMER_RESTART;
2806  } else
2807  return HRTIMER_NORESTART;
2808 }
static void advance_periodic_target_expiration(struct kvm_lapic *apic)
Definition: lapic.c:2042
static bool lapic_is_periodic(struct kvm_lapic *apic)
Definition: lapic.c:2747
static void apic_timer_expired(struct kvm_lapic *apic, bool from_timer_fn)
Definition: lapic.c:1892
struct hrtimer timer
Definition: lapic.h:47
Here is the caller graph for this function:

◆ apic_update_lvtt()

static void apic_update_lvtt ( struct kvm_lapic apic)
static

Definition at line 1754 of file lapic.c.

1755 {
1756  u32 timer_mode = kvm_lapic_get_reg(apic, APIC_LVTT) &
1758 
1759  if (apic->lapic_timer.timer_mode != timer_mode) {
1760  if (apic_lvtt_tscdeadline(apic) != (timer_mode ==
1761  APIC_LVT_TIMER_TSCDEADLINE)) {
1762  cancel_apic_timer(apic);
1763  kvm_lapic_set_reg(apic, APIC_TMICT, 0);
1764  apic->lapic_timer.period = 0;
1765  apic->lapic_timer.tscdeadline = 0;
1766  }
1767  apic->lapic_timer.timer_mode = timer_mode;
1769  }
1770 }
static void cancel_apic_timer(struct kvm_lapic *apic)
Definition: lapic.c:1744
static void limit_periodic_timer_frequency(struct kvm_lapic *apic)
Definition: lapic.c:1721
u32 timer_mode_mask
Definition: lapic.h:51
Here is the call graph for this function:
Here is the caller graph for this function:

◆ apic_update_ppr()

static void apic_update_ppr ( struct kvm_lapic apic)
static

Definition at line 966 of file lapic.c.

967 {
968  u32 ppr;
969 
970  if (__apic_update_ppr(apic, &ppr) &&
971  apic_has_interrupt_for_ppr(apic, ppr) != -1)
972  kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
973 }
static bool __apic_update_ppr(struct kvm_lapic *apic, u32 *new_ppr)
Definition: lapic.c:944
static int apic_has_interrupt_for_ppr(struct kvm_lapic *apic, u32 ppr)
Definition: lapic.c:932
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cancel_apic_timer()

static void cancel_apic_timer ( struct kvm_lapic apic)
static

Definition at line 1744 of file lapic.c.

1745 {
1746  hrtimer_cancel(&apic->lapic_timer.timer);
1747  preempt_disable();
1748  if (apic->lapic_timer.hv_timer_in_use)
1749  cancel_hv_timer(apic);
1750  preempt_enable();
1751  atomic_set(&apic->lapic_timer.pending, 0);
1752 }
static void cancel_hv_timer(struct kvm_lapic *apic)
Definition: lapic.c:2091
Here is the call graph for this function:
Here is the caller graph for this function:

◆ cancel_hv_timer()

static void cancel_hv_timer ( struct kvm_lapic apic)
static

Definition at line 2091 of file lapic.c.

2092 {
2093  WARN_ON(preemptible());
2094  WARN_ON(!apic->lapic_timer.hv_timer_in_use);
2095  static_call(kvm_x86_cancel_hv_timer)(apic->vcpu);
2096  apic->lapic_timer.hv_timer_in_use = false;
2097 }
Here is the caller graph for this function:

◆ count_vectors()

static u8 count_vectors ( void *  bitmap)
static

Definition at line 640 of file lapic.c.

641 {
642  int vec;
643  u32 *reg;
644  u8 count = 0;
645 
646  for (vec = 0; vec < MAX_APIC_VECTOR; vec += APIC_VECTORS_PER_REG) {
647  reg = bitmap + REG_POS(vec);
648  count += hweight32(*reg);
649  }
650 
651  return count;
652 }
#define APIC_VECTORS_PER_REG
Definition: lapic.c:60
Here is the caller graph for this function:

◆ DEFINE_STATIC_KEY_DEFERRED_FALSE() [1/2]

__read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE ( apic_hw_disabled  ,
HZ   
)

◆ DEFINE_STATIC_KEY_DEFERRED_FALSE() [2/2]

__read_mostly DEFINE_STATIC_KEY_DEFERRED_FALSE ( apic_sw_disabled  ,
HZ   
)

◆ EXPORT_SYMBOL_GPL() [1/15]

EXPORT_SYMBOL_GPL ( __kvm_apic_update_irr  )

◆ EXPORT_SYMBOL_GPL() [2/15]

EXPORT_SYMBOL_GPL ( kvm_alloc_apic_access_page  )

◆ EXPORT_SYMBOL_GPL() [3/15]

EXPORT_SYMBOL_GPL ( kvm_apic_clear_irr  )

◆ EXPORT_SYMBOL_GPL() [4/15]

EXPORT_SYMBOL_GPL ( kvm_apic_has_interrupt  )

◆ EXPORT_SYMBOL_GPL() [5/15]

EXPORT_SYMBOL_GPL ( kvm_apic_match_dest  )

◆ EXPORT_SYMBOL_GPL() [6/15]

EXPORT_SYMBOL_GPL ( kvm_apic_send_ipi  )

◆ EXPORT_SYMBOL_GPL() [7/15]

EXPORT_SYMBOL_GPL ( kvm_apic_set_eoi_accelerated  )

◆ EXPORT_SYMBOL_GPL() [8/15]

EXPORT_SYMBOL_GPL ( kvm_apic_update_irr  )

◆ EXPORT_SYMBOL_GPL() [9/15]

EXPORT_SYMBOL_GPL ( kvm_apic_update_ppr  )

◆ EXPORT_SYMBOL_GPL() [10/15]

EXPORT_SYMBOL_GPL ( kvm_apic_write_nodecode  )

◆ EXPORT_SYMBOL_GPL() [11/15]

EXPORT_SYMBOL_GPL ( kvm_lapic_expired_hv_timer  )

◆ EXPORT_SYMBOL_GPL() [12/15]

EXPORT_SYMBOL_GPL ( kvm_lapic_find_highest_irr  )

◆ EXPORT_SYMBOL_GPL() [13/15]

EXPORT_SYMBOL_GPL ( kvm_lapic_readable_reg_mask  )

◆ EXPORT_SYMBOL_GPL() [14/15]

EXPORT_SYMBOL_GPL ( kvm_lapic_set_eoi  )

◆ EXPORT_SYMBOL_GPL() [15/15]

EXPORT_SYMBOL_GPL ( kvm_wait_lapic_expire  )

◆ find_highest_vector()

static int find_highest_vector ( void *  bitmap)
static

Definition at line 625 of file lapic.c.

626 {
627  int vec;
628  u32 *reg;
629 
631  vec >= 0; vec -= APIC_VECTORS_PER_REG) {
632  reg = bitmap + REG_POS(vec);
633  if (*reg)
634  return __fls(*reg) + vec;
635  }
636 
637  return -1;
638 }
Here is the caller graph for this function:

◆ get_lvt_index()

static int get_lvt_index ( u32  reg)
static

Definition at line 2245 of file lapic.c.

2246 {
2247  if (reg == APIC_LVTCMCI)
2248  return LVT_CMCI;
2249  if (reg < APIC_LVTT || reg > APIC_LVTERR)
2250  return -1;
2251  return array_index_nospec(
2252  (reg - APIC_LVTT) >> 4, KVM_APIC_MAX_NR_LVT_ENTRIES);
2253 }
@ LVT_CMCI
Definition: lapic.h:39
@ KVM_APIC_MAX_NR_LVT_ENTRIES
Definition: lapic.h:41
Here is the caller graph for this function:

◆ kvm_alloc_apic_access_page()

int kvm_alloc_apic_access_page ( struct kvm *  kvm)

Definition at line 2598 of file lapic.c.

2599 {
2600  struct page *page;
2601  void __user *hva;
2602  int ret = 0;
2603 
2604  mutex_lock(&kvm->slots_lock);
2605  if (kvm->arch.apic_access_memslot_enabled ||
2606  kvm->arch.apic_access_memslot_inhibited)
2607  goto out;
2608 
2609  hva = __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT,
2610  APIC_DEFAULT_PHYS_BASE, PAGE_SIZE);
2611  if (IS_ERR(hva)) {
2612  ret = PTR_ERR(hva);
2613  goto out;
2614  }
2615 
2616  page = gfn_to_page(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
2617  if (is_error_page(page)) {
2618  ret = -EFAULT;
2619  goto out;
2620  }
2621 
2622  /*
2623  * Do not pin the page in memory, so that memory hot-unplug
2624  * is able to migrate it.
2625  */
2626  put_page(page);
2627  kvm->arch.apic_access_memslot_enabled = true;
2628 out:
2629  mutex_unlock(&kvm->slots_lock);
2630  return ret;
2631 }
struct page * gfn_to_page(struct kvm *kvm, gfn_t gfn)
Definition: kvm_main.c:3126
void __user * __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
Definition: x86.c:12637
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_accept_events()

int kvm_apic_accept_events ( struct kvm_vcpu *  vcpu)

Definition at line 3263 of file lapic.c.

3264 {
3265  struct kvm_lapic *apic = vcpu->arch.apic;
3266  u8 sipi_vector;
3267  int r;
3268 
3270  return 0;
3271 
3272  if (is_guest_mode(vcpu)) {
3274  if (r < 0)
3275  return r == -EBUSY ? 0 : r;
3276  /*
3277  * Continue processing INIT/SIPI even if a nested VM-Exit
3278  * occurred, e.g. pending SIPIs should be dropped if INIT+SIPI
3279  * are blocked as a result of transitioning to VMX root mode.
3280  */
3281  }
3282 
3283  /*
3284  * INITs are blocked while CPU is in specific states (SMM, VMX root
3285  * mode, SVM with GIF=0), while SIPIs are dropped if the CPU isn't in
3286  * wait-for-SIPI (WFS).
3287  */
3289  WARN_ON_ONCE(vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED);
3290  clear_bit(KVM_APIC_SIPI, &apic->pending_events);
3291  return 0;
3292  }
3293 
3294  if (test_and_clear_bit(KVM_APIC_INIT, &apic->pending_events)) {
3295  kvm_vcpu_reset(vcpu, true);
3296  if (kvm_vcpu_is_bsp(apic->vcpu))
3297  vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3298  else
3299  vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
3300  }
3301  if (test_and_clear_bit(KVM_APIC_SIPI, &apic->pending_events)) {
3302  if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
3303  /* evaluate pending_events before reading the vector */
3304  smp_rmb();
3305  sipi_vector = apic->sipi_vector;
3306  static_call(kvm_x86_vcpu_deliver_sipi_vector)(vcpu, sipi_vector);
3307  vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
3308  }
3309  }
3310  return 0;
3311 }
static bool is_guest_mode(struct kvm_vcpu *vcpu)
static bool kvm_apic_has_pending_init_or_sipi(struct kvm_vcpu *vcpu)
Definition: lapic.h:231
static bool kvm_apic_init_sipi_allowed(struct kvm_vcpu *vcpu)
Definition: lapic.h:236
void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
Definition: x86.c:12224
int kvm_check_nested_events(struct kvm_vcpu *vcpu)
Definition: x86.c:10197
bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
Definition: x86.c:12493
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_accept_pic_intr()

int kvm_apic_accept_pic_intr ( struct kvm_vcpu *  vcpu)

Definition at line 2872 of file lapic.c.

2873 {
2874  u32 lvt0 = kvm_lapic_get_reg(vcpu->arch.apic, APIC_LVT0);
2875 
2876  if (!kvm_apic_hw_enabled(vcpu->arch.apic))
2877  return 1;
2878  if ((lvt0 & APIC_LVT_MASKED) == 0 &&
2879  GET_APIC_DELIVERY_MODE(lvt0) == APIC_MODE_EXTINT)
2880  return 1;
2881  return 0;
2882 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_after_set_mcg_cap()

void kvm_apic_after_set_mcg_cap ( struct kvm_vcpu *  vcpu)

Definition at line 596 of file lapic.c.

597 {
598  int nr_lvt_entries = kvm_apic_calc_nr_lvt_entries(vcpu);
599  struct kvm_lapic *apic = vcpu->arch.apic;
600  int i;
601 
603  return;
604 
605  /* Initialize/mask any "new" LVT entries. */
606  for (i = apic->nr_lvt_entries; i < nr_lvt_entries; i++)
607  kvm_lapic_set_reg(apic, APIC_LVTx(i), APIC_LVT_MASKED);
608 
610 
611  /* The number of LVT entries is reflected in the version register. */
613 }
static int kvm_apic_calc_nr_lvt_entries(struct kvm_vcpu *vcpu)
Definition: lapic.c:568
void kvm_apic_set_version(struct kvm_vcpu *vcpu)
Definition: lapic.c:573
#define APIC_LVTx(x)
Definition: lapic.h:44
int nr_lvt_entries
Definition: lapic.h:83
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_broadcast()

static bool kvm_apic_broadcast ( struct kvm_lapic apic,
u32  mda 
)
static

Definition at line 987 of file lapic.c.

988 {
989  return mda == (apic_x2apic_mode(apic) ?
991 }
#define X2APIC_BROADCAST
Definition: lapic.h:23
#define APIC_BROADCAST
Definition: lapic.h:22
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_calc_nr_lvt_entries()

static int kvm_apic_calc_nr_lvt_entries ( struct kvm_vcpu *  vcpu)
inlinestatic

Definition at line 568 of file lapic.c.

569 {
570  return KVM_APIC_MAX_NR_LVT_ENTRIES - !(vcpu->arch.mcg_cap & MCG_CMCI_P);
571 }
Here is the caller graph for this function:

◆ kvm_apic_calc_x2apic_ldr()

static u32 kvm_apic_calc_x2apic_ldr ( u32  id)
inlinestatic

Definition at line 166 of file lapic.c.

167 {
168  return ((id >> 4) << 16) | (1 << (id & 0xf));
169 }
Here is the caller graph for this function:

◆ kvm_apic_clear_irr()

void kvm_apic_clear_irr ( struct kvm_vcpu *  vcpu,
int  vec 
)

Definition at line 738 of file lapic.c.

739 {
740  apic_clear_irr(vec, vcpu->arch.apic);
741 }
static void apic_clear_irr(int vec, struct kvm_lapic *apic)
Definition: lapic.c:723
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_compare_prio()

int kvm_apic_compare_prio ( struct kvm_vcpu *  vcpu1,
struct kvm_vcpu *  vcpu2 
)

Definition at line 1432 of file lapic.c.

1433 {
1434  return vcpu1->arch.apic_arb_prio - vcpu2->arch.apic_arb_prio;
1435 }
Here is the caller graph for this function:

◆ kvm_apic_disabled_lapic_found()

static void kvm_apic_disabled_lapic_found ( struct kvm *  kvm)
static

Definition at line 1108 of file lapic.c.

1109 {
1110  if (!kvm->arch.disabled_lapic_found) {
1111  kvm->arch.disabled_lapic_found = true;
1112  pr_info("Disabled LAPIC found during irq injection\n");
1113  }
1114 }
Here is the caller graph for this function:

◆ kvm_apic_get_state()

int kvm_apic_get_state ( struct kvm_vcpu *  vcpu,
struct kvm_lapic_state *  s 
)

Definition at line 2970 of file lapic.c.

2971 {
2972  memcpy(s->regs, vcpu->arch.apic->regs, sizeof(*s));
2973 
2974  /*
2975  * Get calculated timer current count for remaining timer period (if
2976  * any) and store it in the returned register set.
2977  */
2978  __kvm_lapic_set_reg(s->regs, APIC_TMCCT,
2979  __apic_read(vcpu->arch.apic, APIC_TMCCT));
2980 
2981  return kvm_apic_state_fixup(vcpu, s, false);
2982 }
static int kvm_apic_state_fixup(struct kvm_vcpu *vcpu, struct kvm_lapic_state *s, bool set)
Definition: lapic.c:2932
static void __kvm_lapic_set_reg(char *regs, int reg_off, u32 val)
Definition: lapic.c:72
static u32 __apic_read(struct kvm_lapic *apic, unsigned int offset)
Definition: lapic.c:1566
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_has_interrupt()

int kvm_apic_has_interrupt ( struct kvm_vcpu *  vcpu)

Definition at line 2859 of file lapic.c.

2860 {
2861  struct kvm_lapic *apic = vcpu->arch.apic;
2862  u32 ppr;
2863 
2864  if (!kvm_apic_present(vcpu))
2865  return -1;
2866 
2867  __apic_update_ppr(apic, &ppr);
2868  return apic_has_interrupt_for_ppr(apic, ppr);
2869 }
static bool kvm_apic_present(struct kvm_vcpu *vcpu)
Definition: lapic.h:211
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_inject_pending_timer_irqs()

static void kvm_apic_inject_pending_timer_irqs ( struct kvm_lapic apic)
static

Definition at line 1879 of file lapic.c.

1880 {
1881  struct kvm_timer *ktimer = &apic->lapic_timer;
1882 
1883  kvm_apic_local_deliver(apic, APIC_LVTT);
1884  if (apic_lvtt_tscdeadline(apic)) {
1885  ktimer->tscdeadline = 0;
1886  } else if (apic_lvtt_oneshot(apic)) {
1887  ktimer->tscdeadline = 0;
1888  ktimer->target_expiration = 0;
1889  }
1890 }
int kvm_apic_local_deliver(struct kvm_lapic *apic, int lvt_type)
Definition: lapic.c:2762
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_is_broadcast_dest()

static bool kvm_apic_is_broadcast_dest ( struct kvm *  kvm,
struct kvm_lapic **  src,
struct kvm_lapic_irq *  irq,
struct kvm_apic_map *  map 
)
static

Definition at line 1116 of file lapic.c.

1118 {
1119  if (kvm->arch.x2apic_broadcast_quirk_disabled) {
1120  if ((irq->dest_id == APIC_BROADCAST &&
1121  map->logical_mode != KVM_APIC_MODE_X2APIC))
1122  return true;
1123  if (irq->dest_id == X2APIC_BROADCAST)
1124  return true;
1125  } else {
1126  bool x2apic_ipi = src && *src && apic_x2apic_mode(*src);
1127  if (irq->dest_id == (x2apic_ipi ?
1129  return true;
1130  }
1131 
1132  return false;
1133 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_local_deliver()

int kvm_apic_local_deliver ( struct kvm_lapic apic,
int  lvt_type 
)

Definition at line 2762 of file lapic.c.

2763 {
2764  u32 reg = kvm_lapic_get_reg(apic, lvt_type);
2765  int vector, mode, trig_mode;
2766  int r;
2767 
2768  if (kvm_apic_hw_enabled(apic) && !(reg & APIC_LVT_MASKED)) {
2769  vector = reg & APIC_VECTOR_MASK;
2770  mode = reg & APIC_MODE_MASK;
2771  trig_mode = reg & APIC_LVT_LEVEL_TRIGGER;
2772 
2773  r = __apic_accept_irq(apic, mode, vector, 1, trig_mode, NULL);
2774  if (r && lvt_type == APIC_LVTPC &&
2776  kvm_lapic_set_reg(apic, APIC_LVTPC, reg | APIC_LVT_MASKED);
2777  return r;
2778  }
2779  return 0;
2780 }
static bool guest_cpuid_is_intel_compatible(struct kvm_vcpu *vcpu)
Definition: cpuid.h:128
static int __apic_accept_irq(struct kvm_lapic *apic, int delivery_mode, int vector, int level, int trig_mode, struct dest_map *dest_map)
Definition: lapic.c:1291
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_map_free()

static void kvm_apic_map_free ( struct rcu_head *  rcu)
static

Definition at line 211 of file lapic.c.

212 {
213  struct kvm_apic_map *map = container_of(rcu, struct kvm_apic_map, rcu);
214 
215  kvfree(map);
216 }
Here is the caller graph for this function:

◆ kvm_apic_map_get_dest_lapic()

static bool kvm_apic_map_get_dest_lapic ( struct kvm *  kvm,
struct kvm_lapic **  src,
struct kvm_lapic_irq *  irq,
struct kvm_apic_map *  map,
struct kvm_lapic ***  dst,
unsigned long *  bitmap 
)
inlinestatic

Definition at line 1142 of file lapic.c.

1146 {
1147  int i, lowest;
1148 
1149  if (irq->shorthand == APIC_DEST_SELF && src) {
1150  *dst = src;
1151  *bitmap = 1;
1152  return true;
1153  } else if (irq->shorthand)
1154  return false;
1155 
1156  if (!map || kvm_apic_is_broadcast_dest(kvm, src, irq, map))
1157  return false;
1158 
1159  if (irq->dest_mode == APIC_DEST_PHYSICAL) {
1160  if (irq->dest_id > map->max_apic_id) {
1161  *bitmap = 0;
1162  } else {
1163  u32 dest_id = array_index_nospec(irq->dest_id, map->max_apic_id + 1);
1164  *dst = &map->phys_map[dest_id];
1165  *bitmap = 1;
1166  }
1167  return true;
1168  }
1169 
1170  *bitmap = 0;
1171  if (!kvm_apic_map_get_logical_dest(map, irq->dest_id, dst,
1172  (u16 *)bitmap))
1173  return false;
1174 
1175  if (!kvm_lowest_prio_delivery(irq))
1176  return true;
1177 
1178  if (!kvm_vector_hashing_enabled()) {
1179  lowest = -1;
1180  for_each_set_bit(i, bitmap, 16) {
1181  if (!(*dst)[i])
1182  continue;
1183  if (lowest < 0)
1184  lowest = i;
1185  else if (kvm_apic_compare_prio((*dst)[i]->vcpu,
1186  (*dst)[lowest]->vcpu) < 0)
1187  lowest = i;
1188  }
1189  } else {
1190  if (!*bitmap)
1191  return true;
1192 
1193  lowest = kvm_vector_to_index(irq->vector, hweight16(*bitmap),
1194  bitmap, 16);
1195 
1196  if (!(*dst)[lowest]) {
1198  *bitmap = 0;
1199  return true;
1200  }
1201  }
1202 
1203  *bitmap = (lowest >= 0) ? 1 << lowest : 0;
1204 
1205  return true;
1206 }
static void kvm_apic_disabled_lapic_found(struct kvm *kvm)
Definition: lapic.c:1108
static bool kvm_apic_map_get_logical_dest(struct kvm_apic_map *map, u32 dest_id, struct kvm_lapic ***cluster, u16 *mask)
Definition: lapic.c:171
int kvm_apic_compare_prio(struct kvm_vcpu *vcpu1, struct kvm_vcpu *vcpu2)
Definition: lapic.c:1432
static bool kvm_apic_is_broadcast_dest(struct kvm *kvm, struct kvm_lapic **src, struct kvm_lapic_irq *irq, struct kvm_apic_map *map)
Definition: lapic.c:1116
int kvm_vector_to_index(u32 vector, u32 dest_vcpus, const unsigned long *bitmap, u32 bitmap_size)
Definition: lapic.c:1092
static bool kvm_lowest_prio_delivery(struct kvm_lapic_irq *irq)
Definition: lapic.h:242
bool kvm_vector_hashing_enabled(void)
Definition: x86.c:13520
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_map_get_logical_dest()

static bool kvm_apic_map_get_logical_dest ( struct kvm_apic_map *  map,
u32  dest_id,
struct kvm_lapic ***  cluster,
u16 *  mask 
)
inlinestatic

Definition at line 171 of file lapic.c.

172  {
173  switch (map->logical_mode) {
174  case KVM_APIC_MODE_SW_DISABLED:
175  /* Arbitrarily use the flat map so that @cluster isn't NULL. */
176  *cluster = map->xapic_flat_map;
177  *mask = 0;
178  return true;
179  case KVM_APIC_MODE_X2APIC: {
180  u32 offset = (dest_id >> 16) * 16;
181  u32 max_apic_id = map->max_apic_id;
182 
183  if (offset <= max_apic_id) {
184  u8 cluster_size = min(max_apic_id - offset + 1, 16U);
185 
186  offset = array_index_nospec(offset, map->max_apic_id + 1);
187  *cluster = &map->phys_map[offset];
188  *mask = dest_id & (0xffff >> (16 - cluster_size));
189  } else {
190  *mask = 0;
191  }
192 
193  return true;
194  }
195  case KVM_APIC_MODE_XAPIC_FLAT:
196  *cluster = map->xapic_flat_map;
197  *mask = dest_id & 0xff;
198  return true;
199  case KVM_APIC_MODE_XAPIC_CLUSTER:
200  *cluster = map->xapic_cluster_map[(dest_id >> 4) & 0xf];
201  *mask = dest_id & 0xf;
202  return true;
203  case KVM_APIC_MODE_MAP_DISABLED:
204  return false;
205  default:
206  WARN_ON_ONCE(1);
207  return false;
208  }
209 }
Here is the caller graph for this function:

◆ kvm_apic_match_dest()

bool kvm_apic_match_dest ( struct kvm_vcpu *  vcpu,
struct kvm_lapic source,
int  shorthand,
unsigned int  dest,
int  dest_mode 
)

Definition at line 1067 of file lapic.c.

1069 {
1070  struct kvm_lapic *target = vcpu->arch.apic;
1071  u32 mda = kvm_apic_mda(vcpu, dest, source, target);
1072 
1073  ASSERT(target);
1074  switch (shorthand) {
1075  case APIC_DEST_NOSHORT:
1076  if (dest_mode == APIC_DEST_PHYSICAL)
1077  return kvm_apic_match_physical_addr(target, mda);
1078  else
1079  return kvm_apic_match_logical_addr(target, mda);
1080  case APIC_DEST_SELF:
1081  return target == source;
1082  case APIC_DEST_ALLINC:
1083  return true;
1084  case APIC_DEST_ALLBUT:
1085  return target != source;
1086  default:
1087  return false;
1088  }
1089 }
static bool kvm_apic_match_logical_addr(struct kvm_lapic *apic, u32 mda)
Definition: lapic.c:1013
static u32 kvm_apic_mda(struct kvm_vcpu *vcpu, unsigned int dest_id, struct kvm_lapic *source, struct kvm_lapic *target)
Definition: lapic.c:1055
static bool kvm_apic_match_physical_addr(struct kvm_lapic *apic, u32 mda)
Definition: lapic.c:993
#define APIC_DEST_NOSHORT
Definition: lapic.h:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_match_logical_addr()

static bool kvm_apic_match_logical_addr ( struct kvm_lapic apic,
u32  mda 
)
static

Definition at line 1013 of file lapic.c.

1014 {
1015  u32 logical_id;
1016 
1017  if (kvm_apic_broadcast(apic, mda))
1018  return true;
1019 
1020  logical_id = kvm_lapic_get_reg(apic, APIC_LDR);
1021 
1022  if (apic_x2apic_mode(apic))
1023  return ((logical_id >> 16) == (mda >> 16))
1024  && (logical_id & mda & 0xffff) != 0;
1025 
1026  logical_id = GET_APIC_LOGICAL_ID(logical_id);
1027 
1028  switch (kvm_lapic_get_reg(apic, APIC_DFR)) {
1029  case APIC_DFR_FLAT:
1030  return (logical_id & mda) != 0;
1031  case APIC_DFR_CLUSTER:
1032  return ((logical_id >> 4) == (mda >> 4))
1033  && (logical_id & mda & 0xf) != 0;
1034  default:
1035  return false;
1036  }
1037 }
static bool kvm_apic_broadcast(struct kvm_lapic *apic, u32 mda)
Definition: lapic.c:987
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_match_physical_addr()

static bool kvm_apic_match_physical_addr ( struct kvm_lapic apic,
u32  mda 
)
static

Definition at line 993 of file lapic.c.

994 {
995  if (kvm_apic_broadcast(apic, mda))
996  return true;
997 
998  /*
999  * Hotplug hack: Accept interrupts for vCPUs in xAPIC mode as if they
1000  * were in x2APIC mode if the target APIC ID can't be encoded as an
1001  * xAPIC ID. This allows unique addressing of hotplugged vCPUs (which
1002  * start in xAPIC mode) with an APIC ID that is unaddressable in xAPIC
1003  * mode. Match the x2APIC ID if and only if the target APIC ID can't
1004  * be encoded in xAPIC to avoid spurious matches against a vCPU that
1005  * changed its (addressable) xAPIC ID (which is writable).
1006  */
1007  if (apic_x2apic_mode(apic) || mda > 0xff)
1008  return mda == kvm_x2apic_id(apic);
1009 
1010  return mda == kvm_xapic_id(apic);
1011 }
static u32 kvm_x2apic_id(struct kvm_lapic *apic)
Definition: lapic.c:143
static u8 kvm_xapic_id(struct kvm_lapic *apic)
Definition: lapic.h:276
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_mda()

static u32 kvm_apic_mda ( struct kvm_vcpu *  vcpu,
unsigned int  dest_id,
struct kvm_lapic source,
struct kvm_lapic target 
)
static

Definition at line 1055 of file lapic.c.

1057 {
1058  bool ipi = source != NULL;
1059 
1060  if (!vcpu->kvm->arch.x2apic_broadcast_quirk_disabled &&
1061  !ipi && dest_id == APIC_BROADCAST && apic_x2apic_mode(target))
1062  return X2APIC_BROADCAST;
1063 
1064  return dest_id;
1065 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_nmi_wd_deliver()

void kvm_apic_nmi_wd_deliver ( struct kvm_vcpu *  vcpu)

Definition at line 2782 of file lapic.c.

2783 {
2784  struct kvm_lapic *apic = vcpu->arch.apic;
2785 
2786  if (apic)
2787  kvm_apic_local_deliver(apic, APIC_LVT0);
2788 }
Here is the caller graph for this function:

◆ kvm_apic_pending_eoi()

bool kvm_apic_pending_eoi ( struct kvm_vcpu *  vcpu,
int  vector 
)

Definition at line 110 of file lapic.c.

111 {
112  struct kvm_lapic *apic = vcpu->arch.apic;
113 
114  return apic_test_vector(vector, apic->regs + APIC_ISR) ||
115  apic_test_vector(vector, apic->regs + APIC_IRR);
116 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_send_ipi()

void kvm_apic_send_ipi ( struct kvm_lapic apic,
u32  icr_low,
u32  icr_high 
)

Definition at line 1504 of file lapic.c.

1505 {
1506  struct kvm_lapic_irq irq;
1507 
1508  /* KVM has no delay and should always clear the BUSY/PENDING flag. */
1509  WARN_ON_ONCE(icr_low & APIC_ICR_BUSY);
1510 
1511  irq.vector = icr_low & APIC_VECTOR_MASK;
1512  irq.delivery_mode = icr_low & APIC_MODE_MASK;
1513  irq.dest_mode = icr_low & APIC_DEST_MASK;
1514  irq.level = (icr_low & APIC_INT_ASSERT) != 0;
1515  irq.trig_mode = icr_low & APIC_INT_LEVELTRIG;
1516  irq.shorthand = icr_low & APIC_SHORT_MASK;
1517  irq.msi_redir_hint = false;
1518  if (apic_x2apic_mode(apic))
1519  irq.dest_id = icr_high;
1520  else
1521  irq.dest_id = GET_XAPIC_DEST_FIELD(icr_high);
1522 
1523  trace_kvm_apic_ipi(icr_low, irq.dest_id);
1524 
1525  kvm_irq_delivery_to_apic(apic->vcpu->kvm, apic, &irq, NULL);
1526 }
int kvm_irq_delivery_to_apic(struct kvm *kvm, struct kvm_lapic *src, struct kvm_lapic_irq *irq, struct dest_map *dest_map)
Definition: irq_comm.c:47
#define APIC_DEST_MASK
Definition: lapic.h:17
#define APIC_SHORT_MASK
Definition: lapic.h:15
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_dfr()

static void kvm_apic_set_dfr ( struct kvm_lapic apic,
u32  val 
)
inlinestatic

Definition at line 521 of file lapic.c.

522 {
523  kvm_lapic_set_reg(apic, APIC_DFR, val);
524  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
525 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_eoi_accelerated()

void kvm_apic_set_eoi_accelerated ( struct kvm_vcpu *  vcpu,
int  vector 
)

Definition at line 1493 of file lapic.c.

1494 {
1495  struct kvm_lapic *apic = vcpu->arch.apic;
1496 
1497  trace_kvm_eoi(apic, vector);
1498 
1499  kvm_ioapic_send_eoi(apic, vector);
1500  kvm_make_request(KVM_REQ_EVENT, apic->vcpu);
1501 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_irq()

int kvm_apic_set_irq ( struct kvm_vcpu *  vcpu,
struct kvm_lapic_irq *  irq,
struct dest_map dest_map 
)

Definition at line 823 of file lapic.c.

825 {
826  struct kvm_lapic *apic = vcpu->arch.apic;
827 
828  return __apic_accept_irq(apic, irq->delivery_mode, irq->vector,
829  irq->level, irq->trig_mode, dest_map);
830 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_ldr()

static void kvm_apic_set_ldr ( struct kvm_lapic apic,
u32  id 
)
inlinestatic

Definition at line 515 of file lapic.c.

516 {
517  kvm_lapic_set_reg(apic, APIC_LDR, id);
518  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
519 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_state()

int kvm_apic_set_state ( struct kvm_vcpu *  vcpu,
struct kvm_lapic_state *  s 
)

Definition at line 2984 of file lapic.c.

2985 {
2986  struct kvm_lapic *apic = vcpu->arch.apic;
2987  int r;
2988 
2989  static_call_cond(kvm_x86_apicv_pre_state_restore)(vcpu);
2990 
2991  kvm_lapic_set_base(vcpu, vcpu->arch.apic_base);
2992  /* set SPIV separately to get count of SW disabled APICs right */
2993  apic_set_spiv(apic, *((u32 *)(s->regs + APIC_SPIV)));
2994 
2995  r = kvm_apic_state_fixup(vcpu, s, true);
2996  if (r) {
2998  return r;
2999  }
3000  memcpy(vcpu->arch.apic->regs, s->regs, sizeof(*s));
3001 
3002  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
3005 
3006  apic_update_ppr(apic);
3007  cancel_apic_timer(apic);
3008  apic->lapic_timer.expired_tscdeadline = 0;
3009  apic_update_lvtt(apic);
3010  apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
3011  update_divide_count(apic);
3012  __start_apic_timer(apic, APIC_TMCCT);
3013  kvm_lapic_set_reg(apic, APIC_TMCCT, 0);
3015  if (apic->apicv_active) {
3016  static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu);
3017  static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, apic_find_highest_irr(apic));
3018  static_call_cond(kvm_x86_hwapic_isr_update)(apic_find_highest_isr(apic));
3019  }
3020  kvm_make_request(KVM_REQ_EVENT, vcpu);
3021  if (ioapic_in_kernel(vcpu->kvm))
3023 
3024  vcpu->arch.apic_arb_prio = 0;
3025 
3026  return 0;
3027 }
void kvm_rtc_eoi_tracking_restore_one(struct kvm_vcpu *vcpu)
Definition: ioapic.c:139
static int ioapic_in_kernel(struct kvm *kvm)
Definition: ioapic.h:104
static void __start_apic_timer(struct kvm_lapic *apic, u32 count_reg)
Definition: lapic.c:2216
void kvm_apic_update_apicv(struct kvm_vcpu *vcpu)
Definition: lapic.c:2578
void kvm_recalculate_apic_map(struct kvm *kvm)
Definition: lapic.c:374
void kvm_lapic_set_base(struct kvm_vcpu *vcpu, u64 value)
Definition: lapic.c:2530
static void update_divide_count(struct kvm_lapic *apic)
Definition: lapic.c:1711
static void apic_update_lvtt(struct kvm_lapic *apic)
Definition: lapic.c:1754
static void apic_set_spiv(struct kvm_lapic *apic, u32 val)
Definition: lapic.c:486
static void apic_manage_nmi_watchdog(struct kvm_lapic *apic, u32 lvt0_val)
Definition: lapic.c:2232
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_version()

void kvm_apic_set_version ( struct kvm_vcpu *  vcpu)

Definition at line 573 of file lapic.c.

574 {
575  struct kvm_lapic *apic = vcpu->arch.apic;
576  u32 v = 0;
577 
578  if (!lapic_in_kernel(vcpu))
579  return;
580 
581  v = APIC_VERSION | ((apic->nr_lvt_entries - 1) << 16);
582 
583  /*
584  * KVM emulates 82093AA datasheet (with in-kernel IOAPIC implementation)
585  * which doesn't have EOI register; Some buggy OSes (e.g. Windows with
586  * Hyper-V role) disable EOI broadcast in lapic not checking for IOAPIC
587  * version first and level-triggered interrupts never get EOIed in
588  * IOAPIC.
589  */
590  if (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) &&
591  !ioapic_in_kernel(vcpu->kvm))
592  v |= APIC_LVR_DIRECTED_EOI;
593  kvm_lapic_set_reg(apic, APIC_LVR, v);
594 }
static __always_inline bool guest_cpuid_has(struct kvm_vcpu *vcpu, unsigned int x86_feature)
Definition: cpuid.h:83
#define APIC_VERSION
Definition: lapic.c:56
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_x2apic_id()

static void kvm_apic_set_x2apic_id ( struct kvm_lapic apic,
u32  id 
)
inlinestatic

Definition at line 527 of file lapic.c.

528 {
529  u32 ldr = kvm_apic_calc_x2apic_ldr(id);
530 
531  WARN_ON_ONCE(id != apic->vcpu->vcpu_id);
532 
533  kvm_lapic_set_reg(apic, APIC_ID, id);
534  kvm_lapic_set_reg(apic, APIC_LDR, ldr);
535  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
536 }
static u32 kvm_apic_calc_x2apic_ldr(u32 id)
Definition: lapic.c:166
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_set_xapic_id()

static void kvm_apic_set_xapic_id ( struct kvm_lapic apic,
u8  id 
)
inlinestatic

Definition at line 509 of file lapic.c.

510 {
511  kvm_lapic_set_reg(apic, APIC_ID, id << 24);
512  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
513 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_state_fixup()

static int kvm_apic_state_fixup ( struct kvm_vcpu *  vcpu,
struct kvm_lapic_state *  s,
bool  set 
)
static

Definition at line 2932 of file lapic.c.

2934 {
2935  if (apic_x2apic_mode(vcpu->arch.apic)) {
2936  u32 *id = (u32 *)(s->regs + APIC_ID);
2937  u32 *ldr = (u32 *)(s->regs + APIC_LDR);
2938  u64 icr;
2939 
2940  if (vcpu->kvm->arch.x2apic_format) {
2941  if (*id != vcpu->vcpu_id)
2942  return -EINVAL;
2943  } else {
2944  if (set)
2945  *id >>= 24;
2946  else
2947  *id <<= 24;
2948  }
2949 
2950  /*
2951  * In x2APIC mode, the LDR is fixed and based on the id. And
2952  * ICR is internally a single 64-bit register, but needs to be
2953  * split to ICR+ICR2 in userspace for backwards compatibility.
2954  */
2955  if (set) {
2956  *ldr = kvm_apic_calc_x2apic_ldr(*id);
2957 
2958  icr = __kvm_lapic_get_reg(s->regs, APIC_ICR) |
2959  (u64)__kvm_lapic_get_reg(s->regs, APIC_ICR2) << 32;
2960  __kvm_lapic_set_reg64(s->regs, APIC_ICR, icr);
2961  } else {
2962  icr = __kvm_lapic_get_reg64(s->regs, APIC_ICR);
2963  __kvm_lapic_set_reg(s->regs, APIC_ICR2, icr >> 32);
2964  }
2965  }
2966 
2967  return 0;
2968 }
static __always_inline void __kvm_lapic_set_reg64(char *regs, int reg, u64 val)
Definition: lapic.c:93
static __always_inline u64 __kvm_lapic_get_reg64(char *regs, int reg)
Definition: lapic.c:82
static u32 __kvm_lapic_get_reg(char *regs, int reg_off)
Definition: lapic.h:174
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_update_apicv()

void kvm_apic_update_apicv ( struct kvm_vcpu *  vcpu)

Definition at line 2578 of file lapic.c.

2579 {
2580  struct kvm_lapic *apic = vcpu->arch.apic;
2581 
2582  if (apic->apicv_active) {
2583  /* irr_pending is always true when apicv is activated. */
2584  apic->irr_pending = true;
2585  apic->isr_count = 1;
2586  } else {
2587  /*
2588  * Don't clear irr_pending, searching the IRR can race with
2589  * updates from the CPU as APICv is still active from hardware's
2590  * perspective. The flag will be cleared as appropriate when
2591  * KVM injects the interrupt.
2592  */
2593  apic->isr_count = count_vectors(apic->regs + APIC_ISR);
2594  }
2595  apic->highest_isr_cache = -1;
2596 }
static u8 count_vectors(void *bitmap)
Definition: lapic.c:640
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_update_irr()

bool kvm_apic_update_irr ( struct kvm_vcpu *  vcpu,
u32 *  pir,
int *  max_irr 
)

Definition at line 690 of file lapic.c.

691 {
692  struct kvm_lapic *apic = vcpu->arch.apic;
693  bool irr_updated = __kvm_apic_update_irr(pir, apic->regs, max_irr);
694 
695  if (unlikely(!apic->apicv_active && irr_updated))
696  apic->irr_pending = true;
697  return irr_updated;
698 }
bool __kvm_apic_update_irr(u32 *pir, void *regs, int *max_irr)
Definition: lapic.c:654
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_update_ppr()

void kvm_apic_update_ppr ( struct kvm_vcpu *  vcpu)

Definition at line 975 of file lapic.c.

976 {
977  apic_update_ppr(vcpu->arch.apic);
978 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_apic_write_nodecode()

void kvm_apic_write_nodecode ( struct kvm_vcpu *  vcpu,
u32  offset 
)

Definition at line 2446 of file lapic.c.

2447 {
2448  struct kvm_lapic *apic = vcpu->arch.apic;
2449 
2450  /*
2451  * ICR is a single 64-bit register when x2APIC is enabled, all others
2452  * registers hold 32-bit values. For legacy xAPIC, ICR writes need to
2453  * go down the common path to get the upper half from ICR2.
2454  *
2455  * Note, using the write helpers may incur an unnecessary write to the
2456  * virtual APIC state, but KVM needs to conditionally modify the value
2457  * in certain cases, e.g. to clear the ICR busy bit. The cost of extra
2458  * conditional branches is likely a wash relative to the cost of the
2459  * maybe-unecessary write, and both are in the noise anyways.
2460  */
2461  if (apic_x2apic_mode(apic) && offset == APIC_ICR)
2462  kvm_x2apic_icr_write(apic, kvm_lapic_get_reg64(apic, APIC_ICR));
2463  else
2464  kvm_lapic_reg_write(apic, offset, kvm_lapic_get_reg(apic, offset));
2465 }
static __always_inline u64 kvm_lapic_get_reg64(struct kvm_lapic *apic, int reg)
Definition: lapic.c:88
int kvm_x2apic_icr_write(struct kvm_lapic *apic, u64 data)
Definition: lapic.c:3155
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_bitmap_or_dest_vcpus()

void kvm_bitmap_or_dest_vcpus ( struct kvm *  kvm,
struct kvm_lapic_irq *  irq,
unsigned long *  vcpu_bitmap 
)

Definition at line 1394 of file lapic.c.

1396 {
1397  struct kvm_lapic **dest_vcpu = NULL;
1398  struct kvm_lapic *src = NULL;
1399  struct kvm_apic_map *map;
1400  struct kvm_vcpu *vcpu;
1401  unsigned long bitmap, i;
1402  int vcpu_idx;
1403  bool ret;
1404 
1405  rcu_read_lock();
1406  map = rcu_dereference(kvm->arch.apic_map);
1407 
1408  ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dest_vcpu,
1409  &bitmap);
1410  if (ret) {
1411  for_each_set_bit(i, &bitmap, 16) {
1412  if (!dest_vcpu[i])
1413  continue;
1414  vcpu_idx = dest_vcpu[i]->vcpu->vcpu_idx;
1415  __set_bit(vcpu_idx, vcpu_bitmap);
1416  }
1417  } else {
1418  kvm_for_each_vcpu(i, vcpu, kvm) {
1419  if (!kvm_apic_present(vcpu))
1420  continue;
1421  if (!kvm_apic_match_dest(vcpu, NULL,
1422  irq->shorthand,
1423  irq->dest_id,
1424  irq->dest_mode))
1425  continue;
1426  __set_bit(i, vcpu_bitmap);
1427  }
1428  }
1429  rcu_read_unlock();
1430 }
bool kvm_apic_match_dest(struct kvm_vcpu *vcpu, struct kvm_lapic *source, int shorthand, unsigned int dest, int dest_mode)
Definition: lapic.c:1067
static bool kvm_apic_map_get_dest_lapic(struct kvm *kvm, struct kvm_lapic **src, struct kvm_lapic_irq *irq, struct kvm_apic_map *map, struct kvm_lapic ***dst, unsigned long *bitmap)
Definition: lapic.c:1142
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_can_post_timer_interrupt()

static bool kvm_can_post_timer_interrupt ( struct kvm_vcpu *  vcpu)
static

Definition at line 148 of file lapic.c.

149 {
150  return pi_inject_timer && kvm_vcpu_apicv_active(vcpu) &&
151  (kvm_mwait_in_guest(vcpu->kvm) || kvm_hlt_in_guest(vcpu->kvm));
152 }
static bool kvm_vcpu_apicv_active(struct kvm_vcpu *vcpu)
Definition: lapic.h:226
int __read_mostly pi_inject_timer
Definition: x86.c:188
static bool kvm_mwait_in_guest(struct kvm *kvm)
Definition: x86.h:409
static bool kvm_hlt_in_guest(struct kvm *kvm)
Definition: x86.h:414
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_can_use_hv_timer()

bool kvm_can_use_hv_timer ( struct kvm_vcpu *  vcpu)

Definition at line 154 of file lapic.c.

155 {
156  return kvm_x86_ops.set_hv_timer
157  && !(kvm_mwait_in_guest(vcpu->kvm) ||
159 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_create_lapic()

int kvm_create_lapic ( struct kvm_vcpu *  vcpu,
int  timer_advance_ns 
)

Definition at line 2810 of file lapic.c.

2811 {
2812  struct kvm_lapic *apic;
2813 
2814  ASSERT(vcpu != NULL);
2815 
2816  apic = kzalloc(sizeof(*apic), GFP_KERNEL_ACCOUNT);
2817  if (!apic)
2818  goto nomem;
2819 
2820  vcpu->arch.apic = apic;
2821 
2822  apic->regs = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
2823  if (!apic->regs) {
2824  printk(KERN_ERR "malloc apic regs error for vcpu %x\n",
2825  vcpu->vcpu_id);
2826  goto nomem_free_apic;
2827  }
2828  apic->vcpu = vcpu;
2829 
2831 
2832  hrtimer_init(&apic->lapic_timer.timer, CLOCK_MONOTONIC,
2833  HRTIMER_MODE_ABS_HARD);
2834  apic->lapic_timer.timer.function = apic_timer_fn;
2835  if (timer_advance_ns == -1) {
2837  lapic_timer_advance_dynamic = true;
2838  } else {
2839  apic->lapic_timer.timer_advance_ns = timer_advance_ns;
2840  lapic_timer_advance_dynamic = false;
2841  }
2842 
2843  /*
2844  * Stuff the APIC ENABLE bit in lieu of temporarily incrementing
2845  * apic_hw_disabled; the full RESET value is set by kvm_lapic_reset().
2846  */
2847  vcpu->arch.apic_base = MSR_IA32_APICBASE_ENABLE;
2848  static_branch_inc(&apic_sw_disabled.key); /* sw disabled at reset */
2850 
2851  return 0;
2852 nomem_free_apic:
2853  kfree(apic);
2854  vcpu->arch.apic = NULL;
2855 nomem:
2856  return -ENOMEM;
2857 }
static void kvm_iodevice_init(struct kvm_io_device *dev, const struct kvm_io_device_ops *ops)
Definition: iodev.h:36
static const struct kvm_io_device_ops apic_mmio_ops
Definition: lapic.c:2790
static enum hrtimer_restart apic_timer_fn(struct hrtimer *data)
Definition: lapic.c:2795
struct kvm_io_device dev
Definition: lapic.h:61
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_free_lapic()

void kvm_free_lapic ( struct kvm_vcpu *  vcpu)

Definition at line 2468 of file lapic.c.

2469 {
2470  struct kvm_lapic *apic = vcpu->arch.apic;
2471 
2472  if (!vcpu->arch.apic)
2473  return;
2474 
2475  hrtimer_cancel(&apic->lapic_timer.timer);
2476 
2477  if (!(vcpu->arch.apic_base & MSR_IA32_APICBASE_ENABLE))
2478  static_branch_slow_dec_deferred(&apic_hw_disabled);
2479 
2480  if (!apic->sw_enabled)
2481  static_branch_slow_dec_deferred(&apic_sw_disabled);
2482 
2483  if (apic->regs)
2484  free_page((unsigned long)apic->regs);
2485 
2486  kfree(apic);
2487 }
struct static_key_false_deferred apic_hw_disabled
Here is the caller graph for this function:

◆ kvm_get_apic_interrupt()

int kvm_get_apic_interrupt ( struct kvm_vcpu *  vcpu)

Definition at line 2894 of file lapic.c.

2895 {
2896  int vector = kvm_apic_has_interrupt(vcpu);
2897  struct kvm_lapic *apic = vcpu->arch.apic;
2898  u32 ppr;
2899 
2900  if (vector == -1)
2901  return -1;
2902 
2903  /*
2904  * We get here even with APIC virtualization enabled, if doing
2905  * nested virtualization and L1 runs with the "acknowledge interrupt
2906  * on exit" mode. Then we cannot inject the interrupt via RVI,
2907  * because the process would deliver it through the IDT.
2908  */
2909 
2910  apic_clear_irr(vector, apic);
2911  if (kvm_hv_synic_auto_eoi_set(vcpu, vector)) {
2912  /*
2913  * For auto-EOI interrupts, there might be another pending
2914  * interrupt above PPR, so check whether to raise another
2915  * KVM_REQ_EVENT.
2916  */
2917  apic_update_ppr(apic);
2918  } else {
2919  /*
2920  * For normal interrupts, PPR has been raised and there cannot
2921  * be a higher-priority pending interrupt---except if there was
2922  * a concurrent interrupt injection, but that would have
2923  * triggered KVM_REQ_EVENT already.
2924  */
2925  apic_set_isr(vector, apic);
2926  __apic_update_ppr(apic, &ppr);
2927  }
2928 
2929  return vector;
2930 }
static bool kvm_hv_synic_auto_eoi_set(struct kvm_vcpu *vcpu, int vector)
Definition: hyperv.h:294
int kvm_apic_has_interrupt(struct kvm_vcpu *vcpu)
Definition: lapic.c:2859
static void apic_set_isr(int vec, struct kvm_lapic *apic)
Definition: lapic.c:744
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_get_lapic_tscdeadline_msr()

u64 kvm_get_lapic_tscdeadline_msr ( struct kvm_vcpu *  vcpu)

Definition at line 2494 of file lapic.c.

2495 {
2496  struct kvm_lapic *apic = vcpu->arch.apic;
2497 
2499  return 0;
2500 
2501  return apic->lapic_timer.tscdeadline;
2502 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_hv_vapic_msr_read()

int kvm_hv_vapic_msr_read ( struct kvm_vcpu *  vcpu,
u32  reg,
u64 *  data 
)

Definition at line 3229 of file lapic.c.

3230 {
3231  if (!lapic_in_kernel(vcpu))
3232  return 1;
3233 
3234  return kvm_lapic_msr_read(vcpu->arch.apic, reg, data);
3235 }
static int kvm_lapic_msr_read(struct kvm_lapic *apic, u32 reg, u64 *data)
Definition: lapic.c:3165
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_hv_vapic_msr_write()

int kvm_hv_vapic_msr_write ( struct kvm_vcpu *  vcpu,
u32  reg,
u64  data 
)

Definition at line 3221 of file lapic.c.

3222 {
3223  if (!lapic_in_kernel(vcpu))
3224  return 1;
3225 
3226  return kvm_lapic_msr_write(vcpu->arch.apic, reg, data);
3227 }
static int kvm_lapic_msr_write(struct kvm_lapic *apic, u32 reg, u64 data)
Definition: lapic.c:3182
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_inhibit_apic_access_page()

void kvm_inhibit_apic_access_page ( struct kvm_vcpu *  vcpu)

Definition at line 2634 of file lapic.c.

2635 {
2636  struct kvm *kvm = vcpu->kvm;
2637 
2638  if (!kvm->arch.apic_access_memslot_enabled)
2639  return;
2640 
2641  kvm_vcpu_srcu_read_unlock(vcpu);
2642 
2643  mutex_lock(&kvm->slots_lock);
2644 
2645  if (kvm->arch.apic_access_memslot_enabled) {
2646  __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
2647  /*
2648  * Clear "enabled" after the memslot is deleted so that a
2649  * different vCPU doesn't get a false negative when checking
2650  * the flag out of slots_lock. No additional memory barrier is
2651  * needed as modifying memslots requires waiting other vCPUs to
2652  * drop SRCU (see above), and false positives are ok as the
2653  * flag is rechecked after acquiring slots_lock.
2654  */
2655  kvm->arch.apic_access_memslot_enabled = false;
2656 
2657  /*
2658  * Mark the memslot as inhibited to prevent reallocating the
2659  * memslot during vCPU creation, e.g. if a vCPU is hotplugged.
2660  */
2661  kvm->arch.apic_access_memslot_inhibited = true;
2662  }
2663 
2664  mutex_unlock(&kvm->slots_lock);
2665 
2666  kvm_vcpu_srcu_read_lock(vcpu);
2667 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_inject_apic_timer_irqs()

void kvm_inject_apic_timer_irqs ( struct kvm_vcpu *  vcpu)

Definition at line 2884 of file lapic.c.

2885 {
2886  struct kvm_lapic *apic = vcpu->arch.apic;
2887 
2888  if (atomic_read(&apic->lapic_timer.pending) > 0) {
2890  atomic_set(&apic->lapic_timer.pending, 0);
2891  }
2892 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_intr_is_single_vcpu_fast()

bool kvm_intr_is_single_vcpu_fast ( struct kvm *  kvm,
struct kvm_lapic_irq *  irq,
struct kvm_vcpu **  dest_vcpu 
)

Definition at line 1259 of file lapic.c.

1261 {
1262  struct kvm_apic_map *map;
1263  unsigned long bitmap;
1264  struct kvm_lapic **dst = NULL;
1265  bool ret = false;
1266 
1267  if (irq->shorthand)
1268  return false;
1269 
1270  rcu_read_lock();
1271  map = rcu_dereference(kvm->arch.apic_map);
1272 
1273  if (kvm_apic_map_get_dest_lapic(kvm, NULL, irq, map, &dst, &bitmap) &&
1274  hweight16(bitmap) == 1) {
1275  unsigned long i = find_first_bit(&bitmap, 16);
1276 
1277  if (dst[i]) {
1278  *dest_vcpu = dst[i]->vcpu;
1279  ret = true;
1280  }
1281  }
1282 
1283  rcu_read_unlock();
1284  return ret;
1285 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_ioapic_handles_vector()

static bool kvm_ioapic_handles_vector ( struct kvm_lapic apic,
int  vector 
)
static

Definition at line 1437 of file lapic.c.

1438 {
1439  return test_bit(vector, apic->vcpu->arch.ioapic_handled_vectors);
1440 }
Here is the caller graph for this function:

◆ kvm_ioapic_send_eoi()

static void kvm_ioapic_send_eoi ( struct kvm_lapic apic,
int  vector 
)
static

Definition at line 1442 of file lapic.c.

1443 {
1444  int trigger_mode;
1445 
1446  /* Eoi the ioapic only if the ioapic doesn't own the vector. */
1447  if (!kvm_ioapic_handles_vector(apic, vector))
1448  return;
1449 
1450  /* Request a KVM exit to inform the userspace IOAPIC. */
1451  if (irqchip_split(apic->vcpu->kvm)) {
1452  apic->vcpu->arch.pending_ioapic_eoi = vector;
1453  kvm_make_request(KVM_REQ_IOAPIC_EOI_EXIT, apic->vcpu);
1454  return;
1455  }
1456 
1457  if (apic_test_vector(vector, apic->regs + APIC_TMR))
1458  trigger_mode = IOAPIC_LEVEL_TRIG;
1459  else
1460  trigger_mode = IOAPIC_EDGE_TRIG;
1461 
1462  kvm_ioapic_update_eoi(apic->vcpu, vector, trigger_mode);
1463 }
void kvm_ioapic_update_eoi(struct kvm_vcpu *vcpu, int vector, int trigger_mode)
Definition: ioapic.c:579
#define IOAPIC_LEVEL_TRIG
Definition: ioapic.h:16
#define IOAPIC_EDGE_TRIG
Definition: ioapic.h:15
static int irqchip_split(struct kvm *kvm)
Definition: irq.h:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_irq_delivery_to_apic_fast()

bool kvm_irq_delivery_to_apic_fast ( struct kvm *  kvm,
struct kvm_lapic src,
struct kvm_lapic_irq *  irq,
int *  r,
struct dest_map dest_map 
)

Definition at line 1208 of file lapic.c.

1210 {
1211  struct kvm_apic_map *map;
1212  unsigned long bitmap;
1213  struct kvm_lapic **dst = NULL;
1214  int i;
1215  bool ret;
1216 
1217  *r = -1;
1218 
1219  if (irq->shorthand == APIC_DEST_SELF) {
1220  if (KVM_BUG_ON(!src, kvm)) {
1221  *r = 0;
1222  return true;
1223  }
1224  *r = kvm_apic_set_irq(src->vcpu, irq, dest_map);
1225  return true;
1226  }
1227 
1228  rcu_read_lock();
1229  map = rcu_dereference(kvm->arch.apic_map);
1230 
1231  ret = kvm_apic_map_get_dest_lapic(kvm, &src, irq, map, &dst, &bitmap);
1232  if (ret) {
1233  *r = 0;
1234  for_each_set_bit(i, &bitmap, 16) {
1235  if (!dst[i])
1236  continue;
1237  *r += kvm_apic_set_irq(dst[i]->vcpu, irq, dest_map);
1238  }
1239  }
1240 
1241  rcu_read_unlock();
1242  return ret;
1243 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_exit()

void kvm_lapic_exit ( void  )

Definition at line 3313 of file lapic.c.

3314 {
3315  static_key_deferred_flush(&apic_hw_disabled);
3316  WARN_ON(static_branch_unlikely(&apic_hw_disabled.key));
3317  static_key_deferred_flush(&apic_sw_disabled);
3318  WARN_ON(static_branch_unlikely(&apic_sw_disabled.key));
3319 }
Here is the caller graph for this function:

◆ kvm_lapic_expired_hv_timer()

void kvm_lapic_expired_hv_timer ( struct kvm_vcpu *  vcpu)

Definition at line 2171 of file lapic.c.

2172 {
2173  struct kvm_lapic *apic = vcpu->arch.apic;
2174 
2175  preempt_disable();
2176  /* If the preempt notifier has already run, it also called apic_timer_expired */
2177  if (!apic->lapic_timer.hv_timer_in_use)
2178  goto out;
2179  WARN_ON(kvm_vcpu_is_blocking(vcpu));
2180  apic_timer_expired(apic, false);
2181  cancel_hv_timer(apic);
2182 
2183  if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
2185  restart_apic_timer(apic);
2186  }
2187 out:
2188  preempt_enable();
2189 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_find_highest_irr()

int kvm_lapic_find_highest_irr ( struct kvm_vcpu *  vcpu)

Definition at line 808 of file lapic.c.

809 {
810  /* This may race with setting of irr in __apic_accept_irq() and
811  * value returned may be wrong, but kvm_vcpu_kick() in __apic_accept_irq
812  * will cause vmexit immediately and the value will be recalculated
813  * on the next vmentry.
814  */
815  return apic_find_highest_irr(vcpu->arch.apic);
816 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_get_cr8()

u64 kvm_lapic_get_cr8 ( struct kvm_vcpu *  vcpu)

Definition at line 2521 of file lapic.c.

2522 {
2523  u64 tpr;
2524 
2525  tpr = (u64) kvm_lapic_get_reg(vcpu->arch.apic, APIC_TASKPRI);
2526 
2527  return (tpr & 0xf0) >> 4;
2528 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_get_reg64()

static __always_inline u64 kvm_lapic_get_reg64 ( struct kvm_lapic apic,
int  reg 
)
static

Definition at line 88 of file lapic.c.

89 {
90  return __kvm_lapic_get_reg64(apic->regs, reg);
91 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_hv_timer_in_use()

bool kvm_lapic_hv_timer_in_use ( struct kvm_vcpu *  vcpu)

Definition at line 2083 of file lapic.c.

2084 {
2085  if (!lapic_in_kernel(vcpu))
2086  return false;
2087 
2088  return vcpu->arch.apic->lapic_timer.hv_timer_in_use;
2089 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_lvt_supported()

static bool kvm_lapic_lvt_supported ( struct kvm_lapic apic,
int  lvt_index 
)
inlinestatic

Definition at line 563 of file lapic.c.

564 {
565  return apic->nr_lvt_entries > lvt_index;
566 }
Here is the caller graph for this function:

◆ kvm_lapic_msr_read()

static int kvm_lapic_msr_read ( struct kvm_lapic apic,
u32  reg,
u64 *  data 
)
static

Definition at line 3165 of file lapic.c.

3166 {
3167  u32 low;
3168 
3169  if (reg == APIC_ICR) {
3170  *data = kvm_lapic_get_reg64(apic, APIC_ICR);
3171  return 0;
3172  }
3173 
3174  if (kvm_lapic_reg_read(apic, reg, 4, &low))
3175  return 1;
3176 
3177  *data = low;
3178 
3179  return 0;
3180 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_msr_write()

static int kvm_lapic_msr_write ( struct kvm_lapic apic,
u32  reg,
u64  data 
)
static

Definition at line 3182 of file lapic.c.

3183 {
3184  /*
3185  * ICR is a 64-bit register in x2APIC mode (and Hyper-V PV vAPIC) and
3186  * can be written as such, all other registers remain accessible only
3187  * through 32-bit reads/writes.
3188  */
3189  if (reg == APIC_ICR)
3190  return kvm_x2apic_icr_write(apic, data);
3191 
3192  /* Bits 63:32 are reserved in all other registers. */
3193  if (data >> 32)
3194  return 1;
3195 
3196  return kvm_lapic_reg_write(apic, reg, (u32)data);
3197 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_readable_reg_mask()

u64 kvm_lapic_readable_reg_mask ( struct kvm_lapic apic)

Definition at line 1607 of file lapic.c.

1608 {
1609  /* Leave bits '0' for reserved and write-only registers. */
1610  u64 valid_reg_mask =
1611  APIC_REG_MASK(APIC_ID) |
1612  APIC_REG_MASK(APIC_LVR) |
1613  APIC_REG_MASK(APIC_TASKPRI) |
1614  APIC_REG_MASK(APIC_PROCPRI) |
1615  APIC_REG_MASK(APIC_LDR) |
1616  APIC_REG_MASK(APIC_SPIV) |
1617  APIC_REGS_MASK(APIC_ISR, APIC_ISR_NR) |
1618  APIC_REGS_MASK(APIC_TMR, APIC_ISR_NR) |
1619  APIC_REGS_MASK(APIC_IRR, APIC_ISR_NR) |
1620  APIC_REG_MASK(APIC_ESR) |
1621  APIC_REG_MASK(APIC_ICR) |
1622  APIC_REG_MASK(APIC_LVTT) |
1623  APIC_REG_MASK(APIC_LVTTHMR) |
1624  APIC_REG_MASK(APIC_LVTPC) |
1625  APIC_REG_MASK(APIC_LVT0) |
1626  APIC_REG_MASK(APIC_LVT1) |
1627  APIC_REG_MASK(APIC_LVTERR) |
1628  APIC_REG_MASK(APIC_TMICT) |
1629  APIC_REG_MASK(APIC_TMCCT) |
1630  APIC_REG_MASK(APIC_TDCR);
1631 
1632  if (kvm_lapic_lvt_supported(apic, LVT_CMCI))
1633  valid_reg_mask |= APIC_REG_MASK(APIC_LVTCMCI);
1634 
1635  /* ARBPRI, DFR, and ICR2 are not valid in x2APIC mode. */
1636  if (!apic_x2apic_mode(apic))
1637  valid_reg_mask |= APIC_REG_MASK(APIC_ARBPRI) |
1638  APIC_REG_MASK(APIC_DFR) |
1639  APIC_REG_MASK(APIC_ICR2);
1640 
1641  return valid_reg_mask;
1642 }
#define APIC_REG_MASK(reg)
Definition: lapic.c:1603
static bool kvm_lapic_lvt_supported(struct kvm_lapic *apic, int lvt_index)
Definition: lapic.c:563
#define APIC_REGS_MASK(first, count)
Definition: lapic.c:1604
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_reg_read()

static int kvm_lapic_reg_read ( struct kvm_lapic apic,
u32  offset,
int  len,
void *  data 
)
static

Definition at line 1645 of file lapic.c.

1647 {
1648  unsigned char alignment = offset & 0xf;
1649  u32 result;
1650 
1651  /*
1652  * WARN if KVM reads ICR in x2APIC mode, as it's an 8-byte register in
1653  * x2APIC and needs to be manually handled by the caller.
1654  */
1655  WARN_ON_ONCE(apic_x2apic_mode(apic) && offset == APIC_ICR);
1656 
1657  if (alignment + len > 4)
1658  return 1;
1659 
1660  if (offset > 0x3f0 ||
1661  !(kvm_lapic_readable_reg_mask(apic) & APIC_REG_MASK(offset)))
1662  return 1;
1663 
1664  result = __apic_read(apic, offset & ~0xf);
1665 
1666  trace_kvm_apic_read(offset, result);
1667 
1668  switch (len) {
1669  case 1:
1670  case 2:
1671  case 4:
1672  memcpy(data, (char *)&result + alignment, len);
1673  break;
1674  default:
1675  printk(KERN_ERR "Local APIC read with len = %x, "
1676  "should be 1,2, or 4 instead\n", len);
1677  break;
1678  }
1679  return 0;
1680 }
u64 kvm_lapic_readable_reg_mask(struct kvm_lapic *apic)
Definition: lapic.c:1607
#define trace_kvm_apic_read(reg, val)
Definition: trace.h:280
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_reg_write()

static int kvm_lapic_reg_write ( struct kvm_lapic apic,
u32  reg,
u32  val 
)
static

Definition at line 2255 of file lapic.c.

2256 {
2257  int ret = 0;
2258 
2259  trace_kvm_apic_write(reg, val);
2260 
2261  switch (reg) {
2262  case APIC_ID: /* Local APIC ID */
2263  if (!apic_x2apic_mode(apic)) {
2264  kvm_apic_set_xapic_id(apic, val >> 24);
2265  } else {
2266  ret = 1;
2267  }
2268  break;
2269 
2270  case APIC_TASKPRI:
2271  report_tpr_access(apic, true);
2272  apic_set_tpr(apic, val & 0xff);
2273  break;
2274 
2275  case APIC_EOI:
2276  apic_set_eoi(apic);
2277  break;
2278 
2279  case APIC_LDR:
2280  if (!apic_x2apic_mode(apic))
2281  kvm_apic_set_ldr(apic, val & APIC_LDR_MASK);
2282  else
2283  ret = 1;
2284  break;
2285 
2286  case APIC_DFR:
2287  if (!apic_x2apic_mode(apic))
2288  kvm_apic_set_dfr(apic, val | 0x0FFFFFFF);
2289  else
2290  ret = 1;
2291  break;
2292 
2293  case APIC_SPIV: {
2294  u32 mask = 0x3ff;
2295  if (kvm_lapic_get_reg(apic, APIC_LVR) & APIC_LVR_DIRECTED_EOI)
2296  mask |= APIC_SPIV_DIRECTED_EOI;
2297  apic_set_spiv(apic, val & mask);
2298  if (!(val & APIC_SPIV_APIC_ENABLED)) {
2299  int i;
2300 
2301  for (i = 0; i < apic->nr_lvt_entries; i++) {
2302  kvm_lapic_set_reg(apic, APIC_LVTx(i),
2303  kvm_lapic_get_reg(apic, APIC_LVTx(i)) | APIC_LVT_MASKED);
2304  }
2305  apic_update_lvtt(apic);
2306  atomic_set(&apic->lapic_timer.pending, 0);
2307 
2308  }
2309  break;
2310  }
2311  case APIC_ICR:
2312  WARN_ON_ONCE(apic_x2apic_mode(apic));
2313 
2314  /* No delay here, so we always clear the pending bit */
2315  val &= ~APIC_ICR_BUSY;
2316  kvm_apic_send_ipi(apic, val, kvm_lapic_get_reg(apic, APIC_ICR2));
2317  kvm_lapic_set_reg(apic, APIC_ICR, val);
2318  break;
2319  case APIC_ICR2:
2320  if (apic_x2apic_mode(apic))
2321  ret = 1;
2322  else
2323  kvm_lapic_set_reg(apic, APIC_ICR2, val & 0xff000000);
2324  break;
2325 
2326  case APIC_LVT0:
2327  apic_manage_nmi_watchdog(apic, val);
2328  fallthrough;
2329  case APIC_LVTTHMR:
2330  case APIC_LVTPC:
2331  case APIC_LVT1:
2332  case APIC_LVTERR:
2333  case APIC_LVTCMCI: {
2334  u32 index = get_lvt_index(reg);
2335  if (!kvm_lapic_lvt_supported(apic, index)) {
2336  ret = 1;
2337  break;
2338  }
2339  if (!kvm_apic_sw_enabled(apic))
2340  val |= APIC_LVT_MASKED;
2341  val &= apic_lvt_mask[index];
2342  kvm_lapic_set_reg(apic, reg, val);
2343  break;
2344  }
2345 
2346  case APIC_LVTT:
2347  if (!kvm_apic_sw_enabled(apic))
2348  val |= APIC_LVT_MASKED;
2349  val &= (apic_lvt_mask[0] | apic->lapic_timer.timer_mode_mask);
2350  kvm_lapic_set_reg(apic, APIC_LVTT, val);
2351  apic_update_lvtt(apic);
2352  break;
2353 
2354  case APIC_TMICT:
2355  if (apic_lvtt_tscdeadline(apic))
2356  break;
2357 
2358  cancel_apic_timer(apic);
2359  kvm_lapic_set_reg(apic, APIC_TMICT, val);
2360  start_apic_timer(apic);
2361  break;
2362 
2363  case APIC_TDCR: {
2364  uint32_t old_divisor = apic->divide_count;
2365 
2366  kvm_lapic_set_reg(apic, APIC_TDCR, val & 0xb);
2367  update_divide_count(apic);
2368  if (apic->divide_count != old_divisor &&
2369  apic->lapic_timer.period) {
2370  hrtimer_cancel(&apic->lapic_timer.timer);
2371  update_target_expiration(apic, old_divisor);
2372  restart_apic_timer(apic);
2373  }
2374  break;
2375  }
2376  case APIC_ESR:
2377  if (apic_x2apic_mode(apic) && val != 0)
2378  ret = 1;
2379  break;
2380 
2381  case APIC_SELF_IPI:
2382  /*
2383  * Self-IPI exists only when x2APIC is enabled. Bits 7:0 hold
2384  * the vector, everything else is reserved.
2385  */
2386  if (!apic_x2apic_mode(apic) || (val & ~APIC_VECTOR_MASK))
2387  ret = 1;
2388  else
2389  kvm_apic_send_ipi(apic, APIC_DEST_SELF | val, 0);
2390  break;
2391  default:
2392  ret = 1;
2393  break;
2394  }
2395 
2396  /*
2397  * Recalculate APIC maps if necessary, e.g. if the software enable bit
2398  * was toggled, the APIC ID changed, etc... The maps are marked dirty
2399  * on relevant changes, i.e. this is a nop for most writes.
2400  */
2401  kvm_recalculate_apic_map(apic->vcpu->kvm);
2402 
2403  return ret;
2404 }
static void apic_set_tpr(struct kvm_lapic *apic, u32 tpr)
Definition: lapic.c:981
static void kvm_apic_set_ldr(struct kvm_lapic *apic, u32 id)
Definition: lapic.c:515
void kvm_apic_send_ipi(struct kvm_lapic *apic, u32 icr_low, u32 icr_high)
Definition: lapic.c:1504
static void kvm_apic_set_dfr(struct kvm_lapic *apic, u32 val)
Definition: lapic.c:521
static const unsigned int apic_lvt_mask[KVM_APIC_MAX_NR_LVT_ENTRIES]
Definition: lapic.c:615
static void start_apic_timer(struct kvm_lapic *apic)
Definition: lapic.c:2227
static void update_target_expiration(struct kvm_lapic *apic, uint32_t old_divisor)
Definition: lapic.c:1968
static void kvm_apic_set_xapic_id(struct kvm_lapic *apic, u8 id)
Definition: lapic.c:509
static int get_lvt_index(u32 reg)
Definition: lapic.c:2245
#define trace_kvm_apic_write(reg, val)
Definition: trace.h:281
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_reset()

void kvm_lapic_reset ( struct kvm_vcpu *  vcpu,
bool  init_event 
)

Definition at line 2669 of file lapic.c.

2670 {
2671  struct kvm_lapic *apic = vcpu->arch.apic;
2672  u64 msr_val;
2673  int i;
2674 
2675  static_call_cond(kvm_x86_apicv_pre_state_restore)(vcpu);
2676 
2677  if (!init_event) {
2678  msr_val = APIC_DEFAULT_PHYS_BASE | MSR_IA32_APICBASE_ENABLE;
2680  msr_val |= MSR_IA32_APICBASE_BSP;
2681  kvm_lapic_set_base(vcpu, msr_val);
2682  }
2683 
2684  if (!apic)
2685  return;
2686 
2687  /* Stop the timer in case it's a reset to an active apic */
2688  hrtimer_cancel(&apic->lapic_timer.timer);
2689 
2690  /* The xAPIC ID is set at RESET even if the APIC was already enabled. */
2691  if (!init_event)
2692  kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2693  kvm_apic_set_version(apic->vcpu);
2694 
2695  for (i = 0; i < apic->nr_lvt_entries; i++)
2696  kvm_lapic_set_reg(apic, APIC_LVTx(i), APIC_LVT_MASKED);
2697  apic_update_lvtt(apic);
2698  if (kvm_vcpu_is_reset_bsp(vcpu) &&
2699  kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_LINT0_REENABLED))
2700  kvm_lapic_set_reg(apic, APIC_LVT0,
2701  SET_APIC_DELIVERY_MODE(0, APIC_MODE_EXTINT));
2702  apic_manage_nmi_watchdog(apic, kvm_lapic_get_reg(apic, APIC_LVT0));
2703 
2704  kvm_apic_set_dfr(apic, 0xffffffffU);
2705  apic_set_spiv(apic, 0xff);
2706  kvm_lapic_set_reg(apic, APIC_TASKPRI, 0);
2707  if (!apic_x2apic_mode(apic))
2708  kvm_apic_set_ldr(apic, 0);
2709  kvm_lapic_set_reg(apic, APIC_ESR, 0);
2710  if (!apic_x2apic_mode(apic)) {
2711  kvm_lapic_set_reg(apic, APIC_ICR, 0);
2712  kvm_lapic_set_reg(apic, APIC_ICR2, 0);
2713  } else {
2714  kvm_lapic_set_reg64(apic, APIC_ICR, 0);
2715  }
2716  kvm_lapic_set_reg(apic, APIC_TDCR, 0);
2717  kvm_lapic_set_reg(apic, APIC_TMICT, 0);
2718  for (i = 0; i < 8; i++) {
2719  kvm_lapic_set_reg(apic, APIC_IRR + 0x10 * i, 0);
2720  kvm_lapic_set_reg(apic, APIC_ISR + 0x10 * i, 0);
2721  kvm_lapic_set_reg(apic, APIC_TMR + 0x10 * i, 0);
2722  }
2724  update_divide_count(apic);
2725  atomic_set(&apic->lapic_timer.pending, 0);
2726 
2727  vcpu->arch.pv_eoi.msr_val = 0;
2728  apic_update_ppr(apic);
2729  if (apic->apicv_active) {
2730  static_call_cond(kvm_x86_apicv_post_state_restore)(vcpu);
2731  static_call_cond(kvm_x86_hwapic_irr_update)(vcpu, -1);
2732  static_call_cond(kvm_x86_hwapic_isr_update)(-1);
2733  }
2734 
2735  vcpu->arch.apic_arb_prio = 0;
2736  vcpu->arch.apic_attention = 0;
2737 
2739 }
static __always_inline void kvm_lapic_set_reg64(struct kvm_lapic *apic, int reg, u64 val)
Definition: lapic.c:99
bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
Definition: x86.c:12488
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_restart_hv_timer()

void kvm_lapic_restart_hv_timer ( struct kvm_vcpu *  vcpu)

Definition at line 2208 of file lapic.c.

2209 {
2210  struct kvm_lapic *apic = vcpu->arch.apic;
2211 
2212  WARN_ON(!apic->lapic_timer.hv_timer_in_use);
2213  restart_apic_timer(apic);
2214 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_base()

void kvm_lapic_set_base ( struct kvm_vcpu *  vcpu,
u64  value 
)

Definition at line 2530 of file lapic.c.

2531 {
2532  u64 old_value = vcpu->arch.apic_base;
2533  struct kvm_lapic *apic = vcpu->arch.apic;
2534 
2535  vcpu->arch.apic_base = value;
2536 
2537  if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE)
2539 
2540  if (!apic)
2541  return;
2542 
2543  /* update jump label if enable bit changes */
2544  if ((old_value ^ value) & MSR_IA32_APICBASE_ENABLE) {
2545  if (value & MSR_IA32_APICBASE_ENABLE) {
2546  kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2547  static_branch_slow_dec_deferred(&apic_hw_disabled);
2548  /* Check if there are APF page ready requests pending */
2549  kvm_make_request(KVM_REQ_APF_READY, vcpu);
2550  } else {
2551  static_branch_inc(&apic_hw_disabled.key);
2552  atomic_set_release(&apic->vcpu->kvm->arch.apic_map_dirty, DIRTY);
2553  }
2554  }
2555 
2556  if ((old_value ^ value) & X2APIC_ENABLE) {
2557  if (value & X2APIC_ENABLE)
2558  kvm_apic_set_x2apic_id(apic, vcpu->vcpu_id);
2559  else if (value & MSR_IA32_APICBASE_ENABLE)
2560  kvm_apic_set_xapic_id(apic, vcpu->vcpu_id);
2561  }
2562 
2563  if ((old_value ^ value) & (MSR_IA32_APICBASE_ENABLE | X2APIC_ENABLE)) {
2564  kvm_make_request(KVM_REQ_APICV_UPDATE, vcpu);
2565  static_call_cond(kvm_x86_set_virtual_apic_mode)(vcpu);
2566  }
2567 
2568  apic->base_address = apic->vcpu->arch.apic_base &
2569  MSR_IA32_APICBASE_BASE;
2570 
2571  if ((value & MSR_IA32_APICBASE_ENABLE) &&
2572  apic->base_address != APIC_DEFAULT_PHYS_BASE) {
2573  kvm_set_apicv_inhibit(apic->vcpu->kvm,
2574  APICV_INHIBIT_REASON_APIC_BASE_MODIFIED);
2575  }
2576 }
void kvm_update_cpuid_runtime(struct kvm_vcpu *vcpu)
Definition: cpuid.c:309
static void kvm_apic_set_x2apic_id(struct kvm_lapic *apic, u32 id)
Definition: lapic.c:527
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_eoi()

void kvm_lapic_set_eoi ( struct kvm_vcpu *  vcpu)

Definition at line 2439 of file lapic.c.

2440 {
2441  kvm_lapic_reg_write(vcpu->arch.apic, APIC_EOI, 0);
2442 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_pv_eoi()

int kvm_lapic_set_pv_eoi ( struct kvm_vcpu *  vcpu,
u64  data,
unsigned long  len 
)

Definition at line 3237 of file lapic.c.

3238 {
3239  u64 addr = data & ~KVM_MSR_ENABLED;
3240  struct gfn_to_hva_cache *ghc = &vcpu->arch.pv_eoi.data;
3241  unsigned long new_len;
3242  int ret;
3243 
3244  if (!IS_ALIGNED(addr, 4))
3245  return 1;
3246 
3247  if (data & KVM_MSR_ENABLED) {
3248  if (addr == ghc->gpa && len <= ghc->len)
3249  new_len = ghc->len;
3250  else
3251  new_len = len;
3252 
3253  ret = kvm_gfn_to_hva_cache_init(vcpu->kvm, ghc, addr, new_len);
3254  if (ret)
3255  return ret;
3256  }
3257 
3258  vcpu->arch.pv_eoi.msr_val = data;
3259 
3260  return 0;
3261 }
int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc, gpa_t gpa, unsigned long len)
Definition: kvm_main.c:3532
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_reg()

static void kvm_lapic_set_reg ( struct kvm_lapic apic,
int  reg_off,
u32  val 
)
inlinestatic

Definition at line 77 of file lapic.c.

78 {
79  __kvm_lapic_set_reg(apic->regs, reg_off, val);
80 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_reg64()

static __always_inline void kvm_lapic_set_reg64 ( struct kvm_lapic apic,
int  reg,
u64  val 
)
static

Definition at line 99 of file lapic.c.

101 {
102  __kvm_lapic_set_reg64(apic->regs, reg, val);
103 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_tpr()

void kvm_lapic_set_tpr ( struct kvm_vcpu *  vcpu,
unsigned long  cr8 
)

Definition at line 2516 of file lapic.c.

2517 {
2518  apic_set_tpr(vcpu->arch.apic, (cr8 & 0x0f) << 4);
2519 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_set_vapic_addr()

int kvm_lapic_set_vapic_addr ( struct kvm_vcpu *  vcpu,
gpa_t  vapic_addr 
)

Definition at line 3139 of file lapic.c.

3140 {
3141  if (vapic_addr) {
3142  if (kvm_gfn_to_hva_cache_init(vcpu->kvm,
3143  &vcpu->arch.apic->vapic_cache,
3144  vapic_addr, sizeof(u32)))
3145  return -EINVAL;
3146  __set_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
3147  } else {
3148  __clear_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention);
3149  }
3150 
3151  vcpu->arch.apic->vapic_addr = vapic_addr;
3152  return 0;
3153 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_switch_to_hv_timer()

void kvm_lapic_switch_to_hv_timer ( struct kvm_vcpu *  vcpu)

Definition at line 2192 of file lapic.c.

2193 {
2194  restart_apic_timer(vcpu->arch.apic);
2195 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_switch_to_sw_timer()

void kvm_lapic_switch_to_sw_timer ( struct kvm_vcpu *  vcpu)

Definition at line 2197 of file lapic.c.

2198 {
2199  struct kvm_lapic *apic = vcpu->arch.apic;
2200 
2201  preempt_disable();
2202  /* Possibly the TSC deadline timer is not enabled yet */
2203  if (apic->lapic_timer.hv_timer_in_use)
2204  start_sw_timer(apic);
2205  preempt_enable();
2206 }
static void start_sw_timer(struct kvm_lapic *apic)
Definition: lapic.c:2141
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_sync_from_vapic()

void kvm_lapic_sync_from_vapic ( struct kvm_vcpu *  vcpu)

Definition at line 3072 of file lapic.c.

3073 {
3074  u32 data;
3075 
3076  if (test_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention))
3077  apic_sync_pv_eoi_from_guest(vcpu, vcpu->arch.apic);
3078 
3079  if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
3080  return;
3081 
3082  if (kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
3083  sizeof(u32)))
3084  return;
3085 
3086  apic_set_tpr(vcpu->arch.apic, data & 0xff);
3087 }
int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, void *data, unsigned long len)
Definition: kvm_main.c:3608
static void apic_sync_pv_eoi_from_guest(struct kvm_vcpu *vcpu, struct kvm_lapic *apic)
Definition: lapic.c:3049
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_lapic_sync_to_vapic()

void kvm_lapic_sync_to_vapic ( struct kvm_vcpu *  vcpu)

Definition at line 3115 of file lapic.c.

3116 {
3117  u32 data, tpr;
3118  int max_irr, max_isr;
3119  struct kvm_lapic *apic = vcpu->arch.apic;
3120 
3122 
3123  if (!test_bit(KVM_APIC_CHECK_VAPIC, &vcpu->arch.apic_attention))
3124  return;
3125 
3126  tpr = kvm_lapic_get_reg(apic, APIC_TASKPRI) & 0xff;
3127  max_irr = apic_find_highest_irr(apic);
3128  if (max_irr < 0)
3129  max_irr = 0;
3130  max_isr = apic_find_highest_isr(apic);
3131  if (max_isr < 0)
3132  max_isr = 0;
3133  data = (tpr & 0xff) | ((max_isr & 0xf0) << 8) | (max_irr << 24);
3134 
3135  kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apic->vapic_cache, &data,
3136  sizeof(u32));
3137 }
int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc, void *data, unsigned long len)
Definition: kvm_main.c:3571
static void apic_sync_pv_eoi_to_guest(struct kvm_vcpu *vcpu, struct kvm_lapic *apic)
Definition: lapic.c:3095
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_pv_send_ipi()

int kvm_pv_send_ipi ( struct kvm *  kvm,
unsigned long  ipi_bitmap_low,
unsigned long  ipi_bitmap_high,
u32  min,
unsigned long  icr,
int  op_64_bit 
)

Definition at line 852 of file lapic.c.

855 {
856  struct kvm_apic_map *map;
857  struct kvm_lapic_irq irq = {0};
858  int cluster_size = op_64_bit ? 64 : 32;
859  int count;
860 
861  if (icr & (APIC_DEST_MASK | APIC_SHORT_MASK))
862  return -KVM_EINVAL;
863 
864  irq.vector = icr & APIC_VECTOR_MASK;
865  irq.delivery_mode = icr & APIC_MODE_MASK;
866  irq.level = (icr & APIC_INT_ASSERT) != 0;
867  irq.trig_mode = icr & APIC_INT_LEVELTRIG;
868 
869  rcu_read_lock();
870  map = rcu_dereference(kvm->arch.apic_map);
871 
872  count = -EOPNOTSUPP;
873  if (likely(map)) {
874  count = __pv_send_ipi(&ipi_bitmap_low, map, &irq, min);
875  min += cluster_size;
876  count += __pv_send_ipi(&ipi_bitmap_high, map, &irq, min);
877  }
878 
879  rcu_read_unlock();
880  return count;
881 }
static int __pv_send_ipi(unsigned long *ipi_bitmap, struct kvm_apic_map *map, struct kvm_lapic_irq *irq, u32 min)
Definition: lapic.c:832
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_recalculate_apic_map()

void kvm_recalculate_apic_map ( struct kvm *  kvm)

Definition at line 374 of file lapic.c.

375 {
376  struct kvm_apic_map *new, *old = NULL;
377  struct kvm_vcpu *vcpu;
378  unsigned long i;
379  u32 max_id = 255; /* enough space for any xAPIC ID */
380  bool xapic_id_mismatch;
381  int r;
382 
383  /* Read kvm->arch.apic_map_dirty before kvm->arch.apic_map. */
384  if (atomic_read_acquire(&kvm->arch.apic_map_dirty) == CLEAN)
385  return;
386 
387  WARN_ONCE(!irqchip_in_kernel(kvm),
388  "Dirty APIC map without an in-kernel local APIC");
389 
390  mutex_lock(&kvm->arch.apic_map_lock);
391 
392 retry:
393  /*
394  * Read kvm->arch.apic_map_dirty before kvm->arch.apic_map (if clean)
395  * or the APIC registers (if dirty). Note, on retry the map may have
396  * not yet been marked dirty by whatever task changed a vCPU's x2APIC
397  * ID, i.e. the map may still show up as in-progress. In that case
398  * this task still needs to retry and complete its calculation.
399  */
400  if (atomic_cmpxchg_acquire(&kvm->arch.apic_map_dirty,
402  /* Someone else has updated the map. */
403  mutex_unlock(&kvm->arch.apic_map_lock);
404  return;
405  }
406 
407  /*
408  * Reset the mismatch flag between attempts so that KVM does the right
409  * thing if a vCPU changes its xAPIC ID, but do NOT reset max_id, i.e.
410  * keep max_id strictly increasing. Disallowing max_id from shrinking
411  * ensures KVM won't get stuck in an infinite loop, e.g. if the vCPU
412  * with the highest x2APIC ID is toggling its APIC on and off.
413  */
414  xapic_id_mismatch = false;
415 
416  kvm_for_each_vcpu(i, vcpu, kvm)
417  if (kvm_apic_present(vcpu))
418  max_id = max(max_id, kvm_x2apic_id(vcpu->arch.apic));
419 
420  new = kvzalloc(sizeof(struct kvm_apic_map) +
421  sizeof(struct kvm_lapic *) * ((u64)max_id + 1),
422  GFP_KERNEL_ACCOUNT);
423 
424  if (!new)
425  goto out;
426 
427  new->max_apic_id = max_id;
428  new->logical_mode = KVM_APIC_MODE_SW_DISABLED;
429 
430  kvm_for_each_vcpu(i, vcpu, kvm) {
431  if (!kvm_apic_present(vcpu))
432  continue;
433 
434  r = kvm_recalculate_phys_map(new, vcpu, &xapic_id_mismatch);
435  if (r) {
436  kvfree(new);
437  new = NULL;
438  if (r == -E2BIG) {
439  cond_resched();
440  goto retry;
441  }
442 
443  goto out;
444  }
445 
446  kvm_recalculate_logical_map(new, vcpu);
447  }
448 out:
449  /*
450  * The optimized map is effectively KVM's internal version of APICv,
451  * and all unwanted aliasing that results in disabling the optimized
452  * map also applies to APICv.
453  */
454  if (!new)
455  kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED);
456  else
457  kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_PHYSICAL_ID_ALIASED);
458 
459  if (!new || new->logical_mode == KVM_APIC_MODE_MAP_DISABLED)
460  kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED);
461  else
462  kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_LOGICAL_ID_ALIASED);
463 
464  if (xapic_id_mismatch)
465  kvm_set_apicv_inhibit(kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
466  else
467  kvm_clear_apicv_inhibit(kvm, APICV_INHIBIT_REASON_APIC_ID_MODIFIED);
468 
469  old = rcu_dereference_protected(kvm->arch.apic_map,
470  lockdep_is_held(&kvm->arch.apic_map_lock));
471  rcu_assign_pointer(kvm->arch.apic_map, new);
472  /*
473  * Write kvm->arch.apic_map before clearing apic->apic_map_dirty.
474  * If another update has come in, leave it DIRTY.
475  */
476  atomic_cmpxchg_release(&kvm->arch.apic_map_dirty,
478  mutex_unlock(&kvm->arch.apic_map_lock);
479 
480  if (old)
481  call_rcu(&old->rcu, kvm_apic_map_free);
482 
484 }
#define irqchip_in_kernel(k)
Definition: arm_vgic.h:392
static int kvm_recalculate_phys_map(struct kvm_apic_map *new, struct kvm_vcpu *vcpu, bool *xapic_id_mismatch)
Definition: lapic.c:218
static void kvm_recalculate_logical_map(struct kvm_apic_map *new, struct kvm_vcpu *vcpu)
Definition: lapic.c:294
static void kvm_apic_map_free(struct rcu_head *rcu)
Definition: lapic.c:211
void kvm_make_scan_ioapic_request(struct kvm *kvm)
Definition: x86.c:10520
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_recalculate_logical_map()

static void kvm_recalculate_logical_map ( struct kvm_apic_map *  new,
struct kvm_vcpu *  vcpu 
)
static

Definition at line 294 of file lapic.c.

296 {
297  struct kvm_lapic *apic = vcpu->arch.apic;
298  enum kvm_apic_logical_mode logical_mode;
299  struct kvm_lapic **cluster;
300  u16 mask;
301  u32 ldr;
302 
303  if (new->logical_mode == KVM_APIC_MODE_MAP_DISABLED)
304  return;
305 
306  if (!kvm_apic_sw_enabled(apic))
307  return;
308 
309  ldr = kvm_lapic_get_reg(apic, APIC_LDR);
310  if (!ldr)
311  return;
312 
313  if (apic_x2apic_mode(apic)) {
314  logical_mode = KVM_APIC_MODE_X2APIC;
315  } else {
316  ldr = GET_APIC_LOGICAL_ID(ldr);
317  if (kvm_lapic_get_reg(apic, APIC_DFR) == APIC_DFR_FLAT)
318  logical_mode = KVM_APIC_MODE_XAPIC_FLAT;
319  else
320  logical_mode = KVM_APIC_MODE_XAPIC_CLUSTER;
321  }
322 
323  /*
324  * To optimize logical mode delivery, all software-enabled APICs must
325  * be configured for the same mode.
326  */
327  if (new->logical_mode == KVM_APIC_MODE_SW_DISABLED) {
328  new->logical_mode = logical_mode;
329  } else if (new->logical_mode != logical_mode) {
330  new->logical_mode = KVM_APIC_MODE_MAP_DISABLED;
331  return;
332  }
333 
334  /*
335  * In x2APIC mode, the LDR is read-only and derived directly from the
336  * x2APIC ID, thus is guaranteed to be addressable. KVM reuses
337  * kvm_apic_map.phys_map to optimize logical mode x2APIC interrupts by
338  * reversing the LDR calculation to get cluster of APICs, i.e. no
339  * additional work is required.
340  */
341  if (apic_x2apic_mode(apic)) {
342  WARN_ON_ONCE(ldr != kvm_apic_calc_x2apic_ldr(kvm_x2apic_id(apic)));
343  return;
344  }
345 
346  if (WARN_ON_ONCE(!kvm_apic_map_get_logical_dest(new, ldr,
347  &cluster, &mask))) {
348  new->logical_mode = KVM_APIC_MODE_MAP_DISABLED;
349  return;
350  }
351 
352  if (!mask)
353  return;
354 
355  ldr = ffs(mask) - 1;
356  if (!is_power_of_2(mask) || cluster[ldr])
357  new->logical_mode = KVM_APIC_MODE_MAP_DISABLED;
358  else
359  cluster[ldr] = apic;
360 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_recalculate_phys_map()

static int kvm_recalculate_phys_map ( struct kvm_apic_map *  new,
struct kvm_vcpu *  vcpu,
bool *  xapic_id_mismatch 
)
static

Definition at line 218 of file lapic.c.

221 {
222  struct kvm_lapic *apic = vcpu->arch.apic;
223  u32 x2apic_id = kvm_x2apic_id(apic);
224  u32 xapic_id = kvm_xapic_id(apic);
225  u32 physical_id;
226 
227  /*
228  * For simplicity, KVM always allocates enough space for all possible
229  * xAPIC IDs. Yell, but don't kill the VM, as KVM can continue on
230  * without the optimized map.
231  */
232  if (WARN_ON_ONCE(xapic_id > new->max_apic_id))
233  return -EINVAL;
234 
235  /*
236  * Bail if a vCPU was added and/or enabled its APIC between allocating
237  * the map and doing the actual calculations for the map. Note, KVM
238  * hardcodes the x2APIC ID to vcpu_id, i.e. there's no TOCTOU bug if
239  * the compiler decides to reload x2apic_id after this check.
240  */
241  if (x2apic_id > new->max_apic_id)
242  return -E2BIG;
243 
244  /*
245  * Deliberately truncate the vCPU ID when detecting a mismatched APIC
246  * ID to avoid false positives if the vCPU ID, i.e. x2APIC ID, is a
247  * 32-bit value. Any unwanted aliasing due to truncation results will
248  * be detected below.
249  */
250  if (!apic_x2apic_mode(apic) && xapic_id != (u8)vcpu->vcpu_id)
251  *xapic_id_mismatch = true;
252 
253  /*
254  * Apply KVM's hotplug hack if userspace has enable 32-bit APIC IDs.
255  * Allow sending events to vCPUs by their x2APIC ID even if the target
256  * vCPU is in legacy xAPIC mode, and silently ignore aliased xAPIC IDs
257  * (the x2APIC ID is truncated to 8 bits, causing IDs > 0xff to wrap
258  * and collide).
259  *
260  * Honor the architectural (and KVM's non-optimized) behavior if
261  * userspace has not enabled 32-bit x2APIC IDs. Each APIC is supposed
262  * to process messages independently. If multiple vCPUs have the same
263  * effective APIC ID, e.g. due to the x2APIC wrap or because the guest
264  * manually modified its xAPIC IDs, events targeting that ID are
265  * supposed to be recognized by all vCPUs with said ID.
266  */
267  if (vcpu->kvm->arch.x2apic_format) {
268  /* See also kvm_apic_match_physical_addr(). */
269  if (apic_x2apic_mode(apic) || x2apic_id > 0xff)
270  new->phys_map[x2apic_id] = apic;
271 
272  if (!apic_x2apic_mode(apic) && !new->phys_map[xapic_id])
273  new->phys_map[xapic_id] = apic;
274  } else {
275  /*
276  * Disable the optimized map if the physical APIC ID is already
277  * mapped, i.e. is aliased to multiple vCPUs. The optimized
278  * map requires a strict 1:1 mapping between IDs and vCPUs.
279  */
280  if (apic_x2apic_mode(apic))
281  physical_id = x2apic_id;
282  else
283  physical_id = xapic_id;
284 
285  if (new->phys_map[physical_id])
286  return -EINVAL;
287 
288  new->phys_map[physical_id] = apic;
289  }
290 
291  return 0;
292 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_set_lapic_tscdeadline_msr()

void kvm_set_lapic_tscdeadline_msr ( struct kvm_vcpu *  vcpu,
u64  data 
)

Definition at line 2504 of file lapic.c.

2505 {
2506  struct kvm_lapic *apic = vcpu->arch.apic;
2507 
2509  return;
2510 
2511  hrtimer_cancel(&apic->lapic_timer.timer);
2512  apic->lapic_timer.tscdeadline = data;
2513  start_apic_timer(apic);
2514 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_use_posted_timer_interrupt()

static bool kvm_use_posted_timer_interrupt ( struct kvm_vcpu *  vcpu)
static

Definition at line 161 of file lapic.c.

162 {
163  return kvm_can_post_timer_interrupt(vcpu) && vcpu->mode == IN_GUEST_MODE;
164 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_vector_to_index()

int kvm_vector_to_index ( u32  vector,
u32  dest_vcpus,
const unsigned long *  bitmap,
u32  bitmap_size 
)

Definition at line 1092 of file lapic.c.

1094 {
1095  u32 mod;
1096  int i, idx = -1;
1097 
1098  mod = vector % dest_vcpus;
1099 
1100  for (i = 0; i <= mod; i++) {
1101  idx = find_next_bit(bitmap, bitmap_size, idx + 1);
1102  BUG_ON(idx == bitmap_size);
1103  }
1104 
1105  return idx;
1106 }
Here is the caller graph for this function:

◆ kvm_wait_lapic_expire()

void kvm_wait_lapic_expire ( struct kvm_vcpu *  vcpu)

Definition at line 1869 of file lapic.c.

1870 {
1871  if (lapic_in_kernel(vcpu) &&
1872  vcpu->arch.apic->lapic_timer.expired_tscdeadline &&
1873  vcpu->arch.apic->lapic_timer.timer_advance_ns &&
1876 }
static bool lapic_timer_int_injected(struct kvm_vcpu *vcpu)
Definition: lapic.c:1777
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_x2apic_icr_write()

int kvm_x2apic_icr_write ( struct kvm_lapic apic,
u64  data 
)

Definition at line 3155 of file lapic.c.

3156 {
3157  data &= ~APIC_ICR_BUSY;
3158 
3159  kvm_apic_send_ipi(apic, (u32)data, (u32)(data >> 32));
3160  kvm_lapic_set_reg64(apic, APIC_ICR, data);
3161  trace_kvm_apic_write(APIC_ICR, data);
3162  return 0;
3163 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_x2apic_id()

static u32 kvm_x2apic_id ( struct kvm_lapic apic)
inlinestatic

Definition at line 143 of file lapic.c.

144 {
145  return apic->vcpu->vcpu_id;
146 }
Here is the caller graph for this function:

◆ kvm_x2apic_msr_read()

int kvm_x2apic_msr_read ( struct kvm_vcpu *  vcpu,
u32  msr,
u64 *  data 
)

Definition at line 3210 of file lapic.c.

3211 {
3212  struct kvm_lapic *apic = vcpu->arch.apic;
3213  u32 reg = (msr - APIC_BASE_MSR) << 4;
3214 
3215  if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
3216  return 1;
3217 
3218  return kvm_lapic_msr_read(apic, reg, data);
3219 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kvm_x2apic_msr_write()

int kvm_x2apic_msr_write ( struct kvm_vcpu *  vcpu,
u32  msr,
u64  data 
)

Definition at line 3199 of file lapic.c.

3200 {
3201  struct kvm_lapic *apic = vcpu->arch.apic;
3202  u32 reg = (msr - APIC_BASE_MSR) << 4;
3203 
3204  if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(apic))
3205  return 1;
3206 
3207  return kvm_lapic_msr_write(apic, reg, data);
3208 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ lapic_is_periodic()

static bool lapic_is_periodic ( struct kvm_lapic apic)
static

Definition at line 2747 of file lapic.c.

2748 {
2749  return apic_lvtt_period(apic);
2750 }
Here is the call graph for this function:

◆ lapic_timer_int_injected()

static bool lapic_timer_int_injected ( struct kvm_vcpu *  vcpu)
static

Definition at line 1777 of file lapic.c.

1778 {
1779  struct kvm_lapic *apic = vcpu->arch.apic;
1780  u32 reg = kvm_lapic_get_reg(apic, APIC_LVTT);
1781 
1782  if (kvm_apic_hw_enabled(apic)) {
1783  int vec = reg & APIC_VECTOR_MASK;
1784  void *bitmap = apic->regs + APIC_ISR;
1785 
1786  if (apic->apicv_active)
1787  bitmap = apic->regs + APIC_IRR;
1788 
1789  if (apic_test_vector(vec, bitmap))
1790  return true;
1791  }
1792  return false;
1793 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ limit_periodic_timer_frequency()

static void limit_periodic_timer_frequency ( struct kvm_lapic apic)
static

Definition at line 1721 of file lapic.c.

1722 {
1723  /*
1724  * Do not allow the guest to program periodic timers with small
1725  * interval, since the hrtimers are not throttled by the host
1726  * scheduler.
1727  */
1728  if (apic_lvtt_period(apic) && apic->lapic_timer.period) {
1729  s64 min_period = min_timer_period_us * 1000LL;
1730 
1731  if (apic->lapic_timer.period < min_period) {
1732  pr_info_ratelimited(
1733  "vcpu %i: requested %lld ns "
1734  "lapic timer period limited to %lld ns\n",
1735  apic->vcpu->vcpu_id,
1736  apic->lapic_timer.period, min_period);
1737  apic->lapic_timer.period = min_period;
1738  }
1739  }
1740 }
unsigned int min_timer_period_us
Definition: x86.c:154
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pv_eoi_enabled()

static bool pv_eoi_enabled ( struct kvm_vcpu *  vcpu)
inlinestatic

Definition at line 897 of file lapic.c.

898 {
899  return vcpu->arch.pv_eoi.msr_val & KVM_MSR_ENABLED;
900 }
Here is the caller graph for this function:

◆ pv_eoi_get_user()

static int pv_eoi_get_user ( struct kvm_vcpu *  vcpu,
u8 *  val 
)
static

Definition at line 890 of file lapic.c.

891 {
892 
893  return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, val,
894  sizeof(*val));
895 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pv_eoi_put_user()

static int pv_eoi_put_user ( struct kvm_vcpu *  vcpu,
u8  val 
)
static

Definition at line 883 of file lapic.c.

884 {
885 
886  return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.pv_eoi.data, &val,
887  sizeof(val));
888 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pv_eoi_set_pending()

static void pv_eoi_set_pending ( struct kvm_vcpu *  vcpu)
static

Definition at line 902 of file lapic.c.

903 {
904  if (pv_eoi_put_user(vcpu, KVM_PV_EOI_ENABLED) < 0)
905  return;
906 
907  __set_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
908 }
static int pv_eoi_put_user(struct kvm_vcpu *vcpu, u8 val)
Definition: lapic.c:883
Here is the call graph for this function:
Here is the caller graph for this function:

◆ pv_eoi_test_and_clr_pending()

static bool pv_eoi_test_and_clr_pending ( struct kvm_vcpu *  vcpu)
static

Definition at line 910 of file lapic.c.

911 {
912  u8 val;
913 
914  if (pv_eoi_get_user(vcpu, &val) < 0)
915  return false;
916 
917  val &= KVM_PV_EOI_ENABLED;
918 
919  if (val && pv_eoi_put_user(vcpu, KVM_PV_EOI_DISABLED) < 0)
920  return false;
921 
922  /*
923  * Clear pending bit in any case: it will be set again on vmentry.
924  * While this might not be ideal from performance point of view,
925  * this makes sure pv eoi is only enabled when we know it's safe.
926  */
927  __clear_bit(KVM_APIC_PV_EOI_PENDING, &vcpu->arch.apic_attention);
928 
929  return val;
930 }
static int pv_eoi_get_user(struct kvm_vcpu *vcpu, u8 *val)
Definition: lapic.c:890
Here is the call graph for this function:
Here is the caller graph for this function:

◆ report_tpr_access()

static void report_tpr_access ( struct kvm_lapic apic,
bool  write 
)
inlinestatic

Definition at line 1560 of file lapic.c.

1561 {
1562  if (apic->vcpu->arch.tpr_access_reporting)
1563  __report_tpr_access(apic, write);
1564 }
static void __report_tpr_access(struct kvm_lapic *apic, bool write)
Definition: lapic.c:1550
Here is the call graph for this function:
Here is the caller graph for this function:

◆ restart_apic_timer()

static void restart_apic_timer ( struct kvm_lapic apic)
static

Definition at line 2158 of file lapic.c.

2159 {
2160  preempt_disable();
2161 
2162  if (!apic_lvtt_period(apic) && atomic_read(&apic->lapic_timer.pending))
2163  goto out;
2164 
2165  if (!start_hv_timer(apic))
2166  start_sw_timer(apic);
2167 out:
2168  preempt_enable();
2169 }
static bool start_hv_timer(struct kvm_lapic *apic)
Definition: lapic.c:2099
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_target_expiration()

static bool set_target_expiration ( struct kvm_lapic apic,
u32  count_reg 
)
static

Definition at line 1992 of file lapic.c.

1993 {
1994  ktime_t now;
1995  u64 tscl = rdtsc();
1996  s64 deadline;
1997 
1998  now = ktime_get();
1999  apic->lapic_timer.period =
2000  tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
2001 
2002  if (!apic->lapic_timer.period) {
2003  apic->lapic_timer.tscdeadline = 0;
2004  return false;
2005  }
2006 
2008  deadline = apic->lapic_timer.period;
2009 
2010  if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic)) {
2011  if (unlikely(count_reg != APIC_TMICT)) {
2012  deadline = tmict_to_ns(apic,
2013  kvm_lapic_get_reg(apic, count_reg));
2014  if (unlikely(deadline <= 0)) {
2015  if (apic_lvtt_period(apic))
2016  deadline = apic->lapic_timer.period;
2017  else
2018  deadline = 0;
2019  }
2020  else if (unlikely(deadline > apic->lapic_timer.period)) {
2021  pr_info_ratelimited(
2022  "vcpu %i: requested lapic timer restore with "
2023  "starting count register %#x=%u (%lld ns) > initial count (%lld ns). "
2024  "Using initial count to start timer.\n",
2025  apic->vcpu->vcpu_id,
2026  count_reg,
2027  kvm_lapic_get_reg(apic, count_reg),
2028  deadline, apic->lapic_timer.period);
2029  kvm_lapic_set_reg(apic, count_reg, 0);
2030  deadline = apic->lapic_timer.period;
2031  }
2032  }
2033  }
2034 
2035  apic->lapic_timer.tscdeadline = kvm_read_l1_tsc(apic->vcpu, tscl) +
2036  nsec_to_cycles(apic->vcpu, deadline);
2037  apic->lapic_timer.target_expiration = ktime_add_ns(now, deadline);
2038 
2039  return true;
2040 }
static u64 tmict_to_ns(struct kvm_lapic *apic, u32 tmict)
Definition: lapic.c:1963
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_apic_timer()

static void start_apic_timer ( struct kvm_lapic apic)
static

Definition at line 2227 of file lapic.c.

2228 {
2229  __start_apic_timer(apic, APIC_TMICT);
2230 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_hv_timer()

static bool start_hv_timer ( struct kvm_lapic apic)
static

Definition at line 2099 of file lapic.c.

2100 {
2101  struct kvm_timer *ktimer = &apic->lapic_timer;
2102  struct kvm_vcpu *vcpu = apic->vcpu;
2103  bool expired;
2104 
2105  WARN_ON(preemptible());
2106  if (!kvm_can_use_hv_timer(vcpu))
2107  return false;
2108 
2109  if (!ktimer->tscdeadline)
2110  return false;
2111 
2112  if (static_call(kvm_x86_set_hv_timer)(vcpu, ktimer->tscdeadline, &expired))
2113  return false;
2114 
2115  ktimer->hv_timer_in_use = true;
2116  hrtimer_cancel(&ktimer->timer);
2117 
2118  /*
2119  * To simplify handling the periodic timer, leave the hv timer running
2120  * even if the deadline timer has expired, i.e. rely on the resulting
2121  * VM-Exit to recompute the periodic timer's target expiration.
2122  */
2123  if (!apic_lvtt_period(apic)) {
2124  /*
2125  * Cancel the hv timer if the sw timer fired while the hv timer
2126  * was being programmed, or if the hv timer itself expired.
2127  */
2128  if (atomic_read(&ktimer->pending)) {
2129  cancel_hv_timer(apic);
2130  } else if (expired) {
2131  apic_timer_expired(apic, false);
2132  cancel_hv_timer(apic);
2133  }
2134  }
2135 
2136  trace_kvm_hv_timer_state(vcpu->vcpu_id, ktimer->hv_timer_in_use);
2137 
2138  return true;
2139 }
bool kvm_can_use_hv_timer(struct kvm_vcpu *vcpu)
Definition: lapic.c:154
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_sw_period()

static void start_sw_period ( struct kvm_lapic apic)
static

Definition at line 2063 of file lapic.c.

2064 {
2065  if (!apic->lapic_timer.period)
2066  return;
2067 
2068  if (ktime_after(ktime_get(),
2069  apic->lapic_timer.target_expiration)) {
2070  apic_timer_expired(apic, false);
2071 
2072  if (apic_lvtt_oneshot(apic))
2073  return;
2074 
2076  }
2077 
2078  hrtimer_start(&apic->lapic_timer.timer,
2080  HRTIMER_MODE_ABS_HARD);
2081 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_sw_timer()

static void start_sw_timer ( struct kvm_lapic apic)
static

Definition at line 2141 of file lapic.c.

2142 {
2143  struct kvm_timer *ktimer = &apic->lapic_timer;
2144 
2145  WARN_ON(preemptible());
2146  if (apic->lapic_timer.hv_timer_in_use)
2147  cancel_hv_timer(apic);
2148  if (!apic_lvtt_period(apic) && atomic_read(&ktimer->pending))
2149  return;
2150 
2151  if (apic_lvtt_period(apic) || apic_lvtt_oneshot(apic))
2152  start_sw_period(apic);
2153  else if (apic_lvtt_tscdeadline(apic))
2154  start_sw_tscdeadline(apic);
2155  trace_kvm_hv_timer_state(apic->vcpu->vcpu_id, false);
2156 }
static void start_sw_period(struct kvm_lapic *apic)
Definition: lapic.c:2063
static void start_sw_tscdeadline(struct kvm_lapic *apic)
Definition: lapic.c:1930
Here is the call graph for this function:
Here is the caller graph for this function:

◆ start_sw_tscdeadline()

static void start_sw_tscdeadline ( struct kvm_lapic apic)
static

Definition at line 1930 of file lapic.c.

1931 {
1932  struct kvm_timer *ktimer = &apic->lapic_timer;
1933  u64 guest_tsc, tscdeadline = ktimer->tscdeadline;
1934  u64 ns = 0;
1935  ktime_t expire;
1936  struct kvm_vcpu *vcpu = apic->vcpu;
1937  unsigned long this_tsc_khz = vcpu->arch.virtual_tsc_khz;
1938  unsigned long flags;
1939  ktime_t now;
1940 
1941  if (unlikely(!tscdeadline || !this_tsc_khz))
1942  return;
1943 
1944  local_irq_save(flags);
1945 
1946  now = ktime_get();
1947  guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1948 
1949  ns = (tscdeadline - guest_tsc) * 1000000ULL;
1950  do_div(ns, this_tsc_khz);
1951 
1952  if (likely(tscdeadline > guest_tsc) &&
1953  likely(ns > apic->lapic_timer.timer_advance_ns)) {
1954  expire = ktime_add_ns(now, ns);
1955  expire = ktime_sub_ns(expire, ktimer->timer_advance_ns);
1956  hrtimer_start(&ktimer->timer, expire, HRTIMER_MODE_ABS_HARD);
1957  } else
1958  apic_timer_expired(apic, false);
1959 
1960  local_irq_restore(flags);
1961 }
uint32_t flags
Definition: xen.c:1
Here is the call graph for this function:
Here is the caller graph for this function:

◆ tmict_to_ns()

static u64 tmict_to_ns ( struct kvm_lapic apic,
u32  tmict 
)
inlinestatic

Definition at line 1963 of file lapic.c.

1964 {
1965  return (u64)tmict * APIC_BUS_CYCLE_NS * (u64)apic->divide_count;
1966 }
Here is the caller graph for this function:

◆ to_lapic()

static struct kvm_lapic* to_lapic ( struct kvm_io_device dev)
inlinestatic

Definition at line 1598 of file lapic.c.

1599 {
1600  return container_of(dev, struct kvm_lapic, dev);
1601 }
Here is the caller graph for this function:

◆ update_divide_count()

static void update_divide_count ( struct kvm_lapic apic)
static

Definition at line 1711 of file lapic.c.

1712 {
1713  u32 tmp1, tmp2, tdcr;
1714 
1715  tdcr = kvm_lapic_get_reg(apic, APIC_TDCR);
1716  tmp1 = tdcr & 0xf;
1717  tmp2 = ((tmp1 & 0x3) | ((tmp1 & 0x8) >> 1)) + 1;
1718  apic->divide_count = 0x1 << (tmp2 & 0x7);
1719 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_target_expiration()

static void update_target_expiration ( struct kvm_lapic apic,
uint32_t  old_divisor 
)
static

Definition at line 1968 of file lapic.c.

1969 {
1970  ktime_t now, remaining;
1971  u64 ns_remaining_old, ns_remaining_new;
1972 
1973  apic->lapic_timer.period =
1974  tmict_to_ns(apic, kvm_lapic_get_reg(apic, APIC_TMICT));
1976 
1977  now = ktime_get();
1978  remaining = ktime_sub(apic->lapic_timer.target_expiration, now);
1979  if (ktime_to_ns(remaining) < 0)
1980  remaining = 0;
1981 
1982  ns_remaining_old = ktime_to_ns(remaining);
1983  ns_remaining_new = mul_u64_u32_div(ns_remaining_old,
1984  apic->divide_count, old_divisor);
1985 
1986  apic->lapic_timer.tscdeadline +=
1987  nsec_to_cycles(apic->vcpu, ns_remaining_new) -
1988  nsec_to_cycles(apic->vcpu, ns_remaining_old);
1989  apic->lapic_timer.target_expiration = ktime_add_ns(now, ns_remaining_new);
1990 }
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ __read_mostly

bool lapic_timer_advance_dynamic __read_mostly
static

Definition at line 62 of file lapic.c.

◆ apic_lvt_mask

const unsigned int apic_lvt_mask[KVM_APIC_MAX_NR_LVT_ENTRIES]
static
Initial value:
= {
[LVT_THERMAL_MONITOR] = LVT_MASK | APIC_MODE_MASK,
[LVT_PERFORMANCE_COUNTER] = LVT_MASK | APIC_MODE_MASK,
[LVT_CMCI] = LVT_MASK | APIC_MODE_MASK
}
#define LINT_MASK
Definition: lapic.c:139
@ LVT_LINT0
Definition: lapic.h:36
@ LVT_PERFORMANCE_COUNTER
Definition: lapic.h:35
@ LVT_ERROR
Definition: lapic.h:38
@ LVT_TIMER
Definition: lapic.h:33
@ LVT_LINT1
Definition: lapic.h:37
@ LVT_THERMAL_MONITOR
Definition: lapic.h:34

Definition at line 615 of file lapic.c.

◆ apic_mmio_ops

const struct kvm_io_device_ops apic_mmio_ops
static
Initial value:
= {
.read = apic_mmio_read,
.write = apic_mmio_write,
}
static int apic_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t address, int len, void *data)
Definition: lapic.c:1688
static int apic_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *this, gpa_t address, int len, const void *data)
Definition: lapic.c:2406

Definition at line 2782 of file lapic.c.