36 #include <linux/module.h>
37 #include <linux/mman.h>
44 #include <linux/slab.h>
45 #include <linux/perf_event.h>
47 #include <linux/hash.h>
48 #include <linux/pci.h>
51 #define CREATE_TRACE_POINTS
63 #include <asm/div64.h>
65 #define MAX_IO_MSRS 256
66 #define KVM_MAX_MCE_BANKS 32
67 #define KVM_MCE_CAP_SUPPORTED (MCG_CTL_P | MCG_SER_P)
69 #define emul_to_vcpu(ctxt) \
70 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
83 #define VM_STAT(x) offsetof(struct kvm, stat.x), KVM_STAT_VM
84 #define VCPU_STAT(x) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU
87 static void process_nmi(
struct kvm_vcpu *vcpu);
92 static bool ignore_msrs = 0;
101 static u32 tsc_tolerance_ppm = 250;
104 #define KVM_NR_SHARED_MSRS 16
131 {
"signal_exits",
VCPU_STAT(signal_exits) },
132 {
"irq_window",
VCPU_STAT(irq_window_exits) },
133 {
"nmi_window",
VCPU_STAT(nmi_window_exits) },
135 {
"halt_wakeup",
VCPU_STAT(halt_wakeup) },
137 {
"request_irq",
VCPU_STAT(request_irq_exits) },
139 {
"host_state_reload",
VCPU_STAT(host_state_reload) },
140 {
"efer_reload",
VCPU_STAT(efer_reload) },
142 {
"insn_emulation",
VCPU_STAT(insn_emulation) },
143 {
"insn_emulation_fail",
VCPU_STAT(insn_emulation_fail) },
144 {
"irq_injections",
VCPU_STAT(irq_injections) },
145 {
"nmi_injections",
VCPU_STAT(nmi_injections) },
146 {
"mmu_shadow_zapped",
VM_STAT(mmu_shadow_zapped) },
147 {
"mmu_pte_write",
VM_STAT(mmu_pte_write) },
148 {
"mmu_pte_updated",
VM_STAT(mmu_pte_updated) },
149 {
"mmu_pde_zapped",
VM_STAT(mmu_pde_zapped) },
150 {
"mmu_flooded",
VM_STAT(mmu_flooded) },
151 {
"mmu_recycled",
VM_STAT(mmu_recycled) },
152 {
"mmu_cache_miss",
VM_STAT(mmu_cache_miss) },
153 {
"mmu_unsync",
VM_STAT(mmu_unsync) },
154 {
"remote_tlb_flush",
VM_STAT(remote_tlb_flush) },
155 {
"largepages",
VM_STAT(lpages) },
163 static inline void kvm_async_pf_hash_reset(
struct kvm_vcpu *vcpu)
167 vcpu->
arch.apf.gfns[i] = ~0;
175 struct kvm_shared_msr_values *
values;
177 for (slot = 0; slot < shared_msrs_global.nr; ++
slot) {
179 if (values->host != values->curr) {
180 wrmsrl(shared_msrs_global.msrs[slot], values->host);
181 values->curr = values->host;
188 static void shared_msr_update(
unsigned slot,
u32 msr)
196 if (slot >= shared_msrs_global.nr) {
200 rdmsrl_safe(msr, &value);
207 if (slot >= shared_msrs_global.nr)
208 shared_msrs_global.nr = slot + 1;
209 shared_msrs_global.msrs[
slot] = msr;
215 static void kvm_shared_msr_cpu_online(
void)
219 for (i = 0; i < shared_msrs_global.nr; ++
i)
220 shared_msr_update(i, shared_msrs_global.msrs[i]);
227 if (((value ^ smsr->
values[slot].curr) & mask) == 0)
230 wrmsrl(shared_msrs_global.msrs[slot], value);
232 smsr->
urn.on_user_return = kvm_on_user_return;
239 static void drop_user_return_notifiers(
void *ignore)
244 kvm_on_user_return(&smsr->
urn);
249 return vcpu->
arch.apic_base;
260 #define EXCPT_BENIGN 0
261 #define EXCPT_CONTRIBUTORY 1
264 static int exception_class(
int vector)
281 static void kvm_multiple_exception(
struct kvm_vcpu *vcpu,
290 if (!vcpu->
arch.exception.pending) {
292 vcpu->
arch.exception.pending =
true;
293 vcpu->
arch.exception.has_error_code = has_error;
294 vcpu->
arch.exception.nr =
nr;
296 vcpu->
arch.exception.reinject = reinject;
301 prev_nr = vcpu->
arch.exception.nr;
307 class1 = exception_class(prev_nr);
308 class2 = exception_class(nr);
312 vcpu->
arch.exception.pending =
true;
313 vcpu->
arch.exception.has_error_code =
true;
315 vcpu->
arch.exception.error_code = 0;
325 kvm_multiple_exception(vcpu, nr,
false, 0,
false);
331 kvm_multiple_exception(vcpu, nr,
false, 0,
true);
338 kvm_inject_gp(vcpu, 0);
346 ++vcpu->
stat.pf_guest;
355 vcpu->
arch.nested_mmu.inject_page_fault(vcpu, fault);
357 vcpu->
arch.mmu.inject_page_fault(vcpu, fault);
369 kvm_multiple_exception(vcpu, nr,
true, error_code,
false);
375 kvm_multiple_exception(vcpu, nr,
true, error_code,
true);
385 if (kvm_x86_ops->
get_cpl(vcpu) <= required_cpl)
404 ngpa = gfn_to_gpa(ngfn);
409 real_gfn = gpa_to_gfn(real_gfn);
419 data, offset, len, access);
434 offset *
sizeof(
u64),
sizeof(pdpte),
441 if (is_present_gpte(pdpte[i]) &&
442 (pdpte[
i] & vcpu->
arch.mmu.rsvd_bits_mask[0][2])) {
451 (
unsigned long *)&vcpu->
arch.regs_avail);
453 (
unsigned long *)&vcpu->
arch.regs_dirty);
460 static bool pdptrs_changed(
struct kvm_vcpu *vcpu)
468 if (is_long_mode(vcpu) || !is_pae(vcpu))
472 (
unsigned long *)&vcpu->
arch.regs_avail))
476 offset = (kvm_read_cr3(vcpu) & ~31
u) & (
PAGE_SIZE - 1);
481 changed =
memcmp(pdpte, vcpu->
arch.walk_mmu->pdptrs,
sizeof(pdpte)) != 0;
489 unsigned long old_cr0 = kvm_read_cr0(vcpu);
496 if (cr0 & 0xffffffff00000000UL)
508 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
525 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu,
X86_CR4_PCIDE))
528 kvm_x86_ops->
set_cr0(vcpu, cr0);
530 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
532 kvm_async_pf_hash_reset(vcpu);
535 if ((cr0 ^ old_cr0) & update_bits)
543 (
void)
kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
555 if (kvm_x86_ops->
get_cpl(vcpu) != 0)
561 if (xcr0 & ~host_xcr0)
563 vcpu->
arch.xcr0 = xcr0;
571 kvm_inject_gp(vcpu, 0);
580 unsigned long old_cr4 = kvm_read_cr4(vcpu);
589 if (!guest_cpuid_has_smep(vcpu) && (cr4 &
X86_CR4_SMEP))
595 if (is_long_mode(vcpu)) {
599 && ((cr4 ^ old_cr4) & pdptr_bits)
605 if (!guest_cpuid_has_pcid(vcpu))
613 if (kvm_x86_ops->
set_cr4(vcpu, cr4))
616 if (((cr4 ^ old_cr4) & pdptr_bits) ||
620 if ((cr4 ^ old_cr4) & X86_CR4_OSXSAVE)
629 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
635 if (is_long_mode(vcpu)) {
646 if (is_paging(vcpu) &&
669 vcpu->
arch.mmu.new_cr3(vcpu);
678 if (irqchip_in_kernel(vcpu->
kvm))
688 if (irqchip_in_kernel(vcpu->
kvm))
691 return vcpu->
arch.cr8;
695 static void kvm_update_dr7(
struct kvm_vcpu *vcpu)
700 dr7 = vcpu->
arch.guest_debug_dr7;
702 dr7 = vcpu->
arch.dr7;
703 kvm_x86_ops->
set_dr7(vcpu, dr7);
707 static int __kvm_set_dr(
struct kvm_vcpu *vcpu,
int dr,
unsigned long val)
720 if (val & 0xffffffff00000000ULL)
729 if (val & 0xffffffff00000000ULL)
732 kvm_update_dr7(vcpu);
743 res = __kvm_set_dr(vcpu, dr, val);
747 kvm_inject_gp(vcpu, 0);
753 static int _kvm_get_dr(
struct kvm_vcpu *vcpu,
int dr,
unsigned long *val)
757 *val = vcpu->
arch.db[dr];
764 *val = vcpu->
arch.dr6;
771 *val = vcpu->
arch.dr7;
780 if (_kvm_get_dr(vcpu, dr, val)) {
812 #define KVM_SAVE_MSRS_BEGIN 10
813 static u32 msrs_to_save[] = {
827 static unsigned num_msrs_to_save;
829 static const u32 emulated_msrs[] = {
838 u64 old_efer = vcpu->
arch.efer;
840 if (efer & efer_reserved_bits)
871 if ((efer ^ old_efer) &
EFER_NX)
879 efer_reserved_bits &= ~mask;
891 return kvm_x86_ops->
set_msr(vcpu, msr_index, data);
902 static void kvm_write_wall_clock(
struct kvm *
kvm,
gpa_t wall_clock)
931 if (kvm->
arch.kvmclock_offset) {
933 boot = timespec_sub(boot, ts);
935 wc.sec = boot.tv_sec;
936 wc.nsec = boot.tv_nsec;
952 :
"=a" (quotient),
"=d" (remainder)
953 :
"0" (0),
"1" (dividend),
"r" (divisor) );
958 s8 *pshift,
u32 *pmultiplier)
965 tps64 = base_khz * 1000
LL;
966 scaled64 = scaled_khz * 1000
LL;
967 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
973 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
974 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
982 *pmultiplier = div_frac(scaled64, tps32);
984 pr_debug(
"%s: base_khz %u => %u, shift %d, mul %u\n",
985 __func__, base_khz, scaled_khz, shift, *pmultiplier);
988 static inline u64 get_kernel_ns(
void)
995 return timespec_to_ns(&
ts);
1003 return pvclock_scale_delta(nsec, vcpu->
arch.virtual_tsc_mult,
1004 vcpu->
arch.virtual_tsc_shift);
1007 static u32 adjust_tsc_khz(
u32 khz,
s32 ppm)
1009 u64 v = (
u64)khz * (1000000 + ppm);
1014 static void kvm_set_tsc_khz(
struct kvm_vcpu *vcpu,
u32 this_tsc_khz)
1016 u32 thresh_lo, thresh_hi;
1017 int use_scaling = 0;
1021 &vcpu->
arch.virtual_tsc_shift,
1022 &vcpu->
arch.virtual_tsc_mult);
1023 vcpu->
arch.virtual_tsc_khz = this_tsc_khz;
1031 thresh_lo = adjust_tsc_khz(
tsc_khz, -tsc_tolerance_ppm);
1032 thresh_hi = adjust_tsc_khz(
tsc_khz, tsc_tolerance_ppm);
1033 if (this_tsc_khz < thresh_lo || this_tsc_khz > thresh_hi) {
1034 pr_debug(
"kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", this_tsc_khz, thresh_lo, thresh_hi);
1037 kvm_x86_ops->
set_tsc_khz(vcpu, this_tsc_khz, use_scaling);
1040 static u64 compute_guest_tsc(
struct kvm_vcpu *vcpu,
s64 kernel_ns)
1042 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->
arch.this_tsc_nsec,
1043 vcpu->
arch.virtual_tsc_mult,
1044 vcpu->
arch.virtual_tsc_shift);
1045 tsc += vcpu->
arch.this_tsc_write;
1051 struct kvm *kvm = vcpu->
kvm;
1053 unsigned long flags;
1058 ns = get_kernel_ns();
1059 elapsed = ns - kvm->
arch.last_tsc_nsec;
1062 usdiff = data - kvm->
arch.last_tsc_write;
1063 #ifdef CONFIG_X86_64
1064 usdiff = (usdiff * 1000) / vcpu->
arch.virtual_tsc_khz;
1067 asm(
"idivl %2; xor %%edx, %%edx"
1069 :
"A"(usdiff * 1000),
"rm"(vcpu->
arch.virtual_tsc_khz));
1087 vcpu->
arch.virtual_tsc_khz == kvm->
arch.last_tsc_khz) {
1089 offset = kvm->
arch.cur_tsc_offset;
1090 pr_debug(
"kvm: matched tsc offset for %llu\n", data);
1092 u64 delta = nsec_to_cycles(vcpu, elapsed);
1095 pr_debug(
"kvm: adjusted tsc offset by %llu\n", delta);
1107 kvm->
arch.cur_tsc_generation++;
1108 kvm->
arch.cur_tsc_nsec =
ns;
1111 pr_debug(
"kvm: new tsc generation %u, clock %llu\n",
1112 kvm->
arch.cur_tsc_generation, data);
1119 kvm->
arch.last_tsc_nsec =
ns;
1121 kvm->
arch.last_tsc_khz = vcpu->
arch.virtual_tsc_khz;
1124 vcpu->
arch.hv_clock.tsc_timestamp = 0;
1128 vcpu->
arch.this_tsc_generation = kvm->
arch.cur_tsc_generation;
1129 vcpu->
arch.this_tsc_nsec = kvm->
arch.cur_tsc_nsec;
1130 vcpu->
arch.this_tsc_write = kvm->
arch.cur_tsc_write;
1138 static int kvm_guest_time_update(
struct kvm_vcpu *v)
1140 unsigned long flags;
1143 unsigned long this_tsc_khz;
1144 s64 kernel_ns, max_kernel_ns;
1151 kernel_ns = get_kernel_ns();
1170 u64 tsc = compute_guest_tsc(v, kernel_ns);
1171 if (tsc > tsc_timestamp) {
1172 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
1173 tsc_timestamp =
tsc;
1204 if (vcpu->
hv_clock.tsc_timestamp) {
1207 max_kernel_ns = pvclock_scale_delta(max_kernel_ns,
1216 &vcpu->
hv_clock.tsc_to_system_mul);
1220 if (max_kernel_ns > kernel_ns)
1221 kernel_ns = max_kernel_ns;
1225 vcpu->
hv_clock.system_time = kernel_ns + v->
kvm->arch.kvmclock_offset;
1235 vcpu->
hv_clock.flags = pvclock_flags;
1255 static bool msr_mtrr_valid(
unsigned msr)
1279 static bool valid_pat_type(
unsigned t)
1281 return t < 8 && (1 <<
t) & 0xf3;
1284 static bool valid_mtrr_type(
unsigned t)
1286 return t < 8 && (1 <<
t) & 0x73;
1293 if (!msr_mtrr_valid(msr))
1297 for (i = 0; i < 8; i++)
1298 if (!valid_pat_type((data >> (i * 8)) & 0xff))
1304 return valid_mtrr_type(data & 0xff);
1306 for (i = 0; i < 8 ; i++)
1307 if (!valid_mtrr_type((data >> (i * 8)) & 0xff))
1313 return valid_mtrr_type(data & 0xff);
1316 static int set_msr_mtrr(
struct kvm_vcpu *vcpu,
u32 msr,
u64 data)
1318 u64 *
p = (
u64 *)&vcpu->
arch.mtrr_state.fixed_ranges;
1320 if (!mtrr_valid(vcpu, msr, data))
1324 vcpu->
arch.mtrr_state.def_type =
data;
1325 vcpu->
arch.mtrr_state.enabled = (data & 0xc00) >> 10;
1335 int idx, is_mtrr_mask;
1338 idx = (msr - 0x200) / 2;
1339 is_mtrr_mask = msr - 0x200 - 2 *
idx;
1342 (
u64 *)&vcpu->
arch.mtrr_state.var_ranges[idx].base_lo;
1345 (
u64 *)&vcpu->
arch.mtrr_state.var_ranges[
idx].mask_lo;
1356 unsigned bank_num = mcg_cap & 0xff;
1365 if (data != 0 && data != ~(
u64)0)
1378 if ((offset & 0x3) == 0 &&
1379 data != 0 && (data | (1 << 10)) != ~(
u64)0)
1389 static int xen_hvm_config(
struct kvm_vcpu *vcpu,
u64 data)
1391 struct kvm *kvm = vcpu->
kvm;
1392 int lm = is_long_mode(vcpu);
1393 u8 *blob_addr = lm ? (
u8 *)(
long)kvm->
arch.xen_hvm_config.blob_addr_64
1394 : (
u8 *)(
long)kvm->
arch.xen_hvm_config.blob_addr_32;
1395 u8 blob_size = lm ? kvm->
arch.xen_hvm_config.blob_size_64
1396 : kvm->
arch.xen_hvm_config.blob_size_32;
1398 u64 page_addr = data & PAGE_MASK;
1403 if (page_num >= blob_size)
1420 static bool kvm_hv_hypercall_enabled(
struct kvm *kvm)
1425 static bool kvm_hv_msr_partition_wide(
u32 msr)
1438 static int set_msr_hyperv_pw(
struct kvm_vcpu *vcpu,
u32 msr,
u64 data)
1440 struct kvm *kvm = vcpu->
kvm;
1446 if (!kvm->
arch.hv_guest_os_id)
1455 if (!kvm->
arch.hv_guest_os_id)
1463 if (kvm_is_error_hva(addr))
1466 ((
unsigned char *)instructions)[3] = 0xc3;
1473 vcpu_unimpl(vcpu,
"HYPER-V unimplemented wrmsr: 0x%x "
1474 "data 0x%llx\n", msr, data);
1480 static int set_msr_hyperv(
struct kvm_vcpu *vcpu,
u32 msr,
u64 data)
1492 if (kvm_is_error_hva(addr))
1506 vcpu_unimpl(vcpu,
"HYPER-V unimplemented wrmsr: 0x%x "
1507 "data 0x%llx\n", msr, data);
1514 static int kvm_pv_enable_async_pf(
struct kvm_vcpu *vcpu,
u64 data)
1516 gpa_t gpa = data & ~0x3f;
1526 kvm_async_pf_hash_reset(vcpu);
1538 static void kvmclock_reset(
struct kvm_vcpu *vcpu)
1540 if (vcpu->
arch.time_page) {
1546 static void accumulate_steal_time(
struct kvm_vcpu *vcpu)
1553 delta =
current->sched_info.run_delay - vcpu->
arch.st.last_steal;
1554 vcpu->
arch.st.last_steal =
current->sched_info.run_delay;
1558 static void record_steal_time(
struct kvm_vcpu *vcpu)
1567 vcpu->
arch.st.steal.steal += vcpu->
arch.st.accum_steal;
1568 vcpu->
arch.st.steal.version += 2;
1569 vcpu->
arch.st.accum_steal = 0;
1581 return set_efer(vcpu, data);
1584 data &= ~(
u64)0x100;
1587 vcpu_unimpl(vcpu,
"unimplemented HWCR wrmsr: 0x%llx\n",
1594 vcpu_unimpl(vcpu,
"unimplemented MMIO_CONF_BASE wrmsr: "
1610 vcpu_unimpl(vcpu,
"%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
1618 case 0x200 ... 0x2ff:
1619 return set_msr_mtrr(vcpu, msr, data);
1629 vcpu->
arch.ia32_misc_enable_msr =
data;
1633 vcpu->
kvm->arch.wall_clock =
data;
1634 kvm_write_wall_clock(vcpu->
kvm, data);
1638 kvmclock_reset(vcpu);
1650 vcpu->
arch.time_page =
1653 if (is_error_page(vcpu->
arch.time_page))
1659 if (kvm_pv_enable_async_pf(vcpu, data))
1679 vcpu->
arch.st.last_steal =
current->sched_info.run_delay;
1682 accumulate_steal_time(vcpu);
1696 return set_msr_mce(vcpu, msr, data);
1711 "0x%x data 0x%llx\n", msr, data);
1721 "0x%x data 0x%llx\n", msr, data);
1731 if (pr || data != 0)
1733 "0x%x data 0x%llx\n", msr, data);
1746 if (kvm_hv_msr_partition_wide(msr)) {
1749 r = set_msr_hyperv_pw(vcpu, msr, data);
1753 return set_msr_hyperv(vcpu, msr, data);
1759 vcpu_unimpl(vcpu,
"ignored wrmsr: 0x%x data %llx\n", msr, data);
1762 if (!guest_cpuid_has_osvw(vcpu))
1767 if (!guest_cpuid_has_osvw(vcpu))
1772 if (msr && (msr == vcpu->
kvm->arch.xen_hvm_config.msr))
1773 return xen_hvm_config(vcpu, data);
1777 vcpu_unimpl(vcpu,
"unhandled wrmsr: 0x%x data %llx\n",
1781 vcpu_unimpl(vcpu,
"ignored wrmsr: 0x%x data %llx\n",
1798 return kvm_x86_ops->
get_msr(vcpu, msr_index, pdata);
1803 u64 *p = (
u64 *)&vcpu->
arch.mtrr_state.fixed_ranges;
1805 if (!msr_mtrr_valid(msr))
1809 *pdata = vcpu->
arch.mtrr_state.def_type +
1810 (vcpu->
arch.mtrr_state.enabled << 10);
1818 *pdata = vcpu->
arch.pat;
1820 int idx, is_mtrr_mask;
1823 idx = (msr - 0x200) / 2;
1824 is_mtrr_mask = msr - 0x200 - 2 *
idx;
1827 (
u64 *)&vcpu->
arch.mtrr_state.var_ranges[idx].base_lo;
1830 (
u64 *)&vcpu->
arch.mtrr_state.var_ranges[
idx].mask_lo;
1837 static int get_msr_mce(
struct kvm_vcpu *vcpu,
u32 msr,
u64 *pdata)
1840 u64 mcg_cap = vcpu->
arch.mcg_cap;
1841 unsigned bank_num = mcg_cap & 0xff;
1849 data = vcpu->
arch.mcg_cap;
1852 if (!(mcg_cap & MCG_CTL_P))
1854 data = vcpu->
arch.mcg_ctl;
1857 data = vcpu->
arch.mcg_status;
1860 if (msr >= MSR_IA32_MC0_CTL &&
1861 msr < MSR_IA32_MC0_CTL + 4 * bank_num) {
1872 static int get_msr_hyperv_pw(
struct kvm_vcpu *vcpu,
u32 msr,
u64 *pdata)
1875 struct kvm *kvm = vcpu->
kvm;
1879 data = kvm->
arch.hv_guest_os_id;
1882 data = kvm->
arch.hv_hypercall;
1885 vcpu_unimpl(vcpu,
"Hyper-V unhandled rdmsr: 0x%x\n", msr);
1893 static int get_msr_hyperv(
struct kvm_vcpu *vcpu,
u32 msr,
u64 *pdata)
1913 data = vcpu->
arch.hv_vapic;
1954 data = 0x100000000ULL;
1959 case 0x200 ... 0x2ff:
1960 return get_msr_mtrr(vcpu, msr, pdata);
1988 data = vcpu->arch.ia32_misc_enable_msr;
1997 data = vcpu->arch.efer;
2001 data = vcpu->kvm->arch.wall_clock;
2005 data = vcpu->arch.time;
2008 data = vcpu->arch.apf.msr_val;
2011 data = vcpu->arch.st.msr_val;
2014 data = vcpu->arch.pv_eoi.msr_val;
2022 return get_msr_mce(vcpu, msr, pdata);
2036 if (kvm_hv_msr_partition_wide(msr)) {
2039 r = get_msr_hyperv_pw(vcpu, msr, pdata);
2043 return get_msr_hyperv(vcpu, msr, pdata);
2059 if (!guest_cpuid_has_osvw(vcpu))
2061 data = vcpu->arch.osvw.length;
2064 if (!guest_cpuid_has_osvw(vcpu))
2066 data = vcpu->arch.osvw.status;
2072 vcpu_unimpl(vcpu,
"unhandled rdmsr: 0x%x\n", msr);
2092 int (*do_msr)(
struct kvm_vcpu *vcpu,
2097 idx = srcu_read_lock(&vcpu->
kvm->srcu);
2098 for (i = 0; i < msrs->
nmsrs; ++
i)
2099 if (do_msr(vcpu, entries[i].index, &entries[i].data))
2101 srcu_read_unlock(&vcpu->
kvm->srcu, idx);
2111 static int msr_io(
struct kvm_vcpu *vcpu,
struct kvm_msrs __user *user_msrs,
2112 int (*do_msr)(
struct kvm_vcpu *vcpu,
2113 unsigned index,
u64 *data),
2131 if (IS_ERR(entries)) {
2132 r = PTR_ERR(entries);
2136 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
2141 if (writeback &&
copy_to_user(user_msrs->entries, entries, size))
2167 case KVM_CAP_USER_NMI:
2168 case KVM_CAP_REINJECT_CONTROL:
2174 case KVM_CAP_PIT_STATE2:
2176 case KVM_CAP_XEN_HVM:
2178 case KVM_CAP_VCPU_EVENTS:
2183 case KVM_CAP_DEBUGREGS:
2190 case KVM_CAP_READONLY_MEM:
2236 unsigned int ioctl,
unsigned long arg)
2252 if (
copy_to_user(user_msr_list, &msr_list,
sizeof msr_list))
2255 if (n < msr_list.
nmsrs)
2259 num_msrs_to_save *
sizeof(
u32)))
2263 ARRAY_SIZE(emulated_msrs) *
sizeof(
u32)))
2303 static void wbinvd_ipi(
void *garbage)
2308 static bool need_emulate_wbinvd(
struct kvm_vcpu *vcpu)
2310 return vcpu->
kvm->arch.iommu_domain &&
2317 if (need_emulate_wbinvd(vcpu)) {
2319 cpumask_set_cpu(cpu, vcpu->
arch.wbinvd_dirty_mask);
2320 else if (vcpu->
cpu != -1 && vcpu->
cpu != cpu)
2322 wbinvd_ipi,
NULL, 1);
2329 adjust_tsc_offset_host(vcpu, vcpu->
arch.tsc_offset_adjustment);
2330 vcpu->
arch.tsc_offset_adjustment = 0;
2335 s64 tsc_delta = !vcpu->
arch.last_host_tsc ? 0 :
2341 vcpu->
arch.last_guest_tsc);
2343 vcpu->
arch.tsc_catchup = 1;
2346 if (vcpu->
cpu != cpu)
2347 kvm_migrate_timers(vcpu);
2351 accumulate_steal_time(vcpu);
2362 static int kvm_vcpu_ioctl_get_lapic(
struct kvm_vcpu *vcpu,
2370 static int kvm_vcpu_ioctl_set_lapic(
struct kvm_vcpu *vcpu,
2374 update_cr8_intercept(vcpu);
2384 if (irqchip_in_kernel(vcpu->
kvm))
2387 kvm_queue_interrupt(vcpu, irq->
irq,
false);
2393 static int kvm_vcpu_ioctl_nmi(
struct kvm_vcpu *vcpu)
2400 static int vcpu_ioctl_tpr_access_reporting(
struct kvm_vcpu *vcpu,
2409 static int kvm_vcpu_ioctl_x86_setup_mce(
struct kvm_vcpu *vcpu,
2413 unsigned bank_num = mcg_cap & 0xff, bank;
2421 vcpu->
arch.mcg_cap = mcg_cap;
2423 if (mcg_cap & MCG_CTL_P)
2426 for (bank = 0; bank < bank_num; bank++)
2427 vcpu->
arch.mce_banks[bank*4] = ~(
u64)0;
2432 static int kvm_vcpu_ioctl_x86_set_mce(
struct kvm_vcpu *vcpu,
2433 struct kvm_x86_mce *
mce)
2435 u64 mcg_cap = vcpu->
arch.mcg_cap;
2436 unsigned bank_num = mcg_cap & 0xff;
2445 if ((mce->status &
MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
2446 vcpu->
arch.mcg_ctl != ~(
u64)0)
2448 banks += 4 * mce->bank;
2463 banks[2] = mce->addr;
2464 banks[3] = mce->misc;
2465 vcpu->
arch.mcg_status = mce->mcg_status;
2466 banks[1] = mce->status;
2470 if (banks[1] & MCI_STATUS_VAL)
2472 banks[2] = mce->addr;
2473 banks[3] = mce->misc;
2474 banks[1] = mce->status;
2480 static void kvm_vcpu_ioctl_x86_get_vcpu_events(
struct kvm_vcpu *vcpu,
2485 vcpu->
arch.exception.pending &&
2486 !kvm_exception_is_soft(vcpu->
arch.exception.nr);
2488 events->
exception.has_error_code = vcpu->
arch.exception.has_error_code;
2490 events->
exception.error_code = vcpu->
arch.exception.error_code;
2493 vcpu->
arch.interrupt.pending && !vcpu->
arch.interrupt.soft;
2500 events->
nmi.injected = vcpu->
arch.nmi_injected;
2501 events->
nmi.pending = vcpu->
arch.nmi_pending != 0;
2503 events->
nmi.pad = 0;
2513 static int kvm_vcpu_ioctl_x86_set_vcpu_events(
struct kvm_vcpu *vcpu,
2524 vcpu->
arch.exception.has_error_code = events->
exception.has_error_code;
2525 vcpu->
arch.exception.error_code = events->
exception.error_code;
2534 vcpu->
arch.nmi_injected = events->
nmi.injected;
2536 vcpu->
arch.nmi_pending = events->
nmi.pending;
2547 static void kvm_vcpu_ioctl_x86_get_debugregs(
struct kvm_vcpu *vcpu,
2551 dbgregs->
dr6 = vcpu->
arch.dr6;
2552 dbgregs->
dr7 = vcpu->
arch.dr7;
2557 static int kvm_vcpu_ioctl_x86_set_debugregs(
struct kvm_vcpu *vcpu,
2564 vcpu->
arch.dr6 = dbgregs->
dr6;
2565 vcpu->
arch.dr7 = dbgregs->
dr7;
2570 static void kvm_vcpu_ioctl_x86_get_xsave(
struct kvm_vcpu *vcpu,
2575 &vcpu->
arch.guest_fpu.state->xsave,
2579 &vcpu->
arch.guest_fpu.state->fxsave,
2586 static int kvm_vcpu_ioctl_x86_set_xsave(
struct kvm_vcpu *vcpu,
2604 static void kvm_vcpu_ioctl_x86_get_xcrs(
struct kvm_vcpu *vcpu,
2607 if (!cpu_has_xsave) {
2613 guest_xcrs->
flags = 0;
2615 guest_xcrs->
xcrs[0].value = vcpu->
arch.xcr0;
2618 static int kvm_vcpu_ioctl_x86_set_xcrs(
struct kvm_vcpu *vcpu,
2629 for (i = 0; i < guest_xcrs->
nr_xcrs; i++)
2633 guest_xcrs->
xcrs[0].value);
2647 static int kvm_set_guest_paused(
struct kvm_vcpu *vcpu)
2649 if (!vcpu->
arch.time_page)
2651 vcpu->
arch.pvclock_set_guest_stopped_request =
true;
2657 unsigned int ioctl,
unsigned long arg)
2673 if (!vcpu->
arch.apic)
2680 r = kvm_vcpu_ioctl_get_lapic(vcpu,
u.lapic);
2691 if (!vcpu->
arch.apic)
2694 if (IS_ERR(
u.lapic)) {
2695 r = PTR_ERR(
u.lapic);
2699 r = kvm_vcpu_ioctl_set_lapic(vcpu,
u.lapic);
2718 r = kvm_vcpu_ioctl_nmi(vcpu);
2725 struct kvm_cpuid __user *cpuid_arg = argp;
2770 r = msr_io(vcpu, argp, do_set_msr, 0);
2778 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
2791 if (!irqchip_in_kernel(vcpu->
kvm))
2806 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
2810 struct kvm_x86_mce mce;
2815 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
2821 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
2836 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
2842 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
2859 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
2868 kvm_vcpu_ioctl_x86_get_xsave(vcpu,
u.xsave);
2878 if (IS_ERR(
u.xsave)) {
2879 r = PTR_ERR(
u.xsave);
2883 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu,
u.xsave);
2892 kvm_vcpu_ioctl_x86_get_xcrs(vcpu,
u.xcrs);
2903 if (IS_ERR(
u.xcrs)) {
2904 r = PTR_ERR(
u.xcrs);
2908 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu,
u.xcrs);
2915 user_tsc_khz = (
u32)arg;
2917 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
2920 if (user_tsc_khz == 0)
2923 kvm_set_tsc_khz(vcpu, user_tsc_khz);
2929 r = vcpu->
arch.virtual_tsc_khz;
2933 r = kvm_set_guest_paused(vcpu);
2946 return VM_FAULT_SIGBUS;
2949 static int kvm_vm_ioctl_set_tss_addr(
struct kvm *kvm,
unsigned long addr)
2953 if (addr > (
unsigned int)(-3 *
PAGE_SIZE))
2959 static int kvm_vm_ioctl_set_identity_map_addr(
struct kvm *kvm,
2962 kvm->
arch.ept_identity_map_addr = ident_addr;
2966 static int kvm_vm_ioctl_set_nr_mmu_pages(
struct kvm *kvm,
2967 u32 kvm_nr_mmu_pages)
2976 kvm->
arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
2983 static int kvm_vm_ioctl_get_nr_mmu_pages(
struct kvm *kvm)
2985 return kvm->
arch.n_max_mmu_pages;
2988 static int kvm_vm_ioctl_get_irqchip(
struct kvm *kvm,
struct kvm_irqchip *
chip)
2996 &pic_irqchip(kvm)->pics[0],
3001 &pic_irqchip(kvm)->pics[1],
3014 static int kvm_vm_ioctl_set_irqchip(
struct kvm *kvm,
struct kvm_irqchip *chip)
3021 spin_lock(&pic_irqchip(kvm)->lock);
3022 memcpy(&pic_irqchip(kvm)->pics[0],
3025 spin_unlock(&pic_irqchip(kvm)->lock);
3028 spin_lock(&pic_irqchip(kvm)->lock);
3029 memcpy(&pic_irqchip(kvm)->pics[1],
3032 spin_unlock(&pic_irqchip(kvm)->lock);
3045 static int kvm_vm_ioctl_get_pit(
struct kvm *kvm,
struct kvm_pit_state *
ps)
3055 static int kvm_vm_ioctl_set_pit(
struct kvm *kvm,
struct kvm_pit_state *
ps)
3066 static int kvm_vm_ioctl_get_pit2(
struct kvm *kvm,
struct kvm_pit_state2 *
ps)
3073 ps->
flags = kvm->
arch.vpit->pit_state.flags;
3079 static int kvm_vm_ioctl_set_pit2(
struct kvm *kvm,
struct kvm_pit_state2 *
ps)
3081 int r = 0,
start = 0;
3082 u32 prev_legacy, cur_legacy;
3086 if (!prev_legacy && cur_legacy)
3089 sizeof(kvm->
arch.vpit->pit_state.channels));
3090 kvm->
arch.vpit->pit_state.flags = ps->
flags;
3096 static int kvm_vm_ioctl_reinject(
struct kvm *kvm,
3099 if (!kvm->
arch.vpit)
3132 unsigned long *dirty_bitmap_buffer;
3133 bool is_dirty =
false;
3148 n = kvm_dirty_bitmap_bytes(memslot);
3150 dirty_bitmap_buffer = dirty_bitmap + n /
sizeof(
long);
3151 memset(dirty_bitmap_buffer, 0, n);
3155 for (i = 0; i < n /
sizeof(
long); i++) {
3159 if (!dirty_bitmap[i])
3164 mask =
xchg(&dirty_bitmap[i], 0);
3165 dirty_bitmap_buffer[
i] =
mask;
3187 if (!irqchip_in_kernel(kvm))
3196 unsigned int ioctl,
unsigned long arg)
3214 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
3224 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
3230 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
3235 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
3243 goto create_irqchip_unlock;
3246 goto create_irqchip_unlock;
3261 goto create_irqchip_unlock;
3264 goto create_irqchip_unlock;
3266 kvm->
arch.vpic = vpic;
3277 create_irqchip_unlock:
3293 goto create_pit_unlock;
3312 if (!irqchip_in_kernel(kvm))
3313 goto get_irqchip_out;
3314 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
3316 goto get_irqchip_out;
3319 goto get_irqchip_out;
3338 if (!irqchip_in_kernel(kvm))
3339 goto set_irqchip_out;
3340 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
3342 goto set_irqchip_out;
3355 if (!kvm->
arch.vpit)
3357 r = kvm_vm_ioctl_get_pit(kvm, &
u.ps);
3371 if (!kvm->
arch.vpit)
3373 r = kvm_vm_ioctl_set_pit(kvm, &
u.ps);
3381 if (!kvm->
arch.vpit)
3383 r = kvm_vm_ioctl_get_pit2(kvm, &
u.ps2);
3397 if (!kvm->
arch.vpit)
3399 r = kvm_vm_ioctl_set_pit2(kvm, &
u.ps2);
3410 r = kvm_vm_ioctl_reinject(kvm, &control);
3419 sizeof(
struct kvm_xen_hvm_config)))
3422 if (kvm->
arch.xen_hvm_config.flags)
3442 now_ns = get_kernel_ns();
3443 delta = user_ns.
clock - now_ns;
3453 now_ns = get_kernel_ns();
3454 user_ns.
clock = kvm->
arch.kvmclock_offset + now_ns;
3473 static void kvm_init_msr_list(
void)
3480 if (rdmsr_safe(msrs_to_save[i], &dummy[0], &dummy[1]) < 0)
3483 msrs_to_save[
j] = msrs_to_save[
i];
3486 num_msrs_to_save =
j;
3489 static int vcpu_mmio_write(
struct kvm_vcpu *vcpu,
gpa_t addr,
int len,
3497 if (!(vcpu->
arch.apic &&
3498 !kvm_iodevice_write(&vcpu->
arch.apic->dev, addr, n, v))
3510 static int vcpu_mmio_read(
struct kvm_vcpu *vcpu,
gpa_t addr,
int len,
void *v)
3517 if (!(vcpu->
arch.apic &&
3518 !kvm_iodevice_read(&vcpu->
arch.apic->dev, addr, n, v))
3531 static void kvm_set_segment(
struct kvm_vcpu *vcpu,
3548 BUG_ON(!mmu_is_nested(vcpu));
3552 t_gpa = vcpu->
arch.mmu.gva_to_gpa(vcpu, gpa, access, &exception);
3561 return vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
3569 return vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
3577 return vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
3584 return vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
3587 static int kvm_read_guest_virt_helper(
gva_t addr,
void *val,
unsigned int bytes,
3595 gpa_t gpa = vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
3598 unsigned toread =
min(bytes, (
unsigned)
PAGE_SIZE - offset);
3619 gva_t addr,
void *val,
unsigned int bytes,
3625 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu,
3631 gva_t addr,
void *val,
unsigned int bytes,
3637 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
3643 gva_t addr,
void *val,
unsigned int bytes,
3647 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, 0, exception);
3651 gva_t addr,
void *val,
3660 gpa_t gpa = vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, addr,
3664 unsigned towrite =
min(bytes, (
unsigned)
PAGE_SIZE - offset);
3684 static int vcpu_mmio_gva_to_gpa(
struct kvm_vcpu *vcpu,
unsigned long gva,
3691 if (vcpu_match_mmio_gva(vcpu, gva)
3692 && !permission_fault(vcpu->
arch.walk_mmu, vcpu->
arch.access, access)) {
3695 trace_vcpu_match_mmio(gva, *gpa, write,
false);
3699 *gpa = vcpu->
arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
3708 if (vcpu_match_mmio_gpa(vcpu, *gpa)) {
3709 trace_vcpu_match_mmio(gva, *gpa, write,
true);
3717 const void *val,
int bytes)
3740 static int read_prepare(
struct kvm_vcpu *vcpu,
void *val,
int bytes)
3742 if (vcpu->mmio_read_completed) {
3744 vcpu->mmio_fragments[0].gpa, *(
u64 *)val);
3745 vcpu->mmio_read_completed = 0;
3753 void *val,
int bytes)
3759 void *val,
int bytes)
3764 static int write_mmio(
struct kvm_vcpu *vcpu,
gpa_t gpa,
int bytes,
void *val)
3767 return vcpu_mmio_write(vcpu, gpa, bytes, val);
3771 void *val,
int bytes)
3777 static int write_exit_mmio(
struct kvm_vcpu *vcpu,
gpa_t gpa,
3778 void *val,
int bytes)
3787 .read_write_prepare = read_prepare,
3788 .read_write_emulate = read_emulate,
3789 .read_write_mmio = vcpu_mmio_read,
3790 .read_write_exit_mmio = read_exit_mmio,
3794 .read_write_emulate = write_emulate,
3795 .read_write_mmio = write_mmio,
3796 .read_write_exit_mmio = write_exit_mmio,
3800 static int emulator_read_write_onepage(
unsigned long addr,
void *val,
3808 bool write = ops->
write;
3811 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
3828 if (handled == bytes)
3836 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
3844 void *val,
unsigned int bytes,
3856 vcpu->mmio_nr_fragments = 0;
3859 if (((addr + bytes - 1) ^ addr) &
PAGE_MASK) {
3862 now = -addr & ~PAGE_MASK;
3863 rc = emulator_read_write_onepage(addr, val, now, exception,
3873 rc = emulator_read_write_onepage(addr, val, bytes, exception,
3878 if (!vcpu->mmio_nr_fragments)
3881 gpa = vcpu->mmio_fragments[0].gpa;
3883 vcpu->mmio_needed = 1;
3884 vcpu->mmio_cur_fragment = 0;
3886 vcpu->
run->mmio.len =
min(8
u, vcpu->mmio_fragments[0].len);
3887 vcpu->
run->mmio.is_write = vcpu->mmio_is_write = ops->
write;
3889 vcpu->
run->mmio.phys_addr = gpa;
3901 exception, &read_emultor);
3911 exception, &write_emultor);
3914 #define CMPXCHG_TYPE(t, ptr, old, new) \
3915 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
3917 #ifdef CONFIG_X86_64
3918 # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
3920 # define CMPXCHG64(ptr, old, new) \
3921 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
3938 if (bytes > 8 || (bytes & (bytes - 1)))
3947 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
3951 if (is_error_page(page))
3988 static int kernel_pio(
struct kvm_vcpu *vcpu,
void *pd)
3993 if (vcpu->
arch.pio.in)
3995 vcpu->
arch.pio.size, pd);
3998 vcpu->
arch.pio.port, vcpu->
arch.pio.size,
4003 static int emulator_pio_in_out(
struct kvm_vcpu *vcpu,
int size,
4004 unsigned short port,
void *val,
4007 trace_kvm_pio(!in, port, size, count);
4014 if (!kernel_pio(vcpu, vcpu->
arch.pio_data)) {
4015 vcpu->
arch.pio.count = 0;
4030 int size,
unsigned short port,
void *val,
4036 if (vcpu->
arch.pio.count)
4039 ret = emulator_pio_in_out(vcpu, size, port, val, count,
true);
4042 memcpy(val, vcpu->
arch.pio_data, size * count);
4043 vcpu->
arch.pio.count = 0;
4051 int size,
unsigned short port,
4052 const void *val,
unsigned int count)
4056 memcpy(vcpu->
arch.pio_data, val, size * count);
4057 return emulator_pio_in_out(vcpu, size, port, (
void *)val, count,
false);
4060 static unsigned long get_segment_base(
struct kvm_vcpu *vcpu,
int seg)
4072 if (!need_emulate_wbinvd(vcpu))
4078 cpumask_set_cpu(cpu, vcpu->
arch.wbinvd_dirty_mask);
4080 wbinvd_ipi,
NULL, 1);
4082 cpumask_clear(vcpu->
arch.wbinvd_dirty_mask);
4105 static u64 mk_cr_64(
u64 curr_cr,
u32 new_val)
4107 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
4113 unsigned long value;
4117 value = kvm_read_cr0(vcpu);
4120 value = vcpu->
arch.cr2;
4123 value = kvm_read_cr3(vcpu);
4126 value = kvm_read_cr4(vcpu);
4132 kvm_err(
"%s: unexpected cr %u\n", __func__, cr);
4146 res =
kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
4155 res =
kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
4161 kvm_err(
"%s: unexpected cr %u\n", __func__, cr);
4198 static unsigned long emulator_get_cached_segment_base(
4218 set_desc_limit(desc, var.
limit);
4219 set_desc_base(desc, (
unsigned long)var.
base);
4220 #ifdef CONFIG_X86_64
4222 *base3 = var.
base >> 32;
4244 var.
base = get_desc_base(desc);
4245 #ifdef CONFIG_X86_64
4246 var.
base |= ((
u64)base3) << 32;
4248 var.
limit = get_desc_limit(desc);
4263 kvm_set_segment(vcpu, &var, seg);
4268 u32 msr_index,
u64 *pdata)
4330 .read_gpr = emulator_read_gpr,
4331 .write_gpr = emulator_write_gpr,
4332 .read_std = kvm_read_guest_virt_system,
4334 .fetch = kvm_fetch_guest_virt,
4335 .read_emulated = emulator_read_emulated,
4337 .cmpxchg_emulated = emulator_cmpxchg_emulated,
4338 .invlpg = emulator_invlpg,
4339 .pio_in_emulated = emulator_pio_in_emulated,
4340 .pio_out_emulated = emulator_pio_out_emulated,
4341 .get_segment = emulator_get_segment,
4342 .set_segment = emulator_set_segment,
4343 .get_cached_segment_base = emulator_get_cached_segment_base,
4344 .get_gdt = emulator_get_gdt,
4345 .get_idt = emulator_get_idt,
4346 .set_gdt = emulator_set_gdt,
4347 .set_idt = emulator_set_idt,
4348 .get_cr = emulator_get_cr,
4349 .set_cr = emulator_set_cr,
4350 .set_rflags = emulator_set_rflags,
4351 .cpl = emulator_get_cpl,
4354 .set_msr = emulator_set_msr,
4355 .get_msr = emulator_get_msr,
4356 .read_pmc = emulator_read_pmc,
4357 .halt = emulator_halt,
4358 .wbinvd = emulator_wbinvd,
4360 .get_fpu = emulator_get_fpu,
4361 .put_fpu = emulator_put_fpu,
4362 .intercept = emulator_intercept,
4363 .get_cpuid = emulator_get_cpuid,
4376 if (!(int_shadow & mask))
4380 static void inject_emulated_exception(
struct kvm_vcpu *vcpu)
4385 else if (ctxt->
exception.error_code_valid)
4397 ctxt->
fetch.start = 0;
4398 ctxt->
fetch.end = 0;
4405 static void init_emulate_ctxt(
struct kvm_vcpu *vcpu)
4413 ctxt->
eip = kvm_rip_read(vcpu);
4421 init_decode_cache(ctxt);
4422 vcpu->
arch.emulate_regs_need_sync_from_vcpu =
false;
4430 init_emulate_ctxt(vcpu);
4434 ctxt->
_eip = ctxt->
eip + inc_eip;
4441 kvm_rip_write(vcpu, ctxt->
eip);
4445 vcpu->
arch.nmi_pending = 0;
4447 vcpu->
arch.interrupt.pending =
false;
4453 static int handle_emulation_failure(
struct kvm_vcpu *vcpu)
4457 ++vcpu->
stat.insn_emulation_fail;
4459 if (!is_guest_mode(vcpu)) {
4462 vcpu->
run->internal.ndata = 0;
4470 static bool reexecute_instruction(
struct kvm_vcpu *vcpu,
gva_t gva)
4498 if (!is_error_pfn(pfn)) {
4507 unsigned long cr2,
int emulation_type)
4510 unsigned long last_retry_eip, last_retry_addr, gpa =
cr2;
4512 last_retry_eip = vcpu->
arch.last_retry_eip;
4513 last_retry_addr = vcpu->
arch.last_retry_addr;
4528 vcpu->
arch.last_retry_eip = vcpu->
arch.last_retry_addr = 0;
4536 if (ctxt->
eip == last_retry_eip && last_retry_addr == cr2)
4539 vcpu->
arch.last_retry_eip = ctxt->
eip;
4540 vcpu->
arch.last_retry_addr =
cr2;
4542 if (!vcpu->
arch.mmu.direct_map)
4550 static int complete_emulated_mmio(
struct kvm_vcpu *vcpu);
4551 static int complete_emulated_pio(
struct kvm_vcpu *vcpu);
4561 bool writeback =
true;
4563 kvm_clear_exception_queue(vcpu);
4566 init_emulate_ctxt(vcpu);
4577 ++vcpu->
stat.insn_emulation;
4579 if (emulation_type & EMULTYPE_TRAP_UD)
4581 if (reexecute_instruction(vcpu, cr2))
4585 return handle_emulation_failure(vcpu);
4590 kvm_rip_write(vcpu, ctxt->
_eip);
4594 if (retry_instruction(ctxt, cr2, emulation_type))
4599 if (vcpu->
arch.emulate_regs_need_sync_from_vcpu) {
4600 vcpu->
arch.emulate_regs_need_sync_from_vcpu =
false;
4611 if (reexecute_instruction(vcpu, cr2))
4614 return handle_emulation_failure(vcpu);
4618 inject_emulated_exception(vcpu);
4620 }
else if (vcpu->
arch.pio.count) {
4621 if (!vcpu->
arch.pio.in)
4622 vcpu->
arch.pio.count = 0;
4625 vcpu->
arch.complete_userspace_io = complete_emulated_pio;
4628 }
else if (vcpu->mmio_needed) {
4629 if (!vcpu->mmio_is_write)
4632 vcpu->
arch.complete_userspace_io = complete_emulated_mmio;
4642 vcpu->
arch.emulate_regs_need_sync_to_vcpu =
false;
4643 kvm_rip_write(vcpu, ctxt->
eip);
4645 vcpu->
arch.emulate_regs_need_sync_to_vcpu =
true;
4654 int ret = emulator_pio_out_emulated(&vcpu->
arch.emulate_ctxt,
4655 size, port, &val, 1);
4657 vcpu->
arch.pio.count = 0;
4662 static void tsc_bad(
void *info)
4667 static void tsc_khz_changed(
void *data)
4670 unsigned long khz = 0;
4681 static int kvmclock_cpufreq_notifier(
struct notifier_block *nb,
unsigned long val,
4738 if (vcpu->
cpu != freq->
cpu)
4747 if (freq->
old < freq->
new && send_ipi) {
4766 .notifier_call = kvmclock_cpufreq_notifier
4770 unsigned long action,
void *hcpu)
4772 unsigned int cpu = (
unsigned long)hcpu;
4787 .notifier_call = kvmclock_cpu_notifier,
4791 static void kvm_timer_init(
void)
4798 #ifdef CONFIG_CPU_FREQ
4803 if (
policy.cpuinfo.max_freq)
4804 max_tsc_khz =
policy.cpuinfo.max_freq;
4810 pr_debug(
"kvm: max_tsc_khz = %ld\n", max_tsc_khz);
4822 static int kvm_is_user_mode(
void)
4829 return user_mode != 0;
4832 static unsigned long kvm_get_guest_ip(
void)
4834 unsigned long ip = 0;
4844 .is_user_mode = kvm_is_user_mode,
4845 .get_guest_ip = kvm_get_guest_ip,
4860 static void kvm_set_mmio_spte_mask(
void)
4869 mask = ((1ull << (62 - maxphyaddr + 1)) - 1) << maxphyaddr;
4872 #ifdef CONFIG_X86_64
4877 if (maxphyaddr == 52)
4887 struct kvm_x86_ops *ops = (
struct kvm_x86_ops *)opaque;
4910 kvm_set_mmio_spte_mask();
4911 kvm_init_msr_list();
4945 ++vcpu->
stat.halt_exits;
4946 if (irqchip_in_kernel(vcpu->
kvm)) {
4960 bool fast, longmode;
4967 if (kvm_x86_ops->
get_cpl(vcpu) != 0 || !is_protmode(vcpu)) {
4973 longmode = is_long_mode(vcpu) && cs_l == 1;
4983 #ifdef CONFIG_X86_64
4987 outgpa = kvm_register_read(vcpu, VCPU_REGS_R8);
4991 code = param & 0xffff;
4992 fast = (param >> 16) & 0x1;
4993 rep_cnt = (param >> 32) & 0xfff;
4994 rep_idx = (param >> 48) & 0xfff;
4996 trace_kvm_hv_hypercall(code, fast, rep_cnt, rep_idx, ingpa, outgpa);
5007 ret = res | (((
u64)rep_done & 0xfff) << 32);
5023 if (kvm_hv_hypercall_enabled(vcpu->
kvm))
5032 trace_kvm_hypercall(nr, a0, a1, a2, a3);
5034 if (!is_long_mode(vcpu)) {
5042 if (kvm_x86_ops->
get_cpl(vcpu) != 0) {
5057 ++vcpu->
stat.hypercalls;
5066 unsigned long rip = kvm_rip_read(vcpu);
5086 static int dm_request_for_irq_injection(
struct kvm_vcpu *vcpu)
5089 vcpu->
run->request_interrupt_window &&
5093 static void post_kvm_run_save(
struct kvm_vcpu *vcpu)
5100 if (irqchip_in_kernel(vcpu->
kvm))
5106 !kvm_event_needs_reinjection(vcpu);
5109 static int vapic_enter(
struct kvm_vcpu *vcpu)
5118 if (is_error_page(page))
5121 vcpu->
arch.apic->vapic_page =
page;
5125 static void vapic_exit(
struct kvm_vcpu *vcpu)
5133 idx = srcu_read_lock(&vcpu->
kvm->srcu);
5136 srcu_read_unlock(&vcpu->
kvm->srcu, idx);
5139 static void update_cr8_intercept(
struct kvm_vcpu *vcpu)
5146 if (!vcpu->
arch.apic)
5149 if (!vcpu->
arch.apic->vapic_addr)
5162 static void inject_pending_event(
struct kvm_vcpu *vcpu)
5165 if (vcpu->
arch.exception.pending) {
5166 trace_kvm_inj_exception(vcpu->
arch.exception.nr,
5167 vcpu->
arch.exception.has_error_code,
5168 vcpu->
arch.exception.error_code);
5170 vcpu->
arch.exception.has_error_code,
5171 vcpu->
arch.exception.error_code,
5172 vcpu->
arch.exception.reinject);
5176 if (vcpu->
arch.nmi_injected) {
5181 if (vcpu->
arch.interrupt.pending) {
5187 if (vcpu->
arch.nmi_pending) {
5189 --vcpu->
arch.nmi_pending;
5190 vcpu->
arch.nmi_injected =
true;
5202 static void kvm_load_guest_xcr0(
struct kvm_vcpu *vcpu)
5212 static void kvm_put_guest_xcr0(
struct kvm_vcpu *vcpu)
5215 if (vcpu->
arch.xcr0 != host_xcr0)
5221 static void process_nmi(
struct kvm_vcpu *vcpu)
5234 vcpu->
arch.nmi_pending =
min(vcpu->
arch.nmi_pending, limit);
5238 static int vcpu_enter_guest(
struct kvm_vcpu *vcpu)
5241 bool req_int_win = !irqchip_in_kernel(vcpu->
kvm) &&
5242 vcpu->
run->request_interrupt_window;
5243 bool req_immediate_exit = 0;
5251 r = kvm_guest_time_update(vcpu);
5275 vcpu->
arch.apf.halted =
true;
5280 record_steal_time(vcpu);
5283 req_immediate_exit =
5291 if (kvm_check_request(
KVM_REQ_EVENT, vcpu) || req_int_win) {
5292 inject_pending_event(vcpu);
5295 if (vcpu->
arch.nmi_pending)
5301 update_cr8_intercept(vcpu);
5306 r = kvm_mmu_reload(vcpu);
5308 goto cancel_injection;
5316 kvm_load_guest_xcr0(vcpu);
5328 || need_resched() || signal_pending(
current)) {
5334 goto cancel_injection;
5337 srcu_read_unlock(&vcpu->
kvm->srcu, vcpu->
srcu_idx);
5339 if (req_immediate_exit)
5346 set_debugreg(vcpu->
arch.eff_db[0], 0);
5347 set_debugreg(vcpu->
arch.eff_db[1], 1);
5348 set_debugreg(vcpu->
arch.eff_db[2], 2);
5349 set_debugreg(vcpu->
arch.eff_db[3], 3);
5352 trace_kvm_entry(vcpu->
vcpu_id);
5353 kvm_x86_ops->
run(vcpu);
5362 if (hw_breakpoint_active())
5385 vcpu->
srcu_idx = srcu_read_lock(&vcpu->
kvm->srcu);
5391 unsigned long rip = kvm_rip_read(vcpu);
5398 if (vcpu->
arch.apic_attention)
5413 static int __vcpu_run(
struct kvm_vcpu *vcpu)
5416 struct kvm *kvm = vcpu->
kvm;
5419 pr_debug(
"vcpu %d received sipi with vector # %x\n",
5429 r = vapic_enter(vcpu);
5438 !vcpu->
arch.apf.halted)
5439 r = vcpu_enter_guest(vcpu);
5446 switch(vcpu->
arch.mp_state) {
5448 vcpu->
arch.mp_state =
5451 vcpu->
arch.apf.halted =
false;
5468 if (dm_request_for_irq_injection(vcpu)) {
5471 ++vcpu->
stat.request_irq_exits;
5476 if (signal_pending(
current)) {
5479 ++vcpu->
stat.signal_exits;
5481 if (need_resched()) {
5495 static inline int complete_emulated_io(
struct kvm_vcpu *vcpu)
5498 vcpu->
srcu_idx = srcu_read_lock(&vcpu->
kvm->srcu);
5500 srcu_read_unlock(&vcpu->
kvm->srcu, vcpu->
srcu_idx);
5506 static int complete_emulated_pio(
struct kvm_vcpu *vcpu)
5510 return complete_emulated_io(vcpu);
5531 static int complete_emulated_mmio(
struct kvm_vcpu *vcpu)
5533 struct kvm_run *
run = vcpu->
run;
5537 BUG_ON(!vcpu->mmio_needed);
5540 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
5542 if (!vcpu->mmio_is_write)
5545 if (frag->
len <= 8) {
5548 vcpu->mmio_cur_fragment++;
5556 if (vcpu->mmio_cur_fragment == vcpu->mmio_nr_fragments) {
5557 vcpu->mmio_needed = 0;
5558 if (vcpu->mmio_is_write)
5560 vcpu->mmio_read_completed = 1;
5561 return complete_emulated_io(vcpu);
5565 run->
mmio.phys_addr = frag->
gpa;
5566 if (vcpu->mmio_is_write)
5569 run->
mmio.is_write = vcpu->mmio_is_write;
5570 vcpu->
arch.complete_userspace_io = complete_emulated_mmio;
5594 if (!irqchip_in_kernel(vcpu->
kvm)) {
5603 vcpu->
arch.complete_userspace_io =
NULL;
5608 WARN_ON(vcpu->
arch.pio.count || vcpu->mmio_needed);
5610 r = __vcpu_run(vcpu);
5613 post_kvm_run_save(vcpu);
5622 if (vcpu->
arch.emulate_regs_need_sync_to_vcpu) {
5631 vcpu->
arch.emulate_regs_need_sync_to_vcpu =
false;
5641 #ifdef CONFIG_X86_64
5642 regs->
r8 = kvm_register_read(vcpu, VCPU_REGS_R8);
5643 regs->
r9 = kvm_register_read(vcpu, VCPU_REGS_R9);
5644 regs->
r10 = kvm_register_read(vcpu, VCPU_REGS_R10);
5645 regs->
r11 = kvm_register_read(vcpu, VCPU_REGS_R11);
5646 regs->
r12 = kvm_register_read(vcpu, VCPU_REGS_R12);
5647 regs->
r13 = kvm_register_read(vcpu, VCPU_REGS_R13);
5648 regs->
r14 = kvm_register_read(vcpu, VCPU_REGS_R14);
5649 regs->
r15 = kvm_register_read(vcpu, VCPU_REGS_R15);
5652 regs->
rip = kvm_rip_read(vcpu);
5660 vcpu->
arch.emulate_regs_need_sync_from_vcpu =
true;
5661 vcpu->
arch.emulate_regs_need_sync_to_vcpu =
false;
5671 #ifdef CONFIG_X86_64
5672 kvm_register_write(vcpu, VCPU_REGS_R8, regs->
r8);
5673 kvm_register_write(vcpu, VCPU_REGS_R9, regs->
r9);
5674 kvm_register_write(vcpu, VCPU_REGS_R10, regs->
r10);
5675 kvm_register_write(vcpu, VCPU_REGS_R11, regs->
r11);
5676 kvm_register_write(vcpu, VCPU_REGS_R12, regs->
r12);
5677 kvm_register_write(vcpu, VCPU_REGS_R13, regs->
r13);
5678 kvm_register_write(vcpu, VCPU_REGS_R14, regs->
r14);
5679 kvm_register_write(vcpu, VCPU_REGS_R15, regs->
r15);
5682 kvm_rip_write(vcpu, regs->
rip);
5685 vcpu->
arch.exception.pending =
false;
5717 kvm_x86_ops->
get_idt(vcpu, &dt);
5720 kvm_x86_ops->
get_gdt(vcpu, &dt);
5721 sregs->gdt.limit = dt.
size;
5724 sregs->
cr0 = kvm_read_cr0(vcpu);
5726 sregs->
cr3 = kvm_read_cr3(vcpu);
5727 sregs->
cr4 = kvm_read_cr4(vcpu);
5734 if (vcpu->
arch.interrupt.pending && !vcpu->
arch.interrupt.soft)
5757 int reason,
bool has_error_code,
u32 error_code)
5762 init_emulate_ctxt(vcpu);
5765 has_error_code, error_code);
5770 kvm_rip_write(vcpu, ctxt->
eip);
5780 int mmu_reset_needed = 0;
5789 kvm_x86_ops->
set_idt(vcpu, &dt);
5790 dt.
size = sregs->gdt.limit;
5792 kvm_x86_ops->
set_gdt(vcpu, &dt);
5795 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->
cr3;
5801 mmu_reset_needed |= vcpu->
arch.efer != sregs->
efer;
5805 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->
cr0;
5809 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->
cr4;
5814 idx = srcu_read_lock(&vcpu->
kvm->srcu);
5815 if (!is_long_mode(vcpu) && is_pae(vcpu)) {
5817 mmu_reset_needed = 1;
5819 srcu_read_unlock(&vcpu->
kvm->srcu, idx);
5821 if (mmu_reset_needed)
5827 if (pending_vec < max_bits) {
5828 kvm_queue_interrupt(vcpu, pending_vec,
false);
5829 pr_debug(
"Set back pending irq %d\n", pending_vec);
5842 update_cr8_intercept(vcpu);
5845 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
5846 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
5863 if (vcpu->
arch.exception.pending)
5883 vcpu->
arch.eff_db[i] = dbg->
arch.debugreg[i];
5884 vcpu->
arch.guest_debug_dr7 = dbg->
arch.debugreg[7];
5887 vcpu->
arch.eff_db[i] = vcpu->
arch.db[i];
5889 kvm_update_dr7(vcpu);
5892 vcpu->
arch.singlestep_rip = kvm_rip_read(vcpu) +
5920 idx = srcu_read_lock(&vcpu->
kvm->srcu);
5922 srcu_read_unlock(&vcpu->
kvm->srcu, idx);
5934 &vcpu->
arch.guest_fpu.state->fxsave;
5951 &vcpu->
arch.guest_fpu.state->fxsave;
5969 err = fpu_alloc(&vcpu->
arch.guest_fpu);
5986 static void fx_free(
struct kvm_vcpu *vcpu)
5988 fpu_free(&vcpu->
arch.guest_fpu);
6001 kvm_put_guest_xcr0(vcpu);
6004 fpu_restore_checking(&vcpu->
arch.guest_fpu);
6010 kvm_put_guest_xcr0(vcpu);
6016 fpu_save_init(&vcpu->
arch.guest_fpu);
6018 ++vcpu->
stat.fpu_reload;
6025 kvmclock_reset(vcpu);
6027 free_cpumask_var(vcpu->
arch.wbinvd_dirty_mask);
6037 "kvm: SMP vm created on host with unstable TSC; "
6038 "guest TSC will not be reliable\n");
6046 vcpu->
arch.mtrr_state.have_fixed = 1;
6061 vcpu->
arch.apf.msr_val = 0;
6075 vcpu->
arch.nmi_pending = 0;
6076 vcpu->
arch.nmi_injected =
false;
6081 kvm_update_dr7(vcpu);
6084 vcpu->
arch.apf.msr_val = 0;
6085 vcpu->
arch.st.msr_val = 0;
6087 kvmclock_reset(vcpu);
6090 kvm_async_pf_hash_reset(vcpu);
6091 vcpu->
arch.apf.halted =
false;
6106 bool stable, backwards_tsc =
false;
6108 kvm_shared_msr_cpu_online();
6119 if (stable && vcpu->
arch.last_host_tsc > local_tsc) {
6120 backwards_tsc =
true;
6121 if (vcpu->
arch.last_host_tsc > max_tsc)
6122 max_tsc = vcpu->
arch.last_host_tsc;
6165 if (backwards_tsc) {
6166 u64 delta_cyc = max_tsc - local_tsc;
6169 vcpu->
arch.tsc_offset_adjustment += delta_cyc;
6170 vcpu->
arch.last_host_tsc = local_tsc;
6179 kvm->
arch.last_tsc_nsec = 0;
6180 kvm->
arch.last_tsc_write = 0;
6190 drop_user_return_notifiers(garbage);
6210 return irqchip_in_kernel(vcpu->
kvm) == (vcpu->
arch.apic !=
NULL);
6224 vcpu->
arch.emulate_ctxt.ops = &emulate_ops;
6225 if (!irqchip_in_kernel(kvm) || kvm_vcpu_is_bsp(vcpu))
6237 kvm_set_tsc_khz(vcpu, max_tsc_khz);
6241 goto fail_free_pio_data;
6243 if (irqchip_in_kernel(kvm)) {
6246 goto fail_mmu_destroy;
6252 if (!vcpu->
arch.mce_banks) {
6254 goto fail_free_lapic;
6258 if (!zalloc_cpumask_var(&vcpu->
arch.wbinvd_dirty_mask,
GFP_KERNEL))
6259 goto fail_free_mce_banks;
6261 kvm_async_pf_hash_reset(vcpu);
6265 fail_free_mce_banks:
6284 idx = srcu_read_lock(&vcpu->
kvm->srcu);
6286 srcu_read_unlock(&vcpu->
kvm->srcu, idx);
6288 if (!irqchip_in_kernel(vcpu->
kvm))
6297 INIT_LIST_HEAD(&kvm->
arch.active_mmu_pages);
6298 INIT_LIST_HEAD(&kvm->
arch.assigned_dev_head);
6304 &kvm->
arch.irq_sources_bitmap);
6312 static void kvm_unload_vcpu_mmu(
struct kvm_vcpu *vcpu)
6321 static void kvm_free_vcpus(
struct kvm *kvm)
6331 kvm_unload_vcpu_mmu(vcpu);
6338 kvm->vcpus[i] =
NULL;
6355 kvm_free_vcpus(kvm);
6356 if (kvm->
arch.apic_access_page)
6358 if (kvm->
arch.ept_identity_pagetable)
6369 if (!dont || free->
arch.rmap[i] != dont->
arch.rmap[i]) {
6376 if (!dont || free->
arch.lpage_info[i - 1] !=
6377 dont->
arch.lpage_info[i - 1]) {
6379 free->
arch.lpage_info[i - 1] =
NULL;
6393 lpages = gfn_to_index(slot->
base_gfn + npages - 1,
6396 slot->
arch.rmap[
i] =
6398 if (!slot->
arch.rmap[i])
6404 sizeof(*slot->
arch.lpage_info[i - 1]));
6405 if (!slot->
arch.lpage_info[i - 1])
6409 slot->
arch.lpage_info[i - 1][0].write_count = 1;
6411 slot->
arch.lpage_info[i - 1][lpages - 1].write_count = 1;
6422 for (j = 0; j < lpages; ++
j)
6423 slot->
arch.lpage_info[i - 1][j].write_count = 1;
6437 slot->
arch.lpage_info[i - 1] =
NULL;
6448 int npages = memslot->
npages;
6459 if (npages && !old.
npages) {
6460 unsigned long userspace_addr;
6468 if (IS_ERR((
void *)userspace_addr))
6469 return PTR_ERR((
void *)userspace_addr);
6494 "kvm_vm_ioctl_set_memory_region: "
6495 "failed to munmap memory\n");
6498 if (!kvm->
arch.n_requested_mmu_pages)
6531 !vcpu->
arch.apf.halted)
6532 || !list_empty_careful(&vcpu->async_pf.done)
6551 unsigned long current_rip = kvm_rip_read(vcpu) +
6554 return current_rip == linear_rip;
6583 if ((vcpu->
arch.mmu.direct_map != work->arch.direct_map) ||
6584 is_error_page(work->page))
6587 r = kvm_mmu_reload(vcpu);
6591 if (!vcpu->
arch.mmu.direct_map &&
6592 work->arch.cr3 != vcpu->
arch.mmu.get_cr3(vcpu))
6595 vcpu->
arch.mmu.page_fault(vcpu, work->gva, 0,
true);
6598 static inline u32 kvm_async_pf_hash_fn(
gfn_t gfn)
6603 static inline u32 kvm_async_pf_next_probe(
u32 key)
6608 static void kvm_add_async_pf_gfn(
struct kvm_vcpu *vcpu,
gfn_t gfn)
6610 u32 key = kvm_async_pf_hash_fn(gfn);
6612 while (vcpu->
arch.apf.gfns[key] != ~0)
6613 key = kvm_async_pf_next_probe(key);
6615 vcpu->
arch.apf.gfns[
key] = gfn;
6621 u32 key = kvm_async_pf_hash_fn(gfn);
6624 (vcpu->
arch.apf.gfns[
key] != gfn &&
6625 vcpu->
arch.apf.gfns[
key] != ~0); i++)
6626 key = kvm_async_pf_next_probe(key);
6633 return vcpu->
arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
6636 static void kvm_del_async_pf_gfn(
struct kvm_vcpu *vcpu,
gfn_t gfn)
6640 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
6642 vcpu->
arch.apf.gfns[
i] = ~0;
6644 j = kvm_async_pf_next_probe(j);
6645 if (vcpu->
arch.apf.gfns[j] == ~0)
6647 k = kvm_async_pf_hash_fn(vcpu->
arch.apf.gfns[j]);
6653 }
while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
6654 vcpu->
arch.apf.gfns[
i] = vcpu->
arch.apf.gfns[
j];
6659 static int apf_put_user(
struct kvm_vcpu *vcpu,
u32 val)
6667 struct kvm_async_pf *
work)
6671 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
6672 kvm_add_async_pf_gfn(vcpu, work->
arch.gfn);
6674 if (!(vcpu->
arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
6675 (vcpu->
arch.apf.send_user_only &&
6676 kvm_x86_ops->
get_cpl(vcpu) == 0))
6683 fault.
address = work->arch.token;
6689 struct kvm_async_pf *
work)
6693 trace_kvm_async_pf_ready(work->arch.token, work->gva);
6694 if (is_error_page(work->page))
6695 work->arch.token = ~0;
6697 kvm_del_async_pf_gfn(vcpu, work->
arch.gfn);
6699 if ((vcpu->
arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) &&
6705 fault.
address = work->arch.token;
6708 vcpu->
arch.apf.halted =
false;
6714 if (!(vcpu->
arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
6717 return !kvm_event_needs_reinjection(vcpu) &&