kvm_host.h 41.4 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
21
22

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

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

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

46
#define KVM_HALT_POLL_NS_DEFAULT 200000
47

48
49
#define KVM_IRQCHIP_NUM_PINS  KVM_IOAPIC_NUM_PINS

50
/* x86-specific vcpu->requests bit members */
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#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)
76

77
78
79
80
81
#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))

82
#define CR3_PCID_INVD		 BIT_64(63)
83
84
85
#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     \
86
			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
87
			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
88
89
			  | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
			  | X86_CR4_SMAP | X86_CR4_PKE))
90
91
92
93

#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)


94
95

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

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

100
/* KVM Hugepage definitions for x86 */
101
#define KVM_NR_PAGE_SIZES	3
102
103
#define KVM_HPAGE_GFN_SHIFT(x)	(((x) - 1) * 9)
#define KVM_HPAGE_SHIFT(x)	(PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
104
105
106
#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
107

108
109
110
111
112
113
114
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));
}

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

125
126
#define ASYNC_PF_PER_VCPU 64

127
enum kvm_reg {
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	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
146
	VCPU_REGS_RIP,
147
148
149
	NR_VCPU_REGS
};

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

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

168
#include <asm/kvm_emulate.h>
169

170
171
#define KVM_NR_MEM_OBJS 40

172
173
174
175
#define KVM_NR_DB_REGS	4

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

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

187
188
189
190
191
#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
192
#define PFERR_PK_BIT 5
193
194
#define PFERR_GUEST_FINAL_BIT 32
#define PFERR_GUEST_PAGE_BIT 33
195
196
197
198
199
200

#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)
201
#define PFERR_PK_MASK (1U << PFERR_PK_BIT)
202
203
204
205
206
207
#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)
208

209
210
211
212
213
214
215
/*
 * 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)

216
217
/* apic attention bits */
#define KVM_APIC_CHECK_VAPIC	0
218
219
220
221
222
223
224
/*
 * 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
225

Feng Wu's avatar
Feng Wu committed
226
227
struct kvm_kernel_irq_routing_entry;

228
229
230
231
232
233
234
235
236
/*
 * 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];
};

237
238
239
240
241
242
243
244
/*
 * 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.
 */
245
246
247
union kvm_mmu_page_role {
	unsigned word;
	struct {
248
		unsigned level:4;
249
		unsigned cr4_pae:1;
250
		unsigned quadrant:2;
251
		unsigned direct:1;
252
		unsigned access:3;
253
		unsigned invalid:1;
254
		unsigned nxe:1;
255
		unsigned cr0_wp:1;
256
		unsigned smep_andnot_wp:1;
257
		unsigned smap_andnot_wp:1;
258
259
		unsigned ad_disabled:1;
		unsigned :7;
260
261
262
263
264
265
266
267

		/*
		 * 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;
268
269
270
	};
};

271
272
273
274
struct kvm_rmap_head {
	unsigned long val;
};

275
276
277
278
279
280
281
282
283
284
285
286
287
288
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;
289
	bool unsync;
290
	int root_count;          /* Currently serving as active root */
291
	unsigned int unsync_children;
292
	struct kvm_rmap_head parent_ptes; /* rmap pointers to parent sptes */
293
294

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

297
	DECLARE_BITMAP(unsync_child_bitmap, 512);
298
299

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

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

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

318
#define PT64_ROOT_MAX_LEVEL 5
319

320
struct rsvd_bits_validate {
321
	u64 rsvd_bits_mask[2][PT64_ROOT_MAX_LEVEL];
322
323
324
	u64 bad_mt_xwr;
};

325
/*
326
327
328
 * 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.
329
330
 */
struct kvm_mmu {
331
	void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
332
	unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
333
	u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
334
335
	int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err,
			  bool prefault);
336
337
	void (*inject_page_fault)(struct kvm_vcpu *vcpu,
				  struct x86_exception *fault);
338
	gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
339
			    struct x86_exception *exception);
340
341
	gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
			       struct x86_exception *exception);
