kvm_host.h 41.6 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10
 * Kernel-based Virtual Machine driver for Linux
 *
 * This header defines architecture specific interfaces, x86 version
 *
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 * the COPYING file in the top-level directory.
 *
 */

11 12
#ifndef _ASM_X86_KVM_HOST_H
#define _ASM_X86_KVM_HOST_H
13

14 15
#include <linux/types.h>
#include <linux/mm.h>
16
#include <linux/mmu_notifier.h>
17
#include <linux/tracepoint.h>
18
#include <linux/cpumask.h>
19
#include <linux/irq_work.h>
20
#include <linux/irq.h>
21 22 23

#include <linux/kvm.h>
#include <linux/kvm_para.h>
24
#include <linux/kvm_types.h>
25
#include <linux/perf_event.h>
26 27
#include <linux/pvclock_gtod.h>
#include <linux/clocksource.h>
Feng Wu's avatar
Feng Wu committed
28
#include <linux/irqbypass.h>
29
#include <linux/hyperv.h>
30

31
#include <asm/apic.h>
32
#include <asm/pvclock-abi.h>
33
#include <asm/desc.h>
Sheng Yang's avatar
Sheng Yang committed
34
#include <asm/mtrr.h>
35
#include <asm/msr-index.h>
36
#include <asm/asm.h>
37
#include <asm/kvm_page_track.h>
38

39
#define KVM_MAX_VCPUS 288
40
#define KVM_SOFT_MAX_VCPUS 240
41
#define KVM_MAX_VCPU_ID 1023
42
#define KVM_USER_MEM_SLOTS 509
43 44
/* memory slots that are not exposed to userspace */
#define KVM_PRIVATE_MEM_SLOTS 3
45
#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
46

47
#define KVM_HALT_POLL_NS_DEFAULT 200000
48

49 50
#define KVM_IRQCHIP_NUM_PINS  KVM_IOAPIC_NUM_PINS

