]>
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 | |
b666a4b6 MO |
242 | struct kmem_cache *x86_fpu_cache; |
243 | EXPORT_SYMBOL_GPL(x86_fpu_cache); | |
244 | ||
c9b8b07c SC |
245 | static struct kmem_cache *x86_emulator_cache; |
246 | ||
247 | static struct kmem_cache *kvm_alloc_emulator_cache(void) | |
248 | { | |
06add254 SC |
249 | unsigned int useroffset = offsetof(struct x86_emulate_ctxt, src); |
250 | unsigned int size = sizeof(struct x86_emulate_ctxt); | |
251 | ||
252 | return kmem_cache_create_usercopy("x86_emulator", size, | |
c9b8b07c | 253 | __alignof__(struct x86_emulate_ctxt), |
06add254 SC |
254 | SLAB_ACCOUNT, useroffset, |
255 | size - useroffset, NULL); | |
c9b8b07c SC |
256 | } |
257 | ||
b6785def | 258 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt); |
d6aa1000 | 259 | |
af585b92 GN |
260 | static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu) |
261 | { | |
262 | int i; | |
dd03bcaa | 263 | for (i = 0; i < ASYNC_PF_PER_VCPU; i++) |
af585b92 GN |
264 | vcpu->arch.apf.gfns[i] = ~0; |
265 | } | |
266 | ||
18863bdd AK |
267 | static void kvm_on_user_return(struct user_return_notifier *urn) |
268 | { | |
269 | unsigned slot; | |
18863bdd AK |
270 | struct kvm_shared_msrs *locals |
271 | = container_of(urn, struct kvm_shared_msrs, urn); | |
2bf78fa7 | 272 | struct kvm_shared_msr_values *values; |
1650b4eb IA |
273 | unsigned long flags; |
274 | ||
275 | /* | |
276 | * Disabling irqs at this point since the following code could be | |
277 | * interrupted and executed through kvm_arch_hardware_disable() | |
278 | */ | |
279 | local_irq_save(flags); | |
280 | if (locals->registered) { | |
281 | locals->registered = false; | |
282 | user_return_notifier_unregister(urn); | |
283 | } | |
284 | local_irq_restore(flags); | |
18863bdd | 285 | for (slot = 0; slot < shared_msrs_global.nr; ++slot) { |
2bf78fa7 SY |
286 | values = &locals->values[slot]; |
287 | if (values->host != values->curr) { | |
288 | wrmsrl(shared_msrs_global.msrs[slot], values->host); | |
289 | values->curr = values->host; | |
18863bdd AK |
290 | } |
291 | } | |
18863bdd AK |
292 | } |
293 | ||
2bf78fa7 SY |
294 | void kvm_define_shared_msr(unsigned slot, u32 msr) |
295 | { | |
0123be42 | 296 | BUG_ON(slot >= KVM_NR_SHARED_MSRS); |
c847fe88 | 297 | shared_msrs_global.msrs[slot] = msr; |
18863bdd AK |
298 | if (slot >= shared_msrs_global.nr) |
299 | shared_msrs_global.nr = slot + 1; | |
18863bdd AK |
300 | } |
301 | EXPORT_SYMBOL_GPL(kvm_define_shared_msr); | |
302 | ||
303 | static void kvm_shared_msr_cpu_online(void) | |
304 | { | |
05c19c2f SC |
305 | unsigned int cpu = smp_processor_id(); |
306 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); | |
307 | u64 value; | |
308 | int i; | |
18863bdd | 309 | |
05c19c2f SC |
310 | for (i = 0; i < shared_msrs_global.nr; ++i) { |
311 | rdmsrl_safe(shared_msrs_global.msrs[i], &value); | |
312 | smsr->values[i].host = value; | |
313 | smsr->values[i].curr = value; | |
314 | } | |
18863bdd AK |
315 | } |
316 | ||
8b3c3104 | 317 | int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask) |
18863bdd | 318 | { |
013f6a5d MT |
319 | unsigned int cpu = smp_processor_id(); |
320 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); | |
8b3c3104 | 321 | int err; |
18863bdd | 322 | |
de1fca5d PB |
323 | value = (value & mask) | (smsr->values[slot].host & ~mask); |
324 | if (value == smsr->values[slot].curr) | |
8b3c3104 | 325 | return 0; |
8b3c3104 AH |
326 | err = wrmsrl_safe(shared_msrs_global.msrs[slot], value); |
327 | if (err) | |
328 | return 1; | |
329 | ||
de1fca5d | 330 | smsr->values[slot].curr = value; |
18863bdd AK |
331 | if (!smsr->registered) { |
332 | smsr->urn.on_user_return = kvm_on_user_return; | |
333 | user_return_notifier_register(&smsr->urn); | |
334 | smsr->registered = true; | |
335 | } | |
8b3c3104 | 336 | return 0; |
18863bdd AK |
337 | } |
338 | EXPORT_SYMBOL_GPL(kvm_set_shared_msr); | |
339 | ||
13a34e06 | 340 | static void drop_user_return_notifiers(void) |
3548bab5 | 341 | { |
013f6a5d MT |
342 | unsigned int cpu = smp_processor_id(); |
343 | struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu); | |
3548bab5 AK |
344 | |
345 | if (smsr->registered) | |
346 | kvm_on_user_return(&smsr->urn); | |
347 | } | |
348 | ||
6866b83e CO |
349 | u64 kvm_get_apic_base(struct kvm_vcpu *vcpu) |
350 | { | |
8a5a87d9 | 351 | return vcpu->arch.apic_base; |
6866b83e CO |
352 | } |
353 | EXPORT_SYMBOL_GPL(kvm_get_apic_base); | |
354 | ||
58871649 JM |
355 | enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu) |
356 | { | |
357 | return kvm_apic_mode(kvm_get_apic_base(vcpu)); | |
358 | } | |
359 | EXPORT_SYMBOL_GPL(kvm_get_apic_mode); | |
360 | ||
58cb628d JK |
361 | int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
362 | { | |
58871649 JM |
363 | enum lapic_mode old_mode = kvm_get_apic_mode(vcpu); |
364 | enum lapic_mode new_mode = kvm_apic_mode(msr_info->data); | |
d6321d49 RK |
365 | u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff | |
366 | (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE); | |
58cb628d | 367 | |
58871649 | 368 | if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID) |
58cb628d | 369 | return 1; |
58871649 JM |
370 | if (!msr_info->host_initiated) { |
371 | if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC) | |
372 | return 1; | |
373 | if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC) | |
374 | return 1; | |
375 | } | |
58cb628d JK |
376 | |
377 | kvm_lapic_set_base(vcpu, msr_info->data); | |
4abaffce | 378 | kvm_recalculate_apic_map(vcpu->kvm); |
58cb628d | 379 | return 0; |
6866b83e CO |
380 | } |
381 | EXPORT_SYMBOL_GPL(kvm_set_apic_base); | |
382 | ||
2605fc21 | 383 | asmlinkage __visible void kvm_spurious_fault(void) |
e3ba45b8 GL |
384 | { |
385 | /* Fault while not rebooting. We want the trace. */ | |
b4fdcf60 | 386 | BUG_ON(!kvm_rebooting); |
e3ba45b8 GL |
387 | } |
388 | EXPORT_SYMBOL_GPL(kvm_spurious_fault); | |
389 | ||
3fd28fce ED |
390 | #define EXCPT_BENIGN 0 |
391 | #define EXCPT_CONTRIBUTORY 1 | |
392 | #define EXCPT_PF 2 | |
393 | ||
394 | static int exception_class(int vector) | |
395 | { | |
396 | switch (vector) { | |
397 | case PF_VECTOR: | |
398 | return EXCPT_PF; | |
399 | case DE_VECTOR: | |
400 | case TS_VECTOR: | |
401 | case NP_VECTOR: | |
402 | case SS_VECTOR: | |
403 | case GP_VECTOR: | |
404 | return EXCPT_CONTRIBUTORY; | |
405 | default: | |
406 | break; | |
407 | } | |
408 | return EXCPT_BENIGN; | |
409 | } | |
410 | ||
d6e8c854 NA |
411 | #define EXCPT_FAULT 0 |
412 | #define EXCPT_TRAP 1 | |
413 | #define EXCPT_ABORT 2 | |
414 | #define EXCPT_INTERRUPT 3 | |
415 | ||
416 | static int exception_type(int vector) | |
417 | { | |
418 | unsigned int mask; | |
419 | ||
420 | if (WARN_ON(vector > 31 || vector == NMI_VECTOR)) | |
421 | return EXCPT_INTERRUPT; | |
422 | ||
423 | mask = 1 << vector; | |
424 | ||
425 | /* #DB is trap, as instruction watchpoints are handled elsewhere */ | |
426 | if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR))) | |
427 | return EXCPT_TRAP; | |
428 | ||
429 | if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR))) | |
430 | return EXCPT_ABORT; | |
431 | ||
432 | /* Reserved exceptions will result in fault */ | |
433 | return EXCPT_FAULT; | |
434 | } | |
435 | ||
da998b46 JM |
436 | void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu) |
437 | { | |
438 | unsigned nr = vcpu->arch.exception.nr; | |
439 | bool has_payload = vcpu->arch.exception.has_payload; | |
440 | unsigned long payload = vcpu->arch.exception.payload; | |
441 | ||
442 | if (!has_payload) | |
443 | return; | |
444 | ||
445 | switch (nr) { | |
f10c729f JM |
446 | case DB_VECTOR: |
447 | /* | |
448 | * "Certain debug exceptions may clear bit 0-3. The | |
449 | * remaining contents of the DR6 register are never | |
450 | * cleared by the processor". | |
451 | */ | |
452 | vcpu->arch.dr6 &= ~DR_TRAP_BITS; | |
453 | /* | |
454 | * DR6.RTM is set by all #DB exceptions that don't clear it. | |
455 | */ | |
456 | vcpu->arch.dr6 |= DR6_RTM; | |
457 | vcpu->arch.dr6 |= payload; | |
458 | /* | |
459 | * Bit 16 should be set in the payload whenever the #DB | |
460 | * exception should clear DR6.RTM. This makes the payload | |
461 | * compatible with the pending debug exceptions under VMX. | |
462 | * Though not currently documented in the SDM, this also | |
463 | * makes the payload compatible with the exit qualification | |
464 | * for #DB exceptions under VMX. | |
465 | */ | |
466 | vcpu->arch.dr6 ^= payload & DR6_RTM; | |
307f1cfa OU |
467 | |
468 | /* | |
469 | * The #DB payload is defined as compatible with the 'pending | |
470 | * debug exceptions' field under VMX, not DR6. While bit 12 is | |
471 | * defined in the 'pending debug exceptions' field (enabled | |
472 | * breakpoint), it is reserved and must be zero in DR6. | |
473 | */ | |
474 | vcpu->arch.dr6 &= ~BIT(12); | |
f10c729f | 475 | break; |
da998b46 JM |
476 | case PF_VECTOR: |
477 | vcpu->arch.cr2 = payload; | |
478 | break; | |
479 | } | |
480 | ||
481 | vcpu->arch.exception.has_payload = false; | |
482 | vcpu->arch.exception.payload = 0; | |
483 | } | |
484 | EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload); | |
485 | ||
3fd28fce | 486 | static void kvm_multiple_exception(struct kvm_vcpu *vcpu, |
ce7ddec4 | 487 | unsigned nr, bool has_error, u32 error_code, |
91e86d22 | 488 | bool has_payload, unsigned long payload, bool reinject) |
3fd28fce ED |
489 | { |
490 | u32 prev_nr; | |
491 | int class1, class2; | |
492 | ||
3842d135 AK |
493 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
494 | ||
664f8e26 | 495 | if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) { |
3fd28fce | 496 | queue: |
3ffb2468 NA |
497 | if (has_error && !is_protmode(vcpu)) |
498 | has_error = false; | |
664f8e26 WL |
499 | if (reinject) { |
500 | /* | |
501 | * On vmentry, vcpu->arch.exception.pending is only | |
502 | * true if an event injection was blocked by | |
503 | * nested_run_pending. In that case, however, | |
504 | * vcpu_enter_guest requests an immediate exit, | |
505 | * and the guest shouldn't proceed far enough to | |
506 | * need reinjection. | |
507 | */ | |
508 | WARN_ON_ONCE(vcpu->arch.exception.pending); | |
509 | vcpu->arch.exception.injected = true; | |
91e86d22 JM |
510 | if (WARN_ON_ONCE(has_payload)) { |
511 | /* | |
512 | * A reinjected event has already | |
513 | * delivered its payload. | |
514 | */ | |
515 | has_payload = false; | |
516 | payload = 0; | |
517 | } | |
664f8e26 WL |
518 | } else { |
519 | vcpu->arch.exception.pending = true; | |
520 | vcpu->arch.exception.injected = false; | |
521 | } | |
3fd28fce ED |
522 | vcpu->arch.exception.has_error_code = has_error; |
523 | vcpu->arch.exception.nr = nr; | |
524 | vcpu->arch.exception.error_code = error_code; | |
91e86d22 JM |
525 | vcpu->arch.exception.has_payload = has_payload; |
526 | vcpu->arch.exception.payload = payload; | |
a06230b6 | 527 | if (!is_guest_mode(vcpu)) |
da998b46 | 528 | kvm_deliver_exception_payload(vcpu); |
3fd28fce ED |
529 | return; |
530 | } | |
531 | ||
532 | /* to check exception */ | |
533 | prev_nr = vcpu->arch.exception.nr; | |
534 | if (prev_nr == DF_VECTOR) { | |
535 | /* triple fault -> shutdown */ | |
a8eeb04a | 536 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
3fd28fce ED |
537 | return; |
538 | } | |
539 | class1 = exception_class(prev_nr); | |
540 | class2 = exception_class(nr); | |
541 | if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY) | |
542 | || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) { | |
664f8e26 WL |
543 | /* |
544 | * Generate double fault per SDM Table 5-5. Set | |
545 | * exception.pending = true so that the double fault | |
546 | * can trigger a nested vmexit. | |
547 | */ | |
3fd28fce | 548 | vcpu->arch.exception.pending = true; |
664f8e26 | 549 | vcpu->arch.exception.injected = false; |
3fd28fce ED |
550 | vcpu->arch.exception.has_error_code = true; |
551 | vcpu->arch.exception.nr = DF_VECTOR; | |
552 | vcpu->arch.exception.error_code = 0; | |
c851436a JM |
553 | vcpu->arch.exception.has_payload = false; |
554 | vcpu->arch.exception.payload = 0; | |
3fd28fce ED |
555 | } else |
556 | /* replace previous exception with a new one in a hope | |
557 | that instruction re-execution will regenerate lost | |
558 | exception */ | |
559 | goto queue; | |
560 | } | |
561 | ||
298101da AK |
562 | void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
563 | { | |
91e86d22 | 564 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false); |
298101da AK |
565 | } |
566 | EXPORT_SYMBOL_GPL(kvm_queue_exception); | |
567 | ||
ce7ddec4 JR |
568 | void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr) |
569 | { | |
91e86d22 | 570 | kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true); |
ce7ddec4 JR |
571 | } |
572 | EXPORT_SYMBOL_GPL(kvm_requeue_exception); | |
573 | ||
4d5523cf PB |
574 | void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr, |
575 | unsigned long payload) | |
f10c729f JM |
576 | { |
577 | kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false); | |
578 | } | |
4d5523cf | 579 | EXPORT_SYMBOL_GPL(kvm_queue_exception_p); |
f10c729f | 580 | |
da998b46 JM |
581 | static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr, |
582 | u32 error_code, unsigned long payload) | |
583 | { | |
584 | kvm_multiple_exception(vcpu, nr, true, error_code, | |
585 | true, payload, false); | |
586 | } | |
587 | ||
6affcbed | 588 | int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err) |
c3c91fee | 589 | { |
db8fcefa AP |
590 | if (err) |
591 | kvm_inject_gp(vcpu, 0); | |
592 | else | |
6affcbed KH |
593 | return kvm_skip_emulated_instruction(vcpu); |
594 | ||
595 | return 1; | |
db8fcefa AP |
596 | } |
597 | EXPORT_SYMBOL_GPL(kvm_complete_insn_gp); | |
8df25a32 | 598 | |
6389ee94 | 599 | void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault) |
c3c91fee AK |
600 | { |
601 | ++vcpu->stat.pf_guest; | |
adfe20fb WL |
602 | vcpu->arch.exception.nested_apf = |
603 | is_guest_mode(vcpu) && fault->async_page_fault; | |
da998b46 | 604 | if (vcpu->arch.exception.nested_apf) { |
adfe20fb | 605 | vcpu->arch.apf.nested_apf_token = fault->address; |
da998b46 JM |
606 | kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code); |
607 | } else { | |
608 | kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code, | |
609 | fault->address); | |
610 | } | |
c3c91fee | 611 | } |
27d6c865 | 612 | EXPORT_SYMBOL_GPL(kvm_inject_page_fault); |
c3c91fee | 613 | |
53b3d8e9 SC |
614 | bool kvm_inject_emulated_page_fault(struct kvm_vcpu *vcpu, |
615 | struct x86_exception *fault) | |
d4f8cf66 | 616 | { |
0cd665bd | 617 | struct kvm_mmu *fault_mmu; |
53b3d8e9 SC |
618 | WARN_ON_ONCE(fault->vector != PF_VECTOR); |
619 | ||
0cd665bd PB |
620 | fault_mmu = fault->nested_page_fault ? vcpu->arch.mmu : |
621 | vcpu->arch.walk_mmu; | |
ef54bcfe | 622 | |
ee1fa209 JS |
623 | /* |
624 | * Invalidate the TLB entry for the faulting address, if it exists, | |
625 | * else the access will fault indefinitely (and to emulate hardware). | |
626 | */ | |
627 | if ((fault->error_code & PFERR_PRESENT_MASK) && | |
628 | !(fault->error_code & PFERR_RSVD_MASK)) | |
629 | kvm_mmu_invalidate_gva(vcpu, fault_mmu, fault->address, | |
630 | fault_mmu->root_hpa); | |
631 | ||
632 | fault_mmu->inject_page_fault(vcpu, fault); | |
ef54bcfe | 633 | return fault->nested_page_fault; |
d4f8cf66 | 634 | } |
53b3d8e9 | 635 | EXPORT_SYMBOL_GPL(kvm_inject_emulated_page_fault); |
d4f8cf66 | 636 | |
3419ffc8 SY |
637 | void kvm_inject_nmi(struct kvm_vcpu *vcpu) |
638 | { | |
7460fb4a AK |
639 | atomic_inc(&vcpu->arch.nmi_queued); |
640 | kvm_make_request(KVM_REQ_NMI, vcpu); | |
3419ffc8 SY |
641 | } |
642 | EXPORT_SYMBOL_GPL(kvm_inject_nmi); | |
643 | ||
298101da AK |
644 | void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
645 | { | |
91e86d22 | 646 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false); |
298101da AK |
647 | } |
648 | EXPORT_SYMBOL_GPL(kvm_queue_exception_e); | |
649 | ||
ce7ddec4 JR |
650 | void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code) |
651 | { | |
91e86d22 | 652 | kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true); |
ce7ddec4 JR |
653 | } |
654 | EXPORT_SYMBOL_GPL(kvm_requeue_exception_e); | |
655 | ||
0a79b009 AK |
656 | /* |
657 | * Checks if cpl <= required_cpl; if true, return true. Otherwise queue | |
658 | * a #GP and return false. | |
659 | */ | |
660 | bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl) | |
298101da | 661 | { |
afaf0b2f | 662 | if (kvm_x86_ops.get_cpl(vcpu) <= required_cpl) |
0a79b009 AK |
663 | return true; |
664 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
665 | return false; | |
298101da | 666 | } |
0a79b009 | 667 | EXPORT_SYMBOL_GPL(kvm_require_cpl); |
298101da | 668 | |
16f8a6f9 NA |
669 | bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr) |
670 | { | |
671 | if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE)) | |
672 | return true; | |
673 | ||
674 | kvm_queue_exception(vcpu, UD_VECTOR); | |
675 | return false; | |
676 | } | |
677 | EXPORT_SYMBOL_GPL(kvm_require_dr); | |
678 | ||
ec92fe44 JR |
679 | /* |
680 | * This function will be used to read from the physical memory of the currently | |
54bf36aa | 681 | * running guest. The difference to kvm_vcpu_read_guest_page is that this function |
ec92fe44 JR |
682 | * can read from guest physical or from the guest's guest physical memory. |
683 | */ | |
684 | int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, | |
685 | gfn_t ngfn, void *data, int offset, int len, | |
686 | u32 access) | |
687 | { | |
54987b7a | 688 | struct x86_exception exception; |
ec92fe44 JR |
689 | gfn_t real_gfn; |
690 | gpa_t ngpa; | |
691 | ||
692 | ngpa = gfn_to_gpa(ngfn); | |
54987b7a | 693 | real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception); |
ec92fe44 JR |
694 | if (real_gfn == UNMAPPED_GVA) |
695 | return -EFAULT; | |
696 | ||
697 | real_gfn = gpa_to_gfn(real_gfn); | |
698 | ||
54bf36aa | 699 | return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len); |
ec92fe44 JR |
700 | } |
701 | EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu); | |
702 | ||
69b0049a | 703 | static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, |
3d06b8bf JR |
704 | void *data, int offset, int len, u32 access) |
705 | { | |
706 | return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn, | |
707 | data, offset, len, access); | |
708 | } | |
709 | ||
16cfacc8 SC |
710 | static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu) |
711 | { | |
712 | return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) | | |
713 | rsvd_bits(1, 2); | |
714 | } | |
715 | ||
a03490ed | 716 | /* |
16cfacc8 | 717 | * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise. |
a03490ed | 718 | */ |
ff03a073 | 719 | int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3) |
a03490ed CO |
720 | { |
721 | gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT; | |
722 | unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2; | |
723 | int i; | |
724 | int ret; | |
ff03a073 | 725 | u64 pdpte[ARRAY_SIZE(mmu->pdptrs)]; |
a03490ed | 726 | |
ff03a073 JR |
727 | ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte, |
728 | offset * sizeof(u64), sizeof(pdpte), | |
729 | PFERR_USER_MASK|PFERR_WRITE_MASK); | |
a03490ed CO |
730 | if (ret < 0) { |
731 | ret = 0; | |
732 | goto out; | |
733 | } | |
734 | for (i = 0; i < ARRAY_SIZE(pdpte); ++i) { | |
812f30b2 | 735 | if ((pdpte[i] & PT_PRESENT_MASK) && |
16cfacc8 | 736 | (pdpte[i] & pdptr_rsvd_bits(vcpu))) { |
a03490ed CO |
737 | ret = 0; |
738 | goto out; | |
739 | } | |
740 | } | |
741 | ret = 1; | |
742 | ||
ff03a073 | 743 | memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs)); |
cb3c1e2f SC |
744 | kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR); |
745 | ||
a03490ed | 746 | out: |
a03490ed CO |
747 | |
748 | return ret; | |
749 | } | |
cc4b6871 | 750 | EXPORT_SYMBOL_GPL(load_pdptrs); |
a03490ed | 751 | |
9ed38ffa | 752 | bool pdptrs_changed(struct kvm_vcpu *vcpu) |
d835dfec | 753 | { |
ff03a073 | 754 | u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)]; |
3d06b8bf JR |
755 | int offset; |
756 | gfn_t gfn; | |
d835dfec AK |
757 | int r; |
758 | ||
bf03d4f9 | 759 | if (!is_pae_paging(vcpu)) |
d835dfec AK |
760 | return false; |
761 | ||
cb3c1e2f | 762 | if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR)) |
6de4f3ad AK |
763 | return true; |
764 | ||
a512177e PB |
765 | gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT; |
766 | offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1); | |
3d06b8bf JR |
767 | r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte), |
768 | PFERR_USER_MASK | PFERR_WRITE_MASK); | |
d835dfec | 769 | if (r < 0) |
7f7f0d9c | 770 | return true; |
d835dfec | 771 | |
7f7f0d9c | 772 | return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0; |
d835dfec | 773 | } |
9ed38ffa | 774 | EXPORT_SYMBOL_GPL(pdptrs_changed); |
d835dfec | 775 | |
49a9b07e | 776 | int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
a03490ed | 777 | { |
aad82703 | 778 | unsigned long old_cr0 = kvm_read_cr0(vcpu); |
d81135a5 | 779 | unsigned long update_bits = X86_CR0_PG | X86_CR0_WP; |
aad82703 | 780 | |
f9a48e6a AK |
781 | cr0 |= X86_CR0_ET; |
782 | ||
ab344828 | 783 | #ifdef CONFIG_X86_64 |
0f12244f GN |
784 | if (cr0 & 0xffffffff00000000UL) |
785 | return 1; | |
ab344828 GN |
786 | #endif |
787 | ||
788 | cr0 &= ~CR0_RESERVED_BITS; | |
a03490ed | 789 | |
0f12244f GN |
790 | if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD)) |
791 | return 1; | |
a03490ed | 792 | |
0f12244f GN |
793 | if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE)) |
794 | return 1; | |
a03490ed CO |
795 | |
796 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { | |
797 | #ifdef CONFIG_X86_64 | |
f6801dff | 798 | if ((vcpu->arch.efer & EFER_LME)) { |
a03490ed CO |
799 | int cs_db, cs_l; |
800 | ||
0f12244f GN |
801 | if (!is_pae(vcpu)) |
802 | return 1; | |
afaf0b2f | 803 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
0f12244f GN |
804 | if (cs_l) |
805 | return 1; | |
a03490ed CO |
806 | } else |
807 | #endif | |
ff03a073 | 808 | if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
9f8fe504 | 809 | kvm_read_cr3(vcpu))) |
0f12244f | 810 | return 1; |
a03490ed CO |
811 | } |
812 | ||
ad756a16 MJ |
813 | if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE)) |
814 | return 1; | |
815 | ||
afaf0b2f | 816 | kvm_x86_ops.set_cr0(vcpu, cr0); |
a03490ed | 817 | |
d170c419 | 818 | if ((cr0 ^ old_cr0) & X86_CR0_PG) { |
e5f3f027 | 819 | kvm_clear_async_pf_completion_queue(vcpu); |
d170c419 LJ |
820 | kvm_async_pf_hash_reset(vcpu); |
821 | } | |
e5f3f027 | 822 | |
aad82703 SY |
823 | if ((cr0 ^ old_cr0) & update_bits) |
824 | kvm_mmu_reset_context(vcpu); | |
b18d5431 | 825 | |
879ae188 LE |
826 | if (((cr0 ^ old_cr0) & X86_CR0_CD) && |
827 | kvm_arch_has_noncoherent_dma(vcpu->kvm) && | |
828 | !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED)) | |
b18d5431 XG |
829 | kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL); |
830 | ||
0f12244f GN |
831 | return 0; |
832 | } | |
2d3ad1f4 | 833 | EXPORT_SYMBOL_GPL(kvm_set_cr0); |
a03490ed | 834 | |
2d3ad1f4 | 835 | void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw) |
a03490ed | 836 | { |
49a9b07e | 837 | (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f)); |
a03490ed | 838 | } |
2d3ad1f4 | 839 | EXPORT_SYMBOL_GPL(kvm_lmsw); |
a03490ed | 840 | |
139a12cf | 841 | void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 842 | { |
139a12cf AL |
843 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
844 | ||
845 | if (vcpu->arch.xcr0 != host_xcr0) | |
846 | xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0); | |
847 | ||
848 | if (vcpu->arch.xsaves_enabled && | |
849 | vcpu->arch.ia32_xss != host_xss) | |
850 | wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss); | |
851 | } | |
37486135 BM |
852 | |
853 | if (static_cpu_has(X86_FEATURE_PKU) && | |
854 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
855 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU)) && | |
856 | vcpu->arch.pkru != vcpu->arch.host_pkru) | |
857 | __write_pkru(vcpu->arch.pkru); | |
42bdf991 | 858 | } |
139a12cf | 859 | EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state); |
42bdf991 | 860 | |
139a12cf | 861 | void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu) |
42bdf991 | 862 | { |
37486135 BM |
863 | if (static_cpu_has(X86_FEATURE_PKU) && |
864 | (kvm_read_cr4_bits(vcpu, X86_CR4_PKE) || | |
865 | (vcpu->arch.xcr0 & XFEATURE_MASK_PKRU))) { | |
866 | vcpu->arch.pkru = rdpkru(); | |
867 | if (vcpu->arch.pkru != vcpu->arch.host_pkru) | |
868 | __write_pkru(vcpu->arch.host_pkru); | |
869 | } | |
870 | ||
139a12cf AL |
871 | if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) { |
872 | ||
873 | if (vcpu->arch.xcr0 != host_xcr0) | |
874 | xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0); | |
875 | ||
876 | if (vcpu->arch.xsaves_enabled && | |
877 | vcpu->arch.ia32_xss != host_xss) | |
878 | wrmsrl(MSR_IA32_XSS, host_xss); | |
879 | } | |
880 | ||
42bdf991 | 881 | } |
139a12cf | 882 | EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state); |
42bdf991 | 883 | |
69b0049a | 884 | static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) |
2acf923e | 885 | { |
56c103ec LJ |
886 | u64 xcr0 = xcr; |
887 | u64 old_xcr0 = vcpu->arch.xcr0; | |
46c34cb0 | 888 | u64 valid_bits; |
2acf923e DC |
889 | |
890 | /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */ | |
891 | if (index != XCR_XFEATURE_ENABLED_MASK) | |
892 | return 1; | |
d91cab78 | 893 | if (!(xcr0 & XFEATURE_MASK_FP)) |
2acf923e | 894 | return 1; |
d91cab78 | 895 | if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE)) |
2acf923e | 896 | return 1; |
46c34cb0 PB |
897 | |
898 | /* | |
899 | * Do not allow the guest to set bits that we do not support | |
900 | * saving. However, xcr0 bit 0 is always set, even if the | |
901 | * emulated CPU does not support XSAVE (see fx_init). | |
902 | */ | |
d91cab78 | 903 | valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP; |
46c34cb0 | 904 | if (xcr0 & ~valid_bits) |
2acf923e | 905 | return 1; |
46c34cb0 | 906 | |
d91cab78 DH |
907 | if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) != |
908 | (!(xcr0 & XFEATURE_MASK_BNDCSR))) | |
390bd528 LJ |
909 | return 1; |
910 | ||
d91cab78 DH |
911 | if (xcr0 & XFEATURE_MASK_AVX512) { |
912 | if (!(xcr0 & XFEATURE_MASK_YMM)) | |
612263b3 | 913 | return 1; |
d91cab78 | 914 | if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512) |
612263b3 CP |
915 | return 1; |
916 | } | |
2acf923e | 917 | vcpu->arch.xcr0 = xcr0; |
56c103ec | 918 | |
d91cab78 | 919 | if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND) |
56c103ec | 920 | kvm_update_cpuid(vcpu); |
2acf923e DC |
921 | return 0; |
922 | } | |
923 | ||
924 | int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr) | |
925 | { | |
afaf0b2f | 926 | if (kvm_x86_ops.get_cpl(vcpu) != 0 || |
764bcbc5 | 927 | __kvm_set_xcr(vcpu, index, xcr)) { |
2acf923e DC |
928 | kvm_inject_gp(vcpu, 0); |
929 | return 1; | |
930 | } | |
931 | return 0; | |
932 | } | |
933 | EXPORT_SYMBOL_GPL(kvm_set_xcr); | |
934 | ||
345599f9 SC |
935 | #define __cr4_reserved_bits(__cpu_has, __c) \ |
936 | ({ \ | |
937 | u64 __reserved_bits = CR4_RESERVED_BITS; \ | |
938 | \ | |
939 | if (!__cpu_has(__c, X86_FEATURE_XSAVE)) \ | |
940 | __reserved_bits |= X86_CR4_OSXSAVE; \ | |
941 | if (!__cpu_has(__c, X86_FEATURE_SMEP)) \ | |
942 | __reserved_bits |= X86_CR4_SMEP; \ | |
943 | if (!__cpu_has(__c, X86_FEATURE_SMAP)) \ | |
944 | __reserved_bits |= X86_CR4_SMAP; \ | |
945 | if (!__cpu_has(__c, X86_FEATURE_FSGSBASE)) \ | |
946 | __reserved_bits |= X86_CR4_FSGSBASE; \ | |
947 | if (!__cpu_has(__c, X86_FEATURE_PKU)) \ | |
948 | __reserved_bits |= X86_CR4_PKE; \ | |
949 | if (!__cpu_has(__c, X86_FEATURE_LA57)) \ | |
950 | __reserved_bits |= X86_CR4_LA57; \ | |
d76c7fbc SC |
951 | if (!__cpu_has(__c, X86_FEATURE_UMIP)) \ |
952 | __reserved_bits |= X86_CR4_UMIP; \ | |
345599f9 SC |
953 | __reserved_bits; \ |
954 | }) | |
a03490ed | 955 | |
3ca94192 | 956 | static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
a03490ed | 957 | { |
b11306b5 | 958 | if (cr4 & cr4_reserved_bits) |
3ca94192 | 959 | return -EINVAL; |
b9baba86 | 960 | |
345599f9 | 961 | if (cr4 & __cr4_reserved_bits(guest_cpuid_has, vcpu)) |
3ca94192 WL |
962 | return -EINVAL; |
963 | ||
964 | return 0; | |
965 | } | |
966 | ||
967 | int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
968 | { | |
969 | unsigned long old_cr4 = kvm_read_cr4(vcpu); | |
970 | unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE | | |
971 | X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE; | |
972 | ||
973 | if (kvm_valid_cr4(vcpu, cr4)) | |
ae3e61e1 PB |
974 | return 1; |
975 | ||
a03490ed | 976 | if (is_long_mode(vcpu)) { |
0f12244f GN |
977 | if (!(cr4 & X86_CR4_PAE)) |
978 | return 1; | |
a2edf57f AK |
979 | } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE) |
980 | && ((cr4 ^ old_cr4) & pdptr_bits) | |
9f8fe504 AK |
981 | && !load_pdptrs(vcpu, vcpu->arch.walk_mmu, |
982 | kvm_read_cr3(vcpu))) | |
0f12244f GN |
983 | return 1; |
984 | ||
ad756a16 | 985 | if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) { |
d6321d49 | 986 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID)) |
ad756a16 MJ |
987 | return 1; |
988 | ||
989 | /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */ | |
990 | if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu)) | |
991 | return 1; | |
992 | } | |
993 | ||
afaf0b2f | 994 | if (kvm_x86_ops.set_cr4(vcpu, cr4)) |
0f12244f | 995 | return 1; |
a03490ed | 996 | |
ad756a16 MJ |
997 | if (((cr4 ^ old_cr4) & pdptr_bits) || |
998 | (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE))) | |
aad82703 | 999 | kvm_mmu_reset_context(vcpu); |
0f12244f | 1000 | |
b9baba86 | 1001 | if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE)) |
00b27a3e | 1002 | kvm_update_cpuid(vcpu); |
2acf923e | 1003 | |
0f12244f GN |
1004 | return 0; |
1005 | } | |
2d3ad1f4 | 1006 | EXPORT_SYMBOL_GPL(kvm_set_cr4); |
a03490ed | 1007 | |
2390218b | 1008 | int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) |
a03490ed | 1009 | { |
ade61e28 | 1010 | bool skip_tlb_flush = false; |
ac146235 | 1011 | #ifdef CONFIG_X86_64 |
c19986fe JS |
1012 | bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE); |
1013 | ||
ade61e28 | 1014 | if (pcid_enabled) { |
208320ba JS |
1015 | skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH; |
1016 | cr3 &= ~X86_CR3_PCID_NOFLUSH; | |
ade61e28 | 1017 | } |
ac146235 | 1018 | #endif |
9d88fca7 | 1019 | |
9f8fe504 | 1020 | if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) { |
956bf353 JS |
1021 | if (!skip_tlb_flush) { |
1022 | kvm_mmu_sync_roots(vcpu); | |
eeeb4f67 | 1023 | kvm_make_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); |
956bf353 | 1024 | } |
0f12244f | 1025 | return 0; |
d835dfec AK |
1026 | } |
1027 | ||
d1cd3ce9 | 1028 | if (is_long_mode(vcpu) && |
a780a3ea | 1029 | (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63))) |
d1cd3ce9 | 1030 | return 1; |
bf03d4f9 PB |
1031 | else if (is_pae_paging(vcpu) && |
1032 | !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3)) | |
346874c9 | 1033 | return 1; |
a03490ed | 1034 | |
be01e8e2 | 1035 | kvm_mmu_new_pgd(vcpu, cr3, skip_tlb_flush, skip_tlb_flush); |
0f12244f | 1036 | vcpu->arch.cr3 = cr3; |
cb3c1e2f | 1037 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
7c390d35 | 1038 | |
0f12244f GN |
1039 | return 0; |
1040 | } | |
2d3ad1f4 | 1041 | EXPORT_SYMBOL_GPL(kvm_set_cr3); |
a03490ed | 1042 | |
eea1cff9 | 1043 | int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8) |
a03490ed | 1044 | { |
0f12244f GN |
1045 | if (cr8 & CR8_RESERVED_BITS) |
1046 | return 1; | |
35754c98 | 1047 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1048 | kvm_lapic_set_tpr(vcpu, cr8); |
1049 | else | |
ad312c7c | 1050 | vcpu->arch.cr8 = cr8; |
0f12244f GN |
1051 | return 0; |
1052 | } | |
2d3ad1f4 | 1053 | EXPORT_SYMBOL_GPL(kvm_set_cr8); |
a03490ed | 1054 | |
2d3ad1f4 | 1055 | unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu) |
a03490ed | 1056 | { |
35754c98 | 1057 | if (lapic_in_kernel(vcpu)) |
a03490ed CO |
1058 | return kvm_lapic_get_cr8(vcpu); |
1059 | else | |
ad312c7c | 1060 | return vcpu->arch.cr8; |
a03490ed | 1061 | } |
2d3ad1f4 | 1062 | EXPORT_SYMBOL_GPL(kvm_get_cr8); |
a03490ed | 1063 | |
ae561ede NA |
1064 | static void kvm_update_dr0123(struct kvm_vcpu *vcpu) |
1065 | { | |
1066 | int i; | |
1067 | ||
1068 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) { | |
1069 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
1070 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
1071 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD; | |
1072 | } | |
1073 | } | |
1074 | ||
7c86663b | 1075 | void kvm_update_dr7(struct kvm_vcpu *vcpu) |
c8639010 JK |
1076 | { |
1077 | unsigned long dr7; | |
1078 | ||
1079 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) | |
1080 | dr7 = vcpu->arch.guest_debug_dr7; | |
1081 | else | |
1082 | dr7 = vcpu->arch.dr7; | |
afaf0b2f | 1083 | kvm_x86_ops.set_dr7(vcpu, dr7); |
360b948d PB |
1084 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED; |
1085 | if (dr7 & DR7_BP_EN_MASK) | |
1086 | vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED; | |
c8639010 | 1087 | } |
7c86663b | 1088 | EXPORT_SYMBOL_GPL(kvm_update_dr7); |
c8639010 | 1089 | |
6f43ed01 NA |
1090 | static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu) |
1091 | { | |
1092 | u64 fixed = DR6_FIXED_1; | |
1093 | ||
d6321d49 | 1094 | if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM)) |
6f43ed01 NA |
1095 | fixed |= DR6_RTM; |
1096 | return fixed; | |
1097 | } | |
1098 | ||
338dbc97 | 1099 | static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) |
020df079 | 1100 | { |
ea740059 MP |
1101 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1102 | ||
020df079 GN |
1103 | switch (dr) { |
1104 | case 0 ... 3: | |
ea740059 | 1105 | vcpu->arch.db[array_index_nospec(dr, size)] = val; |
020df079 GN |
1106 | if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) |
1107 | vcpu->arch.eff_db[dr] = val; | |
1108 | break; | |
1109 | case 4: | |
020df079 GN |
1110 | /* fall through */ |
1111 | case 6: | |
338dbc97 GN |
1112 | if (val & 0xffffffff00000000ULL) |
1113 | return -1; /* #GP */ | |
6f43ed01 | 1114 | vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu); |
020df079 GN |
1115 | break; |
1116 | case 5: | |
020df079 GN |
1117 | /* fall through */ |
1118 | default: /* 7 */ | |
b91991bf | 1119 | if (!kvm_dr7_valid(val)) |
338dbc97 | 1120 | return -1; /* #GP */ |
020df079 | 1121 | vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1; |
c8639010 | 1122 | kvm_update_dr7(vcpu); |
020df079 GN |
1123 | break; |
1124 | } | |
1125 | ||
1126 | return 0; | |
1127 | } | |
338dbc97 GN |
1128 | |
1129 | int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val) | |
1130 | { | |
16f8a6f9 | 1131 | if (__kvm_set_dr(vcpu, dr, val)) { |
338dbc97 | 1132 | kvm_inject_gp(vcpu, 0); |
16f8a6f9 NA |
1133 | return 1; |
1134 | } | |
1135 | return 0; | |
338dbc97 | 1136 | } |
020df079 GN |
1137 | EXPORT_SYMBOL_GPL(kvm_set_dr); |
1138 | ||
16f8a6f9 | 1139 | int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val) |
020df079 | 1140 | { |
ea740059 MP |
1141 | size_t size = ARRAY_SIZE(vcpu->arch.db); |
1142 | ||
020df079 GN |
1143 | switch (dr) { |
1144 | case 0 ... 3: | |
ea740059 | 1145 | *val = vcpu->arch.db[array_index_nospec(dr, size)]; |
020df079 GN |
1146 | break; |
1147 | case 4: | |
020df079 GN |
1148 | /* fall through */ |
1149 | case 6: | |
5679b803 | 1150 | *val = vcpu->arch.dr6; |
020df079 GN |
1151 | break; |
1152 | case 5: | |
020df079 GN |
1153 | /* fall through */ |
1154 | default: /* 7 */ | |
1155 | *val = vcpu->arch.dr7; | |
1156 | break; | |
1157 | } | |
338dbc97 GN |
1158 | return 0; |
1159 | } | |
020df079 GN |
1160 | EXPORT_SYMBOL_GPL(kvm_get_dr); |
1161 | ||
022cd0e8 AK |
1162 | bool kvm_rdpmc(struct kvm_vcpu *vcpu) |
1163 | { | |
de3cd117 | 1164 | u32 ecx = kvm_rcx_read(vcpu); |
022cd0e8 AK |
1165 | u64 data; |
1166 | int err; | |
1167 | ||
c6702c9d | 1168 | err = kvm_pmu_rdpmc(vcpu, ecx, &data); |
022cd0e8 AK |
1169 | if (err) |
1170 | return err; | |
de3cd117 SC |
1171 | kvm_rax_write(vcpu, (u32)data); |
1172 | kvm_rdx_write(vcpu, data >> 32); | |
022cd0e8 AK |
1173 | return err; |
1174 | } | |
1175 | EXPORT_SYMBOL_GPL(kvm_rdpmc); | |
1176 | ||
043405e1 CO |
1177 | /* |
1178 | * List of msr numbers which we expose to userspace through KVM_GET_MSRS | |
1179 | * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST. | |
1180 | * | |
7a5ee6ed CQ |
1181 | * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features) |
1182 | * extract the supported MSRs from the related const lists. | |
1183 | * msrs_to_save is selected from the msrs_to_save_all to reflect the | |
e3267cbb | 1184 | * capabilities of the host cpu. This capabilities test skips MSRs that are |
7a5ee6ed | 1185 | * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs |
62ef68bb | 1186 | * may depend on host virtualization features rather than host cpu features. |
043405e1 | 1187 | */ |
e3267cbb | 1188 | |
7a5ee6ed | 1189 | static const u32 msrs_to_save_all[] = { |
043405e1 | 1190 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, |
8c06585d | 1191 | MSR_STAR, |
043405e1 CO |
1192 | #ifdef CONFIG_X86_64 |
1193 | MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR, | |
1194 | #endif | |
b3897a49 | 1195 | MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA, |
32ad73db | 1196 | MSR_IA32_FEAT_CTL, MSR_IA32_BNDCFGS, MSR_TSC_AUX, |
2bdb76c0 | 1197 | MSR_IA32_SPEC_CTRL, |
bf8c55d8 CP |
1198 | MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH, |
1199 | MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK, | |
1200 | MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B, | |
1201 | MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B, | |
1202 | MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B, | |
1203 | MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B, | |
6e3ba4ab TX |
1204 | MSR_IA32_UMWAIT_CONTROL, |
1205 | ||
e2ada66e JM |
1206 | MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1, |
1207 | MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3, | |
1208 | MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS, | |
1209 | MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL, | |
1210 | MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1, | |
1211 | MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3, | |
1212 | MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5, | |
1213 | MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7, | |
1214 | MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9, | |
1215 | MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11, | |
1216 | MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13, | |
1217 | MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15, | |
1218 | MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17, | |
e2ada66e JM |
1219 | MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1, |
1220 | MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3, | |
1221 | MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5, | |
1222 | MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7, | |
1223 | MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9, | |
1224 | MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11, | |
1225 | MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13, | |
1226 | MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15, | |
1227 | MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17, | |
043405e1 CO |
1228 | }; |
1229 | ||
7a5ee6ed | 1230 | static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)]; |
043405e1 CO |
1231 | static unsigned num_msrs_to_save; |
1232 | ||
7a5ee6ed | 1233 | static const u32 emulated_msrs_all[] = { |
62ef68bb PB |
1234 | MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK, |
1235 | MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW, | |
1236 | HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL, | |
1237 | HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC, | |
72c139ba | 1238 | HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY, |
e7d9513b AS |
1239 | HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2, |
1240 | HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL, | |
e516cebb | 1241 | HV_X64_MSR_RESET, |
11c4b1ca | 1242 | HV_X64_MSR_VP_INDEX, |
9eec50b8 | 1243 | HV_X64_MSR_VP_RUNTIME, |
5c919412 | 1244 | HV_X64_MSR_SCONTROL, |
1f4b34f8 | 1245 | HV_X64_MSR_STIMER0_CONFIG, |
d4abc577 | 1246 | HV_X64_MSR_VP_ASSIST_PAGE, |
a2e164e7 VK |
1247 | HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL, |
1248 | HV_X64_MSR_TSC_EMULATION_STATUS, | |
f97f5a56 JD |
1249 | HV_X64_MSR_SYNDBG_OPTIONS, |
1250 | HV_X64_MSR_SYNDBG_CONTROL, HV_X64_MSR_SYNDBG_STATUS, | |
1251 | HV_X64_MSR_SYNDBG_SEND_BUFFER, HV_X64_MSR_SYNDBG_RECV_BUFFER, | |
1252 | HV_X64_MSR_SYNDBG_PENDING_BUFFER, | |
a2e164e7 VK |
1253 | |
1254 | MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME, | |
557a961a | 1255 | MSR_KVM_PV_EOI_EN, MSR_KVM_ASYNC_PF_INT, MSR_KVM_ASYNC_PF_ACK, |
62ef68bb | 1256 | |
ba904635 | 1257 | MSR_IA32_TSC_ADJUST, |
a3e06bbe | 1258 | MSR_IA32_TSCDEADLINE, |
2bdb76c0 | 1259 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1260 | MSR_IA32_PERF_CAPABILITIES, |
043405e1 | 1261 | MSR_IA32_MISC_ENABLE, |
908e75f3 AK |
1262 | MSR_IA32_MCG_STATUS, |
1263 | MSR_IA32_MCG_CTL, | |
c45dcc71 | 1264 | MSR_IA32_MCG_EXT_CTL, |
64d60670 | 1265 | MSR_IA32_SMBASE, |
52797bf9 | 1266 | MSR_SMI_COUNT, |
db2336a8 KH |
1267 | MSR_PLATFORM_INFO, |
1268 | MSR_MISC_FEATURES_ENABLES, | |
bc226f07 | 1269 | MSR_AMD64_VIRT_SPEC_CTRL, |
6c6a2ab9 | 1270 | MSR_IA32_POWER_CTL, |
99634e3e | 1271 | MSR_IA32_UCODE_REV, |
191c8137 | 1272 | |
95c5c7c7 PB |
1273 | /* |
1274 | * The following list leaves out MSRs whose values are determined | |
1275 | * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs. | |
1276 | * We always support the "true" VMX control MSRs, even if the host | |
1277 | * processor does not, so I am putting these registers here rather | |
7a5ee6ed | 1278 | * than in msrs_to_save_all. |
95c5c7c7 PB |
1279 | */ |
1280 | MSR_IA32_VMX_BASIC, | |
1281 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1282 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1283 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1284 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1285 | MSR_IA32_VMX_MISC, | |
1286 | MSR_IA32_VMX_CR0_FIXED0, | |
1287 | MSR_IA32_VMX_CR4_FIXED0, | |
1288 | MSR_IA32_VMX_VMCS_ENUM, | |
1289 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1290 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1291 | MSR_IA32_VMX_VMFUNC, | |
1292 | ||
191c8137 | 1293 | MSR_K7_HWCR, |
2d5ba19b | 1294 | MSR_KVM_POLL_CONTROL, |
043405e1 CO |
1295 | }; |
1296 | ||
7a5ee6ed | 1297 | static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)]; |
62ef68bb PB |
1298 | static unsigned num_emulated_msrs; |
1299 | ||
801e459a TL |
1300 | /* |
1301 | * List of msr numbers which are used to expose MSR-based features that | |
1302 | * can be used by a hypervisor to validate requested CPU features. | |
1303 | */ | |
7a5ee6ed | 1304 | static const u32 msr_based_features_all[] = { |
1389309c PB |
1305 | MSR_IA32_VMX_BASIC, |
1306 | MSR_IA32_VMX_TRUE_PINBASED_CTLS, | |
1307 | MSR_IA32_VMX_PINBASED_CTLS, | |
1308 | MSR_IA32_VMX_TRUE_PROCBASED_CTLS, | |
1309 | MSR_IA32_VMX_PROCBASED_CTLS, | |
1310 | MSR_IA32_VMX_TRUE_EXIT_CTLS, | |
1311 | MSR_IA32_VMX_EXIT_CTLS, | |
1312 | MSR_IA32_VMX_TRUE_ENTRY_CTLS, | |
1313 | MSR_IA32_VMX_ENTRY_CTLS, | |
1314 | MSR_IA32_VMX_MISC, | |
1315 | MSR_IA32_VMX_CR0_FIXED0, | |
1316 | MSR_IA32_VMX_CR0_FIXED1, | |
1317 | MSR_IA32_VMX_CR4_FIXED0, | |
1318 | MSR_IA32_VMX_CR4_FIXED1, | |
1319 | MSR_IA32_VMX_VMCS_ENUM, | |
1320 | MSR_IA32_VMX_PROCBASED_CTLS2, | |
1321 | MSR_IA32_VMX_EPT_VPID_CAP, | |
1322 | MSR_IA32_VMX_VMFUNC, | |
1323 | ||
d1d93fa9 | 1324 | MSR_F10H_DECFG, |
518e7b94 | 1325 | MSR_IA32_UCODE_REV, |
cd283252 | 1326 | MSR_IA32_ARCH_CAPABILITIES, |
27461da3 | 1327 | MSR_IA32_PERF_CAPABILITIES, |
801e459a TL |
1328 | }; |
1329 | ||
7a5ee6ed | 1330 | static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)]; |
801e459a TL |
1331 | static unsigned int num_msr_based_features; |
1332 | ||
4d22c17c | 1333 | static u64 kvm_get_arch_capabilities(void) |
5b76a3cf | 1334 | { |
4d22c17c | 1335 | u64 data = 0; |
5b76a3cf | 1336 | |
4d22c17c XL |
1337 | if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES)) |
1338 | rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data); | |
5b76a3cf | 1339 | |
b8e8c830 PB |
1340 | /* |
1341 | * If nx_huge_pages is enabled, KVM's shadow paging will ensure that | |
1342 | * the nested hypervisor runs with NX huge pages. If it is not, | |
1343 | * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other | |
1344 | * L1 guests, so it need not worry about its own (L2) guests. | |
1345 | */ | |
1346 | data |= ARCH_CAP_PSCHANGE_MC_NO; | |
1347 | ||
5b76a3cf PB |
1348 | /* |
1349 | * If we're doing cache flushes (either "always" or "cond") | |
1350 | * we will do one whenever the guest does a vmlaunch/vmresume. | |
1351 | * If an outer hypervisor is doing the cache flush for us | |
1352 | * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that | |
1353 | * capability to the guest too, and if EPT is disabled we're not | |
1354 | * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will | |
1355 | * require a nested hypervisor to do a flush of its own. | |
1356 | */ | |
1357 | if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER) | |
1358 | data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH; | |
1359 | ||
0c54914d PB |
1360 | if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN)) |
1361 | data |= ARCH_CAP_RDCL_NO; | |
1362 | if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS)) | |
1363 | data |= ARCH_CAP_SSB_NO; | |
1364 | if (!boot_cpu_has_bug(X86_BUG_MDS)) | |
1365 | data |= ARCH_CAP_MDS_NO; | |
1366 | ||
e1d38b63 | 1367 | /* |
c11f83e0 PB |
1368 | * On TAA affected systems: |
1369 | * - nothing to do if TSX is disabled on the host. | |
1370 | * - we emulate TSX_CTRL if present on the host. | |
1371 | * This lets the guest use VERW to clear CPU buffers. | |
e1d38b63 | 1372 | */ |
cbbaa272 | 1373 | if (!boot_cpu_has(X86_FEATURE_RTM)) |
c11f83e0 | 1374 | data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR); |
cbbaa272 PB |
1375 | else if (!boot_cpu_has_bug(X86_BUG_TAA)) |
1376 | data |= ARCH_CAP_TAA_NO; | |
e1d38b63 | 1377 | |
5b76a3cf PB |
1378 | return data; |
1379 | } | |
5b76a3cf | 1380 | |
66421c1e WL |
1381 | static int kvm_get_msr_feature(struct kvm_msr_entry *msr) |
1382 | { | |
1383 | switch (msr->index) { | |
cd283252 | 1384 | case MSR_IA32_ARCH_CAPABILITIES: |
5b76a3cf PB |
1385 | msr->data = kvm_get_arch_capabilities(); |
1386 | break; | |
1387 | case MSR_IA32_UCODE_REV: | |
cd283252 | 1388 | rdmsrl_safe(msr->index, &msr->data); |
518e7b94 | 1389 | break; |
66421c1e | 1390 | default: |
afaf0b2f | 1391 | if (kvm_x86_ops.get_msr_feature(msr)) |
66421c1e WL |
1392 | return 1; |
1393 | } | |
1394 | return 0; | |
1395 | } | |
1396 | ||
801e459a TL |
1397 | static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data) |
1398 | { | |
1399 | struct kvm_msr_entry msr; | |
66421c1e | 1400 | int r; |
801e459a TL |
1401 | |
1402 | msr.index = index; | |
66421c1e WL |
1403 | r = kvm_get_msr_feature(&msr); |
1404 | if (r) | |
1405 | return r; | |
801e459a TL |
1406 | |
1407 | *data = msr.data; | |
1408 | ||
1409 | return 0; | |
1410 | } | |
1411 | ||
11988499 | 1412 | static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) |
15c4a640 | 1413 | { |
1b4d56b8 | 1414 | if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT)) |
11988499 | 1415 | return false; |
1b2fd70c | 1416 | |
1b4d56b8 | 1417 | if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM)) |
11988499 | 1418 | return false; |
d8017474 | 1419 | |
0a629563 SC |
1420 | if (efer & (EFER_LME | EFER_LMA) && |
1421 | !guest_cpuid_has(vcpu, X86_FEATURE_LM)) | |
1422 | return false; | |
1423 | ||
1424 | if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX)) | |
1425 | return false; | |
d8017474 | 1426 | |
384bb783 | 1427 | return true; |
11988499 SC |
1428 | |
1429 | } | |
1430 | bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer) | |
1431 | { | |
1432 | if (efer & efer_reserved_bits) | |
1433 | return false; | |
1434 | ||
1435 | return __kvm_valid_efer(vcpu, efer); | |
384bb783 JK |
1436 | } |
1437 | EXPORT_SYMBOL_GPL(kvm_valid_efer); | |
1438 | ||
11988499 | 1439 | static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
384bb783 JK |
1440 | { |
1441 | u64 old_efer = vcpu->arch.efer; | |
11988499 | 1442 | u64 efer = msr_info->data; |
384bb783 | 1443 | |
11988499 | 1444 | if (efer & efer_reserved_bits) |
66f61c92 | 1445 | return 1; |
384bb783 | 1446 | |
11988499 SC |
1447 | if (!msr_info->host_initiated) { |
1448 | if (!__kvm_valid_efer(vcpu, efer)) | |
1449 | return 1; | |
1450 | ||
1451 | if (is_paging(vcpu) && | |
1452 | (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME)) | |
1453 | return 1; | |
1454 | } | |
384bb783 | 1455 | |
15c4a640 | 1456 | efer &= ~EFER_LMA; |
f6801dff | 1457 | efer |= vcpu->arch.efer & EFER_LMA; |
15c4a640 | 1458 | |
afaf0b2f | 1459 | kvm_x86_ops.set_efer(vcpu, efer); |
a3d204e2 | 1460 | |
aad82703 SY |
1461 | /* Update reserved bits */ |
1462 | if ((efer ^ old_efer) & EFER_NX) | |
1463 | kvm_mmu_reset_context(vcpu); | |
1464 | ||
b69e8cae | 1465 | return 0; |
15c4a640 CO |
1466 | } |
1467 | ||
f2b4b7dd JR |
1468 | void kvm_enable_efer_bits(u64 mask) |
1469 | { | |
1470 | efer_reserved_bits &= ~mask; | |
1471 | } | |
1472 | EXPORT_SYMBOL_GPL(kvm_enable_efer_bits); | |
1473 | ||
15c4a640 | 1474 | /* |
f20935d8 SC |
1475 | * Write @data into the MSR specified by @index. Select MSR specific fault |
1476 | * checks are bypassed if @host_initiated is %true. | |
15c4a640 CO |
1477 | * Returns 0 on success, non-0 otherwise. |
1478 | * Assumes vcpu_load() was already called. | |
1479 | */ | |
f20935d8 SC |
1480 | static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data, |
1481 | bool host_initiated) | |
15c4a640 | 1482 | { |
f20935d8 SC |
1483 | struct msr_data msr; |
1484 | ||
1485 | switch (index) { | |
854e8bb1 NA |
1486 | case MSR_FS_BASE: |
1487 | case MSR_GS_BASE: | |
1488 | case MSR_KERNEL_GS_BASE: | |
1489 | case MSR_CSTAR: | |
1490 | case MSR_LSTAR: | |
f20935d8 | 1491 | if (is_noncanonical_address(data, vcpu)) |
854e8bb1 NA |
1492 | return 1; |
1493 | break; | |
1494 | case MSR_IA32_SYSENTER_EIP: | |
1495 | case MSR_IA32_SYSENTER_ESP: | |
1496 | /* | |
1497 | * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if | |
1498 | * non-canonical address is written on Intel but not on | |
1499 | * AMD (which ignores the top 32-bits, because it does | |
1500 | * not implement 64-bit SYSENTER). | |
1501 | * | |
1502 | * 64-bit code should hence be able to write a non-canonical | |
1503 | * value on AMD. Making the address canonical ensures that | |
1504 | * vmentry does not fail on Intel after writing a non-canonical | |
1505 | * value, and that something deterministic happens if the guest | |
1506 | * invokes 64-bit SYSENTER. | |
1507 | */ | |
f20935d8 | 1508 | data = get_canonical(data, vcpu_virt_addr_bits(vcpu)); |
854e8bb1 | 1509 | } |
f20935d8 SC |
1510 | |
1511 | msr.data = data; | |
1512 | msr.index = index; | |
1513 | msr.host_initiated = host_initiated; | |
1514 | ||
afaf0b2f | 1515 | return kvm_x86_ops.set_msr(vcpu, &msr); |
15c4a640 CO |
1516 | } |
1517 | ||
313a3dc7 | 1518 | /* |
f20935d8 SC |
1519 | * Read the MSR specified by @index into @data. Select MSR specific fault |
1520 | * checks are bypassed if @host_initiated is %true. | |
1521 | * Returns 0 on success, non-0 otherwise. | |
1522 | * Assumes vcpu_load() was already called. | |
313a3dc7 | 1523 | */ |
edef5c36 PB |
1524 | int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data, |
1525 | bool host_initiated) | |
609e36d3 PB |
1526 | { |
1527 | struct msr_data msr; | |
f20935d8 | 1528 | int ret; |
609e36d3 PB |
1529 | |
1530 | msr.index = index; | |
f20935d8 | 1531 | msr.host_initiated = host_initiated; |
609e36d3 | 1532 | |
afaf0b2f | 1533 | ret = kvm_x86_ops.get_msr(vcpu, &msr); |
f20935d8 SC |
1534 | if (!ret) |
1535 | *data = msr.data; | |
1536 | return ret; | |
609e36d3 PB |
1537 | } |
1538 | ||
f20935d8 | 1539 | int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data) |
313a3dc7 | 1540 | { |
f20935d8 SC |
1541 | return __kvm_get_msr(vcpu, index, data, false); |
1542 | } | |
1543 | EXPORT_SYMBOL_GPL(kvm_get_msr); | |
8fe8ab46 | 1544 | |
f20935d8 SC |
1545 | int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data) |
1546 | { | |
1547 | return __kvm_set_msr(vcpu, index, data, false); | |
1548 | } | |
1549 | EXPORT_SYMBOL_GPL(kvm_set_msr); | |
1550 | ||
1edce0a9 SC |
1551 | int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu) |
1552 | { | |
1553 | u32 ecx = kvm_rcx_read(vcpu); | |
1554 | u64 data; | |
1555 | ||
1556 | if (kvm_get_msr(vcpu, ecx, &data)) { | |
1557 | trace_kvm_msr_read_ex(ecx); | |
1558 | kvm_inject_gp(vcpu, 0); | |
1559 | return 1; | |
1560 | } | |
1561 | ||
1562 | trace_kvm_msr_read(ecx, data); | |
1563 | ||
1564 | kvm_rax_write(vcpu, data & -1u); | |
1565 | kvm_rdx_write(vcpu, (data >> 32) & -1u); | |
1566 | return kvm_skip_emulated_instruction(vcpu); | |
1567 | } | |
1568 | EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr); | |
1569 | ||
1570 | int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu) | |
1571 | { | |
1572 | u32 ecx = kvm_rcx_read(vcpu); | |
1573 | u64 data = kvm_read_edx_eax(vcpu); | |
1574 | ||
1575 | if (kvm_set_msr(vcpu, ecx, data)) { | |
1576 | trace_kvm_msr_write_ex(ecx, data); | |
1577 | kvm_inject_gp(vcpu, 0); | |
1578 | return 1; | |
1579 | } | |
1580 | ||
1581 | trace_kvm_msr_write(ecx, data); | |
1582 | return kvm_skip_emulated_instruction(vcpu); | |
1583 | } | |
1584 | EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr); | |
1585 | ||
5a9f5443 WL |
1586 | bool kvm_vcpu_exit_request(struct kvm_vcpu *vcpu) |
1587 | { | |
1588 | return vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu) || | |
1589 | need_resched() || signal_pending(current); | |
1590 | } | |
1591 | EXPORT_SYMBOL_GPL(kvm_vcpu_exit_request); | |
1592 | ||
1e9e2622 WL |
1593 | /* |
1594 | * The fast path for frequent and performance sensitive wrmsr emulation, | |
1595 | * i.e. the sending of IPI, sending IPI early in the VM-Exit flow reduces | |
1596 | * the latency of virtual IPI by avoiding the expensive bits of transitioning | |
1597 | * from guest to host, e.g. reacquiring KVM's SRCU lock. In contrast to the | |
1598 | * other cases which must be called after interrupts are enabled on the host. | |
1599 | */ | |
1600 | static int handle_fastpath_set_x2apic_icr_irqoff(struct kvm_vcpu *vcpu, u64 data) | |
1601 | { | |
e1be9ac8 WL |
1602 | if (!lapic_in_kernel(vcpu) || !apic_x2apic_mode(vcpu->arch.apic)) |
1603 | return 1; | |
1604 | ||
1605 | if (((data & APIC_SHORT_MASK) == APIC_DEST_NOSHORT) && | |
1e9e2622 | 1606 | ((data & APIC_DEST_MASK) == APIC_DEST_PHYSICAL) && |
4064a4c6 WL |
1607 | ((data & APIC_MODE_MASK) == APIC_DM_FIXED) && |
1608 | ((u32)(data >> 32) != X2APIC_BROADCAST)) { | |
1e9e2622 | 1609 | |
d5361678 WL |
1610 | data &= ~(1 << 12); |
1611 | kvm_apic_send_ipi(vcpu->arch.apic, (u32)data, (u32)(data >> 32)); | |
1e9e2622 | 1612 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR2, (u32)(data >> 32)); |
d5361678 WL |
1613 | kvm_lapic_set_reg(vcpu->arch.apic, APIC_ICR, (u32)data); |
1614 | trace_kvm_apic_write(APIC_ICR, (u32)data); | |
1615 | return 0; | |
1e9e2622 WL |
1616 | } |
1617 | ||
1618 | return 1; | |
1619 | } | |
1620 | ||
ae95f566 WL |
1621 | static int handle_fastpath_set_tscdeadline(struct kvm_vcpu *vcpu, u64 data) |
1622 | { | |
1623 | if (!kvm_can_use_hv_timer(vcpu)) | |
1624 | return 1; | |
1625 | ||
1626 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
1627 | return 0; | |
1628 | } | |
1629 | ||
404d5d7b | 1630 | fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu) |
1e9e2622 WL |
1631 | { |
1632 | u32 msr = kvm_rcx_read(vcpu); | |
8a1038de | 1633 | u64 data; |
404d5d7b | 1634 | fastpath_t ret = EXIT_FASTPATH_NONE; |
1e9e2622 WL |
1635 | |
1636 | switch (msr) { | |
1637 | case APIC_BASE_MSR + (APIC_ICR >> 4): | |
8a1038de | 1638 | data = kvm_read_edx_eax(vcpu); |
404d5d7b WL |
1639 | if (!handle_fastpath_set_x2apic_icr_irqoff(vcpu, data)) { |
1640 | kvm_skip_emulated_instruction(vcpu); | |
1641 | ret = EXIT_FASTPATH_EXIT_HANDLED; | |
80bc97f2 | 1642 | } |
1e9e2622 | 1643 | break; |
ae95f566 WL |
1644 | case MSR_IA32_TSCDEADLINE: |
1645 | data = kvm_read_edx_eax(vcpu); | |
1646 | if (!handle_fastpath_set_tscdeadline(vcpu, data)) { | |
1647 | kvm_skip_emulated_instruction(vcpu); | |
1648 | ret = EXIT_FASTPATH_REENTER_GUEST; | |
1649 | } | |
1650 | break; | |
1e9e2622 | 1651 | default: |
404d5d7b | 1652 | break; |
1e9e2622 WL |
1653 | } |
1654 | ||
404d5d7b | 1655 | if (ret != EXIT_FASTPATH_NONE) |
1e9e2622 | 1656 | trace_kvm_msr_write(msr, data); |
1e9e2622 | 1657 | |
404d5d7b | 1658 | return ret; |
1e9e2622 WL |
1659 | } |
1660 | EXPORT_SYMBOL_GPL(handle_fastpath_set_msr_irqoff); | |
1661 | ||
f20935d8 SC |
1662 | /* |
1663 | * Adapt set_msr() to msr_io()'s calling convention | |
1664 | */ | |
1665 | static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1666 | { | |
1667 | return __kvm_get_msr(vcpu, index, data, true); | |
1668 | } | |
1669 | ||
1670 | static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data) | |
1671 | { | |
1672 | return __kvm_set_msr(vcpu, index, *data, true); | |
313a3dc7 CO |
1673 | } |
1674 | ||
16e8d74d | 1675 | #ifdef CONFIG_X86_64 |
53fafdbb MT |
1676 | struct pvclock_clock { |
1677 | int vclock_mode; | |
1678 | u64 cycle_last; | |
1679 | u64 mask; | |
1680 | u32 mult; | |
1681 | u32 shift; | |
917f9475 PB |
1682 | u64 base_cycles; |
1683 | u64 offset; | |
53fafdbb MT |
1684 | }; |
1685 | ||
16e8d74d MT |
1686 | struct pvclock_gtod_data { |
1687 | seqcount_t seq; | |
1688 | ||
53fafdbb MT |
1689 | struct pvclock_clock clock; /* extract of a clocksource struct */ |
1690 | struct pvclock_clock raw_clock; /* extract of a clocksource struct */ | |
16e8d74d | 1691 | |
917f9475 | 1692 | ktime_t offs_boot; |
55dd00a7 | 1693 | u64 wall_time_sec; |
16e8d74d MT |
1694 | }; |
1695 | ||
1696 | static struct pvclock_gtod_data pvclock_gtod_data; | |
1697 | ||
1698 | static void update_pvclock_gtod(struct timekeeper *tk) | |
1699 | { | |
1700 | struct pvclock_gtod_data *vdata = &pvclock_gtod_data; | |
1701 | ||
1702 | write_seqcount_begin(&vdata->seq); | |
1703 | ||
1704 | /* copy pvclock gtod data */ | |
b95a8a27 | 1705 | vdata->clock.vclock_mode = tk->tkr_mono.clock->vdso_clock_mode; |
876e7881 PZ |
1706 | vdata->clock.cycle_last = tk->tkr_mono.cycle_last; |
1707 | vdata->clock.mask = tk->tkr_mono.mask; | |
1708 | vdata->clock.mult = tk->tkr_mono.mult; | |
1709 | vdata->clock.shift = tk->tkr_mono.shift; | |
917f9475 PB |
1710 | vdata->clock.base_cycles = tk->tkr_mono.xtime_nsec; |
1711 | vdata->clock.offset = tk->tkr_mono.base; | |
16e8d74d | 1712 | |
b95a8a27 | 1713 | vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->vdso_clock_mode; |
53fafdbb MT |
1714 | vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last; |
1715 | vdata->raw_clock.mask = tk->tkr_raw.mask; | |
1716 | vdata->raw_clock.mult = tk->tkr_raw.mult; | |
1717 | vdata->raw_clock.shift = tk->tkr_raw.shift; | |
917f9475 PB |
1718 | vdata->raw_clock.base_cycles = tk->tkr_raw.xtime_nsec; |
1719 | vdata->raw_clock.offset = tk->tkr_raw.base; | |
16e8d74d | 1720 | |
55dd00a7 MT |
1721 | vdata->wall_time_sec = tk->xtime_sec; |
1722 | ||
917f9475 | 1723 | vdata->offs_boot = tk->offs_boot; |
53fafdbb | 1724 | |
16e8d74d MT |
1725 | write_seqcount_end(&vdata->seq); |
1726 | } | |
8171cd68 PB |
1727 | |
1728 | static s64 get_kvmclock_base_ns(void) | |
1729 | { | |
1730 | /* Count up from boot time, but with the frequency of the raw clock. */ | |
1731 | return ktime_to_ns(ktime_add(ktime_get_raw(), pvclock_gtod_data.offs_boot)); | |
1732 | } | |
1733 | #else | |
1734 | static s64 get_kvmclock_base_ns(void) | |
1735 | { | |
1736 | /* Master clock not used, so we can just use CLOCK_BOOTTIME. */ | |
1737 | return ktime_get_boottime_ns(); | |
1738 | } | |
16e8d74d MT |
1739 | #endif |
1740 | ||
bab5bb39 NK |
1741 | void kvm_set_pending_timer(struct kvm_vcpu *vcpu) |
1742 | { | |
bab5bb39 | 1743 | kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu); |
4d151bf3 | 1744 | kvm_vcpu_kick(vcpu); |
bab5bb39 | 1745 | } |
16e8d74d | 1746 | |
18068523 GOC |
1747 | static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock) |
1748 | { | |
9ed3c444 AK |
1749 | int version; |
1750 | int r; | |
50d0a0f9 | 1751 | struct pvclock_wall_clock wc; |
8171cd68 | 1752 | u64 wall_nsec; |
18068523 GOC |
1753 | |
1754 | if (!wall_clock) | |
1755 | return; | |
1756 | ||
9ed3c444 AK |
1757 | r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version)); |
1758 | if (r) | |
1759 | return; | |
1760 | ||
1761 | if (version & 1) | |
1762 | ++version; /* first time write, random junk */ | |
1763 | ||
1764 | ++version; | |
18068523 | 1765 | |
1dab1345 NK |
1766 | if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version))) |
1767 | return; | |
18068523 | 1768 | |
50d0a0f9 GH |
1769 | /* |
1770 | * The guest calculates current wall clock time by adding | |
34c238a1 | 1771 | * system time (updated by kvm_guest_time_update below) to the |
8171cd68 | 1772 | * wall clock specified here. We do the reverse here. |
50d0a0f9 | 1773 | */ |
8171cd68 | 1774 | wall_nsec = ktime_get_real_ns() - get_kvmclock_ns(kvm); |
50d0a0f9 | 1775 | |
8171cd68 PB |
1776 | wc.nsec = do_div(wall_nsec, 1000000000); |
1777 | wc.sec = (u32)wall_nsec; /* overflow in 2106 guest time */ | |
50d0a0f9 | 1778 | wc.version = version; |
18068523 GOC |
1779 | |
1780 | kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc)); | |
1781 | ||
1782 | version++; | |
1783 | kvm_write_guest(kvm, wall_clock, &version, sizeof(version)); | |
18068523 GOC |
1784 | } |
1785 | ||
50d0a0f9 GH |
1786 | static uint32_t div_frac(uint32_t dividend, uint32_t divisor) |
1787 | { | |
b51012de PB |
1788 | do_shl32_div32(dividend, divisor); |
1789 | return dividend; | |
50d0a0f9 GH |
1790 | } |
1791 | ||
3ae13faa | 1792 | static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz, |
5f4e3f88 | 1793 | s8 *pshift, u32 *pmultiplier) |
50d0a0f9 | 1794 | { |
5f4e3f88 | 1795 | uint64_t scaled64; |
50d0a0f9 GH |
1796 | int32_t shift = 0; |
1797 | uint64_t tps64; | |
1798 | uint32_t tps32; | |
1799 | ||
3ae13faa PB |
1800 | tps64 = base_hz; |
1801 | scaled64 = scaled_hz; | |
50933623 | 1802 | while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) { |
50d0a0f9 GH |
1803 | tps64 >>= 1; |
1804 | shift--; | |
1805 | } | |
1806 | ||
1807 | tps32 = (uint32_t)tps64; | |
50933623 JK |
1808 | while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) { |
1809 | if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000) | |
5f4e3f88 ZA |
1810 | scaled64 >>= 1; |
1811 | else | |
1812 | tps32 <<= 1; | |
50d0a0f9 GH |
1813 | shift++; |
1814 | } | |
1815 | ||
5f4e3f88 ZA |
1816 | *pshift = shift; |
1817 | *pmultiplier = div_frac(scaled64, tps32); | |
50d0a0f9 GH |
1818 | } |
1819 | ||
d828199e | 1820 | #ifdef CONFIG_X86_64 |
16e8d74d | 1821 | static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0); |
d828199e | 1822 | #endif |
16e8d74d | 1823 | |
c8076604 | 1824 | static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz); |
69b0049a | 1825 | static unsigned long max_tsc_khz; |
c8076604 | 1826 | |
cc578287 | 1827 | static u32 adjust_tsc_khz(u32 khz, s32 ppm) |
1e993611 | 1828 | { |
cc578287 ZA |
1829 | u64 v = (u64)khz * (1000000 + ppm); |
1830 | do_div(v, 1000000); | |
1831 | return v; | |
1e993611 JR |
1832 | } |
1833 | ||
381d585c HZ |
1834 | static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
1835 | { | |
1836 | u64 ratio; | |
1837 | ||
1838 | /* Guest TSC same frequency as host TSC? */ | |
1839 | if (!scale) { | |
1840 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; | |
1841 | return 0; | |
1842 | } | |
1843 | ||
1844 | /* TSC scaling supported? */ | |
1845 | if (!kvm_has_tsc_control) { | |
1846 | if (user_tsc_khz > tsc_khz) { | |
1847 | vcpu->arch.tsc_catchup = 1; | |
1848 | vcpu->arch.tsc_always_catchup = 1; | |
1849 | return 0; | |
1850 | } else { | |
3f16a5c3 | 1851 | pr_warn_ratelimited("user requested TSC rate below hardware speed\n"); |
381d585c HZ |
1852 | return -1; |
1853 | } | |
1854 | } | |
1855 | ||
1856 | /* TSC scaling required - calculate ratio */ | |
1857 | ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits, | |
1858 | user_tsc_khz, tsc_khz); | |
1859 | ||
1860 | if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) { | |
3f16a5c3 PB |
1861 | pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n", |
1862 | user_tsc_khz); | |
381d585c HZ |
1863 | return -1; |
1864 | } | |
1865 | ||
1866 | vcpu->arch.tsc_scaling_ratio = ratio; | |
1867 | return 0; | |
1868 | } | |
1869 | ||
4941b8cb | 1870 | static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz) |
759379dd | 1871 | { |
cc578287 ZA |
1872 | u32 thresh_lo, thresh_hi; |
1873 | int use_scaling = 0; | |
217fc9cf | 1874 | |
03ba32ca | 1875 | /* tsc_khz can be zero if TSC calibration fails */ |
4941b8cb | 1876 | if (user_tsc_khz == 0) { |
ad721883 HZ |
1877 | /* set tsc_scaling_ratio to a safe value */ |
1878 | vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio; | |
381d585c | 1879 | return -1; |
ad721883 | 1880 | } |
03ba32ca | 1881 | |
c285545f | 1882 | /* Compute a scale to convert nanoseconds in TSC cycles */ |
3ae13faa | 1883 | kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC, |
cc578287 ZA |
1884 | &vcpu->arch.virtual_tsc_shift, |
1885 | &vcpu->arch.virtual_tsc_mult); | |
4941b8cb | 1886 | vcpu->arch.virtual_tsc_khz = user_tsc_khz; |
cc578287 ZA |
1887 | |
1888 | /* | |
1889 | * Compute the variation in TSC rate which is acceptable | |
1890 | * within the range of tolerance and decide if the | |
1891 | * rate being applied is within that bounds of the hardware | |
1892 | * rate. If so, no scaling or compensation need be done. | |
1893 | */ | |
1894 | thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm); | |
1895 | thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm); | |
4941b8cb PB |
1896 | if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) { |
1897 | pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi); | |
cc578287 ZA |
1898 | use_scaling = 1; |
1899 | } | |
4941b8cb | 1900 | return set_tsc_khz(vcpu, user_tsc_khz, use_scaling); |
c285545f ZA |
1901 | } |
1902 | ||
1903 | static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns) | |
1904 | { | |
e26101b1 | 1905 | u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec, |
cc578287 ZA |
1906 | vcpu->arch.virtual_tsc_mult, |
1907 | vcpu->arch.virtual_tsc_shift); | |
e26101b1 | 1908 | tsc += vcpu->arch.this_tsc_write; |
c285545f ZA |
1909 | return tsc; |
1910 | } | |
1911 | ||
b0c39dc6 VK |
1912 | static inline int gtod_is_based_on_tsc(int mode) |
1913 | { | |
b95a8a27 | 1914 | return mode == VDSO_CLOCKMODE_TSC || mode == VDSO_CLOCKMODE_HVCLOCK; |
b0c39dc6 VK |
1915 | } |
1916 | ||
69b0049a | 1917 | static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu) |
b48aa97e MT |
1918 | { |
1919 | #ifdef CONFIG_X86_64 | |
1920 | bool vcpus_matched; | |
b48aa97e MT |
1921 | struct kvm_arch *ka = &vcpu->kvm->arch; |
1922 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
1923 | ||
1924 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
1925 | atomic_read(&vcpu->kvm->online_vcpus)); | |
1926 | ||
7f187922 MT |
1927 | /* |
1928 | * Once the masterclock is enabled, always perform request in | |
1929 | * order to update it. | |
1930 | * | |
1931 | * In order to enable masterclock, the host clocksource must be TSC | |
1932 | * and the vcpus need to have matched TSCs. When that happens, | |
1933 | * perform request to enable masterclock. | |
1934 | */ | |
1935 | if (ka->use_master_clock || | |
b0c39dc6 | 1936 | (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched)) |
b48aa97e MT |
1937 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
1938 | ||
1939 | trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc, | |
1940 | atomic_read(&vcpu->kvm->online_vcpus), | |
1941 | ka->use_master_clock, gtod->clock.vclock_mode); | |
1942 | #endif | |
1943 | } | |
1944 | ||
ba904635 WA |
1945 | static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset) |
1946 | { | |
56ba77a4 | 1947 | u64 curr_offset = vcpu->arch.l1_tsc_offset; |
ba904635 WA |
1948 | vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset; |
1949 | } | |
1950 | ||
35181e86 HZ |
1951 | /* |
1952 | * Multiply tsc by a fixed point number represented by ratio. | |
1953 | * | |
1954 | * The most significant 64-N bits (mult) of ratio represent the | |
1955 | * integral part of the fixed point number; the remaining N bits | |
1956 | * (frac) represent the fractional part, ie. ratio represents a fixed | |
1957 | * point number (mult + frac * 2^(-N)). | |
1958 | * | |
1959 | * N equals to kvm_tsc_scaling_ratio_frac_bits. | |
1960 | */ | |
1961 | static inline u64 __scale_tsc(u64 ratio, u64 tsc) | |
1962 | { | |
1963 | return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits); | |
1964 | } | |
1965 | ||
1966 | u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) | |
1967 | { | |
1968 | u64 _tsc = tsc; | |
1969 | u64 ratio = vcpu->arch.tsc_scaling_ratio; | |
1970 | ||
1971 | if (ratio != kvm_default_tsc_scaling_ratio) | |
1972 | _tsc = __scale_tsc(ratio, tsc); | |
1973 | ||
1974 | return _tsc; | |
1975 | } | |
1976 | EXPORT_SYMBOL_GPL(kvm_scale_tsc); | |
1977 | ||
07c1419a HZ |
1978 | static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
1979 | { | |
1980 | u64 tsc; | |
1981 | ||
1982 | tsc = kvm_scale_tsc(vcpu, rdtsc()); | |
1983 | ||
1984 | return target_tsc - tsc; | |
1985 | } | |
1986 | ||
4ba76538 HZ |
1987 | u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc) |
1988 | { | |
56ba77a4 | 1989 | return vcpu->arch.l1_tsc_offset + kvm_scale_tsc(vcpu, host_tsc); |
4ba76538 HZ |
1990 | } |
1991 | EXPORT_SYMBOL_GPL(kvm_read_l1_tsc); | |
1992 | ||
a545ab6a LC |
1993 | static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
1994 | { | |
56ba77a4 | 1995 | vcpu->arch.l1_tsc_offset = offset; |
afaf0b2f | 1996 | vcpu->arch.tsc_offset = kvm_x86_ops.write_l1_tsc_offset(vcpu, offset); |
a545ab6a LC |
1997 | } |
1998 | ||
b0c39dc6 VK |
1999 | static inline bool kvm_check_tsc_unstable(void) |
2000 | { | |
2001 | #ifdef CONFIG_X86_64 | |
2002 | /* | |
2003 | * TSC is marked unstable when we're running on Hyper-V, | |
2004 | * 'TSC page' clocksource is good. | |
2005 | */ | |
b95a8a27 | 2006 | if (pvclock_gtod_data.clock.vclock_mode == VDSO_CLOCKMODE_HVCLOCK) |
b0c39dc6 VK |
2007 | return false; |
2008 | #endif | |
2009 | return check_tsc_unstable(); | |
2010 | } | |
2011 | ||
8fe8ab46 | 2012 | void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr) |
99e3e30a ZA |
2013 | { |
2014 | struct kvm *kvm = vcpu->kvm; | |
f38e098f | 2015 | u64 offset, ns, elapsed; |
99e3e30a | 2016 | unsigned long flags; |
b48aa97e | 2017 | bool matched; |
0d3da0d2 | 2018 | bool already_matched; |
8fe8ab46 | 2019 | u64 data = msr->data; |
c5e8ec8e | 2020 | bool synchronizing = false; |
99e3e30a | 2021 | |
038f8c11 | 2022 | raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags); |
07c1419a | 2023 | offset = kvm_compute_tsc_offset(vcpu, data); |
8171cd68 | 2024 | ns = get_kvmclock_base_ns(); |
f38e098f | 2025 | elapsed = ns - kvm->arch.last_tsc_nsec; |
5d3cb0f6 | 2026 | |
03ba32ca | 2027 | if (vcpu->arch.virtual_tsc_khz) { |
bd8fab39 DP |
2028 | if (data == 0 && msr->host_initiated) { |
2029 | /* | |
2030 | * detection of vcpu initialization -- need to sync | |
2031 | * with other vCPUs. This particularly helps to keep | |
2032 | * kvm_clock stable after CPU hotplug | |
2033 | */ | |
2034 | synchronizing = true; | |
2035 | } else { | |
2036 | u64 tsc_exp = kvm->arch.last_tsc_write + | |
2037 | nsec_to_cycles(vcpu, elapsed); | |
2038 | u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL; | |
2039 | /* | |
2040 | * Special case: TSC write with a small delta (1 second) | |
2041 | * of virtual cycle time against real time is | |
2042 | * interpreted as an attempt to synchronize the CPU. | |
2043 | */ | |
2044 | synchronizing = data < tsc_exp + tsc_hz && | |
2045 | data + tsc_hz > tsc_exp; | |
2046 | } | |
c5e8ec8e | 2047 | } |
f38e098f ZA |
2048 | |
2049 | /* | |
5d3cb0f6 ZA |
2050 | * For a reliable TSC, we can match TSC offsets, and for an unstable |
2051 | * TSC, we add elapsed time in this computation. We could let the | |
2052 | * compensation code attempt to catch up if we fall behind, but | |
2053 | * it's better to try to match offsets from the beginning. | |
2054 | */ | |
c5e8ec8e | 2055 | if (synchronizing && |
5d3cb0f6 | 2056 | vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) { |
b0c39dc6 | 2057 | if (!kvm_check_tsc_unstable()) { |
e26101b1 | 2058 | offset = kvm->arch.cur_tsc_offset; |
f38e098f | 2059 | } else { |
857e4099 | 2060 | u64 delta = nsec_to_cycles(vcpu, elapsed); |
5d3cb0f6 | 2061 | data += delta; |
07c1419a | 2062 | offset = kvm_compute_tsc_offset(vcpu, data); |
f38e098f | 2063 | } |
b48aa97e | 2064 | matched = true; |
0d3da0d2 | 2065 | already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation); |
e26101b1 ZA |
2066 | } else { |
2067 | /* | |
2068 | * We split periods of matched TSC writes into generations. | |
2069 | * For each generation, we track the original measured | |
2070 | * nanosecond time, offset, and write, so if TSCs are in | |
2071 | * sync, we can match exact offset, and if not, we can match | |
4a969980 | 2072 | * exact software computation in compute_guest_tsc() |
e26101b1 ZA |
2073 | * |
2074 | * These values are tracked in kvm->arch.cur_xxx variables. | |
2075 | */ | |
2076 | kvm->arch.cur_tsc_generation++; | |
2077 | kvm->arch.cur_tsc_nsec = ns; | |
2078 | kvm->arch.cur_tsc_write = data; | |
2079 | kvm->arch.cur_tsc_offset = offset; | |
b48aa97e | 2080 | matched = false; |
f38e098f | 2081 | } |
e26101b1 ZA |
2082 | |
2083 | /* | |
2084 | * We also track th most recent recorded KHZ, write and time to | |
2085 | * allow the matching interval to be extended at each write. | |
2086 | */ | |
f38e098f ZA |
2087 | kvm->arch.last_tsc_nsec = ns; |
2088 | kvm->arch.last_tsc_write = data; | |
5d3cb0f6 | 2089 | kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz; |
99e3e30a | 2090 | |
b183aa58 | 2091 | vcpu->arch.last_guest_tsc = data; |
e26101b1 ZA |
2092 | |
2093 | /* Keep track of which generation this VCPU has synchronized to */ | |
2094 | vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation; | |
2095 | vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec; | |
2096 | vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write; | |
2097 | ||
d6321d49 | 2098 | if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) |
ba904635 | 2099 | update_ia32_tsc_adjust_msr(vcpu, offset); |
d6321d49 | 2100 | |
a545ab6a | 2101 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
e26101b1 | 2102 | raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags); |
b48aa97e MT |
2103 | |
2104 | spin_lock(&kvm->arch.pvclock_gtod_sync_lock); | |
0d3da0d2 | 2105 | if (!matched) { |
b48aa97e | 2106 | kvm->arch.nr_vcpus_matched_tsc = 0; |
0d3da0d2 TG |
2107 | } else if (!already_matched) { |
2108 | kvm->arch.nr_vcpus_matched_tsc++; | |
2109 | } | |
b48aa97e MT |
2110 | |
2111 | kvm_track_tsc_matching(vcpu); | |
2112 | spin_unlock(&kvm->arch.pvclock_gtod_sync_lock); | |
99e3e30a | 2113 | } |
e26101b1 | 2114 | |
99e3e30a ZA |
2115 | EXPORT_SYMBOL_GPL(kvm_write_tsc); |
2116 | ||
58ea6767 HZ |
2117 | static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu, |
2118 | s64 adjustment) | |
2119 | { | |
56ba77a4 | 2120 | u64 tsc_offset = vcpu->arch.l1_tsc_offset; |
326e7425 | 2121 | kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment); |
58ea6767 HZ |
2122 | } |
2123 | ||
2124 | static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment) | |
2125 | { | |
2126 | if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio) | |
2127 | WARN_ON(adjustment < 0); | |
2128 | adjustment = kvm_scale_tsc(vcpu, (u64) adjustment); | |
ea26e4ec | 2129 | adjust_tsc_offset_guest(vcpu, adjustment); |
58ea6767 HZ |
2130 | } |
2131 | ||
d828199e MT |
2132 | #ifdef CONFIG_X86_64 |
2133 | ||
a5a1d1c2 | 2134 | static u64 read_tsc(void) |
d828199e | 2135 | { |
a5a1d1c2 | 2136 | u64 ret = (u64)rdtsc_ordered(); |
03b9730b | 2137 | u64 last = pvclock_gtod_data.clock.cycle_last; |
d828199e MT |
2138 | |
2139 | if (likely(ret >= last)) | |
2140 | return ret; | |
2141 | ||
2142 | /* | |
2143 | * GCC likes to generate cmov here, but this branch is extremely | |
6a6256f9 | 2144 | * predictable (it's just a function of time and the likely is |
d828199e MT |
2145 | * very likely) and there's a data dependence, so force GCC |
2146 | * to generate a branch instead. I don't barrier() because | |
2147 | * we don't actually need a barrier, and if this function | |
2148 | * ever gets inlined it will generate worse code. | |
2149 | */ | |
2150 | asm volatile (""); | |
2151 | return last; | |
2152 | } | |
2153 | ||
53fafdbb MT |
2154 | static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp, |
2155 | int *mode) | |
d828199e MT |
2156 | { |
2157 | long v; | |
b0c39dc6 VK |
2158 | u64 tsc_pg_val; |
2159 | ||
53fafdbb | 2160 | switch (clock->vclock_mode) { |
b95a8a27 | 2161 | case VDSO_CLOCKMODE_HVCLOCK: |
b0c39dc6 VK |
2162 | tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(), |
2163 | tsc_timestamp); | |
2164 | if (tsc_pg_val != U64_MAX) { | |
2165 | /* TSC page valid */ | |
b95a8a27 | 2166 | *mode = VDSO_CLOCKMODE_HVCLOCK; |
53fafdbb MT |
2167 | v = (tsc_pg_val - clock->cycle_last) & |
2168 | clock->mask; | |
b0c39dc6 VK |
2169 | } else { |
2170 | /* TSC page invalid */ | |
b95a8a27 | 2171 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 VK |
2172 | } |
2173 | break; | |
b95a8a27 TG |
2174 | case VDSO_CLOCKMODE_TSC: |
2175 | *mode = VDSO_CLOCKMODE_TSC; | |
b0c39dc6 | 2176 | *tsc_timestamp = read_tsc(); |
53fafdbb MT |
2177 | v = (*tsc_timestamp - clock->cycle_last) & |
2178 | clock->mask; | |
b0c39dc6 VK |
2179 | break; |
2180 | default: | |
b95a8a27 | 2181 | *mode = VDSO_CLOCKMODE_NONE; |
b0c39dc6 | 2182 | } |
d828199e | 2183 | |
b95a8a27 | 2184 | if (*mode == VDSO_CLOCKMODE_NONE) |
b0c39dc6 | 2185 | *tsc_timestamp = v = 0; |
d828199e | 2186 | |
53fafdbb | 2187 | return v * clock->mult; |
d828199e MT |
2188 | } |
2189 | ||
53fafdbb | 2190 | static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp) |
d828199e | 2191 | { |
cbcf2dd3 | 2192 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; |
d828199e | 2193 | unsigned long seq; |
d828199e | 2194 | int mode; |
cbcf2dd3 | 2195 | u64 ns; |
d828199e | 2196 | |
d828199e MT |
2197 | do { |
2198 | seq = read_seqcount_begin(>od->seq); | |
917f9475 | 2199 | ns = gtod->raw_clock.base_cycles; |
53fafdbb | 2200 | ns += vgettsc(>od->raw_clock, tsc_timestamp, &mode); |
917f9475 PB |
2201 | ns >>= gtod->raw_clock.shift; |
2202 | ns += ktime_to_ns(ktime_add(gtod->raw_clock.offset, gtod->offs_boot)); | |
d828199e | 2203 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); |
cbcf2dd3 | 2204 | *t = ns; |
d828199e MT |
2205 | |
2206 | return mode; | |
2207 | } | |
2208 | ||
899a31f5 | 2209 | static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp) |
55dd00a7 MT |
2210 | { |
2211 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
2212 | unsigned long seq; | |
2213 | int mode; | |
2214 | u64 ns; | |
2215 | ||
2216 | do { | |
2217 | seq = read_seqcount_begin(>od->seq); | |
55dd00a7 | 2218 | ts->tv_sec = gtod->wall_time_sec; |
917f9475 | 2219 | ns = gtod->clock.base_cycles; |
53fafdbb | 2220 | ns += vgettsc(>od->clock, tsc_timestamp, &mode); |
55dd00a7 MT |
2221 | ns >>= gtod->clock.shift; |
2222 | } while (unlikely(read_seqcount_retry(>od->seq, seq))); | |
2223 | ||
2224 | ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns); | |
2225 | ts->tv_nsec = ns; | |
2226 | ||
2227 | return mode; | |
2228 | } | |
2229 | ||
b0c39dc6 VK |
2230 | /* returns true if host is using TSC based clocksource */ |
2231 | static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp) | |
d828199e | 2232 | { |
d828199e | 2233 | /* checked again under seqlock below */ |
b0c39dc6 | 2234 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
d828199e MT |
2235 | return false; |
2236 | ||
53fafdbb | 2237 | return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns, |
b0c39dc6 | 2238 | tsc_timestamp)); |
d828199e | 2239 | } |
55dd00a7 | 2240 | |
b0c39dc6 | 2241 | /* returns true if host is using TSC based clocksource */ |
899a31f5 | 2242 | static bool kvm_get_walltime_and_clockread(struct timespec64 *ts, |
b0c39dc6 | 2243 | u64 *tsc_timestamp) |
55dd00a7 MT |
2244 | { |
2245 | /* checked again under seqlock below */ | |
b0c39dc6 | 2246 | if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode)) |
55dd00a7 MT |
2247 | return false; |
2248 | ||
b0c39dc6 | 2249 | return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp)); |
55dd00a7 | 2250 | } |
d828199e MT |
2251 | #endif |
2252 | ||
2253 | /* | |
2254 | * | |
b48aa97e MT |
2255 | * Assuming a stable TSC across physical CPUS, and a stable TSC |
2256 | * across virtual CPUs, the following condition is possible. | |
2257 | * Each numbered line represents an event visible to both | |
d828199e MT |
2258 | * CPUs at the next numbered event. |
2259 | * | |
2260 | * "timespecX" represents host monotonic time. "tscX" represents | |
2261 | * RDTSC value. | |
2262 | * | |
2263 | * VCPU0 on CPU0 | VCPU1 on CPU1 | |
2264 | * | |
2265 | * 1. read timespec0,tsc0 | |
2266 | * 2. | timespec1 = timespec0 + N | |
2267 | * | tsc1 = tsc0 + M | |
2268 | * 3. transition to guest | transition to guest | |
2269 | * 4. ret0 = timespec0 + (rdtsc - tsc0) | | |
2270 | * 5. | ret1 = timespec1 + (rdtsc - tsc1) | |
2271 | * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M)) | |
2272 | * | |
2273 | * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity: | |
2274 | * | |
2275 | * - ret0 < ret1 | |
2276 | * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M)) | |
2277 | * ... | |
2278 | * - 0 < N - M => M < N | |
2279 | * | |
2280 | * That is, when timespec0 != timespec1, M < N. Unfortunately that is not | |
2281 | * always the case (the difference between two distinct xtime instances | |
2282 | * might be smaller then the difference between corresponding TSC reads, | |
2283 | * when updating guest vcpus pvclock areas). | |
2284 | * | |
2285 | * To avoid that problem, do not allow visibility of distinct | |
2286 | * system_timestamp/tsc_timestamp values simultaneously: use a master | |
2287 | * copy of host monotonic time values. Update that master copy | |
2288 | * in lockstep. | |
2289 | * | |
b48aa97e | 2290 | * Rely on synchronization of host TSCs and guest TSCs for monotonicity. |
d828199e MT |
2291 | * |
2292 | */ | |
2293 | ||
2294 | static void pvclock_update_vm_gtod_copy(struct kvm *kvm) | |
2295 | { | |
2296 | #ifdef CONFIG_X86_64 | |
2297 | struct kvm_arch *ka = &kvm->arch; | |
2298 | int vclock_mode; | |
b48aa97e MT |
2299 | bool host_tsc_clocksource, vcpus_matched; |
2300 | ||
2301 | vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 == | |
2302 | atomic_read(&kvm->online_vcpus)); | |
d828199e MT |
2303 | |
2304 | /* | |
2305 | * If the host uses TSC clock, then passthrough TSC as stable | |
2306 | * to the guest. | |
2307 | */ | |
b48aa97e | 2308 | host_tsc_clocksource = kvm_get_time_and_clockread( |
d828199e MT |
2309 | &ka->master_kernel_ns, |
2310 | &ka->master_cycle_now); | |
2311 | ||
16a96021 | 2312 | ka->use_master_clock = host_tsc_clocksource && vcpus_matched |
a826faf1 | 2313 | && !ka->backwards_tsc_observed |
54750f2c | 2314 | && !ka->boot_vcpu_runs_old_kvmclock; |
b48aa97e | 2315 | |
d828199e MT |
2316 | if (ka->use_master_clock) |
2317 | atomic_set(&kvm_guest_has_master_clock, 1); | |
2318 | ||
2319 | vclock_mode = pvclock_gtod_data.clock.vclock_mode; | |
b48aa97e MT |
2320 | trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode, |
2321 | vcpus_matched); | |
d828199e MT |
2322 | #endif |
2323 | } | |
2324 | ||
2860c4b1 PB |
2325 | void kvm_make_mclock_inprogress_request(struct kvm *kvm) |
2326 | { | |
2327 | kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS); | |
2328 | } | |
2329 | ||
2e762ff7 MT |
2330 | static void kvm_gen_update_masterclock(struct kvm *kvm) |
2331 | { | |
2332 | #ifdef CONFIG_X86_64 | |
2333 | int i; | |
2334 | struct kvm_vcpu *vcpu; | |
2335 | struct kvm_arch *ka = &kvm->arch; | |
2336 | ||
2337 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
2338 | kvm_make_mclock_inprogress_request(kvm); | |
2339 | /* no guest entries from this point */ | |
2340 | pvclock_update_vm_gtod_copy(kvm); | |
2341 | ||
2342 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 2343 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
2e762ff7 MT |
2344 | |
2345 | /* guest entries allowed */ | |
2346 | kvm_for_each_vcpu(i, vcpu, kvm) | |
72875d8a | 2347 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); |
2e762ff7 MT |
2348 | |
2349 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
2350 | #endif | |
2351 | } | |
2352 | ||
e891a32e | 2353 | u64 get_kvmclock_ns(struct kvm *kvm) |
108b249c | 2354 | { |
108b249c | 2355 | struct kvm_arch *ka = &kvm->arch; |
8b953440 | 2356 | struct pvclock_vcpu_time_info hv_clock; |
e2c2206a | 2357 | u64 ret; |
108b249c | 2358 | |
8b953440 PB |
2359 | spin_lock(&ka->pvclock_gtod_sync_lock); |
2360 | if (!ka->use_master_clock) { | |
2361 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
8171cd68 | 2362 | return get_kvmclock_base_ns() + ka->kvmclock_offset; |
108b249c PB |
2363 | } |
2364 | ||
8b953440 PB |
2365 | hv_clock.tsc_timestamp = ka->master_cycle_now; |
2366 | hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset; | |
2367 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
2368 | ||
e2c2206a WL |
2369 | /* both __this_cpu_read() and rdtsc() should be on the same cpu */ |
2370 | get_cpu(); | |
2371 | ||
e70b57a6 WL |
2372 | if (__this_cpu_read(cpu_tsc_khz)) { |
2373 | kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL, | |
2374 | &hv_clock.tsc_shift, | |
2375 | &hv_clock.tsc_to_system_mul); | |
2376 | ret = __pvclock_read_cycles(&hv_clock, rdtsc()); | |
2377 | } else | |
8171cd68 | 2378 | ret = get_kvmclock_base_ns() + ka->kvmclock_offset; |
e2c2206a WL |
2379 | |
2380 | put_cpu(); | |
2381 | ||
2382 | return ret; | |
108b249c PB |
2383 | } |
2384 | ||
0d6dd2ff PB |
2385 | static void kvm_setup_pvclock_page(struct kvm_vcpu *v) |
2386 | { | |
2387 | struct kvm_vcpu_arch *vcpu = &v->arch; | |
2388 | struct pvclock_vcpu_time_info guest_hv_clock; | |
2389 | ||
4e335d9e | 2390 | if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time, |
0d6dd2ff PB |
2391 | &guest_hv_clock, sizeof(guest_hv_clock)))) |
2392 | return; | |
2393 | ||
2394 | /* This VCPU is paused, but it's legal for a guest to read another | |
2395 | * VCPU's kvmclock, so we really have to follow the specification where | |
2396 | * it says that version is odd if data is being modified, and even after | |
2397 | * it is consistent. | |
2398 | * | |
2399 | * Version field updates must be kept separate. This is because | |
2400 | * kvm_write_guest_cached might use a "rep movs" instruction, and | |
2401 | * writes within a string instruction are weakly ordered. So there | |
2402 | * are three writes overall. | |
2403 | * | |
2404 | * As a small optimization, only write the version field in the first | |
2405 | * and third write. The vcpu->pv_time cache is still valid, because the | |
2406 | * version field is the first in the struct. | |
2407 | */ | |
2408 | BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0); | |
2409 | ||
51c4b8bb LA |
2410 | if (guest_hv_clock.version & 1) |
2411 | ++guest_hv_clock.version; /* first time write, random junk */ | |
2412 | ||
0d6dd2ff | 2413 | vcpu->hv_clock.version = guest_hv_clock.version + 1; |
4e335d9e PB |
2414 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
2415 | &vcpu->hv_clock, | |
2416 | sizeof(vcpu->hv_clock.version)); | |
0d6dd2ff PB |
2417 | |
2418 | smp_wmb(); | |
2419 | ||
2420 | /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */ | |
2421 | vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED); | |
2422 | ||
2423 | if (vcpu->pvclock_set_guest_stopped_request) { | |
2424 | vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED; | |
2425 | vcpu->pvclock_set_guest_stopped_request = false; | |
2426 | } | |
2427 | ||
2428 | trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock); | |
2429 | ||
4e335d9e PB |
2430 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
2431 | &vcpu->hv_clock, | |
2432 | sizeof(vcpu->hv_clock)); | |
0d6dd2ff PB |
2433 | |
2434 | smp_wmb(); | |
2435 | ||
2436 | vcpu->hv_clock.version++; | |
4e335d9e PB |
2437 | kvm_write_guest_cached(v->kvm, &vcpu->pv_time, |
2438 | &vcpu->hv_clock, | |
2439 | sizeof(vcpu->hv_clock.version)); | |
0d6dd2ff PB |
2440 | } |
2441 | ||
34c238a1 | 2442 | static int kvm_guest_time_update(struct kvm_vcpu *v) |
18068523 | 2443 | { |
78db6a50 | 2444 | unsigned long flags, tgt_tsc_khz; |
18068523 | 2445 | struct kvm_vcpu_arch *vcpu = &v->arch; |
d828199e | 2446 | struct kvm_arch *ka = &v->kvm->arch; |
f25e656d | 2447 | s64 kernel_ns; |
d828199e | 2448 | u64 tsc_timestamp, host_tsc; |
51d59c6b | 2449 | u8 pvclock_flags; |
d828199e MT |
2450 | bool use_master_clock; |
2451 | ||
2452 | kernel_ns = 0; | |
2453 | host_tsc = 0; | |
18068523 | 2454 | |
d828199e MT |
2455 | /* |
2456 | * If the host uses TSC clock, then passthrough TSC as stable | |
2457 | * to the guest. | |
2458 | */ | |
2459 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
2460 | use_master_clock = ka->use_master_clock; | |
2461 | if (use_master_clock) { | |
2462 | host_tsc = ka->master_cycle_now; | |
2463 | kernel_ns = ka->master_kernel_ns; | |
2464 | } | |
2465 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
c09664bb MT |
2466 | |
2467 | /* Keep irq disabled to prevent changes to the clock */ | |
2468 | local_irq_save(flags); | |
78db6a50 PB |
2469 | tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz); |
2470 | if (unlikely(tgt_tsc_khz == 0)) { | |
c09664bb MT |
2471 | local_irq_restore(flags); |
2472 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); | |
2473 | return 1; | |
2474 | } | |
d828199e | 2475 | if (!use_master_clock) { |
4ea1636b | 2476 | host_tsc = rdtsc(); |
8171cd68 | 2477 | kernel_ns = get_kvmclock_base_ns(); |
d828199e MT |
2478 | } |
2479 | ||
4ba76538 | 2480 | tsc_timestamp = kvm_read_l1_tsc(v, host_tsc); |
d828199e | 2481 | |
c285545f ZA |
2482 | /* |
2483 | * We may have to catch up the TSC to match elapsed wall clock | |
2484 | * time for two reasons, even if kvmclock is used. | |
2485 | * 1) CPU could have been running below the maximum TSC rate | |
2486 | * 2) Broken TSC compensation resets the base at each VCPU | |
2487 | * entry to avoid unknown leaps of TSC even when running | |
2488 | * again on the same CPU. This may cause apparent elapsed | |
2489 | * time to disappear, and the guest to stand still or run | |
2490 | * very slowly. | |
2491 | */ | |
2492 | if (vcpu->tsc_catchup) { | |
2493 | u64 tsc = compute_guest_tsc(v, kernel_ns); | |
2494 | if (tsc > tsc_timestamp) { | |
f1e2b260 | 2495 | adjust_tsc_offset_guest(v, tsc - tsc_timestamp); |
c285545f ZA |
2496 | tsc_timestamp = tsc; |
2497 | } | |
50d0a0f9 GH |
2498 | } |
2499 | ||
18068523 GOC |
2500 | local_irq_restore(flags); |
2501 | ||
0d6dd2ff | 2502 | /* With all the info we got, fill in the values */ |
18068523 | 2503 | |
78db6a50 PB |
2504 | if (kvm_has_tsc_control) |
2505 | tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz); | |
2506 | ||
2507 | if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) { | |
3ae13faa | 2508 | kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL, |
5f4e3f88 ZA |
2509 | &vcpu->hv_clock.tsc_shift, |
2510 | &vcpu->hv_clock.tsc_to_system_mul); | |
78db6a50 | 2511 | vcpu->hw_tsc_khz = tgt_tsc_khz; |
8cfdc000 ZA |
2512 | } |
2513 | ||
1d5f066e | 2514 | vcpu->hv_clock.tsc_timestamp = tsc_timestamp; |
759379dd | 2515 | vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset; |
28e4639a | 2516 | vcpu->last_guest_tsc = tsc_timestamp; |
51d59c6b | 2517 | |
d828199e | 2518 | /* If the host uses TSC clocksource, then it is stable */ |
0d6dd2ff | 2519 | pvclock_flags = 0; |
d828199e MT |
2520 | if (use_master_clock) |
2521 | pvclock_flags |= PVCLOCK_TSC_STABLE_BIT; | |
2522 | ||
78c0337a MT |
2523 | vcpu->hv_clock.flags = pvclock_flags; |
2524 | ||
095cf55d PB |
2525 | if (vcpu->pv_time_enabled) |
2526 | kvm_setup_pvclock_page(v); | |
2527 | if (v == kvm_get_vcpu(v->kvm, 0)) | |
2528 | kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock); | |
8cfdc000 | 2529 | return 0; |
c8076604 GH |
2530 | } |
2531 | ||
0061d53d MT |
2532 | /* |
2533 | * kvmclock updates which are isolated to a given vcpu, such as | |
2534 | * vcpu->cpu migration, should not allow system_timestamp from | |
2535 | * the rest of the vcpus to remain static. Otherwise ntp frequency | |
2536 | * correction applies to one vcpu's system_timestamp but not | |
2537 | * the others. | |
2538 | * | |
2539 | * So in those cases, request a kvmclock update for all vcpus. | |
7e44e449 AJ |
2540 | * We need to rate-limit these requests though, as they can |
2541 | * considerably slow guests that have a large number of vcpus. | |
2542 | * The time for a remote vcpu to update its kvmclock is bound | |
2543 | * by the delay we use to rate-limit the updates. | |
0061d53d MT |
2544 | */ |
2545 | ||
7e44e449 AJ |
2546 | #define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100) |
2547 | ||
2548 | static void kvmclock_update_fn(struct work_struct *work) | |
0061d53d MT |
2549 | { |
2550 | int i; | |
7e44e449 AJ |
2551 | struct delayed_work *dwork = to_delayed_work(work); |
2552 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
2553 | kvmclock_update_work); | |
2554 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
0061d53d MT |
2555 | struct kvm_vcpu *vcpu; |
2556 | ||
2557 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
105b21bb | 2558 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0061d53d MT |
2559 | kvm_vcpu_kick(vcpu); |
2560 | } | |
2561 | } | |
2562 | ||
7e44e449 AJ |
2563 | static void kvm_gen_kvmclock_update(struct kvm_vcpu *v) |
2564 | { | |
2565 | struct kvm *kvm = v->kvm; | |
2566 | ||
105b21bb | 2567 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, v); |
7e44e449 AJ |
2568 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, |
2569 | KVMCLOCK_UPDATE_DELAY); | |
2570 | } | |
2571 | ||
332967a3 AJ |
2572 | #define KVMCLOCK_SYNC_PERIOD (300 * HZ) |
2573 | ||
2574 | static void kvmclock_sync_fn(struct work_struct *work) | |
2575 | { | |
2576 | struct delayed_work *dwork = to_delayed_work(work); | |
2577 | struct kvm_arch *ka = container_of(dwork, struct kvm_arch, | |
2578 | kvmclock_sync_work); | |
2579 | struct kvm *kvm = container_of(ka, struct kvm, arch); | |
2580 | ||
630994b3 MT |
2581 | if (!kvmclock_periodic_sync) |
2582 | return; | |
2583 | ||
332967a3 AJ |
2584 | schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0); |
2585 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
2586 | KVMCLOCK_SYNC_PERIOD); | |
2587 | } | |
2588 | ||
191c8137 BP |
2589 | /* |
2590 | * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP. | |
2591 | */ | |
2592 | static bool can_set_mci_status(struct kvm_vcpu *vcpu) | |
2593 | { | |
2594 | /* McStatusWrEn enabled? */ | |
23493d0a | 2595 | if (guest_cpuid_is_amd_or_hygon(vcpu)) |
191c8137 BP |
2596 | return !!(vcpu->arch.msr_hwcr & BIT_ULL(18)); |
2597 | ||
2598 | return false; | |
2599 | } | |
2600 | ||
9ffd986c | 2601 | static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 2602 | { |
890ca9ae HY |
2603 | u64 mcg_cap = vcpu->arch.mcg_cap; |
2604 | unsigned bank_num = mcg_cap & 0xff; | |
9ffd986c WL |
2605 | u32 msr = msr_info->index; |
2606 | u64 data = msr_info->data; | |
890ca9ae | 2607 | |
15c4a640 | 2608 | switch (msr) { |
15c4a640 | 2609 | case MSR_IA32_MCG_STATUS: |
890ca9ae | 2610 | vcpu->arch.mcg_status = data; |
15c4a640 | 2611 | break; |
c7ac679c | 2612 | case MSR_IA32_MCG_CTL: |
44883f01 PB |
2613 | if (!(mcg_cap & MCG_CTL_P) && |
2614 | (data || !msr_info->host_initiated)) | |
890ca9ae HY |
2615 | return 1; |
2616 | if (data != 0 && data != ~(u64)0) | |
44883f01 | 2617 | return 1; |
890ca9ae HY |
2618 | vcpu->arch.mcg_ctl = data; |
2619 | break; | |
2620 | default: | |
2621 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 2622 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
2623 | u32 offset = array_index_nospec( |
2624 | msr - MSR_IA32_MC0_CTL, | |
2625 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
2626 | ||
114be429 AP |
2627 | /* only 0 or all 1s can be written to IA32_MCi_CTL |
2628 | * some Linux kernels though clear bit 10 in bank 4 to | |
2629 | * workaround a BIOS/GART TBL issue on AMD K8s, ignore | |
2630 | * this to avoid an uncatched #GP in the guest | |
2631 | */ | |
890ca9ae | 2632 | if ((offset & 0x3) == 0 && |
114be429 | 2633 | data != 0 && (data | (1 << 10)) != ~(u64)0) |
890ca9ae | 2634 | return -1; |
191c8137 BP |
2635 | |
2636 | /* MCi_STATUS */ | |
9ffd986c | 2637 | if (!msr_info->host_initiated && |
191c8137 BP |
2638 | (offset & 0x3) == 1 && data != 0) { |
2639 | if (!can_set_mci_status(vcpu)) | |
2640 | return -1; | |
2641 | } | |
2642 | ||
890ca9ae HY |
2643 | vcpu->arch.mce_banks[offset] = data; |
2644 | break; | |
2645 | } | |
2646 | return 1; | |
2647 | } | |
2648 | return 0; | |
2649 | } | |
2650 | ||
ffde22ac ES |
2651 | static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data) |
2652 | { | |
2653 | struct kvm *kvm = vcpu->kvm; | |
2654 | int lm = is_long_mode(vcpu); | |
2655 | u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64 | |
2656 | : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32; | |
2657 | u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64 | |
2658 | : kvm->arch.xen_hvm_config.blob_size_32; | |
2659 | u32 page_num = data & ~PAGE_MASK; | |
2660 | u64 page_addr = data & PAGE_MASK; | |
2661 | u8 *page; | |
2662 | int r; | |
2663 | ||
2664 | r = -E2BIG; | |
2665 | if (page_num >= blob_size) | |
2666 | goto out; | |
2667 | r = -ENOMEM; | |
ff5c2c03 SL |
2668 | page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE); |
2669 | if (IS_ERR(page)) { | |
2670 | r = PTR_ERR(page); | |
ffde22ac | 2671 | goto out; |
ff5c2c03 | 2672 | } |
54bf36aa | 2673 | if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE)) |
ffde22ac ES |
2674 | goto out_free; |
2675 | r = 0; | |
2676 | out_free: | |
2677 | kfree(page); | |
2678 | out: | |
2679 | return r; | |
2680 | } | |
2681 | ||
2635b5c4 VK |
2682 | static inline bool kvm_pv_async_pf_enabled(struct kvm_vcpu *vcpu) |
2683 | { | |
2684 | u64 mask = KVM_ASYNC_PF_ENABLED | KVM_ASYNC_PF_DELIVERY_AS_INT; | |
2685 | ||
2686 | return (vcpu->arch.apf.msr_en_val & mask) == mask; | |
2687 | } | |
2688 | ||
344d9588 GN |
2689 | static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data) |
2690 | { | |
2691 | gpa_t gpa = data & ~0x3f; | |
2692 | ||
2635b5c4 VK |
2693 | /* Bits 4:5 are reserved, Should be zero */ |
2694 | if (data & 0x30) | |
344d9588 GN |
2695 | return 1; |
2696 | ||
2635b5c4 | 2697 | vcpu->arch.apf.msr_en_val = data; |
344d9588 | 2698 | |
2635b5c4 | 2699 | if (!kvm_pv_async_pf_enabled(vcpu)) { |
344d9588 GN |
2700 | kvm_clear_async_pf_completion_queue(vcpu); |
2701 | kvm_async_pf_hash_reset(vcpu); | |
2702 | return 0; | |
2703 | } | |
2704 | ||
4e335d9e | 2705 | if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa, |
68fd66f1 | 2706 | sizeof(u64))) |
344d9588 GN |
2707 | return 1; |
2708 | ||
6adba527 | 2709 | vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS); |
52a5c155 | 2710 | vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT; |
2635b5c4 | 2711 | |
344d9588 | 2712 | kvm_async_pf_wakeup_all(vcpu); |
2635b5c4 VK |
2713 | |
2714 | return 0; | |
2715 | } | |
2716 | ||
2717 | static int kvm_pv_enable_async_pf_int(struct kvm_vcpu *vcpu, u64 data) | |
2718 | { | |
2719 | /* Bits 8-63 are reserved */ | |
2720 | if (data >> 8) | |
2721 | return 1; | |
2722 | ||
2723 | if (!lapic_in_kernel(vcpu)) | |
2724 | return 1; | |
2725 | ||
2726 | vcpu->arch.apf.msr_int_val = data; | |
2727 | ||
2728 | vcpu->arch.apf.vec = data & KVM_ASYNC_PF_VEC_MASK; | |
2729 | ||
344d9588 GN |
2730 | return 0; |
2731 | } | |
2732 | ||
12f9a48f GC |
2733 | static void kvmclock_reset(struct kvm_vcpu *vcpu) |
2734 | { | |
0b79459b | 2735 | vcpu->arch.pv_time_enabled = false; |
49dedf0d | 2736 | vcpu->arch.time = 0; |
12f9a48f GC |
2737 | } |
2738 | ||
7780938c | 2739 | static void kvm_vcpu_flush_tlb_all(struct kvm_vcpu *vcpu) |
f38a7b75 WL |
2740 | { |
2741 | ++vcpu->stat.tlb_flush; | |
7780938c | 2742 | kvm_x86_ops.tlb_flush_all(vcpu); |
f38a7b75 WL |
2743 | } |
2744 | ||
0baedd79 VK |
2745 | static void kvm_vcpu_flush_tlb_guest(struct kvm_vcpu *vcpu) |
2746 | { | |
2747 | ++vcpu->stat.tlb_flush; | |
2748 | kvm_x86_ops.tlb_flush_guest(vcpu); | |
2749 | } | |
2750 | ||
c9aaa895 GC |
2751 | static void record_steal_time(struct kvm_vcpu *vcpu) |
2752 | { | |
b0431382 BO |
2753 | struct kvm_host_map map; |
2754 | struct kvm_steal_time *st; | |
2755 | ||
c9aaa895 GC |
2756 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
2757 | return; | |
2758 | ||
b0431382 BO |
2759 | /* -EAGAIN is returned in atomic context so we can just return. */ |
2760 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, | |
2761 | &map, &vcpu->arch.st.cache, false)) | |
c9aaa895 GC |
2762 | return; |
2763 | ||
b0431382 BO |
2764 | st = map.hva + |
2765 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); | |
2766 | ||
f38a7b75 WL |
2767 | /* |
2768 | * Doing a TLB flush here, on the guest's behalf, can avoid | |
2769 | * expensive IPIs. | |
2770 | */ | |
b382f44e | 2771 | trace_kvm_pv_tlb_flush(vcpu->vcpu_id, |
b0431382 BO |
2772 | st->preempted & KVM_VCPU_FLUSH_TLB); |
2773 | if (xchg(&st->preempted, 0) & KVM_VCPU_FLUSH_TLB) | |
0baedd79 | 2774 | kvm_vcpu_flush_tlb_guest(vcpu); |
0b9f6c46 | 2775 | |
a6bd811f | 2776 | vcpu->arch.st.preempted = 0; |
35f3fae1 | 2777 | |
b0431382 BO |
2778 | if (st->version & 1) |
2779 | st->version += 1; /* first time write, random junk */ | |
35f3fae1 | 2780 | |
b0431382 | 2781 | st->version += 1; |
35f3fae1 WL |
2782 | |
2783 | smp_wmb(); | |
2784 | ||
b0431382 | 2785 | st->steal += current->sched_info.run_delay - |
c54cdf14 LC |
2786 | vcpu->arch.st.last_steal; |
2787 | vcpu->arch.st.last_steal = current->sched_info.run_delay; | |
35f3fae1 | 2788 | |
35f3fae1 WL |
2789 | smp_wmb(); |
2790 | ||
b0431382 | 2791 | st->version += 1; |
c9aaa895 | 2792 | |
b0431382 | 2793 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, false); |
c9aaa895 GC |
2794 | } |
2795 | ||
8fe8ab46 | 2796 | int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
15c4a640 | 2797 | { |
5753785f | 2798 | bool pr = false; |
8fe8ab46 WA |
2799 | u32 msr = msr_info->index; |
2800 | u64 data = msr_info->data; | |
5753785f | 2801 | |
15c4a640 | 2802 | switch (msr) { |
2e32b719 | 2803 | case MSR_AMD64_NB_CFG: |
2e32b719 BP |
2804 | case MSR_IA32_UCODE_WRITE: |
2805 | case MSR_VM_HSAVE_PA: | |
2806 | case MSR_AMD64_PATCH_LOADER: | |
2807 | case MSR_AMD64_BU_CFG2: | |
405a353a | 2808 | case MSR_AMD64_DC_CFG: |
0e1b869f | 2809 | case MSR_F15H_EX_CFG: |
2e32b719 BP |
2810 | break; |
2811 | ||
518e7b94 WL |
2812 | case MSR_IA32_UCODE_REV: |
2813 | if (msr_info->host_initiated) | |
2814 | vcpu->arch.microcode_version = data; | |
2815 | break; | |
0cf9135b SC |
2816 | case MSR_IA32_ARCH_CAPABILITIES: |
2817 | if (!msr_info->host_initiated) | |
2818 | return 1; | |
2819 | vcpu->arch.arch_capabilities = data; | |
2820 | break; | |
15c4a640 | 2821 | case MSR_EFER: |
11988499 | 2822 | return set_efer(vcpu, msr_info); |
8f1589d9 AP |
2823 | case MSR_K7_HWCR: |
2824 | data &= ~(u64)0x40; /* ignore flush filter disable */ | |
82494028 | 2825 | data &= ~(u64)0x100; /* ignore ignne emulation enable */ |
a223c313 | 2826 | data &= ~(u64)0x8; /* ignore TLB cache disable */ |
191c8137 BP |
2827 | |
2828 | /* Handle McStatusWrEn */ | |
2829 | if (data == BIT_ULL(18)) { | |
2830 | vcpu->arch.msr_hwcr = data; | |
2831 | } else if (data != 0) { | |
a737f256 CD |
2832 | vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n", |
2833 | data); | |
8f1589d9 AP |
2834 | return 1; |
2835 | } | |
15c4a640 | 2836 | break; |
f7c6d140 AP |
2837 | case MSR_FAM10H_MMIO_CONF_BASE: |
2838 | if (data != 0) { | |
a737f256 CD |
2839 | vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: " |
2840 | "0x%llx\n", data); | |
f7c6d140 AP |
2841 | return 1; |
2842 | } | |
15c4a640 | 2843 | break; |
b5e2fec0 AG |
2844 | case MSR_IA32_DEBUGCTLMSR: |
2845 | if (!data) { | |
2846 | /* We support the non-activated case already */ | |
2847 | break; | |
2848 | } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) { | |
2849 | /* Values other than LBR and BTF are vendor-specific, | |
2850 | thus reserved and should throw a #GP */ | |
2851 | return 1; | |
2852 | } | |
a737f256 CD |
2853 | vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n", |
2854 | __func__, data); | |
b5e2fec0 | 2855 | break; |
9ba075a6 | 2856 | case 0x200 ... 0x2ff: |
ff53604b | 2857 | return kvm_mtrr_set_msr(vcpu, msr, data); |
15c4a640 | 2858 | case MSR_IA32_APICBASE: |
58cb628d | 2859 | return kvm_set_apic_base(vcpu, msr_info); |
0105d1a5 GN |
2860 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
2861 | return kvm_x2apic_msr_write(vcpu, msr, data); | |
a3e06bbe LJ |
2862 | case MSR_IA32_TSCDEADLINE: |
2863 | kvm_set_lapic_tscdeadline_msr(vcpu, data); | |
2864 | break; | |
ba904635 | 2865 | case MSR_IA32_TSC_ADJUST: |
d6321d49 | 2866 | if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) { |
ba904635 | 2867 | if (!msr_info->host_initiated) { |
d913b904 | 2868 | s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr; |
d7add054 | 2869 | adjust_tsc_offset_guest(vcpu, adj); |
ba904635 WA |
2870 | } |
2871 | vcpu->arch.ia32_tsc_adjust_msr = data; | |
2872 | } | |
2873 | break; | |
15c4a640 | 2874 | case MSR_IA32_MISC_ENABLE: |
511a8556 WL |
2875 | if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) && |
2876 | ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) { | |
2877 | if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3)) | |
2878 | return 1; | |
2879 | vcpu->arch.ia32_misc_enable_msr = data; | |
2880 | kvm_update_cpuid(vcpu); | |
2881 | } else { | |
2882 | vcpu->arch.ia32_misc_enable_msr = data; | |
2883 | } | |
15c4a640 | 2884 | break; |
64d60670 PB |
2885 | case MSR_IA32_SMBASE: |
2886 | if (!msr_info->host_initiated) | |
2887 | return 1; | |
2888 | vcpu->arch.smbase = data; | |
2889 | break; | |
73f624f4 PB |
2890 | case MSR_IA32_POWER_CTL: |
2891 | vcpu->arch.msr_ia32_power_ctl = data; | |
2892 | break; | |
dd259935 PB |
2893 | case MSR_IA32_TSC: |
2894 | kvm_write_tsc(vcpu, msr_info); | |
2895 | break; | |
864e2ab2 AL |
2896 | case MSR_IA32_XSS: |
2897 | if (!msr_info->host_initiated && | |
2898 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
2899 | return 1; | |
2900 | /* | |
a1bead2a SC |
2901 | * KVM supports exposing PT to the guest, but does not support |
2902 | * IA32_XSS[bit 8]. Guests have to use RDMSR/WRMSR rather than | |
2903 | * XSAVES/XRSTORS to save/restore PT MSRs. | |
864e2ab2 | 2904 | */ |
408e9a31 | 2905 | if (data & ~supported_xss) |
864e2ab2 AL |
2906 | return 1; |
2907 | vcpu->arch.ia32_xss = data; | |
2908 | break; | |
52797bf9 LA |
2909 | case MSR_SMI_COUNT: |
2910 | if (!msr_info->host_initiated) | |
2911 | return 1; | |
2912 | vcpu->arch.smi_count = data; | |
2913 | break; | |
11c6bffa | 2914 | case MSR_KVM_WALL_CLOCK_NEW: |
18068523 GOC |
2915 | case MSR_KVM_WALL_CLOCK: |
2916 | vcpu->kvm->arch.wall_clock = data; | |
2917 | kvm_write_wall_clock(vcpu->kvm, data); | |
2918 | break; | |
11c6bffa | 2919 | case MSR_KVM_SYSTEM_TIME_NEW: |
18068523 | 2920 | case MSR_KVM_SYSTEM_TIME: { |
54750f2c MT |
2921 | struct kvm_arch *ka = &vcpu->kvm->arch; |
2922 | ||
54750f2c MT |
2923 | if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) { |
2924 | bool tmp = (msr == MSR_KVM_SYSTEM_TIME); | |
2925 | ||
2926 | if (ka->boot_vcpu_runs_old_kvmclock != tmp) | |
1bd2009e | 2927 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
54750f2c MT |
2928 | |
2929 | ka->boot_vcpu_runs_old_kvmclock = tmp; | |
2930 | } | |
2931 | ||
18068523 | 2932 | vcpu->arch.time = data; |
0061d53d | 2933 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
18068523 GOC |
2934 | |
2935 | /* we verify if the enable bit is set... */ | |
49dedf0d | 2936 | vcpu->arch.pv_time_enabled = false; |
18068523 GOC |
2937 | if (!(data & 1)) |
2938 | break; | |
2939 | ||
49dedf0d | 2940 | if (!kvm_gfn_to_hva_cache_init(vcpu->kvm, |
8f964525 AH |
2941 | &vcpu->arch.pv_time, data & ~1ULL, |
2942 | sizeof(struct pvclock_vcpu_time_info))) | |
0b79459b | 2943 | vcpu->arch.pv_time_enabled = true; |
32cad84f | 2944 | |
18068523 GOC |
2945 | break; |
2946 | } | |
344d9588 GN |
2947 | case MSR_KVM_ASYNC_PF_EN: |
2948 | if (kvm_pv_enable_async_pf(vcpu, data)) | |
2949 | return 1; | |
2950 | break; | |
2635b5c4 VK |
2951 | case MSR_KVM_ASYNC_PF_INT: |
2952 | if (kvm_pv_enable_async_pf_int(vcpu, data)) | |
2953 | return 1; | |
2954 | break; | |
557a961a VK |
2955 | case MSR_KVM_ASYNC_PF_ACK: |
2956 | if (data & 0x1) { | |
2957 | vcpu->arch.apf.pageready_pending = false; | |
2958 | kvm_check_async_pf_completion(vcpu); | |
2959 | } | |
2960 | break; | |
c9aaa895 GC |
2961 | case MSR_KVM_STEAL_TIME: |
2962 | ||
2963 | if (unlikely(!sched_info_on())) | |
2964 | return 1; | |
2965 | ||
2966 | if (data & KVM_STEAL_RESERVED_MASK) | |
2967 | return 1; | |
2968 | ||
c9aaa895 GC |
2969 | vcpu->arch.st.msr_val = data; |
2970 | ||
2971 | if (!(data & KVM_MSR_ENABLED)) | |
2972 | break; | |
2973 | ||
c9aaa895 GC |
2974 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
2975 | ||
2976 | break; | |
ae7a2a3f | 2977 | case MSR_KVM_PV_EOI_EN: |
72bbf935 | 2978 | if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8))) |
ae7a2a3f MT |
2979 | return 1; |
2980 | break; | |
c9aaa895 | 2981 | |
2d5ba19b MT |
2982 | case MSR_KVM_POLL_CONTROL: |
2983 | /* only enable bit supported */ | |
2984 | if (data & (-1ULL << 1)) | |
2985 | return 1; | |
2986 | ||
2987 | vcpu->arch.msr_kvm_poll_control = data; | |
2988 | break; | |
2989 | ||
890ca9ae HY |
2990 | case MSR_IA32_MCG_CTL: |
2991 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 2992 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
9ffd986c | 2993 | return set_msr_mce(vcpu, msr_info); |
71db6023 | 2994 | |
6912ac32 WH |
2995 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: |
2996 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
2997 | pr = true; /* fall through */ | |
2998 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: | |
2999 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3000 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3001 | return kvm_pmu_set_msr(vcpu, msr_info); |
5753785f GN |
3002 | |
3003 | if (pr || data != 0) | |
a737f256 CD |
3004 | vcpu_unimpl(vcpu, "disabled perfctr wrmsr: " |
3005 | "0x%x data 0x%llx\n", msr, data); | |
5753785f | 3006 | break; |
84e0cefa JS |
3007 | case MSR_K7_CLK_CTL: |
3008 | /* | |
3009 | * Ignore all writes to this no longer documented MSR. | |
3010 | * Writes are only relevant for old K7 processors, | |
3011 | * all pre-dating SVM, but a recommended workaround from | |
4a969980 | 3012 | * AMD for these chips. It is possible to specify the |
84e0cefa JS |
3013 | * affected processor models on the command line, hence |
3014 | * the need to ignore the workaround. | |
3015 | */ | |
3016 | break; | |
55cd8e5a | 3017 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3018 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3019 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3020 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3021 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3022 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3023 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3024 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3025 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e7d9513b AS |
3026 | return kvm_hv_set_msr_common(vcpu, msr, data, |
3027 | msr_info->host_initiated); | |
91c9c3ed | 3028 | case MSR_IA32_BBL_CR_CTL3: |
3029 | /* Drop writes to this legacy MSR -- see rdmsr | |
3030 | * counterpart for further detail. | |
3031 | */ | |
fab0aa3b EM |
3032 | if (report_ignored_msrs) |
3033 | vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n", | |
3034 | msr, data); | |
91c9c3ed | 3035 | break; |
2b036c6b | 3036 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3037 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3038 | return 1; |
3039 | vcpu->arch.osvw.length = data; | |
3040 | break; | |
3041 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3042 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b BO |
3043 | return 1; |
3044 | vcpu->arch.osvw.status = data; | |
3045 | break; | |
db2336a8 KH |
3046 | case MSR_PLATFORM_INFO: |
3047 | if (!msr_info->host_initiated || | |
db2336a8 KH |
3048 | (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) && |
3049 | cpuid_fault_enabled(vcpu))) | |
3050 | return 1; | |
3051 | vcpu->arch.msr_platform_info = data; | |
3052 | break; | |
3053 | case MSR_MISC_FEATURES_ENABLES: | |
3054 | if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT || | |
3055 | (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT && | |
3056 | !supports_cpuid_fault(vcpu))) | |
3057 | return 1; | |
3058 | vcpu->arch.msr_misc_features_enables = data; | |
3059 | break; | |
15c4a640 | 3060 | default: |
ffde22ac ES |
3061 | if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr)) |
3062 | return xen_hvm_config(vcpu, data); | |
c6702c9d | 3063 | if (kvm_pmu_is_valid_msr(vcpu, msr)) |
afd80d85 | 3064 | return kvm_pmu_set_msr(vcpu, msr_info); |
ed85c068 | 3065 | if (!ignore_msrs) { |
ae0f5499 | 3066 | vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n", |
a737f256 | 3067 | msr, data); |
ed85c068 AP |
3068 | return 1; |
3069 | } else { | |
fab0aa3b EM |
3070 | if (report_ignored_msrs) |
3071 | vcpu_unimpl(vcpu, | |
3072 | "ignored wrmsr: 0x%x data 0x%llx\n", | |
3073 | msr, data); | |
ed85c068 AP |
3074 | break; |
3075 | } | |
15c4a640 CO |
3076 | } |
3077 | return 0; | |
3078 | } | |
3079 | EXPORT_SYMBOL_GPL(kvm_set_msr_common); | |
3080 | ||
44883f01 | 3081 | static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host) |
15c4a640 CO |
3082 | { |
3083 | u64 data; | |
890ca9ae HY |
3084 | u64 mcg_cap = vcpu->arch.mcg_cap; |
3085 | unsigned bank_num = mcg_cap & 0xff; | |
15c4a640 CO |
3086 | |
3087 | switch (msr) { | |
15c4a640 CO |
3088 | case MSR_IA32_P5_MC_ADDR: |
3089 | case MSR_IA32_P5_MC_TYPE: | |
890ca9ae HY |
3090 | data = 0; |
3091 | break; | |
15c4a640 | 3092 | case MSR_IA32_MCG_CAP: |
890ca9ae HY |
3093 | data = vcpu->arch.mcg_cap; |
3094 | break; | |
c7ac679c | 3095 | case MSR_IA32_MCG_CTL: |
44883f01 | 3096 | if (!(mcg_cap & MCG_CTL_P) && !host) |
890ca9ae HY |
3097 | return 1; |
3098 | data = vcpu->arch.mcg_ctl; | |
3099 | break; | |
3100 | case MSR_IA32_MCG_STATUS: | |
3101 | data = vcpu->arch.mcg_status; | |
3102 | break; | |
3103 | default: | |
3104 | if (msr >= MSR_IA32_MC0_CTL && | |
81760dcc | 3105 | msr < MSR_IA32_MCx_CTL(bank_num)) { |
6ec4c5ee MP |
3106 | u32 offset = array_index_nospec( |
3107 | msr - MSR_IA32_MC0_CTL, | |
3108 | MSR_IA32_MCx_CTL(bank_num) - MSR_IA32_MC0_CTL); | |
3109 | ||
890ca9ae HY |
3110 | data = vcpu->arch.mce_banks[offset]; |
3111 | break; | |
3112 | } | |
3113 | return 1; | |
3114 | } | |
3115 | *pdata = data; | |
3116 | return 0; | |
3117 | } | |
3118 | ||
609e36d3 | 3119 | int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
890ca9ae | 3120 | { |
609e36d3 | 3121 | switch (msr_info->index) { |
890ca9ae | 3122 | case MSR_IA32_PLATFORM_ID: |
15c4a640 | 3123 | case MSR_IA32_EBL_CR_POWERON: |
b5e2fec0 AG |
3124 | case MSR_IA32_DEBUGCTLMSR: |
3125 | case MSR_IA32_LASTBRANCHFROMIP: | |
3126 | case MSR_IA32_LASTBRANCHTOIP: | |
3127 | case MSR_IA32_LASTINTFROMIP: | |
3128 | case MSR_IA32_LASTINTTOIP: | |
60af2ecd | 3129 | case MSR_K8_SYSCFG: |
3afb1121 PB |
3130 | case MSR_K8_TSEG_ADDR: |
3131 | case MSR_K8_TSEG_MASK: | |
61a6bd67 | 3132 | case MSR_VM_HSAVE_PA: |
1fdbd48c | 3133 | case MSR_K8_INT_PENDING_MSG: |
c323c0e5 | 3134 | case MSR_AMD64_NB_CFG: |
f7c6d140 | 3135 | case MSR_FAM10H_MMIO_CONF_BASE: |
2e32b719 | 3136 | case MSR_AMD64_BU_CFG2: |
0c2df2a1 | 3137 | case MSR_IA32_PERF_CTL: |
405a353a | 3138 | case MSR_AMD64_DC_CFG: |
0e1b869f | 3139 | case MSR_F15H_EX_CFG: |
2ca1a06a VS |
3140 | /* |
3141 | * Intel Sandy Bridge CPUs must support the RAPL (running average power | |
3142 | * limit) MSRs. Just return 0, as we do not want to expose the host | |
3143 | * data here. Do not conditionalize this on CPUID, as KVM does not do | |
3144 | * so for existing CPU-specific MSRs. | |
3145 | */ | |
3146 | case MSR_RAPL_POWER_UNIT: | |
3147 | case MSR_PP0_ENERGY_STATUS: /* Power plane 0 (core) */ | |
3148 | case MSR_PP1_ENERGY_STATUS: /* Power plane 1 (graphics uncore) */ | |
3149 | case MSR_PKG_ENERGY_STATUS: /* Total package */ | |
3150 | case MSR_DRAM_ENERGY_STATUS: /* DRAM controller */ | |
609e36d3 | 3151 | msr_info->data = 0; |
15c4a640 | 3152 | break; |
c51eb52b | 3153 | case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5: |
6912ac32 WH |
3154 | case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3: |
3155 | case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3: | |
3156 | case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1: | |
3157 | case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1: | |
c6702c9d | 3158 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3159 | return kvm_pmu_get_msr(vcpu, msr_info); |
609e36d3 | 3160 | msr_info->data = 0; |
5753785f | 3161 | break; |
742bc670 | 3162 | case MSR_IA32_UCODE_REV: |
518e7b94 | 3163 | msr_info->data = vcpu->arch.microcode_version; |
742bc670 | 3164 | break; |
0cf9135b SC |
3165 | case MSR_IA32_ARCH_CAPABILITIES: |
3166 | if (!msr_info->host_initiated && | |
3167 | !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES)) | |
3168 | return 1; | |
3169 | msr_info->data = vcpu->arch.arch_capabilities; | |
3170 | break; | |
73f624f4 PB |
3171 | case MSR_IA32_POWER_CTL: |
3172 | msr_info->data = vcpu->arch.msr_ia32_power_ctl; | |
3173 | break; | |
dd259935 PB |
3174 | case MSR_IA32_TSC: |
3175 | msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset; | |
3176 | break; | |
9ba075a6 | 3177 | case MSR_MTRRcap: |
9ba075a6 | 3178 | case 0x200 ... 0x2ff: |
ff53604b | 3179 | return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data); |
15c4a640 | 3180 | case 0xcd: /* fsb frequency */ |
609e36d3 | 3181 | msr_info->data = 3; |
15c4a640 | 3182 | break; |
7b914098 JS |
3183 | /* |
3184 | * MSR_EBC_FREQUENCY_ID | |
3185 | * Conservative value valid for even the basic CPU models. | |
3186 | * Models 0,1: 000 in bits 23:21 indicating a bus speed of | |
3187 | * 100MHz, model 2 000 in bits 18:16 indicating 100MHz, | |
3188 | * and 266MHz for model 3, or 4. Set Core Clock | |
3189 | * Frequency to System Bus Frequency Ratio to 1 (bits | |
3190 | * 31:24) even though these are only valid for CPU | |
3191 | * models > 2, however guests may end up dividing or | |
3192 | * multiplying by zero otherwise. | |
3193 | */ | |
3194 | case MSR_EBC_FREQUENCY_ID: | |
609e36d3 | 3195 | msr_info->data = 1 << 24; |
7b914098 | 3196 | break; |
15c4a640 | 3197 | case MSR_IA32_APICBASE: |
609e36d3 | 3198 | msr_info->data = kvm_get_apic_base(vcpu); |
15c4a640 | 3199 | break; |
0105d1a5 | 3200 | case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff: |
609e36d3 | 3201 | return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data); |
a3e06bbe | 3202 | case MSR_IA32_TSCDEADLINE: |
609e36d3 | 3203 | msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu); |
a3e06bbe | 3204 | break; |
ba904635 | 3205 | case MSR_IA32_TSC_ADJUST: |
609e36d3 | 3206 | msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr; |
ba904635 | 3207 | break; |
15c4a640 | 3208 | case MSR_IA32_MISC_ENABLE: |
609e36d3 | 3209 | msr_info->data = vcpu->arch.ia32_misc_enable_msr; |
15c4a640 | 3210 | break; |
64d60670 PB |
3211 | case MSR_IA32_SMBASE: |
3212 | if (!msr_info->host_initiated) | |
3213 | return 1; | |
3214 | msr_info->data = vcpu->arch.smbase; | |
15c4a640 | 3215 | break; |
52797bf9 LA |
3216 | case MSR_SMI_COUNT: |
3217 | msr_info->data = vcpu->arch.smi_count; | |
3218 | break; | |
847f0ad8 AG |
3219 | case MSR_IA32_PERF_STATUS: |
3220 | /* TSC increment by tick */ | |
609e36d3 | 3221 | msr_info->data = 1000ULL; |
847f0ad8 | 3222 | /* CPU multiplier */ |
b0996ae4 | 3223 | msr_info->data |= (((uint64_t)4ULL) << 40); |
847f0ad8 | 3224 | break; |
15c4a640 | 3225 | case MSR_EFER: |
609e36d3 | 3226 | msr_info->data = vcpu->arch.efer; |
15c4a640 | 3227 | break; |
18068523 | 3228 | case MSR_KVM_WALL_CLOCK: |
11c6bffa | 3229 | case MSR_KVM_WALL_CLOCK_NEW: |
609e36d3 | 3230 | msr_info->data = vcpu->kvm->arch.wall_clock; |
18068523 GOC |
3231 | break; |
3232 | case MSR_KVM_SYSTEM_TIME: | |
11c6bffa | 3233 | case MSR_KVM_SYSTEM_TIME_NEW: |
609e36d3 | 3234 | msr_info->data = vcpu->arch.time; |
18068523 | 3235 | break; |
344d9588 | 3236 | case MSR_KVM_ASYNC_PF_EN: |
2635b5c4 VK |
3237 | msr_info->data = vcpu->arch.apf.msr_en_val; |
3238 | break; | |
3239 | case MSR_KVM_ASYNC_PF_INT: | |
3240 | msr_info->data = vcpu->arch.apf.msr_int_val; | |
344d9588 | 3241 | break; |
557a961a VK |
3242 | case MSR_KVM_ASYNC_PF_ACK: |
3243 | msr_info->data = 0; | |
3244 | break; | |
c9aaa895 | 3245 | case MSR_KVM_STEAL_TIME: |
609e36d3 | 3246 | msr_info->data = vcpu->arch.st.msr_val; |
c9aaa895 | 3247 | break; |
1d92128f | 3248 | case MSR_KVM_PV_EOI_EN: |
609e36d3 | 3249 | msr_info->data = vcpu->arch.pv_eoi.msr_val; |
1d92128f | 3250 | break; |
2d5ba19b MT |
3251 | case MSR_KVM_POLL_CONTROL: |
3252 | msr_info->data = vcpu->arch.msr_kvm_poll_control; | |
3253 | break; | |
890ca9ae HY |
3254 | case MSR_IA32_P5_MC_ADDR: |
3255 | case MSR_IA32_P5_MC_TYPE: | |
3256 | case MSR_IA32_MCG_CAP: | |
3257 | case MSR_IA32_MCG_CTL: | |
3258 | case MSR_IA32_MCG_STATUS: | |
81760dcc | 3259 | case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1: |
44883f01 PB |
3260 | return get_msr_mce(vcpu, msr_info->index, &msr_info->data, |
3261 | msr_info->host_initiated); | |
864e2ab2 AL |
3262 | case MSR_IA32_XSS: |
3263 | if (!msr_info->host_initiated && | |
3264 | !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)) | |
3265 | return 1; | |
3266 | msr_info->data = vcpu->arch.ia32_xss; | |
3267 | break; | |
84e0cefa JS |
3268 | case MSR_K7_CLK_CTL: |
3269 | /* | |
3270 | * Provide expected ramp-up count for K7. All other | |
3271 | * are set to zero, indicating minimum divisors for | |
3272 | * every field. | |
3273 | * | |
3274 | * This prevents guest kernels on AMD host with CPU | |
3275 | * type 6, model 8 and higher from exploding due to | |
3276 | * the rdmsr failing. | |
3277 | */ | |
609e36d3 | 3278 | msr_info->data = 0x20000000; |
84e0cefa | 3279 | break; |
55cd8e5a | 3280 | case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15: |
f97f5a56 JD |
3281 | case HV_X64_MSR_SYNDBG_CONTROL ... HV_X64_MSR_SYNDBG_PENDING_BUFFER: |
3282 | case HV_X64_MSR_SYNDBG_OPTIONS: | |
e7d9513b AS |
3283 | case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4: |
3284 | case HV_X64_MSR_CRASH_CTL: | |
1f4b34f8 | 3285 | case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT: |
a2e164e7 VK |
3286 | case HV_X64_MSR_REENLIGHTENMENT_CONTROL: |
3287 | case HV_X64_MSR_TSC_EMULATION_CONTROL: | |
3288 | case HV_X64_MSR_TSC_EMULATION_STATUS: | |
e83d5887 | 3289 | return kvm_hv_get_msr_common(vcpu, |
44883f01 PB |
3290 | msr_info->index, &msr_info->data, |
3291 | msr_info->host_initiated); | |
91c9c3ed | 3292 | case MSR_IA32_BBL_CR_CTL3: |
3293 | /* This legacy MSR exists but isn't fully documented in current | |
3294 | * silicon. It is however accessed by winxp in very narrow | |
3295 | * scenarios where it sets bit #19, itself documented as | |
3296 | * a "reserved" bit. Best effort attempt to source coherent | |
3297 | * read data here should the balance of the register be | |
3298 | * interpreted by the guest: | |
3299 | * | |
3300 | * L2 cache control register 3: 64GB range, 256KB size, | |
3301 | * enabled, latency 0x1, configured | |
3302 | */ | |
609e36d3 | 3303 | msr_info->data = 0xbe702111; |
91c9c3ed | 3304 | break; |
2b036c6b | 3305 | case MSR_AMD64_OSVW_ID_LENGTH: |
d6321d49 | 3306 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 3307 | return 1; |
609e36d3 | 3308 | msr_info->data = vcpu->arch.osvw.length; |
2b036c6b BO |
3309 | break; |
3310 | case MSR_AMD64_OSVW_STATUS: | |
d6321d49 | 3311 | if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW)) |
2b036c6b | 3312 | return 1; |
609e36d3 | 3313 | msr_info->data = vcpu->arch.osvw.status; |
2b036c6b | 3314 | break; |
db2336a8 | 3315 | case MSR_PLATFORM_INFO: |
6fbbde9a DS |
3316 | if (!msr_info->host_initiated && |
3317 | !vcpu->kvm->arch.guest_can_read_msr_platform_info) | |
3318 | return 1; | |
db2336a8 KH |
3319 | msr_info->data = vcpu->arch.msr_platform_info; |
3320 | break; | |
3321 | case MSR_MISC_FEATURES_ENABLES: | |
3322 | msr_info->data = vcpu->arch.msr_misc_features_enables; | |
3323 | break; | |
191c8137 BP |
3324 | case MSR_K7_HWCR: |
3325 | msr_info->data = vcpu->arch.msr_hwcr; | |
3326 | break; | |
15c4a640 | 3327 | default: |
c6702c9d | 3328 | if (kvm_pmu_is_valid_msr(vcpu, msr_info->index)) |
cbd71758 | 3329 | return kvm_pmu_get_msr(vcpu, msr_info); |
ed85c068 | 3330 | if (!ignore_msrs) { |
ae0f5499 BD |
3331 | vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n", |
3332 | msr_info->index); | |
ed85c068 AP |
3333 | return 1; |
3334 | } else { | |
fab0aa3b EM |
3335 | if (report_ignored_msrs) |
3336 | vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n", | |
3337 | msr_info->index); | |
609e36d3 | 3338 | msr_info->data = 0; |
ed85c068 AP |
3339 | } |
3340 | break; | |
15c4a640 | 3341 | } |
15c4a640 CO |
3342 | return 0; |
3343 | } | |
3344 | EXPORT_SYMBOL_GPL(kvm_get_msr_common); | |
3345 | ||
313a3dc7 CO |
3346 | /* |
3347 | * Read or write a bunch of msrs. All parameters are kernel addresses. | |
3348 | * | |
3349 | * @return number of msrs set successfully. | |
3350 | */ | |
3351 | static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs, | |
3352 | struct kvm_msr_entry *entries, | |
3353 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
3354 | unsigned index, u64 *data)) | |
3355 | { | |
801e459a | 3356 | int i; |
313a3dc7 | 3357 | |
313a3dc7 CO |
3358 | for (i = 0; i < msrs->nmsrs; ++i) |
3359 | if (do_msr(vcpu, entries[i].index, &entries[i].data)) | |
3360 | break; | |
3361 | ||
313a3dc7 CO |
3362 | return i; |
3363 | } | |
3364 | ||
3365 | /* | |
3366 | * Read or write a bunch of msrs. Parameters are user addresses. | |
3367 | * | |
3368 | * @return number of msrs set successfully. | |
3369 | */ | |
3370 | static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs, | |
3371 | int (*do_msr)(struct kvm_vcpu *vcpu, | |
3372 | unsigned index, u64 *data), | |
3373 | int writeback) | |
3374 | { | |
3375 | struct kvm_msrs msrs; | |
3376 | struct kvm_msr_entry *entries; | |
3377 | int r, n; | |
3378 | unsigned size; | |
3379 | ||
3380 | r = -EFAULT; | |
0e96f31e | 3381 | if (copy_from_user(&msrs, user_msrs, sizeof(msrs))) |
313a3dc7 CO |
3382 | goto out; |
3383 | ||
3384 | r = -E2BIG; | |
3385 | if (msrs.nmsrs >= MAX_IO_MSRS) | |
3386 | goto out; | |
3387 | ||
313a3dc7 | 3388 | size = sizeof(struct kvm_msr_entry) * msrs.nmsrs; |
ff5c2c03 SL |
3389 | entries = memdup_user(user_msrs->entries, size); |
3390 | if (IS_ERR(entries)) { | |
3391 | r = PTR_ERR(entries); | |
313a3dc7 | 3392 | goto out; |
ff5c2c03 | 3393 | } |
313a3dc7 CO |
3394 | |
3395 | r = n = __msr_io(vcpu, &msrs, entries, do_msr); | |
3396 | if (r < 0) | |
3397 | goto out_free; | |
3398 | ||
3399 | r = -EFAULT; | |
3400 | if (writeback && copy_to_user(user_msrs->entries, entries, size)) | |
3401 | goto out_free; | |
3402 | ||
3403 | r = n; | |
3404 | ||
3405 | out_free: | |
7a73c028 | 3406 | kfree(entries); |
313a3dc7 CO |
3407 | out: |
3408 | return r; | |
3409 | } | |
3410 | ||
4d5422ce WL |
3411 | static inline bool kvm_can_mwait_in_guest(void) |
3412 | { | |
3413 | return boot_cpu_has(X86_FEATURE_MWAIT) && | |
8e9b29b6 KA |
3414 | !boot_cpu_has_bug(X86_BUG_MONITOR) && |
3415 | boot_cpu_has(X86_FEATURE_ARAT); | |
4d5422ce WL |
3416 | } |
3417 | ||
784aa3d7 | 3418 | int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) |
018d00d2 | 3419 | { |
4d5422ce | 3420 | int r = 0; |
018d00d2 ZX |
3421 | |
3422 | switch (ext) { | |
3423 | case KVM_CAP_IRQCHIP: | |
3424 | case KVM_CAP_HLT: | |
3425 | case KVM_CAP_MMU_SHADOW_CACHE_CONTROL: | |
018d00d2 | 3426 | case KVM_CAP_SET_TSS_ADDR: |
07716717 | 3427 | case KVM_CAP_EXT_CPUID: |
9c15bb1d | 3428 | case KVM_CAP_EXT_EMUL_CPUID: |
c8076604 | 3429 | case KVM_CAP_CLOCKSOURCE: |
7837699f | 3430 | case KVM_CAP_PIT: |
a28e4f5a | 3431 | case KVM_CAP_NOP_IO_DELAY: |
62d9f0db | 3432 | case KVM_CAP_MP_STATE: |
ed848624 | 3433 | case KVM_CAP_SYNC_MMU: |
a355c85c | 3434 | case KVM_CAP_USER_NMI: |
52d939a0 | 3435 | case KVM_CAP_REINJECT_CONTROL: |
4925663a | 3436 | case KVM_CAP_IRQ_INJECT_STATUS: |
d34e6b17 | 3437 | case KVM_CAP_IOEVENTFD: |
f848a5a8 | 3438 | case KVM_CAP_IOEVENTFD_NO_LENGTH: |
c5ff41ce | 3439 | case KVM_CAP_PIT2: |
e9f42757 | 3440 | case KVM_CAP_PIT_STATE2: |
b927a3ce | 3441 | case KVM_CAP_SET_IDENTITY_MAP_ADDR: |
ffde22ac | 3442 | case KVM_CAP_XEN_HVM: |
3cfc3092 | 3443 | case KVM_CAP_VCPU_EVENTS: |
55cd8e5a | 3444 | case KVM_CAP_HYPERV: |
10388a07 | 3445 | case KVM_CAP_HYPERV_VAPIC: |
c25bc163 | 3446 | case KVM_CAP_HYPERV_SPIN: |
5c919412 | 3447 | case KVM_CAP_HYPERV_SYNIC: |
efc479e6 | 3448 | case KVM_CAP_HYPERV_SYNIC2: |
d3457c87 | 3449 | case KVM_CAP_HYPERV_VP_INDEX: |
faeb7833 | 3450 | case KVM_CAP_HYPERV_EVENTFD: |
c1aea919 | 3451 | case KVM_CAP_HYPERV_TLBFLUSH: |
214ff83d | 3452 | case KVM_CAP_HYPERV_SEND_IPI: |
2bc39970 | 3453 | case KVM_CAP_HYPERV_CPUID: |
ab9f4ecb | 3454 | case KVM_CAP_PCI_SEGMENT: |
a1efbe77 | 3455 | case KVM_CAP_DEBUGREGS: |
d2be1651 | 3456 | case KVM_CAP_X86_ROBUST_SINGLESTEP: |
2d5b5a66 | 3457 | case KVM_CAP_XSAVE: |
344d9588 | 3458 | case KVM_CAP_ASYNC_PF: |
72de5fa4 | 3459 | case KVM_CAP_ASYNC_PF_INT: |
92a1f12d | 3460 | case KVM_CAP_GET_TSC_KHZ: |
1c0b28c2 | 3461 | case KVM_CAP_KVMCLOCK_CTRL: |
4d8b81ab | 3462 | case KVM_CAP_READONLY_MEM: |
5f66b620 | 3463 | case KVM_CAP_HYPERV_TIME: |
100943c5 | 3464 | case KVM_CAP_IOAPIC_POLARITY_IGNORED: |
defcf51f | 3465 | case KVM_CAP_TSC_DEADLINE_TIMER: |
90de4a18 | 3466 | case KVM_CAP_DISABLE_QUIRKS: |
d71ba788 | 3467 | case KVM_CAP_SET_BOOT_CPU_ID: |
49df6397 | 3468 | case KVM_CAP_SPLIT_IRQCHIP: |
460df4c1 | 3469 | case KVM_CAP_IMMEDIATE_EXIT: |
66bb8a06 | 3470 | case KVM_CAP_PMU_EVENT_FILTER: |
801e459a | 3471 | case KVM_CAP_GET_MSR_FEATURES: |
6fbbde9a | 3472 | case KVM_CAP_MSR_PLATFORM_INFO: |
c4f55198 | 3473 | case KVM_CAP_EXCEPTION_PAYLOAD: |
b9b2782c | 3474 | case KVM_CAP_SET_GUEST_DEBUG: |
018d00d2 ZX |
3475 | r = 1; |
3476 | break; | |
01643c51 KH |
3477 | case KVM_CAP_SYNC_REGS: |
3478 | r = KVM_SYNC_X86_VALID_FIELDS; | |
3479 | break; | |
e3fd9a93 PB |
3480 | case KVM_CAP_ADJUST_CLOCK: |
3481 | r = KVM_CLOCK_TSC_STABLE; | |
3482 | break; | |
4d5422ce | 3483 | case KVM_CAP_X86_DISABLE_EXITS: |
b5170063 WL |
3484 | r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE | |
3485 | KVM_X86_DISABLE_EXITS_CSTATE; | |
4d5422ce WL |
3486 | if(kvm_can_mwait_in_guest()) |
3487 | r |= KVM_X86_DISABLE_EXITS_MWAIT; | |
668fffa3 | 3488 | break; |
6d396b55 PB |
3489 | case KVM_CAP_X86_SMM: |
3490 | /* SMBASE is usually relocated above 1M on modern chipsets, | |
3491 | * and SMM handlers might indeed rely on 4G segment limits, | |
3492 | * so do not report SMM to be available if real mode is | |
3493 | * emulated via vm86 mode. Still, do not go to great lengths | |
3494 | * to avoid userspace's usage of the feature, because it is a | |
3495 | * fringe case that is not enabled except via specific settings | |
3496 | * of the module parameters. | |
3497 | */ | |
afaf0b2f | 3498 | r = kvm_x86_ops.has_emulated_msr(MSR_IA32_SMBASE); |
6d396b55 | 3499 | break; |
774ead3a | 3500 | case KVM_CAP_VAPIC: |
afaf0b2f | 3501 | r = !kvm_x86_ops.cpu_has_accelerated_tpr(); |
774ead3a | 3502 | break; |
f725230a | 3503 | case KVM_CAP_NR_VCPUS: |
8c3ba334 SL |
3504 | r = KVM_SOFT_MAX_VCPUS; |
3505 | break; | |
3506 | case KVM_CAP_MAX_VCPUS: | |
f725230a AK |
3507 | r = KVM_MAX_VCPUS; |
3508 | break; | |
a86cb413 TH |
3509 | case KVM_CAP_MAX_VCPU_ID: |
3510 | r = KVM_MAX_VCPU_ID; | |
3511 | break; | |
a68a6a72 MT |
3512 | case KVM_CAP_PV_MMU: /* obsolete */ |
3513 | r = 0; | |
2f333bcb | 3514 | break; |
890ca9ae HY |
3515 | case KVM_CAP_MCE: |
3516 | r = KVM_MAX_MCE_BANKS; | |
3517 | break; | |
2d5b5a66 | 3518 | case KVM_CAP_XCRS: |
d366bf7e | 3519 | r = boot_cpu_has(X86_FEATURE_XSAVE); |
2d5b5a66 | 3520 | break; |
92a1f12d JR |
3521 | case KVM_CAP_TSC_CONTROL: |
3522 | r = kvm_has_tsc_control; | |
3523 | break; | |
37131313 RK |
3524 | case KVM_CAP_X2APIC_API: |
3525 | r = KVM_X2APIC_API_VALID_FLAGS; | |
3526 | break; | |
8fcc4b59 | 3527 | case KVM_CAP_NESTED_STATE: |
33b22172 PB |
3528 | r = kvm_x86_ops.nested_ops->get_state ? |
3529 | kvm_x86_ops.nested_ops->get_state(NULL, NULL, 0) : 0; | |
8fcc4b59 | 3530 | break; |
344c6c80 | 3531 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 3532 | r = kvm_x86_ops.enable_direct_tlbflush != NULL; |
5a0165f6 VK |
3533 | break; |
3534 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: | |
33b22172 | 3535 | r = kvm_x86_ops.nested_ops->enable_evmcs != NULL; |
344c6c80 | 3536 | break; |
018d00d2 | 3537 | default: |
018d00d2 ZX |
3538 | break; |
3539 | } | |
3540 | return r; | |
3541 | ||
3542 | } | |
3543 | ||
043405e1 CO |
3544 | long kvm_arch_dev_ioctl(struct file *filp, |
3545 | unsigned int ioctl, unsigned long arg) | |
3546 | { | |
3547 | void __user *argp = (void __user *)arg; | |
3548 | long r; | |
3549 | ||
3550 | switch (ioctl) { | |
3551 | case KVM_GET_MSR_INDEX_LIST: { | |
3552 | struct kvm_msr_list __user *user_msr_list = argp; | |
3553 | struct kvm_msr_list msr_list; | |
3554 | unsigned n; | |
3555 | ||
3556 | r = -EFAULT; | |
0e96f31e | 3557 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) |
043405e1 CO |
3558 | goto out; |
3559 | n = msr_list.nmsrs; | |
62ef68bb | 3560 | msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs; |
0e96f31e | 3561 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) |
043405e1 CO |
3562 | goto out; |
3563 | r = -E2BIG; | |
e125e7b6 | 3564 | if (n < msr_list.nmsrs) |
043405e1 CO |
3565 | goto out; |
3566 | r = -EFAULT; | |
3567 | if (copy_to_user(user_msr_list->indices, &msrs_to_save, | |
3568 | num_msrs_to_save * sizeof(u32))) | |
3569 | goto out; | |
e125e7b6 | 3570 | if (copy_to_user(user_msr_list->indices + num_msrs_to_save, |
043405e1 | 3571 | &emulated_msrs, |
62ef68bb | 3572 | num_emulated_msrs * sizeof(u32))) |
043405e1 CO |
3573 | goto out; |
3574 | r = 0; | |
3575 | break; | |
3576 | } | |
9c15bb1d BP |
3577 | case KVM_GET_SUPPORTED_CPUID: |
3578 | case KVM_GET_EMULATED_CPUID: { | |
674eea0f AK |
3579 | struct kvm_cpuid2 __user *cpuid_arg = argp; |
3580 | struct kvm_cpuid2 cpuid; | |
3581 | ||
3582 | r = -EFAULT; | |
0e96f31e | 3583 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
674eea0f | 3584 | goto out; |
9c15bb1d BP |
3585 | |
3586 | r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries, | |
3587 | ioctl); | |
674eea0f AK |
3588 | if (r) |
3589 | goto out; | |
3590 | ||
3591 | r = -EFAULT; | |
0e96f31e | 3592 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
674eea0f AK |
3593 | goto out; |
3594 | r = 0; | |
3595 | break; | |
3596 | } | |
cf6c26ec | 3597 | case KVM_X86_GET_MCE_CAP_SUPPORTED: |
890ca9ae | 3598 | r = -EFAULT; |
c45dcc71 AR |
3599 | if (copy_to_user(argp, &kvm_mce_cap_supported, |
3600 | sizeof(kvm_mce_cap_supported))) | |
890ca9ae HY |
3601 | goto out; |
3602 | r = 0; | |
3603 | break; | |
801e459a TL |
3604 | case KVM_GET_MSR_FEATURE_INDEX_LIST: { |
3605 | struct kvm_msr_list __user *user_msr_list = argp; | |
3606 | struct kvm_msr_list msr_list; | |
3607 | unsigned int n; | |
3608 | ||
3609 | r = -EFAULT; | |
3610 | if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list))) | |
3611 | goto out; | |
3612 | n = msr_list.nmsrs; | |
3613 | msr_list.nmsrs = num_msr_based_features; | |
3614 | if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list))) | |
3615 | goto out; | |
3616 | r = -E2BIG; | |
3617 | if (n < msr_list.nmsrs) | |
3618 | goto out; | |
3619 | r = -EFAULT; | |
3620 | if (copy_to_user(user_msr_list->indices, &msr_based_features, | |
3621 | num_msr_based_features * sizeof(u32))) | |
3622 | goto out; | |
3623 | r = 0; | |
3624 | break; | |
3625 | } | |
3626 | case KVM_GET_MSRS: | |
3627 | r = msr_io(NULL, argp, do_get_msr_feature, 1); | |
3628 | break; | |
043405e1 CO |
3629 | default: |
3630 | r = -EINVAL; | |
cf6c26ec | 3631 | break; |
043405e1 CO |
3632 | } |
3633 | out: | |
3634 | return r; | |
3635 | } | |
3636 | ||
f5f48ee1 SY |
3637 | static void wbinvd_ipi(void *garbage) |
3638 | { | |
3639 | wbinvd(); | |
3640 | } | |
3641 | ||
3642 | static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
3643 | { | |
e0f0bbc5 | 3644 | return kvm_arch_has_noncoherent_dma(vcpu->kvm); |
f5f48ee1 SY |
3645 | } |
3646 | ||
313a3dc7 CO |
3647 | void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
3648 | { | |
f5f48ee1 SY |
3649 | /* Address WBINVD may be executed by guest */ |
3650 | if (need_emulate_wbinvd(vcpu)) { | |
afaf0b2f | 3651 | if (kvm_x86_ops.has_wbinvd_exit()) |
f5f48ee1 SY |
3652 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); |
3653 | else if (vcpu->cpu != -1 && vcpu->cpu != cpu) | |
3654 | smp_call_function_single(vcpu->cpu, | |
3655 | wbinvd_ipi, NULL, 1); | |
3656 | } | |
3657 | ||
afaf0b2f | 3658 | kvm_x86_ops.vcpu_load(vcpu, cpu); |
8f6055cb | 3659 | |
37486135 BM |
3660 | /* Save host pkru register if supported */ |
3661 | vcpu->arch.host_pkru = read_pkru(); | |
3662 | ||
0dd6a6ed ZA |
3663 | /* Apply any externally detected TSC adjustments (due to suspend) */ |
3664 | if (unlikely(vcpu->arch.tsc_offset_adjustment)) { | |
3665 | adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment); | |
3666 | vcpu->arch.tsc_offset_adjustment = 0; | |
105b21bb | 3667 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed | 3668 | } |
8f6055cb | 3669 | |
b0c39dc6 | 3670 | if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) { |
6f526ec5 | 3671 | s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 : |
4ea1636b | 3672 | rdtsc() - vcpu->arch.last_host_tsc; |
e48672fa ZA |
3673 | if (tsc_delta < 0) |
3674 | mark_tsc_unstable("KVM discovered backwards TSC"); | |
ce7a058a | 3675 | |
b0c39dc6 | 3676 | if (kvm_check_tsc_unstable()) { |
07c1419a | 3677 | u64 offset = kvm_compute_tsc_offset(vcpu, |
b183aa58 | 3678 | vcpu->arch.last_guest_tsc); |
a545ab6a | 3679 | kvm_vcpu_write_tsc_offset(vcpu, offset); |
c285545f | 3680 | vcpu->arch.tsc_catchup = 1; |
c285545f | 3681 | } |
a749e247 PB |
3682 | |
3683 | if (kvm_lapic_hv_timer_in_use(vcpu)) | |
3684 | kvm_lapic_restart_hv_timer(vcpu); | |
3685 | ||
d98d07ca MT |
3686 | /* |
3687 | * On a host with synchronized TSC, there is no need to update | |
3688 | * kvmclock on vcpu->cpu migration | |
3689 | */ | |
3690 | if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1) | |
0061d53d | 3691 | kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu); |
c285545f | 3692 | if (vcpu->cpu != cpu) |
1bd2009e | 3693 | kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu); |
e48672fa | 3694 | vcpu->cpu = cpu; |
6b7d7e76 | 3695 | } |
c9aaa895 | 3696 | |
c9aaa895 | 3697 | kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu); |
313a3dc7 CO |
3698 | } |
3699 | ||
0b9f6c46 PX |
3700 | static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu) |
3701 | { | |
b0431382 BO |
3702 | struct kvm_host_map map; |
3703 | struct kvm_steal_time *st; | |
3704 | ||
0b9f6c46 PX |
3705 | if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED)) |
3706 | return; | |
3707 | ||
a6bd811f | 3708 | if (vcpu->arch.st.preempted) |
8c6de56a BO |
3709 | return; |
3710 | ||
b0431382 BO |
3711 | if (kvm_map_gfn(vcpu, vcpu->arch.st.msr_val >> PAGE_SHIFT, &map, |
3712 | &vcpu->arch.st.cache, true)) | |
3713 | return; | |
3714 | ||
3715 | st = map.hva + | |
3716 | offset_in_page(vcpu->arch.st.msr_val & KVM_STEAL_VALID_BITS); | |
0b9f6c46 | 3717 | |
a6bd811f | 3718 | st->preempted = vcpu->arch.st.preempted = KVM_VCPU_PREEMPTED; |
0b9f6c46 | 3719 | |
b0431382 | 3720 | kvm_unmap_gfn(vcpu, &map, &vcpu->arch.st.cache, true, true); |
0b9f6c46 PX |
3721 | } |
3722 | ||
313a3dc7 CO |
3723 | void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) |
3724 | { | |
cc0d907c | 3725 | int idx; |
de63ad4c LM |
3726 | |
3727 | if (vcpu->preempted) | |
afaf0b2f | 3728 | vcpu->arch.preempted_in_kernel = !kvm_x86_ops.get_cpl(vcpu); |
de63ad4c | 3729 | |
931f261b AA |
3730 | /* |
3731 | * Disable page faults because we're in atomic context here. | |
3732 | * kvm_write_guest_offset_cached() would call might_fault() | |
3733 | * that relies on pagefault_disable() to tell if there's a | |
3734 | * bug. NOTE: the write to guest memory may not go through if | |
3735 | * during postcopy live migration or if there's heavy guest | |
3736 | * paging. | |
3737 | */ | |
3738 | pagefault_disable(); | |
cc0d907c AA |
3739 | /* |
3740 | * kvm_memslots() will be called by | |
3741 | * kvm_write_guest_offset_cached() so take the srcu lock. | |
3742 | */ | |
3743 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
0b9f6c46 | 3744 | kvm_steal_time_set_preempted(vcpu); |
cc0d907c | 3745 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
931f261b | 3746 | pagefault_enable(); |
afaf0b2f | 3747 | kvm_x86_ops.vcpu_put(vcpu); |
4ea1636b | 3748 | vcpu->arch.last_host_tsc = rdtsc(); |
efdab992 | 3749 | /* |
f9dcf08e RK |
3750 | * If userspace has set any breakpoints or watchpoints, dr6 is restored |
3751 | * on every vmexit, but if not, we might have a stale dr6 from the | |
3752 | * guest. do_debug expects dr6 to be cleared after it runs, do the same. | |
efdab992 | 3753 | */ |
f9dcf08e | 3754 | set_debugreg(0, 6); |
313a3dc7 CO |
3755 | } |
3756 | ||
313a3dc7 CO |
3757 | static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu, |
3758 | struct kvm_lapic_state *s) | |
3759 | { | |
fa59cc00 | 3760 | if (vcpu->arch.apicv_active) |
afaf0b2f | 3761 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
d62caabb | 3762 | |
a92e2543 | 3763 | return kvm_apic_get_state(vcpu, s); |
313a3dc7 CO |
3764 | } |
3765 | ||
3766 | static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu, | |
3767 | struct kvm_lapic_state *s) | |
3768 | { | |
a92e2543 RK |
3769 | int r; |
3770 | ||
3771 | r = kvm_apic_set_state(vcpu, s); | |
3772 | if (r) | |
3773 | return r; | |
cb142eb7 | 3774 | update_cr8_intercept(vcpu); |
313a3dc7 CO |
3775 | |
3776 | return 0; | |
3777 | } | |
3778 | ||
127a457a MG |
3779 | static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu) |
3780 | { | |
3781 | return (!lapic_in_kernel(vcpu) || | |
3782 | kvm_apic_accept_pic_intr(vcpu)); | |
3783 | } | |
3784 | ||
782d422b MG |
3785 | /* |
3786 | * if userspace requested an interrupt window, check that the | |
3787 | * interrupt window is open. | |
3788 | * | |
3789 | * No need to exit to userspace if we already have an interrupt queued. | |
3790 | */ | |
3791 | static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu) | |
3792 | { | |
3793 | return kvm_arch_interrupt_allowed(vcpu) && | |
3794 | !kvm_cpu_has_interrupt(vcpu) && | |
3795 | !kvm_event_needs_reinjection(vcpu) && | |
3796 | kvm_cpu_accept_dm_intr(vcpu); | |
3797 | } | |
3798 | ||
f77bc6a4 ZX |
3799 | static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu, |
3800 | struct kvm_interrupt *irq) | |
3801 | { | |
02cdb50f | 3802 | if (irq->irq >= KVM_NR_INTERRUPTS) |
f77bc6a4 | 3803 | return -EINVAL; |
1c1a9ce9 SR |
3804 | |
3805 | if (!irqchip_in_kernel(vcpu->kvm)) { | |
3806 | kvm_queue_interrupt(vcpu, irq->irq, false); | |
3807 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
3808 | return 0; | |
3809 | } | |
3810 | ||
3811 | /* | |
3812 | * With in-kernel LAPIC, we only use this to inject EXTINT, so | |
3813 | * fail for in-kernel 8259. | |
3814 | */ | |
3815 | if (pic_in_kernel(vcpu->kvm)) | |
f77bc6a4 | 3816 | return -ENXIO; |
f77bc6a4 | 3817 | |
1c1a9ce9 SR |
3818 | if (vcpu->arch.pending_external_vector != -1) |
3819 | return -EEXIST; | |
f77bc6a4 | 3820 | |
1c1a9ce9 | 3821 | vcpu->arch.pending_external_vector = irq->irq; |
934bf653 | 3822 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
f77bc6a4 ZX |
3823 | return 0; |
3824 | } | |
3825 | ||
c4abb7c9 JK |
3826 | static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu) |
3827 | { | |
c4abb7c9 | 3828 | kvm_inject_nmi(vcpu); |
c4abb7c9 JK |
3829 | |
3830 | return 0; | |
3831 | } | |
3832 | ||
f077825a PB |
3833 | static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu) |
3834 | { | |
64d60670 PB |
3835 | kvm_make_request(KVM_REQ_SMI, vcpu); |
3836 | ||
f077825a PB |
3837 | return 0; |
3838 | } | |
3839 | ||
b209749f AK |
3840 | static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu, |
3841 | struct kvm_tpr_access_ctl *tac) | |
3842 | { | |
3843 | if (tac->flags) | |
3844 | return -EINVAL; | |
3845 | vcpu->arch.tpr_access_reporting = !!tac->enabled; | |
3846 | return 0; | |
3847 | } | |
3848 | ||
890ca9ae HY |
3849 | static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu, |
3850 | u64 mcg_cap) | |
3851 | { | |
3852 | int r; | |
3853 | unsigned bank_num = mcg_cap & 0xff, bank; | |
3854 | ||
3855 | r = -EINVAL; | |
c4e0e4ab | 3856 | if (!bank_num || bank_num > KVM_MAX_MCE_BANKS) |
890ca9ae | 3857 | goto out; |
c45dcc71 | 3858 | if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000)) |
890ca9ae HY |
3859 | goto out; |
3860 | r = 0; | |
3861 | vcpu->arch.mcg_cap = mcg_cap; | |
3862 | /* Init IA32_MCG_CTL to all 1s */ | |
3863 | if (mcg_cap & MCG_CTL_P) | |
3864 | vcpu->arch.mcg_ctl = ~(u64)0; | |
3865 | /* Init IA32_MCi_CTL to all 1s */ | |
3866 | for (bank = 0; bank < bank_num; bank++) | |
3867 | vcpu->arch.mce_banks[bank*4] = ~(u64)0; | |
c45dcc71 | 3868 | |
afaf0b2f | 3869 | kvm_x86_ops.setup_mce(vcpu); |
890ca9ae HY |
3870 | out: |
3871 | return r; | |
3872 | } | |
3873 | ||
3874 | static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu, | |
3875 | struct kvm_x86_mce *mce) | |
3876 | { | |
3877 | u64 mcg_cap = vcpu->arch.mcg_cap; | |
3878 | unsigned bank_num = mcg_cap & 0xff; | |
3879 | u64 *banks = vcpu->arch.mce_banks; | |
3880 | ||
3881 | if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL)) | |
3882 | return -EINVAL; | |
3883 | /* | |
3884 | * if IA32_MCG_CTL is not all 1s, the uncorrected error | |
3885 | * reporting is disabled | |
3886 | */ | |
3887 | if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) && | |
3888 | vcpu->arch.mcg_ctl != ~(u64)0) | |
3889 | return 0; | |
3890 | banks += 4 * mce->bank; | |
3891 | /* | |
3892 | * if IA32_MCi_CTL is not all 1s, the uncorrected error | |
3893 | * reporting is disabled for the bank | |
3894 | */ | |
3895 | if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0) | |
3896 | return 0; | |
3897 | if (mce->status & MCI_STATUS_UC) { | |
3898 | if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) || | |
fc78f519 | 3899 | !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) { |
a8eeb04a | 3900 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); |
890ca9ae HY |
3901 | return 0; |
3902 | } | |
3903 | if (banks[1] & MCI_STATUS_VAL) | |
3904 | mce->status |= MCI_STATUS_OVER; | |
3905 | banks[2] = mce->addr; | |
3906 | banks[3] = mce->misc; | |
3907 | vcpu->arch.mcg_status = mce->mcg_status; | |
3908 | banks[1] = mce->status; | |
3909 | kvm_queue_exception(vcpu, MC_VECTOR); | |
3910 | } else if (!(banks[1] & MCI_STATUS_VAL) | |
3911 | || !(banks[1] & MCI_STATUS_UC)) { | |
3912 | if (banks[1] & MCI_STATUS_VAL) | |
3913 | mce->status |= MCI_STATUS_OVER; | |
3914 | banks[2] = mce->addr; | |
3915 | banks[3] = mce->misc; | |
3916 | banks[1] = mce->status; | |
3917 | } else | |
3918 | banks[1] |= MCI_STATUS_OVER; | |
3919 | return 0; | |
3920 | } | |
3921 | ||
3cfc3092 JK |
3922 | static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu, |
3923 | struct kvm_vcpu_events *events) | |
3924 | { | |
7460fb4a | 3925 | process_nmi(vcpu); |
59073aaf | 3926 | |
a06230b6 OU |
3927 | /* |
3928 | * In guest mode, payload delivery should be deferred, | |
3929 | * so that the L1 hypervisor can intercept #PF before | |
3930 | * CR2 is modified (or intercept #DB before DR6 is | |
3931 | * modified under nVMX). Unless the per-VM capability, | |
3932 | * KVM_CAP_EXCEPTION_PAYLOAD, is set, we may not defer the delivery of | |
3933 | * an exception payload and handle after a KVM_GET_VCPU_EVENTS. Since we | |
3934 | * opportunistically defer the exception payload, deliver it if the | |
3935 | * capability hasn't been requested before processing a | |
3936 | * KVM_GET_VCPU_EVENTS. | |
3937 | */ | |
3938 | if (!vcpu->kvm->arch.exception_payload_enabled && | |
3939 | vcpu->arch.exception.pending && vcpu->arch.exception.has_payload) | |
3940 | kvm_deliver_exception_payload(vcpu); | |
3941 | ||
664f8e26 | 3942 | /* |
59073aaf JM |
3943 | * The API doesn't provide the instruction length for software |
3944 | * exceptions, so don't report them. As long as the guest RIP | |
3945 | * isn't advanced, we should expect to encounter the exception | |
3946 | * again. | |
664f8e26 | 3947 | */ |
59073aaf JM |
3948 | if (kvm_exception_is_soft(vcpu->arch.exception.nr)) { |
3949 | events->exception.injected = 0; | |
3950 | events->exception.pending = 0; | |
3951 | } else { | |
3952 | events->exception.injected = vcpu->arch.exception.injected; | |
3953 | events->exception.pending = vcpu->arch.exception.pending; | |
3954 | /* | |
3955 | * For ABI compatibility, deliberately conflate | |
3956 | * pending and injected exceptions when | |
3957 | * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled. | |
3958 | */ | |
3959 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
3960 | events->exception.injected |= | |
3961 | vcpu->arch.exception.pending; | |
3962 | } | |
3cfc3092 JK |
3963 | events->exception.nr = vcpu->arch.exception.nr; |
3964 | events->exception.has_error_code = vcpu->arch.exception.has_error_code; | |
3965 | events->exception.error_code = vcpu->arch.exception.error_code; | |
59073aaf JM |
3966 | events->exception_has_payload = vcpu->arch.exception.has_payload; |
3967 | events->exception_payload = vcpu->arch.exception.payload; | |
3cfc3092 | 3968 | |
03b82a30 | 3969 | events->interrupt.injected = |
04140b41 | 3970 | vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft; |
3cfc3092 | 3971 | events->interrupt.nr = vcpu->arch.interrupt.nr; |
03b82a30 | 3972 | events->interrupt.soft = 0; |
afaf0b2f | 3973 | events->interrupt.shadow = kvm_x86_ops.get_interrupt_shadow(vcpu); |
3cfc3092 JK |
3974 | |
3975 | events->nmi.injected = vcpu->arch.nmi_injected; | |
7460fb4a | 3976 | events->nmi.pending = vcpu->arch.nmi_pending != 0; |
afaf0b2f | 3977 | events->nmi.masked = kvm_x86_ops.get_nmi_mask(vcpu); |
97e69aa6 | 3978 | events->nmi.pad = 0; |
3cfc3092 | 3979 | |
66450a21 | 3980 | events->sipi_vector = 0; /* never valid when reporting to user space */ |
3cfc3092 | 3981 | |
f077825a PB |
3982 | events->smi.smm = is_smm(vcpu); |
3983 | events->smi.pending = vcpu->arch.smi_pending; | |
3984 | events->smi.smm_inside_nmi = | |
3985 | !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK); | |
3986 | events->smi.latched_init = kvm_lapic_latched_init(vcpu); | |
3987 | ||
dab4b911 | 3988 | events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING |
f077825a PB |
3989 | | KVM_VCPUEVENT_VALID_SHADOW |
3990 | | KVM_VCPUEVENT_VALID_SMM); | |
59073aaf JM |
3991 | if (vcpu->kvm->arch.exception_payload_enabled) |
3992 | events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD; | |
3993 | ||
97e69aa6 | 3994 | memset(&events->reserved, 0, sizeof(events->reserved)); |
3cfc3092 JK |
3995 | } |
3996 | ||
c5833c7a | 3997 | static void kvm_smm_changed(struct kvm_vcpu *vcpu); |
6ef4e07e | 3998 | |
3cfc3092 JK |
3999 | static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu, |
4000 | struct kvm_vcpu_events *events) | |
4001 | { | |
dab4b911 | 4002 | if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING |
48005f64 | 4003 | | KVM_VCPUEVENT_VALID_SIPI_VECTOR |
f077825a | 4004 | | KVM_VCPUEVENT_VALID_SHADOW |
59073aaf JM |
4005 | | KVM_VCPUEVENT_VALID_SMM |
4006 | | KVM_VCPUEVENT_VALID_PAYLOAD)) | |
3cfc3092 JK |
4007 | return -EINVAL; |
4008 | ||
59073aaf JM |
4009 | if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) { |
4010 | if (!vcpu->kvm->arch.exception_payload_enabled) | |
4011 | return -EINVAL; | |
4012 | if (events->exception.pending) | |
4013 | events->exception.injected = 0; | |
4014 | else | |
4015 | events->exception_has_payload = 0; | |
4016 | } else { | |
4017 | events->exception.pending = 0; | |
4018 | events->exception_has_payload = 0; | |
4019 | } | |
4020 | ||
4021 | if ((events->exception.injected || events->exception.pending) && | |
4022 | (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR)) | |
78e546c8 PB |
4023 | return -EINVAL; |
4024 | ||
28bf2888 DH |
4025 | /* INITs are latched while in SMM */ |
4026 | if (events->flags & KVM_VCPUEVENT_VALID_SMM && | |
4027 | (events->smi.smm || events->smi.pending) && | |
4028 | vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) | |
4029 | return -EINVAL; | |
4030 | ||
7460fb4a | 4031 | process_nmi(vcpu); |
59073aaf JM |
4032 | vcpu->arch.exception.injected = events->exception.injected; |
4033 | vcpu->arch.exception.pending = events->exception.pending; | |
3cfc3092 JK |
4034 | vcpu->arch.exception.nr = events->exception.nr; |
4035 | vcpu->arch.exception.has_error_code = events->exception.has_error_code; | |
4036 | vcpu->arch.exception.error_code = events->exception.error_code; | |
59073aaf JM |
4037 | vcpu->arch.exception.has_payload = events->exception_has_payload; |
4038 | vcpu->arch.exception.payload = events->exception_payload; | |
3cfc3092 | 4039 | |
04140b41 | 4040 | vcpu->arch.interrupt.injected = events->interrupt.injected; |
3cfc3092 JK |
4041 | vcpu->arch.interrupt.nr = events->interrupt.nr; |
4042 | vcpu->arch.interrupt.soft = events->interrupt.soft; | |
48005f64 | 4043 | if (events->flags & KVM_VCPUEVENT_VALID_SHADOW) |
afaf0b2f | 4044 | kvm_x86_ops.set_interrupt_shadow(vcpu, |
48005f64 | 4045 | events->interrupt.shadow); |
3cfc3092 JK |
4046 | |
4047 | vcpu->arch.nmi_injected = events->nmi.injected; | |
dab4b911 JK |
4048 | if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING) |
4049 | vcpu->arch.nmi_pending = events->nmi.pending; | |
afaf0b2f | 4050 | kvm_x86_ops.set_nmi_mask(vcpu, events->nmi.masked); |
3cfc3092 | 4051 | |
66450a21 | 4052 | if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR && |
bce87cce | 4053 | lapic_in_kernel(vcpu)) |
66450a21 | 4054 | vcpu->arch.apic->sipi_vector = events->sipi_vector; |
3cfc3092 | 4055 | |
f077825a | 4056 | if (events->flags & KVM_VCPUEVENT_VALID_SMM) { |
c5833c7a SC |
4057 | if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) { |
4058 | if (events->smi.smm) | |
4059 | vcpu->arch.hflags |= HF_SMM_MASK; | |
4060 | else | |
4061 | vcpu->arch.hflags &= ~HF_SMM_MASK; | |
4062 | kvm_smm_changed(vcpu); | |
4063 | } | |
6ef4e07e | 4064 | |
f077825a | 4065 | vcpu->arch.smi_pending = events->smi.pending; |
f4ef1910 WL |
4066 | |
4067 | if (events->smi.smm) { | |
4068 | if (events->smi.smm_inside_nmi) | |
4069 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; | |
f077825a | 4070 | else |
f4ef1910 | 4071 | vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK; |
ff90afa7 LA |
4072 | } |
4073 | ||
4074 | if (lapic_in_kernel(vcpu)) { | |
4075 | if (events->smi.latched_init) | |
4076 | set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
4077 | else | |
4078 | clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events); | |
f077825a PB |
4079 | } |
4080 | } | |
4081 | ||
3842d135 AK |
4082 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
4083 | ||
3cfc3092 JK |
4084 | return 0; |
4085 | } | |
4086 | ||
a1efbe77 JK |
4087 | static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu, |
4088 | struct kvm_debugregs *dbgregs) | |
4089 | { | |
73aaf249 JK |
4090 | unsigned long val; |
4091 | ||
a1efbe77 | 4092 | memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db)); |
16f8a6f9 | 4093 | kvm_get_dr(vcpu, 6, &val); |
73aaf249 | 4094 | dbgregs->dr6 = val; |
a1efbe77 JK |
4095 | dbgregs->dr7 = vcpu->arch.dr7; |
4096 | dbgregs->flags = 0; | |
97e69aa6 | 4097 | memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved)); |
a1efbe77 JK |
4098 | } |
4099 | ||
4100 | static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu, | |
4101 | struct kvm_debugregs *dbgregs) | |
4102 | { | |
4103 | if (dbgregs->flags) | |
4104 | return -EINVAL; | |
4105 | ||
d14bdb55 PB |
4106 | if (dbgregs->dr6 & ~0xffffffffull) |
4107 | return -EINVAL; | |
4108 | if (dbgregs->dr7 & ~0xffffffffull) | |
4109 | return -EINVAL; | |
4110 | ||
a1efbe77 | 4111 | memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db)); |
ae561ede | 4112 | kvm_update_dr0123(vcpu); |
a1efbe77 JK |
4113 | vcpu->arch.dr6 = dbgregs->dr6; |
4114 | vcpu->arch.dr7 = dbgregs->dr7; | |
9926c9fd | 4115 | kvm_update_dr7(vcpu); |
a1efbe77 | 4116 | |
a1efbe77 JK |
4117 | return 0; |
4118 | } | |
4119 | ||
df1daba7 PB |
4120 | #define XSTATE_COMPACTION_ENABLED (1ULL << 63) |
4121 | ||
4122 | static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu) | |
4123 | { | |
b666a4b6 | 4124 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
400e4b20 | 4125 | u64 xstate_bv = xsave->header.xfeatures; |
df1daba7 PB |
4126 | u64 valid; |
4127 | ||
4128 | /* | |
4129 | * Copy legacy XSAVE area, to avoid complications with CPUID | |
4130 | * leaves 0 and 1 in the loop below. | |
4131 | */ | |
4132 | memcpy(dest, xsave, XSAVE_HDR_OFFSET); | |
4133 | ||
4134 | /* Set XSTATE_BV */ | |
00c87e9a | 4135 | xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE; |
df1daba7 PB |
4136 | *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv; |
4137 | ||
4138 | /* | |
4139 | * Copy each region from the possibly compacted offset to the | |
4140 | * non-compacted offset. | |
4141 | */ | |
d91cab78 | 4142 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
df1daba7 | 4143 | while (valid) { |
abd16d68 SAS |
4144 | u64 xfeature_mask = valid & -valid; |
4145 | int xfeature_nr = fls64(xfeature_mask) - 1; | |
4146 | void *src = get_xsave_addr(xsave, xfeature_nr); | |
df1daba7 PB |
4147 | |
4148 | if (src) { | |
4149 | u32 size, offset, ecx, edx; | |
abd16d68 | 4150 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
df1daba7 | 4151 | &size, &offset, &ecx, &edx); |
abd16d68 | 4152 | if (xfeature_nr == XFEATURE_PKRU) |
38cfd5e3 PB |
4153 | memcpy(dest + offset, &vcpu->arch.pkru, |
4154 | sizeof(vcpu->arch.pkru)); | |
4155 | else | |
4156 | memcpy(dest + offset, src, size); | |
4157 | ||
df1daba7 PB |
4158 | } |
4159 | ||
abd16d68 | 4160 | valid -= xfeature_mask; |
df1daba7 PB |
4161 | } |
4162 | } | |
4163 | ||
4164 | static void load_xsave(struct kvm_vcpu *vcpu, u8 *src) | |
4165 | { | |
b666a4b6 | 4166 | struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave; |
df1daba7 PB |
4167 | u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET); |
4168 | u64 valid; | |
4169 | ||
4170 | /* | |
4171 | * Copy legacy XSAVE area, to avoid complications with CPUID | |
4172 | * leaves 0 and 1 in the loop below. | |
4173 | */ | |
4174 | memcpy(xsave, src, XSAVE_HDR_OFFSET); | |
4175 | ||
4176 | /* Set XSTATE_BV and possibly XCOMP_BV. */ | |
400e4b20 | 4177 | xsave->header.xfeatures = xstate_bv; |
782511b0 | 4178 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
3a54450b | 4179 | xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED; |
df1daba7 PB |
4180 | |
4181 | /* | |
4182 | * Copy each region from the non-compacted offset to the | |
4183 | * possibly compacted offset. | |
4184 | */ | |
d91cab78 | 4185 | valid = xstate_bv & ~XFEATURE_MASK_FPSSE; |
df1daba7 | 4186 | while (valid) { |
abd16d68 SAS |
4187 | u64 xfeature_mask = valid & -valid; |
4188 | int xfeature_nr = fls64(xfeature_mask) - 1; | |
4189 | void *dest = get_xsave_addr(xsave, xfeature_nr); | |
df1daba7 PB |
4190 | |
4191 | if (dest) { | |
4192 | u32 size, offset, ecx, edx; | |
abd16d68 | 4193 | cpuid_count(XSTATE_CPUID, xfeature_nr, |
df1daba7 | 4194 | &size, &offset, &ecx, &edx); |
abd16d68 | 4195 | if (xfeature_nr == XFEATURE_PKRU) |
38cfd5e3 PB |
4196 | memcpy(&vcpu->arch.pkru, src + offset, |
4197 | sizeof(vcpu->arch.pkru)); | |
4198 | else | |
4199 | memcpy(dest, src + offset, size); | |
ee4100da | 4200 | } |
df1daba7 | 4201 | |
abd16d68 | 4202 | valid -= xfeature_mask; |
df1daba7 PB |
4203 | } |
4204 | } | |
4205 | ||
2d5b5a66 SY |
4206 | static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu, |
4207 | struct kvm_xsave *guest_xsave) | |
4208 | { | |
d366bf7e | 4209 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
df1daba7 PB |
4210 | memset(guest_xsave, 0, sizeof(struct kvm_xsave)); |
4211 | fill_xsave((u8 *) guest_xsave->region, vcpu); | |
4344ee98 | 4212 | } else { |
2d5b5a66 | 4213 | memcpy(guest_xsave->region, |
b666a4b6 | 4214 | &vcpu->arch.guest_fpu->state.fxsave, |
c47ada30 | 4215 | sizeof(struct fxregs_state)); |
2d5b5a66 | 4216 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] = |
d91cab78 | 4217 | XFEATURE_MASK_FPSSE; |
2d5b5a66 SY |
4218 | } |
4219 | } | |
4220 | ||
a575813b WL |
4221 | #define XSAVE_MXCSR_OFFSET 24 |
4222 | ||
2d5b5a66 SY |
4223 | static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu, |
4224 | struct kvm_xsave *guest_xsave) | |
4225 | { | |
4226 | u64 xstate_bv = | |
4227 | *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)]; | |
a575813b | 4228 | u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)]; |
2d5b5a66 | 4229 | |
d366bf7e | 4230 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
d7876f1b PB |
4231 | /* |
4232 | * Here we allow setting states that are not present in | |
4233 | * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility | |
4234 | * with old userspace. | |
4235 | */ | |
cfc48181 | 4236 | if (xstate_bv & ~supported_xcr0 || mxcsr & ~mxcsr_feature_mask) |
d7876f1b | 4237 | return -EINVAL; |
df1daba7 | 4238 | load_xsave(vcpu, (u8 *)guest_xsave->region); |
d7876f1b | 4239 | } else { |
a575813b WL |
4240 | if (xstate_bv & ~XFEATURE_MASK_FPSSE || |
4241 | mxcsr & ~mxcsr_feature_mask) | |
2d5b5a66 | 4242 | return -EINVAL; |
b666a4b6 | 4243 | memcpy(&vcpu->arch.guest_fpu->state.fxsave, |
c47ada30 | 4244 | guest_xsave->region, sizeof(struct fxregs_state)); |
2d5b5a66 SY |
4245 | } |
4246 | return 0; | |
4247 | } | |
4248 | ||
4249 | static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu, | |
4250 | struct kvm_xcrs *guest_xcrs) | |
4251 | { | |
d366bf7e | 4252 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) { |
2d5b5a66 SY |
4253 | guest_xcrs->nr_xcrs = 0; |
4254 | return; | |
4255 | } | |
4256 | ||
4257 | guest_xcrs->nr_xcrs = 1; | |
4258 | guest_xcrs->flags = 0; | |
4259 | guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK; | |
4260 | guest_xcrs->xcrs[0].value = vcpu->arch.xcr0; | |
4261 | } | |
4262 | ||
4263 | static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu, | |
4264 | struct kvm_xcrs *guest_xcrs) | |
4265 | { | |
4266 | int i, r = 0; | |
4267 | ||
d366bf7e | 4268 | if (!boot_cpu_has(X86_FEATURE_XSAVE)) |
2d5b5a66 SY |
4269 | return -EINVAL; |
4270 | ||
4271 | if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags) | |
4272 | return -EINVAL; | |
4273 | ||
4274 | for (i = 0; i < guest_xcrs->nr_xcrs; i++) | |
4275 | /* Only support XCR0 currently */ | |
c67a04cb | 4276 | if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) { |
2d5b5a66 | 4277 | r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK, |
c67a04cb | 4278 | guest_xcrs->xcrs[i].value); |
2d5b5a66 SY |
4279 | break; |
4280 | } | |
4281 | if (r) | |
4282 | r = -EINVAL; | |
4283 | return r; | |
4284 | } | |
4285 | ||
1c0b28c2 EM |
4286 | /* |
4287 | * kvm_set_guest_paused() indicates to the guest kernel that it has been | |
4288 | * stopped by the hypervisor. This function will be called from the host only. | |
4289 | * EINVAL is returned when the host attempts to set the flag for a guest that | |
4290 | * does not support pv clocks. | |
4291 | */ | |
4292 | static int kvm_set_guest_paused(struct kvm_vcpu *vcpu) | |
4293 | { | |
0b79459b | 4294 | if (!vcpu->arch.pv_time_enabled) |
1c0b28c2 | 4295 | return -EINVAL; |
51d59c6b | 4296 | vcpu->arch.pvclock_set_guest_stopped_request = true; |
1c0b28c2 EM |
4297 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
4298 | return 0; | |
4299 | } | |
4300 | ||
5c919412 AS |
4301 | static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu, |
4302 | struct kvm_enable_cap *cap) | |
4303 | { | |
57b119da VK |
4304 | int r; |
4305 | uint16_t vmcs_version; | |
4306 | void __user *user_ptr; | |
4307 | ||
5c919412 AS |
4308 | if (cap->flags) |
4309 | return -EINVAL; | |
4310 | ||
4311 | switch (cap->cap) { | |
efc479e6 RK |
4312 | case KVM_CAP_HYPERV_SYNIC2: |
4313 | if (cap->args[0]) | |
4314 | return -EINVAL; | |
b2869f28 GS |
4315 | /* fall through */ |
4316 | ||
5c919412 | 4317 | case KVM_CAP_HYPERV_SYNIC: |
546d87e5 WL |
4318 | if (!irqchip_in_kernel(vcpu->kvm)) |
4319 | return -EINVAL; | |
efc479e6 RK |
4320 | return kvm_hv_activate_synic(vcpu, cap->cap == |
4321 | KVM_CAP_HYPERV_SYNIC2); | |
57b119da | 4322 | case KVM_CAP_HYPERV_ENLIGHTENED_VMCS: |
33b22172 | 4323 | if (!kvm_x86_ops.nested_ops->enable_evmcs) |
5158917c | 4324 | return -ENOTTY; |
33b22172 | 4325 | r = kvm_x86_ops.nested_ops->enable_evmcs(vcpu, &vmcs_version); |
57b119da VK |
4326 | if (!r) { |
4327 | user_ptr = (void __user *)(uintptr_t)cap->args[0]; | |
4328 | if (copy_to_user(user_ptr, &vmcs_version, | |
4329 | sizeof(vmcs_version))) | |
4330 | r = -EFAULT; | |
4331 | } | |
4332 | return r; | |
344c6c80 | 4333 | case KVM_CAP_HYPERV_DIRECT_TLBFLUSH: |
afaf0b2f | 4334 | if (!kvm_x86_ops.enable_direct_tlbflush) |
344c6c80 TL |
4335 | return -ENOTTY; |
4336 | ||
afaf0b2f | 4337 | return kvm_x86_ops.enable_direct_tlbflush(vcpu); |
57b119da | 4338 | |
5c919412 AS |
4339 | default: |
4340 | return -EINVAL; | |
4341 | } | |
4342 | } | |
4343 | ||
313a3dc7 CO |
4344 | long kvm_arch_vcpu_ioctl(struct file *filp, |
4345 | unsigned int ioctl, unsigned long arg) | |
4346 | { | |
4347 | struct kvm_vcpu *vcpu = filp->private_data; | |
4348 | void __user *argp = (void __user *)arg; | |
4349 | int r; | |
d1ac91d8 AK |
4350 | union { |
4351 | struct kvm_lapic_state *lapic; | |
4352 | struct kvm_xsave *xsave; | |
4353 | struct kvm_xcrs *xcrs; | |
4354 | void *buffer; | |
4355 | } u; | |
4356 | ||
9b062471 CD |
4357 | vcpu_load(vcpu); |
4358 | ||
d1ac91d8 | 4359 | u.buffer = NULL; |
313a3dc7 CO |
4360 | switch (ioctl) { |
4361 | case KVM_GET_LAPIC: { | |
2204ae3c | 4362 | r = -EINVAL; |
bce87cce | 4363 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 4364 | goto out; |
254272ce BG |
4365 | u.lapic = kzalloc(sizeof(struct kvm_lapic_state), |
4366 | GFP_KERNEL_ACCOUNT); | |
313a3dc7 | 4367 | |
b772ff36 | 4368 | r = -ENOMEM; |
d1ac91d8 | 4369 | if (!u.lapic) |
b772ff36 | 4370 | goto out; |
d1ac91d8 | 4371 | r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic); |
313a3dc7 CO |
4372 | if (r) |
4373 | goto out; | |
4374 | r = -EFAULT; | |
d1ac91d8 | 4375 | if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state))) |
313a3dc7 CO |
4376 | goto out; |
4377 | r = 0; | |
4378 | break; | |
4379 | } | |
4380 | case KVM_SET_LAPIC: { | |
2204ae3c | 4381 | r = -EINVAL; |
bce87cce | 4382 | if (!lapic_in_kernel(vcpu)) |
2204ae3c | 4383 | goto out; |
ff5c2c03 | 4384 | u.lapic = memdup_user(argp, sizeof(*u.lapic)); |
9b062471 CD |
4385 | if (IS_ERR(u.lapic)) { |
4386 | r = PTR_ERR(u.lapic); | |
4387 | goto out_nofree; | |
4388 | } | |
ff5c2c03 | 4389 | |
d1ac91d8 | 4390 | r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic); |
313a3dc7 CO |
4391 | break; |
4392 | } | |
f77bc6a4 ZX |
4393 | case KVM_INTERRUPT: { |
4394 | struct kvm_interrupt irq; | |
4395 | ||
4396 | r = -EFAULT; | |
0e96f31e | 4397 | if (copy_from_user(&irq, argp, sizeof(irq))) |
f77bc6a4 ZX |
4398 | goto out; |
4399 | r = kvm_vcpu_ioctl_interrupt(vcpu, &irq); | |
f77bc6a4 ZX |
4400 | break; |
4401 | } | |
c4abb7c9 JK |
4402 | case KVM_NMI: { |
4403 | r = kvm_vcpu_ioctl_nmi(vcpu); | |
c4abb7c9 JK |
4404 | break; |
4405 | } | |
f077825a PB |
4406 | case KVM_SMI: { |
4407 | r = kvm_vcpu_ioctl_smi(vcpu); | |
4408 | break; | |
4409 | } | |
313a3dc7 CO |
4410 | case KVM_SET_CPUID: { |
4411 | struct kvm_cpuid __user *cpuid_arg = argp; | |
4412 | struct kvm_cpuid cpuid; | |
4413 | ||
4414 | r = -EFAULT; | |
0e96f31e | 4415 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
313a3dc7 CO |
4416 | goto out; |
4417 | r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries); | |
313a3dc7 CO |
4418 | break; |
4419 | } | |
07716717 DK |
4420 | case KVM_SET_CPUID2: { |
4421 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4422 | struct kvm_cpuid2 cpuid; | |
4423 | ||
4424 | r = -EFAULT; | |
0e96f31e | 4425 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
4426 | goto out; |
4427 | r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid, | |
19355475 | 4428 | cpuid_arg->entries); |
07716717 DK |
4429 | break; |
4430 | } | |
4431 | case KVM_GET_CPUID2: { | |
4432 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4433 | struct kvm_cpuid2 cpuid; | |
4434 | ||
4435 | r = -EFAULT; | |
0e96f31e | 4436 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) |
07716717 DK |
4437 | goto out; |
4438 | r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid, | |
19355475 | 4439 | cpuid_arg->entries); |
07716717 DK |
4440 | if (r) |
4441 | goto out; | |
4442 | r = -EFAULT; | |
0e96f31e | 4443 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) |
07716717 DK |
4444 | goto out; |
4445 | r = 0; | |
4446 | break; | |
4447 | } | |
801e459a TL |
4448 | case KVM_GET_MSRS: { |
4449 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
609e36d3 | 4450 | r = msr_io(vcpu, argp, do_get_msr, 1); |
801e459a | 4451 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 4452 | break; |
801e459a TL |
4453 | } |
4454 | case KVM_SET_MSRS: { | |
4455 | int idx = srcu_read_lock(&vcpu->kvm->srcu); | |
313a3dc7 | 4456 | r = msr_io(vcpu, argp, do_set_msr, 0); |
801e459a | 4457 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
313a3dc7 | 4458 | break; |
801e459a | 4459 | } |
b209749f AK |
4460 | case KVM_TPR_ACCESS_REPORTING: { |
4461 | struct kvm_tpr_access_ctl tac; | |
4462 | ||
4463 | r = -EFAULT; | |
0e96f31e | 4464 | if (copy_from_user(&tac, argp, sizeof(tac))) |
b209749f AK |
4465 | goto out; |
4466 | r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac); | |
4467 | if (r) | |
4468 | goto out; | |
4469 | r = -EFAULT; | |
0e96f31e | 4470 | if (copy_to_user(argp, &tac, sizeof(tac))) |
b209749f AK |
4471 | goto out; |
4472 | r = 0; | |
4473 | break; | |
4474 | }; | |
b93463aa AK |
4475 | case KVM_SET_VAPIC_ADDR: { |
4476 | struct kvm_vapic_addr va; | |
7301d6ab | 4477 | int idx; |
b93463aa AK |
4478 | |
4479 | r = -EINVAL; | |
35754c98 | 4480 | if (!lapic_in_kernel(vcpu)) |
b93463aa AK |
4481 | goto out; |
4482 | r = -EFAULT; | |
0e96f31e | 4483 | if (copy_from_user(&va, argp, sizeof(va))) |
b93463aa | 4484 | goto out; |
7301d6ab | 4485 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
fda4e2e8 | 4486 | r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr); |
7301d6ab | 4487 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b93463aa AK |
4488 | break; |
4489 | } | |
890ca9ae HY |
4490 | case KVM_X86_SETUP_MCE: { |
4491 | u64 mcg_cap; | |
4492 | ||
4493 | r = -EFAULT; | |
0e96f31e | 4494 | if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap))) |
890ca9ae HY |
4495 | goto out; |
4496 | r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap); | |
4497 | break; | |
4498 | } | |
4499 | case KVM_X86_SET_MCE: { | |
4500 | struct kvm_x86_mce mce; | |
4501 | ||
4502 | r = -EFAULT; | |
0e96f31e | 4503 | if (copy_from_user(&mce, argp, sizeof(mce))) |
890ca9ae HY |
4504 | goto out; |
4505 | r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce); | |
4506 | break; | |
4507 | } | |
3cfc3092 JK |
4508 | case KVM_GET_VCPU_EVENTS: { |
4509 | struct kvm_vcpu_events events; | |
4510 | ||
4511 | kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events); | |
4512 | ||
4513 | r = -EFAULT; | |
4514 | if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events))) | |
4515 | break; | |
4516 | r = 0; | |
4517 | break; | |
4518 | } | |
4519 | case KVM_SET_VCPU_EVENTS: { | |
4520 | struct kvm_vcpu_events events; | |
4521 | ||
4522 | r = -EFAULT; | |
4523 | if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events))) | |
4524 | break; | |
4525 | ||
4526 | r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events); | |
4527 | break; | |
4528 | } | |
a1efbe77 JK |
4529 | case KVM_GET_DEBUGREGS: { |
4530 | struct kvm_debugregs dbgregs; | |
4531 | ||
4532 | kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs); | |
4533 | ||
4534 | r = -EFAULT; | |
4535 | if (copy_to_user(argp, &dbgregs, | |
4536 | sizeof(struct kvm_debugregs))) | |
4537 | break; | |
4538 | r = 0; | |
4539 | break; | |
4540 | } | |
4541 | case KVM_SET_DEBUGREGS: { | |
4542 | struct kvm_debugregs dbgregs; | |
4543 | ||
4544 | r = -EFAULT; | |
4545 | if (copy_from_user(&dbgregs, argp, | |
4546 | sizeof(struct kvm_debugregs))) | |
4547 | break; | |
4548 | ||
4549 | r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs); | |
4550 | break; | |
4551 | } | |
2d5b5a66 | 4552 | case KVM_GET_XSAVE: { |
254272ce | 4553 | u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 4554 | r = -ENOMEM; |
d1ac91d8 | 4555 | if (!u.xsave) |
2d5b5a66 SY |
4556 | break; |
4557 | ||
d1ac91d8 | 4558 | kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
4559 | |
4560 | r = -EFAULT; | |
d1ac91d8 | 4561 | if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave))) |
2d5b5a66 SY |
4562 | break; |
4563 | r = 0; | |
4564 | break; | |
4565 | } | |
4566 | case KVM_SET_XSAVE: { | |
ff5c2c03 | 4567 | u.xsave = memdup_user(argp, sizeof(*u.xsave)); |
9b062471 CD |
4568 | if (IS_ERR(u.xsave)) { |
4569 | r = PTR_ERR(u.xsave); | |
4570 | goto out_nofree; | |
4571 | } | |
2d5b5a66 | 4572 | |
d1ac91d8 | 4573 | r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave); |
2d5b5a66 SY |
4574 | break; |
4575 | } | |
4576 | case KVM_GET_XCRS: { | |
254272ce | 4577 | u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT); |
2d5b5a66 | 4578 | r = -ENOMEM; |
d1ac91d8 | 4579 | if (!u.xcrs) |
2d5b5a66 SY |
4580 | break; |
4581 | ||
d1ac91d8 | 4582 | kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
4583 | |
4584 | r = -EFAULT; | |
d1ac91d8 | 4585 | if (copy_to_user(argp, u.xcrs, |
2d5b5a66 SY |
4586 | sizeof(struct kvm_xcrs))) |
4587 | break; | |
4588 | r = 0; | |
4589 | break; | |
4590 | } | |
4591 | case KVM_SET_XCRS: { | |
ff5c2c03 | 4592 | u.xcrs = memdup_user(argp, sizeof(*u.xcrs)); |
9b062471 CD |
4593 | if (IS_ERR(u.xcrs)) { |
4594 | r = PTR_ERR(u.xcrs); | |
4595 | goto out_nofree; | |
4596 | } | |
2d5b5a66 | 4597 | |
d1ac91d8 | 4598 | r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs); |
2d5b5a66 SY |
4599 | break; |
4600 | } | |
92a1f12d JR |
4601 | case KVM_SET_TSC_KHZ: { |
4602 | u32 user_tsc_khz; | |
4603 | ||
4604 | r = -EINVAL; | |
92a1f12d JR |
4605 | user_tsc_khz = (u32)arg; |
4606 | ||
4607 | if (user_tsc_khz >= kvm_max_guest_tsc_khz) | |
4608 | goto out; | |
4609 | ||
cc578287 ZA |
4610 | if (user_tsc_khz == 0) |
4611 | user_tsc_khz = tsc_khz; | |
4612 | ||
381d585c HZ |
4613 | if (!kvm_set_tsc_khz(vcpu, user_tsc_khz)) |
4614 | r = 0; | |
92a1f12d | 4615 | |
92a1f12d JR |
4616 | goto out; |
4617 | } | |
4618 | case KVM_GET_TSC_KHZ: { | |
cc578287 | 4619 | r = vcpu->arch.virtual_tsc_khz; |
92a1f12d JR |
4620 | goto out; |
4621 | } | |
1c0b28c2 EM |
4622 | case KVM_KVMCLOCK_CTRL: { |
4623 | r = kvm_set_guest_paused(vcpu); | |
4624 | goto out; | |
4625 | } | |
5c919412 AS |
4626 | case KVM_ENABLE_CAP: { |
4627 | struct kvm_enable_cap cap; | |
4628 | ||
4629 | r = -EFAULT; | |
4630 | if (copy_from_user(&cap, argp, sizeof(cap))) | |
4631 | goto out; | |
4632 | r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap); | |
4633 | break; | |
4634 | } | |
8fcc4b59 JM |
4635 | case KVM_GET_NESTED_STATE: { |
4636 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
4637 | u32 user_data_size; | |
4638 | ||
4639 | r = -EINVAL; | |
33b22172 | 4640 | if (!kvm_x86_ops.nested_ops->get_state) |
8fcc4b59 JM |
4641 | break; |
4642 | ||
4643 | BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size)); | |
26b471c7 | 4644 | r = -EFAULT; |
8fcc4b59 | 4645 | if (get_user(user_data_size, &user_kvm_nested_state->size)) |
26b471c7 | 4646 | break; |
8fcc4b59 | 4647 | |
33b22172 PB |
4648 | r = kvm_x86_ops.nested_ops->get_state(vcpu, user_kvm_nested_state, |
4649 | user_data_size); | |
8fcc4b59 | 4650 | if (r < 0) |
26b471c7 | 4651 | break; |
8fcc4b59 JM |
4652 | |
4653 | if (r > user_data_size) { | |
4654 | if (put_user(r, &user_kvm_nested_state->size)) | |
26b471c7 LA |
4655 | r = -EFAULT; |
4656 | else | |
4657 | r = -E2BIG; | |
4658 | break; | |
8fcc4b59 | 4659 | } |
26b471c7 | 4660 | |
8fcc4b59 JM |
4661 | r = 0; |
4662 | break; | |
4663 | } | |
4664 | case KVM_SET_NESTED_STATE: { | |
4665 | struct kvm_nested_state __user *user_kvm_nested_state = argp; | |
4666 | struct kvm_nested_state kvm_state; | |
ad5996d9 | 4667 | int idx; |
8fcc4b59 JM |
4668 | |
4669 | r = -EINVAL; | |
33b22172 | 4670 | if (!kvm_x86_ops.nested_ops->set_state) |
8fcc4b59 JM |
4671 | break; |
4672 | ||
26b471c7 | 4673 | r = -EFAULT; |
8fcc4b59 | 4674 | if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state))) |
26b471c7 | 4675 | break; |
8fcc4b59 | 4676 | |
26b471c7 | 4677 | r = -EINVAL; |
8fcc4b59 | 4678 | if (kvm_state.size < sizeof(kvm_state)) |
26b471c7 | 4679 | break; |
8fcc4b59 JM |
4680 | |
4681 | if (kvm_state.flags & | |
8cab6507 | 4682 | ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE |
cc440cda PB |
4683 | | KVM_STATE_NESTED_EVMCS | KVM_STATE_NESTED_MTF_PENDING |
4684 | | KVM_STATE_NESTED_GIF_SET)) | |
26b471c7 | 4685 | break; |
8fcc4b59 JM |
4686 | |
4687 | /* nested_run_pending implies guest_mode. */ | |
8cab6507 VK |
4688 | if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING) |
4689 | && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE)) | |
26b471c7 | 4690 | break; |
8fcc4b59 | 4691 | |
ad5996d9 | 4692 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
33b22172 | 4693 | r = kvm_x86_ops.nested_ops->set_state(vcpu, user_kvm_nested_state, &kvm_state); |
ad5996d9 | 4694 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8fcc4b59 JM |
4695 | break; |
4696 | } | |
2bc39970 VK |
4697 | case KVM_GET_SUPPORTED_HV_CPUID: { |
4698 | struct kvm_cpuid2 __user *cpuid_arg = argp; | |
4699 | struct kvm_cpuid2 cpuid; | |
4700 | ||
4701 | r = -EFAULT; | |
4702 | if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid))) | |
4703 | goto out; | |
4704 | ||
4705 | r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid, | |
4706 | cpuid_arg->entries); | |
4707 | if (r) | |
4708 | goto out; | |
4709 | ||
4710 | r = -EFAULT; | |
4711 | if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid))) | |
4712 | goto out; | |
4713 | r = 0; | |
4714 | break; | |
4715 | } | |
313a3dc7 CO |
4716 | default: |
4717 | r = -EINVAL; | |
4718 | } | |
4719 | out: | |
d1ac91d8 | 4720 | kfree(u.buffer); |
9b062471 CD |
4721 | out_nofree: |
4722 | vcpu_put(vcpu); | |
313a3dc7 CO |
4723 | return r; |
4724 | } | |
4725 | ||
1499fa80 | 4726 | vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf) |
5b1c1493 CO |
4727 | { |
4728 | return VM_FAULT_SIGBUS; | |
4729 | } | |
4730 | ||
1fe779f8 CO |
4731 | static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr) |
4732 | { | |
4733 | int ret; | |
4734 | ||
4735 | if (addr > (unsigned int)(-3 * PAGE_SIZE)) | |
951179ce | 4736 | return -EINVAL; |
afaf0b2f | 4737 | ret = kvm_x86_ops.set_tss_addr(kvm, addr); |
1fe779f8 CO |
4738 | return ret; |
4739 | } | |
4740 | ||
b927a3ce SY |
4741 | static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm, |
4742 | u64 ident_addr) | |
4743 | { | |
afaf0b2f | 4744 | return kvm_x86_ops.set_identity_map_addr(kvm, ident_addr); |
b927a3ce SY |
4745 | } |
4746 | ||
1fe779f8 | 4747 | static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm, |
bc8a3d89 | 4748 | unsigned long kvm_nr_mmu_pages) |
1fe779f8 CO |
4749 | { |
4750 | if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES) | |
4751 | return -EINVAL; | |
4752 | ||
79fac95e | 4753 | mutex_lock(&kvm->slots_lock); |
1fe779f8 CO |
4754 | |
4755 | kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages); | |
f05e70ac | 4756 | kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages; |
1fe779f8 | 4757 | |
79fac95e | 4758 | mutex_unlock(&kvm->slots_lock); |
1fe779f8 CO |
4759 | return 0; |
4760 | } | |
4761 | ||
bc8a3d89 | 4762 | static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm) |
1fe779f8 | 4763 | { |
39de71ec | 4764 | return kvm->arch.n_max_mmu_pages; |
1fe779f8 CO |
4765 | } |
4766 | ||
1fe779f8 CO |
4767 | static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) |
4768 | { | |
90bca052 | 4769 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
4770 | int r; |
4771 | ||
4772 | r = 0; | |
4773 | switch (chip->chip_id) { | |
4774 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 | 4775 | memcpy(&chip->chip.pic, &pic->pics[0], |
1fe779f8 CO |
4776 | sizeof(struct kvm_pic_state)); |
4777 | break; | |
4778 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 | 4779 | memcpy(&chip->chip.pic, &pic->pics[1], |
1fe779f8 CO |
4780 | sizeof(struct kvm_pic_state)); |
4781 | break; | |
4782 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 4783 | kvm_get_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
4784 | break; |
4785 | default: | |
4786 | r = -EINVAL; | |
4787 | break; | |
4788 | } | |
4789 | return r; | |
4790 | } | |
4791 | ||
4792 | static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip) | |
4793 | { | |
90bca052 | 4794 | struct kvm_pic *pic = kvm->arch.vpic; |
1fe779f8 CO |
4795 | int r; |
4796 | ||
4797 | r = 0; | |
4798 | switch (chip->chip_id) { | |
4799 | case KVM_IRQCHIP_PIC_MASTER: | |
90bca052 DH |
4800 | spin_lock(&pic->lock); |
4801 | memcpy(&pic->pics[0], &chip->chip.pic, | |
1fe779f8 | 4802 | sizeof(struct kvm_pic_state)); |
90bca052 | 4803 | spin_unlock(&pic->lock); |
1fe779f8 CO |
4804 | break; |
4805 | case KVM_IRQCHIP_PIC_SLAVE: | |
90bca052 DH |
4806 | spin_lock(&pic->lock); |
4807 | memcpy(&pic->pics[1], &chip->chip.pic, | |
1fe779f8 | 4808 | sizeof(struct kvm_pic_state)); |
90bca052 | 4809 | spin_unlock(&pic->lock); |
1fe779f8 CO |
4810 | break; |
4811 | case KVM_IRQCHIP_IOAPIC: | |
33392b49 | 4812 | kvm_set_ioapic(kvm, &chip->chip.ioapic); |
1fe779f8 CO |
4813 | break; |
4814 | default: | |
4815 | r = -EINVAL; | |
4816 | break; | |
4817 | } | |
90bca052 | 4818 | kvm_pic_update_irq(pic); |
1fe779f8 CO |
4819 | return r; |
4820 | } | |
4821 | ||
e0f63cb9 SY |
4822 | static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps) |
4823 | { | |
34f3941c RK |
4824 | struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state; |
4825 | ||
4826 | BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels)); | |
4827 | ||
4828 | mutex_lock(&kps->lock); | |
4829 | memcpy(ps, &kps->channels, sizeof(*ps)); | |
4830 | mutex_unlock(&kps->lock); | |
2da29bcc | 4831 | return 0; |
e0f63cb9 SY |
4832 | } |
4833 | ||
4834 | static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps) | |
4835 | { | |
0185604c | 4836 | int i; |
09edea72 RK |
4837 | struct kvm_pit *pit = kvm->arch.vpit; |
4838 | ||
4839 | mutex_lock(&pit->pit_state.lock); | |
34f3941c | 4840 | memcpy(&pit->pit_state.channels, ps, sizeof(*ps)); |
0185604c | 4841 | for (i = 0; i < 3; i++) |
09edea72 RK |
4842 | kvm_pit_load_count(pit, i, ps->channels[i].count, 0); |
4843 | mutex_unlock(&pit->pit_state.lock); | |
2da29bcc | 4844 | return 0; |
e9f42757 BK |
4845 | } |
4846 | ||
4847 | static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
4848 | { | |
e9f42757 BK |
4849 | mutex_lock(&kvm->arch.vpit->pit_state.lock); |
4850 | memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels, | |
4851 | sizeof(ps->channels)); | |
4852 | ps->flags = kvm->arch.vpit->pit_state.flags; | |
4853 | mutex_unlock(&kvm->arch.vpit->pit_state.lock); | |
97e69aa6 | 4854 | memset(&ps->reserved, 0, sizeof(ps->reserved)); |
2da29bcc | 4855 | return 0; |
e9f42757 BK |
4856 | } |
4857 | ||
4858 | static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps) | |
4859 | { | |
2da29bcc | 4860 | int start = 0; |
0185604c | 4861 | int i; |
e9f42757 | 4862 | u32 prev_legacy, cur_legacy; |
09edea72 RK |
4863 | struct kvm_pit *pit = kvm->arch.vpit; |
4864 | ||
4865 | mutex_lock(&pit->pit_state.lock); | |
4866 | prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY; | |
e9f42757 BK |
4867 | cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY; |
4868 | if (!prev_legacy && cur_legacy) | |
4869 | start = 1; | |
09edea72 RK |
4870 | memcpy(&pit->pit_state.channels, &ps->channels, |
4871 | sizeof(pit->pit_state.channels)); | |
4872 | pit->pit_state.flags = ps->flags; | |
0185604c | 4873 | for (i = 0; i < 3; i++) |
09edea72 | 4874 | kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count, |
e5e57e7a | 4875 | start && i == 0); |
09edea72 | 4876 | mutex_unlock(&pit->pit_state.lock); |
2da29bcc | 4877 | return 0; |
e0f63cb9 SY |
4878 | } |
4879 | ||
52d939a0 MT |
4880 | static int kvm_vm_ioctl_reinject(struct kvm *kvm, |
4881 | struct kvm_reinject_control *control) | |
4882 | { | |
71474e2f RK |
4883 | struct kvm_pit *pit = kvm->arch.vpit; |
4884 | ||
71474e2f RK |
4885 | /* pit->pit_state.lock was overloaded to prevent userspace from getting |
4886 | * an inconsistent state after running multiple KVM_REINJECT_CONTROL | |
4887 | * ioctls in parallel. Use a separate lock if that ioctl isn't rare. | |
4888 | */ | |
4889 | mutex_lock(&pit->pit_state.lock); | |
4890 | kvm_pit_set_reinject(pit, control->pit_reinject); | |
4891 | mutex_unlock(&pit->pit_state.lock); | |
b39c90b6 | 4892 | |
52d939a0 MT |
4893 | return 0; |
4894 | } | |
4895 | ||
0dff0846 | 4896 | void kvm_arch_sync_dirty_log(struct kvm *kvm, struct kvm_memory_slot *memslot) |
5bb064dc | 4897 | { |
88178fd4 KH |
4898 | /* |
4899 | * Flush potentially hardware-cached dirty pages to dirty_bitmap. | |
4900 | */ | |
afaf0b2f SC |
4901 | if (kvm_x86_ops.flush_log_dirty) |
4902 | kvm_x86_ops.flush_log_dirty(kvm); | |
5bb064dc ZX |
4903 | } |
4904 | ||
aa2fbe6d YZ |
4905 | int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event, |
4906 | bool line_status) | |
23d43cf9 CD |
4907 | { |
4908 | if (!irqchip_in_kernel(kvm)) | |
4909 | return -ENXIO; | |
4910 | ||
4911 | irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID, | |
aa2fbe6d YZ |
4912 | irq_event->irq, irq_event->level, |
4913 | line_status); | |
23d43cf9 CD |
4914 | return 0; |
4915 | } | |
4916 | ||
e5d83c74 PB |
4917 | int kvm_vm_ioctl_enable_cap(struct kvm *kvm, |
4918 | struct kvm_enable_cap *cap) | |
90de4a18 NA |
4919 | { |
4920 | int r; | |
4921 | ||
4922 | if (cap->flags) | |
4923 | return -EINVAL; | |
4924 | ||
4925 | switch (cap->cap) { | |
4926 | case KVM_CAP_DISABLE_QUIRKS: | |
4927 | kvm->arch.disabled_quirks = cap->args[0]; | |
4928 | r = 0; | |
4929 | break; | |
49df6397 SR |
4930 | case KVM_CAP_SPLIT_IRQCHIP: { |
4931 | mutex_lock(&kvm->lock); | |
b053b2ae SR |
4932 | r = -EINVAL; |
4933 | if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS) | |
4934 | goto split_irqchip_unlock; | |
49df6397 SR |
4935 | r = -EEXIST; |
4936 | if (irqchip_in_kernel(kvm)) | |
4937 | goto split_irqchip_unlock; | |
557abc40 | 4938 | if (kvm->created_vcpus) |
49df6397 SR |
4939 | goto split_irqchip_unlock; |
4940 | r = kvm_setup_empty_irq_routing(kvm); | |
5c0aea0e | 4941 | if (r) |
49df6397 SR |
4942 | goto split_irqchip_unlock; |
4943 | /* Pairs with irqchip_in_kernel. */ | |
4944 | smp_wmb(); | |
49776faf | 4945 | kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT; |
b053b2ae | 4946 | kvm->arch.nr_reserved_ioapic_pins = cap->args[0]; |
49df6397 SR |
4947 | r = 0; |
4948 | split_irqchip_unlock: | |
4949 | mutex_unlock(&kvm->lock); | |
4950 | break; | |
4951 | } | |
37131313 RK |
4952 | case KVM_CAP_X2APIC_API: |
4953 | r = -EINVAL; | |
4954 | if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS) | |
4955 | break; | |
4956 | ||
4957 | if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS) | |
4958 | kvm->arch.x2apic_format = true; | |
c519265f RK |
4959 | if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK) |
4960 | kvm->arch.x2apic_broadcast_quirk_disabled = true; | |
37131313 RK |
4961 | |
4962 | r = 0; | |
4963 | break; | |
4d5422ce WL |
4964 | case KVM_CAP_X86_DISABLE_EXITS: |
4965 | r = -EINVAL; | |
4966 | if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS) | |
4967 | break; | |
4968 | ||
4969 | if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) && | |
4970 | kvm_can_mwait_in_guest()) | |
4971 | kvm->arch.mwait_in_guest = true; | |
766d3571 | 4972 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT) |
caa057a2 | 4973 | kvm->arch.hlt_in_guest = true; |
b31c114b WL |
4974 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE) |
4975 | kvm->arch.pause_in_guest = true; | |
b5170063 WL |
4976 | if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE) |
4977 | kvm->arch.cstate_in_guest = true; | |
4d5422ce WL |
4978 | r = 0; |
4979 | break; | |
6fbbde9a DS |
4980 | case KVM_CAP_MSR_PLATFORM_INFO: |
4981 | kvm->arch.guest_can_read_msr_platform_info = cap->args[0]; | |
4982 | r = 0; | |
c4f55198 JM |
4983 | break; |
4984 | case KVM_CAP_EXCEPTION_PAYLOAD: | |
4985 | kvm->arch.exception_payload_enabled = cap->args[0]; | |
4986 | r = 0; | |
6fbbde9a | 4987 | break; |
90de4a18 NA |
4988 | default: |
4989 | r = -EINVAL; | |
4990 | break; | |
4991 | } | |
4992 | return r; | |
4993 | } | |
4994 | ||
1fe779f8 CO |
4995 | long kvm_arch_vm_ioctl(struct file *filp, |
4996 | unsigned int ioctl, unsigned long arg) | |
4997 | { | |
4998 | struct kvm *kvm = filp->private_data; | |
4999 | void __user *argp = (void __user *)arg; | |
367e1319 | 5000 | int r = -ENOTTY; |
f0d66275 DH |
5001 | /* |
5002 | * This union makes it completely explicit to gcc-3.x | |
5003 | * that these two variables' stack usage should be | |
5004 | * combined, not added together. | |
5005 | */ | |
5006 | union { | |
5007 | struct kvm_pit_state ps; | |
e9f42757 | 5008 | struct kvm_pit_state2 ps2; |
c5ff41ce | 5009 | struct kvm_pit_config pit_config; |
f0d66275 | 5010 | } u; |
1fe779f8 CO |
5011 | |
5012 | switch (ioctl) { | |
5013 | case KVM_SET_TSS_ADDR: | |
5014 | r = kvm_vm_ioctl_set_tss_addr(kvm, arg); | |
1fe779f8 | 5015 | break; |
b927a3ce SY |
5016 | case KVM_SET_IDENTITY_MAP_ADDR: { |
5017 | u64 ident_addr; | |
5018 | ||
1af1ac91 DH |
5019 | mutex_lock(&kvm->lock); |
5020 | r = -EINVAL; | |
5021 | if (kvm->created_vcpus) | |
5022 | goto set_identity_unlock; | |
b927a3ce | 5023 | r = -EFAULT; |
0e96f31e | 5024 | if (copy_from_user(&ident_addr, argp, sizeof(ident_addr))) |
1af1ac91 | 5025 | goto set_identity_unlock; |
b927a3ce | 5026 | r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr); |
1af1ac91 DH |
5027 | set_identity_unlock: |
5028 | mutex_unlock(&kvm->lock); | |
b927a3ce SY |
5029 | break; |
5030 | } | |
1fe779f8 CO |
5031 | case KVM_SET_NR_MMU_PAGES: |
5032 | r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg); | |
1fe779f8 CO |
5033 | break; |
5034 | case KVM_GET_NR_MMU_PAGES: | |
5035 | r = kvm_vm_ioctl_get_nr_mmu_pages(kvm); | |
5036 | break; | |
3ddea128 | 5037 | case KVM_CREATE_IRQCHIP: { |
3ddea128 | 5038 | mutex_lock(&kvm->lock); |
09941366 | 5039 | |
3ddea128 | 5040 | r = -EEXIST; |
35e6eaa3 | 5041 | if (irqchip_in_kernel(kvm)) |
3ddea128 | 5042 | goto create_irqchip_unlock; |
09941366 | 5043 | |
3e515705 | 5044 | r = -EINVAL; |
557abc40 | 5045 | if (kvm->created_vcpus) |
3e515705 | 5046 | goto create_irqchip_unlock; |
09941366 RK |
5047 | |
5048 | r = kvm_pic_init(kvm); | |
5049 | if (r) | |
3ddea128 | 5050 | goto create_irqchip_unlock; |
09941366 RK |
5051 | |
5052 | r = kvm_ioapic_init(kvm); | |
5053 | if (r) { | |
09941366 | 5054 | kvm_pic_destroy(kvm); |
3ddea128 | 5055 | goto create_irqchip_unlock; |
09941366 RK |
5056 | } |
5057 | ||
399ec807 AK |
5058 | r = kvm_setup_default_irq_routing(kvm); |
5059 | if (r) { | |
72bb2fcd | 5060 | kvm_ioapic_destroy(kvm); |
09941366 | 5061 | kvm_pic_destroy(kvm); |
71ba994c | 5062 | goto create_irqchip_unlock; |
399ec807 | 5063 | } |
49776faf | 5064 | /* Write kvm->irq_routing before enabling irqchip_in_kernel. */ |
71ba994c | 5065 | smp_wmb(); |
49776faf | 5066 | kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL; |
3ddea128 MT |
5067 | create_irqchip_unlock: |
5068 | mutex_unlock(&kvm->lock); | |
1fe779f8 | 5069 | break; |
3ddea128 | 5070 | } |
7837699f | 5071 | case KVM_CREATE_PIT: |
c5ff41ce JK |
5072 | u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY; |
5073 | goto create_pit; | |
5074 | case KVM_CREATE_PIT2: | |
5075 | r = -EFAULT; | |
5076 | if (copy_from_user(&u.pit_config, argp, | |
5077 | sizeof(struct kvm_pit_config))) | |
5078 | goto out; | |
5079 | create_pit: | |
250715a6 | 5080 | mutex_lock(&kvm->lock); |
269e05e4 AK |
5081 | r = -EEXIST; |
5082 | if (kvm->arch.vpit) | |
5083 | goto create_pit_unlock; | |
7837699f | 5084 | r = -ENOMEM; |
c5ff41ce | 5085 | kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags); |
7837699f SY |
5086 | if (kvm->arch.vpit) |
5087 | r = 0; | |
269e05e4 | 5088 | create_pit_unlock: |
250715a6 | 5089 | mutex_unlock(&kvm->lock); |
7837699f | 5090 | break; |
1fe779f8 CO |
5091 | case KVM_GET_IRQCHIP: { |
5092 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 5093 | struct kvm_irqchip *chip; |
1fe779f8 | 5094 | |
ff5c2c03 SL |
5095 | chip = memdup_user(argp, sizeof(*chip)); |
5096 | if (IS_ERR(chip)) { | |
5097 | r = PTR_ERR(chip); | |
1fe779f8 | 5098 | goto out; |
ff5c2c03 SL |
5099 | } |
5100 | ||
1fe779f8 | 5101 | r = -ENXIO; |
826da321 | 5102 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
5103 | goto get_irqchip_out; |
5104 | r = kvm_vm_ioctl_get_irqchip(kvm, chip); | |
1fe779f8 | 5105 | if (r) |
f0d66275 | 5106 | goto get_irqchip_out; |
1fe779f8 | 5107 | r = -EFAULT; |
0e96f31e | 5108 | if (copy_to_user(argp, chip, sizeof(*chip))) |
f0d66275 | 5109 | goto get_irqchip_out; |
1fe779f8 | 5110 | r = 0; |
f0d66275 DH |
5111 | get_irqchip_out: |
5112 | kfree(chip); | |
1fe779f8 CO |
5113 | break; |
5114 | } | |
5115 | case KVM_SET_IRQCHIP: { | |
5116 | /* 0: PIC master, 1: PIC slave, 2: IOAPIC */ | |
ff5c2c03 | 5117 | struct kvm_irqchip *chip; |
1fe779f8 | 5118 | |
ff5c2c03 SL |
5119 | chip = memdup_user(argp, sizeof(*chip)); |
5120 | if (IS_ERR(chip)) { | |
5121 | r = PTR_ERR(chip); | |
1fe779f8 | 5122 | goto out; |
ff5c2c03 SL |
5123 | } |
5124 | ||
1fe779f8 | 5125 | r = -ENXIO; |
826da321 | 5126 | if (!irqchip_kernel(kvm)) |
f0d66275 DH |
5127 | goto set_irqchip_out; |
5128 | r = kvm_vm_ioctl_set_irqchip(kvm, chip); | |
f0d66275 DH |
5129 | set_irqchip_out: |
5130 | kfree(chip); | |
1fe779f8 CO |
5131 | break; |
5132 | } | |
e0f63cb9 | 5133 | case KVM_GET_PIT: { |
e0f63cb9 | 5134 | r = -EFAULT; |
f0d66275 | 5135 | if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
5136 | goto out; |
5137 | r = -ENXIO; | |
5138 | if (!kvm->arch.vpit) | |
5139 | goto out; | |
f0d66275 | 5140 | r = kvm_vm_ioctl_get_pit(kvm, &u.ps); |
e0f63cb9 SY |
5141 | if (r) |
5142 | goto out; | |
5143 | r = -EFAULT; | |
f0d66275 | 5144 | if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state))) |
e0f63cb9 SY |
5145 | goto out; |
5146 | r = 0; | |
5147 | break; | |
5148 | } | |
5149 | case KVM_SET_PIT: { | |
e0f63cb9 | 5150 | r = -EFAULT; |
0e96f31e | 5151 | if (copy_from_user(&u.ps, argp, sizeof(u.ps))) |
e0f63cb9 | 5152 | goto out; |
7289fdb5 | 5153 | mutex_lock(&kvm->lock); |
e0f63cb9 SY |
5154 | r = -ENXIO; |
5155 | if (!kvm->arch.vpit) | |
7289fdb5 | 5156 | goto set_pit_out; |
f0d66275 | 5157 | r = kvm_vm_ioctl_set_pit(kvm, &u.ps); |
7289fdb5 SR |
5158 | set_pit_out: |
5159 | mutex_unlock(&kvm->lock); | |
e0f63cb9 SY |
5160 | break; |
5161 | } | |
e9f42757 BK |
5162 | case KVM_GET_PIT2: { |
5163 | r = -ENXIO; | |
5164 | if (!kvm->arch.vpit) | |
5165 | goto out; | |
5166 | r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2); | |
5167 | if (r) | |
5168 | goto out; | |
5169 | r = -EFAULT; | |
5170 | if (copy_to_user(argp, &u.ps2, sizeof(u.ps2))) | |
5171 | goto out; | |
5172 | r = 0; | |
5173 | break; | |
5174 | } | |
5175 | case KVM_SET_PIT2: { | |
5176 | r = -EFAULT; | |
5177 | if (copy_from_user(&u.ps2, argp, sizeof(u.ps2))) | |
5178 | goto out; | |
7289fdb5 | 5179 | mutex_lock(&kvm->lock); |
e9f42757 BK |
5180 | r = -ENXIO; |
5181 | if (!kvm->arch.vpit) | |
7289fdb5 | 5182 | goto set_pit2_out; |
e9f42757 | 5183 | r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2); |
7289fdb5 SR |
5184 | set_pit2_out: |
5185 | mutex_unlock(&kvm->lock); | |
e9f42757 BK |
5186 | break; |
5187 | } | |
52d939a0 MT |
5188 | case KVM_REINJECT_CONTROL: { |
5189 | struct kvm_reinject_control control; | |
5190 | r = -EFAULT; | |
5191 | if (copy_from_user(&control, argp, sizeof(control))) | |
5192 | goto out; | |
cad23e72 ML |
5193 | r = -ENXIO; |
5194 | if (!kvm->arch.vpit) | |
5195 | goto out; | |
52d939a0 | 5196 | r = kvm_vm_ioctl_reinject(kvm, &control); |
52d939a0 MT |
5197 | break; |
5198 | } | |
d71ba788 PB |
5199 | case KVM_SET_BOOT_CPU_ID: |
5200 | r = 0; | |
5201 | mutex_lock(&kvm->lock); | |
557abc40 | 5202 | if (kvm->created_vcpus) |
d71ba788 PB |
5203 | r = -EBUSY; |
5204 | else | |
5205 | kvm->arch.bsp_vcpu_id = arg; | |
5206 | mutex_unlock(&kvm->lock); | |
5207 | break; | |
ffde22ac | 5208 | case KVM_XEN_HVM_CONFIG: { |
51776043 | 5209 | struct kvm_xen_hvm_config xhc; |
ffde22ac | 5210 | r = -EFAULT; |
51776043 | 5211 | if (copy_from_user(&xhc, argp, sizeof(xhc))) |
ffde22ac ES |
5212 | goto out; |
5213 | r = -EINVAL; | |
51776043 | 5214 | if (xhc.flags) |
ffde22ac | 5215 | goto out; |
51776043 | 5216 | memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc)); |
ffde22ac ES |
5217 | r = 0; |
5218 | break; | |
5219 | } | |
afbcf7ab | 5220 | case KVM_SET_CLOCK: { |
afbcf7ab GC |
5221 | struct kvm_clock_data user_ns; |
5222 | u64 now_ns; | |
afbcf7ab GC |
5223 | |
5224 | r = -EFAULT; | |
5225 | if (copy_from_user(&user_ns, argp, sizeof(user_ns))) | |
5226 | goto out; | |
5227 | ||
5228 | r = -EINVAL; | |
5229 | if (user_ns.flags) | |
5230 | goto out; | |
5231 | ||
5232 | r = 0; | |
0bc48bea RK |
5233 | /* |
5234 | * TODO: userspace has to take care of races with VCPU_RUN, so | |
5235 | * kvm_gen_update_masterclock() can be cut down to locked | |
5236 | * pvclock_update_vm_gtod_copy(). | |
5237 | */ | |
5238 | kvm_gen_update_masterclock(kvm); | |
e891a32e | 5239 | now_ns = get_kvmclock_ns(kvm); |
108b249c | 5240 | kvm->arch.kvmclock_offset += user_ns.clock - now_ns; |
0bc48bea | 5241 | kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE); |
afbcf7ab GC |
5242 | break; |
5243 | } | |
5244 | case KVM_GET_CLOCK: { | |
afbcf7ab GC |
5245 | struct kvm_clock_data user_ns; |
5246 | u64 now_ns; | |
5247 | ||
e891a32e | 5248 | now_ns = get_kvmclock_ns(kvm); |
108b249c | 5249 | user_ns.clock = now_ns; |
e3fd9a93 | 5250 | user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0; |
97e69aa6 | 5251 | memset(&user_ns.pad, 0, sizeof(user_ns.pad)); |
afbcf7ab GC |
5252 | |
5253 | r = -EFAULT; | |
5254 | if (copy_to_user(argp, &user_ns, sizeof(user_ns))) | |
5255 | goto out; | |
5256 | r = 0; | |
5257 | break; | |
5258 | } | |
5acc5c06 BS |
5259 | case KVM_MEMORY_ENCRYPT_OP: { |
5260 | r = -ENOTTY; | |
afaf0b2f SC |
5261 | if (kvm_x86_ops.mem_enc_op) |
5262 | r = kvm_x86_ops.mem_enc_op(kvm, argp); | |
5acc5c06 BS |
5263 | break; |
5264 | } | |
69eaedee BS |
5265 | case KVM_MEMORY_ENCRYPT_REG_REGION: { |
5266 | struct kvm_enc_region region; | |
5267 | ||
5268 | r = -EFAULT; | |
5269 | if (copy_from_user(®ion, argp, sizeof(region))) | |
5270 | goto out; | |
5271 | ||
5272 | r = -ENOTTY; | |
afaf0b2f SC |
5273 | if (kvm_x86_ops.mem_enc_reg_region) |
5274 | r = kvm_x86_ops.mem_enc_reg_region(kvm, ®ion); | |
69eaedee BS |
5275 | break; |
5276 | } | |
5277 | case KVM_MEMORY_ENCRYPT_UNREG_REGION: { | |
5278 | struct kvm_enc_region region; | |
5279 | ||
5280 | r = -EFAULT; | |
5281 | if (copy_from_user(®ion, argp, sizeof(region))) | |
5282 | goto out; | |
5283 | ||
5284 | r = -ENOTTY; | |
afaf0b2f SC |
5285 | if (kvm_x86_ops.mem_enc_unreg_region) |
5286 | r = kvm_x86_ops.mem_enc_unreg_region(kvm, ®ion); | |
69eaedee BS |
5287 | break; |
5288 | } | |
faeb7833 RK |
5289 | case KVM_HYPERV_EVENTFD: { |
5290 | struct kvm_hyperv_eventfd hvevfd; | |
5291 | ||
5292 | r = -EFAULT; | |
5293 | if (copy_from_user(&hvevfd, argp, sizeof(hvevfd))) | |
5294 | goto out; | |
5295 | r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd); | |
5296 | break; | |
5297 | } | |
66bb8a06 EH |
5298 | case KVM_SET_PMU_EVENT_FILTER: |
5299 | r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp); | |
5300 | break; | |
1fe779f8 | 5301 | default: |
ad6260da | 5302 | r = -ENOTTY; |
1fe779f8 CO |
5303 | } |
5304 | out: | |
5305 | return r; | |
5306 | } | |
5307 | ||
a16b043c | 5308 | static void kvm_init_msr_list(void) |
043405e1 | 5309 | { |
24c29b7a | 5310 | struct x86_pmu_capability x86_pmu; |
043405e1 | 5311 | u32 dummy[2]; |
7a5ee6ed | 5312 | unsigned i; |
043405e1 | 5313 | |
e2ada66e | 5314 | BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4, |
7a5ee6ed | 5315 | "Please update the fixed PMCs in msrs_to_saved_all[]"); |
24c29b7a PB |
5316 | |
5317 | perf_get_x86_pmu_capability(&x86_pmu); | |
e2ada66e | 5318 | |
6cbee2b9 XL |
5319 | num_msrs_to_save = 0; |
5320 | num_emulated_msrs = 0; | |
5321 | num_msr_based_features = 0; | |
5322 | ||
7a5ee6ed CQ |
5323 | for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) { |
5324 | if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0) | |
043405e1 | 5325 | continue; |
93c4adc7 PB |
5326 | |
5327 | /* | |
5328 | * Even MSRs that are valid in the host may not be exposed | |
9dbe6cf9 | 5329 | * to the guests in some cases. |
93c4adc7 | 5330 | */ |
7a5ee6ed | 5331 | switch (msrs_to_save_all[i]) { |
93c4adc7 | 5332 | case MSR_IA32_BNDCFGS: |
503234b3 | 5333 | if (!kvm_mpx_supported()) |
93c4adc7 PB |
5334 | continue; |
5335 | break; | |
9dbe6cf9 | 5336 | case MSR_TSC_AUX: |
13908510 | 5337 | if (!kvm_cpu_cap_has(X86_FEATURE_RDTSCP)) |
9dbe6cf9 PB |
5338 | continue; |
5339 | break; | |
f4cfcd2d ML |
5340 | case MSR_IA32_UMWAIT_CONTROL: |
5341 | if (!kvm_cpu_cap_has(X86_FEATURE_WAITPKG)) | |
5342 | continue; | |
5343 | break; | |
bf8c55d8 CP |
5344 | case MSR_IA32_RTIT_CTL: |
5345 | case MSR_IA32_RTIT_STATUS: | |
7b874c26 | 5346 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT)) |
bf8c55d8 CP |
5347 | continue; |
5348 | break; | |
5349 | case MSR_IA32_RTIT_CR3_MATCH: | |
7b874c26 | 5350 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
5351 | !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering)) |
5352 | continue; | |
5353 | break; | |
5354 | case MSR_IA32_RTIT_OUTPUT_BASE: | |
5355 | case MSR_IA32_RTIT_OUTPUT_MASK: | |
7b874c26 | 5356 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
bf8c55d8 CP |
5357 | (!intel_pt_validate_hw_cap(PT_CAP_topa_output) && |
5358 | !intel_pt_validate_hw_cap(PT_CAP_single_range_output))) | |
5359 | continue; | |
5360 | break; | |
7cb85fc4 | 5361 | case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: |
7b874c26 | 5362 | if (!kvm_cpu_cap_has(X86_FEATURE_INTEL_PT) || |
7a5ee6ed | 5363 | msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >= |
bf8c55d8 CP |
5364 | intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2) |
5365 | continue; | |
5366 | break; | |
cf05a67b | 5367 | case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17: |
7a5ee6ed | 5368 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >= |
24c29b7a PB |
5369 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
5370 | continue; | |
5371 | break; | |
cf05a67b | 5372 | case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17: |
7a5ee6ed | 5373 | if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >= |
24c29b7a PB |
5374 | min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp)) |
5375 | continue; | |
7cb85fc4 | 5376 | break; |
93c4adc7 PB |
5377 | default: |
5378 | break; | |
5379 | } | |
5380 | ||
7a5ee6ed | 5381 | msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i]; |
043405e1 | 5382 | } |
62ef68bb | 5383 | |
7a5ee6ed | 5384 | for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) { |
afaf0b2f | 5385 | if (!kvm_x86_ops.has_emulated_msr(emulated_msrs_all[i])) |
bc226f07 | 5386 | continue; |
62ef68bb | 5387 | |
7a5ee6ed | 5388 | emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i]; |
62ef68bb | 5389 | } |
801e459a | 5390 | |
7a5ee6ed | 5391 | for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) { |
801e459a TL |
5392 | struct kvm_msr_entry msr; |
5393 | ||
7a5ee6ed | 5394 | msr.index = msr_based_features_all[i]; |
66421c1e | 5395 | if (kvm_get_msr_feature(&msr)) |
801e459a TL |
5396 | continue; |
5397 | ||
7a5ee6ed | 5398 | msr_based_features[num_msr_based_features++] = msr_based_features_all[i]; |
801e459a | 5399 | } |
043405e1 CO |
5400 | } |
5401 | ||
bda9020e MT |
5402 | static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len, |
5403 | const void *v) | |
bbd9b64e | 5404 | { |
70252a10 AK |
5405 | int handled = 0; |
5406 | int n; | |
5407 | ||
5408 | do { | |
5409 | n = min(len, 8); | |
bce87cce | 5410 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
5411 | !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v)) |
5412 | && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 AK |
5413 | break; |
5414 | handled += n; | |
5415 | addr += n; | |
5416 | len -= n; | |
5417 | v += n; | |
5418 | } while (len); | |
bbd9b64e | 5419 | |
70252a10 | 5420 | return handled; |
bbd9b64e CO |
5421 | } |
5422 | ||
bda9020e | 5423 | static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v) |
bbd9b64e | 5424 | { |
70252a10 AK |
5425 | int handled = 0; |
5426 | int n; | |
5427 | ||
5428 | do { | |
5429 | n = min(len, 8); | |
bce87cce | 5430 | if (!(lapic_in_kernel(vcpu) && |
e32edf4f NN |
5431 | !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev, |
5432 | addr, n, v)) | |
5433 | && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v)) | |
70252a10 | 5434 | break; |
e39d200f | 5435 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v); |
70252a10 AK |
5436 | handled += n; |
5437 | addr += n; | |
5438 | len -= n; | |
5439 | v += n; | |
5440 | } while (len); | |
bbd9b64e | 5441 | |
70252a10 | 5442 | return handled; |
bbd9b64e CO |
5443 | } |
5444 | ||
2dafc6c2 GN |
5445 | static void kvm_set_segment(struct kvm_vcpu *vcpu, |
5446 | struct kvm_segment *var, int seg) | |
5447 | { | |
afaf0b2f | 5448 | kvm_x86_ops.set_segment(vcpu, var, seg); |
2dafc6c2 GN |
5449 | } |
5450 | ||
5451 | void kvm_get_segment(struct kvm_vcpu *vcpu, | |
5452 | struct kvm_segment *var, int seg) | |
5453 | { | |
afaf0b2f | 5454 | kvm_x86_ops.get_segment(vcpu, var, seg); |
2dafc6c2 GN |
5455 | } |
5456 | ||
54987b7a PB |
5457 | gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access, |
5458 | struct x86_exception *exception) | |
02f59dc9 JR |
5459 | { |
5460 | gpa_t t_gpa; | |
02f59dc9 JR |
5461 | |
5462 | BUG_ON(!mmu_is_nested(vcpu)); | |
5463 | ||
5464 | /* NPT walks are always user-walks */ | |
5465 | access |= PFERR_USER_MASK; | |
44dd3ffa | 5466 | t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception); |
02f59dc9 JR |
5467 | |
5468 | return t_gpa; | |
5469 | } | |
5470 | ||
ab9ae313 AK |
5471 | gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva, |
5472 | struct x86_exception *exception) | |
1871c602 | 5473 | { |
afaf0b2f | 5474 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
ab9ae313 | 5475 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
5476 | } |
5477 | ||
ab9ae313 AK |
5478 | gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva, |
5479 | struct x86_exception *exception) | |
1871c602 | 5480 | { |
afaf0b2f | 5481 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 5482 | access |= PFERR_FETCH_MASK; |
ab9ae313 | 5483 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
5484 | } |
5485 | ||
ab9ae313 AK |
5486 | gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva, |
5487 | struct x86_exception *exception) | |
1871c602 | 5488 | { |
afaf0b2f | 5489 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
1871c602 | 5490 | access |= PFERR_WRITE_MASK; |
ab9ae313 | 5491 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
1871c602 GN |
5492 | } |
5493 | ||
5494 | /* uses this to access any guest's mapped memory without checking CPL */ | |
ab9ae313 AK |
5495 | gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva, |
5496 | struct x86_exception *exception) | |
1871c602 | 5497 | { |
ab9ae313 | 5498 | return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception); |
1871c602 GN |
5499 | } |
5500 | ||
5501 | static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, | |
5502 | struct kvm_vcpu *vcpu, u32 access, | |
bcc55cba | 5503 | struct x86_exception *exception) |
bbd9b64e CO |
5504 | { |
5505 | void *data = val; | |
10589a46 | 5506 | int r = X86EMUL_CONTINUE; |
bbd9b64e CO |
5507 | |
5508 | while (bytes) { | |
14dfe855 | 5509 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access, |
ab9ae313 | 5510 | exception); |
bbd9b64e | 5511 | unsigned offset = addr & (PAGE_SIZE-1); |
77c2002e | 5512 | unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset); |
bbd9b64e CO |
5513 | int ret; |
5514 | ||
bcc55cba | 5515 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 5516 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa PB |
5517 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data, |
5518 | offset, toread); | |
10589a46 | 5519 | if (ret < 0) { |
c3cd7ffa | 5520 | r = X86EMUL_IO_NEEDED; |
10589a46 MT |
5521 | goto out; |
5522 | } | |
bbd9b64e | 5523 | |
77c2002e IE |
5524 | bytes -= toread; |
5525 | data += toread; | |
5526 | addr += toread; | |
bbd9b64e | 5527 | } |
10589a46 | 5528 | out: |
10589a46 | 5529 | return r; |
bbd9b64e | 5530 | } |
77c2002e | 5531 | |
1871c602 | 5532 | /* used for instruction fetching */ |
0f65dd70 AK |
5533 | static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt, |
5534 | gva_t addr, void *val, unsigned int bytes, | |
bcc55cba | 5535 | struct x86_exception *exception) |
1871c602 | 5536 | { |
0f65dd70 | 5537 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
afaf0b2f | 5538 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
44583cba PB |
5539 | unsigned offset; |
5540 | int ret; | |
0f65dd70 | 5541 | |
44583cba PB |
5542 | /* Inline kvm_read_guest_virt_helper for speed. */ |
5543 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK, | |
5544 | exception); | |
5545 | if (unlikely(gpa == UNMAPPED_GVA)) | |
5546 | return X86EMUL_PROPAGATE_FAULT; | |
5547 | ||
5548 | offset = addr & (PAGE_SIZE-1); | |
5549 | if (WARN_ON(offset + bytes > PAGE_SIZE)) | |
5550 | bytes = (unsigned)PAGE_SIZE - offset; | |
54bf36aa PB |
5551 | ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val, |
5552 | offset, bytes); | |
44583cba PB |
5553 | if (unlikely(ret < 0)) |
5554 | return X86EMUL_IO_NEEDED; | |
5555 | ||
5556 | return X86EMUL_CONTINUE; | |
1871c602 GN |
5557 | } |
5558 | ||
ce14e868 | 5559 | int kvm_read_guest_virt(struct kvm_vcpu *vcpu, |
0f65dd70 | 5560 | gva_t addr, void *val, unsigned int bytes, |
bcc55cba | 5561 | struct x86_exception *exception) |
1871c602 | 5562 | { |
afaf0b2f | 5563 | u32 access = (kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0; |
0f65dd70 | 5564 | |
353c0956 PB |
5565 | /* |
5566 | * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED | |
5567 | * is returned, but our callers are not ready for that and they blindly | |
5568 | * call kvm_inject_page_fault. Ensure that they at least do not leak | |
5569 | * uninitialized kernel stack memory into cr2 and error code. | |
5570 | */ | |
5571 | memset(exception, 0, sizeof(*exception)); | |
1871c602 | 5572 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, |
bcc55cba | 5573 | exception); |
1871c602 | 5574 | } |
064aea77 | 5575 | EXPORT_SYMBOL_GPL(kvm_read_guest_virt); |
1871c602 | 5576 | |
ce14e868 PB |
5577 | static int emulator_read_std(struct x86_emulate_ctxt *ctxt, |
5578 | gva_t addr, void *val, unsigned int bytes, | |
3c9fa24c | 5579 | struct x86_exception *exception, bool system) |
1871c602 | 5580 | { |
0f65dd70 | 5581 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
3c9fa24c PB |
5582 | u32 access = 0; |
5583 | ||
afaf0b2f | 5584 | if (!system && kvm_x86_ops.get_cpl(vcpu) == 3) |
3c9fa24c PB |
5585 | access |= PFERR_USER_MASK; |
5586 | ||
5587 | return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception); | |
1871c602 GN |
5588 | } |
5589 | ||
7a036a6f RK |
5590 | static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt, |
5591 | unsigned long addr, void *val, unsigned int bytes) | |
5592 | { | |
5593 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
5594 | int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes); | |
5595 | ||
5596 | return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE; | |
5597 | } | |
5598 | ||
ce14e868 PB |
5599 | static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes, |
5600 | struct kvm_vcpu *vcpu, u32 access, | |
5601 | struct x86_exception *exception) | |
77c2002e IE |
5602 | { |
5603 | void *data = val; | |
5604 | int r = X86EMUL_CONTINUE; | |
5605 | ||
5606 | while (bytes) { | |
14dfe855 | 5607 | gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, |
ce14e868 | 5608 | access, |
ab9ae313 | 5609 | exception); |
77c2002e IE |
5610 | unsigned offset = addr & (PAGE_SIZE-1); |
5611 | unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset); | |
5612 | int ret; | |
5613 | ||
bcc55cba | 5614 | if (gpa == UNMAPPED_GVA) |
ab9ae313 | 5615 | return X86EMUL_PROPAGATE_FAULT; |
54bf36aa | 5616 | ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite); |
77c2002e | 5617 | if (ret < 0) { |
c3cd7ffa | 5618 | r = X86EMUL_IO_NEEDED; |
77c2002e IE |
5619 | goto out; |
5620 | } | |
5621 | ||
5622 | bytes -= towrite; | |
5623 | data += towrite; | |
5624 | addr += towrite; | |
5625 | } | |
5626 | out: | |
5627 | return r; | |
5628 | } | |
ce14e868 PB |
5629 | |
5630 | static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val, | |
3c9fa24c PB |
5631 | unsigned int bytes, struct x86_exception *exception, |
5632 | bool system) | |
ce14e868 PB |
5633 | { |
5634 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
3c9fa24c PB |
5635 | u32 access = PFERR_WRITE_MASK; |
5636 | ||
afaf0b2f | 5637 | if (!system && kvm_x86_ops.get_cpl(vcpu) == 3) |
3c9fa24c | 5638 | access |= PFERR_USER_MASK; |
ce14e868 PB |
5639 | |
5640 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, | |
3c9fa24c | 5641 | access, exception); |
ce14e868 PB |
5642 | } |
5643 | ||
5644 | int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val, | |
5645 | unsigned int bytes, struct x86_exception *exception) | |
5646 | { | |
c595ceee PB |
5647 | /* kvm_write_guest_virt_system can pull in tons of pages. */ |
5648 | vcpu->arch.l1tf_flush_l1d = true; | |
5649 | ||
ce14e868 PB |
5650 | return kvm_write_guest_virt_helper(addr, val, bytes, vcpu, |
5651 | PFERR_WRITE_MASK, exception); | |
5652 | } | |
6a4d7550 | 5653 | EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system); |
77c2002e | 5654 | |
082d06ed WL |
5655 | int handle_ud(struct kvm_vcpu *vcpu) |
5656 | { | |
b3dc0695 | 5657 | static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX }; |
6c86eedc | 5658 | int emul_type = EMULTYPE_TRAP_UD; |
6c86eedc WL |
5659 | char sig[5]; /* ud2; .ascii "kvm" */ |
5660 | struct x86_exception e; | |
5661 | ||
5662 | if (force_emulation_prefix && | |
3c9fa24c PB |
5663 | kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu), |
5664 | sig, sizeof(sig), &e) == 0 && | |
b3dc0695 | 5665 | memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) { |
6c86eedc | 5666 | kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig)); |
b4000606 | 5667 | emul_type = EMULTYPE_TRAP_UD_FORCED; |
6c86eedc | 5668 | } |
082d06ed | 5669 | |
60fc3d02 | 5670 | return kvm_emulate_instruction(vcpu, emul_type); |
082d06ed WL |
5671 | } |
5672 | EXPORT_SYMBOL_GPL(handle_ud); | |
5673 | ||
0f89b207 TL |
5674 | static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
5675 | gpa_t gpa, bool write) | |
5676 | { | |
5677 | /* For APIC access vmexit */ | |
5678 | if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
5679 | return 1; | |
5680 | ||
5681 | if (vcpu_match_mmio_gpa(vcpu, gpa)) { | |
5682 | trace_vcpu_match_mmio(gva, gpa, write, true); | |
5683 | return 1; | |
5684 | } | |
5685 | ||
5686 | return 0; | |
5687 | } | |
5688 | ||
af7cc7d1 XG |
5689 | static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva, |
5690 | gpa_t *gpa, struct x86_exception *exception, | |
5691 | bool write) | |
5692 | { | |
afaf0b2f | 5693 | u32 access = ((kvm_x86_ops.get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0) |
97d64b78 | 5694 | | (write ? PFERR_WRITE_MASK : 0); |
af7cc7d1 | 5695 | |
be94f6b7 HH |
5696 | /* |
5697 | * currently PKRU is only applied to ept enabled guest so | |
5698 | * there is no pkey in EPT page table for L1 guest or EPT | |
5699 | * shadow page table for L2 guest. | |
5700 | */ | |
97d64b78 | 5701 | if (vcpu_match_mmio_gva(vcpu, gva) |
97ec8c06 | 5702 | && !permission_fault(vcpu, vcpu->arch.walk_mmu, |
871bd034 | 5703 | vcpu->arch.mmio_access, 0, access)) { |
bebb106a XG |
5704 | *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT | |
5705 | (gva & (PAGE_SIZE - 1)); | |
4f022648 | 5706 | trace_vcpu_match_mmio(gva, *gpa, write, false); |
bebb106a XG |
5707 | return 1; |
5708 | } | |
5709 | ||
af7cc7d1 XG |
5710 | *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception); |
5711 | ||
5712 | if (*gpa == UNMAPPED_GVA) | |
5713 | return -1; | |
5714 | ||
0f89b207 | 5715 | return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write); |
af7cc7d1 XG |
5716 | } |
5717 | ||
3200f405 | 5718 | int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa, |
bcc55cba | 5719 | const void *val, int bytes) |
bbd9b64e CO |
5720 | { |
5721 | int ret; | |
5722 | ||
54bf36aa | 5723 | ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes); |
9f811285 | 5724 | if (ret < 0) |
bbd9b64e | 5725 | return 0; |
0eb05bf2 | 5726 | kvm_page_track_write(vcpu, gpa, val, bytes); |
bbd9b64e CO |
5727 | return 1; |
5728 | } | |
5729 | ||
77d197b2 XG |
5730 | struct read_write_emulator_ops { |
5731 | int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val, | |
5732 | int bytes); | |
5733 | int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5734 | void *val, int bytes); | |
5735 | int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5736 | int bytes, void *val); | |
5737 | int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5738 | void *val, int bytes); | |
5739 | bool write; | |
5740 | }; | |
5741 | ||
5742 | static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes) | |
5743 | { | |
5744 | if (vcpu->mmio_read_completed) { | |
77d197b2 | 5745 | trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes, |
e39d200f | 5746 | vcpu->mmio_fragments[0].gpa, val); |
77d197b2 XG |
5747 | vcpu->mmio_read_completed = 0; |
5748 | return 1; | |
5749 | } | |
5750 | ||
5751 | return 0; | |
5752 | } | |
5753 | ||
5754 | static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5755 | void *val, int bytes) | |
5756 | { | |
54bf36aa | 5757 | return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes); |
77d197b2 XG |
5758 | } |
5759 | ||
5760 | static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5761 | void *val, int bytes) | |
5762 | { | |
5763 | return emulator_write_phys(vcpu, gpa, val, bytes); | |
5764 | } | |
5765 | ||
5766 | static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val) | |
5767 | { | |
e39d200f | 5768 | trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val); |
77d197b2 XG |
5769 | return vcpu_mmio_write(vcpu, gpa, bytes, val); |
5770 | } | |
5771 | ||
5772 | static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5773 | void *val, int bytes) | |
5774 | { | |
e39d200f | 5775 | trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL); |
77d197b2 XG |
5776 | return X86EMUL_IO_NEEDED; |
5777 | } | |
5778 | ||
5779 | static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, | |
5780 | void *val, int bytes) | |
5781 | { | |
f78146b0 AK |
5782 | struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0]; |
5783 | ||
87da7e66 | 5784 | memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len)); |
77d197b2 XG |
5785 | return X86EMUL_CONTINUE; |
5786 | } | |
5787 | ||
0fbe9b0b | 5788 | static const struct read_write_emulator_ops read_emultor = { |
77d197b2 XG |
5789 | .read_write_prepare = read_prepare, |
5790 | .read_write_emulate = read_emulate, | |
5791 | .read_write_mmio = vcpu_mmio_read, | |
5792 | .read_write_exit_mmio = read_exit_mmio, | |
5793 | }; | |
5794 | ||
0fbe9b0b | 5795 | static const struct read_write_emulator_ops write_emultor = { |
77d197b2 XG |
5796 | .read_write_emulate = write_emulate, |
5797 | .read_write_mmio = write_mmio, | |
5798 | .read_write_exit_mmio = write_exit_mmio, | |
5799 | .write = true, | |
5800 | }; | |
5801 | ||
22388a3c XG |
5802 | static int emulator_read_write_onepage(unsigned long addr, void *val, |
5803 | unsigned int bytes, | |
5804 | struct x86_exception *exception, | |
5805 | struct kvm_vcpu *vcpu, | |
0fbe9b0b | 5806 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 5807 | { |
af7cc7d1 XG |
5808 | gpa_t gpa; |
5809 | int handled, ret; | |
22388a3c | 5810 | bool write = ops->write; |
f78146b0 | 5811 | struct kvm_mmio_fragment *frag; |
c9b8b07c | 5812 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
0f89b207 TL |
5813 | |
5814 | /* | |
5815 | * If the exit was due to a NPF we may already have a GPA. | |
5816 | * If the GPA is present, use it to avoid the GVA to GPA table walk. | |
5817 | * Note, this cannot be used on string operations since string | |
5818 | * operation using rep will only have the initial GPA from the NPF | |
5819 | * occurred. | |
5820 | */ | |
744e699c SC |
5821 | if (ctxt->gpa_available && emulator_can_use_gpa(ctxt) && |
5822 | (addr & ~PAGE_MASK) == (ctxt->gpa_val & ~PAGE_MASK)) { | |
5823 | gpa = ctxt->gpa_val; | |
618232e2 BS |
5824 | ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write); |
5825 | } else { | |
5826 | ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write); | |
5827 | if (ret < 0) | |
5828 | return X86EMUL_PROPAGATE_FAULT; | |
0f89b207 | 5829 | } |
10589a46 | 5830 | |
618232e2 | 5831 | if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes)) |
bbd9b64e CO |
5832 | return X86EMUL_CONTINUE; |
5833 | ||
bbd9b64e CO |
5834 | /* |
5835 | * Is this MMIO handled locally? | |
5836 | */ | |
22388a3c | 5837 | handled = ops->read_write_mmio(vcpu, gpa, bytes, val); |
70252a10 | 5838 | if (handled == bytes) |
bbd9b64e | 5839 | return X86EMUL_CONTINUE; |
bbd9b64e | 5840 | |
70252a10 AK |
5841 | gpa += handled; |
5842 | bytes -= handled; | |
5843 | val += handled; | |
5844 | ||
87da7e66 XG |
5845 | WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS); |
5846 | frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++]; | |
5847 | frag->gpa = gpa; | |
5848 | frag->data = val; | |
5849 | frag->len = bytes; | |
f78146b0 | 5850 | return X86EMUL_CONTINUE; |
bbd9b64e CO |
5851 | } |
5852 | ||
52eb5a6d XL |
5853 | static int emulator_read_write(struct x86_emulate_ctxt *ctxt, |
5854 | unsigned long addr, | |
22388a3c XG |
5855 | void *val, unsigned int bytes, |
5856 | struct x86_exception *exception, | |
0fbe9b0b | 5857 | const struct read_write_emulator_ops *ops) |
bbd9b64e | 5858 | { |
0f65dd70 | 5859 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
f78146b0 AK |
5860 | gpa_t gpa; |
5861 | int rc; | |
5862 | ||
5863 | if (ops->read_write_prepare && | |
5864 | ops->read_write_prepare(vcpu, val, bytes)) | |
5865 | return X86EMUL_CONTINUE; | |
5866 | ||
5867 | vcpu->mmio_nr_fragments = 0; | |
0f65dd70 | 5868 | |
bbd9b64e CO |
5869 | /* Crossing a page boundary? */ |
5870 | if (((addr + bytes - 1) ^ addr) & PAGE_MASK) { | |
f78146b0 | 5871 | int now; |
bbd9b64e CO |
5872 | |
5873 | now = -addr & ~PAGE_MASK; | |
22388a3c XG |
5874 | rc = emulator_read_write_onepage(addr, val, now, exception, |
5875 | vcpu, ops); | |
5876 | ||
bbd9b64e CO |
5877 | if (rc != X86EMUL_CONTINUE) |
5878 | return rc; | |
5879 | addr += now; | |
bac15531 NA |
5880 | if (ctxt->mode != X86EMUL_MODE_PROT64) |
5881 | addr = (u32)addr; | |
bbd9b64e CO |
5882 | val += now; |
5883 | bytes -= now; | |
5884 | } | |
22388a3c | 5885 | |
f78146b0 AK |
5886 | rc = emulator_read_write_onepage(addr, val, bytes, exception, |
5887 | vcpu, ops); | |
5888 | if (rc != X86EMUL_CONTINUE) | |
5889 | return rc; | |
5890 | ||
5891 | if (!vcpu->mmio_nr_fragments) | |
5892 | return rc; | |
5893 | ||
5894 | gpa = vcpu->mmio_fragments[0].gpa; | |
5895 | ||
5896 | vcpu->mmio_needed = 1; | |
5897 | vcpu->mmio_cur_fragment = 0; | |
5898 | ||
87da7e66 | 5899 | vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len); |
f78146b0 AK |
5900 | vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write; |
5901 | vcpu->run->exit_reason = KVM_EXIT_MMIO; | |
5902 | vcpu->run->mmio.phys_addr = gpa; | |
5903 | ||
5904 | return ops->read_write_exit_mmio(vcpu, gpa, val, bytes); | |
22388a3c XG |
5905 | } |
5906 | ||
5907 | static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt, | |
5908 | unsigned long addr, | |
5909 | void *val, | |
5910 | unsigned int bytes, | |
5911 | struct x86_exception *exception) | |
5912 | { | |
5913 | return emulator_read_write(ctxt, addr, val, bytes, | |
5914 | exception, &read_emultor); | |
5915 | } | |
5916 | ||
52eb5a6d | 5917 | static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt, |
22388a3c XG |
5918 | unsigned long addr, |
5919 | const void *val, | |
5920 | unsigned int bytes, | |
5921 | struct x86_exception *exception) | |
5922 | { | |
5923 | return emulator_read_write(ctxt, addr, (void *)val, bytes, | |
5924 | exception, &write_emultor); | |
bbd9b64e | 5925 | } |
bbd9b64e | 5926 | |
daea3e73 AK |
5927 | #define CMPXCHG_TYPE(t, ptr, old, new) \ |
5928 | (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old)) | |
5929 | ||
5930 | #ifdef CONFIG_X86_64 | |
5931 | # define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new) | |
5932 | #else | |
5933 | # define CMPXCHG64(ptr, old, new) \ | |
9749a6c0 | 5934 | (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old)) |
daea3e73 AK |
5935 | #endif |
5936 | ||
0f65dd70 AK |
5937 | static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt, |
5938 | unsigned long addr, | |
bbd9b64e CO |
5939 | const void *old, |
5940 | const void *new, | |
5941 | unsigned int bytes, | |
0f65dd70 | 5942 | struct x86_exception *exception) |
bbd9b64e | 5943 | { |
42e35f80 | 5944 | struct kvm_host_map map; |
0f65dd70 | 5945 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
9de6fe3c | 5946 | u64 page_line_mask; |
daea3e73 | 5947 | gpa_t gpa; |
daea3e73 AK |
5948 | char *kaddr; |
5949 | bool exchanged; | |
2bacc55c | 5950 | |
daea3e73 AK |
5951 | /* guests cmpxchg8b have to be emulated atomically */ |
5952 | if (bytes > 8 || (bytes & (bytes - 1))) | |
5953 | goto emul_write; | |
10589a46 | 5954 | |
daea3e73 | 5955 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL); |
2bacc55c | 5956 | |
daea3e73 AK |
5957 | if (gpa == UNMAPPED_GVA || |
5958 | (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE) | |
5959 | goto emul_write; | |
2bacc55c | 5960 | |
9de6fe3c XL |
5961 | /* |
5962 | * Emulate the atomic as a straight write to avoid #AC if SLD is | |
5963 | * enabled in the host and the access splits a cache line. | |
5964 | */ | |
5965 | if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) | |
5966 | page_line_mask = ~(cache_line_size() - 1); | |
5967 | else | |
5968 | page_line_mask = PAGE_MASK; | |
5969 | ||
5970 | if (((gpa + bytes - 1) & page_line_mask) != (gpa & page_line_mask)) | |
daea3e73 | 5971 | goto emul_write; |
72dc67a6 | 5972 | |
42e35f80 | 5973 | if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map)) |
c19b8bd6 | 5974 | goto emul_write; |
72dc67a6 | 5975 | |
42e35f80 KA |
5976 | kaddr = map.hva + offset_in_page(gpa); |
5977 | ||
daea3e73 AK |
5978 | switch (bytes) { |
5979 | case 1: | |
5980 | exchanged = CMPXCHG_TYPE(u8, kaddr, old, new); | |
5981 | break; | |
5982 | case 2: | |
5983 | exchanged = CMPXCHG_TYPE(u16, kaddr, old, new); | |
5984 | break; | |
5985 | case 4: | |
5986 | exchanged = CMPXCHG_TYPE(u32, kaddr, old, new); | |
5987 | break; | |
5988 | case 8: | |
5989 | exchanged = CMPXCHG64(kaddr, old, new); | |
5990 | break; | |
5991 | default: | |
5992 | BUG(); | |
2bacc55c | 5993 | } |
42e35f80 KA |
5994 | |
5995 | kvm_vcpu_unmap(vcpu, &map, true); | |
daea3e73 AK |
5996 | |
5997 | if (!exchanged) | |
5998 | return X86EMUL_CMPXCHG_FAILED; | |
5999 | ||
0eb05bf2 | 6000 | kvm_page_track_write(vcpu, gpa, new, bytes); |
8f6abd06 GN |
6001 | |
6002 | return X86EMUL_CONTINUE; | |
4a5f48f6 | 6003 | |
3200f405 | 6004 | emul_write: |
daea3e73 | 6005 | printk_once(KERN_WARNING "kvm: emulating exchange as write\n"); |
2bacc55c | 6006 | |
0f65dd70 | 6007 | return emulator_write_emulated(ctxt, addr, new, bytes, exception); |
bbd9b64e CO |
6008 | } |
6009 | ||
cf8f70bf GN |
6010 | static int kernel_pio(struct kvm_vcpu *vcpu, void *pd) |
6011 | { | |
cbfc6c91 | 6012 | int r = 0, i; |
cf8f70bf | 6013 | |
cbfc6c91 WL |
6014 | for (i = 0; i < vcpu->arch.pio.count; i++) { |
6015 | if (vcpu->arch.pio.in) | |
6016 | r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port, | |
6017 | vcpu->arch.pio.size, pd); | |
6018 | else | |
6019 | r = kvm_io_bus_write(vcpu, KVM_PIO_BUS, | |
6020 | vcpu->arch.pio.port, vcpu->arch.pio.size, | |
6021 | pd); | |
6022 | if (r) | |
6023 | break; | |
6024 | pd += vcpu->arch.pio.size; | |
6025 | } | |
cf8f70bf GN |
6026 | return r; |
6027 | } | |
6028 | ||
6f6fbe98 XG |
6029 | static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size, |
6030 | unsigned short port, void *val, | |
6031 | unsigned int count, bool in) | |
cf8f70bf | 6032 | { |
cf8f70bf | 6033 | vcpu->arch.pio.port = port; |
6f6fbe98 | 6034 | vcpu->arch.pio.in = in; |
7972995b | 6035 | vcpu->arch.pio.count = count; |
cf8f70bf GN |
6036 | vcpu->arch.pio.size = size; |
6037 | ||
6038 | if (!kernel_pio(vcpu, vcpu->arch.pio_data)) { | |
7972995b | 6039 | vcpu->arch.pio.count = 0; |
cf8f70bf GN |
6040 | return 1; |
6041 | } | |
6042 | ||
6043 | vcpu->run->exit_reason = KVM_EXIT_IO; | |
6f6fbe98 | 6044 | vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT; |
cf8f70bf GN |
6045 | vcpu->run->io.size = size; |
6046 | vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE; | |
6047 | vcpu->run->io.count = count; | |
6048 | vcpu->run->io.port = port; | |
6049 | ||
6050 | return 0; | |
6051 | } | |
6052 | ||
2e3bb4d8 SC |
6053 | static int emulator_pio_in(struct kvm_vcpu *vcpu, int size, |
6054 | unsigned short port, void *val, unsigned int count) | |
cf8f70bf | 6055 | { |
6f6fbe98 | 6056 | int ret; |
ca1d4a9e | 6057 | |
6f6fbe98 XG |
6058 | if (vcpu->arch.pio.count) |
6059 | goto data_avail; | |
cf8f70bf | 6060 | |
cbfc6c91 WL |
6061 | memset(vcpu->arch.pio_data, 0, size * count); |
6062 | ||
6f6fbe98 XG |
6063 | ret = emulator_pio_in_out(vcpu, size, port, val, count, true); |
6064 | if (ret) { | |
6065 | data_avail: | |
6066 | memcpy(val, vcpu->arch.pio_data, size * count); | |
1171903d | 6067 | trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data); |
7972995b | 6068 | vcpu->arch.pio.count = 0; |
cf8f70bf GN |
6069 | return 1; |
6070 | } | |
6071 | ||
cf8f70bf GN |
6072 | return 0; |
6073 | } | |
6074 | ||
2e3bb4d8 SC |
6075 | static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt, |
6076 | int size, unsigned short port, void *val, | |
6077 | unsigned int count) | |
6f6fbe98 | 6078 | { |
2e3bb4d8 | 6079 | return emulator_pio_in(emul_to_vcpu(ctxt), size, port, val, count); |
6f6fbe98 | 6080 | |
2e3bb4d8 | 6081 | } |
6f6fbe98 | 6082 | |
2e3bb4d8 SC |
6083 | static int emulator_pio_out(struct kvm_vcpu *vcpu, int size, |
6084 | unsigned short port, const void *val, | |
6085 | unsigned int count) | |
6086 | { | |
6f6fbe98 | 6087 | memcpy(vcpu->arch.pio_data, val, size * count); |
1171903d | 6088 | trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data); |
6f6fbe98 XG |
6089 | return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false); |
6090 | } | |
6091 | ||
2e3bb4d8 SC |
6092 | static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt, |
6093 | int size, unsigned short port, | |
6094 | const void *val, unsigned int count) | |
6095 | { | |
6096 | return emulator_pio_out(emul_to_vcpu(ctxt), size, port, val, count); | |
6097 | } | |
6098 | ||
bbd9b64e CO |
6099 | static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg) |
6100 | { | |
afaf0b2f | 6101 | return kvm_x86_ops.get_segment_base(vcpu, seg); |
bbd9b64e CO |
6102 | } |
6103 | ||
3cb16fe7 | 6104 | static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address) |
bbd9b64e | 6105 | { |
3cb16fe7 | 6106 | kvm_mmu_invlpg(emul_to_vcpu(ctxt), address); |
bbd9b64e CO |
6107 | } |
6108 | ||
ae6a2375 | 6109 | static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu) |
f5f48ee1 SY |
6110 | { |
6111 | if (!need_emulate_wbinvd(vcpu)) | |
6112 | return X86EMUL_CONTINUE; | |
6113 | ||
afaf0b2f | 6114 | if (kvm_x86_ops.has_wbinvd_exit()) { |
2eec7343 JK |
6115 | int cpu = get_cpu(); |
6116 | ||
6117 | cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask); | |
f5f48ee1 SY |
6118 | smp_call_function_many(vcpu->arch.wbinvd_dirty_mask, |
6119 | wbinvd_ipi, NULL, 1); | |
2eec7343 | 6120 | put_cpu(); |
f5f48ee1 | 6121 | cpumask_clear(vcpu->arch.wbinvd_dirty_mask); |
2eec7343 JK |
6122 | } else |
6123 | wbinvd(); | |
f5f48ee1 SY |
6124 | return X86EMUL_CONTINUE; |
6125 | } | |
5cb56059 JS |
6126 | |
6127 | int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu) | |
6128 | { | |
6affcbed KH |
6129 | kvm_emulate_wbinvd_noskip(vcpu); |
6130 | return kvm_skip_emulated_instruction(vcpu); | |
5cb56059 | 6131 | } |
f5f48ee1 SY |
6132 | EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd); |
6133 | ||
5cb56059 JS |
6134 | |
6135 | ||
bcaf5cc5 AK |
6136 | static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt) |
6137 | { | |
5cb56059 | 6138 | kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt)); |
bcaf5cc5 AK |
6139 | } |
6140 | ||
52eb5a6d XL |
6141 | static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr, |
6142 | unsigned long *dest) | |
bbd9b64e | 6143 | { |
16f8a6f9 | 6144 | return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest); |
bbd9b64e CO |
6145 | } |
6146 | ||
52eb5a6d XL |
6147 | static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr, |
6148 | unsigned long value) | |
bbd9b64e | 6149 | { |
338dbc97 | 6150 | |
717746e3 | 6151 | return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value); |
bbd9b64e CO |
6152 | } |
6153 | ||
52a46617 | 6154 | static u64 mk_cr_64(u64 curr_cr, u32 new_val) |
5fdbf976 | 6155 | { |
52a46617 | 6156 | return (curr_cr & ~((1ULL << 32) - 1)) | new_val; |
5fdbf976 MT |
6157 | } |
6158 | ||
717746e3 | 6159 | static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr) |
bbd9b64e | 6160 | { |
717746e3 | 6161 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
52a46617 GN |
6162 | unsigned long value; |
6163 | ||
6164 | switch (cr) { | |
6165 | case 0: | |
6166 | value = kvm_read_cr0(vcpu); | |
6167 | break; | |
6168 | case 2: | |
6169 | value = vcpu->arch.cr2; | |
6170 | break; | |
6171 | case 3: | |
9f8fe504 | 6172 | value = kvm_read_cr3(vcpu); |
52a46617 GN |
6173 | break; |
6174 | case 4: | |
6175 | value = kvm_read_cr4(vcpu); | |
6176 | break; | |
6177 | case 8: | |
6178 | value = kvm_get_cr8(vcpu); | |
6179 | break; | |
6180 | default: | |
a737f256 | 6181 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
52a46617 GN |
6182 | return 0; |
6183 | } | |
6184 | ||
6185 | return value; | |
6186 | } | |
6187 | ||
717746e3 | 6188 | static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val) |
52a46617 | 6189 | { |
717746e3 | 6190 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
0f12244f GN |
6191 | int res = 0; |
6192 | ||
52a46617 GN |
6193 | switch (cr) { |
6194 | case 0: | |
49a9b07e | 6195 | res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val)); |
52a46617 GN |
6196 | break; |
6197 | case 2: | |
6198 | vcpu->arch.cr2 = val; | |
6199 | break; | |
6200 | case 3: | |
2390218b | 6201 | res = kvm_set_cr3(vcpu, val); |
52a46617 GN |
6202 | break; |
6203 | case 4: | |
a83b29c6 | 6204 | res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val)); |
52a46617 GN |
6205 | break; |
6206 | case 8: | |
eea1cff9 | 6207 | res = kvm_set_cr8(vcpu, val); |
52a46617 GN |
6208 | break; |
6209 | default: | |
a737f256 | 6210 | kvm_err("%s: unexpected cr %u\n", __func__, cr); |
0f12244f | 6211 | res = -1; |
52a46617 | 6212 | } |
0f12244f GN |
6213 | |
6214 | return res; | |
52a46617 GN |
6215 | } |
6216 | ||
717746e3 | 6217 | static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt) |
9c537244 | 6218 | { |
afaf0b2f | 6219 | return kvm_x86_ops.get_cpl(emul_to_vcpu(ctxt)); |
9c537244 GN |
6220 | } |
6221 | ||
4bff1e86 | 6222 | static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
2dafc6c2 | 6223 | { |
afaf0b2f | 6224 | kvm_x86_ops.get_gdt(emul_to_vcpu(ctxt), dt); |
2dafc6c2 GN |
6225 | } |
6226 | ||
4bff1e86 | 6227 | static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
160ce1f1 | 6228 | { |
afaf0b2f | 6229 | kvm_x86_ops.get_idt(emul_to_vcpu(ctxt), dt); |
160ce1f1 MG |
6230 | } |
6231 | ||
1ac9d0cf AK |
6232 | static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) |
6233 | { | |
afaf0b2f | 6234 | kvm_x86_ops.set_gdt(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
6235 | } |
6236 | ||
6237 | static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt) | |
6238 | { | |
afaf0b2f | 6239 | kvm_x86_ops.set_idt(emul_to_vcpu(ctxt), dt); |
1ac9d0cf AK |
6240 | } |
6241 | ||
4bff1e86 AK |
6242 | static unsigned long emulator_get_cached_segment_base( |
6243 | struct x86_emulate_ctxt *ctxt, int seg) | |
5951c442 | 6244 | { |
4bff1e86 | 6245 | return get_segment_base(emul_to_vcpu(ctxt), seg); |
5951c442 GN |
6246 | } |
6247 | ||
1aa36616 AK |
6248 | static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector, |
6249 | struct desc_struct *desc, u32 *base3, | |
6250 | int seg) | |
2dafc6c2 GN |
6251 | { |
6252 | struct kvm_segment var; | |
6253 | ||
4bff1e86 | 6254 | kvm_get_segment(emul_to_vcpu(ctxt), &var, seg); |
1aa36616 | 6255 | *selector = var.selector; |
2dafc6c2 | 6256 | |
378a8b09 GN |
6257 | if (var.unusable) { |
6258 | memset(desc, 0, sizeof(*desc)); | |
f0367ee1 RK |
6259 | if (base3) |
6260 | *base3 = 0; | |
2dafc6c2 | 6261 | return false; |
378a8b09 | 6262 | } |
2dafc6c2 GN |
6263 | |
6264 | if (var.g) | |
6265 | var.limit >>= 12; | |
6266 | set_desc_limit(desc, var.limit); | |
6267 | set_desc_base(desc, (unsigned long)var.base); | |
5601d05b GN |
6268 | #ifdef CONFIG_X86_64 |
6269 | if (base3) | |
6270 | *base3 = var.base >> 32; | |
6271 | #endif | |
2dafc6c2 GN |
6272 | desc->type = var.type; |
6273 | desc->s = var.s; | |
6274 | desc->dpl = var.dpl; | |
6275 | desc->p = var.present; | |
6276 | desc->avl = var.avl; | |
6277 | desc->l = var.l; | |
6278 | desc->d = var.db; | |
6279 | desc->g = var.g; | |
6280 | ||
6281 | return true; | |
6282 | } | |
6283 | ||
1aa36616 AK |
6284 | static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector, |
6285 | struct desc_struct *desc, u32 base3, | |
6286 | int seg) | |
2dafc6c2 | 6287 | { |
4bff1e86 | 6288 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
2dafc6c2 GN |
6289 | struct kvm_segment var; |
6290 | ||
1aa36616 | 6291 | var.selector = selector; |
2dafc6c2 | 6292 | var.base = get_desc_base(desc); |
5601d05b GN |
6293 | #ifdef CONFIG_X86_64 |
6294 | var.base |= ((u64)base3) << 32; | |
6295 | #endif | |
2dafc6c2 GN |
6296 | var.limit = get_desc_limit(desc); |
6297 | if (desc->g) | |
6298 | var.limit = (var.limit << 12) | 0xfff; | |
6299 | var.type = desc->type; | |
2dafc6c2 GN |
6300 | var.dpl = desc->dpl; |
6301 | var.db = desc->d; | |
6302 | var.s = desc->s; | |
6303 | var.l = desc->l; | |
6304 | var.g = desc->g; | |
6305 | var.avl = desc->avl; | |
6306 | var.present = desc->p; | |
6307 | var.unusable = !var.present; | |
6308 | var.padding = 0; | |
6309 | ||
6310 | kvm_set_segment(vcpu, &var, seg); | |
6311 | return; | |
6312 | } | |
6313 | ||
717746e3 AK |
6314 | static int emulator_get_msr(struct x86_emulate_ctxt *ctxt, |
6315 | u32 msr_index, u64 *pdata) | |
6316 | { | |
f20935d8 | 6317 | return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata); |
717746e3 AK |
6318 | } |
6319 | ||
6320 | static int emulator_set_msr(struct x86_emulate_ctxt *ctxt, | |
6321 | u32 msr_index, u64 data) | |
6322 | { | |
f20935d8 | 6323 | return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data); |
717746e3 AK |
6324 | } |
6325 | ||
64d60670 PB |
6326 | static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt) |
6327 | { | |
6328 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6329 | ||
6330 | return vcpu->arch.smbase; | |
6331 | } | |
6332 | ||
6333 | static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase) | |
6334 | { | |
6335 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
6336 | ||
6337 | vcpu->arch.smbase = smbase; | |
6338 | } | |
6339 | ||
67f4d428 NA |
6340 | static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt, |
6341 | u32 pmc) | |
6342 | { | |
98ff80f5 | 6343 | return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc); |
67f4d428 NA |
6344 | } |
6345 | ||
222d21aa AK |
6346 | static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt, |
6347 | u32 pmc, u64 *pdata) | |
6348 | { | |
c6702c9d | 6349 | return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata); |
222d21aa AK |
6350 | } |
6351 | ||
6c3287f7 AK |
6352 | static void emulator_halt(struct x86_emulate_ctxt *ctxt) |
6353 | { | |
6354 | emul_to_vcpu(ctxt)->arch.halt_request = 1; | |
6355 | } | |
6356 | ||
2953538e | 6357 | static int emulator_intercept(struct x86_emulate_ctxt *ctxt, |
8a76d7f2 | 6358 | struct x86_instruction_info *info, |
c4f035c6 AK |
6359 | enum x86_intercept_stage stage) |
6360 | { | |
afaf0b2f | 6361 | return kvm_x86_ops.check_intercept(emul_to_vcpu(ctxt), info, stage, |
21f1b8f2 | 6362 | &ctxt->exception); |
c4f035c6 AK |
6363 | } |
6364 | ||
e911eb3b | 6365 | static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt, |
f91af517 SC |
6366 | u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, |
6367 | bool exact_only) | |
bdb42f5a | 6368 | { |
f91af517 | 6369 | return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, exact_only); |
bdb42f5a SB |
6370 | } |
6371 | ||
5ae78e95 SC |
6372 | static bool emulator_guest_has_long_mode(struct x86_emulate_ctxt *ctxt) |
6373 | { | |
6374 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_LM); | |
6375 | } | |
6376 | ||
6377 | static bool emulator_guest_has_movbe(struct x86_emulate_ctxt *ctxt) | |
6378 | { | |
6379 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_MOVBE); | |
6380 | } | |
6381 | ||
6382 | static bool emulator_guest_has_fxsr(struct x86_emulate_ctxt *ctxt) | |
6383 | { | |
6384 | return guest_cpuid_has(emul_to_vcpu(ctxt), X86_FEATURE_FXSR); | |
6385 | } | |
6386 | ||
dd856efa AK |
6387 | static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg) |
6388 | { | |
6389 | return kvm_register_read(emul_to_vcpu(ctxt), reg); | |
6390 | } | |
6391 | ||
6392 | static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val) | |
6393 | { | |
6394 | kvm_register_write(emul_to_vcpu(ctxt), reg, val); | |
6395 | } | |
6396 | ||
801806d9 NA |
6397 | static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked) |
6398 | { | |
afaf0b2f | 6399 | kvm_x86_ops.set_nmi_mask(emul_to_vcpu(ctxt), masked); |
801806d9 NA |
6400 | } |
6401 | ||
6ed071f0 LP |
6402 | static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt) |
6403 | { | |
6404 | return emul_to_vcpu(ctxt)->arch.hflags; | |
6405 | } | |
6406 | ||
6407 | static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags) | |
6408 | { | |
c5833c7a | 6409 | emul_to_vcpu(ctxt)->arch.hflags = emul_flags; |
6ed071f0 LP |
6410 | } |
6411 | ||
ed19321f SC |
6412 | static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt, |
6413 | const char *smstate) | |
0234bf88 | 6414 | { |
afaf0b2f | 6415 | return kvm_x86_ops.pre_leave_smm(emul_to_vcpu(ctxt), smstate); |
0234bf88 LP |
6416 | } |
6417 | ||
c5833c7a SC |
6418 | static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt) |
6419 | { | |
6420 | kvm_smm_changed(emul_to_vcpu(ctxt)); | |
6421 | } | |
6422 | ||
02d4160f VK |
6423 | static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr) |
6424 | { | |
6425 | return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr); | |
6426 | } | |
6427 | ||
0225fb50 | 6428 | static const struct x86_emulate_ops emulate_ops = { |
dd856efa AK |
6429 | .read_gpr = emulator_read_gpr, |
6430 | .write_gpr = emulator_write_gpr, | |
ce14e868 PB |
6431 | .read_std = emulator_read_std, |
6432 | .write_std = emulator_write_std, | |
7a036a6f | 6433 | .read_phys = kvm_read_guest_phys_system, |
1871c602 | 6434 | .fetch = kvm_fetch_guest_virt, |
bbd9b64e CO |
6435 | .read_emulated = emulator_read_emulated, |
6436 | .write_emulated = emulator_write_emulated, | |
6437 | .cmpxchg_emulated = emulator_cmpxchg_emulated, | |
3cb16fe7 | 6438 | .invlpg = emulator_invlpg, |
cf8f70bf GN |
6439 | .pio_in_emulated = emulator_pio_in_emulated, |
6440 | .pio_out_emulated = emulator_pio_out_emulated, | |
1aa36616 AK |
6441 | .get_segment = emulator_get_segment, |
6442 | .set_segment = emulator_set_segment, | |
5951c442 | 6443 | .get_cached_segment_base = emulator_get_cached_segment_base, |
2dafc6c2 | 6444 | .get_gdt = emulator_get_gdt, |
160ce1f1 | 6445 | .get_idt = emulator_get_idt, |
1ac9d0cf AK |
6446 | .set_gdt = emulator_set_gdt, |
6447 | .set_idt = emulator_set_idt, | |
52a46617 GN |
6448 | .get_cr = emulator_get_cr, |
6449 | .set_cr = emulator_set_cr, | |
9c537244 | 6450 | .cpl = emulator_get_cpl, |
35aa5375 GN |
6451 | .get_dr = emulator_get_dr, |
6452 | .set_dr = emulator_set_dr, | |
64d60670 PB |
6453 | .get_smbase = emulator_get_smbase, |
6454 | .set_smbase = emulator_set_smbase, | |
717746e3 AK |
6455 | .set_msr = emulator_set_msr, |
6456 | .get_msr = emulator_get_msr, | |
67f4d428 | 6457 | .check_pmc = emulator_check_pmc, |
222d21aa | 6458 | .read_pmc = emulator_read_pmc, |
6c3287f7 | 6459 | .halt = emulator_halt, |
bcaf5cc5 | 6460 | .wbinvd = emulator_wbinvd, |
d6aa1000 | 6461 | .fix_hypercall = emulator_fix_hypercall, |
c4f035c6 | 6462 | .intercept = emulator_intercept, |
bdb42f5a | 6463 | .get_cpuid = emulator_get_cpuid, |
5ae78e95 SC |
6464 | .guest_has_long_mode = emulator_guest_has_long_mode, |
6465 | .guest_has_movbe = emulator_guest_has_movbe, | |
6466 | .guest_has_fxsr = emulator_guest_has_fxsr, | |
801806d9 | 6467 | .set_nmi_mask = emulator_set_nmi_mask, |
6ed071f0 LP |
6468 | .get_hflags = emulator_get_hflags, |
6469 | .set_hflags = emulator_set_hflags, | |
0234bf88 | 6470 | .pre_leave_smm = emulator_pre_leave_smm, |
c5833c7a | 6471 | .post_leave_smm = emulator_post_leave_smm, |
02d4160f | 6472 | .set_xcr = emulator_set_xcr, |
bbd9b64e CO |
6473 | }; |
6474 | ||
95cb2295 GN |
6475 | static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask) |
6476 | { | |
afaf0b2f | 6477 | u32 int_shadow = kvm_x86_ops.get_interrupt_shadow(vcpu); |
95cb2295 GN |
6478 | /* |
6479 | * an sti; sti; sequence only disable interrupts for the first | |
6480 | * instruction. So, if the last instruction, be it emulated or | |
6481 | * not, left the system with the INT_STI flag enabled, it | |
6482 | * means that the last instruction is an sti. We should not | |
6483 | * leave the flag on in this case. The same goes for mov ss | |
6484 | */ | |
37ccdcbe PB |
6485 | if (int_shadow & mask) |
6486 | mask = 0; | |
6addfc42 | 6487 | if (unlikely(int_shadow || mask)) { |
afaf0b2f | 6488 | kvm_x86_ops.set_interrupt_shadow(vcpu, mask); |
6addfc42 PB |
6489 | if (!mask) |
6490 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
6491 | } | |
95cb2295 GN |
6492 | } |
6493 | ||
ef54bcfe | 6494 | static bool inject_emulated_exception(struct kvm_vcpu *vcpu) |
54b8486f | 6495 | { |
c9b8b07c | 6496 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
da9cb575 | 6497 | if (ctxt->exception.vector == PF_VECTOR) |
53b3d8e9 | 6498 | return kvm_inject_emulated_page_fault(vcpu, &ctxt->exception); |
ef54bcfe PB |
6499 | |
6500 | if (ctxt->exception.error_code_valid) | |
da9cb575 AK |
6501 | kvm_queue_exception_e(vcpu, ctxt->exception.vector, |
6502 | ctxt->exception.error_code); | |
54b8486f | 6503 | else |
da9cb575 | 6504 | kvm_queue_exception(vcpu, ctxt->exception.vector); |
ef54bcfe | 6505 | return false; |
54b8486f GN |
6506 | } |
6507 | ||
c9b8b07c SC |
6508 | static struct x86_emulate_ctxt *alloc_emulate_ctxt(struct kvm_vcpu *vcpu) |
6509 | { | |
6510 | struct x86_emulate_ctxt *ctxt; | |
6511 | ||
6512 | ctxt = kmem_cache_zalloc(x86_emulator_cache, GFP_KERNEL_ACCOUNT); | |
6513 | if (!ctxt) { | |
6514 | pr_err("kvm: failed to allocate vcpu's emulator\n"); | |
6515 | return NULL; | |
6516 | } | |
6517 | ||
6518 | ctxt->vcpu = vcpu; | |
6519 | ctxt->ops = &emulate_ops; | |
6520 | vcpu->arch.emulate_ctxt = ctxt; | |
6521 | ||
6522 | return ctxt; | |
6523 | } | |
6524 | ||
8ec4722d MG |
6525 | static void init_emulate_ctxt(struct kvm_vcpu *vcpu) |
6526 | { | |
c9b8b07c | 6527 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d MG |
6528 | int cs_db, cs_l; |
6529 | ||
afaf0b2f | 6530 | kvm_x86_ops.get_cs_db_l_bits(vcpu, &cs_db, &cs_l); |
8ec4722d | 6531 | |
744e699c | 6532 | ctxt->gpa_available = false; |
adf52235 | 6533 | ctxt->eflags = kvm_get_rflags(vcpu); |
c8401dda PB |
6534 | ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0; |
6535 | ||
adf52235 TY |
6536 | ctxt->eip = kvm_rip_read(vcpu); |
6537 | ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL : | |
6538 | (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 : | |
42bf549f | 6539 | (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 : |
adf52235 TY |
6540 | cs_db ? X86EMUL_MODE_PROT32 : |
6541 | X86EMUL_MODE_PROT16; | |
a584539b | 6542 | BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK); |
64d60670 PB |
6543 | BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK); |
6544 | BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK); | |
adf52235 | 6545 | |
dd856efa | 6546 | init_decode_cache(ctxt); |
7ae441ea | 6547 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; |
8ec4722d MG |
6548 | } |
6549 | ||
9497e1f2 | 6550 | void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip) |
63995653 | 6551 | { |
c9b8b07c | 6552 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
63995653 MG |
6553 | int ret; |
6554 | ||
6555 | init_emulate_ctxt(vcpu); | |
6556 | ||
9dac77fa AK |
6557 | ctxt->op_bytes = 2; |
6558 | ctxt->ad_bytes = 2; | |
6559 | ctxt->_eip = ctxt->eip + inc_eip; | |
9d74191a | 6560 | ret = emulate_int_real(ctxt, irq); |
63995653 | 6561 | |
9497e1f2 SC |
6562 | if (ret != X86EMUL_CONTINUE) { |
6563 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu); | |
6564 | } else { | |
6565 | ctxt->eip = ctxt->_eip; | |
6566 | kvm_rip_write(vcpu, ctxt->eip); | |
6567 | kvm_set_rflags(vcpu, ctxt->eflags); | |
6568 | } | |
63995653 MG |
6569 | } |
6570 | EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt); | |
6571 | ||
e2366171 | 6572 | static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type) |
6d77dbfc | 6573 | { |
6d77dbfc GN |
6574 | ++vcpu->stat.insn_emulation_fail; |
6575 | trace_kvm_emulate_insn_failed(vcpu); | |
e2366171 | 6576 | |
42cbf068 SC |
6577 | if (emulation_type & EMULTYPE_VMWARE_GP) { |
6578 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 6579 | return 1; |
42cbf068 | 6580 | } |
e2366171 | 6581 | |
738fece4 SC |
6582 | if (emulation_type & EMULTYPE_SKIP) { |
6583 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
6584 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6585 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 6586 | return 0; |
738fece4 SC |
6587 | } |
6588 | ||
22da61c9 SC |
6589 | kvm_queue_exception(vcpu, UD_VECTOR); |
6590 | ||
afaf0b2f | 6591 | if (!is_guest_mode(vcpu) && kvm_x86_ops.get_cpl(vcpu) == 0) { |
fc3a9157 JR |
6592 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
6593 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
6594 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 6595 | return 0; |
fc3a9157 | 6596 | } |
e2366171 | 6597 | |
60fc3d02 | 6598 | return 1; |
6d77dbfc GN |
6599 | } |
6600 | ||
736c291c | 6601 | static bool reexecute_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
991eebf9 GN |
6602 | bool write_fault_to_shadow_pgtable, |
6603 | int emulation_type) | |
a6f177ef | 6604 | { |
736c291c | 6605 | gpa_t gpa = cr2_or_gpa; |
ba049e93 | 6606 | kvm_pfn_t pfn; |
a6f177ef | 6607 | |
92daa48b | 6608 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
991eebf9 GN |
6609 | return false; |
6610 | ||
92daa48b SC |
6611 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
6612 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
6613 | return false; |
6614 | ||
44dd3ffa | 6615 | if (!vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
6616 | /* |
6617 | * Write permission should be allowed since only | |
6618 | * write access need to be emulated. | |
6619 | */ | |
736c291c | 6620 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
a6f177ef | 6621 | |
95b3cf69 XG |
6622 | /* |
6623 | * If the mapping is invalid in guest, let cpu retry | |
6624 | * it to generate fault. | |
6625 | */ | |
6626 | if (gpa == UNMAPPED_GVA) | |
6627 | return true; | |
6628 | } | |
a6f177ef | 6629 | |
8e3d9d06 XG |
6630 | /* |
6631 | * Do not retry the unhandleable instruction if it faults on the | |
6632 | * readonly host memory, otherwise it will goto a infinite loop: | |
6633 | * retry instruction -> write #PF -> emulation fail -> retry | |
6634 | * instruction -> ... | |
6635 | */ | |
6636 | pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa)); | |
95b3cf69 XG |
6637 | |
6638 | /* | |
6639 | * If the instruction failed on the error pfn, it can not be fixed, | |
6640 | * report the error to userspace. | |
6641 | */ | |
6642 | if (is_error_noslot_pfn(pfn)) | |
6643 | return false; | |
6644 | ||
6645 | kvm_release_pfn_clean(pfn); | |
6646 | ||
6647 | /* The instructions are well-emulated on direct mmu. */ | |
44dd3ffa | 6648 | if (vcpu->arch.mmu->direct_map) { |
95b3cf69 XG |
6649 | unsigned int indirect_shadow_pages; |
6650 | ||
6651 | spin_lock(&vcpu->kvm->mmu_lock); | |
6652 | indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages; | |
6653 | spin_unlock(&vcpu->kvm->mmu_lock); | |
6654 | ||
6655 | if (indirect_shadow_pages) | |
6656 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
6657 | ||
a6f177ef | 6658 | return true; |
8e3d9d06 | 6659 | } |
a6f177ef | 6660 | |
95b3cf69 XG |
6661 | /* |
6662 | * if emulation was due to access to shadowed page table | |
6663 | * and it failed try to unshadow page and re-enter the | |
6664 | * guest to let CPU execute the instruction. | |
6665 | */ | |
6666 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); | |
93c05d3e XG |
6667 | |
6668 | /* | |
6669 | * If the access faults on its page table, it can not | |
6670 | * be fixed by unprotecting shadow page and it should | |
6671 | * be reported to userspace. | |
6672 | */ | |
6673 | return !write_fault_to_shadow_pgtable; | |
a6f177ef GN |
6674 | } |
6675 | ||
1cb3f3ae | 6676 | static bool retry_instruction(struct x86_emulate_ctxt *ctxt, |
736c291c | 6677 | gpa_t cr2_or_gpa, int emulation_type) |
1cb3f3ae XG |
6678 | { |
6679 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); | |
736c291c | 6680 | unsigned long last_retry_eip, last_retry_addr, gpa = cr2_or_gpa; |
1cb3f3ae XG |
6681 | |
6682 | last_retry_eip = vcpu->arch.last_retry_eip; | |
6683 | last_retry_addr = vcpu->arch.last_retry_addr; | |
6684 | ||
6685 | /* | |
6686 | * If the emulation is caused by #PF and it is non-page_table | |
6687 | * writing instruction, it means the VM-EXIT is caused by shadow | |
6688 | * page protected, we can zap the shadow page and retry this | |
6689 | * instruction directly. | |
6690 | * | |
6691 | * Note: if the guest uses a non-page-table modifying instruction | |
6692 | * on the PDE that points to the instruction, then we will unmap | |
6693 | * the instruction and go to an infinite loop. So, we cache the | |
6694 | * last retried eip and the last fault address, if we meet the eip | |
6695 | * and the address again, we can break out of the potential infinite | |
6696 | * loop. | |
6697 | */ | |
6698 | vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0; | |
6699 | ||
92daa48b | 6700 | if (!(emulation_type & EMULTYPE_ALLOW_RETRY_PF)) |
1cb3f3ae XG |
6701 | return false; |
6702 | ||
92daa48b SC |
6703 | if (WARN_ON_ONCE(is_guest_mode(vcpu)) || |
6704 | WARN_ON_ONCE(!(emulation_type & EMULTYPE_PF))) | |
6c3dfeb6 SC |
6705 | return false; |
6706 | ||
1cb3f3ae XG |
6707 | if (x86_page_table_writing_insn(ctxt)) |
6708 | return false; | |
6709 | ||
736c291c | 6710 | if (ctxt->eip == last_retry_eip && last_retry_addr == cr2_or_gpa) |
1cb3f3ae XG |
6711 | return false; |
6712 | ||
6713 | vcpu->arch.last_retry_eip = ctxt->eip; | |
736c291c | 6714 | vcpu->arch.last_retry_addr = cr2_or_gpa; |
1cb3f3ae | 6715 | |
44dd3ffa | 6716 | if (!vcpu->arch.mmu->direct_map) |
736c291c | 6717 | gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2_or_gpa, NULL); |
1cb3f3ae | 6718 | |
22368028 | 6719 | kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa)); |
1cb3f3ae XG |
6720 | |
6721 | return true; | |
6722 | } | |
6723 | ||
716d51ab GN |
6724 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu); |
6725 | static int complete_emulated_pio(struct kvm_vcpu *vcpu); | |
6726 | ||
64d60670 | 6727 | static void kvm_smm_changed(struct kvm_vcpu *vcpu) |
a584539b | 6728 | { |
64d60670 | 6729 | if (!(vcpu->arch.hflags & HF_SMM_MASK)) { |
660a5d51 PB |
6730 | /* This is a good place to trace that we are exiting SMM. */ |
6731 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false); | |
6732 | ||
c43203ca PB |
6733 | /* Process a latched INIT or SMI, if any. */ |
6734 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
64d60670 | 6735 | } |
699023e2 PB |
6736 | |
6737 | kvm_mmu_reset_context(vcpu); | |
64d60670 PB |
6738 | } |
6739 | ||
4a1e10d5 PB |
6740 | static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7, |
6741 | unsigned long *db) | |
6742 | { | |
6743 | u32 dr6 = 0; | |
6744 | int i; | |
6745 | u32 enable, rwlen; | |
6746 | ||
6747 | enable = dr7; | |
6748 | rwlen = dr7 >> 16; | |
6749 | for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4) | |
6750 | if ((enable & 3) && (rwlen & 15) == type && db[i] == addr) | |
6751 | dr6 |= (1 << i); | |
6752 | return dr6; | |
6753 | } | |
6754 | ||
120c2c4f | 6755 | static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu) |
663f4c61 PB |
6756 | { |
6757 | struct kvm_run *kvm_run = vcpu->run; | |
6758 | ||
c8401dda PB |
6759 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
6760 | kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM; | |
d5d260c5 | 6761 | kvm_run->debug.arch.pc = kvm_get_linear_rip(vcpu); |
c8401dda PB |
6762 | kvm_run->debug.arch.exception = DB_VECTOR; |
6763 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 6764 | return 0; |
663f4c61 | 6765 | } |
120c2c4f | 6766 | kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS); |
60fc3d02 | 6767 | return 1; |
663f4c61 PB |
6768 | } |
6769 | ||
6affcbed KH |
6770 | int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu) |
6771 | { | |
afaf0b2f | 6772 | unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); |
f8ea7c60 | 6773 | int r; |
6affcbed | 6774 | |
afaf0b2f | 6775 | r = kvm_x86_ops.skip_emulated_instruction(vcpu); |
60fc3d02 | 6776 | if (unlikely(!r)) |
f8ea7c60 | 6777 | return 0; |
c8401dda PB |
6778 | |
6779 | /* | |
6780 | * rflags is the old, "raw" value of the flags. The new value has | |
6781 | * not been saved yet. | |
6782 | * | |
6783 | * This is correct even for TF set by the guest, because "the | |
6784 | * processor will not generate this exception after the instruction | |
6785 | * that sets the TF flag". | |
6786 | */ | |
6787 | if (unlikely(rflags & X86_EFLAGS_TF)) | |
120c2c4f | 6788 | r = kvm_vcpu_do_singlestep(vcpu); |
60fc3d02 | 6789 | return r; |
6affcbed KH |
6790 | } |
6791 | EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction); | |
6792 | ||
4a1e10d5 PB |
6793 | static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r) |
6794 | { | |
4a1e10d5 PB |
6795 | if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) && |
6796 | (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) { | |
82b32774 NA |
6797 | struct kvm_run *kvm_run = vcpu->run; |
6798 | unsigned long eip = kvm_get_linear_rip(vcpu); | |
6799 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
6800 | vcpu->arch.guest_debug_dr7, |
6801 | vcpu->arch.eff_db); | |
6802 | ||
6803 | if (dr6 != 0) { | |
6f43ed01 | 6804 | kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM; |
82b32774 | 6805 | kvm_run->debug.arch.pc = eip; |
4a1e10d5 PB |
6806 | kvm_run->debug.arch.exception = DB_VECTOR; |
6807 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
60fc3d02 | 6808 | *r = 0; |
4a1e10d5 PB |
6809 | return true; |
6810 | } | |
6811 | } | |
6812 | ||
4161a569 NA |
6813 | if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) && |
6814 | !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) { | |
82b32774 NA |
6815 | unsigned long eip = kvm_get_linear_rip(vcpu); |
6816 | u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0, | |
4a1e10d5 PB |
6817 | vcpu->arch.dr7, |
6818 | vcpu->arch.db); | |
6819 | ||
6820 | if (dr6 != 0) { | |
4d5523cf | 6821 | kvm_queue_exception_p(vcpu, DB_VECTOR, dr6); |
60fc3d02 | 6822 | *r = 1; |
4a1e10d5 PB |
6823 | return true; |
6824 | } | |
6825 | } | |
6826 | ||
6827 | return false; | |
6828 | } | |
6829 | ||
04789b66 LA |
6830 | static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt) |
6831 | { | |
2d7921c4 AM |
6832 | switch (ctxt->opcode_len) { |
6833 | case 1: | |
6834 | switch (ctxt->b) { | |
6835 | case 0xe4: /* IN */ | |
6836 | case 0xe5: | |
6837 | case 0xec: | |
6838 | case 0xed: | |
6839 | case 0xe6: /* OUT */ | |
6840 | case 0xe7: | |
6841 | case 0xee: | |
6842 | case 0xef: | |
6843 | case 0x6c: /* INS */ | |
6844 | case 0x6d: | |
6845 | case 0x6e: /* OUTS */ | |
6846 | case 0x6f: | |
6847 | return true; | |
6848 | } | |
6849 | break; | |
6850 | case 2: | |
6851 | switch (ctxt->b) { | |
6852 | case 0x33: /* RDPMC */ | |
6853 | return true; | |
6854 | } | |
6855 | break; | |
04789b66 LA |
6856 | } |
6857 | ||
6858 | return false; | |
6859 | } | |
6860 | ||
736c291c SC |
6861 | int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, |
6862 | int emulation_type, void *insn, int insn_len) | |
bbd9b64e | 6863 | { |
95cb2295 | 6864 | int r; |
c9b8b07c | 6865 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
7ae441ea | 6866 | bool writeback = true; |
93c05d3e | 6867 | bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable; |
bbd9b64e | 6868 | |
c595ceee PB |
6869 | vcpu->arch.l1tf_flush_l1d = true; |
6870 | ||
93c05d3e XG |
6871 | /* |
6872 | * Clear write_fault_to_shadow_pgtable here to ensure it is | |
6873 | * never reused. | |
6874 | */ | |
6875 | vcpu->arch.write_fault_to_shadow_pgtable = false; | |
26eef70c | 6876 | kvm_clear_exception_queue(vcpu); |
8d7d8102 | 6877 | |
571008da | 6878 | if (!(emulation_type & EMULTYPE_NO_DECODE)) { |
8ec4722d | 6879 | init_emulate_ctxt(vcpu); |
4a1e10d5 PB |
6880 | |
6881 | /* | |
6882 | * We will reenter on the same instruction since | |
6883 | * we do not set complete_userspace_io. This does not | |
6884 | * handle watchpoints yet, those would be handled in | |
6885 | * the emulate_ops. | |
6886 | */ | |
d391f120 VK |
6887 | if (!(emulation_type & EMULTYPE_SKIP) && |
6888 | kvm_vcpu_check_breakpoint(vcpu, &r)) | |
4a1e10d5 PB |
6889 | return r; |
6890 | ||
9d74191a TY |
6891 | ctxt->interruptibility = 0; |
6892 | ctxt->have_exception = false; | |
e0ad0b47 | 6893 | ctxt->exception.vector = -1; |
9d74191a | 6894 | ctxt->perm_ok = false; |
bbd9b64e | 6895 | |
b51e974f | 6896 | ctxt->ud = emulation_type & EMULTYPE_TRAP_UD; |
4005996e | 6897 | |
9d74191a | 6898 | r = x86_decode_insn(ctxt, insn, insn_len); |
bbd9b64e | 6899 | |
e46479f8 | 6900 | trace_kvm_emulate_insn_start(vcpu); |
f2b5756b | 6901 | ++vcpu->stat.insn_emulation; |
1d2887e2 | 6902 | if (r != EMULATION_OK) { |
b4000606 | 6903 | if ((emulation_type & EMULTYPE_TRAP_UD) || |
c83fad65 SC |
6904 | (emulation_type & EMULTYPE_TRAP_UD_FORCED)) { |
6905 | kvm_queue_exception(vcpu, UD_VECTOR); | |
60fc3d02 | 6906 | return 1; |
c83fad65 | 6907 | } |
736c291c SC |
6908 | if (reexecute_instruction(vcpu, cr2_or_gpa, |
6909 | write_fault_to_spt, | |
6910 | emulation_type)) | |
60fc3d02 | 6911 | return 1; |
8530a79c | 6912 | if (ctxt->have_exception) { |
c8848cee JD |
6913 | /* |
6914 | * #UD should result in just EMULATION_FAILED, and trap-like | |
6915 | * exception should not be encountered during decode. | |
6916 | */ | |
6917 | WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR || | |
6918 | exception_type(ctxt->exception.vector) == EXCPT_TRAP); | |
8530a79c | 6919 | inject_emulated_exception(vcpu); |
60fc3d02 | 6920 | return 1; |
8530a79c | 6921 | } |
e2366171 | 6922 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
6923 | } |
6924 | } | |
6925 | ||
42cbf068 SC |
6926 | if ((emulation_type & EMULTYPE_VMWARE_GP) && |
6927 | !is_vmware_backdoor_opcode(ctxt)) { | |
6928 | kvm_queue_exception_e(vcpu, GP_VECTOR, 0); | |
60fc3d02 | 6929 | return 1; |
42cbf068 | 6930 | } |
04789b66 | 6931 | |
1957aa63 SC |
6932 | /* |
6933 | * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks | |
6934 | * for kvm_skip_emulated_instruction(). The caller is responsible for | |
6935 | * updating interruptibility state and injecting single-step #DBs. | |
6936 | */ | |
ba8afb6b | 6937 | if (emulation_type & EMULTYPE_SKIP) { |
9dac77fa | 6938 | kvm_rip_write(vcpu, ctxt->_eip); |
bb663c7a NA |
6939 | if (ctxt->eflags & X86_EFLAGS_RF) |
6940 | kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF); | |
60fc3d02 | 6941 | return 1; |
ba8afb6b GN |
6942 | } |
6943 | ||
736c291c | 6944 | if (retry_instruction(ctxt, cr2_or_gpa, emulation_type)) |
60fc3d02 | 6945 | return 1; |
1cb3f3ae | 6946 | |
7ae441ea | 6947 | /* this is needed for vmware backdoor interface to work since it |
4d2179e1 | 6948 | changes registers values during IO operation */ |
7ae441ea GN |
6949 | if (vcpu->arch.emulate_regs_need_sync_from_vcpu) { |
6950 | vcpu->arch.emulate_regs_need_sync_from_vcpu = false; | |
dd856efa | 6951 | emulator_invalidate_register_cache(ctxt); |
7ae441ea | 6952 | } |
4d2179e1 | 6953 | |
5cd21917 | 6954 | restart: |
92daa48b SC |
6955 | if (emulation_type & EMULTYPE_PF) { |
6956 | /* Save the faulting GPA (cr2) in the address field */ | |
6957 | ctxt->exception.address = cr2_or_gpa; | |
6958 | ||
6959 | /* With shadow page tables, cr2 contains a GVA or nGPA. */ | |
6960 | if (vcpu->arch.mmu->direct_map) { | |
744e699c SC |
6961 | ctxt->gpa_available = true; |
6962 | ctxt->gpa_val = cr2_or_gpa; | |
92daa48b SC |
6963 | } |
6964 | } else { | |
6965 | /* Sanitize the address out of an abundance of paranoia. */ | |
6966 | ctxt->exception.address = 0; | |
6967 | } | |
0f89b207 | 6968 | |
9d74191a | 6969 | r = x86_emulate_insn(ctxt); |
bbd9b64e | 6970 | |
775fde86 | 6971 | if (r == EMULATION_INTERCEPTED) |
60fc3d02 | 6972 | return 1; |
775fde86 | 6973 | |
d2ddd1c4 | 6974 | if (r == EMULATION_FAILED) { |
736c291c | 6975 | if (reexecute_instruction(vcpu, cr2_or_gpa, write_fault_to_spt, |
991eebf9 | 6976 | emulation_type)) |
60fc3d02 | 6977 | return 1; |
c3cd7ffa | 6978 | |
e2366171 | 6979 | return handle_emulation_failure(vcpu, emulation_type); |
bbd9b64e CO |
6980 | } |
6981 | ||
9d74191a | 6982 | if (ctxt->have_exception) { |
60fc3d02 | 6983 | r = 1; |
ef54bcfe PB |
6984 | if (inject_emulated_exception(vcpu)) |
6985 | return r; | |
d2ddd1c4 | 6986 | } else if (vcpu->arch.pio.count) { |
0912c977 PB |
6987 | if (!vcpu->arch.pio.in) { |
6988 | /* FIXME: return into emulator if single-stepping. */ | |
3457e419 | 6989 | vcpu->arch.pio.count = 0; |
0912c977 | 6990 | } else { |
7ae441ea | 6991 | writeback = false; |
716d51ab GN |
6992 | vcpu->arch.complete_userspace_io = complete_emulated_pio; |
6993 | } | |
60fc3d02 | 6994 | r = 0; |
7ae441ea | 6995 | } else if (vcpu->mmio_needed) { |
bc8a0aaf SC |
6996 | ++vcpu->stat.mmio_exits; |
6997 | ||
7ae441ea GN |
6998 | if (!vcpu->mmio_is_write) |
6999 | writeback = false; | |
60fc3d02 | 7000 | r = 0; |
716d51ab | 7001 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; |
7ae441ea | 7002 | } else if (r == EMULATION_RESTART) |
5cd21917 | 7003 | goto restart; |
d2ddd1c4 | 7004 | else |
60fc3d02 | 7005 | r = 1; |
f850e2e6 | 7006 | |
7ae441ea | 7007 | if (writeback) { |
afaf0b2f | 7008 | unsigned long rflags = kvm_x86_ops.get_rflags(vcpu); |
9d74191a | 7009 | toggle_interruptibility(vcpu, ctxt->interruptibility); |
7ae441ea | 7010 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
38827dbd | 7011 | if (!ctxt->have_exception || |
75ee23b3 SC |
7012 | exception_type(ctxt->exception.vector) == EXCPT_TRAP) { |
7013 | kvm_rip_write(vcpu, ctxt->eip); | |
60fc3d02 | 7014 | if (r && ctxt->tf) |
120c2c4f | 7015 | r = kvm_vcpu_do_singlestep(vcpu); |
afaf0b2f SC |
7016 | if (kvm_x86_ops.update_emulated_instruction) |
7017 | kvm_x86_ops.update_emulated_instruction(vcpu); | |
38827dbd | 7018 | __kvm_set_rflags(vcpu, ctxt->eflags); |
75ee23b3 | 7019 | } |
6addfc42 PB |
7020 | |
7021 | /* | |
7022 | * For STI, interrupts are shadowed; so KVM_REQ_EVENT will | |
7023 | * do nothing, and it will be requested again as soon as | |
7024 | * the shadow expires. But we still need to check here, | |
7025 | * because POPF has no interrupt shadow. | |
7026 | */ | |
7027 | if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF)) | |
7028 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7ae441ea GN |
7029 | } else |
7030 | vcpu->arch.emulate_regs_need_sync_to_vcpu = true; | |
e85d28f8 GN |
7031 | |
7032 | return r; | |
de7d789a | 7033 | } |
c60658d1 SC |
7034 | |
7035 | int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type) | |
7036 | { | |
7037 | return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0); | |
7038 | } | |
7039 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction); | |
7040 | ||
7041 | int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu, | |
7042 | void *insn, int insn_len) | |
7043 | { | |
7044 | return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len); | |
7045 | } | |
7046 | EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer); | |
de7d789a | 7047 | |
8764ed55 SC |
7048 | static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu) |
7049 | { | |
7050 | vcpu->arch.pio.count = 0; | |
7051 | return 1; | |
7052 | } | |
7053 | ||
45def77e SC |
7054 | static int complete_fast_pio_out(struct kvm_vcpu *vcpu) |
7055 | { | |
7056 | vcpu->arch.pio.count = 0; | |
7057 | ||
7058 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) | |
7059 | return 1; | |
7060 | ||
7061 | return kvm_skip_emulated_instruction(vcpu); | |
7062 | } | |
7063 | ||
dca7f128 SC |
7064 | static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, |
7065 | unsigned short port) | |
de7d789a | 7066 | { |
de3cd117 | 7067 | unsigned long val = kvm_rax_read(vcpu); |
2e3bb4d8 SC |
7068 | int ret = emulator_pio_out(vcpu, size, port, &val, 1); |
7069 | ||
8764ed55 SC |
7070 | if (ret) |
7071 | return ret; | |
45def77e | 7072 | |
8764ed55 SC |
7073 | /* |
7074 | * Workaround userspace that relies on old KVM behavior of %rip being | |
7075 | * incremented prior to exiting to userspace to handle "OUT 0x7e". | |
7076 | */ | |
7077 | if (port == 0x7e && | |
7078 | kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) { | |
7079 | vcpu->arch.complete_userspace_io = | |
7080 | complete_fast_pio_out_port_0x7e; | |
7081 | kvm_skip_emulated_instruction(vcpu); | |
7082 | } else { | |
45def77e SC |
7083 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
7084 | vcpu->arch.complete_userspace_io = complete_fast_pio_out; | |
7085 | } | |
8764ed55 | 7086 | return 0; |
de7d789a | 7087 | } |
de7d789a | 7088 | |
8370c3d0 TL |
7089 | static int complete_fast_pio_in(struct kvm_vcpu *vcpu) |
7090 | { | |
7091 | unsigned long val; | |
7092 | ||
7093 | /* We should only ever be called with arch.pio.count equal to 1 */ | |
7094 | BUG_ON(vcpu->arch.pio.count != 1); | |
7095 | ||
45def77e SC |
7096 | if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) { |
7097 | vcpu->arch.pio.count = 0; | |
7098 | return 1; | |
7099 | } | |
7100 | ||
8370c3d0 | 7101 | /* For size less than 4 we merge, else we zero extend */ |
de3cd117 | 7102 | val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 TL |
7103 | |
7104 | /* | |
2e3bb4d8 | 7105 | * Since vcpu->arch.pio.count == 1 let emulator_pio_in perform |
8370c3d0 TL |
7106 | * the copy and tracing |
7107 | */ | |
2e3bb4d8 | 7108 | emulator_pio_in(vcpu, vcpu->arch.pio.size, vcpu->arch.pio.port, &val, 1); |
de3cd117 | 7109 | kvm_rax_write(vcpu, val); |
8370c3d0 | 7110 | |
45def77e | 7111 | return kvm_skip_emulated_instruction(vcpu); |
8370c3d0 TL |
7112 | } |
7113 | ||
dca7f128 SC |
7114 | static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size, |
7115 | unsigned short port) | |
8370c3d0 TL |
7116 | { |
7117 | unsigned long val; | |
7118 | int ret; | |
7119 | ||
7120 | /* For size less than 4 we merge, else we zero extend */ | |
de3cd117 | 7121 | val = (size < 4) ? kvm_rax_read(vcpu) : 0; |
8370c3d0 | 7122 | |
2e3bb4d8 | 7123 | ret = emulator_pio_in(vcpu, size, port, &val, 1); |
8370c3d0 | 7124 | if (ret) { |
de3cd117 | 7125 | kvm_rax_write(vcpu, val); |
8370c3d0 TL |
7126 | return ret; |
7127 | } | |
7128 | ||
45def77e | 7129 | vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu); |
8370c3d0 TL |
7130 | vcpu->arch.complete_userspace_io = complete_fast_pio_in; |
7131 | ||
7132 | return 0; | |
7133 | } | |
dca7f128 SC |
7134 | |
7135 | int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in) | |
7136 | { | |
45def77e | 7137 | int ret; |
dca7f128 | 7138 | |
dca7f128 | 7139 | if (in) |
45def77e | 7140 | ret = kvm_fast_pio_in(vcpu, size, port); |
dca7f128 | 7141 | else |
45def77e SC |
7142 | ret = kvm_fast_pio_out(vcpu, size, port); |
7143 | return ret && kvm_skip_emulated_instruction(vcpu); | |
dca7f128 SC |
7144 | } |
7145 | EXPORT_SYMBOL_GPL(kvm_fast_pio); | |
8370c3d0 | 7146 | |
251a5fd6 | 7147 | static int kvmclock_cpu_down_prep(unsigned int cpu) |
8cfdc000 | 7148 | { |
0a3aee0d | 7149 | __this_cpu_write(cpu_tsc_khz, 0); |
251a5fd6 | 7150 | return 0; |
8cfdc000 ZA |
7151 | } |
7152 | ||
7153 | static void tsc_khz_changed(void *data) | |
c8076604 | 7154 | { |
8cfdc000 ZA |
7155 | struct cpufreq_freqs *freq = data; |
7156 | unsigned long khz = 0; | |
7157 | ||
7158 | if (data) | |
7159 | khz = freq->new; | |
7160 | else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) | |
7161 | khz = cpufreq_quick_get(raw_smp_processor_id()); | |
7162 | if (!khz) | |
7163 | khz = tsc_khz; | |
0a3aee0d | 7164 | __this_cpu_write(cpu_tsc_khz, khz); |
c8076604 GH |
7165 | } |
7166 | ||
5fa4ec9c | 7167 | #ifdef CONFIG_X86_64 |
0092e434 VK |
7168 | static void kvm_hyperv_tsc_notifier(void) |
7169 | { | |
0092e434 VK |
7170 | struct kvm *kvm; |
7171 | struct kvm_vcpu *vcpu; | |
7172 | int cpu; | |
7173 | ||
0d9ce162 | 7174 | mutex_lock(&kvm_lock); |
0092e434 VK |
7175 | list_for_each_entry(kvm, &vm_list, vm_list) |
7176 | kvm_make_mclock_inprogress_request(kvm); | |
7177 | ||
7178 | hyperv_stop_tsc_emulation(); | |
7179 | ||
7180 | /* TSC frequency always matches when on Hyper-V */ | |
7181 | for_each_present_cpu(cpu) | |
7182 | per_cpu(cpu_tsc_khz, cpu) = tsc_khz; | |
7183 | kvm_max_guest_tsc_khz = tsc_khz; | |
7184 | ||
7185 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
7186 | struct kvm_arch *ka = &kvm->arch; | |
7187 | ||
7188 | spin_lock(&ka->pvclock_gtod_sync_lock); | |
7189 | ||
7190 | pvclock_update_vm_gtod_copy(kvm); | |
7191 | ||
7192 | kvm_for_each_vcpu(cpu, vcpu, kvm) | |
7193 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
7194 | ||
7195 | kvm_for_each_vcpu(cpu, vcpu, kvm) | |
7196 | kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu); | |
7197 | ||
7198 | spin_unlock(&ka->pvclock_gtod_sync_lock); | |
7199 | } | |
0d9ce162 | 7200 | mutex_unlock(&kvm_lock); |
0092e434 | 7201 | } |
5fa4ec9c | 7202 | #endif |
0092e434 | 7203 | |
df24014a | 7204 | static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu) |
c8076604 | 7205 | { |
c8076604 GH |
7206 | struct kvm *kvm; |
7207 | struct kvm_vcpu *vcpu; | |
7208 | int i, send_ipi = 0; | |
7209 | ||
8cfdc000 ZA |
7210 | /* |
7211 | * We allow guests to temporarily run on slowing clocks, | |
7212 | * provided we notify them after, or to run on accelerating | |
7213 | * clocks, provided we notify them before. Thus time never | |
7214 | * goes backwards. | |
7215 | * | |
7216 | * However, we have a problem. We can't atomically update | |
7217 | * the frequency of a given CPU from this function; it is | |
7218 | * merely a notifier, which can be called from any CPU. | |
7219 | * Changing the TSC frequency at arbitrary points in time | |
7220 | * requires a recomputation of local variables related to | |
7221 | * the TSC for each VCPU. We must flag these local variables | |
7222 | * to be updated and be sure the update takes place with the | |
7223 | * new frequency before any guests proceed. | |
7224 | * | |
7225 | * Unfortunately, the combination of hotplug CPU and frequency | |
7226 | * change creates an intractable locking scenario; the order | |
7227 | * of when these callouts happen is undefined with respect to | |
7228 | * CPU hotplug, and they can race with each other. As such, | |
7229 | * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is | |
7230 | * undefined; you can actually have a CPU frequency change take | |
7231 | * place in between the computation of X and the setting of the | |
7232 | * variable. To protect against this problem, all updates of | |
7233 | * the per_cpu tsc_khz variable are done in an interrupt | |
7234 | * protected IPI, and all callers wishing to update the value | |
7235 | * must wait for a synchronous IPI to complete (which is trivial | |
7236 | * if the caller is on the CPU already). This establishes the | |
7237 | * necessary total order on variable updates. | |
7238 | * | |
7239 | * Note that because a guest time update may take place | |
7240 | * anytime after the setting of the VCPU's request bit, the | |
7241 | * correct TSC value must be set before the request. However, | |
7242 | * to ensure the update actually makes it to any guest which | |
7243 | * starts running in hardware virtualization between the set | |
7244 | * and the acquisition of the spinlock, we must also ping the | |
7245 | * CPU after setting the request bit. | |
7246 | * | |
7247 | */ | |
7248 | ||
df24014a | 7249 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 7250 | |
0d9ce162 | 7251 | mutex_lock(&kvm_lock); |
c8076604 | 7252 | list_for_each_entry(kvm, &vm_list, vm_list) { |
988a2cae | 7253 | kvm_for_each_vcpu(i, vcpu, kvm) { |
df24014a | 7254 | if (vcpu->cpu != cpu) |
c8076604 | 7255 | continue; |
c285545f | 7256 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0d9ce162 | 7257 | if (vcpu->cpu != raw_smp_processor_id()) |
8cfdc000 | 7258 | send_ipi = 1; |
c8076604 GH |
7259 | } |
7260 | } | |
0d9ce162 | 7261 | mutex_unlock(&kvm_lock); |
c8076604 GH |
7262 | |
7263 | if (freq->old < freq->new && send_ipi) { | |
7264 | /* | |
7265 | * We upscale the frequency. Must make the guest | |
7266 | * doesn't see old kvmclock values while running with | |
7267 | * the new frequency, otherwise we risk the guest sees | |
7268 | * time go backwards. | |
7269 | * | |
7270 | * In case we update the frequency for another cpu | |
7271 | * (which might be in guest context) send an interrupt | |
7272 | * to kick the cpu out of guest context. Next time | |
7273 | * guest context is entered kvmclock will be updated, | |
7274 | * so the guest will not see stale values. | |
7275 | */ | |
df24014a | 7276 | smp_call_function_single(cpu, tsc_khz_changed, freq, 1); |
c8076604 | 7277 | } |
df24014a VK |
7278 | } |
7279 | ||
7280 | static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val, | |
7281 | void *data) | |
7282 | { | |
7283 | struct cpufreq_freqs *freq = data; | |
7284 | int cpu; | |
7285 | ||
7286 | if (val == CPUFREQ_PRECHANGE && freq->old > freq->new) | |
7287 | return 0; | |
7288 | if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new) | |
7289 | return 0; | |
7290 | ||
7291 | for_each_cpu(cpu, freq->policy->cpus) | |
7292 | __kvmclock_cpufreq_notifier(freq, cpu); | |
7293 | ||
c8076604 GH |
7294 | return 0; |
7295 | } | |
7296 | ||
7297 | static struct notifier_block kvmclock_cpufreq_notifier_block = { | |
8cfdc000 ZA |
7298 | .notifier_call = kvmclock_cpufreq_notifier |
7299 | }; | |
7300 | ||
251a5fd6 | 7301 | static int kvmclock_cpu_online(unsigned int cpu) |
8cfdc000 | 7302 | { |
251a5fd6 SAS |
7303 | tsc_khz_changed(NULL); |
7304 | return 0; | |
8cfdc000 ZA |
7305 | } |
7306 | ||
b820cc0c ZA |
7307 | static void kvm_timer_init(void) |
7308 | { | |
c285545f | 7309 | max_tsc_khz = tsc_khz; |
460dd42e | 7310 | |
b820cc0c | 7311 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) { |
c285545f | 7312 | #ifdef CONFIG_CPU_FREQ |
aaec7c03 | 7313 | struct cpufreq_policy *policy; |
758f588d BP |
7314 | int cpu; |
7315 | ||
3e26f230 | 7316 | cpu = get_cpu(); |
aaec7c03 | 7317 | policy = cpufreq_cpu_get(cpu); |
9a11997e WL |
7318 | if (policy) { |
7319 | if (policy->cpuinfo.max_freq) | |
7320 | max_tsc_khz = policy->cpuinfo.max_freq; | |
7321 | cpufreq_cpu_put(policy); | |
7322 | } | |
3e26f230 | 7323 | put_cpu(); |
c285545f | 7324 | #endif |
b820cc0c ZA |
7325 | cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block, |
7326 | CPUFREQ_TRANSITION_NOTIFIER); | |
7327 | } | |
460dd42e | 7328 | |
73c1b41e | 7329 | cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online", |
251a5fd6 | 7330 | kvmclock_cpu_online, kvmclock_cpu_down_prep); |
b820cc0c ZA |
7331 | } |
7332 | ||
dd60d217 AK |
7333 | DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu); |
7334 | EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu); | |
ff9d07a0 | 7335 | |
f5132b01 | 7336 | int kvm_is_in_guest(void) |
ff9d07a0 | 7337 | { |
086c9855 | 7338 | return __this_cpu_read(current_vcpu) != NULL; |
ff9d07a0 ZY |
7339 | } |
7340 | ||
7341 | static int kvm_is_user_mode(void) | |
7342 | { | |
7343 | int user_mode = 3; | |
dcf46b94 | 7344 | |
086c9855 | 7345 | if (__this_cpu_read(current_vcpu)) |
afaf0b2f | 7346 | user_mode = kvm_x86_ops.get_cpl(__this_cpu_read(current_vcpu)); |
dcf46b94 | 7347 | |
ff9d07a0 ZY |
7348 | return user_mode != 0; |
7349 | } | |
7350 | ||
7351 | static unsigned long kvm_get_guest_ip(void) | |
7352 | { | |
7353 | unsigned long ip = 0; | |
dcf46b94 | 7354 | |
086c9855 AS |
7355 | if (__this_cpu_read(current_vcpu)) |
7356 | ip = kvm_rip_read(__this_cpu_read(current_vcpu)); | |
dcf46b94 | 7357 | |
ff9d07a0 ZY |
7358 | return ip; |
7359 | } | |
7360 | ||
8479e04e LK |
7361 | static void kvm_handle_intel_pt_intr(void) |
7362 | { | |
7363 | struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu); | |
7364 | ||
7365 | kvm_make_request(KVM_REQ_PMI, vcpu); | |
7366 | __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT, | |
7367 | (unsigned long *)&vcpu->arch.pmu.global_status); | |
7368 | } | |
7369 | ||
ff9d07a0 ZY |
7370 | static struct perf_guest_info_callbacks kvm_guest_cbs = { |
7371 | .is_in_guest = kvm_is_in_guest, | |
7372 | .is_user_mode = kvm_is_user_mode, | |
7373 | .get_guest_ip = kvm_get_guest_ip, | |
8479e04e | 7374 | .handle_intel_pt_intr = kvm_handle_intel_pt_intr, |
ff9d07a0 ZY |
7375 | }; |
7376 | ||
16e8d74d MT |
7377 | #ifdef CONFIG_X86_64 |
7378 | static void pvclock_gtod_update_fn(struct work_struct *work) | |
7379 | { | |
d828199e MT |
7380 | struct kvm *kvm; |
7381 | ||
7382 | struct kvm_vcpu *vcpu; | |
7383 | int i; | |
7384 | ||
0d9ce162 | 7385 | mutex_lock(&kvm_lock); |
d828199e MT |
7386 | list_for_each_entry(kvm, &vm_list, vm_list) |
7387 | kvm_for_each_vcpu(i, vcpu, kvm) | |
105b21bb | 7388 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
d828199e | 7389 | atomic_set(&kvm_guest_has_master_clock, 0); |
0d9ce162 | 7390 | mutex_unlock(&kvm_lock); |
16e8d74d MT |
7391 | } |
7392 | ||
7393 | static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn); | |
7394 | ||
7395 | /* | |
7396 | * Notification about pvclock gtod data update. | |
7397 | */ | |
7398 | static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused, | |
7399 | void *priv) | |
7400 | { | |
7401 | struct pvclock_gtod_data *gtod = &pvclock_gtod_data; | |
7402 | struct timekeeper *tk = priv; | |
7403 | ||
7404 | update_pvclock_gtod(tk); | |
7405 | ||
7406 | /* disable master clock if host does not trust, or does not | |
b0c39dc6 | 7407 | * use, TSC based clocksource. |
16e8d74d | 7408 | */ |
b0c39dc6 | 7409 | if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) && |
16e8d74d MT |
7410 | atomic_read(&kvm_guest_has_master_clock) != 0) |
7411 | queue_work(system_long_wq, &pvclock_gtod_work); | |
7412 | ||
7413 | return 0; | |
7414 | } | |
7415 | ||
7416 | static struct notifier_block pvclock_gtod_notifier = { | |
7417 | .notifier_call = pvclock_gtod_notify, | |
7418 | }; | |
7419 | #endif | |
7420 | ||
f8c16bba | 7421 | int kvm_arch_init(void *opaque) |
043405e1 | 7422 | { |
d008dfdb | 7423 | struct kvm_x86_init_ops *ops = opaque; |
b820cc0c | 7424 | int r; |
f8c16bba | 7425 | |
afaf0b2f | 7426 | if (kvm_x86_ops.hardware_enable) { |
f8c16bba | 7427 | printk(KERN_ERR "kvm: already loaded the other module\n"); |
56c6d28a ZX |
7428 | r = -EEXIST; |
7429 | goto out; | |
f8c16bba ZX |
7430 | } |
7431 | ||
7432 | if (!ops->cpu_has_kvm_support()) { | |
ef935c25 | 7433 | pr_err_ratelimited("kvm: no hardware support\n"); |
56c6d28a ZX |
7434 | r = -EOPNOTSUPP; |
7435 | goto out; | |
f8c16bba ZX |
7436 | } |
7437 | if (ops->disabled_by_bios()) { | |
ef935c25 | 7438 | pr_err_ratelimited("kvm: disabled by bios\n"); |
56c6d28a ZX |
7439 | r = -EOPNOTSUPP; |
7440 | goto out; | |
f8c16bba ZX |
7441 | } |
7442 | ||
b666a4b6 MO |
7443 | /* |
7444 | * KVM explicitly assumes that the guest has an FPU and | |
7445 | * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the | |
7446 | * vCPU's FPU state as a fxregs_state struct. | |
7447 | */ | |
7448 | if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) { | |
7449 | printk(KERN_ERR "kvm: inadequate fpu\n"); | |
7450 | r = -EOPNOTSUPP; | |
7451 | goto out; | |
7452 | } | |
7453 | ||
013f6a5d | 7454 | r = -ENOMEM; |
ed8e4812 | 7455 | x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu), |
b666a4b6 MO |
7456 | __alignof__(struct fpu), SLAB_ACCOUNT, |
7457 | NULL); | |
7458 | if (!x86_fpu_cache) { | |
7459 | printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n"); | |
7460 | goto out; | |
7461 | } | |
7462 | ||
c9b8b07c SC |
7463 | x86_emulator_cache = kvm_alloc_emulator_cache(); |
7464 | if (!x86_emulator_cache) { | |
7465 | pr_err("kvm: failed to allocate cache for x86 emulator\n"); | |
7466 | goto out_free_x86_fpu_cache; | |
7467 | } | |
7468 | ||
013f6a5d MT |
7469 | shared_msrs = alloc_percpu(struct kvm_shared_msrs); |
7470 | if (!shared_msrs) { | |
7471 | printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n"); | |
c9b8b07c | 7472 | goto out_free_x86_emulator_cache; |
013f6a5d MT |
7473 | } |
7474 | ||
97db56ce AK |
7475 | r = kvm_mmu_module_init(); |
7476 | if (r) | |
013f6a5d | 7477 | goto out_free_percpu; |
97db56ce | 7478 | |
7b52345e | 7479 | kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK, |
ffb128c8 | 7480 | PT_DIRTY_MASK, PT64_NX_MASK, 0, |
d0ec49d4 | 7481 | PT_PRESENT_MASK, 0, sme_me_mask); |
b820cc0c | 7482 | kvm_timer_init(); |
c8076604 | 7483 | |
ff9d07a0 ZY |
7484 | perf_register_guest_info_callbacks(&kvm_guest_cbs); |
7485 | ||
cfc48181 | 7486 | if (boot_cpu_has(X86_FEATURE_XSAVE)) { |
2acf923e | 7487 | host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK); |
cfc48181 SC |
7488 | supported_xcr0 = host_xcr0 & KVM_SUPPORTED_XCR0; |
7489 | } | |
2acf923e | 7490 | |
c5cc421b | 7491 | kvm_lapic_init(); |
0c5f81da WL |
7492 | if (pi_inject_timer == -1) |
7493 | pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER); | |
16e8d74d MT |
7494 | #ifdef CONFIG_X86_64 |
7495 | pvclock_gtod_register_notifier(&pvclock_gtod_notifier); | |
0092e434 | 7496 | |
5fa4ec9c | 7497 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 | 7498 | set_hv_tscchange_cb(kvm_hyperv_tsc_notifier); |
16e8d74d MT |
7499 | #endif |
7500 | ||
f8c16bba | 7501 | return 0; |
56c6d28a | 7502 | |
013f6a5d MT |
7503 | out_free_percpu: |
7504 | free_percpu(shared_msrs); | |
c9b8b07c SC |
7505 | out_free_x86_emulator_cache: |
7506 | kmem_cache_destroy(x86_emulator_cache); | |
b666a4b6 MO |
7507 | out_free_x86_fpu_cache: |
7508 | kmem_cache_destroy(x86_fpu_cache); | |
56c6d28a | 7509 | out: |
56c6d28a | 7510 | return r; |
043405e1 | 7511 | } |
8776e519 | 7512 | |
f8c16bba ZX |
7513 | void kvm_arch_exit(void) |
7514 | { | |
0092e434 | 7515 | #ifdef CONFIG_X86_64 |
5fa4ec9c | 7516 | if (hypervisor_is_type(X86_HYPER_MS_HYPERV)) |
0092e434 VK |
7517 | clear_hv_tscchange_cb(); |
7518 | #endif | |
cef84c30 | 7519 | kvm_lapic_exit(); |
ff9d07a0 ZY |
7520 | perf_unregister_guest_info_callbacks(&kvm_guest_cbs); |
7521 | ||
888d256e JK |
7522 | if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) |
7523 | cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block, | |
7524 | CPUFREQ_TRANSITION_NOTIFIER); | |
251a5fd6 | 7525 | cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE); |
16e8d74d MT |
7526 | #ifdef CONFIG_X86_64 |
7527 | pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier); | |
7528 | #endif | |
afaf0b2f | 7529 | kvm_x86_ops.hardware_enable = NULL; |
56c6d28a | 7530 | kvm_mmu_module_exit(); |
013f6a5d | 7531 | free_percpu(shared_msrs); |
b666a4b6 | 7532 | kmem_cache_destroy(x86_fpu_cache); |
56c6d28a | 7533 | } |
f8c16bba | 7534 | |
5cb56059 | 7535 | int kvm_vcpu_halt(struct kvm_vcpu *vcpu) |
8776e519 HB |
7536 | { |
7537 | ++vcpu->stat.halt_exits; | |
35754c98 | 7538 | if (lapic_in_kernel(vcpu)) { |
a4535290 | 7539 | vcpu->arch.mp_state = KVM_MP_STATE_HALTED; |
8776e519 HB |
7540 | return 1; |
7541 | } else { | |
7542 | vcpu->run->exit_reason = KVM_EXIT_HLT; | |
7543 | return 0; | |
7544 | } | |
7545 | } | |
5cb56059 JS |
7546 | EXPORT_SYMBOL_GPL(kvm_vcpu_halt); |
7547 | ||
7548 | int kvm_emulate_halt(struct kvm_vcpu *vcpu) | |
7549 | { | |
6affcbed KH |
7550 | int ret = kvm_skip_emulated_instruction(vcpu); |
7551 | /* | |
7552 | * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered | |
7553 | * KVM_EXIT_DEBUG here. | |
7554 | */ | |
7555 | return kvm_vcpu_halt(vcpu) && ret; | |
5cb56059 | 7556 | } |
8776e519 HB |
7557 | EXPORT_SYMBOL_GPL(kvm_emulate_halt); |
7558 | ||
8ef81a9a | 7559 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
7560 | static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr, |
7561 | unsigned long clock_type) | |
7562 | { | |
7563 | struct kvm_clock_pairing clock_pairing; | |
899a31f5 | 7564 | struct timespec64 ts; |
80fbd89c | 7565 | u64 cycle; |
55dd00a7 MT |
7566 | int ret; |
7567 | ||
7568 | if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK) | |
7569 | return -KVM_EOPNOTSUPP; | |
7570 | ||
7571 | if (kvm_get_walltime_and_clockread(&ts, &cycle) == false) | |
7572 | return -KVM_EOPNOTSUPP; | |
7573 | ||
7574 | clock_pairing.sec = ts.tv_sec; | |
7575 | clock_pairing.nsec = ts.tv_nsec; | |
7576 | clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle); | |
7577 | clock_pairing.flags = 0; | |
bcbfbd8e | 7578 | memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad)); |
55dd00a7 MT |
7579 | |
7580 | ret = 0; | |
7581 | if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing, | |
7582 | sizeof(struct kvm_clock_pairing))) | |
7583 | ret = -KVM_EFAULT; | |
7584 | ||
7585 | return ret; | |
7586 | } | |
8ef81a9a | 7587 | #endif |
55dd00a7 | 7588 | |
6aef266c SV |
7589 | /* |
7590 | * kvm_pv_kick_cpu_op: Kick a vcpu. | |
7591 | * | |
7592 | * @apicid - apicid of vcpu to be kicked. | |
7593 | */ | |
7594 | static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid) | |
7595 | { | |
24d2166b | 7596 | struct kvm_lapic_irq lapic_irq; |
6aef266c | 7597 | |
150a84fe | 7598 | lapic_irq.shorthand = APIC_DEST_NOSHORT; |
c96001c5 | 7599 | lapic_irq.dest_mode = APIC_DEST_PHYSICAL; |
ebd28fcb | 7600 | lapic_irq.level = 0; |
24d2166b | 7601 | lapic_irq.dest_id = apicid; |
93bbf0b8 | 7602 | lapic_irq.msi_redir_hint = false; |
6aef266c | 7603 | |
24d2166b | 7604 | lapic_irq.delivery_mode = APIC_DM_REMRD; |
795a149e | 7605 | kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL); |
6aef266c SV |
7606 | } |
7607 | ||
4e19c36f SS |
7608 | bool kvm_apicv_activated(struct kvm *kvm) |
7609 | { | |
7610 | return (READ_ONCE(kvm->arch.apicv_inhibit_reasons) == 0); | |
7611 | } | |
7612 | EXPORT_SYMBOL_GPL(kvm_apicv_activated); | |
7613 | ||
7614 | void kvm_apicv_init(struct kvm *kvm, bool enable) | |
7615 | { | |
7616 | if (enable) | |
7617 | clear_bit(APICV_INHIBIT_REASON_DISABLE, | |
7618 | &kvm->arch.apicv_inhibit_reasons); | |
7619 | else | |
7620 | set_bit(APICV_INHIBIT_REASON_DISABLE, | |
7621 | &kvm->arch.apicv_inhibit_reasons); | |
7622 | } | |
7623 | EXPORT_SYMBOL_GPL(kvm_apicv_init); | |
7624 | ||
71506297 WL |
7625 | static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id) |
7626 | { | |
7627 | struct kvm_vcpu *target = NULL; | |
7628 | struct kvm_apic_map *map; | |
7629 | ||
7630 | rcu_read_lock(); | |
7631 | map = rcu_dereference(kvm->arch.apic_map); | |
7632 | ||
7633 | if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id]) | |
7634 | target = map->phys_map[dest_id]->vcpu; | |
7635 | ||
7636 | rcu_read_unlock(); | |
7637 | ||
266e85a5 | 7638 | if (target && READ_ONCE(target->ready)) |
71506297 WL |
7639 | kvm_vcpu_yield_to(target); |
7640 | } | |
7641 | ||
8776e519 HB |
7642 | int kvm_emulate_hypercall(struct kvm_vcpu *vcpu) |
7643 | { | |
7644 | unsigned long nr, a0, a1, a2, a3, ret; | |
6356ee0c | 7645 | int op_64_bit; |
8776e519 | 7646 | |
696ca779 RK |
7647 | if (kvm_hv_hypercall_enabled(vcpu->kvm)) |
7648 | return kvm_hv_hypercall(vcpu); | |
55cd8e5a | 7649 | |
de3cd117 SC |
7650 | nr = kvm_rax_read(vcpu); |
7651 | a0 = kvm_rbx_read(vcpu); | |
7652 | a1 = kvm_rcx_read(vcpu); | |
7653 | a2 = kvm_rdx_read(vcpu); | |
7654 | a3 = kvm_rsi_read(vcpu); | |
8776e519 | 7655 | |
229456fc | 7656 | trace_kvm_hypercall(nr, a0, a1, a2, a3); |
2714d1d3 | 7657 | |
a449c7aa NA |
7658 | op_64_bit = is_64_bit_mode(vcpu); |
7659 | if (!op_64_bit) { | |
8776e519 HB |
7660 | nr &= 0xFFFFFFFF; |
7661 | a0 &= 0xFFFFFFFF; | |
7662 | a1 &= 0xFFFFFFFF; | |
7663 | a2 &= 0xFFFFFFFF; | |
7664 | a3 &= 0xFFFFFFFF; | |
7665 | } | |
7666 | ||
afaf0b2f | 7667 | if (kvm_x86_ops.get_cpl(vcpu) != 0) { |
07708c4a | 7668 | ret = -KVM_EPERM; |
696ca779 | 7669 | goto out; |
07708c4a JK |
7670 | } |
7671 | ||
8776e519 | 7672 | switch (nr) { |
b93463aa AK |
7673 | case KVM_HC_VAPIC_POLL_IRQ: |
7674 | ret = 0; | |
7675 | break; | |
6aef266c SV |
7676 | case KVM_HC_KICK_CPU: |
7677 | kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1); | |
266e85a5 | 7678 | kvm_sched_yield(vcpu->kvm, a1); |
6aef266c SV |
7679 | ret = 0; |
7680 | break; | |
8ef81a9a | 7681 | #ifdef CONFIG_X86_64 |
55dd00a7 MT |
7682 | case KVM_HC_CLOCK_PAIRING: |
7683 | ret = kvm_pv_clock_pairing(vcpu, a0, a1); | |
7684 | break; | |
1ed199a4 | 7685 | #endif |
4180bf1b WL |
7686 | case KVM_HC_SEND_IPI: |
7687 | ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit); | |
7688 | break; | |
71506297 WL |
7689 | case KVM_HC_SCHED_YIELD: |
7690 | kvm_sched_yield(vcpu->kvm, a0); | |
7691 | ret = 0; | |
7692 | break; | |
8776e519 HB |
7693 | default: |
7694 | ret = -KVM_ENOSYS; | |
7695 | break; | |
7696 | } | |
696ca779 | 7697 | out: |
a449c7aa NA |
7698 | if (!op_64_bit) |
7699 | ret = (u32)ret; | |
de3cd117 | 7700 | kvm_rax_write(vcpu, ret); |
6356ee0c | 7701 | |
f11c3a8d | 7702 | ++vcpu->stat.hypercalls; |
6356ee0c | 7703 | return kvm_skip_emulated_instruction(vcpu); |
8776e519 HB |
7704 | } |
7705 | EXPORT_SYMBOL_GPL(kvm_emulate_hypercall); | |
7706 | ||
b6785def | 7707 | static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt) |
8776e519 | 7708 | { |
d6aa1000 | 7709 | struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt); |
8776e519 | 7710 | char instruction[3]; |
5fdbf976 | 7711 | unsigned long rip = kvm_rip_read(vcpu); |
8776e519 | 7712 | |
afaf0b2f | 7713 | kvm_x86_ops.patch_hypercall(vcpu, instruction); |
8776e519 | 7714 | |
ce2e852e DV |
7715 | return emulator_write_emulated(ctxt, rip, instruction, 3, |
7716 | &ctxt->exception); | |
8776e519 HB |
7717 | } |
7718 | ||
851ba692 | 7719 | static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu) |
b6c7a5dc | 7720 | { |
782d422b MG |
7721 | return vcpu->run->request_interrupt_window && |
7722 | likely(!pic_in_kernel(vcpu->kvm)); | |
b6c7a5dc HB |
7723 | } |
7724 | ||
851ba692 | 7725 | static void post_kvm_run_save(struct kvm_vcpu *vcpu) |
b6c7a5dc | 7726 | { |
851ba692 AK |
7727 | struct kvm_run *kvm_run = vcpu->run; |
7728 | ||
91586a3b | 7729 | kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0; |
f077825a | 7730 | kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0; |
2d3ad1f4 | 7731 | kvm_run->cr8 = kvm_get_cr8(vcpu); |
b6c7a5dc | 7732 | kvm_run->apic_base = kvm_get_apic_base(vcpu); |
127a457a MG |
7733 | kvm_run->ready_for_interrupt_injection = |
7734 | pic_in_kernel(vcpu->kvm) || | |
782d422b | 7735 | kvm_vcpu_ready_for_interrupt_injection(vcpu); |
b6c7a5dc HB |
7736 | } |
7737 | ||
95ba8273 GN |
7738 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) |
7739 | { | |
7740 | int max_irr, tpr; | |
7741 | ||
afaf0b2f | 7742 | if (!kvm_x86_ops.update_cr8_intercept) |
95ba8273 GN |
7743 | return; |
7744 | ||
bce87cce | 7745 | if (!lapic_in_kernel(vcpu)) |
88c808fd AK |
7746 | return; |
7747 | ||
d62caabb AS |
7748 | if (vcpu->arch.apicv_active) |
7749 | return; | |
7750 | ||
8db3baa2 GN |
7751 | if (!vcpu->arch.apic->vapic_addr) |
7752 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
7753 | else | |
7754 | max_irr = -1; | |
95ba8273 GN |
7755 | |
7756 | if (max_irr != -1) | |
7757 | max_irr >>= 4; | |
7758 | ||
7759 | tpr = kvm_lapic_get_cr8(vcpu); | |
7760 | ||
afaf0b2f | 7761 | kvm_x86_ops.update_cr8_intercept(vcpu, tpr, max_irr); |
95ba8273 GN |
7762 | } |
7763 | ||
c9d40913 | 7764 | static void inject_pending_event(struct kvm_vcpu *vcpu, bool *req_immediate_exit) |
95ba8273 | 7765 | { |
b6b8a145 | 7766 | int r; |
c6b22f59 | 7767 | bool can_inject = true; |
b6b8a145 | 7768 | |
95ba8273 | 7769 | /* try to reinject previous events if any */ |
664f8e26 | 7770 | |
c6b22f59 | 7771 | if (vcpu->arch.exception.injected) { |
afaf0b2f | 7772 | kvm_x86_ops.queue_exception(vcpu); |
c6b22f59 PB |
7773 | can_inject = false; |
7774 | } | |
664f8e26 | 7775 | /* |
a042c26f LA |
7776 | * Do not inject an NMI or interrupt if there is a pending |
7777 | * exception. Exceptions and interrupts are recognized at | |
7778 | * instruction boundaries, i.e. the start of an instruction. | |
7779 | * Trap-like exceptions, e.g. #DB, have higher priority than | |
7780 | * NMIs and interrupts, i.e. traps are recognized before an | |
7781 | * NMI/interrupt that's pending on the same instruction. | |
7782 | * Fault-like exceptions, e.g. #GP and #PF, are the lowest | |
7783 | * priority, but are only generated (pended) during instruction | |
7784 | * execution, i.e. a pending fault-like exception means the | |
7785 | * fault occurred on the *previous* instruction and must be | |
7786 | * serviced prior to recognizing any new events in order to | |
7787 | * fully complete the previous instruction. | |
664f8e26 | 7788 | */ |
1a680e35 | 7789 | else if (!vcpu->arch.exception.pending) { |
c6b22f59 | 7790 | if (vcpu->arch.nmi_injected) { |
afaf0b2f | 7791 | kvm_x86_ops.set_nmi(vcpu); |
c6b22f59 PB |
7792 | can_inject = false; |
7793 | } else if (vcpu->arch.interrupt.injected) { | |
afaf0b2f | 7794 | kvm_x86_ops.set_irq(vcpu); |
c6b22f59 PB |
7795 | can_inject = false; |
7796 | } | |
664f8e26 WL |
7797 | } |
7798 | ||
3b82b8d7 SC |
7799 | WARN_ON_ONCE(vcpu->arch.exception.injected && |
7800 | vcpu->arch.exception.pending); | |
7801 | ||
1a680e35 LA |
7802 | /* |
7803 | * Call check_nested_events() even if we reinjected a previous event | |
7804 | * in order for caller to determine if it should require immediate-exit | |
7805 | * from L2 to L1 due to pending L1 events which require exit | |
7806 | * from L2 to L1. | |
7807 | */ | |
56083bdf | 7808 | if (is_guest_mode(vcpu)) { |
33b22172 | 7809 | r = kvm_x86_ops.nested_ops->check_events(vcpu); |
c9d40913 PB |
7810 | if (r < 0) |
7811 | goto busy; | |
664f8e26 WL |
7812 | } |
7813 | ||
7814 | /* try to inject new event if pending */ | |
b59bb7bd | 7815 | if (vcpu->arch.exception.pending) { |
5c1c85d0 AK |
7816 | trace_kvm_inj_exception(vcpu->arch.exception.nr, |
7817 | vcpu->arch.exception.has_error_code, | |
7818 | vcpu->arch.exception.error_code); | |
d6e8c854 | 7819 | |
664f8e26 WL |
7820 | vcpu->arch.exception.pending = false; |
7821 | vcpu->arch.exception.injected = true; | |
7822 | ||
d6e8c854 NA |
7823 | if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT) |
7824 | __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) | | |
7825 | X86_EFLAGS_RF); | |
7826 | ||
f10c729f | 7827 | if (vcpu->arch.exception.nr == DB_VECTOR) { |
f10c729f JM |
7828 | kvm_deliver_exception_payload(vcpu); |
7829 | if (vcpu->arch.dr7 & DR7_GD) { | |
7830 | vcpu->arch.dr7 &= ~DR7_GD; | |
7831 | kvm_update_dr7(vcpu); | |
7832 | } | |
6bdf0662 NA |
7833 | } |
7834 | ||
afaf0b2f | 7835 | kvm_x86_ops.queue_exception(vcpu); |
c6b22f59 | 7836 | can_inject = false; |
1a680e35 LA |
7837 | } |
7838 | ||
c9d40913 PB |
7839 | /* |
7840 | * Finally, inject interrupt events. If an event cannot be injected | |
7841 | * due to architectural conditions (e.g. IF=0) a window-open exit | |
7842 | * will re-request KVM_REQ_EVENT. Sometimes however an event is pending | |
7843 | * and can architecturally be injected, but we cannot do it right now: | |
7844 | * an interrupt could have arrived just now and we have to inject it | |
7845 | * as a vmexit, or there could already an event in the queue, which is | |
7846 | * indicated by can_inject. In that case we request an immediate exit | |
7847 | * in order to make progress and get back here for another iteration. | |
7848 | * The kvm_x86_ops hooks communicate this by returning -EBUSY. | |
7849 | */ | |
7850 | if (vcpu->arch.smi_pending) { | |
7851 | r = can_inject ? kvm_x86_ops.smi_allowed(vcpu, true) : -EBUSY; | |
7852 | if (r < 0) | |
7853 | goto busy; | |
7854 | if (r) { | |
7855 | vcpu->arch.smi_pending = false; | |
7856 | ++vcpu->arch.smi_count; | |
7857 | enter_smm(vcpu); | |
7858 | can_inject = false; | |
7859 | } else | |
7860 | kvm_x86_ops.enable_smi_window(vcpu); | |
7861 | } | |
7862 | ||
7863 | if (vcpu->arch.nmi_pending) { | |
7864 | r = can_inject ? kvm_x86_ops.nmi_allowed(vcpu, true) : -EBUSY; | |
7865 | if (r < 0) | |
7866 | goto busy; | |
7867 | if (r) { | |
7868 | --vcpu->arch.nmi_pending; | |
7869 | vcpu->arch.nmi_injected = true; | |
7870 | kvm_x86_ops.set_nmi(vcpu); | |
7871 | can_inject = false; | |
7872 | WARN_ON(kvm_x86_ops.nmi_allowed(vcpu, true) < 0); | |
7873 | } | |
7874 | if (vcpu->arch.nmi_pending) | |
7875 | kvm_x86_ops.enable_nmi_window(vcpu); | |
7876 | } | |
1a680e35 | 7877 | |
c9d40913 PB |
7878 | if (kvm_cpu_has_injectable_intr(vcpu)) { |
7879 | r = can_inject ? kvm_x86_ops.interrupt_allowed(vcpu, true) : -EBUSY; | |
7880 | if (r < 0) | |
7881 | goto busy; | |
7882 | if (r) { | |
7883 | kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu), false); | |
7884 | kvm_x86_ops.set_irq(vcpu); | |
7885 | WARN_ON(kvm_x86_ops.interrupt_allowed(vcpu, true) < 0); | |
7886 | } | |
7887 | if (kvm_cpu_has_injectable_intr(vcpu)) | |
7888 | kvm_x86_ops.enable_irq_window(vcpu); | |
95ba8273 | 7889 | } |
ee2cd4b7 | 7890 | |
c9d40913 PB |
7891 | if (is_guest_mode(vcpu) && |
7892 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
7893 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
7894 | *req_immediate_exit = true; | |
7895 | ||
7896 | WARN_ON(vcpu->arch.exception.pending); | |
7897 | return; | |
7898 | ||
7899 | busy: | |
7900 | *req_immediate_exit = true; | |
7901 | return; | |
95ba8273 GN |
7902 | } |
7903 | ||
7460fb4a AK |
7904 | static void process_nmi(struct kvm_vcpu *vcpu) |
7905 | { | |
7906 | unsigned limit = 2; | |
7907 | ||
7908 | /* | |
7909 | * x86 is limited to one NMI running, and one NMI pending after it. | |
7910 | * If an NMI is already in progress, limit further NMIs to just one. | |
7911 | * Otherwise, allow two (and we'll inject the first one immediately). | |
7912 | */ | |
afaf0b2f | 7913 | if (kvm_x86_ops.get_nmi_mask(vcpu) || vcpu->arch.nmi_injected) |
7460fb4a AK |
7914 | limit = 1; |
7915 | ||
7916 | vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0); | |
7917 | vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit); | |
7918 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
7919 | } | |
7920 | ||
ee2cd4b7 | 7921 | static u32 enter_smm_get_segment_flags(struct kvm_segment *seg) |
660a5d51 PB |
7922 | { |
7923 | u32 flags = 0; | |
7924 | flags |= seg->g << 23; | |
7925 | flags |= seg->db << 22; | |
7926 | flags |= seg->l << 21; | |
7927 | flags |= seg->avl << 20; | |
7928 | flags |= seg->present << 15; | |
7929 | flags |= seg->dpl << 13; | |
7930 | flags |= seg->s << 12; | |
7931 | flags |= seg->type << 8; | |
7932 | return flags; | |
7933 | } | |
7934 | ||
ee2cd4b7 | 7935 | static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
7936 | { |
7937 | struct kvm_segment seg; | |
7938 | int offset; | |
7939 | ||
7940 | kvm_get_segment(vcpu, &seg, n); | |
7941 | put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector); | |
7942 | ||
7943 | if (n < 3) | |
7944 | offset = 0x7f84 + n * 12; | |
7945 | else | |
7946 | offset = 0x7f2c + (n - 3) * 12; | |
7947 | ||
7948 | put_smstate(u32, buf, offset + 8, seg.base); | |
7949 | put_smstate(u32, buf, offset + 4, seg.limit); | |
ee2cd4b7 | 7950 | put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
7951 | } |
7952 | ||
efbb288a | 7953 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 7954 | static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n) |
660a5d51 PB |
7955 | { |
7956 | struct kvm_segment seg; | |
7957 | int offset; | |
7958 | u16 flags; | |
7959 | ||
7960 | kvm_get_segment(vcpu, &seg, n); | |
7961 | offset = 0x7e00 + n * 16; | |
7962 | ||
ee2cd4b7 | 7963 | flags = enter_smm_get_segment_flags(&seg) >> 8; |
660a5d51 PB |
7964 | put_smstate(u16, buf, offset, seg.selector); |
7965 | put_smstate(u16, buf, offset + 2, flags); | |
7966 | put_smstate(u32, buf, offset + 4, seg.limit); | |
7967 | put_smstate(u64, buf, offset + 8, seg.base); | |
7968 | } | |
efbb288a | 7969 | #endif |
660a5d51 | 7970 | |
ee2cd4b7 | 7971 | static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 PB |
7972 | { |
7973 | struct desc_ptr dt; | |
7974 | struct kvm_segment seg; | |
7975 | unsigned long val; | |
7976 | int i; | |
7977 | ||
7978 | put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu)); | |
7979 | put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu)); | |
7980 | put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu)); | |
7981 | put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu)); | |
7982 | ||
7983 | for (i = 0; i < 8; i++) | |
7984 | put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i)); | |
7985 | ||
7986 | kvm_get_dr(vcpu, 6, &val); | |
7987 | put_smstate(u32, buf, 0x7fcc, (u32)val); | |
7988 | kvm_get_dr(vcpu, 7, &val); | |
7989 | put_smstate(u32, buf, 0x7fc8, (u32)val); | |
7990 | ||
7991 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
7992 | put_smstate(u32, buf, 0x7fc4, seg.selector); | |
7993 | put_smstate(u32, buf, 0x7f64, seg.base); | |
7994 | put_smstate(u32, buf, 0x7f60, seg.limit); | |
ee2cd4b7 | 7995 | put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg)); |
660a5d51 PB |
7996 | |
7997 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
7998 | put_smstate(u32, buf, 0x7fc0, seg.selector); | |
7999 | put_smstate(u32, buf, 0x7f80, seg.base); | |
8000 | put_smstate(u32, buf, 0x7f7c, seg.limit); | |
ee2cd4b7 | 8001 | put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg)); |
660a5d51 | 8002 | |
afaf0b2f | 8003 | kvm_x86_ops.get_gdt(vcpu, &dt); |
660a5d51 PB |
8004 | put_smstate(u32, buf, 0x7f74, dt.address); |
8005 | put_smstate(u32, buf, 0x7f70, dt.size); | |
8006 | ||
afaf0b2f | 8007 | kvm_x86_ops.get_idt(vcpu, &dt); |
660a5d51 PB |
8008 | put_smstate(u32, buf, 0x7f58, dt.address); |
8009 | put_smstate(u32, buf, 0x7f54, dt.size); | |
8010 | ||
8011 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 8012 | enter_smm_save_seg_32(vcpu, buf, i); |
660a5d51 PB |
8013 | |
8014 | put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu)); | |
8015 | ||
8016 | /* revision id */ | |
8017 | put_smstate(u32, buf, 0x7efc, 0x00020000); | |
8018 | put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase); | |
8019 | } | |
8020 | ||
b68f3cc7 | 8021 | #ifdef CONFIG_X86_64 |
ee2cd4b7 | 8022 | static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf) |
660a5d51 | 8023 | { |
660a5d51 PB |
8024 | struct desc_ptr dt; |
8025 | struct kvm_segment seg; | |
8026 | unsigned long val; | |
8027 | int i; | |
8028 | ||
8029 | for (i = 0; i < 16; i++) | |
8030 | put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i)); | |
8031 | ||
8032 | put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu)); | |
8033 | put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu)); | |
8034 | ||
8035 | kvm_get_dr(vcpu, 6, &val); | |
8036 | put_smstate(u64, buf, 0x7f68, val); | |
8037 | kvm_get_dr(vcpu, 7, &val); | |
8038 | put_smstate(u64, buf, 0x7f60, val); | |
8039 | ||
8040 | put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu)); | |
8041 | put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu)); | |
8042 | put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu)); | |
8043 | ||
8044 | put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase); | |
8045 | ||
8046 | /* revision id */ | |
8047 | put_smstate(u32, buf, 0x7efc, 0x00020064); | |
8048 | ||
8049 | put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer); | |
8050 | ||
8051 | kvm_get_segment(vcpu, &seg, VCPU_SREG_TR); | |
8052 | put_smstate(u16, buf, 0x7e90, seg.selector); | |
ee2cd4b7 | 8053 | put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
8054 | put_smstate(u32, buf, 0x7e94, seg.limit); |
8055 | put_smstate(u64, buf, 0x7e98, seg.base); | |
8056 | ||
afaf0b2f | 8057 | kvm_x86_ops.get_idt(vcpu, &dt); |
660a5d51 PB |
8058 | put_smstate(u32, buf, 0x7e84, dt.size); |
8059 | put_smstate(u64, buf, 0x7e88, dt.address); | |
8060 | ||
8061 | kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR); | |
8062 | put_smstate(u16, buf, 0x7e70, seg.selector); | |
ee2cd4b7 | 8063 | put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8); |
660a5d51 PB |
8064 | put_smstate(u32, buf, 0x7e74, seg.limit); |
8065 | put_smstate(u64, buf, 0x7e78, seg.base); | |
8066 | ||
afaf0b2f | 8067 | kvm_x86_ops.get_gdt(vcpu, &dt); |
660a5d51 PB |
8068 | put_smstate(u32, buf, 0x7e64, dt.size); |
8069 | put_smstate(u64, buf, 0x7e68, dt.address); | |
8070 | ||
8071 | for (i = 0; i < 6; i++) | |
ee2cd4b7 | 8072 | enter_smm_save_seg_64(vcpu, buf, i); |
660a5d51 | 8073 | } |
b68f3cc7 | 8074 | #endif |
660a5d51 | 8075 | |
ee2cd4b7 | 8076 | static void enter_smm(struct kvm_vcpu *vcpu) |
64d60670 | 8077 | { |
660a5d51 | 8078 | struct kvm_segment cs, ds; |
18c3626e | 8079 | struct desc_ptr dt; |
660a5d51 PB |
8080 | char buf[512]; |
8081 | u32 cr0; | |
8082 | ||
660a5d51 | 8083 | trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true); |
660a5d51 | 8084 | memset(buf, 0, 512); |
b68f3cc7 | 8085 | #ifdef CONFIG_X86_64 |
d6321d49 | 8086 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
ee2cd4b7 | 8087 | enter_smm_save_state_64(vcpu, buf); |
660a5d51 | 8088 | else |
b68f3cc7 | 8089 | #endif |
ee2cd4b7 | 8090 | enter_smm_save_state_32(vcpu, buf); |
660a5d51 | 8091 | |
0234bf88 LP |
8092 | /* |
8093 | * Give pre_enter_smm() a chance to make ISA-specific changes to the | |
8094 | * vCPU state (e.g. leave guest mode) after we've saved the state into | |
8095 | * the SMM state-save area. | |
8096 | */ | |
afaf0b2f | 8097 | kvm_x86_ops.pre_enter_smm(vcpu, buf); |
0234bf88 LP |
8098 | |
8099 | vcpu->arch.hflags |= HF_SMM_MASK; | |
54bf36aa | 8100 | kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf)); |
660a5d51 | 8101 | |
afaf0b2f | 8102 | if (kvm_x86_ops.get_nmi_mask(vcpu)) |
660a5d51 PB |
8103 | vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK; |
8104 | else | |
afaf0b2f | 8105 | kvm_x86_ops.set_nmi_mask(vcpu, true); |
660a5d51 PB |
8106 | |
8107 | kvm_set_rflags(vcpu, X86_EFLAGS_FIXED); | |
8108 | kvm_rip_write(vcpu, 0x8000); | |
8109 | ||
8110 | cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG); | |
afaf0b2f | 8111 | kvm_x86_ops.set_cr0(vcpu, cr0); |
660a5d51 PB |
8112 | vcpu->arch.cr0 = cr0; |
8113 | ||
afaf0b2f | 8114 | kvm_x86_ops.set_cr4(vcpu, 0); |
660a5d51 | 8115 | |
18c3626e PB |
8116 | /* Undocumented: IDT limit is set to zero on entry to SMM. */ |
8117 | dt.address = dt.size = 0; | |
afaf0b2f | 8118 | kvm_x86_ops.set_idt(vcpu, &dt); |
18c3626e | 8119 | |
660a5d51 PB |
8120 | __kvm_set_dr(vcpu, 7, DR7_FIXED_1); |
8121 | ||
8122 | cs.selector = (vcpu->arch.smbase >> 4) & 0xffff; | |
8123 | cs.base = vcpu->arch.smbase; | |
8124 | ||
8125 | ds.selector = 0; | |
8126 | ds.base = 0; | |
8127 | ||
8128 | cs.limit = ds.limit = 0xffffffff; | |
8129 | cs.type = ds.type = 0x3; | |
8130 | cs.dpl = ds.dpl = 0; | |
8131 | cs.db = ds.db = 0; | |
8132 | cs.s = ds.s = 1; | |
8133 | cs.l = ds.l = 0; | |
8134 | cs.g = ds.g = 1; | |
8135 | cs.avl = ds.avl = 0; | |
8136 | cs.present = ds.present = 1; | |
8137 | cs.unusable = ds.unusable = 0; | |
8138 | cs.padding = ds.padding = 0; | |
8139 | ||
8140 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
8141 | kvm_set_segment(vcpu, &ds, VCPU_SREG_DS); | |
8142 | kvm_set_segment(vcpu, &ds, VCPU_SREG_ES); | |
8143 | kvm_set_segment(vcpu, &ds, VCPU_SREG_FS); | |
8144 | kvm_set_segment(vcpu, &ds, VCPU_SREG_GS); | |
8145 | kvm_set_segment(vcpu, &ds, VCPU_SREG_SS); | |
8146 | ||
b68f3cc7 | 8147 | #ifdef CONFIG_X86_64 |
d6321d49 | 8148 | if (guest_cpuid_has(vcpu, X86_FEATURE_LM)) |
afaf0b2f | 8149 | kvm_x86_ops.set_efer(vcpu, 0); |
b68f3cc7 | 8150 | #endif |
660a5d51 PB |
8151 | |
8152 | kvm_update_cpuid(vcpu); | |
8153 | kvm_mmu_reset_context(vcpu); | |
64d60670 PB |
8154 | } |
8155 | ||
ee2cd4b7 | 8156 | static void process_smi(struct kvm_vcpu *vcpu) |
c43203ca PB |
8157 | { |
8158 | vcpu->arch.smi_pending = true; | |
8159 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
8160 | } | |
8161 | ||
7ee30bc1 NNL |
8162 | void kvm_make_scan_ioapic_request_mask(struct kvm *kvm, |
8163 | unsigned long *vcpu_bitmap) | |
8164 | { | |
8165 | cpumask_var_t cpus; | |
7ee30bc1 NNL |
8166 | |
8167 | zalloc_cpumask_var(&cpus, GFP_ATOMIC); | |
8168 | ||
db5a95ec | 8169 | kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC, |
54163a34 | 8170 | NULL, vcpu_bitmap, cpus); |
7ee30bc1 NNL |
8171 | |
8172 | free_cpumask_var(cpus); | |
8173 | } | |
8174 | ||
2860c4b1 PB |
8175 | void kvm_make_scan_ioapic_request(struct kvm *kvm) |
8176 | { | |
8177 | kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC); | |
8178 | } | |
8179 | ||
8df14af4 SS |
8180 | void kvm_vcpu_update_apicv(struct kvm_vcpu *vcpu) |
8181 | { | |
8182 | if (!lapic_in_kernel(vcpu)) | |
8183 | return; | |
8184 | ||
8185 | vcpu->arch.apicv_active = kvm_apicv_activated(vcpu->kvm); | |
8186 | kvm_apic_update_apicv(vcpu); | |
afaf0b2f | 8187 | kvm_x86_ops.refresh_apicv_exec_ctrl(vcpu); |
8df14af4 SS |
8188 | } |
8189 | EXPORT_SYMBOL_GPL(kvm_vcpu_update_apicv); | |
8190 | ||
8191 | /* | |
8192 | * NOTE: Do not hold any lock prior to calling this. | |
8193 | * | |
8194 | * In particular, kvm_request_apicv_update() expects kvm->srcu not to be | |
8195 | * locked, because it calls __x86_set_memory_region() which does | |
8196 | * synchronize_srcu(&kvm->srcu). | |
8197 | */ | |
8198 | void kvm_request_apicv_update(struct kvm *kvm, bool activate, ulong bit) | |
8199 | { | |
7d611233 | 8200 | struct kvm_vcpu *except; |
8e205a6b PB |
8201 | unsigned long old, new, expected; |
8202 | ||
afaf0b2f SC |
8203 | if (!kvm_x86_ops.check_apicv_inhibit_reasons || |
8204 | !kvm_x86_ops.check_apicv_inhibit_reasons(bit)) | |
ef8efd7a SS |
8205 | return; |
8206 | ||
8e205a6b PB |
8207 | old = READ_ONCE(kvm->arch.apicv_inhibit_reasons); |
8208 | do { | |
8209 | expected = new = old; | |
8210 | if (activate) | |
8211 | __clear_bit(bit, &new); | |
8212 | else | |
8213 | __set_bit(bit, &new); | |
8214 | if (new == old) | |
8215 | break; | |
8216 | old = cmpxchg(&kvm->arch.apicv_inhibit_reasons, expected, new); | |
8217 | } while (old != expected); | |
8218 | ||
8219 | if (!!old == !!new) | |
8220 | return; | |
8df14af4 | 8221 | |
24bbf74c | 8222 | trace_kvm_apicv_update_request(activate, bit); |
afaf0b2f SC |
8223 | if (kvm_x86_ops.pre_update_apicv_exec_ctrl) |
8224 | kvm_x86_ops.pre_update_apicv_exec_ctrl(kvm, activate); | |
7d611233 SS |
8225 | |
8226 | /* | |
8227 | * Sending request to update APICV for all other vcpus, | |
8228 | * while update the calling vcpu immediately instead of | |
8229 | * waiting for another #VMEXIT to handle the request. | |
8230 | */ | |
8231 | except = kvm_get_running_vcpu(); | |
8232 | kvm_make_all_cpus_request_except(kvm, KVM_REQ_APICV_UPDATE, | |
8233 | except); | |
8234 | if (except) | |
8235 | kvm_vcpu_update_apicv(except); | |
8df14af4 SS |
8236 | } |
8237 | EXPORT_SYMBOL_GPL(kvm_request_apicv_update); | |
8238 | ||
3d81bc7e | 8239 | static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu) |
c7c9c56c | 8240 | { |
dcbd3e49 | 8241 | if (!kvm_apic_present(vcpu)) |
3d81bc7e | 8242 | return; |
c7c9c56c | 8243 | |
6308630b | 8244 | bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256); |
c7c9c56c | 8245 | |
b053b2ae | 8246 | if (irqchip_split(vcpu->kvm)) |
6308630b | 8247 | kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors); |
db2bdcbb | 8248 | else { |
fa59cc00 | 8249 | if (vcpu->arch.apicv_active) |
afaf0b2f | 8250 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
e97f852f WL |
8251 | if (ioapic_in_kernel(vcpu->kvm)) |
8252 | kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors); | |
db2bdcbb | 8253 | } |
e40ff1d6 LA |
8254 | |
8255 | if (is_guest_mode(vcpu)) | |
8256 | vcpu->arch.load_eoi_exitmap_pending = true; | |
8257 | else | |
8258 | kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu); | |
8259 | } | |
8260 | ||
8261 | static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu) | |
8262 | { | |
8263 | u64 eoi_exit_bitmap[4]; | |
8264 | ||
8265 | if (!kvm_apic_hw_enabled(vcpu->arch.apic)) | |
8266 | return; | |
8267 | ||
5c919412 AS |
8268 | bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors, |
8269 | vcpu_to_synic(vcpu)->vec_bitmap, 256); | |
afaf0b2f | 8270 | kvm_x86_ops.load_eoi_exitmap(vcpu, eoi_exit_bitmap); |
c7c9c56c YZ |
8271 | } |
8272 | ||
e649b3f0 ET |
8273 | void kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm, |
8274 | unsigned long start, unsigned long end) | |
b1394e74 RK |
8275 | { |
8276 | unsigned long apic_address; | |
8277 | ||
8278 | /* | |
8279 | * The physical address of apic access page is stored in the VMCS. | |
8280 | * Update it when it becomes invalid. | |
8281 | */ | |
8282 | apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT); | |
8283 | if (start <= apic_address && apic_address < end) | |
8284 | kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD); | |
8285 | } | |
8286 | ||
4256f43f TC |
8287 | void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu) |
8288 | { | |
35754c98 | 8289 | if (!lapic_in_kernel(vcpu)) |
f439ed27 PB |
8290 | return; |
8291 | ||
afaf0b2f | 8292 | if (!kvm_x86_ops.set_apic_access_page_addr) |
4256f43f TC |
8293 | return; |
8294 | ||
a4148b7c | 8295 | kvm_x86_ops.set_apic_access_page_addr(vcpu); |
4256f43f | 8296 | } |
4256f43f | 8297 | |
d264ee0c SC |
8298 | void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu) |
8299 | { | |
8300 | smp_send_reschedule(vcpu->cpu); | |
8301 | } | |
8302 | EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit); | |
8303 | ||
9357d939 | 8304 | /* |
362c698f | 8305 | * Returns 1 to let vcpu_run() continue the guest execution loop without |
9357d939 TY |
8306 | * exiting to the userspace. Otherwise, the value will be returned to the |
8307 | * userspace. | |
8308 | */ | |
851ba692 | 8309 | static int vcpu_enter_guest(struct kvm_vcpu *vcpu) |
b6c7a5dc HB |
8310 | { |
8311 | int r; | |
62a193ed MG |
8312 | bool req_int_win = |
8313 | dm_request_for_irq_injection(vcpu) && | |
8314 | kvm_cpu_accept_dm_intr(vcpu); | |
404d5d7b | 8315 | fastpath_t exit_fastpath; |
62a193ed | 8316 | |
730dca42 | 8317 | bool req_immediate_exit = false; |
b6c7a5dc | 8318 | |
2fa6e1e1 | 8319 | if (kvm_request_pending(vcpu)) { |
671ddc70 | 8320 | if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) { |
33b22172 | 8321 | if (unlikely(!kvm_x86_ops.nested_ops->get_vmcs12_pages(vcpu))) { |
671ddc70 JM |
8322 | r = 0; |
8323 | goto out; | |
8324 | } | |
8325 | } | |
a8eeb04a | 8326 | if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu)) |
2e53d63a | 8327 | kvm_mmu_unload(vcpu); |
a8eeb04a | 8328 | if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu)) |
2f599714 | 8329 | __kvm_migrate_timers(vcpu); |
d828199e MT |
8330 | if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu)) |
8331 | kvm_gen_update_masterclock(vcpu->kvm); | |
0061d53d MT |
8332 | if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu)) |
8333 | kvm_gen_kvmclock_update(vcpu); | |
34c238a1 ZA |
8334 | if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) { |
8335 | r = kvm_guest_time_update(vcpu); | |
8cfdc000 ZA |
8336 | if (unlikely(r)) |
8337 | goto out; | |
8338 | } | |
a8eeb04a | 8339 | if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu)) |
4731d4c7 | 8340 | kvm_mmu_sync_roots(vcpu); |
727a7e27 PB |
8341 | if (kvm_check_request(KVM_REQ_LOAD_MMU_PGD, vcpu)) |
8342 | kvm_mmu_load_pgd(vcpu); | |
eeeb4f67 | 8343 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) { |
7780938c | 8344 | kvm_vcpu_flush_tlb_all(vcpu); |
eeeb4f67 SC |
8345 | |
8346 | /* Flushing all ASIDs flushes the current ASID... */ | |
8347 | kvm_clear_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu); | |
8348 | } | |
8349 | if (kvm_check_request(KVM_REQ_TLB_FLUSH_CURRENT, vcpu)) | |
8350 | kvm_vcpu_flush_tlb_current(vcpu); | |
0baedd79 VK |
8351 | if (kvm_check_request(KVM_REQ_HV_TLB_FLUSH, vcpu)) |
8352 | kvm_vcpu_flush_tlb_guest(vcpu); | |
eeeb4f67 | 8353 | |
a8eeb04a | 8354 | if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) { |
851ba692 | 8355 | vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS; |
b93463aa AK |
8356 | r = 0; |
8357 | goto out; | |
8358 | } | |
a8eeb04a | 8359 | if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) { |
851ba692 | 8360 | vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN; |
bbeac283 | 8361 | vcpu->mmio_needed = 0; |
71c4dfaf JR |
8362 | r = 0; |
8363 | goto out; | |
8364 | } | |
af585b92 GN |
8365 | if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) { |
8366 | /* Page is swapped out. Do synthetic halt */ | |
8367 | vcpu->arch.apf.halted = true; | |
8368 | r = 1; | |
8369 | goto out; | |
8370 | } | |
c9aaa895 GC |
8371 | if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu)) |
8372 | record_steal_time(vcpu); | |
64d60670 PB |
8373 | if (kvm_check_request(KVM_REQ_SMI, vcpu)) |
8374 | process_smi(vcpu); | |
7460fb4a AK |
8375 | if (kvm_check_request(KVM_REQ_NMI, vcpu)) |
8376 | process_nmi(vcpu); | |
f5132b01 | 8377 | if (kvm_check_request(KVM_REQ_PMU, vcpu)) |
c6702c9d | 8378 | kvm_pmu_handle_event(vcpu); |
f5132b01 | 8379 | if (kvm_check_request(KVM_REQ_PMI, vcpu)) |
c6702c9d | 8380 | kvm_pmu_deliver_pmi(vcpu); |
7543a635 SR |
8381 | if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) { |
8382 | BUG_ON(vcpu->arch.pending_ioapic_eoi > 255); | |
8383 | if (test_bit(vcpu->arch.pending_ioapic_eoi, | |
6308630b | 8384 | vcpu->arch.ioapic_handled_vectors)) { |
7543a635 SR |
8385 | vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI; |
8386 | vcpu->run->eoi.vector = | |
8387 | vcpu->arch.pending_ioapic_eoi; | |
8388 | r = 0; | |
8389 | goto out; | |
8390 | } | |
8391 | } | |
3d81bc7e YZ |
8392 | if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu)) |
8393 | vcpu_scan_ioapic(vcpu); | |
e40ff1d6 LA |
8394 | if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu)) |
8395 | vcpu_load_eoi_exitmap(vcpu); | |
4256f43f TC |
8396 | if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu)) |
8397 | kvm_vcpu_reload_apic_access_page(vcpu); | |
2ce79189 AS |
8398 | if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) { |
8399 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
8400 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH; | |
8401 | r = 0; | |
8402 | goto out; | |
8403 | } | |
e516cebb AS |
8404 | if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) { |
8405 | vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT; | |
8406 | vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET; | |
8407 | r = 0; | |
8408 | goto out; | |
8409 | } | |
db397571 AS |
8410 | if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) { |
8411 | vcpu->run->exit_reason = KVM_EXIT_HYPERV; | |
8412 | vcpu->run->hyperv = vcpu->arch.hyperv.exit; | |
8413 | r = 0; | |
8414 | goto out; | |
8415 | } | |
f3b138c5 AS |
8416 | |
8417 | /* | |
8418 | * KVM_REQ_HV_STIMER has to be processed after | |
8419 | * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers | |
8420 | * depend on the guest clock being up-to-date | |
8421 | */ | |
1f4b34f8 AS |
8422 | if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu)) |
8423 | kvm_hv_process_stimers(vcpu); | |
8df14af4 SS |
8424 | if (kvm_check_request(KVM_REQ_APICV_UPDATE, vcpu)) |
8425 | kvm_vcpu_update_apicv(vcpu); | |
557a961a VK |
8426 | if (kvm_check_request(KVM_REQ_APF_READY, vcpu)) |
8427 | kvm_check_async_pf_completion(vcpu); | |
2f52d58c | 8428 | } |
b93463aa | 8429 | |
b463a6f7 | 8430 | if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) { |
0f1e261e | 8431 | ++vcpu->stat.req_event; |
66450a21 JK |
8432 | kvm_apic_accept_events(vcpu); |
8433 | if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) { | |
8434 | r = 1; | |
8435 | goto out; | |
8436 | } | |
8437 | ||
c9d40913 PB |
8438 | inject_pending_event(vcpu, &req_immediate_exit); |
8439 | if (req_int_win) | |
8440 | kvm_x86_ops.enable_irq_window(vcpu); | |
b463a6f7 AK |
8441 | |
8442 | if (kvm_lapic_enabled(vcpu)) { | |
8443 | update_cr8_intercept(vcpu); | |
8444 | kvm_lapic_sync_to_vapic(vcpu); | |
8445 | } | |
8446 | } | |
8447 | ||
d8368af8 AK |
8448 | r = kvm_mmu_reload(vcpu); |
8449 | if (unlikely(r)) { | |
d905c069 | 8450 | goto cancel_injection; |
d8368af8 AK |
8451 | } |
8452 | ||
b6c7a5dc HB |
8453 | preempt_disable(); |
8454 | ||
afaf0b2f | 8455 | kvm_x86_ops.prepare_guest_switch(vcpu); |
b95234c8 PB |
8456 | |
8457 | /* | |
8458 | * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt | |
8459 | * IPI are then delayed after guest entry, which ensures that they | |
8460 | * result in virtual interrupt delivery. | |
8461 | */ | |
8462 | local_irq_disable(); | |
6b7e2d09 XG |
8463 | vcpu->mode = IN_GUEST_MODE; |
8464 | ||
01b71917 MT |
8465 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
8466 | ||
0f127d12 | 8467 | /* |
b95234c8 | 8468 | * 1) We should set ->mode before checking ->requests. Please see |
cde9af6e | 8469 | * the comment in kvm_vcpu_exiting_guest_mode(). |
b95234c8 | 8470 | * |
81b01667 | 8471 | * 2) For APICv, we should set ->mode before checking PID.ON. This |
b95234c8 PB |
8472 | * pairs with the memory barrier implicit in pi_test_and_set_on |
8473 | * (see vmx_deliver_posted_interrupt). | |
8474 | * | |
8475 | * 3) This also orders the write to mode from any reads to the page | |
8476 | * tables done while the VCPU is running. Please see the comment | |
8477 | * in kvm_flush_remote_tlbs. | |
6b7e2d09 | 8478 | */ |
01b71917 | 8479 | smp_mb__after_srcu_read_unlock(); |
b6c7a5dc | 8480 | |
b95234c8 PB |
8481 | /* |
8482 | * This handles the case where a posted interrupt was | |
8483 | * notified with kvm_vcpu_kick. | |
8484 | */ | |
fa59cc00 | 8485 | if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active) |
afaf0b2f | 8486 | kvm_x86_ops.sync_pir_to_irr(vcpu); |
32f88400 | 8487 | |
5a9f5443 | 8488 | if (kvm_vcpu_exit_request(vcpu)) { |
6b7e2d09 | 8489 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 8490 | smp_wmb(); |
6c142801 AK |
8491 | local_irq_enable(); |
8492 | preempt_enable(); | |
01b71917 | 8493 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
6c142801 | 8494 | r = 1; |
d905c069 | 8495 | goto cancel_injection; |
6c142801 AK |
8496 | } |
8497 | ||
c43203ca PB |
8498 | if (req_immediate_exit) { |
8499 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
afaf0b2f | 8500 | kvm_x86_ops.request_immediate_exit(vcpu); |
c43203ca | 8501 | } |
d6185f20 | 8502 | |
8b89fe1f | 8503 | trace_kvm_entry(vcpu->vcpu_id); |
6edaa530 | 8504 | guest_enter_irqoff(); |
b6c7a5dc | 8505 | |
2620fe26 SC |
8506 | fpregs_assert_state_consistent(); |
8507 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
8508 | switch_fpu_return(); | |
5f409e20 | 8509 | |
42dbaa5a | 8510 | if (unlikely(vcpu->arch.switch_db_regs)) { |
42dbaa5a JK |
8511 | set_debugreg(0, 7); |
8512 | set_debugreg(vcpu->arch.eff_db[0], 0); | |
8513 | set_debugreg(vcpu->arch.eff_db[1], 1); | |
8514 | set_debugreg(vcpu->arch.eff_db[2], 2); | |
8515 | set_debugreg(vcpu->arch.eff_db[3], 3); | |
c77fb5fe | 8516 | set_debugreg(vcpu->arch.dr6, 6); |
ae561ede | 8517 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; |
42dbaa5a | 8518 | } |
b6c7a5dc | 8519 | |
a9ab13ff | 8520 | exit_fastpath = kvm_x86_ops.run(vcpu); |
b6c7a5dc | 8521 | |
c77fb5fe PB |
8522 | /* |
8523 | * Do this here before restoring debug registers on the host. And | |
8524 | * since we do this before handling the vmexit, a DR access vmexit | |
8525 | * can (a) read the correct value of the debug registers, (b) set | |
8526 | * KVM_DEBUGREG_WONT_EXIT again. | |
8527 | */ | |
8528 | if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) { | |
c77fb5fe | 8529 | WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP); |
afaf0b2f | 8530 | kvm_x86_ops.sync_dirty_debug_regs(vcpu); |
70e4da7a | 8531 | kvm_update_dr0123(vcpu); |
70e4da7a PB |
8532 | kvm_update_dr7(vcpu); |
8533 | vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD; | |
c77fb5fe PB |
8534 | } |
8535 | ||
24f1e32c FW |
8536 | /* |
8537 | * If the guest has used debug registers, at least dr7 | |
8538 | * will be disabled while returning to the host. | |
8539 | * If we don't have active breakpoints in the host, we don't | |
8540 | * care about the messed up debug address registers. But if | |
8541 | * we have some of them active, restore the old state. | |
8542 | */ | |
59d8eb53 | 8543 | if (hw_breakpoint_active()) |
24f1e32c | 8544 | hw_breakpoint_restore(); |
42dbaa5a | 8545 | |
4ba76538 | 8546 | vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc()); |
1d5f066e | 8547 | |
6b7e2d09 | 8548 | vcpu->mode = OUTSIDE_GUEST_MODE; |
d94e1dc9 | 8549 | smp_wmb(); |
a547c6db | 8550 | |
a9ab13ff | 8551 | kvm_x86_ops.handle_exit_irqoff(vcpu); |
b6c7a5dc | 8552 | |
d7a08882 SC |
8553 | /* |
8554 | * Consume any pending interrupts, including the possible source of | |
8555 | * VM-Exit on SVM and any ticks that occur between VM-Exit and now. | |
8556 | * An instruction is required after local_irq_enable() to fully unblock | |
8557 | * interrupts on processors that implement an interrupt shadow, the | |
8558 | * stat.exits increment will do nicely. | |
8559 | */ | |
8560 | kvm_before_interrupt(vcpu); | |
8561 | local_irq_enable(); | |
b6c7a5dc | 8562 | ++vcpu->stat.exits; |
d7a08882 SC |
8563 | local_irq_disable(); |
8564 | kvm_after_interrupt(vcpu); | |
b6c7a5dc | 8565 | |
f2485b3e | 8566 | guest_exit_irqoff(); |
ec0671d5 WL |
8567 | if (lapic_in_kernel(vcpu)) { |
8568 | s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta; | |
8569 | if (delta != S64_MIN) { | |
8570 | trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta); | |
8571 | vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN; | |
8572 | } | |
8573 | } | |
b6c7a5dc | 8574 | |
f2485b3e | 8575 | local_irq_enable(); |
b6c7a5dc HB |
8576 | preempt_enable(); |
8577 | ||
f656ce01 | 8578 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
3200f405 | 8579 | |
b6c7a5dc HB |
8580 | /* |
8581 | * Profile KVM exit RIPs: | |
8582 | */ | |
8583 | if (unlikely(prof_on == KVM_PROFILING)) { | |
5fdbf976 MT |
8584 | unsigned long rip = kvm_rip_read(vcpu); |
8585 | profile_hit(KVM_PROFILING, (void *)rip); | |
b6c7a5dc HB |
8586 | } |
8587 | ||
cc578287 ZA |
8588 | if (unlikely(vcpu->arch.tsc_always_catchup)) |
8589 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); | |
298101da | 8590 | |
5cfb1d5a MT |
8591 | if (vcpu->arch.apic_attention) |
8592 | kvm_lapic_sync_from_vapic(vcpu); | |
b93463aa | 8593 | |
afaf0b2f | 8594 | r = kvm_x86_ops.handle_exit(vcpu, exit_fastpath); |
d905c069 MT |
8595 | return r; |
8596 | ||
8597 | cancel_injection: | |
8081ad06 SC |
8598 | if (req_immediate_exit) |
8599 | kvm_make_request(KVM_REQ_EVENT, vcpu); | |
afaf0b2f | 8600 | kvm_x86_ops.cancel_injection(vcpu); |
ae7a2a3f MT |
8601 | if (unlikely(vcpu->arch.apic_attention)) |
8602 | kvm_lapic_sync_from_vapic(vcpu); | |
d7690175 MT |
8603 | out: |
8604 | return r; | |
8605 | } | |
b6c7a5dc | 8606 | |
362c698f PB |
8607 | static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu) |
8608 | { | |
bf9f6ac8 | 8609 | if (!kvm_arch_vcpu_runnable(vcpu) && |
afaf0b2f | 8610 | (!kvm_x86_ops.pre_block || kvm_x86_ops.pre_block(vcpu) == 0)) { |
9c8fd1ba PB |
8611 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
8612 | kvm_vcpu_block(vcpu); | |
8613 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); | |
bf9f6ac8 | 8614 | |
afaf0b2f SC |
8615 | if (kvm_x86_ops.post_block) |
8616 | kvm_x86_ops.post_block(vcpu); | |
bf9f6ac8 | 8617 | |
9c8fd1ba PB |
8618 | if (!kvm_check_request(KVM_REQ_UNHALT, vcpu)) |
8619 | return 1; | |
8620 | } | |
362c698f PB |
8621 | |
8622 | kvm_apic_accept_events(vcpu); | |
8623 | switch(vcpu->arch.mp_state) { | |
8624 | case KVM_MP_STATE_HALTED: | |
8625 | vcpu->arch.pv.pv_unhalted = false; | |
8626 | vcpu->arch.mp_state = | |
8627 | KVM_MP_STATE_RUNNABLE; | |
b2869f28 | 8628 | /* fall through */ |
362c698f PB |
8629 | case KVM_MP_STATE_RUNNABLE: |
8630 | vcpu->arch.apf.halted = false; | |
8631 | break; | |
8632 | case KVM_MP_STATE_INIT_RECEIVED: | |
8633 | break; | |
8634 | default: | |
8635 | return -EINTR; | |
362c698f PB |
8636 | } |
8637 | return 1; | |
8638 | } | |
09cec754 | 8639 | |
5d9bc648 PB |
8640 | static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu) |
8641 | { | |
56083bdf | 8642 | if (is_guest_mode(vcpu)) |
33b22172 | 8643 | kvm_x86_ops.nested_ops->check_events(vcpu); |
0ad3bed6 | 8644 | |
5d9bc648 PB |
8645 | return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE && |
8646 | !vcpu->arch.apf.halted); | |
8647 | } | |
8648 | ||
362c698f | 8649 | static int vcpu_run(struct kvm_vcpu *vcpu) |
d7690175 MT |
8650 | { |
8651 | int r; | |
f656ce01 | 8652 | struct kvm *kvm = vcpu->kvm; |
d7690175 | 8653 | |
f656ce01 | 8654 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
c595ceee | 8655 | vcpu->arch.l1tf_flush_l1d = true; |
d7690175 | 8656 | |
362c698f | 8657 | for (;;) { |
58f800d5 | 8658 | if (kvm_vcpu_running(vcpu)) { |
851ba692 | 8659 | r = vcpu_enter_guest(vcpu); |
bf9f6ac8 | 8660 | } else { |
362c698f | 8661 | r = vcpu_block(kvm, vcpu); |
bf9f6ac8 FW |
8662 | } |
8663 | ||
09cec754 GN |
8664 | if (r <= 0) |
8665 | break; | |
8666 | ||
72875d8a | 8667 | kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu); |
09cec754 GN |
8668 | if (kvm_cpu_has_pending_timer(vcpu)) |
8669 | kvm_inject_pending_timer_irqs(vcpu); | |
8670 | ||
782d422b MG |
8671 | if (dm_request_for_irq_injection(vcpu) && |
8672 | kvm_vcpu_ready_for_interrupt_injection(vcpu)) { | |
4ca7dd8c PB |
8673 | r = 0; |
8674 | vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; | |
09cec754 | 8675 | ++vcpu->stat.request_irq_exits; |
362c698f | 8676 | break; |
09cec754 | 8677 | } |
af585b92 | 8678 | |
09cec754 GN |
8679 | if (signal_pending(current)) { |
8680 | r = -EINTR; | |
851ba692 | 8681 | vcpu->run->exit_reason = KVM_EXIT_INTR; |
09cec754 | 8682 | ++vcpu->stat.signal_exits; |
362c698f | 8683 | break; |
09cec754 GN |
8684 | } |
8685 | if (need_resched()) { | |
f656ce01 | 8686 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
c08ac06a | 8687 | cond_resched(); |
f656ce01 | 8688 | vcpu->srcu_idx = srcu_read_lock(&kvm->srcu); |
d7690175 | 8689 | } |
b6c7a5dc HB |
8690 | } |
8691 | ||
f656ce01 | 8692 | srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx); |
b6c7a5dc HB |
8693 | |
8694 | return r; | |
8695 | } | |
8696 | ||
716d51ab GN |
8697 | static inline int complete_emulated_io(struct kvm_vcpu *vcpu) |
8698 | { | |
8699 | int r; | |
60fc3d02 | 8700 | |
716d51ab | 8701 | vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
0ce97a2b | 8702 | r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE); |
716d51ab | 8703 | srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx); |
60fc3d02 | 8704 | return r; |
716d51ab GN |
8705 | } |
8706 | ||
8707 | static int complete_emulated_pio(struct kvm_vcpu *vcpu) | |
8708 | { | |
8709 | BUG_ON(!vcpu->arch.pio.count); | |
8710 | ||
8711 | return complete_emulated_io(vcpu); | |
8712 | } | |
8713 | ||
f78146b0 AK |
8714 | /* |
8715 | * Implements the following, as a state machine: | |
8716 | * | |
8717 | * read: | |
8718 | * for each fragment | |
87da7e66 XG |
8719 | * for each mmio piece in the fragment |
8720 | * write gpa, len | |
8721 | * exit | |
8722 | * copy data | |
f78146b0 AK |
8723 | * execute insn |
8724 | * | |
8725 | * write: | |
8726 | * for each fragment | |
87da7e66 XG |
8727 | * for each mmio piece in the fragment |
8728 | * write gpa, len | |
8729 | * copy data | |
8730 | * exit | |
f78146b0 | 8731 | */ |
716d51ab | 8732 | static int complete_emulated_mmio(struct kvm_vcpu *vcpu) |
5287f194 AK |
8733 | { |
8734 | struct kvm_run *run = vcpu->run; | |
f78146b0 | 8735 | struct kvm_mmio_fragment *frag; |
87da7e66 | 8736 | unsigned len; |
5287f194 | 8737 | |
716d51ab | 8738 | BUG_ON(!vcpu->mmio_needed); |
5287f194 | 8739 | |
716d51ab | 8740 | /* Complete previous fragment */ |
87da7e66 XG |
8741 | frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment]; |
8742 | len = min(8u, frag->len); | |
716d51ab | 8743 | if (!vcpu->mmio_is_write) |
87da7e66 XG |
8744 | memcpy(frag->data, run->mmio.data, len); |
8745 | ||
8746 | if (frag->len <= 8) { | |
8747 | /* Switch to the next fragment. */ | |
8748 | frag++; | |
8749 | vcpu->mmio_cur_fragment++; | |
8750 | } else { | |
8751 | /* Go forward to the next mmio piece. */ | |
8752 | frag->data += len; | |
8753 | frag->gpa += len; | |
8754 | frag->len -= len; | |
8755 | } | |
8756 | ||
a08d3b3b | 8757 | if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) { |
716d51ab | 8758 | vcpu->mmio_needed = 0; |
0912c977 PB |
8759 | |
8760 | /* FIXME: return into emulator if single-stepping. */ | |
cef4dea0 | 8761 | if (vcpu->mmio_is_write) |
716d51ab GN |
8762 | return 1; |
8763 | vcpu->mmio_read_completed = 1; | |
8764 | return complete_emulated_io(vcpu); | |
8765 | } | |
87da7e66 | 8766 | |
716d51ab GN |
8767 | run->exit_reason = KVM_EXIT_MMIO; |
8768 | run->mmio.phys_addr = frag->gpa; | |
8769 | if (vcpu->mmio_is_write) | |
87da7e66 XG |
8770 | memcpy(run->mmio.data, frag->data, min(8u, frag->len)); |
8771 | run->mmio.len = min(8u, frag->len); | |
716d51ab GN |
8772 | run->mmio.is_write = vcpu->mmio_is_write; |
8773 | vcpu->arch.complete_userspace_io = complete_emulated_mmio; | |
8774 | return 0; | |
5287f194 AK |
8775 | } |
8776 | ||
c9aef3b8 SC |
8777 | static void kvm_save_current_fpu(struct fpu *fpu) |
8778 | { | |
8779 | /* | |
8780 | * If the target FPU state is not resident in the CPU registers, just | |
8781 | * memcpy() from current, else save CPU state directly to the target. | |
8782 | */ | |
8783 | if (test_thread_flag(TIF_NEED_FPU_LOAD)) | |
8784 | memcpy(&fpu->state, ¤t->thread.fpu.state, | |
8785 | fpu_kernel_xstate_size); | |
8786 | else | |
8787 | copy_fpregs_to_fpstate(fpu); | |
8788 | } | |
8789 | ||
822f312d SAS |
8790 | /* Swap (qemu) user FPU context for the guest FPU context. */ |
8791 | static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu) | |
8792 | { | |
5f409e20 RR |
8793 | fpregs_lock(); |
8794 | ||
c9aef3b8 SC |
8795 | kvm_save_current_fpu(vcpu->arch.user_fpu); |
8796 | ||
afaf0b2f | 8797 | /* PKRU is separately restored in kvm_x86_ops.run. */ |
b666a4b6 | 8798 | __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state, |
822f312d | 8799 | ~XFEATURE_MASK_PKRU); |
5f409e20 RR |
8800 | |
8801 | fpregs_mark_activate(); | |
8802 | fpregs_unlock(); | |
8803 | ||
822f312d SAS |
8804 | trace_kvm_fpu(1); |
8805 | } | |
8806 | ||
8807 | /* When vcpu_run ends, restore user space FPU context. */ | |
8808 | static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu) | |
8809 | { | |
5f409e20 RR |
8810 | fpregs_lock(); |
8811 | ||
c9aef3b8 SC |
8812 | kvm_save_current_fpu(vcpu->arch.guest_fpu); |
8813 | ||
d9a710e5 | 8814 | copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state); |
5f409e20 RR |
8815 | |
8816 | fpregs_mark_activate(); | |
8817 | fpregs_unlock(); | |
8818 | ||
822f312d SAS |
8819 | ++vcpu->stat.fpu_reload; |
8820 | trace_kvm_fpu(0); | |
8821 | } | |
8822 | ||
1b94f6f8 | 8823 | int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu) |
b6c7a5dc | 8824 | { |
1b94f6f8 | 8825 | struct kvm_run *kvm_run = vcpu->run; |
b6c7a5dc | 8826 | int r; |
b6c7a5dc | 8827 | |
accb757d | 8828 | vcpu_load(vcpu); |
20b7035c | 8829 | kvm_sigset_activate(vcpu); |
5663d8f9 PX |
8830 | kvm_load_guest_fpu(vcpu); |
8831 | ||
a4535290 | 8832 | if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) { |
2f173d26 JS |
8833 | if (kvm_run->immediate_exit) { |
8834 | r = -EINTR; | |
8835 | goto out; | |
8836 | } | |
b6c7a5dc | 8837 | kvm_vcpu_block(vcpu); |
66450a21 | 8838 | kvm_apic_accept_events(vcpu); |
72875d8a | 8839 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
ac9f6dc0 | 8840 | r = -EAGAIN; |
a0595000 JS |
8841 | if (signal_pending(current)) { |
8842 | r = -EINTR; | |
1b94f6f8 | 8843 | kvm_run->exit_reason = KVM_EXIT_INTR; |
a0595000 JS |
8844 | ++vcpu->stat.signal_exits; |
8845 | } | |
ac9f6dc0 | 8846 | goto out; |
b6c7a5dc HB |
8847 | } |
8848 | ||
1b94f6f8 | 8849 | if (kvm_run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) { |
01643c51 KH |
8850 | r = -EINVAL; |
8851 | goto out; | |
8852 | } | |
8853 | ||
1b94f6f8 | 8854 | if (kvm_run->kvm_dirty_regs) { |
01643c51 KH |
8855 | r = sync_regs(vcpu); |
8856 | if (r != 0) | |
8857 | goto out; | |
8858 | } | |
8859 | ||
b6c7a5dc | 8860 | /* re-sync apic's tpr */ |
35754c98 | 8861 | if (!lapic_in_kernel(vcpu)) { |
eea1cff9 AP |
8862 | if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) { |
8863 | r = -EINVAL; | |
8864 | goto out; | |
8865 | } | |
8866 | } | |
b6c7a5dc | 8867 | |
716d51ab GN |
8868 | if (unlikely(vcpu->arch.complete_userspace_io)) { |
8869 | int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io; | |
8870 | vcpu->arch.complete_userspace_io = NULL; | |
8871 | r = cui(vcpu); | |
8872 | if (r <= 0) | |
5663d8f9 | 8873 | goto out; |
716d51ab GN |
8874 | } else |
8875 | WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed); | |
5287f194 | 8876 | |
460df4c1 PB |
8877 | if (kvm_run->immediate_exit) |
8878 | r = -EINTR; | |
8879 | else | |
8880 | r = vcpu_run(vcpu); | |
b6c7a5dc HB |
8881 | |
8882 | out: | |
5663d8f9 | 8883 | kvm_put_guest_fpu(vcpu); |
1b94f6f8 | 8884 | if (kvm_run->kvm_valid_regs) |
01643c51 | 8885 | store_regs(vcpu); |
f1d86e46 | 8886 | post_kvm_run_save(vcpu); |
20b7035c | 8887 | kvm_sigset_deactivate(vcpu); |
b6c7a5dc | 8888 | |
accb757d | 8889 | vcpu_put(vcpu); |
b6c7a5dc HB |
8890 | return r; |
8891 | } | |
8892 | ||
01643c51 | 8893 | static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 8894 | { |
7ae441ea GN |
8895 | if (vcpu->arch.emulate_regs_need_sync_to_vcpu) { |
8896 | /* | |
8897 | * We are here if userspace calls get_regs() in the middle of | |
8898 | * instruction emulation. Registers state needs to be copied | |
4a969980 | 8899 | * back from emulation context to vcpu. Userspace shouldn't do |
7ae441ea GN |
8900 | * that usually, but some bad designed PV devices (vmware |
8901 | * backdoor interface) need this to work | |
8902 | */ | |
c9b8b07c | 8903 | emulator_writeback_register_cache(vcpu->arch.emulate_ctxt); |
7ae441ea GN |
8904 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; |
8905 | } | |
de3cd117 SC |
8906 | regs->rax = kvm_rax_read(vcpu); |
8907 | regs->rbx = kvm_rbx_read(vcpu); | |
8908 | regs->rcx = kvm_rcx_read(vcpu); | |
8909 | regs->rdx = kvm_rdx_read(vcpu); | |
8910 | regs->rsi = kvm_rsi_read(vcpu); | |
8911 | regs->rdi = kvm_rdi_read(vcpu); | |
e9c16c78 | 8912 | regs->rsp = kvm_rsp_read(vcpu); |
de3cd117 | 8913 | regs->rbp = kvm_rbp_read(vcpu); |
b6c7a5dc | 8914 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
8915 | regs->r8 = kvm_r8_read(vcpu); |
8916 | regs->r9 = kvm_r9_read(vcpu); | |
8917 | regs->r10 = kvm_r10_read(vcpu); | |
8918 | regs->r11 = kvm_r11_read(vcpu); | |
8919 | regs->r12 = kvm_r12_read(vcpu); | |
8920 | regs->r13 = kvm_r13_read(vcpu); | |
8921 | regs->r14 = kvm_r14_read(vcpu); | |
8922 | regs->r15 = kvm_r15_read(vcpu); | |
b6c7a5dc HB |
8923 | #endif |
8924 | ||
5fdbf976 | 8925 | regs->rip = kvm_rip_read(vcpu); |
91586a3b | 8926 | regs->rflags = kvm_get_rflags(vcpu); |
01643c51 | 8927 | } |
b6c7a5dc | 8928 | |
01643c51 KH |
8929 | int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
8930 | { | |
8931 | vcpu_load(vcpu); | |
8932 | __get_regs(vcpu, regs); | |
1fc9b76b | 8933 | vcpu_put(vcpu); |
b6c7a5dc HB |
8934 | return 0; |
8935 | } | |
8936 | ||
01643c51 | 8937 | static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
b6c7a5dc | 8938 | { |
7ae441ea GN |
8939 | vcpu->arch.emulate_regs_need_sync_from_vcpu = true; |
8940 | vcpu->arch.emulate_regs_need_sync_to_vcpu = false; | |
8941 | ||
de3cd117 SC |
8942 | kvm_rax_write(vcpu, regs->rax); |
8943 | kvm_rbx_write(vcpu, regs->rbx); | |
8944 | kvm_rcx_write(vcpu, regs->rcx); | |
8945 | kvm_rdx_write(vcpu, regs->rdx); | |
8946 | kvm_rsi_write(vcpu, regs->rsi); | |
8947 | kvm_rdi_write(vcpu, regs->rdi); | |
e9c16c78 | 8948 | kvm_rsp_write(vcpu, regs->rsp); |
de3cd117 | 8949 | kvm_rbp_write(vcpu, regs->rbp); |
b6c7a5dc | 8950 | #ifdef CONFIG_X86_64 |
de3cd117 SC |
8951 | kvm_r8_write(vcpu, regs->r8); |
8952 | kvm_r9_write(vcpu, regs->r9); | |
8953 | kvm_r10_write(vcpu, regs->r10); | |
8954 | kvm_r11_write(vcpu, regs->r11); | |
8955 | kvm_r12_write(vcpu, regs->r12); | |
8956 | kvm_r13_write(vcpu, regs->r13); | |
8957 | kvm_r14_write(vcpu, regs->r14); | |
8958 | kvm_r15_write(vcpu, regs->r15); | |
b6c7a5dc HB |
8959 | #endif |
8960 | ||
5fdbf976 | 8961 | kvm_rip_write(vcpu, regs->rip); |
d73235d1 | 8962 | kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED); |
b6c7a5dc | 8963 | |
b4f14abd JK |
8964 | vcpu->arch.exception.pending = false; |
8965 | ||
3842d135 | 8966 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
01643c51 | 8967 | } |
3842d135 | 8968 | |
01643c51 KH |
8969 | int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs) |
8970 | { | |
8971 | vcpu_load(vcpu); | |
8972 | __set_regs(vcpu, regs); | |
875656fe | 8973 | vcpu_put(vcpu); |
b6c7a5dc HB |
8974 | return 0; |
8975 | } | |
8976 | ||
b6c7a5dc HB |
8977 | void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
8978 | { | |
8979 | struct kvm_segment cs; | |
8980 | ||
3e6e0aab | 8981 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); |
b6c7a5dc HB |
8982 | *db = cs.db; |
8983 | *l = cs.l; | |
8984 | } | |
8985 | EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits); | |
8986 | ||
01643c51 | 8987 | static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 8988 | { |
89a27f4d | 8989 | struct desc_ptr dt; |
b6c7a5dc | 8990 | |
3e6e0aab GT |
8991 | kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
8992 | kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
8993 | kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
8994 | kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
8995 | kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
8996 | kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 8997 | |
3e6e0aab GT |
8998 | kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
8999 | kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 9000 | |
afaf0b2f | 9001 | kvm_x86_ops.get_idt(vcpu, &dt); |
89a27f4d GN |
9002 | sregs->idt.limit = dt.size; |
9003 | sregs->idt.base = dt.address; | |
afaf0b2f | 9004 | kvm_x86_ops.get_gdt(vcpu, &dt); |
89a27f4d GN |
9005 | sregs->gdt.limit = dt.size; |
9006 | sregs->gdt.base = dt.address; | |
b6c7a5dc | 9007 | |
4d4ec087 | 9008 | sregs->cr0 = kvm_read_cr0(vcpu); |
ad312c7c | 9009 | sregs->cr2 = vcpu->arch.cr2; |
9f8fe504 | 9010 | sregs->cr3 = kvm_read_cr3(vcpu); |
fc78f519 | 9011 | sregs->cr4 = kvm_read_cr4(vcpu); |
2d3ad1f4 | 9012 | sregs->cr8 = kvm_get_cr8(vcpu); |
f6801dff | 9013 | sregs->efer = vcpu->arch.efer; |
b6c7a5dc HB |
9014 | sregs->apic_base = kvm_get_apic_base(vcpu); |
9015 | ||
0e96f31e | 9016 | memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap)); |
b6c7a5dc | 9017 | |
04140b41 | 9018 | if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft) |
14d0bc1f GN |
9019 | set_bit(vcpu->arch.interrupt.nr, |
9020 | (unsigned long *)sregs->interrupt_bitmap); | |
01643c51 | 9021 | } |
16d7a191 | 9022 | |
01643c51 KH |
9023 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, |
9024 | struct kvm_sregs *sregs) | |
9025 | { | |
9026 | vcpu_load(vcpu); | |
9027 | __get_sregs(vcpu, sregs); | |
bcdec41c | 9028 | vcpu_put(vcpu); |
b6c7a5dc HB |
9029 | return 0; |
9030 | } | |
9031 | ||
62d9f0db MT |
9032 | int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu, |
9033 | struct kvm_mp_state *mp_state) | |
9034 | { | |
fd232561 | 9035 | vcpu_load(vcpu); |
f958bd23 SC |
9036 | if (kvm_mpx_supported()) |
9037 | kvm_load_guest_fpu(vcpu); | |
fd232561 | 9038 | |
66450a21 | 9039 | kvm_apic_accept_events(vcpu); |
6aef266c SV |
9040 | if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED && |
9041 | vcpu->arch.pv.pv_unhalted) | |
9042 | mp_state->mp_state = KVM_MP_STATE_RUNNABLE; | |
9043 | else | |
9044 | mp_state->mp_state = vcpu->arch.mp_state; | |
9045 | ||
f958bd23 SC |
9046 | if (kvm_mpx_supported()) |
9047 | kvm_put_guest_fpu(vcpu); | |
fd232561 | 9048 | vcpu_put(vcpu); |
62d9f0db MT |
9049 | return 0; |
9050 | } | |
9051 | ||
9052 | int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu, | |
9053 | struct kvm_mp_state *mp_state) | |
9054 | { | |
e83dff5e CD |
9055 | int ret = -EINVAL; |
9056 | ||
9057 | vcpu_load(vcpu); | |
9058 | ||
bce87cce | 9059 | if (!lapic_in_kernel(vcpu) && |
66450a21 | 9060 | mp_state->mp_state != KVM_MP_STATE_RUNNABLE) |
e83dff5e | 9061 | goto out; |
66450a21 | 9062 | |
27cbe7d6 LA |
9063 | /* |
9064 | * KVM_MP_STATE_INIT_RECEIVED means the processor is in | |
9065 | * INIT state; latched init should be reported using | |
9066 | * KVM_SET_VCPU_EVENTS, so reject it here. | |
9067 | */ | |
9068 | if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) && | |
28bf2888 DH |
9069 | (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED || |
9070 | mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED)) | |
e83dff5e | 9071 | goto out; |
28bf2888 | 9072 | |
66450a21 JK |
9073 | if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) { |
9074 | vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED; | |
9075 | set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events); | |
9076 | } else | |
9077 | vcpu->arch.mp_state = mp_state->mp_state; | |
3842d135 | 9078 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
e83dff5e CD |
9079 | |
9080 | ret = 0; | |
9081 | out: | |
9082 | vcpu_put(vcpu); | |
9083 | return ret; | |
62d9f0db MT |
9084 | } |
9085 | ||
7f3d35fd KW |
9086 | int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index, |
9087 | int reason, bool has_error_code, u32 error_code) | |
b6c7a5dc | 9088 | { |
c9b8b07c | 9089 | struct x86_emulate_ctxt *ctxt = vcpu->arch.emulate_ctxt; |
8ec4722d | 9090 | int ret; |
e01c2426 | 9091 | |
8ec4722d | 9092 | init_emulate_ctxt(vcpu); |
c697518a | 9093 | |
7f3d35fd | 9094 | ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason, |
9d74191a | 9095 | has_error_code, error_code); |
1051778f SC |
9096 | if (ret) { |
9097 | vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
9098 | vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
9099 | vcpu->run->internal.ndata = 0; | |
60fc3d02 | 9100 | return 0; |
1051778f | 9101 | } |
37817f29 | 9102 | |
9d74191a TY |
9103 | kvm_rip_write(vcpu, ctxt->eip); |
9104 | kvm_set_rflags(vcpu, ctxt->eflags); | |
60fc3d02 | 9105 | return 1; |
37817f29 IE |
9106 | } |
9107 | EXPORT_SYMBOL_GPL(kvm_task_switch); | |
9108 | ||
3140c156 | 9109 | static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
f2981033 | 9110 | { |
37b95951 | 9111 | if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) { |
f2981033 LT |
9112 | /* |
9113 | * When EFER.LME and CR0.PG are set, the processor is in | |
9114 | * 64-bit mode (though maybe in a 32-bit code segment). | |
9115 | * CR4.PAE and EFER.LMA must be set. | |
9116 | */ | |
37b95951 | 9117 | if (!(sregs->cr4 & X86_CR4_PAE) |
f2981033 LT |
9118 | || !(sregs->efer & EFER_LMA)) |
9119 | return -EINVAL; | |
9120 | } else { | |
9121 | /* | |
9122 | * Not in 64-bit mode: EFER.LMA is clear and the code | |
9123 | * segment cannot be 64-bit. | |
9124 | */ | |
9125 | if (sregs->efer & EFER_LMA || sregs->cs.l) | |
9126 | return -EINVAL; | |
9127 | } | |
9128 | ||
3ca94192 | 9129 | return kvm_valid_cr4(vcpu, sregs->cr4); |
f2981033 LT |
9130 | } |
9131 | ||
01643c51 | 9132 | static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs) |
b6c7a5dc | 9133 | { |
58cb628d | 9134 | struct msr_data apic_base_msr; |
b6c7a5dc | 9135 | int mmu_reset_needed = 0; |
c4d21882 | 9136 | int cpuid_update_needed = 0; |
63f42e02 | 9137 | int pending_vec, max_bits, idx; |
89a27f4d | 9138 | struct desc_ptr dt; |
b4ef9d4e CD |
9139 | int ret = -EINVAL; |
9140 | ||
f2981033 | 9141 | if (kvm_valid_sregs(vcpu, sregs)) |
8dbfb2bf | 9142 | goto out; |
f2981033 | 9143 | |
d3802286 JM |
9144 | apic_base_msr.data = sregs->apic_base; |
9145 | apic_base_msr.host_initiated = true; | |
9146 | if (kvm_set_apic_base(vcpu, &apic_base_msr)) | |
b4ef9d4e | 9147 | goto out; |
6d1068b3 | 9148 | |
89a27f4d GN |
9149 | dt.size = sregs->idt.limit; |
9150 | dt.address = sregs->idt.base; | |
afaf0b2f | 9151 | kvm_x86_ops.set_idt(vcpu, &dt); |
89a27f4d GN |
9152 | dt.size = sregs->gdt.limit; |
9153 | dt.address = sregs->gdt.base; | |
afaf0b2f | 9154 | kvm_x86_ops.set_gdt(vcpu, &dt); |
b6c7a5dc | 9155 | |
ad312c7c | 9156 | vcpu->arch.cr2 = sregs->cr2; |
9f8fe504 | 9157 | mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3; |
dc7e795e | 9158 | vcpu->arch.cr3 = sregs->cr3; |
cb3c1e2f | 9159 | kvm_register_mark_available(vcpu, VCPU_EXREG_CR3); |
b6c7a5dc | 9160 | |
2d3ad1f4 | 9161 | kvm_set_cr8(vcpu, sregs->cr8); |
b6c7a5dc | 9162 | |
f6801dff | 9163 | mmu_reset_needed |= vcpu->arch.efer != sregs->efer; |
afaf0b2f | 9164 | kvm_x86_ops.set_efer(vcpu, sregs->efer); |
b6c7a5dc | 9165 | |
4d4ec087 | 9166 | mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0; |
afaf0b2f | 9167 | kvm_x86_ops.set_cr0(vcpu, sregs->cr0); |
d7306163 | 9168 | vcpu->arch.cr0 = sregs->cr0; |
b6c7a5dc | 9169 | |
fc78f519 | 9170 | mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4; |
c4d21882 WH |
9171 | cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) & |
9172 | (X86_CR4_OSXSAVE | X86_CR4_PKE)); | |
afaf0b2f | 9173 | kvm_x86_ops.set_cr4(vcpu, sregs->cr4); |
c4d21882 | 9174 | if (cpuid_update_needed) |
00b27a3e | 9175 | kvm_update_cpuid(vcpu); |
63f42e02 XG |
9176 | |
9177 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
bf03d4f9 | 9178 | if (is_pae_paging(vcpu)) { |
9f8fe504 | 9179 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
7c93be44 MT |
9180 | mmu_reset_needed = 1; |
9181 | } | |
63f42e02 | 9182 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
b6c7a5dc HB |
9183 | |
9184 | if (mmu_reset_needed) | |
9185 | kvm_mmu_reset_context(vcpu); | |
9186 | ||
a50abc3b | 9187 | max_bits = KVM_NR_INTERRUPTS; |
923c61bb GN |
9188 | pending_vec = find_first_bit( |
9189 | (const unsigned long *)sregs->interrupt_bitmap, max_bits); | |
9190 | if (pending_vec < max_bits) { | |
66fd3f7f | 9191 | kvm_queue_interrupt(vcpu, pending_vec, false); |
923c61bb | 9192 | pr_debug("Set back pending irq %d\n", pending_vec); |
b6c7a5dc HB |
9193 | } |
9194 | ||
3e6e0aab GT |
9195 | kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS); |
9196 | kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS); | |
9197 | kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES); | |
9198 | kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS); | |
9199 | kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS); | |
9200 | kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS); | |
b6c7a5dc | 9201 | |
3e6e0aab GT |
9202 | kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR); |
9203 | kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR); | |
b6c7a5dc | 9204 | |
5f0269f5 ME |
9205 | update_cr8_intercept(vcpu); |
9206 | ||
9c3e4aab | 9207 | /* Older userspace won't unhalt the vcpu on reset. */ |
c5af89b6 | 9208 | if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 && |
9c3e4aab | 9209 | sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 && |
3eeb3288 | 9210 | !is_protmode(vcpu)) |
9c3e4aab MT |
9211 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
9212 | ||
3842d135 AK |
9213 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
9214 | ||
b4ef9d4e CD |
9215 | ret = 0; |
9216 | out: | |
01643c51 KH |
9217 | return ret; |
9218 | } | |
9219 | ||
9220 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
9221 | struct kvm_sregs *sregs) | |
9222 | { | |
9223 | int ret; | |
9224 | ||
9225 | vcpu_load(vcpu); | |
9226 | ret = __set_sregs(vcpu, sregs); | |
b4ef9d4e CD |
9227 | vcpu_put(vcpu); |
9228 | return ret; | |
b6c7a5dc HB |
9229 | } |
9230 | ||
d0bfb940 JK |
9231 | int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu, |
9232 | struct kvm_guest_debug *dbg) | |
b6c7a5dc | 9233 | { |
355be0b9 | 9234 | unsigned long rflags; |
ae675ef0 | 9235 | int i, r; |
b6c7a5dc | 9236 | |
66b56562 CD |
9237 | vcpu_load(vcpu); |
9238 | ||
4f926bf2 JK |
9239 | if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) { |
9240 | r = -EBUSY; | |
9241 | if (vcpu->arch.exception.pending) | |
2122ff5e | 9242 | goto out; |
4f926bf2 JK |
9243 | if (dbg->control & KVM_GUESTDBG_INJECT_DB) |
9244 | kvm_queue_exception(vcpu, DB_VECTOR); | |
9245 | else | |
9246 | kvm_queue_exception(vcpu, BP_VECTOR); | |
9247 | } | |
9248 | ||
91586a3b JK |
9249 | /* |
9250 | * Read rflags as long as potentially injected trace flags are still | |
9251 | * filtered out. | |
9252 | */ | |
9253 | rflags = kvm_get_rflags(vcpu); | |
355be0b9 JK |
9254 | |
9255 | vcpu->guest_debug = dbg->control; | |
9256 | if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE)) | |
9257 | vcpu->guest_debug = 0; | |
9258 | ||
9259 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) { | |
ae675ef0 JK |
9260 | for (i = 0; i < KVM_NR_DB_REGS; ++i) |
9261 | vcpu->arch.eff_db[i] = dbg->arch.debugreg[i]; | |
c8639010 | 9262 | vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7]; |
ae675ef0 JK |
9263 | } else { |
9264 | for (i = 0; i < KVM_NR_DB_REGS; i++) | |
9265 | vcpu->arch.eff_db[i] = vcpu->arch.db[i]; | |
ae675ef0 | 9266 | } |
c8639010 | 9267 | kvm_update_dr7(vcpu); |
ae675ef0 | 9268 | |
f92653ee JK |
9269 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
9270 | vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) + | |
9271 | get_segment_base(vcpu, VCPU_SREG_CS); | |
94fe45da | 9272 | |
91586a3b JK |
9273 | /* |
9274 | * Trigger an rflags update that will inject or remove the trace | |
9275 | * flags. | |
9276 | */ | |
9277 | kvm_set_rflags(vcpu, rflags); | |
b6c7a5dc | 9278 | |
afaf0b2f | 9279 | kvm_x86_ops.update_bp_intercept(vcpu); |
b6c7a5dc | 9280 | |
4f926bf2 | 9281 | r = 0; |
d0bfb940 | 9282 | |
2122ff5e | 9283 | out: |
66b56562 | 9284 | vcpu_put(vcpu); |
b6c7a5dc HB |
9285 | return r; |
9286 | } | |
9287 | ||
8b006791 ZX |
9288 | /* |
9289 | * Translate a guest virtual address to a guest physical address. | |
9290 | */ | |
9291 | int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu, | |
9292 | struct kvm_translation *tr) | |
9293 | { | |
9294 | unsigned long vaddr = tr->linear_address; | |
9295 | gpa_t gpa; | |
f656ce01 | 9296 | int idx; |
8b006791 | 9297 | |
1da5b61d CD |
9298 | vcpu_load(vcpu); |
9299 | ||
f656ce01 | 9300 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
1871c602 | 9301 | gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL); |
f656ce01 | 9302 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8b006791 ZX |
9303 | tr->physical_address = gpa; |
9304 | tr->valid = gpa != UNMAPPED_GVA; | |
9305 | tr->writeable = 1; | |
9306 | tr->usermode = 0; | |
8b006791 | 9307 | |
1da5b61d | 9308 | vcpu_put(vcpu); |
8b006791 ZX |
9309 | return 0; |
9310 | } | |
9311 | ||
d0752060 HB |
9312 | int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) |
9313 | { | |
1393123e | 9314 | struct fxregs_state *fxsave; |
d0752060 | 9315 | |
1393123e | 9316 | vcpu_load(vcpu); |
d0752060 | 9317 | |
b666a4b6 | 9318 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
d0752060 HB |
9319 | memcpy(fpu->fpr, fxsave->st_space, 128); |
9320 | fpu->fcw = fxsave->cwd; | |
9321 | fpu->fsw = fxsave->swd; | |
9322 | fpu->ftwx = fxsave->twd; | |
9323 | fpu->last_opcode = fxsave->fop; | |
9324 | fpu->last_ip = fxsave->rip; | |
9325 | fpu->last_dp = fxsave->rdp; | |
0e96f31e | 9326 | memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space)); |
d0752060 | 9327 | |
1393123e | 9328 | vcpu_put(vcpu); |
d0752060 HB |
9329 | return 0; |
9330 | } | |
9331 | ||
9332 | int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu) | |
9333 | { | |
6a96bc7f CD |
9334 | struct fxregs_state *fxsave; |
9335 | ||
9336 | vcpu_load(vcpu); | |
9337 | ||
b666a4b6 | 9338 | fxsave = &vcpu->arch.guest_fpu->state.fxsave; |
d0752060 | 9339 | |
d0752060 HB |
9340 | memcpy(fxsave->st_space, fpu->fpr, 128); |
9341 | fxsave->cwd = fpu->fcw; | |
9342 | fxsave->swd = fpu->fsw; | |
9343 | fxsave->twd = fpu->ftwx; | |
9344 | fxsave->fop = fpu->last_opcode; | |
9345 | fxsave->rip = fpu->last_ip; | |
9346 | fxsave->rdp = fpu->last_dp; | |
0e96f31e | 9347 | memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space)); |
d0752060 | 9348 | |
6a96bc7f | 9349 | vcpu_put(vcpu); |
d0752060 HB |
9350 | return 0; |
9351 | } | |
9352 | ||
01643c51 KH |
9353 | static void store_regs(struct kvm_vcpu *vcpu) |
9354 | { | |
9355 | BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES); | |
9356 | ||
9357 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS) | |
9358 | __get_regs(vcpu, &vcpu->run->s.regs.regs); | |
9359 | ||
9360 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS) | |
9361 | __get_sregs(vcpu, &vcpu->run->s.regs.sregs); | |
9362 | ||
9363 | if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS) | |
9364 | kvm_vcpu_ioctl_x86_get_vcpu_events( | |
9365 | vcpu, &vcpu->run->s.regs.events); | |
9366 | } | |
9367 | ||
9368 | static int sync_regs(struct kvm_vcpu *vcpu) | |
9369 | { | |
9370 | if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS) | |
9371 | return -EINVAL; | |
9372 | ||
9373 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) { | |
9374 | __set_regs(vcpu, &vcpu->run->s.regs.regs); | |
9375 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS; | |
9376 | } | |
9377 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) { | |
9378 | if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs)) | |
9379 | return -EINVAL; | |
9380 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS; | |
9381 | } | |
9382 | if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) { | |
9383 | if (kvm_vcpu_ioctl_x86_set_vcpu_events( | |
9384 | vcpu, &vcpu->run->s.regs.events)) | |
9385 | return -EINVAL; | |
9386 | vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS; | |
9387 | } | |
9388 | ||
9389 | return 0; | |
9390 | } | |
9391 | ||
0ee6a517 | 9392 | static void fx_init(struct kvm_vcpu *vcpu) |
d0752060 | 9393 | { |
b666a4b6 | 9394 | fpstate_init(&vcpu->arch.guest_fpu->state); |
782511b0 | 9395 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
b666a4b6 | 9396 | vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv = |
df1daba7 | 9397 | host_xcr0 | XSTATE_COMPACTION_ENABLED; |
d0752060 | 9398 | |
2acf923e DC |
9399 | /* |
9400 | * Ensure guest xcr0 is valid for loading | |
9401 | */ | |
d91cab78 | 9402 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; |
2acf923e | 9403 | |
ad312c7c | 9404 | vcpu->arch.cr0 |= X86_CR0_ET; |
d0752060 | 9405 | } |
d0752060 | 9406 | |
897cc38e | 9407 | int kvm_arch_vcpu_precreate(struct kvm *kvm, unsigned int id) |
e9b11c17 | 9408 | { |
897cc38e SC |
9409 | if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0) |
9410 | pr_warn_once("kvm: SMP vm created on host with unstable TSC; " | |
9411 | "guest TSC will not be reliable\n"); | |
7f1ea208 | 9412 | |
897cc38e | 9413 | return 0; |
e9b11c17 ZX |
9414 | } |
9415 | ||
e529ef66 | 9416 | int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) |
e9b11c17 | 9417 | { |
95a0d01e SC |
9418 | struct page *page; |
9419 | int r; | |
c447e76b | 9420 | |
95a0d01e SC |
9421 | if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu)) |
9422 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; | |
9423 | else | |
9424 | vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED; | |
c447e76b | 9425 | |
95a0d01e | 9426 | kvm_set_tsc_khz(vcpu, max_tsc_khz); |
c447e76b | 9427 | |
95a0d01e SC |
9428 | r = kvm_mmu_create(vcpu); |
9429 | if (r < 0) | |
9430 | return r; | |
9431 | ||
9432 | if (irqchip_in_kernel(vcpu->kvm)) { | |
95a0d01e SC |
9433 | r = kvm_create_lapic(vcpu, lapic_timer_advance_ns); |
9434 | if (r < 0) | |
9435 | goto fail_mmu_destroy; | |
4e19c36f SS |
9436 | if (kvm_apicv_activated(vcpu->kvm)) |
9437 | vcpu->arch.apicv_active = true; | |
95a0d01e SC |
9438 | } else |
9439 | static_key_slow_inc(&kvm_no_apic_vcpu); | |
9440 | ||
9441 | r = -ENOMEM; | |
9442 | ||
9443 | page = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
9444 | if (!page) | |
9445 | goto fail_free_lapic; | |
9446 | vcpu->arch.pio_data = page_address(page); | |
9447 | ||
9448 | vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4, | |
9449 | GFP_KERNEL_ACCOUNT); | |
9450 | if (!vcpu->arch.mce_banks) | |
9451 | goto fail_free_pio_data; | |
9452 | vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS; | |
9453 | ||
9454 | if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, | |
9455 | GFP_KERNEL_ACCOUNT)) | |
9456 | goto fail_free_mce_banks; | |
9457 | ||
c9b8b07c SC |
9458 | if (!alloc_emulate_ctxt(vcpu)) |
9459 | goto free_wbinvd_dirty_mask; | |
9460 | ||
95a0d01e SC |
9461 | vcpu->arch.user_fpu = kmem_cache_zalloc(x86_fpu_cache, |
9462 | GFP_KERNEL_ACCOUNT); | |
9463 | if (!vcpu->arch.user_fpu) { | |
9464 | pr_err("kvm: failed to allocate userspace's fpu\n"); | |
c9b8b07c | 9465 | goto free_emulate_ctxt; |
95a0d01e SC |
9466 | } |
9467 | ||
9468 | vcpu->arch.guest_fpu = kmem_cache_zalloc(x86_fpu_cache, | |
9469 | GFP_KERNEL_ACCOUNT); | |
9470 | if (!vcpu->arch.guest_fpu) { | |
9471 | pr_err("kvm: failed to allocate vcpu's fpu\n"); | |
9472 | goto free_user_fpu; | |
9473 | } | |
9474 | fx_init(vcpu); | |
9475 | ||
95a0d01e | 9476 | vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu); |
7d2e8748 | 9477 | vcpu->arch.tdp_level = kvm_x86_ops.get_tdp_level(vcpu); |
95a0d01e SC |
9478 | |
9479 | vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT; | |
9480 | ||
9481 | kvm_async_pf_hash_reset(vcpu); | |
9482 | kvm_pmu_init(vcpu); | |
9483 | ||
9484 | vcpu->arch.pending_external_vector = -1; | |
9485 | vcpu->arch.preempted_in_kernel = false; | |
9486 | ||
9487 | kvm_hv_vcpu_init(vcpu); | |
9488 | ||
afaf0b2f | 9489 | r = kvm_x86_ops.vcpu_create(vcpu); |
95a0d01e SC |
9490 | if (r) |
9491 | goto free_guest_fpu; | |
e9b11c17 | 9492 | |
0cf9135b | 9493 | vcpu->arch.arch_capabilities = kvm_get_arch_capabilities(); |
e53d88af | 9494 | vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT; |
19efffa2 | 9495 | kvm_vcpu_mtrr_init(vcpu); |
ec7660cc | 9496 | vcpu_load(vcpu); |
d28bc9dd | 9497 | kvm_vcpu_reset(vcpu, false); |
e1732991 | 9498 | kvm_init_mmu(vcpu, false); |
e9b11c17 | 9499 | vcpu_put(vcpu); |
ec7660cc | 9500 | return 0; |
95a0d01e SC |
9501 | |
9502 | free_guest_fpu: | |
9503 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); | |
9504 | free_user_fpu: | |
9505 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); | |
c9b8b07c SC |
9506 | free_emulate_ctxt: |
9507 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); | |
95a0d01e SC |
9508 | free_wbinvd_dirty_mask: |
9509 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); | |
9510 | fail_free_mce_banks: | |
9511 | kfree(vcpu->arch.mce_banks); | |
9512 | fail_free_pio_data: | |
9513 | free_page((unsigned long)vcpu->arch.pio_data); | |
9514 | fail_free_lapic: | |
9515 | kvm_free_lapic(vcpu); | |
9516 | fail_mmu_destroy: | |
9517 | kvm_mmu_destroy(vcpu); | |
9518 | return r; | |
e9b11c17 ZX |
9519 | } |
9520 | ||
31928aa5 | 9521 | void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu) |
42897d86 | 9522 | { |
8fe8ab46 | 9523 | struct msr_data msr; |
332967a3 | 9524 | struct kvm *kvm = vcpu->kvm; |
42897d86 | 9525 | |
d3457c87 RK |
9526 | kvm_hv_vcpu_postcreate(vcpu); |
9527 | ||
ec7660cc | 9528 | if (mutex_lock_killable(&vcpu->mutex)) |
31928aa5 | 9529 | return; |
ec7660cc | 9530 | vcpu_load(vcpu); |
8fe8ab46 WA |
9531 | msr.data = 0x0; |
9532 | msr.index = MSR_IA32_TSC; | |
9533 | msr.host_initiated = true; | |
9534 | kvm_write_tsc(vcpu, &msr); | |
42897d86 | 9535 | vcpu_put(vcpu); |
2d5ba19b MT |
9536 | |
9537 | /* poll control enabled by default */ | |
9538 | vcpu->arch.msr_kvm_poll_control = 1; | |
9539 | ||
ec7660cc | 9540 | mutex_unlock(&vcpu->mutex); |
42897d86 | 9541 | |
b34de572 WL |
9542 | if (kvmclock_periodic_sync && vcpu->vcpu_idx == 0) |
9543 | schedule_delayed_work(&kvm->arch.kvmclock_sync_work, | |
9544 | KVMCLOCK_SYNC_PERIOD); | |
42897d86 MT |
9545 | } |
9546 | ||
d40ccc62 | 9547 | void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu) |
e9b11c17 | 9548 | { |
4cbc418a | 9549 | struct gfn_to_pfn_cache *cache = &vcpu->arch.st.cache; |
95a0d01e | 9550 | int idx; |
344d9588 | 9551 | |
4cbc418a PB |
9552 | kvm_release_pfn(cache->pfn, cache->dirty, cache); |
9553 | ||
50b143e1 | 9554 | kvmclock_reset(vcpu); |
e9b11c17 | 9555 | |
afaf0b2f | 9556 | kvm_x86_ops.vcpu_free(vcpu); |
50b143e1 | 9557 | |
c9b8b07c | 9558 | kmem_cache_free(x86_emulator_cache, vcpu->arch.emulate_ctxt); |
50b143e1 SC |
9559 | free_cpumask_var(vcpu->arch.wbinvd_dirty_mask); |
9560 | kmem_cache_free(x86_fpu_cache, vcpu->arch.user_fpu); | |
9561 | kmem_cache_free(x86_fpu_cache, vcpu->arch.guest_fpu); | |
95a0d01e SC |
9562 | |
9563 | kvm_hv_vcpu_uninit(vcpu); | |
9564 | kvm_pmu_destroy(vcpu); | |
9565 | kfree(vcpu->arch.mce_banks); | |
9566 | kvm_free_lapic(vcpu); | |
9567 | idx = srcu_read_lock(&vcpu->kvm->srcu); | |
9568 | kvm_mmu_destroy(vcpu); | |
9569 | srcu_read_unlock(&vcpu->kvm->srcu, idx); | |
9570 | free_page((unsigned long)vcpu->arch.pio_data); | |
9571 | if (!lapic_in_kernel(vcpu)) | |
9572 | static_key_slow_dec(&kvm_no_apic_vcpu); | |
e9b11c17 ZX |
9573 | } |
9574 | ||
d28bc9dd | 9575 | void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event) |
e9b11c17 | 9576 | { |
b7e31be3 RK |
9577 | kvm_lapic_reset(vcpu, init_event); |
9578 | ||
e69fab5d PB |
9579 | vcpu->arch.hflags = 0; |
9580 | ||
c43203ca | 9581 | vcpu->arch.smi_pending = 0; |
52797bf9 | 9582 | vcpu->arch.smi_count = 0; |
7460fb4a AK |
9583 | atomic_set(&vcpu->arch.nmi_queued, 0); |
9584 | vcpu->arch.nmi_pending = 0; | |
448fa4a9 | 9585 | vcpu->arch.nmi_injected = false; |
5f7552d4 NA |
9586 | kvm_clear_interrupt_queue(vcpu); |
9587 | kvm_clear_exception_queue(vcpu); | |
448fa4a9 | 9588 | |
42dbaa5a | 9589 | memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db)); |
ae561ede | 9590 | kvm_update_dr0123(vcpu); |
6f43ed01 | 9591 | vcpu->arch.dr6 = DR6_INIT; |
42dbaa5a | 9592 | vcpu->arch.dr7 = DR7_FIXED_1; |
c8639010 | 9593 | kvm_update_dr7(vcpu); |
42dbaa5a | 9594 | |
1119022c NA |
9595 | vcpu->arch.cr2 = 0; |
9596 | ||
3842d135 | 9597 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
2635b5c4 VK |
9598 | vcpu->arch.apf.msr_en_val = 0; |
9599 | vcpu->arch.apf.msr_int_val = 0; | |
c9aaa895 | 9600 | vcpu->arch.st.msr_val = 0; |
3842d135 | 9601 | |
12f9a48f GC |
9602 | kvmclock_reset(vcpu); |
9603 | ||
af585b92 GN |
9604 | kvm_clear_async_pf_completion_queue(vcpu); |
9605 | kvm_async_pf_hash_reset(vcpu); | |
9606 | vcpu->arch.apf.halted = false; | |
3842d135 | 9607 | |
a554d207 WL |
9608 | if (kvm_mpx_supported()) { |
9609 | void *mpx_state_buffer; | |
9610 | ||
9611 | /* | |
9612 | * To avoid have the INIT path from kvm_apic_has_events() that be | |
9613 | * called with loaded FPU and does not let userspace fix the state. | |
9614 | */ | |
f775b13e RR |
9615 | if (init_event) |
9616 | kvm_put_guest_fpu(vcpu); | |
b666a4b6 | 9617 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
abd16d68 | 9618 | XFEATURE_BNDREGS); |
a554d207 WL |
9619 | if (mpx_state_buffer) |
9620 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state)); | |
b666a4b6 | 9621 | mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave, |
abd16d68 | 9622 | XFEATURE_BNDCSR); |
a554d207 WL |
9623 | if (mpx_state_buffer) |
9624 | memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr)); | |
f775b13e RR |
9625 | if (init_event) |
9626 | kvm_load_guest_fpu(vcpu); | |
a554d207 WL |
9627 | } |
9628 | ||
64d60670 | 9629 | if (!init_event) { |
d28bc9dd | 9630 | kvm_pmu_reset(vcpu); |
64d60670 | 9631 | vcpu->arch.smbase = 0x30000; |
db2336a8 | 9632 | |
db2336a8 | 9633 | vcpu->arch.msr_misc_features_enables = 0; |
a554d207 WL |
9634 | |
9635 | vcpu->arch.xcr0 = XFEATURE_MASK_FP; | |
64d60670 | 9636 | } |
f5132b01 | 9637 | |
66f7b72e JS |
9638 | memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs)); |
9639 | vcpu->arch.regs_avail = ~0; | |
9640 | vcpu->arch.regs_dirty = ~0; | |
9641 | ||
a554d207 WL |
9642 | vcpu->arch.ia32_xss = 0; |
9643 | ||
afaf0b2f | 9644 | kvm_x86_ops.vcpu_reset(vcpu, init_event); |
e9b11c17 ZX |
9645 | } |
9646 | ||
2b4a273b | 9647 | void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector) |
66450a21 JK |
9648 | { |
9649 | struct kvm_segment cs; | |
9650 | ||
9651 | kvm_get_segment(vcpu, &cs, VCPU_SREG_CS); | |
9652 | cs.selector = vector << 8; | |
9653 | cs.base = vector << 12; | |
9654 | kvm_set_segment(vcpu, &cs, VCPU_SREG_CS); | |
9655 | kvm_rip_write(vcpu, 0); | |
e9b11c17 ZX |
9656 | } |
9657 | ||
13a34e06 | 9658 | int kvm_arch_hardware_enable(void) |
e9b11c17 | 9659 | { |
ca84d1a2 ZA |
9660 | struct kvm *kvm; |
9661 | struct kvm_vcpu *vcpu; | |
9662 | int i; | |
0dd6a6ed ZA |
9663 | int ret; |
9664 | u64 local_tsc; | |
9665 | u64 max_tsc = 0; | |
9666 | bool stable, backwards_tsc = false; | |
18863bdd AK |
9667 | |
9668 | kvm_shared_msr_cpu_online(); | |
afaf0b2f | 9669 | ret = kvm_x86_ops.hardware_enable(); |
0dd6a6ed ZA |
9670 | if (ret != 0) |
9671 | return ret; | |
9672 | ||
4ea1636b | 9673 | local_tsc = rdtsc(); |
b0c39dc6 | 9674 | stable = !kvm_check_tsc_unstable(); |
0dd6a6ed ZA |
9675 | list_for_each_entry(kvm, &vm_list, vm_list) { |
9676 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
9677 | if (!stable && vcpu->cpu == smp_processor_id()) | |
105b21bb | 9678 | kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
9679 | if (stable && vcpu->arch.last_host_tsc > local_tsc) { |
9680 | backwards_tsc = true; | |
9681 | if (vcpu->arch.last_host_tsc > max_tsc) | |
9682 | max_tsc = vcpu->arch.last_host_tsc; | |
9683 | } | |
9684 | } | |
9685 | } | |
9686 | ||
9687 | /* | |
9688 | * Sometimes, even reliable TSCs go backwards. This happens on | |
9689 | * platforms that reset TSC during suspend or hibernate actions, but | |
9690 | * maintain synchronization. We must compensate. Fortunately, we can | |
9691 | * detect that condition here, which happens early in CPU bringup, | |
9692 | * before any KVM threads can be running. Unfortunately, we can't | |
9693 | * bring the TSCs fully up to date with real time, as we aren't yet far | |
9694 | * enough into CPU bringup that we know how much real time has actually | |
9285ec4c | 9695 | * elapsed; our helper function, ktime_get_boottime_ns() will be using boot |
0dd6a6ed ZA |
9696 | * variables that haven't been updated yet. |
9697 | * | |
9698 | * So we simply find the maximum observed TSC above, then record the | |
9699 | * adjustment to TSC in each VCPU. When the VCPU later gets loaded, | |
9700 | * the adjustment will be applied. Note that we accumulate | |
9701 | * adjustments, in case multiple suspend cycles happen before some VCPU | |
9702 | * gets a chance to run again. In the event that no KVM threads get a | |
9703 | * chance to run, we will miss the entire elapsed period, as we'll have | |
9704 | * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may | |
9705 | * loose cycle time. This isn't too big a deal, since the loss will be | |
9706 | * uniform across all VCPUs (not to mention the scenario is extremely | |
9707 | * unlikely). It is possible that a second hibernate recovery happens | |
9708 | * much faster than a first, causing the observed TSC here to be | |
9709 | * smaller; this would require additional padding adjustment, which is | |
9710 | * why we set last_host_tsc to the local tsc observed here. | |
9711 | * | |
9712 | * N.B. - this code below runs only on platforms with reliable TSC, | |
9713 | * as that is the only way backwards_tsc is set above. Also note | |
9714 | * that this runs for ALL vcpus, which is not a bug; all VCPUs should | |
9715 | * have the same delta_cyc adjustment applied if backwards_tsc | |
9716 | * is detected. Note further, this adjustment is only done once, | |
9717 | * as we reset last_host_tsc on all VCPUs to stop this from being | |
9718 | * called multiple times (one for each physical CPU bringup). | |
9719 | * | |
4a969980 | 9720 | * Platforms with unreliable TSCs don't have to deal with this, they |
0dd6a6ed ZA |
9721 | * will be compensated by the logic in vcpu_load, which sets the TSC to |
9722 | * catchup mode. This will catchup all VCPUs to real time, but cannot | |
9723 | * guarantee that they stay in perfect synchronization. | |
9724 | */ | |
9725 | if (backwards_tsc) { | |
9726 | u64 delta_cyc = max_tsc - local_tsc; | |
9727 | list_for_each_entry(kvm, &vm_list, vm_list) { | |
a826faf1 | 9728 | kvm->arch.backwards_tsc_observed = true; |
0dd6a6ed ZA |
9729 | kvm_for_each_vcpu(i, vcpu, kvm) { |
9730 | vcpu->arch.tsc_offset_adjustment += delta_cyc; | |
9731 | vcpu->arch.last_host_tsc = local_tsc; | |
105b21bb | 9732 | kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu); |
0dd6a6ed ZA |
9733 | } |
9734 | ||
9735 | /* | |
9736 | * We have to disable TSC offset matching.. if you were | |
9737 | * booting a VM while issuing an S4 host suspend.... | |
9738 | * you may have some problem. Solving this issue is | |
9739 | * left as an exercise to the reader. | |
9740 | */ | |
9741 | kvm->arch.last_tsc_nsec = 0; | |
9742 | kvm->arch.last_tsc_write = 0; | |
9743 | } | |
9744 | ||
9745 | } | |
9746 | return 0; | |
e9b11c17 ZX |
9747 | } |
9748 | ||
13a34e06 | 9749 | void kvm_arch_hardware_disable(void) |
e9b11c17 | 9750 | { |
afaf0b2f | 9751 | kvm_x86_ops.hardware_disable(); |
13a34e06 | 9752 | drop_user_return_notifiers(); |
e9b11c17 ZX |
9753 | } |
9754 | ||
b9904085 | 9755 | int kvm_arch_hardware_setup(void *opaque) |
e9b11c17 | 9756 | { |
d008dfdb | 9757 | struct kvm_x86_init_ops *ops = opaque; |
9e9c3fe4 NA |
9758 | int r; |
9759 | ||
91661989 SC |
9760 | rdmsrl_safe(MSR_EFER, &host_efer); |
9761 | ||
408e9a31 PB |
9762 | if (boot_cpu_has(X86_FEATURE_XSAVES)) |
9763 | rdmsrl(MSR_IA32_XSS, host_xss); | |
9764 | ||
d008dfdb | 9765 | r = ops->hardware_setup(); |
9e9c3fe4 NA |
9766 | if (r != 0) |
9767 | return r; | |
9768 | ||
afaf0b2f | 9769 | memcpy(&kvm_x86_ops, ops->runtime_ops, sizeof(kvm_x86_ops)); |
69c6f69a | 9770 | |
408e9a31 PB |
9771 | if (!kvm_cpu_cap_has(X86_FEATURE_XSAVES)) |
9772 | supported_xss = 0; | |
9773 | ||
139f7425 PB |
9774 | #define __kvm_cpu_cap_has(UNUSED_, f) kvm_cpu_cap_has(f) |
9775 | cr4_reserved_bits = __cr4_reserved_bits(__kvm_cpu_cap_has, UNUSED_); | |
9776 | #undef __kvm_cpu_cap_has | |
b11306b5 | 9777 | |
35181e86 HZ |
9778 | if (kvm_has_tsc_control) { |
9779 | /* | |
9780 | * Make sure the user can only configure tsc_khz values that | |
9781 | * fit into a signed integer. | |
273ba457 | 9782 | * A min value is not calculated because it will always |
35181e86 HZ |
9783 | * be 1 on all machines. |
9784 | */ | |
9785 | u64 max = min(0x7fffffffULL, | |
9786 | __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz)); | |
9787 | kvm_max_guest_tsc_khz = max; | |
9788 | ||
ad721883 | 9789 | kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits; |
35181e86 | 9790 | } |
ad721883 | 9791 | |
9e9c3fe4 NA |
9792 | kvm_init_msr_list(); |
9793 | return 0; | |
e9b11c17 ZX |
9794 | } |
9795 | ||
9796 | void kvm_arch_hardware_unsetup(void) | |
9797 | { | |
afaf0b2f | 9798 | kvm_x86_ops.hardware_unsetup(); |
e9b11c17 ZX |
9799 | } |
9800 | ||
b9904085 | 9801 | int kvm_arch_check_processor_compat(void *opaque) |
e9b11c17 | 9802 | { |
f1cdecf5 | 9803 | struct cpuinfo_x86 *c = &cpu_data(smp_processor_id()); |
d008dfdb | 9804 | struct kvm_x86_init_ops *ops = opaque; |
f1cdecf5 SC |
9805 | |
9806 | WARN_ON(!irqs_disabled()); | |
9807 | ||
139f7425 PB |
9808 | if (__cr4_reserved_bits(cpu_has, c) != |
9809 | __cr4_reserved_bits(cpu_has, &boot_cpu_data)) | |
f1cdecf5 SC |
9810 | return -EIO; |
9811 | ||
d008dfdb | 9812 | return ops->check_processor_compatibility(); |
d71ba788 PB |
9813 | } |
9814 | ||
9815 | bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu) | |
9816 | { | |
9817 | return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id; | |
9818 | } | |
9819 | EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp); | |
9820 | ||
9821 | bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu) | |
9822 | { | |
9823 | return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0; | |
e9b11c17 ZX |
9824 | } |
9825 | ||
54e9818f | 9826 | struct static_key kvm_no_apic_vcpu __read_mostly; |
bce87cce | 9827 | EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu); |
54e9818f | 9828 | |
e790d9ef RK |
9829 | void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) |
9830 | { | |
b35e5548 LX |
9831 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
9832 | ||
c595ceee | 9833 | vcpu->arch.l1tf_flush_l1d = true; |
b35e5548 LX |
9834 | if (pmu->version && unlikely(pmu->event_count)) { |
9835 | pmu->need_cleanup = true; | |
9836 | kvm_make_request(KVM_REQ_PMU, vcpu); | |
9837 | } | |
afaf0b2f | 9838 | kvm_x86_ops.sched_in(vcpu, cpu); |
e790d9ef RK |
9839 | } |
9840 | ||
562b6b08 SC |
9841 | void kvm_arch_free_vm(struct kvm *kvm) |
9842 | { | |
9843 | kfree(kvm->arch.hyperv.hv_pa_pg); | |
9844 | vfree(kvm); | |
e790d9ef RK |
9845 | } |
9846 | ||
562b6b08 | 9847 | |
e08b9637 | 9848 | int kvm_arch_init_vm(struct kvm *kvm, unsigned long type) |
d19a9cd2 | 9849 | { |
e08b9637 CO |
9850 | if (type) |
9851 | return -EINVAL; | |
9852 | ||
6ef768fa | 9853 | INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list); |
f05e70ac | 9854 | INIT_LIST_HEAD(&kvm->arch.active_mmu_pages); |
10605204 | 9855 | INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages); |
1aa9b957 | 9856 | INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages); |
4d5c5d0f | 9857 | INIT_LIST_HEAD(&kvm->arch.assigned_dev_head); |
e0f0bbc5 | 9858 | atomic_set(&kvm->arch.noncoherent_dma_count, 0); |
d19a9cd2 | 9859 | |
5550af4d SY |
9860 | /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */ |
9861 | set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap); | |
7a84428a AW |
9862 | /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */ |
9863 | set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID, | |
9864 | &kvm->arch.irq_sources_bitmap); | |
5550af4d | 9865 | |
038f8c11 | 9866 | raw_spin_lock_init(&kvm->arch.tsc_write_lock); |
1e08ec4a | 9867 | mutex_init(&kvm->arch.apic_map_lock); |
d828199e MT |
9868 | spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock); |
9869 | ||
8171cd68 | 9870 | kvm->arch.kvmclock_offset = -get_kvmclock_base_ns(); |
d828199e | 9871 | pvclock_update_vm_gtod_copy(kvm); |
53f658b3 | 9872 | |
6fbbde9a DS |
9873 | kvm->arch.guest_can_read_msr_platform_info = true; |
9874 | ||
7e44e449 | 9875 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn); |
332967a3 | 9876 | INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn); |
7e44e449 | 9877 | |
cbc0236a | 9878 | kvm_hv_init_vm(kvm); |
0eb05bf2 | 9879 | kvm_page_track_init(kvm); |
13d268ca | 9880 | kvm_mmu_init_vm(kvm); |
0eb05bf2 | 9881 | |
afaf0b2f | 9882 | return kvm_x86_ops.vm_init(kvm); |
d19a9cd2 ZX |
9883 | } |
9884 | ||
1aa9b957 JS |
9885 | int kvm_arch_post_init_vm(struct kvm *kvm) |
9886 | { | |
9887 | return kvm_mmu_post_init_vm(kvm); | |
9888 | } | |
9889 | ||
d19a9cd2 ZX |
9890 | static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu) |
9891 | { | |
ec7660cc | 9892 | vcpu_load(vcpu); |
d19a9cd2 ZX |
9893 | kvm_mmu_unload(vcpu); |
9894 | vcpu_put(vcpu); | |
9895 | } | |
9896 | ||
9897 | static void kvm_free_vcpus(struct kvm *kvm) | |
9898 | { | |
9899 | unsigned int i; | |
988a2cae | 9900 | struct kvm_vcpu *vcpu; |
d19a9cd2 ZX |
9901 | |
9902 | /* | |
9903 | * Unpin any mmu pages first. | |
9904 | */ | |
af585b92 GN |
9905 | kvm_for_each_vcpu(i, vcpu, kvm) { |
9906 | kvm_clear_async_pf_completion_queue(vcpu); | |
988a2cae | 9907 | kvm_unload_vcpu_mmu(vcpu); |
af585b92 | 9908 | } |
988a2cae | 9909 | kvm_for_each_vcpu(i, vcpu, kvm) |
4543bdc0 | 9910 | kvm_vcpu_destroy(vcpu); |
988a2cae GN |
9911 | |
9912 | mutex_lock(&kvm->lock); | |
9913 | for (i = 0; i < atomic_read(&kvm->online_vcpus); i++) | |
9914 | kvm->vcpus[i] = NULL; | |
d19a9cd2 | 9915 | |
988a2cae GN |
9916 | atomic_set(&kvm->online_vcpus, 0); |
9917 | mutex_unlock(&kvm->lock); | |
d19a9cd2 ZX |
9918 | } |
9919 | ||
ad8ba2cd SY |
9920 | void kvm_arch_sync_events(struct kvm *kvm) |
9921 | { | |
332967a3 | 9922 | cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work); |
7e44e449 | 9923 | cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work); |
aea924f6 | 9924 | kvm_free_pit(kvm); |
ad8ba2cd SY |
9925 | } |
9926 | ||
1d8007bd | 9927 | int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size) |
9da0e4d5 PB |
9928 | { |
9929 | int i, r; | |
0577d1ab | 9930 | unsigned long hva, uninitialized_var(old_npages); |
f0d648bd | 9931 | struct kvm_memslots *slots = kvm_memslots(kvm); |
0577d1ab | 9932 | struct kvm_memory_slot *slot; |
9da0e4d5 PB |
9933 | |
9934 | /* Called with kvm->slots_lock held. */ | |
1d8007bd PB |
9935 | if (WARN_ON(id >= KVM_MEM_SLOTS_NUM)) |
9936 | return -EINVAL; | |
9da0e4d5 | 9937 | |
f0d648bd PB |
9938 | slot = id_to_memslot(slots, id); |
9939 | if (size) { | |
0577d1ab | 9940 | if (slot && slot->npages) |
f0d648bd PB |
9941 | return -EEXIST; |
9942 | ||
9943 | /* | |
9944 | * MAP_SHARED to prevent internal slot pages from being moved | |
9945 | * by fork()/COW. | |
9946 | */ | |
9947 | hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE, | |
9948 | MAP_SHARED | MAP_ANONYMOUS, 0); | |
9949 | if (IS_ERR((void *)hva)) | |
9950 | return PTR_ERR((void *)hva); | |
9951 | } else { | |
0577d1ab | 9952 | if (!slot || !slot->npages) |
f0d648bd PB |
9953 | return 0; |
9954 | ||
abbed4fa SC |
9955 | /* |
9956 | * Stuff a non-canonical value to catch use-after-delete. This | |
9957 | * ends up being 0 on 32-bit KVM, but there's no better | |
9958 | * alternative. | |
9959 | */ | |
9960 | hva = (unsigned long)(0xdeadull << 48); | |
0577d1ab | 9961 | old_npages = slot->npages; |
f0d648bd PB |
9962 | } |
9963 | ||
9da0e4d5 | 9964 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
1d8007bd | 9965 | struct kvm_userspace_memory_region m; |
9da0e4d5 | 9966 | |
1d8007bd PB |
9967 | m.slot = id | (i << 16); |
9968 | m.flags = 0; | |
9969 | m.guest_phys_addr = gpa; | |
f0d648bd | 9970 | m.userspace_addr = hva; |
1d8007bd | 9971 | m.memory_size = size; |
9da0e4d5 PB |
9972 | r = __kvm_set_memory_region(kvm, &m); |
9973 | if (r < 0) | |
9974 | return r; | |
9975 | } | |
9976 | ||
103c763c | 9977 | if (!size) |
0577d1ab | 9978 | vm_munmap(hva, old_npages * PAGE_SIZE); |
f0d648bd | 9979 | |
9da0e4d5 PB |
9980 | return 0; |
9981 | } | |
9982 | EXPORT_SYMBOL_GPL(__x86_set_memory_region); | |
9983 | ||
1aa9b957 JS |
9984 | void kvm_arch_pre_destroy_vm(struct kvm *kvm) |
9985 | { | |
9986 | kvm_mmu_pre_destroy_vm(kvm); | |
9987 | } | |
9988 | ||
d19a9cd2 ZX |
9989 | void kvm_arch_destroy_vm(struct kvm *kvm) |
9990 | { | |
27469d29 AH |
9991 | if (current->mm == kvm->mm) { |
9992 | /* | |
9993 | * Free memory regions allocated on behalf of userspace, | |
9994 | * unless the the memory map has changed due to process exit | |
9995 | * or fd copying. | |
9996 | */ | |
6a3c623b PX |
9997 | mutex_lock(&kvm->slots_lock); |
9998 | __x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, | |
9999 | 0, 0); | |
10000 | __x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, | |
10001 | 0, 0); | |
10002 | __x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0); | |
10003 | mutex_unlock(&kvm->slots_lock); | |
27469d29 | 10004 | } |
afaf0b2f SC |
10005 | if (kvm_x86_ops.vm_destroy) |
10006 | kvm_x86_ops.vm_destroy(kvm); | |
c761159c PX |
10007 | kvm_pic_destroy(kvm); |
10008 | kvm_ioapic_destroy(kvm); | |
d19a9cd2 | 10009 | kvm_free_vcpus(kvm); |
af1bae54 | 10010 | kvfree(rcu_dereference_check(kvm->arch.apic_map, 1)); |
66bb8a06 | 10011 | kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1)); |
13d268ca | 10012 | kvm_mmu_uninit_vm(kvm); |
2beb6dad | 10013 | kvm_page_track_cleanup(kvm); |
cbc0236a | 10014 | kvm_hv_destroy_vm(kvm); |
d19a9cd2 | 10015 | } |
0de10343 | 10016 | |
e96c81ee | 10017 | void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) |
db3fe4eb TY |
10018 | { |
10019 | int i; | |
10020 | ||
d89cc617 | 10021 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
e96c81ee SC |
10022 | kvfree(slot->arch.rmap[i]); |
10023 | slot->arch.rmap[i] = NULL; | |
10024 | ||
d89cc617 TY |
10025 | if (i == 0) |
10026 | continue; | |
10027 | ||
e96c81ee SC |
10028 | kvfree(slot->arch.lpage_info[i - 1]); |
10029 | slot->arch.lpage_info[i - 1] = NULL; | |
db3fe4eb | 10030 | } |
21ebbeda | 10031 | |
e96c81ee | 10032 | kvm_page_track_free_memslot(slot); |
db3fe4eb TY |
10033 | } |
10034 | ||
0dab98b7 SC |
10035 | static int kvm_alloc_memslot_metadata(struct kvm_memory_slot *slot, |
10036 | unsigned long npages) | |
db3fe4eb TY |
10037 | { |
10038 | int i; | |
10039 | ||
edd4fa37 SC |
10040 | /* |
10041 | * Clear out the previous array pointers for the KVM_MR_MOVE case. The | |
10042 | * old arrays will be freed by __kvm_set_memory_region() if installing | |
10043 | * the new memslot is successful. | |
10044 | */ | |
10045 | memset(&slot->arch, 0, sizeof(slot->arch)); | |
10046 | ||
d89cc617 | 10047 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
92f94f1e | 10048 | struct kvm_lpage_info *linfo; |
db3fe4eb TY |
10049 | unsigned long ugfn; |
10050 | int lpages; | |
d89cc617 | 10051 | int level = i + 1; |
db3fe4eb TY |
10052 | |
10053 | lpages = gfn_to_index(slot->base_gfn + npages - 1, | |
10054 | slot->base_gfn, level) + 1; | |
10055 | ||
d89cc617 | 10056 | slot->arch.rmap[i] = |
778e1cdd | 10057 | kvcalloc(lpages, sizeof(*slot->arch.rmap[i]), |
254272ce | 10058 | GFP_KERNEL_ACCOUNT); |
d89cc617 | 10059 | if (!slot->arch.rmap[i]) |
77d11309 | 10060 | goto out_free; |
d89cc617 TY |
10061 | if (i == 0) |
10062 | continue; | |
77d11309 | 10063 | |
254272ce | 10064 | linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT); |
92f94f1e | 10065 | if (!linfo) |
db3fe4eb TY |
10066 | goto out_free; |
10067 | ||
92f94f1e XG |
10068 | slot->arch.lpage_info[i - 1] = linfo; |
10069 | ||
db3fe4eb | 10070 | if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 10071 | linfo[0].disallow_lpage = 1; |
db3fe4eb | 10072 | if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1)) |
92f94f1e | 10073 | linfo[lpages - 1].disallow_lpage = 1; |
db3fe4eb TY |
10074 | ugfn = slot->userspace_addr >> PAGE_SHIFT; |
10075 | /* | |
10076 | * If the gfn and userspace address are not aligned wrt each | |
600087b6 | 10077 | * other, disable large page support for this slot. |
db3fe4eb | 10078 | */ |
600087b6 | 10079 | if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1)) { |
db3fe4eb TY |
10080 | unsigned long j; |
10081 | ||
10082 | for (j = 0; j < lpages; ++j) | |
92f94f1e | 10083 | linfo[j].disallow_lpage = 1; |
db3fe4eb TY |
10084 | } |
10085 | } | |
10086 | ||
21ebbeda XG |
10087 | if (kvm_page_track_create_memslot(slot, npages)) |
10088 | goto out_free; | |
10089 | ||
db3fe4eb TY |
10090 | return 0; |
10091 | ||
10092 | out_free: | |
d89cc617 | 10093 | for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) { |
548ef284 | 10094 | kvfree(slot->arch.rmap[i]); |
d89cc617 TY |
10095 | slot->arch.rmap[i] = NULL; |
10096 | if (i == 0) | |
10097 | continue; | |
10098 | ||
548ef284 | 10099 | kvfree(slot->arch.lpage_info[i - 1]); |
d89cc617 | 10100 | slot->arch.lpage_info[i - 1] = NULL; |
db3fe4eb TY |
10101 | } |
10102 | return -ENOMEM; | |
10103 | } | |
10104 | ||
15248258 | 10105 | void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen) |
e59dbe09 | 10106 | { |
91724814 BO |
10107 | struct kvm_vcpu *vcpu; |
10108 | int i; | |
10109 | ||
e6dff7d1 TY |
10110 | /* |
10111 | * memslots->generation has been incremented. | |
10112 | * mmio generation may have reached its maximum value. | |
10113 | */ | |
15248258 | 10114 | kvm_mmu_invalidate_mmio_sptes(kvm, gen); |
91724814 BO |
10115 | |
10116 | /* Force re-initialization of steal_time cache */ | |
10117 | kvm_for_each_vcpu(i, vcpu, kvm) | |
10118 | kvm_vcpu_kick(vcpu); | |
e59dbe09 TY |
10119 | } |
10120 | ||
f7784b8e MT |
10121 | int kvm_arch_prepare_memory_region(struct kvm *kvm, |
10122 | struct kvm_memory_slot *memslot, | |
09170a49 | 10123 | const struct kvm_userspace_memory_region *mem, |
7b6195a9 | 10124 | enum kvm_mr_change change) |
0de10343 | 10125 | { |
0dab98b7 SC |
10126 | if (change == KVM_MR_CREATE || change == KVM_MR_MOVE) |
10127 | return kvm_alloc_memslot_metadata(memslot, | |
10128 | mem->memory_size >> PAGE_SHIFT); | |
f7784b8e MT |
10129 | return 0; |
10130 | } | |
10131 | ||
88178fd4 | 10132 | static void kvm_mmu_slot_apply_flags(struct kvm *kvm, |
3741679b AY |
10133 | struct kvm_memory_slot *old, |
10134 | struct kvm_memory_slot *new, | |
10135 | enum kvm_mr_change change) | |
88178fd4 | 10136 | { |
3741679b AY |
10137 | /* |
10138 | * Nothing to do for RO slots or CREATE/MOVE/DELETE of a slot. | |
10139 | * See comments below. | |
10140 | */ | |
10141 | if ((change != KVM_MR_FLAGS_ONLY) || (new->flags & KVM_MEM_READONLY)) | |
88178fd4 | 10142 | return; |
88178fd4 KH |
10143 | |
10144 | /* | |
3741679b AY |
10145 | * Dirty logging tracks sptes in 4k granularity, meaning that large |
10146 | * sptes have to be split. If live migration is successful, the guest | |
10147 | * in the source machine will be destroyed and large sptes will be | |
10148 | * created in the destination. However, if the guest continues to run | |
10149 | * in the source machine (for example if live migration fails), small | |
10150 | * sptes will remain around and cause bad performance. | |
88178fd4 | 10151 | * |
3741679b AY |
10152 | * Scan sptes if dirty logging has been stopped, dropping those |
10153 | * which can be collapsed into a single large-page spte. Later | |
10154 | * page faults will create the large-page sptes. | |
88178fd4 | 10155 | * |
3741679b AY |
10156 | * There is no need to do this in any of the following cases: |
10157 | * CREATE: No dirty mappings will already exist. | |
10158 | * MOVE/DELETE: The old mappings will already have been cleaned up by | |
10159 | * kvm_arch_flush_shadow_memslot() | |
10160 | */ | |
10161 | if ((old->flags & KVM_MEM_LOG_DIRTY_PAGES) && | |
10162 | !(new->flags & KVM_MEM_LOG_DIRTY_PAGES)) | |
10163 | kvm_mmu_zap_collapsible_sptes(kvm, new); | |
10164 | ||
10165 | /* | |
10166 | * Enable or disable dirty logging for the slot. | |
88178fd4 | 10167 | * |
3741679b AY |
10168 | * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of the old |
10169 | * slot have been zapped so no dirty logging updates are needed for | |
10170 | * the old slot. | |
10171 | * For KVM_MR_CREATE and KVM_MR_MOVE, once the new slot is visible | |
10172 | * any mappings that might be created in it will consume the | |
10173 | * properties of the new slot and do not need to be updated here. | |
88178fd4 | 10174 | * |
3741679b AY |
10175 | * When PML is enabled, the kvm_x86_ops dirty logging hooks are |
10176 | * called to enable/disable dirty logging. | |
88178fd4 | 10177 | * |
3741679b AY |
10178 | * When disabling dirty logging with PML enabled, the D-bit is set |
10179 | * for sptes in the slot in order to prevent unnecessary GPA | |
10180 | * logging in the PML buffer (and potential PML buffer full VMEXIT). | |
10181 | * This guarantees leaving PML enabled for the guest's lifetime | |
10182 | * won't have any additional overhead from PML when the guest is | |
10183 | * running with dirty logging disabled. | |
88178fd4 | 10184 | * |
3741679b AY |
10185 | * When enabling dirty logging, large sptes are write-protected |
10186 | * so they can be split on first write. New large sptes cannot | |
10187 | * be created for this slot until the end of the logging. | |
88178fd4 | 10188 | * See the comments in fast_page_fault(). |
3741679b AY |
10189 | * For small sptes, nothing is done if the dirty log is in the |
10190 | * initial-all-set state. Otherwise, depending on whether pml | |
10191 | * is enabled the D-bit or the W-bit will be cleared. | |
88178fd4 KH |
10192 | */ |
10193 | if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) { | |
afaf0b2f SC |
10194 | if (kvm_x86_ops.slot_enable_log_dirty) { |
10195 | kvm_x86_ops.slot_enable_log_dirty(kvm, new); | |
3c9bd400 JZ |
10196 | } else { |
10197 | int level = | |
10198 | kvm_dirty_log_manual_protect_and_init_set(kvm) ? | |
3bae0459 | 10199 | PG_LEVEL_2M : PG_LEVEL_4K; |
3c9bd400 JZ |
10200 | |
10201 | /* | |
10202 | * If we're with initial-all-set, we don't need | |
10203 | * to write protect any small page because | |
10204 | * they're reported as dirty already. However | |
10205 | * we still need to write-protect huge pages | |
10206 | * so that the page split can happen lazily on | |
10207 | * the first write to the huge page. | |
10208 | */ | |
10209 | kvm_mmu_slot_remove_write_access(kvm, new, level); | |
10210 | } | |
88178fd4 | 10211 | } else { |
afaf0b2f SC |
10212 | if (kvm_x86_ops.slot_disable_log_dirty) |
10213 | kvm_x86_ops.slot_disable_log_dirty(kvm, new); | |
88178fd4 KH |
10214 | } |
10215 | } | |
10216 | ||
f7784b8e | 10217 | void kvm_arch_commit_memory_region(struct kvm *kvm, |
09170a49 | 10218 | const struct kvm_userspace_memory_region *mem, |
9d4c197c | 10219 | struct kvm_memory_slot *old, |
f36f3f28 | 10220 | const struct kvm_memory_slot *new, |
8482644a | 10221 | enum kvm_mr_change change) |
f7784b8e | 10222 | { |
48c0e4e9 | 10223 | if (!kvm->arch.n_requested_mmu_pages) |
4d66623c WY |
10224 | kvm_mmu_change_mmu_pages(kvm, |
10225 | kvm_mmu_calculate_default_mmu_pages(kvm)); | |
1c91cad4 | 10226 | |
3ea3b7fa | 10227 | /* |
f36f3f28 | 10228 | * FIXME: const-ify all uses of struct kvm_memory_slot. |
c972f3b1 | 10229 | */ |
3741679b | 10230 | kvm_mmu_slot_apply_flags(kvm, old, (struct kvm_memory_slot *) new, change); |
21198846 SC |
10231 | |
10232 | /* Free the arrays associated with the old memslot. */ | |
10233 | if (change == KVM_MR_MOVE) | |
e96c81ee | 10234 | kvm_arch_free_memslot(kvm, old); |
0de10343 | 10235 | } |
1d737c8a | 10236 | |
2df72e9b | 10237 | void kvm_arch_flush_shadow_all(struct kvm *kvm) |
34d4cb8f | 10238 | { |
7390de1e | 10239 | kvm_mmu_zap_all(kvm); |
34d4cb8f MT |
10240 | } |
10241 | ||
2df72e9b MT |
10242 | void kvm_arch_flush_shadow_memslot(struct kvm *kvm, |
10243 | struct kvm_memory_slot *slot) | |
10244 | { | |
ae7cd873 | 10245 | kvm_page_track_flush_slot(kvm, slot); |
2df72e9b MT |
10246 | } |
10247 | ||
e6c67d8c LA |
10248 | static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu) |
10249 | { | |
10250 | return (is_guest_mode(vcpu) && | |
afaf0b2f SC |
10251 | kvm_x86_ops.guest_apic_has_interrupt && |
10252 | kvm_x86_ops.guest_apic_has_interrupt(vcpu)); | |
e6c67d8c LA |
10253 | } |
10254 | ||
5d9bc648 PB |
10255 | static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu) |
10256 | { | |
10257 | if (!list_empty_careful(&vcpu->async_pf.done)) | |
10258 | return true; | |
10259 | ||
10260 | if (kvm_apic_has_events(vcpu)) | |
10261 | return true; | |
10262 | ||
10263 | if (vcpu->arch.pv.pv_unhalted) | |
10264 | return true; | |
10265 | ||
a5f01f8e WL |
10266 | if (vcpu->arch.exception.pending) |
10267 | return true; | |
10268 | ||
47a66eed Z |
10269 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || |
10270 | (vcpu->arch.nmi_pending && | |
c300ab9f | 10271 | kvm_x86_ops.nmi_allowed(vcpu, false))) |
5d9bc648 PB |
10272 | return true; |
10273 | ||
47a66eed | 10274 | if (kvm_test_request(KVM_REQ_SMI, vcpu) || |
a9fa7cb6 | 10275 | (vcpu->arch.smi_pending && |
c300ab9f | 10276 | kvm_x86_ops.smi_allowed(vcpu, false))) |
73917739 PB |
10277 | return true; |
10278 | ||
5d9bc648 | 10279 | if (kvm_arch_interrupt_allowed(vcpu) && |
e6c67d8c LA |
10280 | (kvm_cpu_has_interrupt(vcpu) || |
10281 | kvm_guest_apic_has_interrupt(vcpu))) | |
5d9bc648 PB |
10282 | return true; |
10283 | ||
1f4b34f8 AS |
10284 | if (kvm_hv_has_stimer_pending(vcpu)) |
10285 | return true; | |
10286 | ||
d2060bd4 SC |
10287 | if (is_guest_mode(vcpu) && |
10288 | kvm_x86_ops.nested_ops->hv_timer_pending && | |
10289 | kvm_x86_ops.nested_ops->hv_timer_pending(vcpu)) | |
10290 | return true; | |
10291 | ||
5d9bc648 PB |
10292 | return false; |
10293 | } | |
10294 | ||
1d737c8a ZX |
10295 | int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu) |
10296 | { | |
5d9bc648 | 10297 | return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu); |
1d737c8a | 10298 | } |
5736199a | 10299 | |
17e433b5 WL |
10300 | bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu) |
10301 | { | |
10302 | if (READ_ONCE(vcpu->arch.pv.pv_unhalted)) | |
10303 | return true; | |
10304 | ||
10305 | if (kvm_test_request(KVM_REQ_NMI, vcpu) || | |
10306 | kvm_test_request(KVM_REQ_SMI, vcpu) || | |
10307 | kvm_test_request(KVM_REQ_EVENT, vcpu)) | |
10308 | return true; | |
10309 | ||
afaf0b2f | 10310 | if (vcpu->arch.apicv_active && kvm_x86_ops.dy_apicv_has_pending_interrupt(vcpu)) |
17e433b5 WL |
10311 | return true; |
10312 | ||
10313 | return false; | |
10314 | } | |
10315 | ||
199b5763 LM |
10316 | bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu) |
10317 | { | |
de63ad4c | 10318 | return vcpu->arch.preempted_in_kernel; |
199b5763 LM |
10319 | } |
10320 | ||
b6d33834 | 10321 | int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu) |
5736199a | 10322 | { |
b6d33834 | 10323 | return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE; |
5736199a | 10324 | } |
78646121 GN |
10325 | |
10326 | int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu) | |
10327 | { | |
c300ab9f | 10328 | return kvm_x86_ops.interrupt_allowed(vcpu, false); |
78646121 | 10329 | } |
229456fc | 10330 | |
82b32774 | 10331 | unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu) |
f92653ee | 10332 | { |
82b32774 NA |
10333 | if (is_64_bit_mode(vcpu)) |
10334 | return kvm_rip_read(vcpu); | |
10335 | return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) + | |
10336 | kvm_rip_read(vcpu)); | |
10337 | } | |
10338 | EXPORT_SYMBOL_GPL(kvm_get_linear_rip); | |
f92653ee | 10339 | |
82b32774 NA |
10340 | bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip) |
10341 | { | |
10342 | return kvm_get_linear_rip(vcpu) == linear_rip; | |
f92653ee JK |
10343 | } |
10344 | EXPORT_SYMBOL_GPL(kvm_is_linear_rip); | |
10345 | ||
94fe45da JK |
10346 | unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu) |
10347 | { | |
10348 | unsigned long rflags; | |
10349 | ||
afaf0b2f | 10350 | rflags = kvm_x86_ops.get_rflags(vcpu); |
94fe45da | 10351 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) |
c310bac5 | 10352 | rflags &= ~X86_EFLAGS_TF; |
94fe45da JK |
10353 | return rflags; |
10354 | } | |
10355 | EXPORT_SYMBOL_GPL(kvm_get_rflags); | |
10356 | ||
6addfc42 | 10357 | static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) |
94fe45da JK |
10358 | { |
10359 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP && | |
f92653ee | 10360 | kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip)) |
c310bac5 | 10361 | rflags |= X86_EFLAGS_TF; |
afaf0b2f | 10362 | kvm_x86_ops.set_rflags(vcpu, rflags); |
6addfc42 PB |
10363 | } |
10364 | ||
10365 | void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
10366 | { | |
10367 | __kvm_set_rflags(vcpu, rflags); | |
3842d135 | 10368 | kvm_make_request(KVM_REQ_EVENT, vcpu); |
94fe45da JK |
10369 | } |
10370 | EXPORT_SYMBOL_GPL(kvm_set_rflags); | |
10371 | ||
56028d08 GN |
10372 | void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work) |
10373 | { | |
10374 | int r; | |
10375 | ||
44dd3ffa | 10376 | if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) || |
f2e10669 | 10377 | work->wakeup_all) |
56028d08 GN |
10378 | return; |
10379 | ||
10380 | r = kvm_mmu_reload(vcpu); | |
10381 | if (unlikely(r)) | |
10382 | return; | |
10383 | ||
44dd3ffa | 10384 | if (!vcpu->arch.mmu->direct_map && |
d8dd54e0 | 10385 | work->arch.cr3 != vcpu->arch.mmu->get_guest_pgd(vcpu)) |
fb67e14f XG |
10386 | return; |
10387 | ||
7a02674d | 10388 | kvm_mmu_do_page_fault(vcpu, work->cr2_or_gpa, 0, true); |
56028d08 GN |
10389 | } |
10390 | ||
af585b92 GN |
10391 | static inline u32 kvm_async_pf_hash_fn(gfn_t gfn) |
10392 | { | |
dd03bcaa PX |
10393 | BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU)); |
10394 | ||
af585b92 GN |
10395 | return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU)); |
10396 | } | |
10397 | ||
10398 | static inline u32 kvm_async_pf_next_probe(u32 key) | |
10399 | { | |
dd03bcaa | 10400 | return (key + 1) & (ASYNC_PF_PER_VCPU - 1); |
af585b92 GN |
10401 | } |
10402 | ||
10403 | static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10404 | { | |
10405 | u32 key = kvm_async_pf_hash_fn(gfn); | |
10406 | ||
10407 | while (vcpu->arch.apf.gfns[key] != ~0) | |
10408 | key = kvm_async_pf_next_probe(key); | |
10409 | ||
10410 | vcpu->arch.apf.gfns[key] = gfn; | |
10411 | } | |
10412 | ||
10413 | static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10414 | { | |
10415 | int i; | |
10416 | u32 key = kvm_async_pf_hash_fn(gfn); | |
10417 | ||
dd03bcaa | 10418 | for (i = 0; i < ASYNC_PF_PER_VCPU && |
c7d28c24 XG |
10419 | (vcpu->arch.apf.gfns[key] != gfn && |
10420 | vcpu->arch.apf.gfns[key] != ~0); i++) | |
af585b92 GN |
10421 | key = kvm_async_pf_next_probe(key); |
10422 | ||
10423 | return key; | |
10424 | } | |
10425 | ||
10426 | bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10427 | { | |
10428 | return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn; | |
10429 | } | |
10430 | ||
10431 | static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn) | |
10432 | { | |
10433 | u32 i, j, k; | |
10434 | ||
10435 | i = j = kvm_async_pf_gfn_slot(vcpu, gfn); | |
0fd46044 PX |
10436 | |
10437 | if (WARN_ON_ONCE(vcpu->arch.apf.gfns[i] != gfn)) | |
10438 | return; | |
10439 | ||
af585b92 GN |
10440 | while (true) { |
10441 | vcpu->arch.apf.gfns[i] = ~0; | |
10442 | do { | |
10443 | j = kvm_async_pf_next_probe(j); | |
10444 | if (vcpu->arch.apf.gfns[j] == ~0) | |
10445 | return; | |
10446 | k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]); | |
10447 | /* | |
10448 | * k lies cyclically in ]i,j] | |
10449 | * | i.k.j | | |
10450 | * |....j i.k.| or |.k..j i...| | |
10451 | */ | |
10452 | } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j)); | |
10453 | vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j]; | |
10454 | i = j; | |
10455 | } | |
10456 | } | |
10457 | ||
68fd66f1 | 10458 | static inline int apf_put_user_notpresent(struct kvm_vcpu *vcpu) |
7c90705b | 10459 | { |
68fd66f1 VK |
10460 | u32 reason = KVM_PV_REASON_PAGE_NOT_PRESENT; |
10461 | ||
10462 | return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &reason, | |
10463 | sizeof(reason)); | |
10464 | } | |
10465 | ||
10466 | static inline int apf_put_user_ready(struct kvm_vcpu *vcpu, u32 token) | |
10467 | { | |
2635b5c4 | 10468 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); |
4e335d9e | 10469 | |
2635b5c4 VK |
10470 | return kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, |
10471 | &token, offset, sizeof(token)); | |
10472 | } | |
10473 | ||
10474 | static inline bool apf_pageready_slot_free(struct kvm_vcpu *vcpu) | |
10475 | { | |
10476 | unsigned int offset = offsetof(struct kvm_vcpu_pv_apf_data, token); | |
10477 | u32 val; | |
10478 | ||
10479 | if (kvm_read_guest_offset_cached(vcpu->kvm, &vcpu->arch.apf.data, | |
10480 | &val, offset, sizeof(val))) | |
10481 | return false; | |
10482 | ||
10483 | return !val; | |
7c90705b GN |
10484 | } |
10485 | ||
1dfdb45e PB |
10486 | static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu) |
10487 | { | |
10488 | if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu)) | |
10489 | return false; | |
10490 | ||
2635b5c4 VK |
10491 | if (!kvm_pv_async_pf_enabled(vcpu) || |
10492 | (vcpu->arch.apf.send_user_only && kvm_x86_ops.get_cpl(vcpu) == 0)) | |
1dfdb45e PB |
10493 | return false; |
10494 | ||
10495 | return true; | |
10496 | } | |
10497 | ||
10498 | bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu) | |
10499 | { | |
10500 | if (unlikely(!lapic_in_kernel(vcpu) || | |
10501 | kvm_event_needs_reinjection(vcpu) || | |
10502 | vcpu->arch.exception.pending)) | |
10503 | return false; | |
10504 | ||
10505 | if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu)) | |
10506 | return false; | |
10507 | ||
10508 | /* | |
10509 | * If interrupts are off we cannot even use an artificial | |
10510 | * halt state. | |
10511 | */ | |
c300ab9f | 10512 | return kvm_arch_interrupt_allowed(vcpu); |
1dfdb45e PB |
10513 | } |
10514 | ||
af585b92 GN |
10515 | void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu, |
10516 | struct kvm_async_pf *work) | |
10517 | { | |
6389ee94 AK |
10518 | struct x86_exception fault; |
10519 | ||
736c291c | 10520 | trace_kvm_async_pf_not_present(work->arch.token, work->cr2_or_gpa); |
af585b92 | 10521 | kvm_add_async_pf_gfn(vcpu, work->arch.gfn); |
7c90705b | 10522 | |
1dfdb45e | 10523 | if (kvm_can_deliver_async_pf(vcpu) && |
68fd66f1 | 10524 | !apf_put_user_notpresent(vcpu)) { |
6389ee94 AK |
10525 | fault.vector = PF_VECTOR; |
10526 | fault.error_code_valid = true; | |
10527 | fault.error_code = 0; | |
10528 | fault.nested_page_fault = false; | |
10529 | fault.address = work->arch.token; | |
adfe20fb | 10530 | fault.async_page_fault = true; |
6389ee94 | 10531 | kvm_inject_page_fault(vcpu, &fault); |
1dfdb45e PB |
10532 | } else { |
10533 | /* | |
10534 | * It is not possible to deliver a paravirtualized asynchronous | |
10535 | * page fault, but putting the guest in an artificial halt state | |
10536 | * can be beneficial nevertheless: if an interrupt arrives, we | |
10537 | * can deliver it timely and perhaps the guest will schedule | |
10538 | * another process. When the instruction that triggered a page | |
10539 | * fault is retried, hopefully the page will be ready in the host. | |
10540 | */ | |
10541 | kvm_make_request(KVM_REQ_APF_HALT, vcpu); | |
7c90705b | 10542 | } |
af585b92 GN |
10543 | } |
10544 | ||
10545 | void kvm_arch_async_page_present(struct kvm_vcpu *vcpu, | |
10546 | struct kvm_async_pf *work) | |
10547 | { | |
2635b5c4 VK |
10548 | struct kvm_lapic_irq irq = { |
10549 | .delivery_mode = APIC_DM_FIXED, | |
10550 | .vector = vcpu->arch.apf.vec | |
10551 | }; | |
6389ee94 | 10552 | |
f2e10669 | 10553 | if (work->wakeup_all) |
7c90705b GN |
10554 | work->arch.token = ~0; /* broadcast wakeup */ |
10555 | else | |
10556 | kvm_del_async_pf_gfn(vcpu, work->arch.gfn); | |
736c291c | 10557 | trace_kvm_async_pf_ready(work->arch.token, work->cr2_or_gpa); |
7c90705b | 10558 | |
2635b5c4 | 10559 | if (kvm_pv_async_pf_enabled(vcpu) && |
557a961a VK |
10560 | !apf_put_user_ready(vcpu, work->arch.token)) { |
10561 | vcpu->arch.apf.pageready_pending = true; | |
2635b5c4 | 10562 | kvm_apic_set_irq(vcpu, &irq, NULL); |
557a961a | 10563 | } |
2635b5c4 | 10564 | |
e6d53e3b | 10565 | vcpu->arch.apf.halted = false; |
a4fa1635 | 10566 | vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE; |
7c90705b GN |
10567 | } |
10568 | ||
557a961a VK |
10569 | void kvm_arch_async_page_present_queued(struct kvm_vcpu *vcpu) |
10570 | { | |
10571 | kvm_make_request(KVM_REQ_APF_READY, vcpu); | |
10572 | if (!vcpu->arch.apf.pageready_pending) | |
10573 | kvm_vcpu_kick(vcpu); | |
10574 | } | |
10575 | ||
7c0ade6c | 10576 | bool kvm_arch_can_dequeue_async_page_present(struct kvm_vcpu *vcpu) |
7c90705b | 10577 | { |
2635b5c4 | 10578 | if (!kvm_pv_async_pf_enabled(vcpu)) |
7c90705b GN |
10579 | return true; |
10580 | else | |
2635b5c4 | 10581 | return apf_pageready_slot_free(vcpu); |
af585b92 GN |
10582 | } |
10583 | ||
5544eb9b PB |
10584 | void kvm_arch_start_assignment(struct kvm *kvm) |
10585 | { | |
10586 | atomic_inc(&kvm->arch.assigned_device_count); | |
10587 | } | |
10588 | EXPORT_SYMBOL_GPL(kvm_arch_start_assignment); | |
10589 | ||
10590 | void kvm_arch_end_assignment(struct kvm *kvm) | |
10591 | { | |
10592 | atomic_dec(&kvm->arch.assigned_device_count); | |
10593 | } | |
10594 | EXPORT_SYMBOL_GPL(kvm_arch_end_assignment); | |
10595 | ||
10596 | bool kvm_arch_has_assigned_device(struct kvm *kvm) | |
10597 | { | |
10598 | return atomic_read(&kvm->arch.assigned_device_count); | |
10599 | } | |
10600 | EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device); | |
10601 | ||
e0f0bbc5 AW |
10602 | void kvm_arch_register_noncoherent_dma(struct kvm *kvm) |
10603 | { | |
10604 | atomic_inc(&kvm->arch.noncoherent_dma_count); | |
10605 | } | |
10606 | EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma); | |
10607 | ||
10608 | void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm) | |
10609 | { | |
10610 | atomic_dec(&kvm->arch.noncoherent_dma_count); | |
10611 | } | |
10612 | EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma); | |
10613 | ||
10614 | bool kvm_arch_has_noncoherent_dma(struct kvm *kvm) | |
10615 | { | |
10616 | return atomic_read(&kvm->arch.noncoherent_dma_count); | |
10617 | } | |
10618 | EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma); | |
10619 | ||
14717e20 AW |
10620 | bool kvm_arch_has_irq_bypass(void) |
10621 | { | |
92735b1b | 10622 | return true; |
14717e20 AW |
10623 | } |
10624 | ||
87276880 FW |
10625 | int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons, |
10626 | struct irq_bypass_producer *prod) | |
10627 | { | |
10628 | struct kvm_kernel_irqfd *irqfd = | |
10629 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
10630 | ||
14717e20 | 10631 | irqfd->producer = prod; |
87276880 | 10632 | |
afaf0b2f | 10633 | return kvm_x86_ops.update_pi_irte(irqfd->kvm, |
14717e20 | 10634 | prod->irq, irqfd->gsi, 1); |
87276880 FW |
10635 | } |
10636 | ||
10637 | void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons, | |
10638 | struct irq_bypass_producer *prod) | |
10639 | { | |
10640 | int ret; | |
10641 | struct kvm_kernel_irqfd *irqfd = | |
10642 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
10643 | ||
87276880 FW |
10644 | WARN_ON(irqfd->producer != prod); |
10645 | irqfd->producer = NULL; | |
10646 | ||
10647 | /* | |
10648 | * When producer of consumer is unregistered, we change back to | |
10649 | * remapped mode, so we can re-use the current implementation | |
bb3541f1 | 10650 | * when the irq is masked/disabled or the consumer side (KVM |
87276880 FW |
10651 | * int this case doesn't want to receive the interrupts. |
10652 | */ | |
afaf0b2f | 10653 | ret = kvm_x86_ops.update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0); |
87276880 FW |
10654 | if (ret) |
10655 | printk(KERN_INFO "irq bypass consumer (token %p) unregistration" | |
10656 | " fails: %d\n", irqfd->consumer.token, ret); | |
10657 | } | |
10658 | ||
10659 | int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq, | |
10660 | uint32_t guest_irq, bool set) | |
10661 | { | |
afaf0b2f | 10662 | return kvm_x86_ops.update_pi_irte(kvm, host_irq, guest_irq, set); |
87276880 FW |
10663 | } |
10664 | ||
52004014 FW |
10665 | bool kvm_vector_hashing_enabled(void) |
10666 | { | |
10667 | return vector_hashing; | |
10668 | } | |
52004014 | 10669 | |
2d5ba19b MT |
10670 | bool kvm_arch_no_poll(struct kvm_vcpu *vcpu) |
10671 | { | |
10672 | return (vcpu->arch.msr_kvm_poll_control & 1) == 0; | |
10673 | } | |
10674 | EXPORT_SYMBOL_GPL(kvm_arch_no_poll); | |
10675 | ||
6441fa61 PB |
10676 | u64 kvm_spec_ctrl_valid_bits(struct kvm_vcpu *vcpu) |
10677 | { | |
10678 | uint64_t bits = SPEC_CTRL_IBRS | SPEC_CTRL_STIBP | SPEC_CTRL_SSBD; | |
10679 | ||
10680 | /* The STIBP bit doesn't fault even if it's not advertised */ | |
10681 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL) && | |
10682 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_IBRS)) | |
10683 | bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP); | |
10684 | if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL) && | |
10685 | !boot_cpu_has(X86_FEATURE_AMD_IBRS)) | |
10686 | bits &= ~(SPEC_CTRL_IBRS | SPEC_CTRL_STIBP); | |
10687 | ||
10688 | if (!guest_cpuid_has(vcpu, X86_FEATURE_SPEC_CTRL_SSBD) && | |
10689 | !guest_cpuid_has(vcpu, X86_FEATURE_AMD_SSBD)) | |
10690 | bits &= ~SPEC_CTRL_SSBD; | |
10691 | if (!boot_cpu_has(X86_FEATURE_SPEC_CTRL_SSBD) && | |
10692 | !boot_cpu_has(X86_FEATURE_AMD_SSBD)) | |
10693 | bits &= ~SPEC_CTRL_SSBD; | |
10694 | ||
10695 | return bits; | |
10696 | } | |
10697 | EXPORT_SYMBOL_GPL(kvm_spec_ctrl_valid_bits); | |
2d5ba19b | 10698 | |
229456fc | 10699 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit); |
931c33b1 | 10700 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio); |
229456fc MT |
10701 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq); |
10702 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault); | |
10703 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr); | |
10704 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr); | |
0ac406de | 10705 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun); |
d8cabddf | 10706 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit); |
17897f36 | 10707 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject); |
236649de | 10708 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit); |
5497b955 | 10709 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed); |
ec1ff790 | 10710 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga); |
532a46b9 | 10711 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit); |
2e554e8d | 10712 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts); |
489223ed | 10713 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset); |
4f75bcc3 | 10714 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update); |
843e4330 | 10715 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full); |
efc64404 | 10716 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update); |
18f40c53 SS |
10717 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access); |
10718 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi); | |
ab56f8e6 | 10719 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_ga_log); |
24bbf74c | 10720 | EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_apicv_update_request); |