342
	int (*sync_page)(struct kvm_vcpu *vcpu,
343
			 struct kvm_mmu_page *sp);
Marcelo Tosatti's avatar
Marcelo Tosatti committed
344
	void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
345
	void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
346
			   u64 *spte, const void *pte);
347
	hpa_t root_hpa;
348
	union kvm_mmu_page_role base_role;
349
350
351
	u8 root_level;
	u8 shadow_root_level;
	u8 ept_ad;
352
	bool direct_map;
353

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

361
362
363
364
365
366
367
368
	/*
	* 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;

369
	u64 *pae_root;
370
	u64 *lm_root;
371
372
373
374
375
376
377
378

	/*
	 * 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;

379
	struct rsvd_bits_validate guest_rsvd_check;
380

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

384
385
	bool nx;

386
	u64 pdptrs[4]; /* pae */
387
388
};

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
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;
413
	u64 reserved_bits;
414
	u8 version;
415
416
	struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
	struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
417
418
419
420
	struct irq_work irq_work;
	u64 reprogram_pmi;
};

421
422
struct kvm_pmu_ops;

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

429
430
431
struct kvm_mtrr_range {
	u64 base;
	u64 mask;
432
	struct list_head node;
433
434
};

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

	struct list_head head;
441
442
};

443
444
445
446
447
448
449
450
451
452
453
/* 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;
};

454
455
456
457
458
459
460
461
462
463
464
/* 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;
465
	bool dont_zero_synic_pages;
466
467
};

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

479
struct kvm_vcpu_arch {
480
481
482
483
484
485
486
	/*
	 * 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;
487
488

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

510
511
512
513
514
515
516
	/*
	 * 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.
	 */
517
	struct kvm_mmu mmu;
518

519
520
521
522
523
524
525
526
527
528
	/*
	 * 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;

529
530
531
532
533
534
	/*
	 * Pointer to the mmu context currently used for
	 * gva_to_gpa translations.
	 */
	struct kvm_mmu *walk_mmu;

535
	struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
536
537
538
	struct kvm_mmu_memory_cache mmu_page_cache;
	struct kvm_mmu_memory_cache mmu_page_header_cache;

Sheng Yang's avatar
Sheng Yang committed
539
	struct fpu guest_fpu;
540
	u64 xcr0;
541
	u64 guest_supported_xcr0;
542
	u32 guest_xstate_size;
543
544
545
546

	struct kvm_pio_request pio;
	void *pio_data;

547
548
	u8 event_exit_inst_len;

549
550
	struct kvm_queued_exception {
		bool pending;
551
		bool injected;
552
553
554
		bool has_error_code;
		u8 nr;
		u32 error_code;
555
		u8 nested_apf;
556
557
	} exception;

558
559
	struct kvm_queued_interrupt {
		bool pending;
560
		bool soft;
561
562
563
		u8 nr;
	} interrupt;

564
565
566
	int halt_request; /* real mode on Intel only */

	int cpuid_nent;
567
	struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
568
569
570

	int maxphyaddr;

571
572
573
	/* emulate context */

	struct x86_emulate_ctxt emulate_ctxt;
574
575
	bool emulate_regs_need_sync_to_vcpu;
	bool emulate_regs_need_sync_from_vcpu;
576
	int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
577
578

	gpa_t time;
579
	struct pvclock_vcpu_time_info hv_clock;
Zachary Amsden's avatar
Zachary Amsden committed
580
	unsigned int hw_tsc_khz;
581
582
	struct gfn_to_hva_cache pv_time;
	bool pv_time_enabled;
583
584
	/* set guest stopped flag in pvclock flags field */
	bool pvclock_set_guest_stopped_request;
Glauber Costa's avatar
Glauber Costa committed
585
586
587
588
589
590
591
592

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

593
	u64 tsc_offset;
594
	u64 last_guest_tsc;
595
	u64 last_host_tsc;
596
	u64 tsc_offset_adjustment;
597
598
	u64 this_tsc_nsec;
	u64 this_tsc_write;
Tomasz Grabiec's avatar
Tomasz Grabiec committed
599
	u64 this_tsc_generation;