51
/* x86-specific vcpu->requests bit members */
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#define KVM_REQ_MIGRATE_TIMER		KVM_ARCH_REQ(0)
#define KVM_REQ_REPORT_TPR_ACCESS	KVM_ARCH_REQ(1)
#define KVM_REQ_TRIPLE_FAULT		KVM_ARCH_REQ(2)
#define KVM_REQ_MMU_SYNC		KVM_ARCH_REQ(3)
#define KVM_REQ_CLOCK_UPDATE		KVM_ARCH_REQ(4)
#define KVM_REQ_EVENT			KVM_ARCH_REQ(6)
#define KVM_REQ_APF_HALT		KVM_ARCH_REQ(7)
#define KVM_REQ_STEAL_UPDATE		KVM_ARCH_REQ(8)
#define KVM_REQ_NMI			KVM_ARCH_REQ(9)
#define KVM_REQ_PMU			KVM_ARCH_REQ(10)
#define KVM_REQ_PMI			KVM_ARCH_REQ(11)
#define KVM_REQ_SMI			KVM_ARCH_REQ(12)
#define KVM_REQ_MASTERCLOCK_UPDATE	KVM_ARCH_REQ(13)
#define KVM_REQ_MCLOCK_INPROGRESS \
	KVM_ARCH_REQ_FLAGS(14, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_SCAN_IOAPIC \
	KVM_ARCH_REQ_FLAGS(15, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_GLOBAL_CLOCK_UPDATE	KVM_ARCH_REQ(16)
#define KVM_REQ_APIC_PAGE_RELOAD \
	KVM_ARCH_REQ_FLAGS(17, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
#define KVM_REQ_HV_CRASH		KVM_ARCH_REQ(18)
#define KVM_REQ_IOAPIC_EOI_EXIT		KVM_ARCH_REQ(19)
#define KVM_REQ_HV_RESET		KVM_ARCH_REQ(20)
#define KVM_REQ_HV_EXIT			KVM_ARCH_REQ(21)
#define KVM_REQ_HV_STIMER		KVM_ARCH_REQ(22)
77

78 79 80 81 82
#define CR0_RESERVED_BITS                                               \
	(~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
			  | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
			  | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))

83
#define CR3_PCID_INVD		 BIT_64(63)
84 85 86
#define CR4_RESERVED_BITS                                               \
	(~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
			  | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE     \
87
			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
88
			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
89 90
			  | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
			  | X86_CR4_SMAP | X86_CR4_PKE))
91 92 93 94

#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)


95 96

#define INVALID_PAGE (~(hpa_t)0)
97 98
#define VALID_PAGE(x) ((x) != INVALID_PAGE)

99 100
#define UNMAPPED_GVA (~(gpa_t)0)

101
/* KVM Hugepage definitions for x86 */
102
#define KVM_NR_PAGE_SIZES	3
103 104
#define KVM_HPAGE_GFN_SHIFT(x)	(((x) - 1) * 9)
#define KVM_HPAGE_SHIFT(x)	(PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
105 106 107
#define KVM_HPAGE_SIZE(x)	(1UL << KVM_HPAGE_SHIFT(x))
#define KVM_HPAGE_MASK(x)	(~(KVM_HPAGE_SIZE(x) - 1))
#define KVM_PAGES_PER_HPAGE(x)	(KVM_HPAGE_SIZE(x) / PAGE_SIZE)
Marcelo Tosatti's avatar
Marcelo Tosatti committed
108

109 110 111 112 113 114 115
static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
{
	/* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
	return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
		(base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
}

116 117
#define KVM_PERMILLE_MMU_PAGES 20
#define KVM_MIN_ALLOC_MMU_PAGES 64
118
#define KVM_MMU_HASH_SHIFT 12
119
#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
120 121
#define KVM_MIN_FREE_MMU_PAGES 5
#define KVM_REFILL_PAGES 25
122
#define KVM_MAX_CPUID_ENTRIES 80
Sheng Yang's avatar
Sheng Yang committed
123
#define KVM_NR_FIXED_MTRR_REGION 88
124
#define KVM_NR_VAR_MTRR 8
125

126 127
#define ASYNC_PF_PER_VCPU 64

128
enum kvm_reg {
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	VCPU_REGS_RAX = 0,
	VCPU_REGS_RCX = 1,
	VCPU_REGS_RDX = 2,
	VCPU_REGS_RBX = 3,
	VCPU_REGS_RSP = 4,
	VCPU_REGS_RBP = 5,
	VCPU_REGS_RSI = 6,
	VCPU_REGS_RDI = 7,
#ifdef CONFIG_X86_64
	VCPU_REGS_R8 = 8,
	VCPU_REGS_R9 = 9,
	VCPU_REGS_R10 = 10,
	VCPU_REGS_R11 = 11,
	VCPU_REGS_R12 = 12,
	VCPU_REGS_R13 = 13,
	VCPU_REGS_R14 = 14,
	VCPU_REGS_R15 = 15,
#endif
147
	VCPU_REGS_RIP,
148 149 150
	NR_VCPU_REGS
};

Avi Kivity's avatar
Avi Kivity committed
151 152
enum kvm_reg_ex {
	VCPU_EXREG_PDPTR = NR_VCPU_REGS,
153
	VCPU_EXREG_CR3,
154
	VCPU_EXREG_RFLAGS,
155
	VCPU_EXREG_SEGMENTS,
Avi Kivity's avatar
Avi Kivity committed
156 157
};

158
enum {
159
	VCPU_SREG_ES,
160
	VCPU_SREG_CS,
161
	VCPU_SREG_SS,
162 163 164 165 166 167 168
	VCPU_SREG_DS,
	VCPU_SREG_FS,
	VCPU_SREG_GS,
	VCPU_SREG_TR,
	VCPU_SREG_LDTR,
};

169
#include <asm/kvm_emulate.h>
170

171 172
#define KVM_NR_MEM_OBJS 40

173 174 175 176
#define KVM_NR_DB_REGS	4

#define DR6_BD		(1 << 13)
#define DR6_BS		(1 << 14)
177
#define DR6_BT		(1 << 15)
178 179 180 181
#define DR6_RTM		(1 << 16)
#define DR6_FIXED_1	0xfffe0ff0
#define DR6_INIT	0xffff0ff0
#define DR6_VOLATILE	0x0001e00f
182 183 184 185 186

#define DR7_BP_EN_MASK	0x000000ff
#define DR7_GE		(1 << 9)
#define DR7_GD		(1 << 13)
#define DR7_FIXED_1	0x00000400
187
#define DR7_VOLATILE	0xffff2bff
188

189 190 191 192 193
#define PFERR_PRESENT_BIT 0
#define PFERR_WRITE_BIT 1
#define PFERR_USER_BIT 2
#define PFERR_RSVD_BIT 3
#define PFERR_FETCH_BIT 4
194
#define PFERR_PK_BIT 5
195 196
#define PFERR_GUEST_FINAL_BIT 32
#define PFERR_GUEST_PAGE_BIT 33
197 198 199 200 201 202

#define PFERR_PRESENT_MASK (1U << PFERR_PRESENT_BIT)
#define PFERR_WRITE_MASK (1U << PFERR_WRITE_BIT)
#define PFERR_USER_MASK (1U << PFERR_USER_BIT)
#define PFERR_RSVD_MASK (1U << PFERR_RSVD_BIT)
#define PFERR_FETCH_MASK (1U << PFERR_FETCH_BIT)
203
#define PFERR_PK_MASK (1U << PFERR_PK_BIT)
204 205 206 207 208 209
#define PFERR_GUEST_FINAL_MASK (1ULL << PFERR_GUEST_FINAL_BIT)
#define PFERR_GUEST_PAGE_MASK (1ULL << PFERR_GUEST_PAGE_BIT)

#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK |	\
				 PFERR_WRITE_MASK |		\
				 PFERR_PRESENT_MASK)
210

211 212 213 214 215 216 217
/*
 * The mask used to denote special SPTEs, which can be either MMIO SPTEs or
 * Access Tracking SPTEs. We use bit 62 instead of bit 63 to avoid conflicting
 * with the SVE bit in EPT PTEs.
 */
#define SPTE_SPECIAL_MASK (1ULL << 62)

218 219
/* apic attention bits */
#define KVM_APIC_CHECK_VAPIC	0
220 221 222 223 224 225 226
/*
 * The following bit is set with PV-EOI, unset on EOI.
 * We detect PV-EOI changes by guest by comparing
 * this bit with PV-EOI in guest memory.
 * See the implementation in apic_update_pv_eoi.
 */
#define KVM_APIC_PV_EOI_PENDING	1
227

Feng Wu's avatar
Feng Wu committed
228 229
struct kvm_kernel_irq_routing_entry;

230 231 232 233 234 235 236 237 238
/*
 * We don't want allocation failures within the mmu code, so we preallocate
 * enough memory for a single page fault in a cache.
 */
struct kvm_mmu_memory_cache {
	int nobjs;
	void *objects[KVM_NR_MEM_OBJS];
};

239 240 241 242 243 244 245 246
/*
 * the pages used as guest page table on soft mmu are tracked by
 * kvm_memory_slot.arch.gfn_track which is 16 bits, so the role bits used
 * by indirect shadow page can not be more than 15 bits.
 *
 * Currently, we used 14 bits that are @level, @cr4_pae, @quadrant, @access,
 * @nxe, @cr0_wp, @smep_andnot_wp and @smap_andnot_wp.
 */
247 248 249
union kvm_mmu_page_role {
	unsigned word;
	struct {
250
		unsigned level:4;
251
		unsigned cr4_pae:1;
252
		unsigned quadrant:2;
253
		unsigned direct:1;
254
		unsigned access:3;
255
		unsigned invalid:1;
256
		unsigned nxe:1;
257
		unsigned cr0_wp:1;
258
		unsigned smep_andnot_wp:1;
259
		unsigned smap_andnot_wp:1;
260 261
		unsigned ad_disabled:1;
		unsigned :7;
262 263 264 265 266 267 268 269

		/*
		 * This is left at the top of the word so that
		 * kvm_memslots_for_spte_role can extract it with a
		 * simple shift.  While there is room, give it a whole
		 * byte so it is also faster to load it from memory.
		 */
		unsigned smm:8;
270 271 272
	};
};

273 274 275 276
struct kvm_rmap_head {
	unsigned long val;
};

277 278 279 280 281 282 283 284 285 286 287 288 289 290
struct kvm_mmu_page {
	struct list_head link;
	struct hlist_node hash_link;

	/*
	 * The following two entries are used to key the shadow page in the
	 * hash table.
	 */
	gfn_t gfn;
	union kvm_mmu_page_role role;

	u64 *spt;
	/* hold the gfn of each spte inside spt */
	gfn_t *gfns;
291
	bool unsync;
292
	int root_count;          /* Currently serving as active root */
293
	unsigned int unsync_children;
294
	struct kvm_rmap_head parent_ptes; /* rmap pointers to parent sptes */
295 296

	/* The page is obsolete if mmu_valid_gen != kvm->arch.mmu_valid_gen.  */
297
	unsigned long mmu_valid_gen;
298

299
	DECLARE_BITMAP(unsync_child_bitmap, 512);
300 301

#ifdef CONFIG_X86_32
302 303 304 305
	/*
	 * Used out of the mmu-lock to avoid reading spte values while an
	 * update is in progress; see the comments in __get_spte_lockless().
	 */
306 307 308
	int clear_spte_count;
#endif

309
	/* Number of writes since the last time traversal visited this page.  */
310
	atomic_t write_flooding_count;
311 312
};

313 314 315 316 317 318 319
struct kvm_pio_request {
	unsigned long count;
	int in;
	int port;
	int size;
};

320
#define PT64_ROOT_MAX_LEVEL 5
321

322
struct rsvd_bits_validate {
323
	u64 rsvd_bits_mask[2][PT64_ROOT_MAX_LEVEL];
324 325 326
	u64 bad_mt_xwr;
};

327
/*
328 329 330
 * x86 supports 4 paging modes (5-level 64-bit, 4-level 64-bit, 3-level 32-bit,
 * and 2-level 32-bit).  The kvm_mmu structure abstracts the details of the
 * current mmu mode.
331 332
 */
struct kvm_mmu {
333
	void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
334
	unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
335
	u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
336 337
	int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err,
			  bool prefault);
338 339
	void (*inject_page_fault)(struct kvm_vcpu *vcpu,
				  struct x86_exception *fault);
340
	gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
341
			    struct x86_exception *exception);
342 343
	gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
			       struct x86_exception *exception);
