]>
Commit | Line | Data |
---|---|---|
20c8ccb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
043405e1 CO |
2 | /* |
3 | * Kernel-based Virtual Machine driver for Linux | |
4 | * | |
5 | * derived from drivers/kvm/kvm_main.c | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
4d5c5d0f BAY |
8 | * Copyright (C) 2008 Qumranet, Inc. |
9 | * Copyright IBM Corporation, 2008 | |
9611c187 | 10 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
043405e1 CO |
11 | * |
12 | * Authors: | |
13 | * Avi Kivity <avi@qumranet.com> | |
14 | * Yaniv Kamay <yaniv@qumranet.com> | |
4d5c5d0f BAY |
15 | * Amit Shah <amit.shah@qumranet.com> |
16 | * Ben-Ami Yassour <benami@il.ibm.com> | |
043405e1 CO |
17 | */ |
18 | ||
edf88417 | 19 | #include <linux/kvm_host.h> |
313a3dc7 | 20 | #include "irq.h" |
88197e6a | 21 | #include "ioapic.h" |
1d737c8a | 22 | #include "mmu.h" |
7837699f | 23 | #include "i8254.h" |
37817f29 | 24 | #include "tss.h" |
5fdbf976 | 25 | #include "kvm_cache_regs.h" |
2f728d66 | 26 | #include "kvm_emulate.h" |
26eef70c | 27 | #include "x86.h" |
00b27a3e | 28 | #include "cpuid.h" |
474a5bb9 | 29 | #include "pmu.h" |
e83d5887 | 30 | #include "hyperv.h" |
8df14af4 | 31 | #include "lapic.h" |
313a3dc7 | 32 | |
18068523 | 33 | #include <linux/clocksource.h> |
4d5c5d0f | 34 | #include <linux/interrupt.h> |
313a3dc7 CO |
35 | #include <linux/kvm.h> |
36 | #include <linux/fs.h> | |
37 | #include <linux/vmalloc.h> | |
1767e931 PG |
38 | #include <linux/export.h> |
39 | #include <linux/moduleparam.h> | |
0de10343 | 40 | #include <linux/mman.h> |
2bacc55c | 41 | #include <linux/highmem.h> |
19de40a8 | 42 | #include <linux/iommu.h> |
62c476c7 | 43 | #include <linux/intel-iommu.h> |
c8076604 | 44 | #include <linux/cpufreq.h> |
18863bdd | 45 | #include <linux/user-return-notifier.h> |
a983fb23 | 46 | #include <linux/srcu.h> |
5a0e3ad6 | 47 | #include <linux/slab.h> |
ff9d07a0 | 48 | #include <linux/perf_event.h> |
7bee342a | 49 | #include <linux/uaccess.h> |
af585b92 | 50 | #include <linux/hash.h> |
a1b60c1c | 51 | #include <linux/pci.h> |
16e8d74d MT |
52 | #include <linux/timekeeper_internal.h> |
53 | #include <linux/pvclock_gtod.h> | |
87276880 FW |
54 | #include <linux/kvm_irqfd.h> |
55 | #include <linux/irqbypass.h> | |
3905f9ad | 56 | #include <linux/sched/stat.h> |
0c5f81da | 57 | #include <linux/sched/isolation.h> |
d0ec49d4 | 58 | #include <linux/mem_encrypt.h> |
3905f9ad | 59 | |
aec51dc4 | 60 | #include <trace/events/kvm.h> |
2ed152af | 61 | |
24f1e32c | 62 | #include <asm/debugreg.h> |
d825ed0a | 63 | #include <asm/msr.h> |
a5f61300 | 64 | #include <asm/desc.h> |
890ca9ae | 65 | #include <asm/mce.h> |
f89e32e0 | 66 | #include <linux/kernel_stat.h> |
78f7f1e5 | 67 | #include <asm/fpu/internal.h> /* Ugh! */ |
1d5f066e | 68 | #include <asm/pvclock.h> |
217fc9cf | 69 | #include <asm/div64.h> |
efc64404 | 70 | #include <asm/irq_remapping.h> |
b0c39dc6 | 71 | #include <asm/mshyperv.h> |
0092e434 | 72 | #include <asm/hypervisor.h> |
bf8c55d8 | 73 | #include <asm/intel_pt.h> |
b3dc0695 | 74 | #include <asm/emulate_prefix.h> |
dd2cb348 | 75 | #include <clocksource/hyperv_timer.h> |
043405e1 | 76 | |
d1898b73 DH |
77 | #define CREATE_TRACE_POINTS |
78 | #include "trace.h" | |
79 | ||
313a3dc7 | 80 | #define MAX_IO_MSRS 256 |
890ca9ae | 81 | #define KVM_MAX_MCE_BANKS 32 |
c45dcc71 AR |
82 | u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P; |
83 | EXPORT_SYMBOL_GPL(kvm_mce_cap_supported); | |
890ca9ae | 84 | |
0f65dd70 | 85 | #define emul_to_vcpu(ctxt) \ |
c9b8b07c | 86 | ((struct kvm_vcpu *)(ctxt)->vcpu) |
0f65dd70 | 87 | |
50a37eb4 JR |
88 | /* EFER defaults: |
89 | * - enable syscall per default because its emulated by KVM | |
90 | * - enable LME and LMA per default on 64 bit KVM | |
91 | */ | |
92 | #ifdef CONFIG_X86_64 | |
1260edbe LJ |
93 | static |
94 | u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA)); | |
50a37eb4 | 95 | #else |
1260edbe | 96 | static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE); |
50a37eb4 | 97 | #endif |
313a3dc7 | 98 | |
b11306b5 SC |
99 | static u64 __read_mostly cr4_reserved_bits = CR4_RESERVED_BITS; |
100 | ||
c519265f RK |
101 | #define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \ |
102 | KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) | |
37131313 | 103 | |
cb142eb7 | 104 | static void update_cr8_intercept(struct kvm_vcpu *vcpu); |
7460fb4a | 105 | static void process_nmi(struct kvm_vcpu *vcpu); |
ee2cd4b7 | 106 | static void enter_smm(struct kvm_vcpu *vcpu); |
6addfc42 | 107 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags); |
01643c51 KH |
108 | static void store_regs(struct kvm_vcpu *vcpu); |
109 | static int sync_regs(struct kvm_vcpu *vcpu); | |
674eea0f | 110 | |
afaf0b2f | 111 | struct kvm_x86_ops kvm_x86_ops __read_mostly; |
5fdbf976 | 112 | EXPORT_SYMBOL_GPL(kvm_x86_ops); |
97896d04 | 113 | |
893590c7 | 114 | static bool __read_mostly ignore_msrs = 0; |
476bc001 | 115 | module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR); |
ed85c068 | 116 | |
fab0aa3b EM |
117 | static bool __read_mostly report_ignored_msrs = true; |
118 | module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR); | |
119 | ||
4c27625b | 120 | unsigned int min_timer_period_us = 200; |
9ed96e87 MT |
121 | module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR); |
122 | ||
630994b3 MT |
123 | static bool __read_mostly kvmclock_periodic_sync = true; |
124 | module_param(kvmclock_periodic_sync, bool, S_IRUGO); | |
125 | ||
893590c7 | 126 | bool __read_mostly kvm_has_tsc_control; |
92a1f12d | 127 | EXPORT_SYMBOL_GPL(kvm_has_tsc_control); |
893590c7 | 128 | u32 __read_mostly kvm_max_guest_tsc_khz; |
92a1f12d | 129 | EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz); |
bc9b961b HZ |
130 | u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits; |
131 | EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits); | |
132 | u64 __read_mostly kvm_max_tsc_scaling_ratio; | |
133 | EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio); | |
64672c95 YJ |
134 | u64 __read_mostly kvm_default_tsc_scaling_ratio; |
135 | EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio); | |
92a1f12d | 136 | |
cc578287 | 137 | /* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */ |
893590c7 | 138 | static u32 __read_mostly tsc_tolerance_ppm = 250; |
cc578287 ZA |
139 | module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR); |
140 | ||
c3941d9e SC |
141 | /* |
142 | * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables | |
143 | * adaptive tuning starting from default advancment of 1000ns. '0' disables | |
144 | * advancement entirely. Any other value is used as-is and disables adaptive | |
145 | * tuning, i.e. allows priveleged userspace to set an exact advancement time. | |
146 | */ | |
147 | static int __read_mostly lapic_timer_advance_ns = -1; | |
0e6edceb | 148 | module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR); |
d0659d94 | 149 | |
52004014 FW |
150 | static bool __read_mostly vector_hashing = true; |
151 | module_param(vector_hashing, bool, S_IRUGO); | |
152 | ||
c4ae60e4 LA |
153 | bool __read_mostly enable_vmware_backdoor = false; |
154 | module_param(enable_vmware_backdoor, bool, S_IRUGO); | |
155 | EXPORT_SYMBOL_GPL(enable_vmware_backdoor); | |
156 | ||
6c86eedc WL |
157 | static bool __read_mostly force_emulation_prefix = false; |
158 | module_param(force_emulation_prefix, bool, S_IRUGO); | |
159 | ||
0c5f81da WL |
160 | int __read_mostly pi_inject_timer = -1; |
161 | module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR); | |
162 | ||
18863bdd AK |
163 | #define KVM_NR_SHARED_MSRS 16 |
164 | ||
165 | struct kvm_shared_msrs_global { | |
166 | int nr; | |
2bf78fa7 | 167 | u32 msrs[KVM_NR_SHARED_MSRS]; |
18863bdd AK |
168 | }; |
169 | ||
170 | struct kvm_shared_msrs { | |
171 | struct user_return_notifier urn; | |
172 | bool registered; | |
2bf78fa7 SY |
173 | struct kvm_shared_msr_values { |
174 | u64 host; | |
175 | u64 curr; | |
176 | } values[KVM_NR_SHARED_MSRS]; | |
18863bdd AK |
177 | }; |
178 | ||
179 | static struct kvm_shared_msrs_global __read_mostly shared_msrs_global; | |
013f6a5d | 180 | static struct kvm_shared_msrs __percpu *shared_msrs; |
18863bdd | 181 | |
cfc48181 SC |
182 | #define KVM_SUPPORTED_XCR0 (XFEATURE_MASK_FP | XFEATURE_MASK_SSE \ |
183 | | XFEATURE_MASK_YMM | XFEATURE_MASK_BNDREGS \ | |
184 | | XFEATURE_MASK_BNDCSR | XFEATURE_MASK_AVX512 \ | |
185 | | XFEATURE_MASK_PKRU) | |
186 | ||
91661989 SC |
187 | u64 __read_mostly host_efer; |
188 | EXPORT_SYMBOL_GPL(host_efer); | |
189 | ||
139a12cf | 190 | static u64 __read_mostly host_xss; |
408e9a31 PB |
191 | u64 __read_mostly supported_xss; |
192 | EXPORT_SYMBOL_GPL(supported_xss); | |
139a12cf | 193 | |
417bc304 | 194 | struct kvm_stats_debugfs_item debugfs_entries[] = { |
812756a8 EGE |
195 | VCPU_STAT("pf_fixed", pf_fixed), |
196 | VCPU_STAT("pf_guest", pf_guest), | |
197 | VCPU_STAT("tlb_flush", tlb_flush), | |
198 | VCPU_STAT("invlpg", invlpg), | |
199 | VCPU_STAT("exits", exits), | |
200 | VCPU_STAT("io_exits", io_exits), | |
201 | VCPU_STAT("mmio_exits", mmio_exits), | |
202 | VCPU_STAT("signal_exits", signal_exits), | |
203 | VCPU_STAT("irq_window", irq_window_exits), | |
204 | VCPU_STAT("nmi_window", nmi_window_exits), | |
205 | VCPU_STAT("halt_exits", halt_exits), | |
206 | VCPU_STAT("halt_successful_poll", halt_successful_poll), | |
207 | VCPU_STAT("halt_attempted_poll", halt_attempted_poll), | |
208 | VCPU_STAT("halt_poll_invalid", halt_poll_invalid), | |
209 | VCPU_STAT("halt_wakeup", halt_wakeup), | |
210 | VCPU_STAT("hypercalls", hypercalls), | |
211 | VCPU_STAT("request_irq", request_irq_exits), | |
212 | VCPU_STAT("irq_exits", irq_exits), | |
213 | VCPU_STAT("host_state_reload", host_state_reload), | |
214 | VCPU_STAT("fpu_reload", fpu_reload), | |
215 | VCPU_STAT("insn_emulation", insn_emulation), | |
216 | VCPU_STAT("insn_emulation_fail", insn_emulation_fail), | |
217 | VCPU_STAT("irq_injections", irq_injections), | |
218 | VCPU_STAT("nmi_injections", nmi_injections), | |
219 | VCPU_STAT("req_event", req_event), | |
220 | VCPU_STAT("l1d_flush", l1d_flush), | |
cb953129 DM |
221 | VCPU_STAT("halt_poll_success_ns", halt_poll_success_ns), |
222 | VCPU_STAT("halt_poll_fail_ns", halt_poll_fail_ns), | |
812756a8 EGE |
223 | VM_STAT("mmu_shadow_zapped", mmu_shadow_zapped), |
224 | VM_STAT("mmu_pte_write", mmu_pte_write), | |
225 | VM_STAT("mmu_pte_updated", mmu_pte_updated), | |
226 | VM_STAT("mmu_pde_zapped", mmu_pde_zapped), | |
227 | VM_STAT("mmu_flooded", mmu_flooded), | |
228 | VM_STAT("mmu_recycled", mmu_recycled), | |
229 | VM_STAT("mmu_cache_miss", mmu_cache_miss), | |
230 | VM_STAT("mmu_unsync", mmu_unsync), | |
231 | VM_STAT("remote_tlb_flush", remote_tlb_flush), | |
232 | VM_STAT("largepages", lpages, .mode = 0444), | |
233 | VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444), | |
234 | VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions), | |
417bc304 HB |
235 | { NULL } |
236 | }; | |
237 | ||
2acf923e | 238 | u64 __read_mostly host_xcr0; |
cfc48181 SC |
239 | u64 __read_mostly supported_xcr0; |
240 | EXPORT_SYMBOL_GPL(supported_xcr0); | |
2acf923e | 241 | |
80fbd280 | 242 | static struct kmem_cache *x86_fpu_cache; |
b666a4b6 | 243 | |
c9b8b07c SC |
244 | static struct kmem_cache *x86_emulator_cache; |
245 | ||
246 | static struct kmem_cache *kvm_alloc_emulator_cache(void) | |
247 | { | |
06add254 SC |
248 | unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src); |
249 | unsigned int size = sizeof(struct x86_emulate_ctxt); | |
250 | ||
251 | return kmem_cache_create_usercopy("x86_emulator", size, | |
c9b8b07c | 252 | __alignof__(struct x86_emulate_ctxt), |
06add254 SC |
253 | SLAB_ACCOUNT, useroffset, |
254 | size - useroffset, NULL); | |
c9b8b07c SC |
255 | } |
256 | ||
b6785def | 257 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); |
d6aa1000 | 258 | |
af585b92 GN |
259 | static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) |
260 | { | |
261 | int i; | |
dd03bcaa | 262 | for (i = 0; i < ASYNC_PF_PER_VCPU; i++) |
af585b92 GN |
263 | vcpu->arch.apf.gfns[i] = ~0; |
264 | } | |
265 | ||
18863bdd AK |
266 | static void kvm_on_user_return(struct user_return_notifier *urn) |
267 | { | |
268 | unsigned slot; | |
18863bdd AK |
269 | struct kvm_shared_msrs *locals |
270 | = container_of(urn, struct kvm_shared_msrs, urn); | |
2bf78fa7 | 271 | struct kvm_shared_msr_values *values; |
1650b4eb IA |
272 | unsigned long flags; |
273 | ||
274 | /* | |
275 | * Disabling irqs at this point since the following code could be | |
276 | * interrupted and executed through kvm_arch_hardware_disable() | |
277 | */ | |
278 | local_irq_save(flags); | |
279 | if (locals->registered) { | |
280 | locals->registered = false; | |
281 | user_return_notifier_unregister(urn); | |
282 | } | |
283 | local_irq_restore(flags); | |
18863bdd | 284 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
2bf78fa7 SY |
285 | values = &locals->values[slot]; |
286 | if (values->host != values->curr) { | |
287 | wrmsrl(shared_msrs_global.msrs[slot], values->host); | |
288 | values->curr = values->host; | |
18863bdd AK |
289 | } |
290 | } | |
18863bdd AK |
291 | } |
292 | ||
2bf78fa7 SY |
293 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
294 | { | |
0123be42 | 295 | BUG_ON(slot >= KVM_NR_SHARED_MSRS); |
c847fe88 | 296 | shared_msrs_global.msrs[slot] = msr; |
18863bdd AK |
297 | if (slot >= shared_msrs_global.nr) |
298 | shared_msrs_global.nr = slot + 1; | |
18863bdd AK |
299 | } |
300 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); | |
301 | ||
302 | static void kvm_shared_msr_cpu_online(void) | |
303 | { | |
05c19c2f SC |
304 | unsigned int cpu = smp_processor_id(); |
305 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); | |
306 | u64 value; | |
307 | int i; | |
18863bdd | 308 | |
05c19c2f SC |
309 | for (i = 0; i < shared_msrs_global.nr; ++i) { |
310 | rdmsrl_safe(shared_msrs_global.msrs[i], &value); | |
311 | smsr->values[i].host = value; | |
312 | smsr->values[i].curr = value; | |
313 | } | |
18863bdd AK |
314 | } |
315 | ||
8b3c3104 | 316 | int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
18863bdd | 317 | { |
013f6a5d MT |
318 | unsigned int cpu = smp_processor_id(); |
319 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); | |
8b3c3104 | 320 | int err; |
18863bdd | 321 | |
de1fca5d PB |
322 | value = (value & mask) | (smsr->values[slot].host & ~mask); |
323 | if (value == smsr->values[slot].curr) | |
8b3c3104 | 324 | return 0; |
8b3c3104 AH |
325 | err = wrmsrl_safe(shared_msrs_global.msrs[slot], value); |
326 | if (err) | |
327 | return 1; | |
328 | ||
de1fca5d | 329 | smsr->values[slot].curr = value; |
18863bdd AK |
330 | if (!smsr->registered) { |
331 | smsr->urn.on_user_return = kvm_on_user_return; | |
332 | user_return_notifier_register(&smsr->urn); | |
333 | smsr->registered = true; | |
334 | } | |
8b3c3104 | 335 | return 0; |
18863bdd AK |
336 | } |
337 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); | |
338 | ||
13a34e06 | 339 | static void drop_user_return_notifiers(void) |
3548bab5 | 340 | { |
013f6a5d MT |
341 | unsigned int cpu = smp_processor_id(); |
342 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); | |
3548bab5 AK |
343 | |
344 | if (smsr->registered) | |
345 | kvm_on_user_return(&smsr->urn); | |
346 | } | |
347 | ||
6866b83e CO |
348 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
349 | { | |
8a5a87d9 | 350 | return vcpu->arch.apic_base; |
6866b83e CO |
351 | } |
352 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
353 | ||
58871649 JM |
354 | enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) |
355 | { | |
356 | return kvm_apic_mode(kvm_get_apic_base(vcpu)); | |
357 | } | |
358 | EXPORT_SYMBOL_GPL(kvm_get_apic_mode); | |
359 | ||
58cb628d JK |
360 | int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
361 | { | |
58871649 JM |
362 | enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); |
363 | enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); | |
d6321d49 RK |
364 | u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff | |
365 | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); | |
58cb628d | 366 | |
58871649 | 367 | if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) |
58cb628d | 368 | return 1; |
58871649 JM |
369 | if (!msr_info->host_initiated) { |
370 | if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) | |
371 | return 1; | |
372 | if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) | |
373 | return 1; | |
374 | } | |
58cb628d JK |
375 | |
376 | kvm_lapic_set_base(vcpu, msr_info->data); | |
4abaffce | 377 | kvm_recalculate_apic_map(vcpu->kvm); |
58cb628d | 378 | return 0; |
6866b83e CO |
379 | } |
380 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
381 | ||
2605fc21 | 382 | asmlinkage __visible void kvm_spurious_fault(void) |
e3ba45b8 GL |
383 | { |
384 | /* Fault while not rebooting. We want the trace. */ | |
b4fdcf60 | 385 | BUG_ON(!kvm_rebooting); |
e3ba45b8 GL |
386 | } |
387 | EXPORT_SYMBOL_GPL(kvm_spurious_fault); | |
388 | ||
3fd28fce ED |
389 | #define EXCPT_BENIGN 0 |
390 | #define EXCPT_CONTRIBUTORY 1 | |
391 | #define EXCPT_PF 2 | |
392 | ||
393 | static int exception_class(int vector) | |
394 | { | |
395 | switch (vector) { | |
396 | case PF_VECTOR: | |
397 | return EXCPT_PF; | |
398 | case DE_VECTOR: | |
399 | case TS_VECTOR: | |
400 | case NP_VECTOR: | |
401 | case SS_VECTOR: | |
402 | case GP_VECTOR: | |
403 | return EXCPT_CONTRIBUTORY; | |
404 | default: | |
405 | break; | |
406 | } | |
407 | return EXCPT_BENIGN; | |
408 | } | |
409 | ||
d6e8c854 NA |
410 | #define EXCPT_FAULT 0 |
411 | #define EXCPT_TRAP 1 | |
412 | #define EXCPT_ABORT 2 | |
413 | #define EXCPT_INTERRUPT 3 | |
414 | ||
415 | static int exception_type(int vector) | |
416 | { | |
417 | unsigned int mask; | |
418 | ||
419 | if (WARN_ON(vector > 31 || vector == NMI_VECTOR)) | |
420 | return EXCPT_INTERRUPT; | |
421 | ||
422 | mask = 1 << vector; | |
423 | ||
424 | /* #DB is trap, as instruction watchpoints are handled elsewhere */ | |
425 | if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) | |
426 | return EXCPT_TRAP; | |
427 | ||
428 | if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) | |
429 | return EXCPT_ABORT; | |
430 | ||
431 | /* Reserved exceptions will result in fault */ | |
432 | return EXCPT_FAULT; | |
433 | } | |
434 | ||
da998b46 JM |
435 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu) |
436 | { | |
437 | unsigned nr = vcpu->arch.exception.nr; | |
438 | bool has_payload = vcpu->arch.exception.has_payload; | |
439 | unsigned long payload = vcpu->arch.exception.payload; | |
440 | ||
441 | if (!has_payload) | |
442 | return; | |
443 | ||
444 | switch (nr) { | |
f10c729f JM |
445 | case DB_VECTOR: |
446 | /* | |
447 | * "Certain debug exceptions may clear bit 0-3. The | |
448 | * remaining contents of the DR6 register are never | |
449 | * cleared by the processor". | |
450 | */ | |
451 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; | |
452 | /* | |
453 | * DR6.RTM is set by all #DB exceptions that don't clear it. | |
454 | */ | |
455 | vcpu->arch.dr6 |= DR6_RTM; | |
456 | vcpu->arch.dr6 |= payload; | |
457 | /* | |
458 | * Bit 16 should be set in the payload whenever the #DB | |
459 | * exception should clear DR6.RTM. This makes the payload | |
460 | * compatible with the pending debug exceptions under VMX. | |
461 | * Though not currently documented in the SDM, this also | |
462 | * makes the payload compatible with the exit qualification | |
463 | * for #DB exceptions under VMX. | |
464 | */ | |
465 | vcpu->arch.dr6 ^= payload & DR6_RTM; | |
307f1cfa OU |
466 | |
467 | /* | |
468 | * The #DB payload is defined as compatible with the 'pending | |
469 | * debug exceptions' field under VMX, not DR6. While bit 12 is | |
470 | * defined in the 'pending debug exceptions' field (enabled | |
471 | * breakpoint), it is reserved and must be zero in DR6. | |
472 | */ | |
473 | vcpu->arch.dr6 &= ~BIT(12); | |
f10c729f | 474 | break; |
da998b46 JM |
475 | case PF_VECTOR: |
476 | vcpu->arch.cr2 = payload; | |
477 | break; | |
478 | } | |
479 | ||
480 | vcpu->arch.exception.has_payload = false; | |
481 | vcpu->arch.exception.payload = 0; | |
482 | } | |
483 | EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload); | |
484 | ||
3fd28fce | 485 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
ce7ddec4 | 486 | unsigned nr, bool has_error, u32 error_code, |
91e86d22 | 487 | bool has_payload, unsigned long payload, bool reinject) |
3fd28fce ED |
488 | { |
489 | u32 prev_nr; | |
490 | int class1, class2; | |
491 | ||
3842d135 AK |
492 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
493 | ||
664f8e26 | 494 | if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { |
3fd28fce | 495 | queue: |
3ffb2468 NA |
496 | if (has_error && !is_protmode(vcpu)) |
497 | has_error = false; | |
664f8e26 WL |
498 | if (reinject) { |
499 | /* | |
500 | * On vmentry, vcpu->arch.exception.pending is only | |
501 | * true if an event injection was blocked by | |
502 | * nested_run_pending. In that case, however, | |
503 | * vcpu_enter_guest requests an immediate exit, | |
504 | * and the guest shouldn't proceed far enough to | |
505 | * need reinjection. | |
506 | */ | |
507 | WARN_ON_ONCE(vcpu->arch.exception.pending); | |
508 | vcpu->arch.exception.injected = true; | |
91e86d22 JM |
509 | if (WARN_ON_ONCE(has_payload)) { |
510 | /* | |
511 | * A reinjected event has already | |
512 | * delivered its payload. | |
513 | */ | |
514 | has_payload = false; | |
515 | payload = 0; | |
516 | } | |
664f8e26 WL |
517 | } else { |
518 | vcpu->arch.exception.pending = true; | |
519 | vcpu->arch.exception.injected = false; | |
520 | } | |
3fd28fce ED |
521 | vcpu->arch.exception.has_error_code = has_error; |
522 | vcpu->arch.exception.nr = nr; | |
523 | vcpu->arch.exception.error_code = error_code; | |
91e86d22 JM |
524 | vcpu->arch.exception.has_payload = has_payload; |
525 | vcpu->arch.exception.payload = payload; | |
a06230b6 | 526 | if (!is_guest_mode(vcpu)) |
da998b46 | 527 | kvm_deliver_exception_payload(vcpu); |
3fd28fce ED |
528 | return; |
529 | } | |
530 | ||
531 | /* to check exception */ | |
532 | prev_nr = vcpu->arch.exception.nr; | |
533 | if (prev_nr == DF_VECTOR) { | |
534 | /* triple fault -> shutdown */ | |
a8eeb04a | 535 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
3fd28fce ED |
536 | return; |
537 | } | |
538 | class1 = exception_class(prev_nr); | |
539 | class2 = exception_class(nr); | |
540 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) | |
541 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { | |
664f8e26 WL |
542 | /* |
543 | * Generate double fault per SDM Table 5-5. Set | |
544 | * exception.pending = true so that the double fault | |
545 | * can trigger a nested vmexit. | |
546 | */ | |
3fd28fce | 547 | vcpu->arch.exception.pending = true; |
664f8e26 | 548 | vcpu->arch.exception.injected = false; |
3fd28fce ED |
549 | vcpu->arch.exception.has_error_code = true; |
550 | vcpu->arch.exception.nr = DF_VECTOR; | |
551 | vcpu->arch.exception.error_code = 0; | |
c851436a JM |
552 | vcpu->arch.exception.has_payload = false; |
553 | vcpu->arch.exception.payload = 0; | |
3fd28fce ED |
554 | } else |
555 | /* replace previous exception with a new one in a hope | |
556 | that instruction re-execution will regenerate lost | |
557 | exception */ | |
558 | goto queue; | |
559 | } | |
560 | ||
298101da AK |
561 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
562 | { | |
91e86d22 | 563 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false); |
298101da AK |
564 | } |
565 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
566 | ||
ce7ddec4 JR |
567 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
568 | { | |
91e86d22 | 569 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true); |
ce7ddec4 JR |
570 | } |
571 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); | |
572 | ||
4d5523cf PB |
573 | void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, |
574 | unsigned long payload) | |
f10c729f JM |
575 | { |
576 | kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false); | |
577 | } | |
4d5523cf | 578 | EXPORT_SYMBOL_GPL(kvm_queue_exception_p); |
f10c729f | 579 | |
da998b46 JM |
580 | static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, |
581 | u32 error_code, unsigned long payload) | |
582 | { | |
583 | kvm_multiple_exception(vcpu, nr, true, error_code, | |
584 | true, payload, false); | |
585 | } | |
586 | ||
6affcbed | 587 | int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
c3c91fee | 588 | { |
db8fcefa AP |
589 | if (err) |
590 | kvm_inject_gp(vcpu, 0); | |
591 | else | |
6affcbed KH |
592 | return kvm_skip_emulated_instruction(vcpu); |
593 | ||
594 | return 1; | |
db8fcefa AP |
595 | } |
596 | EXPORT_SYMBOL_GPL(kvm_complete_insn_gp); | |
8df25a32 | 597 | |
6389ee94 | 598 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
c3c91fee AK |
599 | { |
600 | ++vcpu->stat.pf_guest; | |
adfe20fb WL |
601 | vcpu->arch.exception.nested_apf = |
602 | is_guest_mode(vcpu) && fault->async_page_fault; | |
da998b46 | 603 | if (vcpu->arch.exception.nested_apf) { |
adfe20fb | 604 | vcpu->arch.apf.nested_apf_token = fault->address; |
da998b46 JM |
605 | kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code); |
606 | } else { | |
607 | kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, | |
608 | fault->address); | |
609 | } | |
c3c91fee | 610 | } |
27d6c865 | 611 | EXPORT_SYMBOL_GPL(kvm_inject_page_fault); |
c3c91fee | 612 | |
53b3d8e9 SC |
613 | bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu, |
614 | struct x86_exception *fault) | |
d4f8cf66 | 615 | { |
0cd665bd | 616 | struct kvm_mmu *fault_mmu; |
53b3d8e9 SC |
617 | WARN_ON_ONCE(fault->vector != PF_VECTOR); |
618 | ||
0cd665bd PB |
619 | fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : |
620 | vcpu->arch.walk_mmu; | |
ef54bcfe | 621 | |
ee1fa209 JS |
622 | /* |
623 | * Invalidate the TLB entry for the faulting address, if it exists, | |
624 | * else the access will fault indefinitely (and to emulate hardware). | |
625 | */ | |
626 | if ((fault->error_code & PFERR_PRESENT_MASK) && | |
627 | !(fault->error_code & PFERR_RSVD_MASK)) | |
628 | kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address, | |
629 | fault_mmu->root_hpa); | |
630 | ||
631 | fault_mmu->inject_page_fault(vcpu, fault); | |
ef54bcfe | 632 | return fault->nested_page_fault; |
d4f8cf66 | 633 | } |
53b3d8e9 | 634 | EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault); |
d4f8cf66 | 635 | |
3419ffc8 SY |
636 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
637 | { | |
7460fb4a AK |
638 | atomic_inc(&vcpu->arch.nmi_queued); |
639 | kvm_make_request(KVM_REQ_NMI, vcpu); | |
3419ffc8 SY |
640 | } |
641 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); | |
642 | ||
298101da AK |
643 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
644 | { | |
91e86d22 | 645 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false); |
298101da AK |
646 | } |
647 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
648 | ||
ce7ddec4 JR |
649 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
650 | { | |
91e86d22 | 651 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true); |
ce7ddec4 JR |
652 | } |
653 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); | |
654 | ||
0a79b009 AK |
655 | /* |
656 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue | |
657 | * a #GP and return false. | |
658 | */ | |
659 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) | |
298101da | 660 | { |
afaf0b2f | 661 | if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl) |
0a79b009 AK |
662 | return true; |
663 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
664 | return false; | |
298101da | 665 | } |
0a79b009 | 666 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
298101da | 667 | |
16f8a6f9 NA |
668 | bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr) |
669 | { | |
670 | if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE)) | |
671 | return true; | |
672 | ||
673 | kvm_queue_exception(vcpu, UD_VECTOR); | |
674 | return false; | |
675 | } | |
676 | EXPORT_SYMBOL_GPL(kvm_require_dr); | |
677 | ||
ec92fe44 JR |
678 | /* |
679 | * This function will be used to read from the physical memory of the currently | |
54bf36aa | 680 | * running guest. The difference to kvm_vcpu_read_guest_page is that this function |
ec92fe44 JR |
681 | * can read from guest physical or from the guest's guest physical memory. |
682 | */ | |
683 | int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, | |
684 | gfn_t ngfn, void *data, int offset, int len, | |
685 | u32 access) | |
686 | { | |
54987b7a | 687 | struct x86_exception exception; |
ec92fe44 JR |
688 | gfn_t real_gfn; |
689 | gpa_t ngpa; | |
690 | ||
691 | ngpa = gfn_to_gpa(ngfn); | |
54987b7a | 692 | real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception); |
ec92fe44 JR |
693 | if (real_gfn == UNMAPPED_GVA) |
694 | return -EFAULT; | |
695 | ||
696 | real_gfn = gpa_to_gfn(real_gfn); | |
697 | ||
54bf36aa | 698 | return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len); |
ec92fe44 JR |
699 | } |
700 | EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu); | |
701 | ||
69b0049a | 702 | static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
3d06b8bf JR |
703 | void *data, int offset, int len, u32 access) |
704 | { | |
705 | return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn, | |
706 | data, offset, len, access); | |
707 | } | |
708 | ||
16cfacc8 SC |
709 | static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu) |
710 | { | |
711 | return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) | | |
712 | rsvd_bits(1, 2); | |
713 | } | |
714 | ||
a03490ed | 715 | /* |
16cfacc8 | 716 | * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. |
a03490ed | 717 | */ |
ff03a073 | 718 | int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) |
a03490ed CO |
719 | { |
720 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
721 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; | |
722 | int i; | |
723 | int ret; | |
ff03a073 | 724 | u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; |
a03490ed | 725 | |
ff03a073 JR |
726 | ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte, |
727 | offset * sizeof(u64), sizeof(pdpte), | |
728 | PFERR_USER_MASK|PFERR_WRITE_MASK); | |
a03490ed CO |
729 | if (ret < 0) { |
730 | ret = 0; | |
731 | goto out; | |
732 | } | |
733 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { | |
812f30b2 | 734 | if ((pdpte[i] & PT_PRESENT_MASK) && |
16cfacc8 | 735 | (pdpte[i] & pdptr_rsvd_bits(vcpu))) { |
a03490ed CO |
736 | ret = 0; |
737 | goto out; | |
738 | } | |
739 | } | |
740 | ret = 1; | |
741 | ||
ff03a073 | 742 | memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); |
cb3c1e2f SC |
743 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); |
744 | ||
a03490ed | 745 | out: |
a03490ed CO |
746 | |
747 | return ret; | |
748 | } | |
cc4b6871 | 749 | EXPORT_SYMBOL_GPL(load_pdptrs); |
a03490ed | 750 | |
9ed38ffa | 751 | bool pdptrs_changed(struct kvm_vcpu *vcpu) |
d835dfec | 752 | { |
ff03a073 | 753 | u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)]; |
3d06b8bf JR |
754 | int offset; |
755 | gfn_t gfn; | |
d835dfec AK |
756 | int r; |
757 | ||
bf03d4f9 | 758 | if (!is_pae_paging(vcpu)) |
d835dfec AK |
759 | return false; |
760 | ||
cb3c1e2f | 761 | if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR)) |
6de4f3ad AK |
762 | return true; |
763 | ||
a512177e PB |
764 | gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT; |
765 | offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1); | |
3d06b8bf JR |
766 | r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte), |
767 | PFERR_USER_MASK | PFERR_WRITE_MASK); | |
d835dfec | 768 | if (r < 0) |
7f7f0d9c | 769 | return true; |
d835dfec | 770 | |
7f7f0d9c | 771 | return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0; |
d835dfec | 772 | } |
9ed38ffa | 773 | EXPORT_SYMBOL_GPL(pdptrs_changed); |
d835dfec | 774 | |
49a9b07e | 775 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
a03490ed | 776 | { |
aad82703 | 777 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
d81135a5 | 778 | unsigned long update_bits = X86_CR0_PG | X86_CR0_WP; |
aad82703 | 779 | |
f9a48e6a AK |
780 | cr0 |= X86_CR0_ET; |
781 | ||
ab344828 | 782 | #ifdef CONFIG_X86_64 |
0f12244f GN |
783 | if (cr0 & 0xffffffff00000000UL) |
784 | return 1; | |
ab344828 GN |
785 | #endif |
786 | ||
787 | cr0 &= ~CR0_RESERVED_BITS; | |
a03490ed | 788 | |
0f12244f GN |
789 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
790 | return 1; | |
a03490ed | 791 | |
0f12244f GN |
792 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
793 | return 1; | |
a03490ed CO |
794 | |
795 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { | |
796 | #ifdef CONFIG_X86_64 | |
f6801dff | 797 | if ((vcpu->arch.efer & EFER_LME)) { |
a03490ed CO |
798 | int cs_db, cs_l; |
799 | ||
0f12244f GN |
800 | if (!is_pae(vcpu)) |
801 | return 1; | |
afaf0b2f | 802 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
0f12244f GN |
803 | if (cs_l) |
804 | return 1; | |
a03490ed CO |
805 | } else |
806 | #endif | |
ff03a073 | 807 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
9f8fe504 | 808 | kvm_read_cr3(vcpu))) |
0f12244f | 809 | return 1; |
a03490ed CO |
810 | } |
811 | ||
ad756a16 MJ |
812 | if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) |
813 | return 1; | |
814 | ||
afaf0b2f | 815 | kvm_x86_ops.set_cr0(vcpu, cr0); |
a03490ed | 816 | |
d170c419 | 817 | if ((cr0 ^ old_cr0) & X86_CR0_PG) { |
e5f3f027 | 818 | kvm_clear_async_pf_completion_queue(vcpu); |
d170c419 LJ |
819 | kvm_async_pf_hash_reset(vcpu); |
820 | } | |
e5f3f027 | 821 | |
aad82703 SY |
822 | if ((cr0 ^ old_cr0) & update_bits) |
823 | kvm_mmu_reset_context(vcpu); | |
b18d5431 | 824 | |
879ae188 LE |
825 | if (((cr0 ^ old_cr0) & X86_CR0_CD) && |
826 | kvm_arch_has_noncoherent_dma(vcpu->kvm) && | |
827 | !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) | |
b18d5431 XG |
828 | kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); |
829 | ||
0f12244f GN |
830 | return 0; |
831 | } | |
2d3ad1f4 | 832 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
a03490ed | 833 | |
2d3ad1f4 | 834 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
a03490ed | 835 | { |
49a9b07e | 836 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
a03490ed | 837 | } |
2d3ad1f4 | 838 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
a03490ed | 839 | |
139a12cf | 840 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 841 | { |
139a12cf AL |
842 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
843 | ||
844 | if (vcpu->arch.xcr0 != host_xcr0) | |
845 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); | |
846 | ||
847 | if (vcpu->arch.xsaves_enabled && | |
848 | vcpu->arch.ia32_xss != host_xss) | |
849 | wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); | |
850 | } | |
37486135 BM |
851 | |
852 | if (static_cpu_has(X86_FEATURE_PKU) && | |
853 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
854 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) && | |
855 | vcpu->arch.pkru != vcpu->arch.host_pkru) | |
856 | __write_pkru(vcpu->arch.pkru); | |
42bdf991 | 857 | } |
139a12cf | 858 | EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state); |
42bdf991 | 859 | |
139a12cf | 860 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 861 | { |
37486135 BM |
862 | if (static_cpu_has(X86_FEATURE_PKU) && |
863 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
864 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) { | |
865 | vcpu->arch.pkru = rdpkru(); | |
866 | if (vcpu->arch.pkru != vcpu->arch.host_pkru) | |
867 | __write_pkru(vcpu->arch.host_pkru); | |
868 | } | |
869 | ||
139a12cf AL |
870 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
871 | ||
872 | if (vcpu->arch.xcr0 != host_xcr0) | |
873 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); | |
874 | ||
875 | if (vcpu->arch.xsaves_enabled && | |
876 | vcpu->arch.ia32_xss != host_xss) | |
877 | wrmsrl(MSR_IA32_XSS, host_xss); | |
878 | } | |
879 | ||
42bdf991 | 880 | } |
139a12cf | 881 | EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); |
42bdf991 | 882 | |
69b0049a | 883 | static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
2acf923e | 884 | { |
56c103ec LJ |
885 | u64 xcr0 = xcr; |
886 | u64 old_xcr0 = vcpu->arch.xcr0; | |
46c34cb0 | 887 | u64 valid_bits; |
2acf923e DC |
888 | |
889 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ | |
890 | if (index != XCR_XFEATURE_ENABLED_MASK) | |
891 | return 1; | |
d91cab78 | 892 | if (!(xcr0 & XFEATURE_MASK_FP)) |
2acf923e | 893 | return 1; |
d91cab78 | 894 | if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE)) |
2acf923e | 895 | return 1; |
46c34cb0 PB |
896 | |
897 | /* | |
898 | * Do not allow the guest to set bits that we do not support | |
899 | * saving. However, xcr0 bit 0 is always set, even if the | |
900 | * emulated CPU does not support XSAVE (see fx_init). | |
901 | */ | |
d91cab78 | 902 | valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; |
46c34cb0 | 903 | if (xcr0 & ~valid_bits) |
2acf923e | 904 | return 1; |
46c34cb0 | 905 | |
d91cab78 DH |
906 | if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) != |
907 | (!(xcr0 & XFEATURE_MASK_BNDCSR))) | |
390bd528 LJ |
908 | return 1; |
909 | ||
d91cab78 DH |
910 | if (xcr0 & XFEATURE_MASK_AVX512) { |
911 | if (!(xcr0 & XFEATURE_MASK_YMM)) | |
612263b3 | 912 | return 1; |
d91cab78 | 913 | if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512) |
612263b3 CP |
914 | return 1; |
915 | } | |
2acf923e | 916 | vcpu->arch.xcr0 = xcr0; |
56c103ec | 917 | |
d91cab78 | 918 | if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) |
56c103ec | 919 | kvm_update_cpuid(vcpu); |
2acf923e DC |
920 | return 0; |
921 | } | |
922 | ||
923 | int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) | |
924 | { | |
afaf0b2f | 925 | if (kvm_x86_ops.get_cpl(vcpu) != 0 || |
764bcbc5 | 926 | __kvm_set_xcr(vcpu, index, xcr)) { |
2acf923e DC |
927 | kvm_inject_gp(vcpu, 0); |
928 | return 1; | |
929 | } | |
930 | return 0; | |
931 | } | |
932 | EXPORT_SYMBOL_GPL(kvm_set_xcr); | |
933 | ||
345599f9 SC |
934 | #define __cr4_reserved_bits(__cpu_has, __c) \ |
935 | ({ \ | |
936 | u64 __reserved_bits = CR4_RESERVED_BITS; \ | |
937 | \ | |
938 | if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ | |
939 | __reserved_bits |= X86_CR4_OSXSAVE; \ | |
940 | if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ | |
941 | __reserved_bits |= X86_CR4_SMEP; \ | |
942 | if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ | |
943 | __reserved_bits |= X86_CR4_SMAP; \ | |
944 | if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ | |
945 | __reserved_bits |= X86_CR4_FSGSBASE; \ | |
946 | if (!__cpu_has(__c, X86_FEATURE_PKU)) \ | |
947 | __reserved_bits |= X86_CR4_PKE; \ | |
948 | if (!__cpu_has(__c, X86_FEATURE_LA57)) \ | |
949 | __reserved_bits |= X86_CR4_LA57; \ | |
d76c7fbc SC |
950 | if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ |
951 | __reserved_bits |= X86_CR4_UMIP; \ | |
345599f9 SC |
952 | __reserved_bits; \ |
953 | }) | |
a03490ed | 954 | |
3ca94192 | 955 | static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
a03490ed | 956 | { |
b11306b5 | 957 | if (cr4 & cr4_reserved_bits) |
3ca94192 | 958 | return -EINVAL; |
b9baba86 | 959 | |
345599f9 | 960 | if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu)) |
3ca94192 WL |
961 | return -EINVAL; |
962 | ||
963 | return 0; | |
964 | } | |
965 | ||
966 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
967 | { | |
968 | unsigned long old_cr4 = kvm_read_cr4(vcpu); | |
969 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | | |
970 | X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE; | |
971 | ||
972 | if (kvm_valid_cr4(vcpu, cr4)) | |
ae3e61e1 PB |
973 | return 1; |
974 | ||
a03490ed | 975 | if (is_long_mode(vcpu)) { |
0f12244f GN |
976 | if (!(cr4 & X86_CR4_PAE)) |
977 | return 1; | |
a2edf57f AK |
978 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
979 | && ((cr4 ^ old_cr4) & pdptr_bits) | |
9f8fe504 AK |
980 | && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
981 | kvm_read_cr3(vcpu))) | |
0f12244f GN |
982 | return 1; |
983 | ||
ad756a16 | 984 | if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) { |
d6321d49 | 985 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID)) |
ad756a16 MJ |
986 | return 1; |
987 | ||
988 | /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */ | |
989 | if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu)) | |
990 | return 1; | |
991 | } | |
992 | ||
afaf0b2f | 993 | if (kvm_x86_ops.set_cr4(vcpu, cr4)) |
0f12244f | 994 | return 1; |
a03490ed | 995 | |
ad756a16 MJ |
996 | if (((cr4 ^ old_cr4) & pdptr_bits) || |
997 | (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE))) | |
aad82703 | 998 | kvm_mmu_reset_context(vcpu); |
0f12244f | 999 | |
b9baba86 | 1000 | if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) |
00b27a3e | 1001 | kvm_update_cpuid(vcpu); |
2acf923e | 1002 | |
0f12244f GN |
1003 | return 0; |
1004 | } | |
2d3ad1f4 | 1005 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
a03490ed | 1006 | |
2390218b | 1007 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 1008 | { |
ade61e28 | 1009 | bool skip_tlb_flush = false; |
ac146235 | 1010 | #ifdef CONFIG_X86_64 |
c19986fe JS |
1011 | bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
1012 | ||
ade61e28 | 1013 | if (pcid_enabled) { |
208320ba JS |
1014 | skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH; |
1015 | cr3 &= ~X86_CR3_PCID_NOFLUSH; | |
ade61e28 | 1016 | } |
ac146235 | 1017 | #endif |
9d88fca7 | 1018 | |
9f8fe504 | 1019 | if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { |
956bf353 JS |
1020 | if (!skip_tlb_flush) { |
1021 | kvm_mmu_sync_roots(vcpu); | |
eeeb4f67 | 1022 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
956bf353 | 1023 | } |
0f12244f | 1024 | return 0; |
d835dfec AK |
1025 | } |
1026 | ||
d1cd3ce9 | 1027 | if (is_long_mode(vcpu) && |
a780a3ea | 1028 | (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))) |
d1cd3ce9 | 1029 | return 1; |
bf03d4f9 PB |
1030 | else if (is_pae_paging(vcpu) && |
1031 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) | |
346874c9 | 1032 | return 1; |
a03490ed | 1033 | |
be01e8e2 | 1034 | kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush); |
0f12244f | 1035 | vcpu->arch.cr3 = cr3; |
cb3c1e2f | 1036 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
7c390d35 | 1037 | |
0f12244f GN |
1038 | return 0; |
1039 | } | |
2d3ad1f4 | 1040 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
a03490ed | 1041 | |
eea1cff9 | 1042 | int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
a03490ed | 1043 | { |
0f12244f GN |
1044 | if (cr8 & CR8_RESERVED_BITS) |
1045 | return 1; | |
35754c98 | 1046 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1047 | kvm_lapic_set_tpr(vcpu, cr8); |
1048 | else | |
ad312c7c | 1049 | vcpu->arch.cr8 = cr8; |
0f12244f GN |
1050 | return 0; |
1051 | } | |
2d3ad1f4 | 1052 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
a03490ed | 1053 | |
2d3ad1f4 | 1054 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
a03490ed | 1055 | { |
35754c98 | 1056 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1057 | return kvm_lapic_get_cr8(vcpu); |
1058 | else | |
ad312c7c | 1059 | return vcpu->arch.cr8; |
a03490ed | 1060 | } |
2d3ad1f4 | 1061 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
a03490ed | 1062 | |
ae561ede NA |
1063 | static void kvm_update_dr0123(struct kvm_vcpu *vcpu) |
1064 | { | |
1065 | int i; | |
1066 | ||
1067 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { | |
1068 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
1069 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
1070 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD; | |
1071 | } | |
1072 | } | |
1073 | ||
7c86663b | 1074 | void kvm_update_dr7(struct kvm_vcpu *vcpu) |
c8639010 JK |
1075 | { |
1076 | unsigned long dr7; | |
1077 | ||
1078 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) | |
1079 | dr7 = vcpu->arch.guest_debug_dr7; | |
1080 | else | |
1081 | dr7 = vcpu->arch.dr7; | |
afaf0b2f | 1082 | kvm_x86_ops.set_dr7(vcpu, dr7); |
360b948d PB |
1083 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; |
1084 | if (dr7 & DR7_BP_EN_MASK) | |
1085 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; | |
c8639010 | 1086 | } |
7c86663b | 1087 | EXPORT_SYMBOL_GPL(kvm_update_dr7); |
c8639010 | 1088 | |
6f43ed01 NA |
1089 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) |
1090 | { | |
1091 | u64 fixed = DR6_FIXED_1; | |
1092 | ||
d6321d49 | 1093 | if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM)) |
6f43ed01 NA |
1094 | fixed |= DR6_RTM; |
1095 | return fixed; | |
1096 | } | |
1097 | ||
338dbc97 | 1098 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
020df079 | 1099 | { |
ea740059 MP |
1100 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1101 | ||
020df079 GN |
1102 | switch (dr) { |
1103 | case 0 ... 3: | |
ea740059 | 1104 | vcpu->arch.db[array_index_nospec(dr, size)] = val; |
020df079 GN |
1105 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
1106 | vcpu->arch.eff_db[dr] = val; | |
1107 | break; | |
1108 | case 4: | |
020df079 GN |
1109 | /* fall through */ |
1110 | case 6: | |
338dbc97 GN |
1111 | if (val & 0xffffffff00000000ULL) |
1112 | return -1; /* #GP */ | |
6f43ed01 | 1113 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
020df079 GN |
1114 | break; |
1115 | case 5: | |
020df079 GN |
1116 | /* fall through */ |
1117 | default: /* 7 */ | |
b91991bf | 1118 | if (!kvm_dr7_valid(val)) |
338dbc97 | 1119 | return -1; /* #GP */ |
020df079 | 1120 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
c8639010 | 1121 | kvm_update_dr7(vcpu); |
020df079 GN |
1122 | break; |
1123 | } | |
1124 | ||
1125 | return 0; | |
1126 | } | |
338dbc97 GN |
1127 | |
1128 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) | |
1129 | { | |
16f8a6f9 | 1130 | if (__kvm_set_dr(vcpu, dr, val)) { |
338dbc97 | 1131 | kvm_inject_gp(vcpu, 0); |
16f8a6f9 NA |
1132 | return 1; |
1133 | } | |
1134 | return 0; | |
338dbc97 | 1135 | } |
020df079 GN |
1136 | EXPORT_SYMBOL_GPL(kvm_set_dr); |
1137 | ||
16f8a6f9 | 1138 | int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
020df079 | 1139 | { |
ea740059 MP |
1140 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1141 | ||
020df079 GN |
1142 | switch (dr) { |
1143 | case 0 ... 3: | |
ea740059 | 1144 | *val = vcpu->arch.db[array_index_nospec(dr, size)]; |
020df079 GN |
1145 | break; |
1146 | case 4: | |
020df079 GN |
1147 | /* fall through */ |
1148 | case 6: | |
5679b803 | 1149 | *val = vcpu->arch.dr6; |
020df079 GN |
1150 | break; |
1151 | case 5: | |
020df079 GN |
1152 | /* fall through */ |
1153 | default: /* 7 */ | |
1154 | *val = vcpu->arch.dr7; | |
1155 | break; | |
1156 | } | |
338dbc97 GN |
1157 | return 0; |
1158 | } | |
020df079 GN |
1159 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
1160 | ||
022cd0e8 AK |
1161 | bool kvm_rdpmc(struct kvm_vcpu *vcpu) |
1162 | { | |
de3cd117 | 1163 | u32 ecx = kvm_rcx_read(vcpu); |
022cd0e8 AK |
1164 | u64 data; |
1165 | int err; | |
1166 | ||
c6702c9d | 1167 | err = kvm_pmu_rdpmc(vcpu, ecx, &data); |
022cd0e8 AK |
1168 | if (err) |
1169 | return err; | |
de3cd117 SC |
1170 | kvm_rax_write(vcpu, (u32)data); |
1171 | kvm_rdx_write(vcpu, data >> 32); | |
022cd0e8 AK |
1172 | return err; |
1173 | } | |
1174 | EXPORT_SYMBOL_GPL(kvm_rdpmc); | |
1175 | ||
043405e1 CO |
1176 | /* |
1177 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
1178 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
1179 | * | |
7a5ee6ed CQ |
1180 | * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) |
1181 | * extract the supported MSRs from the related const lists. | |
1182 | * msrs_to_save is selected from the msrs_to_save_all to reflect the | |
e3267cbb | 1183 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
7a5ee6ed | 1184 | * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs |
62ef68bb | 1185 | * may depend on host virtualization features rather than host cpu features. |
043405e1 | 1186 | */ |
e3267cbb | 1187 | |
7a5ee6ed | 1188 | static const u32 msrs_to_save_all[] = { |
043405e1 | 1189 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
8c06585d | 1190 | MSR_STAR, |
043405e1 CO |
1191 | #ifdef CONFIG_X86_64 |
1192 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
1193 | #endif | |
b3897a49 | 1194 | MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, |
32ad73db | 1195 | MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, |
2bdb76c0 | 1196 | MSR_IA32_SPEC_CTRL, |
bf8c55d8 CP |
1197 | MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH, |
1198 | MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK, | |
1199 | MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B, | |
1200 | MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, | |
1201 | MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, | |
1202 | MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, | |
6e3ba4ab TX |
1203 | MSR_IA32_UMWAIT_CONTROL, |
1204 | ||
e2ada66e JM |
1205 | MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, |
1206 | MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3, | |
1207 | MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, | |
1208 | MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, | |
1209 | MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1, | |
1210 | MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3, | |
1211 | MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5, | |
1212 | MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7, | |
1213 | MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9, | |
1214 | MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11, | |
1215 | MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13, | |
1216 | MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15, | |
1217 | MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17, | |
e2ada66e JM |
1218 | MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1, |
1219 | MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3, | |
1220 | MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5, | |
1221 | MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7, | |
1222 | MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9, | |
1223 | MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11, | |
1224 | MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13, | |
1225 | MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15, | |
1226 | MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17, | |
043405e1 CO |
1227 | }; |
1228 | ||
7a5ee6ed | 1229 | static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; |
043405e1 CO |
1230 | static unsigned num_msrs_to_save; |
1231 | ||
7a5ee6ed | 1232 | static const u32 emulated_msrs_all[] = { |
62ef68bb PB |
1233 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
1234 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, | |
1235 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, | |
1236 | HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC, | |
72c139ba | 1237 | HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY, |
e7d9513b AS |
1238 | HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2, |
1239 | HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL, | |
e516cebb | 1240 | HV_X64_MSR_RESET, |
11c4b1ca | 1241 | HV_X64_MSR_VP_INDEX, |
9eec50b8 | 1242 | HV_X64_MSR_VP_RUNTIME, |
5c919412 | 1243 | HV_X64_MSR_SCONTROL, |
1f4b34f8 | 1244 | HV_X64_MSR_STIMER0_CONFIG, |
d4abc577 | 1245 | HV_X64_MSR_VP_ASSIST_PAGE, |
a2e164e7 VK |
1246 | HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL, |
1247 | HV_X64_MSR_TSC_EMULATION_STATUS, | |
f97f5a56 JD |
1248 | HV_X64_MSR_SYNDBG_OPTIONS, |
1249 | HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS, | |
1250 | HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER, | |
1251 | HV_X64_MSR_SYNDBG_PENDING_BUFFER, | |
a2e164e7 VK |
1252 | |
1253 | MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, | |
557a961a | 1254 | MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, |
62ef68bb | 1255 | |
ba904635 | 1256 | MSR_IA32_TSC_ADJUST, |
a3e06bbe | 1257 | MSR_IA32_TSCDEADLINE, |
2bdb76c0 | 1258 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1259 | MSR_IA32_PERF_CAPABILITIES, |
043405e1 | 1260 | MSR_IA32_MISC_ENABLE, |
908e75f3 AK |
1261 | MSR_IA32_MCG_STATUS, |
1262 | MSR_IA32_MCG_CTL, | |
c45dcc71 | 1263 | MSR_IA32_MCG_EXT_CTL, |
64d60670 | 1264 | MSR_IA32_SMBASE, |
52797bf9 | 1265 | MSR_SMI_COUNT, |
db2336a8 KH |
1266 | MSR_PLATFORM_INFO, |
1267 | MSR_MISC_FEATURES_ENABLES, | |
bc226f07 | 1268 | MSR_AMD64_VIRT_SPEC_CTRL, |
6c6a2ab9 | 1269 | MSR_IA32_POWER_CTL, |
99634e3e | 1270 | MSR_IA32_UCODE_REV, |
191c8137 | 1271 | |
95c5c7c7 PB |
1272 | /* |
1273 | * The following list leaves out MSRs whose values are determined | |
1274 | * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. | |
1275 | * We always support the "true" VMX control MSRs, even if the host | |
1276 | * processor does not, so I am putting these registers here rather | |
7a5ee6ed | 1277 | * than in msrs_to_save_all. |
95c5c7c7 PB |
1278 | */ |
1279 | MSR_IA32_VMX_BASIC, | |
1280 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1281 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1282 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1283 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1284 | MSR_IA32_VMX_MISC, | |
1285 | MSR_IA32_VMX_CR0_FIXED0, | |
1286 | MSR_IA32_VMX_CR4_FIXED0, | |
1287 | MSR_IA32_VMX_VMCS_ENUM, | |
1288 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1289 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1290 | MSR_IA32_VMX_VMFUNC, | |
1291 | ||
191c8137 | 1292 | MSR_K7_HWCR, |
2d5ba19b | 1293 | MSR_KVM_POLL_CONTROL, |
043405e1 CO |
1294 | }; |
1295 | ||
7a5ee6ed | 1296 | static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)]; |
62ef68bb PB |
1297 | static unsigned num_emulated_msrs; |
1298 | ||
801e459a TL |
1299 | /* |
1300 | * List of msr numbers which are used to expose MSR-based features that | |
1301 | * can be used by a hypervisor to validate requested CPU features. | |
1302 | */ | |
7a5ee6ed | 1303 | static const u32 msr_based_features_all[] = { |
1389309c PB |
1304 | MSR_IA32_VMX_BASIC, |
1305 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1306 | MSR_IA32_VMX_PINBASED_CTLS, | |
1307 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1308 | MSR_IA32_VMX_PROCBASED_CTLS, | |
1309 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1310 | MSR_IA32_VMX_EXIT_CTLS, | |
1311 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1312 | MSR_IA32_VMX_ENTRY_CTLS, | |
1313 | MSR_IA32_VMX_MISC, | |
1314 | MSR_IA32_VMX_CR0_FIXED0, | |
1315 | MSR_IA32_VMX_CR0_FIXED1, | |
1316 | MSR_IA32_VMX_CR4_FIXED0, | |
1317 | MSR_IA32_VMX_CR4_FIXED1, | |
1318 | MSR_IA32_VMX_VMCS_ENUM, | |
1319 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1320 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1321 | MSR_IA32_VMX_VMFUNC, | |
1322 | ||
d1d93fa9 | 1323 | MSR_F10H_DECFG, |
518e7b94 | 1324 | MSR_IA32_UCODE_REV, |
cd283252 | 1325 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1326 | MSR_IA32_PERF_CAPABILITIES, |
801e459a TL |
1327 | }; |
1328 | ||
7a5ee6ed | 1329 | static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; |
801e459a TL |
1330 | static unsigned int num_msr_based_features; |
1331 | ||
4d22c17c | 1332 | static u64 kvm_get_arch_capabilities(void) |
5b76a3cf | 1333 | { |
4d22c17c | 1334 | u64 data = 0; |
5b76a3cf | 1335 | |
4d22c17c XL |
1336 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
1337 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data); | |
5b76a3cf | 1338 | |
b8e8c830 PB |
1339 | /* |
1340 | * If nx_huge_pages is enabled, KVM's shadow paging will ensure that | |
1341 | * the nested hypervisor runs with NX huge pages. If it is not, | |
1342 | * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other | |
1343 | * L1 guests, so it need not worry about its own (L2) guests. | |
1344 | */ | |
1345 | data |= ARCH_CAP_PSCHANGE_MC_NO; | |
1346 | ||
5b76a3cf PB |
1347 | /* |
1348 | * If we're doing cache flushes (either "always" or "cond") | |
1349 | * we will do one whenever the guest does a vmlaunch/vmresume. | |
1350 | * If an outer hypervisor is doing the cache flush for us | |
1351 | * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that | |
1352 | * capability to the guest too, and if EPT is disabled we're not | |
1353 | * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will | |
1354 | * require a nested hypervisor to do a flush of its own. | |
1355 | */ | |
1356 | if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER) | |
1357 | data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH; | |
1358 | ||
0c54914d PB |
1359 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
1360 | data |= ARCH_CAP_RDCL_NO; | |
1361 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
1362 | data |= ARCH_CAP_SSB_NO; | |
1363 | if (!boot_cpu_has_bug(X86_BUG_MDS)) | |
1364 | data |= ARCH_CAP_MDS_NO; | |
1365 | ||
e1d38b63 | 1366 | /* |
c11f83e0 PB |
1367 | * On TAA affected systems: |
1368 | * - nothing to do if TSX is disabled on the host. | |
1369 | * - we emulate TSX_CTRL if present on the host. | |
1370 | * This lets the guest use VERW to clear CPU buffers. | |
e1d38b63 | 1371 | */ |
cbbaa272 | 1372 | if (!boot_cpu_has(X86_FEATURE_RTM)) |
c11f83e0 | 1373 | data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR); |
cbbaa272 PB |
1374 | else if (!boot_cpu_has_bug(X86_BUG_TAA)) |
1375 | data |= ARCH_CAP_TAA_NO; | |
e1d38b63 | 1376 | |
5b76a3cf PB |
1377 | return data; |
1378 | } | |
5b76a3cf | 1379 | |
66421c1e WL |
1380 | static int kvm_get_msr_feature(struct kvm_msr_entry *msr) |
1381 | { | |
1382 | switch (msr->index) { | |
cd283252 | 1383 | case MSR_IA32_ARCH_CAPABILITIES: |
5b76a3cf PB |
1384 | msr->data = kvm_get_arch_capabilities(); |
1385 | break; | |
1386 | case MSR_IA32_UCODE_REV: | |
cd283252 | 1387 | rdmsrl_safe(msr->index, &msr->data); |
518e7b94 | 1388 | break; |
66421c1e | 1389 | default: |
afaf0b2f | 1390 | if (kvm_x86_ops.get_msr_feature(msr)) |
66421c1e WL |
1391 | return 1; |
1392 | } | |
1393 | return 0; | |
1394 | } | |
1395 | ||
801e459a TL |
1396 | static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
1397 | { | |
1398 | struct kvm_msr_entry msr; | |
66421c1e | 1399 | int r; |
801e459a TL |
1400 | |
1401 | msr.index = index; | |
66421c1e WL |
1402 | r = kvm_get_msr_feature(&msr); |
1403 | if (r) | |
1404 | return r; | |
801e459a TL |
1405 | |
1406 | *data = msr.data; | |
1407 | ||
1408 | return 0; | |
1409 | } | |
1410 | ||
11988499 | 1411 | static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
15c4a640 | 1412 | { |
1b4d56b8 | 1413 | if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT)) |
11988499 | 1414 | return false; |
1b2fd70c | 1415 | |
1b4d56b8 | 1416 | if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
11988499 | 1417 | return false; |
d8017474 | 1418 | |
0a629563 SC |
1419 | if (efer & (EFER_LME | EFER_LMA) && |
1420 | !guest_cpuid_has(vcpu, X86_FEATURE_LM)) | |
1421 | return false; | |
1422 | ||
1423 | if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX)) | |
1424 | return false; | |
d8017474 | 1425 | |
384bb783 | 1426 | return true; |
11988499 SC |
1427 | |
1428 | } | |
1429 | bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) | |
1430 | { | |
1431 | if (efer & efer_reserved_bits) | |
1432 | return false; | |
1433 | ||
1434 | return __kvm_valid_efer(vcpu, efer); | |
384bb783 JK |
1435 | } |
1436 | EXPORT_SYMBOL_GPL(kvm_valid_efer); | |
1437 | ||
11988499 | 1438 | static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
384bb783 JK |
1439 | { |
1440 | u64 old_efer = vcpu->arch.efer; | |
11988499 | 1441 | u64 efer = msr_info->data; |
384bb783 | 1442 | |
11988499 | 1443 | if (efer & efer_reserved_bits) |
66f61c92 | 1444 | return 1; |
384bb783 | 1445 | |
11988499 SC |
1446 | if (!msr_info->host_initiated) { |
1447 | if (!__kvm_valid_efer(vcpu, efer)) | |
1448 | return 1; | |
1449 | ||
1450 | if (is_paging(vcpu) && | |
1451 | (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) | |
1452 | return 1; | |
1453 | } | |
384bb783 | 1454 | |
15c4a640 | 1455 | efer &= ~EFER_LMA; |
f6801dff | 1456 | efer |= vcpu->arch.efer & EFER_LMA; |
15c4a640 | 1457 | |
afaf0b2f | 1458 | kvm_x86_ops.set_efer(vcpu, efer); |
a3d204e2 | 1459 | |
aad82703 SY |
1460 | /* Update reserved bits */ |
1461 | if ((efer ^ old_efer) & EFER_NX) | |
1462 | kvm_mmu_reset_context(vcpu); | |
1463 | ||
b69e8cae | 1464 | return 0; |
15c4a640 CO |
1465 | } |
1466 | ||
f2b4b7dd JR |
1467 | void kvm_enable_efer_bits(u64 mask) |
1468 | { | |
1469 | efer_reserved_bits &= ~mask; | |
1470 | } | |
1471 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); | |
1472 | ||
15c4a640 | 1473 | /* |
f20935d8 SC |
1474 | * Write @data into the MSR specified by @index. Select MSR specific fault |
1475 | * checks are bypassed if @host_initiated is %true. | |
15c4a640 CO |
1476 | * Returns 0 on success, non-0 otherwise. |
1477 | * Assumes vcpu_load() was already called. | |
1478 | */ | |
f20935d8 SC |
1479 | static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, |
1480 | bool host_initiated) | |
15c4a640 | 1481 | { |
f20935d8 SC |
1482 | struct msr_data msr; |
1483 | ||
1484 | switch (index) { | |
854e8bb1 NA |
1485 | case MSR_FS_BASE: |
1486 | case MSR_GS_BASE: | |
1487 | case MSR_KERNEL_GS_BASE: | |
1488 | case MSR_CSTAR: | |
1489 | case MSR_LSTAR: | |
f20935d8 | 1490 | if (is_noncanonical_address(data, vcpu)) |
854e8bb1 NA |
1491 | return 1; |
1492 | break; | |
1493 | case MSR_IA32_SYSENTER_EIP: | |
1494 | case MSR_IA32_SYSENTER_ESP: | |
1495 | /* | |
1496 | * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if | |
1497 | * non-canonical address is written on Intel but not on | |
1498 | * AMD (which ignores the top 32-bits, because it does | |
1499 | * not implement 64-bit SYSENTER). | |
1500 | * | |
1501 | * 64-bit code should hence be able to write a non-canonical | |
1502 | * value on AMD. Making the address canonical ensures that | |
1503 | * vmentry does not fail on Intel after writing a non-canonical | |
1504 | * value, and that something deterministic happens if the guest | |
1505 | * invokes 64-bit SYSENTER. | |
1506 | */ | |
f20935d8 | 1507 | data = get_canonical(data, vcpu_virt_addr_bits(vcpu)); |
854e8bb1 | 1508 | } |
f20935d8 SC |
1509 | |
1510 | msr.data = data; | |
1511 | msr.index = index; | |
1512 | msr.host_initiated = host_initiated; | |
1513 | ||
afaf0b2f | 1514 | return kvm_x86_ops.set_msr(vcpu, &msr); |
15c4a640 CO |
1515 | } |
1516 | ||
313a3dc7 | 1517 | /* |
f20935d8 SC |
1518 | * Read the MSR specified by @index into @data. Select MSR specific fault |
1519 | * checks are bypassed if @host_initiated is %true. | |
1520 | * Returns 0 on success, non-0 otherwise. | |
1521 | * Assumes vcpu_load() was already called. | |
313a3dc7 | 1522 | */ |
edef5c36 PB |
1523 | int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, |
1524 | bool host_initiated) | |
609e36d3 PB |
1525 | { |
1526 | struct msr_data msr; | |
f20935d8 | 1527 | int ret; |
609e36d3 PB |
1528 | |
1529 | msr.index = index; | |
f20935d8 | 1530 | msr.host_initiated = host_initiated; |
609e36d3 | 1531 | |
afaf0b2f | 1532 | ret = kvm_x86_ops.get_msr(vcpu, &msr); |
f20935d8 SC |
1533 | if (!ret) |
1534 | *data = msr.data; | |
1535 | return ret; | |
609e36d3 PB |
1536 | } |
1537 | ||
f20935d8 | 1538 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
313a3dc7 | 1539 | { |
f20935d8 SC |
1540 | return __kvm_get_msr(vcpu, index, data, false); |
1541 | } | |
1542 | EXPORT_SYMBOL_GPL(kvm_get_msr); | |
8fe8ab46 | 1543 | |
f20935d8 SC |
1544 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data) |
1545 | { | |
1546 | return __kvm_set_msr(vcpu, index, data, false); | |
1547 | } | |
1548 | EXPORT_SYMBOL_GPL(kvm_set_msr); | |
1549 | ||
1edce0a9 SC |
1550 | int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu) |
1551 | { | |
1552 | u32 ecx = kvm_rcx_read(vcpu); | |
1553 | u64 data; | |
1554 | ||
1555 | if (kvm_get_msr(vcpu, ecx, &data)) { | |
1556 | trace_kvm_msr_read_ex(ecx); | |
1557 | kvm_inject_gp(vcpu, 0); | |
1558 | return 1; | |
1559 | } | |
1560 | ||
1561 | trace_kvm_msr_read(ecx, data); | |
1562 | ||
1563 | kvm_rax_write(vcpu, data & -1u); | |
1564 | kvm_rdx_write(vcpu, (data >> 32) & -1u); | |
1565 | return kvm_skip_emulated_instruction(vcpu); | |
1566 | } | |
1567 | EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr); | |
1568 | ||
1569 | int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) | |
1570 | { | |
1571 | u32 ecx = kvm_rcx_read(vcpu); | |
1572 | u64 data = kvm_read_edx_eax(vcpu); | |
1573 | ||
1574 | if (kvm_set_msr(vcpu, ecx, data)) { | |
1575 | trace_kvm_msr_write_ex(ecx, data); | |
1576 | kvm_inject_gp(vcpu, 0); | |
1577 | return 1; | |
1578 | } | |
1579 | ||
1580 | trace_kvm_msr_write(ecx, data); | |
1581 | return kvm_skip_emulated_instruction(vcpu); | |
1582 | } | |
1583 | EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr); | |
1584 | ||
5a9f5443 WL |
1585 | bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu) |
1586 | { | |
1587 | return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || | |
1588 | need_resched() || signal_pending(current); | |
1589 | } | |
1590 | EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request); | |
1591 | ||
1e9e2622 WL |
1592 | /* |
1593 | * The fast path for frequent and performance sensitive wrmsr emulation, | |
1594 | * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces | |
1595 | * the latency of virtual IPI by avoiding the expensive bits of transitioning | |
1596 | * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the | |
1597 | * other cases which must be called after interrupts are enabled on the host. | |
1598 | */ | |
1599 | static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data) | |
1600 | { | |
e1be9ac8 WL |
1601 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) |
1602 | return 1; | |
1603 | ||
1604 | if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) && | |
1e9e2622 | 1605 | ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) && |
4064a4c6 WL |
1606 | ((data & APIC_MODE_MASK) == APIC_DM_FIXED) && |
1607 | ((u32)(data >> 32) != X2APIC_BROADCAST)) { | |
1e9e2622 | 1608 | |
d5361678 WL |
1609 | data &= ~(1 << 12); |
1610 | kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32)); | |
1e9e2622 | 1611 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32)); |
d5361678 WL |
1612 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data); |
1613 | trace_kvm_apic_write(APIC_ICR, (u32)data); | |
1614 | return 0; | |
1e9e2622 WL |
1615 | } |
1616 | ||
1617 | return 1; | |
1618 | } | |
1619 | ||
ae95f566 WL |
1620 | static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data) |
1621 | { | |
1622 | if (!kvm_can_use_hv_timer(vcpu)) | |
1623 | return 1; | |
1624 | ||
1625 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
1626 | return 0; | |
1627 | } | |
1628 | ||
404d5d7b | 1629 | fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) |
1e9e2622 WL |
1630 | { |
1631 | u32 msr = kvm_rcx_read(vcpu); | |
8a1038de | 1632 | u64 data; |
404d5d7b | 1633 | fastpath_t ret = EXIT_FASTPATH_NONE; |
1e9e2622 WL |
1634 | |
1635 | switch (msr) { | |
1636 | case APIC_BASE_MSR + (APIC_ICR >> 4): | |
8a1038de | 1637 | data = kvm_read_edx_eax(vcpu); |
404d5d7b WL |
1638 | if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { |
1639 | kvm_skip_emulated_instruction(vcpu); | |
1640 | ret = EXIT_FASTPATH_EXIT_HANDLED; | |
80bc97f2 | 1641 | } |
1e9e2622 | 1642 | break; |
ae95f566 WL |
1643 | case MSR_IA32_TSCDEADLINE: |
1644 | data = kvm_read_edx_eax(vcpu); | |
1645 | if (!handle_fastpath_set_tscdeadline(vcpu, data)) { | |
1646 | kvm_skip_emulated_instruction(vcpu); | |
1647 | ret = EXIT_FASTPATH_REENTER_GUEST; | |
1648 | } | |
1649 | break; | |
1e9e2622 | 1650 | default: |
404d5d7b | 1651 | break; |
1e9e2622 WL |
1652 | } |
1653 | ||
404d5d7b | 1654 | if (ret != EXIT_FASTPATH_NONE) |
1e9e2622 | 1655 | trace_kvm_msr_write(msr, data); |
1e9e2622 | 1656 | |
404d5d7b | 1657 | return ret; |
1e9e2622 WL |
1658 | } |
1659 | EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); | |
1660 | ||
f20935d8 SC |
1661 | /* |
1662 | * Adapt set_msr() to msr_io()'s calling convention | |
1663 | */ | |
1664 | static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1665 | { | |
1666 | return __kvm_get_msr(vcpu, index, data, true); | |
1667 | } | |
1668 | ||
1669 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1670 | { | |
1671 | return __kvm_set_msr(vcpu, index, *data, true); | |
313a3dc7 CO |
1672 | } |
1673 | ||
16e8d74d | 1674 | #ifdef CONFIG_X86_64 |
53fafdbb MT |
1675 | struct pvclock_clock { |
1676 | int vclock_mode; | |
1677 | u64 cycle_last; | |
1678 | u64 mask; | |
1679 | u32 mult; | |
1680 | u32 shift; | |
917f9475 PB |
1681 | u64 base_cycles; |
1682 | u64 offset; | |
53fafdbb MT |
1683 | }; |
1684 | ||
16e8d74d MT |
1685 | struct pvclock_gtod_data { |
1686 | seqcount_t seq; | |
1687 | ||
53fafdbb MT |
1688 | struct pvclock_clock clock; /* extract of a clocksource struct */ |
1689 | struct pvclock_clock raw_clock; /* extract of a clocksource struct */ | |
16e8d74d | 1690 | |
917f9475 | 1691 | ktime_t offs_boot; |
55dd00a7 | 1692 | u64 wall_time_sec; |
16e8d74d MT |
1693 | }; |
1694 | ||
1695 | static struct pvclock_gtod_data pvclock_gtod_data; | |
1696 | ||
1697 | static void update_pvclock_gtod(struct timekeeper *tk) | |
1698 | { | |
1699 | struct pvclock_gtod_data *vdata = &pvclock_gtod_data; | |
1700 | ||
1701 | write_seqcount_begin(&vdata->seq); | |
1702 | ||
1703 | /* copy pvclock gtod data */ | |
b95a8a27 | 1704 | vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; |
876e7881 PZ |
1705 | vdata->clock.cycle_last = tk->tkr_mono.cycle_last; |
1706 | vdata->clock.mask = tk->tkr_mono.mask; | |
1707 | vdata->clock.mult = tk->tkr_mono.mult; | |
1708 | vdata->clock.shift = tk->tkr_mono.shift; | |
917f9475 PB |
1709 | vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; |
1710 | vdata->clock.offset = tk->tkr_mono.base; | |
16e8d74d | 1711 | |
b95a8a27 | 1712 | vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; |
53fafdbb MT |
1713 | vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; |
1714 | vdata->raw_clock.mask = tk->tkr_raw.mask; | |
1715 | vdata->raw_clock.mult = tk->tkr_raw.mult; | |
1716 | vdata->raw_clock.shift = tk->tkr_raw.shift; | |
917f9475 PB |
1717 | vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; |
1718 | vdata->raw_clock.offset = tk->tkr_raw.base; | |
16e8d74d | 1719 | |
55dd00a7 MT |
1720 | vdata->wall_time_sec = tk->xtime_sec; |
1721 | ||
917f9475 | 1722 | vdata->offs_boot = tk->offs_boot; |
53fafdbb | 1723 | |
16e8d74d MT |
1724 | write_seqcount_end(&vdata->seq); |
1725 | } | |
8171cd68 PB |
1726 | |
1727 | static s64 get_kvmclock_base_ns(void) | |
1728 | { | |
1729 | /* Count up from boot time, but with the frequency of the raw clock. */ | |
1730 | return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); | |
1731 | } | |
1732 | #else | |
1733 | static s64 get_kvmclock_base_ns(void) | |
1734 | { | |
1735 | /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ | |
1736 | return ktime_get_boottime_ns(); | |
1737 | } | |
16e8d74d MT |
1738 | #endif |
1739 | ||
bab5bb39 NK |
1740 | void kvm_set_pending_timer(struct kvm_vcpu *vcpu) |
1741 | { | |
bab5bb39 | 1742 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
4d151bf3 | 1743 | kvm_vcpu_kick(vcpu); |
bab5bb39 | 1744 | } |
16e8d74d | 1745 | |
18068523 GOC |
1746 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
1747 | { | |
9ed3c444 AK |
1748 | int version; |
1749 | int r; | |
50d0a0f9 | 1750 | struct pvclock_wall_clock wc; |
8171cd68 | 1751 | u64 wall_nsec; |
18068523 GOC |
1752 | |
1753 | if (!wall_clock) | |
1754 | return; | |
1755 | ||
9ed3c444 AK |
1756 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
1757 | if (r) | |
1758 | return; | |
1759 | ||
1760 | if (version & 1) | |
1761 | ++version; /* first time write, random junk */ | |
1762 | ||
1763 | ++version; | |
18068523 | 1764 | |
1dab1345 NK |
1765 | if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version))) |
1766 | return; | |
18068523 | 1767 | |
50d0a0f9 GH |
1768 | /* |
1769 | * The guest calculates current wall clock time by adding | |
34c238a1 | 1770 | * system time (updated by kvm_guest_time_update below) to the |
8171cd68 | 1771 | * wall clock specified here. We do the reverse here. |
50d0a0f9 | 1772 | */ |
8171cd68 | 1773 | wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); |
50d0a0f9 | 1774 | |
8171cd68 PB |
1775 | wc.nsec = do_div(wall_nsec, 1000000000); |
1776 | wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ | |
50d0a0f9 | 1777 | wc.version = version; |
18068523 GOC |
1778 | |
1779 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); | |
1780 | ||
1781 | version++; | |
1782 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); | |
18068523 GOC |
1783 | } |
1784 | ||
50d0a0f9 GH |
1785 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
1786 | { | |
b51012de PB |
1787 | do_shl32_div32(dividend, divisor); |
1788 | return dividend; | |
50d0a0f9 GH |
1789 | } |
1790 | ||
3ae13faa | 1791 | static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, |
5f4e3f88 | 1792 | s8 *pshift, u32 *pmultiplier) |
50d0a0f9 | 1793 | { |
5f4e3f88 | 1794 | uint64_t scaled64; |
50d0a0f9 GH |
1795 | int32_t shift = 0; |
1796 | uint64_t tps64; | |
1797 | uint32_t tps32; | |
1798 | ||
3ae13faa PB |
1799 | tps64 = base_hz; |
1800 | scaled64 = scaled_hz; | |
50933623 | 1801 | while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { |
50d0a0f9 GH |
1802 | tps64 >>= 1; |
1803 | shift--; | |
1804 | } | |
1805 | ||
1806 | tps32 = (uint32_t)tps64; | |
50933623 JK |
1807 | while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) { |
1808 | if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) | |
5f4e3f88 ZA |
1809 | scaled64 >>= 1; |
1810 | else | |
1811 | tps32 <<= 1; | |
50d0a0f9 GH |
1812 | shift++; |
1813 | } | |
1814 | ||
5f4e3f88 ZA |
1815 | *pshift = shift; |
1816 | *pmultiplier = div_frac(scaled64, tps32); | |
50d0a0f9 GH |
1817 | } |
1818 | ||
d828199e | 1819 | #ifdef CONFIG_X86_64 |
16e8d74d | 1820 | static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); |
d828199e | 1821 | #endif |
16e8d74d | 1822 | |
c8076604 | 1823 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
69b0049a | 1824 | static unsigned long max_tsc_khz; |
c8076604 | 1825 | |
cc578287 | 1826 | static u32 adjust_tsc_khz(u32 khz, s32 ppm) |
1e993611 | 1827 | { |
cc578287 ZA |
1828 | u64 v = (u64)khz * (1000000 + ppm); |
1829 | do_div(v, 1000000); | |
1830 | return v; | |
1e993611 JR |
1831 | } |
1832 | ||
381d585c HZ |
1833 | static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
1834 | { | |
1835 | u64 ratio; | |
1836 | ||
1837 | /* Guest TSC same frequency as host TSC? */ | |
1838 | if (!scale) { | |
1839 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; | |
1840 | return 0; | |
1841 | } | |
1842 | ||
1843 | /* TSC scaling supported? */ | |
1844 | if (!kvm_has_tsc_control) { | |
1845 | if (user_tsc_khz > tsc_khz) { | |
1846 | vcpu->arch.tsc_catchup = 1; | |
1847 | vcpu->arch.tsc_always_catchup = 1; | |
1848 | return 0; | |
1849 | } else { | |
3f16a5c3 | 1850 | pr_warn_ratelimited("user requested TSC rate below hardware speed\n"); |
381d585c HZ |
1851 | return -1; |
1852 | } | |
1853 | } | |
1854 | ||
1855 | /* TSC scaling required - calculate ratio */ | |
1856 | ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits, | |
1857 | user_tsc_khz, tsc_khz); | |
1858 | ||
1859 | if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) { | |
3f16a5c3 PB |
1860 | pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", |
1861 | user_tsc_khz); | |
381d585c HZ |
1862 | return -1; |
1863 | } | |
1864 | ||
1865 | vcpu->arch.tsc_scaling_ratio = ratio; | |
1866 | return 0; | |
1867 | } | |
1868 | ||
4941b8cb | 1869 | static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
759379dd | 1870 | { |
cc578287 ZA |
1871 | u32 thresh_lo, thresh_hi; |
1872 | int use_scaling = 0; | |
217fc9cf | 1873 | |
03ba32ca | 1874 | /* tsc_khz can be zero if TSC calibration fails */ |
4941b8cb | 1875 | if (user_tsc_khz == 0) { |
ad721883 HZ |
1876 | /* set tsc_scaling_ratio to a safe value */ |
1877 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; | |
381d585c | 1878 | return -1; |
ad721883 | 1879 | } |
03ba32ca | 1880 | |
c285545f | 1881 | /* Compute a scale to convert nanoseconds in TSC cycles */ |
3ae13faa | 1882 | kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC, |
cc578287 ZA |
1883 | &vcpu->arch.virtual_tsc_shift, |
1884 | &vcpu->arch.virtual_tsc_mult); | |
4941b8cb | 1885 | vcpu->arch.virtual_tsc_khz = user_tsc_khz; |
cc578287 ZA |
1886 | |
1887 | /* | |
1888 | * Compute the variation in TSC rate which is acceptable | |
1889 | * within the range of tolerance and decide if the | |
1890 | * rate being applied is within that bounds of the hardware | |
1891 | * rate. If so, no scaling or compensation need be done. | |
1892 | */ | |
1893 | thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); | |
1894 | thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm); | |
4941b8cb PB |
1895 | if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) { |
1896 | pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi); | |
cc578287 ZA |
1897 | use_scaling = 1; |
1898 | } | |
4941b8cb | 1899 | return set_tsc_khz(vcpu, user_tsc_khz, use_scaling); |
c285545f ZA |
1900 | } |
1901 | ||
1902 | static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) | |
1903 | { | |
e26101b1 | 1904 | u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, |
cc578287 ZA |
1905 | vcpu->arch.virtual_tsc_mult, |
1906 | vcpu->arch.virtual_tsc_shift); | |
e26101b1 | 1907 | tsc += vcpu->arch.this_tsc_write; |
c285545f ZA |
1908 | return tsc; |
1909 | } | |
1910 | ||
b0c39dc6 VK |
1911 | static inline int gtod_is_based_on_tsc(int mode) |
1912 | { | |
b95a8a27 | 1913 | return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; |
b0c39dc6 VK |
1914 | } |
1915 | ||
69b0049a | 1916 | static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) |
b48aa97e MT |
1917 | { |
1918 | #ifdef CONFIG_X86_64 | |
1919 | bool vcpus_matched; | |
b48aa97e MT |
1920 | struct kvm_arch *ka = &vcpu->kvm->arch; |
1921 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
1922 | ||
1923 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
1924 | atomic_read(&vcpu->kvm->online_vcpus)); | |
1925 | ||
7f187922 MT |
1926 | /* |
1927 | * Once the masterclock is enabled, always perform request in | |
1928 | * order to update it. | |
1929 | * | |
1930 | * In order to enable masterclock, the host clocksource must be TSC | |
1931 | * and the vcpus need to have matched TSCs. When that happens, | |
1932 | * perform request to enable masterclock. | |
1933 | */ | |
1934 | if (ka->use_master_clock || | |
b0c39dc6 | 1935 | (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) |
b48aa97e MT |
1936 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
1937 | ||
1938 | trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, | |
1939 | atomic_read(&vcpu->kvm->online_vcpus), | |
1940 | ka->use_master_clock, gtod->clock.vclock_mode); | |
1941 | #endif | |
1942 | } | |
1943 | ||
ba904635 WA |
1944 | static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset) |
1945 | { | |
56ba77a4 | 1946 | u64 curr_offset = vcpu->arch.l1_tsc_offset; |
ba904635 WA |
1947 | vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset; |
1948 | } | |
1949 | ||
35181e86 HZ |
1950 | /* |
1951 | * Multiply tsc by a fixed point number represented by ratio. | |
1952 | * | |
1953 | * The most significant 64-N bits (mult) of ratio represent the | |
1954 | * integral part of the fixed point number; the remaining N bits | |
1955 | * (frac) represent the fractional part, ie. ratio represents a fixed | |
1956 | * point number (mult + frac * 2^(-N)). | |
1957 | * | |
1958 | * N equals to kvm_tsc_scaling_ratio_frac_bits. | |
1959 | */ | |
1960 | static inline u64 __scale_tsc(u64 ratio, u64 tsc) | |
1961 | { | |
1962 | return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits); | |
1963 | } | |
1964 | ||
1965 | u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) | |
1966 | { | |
1967 | u64 _tsc = tsc; | |
1968 | u64 ratio = vcpu->arch.tsc_scaling_ratio; | |
1969 | ||
1970 | if (ratio != kvm_default_tsc_scaling_ratio) | |
1971 | _tsc = __scale_tsc(ratio, tsc); | |
1972 | ||
1973 | return _tsc; | |
1974 | } | |
1975 | EXPORT_SYMBOL_GPL(kvm_scale_tsc); | |
1976 | ||
07c1419a HZ |
1977 | static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
1978 | { | |
1979 | u64 tsc; | |
1980 | ||
1981 | tsc = kvm_scale_tsc(vcpu, rdtsc()); | |
1982 | ||
1983 | return target_tsc - tsc; | |
1984 | } | |
1985 | ||
4ba76538 HZ |
1986 | u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
1987 | { | |
56ba77a4 | 1988 | return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc); |
4ba76538 HZ |
1989 | } |
1990 | EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); | |
1991 | ||
a545ab6a LC |
1992 | static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
1993 | { | |
56ba77a4 | 1994 | vcpu->arch.l1_tsc_offset = offset; |
afaf0b2f | 1995 | vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset); |
a545ab6a LC |
1996 | } |
1997 | ||
b0c39dc6 VK |
1998 | static inline bool kvm_check_tsc_unstable(void) |
1999 | { | |
2000 | #ifdef CONFIG_X86_64 | |
2001 | /* | |
2002 | * TSC is marked unstable when we're running on Hyper-V, | |
2003 | * 'TSC page' clocksource is good. | |
2004 | */ | |
b95a8a27 | 2005 | if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK) |
b0c39dc6 VK |
2006 | return false; |
2007 | #endif | |
2008 | return check_tsc_unstable(); | |
2009 | } | |
2010 | ||
8fe8ab46 | 2011 | void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) |
99e3e30a ZA |
2012 | { |
2013 | struct kvm *kvm = vcpu->kvm; | |
f38e098f | 2014 | u64 offset, ns, elapsed; |
99e3e30a | 2015 | unsigned long flags; |
b48aa97e | 2016 | bool matched; |
0d3da0d2 | 2017 | bool already_matched; |
8fe8ab46 | 2018 | u64 data = msr->data; |
c5e8ec8e | 2019 | bool synchronizing = false; |
99e3e30a | 2020 | |
038f8c11 | 2021 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
07c1419a | 2022 | offset = kvm_compute_tsc_offset(vcpu, data); |
8171cd68 | 2023 | ns = get_kvmclock_base_ns(); |
f38e098f | 2024 | elapsed = ns - kvm->arch.last_tsc_nsec; |
5d3cb0f6 | 2025 | |
03ba32ca | 2026 | if (vcpu->arch.virtual_tsc_khz) { |
bd8fab39 DP |
2027 | if (data == 0 && msr->host_initiated) { |
2028 | /* | |
2029 | * detection of vcpu initialization -- need to sync | |
2030 | * with other vCPUs. This particularly helps to keep | |
2031 | * kvm_clock stable after CPU hotplug | |
2032 | */ | |
2033 | synchronizing = true; | |
2034 | } else { | |
2035 | u64 tsc_exp = kvm->arch.last_tsc_write + | |
2036 | nsec_to_cycles(vcpu, elapsed); | |
2037 | u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; | |
2038 | /* | |
2039 | * Special case: TSC write with a small delta (1 second) | |
2040 | * of virtual cycle time against real time is | |
2041 | * interpreted as an attempt to synchronize the CPU. | |
2042 | */ | |
2043 | synchronizing = data < tsc_exp + tsc_hz && | |
2044 | data + tsc_hz > tsc_exp; | |
2045 | } | |
c5e8ec8e | 2046 | } |
f38e098f ZA |
2047 | |
2048 | /* | |
5d3cb0f6 ZA |
2049 | * For a reliable TSC, we can match TSC offsets, and for an unstable |
2050 | * TSC, we add elapsed time in this computation. We could let the | |
2051 | * compensation code attempt to catch up if we fall behind, but | |
2052 | * it's better to try to match offsets from the beginning. | |
2053 | */ | |
c5e8ec8e | 2054 | if (synchronizing && |
5d3cb0f6 | 2055 | vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { |
b0c39dc6 | 2056 | if (!kvm_check_tsc_unstable()) { |
e26101b1 | 2057 | offset = kvm->arch.cur_tsc_offset; |
f38e098f | 2058 | } else { |
857e4099 | 2059 | u64 delta = nsec_to_cycles(vcpu, elapsed); |
5d3cb0f6 | 2060 | data += delta; |
07c1419a | 2061 | offset = kvm_compute_tsc_offset(vcpu, data); |
f38e098f | 2062 | } |
b48aa97e | 2063 | matched = true; |
0d3da0d2 | 2064 | already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation); |
e26101b1 ZA |
2065 | } else { |
2066 | /* | |
2067 | * We split periods of matched TSC writes into generations. | |
2068 | * For each generation, we track the original measured | |
2069 | * nanosecond time, offset, and write, so if TSCs are in | |
2070 | * sync, we can match exact offset, and if not, we can match | |
4a969980 | 2071 | * exact software computation in compute_guest_tsc() |
e26101b1 ZA |
2072 | * |
2073 | * These values are tracked in kvm->arch.cur_xxx variables. | |
2074 | */ | |
2075 | kvm->arch.cur_tsc_generation++; | |
2076 | kvm->arch.cur_tsc_nsec = ns; | |
2077 | kvm->arch.cur_tsc_write = data; | |
2078 | kvm->arch.cur_tsc_offset = offset; | |
b48aa97e | 2079 | matched = false; |
f38e098f | 2080 | } |
e26101b1 ZA |
2081 | |
2082 | /* | |
2083 | * We also track th most recent recorded KHZ, write and time to | |
2084 | * allow the matching interval to be extended at each write. | |
2085 | */ | |
f38e098f ZA |
2086 | kvm->arch.last_tsc_nsec = ns; |
2087 | kvm->arch.last_tsc_write = data; | |
5d3cb0f6 | 2088 | kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; |
99e3e30a | 2089 | |
b183aa58 | 2090 | vcpu->arch.last_guest_tsc = data; |
e26101b1 ZA |
2091 | |
2092 | /* Keep track of which generation this VCPU has synchronized to */ | |
2093 | vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; | |
2094 | vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; | |
2095 | vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; | |
2096 | ||
d6321d49 | 2097 | if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) |
ba904635 | 2098 | update_ia32_tsc_adjust_msr(vcpu, offset); |
d6321d49 | 2099 | |
a545ab6a | 2100 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
e26101b1 | 2101 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
b48aa97e MT |
2102 | |
2103 | spin_lock(&kvm->arch.pvclock_gtod_sync_lock); | |
0d3da0d2 | 2104 | if (!matched) { |
b48aa97e | 2105 | kvm->arch.nr_vcpus_matched_tsc = 0; |
0d3da0d2 TG |
2106 | } else if (!already_matched) { |
2107 | kvm->arch.nr_vcpus_matched_tsc++; | |
2108 | } | |
b48aa97e MT |
2109 | |
2110 | kvm_track_tsc_matching(vcpu); | |
2111 | spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); | |
99e3e30a | 2112 | } |
e26101b1 | 2113 | |
99e3e30a ZA |
2114 | EXPORT_SYMBOL_GPL(kvm_write_tsc); |
2115 | ||
58ea6767 HZ |
2116 | static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, |
2117 | s64 adjustment) | |
2118 | { | |
56ba77a4 | 2119 | u64 tsc_offset = vcpu->arch.l1_tsc_offset; |
326e7425 | 2120 | kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment); |
58ea6767 HZ |
2121 | } |
2122 | ||
2123 | static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) | |
2124 | { | |
2125 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) | |
2126 | WARN_ON(adjustment < 0); | |
2127 | adjustment = kvm_scale_tsc(vcpu, (u64) adjustment); | |
ea26e4ec | 2128 | adjust_tsc_offset_guest(vcpu, adjustment); |
58ea6767 HZ |
2129 | } |
2130 | ||
d828199e MT |
2131 | #ifdef CONFIG_X86_64 |
2132 | ||
a5a1d1c2 | 2133 | static u64 read_tsc(void) |
d828199e | 2134 | { |
a5a1d1c2 | 2135 | u64 ret = (u64)rdtsc_ordered(); |
03b9730b | 2136 | u64 last = pvclock_gtod_data.clock.cycle_last; |
d828199e MT |
2137 | |
2138 | if (likely(ret >= last)) | |
2139 | return ret; | |
2140 | ||
2141 | /* | |
2142 | * GCC likes to generate cmov here, but this branch is extremely | |
6a6256f9 | 2143 | * predictable (it's just a function of time and the likely is |
d828199e MT |
2144 | * very likely) and there's a data dependence, so force GCC |
2145 | * to generate a branch instead. I don't barrier() because | |
2146 | * we don't actually need a barrier, and if this function | |
2147 | * ever gets inlined it will generate worse code. | |
2148 | */ | |
2149 | asm volatile (""); | |
2150 | return last; | |
2151 | } | |
2152 | ||
53fafdbb MT |
2153 | static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, |
2154 | int *mode) | |
d828199e MT |
2155 | { |
2156 | long v; | |
b0c39dc6 VK |
2157 | u64 tsc_pg_val; |
2158 | ||
53fafdbb | 2159 | switch (clock->vclock_mode) { |
b95a8a27 | 2160 | case VDSO_CLOCKMODE_HVCLOCK: |
b0c39dc6 VK |
2161 | tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(), |
2162 | tsc_timestamp); | |
2163 | if (tsc_pg_val != U64_MAX) { | |
2164 | /* TSC page valid */ | |
b95a8a27 | 2165 | *mode = VDSO_CLOCKMODE_HVCLOCK; |
53fafdbb MT |
2166 | v = (tsc_pg_val - clock->cycle_last) & |
2167 | clock->mask; | |
b0c39dc6 VK |
2168 | } else { |
2169 | /* TSC page invalid */ | |
b95a8a27 | 2170 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 VK |
2171 | } |
2172 | break; | |
b95a8a27 TG |
2173 | case VDSO_CLOCKMODE_TSC: |
2174 | *mode = VDSO_CLOCKMODE_TSC; | |
b0c39dc6 | 2175 | *tsc_timestamp = read_tsc(); |
53fafdbb MT |
2176 | v = (*tsc_timestamp - clock->cycle_last) & |
2177 | clock->mask; | |
b0c39dc6 VK |
2178 | break; |
2179 | default: | |
b95a8a27 | 2180 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 | 2181 | } |
d828199e | 2182 | |
b95a8a27 | 2183 | if (*mode == VDSO_CLOCKMODE_NONE) |
b0c39dc6 | 2184 | *tsc_timestamp = v = 0; |
d828199e | 2185 | |
53fafdbb | 2186 | return v * clock->mult; |
d828199e MT |
2187 | } |
2188 | ||
53fafdbb | 2189 | static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp) |
d828199e | 2190 | { |
cbcf2dd3 | 2191 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
d828199e | 2192 | unsigned long seq; |
d828199e | 2193 | int mode; |
cbcf2dd3 | 2194 | u64 ns; |
d828199e | 2195 | |
d828199e MT |
2196 | do { |
2197 | seq = read_seqcount_begin(>od->seq); | |
917f9475 | 2198 | ns = gtod->raw_clock.base_cycles; |
53fafdbb | 2199 | ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); |
917f9475 PB |
2200 | ns >>= gtod->raw_clock.shift; |
2201 | ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); | |
d828199e | 2202 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
cbcf2dd3 | 2203 | *t = ns; |
d828199e MT |
2204 | |
2205 | return mode; | |
2206 | } | |
2207 | ||
899a31f5 | 2208 | static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) |
55dd00a7 MT |
2209 | { |
2210 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
2211 | unsigned long seq; | |
2212 | int mode; | |
2213 | u64 ns; | |
2214 | ||
2215 | do { | |
2216 | seq = read_seqcount_begin(>od->seq); | |
55dd00a7 | 2217 | ts->tv_sec = gtod->wall_time_sec; |
917f9475 | 2218 | ns = gtod->clock.base_cycles; |
53fafdbb | 2219 | ns += vgettsc(>od->clock, tsc_timestamp, &mode); |
55dd00a7 MT |
2220 | ns >>= gtod->clock.shift; |
2221 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); | |
2222 | ||
2223 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); | |
2224 | ts->tv_nsec = ns; | |
2225 | ||
2226 | return mode; | |
2227 | } | |
2228 | ||
b0c39dc6 VK |
2229 | /* returns true if host is using TSC based clocksource */ |
2230 | static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) | |
d828199e | 2231 | { |
d828199e | 2232 | /* checked again under seqlock below */ |
b0c39dc6 | 2233 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
d828199e MT |
2234 | return false; |
2235 | ||
53fafdbb | 2236 | return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns, |
b0c39dc6 | 2237 | tsc_timestamp)); |
d828199e | 2238 | } |
55dd00a7 | 2239 | |
b0c39dc6 | 2240 | /* returns true if host is using TSC based clocksource */ |
899a31f5 | 2241 | static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, |
b0c39dc6 | 2242 | u64 *tsc_timestamp) |
55dd00a7 MT |
2243 | { |
2244 | /* checked again under seqlock below */ | |
b0c39dc6 | 2245 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
55dd00a7 MT |
2246 | return false; |
2247 | ||
b0c39dc6 | 2248 | return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); |
55dd00a7 | 2249 | } |
d828199e MT |
2250 | #endif |
2251 | ||
2252 | /* | |
2253 | * | |
b48aa97e MT |
2254 | * Assuming a stable TSC across physical CPUS, and a stable TSC |
2255 | * across virtual CPUs, the following condition is possible. | |
2256 | * Each numbered line represents an event visible to both | |
d828199e MT |
2257 | * CPUs at the next numbered event. |
2258 | * | |
2259 | * "timespecX" represents host monotonic time. "tscX" represents | |
2260 | * RDTSC value. | |
2261 | * | |
2262 | * VCPU0 on CPU0 | VCPU1 on CPU1 | |
2263 | * | |
2264 | * 1. read timespec0,tsc0 | |
2265 | * 2. | timespec1 = timespec0 + N | |
2266 | * | tsc1 = tsc0 + M | |
2267 | * 3. transition to guest | transition to guest | |
2268 | * 4. ret0 = timespec0 + (rdtsc - tsc0) | | |
2269 | * 5. | ret1 = timespec1 + (rdtsc - tsc1) | |
2270 | * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M)) | |
2271 | * | |
2272 | * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity: | |
2273 | * | |
2274 | * - ret0 < ret1 | |
2275 | * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M)) | |
2276 | * ... | |
2277 | * - 0 < N - M => M < N | |
2278 | * | |
2279 | * That is, when timespec0 != timespec1, M < N. Unfortunately that is not | |
2280 | * always the case (the difference between two distinct xtime instances | |
2281 | * might be smaller then the difference between corresponding TSC reads, | |
2282 | * when updating guest vcpus pvclock areas). | |
2283 | * | |
2284 | * To avoid that problem, do not allow visibility of distinct | |
2285 | * system_timestamp/tsc_timestamp values simultaneously: use a master | |
2286 | * copy of host monotonic time values. Update that master copy | |
2287 | * in lockstep. | |
2288 | * | |
b48aa97e | 2289 | * Rely on synchronization of host TSCs and guest TSCs for monotonicity. |
d828199e MT |
2290 | * |
2291 | */ | |
2292 | ||
2293 | static void pvclock_update_vm_gtod_copy(struct kvm *kvm) | |
2294 | { | |
2295 | #ifdef CONFIG_X86_64 | |
2296 | struct kvm_arch *ka = &kvm->arch; | |
2297 | int vclock_mode; | |
b48aa97e MT |
2298 | bool host_tsc_clocksource, vcpus_matched; |
2299 | ||
2300 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
2301 | atomic_read(&kvm->online_vcpus)); | |
d828199e MT |
2302 | |
2303 | /* | |
2304 | * If the host uses TSC clock, then passthrough TSC as stable | |
2305 | * to the guest. | |
2306 | */ | |
b48aa97e | 2307 | host_tsc_clocksource = kvm_get_time_and_clockread( |
d828199e MT |
2308 | &ka->master_kernel_ns, |
2309 | &ka->master_cycle_now); | |
2310 | ||
16a96021 | 2311 | ka->use_master_clock = host_tsc_clocksource && vcpus_matched |
a826faf1 | 2312 | && !ka->backwards_tsc_observed |
54750f2c | 2313 | && !ka->boot_vcpu_runs_old_kvmclock; |
b48aa97e | 2314 | |
d828199e MT |
2315 | if (ka->use_master_clock) |
2316 | atomic_set(&kvm_guest_has_master_clock, 1); | |
2317 | ||
2318 | vclock_mode = pvclock_gtod_data.clock.vclock_mode; | |
b48aa97e MT |
2319 | trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, |
2320 | vcpus_matched); | |
d828199e MT |
2321 | #endif |
2322 | } | |
2323 | ||
2860c4b1 PB |
2324 | void kvm_make_mclock_inprogress_request(struct kvm *kvm) |
2325 | { | |
2326 | kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); | |
2327 | } | |
2328 | ||
2e762ff7 MT |
2329 | static void kvm_gen_update_masterclock(struct kvm *kvm) |
2330 | { | |
2331 | #ifdef CONFIG_X86_64 | |
2332 | int i; | |
2333 | struct kvm_vcpu *vcpu; | |
2334 | struct kvm_arch *ka = &kvm->arch; | |
2335 | ||
2336 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
2337 | kvm_make_mclock_inprogress_request(kvm); | |
2338 | /* no guest entries from this point */ | |
2339 | pvclock_update_vm_gtod_copy(kvm); | |
2340 | ||
2341 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 2342 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
2e762ff7 MT |
2343 | |
2344 | /* guest entries allowed */ | |
2345 | kvm_for_each_vcpu(i, vcpu, kvm) | |
72875d8a | 2346 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
2e762ff7 MT |
2347 | |
2348 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
2349 | #endif | |
2350 | } | |
2351 | ||
e891a32e | 2352 | u64 get_kvmclock_ns(struct kvm *kvm) |
108b249c | 2353 | { |
108b249c | 2354 | struct kvm_arch *ka = &kvm->arch; |
8b953440 | 2355 | struct pvclock_vcpu_time_info hv_clock; |
e2c2206a | 2356 | u64 ret; |
108b249c | 2357 | |
8b953440 PB |
2358 | spin_lock(&ka->pvclock_gtod_sync_lock); |
2359 | if (!ka->use_master_clock) { | |
2360 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
8171cd68 | 2361 | return get_kvmclock_base_ns() + ka->kvmclock_offset; |
108b249c PB |
2362 | } |
2363 | ||
8b953440 PB |
2364 | hv_clock.tsc_timestamp = ka->master_cycle_now; |
2365 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; | |
2366 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
2367 | ||
e2c2206a WL |
2368 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ |
2369 | get_cpu(); | |
2370 | ||
e70b57a6 WL |
2371 | if (__this_cpu_read(cpu_tsc_khz)) { |
2372 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, | |
2373 | &hv_clock.tsc_shift, | |
2374 | &hv_clock.tsc_to_system_mul); | |
2375 | ret = __pvclock_read_cycles(&hv_clock, rdtsc()); | |
2376 | } else | |
8171cd68 | 2377 | ret = get_kvmclock_base_ns() + ka->kvmclock_offset; |
e2c2206a WL |
2378 | |
2379 | put_cpu(); | |
2380 | ||
2381 | return ret; | |
108b249c PB |
2382 | } |
2383 | ||
0d6dd2ff PB |
2384 | static void kvm_setup_pvclock_page(struct kvm_vcpu *v) |
2385 | { | |
2386 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
2387 | struct pvclock_vcpu_time_info guest_hv_clock; | |
2388 | ||
4e335d9e | 2389 | if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time, |
0d6dd2ff PB |
2390 | &guest_hv_clock, sizeof(guest_hv_clock)))) |
2391 | return; | |
2392 | ||
2393 | /* This VCPU is paused, but it's legal for a guest to read another | |
2394 | * VCPU's kvmclock, so we really have to follow the specification where | |
2395 | * it says that version is odd if data is being modified, and even after | |
2396 | * it is consistent. | |
2397 | * | |
2398 | * Version field updates must be kept separate. This is because | |
2399 | * kvm_write_guest_cached might use a "rep movs" instruction, and | |
2400 | * writes within a string instruction are weakly ordered. So there | |
2401 | * are three writes overall. | |
2402 | * | |
2403 | * As a small optimization, only write the version field in the first | |
2404 | * and third write. The vcpu->pv_time cache is still valid, because the | |
2405 | * version field is the first in the struct. | |
2406 | */ | |
2407 | BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0); | |
2408 | ||
51c4b8bb LA |
2409 | if (guest_hv_clock.version & 1) |
2410 | ++guest_hv_clock.version; /* first time write, random junk */ | |
2411 | ||
0d6dd2ff | 2412 | vcpu->hv_clock.version = guest_hv_clock.version + 1; |
4e335d9e PB |
2413 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
2414 | &vcpu->hv_clock, | |
2415 | sizeof(vcpu->hv_clock.version)); | |
0d6dd2ff PB |
2416 | |
2417 | smp_wmb(); | |
2418 | ||
2419 | /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ | |
2420 | vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED); | |
2421 | ||
2422 | if (vcpu->pvclock_set_guest_stopped_request) { | |
2423 | vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; | |
2424 | vcpu->pvclock_set_guest_stopped_request = false; | |
2425 | } | |
2426 | ||
2427 | trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); | |
2428 | ||
4e335d9e PB |
2429 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
2430 | &vcpu->hv_clock, | |
2431 | sizeof(vcpu->hv_clock)); | |
0d6dd2ff PB |
2432 | |
2433 | smp_wmb(); | |
2434 | ||
2435 | vcpu->hv_clock.version++; | |
4e335d9e PB |
2436 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
2437 | &vcpu->hv_clock, | |
2438 | sizeof(vcpu->hv_clock.version)); | |
0d6dd2ff PB |
2439 | } |
2440 | ||
34c238a1 | 2441 | static int kvm_guest_time_update(struct kvm_vcpu *v) |
18068523 | 2442 | { |
78db6a50 | 2443 | unsigned long flags, tgt_tsc_khz; |
18068523 | 2444 | struct kvm_vcpu_arch *vcpu = &v->arch; |
d828199e | 2445 | struct kvm_arch *ka = &v->kvm->arch; |
f25e656d | 2446 | s64 kernel_ns; |
d828199e | 2447 | u64 tsc_timestamp, host_tsc; |
51d59c6b | 2448 | u8 pvclock_flags; |
d828199e MT |
2449 | bool use_master_clock; |
2450 | ||
2451 | kernel_ns = 0; | |
2452 | host_tsc = 0; | |
18068523 | 2453 | |
d828199e MT |
2454 | /* |
2455 | * If the host uses TSC clock, then passthrough TSC as stable | |
2456 | * to the guest. | |
2457 | */ | |
2458 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
2459 | use_master_clock = ka->use_master_clock; | |
2460 | if (use_master_clock) { | |
2461 | host_tsc = ka->master_cycle_now; | |
2462 | kernel_ns = ka->master_kernel_ns; | |
2463 | } | |
2464 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
c09664bb MT |
2465 | |
2466 | /* Keep irq disabled to prevent changes to the clock */ | |
2467 | local_irq_save(flags); | |
78db6a50 PB |
2468 | tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz); |
2469 | if (unlikely(tgt_tsc_khz == 0)) { | |
c09664bb MT |
2470 | local_irq_restore(flags); |
2471 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); | |
2472 | return 1; | |
2473 | } | |
d828199e | 2474 | if (!use_master_clock) { |
4ea1636b | 2475 | host_tsc = rdtsc(); |
8171cd68 | 2476 | kernel_ns = get_kvmclock_base_ns(); |
d828199e MT |
2477 | } |
2478 | ||
4ba76538 | 2479 | tsc_timestamp = kvm_read_l1_tsc(v, host_tsc); |
d828199e | 2480 | |
c285545f ZA |
2481 | /* |
2482 | * We may have to catch up the TSC to match elapsed wall clock | |
2483 | * time for two reasons, even if kvmclock is used. | |
2484 | * 1) CPU could have been running below the maximum TSC rate | |
2485 | * 2) Broken TSC compensation resets the base at each VCPU | |
2486 | * entry to avoid unknown leaps of TSC even when running | |
2487 | * again on the same CPU. This may cause apparent elapsed | |
2488 | * time to disappear, and the guest to stand still or run | |
2489 | * very slowly. | |
2490 | */ | |
2491 | if (vcpu->tsc_catchup) { | |
2492 | u64 tsc = compute_guest_tsc(v, kernel_ns); | |
2493 | if (tsc > tsc_timestamp) { | |
f1e2b260 | 2494 | adjust_tsc_offset_guest(v, tsc - tsc_timestamp); |
c285545f ZA |
2495 | tsc_timestamp = tsc; |
2496 | } | |
50d0a0f9 GH |
2497 | } |
2498 | ||
18068523 GOC |
2499 | local_irq_restore(flags); |
2500 | ||
0d6dd2ff | 2501 | /* With all the info we got, fill in the values */ |
18068523 | 2502 | |
78db6a50 PB |
2503 | if (kvm_has_tsc_control) |
2504 | tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz); | |
2505 | ||
2506 | if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { | |
3ae13faa | 2507 | kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, |
5f4e3f88 ZA |
2508 | &vcpu->hv_clock.tsc_shift, |
2509 | &vcpu->hv_clock.tsc_to_system_mul); | |
78db6a50 | 2510 | vcpu->hw_tsc_khz = tgt_tsc_khz; |
8cfdc000 ZA |
2511 | } |
2512 | ||
1d5f066e | 2513 | vcpu->hv_clock.tsc_timestamp = tsc_timestamp; |
759379dd | 2514 | vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; |
28e4639a | 2515 | vcpu->last_guest_tsc = tsc_timestamp; |
51d59c6b | 2516 | |
d828199e | 2517 | /* If the host uses TSC clocksource, then it is stable */ |
0d6dd2ff | 2518 | pvclock_flags = 0; |
d828199e MT |
2519 | if (use_master_clock) |
2520 | pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; | |
2521 | ||
78c0337a MT |
2522 | vcpu->hv_clock.flags = pvclock_flags; |
2523 | ||
095cf55d PB |
2524 | if (vcpu->pv_time_enabled) |
2525 | kvm_setup_pvclock_page(v); | |
2526 | if (v == kvm_get_vcpu(v->kvm, 0)) | |
2527 | kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); | |
8cfdc000 | 2528 | return 0; |
c8076604 GH |
2529 | } |
2530 | ||
0061d53d MT |
2531 | /* |
2532 | * kvmclock updates which are isolated to a given vcpu, such as | |
2533 | * vcpu->cpu migration, should not allow system_timestamp from | |
2534 | * the rest of the vcpus to remain static. Otherwise ntp frequency | |
2535 | * correction applies to one vcpu's system_timestamp but not | |
2536 | * the others. | |
2537 | * | |
2538 | * So in those cases, request a kvmclock update for all vcpus. | |
7e44e449 AJ |
2539 | * We need to rate-limit these requests though, as they can |
2540 | * considerably slow guests that have a large number of vcpus. | |
2541 | * The time for a remote vcpu to update its kvmclock is bound | |
2542 | * by the delay we use to rate-limit the updates. | |
0061d53d MT |
2543 | */ |
2544 | ||
7e44e449 AJ |
2545 | #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100) |
2546 | ||
2547 | static void kvmclock_update_fn(struct work_struct *work) | |
0061d53d MT |
2548 | { |
2549 | int i; | |
7e44e449 AJ |
2550 | struct delayed_work *dwork = to_delayed_work(work); |
2551 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
2552 | kvmclock_update_work); | |
2553 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
0061d53d MT |
2554 | struct kvm_vcpu *vcpu; |
2555 | ||
2556 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
105b21bb | 2557 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0061d53d MT |
2558 | kvm_vcpu_kick(vcpu); |
2559 | } | |
2560 | } | |
2561 | ||
7e44e449 AJ |
2562 | static void kvm_gen_kvmclock_update(struct kvm_vcpu *v) |
2563 | { | |
2564 | struct kvm *kvm = v->kvm; | |
2565 | ||
105b21bb | 2566 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
7e44e449 AJ |
2567 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, |
2568 | KVMCLOCK_UPDATE_DELAY); | |
2569 | } | |
2570 | ||
332967a3 AJ |
2571 | #define KVMCLOCK_SYNC_PERIOD (300 * HZ) |
2572 | ||
2573 | static void kvmclock_sync_fn(struct work_struct *work) | |
2574 | { | |
2575 | struct delayed_work *dwork = to_delayed_work(work); | |
2576 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
2577 | kvmclock_sync_work); | |
2578 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
2579 | ||
630994b3 MT |
2580 | if (!kvmclock_periodic_sync) |
2581 | return; | |
2582 | ||
332967a3 AJ |
2583 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); |
2584 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
2585 | KVMCLOCK_SYNC_PERIOD); | |
2586 | } | |
2587 | ||
191c8137 BP |
2588 | /* |
2589 | * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP. | |
2590 | */ | |
2591 | static bool can_set_mci_status(struct kvm_vcpu *vcpu) | |
2592 | { | |
2593 | /* McStatusWrEn enabled? */ | |
23493d0a | 2594 | if (guest_cpuid_is_amd_or_hygon(vcpu)) |
191c8137 BP |
2595 | return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); |
2596 | ||
2597 | return false; | |
2598 | } | |
2599 | ||
9ffd986c | 2600 | static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 2601 | { |
890ca9ae HY |
2602 | u64 mcg_cap = vcpu->arch.mcg_cap; |
2603 | unsigned bank_num = mcg_cap & 0xff; | |
9ffd986c WL |
2604 | u32 msr = msr_info->index; |
2605 | u64 data = msr_info->data; | |
890ca9ae | 2606 | |
15c4a640 | 2607 | switch (msr) { |
15c4a640 | 2608 | case MSR_IA32_MCG_STATUS: |
890ca9ae | 2609 | vcpu->arch.mcg_status = data; |
15c4a640 | 2610 | break; |
c7ac679c | 2611 | case MSR_IA32_MCG_CTL: |
44883f01 PB |
2612 | if (!(mcg_cap & MCG_CTL_P) && |
2613 | (data || !msr_info->host_initiated)) | |
890ca9ae HY |
2614 | return 1; |
2615 | if (data != 0 && data != ~(u64)0) | |
44883f01 | 2616 | return 1; |
890ca9ae HY |
2617 | vcpu->arch.mcg_ctl = data; |
2618 | break; | |
2619 | default: | |
2620 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 2621 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
2622 | u32 offset = array_index_nospec( |
2623 | msr - MSR_IA32_MC0_CTL, | |
2624 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
2625 | ||
114be429 AP |
2626 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
2627 | * some Linux kernels though clear bit 10 in bank 4 to | |
2628 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore | |
2629 | * this to avoid an uncatched #GP in the guest | |
2630 | */ | |
890ca9ae | 2631 | if ((offset & 0x3) == 0 && |
114be429 | 2632 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
890ca9ae | 2633 | return -1; |
191c8137 BP |
2634 | |
2635 | /* MCi_STATUS */ | |
9ffd986c | 2636 | if (!msr_info->host_initiated && |
191c8137 BP |
2637 | (offset & 0x3) == 1 && data != 0) { |
2638 | if (!can_set_mci_status(vcpu)) | |
2639 | return -1; | |
2640 | } | |
2641 | ||
890ca9ae HY |
2642 | vcpu->arch.mce_banks[offset] = data; |
2643 | break; | |
2644 | } | |
2645 | return 1; | |
2646 | } | |
2647 | return 0; | |
2648 | } | |
2649 | ||
ffde22ac ES |
2650 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
2651 | { | |
2652 | struct kvm *kvm = vcpu->kvm; | |
2653 | int lm = is_long_mode(vcpu); | |
2654 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 | |
2655 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; | |
2656 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 | |
2657 | : kvm->arch.xen_hvm_config.blob_size_32; | |
2658 | u32 page_num = data & ~PAGE_MASK; | |
2659 | u64 page_addr = data & PAGE_MASK; | |
2660 | u8 *page; | |
2661 | int r; | |
2662 | ||
2663 | r = -E2BIG; | |
2664 | if (page_num >= blob_size) | |
2665 | goto out; | |
2666 | r = -ENOMEM; | |
ff5c2c03 SL |
2667 | page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE); |
2668 | if (IS_ERR(page)) { | |
2669 | r = PTR_ERR(page); | |
ffde22ac | 2670 | goto out; |
ff5c2c03 | 2671 | } |
54bf36aa | 2672 | if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) |
ffde22ac ES |
2673 | goto out_free; |
2674 | r = 0; | |
2675 | out_free: | |
2676 | kfree(page); | |
2677 | out: | |
2678 | return r; | |
2679 | } | |
2680 | ||
2635b5c4 VK |
2681 | static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) |
2682 | { | |
2683 | u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; | |
2684 | ||
2685 | return (vcpu->arch.apf.msr_en_val & mask) == mask; | |
2686 | } | |
2687 | ||
344d9588 GN |
2688 | static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) |
2689 | { | |
2690 | gpa_t gpa = data & ~0x3f; | |
2691 | ||
2635b5c4 VK |
2692 | /* Bits 4:5 are reserved, Should be zero */ |
2693 | if (data & 0x30) | |
344d9588 GN |
2694 | return 1; |
2695 | ||
2635b5c4 | 2696 | vcpu->arch.apf.msr_en_val = data; |
344d9588 | 2697 | |
2635b5c4 | 2698 | if (!kvm_pv_async_pf_enabled(vcpu)) { |
344d9588 GN |
2699 | kvm_clear_async_pf_completion_queue(vcpu); |
2700 | kvm_async_pf_hash_reset(vcpu); | |
2701 | return 0; | |
2702 | } | |
2703 | ||
4e335d9e | 2704 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, |
68fd66f1 | 2705 | sizeof(u64))) |
344d9588 GN |
2706 | return 1; |
2707 | ||
6adba527 | 2708 | vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); |
52a5c155 | 2709 | vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
2635b5c4 | 2710 | |
344d9588 | 2711 | kvm_async_pf_wakeup_all(vcpu); |
2635b5c4 VK |
2712 | |
2713 | return 0; | |
2714 | } | |
2715 | ||
2716 | static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data) | |
2717 | { | |
2718 | /* Bits 8-63 are reserved */ | |
2719 | if (data >> 8) | |
2720 | return 1; | |
2721 | ||
2722 | if (!lapic_in_kernel(vcpu)) | |
2723 | return 1; | |
2724 | ||
2725 | vcpu->arch.apf.msr_int_val = data; | |
2726 | ||
2727 | vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; | |
2728 | ||
344d9588 GN |
2729 | return 0; |
2730 | } | |
2731 | ||
12f9a48f GC |
2732 | static void kvmclock_reset(struct kvm_vcpu *vcpu) |
2733 | { | |
0b79459b | 2734 | vcpu->arch.pv_time_enabled = false; |
49dedf0d | 2735 | vcpu->arch.time = 0; |
12f9a48f GC |
2736 | } |
2737 | ||
7780938c | 2738 | static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) |
f38a7b75 WL |
2739 | { |
2740 | ++vcpu->stat.tlb_flush; | |
7780938c | 2741 | kvm_x86_ops.tlb_flush_all(vcpu); |
f38a7b75 WL |
2742 | } |
2743 | ||
0baedd79 VK |
2744 | static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) |
2745 | { | |
2746 | ++vcpu->stat.tlb_flush; | |
2747 | kvm_x86_ops.tlb_flush_guest(vcpu); | |
2748 | } | |
2749 | ||
c9aaa895 GC |
2750 | static void record_steal_time(struct kvm_vcpu *vcpu) |
2751 | { | |
b0431382 BO |
2752 | struct kvm_host_map map; |
2753 | struct kvm_steal_time *st; | |
2754 | ||
c9aaa895 GC |
2755 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
2756 | return; | |
2757 | ||
b0431382 BO |
2758 | /* -EAGAIN is returned in atomic context so we can just return. */ |
2759 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, | |
2760 | &map, &vcpu->arch.st.cache, false)) | |
c9aaa895 GC |
2761 | return; |
2762 | ||
b0431382 BO |
2763 | st = map.hva + |
2764 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); | |
2765 | ||
f38a7b75 WL |
2766 | /* |
2767 | * Doing a TLB flush here, on the guest's behalf, can avoid | |
2768 | * expensive IPIs. | |
2769 | */ | |
b382f44e | 2770 | trace_kvm_pv_tlb_flush(vcpu->vcpu_id, |
b0431382 BO |
2771 | st->preempted & KVM_VCPU_FLUSH_TLB); |
2772 | if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) | |
0baedd79 | 2773 | kvm_vcpu_flush_tlb_guest(vcpu); |
0b9f6c46 | 2774 | |
a6bd811f | 2775 | vcpu->arch.st.preempted = 0; |
35f3fae1 | 2776 | |
b0431382 BO |
2777 | if (st->version & 1) |
2778 | st->version += 1; /* first time write, random junk */ | |
35f3fae1 | 2779 | |
b0431382 | 2780 | st->version += 1; |
35f3fae1 WL |
2781 | |
2782 | smp_wmb(); | |
2783 | ||
b0431382 | 2784 | st->steal += current->sched_info.run_delay - |
c54cdf14 LC |
2785 | vcpu->arch.st.last_steal; |
2786 | vcpu->arch.st.last_steal = current->sched_info.run_delay; | |
35f3fae1 | 2787 | |
35f3fae1 WL |
2788 | smp_wmb(); |
2789 | ||
b0431382 | 2790 | st->version += 1; |
c9aaa895 | 2791 | |
b0431382 | 2792 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false); |
c9aaa895 GC |
2793 | } |
2794 | ||
8fe8ab46 | 2795 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 2796 | { |
5753785f | 2797 | bool pr = false; |
8fe8ab46 WA |
2798 | u32 msr = msr_info->index; |
2799 | u64 data = msr_info->data; | |
5753785f | 2800 | |
15c4a640 | 2801 | switch (msr) { |
2e32b719 | 2802 | case MSR_AMD64_NB_CFG: |
2e32b719 BP |
2803 | case MSR_IA32_UCODE_WRITE: |
2804 | case MSR_VM_HSAVE_PA: | |
2805 | case MSR_AMD64_PATCH_LOADER: | |
2806 | case MSR_AMD64_BU_CFG2: | |
405a353a | 2807 | case MSR_AMD64_DC_CFG: |
0e1b869f | 2808 | case MSR_F15H_EX_CFG: |
2e32b719 BP |
2809 | break; |
2810 | ||
518e7b94 WL |
2811 | case MSR_IA32_UCODE_REV: |
2812 | if (msr_info->host_initiated) | |
2813 | vcpu->arch.microcode_version = data; | |
2814 | break; | |
0cf9135b SC |
2815 | case MSR_IA32_ARCH_CAPABILITIES: |
2816 | if (!msr_info->host_initiated) | |
2817 | return 1; | |
2818 | vcpu->arch.arch_capabilities = data; | |
2819 | break; | |
15c4a640 | 2820 | case MSR_EFER: |
11988499 | 2821 | return set_efer(vcpu, msr_info); |
8f1589d9 AP |
2822 | case MSR_K7_HWCR: |
2823 | data &= ~(u64)0x40; /* ignore flush filter disable */ | |
82494028 | 2824 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
a223c313 | 2825 | data &= ~(u64)0x8; /* ignore TLB cache disable */ |
191c8137 BP |
2826 | |
2827 | /* Handle McStatusWrEn */ | |
2828 | if (data == BIT_ULL(18)) { | |
2829 | vcpu->arch.msr_hwcr = data; | |
2830 | } else if (data != 0) { | |
a737f256 CD |
2831 | vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
2832 | data); | |
8f1589d9 AP |
2833 | return 1; |
2834 | } | |
15c4a640 | 2835 | break; |
f7c6d140 AP |
2836 | case MSR_FAM10H_MMIO_CONF_BASE: |
2837 | if (data != 0) { | |
a737f256 CD |
2838 | vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
2839 | "0x%llx\n", data); | |
f7c6d140 AP |
2840 | return 1; |
2841 | } | |
15c4a640 | 2842 | break; |
b5e2fec0 AG |
2843 | case MSR_IA32_DEBUGCTLMSR: |
2844 | if (!data) { | |
2845 | /* We support the non-activated case already */ | |
2846 | break; | |
2847 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { | |
2848 | /* Values other than LBR and BTF are vendor-specific, | |
2849 | thus reserved and should throw a #GP */ | |
2850 | return 1; | |
2851 | } | |
a737f256 CD |
2852 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
2853 | __func__, data); | |
b5e2fec0 | 2854 | break; |
9ba075a6 | 2855 | case 0x200 ... 0x2ff: |
ff53604b | 2856 | return kvm_mtrr_set_msr(vcpu, msr, data); |
15c4a640 | 2857 | case MSR_IA32_APICBASE: |
58cb628d | 2858 | return kvm_set_apic_base(vcpu, msr_info); |
0105d1a5 GN |
2859 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
2860 | return kvm_x2apic_msr_write(vcpu, msr, data); | |
a3e06bbe LJ |
2861 | case MSR_IA32_TSCDEADLINE: |
2862 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
2863 | break; | |
ba904635 | 2864 | case MSR_IA32_TSC_ADJUST: |
d6321d49 | 2865 | if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) { |
ba904635 | 2866 | if (!msr_info->host_initiated) { |
d913b904 | 2867 | s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; |
d7add054 | 2868 | adjust_tsc_offset_guest(vcpu, adj); |
ba904635 WA |
2869 | } |
2870 | vcpu->arch.ia32_tsc_adjust_msr = data; | |
2871 | } | |
2872 | break; | |
15c4a640 | 2873 | case MSR_IA32_MISC_ENABLE: |
511a8556 WL |
2874 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && |
2875 | ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { | |
2876 | if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) | |
2877 | return 1; | |
2878 | vcpu->arch.ia32_misc_enable_msr = data; | |
2879 | kvm_update_cpuid(vcpu); | |
2880 | } else { | |
2881 | vcpu->arch.ia32_misc_enable_msr = data; | |
2882 | } | |
15c4a640 | 2883 | break; |
64d60670 PB |
2884 | case MSR_IA32_SMBASE: |
2885 | if (!msr_info->host_initiated) | |
2886 | return 1; | |
2887 | vcpu->arch.smbase = data; | |
2888 | break; | |
73f624f4 PB |
2889 | case MSR_IA32_POWER_CTL: |
2890 | vcpu->arch.msr_ia32_power_ctl = data; | |
2891 | break; | |
dd259935 PB |
2892 | case MSR_IA32_TSC: |
2893 | kvm_write_tsc(vcpu, msr_info); | |
2894 | break; | |
864e2ab2 AL |
2895 | case MSR_IA32_XSS: |
2896 | if (!msr_info->host_initiated && | |
2897 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
2898 | return 1; | |
2899 | /* | |
a1bead2a SC |
2900 | * KVM supports exposing PT to the guest, but does not support |
2901 | * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than | |
2902 | * XSAVES/XRSTORS to save/restore PT MSRs. | |
864e2ab2 | 2903 | */ |
408e9a31 | 2904 | if (data & ~supported_xss) |
864e2ab2 AL |
2905 | return 1; |
2906 | vcpu->arch.ia32_xss = data; | |
2907 | break; | |
52797bf9 LA |
2908 | case MSR_SMI_COUNT: |
2909 | if (!msr_info->host_initiated) | |
2910 | return 1; | |
2911 | vcpu->arch.smi_count = data; | |
2912 | break; | |
11c6bffa | 2913 | case MSR_KVM_WALL_CLOCK_NEW: |
18068523 GOC |
2914 | case MSR_KVM_WALL_CLOCK: |
2915 | vcpu->kvm->arch.wall_clock = data; | |
2916 | kvm_write_wall_clock(vcpu->kvm, data); | |
2917 | break; | |
11c6bffa | 2918 | case MSR_KVM_SYSTEM_TIME_NEW: |
18068523 | 2919 | case MSR_KVM_SYSTEM_TIME: { |
54750f2c MT |
2920 | struct kvm_arch *ka = &vcpu->kvm->arch; |
2921 | ||
54750f2c MT |
2922 | if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) { |
2923 | bool tmp = (msr == MSR_KVM_SYSTEM_TIME); | |
2924 | ||
2925 | if (ka->boot_vcpu_runs_old_kvmclock != tmp) | |
1bd2009e | 2926 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
54750f2c MT |
2927 | |
2928 | ka->boot_vcpu_runs_old_kvmclock = tmp; | |
2929 | } | |
2930 | ||
18068523 | 2931 | vcpu->arch.time = data; |
0061d53d | 2932 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
18068523 GOC |
2933 | |
2934 | /* we verify if the enable bit is set... */ | |
49dedf0d | 2935 | vcpu->arch.pv_time_enabled = false; |
18068523 GOC |
2936 | if (!(data & 1)) |
2937 | break; | |
2938 | ||
49dedf0d | 2939 | if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, |
8f964525 AH |
2940 | &vcpu->arch.pv_time, data & ~1ULL, |
2941 | sizeof(struct pvclock_vcpu_time_info))) | |
0b79459b | 2942 | vcpu->arch.pv_time_enabled = true; |
32cad84f | 2943 | |
18068523 GOC |
2944 | break; |
2945 | } | |
344d9588 GN |
2946 | case MSR_KVM_ASYNC_PF_EN: |
2947 | if (kvm_pv_enable_async_pf(vcpu, data)) | |
2948 | return 1; | |
2949 | break; | |
2635b5c4 VK |
2950 | case MSR_KVM_ASYNC_PF_INT: |
2951 | if (kvm_pv_enable_async_pf_int(vcpu, data)) | |
2952 | return 1; | |
2953 | break; | |
557a961a VK |
2954 | case MSR_KVM_ASYNC_PF_ACK: |
2955 | if (data & 0x1) { | |
2956 | vcpu->arch.apf.pageready_pending = false; | |
2957 | kvm_check_async_pf_completion(vcpu); | |
2958 | } | |
2959 | break; | |
c9aaa895 GC |
2960 | case MSR_KVM_STEAL_TIME: |
2961 | ||
2962 | if (unlikely(!sched_info_on())) | |
2963 | return 1; | |
2964 | ||
2965 | if (data & KVM_STEAL_RESERVED_MASK) | |
2966 | return 1; | |
2967 | ||
c9aaa895 GC |
2968 | vcpu->arch.st.msr_val = data; |
2969 | ||
2970 | if (!(data & KVM_MSR_ENABLED)) | |
2971 | break; | |
2972 | ||
c9aaa895 GC |
2973 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
2974 | ||
2975 | break; | |
ae7a2a3f | 2976 | case MSR_KVM_PV_EOI_EN: |
72bbf935 | 2977 | if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8))) |
ae7a2a3f MT |
2978 | return 1; |
2979 | break; | |
c9aaa895 | 2980 | |
2d5ba19b MT |
2981 | case MSR_KVM_POLL_CONTROL: |
2982 | /* only enable bit supported */ | |
2983 | if (data & (-1ULL << 1)) | |
2984 | return 1; | |
2985 | ||
2986 | vcpu->arch.msr_kvm_poll_control = data; | |
2987 | break; | |
2988 | ||
890ca9ae HY |
2989 | case MSR_IA32_MCG_CTL: |
2990 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 2991 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
9ffd986c | 2992 | return set_msr_mce(vcpu, msr_info); |
71db6023 | 2993 | |
6912ac32 WH |
2994 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
2995 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
2996 | pr = true; /* fall through */ | |
2997 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: | |
2998 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 2999 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3000 | return kvm_pmu_set_msr(vcpu, msr_info); |
5753785f GN |
3001 | |
3002 | if (pr || data != 0) | |
a737f256 CD |
3003 | vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " |
3004 | "0x%x data 0x%llx\n", msr, data); | |
5753785f | 3005 | break; |
84e0cefa JS |
3006 | case MSR_K7_CLK_CTL: |
3007 | /* | |
3008 | * Ignore all writes to this no longer documented MSR. | |
3009 | * Writes are only relevant for old K7 processors, | |
3010 | * all pre-dating SVM, but a recommended workaround from | |
4a969980 | 3011 | * AMD for these chips. It is possible to specify the |
84e0cefa JS |
3012 | * affected processor models on the command line, hence |
3013 | * the need to ignore the workaround. | |
3014 | */ | |
3015 | break; | |
55cd8e5a | 3016 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3017 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3018 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3019 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3020 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3021 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3022 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3023 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3024 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e7d9513b AS |
3025 | return kvm_hv_set_msr_common(vcpu, msr, data, |
3026 | msr_info->host_initiated); | |
91c9c3ed | 3027 | case MSR_IA32_BBL_CR_CTL3: |
3028 | /* Drop writes to this legacy MSR -- see rdmsr | |
3029 | * counterpart for further detail. | |
3030 | */ | |
fab0aa3b EM |
3031 | if (report_ignored_msrs) |
3032 | vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", | |
3033 | msr, data); | |
91c9c3ed | 3034 | break; |
2b036c6b | 3035 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3036 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3037 | return 1; |
3038 | vcpu->arch.osvw.length = data; | |
3039 | break; | |
3040 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3041 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3042 | return 1; |
3043 | vcpu->arch.osvw.status = data; | |
3044 | break; | |
db2336a8 KH |
3045 | case MSR_PLATFORM_INFO: |
3046 | if (!msr_info->host_initiated || | |
db2336a8 KH |
3047 | (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) && |
3048 | cpuid_fault_enabled(vcpu))) | |
3049 | return 1; | |
3050 | vcpu->arch.msr_platform_info = data; | |
3051 | break; | |
3052 | case MSR_MISC_FEATURES_ENABLES: | |
3053 | if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || | |
3054 | (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && | |
3055 | !supports_cpuid_fault(vcpu))) | |
3056 | return 1; | |
3057 | vcpu->arch.msr_misc_features_enables = data; | |
3058 | break; | |
15c4a640 | 3059 | default: |
ffde22ac ES |
3060 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
3061 | return xen_hvm_config(vcpu, data); | |
c6702c9d | 3062 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3063 | return kvm_pmu_set_msr(vcpu, msr_info); |
ed85c068 | 3064 | if (!ignore_msrs) { |
ae0f5499 | 3065 | vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n", |
a737f256 | 3066 | msr, data); |
ed85c068 AP |
3067 | return 1; |
3068 | } else { | |
fab0aa3b EM |
3069 | if (report_ignored_msrs) |
3070 | vcpu_unimpl(vcpu, | |
3071 | "ignored wrmsr: 0x%x data 0x%llx\n", | |
3072 | msr, data); | |
ed85c068 AP |
3073 | break; |
3074 | } | |
15c4a640 CO |
3075 | } |
3076 | return 0; | |
3077 | } | |
3078 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
3079 | ||
44883f01 | 3080 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
15c4a640 CO |
3081 | { |
3082 | u64 data; | |
890ca9ae HY |
3083 | u64 mcg_cap = vcpu->arch.mcg_cap; |
3084 | unsigned bank_num = mcg_cap & 0xff; | |
15c4a640 CO |
3085 | |
3086 | switch (msr) { | |
15c4a640 CO |
3087 | case MSR_IA32_P5_MC_ADDR: |
3088 | case MSR_IA32_P5_MC_TYPE: | |
890ca9ae HY |
3089 | data = 0; |
3090 | break; | |
15c4a640 | 3091 | case MSR_IA32_MCG_CAP: |
890ca9ae HY |
3092 | data = vcpu->arch.mcg_cap; |
3093 | break; | |
c7ac679c | 3094 | case MSR_IA32_MCG_CTL: |
44883f01 | 3095 | if (!(mcg_cap & MCG_CTL_P) && !host) |
890ca9ae HY |
3096 | return 1; |
3097 | data = vcpu->arch.mcg_ctl; | |
3098 | break; | |
3099 | case MSR_IA32_MCG_STATUS: | |
3100 | data = vcpu->arch.mcg_status; | |
3101 | break; | |
3102 | default: | |
3103 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 3104 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
3105 | u32 offset = array_index_nospec( |
3106 | msr - MSR_IA32_MC0_CTL, | |
3107 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
3108 | ||
890ca9ae HY |
3109 | data = vcpu->arch.mce_banks[offset]; |
3110 | break; | |
3111 | } | |
3112 | return 1; | |
3113 | } | |
3114 | *pdata = data; | |
3115 | return 0; | |
3116 | } | |
3117 | ||
609e36d3 | 3118 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
890ca9ae | 3119 | { |
609e36d3 | 3120 | switch (msr_info->index) { |
890ca9ae | 3121 | case MSR_IA32_PLATFORM_ID: |
15c4a640 | 3122 | case MSR_IA32_EBL_CR_POWERON: |
b5e2fec0 AG |
3123 | case MSR_IA32_DEBUGCTLMSR: |
3124 | case MSR_IA32_LASTBRANCHFROMIP: | |
3125 | case MSR_IA32_LASTBRANCHTOIP: | |
3126 | case MSR_IA32_LASTINTFROMIP: | |
3127 | case MSR_IA32_LASTINTTOIP: | |
60af2ecd | 3128 | case MSR_K8_SYSCFG: |
3afb1121 PB |
3129 | case MSR_K8_TSEG_ADDR: |
3130 | case MSR_K8_TSEG_MASK: | |
61a6bd67 | 3131 | case MSR_VM_HSAVE_PA: |
1fdbd48c | 3132 | case MSR_K8_INT_PENDING_MSG: |
c323c0e5 | 3133 | case MSR_AMD64_NB_CFG: |
f7c6d140 | 3134 | case MSR_FAM10H_MMIO_CONF_BASE: |
2e32b719 | 3135 | case MSR_AMD64_BU_CFG2: |
0c2df2a1 | 3136 | case MSR_IA32_PERF_CTL: |
405a353a | 3137 | case MSR_AMD64_DC_CFG: |
0e1b869f | 3138 | case MSR_F15H_EX_CFG: |
2ca1a06a VS |
3139 | /* |
3140 | * Intel Sandy Bridge CPUs must support the RAPL (running average power | |
3141 | * limit) MSRs. Just return 0, as we do not want to expose the host | |
3142 | * data here. Do not conditionalize this on CPUID, as KVM does not do | |
3143 | * so for existing CPU-specific MSRs. | |
3144 | */ | |
3145 | case MSR_RAPL_POWER_UNIT: | |
3146 | case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */ | |
3147 | case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */ | |
3148 | case MSR_PKG_ENERGY_STATUS: /* Total package */ | |
3149 | case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ | |
609e36d3 | 3150 | msr_info->data = 0; |
15c4a640 | 3151 | break; |
c51eb52b | 3152 | case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: |
6912ac32 WH |
3153 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
3154 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: | |
3155 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
3156 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3157 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3158 | return kvm_pmu_get_msr(vcpu, msr_info); |
609e36d3 | 3159 | msr_info->data = 0; |
5753785f | 3160 | break; |
742bc670 | 3161 | case MSR_IA32_UCODE_REV: |
518e7b94 | 3162 | msr_info->data = vcpu->arch.microcode_version; |
742bc670 | 3163 | break; |
0cf9135b SC |
3164 | case MSR_IA32_ARCH_CAPABILITIES: |
3165 | if (!msr_info->host_initiated && | |
3166 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) | |
3167 | return 1; | |
3168 | msr_info->data = vcpu->arch.arch_capabilities; | |
3169 | break; | |
73f624f4 PB |
3170 | case MSR_IA32_POWER_CTL: |
3171 | msr_info->data = vcpu->arch.msr_ia32_power_ctl; | |
3172 | break; | |
dd259935 PB |
3173 | case MSR_IA32_TSC: |
3174 | msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset; | |
3175 | break; | |
9ba075a6 | 3176 | case MSR_MTRRcap: |
9ba075a6 | 3177 | case 0x200 ... 0x2ff: |
ff53604b | 3178 | return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); |
15c4a640 | 3179 | case 0xcd: /* fsb frequency */ |
609e36d3 | 3180 | msr_info->data = 3; |
15c4a640 | 3181 | break; |
7b914098 JS |
3182 | /* |
3183 | * MSR_EBC_FREQUENCY_ID | |
3184 | * Conservative value valid for even the basic CPU models. | |
3185 | * Models 0,1: 000 in bits 23:21 indicating a bus speed of | |
3186 | * 100MHz, model 2 000 in bits 18:16 indicating 100MHz, | |
3187 | * and 266MHz for model 3, or 4. Set Core Clock | |
3188 | * Frequency to System Bus Frequency Ratio to 1 (bits | |
3189 | * 31:24) even though these are only valid for CPU | |
3190 | * models > 2, however guests may end up dividing or | |
3191 | * multiplying by zero otherwise. | |
3192 | */ | |
3193 | case MSR_EBC_FREQUENCY_ID: | |
609e36d3 | 3194 | msr_info->data = 1 << 24; |
7b914098 | 3195 | break; |
15c4a640 | 3196 | case MSR_IA32_APICBASE: |
609e36d3 | 3197 | msr_info->data = kvm_get_apic_base(vcpu); |
15c4a640 | 3198 | break; |
0105d1a5 | 3199 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
609e36d3 | 3200 | return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); |
a3e06bbe | 3201 | case MSR_IA32_TSCDEADLINE: |
609e36d3 | 3202 | msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); |
a3e06bbe | 3203 | break; |
ba904635 | 3204 | case MSR_IA32_TSC_ADJUST: |
609e36d3 | 3205 | msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; |
ba904635 | 3206 | break; |
15c4a640 | 3207 | case MSR_IA32_MISC_ENABLE: |
609e36d3 | 3208 | msr_info->data = vcpu->arch.ia32_misc_enable_msr; |
15c4a640 | 3209 | break; |
64d60670 PB |
3210 | case MSR_IA32_SMBASE: |
3211 | if (!msr_info->host_initiated) | |
3212 | return 1; | |
3213 | msr_info->data = vcpu->arch.smbase; | |
15c4a640 | 3214 | break; |
52797bf9 LA |
3215 | case MSR_SMI_COUNT: |
3216 | msr_info->data = vcpu->arch.smi_count; | |
3217 | break; | |
847f0ad8 AG |
3218 | case MSR_IA32_PERF_STATUS: |
3219 | /* TSC increment by tick */ | |
609e36d3 | 3220 | msr_info->data = 1000ULL; |
847f0ad8 | 3221 | /* CPU multiplier */ |
b0996ae4 | 3222 | msr_info->data |= (((uint64_t)4ULL) << 40); |
847f0ad8 | 3223 | break; |
15c4a640 | 3224 | case MSR_EFER: |
609e36d3 | 3225 | msr_info->data = vcpu->arch.efer; |
15c4a640 | 3226 | break; |
18068523 | 3227 | case MSR_KVM_WALL_CLOCK: |
11c6bffa | 3228 | case MSR_KVM_WALL_CLOCK_NEW: |
609e36d3 | 3229 | msr_info->data = vcpu->kvm->arch.wall_clock; |
18068523 GOC |
3230 | break; |
3231 | case MSR_KVM_SYSTEM_TIME: | |
11c6bffa | 3232 | case MSR_KVM_SYSTEM_TIME_NEW: |
609e36d3 | 3233 | msr_info->data = vcpu->arch.time; |
18068523 | 3234 | break; |
344d9588 | 3235 | case MSR_KVM_ASYNC_PF_EN: |
2635b5c4 VK |
3236 | msr_info->data = vcpu->arch.apf.msr_en_val; |
3237 | break; | |
3238 | case MSR_KVM_ASYNC_PF_INT: | |
3239 | msr_info->data = vcpu->arch.apf.msr_int_val; | |
344d9588 | 3240 | break; |
557a961a VK |
3241 | case MSR_KVM_ASYNC_PF_ACK: |
3242 | msr_info->data = 0; | |
3243 | break; | |
c9aaa895 | 3244 | case MSR_KVM_STEAL_TIME: |
609e36d3 | 3245 | msr_info->data = vcpu->arch.st.msr_val; |
c9aaa895 | 3246 | break; |
1d92128f | 3247 | case MSR_KVM_PV_EOI_EN: |
609e36d3 | 3248 | msr_info->data = vcpu->arch.pv_eoi.msr_val; |
1d92128f | 3249 | break; |
2d5ba19b MT |
3250 | case MSR_KVM_POLL_CONTROL: |
3251 | msr_info->data = vcpu->arch.msr_kvm_poll_control; | |
3252 | break; | |
890ca9ae HY |
3253 | case MSR_IA32_P5_MC_ADDR: |
3254 | case MSR_IA32_P5_MC_TYPE: | |
3255 | case MSR_IA32_MCG_CAP: | |
3256 | case MSR_IA32_MCG_CTL: | |
3257 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 3258 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
44883f01 PB |
3259 | return get_msr_mce(vcpu, msr_info->index, &msr_info->data, |
3260 | msr_info->host_initiated); | |
864e2ab2 AL |
3261 | case MSR_IA32_XSS: |
3262 | if (!msr_info->host_initiated && | |
3263 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
3264 | return 1; | |
3265 | msr_info->data = vcpu->arch.ia32_xss; | |
3266 | break; | |
84e0cefa JS |
3267 | case MSR_K7_CLK_CTL: |
3268 | /* | |
3269 | * Provide expected ramp-up count for K7. All other | |
3270 | * are set to zero, indicating minimum divisors for | |
3271 | * every field. | |
3272 | * | |
3273 | * This prevents guest kernels on AMD host with CPU | |
3274 | * type 6, model 8 and higher from exploding due to | |
3275 | * the rdmsr failing. | |
3276 | */ | |
609e36d3 | 3277 | msr_info->data = 0x20000000; |
84e0cefa | 3278 | break; |
55cd8e5a | 3279 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3280 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3281 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3282 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3283 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3284 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3285 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3286 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3287 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e83d5887 | 3288 | return kvm_hv_get_msr_common(vcpu, |
44883f01 PB |
3289 | msr_info->index, &msr_info->data, |
3290 | msr_info->host_initiated); | |
91c9c3ed | 3291 | case MSR_IA32_BBL_CR_CTL3: |
3292 | /* This legacy MSR exists but isn't fully documented in current | |
3293 | * silicon. It is however accessed by winxp in very narrow | |
3294 | * scenarios where it sets bit #19, itself documented as | |
3295 | * a "reserved" bit. Best effort attempt to source coherent | |
3296 | * read data here should the balance of the register be | |
3297 | * interpreted by the guest: | |
3298 | * | |
3299 | * L2 cache control register 3: 64GB range, 256KB size, | |
3300 | * enabled, latency 0x1, configured | |
3301 | */ | |
609e36d3 | 3302 | msr_info->data = 0xbe702111; |
91c9c3ed | 3303 | break; |
2b036c6b | 3304 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3305 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 3306 | return 1; |
609e36d3 | 3307 | msr_info->data = vcpu->arch.osvw.length; |
2b036c6b BO |
3308 | break; |
3309 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3310 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 3311 | return 1; |
609e36d3 | 3312 | msr_info->data = vcpu->arch.osvw.status; |
2b036c6b | 3313 | break; |
db2336a8 | 3314 | case MSR_PLATFORM_INFO: |
6fbbde9a DS |
3315 | if (!msr_info->host_initiated && |
3316 | !vcpu->kvm->arch.guest_can_read_msr_platform_info) | |
3317 | return 1; | |
db2336a8 KH |
3318 | msr_info->data = vcpu->arch.msr_platform_info; |
3319 | break; | |
3320 | case MSR_MISC_FEATURES_ENABLES: | |
3321 | msr_info->data = vcpu->arch.msr_misc_features_enables; | |
3322 | break; | |
191c8137 BP |
3323 | case MSR_K7_HWCR: |
3324 | msr_info->data = vcpu->arch.msr_hwcr; | |
3325 | break; | |
15c4a640 | 3326 | default: |
c6702c9d | 3327 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3328 | return kvm_pmu_get_msr(vcpu, msr_info); |
ed85c068 | 3329 | if (!ignore_msrs) { |
ae0f5499 BD |
3330 | vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n", |
3331 | msr_info->index); | |
ed85c068 AP |
3332 | return 1; |
3333 | } else { | |
fab0aa3b EM |
3334 | if (report_ignored_msrs) |
3335 | vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", | |
3336 | msr_info->index); | |
609e36d3 | 3337 | msr_info->data = 0; |
ed85c068 AP |
3338 | } |
3339 | break; | |
15c4a640 | 3340 | } |
15c4a640 CO |
3341 | return 0; |
3342 | } | |
3343 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
3344 | ||
313a3dc7 CO |
3345 | /* |
3346 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
3347 | * | |
3348 | * @return number of msrs set successfully. | |
3349 | */ | |
3350 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
3351 | struct kvm_msr_entry *entries, | |
3352 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
3353 | unsigned index, u64 *data)) | |
3354 | { | |
801e459a | 3355 | int i; |
313a3dc7 | 3356 | |
313a3dc7 CO |
3357 | for (i = 0; i < msrs->nmsrs; ++i) |
3358 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
3359 | break; | |
3360 | ||
313a3dc7 CO |
3361 | return i; |
3362 | } | |
3363 | ||
3364 | /* | |
3365 | * Read or write a bunch of msrs. Parameters are user addresses. | |
3366 | * | |
3367 | * @return number of msrs set successfully. | |
3368 | */ | |
3369 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
3370 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
3371 | unsigned index, u64 *data), | |
3372 | int writeback) | |
3373 | { | |
3374 | struct kvm_msrs msrs; | |
3375 | struct kvm_msr_entry *entries; | |
3376 | int r, n; | |
3377 | unsigned size; | |
3378 | ||
3379 | r = -EFAULT; | |
0e96f31e | 3380 | if (copy_from_user(&msrs, user_msrs, sizeof(msrs))) |
313a3dc7 CO |
3381 | goto out; |
3382 | ||
3383 | r = -E2BIG; | |
3384 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
3385 | goto out; | |
3386 | ||
313a3dc7 | 3387 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
ff5c2c03 SL |
3388 | entries = memdup_user(user_msrs->entries, size); |
3389 | if (IS_ERR(entries)) { | |
3390 | r = PTR_ERR(entries); | |
313a3dc7 | 3391 | goto out; |
ff5c2c03 | 3392 | } |
313a3dc7 CO |
3393 | |
3394 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
3395 | if (r < 0) | |
3396 | goto out_free; | |
3397 | ||
3398 | r = -EFAULT; | |
3399 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
3400 | goto out_free; | |
3401 | ||
3402 | r = n; | |
3403 | ||
3404 | out_free: | |
7a73c028 | 3405 | kfree(entries); |
313a3dc7 CO |
3406 | out: |
3407 | return r; | |
3408 | } | |
3409 | ||
4d5422ce WL |
3410 | static inline bool kvm_can_mwait_in_guest(void) |
3411 | { | |
3412 | return boot_cpu_has(X86_FEATURE_MWAIT) && | |
8e9b29b6 KA |
3413 | !boot_cpu_has_bug(X86_BUG_MONITOR) && |
3414 | boot_cpu_has(X86_FEATURE_ARAT); | |
4d5422ce WL |
3415 | } |
3416 | ||
784aa3d7 | 3417 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
018d00d2 | 3418 | { |
4d5422ce | 3419 | int r = 0; |
018d00d2 ZX |
3420 | |
3421 | switch (ext) { | |
3422 | case KVM_CAP_IRQCHIP: | |
3423 | case KVM_CAP_HLT: | |
3424 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
018d00d2 | 3425 | case KVM_CAP_SET_TSS_ADDR: |
07716717 | 3426 | case KVM_CAP_EXT_CPUID: |
9c15bb1d | 3427 | case KVM_CAP_EXT_EMUL_CPUID: |
c8076604 | 3428 | case KVM_CAP_CLOCKSOURCE: |
7837699f | 3429 | case KVM_CAP_PIT: |
a28e4f5a | 3430 | case KVM_CAP_NOP_IO_DELAY: |
62d9f0db | 3431 | case KVM_CAP_MP_STATE: |
ed848624 | 3432 | case KVM_CAP_SYNC_MMU: |
a355c85c | 3433 | case KVM_CAP_USER_NMI: |
52d939a0 | 3434 | case KVM_CAP_REINJECT_CONTROL: |
4925663a | 3435 | case KVM_CAP_IRQ_INJECT_STATUS: |
d34e6b17 | 3436 | case KVM_CAP_IOEVENTFD: |
f848a5a8 | 3437 | case KVM_CAP_IOEVENTFD_NO_LENGTH: |
c5ff41ce | 3438 | case KVM_CAP_PIT2: |
e9f42757 | 3439 | case KVM_CAP_PIT_STATE2: |
b927a3ce | 3440 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
ffde22ac | 3441 | case KVM_CAP_XEN_HVM: |
3cfc3092 | 3442 | case KVM_CAP_VCPU_EVENTS: |
55cd8e5a | 3443 | case KVM_CAP_HYPERV: |
10388a07 | 3444 | case KVM_CAP_HYPERV_VAPIC: |
c25bc163 | 3445 | case KVM_CAP_HYPERV_SPIN: |
5c919412 | 3446 | case KVM_CAP_HYPERV_SYNIC: |
efc479e6 | 3447 | case KVM_CAP_HYPERV_SYNIC2: |
d3457c87 | 3448 | case KVM_CAP_HYPERV_VP_INDEX: |
faeb7833 | 3449 | case KVM_CAP_HYPERV_EVENTFD: |
c1aea919 | 3450 | case KVM_CAP_HYPERV_TLBFLUSH: |
214ff83d | 3451 | case KVM_CAP_HYPERV_SEND_IPI: |
2bc39970 | 3452 | case KVM_CAP_HYPERV_CPUID: |
ab9f4ecb | 3453 | case KVM_CAP_PCI_SEGMENT: |
a1efbe77 | 3454 | case KVM_CAP_DEBUGREGS: |
d2be1651 | 3455 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
2d5b5a66 | 3456 | case KVM_CAP_XSAVE: |
344d9588 | 3457 | case KVM_CAP_ASYNC_PF: |
72de5fa4 | 3458 | case KVM_CAP_ASYNC_PF_INT: |
92a1f12d | 3459 | case KVM_CAP_GET_TSC_KHZ: |
1c0b28c2 | 3460 | case KVM_CAP_KVMCLOCK_CTRL: |
4d8b81ab | 3461 | case KVM_CAP_READONLY_MEM: |
5f66b620 | 3462 | case KVM_CAP_HYPERV_TIME: |
100943c5 | 3463 | case KVM_CAP_IOAPIC_POLARITY_IGNORED: |
defcf51f | 3464 | case KVM_CAP_TSC_DEADLINE_TIMER: |
90de4a18 | 3465 | case KVM_CAP_DISABLE_QUIRKS: |
d71ba788 | 3466 | case KVM_CAP_SET_BOOT_CPU_ID: |
49df6397 | 3467 | case KVM_CAP_SPLIT_IRQCHIP: |
460df4c1 | 3468 | case KVM_CAP_IMMEDIATE_EXIT: |
66bb8a06 | 3469 | case KVM_CAP_PMU_EVENT_FILTER: |
801e459a | 3470 | case KVM_CAP_GET_MSR_FEATURES: |
6fbbde9a | 3471 | case KVM_CAP_MSR_PLATFORM_INFO: |
c4f55198 | 3472 | case KVM_CAP_EXCEPTION_PAYLOAD: |
b9b2782c | 3473 | case KVM_CAP_SET_GUEST_DEBUG: |
018d00d2 ZX |
3474 | r = 1; |
3475 | break; | |
01643c51 KH |
3476 | case KVM_CAP_SYNC_REGS: |
3477 | r = KVM_SYNC_X86_VALID_FIELDS; | |
3478 | break; | |
e3fd9a93 PB |
3479 | case KVM_CAP_ADJUST_CLOCK: |
3480 | r = KVM_CLOCK_TSC_STABLE; | |
3481 | break; | |
4d5422ce | 3482 | case KVM_CAP_X86_DISABLE_EXITS: |
b5170063 WL |
3483 | r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | |
3484 | KVM_X86_DISABLE_EXITS_CSTATE; | |
4d5422ce WL |
3485 | if(kvm_can_mwait_in_guest()) |
3486 | r |= KVM_X86_DISABLE_EXITS_MWAIT; | |
668fffa3 | 3487 | break; |
6d396b55 PB |
3488 | case KVM_CAP_X86_SMM: |
3489 | /* SMBASE is usually relocated above 1M on modern chipsets, | |
3490 | * and SMM handlers might indeed rely on 4G segment limits, | |
3491 | * so do not report SMM to be available if real mode is | |
3492 | * emulated via vm86 mode. Still, do not go to great lengths | |
3493 | * to avoid userspace's usage of the feature, because it is a | |
3494 | * fringe case that is not enabled except via specific settings | |
3495 | * of the module parameters. | |
3496 | */ | |
afaf0b2f | 3497 | r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE); |
6d396b55 | 3498 | break; |
774ead3a | 3499 | case KVM_CAP_VAPIC: |
afaf0b2f | 3500 | r = !kvm_x86_ops.cpu_has_accelerated_tpr(); |
774ead3a | 3501 | break; |
f725230a | 3502 | case KVM_CAP_NR_VCPUS: |
8c3ba334 SL |
3503 | r = KVM_SOFT_MAX_VCPUS; |
3504 | break; | |
3505 | case KVM_CAP_MAX_VCPUS: | |
f725230a AK |
3506 | r = KVM_MAX_VCPUS; |
3507 | break; | |
a86cb413 TH |
3508 | case KVM_CAP_MAX_VCPU_ID: |
3509 | r = KVM_MAX_VCPU_ID; | |
3510 | break; | |
a68a6a72 MT |
3511 | case KVM_CAP_PV_MMU: /* obsolete */ |
3512 | r = 0; | |
2f333bcb | 3513 | break; |
890ca9ae HY |
3514 | case KVM_CAP_MCE: |
3515 | r = KVM_MAX_MCE_BANKS; | |
3516 | break; | |
2d5b5a66 | 3517 | case KVM_CAP_XCRS: |
d366bf7e | 3518 | r = boot_cpu_has(X86_FEATURE_XSAVE); |
2d5b5a66 | 3519 | break; |
92a1f12d JR |
3520 | case KVM_CAP_TSC_CONTROL: |
3521 | r = kvm_has_tsc_control; | |
3522 | break; | |
37131313 RK |
3523 | case KVM_CAP_X2APIC_API: |
3524 | r = KVM_X2APIC_API_VALID_FLAGS; | |
3525 | break; | |
8fcc4b59 | 3526 | case KVM_CAP_NESTED_STATE: |
33b22172 PB |
3527 | r = kvm_x86_ops.nested_ops->get_state ? |
3528 | kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; | |
8fcc4b59 | 3529 | break; |
344c6c80 | 3530 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 3531 | r = kvm_x86_ops.enable_direct_tlbflush != NULL; |
5a0165f6 VK |
3532 | break; |
3533 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: | |
33b22172 | 3534 | r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; |
344c6c80 | 3535 | break; |
018d00d2 | 3536 | default: |
018d00d2 ZX |
3537 | break; |
3538 | } | |
3539 | return r; | |
3540 | ||
3541 | } | |
3542 | ||
043405e1 CO |
3543 | long kvm_arch_dev_ioctl(struct file *filp, |
3544 | unsigned int ioctl, unsigned long arg) | |
3545 | { | |
3546 | void __user *argp = (void __user *)arg; | |
3547 | long r; | |
3548 | ||
3549 | switch (ioctl) { | |
3550 | case KVM_GET_MSR_INDEX_LIST: { | |
3551 | struct kvm_msr_list __user *user_msr_list = argp; | |
3552 | struct kvm_msr_list msr_list; | |
3553 | unsigned n; | |
3554 | ||
3555 | r = -EFAULT; | |
0e96f31e | 3556 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
043405e1 CO |
3557 | goto out; |
3558 | n = msr_list.nmsrs; | |
62ef68bb | 3559 | msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; |
0e96f31e | 3560 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
043405e1 CO |
3561 | goto out; |
3562 | r = -E2BIG; | |
e125e7b6 | 3563 | if (n < msr_list.nmsrs) |
043405e1 CO |
3564 | goto out; |
3565 | r = -EFAULT; | |
3566 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
3567 | num_msrs_to_save * sizeof(u32))) | |
3568 | goto out; | |
e125e7b6 | 3569 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
043405e1 | 3570 | &emulated_msrs, |
62ef68bb | 3571 | num_emulated_msrs * sizeof(u32))) |
043405e1 CO |
3572 | goto out; |
3573 | r = 0; | |
3574 | break; | |
3575 | } | |
9c15bb1d BP |
3576 | case KVM_GET_SUPPORTED_CPUID: |
3577 | case KVM_GET_EMULATED_CPUID: { | |
674eea0f AK |
3578 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
3579 | struct kvm_cpuid2 cpuid; | |
3580 | ||
3581 | r = -EFAULT; | |
0e96f31e | 3582 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
674eea0f | 3583 | goto out; |
9c15bb1d BP |
3584 | |
3585 | r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, | |
3586 | ioctl); | |
674eea0f AK |
3587 | if (r) |
3588 | goto out; | |
3589 | ||
3590 | r = -EFAULT; | |
0e96f31e | 3591 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
674eea0f AK |
3592 | goto out; |
3593 | r = 0; | |
3594 | break; | |
3595 | } | |
cf6c26ec | 3596 | case KVM_X86_GET_MCE_CAP_SUPPORTED: |
890ca9ae | 3597 | r = -EFAULT; |
c45dcc71 AR |
3598 | if (copy_to_user(argp, &kvm_mce_cap_supported, |
3599 | sizeof(kvm_mce_cap_supported))) | |
890ca9ae HY |
3600 | goto out; |
3601 | r = 0; | |
3602 | break; | |
801e459a TL |
3603 | case KVM_GET_MSR_FEATURE_INDEX_LIST: { |
3604 | struct kvm_msr_list __user *user_msr_list = argp; | |
3605 | struct kvm_msr_list msr_list; | |
3606 | unsigned int n; | |
3607 | ||
3608 | r = -EFAULT; | |
3609 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) | |
3610 | goto out; | |
3611 | n = msr_list.nmsrs; | |
3612 | msr_list.nmsrs = num_msr_based_features; | |
3613 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) | |
3614 | goto out; | |
3615 | r = -E2BIG; | |
3616 | if (n < msr_list.nmsrs) | |
3617 | goto out; | |
3618 | r = -EFAULT; | |
3619 | if (copy_to_user(user_msr_list->indices, &msr_based_features, | |
3620 | num_msr_based_features * sizeof(u32))) | |
3621 | goto out; | |
3622 | r = 0; | |
3623 | break; | |
3624 | } | |
3625 | case KVM_GET_MSRS: | |
3626 | r = msr_io(NULL, argp, do_get_msr_feature, 1); | |
3627 | break; | |
043405e1 CO |
3628 | default: |
3629 | r = -EINVAL; | |
cf6c26ec | 3630 | break; |
043405e1 CO |
3631 | } |
3632 | out: | |
3633 | return r; | |
3634 | } | |
3635 | ||
f5f48ee1 SY |
3636 | static void wbinvd_ipi(void *garbage) |
3637 | { | |
3638 | wbinvd(); | |
3639 | } | |
3640 | ||
3641 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
3642 | { | |
e0f0bbc5 | 3643 | return kvm_arch_has_noncoherent_dma(vcpu->kvm); |
f5f48ee1 SY |
3644 | } |
3645 | ||
313a3dc7 CO |
3646 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
3647 | { | |
f5f48ee1 SY |
3648 | /* Address WBINVD may be executed by guest */ |
3649 | if (need_emulate_wbinvd(vcpu)) { | |
afaf0b2f | 3650 | if (kvm_x86_ops.has_wbinvd_exit()) |
f5f48ee1 SY |
3651 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
3652 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) | |
3653 | smp_call_function_single(vcpu->cpu, | |
3654 | wbinvd_ipi, NULL, 1); | |
3655 | } | |
3656 | ||
afaf0b2f | 3657 | kvm_x86_ops.vcpu_load(vcpu, cpu); |
8f6055cb | 3658 | |
37486135 BM |
3659 | /* Save host pkru register if supported */ |
3660 | vcpu->arch.host_pkru = read_pkru(); | |
3661 | ||
0dd6a6ed ZA |
3662 | /* Apply any externally detected TSC adjustments (due to suspend) */ |
3663 | if (unlikely(vcpu->arch.tsc_offset_adjustment)) { | |
3664 | adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); | |
3665 | vcpu->arch.tsc_offset_adjustment = 0; | |
105b21bb | 3666 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed | 3667 | } |
8f6055cb | 3668 | |
b0c39dc6 | 3669 | if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { |
6f526ec5 | 3670 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
4ea1636b | 3671 | rdtsc() - vcpu->arch.last_host_tsc; |
e48672fa ZA |
3672 | if (tsc_delta < 0) |
3673 | mark_tsc_unstable("KVM discovered backwards TSC"); | |
ce7a058a | 3674 | |
b0c39dc6 | 3675 | if (kvm_check_tsc_unstable()) { |
07c1419a | 3676 | u64 offset = kvm_compute_tsc_offset(vcpu, |
b183aa58 | 3677 | vcpu->arch.last_guest_tsc); |
a545ab6a | 3678 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
c285545f | 3679 | vcpu->arch.tsc_catchup = 1; |
c285545f | 3680 | } |
a749e247 PB |
3681 | |
3682 | if (kvm_lapic_hv_timer_in_use(vcpu)) | |
3683 | kvm_lapic_restart_hv_timer(vcpu); | |
3684 | ||
d98d07ca MT |
3685 | /* |
3686 | * On a host with synchronized TSC, there is no need to update | |
3687 | * kvmclock on vcpu->cpu migration | |
3688 | */ | |
3689 | if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) | |
0061d53d | 3690 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
c285545f | 3691 | if (vcpu->cpu != cpu) |
1bd2009e | 3692 | kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu); |
e48672fa | 3693 | vcpu->cpu = cpu; |
6b7d7e76 | 3694 | } |
c9aaa895 | 3695 | |
c9aaa895 | 3696 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
313a3dc7 CO |
3697 | } |
3698 | ||
0b9f6c46 PX |
3699 | static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) |
3700 | { | |
b0431382 BO |
3701 | struct kvm_host_map map; |
3702 | struct kvm_steal_time *st; | |
3703 | ||
0b9f6c46 PX |
3704 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
3705 | return; | |
3706 | ||
a6bd811f | 3707 | if (vcpu->arch.st.preempted) |
8c6de56a BO |
3708 | return; |
3709 | ||
b0431382 BO |
3710 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map, |
3711 | &vcpu->arch.st.cache, true)) | |
3712 | return; | |
3713 | ||
3714 | st = map.hva + | |
3715 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); | |
0b9f6c46 | 3716 | |
a6bd811f | 3717 | st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; |
0b9f6c46 | 3718 | |
b0431382 | 3719 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true); |
0b9f6c46 PX |
3720 | } |
3721 | ||
313a3dc7 CO |
3722 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
3723 | { | |
cc0d907c | 3724 | int idx; |
de63ad4c LM |
3725 | |
3726 | if (vcpu->preempted) | |
afaf0b2f | 3727 | vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu); |
de63ad4c | 3728 | |
931f261b AA |
3729 | /* |
3730 | * Disable page faults because we're in atomic context here. | |
3731 | * kvm_write_guest_offset_cached() would call might_fault() | |
3732 | * that relies on pagefault_disable() to tell if there's a | |
3733 | * bug. NOTE: the write to guest memory may not go through if | |
3734 | * during postcopy live migration or if there's heavy guest | |
3735 | * paging. | |
3736 | */ | |
3737 | pagefault_disable(); | |
cc0d907c AA |
3738 | /* |
3739 | * kvm_memslots() will be called by | |
3740 | * kvm_write_guest_offset_cached() so take the srcu lock. | |
3741 | */ | |
3742 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
0b9f6c46 | 3743 | kvm_steal_time_set_preempted(vcpu); |
cc0d907c | 3744 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
931f261b | 3745 | pagefault_enable(); |
afaf0b2f | 3746 | kvm_x86_ops.vcpu_put(vcpu); |
4ea1636b | 3747 | vcpu->arch.last_host_tsc = rdtsc(); |
efdab992 | 3748 | /* |
f9dcf08e RK |
3749 | * If userspace has set any breakpoints or watchpoints, dr6 is restored |
3750 | * on every vmexit, but if not, we might have a stale dr6 from the | |
3751 | * guest. do_debug expects dr6 to be cleared after it runs, do the same. | |
efdab992 | 3752 | */ |
f9dcf08e | 3753 | set_debugreg(0, 6); |
313a3dc7 CO |
3754 | } |
3755 | ||
313a3dc7 CO |
3756 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
3757 | struct kvm_lapic_state *s) | |
3758 | { | |
fa59cc00 | 3759 | if (vcpu->arch.apicv_active) |
afaf0b2f | 3760 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
d62caabb | 3761 | |
a92e2543 | 3762 | return kvm_apic_get_state(vcpu, s); |
313a3dc7 CO |
3763 | } |
3764 | ||
3765 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
3766 | struct kvm_lapic_state *s) | |
3767 | { | |
a92e2543 RK |
3768 | int r; |
3769 | ||
3770 | r = kvm_apic_set_state(vcpu, s); | |
3771 | if (r) | |
3772 | return r; | |
cb142eb7 | 3773 | update_cr8_intercept(vcpu); |
313a3dc7 CO |
3774 | |
3775 | return 0; | |
3776 | } | |
3777 | ||
127a457a MG |
3778 | static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) |
3779 | { | |
3780 | return (!lapic_in_kernel(vcpu) || | |
3781 | kvm_apic_accept_pic_intr(vcpu)); | |
3782 | } | |
3783 | ||
782d422b MG |
3784 | /* |
3785 | * if userspace requested an interrupt window, check that the | |
3786 | * interrupt window is open. | |
3787 | * | |
3788 | * No need to exit to userspace if we already have an interrupt queued. | |
3789 | */ | |
3790 | static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) | |
3791 | { | |
3792 | return kvm_arch_interrupt_allowed(vcpu) && | |
3793 | !kvm_cpu_has_interrupt(vcpu) && | |
3794 | !kvm_event_needs_reinjection(vcpu) && | |
3795 | kvm_cpu_accept_dm_intr(vcpu); | |
3796 | } | |
3797 | ||
f77bc6a4 ZX |
3798 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
3799 | struct kvm_interrupt *irq) | |
3800 | { | |
02cdb50f | 3801 | if (irq->irq >= KVM_NR_INTERRUPTS) |
f77bc6a4 | 3802 | return -EINVAL; |
1c1a9ce9 SR |
3803 | |
3804 | if (!irqchip_in_kernel(vcpu->kvm)) { | |
3805 | kvm_queue_interrupt(vcpu, irq->irq, false); | |
3806 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
3807 | return 0; | |
3808 | } | |
3809 | ||
3810 | /* | |
3811 | * With in-kernel LAPIC, we only use this to inject EXTINT, so | |
3812 | * fail for in-kernel 8259. | |
3813 | */ | |
3814 | if (pic_in_kernel(vcpu->kvm)) | |
f77bc6a4 | 3815 | return -ENXIO; |
f77bc6a4 | 3816 | |
1c1a9ce9 SR |
3817 | if (vcpu->arch.pending_external_vector != -1) |
3818 | return -EEXIST; | |
f77bc6a4 | 3819 | |
1c1a9ce9 | 3820 | vcpu->arch.pending_external_vector = irq->irq; |
934bf653 | 3821 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f77bc6a4 ZX |
3822 | return 0; |
3823 | } | |
3824 | ||
c4abb7c9 JK |
3825 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
3826 | { | |
c4abb7c9 | 3827 | kvm_inject_nmi(vcpu); |
c4abb7c9 JK |
3828 | |
3829 | return 0; | |
3830 | } | |
3831 | ||
f077825a PB |
3832 | static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu) |
3833 | { | |
64d60670 PB |
3834 | kvm_make_request(KVM_REQ_SMI, vcpu); |
3835 | ||
f077825a PB |
3836 | return 0; |
3837 | } | |
3838 | ||
b209749f AK |
3839 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
3840 | struct kvm_tpr_access_ctl *tac) | |
3841 | { | |
3842 | if (tac->flags) | |
3843 | return -EINVAL; | |
3844 | vcpu->arch.tpr_access_reporting = !!tac->enabled; | |
3845 | return 0; | |
3846 | } | |
3847 | ||
890ca9ae HY |
3848 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
3849 | u64 mcg_cap) | |
3850 | { | |
3851 | int r; | |
3852 | unsigned bank_num = mcg_cap & 0xff, bank; | |
3853 | ||
3854 | r = -EINVAL; | |
c4e0e4ab | 3855 | if (!bank_num || bank_num > KVM_MAX_MCE_BANKS) |
890ca9ae | 3856 | goto out; |
c45dcc71 | 3857 | if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000)) |
890ca9ae HY |
3858 | goto out; |
3859 | r = 0; | |
3860 | vcpu->arch.mcg_cap = mcg_cap; | |
3861 | /* Init IA32_MCG_CTL to all 1s */ | |
3862 | if (mcg_cap & MCG_CTL_P) | |
3863 | vcpu->arch.mcg_ctl = ~(u64)0; | |
3864 | /* Init IA32_MCi_CTL to all 1s */ | |
3865 | for (bank = 0; bank < bank_num; bank++) | |
3866 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; | |
c45dcc71 | 3867 | |
afaf0b2f | 3868 | kvm_x86_ops.setup_mce(vcpu); |
890ca9ae HY |
3869 | out: |
3870 | return r; | |
3871 | } | |
3872 | ||
3873 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, | |
3874 | struct kvm_x86_mce *mce) | |
3875 | { | |
3876 | u64 mcg_cap = vcpu->arch.mcg_cap; | |
3877 | unsigned bank_num = mcg_cap & 0xff; | |
3878 | u64 *banks = vcpu->arch.mce_banks; | |
3879 | ||
3880 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) | |
3881 | return -EINVAL; | |
3882 | /* | |
3883 | * if IA32_MCG_CTL is not all 1s, the uncorrected error | |
3884 | * reporting is disabled | |
3885 | */ | |
3886 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && | |
3887 | vcpu->arch.mcg_ctl != ~(u64)0) | |
3888 | return 0; | |
3889 | banks += 4 * mce->bank; | |
3890 | /* | |
3891 | * if IA32_MCi_CTL is not all 1s, the uncorrected error | |
3892 | * reporting is disabled for the bank | |
3893 | */ | |
3894 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) | |
3895 | return 0; | |
3896 | if (mce->status & MCI_STATUS_UC) { | |
3897 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || | |
fc78f519 | 3898 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
a8eeb04a | 3899 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
890ca9ae HY |
3900 | return 0; |
3901 | } | |
3902 | if (banks[1] & MCI_STATUS_VAL) | |
3903 | mce->status |= MCI_STATUS_OVER; | |
3904 | banks[2] = mce->addr; | |
3905 | banks[3] = mce->misc; | |
3906 | vcpu->arch.mcg_status = mce->mcg_status; | |
3907 | banks[1] = mce->status; | |
3908 | kvm_queue_exception(vcpu, MC_VECTOR); | |
3909 | } else if (!(banks[1] & MCI_STATUS_VAL) | |
3910 | || !(banks[1] & MCI_STATUS_UC)) { | |
3911 | if (banks[1] & MCI_STATUS_VAL) | |
3912 | mce->status |= MCI_STATUS_OVER; | |
3913 | banks[2] = mce->addr; | |
3914 | banks[3] = mce->misc; | |
3915 | banks[1] = mce->status; | |
3916 | } else | |
3917 | banks[1] |= MCI_STATUS_OVER; | |
3918 | return 0; | |
3919 | } | |
3920 | ||
3cfc3092 JK |
3921 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
3922 | struct kvm_vcpu_events *events) | |
3923 | { | |
7460fb4a | 3924 | process_nmi(vcpu); |
59073aaf | 3925 | |
a06230b6 OU |
3926 | /* |
3927 | * In guest mode, payload delivery should be deferred, | |
3928 | * so that the L1 hypervisor can intercept #PF before | |
3929 | * CR2 is modified (or intercept #DB before DR6 is | |
3930 | * modified under nVMX). Unless the per-VM capability, | |
3931 | * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of | |
3932 | * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we | |
3933 | * opportunistically defer the exception payload, deliver it if the | |
3934 | * capability hasn't been requested before processing a | |
3935 | * KVM_GET_VCPU_EVENTS. | |
3936 | */ | |
3937 | if (!vcpu->kvm->arch.exception_payload_enabled && | |
3938 | vcpu->arch.exception.pending && vcpu->arch.exception.has_payload) | |
3939 | kvm_deliver_exception_payload(vcpu); | |
3940 | ||
664f8e26 | 3941 | /* |
59073aaf JM |
3942 | * The API doesn't provide the instruction length for software |
3943 | * exceptions, so don't report them. As long as the guest RIP | |
3944 | * isn't advanced, we should expect to encounter the exception | |
3945 | * again. | |
664f8e26 | 3946 | */ |
59073aaf JM |
3947 | if (kvm_exception_is_soft(vcpu->arch.exception.nr)) { |
3948 | events->exception.injected = 0; | |
3949 | events->exception.pending = 0; | |
3950 | } else { | |
3951 | events->exception.injected = vcpu->arch.exception.injected; | |
3952 | events->exception.pending = vcpu->arch.exception.pending; | |
3953 | /* | |
3954 | * For ABI compatibility, deliberately conflate | |
3955 | * pending and injected exceptions when | |
3956 | * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled. | |
3957 | */ | |
3958 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
3959 | events->exception.injected |= | |
3960 | vcpu->arch.exception.pending; | |
3961 | } | |
3cfc3092 JK |
3962 | events->exception.nr = vcpu->arch.exception.nr; |
3963 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; | |
3964 | events->exception.error_code = vcpu->arch.exception.error_code; | |
59073aaf JM |
3965 | events->exception_has_payload = vcpu->arch.exception.has_payload; |
3966 | events->exception_payload = vcpu->arch.exception.payload; | |
3cfc3092 | 3967 | |
03b82a30 | 3968 | events->interrupt.injected = |
04140b41 | 3969 | vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; |
3cfc3092 | 3970 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
03b82a30 | 3971 | events->interrupt.soft = 0; |
afaf0b2f | 3972 | events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu); |
3cfc3092 JK |
3973 | |
3974 | events->nmi.injected = vcpu->arch.nmi_injected; | |
7460fb4a | 3975 | events->nmi.pending = vcpu->arch.nmi_pending != 0; |
afaf0b2f | 3976 | events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu); |
97e69aa6 | 3977 | events->nmi.pad = 0; |
3cfc3092 | 3978 | |
66450a21 | 3979 | events->sipi_vector = 0; /* never valid when reporting to user space */ |
3cfc3092 | 3980 | |
f077825a PB |
3981 | events->smi.smm = is_smm(vcpu); |
3982 | events->smi.pending = vcpu->arch.smi_pending; | |
3983 | events->smi.smm_inside_nmi = | |
3984 | !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); | |
3985 | events->smi.latched_init = kvm_lapic_latched_init(vcpu); | |
3986 | ||
dab4b911 | 3987 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
f077825a PB |
3988 | | KVM_VCPUEVENT_VALID_SHADOW |
3989 | | KVM_VCPUEVENT_VALID_SMM); | |
59073aaf JM |
3990 | if (vcpu->kvm->arch.exception_payload_enabled) |
3991 | events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; | |
3992 | ||
97e69aa6 | 3993 | memset(&events->reserved, 0, sizeof(events->reserved)); |
3cfc3092 JK |
3994 | } |
3995 | ||
c5833c7a | 3996 | static void kvm_smm_changed(struct kvm_vcpu *vcpu); |
6ef4e07e | 3997 | |
3cfc3092 JK |
3998 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
3999 | struct kvm_vcpu_events *events) | |
4000 | { | |
dab4b911 | 4001 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
48005f64 | 4002 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
f077825a | 4003 | | KVM_VCPUEVENT_VALID_SHADOW |
59073aaf JM |
4004 | | KVM_VCPUEVENT_VALID_SMM |
4005 | | KVM_VCPUEVENT_VALID_PAYLOAD)) | |
3cfc3092 JK |
4006 | return -EINVAL; |
4007 | ||
59073aaf JM |
4008 | if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { |
4009 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
4010 | return -EINVAL; | |
4011 | if (events->exception.pending) | |
4012 | events->exception.injected = 0; | |
4013 | else | |
4014 | events->exception_has_payload = 0; | |
4015 | } else { | |
4016 | events->exception.pending = 0; | |
4017 | events->exception_has_payload = 0; | |
4018 | } | |
4019 | ||
4020 | if ((events->exception.injected || events->exception.pending) && | |
4021 | (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) | |
78e546c8 PB |
4022 | return -EINVAL; |
4023 | ||
28bf2888 DH |
4024 | /* INITs are latched while in SMM */ |
4025 | if (events->flags & KVM_VCPUEVENT_VALID_SMM && | |
4026 | (events->smi.smm || events->smi.pending) && | |
4027 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) | |
4028 | return -EINVAL; | |
4029 | ||
7460fb4a | 4030 | process_nmi(vcpu); |
59073aaf JM |
4031 | vcpu->arch.exception.injected = events->exception.injected; |
4032 | vcpu->arch.exception.pending = events->exception.pending; | |
3cfc3092 JK |
4033 | vcpu->arch.exception.nr = events->exception.nr; |
4034 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; | |
4035 | vcpu->arch.exception.error_code = events->exception.error_code; | |
59073aaf JM |
4036 | vcpu->arch.exception.has_payload = events->exception_has_payload; |
4037 | vcpu->arch.exception.payload = events->exception_payload; | |
3cfc3092 | 4038 | |
04140b41 | 4039 | vcpu->arch.interrupt.injected = events->interrupt.injected; |
3cfc3092 JK |
4040 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
4041 | vcpu->arch.interrupt.soft = events->interrupt.soft; | |
48005f64 | 4042 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
afaf0b2f | 4043 | kvm_x86_ops.set_interrupt_shadow(vcpu, |
48005f64 | 4044 | events->interrupt.shadow); |
3cfc3092 JK |
4045 | |
4046 | vcpu->arch.nmi_injected = events->nmi.injected; | |
dab4b911 JK |
4047 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
4048 | vcpu->arch.nmi_pending = events->nmi.pending; | |
afaf0b2f | 4049 | kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked); |
3cfc3092 | 4050 | |
66450a21 | 4051 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && |
bce87cce | 4052 | lapic_in_kernel(vcpu)) |
66450a21 | 4053 | vcpu->arch.apic->sipi_vector = events->sipi_vector; |
3cfc3092 | 4054 | |
f077825a | 4055 | if (events->flags & KVM_VCPUEVENT_VALID_SMM) { |
c5833c7a SC |
4056 | if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { |
4057 | if (events->smi.smm) | |
4058 | vcpu->arch.hflags |= HF_SMM_MASK; | |
4059 | else | |
4060 | vcpu->arch.hflags &= ~HF_SMM_MASK; | |
4061 | kvm_smm_changed(vcpu); | |
4062 | } | |
6ef4e07e | 4063 | |
f077825a | 4064 | vcpu->arch.smi_pending = events->smi.pending; |
f4ef1910 WL |
4065 | |
4066 | if (events->smi.smm) { | |
4067 | if (events->smi.smm_inside_nmi) | |
4068 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; | |
f077825a | 4069 | else |
f4ef1910 | 4070 | vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; |
ff90afa7 LA |
4071 | } |
4072 | ||
4073 | if (lapic_in_kernel(vcpu)) { | |
4074 | if (events->smi.latched_init) | |
4075 | set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
4076 | else | |
4077 | clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
f077825a PB |
4078 | } |
4079 | } | |
4080 | ||
3842d135 AK |
4081 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
4082 | ||
3cfc3092 JK |
4083 | return 0; |
4084 | } | |
4085 | ||
a1efbe77 JK |
4086 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
4087 | struct kvm_debugregs *dbgregs) | |
4088 | { | |
73aaf249 JK |
4089 | unsigned long val; |
4090 | ||
a1efbe77 | 4091 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
16f8a6f9 | 4092 | kvm_get_dr(vcpu, 6, &val); |
73aaf249 | 4093 | dbgregs->dr6 = val; |
a1efbe77 JK |
4094 | dbgregs->dr7 = vcpu->arch.dr7; |
4095 | dbgregs->flags = 0; | |
97e69aa6 | 4096 | memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved)); |
a1efbe77 JK |
4097 | } |
4098 | ||
4099 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, | |
4100 | struct kvm_debugregs *dbgregs) | |
4101 | { | |
4102 | if (dbgregs->flags) | |
4103 | return -EINVAL; | |
4104 | ||
d14bdb55 PB |
4105 | if (dbgregs->dr6 & ~0xffffffffull) |
4106 | return -EINVAL; | |
4107 | if (dbgregs->dr7 & ~0xffffffffull) | |
4108 | return -EINVAL; | |
4109 | ||
a1efbe77 | 4110 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
ae561ede | 4111 | kvm_update_dr0123(vcpu); |
a1efbe77 JK |
4112 | vcpu->arch.dr6 = dbgregs->dr6; |
4113 | vcpu->arch.dr7 = dbgregs->dr7; | |
9926c9fd | 4114 | kvm_update_dr7(vcpu); |
a1efbe77 | 4115 | |
a1efbe77 JK |
4116 | return 0; |
4117 | } | |
4118 | ||
df1daba7 PB |
4119 | #define XSTATE_COMPACTION_ENABLED (1ULL << 63) |
4120 | ||
4121 | static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) | |
4122 | { | |
b666a4b6 | 4123 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
400e4b20 | 4124 | u64 xstate_bv = xsave->header.xfeatures; |
df1daba7 PB |
4125 | u64 valid; |
4126 | ||
4127 | /* | |
4128 | * Copy legacy XSAVE area, to avoid complications with CPUID | |
4129 | * leaves 0 and 1 in the loop below. | |
4130 | */ | |
4131 | memcpy(dest, xsave, XSAVE_HDR_OFFSET); | |
4132 | ||
4133 | /* Set XSTATE_BV */ | |
00c87e9a | 4134 | xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE; |
df1daba7 PB |
4135 | *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv; |
4136 | ||
4137 | /* | |
4138 | * Copy each region from the possibly compacted offset to the | |
4139 | * non-compacted offset. | |
4140 | */ | |
d91cab78 | 4141 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
df1daba7 | 4142 | while (valid) { |
abd16d68 SAS |
4143 | u64 xfeature_mask = valid & -valid; |
4144 | int xfeature_nr = fls64(xfeature_mask) - 1; | |
4145 | void *src = get_xsave_addr(xsave, xfeature_nr); | |
df1daba7 PB |
4146 | |
4147 | if (src) { | |
4148 | u32 size, offset, ecx, edx; | |
abd16d68 | 4149 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
df1daba7 | 4150 | &size, &offset, &ecx, &edx); |
abd16d68 | 4151 | if (xfeature_nr == XFEATURE_PKRU) |
38cfd5e3 PB |
4152 | memcpy(dest + offset, &vcpu->arch.pkru, |
4153 | sizeof(vcpu->arch.pkru)); | |
4154 | else | |
4155 | memcpy(dest + offset, src, size); | |
4156 | ||
df1daba7 PB |
4157 | } |
4158 | ||
abd16d68 | 4159 | valid -= xfeature_mask; |
df1daba7 PB |
4160 | } |
4161 | } | |
4162 | ||
4163 | static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) | |
4164 | { | |
b666a4b6 | 4165 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
df1daba7 PB |
4166 | u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET); |
4167 | u64 valid; | |
4168 | ||
4169 | /* | |
4170 | * Copy legacy XSAVE area, to avoid complications with CPUID | |
4171 | * leaves 0 and 1 in the loop below. | |
4172 | */ | |
4173 | memcpy(xsave, src, XSAVE_HDR_OFFSET); | |
4174 | ||
4175 | /* Set XSTATE_BV and possibly XCOMP_BV. */ | |
400e4b20 | 4176 | xsave->header.xfeatures = xstate_bv; |
782511b0 | 4177 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
3a54450b | 4178 | xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED; |
df1daba7 PB |
4179 | |
4180 | /* | |
4181 | * Copy each region from the non-compacted offset to the | |
4182 | * possibly compacted offset. | |
4183 | */ | |
d91cab78 | 4184 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
df1daba7 | 4185 | while (valid) { |
abd16d68 SAS |
4186 | u64 xfeature_mask = valid & -valid; |
4187 | int xfeature_nr = fls64(xfeature_mask) - 1; | |
4188 | void *dest = get_xsave_addr(xsave, xfeature_nr); | |
df1daba7 PB |
4189 | |
4190 | if (dest) { | |
4191 | u32 size, offset, ecx, edx; | |
abd16d68 | 4192 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
df1daba7 | 4193 | &size, &offset, &ecx, &edx); |
abd16d68 | 4194 | if (xfeature_nr == XFEATURE_PKRU) |
38cfd5e3 PB |
4195 | memcpy(&vcpu->arch.pkru, src + offset, |
4196 | sizeof(vcpu->arch.pkru)); | |
4197 | else | |
4198 | memcpy(dest, src + offset, size); | |
ee4100da | 4199 | } |
df1daba7 | 4200 | |
abd16d68 | 4201 | valid -= xfeature_mask; |
df1daba7 PB |
4202 | } |
4203 | } | |
4204 | ||
2d5b5a66 SY |
4205 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
4206 | struct kvm_xsave *guest_xsave) | |
4207 | { | |
d366bf7e | 4208 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
df1daba7 PB |
4209 | memset(guest_xsave, 0, sizeof(struct kvm_xsave)); |
4210 | fill_xsave((u8 *) guest_xsave->region, vcpu); | |
4344ee98 | 4211 | } else { |
2d5b5a66 | 4212 | memcpy(guest_xsave->region, |
b666a4b6 | 4213 | &vcpu->arch.guest_fpu->state.fxsave, |
c47ada30 | 4214 | sizeof(struct fxregs_state)); |
2d5b5a66 | 4215 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = |
d91cab78 | 4216 | XFEATURE_MASK_FPSSE; |
2d5b5a66 SY |
4217 | } |
4218 | } | |
4219 | ||
a575813b WL |
4220 | #define XSAVE_MXCSR_OFFSET 24 |
4221 | ||
2d5b5a66 SY |
4222 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
4223 | struct kvm_xsave *guest_xsave) | |
4224 | { | |
4225 | u64 xstate_bv = | |
4226 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)]; | |
a575813b | 4227 | u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)]; |
2d5b5a66 | 4228 | |
d366bf7e | 4229 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
d7876f1b PB |
4230 | /* |
4231 | * Here we allow setting states that are not present in | |
4232 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility | |
4233 | * with old userspace. | |
4234 | */ | |
cfc48181 | 4235 | if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask) |
d7876f1b | 4236 | return -EINVAL; |
df1daba7 | 4237 | load_xsave(vcpu, (u8 *)guest_xsave->region); |
d7876f1b | 4238 | } else { |
a575813b WL |
4239 | if (xstate_bv & ~XFEATURE_MASK_FPSSE || |
4240 | mxcsr & ~mxcsr_feature_mask) | |
2d5b5a66 | 4241 | return -EINVAL; |
b666a4b6 | 4242 | memcpy(&vcpu->arch.guest_fpu->state.fxsave, |
c47ada30 | 4243 | guest_xsave->region, sizeof(struct fxregs_state)); |
2d5b5a66 SY |
4244 | } |
4245 | return 0; | |
4246 | } | |
4247 | ||
4248 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, | |
4249 | struct kvm_xcrs *guest_xcrs) | |
4250 | { | |
d366bf7e | 4251 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) { |
2d5b5a66 SY |
4252 | guest_xcrs->nr_xcrs = 0; |
4253 | return; | |
4254 | } | |
4255 | ||
4256 | guest_xcrs->nr_xcrs = 1; | |
4257 | guest_xcrs->flags = 0; | |
4258 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; | |
4259 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; | |
4260 | } | |
4261 | ||
4262 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, | |
4263 | struct kvm_xcrs *guest_xcrs) | |
4264 | { | |
4265 | int i, r = 0; | |
4266 | ||
d366bf7e | 4267 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) |
2d5b5a66 SY |
4268 | return -EINVAL; |
4269 | ||
4270 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) | |
4271 | return -EINVAL; | |
4272 | ||
4273 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) | |
4274 | /* Only support XCR0 currently */ | |
c67a04cb | 4275 | if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { |
2d5b5a66 | 4276 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
c67a04cb | 4277 | guest_xcrs->xcrs[i].value); |
2d5b5a66 SY |
4278 | break; |
4279 | } | |
4280 | if (r) | |
4281 | r = -EINVAL; | |
4282 | return r; | |
4283 | } | |
4284 | ||
1c0b28c2 EM |
4285 | /* |
4286 | * kvm_set_guest_paused() indicates to the guest kernel that it has been | |
4287 | * stopped by the hypervisor. This function will be called from the host only. | |
4288 | * EINVAL is returned when the host attempts to set the flag for a guest that | |
4289 | * does not support pv clocks. | |
4290 | */ | |
4291 | static int kvm_set_guest_paused(struct kvm_vcpu *vcpu) | |
4292 | { | |
0b79459b | 4293 | if (!vcpu->arch.pv_time_enabled) |
1c0b28c2 | 4294 | return -EINVAL; |
51d59c6b | 4295 | vcpu->arch.pvclock_set_guest_stopped_request = true; |
1c0b28c2 EM |
4296 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
4297 | return 0; | |
4298 | } | |
4299 | ||
5c919412 AS |
4300 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
4301 | struct kvm_enable_cap *cap) | |
4302 | { | |
57b119da VK |
4303 | int r; |
4304 | uint16_t vmcs_version; | |
4305 | void __user *user_ptr; | |
4306 | ||
5c919412 AS |
4307 | if (cap->flags) |
4308 | return -EINVAL; | |
4309 | ||
4310 | switch (cap->cap) { | |
efc479e6 RK |
4311 | case KVM_CAP_HYPERV_SYNIC2: |
4312 | if (cap->args[0]) | |
4313 | return -EINVAL; | |
b2869f28 GS |
4314 | /* fall through */ |
4315 | ||
5c919412 | 4316 | case KVM_CAP_HYPERV_SYNIC: |
546d87e5 WL |
4317 | if (!irqchip_in_kernel(vcpu->kvm)) |
4318 | return -EINVAL; | |
efc479e6 RK |
4319 | return kvm_hv_activate_synic(vcpu, cap->cap == |
4320 | KVM_CAP_HYPERV_SYNIC2); | |
57b119da | 4321 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
33b22172 | 4322 | if (!kvm_x86_ops.nested_ops->enable_evmcs) |
5158917c | 4323 | return -ENOTTY; |
33b22172 | 4324 | r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); |
57b119da VK |
4325 | if (!r) { |
4326 | user_ptr = (void __user *)(uintptr_t)cap->args[0]; | |
4327 | if (copy_to_user(user_ptr, &vmcs_version, | |
4328 | sizeof(vmcs_version))) | |
4329 | r = -EFAULT; | |
4330 | } | |
4331 | return r; | |
344c6c80 | 4332 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 4333 | if (!kvm_x86_ops.enable_direct_tlbflush) |
344c6c80 TL |
4334 | return -ENOTTY; |
4335 | ||
afaf0b2f | 4336 | return kvm_x86_ops.enable_direct_tlbflush(vcpu); |
57b119da | 4337 | |
5c919412 AS |
4338 | default: |
4339 | return -EINVAL; | |
4340 | } | |
4341 | } | |
4342 | ||
313a3dc7 CO |
4343 | long kvm_arch_vcpu_ioctl(struct file *filp, |
4344 | unsigned int ioctl, unsigned long arg) | |
4345 | { | |
4346 | struct kvm_vcpu *vcpu = filp->private_data; | |
4347 | void __user *argp = (void __user *)arg; | |
4348 | int r; | |
d1ac91d8 AK |
4349 | union { |
4350 | struct kvm_lapic_state *lapic; | |
4351 | struct kvm_xsave *xsave; | |
4352 | struct kvm_xcrs *xcrs; | |
4353 | void *buffer; | |
4354 | } u; | |
4355 | ||
9b062471 CD |
4356 | vcpu_load(vcpu); |
4357 | ||
d1ac91d8 | 4358 | u.buffer = NULL; |
313a3dc7 CO |
4359 | switch (ioctl) { |
4360 | case KVM_GET_LAPIC: { | |
2204ae3c | 4361 | r = -EINVAL; |
bce87cce | 4362 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 4363 | goto out; |
254272ce BG |
4364 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), |
4365 | GFP_KERNEL_ACCOUNT); | |
313a3dc7 | 4366 | |
b772ff36 | 4367 | r = -ENOMEM; |
d1ac91d8 | 4368 | if (!u.lapic) |
b772ff36 | 4369 | goto out; |
d1ac91d8 | 4370 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
313a3dc7 CO |
4371 | if (r) |
4372 | goto out; | |
4373 | r = -EFAULT; | |
d1ac91d8 | 4374 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
313a3dc7 CO |
4375 | goto out; |
4376 | r = 0; | |
4377 | break; | |
4378 | } | |
4379 | case KVM_SET_LAPIC: { | |
2204ae3c | 4380 | r = -EINVAL; |
bce87cce | 4381 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 4382 | goto out; |
ff5c2c03 | 4383 | u.lapic = memdup_user(argp, sizeof(*u.lapic)); |
9b062471 CD |
4384 | if (IS_ERR(u.lapic)) { |
4385 | r = PTR_ERR(u.lapic); | |
4386 | goto out_nofree; | |
4387 | } | |
ff5c2c03 | 4388 | |
d1ac91d8 | 4389 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
313a3dc7 CO |
4390 | break; |
4391 | } | |
f77bc6a4 ZX |
4392 | case KVM_INTERRUPT: { |
4393 | struct kvm_interrupt irq; | |
4394 | ||
4395 | r = -EFAULT; | |
0e96f31e | 4396 | if (copy_from_user(&irq, argp, sizeof(irq))) |
f77bc6a4 ZX |
4397 | goto out; |
4398 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
f77bc6a4 ZX |
4399 | break; |
4400 | } | |
c4abb7c9 JK |
4401 | case KVM_NMI: { |
4402 | r = kvm_vcpu_ioctl_nmi(vcpu); | |
c4abb7c9 JK |
4403 | break; |
4404 | } | |
f077825a PB |
4405 | case KVM_SMI: { |
4406 | r = kvm_vcpu_ioctl_smi(vcpu); | |
4407 | break; | |
4408 | } | |
313a3dc7 CO |
4409 | case KVM_SET_CPUID: { |
4410 | struct kvm_cpuid __user *cpuid_arg = argp; | |
4411 | struct kvm_cpuid cpuid; | |
4412 | ||
4413 | r = -EFAULT; | |
0e96f31e | 4414 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
313a3dc7 CO |
4415 | goto out; |
4416 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
313a3dc7 CO |
4417 | break; |
4418 | } | |
07716717 DK |
4419 | case KVM_SET_CPUID2: { |
4420 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4421 | struct kvm_cpuid2 cpuid; | |
4422 | ||
4423 | r = -EFAULT; | |
0e96f31e | 4424 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
4425 | goto out; |
4426 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
19355475 | 4427 | cpuid_arg->entries); |
07716717 DK |
4428 | break; |
4429 | } | |
4430 | case KVM_GET_CPUID2: { | |
4431 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4432 | struct kvm_cpuid2 cpuid; | |
4433 | ||
4434 | r = -EFAULT; | |
0e96f31e | 4435 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
4436 | goto out; |
4437 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
19355475 | 4438 | cpuid_arg->entries); |
07716717 DK |
4439 | if (r) |
4440 | goto out; | |
4441 | r = -EFAULT; | |
0e96f31e | 4442 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
07716717 DK |
4443 | goto out; |
4444 | r = 0; | |
4445 | break; | |
4446 | } | |
801e459a TL |
4447 | case KVM_GET_MSRS: { |
4448 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
609e36d3 | 4449 | r = msr_io(vcpu, argp, do_get_msr, 1); |
801e459a | 4450 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 4451 | break; |
801e459a TL |
4452 | } |
4453 | case KVM_SET_MSRS: { | |
4454 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
313a3dc7 | 4455 | r = msr_io(vcpu, argp, do_set_msr, 0); |
801e459a | 4456 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 4457 | break; |
801e459a | 4458 | } |
b209749f AK |
4459 | case KVM_TPR_ACCESS_REPORTING: { |
4460 | struct kvm_tpr_access_ctl tac; | |
4461 | ||
4462 | r = -EFAULT; | |
0e96f31e | 4463 | if (copy_from_user(&tac, argp, sizeof(tac))) |
b209749f AK |
4464 | goto out; |
4465 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); | |
4466 | if (r) | |
4467 | goto out; | |
4468 | r = -EFAULT; | |
0e96f31e | 4469 | if (copy_to_user(argp, &tac, sizeof(tac))) |
b209749f AK |
4470 | goto out; |
4471 | r = 0; | |
4472 | break; | |
4473 | }; | |
b93463aa AK |
4474 | case KVM_SET_VAPIC_ADDR: { |
4475 | struct kvm_vapic_addr va; | |
7301d6ab | 4476 | int idx; |
b93463aa AK |
4477 | |
4478 | r = -EINVAL; | |
35754c98 | 4479 | if (!lapic_in_kernel(vcpu)) |
b93463aa AK |
4480 | goto out; |
4481 | r = -EFAULT; | |
0e96f31e | 4482 | if (copy_from_user(&va, argp, sizeof(va))) |
b93463aa | 4483 | goto out; |
7301d6ab | 4484 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
fda4e2e8 | 4485 | r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
7301d6ab | 4486 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b93463aa AK |
4487 | break; |
4488 | } | |
890ca9ae HY |
4489 | case KVM_X86_SETUP_MCE: { |
4490 | u64 mcg_cap; | |
4491 | ||
4492 | r = -EFAULT; | |
0e96f31e | 4493 | if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap))) |
890ca9ae HY |
4494 | goto out; |
4495 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); | |
4496 | break; | |
4497 | } | |
4498 | case KVM_X86_SET_MCE: { | |
4499 | struct kvm_x86_mce mce; | |
4500 | ||
4501 | r = -EFAULT; | |
0e96f31e | 4502 | if (copy_from_user(&mce, argp, sizeof(mce))) |
890ca9ae HY |
4503 | goto out; |
4504 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); | |
4505 | break; | |
4506 | } | |
3cfc3092 JK |
4507 | case KVM_GET_VCPU_EVENTS: { |
4508 | struct kvm_vcpu_events events; | |
4509 | ||
4510 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); | |
4511 | ||
4512 | r = -EFAULT; | |
4513 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) | |
4514 | break; | |
4515 | r = 0; | |
4516 | break; | |
4517 | } | |
4518 | case KVM_SET_VCPU_EVENTS: { | |
4519 | struct kvm_vcpu_events events; | |
4520 | ||
4521 | r = -EFAULT; | |
4522 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) | |
4523 | break; | |
4524 | ||
4525 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); | |
4526 | break; | |
4527 | } | |
a1efbe77 JK |
4528 | case KVM_GET_DEBUGREGS: { |
4529 | struct kvm_debugregs dbgregs; | |
4530 | ||
4531 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); | |
4532 | ||
4533 | r = -EFAULT; | |
4534 | if (copy_to_user(argp, &dbgregs, | |
4535 | sizeof(struct kvm_debugregs))) | |
4536 | break; | |
4537 | r = 0; | |
4538 | break; | |
4539 | } | |
4540 | case KVM_SET_DEBUGREGS: { | |
4541 | struct kvm_debugregs dbgregs; | |
4542 | ||
4543 | r = -EFAULT; | |
4544 | if (copy_from_user(&dbgregs, argp, | |
4545 | sizeof(struct kvm_debugregs))) | |
4546 | break; | |
4547 | ||
4548 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); | |
4549 | break; | |
4550 | } | |
2d5b5a66 | 4551 | case KVM_GET_XSAVE: { |
254272ce | 4552 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 4553 | r = -ENOMEM; |
d1ac91d8 | 4554 | if (!u.xsave) |
2d5b5a66 SY |
4555 | break; |
4556 | ||
d1ac91d8 | 4557 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
4558 | |
4559 | r = -EFAULT; | |
d1ac91d8 | 4560 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
2d5b5a66 SY |
4561 | break; |
4562 | r = 0; | |
4563 | break; | |
4564 | } | |
4565 | case KVM_SET_XSAVE: { | |
ff5c2c03 | 4566 | u.xsave = memdup_user(argp, sizeof(*u.xsave)); |
9b062471 CD |
4567 | if (IS_ERR(u.xsave)) { |
4568 | r = PTR_ERR(u.xsave); | |
4569 | goto out_nofree; | |
4570 | } | |
2d5b5a66 | 4571 | |
d1ac91d8 | 4572 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
4573 | break; |
4574 | } | |
4575 | case KVM_GET_XCRS: { | |
254272ce | 4576 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 4577 | r = -ENOMEM; |
d1ac91d8 | 4578 | if (!u.xcrs) |
2d5b5a66 SY |
4579 | break; |
4580 | ||
d1ac91d8 | 4581 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
4582 | |
4583 | r = -EFAULT; | |
d1ac91d8 | 4584 | if (copy_to_user(argp, u.xcrs, |
2d5b5a66 SY |
4585 | sizeof(struct kvm_xcrs))) |
4586 | break; | |
4587 | r = 0; | |
4588 | break; | |
4589 | } | |
4590 | case KVM_SET_XCRS: { | |
ff5c2c03 | 4591 | u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); |
9b062471 CD |
4592 | if (IS_ERR(u.xcrs)) { |
4593 | r = PTR_ERR(u.xcrs); | |
4594 | goto out_nofree; | |
4595 | } | |
2d5b5a66 | 4596 | |
d1ac91d8 | 4597 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
4598 | break; |
4599 | } | |
92a1f12d JR |
4600 | case KVM_SET_TSC_KHZ: { |
4601 | u32 user_tsc_khz; | |
4602 | ||
4603 | r = -EINVAL; | |
92a1f12d JR |
4604 | user_tsc_khz = (u32)arg; |
4605 | ||
4606 | if (user_tsc_khz >= kvm_max_guest_tsc_khz) | |
4607 | goto out; | |
4608 | ||
cc578287 ZA |
4609 | if (user_tsc_khz == 0) |
4610 | user_tsc_khz = tsc_khz; | |
4611 | ||
381d585c HZ |
4612 | if (!kvm_set_tsc_khz(vcpu, user_tsc_khz)) |
4613 | r = 0; | |
92a1f12d | 4614 | |
92a1f12d JR |
4615 | goto out; |
4616 | } | |
4617 | case KVM_GET_TSC_KHZ: { | |
cc578287 | 4618 | r = vcpu->arch.virtual_tsc_khz; |
92a1f12d JR |
4619 | goto out; |
4620 | } | |
1c0b28c2 EM |
4621 | case KVM_KVMCLOCK_CTRL: { |
4622 | r = kvm_set_guest_paused(vcpu); | |
4623 | goto out; | |
4624 | } | |
5c919412 AS |
4625 | case KVM_ENABLE_CAP: { |
4626 | struct kvm_enable_cap cap; | |
4627 | ||
4628 | r = -EFAULT; | |
4629 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
4630 | goto out; | |
4631 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
4632 | break; | |
4633 | } | |
8fcc4b59 JM |
4634 | case KVM_GET_NESTED_STATE: { |
4635 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
4636 | u32 user_data_size; | |
4637 | ||
4638 | r = -EINVAL; | |
33b22172 | 4639 | if (!kvm_x86_ops.nested_ops->get_state) |
8fcc4b59 JM |
4640 | break; |
4641 | ||
4642 | BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); | |
26b471c7 | 4643 | r = -EFAULT; |
8fcc4b59 | 4644 | if (get_user(user_data_size, &user_kvm_nested_state->size)) |
26b471c7 | 4645 | break; |
8fcc4b59 | 4646 | |
33b22172 PB |
4647 | r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, |
4648 | user_data_size); | |
8fcc4b59 | 4649 | if (r < 0) |
26b471c7 | 4650 | break; |
8fcc4b59 JM |
4651 | |
4652 | if (r > user_data_size) { | |
4653 | if (put_user(r, &user_kvm_nested_state->size)) | |
26b471c7 LA |
4654 | r = -EFAULT; |
4655 | else | |
4656 | r = -E2BIG; | |
4657 | break; | |
8fcc4b59 | 4658 | } |
26b471c7 | 4659 | |
8fcc4b59 JM |
4660 | r = 0; |
4661 | break; | |
4662 | } | |
4663 | case KVM_SET_NESTED_STATE: { | |
4664 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
4665 | struct kvm_nested_state kvm_state; | |
ad5996d9 | 4666 | int idx; |
8fcc4b59 JM |
4667 | |
4668 | r = -EINVAL; | |
33b22172 | 4669 | if (!kvm_x86_ops.nested_ops->set_state) |
8fcc4b59 JM |
4670 | break; |
4671 | ||
26b471c7 | 4672 | r = -EFAULT; |
8fcc4b59 | 4673 | if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state))) |
26b471c7 | 4674 | break; |
8fcc4b59 | 4675 | |
26b471c7 | 4676 | r = -EINVAL; |
8fcc4b59 | 4677 | if (kvm_state.size < sizeof(kvm_state)) |
26b471c7 | 4678 | break; |
8fcc4b59 JM |
4679 | |
4680 | if (kvm_state.flags & | |
8cab6507 | 4681 | ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE |
cc440cda PB |
4682 | | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING |
4683 | | KVM_STATE_NESTED_GIF_SET)) | |
26b471c7 | 4684 | break; |
8fcc4b59 JM |
4685 | |
4686 | /* nested_run_pending implies guest_mode. */ | |
8cab6507 VK |
4687 | if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING) |
4688 | && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE)) | |
26b471c7 | 4689 | break; |
8fcc4b59 | 4690 | |
ad5996d9 | 4691 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
33b22172 | 4692 | r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); |
ad5996d9 | 4693 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8fcc4b59 JM |
4694 | break; |
4695 | } | |
2bc39970 VK |
4696 | case KVM_GET_SUPPORTED_HV_CPUID: { |
4697 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4698 | struct kvm_cpuid2 cpuid; | |
4699 | ||
4700 | r = -EFAULT; | |
4701 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) | |
4702 | goto out; | |
4703 | ||
4704 | r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid, | |
4705 | cpuid_arg->entries); | |
4706 | if (r) | |
4707 | goto out; | |
4708 | ||
4709 | r = -EFAULT; | |
4710 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) | |
4711 | goto out; | |
4712 | r = 0; | |
4713 | break; | |
4714 | } | |
313a3dc7 CO |
4715 | default: |
4716 | r = -EINVAL; | |
4717 | } | |
4718 | out: | |
d1ac91d8 | 4719 | kfree(u.buffer); |
9b062471 CD |
4720 | out_nofree: |
4721 | vcpu_put(vcpu); | |
313a3dc7 CO |
4722 | return r; |
4723 | } | |
4724 | ||
1499fa80 | 4725 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
5b1c1493 CO |
4726 | { |
4727 | return VM_FAULT_SIGBUS; | |
4728 | } | |
4729 | ||
1fe779f8 CO |
4730 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
4731 | { | |
4732 | int ret; | |
4733 | ||
4734 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
951179ce | 4735 | return -EINVAL; |
afaf0b2f | 4736 | ret = kvm_x86_ops.set_tss_addr(kvm, addr); |
1fe779f8 CO |
4737 | return ret; |
4738 | } | |
4739 | ||
b927a3ce SY |
4740 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
4741 | u64 ident_addr) | |
4742 | { | |
afaf0b2f | 4743 | return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr); |
b927a3ce SY |
4744 | } |
4745 | ||
1fe779f8 | 4746 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
bc8a3d89 | 4747 | unsigned long kvm_nr_mmu_pages) |
1fe779f8 CO |
4748 | { |
4749 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
4750 | return -EINVAL; | |
4751 | ||
79fac95e | 4752 | mutex_lock(&kvm->slots_lock); |
1fe779f8 CO |
4753 | |
4754 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
f05e70ac | 4755 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
1fe779f8 | 4756 | |
79fac95e | 4757 | mutex_unlock(&kvm->slots_lock); |
1fe779f8 CO |
4758 | return 0; |
4759 | } | |
4760 | ||
bc8a3d89 | 4761 | static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
1fe779f8 | 4762 | { |
39de71ec | 4763 | return kvm->arch.n_max_mmu_pages; |
1fe779f8 CO |
4764 | } |
4765 | ||
1fe779f8 CO |
4766 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
4767 | { | |
90bca052 | 4768 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
4769 | int r; |
4770 | ||
4771 | r = 0; | |
4772 | switch (chip->chip_id) { | |
4773 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 | 4774 | memcpy(&chip->chip.pic, &pic->pics[0], |
1fe779f8 CO |
4775 | sizeof(struct kvm_pic_state)); |
4776 | break; | |
4777 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 | 4778 | memcpy(&chip->chip.pic, &pic->pics[1], |
1fe779f8 CO |
4779 | sizeof(struct kvm_pic_state)); |
4780 | break; | |
4781 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 4782 | kvm_get_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
4783 | break; |
4784 | default: | |
4785 | r = -EINVAL; | |
4786 | break; | |
4787 | } | |
4788 | return r; | |
4789 | } | |
4790 | ||
4791 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
4792 | { | |
90bca052 | 4793 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
4794 | int r; |
4795 | ||
4796 | r = 0; | |
4797 | switch (chip->chip_id) { | |
4798 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 DH |
4799 | spin_lock(&pic->lock); |
4800 | memcpy(&pic->pics[0], &chip->chip.pic, | |
1fe779f8 | 4801 | sizeof(struct kvm_pic_state)); |
90bca052 | 4802 | spin_unlock(&pic->lock); |
1fe779f8 CO |
4803 | break; |
4804 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 DH |
4805 | spin_lock(&pic->lock); |
4806 | memcpy(&pic->pics[1], &chip->chip.pic, | |
1fe779f8 | 4807 | sizeof(struct kvm_pic_state)); |
90bca052 | 4808 | spin_unlock(&pic->lock); |
1fe779f8 CO |
4809 | break; |
4810 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 4811 | kvm_set_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
4812 | break; |
4813 | default: | |
4814 | r = -EINVAL; | |
4815 | break; | |
4816 | } | |
90bca052 | 4817 | kvm_pic_update_irq(pic); |
1fe779f8 CO |
4818 | return r; |
4819 | } | |
4820 | ||
e0f63cb9 SY |
4821 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
4822 | { | |
34f3941c RK |
4823 | struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; |
4824 | ||
4825 | BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); | |
4826 | ||
4827 | mutex_lock(&kps->lock); | |
4828 | memcpy(ps, &kps->channels, sizeof(*ps)); | |
4829 | mutex_unlock(&kps->lock); | |
2da29bcc | 4830 | return 0; |
e0f63cb9 SY |
4831 | } |
4832 | ||
4833 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |
4834 | { | |
0185604c | 4835 | int i; |
09edea72 RK |
4836 | struct kvm_pit *pit = kvm->arch.vpit; |
4837 | ||
4838 | mutex_lock(&pit->pit_state.lock); | |
34f3941c | 4839 | memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); |
0185604c | 4840 | for (i = 0; i < 3; i++) |
09edea72 RK |
4841 | kvm_pit_load_count(pit, i, ps->channels[i].count, 0); |
4842 | mutex_unlock(&pit->pit_state.lock); | |
2da29bcc | 4843 | return 0; |
e9f42757 BK |
4844 | } |
4845 | ||
4846 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
4847 | { | |
e9f42757 BK |
4848 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
4849 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, | |
4850 | sizeof(ps->channels)); | |
4851 | ps->flags = kvm->arch.vpit->pit_state.flags; | |
4852 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); | |
97e69aa6 | 4853 | memset(&ps->reserved, 0, sizeof(ps->reserved)); |
2da29bcc | 4854 | return 0; |
e9f42757 BK |
4855 | } |
4856 | ||
4857 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
4858 | { | |
2da29bcc | 4859 | int start = 0; |
0185604c | 4860 | int i; |
e9f42757 | 4861 | u32 prev_legacy, cur_legacy; |
09edea72 RK |
4862 | struct kvm_pit *pit = kvm->arch.vpit; |
4863 | ||
4864 | mutex_lock(&pit->pit_state.lock); | |
4865 | prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; | |
e9f42757 BK |
4866 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
4867 | if (!prev_legacy && cur_legacy) | |
4868 | start = 1; | |
09edea72 RK |
4869 | memcpy(&pit->pit_state.channels, &ps->channels, |
4870 | sizeof(pit->pit_state.channels)); | |
4871 | pit->pit_state.flags = ps->flags; | |
0185604c | 4872 | for (i = 0; i < 3; i++) |
09edea72 | 4873 | kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, |
e5e57e7a | 4874 | start && i == 0); |
09edea72 | 4875 | mutex_unlock(&pit->pit_state.lock); |
2da29bcc | 4876 | return 0; |
e0f63cb9 SY |
4877 | } |
4878 | ||
52d939a0 MT |
4879 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
4880 | struct kvm_reinject_control *control) | |
4881 | { | |
71474e2f RK |
4882 | struct kvm_pit *pit = kvm->arch.vpit; |
4883 | ||
71474e2f RK |
4884 | /* pit->pit_state.lock was overloaded to prevent userspace from getting |
4885 | * an inconsistent state after running multiple KVM_REINJECT_CONTROL | |
4886 | * ioctls in parallel. Use a separate lock if that ioctl isn't rare. | |
4887 | */ | |
4888 | mutex_lock(&pit->pit_state.lock); | |
4889 | kvm_pit_set_reinject(pit, control->pit_reinject); | |
4890 | mutex_unlock(&pit->pit_state.lock); | |
b39c90b6 | 4891 | |
52d939a0 MT |
4892 | return 0; |
4893 | } | |
4894 | ||
0dff0846 | 4895 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
5bb064dc | 4896 | { |
88178fd4 KH |
4897 | /* |
4898 | * Flush potentially hardware-cached dirty pages to dirty_bitmap. | |
4899 | */ | |
afaf0b2f SC |
4900 | if (kvm_x86_ops.flush_log_dirty) |
4901 | kvm_x86_ops.flush_log_dirty(kvm); | |
5bb064dc ZX |
4902 | } |
4903 | ||
aa2fbe6d YZ |
4904 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
4905 | bool line_status) | |
23d43cf9 CD |
4906 | { |
4907 | if (!irqchip_in_kernel(kvm)) | |
4908 | return -ENXIO; | |
4909 | ||
4910 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, | |
aa2fbe6d YZ |
4911 | irq_event->irq, irq_event->level, |
4912 | line_status); | |
23d43cf9 CD |
4913 | return 0; |
4914 | } | |
4915 | ||
e5d83c74 PB |
4916 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
4917 | struct kvm_enable_cap *cap) | |
90de4a18 NA |
4918 | { |
4919 | int r; | |
4920 | ||
4921 | if (cap->flags) | |
4922 | return -EINVAL; | |
4923 | ||
4924 | switch (cap->cap) { | |
4925 | case KVM_CAP_DISABLE_QUIRKS: | |
4926 | kvm->arch.disabled_quirks = cap->args[0]; | |
4927 | r = 0; | |
4928 | break; | |
49df6397 SR |
4929 | case KVM_CAP_SPLIT_IRQCHIP: { |
4930 | mutex_lock(&kvm->lock); | |
b053b2ae SR |
4931 | r = -EINVAL; |
4932 | if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) | |
4933 | goto split_irqchip_unlock; | |
49df6397 SR |
4934 | r = -EEXIST; |
4935 | if (irqchip_in_kernel(kvm)) | |
4936 | goto split_irqchip_unlock; | |
557abc40 | 4937 | if (kvm->created_vcpus) |
49df6397 SR |
4938 | goto split_irqchip_unlock; |
4939 | r = kvm_setup_empty_irq_routing(kvm); | |
5c0aea0e | 4940 | if (r) |
49df6397 SR |
4941 | goto split_irqchip_unlock; |
4942 | /* Pairs with irqchip_in_kernel. */ | |
4943 | smp_wmb(); | |
49776faf | 4944 | kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; |
b053b2ae | 4945 | kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; |
49df6397 SR |
4946 | r = 0; |
4947 | split_irqchip_unlock: | |
4948 | mutex_unlock(&kvm->lock); | |
4949 | break; | |
4950 | } | |
37131313 RK |
4951 | case KVM_CAP_X2APIC_API: |
4952 | r = -EINVAL; | |
4953 | if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) | |
4954 | break; | |
4955 | ||
4956 | if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) | |
4957 | kvm->arch.x2apic_format = true; | |
c519265f RK |
4958 | if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
4959 | kvm->arch.x2apic_broadcast_quirk_disabled = true; | |
37131313 RK |
4960 | |
4961 | r = 0; | |
4962 | break; | |
4d5422ce WL |
4963 | case KVM_CAP_X86_DISABLE_EXITS: |
4964 | r = -EINVAL; | |
4965 | if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) | |
4966 | break; | |
4967 | ||
4968 | if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && | |
4969 | kvm_can_mwait_in_guest()) | |
4970 | kvm->arch.mwait_in_guest = true; | |
766d3571 | 4971 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) |
caa057a2 | 4972 | kvm->arch.hlt_in_guest = true; |
b31c114b WL |
4973 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) |
4974 | kvm->arch.pause_in_guest = true; | |
b5170063 WL |
4975 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) |
4976 | kvm->arch.cstate_in_guest = true; | |
4d5422ce WL |
4977 | r = 0; |
4978 | break; | |
6fbbde9a DS |
4979 | case KVM_CAP_MSR_PLATFORM_INFO: |
4980 | kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; | |
4981 | r = 0; | |
c4f55198 JM |
4982 | break; |
4983 | case KVM_CAP_EXCEPTION_PAYLOAD: | |
4984 | kvm->arch.exception_payload_enabled = cap->args[0]; | |
4985 | r = 0; | |
6fbbde9a | 4986 | break; |
90de4a18 NA |
4987 | default: |
4988 | r = -EINVAL; | |
4989 | break; | |
4990 | } | |
4991 | return r; | |
4992 | } | |
4993 | ||
1fe779f8 CO |
4994 | long kvm_arch_vm_ioctl(struct file *filp, |
4995 | unsigned int ioctl, unsigned long arg) | |
4996 | { | |
4997 | struct kvm *kvm = filp->private_data; | |
4998 | void __user *argp = (void __user *)arg; | |
367e1319 | 4999 | int r = -ENOTTY; |
f0d66275 DH |
5000 | /* |
5001 | * This union makes it completely explicit to gcc-3.x | |
5002 | * that these two variables' stack usage should be | |
5003 | * combined, not added together. | |
5004 | */ | |
5005 | union { | |
5006 | struct kvm_pit_state ps; | |
e9f42757 | 5007 | struct kvm_pit_state2 ps2; |
c5ff41ce | 5008 | struct kvm_pit_config pit_config; |
f0d66275 | 5009 | } u; |
1fe779f8 CO |
5010 | |
5011 | switch (ioctl) { | |
5012 | case KVM_SET_TSS_ADDR: | |
5013 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
1fe779f8 | 5014 | break; |
b927a3ce SY |
5015 | case KVM_SET_IDENTITY_MAP_ADDR: { |
5016 | u64 ident_addr; | |
5017 | ||
1af1ac91 DH |
5018 | mutex_lock(&kvm->lock); |
5019 | r = -EINVAL; | |
5020 | if (kvm->created_vcpus) | |
5021 | goto set_identity_unlock; | |
b927a3ce | 5022 | r = -EFAULT; |
0e96f31e | 5023 | if (copy_from_user(&ident_addr, argp, sizeof(ident_addr))) |
1af1ac91 | 5024 | goto set_identity_unlock; |
b927a3ce | 5025 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
1af1ac91 DH |
5026 | set_identity_unlock: |
5027 | mutex_unlock(&kvm->lock); | |
b927a3ce SY |
5028 | break; |
5029 | } | |
1fe779f8 CO |
5030 | case KVM_SET_NR_MMU_PAGES: |
5031 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
1fe779f8 CO |
5032 | break; |
5033 | case KVM_GET_NR_MMU_PAGES: | |
5034 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
5035 | break; | |
3ddea128 | 5036 | case KVM_CREATE_IRQCHIP: { |
3ddea128 | 5037 | mutex_lock(&kvm->lock); |
09941366 | 5038 | |
3ddea128 | 5039 | r = -EEXIST; |
35e6eaa3 | 5040 | if (irqchip_in_kernel(kvm)) |
3ddea128 | 5041 | goto create_irqchip_unlock; |
09941366 | 5042 | |
3e515705 | 5043 | r = -EINVAL; |
557abc40 | 5044 | if (kvm->created_vcpus) |
3e515705 | 5045 | goto create_irqchip_unlock; |
09941366 RK |
5046 | |
5047 | r = kvm_pic_init(kvm); | |
5048 | if (r) | |
3ddea128 | 5049 | goto create_irqchip_unlock; |
09941366 RK |
5050 | |
5051 | r = kvm_ioapic_init(kvm); | |
5052 | if (r) { | |
09941366 | 5053 | kvm_pic_destroy(kvm); |
3ddea128 | 5054 | goto create_irqchip_unlock; |
09941366 RK |
5055 | } |
5056 | ||
399ec807 AK |
5057 | r = kvm_setup_default_irq_routing(kvm); |
5058 | if (r) { | |
72bb2fcd | 5059 | kvm_ioapic_destroy(kvm); |
09941366 | 5060 | kvm_pic_destroy(kvm); |
71ba994c | 5061 | goto create_irqchip_unlock; |
399ec807 | 5062 | } |
49776faf | 5063 | /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ |
71ba994c | 5064 | smp_wmb(); |
49776faf | 5065 | kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; |
3ddea128 MT |
5066 | create_irqchip_unlock: |
5067 | mutex_unlock(&kvm->lock); | |
1fe779f8 | 5068 | break; |
3ddea128 | 5069 | } |
7837699f | 5070 | case KVM_CREATE_PIT: |
c5ff41ce JK |
5071 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
5072 | goto create_pit; | |
5073 | case KVM_CREATE_PIT2: | |
5074 | r = -EFAULT; | |
5075 | if (copy_from_user(&u.pit_config, argp, | |
5076 | sizeof(struct kvm_pit_config))) | |
5077 | goto out; | |
5078 | create_pit: | |
250715a6 | 5079 | mutex_lock(&kvm->lock); |
269e05e4 AK |
5080 | r = -EEXIST; |
5081 | if (kvm->arch.vpit) | |
5082 | goto create_pit_unlock; | |
7837699f | 5083 | r = -ENOMEM; |
c5ff41ce | 5084 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
7837699f SY |
5085 | if (kvm->arch.vpit) |
5086 | r = 0; | |
269e05e4 | 5087 | create_pit_unlock: |
250715a6 | 5088 | mutex_unlock(&kvm->lock); |
7837699f | 5089 | break; |
1fe779f8 CO |
5090 | case KVM_GET_IRQCHIP: { |
5091 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 5092 | struct kvm_irqchip *chip; |
1fe779f8 | 5093 | |
ff5c2c03 SL |
5094 | chip = memdup_user(argp, sizeof(*chip)); |
5095 | if (IS_ERR(chip)) { | |
5096 | r = PTR_ERR(chip); | |
1fe779f8 | 5097 | goto out; |
ff5c2c03 SL |
5098 | } |
5099 | ||
1fe779f8 | 5100 | r = -ENXIO; |
826da321 | 5101 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
5102 | goto get_irqchip_out; |
5103 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); | |
1fe779f8 | 5104 | if (r) |
f0d66275 | 5105 | goto get_irqchip_out; |
1fe779f8 | 5106 | r = -EFAULT; |
0e96f31e | 5107 | if (copy_to_user(argp, chip, sizeof(*chip))) |
f0d66275 | 5108 | goto get_irqchip_out; |
1fe779f8 | 5109 | r = 0; |
f0d66275 DH |
5110 | get_irqchip_out: |
5111 | kfree(chip); | |
1fe779f8 CO |
5112 | break; |
5113 | } | |
5114 | case KVM_SET_IRQCHIP: { | |
5115 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 5116 | struct kvm_irqchip *chip; |
1fe779f8 | 5117 | |
ff5c2c03 SL |
5118 | chip = memdup_user(argp, sizeof(*chip)); |
5119 | if (IS_ERR(chip)) { | |
5120 | r = PTR_ERR(chip); | |
1fe779f8 | 5121 | goto out; |
ff5c2c03 SL |
5122 | } |
5123 | ||
1fe779f8 | 5124 | r = -ENXIO; |
826da321 | 5125 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
5126 | goto set_irqchip_out; |
5127 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); | |
f0d66275 DH |
5128 | set_irqchip_out: |
5129 | kfree(chip); | |
1fe779f8 CO |
5130 | break; |
5131 | } | |
e0f63cb9 | 5132 | case KVM_GET_PIT: { |
e0f63cb9 | 5133 | r = -EFAULT; |
f0d66275 | 5134 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
5135 | goto out; |
5136 | r = -ENXIO; | |
5137 | if (!kvm->arch.vpit) | |
5138 | goto out; | |
f0d66275 | 5139 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
e0f63cb9 SY |
5140 | if (r) |
5141 | goto out; | |
5142 | r = -EFAULT; | |
f0d66275 | 5143 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
5144 | goto out; |
5145 | r = 0; | |
5146 | break; | |
5147 | } | |
5148 | case KVM_SET_PIT: { | |
e0f63cb9 | 5149 | r = -EFAULT; |
0e96f31e | 5150 | if (copy_from_user(&u.ps, argp, sizeof(u.ps))) |
e0f63cb9 | 5151 | goto out; |
7289fdb5 | 5152 | mutex_lock(&kvm->lock); |
e0f63cb9 SY |
5153 | r = -ENXIO; |
5154 | if (!kvm->arch.vpit) | |
7289fdb5 | 5155 | goto set_pit_out; |
f0d66275 | 5156 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
7289fdb5 SR |
5157 | set_pit_out: |
5158 | mutex_unlock(&kvm->lock); | |
e0f63cb9 SY |
5159 | break; |
5160 | } | |
e9f42757 BK |
5161 | case KVM_GET_PIT2: { |
5162 | r = -ENXIO; | |
5163 | if (!kvm->arch.vpit) | |
5164 | goto out; | |
5165 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); | |
5166 | if (r) | |
5167 | goto out; | |
5168 | r = -EFAULT; | |
5169 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) | |
5170 | goto out; | |
5171 | r = 0; | |
5172 | break; | |
5173 | } | |
5174 | case KVM_SET_PIT2: { | |
5175 | r = -EFAULT; | |
5176 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) | |
5177 | goto out; | |
7289fdb5 | 5178 | mutex_lock(&kvm->lock); |
e9f42757 BK |
5179 | r = -ENXIO; |
5180 | if (!kvm->arch.vpit) | |
7289fdb5 | 5181 | goto set_pit2_out; |
e9f42757 | 5182 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
7289fdb5 SR |
5183 | set_pit2_out: |
5184 | mutex_unlock(&kvm->lock); | |
e9f42757 BK |
5185 | break; |
5186 | } | |
52d939a0 MT |
5187 | case KVM_REINJECT_CONTROL: { |
5188 | struct kvm_reinject_control control; | |
5189 | r = -EFAULT; | |
5190 | if (copy_from_user(&control, argp, sizeof(control))) | |
5191 | goto out; | |
cad23e72 ML |
5192 | r = -ENXIO; |
5193 | if (!kvm->arch.vpit) | |
5194 | goto out; | |
52d939a0 | 5195 | r = kvm_vm_ioctl_reinject(kvm, &control); |
52d939a0 MT |
5196 | break; |
5197 | } | |
d71ba788 PB |
5198 | case KVM_SET_BOOT_CPU_ID: |
5199 | r = 0; | |
5200 | mutex_lock(&kvm->lock); | |
557abc40 | 5201 | if (kvm->created_vcpus) |
d71ba788 PB |
5202 | r = -EBUSY; |
5203 | else | |
5204 | kvm->arch.bsp_vcpu_id = arg; | |
5205 | mutex_unlock(&kvm->lock); | |
5206 | break; | |
ffde22ac | 5207 | case KVM_XEN_HVM_CONFIG: { |
51776043 | 5208 | struct kvm_xen_hvm_config xhc; |
ffde22ac | 5209 | r = -EFAULT; |
51776043 | 5210 | if (copy_from_user(&xhc, argp, sizeof(xhc))) |
ffde22ac ES |
5211 | goto out; |
5212 | r = -EINVAL; | |
51776043 | 5213 | if (xhc.flags) |
ffde22ac | 5214 | goto out; |
51776043 | 5215 | memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc)); |
ffde22ac ES |
5216 | r = 0; |
5217 | break; | |
5218 | } | |
afbcf7ab | 5219 | case KVM_SET_CLOCK: { |
afbcf7ab GC |
5220 | struct kvm_clock_data user_ns; |
5221 | u64 now_ns; | |
afbcf7ab GC |
5222 | |
5223 | r = -EFAULT; | |
5224 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) | |
5225 | goto out; | |
5226 | ||
5227 | r = -EINVAL; | |
5228 | if (user_ns.flags) | |
5229 | goto out; | |
5230 | ||
5231 | r = 0; | |
0bc48bea RK |
5232 | /* |
5233 | * TODO: userspace has to take care of races with VCPU_RUN, so | |
5234 | * kvm_gen_update_masterclock() can be cut down to locked | |
5235 | * pvclock_update_vm_gtod_copy(). | |
5236 | */ | |
5237 | kvm_gen_update_masterclock(kvm); | |
e891a32e | 5238 | now_ns = get_kvmclock_ns(kvm); |
108b249c | 5239 | kvm->arch.kvmclock_offset += user_ns.clock - now_ns; |
0bc48bea | 5240 | kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); |
afbcf7ab GC |
5241 | break; |
5242 | } | |
5243 | case KVM_GET_CLOCK: { | |
afbcf7ab GC |
5244 | struct kvm_clock_data user_ns; |
5245 | u64 now_ns; | |
5246 | ||
e891a32e | 5247 | now_ns = get_kvmclock_ns(kvm); |
108b249c | 5248 | user_ns.clock = now_ns; |
e3fd9a93 | 5249 | user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; |
97e69aa6 | 5250 | memset(&user_ns.pad, 0, sizeof(user_ns.pad)); |
afbcf7ab GC |
5251 | |
5252 | r = -EFAULT; | |
5253 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) | |
5254 | goto out; | |
5255 | r = 0; | |
5256 | break; | |
5257 | } | |
5acc5c06 BS |
5258 | case KVM_MEMORY_ENCRYPT_OP: { |
5259 | r = -ENOTTY; | |
afaf0b2f SC |
5260 | if (kvm_x86_ops.mem_enc_op) |
5261 | r = kvm_x86_ops.mem_enc_op(kvm, argp); | |
5acc5c06 BS |
5262 | break; |
5263 | } | |
69eaedee BS |
5264 | case KVM_MEMORY_ENCRYPT_REG_REGION: { |
5265 | struct kvm_enc_region region; | |
5266 | ||
5267 | r = -EFAULT; | |
5268 | if (copy_from_user(®ion, argp, sizeof(region))) | |
5269 | goto out; | |
5270 | ||
5271 | r = -ENOTTY; | |
afaf0b2f SC |
5272 | if (kvm_x86_ops.mem_enc_reg_region) |
5273 | r = kvm_x86_ops.mem_enc_reg_region(kvm, ®ion); | |
69eaedee BS |
5274 | break; |
5275 | } | |
5276 | case KVM_MEMORY_ENCRYPT_UNREG_REGION: { | |
5277 | struct kvm_enc_region region; | |
5278 | ||
5279 | r = -EFAULT; | |
5280 | if (copy_from_user(®ion, argp, sizeof(region))) | |
5281 | goto out; | |
5282 | ||
5283 | r = -ENOTTY; | |
afaf0b2f SC |
5284 | if (kvm_x86_ops.mem_enc_unreg_region) |
5285 | r = kvm_x86_ops.mem_enc_unreg_region(kvm, ®ion); | |
69eaedee BS |
5286 | break; |
5287 | } | |
faeb7833 RK |
5288 | case KVM_HYPERV_EVENTFD: { |
5289 | struct kvm_hyperv_eventfd hvevfd; | |
5290 | ||
5291 | r = -EFAULT; | |
5292 | if (copy_from_user(&hvevfd, argp, sizeof(hvevfd))) | |
5293 | goto out; | |
5294 | r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd); | |
5295 | break; | |
5296 | } | |
66bb8a06 EH |
5297 | case KVM_SET_PMU_EVENT_FILTER: |
5298 | r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); | |
5299 | break; | |
1fe779f8 | 5300 | default: |
ad6260da | 5301 | r = -ENOTTY; |
1fe779f8 CO |
5302 | } |
5303 | out: | |
5304 | return r; | |
5305 | } | |
5306 | ||
a16b043c | 5307 | static void kvm_init_msr_list(void) |
043405e1 | 5308 | { |
24c29b7a | 5309 | struct x86_pmu_capability x86_pmu; |
043405e1 | 5310 | u32 dummy[2]; |
7a5ee6ed | 5311 | unsigned i; |
043405e1 | 5312 | |
e2ada66e | 5313 | BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4, |
7a5ee6ed | 5314 | "Please update the fixed PMCs in msrs_to_saved_all[]"); |
24c29b7a PB |
5315 | |
5316 | perf_get_x86_pmu_capability(&x86_pmu); | |
e2ada66e | 5317 | |
6cbee2b9 XL |
5318 | num_msrs_to_save = 0; |
5319 | num_emulated_msrs = 0; | |
5320 | num_msr_based_features = 0; | |
5321 | ||
7a5ee6ed CQ |
5322 | for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { |
5323 | if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) | |
043405e1 | 5324 | continue; |
93c4adc7 PB |
5325 | |
5326 | /* | |
5327 | * Even MSRs that are valid in the host may not be exposed | |
9dbe6cf9 | 5328 | * to the guests in some cases. |
93c4adc7 | 5329 | */ |
7a5ee6ed | 5330 | switch (msrs_to_save_all[i]) { |
93c4adc7 | 5331 | case MSR_IA32_BNDCFGS: |
503234b3 | 5332 | if (!kvm_mpx_supported()) |
93c4adc7 PB |
5333 | continue; |
5334 | break; | |
9dbe6cf9 | 5335 | case MSR_TSC_AUX: |
13908510 | 5336 | if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) |
9dbe6cf9 PB |
5337 | continue; |
5338 | break; | |
f4cfcd2d ML |
5339 | case MSR_IA32_UMWAIT_CONTROL: |
5340 | if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) | |
5341 | continue; | |
5342 | break; | |
bf8c55d8 CP |
5343 | case MSR_IA32_RTIT_CTL: |
5344 | case MSR_IA32_RTIT_STATUS: | |
7b874c26 | 5345 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) |
bf8c55d8 CP |
5346 | continue; |
5347 | break; | |
5348 | case MSR_IA32_RTIT_CR3_MATCH: | |
7b874c26 | 5349 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
5350 | !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) |
5351 | continue; | |
5352 | break; | |
5353 | case MSR_IA32_RTIT_OUTPUT_BASE: | |
5354 | case MSR_IA32_RTIT_OUTPUT_MASK: | |
7b874c26 | 5355 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
5356 | (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && |
5357 | !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) | |
5358 | continue; | |
5359 | break; | |
7cb85fc4 | 5360 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
7b874c26 | 5361 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
7a5ee6ed | 5362 | msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= |
bf8c55d8 CP |
5363 | intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) |
5364 | continue; | |
5365 | break; | |
cf05a67b | 5366 | case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17: |
7a5ee6ed | 5367 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= |
24c29b7a PB |
5368 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
5369 | continue; | |
5370 | break; | |
cf05a67b | 5371 | case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17: |
7a5ee6ed | 5372 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= |
24c29b7a PB |
5373 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
5374 | continue; | |
7cb85fc4 | 5375 | break; |
93c4adc7 PB |
5376 | default: |
5377 | break; | |
5378 | } | |
5379 | ||
7a5ee6ed | 5380 | msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; |
043405e1 | 5381 | } |
62ef68bb | 5382 | |
7a5ee6ed | 5383 | for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { |
afaf0b2f | 5384 | if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i])) |
bc226f07 | 5385 | continue; |
62ef68bb | 5386 | |
7a5ee6ed | 5387 | emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i]; |
62ef68bb | 5388 | } |
801e459a | 5389 | |
7a5ee6ed | 5390 | for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) { |
801e459a TL |
5391 | struct kvm_msr_entry msr; |
5392 | ||
7a5ee6ed | 5393 | msr.index = msr_based_features_all[i]; |
66421c1e | 5394 | if (kvm_get_msr_feature(&msr)) |
801e459a TL |
5395 | continue; |
5396 | ||
7a5ee6ed | 5397 | msr_based_features[num_msr_based_features++] = msr_based_features_all[i]; |
801e459a | 5398 | } |
043405e1 CO |
5399 | } |
5400 | ||
bda9020e MT |
5401 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
5402 | const void *v) | |
bbd9b64e | 5403 | { |
70252a10 AK |
5404 | int handled = 0; |
5405 | int n; | |
5406 | ||
5407 | do { | |
5408 | n = min(len, 8); | |
bce87cce | 5409 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
5410 | !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) |
5411 | && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 AK |
5412 | break; |
5413 | handled += n; | |
5414 | addr += n; | |
5415 | len -= n; | |
5416 | v += n; | |
5417 | } while (len); | |
bbd9b64e | 5418 | |
70252a10 | 5419 | return handled; |
bbd9b64e CO |
5420 | } |
5421 | ||
bda9020e | 5422 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
bbd9b64e | 5423 | { |
70252a10 AK |
5424 | int handled = 0; |
5425 | int n; | |
5426 | ||
5427 | do { | |
5428 | n = min(len, 8); | |
bce87cce | 5429 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
5430 | !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, |
5431 | addr, n, v)) | |
5432 | && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 | 5433 | break; |
e39d200f | 5434 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v); |
70252a10 AK |
5435 | handled += n; |
5436 | addr += n; | |
5437 | len -= n; | |
5438 | v += n; | |
5439 | } while (len); | |
bbd9b64e | 5440 | |
70252a10 | 5441 | return handled; |
bbd9b64e CO |
5442 | } |
5443 | ||
2dafc6c2 GN |
5444 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
5445 | struct kvm_segment *var, int seg) | |
5446 | { | |
afaf0b2f | 5447 | kvm_x86_ops.set_segment(vcpu, var, seg); |
2dafc6c2 GN |
5448 | } |
5449 | ||
5450 | void kvm_get_segment(struct kvm_vcpu *vcpu, | |
5451 | struct kvm_segment *var, int seg) | |
5452 | { | |
afaf0b2f | 5453 | kvm_x86_ops.get_segment(vcpu, var, seg); |
2dafc6c2 GN |
5454 | } |
5455 | ||
54987b7a PB |
5456 | gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, |
5457 | struct x86_exception *exception) | |
02f59dc9 JR |
5458 | { |
5459 | gpa_t t_gpa; | |
02f59dc9 JR |
5460 | |
5461 | BUG_ON(!mmu_is_nested(vcpu)); | |
5462 | ||
5463 | /* NPT walks are always user-walks */ | |
5464 | access |= PFERR_USER_MASK; | |
44dd3ffa | 5465 | t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception); |
02f59dc9 JR |
5466 | |
5467 | return t_gpa; | |
5468 | } | |
5469 | ||
ab9ae313 AK |
5470 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, |
5471 | struct x86_exception *exception) | |
1871c602 | 5472 | { |
afaf0b2f | 5473 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
ab9ae313 | 5474 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
5475 | } |
5476 | ||
ab9ae313 AK |
5477 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, |
5478 | struct x86_exception *exception) | |
1871c602 | 5479 | { |
afaf0b2f | 5480 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 5481 | access |= PFERR_FETCH_MASK; |
ab9ae313 | 5482 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
5483 | } |
5484 | ||
ab9ae313 AK |
5485 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, |
5486 | struct x86_exception *exception) | |
1871c602 | 5487 | { |
afaf0b2f | 5488 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 5489 | access |= PFERR_WRITE_MASK; |
ab9ae313 | 5490 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
5491 | } |
5492 | ||
5493 | /* uses this to access any guest's mapped memory without checking CPL */ | |
ab9ae313 AK |
5494 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, |
5495 | struct x86_exception *exception) | |
1871c602 | 5496 | { |
ab9ae313 | 5497 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception); |
1871c602 GN |
5498 | } |
5499 | ||
5500 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, | |
5501 | struct kvm_vcpu *vcpu, u32 access, | |
bcc55cba | 5502 | struct x86_exception *exception) |
bbd9b64e CO |
5503 | { |
5504 | void *data = val; | |
10589a46 | 5505 | int r = X86EMUL_CONTINUE; |
bbd9b64e CO |
5506 | |
5507 | while (bytes) { | |
14dfe855 | 5508 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access, |
ab9ae313 | 5509 | exception); |
bbd9b64e | 5510 | unsigned offset = addr & (PAGE_SIZE-1); |
77c2002e | 5511 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
bbd9b64e CO |
5512 | int ret; |
5513 | ||
bcc55cba | 5514 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 5515 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa PB |
5516 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data, |
5517 | offset, toread); | |
10589a46 | 5518 | if (ret < 0) { |
c3cd7ffa | 5519 | r = X86EMUL_IO_NEEDED; |
10589a46 MT |
5520 | goto out; |
5521 | } | |
bbd9b64e | 5522 | |
77c2002e IE |
5523 | bytes -= toread; |
5524 | data += toread; | |
5525 | addr += toread; | |
bbd9b64e | 5526 | } |
10589a46 | 5527 | out: |
10589a46 | 5528 | return r; |
bbd9b64e | 5529 | } |
77c2002e | 5530 | |
1871c602 | 5531 | /* used for instruction fetching */ |
0f65dd70 AK |
5532 | static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, |
5533 | gva_t addr, void *val, unsigned int bytes, | |
bcc55cba | 5534 | struct x86_exception *exception) |
1871c602 | 5535 | { |
0f65dd70 | 5536 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
afaf0b2f | 5537 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
44583cba PB |
5538 | unsigned offset; |
5539 | int ret; | |
0f65dd70 | 5540 | |
44583cba PB |
5541 | /* Inline kvm_read_guest_virt_helper for speed. */ |
5542 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK, | |
5543 | exception); | |
5544 | if (unlikely(gpa == UNMAPPED_GVA)) | |
5545 | return X86EMUL_PROPAGATE_FAULT; | |
5546 | ||
5547 | offset = addr & (PAGE_SIZE-1); | |
5548 | if (WARN_ON(offset + bytes > PAGE_SIZE)) | |
5549 | bytes = (unsigned)PAGE_SIZE - offset; | |
54bf36aa PB |
5550 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, |
5551 | offset, bytes); | |
44583cba PB |
5552 | if (unlikely(ret < 0)) |
5553 | return X86EMUL_IO_NEEDED; | |
5554 | ||
5555 | return X86EMUL_CONTINUE; | |
1871c602 GN |
5556 | } |
5557 | ||
ce14e868 | 5558 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
0f65dd70 | 5559 | gva_t addr, void *val, unsigned int bytes, |
bcc55cba | 5560 | struct x86_exception *exception) |
1871c602 | 5561 | { |
afaf0b2f | 5562 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
0f65dd70 | 5563 | |
353c0956 PB |
5564 | /* |
5565 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED | |
5566 | * is returned, but our callers are not ready for that and they blindly | |
5567 | * call kvm_inject_page_fault. Ensure that they at least do not leak | |
5568 | * uninitialized kernel stack memory into cr2 and error code. | |
5569 | */ | |
5570 | memset(exception, 0, sizeof(*exception)); | |
1871c602 | 5571 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
bcc55cba | 5572 | exception); |
1871c602 | 5573 | } |
064aea77 | 5574 | EXPORT_SYMBOL_GPL(kvm_read_guest_virt); |
1871c602 | 5575 | |
ce14e868 PB |
5576 | static int emulator_read_std(struct x86_emulate_ctxt *ctxt, |
5577 | gva_t addr, void *val, unsigned int bytes, | |
3c9fa24c | 5578 | struct x86_exception *exception, bool system) |
1871c602 | 5579 | { |
0f65dd70 | 5580 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
3c9fa24c PB |
5581 | u32 access = 0; |
5582 | ||
afaf0b2f | 5583 | if (!system && kvm_x86_ops.get_cpl(vcpu) == 3) |
3c9fa24c PB |
5584 | access |= PFERR_USER_MASK; |
5585 | ||
5586 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); | |
1871c602 GN |
5587 | } |
5588 | ||
7a036a6f RK |
5589 | static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt, |
5590 | unsigned long addr, void *val, unsigned int bytes) | |
5591 | { | |
5592 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
5593 | int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes); | |
5594 | ||
5595 | return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE; | |
5596 | } | |
5597 | ||
ce14e868 PB |
5598 | static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
5599 | struct kvm_vcpu *vcpu, u32 access, | |
5600 | struct x86_exception *exception) | |
77c2002e IE |
5601 | { |
5602 | void *data = val; | |
5603 | int r = X86EMUL_CONTINUE; | |
5604 | ||
5605 | while (bytes) { | |
14dfe855 | 5606 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, |
ce14e868 | 5607 | access, |
ab9ae313 | 5608 | exception); |
77c2002e IE |
5609 | unsigned offset = addr & (PAGE_SIZE-1); |
5610 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); | |
5611 | int ret; | |
5612 | ||
bcc55cba | 5613 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 5614 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa | 5615 | ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite); |
77c2002e | 5616 | if (ret < 0) { |
c3cd7ffa | 5617 | r = X86EMUL_IO_NEEDED; |
77c2002e IE |
5618 | goto out; |
5619 | } | |
5620 | ||
5621 | bytes -= towrite; | |
5622 | data += towrite; | |
5623 | addr += towrite; | |
5624 | } | |
5625 | out: | |
5626 | return r; | |
5627 | } | |
ce14e868 PB |
5628 | |
5629 | static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, | |
3c9fa24c PB |
5630 | unsigned int bytes, struct x86_exception *exception, |
5631 | bool system) | |
ce14e868 PB |
5632 | { |
5633 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
3c9fa24c PB |
5634 | u32 access = PFERR_WRITE_MASK; |
5635 | ||
afaf0b2f | 5636 | if (!system && kvm_x86_ops.get_cpl(vcpu) == 3) |
3c9fa24c | 5637 | access |= PFERR_USER_MASK; |
ce14e868 PB |
5638 | |
5639 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, | |
3c9fa24c | 5640 | access, exception); |
ce14e868 PB |
5641 | } |
5642 | ||
5643 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, | |
5644 | unsigned int bytes, struct x86_exception *exception) | |
5645 | { | |
c595ceee PB |
5646 | /* kvm_write_guest_virt_system can pull in tons of pages. */ |
5647 | vcpu->arch.l1tf_flush_l1d = true; | |
5648 | ||
ce14e868 PB |
5649 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
5650 | PFERR_WRITE_MASK, exception); | |
5651 | } | |
6a4d7550 | 5652 | EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); |
77c2002e | 5653 | |
082d06ed WL |
5654 | int handle_ud(struct kvm_vcpu *vcpu) |
5655 | { | |
b3dc0695 | 5656 | static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX }; |
6c86eedc | 5657 | int emul_type = EMULTYPE_TRAP_UD; |
6c86eedc WL |
5658 | char sig[5]; /* ud2; .ascii "kvm" */ |
5659 | struct x86_exception e; | |
5660 | ||
5661 | if (force_emulation_prefix && | |
3c9fa24c PB |
5662 | kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), |
5663 | sig, sizeof(sig), &e) == 0 && | |
b3dc0695 | 5664 | memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) { |
6c86eedc | 5665 | kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); |
b4000606 | 5666 | emul_type = EMULTYPE_TRAP_UD_FORCED; |
6c86eedc | 5667 | } |
082d06ed | 5668 | |
60fc3d02 | 5669 | return kvm_emulate_instruction(vcpu, emul_type); |
082d06ed WL |
5670 | } |
5671 | EXPORT_SYMBOL_GPL(handle_ud); | |
5672 | ||
0f89b207 TL |
5673 | static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
5674 | gpa_t gpa, bool write) | |
5675 | { | |
5676 | /* For APIC access vmexit */ | |
5677 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
5678 | return 1; | |
5679 | ||
5680 | if (vcpu_match_mmio_gpa(vcpu, gpa)) { | |
5681 | trace_vcpu_match_mmio(gva, gpa, write, true); | |
5682 | return 1; | |
5683 | } | |
5684 | ||
5685 | return 0; | |
5686 | } | |
5687 | ||
af7cc7d1 XG |
5688 | static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
5689 | gpa_t *gpa, struct x86_exception *exception, | |
5690 | bool write) | |
5691 | { | |
afaf0b2f | 5692 | u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0) |
97d64b78 | 5693 | | (write ? PFERR_WRITE_MASK : 0); |
af7cc7d1 | 5694 | |
be94f6b7 HH |
5695 | /* |
5696 | * currently PKRU is only applied to ept enabled guest so | |
5697 | * there is no pkey in EPT page table for L1 guest or EPT | |
5698 | * shadow page table for L2 guest. | |
5699 | */ | |
97d64b78 | 5700 | if (vcpu_match_mmio_gva(vcpu, gva) |
97ec8c06 | 5701 | && !permission_fault(vcpu, vcpu->arch.walk_mmu, |
871bd034 | 5702 | vcpu->arch.mmio_access, 0, access)) { |
bebb106a XG |
5703 | *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | |
5704 | (gva & (PAGE_SIZE - 1)); | |
4f022648 | 5705 | trace_vcpu_match_mmio(gva, *gpa, write, false); |
bebb106a XG |
5706 | return 1; |
5707 | } | |
5708 | ||
af7cc7d1 XG |
5709 | *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
5710 | ||
5711 | if (*gpa == UNMAPPED_GVA) | |
5712 | return -1; | |
5713 | ||
0f89b207 | 5714 | return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); |
af7cc7d1 XG |
5715 | } |
5716 | ||
3200f405 | 5717 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
bcc55cba | 5718 | const void *val, int bytes) |
bbd9b64e CO |
5719 | { |
5720 | int ret; | |
5721 | ||
54bf36aa | 5722 | ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes); |
9f811285 | 5723 | if (ret < 0) |
bbd9b64e | 5724 | return 0; |
0eb05bf2 | 5725 | kvm_page_track_write(vcpu, gpa, val, bytes); |
bbd9b64e CO |
5726 | return 1; |
5727 | } | |
5728 | ||
77d197b2 XG |
5729 | struct read_write_emulator_ops { |
5730 | int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val, | |
5731 | int bytes); | |
5732 | int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5733 | void *val, int bytes); | |
5734 | int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5735 | int bytes, void *val); | |
5736 | int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5737 | void *val, int bytes); | |
5738 | bool write; | |
5739 | }; | |
5740 | ||
5741 | static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes) | |
5742 | { | |
5743 | if (vcpu->mmio_read_completed) { | |
77d197b2 | 5744 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
e39d200f | 5745 | vcpu->mmio_fragments[0].gpa, val); |
77d197b2 XG |
5746 | vcpu->mmio_read_completed = 0; |
5747 | return 1; | |
5748 | } | |
5749 | ||
5750 | return 0; | |
5751 | } | |
5752 | ||
5753 | static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5754 | void *val, int bytes) | |
5755 | { | |
54bf36aa | 5756 | return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); |
77d197b2 XG |
5757 | } |
5758 | ||
5759 | static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5760 | void *val, int bytes) | |
5761 | { | |
5762 | return emulator_write_phys(vcpu, gpa, val, bytes); | |
5763 | } | |
5764 | ||
5765 | static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) | |
5766 | { | |
e39d200f | 5767 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val); |
77d197b2 XG |
5768 | return vcpu_mmio_write(vcpu, gpa, bytes, val); |
5769 | } | |
5770 | ||
5771 | static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5772 | void *val, int bytes) | |
5773 | { | |
e39d200f | 5774 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL); |
77d197b2 XG |
5775 | return X86EMUL_IO_NEEDED; |
5776 | } | |
5777 | ||
5778 | static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5779 | void *val, int bytes) | |
5780 | { | |
f78146b0 AK |
5781 | struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; |
5782 | ||
87da7e66 | 5783 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); |
77d197b2 XG |
5784 | return X86EMUL_CONTINUE; |
5785 | } | |
5786 | ||
0fbe9b0b | 5787 | static const struct read_write_emulator_ops read_emultor = { |
77d197b2 XG |
5788 | .read_write_prepare = read_prepare, |
5789 | .read_write_emulate = read_emulate, | |
5790 | .read_write_mmio = vcpu_mmio_read, | |
5791 | .read_write_exit_mmio = read_exit_mmio, | |
5792 | }; | |
5793 | ||
0fbe9b0b | 5794 | static const struct read_write_emulator_ops write_emultor = { |
77d197b2 XG |
5795 | .read_write_emulate = write_emulate, |
5796 | .read_write_mmio = write_mmio, | |
5797 | .read_write_exit_mmio = write_exit_mmio, | |
5798 | .write = true, | |
5799 | }; | |
5800 | ||
22388a3c XG |
5801 | static int emulator_read_write_onepage(unsigned long addr, void *val, |
5802 | unsigned int bytes, | |
5803 | struct x86_exception *exception, | |
5804 | struct kvm_vcpu *vcpu, | |
0fbe9b0b | 5805 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 5806 | { |
af7cc7d1 XG |
5807 | gpa_t gpa; |
5808 | int handled, ret; | |
22388a3c | 5809 | bool write = ops->write; |
f78146b0 | 5810 | struct kvm_mmio_fragment *frag; |
c9b8b07c | 5811 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
0f89b207 TL |
5812 | |
5813 | /* | |
5814 | * If the exit was due to a NPF we may already have a GPA. | |
5815 | * If the GPA is present, use it to avoid the GVA to GPA table walk. | |
5816 | * Note, this cannot be used on string operations since string | |
5817 | * operation using rep will only have the initial GPA from the NPF | |
5818 | * occurred. | |
5819 | */ | |
744e699c SC |
5820 | if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && |
5821 | (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { | |
5822 | gpa = ctxt->gpa_val; | |
618232e2 BS |
5823 | ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); |
5824 | } else { | |
5825 | ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); | |
5826 | if (ret < 0) | |
5827 | return X86EMUL_PROPAGATE_FAULT; | |
0f89b207 | 5828 | } |
10589a46 | 5829 | |
618232e2 | 5830 | if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) |
bbd9b64e CO |
5831 | return X86EMUL_CONTINUE; |
5832 | ||
bbd9b64e CO |
5833 | /* |
5834 | * Is this MMIO handled locally? | |
5835 | */ | |
22388a3c | 5836 | handled = ops->read_write_mmio(vcpu, gpa, bytes, val); |
70252a10 | 5837 | if (handled == bytes) |
bbd9b64e | 5838 | return X86EMUL_CONTINUE; |
bbd9b64e | 5839 | |
70252a10 AK |
5840 | gpa += handled; |
5841 | bytes -= handled; | |
5842 | val += handled; | |
5843 | ||
87da7e66 XG |
5844 | WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); |
5845 | frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; | |
5846 | frag->gpa = gpa; | |
5847 | frag->data = val; | |
5848 | frag->len = bytes; | |
f78146b0 | 5849 | return X86EMUL_CONTINUE; |
bbd9b64e CO |
5850 | } |
5851 | ||
52eb5a6d XL |
5852 | static int emulator_read_write(struct x86_emulate_ctxt *ctxt, |
5853 | unsigned long addr, | |
22388a3c XG |
5854 | void *val, unsigned int bytes, |
5855 | struct x86_exception *exception, | |
0fbe9b0b | 5856 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 5857 | { |
0f65dd70 | 5858 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
f78146b0 AK |
5859 | gpa_t gpa; |
5860 | int rc; | |
5861 | ||
5862 | if (ops->read_write_prepare && | |
5863 | ops->read_write_prepare(vcpu, val, bytes)) | |
5864 | return X86EMUL_CONTINUE; | |
5865 | ||
5866 | vcpu->mmio_nr_fragments = 0; | |
0f65dd70 | 5867 | |
bbd9b64e CO |
5868 | /* Crossing a page boundary? */ |
5869 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
f78146b0 | 5870 | int now; |
bbd9b64e CO |
5871 | |
5872 | now = -addr & ~PAGE_MASK; | |
22388a3c XG |
5873 | rc = emulator_read_write_onepage(addr, val, now, exception, |
5874 | vcpu, ops); | |
5875 | ||
bbd9b64e CO |
5876 | if (rc != X86EMUL_CONTINUE) |
5877 | return rc; | |
5878 | addr += now; | |
bac15531 NA |
5879 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
5880 | addr = (u32)addr; | |
bbd9b64e CO |
5881 | val += now; |
5882 | bytes -= now; | |
5883 | } | |
22388a3c | 5884 | |
f78146b0 AK |
5885 | rc = emulator_read_write_onepage(addr, val, bytes, exception, |
5886 | vcpu, ops); | |
5887 | if (rc != X86EMUL_CONTINUE) | |
5888 | return rc; | |
5889 | ||
5890 | if (!vcpu->mmio_nr_fragments) | |
5891 | return rc; | |
5892 | ||
5893 | gpa = vcpu->mmio_fragments[0].gpa; | |
5894 | ||
5895 | vcpu->mmio_needed = 1; | |
5896 | vcpu->mmio_cur_fragment = 0; | |
5897 | ||
87da7e66 | 5898 | vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); |
f78146b0 AK |
5899 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; |
5900 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
5901 | vcpu->run->mmio.phys_addr = gpa; | |
5902 | ||
5903 | return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); | |
22388a3c XG |
5904 | } |
5905 | ||
5906 | static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, | |
5907 | unsigned long addr, | |
5908 | void *val, | |
5909 | unsigned int bytes, | |
5910 | struct x86_exception *exception) | |
5911 | { | |
5912 | return emulator_read_write(ctxt, addr, val, bytes, | |
5913 | exception, &read_emultor); | |
5914 | } | |
5915 | ||
52eb5a6d | 5916 | static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, |
22388a3c XG |
5917 | unsigned long addr, |
5918 | const void *val, | |
5919 | unsigned int bytes, | |
5920 | struct x86_exception *exception) | |
5921 | { | |
5922 | return emulator_read_write(ctxt, addr, (void *)val, bytes, | |
5923 | exception, &write_emultor); | |
bbd9b64e | 5924 | } |
bbd9b64e | 5925 | |
daea3e73 AK |
5926 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
5927 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) | |
5928 | ||
5929 | #ifdef CONFIG_X86_64 | |
5930 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) | |
5931 | #else | |
5932 | # define CMPXCHG64(ptr, old, new) \ | |
9749a6c0 | 5933 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
daea3e73 AK |
5934 | #endif |
5935 | ||
0f65dd70 AK |
5936 | static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, |
5937 | unsigned long addr, | |
bbd9b64e CO |
5938 | const void *old, |
5939 | const void *new, | |
5940 | unsigned int bytes, | |
0f65dd70 | 5941 | struct x86_exception *exception) |
bbd9b64e | 5942 | { |
42e35f80 | 5943 | struct kvm_host_map map; |
0f65dd70 | 5944 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
9de6fe3c | 5945 | u64 page_line_mask; |
daea3e73 | 5946 | gpa_t gpa; |
daea3e73 AK |
5947 | char *kaddr; |
5948 | bool exchanged; | |
2bacc55c | 5949 | |
daea3e73 AK |
5950 | /* guests cmpxchg8b have to be emulated atomically */ |
5951 | if (bytes > 8 || (bytes & (bytes - 1))) | |
5952 | goto emul_write; | |
10589a46 | 5953 | |
daea3e73 | 5954 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
2bacc55c | 5955 | |
daea3e73 AK |
5956 | if (gpa == UNMAPPED_GVA || |
5957 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
5958 | goto emul_write; | |
2bacc55c | 5959 | |
9de6fe3c XL |
5960 | /* |
5961 | * Emulate the atomic as a straight write to avoid #AC if SLD is | |
5962 | * enabled in the host and the access splits a cache line. | |
5963 | */ | |
5964 | if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) | |
5965 | page_line_mask = ~(cache_line_size() - 1); | |
5966 | else | |
5967 | page_line_mask = PAGE_MASK; | |
5968 | ||
5969 | if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) | |
daea3e73 | 5970 | goto emul_write; |
72dc67a6 | 5971 | |
42e35f80 | 5972 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map)) |
c19b8bd6 | 5973 | goto emul_write; |
72dc67a6 | 5974 | |
42e35f80 KA |
5975 | kaddr = map.hva + offset_in_page(gpa); |
5976 | ||
daea3e73 AK |
5977 | switch (bytes) { |
5978 | case 1: | |
5979 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); | |
5980 | break; | |
5981 | case 2: | |
5982 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); | |
5983 | break; | |
5984 | case 4: | |
5985 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); | |
5986 | break; | |
5987 | case 8: | |
5988 | exchanged = CMPXCHG64(kaddr, old, new); | |
5989 | break; | |
5990 | default: | |
5991 | BUG(); | |
2bacc55c | 5992 | } |
42e35f80 KA |
5993 | |
5994 | kvm_vcpu_unmap(vcpu, &map, true); | |
daea3e73 AK |
5995 | |
5996 | if (!exchanged) | |
5997 | return X86EMUL_CMPXCHG_FAILED; | |
5998 | ||
0eb05bf2 | 5999 | kvm_page_track_write(vcpu, gpa, new, bytes); |
8f6abd06 GN |
6000 | |
6001 | return X86EMUL_CONTINUE; | |
4a5f48f6 | 6002 | |
3200f405 | 6003 | emul_write: |
daea3e73 | 6004 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
2bacc55c | 6005 | |
0f65dd70 | 6006 | return emulator_write_emulated(ctxt, addr, new, bytes, exception); |
bbd9b64e CO |
6007 | } |
6008 | ||
cf8f70bf GN |
6009 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
6010 | { | |
cbfc6c91 | 6011 | int r = 0, i; |
cf8f70bf | 6012 | |
cbfc6c91 WL |
6013 | for (i = 0; i < vcpu->arch.pio.count; i++) { |
6014 | if (vcpu->arch.pio.in) | |
6015 | r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, | |
6016 | vcpu->arch.pio.size, pd); | |
6017 | else | |
6018 | r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, | |
6019 | vcpu->arch.pio.port, vcpu->arch.pio.size, | |
6020 | pd); | |
6021 | if (r) | |
6022 | break; | |
6023 | pd += vcpu->arch.pio.size; | |
6024 | } | |
cf8f70bf GN |
6025 | return r; |
6026 | } | |
6027 | ||
6f6fbe98 XG |
6028 | static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, |
6029 | unsigned short port, void *val, | |
6030 | unsigned int count, bool in) | |
cf8f70bf | 6031 | { |
cf8f70bf | 6032 | vcpu->arch.pio.port = port; |
6f6fbe98 | 6033 | vcpu->arch.pio.in = in; |
7972995b | 6034 | vcpu->arch.pio.count = count; |
cf8f70bf GN |
6035 | vcpu->arch.pio.size = size; |
6036 | ||
6037 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { | |
7972995b | 6038 | vcpu->arch.pio.count = 0; |
cf8f70bf GN |
6039 | return 1; |
6040 | } | |
6041 | ||
6042 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
6f6fbe98 | 6043 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
cf8f70bf GN |
6044 | vcpu->run->io.size = size; |
6045 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
6046 | vcpu->run->io.count = count; | |
6047 | vcpu->run->io.port = port; | |
6048 | ||
6049 | return 0; | |
6050 | } | |
6051 | ||
2e3bb4d8 SC |
6052 | static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
6053 | unsigned short port, void *val, unsigned int count) | |
cf8f70bf | 6054 | { |
6f6fbe98 | 6055 | int ret; |
ca1d4a9e | 6056 | |
6f6fbe98 XG |
6057 | if (vcpu->arch.pio.count) |
6058 | goto data_avail; | |
cf8f70bf | 6059 | |
cbfc6c91 WL |
6060 | memset(vcpu->arch.pio_data, 0, size * count); |
6061 | ||
6f6fbe98 XG |
6062 | ret = emulator_pio_in_out(vcpu, size, port, val, count, true); |
6063 | if (ret) { | |
6064 | data_avail: | |
6065 | memcpy(val, vcpu->arch.pio_data, size * count); | |
1171903d | 6066 | trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data); |
7972995b | 6067 | vcpu->arch.pio.count = 0; |
cf8f70bf GN |
6068 | return 1; |
6069 | } | |
6070 | ||
cf8f70bf GN |
6071 | return 0; |
6072 | } | |
6073 | ||
2e3bb4d8 SC |
6074 | static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
6075 | int size, unsigned short port, void *val, | |
6076 | unsigned int count) | |
6f6fbe98 | 6077 | { |
2e3bb4d8 | 6078 | return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); |
6f6fbe98 | 6079 | |
2e3bb4d8 | 6080 | } |
6f6fbe98 | 6081 | |
2e3bb4d8 SC |
6082 | static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, |
6083 | unsigned short port, const void *val, | |
6084 | unsigned int count) | |
6085 | { | |
6f6fbe98 | 6086 | memcpy(vcpu->arch.pio_data, val, size * count); |
1171903d | 6087 | trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); |
6f6fbe98 XG |
6088 | return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); |
6089 | } | |
6090 | ||
2e3bb4d8 SC |
6091 | static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, |
6092 | int size, unsigned short port, | |
6093 | const void *val, unsigned int count) | |
6094 | { | |
6095 | return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count); | |
6096 | } | |
6097 | ||
bbd9b64e CO |
6098 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
6099 | { | |
afaf0b2f | 6100 | return kvm_x86_ops.get_segment_base(vcpu, seg); |
bbd9b64e CO |
6101 | } |
6102 | ||
3cb16fe7 | 6103 | static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address) |
bbd9b64e | 6104 | { |
3cb16fe7 | 6105 | kvm_mmu_invlpg(emul_to_vcpu(ctxt), address); |
bbd9b64e CO |
6106 | } |
6107 | ||
ae6a2375 | 6108 | static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu) |
f5f48ee1 SY |
6109 | { |
6110 | if (!need_emulate_wbinvd(vcpu)) | |
6111 | return X86EMUL_CONTINUE; | |
6112 | ||
afaf0b2f | 6113 | if (kvm_x86_ops.has_wbinvd_exit()) { |
2eec7343 JK |
6114 | int cpu = get_cpu(); |
6115 | ||
6116 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); | |
f5f48ee1 SY |
6117 | smp_call_function_many(vcpu->arch.wbinvd_dirty_mask, |
6118 | wbinvd_ipi, NULL, 1); | |
2eec7343 | 6119 | put_cpu(); |
f5f48ee1 | 6120 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
2eec7343 JK |
6121 | } else |
6122 | wbinvd(); | |
f5f48ee1 SY |
6123 | return X86EMUL_CONTINUE; |
6124 | } | |
5cb56059 JS |
6125 | |
6126 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
6127 | { | |
6affcbed KH |
6128 | kvm_emulate_wbinvd_noskip(vcpu); |
6129 | return kvm_skip_emulated_instruction(vcpu); | |
5cb56059 | 6130 | } |
f5f48ee1 SY |
6131 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
6132 | ||
5cb56059 JS |
6133 | |
6134 | ||
bcaf5cc5 AK |
6135 | static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt) |
6136 | { | |
5cb56059 | 6137 | kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt)); |
bcaf5cc5 AK |
6138 | } |
6139 | ||
52eb5a6d XL |
6140 | static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, |
6141 | unsigned long *dest) | |
bbd9b64e | 6142 | { |
16f8a6f9 | 6143 | return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest); |
bbd9b64e CO |
6144 | } |
6145 | ||
52eb5a6d XL |
6146 | static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, |
6147 | unsigned long value) | |
bbd9b64e | 6148 | { |
338dbc97 | 6149 | |
717746e3 | 6150 | return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value); |
bbd9b64e CO |
6151 | } |
6152 | ||
52a46617 | 6153 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
5fdbf976 | 6154 | { |
52a46617 | 6155 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
5fdbf976 MT |
6156 | } |
6157 | ||
717746e3 | 6158 | static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) |
bbd9b64e | 6159 | { |
717746e3 | 6160 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
52a46617 GN |
6161 | unsigned long value; |
6162 | ||
6163 | switch (cr) { | |
6164 | case 0: | |
6165 | value = kvm_read_cr0(vcpu); | |
6166 | break; | |
6167 | case 2: | |
6168 | value = vcpu->arch.cr2; | |
6169 | break; | |
6170 | case 3: | |
9f8fe504 | 6171 | value = kvm_read_cr3(vcpu); |
52a46617 GN |
6172 | break; |
6173 | case 4: | |
6174 | value = kvm_read_cr4(vcpu); | |
6175 | break; | |
6176 | case 8: | |
6177 | value = kvm_get_cr8(vcpu); | |
6178 | break; | |
6179 | default: | |
a737f256 | 6180 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
52a46617 GN |
6181 | return 0; |
6182 | } | |
6183 | ||
6184 | return value; | |
6185 | } | |
6186 | ||
717746e3 | 6187 | static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val) |
52a46617 | 6188 | { |
717746e3 | 6189 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
0f12244f GN |
6190 | int res = 0; |
6191 | ||
52a46617 GN |
6192 | switch (cr) { |
6193 | case 0: | |
49a9b07e | 6194 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
52a46617 GN |
6195 | break; |
6196 | case 2: | |
6197 | vcpu->arch.cr2 = val; | |
6198 | break; | |
6199 | case 3: | |
2390218b | 6200 | res = kvm_set_cr3(vcpu, val); |
52a46617 GN |
6201 | break; |
6202 | case 4: | |
a83b29c6 | 6203 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
52a46617 GN |
6204 | break; |
6205 | case 8: | |
eea1cff9 | 6206 | res = kvm_set_cr8(vcpu, val); |
52a46617 GN |
6207 | break; |
6208 | default: | |
a737f256 | 6209 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
0f12244f | 6210 | res = -1; |
52a46617 | 6211 | } |
0f12244f GN |
6212 | |
6213 | return res; | |
52a46617 GN |
6214 | } |
6215 | ||
717746e3 | 6216 | static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt) |
9c537244 | 6217 | { |
afaf0b2f | 6218 | return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt)); |
9c537244 GN |
6219 | } |
6220 | ||
4bff1e86 | 6221 | static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
2dafc6c2 | 6222 | { |
afaf0b2f | 6223 | kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt); |
2dafc6c2 GN |
6224 | } |
6225 | ||
4bff1e86 | 6226 | static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
160ce1f1 | 6227 | { |
afaf0b2f | 6228 | kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt); |
160ce1f1 MG |
6229 | } |
6230 | ||
1ac9d0cf AK |
6231 | static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
6232 | { | |
afaf0b2f | 6233 | kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
6234 | } |
6235 | ||
6236 | static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) | |
6237 | { | |
afaf0b2f | 6238 | kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
6239 | } |
6240 | ||
4bff1e86 AK |
6241 | static unsigned long emulator_get_cached_segment_base( |
6242 | struct x86_emulate_ctxt *ctxt, int seg) | |
5951c442 | 6243 | { |
4bff1e86 | 6244 | return get_segment_base(emul_to_vcpu(ctxt), seg); |
5951c442 GN |
6245 | } |
6246 | ||
1aa36616 AK |
6247 | static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector, |
6248 | struct desc_struct *desc, u32 *base3, | |
6249 | int seg) | |
2dafc6c2 GN |
6250 | { |
6251 | struct kvm_segment var; | |
6252 | ||
4bff1e86 | 6253 | kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); |
1aa36616 | 6254 | *selector = var.selector; |
2dafc6c2 | 6255 | |
378a8b09 GN |
6256 | if (var.unusable) { |
6257 | memset(desc, 0, sizeof(*desc)); | |
f0367ee1 RK |
6258 | if (base3) |
6259 | *base3 = 0; | |
2dafc6c2 | 6260 | return false; |
378a8b09 | 6261 | } |
2dafc6c2 GN |
6262 | |
6263 | if (var.g) | |
6264 | var.limit >>= 12; | |
6265 | set_desc_limit(desc, var.limit); | |
6266 | set_desc_base(desc, (unsigned long)var.base); | |
5601d05b GN |
6267 | #ifdef CONFIG_X86_64 |
6268 | if (base3) | |
6269 | *base3 = var.base >> 32; | |
6270 | #endif | |
2dafc6c2 GN |
6271 | desc->type = var.type; |
6272 | desc->s = var.s; | |
6273 | desc->dpl = var.dpl; | |
6274 | desc->p = var.present; | |
6275 | desc->avl = var.avl; | |
6276 | desc->l = var.l; | |
6277 | desc->d = var.db; | |
6278 | desc->g = var.g; | |
6279 | ||
6280 | return true; | |
6281 | } | |
6282 | ||
1aa36616 AK |
6283 | static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, |
6284 | struct desc_struct *desc, u32 base3, | |
6285 | int seg) | |
2dafc6c2 | 6286 | { |
4bff1e86 | 6287 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
2dafc6c2 GN |
6288 | struct kvm_segment var; |
6289 | ||
1aa36616 | 6290 | var.selector = selector; |
2dafc6c2 | 6291 | var.base = get_desc_base(desc); |
5601d05b GN |
6292 | #ifdef CONFIG_X86_64 |
6293 | var.base |= ((u64)base3) << 32; | |
6294 | #endif | |
2dafc6c2 GN |
6295 | var.limit = get_desc_limit(desc); |
6296 | if (desc->g) | |
6297 | var.limit = (var.limit << 12) | 0xfff; | |
6298 | var.type = desc->type; | |
2dafc6c2 GN |
6299 | var.dpl = desc->dpl; |
6300 | var.db = desc->d; | |
6301 | var.s = desc->s; | |
6302 | var.l = desc->l; | |
6303 | var.g = desc->g; | |
6304 | var.avl = desc->avl; | |
6305 | var.present = desc->p; | |
6306 | var.unusable = !var.present; | |
6307 | var.padding = 0; | |
6308 | ||
6309 | kvm_set_segment(vcpu, &var, seg); | |
6310 | return; | |
6311 | } | |
6312 | ||
717746e3 AK |
6313 | static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, |
6314 | u32 msr_index, u64 *pdata) | |
6315 | { | |
f20935d8 | 6316 | return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata); |
717746e3 AK |
6317 | } |
6318 | ||
6319 | static int emulator_set_msr(struct x86_emulate_ctxt *ctxt, | |
6320 | u32 msr_index, u64 data) | |
6321 | { | |
f20935d8 | 6322 | return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data); |
717746e3 AK |
6323 | } |
6324 | ||
64d60670 PB |
6325 | static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt) |
6326 | { | |
6327 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6328 | ||
6329 | return vcpu->arch.smbase; | |
6330 | } | |
6331 | ||
6332 | static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase) | |
6333 | { | |
6334 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6335 | ||
6336 | vcpu->arch.smbase = smbase; | |
6337 | } | |
6338 | ||
67f4d428 NA |
6339 | static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, |
6340 | u32 pmc) | |
6341 | { | |
98ff80f5 | 6342 | return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc); |
67f4d428 NA |
6343 | } |
6344 | ||
222d21aa AK |
6345 | static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, |
6346 | u32 pmc, u64 *pdata) | |
6347 | { | |
c6702c9d | 6348 | return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); |
222d21aa AK |
6349 | } |
6350 | ||
6c3287f7 AK |
6351 | static void emulator_halt(struct x86_emulate_ctxt *ctxt) |
6352 | { | |
6353 | emul_to_vcpu(ctxt)->arch.halt_request = 1; | |
6354 | } | |
6355 | ||
2953538e | 6356 | static int emulator_intercept(struct x86_emulate_ctxt *ctxt, |
8a76d7f2 | 6357 | struct x86_instruction_info *info, |
c4f035c6 AK |
6358 | enum x86_intercept_stage stage) |
6359 | { | |
afaf0b2f | 6360 | return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage, |
21f1b8f2 | 6361 | &ctxt->exception); |
c4f035c6 AK |
6362 | } |
6363 | ||
e911eb3b | 6364 | static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt, |
f91af517 SC |
6365 | u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, |
6366 | bool exact_only) | |
bdb42f5a | 6367 | { |
f91af517 | 6368 | return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only); |
bdb42f5a SB |
6369 | } |
6370 | ||
5ae78e95 SC |
6371 | static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt) |
6372 | { | |
6373 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM); | |
6374 | } | |
6375 | ||
6376 | static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt) | |
6377 | { | |
6378 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE); | |
6379 | } | |
6380 | ||
6381 | static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) | |
6382 | { | |
6383 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); | |
6384 | } | |
6385 | ||
dd856efa AK |
6386 | static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) |
6387 | { | |
6388 | return kvm_register_read(emul_to_vcpu(ctxt), reg); | |
6389 | } | |
6390 | ||
6391 | static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val) | |
6392 | { | |
6393 | kvm_register_write(emul_to_vcpu(ctxt), reg, val); | |
6394 | } | |
6395 | ||
801806d9 NA |
6396 | static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked) |
6397 | { | |
afaf0b2f | 6398 | kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked); |
801806d9 NA |
6399 | } |
6400 | ||
6ed071f0 LP |
6401 | static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt) |
6402 | { | |
6403 | return emul_to_vcpu(ctxt)->arch.hflags; | |
6404 | } | |
6405 | ||
6406 | static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags) | |
6407 | { | |
c5833c7a | 6408 | emul_to_vcpu(ctxt)->arch.hflags = emul_flags; |
6ed071f0 LP |
6409 | } |
6410 | ||
ed19321f SC |
6411 | static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt, |
6412 | const char *smstate) | |
0234bf88 | 6413 | { |
afaf0b2f | 6414 | return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate); |
0234bf88 LP |
6415 | } |
6416 | ||
c5833c7a SC |
6417 | static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt) |
6418 | { | |
6419 | kvm_smm_changed(emul_to_vcpu(ctxt)); | |
6420 | } | |
6421 | ||
02d4160f VK |
6422 | static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr) |
6423 | { | |
6424 | return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); | |
6425 | } | |
6426 | ||
0225fb50 | 6427 | static const struct x86_emulate_ops emulate_ops = { |
dd856efa AK |
6428 | .read_gpr = emulator_read_gpr, |
6429 | .write_gpr = emulator_write_gpr, | |
ce14e868 PB |
6430 | .read_std = emulator_read_std, |
6431 | .write_std = emulator_write_std, | |
7a036a6f | 6432 | .read_phys = kvm_read_guest_phys_system, |
1871c602 | 6433 | .fetch = kvm_fetch_guest_virt, |
bbd9b64e CO |
6434 | .read_emulated = emulator_read_emulated, |
6435 | .write_emulated = emulator_write_emulated, | |
6436 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
3cb16fe7 | 6437 | .invlpg = emulator_invlpg, |
cf8f70bf GN |
6438 | .pio_in_emulated = emulator_pio_in_emulated, |
6439 | .pio_out_emulated = emulator_pio_out_emulated, | |
1aa36616 AK |
6440 | .get_segment = emulator_get_segment, |
6441 | .set_segment = emulator_set_segment, | |
5951c442 | 6442 | .get_cached_segment_base = emulator_get_cached_segment_base, |
2dafc6c2 | 6443 | .get_gdt = emulator_get_gdt, |
160ce1f1 | 6444 | .get_idt = emulator_get_idt, |
1ac9d0cf AK |
6445 | .set_gdt = emulator_set_gdt, |
6446 | .set_idt = emulator_set_idt, | |
52a46617 GN |
6447 | .get_cr = emulator_get_cr, |
6448 | .set_cr = emulator_set_cr, | |
9c537244 | 6449 | .cpl = emulator_get_cpl, |
35aa5375 GN |
6450 | .get_dr = emulator_get_dr, |
6451 | .set_dr = emulator_set_dr, | |
64d60670 PB |
6452 | .get_smbase = emulator_get_smbase, |
6453 | .set_smbase = emulator_set_smbase, | |
717746e3 AK |
6454 | .set_msr = emulator_set_msr, |
6455 | .get_msr = emulator_get_msr, | |
67f4d428 | 6456 | .check_pmc = emulator_check_pmc, |
222d21aa | 6457 | .read_pmc = emulator_read_pmc, |
6c3287f7 | 6458 | .halt = emulator_halt, |
bcaf5cc5 | 6459 | .wbinvd = emulator_wbinvd, |
d6aa1000 | 6460 | .fix_hypercall = emulator_fix_hypercall, |
c4f035c6 | 6461 | .intercept = emulator_intercept, |
bdb42f5a | 6462 | .get_cpuid = emulator_get_cpuid, |
5ae78e95 SC |
6463 | .guest_has_long_mode = emulator_guest_has_long_mode, |
6464 | .guest_has_movbe = emulator_guest_has_movbe, | |
6465 | .guest_has_fxsr = emulator_guest_has_fxsr, | |
801806d9 | 6466 | .set_nmi_mask = emulator_set_nmi_mask, |
6ed071f0 LP |
6467 | .get_hflags = emulator_get_hflags, |
6468 | .set_hflags = emulator_set_hflags, | |
0234bf88 | 6469 | .pre_leave_smm = emulator_pre_leave_smm, |
c5833c7a | 6470 | .post_leave_smm = emulator_post_leave_smm, |
02d4160f | 6471 | .set_xcr = emulator_set_xcr, |
bbd9b64e CO |
6472 | }; |
6473 | ||
95cb2295 GN |
6474 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
6475 | { | |
afaf0b2f | 6476 | u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu); |
95cb2295 GN |
6477 | /* |
6478 | * an sti; sti; sequence only disable interrupts for the first | |
6479 | * instruction. So, if the last instruction, be it emulated or | |
6480 | * not, left the system with the INT_STI flag enabled, it | |
6481 | * means that the last instruction is an sti. We should not | |
6482 | * leave the flag on in this case. The same goes for mov ss | |
6483 | */ | |
37ccdcbe PB |
6484 | if (int_shadow & mask) |
6485 | mask = 0; | |
6addfc42 | 6486 | if (unlikely(int_shadow || mask)) { |
afaf0b2f | 6487 | kvm_x86_ops.set_interrupt_shadow(vcpu, mask); |
6addfc42 PB |
6488 | if (!mask) |
6489 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
6490 | } | |
95cb2295 GN |
6491 | } |
6492 | ||
ef54bcfe | 6493 | static bool inject_emulated_exception(struct kvm_vcpu *vcpu) |
54b8486f | 6494 | { |
c9b8b07c | 6495 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
da9cb575 | 6496 | if (ctxt->exception.vector == PF_VECTOR) |
53b3d8e9 | 6497 | return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); |
ef54bcfe PB |
6498 | |
6499 | if (ctxt->exception.error_code_valid) | |
da9cb575 AK |
6500 | kvm_queue_exception_e(vcpu, ctxt->exception.vector, |
6501 | ctxt->exception.error_code); | |
54b8486f | 6502 | else |
da9cb575 | 6503 | kvm_queue_exception(vcpu, ctxt->exception.vector); |
ef54bcfe | 6504 | return false; |
54b8486f GN |
6505 | } |
6506 | ||
c9b8b07c SC |
6507 | static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) |
6508 | { | |
6509 | struct x86_emulate_ctxt *ctxt; | |
6510 | ||
6511 | ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT); | |
6512 | if (!ctxt) { | |
6513 | pr_err("kvm: failed to allocate vcpu's emulator\n"); | |
6514 | return NULL; | |
6515 | } | |
6516 | ||
6517 | ctxt->vcpu = vcpu; | |
6518 | ctxt->ops = &emulate_ops; | |
6519 | vcpu->arch.emulate_ctxt = ctxt; | |
6520 | ||
6521 | return ctxt; | |
6522 | } | |
6523 | ||
8ec4722d MG |
6524 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
6525 | { | |
c9b8b07c | 6526 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d MG |
6527 | int cs_db, cs_l; |
6528 | ||
afaf0b2f | 6529 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
8ec4722d | 6530 | |
744e699c | 6531 | ctxt->gpa_available = false; |
adf52235 | 6532 | ctxt->eflags = kvm_get_rflags(vcpu); |
c8401dda PB |
6533 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
6534 | ||
adf52235 TY |
6535 | ctxt->eip = kvm_rip_read(vcpu); |
6536 | ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : | |
6537 | (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : | |
42bf549f | 6538 | (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 : |
adf52235 TY |
6539 | cs_db ? X86EMUL_MODE_PROT32 : |
6540 | X86EMUL_MODE_PROT16; | |
a584539b | 6541 | BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK); |
64d60670 PB |
6542 | BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); |
6543 | BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); | |
adf52235 | 6544 | |
dd856efa | 6545 | init_decode_cache(ctxt); |
7ae441ea | 6546 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
8ec4722d MG |
6547 | } |
6548 | ||
9497e1f2 | 6549 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) |
63995653 | 6550 | { |
c9b8b07c | 6551 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
63995653 MG |
6552 | int ret; |
6553 | ||
6554 | init_emulate_ctxt(vcpu); | |
6555 | ||
9dac77fa AK |
6556 | ctxt->op_bytes = 2; |
6557 | ctxt->ad_bytes = 2; | |
6558 | ctxt->_eip = ctxt->eip + inc_eip; | |
9d74191a | 6559 | ret = emulate_int_real(ctxt, irq); |
63995653 | 6560 | |
9497e1f2 SC |
6561 | if (ret != X86EMUL_CONTINUE) { |
6562 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
6563 | } else { | |
6564 | ctxt->eip = ctxt->_eip; | |
6565 | kvm_rip_write(vcpu, ctxt->eip); | |
6566 | kvm_set_rflags(vcpu, ctxt->eflags); | |
6567 | } | |
63995653 MG |
6568 | } |
6569 | EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); | |
6570 | ||
e2366171 | 6571 | static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) |
6d77dbfc | 6572 | { |
6d77dbfc GN |
6573 | ++vcpu->stat.insn_emulation_fail; |
6574 | trace_kvm_emulate_insn_failed(vcpu); | |
e2366171 | 6575 | |
42cbf068 SC |
6576 | if (emulation_type & EMULTYPE_VMWARE_GP) { |
6577 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 6578 | return 1; |
42cbf068 | 6579 | } |
e2366171 | 6580 | |
738fece4 SC |
6581 | if (emulation_type & EMULTYPE_SKIP) { |
6582 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6583 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6584 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 6585 | return 0; |
738fece4 SC |
6586 | } |
6587 | ||
22da61c9 SC |
6588 | kvm_queue_exception(vcpu, UD_VECTOR); |
6589 | ||
afaf0b2f | 6590 | if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) { |
fc3a9157 JR |
6591 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
6592 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6593 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 6594 | return 0; |
fc3a9157 | 6595 | } |
e2366171 | 6596 | |
60fc3d02 | 6597 | return 1; |
6d77dbfc GN |
6598 | } |
6599 | ||
736c291c | 6600 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
991eebf9 GN |
6601 | bool write_fault_to_shadow_pgtable, |
6602 | int emulation_type) | |
a6f177ef | 6603 | { |
736c291c | 6604 | gpa_t gpa = cr2_or_gpa; |
ba049e93 | 6605 | kvm_pfn_t pfn; |
a6f177ef | 6606 | |
92daa48b | 6607 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
991eebf9 GN |
6608 | return false; |
6609 | ||
92daa48b SC |
6610 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
6611 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
6612 | return false; |
6613 | ||
44dd3ffa | 6614 | if (!vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
6615 | /* |
6616 | * Write permission should be allowed since only | |
6617 | * write access need to be emulated. | |
6618 | */ | |
736c291c | 6619 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
a6f177ef | 6620 | |
95b3cf69 XG |
6621 | /* |
6622 | * If the mapping is invalid in guest, let cpu retry | |
6623 | * it to generate fault. | |
6624 | */ | |
6625 | if (gpa == UNMAPPED_GVA) | |
6626 | return true; | |
6627 | } | |
a6f177ef | 6628 | |
8e3d9d06 XG |
6629 | /* |
6630 | * Do not retry the unhandleable instruction if it faults on the | |
6631 | * readonly host memory, otherwise it will goto a infinite loop: | |
6632 | * retry instruction -> write #PF -> emulation fail -> retry | |
6633 | * instruction -> ... | |
6634 | */ | |
6635 | pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); | |
95b3cf69 XG |
6636 | |
6637 | /* | |
6638 | * If the instruction failed on the error pfn, it can not be fixed, | |
6639 | * report the error to userspace. | |
6640 | */ | |
6641 | if (is_error_noslot_pfn(pfn)) | |
6642 | return false; | |
6643 | ||
6644 | kvm_release_pfn_clean(pfn); | |
6645 | ||
6646 | /* The instructions are well-emulated on direct mmu. */ | |
44dd3ffa | 6647 | if (vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
6648 | unsigned int indirect_shadow_pages; |
6649 | ||
6650 | spin_lock(&vcpu->kvm->mmu_lock); | |
6651 | indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; | |
6652 | spin_unlock(&vcpu->kvm->mmu_lock); | |
6653 | ||
6654 | if (indirect_shadow_pages) | |
6655 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
6656 | ||
a6f177ef | 6657 | return true; |
8e3d9d06 | 6658 | } |
a6f177ef | 6659 | |
95b3cf69 XG |
6660 | /* |
6661 | * if emulation was due to access to shadowed page table | |
6662 | * and it failed try to unshadow page and re-enter the | |
6663 | * guest to let CPU execute the instruction. | |
6664 | */ | |
6665 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
93c05d3e XG |
6666 | |
6667 | /* | |
6668 | * If the access faults on its page table, it can not | |
6669 | * be fixed by unprotecting shadow page and it should | |
6670 | * be reported to userspace. | |
6671 | */ | |
6672 | return !write_fault_to_shadow_pgtable; | |
a6f177ef GN |
6673 | } |
6674 | ||
1cb3f3ae | 6675 | static bool retry_instruction(struct x86_emulate_ctxt *ctxt, |
736c291c | 6676 | gpa_t cr2_or_gpa, int emulation_type) |
1cb3f3ae XG |
6677 | { |
6678 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
736c291c | 6679 | unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa; |
1cb3f3ae XG |
6680 | |
6681 | last_retry_eip = vcpu->arch.last_retry_eip; | |
6682 | last_retry_addr = vcpu->arch.last_retry_addr; | |
6683 | ||
6684 | /* | |
6685 | * If the emulation is caused by #PF and it is non-page_table | |
6686 | * writing instruction, it means the VM-EXIT is caused by shadow | |
6687 | * page protected, we can zap the shadow page and retry this | |
6688 | * instruction directly. | |
6689 | * | |
6690 | * Note: if the guest uses a non-page-table modifying instruction | |
6691 | * on the PDE that points to the instruction, then we will unmap | |
6692 | * the instruction and go to an infinite loop. So, we cache the | |
6693 | * last retried eip and the last fault address, if we meet the eip | |
6694 | * and the address again, we can break out of the potential infinite | |
6695 | * loop. | |
6696 | */ | |
6697 | vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; | |
6698 | ||
92daa48b | 6699 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
1cb3f3ae XG |
6700 | return false; |
6701 | ||
92daa48b SC |
6702 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
6703 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
6704 | return false; |
6705 | ||
1cb3f3ae XG |
6706 | if (x86_page_table_writing_insn(ctxt)) |
6707 | return false; | |
6708 | ||
736c291c | 6709 | if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) |
1cb3f3ae XG |
6710 | return false; |
6711 | ||
6712 | vcpu->arch.last_retry_eip = ctxt->eip; | |
736c291c | 6713 | vcpu->arch.last_retry_addr = cr2_or_gpa; |
1cb3f3ae | 6714 | |
44dd3ffa | 6715 | if (!vcpu->arch.mmu->direct_map) |
736c291c | 6716 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
1cb3f3ae | 6717 | |
22368028 | 6718 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
1cb3f3ae XG |
6719 | |
6720 | return true; | |
6721 | } | |
6722 | ||
716d51ab GN |
6723 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu); |
6724 | static int complete_emulated_pio(struct kvm_vcpu *vcpu); | |
6725 | ||
64d60670 | 6726 | static void kvm_smm_changed(struct kvm_vcpu *vcpu) |
a584539b | 6727 | { |
64d60670 | 6728 | if (!(vcpu->arch.hflags & HF_SMM_MASK)) { |
660a5d51 PB |
6729 | /* This is a good place to trace that we are exiting SMM. */ |
6730 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false); | |
6731 | ||
c43203ca PB |
6732 | /* Process a latched INIT or SMI, if any. */ |
6733 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
64d60670 | 6734 | } |
699023e2 PB |
6735 | |
6736 | kvm_mmu_reset_context(vcpu); | |
64d60670 PB |
6737 | } |
6738 | ||
4a1e10d5 PB |
6739 | static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7, |
6740 | unsigned long *db) | |
6741 | { | |
6742 | u32 dr6 = 0; | |
6743 | int i; | |
6744 | u32 enable, rwlen; | |
6745 | ||
6746 | enable = dr7; | |
6747 | rwlen = dr7 >> 16; | |
6748 | for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4) | |
6749 | if ((enable & 3) && (rwlen & 15) == type && db[i] == addr) | |
6750 | dr6 |= (1 << i); | |
6751 | return dr6; | |
6752 | } | |
6753 | ||
120c2c4f | 6754 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) |
663f4c61 PB |
6755 | { |
6756 | struct kvm_run *kvm_run = vcpu->run; | |
6757 | ||
c8401dda PB |
6758 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
6759 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM; | |
d5d260c5 | 6760 | kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
c8401dda PB |
6761 | kvm_run->debug.arch.exception = DB_VECTOR; |
6762 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 6763 | return 0; |
663f4c61 | 6764 | } |
120c2c4f | 6765 | kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); |
60fc3d02 | 6766 | return 1; |
663f4c61 PB |
6767 | } |
6768 | ||
6affcbed KH |
6769 | int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) |
6770 | { | |
afaf0b2f | 6771 | unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); |
f8ea7c60 | 6772 | int r; |
6affcbed | 6773 | |
afaf0b2f | 6774 | r = kvm_x86_ops.skip_emulated_instruction(vcpu); |
60fc3d02 | 6775 | if (unlikely(!r)) |
f8ea7c60 | 6776 | return 0; |
c8401dda PB |
6777 | |
6778 | /* | |
6779 | * rflags is the old, "raw" value of the flags. The new value has | |
6780 | * not been saved yet. | |
6781 | * | |
6782 | * This is correct even for TF set by the guest, because "the | |
6783 | * processor will not generate this exception after the instruction | |
6784 | * that sets the TF flag". | |
6785 | */ | |
6786 | if (unlikely(rflags & X86_EFLAGS_TF)) | |
120c2c4f | 6787 | r = kvm_vcpu_do_singlestep(vcpu); |
60fc3d02 | 6788 | return r; |
6affcbed KH |
6789 | } |
6790 | EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); | |
6791 | ||
4a1e10d5 PB |
6792 | static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) |
6793 | { | |
4a1e10d5 PB |
6794 | if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && |
6795 | (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { | |
82b32774 NA |
6796 | struct kvm_run *kvm_run = vcpu->run; |
6797 | unsigned long eip = kvm_get_linear_rip(vcpu); | |
6798 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
6799 | vcpu->arch.guest_debug_dr7, |
6800 | vcpu->arch.eff_db); | |
6801 | ||
6802 | if (dr6 != 0) { | |
6f43ed01 | 6803 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; |
82b32774 | 6804 | kvm_run->debug.arch.pc = eip; |
4a1e10d5 PB |
6805 | kvm_run->debug.arch.exception = DB_VECTOR; |
6806 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 6807 | *r = 0; |
4a1e10d5 PB |
6808 | return true; |
6809 | } | |
6810 | } | |
6811 | ||
4161a569 NA |
6812 | if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && |
6813 | !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { | |
82b32774 NA |
6814 | unsigned long eip = kvm_get_linear_rip(vcpu); |
6815 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
6816 | vcpu->arch.dr7, |
6817 | vcpu->arch.db); | |
6818 | ||
6819 | if (dr6 != 0) { | |
4d5523cf | 6820 | kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); |
60fc3d02 | 6821 | *r = 1; |
4a1e10d5 PB |
6822 | return true; |
6823 | } | |
6824 | } | |
6825 | ||
6826 | return false; | |
6827 | } | |
6828 | ||
04789b66 LA |
6829 | static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) |
6830 | { | |
2d7921c4 AM |
6831 | switch (ctxt->opcode_len) { |
6832 | case 1: | |
6833 | switch (ctxt->b) { | |
6834 | case 0xe4: /* IN */ | |
6835 | case 0xe5: | |
6836 | case 0xec: | |
6837 | case 0xed: | |
6838 | case 0xe6: /* OUT */ | |
6839 | case 0xe7: | |
6840 | case 0xee: | |
6841 | case 0xef: | |
6842 | case 0x6c: /* INS */ | |
6843 | case 0x6d: | |
6844 | case 0x6e: /* OUTS */ | |
6845 | case 0x6f: | |
6846 | return true; | |
6847 | } | |
6848 | break; | |
6849 | case 2: | |
6850 | switch (ctxt->b) { | |
6851 | case 0x33: /* RDPMC */ | |
6852 | return true; | |
6853 | } | |
6854 | break; | |
04789b66 LA |
6855 | } |
6856 | ||
6857 | return false; | |
6858 | } | |
6859 | ||
736c291c SC |
6860 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
6861 | int emulation_type, void *insn, int insn_len) | |
bbd9b64e | 6862 | { |
95cb2295 | 6863 | int r; |
c9b8b07c | 6864 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
7ae441ea | 6865 | bool writeback = true; |
93c05d3e | 6866 | bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; |
bbd9b64e | 6867 | |
c595ceee PB |
6868 | vcpu->arch.l1tf_flush_l1d = true; |
6869 | ||
93c05d3e XG |
6870 | /* |
6871 | * Clear write_fault_to_shadow_pgtable here to ensure it is | |
6872 | * never reused. | |
6873 | */ | |
6874 | vcpu->arch.write_fault_to_shadow_pgtable = false; | |
26eef70c | 6875 | kvm_clear_exception_queue(vcpu); |
8d7d8102 | 6876 | |
571008da | 6877 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
8ec4722d | 6878 | init_emulate_ctxt(vcpu); |
4a1e10d5 PB |
6879 | |
6880 | /* | |
6881 | * We will reenter on the same instruction since | |
6882 | * we do not set complete_userspace_io. This does not | |
6883 | * handle watchpoints yet, those would be handled in | |
6884 | * the emulate_ops. | |
6885 | */ | |
d391f120 VK |
6886 | if (!(emulation_type & EMULTYPE_SKIP) && |
6887 | kvm_vcpu_check_breakpoint(vcpu, &r)) | |
4a1e10d5 PB |
6888 | return r; |
6889 | ||
9d74191a TY |
6890 | ctxt->interruptibility = 0; |
6891 | ctxt->have_exception = false; | |
e0ad0b47 | 6892 | ctxt->exception.vector = -1; |
9d74191a | 6893 | ctxt->perm_ok = false; |
bbd9b64e | 6894 | |
b51e974f | 6895 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; |
4005996e | 6896 | |
9d74191a | 6897 | r = x86_decode_insn(ctxt, insn, insn_len); |
bbd9b64e | 6898 | |
e46479f8 | 6899 | trace_kvm_emulate_insn_start(vcpu); |
f2b5756b | 6900 | ++vcpu->stat.insn_emulation; |
1d2887e2 | 6901 | if (r != EMULATION_OK) { |
b4000606 | 6902 | if ((emulation_type & EMULTYPE_TRAP_UD) || |
c83fad65 SC |
6903 | (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { |
6904 | kvm_queue_exception(vcpu, UD_VECTOR); | |
60fc3d02 | 6905 | return 1; |
c83fad65 | 6906 | } |
736c291c SC |
6907 | if (reexecute_instruction(vcpu, cr2_or_gpa, |
6908 | write_fault_to_spt, | |
6909 | emulation_type)) | |
60fc3d02 | 6910 | return 1; |
8530a79c | 6911 | if (ctxt->have_exception) { |
c8848cee JD |
6912 | /* |
6913 | * #UD should result in just EMULATION_FAILED, and trap-like | |
6914 | * exception should not be encountered during decode. | |
6915 | */ | |
6916 | WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || | |
6917 | exception_type(ctxt->exception.vector) == EXCPT_TRAP); | |
8530a79c | 6918 | inject_emulated_exception(vcpu); |
60fc3d02 | 6919 | return 1; |
8530a79c | 6920 | } |
e2366171 | 6921 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
6922 | } |
6923 | } | |
6924 | ||
42cbf068 SC |
6925 | if ((emulation_type & EMULTYPE_VMWARE_GP) && |
6926 | !is_vmware_backdoor_opcode(ctxt)) { | |
6927 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 6928 | return 1; |
42cbf068 | 6929 | } |
04789b66 | 6930 | |
1957aa63 SC |
6931 | /* |
6932 | * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks | |
6933 | * for kvm_skip_emulated_instruction(). The caller is responsible for | |
6934 | * updating interruptibility state and injecting single-step #DBs. | |
6935 | */ | |
ba8afb6b | 6936 | if (emulation_type & EMULTYPE_SKIP) { |
9dac77fa | 6937 | kvm_rip_write(vcpu, ctxt->_eip); |
bb663c7a NA |
6938 | if (ctxt->eflags & X86_EFLAGS_RF) |
6939 | kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); | |
60fc3d02 | 6940 | return 1; |
ba8afb6b GN |
6941 | } |
6942 | ||
736c291c | 6943 | if (retry_instruction(ctxt, cr2_or_gpa, emulation_type)) |
60fc3d02 | 6944 | return 1; |
1cb3f3ae | 6945 | |
7ae441ea | 6946 | /* this is needed for vmware backdoor interface to work since it |
4d2179e1 | 6947 | changes registers values during IO operation */ |
7ae441ea GN |
6948 | if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { |
6949 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; | |
dd856efa | 6950 | emulator_invalidate_register_cache(ctxt); |
7ae441ea | 6951 | } |
4d2179e1 | 6952 | |
5cd21917 | 6953 | restart: |
92daa48b SC |
6954 | if (emulation_type & EMULTYPE_PF) { |
6955 | /* Save the faulting GPA (cr2) in the address field */ | |
6956 | ctxt->exception.address = cr2_or_gpa; | |
6957 | ||
6958 | /* With shadow page tables, cr2 contains a GVA or nGPA. */ | |
6959 | if (vcpu->arch.mmu->direct_map) { | |
744e699c SC |
6960 | ctxt->gpa_available = true; |
6961 | ctxt->gpa_val = cr2_or_gpa; | |
92daa48b SC |
6962 | } |
6963 | } else { | |
6964 | /* Sanitize the address out of an abundance of paranoia. */ | |
6965 | ctxt->exception.address = 0; | |
6966 | } | |
0f89b207 | 6967 | |
9d74191a | 6968 | r = x86_emulate_insn(ctxt); |
bbd9b64e | 6969 | |
775fde86 | 6970 | if (r == EMULATION_INTERCEPTED) |
60fc3d02 | 6971 | return 1; |
775fde86 | 6972 | |
d2ddd1c4 | 6973 | if (r == EMULATION_FAILED) { |
736c291c | 6974 | if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt, |
991eebf9 | 6975 | emulation_type)) |
60fc3d02 | 6976 | return 1; |
c3cd7ffa | 6977 | |
e2366171 | 6978 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
6979 | } |
6980 | ||
9d74191a | 6981 | if (ctxt->have_exception) { |
60fc3d02 | 6982 | r = 1; |
ef54bcfe PB |
6983 | if (inject_emulated_exception(vcpu)) |
6984 | return r; | |
d2ddd1c4 | 6985 | } else if (vcpu->arch.pio.count) { |
0912c977 PB |
6986 | if (!vcpu->arch.pio.in) { |
6987 | /* FIXME: return into emulator if single-stepping. */ | |
3457e419 | 6988 | vcpu->arch.pio.count = 0; |
0912c977 | 6989 | } else { |
7ae441ea | 6990 | writeback = false; |
716d51ab GN |
6991 | vcpu->arch.complete_userspace_io = complete_emulated_pio; |
6992 | } | |
60fc3d02 | 6993 | r = 0; |
7ae441ea | 6994 | } else if (vcpu->mmio_needed) { |
bc8a0aaf SC |
6995 | ++vcpu->stat.mmio_exits; |
6996 | ||
7ae441ea GN |
6997 | if (!vcpu->mmio_is_write) |
6998 | writeback = false; | |
60fc3d02 | 6999 | r = 0; |
716d51ab | 7000 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
7ae441ea | 7001 | } else if (r == EMULATION_RESTART) |
5cd21917 | 7002 | goto restart; |
d2ddd1c4 | 7003 | else |
60fc3d02 | 7004 | r = 1; |
f850e2e6 | 7005 | |
7ae441ea | 7006 | if (writeback) { |
afaf0b2f | 7007 | unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); |
9d74191a | 7008 | toggle_interruptibility(vcpu, ctxt->interruptibility); |
7ae441ea | 7009 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
38827dbd | 7010 | if (!ctxt->have_exception || |
75ee23b3 SC |
7011 | exception_type(ctxt->exception.vector) == EXCPT_TRAP) { |
7012 | kvm_rip_write(vcpu, ctxt->eip); | |
60fc3d02 | 7013 | if (r && ctxt->tf) |
120c2c4f | 7014 | r = kvm_vcpu_do_singlestep(vcpu); |
afaf0b2f SC |
7015 | if (kvm_x86_ops.update_emulated_instruction) |
7016 | kvm_x86_ops.update_emulated_instruction(vcpu); | |
38827dbd | 7017 | __kvm_set_rflags(vcpu, ctxt->eflags); |
75ee23b3 | 7018 | } |
6addfc42 PB |
7019 | |
7020 | /* | |
7021 | * For STI, interrupts are shadowed; so KVM_REQ_EVENT will | |
7022 | * do nothing, and it will be requested again as soon as | |
7023 | * the shadow expires. But we still need to check here, | |
7024 | * because POPF has no interrupt shadow. | |
7025 | */ | |
7026 | if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) | |
7027 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7ae441ea GN |
7028 | } else |
7029 | vcpu->arch.emulate_regs_need_sync_to_vcpu = true; | |
e85d28f8 GN |
7030 | |
7031 | return r; | |
de7d789a | 7032 | } |
c60658d1 SC |
7033 | |
7034 | int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) | |
7035 | { | |
7036 | return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); | |
7037 | } | |
7038 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction); | |
7039 | ||
7040 | int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, | |
7041 | void *insn, int insn_len) | |
7042 | { | |
7043 | return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); | |
7044 | } | |
7045 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); | |
de7d789a | 7046 | |
8764ed55 SC |
7047 | static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu) |
7048 | { | |
7049 | vcpu->arch.pio.count = 0; | |
7050 | return 1; | |
7051 | } | |
7052 | ||
45def77e SC |
7053 | static int complete_fast_pio_out(struct kvm_vcpu *vcpu) |
7054 | { | |
7055 | vcpu->arch.pio.count = 0; | |
7056 | ||
7057 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) | |
7058 | return 1; | |
7059 | ||
7060 | return kvm_skip_emulated_instruction(vcpu); | |
7061 | } | |
7062 | ||
dca7f128 SC |
7063 | static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, |
7064 | unsigned short port) | |
de7d789a | 7065 | { |
de3cd117 | 7066 | unsigned long val = kvm_rax_read(vcpu); |
2e3bb4d8 SC |
7067 | int ret = emulator_pio_out(vcpu, size, port, &val, 1); |
7068 | ||
8764ed55 SC |
7069 | if (ret) |
7070 | return ret; | |
45def77e | 7071 | |
8764ed55 SC |
7072 | /* |
7073 | * Workaround userspace that relies on old KVM behavior of %rip being | |
7074 | * incremented prior to exiting to userspace to handle "OUT 0x7e". | |
7075 | */ | |
7076 | if (port == 0x7e && | |
7077 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { | |
7078 | vcpu->arch.complete_userspace_io = | |
7079 | complete_fast_pio_out_port_0x7e; | |
7080 | kvm_skip_emulated_instruction(vcpu); | |
7081 | } else { | |
45def77e SC |
7082 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
7083 | vcpu->arch.complete_userspace_io = complete_fast_pio_out; | |
7084 | } | |
8764ed55 | 7085 | return 0; |
de7d789a | 7086 | } |
de7d789a | 7087 | |
8370c3d0 TL |
7088 | static int complete_fast_pio_in(struct kvm_vcpu *vcpu) |
7089 | { | |
7090 | unsigned long val; | |
7091 | ||
7092 | /* We should only ever be called with arch.pio.count equal to 1 */ | |
7093 | BUG_ON(vcpu->arch.pio.count != 1); | |
7094 | ||
45def77e SC |
7095 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { |
7096 | vcpu->arch.pio.count = 0; | |
7097 | return 1; | |
7098 | } | |
7099 | ||
8370c3d0 | 7100 | /* For size less than 4 we merge, else we zero extend */ |
de3cd117 | 7101 | val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 TL |
7102 | |
7103 | /* | |
2e3bb4d8 | 7104 | * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform |
8370c3d0 TL |
7105 | * the copy and tracing |
7106 | */ | |
2e3bb4d8 | 7107 | emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); |
de3cd117 | 7108 | kvm_rax_write(vcpu, val); |
8370c3d0 | 7109 | |
45def77e | 7110 | return kvm_skip_emulated_instruction(vcpu); |
8370c3d0 TL |
7111 | } |
7112 | ||
dca7f128 SC |
7113 | static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size, |
7114 | unsigned short port) | |
8370c3d0 TL |
7115 | { |
7116 | unsigned long val; | |
7117 | int ret; | |
7118 | ||
7119 | /* For size less than 4 we merge, else we zero extend */ | |
de3cd117 | 7120 | val = (size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 | 7121 | |
2e3bb4d8 | 7122 | ret = emulator_pio_in(vcpu, size, port, &val, 1); |
8370c3d0 | 7123 | if (ret) { |
de3cd117 | 7124 | kvm_rax_write(vcpu, val); |
8370c3d0 TL |
7125 | return ret; |
7126 | } | |
7127 | ||
45def77e | 7128 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
8370c3d0 TL |
7129 | vcpu->arch.complete_userspace_io = complete_fast_pio_in; |
7130 | ||
7131 | return 0; | |
7132 | } | |
dca7f128 SC |
7133 | |
7134 | int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in) | |
7135 | { | |
45def77e | 7136 | int ret; |
dca7f128 | 7137 | |
dca7f128 | 7138 | if (in) |
45def77e | 7139 | ret = kvm_fast_pio_in(vcpu, size, port); |
dca7f128 | 7140 | else |
45def77e SC |
7141 | ret = kvm_fast_pio_out(vcpu, size, port); |
7142 | return ret && kvm_skip_emulated_instruction(vcpu); | |
dca7f128 SC |
7143 | } |
7144 | EXPORT_SYMBOL_GPL(kvm_fast_pio); | |
8370c3d0 | 7145 | |
251a5fd6 | 7146 | static int kvmclock_cpu_down_prep(unsigned int cpu) |
8cfdc000 | 7147 | { |
0a3aee0d | 7148 | __this_cpu_write(cpu_tsc_khz, 0); |
251a5fd6 | 7149 | return 0; |
8cfdc000 ZA |
7150 | } |
7151 | ||
7152 | static void tsc_khz_changed(void *data) | |
c8076604 | 7153 | { |
8cfdc000 ZA |
7154 | struct cpufreq_freqs *freq = data; |
7155 | unsigned long khz = 0; | |
7156 | ||
7157 | if (data) | |
7158 | khz = freq->new; | |
7159 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) | |
7160 | khz = cpufreq_quick_get(raw_smp_processor_id()); | |
7161 | if (!khz) | |
7162 | khz = tsc_khz; | |
0a3aee0d | 7163 | __this_cpu_write(cpu_tsc_khz, khz); |
c8076604 GH |
7164 | } |
7165 | ||
5fa4ec9c | 7166 | #ifdef CONFIG_X86_64 |
0092e434 VK |
7167 | static void kvm_hyperv_tsc_notifier(void) |
7168 | { | |
0092e434 VK |
7169 | struct kvm *kvm; |
7170 | struct kvm_vcpu *vcpu; | |
7171 | int cpu; | |
7172 | ||
0d9ce162 | 7173 | mutex_lock(&kvm_lock); |
0092e434 VK |
7174 | list_for_each_entry(kvm, &vm_list, vm_list) |
7175 | kvm_make_mclock_inprogress_request(kvm); | |
7176 | ||
7177 | hyperv_stop_tsc_emulation(); | |
7178 | ||
7179 | /* TSC frequency always matches when on Hyper-V */ | |
7180 | for_each_present_cpu(cpu) | |
7181 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; | |
7182 | kvm_max_guest_tsc_khz = tsc_khz; | |
7183 | ||
7184 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
7185 | struct kvm_arch *ka = &kvm->arch; | |
7186 | ||
7187 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
7188 | ||
7189 | pvclock_update_vm_gtod_copy(kvm); | |
7190 | ||
7191 | kvm_for_each_vcpu(cpu, vcpu, kvm) | |
7192 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
7193 | ||
7194 | kvm_for_each_vcpu(cpu, vcpu, kvm) | |
7195 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); | |
7196 | ||
7197 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
7198 | } | |
0d9ce162 | 7199 | mutex_unlock(&kvm_lock); |
0092e434 | 7200 | } |
5fa4ec9c | 7201 | #endif |
0092e434 | 7202 | |
df24014a | 7203 | static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) |
c8076604 | 7204 | { |
c8076604 GH |
7205 | struct kvm *kvm; |
7206 | struct kvm_vcpu *vcpu; | |
7207 | int i, send_ipi = 0; | |
7208 | ||
8cfdc000 ZA |
7209 | /* |
7210 | * We allow guests to temporarily run on slowing clocks, | |
7211 | * provided we notify them after, or to run on accelerating | |
7212 | * clocks, provided we notify them before. Thus time never | |
7213 | * goes backwards. | |
7214 | * | |
7215 | * However, we have a problem. We can't atomically update | |
7216 | * the frequency of a given CPU from this function; it is | |
7217 | * merely a notifier, which can be called from any CPU. | |
7218 | * Changing the TSC frequency at arbitrary points in time | |
7219 | * requires a recomputation of local variables related to | |
7220 | * the TSC for each VCPU. We must flag these local variables | |
7221 | * to be updated and be sure the update takes place with the | |
7222 | * new frequency before any guests proceed. | |
7223 | * | |
7224 | * Unfortunately, the combination of hotplug CPU and frequency | |
7225 | * change creates an intractable locking scenario; the order | |
7226 | * of when these callouts happen is undefined with respect to | |
7227 | * CPU hotplug, and they can race with each other. As such, | |
7228 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is | |
7229 | * undefined; you can actually have a CPU frequency change take | |
7230 | * place in between the computation of X and the setting of the | |
7231 | * variable. To protect against this problem, all updates of | |
7232 | * the per_cpu tsc_khz variable are done in an interrupt | |
7233 | * protected IPI, and all callers wishing to update the value | |
7234 | * must wait for a synchronous IPI to complete (which is trivial | |
7235 | * if the caller is on the CPU already). This establishes the | |
7236 | * necessary total order on variable updates. | |
7237 | * | |
7238 | * Note that because a guest time update may take place | |
7239 | * anytime after the setting of the VCPU's request bit, the | |
7240 | * correct TSC value must be set before the request. However, | |
7241 | * to ensure the update actually makes it to any guest which | |
7242 | * starts running in hardware virtualization between the set | |
7243 | * and the acquisition of the spinlock, we must also ping the | |
7244 | * CPU after setting the request bit. | |
7245 | * | |
7246 | */ | |
7247 | ||
df24014a | 7248 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 7249 | |
0d9ce162 | 7250 | mutex_lock(&kvm_lock); |
c8076604 | 7251 | list_for_each_entry(kvm, &vm_list, vm_list) { |
988a2cae | 7252 | kvm_for_each_vcpu(i, vcpu, kvm) { |
df24014a | 7253 | if (vcpu->cpu != cpu) |
c8076604 | 7254 | continue; |
c285545f | 7255 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0d9ce162 | 7256 | if (vcpu->cpu != raw_smp_processor_id()) |
8cfdc000 | 7257 | send_ipi = 1; |
c8076604 GH |
7258 | } |
7259 | } | |
0d9ce162 | 7260 | mutex_unlock(&kvm_lock); |
c8076604 GH |
7261 | |
7262 | if (freq->old < freq->new && send_ipi) { | |
7263 | /* | |
7264 | * We upscale the frequency. Must make the guest | |
7265 | * doesn't see old kvmclock values while running with | |
7266 | * the new frequency, otherwise we risk the guest sees | |
7267 | * time go backwards. | |
7268 | * | |
7269 | * In case we update the frequency for another cpu | |
7270 | * (which might be in guest context) send an interrupt | |
7271 | * to kick the cpu out of guest context. Next time | |
7272 | * guest context is entered kvmclock will be updated, | |
7273 | * so the guest will not see stale values. | |
7274 | */ | |
df24014a | 7275 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 7276 | } |
df24014a VK |
7277 | } |
7278 | ||
7279 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | |
7280 | void *data) | |
7281 | { | |
7282 | struct cpufreq_freqs *freq = data; | |
7283 | int cpu; | |
7284 | ||
7285 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) | |
7286 | return 0; | |
7287 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) | |
7288 | return 0; | |
7289 | ||
7290 | for_each_cpu(cpu, freq->policy->cpus) | |
7291 | __kvmclock_cpufreq_notifier(freq, cpu); | |
7292 | ||
c8076604 GH |
7293 | return 0; |
7294 | } | |
7295 | ||
7296 | static struct notifier_block kvmclock_cpufreq_notifier_block = { | |
8cfdc000 ZA |
7297 | .notifier_call = kvmclock_cpufreq_notifier |
7298 | }; | |
7299 | ||
251a5fd6 | 7300 | static int kvmclock_cpu_online(unsigned int cpu) |
8cfdc000 | 7301 | { |
251a5fd6 SAS |
7302 | tsc_khz_changed(NULL); |
7303 | return 0; | |
8cfdc000 ZA |
7304 | } |
7305 | ||
b820cc0c ZA |
7306 | static void kvm_timer_init(void) |
7307 | { | |
c285545f | 7308 | max_tsc_khz = tsc_khz; |
460dd42e | 7309 | |
b820cc0c | 7310 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
c285545f | 7311 | #ifdef CONFIG_CPU_FREQ |
aaec7c03 | 7312 | struct cpufreq_policy *policy; |
758f588d BP |
7313 | int cpu; |
7314 | ||
3e26f230 | 7315 | cpu = get_cpu(); |
aaec7c03 | 7316 | policy = cpufreq_cpu_get(cpu); |
9a11997e WL |
7317 | if (policy) { |
7318 | if (policy->cpuinfo.max_freq) | |
7319 | max_tsc_khz = policy->cpuinfo.max_freq; | |
7320 | cpufreq_cpu_put(policy); | |
7321 | } | |
3e26f230 | 7322 | put_cpu(); |
c285545f | 7323 | #endif |
b820cc0c ZA |
7324 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
7325 | CPUFREQ_TRANSITION_NOTIFIER); | |
7326 | } | |
460dd42e | 7327 | |
73c1b41e | 7328 | cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", |
251a5fd6 | 7329 | kvmclock_cpu_online, kvmclock_cpu_down_prep); |
b820cc0c ZA |
7330 | } |
7331 | ||
dd60d217 AK |
7332 | DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
7333 | EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu); | |
ff9d07a0 | 7334 | |
f5132b01 | 7335 | int kvm_is_in_guest(void) |
ff9d07a0 | 7336 | { |
086c9855 | 7337 | return __this_cpu_read(current_vcpu) != NULL; |
ff9d07a0 ZY |
7338 | } |
7339 | ||
7340 | static int kvm_is_user_mode(void) | |
7341 | { | |
7342 | int user_mode = 3; | |
dcf46b94 | 7343 | |
086c9855 | 7344 | if (__this_cpu_read(current_vcpu)) |
afaf0b2f | 7345 | user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu)); |
dcf46b94 | 7346 | |
ff9d07a0 ZY |
7347 | return user_mode != 0; |
7348 | } | |
7349 | ||
7350 | static unsigned long kvm_get_guest_ip(void) | |
7351 | { | |
7352 | unsigned long ip = 0; | |
dcf46b94 | 7353 | |
086c9855 AS |
7354 | if (__this_cpu_read(current_vcpu)) |
7355 | ip = kvm_rip_read(__this_cpu_read(current_vcpu)); | |
dcf46b94 | 7356 | |
ff9d07a0 ZY |
7357 | return ip; |
7358 | } | |
7359 | ||
8479e04e LK |
7360 | static void kvm_handle_intel_pt_intr(void) |
7361 | { | |
7362 | struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu); | |
7363 | ||
7364 | kvm_make_request(KVM_REQ_PMI, vcpu); | |
7365 | __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, | |
7366 | (unsigned long *)&vcpu->arch.pmu.global_status); | |
7367 | } | |
7368 | ||
ff9d07a0 ZY |
7369 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
7370 | .is_in_guest = kvm_is_in_guest, | |
7371 | .is_user_mode = kvm_is_user_mode, | |
7372 | .get_guest_ip = kvm_get_guest_ip, | |
8479e04e | 7373 | .handle_intel_pt_intr = kvm_handle_intel_pt_intr, |
ff9d07a0 ZY |
7374 | }; |
7375 | ||
16e8d74d MT |
7376 | #ifdef CONFIG_X86_64 |
7377 | static void pvclock_gtod_update_fn(struct work_struct *work) | |
7378 | { | |
d828199e MT |
7379 | struct kvm *kvm; |
7380 | ||
7381 | struct kvm_vcpu *vcpu; | |
7382 | int i; | |
7383 | ||
0d9ce162 | 7384 | mutex_lock(&kvm_lock); |
d828199e MT |
7385 | list_for_each_entry(kvm, &vm_list, vm_list) |
7386 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 7387 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
d828199e | 7388 | atomic_set(&kvm_guest_has_master_clock, 0); |
0d9ce162 | 7389 | mutex_unlock(&kvm_lock); |
16e8d74d MT |
7390 | } |
7391 | ||
7392 | static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn); | |
7393 | ||
7394 | /* | |
7395 | * Notification about pvclock gtod data update. | |
7396 | */ | |
7397 | static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, | |
7398 | void *priv) | |
7399 | { | |
7400 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
7401 | struct timekeeper *tk = priv; | |
7402 | ||
7403 | update_pvclock_gtod(tk); | |
7404 | ||
7405 | /* disable master clock if host does not trust, or does not | |
b0c39dc6 | 7406 | * use, TSC based clocksource. |
16e8d74d | 7407 | */ |
b0c39dc6 | 7408 | if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && |
16e8d74d MT |
7409 | atomic_read(&kvm_guest_has_master_clock) != 0) |
7410 | queue_work(system_long_wq, &pvclock_gtod_work); | |
7411 | ||
7412 | return 0; | |
7413 | } | |
7414 | ||
7415 | static struct notifier_block pvclock_gtod_notifier = { | |
7416 | .notifier_call = pvclock_gtod_notify, | |
7417 | }; | |
7418 | #endif | |
7419 | ||
f8c16bba | 7420 | int kvm_arch_init(void *opaque) |
043405e1 | 7421 | { |
d008dfdb | 7422 | struct kvm_x86_init_ops *ops = opaque; |
b820cc0c | 7423 | int r; |
f8c16bba | 7424 | |
afaf0b2f | 7425 | if (kvm_x86_ops.hardware_enable) { |
f8c16bba | 7426 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
56c6d28a ZX |
7427 | r = -EEXIST; |
7428 | goto out; | |
f8c16bba ZX |
7429 | } |
7430 | ||
7431 | if (!ops->cpu_has_kvm_support()) { | |
ef935c25 | 7432 | pr_err_ratelimited("kvm: no hardware support\n"); |
56c6d28a ZX |
7433 | r = -EOPNOTSUPP; |
7434 | goto out; | |
f8c16bba ZX |
7435 | } |
7436 | if (ops->disabled_by_bios()) { | |
ef935c25 | 7437 | pr_err_ratelimited("kvm: disabled by bios\n"); |
56c6d28a ZX |
7438 | r = -EOPNOTSUPP; |
7439 | goto out; | |
f8c16bba ZX |
7440 | } |
7441 | ||
b666a4b6 MO |
7442 | /* |
7443 | * KVM explicitly assumes that the guest has an FPU and | |
7444 | * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the | |
7445 | * vCPU's FPU state as a fxregs_state struct. | |
7446 | */ | |
7447 | if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) { | |
7448 | printk(KERN_ERR "kvm: inadequate fpu\n"); | |
7449 | r = -EOPNOTSUPP; | |
7450 | goto out; | |
7451 | } | |
7452 | ||
013f6a5d | 7453 | r = -ENOMEM; |
ed8e4812 | 7454 | x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu), |
b666a4b6 MO |
7455 | __alignof__(struct fpu), SLAB_ACCOUNT, |
7456 | NULL); | |
7457 | if (!x86_fpu_cache) { | |
7458 | printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n"); | |
7459 | goto out; | |
7460 | } | |
7461 | ||
c9b8b07c SC |
7462 | x86_emulator_cache = kvm_alloc_emulator_cache(); |
7463 | if (!x86_emulator_cache) { | |
7464 | pr_err("kvm: failed to allocate cache for x86 emulator\n"); | |
7465 | goto out_free_x86_fpu_cache; | |
7466 | } | |
7467 | ||
013f6a5d MT |
7468 | shared_msrs = alloc_percpu(struct kvm_shared_msrs); |
7469 | if (!shared_msrs) { | |
7470 | printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n"); | |
c9b8b07c | 7471 | goto out_free_x86_emulator_cache; |
013f6a5d MT |
7472 | } |
7473 | ||
97db56ce AK |
7474 | r = kvm_mmu_module_init(); |
7475 | if (r) | |
013f6a5d | 7476 | goto out_free_percpu; |
97db56ce | 7477 | |
7b52345e | 7478 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
ffb128c8 | 7479 | PT_DIRTY_MASK, PT64_NX_MASK, 0, |
d0ec49d4 | 7480 | PT_PRESENT_MASK, 0, sme_me_mask); |
b820cc0c | 7481 | kvm_timer_init(); |
c8076604 | 7482 | |
ff9d07a0 ZY |
7483 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
7484 | ||
cfc48181 | 7485 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
2acf923e | 7486 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
cfc48181 SC |
7487 | supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0; |
7488 | } | |
2acf923e | 7489 | |
c5cc421b | 7490 | kvm_lapic_init(); |
0c5f81da WL |
7491 | if (pi_inject_timer == -1) |
7492 | pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER); | |
16e8d74d MT |
7493 | #ifdef CONFIG_X86_64 |
7494 | pvclock_gtod_register_notifier(&pvclock_gtod_notifier); | |
0092e434 | 7495 | |
5fa4ec9c | 7496 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 | 7497 | set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); |
16e8d74d MT |
7498 | #endif |
7499 | ||
f8c16bba | 7500 | return 0; |
56c6d28a | 7501 | |
013f6a5d MT |
7502 | out_free_percpu: |
7503 | free_percpu(shared_msrs); | |
c9b8b07c SC |
7504 | out_free_x86_emulator_cache: |
7505 | kmem_cache_destroy(x86_emulator_cache); | |
b666a4b6 MO |
7506 | out_free_x86_fpu_cache: |
7507 | kmem_cache_destroy(x86_fpu_cache); | |
56c6d28a | 7508 | out: |
56c6d28a | 7509 | return r; |
043405e1 | 7510 | } |
8776e519 | 7511 | |
f8c16bba ZX |
7512 | void kvm_arch_exit(void) |
7513 | { | |
0092e434 | 7514 | #ifdef CONFIG_X86_64 |
5fa4ec9c | 7515 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 VK |
7516 | clear_hv_tscchange_cb(); |
7517 | #endif | |
cef84c30 | 7518 | kvm_lapic_exit(); |
ff9d07a0 ZY |
7519 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
7520 | ||
888d256e JK |
7521 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
7522 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, | |
7523 | CPUFREQ_TRANSITION_NOTIFIER); | |
251a5fd6 | 7524 | cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); |
16e8d74d MT |
7525 | #ifdef CONFIG_X86_64 |
7526 | pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); | |
7527 | #endif | |
afaf0b2f | 7528 | kvm_x86_ops.hardware_enable = NULL; |
56c6d28a | 7529 | kvm_mmu_module_exit(); |
013f6a5d | 7530 | free_percpu(shared_msrs); |
b666a4b6 | 7531 | kmem_cache_destroy(x86_fpu_cache); |
56c6d28a | 7532 | } |
f8c16bba | 7533 | |
5cb56059 | 7534 | int kvm_vcpu_halt(struct kvm_vcpu *vcpu) |
8776e519 HB |
7535 | { |
7536 | ++vcpu->stat.halt_exits; | |
35754c98 | 7537 | if (lapic_in_kernel(vcpu)) { |
a4535290 | 7538 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
8776e519 HB |
7539 | return 1; |
7540 | } else { | |
7541 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
7542 | return 0; | |
7543 | } | |
7544 | } | |
5cb56059 JS |
7545 | EXPORT_SYMBOL_GPL(kvm_vcpu_halt); |
7546 | ||
7547 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
7548 | { | |
6affcbed KH |
7549 | int ret = kvm_skip_emulated_instruction(vcpu); |
7550 | /* | |
7551 | * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered | |
7552 | * KVM_EXIT_DEBUG here. | |
7553 | */ | |
7554 | return kvm_vcpu_halt(vcpu) && ret; | |
5cb56059 | 7555 | } |
8776e519 HB |
7556 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
7557 | ||
8ef81a9a | 7558 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
7559 | static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, |
7560 | unsigned long clock_type) | |
7561 | { | |
7562 | struct kvm_clock_pairing clock_pairing; | |
899a31f5 | 7563 | struct timespec64 ts; |
80fbd89c | 7564 | u64 cycle; |
55dd00a7 MT |
7565 | int ret; |
7566 | ||
7567 | if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK) | |
7568 | return -KVM_EOPNOTSUPP; | |
7569 | ||
7570 | if (kvm_get_walltime_and_clockread(&ts, &cycle) == false) | |
7571 | return -KVM_EOPNOTSUPP; | |
7572 | ||
7573 | clock_pairing.sec = ts.tv_sec; | |
7574 | clock_pairing.nsec = ts.tv_nsec; | |
7575 | clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle); | |
7576 | clock_pairing.flags = 0; | |
bcbfbd8e | 7577 | memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad)); |
55dd00a7 MT |
7578 | |
7579 | ret = 0; | |
7580 | if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, | |
7581 | sizeof(struct kvm_clock_pairing))) | |
7582 | ret = -KVM_EFAULT; | |
7583 | ||
7584 | return ret; | |
7585 | } | |
8ef81a9a | 7586 | #endif |
55dd00a7 | 7587 | |
6aef266c SV |
7588 | /* |
7589 | * kvm_pv_kick_cpu_op: Kick a vcpu. | |
7590 | * | |
7591 | * @apicid - apicid of vcpu to be kicked. | |
7592 | */ | |
7593 | static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid) | |
7594 | { | |
24d2166b | 7595 | struct kvm_lapic_irq lapic_irq; |
6aef266c | 7596 | |
150a84fe | 7597 | lapic_irq.shorthand = APIC_DEST_NOSHORT; |
c96001c5 | 7598 | lapic_irq.dest_mode = APIC_DEST_PHYSICAL; |
ebd28fcb | 7599 | lapic_irq.level = 0; |
24d2166b | 7600 | lapic_irq.dest_id = apicid; |
93bbf0b8 | 7601 | lapic_irq.msi_redir_hint = false; |
6aef266c | 7602 | |
24d2166b | 7603 | lapic_irq.delivery_mode = APIC_DM_REMRD; |
795a149e | 7604 | kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL); |
6aef266c SV |
7605 | } |
7606 | ||
4e19c36f SS |
7607 | bool kvm_apicv_activated(struct kvm *kvm) |
7608 | { | |
7609 | return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); | |
7610 | } | |
7611 | EXPORT_SYMBOL_GPL(kvm_apicv_activated); | |
7612 | ||
7613 | void kvm_apicv_init(struct kvm *kvm, bool enable) | |
7614 | { | |
7615 | if (enable) | |
7616 | clear_bit(APICV_INHIBIT_REASON_DISABLE, | |
7617 | &kvm->arch.apicv_inhibit_reasons); | |
7618 | else | |
7619 | set_bit(APICV_INHIBIT_REASON_DISABLE, | |
7620 | &kvm->arch.apicv_inhibit_reasons); | |
7621 | } | |
7622 | EXPORT_SYMBOL_GPL(kvm_apicv_init); | |
7623 | ||
71506297 WL |
7624 | static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id) |
7625 | { | |
7626 | struct kvm_vcpu *target = NULL; | |
7627 | struct kvm_apic_map *map; | |
7628 | ||
7629 | rcu_read_lock(); | |
7630 | map = rcu_dereference(kvm->arch.apic_map); | |
7631 | ||
7632 | if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) | |
7633 | target = map->phys_map[dest_id]->vcpu; | |
7634 | ||
7635 | rcu_read_unlock(); | |
7636 | ||
266e85a5 | 7637 | if (target && READ_ONCE(target->ready)) |
71506297 WL |
7638 | kvm_vcpu_yield_to(target); |
7639 | } | |
7640 | ||
8776e519 HB |
7641 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
7642 | { | |
7643 | unsigned long nr, a0, a1, a2, a3, ret; | |
6356ee0c | 7644 | int op_64_bit; |
8776e519 | 7645 | |
696ca779 RK |
7646 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
7647 | return kvm_hv_hypercall(vcpu); | |
55cd8e5a | 7648 | |
de3cd117 SC |
7649 | nr = kvm_rax_read(vcpu); |
7650 | a0 = kvm_rbx_read(vcpu); | |
7651 | a1 = kvm_rcx_read(vcpu); | |
7652 | a2 = kvm_rdx_read(vcpu); | |
7653 | a3 = kvm_rsi_read(vcpu); | |
8776e519 | 7654 | |
229456fc | 7655 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
2714d1d3 | 7656 | |
a449c7aa NA |
7657 | op_64_bit = is_64_bit_mode(vcpu); |
7658 | if (!op_64_bit) { | |
8776e519 HB |
7659 | nr &= 0xFFFFFFFF; |
7660 | a0 &= 0xFFFFFFFF; | |
7661 | a1 &= 0xFFFFFFFF; | |
7662 | a2 &= 0xFFFFFFFF; | |
7663 | a3 &= 0xFFFFFFFF; | |
7664 | } | |
7665 | ||
afaf0b2f | 7666 | if (kvm_x86_ops.get_cpl(vcpu) != 0) { |
07708c4a | 7667 | ret = -KVM_EPERM; |
696ca779 | 7668 | goto out; |
07708c4a JK |
7669 | } |
7670 | ||
8776e519 | 7671 | switch (nr) { |
b93463aa AK |
7672 | case KVM_HC_VAPIC_POLL_IRQ: |
7673 | ret = 0; | |
7674 | break; | |
6aef266c SV |
7675 | case KVM_HC_KICK_CPU: |
7676 | kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); | |
266e85a5 | 7677 | kvm_sched_yield(vcpu->kvm, a1); |
6aef266c SV |
7678 | ret = 0; |
7679 | break; | |
8ef81a9a | 7680 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
7681 | case KVM_HC_CLOCK_PAIRING: |
7682 | ret = kvm_pv_clock_pairing(vcpu, a0, a1); | |
7683 | break; | |
1ed199a4 | 7684 | #endif |
4180bf1b WL |
7685 | case KVM_HC_SEND_IPI: |
7686 | ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); | |
7687 | break; | |
71506297 WL |
7688 | case KVM_HC_SCHED_YIELD: |
7689 | kvm_sched_yield(vcpu->kvm, a0); | |
7690 | ret = 0; | |
7691 | break; | |
8776e519 HB |
7692 | default: |
7693 | ret = -KVM_ENOSYS; | |
7694 | break; | |
7695 | } | |
696ca779 | 7696 | out: |
a449c7aa NA |
7697 | if (!op_64_bit) |
7698 | ret = (u32)ret; | |
de3cd117 | 7699 | kvm_rax_write(vcpu, ret); |
6356ee0c | 7700 | |
f11c3a8d | 7701 | ++vcpu->stat.hypercalls; |
6356ee0c | 7702 | return kvm_skip_emulated_instruction(vcpu); |
8776e519 HB |
7703 | } |
7704 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
7705 | ||
b6785def | 7706 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt) |
8776e519 | 7707 | { |
d6aa1000 | 7708 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
8776e519 | 7709 | char instruction[3]; |
5fdbf976 | 7710 | unsigned long rip = kvm_rip_read(vcpu); |
8776e519 | 7711 | |
afaf0b2f | 7712 | kvm_x86_ops.patch_hypercall(vcpu, instruction); |
8776e519 | 7713 | |
ce2e852e DV |
7714 | return emulator_write_emulated(ctxt, rip, instruction, 3, |
7715 | &ctxt->exception); | |
8776e519 HB |
7716 | } |
7717 | ||
851ba692 | 7718 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
b6c7a5dc | 7719 | { |
782d422b MG |
7720 | return vcpu->run->request_interrupt_window && |
7721 | likely(!pic_in_kernel(vcpu->kvm)); | |
b6c7a5dc HB |
7722 | } |
7723 | ||
851ba692 | 7724 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
b6c7a5dc | 7725 | { |
851ba692 AK |
7726 | struct kvm_run *kvm_run = vcpu->run; |
7727 | ||
91586a3b | 7728 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
f077825a | 7729 | kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0; |
2d3ad1f4 | 7730 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
b6c7a5dc | 7731 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
127a457a MG |
7732 | kvm_run->ready_for_interrupt_injection = |
7733 | pic_in_kernel(vcpu->kvm) || | |
782d422b | 7734 | kvm_vcpu_ready_for_interrupt_injection(vcpu); |
b6c7a5dc HB |
7735 | } |
7736 | ||
95ba8273 GN |
7737 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
7738 | { | |
7739 | int max_irr, tpr; | |
7740 | ||
afaf0b2f | 7741 | if (!kvm_x86_ops.update_cr8_intercept) |
95ba8273 GN |
7742 | return; |
7743 | ||
bce87cce | 7744 | if (!lapic_in_kernel(vcpu)) |
88c808fd AK |
7745 | return; |
7746 | ||
d62caabb AS |
7747 | if (vcpu->arch.apicv_active) |
7748 | return; | |
7749 | ||
8db3baa2 GN |
7750 | if (!vcpu->arch.apic->vapic_addr) |
7751 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
7752 | else | |
7753 | max_irr = -1; | |
95ba8273 GN |
7754 | |
7755 | if (max_irr != -1) | |
7756 | max_irr >>= 4; | |
7757 | ||
7758 | tpr = kvm_lapic_get_cr8(vcpu); | |
7759 | ||
afaf0b2f | 7760 | kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr); |
95ba8273 GN |
7761 | } |
7762 | ||
c9d40913 | 7763 | static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) |
95ba8273 | 7764 | { |
b6b8a145 | 7765 | int r; |
c6b22f59 | 7766 | bool can_inject = true; |
b6b8a145 | 7767 | |
95ba8273 | 7768 | /* try to reinject previous events if any */ |
664f8e26 | 7769 | |
c6b22f59 | 7770 | if (vcpu->arch.exception.injected) { |
afaf0b2f | 7771 | kvm_x86_ops.queue_exception(vcpu); |
c6b22f59 PB |
7772 | can_inject = false; |
7773 | } | |
664f8e26 | 7774 | /* |
a042c26f LA |
7775 | * Do not inject an NMI or interrupt if there is a pending |
7776 | * exception. Exceptions and interrupts are recognized at | |
7777 | * instruction boundaries, i.e. the start of an instruction. | |
7778 | * Trap-like exceptions, e.g. #DB, have higher priority than | |
7779 | * NMIs and interrupts, i.e. traps are recognized before an | |
7780 | * NMI/interrupt that's pending on the same instruction. | |
7781 | * Fault-like exceptions, e.g. #GP and #PF, are the lowest | |
7782 | * priority, but are only generated (pended) during instruction | |
7783 | * execution, i.e. a pending fault-like exception means the | |
7784 | * fault occurred on the *previous* instruction and must be | |
7785 | * serviced prior to recognizing any new events in order to | |
7786 | * fully complete the previous instruction. | |
664f8e26 | 7787 | */ |
1a680e35 | 7788 | else if (!vcpu->arch.exception.pending) { |
c6b22f59 | 7789 | if (vcpu->arch.nmi_injected) { |
afaf0b2f | 7790 | kvm_x86_ops.set_nmi(vcpu); |
c6b22f59 PB |
7791 | can_inject = false; |
7792 | } else if (vcpu->arch.interrupt.injected) { | |
afaf0b2f | 7793 | kvm_x86_ops.set_irq(vcpu); |
c6b22f59 PB |
7794 | can_inject = false; |
7795 | } | |
664f8e26 WL |
7796 | } |
7797 | ||
3b82b8d7 SC |
7798 | WARN_ON_ONCE(vcpu->arch.exception.injected && |
7799 | vcpu->arch.exception.pending); | |
7800 | ||
1a680e35 LA |
7801 | /* |
7802 | * Call check_nested_events() even if we reinjected a previous event | |
7803 | * in order for caller to determine if it should require immediate-exit | |
7804 | * from L2 to L1 due to pending L1 events which require exit | |
7805 | * from L2 to L1. | |
7806 | */ | |
56083bdf | 7807 | if (is_guest_mode(vcpu)) { |
33b22172 | 7808 | r = kvm_x86_ops.nested_ops->check_events(vcpu); |
c9d40913 PB |
7809 | if (r < 0) |
7810 | goto busy; | |
664f8e26 WL |
7811 | } |
7812 | ||
7813 | /* try to inject new event if pending */ | |
b59bb7bd | 7814 | if (vcpu->arch.exception.pending) { |
5c1c85d0 AK |
7815 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
7816 | vcpu->arch.exception.has_error_code, | |
7817 | vcpu->arch.exception.error_code); | |
d6e8c854 | 7818 | |
664f8e26 WL |
7819 | vcpu->arch.exception.pending = false; |
7820 | vcpu->arch.exception.injected = true; | |
7821 | ||
d6e8c854 NA |
7822 | if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) |
7823 | __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | | |
7824 | X86_EFLAGS_RF); | |
7825 | ||
f10c729f | 7826 | if (vcpu->arch.exception.nr == DB_VECTOR) { |
f10c729f JM |
7827 | kvm_deliver_exception_payload(vcpu); |
7828 | if (vcpu->arch.dr7 & DR7_GD) { | |
7829 | vcpu->arch.dr7 &= ~DR7_GD; | |
7830 | kvm_update_dr7(vcpu); | |
7831 | } | |
6bdf0662 NA |
7832 | } |
7833 | ||
afaf0b2f | 7834 | kvm_x86_ops.queue_exception(vcpu); |
c6b22f59 | 7835 | can_inject = false; |
1a680e35 LA |
7836 | } |
7837 | ||
c9d40913 PB |
7838 | /* |
7839 | * Finally, inject interrupt events. If an event cannot be injected | |
7840 | * due to architectural conditions (e.g. IF=0) a window-open exit | |
7841 | * will re-request KVM_REQ_EVENT. Sometimes however an event is pending | |
7842 | * and can architecturally be injected, but we cannot do it right now: | |
7843 | * an interrupt could have arrived just now and we have to inject it | |
7844 | * as a vmexit, or there could already an event in the queue, which is | |
7845 | * indicated by can_inject. In that case we request an immediate exit | |
7846 | * in order to make progress and get back here for another iteration. | |
7847 | * The kvm_x86_ops hooks communicate this by returning -EBUSY. | |
7848 | */ | |
7849 | if (vcpu->arch.smi_pending) { | |
7850 | r = can_inject ? kvm_x86_ops.smi_allowed(vcpu, true) : -EBUSY; | |
7851 | if (r < 0) | |
7852 | goto busy; | |
7853 | if (r) { | |
7854 | vcpu->arch.smi_pending = false; | |
7855 | ++vcpu->arch.smi_count; | |
7856 | enter_smm(vcpu); | |
7857 | can_inject = false; | |
7858 | } else | |
7859 | kvm_x86_ops.enable_smi_window(vcpu); | |
7860 | } | |
7861 | ||
7862 | if (vcpu->arch.nmi_pending) { | |
7863 | r = can_inject ? kvm_x86_ops.nmi_allowed(vcpu, true) : -EBUSY; | |
7864 | if (r < 0) | |
7865 | goto busy; | |
7866 | if (r) { | |
7867 | --vcpu->arch.nmi_pending; | |
7868 | vcpu->arch.nmi_injected = true; | |
7869 | kvm_x86_ops.set_nmi(vcpu); | |
7870 | can_inject = false; | |
7871 | WARN_ON(kvm_x86_ops.nmi_allowed(vcpu, true) < 0); | |
7872 | } | |
7873 | if (vcpu->arch.nmi_pending) | |
7874 | kvm_x86_ops.enable_nmi_window(vcpu); | |
7875 | } | |
1a680e35 | 7876 | |
c9d40913 PB |
7877 | if (kvm_cpu_has_injectable_intr(vcpu)) { |
7878 | r = can_inject ? kvm_x86_ops.interrupt_allowed(vcpu, true) : -EBUSY; | |
7879 | if (r < 0) | |
7880 | goto busy; | |
7881 | if (r) { | |
7882 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); | |
7883 | kvm_x86_ops.set_irq(vcpu); | |
7884 | WARN_ON(kvm_x86_ops.interrupt_allowed(vcpu, true) < 0); | |
7885 | } | |
7886 | if (kvm_cpu_has_injectable_intr(vcpu)) | |
7887 | kvm_x86_ops.enable_irq_window(vcpu); | |
95ba8273 | 7888 | } |
ee2cd4b7 | 7889 | |
c9d40913 PB |
7890 | if (is_guest_mode(vcpu) && |
7891 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
7892 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
7893 | *req_immediate_exit = true; | |
7894 | ||
7895 | WARN_ON(vcpu->arch.exception.pending); | |
7896 | return; | |
7897 | ||
7898 | busy: | |
7899 | *req_immediate_exit = true; | |
7900 | return; | |
95ba8273 GN |
7901 | } |
7902 | ||
7460fb4a AK |
7903 | static void process_nmi(struct kvm_vcpu *vcpu) |
7904 | { | |
7905 | unsigned limit = 2; | |
7906 | ||
7907 | /* | |
7908 | * x86 is limited to one NMI running, and one NMI pending after it. | |
7909 | * If an NMI is already in progress, limit further NMIs to just one. | |
7910 | * Otherwise, allow two (and we'll inject the first one immediately). | |
7911 | */ | |
afaf0b2f | 7912 | if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected) |
7460fb4a AK |
7913 | limit = 1; |
7914 | ||
7915 | vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); | |
7916 | vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); | |
7917 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7918 | } | |
7919 | ||
ee2cd4b7 | 7920 | static u32 enter_smm_get_segment_flags(struct kvm_segment *seg) |
660a5d51 PB |
7921 | { |
7922 | u32 flags = 0; | |
7923 | flags |= seg->g << 23; | |
7924 | flags |= seg->db << 22; | |
7925 | flags |= seg->l << 21; | |
7926 | flags |= seg->avl << 20; | |
7927 | flags |= seg->present << 15; | |
7928 | flags |= seg->dpl << 13; | |
7929 | flags |= seg->s << 12; | |
7930 | flags |= seg->type << 8; | |
7931 | return flags; | |
7932 | } | |
7933 | ||
ee2cd4b7 | 7934 | static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
7935 | { |
7936 | struct kvm_segment seg; | |
7937 | int offset; | |
7938 | ||
7939 | kvm_get_segment(vcpu, &seg, n); | |
7940 | put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector); | |
7941 | ||
7942 | if (n < 3) | |
7943 | offset = 0x7f84 + n * 12; | |
7944 | else | |
7945 | offset = 0x7f2c + (n - 3) * 12; | |
7946 | ||
7947 | put_smstate(u32, buf, offset + 8, seg.base); | |
7948 | put_smstate(u32, buf, offset + 4, seg.limit); | |
ee2cd4b7 | 7949 | put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
7950 | } |
7951 | ||
efbb288a | 7952 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 7953 | static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
7954 | { |
7955 | struct kvm_segment seg; | |
7956 | int offset; | |
7957 | u16 flags; | |
7958 | ||
7959 | kvm_get_segment(vcpu, &seg, n); | |
7960 | offset = 0x7e00 + n * 16; | |
7961 | ||
ee2cd4b7 | 7962 | flags = enter_smm_get_segment_flags(&seg) >> 8; |
660a5d51 PB |
7963 | put_smstate(u16, buf, offset, seg.selector); |
7964 | put_smstate(u16, buf, offset + 2, flags); | |
7965 | put_smstate(u32, buf, offset + 4, seg.limit); | |
7966 | put_smstate(u64, buf, offset + 8, seg.base); | |
7967 | } | |
efbb288a | 7968 | #endif |
660a5d51 | 7969 | |
ee2cd4b7 | 7970 | static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 PB |
7971 | { |
7972 | struct desc_ptr dt; | |
7973 | struct kvm_segment seg; | |
7974 | unsigned long val; | |
7975 | int i; | |
7976 | ||
7977 | put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu)); | |
7978 | put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu)); | |
7979 | put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu)); | |
7980 | put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu)); | |
7981 | ||
7982 | for (i = 0; i < 8; i++) | |
7983 | put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i)); | |
7984 | ||
7985 | kvm_get_dr(vcpu, 6, &val); | |
7986 | put_smstate(u32, buf, 0x7fcc, (u32)val); | |
7987 | kvm_get_dr(vcpu, 7, &val); | |
7988 | put_smstate(u32, buf, 0x7fc8, (u32)val); | |
7989 | ||
7990 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
7991 | put_smstate(u32, buf, 0x7fc4, seg.selector); | |
7992 | put_smstate(u32, buf, 0x7f64, seg.base); | |
7993 | put_smstate(u32, buf, 0x7f60, seg.limit); | |
ee2cd4b7 | 7994 | put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
7995 | |
7996 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
7997 | put_smstate(u32, buf, 0x7fc0, seg.selector); | |
7998 | put_smstate(u32, buf, 0x7f80, seg.base); | |
7999 | put_smstate(u32, buf, 0x7f7c, seg.limit); | |
ee2cd4b7 | 8000 | put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg)); |
660a5d51 | 8001 | |
afaf0b2f | 8002 | kvm_x86_ops.get_gdt(vcpu, &dt); |
660a5d51 PB |
8003 | put_smstate(u32, buf, 0x7f74, dt.address); |
8004 | put_smstate(u32, buf, 0x7f70, dt.size); | |
8005 | ||
afaf0b2f | 8006 | kvm_x86_ops.get_idt(vcpu, &dt); |
660a5d51 PB |
8007 | put_smstate(u32, buf, 0x7f58, dt.address); |
8008 | put_smstate(u32, buf, 0x7f54, dt.size); | |
8009 | ||
8010 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 8011 | enter_smm_save_seg_32(vcpu, buf, i); |
660a5d51 PB |
8012 | |
8013 | put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu)); | |
8014 | ||
8015 | /* revision id */ | |
8016 | put_smstate(u32, buf, 0x7efc, 0x00020000); | |
8017 | put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase); | |
8018 | } | |
8019 | ||
b68f3cc7 | 8020 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 8021 | static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 | 8022 | { |
660a5d51 PB |
8023 | struct desc_ptr dt; |
8024 | struct kvm_segment seg; | |
8025 | unsigned long val; | |
8026 | int i; | |
8027 | ||
8028 | for (i = 0; i < 16; i++) | |
8029 | put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i)); | |
8030 | ||
8031 | put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu)); | |
8032 | put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu)); | |
8033 | ||
8034 | kvm_get_dr(vcpu, 6, &val); | |
8035 | put_smstate(u64, buf, 0x7f68, val); | |
8036 | kvm_get_dr(vcpu, 7, &val); | |
8037 | put_smstate(u64, buf, 0x7f60, val); | |
8038 | ||
8039 | put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu)); | |
8040 | put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu)); | |
8041 | put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu)); | |
8042 | ||
8043 | put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase); | |
8044 | ||
8045 | /* revision id */ | |
8046 | put_smstate(u32, buf, 0x7efc, 0x00020064); | |
8047 | ||
8048 | put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer); | |
8049 | ||
8050 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
8051 | put_smstate(u16, buf, 0x7e90, seg.selector); | |
ee2cd4b7 | 8052 | put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
8053 | put_smstate(u32, buf, 0x7e94, seg.limit); |
8054 | put_smstate(u64, buf, 0x7e98, seg.base); | |
8055 | ||
afaf0b2f | 8056 | kvm_x86_ops.get_idt(vcpu, &dt); |
660a5d51 PB |
8057 | put_smstate(u32, buf, 0x7e84, dt.size); |
8058 | put_smstate(u64, buf, 0x7e88, dt.address); | |
8059 | ||
8060 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
8061 | put_smstate(u16, buf, 0x7e70, seg.selector); | |
ee2cd4b7 | 8062 | put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
8063 | put_smstate(u32, buf, 0x7e74, seg.limit); |
8064 | put_smstate(u64, buf, 0x7e78, seg.base); | |
8065 | ||
afaf0b2f | 8066 | kvm_x86_ops.get_gdt(vcpu, &dt); |
660a5d51 PB |
8067 | put_smstate(u32, buf, 0x7e64, dt.size); |
8068 | put_smstate(u64, buf, 0x7e68, dt.address); | |
8069 | ||
8070 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 8071 | enter_smm_save_seg_64(vcpu, buf, i); |
660a5d51 | 8072 | } |
b68f3cc7 | 8073 | #endif |
660a5d51 | 8074 | |
ee2cd4b7 | 8075 | static void enter_smm(struct kvm_vcpu *vcpu) |
64d60670 | 8076 | { |
660a5d51 | 8077 | struct kvm_segment cs, ds; |
18c3626e | 8078 | struct desc_ptr dt; |
660a5d51 PB |
8079 | char buf[512]; |
8080 | u32 cr0; | |
8081 | ||
660a5d51 | 8082 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true); |
660a5d51 | 8083 | memset(buf, 0, 512); |
b68f3cc7 | 8084 | #ifdef CONFIG_X86_64 |
d6321d49 | 8085 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
ee2cd4b7 | 8086 | enter_smm_save_state_64(vcpu, buf); |
660a5d51 | 8087 | else |
b68f3cc7 | 8088 | #endif |
ee2cd4b7 | 8089 | enter_smm_save_state_32(vcpu, buf); |
660a5d51 | 8090 | |
0234bf88 LP |
8091 | /* |
8092 | * Give pre_enter_smm() a chance to make ISA-specific changes to the | |
8093 | * vCPU state (e.g. leave guest mode) after we've saved the state into | |
8094 | * the SMM state-save area. | |
8095 | */ | |
afaf0b2f | 8096 | kvm_x86_ops.pre_enter_smm(vcpu, buf); |
0234bf88 LP |
8097 | |
8098 | vcpu->arch.hflags |= HF_SMM_MASK; | |
54bf36aa | 8099 | kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); |
660a5d51 | 8100 | |
afaf0b2f | 8101 | if (kvm_x86_ops.get_nmi_mask(vcpu)) |
660a5d51 PB |
8102 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
8103 | else | |
afaf0b2f | 8104 | kvm_x86_ops.set_nmi_mask(vcpu, true); |
660a5d51 PB |
8105 | |
8106 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); | |
8107 | kvm_rip_write(vcpu, 0x8000); | |
8108 | ||
8109 | cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG); | |
afaf0b2f | 8110 | kvm_x86_ops.set_cr0(vcpu, cr0); |
660a5d51 PB |
8111 | vcpu->arch.cr0 = cr0; |
8112 | ||
afaf0b2f | 8113 | kvm_x86_ops.set_cr4(vcpu, 0); |
660a5d51 | 8114 | |
18c3626e PB |
8115 | /* Undocumented: IDT limit is set to zero on entry to SMM. */ |
8116 | dt.address = dt.size = 0; | |
afaf0b2f | 8117 | kvm_x86_ops.set_idt(vcpu, &dt); |
18c3626e | 8118 | |
660a5d51 PB |
8119 | __kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
8120 | ||
8121 | cs.selector = (vcpu->arch.smbase >> 4) & 0xffff; | |
8122 | cs.base = vcpu->arch.smbase; | |
8123 | ||
8124 | ds.selector = 0; | |
8125 | ds.base = 0; | |
8126 | ||
8127 | cs.limit = ds.limit = 0xffffffff; | |
8128 | cs.type = ds.type = 0x3; | |
8129 | cs.dpl = ds.dpl = 0; | |
8130 | cs.db = ds.db = 0; | |
8131 | cs.s = ds.s = 1; | |
8132 | cs.l = ds.l = 0; | |
8133 | cs.g = ds.g = 1; | |
8134 | cs.avl = ds.avl = 0; | |
8135 | cs.present = ds.present = 1; | |
8136 | cs.unusable = ds.unusable = 0; | |
8137 | cs.padding = ds.padding = 0; | |
8138 | ||
8139 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
8140 | kvm_set_segment(vcpu, &ds, VCPU_SREG_DS); | |
8141 | kvm_set_segment(vcpu, &ds, VCPU_SREG_ES); | |
8142 | kvm_set_segment(vcpu, &ds, VCPU_SREG_FS); | |
8143 | kvm_set_segment(vcpu, &ds, VCPU_SREG_GS); | |
8144 | kvm_set_segment(vcpu, &ds, VCPU_SREG_SS); | |
8145 | ||
b68f3cc7 | 8146 | #ifdef CONFIG_X86_64 |
d6321d49 | 8147 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
afaf0b2f | 8148 | kvm_x86_ops.set_efer(vcpu, 0); |
b68f3cc7 | 8149 | #endif |
660a5d51 PB |
8150 | |
8151 | kvm_update_cpuid(vcpu); | |
8152 | kvm_mmu_reset_context(vcpu); | |
64d60670 PB |
8153 | } |
8154 | ||
ee2cd4b7 | 8155 | static void process_smi(struct kvm_vcpu *vcpu) |
c43203ca PB |
8156 | { |
8157 | vcpu->arch.smi_pending = true; | |
8158 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
8159 | } | |
8160 | ||
7ee30bc1 NNL |
8161 | void kvm_make_scan_ioapic_request_mask(struct kvm *kvm, |
8162 | unsigned long *vcpu_bitmap) | |
8163 | { | |
8164 | cpumask_var_t cpus; | |
7ee30bc1 NNL |
8165 | |
8166 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); | |
8167 | ||
db5a95ec | 8168 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC, |
54163a34 | 8169 | NULL, vcpu_bitmap, cpus); |
7ee30bc1 NNL |
8170 | |
8171 | free_cpumask_var(cpus); | |
8172 | } | |
8173 | ||
2860c4b1 PB |
8174 | void kvm_make_scan_ioapic_request(struct kvm *kvm) |
8175 | { | |
8176 | kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC); | |
8177 | } | |
8178 | ||
8df14af4 SS |
8179 | void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu) |
8180 | { | |
8181 | if (!lapic_in_kernel(vcpu)) | |
8182 | return; | |
8183 | ||
8184 | vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm); | |
8185 | kvm_apic_update_apicv(vcpu); | |
afaf0b2f | 8186 | kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu); |
8df14af4 SS |
8187 | } |
8188 | EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv); | |
8189 | ||
8190 | /* | |
8191 | * NOTE: Do not hold any lock prior to calling this. | |
8192 | * | |
8193 | * In particular, kvm_request_apicv_update() expects kvm->srcu not to be | |
8194 | * locked, because it calls __x86_set_memory_region() which does | |
8195 | * synchronize_srcu(&kvm->srcu). | |
8196 | */ | |
8197 | void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit) | |
8198 | { | |
7d611233 | 8199 | struct kvm_vcpu *except; |
8e205a6b PB |
8200 | unsigned long old, new, expected; |
8201 | ||
afaf0b2f SC |
8202 | if (!kvm_x86_ops.check_apicv_inhibit_reasons || |
8203 | !kvm_x86_ops.check_apicv_inhibit_reasons(bit)) | |
ef8efd7a SS |
8204 | return; |
8205 | ||
8e205a6b PB |
8206 | old = READ_ONCE(kvm->arch.apicv_inhibit_reasons); |
8207 | do { | |
8208 | expected = new = old; | |
8209 | if (activate) | |
8210 | __clear_bit(bit, &new); | |
8211 | else | |
8212 | __set_bit(bit, &new); | |
8213 | if (new == old) | |
8214 | break; | |
8215 | old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new); | |
8216 | } while (old != expected); | |
8217 | ||
8218 | if (!!old == !!new) | |
8219 | return; | |
8df14af4 | 8220 | |
24bbf74c | 8221 | trace_kvm_apicv_update_request(activate, bit); |
afaf0b2f SC |
8222 | if (kvm_x86_ops.pre_update_apicv_exec_ctrl) |
8223 | kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate); | |
7d611233 SS |
8224 | |
8225 | /* | |
8226 | * Sending request to update APICV for all other vcpus, | |
8227 | * while update the calling vcpu immediately instead of | |
8228 | * waiting for another #VMEXIT to handle the request. | |
8229 | */ | |
8230 | except = kvm_get_running_vcpu(); | |
8231 | kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE, | |
8232 | except); | |
8233 | if (except) | |
8234 | kvm_vcpu_update_apicv(except); | |
8df14af4 SS |
8235 | } |
8236 | EXPORT_SYMBOL_GPL(kvm_request_apicv_update); | |
8237 | ||
3d81bc7e | 8238 | static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) |
c7c9c56c | 8239 | { |
dcbd3e49 | 8240 | if (!kvm_apic_present(vcpu)) |
3d81bc7e | 8241 | return; |
c7c9c56c | 8242 | |
6308630b | 8243 | bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); |
c7c9c56c | 8244 | |
b053b2ae | 8245 | if (irqchip_split(vcpu->kvm)) |
6308630b | 8246 | kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); |
db2bdcbb | 8247 | else { |
fa59cc00 | 8248 | if (vcpu->arch.apicv_active) |
afaf0b2f | 8249 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
e97f852f WL |
8250 | if (ioapic_in_kernel(vcpu->kvm)) |
8251 | kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); | |
db2bdcbb | 8252 | } |
e40ff1d6 LA |
8253 | |
8254 | if (is_guest_mode(vcpu)) | |
8255 | vcpu->arch.load_eoi_exitmap_pending = true; | |
8256 | else | |
8257 | kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu); | |
8258 | } | |
8259 | ||
8260 | static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) | |
8261 | { | |
8262 | u64 eoi_exit_bitmap[4]; | |
8263 | ||
8264 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) | |
8265 | return; | |
8266 | ||
5c919412 AS |
8267 | bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors, |
8268 | vcpu_to_synic(vcpu)->vec_bitmap, 256); | |
afaf0b2f | 8269 | kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap); |
c7c9c56c YZ |
8270 | } |
8271 | ||
e649b3f0 ET |
8272 | void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
8273 | unsigned long start, unsigned long end) | |
b1394e74 RK |
8274 | { |
8275 | unsigned long apic_address; | |
8276 | ||
8277 | /* | |
8278 | * The physical address of apic access page is stored in the VMCS. | |
8279 | * Update it when it becomes invalid. | |
8280 | */ | |
8281 | apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); | |
8282 | if (start <= apic_address && apic_address < end) | |
8283 | kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); | |
8284 | } | |
8285 | ||
4256f43f TC |
8286 | void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) |
8287 | { | |
35754c98 | 8288 | if (!lapic_in_kernel(vcpu)) |
f439ed27 PB |
8289 | return; |
8290 | ||
afaf0b2f | 8291 | if (!kvm_x86_ops.set_apic_access_page_addr) |
4256f43f TC |
8292 | return; |
8293 | ||
a4148b7c | 8294 | kvm_x86_ops.set_apic_access_page_addr(vcpu); |
4256f43f | 8295 | } |
4256f43f | 8296 | |
d264ee0c SC |
8297 | void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) |
8298 | { | |
8299 | smp_send_reschedule(vcpu->cpu); | |
8300 | } | |
8301 | EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); | |
8302 | ||
9357d939 | 8303 | /* |
362c698f | 8304 | * Returns 1 to let vcpu_run() continue the guest execution loop without |
9357d939 TY |
8305 | * exiting to the userspace. Otherwise, the value will be returned to the |
8306 | * userspace. | |
8307 | */ | |
851ba692 | 8308 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
b6c7a5dc HB |
8309 | { |
8310 | int r; | |
62a193ed MG |
8311 | bool req_int_win = |
8312 | dm_request_for_irq_injection(vcpu) && | |
8313 | kvm_cpu_accept_dm_intr(vcpu); | |
404d5d7b | 8314 | fastpath_t exit_fastpath; |
62a193ed | 8315 | |
730dca42 | 8316 | bool req_immediate_exit = false; |
b6c7a5dc | 8317 | |
2fa6e1e1 | 8318 | if (kvm_request_pending(vcpu)) { |
671ddc70 | 8319 | if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) { |
33b22172 | 8320 | if (unlikely(!kvm_x86_ops.nested_ops->get_vmcs12_pages(vcpu))) { |
671ddc70 JM |
8321 | r = 0; |
8322 | goto out; | |
8323 | } | |
8324 | } | |
a8eeb04a | 8325 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) |
2e53d63a | 8326 | kvm_mmu_unload(vcpu); |
a8eeb04a | 8327 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
2f599714 | 8328 | __kvm_migrate_timers(vcpu); |
d828199e MT |
8329 | if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) |
8330 | kvm_gen_update_masterclock(vcpu->kvm); | |
0061d53d MT |
8331 | if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) |
8332 | kvm_gen_kvmclock_update(vcpu); | |
34c238a1 ZA |
8333 | if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { |
8334 | r = kvm_guest_time_update(vcpu); | |
8cfdc000 ZA |
8335 | if (unlikely(r)) |
8336 | goto out; | |
8337 | } | |
a8eeb04a | 8338 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
4731d4c7 | 8339 | kvm_mmu_sync_roots(vcpu); |
727a7e27 PB |
8340 | if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) |
8341 | kvm_mmu_load_pgd(vcpu); | |
eeeb4f67 | 8342 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
7780938c | 8343 | kvm_vcpu_flush_tlb_all(vcpu); |
eeeb4f67 SC |
8344 | |
8345 | /* Flushing all ASIDs flushes the current ASID... */ | |
8346 | kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); | |
8347 | } | |
8348 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) | |
8349 | kvm_vcpu_flush_tlb_current(vcpu); | |
0baedd79 VK |
8350 | if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) |
8351 | kvm_vcpu_flush_tlb_guest(vcpu); | |
eeeb4f67 | 8352 | |
a8eeb04a | 8353 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
851ba692 | 8354 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
b93463aa AK |
8355 | r = 0; |
8356 | goto out; | |
8357 | } | |
a8eeb04a | 8358 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
851ba692 | 8359 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
bbeac283 | 8360 | vcpu->mmio_needed = 0; |
71c4dfaf JR |
8361 | r = 0; |
8362 | goto out; | |
8363 | } | |
af585b92 GN |
8364 | if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) { |
8365 | /* Page is swapped out. Do synthetic halt */ | |
8366 | vcpu->arch.apf.halted = true; | |
8367 | r = 1; | |
8368 | goto out; | |
8369 | } | |
c9aaa895 GC |
8370 | if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) |
8371 | record_steal_time(vcpu); | |
64d60670 PB |
8372 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
8373 | process_smi(vcpu); | |
7460fb4a AK |
8374 | if (kvm_check_request(KVM_REQ_NMI, vcpu)) |
8375 | process_nmi(vcpu); | |
f5132b01 | 8376 | if (kvm_check_request(KVM_REQ_PMU, vcpu)) |
c6702c9d | 8377 | kvm_pmu_handle_event(vcpu); |
f5132b01 | 8378 | if (kvm_check_request(KVM_REQ_PMI, vcpu)) |
c6702c9d | 8379 | kvm_pmu_deliver_pmi(vcpu); |
7543a635 SR |
8380 | if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { |
8381 | BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); | |
8382 | if (test_bit(vcpu->arch.pending_ioapic_eoi, | |
6308630b | 8383 | vcpu->arch.ioapic_handled_vectors)) { |
7543a635 SR |
8384 | vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; |
8385 | vcpu->run->eoi.vector = | |
8386 | vcpu->arch.pending_ioapic_eoi; | |
8387 | r = 0; | |
8388 | goto out; | |
8389 | } | |
8390 | } | |
3d81bc7e YZ |
8391 | if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) |
8392 | vcpu_scan_ioapic(vcpu); | |
e40ff1d6 LA |
8393 | if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu)) |
8394 | vcpu_load_eoi_exitmap(vcpu); | |
4256f43f TC |
8395 | if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) |
8396 | kvm_vcpu_reload_apic_access_page(vcpu); | |
2ce79189 AS |
8397 | if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) { |
8398 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
8399 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; | |
8400 | r = 0; | |
8401 | goto out; | |
8402 | } | |
e516cebb AS |
8403 | if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) { |
8404 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
8405 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; | |
8406 | r = 0; | |
8407 | goto out; | |
8408 | } | |
db397571 AS |
8409 | if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) { |
8410 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; | |
8411 | vcpu->run->hyperv = vcpu->arch.hyperv.exit; | |
8412 | r = 0; | |
8413 | goto out; | |
8414 | } | |
f3b138c5 AS |
8415 | |
8416 | /* | |
8417 | * KVM_REQ_HV_STIMER has to be processed after | |
8418 | * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers | |
8419 | * depend on the guest clock being up-to-date | |
8420 | */ | |
1f4b34f8 AS |
8421 | if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) |
8422 | kvm_hv_process_stimers(vcpu); | |
8df14af4 SS |
8423 | if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu)) |
8424 | kvm_vcpu_update_apicv(vcpu); | |
557a961a VK |
8425 | if (kvm_check_request(KVM_REQ_APF_READY, vcpu)) |
8426 | kvm_check_async_pf_completion(vcpu); | |
2f52d58c | 8427 | } |
b93463aa | 8428 | |
b463a6f7 | 8429 | if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { |
0f1e261e | 8430 | ++vcpu->stat.req_event; |
66450a21 JK |
8431 | kvm_apic_accept_events(vcpu); |
8432 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { | |
8433 | r = 1; | |
8434 | goto out; | |
8435 | } | |
8436 | ||
c9d40913 PB |
8437 | inject_pending_event(vcpu, &req_immediate_exit); |
8438 | if (req_int_win) | |
8439 | kvm_x86_ops.enable_irq_window(vcpu); | |
b463a6f7 AK |
8440 | |
8441 | if (kvm_lapic_enabled(vcpu)) { | |
8442 | update_cr8_intercept(vcpu); | |
8443 | kvm_lapic_sync_to_vapic(vcpu); | |
8444 | } | |
8445 | } | |
8446 | ||
d8368af8 AK |
8447 | r = kvm_mmu_reload(vcpu); |
8448 | if (unlikely(r)) { | |
d905c069 | 8449 | goto cancel_injection; |
d8368af8 AK |
8450 | } |
8451 | ||
b6c7a5dc HB |
8452 | preempt_disable(); |
8453 | ||
afaf0b2f | 8454 | kvm_x86_ops.prepare_guest_switch(vcpu); |
b95234c8 PB |
8455 | |
8456 | /* | |
8457 | * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt | |
8458 | * IPI are then delayed after guest entry, which ensures that they | |
8459 | * result in virtual interrupt delivery. | |
8460 | */ | |
8461 | local_irq_disable(); | |
6b7e2d09 XG |
8462 | vcpu->mode = IN_GUEST_MODE; |
8463 | ||
01b71917 MT |
8464 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
8465 | ||
0f127d12 | 8466 | /* |
b95234c8 | 8467 | * 1) We should set ->mode before checking ->requests. Please see |
cde9af6e | 8468 | * the comment in kvm_vcpu_exiting_guest_mode(). |
b95234c8 | 8469 | * |
81b01667 | 8470 | * 2) For APICv, we should set ->mode before checking PID.ON. This |
b95234c8 PB |
8471 | * pairs with the memory barrier implicit in pi_test_and_set_on |
8472 | * (see vmx_deliver_posted_interrupt). | |
8473 | * | |
8474 | * 3) This also orders the write to mode from any reads to the page | |
8475 | * tables done while the VCPU is running. Please see the comment | |
8476 | * in kvm_flush_remote_tlbs. | |
6b7e2d09 | 8477 | */ |
01b71917 | 8478 | smp_mb__after_srcu_read_unlock(); |
b6c7a5dc | 8479 | |
b95234c8 PB |
8480 | /* |
8481 | * This handles the case where a posted interrupt was | |
8482 | * notified with kvm_vcpu_kick. | |
8483 | */ | |
fa59cc00 | 8484 | if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active) |
afaf0b2f | 8485 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
32f88400 | 8486 | |
5a9f5443 | 8487 | if (kvm_vcpu_exit_request(vcpu)) { |
6b7e2d09 | 8488 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 8489 | smp_wmb(); |
6c142801 AK |
8490 | local_irq_enable(); |
8491 | preempt_enable(); | |
01b71917 | 8492 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
6c142801 | 8493 | r = 1; |
d905c069 | 8494 | goto cancel_injection; |
6c142801 AK |
8495 | } |
8496 | ||
c43203ca PB |
8497 | if (req_immediate_exit) { |
8498 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
afaf0b2f | 8499 | kvm_x86_ops.request_immediate_exit(vcpu); |
c43203ca | 8500 | } |
d6185f20 | 8501 | |
8b89fe1f | 8502 | trace_kvm_entry(vcpu->vcpu_id); |
6edaa530 | 8503 | guest_enter_irqoff(); |
b6c7a5dc | 8504 | |
2620fe26 SC |
8505 | fpregs_assert_state_consistent(); |
8506 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
8507 | switch_fpu_return(); | |
5f409e20 | 8508 | |
42dbaa5a | 8509 | if (unlikely(vcpu->arch.switch_db_regs)) { |
42dbaa5a JK |
8510 | set_debugreg(0, 7); |
8511 | set_debugreg(vcpu->arch.eff_db[0], 0); | |
8512 | set_debugreg(vcpu->arch.eff_db[1], 1); | |
8513 | set_debugreg(vcpu->arch.eff_db[2], 2); | |
8514 | set_debugreg(vcpu->arch.eff_db[3], 3); | |
c77fb5fe | 8515 | set_debugreg(vcpu->arch.dr6, 6); |
ae561ede | 8516 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
42dbaa5a | 8517 | } |
b6c7a5dc | 8518 | |
a9ab13ff | 8519 | exit_fastpath = kvm_x86_ops.run(vcpu); |
b6c7a5dc | 8520 | |
c77fb5fe PB |
8521 | /* |
8522 | * Do this here before restoring debug registers on the host. And | |
8523 | * since we do this before handling the vmexit, a DR access vmexit | |
8524 | * can (a) read the correct value of the debug registers, (b) set | |
8525 | * KVM_DEBUGREG_WONT_EXIT again. | |
8526 | */ | |
8527 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { | |
c77fb5fe | 8528 | WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); |
afaf0b2f | 8529 | kvm_x86_ops.sync_dirty_debug_regs(vcpu); |
70e4da7a | 8530 | kvm_update_dr0123(vcpu); |
70e4da7a PB |
8531 | kvm_update_dr7(vcpu); |
8532 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; | |
c77fb5fe PB |
8533 | } |
8534 | ||
24f1e32c FW |
8535 | /* |
8536 | * If the guest has used debug registers, at least dr7 | |
8537 | * will be disabled while returning to the host. | |
8538 | * If we don't have active breakpoints in the host, we don't | |
8539 | * care about the messed up debug address registers. But if | |
8540 | * we have some of them active, restore the old state. | |
8541 | */ | |
59d8eb53 | 8542 | if (hw_breakpoint_active()) |
24f1e32c | 8543 | hw_breakpoint_restore(); |
42dbaa5a | 8544 | |
4ba76538 | 8545 | vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
1d5f066e | 8546 | |
6b7e2d09 | 8547 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 8548 | smp_wmb(); |
a547c6db | 8549 | |
a9ab13ff | 8550 | kvm_x86_ops.handle_exit_irqoff(vcpu); |
b6c7a5dc | 8551 | |
d7a08882 SC |
8552 | /* |
8553 | * Consume any pending interrupts, including the possible source of | |
8554 | * VM-Exit on SVM and any ticks that occur between VM-Exit and now. | |
8555 | * An instruction is required after local_irq_enable() to fully unblock | |
8556 | * interrupts on processors that implement an interrupt shadow, the | |
8557 | * stat.exits increment will do nicely. | |
8558 | */ | |
8559 | kvm_before_interrupt(vcpu); | |
8560 | local_irq_enable(); | |
b6c7a5dc | 8561 | ++vcpu->stat.exits; |
d7a08882 SC |
8562 | local_irq_disable(); |
8563 | kvm_after_interrupt(vcpu); | |
b6c7a5dc | 8564 | |
f2485b3e | 8565 | guest_exit_irqoff(); |
ec0671d5 WL |
8566 | if (lapic_in_kernel(vcpu)) { |
8567 | s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta; | |
8568 | if (delta != S64_MIN) { | |
8569 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta); | |
8570 | vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN; | |
8571 | } | |
8572 | } | |
b6c7a5dc | 8573 | |
f2485b3e | 8574 | local_irq_enable(); |
b6c7a5dc HB |
8575 | preempt_enable(); |
8576 | ||
f656ce01 | 8577 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
3200f405 | 8578 | |
b6c7a5dc HB |
8579 | /* |
8580 | * Profile KVM exit RIPs: | |
8581 | */ | |
8582 | if (unlikely(prof_on == KVM_PROFILING)) { | |
5fdbf976 MT |
8583 | unsigned long rip = kvm_rip_read(vcpu); |
8584 | profile_hit(KVM_PROFILING, (void *)rip); | |
b6c7a5dc HB |
8585 | } |
8586 | ||
cc578287 ZA |
8587 | if (unlikely(vcpu->arch.tsc_always_catchup)) |
8588 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
298101da | 8589 | |
5cfb1d5a MT |
8590 | if (vcpu->arch.apic_attention) |
8591 | kvm_lapic_sync_from_vapic(vcpu); | |
b93463aa | 8592 | |
afaf0b2f | 8593 | r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath); |
d905c069 MT |
8594 | return r; |
8595 | ||
8596 | cancel_injection: | |
8081ad06 SC |
8597 | if (req_immediate_exit) |
8598 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
afaf0b2f | 8599 | kvm_x86_ops.cancel_injection(vcpu); |
ae7a2a3f MT |
8600 | if (unlikely(vcpu->arch.apic_attention)) |
8601 | kvm_lapic_sync_from_vapic(vcpu); | |
d7690175 MT |
8602 | out: |
8603 | return r; | |
8604 | } | |
b6c7a5dc | 8605 | |
362c698f PB |
8606 | static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) |
8607 | { | |
bf9f6ac8 | 8608 | if (!kvm_arch_vcpu_runnable(vcpu) && |
afaf0b2f | 8609 | (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) { |
9c8fd1ba PB |
8610 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
8611 | kvm_vcpu_block(vcpu); | |
8612 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); | |
bf9f6ac8 | 8613 | |
afaf0b2f SC |
8614 | if (kvm_x86_ops.post_block) |
8615 | kvm_x86_ops.post_block(vcpu); | |
bf9f6ac8 | 8616 | |
9c8fd1ba PB |
8617 | if (!kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
8618 | return 1; | |
8619 | } | |
362c698f PB |
8620 | |
8621 | kvm_apic_accept_events(vcpu); | |
8622 | switch(vcpu->arch.mp_state) { | |
8623 | case KVM_MP_STATE_HALTED: | |
8624 | vcpu->arch.pv.pv_unhalted = false; | |
8625 | vcpu->arch.mp_state = | |
8626 | KVM_MP_STATE_RUNNABLE; | |
b2869f28 | 8627 | /* fall through */ |
362c698f PB |
8628 | case KVM_MP_STATE_RUNNABLE: |
8629 | vcpu->arch.apf.halted = false; | |
8630 | break; | |
8631 | case KVM_MP_STATE_INIT_RECEIVED: | |
8632 | break; | |
8633 | default: | |
8634 | return -EINTR; | |
362c698f PB |
8635 | } |
8636 | return 1; | |
8637 | } | |
09cec754 | 8638 | |
5d9bc648 PB |
8639 | static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) |
8640 | { | |
56083bdf | 8641 | if (is_guest_mode(vcpu)) |
33b22172 | 8642 | kvm_x86_ops.nested_ops->check_events(vcpu); |
0ad3bed6 | 8643 | |
5d9bc648 PB |
8644 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && |
8645 | !vcpu->arch.apf.halted); | |
8646 | } | |
8647 | ||
362c698f | 8648 | static int vcpu_run(struct kvm_vcpu *vcpu) |
d7690175 MT |
8649 | { |
8650 | int r; | |
f656ce01 | 8651 | struct kvm *kvm = vcpu->kvm; |
d7690175 | 8652 | |
f656ce01 | 8653 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
c595ceee | 8654 | vcpu->arch.l1tf_flush_l1d = true; |
d7690175 | 8655 | |
362c698f | 8656 | for (;;) { |
58f800d5 | 8657 | if (kvm_vcpu_running(vcpu)) { |
851ba692 | 8658 | r = vcpu_enter_guest(vcpu); |
bf9f6ac8 | 8659 | } else { |
362c698f | 8660 | r = vcpu_block(kvm, vcpu); |
bf9f6ac8 FW |
8661 | } |
8662 | ||
09cec754 GN |
8663 | if (r <= 0) |
8664 | break; | |
8665 | ||
72875d8a | 8666 | kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu); |
09cec754 GN |
8667 | if (kvm_cpu_has_pending_timer(vcpu)) |
8668 | kvm_inject_pending_timer_irqs(vcpu); | |
8669 | ||
782d422b MG |
8670 | if (dm_request_for_irq_injection(vcpu) && |
8671 | kvm_vcpu_ready_for_interrupt_injection(vcpu)) { | |
4ca7dd8c PB |
8672 | r = 0; |
8673 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; | |
09cec754 | 8674 | ++vcpu->stat.request_irq_exits; |
362c698f | 8675 | break; |
09cec754 | 8676 | } |
af585b92 | 8677 | |
09cec754 GN |
8678 | if (signal_pending(current)) { |
8679 | r = -EINTR; | |
851ba692 | 8680 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
09cec754 | 8681 | ++vcpu->stat.signal_exits; |
362c698f | 8682 | break; |
09cec754 GN |
8683 | } |
8684 | if (need_resched()) { | |
f656ce01 | 8685 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
c08ac06a | 8686 | cond_resched(); |
f656ce01 | 8687 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
d7690175 | 8688 | } |
b6c7a5dc HB |
8689 | } |
8690 | ||
f656ce01 | 8691 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
b6c7a5dc HB |
8692 | |
8693 | return r; | |
8694 | } | |
8695 | ||
716d51ab GN |
8696 | static inline int complete_emulated_io(struct kvm_vcpu *vcpu) |
8697 | { | |
8698 | int r; | |
60fc3d02 | 8699 | |
716d51ab | 8700 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
0ce97a2b | 8701 | r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); |
716d51ab | 8702 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
60fc3d02 | 8703 | return r; |
716d51ab GN |
8704 | } |
8705 | ||
8706 | static int complete_emulated_pio(struct kvm_vcpu *vcpu) | |
8707 | { | |
8708 | BUG_ON(!vcpu->arch.pio.count); | |
8709 | ||
8710 | return complete_emulated_io(vcpu); | |
8711 | } | |
8712 | ||
f78146b0 AK |
8713 | /* |
8714 | * Implements the following, as a state machine: | |
8715 | * | |
8716 | * read: | |
8717 | * for each fragment | |
87da7e66 XG |
8718 | * for each mmio piece in the fragment |
8719 | * write gpa, len | |
8720 | * exit | |
8721 | * copy data | |
f78146b0 AK |
8722 | * execute insn |
8723 | * | |
8724 | * write: | |
8725 | * for each fragment | |
87da7e66 XG |
8726 | * for each mmio piece in the fragment |
8727 | * write gpa, len | |
8728 | * copy data | |
8729 | * exit | |
f78146b0 | 8730 | */ |
716d51ab | 8731 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu) |
5287f194 AK |
8732 | { |
8733 | struct kvm_run *run = vcpu->run; | |
f78146b0 | 8734 | struct kvm_mmio_fragment *frag; |
87da7e66 | 8735 | unsigned len; |
5287f194 | 8736 | |
716d51ab | 8737 | BUG_ON(!vcpu->mmio_needed); |
5287f194 | 8738 | |
716d51ab | 8739 | /* Complete previous fragment */ |
87da7e66 XG |
8740 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; |
8741 | len = min(8u, frag->len); | |
716d51ab | 8742 | if (!vcpu->mmio_is_write) |
87da7e66 XG |
8743 | memcpy(frag->data, run->mmio.data, len); |
8744 | ||
8745 | if (frag->len <= 8) { | |
8746 | /* Switch to the next fragment. */ | |
8747 | frag++; | |
8748 | vcpu->mmio_cur_fragment++; | |
8749 | } else { | |
8750 | /* Go forward to the next mmio piece. */ | |
8751 | frag->data += len; | |
8752 | frag->gpa += len; | |
8753 | frag->len -= len; | |
8754 | } | |
8755 | ||
a08d3b3b | 8756 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { |
716d51ab | 8757 | vcpu->mmio_needed = 0; |
0912c977 PB |
8758 | |
8759 | /* FIXME: return into emulator if single-stepping. */ | |
cef4dea0 | 8760 | if (vcpu->mmio_is_write) |
716d51ab GN |
8761 | return 1; |
8762 | vcpu->mmio_read_completed = 1; | |
8763 | return complete_emulated_io(vcpu); | |
8764 | } | |
87da7e66 | 8765 | |
716d51ab GN |
8766 | run->exit_reason = KVM_EXIT_MMIO; |
8767 | run->mmio.phys_addr = frag->gpa; | |
8768 | if (vcpu->mmio_is_write) | |
87da7e66 XG |
8769 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); |
8770 | run->mmio.len = min(8u, frag->len); | |
716d51ab GN |
8771 | run->mmio.is_write = vcpu->mmio_is_write; |
8772 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; | |
8773 | return 0; | |
5287f194 AK |
8774 | } |
8775 | ||
c9aef3b8 SC |
8776 | static void kvm_save_current_fpu(struct fpu *fpu) |
8777 | { | |
8778 | /* | |
8779 | * If the target FPU state is not resident in the CPU registers, just | |
8780 | * memcpy() from current, else save CPU state directly to the target. | |
8781 | */ | |
8782 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
8783 | memcpy(&fpu->state, ¤t->thread.fpu.state, | |
8784 | fpu_kernel_xstate_size); | |
8785 | else | |
8786 | copy_fpregs_to_fpstate(fpu); | |
8787 | } | |
8788 | ||
822f312d SAS |
8789 | /* Swap (qemu) user FPU context for the guest FPU context. */ |
8790 | static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
8791 | { | |
5f409e20 RR |
8792 | fpregs_lock(); |
8793 | ||
c9aef3b8 SC |
8794 | kvm_save_current_fpu(vcpu->arch.user_fpu); |
8795 | ||
afaf0b2f | 8796 | /* PKRU is separately restored in kvm_x86_ops.run. */ |
b666a4b6 | 8797 | __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state, |
822f312d | 8798 | ~XFEATURE_MASK_PKRU); |
5f409e20 RR |
8799 | |
8800 | fpregs_mark_activate(); | |
8801 | fpregs_unlock(); | |
8802 | ||
822f312d SAS |
8803 | trace_kvm_fpu(1); |
8804 | } | |
8805 | ||
8806 | /* When vcpu_run ends, restore user space FPU context. */ | |
8807 | static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
8808 | { | |
5f409e20 RR |
8809 | fpregs_lock(); |
8810 | ||
c9aef3b8 SC |
8811 | kvm_save_current_fpu(vcpu->arch.guest_fpu); |
8812 | ||
d9a710e5 | 8813 | copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state); |
5f409e20 RR |
8814 | |
8815 | fpregs_mark_activate(); | |
8816 | fpregs_unlock(); | |
8817 | ||
822f312d SAS |
8818 | ++vcpu->stat.fpu_reload; |
8819 | trace_kvm_fpu(0); | |
8820 | } | |
8821 | ||
1b94f6f8 | 8822 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
b6c7a5dc | 8823 | { |
1b94f6f8 | 8824 | struct kvm_run *kvm_run = vcpu->run; |
b6c7a5dc | 8825 | int r; |
b6c7a5dc | 8826 | |
accb757d | 8827 | vcpu_load(vcpu); |
20b7035c | 8828 | kvm_sigset_activate(vcpu); |
5663d8f9 PX |
8829 | kvm_load_guest_fpu(vcpu); |
8830 | ||
a4535290 | 8831 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
2f173d26 JS |
8832 | if (kvm_run->immediate_exit) { |
8833 | r = -EINTR; | |
8834 | goto out; | |
8835 | } | |
b6c7a5dc | 8836 | kvm_vcpu_block(vcpu); |
66450a21 | 8837 | kvm_apic_accept_events(vcpu); |
72875d8a | 8838 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
ac9f6dc0 | 8839 | r = -EAGAIN; |
a0595000 JS |
8840 | if (signal_pending(current)) { |
8841 | r = -EINTR; | |
1b94f6f8 | 8842 | kvm_run->exit_reason = KVM_EXIT_INTR; |
a0595000 JS |
8843 | ++vcpu->stat.signal_exits; |
8844 | } | |
ac9f6dc0 | 8845 | goto out; |
b6c7a5dc HB |
8846 | } |
8847 | ||
1b94f6f8 | 8848 | if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) { |
01643c51 KH |
8849 | r = -EINVAL; |
8850 | goto out; | |
8851 | } | |
8852 | ||
1b94f6f8 | 8853 | if (kvm_run->kvm_dirty_regs) { |
01643c51 KH |
8854 | r = sync_regs(vcpu); |
8855 | if (r != 0) | |
8856 | goto out; | |
8857 | } | |
8858 | ||
b6c7a5dc | 8859 | /* re-sync apic's tpr */ |
35754c98 | 8860 | if (!lapic_in_kernel(vcpu)) { |
eea1cff9 AP |
8861 | if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { |
8862 | r = -EINVAL; | |
8863 | goto out; | |
8864 | } | |
8865 | } | |
b6c7a5dc | 8866 | |
716d51ab GN |
8867 | if (unlikely(vcpu->arch.complete_userspace_io)) { |
8868 | int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; | |
8869 | vcpu->arch.complete_userspace_io = NULL; | |
8870 | r = cui(vcpu); | |
8871 | if (r <= 0) | |
5663d8f9 | 8872 | goto out; |
716d51ab GN |
8873 | } else |
8874 | WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); | |
5287f194 | 8875 | |
460df4c1 PB |
8876 | if (kvm_run->immediate_exit) |
8877 | r = -EINTR; | |
8878 | else | |
8879 | r = vcpu_run(vcpu); | |
b6c7a5dc HB |
8880 | |
8881 | out: | |
5663d8f9 | 8882 | kvm_put_guest_fpu(vcpu); |
1b94f6f8 | 8883 | if (kvm_run->kvm_valid_regs) |
01643c51 | 8884 | store_regs(vcpu); |
f1d86e46 | 8885 | post_kvm_run_save(vcpu); |
20b7035c | 8886 | kvm_sigset_deactivate(vcpu); |
b6c7a5dc | 8887 | |
accb757d | 8888 | vcpu_put(vcpu); |
b6c7a5dc HB |
8889 | return r; |
8890 | } | |
8891 | ||
01643c51 | 8892 | static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 8893 | { |
7ae441ea GN |
8894 | if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { |
8895 | /* | |
8896 | * We are here if userspace calls get_regs() in the middle of | |
8897 | * instruction emulation. Registers state needs to be copied | |
4a969980 | 8898 | * back from emulation context to vcpu. Userspace shouldn't do |
7ae441ea GN |
8899 | * that usually, but some bad designed PV devices (vmware |
8900 | * backdoor interface) need this to work | |
8901 | */ | |
c9b8b07c | 8902 | emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); |
7ae441ea GN |
8903 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
8904 | } | |
de3cd117 SC |
8905 | regs->rax = kvm_rax_read(vcpu); |
8906 | regs->rbx = kvm_rbx_read(vcpu); | |
8907 | regs->rcx = kvm_rcx_read(vcpu); | |
8908 | regs->rdx = kvm_rdx_read(vcpu); | |
8909 | regs->rsi = kvm_rsi_read(vcpu); | |
8910 | regs->rdi = kvm_rdi_read(vcpu); | |
e9c16c78 | 8911 | regs->rsp = kvm_rsp_read(vcpu); |
de3cd117 | 8912 | regs->rbp = kvm_rbp_read(vcpu); |
b6c7a5dc | 8913 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
8914 | regs->r8 = kvm_r8_read(vcpu); |
8915 | regs->r9 = kvm_r9_read(vcpu); | |
8916 | regs->r10 = kvm_r10_read(vcpu); | |
8917 | regs->r11 = kvm_r11_read(vcpu); | |
8918 | regs->r12 = kvm_r12_read(vcpu); | |
8919 | regs->r13 = kvm_r13_read(vcpu); | |
8920 | regs->r14 = kvm_r14_read(vcpu); | |
8921 | regs->r15 = kvm_r15_read(vcpu); | |
b6c7a5dc HB |
8922 | #endif |
8923 | ||
5fdbf976 | 8924 | regs->rip = kvm_rip_read(vcpu); |
91586a3b | 8925 | regs->rflags = kvm_get_rflags(vcpu); |
01643c51 | 8926 | } |
b6c7a5dc | 8927 | |
01643c51 KH |
8928 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
8929 | { | |
8930 | vcpu_load(vcpu); | |
8931 | __get_regs(vcpu, regs); | |
1fc9b76b | 8932 | vcpu_put(vcpu); |
b6c7a5dc HB |
8933 | return 0; |
8934 | } | |
8935 | ||
01643c51 | 8936 | static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 8937 | { |
7ae441ea GN |
8938 | vcpu->arch.emulate_regs_need_sync_from_vcpu = true; |
8939 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; | |
8940 | ||
de3cd117 SC |
8941 | kvm_rax_write(vcpu, regs->rax); |
8942 | kvm_rbx_write(vcpu, regs->rbx); | |
8943 | kvm_rcx_write(vcpu, regs->rcx); | |
8944 | kvm_rdx_write(vcpu, regs->rdx); | |
8945 | kvm_rsi_write(vcpu, regs->rsi); | |
8946 | kvm_rdi_write(vcpu, regs->rdi); | |
e9c16c78 | 8947 | kvm_rsp_write(vcpu, regs->rsp); |
de3cd117 | 8948 | kvm_rbp_write(vcpu, regs->rbp); |
b6c7a5dc | 8949 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
8950 | kvm_r8_write(vcpu, regs->r8); |
8951 | kvm_r9_write(vcpu, regs->r9); | |
8952 | kvm_r10_write(vcpu, regs->r10); | |
8953 | kvm_r11_write(vcpu, regs->r11); | |
8954 | kvm_r12_write(vcpu, regs->r12); | |
8955 | kvm_r13_write(vcpu, regs->r13); | |
8956 | kvm_r14_write(vcpu, regs->r14); | |
8957 | kvm_r15_write(vcpu, regs->r15); | |
b6c7a5dc HB |
8958 | #endif |
8959 | ||
5fdbf976 | 8960 | kvm_rip_write(vcpu, regs->rip); |
d73235d1 | 8961 | kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); |
b6c7a5dc | 8962 | |
b4f14abd JK |
8963 | vcpu->arch.exception.pending = false; |
8964 | ||
3842d135 | 8965 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
01643c51 | 8966 | } |
3842d135 | 8967 | |
01643c51 KH |
8968 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
8969 | { | |
8970 | vcpu_load(vcpu); | |
8971 | __set_regs(vcpu, regs); | |
875656fe | 8972 | vcpu_put(vcpu); |
b6c7a5dc HB |
8973 | return 0; |
8974 | } | |
8975 | ||
b6c7a5dc HB |
8976 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
8977 | { | |
8978 | struct kvm_segment cs; | |
8979 | ||
3e6e0aab | 8980 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
b6c7a5dc HB |
8981 | *db = cs.db; |
8982 | *l = cs.l; | |
8983 | } | |
8984 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
8985 | ||
01643c51 | 8986 | static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 8987 | { |
89a27f4d | 8988 | struct desc_ptr dt; |
b6c7a5dc | 8989 | |
3e6e0aab GT |
8990 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
8991 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
8992 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
8993 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
8994 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
8995 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 8996 | |
3e6e0aab GT |
8997 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
8998 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 8999 | |
afaf0b2f | 9000 | kvm_x86_ops.get_idt(vcpu, &dt); |
89a27f4d GN |
9001 | sregs->idt.limit = dt.size; |
9002 | sregs->idt.base = dt.address; | |
afaf0b2f | 9003 | kvm_x86_ops.get_gdt(vcpu, &dt); |
89a27f4d GN |
9004 | sregs->gdt.limit = dt.size; |
9005 | sregs->gdt.base = dt.address; | |
b6c7a5dc | 9006 | |
4d4ec087 | 9007 | sregs->cr0 = kvm_read_cr0(vcpu); |
ad312c7c | 9008 | sregs->cr2 = vcpu->arch.cr2; |
9f8fe504 | 9009 | sregs->cr3 = kvm_read_cr3(vcpu); |
fc78f519 | 9010 | sregs->cr4 = kvm_read_cr4(vcpu); |
2d3ad1f4 | 9011 | sregs->cr8 = kvm_get_cr8(vcpu); |
f6801dff | 9012 | sregs->efer = vcpu->arch.efer; |
b6c7a5dc HB |
9013 | sregs->apic_base = kvm_get_apic_base(vcpu); |
9014 | ||
0e96f31e | 9015 | memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap)); |
b6c7a5dc | 9016 | |
04140b41 | 9017 | if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) |
14d0bc1f GN |
9018 | set_bit(vcpu->arch.interrupt.nr, |
9019 | (unsigned long *)sregs->interrupt_bitmap); | |
01643c51 | 9020 | } |
16d7a191 | 9021 | |
01643c51 KH |
9022 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
9023 | struct kvm_sregs *sregs) | |
9024 | { | |
9025 | vcpu_load(vcpu); | |
9026 | __get_sregs(vcpu, sregs); | |
bcdec41c | 9027 | vcpu_put(vcpu); |
b6c7a5dc HB |
9028 | return 0; |
9029 | } | |
9030 | ||
62d9f0db MT |
9031 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
9032 | struct kvm_mp_state *mp_state) | |
9033 | { | |
fd232561 | 9034 | vcpu_load(vcpu); |
f958bd23 SC |
9035 | if (kvm_mpx_supported()) |
9036 | kvm_load_guest_fpu(vcpu); | |
fd232561 | 9037 | |
66450a21 | 9038 | kvm_apic_accept_events(vcpu); |
6aef266c SV |
9039 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED && |
9040 | vcpu->arch.pv.pv_unhalted) | |
9041 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; | |
9042 | else | |
9043 | mp_state->mp_state = vcpu->arch.mp_state; | |
9044 | ||
f958bd23 SC |
9045 | if (kvm_mpx_supported()) |
9046 | kvm_put_guest_fpu(vcpu); | |
fd232561 | 9047 | vcpu_put(vcpu); |
62d9f0db MT |
9048 | return 0; |
9049 | } | |
9050 | ||
9051 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
9052 | struct kvm_mp_state *mp_state) | |
9053 | { | |
e83dff5e CD |
9054 | int ret = -EINVAL; |
9055 | ||
9056 | vcpu_load(vcpu); | |
9057 | ||
bce87cce | 9058 | if (!lapic_in_kernel(vcpu) && |
66450a21 | 9059 | mp_state->mp_state != KVM_MP_STATE_RUNNABLE) |
e83dff5e | 9060 | goto out; |
66450a21 | 9061 | |
27cbe7d6 LA |
9062 | /* |
9063 | * KVM_MP_STATE_INIT_RECEIVED means the processor is in | |
9064 | * INIT state; latched init should be reported using | |
9065 | * KVM_SET_VCPU_EVENTS, so reject it here. | |
9066 | */ | |
9067 | if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) && | |
28bf2888 DH |
9068 | (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || |
9069 | mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) | |
e83dff5e | 9070 | goto out; |
28bf2888 | 9071 | |
66450a21 JK |
9072 | if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { |
9073 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; | |
9074 | set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); | |
9075 | } else | |
9076 | vcpu->arch.mp_state = mp_state->mp_state; | |
3842d135 | 9077 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
e83dff5e CD |
9078 | |
9079 | ret = 0; | |
9080 | out: | |
9081 | vcpu_put(vcpu); | |
9082 | return ret; | |
62d9f0db MT |
9083 | } |
9084 | ||
7f3d35fd KW |
9085 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, |
9086 | int reason, bool has_error_code, u32 error_code) | |
b6c7a5dc | 9087 | { |
c9b8b07c | 9088 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d | 9089 | int ret; |
e01c2426 | 9090 | |
8ec4722d | 9091 | init_emulate_ctxt(vcpu); |
c697518a | 9092 | |
7f3d35fd | 9093 | ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason, |
9d74191a | 9094 | has_error_code, error_code); |
1051778f SC |
9095 | if (ret) { |
9096 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
9097 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
9098 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 9099 | return 0; |
1051778f | 9100 | } |
37817f29 | 9101 | |
9d74191a TY |
9102 | kvm_rip_write(vcpu, ctxt->eip); |
9103 | kvm_set_rflags(vcpu, ctxt->eflags); | |
60fc3d02 | 9104 | return 1; |
37817f29 IE |
9105 | } |
9106 | EXPORT_SYMBOL_GPL(kvm_task_switch); | |
9107 | ||
3140c156 | 9108 | static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
f2981033 | 9109 | { |
37b95951 | 9110 | if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { |
f2981033 LT |
9111 | /* |
9112 | * When EFER.LME and CR0.PG are set, the processor is in | |
9113 | * 64-bit mode (though maybe in a 32-bit code segment). | |
9114 | * CR4.PAE and EFER.LMA must be set. | |
9115 | */ | |
37b95951 | 9116 | if (!(sregs->cr4 & X86_CR4_PAE) |
f2981033 LT |
9117 | || !(sregs->efer & EFER_LMA)) |
9118 | return -EINVAL; | |
9119 | } else { | |
9120 | /* | |
9121 | * Not in 64-bit mode: EFER.LMA is clear and the code | |
9122 | * segment cannot be 64-bit. | |
9123 | */ | |
9124 | if (sregs->efer & EFER_LMA || sregs->cs.l) | |
9125 | return -EINVAL; | |
9126 | } | |
9127 | ||
3ca94192 | 9128 | return kvm_valid_cr4(vcpu, sregs->cr4); |
f2981033 LT |
9129 | } |
9130 | ||
01643c51 | 9131 | static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 9132 | { |
58cb628d | 9133 | struct msr_data apic_base_msr; |
b6c7a5dc | 9134 | int mmu_reset_needed = 0; |
c4d21882 | 9135 | int cpuid_update_needed = 0; |
63f42e02 | 9136 | int pending_vec, max_bits, idx; |
89a27f4d | 9137 | struct desc_ptr dt; |
b4ef9d4e CD |
9138 | int ret = -EINVAL; |
9139 | ||
f2981033 | 9140 | if (kvm_valid_sregs(vcpu, sregs)) |
8dbfb2bf | 9141 | goto out; |
f2981033 | 9142 | |
d3802286 JM |
9143 | apic_base_msr.data = sregs->apic_base; |
9144 | apic_base_msr.host_initiated = true; | |
9145 | if (kvm_set_apic_base(vcpu, &apic_base_msr)) | |
b4ef9d4e | 9146 | goto out; |
6d1068b3 | 9147 | |
89a27f4d GN |
9148 | dt.size = sregs->idt.limit; |
9149 | dt.address = sregs->idt.base; | |
afaf0b2f | 9150 | kvm_x86_ops.set_idt(vcpu, &dt); |
89a27f4d GN |
9151 | dt.size = sregs->gdt.limit; |
9152 | dt.address = sregs->gdt.base; | |
afaf0b2f | 9153 | kvm_x86_ops.set_gdt(vcpu, &dt); |
b6c7a5dc | 9154 | |
ad312c7c | 9155 | vcpu->arch.cr2 = sregs->cr2; |
9f8fe504 | 9156 | mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; |
dc7e795e | 9157 | vcpu->arch.cr3 = sregs->cr3; |
cb3c1e2f | 9158 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
b6c7a5dc | 9159 | |
2d3ad1f4 | 9160 | kvm_set_cr8(vcpu, sregs->cr8); |
b6c7a5dc | 9161 | |
f6801dff | 9162 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
afaf0b2f | 9163 | kvm_x86_ops.set_efer(vcpu, sregs->efer); |
b6c7a5dc | 9164 | |
4d4ec087 | 9165 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
afaf0b2f | 9166 | kvm_x86_ops.set_cr0(vcpu, sregs->cr0); |
d7306163 | 9167 | vcpu->arch.cr0 = sregs->cr0; |
b6c7a5dc | 9168 | |
fc78f519 | 9169 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
c4d21882 WH |
9170 | cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) & |
9171 | (X86_CR4_OSXSAVE | X86_CR4_PKE)); | |
afaf0b2f | 9172 | kvm_x86_ops.set_cr4(vcpu, sregs->cr4); |
c4d21882 | 9173 | if (cpuid_update_needed) |
00b27a3e | 9174 | kvm_update_cpuid(vcpu); |
63f42e02 XG |
9175 | |
9176 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
bf03d4f9 | 9177 | if (is_pae_paging(vcpu)) { |
9f8fe504 | 9178 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
7c93be44 MT |
9179 | mmu_reset_needed = 1; |
9180 | } | |
63f42e02 | 9181 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b6c7a5dc HB |
9182 | |
9183 | if (mmu_reset_needed) | |
9184 | kvm_mmu_reset_context(vcpu); | |
9185 | ||
a50abc3b | 9186 | max_bits = KVM_NR_INTERRUPTS; |
923c61bb GN |
9187 | pending_vec = find_first_bit( |
9188 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); | |
9189 | if (pending_vec < max_bits) { | |
66fd3f7f | 9190 | kvm_queue_interrupt(vcpu, pending_vec, false); |
923c61bb | 9191 | pr_debug("Set back pending irq %d\n", pending_vec); |
b6c7a5dc HB |
9192 | } |
9193 | ||
3e6e0aab GT |
9194 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
9195 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
9196 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
9197 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
9198 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
9199 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 9200 | |
3e6e0aab GT |
9201 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
9202 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 9203 | |
5f0269f5 ME |
9204 | update_cr8_intercept(vcpu); |
9205 | ||
9c3e4aab | 9206 | /* Older userspace won't unhalt the vcpu on reset. */ |
c5af89b6 | 9207 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
9c3e4aab | 9208 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
3eeb3288 | 9209 | !is_protmode(vcpu)) |
9c3e4aab MT |
9210 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
9211 | ||
3842d135 AK |
9212 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
9213 | ||
b4ef9d4e CD |
9214 | ret = 0; |
9215 | out: | |
01643c51 KH |
9216 | return ret; |
9217 | } | |
9218 | ||
9219 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
9220 | struct kvm_sregs *sregs) | |
9221 | { | |
9222 | int ret; | |
9223 | ||
9224 | vcpu_load(vcpu); | |
9225 | ret = __set_sregs(vcpu, sregs); | |
b4ef9d4e CD |
9226 | vcpu_put(vcpu); |
9227 | return ret; | |
b6c7a5dc HB |
9228 | } |
9229 | ||
d0bfb940 JK |
9230 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
9231 | struct kvm_guest_debug *dbg) | |
b6c7a5dc | 9232 | { |
355be0b9 | 9233 | unsigned long rflags; |
ae675ef0 | 9234 | int i, r; |
b6c7a5dc | 9235 | |
66b56562 CD |
9236 | vcpu_load(vcpu); |
9237 | ||
4f926bf2 JK |
9238 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
9239 | r = -EBUSY; | |
9240 | if (vcpu->arch.exception.pending) | |
2122ff5e | 9241 | goto out; |
4f926bf2 JK |
9242 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
9243 | kvm_queue_exception(vcpu, DB_VECTOR); | |
9244 | else | |
9245 | kvm_queue_exception(vcpu, BP_VECTOR); | |
9246 | } | |
9247 | ||
91586a3b JK |
9248 | /* |
9249 | * Read rflags as long as potentially injected trace flags are still | |
9250 | * filtered out. | |
9251 | */ | |
9252 | rflags = kvm_get_rflags(vcpu); | |
355be0b9 JK |
9253 | |
9254 | vcpu->guest_debug = dbg->control; | |
9255 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) | |
9256 | vcpu->guest_debug = 0; | |
9257 | ||
9258 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
ae675ef0 JK |
9259 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
9260 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; | |
c8639010 | 9261 | vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; |
ae675ef0 JK |
9262 | } else { |
9263 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
9264 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
ae675ef0 | 9265 | } |
c8639010 | 9266 | kvm_update_dr7(vcpu); |
ae675ef0 | 9267 | |
f92653ee JK |
9268 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
9269 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + | |
9270 | get_segment_base(vcpu, VCPU_SREG_CS); | |
94fe45da | 9271 | |
91586a3b JK |
9272 | /* |
9273 | * Trigger an rflags update that will inject or remove the trace | |
9274 | * flags. | |
9275 | */ | |
9276 | kvm_set_rflags(vcpu, rflags); | |
b6c7a5dc | 9277 | |
afaf0b2f | 9278 | kvm_x86_ops.update_bp_intercept(vcpu); |
b6c7a5dc | 9279 | |
4f926bf2 | 9280 | r = 0; |
d0bfb940 | 9281 | |
2122ff5e | 9282 | out: |
66b56562 | 9283 | vcpu_put(vcpu); |
b6c7a5dc HB |
9284 | return r; |
9285 | } | |
9286 | ||
8b006791 ZX |
9287 | /* |
9288 | * Translate a guest virtual address to a guest physical address. | |
9289 | */ | |
9290 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
9291 | struct kvm_translation *tr) | |
9292 | { | |
9293 | unsigned long vaddr = tr->linear_address; | |
9294 | gpa_t gpa; | |
f656ce01 | 9295 | int idx; |
8b006791 | 9296 | |
1da5b61d CD |
9297 | vcpu_load(vcpu); |
9298 | ||
f656ce01 | 9299 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
1871c602 | 9300 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
f656ce01 | 9301 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8b006791 ZX |
9302 | tr->physical_address = gpa; |
9303 | tr->valid = gpa != UNMAPPED_GVA; | |
9304 | tr->writeable = 1; | |
9305 | tr->usermode = 0; | |
8b006791 | 9306 | |
1da5b61d | 9307 | vcpu_put(vcpu); |
8b006791 ZX |
9308 | return 0; |
9309 | } | |
9310 | ||
d0752060 HB |
9311 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
9312 | { | |
1393123e | 9313 | struct fxregs_state *fxsave; |
d0752060 | 9314 | |
1393123e | 9315 | vcpu_load(vcpu); |
d0752060 | 9316 | |
b666a4b6 | 9317 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
d0752060 HB |
9318 | memcpy(fpu->fpr, fxsave->st_space, 128); |
9319 | fpu->fcw = fxsave->cwd; | |
9320 | fpu->fsw = fxsave->swd; | |
9321 | fpu->ftwx = fxsave->twd; | |
9322 | fpu->last_opcode = fxsave->fop; | |
9323 | fpu->last_ip = fxsave->rip; | |
9324 | fpu->last_dp = fxsave->rdp; | |
0e96f31e | 9325 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); |
d0752060 | 9326 | |
1393123e | 9327 | vcpu_put(vcpu); |
d0752060 HB |
9328 | return 0; |
9329 | } | |
9330 | ||
9331 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
9332 | { | |
6a96bc7f CD |
9333 | struct fxregs_state *fxsave; |
9334 | ||
9335 | vcpu_load(vcpu); | |
9336 | ||
b666a4b6 | 9337 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
d0752060 | 9338 | |
d0752060 HB |
9339 | memcpy(fxsave->st_space, fpu->fpr, 128); |
9340 | fxsave->cwd = fpu->fcw; | |
9341 | fxsave->swd = fpu->fsw; | |
9342 | fxsave->twd = fpu->ftwx; | |
9343 | fxsave->fop = fpu->last_opcode; | |
9344 | fxsave->rip = fpu->last_ip; | |
9345 | fxsave->rdp = fpu->last_dp; | |
0e96f31e | 9346 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); |
d0752060 | 9347 | |
6a96bc7f | 9348 | vcpu_put(vcpu); |
d0752060 HB |
9349 | return 0; |
9350 | } | |
9351 | ||
01643c51 KH |
9352 | static void store_regs(struct kvm_vcpu *vcpu) |
9353 | { | |
9354 | BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES); | |
9355 | ||
9356 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) | |
9357 | __get_regs(vcpu, &vcpu->run->s.regs.regs); | |
9358 | ||
9359 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) | |
9360 | __get_sregs(vcpu, &vcpu->run->s.regs.sregs); | |
9361 | ||
9362 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) | |
9363 | kvm_vcpu_ioctl_x86_get_vcpu_events( | |
9364 | vcpu, &vcpu->run->s.regs.events); | |
9365 | } | |
9366 | ||
9367 | static int sync_regs(struct kvm_vcpu *vcpu) | |
9368 | { | |
9369 | if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS) | |
9370 | return -EINVAL; | |
9371 | ||
9372 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { | |
9373 | __set_regs(vcpu, &vcpu->run->s.regs.regs); | |
9374 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; | |
9375 | } | |
9376 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { | |
9377 | if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs)) | |
9378 | return -EINVAL; | |
9379 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; | |
9380 | } | |
9381 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { | |
9382 | if (kvm_vcpu_ioctl_x86_set_vcpu_events( | |
9383 | vcpu, &vcpu->run->s.regs.events)) | |
9384 | return -EINVAL; | |
9385 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; | |
9386 | } | |
9387 | ||
9388 | return 0; | |
9389 | } | |
9390 | ||
0ee6a517 | 9391 | static void fx_init(struct kvm_vcpu *vcpu) |
d0752060 | 9392 | { |
b666a4b6 | 9393 | fpstate_init(&vcpu->arch.guest_fpu->state); |
782511b0 | 9394 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
b666a4b6 | 9395 | vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv = |
df1daba7 | 9396 | host_xcr0 | XSTATE_COMPACTION_ENABLED; |
d0752060 | 9397 | |
2acf923e DC |
9398 | /* |
9399 | * Ensure guest xcr0 is valid for loading | |
9400 | */ | |
d91cab78 | 9401 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
2acf923e | 9402 | |
ad312c7c | 9403 | vcpu->arch.cr0 |= X86_CR0_ET; |
d0752060 | 9404 | } |
d0752060 | 9405 | |
897cc38e | 9406 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
e9b11c17 | 9407 | { |
897cc38e SC |
9408 | if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
9409 | pr_warn_once("kvm: SMP vm created on host with unstable TSC; " | |
9410 | "guest TSC will not be reliable\n"); | |
7f1ea208 | 9411 | |
897cc38e | 9412 | return 0; |
e9b11c17 ZX |
9413 | } |
9414 | ||
e529ef66 | 9415 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
e9b11c17 | 9416 | { |
95a0d01e SC |
9417 | struct page *page; |
9418 | int r; | |
c447e76b | 9419 | |
95a0d01e SC |
9420 | if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) |
9421 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
9422 | else | |
9423 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; | |
c447e76b | 9424 | |
95a0d01e | 9425 | kvm_set_tsc_khz(vcpu, max_tsc_khz); |
c447e76b | 9426 | |
95a0d01e SC |
9427 | r = kvm_mmu_create(vcpu); |
9428 | if (r < 0) | |
9429 | return r; | |
9430 | ||
9431 | if (irqchip_in_kernel(vcpu->kvm)) { | |
95a0d01e SC |
9432 | r = kvm_create_lapic(vcpu, lapic_timer_advance_ns); |
9433 | if (r < 0) | |
9434 | goto fail_mmu_destroy; | |
4e19c36f SS |
9435 | if (kvm_apicv_activated(vcpu->kvm)) |
9436 | vcpu->arch.apicv_active = true; | |
95a0d01e SC |
9437 | } else |
9438 | static_key_slow_inc(&kvm_no_apic_vcpu); | |
9439 | ||
9440 | r = -ENOMEM; | |
9441 | ||
9442 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
9443 | if (!page) | |
9444 | goto fail_free_lapic; | |
9445 | vcpu->arch.pio_data = page_address(page); | |
9446 | ||
9447 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, | |
9448 | GFP_KERNEL_ACCOUNT); | |
9449 | if (!vcpu->arch.mce_banks) | |
9450 | goto fail_free_pio_data; | |
9451 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; | |
9452 | ||
9453 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, | |
9454 | GFP_KERNEL_ACCOUNT)) | |
9455 | goto fail_free_mce_banks; | |
9456 | ||
c9b8b07c SC |
9457 | if (!alloc_emulate_ctxt(vcpu)) |
9458 | goto free_wbinvd_dirty_mask; | |
9459 | ||
95a0d01e SC |
9460 | vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, |
9461 | GFP_KERNEL_ACCOUNT); | |
9462 | if (!vcpu->arch.user_fpu) { | |
9463 | pr_err("kvm: failed to allocate userspace's fpu\n"); | |
c9b8b07c | 9464 | goto free_emulate_ctxt; |
95a0d01e SC |
9465 | } |
9466 | ||
9467 | vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, | |
9468 | GFP_KERNEL_ACCOUNT); | |
9469 | if (!vcpu->arch.guest_fpu) { | |
9470 | pr_err("kvm: failed to allocate vcpu's fpu\n"); | |
9471 | goto free_user_fpu; | |
9472 | } | |
9473 | fx_init(vcpu); | |
9474 | ||
95a0d01e | 9475 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
7d2e8748 | 9476 | vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level(vcpu); |
95a0d01e SC |
9477 | |
9478 | vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; | |
9479 | ||
9480 | kvm_async_pf_hash_reset(vcpu); | |
9481 | kvm_pmu_init(vcpu); | |
9482 | ||
9483 | vcpu->arch.pending_external_vector = -1; | |
9484 | vcpu->arch.preempted_in_kernel = false; | |
9485 | ||
9486 | kvm_hv_vcpu_init(vcpu); | |
9487 | ||
afaf0b2f | 9488 | r = kvm_x86_ops.vcpu_create(vcpu); |
95a0d01e SC |
9489 | if (r) |
9490 | goto free_guest_fpu; | |
e9b11c17 | 9491 | |
0cf9135b | 9492 | vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); |
e53d88af | 9493 | vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; |
19efffa2 | 9494 | kvm_vcpu_mtrr_init(vcpu); |
ec7660cc | 9495 | vcpu_load(vcpu); |
d28bc9dd | 9496 | kvm_vcpu_reset(vcpu, false); |
e1732991 | 9497 | kvm_init_mmu(vcpu, false); |
e9b11c17 | 9498 | vcpu_put(vcpu); |
ec7660cc | 9499 | return 0; |
95a0d01e SC |
9500 | |
9501 | free_guest_fpu: | |
9502 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); | |
9503 | free_user_fpu: | |
9504 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); | |
c9b8b07c SC |
9505 | free_emulate_ctxt: |
9506 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); | |
95a0d01e SC |
9507 | free_wbinvd_dirty_mask: |
9508 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); | |
9509 | fail_free_mce_banks: | |
9510 | kfree(vcpu->arch.mce_banks); | |
9511 | fail_free_pio_data: | |
9512 | free_page((unsigned long)vcpu->arch.pio_data); | |
9513 | fail_free_lapic: | |
9514 | kvm_free_lapic(vcpu); | |
9515 | fail_mmu_destroy: | |
9516 | kvm_mmu_destroy(vcpu); | |
9517 | return r; | |
e9b11c17 ZX |
9518 | } |
9519 | ||
31928aa5 | 9520 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
42897d86 | 9521 | { |
8fe8ab46 | 9522 | struct msr_data msr; |
332967a3 | 9523 | struct kvm *kvm = vcpu->kvm; |
42897d86 | 9524 | |
d3457c87 RK |
9525 | kvm_hv_vcpu_postcreate(vcpu); |
9526 | ||
ec7660cc | 9527 | if (mutex_lock_killable(&vcpu->mutex)) |
31928aa5 | 9528 | return; |
ec7660cc | 9529 | vcpu_load(vcpu); |
8fe8ab46 WA |
9530 | msr.data = 0x0; |
9531 | msr.index = MSR_IA32_TSC; | |
9532 | msr.host_initiated = true; | |
9533 | kvm_write_tsc(vcpu, &msr); | |
42897d86 | 9534 | vcpu_put(vcpu); |
2d5ba19b MT |
9535 | |
9536 | /* poll control enabled by default */ | |
9537 | vcpu->arch.msr_kvm_poll_control = 1; | |
9538 | ||
ec7660cc | 9539 | mutex_unlock(&vcpu->mutex); |
42897d86 | 9540 | |
b34de572 WL |
9541 | if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) |
9542 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
9543 | KVMCLOCK_SYNC_PERIOD); | |
42897d86 MT |
9544 | } |
9545 | ||
d40ccc62 | 9546 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 | 9547 | { |
4cbc418a | 9548 | struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache; |
95a0d01e | 9549 | int idx; |
344d9588 | 9550 | |
4cbc418a PB |
9551 | kvm_release_pfn(cache->pfn, cache->dirty, cache); |
9552 | ||
50b143e1 | 9553 | kvmclock_reset(vcpu); |
e9b11c17 | 9554 | |
afaf0b2f | 9555 | kvm_x86_ops.vcpu_free(vcpu); |
50b143e1 | 9556 | |
c9b8b07c | 9557 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
50b143e1 SC |
9558 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
9559 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); | |
9560 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); | |
95a0d01e SC |
9561 | |
9562 | kvm_hv_vcpu_uninit(vcpu); | |
9563 | kvm_pmu_destroy(vcpu); | |
9564 | kfree(vcpu->arch.mce_banks); | |
9565 | kvm_free_lapic(vcpu); | |
9566 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
9567 | kvm_mmu_destroy(vcpu); | |
9568 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
9569 | free_page((unsigned long)vcpu->arch.pio_data); | |
9570 | if (!lapic_in_kernel(vcpu)) | |
9571 | static_key_slow_dec(&kvm_no_apic_vcpu); | |
e9b11c17 ZX |
9572 | } |
9573 | ||
d28bc9dd | 9574 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e9b11c17 | 9575 | { |
b7e31be3 RK |
9576 | kvm_lapic_reset(vcpu, init_event); |
9577 | ||
e69fab5d PB |
9578 | vcpu->arch.hflags = 0; |
9579 | ||
c43203ca | 9580 | vcpu->arch.smi_pending = 0; |
52797bf9 | 9581 | vcpu->arch.smi_count = 0; |
7460fb4a AK |
9582 | atomic_set(&vcpu->arch.nmi_queued, 0); |
9583 | vcpu->arch.nmi_pending = 0; | |
448fa4a9 | 9584 | vcpu->arch.nmi_injected = false; |
5f7552d4 NA |
9585 | kvm_clear_interrupt_queue(vcpu); |
9586 | kvm_clear_exception_queue(vcpu); | |
448fa4a9 | 9587 | |
42dbaa5a | 9588 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
ae561ede | 9589 | kvm_update_dr0123(vcpu); |
6f43ed01 | 9590 | vcpu->arch.dr6 = DR6_INIT; |
42dbaa5a | 9591 | vcpu->arch.dr7 = DR7_FIXED_1; |
c8639010 | 9592 | kvm_update_dr7(vcpu); |
42dbaa5a | 9593 | |
1119022c NA |
9594 | vcpu->arch.cr2 = 0; |
9595 | ||
3842d135 | 9596 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
2635b5c4 VK |
9597 | vcpu->arch.apf.msr_en_val = 0; |
9598 | vcpu->arch.apf.msr_int_val = 0; | |
c9aaa895 | 9599 | vcpu->arch.st.msr_val = 0; |
3842d135 | 9600 | |
12f9a48f GC |
9601 | kvmclock_reset(vcpu); |
9602 | ||
af585b92 GN |
9603 | kvm_clear_async_pf_completion_queue(vcpu); |
9604 | kvm_async_pf_hash_reset(vcpu); | |
9605 | vcpu->arch.apf.halted = false; | |
3842d135 | 9606 | |
a554d207 WL |
9607 | if (kvm_mpx_supported()) { |
9608 | void *mpx_state_buffer; | |
9609 | ||
9610 | /* | |
9611 | * To avoid have the INIT path from kvm_apic_has_events() that be | |
9612 | * called with loaded FPU and does not let userspace fix the state. | |
9613 | */ | |
f775b13e RR |
9614 | if (init_event) |
9615 | kvm_put_guest_fpu(vcpu); | |
b666a4b6 | 9616 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
abd16d68 | 9617 | XFEATURE_BNDREGS); |
a554d207 WL |
9618 | if (mpx_state_buffer) |
9619 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state)); | |
b666a4b6 | 9620 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
abd16d68 | 9621 | XFEATURE_BNDCSR); |
a554d207 WL |
9622 | if (mpx_state_buffer) |
9623 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr)); | |
f775b13e RR |
9624 | if (init_event) |
9625 | kvm_load_guest_fpu(vcpu); | |
a554d207 WL |
9626 | } |
9627 | ||
64d60670 | 9628 | if (!init_event) { |
d28bc9dd | 9629 | kvm_pmu_reset(vcpu); |
64d60670 | 9630 | vcpu->arch.smbase = 0x30000; |
db2336a8 | 9631 | |
db2336a8 | 9632 | vcpu->arch.msr_misc_features_enables = 0; |
a554d207 WL |
9633 | |
9634 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; | |
64d60670 | 9635 | } |
f5132b01 | 9636 | |
66f7b72e JS |
9637 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
9638 | vcpu->arch.regs_avail = ~0; | |
9639 | vcpu->arch.regs_dirty = ~0; | |
9640 | ||
a554d207 WL |
9641 | vcpu->arch.ia32_xss = 0; |
9642 | ||
afaf0b2f | 9643 | kvm_x86_ops.vcpu_reset(vcpu, init_event); |
e9b11c17 ZX |
9644 | } |
9645 | ||
2b4a273b | 9646 | void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
66450a21 JK |
9647 | { |
9648 | struct kvm_segment cs; | |
9649 | ||
9650 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
9651 | cs.selector = vector << 8; | |
9652 | cs.base = vector << 12; | |
9653 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
9654 | kvm_rip_write(vcpu, 0); | |
e9b11c17 ZX |
9655 | } |
9656 | ||
13a34e06 | 9657 | int kvm_arch_hardware_enable(void) |
e9b11c17 | 9658 | { |
ca84d1a2 ZA |
9659 | struct kvm *kvm; |
9660 | struct kvm_vcpu *vcpu; | |
9661 | int i; | |
0dd6a6ed ZA |
9662 | int ret; |
9663 | u64 local_tsc; | |
9664 | u64 max_tsc = 0; | |
9665 | bool stable, backwards_tsc = false; | |
18863bdd AK |
9666 | |
9667 | kvm_shared_msr_cpu_online(); | |
afaf0b2f | 9668 | ret = kvm_x86_ops.hardware_enable(); |
0dd6a6ed ZA |
9669 | if (ret != 0) |
9670 | return ret; | |
9671 | ||
4ea1636b | 9672 | local_tsc = rdtsc(); |
b0c39dc6 | 9673 | stable = !kvm_check_tsc_unstable(); |
0dd6a6ed ZA |
9674 | list_for_each_entry(kvm, &vm_list, vm_list) { |
9675 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
9676 | if (!stable && vcpu->cpu == smp_processor_id()) | |
105b21bb | 9677 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
9678 | if (stable && vcpu->arch.last_host_tsc > local_tsc) { |
9679 | backwards_tsc = true; | |
9680 | if (vcpu->arch.last_host_tsc > max_tsc) | |
9681 | max_tsc = vcpu->arch.last_host_tsc; | |
9682 | } | |
9683 | } | |
9684 | } | |
9685 | ||
9686 | /* | |
9687 | * Sometimes, even reliable TSCs go backwards. This happens on | |
9688 | * platforms that reset TSC during suspend or hibernate actions, but | |
9689 | * maintain synchronization. We must compensate. Fortunately, we can | |
9690 | * detect that condition here, which happens early in CPU bringup, | |
9691 | * before any KVM threads can be running. Unfortunately, we can't | |
9692 | * bring the TSCs fully up to date with real time, as we aren't yet far | |
9693 | * enough into CPU bringup that we know how much real time has actually | |
9285ec4c | 9694 | * elapsed; our helper function, ktime_get_boottime_ns() will be using boot |
0dd6a6ed ZA |
9695 | * variables that haven't been updated yet. |
9696 | * | |
9697 | * So we simply find the maximum observed TSC above, then record the | |
9698 | * adjustment to TSC in each VCPU. When the VCPU later gets loaded, | |
9699 | * the adjustment will be applied. Note that we accumulate | |
9700 | * adjustments, in case multiple suspend cycles happen before some VCPU | |
9701 | * gets a chance to run again. In the event that no KVM threads get a | |
9702 | * chance to run, we will miss the entire elapsed period, as we'll have | |
9703 | * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may | |
9704 | * loose cycle time. This isn't too big a deal, since the loss will be | |
9705 | * uniform across all VCPUs (not to mention the scenario is extremely | |
9706 | * unlikely). It is possible that a second hibernate recovery happens | |
9707 | * much faster than a first, causing the observed TSC here to be | |
9708 | * smaller; this would require additional padding adjustment, which is | |
9709 | * why we set last_host_tsc to the local tsc observed here. | |
9710 | * | |
9711 | * N.B. - this code below runs only on platforms with reliable TSC, | |
9712 | * as that is the only way backwards_tsc is set above. Also note | |
9713 | * that this runs for ALL vcpus, which is not a bug; all VCPUs should | |
9714 | * have the same delta_cyc adjustment applied if backwards_tsc | |
9715 | * is detected. Note further, this adjustment is only done once, | |
9716 | * as we reset last_host_tsc on all VCPUs to stop this from being | |
9717 | * called multiple times (one for each physical CPU bringup). | |
9718 | * | |
4a969980 | 9719 | * Platforms with unreliable TSCs don't have to deal with this, they |
0dd6a6ed ZA |
9720 | * will be compensated by the logic in vcpu_load, which sets the TSC to |
9721 | * catchup mode. This will catchup all VCPUs to real time, but cannot | |
9722 | * guarantee that they stay in perfect synchronization. | |
9723 | */ | |
9724 | if (backwards_tsc) { | |
9725 | u64 delta_cyc = max_tsc - local_tsc; | |
9726 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
a826faf1 | 9727 | kvm->arch.backwards_tsc_observed = true; |
0dd6a6ed ZA |
9728 | kvm_for_each_vcpu(i, vcpu, kvm) { |
9729 | vcpu->arch.tsc_offset_adjustment += delta_cyc; | |
9730 | vcpu->arch.last_host_tsc = local_tsc; | |
105b21bb | 9731 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
9732 | } |
9733 | ||
9734 | /* | |
9735 | * We have to disable TSC offset matching.. if you were | |
9736 | * booting a VM while issuing an S4 host suspend.... | |
9737 | * you may have some problem. Solving this issue is | |
9738 | * left as an exercise to the reader. | |
9739 | */ | |
9740 | kvm->arch.last_tsc_nsec = 0; | |
9741 | kvm->arch.last_tsc_write = 0; | |
9742 | } | |
9743 | ||
9744 | } | |
9745 | return 0; | |
e9b11c17 ZX |
9746 | } |
9747 | ||
13a34e06 | 9748 | void kvm_arch_hardware_disable(void) |
e9b11c17 | 9749 | { |
afaf0b2f | 9750 | kvm_x86_ops.hardware_disable(); |
13a34e06 | 9751 | drop_user_return_notifiers(); |
e9b11c17 ZX |
9752 | } |
9753 | ||
b9904085 | 9754 | int kvm_arch_hardware_setup(void *opaque) |
e9b11c17 | 9755 | { |
d008dfdb | 9756 | struct kvm_x86_init_ops *ops = opaque; |
9e9c3fe4 NA |
9757 | int r; |
9758 | ||
91661989 SC |
9759 | rdmsrl_safe(MSR_EFER, &host_efer); |
9760 | ||
408e9a31 PB |
9761 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
9762 | rdmsrl(MSR_IA32_XSS, host_xss); | |
9763 | ||
d008dfdb | 9764 | r = ops->hardware_setup(); |
9e9c3fe4 NA |
9765 | if (r != 0) |
9766 | return r; | |
9767 | ||
afaf0b2f | 9768 | memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); |
69c6f69a | 9769 | |
408e9a31 PB |
9770 | if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) |
9771 | supported_xss = 0; | |
9772 | ||
139f7425 PB |
9773 | #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) |
9774 | cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_); | |
9775 | #undef __kvm_cpu_cap_has | |
b11306b5 | 9776 | |
35181e86 HZ |
9777 | if (kvm_has_tsc_control) { |
9778 | /* | |
9779 | * Make sure the user can only configure tsc_khz values that | |
9780 | * fit into a signed integer. | |
273ba457 | 9781 | * A min value is not calculated because it will always |
35181e86 HZ |
9782 | * be 1 on all machines. |
9783 | */ | |
9784 | u64 max = min(0x7fffffffULL, | |
9785 | __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz)); | |
9786 | kvm_max_guest_tsc_khz = max; | |
9787 | ||
ad721883 | 9788 | kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits; |
35181e86 | 9789 | } |
ad721883 | 9790 | |
9e9c3fe4 NA |
9791 | kvm_init_msr_list(); |
9792 | return 0; | |
e9b11c17 ZX |
9793 | } |
9794 | ||
9795 | void kvm_arch_hardware_unsetup(void) | |
9796 | { | |
afaf0b2f | 9797 | kvm_x86_ops.hardware_unsetup(); |
e9b11c17 ZX |
9798 | } |
9799 | ||
b9904085 | 9800 | int kvm_arch_check_processor_compat(void *opaque) |
e9b11c17 | 9801 | { |
f1cdecf5 | 9802 | struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
d008dfdb | 9803 | struct kvm_x86_init_ops *ops = opaque; |
f1cdecf5 SC |
9804 | |
9805 | WARN_ON(!irqs_disabled()); | |
9806 | ||
139f7425 PB |
9807 | if (__cr4_reserved_bits(cpu_has, c) != |
9808 | __cr4_reserved_bits(cpu_has, &boot_cpu_data)) | |
f1cdecf5 SC |
9809 | return -EIO; |
9810 | ||
d008dfdb | 9811 | return ops->check_processor_compatibility(); |
d71ba788 PB |
9812 | } |
9813 | ||
9814 | bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) | |
9815 | { | |
9816 | return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; | |
9817 | } | |
9818 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp); | |
9819 | ||
9820 | bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) | |
9821 | { | |
9822 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; | |
e9b11c17 ZX |
9823 | } |
9824 | ||
54e9818f | 9825 | struct static_key kvm_no_apic_vcpu __read_mostly; |
bce87cce | 9826 | EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu); |
54e9818f | 9827 | |
e790d9ef RK |
9828 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) |
9829 | { | |
b35e5548 LX |
9830 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
9831 | ||
c595ceee | 9832 | vcpu->arch.l1tf_flush_l1d = true; |
b35e5548 LX |
9833 | if (pmu->version && unlikely(pmu->event_count)) { |
9834 | pmu->need_cleanup = true; | |
9835 | kvm_make_request(KVM_REQ_PMU, vcpu); | |
9836 | } | |
afaf0b2f | 9837 | kvm_x86_ops.sched_in(vcpu, cpu); |
e790d9ef RK |
9838 | } |
9839 | ||
562b6b08 SC |
9840 | void kvm_arch_free_vm(struct kvm *kvm) |
9841 | { | |
9842 | kfree(kvm->arch.hyperv.hv_pa_pg); | |
9843 | vfree(kvm); | |
e790d9ef RK |
9844 | } |
9845 | ||
562b6b08 | 9846 | |
e08b9637 | 9847 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
d19a9cd2 | 9848 | { |
e08b9637 CO |
9849 | if (type) |
9850 | return -EINVAL; | |
9851 | ||
6ef768fa | 9852 | INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); |
f05e70ac | 9853 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
10605204 | 9854 | INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages); |
1aa9b957 | 9855 | INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages); |
4d5c5d0f | 9856 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
e0f0bbc5 | 9857 | atomic_set(&kvm->arch.noncoherent_dma_count, 0); |
d19a9cd2 | 9858 | |
5550af4d SY |
9859 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
9860 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); | |
7a84428a AW |
9861 | /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ |
9862 | set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, | |
9863 | &kvm->arch.irq_sources_bitmap); | |
5550af4d | 9864 | |
038f8c11 | 9865 | raw_spin_lock_init(&kvm->arch.tsc_write_lock); |
1e08ec4a | 9866 | mutex_init(&kvm->arch.apic_map_lock); |
d828199e MT |
9867 | spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock); |
9868 | ||
8171cd68 | 9869 | kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); |
d828199e | 9870 | pvclock_update_vm_gtod_copy(kvm); |
53f658b3 | 9871 | |
6fbbde9a DS |
9872 | kvm->arch.guest_can_read_msr_platform_info = true; |
9873 | ||
7e44e449 | 9874 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); |
332967a3 | 9875 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); |
7e44e449 | 9876 | |
cbc0236a | 9877 | kvm_hv_init_vm(kvm); |
0eb05bf2 | 9878 | kvm_page_track_init(kvm); |
13d268ca | 9879 | kvm_mmu_init_vm(kvm); |
0eb05bf2 | 9880 | |
afaf0b2f | 9881 | return kvm_x86_ops.vm_init(kvm); |
d19a9cd2 ZX |
9882 | } |
9883 | ||
1aa9b957 JS |
9884 | int kvm_arch_post_init_vm(struct kvm *kvm) |
9885 | { | |
9886 | return kvm_mmu_post_init_vm(kvm); | |
9887 | } | |
9888 | ||
d19a9cd2 ZX |
9889 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
9890 | { | |
ec7660cc | 9891 | vcpu_load(vcpu); |
d19a9cd2 ZX |
9892 | kvm_mmu_unload(vcpu); |
9893 | vcpu_put(vcpu); | |
9894 | } | |
9895 | ||
9896 | static void kvm_free_vcpus(struct kvm *kvm) | |
9897 | { | |
9898 | unsigned int i; | |
988a2cae | 9899 | struct kvm_vcpu *vcpu; |
d19a9cd2 ZX |
9900 | |
9901 | /* | |
9902 | * Unpin any mmu pages first. | |
9903 | */ | |
af585b92 GN |
9904 | kvm_for_each_vcpu(i, vcpu, kvm) { |
9905 | kvm_clear_async_pf_completion_queue(vcpu); | |
988a2cae | 9906 | kvm_unload_vcpu_mmu(vcpu); |
af585b92 | 9907 | } |
988a2cae | 9908 | kvm_for_each_vcpu(i, vcpu, kvm) |
4543bdc0 | 9909 | kvm_vcpu_destroy(vcpu); |
988a2cae GN |
9910 | |
9911 | mutex_lock(&kvm->lock); | |
9912 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
9913 | kvm->vcpus[i] = NULL; | |
d19a9cd2 | 9914 | |
988a2cae GN |
9915 | atomic_set(&kvm->online_vcpus, 0); |
9916 | mutex_unlock(&kvm->lock); | |
d19a9cd2 ZX |
9917 | } |
9918 | ||
ad8ba2cd SY |
9919 | void kvm_arch_sync_events(struct kvm *kvm) |
9920 | { | |
332967a3 | 9921 | cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); |
7e44e449 | 9922 | cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); |
aea924f6 | 9923 | kvm_free_pit(kvm); |
ad8ba2cd SY |
9924 | } |
9925 | ||
1d8007bd | 9926 | int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) |
9da0e4d5 PB |
9927 | { |
9928 | int i, r; | |
0577d1ab | 9929 | unsigned long hva, uninitialized_var(old_npages); |
f0d648bd | 9930 | struct kvm_memslots *slots = kvm_memslots(kvm); |
0577d1ab | 9931 | struct kvm_memory_slot *slot; |
9da0e4d5 PB |
9932 | |
9933 | /* Called with kvm->slots_lock held. */ | |
1d8007bd PB |
9934 | if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) |
9935 | return -EINVAL; | |
9da0e4d5 | 9936 | |
f0d648bd PB |
9937 | slot = id_to_memslot(slots, id); |
9938 | if (size) { | |
0577d1ab | 9939 | if (slot && slot->npages) |
f0d648bd PB |
9940 | return -EEXIST; |
9941 | ||
9942 | /* | |
9943 | * MAP_SHARED to prevent internal slot pages from being moved | |
9944 | * by fork()/COW. | |
9945 | */ | |
9946 | hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, | |
9947 | MAP_SHARED | MAP_ANONYMOUS, 0); | |
9948 | if (IS_ERR((void *)hva)) | |
9949 | return PTR_ERR((void *)hva); | |
9950 | } else { | |
0577d1ab | 9951 | if (!slot || !slot->npages) |
f0d648bd PB |
9952 | return 0; |
9953 | ||
abbed4fa SC |
9954 | /* |
9955 | * Stuff a non-canonical value to catch use-after-delete. This | |
9956 | * ends up being 0 on 32-bit KVM, but there's no better | |
9957 | * alternative. | |
9958 | */ | |
9959 | hva = (unsigned long)(0xdeadull << 48); | |
0577d1ab | 9960 | old_npages = slot->npages; |
f0d648bd PB |
9961 | } |
9962 | ||
9da0e4d5 | 9963 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
1d8007bd | 9964 | struct kvm_userspace_memory_region m; |
9da0e4d5 | 9965 | |
1d8007bd PB |
9966 | m.slot = id | (i << 16); |
9967 | m.flags = 0; | |
9968 | m.guest_phys_addr = gpa; | |
f0d648bd | 9969 | m.userspace_addr = hva; |
1d8007bd | 9970 | m.memory_size = size; |
9da0e4d5 PB |
9971 | r = __kvm_set_memory_region(kvm, &m); |
9972 | if (r < 0) | |
9973 | return r; | |
9974 | } | |
9975 | ||
103c763c | 9976 | if (!size) |
0577d1ab | 9977 | vm_munmap(hva, old_npages * PAGE_SIZE); |
f0d648bd | 9978 | |
9da0e4d5 PB |
9979 | return 0; |
9980 | } | |
9981 | EXPORT_SYMBOL_GPL(__x86_set_memory_region); | |
9982 | ||
1aa9b957 JS |
9983 | void kvm_arch_pre_destroy_vm(struct kvm *kvm) |
9984 | { | |
9985 | kvm_mmu_pre_destroy_vm(kvm); | |
9986 | } | |
9987 | ||
d19a9cd2 ZX |
9988 | void kvm_arch_destroy_vm(struct kvm *kvm) |
9989 | { | |
27469d29 AH |
9990 | if (current->mm == kvm->mm) { |
9991 | /* | |
9992 | * Free memory regions allocated on behalf of userspace, | |
9993 | * unless the the memory map has changed due to process exit | |
9994 | * or fd copying. | |
9995 | */ | |
6a3c623b PX |
9996 | mutex_lock(&kvm->slots_lock); |
9997 | __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, | |
9998 | 0, 0); | |
9999 | __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, | |
10000 | 0, 0); | |
10001 | __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); | |
10002 | mutex_unlock(&kvm->slots_lock); | |
27469d29 | 10003 | } |
afaf0b2f SC |
10004 | if (kvm_x86_ops.vm_destroy) |
10005 | kvm_x86_ops.vm_destroy(kvm); | |
c761159c PX |
10006 | kvm_pic_destroy(kvm); |
10007 | kvm_ioapic_destroy(kvm); | |
d19a9cd2 | 10008 | kvm_free_vcpus(kvm); |
af1bae54 | 10009 | kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); |
66bb8a06 | 10010 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); |
13d268ca | 10011 | kvm_mmu_uninit_vm(kvm); |
2beb6dad | 10012 | kvm_page_track_cleanup(kvm); |
cbc0236a | 10013 | kvm_hv_destroy_vm(kvm); |
d19a9cd2 | 10014 | } |
0de10343 | 10015 | |
e96c81ee | 10016 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
db3fe4eb TY |
10017 | { |
10018 | int i; | |
10019 | ||
d89cc617 | 10020 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
e96c81ee SC |
10021 | kvfree(slot->arch.rmap[i]); |
10022 | slot->arch.rmap[i] = NULL; | |
10023 | ||
d89cc617 TY |
10024 | if (i == 0) |
10025 | continue; | |
10026 | ||
e96c81ee SC |
10027 | kvfree(slot->arch.lpage_info[i - 1]); |
10028 | slot->arch.lpage_info[i - 1] = NULL; | |
db3fe4eb | 10029 | } |
21ebbeda | 10030 | |
e96c81ee | 10031 | kvm_page_track_free_memslot(slot); |
db3fe4eb TY |
10032 | } |
10033 | ||
0dab98b7 SC |
10034 | static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, |
10035 | unsigned long npages) | |
db3fe4eb TY |
10036 | { |
10037 | int i; | |
10038 | ||
edd4fa37 SC |
10039 | /* |
10040 | * Clear out the previous array pointers for the KVM_MR_MOVE case. The | |
10041 | * old arrays will be freed by __kvm_set_memory_region() if installing | |
10042 | * the new memslot is successful. | |
10043 | */ | |
10044 | memset(&slot->arch, 0, sizeof(slot->arch)); | |
10045 | ||
d89cc617 | 10046 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
92f94f1e | 10047 | struct kvm_lpage_info *linfo; |
db3fe4eb TY |
10048 | unsigned long ugfn; |
10049 | int lpages; | |
d89cc617 | 10050 | int level = i + 1; |
db3fe4eb TY |
10051 | |
10052 | lpages = gfn_to_index(slot->base_gfn + npages - 1, | |
10053 | slot->base_gfn, level) + 1; | |
10054 | ||
d89cc617 | 10055 | slot->arch.rmap[i] = |
778e1cdd | 10056 | kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), |
254272ce | 10057 | GFP_KERNEL_ACCOUNT); |
d89cc617 | 10058 | if (!slot->arch.rmap[i]) |
77d11309 | 10059 | goto out_free; |
d89cc617 TY |
10060 | if (i == 0) |
10061 | continue; | |
77d11309 | 10062 | |
254272ce | 10063 | linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); |
92f94f1e | 10064 | if (!linfo) |
db3fe4eb TY |
10065 | goto out_free; |
10066 | ||
92f94f1e XG |
10067 | slot->arch.lpage_info[i - 1] = linfo; |
10068 | ||
db3fe4eb | 10069 | if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 10070 | linfo[0].disallow_lpage = 1; |
db3fe4eb | 10071 | if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 10072 | linfo[lpages - 1].disallow_lpage = 1; |
db3fe4eb TY |
10073 | ugfn = slot->userspace_addr >> PAGE_SHIFT; |
10074 | /* | |
10075 | * If the gfn and userspace address are not aligned wrt each | |
600087b6 | 10076 | * other, disable large page support for this slot. |
db3fe4eb | 10077 | */ |
600087b6 | 10078 | if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { |
db3fe4eb TY |
10079 | unsigned long j; |
10080 | ||
10081 | for (j = 0; j < lpages; ++j) | |
92f94f1e | 10082 | linfo[j].disallow_lpage = 1; |
db3fe4eb TY |
10083 | } |
10084 | } | |
10085 | ||
21ebbeda XG |
10086 | if (kvm_page_track_create_memslot(slot, npages)) |
10087 | goto out_free; | |
10088 | ||
db3fe4eb TY |
10089 | return 0; |
10090 | ||
10091 | out_free: | |
d89cc617 | 10092 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
548ef284 | 10093 | kvfree(slot->arch.rmap[i]); |
d89cc617 TY |
10094 | slot->arch.rmap[i] = NULL; |
10095 | if (i == 0) | |
10096 | continue; | |
10097 | ||
548ef284 | 10098 | kvfree(slot->arch.lpage_info[i - 1]); |
d89cc617 | 10099 | slot->arch.lpage_info[i - 1] = NULL; |
db3fe4eb TY |
10100 | } |
10101 | return -ENOMEM; | |
10102 | } | |
10103 | ||
15248258 | 10104 | void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) |
e59dbe09 | 10105 | { |
91724814 BO |
10106 | struct kvm_vcpu *vcpu; |
10107 | int i; | |
10108 | ||
e6dff7d1 TY |
10109 | /* |
10110 | * memslots->generation has been incremented. | |
10111 | * mmio generation may have reached its maximum value. | |
10112 | */ | |
15248258 | 10113 | kvm_mmu_invalidate_mmio_sptes(kvm, gen); |
91724814 BO |
10114 | |
10115 | /* Force re-initialization of steal_time cache */ | |
10116 | kvm_for_each_vcpu(i, vcpu, kvm) | |
10117 | kvm_vcpu_kick(vcpu); | |
e59dbe09 TY |
10118 | } |
10119 | ||
f7784b8e MT |
10120 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
10121 | struct kvm_memory_slot *memslot, | |
09170a49 | 10122 | const struct kvm_userspace_memory_region *mem, |
7b6195a9 | 10123 | enum kvm_mr_change change) |
0de10343 | 10124 | { |
0dab98b7 SC |
10125 | if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) |
10126 | return kvm_alloc_memslot_metadata(memslot, | |
10127 | mem->memory_size >> PAGE_SHIFT); | |
f7784b8e MT |
10128 | return 0; |
10129 | } | |
10130 | ||
88178fd4 | 10131 | static void kvm_mmu_slot_apply_flags(struct kvm *kvm, |
3741679b AY |
10132 | struct kvm_memory_slot *old, |
10133 | struct kvm_memory_slot *new, | |
10134 | enum kvm_mr_change change) | |
88178fd4 | 10135 | { |
3741679b AY |
10136 | /* |
10137 | * Nothing to do for RO slots or CREATE/MOVE/DELETE of a slot. | |
10138 | * See comments below. | |
10139 | */ | |
10140 | if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY)) | |
88178fd4 | 10141 | return; |
88178fd4 KH |
10142 | |
10143 | /* | |
3741679b AY |
10144 | * Dirty logging tracks sptes in 4k granularity, meaning that large |
10145 | * sptes have to be split. If live migration is successful, the guest | |
10146 | * in the source machine will be destroyed and large sptes will be | |
10147 | * created in the destination. However, if the guest continues to run | |
10148 | * in the source machine (for example if live migration fails), small | |
10149 | * sptes will remain around and cause bad performance. | |
88178fd4 | 10150 | * |
3741679b AY |
10151 | * Scan sptes if dirty logging has been stopped, dropping those |
10152 | * which can be collapsed into a single large-page spte. Later | |
10153 | * page faults will create the large-page sptes. | |
88178fd4 | 10154 | * |
3741679b AY |
10155 | * There is no need to do this in any of the following cases: |
10156 | * CREATE: No dirty mappings will already exist. | |
10157 | * MOVE/DELETE: The old mappings will already have been cleaned up by | |
10158 | * kvm_arch_flush_shadow_memslot() | |
10159 | */ | |
10160 | if ((old->flags & KVM_MEM_LOG_DIRTY_PAGES) && | |
10161 | !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
10162 | kvm_mmu_zap_collapsible_sptes(kvm, new); | |
10163 | ||
10164 | /* | |
10165 | * Enable or disable dirty logging for the slot. | |
88178fd4 | 10166 | * |
3741679b AY |
10167 | * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of the old |
10168 | * slot have been zapped so no dirty logging updates are needed for | |
10169 | * the old slot. | |
10170 | * For KVM_MR_CREATE and KVM_MR_MOVE, once the new slot is visible | |
10171 | * any mappings that might be created in it will consume the | |
10172 | * properties of the new slot and do not need to be updated here. | |
88178fd4 | 10173 | * |
3741679b AY |
10174 | * When PML is enabled, the kvm_x86_ops dirty logging hooks are |
10175 | * called to enable/disable dirty logging. | |
88178fd4 | 10176 | * |
3741679b AY |
10177 | * When disabling dirty logging with PML enabled, the D-bit is set |
10178 | * for sptes in the slot in order to prevent unnecessary GPA | |
10179 | * logging in the PML buffer (and potential PML buffer full VMEXIT). | |
10180 | * This guarantees leaving PML enabled for the guest's lifetime | |
10181 | * won't have any additional overhead from PML when the guest is | |
10182 | * running with dirty logging disabled. | |
88178fd4 | 10183 | * |
3741679b AY |
10184 | * When enabling dirty logging, large sptes are write-protected |
10185 | * so they can be split on first write. New large sptes cannot | |
10186 | * be created for this slot until the end of the logging. | |
88178fd4 | 10187 | * See the comments in fast_page_fault(). |
3741679b AY |
10188 | * For small sptes, nothing is done if the dirty log is in the |
10189 | * initial-all-set state. Otherwise, depending on whether pml | |
10190 | * is enabled the D-bit or the W-bit will be cleared. | |
88178fd4 KH |
10191 | */ |
10192 | if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) { | |
afaf0b2f SC |
10193 | if (kvm_x86_ops.slot_enable_log_dirty) { |
10194 | kvm_x86_ops.slot_enable_log_dirty(kvm, new); | |
3c9bd400 JZ |
10195 | } else { |
10196 | int level = | |
10197 | kvm_dirty_log_manual_protect_and_init_set(kvm) ? | |
3bae0459 | 10198 | PG_LEVEL_2M : PG_LEVEL_4K; |
3c9bd400 JZ |
10199 | |
10200 | /* | |
10201 | * If we're with initial-all-set, we don't need | |
10202 | * to write protect any small page because | |
10203 | * they're reported as dirty already. However | |
10204 | * we still need to write-protect huge pages | |
10205 | * so that the page split can happen lazily on | |
10206 | * the first write to the huge page. | |
10207 | */ | |
10208 | kvm_mmu_slot_remove_write_access(kvm, new, level); | |
10209 | } | |
88178fd4 | 10210 | } else { |
afaf0b2f SC |
10211 | if (kvm_x86_ops.slot_disable_log_dirty) |
10212 | kvm_x86_ops.slot_disable_log_dirty(kvm, new); | |
88178fd4 KH |
10213 | } |
10214 | } | |
10215 | ||
f7784b8e | 10216 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
09170a49 | 10217 | const struct kvm_userspace_memory_region *mem, |
9d4c197c | 10218 | struct kvm_memory_slot *old, |
f36f3f28 | 10219 | const struct kvm_memory_slot *new, |
8482644a | 10220 | enum kvm_mr_change change) |
f7784b8e | 10221 | { |
48c0e4e9 | 10222 | if (!kvm->arch.n_requested_mmu_pages) |
4d66623c WY |
10223 | kvm_mmu_change_mmu_pages(kvm, |
10224 | kvm_mmu_calculate_default_mmu_pages(kvm)); | |
1c91cad4 | 10225 | |
3ea3b7fa | 10226 | /* |
f36f3f28 | 10227 | * FIXME: const-ify all uses of struct kvm_memory_slot. |
c972f3b1 | 10228 | */ |
3741679b | 10229 | kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change); |
21198846 SC |
10230 | |
10231 | /* Free the arrays associated with the old memslot. */ | |
10232 | if (change == KVM_MR_MOVE) | |
e96c81ee | 10233 | kvm_arch_free_memslot(kvm, old); |
0de10343 | 10234 | } |
1d737c8a | 10235 | |
2df72e9b | 10236 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
34d4cb8f | 10237 | { |
7390de1e | 10238 | kvm_mmu_zap_all(kvm); |
34d4cb8f MT |
10239 | } |
10240 | ||
2df72e9b MT |
10241 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
10242 | struct kvm_memory_slot *slot) | |
10243 | { | |
ae7cd873 | 10244 | kvm_page_track_flush_slot(kvm, slot); |
2df72e9b MT |
10245 | } |
10246 | ||
e6c67d8c LA |
10247 | static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
10248 | { | |
10249 | return (is_guest_mode(vcpu) && | |
afaf0b2f SC |
10250 | kvm_x86_ops.guest_apic_has_interrupt && |
10251 | kvm_x86_ops.guest_apic_has_interrupt(vcpu)); | |
e6c67d8c LA |
10252 | } |
10253 | ||
5d9bc648 PB |
10254 | static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) |
10255 | { | |
10256 | if (!list_empty_careful(&vcpu->async_pf.done)) | |
10257 | return true; | |
10258 | ||
10259 | if (kvm_apic_has_events(vcpu)) | |
10260 | return true; | |
10261 | ||
10262 | if (vcpu->arch.pv.pv_unhalted) | |
10263 | return true; | |
10264 | ||
a5f01f8e WL |
10265 | if (vcpu->arch.exception.pending) |
10266 | return true; | |
10267 | ||
47a66eed Z |
10268 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
10269 | (vcpu->arch.nmi_pending && | |
c300ab9f | 10270 | kvm_x86_ops.nmi_allowed(vcpu, false))) |
5d9bc648 PB |
10271 | return true; |
10272 | ||
47a66eed | 10273 | if (kvm_test_request(KVM_REQ_SMI, vcpu) || |
a9fa7cb6 | 10274 | (vcpu->arch.smi_pending && |
c300ab9f | 10275 | kvm_x86_ops.smi_allowed(vcpu, false))) |
73917739 PB |
10276 | return true; |
10277 | ||
5d9bc648 | 10278 | if (kvm_arch_interrupt_allowed(vcpu) && |
e6c67d8c LA |
10279 | (kvm_cpu_has_interrupt(vcpu) || |
10280 | kvm_guest_apic_has_interrupt(vcpu))) | |
5d9bc648 PB |
10281 | return true; |
10282 | ||
1f4b34f8 AS |
10283 | if (kvm_hv_has_stimer_pending(vcpu)) |
10284 | return true; | |
10285 | ||
d2060bd4 SC |
10286 | if (is_guest_mode(vcpu) && |
10287 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
10288 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
10289 | return true; | |
10290 | ||
5d9bc648 PB |
10291 | return false; |
10292 | } | |
10293 | ||
1d737c8a ZX |
10294 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
10295 | { | |
5d9bc648 | 10296 | return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); |
1d737c8a | 10297 | } |
5736199a | 10298 | |
17e433b5 WL |
10299 | bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
10300 | { | |
10301 | if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) | |
10302 | return true; | |
10303 | ||
10304 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || | |
10305 | kvm_test_request(KVM_REQ_SMI, vcpu) || | |
10306 | kvm_test_request(KVM_REQ_EVENT, vcpu)) | |
10307 | return true; | |
10308 | ||
afaf0b2f | 10309 | if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu)) |
17e433b5 WL |
10310 | return true; |
10311 | ||
10312 | return false; | |
10313 | } | |
10314 | ||
199b5763 LM |
10315 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
10316 | { | |
de63ad4c | 10317 | return vcpu->arch.preempted_in_kernel; |
199b5763 LM |
10318 | } |
10319 | ||
b6d33834 | 10320 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
5736199a | 10321 | { |
b6d33834 | 10322 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
5736199a | 10323 | } |
78646121 GN |
10324 | |
10325 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) | |
10326 | { | |
c300ab9f | 10327 | return kvm_x86_ops.interrupt_allowed(vcpu, false); |
78646121 | 10328 | } |
229456fc | 10329 | |
82b32774 | 10330 | unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu) |
f92653ee | 10331 | { |
82b32774 NA |
10332 | if (is_64_bit_mode(vcpu)) |
10333 | return kvm_rip_read(vcpu); | |
10334 | return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) + | |
10335 | kvm_rip_read(vcpu)); | |
10336 | } | |
10337 | EXPORT_SYMBOL_GPL(kvm_get_linear_rip); | |
f92653ee | 10338 | |
82b32774 NA |
10339 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
10340 | { | |
10341 | return kvm_get_linear_rip(vcpu) == linear_rip; | |
f92653ee JK |
10342 | } |
10343 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); | |
10344 | ||
94fe45da JK |
10345 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
10346 | { | |
10347 | unsigned long rflags; | |
10348 | ||
afaf0b2f | 10349 | rflags = kvm_x86_ops.get_rflags(vcpu); |
94fe45da | 10350 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
c310bac5 | 10351 | rflags &= ~X86_EFLAGS_TF; |
94fe45da JK |
10352 | return rflags; |
10353 | } | |
10354 | EXPORT_SYMBOL_GPL(kvm_get_rflags); | |
10355 | ||
6addfc42 | 10356 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
94fe45da JK |
10357 | { |
10358 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && | |
f92653ee | 10359 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
c310bac5 | 10360 | rflags |= X86_EFLAGS_TF; |
afaf0b2f | 10361 | kvm_x86_ops.set_rflags(vcpu, rflags); |
6addfc42 PB |
10362 | } |
10363 | ||
10364 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
10365 | { | |
10366 | __kvm_set_rflags(vcpu, rflags); | |
3842d135 | 10367 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
94fe45da JK |
10368 | } |
10369 | EXPORT_SYMBOL_GPL(kvm_set_rflags); | |
10370 | ||
56028d08 GN |
10371 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) |
10372 | { | |
10373 | int r; | |
10374 | ||
44dd3ffa | 10375 | if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || |
f2e10669 | 10376 | work->wakeup_all) |
56028d08 GN |
10377 | return; |
10378 | ||
10379 | r = kvm_mmu_reload(vcpu); | |
10380 | if (unlikely(r)) | |
10381 | return; | |
10382 | ||
44dd3ffa | 10383 | if (!vcpu->arch.mmu->direct_map && |
d8dd54e0 | 10384 | work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu)) |
fb67e14f XG |
10385 | return; |
10386 | ||
7a02674d | 10387 | kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true); |
56028d08 GN |
10388 | } |
10389 | ||
af585b92 GN |
10390 | static inline u32 kvm_async_pf_hash_fn(gfn_t gfn) |
10391 | { | |
dd03bcaa PX |
10392 | BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU)); |
10393 | ||
af585b92 GN |
10394 | return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU)); |
10395 | } | |
10396 | ||
10397 | static inline u32 kvm_async_pf_next_probe(u32 key) | |
10398 | { | |
dd03bcaa | 10399 | return (key + 1) & (ASYNC_PF_PER_VCPU - 1); |
af585b92 GN |
10400 | } |
10401 | ||
10402 | static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10403 | { | |
10404 | u32 key = kvm_async_pf_hash_fn(gfn); | |
10405 | ||
10406 | while (vcpu->arch.apf.gfns[key] != ~0) | |
10407 | key = kvm_async_pf_next_probe(key); | |
10408 | ||
10409 | vcpu->arch.apf.gfns[key] = gfn; | |
10410 | } | |
10411 | ||
10412 | static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10413 | { | |
10414 | int i; | |
10415 | u32 key = kvm_async_pf_hash_fn(gfn); | |
10416 | ||
dd03bcaa | 10417 | for (i = 0; i < ASYNC_PF_PER_VCPU && |
c7d28c24 XG |
10418 | (vcpu->arch.apf.gfns[key] != gfn && |
10419 | vcpu->arch.apf.gfns[key] != ~0); i++) | |
af585b92 GN |
10420 | key = kvm_async_pf_next_probe(key); |
10421 | ||
10422 | return key; | |
10423 | } | |
10424 | ||
10425 | bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10426 | { | |
10427 | return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; | |
10428 | } | |
10429 | ||
10430 | static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10431 | { | |
10432 | u32 i, j, k; | |
10433 | ||
10434 | i = j = kvm_async_pf_gfn_slot(vcpu, gfn); | |
0fd46044 PX |
10435 | |
10436 | if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) | |
10437 | return; | |
10438 | ||
af585b92 GN |
10439 | while (true) { |
10440 | vcpu->arch.apf.gfns[i] = ~0; | |
10441 | do { | |
10442 | j = kvm_async_pf_next_probe(j); | |
10443 | if (vcpu->arch.apf.gfns[j] == ~0) | |
10444 | return; | |
10445 | k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); | |
10446 | /* | |
10447 | * k lies cyclically in ]i,j] | |
10448 | * | i.k.j | | |
10449 | * |....j i.k.| or |.k..j i...| | |
10450 | */ | |
10451 | } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j)); | |
10452 | vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; | |
10453 | i = j; | |
10454 | } | |
10455 | } | |
10456 | ||
68fd66f1 | 10457 | static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu) |
7c90705b | 10458 | { |
68fd66f1 VK |
10459 | u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT; |
10460 | ||
10461 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, | |
10462 | sizeof(reason)); | |
10463 | } | |
10464 | ||
10465 | static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token) | |
10466 | { | |
2635b5c4 | 10467 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); |
4e335d9e | 10468 | |
2635b5c4 VK |
10469 | return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, |
10470 | &token, offset, sizeof(token)); | |
10471 | } | |
10472 | ||
10473 | static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu) | |
10474 | { | |
10475 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); | |
10476 | u32 val; | |
10477 | ||
10478 | if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, | |
10479 | &val, offset, sizeof(val))) | |
10480 | return false; | |
10481 | ||
10482 | return !val; | |
7c90705b GN |
10483 | } |
10484 | ||
1dfdb45e PB |
10485 | static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu) |
10486 | { | |
10487 | if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu)) | |
10488 | return false; | |
10489 | ||
2635b5c4 VK |
10490 | if (!kvm_pv_async_pf_enabled(vcpu) || |
10491 | (vcpu->arch.apf.send_user_only && kvm_x86_ops.get_cpl(vcpu) == 0)) | |
1dfdb45e PB |
10492 | return false; |
10493 | ||
10494 | return true; | |
10495 | } | |
10496 | ||
10497 | bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu) | |
10498 | { | |
10499 | if (unlikely(!lapic_in_kernel(vcpu) || | |
10500 | kvm_event_needs_reinjection(vcpu) || | |
10501 | vcpu->arch.exception.pending)) | |
10502 | return false; | |
10503 | ||
10504 | if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) | |
10505 | return false; | |
10506 | ||
10507 | /* | |
10508 | * If interrupts are off we cannot even use an artificial | |
10509 | * halt state. | |
10510 | */ | |
c300ab9f | 10511 | return kvm_arch_interrupt_allowed(vcpu); |
1dfdb45e PB |
10512 | } |
10513 | ||
af585b92 GN |
10514 | void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
10515 | struct kvm_async_pf *work) | |
10516 | { | |
6389ee94 AK |
10517 | struct x86_exception fault; |
10518 | ||
736c291c | 10519 | trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); |
af585b92 | 10520 | kvm_add_async_pf_gfn(vcpu, work->arch.gfn); |
7c90705b | 10521 | |
1dfdb45e | 10522 | if (kvm_can_deliver_async_pf(vcpu) && |
68fd66f1 | 10523 | !apf_put_user_notpresent(vcpu)) { |
6389ee94 AK |
10524 | fault.vector = PF_VECTOR; |
10525 | fault.error_code_valid = true; | |
10526 | fault.error_code = 0; | |
10527 | fault.nested_page_fault = false; | |
10528 | fault.address = work->arch.token; | |
adfe20fb | 10529 | fault.async_page_fault = true; |
6389ee94 | 10530 | kvm_inject_page_fault(vcpu, &fault); |
1dfdb45e PB |
10531 | } else { |
10532 | /* | |
10533 | * It is not possible to deliver a paravirtualized asynchronous | |
10534 | * page fault, but putting the guest in an artificial halt state | |
10535 | * can be beneficial nevertheless: if an interrupt arrives, we | |
10536 | * can deliver it timely and perhaps the guest will schedule | |
10537 | * another process. When the instruction that triggered a page | |
10538 | * fault is retried, hopefully the page will be ready in the host. | |
10539 | */ | |
10540 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); | |
7c90705b | 10541 | } |
af585b92 GN |
10542 | } |
10543 | ||
10544 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, | |
10545 | struct kvm_async_pf *work) | |
10546 | { | |
2635b5c4 VK |
10547 | struct kvm_lapic_irq irq = { |
10548 | .delivery_mode = APIC_DM_FIXED, | |
10549 | .vector = vcpu->arch.apf.vec | |
10550 | }; | |
6389ee94 | 10551 | |
f2e10669 | 10552 | if (work->wakeup_all) |
7c90705b GN |
10553 | work->arch.token = ~0; /* broadcast wakeup */ |
10554 | else | |
10555 | kvm_del_async_pf_gfn(vcpu, work->arch.gfn); | |
736c291c | 10556 | trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); |
7c90705b | 10557 | |
2635b5c4 | 10558 | if (kvm_pv_async_pf_enabled(vcpu) && |
557a961a VK |
10559 | !apf_put_user_ready(vcpu, work->arch.token)) { |
10560 | vcpu->arch.apf.pageready_pending = true; | |
2635b5c4 | 10561 | kvm_apic_set_irq(vcpu, &irq, NULL); |
557a961a | 10562 | } |
2635b5c4 | 10563 | |
e6d53e3b | 10564 | vcpu->arch.apf.halted = false; |
a4fa1635 | 10565 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
7c90705b GN |
10566 | } |
10567 | ||
557a961a VK |
10568 | void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) |
10569 | { | |
10570 | kvm_make_request(KVM_REQ_APF_READY, vcpu); | |
10571 | if (!vcpu->arch.apf.pageready_pending) | |
10572 | kvm_vcpu_kick(vcpu); | |
10573 | } | |
10574 | ||
7c0ade6c | 10575 | bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) |
7c90705b | 10576 | { |
2635b5c4 | 10577 | if (!kvm_pv_async_pf_enabled(vcpu)) |
7c90705b GN |
10578 | return true; |
10579 | else | |
2635b5c4 | 10580 | return apf_pageready_slot_free(vcpu); |
af585b92 GN |
10581 | } |
10582 | ||
5544eb9b PB |
10583 | void kvm_arch_start_assignment(struct kvm *kvm) |
10584 | { | |
10585 | atomic_inc(&kvm->arch.assigned_device_count); | |
10586 | } | |
10587 | EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); | |
10588 | ||
10589 | void kvm_arch_end_assignment(struct kvm *kvm) | |
10590 | { | |
10591 | atomic_dec(&kvm->arch.assigned_device_count); | |
10592 | } | |
10593 | EXPORT_SYMBOL_GPL(kvm_arch_end_assignment); | |
10594 | ||
10595 | bool kvm_arch_has_assigned_device(struct kvm *kvm) | |
10596 | { | |
10597 | return atomic_read(&kvm->arch.assigned_device_count); | |
10598 | } | |
10599 | EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); | |
10600 | ||
e0f0bbc5 AW |
10601 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
10602 | { | |
10603 | atomic_inc(&kvm->arch.noncoherent_dma_count); | |
10604 | } | |
10605 | EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); | |
10606 | ||
10607 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) | |
10608 | { | |
10609 | atomic_dec(&kvm->arch.noncoherent_dma_count); | |
10610 | } | |
10611 | EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma); | |
10612 | ||
10613 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) | |
10614 | { | |
10615 | return atomic_read(&kvm->arch.noncoherent_dma_count); | |
10616 | } | |
10617 | EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); | |
10618 | ||
14717e20 AW |
10619 | bool kvm_arch_has_irq_bypass(void) |
10620 | { | |
92735b1b | 10621 | return true; |
14717e20 AW |
10622 | } |
10623 | ||
87276880 FW |
10624 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
10625 | struct irq_bypass_producer *prod) | |
10626 | { | |
10627 | struct kvm_kernel_irqfd *irqfd = | |
10628 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
10629 | ||
14717e20 | 10630 | irqfd->producer = prod; |
87276880 | 10631 | |
afaf0b2f | 10632 | return kvm_x86_ops.update_pi_irte(irqfd->kvm, |
14717e20 | 10633 | prod->irq, irqfd->gsi, 1); |
87276880 FW |
10634 | } |
10635 | ||
10636 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, | |
10637 | struct irq_bypass_producer *prod) | |
10638 | { | |
10639 | int ret; | |
10640 | struct kvm_kernel_irqfd *irqfd = | |
10641 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
10642 | ||
87276880 FW |
10643 | WARN_ON(irqfd->producer != prod); |
10644 | irqfd->producer = NULL; | |
10645 | ||
10646 | /* | |
10647 | * When producer of consumer is unregistered, we change back to | |
10648 | * remapped mode, so we can re-use the current implementation | |
bb3541f1 | 10649 | * when the irq is masked/disabled or the consumer side (KVM |
87276880 FW |
10650 | * int this case doesn't want to receive the interrupts. |
10651 | */ | |
afaf0b2f | 10652 | ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0); |
87276880 FW |
10653 | if (ret) |
10654 | printk(KERN_INFO "irq bypass consumer (token %p) unregistration" | |
10655 | " fails: %d\n", irqfd->consumer.token, ret); | |
10656 | } | |
10657 | ||
10658 | int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, | |
10659 | uint32_t guest_irq, bool set) | |
10660 | { | |
afaf0b2f | 10661 | return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set); |
87276880 FW |
10662 | } |
10663 | ||
52004014 FW |
10664 | bool kvm_vector_hashing_enabled(void) |
10665 | { | |
10666 | return vector_hashing; | |
10667 | } | |
52004014 | 10668 | |
2d5ba19b MT |
10669 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
10670 | { | |
10671 | return (vcpu->arch.msr_kvm_poll_control & 1) == 0; | |
10672 | } | |
10673 | EXPORT_SYMBOL_GPL(kvm_arch_no_poll); | |
10674 | ||
6441fa61 PB |
10675 | u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu) |
10676 | { | |
10677 | uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD; | |
10678 | ||
10679 | /* The STIBP bit doesn't fault even if it's not advertised */ | |
10680 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) && | |
10681 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS)) | |
10682 | bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP); | |
10683 | if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) && | |
10684 | !boot_cpu_has(X86_FEATURE_AMD_IBRS)) | |
10685 | bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP); | |
10686 | ||
10687 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) && | |
10688 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) | |
10689 | bits &= ~SPEC_CTRL_SSBD; | |
10690 | if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) && | |
10691 | !boot_cpu_has(X86_FEATURE_AMD_SSBD)) | |
10692 | bits &= ~SPEC_CTRL_SSBD; | |
10693 | ||
10694 | return bits; | |
10695 | } | |
10696 | EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits); | |
2d5ba19b | 10697 | |
229456fc | 10698 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
931c33b1 | 10699 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); |
229456fc MT |
10700 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
10701 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); | |
10702 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); | |
10703 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); | |
0ac406de | 10704 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
d8cabddf | 10705 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
17897f36 | 10706 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
236649de | 10707 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
5497b955 | 10708 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); |
ec1ff790 | 10709 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
532a46b9 | 10710 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
2e554e8d | 10711 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |
489223ed | 10712 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); |
4f75bcc3 | 10713 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); |
843e4330 | 10714 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); |
efc64404 | 10715 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); |
18f40c53 SS |
10716 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); |
10717 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); | |
ab56f8e6 | 10718 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); |
24bbf74c | 10719 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request); |