Zachary Amsden's avatar
Zachary Amsden committed
600
	bool tsc_catchup;
601
602
603
604
	bool tsc_always_catchup;
	s8 virtual_tsc_shift;
	u32 virtual_tsc_mult;
	u32 virtual_tsc_khz;
605
	s64 ia32_tsc_adjust_msr;
606
	u64 tsc_scaling_ratio;
607

Avi Kivity's avatar
Avi Kivity committed
608
609
610
	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 */
611
	bool smi_pending;    /* SMI queued after currently running handler */
Avi Kivity's avatar
Avi Kivity committed
612

613
	struct kvm_mtrr mtrr_state;
614
	u64 pat;
615

616
	unsigned switch_db_regs;
617
618
619
620
	unsigned long db[KVM_NR_DB_REGS];
	unsigned long dr6;
	unsigned long dr7;
	unsigned long eff_db[KVM_NR_DB_REGS];
621
	unsigned long guest_debug_dr7;
622
623
	u64 msr_platform_info;
	u64 msr_misc_features_enables;
Huang Ying's avatar
Huang Ying committed
624
625
626
627

	u64 mcg_cap;
	u64 mcg_status;
	u64 mcg_ctl;
628
	u64 mcg_ext_ctl;
Huang Ying's avatar
Huang Ying committed
629
	u64 *mce_banks;
630

631
632
633
634
	/* Cache MMIO info */
	u64 mmio_gva;
	unsigned access;
	gfn_t mmio_gfn;
635
	u64 mmio_gen;
636

637
638
	struct kvm_pmu pmu;

639
640
	/* used for guest single stepping over the given code position */
	unsigned long singlestep_rip;
Jan Kiszka's avatar
Jan Kiszka committed
641

642
	struct kvm_vcpu_hv hyperv;
643
644

	cpumask_var_t wbinvd_dirty_mask;
645

646
647
648
	unsigned long last_retry_eip;
	unsigned long last_retry_addr;

649
650
651
	struct {
		bool halted;
		gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
652
653
		struct gfn_to_hva_cache data;
		u64 msr_val;
654
		u32 id;
655
		bool send_user_only;
656
		u32 host_apf_reason;
657
		unsigned long nested_apf_token;
658
		bool delivery_as_pf_vmexit;
659
	} apf;
660
661
662
663
664
665

	/* OSVW MSRs (AMD only) */
	struct {
		u64 length;
		u64 status;
	} osvw;
666
667
668
669
670

	struct {
		u64 msr_val;
		struct gfn_to_hva_cache data;
	} pv_eoi;
671
672
673
674
675
676
677

	/*
	 * 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;
678
679
680

	/* set at EPT violation at this point */
	unsigned long exit_qualification;
681
682
683
684
685

	/* pv related host specific info */
	struct {
		bool pv_unhalted;
	} pv;
686
687

	int pending_ioapic_eoi;
688
	int pending_external_vector;
689

690
	/* GPA available */
691
	bool gpa_available;
692
	gpa_t gpa_val;
693
694
695

	/* be preempted when it's in kernel-mode(cpl=0) */
	bool preempted_in_kernel;
696
697
};

698
struct kvm_lpage_info {
699
	int disallow_lpage;
700
701
702
};

struct kvm_arch_memory_slot {
703
	struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
704
	struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
705
	unsigned short *gfn_track[KVM_PAGE_TRACK_MAX];
706
707
};

708
709
710
711
712
713
714
715
716
717
718
/*
 * 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

719
720
struct kvm_apic_map {
	struct rcu_head rcu;
721
	u8 mode;
722
	u32 max_apic_id;
723
724
725
726
	union {
		struct kvm_lapic *xapic_flat_map[8];
		struct kvm_lapic *xapic_cluster_map[16][4];
	};
727
	struct kvm_lapic *phys_map[];
728
729
};

730
731
/* Hyper-V emulation context */
struct kvm_hv {
732
	struct mutex hv_lock;
733
734
735
	u64 hv_guest_os_id;
	u64 hv_hypercall;
	u64 hv_tsc_page;
736
737
738
739

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