344
	int (*sync_page)(struct kvm_vcpu *vcpu,
345
			 struct kvm_mmu_page *sp);
Marcelo Tosatti's avatar
Marcelo Tosatti committed
346
	void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
347
	void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
348
			   u64 *spte, const void *pte);
349
	hpa_t root_hpa;
350
	union kvm_mmu_page_role base_role;
351 352 353
	u8 root_level;
	u8 shadow_root_level;
	u8 ept_ad;
354
	bool direct_map;
355

356 357 358 359 360 361 362
	/*
	 * Bitmap; bit set = permission fault
	 * Byte index: page fault error code [4:1]
	 * Bit index: pte permissions in ACC_* format
	 */
	u8 permissions[16];

363 364 365 366 367 368 369 370
	/*
	* The pkru_mask indicates if protection key checks are needed.  It
	* consists of 16 domains indexed by page fault error code bits [4:1],
	* with PFEC.RSVD replaced by ACC_USER_MASK from the page tables.
	* Each domain has 2 bits which are ANDed with AD and WD from PKRU.
	*/
	u32 pkru_mask;

371
	u64 *pae_root;
372
	u64 *lm_root;
373 374 375 376 377 378 379 380

	/*
	 * check zero bits on shadow page table entries, these
	 * bits include not only hardware reserved bits but also
	 * the bits spte never used.
	 */
	struct rsvd_bits_validate shadow_zero_check;

