]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/x86/kvm/x86.c
KVM: X86: Move irrelevant declarations out of ioapic.h
[mirror_ubuntu-hirsute-kernel.git] / arch / x86 / kvm / x86.c
CommitLineData
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"
1d737c8a 21#include "mmu.h"
7837699f 22#include "i8254.h"
37817f29 23#include "tss.h"
5fdbf976 24#include "kvm_cache_regs.h"
26eef70c 25#include "x86.h"
00b27a3e 26#include "cpuid.h"
474a5bb9 27#include "pmu.h"
e83d5887 28#include "hyperv.h"
313a3dc7 29
18068523 30#include <linux/clocksource.h>
4d5c5d0f 31#include <linux/interrupt.h>
313a3dc7
CO
32#include <linux/kvm.h>
33#include <linux/fs.h>
34#include <linux/vmalloc.h>
1767e931
PG
35#include <linux/export.h>
36#include <linux/moduleparam.h>
0de10343 37#include <linux/mman.h>
2bacc55c 38#include <linux/highmem.h>
19de40a8 39#include <linux/iommu.h>
62c476c7 40#include <linux/intel-iommu.h>
c8076604 41#include <linux/cpufreq.h>
18863bdd 42#include <linux/user-return-notifier.h>
a983fb23 43#include <linux/srcu.h>
5a0e3ad6 44#include <linux/slab.h>
ff9d07a0 45#include <linux/perf_event.h>
7bee342a 46#include <linux/uaccess.h>
af585b92 47#include <linux/hash.h>
a1b60c1c 48#include <linux/pci.h>
16e8d74d
MT
49#include <linux/timekeeper_internal.h>
50#include <linux/pvclock_gtod.h>
87276880
FW
51#include <linux/kvm_irqfd.h>
52#include <linux/irqbypass.h>
3905f9ad 53#include <linux/sched/stat.h>
0c5f81da 54#include <linux/sched/isolation.h>
d0ec49d4 55#include <linux/mem_encrypt.h>
3905f9ad 56
aec51dc4 57#include <trace/events/kvm.h>
2ed152af 58
24f1e32c 59#include <asm/debugreg.h>
d825ed0a 60#include <asm/msr.h>
a5f61300 61#include <asm/desc.h>
890ca9ae 62#include <asm/mce.h>
f89e32e0 63#include <linux/kernel_stat.h>
78f7f1e5 64#include <asm/fpu/internal.h> /* Ugh! */
1d5f066e 65#include <asm/pvclock.h>
217fc9cf 66#include <asm/div64.h>
efc64404 67#include <asm/irq_remapping.h>
b0c39dc6 68#include <asm/mshyperv.h>
0092e434 69#include <asm/hypervisor.h>
bf8c55d8 70#include <asm/intel_pt.h>
b3dc0695 71#include <asm/emulate_prefix.h>
dd2cb348 72#include <clocksource/hyperv_timer.h>
043405e1 73
d1898b73
DH
74#define CREATE_TRACE_POINTS
75#include "trace.h"
76
313a3dc7 77#define MAX_IO_MSRS 256
890ca9ae 78#define KVM_MAX_MCE_BANKS 32
c45dcc71
AR
79u64 __read_mostly kvm_mce_cap_supported = MCG_CTL_P | MCG_SER_P;
80EXPORT_SYMBOL_GPL(kvm_mce_cap_supported);
890ca9ae 81
0f65dd70
AK
82#define emul_to_vcpu(ctxt) \
83 container_of(ctxt, struct kvm_vcpu, arch.emulate_ctxt)
84
50a37eb4
JR
85/* EFER defaults:
86 * - enable syscall per default because its emulated by KVM
87 * - enable LME and LMA per default on 64 bit KVM
88 */
89#ifdef CONFIG_X86_64
1260edbe
LJ
90static
91u64 __read_mostly efer_reserved_bits = ~((u64)(EFER_SCE | EFER_LME | EFER_LMA));
50a37eb4 92#else
1260edbe 93static u64 __read_mostly efer_reserved_bits = ~((u64)EFER_SCE);
50a37eb4 94#endif
313a3dc7 95
833b45de
PB
96#define VM_STAT(x, ...) offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__
97#define VCPU_STAT(x, ...) offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__
417bc304 98
c519265f
RK
99#define KVM_X2APIC_API_VALID_FLAGS (KVM_X2APIC_API_USE_32BIT_IDS | \
100 KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
37131313 101
cb142eb7 102static void update_cr8_intercept(struct kvm_vcpu *vcpu);
7460fb4a 103static void process_nmi(struct kvm_vcpu *vcpu);
ee2cd4b7 104static void enter_smm(struct kvm_vcpu *vcpu);
6addfc42 105static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
01643c51
KH
106static void store_regs(struct kvm_vcpu *vcpu);
107static int sync_regs(struct kvm_vcpu *vcpu);
674eea0f 108
893590c7 109struct kvm_x86_ops *kvm_x86_ops __read_mostly;
5fdbf976 110EXPORT_SYMBOL_GPL(kvm_x86_ops);
97896d04 111
893590c7 112static bool __read_mostly ignore_msrs = 0;
476bc001 113module_param(ignore_msrs, bool, S_IRUGO | S_IWUSR);
ed85c068 114
fab0aa3b
EM
115static bool __read_mostly report_ignored_msrs = true;
116module_param(report_ignored_msrs, bool, S_IRUGO | S_IWUSR);
117
4c27625b 118unsigned int min_timer_period_us = 200;
9ed96e87
MT
119module_param(min_timer_period_us, uint, S_IRUGO | S_IWUSR);
120
630994b3
MT
121static bool __read_mostly kvmclock_periodic_sync = true;
122module_param(kvmclock_periodic_sync, bool, S_IRUGO);
123
893590c7 124bool __read_mostly kvm_has_tsc_control;
92a1f12d 125EXPORT_SYMBOL_GPL(kvm_has_tsc_control);
893590c7 126u32 __read_mostly kvm_max_guest_tsc_khz;
92a1f12d 127EXPORT_SYMBOL_GPL(kvm_max_guest_tsc_khz);
bc9b961b
HZ
128u8 __read_mostly kvm_tsc_scaling_ratio_frac_bits;
129EXPORT_SYMBOL_GPL(kvm_tsc_scaling_ratio_frac_bits);
130u64 __read_mostly kvm_max_tsc_scaling_ratio;
131EXPORT_SYMBOL_GPL(kvm_max_tsc_scaling_ratio);
64672c95
YJ
132u64 __read_mostly kvm_default_tsc_scaling_ratio;
133EXPORT_SYMBOL_GPL(kvm_default_tsc_scaling_ratio);
92a1f12d 134
cc578287 135/* tsc tolerance in parts per million - default to 1/2 of the NTP threshold */
893590c7 136static u32 __read_mostly tsc_tolerance_ppm = 250;
cc578287
ZA
137module_param(tsc_tolerance_ppm, uint, S_IRUGO | S_IWUSR);
138
c3941d9e
SC
139/*
140 * lapic timer advance (tscdeadline mode only) in nanoseconds. '-1' enables
141 * adaptive tuning starting from default advancment of 1000ns. '0' disables
142 * advancement entirely. Any other value is used as-is and disables adaptive
143 * tuning, i.e. allows priveleged userspace to set an exact advancement time.
144 */
145static int __read_mostly lapic_timer_advance_ns = -1;
0e6edceb 146module_param(lapic_timer_advance_ns, int, S_IRUGO | S_IWUSR);
d0659d94 147
52004014
FW
148static bool __read_mostly vector_hashing = true;
149module_param(vector_hashing, bool, S_IRUGO);
150
c4ae60e4
LA
151bool __read_mostly enable_vmware_backdoor = false;
152module_param(enable_vmware_backdoor, bool, S_IRUGO);
153EXPORT_SYMBOL_GPL(enable_vmware_backdoor);
154
6c86eedc
WL
155static bool __read_mostly force_emulation_prefix = false;
156module_param(force_emulation_prefix, bool, S_IRUGO);
157
0c5f81da
WL
158int __read_mostly pi_inject_timer = -1;
159module_param(pi_inject_timer, bint, S_IRUGO | S_IWUSR);
160
18863bdd
AK
161#define KVM_NR_SHARED_MSRS 16
162
163struct kvm_shared_msrs_global {
164 int nr;
2bf78fa7 165 u32 msrs[KVM_NR_SHARED_MSRS];
18863bdd
AK
166};
167
168struct kvm_shared_msrs {
169 struct user_return_notifier urn;
170 bool registered;
2bf78fa7
SY
171 struct kvm_shared_msr_values {
172 u64 host;
173 u64 curr;
174 } values[KVM_NR_SHARED_MSRS];
18863bdd
AK
175};
176
177static struct kvm_shared_msrs_global __read_mostly shared_msrs_global;
013f6a5d 178static struct kvm_shared_msrs __percpu *shared_msrs;
18863bdd 179
139a12cf
AL
180static u64 __read_mostly host_xss;
181
417bc304 182struct kvm_stats_debugfs_item debugfs_entries[] = {
ba1389b7
AK
183 { "pf_fixed", VCPU_STAT(pf_fixed) },
184 { "pf_guest", VCPU_STAT(pf_guest) },
185 { "tlb_flush", VCPU_STAT(tlb_flush) },
186 { "invlpg", VCPU_STAT(invlpg) },
187 { "exits", VCPU_STAT(exits) },
188 { "io_exits", VCPU_STAT(io_exits) },
189 { "mmio_exits", VCPU_STAT(mmio_exits) },
190 { "signal_exits", VCPU_STAT(signal_exits) },
191 { "irq_window", VCPU_STAT(irq_window_exits) },
f08864b4 192 { "nmi_window", VCPU_STAT(nmi_window_exits) },
ba1389b7 193 { "halt_exits", VCPU_STAT(halt_exits) },
f7819512 194 { "halt_successful_poll", VCPU_STAT(halt_successful_poll) },
62bea5bf 195 { "halt_attempted_poll", VCPU_STAT(halt_attempted_poll) },
3491caf2 196 { "halt_poll_invalid", VCPU_STAT(halt_poll_invalid) },
ba1389b7 197 { "halt_wakeup", VCPU_STAT(halt_wakeup) },
f11c3a8d 198 { "hypercalls", VCPU_STAT(hypercalls) },
ba1389b7
AK
199 { "request_irq", VCPU_STAT(request_irq_exits) },
200 { "irq_exits", VCPU_STAT(irq_exits) },
201 { "host_state_reload", VCPU_STAT(host_state_reload) },
ba1389b7
AK
202 { "fpu_reload", VCPU_STAT(fpu_reload) },
203 { "insn_emulation", VCPU_STAT(insn_emulation) },
204 { "insn_emulation_fail", VCPU_STAT(insn_emulation_fail) },
fa89a817 205 { "irq_injections", VCPU_STAT(irq_injections) },
c4abb7c9 206 { "nmi_injections", VCPU_STAT(nmi_injections) },
0f1e261e 207 { "req_event", VCPU_STAT(req_event) },
c595ceee 208 { "l1d_flush", VCPU_STAT(l1d_flush) },
4cee5764
AK
209 { "mmu_shadow_zapped", VM_STAT(mmu_shadow_zapped) },
210 { "mmu_pte_write", VM_STAT(mmu_pte_write) },
211 { "mmu_pte_updated", VM_STAT(mmu_pte_updated) },
212 { "mmu_pde_zapped", VM_STAT(mmu_pde_zapped) },
213 { "mmu_flooded", VM_STAT(mmu_flooded) },
214 { "mmu_recycled", VM_STAT(mmu_recycled) },
dfc5aa00 215 { "mmu_cache_miss", VM_STAT(mmu_cache_miss) },
4731d4c7 216 { "mmu_unsync", VM_STAT(mmu_unsync) },
0f74a24c 217 { "remote_tlb_flush", VM_STAT(remote_tlb_flush) },
833b45de 218 { "largepages", VM_STAT(lpages, .mode = 0444) },
b8e8c830 219 { "nx_largepages_splitted", VM_STAT(nx_lpage_splits, .mode = 0444) },
f3414bc7
DM
220 { "max_mmu_page_hash_collisions",
221 VM_STAT(max_mmu_page_hash_collisions) },
417bc304
HB
222 { NULL }
223};
224
2acf923e
DC
225u64 __read_mostly host_xcr0;
226
b666a4b6
MO
227struct kmem_cache *x86_fpu_cache;
228EXPORT_SYMBOL_GPL(x86_fpu_cache);
229
b6785def 230static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt);
d6aa1000 231
af585b92
GN
232static inline void kvm_async_pf_hash_reset(struct kvm_vcpu *vcpu)
233{
234 int i;
235 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU); i++)
236 vcpu->arch.apf.gfns[i] = ~0;
237}
238
18863bdd
AK
239static void kvm_on_user_return(struct user_return_notifier *urn)
240{
241 unsigned slot;
18863bdd
AK
242 struct kvm_shared_msrs *locals
243 = container_of(urn, struct kvm_shared_msrs, urn);
2bf78fa7 244 struct kvm_shared_msr_values *values;
1650b4eb
IA
245 unsigned long flags;
246
247 /*
248 * Disabling irqs at this point since the following code could be
249 * interrupted and executed through kvm_arch_hardware_disable()
250 */
251 local_irq_save(flags);
252 if (locals->registered) {
253 locals->registered = false;
254 user_return_notifier_unregister(urn);
255 }
256 local_irq_restore(flags);
18863bdd 257 for (slot = 0; slot < shared_msrs_global.nr; ++slot) {
2bf78fa7
SY
258 values = &locals->values[slot];
259 if (values->host != values->curr) {
260 wrmsrl(shared_msrs_global.msrs[slot], values->host);
261 values->curr = values->host;
18863bdd
AK
262 }
263 }
18863bdd
AK
264}
265
2bf78fa7
SY
266void kvm_define_shared_msr(unsigned slot, u32 msr)
267{
0123be42 268 BUG_ON(slot >= KVM_NR_SHARED_MSRS);
c847fe88 269 shared_msrs_global.msrs[slot] = msr;
18863bdd
AK
270 if (slot >= shared_msrs_global.nr)
271 shared_msrs_global.nr = slot + 1;
18863bdd
AK
272}
273EXPORT_SYMBOL_GPL(kvm_define_shared_msr);
274
275static void kvm_shared_msr_cpu_online(void)
276{
05c19c2f
SC
277 unsigned int cpu = smp_processor_id();
278 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
279 u64 value;
280 int i;
18863bdd 281
05c19c2f
SC
282 for (i = 0; i < shared_msrs_global.nr; ++i) {
283 rdmsrl_safe(shared_msrs_global.msrs[i], &value);
284 smsr->values[i].host = value;
285 smsr->values[i].curr = value;
286 }
18863bdd
AK
287}
288
8b3c3104 289int kvm_set_shared_msr(unsigned slot, u64 value, u64 mask)
18863bdd 290{
013f6a5d
MT
291 unsigned int cpu = smp_processor_id();
292 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
8b3c3104 293 int err;
18863bdd 294
de1fca5d
PB
295 value = (value & mask) | (smsr->values[slot].host & ~mask);
296 if (value == smsr->values[slot].curr)
8b3c3104 297 return 0;
8b3c3104
AH
298 err = wrmsrl_safe(shared_msrs_global.msrs[slot], value);
299 if (err)
300 return 1;
301
de1fca5d 302 smsr->values[slot].curr = value;
18863bdd
AK
303 if (!smsr->registered) {
304 smsr->urn.on_user_return = kvm_on_user_return;
305 user_return_notifier_register(&smsr->urn);
306 smsr->registered = true;
307 }
8b3c3104 308 return 0;
18863bdd
AK
309}
310EXPORT_SYMBOL_GPL(kvm_set_shared_msr);
311
13a34e06 312static void drop_user_return_notifiers(void)
3548bab5 313{
013f6a5d
MT
314 unsigned int cpu = smp_processor_id();
315 struct kvm_shared_msrs *smsr = per_cpu_ptr(shared_msrs, cpu);
3548bab5
AK
316
317 if (smsr->registered)
318 kvm_on_user_return(&smsr->urn);
319}
320
6866b83e
CO
321u64 kvm_get_apic_base(struct kvm_vcpu *vcpu)
322{
8a5a87d9 323 return vcpu->arch.apic_base;
6866b83e
CO
324}
325EXPORT_SYMBOL_GPL(kvm_get_apic_base);
326
58871649
JM
327enum lapic_mode kvm_get_apic_mode(struct kvm_vcpu *vcpu)
328{
329 return kvm_apic_mode(kvm_get_apic_base(vcpu));
330}
331EXPORT_SYMBOL_GPL(kvm_get_apic_mode);
332
58cb628d
JK
333int kvm_set_apic_base(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
334{
58871649
JM
335 enum lapic_mode old_mode = kvm_get_apic_mode(vcpu);
336 enum lapic_mode new_mode = kvm_apic_mode(msr_info->data);
d6321d49
RK
337 u64 reserved_bits = ((~0ULL) << cpuid_maxphyaddr(vcpu)) | 0x2ff |
338 (guest_cpuid_has(vcpu, X86_FEATURE_X2APIC) ? 0 : X2APIC_ENABLE);
58cb628d 339
58871649 340 if ((msr_info->data & reserved_bits) != 0 || new_mode == LAPIC_MODE_INVALID)
58cb628d 341 return 1;
58871649
JM
342 if (!msr_info->host_initiated) {
343 if (old_mode == LAPIC_MODE_X2APIC && new_mode == LAPIC_MODE_XAPIC)
344 return 1;
345 if (old_mode == LAPIC_MODE_DISABLED && new_mode == LAPIC_MODE_X2APIC)
346 return 1;
347 }
58cb628d
JK
348
349 kvm_lapic_set_base(vcpu, msr_info->data);
350 return 0;
6866b83e
CO
351}
352EXPORT_SYMBOL_GPL(kvm_set_apic_base);
353
2605fc21 354asmlinkage __visible void kvm_spurious_fault(void)
e3ba45b8
GL
355{
356 /* Fault while not rebooting. We want the trace. */
b4fdcf60 357 BUG_ON(!kvm_rebooting);
e3ba45b8
GL
358}
359EXPORT_SYMBOL_GPL(kvm_spurious_fault);
360
3fd28fce
ED
361#define EXCPT_BENIGN 0
362#define EXCPT_CONTRIBUTORY 1
363#define EXCPT_PF 2
364
365static int exception_class(int vector)
366{
367 switch (vector) {
368 case PF_VECTOR:
369 return EXCPT_PF;
370 case DE_VECTOR:
371 case TS_VECTOR:
372 case NP_VECTOR:
373 case SS_VECTOR:
374 case GP_VECTOR:
375 return EXCPT_CONTRIBUTORY;
376 default:
377 break;
378 }
379 return EXCPT_BENIGN;
380}
381
d6e8c854
NA
382#define EXCPT_FAULT 0
383#define EXCPT_TRAP 1
384#define EXCPT_ABORT 2
385#define EXCPT_INTERRUPT 3
386
387static int exception_type(int vector)
388{
389 unsigned int mask;
390
391 if (WARN_ON(vector > 31 || vector == NMI_VECTOR))
392 return EXCPT_INTERRUPT;
393
394 mask = 1 << vector;
395
396 /* #DB is trap, as instruction watchpoints are handled elsewhere */
397 if (mask & ((1 << DB_VECTOR) | (1 << BP_VECTOR) | (1 << OF_VECTOR)))
398 return EXCPT_TRAP;
399
400 if (mask & ((1 << DF_VECTOR) | (1 << MC_VECTOR)))
401 return EXCPT_ABORT;
402
403 /* Reserved exceptions will result in fault */
404 return EXCPT_FAULT;
405}
406
da998b46
JM
407void kvm_deliver_exception_payload(struct kvm_vcpu *vcpu)
408{
409 unsigned nr = vcpu->arch.exception.nr;
410 bool has_payload = vcpu->arch.exception.has_payload;
411 unsigned long payload = vcpu->arch.exception.payload;
412
413 if (!has_payload)
414 return;
415
416 switch (nr) {
f10c729f
JM
417 case DB_VECTOR:
418 /*
419 * "Certain debug exceptions may clear bit 0-3. The
420 * remaining contents of the DR6 register are never
421 * cleared by the processor".
422 */
423 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
424 /*
425 * DR6.RTM is set by all #DB exceptions that don't clear it.
426 */
427 vcpu->arch.dr6 |= DR6_RTM;
428 vcpu->arch.dr6 |= payload;
429 /*
430 * Bit 16 should be set in the payload whenever the #DB
431 * exception should clear DR6.RTM. This makes the payload
432 * compatible with the pending debug exceptions under VMX.
433 * Though not currently documented in the SDM, this also
434 * makes the payload compatible with the exit qualification
435 * for #DB exceptions under VMX.
436 */
437 vcpu->arch.dr6 ^= payload & DR6_RTM;
438 break;
da998b46
JM
439 case PF_VECTOR:
440 vcpu->arch.cr2 = payload;
441 break;
442 }
443
444 vcpu->arch.exception.has_payload = false;
445 vcpu->arch.exception.payload = 0;
446}
447EXPORT_SYMBOL_GPL(kvm_deliver_exception_payload);
448
3fd28fce 449static void kvm_multiple_exception(struct kvm_vcpu *vcpu,
ce7ddec4 450 unsigned nr, bool has_error, u32 error_code,
91e86d22 451 bool has_payload, unsigned long payload, bool reinject)
3fd28fce
ED
452{
453 u32 prev_nr;
454 int class1, class2;
455
3842d135
AK
456 kvm_make_request(KVM_REQ_EVENT, vcpu);
457
664f8e26 458 if (!vcpu->arch.exception.pending && !vcpu->arch.exception.injected) {
3fd28fce 459 queue:
3ffb2468
NA
460 if (has_error && !is_protmode(vcpu))
461 has_error = false;
664f8e26
WL
462 if (reinject) {
463 /*
464 * On vmentry, vcpu->arch.exception.pending is only
465 * true if an event injection was blocked by
466 * nested_run_pending. In that case, however,
467 * vcpu_enter_guest requests an immediate exit,
468 * and the guest shouldn't proceed far enough to
469 * need reinjection.
470 */
471 WARN_ON_ONCE(vcpu->arch.exception.pending);
472 vcpu->arch.exception.injected = true;
91e86d22
JM
473 if (WARN_ON_ONCE(has_payload)) {
474 /*
475 * A reinjected event has already
476 * delivered its payload.
477 */
478 has_payload = false;
479 payload = 0;
480 }
664f8e26
WL
481 } else {
482 vcpu->arch.exception.pending = true;
483 vcpu->arch.exception.injected = false;
484 }
3fd28fce
ED
485 vcpu->arch.exception.has_error_code = has_error;
486 vcpu->arch.exception.nr = nr;
487 vcpu->arch.exception.error_code = error_code;
91e86d22
JM
488 vcpu->arch.exception.has_payload = has_payload;
489 vcpu->arch.exception.payload = payload;
da998b46
JM
490 /*
491 * In guest mode, payload delivery should be deferred,
492 * so that the L1 hypervisor can intercept #PF before
f10c729f
JM
493 * CR2 is modified (or intercept #DB before DR6 is
494 * modified under nVMX). However, for ABI
495 * compatibility with KVM_GET_VCPU_EVENTS and
496 * KVM_SET_VCPU_EVENTS, we can't delay payload
497 * delivery unless userspace has enabled this
498 * functionality via the per-VM capability,
499 * KVM_CAP_EXCEPTION_PAYLOAD.
da998b46
JM
500 */
501 if (!vcpu->kvm->arch.exception_payload_enabled ||
502 !is_guest_mode(vcpu))
503 kvm_deliver_exception_payload(vcpu);
3fd28fce
ED
504 return;
505 }
506
507 /* to check exception */
508 prev_nr = vcpu->arch.exception.nr;
509 if (prev_nr == DF_VECTOR) {
510 /* triple fault -> shutdown */
a8eeb04a 511 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
3fd28fce
ED
512 return;
513 }
514 class1 = exception_class(prev_nr);
515 class2 = exception_class(nr);
516 if ((class1 == EXCPT_CONTRIBUTORY && class2 == EXCPT_CONTRIBUTORY)
517 || (class1 == EXCPT_PF && class2 != EXCPT_BENIGN)) {
664f8e26
WL
518 /*
519 * Generate double fault per SDM Table 5-5. Set
520 * exception.pending = true so that the double fault
521 * can trigger a nested vmexit.
522 */
3fd28fce 523 vcpu->arch.exception.pending = true;
664f8e26 524 vcpu->arch.exception.injected = false;
3fd28fce
ED
525 vcpu->arch.exception.has_error_code = true;
526 vcpu->arch.exception.nr = DF_VECTOR;
527 vcpu->arch.exception.error_code = 0;
c851436a
JM
528 vcpu->arch.exception.has_payload = false;
529 vcpu->arch.exception.payload = 0;
3fd28fce
ED
530 } else
531 /* replace previous exception with a new one in a hope
532 that instruction re-execution will regenerate lost
533 exception */
534 goto queue;
535}
536
298101da
AK
537void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr)
538{
91e86d22 539 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, false);
298101da
AK
540}
541EXPORT_SYMBOL_GPL(kvm_queue_exception);
542
ce7ddec4
JR
543void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr)
544{
91e86d22 545 kvm_multiple_exception(vcpu, nr, false, 0, false, 0, true);
ce7ddec4
JR
546}
547EXPORT_SYMBOL_GPL(kvm_requeue_exception);
548
f10c729f
JM
549static void kvm_queue_exception_p(struct kvm_vcpu *vcpu, unsigned nr,
550 unsigned long payload)
551{
552 kvm_multiple_exception(vcpu, nr, false, 0, true, payload, false);
553}
554
da998b46
JM
555static void kvm_queue_exception_e_p(struct kvm_vcpu *vcpu, unsigned nr,
556 u32 error_code, unsigned long payload)
557{
558 kvm_multiple_exception(vcpu, nr, true, error_code,
559 true, payload, false);
560}
561
6affcbed 562int kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err)
c3c91fee 563{
db8fcefa
AP
564 if (err)
565 kvm_inject_gp(vcpu, 0);
566 else
6affcbed
KH
567 return kvm_skip_emulated_instruction(vcpu);
568
569 return 1;
db8fcefa
AP
570}
571EXPORT_SYMBOL_GPL(kvm_complete_insn_gp);
8df25a32 572
6389ee94 573void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
c3c91fee
AK
574{
575 ++vcpu->stat.pf_guest;
adfe20fb
WL
576 vcpu->arch.exception.nested_apf =
577 is_guest_mode(vcpu) && fault->async_page_fault;
da998b46 578 if (vcpu->arch.exception.nested_apf) {
adfe20fb 579 vcpu->arch.apf.nested_apf_token = fault->address;
da998b46
JM
580 kvm_queue_exception_e(vcpu, PF_VECTOR, fault->error_code);
581 } else {
582 kvm_queue_exception_e_p(vcpu, PF_VECTOR, fault->error_code,
583 fault->address);
584 }
c3c91fee 585}
27d6c865 586EXPORT_SYMBOL_GPL(kvm_inject_page_fault);
c3c91fee 587
ef54bcfe 588static bool kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault)
d4f8cf66 589{
6389ee94
AK
590 if (mmu_is_nested(vcpu) && !fault->nested_page_fault)
591 vcpu->arch.nested_mmu.inject_page_fault(vcpu, fault);
d4f8cf66 592 else
44dd3ffa 593 vcpu->arch.mmu->inject_page_fault(vcpu, fault);
ef54bcfe
PB
594
595 return fault->nested_page_fault;
d4f8cf66
JR
596}
597
3419ffc8
SY
598void kvm_inject_nmi(struct kvm_vcpu *vcpu)
599{
7460fb4a
AK
600 atomic_inc(&vcpu->arch.nmi_queued);
601 kvm_make_request(KVM_REQ_NMI, vcpu);
3419ffc8
SY
602}
603EXPORT_SYMBOL_GPL(kvm_inject_nmi);
604
298101da
AK
605void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
606{
91e86d22 607 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, false);
298101da
AK
608}
609EXPORT_SYMBOL_GPL(kvm_queue_exception_e);
610
ce7ddec4
JR
611void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code)
612{
91e86d22 613 kvm_multiple_exception(vcpu, nr, true, error_code, false, 0, true);
ce7ddec4
JR
614}
615EXPORT_SYMBOL_GPL(kvm_requeue_exception_e);
616
0a79b009
AK
617/*
618 * Checks if cpl <= required_cpl; if true, return true. Otherwise queue
619 * a #GP and return false.
620 */
621bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl)
298101da 622{
0a79b009
AK
623 if (kvm_x86_ops->get_cpl(vcpu) <= required_cpl)
624 return true;
625 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
626 return false;
298101da 627}
0a79b009 628EXPORT_SYMBOL_GPL(kvm_require_cpl);
298101da 629
16f8a6f9
NA
630bool kvm_require_dr(struct kvm_vcpu *vcpu, int dr)
631{
632 if ((dr != 4 && dr != 5) || !kvm_read_cr4_bits(vcpu, X86_CR4_DE))
633 return true;
634
635 kvm_queue_exception(vcpu, UD_VECTOR);
636 return false;
637}
638EXPORT_SYMBOL_GPL(kvm_require_dr);
639
ec92fe44
JR
640/*
641 * This function will be used to read from the physical memory of the currently
54bf36aa 642 * running guest. The difference to kvm_vcpu_read_guest_page is that this function
ec92fe44
JR
643 * can read from guest physical or from the guest's guest physical memory.
644 */
645int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
646 gfn_t ngfn, void *data, int offset, int len,
647 u32 access)
648{
54987b7a 649 struct x86_exception exception;
ec92fe44
JR
650 gfn_t real_gfn;
651 gpa_t ngpa;
652
653 ngpa = gfn_to_gpa(ngfn);
54987b7a 654 real_gfn = mmu->translate_gpa(vcpu, ngpa, access, &exception);
ec92fe44
JR
655 if (real_gfn == UNMAPPED_GVA)
656 return -EFAULT;
657
658 real_gfn = gpa_to_gfn(real_gfn);
659
54bf36aa 660 return kvm_vcpu_read_guest_page(vcpu, real_gfn, data, offset, len);
ec92fe44
JR
661}
662EXPORT_SYMBOL_GPL(kvm_read_guest_page_mmu);
663
69b0049a 664static int kvm_read_nested_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
3d06b8bf
JR
665 void *data, int offset, int len, u32 access)
666{
667 return kvm_read_guest_page_mmu(vcpu, vcpu->arch.walk_mmu, gfn,
668 data, offset, len, access);
669}
670
16cfacc8
SC
671static inline u64 pdptr_rsvd_bits(struct kvm_vcpu *vcpu)
672{
673 return rsvd_bits(cpuid_maxphyaddr(vcpu), 63) | rsvd_bits(5, 8) |
674 rsvd_bits(1, 2);
675}
676
a03490ed 677/*
16cfacc8 678 * Load the pae pdptrs. Return 1 if they are all valid, 0 otherwise.
a03490ed 679 */
ff03a073 680int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3)
a03490ed
CO
681{
682 gfn_t pdpt_gfn = cr3 >> PAGE_SHIFT;
683 unsigned offset = ((cr3 & (PAGE_SIZE-1)) >> 5) << 2;
684 int i;
685 int ret;
ff03a073 686 u64 pdpte[ARRAY_SIZE(mmu->pdptrs)];
a03490ed 687
ff03a073
JR
688 ret = kvm_read_guest_page_mmu(vcpu, mmu, pdpt_gfn, pdpte,
689 offset * sizeof(u64), sizeof(pdpte),
690 PFERR_USER_MASK|PFERR_WRITE_MASK);
a03490ed
CO
691 if (ret < 0) {
692 ret = 0;
693 goto out;
694 }
695 for (i = 0; i < ARRAY_SIZE(pdpte); ++i) {
812f30b2 696 if ((pdpte[i] & PT_PRESENT_MASK) &&
16cfacc8 697 (pdpte[i] & pdptr_rsvd_bits(vcpu))) {
a03490ed
CO
698 ret = 0;
699 goto out;
700 }
701 }
702 ret = 1;
703
ff03a073 704 memcpy(mmu->pdptrs, pdpte, sizeof(mmu->pdptrs));
cb3c1e2f
SC
705 kvm_register_mark_dirty(vcpu, VCPU_EXREG_PDPTR);
706
a03490ed 707out:
a03490ed
CO
708
709 return ret;
710}
cc4b6871 711EXPORT_SYMBOL_GPL(load_pdptrs);
a03490ed 712
9ed38ffa 713bool pdptrs_changed(struct kvm_vcpu *vcpu)
d835dfec 714{
ff03a073 715 u64 pdpte[ARRAY_SIZE(vcpu->arch.walk_mmu->pdptrs)];
3d06b8bf
JR
716 int offset;
717 gfn_t gfn;
d835dfec
AK
718 int r;
719
bf03d4f9 720 if (!is_pae_paging(vcpu))
d835dfec
AK
721 return false;
722
cb3c1e2f 723 if (!kvm_register_is_available(vcpu, VCPU_EXREG_PDPTR))
6de4f3ad
AK
724 return true;
725
a512177e
PB
726 gfn = (kvm_read_cr3(vcpu) & 0xffffffe0ul) >> PAGE_SHIFT;
727 offset = (kvm_read_cr3(vcpu) & 0xffffffe0ul) & (PAGE_SIZE - 1);
3d06b8bf
JR
728 r = kvm_read_nested_guest_page(vcpu, gfn, pdpte, offset, sizeof(pdpte),
729 PFERR_USER_MASK | PFERR_WRITE_MASK);
d835dfec 730 if (r < 0)
7f7f0d9c 731 return true;
d835dfec 732
7f7f0d9c 733 return memcmp(pdpte, vcpu->arch.walk_mmu->pdptrs, sizeof(pdpte)) != 0;
d835dfec 734}
9ed38ffa 735EXPORT_SYMBOL_GPL(pdptrs_changed);
d835dfec 736
49a9b07e 737int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
a03490ed 738{
aad82703 739 unsigned long old_cr0 = kvm_read_cr0(vcpu);
d81135a5 740 unsigned long update_bits = X86_CR0_PG | X86_CR0_WP;
aad82703 741
f9a48e6a
AK
742 cr0 |= X86_CR0_ET;
743
ab344828 744#ifdef CONFIG_X86_64
0f12244f
GN
745 if (cr0 & 0xffffffff00000000UL)
746 return 1;
ab344828
GN
747#endif
748
749 cr0 &= ~CR0_RESERVED_BITS;
a03490ed 750
0f12244f
GN
751 if ((cr0 & X86_CR0_NW) && !(cr0 & X86_CR0_CD))
752 return 1;
a03490ed 753
0f12244f
GN
754 if ((cr0 & X86_CR0_PG) && !(cr0 & X86_CR0_PE))
755 return 1;
a03490ed
CO
756
757 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
758#ifdef CONFIG_X86_64
f6801dff 759 if ((vcpu->arch.efer & EFER_LME)) {
a03490ed
CO
760 int cs_db, cs_l;
761
0f12244f
GN
762 if (!is_pae(vcpu))
763 return 1;
a03490ed 764 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
0f12244f
GN
765 if (cs_l)
766 return 1;
a03490ed
CO
767 } else
768#endif
ff03a073 769 if (is_pae(vcpu) && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
9f8fe504 770 kvm_read_cr3(vcpu)))
0f12244f 771 return 1;
a03490ed
CO
772 }
773
ad756a16
MJ
774 if (!(cr0 & X86_CR0_PG) && kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE))
775 return 1;
776
a03490ed 777 kvm_x86_ops->set_cr0(vcpu, cr0);
a03490ed 778
d170c419 779 if ((cr0 ^ old_cr0) & X86_CR0_PG) {
e5f3f027 780 kvm_clear_async_pf_completion_queue(vcpu);
d170c419
LJ
781 kvm_async_pf_hash_reset(vcpu);
782 }
e5f3f027 783
aad82703
SY
784 if ((cr0 ^ old_cr0) & update_bits)
785 kvm_mmu_reset_context(vcpu);
b18d5431 786
879ae188
LE
787 if (((cr0 ^ old_cr0) & X86_CR0_CD) &&
788 kvm_arch_has_noncoherent_dma(vcpu->kvm) &&
789 !kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_CD_NW_CLEARED))
b18d5431
XG
790 kvm_zap_gfn_range(vcpu->kvm, 0, ~0ULL);
791
0f12244f
GN
792 return 0;
793}
2d3ad1f4 794EXPORT_SYMBOL_GPL(kvm_set_cr0);
a03490ed 795
2d3ad1f4 796void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw)
a03490ed 797{
49a9b07e 798 (void)kvm_set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~0x0eul) | (msw & 0x0f));
a03490ed 799}
2d3ad1f4 800EXPORT_SYMBOL_GPL(kvm_lmsw);
a03490ed 801
139a12cf 802void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu)
42bdf991 803{
139a12cf
AL
804 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
805
806 if (vcpu->arch.xcr0 != host_xcr0)
807 xsetbv(XCR_XFEATURE_ENABLED_MASK, vcpu->arch.xcr0);
808
809 if (vcpu->arch.xsaves_enabled &&
810 vcpu->arch.ia32_xss != host_xss)
811 wrmsrl(MSR_IA32_XSS, vcpu->arch.ia32_xss);
812 }
42bdf991 813}
139a12cf 814EXPORT_SYMBOL_GPL(kvm_load_guest_xsave_state);
42bdf991 815
139a12cf 816void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu)
42bdf991 817{
139a12cf
AL
818 if (kvm_read_cr4_bits(vcpu, X86_CR4_OSXSAVE)) {
819
820 if (vcpu->arch.xcr0 != host_xcr0)
821 xsetbv(XCR_XFEATURE_ENABLED_MASK, host_xcr0);
822
823 if (vcpu->arch.xsaves_enabled &&
824 vcpu->arch.ia32_xss != host_xss)
825 wrmsrl(MSR_IA32_XSS, host_xss);
826 }
827
42bdf991 828}
139a12cf 829EXPORT_SYMBOL_GPL(kvm_load_host_xsave_state);
42bdf991 830
69b0049a 831static int __kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
2acf923e 832{
56c103ec
LJ
833 u64 xcr0 = xcr;
834 u64 old_xcr0 = vcpu->arch.xcr0;
46c34cb0 835 u64 valid_bits;
2acf923e
DC
836
837 /* Only support XCR_XFEATURE_ENABLED_MASK(xcr0) now */
838 if (index != XCR_XFEATURE_ENABLED_MASK)
839 return 1;
d91cab78 840 if (!(xcr0 & XFEATURE_MASK_FP))
2acf923e 841 return 1;
d91cab78 842 if ((xcr0 & XFEATURE_MASK_YMM) && !(xcr0 & XFEATURE_MASK_SSE))
2acf923e 843 return 1;
46c34cb0
PB
844
845 /*
846 * Do not allow the guest to set bits that we do not support
847 * saving. However, xcr0 bit 0 is always set, even if the
848 * emulated CPU does not support XSAVE (see fx_init).
849 */
d91cab78 850 valid_bits = vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FP;
46c34cb0 851 if (xcr0 & ~valid_bits)
2acf923e 852 return 1;
46c34cb0 853
d91cab78
DH
854 if ((!(xcr0 & XFEATURE_MASK_BNDREGS)) !=
855 (!(xcr0 & XFEATURE_MASK_BNDCSR)))
390bd528
LJ
856 return 1;
857
d91cab78
DH
858 if (xcr0 & XFEATURE_MASK_AVX512) {
859 if (!(xcr0 & XFEATURE_MASK_YMM))
612263b3 860 return 1;
d91cab78 861 if ((xcr0 & XFEATURE_MASK_AVX512) != XFEATURE_MASK_AVX512)
612263b3
CP
862 return 1;
863 }
2acf923e 864 vcpu->arch.xcr0 = xcr0;
56c103ec 865
d91cab78 866 if ((xcr0 ^ old_xcr0) & XFEATURE_MASK_EXTEND)
56c103ec 867 kvm_update_cpuid(vcpu);
2acf923e
DC
868 return 0;
869}
870
871int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr)
872{
764bcbc5
Z
873 if (kvm_x86_ops->get_cpl(vcpu) != 0 ||
874 __kvm_set_xcr(vcpu, index, xcr)) {
2acf923e
DC
875 kvm_inject_gp(vcpu, 0);
876 return 1;
877 }
878 return 0;
879}
880EXPORT_SYMBOL_GPL(kvm_set_xcr);
881
3ca94192 882static int kvm_valid_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
a03490ed 883{
0f12244f 884 if (cr4 & CR4_RESERVED_BITS)
3ca94192 885 return -EINVAL;
a03490ed 886
d6321d49 887 if (!guest_cpuid_has(vcpu, X86_FEATURE_XSAVE) && (cr4 & X86_CR4_OSXSAVE))
3ca94192 888 return -EINVAL;
2acf923e 889
d6321d49 890 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMEP) && (cr4 & X86_CR4_SMEP))
3ca94192 891 return -EINVAL;
2acf923e 892
d6321d49 893 if (!guest_cpuid_has(vcpu, X86_FEATURE_SMAP) && (cr4 & X86_CR4_SMAP))
3ca94192 894 return -EINVAL;
c68b734f 895
d6321d49 896 if (!guest_cpuid_has(vcpu, X86_FEATURE_FSGSBASE) && (cr4 & X86_CR4_FSGSBASE))
3ca94192 897 return -EINVAL;
97ec8c06 898
d6321d49 899 if (!guest_cpuid_has(vcpu, X86_FEATURE_PKU) && (cr4 & X86_CR4_PKE))
3ca94192 900 return -EINVAL;
74dc2b4f 901
fd8cb433 902 if (!guest_cpuid_has(vcpu, X86_FEATURE_LA57) && (cr4 & X86_CR4_LA57))
3ca94192 903 return -EINVAL;
b9baba86 904
ae3e61e1 905 if (!guest_cpuid_has(vcpu, X86_FEATURE_UMIP) && (cr4 & X86_CR4_UMIP))
3ca94192
WL
906 return -EINVAL;
907
908 return 0;
909}
910
911int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
912{
913 unsigned long old_cr4 = kvm_read_cr4(vcpu);
914 unsigned long pdptr_bits = X86_CR4_PGE | X86_CR4_PSE | X86_CR4_PAE |
915 X86_CR4_SMEP | X86_CR4_SMAP | X86_CR4_PKE;
916
917 if (kvm_valid_cr4(vcpu, cr4))
ae3e61e1
PB
918 return 1;
919
a03490ed 920 if (is_long_mode(vcpu)) {
0f12244f
GN
921 if (!(cr4 & X86_CR4_PAE))
922 return 1;
a2edf57f
AK
923 } else if (is_paging(vcpu) && (cr4 & X86_CR4_PAE)
924 && ((cr4 ^ old_cr4) & pdptr_bits)
9f8fe504
AK
925 && !load_pdptrs(vcpu, vcpu->arch.walk_mmu,
926 kvm_read_cr3(vcpu)))
0f12244f
GN
927 return 1;
928
ad756a16 929 if ((cr4 & X86_CR4_PCIDE) && !(old_cr4 & X86_CR4_PCIDE)) {
d6321d49 930 if (!guest_cpuid_has(vcpu, X86_FEATURE_PCID))
ad756a16
MJ
931 return 1;
932
933 /* PCID can not be enabled when cr3[11:0]!=000H or EFER.LMA=0 */
934 if ((kvm_read_cr3(vcpu) & X86_CR3_PCID_MASK) || !is_long_mode(vcpu))
935 return 1;
936 }
937
5e1746d6 938 if (kvm_x86_ops->set_cr4(vcpu, cr4))
0f12244f 939 return 1;
a03490ed 940
ad756a16
MJ
941 if (((cr4 ^ old_cr4) & pdptr_bits) ||
942 (!(cr4 & X86_CR4_PCIDE) && (old_cr4 & X86_CR4_PCIDE)))
aad82703 943 kvm_mmu_reset_context(vcpu);
0f12244f 944
b9baba86 945 if ((cr4 ^ old_cr4) & (X86_CR4_OSXSAVE | X86_CR4_PKE))
00b27a3e 946 kvm_update_cpuid(vcpu);
2acf923e 947
0f12244f
GN
948 return 0;
949}
2d3ad1f4 950EXPORT_SYMBOL_GPL(kvm_set_cr4);
a03490ed 951
2390218b 952int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3)
a03490ed 953{
ade61e28 954 bool skip_tlb_flush = false;
ac146235 955#ifdef CONFIG_X86_64
c19986fe
JS
956 bool pcid_enabled = kvm_read_cr4_bits(vcpu, X86_CR4_PCIDE);
957
ade61e28 958 if (pcid_enabled) {
208320ba
JS
959 skip_tlb_flush = cr3 & X86_CR3_PCID_NOFLUSH;
960 cr3 &= ~X86_CR3_PCID_NOFLUSH;
ade61e28 961 }
ac146235 962#endif
9d88fca7 963
9f8fe504 964 if (cr3 == kvm_read_cr3(vcpu) && !pdptrs_changed(vcpu)) {
956bf353
JS
965 if (!skip_tlb_flush) {
966 kvm_mmu_sync_roots(vcpu);
ade61e28 967 kvm_make_request(KVM_REQ_TLB_FLUSH, vcpu);
956bf353 968 }
0f12244f 969 return 0;
d835dfec
AK
970 }
971
d1cd3ce9 972 if (is_long_mode(vcpu) &&
a780a3ea 973 (cr3 & rsvd_bits(cpuid_maxphyaddr(vcpu), 63)))
d1cd3ce9 974 return 1;
bf03d4f9
PB
975 else if (is_pae_paging(vcpu) &&
976 !load_pdptrs(vcpu, vcpu->arch.walk_mmu, cr3))
346874c9 977 return 1;
a03490ed 978
ade61e28 979 kvm_mmu_new_cr3(vcpu, cr3, skip_tlb_flush);
0f12244f 980 vcpu->arch.cr3 = cr3;
cb3c1e2f 981 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
7c390d35 982
0f12244f
GN
983 return 0;
984}
2d3ad1f4 985EXPORT_SYMBOL_GPL(kvm_set_cr3);
a03490ed 986
eea1cff9 987int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8)
a03490ed 988{
0f12244f
GN
989 if (cr8 & CR8_RESERVED_BITS)
990 return 1;
35754c98 991 if (lapic_in_kernel(vcpu))
a03490ed
CO
992 kvm_lapic_set_tpr(vcpu, cr8);
993 else
ad312c7c 994 vcpu->arch.cr8 = cr8;
0f12244f
GN
995 return 0;
996}
2d3ad1f4 997EXPORT_SYMBOL_GPL(kvm_set_cr8);
a03490ed 998
2d3ad1f4 999unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu)
a03490ed 1000{
35754c98 1001 if (lapic_in_kernel(vcpu))
a03490ed
CO
1002 return kvm_lapic_get_cr8(vcpu);
1003 else
ad312c7c 1004 return vcpu->arch.cr8;
a03490ed 1005}
2d3ad1f4 1006EXPORT_SYMBOL_GPL(kvm_get_cr8);
a03490ed 1007
ae561ede
NA
1008static void kvm_update_dr0123(struct kvm_vcpu *vcpu)
1009{
1010 int i;
1011
1012 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)) {
1013 for (i = 0; i < KVM_NR_DB_REGS; i++)
1014 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
1015 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_RELOAD;
1016 }
1017}
1018
73aaf249
JK
1019static void kvm_update_dr6(struct kvm_vcpu *vcpu)
1020{
1021 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1022 kvm_x86_ops->set_dr6(vcpu, vcpu->arch.dr6);
1023}
1024
c8639010
JK
1025static void kvm_update_dr7(struct kvm_vcpu *vcpu)
1026{
1027 unsigned long dr7;
1028
1029 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1030 dr7 = vcpu->arch.guest_debug_dr7;
1031 else
1032 dr7 = vcpu->arch.dr7;
1033 kvm_x86_ops->set_dr7(vcpu, dr7);
360b948d
PB
1034 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_BP_ENABLED;
1035 if (dr7 & DR7_BP_EN_MASK)
1036 vcpu->arch.switch_db_regs |= KVM_DEBUGREG_BP_ENABLED;
c8639010
JK
1037}
1038
6f43ed01
NA
1039static u64 kvm_dr6_fixed(struct kvm_vcpu *vcpu)
1040{
1041 u64 fixed = DR6_FIXED_1;
1042
d6321d49 1043 if (!guest_cpuid_has(vcpu, X86_FEATURE_RTM))
6f43ed01
NA
1044 fixed |= DR6_RTM;
1045 return fixed;
1046}
1047
338dbc97 1048static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
020df079
GN
1049{
1050 switch (dr) {
1051 case 0 ... 3:
1052 vcpu->arch.db[dr] = val;
1053 if (!(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP))
1054 vcpu->arch.eff_db[dr] = val;
1055 break;
1056 case 4:
020df079
GN
1057 /* fall through */
1058 case 6:
338dbc97
GN
1059 if (val & 0xffffffff00000000ULL)
1060 return -1; /* #GP */
6f43ed01 1061 vcpu->arch.dr6 = (val & DR6_VOLATILE) | kvm_dr6_fixed(vcpu);
73aaf249 1062 kvm_update_dr6(vcpu);
020df079
GN
1063 break;
1064 case 5:
020df079
GN
1065 /* fall through */
1066 default: /* 7 */
338dbc97
GN
1067 if (val & 0xffffffff00000000ULL)
1068 return -1; /* #GP */
020df079 1069 vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
c8639010 1070 kvm_update_dr7(vcpu);
020df079
GN
1071 break;
1072 }
1073
1074 return 0;
1075}
338dbc97
GN
1076
1077int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
1078{
16f8a6f9 1079 if (__kvm_set_dr(vcpu, dr, val)) {
338dbc97 1080 kvm_inject_gp(vcpu, 0);
16f8a6f9
NA
1081 return 1;
1082 }
1083 return 0;
338dbc97 1084}
020df079
GN
1085EXPORT_SYMBOL_GPL(kvm_set_dr);
1086
16f8a6f9 1087int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val)
020df079
GN
1088{
1089 switch (dr) {
1090 case 0 ... 3:
1091 *val = vcpu->arch.db[dr];
1092 break;
1093 case 4:
020df079
GN
1094 /* fall through */
1095 case 6:
73aaf249
JK
1096 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1097 *val = vcpu->arch.dr6;
1098 else
1099 *val = kvm_x86_ops->get_dr6(vcpu);
020df079
GN
1100 break;
1101 case 5:
020df079
GN
1102 /* fall through */
1103 default: /* 7 */
1104 *val = vcpu->arch.dr7;
1105 break;
1106 }
338dbc97
GN
1107 return 0;
1108}
020df079
GN
1109EXPORT_SYMBOL_GPL(kvm_get_dr);
1110
022cd0e8
AK
1111bool kvm_rdpmc(struct kvm_vcpu *vcpu)
1112{
de3cd117 1113 u32 ecx = kvm_rcx_read(vcpu);
022cd0e8
AK
1114 u64 data;
1115 int err;
1116
c6702c9d 1117 err = kvm_pmu_rdpmc(vcpu, ecx, &data);
022cd0e8
AK
1118 if (err)
1119 return err;
de3cd117
SC
1120 kvm_rax_write(vcpu, (u32)data);
1121 kvm_rdx_write(vcpu, data >> 32);
022cd0e8
AK
1122 return err;
1123}
1124EXPORT_SYMBOL_GPL(kvm_rdpmc);
1125
043405e1
CO
1126/*
1127 * List of msr numbers which we expose to userspace through KVM_GET_MSRS
1128 * and KVM_SET_MSRS, and KVM_GET_MSR_INDEX_LIST.
1129 *
7a5ee6ed
CQ
1130 * The three MSR lists(msrs_to_save, emulated_msrs, msr_based_features)
1131 * extract the supported MSRs from the related const lists.
1132 * msrs_to_save is selected from the msrs_to_save_all to reflect the
e3267cbb 1133 * capabilities of the host cpu. This capabilities test skips MSRs that are
7a5ee6ed 1134 * kvm-specific. Those are put in emulated_msrs_all; filtering of emulated_msrs
62ef68bb 1135 * may depend on host virtualization features rather than host cpu features.
043405e1 1136 */
e3267cbb 1137
7a5ee6ed 1138static const u32 msrs_to_save_all[] = {
043405e1 1139 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
8c06585d 1140 MSR_STAR,
043405e1
CO
1141#ifdef CONFIG_X86_64
1142 MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
1143#endif
b3897a49 1144 MSR_IA32_TSC, MSR_IA32_CR_PAT, MSR_VM_HSAVE_PA,
9dbe6cf9 1145 MSR_IA32_FEATURE_CONTROL, MSR_IA32_BNDCFGS, MSR_TSC_AUX,
2bdb76c0 1146 MSR_IA32_SPEC_CTRL,
bf8c55d8
CP
1147 MSR_IA32_RTIT_CTL, MSR_IA32_RTIT_STATUS, MSR_IA32_RTIT_CR3_MATCH,
1148 MSR_IA32_RTIT_OUTPUT_BASE, MSR_IA32_RTIT_OUTPUT_MASK,
1149 MSR_IA32_RTIT_ADDR0_A, MSR_IA32_RTIT_ADDR0_B,
1150 MSR_IA32_RTIT_ADDR1_A, MSR_IA32_RTIT_ADDR1_B,
1151 MSR_IA32_RTIT_ADDR2_A, MSR_IA32_RTIT_ADDR2_B,
1152 MSR_IA32_RTIT_ADDR3_A, MSR_IA32_RTIT_ADDR3_B,
6e3ba4ab
TX
1153 MSR_IA32_UMWAIT_CONTROL,
1154
e2ada66e
JM
1155 MSR_ARCH_PERFMON_FIXED_CTR0, MSR_ARCH_PERFMON_FIXED_CTR1,
1156 MSR_ARCH_PERFMON_FIXED_CTR0 + 2, MSR_ARCH_PERFMON_FIXED_CTR0 + 3,
1157 MSR_CORE_PERF_FIXED_CTR_CTRL, MSR_CORE_PERF_GLOBAL_STATUS,
1158 MSR_CORE_PERF_GLOBAL_CTRL, MSR_CORE_PERF_GLOBAL_OVF_CTRL,
1159 MSR_ARCH_PERFMON_PERFCTR0, MSR_ARCH_PERFMON_PERFCTR1,
1160 MSR_ARCH_PERFMON_PERFCTR0 + 2, MSR_ARCH_PERFMON_PERFCTR0 + 3,
1161 MSR_ARCH_PERFMON_PERFCTR0 + 4, MSR_ARCH_PERFMON_PERFCTR0 + 5,
1162 MSR_ARCH_PERFMON_PERFCTR0 + 6, MSR_ARCH_PERFMON_PERFCTR0 + 7,
1163 MSR_ARCH_PERFMON_PERFCTR0 + 8, MSR_ARCH_PERFMON_PERFCTR0 + 9,
1164 MSR_ARCH_PERFMON_PERFCTR0 + 10, MSR_ARCH_PERFMON_PERFCTR0 + 11,
1165 MSR_ARCH_PERFMON_PERFCTR0 + 12, MSR_ARCH_PERFMON_PERFCTR0 + 13,
1166 MSR_ARCH_PERFMON_PERFCTR0 + 14, MSR_ARCH_PERFMON_PERFCTR0 + 15,
1167 MSR_ARCH_PERFMON_PERFCTR0 + 16, MSR_ARCH_PERFMON_PERFCTR0 + 17,
e2ada66e
JM
1168 MSR_ARCH_PERFMON_EVENTSEL0, MSR_ARCH_PERFMON_EVENTSEL1,
1169 MSR_ARCH_PERFMON_EVENTSEL0 + 2, MSR_ARCH_PERFMON_EVENTSEL0 + 3,
1170 MSR_ARCH_PERFMON_EVENTSEL0 + 4, MSR_ARCH_PERFMON_EVENTSEL0 + 5,
1171 MSR_ARCH_PERFMON_EVENTSEL0 + 6, MSR_ARCH_PERFMON_EVENTSEL0 + 7,
1172 MSR_ARCH_PERFMON_EVENTSEL0 + 8, MSR_ARCH_PERFMON_EVENTSEL0 + 9,
1173 MSR_ARCH_PERFMON_EVENTSEL0 + 10, MSR_ARCH_PERFMON_EVENTSEL0 + 11,
1174 MSR_ARCH_PERFMON_EVENTSEL0 + 12, MSR_ARCH_PERFMON_EVENTSEL0 + 13,
1175 MSR_ARCH_PERFMON_EVENTSEL0 + 14, MSR_ARCH_PERFMON_EVENTSEL0 + 15,
1176 MSR_ARCH_PERFMON_EVENTSEL0 + 16, MSR_ARCH_PERFMON_EVENTSEL0 + 17,
043405e1
CO
1177};
1178
7a5ee6ed 1179static u32 msrs_to_save[ARRAY_SIZE(msrs_to_save_all)];
043405e1
CO
1180static unsigned num_msrs_to_save;
1181
7a5ee6ed 1182static const u32 emulated_msrs_all[] = {
62ef68bb
PB
1183 MSR_KVM_SYSTEM_TIME, MSR_KVM_WALL_CLOCK,
1184 MSR_KVM_SYSTEM_TIME_NEW, MSR_KVM_WALL_CLOCK_NEW,
1185 HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
1186 HV_X64_MSR_TIME_REF_COUNT, HV_X64_MSR_REFERENCE_TSC,
72c139ba 1187 HV_X64_MSR_TSC_FREQUENCY, HV_X64_MSR_APIC_FREQUENCY,
e7d9513b
AS
1188 HV_X64_MSR_CRASH_P0, HV_X64_MSR_CRASH_P1, HV_X64_MSR_CRASH_P2,
1189 HV_X64_MSR_CRASH_P3, HV_X64_MSR_CRASH_P4, HV_X64_MSR_CRASH_CTL,
e516cebb 1190 HV_X64_MSR_RESET,
11c4b1ca 1191 HV_X64_MSR_VP_INDEX,
9eec50b8 1192 HV_X64_MSR_VP_RUNTIME,
5c919412 1193 HV_X64_MSR_SCONTROL,
1f4b34f8 1194 HV_X64_MSR_STIMER0_CONFIG,
d4abc577 1195 HV_X64_MSR_VP_ASSIST_PAGE,
a2e164e7
VK
1196 HV_X64_MSR_REENLIGHTENMENT_CONTROL, HV_X64_MSR_TSC_EMULATION_CONTROL,
1197 HV_X64_MSR_TSC_EMULATION_STATUS,
1198
1199 MSR_KVM_ASYNC_PF_EN, MSR_KVM_STEAL_TIME,
62ef68bb
PB
1200 MSR_KVM_PV_EOI_EN,
1201
ba904635 1202 MSR_IA32_TSC_ADJUST,
a3e06bbe 1203 MSR_IA32_TSCDEADLINE,
2bdb76c0 1204 MSR_IA32_ARCH_CAPABILITIES,
043405e1 1205 MSR_IA32_MISC_ENABLE,
908e75f3
AK
1206 MSR_IA32_MCG_STATUS,
1207 MSR_IA32_MCG_CTL,
c45dcc71 1208 MSR_IA32_MCG_EXT_CTL,
64d60670 1209 MSR_IA32_SMBASE,
52797bf9 1210 MSR_SMI_COUNT,
db2336a8
KH
1211 MSR_PLATFORM_INFO,
1212 MSR_MISC_FEATURES_ENABLES,
bc226f07 1213 MSR_AMD64_VIRT_SPEC_CTRL,
6c6a2ab9 1214 MSR_IA32_POWER_CTL,
191c8137 1215
95c5c7c7
PB
1216 /*
1217 * The following list leaves out MSRs whose values are determined
1218 * by arch/x86/kvm/vmx/nested.c based on CPUID or other MSRs.
1219 * We always support the "true" VMX control MSRs, even if the host
1220 * processor does not, so I am putting these registers here rather
7a5ee6ed 1221 * than in msrs_to_save_all.
95c5c7c7
PB
1222 */
1223 MSR_IA32_VMX_BASIC,
1224 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1225 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1226 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1227 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1228 MSR_IA32_VMX_MISC,
1229 MSR_IA32_VMX_CR0_FIXED0,
1230 MSR_IA32_VMX_CR4_FIXED0,
1231 MSR_IA32_VMX_VMCS_ENUM,
1232 MSR_IA32_VMX_PROCBASED_CTLS2,
1233 MSR_IA32_VMX_EPT_VPID_CAP,
1234 MSR_IA32_VMX_VMFUNC,
1235
191c8137 1236 MSR_K7_HWCR,
2d5ba19b 1237 MSR_KVM_POLL_CONTROL,
043405e1
CO
1238};
1239
7a5ee6ed 1240static u32 emulated_msrs[ARRAY_SIZE(emulated_msrs_all)];
62ef68bb
PB
1241static unsigned num_emulated_msrs;
1242
801e459a
TL
1243/*
1244 * List of msr numbers which are used to expose MSR-based features that
1245 * can be used by a hypervisor to validate requested CPU features.
1246 */
7a5ee6ed 1247static const u32 msr_based_features_all[] = {
1389309c
PB
1248 MSR_IA32_VMX_BASIC,
1249 MSR_IA32_VMX_TRUE_PINBASED_CTLS,
1250 MSR_IA32_VMX_PINBASED_CTLS,
1251 MSR_IA32_VMX_TRUE_PROCBASED_CTLS,
1252 MSR_IA32_VMX_PROCBASED_CTLS,
1253 MSR_IA32_VMX_TRUE_EXIT_CTLS,
1254 MSR_IA32_VMX_EXIT_CTLS,
1255 MSR_IA32_VMX_TRUE_ENTRY_CTLS,
1256 MSR_IA32_VMX_ENTRY_CTLS,
1257 MSR_IA32_VMX_MISC,
1258 MSR_IA32_VMX_CR0_FIXED0,
1259 MSR_IA32_VMX_CR0_FIXED1,
1260 MSR_IA32_VMX_CR4_FIXED0,
1261 MSR_IA32_VMX_CR4_FIXED1,
1262 MSR_IA32_VMX_VMCS_ENUM,
1263 MSR_IA32_VMX_PROCBASED_CTLS2,
1264 MSR_IA32_VMX_EPT_VPID_CAP,
1265 MSR_IA32_VMX_VMFUNC,
1266
d1d93fa9 1267 MSR_F10H_DECFG,
518e7b94 1268 MSR_IA32_UCODE_REV,
cd283252 1269 MSR_IA32_ARCH_CAPABILITIES,
801e459a
TL
1270};
1271
7a5ee6ed 1272static u32 msr_based_features[ARRAY_SIZE(msr_based_features_all)];
801e459a
TL
1273static unsigned int num_msr_based_features;
1274
4d22c17c 1275static u64 kvm_get_arch_capabilities(void)
5b76a3cf 1276{
4d22c17c 1277 u64 data = 0;
5b76a3cf 1278
4d22c17c
XL
1279 if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
1280 rdmsrl(MSR_IA32_ARCH_CAPABILITIES, data);
5b76a3cf 1281
b8e8c830
PB
1282 /*
1283 * If nx_huge_pages is enabled, KVM's shadow paging will ensure that
1284 * the nested hypervisor runs with NX huge pages. If it is not,
1285 * L1 is anyway vulnerable to ITLB_MULTIHIT explots from other
1286 * L1 guests, so it need not worry about its own (L2) guests.
1287 */
1288 data |= ARCH_CAP_PSCHANGE_MC_NO;
1289
5b76a3cf
PB
1290 /*
1291 * If we're doing cache flushes (either "always" or "cond")
1292 * we will do one whenever the guest does a vmlaunch/vmresume.
1293 * If an outer hypervisor is doing the cache flush for us
1294 * (VMENTER_L1D_FLUSH_NESTED_VM), we can safely pass that
1295 * capability to the guest too, and if EPT is disabled we're not
1296 * vulnerable. Overall, only VMENTER_L1D_FLUSH_NEVER will
1297 * require a nested hypervisor to do a flush of its own.
1298 */
1299 if (l1tf_vmx_mitigation != VMENTER_L1D_FLUSH_NEVER)
1300 data |= ARCH_CAP_SKIP_VMENTRY_L1DFLUSH;
1301
0c54914d
PB
1302 if (!boot_cpu_has_bug(X86_BUG_CPU_MELTDOWN))
1303 data |= ARCH_CAP_RDCL_NO;
1304 if (!boot_cpu_has_bug(X86_BUG_SPEC_STORE_BYPASS))
1305 data |= ARCH_CAP_SSB_NO;
1306 if (!boot_cpu_has_bug(X86_BUG_MDS))
1307 data |= ARCH_CAP_MDS_NO;
1308
e1d38b63 1309 /*
c11f83e0
PB
1310 * On TAA affected systems:
1311 * - nothing to do if TSX is disabled on the host.
1312 * - we emulate TSX_CTRL if present on the host.
1313 * This lets the guest use VERW to clear CPU buffers.
e1d38b63 1314 */
cbbaa272 1315 if (!boot_cpu_has(X86_FEATURE_RTM))
c11f83e0 1316 data &= ~(ARCH_CAP_TAA_NO | ARCH_CAP_TSX_CTRL_MSR);
cbbaa272
PB
1317 else if (!boot_cpu_has_bug(X86_BUG_TAA))
1318 data |= ARCH_CAP_TAA_NO;
e1d38b63 1319
5b76a3cf
PB
1320 return data;
1321}
5b76a3cf 1322
66421c1e
WL
1323static int kvm_get_msr_feature(struct kvm_msr_entry *msr)
1324{
1325 switch (msr->index) {
cd283252 1326 case MSR_IA32_ARCH_CAPABILITIES:
5b76a3cf
PB
1327 msr->data = kvm_get_arch_capabilities();
1328 break;
1329 case MSR_IA32_UCODE_REV:
cd283252 1330 rdmsrl_safe(msr->index, &msr->data);
518e7b94 1331 break;
66421c1e
WL
1332 default:
1333 if (kvm_x86_ops->get_msr_feature(msr))
1334 return 1;
1335 }
1336 return 0;
1337}
1338
801e459a
TL
1339static int do_get_msr_feature(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1340{
1341 struct kvm_msr_entry msr;
66421c1e 1342 int r;
801e459a
TL
1343
1344 msr.index = index;
66421c1e
WL
1345 r = kvm_get_msr_feature(&msr);
1346 if (r)
1347 return r;
801e459a
TL
1348
1349 *data = msr.data;
1350
1351 return 0;
1352}
1353
11988499 1354static bool __kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
15c4a640 1355{
1b4d56b8 1356 if (efer & EFER_FFXSR && !guest_cpuid_has(vcpu, X86_FEATURE_FXSR_OPT))
11988499 1357 return false;
1b2fd70c 1358
1b4d56b8 1359 if (efer & EFER_SVME && !guest_cpuid_has(vcpu, X86_FEATURE_SVM))
11988499 1360 return false;
d8017474 1361
0a629563
SC
1362 if (efer & (EFER_LME | EFER_LMA) &&
1363 !guest_cpuid_has(vcpu, X86_FEATURE_LM))
1364 return false;
1365
1366 if (efer & EFER_NX && !guest_cpuid_has(vcpu, X86_FEATURE_NX))
1367 return false;
d8017474 1368
384bb783 1369 return true;
11988499
SC
1370
1371}
1372bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer)
1373{
1374 if (efer & efer_reserved_bits)
1375 return false;
1376
1377 return __kvm_valid_efer(vcpu, efer);
384bb783
JK
1378}
1379EXPORT_SYMBOL_GPL(kvm_valid_efer);
1380
11988499 1381static int set_efer(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
384bb783
JK
1382{
1383 u64 old_efer = vcpu->arch.efer;
11988499 1384 u64 efer = msr_info->data;
384bb783 1385
11988499 1386 if (efer & efer_reserved_bits)
66f61c92 1387 return 1;
384bb783 1388
11988499
SC
1389 if (!msr_info->host_initiated) {
1390 if (!__kvm_valid_efer(vcpu, efer))
1391 return 1;
1392
1393 if (is_paging(vcpu) &&
1394 (vcpu->arch.efer & EFER_LME) != (efer & EFER_LME))
1395 return 1;
1396 }
384bb783 1397
15c4a640 1398 efer &= ~EFER_LMA;
f6801dff 1399 efer |= vcpu->arch.efer & EFER_LMA;
15c4a640 1400
a3d204e2
SY
1401 kvm_x86_ops->set_efer(vcpu, efer);
1402
aad82703
SY
1403 /* Update reserved bits */
1404 if ((efer ^ old_efer) & EFER_NX)
1405 kvm_mmu_reset_context(vcpu);
1406
b69e8cae 1407 return 0;
15c4a640
CO
1408}
1409
f2b4b7dd
JR
1410void kvm_enable_efer_bits(u64 mask)
1411{
1412 efer_reserved_bits &= ~mask;
1413}
1414EXPORT_SYMBOL_GPL(kvm_enable_efer_bits);
1415
15c4a640 1416/*
f20935d8
SC
1417 * Write @data into the MSR specified by @index. Select MSR specific fault
1418 * checks are bypassed if @host_initiated is %true.
15c4a640
CO
1419 * Returns 0 on success, non-0 otherwise.
1420 * Assumes vcpu_load() was already called.
1421 */
f20935d8
SC
1422static int __kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data,
1423 bool host_initiated)
15c4a640 1424{
f20935d8
SC
1425 struct msr_data msr;
1426
1427 switch (index) {
854e8bb1
NA
1428 case MSR_FS_BASE:
1429 case MSR_GS_BASE:
1430 case MSR_KERNEL_GS_BASE:
1431 case MSR_CSTAR:
1432 case MSR_LSTAR:
f20935d8 1433 if (is_noncanonical_address(data, vcpu))
854e8bb1
NA
1434 return 1;
1435 break;
1436 case MSR_IA32_SYSENTER_EIP:
1437 case MSR_IA32_SYSENTER_ESP:
1438 /*
1439 * IA32_SYSENTER_ESP and IA32_SYSENTER_EIP cause #GP if
1440 * non-canonical address is written on Intel but not on
1441 * AMD (which ignores the top 32-bits, because it does
1442 * not implement 64-bit SYSENTER).
1443 *
1444 * 64-bit code should hence be able to write a non-canonical
1445 * value on AMD. Making the address canonical ensures that
1446 * vmentry does not fail on Intel after writing a non-canonical
1447 * value, and that something deterministic happens if the guest
1448 * invokes 64-bit SYSENTER.
1449 */
f20935d8 1450 data = get_canonical(data, vcpu_virt_addr_bits(vcpu));
854e8bb1 1451 }
f20935d8
SC
1452
1453 msr.data = data;
1454 msr.index = index;
1455 msr.host_initiated = host_initiated;
1456
1457 return kvm_x86_ops->set_msr(vcpu, &msr);
15c4a640
CO
1458}
1459
313a3dc7 1460/*
f20935d8
SC
1461 * Read the MSR specified by @index into @data. Select MSR specific fault
1462 * checks are bypassed if @host_initiated is %true.
1463 * Returns 0 on success, non-0 otherwise.
1464 * Assumes vcpu_load() was already called.
313a3dc7 1465 */
edef5c36
PB
1466int __kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data,
1467 bool host_initiated)
609e36d3
PB
1468{
1469 struct msr_data msr;
f20935d8 1470 int ret;
609e36d3
PB
1471
1472 msr.index = index;
f20935d8 1473 msr.host_initiated = host_initiated;
609e36d3 1474
f20935d8
SC
1475 ret = kvm_x86_ops->get_msr(vcpu, &msr);
1476 if (!ret)
1477 *data = msr.data;
1478 return ret;
609e36d3
PB
1479}
1480
f20935d8 1481int kvm_get_msr(struct kvm_vcpu *vcpu, u32 index, u64 *data)
313a3dc7 1482{
f20935d8
SC
1483 return __kvm_get_msr(vcpu, index, data, false);
1484}
1485EXPORT_SYMBOL_GPL(kvm_get_msr);
8fe8ab46 1486
f20935d8
SC
1487int kvm_set_msr(struct kvm_vcpu *vcpu, u32 index, u64 data)
1488{
1489 return __kvm_set_msr(vcpu, index, data, false);
1490}
1491EXPORT_SYMBOL_GPL(kvm_set_msr);
1492
1edce0a9
SC
1493int kvm_emulate_rdmsr(struct kvm_vcpu *vcpu)
1494{
1495 u32 ecx = kvm_rcx_read(vcpu);
1496 u64 data;
1497
1498 if (kvm_get_msr(vcpu, ecx, &data)) {
1499 trace_kvm_msr_read_ex(ecx);
1500 kvm_inject_gp(vcpu, 0);
1501 return 1;
1502 }
1503
1504 trace_kvm_msr_read(ecx, data);
1505
1506 kvm_rax_write(vcpu, data & -1u);
1507 kvm_rdx_write(vcpu, (data >> 32) & -1u);
1508 return kvm_skip_emulated_instruction(vcpu);
1509}
1510EXPORT_SYMBOL_GPL(kvm_emulate_rdmsr);
1511
1512int kvm_emulate_wrmsr(struct kvm_vcpu *vcpu)
1513{
1514 u32 ecx = kvm_rcx_read(vcpu);
1515 u64 data = kvm_read_edx_eax(vcpu);
1516
1517 if (kvm_set_msr(vcpu, ecx, data)) {
1518 trace_kvm_msr_write_ex(ecx, data);
1519 kvm_inject_gp(vcpu, 0);
1520 return 1;
1521 }
1522
1523 trace_kvm_msr_write(ecx, data);
1524 return kvm_skip_emulated_instruction(vcpu);
1525}
1526EXPORT_SYMBOL_GPL(kvm_emulate_wrmsr);
1527
f20935d8
SC
1528/*
1529 * Adapt set_msr() to msr_io()'s calling convention
1530 */
1531static int do_get_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1532{
1533 return __kvm_get_msr(vcpu, index, data, true);
1534}
1535
1536static int do_set_msr(struct kvm_vcpu *vcpu, unsigned index, u64 *data)
1537{
1538 return __kvm_set_msr(vcpu, index, *data, true);
313a3dc7
CO
1539}
1540
16e8d74d 1541#ifdef CONFIG_X86_64
53fafdbb
MT
1542struct pvclock_clock {
1543 int vclock_mode;
1544 u64 cycle_last;
1545 u64 mask;
1546 u32 mult;
1547 u32 shift;
1548};
1549
16e8d74d
MT
1550struct pvclock_gtod_data {
1551 seqcount_t seq;
1552
53fafdbb
MT
1553 struct pvclock_clock clock; /* extract of a clocksource struct */
1554 struct pvclock_clock raw_clock; /* extract of a clocksource struct */
16e8d74d 1555
53fafdbb 1556 u64 boot_ns_raw;
cbcf2dd3
TG
1557 u64 boot_ns;
1558 u64 nsec_base;
55dd00a7 1559 u64 wall_time_sec;
53fafdbb 1560 u64 monotonic_raw_nsec;
16e8d74d
MT
1561};
1562
1563static struct pvclock_gtod_data pvclock_gtod_data;
1564
1565static void update_pvclock_gtod(struct timekeeper *tk)
1566{
1567 struct pvclock_gtod_data *vdata = &pvclock_gtod_data;
53fafdbb 1568 u64 boot_ns, boot_ns_raw;
cbcf2dd3 1569
876e7881 1570 boot_ns = ktime_to_ns(ktime_add(tk->tkr_mono.base, tk->offs_boot));
53fafdbb 1571 boot_ns_raw = ktime_to_ns(ktime_add(tk->tkr_raw.base, tk->offs_boot));
16e8d74d
MT
1572
1573 write_seqcount_begin(&vdata->seq);
1574
1575 /* copy pvclock gtod data */
876e7881
PZ
1576 vdata->clock.vclock_mode = tk->tkr_mono.clock->archdata.vclock_mode;
1577 vdata->clock.cycle_last = tk->tkr_mono.cycle_last;
1578 vdata->clock.mask = tk->tkr_mono.mask;
1579 vdata->clock.mult = tk->tkr_mono.mult;
1580 vdata->clock.shift = tk->tkr_mono.shift;
16e8d74d 1581
53fafdbb
MT
1582 vdata->raw_clock.vclock_mode = tk->tkr_raw.clock->archdata.vclock_mode;
1583 vdata->raw_clock.cycle_last = tk->tkr_raw.cycle_last;
1584 vdata->raw_clock.mask = tk->tkr_raw.mask;
1585 vdata->raw_clock.mult = tk->tkr_raw.mult;
1586 vdata->raw_clock.shift = tk->tkr_raw.shift;
1587
cbcf2dd3 1588 vdata->boot_ns = boot_ns;
876e7881 1589 vdata->nsec_base = tk->tkr_mono.xtime_nsec;
16e8d74d 1590
55dd00a7
MT
1591 vdata->wall_time_sec = tk->xtime_sec;
1592
53fafdbb
MT
1593 vdata->boot_ns_raw = boot_ns_raw;
1594 vdata->monotonic_raw_nsec = tk->tkr_raw.xtime_nsec;
1595
16e8d74d
MT
1596 write_seqcount_end(&vdata->seq);
1597}
1598#endif
1599
bab5bb39
NK
1600void kvm_set_pending_timer(struct kvm_vcpu *vcpu)
1601{
bab5bb39 1602 kvm_make_request(KVM_REQ_PENDING_TIMER, vcpu);
4d151bf3 1603 kvm_vcpu_kick(vcpu);
bab5bb39 1604}
16e8d74d 1605
18068523
GOC
1606static void kvm_write_wall_clock(struct kvm *kvm, gpa_t wall_clock)
1607{
9ed3c444
AK
1608 int version;
1609 int r;
50d0a0f9 1610 struct pvclock_wall_clock wc;
87aeb54f 1611 struct timespec64 boot;
18068523
GOC
1612
1613 if (!wall_clock)
1614 return;
1615
9ed3c444
AK
1616 r = kvm_read_guest(kvm, wall_clock, &version, sizeof(version));
1617 if (r)
1618 return;
1619
1620 if (version & 1)
1621 ++version; /* first time write, random junk */
1622
1623 ++version;
18068523 1624
1dab1345
NK
1625 if (kvm_write_guest(kvm, wall_clock, &version, sizeof(version)))
1626 return;
18068523 1627
50d0a0f9
GH
1628 /*
1629 * The guest calculates current wall clock time by adding
34c238a1 1630 * system time (updated by kvm_guest_time_update below) to the
50d0a0f9
GH
1631 * wall clock specified here. guest system time equals host
1632 * system time for us, thus we must fill in host boot time here.
1633 */
87aeb54f 1634 getboottime64(&boot);
50d0a0f9 1635
4b648665 1636 if (kvm->arch.kvmclock_offset) {
87aeb54f
AB
1637 struct timespec64 ts = ns_to_timespec64(kvm->arch.kvmclock_offset);
1638 boot = timespec64_sub(boot, ts);
4b648665 1639 }
87aeb54f 1640 wc.sec = (u32)boot.tv_sec; /* overflow in 2106 guest time */
50d0a0f9
GH
1641 wc.nsec = boot.tv_nsec;
1642 wc.version = version;
18068523
GOC
1643
1644 kvm_write_guest(kvm, wall_clock, &wc, sizeof(wc));
1645
1646 version++;
1647 kvm_write_guest(kvm, wall_clock, &version, sizeof(version));
18068523
GOC
1648}
1649
50d0a0f9
GH
1650static uint32_t div_frac(uint32_t dividend, uint32_t divisor)
1651{
b51012de
PB
1652 do_shl32_div32(dividend, divisor);
1653 return dividend;
50d0a0f9
GH
1654}
1655
3ae13faa 1656static void kvm_get_time_scale(uint64_t scaled_hz, uint64_t base_hz,
5f4e3f88 1657 s8 *pshift, u32 *pmultiplier)
50d0a0f9 1658{
5f4e3f88 1659 uint64_t scaled64;
50d0a0f9
GH
1660 int32_t shift = 0;
1661 uint64_t tps64;
1662 uint32_t tps32;
1663
3ae13faa
PB
1664 tps64 = base_hz;
1665 scaled64 = scaled_hz;
50933623 1666 while (tps64 > scaled64*2 || tps64 & 0xffffffff00000000ULL) {
50d0a0f9
GH
1667 tps64 >>= 1;
1668 shift--;
1669 }
1670
1671 tps32 = (uint32_t)tps64;
50933623
JK
1672 while (tps32 <= scaled64 || scaled64 & 0xffffffff00000000ULL) {
1673 if (scaled64 & 0xffffffff00000000ULL || tps32 & 0x80000000)
5f4e3f88
ZA
1674 scaled64 >>= 1;
1675 else
1676 tps32 <<= 1;
50d0a0f9
GH
1677 shift++;
1678 }
1679
5f4e3f88
ZA
1680 *pshift = shift;
1681 *pmultiplier = div_frac(scaled64, tps32);
50d0a0f9
GH
1682}
1683
d828199e 1684#ifdef CONFIG_X86_64
16e8d74d 1685static atomic_t kvm_guest_has_master_clock = ATOMIC_INIT(0);
d828199e 1686#endif
16e8d74d 1687
c8076604 1688static DEFINE_PER_CPU(unsigned long, cpu_tsc_khz);
69b0049a 1689static unsigned long max_tsc_khz;
c8076604 1690
cc578287 1691static u32 adjust_tsc_khz(u32 khz, s32 ppm)
1e993611 1692{
cc578287
ZA
1693 u64 v = (u64)khz * (1000000 + ppm);
1694 do_div(v, 1000000);
1695 return v;
1e993611
JR
1696}
1697
381d585c
HZ
1698static int set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale)
1699{
1700 u64 ratio;
1701
1702 /* Guest TSC same frequency as host TSC? */
1703 if (!scale) {
1704 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
1705 return 0;
1706 }
1707
1708 /* TSC scaling supported? */
1709 if (!kvm_has_tsc_control) {
1710 if (user_tsc_khz > tsc_khz) {
1711 vcpu->arch.tsc_catchup = 1;
1712 vcpu->arch.tsc_always_catchup = 1;
1713 return 0;
1714 } else {
3f16a5c3 1715 pr_warn_ratelimited("user requested TSC rate below hardware speed\n");
381d585c
HZ
1716 return -1;
1717 }
1718 }
1719
1720 /* TSC scaling required - calculate ratio */
1721 ratio = mul_u64_u32_div(1ULL << kvm_tsc_scaling_ratio_frac_bits,
1722 user_tsc_khz, tsc_khz);
1723
1724 if (ratio == 0 || ratio >= kvm_max_tsc_scaling_ratio) {
3f16a5c3
PB
1725 pr_warn_ratelimited("Invalid TSC scaling ratio - virtual-tsc-khz=%u\n",
1726 user_tsc_khz);
381d585c
HZ
1727 return -1;
1728 }
1729
1730 vcpu->arch.tsc_scaling_ratio = ratio;
1731 return 0;
1732}
1733
4941b8cb 1734static int kvm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz)
759379dd 1735{
cc578287
ZA
1736 u32 thresh_lo, thresh_hi;
1737 int use_scaling = 0;
217fc9cf 1738
03ba32ca 1739 /* tsc_khz can be zero if TSC calibration fails */
4941b8cb 1740 if (user_tsc_khz == 0) {
ad721883
HZ
1741 /* set tsc_scaling_ratio to a safe value */
1742 vcpu->arch.tsc_scaling_ratio = kvm_default_tsc_scaling_ratio;
381d585c 1743 return -1;
ad721883 1744 }
03ba32ca 1745
c285545f 1746 /* Compute a scale to convert nanoseconds in TSC cycles */
3ae13faa 1747 kvm_get_time_scale(user_tsc_khz * 1000LL, NSEC_PER_SEC,
cc578287
ZA
1748 &vcpu->arch.virtual_tsc_shift,
1749 &vcpu->arch.virtual_tsc_mult);
4941b8cb 1750 vcpu->arch.virtual_tsc_khz = user_tsc_khz;
cc578287
ZA
1751
1752 /*
1753 * Compute the variation in TSC rate which is acceptable
1754 * within the range of tolerance and decide if the
1755 * rate being applied is within that bounds of the hardware
1756 * rate. If so, no scaling or compensation need be done.
1757 */
1758 thresh_lo = adjust_tsc_khz(tsc_khz, -tsc_tolerance_ppm);
1759 thresh_hi = adjust_tsc_khz(tsc_khz, tsc_tolerance_ppm);
4941b8cb
PB
1760 if (user_tsc_khz < thresh_lo || user_tsc_khz > thresh_hi) {
1761 pr_debug("kvm: requested TSC rate %u falls outside tolerance [%u,%u]\n", user_tsc_khz, thresh_lo, thresh_hi);
cc578287
ZA
1762 use_scaling = 1;
1763 }
4941b8cb 1764 return set_tsc_khz(vcpu, user_tsc_khz, use_scaling);
c285545f
ZA
1765}
1766
1767static u64 compute_guest_tsc(struct kvm_vcpu *vcpu, s64 kernel_ns)
1768{
e26101b1 1769 u64 tsc = pvclock_scale_delta(kernel_ns-vcpu->arch.this_tsc_nsec,
cc578287
ZA
1770 vcpu->arch.virtual_tsc_mult,
1771 vcpu->arch.virtual_tsc_shift);
e26101b1 1772 tsc += vcpu->arch.this_tsc_write;
c285545f
ZA
1773 return tsc;
1774}
1775
b0c39dc6
VK
1776static inline int gtod_is_based_on_tsc(int mode)
1777{
1778 return mode == VCLOCK_TSC || mode == VCLOCK_HVCLOCK;
1779}
1780
69b0049a 1781static void kvm_track_tsc_matching(struct kvm_vcpu *vcpu)
b48aa97e
MT
1782{
1783#ifdef CONFIG_X86_64
1784 bool vcpus_matched;
b48aa97e
MT
1785 struct kvm_arch *ka = &vcpu->kvm->arch;
1786 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
1787
1788 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
1789 atomic_read(&vcpu->kvm->online_vcpus));
1790
7f187922
MT
1791 /*
1792 * Once the masterclock is enabled, always perform request in
1793 * order to update it.
1794 *
1795 * In order to enable masterclock, the host clocksource must be TSC
1796 * and the vcpus need to have matched TSCs. When that happens,
1797 * perform request to enable masterclock.
1798 */
1799 if (ka->use_master_clock ||
b0c39dc6 1800 (gtod_is_based_on_tsc(gtod->clock.vclock_mode) && vcpus_matched))
b48aa97e
MT
1801 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
1802
1803 trace_kvm_track_tsc(vcpu->vcpu_id, ka->nr_vcpus_matched_tsc,
1804 atomic_read(&vcpu->kvm->online_vcpus),
1805 ka->use_master_clock, gtod->clock.vclock_mode);
1806#endif
1807}
1808
ba904635
WA
1809static void update_ia32_tsc_adjust_msr(struct kvm_vcpu *vcpu, s64 offset)
1810{
e79f245d 1811 u64 curr_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
ba904635
WA
1812 vcpu->arch.ia32_tsc_adjust_msr += offset - curr_offset;
1813}
1814
35181e86
HZ
1815/*
1816 * Multiply tsc by a fixed point number represented by ratio.
1817 *
1818 * The most significant 64-N bits (mult) of ratio represent the
1819 * integral part of the fixed point number; the remaining N bits
1820 * (frac) represent the fractional part, ie. ratio represents a fixed
1821 * point number (mult + frac * 2^(-N)).
1822 *
1823 * N equals to kvm_tsc_scaling_ratio_frac_bits.
1824 */
1825static inline u64 __scale_tsc(u64 ratio, u64 tsc)
1826{
1827 return mul_u64_u64_shr(tsc, ratio, kvm_tsc_scaling_ratio_frac_bits);
1828}
1829
1830u64 kvm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc)
1831{
1832 u64 _tsc = tsc;
1833 u64 ratio = vcpu->arch.tsc_scaling_ratio;
1834
1835 if (ratio != kvm_default_tsc_scaling_ratio)
1836 _tsc = __scale_tsc(ratio, tsc);
1837
1838 return _tsc;
1839}
1840EXPORT_SYMBOL_GPL(kvm_scale_tsc);
1841
07c1419a
HZ
1842static u64 kvm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc)
1843{
1844 u64 tsc;
1845
1846 tsc = kvm_scale_tsc(vcpu, rdtsc());
1847
1848 return target_tsc - tsc;
1849}
1850
4ba76538
HZ
1851u64 kvm_read_l1_tsc(struct kvm_vcpu *vcpu, u64 host_tsc)
1852{
e79f245d
KA
1853 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1854
1855 return tsc_offset + kvm_scale_tsc(vcpu, host_tsc);
4ba76538
HZ
1856}
1857EXPORT_SYMBOL_GPL(kvm_read_l1_tsc);
1858
a545ab6a
LC
1859static void kvm_vcpu_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
1860{
326e7425 1861 vcpu->arch.tsc_offset = kvm_x86_ops->write_l1_tsc_offset(vcpu, offset);
a545ab6a
LC
1862}
1863
b0c39dc6
VK
1864static inline bool kvm_check_tsc_unstable(void)
1865{
1866#ifdef CONFIG_X86_64
1867 /*
1868 * TSC is marked unstable when we're running on Hyper-V,
1869 * 'TSC page' clocksource is good.
1870 */
1871 if (pvclock_gtod_data.clock.vclock_mode == VCLOCK_HVCLOCK)
1872 return false;
1873#endif
1874 return check_tsc_unstable();
1875}
1876
8fe8ab46 1877void kvm_write_tsc(struct kvm_vcpu *vcpu, struct msr_data *msr)
99e3e30a
ZA
1878{
1879 struct kvm *kvm = vcpu->kvm;
f38e098f 1880 u64 offset, ns, elapsed;
99e3e30a 1881 unsigned long flags;
b48aa97e 1882 bool matched;
0d3da0d2 1883 bool already_matched;
8fe8ab46 1884 u64 data = msr->data;
c5e8ec8e 1885 bool synchronizing = false;
99e3e30a 1886
038f8c11 1887 raw_spin_lock_irqsave(&kvm->arch.tsc_write_lock, flags);
07c1419a 1888 offset = kvm_compute_tsc_offset(vcpu, data);
9285ec4c 1889 ns = ktime_get_boottime_ns();
f38e098f 1890 elapsed = ns - kvm->arch.last_tsc_nsec;
5d3cb0f6 1891
03ba32ca 1892 if (vcpu->arch.virtual_tsc_khz) {
bd8fab39
DP
1893 if (data == 0 && msr->host_initiated) {
1894 /*
1895 * detection of vcpu initialization -- need to sync
1896 * with other vCPUs. This particularly helps to keep
1897 * kvm_clock stable after CPU hotplug
1898 */
1899 synchronizing = true;
1900 } else {
1901 u64 tsc_exp = kvm->arch.last_tsc_write +
1902 nsec_to_cycles(vcpu, elapsed);
1903 u64 tsc_hz = vcpu->arch.virtual_tsc_khz * 1000LL;
1904 /*
1905 * Special case: TSC write with a small delta (1 second)
1906 * of virtual cycle time against real time is
1907 * interpreted as an attempt to synchronize the CPU.
1908 */
1909 synchronizing = data < tsc_exp + tsc_hz &&
1910 data + tsc_hz > tsc_exp;
1911 }
c5e8ec8e 1912 }
f38e098f
ZA
1913
1914 /*
5d3cb0f6
ZA
1915 * For a reliable TSC, we can match TSC offsets, and for an unstable
1916 * TSC, we add elapsed time in this computation. We could let the
1917 * compensation code attempt to catch up if we fall behind, but
1918 * it's better to try to match offsets from the beginning.
1919 */
c5e8ec8e 1920 if (synchronizing &&
5d3cb0f6 1921 vcpu->arch.virtual_tsc_khz == kvm->arch.last_tsc_khz) {
b0c39dc6 1922 if (!kvm_check_tsc_unstable()) {
e26101b1 1923 offset = kvm->arch.cur_tsc_offset;
f38e098f 1924 } else {
857e4099 1925 u64 delta = nsec_to_cycles(vcpu, elapsed);
5d3cb0f6 1926 data += delta;
07c1419a 1927 offset = kvm_compute_tsc_offset(vcpu, data);
f38e098f 1928 }
b48aa97e 1929 matched = true;
0d3da0d2 1930 already_matched = (vcpu->arch.this_tsc_generation == kvm->arch.cur_tsc_generation);
e26101b1
ZA
1931 } else {
1932 /*
1933 * We split periods of matched TSC writes into generations.
1934 * For each generation, we track the original measured
1935 * nanosecond time, offset, and write, so if TSCs are in
1936 * sync, we can match exact offset, and if not, we can match
4a969980 1937 * exact software computation in compute_guest_tsc()
e26101b1
ZA
1938 *
1939 * These values are tracked in kvm->arch.cur_xxx variables.
1940 */
1941 kvm->arch.cur_tsc_generation++;
1942 kvm->arch.cur_tsc_nsec = ns;
1943 kvm->arch.cur_tsc_write = data;
1944 kvm->arch.cur_tsc_offset = offset;
b48aa97e 1945 matched = false;
f38e098f 1946 }
e26101b1
ZA
1947
1948 /*
1949 * We also track th most recent recorded KHZ, write and time to
1950 * allow the matching interval to be extended at each write.
1951 */
f38e098f
ZA
1952 kvm->arch.last_tsc_nsec = ns;
1953 kvm->arch.last_tsc_write = data;
5d3cb0f6 1954 kvm->arch.last_tsc_khz = vcpu->arch.virtual_tsc_khz;
99e3e30a 1955
b183aa58 1956 vcpu->arch.last_guest_tsc = data;
e26101b1
ZA
1957
1958 /* Keep track of which generation this VCPU has synchronized to */
1959 vcpu->arch.this_tsc_generation = kvm->arch.cur_tsc_generation;
1960 vcpu->arch.this_tsc_nsec = kvm->arch.cur_tsc_nsec;
1961 vcpu->arch.this_tsc_write = kvm->arch.cur_tsc_write;
1962
d6321d49 1963 if (!msr->host_initiated && guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST))
ba904635 1964 update_ia32_tsc_adjust_msr(vcpu, offset);
d6321d49 1965
a545ab6a 1966 kvm_vcpu_write_tsc_offset(vcpu, offset);
e26101b1 1967 raw_spin_unlock_irqrestore(&kvm->arch.tsc_write_lock, flags);
b48aa97e
MT
1968
1969 spin_lock(&kvm->arch.pvclock_gtod_sync_lock);
0d3da0d2 1970 if (!matched) {
b48aa97e 1971 kvm->arch.nr_vcpus_matched_tsc = 0;
0d3da0d2
TG
1972 } else if (!already_matched) {
1973 kvm->arch.nr_vcpus_matched_tsc++;
1974 }
b48aa97e
MT
1975
1976 kvm_track_tsc_matching(vcpu);
1977 spin_unlock(&kvm->arch.pvclock_gtod_sync_lock);
99e3e30a 1978}
e26101b1 1979
99e3e30a
ZA
1980EXPORT_SYMBOL_GPL(kvm_write_tsc);
1981
58ea6767
HZ
1982static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
1983 s64 adjustment)
1984{
326e7425
LS
1985 u64 tsc_offset = kvm_x86_ops->read_l1_tsc_offset(vcpu);
1986 kvm_vcpu_write_tsc_offset(vcpu, tsc_offset + adjustment);
58ea6767
HZ
1987}
1988
1989static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
1990{
1991 if (vcpu->arch.tsc_scaling_ratio != kvm_default_tsc_scaling_ratio)
1992 WARN_ON(adjustment < 0);
1993 adjustment = kvm_scale_tsc(vcpu, (u64) adjustment);
ea26e4ec 1994 adjust_tsc_offset_guest(vcpu, adjustment);
58ea6767
HZ
1995}
1996
d828199e
MT
1997#ifdef CONFIG_X86_64
1998
a5a1d1c2 1999static u64 read_tsc(void)
d828199e 2000{
a5a1d1c2 2001 u64 ret = (u64)rdtsc_ordered();
03b9730b 2002 u64 last = pvclock_gtod_data.clock.cycle_last;
d828199e
MT
2003
2004 if (likely(ret >= last))
2005 return ret;
2006
2007 /*
2008 * GCC likes to generate cmov here, but this branch is extremely
6a6256f9 2009 * predictable (it's just a function of time and the likely is
d828199e
MT
2010 * very likely) and there's a data dependence, so force GCC
2011 * to generate a branch instead. I don't barrier() because
2012 * we don't actually need a barrier, and if this function
2013 * ever gets inlined it will generate worse code.
2014 */
2015 asm volatile ("");
2016 return last;
2017}
2018
53fafdbb
MT
2019static inline u64 vgettsc(struct pvclock_clock *clock, u64 *tsc_timestamp,
2020 int *mode)
d828199e
MT
2021{
2022 long v;
b0c39dc6
VK
2023 u64 tsc_pg_val;
2024
53fafdbb 2025 switch (clock->vclock_mode) {
b0c39dc6
VK
2026 case VCLOCK_HVCLOCK:
2027 tsc_pg_val = hv_read_tsc_page_tsc(hv_get_tsc_page(),
2028 tsc_timestamp);
2029 if (tsc_pg_val != U64_MAX) {
2030 /* TSC page valid */
2031 *mode = VCLOCK_HVCLOCK;
53fafdbb
MT
2032 v = (tsc_pg_val - clock->cycle_last) &
2033 clock->mask;
b0c39dc6
VK
2034 } else {
2035 /* TSC page invalid */
2036 *mode = VCLOCK_NONE;
2037 }
2038 break;
2039 case VCLOCK_TSC:
2040 *mode = VCLOCK_TSC;
2041 *tsc_timestamp = read_tsc();
53fafdbb
MT
2042 v = (*tsc_timestamp - clock->cycle_last) &
2043 clock->mask;
b0c39dc6
VK
2044 break;
2045 default:
2046 *mode = VCLOCK_NONE;
2047 }
d828199e 2048
b0c39dc6
VK
2049 if (*mode == VCLOCK_NONE)
2050 *tsc_timestamp = v = 0;
d828199e 2051
53fafdbb 2052 return v * clock->mult;
d828199e
MT
2053}
2054
53fafdbb 2055static int do_monotonic_raw(s64 *t, u64 *tsc_timestamp)
d828199e 2056{
cbcf2dd3 2057 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
d828199e 2058 unsigned long seq;
d828199e 2059 int mode;
cbcf2dd3 2060 u64 ns;
d828199e 2061
d828199e
MT
2062 do {
2063 seq = read_seqcount_begin(&gtod->seq);
53fafdbb
MT
2064 ns = gtod->monotonic_raw_nsec;
2065 ns += vgettsc(&gtod->raw_clock, tsc_timestamp, &mode);
d828199e 2066 ns >>= gtod->clock.shift;
53fafdbb 2067 ns += gtod->boot_ns_raw;
d828199e 2068 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
cbcf2dd3 2069 *t = ns;
d828199e
MT
2070
2071 return mode;
2072}
2073
899a31f5 2074static int do_realtime(struct timespec64 *ts, u64 *tsc_timestamp)
55dd00a7
MT
2075{
2076 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
2077 unsigned long seq;
2078 int mode;
2079 u64 ns;
2080
2081 do {
2082 seq = read_seqcount_begin(&gtod->seq);
55dd00a7
MT
2083 ts->tv_sec = gtod->wall_time_sec;
2084 ns = gtod->nsec_base;
53fafdbb 2085 ns += vgettsc(&gtod->clock, tsc_timestamp, &mode);
55dd00a7
MT
2086 ns >>= gtod->clock.shift;
2087 } while (unlikely(read_seqcount_retry(&gtod->seq, seq)));
2088
2089 ts->tv_sec += __iter_div_u64_rem(ns, NSEC_PER_SEC, &ns);
2090 ts->tv_nsec = ns;
2091
2092 return mode;
2093}
2094
b0c39dc6
VK
2095/* returns true if host is using TSC based clocksource */
2096static bool kvm_get_time_and_clockread(s64 *kernel_ns, u64 *tsc_timestamp)
d828199e 2097{
d828199e 2098 /* checked again under seqlock below */
b0c39dc6 2099 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
d828199e
MT
2100 return false;
2101
53fafdbb 2102 return gtod_is_based_on_tsc(do_monotonic_raw(kernel_ns,
b0c39dc6 2103 tsc_timestamp));
d828199e 2104}
55dd00a7 2105
b0c39dc6 2106/* returns true if host is using TSC based clocksource */
899a31f5 2107static bool kvm_get_walltime_and_clockread(struct timespec64 *ts,
b0c39dc6 2108 u64 *tsc_timestamp)
55dd00a7
MT
2109{
2110 /* checked again under seqlock below */
b0c39dc6 2111 if (!gtod_is_based_on_tsc(pvclock_gtod_data.clock.vclock_mode))
55dd00a7
MT
2112 return false;
2113
b0c39dc6 2114 return gtod_is_based_on_tsc(do_realtime(ts, tsc_timestamp));
55dd00a7 2115}
d828199e
MT
2116#endif
2117
2118/*
2119 *
b48aa97e
MT
2120 * Assuming a stable TSC across physical CPUS, and a stable TSC
2121 * across virtual CPUs, the following condition is possible.
2122 * Each numbered line represents an event visible to both
d828199e
MT
2123 * CPUs at the next numbered event.
2124 *
2125 * "timespecX" represents host monotonic time. "tscX" represents
2126 * RDTSC value.
2127 *
2128 * VCPU0 on CPU0 | VCPU1 on CPU1
2129 *
2130 * 1. read timespec0,tsc0
2131 * 2. | timespec1 = timespec0 + N
2132 * | tsc1 = tsc0 + M
2133 * 3. transition to guest | transition to guest
2134 * 4. ret0 = timespec0 + (rdtsc - tsc0) |
2135 * 5. | ret1 = timespec1 + (rdtsc - tsc1)
2136 * | ret1 = timespec0 + N + (rdtsc - (tsc0 + M))
2137 *
2138 * Since ret0 update is visible to VCPU1 at time 5, to obey monotonicity:
2139 *
2140 * - ret0 < ret1
2141 * - timespec0 + (rdtsc - tsc0) < timespec0 + N + (rdtsc - (tsc0 + M))
2142 * ...
2143 * - 0 < N - M => M < N
2144 *
2145 * That is, when timespec0 != timespec1, M < N. Unfortunately that is not
2146 * always the case (the difference between two distinct xtime instances
2147 * might be smaller then the difference between corresponding TSC reads,
2148 * when updating guest vcpus pvclock areas).
2149 *
2150 * To avoid that problem, do not allow visibility of distinct
2151 * system_timestamp/tsc_timestamp values simultaneously: use a master
2152 * copy of host monotonic time values. Update that master copy
2153 * in lockstep.
2154 *
b48aa97e 2155 * Rely on synchronization of host TSCs and guest TSCs for monotonicity.
d828199e
MT
2156 *
2157 */
2158
2159static void pvclock_update_vm_gtod_copy(struct kvm *kvm)
2160{
2161#ifdef CONFIG_X86_64
2162 struct kvm_arch *ka = &kvm->arch;
2163 int vclock_mode;
b48aa97e
MT
2164 bool host_tsc_clocksource, vcpus_matched;
2165
2166 vcpus_matched = (ka->nr_vcpus_matched_tsc + 1 ==
2167 atomic_read(&kvm->online_vcpus));
d828199e
MT
2168
2169 /*
2170 * If the host uses TSC clock, then passthrough TSC as stable
2171 * to the guest.
2172 */
b48aa97e 2173 host_tsc_clocksource = kvm_get_time_and_clockread(
d828199e
MT
2174 &ka->master_kernel_ns,
2175 &ka->master_cycle_now);
2176
16a96021 2177 ka->use_master_clock = host_tsc_clocksource && vcpus_matched
a826faf1 2178 && !ka->backwards_tsc_observed
54750f2c 2179 && !ka->boot_vcpu_runs_old_kvmclock;
b48aa97e 2180
d828199e
MT
2181 if (ka->use_master_clock)
2182 atomic_set(&kvm_guest_has_master_clock, 1);
2183
2184 vclock_mode = pvclock_gtod_data.clock.vclock_mode;
b48aa97e
MT
2185 trace_kvm_update_master_clock(ka->use_master_clock, vclock_mode,
2186 vcpus_matched);
d828199e
MT
2187#endif
2188}
2189
2860c4b1
PB
2190void kvm_make_mclock_inprogress_request(struct kvm *kvm)
2191{
2192 kvm_make_all_cpus_request(kvm, KVM_REQ_MCLOCK_INPROGRESS);
2193}
2194
2e762ff7
MT
2195static void kvm_gen_update_masterclock(struct kvm *kvm)
2196{
2197#ifdef CONFIG_X86_64
2198 int i;
2199 struct kvm_vcpu *vcpu;
2200 struct kvm_arch *ka = &kvm->arch;
2201
2202 spin_lock(&ka->pvclock_gtod_sync_lock);
2203 kvm_make_mclock_inprogress_request(kvm);
2204 /* no guest entries from this point */
2205 pvclock_update_vm_gtod_copy(kvm);
2206
2207 kvm_for_each_vcpu(i, vcpu, kvm)
105b21bb 2208 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
2e762ff7
MT
2209
2210 /* guest entries allowed */
2211 kvm_for_each_vcpu(i, vcpu, kvm)
72875d8a 2212 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
2e762ff7
MT
2213
2214 spin_unlock(&ka->pvclock_gtod_sync_lock);
2215#endif
2216}
2217
e891a32e 2218u64 get_kvmclock_ns(struct kvm *kvm)
108b249c 2219{
108b249c 2220 struct kvm_arch *ka = &kvm->arch;
8b953440 2221 struct pvclock_vcpu_time_info hv_clock;
e2c2206a 2222 u64 ret;
108b249c 2223
8b953440
PB
2224 spin_lock(&ka->pvclock_gtod_sync_lock);
2225 if (!ka->use_master_clock) {
2226 spin_unlock(&ka->pvclock_gtod_sync_lock);
9285ec4c 2227 return ktime_get_boottime_ns() + ka->kvmclock_offset;
108b249c
PB
2228 }
2229
8b953440
PB
2230 hv_clock.tsc_timestamp = ka->master_cycle_now;
2231 hv_clock.system_time = ka->master_kernel_ns + ka->kvmclock_offset;
2232 spin_unlock(&ka->pvclock_gtod_sync_lock);
2233
e2c2206a
WL
2234 /* both __this_cpu_read() and rdtsc() should be on the same cpu */
2235 get_cpu();
2236
e70b57a6
WL
2237 if (__this_cpu_read(cpu_tsc_khz)) {
2238 kvm_get_time_scale(NSEC_PER_SEC, __this_cpu_read(cpu_tsc_khz) * 1000LL,
2239 &hv_clock.tsc_shift,
2240 &hv_clock.tsc_to_system_mul);
2241 ret = __pvclock_read_cycles(&hv_clock, rdtsc());
2242 } else
9285ec4c 2243 ret = ktime_get_boottime_ns() + ka->kvmclock_offset;
e2c2206a
WL
2244
2245 put_cpu();
2246
2247 return ret;
108b249c
PB
2248}
2249
0d6dd2ff
PB
2250static void kvm_setup_pvclock_page(struct kvm_vcpu *v)
2251{
2252 struct kvm_vcpu_arch *vcpu = &v->arch;
2253 struct pvclock_vcpu_time_info guest_hv_clock;
2254
4e335d9e 2255 if (unlikely(kvm_read_guest_cached(v->kvm, &vcpu->pv_time,
0d6dd2ff
PB
2256 &guest_hv_clock, sizeof(guest_hv_clock))))
2257 return;
2258
2259 /* This VCPU is paused, but it's legal for a guest to read another
2260 * VCPU's kvmclock, so we really have to follow the specification where
2261 * it says that version is odd if data is being modified, and even after
2262 * it is consistent.
2263 *
2264 * Version field updates must be kept separate. This is because
2265 * kvm_write_guest_cached might use a "rep movs" instruction, and
2266 * writes within a string instruction are weakly ordered. So there
2267 * are three writes overall.
2268 *
2269 * As a small optimization, only write the version field in the first
2270 * and third write. The vcpu->pv_time cache is still valid, because the
2271 * version field is the first in the struct.
2272 */
2273 BUILD_BUG_ON(offsetof(struct pvclock_vcpu_time_info, version) != 0);
2274
51c4b8bb
LA
2275 if (guest_hv_clock.version & 1)
2276 ++guest_hv_clock.version; /* first time write, random junk */
2277
0d6dd2ff 2278 vcpu->hv_clock.version = guest_hv_clock.version + 1;
4e335d9e
PB
2279 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2280 &vcpu->hv_clock,
2281 sizeof(vcpu->hv_clock.version));
0d6dd2ff
PB
2282
2283 smp_wmb();
2284
2285 /* retain PVCLOCK_GUEST_STOPPED if set in guest copy */
2286 vcpu->hv_clock.flags |= (guest_hv_clock.flags & PVCLOCK_GUEST_STOPPED);
2287
2288 if (vcpu->pvclock_set_guest_stopped_request) {
2289 vcpu->hv_clock.flags |= PVCLOCK_GUEST_STOPPED;
2290 vcpu->pvclock_set_guest_stopped_request = false;
2291 }
2292
2293 trace_kvm_pvclock_update(v->vcpu_id, &vcpu->hv_clock);
2294
4e335d9e
PB
2295 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2296 &vcpu->hv_clock,
2297 sizeof(vcpu->hv_clock));
0d6dd2ff
PB
2298
2299 smp_wmb();
2300
2301 vcpu->hv_clock.version++;
4e335d9e
PB
2302 kvm_write_guest_cached(v->kvm, &vcpu->pv_time,
2303 &vcpu->hv_clock,
2304 sizeof(vcpu->hv_clock.version));
0d6dd2ff
PB
2305}
2306
34c238a1 2307static int kvm_guest_time_update(struct kvm_vcpu *v)
18068523 2308{
78db6a50 2309 unsigned long flags, tgt_tsc_khz;
18068523 2310 struct kvm_vcpu_arch *vcpu = &v->arch;
d828199e 2311 struct kvm_arch *ka = &v->kvm->arch;
f25e656d 2312 s64 kernel_ns;
d828199e 2313 u64 tsc_timestamp, host_tsc;
51d59c6b 2314 u8 pvclock_flags;
d828199e
MT
2315 bool use_master_clock;
2316
2317 kernel_ns = 0;
2318 host_tsc = 0;
18068523 2319
d828199e
MT
2320 /*
2321 * If the host uses TSC clock, then passthrough TSC as stable
2322 * to the guest.
2323 */
2324 spin_lock(&ka->pvclock_gtod_sync_lock);
2325 use_master_clock = ka->use_master_clock;
2326 if (use_master_clock) {
2327 host_tsc = ka->master_cycle_now;
2328 kernel_ns = ka->master_kernel_ns;
2329 }
2330 spin_unlock(&ka->pvclock_gtod_sync_lock);
c09664bb
MT
2331
2332 /* Keep irq disabled to prevent changes to the clock */
2333 local_irq_save(flags);
78db6a50
PB
2334 tgt_tsc_khz = __this_cpu_read(cpu_tsc_khz);
2335 if (unlikely(tgt_tsc_khz == 0)) {
c09664bb
MT
2336 local_irq_restore(flags);
2337 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
2338 return 1;
2339 }
d828199e 2340 if (!use_master_clock) {
4ea1636b 2341 host_tsc = rdtsc();
9285ec4c 2342 kernel_ns = ktime_get_boottime_ns();
d828199e
MT
2343 }
2344
4ba76538 2345 tsc_timestamp = kvm_read_l1_tsc(v, host_tsc);
d828199e 2346
c285545f
ZA
2347 /*
2348 * We may have to catch up the TSC to match elapsed wall clock
2349 * time for two reasons, even if kvmclock is used.
2350 * 1) CPU could have been running below the maximum TSC rate
2351 * 2) Broken TSC compensation resets the base at each VCPU
2352 * entry to avoid unknown leaps of TSC even when running
2353 * again on the same CPU. This may cause apparent elapsed
2354 * time to disappear, and the guest to stand still or run
2355 * very slowly.
2356 */
2357 if (vcpu->tsc_catchup) {
2358 u64 tsc = compute_guest_tsc(v, kernel_ns);
2359 if (tsc > tsc_timestamp) {
f1e2b260 2360 adjust_tsc_offset_guest(v, tsc - tsc_timestamp);
c285545f
ZA
2361 tsc_timestamp = tsc;
2362 }
50d0a0f9
GH
2363 }
2364
18068523
GOC
2365 local_irq_restore(flags);
2366
0d6dd2ff 2367 /* With all the info we got, fill in the values */
18068523 2368
78db6a50
PB
2369 if (kvm_has_tsc_control)
2370 tgt_tsc_khz = kvm_scale_tsc(v, tgt_tsc_khz);
2371
2372 if (unlikely(vcpu->hw_tsc_khz != tgt_tsc_khz)) {
3ae13faa 2373 kvm_get_time_scale(NSEC_PER_SEC, tgt_tsc_khz * 1000LL,
5f4e3f88
ZA
2374 &vcpu->hv_clock.tsc_shift,
2375 &vcpu->hv_clock.tsc_to_system_mul);
78db6a50 2376 vcpu->hw_tsc_khz = tgt_tsc_khz;
8cfdc000
ZA
2377 }
2378
1d5f066e 2379 vcpu->hv_clock.tsc_timestamp = tsc_timestamp;
759379dd 2380 vcpu->hv_clock.system_time = kernel_ns + v->kvm->arch.kvmclock_offset;
28e4639a 2381 vcpu->last_guest_tsc = tsc_timestamp;
51d59c6b 2382
d828199e 2383 /* If the host uses TSC clocksource, then it is stable */
0d6dd2ff 2384 pvclock_flags = 0;
d828199e
MT
2385 if (use_master_clock)
2386 pvclock_flags |= PVCLOCK_TSC_STABLE_BIT;
2387
78c0337a
MT
2388 vcpu->hv_clock.flags = pvclock_flags;
2389
095cf55d
PB
2390 if (vcpu->pv_time_enabled)
2391 kvm_setup_pvclock_page(v);
2392 if (v == kvm_get_vcpu(v->kvm, 0))
2393 kvm_hv_setup_tsc_page(v->kvm, &vcpu->hv_clock);
8cfdc000 2394 return 0;
c8076604
GH
2395}
2396
0061d53d
MT
2397/*
2398 * kvmclock updates which are isolated to a given vcpu, such as
2399 * vcpu->cpu migration, should not allow system_timestamp from
2400 * the rest of the vcpus to remain static. Otherwise ntp frequency
2401 * correction applies to one vcpu's system_timestamp but not
2402 * the others.
2403 *
2404 * So in those cases, request a kvmclock update for all vcpus.
7e44e449
AJ
2405 * We need to rate-limit these requests though, as they can
2406 * considerably slow guests that have a large number of vcpus.
2407 * The time for a remote vcpu to update its kvmclock is bound
2408 * by the delay we use to rate-limit the updates.
0061d53d
MT
2409 */
2410
7e44e449
AJ
2411#define KVMCLOCK_UPDATE_DELAY msecs_to_jiffies(100)
2412
2413static void kvmclock_update_fn(struct work_struct *work)
0061d53d
MT
2414{
2415 int i;
7e44e449
AJ
2416 struct delayed_work *dwork = to_delayed_work(work);
2417 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2418 kvmclock_update_work);
2419 struct kvm *kvm = container_of(ka, struct kvm, arch);
0061d53d
MT
2420 struct kvm_vcpu *vcpu;
2421
2422 kvm_for_each_vcpu(i, vcpu, kvm) {
105b21bb 2423 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
0061d53d
MT
2424 kvm_vcpu_kick(vcpu);
2425 }
2426}
2427
7e44e449
AJ
2428static void kvm_gen_kvmclock_update(struct kvm_vcpu *v)
2429{
2430 struct kvm *kvm = v->kvm;
2431
105b21bb 2432 kvm_make_request(KVM_REQ_CLOCK_UPDATE, v);
7e44e449
AJ
2433 schedule_delayed_work(&kvm->arch.kvmclock_update_work,
2434 KVMCLOCK_UPDATE_DELAY);
2435}
2436
332967a3
AJ
2437#define KVMCLOCK_SYNC_PERIOD (300 * HZ)
2438
2439static void kvmclock_sync_fn(struct work_struct *work)
2440{
2441 struct delayed_work *dwork = to_delayed_work(work);
2442 struct kvm_arch *ka = container_of(dwork, struct kvm_arch,
2443 kvmclock_sync_work);
2444 struct kvm *kvm = container_of(ka, struct kvm, arch);
2445
630994b3
MT
2446 if (!kvmclock_periodic_sync)
2447 return;
2448
332967a3
AJ
2449 schedule_delayed_work(&kvm->arch.kvmclock_update_work, 0);
2450 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
2451 KVMCLOCK_SYNC_PERIOD);
2452}
2453
191c8137
BP
2454/*
2455 * On AMD, HWCR[McStatusWrEn] controls whether setting MCi_STATUS results in #GP.
2456 */
2457static bool can_set_mci_status(struct kvm_vcpu *vcpu)
2458{
2459 /* McStatusWrEn enabled? */
2460 if (guest_cpuid_is_amd(vcpu))
2461 return !!(vcpu->arch.msr_hwcr & BIT_ULL(18));
2462
2463 return false;
2464}
2465
9ffd986c 2466static int set_msr_mce(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
15c4a640 2467{
890ca9ae
HY
2468 u64 mcg_cap = vcpu->arch.mcg_cap;
2469 unsigned bank_num = mcg_cap & 0xff;
9ffd986c
WL
2470 u32 msr = msr_info->index;
2471 u64 data = msr_info->data;
890ca9ae 2472
15c4a640 2473 switch (msr) {
15c4a640 2474 case MSR_IA32_MCG_STATUS:
890ca9ae 2475 vcpu->arch.mcg_status = data;
15c4a640 2476 break;
c7ac679c 2477 case MSR_IA32_MCG_CTL:
44883f01
PB
2478 if (!(mcg_cap & MCG_CTL_P) &&
2479 (data || !msr_info->host_initiated))
890ca9ae
HY
2480 return 1;
2481 if (data != 0 && data != ~(u64)0)
44883f01 2482 return 1;
890ca9ae
HY
2483 vcpu->arch.mcg_ctl = data;
2484 break;
2485 default:
2486 if (msr >= MSR_IA32_MC0_CTL &&
81760dcc 2487 msr < MSR_IA32_MCx_CTL(bank_num)) {
890ca9ae 2488 u32 offset = msr - MSR_IA32_MC0_CTL;
114be429
AP
2489 /* only 0 or all 1s can be written to IA32_MCi_CTL
2490 * some Linux kernels though clear bit 10 in bank 4 to
2491 * workaround a BIOS/GART TBL issue on AMD K8s, ignore
2492 * this to avoid an uncatched #GP in the guest
2493 */
890ca9ae 2494 if ((offset & 0x3) == 0 &&
114be429 2495 data != 0 && (data | (1 << 10)) != ~(u64)0)
890ca9ae 2496 return -1;
191c8137
BP
2497
2498 /* MCi_STATUS */
9ffd986c 2499 if (!msr_info->host_initiated &&
191c8137
BP
2500 (offset & 0x3) == 1 && data != 0) {
2501 if (!can_set_mci_status(vcpu))
2502 return -1;
2503 }
2504
890ca9ae
HY
2505 vcpu->arch.mce_banks[offset] = data;
2506 break;
2507 }
2508 return 1;
2509 }
2510 return 0;
2511}
2512
ffde22ac
ES
2513static int xen_hvm_config(struct kvm_vcpu *vcpu, u64 data)
2514{
2515 struct kvm *kvm = vcpu->kvm;
2516 int lm = is_long_mode(vcpu);
2517 u8 *blob_addr = lm ? (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_64
2518 : (u8 *)(long)kvm->arch.xen_hvm_config.blob_addr_32;
2519 u8 blob_size = lm ? kvm->arch.xen_hvm_config.blob_size_64
2520 : kvm->arch.xen_hvm_config.blob_size_32;
2521 u32 page_num = data & ~PAGE_MASK;
2522 u64 page_addr = data & PAGE_MASK;
2523 u8 *page;
2524 int r;
2525
2526 r = -E2BIG;
2527 if (page_num >= blob_size)
2528 goto out;
2529 r = -ENOMEM;
ff5c2c03
SL
2530 page = memdup_user(blob_addr + (page_num * PAGE_SIZE), PAGE_SIZE);
2531 if (IS_ERR(page)) {
2532 r = PTR_ERR(page);
ffde22ac 2533 goto out;
ff5c2c03 2534 }
54bf36aa 2535 if (kvm_vcpu_write_guest(vcpu, page_addr, page, PAGE_SIZE))
ffde22ac
ES
2536 goto out_free;
2537 r = 0;
2538out_free:
2539 kfree(page);
2540out:
2541 return r;
2542}
2543
344d9588
GN
2544static int kvm_pv_enable_async_pf(struct kvm_vcpu *vcpu, u64 data)
2545{
2546 gpa_t gpa = data & ~0x3f;
2547
52a5c155
WL
2548 /* Bits 3:5 are reserved, Should be zero */
2549 if (data & 0x38)
344d9588
GN
2550 return 1;
2551
2552 vcpu->arch.apf.msr_val = data;
2553
2554 if (!(data & KVM_ASYNC_PF_ENABLED)) {
2555 kvm_clear_async_pf_completion_queue(vcpu);
2556 kvm_async_pf_hash_reset(vcpu);
2557 return 0;
2558 }
2559
4e335d9e 2560 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.apf.data, gpa,
8f964525 2561 sizeof(u32)))
344d9588
GN
2562 return 1;
2563
6adba527 2564 vcpu->arch.apf.send_user_only = !(data & KVM_ASYNC_PF_SEND_ALWAYS);
52a5c155 2565 vcpu->arch.apf.delivery_as_pf_vmexit = data & KVM_ASYNC_PF_DELIVERY_AS_PF_VMEXIT;
344d9588
GN
2566 kvm_async_pf_wakeup_all(vcpu);
2567 return 0;
2568}
2569
12f9a48f
GC
2570static void kvmclock_reset(struct kvm_vcpu *vcpu)
2571{
0b79459b 2572 vcpu->arch.pv_time_enabled = false;
49dedf0d 2573 vcpu->arch.time = 0;
12f9a48f
GC
2574}
2575
f38a7b75
WL
2576static void kvm_vcpu_flush_tlb(struct kvm_vcpu *vcpu, bool invalidate_gpa)
2577{
2578 ++vcpu->stat.tlb_flush;
2579 kvm_x86_ops->tlb_flush(vcpu, invalidate_gpa);
2580}
2581
c9aaa895
GC
2582static void record_steal_time(struct kvm_vcpu *vcpu)
2583{
2584 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
2585 return;
2586
4e335d9e 2587 if (unlikely(kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
c9aaa895
GC
2588 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time))))
2589 return;
2590
f38a7b75
WL
2591 /*
2592 * Doing a TLB flush here, on the guest's behalf, can avoid
2593 * expensive IPIs.
2594 */
b382f44e
WL
2595 trace_kvm_pv_tlb_flush(vcpu->vcpu_id,
2596 vcpu->arch.st.steal.preempted & KVM_VCPU_FLUSH_TLB);
f38a7b75
WL
2597 if (xchg(&vcpu->arch.st.steal.preempted, 0) & KVM_VCPU_FLUSH_TLB)
2598 kvm_vcpu_flush_tlb(vcpu, false);
0b9f6c46 2599
35f3fae1
WL
2600 if (vcpu->arch.st.steal.version & 1)
2601 vcpu->arch.st.steal.version += 1; /* first time write, random junk */
2602
2603 vcpu->arch.st.steal.version += 1;
2604
4e335d9e 2605 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
35f3fae1
WL
2606 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2607
2608 smp_wmb();
2609
c54cdf14
LC
2610 vcpu->arch.st.steal.steal += current->sched_info.run_delay -
2611 vcpu->arch.st.last_steal;
2612 vcpu->arch.st.last_steal = current->sched_info.run_delay;
35f3fae1 2613
4e335d9e 2614 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
35f3fae1
WL
2615 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2616
2617 smp_wmb();
2618
2619 vcpu->arch.st.steal.version += 1;
c9aaa895 2620
4e335d9e 2621 kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.st.stime,
c9aaa895
GC
2622 &vcpu->arch.st.steal, sizeof(struct kvm_steal_time));
2623}
2624
8fe8ab46 2625int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
15c4a640 2626{
5753785f 2627 bool pr = false;
8fe8ab46
WA
2628 u32 msr = msr_info->index;
2629 u64 data = msr_info->data;
5753785f 2630
15c4a640 2631 switch (msr) {
2e32b719 2632 case MSR_AMD64_NB_CFG:
2e32b719
BP
2633 case MSR_IA32_UCODE_WRITE:
2634 case MSR_VM_HSAVE_PA:
2635 case MSR_AMD64_PATCH_LOADER:
2636 case MSR_AMD64_BU_CFG2:
405a353a 2637 case MSR_AMD64_DC_CFG:
0e1b869f 2638 case MSR_F15H_EX_CFG:
2e32b719
BP
2639 break;
2640
518e7b94
WL
2641 case MSR_IA32_UCODE_REV:
2642 if (msr_info->host_initiated)
2643 vcpu->arch.microcode_version = data;
2644 break;
0cf9135b
SC
2645 case MSR_IA32_ARCH_CAPABILITIES:
2646 if (!msr_info->host_initiated)
2647 return 1;
2648 vcpu->arch.arch_capabilities = data;
2649 break;
15c4a640 2650 case MSR_EFER:
11988499 2651 return set_efer(vcpu, msr_info);
8f1589d9
AP
2652 case MSR_K7_HWCR:
2653 data &= ~(u64)0x40; /* ignore flush filter disable */
82494028 2654 data &= ~(u64)0x100; /* ignore ignne emulation enable */
a223c313 2655 data &= ~(u64)0x8; /* ignore TLB cache disable */
191c8137
BP
2656
2657 /* Handle McStatusWrEn */
2658 if (data == BIT_ULL(18)) {
2659 vcpu->arch.msr_hwcr = data;
2660 } else if (data != 0) {
a737f256
CD
2661 vcpu_unimpl(vcpu, "unimplemented HWCR wrmsr: 0x%llx\n",
2662 data);
8f1589d9
AP
2663 return 1;
2664 }
15c4a640 2665 break;
f7c6d140
AP
2666 case MSR_FAM10H_MMIO_CONF_BASE:
2667 if (data != 0) {
a737f256
CD
2668 vcpu_unimpl(vcpu, "unimplemented MMIO_CONF_BASE wrmsr: "
2669 "0x%llx\n", data);
f7c6d140
AP
2670 return 1;
2671 }
15c4a640 2672 break;
b5e2fec0
AG
2673 case MSR_IA32_DEBUGCTLMSR:
2674 if (!data) {
2675 /* We support the non-activated case already */
2676 break;
2677 } else if (data & ~(DEBUGCTLMSR_LBR | DEBUGCTLMSR_BTF)) {
2678 /* Values other than LBR and BTF are vendor-specific,
2679 thus reserved and should throw a #GP */
2680 return 1;
2681 }
a737f256
CD
2682 vcpu_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTLMSR 0x%llx, nop\n",
2683 __func__, data);
b5e2fec0 2684 break;
9ba075a6 2685 case 0x200 ... 0x2ff:
ff53604b 2686 return kvm_mtrr_set_msr(vcpu, msr, data);
15c4a640 2687 case MSR_IA32_APICBASE:
58cb628d 2688 return kvm_set_apic_base(vcpu, msr_info);
0105d1a5
GN
2689 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
2690 return kvm_x2apic_msr_write(vcpu, msr, data);
a3e06bbe
LJ
2691 case MSR_IA32_TSCDEADLINE:
2692 kvm_set_lapic_tscdeadline_msr(vcpu, data);
2693 break;
ba904635 2694 case MSR_IA32_TSC_ADJUST:
d6321d49 2695 if (guest_cpuid_has(vcpu, X86_FEATURE_TSC_ADJUST)) {
ba904635 2696 if (!msr_info->host_initiated) {
d913b904 2697 s64 adj = data - vcpu->arch.ia32_tsc_adjust_msr;
d7add054 2698 adjust_tsc_offset_guest(vcpu, adj);
ba904635
WA
2699 }
2700 vcpu->arch.ia32_tsc_adjust_msr = data;
2701 }
2702 break;
15c4a640 2703 case MSR_IA32_MISC_ENABLE:
511a8556
WL
2704 if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT) &&
2705 ((vcpu->arch.ia32_misc_enable_msr ^ data) & MSR_IA32_MISC_ENABLE_MWAIT)) {
2706 if (!guest_cpuid_has(vcpu, X86_FEATURE_XMM3))
2707 return 1;
2708 vcpu->arch.ia32_misc_enable_msr = data;
2709 kvm_update_cpuid(vcpu);
2710 } else {
2711 vcpu->arch.ia32_misc_enable_msr = data;
2712 }
15c4a640 2713 break;
64d60670
PB
2714 case MSR_IA32_SMBASE:
2715 if (!msr_info->host_initiated)
2716 return 1;
2717 vcpu->arch.smbase = data;
2718 break;
73f624f4
PB
2719 case MSR_IA32_POWER_CTL:
2720 vcpu->arch.msr_ia32_power_ctl = data;
2721 break;
dd259935
PB
2722 case MSR_IA32_TSC:
2723 kvm_write_tsc(vcpu, msr_info);
2724 break;
864e2ab2
AL
2725 case MSR_IA32_XSS:
2726 if (!msr_info->host_initiated &&
2727 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
2728 return 1;
2729 /*
2730 * We do support PT if kvm_x86_ops->pt_supported(), but we do
2731 * not support IA32_XSS[bit 8]. Guests will have to use
2732 * RDMSR/WRMSR rather than XSAVES/XRSTORS to save/restore PT
2733 * MSRs.
2734 */
2735 if (data != 0)
2736 return 1;
2737 vcpu->arch.ia32_xss = data;
2738 break;
52797bf9
LA
2739 case MSR_SMI_COUNT:
2740 if (!msr_info->host_initiated)
2741 return 1;
2742 vcpu->arch.smi_count = data;
2743 break;
11c6bffa 2744 case MSR_KVM_WALL_CLOCK_NEW:
18068523
GOC
2745 case MSR_KVM_WALL_CLOCK:
2746 vcpu->kvm->arch.wall_clock = data;
2747 kvm_write_wall_clock(vcpu->kvm, data);
2748 break;
11c6bffa 2749 case MSR_KVM_SYSTEM_TIME_NEW:
18068523 2750 case MSR_KVM_SYSTEM_TIME: {
54750f2c
MT
2751 struct kvm_arch *ka = &vcpu->kvm->arch;
2752
54750f2c
MT
2753 if (vcpu->vcpu_id == 0 && !msr_info->host_initiated) {
2754 bool tmp = (msr == MSR_KVM_SYSTEM_TIME);
2755
2756 if (ka->boot_vcpu_runs_old_kvmclock != tmp)
1bd2009e 2757 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
54750f2c
MT
2758
2759 ka->boot_vcpu_runs_old_kvmclock = tmp;
2760 }
2761
18068523 2762 vcpu->arch.time = data;
0061d53d 2763 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
18068523
GOC
2764
2765 /* we verify if the enable bit is set... */
49dedf0d 2766 vcpu->arch.pv_time_enabled = false;
18068523
GOC
2767 if (!(data & 1))
2768 break;
2769
49dedf0d 2770 if (!kvm_gfn_to_hva_cache_init(vcpu->kvm,
8f964525
AH
2771 &vcpu->arch.pv_time, data & ~1ULL,
2772 sizeof(struct pvclock_vcpu_time_info)))
0b79459b 2773 vcpu->arch.pv_time_enabled = true;
32cad84f 2774
18068523
GOC
2775 break;
2776 }
344d9588
GN
2777 case MSR_KVM_ASYNC_PF_EN:
2778 if (kvm_pv_enable_async_pf(vcpu, data))
2779 return 1;
2780 break;
c9aaa895
GC
2781 case MSR_KVM_STEAL_TIME:
2782
2783 if (unlikely(!sched_info_on()))
2784 return 1;
2785
2786 if (data & KVM_STEAL_RESERVED_MASK)
2787 return 1;
2788
4e335d9e 2789 if (kvm_gfn_to_hva_cache_init(vcpu->kvm, &vcpu->arch.st.stime,
8f964525
AH
2790 data & KVM_STEAL_VALID_BITS,
2791 sizeof(struct kvm_steal_time)))
c9aaa895
GC
2792 return 1;
2793
2794 vcpu->arch.st.msr_val = data;
2795
2796 if (!(data & KVM_MSR_ENABLED))
2797 break;
2798
c9aaa895
GC
2799 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
2800
2801 break;
ae7a2a3f 2802 case MSR_KVM_PV_EOI_EN:
72bbf935 2803 if (kvm_lapic_enable_pv_eoi(vcpu, data, sizeof(u8)))
ae7a2a3f
MT
2804 return 1;
2805 break;
c9aaa895 2806
2d5ba19b
MT
2807 case MSR_KVM_POLL_CONTROL:
2808 /* only enable bit supported */
2809 if (data & (-1ULL << 1))
2810 return 1;
2811
2812 vcpu->arch.msr_kvm_poll_control = data;
2813 break;
2814
890ca9ae
HY
2815 case MSR_IA32_MCG_CTL:
2816 case MSR_IA32_MCG_STATUS:
81760dcc 2817 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
9ffd986c 2818 return set_msr_mce(vcpu, msr_info);
71db6023 2819
6912ac32
WH
2820 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2821 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2822 pr = true; /* fall through */
2823 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2824 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
c6702c9d 2825 if (kvm_pmu_is_valid_msr(vcpu, msr))
afd80d85 2826 return kvm_pmu_set_msr(vcpu, msr_info);
5753785f
GN
2827
2828 if (pr || data != 0)
a737f256
CD
2829 vcpu_unimpl(vcpu, "disabled perfctr wrmsr: "
2830 "0x%x data 0x%llx\n", msr, data);
5753785f 2831 break;
84e0cefa
JS
2832 case MSR_K7_CLK_CTL:
2833 /*
2834 * Ignore all writes to this no longer documented MSR.
2835 * Writes are only relevant for old K7 processors,
2836 * all pre-dating SVM, but a recommended workaround from
4a969980 2837 * AMD for these chips. It is possible to specify the
84e0cefa
JS
2838 * affected processor models on the command line, hence
2839 * the need to ignore the workaround.
2840 */
2841 break;
55cd8e5a 2842 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
e7d9513b
AS
2843 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
2844 case HV_X64_MSR_CRASH_CTL:
1f4b34f8 2845 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
a2e164e7
VK
2846 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
2847 case HV_X64_MSR_TSC_EMULATION_CONTROL:
2848 case HV_X64_MSR_TSC_EMULATION_STATUS:
e7d9513b
AS
2849 return kvm_hv_set_msr_common(vcpu, msr, data,
2850 msr_info->host_initiated);
91c9c3ed 2851 case MSR_IA32_BBL_CR_CTL3:
2852 /* Drop writes to this legacy MSR -- see rdmsr
2853 * counterpart for further detail.
2854 */
fab0aa3b
EM
2855 if (report_ignored_msrs)
2856 vcpu_unimpl(vcpu, "ignored wrmsr: 0x%x data 0x%llx\n",
2857 msr, data);
91c9c3ed 2858 break;
2b036c6b 2859 case MSR_AMD64_OSVW_ID_LENGTH:
d6321d49 2860 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
2b036c6b
BO
2861 return 1;
2862 vcpu->arch.osvw.length = data;
2863 break;
2864 case MSR_AMD64_OSVW_STATUS:
d6321d49 2865 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
2b036c6b
BO
2866 return 1;
2867 vcpu->arch.osvw.status = data;
2868 break;
db2336a8
KH
2869 case MSR_PLATFORM_INFO:
2870 if (!msr_info->host_initiated ||
db2336a8
KH
2871 (!(data & MSR_PLATFORM_INFO_CPUID_FAULT) &&
2872 cpuid_fault_enabled(vcpu)))
2873 return 1;
2874 vcpu->arch.msr_platform_info = data;
2875 break;
2876 case MSR_MISC_FEATURES_ENABLES:
2877 if (data & ~MSR_MISC_FEATURES_ENABLES_CPUID_FAULT ||
2878 (data & MSR_MISC_FEATURES_ENABLES_CPUID_FAULT &&
2879 !supports_cpuid_fault(vcpu)))
2880 return 1;
2881 vcpu->arch.msr_misc_features_enables = data;
2882 break;
15c4a640 2883 default:
ffde22ac
ES
2884 if (msr && (msr == vcpu->kvm->arch.xen_hvm_config.msr))
2885 return xen_hvm_config(vcpu, data);
c6702c9d 2886 if (kvm_pmu_is_valid_msr(vcpu, msr))
afd80d85 2887 return kvm_pmu_set_msr(vcpu, msr_info);
ed85c068 2888 if (!ignore_msrs) {
ae0f5499 2889 vcpu_debug_ratelimited(vcpu, "unhandled wrmsr: 0x%x data 0x%llx\n",
a737f256 2890 msr, data);
ed85c068
AP
2891 return 1;
2892 } else {
fab0aa3b
EM
2893 if (report_ignored_msrs)
2894 vcpu_unimpl(vcpu,
2895 "ignored wrmsr: 0x%x data 0x%llx\n",
2896 msr, data);
ed85c068
AP
2897 break;
2898 }
15c4a640
CO
2899 }
2900 return 0;
2901}
2902EXPORT_SYMBOL_GPL(kvm_set_msr_common);
2903
44883f01 2904static int get_msr_mce(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata, bool host)
15c4a640
CO
2905{
2906 u64 data;
890ca9ae
HY
2907 u64 mcg_cap = vcpu->arch.mcg_cap;
2908 unsigned bank_num = mcg_cap & 0xff;
15c4a640
CO
2909
2910 switch (msr) {
15c4a640
CO
2911 case MSR_IA32_P5_MC_ADDR:
2912 case MSR_IA32_P5_MC_TYPE:
890ca9ae
HY
2913 data = 0;
2914 break;
15c4a640 2915 case MSR_IA32_MCG_CAP:
890ca9ae
HY
2916 data = vcpu->arch.mcg_cap;
2917 break;
c7ac679c 2918 case MSR_IA32_MCG_CTL:
44883f01 2919 if (!(mcg_cap & MCG_CTL_P) && !host)
890ca9ae
HY
2920 return 1;
2921 data = vcpu->arch.mcg_ctl;
2922 break;
2923 case MSR_IA32_MCG_STATUS:
2924 data = vcpu->arch.mcg_status;
2925 break;
2926 default:
2927 if (msr >= MSR_IA32_MC0_CTL &&
81760dcc 2928 msr < MSR_IA32_MCx_CTL(bank_num)) {
890ca9ae
HY
2929 u32 offset = msr - MSR_IA32_MC0_CTL;
2930 data = vcpu->arch.mce_banks[offset];
2931 break;
2932 }
2933 return 1;
2934 }
2935 *pdata = data;
2936 return 0;
2937}
2938
609e36d3 2939int kvm_get_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
890ca9ae 2940{
609e36d3 2941 switch (msr_info->index) {
890ca9ae 2942 case MSR_IA32_PLATFORM_ID:
15c4a640 2943 case MSR_IA32_EBL_CR_POWERON:
b5e2fec0
AG
2944 case MSR_IA32_DEBUGCTLMSR:
2945 case MSR_IA32_LASTBRANCHFROMIP:
2946 case MSR_IA32_LASTBRANCHTOIP:
2947 case MSR_IA32_LASTINTFROMIP:
2948 case MSR_IA32_LASTINTTOIP:
60af2ecd 2949 case MSR_K8_SYSCFG:
3afb1121
PB
2950 case MSR_K8_TSEG_ADDR:
2951 case MSR_K8_TSEG_MASK:
61a6bd67 2952 case MSR_VM_HSAVE_PA:
1fdbd48c 2953 case MSR_K8_INT_PENDING_MSG:
c323c0e5 2954 case MSR_AMD64_NB_CFG:
f7c6d140 2955 case MSR_FAM10H_MMIO_CONF_BASE:
2e32b719 2956 case MSR_AMD64_BU_CFG2:
0c2df2a1 2957 case MSR_IA32_PERF_CTL:
405a353a 2958 case MSR_AMD64_DC_CFG:
0e1b869f 2959 case MSR_F15H_EX_CFG:
609e36d3 2960 msr_info->data = 0;
15c4a640 2961 break;
c51eb52b 2962 case MSR_F15H_PERF_CTL0 ... MSR_F15H_PERF_CTR5:
6912ac32
WH
2963 case MSR_K7_EVNTSEL0 ... MSR_K7_EVNTSEL3:
2964 case MSR_K7_PERFCTR0 ... MSR_K7_PERFCTR3:
2965 case MSR_P6_PERFCTR0 ... MSR_P6_PERFCTR1:
2966 case MSR_P6_EVNTSEL0 ... MSR_P6_EVNTSEL1:
c6702c9d 2967 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
609e36d3
PB
2968 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
2969 msr_info->data = 0;
5753785f 2970 break;
742bc670 2971 case MSR_IA32_UCODE_REV:
518e7b94 2972 msr_info->data = vcpu->arch.microcode_version;
742bc670 2973 break;
0cf9135b
SC
2974 case MSR_IA32_ARCH_CAPABILITIES:
2975 if (!msr_info->host_initiated &&
2976 !guest_cpuid_has(vcpu, X86_FEATURE_ARCH_CAPABILITIES))
2977 return 1;
2978 msr_info->data = vcpu->arch.arch_capabilities;
2979 break;
73f624f4
PB
2980 case MSR_IA32_POWER_CTL:
2981 msr_info->data = vcpu->arch.msr_ia32_power_ctl;
2982 break;
dd259935
PB
2983 case MSR_IA32_TSC:
2984 msr_info->data = kvm_scale_tsc(vcpu, rdtsc()) + vcpu->arch.tsc_offset;
2985 break;
9ba075a6 2986 case MSR_MTRRcap:
9ba075a6 2987 case 0x200 ... 0x2ff:
ff53604b 2988 return kvm_mtrr_get_msr(vcpu, msr_info->index, &msr_info->data);
15c4a640 2989 case 0xcd: /* fsb frequency */
609e36d3 2990 msr_info->data = 3;
15c4a640 2991 break;
7b914098
JS
2992 /*
2993 * MSR_EBC_FREQUENCY_ID
2994 * Conservative value valid for even the basic CPU models.
2995 * Models 0,1: 000 in bits 23:21 indicating a bus speed of
2996 * 100MHz, model 2 000 in bits 18:16 indicating 100MHz,
2997 * and 266MHz for model 3, or 4. Set Core Clock
2998 * Frequency to System Bus Frequency Ratio to 1 (bits
2999 * 31:24) even though these are only valid for CPU
3000 * models > 2, however guests may end up dividing or
3001 * multiplying by zero otherwise.
3002 */
3003 case MSR_EBC_FREQUENCY_ID:
609e36d3 3004 msr_info->data = 1 << 24;
7b914098 3005 break;
15c4a640 3006 case MSR_IA32_APICBASE:
609e36d3 3007 msr_info->data = kvm_get_apic_base(vcpu);
15c4a640 3008 break;
0105d1a5 3009 case APIC_BASE_MSR ... APIC_BASE_MSR + 0x3ff:
609e36d3 3010 return kvm_x2apic_msr_read(vcpu, msr_info->index, &msr_info->data);
0105d1a5 3011 break;
a3e06bbe 3012 case MSR_IA32_TSCDEADLINE:
609e36d3 3013 msr_info->data = kvm_get_lapic_tscdeadline_msr(vcpu);
a3e06bbe 3014 break;
ba904635 3015 case MSR_IA32_TSC_ADJUST:
609e36d3 3016 msr_info->data = (u64)vcpu->arch.ia32_tsc_adjust_msr;
ba904635 3017 break;
15c4a640 3018 case MSR_IA32_MISC_ENABLE:
609e36d3 3019 msr_info->data = vcpu->arch.ia32_misc_enable_msr;
15c4a640 3020 break;
64d60670
PB
3021 case MSR_IA32_SMBASE:
3022 if (!msr_info->host_initiated)
3023 return 1;
3024 msr_info->data = vcpu->arch.smbase;
15c4a640 3025 break;
52797bf9
LA
3026 case MSR_SMI_COUNT:
3027 msr_info->data = vcpu->arch.smi_count;
3028 break;
847f0ad8
AG
3029 case MSR_IA32_PERF_STATUS:
3030 /* TSC increment by tick */
609e36d3 3031 msr_info->data = 1000ULL;
847f0ad8 3032 /* CPU multiplier */
b0996ae4 3033 msr_info->data |= (((uint64_t)4ULL) << 40);
847f0ad8 3034 break;
15c4a640 3035 case MSR_EFER:
609e36d3 3036 msr_info->data = vcpu->arch.efer;
15c4a640 3037 break;
18068523 3038 case MSR_KVM_WALL_CLOCK:
11c6bffa 3039 case MSR_KVM_WALL_CLOCK_NEW:
609e36d3 3040 msr_info->data = vcpu->kvm->arch.wall_clock;
18068523
GOC
3041 break;
3042 case MSR_KVM_SYSTEM_TIME:
11c6bffa 3043 case MSR_KVM_SYSTEM_TIME_NEW:
609e36d3 3044 msr_info->data = vcpu->arch.time;
18068523 3045 break;
344d9588 3046 case MSR_KVM_ASYNC_PF_EN:
609e36d3 3047 msr_info->data = vcpu->arch.apf.msr_val;
344d9588 3048 break;
c9aaa895 3049 case MSR_KVM_STEAL_TIME:
609e36d3 3050 msr_info->data = vcpu->arch.st.msr_val;
c9aaa895 3051 break;
1d92128f 3052 case MSR_KVM_PV_EOI_EN:
609e36d3 3053 msr_info->data = vcpu->arch.pv_eoi.msr_val;
1d92128f 3054 break;
2d5ba19b
MT
3055 case MSR_KVM_POLL_CONTROL:
3056 msr_info->data = vcpu->arch.msr_kvm_poll_control;
3057 break;
890ca9ae
HY
3058 case MSR_IA32_P5_MC_ADDR:
3059 case MSR_IA32_P5_MC_TYPE:
3060 case MSR_IA32_MCG_CAP:
3061 case MSR_IA32_MCG_CTL:
3062 case MSR_IA32_MCG_STATUS:
81760dcc 3063 case MSR_IA32_MC0_CTL ... MSR_IA32_MCx_CTL(KVM_MAX_MCE_BANKS) - 1:
44883f01
PB
3064 return get_msr_mce(vcpu, msr_info->index, &msr_info->data,
3065 msr_info->host_initiated);
864e2ab2
AL
3066 case MSR_IA32_XSS:
3067 if (!msr_info->host_initiated &&
3068 !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES))
3069 return 1;
3070 msr_info->data = vcpu->arch.ia32_xss;
3071 break;
84e0cefa
JS
3072 case MSR_K7_CLK_CTL:
3073 /*
3074 * Provide expected ramp-up count for K7. All other
3075 * are set to zero, indicating minimum divisors for
3076 * every field.
3077 *
3078 * This prevents guest kernels on AMD host with CPU
3079 * type 6, model 8 and higher from exploding due to
3080 * the rdmsr failing.
3081 */
609e36d3 3082 msr_info->data = 0x20000000;
84e0cefa 3083 break;
55cd8e5a 3084 case HV_X64_MSR_GUEST_OS_ID ... HV_X64_MSR_SINT15:
e7d9513b
AS
3085 case HV_X64_MSR_CRASH_P0 ... HV_X64_MSR_CRASH_P4:
3086 case HV_X64_MSR_CRASH_CTL:
1f4b34f8 3087 case HV_X64_MSR_STIMER0_CONFIG ... HV_X64_MSR_STIMER3_COUNT:
a2e164e7
VK
3088 case HV_X64_MSR_REENLIGHTENMENT_CONTROL:
3089 case HV_X64_MSR_TSC_EMULATION_CONTROL:
3090 case HV_X64_MSR_TSC_EMULATION_STATUS:
e83d5887 3091 return kvm_hv_get_msr_common(vcpu,
44883f01
PB
3092 msr_info->index, &msr_info->data,
3093 msr_info->host_initiated);
55cd8e5a 3094 break;
91c9c3ed 3095 case MSR_IA32_BBL_CR_CTL3:
3096 /* This legacy MSR exists but isn't fully documented in current
3097 * silicon. It is however accessed by winxp in very narrow
3098 * scenarios where it sets bit #19, itself documented as
3099 * a "reserved" bit. Best effort attempt to source coherent
3100 * read data here should the balance of the register be
3101 * interpreted by the guest:
3102 *
3103 * L2 cache control register 3: 64GB range, 256KB size,
3104 * enabled, latency 0x1, configured
3105 */
609e36d3 3106 msr_info->data = 0xbe702111;
91c9c3ed 3107 break;
2b036c6b 3108 case MSR_AMD64_OSVW_ID_LENGTH:
d6321d49 3109 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
2b036c6b 3110 return 1;
609e36d3 3111 msr_info->data = vcpu->arch.osvw.length;
2b036c6b
BO
3112 break;
3113 case MSR_AMD64_OSVW_STATUS:
d6321d49 3114 if (!guest_cpuid_has(vcpu, X86_FEATURE_OSVW))
2b036c6b 3115 return 1;
609e36d3 3116 msr_info->data = vcpu->arch.osvw.status;
2b036c6b 3117 break;
db2336a8 3118 case MSR_PLATFORM_INFO:
6fbbde9a
DS
3119 if (!msr_info->host_initiated &&
3120 !vcpu->kvm->arch.guest_can_read_msr_platform_info)
3121 return 1;
db2336a8
KH
3122 msr_info->data = vcpu->arch.msr_platform_info;
3123 break;
3124 case MSR_MISC_FEATURES_ENABLES:
3125 msr_info->data = vcpu->arch.msr_misc_features_enables;
3126 break;
191c8137
BP
3127 case MSR_K7_HWCR:
3128 msr_info->data = vcpu->arch.msr_hwcr;
3129 break;
15c4a640 3130 default:
c6702c9d 3131 if (kvm_pmu_is_valid_msr(vcpu, msr_info->index))
609e36d3 3132 return kvm_pmu_get_msr(vcpu, msr_info->index, &msr_info->data);
ed85c068 3133 if (!ignore_msrs) {
ae0f5499
BD
3134 vcpu_debug_ratelimited(vcpu, "unhandled rdmsr: 0x%x\n",
3135 msr_info->index);
ed85c068
AP
3136 return 1;
3137 } else {
fab0aa3b
EM
3138 if (report_ignored_msrs)
3139 vcpu_unimpl(vcpu, "ignored rdmsr: 0x%x\n",
3140 msr_info->index);
609e36d3 3141 msr_info->data = 0;
ed85c068
AP
3142 }
3143 break;
15c4a640 3144 }
15c4a640
CO
3145 return 0;
3146}
3147EXPORT_SYMBOL_GPL(kvm_get_msr_common);
3148
313a3dc7
CO
3149/*
3150 * Read or write a bunch of msrs. All parameters are kernel addresses.
3151 *
3152 * @return number of msrs set successfully.
3153 */
3154static int __msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs *msrs,
3155 struct kvm_msr_entry *entries,
3156 int (*do_msr)(struct kvm_vcpu *vcpu,
3157 unsigned index, u64 *data))
3158{
801e459a 3159 int i;
313a3dc7 3160
313a3dc7
CO
3161 for (i = 0; i < msrs->nmsrs; ++i)
3162 if (do_msr(vcpu, entries[i].index, &entries[i].data))
3163 break;
3164
313a3dc7
CO
3165 return i;
3166}
3167
3168/*
3169 * Read or write a bunch of msrs. Parameters are user addresses.
3170 *
3171 * @return number of msrs set successfully.
3172 */
3173static int msr_io(struct kvm_vcpu *vcpu, struct kvm_msrs __user *user_msrs,
3174 int (*do_msr)(struct kvm_vcpu *vcpu,
3175 unsigned index, u64 *data),
3176 int writeback)
3177{
3178 struct kvm_msrs msrs;
3179 struct kvm_msr_entry *entries;
3180 int r, n;
3181 unsigned size;
3182
3183 r = -EFAULT;
0e96f31e 3184 if (copy_from_user(&msrs, user_msrs, sizeof(msrs)))
313a3dc7
CO
3185 goto out;
3186
3187 r = -E2BIG;
3188 if (msrs.nmsrs >= MAX_IO_MSRS)
3189 goto out;
3190
313a3dc7 3191 size = sizeof(struct kvm_msr_entry) * msrs.nmsrs;
ff5c2c03
SL
3192 entries = memdup_user(user_msrs->entries, size);
3193 if (IS_ERR(entries)) {
3194 r = PTR_ERR(entries);
313a3dc7 3195 goto out;
ff5c2c03 3196 }
313a3dc7
CO
3197
3198 r = n = __msr_io(vcpu, &msrs, entries, do_msr);
3199 if (r < 0)
3200 goto out_free;
3201
3202 r = -EFAULT;
3203 if (writeback && copy_to_user(user_msrs->entries, entries, size))
3204 goto out_free;
3205
3206 r = n;
3207
3208out_free:
7a73c028 3209 kfree(entries);
313a3dc7
CO
3210out:
3211 return r;
3212}
3213
4d5422ce
WL
3214static inline bool kvm_can_mwait_in_guest(void)
3215{
3216 return boot_cpu_has(X86_FEATURE_MWAIT) &&
8e9b29b6
KA
3217 !boot_cpu_has_bug(X86_BUG_MONITOR) &&
3218 boot_cpu_has(X86_FEATURE_ARAT);
4d5422ce
WL
3219}
3220
784aa3d7 3221int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
018d00d2 3222{
4d5422ce 3223 int r = 0;
018d00d2
ZX
3224
3225 switch (ext) {
3226 case KVM_CAP_IRQCHIP:
3227 case KVM_CAP_HLT:
3228 case KVM_CAP_MMU_SHADOW_CACHE_CONTROL:
018d00d2 3229 case KVM_CAP_SET_TSS_ADDR:
07716717 3230 case KVM_CAP_EXT_CPUID:
9c15bb1d 3231 case KVM_CAP_EXT_EMUL_CPUID:
c8076604 3232 case KVM_CAP_CLOCKSOURCE:
7837699f 3233 case KVM_CAP_PIT:
a28e4f5a 3234 case KVM_CAP_NOP_IO_DELAY:
62d9f0db 3235 case KVM_CAP_MP_STATE:
ed848624 3236 case KVM_CAP_SYNC_MMU:
a355c85c 3237 case KVM_CAP_USER_NMI:
52d939a0 3238 case KVM_CAP_REINJECT_CONTROL:
4925663a 3239 case KVM_CAP_IRQ_INJECT_STATUS:
d34e6b17 3240 case KVM_CAP_IOEVENTFD:
f848a5a8 3241 case KVM_CAP_IOEVENTFD_NO_LENGTH:
c5ff41ce 3242 case KVM_CAP_PIT2:
e9f42757 3243 case KVM_CAP_PIT_STATE2:
b927a3ce 3244 case KVM_CAP_SET_IDENTITY_MAP_ADDR:
ffde22ac 3245 case KVM_CAP_XEN_HVM:
3cfc3092 3246 case KVM_CAP_VCPU_EVENTS:
55cd8e5a 3247 case KVM_CAP_HYPERV:
10388a07 3248 case KVM_CAP_HYPERV_VAPIC:
c25bc163 3249 case KVM_CAP_HYPERV_SPIN:
5c919412 3250 case KVM_CAP_HYPERV_SYNIC:
efc479e6 3251 case KVM_CAP_HYPERV_SYNIC2:
d3457c87 3252 case KVM_CAP_HYPERV_VP_INDEX:
faeb7833 3253 case KVM_CAP_HYPERV_EVENTFD:
c1aea919 3254 case KVM_CAP_HYPERV_TLBFLUSH:
214ff83d 3255 case KVM_CAP_HYPERV_SEND_IPI:
2bc39970 3256 case KVM_CAP_HYPERV_CPUID:
ab9f4ecb 3257 case KVM_CAP_PCI_SEGMENT:
a1efbe77 3258 case KVM_CAP_DEBUGREGS:
d2be1651 3259 case KVM_CAP_X86_ROBUST_SINGLESTEP:
2d5b5a66 3260 case KVM_CAP_XSAVE:
344d9588 3261 case KVM_CAP_ASYNC_PF:
92a1f12d 3262 case KVM_CAP_GET_TSC_KHZ:
1c0b28c2 3263 case KVM_CAP_KVMCLOCK_CTRL:
4d8b81ab 3264 case KVM_CAP_READONLY_MEM:
5f66b620 3265 case KVM_CAP_HYPERV_TIME:
100943c5 3266 case KVM_CAP_IOAPIC_POLARITY_IGNORED:
defcf51f 3267 case KVM_CAP_TSC_DEADLINE_TIMER:
90de4a18 3268 case KVM_CAP_DISABLE_QUIRKS:
d71ba788 3269 case KVM_CAP_SET_BOOT_CPU_ID:
49df6397 3270 case KVM_CAP_SPLIT_IRQCHIP:
460df4c1 3271 case KVM_CAP_IMMEDIATE_EXIT:
66bb8a06 3272 case KVM_CAP_PMU_EVENT_FILTER:
801e459a 3273 case KVM_CAP_GET_MSR_FEATURES:
6fbbde9a 3274 case KVM_CAP_MSR_PLATFORM_INFO:
c4f55198 3275 case KVM_CAP_EXCEPTION_PAYLOAD:
018d00d2
ZX
3276 r = 1;
3277 break;
01643c51
KH
3278 case KVM_CAP_SYNC_REGS:
3279 r = KVM_SYNC_X86_VALID_FIELDS;
3280 break;
e3fd9a93
PB
3281 case KVM_CAP_ADJUST_CLOCK:
3282 r = KVM_CLOCK_TSC_STABLE;
3283 break;
4d5422ce 3284 case KVM_CAP_X86_DISABLE_EXITS:
b5170063
WL
3285 r |= KVM_X86_DISABLE_EXITS_HLT | KVM_X86_DISABLE_EXITS_PAUSE |
3286 KVM_X86_DISABLE_EXITS_CSTATE;
4d5422ce
WL
3287 if(kvm_can_mwait_in_guest())
3288 r |= KVM_X86_DISABLE_EXITS_MWAIT;
668fffa3 3289 break;
6d396b55
PB
3290 case KVM_CAP_X86_SMM:
3291 /* SMBASE is usually relocated above 1M on modern chipsets,
3292 * and SMM handlers might indeed rely on 4G segment limits,
3293 * so do not report SMM to be available if real mode is
3294 * emulated via vm86 mode. Still, do not go to great lengths
3295 * to avoid userspace's usage of the feature, because it is a
3296 * fringe case that is not enabled except via specific settings
3297 * of the module parameters.
3298 */
bc226f07 3299 r = kvm_x86_ops->has_emulated_msr(MSR_IA32_SMBASE);
6d396b55 3300 break;
774ead3a
AK
3301 case KVM_CAP_VAPIC:
3302 r = !kvm_x86_ops->cpu_has_accelerated_tpr();
3303 break;
f725230a 3304 case KVM_CAP_NR_VCPUS:
8c3ba334
SL
3305 r = KVM_SOFT_MAX_VCPUS;
3306 break;
3307 case KVM_CAP_MAX_VCPUS:
f725230a
AK
3308 r = KVM_MAX_VCPUS;
3309 break;
a86cb413
TH
3310 case KVM_CAP_MAX_VCPU_ID:
3311 r = KVM_MAX_VCPU_ID;
3312 break;
a68a6a72
MT
3313 case KVM_CAP_PV_MMU: /* obsolete */
3314 r = 0;
2f333bcb 3315 break;
890ca9ae
HY
3316 case KVM_CAP_MCE:
3317 r = KVM_MAX_MCE_BANKS;
3318 break;
2d5b5a66 3319 case KVM_CAP_XCRS:
d366bf7e 3320 r = boot_cpu_has(X86_FEATURE_XSAVE);
2d5b5a66 3321 break;
92a1f12d
JR
3322 case KVM_CAP_TSC_CONTROL:
3323 r = kvm_has_tsc_control;
3324 break;
37131313
RK
3325 case KVM_CAP_X2APIC_API:
3326 r = KVM_X2APIC_API_VALID_FLAGS;
3327 break;
8fcc4b59
JM
3328 case KVM_CAP_NESTED_STATE:
3329 r = kvm_x86_ops->get_nested_state ?
be43c440 3330 kvm_x86_ops->get_nested_state(NULL, NULL, 0) : 0;
8fcc4b59 3331 break;
344c6c80 3332 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
5a0165f6
VK
3333 r = kvm_x86_ops->enable_direct_tlbflush != NULL;
3334 break;
3335 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
3336 r = kvm_x86_ops->nested_enable_evmcs != NULL;
344c6c80 3337 break;
018d00d2 3338 default:
018d00d2
ZX
3339 break;
3340 }
3341 return r;
3342
3343}
3344
043405e1
CO
3345long kvm_arch_dev_ioctl(struct file *filp,
3346 unsigned int ioctl, unsigned long arg)
3347{
3348 void __user *argp = (void __user *)arg;
3349 long r;
3350
3351 switch (ioctl) {
3352 case KVM_GET_MSR_INDEX_LIST: {
3353 struct kvm_msr_list __user *user_msr_list = argp;
3354 struct kvm_msr_list msr_list;
3355 unsigned n;
3356
3357 r = -EFAULT;
0e96f31e 3358 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
043405e1
CO
3359 goto out;
3360 n = msr_list.nmsrs;
62ef68bb 3361 msr_list.nmsrs = num_msrs_to_save + num_emulated_msrs;
0e96f31e 3362 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
043405e1
CO
3363 goto out;
3364 r = -E2BIG;
e125e7b6 3365 if (n < msr_list.nmsrs)
043405e1
CO
3366 goto out;
3367 r = -EFAULT;
3368 if (copy_to_user(user_msr_list->indices, &msrs_to_save,
3369 num_msrs_to_save * sizeof(u32)))
3370 goto out;
e125e7b6 3371 if (copy_to_user(user_msr_list->indices + num_msrs_to_save,
043405e1 3372 &emulated_msrs,
62ef68bb 3373 num_emulated_msrs * sizeof(u32)))
043405e1
CO
3374 goto out;
3375 r = 0;
3376 break;
3377 }
9c15bb1d
BP
3378 case KVM_GET_SUPPORTED_CPUID:
3379 case KVM_GET_EMULATED_CPUID: {
674eea0f
AK
3380 struct kvm_cpuid2 __user *cpuid_arg = argp;
3381 struct kvm_cpuid2 cpuid;
3382
3383 r = -EFAULT;
0e96f31e 3384 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
674eea0f 3385 goto out;
9c15bb1d
BP
3386
3387 r = kvm_dev_ioctl_get_cpuid(&cpuid, cpuid_arg->entries,
3388 ioctl);
674eea0f
AK
3389 if (r)
3390 goto out;
3391
3392 r = -EFAULT;
0e96f31e 3393 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
674eea0f
AK
3394 goto out;
3395 r = 0;
3396 break;
3397 }
890ca9ae 3398 case KVM_X86_GET_MCE_CAP_SUPPORTED: {
890ca9ae 3399 r = -EFAULT;
c45dcc71
AR
3400 if (copy_to_user(argp, &kvm_mce_cap_supported,
3401 sizeof(kvm_mce_cap_supported)))
890ca9ae
HY
3402 goto out;
3403 r = 0;
3404 break;
801e459a
TL
3405 case KVM_GET_MSR_FEATURE_INDEX_LIST: {
3406 struct kvm_msr_list __user *user_msr_list = argp;
3407 struct kvm_msr_list msr_list;
3408 unsigned int n;
3409
3410 r = -EFAULT;
3411 if (copy_from_user(&msr_list, user_msr_list, sizeof(msr_list)))
3412 goto out;
3413 n = msr_list.nmsrs;
3414 msr_list.nmsrs = num_msr_based_features;
3415 if (copy_to_user(user_msr_list, &msr_list, sizeof(msr_list)))
3416 goto out;
3417 r = -E2BIG;
3418 if (n < msr_list.nmsrs)
3419 goto out;
3420 r = -EFAULT;
3421 if (copy_to_user(user_msr_list->indices, &msr_based_features,
3422 num_msr_based_features * sizeof(u32)))
3423 goto out;
3424 r = 0;
3425 break;
3426 }
3427 case KVM_GET_MSRS:
3428 r = msr_io(NULL, argp, do_get_msr_feature, 1);
3429 break;
890ca9ae 3430 }
043405e1
CO
3431 default:
3432 r = -EINVAL;
3433 }
3434out:
3435 return r;
3436}
3437
f5f48ee1
SY
3438static void wbinvd_ipi(void *garbage)
3439{
3440 wbinvd();
3441}
3442
3443static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
3444{
e0f0bbc5 3445 return kvm_arch_has_noncoherent_dma(vcpu->kvm);
f5f48ee1
SY
3446}
3447
313a3dc7
CO
3448void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
3449{
f5f48ee1
SY
3450 /* Address WBINVD may be executed by guest */
3451 if (need_emulate_wbinvd(vcpu)) {
3452 if (kvm_x86_ops->has_wbinvd_exit())
3453 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
3454 else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
3455 smp_call_function_single(vcpu->cpu,
3456 wbinvd_ipi, NULL, 1);
3457 }
3458
313a3dc7 3459 kvm_x86_ops->vcpu_load(vcpu, cpu);
8f6055cb 3460
e7517324
WL
3461 fpregs_assert_state_consistent();
3462 if (test_thread_flag(TIF_NEED_FPU_LOAD))
3463 switch_fpu_return();
3464
0dd6a6ed
ZA
3465 /* Apply any externally detected TSC adjustments (due to suspend) */
3466 if (unlikely(vcpu->arch.tsc_offset_adjustment)) {
3467 adjust_tsc_offset_host(vcpu, vcpu->arch.tsc_offset_adjustment);
3468 vcpu->arch.tsc_offset_adjustment = 0;
105b21bb 3469 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
0dd6a6ed 3470 }
8f6055cb 3471
b0c39dc6 3472 if (unlikely(vcpu->cpu != cpu) || kvm_check_tsc_unstable()) {
6f526ec5 3473 s64 tsc_delta = !vcpu->arch.last_host_tsc ? 0 :
4ea1636b 3474 rdtsc() - vcpu->arch.last_host_tsc;
e48672fa
ZA
3475 if (tsc_delta < 0)
3476 mark_tsc_unstable("KVM discovered backwards TSC");
ce7a058a 3477
b0c39dc6 3478 if (kvm_check_tsc_unstable()) {
07c1419a 3479 u64 offset = kvm_compute_tsc_offset(vcpu,
b183aa58 3480 vcpu->arch.last_guest_tsc);
a545ab6a 3481 kvm_vcpu_write_tsc_offset(vcpu, offset);
c285545f 3482 vcpu->arch.tsc_catchup = 1;
c285545f 3483 }
a749e247
PB
3484
3485 if (kvm_lapic_hv_timer_in_use(vcpu))
3486 kvm_lapic_restart_hv_timer(vcpu);
3487
d98d07ca
MT
3488 /*
3489 * On a host with synchronized TSC, there is no need to update
3490 * kvmclock on vcpu->cpu migration
3491 */
3492 if (!vcpu->kvm->arch.use_master_clock || vcpu->cpu == -1)
0061d53d 3493 kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
c285545f 3494 if (vcpu->cpu != cpu)
1bd2009e 3495 kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
e48672fa 3496 vcpu->cpu = cpu;
6b7d7e76 3497 }
c9aaa895 3498
c9aaa895 3499 kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
313a3dc7
CO
3500}
3501
0b9f6c46
PX
3502static void kvm_steal_time_set_preempted(struct kvm_vcpu *vcpu)
3503{
3504 if (!(vcpu->arch.st.msr_val & KVM_MSR_ENABLED))
3505 return;
3506
fa55eedd 3507 vcpu->arch.st.steal.preempted = KVM_VCPU_PREEMPTED;
0b9f6c46 3508
4e335d9e 3509 kvm_write_guest_offset_cached(vcpu->kvm, &vcpu->arch.st.stime,
0b9f6c46
PX
3510 &vcpu->arch.st.steal.preempted,
3511 offsetof(struct kvm_steal_time, preempted),
3512 sizeof(vcpu->arch.st.steal.preempted));
3513}
3514
313a3dc7
CO
3515void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
3516{
cc0d907c 3517 int idx;
de63ad4c
LM
3518
3519 if (vcpu->preempted)
3520 vcpu->arch.preempted_in_kernel = !kvm_x86_ops->get_cpl(vcpu);
3521
931f261b
AA
3522 /*
3523 * Disable page faults because we're in atomic context here.
3524 * kvm_write_guest_offset_cached() would call might_fault()
3525 * that relies on pagefault_disable() to tell if there's a
3526 * bug. NOTE: the write to guest memory may not go through if
3527 * during postcopy live migration or if there's heavy guest
3528 * paging.
3529 */
3530 pagefault_disable();
cc0d907c
AA
3531 /*
3532 * kvm_memslots() will be called by
3533 * kvm_write_guest_offset_cached() so take the srcu lock.
3534 */
3535 idx = srcu_read_lock(&vcpu->kvm->srcu);
0b9f6c46 3536 kvm_steal_time_set_preempted(vcpu);
cc0d907c 3537 srcu_read_unlock(&vcpu->kvm->srcu, idx);
931f261b 3538 pagefault_enable();
02daab21 3539 kvm_x86_ops->vcpu_put(vcpu);
4ea1636b 3540 vcpu->arch.last_host_tsc = rdtsc();
efdab992 3541 /*
f9dcf08e
RK
3542 * If userspace has set any breakpoints or watchpoints, dr6 is restored
3543 * on every vmexit, but if not, we might have a stale dr6 from the
3544 * guest. do_debug expects dr6 to be cleared after it runs, do the same.
efdab992 3545 */
f9dcf08e 3546 set_debugreg(0, 6);
313a3dc7
CO
3547}
3548
313a3dc7
CO
3549static int kvm_vcpu_ioctl_get_lapic(struct kvm_vcpu *vcpu,
3550 struct kvm_lapic_state *s)
3551{
fa59cc00 3552 if (vcpu->arch.apicv_active)
d62caabb
AS
3553 kvm_x86_ops->sync_pir_to_irr(vcpu);
3554
a92e2543 3555 return kvm_apic_get_state(vcpu, s);
313a3dc7
CO
3556}
3557
3558static int kvm_vcpu_ioctl_set_lapic(struct kvm_vcpu *vcpu,
3559 struct kvm_lapic_state *s)
3560{
a92e2543
RK
3561 int r;
3562
3563 r = kvm_apic_set_state(vcpu, s);
3564 if (r)
3565 return r;
cb142eb7 3566 update_cr8_intercept(vcpu);
313a3dc7
CO
3567
3568 return 0;
3569}
3570
127a457a
MG
3571static int kvm_cpu_accept_dm_intr(struct kvm_vcpu *vcpu)
3572{
3573 return (!lapic_in_kernel(vcpu) ||
3574 kvm_apic_accept_pic_intr(vcpu));
3575}
3576
782d422b
MG
3577/*
3578 * if userspace requested an interrupt window, check that the
3579 * interrupt window is open.
3580 *
3581 * No need to exit to userspace if we already have an interrupt queued.
3582 */
3583static int kvm_vcpu_ready_for_interrupt_injection(struct kvm_vcpu *vcpu)
3584{
3585 return kvm_arch_interrupt_allowed(vcpu) &&
3586 !kvm_cpu_has_interrupt(vcpu) &&
3587 !kvm_event_needs_reinjection(vcpu) &&
3588 kvm_cpu_accept_dm_intr(vcpu);
3589}
3590
f77bc6a4
ZX
3591static int kvm_vcpu_ioctl_interrupt(struct kvm_vcpu *vcpu,
3592 struct kvm_interrupt *irq)
3593{
02cdb50f 3594 if (irq->irq >= KVM_NR_INTERRUPTS)
f77bc6a4 3595 return -EINVAL;
1c1a9ce9
SR
3596
3597 if (!irqchip_in_kernel(vcpu->kvm)) {
3598 kvm_queue_interrupt(vcpu, irq->irq, false);
3599 kvm_make_request(KVM_REQ_EVENT, vcpu);
3600 return 0;
3601 }
3602
3603 /*
3604 * With in-kernel LAPIC, we only use this to inject EXTINT, so
3605 * fail for in-kernel 8259.
3606 */
3607 if (pic_in_kernel(vcpu->kvm))
f77bc6a4 3608 return -ENXIO;
f77bc6a4 3609
1c1a9ce9
SR
3610 if (vcpu->arch.pending_external_vector != -1)
3611 return -EEXIST;
f77bc6a4 3612
1c1a9ce9 3613 vcpu->arch.pending_external_vector = irq->irq;
934bf653 3614 kvm_make_request(KVM_REQ_EVENT, vcpu);
f77bc6a4
ZX
3615 return 0;
3616}
3617
c4abb7c9
JK
3618static int kvm_vcpu_ioctl_nmi(struct kvm_vcpu *vcpu)
3619{
c4abb7c9 3620 kvm_inject_nmi(vcpu);
c4abb7c9
JK
3621
3622 return 0;
3623}
3624
f077825a
PB
3625static int kvm_vcpu_ioctl_smi(struct kvm_vcpu *vcpu)
3626{
64d60670
PB
3627 kvm_make_request(KVM_REQ_SMI, vcpu);
3628
f077825a
PB
3629 return 0;
3630}
3631
b209749f
AK
3632static int vcpu_ioctl_tpr_access_reporting(struct kvm_vcpu *vcpu,
3633 struct kvm_tpr_access_ctl *tac)
3634{
3635 if (tac->flags)
3636 return -EINVAL;
3637 vcpu->arch.tpr_access_reporting = !!tac->enabled;
3638 return 0;
3639}
3640
890ca9ae
HY
3641static int kvm_vcpu_ioctl_x86_setup_mce(struct kvm_vcpu *vcpu,
3642 u64 mcg_cap)
3643{
3644 int r;
3645 unsigned bank_num = mcg_cap & 0xff, bank;
3646
3647 r = -EINVAL;
a9e38c3e 3648 if (!bank_num || bank_num >= KVM_MAX_MCE_BANKS)
890ca9ae 3649 goto out;
c45dcc71 3650 if (mcg_cap & ~(kvm_mce_cap_supported | 0xff | 0xff0000))
890ca9ae
HY
3651 goto out;
3652 r = 0;
3653 vcpu->arch.mcg_cap = mcg_cap;
3654 /* Init IA32_MCG_CTL to all 1s */
3655 if (mcg_cap & MCG_CTL_P)
3656 vcpu->arch.mcg_ctl = ~(u64)0;
3657 /* Init IA32_MCi_CTL to all 1s */
3658 for (bank = 0; bank < bank_num; bank++)
3659 vcpu->arch.mce_banks[bank*4] = ~(u64)0;
c45dcc71 3660
92735b1b 3661 kvm_x86_ops->setup_mce(vcpu);
890ca9ae
HY
3662out:
3663 return r;
3664}
3665
3666static int kvm_vcpu_ioctl_x86_set_mce(struct kvm_vcpu *vcpu,
3667 struct kvm_x86_mce *mce)
3668{
3669 u64 mcg_cap = vcpu->arch.mcg_cap;
3670 unsigned bank_num = mcg_cap & 0xff;
3671 u64 *banks = vcpu->arch.mce_banks;
3672
3673 if (mce->bank >= bank_num || !(mce->status & MCI_STATUS_VAL))
3674 return -EINVAL;
3675 /*
3676 * if IA32_MCG_CTL is not all 1s, the uncorrected error
3677 * reporting is disabled
3678 */
3679 if ((mce->status & MCI_STATUS_UC) && (mcg_cap & MCG_CTL_P) &&
3680 vcpu->arch.mcg_ctl != ~(u64)0)
3681 return 0;
3682 banks += 4 * mce->bank;
3683 /*
3684 * if IA32_MCi_CTL is not all 1s, the uncorrected error
3685 * reporting is disabled for the bank
3686 */
3687 if ((mce->status & MCI_STATUS_UC) && banks[0] != ~(u64)0)
3688 return 0;
3689 if (mce->status & MCI_STATUS_UC) {
3690 if ((vcpu->arch.mcg_status & MCG_STATUS_MCIP) ||
fc78f519 3691 !kvm_read_cr4_bits(vcpu, X86_CR4_MCE)) {
a8eeb04a 3692 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
890ca9ae
HY
3693 return 0;
3694 }
3695 if (banks[1] & MCI_STATUS_VAL)
3696 mce->status |= MCI_STATUS_OVER;
3697 banks[2] = mce->addr;
3698 banks[3] = mce->misc;
3699 vcpu->arch.mcg_status = mce->mcg_status;
3700 banks[1] = mce->status;
3701 kvm_queue_exception(vcpu, MC_VECTOR);
3702 } else if (!(banks[1] & MCI_STATUS_VAL)
3703 || !(banks[1] & MCI_STATUS_UC)) {
3704 if (banks[1] & MCI_STATUS_VAL)
3705 mce->status |= MCI_STATUS_OVER;
3706 banks[2] = mce->addr;
3707 banks[3] = mce->misc;
3708 banks[1] = mce->status;
3709 } else
3710 banks[1] |= MCI_STATUS_OVER;
3711 return 0;
3712}
3713
3cfc3092
JK
3714static void kvm_vcpu_ioctl_x86_get_vcpu_events(struct kvm_vcpu *vcpu,
3715 struct kvm_vcpu_events *events)
3716{
7460fb4a 3717 process_nmi(vcpu);
59073aaf 3718
664f8e26 3719 /*
59073aaf
JM
3720 * The API doesn't provide the instruction length for software
3721 * exceptions, so don't report them. As long as the guest RIP
3722 * isn't advanced, we should expect to encounter the exception
3723 * again.
664f8e26 3724 */
59073aaf
JM
3725 if (kvm_exception_is_soft(vcpu->arch.exception.nr)) {
3726 events->exception.injected = 0;
3727 events->exception.pending = 0;
3728 } else {
3729 events->exception.injected = vcpu->arch.exception.injected;
3730 events->exception.pending = vcpu->arch.exception.pending;
3731 /*
3732 * For ABI compatibility, deliberately conflate
3733 * pending and injected exceptions when
3734 * KVM_CAP_EXCEPTION_PAYLOAD isn't enabled.
3735 */
3736 if (!vcpu->kvm->arch.exception_payload_enabled)
3737 events->exception.injected |=
3738 vcpu->arch.exception.pending;
3739 }
3cfc3092
JK
3740 events->exception.nr = vcpu->arch.exception.nr;
3741 events->exception.has_error_code = vcpu->arch.exception.has_error_code;
3742 events->exception.error_code = vcpu->arch.exception.error_code;
59073aaf
JM
3743 events->exception_has_payload = vcpu->arch.exception.has_payload;
3744 events->exception_payload = vcpu->arch.exception.payload;
3cfc3092 3745
03b82a30 3746 events->interrupt.injected =
04140b41 3747 vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft;
3cfc3092 3748 events->interrupt.nr = vcpu->arch.interrupt.nr;
03b82a30 3749 events->interrupt.soft = 0;
37ccdcbe 3750 events->interrupt.shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
3cfc3092
JK
3751
3752 events->nmi.injected = vcpu->arch.nmi_injected;
7460fb4a 3753 events->nmi.pending = vcpu->arch.nmi_pending != 0;
3cfc3092 3754 events->nmi.masked = kvm_x86_ops->get_nmi_mask(vcpu);
97e69aa6 3755 events->nmi.pad = 0;
3cfc3092 3756
66450a21 3757 events->sipi_vector = 0; /* never valid when reporting to user space */
3cfc3092 3758
f077825a
PB
3759 events->smi.smm = is_smm(vcpu);
3760 events->smi.pending = vcpu->arch.smi_pending;
3761 events->smi.smm_inside_nmi =
3762 !!(vcpu->arch.hflags & HF_SMM_INSIDE_NMI_MASK);
3763 events->smi.latched_init = kvm_lapic_latched_init(vcpu);
3764
dab4b911 3765 events->flags = (KVM_VCPUEVENT_VALID_NMI_PENDING
f077825a
PB
3766 | KVM_VCPUEVENT_VALID_SHADOW
3767 | KVM_VCPUEVENT_VALID_SMM);
59073aaf
JM
3768 if (vcpu->kvm->arch.exception_payload_enabled)
3769 events->flags |= KVM_VCPUEVENT_VALID_PAYLOAD;
3770
97e69aa6 3771 memset(&events->reserved, 0, sizeof(events->reserved));
3cfc3092
JK
3772}
3773
c5833c7a 3774static void kvm_smm_changed(struct kvm_vcpu *vcpu);
6ef4e07e 3775
3cfc3092
JK
3776static int kvm_vcpu_ioctl_x86_set_vcpu_events(struct kvm_vcpu *vcpu,
3777 struct kvm_vcpu_events *events)
3778{
dab4b911 3779 if (events->flags & ~(KVM_VCPUEVENT_VALID_NMI_PENDING
48005f64 3780 | KVM_VCPUEVENT_VALID_SIPI_VECTOR
f077825a 3781 | KVM_VCPUEVENT_VALID_SHADOW
59073aaf
JM
3782 | KVM_VCPUEVENT_VALID_SMM
3783 | KVM_VCPUEVENT_VALID_PAYLOAD))
3cfc3092
JK
3784 return -EINVAL;
3785
59073aaf
JM
3786 if (events->flags & KVM_VCPUEVENT_VALID_PAYLOAD) {
3787 if (!vcpu->kvm->arch.exception_payload_enabled)
3788 return -EINVAL;
3789 if (events->exception.pending)
3790 events->exception.injected = 0;
3791 else
3792 events->exception_has_payload = 0;
3793 } else {
3794 events->exception.pending = 0;
3795 events->exception_has_payload = 0;
3796 }
3797
3798 if ((events->exception.injected || events->exception.pending) &&
3799 (events->exception.nr > 31 || events->exception.nr == NMI_VECTOR))
78e546c8
PB
3800 return -EINVAL;
3801
28bf2888
DH
3802 /* INITs are latched while in SMM */
3803 if (events->flags & KVM_VCPUEVENT_VALID_SMM &&
3804 (events->smi.smm || events->smi.pending) &&
3805 vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED)
3806 return -EINVAL;
3807
7460fb4a 3808 process_nmi(vcpu);
59073aaf
JM
3809 vcpu->arch.exception.injected = events->exception.injected;
3810 vcpu->arch.exception.pending = events->exception.pending;
3cfc3092
JK
3811 vcpu->arch.exception.nr = events->exception.nr;
3812 vcpu->arch.exception.has_error_code = events->exception.has_error_code;
3813 vcpu->arch.exception.error_code = events->exception.error_code;
59073aaf
JM
3814 vcpu->arch.exception.has_payload = events->exception_has_payload;
3815 vcpu->arch.exception.payload = events->exception_payload;
3cfc3092 3816
04140b41 3817 vcpu->arch.interrupt.injected = events->interrupt.injected;
3cfc3092
JK
3818 vcpu->arch.interrupt.nr = events->interrupt.nr;
3819 vcpu->arch.interrupt.soft = events->interrupt.soft;
48005f64
JK
3820 if (events->flags & KVM_VCPUEVENT_VALID_SHADOW)
3821 kvm_x86_ops->set_interrupt_shadow(vcpu,
3822 events->interrupt.shadow);
3cfc3092
JK
3823
3824 vcpu->arch.nmi_injected = events->nmi.injected;
dab4b911
JK
3825 if (events->flags & KVM_VCPUEVENT_VALID_NMI_PENDING)
3826 vcpu->arch.nmi_pending = events->nmi.pending;
3cfc3092
JK
3827 kvm_x86_ops->set_nmi_mask(vcpu, events->nmi.masked);
3828
66450a21 3829 if (events->flags & KVM_VCPUEVENT_VALID_SIPI_VECTOR &&
bce87cce 3830 lapic_in_kernel(vcpu))
66450a21 3831 vcpu->arch.apic->sipi_vector = events->sipi_vector;
3cfc3092 3832
f077825a 3833 if (events->flags & KVM_VCPUEVENT_VALID_SMM) {
c5833c7a
SC
3834 if (!!(vcpu->arch.hflags & HF_SMM_MASK) != events->smi.smm) {
3835 if (events->smi.smm)
3836 vcpu->arch.hflags |= HF_SMM_MASK;
3837 else
3838 vcpu->arch.hflags &= ~HF_SMM_MASK;
3839 kvm_smm_changed(vcpu);
3840 }
6ef4e07e 3841
f077825a 3842 vcpu->arch.smi_pending = events->smi.pending;
f4ef1910
WL
3843
3844 if (events->smi.smm) {
3845 if (events->smi.smm_inside_nmi)
3846 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
f077825a 3847 else
f4ef1910 3848 vcpu->arch.hflags &= ~HF_SMM_INSIDE_NMI_MASK;
ff90afa7
LA
3849 }
3850
3851 if (lapic_in_kernel(vcpu)) {
3852 if (events->smi.latched_init)
3853 set_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
3854 else
3855 clear_bit(KVM_APIC_INIT, &vcpu->arch.apic->pending_events);
f077825a
PB
3856 }
3857 }
3858
3842d135
AK
3859 kvm_make_request(KVM_REQ_EVENT, vcpu);
3860
3cfc3092
JK
3861 return 0;
3862}
3863
a1efbe77
JK
3864static void kvm_vcpu_ioctl_x86_get_debugregs(struct kvm_vcpu *vcpu,
3865 struct kvm_debugregs *dbgregs)
3866{
73aaf249
JK
3867 unsigned long val;
3868
a1efbe77 3869 memcpy(dbgregs->db, vcpu->arch.db, sizeof(vcpu->arch.db));
16f8a6f9 3870 kvm_get_dr(vcpu, 6, &val);
73aaf249 3871 dbgregs->dr6 = val;
a1efbe77
JK
3872 dbgregs->dr7 = vcpu->arch.dr7;
3873 dbgregs->flags = 0;
97e69aa6 3874 memset(&dbgregs->reserved, 0, sizeof(dbgregs->reserved));
a1efbe77
JK
3875}
3876
3877static int kvm_vcpu_ioctl_x86_set_debugregs(struct kvm_vcpu *vcpu,
3878 struct kvm_debugregs *dbgregs)
3879{
3880 if (dbgregs->flags)
3881 return -EINVAL;
3882
d14bdb55
PB
3883 if (dbgregs->dr6 & ~0xffffffffull)
3884 return -EINVAL;
3885 if (dbgregs->dr7 & ~0xffffffffull)
3886 return -EINVAL;
3887
a1efbe77 3888 memcpy(vcpu->arch.db, dbgregs->db, sizeof(vcpu->arch.db));
ae561ede 3889 kvm_update_dr0123(vcpu);
a1efbe77 3890 vcpu->arch.dr6 = dbgregs->dr6;
73aaf249 3891 kvm_update_dr6(vcpu);
a1efbe77 3892 vcpu->arch.dr7 = dbgregs->dr7;
9926c9fd 3893 kvm_update_dr7(vcpu);
a1efbe77 3894
a1efbe77
JK
3895 return 0;
3896}
3897
df1daba7
PB
3898#define XSTATE_COMPACTION_ENABLED (1ULL << 63)
3899
3900static void fill_xsave(u8 *dest, struct kvm_vcpu *vcpu)
3901{
b666a4b6 3902 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
400e4b20 3903 u64 xstate_bv = xsave->header.xfeatures;
df1daba7
PB
3904 u64 valid;
3905
3906 /*
3907 * Copy legacy XSAVE area, to avoid complications with CPUID
3908 * leaves 0 and 1 in the loop below.
3909 */
3910 memcpy(dest, xsave, XSAVE_HDR_OFFSET);
3911
3912 /* Set XSTATE_BV */
00c87e9a 3913 xstate_bv &= vcpu->arch.guest_supported_xcr0 | XFEATURE_MASK_FPSSE;
df1daba7
PB
3914 *(u64 *)(dest + XSAVE_HDR_OFFSET) = xstate_bv;
3915
3916 /*
3917 * Copy each region from the possibly compacted offset to the
3918 * non-compacted offset.
3919 */
d91cab78 3920 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
df1daba7 3921 while (valid) {
abd16d68
SAS
3922 u64 xfeature_mask = valid & -valid;
3923 int xfeature_nr = fls64(xfeature_mask) - 1;
3924 void *src = get_xsave_addr(xsave, xfeature_nr);
df1daba7
PB
3925
3926 if (src) {
3927 u32 size, offset, ecx, edx;
abd16d68 3928 cpuid_count(XSTATE_CPUID, xfeature_nr,
df1daba7 3929 &size, &offset, &ecx, &edx);
abd16d68 3930 if (xfeature_nr == XFEATURE_PKRU)
38cfd5e3
PB
3931 memcpy(dest + offset, &vcpu->arch.pkru,
3932 sizeof(vcpu->arch.pkru));
3933 else
3934 memcpy(dest + offset, src, size);
3935
df1daba7
PB
3936 }
3937
abd16d68 3938 valid -= xfeature_mask;
df1daba7
PB
3939 }
3940}
3941
3942static void load_xsave(struct kvm_vcpu *vcpu, u8 *src)
3943{
b666a4b6 3944 struct xregs_state *xsave = &vcpu->arch.guest_fpu->state.xsave;
df1daba7
PB
3945 u64 xstate_bv = *(u64 *)(src + XSAVE_HDR_OFFSET);
3946 u64 valid;
3947
3948 /*
3949 * Copy legacy XSAVE area, to avoid complications with CPUID
3950 * leaves 0 and 1 in the loop below.
3951 */
3952 memcpy(xsave, src, XSAVE_HDR_OFFSET);
3953
3954 /* Set XSTATE_BV and possibly XCOMP_BV. */
400e4b20 3955 xsave->header.xfeatures = xstate_bv;
782511b0 3956 if (boot_cpu_has(X86_FEATURE_XSAVES))
3a54450b 3957 xsave->header.xcomp_bv = host_xcr0 | XSTATE_COMPACTION_ENABLED;
df1daba7
PB
3958
3959 /*
3960 * Copy each region from the non-compacted offset to the
3961 * possibly compacted offset.
3962 */
d91cab78 3963 valid = xstate_bv & ~XFEATURE_MASK_FPSSE;
df1daba7 3964 while (valid) {
abd16d68
SAS
3965 u64 xfeature_mask = valid & -valid;
3966 int xfeature_nr = fls64(xfeature_mask) - 1;
3967 void *dest = get_xsave_addr(xsave, xfeature_nr);
df1daba7
PB
3968
3969 if (dest) {
3970 u32 size, offset, ecx, edx;
abd16d68 3971 cpuid_count(XSTATE_CPUID, xfeature_nr,
df1daba7 3972 &size, &offset, &ecx, &edx);
abd16d68 3973 if (xfeature_nr == XFEATURE_PKRU)
38cfd5e3
PB
3974 memcpy(&vcpu->arch.pkru, src + offset,
3975 sizeof(vcpu->arch.pkru));
3976 else
3977 memcpy(dest, src + offset, size);
ee4100da 3978 }
df1daba7 3979
abd16d68 3980 valid -= xfeature_mask;
df1daba7
PB
3981 }
3982}
3983
2d5b5a66
SY
3984static void kvm_vcpu_ioctl_x86_get_xsave(struct kvm_vcpu *vcpu,
3985 struct kvm_xsave *guest_xsave)
3986{
d366bf7e 3987 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
df1daba7
PB
3988 memset(guest_xsave, 0, sizeof(struct kvm_xsave));
3989 fill_xsave((u8 *) guest_xsave->region, vcpu);
4344ee98 3990 } else {
2d5b5a66 3991 memcpy(guest_xsave->region,
b666a4b6 3992 &vcpu->arch.guest_fpu->state.fxsave,
c47ada30 3993 sizeof(struct fxregs_state));
2d5b5a66 3994 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)] =
d91cab78 3995 XFEATURE_MASK_FPSSE;
2d5b5a66
SY
3996 }
3997}
3998
a575813b
WL
3999#define XSAVE_MXCSR_OFFSET 24
4000
2d5b5a66
SY
4001static int kvm_vcpu_ioctl_x86_set_xsave(struct kvm_vcpu *vcpu,
4002 struct kvm_xsave *guest_xsave)
4003{
4004 u64 xstate_bv =
4005 *(u64 *)&guest_xsave->region[XSAVE_HDR_OFFSET / sizeof(u32)];
a575813b 4006 u32 mxcsr = *(u32 *)&guest_xsave->region[XSAVE_MXCSR_OFFSET / sizeof(u32)];
2d5b5a66 4007
d366bf7e 4008 if (boot_cpu_has(X86_FEATURE_XSAVE)) {
d7876f1b
PB
4009 /*
4010 * Here we allow setting states that are not present in
4011 * CPUID leaf 0xD, index 0, EDX:EAX. This is for compatibility
4012 * with old userspace.
4013 */
a575813b
WL
4014 if (xstate_bv & ~kvm_supported_xcr0() ||
4015 mxcsr & ~mxcsr_feature_mask)
d7876f1b 4016 return -EINVAL;
df1daba7 4017 load_xsave(vcpu, (u8 *)guest_xsave->region);
d7876f1b 4018 } else {
a575813b
WL
4019 if (xstate_bv & ~XFEATURE_MASK_FPSSE ||
4020 mxcsr & ~mxcsr_feature_mask)
2d5b5a66 4021 return -EINVAL;
b666a4b6 4022 memcpy(&vcpu->arch.guest_fpu->state.fxsave,
c47ada30 4023 guest_xsave->region, sizeof(struct fxregs_state));
2d5b5a66
SY
4024 }
4025 return 0;
4026}
4027
4028static void kvm_vcpu_ioctl_x86_get_xcrs(struct kvm_vcpu *vcpu,
4029 struct kvm_xcrs *guest_xcrs)
4030{
d366bf7e 4031 if (!boot_cpu_has(X86_FEATURE_XSAVE)) {
2d5b5a66
SY
4032 guest_xcrs->nr_xcrs = 0;
4033 return;
4034 }
4035
4036 guest_xcrs->nr_xcrs = 1;
4037 guest_xcrs->flags = 0;
4038 guest_xcrs->xcrs[0].xcr = XCR_XFEATURE_ENABLED_MASK;
4039 guest_xcrs->xcrs[0].value = vcpu->arch.xcr0;
4040}
4041
4042static int kvm_vcpu_ioctl_x86_set_xcrs(struct kvm_vcpu *vcpu,
4043 struct kvm_xcrs *guest_xcrs)
4044{
4045 int i, r = 0;
4046
d366bf7e 4047 if (!boot_cpu_has(X86_FEATURE_XSAVE))
2d5b5a66
SY
4048 return -EINVAL;
4049
4050 if (guest_xcrs->nr_xcrs > KVM_MAX_XCRS || guest_xcrs->flags)
4051 return -EINVAL;
4052
4053 for (i = 0; i < guest_xcrs->nr_xcrs; i++)
4054 /* Only support XCR0 currently */
c67a04cb 4055 if (guest_xcrs->xcrs[i].xcr == XCR_XFEATURE_ENABLED_MASK) {
2d5b5a66 4056 r = __kvm_set_xcr(vcpu, XCR_XFEATURE_ENABLED_MASK,
c67a04cb 4057 guest_xcrs->xcrs[i].value);
2d5b5a66
SY
4058 break;
4059 }
4060 if (r)
4061 r = -EINVAL;
4062 return r;
4063}
4064
1c0b28c2
EM
4065/*
4066 * kvm_set_guest_paused() indicates to the guest kernel that it has been
4067 * stopped by the hypervisor. This function will be called from the host only.
4068 * EINVAL is returned when the host attempts to set the flag for a guest that
4069 * does not support pv clocks.
4070 */
4071static int kvm_set_guest_paused(struct kvm_vcpu *vcpu)
4072{
0b79459b 4073 if (!vcpu->arch.pv_time_enabled)
1c0b28c2 4074 return -EINVAL;
51d59c6b 4075 vcpu->arch.pvclock_set_guest_stopped_request = true;
1c0b28c2
EM
4076 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
4077 return 0;
4078}
4079
5c919412
AS
4080static int kvm_vcpu_ioctl_enable_cap(struct kvm_vcpu *vcpu,
4081 struct kvm_enable_cap *cap)
4082{
57b119da
VK
4083 int r;
4084 uint16_t vmcs_version;
4085 void __user *user_ptr;
4086
5c919412
AS
4087 if (cap->flags)
4088 return -EINVAL;
4089
4090 switch (cap->cap) {
efc479e6
RK
4091 case KVM_CAP_HYPERV_SYNIC2:
4092 if (cap->args[0])
4093 return -EINVAL;
b2869f28
GS
4094 /* fall through */
4095
5c919412 4096 case KVM_CAP_HYPERV_SYNIC:
546d87e5
WL
4097 if (!irqchip_in_kernel(vcpu->kvm))
4098 return -EINVAL;
efc479e6
RK
4099 return kvm_hv_activate_synic(vcpu, cap->cap ==
4100 KVM_CAP_HYPERV_SYNIC2);
57b119da 4101 case KVM_CAP_HYPERV_ENLIGHTENED_VMCS:
5158917c
SC
4102 if (!kvm_x86_ops->nested_enable_evmcs)
4103 return -ENOTTY;
57b119da
VK
4104 r = kvm_x86_ops->nested_enable_evmcs(vcpu, &vmcs_version);
4105 if (!r) {
4106 user_ptr = (void __user *)(uintptr_t)cap->args[0];
4107 if (copy_to_user(user_ptr, &vmcs_version,
4108 sizeof(vmcs_version)))
4109 r = -EFAULT;
4110 }
4111 return r;
344c6c80
TL
4112 case KVM_CAP_HYPERV_DIRECT_TLBFLUSH:
4113 if (!kvm_x86_ops->enable_direct_tlbflush)
4114 return -ENOTTY;
4115
4116 return kvm_x86_ops->enable_direct_tlbflush(vcpu);
57b119da 4117
5c919412
AS
4118 default:
4119 return -EINVAL;
4120 }
4121}
4122
313a3dc7
CO
4123long kvm_arch_vcpu_ioctl(struct file *filp,
4124 unsigned int ioctl, unsigned long arg)
4125{
4126 struct kvm_vcpu *vcpu = filp->private_data;
4127 void __user *argp = (void __user *)arg;
4128 int r;
d1ac91d8
AK
4129 union {
4130 struct kvm_lapic_state *lapic;
4131 struct kvm_xsave *xsave;
4132 struct kvm_xcrs *xcrs;
4133 void *buffer;
4134 } u;
4135
9b062471
CD
4136 vcpu_load(vcpu);
4137
d1ac91d8 4138 u.buffer = NULL;
313a3dc7
CO
4139 switch (ioctl) {
4140 case KVM_GET_LAPIC: {
2204ae3c 4141 r = -EINVAL;
bce87cce 4142 if (!lapic_in_kernel(vcpu))
2204ae3c 4143 goto out;
254272ce
BG
4144 u.lapic = kzalloc(sizeof(struct kvm_lapic_state),
4145 GFP_KERNEL_ACCOUNT);
313a3dc7 4146
b772ff36 4147 r = -ENOMEM;
d1ac91d8 4148 if (!u.lapic)
b772ff36 4149 goto out;
d1ac91d8 4150 r = kvm_vcpu_ioctl_get_lapic(vcpu, u.lapic);
313a3dc7
CO
4151 if (r)
4152 goto out;
4153 r = -EFAULT;
d1ac91d8 4154 if (copy_to_user(argp, u.lapic, sizeof(struct kvm_lapic_state)))
313a3dc7
CO
4155 goto out;
4156 r = 0;
4157 break;
4158 }
4159 case KVM_SET_LAPIC: {
2204ae3c 4160 r = -EINVAL;
bce87cce 4161 if (!lapic_in_kernel(vcpu))
2204ae3c 4162 goto out;
ff5c2c03 4163 u.lapic = memdup_user(argp, sizeof(*u.lapic));
9b062471
CD
4164 if (IS_ERR(u.lapic)) {
4165 r = PTR_ERR(u.lapic);
4166 goto out_nofree;
4167 }
ff5c2c03 4168
d1ac91d8 4169 r = kvm_vcpu_ioctl_set_lapic(vcpu, u.lapic);
313a3dc7
CO
4170 break;
4171 }
f77bc6a4
ZX
4172 case KVM_INTERRUPT: {
4173 struct kvm_interrupt irq;
4174
4175 r = -EFAULT;
0e96f31e 4176 if (copy_from_user(&irq, argp, sizeof(irq)))
f77bc6a4
ZX
4177 goto out;
4178 r = kvm_vcpu_ioctl_interrupt(vcpu, &irq);
f77bc6a4
ZX
4179 break;
4180 }
c4abb7c9
JK
4181 case KVM_NMI: {
4182 r = kvm_vcpu_ioctl_nmi(vcpu);
c4abb7c9
JK
4183 break;
4184 }
f077825a
PB
4185 case KVM_SMI: {
4186 r = kvm_vcpu_ioctl_smi(vcpu);
4187 break;
4188 }
313a3dc7
CO
4189 case KVM_SET_CPUID: {
4190 struct kvm_cpuid __user *cpuid_arg = argp;
4191 struct kvm_cpuid cpuid;
4192
4193 r = -EFAULT;
0e96f31e 4194 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
313a3dc7
CO
4195 goto out;
4196 r = kvm_vcpu_ioctl_set_cpuid(vcpu, &cpuid, cpuid_arg->entries);
313a3dc7
CO
4197 break;
4198 }
07716717
DK
4199 case KVM_SET_CPUID2: {
4200 struct kvm_cpuid2 __user *cpuid_arg = argp;
4201 struct kvm_cpuid2 cpuid;
4202
4203 r = -EFAULT;
0e96f31e 4204 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
07716717
DK
4205 goto out;
4206 r = kvm_vcpu_ioctl_set_cpuid2(vcpu, &cpuid,
19355475 4207 cpuid_arg->entries);
07716717
DK
4208 break;
4209 }
4210 case KVM_GET_CPUID2: {
4211 struct kvm_cpuid2 __user *cpuid_arg = argp;
4212 struct kvm_cpuid2 cpuid;
4213
4214 r = -EFAULT;
0e96f31e 4215 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
07716717
DK
4216 goto out;
4217 r = kvm_vcpu_ioctl_get_cpuid2(vcpu, &cpuid,
19355475 4218 cpuid_arg->entries);
07716717
DK
4219 if (r)
4220 goto out;
4221 r = -EFAULT;
0e96f31e 4222 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
07716717
DK
4223 goto out;
4224 r = 0;
4225 break;
4226 }
801e459a
TL
4227 case KVM_GET_MSRS: {
4228 int idx = srcu_read_lock(&vcpu->kvm->srcu);
609e36d3 4229 r = msr_io(vcpu, argp, do_get_msr, 1);
801e459a 4230 srcu_read_unlock(&vcpu->kvm->srcu, idx);
313a3dc7 4231 break;
801e459a
TL
4232 }
4233 case KVM_SET_MSRS: {
4234 int idx = srcu_read_lock(&vcpu->kvm->srcu);
313a3dc7 4235 r = msr_io(vcpu, argp, do_set_msr, 0);
801e459a 4236 srcu_read_unlock(&vcpu->kvm->srcu, idx);
313a3dc7 4237 break;
801e459a 4238 }
b209749f
AK
4239 case KVM_TPR_ACCESS_REPORTING: {
4240 struct kvm_tpr_access_ctl tac;
4241
4242 r = -EFAULT;
0e96f31e 4243 if (copy_from_user(&tac, argp, sizeof(tac)))
b209749f
AK
4244 goto out;
4245 r = vcpu_ioctl_tpr_access_reporting(vcpu, &tac);
4246 if (r)
4247 goto out;
4248 r = -EFAULT;
0e96f31e 4249 if (copy_to_user(argp, &tac, sizeof(tac)))
b209749f
AK
4250 goto out;
4251 r = 0;
4252 break;
4253 };
b93463aa
AK
4254 case KVM_SET_VAPIC_ADDR: {
4255 struct kvm_vapic_addr va;
7301d6ab 4256 int idx;
b93463aa
AK
4257
4258 r = -EINVAL;
35754c98 4259 if (!lapic_in_kernel(vcpu))
b93463aa
AK
4260 goto out;
4261 r = -EFAULT;
0e96f31e 4262 if (copy_from_user(&va, argp, sizeof(va)))
b93463aa 4263 goto out;
7301d6ab 4264 idx = srcu_read_lock(&vcpu->kvm->srcu);
fda4e2e8 4265 r = kvm_lapic_set_vapic_addr(vcpu, va.vapic_addr);
7301d6ab 4266 srcu_read_unlock(&vcpu->kvm->srcu, idx);
b93463aa
AK
4267 break;
4268 }
890ca9ae
HY
4269 case KVM_X86_SETUP_MCE: {
4270 u64 mcg_cap;
4271
4272 r = -EFAULT;
0e96f31e 4273 if (copy_from_user(&mcg_cap, argp, sizeof(mcg_cap)))
890ca9ae
HY
4274 goto out;
4275 r = kvm_vcpu_ioctl_x86_setup_mce(vcpu, mcg_cap);
4276 break;
4277 }
4278 case KVM_X86_SET_MCE: {
4279 struct kvm_x86_mce mce;
4280
4281 r = -EFAULT;
0e96f31e 4282 if (copy_from_user(&mce, argp, sizeof(mce)))
890ca9ae
HY
4283 goto out;
4284 r = kvm_vcpu_ioctl_x86_set_mce(vcpu, &mce);
4285 break;
4286 }
3cfc3092
JK
4287 case KVM_GET_VCPU_EVENTS: {
4288 struct kvm_vcpu_events events;
4289
4290 kvm_vcpu_ioctl_x86_get_vcpu_events(vcpu, &events);
4291
4292 r = -EFAULT;
4293 if (copy_to_user(argp, &events, sizeof(struct kvm_vcpu_events)))
4294 break;
4295 r = 0;
4296 break;
4297 }
4298 case KVM_SET_VCPU_EVENTS: {
4299 struct kvm_vcpu_events events;
4300
4301 r = -EFAULT;
4302 if (copy_from_user(&events, argp, sizeof(struct kvm_vcpu_events)))
4303 break;
4304
4305 r = kvm_vcpu_ioctl_x86_set_vcpu_events(vcpu, &events);
4306 break;
4307 }
a1efbe77
JK
4308 case KVM_GET_DEBUGREGS: {
4309 struct kvm_debugregs dbgregs;
4310
4311 kvm_vcpu_ioctl_x86_get_debugregs(vcpu, &dbgregs);
4312
4313 r = -EFAULT;
4314 if (copy_to_user(argp, &dbgregs,
4315 sizeof(struct kvm_debugregs)))
4316 break;
4317 r = 0;
4318 break;
4319 }
4320 case KVM_SET_DEBUGREGS: {
4321 struct kvm_debugregs dbgregs;
4322
4323 r = -EFAULT;
4324 if (copy_from_user(&dbgregs, argp,
4325 sizeof(struct kvm_debugregs)))
4326 break;
4327
4328 r = kvm_vcpu_ioctl_x86_set_debugregs(vcpu, &dbgregs);
4329 break;
4330 }
2d5b5a66 4331 case KVM_GET_XSAVE: {
254272ce 4332 u.xsave = kzalloc(sizeof(struct kvm_xsave), GFP_KERNEL_ACCOUNT);
2d5b5a66 4333 r = -ENOMEM;
d1ac91d8 4334 if (!u.xsave)
2d5b5a66
SY
4335 break;
4336
d1ac91d8 4337 kvm_vcpu_ioctl_x86_get_xsave(vcpu, u.xsave);
2d5b5a66
SY
4338
4339 r = -EFAULT;
d1ac91d8 4340 if (copy_to_user(argp, u.xsave, sizeof(struct kvm_xsave)))
2d5b5a66
SY
4341 break;
4342 r = 0;
4343 break;
4344 }
4345 case KVM_SET_XSAVE: {
ff5c2c03 4346 u.xsave = memdup_user(argp, sizeof(*u.xsave));
9b062471
CD
4347 if (IS_ERR(u.xsave)) {
4348 r = PTR_ERR(u.xsave);
4349 goto out_nofree;
4350 }
2d5b5a66 4351
d1ac91d8 4352 r = kvm_vcpu_ioctl_x86_set_xsave(vcpu, u.xsave);
2d5b5a66
SY
4353 break;
4354 }
4355 case KVM_GET_XCRS: {
254272ce 4356 u.xcrs = kzalloc(sizeof(struct kvm_xcrs), GFP_KERNEL_ACCOUNT);
2d5b5a66 4357 r = -ENOMEM;
d1ac91d8 4358 if (!u.xcrs)
2d5b5a66
SY
4359 break;
4360
d1ac91d8 4361 kvm_vcpu_ioctl_x86_get_xcrs(vcpu, u.xcrs);
2d5b5a66
SY
4362
4363 r = -EFAULT;
d1ac91d8 4364 if (copy_to_user(argp, u.xcrs,
2d5b5a66
SY
4365 sizeof(struct kvm_xcrs)))
4366 break;
4367 r = 0;
4368 break;
4369 }
4370 case KVM_SET_XCRS: {
ff5c2c03 4371 u.xcrs = memdup_user(argp, sizeof(*u.xcrs));
9b062471
CD
4372 if (IS_ERR(u.xcrs)) {
4373 r = PTR_ERR(u.xcrs);
4374 goto out_nofree;
4375 }
2d5b5a66 4376
d1ac91d8 4377 r = kvm_vcpu_ioctl_x86_set_xcrs(vcpu, u.xcrs);
2d5b5a66
SY
4378 break;
4379 }
92a1f12d
JR
4380 case KVM_SET_TSC_KHZ: {
4381 u32 user_tsc_khz;
4382
4383 r = -EINVAL;
92a1f12d
JR
4384 user_tsc_khz = (u32)arg;
4385
4386 if (user_tsc_khz >= kvm_max_guest_tsc_khz)
4387 goto out;
4388
cc578287
ZA
4389 if (user_tsc_khz == 0)
4390 user_tsc_khz = tsc_khz;
4391
381d585c
HZ
4392 if (!kvm_set_tsc_khz(vcpu, user_tsc_khz))
4393 r = 0;
92a1f12d 4394
92a1f12d
JR
4395 goto out;
4396 }
4397 case KVM_GET_TSC_KHZ: {
cc578287 4398 r = vcpu->arch.virtual_tsc_khz;
92a1f12d
JR
4399 goto out;
4400 }
1c0b28c2
EM
4401 case KVM_KVMCLOCK_CTRL: {
4402 r = kvm_set_guest_paused(vcpu);
4403 goto out;
4404 }
5c919412
AS
4405 case KVM_ENABLE_CAP: {
4406 struct kvm_enable_cap cap;
4407
4408 r = -EFAULT;
4409 if (copy_from_user(&cap, argp, sizeof(cap)))
4410 goto out;
4411 r = kvm_vcpu_ioctl_enable_cap(vcpu, &cap);
4412 break;
4413 }
8fcc4b59
JM
4414 case KVM_GET_NESTED_STATE: {
4415 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4416 u32 user_data_size;
4417
4418 r = -EINVAL;
4419 if (!kvm_x86_ops->get_nested_state)
4420 break;
4421
4422 BUILD_BUG_ON(sizeof(user_data_size) != sizeof(user_kvm_nested_state->size));
26b471c7 4423 r = -EFAULT;
8fcc4b59 4424 if (get_user(user_data_size, &user_kvm_nested_state->size))
26b471c7 4425 break;
8fcc4b59
JM
4426
4427 r = kvm_x86_ops->get_nested_state(vcpu, user_kvm_nested_state,
4428 user_data_size);
4429 if (r < 0)
26b471c7 4430 break;
8fcc4b59
JM
4431
4432 if (r > user_data_size) {
4433 if (put_user(r, &user_kvm_nested_state->size))
26b471c7
LA
4434 r = -EFAULT;
4435 else
4436 r = -E2BIG;
4437 break;
8fcc4b59 4438 }
26b471c7 4439
8fcc4b59
JM
4440 r = 0;
4441 break;
4442 }
4443 case KVM_SET_NESTED_STATE: {
4444 struct kvm_nested_state __user *user_kvm_nested_state = argp;
4445 struct kvm_nested_state kvm_state;
ad5996d9 4446 int idx;
8fcc4b59
JM
4447
4448 r = -EINVAL;
4449 if (!kvm_x86_ops->set_nested_state)
4450 break;
4451
26b471c7 4452 r = -EFAULT;
8fcc4b59 4453 if (copy_from_user(&kvm_state, user_kvm_nested_state, sizeof(kvm_state)))
26b471c7 4454 break;
8fcc4b59 4455
26b471c7 4456 r = -EINVAL;
8fcc4b59 4457 if (kvm_state.size < sizeof(kvm_state))
26b471c7 4458 break;
8fcc4b59
JM
4459
4460 if (kvm_state.flags &
8cab6507
VK
4461 ~(KVM_STATE_NESTED_RUN_PENDING | KVM_STATE_NESTED_GUEST_MODE
4462 | KVM_STATE_NESTED_EVMCS))
26b471c7 4463 break;
8fcc4b59
JM
4464
4465 /* nested_run_pending implies guest_mode. */
8cab6507
VK
4466 if ((kvm_state.flags & KVM_STATE_NESTED_RUN_PENDING)
4467 && !(kvm_state.flags & KVM_STATE_NESTED_GUEST_MODE))
26b471c7 4468 break;
8fcc4b59 4469
ad5996d9 4470 idx = srcu_read_lock(&vcpu->kvm->srcu);
8fcc4b59 4471 r = kvm_x86_ops->set_nested_state(vcpu, user_kvm_nested_state, &kvm_state);
ad5996d9 4472 srcu_read_unlock(&vcpu->kvm->srcu, idx);
8fcc4b59
JM
4473 break;
4474 }
2bc39970
VK
4475 case KVM_GET_SUPPORTED_HV_CPUID: {
4476 struct kvm_cpuid2 __user *cpuid_arg = argp;
4477 struct kvm_cpuid2 cpuid;
4478
4479 r = -EFAULT;
4480 if (copy_from_user(&cpuid, cpuid_arg, sizeof(cpuid)))
4481 goto out;
4482
4483 r = kvm_vcpu_ioctl_get_hv_cpuid(vcpu, &cpuid,
4484 cpuid_arg->entries);
4485 if (r)
4486 goto out;
4487
4488 r = -EFAULT;
4489 if (copy_to_user(cpuid_arg, &cpuid, sizeof(cpuid)))
4490 goto out;
4491 r = 0;
4492 break;
4493 }
313a3dc7
CO
4494 default:
4495 r = -EINVAL;
4496 }
4497out:
d1ac91d8 4498 kfree(u.buffer);
9b062471
CD
4499out_nofree:
4500 vcpu_put(vcpu);
313a3dc7
CO
4501 return r;
4502}
4503
1499fa80 4504vm_fault_t kvm_arch_vcpu_fault(struct kvm_vcpu *vcpu, struct vm_fault *vmf)
5b1c1493
CO
4505{
4506 return VM_FAULT_SIGBUS;
4507}
4508
1fe779f8
CO
4509static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)
4510{
4511 int ret;
4512
4513 if (addr > (unsigned int)(-3 * PAGE_SIZE))
951179ce 4514 return -EINVAL;
1fe779f8
CO
4515 ret = kvm_x86_ops->set_tss_addr(kvm, addr);
4516 return ret;
4517}
4518
b927a3ce
SY
4519static int kvm_vm_ioctl_set_identity_map_addr(struct kvm *kvm,
4520 u64 ident_addr)
4521{
2ac52ab8 4522 return kvm_x86_ops->set_identity_map_addr(kvm, ident_addr);
b927a3ce
SY
4523}
4524
1fe779f8 4525static int kvm_vm_ioctl_set_nr_mmu_pages(struct kvm *kvm,
bc8a3d89 4526 unsigned long kvm_nr_mmu_pages)
1fe779f8
CO
4527{
4528 if (kvm_nr_mmu_pages < KVM_MIN_ALLOC_MMU_PAGES)
4529 return -EINVAL;
4530
79fac95e 4531 mutex_lock(&kvm->slots_lock);
1fe779f8
CO
4532
4533 kvm_mmu_change_mmu_pages(kvm, kvm_nr_mmu_pages);
f05e70ac 4534 kvm->arch.n_requested_mmu_pages = kvm_nr_mmu_pages;
1fe779f8 4535
79fac95e 4536 mutex_unlock(&kvm->slots_lock);
1fe779f8
CO
4537 return 0;
4538}
4539
bc8a3d89 4540static unsigned long kvm_vm_ioctl_get_nr_mmu_pages(struct kvm *kvm)
1fe779f8 4541{
39de71ec 4542 return kvm->arch.n_max_mmu_pages;
1fe779f8
CO
4543}
4544
1fe779f8
CO
4545static int kvm_vm_ioctl_get_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4546{
90bca052 4547 struct kvm_pic *pic = kvm->arch.vpic;
1fe779f8
CO
4548 int r;
4549
4550 r = 0;
4551 switch (chip->chip_id) {
4552 case KVM_IRQCHIP_PIC_MASTER:
90bca052 4553 memcpy(&chip->chip.pic, &pic->pics[0],
1fe779f8
CO
4554 sizeof(struct kvm_pic_state));
4555 break;
4556 case KVM_IRQCHIP_PIC_SLAVE:
90bca052 4557 memcpy(&chip->chip.pic, &pic->pics[1],
1fe779f8
CO
4558 sizeof(struct kvm_pic_state));
4559 break;
4560 case KVM_IRQCHIP_IOAPIC:
33392b49 4561 kvm_get_ioapic(kvm, &chip->chip.ioapic);
1fe779f8
CO
4562 break;
4563 default:
4564 r = -EINVAL;
4565 break;
4566 }
4567 return r;
4568}
4569
4570static int kvm_vm_ioctl_set_irqchip(struct kvm *kvm, struct kvm_irqchip *chip)
4571{
90bca052 4572 struct kvm_pic *pic = kvm->arch.vpic;
1fe779f8
CO
4573 int r;
4574
4575 r = 0;
4576 switch (chip->chip_id) {
4577 case KVM_IRQCHIP_PIC_MASTER:
90bca052
DH
4578 spin_lock(&pic->lock);
4579 memcpy(&pic->pics[0], &chip->chip.pic,
1fe779f8 4580 sizeof(struct kvm_pic_state));
90bca052 4581 spin_unlock(&pic->lock);
1fe779f8
CO
4582 break;
4583 case KVM_IRQCHIP_PIC_SLAVE:
90bca052
DH
4584 spin_lock(&pic->lock);
4585 memcpy(&pic->pics[1], &chip->chip.pic,
1fe779f8 4586 sizeof(struct kvm_pic_state));
90bca052 4587 spin_unlock(&pic->lock);
1fe779f8
CO
4588 break;
4589 case KVM_IRQCHIP_IOAPIC:
33392b49 4590 kvm_set_ioapic(kvm, &chip->chip.ioapic);
1fe779f8
CO
4591 break;
4592 default:
4593 r = -EINVAL;
4594 break;
4595 }
90bca052 4596 kvm_pic_update_irq(pic);
1fe779f8
CO
4597 return r;
4598}
4599
e0f63cb9
SY
4600static int kvm_vm_ioctl_get_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4601{
34f3941c
RK
4602 struct kvm_kpit_state *kps = &kvm->arch.vpit->pit_state;
4603
4604 BUILD_BUG_ON(sizeof(*ps) != sizeof(kps->channels));
4605
4606 mutex_lock(&kps->lock);
4607 memcpy(ps, &kps->channels, sizeof(*ps));
4608 mutex_unlock(&kps->lock);
2da29bcc 4609 return 0;
e0f63cb9
SY
4610}
4611
4612static int kvm_vm_ioctl_set_pit(struct kvm *kvm, struct kvm_pit_state *ps)
4613{
0185604c 4614 int i;
09edea72
RK
4615 struct kvm_pit *pit = kvm->arch.vpit;
4616
4617 mutex_lock(&pit->pit_state.lock);
34f3941c 4618 memcpy(&pit->pit_state.channels, ps, sizeof(*ps));
0185604c 4619 for (i = 0; i < 3; i++)
09edea72
RK
4620 kvm_pit_load_count(pit, i, ps->channels[i].count, 0);
4621 mutex_unlock(&pit->pit_state.lock);
2da29bcc 4622 return 0;
e9f42757
BK
4623}
4624
4625static int kvm_vm_ioctl_get_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4626{
e9f42757
BK
4627 mutex_lock(&kvm->arch.vpit->pit_state.lock);
4628 memcpy(ps->channels, &kvm->arch.vpit->pit_state.channels,
4629 sizeof(ps->channels));
4630 ps->flags = kvm->arch.vpit->pit_state.flags;
4631 mutex_unlock(&kvm->arch.vpit->pit_state.lock);
97e69aa6 4632 memset(&ps->reserved, 0, sizeof(ps->reserved));
2da29bcc 4633 return 0;
e9f42757
BK
4634}
4635
4636static int kvm_vm_ioctl_set_pit2(struct kvm *kvm, struct kvm_pit_state2 *ps)
4637{
2da29bcc 4638 int start = 0;
0185604c 4639 int i;
e9f42757 4640 u32 prev_legacy, cur_legacy;
09edea72
RK
4641 struct kvm_pit *pit = kvm->arch.vpit;
4642
4643 mutex_lock(&pit->pit_state.lock);
4644 prev_legacy = pit->pit_state.flags & KVM_PIT_FLAGS_HPET_LEGACY;
e9f42757
BK
4645 cur_legacy = ps->flags & KVM_PIT_FLAGS_HPET_LEGACY;
4646 if (!prev_legacy && cur_legacy)
4647 start = 1;
09edea72
RK
4648 memcpy(&pit->pit_state.channels, &ps->channels,
4649 sizeof(pit->pit_state.channels));
4650 pit->pit_state.flags = ps->flags;
0185604c 4651 for (i = 0; i < 3; i++)
09edea72 4652 kvm_pit_load_count(pit, i, pit->pit_state.channels[i].count,
e5e57e7a 4653 start && i == 0);
09edea72 4654 mutex_unlock(&pit->pit_state.lock);
2da29bcc 4655 return 0;
e0f63cb9
SY
4656}
4657
52d939a0
MT
4658static int kvm_vm_ioctl_reinject(struct kvm *kvm,
4659 struct kvm_reinject_control *control)
4660{
71474e2f
RK
4661 struct kvm_pit *pit = kvm->arch.vpit;
4662
4663 if (!pit)
52d939a0 4664 return -ENXIO;
b39c90b6 4665
71474e2f
RK
4666 /* pit->pit_state.lock was overloaded to prevent userspace from getting
4667 * an inconsistent state after running multiple KVM_REINJECT_CONTROL
4668 * ioctls in parallel. Use a separate lock if that ioctl isn't rare.
4669 */
4670 mutex_lock(&pit->pit_state.lock);
4671 kvm_pit_set_reinject(pit, control->pit_reinject);
4672 mutex_unlock(&pit->pit_state.lock);
b39c90b6 4673
52d939a0
MT
4674 return 0;
4675}
4676
95d4c16c 4677/**
60c34612
TY
4678 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
4679 * @kvm: kvm instance
4680 * @log: slot id and address to which we copy the log
95d4c16c 4681 *
e108ff2f
PB
4682 * Steps 1-4 below provide general overview of dirty page logging. See
4683 * kvm_get_dirty_log_protect() function description for additional details.
4684 *
4685 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
4686 * always flush the TLB (step 4) even if previous step failed and the dirty
4687 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
4688 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
4689 * writes will be marked dirty for next log read.
95d4c16c 4690 *
60c34612
TY
4691 * 1. Take a snapshot of the bit and clear it if needed.
4692 * 2. Write protect the corresponding page.
e108ff2f
PB
4693 * 3. Copy the snapshot to the userspace.
4694 * 4. Flush TLB's if needed.
5bb064dc 4695 */
60c34612 4696int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log)
5bb064dc 4697{
8fe65a82 4698 bool flush = false;
e108ff2f 4699 int r;
5bb064dc 4700
79fac95e 4701 mutex_lock(&kvm->slots_lock);
5bb064dc 4702
88178fd4
KH
4703 /*
4704 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4705 */
4706 if (kvm_x86_ops->flush_log_dirty)
4707 kvm_x86_ops->flush_log_dirty(kvm);
4708
8fe65a82 4709 r = kvm_get_dirty_log_protect(kvm, log, &flush);
198c74f4
XG
4710
4711 /*
4712 * All the TLBs can be flushed out of mmu lock, see the comments in
4713 * kvm_mmu_slot_remove_write_access().
4714 */
e108ff2f 4715 lockdep_assert_held(&kvm->slots_lock);
8fe65a82 4716 if (flush)
2a31b9db
PB
4717 kvm_flush_remote_tlbs(kvm);
4718
4719 mutex_unlock(&kvm->slots_lock);
4720 return r;
4721}
4722
4723int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm, struct kvm_clear_dirty_log *log)
4724{
4725 bool flush = false;
4726 int r;
4727
4728 mutex_lock(&kvm->slots_lock);
4729
4730 /*
4731 * Flush potentially hardware-cached dirty pages to dirty_bitmap.
4732 */
4733 if (kvm_x86_ops->flush_log_dirty)
4734 kvm_x86_ops->flush_log_dirty(kvm);
4735
4736 r = kvm_clear_dirty_log_protect(kvm, log, &flush);
4737
4738 /*
4739 * All the TLBs can be flushed out of mmu lock, see the comments in
4740 * kvm_mmu_slot_remove_write_access().
4741 */
4742 lockdep_assert_held(&kvm->slots_lock);
4743 if (flush)
198c74f4
XG
4744 kvm_flush_remote_tlbs(kvm);
4745
79fac95e 4746 mutex_unlock(&kvm->slots_lock);
5bb064dc
ZX
4747 return r;
4748}
4749
aa2fbe6d
YZ
4750int kvm_vm_ioctl_irq_line(struct kvm *kvm, struct kvm_irq_level *irq_event,
4751 bool line_status)
23d43cf9
CD
4752{
4753 if (!irqchip_in_kernel(kvm))
4754 return -ENXIO;
4755
4756 irq_event->status = kvm_set_irq(kvm, KVM_USERSPACE_IRQ_SOURCE_ID,
aa2fbe6d
YZ
4757 irq_event->irq, irq_event->level,
4758 line_status);
23d43cf9
CD
4759 return 0;
4760}
4761
e5d83c74
PB
4762int kvm_vm_ioctl_enable_cap(struct kvm *kvm,
4763 struct kvm_enable_cap *cap)
90de4a18
NA
4764{
4765 int r;
4766
4767 if (cap->flags)
4768 return -EINVAL;
4769
4770 switch (cap->cap) {
4771 case KVM_CAP_DISABLE_QUIRKS:
4772 kvm->arch.disabled_quirks = cap->args[0];
4773 r = 0;
4774 break;
49df6397
SR
4775 case KVM_CAP_SPLIT_IRQCHIP: {
4776 mutex_lock(&kvm->lock);
b053b2ae
SR
4777 r = -EINVAL;
4778 if (cap->args[0] > MAX_NR_RESERVED_IOAPIC_PINS)
4779 goto split_irqchip_unlock;
49df6397
SR
4780 r = -EEXIST;
4781 if (irqchip_in_kernel(kvm))
4782 goto split_irqchip_unlock;
557abc40 4783 if (kvm->created_vcpus)
49df6397
SR
4784 goto split_irqchip_unlock;
4785 r = kvm_setup_empty_irq_routing(kvm);
5c0aea0e 4786 if (r)
49df6397
SR
4787 goto split_irqchip_unlock;
4788 /* Pairs with irqchip_in_kernel. */
4789 smp_wmb();
49776faf 4790 kvm->arch.irqchip_mode = KVM_IRQCHIP_SPLIT;
b053b2ae 4791 kvm->arch.nr_reserved_ioapic_pins = cap->args[0];
49df6397
SR
4792 r = 0;
4793split_irqchip_unlock:
4794 mutex_unlock(&kvm->lock);
4795 break;
4796 }
37131313
RK
4797 case KVM_CAP_X2APIC_API:
4798 r = -EINVAL;
4799 if (cap->args[0] & ~KVM_X2APIC_API_VALID_FLAGS)
4800 break;
4801
4802 if (cap->args[0] & KVM_X2APIC_API_USE_32BIT_IDS)
4803 kvm->arch.x2apic_format = true;
c519265f
RK
4804 if (cap->args[0] & KVM_X2APIC_API_DISABLE_BROADCAST_QUIRK)
4805 kvm->arch.x2apic_broadcast_quirk_disabled = true;
37131313
RK
4806
4807 r = 0;
4808 break;
4d5422ce
WL
4809 case KVM_CAP_X86_DISABLE_EXITS:
4810 r = -EINVAL;
4811 if (cap->args[0] & ~KVM_X86_DISABLE_VALID_EXITS)
4812 break;
4813
4814 if ((cap->args[0] & KVM_X86_DISABLE_EXITS_MWAIT) &&
4815 kvm_can_mwait_in_guest())
4816 kvm->arch.mwait_in_guest = true;
766d3571 4817 if (cap->args[0] & KVM_X86_DISABLE_EXITS_HLT)
caa057a2 4818 kvm->arch.hlt_in_guest = true;
b31c114b
WL
4819 if (cap->args[0] & KVM_X86_DISABLE_EXITS_PAUSE)
4820 kvm->arch.pause_in_guest = true;
b5170063
WL
4821 if (cap->args[0] & KVM_X86_DISABLE_EXITS_CSTATE)
4822 kvm->arch.cstate_in_guest = true;
4d5422ce
WL
4823 r = 0;
4824 break;
6fbbde9a
DS
4825 case KVM_CAP_MSR_PLATFORM_INFO:
4826 kvm->arch.guest_can_read_msr_platform_info = cap->args[0];
4827 r = 0;
c4f55198
JM
4828 break;
4829 case KVM_CAP_EXCEPTION_PAYLOAD:
4830 kvm->arch.exception_payload_enabled = cap->args[0];
4831 r = 0;
6fbbde9a 4832 break;
90de4a18
NA
4833 default:
4834 r = -EINVAL;
4835 break;
4836 }
4837 return r;
4838}
4839
1fe779f8
CO
4840long kvm_arch_vm_ioctl(struct file *filp,
4841 unsigned int ioctl, unsigned long arg)
4842{
4843 struct kvm *kvm = filp->private_data;
4844 void __user *argp = (void __user *)arg;
367e1319 4845 int r = -ENOTTY;
f0d66275
DH
4846 /*
4847 * This union makes it completely explicit to gcc-3.x
4848 * that these two variables' stack usage should be
4849 * combined, not added together.
4850 */
4851 union {
4852 struct kvm_pit_state ps;
e9f42757 4853 struct kvm_pit_state2 ps2;
c5ff41ce 4854 struct kvm_pit_config pit_config;
f0d66275 4855 } u;
1fe779f8
CO
4856
4857 switch (ioctl) {
4858 case KVM_SET_TSS_ADDR:
4859 r = kvm_vm_ioctl_set_tss_addr(kvm, arg);
1fe779f8 4860 break;
b927a3ce
SY
4861 case KVM_SET_IDENTITY_MAP_ADDR: {
4862 u64 ident_addr;
4863
1af1ac91
DH
4864 mutex_lock(&kvm->lock);
4865 r = -EINVAL;
4866 if (kvm->created_vcpus)
4867 goto set_identity_unlock;
b927a3ce 4868 r = -EFAULT;
0e96f31e 4869 if (copy_from_user(&ident_addr, argp, sizeof(ident_addr)))
1af1ac91 4870 goto set_identity_unlock;
b927a3ce 4871 r = kvm_vm_ioctl_set_identity_map_addr(kvm, ident_addr);
1af1ac91
DH
4872set_identity_unlock:
4873 mutex_unlock(&kvm->lock);
b927a3ce
SY
4874 break;
4875 }
1fe779f8
CO
4876 case KVM_SET_NR_MMU_PAGES:
4877 r = kvm_vm_ioctl_set_nr_mmu_pages(kvm, arg);
1fe779f8
CO
4878 break;
4879 case KVM_GET_NR_MMU_PAGES:
4880 r = kvm_vm_ioctl_get_nr_mmu_pages(kvm);
4881 break;
3ddea128 4882 case KVM_CREATE_IRQCHIP: {
3ddea128 4883 mutex_lock(&kvm->lock);
09941366 4884
3ddea128 4885 r = -EEXIST;
35e6eaa3 4886 if (irqchip_in_kernel(kvm))
3ddea128 4887 goto create_irqchip_unlock;
09941366 4888
3e515705 4889 r = -EINVAL;
557abc40 4890 if (kvm->created_vcpus)
3e515705 4891 goto create_irqchip_unlock;
09941366
RK
4892
4893 r = kvm_pic_init(kvm);
4894 if (r)
3ddea128 4895 goto create_irqchip_unlock;
09941366
RK
4896
4897 r = kvm_ioapic_init(kvm);
4898 if (r) {
09941366 4899 kvm_pic_destroy(kvm);
3ddea128 4900 goto create_irqchip_unlock;
09941366
RK
4901 }
4902
399ec807
AK
4903 r = kvm_setup_default_irq_routing(kvm);
4904 if (r) {
72bb2fcd 4905 kvm_ioapic_destroy(kvm);
09941366 4906 kvm_pic_destroy(kvm);
71ba994c 4907 goto create_irqchip_unlock;
399ec807 4908 }
49776faf 4909 /* Write kvm->irq_routing before enabling irqchip_in_kernel. */
71ba994c 4910 smp_wmb();
49776faf 4911 kvm->arch.irqchip_mode = KVM_IRQCHIP_KERNEL;
3ddea128
MT
4912 create_irqchip_unlock:
4913 mutex_unlock(&kvm->lock);
1fe779f8 4914 break;
3ddea128 4915 }
7837699f 4916 case KVM_CREATE_PIT:
c5ff41ce
JK
4917 u.pit_config.flags = KVM_PIT_SPEAKER_DUMMY;
4918 goto create_pit;
4919 case KVM_CREATE_PIT2:
4920 r = -EFAULT;
4921 if (copy_from_user(&u.pit_config, argp,
4922 sizeof(struct kvm_pit_config)))
4923 goto out;
4924 create_pit:
250715a6 4925 mutex_lock(&kvm->lock);
269e05e4
AK
4926 r = -EEXIST;
4927 if (kvm->arch.vpit)
4928 goto create_pit_unlock;
7837699f 4929 r = -ENOMEM;
c5ff41ce 4930 kvm->arch.vpit = kvm_create_pit(kvm, u.pit_config.flags);
7837699f
SY
4931 if (kvm->arch.vpit)
4932 r = 0;
269e05e4 4933 create_pit_unlock:
250715a6 4934 mutex_unlock(&kvm->lock);
7837699f 4935 break;
1fe779f8
CO
4936 case KVM_GET_IRQCHIP: {
4937 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
ff5c2c03 4938 struct kvm_irqchip *chip;
1fe779f8 4939
ff5c2c03
SL
4940 chip = memdup_user(argp, sizeof(*chip));
4941 if (IS_ERR(chip)) {
4942 r = PTR_ERR(chip);
1fe779f8 4943 goto out;
ff5c2c03
SL
4944 }
4945
1fe779f8 4946 r = -ENXIO;
826da321 4947 if (!irqchip_kernel(kvm))
f0d66275
DH
4948 goto get_irqchip_out;
4949 r = kvm_vm_ioctl_get_irqchip(kvm, chip);
1fe779f8 4950 if (r)
f0d66275 4951 goto get_irqchip_out;
1fe779f8 4952 r = -EFAULT;
0e96f31e 4953 if (copy_to_user(argp, chip, sizeof(*chip)))
f0d66275 4954 goto get_irqchip_out;
1fe779f8 4955 r = 0;
f0d66275
DH
4956 get_irqchip_out:
4957 kfree(chip);
1fe779f8
CO
4958 break;
4959 }
4960 case KVM_SET_IRQCHIP: {
4961 /* 0: PIC master, 1: PIC slave, 2: IOAPIC */
ff5c2c03 4962 struct kvm_irqchip *chip;
1fe779f8 4963
ff5c2c03
SL
4964 chip = memdup_user(argp, sizeof(*chip));
4965 if (IS_ERR(chip)) {
4966 r = PTR_ERR(chip);
1fe779f8 4967 goto out;
ff5c2c03
SL
4968 }
4969
1fe779f8 4970 r = -ENXIO;
826da321 4971 if (!irqchip_kernel(kvm))
f0d66275
DH
4972 goto set_irqchip_out;
4973 r = kvm_vm_ioctl_set_irqchip(kvm, chip);
f0d66275
DH
4974 set_irqchip_out:
4975 kfree(chip);
1fe779f8
CO
4976 break;
4977 }
e0f63cb9 4978 case KVM_GET_PIT: {
e0f63cb9 4979 r = -EFAULT;
f0d66275 4980 if (copy_from_user(&u.ps, argp, sizeof(struct kvm_pit_state)))
e0f63cb9
SY
4981 goto out;
4982 r = -ENXIO;
4983 if (!kvm->arch.vpit)
4984 goto out;
f0d66275 4985 r = kvm_vm_ioctl_get_pit(kvm, &u.ps);
e0f63cb9
SY
4986 if (r)
4987 goto out;
4988 r = -EFAULT;
f0d66275 4989 if (copy_to_user(argp, &u.ps, sizeof(struct kvm_pit_state)))
e0f63cb9
SY
4990 goto out;
4991 r = 0;
4992 break;
4993 }
4994 case KVM_SET_PIT: {
e0f63cb9 4995 r = -EFAULT;
0e96f31e 4996 if (copy_from_user(&u.ps, argp, sizeof(u.ps)))
e0f63cb9
SY
4997 goto out;
4998 r = -ENXIO;
4999 if (!kvm->arch.vpit)
5000 goto out;
f0d66275 5001 r = kvm_vm_ioctl_set_pit(kvm, &u.ps);
e0f63cb9
SY
5002 break;
5003 }
e9f42757
BK
5004 case KVM_GET_PIT2: {
5005 r = -ENXIO;
5006 if (!kvm->arch.vpit)
5007 goto out;
5008 r = kvm_vm_ioctl_get_pit2(kvm, &u.ps2);
5009 if (r)
5010 goto out;
5011 r = -EFAULT;
5012 if (copy_to_user(argp, &u.ps2, sizeof(u.ps2)))
5013 goto out;
5014 r = 0;
5015 break;
5016 }
5017 case KVM_SET_PIT2: {
5018 r = -EFAULT;
5019 if (copy_from_user(&u.ps2, argp, sizeof(u.ps2)))
5020 goto out;
5021 r = -ENXIO;
5022 if (!kvm->arch.vpit)
5023 goto out;
5024 r = kvm_vm_ioctl_set_pit2(kvm, &u.ps2);
e9f42757
BK
5025 break;
5026 }
52d939a0
MT
5027 case KVM_REINJECT_CONTROL: {
5028 struct kvm_reinject_control control;
5029 r = -EFAULT;
5030 if (copy_from_user(&control, argp, sizeof(control)))
5031 goto out;
5032 r = kvm_vm_ioctl_reinject(kvm, &control);
52d939a0
MT
5033 break;
5034 }
d71ba788
PB
5035 case KVM_SET_BOOT_CPU_ID:
5036 r = 0;
5037 mutex_lock(&kvm->lock);
557abc40 5038 if (kvm->created_vcpus)
d71ba788
PB
5039 r = -EBUSY;
5040 else
5041 kvm->arch.bsp_vcpu_id = arg;
5042 mutex_unlock(&kvm->lock);
5043 break;
ffde22ac 5044 case KVM_XEN_HVM_CONFIG: {
51776043 5045 struct kvm_xen_hvm_config xhc;
ffde22ac 5046 r = -EFAULT;
51776043 5047 if (copy_from_user(&xhc, argp, sizeof(xhc)))
ffde22ac
ES
5048 goto out;
5049 r = -EINVAL;
51776043 5050 if (xhc.flags)
ffde22ac 5051 goto out;
51776043 5052 memcpy(&kvm->arch.xen_hvm_config, &xhc, sizeof(xhc));
ffde22ac
ES
5053 r = 0;
5054 break;
5055 }
afbcf7ab 5056 case KVM_SET_CLOCK: {
afbcf7ab
GC
5057 struct kvm_clock_data user_ns;
5058 u64 now_ns;
afbcf7ab
GC
5059
5060 r = -EFAULT;
5061 if (copy_from_user(&user_ns, argp, sizeof(user_ns)))
5062 goto out;
5063
5064 r = -EINVAL;
5065 if (user_ns.flags)
5066 goto out;
5067
5068 r = 0;
0bc48bea
RK
5069 /*
5070 * TODO: userspace has to take care of races with VCPU_RUN, so
5071 * kvm_gen_update_masterclock() can be cut down to locked
5072 * pvclock_update_vm_gtod_copy().
5073 */
5074 kvm_gen_update_masterclock(kvm);
e891a32e 5075 now_ns = get_kvmclock_ns(kvm);
108b249c 5076 kvm->arch.kvmclock_offset += user_ns.clock - now_ns;
0bc48bea 5077 kvm_make_all_cpus_request(kvm, KVM_REQ_CLOCK_UPDATE);
afbcf7ab
GC
5078 break;
5079 }
5080 case KVM_GET_CLOCK: {
afbcf7ab
GC
5081 struct kvm_clock_data user_ns;
5082 u64 now_ns;
5083
e891a32e 5084 now_ns = get_kvmclock_ns(kvm);
108b249c 5085 user_ns.clock = now_ns;
e3fd9a93 5086 user_ns.flags = kvm->arch.use_master_clock ? KVM_CLOCK_TSC_STABLE : 0;
97e69aa6 5087 memset(&user_ns.pad, 0, sizeof(user_ns.pad));
afbcf7ab
GC
5088
5089 r = -EFAULT;
5090 if (copy_to_user(argp, &user_ns, sizeof(user_ns)))
5091 goto out;
5092 r = 0;
5093 break;
5094 }
5acc5c06
BS
5095 case KVM_MEMORY_ENCRYPT_OP: {
5096 r = -ENOTTY;
5097 if (kvm_x86_ops->mem_enc_op)
5098 r = kvm_x86_ops->mem_enc_op(kvm, argp);
5099 break;
5100 }
69eaedee
BS
5101 case KVM_MEMORY_ENCRYPT_REG_REGION: {
5102 struct kvm_enc_region region;
5103
5104 r = -EFAULT;
5105 if (copy_from_user(&region, argp, sizeof(region)))
5106 goto out;
5107
5108 r = -ENOTTY;
5109 if (kvm_x86_ops->mem_enc_reg_region)
5110 r = kvm_x86_ops->mem_enc_reg_region(kvm, &region);
5111 break;
5112 }
5113 case KVM_MEMORY_ENCRYPT_UNREG_REGION: {
5114 struct kvm_enc_region region;
5115
5116 r = -EFAULT;
5117 if (copy_from_user(&region, argp, sizeof(region)))
5118 goto out;
5119
5120 r = -ENOTTY;
5121 if (kvm_x86_ops->mem_enc_unreg_region)
5122 r = kvm_x86_ops->mem_enc_unreg_region(kvm, &region);
5123 break;
5124 }
faeb7833
RK
5125 case KVM_HYPERV_EVENTFD: {
5126 struct kvm_hyperv_eventfd hvevfd;
5127
5128 r = -EFAULT;
5129 if (copy_from_user(&hvevfd, argp, sizeof(hvevfd)))
5130 goto out;
5131 r = kvm_vm_ioctl_hv_eventfd(kvm, &hvevfd);
5132 break;
5133 }
66bb8a06
EH
5134 case KVM_SET_PMU_EVENT_FILTER:
5135 r = kvm_vm_ioctl_set_pmu_event_filter(kvm, argp);
5136 break;
1fe779f8 5137 default:
ad6260da 5138 r = -ENOTTY;
1fe779f8
CO
5139 }
5140out:
5141 return r;
5142}
5143
a16b043c 5144static void kvm_init_msr_list(void)
043405e1 5145{
24c29b7a 5146 struct x86_pmu_capability x86_pmu;
043405e1 5147 u32 dummy[2];
7a5ee6ed 5148 unsigned i;
043405e1 5149
e2ada66e 5150 BUILD_BUG_ON_MSG(INTEL_PMC_MAX_FIXED != 4,
7a5ee6ed 5151 "Please update the fixed PMCs in msrs_to_saved_all[]");
24c29b7a
PB
5152
5153 perf_get_x86_pmu_capability(&x86_pmu);
e2ada66e 5154
6cbee2b9
XL
5155 num_msrs_to_save = 0;
5156 num_emulated_msrs = 0;
5157 num_msr_based_features = 0;
5158
7a5ee6ed
CQ
5159 for (i = 0; i < ARRAY_SIZE(msrs_to_save_all); i++) {
5160 if (rdmsr_safe(msrs_to_save_all[i], &dummy[0], &dummy[1]) < 0)
043405e1 5161 continue;
93c4adc7
PB
5162
5163 /*
5164 * Even MSRs that are valid in the host may not be exposed
9dbe6cf9 5165 * to the guests in some cases.
93c4adc7 5166 */
7a5ee6ed 5167 switch (msrs_to_save_all[i]) {
93c4adc7 5168 case MSR_IA32_BNDCFGS:
503234b3 5169 if (!kvm_mpx_supported())
93c4adc7
PB
5170 continue;
5171 break;
9dbe6cf9
PB
5172 case MSR_TSC_AUX:
5173 if (!kvm_x86_ops->rdtscp_supported())
5174 continue;
5175 break;
bf8c55d8
CP
5176 case MSR_IA32_RTIT_CTL:
5177 case MSR_IA32_RTIT_STATUS:
5178 if (!kvm_x86_ops->pt_supported())
5179 continue;
5180 break;
5181 case MSR_IA32_RTIT_CR3_MATCH:
5182 if (!kvm_x86_ops->pt_supported() ||
5183 !intel_pt_validate_hw_cap(PT_CAP_cr3_filtering))
5184 continue;
5185 break;
5186 case MSR_IA32_RTIT_OUTPUT_BASE:
5187 case MSR_IA32_RTIT_OUTPUT_MASK:
5188 if (!kvm_x86_ops->pt_supported() ||
5189 (!intel_pt_validate_hw_cap(PT_CAP_topa_output) &&
5190 !intel_pt_validate_hw_cap(PT_CAP_single_range_output)))
5191 continue;
5192 break;
5193 case MSR_IA32_RTIT_ADDR0_A ... MSR_IA32_RTIT_ADDR3_B: {
5194 if (!kvm_x86_ops->pt_supported() ||
7a5ee6ed 5195 msrs_to_save_all[i] - MSR_IA32_RTIT_ADDR0_A >=
bf8c55d8
CP
5196 intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2)
5197 continue;
5198 break;
cf05a67b 5199 case MSR_ARCH_PERFMON_PERFCTR0 ... MSR_ARCH_PERFMON_PERFCTR0 + 17:
7a5ee6ed 5200 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_PERFCTR0 >=
24c29b7a
PB
5201 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5202 continue;
5203 break;
cf05a67b 5204 case MSR_ARCH_PERFMON_EVENTSEL0 ... MSR_ARCH_PERFMON_EVENTSEL0 + 17:
7a5ee6ed 5205 if (msrs_to_save_all[i] - MSR_ARCH_PERFMON_EVENTSEL0 >=
24c29b7a
PB
5206 min(INTEL_PMC_MAX_GENERIC, x86_pmu.num_counters_gp))
5207 continue;
bf8c55d8 5208 }
93c4adc7
PB
5209 default:
5210 break;
5211 }
5212
7a5ee6ed 5213 msrs_to_save[num_msrs_to_save++] = msrs_to_save_all[i];
043405e1 5214 }
62ef68bb 5215
7a5ee6ed
CQ
5216 for (i = 0; i < ARRAY_SIZE(emulated_msrs_all); i++) {
5217 if (!kvm_x86_ops->has_emulated_msr(emulated_msrs_all[i]))
bc226f07 5218 continue;
62ef68bb 5219
7a5ee6ed 5220 emulated_msrs[num_emulated_msrs++] = emulated_msrs_all[i];
62ef68bb 5221 }
801e459a 5222
7a5ee6ed 5223 for (i = 0; i < ARRAY_SIZE(msr_based_features_all); i++) {
801e459a
TL
5224 struct kvm_msr_entry msr;
5225
7a5ee6ed 5226 msr.index = msr_based_features_all[i];
66421c1e 5227 if (kvm_get_msr_feature(&msr))
801e459a
TL
5228 continue;
5229
7a5ee6ed 5230 msr_based_features[num_msr_based_features++] = msr_based_features_all[i];
801e459a 5231 }
043405e1
CO
5232}
5233
bda9020e
MT
5234static int vcpu_mmio_write(struct kvm_vcpu *vcpu, gpa_t addr, int len,
5235 const void *v)
bbd9b64e 5236{
70252a10
AK
5237 int handled = 0;
5238 int n;
5239
5240 do {
5241 n = min(len, 8);
bce87cce 5242 if (!(lapic_in_kernel(vcpu) &&
e32edf4f
NN
5243 !kvm_iodevice_write(vcpu, &vcpu->arch.apic->dev, addr, n, v))
5244 && kvm_io_bus_write(vcpu, KVM_MMIO_BUS, addr, n, v))
70252a10
AK
5245 break;
5246 handled += n;
5247 addr += n;
5248 len -= n;
5249 v += n;
5250 } while (len);
bbd9b64e 5251
70252a10 5252 return handled;
bbd9b64e
CO
5253}
5254
bda9020e 5255static int vcpu_mmio_read(struct kvm_vcpu *vcpu, gpa_t addr, int len, void *v)
bbd9b64e 5256{
70252a10
AK
5257 int handled = 0;
5258 int n;
5259
5260 do {
5261 n = min(len, 8);
bce87cce 5262 if (!(lapic_in_kernel(vcpu) &&
e32edf4f
NN
5263 !kvm_iodevice_read(vcpu, &vcpu->arch.apic->dev,
5264 addr, n, v))
5265 && kvm_io_bus_read(vcpu, KVM_MMIO_BUS, addr, n, v))
70252a10 5266 break;
e39d200f 5267 trace_kvm_mmio(KVM_TRACE_MMIO_READ, n, addr, v);
70252a10
AK
5268 handled += n;
5269 addr += n;
5270 len -= n;
5271 v += n;
5272 } while (len);
bbd9b64e 5273
70252a10 5274 return handled;
bbd9b64e
CO
5275}
5276
2dafc6c2
GN
5277static void kvm_set_segment(struct kvm_vcpu *vcpu,
5278 struct kvm_segment *var, int seg)
5279{
5280 kvm_x86_ops->set_segment(vcpu, var, seg);
5281}
5282
5283void kvm_get_segment(struct kvm_vcpu *vcpu,
5284 struct kvm_segment *var, int seg)
5285{
5286 kvm_x86_ops->get_segment(vcpu, var, seg);
5287}
5288
54987b7a
PB
5289gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
5290 struct x86_exception *exception)
02f59dc9
JR
5291{
5292 gpa_t t_gpa;
02f59dc9
JR
5293
5294 BUG_ON(!mmu_is_nested(vcpu));
5295
5296 /* NPT walks are always user-walks */
5297 access |= PFERR_USER_MASK;
44dd3ffa 5298 t_gpa = vcpu->arch.mmu->gva_to_gpa(vcpu, gpa, access, exception);
02f59dc9
JR
5299
5300 return t_gpa;
5301}
5302
ab9ae313
AK
5303gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
5304 struct x86_exception *exception)
1871c602
GN
5305{
5306 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
ab9ae313 5307 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
1871c602
GN
5308}
5309
ab9ae313
AK
5310 gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
5311 struct x86_exception *exception)
1871c602
GN
5312{
5313 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5314 access |= PFERR_FETCH_MASK;
ab9ae313 5315 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
1871c602
GN
5316}
5317
ab9ae313
AK
5318gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
5319 struct x86_exception *exception)
1871c602
GN
5320{
5321 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
5322 access |= PFERR_WRITE_MASK;
ab9ae313 5323 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
1871c602
GN
5324}
5325
5326/* uses this to access any guest's mapped memory without checking CPL */
ab9ae313
AK
5327gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
5328 struct x86_exception *exception)
1871c602 5329{
ab9ae313 5330 return vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, 0, exception);
1871c602
GN
5331}
5332
5333static int kvm_read_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5334 struct kvm_vcpu *vcpu, u32 access,
bcc55cba 5335 struct x86_exception *exception)
bbd9b64e
CO
5336{
5337 void *data = val;
10589a46 5338 int r = X86EMUL_CONTINUE;
bbd9b64e
CO
5339
5340 while (bytes) {
14dfe855 5341 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access,
ab9ae313 5342 exception);
bbd9b64e 5343 unsigned offset = addr & (PAGE_SIZE-1);
77c2002e 5344 unsigned toread = min(bytes, (unsigned)PAGE_SIZE - offset);
bbd9b64e
CO
5345 int ret;
5346
bcc55cba 5347 if (gpa == UNMAPPED_GVA)
ab9ae313 5348 return X86EMUL_PROPAGATE_FAULT;
54bf36aa
PB
5349 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, data,
5350 offset, toread);
10589a46 5351 if (ret < 0) {
c3cd7ffa 5352 r = X86EMUL_IO_NEEDED;
10589a46
MT
5353 goto out;
5354 }
bbd9b64e 5355
77c2002e
IE
5356 bytes -= toread;
5357 data += toread;
5358 addr += toread;
bbd9b64e 5359 }
10589a46 5360out:
10589a46 5361 return r;
bbd9b64e 5362}
77c2002e 5363
1871c602 5364/* used for instruction fetching */
0f65dd70
AK
5365static int kvm_fetch_guest_virt(struct x86_emulate_ctxt *ctxt,
5366 gva_t addr, void *val, unsigned int bytes,
bcc55cba 5367 struct x86_exception *exception)
1871c602 5368{
0f65dd70 5369 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
1871c602 5370 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
44583cba
PB
5371 unsigned offset;
5372 int ret;
0f65dd70 5373
44583cba
PB
5374 /* Inline kvm_read_guest_virt_helper for speed. */
5375 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr, access|PFERR_FETCH_MASK,
5376 exception);
5377 if (unlikely(gpa == UNMAPPED_GVA))
5378 return X86EMUL_PROPAGATE_FAULT;
5379
5380 offset = addr & (PAGE_SIZE-1);
5381 if (WARN_ON(offset + bytes > PAGE_SIZE))
5382 bytes = (unsigned)PAGE_SIZE - offset;
54bf36aa
PB
5383 ret = kvm_vcpu_read_guest_page(vcpu, gpa >> PAGE_SHIFT, val,
5384 offset, bytes);
44583cba
PB
5385 if (unlikely(ret < 0))
5386 return X86EMUL_IO_NEEDED;
5387
5388 return X86EMUL_CONTINUE;
1871c602
GN
5389}
5390
ce14e868 5391int kvm_read_guest_virt(struct kvm_vcpu *vcpu,
0f65dd70 5392 gva_t addr, void *val, unsigned int bytes,
bcc55cba 5393 struct x86_exception *exception)
1871c602
GN
5394{
5395 u32 access = (kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0;
0f65dd70 5396
353c0956
PB
5397 /*
5398 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5399 * is returned, but our callers are not ready for that and they blindly
5400 * call kvm_inject_page_fault. Ensure that they at least do not leak
5401 * uninitialized kernel stack memory into cr2 and error code.
5402 */
5403 memset(exception, 0, sizeof(*exception));
1871c602 5404 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access,
bcc55cba 5405 exception);
1871c602 5406}
064aea77 5407EXPORT_SYMBOL_GPL(kvm_read_guest_virt);
1871c602 5408
ce14e868
PB
5409static int emulator_read_std(struct x86_emulate_ctxt *ctxt,
5410 gva_t addr, void *val, unsigned int bytes,
3c9fa24c 5411 struct x86_exception *exception, bool system)
1871c602 5412{
0f65dd70 5413 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
3c9fa24c
PB
5414 u32 access = 0;
5415
5416 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5417 access |= PFERR_USER_MASK;
5418
5419 return kvm_read_guest_virt_helper(addr, val, bytes, vcpu, access, exception);
1871c602
GN
5420}
5421
7a036a6f
RK
5422static int kvm_read_guest_phys_system(struct x86_emulate_ctxt *ctxt,
5423 unsigned long addr, void *val, unsigned int bytes)
5424{
5425 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5426 int r = kvm_vcpu_read_guest(vcpu, addr, val, bytes);
5427
5428 return r < 0 ? X86EMUL_IO_NEEDED : X86EMUL_CONTINUE;
5429}
5430
ce14e868
PB
5431static int kvm_write_guest_virt_helper(gva_t addr, void *val, unsigned int bytes,
5432 struct kvm_vcpu *vcpu, u32 access,
5433 struct x86_exception *exception)
77c2002e
IE
5434{
5435 void *data = val;
5436 int r = X86EMUL_CONTINUE;
5437
5438 while (bytes) {
14dfe855 5439 gpa_t gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, addr,
ce14e868 5440 access,
ab9ae313 5441 exception);
77c2002e
IE
5442 unsigned offset = addr & (PAGE_SIZE-1);
5443 unsigned towrite = min(bytes, (unsigned)PAGE_SIZE - offset);
5444 int ret;
5445
bcc55cba 5446 if (gpa == UNMAPPED_GVA)
ab9ae313 5447 return X86EMUL_PROPAGATE_FAULT;
54bf36aa 5448 ret = kvm_vcpu_write_guest(vcpu, gpa, data, towrite);
77c2002e 5449 if (ret < 0) {
c3cd7ffa 5450 r = X86EMUL_IO_NEEDED;
77c2002e
IE
5451 goto out;
5452 }
5453
5454 bytes -= towrite;
5455 data += towrite;
5456 addr += towrite;
5457 }
5458out:
5459 return r;
5460}
ce14e868
PB
5461
5462static int emulator_write_std(struct x86_emulate_ctxt *ctxt, gva_t addr, void *val,
3c9fa24c
PB
5463 unsigned int bytes, struct x86_exception *exception,
5464 bool system)
ce14e868
PB
5465{
5466 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
3c9fa24c
PB
5467 u32 access = PFERR_WRITE_MASK;
5468
5469 if (!system && kvm_x86_ops->get_cpl(vcpu) == 3)
5470 access |= PFERR_USER_MASK;
ce14e868
PB
5471
5472 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
3c9fa24c 5473 access, exception);
ce14e868
PB
5474}
5475
5476int kvm_write_guest_virt_system(struct kvm_vcpu *vcpu, gva_t addr, void *val,
5477 unsigned int bytes, struct x86_exception *exception)
5478{
c595ceee
PB
5479 /* kvm_write_guest_virt_system can pull in tons of pages. */
5480 vcpu->arch.l1tf_flush_l1d = true;
5481
541ab2ae
FH
5482 /*
5483 * FIXME: this should call handle_emulation_failure if X86EMUL_IO_NEEDED
5484 * is returned, but our callers are not ready for that and they blindly
5485 * call kvm_inject_page_fault. Ensure that they at least do not leak
5486 * uninitialized kernel stack memory into cr2 and error code.
5487 */
5488 memset(exception, 0, sizeof(*exception));
ce14e868
PB
5489 return kvm_write_guest_virt_helper(addr, val, bytes, vcpu,
5490 PFERR_WRITE_MASK, exception);
5491}
6a4d7550 5492EXPORT_SYMBOL_GPL(kvm_write_guest_virt_system);
77c2002e 5493
082d06ed
WL
5494int handle_ud(struct kvm_vcpu *vcpu)
5495{
b3dc0695 5496 static const char kvm_emulate_prefix[] = { __KVM_EMULATE_PREFIX };
6c86eedc 5497 int emul_type = EMULTYPE_TRAP_UD;
6c86eedc
WL
5498 char sig[5]; /* ud2; .ascii "kvm" */
5499 struct x86_exception e;
5500
5501 if (force_emulation_prefix &&
3c9fa24c
PB
5502 kvm_read_guest_virt(vcpu, kvm_get_linear_rip(vcpu),
5503 sig, sizeof(sig), &e) == 0 &&
b3dc0695 5504 memcmp(sig, kvm_emulate_prefix, sizeof(sig)) == 0) {
6c86eedc 5505 kvm_rip_write(vcpu, kvm_rip_read(vcpu) + sizeof(sig));
b4000606 5506 emul_type = EMULTYPE_TRAP_UD_FORCED;
6c86eedc 5507 }
082d06ed 5508
60fc3d02 5509 return kvm_emulate_instruction(vcpu, emul_type);
082d06ed
WL
5510}
5511EXPORT_SYMBOL_GPL(handle_ud);
5512
0f89b207
TL
5513static int vcpu_is_mmio_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5514 gpa_t gpa, bool write)
5515{
5516 /* For APIC access vmexit */
5517 if ((gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5518 return 1;
5519
5520 if (vcpu_match_mmio_gpa(vcpu, gpa)) {
5521 trace_vcpu_match_mmio(gva, gpa, write, true);
5522 return 1;
5523 }
5524
5525 return 0;
5526}
5527
af7cc7d1
XG
5528static int vcpu_mmio_gva_to_gpa(struct kvm_vcpu *vcpu, unsigned long gva,
5529 gpa_t *gpa, struct x86_exception *exception,
5530 bool write)
5531{
97d64b78
AK
5532 u32 access = ((kvm_x86_ops->get_cpl(vcpu) == 3) ? PFERR_USER_MASK : 0)
5533 | (write ? PFERR_WRITE_MASK : 0);
af7cc7d1 5534
be94f6b7
HH
5535 /*
5536 * currently PKRU is only applied to ept enabled guest so
5537 * there is no pkey in EPT page table for L1 guest or EPT
5538 * shadow page table for L2 guest.
5539 */
97d64b78 5540 if (vcpu_match_mmio_gva(vcpu, gva)
97ec8c06 5541 && !permission_fault(vcpu, vcpu->arch.walk_mmu,
871bd034 5542 vcpu->arch.mmio_access, 0, access)) {
bebb106a
XG
5543 *gpa = vcpu->arch.mmio_gfn << PAGE_SHIFT |
5544 (gva & (PAGE_SIZE - 1));
4f022648 5545 trace_vcpu_match_mmio(gva, *gpa, write, false);
bebb106a
XG
5546 return 1;
5547 }
5548
af7cc7d1
XG
5549 *gpa = vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, access, exception);
5550
5551 if (*gpa == UNMAPPED_GVA)
5552 return -1;
5553
0f89b207 5554 return vcpu_is_mmio_gpa(vcpu, gva, *gpa, write);
af7cc7d1
XG
5555}
5556
3200f405 5557int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
bcc55cba 5558 const void *val, int bytes)
bbd9b64e
CO
5559{
5560 int ret;
5561
54bf36aa 5562 ret = kvm_vcpu_write_guest(vcpu, gpa, val, bytes);
9f811285 5563 if (ret < 0)
bbd9b64e 5564 return 0;
0eb05bf2 5565 kvm_page_track_write(vcpu, gpa, val, bytes);
bbd9b64e
CO
5566 return 1;
5567}
5568
77d197b2
XG
5569struct read_write_emulator_ops {
5570 int (*read_write_prepare)(struct kvm_vcpu *vcpu, void *val,
5571 int bytes);
5572 int (*read_write_emulate)(struct kvm_vcpu *vcpu, gpa_t gpa,
5573 void *val, int bytes);
5574 int (*read_write_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5575 int bytes, void *val);
5576 int (*read_write_exit_mmio)(struct kvm_vcpu *vcpu, gpa_t gpa,
5577 void *val, int bytes);
5578 bool write;
5579};
5580
5581static int read_prepare(struct kvm_vcpu *vcpu, void *val, int bytes)
5582{
5583 if (vcpu->mmio_read_completed) {
77d197b2 5584 trace_kvm_mmio(KVM_TRACE_MMIO_READ, bytes,
e39d200f 5585 vcpu->mmio_fragments[0].gpa, val);
77d197b2
XG
5586 vcpu->mmio_read_completed = 0;
5587 return 1;
5588 }
5589
5590 return 0;
5591}
5592
5593static int read_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5594 void *val, int bytes)
5595{
54bf36aa 5596 return !kvm_vcpu_read_guest(vcpu, gpa, val, bytes);
77d197b2
XG
5597}
5598
5599static int write_emulate(struct kvm_vcpu *vcpu, gpa_t gpa,
5600 void *val, int bytes)
5601{
5602 return emulator_write_phys(vcpu, gpa, val, bytes);
5603}
5604
5605static int write_mmio(struct kvm_vcpu *vcpu, gpa_t gpa, int bytes, void *val)
5606{
e39d200f 5607 trace_kvm_mmio(KVM_TRACE_MMIO_WRITE, bytes, gpa, val);
77d197b2
XG
5608 return vcpu_mmio_write(vcpu, gpa, bytes, val);
5609}
5610
5611static int read_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5612 void *val, int bytes)
5613{
e39d200f 5614 trace_kvm_mmio(KVM_TRACE_MMIO_READ_UNSATISFIED, bytes, gpa, NULL);
77d197b2
XG
5615 return X86EMUL_IO_NEEDED;
5616}
5617
5618static int write_exit_mmio(struct kvm_vcpu *vcpu, gpa_t gpa,
5619 void *val, int bytes)
5620{
f78146b0
AK
5621 struct kvm_mmio_fragment *frag = &vcpu->mmio_fragments[0];
5622
87da7e66 5623 memcpy(vcpu->run->mmio.data, frag->data, min(8u, frag->len));
77d197b2
XG
5624 return X86EMUL_CONTINUE;
5625}
5626
0fbe9b0b 5627static const struct read_write_emulator_ops read_emultor = {
77d197b2
XG
5628 .read_write_prepare = read_prepare,
5629 .read_write_emulate = read_emulate,
5630 .read_write_mmio = vcpu_mmio_read,
5631 .read_write_exit_mmio = read_exit_mmio,
5632};
5633
0fbe9b0b 5634static const struct read_write_emulator_ops write_emultor = {
77d197b2
XG
5635 .read_write_emulate = write_emulate,
5636 .read_write_mmio = write_mmio,
5637 .read_write_exit_mmio = write_exit_mmio,
5638 .write = true,
5639};
5640
22388a3c
XG
5641static int emulator_read_write_onepage(unsigned long addr, void *val,
5642 unsigned int bytes,
5643 struct x86_exception *exception,
5644 struct kvm_vcpu *vcpu,
0fbe9b0b 5645 const struct read_write_emulator_ops *ops)
bbd9b64e 5646{
af7cc7d1
XG
5647 gpa_t gpa;
5648 int handled, ret;
22388a3c 5649 bool write = ops->write;
f78146b0 5650 struct kvm_mmio_fragment *frag;
0f89b207
TL
5651 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
5652
5653 /*
5654 * If the exit was due to a NPF we may already have a GPA.
5655 * If the GPA is present, use it to avoid the GVA to GPA table walk.
5656 * Note, this cannot be used on string operations since string
5657 * operation using rep will only have the initial GPA from the NPF
5658 * occurred.
5659 */
5660 if (vcpu->arch.gpa_available &&
5661 emulator_can_use_gpa(ctxt) &&
618232e2
BS
5662 (addr & ~PAGE_MASK) == (vcpu->arch.gpa_val & ~PAGE_MASK)) {
5663 gpa = vcpu->arch.gpa_val;
5664 ret = vcpu_is_mmio_gpa(vcpu, addr, gpa, write);
5665 } else {
5666 ret = vcpu_mmio_gva_to_gpa(vcpu, addr, &gpa, exception, write);
5667 if (ret < 0)
5668 return X86EMUL_PROPAGATE_FAULT;
0f89b207 5669 }
10589a46 5670
618232e2 5671 if (!ret && ops->read_write_emulate(vcpu, gpa, val, bytes))
bbd9b64e
CO
5672 return X86EMUL_CONTINUE;
5673
bbd9b64e
CO
5674 /*
5675 * Is this MMIO handled locally?
5676 */
22388a3c 5677 handled = ops->read_write_mmio(vcpu, gpa, bytes, val);
70252a10 5678 if (handled == bytes)
bbd9b64e 5679 return X86EMUL_CONTINUE;
bbd9b64e 5680
70252a10
AK
5681 gpa += handled;
5682 bytes -= handled;
5683 val += handled;
5684
87da7e66
XG
5685 WARN_ON(vcpu->mmio_nr_fragments >= KVM_MAX_MMIO_FRAGMENTS);
5686 frag = &vcpu->mmio_fragments[vcpu->mmio_nr_fragments++];
5687 frag->gpa = gpa;
5688 frag->data = val;
5689 frag->len = bytes;
f78146b0 5690 return X86EMUL_CONTINUE;
bbd9b64e
CO
5691}
5692
52eb5a6d
XL
5693static int emulator_read_write(struct x86_emulate_ctxt *ctxt,
5694 unsigned long addr,
22388a3c
XG
5695 void *val, unsigned int bytes,
5696 struct x86_exception *exception,
0fbe9b0b 5697 const struct read_write_emulator_ops *ops)
bbd9b64e 5698{
0f65dd70 5699 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
f78146b0
AK
5700 gpa_t gpa;
5701 int rc;
5702
5703 if (ops->read_write_prepare &&
5704 ops->read_write_prepare(vcpu, val, bytes))
5705 return X86EMUL_CONTINUE;
5706
5707 vcpu->mmio_nr_fragments = 0;
0f65dd70 5708
bbd9b64e
CO
5709 /* Crossing a page boundary? */
5710 if (((addr + bytes - 1) ^ addr) & PAGE_MASK) {
f78146b0 5711 int now;
bbd9b64e
CO
5712
5713 now = -addr & ~PAGE_MASK;
22388a3c
XG
5714 rc = emulator_read_write_onepage(addr, val, now, exception,
5715 vcpu, ops);
5716
bbd9b64e
CO
5717 if (rc != X86EMUL_CONTINUE)
5718 return rc;
5719 addr += now;
bac15531
NA
5720 if (ctxt->mode != X86EMUL_MODE_PROT64)
5721 addr = (u32)addr;
bbd9b64e
CO
5722 val += now;
5723 bytes -= now;
5724 }
22388a3c 5725
f78146b0
AK
5726 rc = emulator_read_write_onepage(addr, val, bytes, exception,
5727 vcpu, ops);
5728 if (rc != X86EMUL_CONTINUE)
5729 return rc;
5730
5731 if (!vcpu->mmio_nr_fragments)
5732 return rc;
5733
5734 gpa = vcpu->mmio_fragments[0].gpa;
5735
5736 vcpu->mmio_needed = 1;
5737 vcpu->mmio_cur_fragment = 0;
5738
87da7e66 5739 vcpu->run->mmio.len = min(8u, vcpu->mmio_fragments[0].len);
f78146b0
AK
5740 vcpu->run->mmio.is_write = vcpu->mmio_is_write = ops->write;
5741 vcpu->run->exit_reason = KVM_EXIT_MMIO;
5742 vcpu->run->mmio.phys_addr = gpa;
5743
5744 return ops->read_write_exit_mmio(vcpu, gpa, val, bytes);
22388a3c
XG
5745}
5746
5747static int emulator_read_emulated(struct x86_emulate_ctxt *ctxt,
5748 unsigned long addr,
5749 void *val,
5750 unsigned int bytes,
5751 struct x86_exception *exception)
5752{
5753 return emulator_read_write(ctxt, addr, val, bytes,
5754 exception, &read_emultor);
5755}
5756
52eb5a6d 5757static int emulator_write_emulated(struct x86_emulate_ctxt *ctxt,
22388a3c
XG
5758 unsigned long addr,
5759 const void *val,
5760 unsigned int bytes,
5761 struct x86_exception *exception)
5762{
5763 return emulator_read_write(ctxt, addr, (void *)val, bytes,
5764 exception, &write_emultor);
bbd9b64e 5765}
bbd9b64e 5766
daea3e73
AK
5767#define CMPXCHG_TYPE(t, ptr, old, new) \
5768 (cmpxchg((t *)(ptr), *(t *)(old), *(t *)(new)) == *(t *)(old))
5769
5770#ifdef CONFIG_X86_64
5771# define CMPXCHG64(ptr, old, new) CMPXCHG_TYPE(u64, ptr, old, new)
5772#else
5773# define CMPXCHG64(ptr, old, new) \
9749a6c0 5774 (cmpxchg64((u64 *)(ptr), *(u64 *)(old), *(u64 *)(new)) == *(u64 *)(old))
daea3e73
AK
5775#endif
5776
0f65dd70
AK
5777static int emulator_cmpxchg_emulated(struct x86_emulate_ctxt *ctxt,
5778 unsigned long addr,
bbd9b64e
CO
5779 const void *old,
5780 const void *new,
5781 unsigned int bytes,
0f65dd70 5782 struct x86_exception *exception)
bbd9b64e 5783{
42e35f80 5784 struct kvm_host_map map;
0f65dd70 5785 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
daea3e73 5786 gpa_t gpa;
daea3e73
AK
5787 char *kaddr;
5788 bool exchanged;
2bacc55c 5789
daea3e73
AK
5790 /* guests cmpxchg8b have to be emulated atomically */
5791 if (bytes > 8 || (bytes & (bytes - 1)))
5792 goto emul_write;
10589a46 5793
daea3e73 5794 gpa = kvm_mmu_gva_to_gpa_write(vcpu, addr, NULL);
2bacc55c 5795
daea3e73
AK
5796 if (gpa == UNMAPPED_GVA ||
5797 (gpa & PAGE_MASK) == APIC_DEFAULT_PHYS_BASE)
5798 goto emul_write;
2bacc55c 5799
daea3e73
AK
5800 if (((gpa + bytes - 1) & PAGE_MASK) != (gpa & PAGE_MASK))
5801 goto emul_write;
72dc67a6 5802
42e35f80 5803 if (kvm_vcpu_map(vcpu, gpa_to_gfn(gpa), &map))
c19b8bd6 5804 goto emul_write;
72dc67a6 5805
42e35f80
KA
5806 kaddr = map.hva + offset_in_page(gpa);
5807
daea3e73
AK
5808 switch (bytes) {
5809 case 1:
5810 exchanged = CMPXCHG_TYPE(u8, kaddr, old, new);
5811 break;
5812 case 2:
5813 exchanged = CMPXCHG_TYPE(u16, kaddr, old, new);
5814 break;
5815 case 4:
5816 exchanged = CMPXCHG_TYPE(u32, kaddr, old, new);
5817 break;
5818 case 8:
5819 exchanged = CMPXCHG64(kaddr, old, new);
5820 break;
5821 default:
5822 BUG();
2bacc55c 5823 }
42e35f80
KA
5824
5825 kvm_vcpu_unmap(vcpu, &map, true);
daea3e73
AK
5826
5827 if (!exchanged)
5828 return X86EMUL_CMPXCHG_FAILED;
5829
0eb05bf2 5830 kvm_page_track_write(vcpu, gpa, new, bytes);
8f6abd06
GN
5831
5832 return X86EMUL_CONTINUE;
4a5f48f6 5833
3200f405 5834emul_write:
daea3e73 5835 printk_once(KERN_WARNING "kvm: emulating exchange as write\n");
2bacc55c 5836
0f65dd70 5837 return emulator_write_emulated(ctxt, addr, new, bytes, exception);
bbd9b64e
CO
5838}
5839
cf8f70bf
GN
5840static int kernel_pio(struct kvm_vcpu *vcpu, void *pd)
5841{
cbfc6c91 5842 int r = 0, i;
cf8f70bf 5843
cbfc6c91
WL
5844 for (i = 0; i < vcpu->arch.pio.count; i++) {
5845 if (vcpu->arch.pio.in)
5846 r = kvm_io_bus_read(vcpu, KVM_PIO_BUS, vcpu->arch.pio.port,
5847 vcpu->arch.pio.size, pd);
5848 else
5849 r = kvm_io_bus_write(vcpu, KVM_PIO_BUS,
5850 vcpu->arch.pio.port, vcpu->arch.pio.size,
5851 pd);
5852 if (r)
5853 break;
5854 pd += vcpu->arch.pio.size;
5855 }
cf8f70bf
GN
5856 return r;
5857}
5858
6f6fbe98
XG
5859static int emulator_pio_in_out(struct kvm_vcpu *vcpu, int size,
5860 unsigned short port, void *val,
5861 unsigned int count, bool in)
cf8f70bf 5862{
cf8f70bf 5863 vcpu->arch.pio.port = port;
6f6fbe98 5864 vcpu->arch.pio.in = in;
7972995b 5865 vcpu->arch.pio.count = count;
cf8f70bf
GN
5866 vcpu->arch.pio.size = size;
5867
5868 if (!kernel_pio(vcpu, vcpu->arch.pio_data)) {
7972995b 5869 vcpu->arch.pio.count = 0;
cf8f70bf
GN
5870 return 1;
5871 }
5872
5873 vcpu->run->exit_reason = KVM_EXIT_IO;
6f6fbe98 5874 vcpu->run->io.direction = in ? KVM_EXIT_IO_IN : KVM_EXIT_IO_OUT;
cf8f70bf
GN
5875 vcpu->run->io.size = size;
5876 vcpu->run->io.data_offset = KVM_PIO_PAGE_OFFSET * PAGE_SIZE;
5877 vcpu->run->io.count = count;
5878 vcpu->run->io.port = port;
5879
5880 return 0;
5881}
5882
6f6fbe98
XG
5883static int emulator_pio_in_emulated(struct x86_emulate_ctxt *ctxt,
5884 int size, unsigned short port, void *val,
5885 unsigned int count)
cf8f70bf 5886{
ca1d4a9e 5887 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6f6fbe98 5888 int ret;
ca1d4a9e 5889
6f6fbe98
XG
5890 if (vcpu->arch.pio.count)
5891 goto data_avail;
cf8f70bf 5892
cbfc6c91
WL
5893 memset(vcpu->arch.pio_data, 0, size * count);
5894
6f6fbe98
XG
5895 ret = emulator_pio_in_out(vcpu, size, port, val, count, true);
5896 if (ret) {
5897data_avail:
5898 memcpy(val, vcpu->arch.pio_data, size * count);
1171903d 5899 trace_kvm_pio(KVM_PIO_IN, port, size, count, vcpu->arch.pio_data);
7972995b 5900 vcpu->arch.pio.count = 0;
cf8f70bf
GN
5901 return 1;
5902 }
5903
cf8f70bf
GN
5904 return 0;
5905}
5906
6f6fbe98
XG
5907static int emulator_pio_out_emulated(struct x86_emulate_ctxt *ctxt,
5908 int size, unsigned short port,
5909 const void *val, unsigned int count)
5910{
5911 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
5912
5913 memcpy(vcpu->arch.pio_data, val, size * count);
1171903d 5914 trace_kvm_pio(KVM_PIO_OUT, port, size, count, vcpu->arch.pio_data);
6f6fbe98
XG
5915 return emulator_pio_in_out(vcpu, size, port, (void *)val, count, false);
5916}
5917
bbd9b64e
CO
5918static unsigned long get_segment_base(struct kvm_vcpu *vcpu, int seg)
5919{
5920 return kvm_x86_ops->get_segment_base(vcpu, seg);
5921}
5922
3cb16fe7 5923static void emulator_invlpg(struct x86_emulate_ctxt *ctxt, ulong address)
bbd9b64e 5924{
3cb16fe7 5925 kvm_mmu_invlpg(emul_to_vcpu(ctxt), address);
bbd9b64e
CO
5926}
5927
ae6a2375 5928static int kvm_emulate_wbinvd_noskip(struct kvm_vcpu *vcpu)
f5f48ee1
SY
5929{
5930 if (!need_emulate_wbinvd(vcpu))
5931 return X86EMUL_CONTINUE;
5932
5933 if (kvm_x86_ops->has_wbinvd_exit()) {
2eec7343
JK
5934 int cpu = get_cpu();
5935
5936 cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
f5f48ee1
SY
5937 smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
5938 wbinvd_ipi, NULL, 1);
2eec7343 5939 put_cpu();
f5f48ee1 5940 cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
2eec7343
JK
5941 } else
5942 wbinvd();
f5f48ee1
SY
5943 return X86EMUL_CONTINUE;
5944}
5cb56059
JS
5945
5946int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
5947{
6affcbed
KH
5948 kvm_emulate_wbinvd_noskip(vcpu);
5949 return kvm_skip_emulated_instruction(vcpu);
5cb56059 5950}
f5f48ee1
SY
5951EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
5952
5cb56059
JS
5953
5954
bcaf5cc5
AK
5955static void emulator_wbinvd(struct x86_emulate_ctxt *ctxt)
5956{
5cb56059 5957 kvm_emulate_wbinvd_noskip(emul_to_vcpu(ctxt));
bcaf5cc5
AK
5958}
5959
52eb5a6d
XL
5960static int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
5961 unsigned long *dest)
bbd9b64e 5962{
16f8a6f9 5963 return kvm_get_dr(emul_to_vcpu(ctxt), dr, dest);
bbd9b64e
CO
5964}
5965
52eb5a6d
XL
5966static int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
5967 unsigned long value)
bbd9b64e 5968{
338dbc97 5969
717746e3 5970 return __kvm_set_dr(emul_to_vcpu(ctxt), dr, value);
bbd9b64e
CO
5971}
5972
52a46617 5973static u64 mk_cr_64(u64 curr_cr, u32 new_val)
5fdbf976 5974{
52a46617 5975 return (curr_cr & ~((1ULL << 32) - 1)) | new_val;
5fdbf976
MT
5976}
5977
717746e3 5978static unsigned long emulator_get_cr(struct x86_emulate_ctxt *ctxt, int cr)
bbd9b64e 5979{
717746e3 5980 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
52a46617
GN
5981 unsigned long value;
5982
5983 switch (cr) {
5984 case 0:
5985 value = kvm_read_cr0(vcpu);
5986 break;
5987 case 2:
5988 value = vcpu->arch.cr2;
5989 break;
5990 case 3:
9f8fe504 5991 value = kvm_read_cr3(vcpu);
52a46617
GN
5992 break;
5993 case 4:
5994 value = kvm_read_cr4(vcpu);
5995 break;
5996 case 8:
5997 value = kvm_get_cr8(vcpu);
5998 break;
5999 default:
a737f256 6000 kvm_err("%s: unexpected cr %u\n", __func__, cr);
52a46617
GN
6001 return 0;
6002 }
6003
6004 return value;
6005}
6006
717746e3 6007static int emulator_set_cr(struct x86_emulate_ctxt *ctxt, int cr, ulong val)
52a46617 6008{
717746e3 6009 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
0f12244f
GN
6010 int res = 0;
6011
52a46617
GN
6012 switch (cr) {
6013 case 0:
49a9b07e 6014 res = kvm_set_cr0(vcpu, mk_cr_64(kvm_read_cr0(vcpu), val));
52a46617
GN
6015 break;
6016 case 2:
6017 vcpu->arch.cr2 = val;
6018 break;
6019 case 3:
2390218b 6020 res = kvm_set_cr3(vcpu, val);
52a46617
GN
6021 break;
6022 case 4:
a83b29c6 6023 res = kvm_set_cr4(vcpu, mk_cr_64(kvm_read_cr4(vcpu), val));
52a46617
GN
6024 break;
6025 case 8:
eea1cff9 6026 res = kvm_set_cr8(vcpu, val);
52a46617
GN
6027 break;
6028 default:
a737f256 6029 kvm_err("%s: unexpected cr %u\n", __func__, cr);
0f12244f 6030 res = -1;
52a46617 6031 }
0f12244f
GN
6032
6033 return res;
52a46617
GN
6034}
6035
717746e3 6036static int emulator_get_cpl(struct x86_emulate_ctxt *ctxt)
9c537244 6037{
717746e3 6038 return kvm_x86_ops->get_cpl(emul_to_vcpu(ctxt));
9c537244
GN
6039}
6040
4bff1e86 6041static void emulator_get_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
2dafc6c2 6042{
4bff1e86 6043 kvm_x86_ops->get_gdt(emul_to_vcpu(ctxt), dt);
2dafc6c2
GN
6044}
6045
4bff1e86 6046static void emulator_get_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
160ce1f1 6047{
4bff1e86 6048 kvm_x86_ops->get_idt(emul_to_vcpu(ctxt), dt);
160ce1f1
MG
6049}
6050
1ac9d0cf
AK
6051static void emulator_set_gdt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6052{
6053 kvm_x86_ops->set_gdt(emul_to_vcpu(ctxt), dt);
6054}
6055
6056static void emulator_set_idt(struct x86_emulate_ctxt *ctxt, struct desc_ptr *dt)
6057{
6058 kvm_x86_ops->set_idt(emul_to_vcpu(ctxt), dt);
6059}
6060
4bff1e86
AK
6061static unsigned long emulator_get_cached_segment_base(
6062 struct x86_emulate_ctxt *ctxt, int seg)
5951c442 6063{
4bff1e86 6064 return get_segment_base(emul_to_vcpu(ctxt), seg);
5951c442
GN
6065}
6066
1aa36616
AK
6067static bool emulator_get_segment(struct x86_emulate_ctxt *ctxt, u16 *selector,
6068 struct desc_struct *desc, u32 *base3,
6069 int seg)
2dafc6c2
GN
6070{
6071 struct kvm_segment var;
6072
4bff1e86 6073 kvm_get_segment(emul_to_vcpu(ctxt), &var, seg);
1aa36616 6074 *selector = var.selector;
2dafc6c2 6075
378a8b09
GN
6076 if (var.unusable) {
6077 memset(desc, 0, sizeof(*desc));
f0367ee1
RK
6078 if (base3)
6079 *base3 = 0;
2dafc6c2 6080 return false;
378a8b09 6081 }
2dafc6c2
GN
6082
6083 if (var.g)
6084 var.limit >>= 12;
6085 set_desc_limit(desc, var.limit);
6086 set_desc_base(desc, (unsigned long)var.base);
5601d05b
GN
6087#ifdef CONFIG_X86_64
6088 if (base3)
6089 *base3 = var.base >> 32;
6090#endif
2dafc6c2
GN
6091 desc->type = var.type;
6092 desc->s = var.s;
6093 desc->dpl = var.dpl;
6094 desc->p = var.present;
6095 desc->avl = var.avl;
6096 desc->l = var.l;
6097 desc->d = var.db;
6098 desc->g = var.g;
6099
6100 return true;
6101}
6102
1aa36616
AK
6103static void emulator_set_segment(struct x86_emulate_ctxt *ctxt, u16 selector,
6104 struct desc_struct *desc, u32 base3,
6105 int seg)
2dafc6c2 6106{
4bff1e86 6107 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
2dafc6c2
GN
6108 struct kvm_segment var;
6109
1aa36616 6110 var.selector = selector;
2dafc6c2 6111 var.base = get_desc_base(desc);
5601d05b
GN
6112#ifdef CONFIG_X86_64
6113 var.base |= ((u64)base3) << 32;
6114#endif
2dafc6c2
GN
6115 var.limit = get_desc_limit(desc);
6116 if (desc->g)
6117 var.limit = (var.limit << 12) | 0xfff;
6118 var.type = desc->type;
2dafc6c2
GN
6119 var.dpl = desc->dpl;
6120 var.db = desc->d;
6121 var.s = desc->s;
6122 var.l = desc->l;
6123 var.g = desc->g;
6124 var.avl = desc->avl;
6125 var.present = desc->p;
6126 var.unusable = !var.present;
6127 var.padding = 0;
6128
6129 kvm_set_segment(vcpu, &var, seg);
6130 return;
6131}
6132
717746e3
AK
6133static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
6134 u32 msr_index, u64 *pdata)
6135{
f20935d8 6136 return kvm_get_msr(emul_to_vcpu(ctxt), msr_index, pdata);
717746e3
AK
6137}
6138
6139static int emulator_set_msr(struct x86_emulate_ctxt *ctxt,
6140 u32 msr_index, u64 data)
6141{
f20935d8 6142 return kvm_set_msr(emul_to_vcpu(ctxt), msr_index, data);
717746e3
AK
6143}
6144
64d60670
PB
6145static u64 emulator_get_smbase(struct x86_emulate_ctxt *ctxt)
6146{
6147 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6148
6149 return vcpu->arch.smbase;
6150}
6151
6152static void emulator_set_smbase(struct x86_emulate_ctxt *ctxt, u64 smbase)
6153{
6154 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6155
6156 vcpu->arch.smbase = smbase;
6157}
6158
67f4d428
NA
6159static int emulator_check_pmc(struct x86_emulate_ctxt *ctxt,
6160 u32 pmc)
6161{
98ff80f5 6162 return kvm_pmu_is_valid_rdpmc_ecx(emul_to_vcpu(ctxt), pmc);
67f4d428
NA
6163}
6164
222d21aa
AK
6165static int emulator_read_pmc(struct x86_emulate_ctxt *ctxt,
6166 u32 pmc, u64 *pdata)
6167{
c6702c9d 6168 return kvm_pmu_rdpmc(emul_to_vcpu(ctxt), pmc, pdata);
222d21aa
AK
6169}
6170
6c3287f7
AK
6171static void emulator_halt(struct x86_emulate_ctxt *ctxt)
6172{
6173 emul_to_vcpu(ctxt)->arch.halt_request = 1;
6174}
6175
2953538e 6176static int emulator_intercept(struct x86_emulate_ctxt *ctxt,
8a76d7f2 6177 struct x86_instruction_info *info,
c4f035c6
AK
6178 enum x86_intercept_stage stage)
6179{
2953538e 6180 return kvm_x86_ops->check_intercept(emul_to_vcpu(ctxt), info, stage);
c4f035c6
AK
6181}
6182
e911eb3b
YZ
6183static bool emulator_get_cpuid(struct x86_emulate_ctxt *ctxt,
6184 u32 *eax, u32 *ebx, u32 *ecx, u32 *edx, bool check_limit)
bdb42f5a 6185{
e911eb3b 6186 return kvm_cpuid(emul_to_vcpu(ctxt), eax, ebx, ecx, edx, check_limit);
bdb42f5a
SB
6187}
6188
dd856efa
AK
6189static ulong emulator_read_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg)
6190{
6191 return kvm_register_read(emul_to_vcpu(ctxt), reg);
6192}
6193
6194static void emulator_write_gpr(struct x86_emulate_ctxt *ctxt, unsigned reg, ulong val)
6195{
6196 kvm_register_write(emul_to_vcpu(ctxt), reg, val);
6197}
6198
801806d9
NA
6199static void emulator_set_nmi_mask(struct x86_emulate_ctxt *ctxt, bool masked)
6200{
6201 kvm_x86_ops->set_nmi_mask(emul_to_vcpu(ctxt), masked);
6202}
6203
6ed071f0
LP
6204static unsigned emulator_get_hflags(struct x86_emulate_ctxt *ctxt)
6205{
6206 return emul_to_vcpu(ctxt)->arch.hflags;
6207}
6208
6209static void emulator_set_hflags(struct x86_emulate_ctxt *ctxt, unsigned emul_flags)
6210{
c5833c7a 6211 emul_to_vcpu(ctxt)->arch.hflags = emul_flags;
6ed071f0
LP
6212}
6213
ed19321f
SC
6214static int emulator_pre_leave_smm(struct x86_emulate_ctxt *ctxt,
6215 const char *smstate)
0234bf88 6216{
ed19321f 6217 return kvm_x86_ops->pre_leave_smm(emul_to_vcpu(ctxt), smstate);
0234bf88
LP
6218}
6219
c5833c7a
SC
6220static void emulator_post_leave_smm(struct x86_emulate_ctxt *ctxt)
6221{
6222 kvm_smm_changed(emul_to_vcpu(ctxt));
6223}
6224
02d4160f
VK
6225static int emulator_set_xcr(struct x86_emulate_ctxt *ctxt, u32 index, u64 xcr)
6226{
6227 return __kvm_set_xcr(emul_to_vcpu(ctxt), index, xcr);
6228}
6229
0225fb50 6230static const struct x86_emulate_ops emulate_ops = {
dd856efa
AK
6231 .read_gpr = emulator_read_gpr,
6232 .write_gpr = emulator_write_gpr,
ce14e868
PB
6233 .read_std = emulator_read_std,
6234 .write_std = emulator_write_std,
7a036a6f 6235 .read_phys = kvm_read_guest_phys_system,
1871c602 6236 .fetch = kvm_fetch_guest_virt,
bbd9b64e
CO
6237 .read_emulated = emulator_read_emulated,
6238 .write_emulated = emulator_write_emulated,
6239 .cmpxchg_emulated = emulator_cmpxchg_emulated,
3cb16fe7 6240 .invlpg = emulator_invlpg,
cf8f70bf
GN
6241 .pio_in_emulated = emulator_pio_in_emulated,
6242 .pio_out_emulated = emulator_pio_out_emulated,
1aa36616
AK
6243 .get_segment = emulator_get_segment,
6244 .set_segment = emulator_set_segment,
5951c442 6245 .get_cached_segment_base = emulator_get_cached_segment_base,
2dafc6c2 6246 .get_gdt = emulator_get_gdt,
160ce1f1 6247 .get_idt = emulator_get_idt,
1ac9d0cf
AK
6248 .set_gdt = emulator_set_gdt,
6249 .set_idt = emulator_set_idt,
52a46617
GN
6250 .get_cr = emulator_get_cr,
6251 .set_cr = emulator_set_cr,
9c537244 6252 .cpl = emulator_get_cpl,
35aa5375
GN
6253 .get_dr = emulator_get_dr,
6254 .set_dr = emulator_set_dr,
64d60670
PB
6255 .get_smbase = emulator_get_smbase,
6256 .set_smbase = emulator_set_smbase,
717746e3
AK
6257 .set_msr = emulator_set_msr,
6258 .get_msr = emulator_get_msr,
67f4d428 6259 .check_pmc = emulator_check_pmc,
222d21aa 6260 .read_pmc = emulator_read_pmc,
6c3287f7 6261 .halt = emulator_halt,
bcaf5cc5 6262 .wbinvd = emulator_wbinvd,
d6aa1000 6263 .fix_hypercall = emulator_fix_hypercall,
c4f035c6 6264 .intercept = emulator_intercept,
bdb42f5a 6265 .get_cpuid = emulator_get_cpuid,
801806d9 6266 .set_nmi_mask = emulator_set_nmi_mask,
6ed071f0
LP
6267 .get_hflags = emulator_get_hflags,
6268 .set_hflags = emulator_set_hflags,
0234bf88 6269 .pre_leave_smm = emulator_pre_leave_smm,
c5833c7a 6270 .post_leave_smm = emulator_post_leave_smm,
02d4160f 6271 .set_xcr = emulator_set_xcr,
bbd9b64e
CO
6272};
6273
95cb2295
GN
6274static void toggle_interruptibility(struct kvm_vcpu *vcpu, u32 mask)
6275{
37ccdcbe 6276 u32 int_shadow = kvm_x86_ops->get_interrupt_shadow(vcpu);
95cb2295
GN
6277 /*
6278 * an sti; sti; sequence only disable interrupts for the first
6279 * instruction. So, if the last instruction, be it emulated or
6280 * not, left the system with the INT_STI flag enabled, it
6281 * means that the last instruction is an sti. We should not
6282 * leave the flag on in this case. The same goes for mov ss
6283 */
37ccdcbe
PB
6284 if (int_shadow & mask)
6285 mask = 0;
6addfc42 6286 if (unlikely(int_shadow || mask)) {
95cb2295 6287 kvm_x86_ops->set_interrupt_shadow(vcpu, mask);
6addfc42
PB
6288 if (!mask)
6289 kvm_make_request(KVM_REQ_EVENT, vcpu);
6290 }
95cb2295
GN
6291}
6292
ef54bcfe 6293static bool inject_emulated_exception(struct kvm_vcpu *vcpu)
54b8486f
GN
6294{
6295 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
da9cb575 6296 if (ctxt->exception.vector == PF_VECTOR)
ef54bcfe
PB
6297 return kvm_propagate_fault(vcpu, &ctxt->exception);
6298
6299 if (ctxt->exception.error_code_valid)
da9cb575
AK
6300 kvm_queue_exception_e(vcpu, ctxt->exception.vector,
6301 ctxt->exception.error_code);
54b8486f 6302 else
da9cb575 6303 kvm_queue_exception(vcpu, ctxt->exception.vector);
ef54bcfe 6304 return false;
54b8486f
GN
6305}
6306
8ec4722d
MG
6307static void init_emulate_ctxt(struct kvm_vcpu *vcpu)
6308{
adf52235 6309 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
8ec4722d
MG
6310 int cs_db, cs_l;
6311
8ec4722d
MG
6312 kvm_x86_ops->get_cs_db_l_bits(vcpu, &cs_db, &cs_l);
6313
adf52235 6314 ctxt->eflags = kvm_get_rflags(vcpu);
c8401dda
PB
6315 ctxt->tf = (ctxt->eflags & X86_EFLAGS_TF) != 0;
6316
adf52235
TY
6317 ctxt->eip = kvm_rip_read(vcpu);
6318 ctxt->mode = (!is_protmode(vcpu)) ? X86EMUL_MODE_REAL :
6319 (ctxt->eflags & X86_EFLAGS_VM) ? X86EMUL_MODE_VM86 :
42bf549f 6320 (cs_l && is_long_mode(vcpu)) ? X86EMUL_MODE_PROT64 :
adf52235
TY
6321 cs_db ? X86EMUL_MODE_PROT32 :
6322 X86EMUL_MODE_PROT16;
a584539b 6323 BUILD_BUG_ON(HF_GUEST_MASK != X86EMUL_GUEST_MASK);
64d60670
PB
6324 BUILD_BUG_ON(HF_SMM_MASK != X86EMUL_SMM_MASK);
6325 BUILD_BUG_ON(HF_SMM_INSIDE_NMI_MASK != X86EMUL_SMM_INSIDE_NMI_MASK);
adf52235 6326
dd856efa 6327 init_decode_cache(ctxt);
7ae441ea 6328 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
8ec4722d
MG
6329}
6330
9497e1f2 6331void kvm_inject_realmode_interrupt(struct kvm_vcpu *vcpu, int irq, int inc_eip)
63995653 6332{
9d74191a 6333 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
63995653
MG
6334 int ret;
6335
6336 init_emulate_ctxt(vcpu);
6337
9dac77fa
AK
6338 ctxt->op_bytes = 2;
6339 ctxt->ad_bytes = 2;
6340 ctxt->_eip = ctxt->eip + inc_eip;
9d74191a 6341 ret = emulate_int_real(ctxt, irq);
63995653 6342
9497e1f2
SC
6343 if (ret != X86EMUL_CONTINUE) {
6344 kvm_make_request(KVM_REQ_TRIPLE_FAULT, vcpu);
6345 } else {
6346 ctxt->eip = ctxt->_eip;
6347 kvm_rip_write(vcpu, ctxt->eip);
6348 kvm_set_rflags(vcpu, ctxt->eflags);
6349 }
63995653
MG
6350}
6351EXPORT_SYMBOL_GPL(kvm_inject_realmode_interrupt);
6352
e2366171 6353static int handle_emulation_failure(struct kvm_vcpu *vcpu, int emulation_type)
6d77dbfc 6354{
6d77dbfc
GN
6355 ++vcpu->stat.insn_emulation_fail;
6356 trace_kvm_emulate_insn_failed(vcpu);
e2366171 6357
42cbf068
SC
6358 if (emulation_type & EMULTYPE_VMWARE_GP) {
6359 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
60fc3d02 6360 return 1;
42cbf068 6361 }
e2366171 6362
738fece4
SC
6363 if (emulation_type & EMULTYPE_SKIP) {
6364 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6365 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6366 vcpu->run->internal.ndata = 0;
60fc3d02 6367 return 0;
738fece4
SC
6368 }
6369
22da61c9
SC
6370 kvm_queue_exception(vcpu, UD_VECTOR);
6371
a2b9e6c1 6372 if (!is_guest_mode(vcpu) && kvm_x86_ops->get_cpl(vcpu) == 0) {
fc3a9157
JR
6373 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
6374 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
6375 vcpu->run->internal.ndata = 0;
60fc3d02 6376 return 0;
fc3a9157 6377 }
e2366171 6378
60fc3d02 6379 return 1;
6d77dbfc
GN
6380}
6381
93c05d3e 6382static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t cr2,
991eebf9
GN
6383 bool write_fault_to_shadow_pgtable,
6384 int emulation_type)
a6f177ef 6385{
95b3cf69 6386 gpa_t gpa = cr2;
ba049e93 6387 kvm_pfn_t pfn;
a6f177ef 6388
384bf221 6389 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
991eebf9
GN
6390 return false;
6391
6c3dfeb6
SC
6392 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6393 return false;
6394
44dd3ffa 6395 if (!vcpu->arch.mmu->direct_map) {
95b3cf69
XG
6396 /*
6397 * Write permission should be allowed since only
6398 * write access need to be emulated.
6399 */
6400 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
a6f177ef 6401
95b3cf69
XG
6402 /*
6403 * If the mapping is invalid in guest, let cpu retry
6404 * it to generate fault.
6405 */
6406 if (gpa == UNMAPPED_GVA)
6407 return true;
6408 }
a6f177ef 6409
8e3d9d06
XG
6410 /*
6411 * Do not retry the unhandleable instruction if it faults on the
6412 * readonly host memory, otherwise it will goto a infinite loop:
6413 * retry instruction -> write #PF -> emulation fail -> retry
6414 * instruction -> ...
6415 */
6416 pfn = gfn_to_pfn(vcpu->kvm, gpa_to_gfn(gpa));
95b3cf69
XG
6417
6418 /*
6419 * If the instruction failed on the error pfn, it can not be fixed,
6420 * report the error to userspace.
6421 */
6422 if (is_error_noslot_pfn(pfn))
6423 return false;
6424
6425 kvm_release_pfn_clean(pfn);
6426
6427 /* The instructions are well-emulated on direct mmu. */
44dd3ffa 6428 if (vcpu->arch.mmu->direct_map) {
95b3cf69
XG
6429 unsigned int indirect_shadow_pages;
6430
6431 spin_lock(&vcpu->kvm->mmu_lock);
6432 indirect_shadow_pages = vcpu->kvm->arch.indirect_shadow_pages;
6433 spin_unlock(&vcpu->kvm->mmu_lock);
6434
6435 if (indirect_shadow_pages)
6436 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
6437
a6f177ef 6438 return true;
8e3d9d06 6439 }
a6f177ef 6440
95b3cf69
XG
6441 /*
6442 * if emulation was due to access to shadowed page table
6443 * and it failed try to unshadow page and re-enter the
6444 * guest to let CPU execute the instruction.
6445 */
6446 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
93c05d3e
XG
6447
6448 /*
6449 * If the access faults on its page table, it can not
6450 * be fixed by unprotecting shadow page and it should
6451 * be reported to userspace.
6452 */
6453 return !write_fault_to_shadow_pgtable;
a6f177ef
GN
6454}
6455
1cb3f3ae
XG
6456static bool retry_instruction(struct x86_emulate_ctxt *ctxt,
6457 unsigned long cr2, int emulation_type)
6458{
6459 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
6460 unsigned long last_retry_eip, last_retry_addr, gpa = cr2;
6461
6462 last_retry_eip = vcpu->arch.last_retry_eip;
6463 last_retry_addr = vcpu->arch.last_retry_addr;
6464
6465 /*
6466 * If the emulation is caused by #PF and it is non-page_table
6467 * writing instruction, it means the VM-EXIT is caused by shadow
6468 * page protected, we can zap the shadow page and retry this
6469 * instruction directly.
6470 *
6471 * Note: if the guest uses a non-page-table modifying instruction
6472 * on the PDE that points to the instruction, then we will unmap
6473 * the instruction and go to an infinite loop. So, we cache the
6474 * last retried eip and the last fault address, if we meet the eip
6475 * and the address again, we can break out of the potential infinite
6476 * loop.
6477 */
6478 vcpu->arch.last_retry_eip = vcpu->arch.last_retry_addr = 0;
6479
384bf221 6480 if (!(emulation_type & EMULTYPE_ALLOW_RETRY))
1cb3f3ae
XG
6481 return false;
6482
6c3dfeb6
SC
6483 if (WARN_ON_ONCE(is_guest_mode(vcpu)))
6484 return false;
6485
1cb3f3ae
XG
6486 if (x86_page_table_writing_insn(ctxt))
6487 return false;
6488
6489 if (ctxt->eip == last_retry_eip && last_retry_addr == cr2)
6490 return false;
6491
6492 vcpu->arch.last_retry_eip = ctxt->eip;
6493 vcpu->arch.last_retry_addr = cr2;
6494
44dd3ffa 6495 if (!vcpu->arch.mmu->direct_map)
1cb3f3ae
XG
6496 gpa = kvm_mmu_gva_to_gpa_write(vcpu, cr2, NULL);
6497
22368028 6498 kvm_mmu_unprotect_page(vcpu->kvm, gpa_to_gfn(gpa));
1cb3f3ae
XG
6499
6500 return true;
6501}
6502
716d51ab
GN
6503static int complete_emulated_mmio(struct kvm_vcpu *vcpu);
6504static int complete_emulated_pio(struct kvm_vcpu *vcpu);
6505
64d60670 6506static void kvm_smm_changed(struct kvm_vcpu *vcpu)
a584539b 6507{
64d60670 6508 if (!(vcpu->arch.hflags & HF_SMM_MASK)) {
660a5d51
PB
6509 /* This is a good place to trace that we are exiting SMM. */
6510 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, false);
6511
c43203ca
PB
6512 /* Process a latched INIT or SMI, if any. */
6513 kvm_make_request(KVM_REQ_EVENT, vcpu);
64d60670 6514 }
699023e2
PB
6515
6516 kvm_mmu_reset_context(vcpu);
64d60670
PB
6517}
6518
4a1e10d5
PB
6519static int kvm_vcpu_check_hw_bp(unsigned long addr, u32 type, u32 dr7,
6520 unsigned long *db)
6521{
6522 u32 dr6 = 0;
6523 int i;
6524 u32 enable, rwlen;
6525
6526 enable = dr7;
6527 rwlen = dr7 >> 16;
6528 for (i = 0; i < 4; i++, enable >>= 2, rwlen >>= 4)
6529 if ((enable & 3) && (rwlen & 15) == type && db[i] == addr)
6530 dr6 |= (1 << i);
6531 return dr6;
6532}
6533
120c2c4f 6534static int kvm_vcpu_do_singlestep(struct kvm_vcpu *vcpu)
663f4c61
PB
6535{
6536 struct kvm_run *kvm_run = vcpu->run;
6537
c8401dda
PB
6538 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
6539 kvm_run->debug.arch.dr6 = DR6_BS | DR6_FIXED_1 | DR6_RTM;
6540 kvm_run->debug.arch.pc = vcpu->arch.singlestep_rip;
6541 kvm_run->debug.arch.exception = DB_VECTOR;
6542 kvm_run->exit_reason = KVM_EXIT_DEBUG;
60fc3d02 6543 return 0;
663f4c61 6544 }
120c2c4f 6545 kvm_queue_exception_p(vcpu, DB_VECTOR, DR6_BS);
60fc3d02 6546 return 1;
663f4c61
PB
6547}
6548
6affcbed
KH
6549int kvm_skip_emulated_instruction(struct kvm_vcpu *vcpu)
6550{
6551 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
f8ea7c60 6552 int r;
6affcbed 6553
f8ea7c60 6554 r = kvm_x86_ops->skip_emulated_instruction(vcpu);
60fc3d02 6555 if (unlikely(!r))
f8ea7c60 6556 return 0;
c8401dda
PB
6557
6558 /*
6559 * rflags is the old, "raw" value of the flags. The new value has
6560 * not been saved yet.
6561 *
6562 * This is correct even for TF set by the guest, because "the
6563 * processor will not generate this exception after the instruction
6564 * that sets the TF flag".
6565 */
6566 if (unlikely(rflags & X86_EFLAGS_TF))
120c2c4f 6567 r = kvm_vcpu_do_singlestep(vcpu);
60fc3d02 6568 return r;
6affcbed
KH
6569}
6570EXPORT_SYMBOL_GPL(kvm_skip_emulated_instruction);
6571
4a1e10d5
PB
6572static bool kvm_vcpu_check_breakpoint(struct kvm_vcpu *vcpu, int *r)
6573{
4a1e10d5
PB
6574 if (unlikely(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) &&
6575 (vcpu->arch.guest_debug_dr7 & DR7_BP_EN_MASK)) {
82b32774
NA
6576 struct kvm_run *kvm_run = vcpu->run;
6577 unsigned long eip = kvm_get_linear_rip(vcpu);
6578 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
4a1e10d5
PB
6579 vcpu->arch.guest_debug_dr7,
6580 vcpu->arch.eff_db);
6581
6582 if (dr6 != 0) {
6f43ed01 6583 kvm_run->debug.arch.dr6 = dr6 | DR6_FIXED_1 | DR6_RTM;
82b32774 6584 kvm_run->debug.arch.pc = eip;
4a1e10d5
PB
6585 kvm_run->debug.arch.exception = DB_VECTOR;
6586 kvm_run->exit_reason = KVM_EXIT_DEBUG;
60fc3d02 6587 *r = 0;
4a1e10d5
PB
6588 return true;
6589 }
6590 }
6591
4161a569
NA
6592 if (unlikely(vcpu->arch.dr7 & DR7_BP_EN_MASK) &&
6593 !(kvm_get_rflags(vcpu) & X86_EFLAGS_RF)) {
82b32774
NA
6594 unsigned long eip = kvm_get_linear_rip(vcpu);
6595 u32 dr6 = kvm_vcpu_check_hw_bp(eip, 0,
4a1e10d5
PB
6596 vcpu->arch.dr7,
6597 vcpu->arch.db);
6598
6599 if (dr6 != 0) {
1fc5d194 6600 vcpu->arch.dr6 &= ~DR_TRAP_BITS;
6f43ed01 6601 vcpu->arch.dr6 |= dr6 | DR6_RTM;
4a1e10d5 6602 kvm_queue_exception(vcpu, DB_VECTOR);
60fc3d02 6603 *r = 1;
4a1e10d5
PB
6604 return true;
6605 }
6606 }
6607
6608 return false;
6609}
6610
04789b66
LA
6611static bool is_vmware_backdoor_opcode(struct x86_emulate_ctxt *ctxt)
6612{
2d7921c4
AM
6613 switch (ctxt->opcode_len) {
6614 case 1:
6615 switch (ctxt->b) {
6616 case 0xe4: /* IN */
6617 case 0xe5:
6618 case 0xec:
6619 case 0xed:
6620 case 0xe6: /* OUT */
6621 case 0xe7:
6622 case 0xee:
6623 case 0xef:
6624 case 0x6c: /* INS */
6625 case 0x6d:
6626 case 0x6e: /* OUTS */
6627 case 0x6f:
6628 return true;
6629 }
6630 break;
6631 case 2:
6632 switch (ctxt->b) {
6633 case 0x33: /* RDPMC */
6634 return true;
6635 }
6636 break;
04789b66
LA
6637 }
6638
6639 return false;
6640}
6641
51d8b661
AP
6642int x86_emulate_instruction(struct kvm_vcpu *vcpu,
6643 unsigned long cr2,
dc25e89e
AP
6644 int emulation_type,
6645 void *insn,
6646 int insn_len)
bbd9b64e 6647{
95cb2295 6648 int r;
9d74191a 6649 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
7ae441ea 6650 bool writeback = true;
93c05d3e 6651 bool write_fault_to_spt = vcpu->arch.write_fault_to_shadow_pgtable;
bbd9b64e 6652
c595ceee
PB
6653 vcpu->arch.l1tf_flush_l1d = true;
6654
93c05d3e
XG
6655 /*
6656 * Clear write_fault_to_shadow_pgtable here to ensure it is
6657 * never reused.
6658 */
6659 vcpu->arch.write_fault_to_shadow_pgtable = false;
26eef70c 6660 kvm_clear_exception_queue(vcpu);
8d7d8102 6661
571008da 6662 if (!(emulation_type & EMULTYPE_NO_DECODE)) {
8ec4722d 6663 init_emulate_ctxt(vcpu);
4a1e10d5
PB
6664
6665 /*
6666 * We will reenter on the same instruction since
6667 * we do not set complete_userspace_io. This does not
6668 * handle watchpoints yet, those would be handled in
6669 * the emulate_ops.
6670 */
d391f120
VK
6671 if (!(emulation_type & EMULTYPE_SKIP) &&
6672 kvm_vcpu_check_breakpoint(vcpu, &r))
4a1e10d5
PB
6673 return r;
6674
9d74191a
TY
6675 ctxt->interruptibility = 0;
6676 ctxt->have_exception = false;
e0ad0b47 6677 ctxt->exception.vector = -1;
9d74191a 6678 ctxt->perm_ok = false;
bbd9b64e 6679
b51e974f 6680 ctxt->ud = emulation_type & EMULTYPE_TRAP_UD;
4005996e 6681
9d74191a 6682 r = x86_decode_insn(ctxt, insn, insn_len);
bbd9b64e 6683
e46479f8 6684 trace_kvm_emulate_insn_start(vcpu);
f2b5756b 6685 ++vcpu->stat.insn_emulation;
1d2887e2 6686 if (r != EMULATION_OK) {
b4000606 6687 if ((emulation_type & EMULTYPE_TRAP_UD) ||
c83fad65
SC
6688 (emulation_type & EMULTYPE_TRAP_UD_FORCED)) {
6689 kvm_queue_exception(vcpu, UD_VECTOR);
60fc3d02 6690 return 1;
c83fad65 6691 }
991eebf9
GN
6692 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6693 emulation_type))
60fc3d02 6694 return 1;
8530a79c 6695 if (ctxt->have_exception) {
c8848cee
JD
6696 /*
6697 * #UD should result in just EMULATION_FAILED, and trap-like
6698 * exception should not be encountered during decode.
6699 */
6700 WARN_ON_ONCE(ctxt->exception.vector == UD_VECTOR ||
6701 exception_type(ctxt->exception.vector) == EXCPT_TRAP);
8530a79c 6702 inject_emulated_exception(vcpu);
60fc3d02 6703 return 1;
8530a79c 6704 }
e2366171 6705 return handle_emulation_failure(vcpu, emulation_type);
bbd9b64e
CO
6706 }
6707 }
6708
42cbf068
SC
6709 if ((emulation_type & EMULTYPE_VMWARE_GP) &&
6710 !is_vmware_backdoor_opcode(ctxt)) {
6711 kvm_queue_exception_e(vcpu, GP_VECTOR, 0);
60fc3d02 6712 return 1;
42cbf068 6713 }
04789b66 6714
1957aa63
SC
6715 /*
6716 * Note, EMULTYPE_SKIP is intended for use *only* by vendor callbacks
6717 * for kvm_skip_emulated_instruction(). The caller is responsible for
6718 * updating interruptibility state and injecting single-step #DBs.
6719 */
ba8afb6b 6720 if (emulation_type & EMULTYPE_SKIP) {
9dac77fa 6721 kvm_rip_write(vcpu, ctxt->_eip);
bb663c7a
NA
6722 if (ctxt->eflags & X86_EFLAGS_RF)
6723 kvm_set_rflags(vcpu, ctxt->eflags & ~X86_EFLAGS_RF);
60fc3d02 6724 return 1;
ba8afb6b
GN
6725 }
6726
1cb3f3ae 6727 if (retry_instruction(ctxt, cr2, emulation_type))
60fc3d02 6728 return 1;
1cb3f3ae 6729
7ae441ea 6730 /* this is needed for vmware backdoor interface to work since it
4d2179e1 6731 changes registers values during IO operation */
7ae441ea
GN
6732 if (vcpu->arch.emulate_regs_need_sync_from_vcpu) {
6733 vcpu->arch.emulate_regs_need_sync_from_vcpu = false;
dd856efa 6734 emulator_invalidate_register_cache(ctxt);
7ae441ea 6735 }
4d2179e1 6736
5cd21917 6737restart:
0f89b207
TL
6738 /* Save the faulting GPA (cr2) in the address field */
6739 ctxt->exception.address = cr2;
6740
9d74191a 6741 r = x86_emulate_insn(ctxt);
bbd9b64e 6742
775fde86 6743 if (r == EMULATION_INTERCEPTED)
60fc3d02 6744 return 1;
775fde86 6745
d2ddd1c4 6746 if (r == EMULATION_FAILED) {
991eebf9
GN
6747 if (reexecute_instruction(vcpu, cr2, write_fault_to_spt,
6748 emulation_type))
60fc3d02 6749 return 1;
c3cd7ffa 6750
e2366171 6751 return handle_emulation_failure(vcpu, emulation_type);
bbd9b64e
CO
6752 }
6753
9d74191a 6754 if (ctxt->have_exception) {
60fc3d02 6755 r = 1;
ef54bcfe
PB
6756 if (inject_emulated_exception(vcpu))
6757 return r;
d2ddd1c4 6758 } else if (vcpu->arch.pio.count) {
0912c977
PB
6759 if (!vcpu->arch.pio.in) {
6760 /* FIXME: return into emulator if single-stepping. */
3457e419 6761 vcpu->arch.pio.count = 0;
0912c977 6762 } else {
7ae441ea 6763 writeback = false;
716d51ab
GN
6764 vcpu->arch.complete_userspace_io = complete_emulated_pio;
6765 }
60fc3d02 6766 r = 0;
7ae441ea 6767 } else if (vcpu->mmio_needed) {
bc8a0aaf
SC
6768 ++vcpu->stat.mmio_exits;
6769
7ae441ea
GN
6770 if (!vcpu->mmio_is_write)
6771 writeback = false;
60fc3d02 6772 r = 0;
716d51ab 6773 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
7ae441ea 6774 } else if (r == EMULATION_RESTART)
5cd21917 6775 goto restart;
d2ddd1c4 6776 else
60fc3d02 6777 r = 1;
f850e2e6 6778
7ae441ea 6779 if (writeback) {
6addfc42 6780 unsigned long rflags = kvm_x86_ops->get_rflags(vcpu);
9d74191a 6781 toggle_interruptibility(vcpu, ctxt->interruptibility);
7ae441ea 6782 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
38827dbd 6783 if (!ctxt->have_exception ||
75ee23b3
SC
6784 exception_type(ctxt->exception.vector) == EXCPT_TRAP) {
6785 kvm_rip_write(vcpu, ctxt->eip);
60fc3d02 6786 if (r && ctxt->tf)
120c2c4f 6787 r = kvm_vcpu_do_singlestep(vcpu);
38827dbd 6788 __kvm_set_rflags(vcpu, ctxt->eflags);
75ee23b3 6789 }
6addfc42
PB
6790
6791 /*
6792 * For STI, interrupts are shadowed; so KVM_REQ_EVENT will
6793 * do nothing, and it will be requested again as soon as
6794 * the shadow expires. But we still need to check here,
6795 * because POPF has no interrupt shadow.
6796 */
6797 if (unlikely((ctxt->eflags & ~rflags) & X86_EFLAGS_IF))
6798 kvm_make_request(KVM_REQ_EVENT, vcpu);
7ae441ea
GN
6799 } else
6800 vcpu->arch.emulate_regs_need_sync_to_vcpu = true;
e85d28f8
GN
6801
6802 return r;
de7d789a 6803}
c60658d1
SC
6804
6805int kvm_emulate_instruction(struct kvm_vcpu *vcpu, int emulation_type)
6806{
6807 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
6808}
6809EXPORT_SYMBOL_GPL(kvm_emulate_instruction);
6810
6811int kvm_emulate_instruction_from_buffer(struct kvm_vcpu *vcpu,
6812 void *insn, int insn_len)
6813{
6814 return x86_emulate_instruction(vcpu, 0, 0, insn, insn_len);
6815}
6816EXPORT_SYMBOL_GPL(kvm_emulate_instruction_from_buffer);
de7d789a 6817
8764ed55
SC
6818static int complete_fast_pio_out_port_0x7e(struct kvm_vcpu *vcpu)
6819{
6820 vcpu->arch.pio.count = 0;
6821 return 1;
6822}
6823
45def77e
SC
6824static int complete_fast_pio_out(struct kvm_vcpu *vcpu)
6825{
6826 vcpu->arch.pio.count = 0;
6827
6828 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip)))
6829 return 1;
6830
6831 return kvm_skip_emulated_instruction(vcpu);
6832}
6833
dca7f128
SC
6834static int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size,
6835 unsigned short port)
de7d789a 6836{
de3cd117 6837 unsigned long val = kvm_rax_read(vcpu);
ca1d4a9e
AK
6838 int ret = emulator_pio_out_emulated(&vcpu->arch.emulate_ctxt,
6839 size, port, &val, 1);
8764ed55
SC
6840 if (ret)
6841 return ret;
45def77e 6842
8764ed55
SC
6843 /*
6844 * Workaround userspace that relies on old KVM behavior of %rip being
6845 * incremented prior to exiting to userspace to handle "OUT 0x7e".
6846 */
6847 if (port == 0x7e &&
6848 kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_OUT_7E_INC_RIP)) {
6849 vcpu->arch.complete_userspace_io =
6850 complete_fast_pio_out_port_0x7e;
6851 kvm_skip_emulated_instruction(vcpu);
6852 } else {
45def77e
SC
6853 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
6854 vcpu->arch.complete_userspace_io = complete_fast_pio_out;
6855 }
8764ed55 6856 return 0;
de7d789a 6857}
de7d789a 6858
8370c3d0
TL
6859static int complete_fast_pio_in(struct kvm_vcpu *vcpu)
6860{
6861 unsigned long val;
6862
6863 /* We should only ever be called with arch.pio.count equal to 1 */
6864 BUG_ON(vcpu->arch.pio.count != 1);
6865
45def77e
SC
6866 if (unlikely(!kvm_is_linear_rip(vcpu, vcpu->arch.pio.linear_rip))) {
6867 vcpu->arch.pio.count = 0;
6868 return 1;
6869 }
6870
8370c3d0 6871 /* For size less than 4 we merge, else we zero extend */
de3cd117 6872 val = (vcpu->arch.pio.size < 4) ? kvm_rax_read(vcpu) : 0;
8370c3d0
TL
6873
6874 /*
6875 * Since vcpu->arch.pio.count == 1 let emulator_pio_in_emulated perform
6876 * the copy and tracing
6877 */
6878 emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, vcpu->arch.pio.size,
6879 vcpu->arch.pio.port, &val, 1);
de3cd117 6880 kvm_rax_write(vcpu, val);
8370c3d0 6881
45def77e 6882 return kvm_skip_emulated_instruction(vcpu);
8370c3d0
TL
6883}
6884
dca7f128
SC
6885static int kvm_fast_pio_in(struct kvm_vcpu *vcpu, int size,
6886 unsigned short port)
8370c3d0
TL
6887{
6888 unsigned long val;
6889 int ret;
6890
6891 /* For size less than 4 we merge, else we zero extend */
de3cd117 6892 val = (size < 4) ? kvm_rax_read(vcpu) : 0;
8370c3d0
TL
6893
6894 ret = emulator_pio_in_emulated(&vcpu->arch.emulate_ctxt, size, port,
6895 &val, 1);
6896 if (ret) {
de3cd117 6897 kvm_rax_write(vcpu, val);
8370c3d0
TL
6898 return ret;
6899 }
6900
45def77e 6901 vcpu->arch.pio.linear_rip = kvm_get_linear_rip(vcpu);
8370c3d0
TL
6902 vcpu->arch.complete_userspace_io = complete_fast_pio_in;
6903
6904 return 0;
6905}
dca7f128
SC
6906
6907int kvm_fast_pio(struct kvm_vcpu *vcpu, int size, unsigned short port, int in)
6908{
45def77e 6909 int ret;
dca7f128 6910
dca7f128 6911 if (in)
45def77e 6912 ret = kvm_fast_pio_in(vcpu, size, port);
dca7f128 6913 else
45def77e
SC
6914 ret = kvm_fast_pio_out(vcpu, size, port);
6915 return ret && kvm_skip_emulated_instruction(vcpu);
dca7f128
SC
6916}
6917EXPORT_SYMBOL_GPL(kvm_fast_pio);
8370c3d0 6918
251a5fd6 6919static int kvmclock_cpu_down_prep(unsigned int cpu)
8cfdc000 6920{
0a3aee0d 6921 __this_cpu_write(cpu_tsc_khz, 0);
251a5fd6 6922 return 0;
8cfdc000
ZA
6923}
6924
6925static void tsc_khz_changed(void *data)
c8076604 6926{
8cfdc000
ZA
6927 struct cpufreq_freqs *freq = data;
6928 unsigned long khz = 0;
6929
6930 if (data)
6931 khz = freq->new;
6932 else if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
6933 khz = cpufreq_quick_get(raw_smp_processor_id());
6934 if (!khz)
6935 khz = tsc_khz;
0a3aee0d 6936 __this_cpu_write(cpu_tsc_khz, khz);
c8076604
GH
6937}
6938
5fa4ec9c 6939#ifdef CONFIG_X86_64
0092e434
VK
6940static void kvm_hyperv_tsc_notifier(void)
6941{
0092e434
VK
6942 struct kvm *kvm;
6943 struct kvm_vcpu *vcpu;
6944 int cpu;
6945
0d9ce162 6946 mutex_lock(&kvm_lock);
0092e434
VK
6947 list_for_each_entry(kvm, &vm_list, vm_list)
6948 kvm_make_mclock_inprogress_request(kvm);
6949
6950 hyperv_stop_tsc_emulation();
6951
6952 /* TSC frequency always matches when on Hyper-V */
6953 for_each_present_cpu(cpu)
6954 per_cpu(cpu_tsc_khz, cpu) = tsc_khz;
6955 kvm_max_guest_tsc_khz = tsc_khz;
6956
6957 list_for_each_entry(kvm, &vm_list, vm_list) {
6958 struct kvm_arch *ka = &kvm->arch;
6959
6960 spin_lock(&ka->pvclock_gtod_sync_lock);
6961
6962 pvclock_update_vm_gtod_copy(kvm);
6963
6964 kvm_for_each_vcpu(cpu, vcpu, kvm)
6965 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
6966
6967 kvm_for_each_vcpu(cpu, vcpu, kvm)
6968 kvm_clear_request(KVM_REQ_MCLOCK_INPROGRESS, vcpu);
6969
6970 spin_unlock(&ka->pvclock_gtod_sync_lock);
6971 }
0d9ce162 6972 mutex_unlock(&kvm_lock);
0092e434 6973}
5fa4ec9c 6974#endif
0092e434 6975
df24014a 6976static void __kvmclock_cpufreq_notifier(struct cpufreq_freqs *freq, int cpu)
c8076604 6977{
c8076604
GH
6978 struct kvm *kvm;
6979 struct kvm_vcpu *vcpu;
6980 int i, send_ipi = 0;
6981
8cfdc000
ZA
6982 /*
6983 * We allow guests to temporarily run on slowing clocks,
6984 * provided we notify them after, or to run on accelerating
6985 * clocks, provided we notify them before. Thus time never
6986 * goes backwards.
6987 *
6988 * However, we have a problem. We can't atomically update
6989 * the frequency of a given CPU from this function; it is
6990 * merely a notifier, which can be called from any CPU.
6991 * Changing the TSC frequency at arbitrary points in time
6992 * requires a recomputation of local variables related to
6993 * the TSC for each VCPU. We must flag these local variables
6994 * to be updated and be sure the update takes place with the
6995 * new frequency before any guests proceed.
6996 *
6997 * Unfortunately, the combination of hotplug CPU and frequency
6998 * change creates an intractable locking scenario; the order
6999 * of when these callouts happen is undefined with respect to
7000 * CPU hotplug, and they can race with each other. As such,
7001 * merely setting per_cpu(cpu_tsc_khz) = X during a hotadd is
7002 * undefined; you can actually have a CPU frequency change take
7003 * place in between the computation of X and the setting of the
7004 * variable. To protect against this problem, all updates of
7005 * the per_cpu tsc_khz variable are done in an interrupt
7006 * protected IPI, and all callers wishing to update the value
7007 * must wait for a synchronous IPI to complete (which is trivial
7008 * if the caller is on the CPU already). This establishes the
7009 * necessary total order on variable updates.
7010 *
7011 * Note that because a guest time update may take place
7012 * anytime after the setting of the VCPU's request bit, the
7013 * correct TSC value must be set before the request. However,
7014 * to ensure the update actually makes it to any guest which
7015 * starts running in hardware virtualization between the set
7016 * and the acquisition of the spinlock, we must also ping the
7017 * CPU after setting the request bit.
7018 *
7019 */
7020
df24014a 7021 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
c8076604 7022
0d9ce162 7023 mutex_lock(&kvm_lock);
c8076604 7024 list_for_each_entry(kvm, &vm_list, vm_list) {
988a2cae 7025 kvm_for_each_vcpu(i, vcpu, kvm) {
df24014a 7026 if (vcpu->cpu != cpu)
c8076604 7027 continue;
c285545f 7028 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
0d9ce162 7029 if (vcpu->cpu != raw_smp_processor_id())
8cfdc000 7030 send_ipi = 1;
c8076604
GH
7031 }
7032 }
0d9ce162 7033 mutex_unlock(&kvm_lock);
c8076604
GH
7034
7035 if (freq->old < freq->new && send_ipi) {
7036 /*
7037 * We upscale the frequency. Must make the guest
7038 * doesn't see old kvmclock values while running with
7039 * the new frequency, otherwise we risk the guest sees
7040 * time go backwards.
7041 *
7042 * In case we update the frequency for another cpu
7043 * (which might be in guest context) send an interrupt
7044 * to kick the cpu out of guest context. Next time
7045 * guest context is entered kvmclock will be updated,
7046 * so the guest will not see stale values.
7047 */
df24014a 7048 smp_call_function_single(cpu, tsc_khz_changed, freq, 1);
c8076604 7049 }
df24014a
VK
7050}
7051
7052static int kvmclock_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
7053 void *data)
7054{
7055 struct cpufreq_freqs *freq = data;
7056 int cpu;
7057
7058 if (val == CPUFREQ_PRECHANGE && freq->old > freq->new)
7059 return 0;
7060 if (val == CPUFREQ_POSTCHANGE && freq->old < freq->new)
7061 return 0;
7062
7063 for_each_cpu(cpu, freq->policy->cpus)
7064 __kvmclock_cpufreq_notifier(freq, cpu);
7065
c8076604
GH
7066 return 0;
7067}
7068
7069static struct notifier_block kvmclock_cpufreq_notifier_block = {
8cfdc000
ZA
7070 .notifier_call = kvmclock_cpufreq_notifier
7071};
7072
251a5fd6 7073static int kvmclock_cpu_online(unsigned int cpu)
8cfdc000 7074{
251a5fd6
SAS
7075 tsc_khz_changed(NULL);
7076 return 0;
8cfdc000
ZA
7077}
7078
b820cc0c
ZA
7079static void kvm_timer_init(void)
7080{
c285545f 7081 max_tsc_khz = tsc_khz;
460dd42e 7082
b820cc0c 7083 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) {
c285545f
ZA
7084#ifdef CONFIG_CPU_FREQ
7085 struct cpufreq_policy policy;
758f588d
BP
7086 int cpu;
7087
c285545f 7088 memset(&policy, 0, sizeof(policy));
3e26f230
AK
7089 cpu = get_cpu();
7090 cpufreq_get_policy(&policy, cpu);
c285545f
ZA
7091 if (policy.cpuinfo.max_freq)
7092 max_tsc_khz = policy.cpuinfo.max_freq;
3e26f230 7093 put_cpu();
c285545f 7094#endif
b820cc0c
ZA
7095 cpufreq_register_notifier(&kvmclock_cpufreq_notifier_block,
7096 CPUFREQ_TRANSITION_NOTIFIER);
7097 }
460dd42e 7098
73c1b41e 7099 cpuhp_setup_state(CPUHP_AP_X86_KVM_CLK_ONLINE, "x86/kvm/clk:online",
251a5fd6 7100 kvmclock_cpu_online, kvmclock_cpu_down_prep);
b820cc0c
ZA
7101}
7102
dd60d217
AK
7103DEFINE_PER_CPU(struct kvm_vcpu *, current_vcpu);
7104EXPORT_PER_CPU_SYMBOL_GPL(current_vcpu);
ff9d07a0 7105
f5132b01 7106int kvm_is_in_guest(void)
ff9d07a0 7107{
086c9855 7108 return __this_cpu_read(current_vcpu) != NULL;
ff9d07a0
ZY
7109}
7110
7111static int kvm_is_user_mode(void)
7112{
7113 int user_mode = 3;
dcf46b94 7114
086c9855
AS
7115 if (__this_cpu_read(current_vcpu))
7116 user_mode = kvm_x86_ops->get_cpl(__this_cpu_read(current_vcpu));
dcf46b94 7117
ff9d07a0
ZY
7118 return user_mode != 0;
7119}
7120
7121static unsigned long kvm_get_guest_ip(void)
7122{
7123 unsigned long ip = 0;
dcf46b94 7124
086c9855
AS
7125 if (__this_cpu_read(current_vcpu))
7126 ip = kvm_rip_read(__this_cpu_read(current_vcpu));
dcf46b94 7127
ff9d07a0
ZY
7128 return ip;
7129}
7130
8479e04e
LK
7131static void kvm_handle_intel_pt_intr(void)
7132{
7133 struct kvm_vcpu *vcpu = __this_cpu_read(current_vcpu);
7134
7135 kvm_make_request(KVM_REQ_PMI, vcpu);
7136 __set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
7137 (unsigned long *)&vcpu->arch.pmu.global_status);
7138}
7139
ff9d07a0
ZY
7140static struct perf_guest_info_callbacks kvm_guest_cbs = {
7141 .is_in_guest = kvm_is_in_guest,
7142 .is_user_mode = kvm_is_user_mode,
7143 .get_guest_ip = kvm_get_guest_ip,
8479e04e 7144 .handle_intel_pt_intr = kvm_handle_intel_pt_intr,
ff9d07a0
ZY
7145};
7146
16e8d74d
MT
7147#ifdef CONFIG_X86_64
7148static void pvclock_gtod_update_fn(struct work_struct *work)
7149{
d828199e
MT
7150 struct kvm *kvm;
7151
7152 struct kvm_vcpu *vcpu;
7153 int i;
7154
0d9ce162 7155 mutex_lock(&kvm_lock);
d828199e
MT
7156 list_for_each_entry(kvm, &vm_list, vm_list)
7157 kvm_for_each_vcpu(i, vcpu, kvm)
105b21bb 7158 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
d828199e 7159 atomic_set(&kvm_guest_has_master_clock, 0);
0d9ce162 7160 mutex_unlock(&kvm_lock);
16e8d74d
MT
7161}
7162
7163static DECLARE_WORK(pvclock_gtod_work, pvclock_gtod_update_fn);
7164
7165/*
7166 * Notification about pvclock gtod data update.
7167 */
7168static int pvclock_gtod_notify(struct notifier_block *nb, unsigned long unused,
7169 void *priv)
7170{
7171 struct pvclock_gtod_data *gtod = &pvclock_gtod_data;
7172 struct timekeeper *tk = priv;
7173
7174 update_pvclock_gtod(tk);
7175
7176 /* disable master clock if host does not trust, or does not
b0c39dc6 7177 * use, TSC based clocksource.
16e8d74d 7178 */
b0c39dc6 7179 if (!gtod_is_based_on_tsc(gtod->clock.vclock_mode) &&
16e8d74d
MT
7180 atomic_read(&kvm_guest_has_master_clock) != 0)
7181 queue_work(system_long_wq, &pvclock_gtod_work);
7182
7183 return 0;
7184}
7185
7186static struct notifier_block pvclock_gtod_notifier = {
7187 .notifier_call = pvclock_gtod_notify,
7188};
7189#endif
7190
f8c16bba 7191int kvm_arch_init(void *opaque)
043405e1 7192{
b820cc0c 7193 int r;
6b61edf7 7194 struct kvm_x86_ops *ops = opaque;
f8c16bba 7195
f8c16bba
ZX
7196 if (kvm_x86_ops) {
7197 printk(KERN_ERR "kvm: already loaded the other module\n");
56c6d28a
ZX
7198 r = -EEXIST;
7199 goto out;
f8c16bba
ZX
7200 }
7201
7202 if (!ops->cpu_has_kvm_support()) {
7203 printk(KERN_ERR "kvm: no hardware support\n");
56c6d28a
ZX
7204 r = -EOPNOTSUPP;
7205 goto out;
f8c16bba
ZX
7206 }
7207 if (ops->disabled_by_bios()) {
7208 printk(KERN_ERR "kvm: disabled by bios\n");
56c6d28a
ZX
7209 r = -EOPNOTSUPP;
7210 goto out;
f8c16bba
ZX
7211 }
7212
b666a4b6
MO
7213 /*
7214 * KVM explicitly assumes that the guest has an FPU and
7215 * FXSAVE/FXRSTOR. For example, the KVM_GET_FPU explicitly casts the
7216 * vCPU's FPU state as a fxregs_state struct.
7217 */
7218 if (!boot_cpu_has(X86_FEATURE_FPU) || !boot_cpu_has(X86_FEATURE_FXSR)) {
7219 printk(KERN_ERR "kvm: inadequate fpu\n");
7220 r = -EOPNOTSUPP;
7221 goto out;
7222 }
7223
013f6a5d 7224 r = -ENOMEM;
ed8e4812 7225 x86_fpu_cache = kmem_cache_create("x86_fpu", sizeof(struct fpu),
b666a4b6
MO
7226 __alignof__(struct fpu), SLAB_ACCOUNT,
7227 NULL);
7228 if (!x86_fpu_cache) {
7229 printk(KERN_ERR "kvm: failed to allocate cache for x86 fpu\n");
7230 goto out;
7231 }
7232
013f6a5d
MT
7233 shared_msrs = alloc_percpu(struct kvm_shared_msrs);
7234 if (!shared_msrs) {
7235 printk(KERN_ERR "kvm: failed to allocate percpu kvm_shared_msrs\n");
b666a4b6 7236 goto out_free_x86_fpu_cache;
013f6a5d
MT
7237 }
7238
97db56ce
AK
7239 r = kvm_mmu_module_init();
7240 if (r)
013f6a5d 7241 goto out_free_percpu;
97db56ce 7242
f8c16bba 7243 kvm_x86_ops = ops;
920c8377 7244
7b52345e 7245 kvm_mmu_set_mask_ptes(PT_USER_MASK, PT_ACCESSED_MASK,
ffb128c8 7246 PT_DIRTY_MASK, PT64_NX_MASK, 0,
d0ec49d4 7247 PT_PRESENT_MASK, 0, sme_me_mask);
b820cc0c 7248 kvm_timer_init();
c8076604 7249
ff9d07a0
ZY
7250 perf_register_guest_info_callbacks(&kvm_guest_cbs);
7251
d366bf7e 7252 if (boot_cpu_has(X86_FEATURE_XSAVE))
2acf923e
DC
7253 host_xcr0 = xgetbv(XCR_XFEATURE_ENABLED_MASK);
7254
c5cc421b 7255 kvm_lapic_init();
0c5f81da
WL
7256 if (pi_inject_timer == -1)
7257 pi_inject_timer = housekeeping_enabled(HK_FLAG_TIMER);
16e8d74d
MT
7258#ifdef CONFIG_X86_64
7259 pvclock_gtod_register_notifier(&pvclock_gtod_notifier);
0092e434 7260
5fa4ec9c 7261 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
0092e434 7262 set_hv_tscchange_cb(kvm_hyperv_tsc_notifier);
16e8d74d
MT
7263#endif
7264
f8c16bba 7265 return 0;
56c6d28a 7266
013f6a5d
MT
7267out_free_percpu:
7268 free_percpu(shared_msrs);
b666a4b6
MO
7269out_free_x86_fpu_cache:
7270 kmem_cache_destroy(x86_fpu_cache);
56c6d28a 7271out:
56c6d28a 7272 return r;
043405e1 7273}
8776e519 7274
f8c16bba
ZX
7275void kvm_arch_exit(void)
7276{
0092e434 7277#ifdef CONFIG_X86_64
5fa4ec9c 7278 if (hypervisor_is_type(X86_HYPER_MS_HYPERV))
0092e434
VK
7279 clear_hv_tscchange_cb();
7280#endif
cef84c30 7281 kvm_lapic_exit();
ff9d07a0
ZY
7282 perf_unregister_guest_info_callbacks(&kvm_guest_cbs);
7283
888d256e
JK
7284 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
7285 cpufreq_unregister_notifier(&kvmclock_cpufreq_notifier_block,
7286 CPUFREQ_TRANSITION_NOTIFIER);
251a5fd6 7287 cpuhp_remove_state_nocalls(CPUHP_AP_X86_KVM_CLK_ONLINE);
16e8d74d
MT
7288#ifdef CONFIG_X86_64
7289 pvclock_gtod_unregister_notifier(&pvclock_gtod_notifier);
7290#endif
f8c16bba 7291 kvm_x86_ops = NULL;
56c6d28a 7292 kvm_mmu_module_exit();
013f6a5d 7293 free_percpu(shared_msrs);
b666a4b6 7294 kmem_cache_destroy(x86_fpu_cache);
56c6d28a 7295}
f8c16bba 7296
5cb56059 7297int kvm_vcpu_halt(struct kvm_vcpu *vcpu)
8776e519
HB
7298{
7299 ++vcpu->stat.halt_exits;
35754c98 7300 if (lapic_in_kernel(vcpu)) {
a4535290 7301 vcpu->arch.mp_state = KVM_MP_STATE_HALTED;
8776e519
HB
7302 return 1;
7303 } else {
7304 vcpu->run->exit_reason = KVM_EXIT_HLT;
7305 return 0;
7306 }
7307}
5cb56059
JS
7308EXPORT_SYMBOL_GPL(kvm_vcpu_halt);
7309
7310int kvm_emulate_halt(struct kvm_vcpu *vcpu)
7311{
6affcbed
KH
7312 int ret = kvm_skip_emulated_instruction(vcpu);
7313 /*
7314 * TODO: we might be squashing a GUESTDBG_SINGLESTEP-triggered
7315 * KVM_EXIT_DEBUG here.
7316 */
7317 return kvm_vcpu_halt(vcpu) && ret;
5cb56059 7318}
8776e519
HB
7319EXPORT_SYMBOL_GPL(kvm_emulate_halt);
7320
8ef81a9a 7321#ifdef CONFIG_X86_64
55dd00a7
MT
7322static int kvm_pv_clock_pairing(struct kvm_vcpu *vcpu, gpa_t paddr,
7323 unsigned long clock_type)
7324{
7325 struct kvm_clock_pairing clock_pairing;
899a31f5 7326 struct timespec64 ts;
80fbd89c 7327 u64 cycle;
55dd00a7
MT
7328 int ret;
7329
7330 if (clock_type != KVM_CLOCK_PAIRING_WALLCLOCK)
7331 return -KVM_EOPNOTSUPP;
7332
7333 if (kvm_get_walltime_and_clockread(&ts, &cycle) == false)
7334 return -KVM_EOPNOTSUPP;
7335
7336 clock_pairing.sec = ts.tv_sec;
7337 clock_pairing.nsec = ts.tv_nsec;
7338 clock_pairing.tsc = kvm_read_l1_tsc(vcpu, cycle);
7339 clock_pairing.flags = 0;
bcbfbd8e 7340 memset(&clock_pairing.pad, 0, sizeof(clock_pairing.pad));
55dd00a7
MT
7341
7342 ret = 0;
7343 if (kvm_write_guest(vcpu->kvm, paddr, &clock_pairing,
7344 sizeof(struct kvm_clock_pairing)))
7345 ret = -KVM_EFAULT;
7346
7347 return ret;
7348}
8ef81a9a 7349#endif
55dd00a7 7350
6aef266c
SV
7351/*
7352 * kvm_pv_kick_cpu_op: Kick a vcpu.
7353 *
7354 * @apicid - apicid of vcpu to be kicked.
7355 */
7356static void kvm_pv_kick_cpu_op(struct kvm *kvm, unsigned long flags, int apicid)
7357{
24d2166b 7358 struct kvm_lapic_irq lapic_irq;
6aef266c 7359
24d2166b
R
7360 lapic_irq.shorthand = 0;
7361 lapic_irq.dest_mode = 0;
ebd28fcb 7362 lapic_irq.level = 0;
24d2166b 7363 lapic_irq.dest_id = apicid;
93bbf0b8 7364 lapic_irq.msi_redir_hint = false;
6aef266c 7365
24d2166b 7366 lapic_irq.delivery_mode = APIC_DM_REMRD;
795a149e 7367 kvm_irq_delivery_to_apic(kvm, NULL, &lapic_irq, NULL);
6aef266c
SV
7368}
7369
d62caabb
AS
7370void kvm_vcpu_deactivate_apicv(struct kvm_vcpu *vcpu)
7371{
f7589cca
PB
7372 if (!lapic_in_kernel(vcpu)) {
7373 WARN_ON_ONCE(vcpu->arch.apicv_active);
7374 return;
7375 }
7376 if (!vcpu->arch.apicv_active)
7377 return;
7378
d62caabb
AS
7379 vcpu->arch.apicv_active = false;
7380 kvm_x86_ops->refresh_apicv_exec_ctrl(vcpu);
7381}
7382
71506297
WL
7383static void kvm_sched_yield(struct kvm *kvm, unsigned long dest_id)
7384{
7385 struct kvm_vcpu *target = NULL;
7386 struct kvm_apic_map *map;
7387
7388 rcu_read_lock();
7389 map = rcu_dereference(kvm->arch.apic_map);
7390
7391 if (likely(map) && dest_id <= map->max_apic_id && map->phys_map[dest_id])
7392 target = map->phys_map[dest_id]->vcpu;
7393
7394 rcu_read_unlock();
7395
266e85a5 7396 if (target && READ_ONCE(target->ready))
71506297
WL
7397 kvm_vcpu_yield_to(target);
7398}
7399
8776e519
HB
7400int kvm_emulate_hypercall(struct kvm_vcpu *vcpu)
7401{
7402 unsigned long nr, a0, a1, a2, a3, ret;
6356ee0c 7403 int op_64_bit;
8776e519 7404
696ca779
RK
7405 if (kvm_hv_hypercall_enabled(vcpu->kvm))
7406 return kvm_hv_hypercall(vcpu);
55cd8e5a 7407
de3cd117
SC
7408 nr = kvm_rax_read(vcpu);
7409 a0 = kvm_rbx_read(vcpu);
7410 a1 = kvm_rcx_read(vcpu);
7411 a2 = kvm_rdx_read(vcpu);
7412 a3 = kvm_rsi_read(vcpu);
8776e519 7413
229456fc 7414 trace_kvm_hypercall(nr, a0, a1, a2, a3);
2714d1d3 7415
a449c7aa
NA
7416 op_64_bit = is_64_bit_mode(vcpu);
7417 if (!op_64_bit) {
8776e519
HB
7418 nr &= 0xFFFFFFFF;
7419 a0 &= 0xFFFFFFFF;
7420 a1 &= 0xFFFFFFFF;
7421 a2 &= 0xFFFFFFFF;
7422 a3 &= 0xFFFFFFFF;
7423 }
7424
07708c4a
JK
7425 if (kvm_x86_ops->get_cpl(vcpu) != 0) {
7426 ret = -KVM_EPERM;
696ca779 7427 goto out;
07708c4a
JK
7428 }
7429
8776e519 7430 switch (nr) {
b93463aa
AK
7431 case KVM_HC_VAPIC_POLL_IRQ:
7432 ret = 0;
7433 break;
6aef266c
SV
7434 case KVM_HC_KICK_CPU:
7435 kvm_pv_kick_cpu_op(vcpu->kvm, a0, a1);
266e85a5 7436 kvm_sched_yield(vcpu->kvm, a1);
6aef266c
SV
7437 ret = 0;
7438 break;
8ef81a9a 7439#ifdef CONFIG_X86_64
55dd00a7
MT
7440 case KVM_HC_CLOCK_PAIRING:
7441 ret = kvm_pv_clock_pairing(vcpu, a0, a1);
7442 break;
1ed199a4 7443#endif
4180bf1b
WL
7444 case KVM_HC_SEND_IPI:
7445 ret = kvm_pv_send_ipi(vcpu->kvm, a0, a1, a2, a3, op_64_bit);
7446 break;
71506297
WL
7447 case KVM_HC_SCHED_YIELD:
7448 kvm_sched_yield(vcpu->kvm, a0);
7449 ret = 0;
7450 break;
8776e519
HB
7451 default:
7452 ret = -KVM_ENOSYS;
7453 break;
7454 }
696ca779 7455out:
a449c7aa
NA
7456 if (!op_64_bit)
7457 ret = (u32)ret;
de3cd117 7458 kvm_rax_write(vcpu, ret);
6356ee0c 7459
f11c3a8d 7460 ++vcpu->stat.hypercalls;
6356ee0c 7461 return kvm_skip_emulated_instruction(vcpu);
8776e519
HB
7462}
7463EXPORT_SYMBOL_GPL(kvm_emulate_hypercall);
7464
b6785def 7465static int emulator_fix_hypercall(struct x86_emulate_ctxt *ctxt)
8776e519 7466{
d6aa1000 7467 struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
8776e519 7468 char instruction[3];
5fdbf976 7469 unsigned long rip = kvm_rip_read(vcpu);
8776e519 7470
8776e519 7471 kvm_x86_ops->patch_hypercall(vcpu, instruction);
8776e519 7472
ce2e852e
DV
7473 return emulator_write_emulated(ctxt, rip, instruction, 3,
7474 &ctxt->exception);
8776e519
HB
7475}
7476
851ba692 7477static int dm_request_for_irq_injection(struct kvm_vcpu *vcpu)
b6c7a5dc 7478{
782d422b
MG
7479 return vcpu->run->request_interrupt_window &&
7480 likely(!pic_in_kernel(vcpu->kvm));
b6c7a5dc
HB
7481}
7482
851ba692 7483static void post_kvm_run_save(struct kvm_vcpu *vcpu)
b6c7a5dc 7484{
851ba692
AK
7485 struct kvm_run *kvm_run = vcpu->run;
7486
91586a3b 7487 kvm_run->if_flag = (kvm_get_rflags(vcpu) & X86_EFLAGS_IF) != 0;
f077825a 7488 kvm_run->flags = is_smm(vcpu) ? KVM_RUN_X86_SMM : 0;
2d3ad1f4 7489 kvm_run->cr8 = kvm_get_cr8(vcpu);
b6c7a5dc 7490 kvm_run->apic_base = kvm_get_apic_base(vcpu);
127a457a
MG
7491 kvm_run->ready_for_interrupt_injection =
7492 pic_in_kernel(vcpu->kvm) ||
782d422b 7493 kvm_vcpu_ready_for_interrupt_injection(vcpu);
b6c7a5dc
HB
7494}
7495
95ba8273
GN
7496static void update_cr8_intercept(struct kvm_vcpu *vcpu)
7497{
7498 int max_irr, tpr;
7499
7500 if (!kvm_x86_ops->update_cr8_intercept)
7501 return;
7502
bce87cce 7503 if (!lapic_in_kernel(vcpu))
88c808fd
AK
7504 return;
7505
d62caabb
AS
7506 if (vcpu->arch.apicv_active)
7507 return;
7508
8db3baa2
GN
7509 if (!vcpu->arch.apic->vapic_addr)
7510 max_irr = kvm_lapic_find_highest_irr(vcpu);
7511 else
7512 max_irr = -1;
95ba8273
GN
7513
7514 if (max_irr != -1)
7515 max_irr >>= 4;
7516
7517 tpr = kvm_lapic_get_cr8(vcpu);
7518
7519 kvm_x86_ops->update_cr8_intercept(vcpu, tpr, max_irr);
7520}
7521
b6b8a145 7522static int inject_pending_event(struct kvm_vcpu *vcpu, bool req_int_win)
95ba8273 7523{
b6b8a145
JK
7524 int r;
7525
95ba8273 7526 /* try to reinject previous events if any */
664f8e26 7527
1a680e35
LA
7528 if (vcpu->arch.exception.injected)
7529 kvm_x86_ops->queue_exception(vcpu);
664f8e26 7530 /*
a042c26f
LA
7531 * Do not inject an NMI or interrupt if there is a pending
7532 * exception. Exceptions and interrupts are recognized at
7533 * instruction boundaries, i.e. the start of an instruction.
7534 * Trap-like exceptions, e.g. #DB, have higher priority than
7535 * NMIs and interrupts, i.e. traps are recognized before an
7536 * NMI/interrupt that's pending on the same instruction.
7537 * Fault-like exceptions, e.g. #GP and #PF, are the lowest
7538 * priority, but are only generated (pended) during instruction
7539 * execution, i.e. a pending fault-like exception means the
7540 * fault occurred on the *previous* instruction and must be
7541 * serviced prior to recognizing any new events in order to
7542 * fully complete the previous instruction.
664f8e26 7543 */
1a680e35
LA
7544 else if (!vcpu->arch.exception.pending) {
7545 if (vcpu->arch.nmi_injected)
664f8e26 7546 kvm_x86_ops->set_nmi(vcpu);
1a680e35 7547 else if (vcpu->arch.interrupt.injected)
664f8e26 7548 kvm_x86_ops->set_irq(vcpu);
664f8e26
WL
7549 }
7550
1a680e35
LA
7551 /*
7552 * Call check_nested_events() even if we reinjected a previous event
7553 * in order for caller to determine if it should require immediate-exit
7554 * from L2 to L1 due to pending L1 events which require exit
7555 * from L2 to L1.
7556 */
664f8e26
WL
7557 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7558 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7559 if (r != 0)
7560 return r;
7561 }
7562
7563 /* try to inject new event if pending */
b59bb7bd 7564 if (vcpu->arch.exception.pending) {
5c1c85d0
AK
7565 trace_kvm_inj_exception(vcpu->arch.exception.nr,
7566 vcpu->arch.exception.has_error_code,
7567 vcpu->arch.exception.error_code);
d6e8c854 7568
1a680e35 7569 WARN_ON_ONCE(vcpu->arch.exception.injected);
664f8e26
WL
7570 vcpu->arch.exception.pending = false;
7571 vcpu->arch.exception.injected = true;
7572
d6e8c854
NA
7573 if (exception_type(vcpu->arch.exception.nr) == EXCPT_FAULT)
7574 __kvm_set_rflags(vcpu, kvm_get_rflags(vcpu) |
7575 X86_EFLAGS_RF);
7576
f10c729f
JM
7577 if (vcpu->arch.exception.nr == DB_VECTOR) {
7578 /*
7579 * This code assumes that nSVM doesn't use
7580 * check_nested_events(). If it does, the
7581 * DR6/DR7 changes should happen before L1
7582 * gets a #VMEXIT for an intercepted #DB in
7583 * L2. (Under VMX, on the other hand, the
7584 * DR6/DR7 changes should not happen in the
7585 * event of a VM-exit to L1 for an intercepted
7586 * #DB in L2.)
7587 */
7588 kvm_deliver_exception_payload(vcpu);
7589 if (vcpu->arch.dr7 & DR7_GD) {
7590 vcpu->arch.dr7 &= ~DR7_GD;
7591 kvm_update_dr7(vcpu);
7592 }
6bdf0662
NA
7593 }
7594
cfcd20e5 7595 kvm_x86_ops->queue_exception(vcpu);
1a680e35
LA
7596 }
7597
7598 /* Don't consider new event if we re-injected an event */
7599 if (kvm_event_needs_reinjection(vcpu))
7600 return 0;
7601
7602 if (vcpu->arch.smi_pending && !is_smm(vcpu) &&
7603 kvm_x86_ops->smi_allowed(vcpu)) {
c43203ca 7604 vcpu->arch.smi_pending = false;
52797bf9 7605 ++vcpu->arch.smi_count;
ee2cd4b7 7606 enter_smm(vcpu);
c43203ca 7607 } else if (vcpu->arch.nmi_pending && kvm_x86_ops->nmi_allowed(vcpu)) {
321c5658
YS
7608 --vcpu->arch.nmi_pending;
7609 vcpu->arch.nmi_injected = true;
7610 kvm_x86_ops->set_nmi(vcpu);
c7c9c56c 7611 } else if (kvm_cpu_has_injectable_intr(vcpu)) {
9242b5b6
BD
7612 /*
7613 * Because interrupts can be injected asynchronously, we are
7614 * calling check_nested_events again here to avoid a race condition.
7615 * See https://lkml.org/lkml/2014/7/2/60 for discussion about this
7616 * proposal and current concerns. Perhaps we should be setting
7617 * KVM_REQ_EVENT only on certain events and not unconditionally?
7618 */
7619 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events) {
7620 r = kvm_x86_ops->check_nested_events(vcpu, req_int_win);
7621 if (r != 0)
7622 return r;
7623 }
95ba8273 7624 if (kvm_x86_ops->interrupt_allowed(vcpu)) {
66fd3f7f
GN
7625 kvm_queue_interrupt(vcpu, kvm_cpu_get_interrupt(vcpu),
7626 false);
7627 kvm_x86_ops->set_irq(vcpu);
95ba8273
GN
7628 }
7629 }
ee2cd4b7 7630
b6b8a145 7631 return 0;
95ba8273
GN
7632}
7633
7460fb4a
AK
7634static void process_nmi(struct kvm_vcpu *vcpu)
7635{
7636 unsigned limit = 2;
7637
7638 /*
7639 * x86 is limited to one NMI running, and one NMI pending after it.
7640 * If an NMI is already in progress, limit further NMIs to just one.
7641 * Otherwise, allow two (and we'll inject the first one immediately).
7642 */
7643 if (kvm_x86_ops->get_nmi_mask(vcpu) || vcpu->arch.nmi_injected)
7644 limit = 1;
7645
7646 vcpu->arch.nmi_pending += atomic_xchg(&vcpu->arch.nmi_queued, 0);
7647 vcpu->arch.nmi_pending = min(vcpu->arch.nmi_pending, limit);
7648 kvm_make_request(KVM_REQ_EVENT, vcpu);
7649}
7650
ee2cd4b7 7651static u32 enter_smm_get_segment_flags(struct kvm_segment *seg)
660a5d51
PB
7652{
7653 u32 flags = 0;
7654 flags |= seg->g << 23;
7655 flags |= seg->db << 22;
7656 flags |= seg->l << 21;
7657 flags |= seg->avl << 20;
7658 flags |= seg->present << 15;
7659 flags |= seg->dpl << 13;
7660 flags |= seg->s << 12;
7661 flags |= seg->type << 8;
7662 return flags;
7663}
7664
ee2cd4b7 7665static void enter_smm_save_seg_32(struct kvm_vcpu *vcpu, char *buf, int n)
660a5d51
PB
7666{
7667 struct kvm_segment seg;
7668 int offset;
7669
7670 kvm_get_segment(vcpu, &seg, n);
7671 put_smstate(u32, buf, 0x7fa8 + n * 4, seg.selector);
7672
7673 if (n < 3)
7674 offset = 0x7f84 + n * 12;
7675 else
7676 offset = 0x7f2c + (n - 3) * 12;
7677
7678 put_smstate(u32, buf, offset + 8, seg.base);
7679 put_smstate(u32, buf, offset + 4, seg.limit);
ee2cd4b7 7680 put_smstate(u32, buf, offset, enter_smm_get_segment_flags(&seg));
660a5d51
PB
7681}
7682
efbb288a 7683#ifdef CONFIG_X86_64
ee2cd4b7 7684static void enter_smm_save_seg_64(struct kvm_vcpu *vcpu, char *buf, int n)
660a5d51
PB
7685{
7686 struct kvm_segment seg;
7687 int offset;
7688 u16 flags;
7689
7690 kvm_get_segment(vcpu, &seg, n);
7691 offset = 0x7e00 + n * 16;
7692
ee2cd4b7 7693 flags = enter_smm_get_segment_flags(&seg) >> 8;
660a5d51
PB
7694 put_smstate(u16, buf, offset, seg.selector);
7695 put_smstate(u16, buf, offset + 2, flags);
7696 put_smstate(u32, buf, offset + 4, seg.limit);
7697 put_smstate(u64, buf, offset + 8, seg.base);
7698}
efbb288a 7699#endif
660a5d51 7700
ee2cd4b7 7701static void enter_smm_save_state_32(struct kvm_vcpu *vcpu, char *buf)
660a5d51
PB
7702{
7703 struct desc_ptr dt;
7704 struct kvm_segment seg;
7705 unsigned long val;
7706 int i;
7707
7708 put_smstate(u32, buf, 0x7ffc, kvm_read_cr0(vcpu));
7709 put_smstate(u32, buf, 0x7ff8, kvm_read_cr3(vcpu));
7710 put_smstate(u32, buf, 0x7ff4, kvm_get_rflags(vcpu));
7711 put_smstate(u32, buf, 0x7ff0, kvm_rip_read(vcpu));
7712
7713 for (i = 0; i < 8; i++)
7714 put_smstate(u32, buf, 0x7fd0 + i * 4, kvm_register_read(vcpu, i));
7715
7716 kvm_get_dr(vcpu, 6, &val);
7717 put_smstate(u32, buf, 0x7fcc, (u32)val);
7718 kvm_get_dr(vcpu, 7, &val);
7719 put_smstate(u32, buf, 0x7fc8, (u32)val);
7720
7721 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7722 put_smstate(u32, buf, 0x7fc4, seg.selector);
7723 put_smstate(u32, buf, 0x7f64, seg.base);
7724 put_smstate(u32, buf, 0x7f60, seg.limit);
ee2cd4b7 7725 put_smstate(u32, buf, 0x7f5c, enter_smm_get_segment_flags(&seg));
660a5d51
PB
7726
7727 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7728 put_smstate(u32, buf, 0x7fc0, seg.selector);
7729 put_smstate(u32, buf, 0x7f80, seg.base);
7730 put_smstate(u32, buf, 0x7f7c, seg.limit);
ee2cd4b7 7731 put_smstate(u32, buf, 0x7f78, enter_smm_get_segment_flags(&seg));
660a5d51
PB
7732
7733 kvm_x86_ops->get_gdt(vcpu, &dt);
7734 put_smstate(u32, buf, 0x7f74, dt.address);
7735 put_smstate(u32, buf, 0x7f70, dt.size);
7736
7737 kvm_x86_ops->get_idt(vcpu, &dt);
7738 put_smstate(u32, buf, 0x7f58, dt.address);
7739 put_smstate(u32, buf, 0x7f54, dt.size);
7740
7741 for (i = 0; i < 6; i++)
ee2cd4b7 7742 enter_smm_save_seg_32(vcpu, buf, i);
660a5d51
PB
7743
7744 put_smstate(u32, buf, 0x7f14, kvm_read_cr4(vcpu));
7745
7746 /* revision id */
7747 put_smstate(u32, buf, 0x7efc, 0x00020000);
7748 put_smstate(u32, buf, 0x7ef8, vcpu->arch.smbase);
7749}
7750
b68f3cc7 7751#ifdef CONFIG_X86_64
ee2cd4b7 7752static void enter_smm_save_state_64(struct kvm_vcpu *vcpu, char *buf)
660a5d51 7753{
660a5d51
PB
7754 struct desc_ptr dt;
7755 struct kvm_segment seg;
7756 unsigned long val;
7757 int i;
7758
7759 for (i = 0; i < 16; i++)
7760 put_smstate(u64, buf, 0x7ff8 - i * 8, kvm_register_read(vcpu, i));
7761
7762 put_smstate(u64, buf, 0x7f78, kvm_rip_read(vcpu));
7763 put_smstate(u32, buf, 0x7f70, kvm_get_rflags(vcpu));
7764
7765 kvm_get_dr(vcpu, 6, &val);
7766 put_smstate(u64, buf, 0x7f68, val);
7767 kvm_get_dr(vcpu, 7, &val);
7768 put_smstate(u64, buf, 0x7f60, val);
7769
7770 put_smstate(u64, buf, 0x7f58, kvm_read_cr0(vcpu));
7771 put_smstate(u64, buf, 0x7f50, kvm_read_cr3(vcpu));
7772 put_smstate(u64, buf, 0x7f48, kvm_read_cr4(vcpu));
7773
7774 put_smstate(u32, buf, 0x7f00, vcpu->arch.smbase);
7775
7776 /* revision id */
7777 put_smstate(u32, buf, 0x7efc, 0x00020064);
7778
7779 put_smstate(u64, buf, 0x7ed0, vcpu->arch.efer);
7780
7781 kvm_get_segment(vcpu, &seg, VCPU_SREG_TR);
7782 put_smstate(u16, buf, 0x7e90, seg.selector);
ee2cd4b7 7783 put_smstate(u16, buf, 0x7e92, enter_smm_get_segment_flags(&seg) >> 8);
660a5d51
PB
7784 put_smstate(u32, buf, 0x7e94, seg.limit);
7785 put_smstate(u64, buf, 0x7e98, seg.base);
7786
7787 kvm_x86_ops->get_idt(vcpu, &dt);
7788 put_smstate(u32, buf, 0x7e84, dt.size);
7789 put_smstate(u64, buf, 0x7e88, dt.address);
7790
7791 kvm_get_segment(vcpu, &seg, VCPU_SREG_LDTR);
7792 put_smstate(u16, buf, 0x7e70, seg.selector);
ee2cd4b7 7793 put_smstate(u16, buf, 0x7e72, enter_smm_get_segment_flags(&seg) >> 8);
660a5d51
PB
7794 put_smstate(u32, buf, 0x7e74, seg.limit);
7795 put_smstate(u64, buf, 0x7e78, seg.base);
7796
7797 kvm_x86_ops->get_gdt(vcpu, &dt);
7798 put_smstate(u32, buf, 0x7e64, dt.size);
7799 put_smstate(u64, buf, 0x7e68, dt.address);
7800
7801 for (i = 0; i < 6; i++)
ee2cd4b7 7802 enter_smm_save_seg_64(vcpu, buf, i);
660a5d51 7803}
b68f3cc7 7804#endif
660a5d51 7805
ee2cd4b7 7806static void enter_smm(struct kvm_vcpu *vcpu)
64d60670 7807{
660a5d51 7808 struct kvm_segment cs, ds;
18c3626e 7809 struct desc_ptr dt;
660a5d51
PB
7810 char buf[512];
7811 u32 cr0;
7812
660a5d51 7813 trace_kvm_enter_smm(vcpu->vcpu_id, vcpu->arch.smbase, true);
660a5d51 7814 memset(buf, 0, 512);
b68f3cc7 7815#ifdef CONFIG_X86_64
d6321d49 7816 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
ee2cd4b7 7817 enter_smm_save_state_64(vcpu, buf);
660a5d51 7818 else
b68f3cc7 7819#endif
ee2cd4b7 7820 enter_smm_save_state_32(vcpu, buf);
660a5d51 7821
0234bf88
LP
7822 /*
7823 * Give pre_enter_smm() a chance to make ISA-specific changes to the
7824 * vCPU state (e.g. leave guest mode) after we've saved the state into
7825 * the SMM state-save area.
7826 */
7827 kvm_x86_ops->pre_enter_smm(vcpu, buf);
7828
7829 vcpu->arch.hflags |= HF_SMM_MASK;
54bf36aa 7830 kvm_vcpu_write_guest(vcpu, vcpu->arch.smbase + 0xfe00, buf, sizeof(buf));
660a5d51
PB
7831
7832 if (kvm_x86_ops->get_nmi_mask(vcpu))
7833 vcpu->arch.hflags |= HF_SMM_INSIDE_NMI_MASK;
7834 else
7835 kvm_x86_ops->set_nmi_mask(vcpu, true);
7836
7837 kvm_set_rflags(vcpu, X86_EFLAGS_FIXED);
7838 kvm_rip_write(vcpu, 0x8000);
7839
7840 cr0 = vcpu->arch.cr0 & ~(X86_CR0_PE | X86_CR0_EM | X86_CR0_TS | X86_CR0_PG);
7841 kvm_x86_ops->set_cr0(vcpu, cr0);
7842 vcpu->arch.cr0 = cr0;
7843
7844 kvm_x86_ops->set_cr4(vcpu, 0);
7845
18c3626e
PB
7846 /* Undocumented: IDT limit is set to zero on entry to SMM. */
7847 dt.address = dt.size = 0;
7848 kvm_x86_ops->set_idt(vcpu, &dt);
7849
660a5d51
PB
7850 __kvm_set_dr(vcpu, 7, DR7_FIXED_1);
7851
7852 cs.selector = (vcpu->arch.smbase >> 4) & 0xffff;
7853 cs.base = vcpu->arch.smbase;
7854
7855 ds.selector = 0;
7856 ds.base = 0;
7857
7858 cs.limit = ds.limit = 0xffffffff;
7859 cs.type = ds.type = 0x3;
7860 cs.dpl = ds.dpl = 0;
7861 cs.db = ds.db = 0;
7862 cs.s = ds.s = 1;
7863 cs.l = ds.l = 0;
7864 cs.g = ds.g = 1;
7865 cs.avl = ds.avl = 0;
7866 cs.present = ds.present = 1;
7867 cs.unusable = ds.unusable = 0;
7868 cs.padding = ds.padding = 0;
7869
7870 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
7871 kvm_set_segment(vcpu, &ds, VCPU_SREG_DS);
7872 kvm_set_segment(vcpu, &ds, VCPU_SREG_ES);
7873 kvm_set_segment(vcpu, &ds, VCPU_SREG_FS);
7874 kvm_set_segment(vcpu, &ds, VCPU_SREG_GS);
7875 kvm_set_segment(vcpu, &ds, VCPU_SREG_SS);
7876
b68f3cc7 7877#ifdef CONFIG_X86_64
d6321d49 7878 if (guest_cpuid_has(vcpu, X86_FEATURE_LM))
660a5d51 7879 kvm_x86_ops->set_efer(vcpu, 0);
b68f3cc7 7880#endif
660a5d51
PB
7881
7882 kvm_update_cpuid(vcpu);
7883 kvm_mmu_reset_context(vcpu);
64d60670
PB
7884}
7885
ee2cd4b7 7886static void process_smi(struct kvm_vcpu *vcpu)
c43203ca
PB
7887{
7888 vcpu->arch.smi_pending = true;
7889 kvm_make_request(KVM_REQ_EVENT, vcpu);
7890}
7891
7ee30bc1
NNL
7892void kvm_make_scan_ioapic_request_mask(struct kvm *kvm,
7893 unsigned long *vcpu_bitmap)
7894{
7895 cpumask_var_t cpus;
7ee30bc1
NNL
7896
7897 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
7898
db5a95ec
MW
7899 kvm_make_vcpus_request_mask(kvm, KVM_REQ_SCAN_IOAPIC,
7900 vcpu_bitmap, cpus);
7ee30bc1
NNL
7901
7902 free_cpumask_var(cpus);
7903}
7904
2860c4b1
PB
7905void kvm_make_scan_ioapic_request(struct kvm *kvm)
7906{
7907 kvm_make_all_cpus_request(kvm, KVM_REQ_SCAN_IOAPIC);
7908}
7909
3d81bc7e 7910static void vcpu_scan_ioapic(struct kvm_vcpu *vcpu)
c7c9c56c 7911{
dcbd3e49 7912 if (!kvm_apic_present(vcpu))
3d81bc7e 7913 return;
c7c9c56c 7914
6308630b 7915 bitmap_zero(vcpu->arch.ioapic_handled_vectors, 256);
c7c9c56c 7916
b053b2ae 7917 if (irqchip_split(vcpu->kvm))
6308630b 7918 kvm_scan_ioapic_routes(vcpu, vcpu->arch.ioapic_handled_vectors);
db2bdcbb 7919 else {
fa59cc00 7920 if (vcpu->arch.apicv_active)
d62caabb 7921 kvm_x86_ops->sync_pir_to_irr(vcpu);
e97f852f
WL
7922 if (ioapic_in_kernel(vcpu->kvm))
7923 kvm_ioapic_scan_entry(vcpu, vcpu->arch.ioapic_handled_vectors);
db2bdcbb 7924 }
e40ff1d6
LA
7925
7926 if (is_guest_mode(vcpu))
7927 vcpu->arch.load_eoi_exitmap_pending = true;
7928 else
7929 kvm_make_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu);
7930}
7931
7932static void vcpu_load_eoi_exitmap(struct kvm_vcpu *vcpu)
7933{
7934 u64 eoi_exit_bitmap[4];
7935
7936 if (!kvm_apic_hw_enabled(vcpu->arch.apic))
7937 return;
7938
5c919412
AS
7939 bitmap_or((ulong *)eoi_exit_bitmap, vcpu->arch.ioapic_handled_vectors,
7940 vcpu_to_synic(vcpu)->vec_bitmap, 256);
7941 kvm_x86_ops->load_eoi_exitmap(vcpu, eoi_exit_bitmap);
c7c9c56c
YZ
7942}
7943
93065ac7
MH
7944int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
7945 unsigned long start, unsigned long end,
7946 bool blockable)
b1394e74
RK
7947{
7948 unsigned long apic_address;
7949
7950 /*
7951 * The physical address of apic access page is stored in the VMCS.
7952 * Update it when it becomes invalid.
7953 */
7954 apic_address = gfn_to_hva(kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
7955 if (start <= apic_address && apic_address < end)
7956 kvm_make_all_cpus_request(kvm, KVM_REQ_APIC_PAGE_RELOAD);
93065ac7
MH
7957
7958 return 0;
b1394e74
RK
7959}
7960
4256f43f
TC
7961void kvm_vcpu_reload_apic_access_page(struct kvm_vcpu *vcpu)
7962{
c24ae0dc
TC
7963 struct page *page = NULL;
7964
35754c98 7965 if (!lapic_in_kernel(vcpu))
f439ed27
PB
7966 return;
7967
4256f43f
TC
7968 if (!kvm_x86_ops->set_apic_access_page_addr)
7969 return;
7970
c24ae0dc 7971 page = gfn_to_page(vcpu->kvm, APIC_DEFAULT_PHYS_BASE >> PAGE_SHIFT);
e8fd5e9e
AA
7972 if (is_error_page(page))
7973 return;
c24ae0dc
TC
7974 kvm_x86_ops->set_apic_access_page_addr(vcpu, page_to_phys(page));
7975
7976 /*
7977 * Do not pin apic access page in memory, the MMU notifier
7978 * will call us again if it is migrated or swapped out.
7979 */
7980 put_page(page);
4256f43f 7981}
4256f43f 7982
d264ee0c
SC
7983void __kvm_request_immediate_exit(struct kvm_vcpu *vcpu)
7984{
7985 smp_send_reschedule(vcpu->cpu);
7986}
7987EXPORT_SYMBOL_GPL(__kvm_request_immediate_exit);
7988
9357d939 7989/*
362c698f 7990 * Returns 1 to let vcpu_run() continue the guest execution loop without
9357d939
TY
7991 * exiting to the userspace. Otherwise, the value will be returned to the
7992 * userspace.
7993 */
851ba692 7994static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
b6c7a5dc
HB
7995{
7996 int r;
62a193ed
MG
7997 bool req_int_win =
7998 dm_request_for_irq_injection(vcpu) &&
7999 kvm_cpu_accept_dm_intr(vcpu);
8000
730dca42 8001 bool req_immediate_exit = false;
b6c7a5dc 8002
2fa6e1e1 8003 if (kvm_request_pending(vcpu)) {
671ddc70
JM
8004 if (kvm_check_request(KVM_REQ_GET_VMCS12_PAGES, vcpu)) {
8005 if (unlikely(!kvm_x86_ops->get_vmcs12_pages(vcpu))) {
8006 r = 0;
8007 goto out;
8008 }
8009 }
a8eeb04a 8010 if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
2e53d63a 8011 kvm_mmu_unload(vcpu);
a8eeb04a 8012 if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
2f599714 8013 __kvm_migrate_timers(vcpu);
d828199e
MT
8014 if (kvm_check_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu))
8015 kvm_gen_update_masterclock(vcpu->kvm);
0061d53d
MT
8016 if (kvm_check_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu))
8017 kvm_gen_kvmclock_update(vcpu);
34c238a1
ZA
8018 if (kvm_check_request(KVM_REQ_CLOCK_UPDATE, vcpu)) {
8019 r = kvm_guest_time_update(vcpu);
8cfdc000
ZA
8020 if (unlikely(r))
8021 goto out;
8022 }
a8eeb04a 8023 if (kvm_check_request(KVM_REQ_MMU_SYNC, vcpu))
4731d4c7 8024 kvm_mmu_sync_roots(vcpu);
6e42782f
JS
8025 if (kvm_check_request(KVM_REQ_LOAD_CR3, vcpu))
8026 kvm_mmu_load_cr3(vcpu);
a8eeb04a 8027 if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu))
c2ba05cc 8028 kvm_vcpu_flush_tlb(vcpu, true);
a8eeb04a 8029 if (kvm_check_request(KVM_REQ_REPORT_TPR_ACCESS, vcpu)) {
851ba692 8030 vcpu->run->exit_reason = KVM_EXIT_TPR_ACCESS;
b93463aa
AK
8031 r = 0;
8032 goto out;
8033 }
a8eeb04a 8034 if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
851ba692 8035 vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
bbeac283 8036 vcpu->mmio_needed = 0;
71c4dfaf
JR
8037 r = 0;
8038 goto out;
8039 }
af585b92
GN
8040 if (kvm_check_request(KVM_REQ_APF_HALT, vcpu)) {
8041 /* Page is swapped out. Do synthetic halt */
8042 vcpu->arch.apf.halted = true;
8043 r = 1;
8044 goto out;
8045 }
c9aaa895
GC
8046 if (kvm_check_request(KVM_REQ_STEAL_UPDATE, vcpu))
8047 record_steal_time(vcpu);
64d60670
PB
8048 if (kvm_check_request(KVM_REQ_SMI, vcpu))
8049 process_smi(vcpu);
7460fb4a
AK
8050 if (kvm_check_request(KVM_REQ_NMI, vcpu))
8051 process_nmi(vcpu);
f5132b01 8052 if (kvm_check_request(KVM_REQ_PMU, vcpu))
c6702c9d 8053 kvm_pmu_handle_event(vcpu);
f5132b01 8054 if (kvm_check_request(KVM_REQ_PMI, vcpu))
c6702c9d 8055 kvm_pmu_deliver_pmi(vcpu);
7543a635
SR
8056 if (kvm_check_request(KVM_REQ_IOAPIC_EOI_EXIT, vcpu)) {
8057 BUG_ON(vcpu->arch.pending_ioapic_eoi > 255);
8058 if (test_bit(vcpu->arch.pending_ioapic_eoi,
6308630b 8059 vcpu->arch.ioapic_handled_vectors)) {
7543a635
SR
8060 vcpu->run->exit_reason = KVM_EXIT_IOAPIC_EOI;
8061 vcpu->run->eoi.vector =
8062 vcpu->arch.pending_ioapic_eoi;
8063 r = 0;
8064 goto out;
8065 }
8066 }
3d81bc7e
YZ
8067 if (kvm_check_request(KVM_REQ_SCAN_IOAPIC, vcpu))
8068 vcpu_scan_ioapic(vcpu);
e40ff1d6
LA
8069 if (kvm_check_request(KVM_REQ_LOAD_EOI_EXITMAP, vcpu))
8070 vcpu_load_eoi_exitmap(vcpu);
4256f43f
TC
8071 if (kvm_check_request(KVM_REQ_APIC_PAGE_RELOAD, vcpu))
8072 kvm_vcpu_reload_apic_access_page(vcpu);
2ce79189
AS
8073 if (kvm_check_request(KVM_REQ_HV_CRASH, vcpu)) {
8074 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8075 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_CRASH;
8076 r = 0;
8077 goto out;
8078 }
e516cebb
AS
8079 if (kvm_check_request(KVM_REQ_HV_RESET, vcpu)) {
8080 vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
8081 vcpu->run->system_event.type = KVM_SYSTEM_EVENT_RESET;
8082 r = 0;
8083 goto out;
8084 }
db397571
AS
8085 if (kvm_check_request(KVM_REQ_HV_EXIT, vcpu)) {
8086 vcpu->run->exit_reason = KVM_EXIT_HYPERV;
8087 vcpu->run->hyperv = vcpu->arch.hyperv.exit;
8088 r = 0;
8089 goto out;
8090 }
f3b138c5
AS
8091
8092 /*
8093 * KVM_REQ_HV_STIMER has to be processed after
8094 * KVM_REQ_CLOCK_UPDATE, because Hyper-V SynIC timers
8095 * depend on the guest clock being up-to-date
8096 */
1f4b34f8
AS
8097 if (kvm_check_request(KVM_REQ_HV_STIMER, vcpu))
8098 kvm_hv_process_stimers(vcpu);
2f52d58c 8099 }
b93463aa 8100
b463a6f7 8101 if (kvm_check_request(KVM_REQ_EVENT, vcpu) || req_int_win) {
0f1e261e 8102 ++vcpu->stat.req_event;
66450a21
JK
8103 kvm_apic_accept_events(vcpu);
8104 if (vcpu->arch.mp_state == KVM_MP_STATE_INIT_RECEIVED) {
8105 r = 1;
8106 goto out;
8107 }
8108
b6b8a145
JK
8109 if (inject_pending_event(vcpu, req_int_win) != 0)
8110 req_immediate_exit = true;
321c5658 8111 else {
cc3d967f 8112 /* Enable SMI/NMI/IRQ window open exits if needed.
c43203ca 8113 *
cc3d967f
LP
8114 * SMIs have three cases:
8115 * 1) They can be nested, and then there is nothing to
8116 * do here because RSM will cause a vmexit anyway.
8117 * 2) There is an ISA-specific reason why SMI cannot be
8118 * injected, and the moment when this changes can be
8119 * intercepted.
8120 * 3) Or the SMI can be pending because
8121 * inject_pending_event has completed the injection
8122 * of an IRQ or NMI from the previous vmexit, and
8123 * then we request an immediate exit to inject the
8124 * SMI.
c43203ca
PB
8125 */
8126 if (vcpu->arch.smi_pending && !is_smm(vcpu))
cc3d967f
LP
8127 if (!kvm_x86_ops->enable_smi_window(vcpu))
8128 req_immediate_exit = true;
321c5658
YS
8129 if (vcpu->arch.nmi_pending)
8130 kvm_x86_ops->enable_nmi_window(vcpu);
8131 if (kvm_cpu_has_injectable_intr(vcpu) || req_int_win)
8132 kvm_x86_ops->enable_irq_window(vcpu);
664f8e26 8133 WARN_ON(vcpu->arch.exception.pending);
321c5658 8134 }
b463a6f7
AK
8135
8136 if (kvm_lapic_enabled(vcpu)) {
8137 update_cr8_intercept(vcpu);
8138 kvm_lapic_sync_to_vapic(vcpu);
8139 }
8140 }
8141
d8368af8
AK
8142 r = kvm_mmu_reload(vcpu);
8143 if (unlikely(r)) {
d905c069 8144 goto cancel_injection;
d8368af8
AK
8145 }
8146
b6c7a5dc
HB
8147 preempt_disable();
8148
8149 kvm_x86_ops->prepare_guest_switch(vcpu);
b95234c8
PB
8150
8151 /*
8152 * Disable IRQs before setting IN_GUEST_MODE. Posted interrupt
8153 * IPI are then delayed after guest entry, which ensures that they
8154 * result in virtual interrupt delivery.
8155 */
8156 local_irq_disable();
6b7e2d09
XG
8157 vcpu->mode = IN_GUEST_MODE;
8158
01b71917
MT
8159 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
8160
0f127d12 8161 /*
b95234c8 8162 * 1) We should set ->mode before checking ->requests. Please see
cde9af6e 8163 * the comment in kvm_vcpu_exiting_guest_mode().
b95234c8 8164 *
81b01667 8165 * 2) For APICv, we should set ->mode before checking PID.ON. This
b95234c8
PB
8166 * pairs with the memory barrier implicit in pi_test_and_set_on
8167 * (see vmx_deliver_posted_interrupt).
8168 *
8169 * 3) This also orders the write to mode from any reads to the page
8170 * tables done while the VCPU is running. Please see the comment
8171 * in kvm_flush_remote_tlbs.
6b7e2d09 8172 */
01b71917 8173 smp_mb__after_srcu_read_unlock();
b6c7a5dc 8174
b95234c8
PB
8175 /*
8176 * This handles the case where a posted interrupt was
8177 * notified with kvm_vcpu_kick.
8178 */
fa59cc00
LA
8179 if (kvm_lapic_enabled(vcpu) && vcpu->arch.apicv_active)
8180 kvm_x86_ops->sync_pir_to_irr(vcpu);
32f88400 8181
2fa6e1e1 8182 if (vcpu->mode == EXITING_GUEST_MODE || kvm_request_pending(vcpu)
d94e1dc9 8183 || need_resched() || signal_pending(current)) {
6b7e2d09 8184 vcpu->mode = OUTSIDE_GUEST_MODE;
d94e1dc9 8185 smp_wmb();
6c142801
AK
8186 local_irq_enable();
8187 preempt_enable();
01b71917 8188 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
6c142801 8189 r = 1;
d905c069 8190 goto cancel_injection;
6c142801
AK
8191 }
8192
c43203ca
PB
8193 if (req_immediate_exit) {
8194 kvm_make_request(KVM_REQ_EVENT, vcpu);
d264ee0c 8195 kvm_x86_ops->request_immediate_exit(vcpu);
c43203ca 8196 }
d6185f20 8197
8b89fe1f 8198 trace_kvm_entry(vcpu->vcpu_id);
6edaa530 8199 guest_enter_irqoff();
b6c7a5dc 8200
e7517324
WL
8201 /* The preempt notifier should have taken care of the FPU already. */
8202 WARN_ON_ONCE(test_thread_flag(TIF_NEED_FPU_LOAD));
5f409e20 8203
42dbaa5a 8204 if (unlikely(vcpu->arch.switch_db_regs)) {
42dbaa5a
JK
8205 set_debugreg(0, 7);
8206 set_debugreg(vcpu->arch.eff_db[0], 0);
8207 set_debugreg(vcpu->arch.eff_db[1], 1);
8208 set_debugreg(vcpu->arch.eff_db[2], 2);
8209 set_debugreg(vcpu->arch.eff_db[3], 3);
c77fb5fe 8210 set_debugreg(vcpu->arch.dr6, 6);
ae561ede 8211 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
42dbaa5a 8212 }
b6c7a5dc 8213
851ba692 8214 kvm_x86_ops->run(vcpu);
b6c7a5dc 8215
c77fb5fe
PB
8216 /*
8217 * Do this here before restoring debug registers on the host. And
8218 * since we do this before handling the vmexit, a DR access vmexit
8219 * can (a) read the correct value of the debug registers, (b) set
8220 * KVM_DEBUGREG_WONT_EXIT again.
8221 */
8222 if (unlikely(vcpu->arch.switch_db_regs & KVM_DEBUGREG_WONT_EXIT)) {
c77fb5fe
PB
8223 WARN_ON(vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP);
8224 kvm_x86_ops->sync_dirty_debug_regs(vcpu);
70e4da7a
PB
8225 kvm_update_dr0123(vcpu);
8226 kvm_update_dr6(vcpu);
8227 kvm_update_dr7(vcpu);
8228 vcpu->arch.switch_db_regs &= ~KVM_DEBUGREG_RELOAD;
c77fb5fe
PB
8229 }
8230
24f1e32c
FW
8231 /*
8232 * If the guest has used debug registers, at least dr7
8233 * will be disabled while returning to the host.
8234 * If we don't have active breakpoints in the host, we don't
8235 * care about the messed up debug address registers. But if
8236 * we have some of them active, restore the old state.
8237 */
59d8eb53 8238 if (hw_breakpoint_active())
24f1e32c 8239 hw_breakpoint_restore();
42dbaa5a 8240
4ba76538 8241 vcpu->arch.last_guest_tsc = kvm_read_l1_tsc(vcpu, rdtsc());
1d5f066e 8242
6b7e2d09 8243 vcpu->mode = OUTSIDE_GUEST_MODE;
d94e1dc9 8244 smp_wmb();
a547c6db 8245
95b5a48c 8246 kvm_x86_ops->handle_exit_irqoff(vcpu);
b6c7a5dc 8247
d7a08882
SC
8248 /*
8249 * Consume any pending interrupts, including the possible source of
8250 * VM-Exit on SVM and any ticks that occur between VM-Exit and now.
8251 * An instruction is required after local_irq_enable() to fully unblock
8252 * interrupts on processors that implement an interrupt shadow, the
8253 * stat.exits increment will do nicely.
8254 */
8255 kvm_before_interrupt(vcpu);
8256 local_irq_enable();
b6c7a5dc 8257 ++vcpu->stat.exits;
d7a08882
SC
8258 local_irq_disable();
8259 kvm_after_interrupt(vcpu);
b6c7a5dc 8260
f2485b3e 8261 guest_exit_irqoff();
ec0671d5
WL
8262 if (lapic_in_kernel(vcpu)) {
8263 s64 delta = vcpu->arch.apic->lapic_timer.advance_expire_delta;
8264 if (delta != S64_MIN) {
8265 trace_kvm_wait_lapic_expire(vcpu->vcpu_id, delta);
8266 vcpu->arch.apic->lapic_timer.advance_expire_delta = S64_MIN;
8267 }
8268 }
b6c7a5dc 8269
f2485b3e 8270 local_irq_enable();
b6c7a5dc
HB
8271 preempt_enable();
8272
f656ce01 8273 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
3200f405 8274
b6c7a5dc
HB
8275 /*
8276 * Profile KVM exit RIPs:
8277 */
8278 if (unlikely(prof_on == KVM_PROFILING)) {
5fdbf976
MT
8279 unsigned long rip = kvm_rip_read(vcpu);
8280 profile_hit(KVM_PROFILING, (void *)rip);
b6c7a5dc
HB
8281 }
8282
cc578287
ZA
8283 if (unlikely(vcpu->arch.tsc_always_catchup))
8284 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
298101da 8285
5cfb1d5a
MT
8286 if (vcpu->arch.apic_attention)
8287 kvm_lapic_sync_from_vapic(vcpu);
b93463aa 8288
618232e2 8289 vcpu->arch.gpa_available = false;
851ba692 8290 r = kvm_x86_ops->handle_exit(vcpu);
d905c069
MT
8291 return r;
8292
8293cancel_injection:
8294 kvm_x86_ops->cancel_injection(vcpu);
ae7a2a3f
MT
8295 if (unlikely(vcpu->arch.apic_attention))
8296 kvm_lapic_sync_from_vapic(vcpu);
d7690175
MT
8297out:
8298 return r;
8299}
b6c7a5dc 8300
362c698f
PB
8301static inline int vcpu_block(struct kvm *kvm, struct kvm_vcpu *vcpu)
8302{
bf9f6ac8
FW
8303 if (!kvm_arch_vcpu_runnable(vcpu) &&
8304 (!kvm_x86_ops->pre_block || kvm_x86_ops->pre_block(vcpu) == 0)) {
9c8fd1ba
PB
8305 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
8306 kvm_vcpu_block(vcpu);
8307 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
bf9f6ac8
FW
8308
8309 if (kvm_x86_ops->post_block)
8310 kvm_x86_ops->post_block(vcpu);
8311
9c8fd1ba
PB
8312 if (!kvm_check_request(KVM_REQ_UNHALT, vcpu))
8313 return 1;
8314 }
362c698f
PB
8315
8316 kvm_apic_accept_events(vcpu);
8317 switch(vcpu->arch.mp_state) {
8318 case KVM_MP_STATE_HALTED:
8319 vcpu->arch.pv.pv_unhalted = false;
8320 vcpu->arch.mp_state =
8321 KVM_MP_STATE_RUNNABLE;
b2869f28 8322 /* fall through */
362c698f
PB
8323 case KVM_MP_STATE_RUNNABLE:
8324 vcpu->arch.apf.halted = false;
8325 break;
8326 case KVM_MP_STATE_INIT_RECEIVED:
8327 break;
8328 default:
8329 return -EINTR;
8330 break;
8331 }
8332 return 1;
8333}
09cec754 8334
5d9bc648
PB
8335static inline bool kvm_vcpu_running(struct kvm_vcpu *vcpu)
8336{
0ad3bed6
PB
8337 if (is_guest_mode(vcpu) && kvm_x86_ops->check_nested_events)
8338 kvm_x86_ops->check_nested_events(vcpu, false);
8339
5d9bc648
PB
8340 return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
8341 !vcpu->arch.apf.halted);
8342}
8343
362c698f 8344static int vcpu_run(struct kvm_vcpu *vcpu)
d7690175
MT
8345{
8346 int r;
f656ce01 8347 struct kvm *kvm = vcpu->kvm;
d7690175 8348
f656ce01 8349 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
c595ceee 8350 vcpu->arch.l1tf_flush_l1d = true;
d7690175 8351
362c698f 8352 for (;;) {
58f800d5 8353 if (kvm_vcpu_running(vcpu)) {
851ba692 8354 r = vcpu_enter_guest(vcpu);
bf9f6ac8 8355 } else {
362c698f 8356 r = vcpu_block(kvm, vcpu);
bf9f6ac8
FW
8357 }
8358
09cec754
GN
8359 if (r <= 0)
8360 break;
8361
72875d8a 8362 kvm_clear_request(KVM_REQ_PENDING_TIMER, vcpu);
09cec754
GN
8363 if (kvm_cpu_has_pending_timer(vcpu))
8364 kvm_inject_pending_timer_irqs(vcpu);
8365
782d422b
MG
8366 if (dm_request_for_irq_injection(vcpu) &&
8367 kvm_vcpu_ready_for_interrupt_injection(vcpu)) {
4ca7dd8c
PB
8368 r = 0;
8369 vcpu->run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
09cec754 8370 ++vcpu->stat.request_irq_exits;
362c698f 8371 break;
09cec754 8372 }
af585b92
GN
8373
8374 kvm_check_async_pf_completion(vcpu);
8375
09cec754
GN
8376 if (signal_pending(current)) {
8377 r = -EINTR;
851ba692 8378 vcpu->run->exit_reason = KVM_EXIT_INTR;
09cec754 8379 ++vcpu->stat.signal_exits;
362c698f 8380 break;
09cec754
GN
8381 }
8382 if (need_resched()) {
f656ce01 8383 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
c08ac06a 8384 cond_resched();
f656ce01 8385 vcpu->srcu_idx = srcu_read_lock(&kvm->srcu);
d7690175 8386 }
b6c7a5dc
HB
8387 }
8388
f656ce01 8389 srcu_read_unlock(&kvm->srcu, vcpu->srcu_idx);
b6c7a5dc
HB
8390
8391 return r;
8392}
8393
716d51ab
GN
8394static inline int complete_emulated_io(struct kvm_vcpu *vcpu)
8395{
8396 int r;
60fc3d02 8397
716d51ab 8398 vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
0ce97a2b 8399 r = kvm_emulate_instruction(vcpu, EMULTYPE_NO_DECODE);
716d51ab 8400 srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
60fc3d02 8401 return r;
716d51ab
GN
8402}
8403
8404static int complete_emulated_pio(struct kvm_vcpu *vcpu)
8405{
8406 BUG_ON(!vcpu->arch.pio.count);
8407
8408 return complete_emulated_io(vcpu);
8409}
8410
f78146b0
AK
8411/*
8412 * Implements the following, as a state machine:
8413 *
8414 * read:
8415 * for each fragment
87da7e66
XG
8416 * for each mmio piece in the fragment
8417 * write gpa, len
8418 * exit
8419 * copy data
f78146b0
AK
8420 * execute insn
8421 *
8422 * write:
8423 * for each fragment
87da7e66
XG
8424 * for each mmio piece in the fragment
8425 * write gpa, len
8426 * copy data
8427 * exit
f78146b0 8428 */
716d51ab 8429static int complete_emulated_mmio(struct kvm_vcpu *vcpu)
5287f194
AK
8430{
8431 struct kvm_run *run = vcpu->run;
f78146b0 8432 struct kvm_mmio_fragment *frag;
87da7e66 8433 unsigned len;
5287f194 8434
716d51ab 8435 BUG_ON(!vcpu->mmio_needed);
5287f194 8436
716d51ab 8437 /* Complete previous fragment */
87da7e66
XG
8438 frag = &vcpu->mmio_fragments[vcpu->mmio_cur_fragment];
8439 len = min(8u, frag->len);
716d51ab 8440 if (!vcpu->mmio_is_write)
87da7e66
XG
8441 memcpy(frag->data, run->mmio.data, len);
8442
8443 if (frag->len <= 8) {
8444 /* Switch to the next fragment. */
8445 frag++;
8446 vcpu->mmio_cur_fragment++;
8447 } else {
8448 /* Go forward to the next mmio piece. */
8449 frag->data += len;
8450 frag->gpa += len;
8451 frag->len -= len;
8452 }
8453
a08d3b3b 8454 if (vcpu->mmio_cur_fragment >= vcpu->mmio_nr_fragments) {
716d51ab 8455 vcpu->mmio_needed = 0;
0912c977
PB
8456
8457 /* FIXME: return into emulator if single-stepping. */
cef4dea0 8458 if (vcpu->mmio_is_write)
716d51ab
GN
8459 return 1;
8460 vcpu->mmio_read_completed = 1;
8461 return complete_emulated_io(vcpu);
8462 }
87da7e66 8463
716d51ab
GN
8464 run->exit_reason = KVM_EXIT_MMIO;
8465 run->mmio.phys_addr = frag->gpa;
8466 if (vcpu->mmio_is_write)
87da7e66
XG
8467 memcpy(run->mmio.data, frag->data, min(8u, frag->len));
8468 run->mmio.len = min(8u, frag->len);
716d51ab
GN
8469 run->mmio.is_write = vcpu->mmio_is_write;
8470 vcpu->arch.complete_userspace_io = complete_emulated_mmio;
8471 return 0;
5287f194
AK
8472}
8473
822f312d
SAS
8474/* Swap (qemu) user FPU context for the guest FPU context. */
8475static void kvm_load_guest_fpu(struct kvm_vcpu *vcpu)
8476{
5f409e20
RR
8477 fpregs_lock();
8478
d9a710e5 8479 copy_fpregs_to_fpstate(vcpu->arch.user_fpu);
822f312d 8480 /* PKRU is separately restored in kvm_x86_ops->run. */
b666a4b6 8481 __copy_kernel_to_fpregs(&vcpu->arch.guest_fpu->state,
822f312d 8482 ~XFEATURE_MASK_PKRU);
5f409e20
RR
8483
8484 fpregs_mark_activate();
8485 fpregs_unlock();
8486
822f312d
SAS
8487 trace_kvm_fpu(1);
8488}
8489
8490/* When vcpu_run ends, restore user space FPU context. */
8491static void kvm_put_guest_fpu(struct kvm_vcpu *vcpu)
8492{
5f409e20
RR
8493 fpregs_lock();
8494
b666a4b6 8495 copy_fpregs_to_fpstate(vcpu->arch.guest_fpu);
d9a710e5 8496 copy_kernel_to_fpregs(&vcpu->arch.user_fpu->state);
5f409e20
RR
8497
8498 fpregs_mark_activate();
8499 fpregs_unlock();
8500
822f312d
SAS
8501 ++vcpu->stat.fpu_reload;
8502 trace_kvm_fpu(0);
8503}
8504
b6c7a5dc
HB
8505int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run)
8506{
8507 int r;
b6c7a5dc 8508
accb757d 8509 vcpu_load(vcpu);
20b7035c 8510 kvm_sigset_activate(vcpu);
5663d8f9
PX
8511 kvm_load_guest_fpu(vcpu);
8512
a4535290 8513 if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
2f173d26
JS
8514 if (kvm_run->immediate_exit) {
8515 r = -EINTR;
8516 goto out;
8517 }
b6c7a5dc 8518 kvm_vcpu_block(vcpu);
66450a21 8519 kvm_apic_accept_events(vcpu);
72875d8a 8520 kvm_clear_request(KVM_REQ_UNHALT, vcpu);
ac9f6dc0 8521 r = -EAGAIN;
a0595000
JS
8522 if (signal_pending(current)) {
8523 r = -EINTR;
8524 vcpu->run->exit_reason = KVM_EXIT_INTR;
8525 ++vcpu->stat.signal_exits;
8526 }
ac9f6dc0 8527 goto out;
b6c7a5dc
HB
8528 }
8529
01643c51
KH
8530 if (vcpu->run->kvm_valid_regs & ~KVM_SYNC_X86_VALID_FIELDS) {
8531 r = -EINVAL;
8532 goto out;
8533 }
8534
8535 if (vcpu->run->kvm_dirty_regs) {
8536 r = sync_regs(vcpu);
8537 if (r != 0)
8538 goto out;
8539 }
8540
b6c7a5dc 8541 /* re-sync apic's tpr */
35754c98 8542 if (!lapic_in_kernel(vcpu)) {
eea1cff9
AP
8543 if (kvm_set_cr8(vcpu, kvm_run->cr8) != 0) {
8544 r = -EINVAL;
8545 goto out;
8546 }
8547 }
b6c7a5dc 8548
716d51ab
GN
8549 if (unlikely(vcpu->arch.complete_userspace_io)) {
8550 int (*cui)(struct kvm_vcpu *) = vcpu->arch.complete_userspace_io;
8551 vcpu->arch.complete_userspace_io = NULL;
8552 r = cui(vcpu);
8553 if (r <= 0)
5663d8f9 8554 goto out;
716d51ab
GN
8555 } else
8556 WARN_ON(vcpu->arch.pio.count || vcpu->mmio_needed);
5287f194 8557
460df4c1
PB
8558 if (kvm_run->immediate_exit)
8559 r = -EINTR;
8560 else
8561 r = vcpu_run(vcpu);
b6c7a5dc
HB
8562
8563out:
5663d8f9 8564 kvm_put_guest_fpu(vcpu);
01643c51
KH
8565 if (vcpu->run->kvm_valid_regs)
8566 store_regs(vcpu);
f1d86e46 8567 post_kvm_run_save(vcpu);
20b7035c 8568 kvm_sigset_deactivate(vcpu);
b6c7a5dc 8569
accb757d 8570 vcpu_put(vcpu);
b6c7a5dc
HB
8571 return r;
8572}
8573
01643c51 8574static void __get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
b6c7a5dc 8575{
7ae441ea
GN
8576 if (vcpu->arch.emulate_regs_need_sync_to_vcpu) {
8577 /*
8578 * We are here if userspace calls get_regs() in the middle of
8579 * instruction emulation. Registers state needs to be copied
4a969980 8580 * back from emulation context to vcpu. Userspace shouldn't do
7ae441ea
GN
8581 * that usually, but some bad designed PV devices (vmware
8582 * backdoor interface) need this to work
8583 */
dd856efa 8584 emulator_writeback_register_cache(&vcpu->arch.emulate_ctxt);
7ae441ea
GN
8585 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8586 }
de3cd117
SC
8587 regs->rax = kvm_rax_read(vcpu);
8588 regs->rbx = kvm_rbx_read(vcpu);
8589 regs->rcx = kvm_rcx_read(vcpu);
8590 regs->rdx = kvm_rdx_read(vcpu);
8591 regs->rsi = kvm_rsi_read(vcpu);
8592 regs->rdi = kvm_rdi_read(vcpu);
e9c16c78 8593 regs->rsp = kvm_rsp_read(vcpu);
de3cd117 8594 regs->rbp = kvm_rbp_read(vcpu);
b6c7a5dc 8595#ifdef CONFIG_X86_64
de3cd117
SC
8596 regs->r8 = kvm_r8_read(vcpu);
8597 regs->r9 = kvm_r9_read(vcpu);
8598 regs->r10 = kvm_r10_read(vcpu);
8599 regs->r11 = kvm_r11_read(vcpu);
8600 regs->r12 = kvm_r12_read(vcpu);
8601 regs->r13 = kvm_r13_read(vcpu);
8602 regs->r14 = kvm_r14_read(vcpu);
8603 regs->r15 = kvm_r15_read(vcpu);
b6c7a5dc
HB
8604#endif
8605
5fdbf976 8606 regs->rip = kvm_rip_read(vcpu);
91586a3b 8607 regs->rflags = kvm_get_rflags(vcpu);
01643c51 8608}
b6c7a5dc 8609
01643c51
KH
8610int kvm_arch_vcpu_ioctl_get_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8611{
8612 vcpu_load(vcpu);
8613 __get_regs(vcpu, regs);
1fc9b76b 8614 vcpu_put(vcpu);
b6c7a5dc
HB
8615 return 0;
8616}
8617
01643c51 8618static void __set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
b6c7a5dc 8619{
7ae441ea
GN
8620 vcpu->arch.emulate_regs_need_sync_from_vcpu = true;
8621 vcpu->arch.emulate_regs_need_sync_to_vcpu = false;
8622
de3cd117
SC
8623 kvm_rax_write(vcpu, regs->rax);
8624 kvm_rbx_write(vcpu, regs->rbx);
8625 kvm_rcx_write(vcpu, regs->rcx);
8626 kvm_rdx_write(vcpu, regs->rdx);
8627 kvm_rsi_write(vcpu, regs->rsi);
8628 kvm_rdi_write(vcpu, regs->rdi);
e9c16c78 8629 kvm_rsp_write(vcpu, regs->rsp);
de3cd117 8630 kvm_rbp_write(vcpu, regs->rbp);
b6c7a5dc 8631#ifdef CONFIG_X86_64
de3cd117
SC
8632 kvm_r8_write(vcpu, regs->r8);
8633 kvm_r9_write(vcpu, regs->r9);
8634 kvm_r10_write(vcpu, regs->r10);
8635 kvm_r11_write(vcpu, regs->r11);
8636 kvm_r12_write(vcpu, regs->r12);
8637 kvm_r13_write(vcpu, regs->r13);
8638 kvm_r14_write(vcpu, regs->r14);
8639 kvm_r15_write(vcpu, regs->r15);
b6c7a5dc
HB
8640#endif
8641
5fdbf976 8642 kvm_rip_write(vcpu, regs->rip);
d73235d1 8643 kvm_set_rflags(vcpu, regs->rflags | X86_EFLAGS_FIXED);
b6c7a5dc 8644
b4f14abd
JK
8645 vcpu->arch.exception.pending = false;
8646
3842d135 8647 kvm_make_request(KVM_REQ_EVENT, vcpu);
01643c51 8648}
3842d135 8649
01643c51
KH
8650int kvm_arch_vcpu_ioctl_set_regs(struct kvm_vcpu *vcpu, struct kvm_regs *regs)
8651{
8652 vcpu_load(vcpu);
8653 __set_regs(vcpu, regs);
875656fe 8654 vcpu_put(vcpu);
b6c7a5dc
HB
8655 return 0;
8656}
8657
b6c7a5dc
HB
8658void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l)
8659{
8660 struct kvm_segment cs;
8661
3e6e0aab 8662 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
b6c7a5dc
HB
8663 *db = cs.db;
8664 *l = cs.l;
8665}
8666EXPORT_SYMBOL_GPL(kvm_get_cs_db_l_bits);
8667
01643c51 8668static void __get_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
b6c7a5dc 8669{
89a27f4d 8670 struct desc_ptr dt;
b6c7a5dc 8671
3e6e0aab
GT
8672 kvm_get_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8673 kvm_get_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8674 kvm_get_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8675 kvm_get_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8676 kvm_get_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8677 kvm_get_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
b6c7a5dc 8678
3e6e0aab
GT
8679 kvm_get_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8680 kvm_get_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
b6c7a5dc
HB
8681
8682 kvm_x86_ops->get_idt(vcpu, &dt);
89a27f4d
GN
8683 sregs->idt.limit = dt.size;
8684 sregs->idt.base = dt.address;
b6c7a5dc 8685 kvm_x86_ops->get_gdt(vcpu, &dt);
89a27f4d
GN
8686 sregs->gdt.limit = dt.size;
8687 sregs->gdt.base = dt.address;
b6c7a5dc 8688
4d4ec087 8689 sregs->cr0 = kvm_read_cr0(vcpu);
ad312c7c 8690 sregs->cr2 = vcpu->arch.cr2;
9f8fe504 8691 sregs->cr3 = kvm_read_cr3(vcpu);
fc78f519 8692 sregs->cr4 = kvm_read_cr4(vcpu);
2d3ad1f4 8693 sregs->cr8 = kvm_get_cr8(vcpu);
f6801dff 8694 sregs->efer = vcpu->arch.efer;
b6c7a5dc
HB
8695 sregs->apic_base = kvm_get_apic_base(vcpu);
8696
0e96f31e 8697 memset(sregs->interrupt_bitmap, 0, sizeof(sregs->interrupt_bitmap));
b6c7a5dc 8698
04140b41 8699 if (vcpu->arch.interrupt.injected && !vcpu->arch.interrupt.soft)
14d0bc1f
GN
8700 set_bit(vcpu->arch.interrupt.nr,
8701 (unsigned long *)sregs->interrupt_bitmap);
01643c51 8702}
16d7a191 8703
01643c51
KH
8704int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
8705 struct kvm_sregs *sregs)
8706{
8707 vcpu_load(vcpu);
8708 __get_sregs(vcpu, sregs);
bcdec41c 8709 vcpu_put(vcpu);
b6c7a5dc
HB
8710 return 0;
8711}
8712
62d9f0db
MT
8713int kvm_arch_vcpu_ioctl_get_mpstate(struct kvm_vcpu *vcpu,
8714 struct kvm_mp_state *mp_state)
8715{
fd232561
CD
8716 vcpu_load(vcpu);
8717
66450a21 8718 kvm_apic_accept_events(vcpu);
6aef266c
SV
8719 if (vcpu->arch.mp_state == KVM_MP_STATE_HALTED &&
8720 vcpu->arch.pv.pv_unhalted)
8721 mp_state->mp_state = KVM_MP_STATE_RUNNABLE;
8722 else
8723 mp_state->mp_state = vcpu->arch.mp_state;
8724
fd232561 8725 vcpu_put(vcpu);
62d9f0db
MT
8726 return 0;
8727}
8728
8729int kvm_arch_vcpu_ioctl_set_mpstate(struct kvm_vcpu *vcpu,
8730 struct kvm_mp_state *mp_state)
8731{
e83dff5e
CD
8732 int ret = -EINVAL;
8733
8734 vcpu_load(vcpu);
8735
bce87cce 8736 if (!lapic_in_kernel(vcpu) &&
66450a21 8737 mp_state->mp_state != KVM_MP_STATE_RUNNABLE)
e83dff5e 8738 goto out;
66450a21 8739
27cbe7d6
LA
8740 /*
8741 * KVM_MP_STATE_INIT_RECEIVED means the processor is in
8742 * INIT state; latched init should be reported using
8743 * KVM_SET_VCPU_EVENTS, so reject it here.
8744 */
8745 if ((kvm_vcpu_latch_init(vcpu) || vcpu->arch.smi_pending) &&
28bf2888
DH
8746 (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED ||
8747 mp_state->mp_state == KVM_MP_STATE_INIT_RECEIVED))
e83dff5e 8748 goto out;
28bf2888 8749
66450a21
JK
8750 if (mp_state->mp_state == KVM_MP_STATE_SIPI_RECEIVED) {
8751 vcpu->arch.mp_state = KVM_MP_STATE_INIT_RECEIVED;
8752 set_bit(KVM_APIC_SIPI, &vcpu->arch.apic->pending_events);
8753 } else
8754 vcpu->arch.mp_state = mp_state->mp_state;
3842d135 8755 kvm_make_request(KVM_REQ_EVENT, vcpu);
e83dff5e
CD
8756
8757 ret = 0;
8758out:
8759 vcpu_put(vcpu);
8760 return ret;
62d9f0db
MT
8761}
8762
7f3d35fd
KW
8763int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
8764 int reason, bool has_error_code, u32 error_code)
b6c7a5dc 8765{
9d74191a 8766 struct x86_emulate_ctxt *ctxt = &vcpu->arch.emulate_ctxt;
8ec4722d 8767 int ret;
e01c2426 8768
8ec4722d 8769 init_emulate_ctxt(vcpu);
c697518a 8770
7f3d35fd 8771 ret = emulator_task_switch(ctxt, tss_selector, idt_index, reason,
9d74191a 8772 has_error_code, error_code);
1051778f
SC
8773 if (ret) {
8774 vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
8775 vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
8776 vcpu->run->internal.ndata = 0;
60fc3d02 8777 return 0;
1051778f 8778 }
37817f29 8779
9d74191a
TY
8780 kvm_rip_write(vcpu, ctxt->eip);
8781 kvm_set_rflags(vcpu, ctxt->eflags);
3842d135 8782 kvm_make_request(KVM_REQ_EVENT, vcpu);
60fc3d02 8783 return 1;
37817f29
IE
8784}
8785EXPORT_SYMBOL_GPL(kvm_task_switch);
8786
3140c156 8787static int kvm_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
f2981033 8788{
37b95951 8789 if ((sregs->efer & EFER_LME) && (sregs->cr0 & X86_CR0_PG)) {
f2981033
LT
8790 /*
8791 * When EFER.LME and CR0.PG are set, the processor is in
8792 * 64-bit mode (though maybe in a 32-bit code segment).
8793 * CR4.PAE and EFER.LMA must be set.
8794 */
37b95951 8795 if (!(sregs->cr4 & X86_CR4_PAE)
f2981033
LT
8796 || !(sregs->efer & EFER_LMA))
8797 return -EINVAL;
8798 } else {
8799 /*
8800 * Not in 64-bit mode: EFER.LMA is clear and the code
8801 * segment cannot be 64-bit.
8802 */
8803 if (sregs->efer & EFER_LMA || sregs->cs.l)
8804 return -EINVAL;
8805 }
8806
3ca94192 8807 return kvm_valid_cr4(vcpu, sregs->cr4);
f2981033
LT
8808}
8809
01643c51 8810static int __set_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
b6c7a5dc 8811{
58cb628d 8812 struct msr_data apic_base_msr;
b6c7a5dc 8813 int mmu_reset_needed = 0;
c4d21882 8814 int cpuid_update_needed = 0;
63f42e02 8815 int pending_vec, max_bits, idx;
89a27f4d 8816 struct desc_ptr dt;
b4ef9d4e
CD
8817 int ret = -EINVAL;
8818
f2981033 8819 if (kvm_valid_sregs(vcpu, sregs))
8dbfb2bf 8820 goto out;
f2981033 8821
d3802286
JM
8822 apic_base_msr.data = sregs->apic_base;
8823 apic_base_msr.host_initiated = true;
8824 if (kvm_set_apic_base(vcpu, &apic_base_msr))
b4ef9d4e 8825 goto out;
6d1068b3 8826
89a27f4d
GN
8827 dt.size = sregs->idt.limit;
8828 dt.address = sregs->idt.base;
b6c7a5dc 8829 kvm_x86_ops->set_idt(vcpu, &dt);
89a27f4d
GN
8830 dt.size = sregs->gdt.limit;
8831 dt.address = sregs->gdt.base;
b6c7a5dc
HB
8832 kvm_x86_ops->set_gdt(vcpu, &dt);
8833
ad312c7c 8834 vcpu->arch.cr2 = sregs->cr2;
9f8fe504 8835 mmu_reset_needed |= kvm_read_cr3(vcpu) != sregs->cr3;
dc7e795e 8836 vcpu->arch.cr3 = sregs->cr3;
cb3c1e2f 8837 kvm_register_mark_available(vcpu, VCPU_EXREG_CR3);
b6c7a5dc 8838
2d3ad1f4 8839 kvm_set_cr8(vcpu, sregs->cr8);
b6c7a5dc 8840
f6801dff 8841 mmu_reset_needed |= vcpu->arch.efer != sregs->efer;
b6c7a5dc 8842 kvm_x86_ops->set_efer(vcpu, sregs->efer);
b6c7a5dc 8843
4d4ec087 8844 mmu_reset_needed |= kvm_read_cr0(vcpu) != sregs->cr0;
b6c7a5dc 8845 kvm_x86_ops->set_cr0(vcpu, sregs->cr0);
d7306163 8846 vcpu->arch.cr0 = sregs->cr0;
b6c7a5dc 8847
fc78f519 8848 mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
c4d21882
WH
8849 cpuid_update_needed |= ((kvm_read_cr4(vcpu) ^ sregs->cr4) &
8850 (X86_CR4_OSXSAVE | X86_CR4_PKE));
b6c7a5dc 8851 kvm_x86_ops->set_cr4(vcpu, sregs->cr4);
c4d21882 8852 if (cpuid_update_needed)
00b27a3e 8853 kvm_update_cpuid(vcpu);
63f42e02
XG
8854
8855 idx = srcu_read_lock(&vcpu->kvm->srcu);
bf03d4f9 8856 if (is_pae_paging(vcpu)) {
9f8fe504 8857 load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu));
7c93be44
MT
8858 mmu_reset_needed = 1;
8859 }
63f42e02 8860 srcu_read_unlock(&vcpu->kvm->srcu, idx);
b6c7a5dc
HB
8861
8862 if (mmu_reset_needed)
8863 kvm_mmu_reset_context(vcpu);
8864
a50abc3b 8865 max_bits = KVM_NR_INTERRUPTS;
923c61bb
GN
8866 pending_vec = find_first_bit(
8867 (const unsigned long *)sregs->interrupt_bitmap, max_bits);
8868 if (pending_vec < max_bits) {
66fd3f7f 8869 kvm_queue_interrupt(vcpu, pending_vec, false);
923c61bb 8870 pr_debug("Set back pending irq %d\n", pending_vec);
b6c7a5dc
HB
8871 }
8872
3e6e0aab
GT
8873 kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
8874 kvm_set_segment(vcpu, &sregs->ds, VCPU_SREG_DS);
8875 kvm_set_segment(vcpu, &sregs->es, VCPU_SREG_ES);
8876 kvm_set_segment(vcpu, &sregs->fs, VCPU_SREG_FS);
8877 kvm_set_segment(vcpu, &sregs->gs, VCPU_SREG_GS);
8878 kvm_set_segment(vcpu, &sregs->ss, VCPU_SREG_SS);
b6c7a5dc 8879
3e6e0aab
GT
8880 kvm_set_segment(vcpu, &sregs->tr, VCPU_SREG_TR);
8881 kvm_set_segment(vcpu, &sregs->ldt, VCPU_SREG_LDTR);
b6c7a5dc 8882
5f0269f5
ME
8883 update_cr8_intercept(vcpu);
8884
9c3e4aab 8885 /* Older userspace won't unhalt the vcpu on reset. */
c5af89b6 8886 if (kvm_vcpu_is_bsp(vcpu) && kvm_rip_read(vcpu) == 0xfff0 &&
9c3e4aab 8887 sregs->cs.selector == 0xf000 && sregs->cs.base == 0xffff0000 &&
3eeb3288 8888 !is_protmode(vcpu))
9c3e4aab
MT
8889 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
8890
3842d135
AK
8891 kvm_make_request(KVM_REQ_EVENT, vcpu);
8892
b4ef9d4e
CD
8893 ret = 0;
8894out:
01643c51
KH
8895 return ret;
8896}
8897
8898int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
8899 struct kvm_sregs *sregs)
8900{
8901 int ret;
8902
8903 vcpu_load(vcpu);
8904 ret = __set_sregs(vcpu, sregs);
b4ef9d4e
CD
8905 vcpu_put(vcpu);
8906 return ret;
b6c7a5dc
HB
8907}
8908
d0bfb940
JK
8909int kvm_arch_vcpu_ioctl_set_guest_debug(struct kvm_vcpu *vcpu,
8910 struct kvm_guest_debug *dbg)
b6c7a5dc 8911{
355be0b9 8912 unsigned long rflags;
ae675ef0 8913 int i, r;
b6c7a5dc 8914
66b56562
CD
8915 vcpu_load(vcpu);
8916
4f926bf2
JK
8917 if (dbg->control & (KVM_GUESTDBG_INJECT_DB | KVM_GUESTDBG_INJECT_BP)) {
8918 r = -EBUSY;
8919 if (vcpu->arch.exception.pending)
2122ff5e 8920 goto out;
4f926bf2
JK
8921 if (dbg->control & KVM_GUESTDBG_INJECT_DB)
8922 kvm_queue_exception(vcpu, DB_VECTOR);
8923 else
8924 kvm_queue_exception(vcpu, BP_VECTOR);
8925 }
8926
91586a3b
JK
8927 /*
8928 * Read rflags as long as potentially injected trace flags are still
8929 * filtered out.
8930 */
8931 rflags = kvm_get_rflags(vcpu);
355be0b9
JK
8932
8933 vcpu->guest_debug = dbg->control;
8934 if (!(vcpu->guest_debug & KVM_GUESTDBG_ENABLE))
8935 vcpu->guest_debug = 0;
8936
8937 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) {
ae675ef0
JK
8938 for (i = 0; i < KVM_NR_DB_REGS; ++i)
8939 vcpu->arch.eff_db[i] = dbg->arch.debugreg[i];
c8639010 8940 vcpu->arch.guest_debug_dr7 = dbg->arch.debugreg[7];
ae675ef0
JK
8941 } else {
8942 for (i = 0; i < KVM_NR_DB_REGS; i++)
8943 vcpu->arch.eff_db[i] = vcpu->arch.db[i];
ae675ef0 8944 }
c8639010 8945 kvm_update_dr7(vcpu);
ae675ef0 8946
f92653ee
JK
8947 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
8948 vcpu->arch.singlestep_rip = kvm_rip_read(vcpu) +
8949 get_segment_base(vcpu, VCPU_SREG_CS);
94fe45da 8950
91586a3b
JK
8951 /*
8952 * Trigger an rflags update that will inject or remove the trace
8953 * flags.
8954 */
8955 kvm_set_rflags(vcpu, rflags);
b6c7a5dc 8956
a96036b8 8957 kvm_x86_ops->update_bp_intercept(vcpu);
b6c7a5dc 8958
4f926bf2 8959 r = 0;
d0bfb940 8960
2122ff5e 8961out:
66b56562 8962 vcpu_put(vcpu);
b6c7a5dc
HB
8963 return r;
8964}
8965
8b006791
ZX
8966/*
8967 * Translate a guest virtual address to a guest physical address.
8968 */
8969int kvm_arch_vcpu_ioctl_translate(struct kvm_vcpu *vcpu,
8970 struct kvm_translation *tr)
8971{
8972 unsigned long vaddr = tr->linear_address;
8973 gpa_t gpa;
f656ce01 8974 int idx;
8b006791 8975
1da5b61d
CD
8976 vcpu_load(vcpu);
8977
f656ce01 8978 idx = srcu_read_lock(&vcpu->kvm->srcu);
1871c602 8979 gpa = kvm_mmu_gva_to_gpa_system(vcpu, vaddr, NULL);
f656ce01 8980 srcu_read_unlock(&vcpu->kvm->srcu, idx);
8b006791
ZX
8981 tr->physical_address = gpa;
8982 tr->valid = gpa != UNMAPPED_GVA;
8983 tr->writeable = 1;
8984 tr->usermode = 0;
8b006791 8985
1da5b61d 8986 vcpu_put(vcpu);
8b006791
ZX
8987 return 0;
8988}
8989
d0752060
HB
8990int kvm_arch_vcpu_ioctl_get_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
8991{
1393123e 8992 struct fxregs_state *fxsave;
d0752060 8993
1393123e 8994 vcpu_load(vcpu);
d0752060 8995
b666a4b6 8996 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
d0752060
HB
8997 memcpy(fpu->fpr, fxsave->st_space, 128);
8998 fpu->fcw = fxsave->cwd;
8999 fpu->fsw = fxsave->swd;
9000 fpu->ftwx = fxsave->twd;
9001 fpu->last_opcode = fxsave->fop;
9002 fpu->last_ip = fxsave->rip;
9003 fpu->last_dp = fxsave->rdp;
0e96f31e 9004 memcpy(fpu->xmm, fxsave->xmm_space, sizeof(fxsave->xmm_space));
d0752060 9005
1393123e 9006 vcpu_put(vcpu);
d0752060
HB
9007 return 0;
9008}
9009
9010int kvm_arch_vcpu_ioctl_set_fpu(struct kvm_vcpu *vcpu, struct kvm_fpu *fpu)
9011{
6a96bc7f
CD
9012 struct fxregs_state *fxsave;
9013
9014 vcpu_load(vcpu);
9015
b666a4b6 9016 fxsave = &vcpu->arch.guest_fpu->state.fxsave;
d0752060 9017
d0752060
HB
9018 memcpy(fxsave->st_space, fpu->fpr, 128);
9019 fxsave->cwd = fpu->fcw;
9020 fxsave->swd = fpu->fsw;
9021 fxsave->twd = fpu->ftwx;
9022 fxsave->fop = fpu->last_opcode;
9023 fxsave->rip = fpu->last_ip;
9024 fxsave->rdp = fpu->last_dp;
0e96f31e 9025 memcpy(fxsave->xmm_space, fpu->xmm, sizeof(fxsave->xmm_space));
d0752060 9026
6a96bc7f 9027 vcpu_put(vcpu);
d0752060
HB
9028 return 0;
9029}
9030
01643c51
KH
9031static void store_regs(struct kvm_vcpu *vcpu)
9032{
9033 BUILD_BUG_ON(sizeof(struct kvm_sync_regs) > SYNC_REGS_SIZE_BYTES);
9034
9035 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_REGS)
9036 __get_regs(vcpu, &vcpu->run->s.regs.regs);
9037
9038 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_SREGS)
9039 __get_sregs(vcpu, &vcpu->run->s.regs.sregs);
9040
9041 if (vcpu->run->kvm_valid_regs & KVM_SYNC_X86_EVENTS)
9042 kvm_vcpu_ioctl_x86_get_vcpu_events(
9043 vcpu, &vcpu->run->s.regs.events);
9044}
9045
9046static int sync_regs(struct kvm_vcpu *vcpu)
9047{
9048 if (vcpu->run->kvm_dirty_regs & ~KVM_SYNC_X86_VALID_FIELDS)
9049 return -EINVAL;
9050
9051 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_REGS) {
9052 __set_regs(vcpu, &vcpu->run->s.regs.regs);
9053 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_REGS;
9054 }
9055 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_SREGS) {
9056 if (__set_sregs(vcpu, &vcpu->run->s.regs.sregs))
9057 return -EINVAL;
9058 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_SREGS;
9059 }
9060 if (vcpu->run->kvm_dirty_regs & KVM_SYNC_X86_EVENTS) {
9061 if (kvm_vcpu_ioctl_x86_set_vcpu_events(
9062 vcpu, &vcpu->run->s.regs.events))
9063 return -EINVAL;
9064 vcpu->run->kvm_dirty_regs &= ~KVM_SYNC_X86_EVENTS;
9065 }
9066
9067 return 0;
9068}
9069
0ee6a517 9070static void fx_init(struct kvm_vcpu *vcpu)
d0752060 9071{
b666a4b6 9072 fpstate_init(&vcpu->arch.guest_fpu->state);
782511b0 9073 if (boot_cpu_has(X86_FEATURE_XSAVES))
b666a4b6 9074 vcpu->arch.guest_fpu->state.xsave.header.xcomp_bv =
df1daba7 9075 host_xcr0 | XSTATE_COMPACTION_ENABLED;
d0752060 9076
2acf923e
DC
9077 /*
9078 * Ensure guest xcr0 is valid for loading
9079 */
d91cab78 9080 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
2acf923e 9081
ad312c7c 9082 vcpu->arch.cr0 |= X86_CR0_ET;
d0752060 9083}
d0752060 9084
e9b11c17
ZX
9085void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
9086{
bd768e14
IY
9087 void *wbinvd_dirty_mask = vcpu->arch.wbinvd_dirty_mask;
9088
12f9a48f 9089 kvmclock_reset(vcpu);
7f1ea208 9090
e9b11c17 9091 kvm_x86_ops->vcpu_free(vcpu);
bd768e14 9092 free_cpumask_var(wbinvd_dirty_mask);
e9b11c17
ZX
9093}
9094
9095struct kvm_vcpu *kvm_arch_vcpu_create(struct kvm *kvm,
9096 unsigned int id)
9097{
c447e76b
LL
9098 struct kvm_vcpu *vcpu;
9099
b0c39dc6 9100 if (kvm_check_tsc_unstable() && atomic_read(&kvm->online_vcpus) != 0)
6755bae8
ZA
9101 printk_once(KERN_WARNING
9102 "kvm: SMP vm created on host with unstable TSC; "
9103 "guest TSC will not be reliable\n");
c447e76b
LL
9104
9105 vcpu = kvm_x86_ops->vcpu_create(kvm, id);
9106
c447e76b 9107 return vcpu;
26e5215f 9108}
e9b11c17 9109
26e5215f
AK
9110int kvm_arch_vcpu_setup(struct kvm_vcpu *vcpu)
9111{
0cf9135b 9112 vcpu->arch.arch_capabilities = kvm_get_arch_capabilities();
e53d88af 9113 vcpu->arch.msr_platform_info = MSR_PLATFORM_INFO_CPUID_FAULT;
19efffa2 9114 kvm_vcpu_mtrr_init(vcpu);
ec7660cc 9115 vcpu_load(vcpu);
d28bc9dd 9116 kvm_vcpu_reset(vcpu, false);
e1732991 9117 kvm_init_mmu(vcpu, false);
e9b11c17 9118 vcpu_put(vcpu);
ec7660cc 9119 return 0;
e9b11c17
ZX
9120}
9121
31928aa5 9122void kvm_arch_vcpu_postcreate(struct kvm_vcpu *vcpu)
42897d86 9123{
8fe8ab46 9124 struct msr_data msr;
332967a3 9125 struct kvm *kvm = vcpu->kvm;
42897d86 9126
d3457c87
RK
9127 kvm_hv_vcpu_postcreate(vcpu);
9128
ec7660cc 9129 if (mutex_lock_killable(&vcpu->mutex))
31928aa5 9130 return;
ec7660cc 9131 vcpu_load(vcpu);
8fe8ab46
WA
9132 msr.data = 0x0;
9133 msr.index = MSR_IA32_TSC;
9134 msr.host_initiated = true;
9135 kvm_write_tsc(vcpu, &msr);
42897d86 9136 vcpu_put(vcpu);
2d5ba19b
MT
9137
9138 /* poll control enabled by default */
9139 vcpu->arch.msr_kvm_poll_control = 1;
9140
ec7660cc 9141 mutex_unlock(&vcpu->mutex);
42897d86 9142
630994b3
MT
9143 if (!kvmclock_periodic_sync)
9144 return;
9145
332967a3
AJ
9146 schedule_delayed_work(&kvm->arch.kvmclock_sync_work,
9147 KVMCLOCK_SYNC_PERIOD);
42897d86
MT
9148}
9149
d40ccc62 9150void kvm_arch_vcpu_destroy(struct kvm_vcpu *vcpu)
e9b11c17 9151{
344d9588
GN
9152 vcpu->arch.apf.msr_val = 0;
9153
ec7660cc 9154 vcpu_load(vcpu);
e9b11c17
ZX
9155 kvm_mmu_unload(vcpu);
9156 vcpu_put(vcpu);
9157
9158 kvm_x86_ops->vcpu_free(vcpu);
9159}
9160
d28bc9dd 9161void kvm_vcpu_reset(struct kvm_vcpu *vcpu, bool init_event)
e9b11c17 9162{
b7e31be3
RK
9163 kvm_lapic_reset(vcpu, init_event);
9164
e69fab5d
PB
9165 vcpu->arch.hflags = 0;
9166
c43203ca 9167 vcpu->arch.smi_pending = 0;
52797bf9 9168 vcpu->arch.smi_count = 0;
7460fb4a
AK
9169 atomic_set(&vcpu->arch.nmi_queued, 0);
9170 vcpu->arch.nmi_pending = 0;
448fa4a9 9171 vcpu->arch.nmi_injected = false;
5f7552d4
NA
9172 kvm_clear_interrupt_queue(vcpu);
9173 kvm_clear_exception_queue(vcpu);
664f8e26 9174 vcpu->arch.exception.pending = false;
448fa4a9 9175
42dbaa5a 9176 memset(vcpu->arch.db, 0, sizeof(vcpu->arch.db));
ae561ede 9177 kvm_update_dr0123(vcpu);
6f43ed01 9178 vcpu->arch.dr6 = DR6_INIT;
73aaf249 9179 kvm_update_dr6(vcpu);
42dbaa5a 9180 vcpu->arch.dr7 = DR7_FIXED_1;
c8639010 9181 kvm_update_dr7(vcpu);
42dbaa5a 9182
1119022c
NA
9183 vcpu->arch.cr2 = 0;
9184
3842d135 9185 kvm_make_request(KVM_REQ_EVENT, vcpu);
344d9588 9186 vcpu->arch.apf.msr_val = 0;
c9aaa895 9187 vcpu->arch.st.msr_val = 0;
3842d135 9188
12f9a48f
GC
9189 kvmclock_reset(vcpu);
9190
af585b92
GN
9191 kvm_clear_async_pf_completion_queue(vcpu);
9192 kvm_async_pf_hash_reset(vcpu);
9193 vcpu->arch.apf.halted = false;
3842d135 9194
a554d207
WL
9195 if (kvm_mpx_supported()) {
9196 void *mpx_state_buffer;
9197
9198 /*
9199 * To avoid have the INIT path from kvm_apic_has_events() that be
9200 * called with loaded FPU and does not let userspace fix the state.
9201 */
f775b13e
RR
9202 if (init_event)
9203 kvm_put_guest_fpu(vcpu);
b666a4b6 9204 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
abd16d68 9205 XFEATURE_BNDREGS);
a554d207
WL
9206 if (mpx_state_buffer)
9207 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndreg_state));
b666a4b6 9208 mpx_state_buffer = get_xsave_addr(&vcpu->arch.guest_fpu->state.xsave,
abd16d68 9209 XFEATURE_BNDCSR);
a554d207
WL
9210 if (mpx_state_buffer)
9211 memset(mpx_state_buffer, 0, sizeof(struct mpx_bndcsr));
f775b13e
RR
9212 if (init_event)
9213 kvm_load_guest_fpu(vcpu);
a554d207
WL
9214 }
9215
64d60670 9216 if (!init_event) {
d28bc9dd 9217 kvm_pmu_reset(vcpu);
64d60670 9218 vcpu->arch.smbase = 0x30000;
db2336a8 9219
db2336a8 9220 vcpu->arch.msr_misc_features_enables = 0;
a554d207
WL
9221
9222 vcpu->arch.xcr0 = XFEATURE_MASK_FP;
64d60670 9223 }
f5132b01 9224
66f7b72e
JS
9225 memset(vcpu->arch.regs, 0, sizeof(vcpu->arch.regs));
9226 vcpu->arch.regs_avail = ~0;
9227 vcpu->arch.regs_dirty = ~0;
9228
a554d207
WL
9229 vcpu->arch.ia32_xss = 0;
9230
d28bc9dd 9231 kvm_x86_ops->vcpu_reset(vcpu, init_event);
e9b11c17
ZX
9232}
9233
2b4a273b 9234void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector)
66450a21
JK
9235{
9236 struct kvm_segment cs;
9237
9238 kvm_get_segment(vcpu, &cs, VCPU_SREG_CS);
9239 cs.selector = vector << 8;
9240 cs.base = vector << 12;
9241 kvm_set_segment(vcpu, &cs, VCPU_SREG_CS);
9242 kvm_rip_write(vcpu, 0);
e9b11c17
ZX
9243}
9244
13a34e06 9245int kvm_arch_hardware_enable(void)
e9b11c17 9246{
ca84d1a2
ZA
9247 struct kvm *kvm;
9248 struct kvm_vcpu *vcpu;
9249 int i;
0dd6a6ed
ZA
9250 int ret;
9251 u64 local_tsc;
9252 u64 max_tsc = 0;
9253 bool stable, backwards_tsc = false;
18863bdd
AK
9254
9255 kvm_shared_msr_cpu_online();
13a34e06 9256 ret = kvm_x86_ops->hardware_enable();
0dd6a6ed
ZA
9257 if (ret != 0)
9258 return ret;
9259
4ea1636b 9260 local_tsc = rdtsc();
b0c39dc6 9261 stable = !kvm_check_tsc_unstable();
0dd6a6ed
ZA
9262 list_for_each_entry(kvm, &vm_list, vm_list) {
9263 kvm_for_each_vcpu(i, vcpu, kvm) {
9264 if (!stable && vcpu->cpu == smp_processor_id())
105b21bb 9265 kvm_make_request(KVM_REQ_CLOCK_UPDATE, vcpu);
0dd6a6ed
ZA
9266 if (stable && vcpu->arch.last_host_tsc > local_tsc) {
9267 backwards_tsc = true;
9268 if (vcpu->arch.last_host_tsc > max_tsc)
9269 max_tsc = vcpu->arch.last_host_tsc;
9270 }
9271 }
9272 }
9273
9274 /*
9275 * Sometimes, even reliable TSCs go backwards. This happens on
9276 * platforms that reset TSC during suspend or hibernate actions, but
9277 * maintain synchronization. We must compensate. Fortunately, we can
9278 * detect that condition here, which happens early in CPU bringup,
9279 * before any KVM threads can be running. Unfortunately, we can't
9280 * bring the TSCs fully up to date with real time, as we aren't yet far
9281 * enough into CPU bringup that we know how much real time has actually
9285ec4c 9282 * elapsed; our helper function, ktime_get_boottime_ns() will be using boot
0dd6a6ed
ZA
9283 * variables that haven't been updated yet.
9284 *
9285 * So we simply find the maximum observed TSC above, then record the
9286 * adjustment to TSC in each VCPU. When the VCPU later gets loaded,
9287 * the adjustment will be applied. Note that we accumulate
9288 * adjustments, in case multiple suspend cycles happen before some VCPU
9289 * gets a chance to run again. In the event that no KVM threads get a
9290 * chance to run, we will miss the entire elapsed period, as we'll have
9291 * reset last_host_tsc, so VCPUs will not have the TSC adjusted and may
9292 * loose cycle time. This isn't too big a deal, since the loss will be
9293 * uniform across all VCPUs (not to mention the scenario is extremely
9294 * unlikely). It is possible that a second hibernate recovery happens
9295 * much faster than a first, causing the observed TSC here to be
9296 * smaller; this would require additional padding adjustment, which is
9297 * why we set last_host_tsc to the local tsc observed here.
9298 *
9299 * N.B. - this code below runs only on platforms with reliable TSC,
9300 * as that is the only way backwards_tsc is set above. Also note
9301 * that this runs for ALL vcpus, which is not a bug; all VCPUs should
9302 * have the same delta_cyc adjustment applied if backwards_tsc
9303 * is detected. Note further, this adjustment is only done once,
9304 * as we reset last_host_tsc on all VCPUs to stop this from being
9305 * called multiple times (one for each physical CPU bringup).
9306 *
4a969980 9307 * Platforms with unreliable TSCs don't have to deal with this, they
0dd6a6ed
ZA
9308 * will be compensated by the logic in vcpu_load, which sets the TSC to
9309 * catchup mode. This will catchup all VCPUs to real time, but cannot
9310 * guarantee that they stay in perfect synchronization.
9311 */
9312 if (backwards_tsc) {
9313 u64 delta_cyc = max_tsc - local_tsc;
9314 list_for_each_entry(kvm, &vm_list, vm_list) {
a826faf1 9315 kvm->arch.backwards_tsc_observed = true;
0dd6a6ed
ZA
9316 kvm_for_each_vcpu(i, vcpu, kvm) {
9317 vcpu->arch.tsc_offset_adjustment += delta_cyc;
9318 vcpu->arch.last_host_tsc = local_tsc;
105b21bb 9319 kvm_make_request(KVM_REQ_MASTERCLOCK_UPDATE, vcpu);
0dd6a6ed
ZA
9320 }
9321
9322 /*
9323 * We have to disable TSC offset matching.. if you were
9324 * booting a VM while issuing an S4 host suspend....
9325 * you may have some problem. Solving this issue is
9326 * left as an exercise to the reader.
9327 */
9328 kvm->arch.last_tsc_nsec = 0;
9329 kvm->arch.last_tsc_write = 0;
9330 }
9331
9332 }
9333 return 0;
e9b11c17
ZX
9334}
9335
13a34e06 9336void kvm_arch_hardware_disable(void)
e9b11c17 9337{
13a34e06
RK
9338 kvm_x86_ops->hardware_disable();
9339 drop_user_return_notifiers();
e9b11c17
ZX
9340}
9341
9342int kvm_arch_hardware_setup(void)
9343{
9e9c3fe4
NA
9344 int r;
9345
9346 r = kvm_x86_ops->hardware_setup();
9347 if (r != 0)
9348 return r;
9349
35181e86
HZ
9350 if (kvm_has_tsc_control) {
9351 /*
9352 * Make sure the user can only configure tsc_khz values that
9353 * fit into a signed integer.
273ba457 9354 * A min value is not calculated because it will always
35181e86
HZ
9355 * be 1 on all machines.
9356 */
9357 u64 max = min(0x7fffffffULL,
9358 __scale_tsc(kvm_max_tsc_scaling_ratio, tsc_khz));
9359 kvm_max_guest_tsc_khz = max;
9360
ad721883 9361 kvm_default_tsc_scaling_ratio = 1ULL << kvm_tsc_scaling_ratio_frac_bits;
35181e86 9362 }
ad721883 9363
139a12cf
AL
9364 if (boot_cpu_has(X86_FEATURE_XSAVES))
9365 rdmsrl(MSR_IA32_XSS, host_xss);
9366
9e9c3fe4
NA
9367 kvm_init_msr_list();
9368 return 0;
e9b11c17
ZX
9369}
9370
9371void kvm_arch_hardware_unsetup(void)
9372{
9373 kvm_x86_ops->hardware_unsetup();
9374}
9375
f257d6dc 9376int kvm_arch_check_processor_compat(void)
e9b11c17 9377{
f257d6dc 9378 return kvm_x86_ops->check_processor_compatibility();
d71ba788
PB
9379}
9380
9381bool kvm_vcpu_is_reset_bsp(struct kvm_vcpu *vcpu)
9382{
9383 return vcpu->kvm->arch.bsp_vcpu_id == vcpu->vcpu_id;
9384}
9385EXPORT_SYMBOL_GPL(kvm_vcpu_is_reset_bsp);
9386
9387bool kvm_vcpu_is_bsp(struct kvm_vcpu *vcpu)
9388{
9389 return (vcpu->arch.apic_base & MSR_IA32_APICBASE_BSP) != 0;
e9b11c17
ZX
9390}
9391
54e9818f 9392struct static_key kvm_no_apic_vcpu __read_mostly;
bce87cce 9393EXPORT_SYMBOL_GPL(kvm_no_apic_vcpu);
54e9818f 9394
e9b11c17
ZX
9395int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
9396{
9397 struct page *page;
e9b11c17
ZX
9398 int r;
9399
9aabc88f 9400 vcpu->arch.emulate_ctxt.ops = &emulate_ops;
26de7988 9401 if (!irqchip_in_kernel(vcpu->kvm) || kvm_vcpu_is_reset_bsp(vcpu))
a4535290 9402 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
e9b11c17 9403 else
a4535290 9404 vcpu->arch.mp_state = KVM_MP_STATE_UNINITIALIZED;
e9b11c17
ZX
9405
9406 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
9407 if (!page) {
9408 r = -ENOMEM;
9409 goto fail;
9410 }
ad312c7c 9411 vcpu->arch.pio_data = page_address(page);
e9b11c17 9412
cc578287 9413 kvm_set_tsc_khz(vcpu, max_tsc_khz);
c285545f 9414
e9b11c17
ZX
9415 r = kvm_mmu_create(vcpu);
9416 if (r < 0)
9417 goto fail_free_pio_data;
9418
26de7988 9419 if (irqchip_in_kernel(vcpu->kvm)) {
2cf9af0b 9420 vcpu->arch.apicv_active = kvm_x86_ops->get_enable_apicv(vcpu->kvm);
39497d76 9421 r = kvm_create_lapic(vcpu, lapic_timer_advance_ns);
e9b11c17
ZX
9422 if (r < 0)
9423 goto fail_mmu_destroy;
54e9818f
GN
9424 } else
9425 static_key_slow_inc(&kvm_no_apic_vcpu);
e9b11c17 9426
890ca9ae 9427 vcpu->arch.mce_banks = kzalloc(KVM_MAX_MCE_BANKS * sizeof(u64) * 4,
254272ce 9428 GFP_KERNEL_ACCOUNT);
890ca9ae
HY
9429 if (!vcpu->arch.mce_banks) {
9430 r = -ENOMEM;
443c39bc 9431 goto fail_free_lapic;
890ca9ae
HY
9432 }
9433 vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
9434
254272ce
BG
9435 if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask,
9436 GFP_KERNEL_ACCOUNT)) {
f1797359 9437 r = -ENOMEM;
f5f48ee1 9438 goto fail_free_mce_banks;
f1797359 9439 }
f5f48ee1 9440
0ee6a517 9441 fx_init(vcpu);
66f7b72e 9442
4344ee98 9443 vcpu->arch.guest_xstate_size = XSAVE_HDR_SIZE + XSAVE_HDR_OFFSET;
d7876f1b 9444
5a4f55cd
EK
9445 vcpu->arch.maxphyaddr = cpuid_query_maxphyaddr(vcpu);
9446
74545705
RK
9447 vcpu->arch.pat = MSR_IA32_CR_PAT_DEFAULT;
9448
af585b92 9449 kvm_async_pf_hash_reset(vcpu);
f5132b01 9450 kvm_pmu_init(vcpu);
af585b92 9451
1c1a9ce9 9452 vcpu->arch.pending_external_vector = -1;
de63ad4c 9453 vcpu->arch.preempted_in_kernel = false;
1c1a9ce9 9454
5c919412
AS
9455 kvm_hv_vcpu_init(vcpu);
9456
e9b11c17 9457 return 0;
0ee6a517 9458
f5f48ee1
SY
9459fail_free_mce_banks:
9460 kfree(vcpu->arch.mce_banks);
443c39bc
WY
9461fail_free_lapic:
9462 kvm_free_lapic(vcpu);
e9b11c17
ZX
9463fail_mmu_destroy:
9464 kvm_mmu_destroy(vcpu);
9465fail_free_pio_data:
ad312c7c 9466 free_page((unsigned long)vcpu->arch.pio_data);
e9b11c17
ZX
9467fail:
9468 return r;
9469}
9470
9471void kvm_arch_vcpu_uninit(struct kvm_vcpu *vcpu)
9472{
f656ce01
MT
9473 int idx;
9474
1f4b34f8 9475 kvm_hv_vcpu_uninit(vcpu);
f5132b01 9476 kvm_pmu_destroy(vcpu);
36cb93fd 9477 kfree(vcpu->arch.mce_banks);
e9b11c17 9478 kvm_free_lapic(vcpu);
f656ce01 9479 idx = srcu_read_lock(&vcpu->kvm->srcu);
e9b11c17 9480 kvm_mmu_destroy(vcpu);
f656ce01 9481 srcu_read_unlock(&vcpu->kvm->srcu, idx);
ad312c7c 9482 free_page((unsigned long)vcpu->arch.pio_data);
35754c98 9483 if (!lapic_in_kernel(vcpu))
54e9818f 9484 static_key_slow_dec(&kvm_no_apic_vcpu);
e9b11c17 9485}
d19a9cd2 9486
e790d9ef
RK
9487void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu)
9488{
b35e5548
LX
9489 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
9490
c595ceee 9491 vcpu->arch.l1tf_flush_l1d = true;
b35e5548
LX
9492 if (pmu->version && unlikely(pmu->event_count)) {
9493 pmu->need_cleanup = true;
9494 kvm_make_request(KVM_REQ_PMU, vcpu);
9495 }
ae97a3b8 9496 kvm_x86_ops->sched_in(vcpu, cpu);
e790d9ef
RK
9497}
9498
e08b9637 9499int kvm_arch_init_vm(struct kvm *kvm, unsigned long type)
d19a9cd2 9500{
e08b9637
CO
9501 if (type)
9502 return -EINVAL;
9503
6ef768fa 9504 INIT_HLIST_HEAD(&kvm->arch.mask_notifier_list);
f05e70ac 9505 INIT_LIST_HEAD(&kvm->arch.active_mmu_pages);
10605204 9506 INIT_LIST_HEAD(&kvm->arch.zapped_obsolete_pages);
1aa9b957 9507 INIT_LIST_HEAD(&kvm->arch.lpage_disallowed_mmu_pages);
4d5c5d0f 9508 INIT_LIST_HEAD(&kvm->arch.assigned_dev_head);
e0f0bbc5 9509 atomic_set(&kvm->arch.noncoherent_dma_count, 0);
d19a9cd2 9510
5550af4d
SY
9511 /* Reserve bit 0 of irq_sources_bitmap for userspace irq source */
9512 set_bit(KVM_USERSPACE_IRQ_SOURCE_ID, &kvm->arch.irq_sources_bitmap);
7a84428a
AW
9513 /* Reserve bit 1 of irq_sources_bitmap for irqfd-resampler */
9514 set_bit(KVM_IRQFD_RESAMPLE_IRQ_SOURCE_ID,
9515 &kvm->arch.irq_sources_bitmap);
5550af4d 9516
038f8c11 9517 raw_spin_lock_init(&kvm->arch.tsc_write_lock);
1e08ec4a 9518 mutex_init(&kvm->arch.apic_map_lock);
d828199e
MT
9519 spin_lock_init(&kvm->arch.pvclock_gtod_sync_lock);
9520
9285ec4c 9521 kvm->arch.kvmclock_offset = -ktime_get_boottime_ns();
d828199e 9522 pvclock_update_vm_gtod_copy(kvm);
53f658b3 9523
6fbbde9a
DS
9524 kvm->arch.guest_can_read_msr_platform_info = true;
9525
7e44e449 9526 INIT_DELAYED_WORK(&kvm->arch.kvmclock_update_work, kvmclock_update_fn);
332967a3 9527 INIT_DELAYED_WORK(&kvm->arch.kvmclock_sync_work, kvmclock_sync_fn);
7e44e449 9528
cbc0236a 9529 kvm_hv_init_vm(kvm);
0eb05bf2 9530 kvm_page_track_init(kvm);
13d268ca 9531 kvm_mmu_init_vm(kvm);
0eb05bf2 9532
92735b1b 9533 return kvm_x86_ops->vm_init(kvm);
d19a9cd2
ZX
9534}
9535
1aa9b957
JS
9536int kvm_arch_post_init_vm(struct kvm *kvm)
9537{
9538 return kvm_mmu_post_init_vm(kvm);
9539}
9540
d19a9cd2
ZX
9541static void kvm_unload_vcpu_mmu(struct kvm_vcpu *vcpu)
9542{
ec7660cc 9543 vcpu_load(vcpu);
d19a9cd2
ZX
9544 kvm_mmu_unload(vcpu);
9545 vcpu_put(vcpu);
9546}
9547
9548static void kvm_free_vcpus(struct kvm *kvm)
9549{
9550 unsigned int i;
988a2cae 9551 struct kvm_vcpu *vcpu;
d19a9cd2
ZX
9552
9553 /*
9554 * Unpin any mmu pages first.
9555 */
af585b92
GN
9556 kvm_for_each_vcpu(i, vcpu, kvm) {
9557 kvm_clear_async_pf_completion_queue(vcpu);
988a2cae 9558 kvm_unload_vcpu_mmu(vcpu);
af585b92 9559 }
988a2cae
GN
9560 kvm_for_each_vcpu(i, vcpu, kvm)
9561 kvm_arch_vcpu_free(vcpu);
9562
9563 mutex_lock(&kvm->lock);
9564 for (i = 0; i < atomic_read(&kvm->online_vcpus); i++)
9565 kvm->vcpus[i] = NULL;
d19a9cd2 9566
988a2cae
GN
9567 atomic_set(&kvm->online_vcpus, 0);
9568 mutex_unlock(&kvm->lock);
d19a9cd2
ZX
9569}
9570
ad8ba2cd
SY
9571void kvm_arch_sync_events(struct kvm *kvm)
9572{
332967a3 9573 cancel_delayed_work_sync(&kvm->arch.kvmclock_sync_work);
7e44e449 9574 cancel_delayed_work_sync(&kvm->arch.kvmclock_update_work);
aea924f6 9575 kvm_free_pit(kvm);
ad8ba2cd
SY
9576}
9577
1d8007bd 9578int __x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
9da0e4d5
PB
9579{
9580 int i, r;
25188b99 9581 unsigned long hva;
f0d648bd
PB
9582 struct kvm_memslots *slots = kvm_memslots(kvm);
9583 struct kvm_memory_slot *slot, old;
9da0e4d5
PB
9584
9585 /* Called with kvm->slots_lock held. */
1d8007bd
PB
9586 if (WARN_ON(id >= KVM_MEM_SLOTS_NUM))
9587 return -EINVAL;
9da0e4d5 9588
f0d648bd
PB
9589 slot = id_to_memslot(slots, id);
9590 if (size) {
b21629da 9591 if (slot->npages)
f0d648bd
PB
9592 return -EEXIST;
9593
9594 /*
9595 * MAP_SHARED to prevent internal slot pages from being moved
9596 * by fork()/COW.
9597 */
9598 hva = vm_mmap(NULL, 0, size, PROT_READ | PROT_WRITE,
9599 MAP_SHARED | MAP_ANONYMOUS, 0);
9600 if (IS_ERR((void *)hva))
9601 return PTR_ERR((void *)hva);
9602 } else {
9603 if (!slot->npages)
9604 return 0;
9605
9606 hva = 0;
9607 }
9608
9609 old = *slot;
9da0e4d5 9610 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
1d8007bd 9611 struct kvm_userspace_memory_region m;
9da0e4d5 9612
1d8007bd
PB
9613 m.slot = id | (i << 16);
9614 m.flags = 0;
9615 m.guest_phys_addr = gpa;
f0d648bd 9616 m.userspace_addr = hva;
1d8007bd 9617 m.memory_size = size;
9da0e4d5
PB
9618 r = __kvm_set_memory_region(kvm, &m);
9619 if (r < 0)
9620 return r;
9621 }
9622
103c763c
EB
9623 if (!size)
9624 vm_munmap(old.userspace_addr, old.npages * PAGE_SIZE);
f0d648bd 9625
9da0e4d5
PB
9626 return 0;
9627}
9628EXPORT_SYMBOL_GPL(__x86_set_memory_region);
9629
1d8007bd 9630int x86_set_memory_region(struct kvm *kvm, int id, gpa_t gpa, u32 size)
9da0e4d5
PB
9631{
9632 int r;
9633
9634 mutex_lock(&kvm->slots_lock);
1d8007bd 9635 r = __x86_set_memory_region(kvm, id, gpa, size);
9da0e4d5
PB
9636 mutex_unlock(&kvm->slots_lock);
9637
9638 return r;
9639}
9640EXPORT_SYMBOL_GPL(x86_set_memory_region);
9641
1aa9b957
JS
9642void kvm_arch_pre_destroy_vm(struct kvm *kvm)
9643{
9644 kvm_mmu_pre_destroy_vm(kvm);
9645}
9646
d19a9cd2
ZX
9647void kvm_arch_destroy_vm(struct kvm *kvm)
9648{
27469d29
AH
9649 if (current->mm == kvm->mm) {
9650 /*
9651 * Free memory regions allocated on behalf of userspace,
9652 * unless the the memory map has changed due to process exit
9653 * or fd copying.
9654 */
1d8007bd
PB
9655 x86_set_memory_region(kvm, APIC_ACCESS_PAGE_PRIVATE_MEMSLOT, 0, 0);
9656 x86_set_memory_region(kvm, IDENTITY_PAGETABLE_PRIVATE_MEMSLOT, 0, 0);
9657 x86_set_memory_region(kvm, TSS_PRIVATE_MEMSLOT, 0, 0);
27469d29 9658 }
03543133
SS
9659 if (kvm_x86_ops->vm_destroy)
9660 kvm_x86_ops->vm_destroy(kvm);
c761159c
PX
9661 kvm_pic_destroy(kvm);
9662 kvm_ioapic_destroy(kvm);
d19a9cd2 9663 kvm_free_vcpus(kvm);
af1bae54 9664 kvfree(rcu_dereference_check(kvm->arch.apic_map, 1));
66bb8a06 9665 kfree(srcu_dereference_check(kvm->arch.pmu_event_filter, &kvm->srcu, 1));
13d268ca 9666 kvm_mmu_uninit_vm(kvm);
2beb6dad 9667 kvm_page_track_cleanup(kvm);
cbc0236a 9668 kvm_hv_destroy_vm(kvm);
d19a9cd2 9669}
0de10343 9670
5587027c 9671void kvm_arch_free_memslot(struct kvm *kvm, struct kvm_memory_slot *free,
db3fe4eb
TY
9672 struct kvm_memory_slot *dont)
9673{
9674 int i;
9675
d89cc617
TY
9676 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
9677 if (!dont || free->arch.rmap[i] != dont->arch.rmap[i]) {
548ef284 9678 kvfree(free->arch.rmap[i]);
d89cc617 9679 free->arch.rmap[i] = NULL;
77d11309 9680 }
d89cc617
TY
9681 if (i == 0)
9682 continue;
9683
9684 if (!dont || free->arch.lpage_info[i - 1] !=
9685 dont->arch.lpage_info[i - 1]) {
548ef284 9686 kvfree(free->arch.lpage_info[i - 1]);
d89cc617 9687 free->arch.lpage_info[i - 1] = NULL;
db3fe4eb
TY
9688 }
9689 }
21ebbeda
XG
9690
9691 kvm_page_track_free_memslot(free, dont);
db3fe4eb
TY
9692}
9693
5587027c
AK
9694int kvm_arch_create_memslot(struct kvm *kvm, struct kvm_memory_slot *slot,
9695 unsigned long npages)
db3fe4eb
TY
9696{
9697 int i;
9698
d89cc617 9699 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
92f94f1e 9700 struct kvm_lpage_info *linfo;
db3fe4eb
TY
9701 unsigned long ugfn;
9702 int lpages;
d89cc617 9703 int level = i + 1;
db3fe4eb
TY
9704
9705 lpages = gfn_to_index(slot->base_gfn + npages - 1,
9706 slot->base_gfn, level) + 1;
9707
d89cc617 9708 slot->arch.rmap[i] =
778e1cdd 9709 kvcalloc(lpages, sizeof(*slot->arch.rmap[i]),
254272ce 9710 GFP_KERNEL_ACCOUNT);
d89cc617 9711 if (!slot->arch.rmap[i])
77d11309 9712 goto out_free;
d89cc617
TY
9713 if (i == 0)
9714 continue;
77d11309 9715
254272ce 9716 linfo = kvcalloc(lpages, sizeof(*linfo), GFP_KERNEL_ACCOUNT);
92f94f1e 9717 if (!linfo)
db3fe4eb
TY
9718 goto out_free;
9719
92f94f1e
XG
9720 slot->arch.lpage_info[i - 1] = linfo;
9721
db3fe4eb 9722 if (slot->base_gfn & (KVM_PAGES_PER_HPAGE(level) - 1))
92f94f1e 9723 linfo[0].disallow_lpage = 1;
db3fe4eb 9724 if ((slot->base_gfn + npages) & (KVM_PAGES_PER_HPAGE(level) - 1))
92f94f1e 9725 linfo[lpages - 1].disallow_lpage = 1;
db3fe4eb
TY
9726 ugfn = slot->userspace_addr >> PAGE_SHIFT;
9727 /*
9728 * If the gfn and userspace address are not aligned wrt each
9729 * other, or if explicitly asked to, disable large page
9730 * support for this slot
9731 */
9732 if ((slot->base_gfn ^ ugfn) & (KVM_PAGES_PER_HPAGE(level) - 1) ||
9733 !kvm_largepages_enabled()) {
9734 unsigned long j;
9735
9736 for (j = 0; j < lpages; ++j)
92f94f1e 9737 linfo[j].disallow_lpage = 1;
db3fe4eb
TY
9738 }
9739 }
9740
21ebbeda
XG
9741 if (kvm_page_track_create_memslot(slot, npages))
9742 goto out_free;
9743
db3fe4eb
TY
9744 return 0;
9745
9746out_free:
d89cc617 9747 for (i = 0; i < KVM_NR_PAGE_SIZES; ++i) {
548ef284 9748 kvfree(slot->arch.rmap[i]);
d89cc617
TY
9749 slot->arch.rmap[i] = NULL;
9750 if (i == 0)
9751 continue;
9752
548ef284 9753 kvfree(slot->arch.lpage_info[i - 1]);
d89cc617 9754 slot->arch.lpage_info[i - 1] = NULL;
db3fe4eb
TY
9755 }
9756 return -ENOMEM;
9757}
9758
15248258 9759void kvm_arch_memslots_updated(struct kvm *kvm, u64 gen)
e59dbe09 9760{
e6dff7d1
TY
9761 /*
9762 * memslots->generation has been incremented.
9763 * mmio generation may have reached its maximum value.
9764 */
15248258 9765 kvm_mmu_invalidate_mmio_sptes(kvm, gen);
e59dbe09
TY
9766}
9767
f7784b8e
MT
9768int kvm_arch_prepare_memory_region(struct kvm *kvm,
9769 struct kvm_memory_slot *memslot,
09170a49 9770 const struct kvm_userspace_memory_region *mem,
7b6195a9 9771 enum kvm_mr_change change)
0de10343 9772{
f7784b8e
MT
9773 return 0;
9774}
9775
88178fd4
KH
9776static void kvm_mmu_slot_apply_flags(struct kvm *kvm,
9777 struct kvm_memory_slot *new)
9778{
9779 /* Still write protect RO slot */
9780 if (new->flags & KVM_MEM_READONLY) {
9781 kvm_mmu_slot_remove_write_access(kvm, new);
9782 return;
9783 }
9784
9785 /*
9786 * Call kvm_x86_ops dirty logging hooks when they are valid.
9787 *
9788 * kvm_x86_ops->slot_disable_log_dirty is called when:
9789 *
9790 * - KVM_MR_CREATE with dirty logging is disabled
9791 * - KVM_MR_FLAGS_ONLY with dirty logging is disabled in new flag
9792 *
9793 * The reason is, in case of PML, we need to set D-bit for any slots
9794 * with dirty logging disabled in order to eliminate unnecessary GPA
9795 * logging in PML buffer (and potential PML buffer full VMEXT). This
9796 * guarantees leaving PML enabled during guest's lifetime won't have
bdd303cb 9797 * any additional overhead from PML when guest is running with dirty
88178fd4
KH
9798 * logging disabled for memory slots.
9799 *
9800 * kvm_x86_ops->slot_enable_log_dirty is called when switching new slot
9801 * to dirty logging mode.
9802 *
9803 * If kvm_x86_ops dirty logging hooks are invalid, use write protect.
9804 *
9805 * In case of write protect:
9806 *
9807 * Write protect all pages for dirty logging.
9808 *
9809 * All the sptes including the large sptes which point to this
9810 * slot are set to readonly. We can not create any new large
9811 * spte on this slot until the end of the logging.
9812 *
9813 * See the comments in fast_page_fault().
9814 */
9815 if (new->flags & KVM_MEM_LOG_DIRTY_PAGES) {
9816 if (kvm_x86_ops->slot_enable_log_dirty)
9817 kvm_x86_ops->slot_enable_log_dirty(kvm, new);
9818 else
9819 kvm_mmu_slot_remove_write_access(kvm, new);
9820 } else {
9821 if (kvm_x86_ops->slot_disable_log_dirty)
9822 kvm_x86_ops->slot_disable_log_dirty(kvm, new);
9823 }
9824}
9825
f7784b8e 9826void kvm_arch_commit_memory_region(struct kvm *kvm,
09170a49 9827 const struct kvm_userspace_memory_region *mem,
8482644a 9828 const struct kvm_memory_slot *old,
f36f3f28 9829 const struct kvm_memory_slot *new,
8482644a 9830 enum kvm_mr_change change)
f7784b8e 9831{
48c0e4e9 9832 if (!kvm->arch.n_requested_mmu_pages)
4d66623c
WY
9833 kvm_mmu_change_mmu_pages(kvm,
9834 kvm_mmu_calculate_default_mmu_pages(kvm));
1c91cad4 9835
3ea3b7fa
WL
9836 /*
9837 * Dirty logging tracks sptes in 4k granularity, meaning that large
9838 * sptes have to be split. If live migration is successful, the guest
9839 * in the source machine will be destroyed and large sptes will be
9840 * created in the destination. However, if the guest continues to run
9841 * in the source machine (for example if live migration fails), small
9842 * sptes will remain around and cause bad performance.
9843 *
9844 * Scan sptes if dirty logging has been stopped, dropping those
9845 * which can be collapsed into a single large-page spte. Later
9846 * page faults will create the large-page sptes.
319109a2
SC
9847 *
9848 * There is no need to do this in any of the following cases:
9849 * CREATE: No dirty mappings will already exist.
9850 * MOVE/DELETE: The old mappings will already have been cleaned up by
9851 * kvm_arch_flush_shadow_memslot()
3ea3b7fa 9852 */
319109a2 9853 if (change == KVM_MR_FLAGS_ONLY &&
3ea3b7fa
WL
9854 (old->flags & KVM_MEM_LOG_DIRTY_PAGES) &&
9855 !(new->flags & KVM_MEM_LOG_DIRTY_PAGES))
9856 kvm_mmu_zap_collapsible_sptes(kvm, new);
9857
c972f3b1 9858 /*
88178fd4 9859 * Set up write protection and/or dirty logging for the new slot.
c126d94f 9860 *
88178fd4
KH
9861 * For KVM_MR_DELETE and KVM_MR_MOVE, the shadow pages of old slot have
9862 * been zapped so no dirty logging staff is needed for old slot. For
9863 * KVM_MR_FLAGS_ONLY, the old slot is essentially the same one as the
9864 * new and it's also covered when dealing with the new slot.
f36f3f28
PB
9865 *
9866 * FIXME: const-ify all uses of struct kvm_memory_slot.
c972f3b1 9867 */
88178fd4 9868 if (change != KVM_MR_DELETE)
f36f3f28 9869 kvm_mmu_slot_apply_flags(kvm, (struct kvm_memory_slot *) new);
0de10343 9870}
1d737c8a 9871
2df72e9b 9872void kvm_arch_flush_shadow_all(struct kvm *kvm)
34d4cb8f 9873{
7390de1e 9874 kvm_mmu_zap_all(kvm);
34d4cb8f
MT
9875}
9876
2df72e9b
MT
9877void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
9878 struct kvm_memory_slot *slot)
9879{
ae7cd873 9880 kvm_page_track_flush_slot(kvm, slot);
2df72e9b
MT
9881}
9882
e6c67d8c
LA
9883static inline bool kvm_guest_apic_has_interrupt(struct kvm_vcpu *vcpu)
9884{
9885 return (is_guest_mode(vcpu) &&
9886 kvm_x86_ops->guest_apic_has_interrupt &&
9887 kvm_x86_ops->guest_apic_has_interrupt(vcpu));
9888}
9889
5d9bc648
PB
9890static inline bool kvm_vcpu_has_events(struct kvm_vcpu *vcpu)
9891{
9892 if (!list_empty_careful(&vcpu->async_pf.done))
9893 return true;
9894
9895 if (kvm_apic_has_events(vcpu))
9896 return true;
9897
9898 if (vcpu->arch.pv.pv_unhalted)
9899 return true;
9900
a5f01f8e
WL
9901 if (vcpu->arch.exception.pending)
9902 return true;
9903
47a66eed
Z
9904 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9905 (vcpu->arch.nmi_pending &&
9906 kvm_x86_ops->nmi_allowed(vcpu)))
5d9bc648
PB
9907 return true;
9908
47a66eed
Z
9909 if (kvm_test_request(KVM_REQ_SMI, vcpu) ||
9910 (vcpu->arch.smi_pending && !is_smm(vcpu)))
73917739
PB
9911 return true;
9912
5d9bc648 9913 if (kvm_arch_interrupt_allowed(vcpu) &&
e6c67d8c
LA
9914 (kvm_cpu_has_interrupt(vcpu) ||
9915 kvm_guest_apic_has_interrupt(vcpu)))
5d9bc648
PB
9916 return true;
9917
1f4b34f8
AS
9918 if (kvm_hv_has_stimer_pending(vcpu))
9919 return true;
9920
5d9bc648
PB
9921 return false;
9922}
9923
1d737c8a
ZX
9924int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
9925{
5d9bc648 9926 return kvm_vcpu_running(vcpu) || kvm_vcpu_has_events(vcpu);
1d737c8a 9927}
5736199a 9928
17e433b5
WL
9929bool kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
9930{
9931 if (READ_ONCE(vcpu->arch.pv.pv_unhalted))
9932 return true;
9933
9934 if (kvm_test_request(KVM_REQ_NMI, vcpu) ||
9935 kvm_test_request(KVM_REQ_SMI, vcpu) ||
9936 kvm_test_request(KVM_REQ_EVENT, vcpu))
9937 return true;
9938
9939 if (vcpu->arch.apicv_active && kvm_x86_ops->dy_apicv_has_pending_interrupt(vcpu))
9940 return true;
9941
9942 return false;
9943}
9944
199b5763
LM
9945bool kvm_arch_vcpu_in_kernel(struct kvm_vcpu *vcpu)
9946{
de63ad4c 9947 return vcpu->arch.preempted_in_kernel;
199b5763
LM
9948}
9949
b6d33834 9950int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)
5736199a 9951{
b6d33834 9952 return kvm_vcpu_exiting_guest_mode(vcpu) == IN_GUEST_MODE;
5736199a 9953}
78646121
GN
9954
9955int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu)
9956{
9957 return kvm_x86_ops->interrupt_allowed(vcpu);
9958}
229456fc 9959
82b32774 9960unsigned long kvm_get_linear_rip(struct kvm_vcpu *vcpu)
f92653ee 9961{
82b32774
NA
9962 if (is_64_bit_mode(vcpu))
9963 return kvm_rip_read(vcpu);
9964 return (u32)(get_segment_base(vcpu, VCPU_SREG_CS) +
9965 kvm_rip_read(vcpu));
9966}
9967EXPORT_SYMBOL_GPL(kvm_get_linear_rip);
f92653ee 9968
82b32774
NA
9969bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip)
9970{
9971 return kvm_get_linear_rip(vcpu) == linear_rip;
f92653ee
JK
9972}
9973EXPORT_SYMBOL_GPL(kvm_is_linear_rip);
9974
94fe45da
JK
9975unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu)
9976{
9977 unsigned long rflags;
9978
9979 rflags = kvm_x86_ops->get_rflags(vcpu);
9980 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP)
c310bac5 9981 rflags &= ~X86_EFLAGS_TF;
94fe45da
JK
9982 return rflags;
9983}
9984EXPORT_SYMBOL_GPL(kvm_get_rflags);
9985
6addfc42 9986static void __kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
94fe45da
JK
9987{
9988 if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP &&
f92653ee 9989 kvm_is_linear_rip(vcpu, vcpu->arch.singlestep_rip))
c310bac5 9990 rflags |= X86_EFLAGS_TF;
94fe45da 9991 kvm_x86_ops->set_rflags(vcpu, rflags);
6addfc42
PB
9992}
9993
9994void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
9995{
9996 __kvm_set_rflags(vcpu, rflags);
3842d135 9997 kvm_make_request(KVM_REQ_EVENT, vcpu);
94fe45da
JK
9998}
9999EXPORT_SYMBOL_GPL(kvm_set_rflags);
10000
56028d08
GN
10001void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu, struct kvm_async_pf *work)
10002{
10003 int r;
10004
44dd3ffa 10005 if ((vcpu->arch.mmu->direct_map != work->arch.direct_map) ||
f2e10669 10006 work->wakeup_all)
56028d08
GN
10007 return;
10008
10009 r = kvm_mmu_reload(vcpu);
10010 if (unlikely(r))
10011 return;
10012
44dd3ffa
VK
10013 if (!vcpu->arch.mmu->direct_map &&
10014 work->arch.cr3 != vcpu->arch.mmu->get_cr3(vcpu))
fb67e14f
XG
10015 return;
10016
44dd3ffa 10017 vcpu->arch.mmu->page_fault(vcpu, work->gva, 0, true);
56028d08
GN
10018}
10019
af585b92
GN
10020static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
10021{
10022 return hash_32(gfn & 0xffffffff, order_base_2(ASYNC_PF_PER_VCPU));
10023}
10024
10025static inline u32 kvm_async_pf_next_probe(u32 key)
10026{
10027 return (key + 1) & (roundup_pow_of_two(ASYNC_PF_PER_VCPU) - 1);
10028}
10029
10030static void kvm_add_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10031{
10032 u32 key = kvm_async_pf_hash_fn(gfn);
10033
10034 while (vcpu->arch.apf.gfns[key] != ~0)
10035 key = kvm_async_pf_next_probe(key);
10036
10037 vcpu->arch.apf.gfns[key] = gfn;
10038}
10039
10040static u32 kvm_async_pf_gfn_slot(struct kvm_vcpu *vcpu, gfn_t gfn)
10041{
10042 int i;
10043 u32 key = kvm_async_pf_hash_fn(gfn);
10044
10045 for (i = 0; i < roundup_pow_of_two(ASYNC_PF_PER_VCPU) &&
c7d28c24
XG
10046 (vcpu->arch.apf.gfns[key] != gfn &&
10047 vcpu->arch.apf.gfns[key] != ~0); i++)
af585b92
GN
10048 key = kvm_async_pf_next_probe(key);
10049
10050 return key;
10051}
10052
10053bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10054{
10055 return vcpu->arch.apf.gfns[kvm_async_pf_gfn_slot(vcpu, gfn)] == gfn;
10056}
10057
10058static void kvm_del_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn)
10059{
10060 u32 i, j, k;
10061
10062 i = j = kvm_async_pf_gfn_slot(vcpu, gfn);
10063 while (true) {
10064 vcpu->arch.apf.gfns[i] = ~0;
10065 do {
10066 j = kvm_async_pf_next_probe(j);
10067 if (vcpu->arch.apf.gfns[j] == ~0)
10068 return;
10069 k = kvm_async_pf_hash_fn(vcpu->arch.apf.gfns[j]);
10070 /*
10071 * k lies cyclically in ]i,j]
10072 * | i.k.j |
10073 * |....j i.k.| or |.k..j i...|
10074 */
10075 } while ((i <= j) ? (i < k && k <= j) : (i < k || k <= j));
10076 vcpu->arch.apf.gfns[i] = vcpu->arch.apf.gfns[j];
10077 i = j;
10078 }
10079}
10080
7c90705b
GN
10081static int apf_put_user(struct kvm_vcpu *vcpu, u32 val)
10082{
4e335d9e
PB
10083
10084 return kvm_write_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, &val,
10085 sizeof(val));
7c90705b
GN
10086}
10087
9a6e7c39
WL
10088static int apf_get_user(struct kvm_vcpu *vcpu, u32 *val)
10089{
10090
10091 return kvm_read_guest_cached(vcpu->kvm, &vcpu->arch.apf.data, val,
10092 sizeof(u32));
10093}
10094
1dfdb45e
PB
10095static bool kvm_can_deliver_async_pf(struct kvm_vcpu *vcpu)
10096{
10097 if (!vcpu->arch.apf.delivery_as_pf_vmexit && is_guest_mode(vcpu))
10098 return false;
10099
10100 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED) ||
10101 (vcpu->arch.apf.send_user_only &&
10102 kvm_x86_ops->get_cpl(vcpu) == 0))
10103 return false;
10104
10105 return true;
10106}
10107
10108bool kvm_can_do_async_pf(struct kvm_vcpu *vcpu)
10109{
10110 if (unlikely(!lapic_in_kernel(vcpu) ||
10111 kvm_event_needs_reinjection(vcpu) ||
10112 vcpu->arch.exception.pending))
10113 return false;
10114
10115 if (kvm_hlt_in_guest(vcpu->kvm) && !kvm_can_deliver_async_pf(vcpu))
10116 return false;
10117
10118 /*
10119 * If interrupts are off we cannot even use an artificial
10120 * halt state.
10121 */
10122 return kvm_x86_ops->interrupt_allowed(vcpu);
10123}
10124
af585b92
GN
10125void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
10126 struct kvm_async_pf *work)
10127{
6389ee94
AK
10128 struct x86_exception fault;
10129
7c90705b 10130 trace_kvm_async_pf_not_present(work->arch.token, work->gva);
af585b92 10131 kvm_add_async_pf_gfn(vcpu, work->arch.gfn);
7c90705b 10132
1dfdb45e
PB
10133 if (kvm_can_deliver_async_pf(vcpu) &&
10134 !apf_put_user(vcpu, KVM_PV_REASON_PAGE_NOT_PRESENT)) {
6389ee94
AK
10135 fault.vector = PF_VECTOR;
10136 fault.error_code_valid = true;
10137 fault.error_code = 0;
10138 fault.nested_page_fault = false;
10139 fault.address = work->arch.token;
adfe20fb 10140 fault.async_page_fault = true;
6389ee94 10141 kvm_inject_page_fault(vcpu, &fault);
1dfdb45e
PB
10142 } else {
10143 /*
10144 * It is not possible to deliver a paravirtualized asynchronous
10145 * page fault, but putting the guest in an artificial halt state
10146 * can be beneficial nevertheless: if an interrupt arrives, we
10147 * can deliver it timely and perhaps the guest will schedule
10148 * another process. When the instruction that triggered a page
10149 * fault is retried, hopefully the page will be ready in the host.
10150 */
10151 kvm_make_request(KVM_REQ_APF_HALT, vcpu);
7c90705b 10152 }
af585b92
GN
10153}
10154
10155void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
10156 struct kvm_async_pf *work)
10157{
6389ee94 10158 struct x86_exception fault;
9a6e7c39 10159 u32 val;
6389ee94 10160
f2e10669 10161 if (work->wakeup_all)
7c90705b
GN
10162 work->arch.token = ~0; /* broadcast wakeup */
10163 else
10164 kvm_del_async_pf_gfn(vcpu, work->arch.gfn);
24dccf83 10165 trace_kvm_async_pf_ready(work->arch.token, work->gva);
7c90705b 10166
9a6e7c39
WL
10167 if (vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED &&
10168 !apf_get_user(vcpu, &val)) {
10169 if (val == KVM_PV_REASON_PAGE_NOT_PRESENT &&
10170 vcpu->arch.exception.pending &&
10171 vcpu->arch.exception.nr == PF_VECTOR &&
10172 !apf_put_user(vcpu, 0)) {
10173 vcpu->arch.exception.injected = false;
10174 vcpu->arch.exception.pending = false;
10175 vcpu->arch.exception.nr = 0;
10176 vcpu->arch.exception.has_error_code = false;
10177 vcpu->arch.exception.error_code = 0;
c851436a
JM
10178 vcpu->arch.exception.has_payload = false;
10179 vcpu->arch.exception.payload = 0;
9a6e7c39
WL
10180 } else if (!apf_put_user(vcpu, KVM_PV_REASON_PAGE_READY)) {
10181 fault.vector = PF_VECTOR;
10182 fault.error_code_valid = true;
10183 fault.error_code = 0;
10184 fault.nested_page_fault = false;
10185 fault.address = work->arch.token;
10186 fault.async_page_fault = true;
10187 kvm_inject_page_fault(vcpu, &fault);
10188 }
7c90705b 10189 }
e6d53e3b 10190 vcpu->arch.apf.halted = false;
a4fa1635 10191 vcpu->arch.mp_state = KVM_MP_STATE_RUNNABLE;
7c90705b
GN
10192}
10193
10194bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu)
10195{
10196 if (!(vcpu->arch.apf.msr_val & KVM_ASYNC_PF_ENABLED))
10197 return true;
10198 else
9bc1f09f 10199 return kvm_can_do_async_pf(vcpu);
af585b92
GN
10200}
10201
5544eb9b
PB
10202void kvm_arch_start_assignment(struct kvm *kvm)
10203{
10204 atomic_inc(&kvm->arch.assigned_device_count);
10205}
10206EXPORT_SYMBOL_GPL(kvm_arch_start_assignment);
10207
10208void kvm_arch_end_assignment(struct kvm *kvm)
10209{
10210 atomic_dec(&kvm->arch.assigned_device_count);
10211}
10212EXPORT_SYMBOL_GPL(kvm_arch_end_assignment);
10213
10214bool kvm_arch_has_assigned_device(struct kvm *kvm)
10215{
10216 return atomic_read(&kvm->arch.assigned_device_count);
10217}
10218EXPORT_SYMBOL_GPL(kvm_arch_has_assigned_device);
10219
e0f0bbc5
AW
10220void kvm_arch_register_noncoherent_dma(struct kvm *kvm)
10221{
10222 atomic_inc(&kvm->arch.noncoherent_dma_count);
10223}
10224EXPORT_SYMBOL_GPL(kvm_arch_register_noncoherent_dma);
10225
10226void kvm_arch_unregister_noncoherent_dma(struct kvm *kvm)
10227{
10228 atomic_dec(&kvm->arch.noncoherent_dma_count);
10229}
10230EXPORT_SYMBOL_GPL(kvm_arch_unregister_noncoherent_dma);
10231
10232bool kvm_arch_has_noncoherent_dma(struct kvm *kvm)
10233{
10234 return atomic_read(&kvm->arch.noncoherent_dma_count);
10235}
10236EXPORT_SYMBOL_GPL(kvm_arch_has_noncoherent_dma);
10237
14717e20
AW
10238bool kvm_arch_has_irq_bypass(void)
10239{
92735b1b 10240 return true;
14717e20
AW
10241}
10242
87276880
FW
10243int kvm_arch_irq_bypass_add_producer(struct irq_bypass_consumer *cons,
10244 struct irq_bypass_producer *prod)
10245{
10246 struct kvm_kernel_irqfd *irqfd =
10247 container_of(cons, struct kvm_kernel_irqfd, consumer);
10248
14717e20 10249 irqfd->producer = prod;
87276880 10250
14717e20
AW
10251 return kvm_x86_ops->update_pi_irte(irqfd->kvm,
10252 prod->irq, irqfd->gsi, 1);
87276880
FW
10253}
10254
10255void kvm_arch_irq_bypass_del_producer(struct irq_bypass_consumer *cons,
10256 struct irq_bypass_producer *prod)
10257{
10258 int ret;
10259 struct kvm_kernel_irqfd *irqfd =
10260 container_of(cons, struct kvm_kernel_irqfd, consumer);
10261
87276880
FW
10262 WARN_ON(irqfd->producer != prod);
10263 irqfd->producer = NULL;
10264
10265 /*
10266 * When producer of consumer is unregistered, we change back to
10267 * remapped mode, so we can re-use the current implementation
bb3541f1 10268 * when the irq is masked/disabled or the consumer side (KVM
87276880
FW
10269 * int this case doesn't want to receive the interrupts.
10270 */
10271 ret = kvm_x86_ops->update_pi_irte(irqfd->kvm, prod->irq, irqfd->gsi, 0);
10272 if (ret)
10273 printk(KERN_INFO "irq bypass consumer (token %p) unregistration"
10274 " fails: %d\n", irqfd->consumer.token, ret);
10275}
10276
10277int kvm_arch_update_irqfd_routing(struct kvm *kvm, unsigned int host_irq,
10278 uint32_t guest_irq, bool set)
10279{
87276880
FW
10280 return kvm_x86_ops->update_pi_irte(kvm, host_irq, guest_irq, set);
10281}
10282
52004014
FW
10283bool kvm_vector_hashing_enabled(void)
10284{
10285 return vector_hashing;
10286}
10287EXPORT_SYMBOL_GPL(kvm_vector_hashing_enabled);
10288
2d5ba19b
MT
10289bool kvm_arch_no_poll(struct kvm_vcpu *vcpu)
10290{
10291 return (vcpu->arch.msr_kvm_poll_control & 1) == 0;
10292}
10293EXPORT_SYMBOL_GPL(kvm_arch_no_poll);
10294
10295
229456fc 10296EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
931c33b1 10297EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
229456fc
MT
10298EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);
10299EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_page_fault);
10300EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_msr);
10301EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_cr);
0ac406de 10302EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmrun);
d8cabddf 10303EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit);
17897f36 10304EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmexit_inject);
236649de 10305EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intr_vmexit);
5497b955 10306EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_vmenter_failed);
ec1ff790 10307EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_invlpga);
532a46b9 10308EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_skinit);
2e554e8d 10309EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_nested_intercepts);
489223ed 10310EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_write_tsc_offset);
4f75bcc3 10311EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_ple_window_update);
843e4330 10312EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pml_full);
efc64404 10313EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_pi_irte_update);
18f40c53
SS
10314EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_unaccelerated_access);
10315EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_avic_incomplete_ipi);