	HV_REFERENCE_TSC_PAGE tsc_ref;
742
743
};

744
745
746
747
748
749
enum kvm_irqchip_mode {
	KVM_IRQCHIP_NONE,
	KVM_IRQCHIP_KERNEL,       /* created with KVM_CREATE_IRQCHIP */
	KVM_IRQCHIP_SPLIT,        /* created with KVM_CAP_SPLIT_IRQCHIP */
};

750
751
752
753
754
struct kvm_sev_info {
	bool active;		/* SEV enabled guest */
	unsigned int asid;	/* ASID used for this guest */
};

755
struct kvm_arch {
756
	unsigned int n_used_mmu_pages;
757
	unsigned int n_requested_mmu_pages;
758
	unsigned int n_max_mmu_pages;
759
	unsigned int indirect_shadow_pages;
760
	unsigned long mmu_valid_gen;
761
762
763
764
765
	struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
	/*
	 * Hash table of struct kvm_mmu_page.
	 */
	struct list_head active_mmu_pages;
766
	struct list_head zapped_obsolete_pages;
767
	struct kvm_page_track_notifier_node mmu_sp_tracker;
768
	struct kvm_page_track_notifier_head track_notifier_head;
769

Ben-Ami Yassour's avatar
Ben-Ami Yassour committed
770
	struct list_head assigned_dev_head;
771
	struct iommu_domain *iommu_domain;
772
	bool iommu_noncoherent;
773
774
#define __KVM_HAVE_ARCH_NONCOHERENT_DMA
	atomic_t noncoherent_dma_count;
775
776
#define __KVM_HAVE_ARCH_ASSIGNED_DEVICE
	atomic_t assigned_device_count;
777
778
	struct kvm_pic *vpic;
	struct kvm_ioapic *vioapic;
Sheng Yang's avatar
Sheng Yang committed
779
	struct kvm_pit *vpit;
780
	atomic_t vapics_in_nmi_mode;
781
782
	struct mutex apic_map_lock;
	struct kvm_apic_map *apic_map;
783
784

	unsigned int tss_addr;
785
	bool apic_access_page_done;
786
787

	gpa_t wall_clock;
788
789

	bool ept_identity_pagetable_done;
790
	gpa_t ept_identity_map_addr;
791
792

	unsigned long irq_sources_bitmap;
793
	s64 kvmclock_offset;
794
	raw_spinlock_t tsc_write_lock;
795
796
	u64 last_tsc_nsec;
	u64 last_tsc_write;
797
	u32 last_tsc_khz;
798
799
800
	u64 cur_tsc_nsec;
	u64 cur_tsc_write;
	u64 cur_tsc_offset;
Tomasz Grabiec's avatar
Tomasz Grabiec committed
801
	u64 cur_tsc_generation;
802
	int nr_vcpus_matched_tsc;
Ed Swierk's avatar
Ed Swierk committed
803

804
805
806
	spinlock_t pvclock_gtod_sync_lock;
	bool use_master_clock;
	u64 master_kernel_ns;
807
	u64 master_cycle_now;
808
	struct delayed_work kvmclock_update_work;
809
	struct delayed_work kvmclock_sync_work;
810

Ed Swierk's avatar
Ed Swierk committed
811
	struct kvm_xen_hvm_config xen_hvm_config;
812

813
814
815
	/* reads protected by irq_srcu, writes by irq_lock */
	struct hlist_head mask_notifier_list;

816
	struct kvm_hv hyperv;
817
818
819
820

	#ifdef CONFIG_KVM_MMU_AUDIT
	int audit_point;
	#endif
821

822
	bool backwards_tsc_observed;
823
	bool boot_vcpu_runs_old_kvmclock;
824
	u32 bsp_vcpu_id;
825
826

	u64 disabled_quirks;
827

828
	enum kvm_irqchip_mode irqchip_mode;
829
	u8 nr_reserved_ioapic_pins;
830
831

	bool disabled_lapic_found;
832
833