381
	struct rsvd_bits_validate guest_rsvd_check;
382

383 384
	/* Can have large pages at levels 2..last_nonleaf_level-1. */
	u8 last_nonleaf_level;
Avi Kivity's avatar
Avi Kivity committed
385

386 387
	bool nx;

388
	u64 pdptrs[4]; /* pae */
389 390
};

391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
enum pmc_type {
	KVM_PMC_GP = 0,
	KVM_PMC_FIXED,
};

struct kvm_pmc {
	enum pmc_type type;
	u8 idx;
	u64 counter;
	u64 eventsel;
	struct perf_event *perf_event;
	struct kvm_vcpu *vcpu;
};

struct kvm_pmu {
	unsigned nr_arch_gp_counters;
	unsigned nr_arch_fixed_counters;
	unsigned available_event_types;
	u64 fixed_ctr_ctrl;
	u64 global_ctrl;
	u64 global_status;
	u64 global_ovf_ctrl;
	u64 counter_bitmask[2];
	u64 global_ctrl_mask;
415
	u64 reserved_bits;
416
	u8 version;
417 418
	struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
	struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
419 420 421 422
	struct irq_work irq_work;
	u64 reprogram_pmi;
};

423 424
struct kvm_pmu_ops;

425 426
enum {
	KVM_DEBUGREG_BP_ENABLED = 1,
427
	KVM_DEBUGREG_WONT_EXIT = 2,
428
	KVM_DEBUGREG_RELOAD = 4,
429 430
};

431 432 433
struct kvm_mtrr_range {
	u64 base;
	u64 mask;
434
	struct list_head node;
435 436
};

437
struct kvm_mtrr {
438
	struct kvm_mtrr_range var_ranges[KVM_NR_VAR_MTRR];
439
	mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
440
	u64 deftype;
441 442

	struct list_head head;
443 444
};

445 446 447 448 449 450 451 452 453 454 455
/* Hyper-V SynIC timer */
struct kvm_vcpu_hv_stimer {
	struct hrtimer timer;
	int index;
	u64 config;
	u64 count;
	u64 exp_time;
	struct hv_message msg;
	bool msg_pending;
};

456 457 458 459 460 461 462 463 464 465 466
/* Hyper-V synthetic interrupt controller (SynIC)*/
struct kvm_vcpu_hv_synic {
	u64 version;
	u64 control;
	u64 msg_page;
	u64 evt_page;
	atomic64_t sint[HV_SYNIC_SINT_COUNT];
	atomic_t sint_to_gsi[HV_SYNIC_SINT_COUNT];
	DECLARE_BITMAP(auto_eoi_bitmap, 256);
	DECLARE_BITMAP(vec_bitmap, 256);
	bool active;
467
	bool dont_zero_synic_pages;
468 469
};

470 471
/* Hyper-V per vcpu emulation context */
struct kvm_vcpu_hv {
472
	u32 vp_index;
473
	u64 hv_vapic;
474
	s64 runtime_offset;
475
	struct kvm_vcpu_hv_synic synic;
Andrey Smetanin's avatar
Andrey Smetanin committed
476
	struct kvm_hyperv_exit exit;
477 478
	struct kvm_vcpu_hv_stimer stimer[HV_SYNIC_STIMER_COUNT];
	DECLARE_BITMAP(stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
479 480
};

481
struct kvm_vcpu_arch {
482 483 484 485 486 487 488
	/*
	 * rip and regs accesses must go through
	 * kvm_{register,rip}_{read,write} functions.
	 */
	unsigned long regs[NR_VCPU_REGS];
	u32 regs_avail;
	u32 regs_dirty;
489 490

	unsigned long cr0;
491
	unsigned long cr0_guest_owned_bits;
492 493 494
	unsigned long cr2;
	unsigned long cr3;
	unsigned long cr4;
495
	unsigned long cr4_guest_owned_bits;
496
	unsigned long cr8;
497
	u32 pkru;
498
	u32 hflags;
499
	u64 efer;
500 501
	u64 apic_base;
	struct kvm_lapic *apic;    /* kernel irqchip context */
502
	bool apicv_active;
503
	DECLARE_BITMAP(ioapic_handled_vectors, 256);
504
	unsigned long apic_attention;
505
	int32_t apic_arb_prio;
506 507
	int mp_state;
	u64 ia32_misc_enable_msr;
508
	u64 smbase;
509
	bool tpr_access_reporting;
510
	u64 ia32_xss;
511
	u64 microcode_version;
512

513 514 515 516 517 518 519
	/*
	 * Paging state of the vcpu
	 *
	 * If the vcpu runs in guest mode with two level paging this still saves
	 * the paging mode of the l1 guest. This context is always used to
	 * handle faults.
	 */
520
	struct kvm_mmu mmu;
521

522 523 524 525 526 527 528 529 530 531
	/*
	 * Paging state of an L2 guest (used for nested npt)
	 *
	 * This context will save all necessary information to walk page tables
	 * of the an L2 guest. This context is only initialized for page table
	 * walking and not for faulting since we never handle l2 page faults on
	 * the host.
	 */
	struct kvm_mmu nested_mmu;

532 533 534 535 536 537
	/*
	 * Pointer to the mmu context currently used for
	 * gva_to_gpa translations.
	 */
	struct kvm_mmu *walk_mmu;

538
	struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
539 540 541
	struct kvm_mmu_memory_cache mmu_page_cache;
	struct kvm_mmu_memory_cache mmu_page_header_cache;

542 543 544 545 546 547 548 549 550 551 552 553
	/*
	 * QEMU userspace and the guest each have their own FPU state.
	 * In vcpu_run, we switch between the user and guest FPU contexts.
	 * While running a VCPU, the VCPU thread will have the guest FPU
	 * context.
	 *
	 * Note that while the PKRU state lives inside the fpu registers,
	 * it is switched out separately at VMENTER and VMEXIT time. The
	 * "guest_fpu" state here contains the guest FPU context, with the
	 * host PRKU bits.
	 */
	struct fpu user_fpu;
Sheng Yang's avatar
Sheng Yang committed
554
	struct fpu guest_fpu;
555

556
	u64 xcr0;
557
	u64 guest_supported_xcr0;
558
	u32 guest_xstate_size;
559 560 561 562

	struct kvm_pio_request pio;
	void *pio_data;

563 564
	u8 event_exit_inst_len;

565 566
	struct kvm_queued_exception {
		bool pending;
567
		bool injected;
568 569 570
		bool has_error_code;
		u8 nr;
		u32 error_code;
571
		u8 nested_apf;
572 573
	} exception;

574 575
	struct kvm_queued_interrupt {
		bool pending;
576
		bool soft;
577 578 579
		u8 nr;
	} interrupt;

580 581 582
	int halt_request; /* real mode on Intel only */

	int cpuid_nent;
583
	struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
584 585 586

	int maxphyaddr;

587 588 589
	/* emulate context */

	struct x86_emulate_ctxt emulate_ctxt;
590 591
	bool emulate_regs_need_sync_to_vcpu;
	bool emulate_regs_need_sync_from_vcpu;
592
	int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
593 594