	/* Struct members for AVIC */
834
	u32 avic_vm_id;
835
	u32 ldr_mode;
836
837
	struct page *avic_logical_id_table_page;
	struct page *avic_physical_id_table_page;
838
	struct hlist_node hnode;
839
840

	bool x2apic_format;
841
	bool x2apic_broadcast_quirk_disabled;
842
843

	struct kvm_sev_info sev_info;
844
845
};

846
struct kvm_vm_stat {
847
848
849
850
851
852
853
854
855
856
	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;
857
	ulong max_mmu_page_hash_collisions;
858
859
};

860
struct kvm_vcpu_stat {
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
	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;
	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;
887
	u64 req_event;
888
};
889

890
891
struct x86_instruction_info;

892
893
894
895
896
897
struct msr_data {
	bool host_initiated;
	u32 index;
	u64 data;
};

898
899
struct kvm_lapic_irq {
	u32 vector;
900
901
902
903
	u16 delivery_mode;
	u16 dest_mode;
	bool level;
	u16 trig_mode;
904
905
	u32 shorthand;
	u32 dest_id;
906
	bool msi_redir_hint;
907
908
};

909
910
911
struct kvm_x86_ops {
	int (*cpu_has_kvm_support)(void);          /* __init */
	int (*disabled_by_bios)(void);             /* __init */
912
913
	int (*hardware_enable)(void);
	void (*hardware_disable)(void);
914
915
916
	void (*check_processor_compatibility)(void *rtn);
	int (*hardware_setup)(void);               /* __init */
	void (*hardware_unsetup)(void);            /* __exit */
917
	bool (*cpu_has_accelerated_tpr)(void);
918
	bool (*cpu_has_high_real_mode_segbase)(void);
919
	void (*cpuid_update)(struct kvm_vcpu *vcpu);
920

921
922
923
	int (*vm_init)(struct kvm *kvm);
	void (*vm_destroy)(struct kvm *kvm);

924
925
926
	/* Create, but do not attach this VCPU */
	struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
	void (*vcpu_free)(struct kvm_vcpu *vcpu);
927
	void (*vcpu_reset)(struct kvm_vcpu *vcpu, bool init_event);
928
929
930
931
932

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

933
	void (*update_bp_intercept)(struct kvm_vcpu *vcpu);
934
	int (*get_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
935
	int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
936
937
938
	u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
	void (*get_segment)(struct kvm_vcpu *vcpu,
			    struct kvm_segment *var, int seg);
939
	int (*get_cpl)(struct kvm_vcpu *vcpu);
940
941
942
	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);
943
	void (*decache_cr0_guest_bits)(struct kvm_vcpu *vcpu);
944
	void (*decache_cr3)(struct kvm_vcpu *vcpu);
945
946
947
	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);
948
	int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
949
	void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
950
951
952
953
	void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
	void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
	void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
	void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
Jan Kiszka's avatar
Jan Kiszka committed
954
955
	u64 (*get_dr6)(struct kvm_vcpu *vcpu);
	void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value);
956
	void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
957
	void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
958
	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
959
960
961
962
963
	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);

	void (*tlb_flush)(struct kvm_vcpu *vcpu);

Avi Kivity's avatar
Avi Kivity committed
964
965
	void (*run)(struct kvm_vcpu *vcpu);
	int (*handle_exit)(struct kvm_vcpu *vcpu);
966
	void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
967
	void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
968
	u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
969
970
	void (*patch_hypercall)(struct kvm_vcpu *vcpu,
				unsigned char *hypercall_addr);
971
	void (*set_irq)(struct kvm_vcpu *vcpu);
972
	void (*set_nmi)(struct kvm_vcpu *vcpu);
973
	void (*queue_exception)(struct kvm_vcpu *vcpu);
974
	void (*cancel_injection)(struct kvm_vcpu *vcpu);
975
	int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
976
	int (*nmi_allowed)(struct kvm_vcpu *vcpu);
977
978
	bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
	void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
979
980
	void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
	void (*enable_irq_window)(struct kvm_vcpu *vcpu);
981
	void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);