	gpa_t time;
595
	struct pvclock_vcpu_time_info hv_clock;
Zachary Amsden's avatar
Zachary Amsden committed
596
	unsigned int hw_tsc_khz;
597 598
	struct gfn_to_hva_cache pv_time;
	bool pv_time_enabled;
599 600
	/* set guest stopped flag in pvclock flags field */
	bool pvclock_set_guest_stopped_request;
Glauber Costa's avatar
Glauber Costa committed
601 602 603 604 605 606 607 608

	struct {
		u64 msr_val;
		u64 last_steal;
		struct gfn_to_hva_cache stime;
		struct kvm_steal_time steal;
	} st;

609
	u64 tsc_offset;
610
	u64 last_guest_tsc;
611
	u64 last_host_tsc;
612
	u64 tsc_offset_adjustment;
613 614
	u64 this_tsc_nsec;
	u64 this_tsc_write;
Tomasz Grabiec's avatar
Tomasz Grabiec committed
615
	u64 this_tsc_generation;
Zachary Amsden's avatar
Zachary Amsden committed
616
	bool tsc_catchup;
617 618 619 620
	bool tsc_always_catchup;
	s8 virtual_tsc_shift;
	u32 virtual_tsc_mult;
	u32 virtual_tsc_khz;
621
	s64 ia32_tsc_adjust_msr;
622
	u64 tsc_scaling_ratio;
623

Avi Kivity's avatar
Avi Kivity committed
624 625 626
	atomic_t nmi_queued;  /* unprocessed asynchronous NMIs */
	unsigned nmi_pending; /* NMI queued after currently running handler */
	bool nmi_injected;    /* Trying to inject an NMI this entry */
627
	bool smi_pending;    /* SMI queued after currently running handler */
Avi Kivity's avatar
Avi Kivity committed
628

629
	struct kvm_mtrr mtrr_state;
630
	u64 pat;
631

632
	unsigned switch_db_regs;
633 634 635 636
	unsigned long db[KVM_NR_DB_REGS];
	unsigned long dr6;
	unsigned long dr7;
	unsigned long eff_db[KVM_NR_DB_REGS];
637
	unsigned long guest_debug_dr7;
638 639
	u64 msr_platform_info;
	u64 msr_misc_features_enables;
Huang Ying's avatar
Huang Ying committed
640 641 642 643

	u64 mcg_cap;
	u64 mcg_status;
	u64 mcg_ctl;
644
	u64 mcg_ext_ctl;
Huang Ying's avatar
Huang Ying committed
645
	u64 *mce_banks;
646

647 648 649 650
	/* Cache MMIO info */
	u64 mmio_gva;
	unsigned access;
	gfn_t mmio_gfn;
651
	u64 mmio_gen;
652

653 654
	struct kvm_pmu pmu;

655 656
	/* used for guest single stepping over the given code position */
	unsigned long singlestep_rip;
Jan Kiszka's avatar
Jan Kiszka committed
657

658
	struct kvm_vcpu_hv hyperv;
659 660

	cpumask_var_t wbinvd_dirty_mask;
661

662 663 664
	unsigned long last_retry_eip;
	unsigned long last_retry_addr;

665 666 667
	struct {
		bool halted;
		gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
668 669
		struct gfn_to_hva_cache data;
		u64 msr_val;
670
		u32 id;
671
		bool send_user_only;
672
		u32 host_apf_reason;
673
		unsigned long nested_apf_token;
674
		bool delivery_as_pf_vmexit;
675
	} apf;
676 677 678 679 680 681

	/* OSVW MSRs (AMD only) */
	struct {
		u64 length;
		u64 status;
	} osvw;
682 683 684 685 686

	struct {
		u64 msr_val;
		struct gfn_to_hva_cache data;
	} pv_eoi;
687 688 689 690 691 692 693

	/*
	 * Indicate whether the access faults on its page table in guest
	 * which is set when fix page fault and used to detect unhandeable
	 * instruction.
	 */
	bool write_fault_to_shadow_pgtable;
694 695 696

	/* set at EPT violation at this point */
	unsigned long exit_qualification;
697 698 699 700 701

	/* pv related host specific info */
	struct {
		bool pv_unhalted;
	} pv;
702 703

	int pending_ioapic_eoi;
704
	int pending_external_vector;
705

706
	/* GPA available */
707
	bool gpa_available;
708
	gpa_t gpa_val;
709 710 711

	/* be preempted when it's in kernel-mode(cpl=0) */
	bool preempted_in_kernel;
712 713 714

	/* Flush the L1 Data cache for L1TF mitigation on VMENTER */
	bool l1tf_flush_l1d;
715 716
};

717
struct kvm_lpage_info {
718
	int disallow_lpage;
719 720 721
};

struct kvm_arch_memory_slot {
722
	struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
723
	struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
724
	unsigned short *gfn_track[KVM_PAGE_TRACK_MAX];
725 726
};

727 728 729 730 731 732 733 734 735 736 737
/*
 * We use as the mode the number of bits allocated in the LDR for the
 * logical processor ID.  It happens that these are all powers of two.
 * This makes it is very easy to detect cases where the APICs are
 * configured for multiple modes; in that case, we cannot use the map and
 * hence cannot use kvm_irq_delivery_to_apic_fast either.
 */
#define KVM_APIC_MODE_XAPIC_CLUSTER          4
#define KVM_APIC_MODE_XAPIC_FLAT             8
#define KVM_APIC_MODE_X2APIC                16

738 739
struct kvm_apic_map {
	struct rcu_head rcu;
740
	u8 mode;
741
	u32 max_apic_id;
742 743 744 745
	union {
		struct kvm_lapic *xapic_flat_map[8];
		struct kvm_lapic *xapic_cluster_map[16][4];
	};
746
	struct kvm_lapic *phys_map[];
747 748
};

749 750
/* Hyper-V emulation context */
struct kvm_hv {
751
	struct mutex hv_lock;
752 753 754
	u64 hv_guest_os_id;
	u64 hv_hypercall;
	u64 hv_tsc_page;
755 756 757 758

	/* Hyper-v based guest crash (NT kernel bugcheck) parameters */
	u64 hv_crash_param[HV_X64_MSR_CRASH_PARAMS];
	u64 hv_crash_ctl;
759 760

	HV_REFERENCE_TSC_PAGE tsc_ref;
761 762
};

763 764 765 766 767 768
enum kvm_irqchip_mode {
	KVM_IRQCHIP_NONE,
	KVM_IRQCHIP_KERNEL,       /* created with KVM_CREATE_IRQCHIP */
	KVM_IRQCHIP_SPLIT,        /* created with KVM_CAP_SPLIT_IRQCHIP */
};

769
struct kvm_arch {
770
	unsigned int n_used_mmu_pages;
771
	unsigned int n_requested_mmu_pages;
772
	unsigned int n_max_mmu_pages;
773
	unsigned int indirect_shadow_pages;
774
	unsigned long mmu_valid_gen;
775 776 777 778 779
	struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
	/*
	 * Hash table of struct kvm_mmu_page.
	 */
	struct list_head active_mmu_pages;
780
	struct list_head zapped_obsolete_pages;
781
	struct kvm_page_track_notifier_node mmu_sp_tracker;
782
	struct kvm_page_track_notifier_head track_notifier_head;
783

Ben-Ami Yassour's avatar
Ben-Ami Yassour committed
784
	struct list_head assigned_dev_head;
785
	struct iommu_domain *iommu_domain;
786
	bool iommu_noncoherent;
787 788
#define __KVM_HAVE_ARCH_NONCOHERENT_DMA
	atomic_t noncoherent_dma_count;
789 790
#define __KVM_HAVE_ARCH_ASSIGNED_DEVICE
	atomic_t assigned_device_count;
791 792
	struct kvm_pic *vpic;
	struct kvm_ioapic *vioapic;
Sheng Yang's avatar
Sheng Yang committed
793
	struct kvm_pit *vpit;
794
	atomic_t vapics_in_nmi_mode;
795 796
	struct mutex apic_map_lock;
	struct kvm_apic_map *apic_map;
797 798

	unsigned int tss_addr;
799
	bool apic_access_page_done;
800 801

	gpa_t wall_clock;
802 803

	bool ept_identity_pagetable_done;
804
	gpa_t ept_identity_map_addr;
805 806

	unsigned long irq_sources_bitmap;
807
	s64 kvmclock_offset;
808
	raw_spinlock_t tsc_write_lock;
809 810
	u64 last_tsc_nsec;
	u64 last_tsc_write;
811
	u32 last_tsc_khz;
812 813 814
	u64 cur_tsc_nsec;
	u64 cur_tsc_write;
	u64 cur_tsc_offset;
Tomasz Grabiec's avatar
Tomasz Grabiec committed
815
	u64 cur_tsc_generation;
816
	int nr_vcpus_matched_tsc;
Ed Swierk's avatar
Ed Swierk committed
817

818 819 820
	spinlock_t pvclock_gtod_sync_lock;
	bool use_master_clock;
	u64 master_kernel_ns;
821
	u64 master_cycle_now;
822
	struct delayed_work kvmclock_update_work;
823
	struct delayed_work kvmclock_sync_work;
824

Ed Swierk's avatar
Ed Swierk committed
825
	struct kvm_xen_hvm_config xen_hvm_config;
826

827 828 829
	/* reads protected by irq_srcu, writes by irq_lock */
	struct hlist_head mask_notifier_list;

830
	struct kvm_hv hyperv;
831 832 833 834

	#ifdef CONFIG_KVM_MMU_AUDIT
	int audit_point;
	#endif
835

836
	bool backwards_tsc_observed;
837
	bool boot_vcpu_runs_old_kvmclock;
838
	u32 bsp_vcpu_id;
839 840

	u64 disabled_quirks;
841

842
	enum kvm_irqchip_mode irqchip_mode;
843
	u8 nr_reserved_ioapic_pins;
844 845

	bool disabled_lapic_found;
846 847

	/* Struct members for AVIC */
848
	u32 avic_vm_id;
849
	u32 ldr_mode;
850 851
	struct page *avic_logical_id_table_page;
	struct page *avic_physical_id_table_page;
852
	struct hlist_node hnode;
853 854

	bool x2apic_format;
855
	bool x2apic_broadcast_quirk_disabled;
856 857
};

858
struct kvm_vm_stat {
859 860 861 862 863 864 865 866 867 868
	ulong mmu_shadow_zapped;
	ulong mmu_pte_write;
	ulong mmu_pte_updated;
	ulong mmu_pde_zapped;
	ulong mmu_flooded;
	ulong mmu_recycled;
	ulong mmu_cache_miss;
	ulong mmu_unsync;
	ulong remote_tlb_flush;
	ulong lpages;
869
	ulong max_mmu_page_hash_collisions;
870 871
};

872
struct kvm_vcpu_stat {
873 874 875 876 877 878 879 880 881 882 883
	u64 pf_fixed;
	u64 pf_guest;
	u64 tlb_flush;
	u64 invlpg;

	u64 exits;
	u64 io_exits;
	u64 mmio_exits;
	u64 signal_exits;
	u64 irq_window_exits;
	u64 nmi_window_exits;
884
	u64 l1d_flush;
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
	u64 halt_exits;
	u64 halt_successful_poll;
	u64 halt_attempted_poll;
	u64 halt_poll_invalid;
	u64 halt_wakeup;
	u64 request_irq_exits;
	u64 irq_exits;
	u64 host_state_reload;
	u64 efer_reload;
	u64 fpu_reload;
	u64 insn_emulation;
	u64 insn_emulation_fail;
	u64 hypercalls;
	u64 irq_injections;
	u64 nmi_injections;
900
	u64 req_event;
901
};
902

903 904
struct x86_instruction_info;

905 906 907 908 909 910
struct msr_data {
	bool host_initiated;
	u32 index;
	u64 data;
};

911 912
struct kvm_lapic_irq {
	u32 vector;
913 914 915 916
	u16 delivery_mode;
	u16 dest_mode;
	bool level;
	u16 trig_mode;
917 918
	u32 shorthand;
	u32 dest_id;
919
	bool msi_redir_hint;
920 921
};

922 923 924
struct kvm_x86_ops {
	int (*cpu_has_kvm_support)(void);          /* __init */
	int (*disabled_by_bios)(void);             /* __init */
925 926
	int (*hardware_enable)(void);
	void (*hardware_disable)(void);
927 928 929
	void (*check_processor_compatibility)(void *rtn);
	int (*hardware_setup)(void);               /* __init */
	void (*hardware_unsetup)(void);            /* __exit */
930
	bool (*cpu_has_accelerated_tpr)(void);
931
	bool (*has_emulated_msr)(int index);
932
	void (*cpuid_update)(struct kvm_vcpu *vcpu);
933

934 935 936
	int (*vm_init)(struct kvm *kvm);
	void (*vm_destroy)(struct kvm *kvm);

937 938 939
	/* Create, but do not attach this VCPU */
	struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
	void (*vcpu_free)(struct kvm_vcpu *vcpu);
940
	void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
941 942 943 944 945

	void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
	void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
	void (*vcpu_put)(struct kvm_vcpu *vcpu);

946
	void (*update_bp_intercept)(struct kvm_vcpu *vcpu);
947
	int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
948
	int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
949 950 951
	u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
	void (*get_segment)(struct kvm_vcpu *vcpu,
			    struct kvm_segment *var, int seg);
952
	int (*get_cpl)(struct kvm_vcpu *vcpu);
953 954 955
	void (*set_segment)(struct kvm_vcpu *vcpu,
			    struct kvm_segment *var, int seg);
	void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
956
	void (*decache_cr0_guest_bits)(struct kvm_vcpu *vcpu);
957
	void (*decache_cr3)(struct kvm_vcpu *vcpu);
958 959 960
	void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
	void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
	void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
961
	int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long</