kvm_host.h 54.6 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0-only */
2
/*
3
4
5
6
7
 * Kernel-based Virtual Machine driver for Linux
 *
 * This header defines architecture specific interfaces, x86 version
 */

8
9
#ifndef _ASM_X86_KVM_HOST_H
#define _ASM_X86_KVM_HOST_H
10

11
12
#include <linux/types.h>
#include <linux/mm.h>
13
#include <linux/mmu_notifier.h>
14
#include <linux/tracepoint.h>
15
#include <linux/cpumask.h>
16
#include <linux/irq_work.h>
17
#include <linux/irq.h>
18
19
20

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

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

38
39
#define __KVM_HAVE_ARCH_VCPU_DEBUGFS

40
#define KVM_MAX_VCPUS 288
41
#define KVM_SOFT_MAX_VCPUS 240
42
#define KVM_MAX_VCPU_ID 1023
43
44
/* memory slots that are not exposed to userspace */
#define KVM_PRIVATE_MEM_SLOTS 3
45

46
#define KVM_HALT_POLL_NS_DEFAULT 200000
47

48
49
#define KVM_IRQCHIP_NUM_PINS  KVM_IOAPIC_NUM_PINS

50
51
52
#define KVM_DIRTY_LOG_MANUAL_CAPS   (KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE | \
					KVM_DIRTY_LOG_INITIALLY_SET)

53
54
55
#define KVM_BUS_LOCK_DETECTION_VALID_MODE	(KVM_BUS_LOCK_DETECTION_OFF | \
						 KVM_BUS_LOCK_DETECTION_EXIT)

56
/* x86-specific vcpu->requests bit members */
57
58
59
60
61
#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)
62
#define KVM_REQ_LOAD_MMU_PGD		KVM_ARCH_REQ(5)
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#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)
83
#define KVM_REQ_LOAD_EOI_EXITMAP	KVM_ARCH_REQ(23)
84
#define KVM_REQ_GET_NESTED_STATE_PAGES	KVM_ARCH_REQ(24)
85
86
#define KVM_REQ_APICV_UPDATE \
	KVM_ARCH_REQ_FLAGS(25, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
87
#define KVM_REQ_TLB_FLUSH_CURRENT	KVM_ARCH_REQ(26)
88
#define KVM_REQ_HV_TLB_FLUSH \
89
	KVM_ARCH_REQ_FLAGS(27, KVM_REQUEST_NO_WAKEUP)
90
#define KVM_REQ_APF_READY		KVM_ARCH_REQ(28)
91
#define KVM_REQ_MSR_FILTER_CHANGED	KVM_ARCH_REQ(29)
92
93
#define KVM_REQ_UPDATE_CPU_DIRTY_LOGGING \
	KVM_ARCH_REQ_FLAGS(30, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)
94

95
96
97
98
99
100
101
102
#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))

#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     \
103
			  | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
104
			  | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
105
			  | X86_CR4_OSXMMEXCPT | X86_CR4_LA57 | X86_CR4_VMXE \
Paolo Bonzini's avatar
Paolo Bonzini committed
106
			  | X86_CR4_SMAP | X86_CR4_PKE | X86_CR4_UMIP))
107
108
109
110

#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)


111
112

#define INVALID_PAGE (~(hpa_t)0)
113
114
#define VALID_PAGE(x) ((x) != INVALID_PAGE)

115
#define UNMAPPED_GVA (~(gpa_t)0)
116
#define INVALID_GPA (~(gpa_t)0)
117

118
/* KVM Hugepage definitions for x86 */
119
120
#define KVM_MAX_HUGEPAGE_LEVEL	PG_LEVEL_1G
#define KVM_NR_PAGE_SIZES	(KVM_MAX_HUGEPAGE_LEVEL - PG_LEVEL_4K + 1)
121
122
#define KVM_HPAGE_GFN_SHIFT(x)	(((x) - 1) * 9)
#define KVM_HPAGE_SHIFT(x)	(PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
123
124
125
#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
126

127
128
static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
{
129
	/* KVM_HPAGE_GFN_SHIFT(PG_LEVEL_4K) must be 0. */
130
131
132
133
	return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
		(base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
}

134
#define KVM_PERMILLE_MMU_PAGES 20
135
#define KVM_MIN_ALLOC_MMU_PAGES 64UL
136
#define KVM_MMU_HASH_SHIFT 12
137
#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
138
139
#define KVM_MIN_FREE_MMU_PAGES 5
#define KVM_REFILL_PAGES 25
140
#define KVM_MAX_CPUID_ENTRIES 256
Sheng Yang's avatar
Sheng Yang committed
141
#define KVM_NR_FIXED_MTRR_REGION 88
142
#define KVM_NR_VAR_MTRR 8
143

144
145
#define ASYNC_PF_PER_VCPU 64

146
enum kvm_reg {
147
148
149
150
151
152
153
154
	VCPU_REGS_RAX = __VCPU_REGS_RAX,
	VCPU_REGS_RCX = __VCPU_REGS_RCX,
	VCPU_REGS_RDX = __VCPU_REGS_RDX,
	VCPU_REGS_RBX = __VCPU_REGS_RBX,
	VCPU_REGS_RSP = __VCPU_REGS_RSP,
	VCPU_REGS_RBP = __VCPU_REGS_RBP,
	VCPU_REGS_RSI = __VCPU_REGS_RSI,
	VCPU_REGS_RDI = __VCPU_REGS_RDI,
155
#ifdef CONFIG_X86_64
156
157
158
159
160
161
162
163
	VCPU_REGS_R8  = __VCPU_REGS_R8,
	VCPU_REGS_R9  = __VCPU_REGS_R9,
	VCPU_REGS_R10 = __VCPU_REGS_R10,
	VCPU_REGS_R11 = __VCPU_REGS_R11,
	VCPU_REGS_R12 = __VCPU_REGS_R12,
	VCPU_REGS_R13 = __VCPU_REGS_R13,
	VCPU_REGS_R14 = __VCPU_REGS_R14,
	VCPU_REGS_R15 = __VCPU_REGS_R15,
164
#endif
165
	VCPU_REGS_RIP,
166
	NR_VCPU_REGS,
167

Avi Kivity's avatar
Avi Kivity committed
168
	VCPU_EXREG_PDPTR = NR_VCPU_REGS,
169
	VCPU_EXREG_CR0,
170
	VCPU_EXREG_CR3,
171
	VCPU_EXREG_CR4,
172
	VCPU_EXREG_RFLAGS,
173
	VCPU_EXREG_SEGMENTS,
174
	VCPU_EXREG_EXIT_INFO_1,
175
	VCPU_EXREG_EXIT_INFO_2,
Avi Kivity's avatar
Avi Kivity committed
176
177
};

178
enum {
179
	VCPU_SREG_ES,
180
	VCPU_SREG_CS,
181
	VCPU_SREG_SS,
182
183
184
185
186
187
188
	VCPU_SREG_DS,
	VCPU_SREG_FS,
	VCPU_SREG_GS,
	VCPU_SREG_TR,
	VCPU_SREG_LDTR,
};

189
190
enum exit_fastpath_completion {
	EXIT_FASTPATH_NONE,
191
192
	EXIT_FASTPATH_REENTER_GUEST,
	EXIT_FASTPATH_EXIT_HANDLED,
193
};
194
typedef enum exit_fastpath_completion fastpath_t;
195

196
197
198
199
struct x86_emulate_ctxt;
struct x86_exception;
enum x86_intercept;
enum x86_intercept_stage;
200

201
202
#define KVM_NR_DB_REGS	4

203
#define DR6_BUS_LOCK   (1 << 11)
204
205
#define DR6_BD		(1 << 13)
#define DR6_BS		(1 << 14)
206
#define DR6_BT		(1 << 15)
207
#define DR6_RTM		(1 << 16)
208
209
210
211
212
213
214
215
216
/*
 * DR6_ACTIVE_LOW combines fixed-1 and active-low bits.
 * We can regard all the bits in DR6_FIXED_1 as active_low bits;
 * they will never be 0 for now, but when they are defined
 * in the future it will require no code change.
 *
 * DR6_ACTIVE_LOW is also used as the init/reset value for DR6.
 */
#define DR6_ACTIVE_LOW	0xffff0ff0
217
#define DR6_VOLATILE	0x0001e80f
218
#define DR6_FIXED_1	(DR6_ACTIVE_LOW & ~DR6_VOLATILE)
219
220
221
222
223

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

226
227
228
229
230
231
232
233
234
#define KVM_GUESTDBG_VALID_MASK \
	(KVM_GUESTDBG_ENABLE | \
	KVM_GUESTDBG_SINGLESTEP | \
	KVM_GUESTDBG_USE_HW_BP | \
	KVM_GUESTDBG_USE_SW_BP | \
	KVM_GUESTDBG_INJECT_BP | \
	KVM_GUESTDBG_INJECT_DB)


235
236
237
238
239
#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
240
#define PFERR_PK_BIT 5
241
#define PFERR_SGX_BIT 15
242
243
#define PFERR_GUEST_FINAL_BIT 32
#define PFERR_GUEST_PAGE_BIT 33
244
245
246
247
248
249

#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)
250
#define PFERR_PK_MASK (1U << PFERR_PK_BIT)
251
#define PFERR_SGX_MASK (1U << PFERR_SGX_BIT)
252
253
254
255
256
257
#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)
258

259
260
/* apic attention bits */
#define KVM_APIC_CHECK_VAPIC	0
261
262
263
264
265
266
267
/*
 * 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
268

Feng Wu's avatar
Feng Wu committed
269
270
struct kvm_kernel_irq_routing_entry;

271
272
273
274
275
/*
 * 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.
 *
276
 * Currently, we used 14 bits that are @level, @gpte_is_8_bytes, @quadrant, @access,
277
278
 * @nxe, @cr0_wp, @smep_andnot_wp and @smap_andnot_wp.
 */
279
union kvm_mmu_page_role {
280
	u32 word;
281
	struct {
282
		unsigned level:4;
283
		unsigned gpte_is_8_bytes:1;
284
		unsigned quadrant:2;
285
		unsigned direct:1;
286
		unsigned access:3;
287
		unsigned invalid:1;
288
		unsigned nxe:1;
289
		unsigned cr0_wp:1;
290
		unsigned smep_andnot_wp:1;
291
		unsigned smap_andnot_wp:1;
292
		unsigned ad_disabled:1;
293
294
		unsigned guest_mode:1;
		unsigned :6;
295
296
297
298
299
300
301
302

		/*
		 * 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;
303
304
305
	};
};

306
union kvm_mmu_extended_role {
307
308
309
310
311
312
/*
 * This structure complements kvm_mmu_page_role caching everything needed for
 * MMU configuration. If nothing in both these structures changed, MMU
 * re-configuration can be skipped. @valid bit is set on first usage so we don't
 * treat all-zero structure as valid data.
 */
313
	u32 word;
314
315
316
	struct {
		unsigned int valid:1;
		unsigned int execonly:1;
317
		unsigned int cr0_pg:1;
318
		unsigned int cr4_pae:1;
319
320
321
322
		unsigned int cr4_pse:1;
		unsigned int cr4_pke:1;
		unsigned int cr4_smap:1;
		unsigned int cr4_smep:1;
323
		unsigned int maxphyaddr:6;
324
	};
325
326
327
328
329
330
331
332
333
334
};

union kvm_mmu_role {
	u64 as_u64;
	struct {
		union kvm_mmu_page_role base;
		union kvm_mmu_extended_role ext;
	};
};

335
336
337
338
struct kvm_rmap_head {
	unsigned long val;
};

339
struct kvm_pio_request {
340
	unsigned long linear_rip;
341
342
343
344
345
346
	unsigned long count;
	int in;
	int port;
	int size;
};

347
#define PT64_ROOT_MAX_LEVEL 5
348

349
struct rsvd_bits_validate {
350
	u64 rsvd_bits_mask[2][PT64_ROOT_MAX_LEVEL];
351
352
353
	u64 bad_mt_xwr;
};

354
struct kvm_mmu_root_info {
355
	gpa_t pgd;
356
357
358
359
	hpa_t hpa;
};

#define KVM_MMU_ROOT_INFO_INVALID \
360
	((struct kvm_mmu_root_info) { .pgd = INVALID_PAGE, .hpa = INVALID_PAGE })
361

362
363
#define KVM_MMU_NUM_PREV_ROOTS 3

364
365
#define KVM_HAVE_MMU_RWLOCK

366
367
struct kvm_mmu_page;

368
/*
369
370
371
 * 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.
372
373
 */
struct kvm_mmu {
374
	unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);
375
	u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
376
	int (*page_fault)(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u32 err,
377
			  bool prefault);
378
379
	void (*inject_page_fault)(struct kvm_vcpu *vcpu,
				  struct x86_exception *fault);
380
381
	gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gpa_t gva_or_gpa,
			    u32 access, struct x86_exception *exception);
382
383
	gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access,
			       struct x86_exception *exception);
384
	int (*sync_page)(struct kvm_vcpu *vcpu,
385
			 struct kvm_mmu_page *sp);
386
	void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva, hpa_t root_hpa);
387
	hpa_t root_hpa;
388
	gpa_t root_pgd;
389
	union kvm_mmu_role mmu_role;
390
391
392
	u8 root_level;
	u8 shadow_root_level;
	u8 ept_ad;
393
	bool direct_map;
394
	struct kvm_mmu_root_info prev_roots[KVM_MMU_NUM_PREV_ROOTS];
395

396
397
398
399
400
401
402
	/*
	 * Bitmap; bit set = permission fault
	 * Byte index: page fault error code [4:1]
	 * Bit index: pte permissions in ACC_* format
	 */
	u8 permissions[16];

403
404
405
406
407
408
409
410
	/*
	* 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;

411
	u64 *pae_root;
412
	u64 *pml4_root;
413
414
415
416
417
418
419
420

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

421
	struct rsvd_bits_validate guest_rsvd_check;
422

423
424
	/* Can have large pages at levels 2..last_nonleaf_level-1. */
	u8 last_nonleaf_level;
Avi Kivity's avatar
Avi Kivity committed
425

426
427
	bool nx;

428
	u64 pdptrs[4]; /* pae */
429
430
};

431
432
433
434
435
struct kvm_tlb_range {
	u64 start_gfn;
	u64 pages;
};

436
437
438
439
440
441
442
443
444
445
446
447
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;
448
449
450
451
452
	/*
	 * eventsel value for general purpose counters,
	 * ctrl value for fixed counters.
	 */
	u64 current_config;
453
454
455
456
457
458
459
460
461
462
463
464
};

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;
465
	u64 global_ovf_ctrl_mask;
466
	u64 reserved_bits;
467
	u8 version;
468
469
	struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
	struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
470
	struct irq_work irq_work;
471
	DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
472
473
474
475
476
477
478
479
480
481
482
483
484
485
	DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);

	/*
	 * The gate to release perf_events not marked in
	 * pmc_in_use only once in a vcpu time slice.
	 */
	bool need_cleanup;

	/*
	 * The total number of programmed perf_events and it helps to avoid
	 * redundant check before cleanup if guest don't use vPMU at all.
	 */
	u8 event_count;
486
487
};

488
489
struct kvm_pmu_ops;

490
491
enum {
	KVM_DEBUGREG_BP_ENABLED = 1,
492
	KVM_DEBUGREG_WONT_EXIT = 2,
493
	KVM_DEBUGREG_RELOAD = 4,
494
495
};

496
497
498
struct kvm_mtrr_range {
	u64 base;
	u64 mask;
499
	struct list_head node;
500
501
};

502
struct kvm_mtrr {
503
	struct kvm_mtrr_range var_ranges[KVM_NR_VAR_MTRR];
504
	mtrr_type fixed_ranges[KVM_NR_FIXED_MTRR_REGION];
505
	u64 deftype;
506
507

	struct list_head head;
508
509
};

510
511
512
513
/* Hyper-V SynIC timer */
struct kvm_vcpu_hv_stimer {
	struct hrtimer timer;
	int index;
514
	union hv_stimer_config config;
515
516
517
518
519
520
	u64 count;
	u64 exp_time;
	struct hv_message msg;
	bool msg_pending;
};

521
522
523
524
525
526
527
528
529
530
531
/* 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;
532
	bool dont_zero_synic_pages;
533
534
};

535
536
/* Hyper-V per vcpu emulation context */
struct kvm_vcpu_hv {
537
	struct kvm_vcpu *vcpu;
538
	u32 vp_index;
539
	u64 hv_vapic;
540
	s64 runtime_offset;
541
	struct kvm_vcpu_hv_synic synic;
Andrey Smetanin's avatar
Andrey Smetanin committed
542
	struct kvm_hyperv_exit exit;
543
544
	struct kvm_vcpu_hv_stimer stimer[HV_SYNIC_STIMER_COUNT];
	DECLARE_BITMAP(stimer_pending_bitmap, HV_SYNIC_STIMER_COUNT);
545
	cpumask_t tlb_flush;
546
547
};

548
549
550
/* Xen HVM per vcpu emulation context */
struct kvm_vcpu_xen {
	u64 hypercall_rip;
551
	u32 current_runstate;
552
	bool vcpu_info_set;
553
	bool vcpu_time_info_set;
554
	bool runstate_set;
555
	struct gfn_to_hva_cache vcpu_info_cache;
556
	struct gfn_to_hva_cache vcpu_time_info_cache;
557
558
559
560
	struct gfn_to_hva_cache runstate_cache;
	u64 last_steal;
	u64 runstate_entry_time;
	u64 runstate_times[4];
561
562
};

563
struct kvm_vcpu_arch {
564
565
566
567
568
569
570
	/*
	 * 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;
571
572

	unsigned long cr0;
573
	unsigned long cr0_guest_owned_bits;
574
575
576
	unsigned long cr2;
	unsigned long cr3;
	unsigned long cr4;
577
	unsigned long cr4_guest_owned_bits;
578
	unsigned long cr4_guest_rsvd_bits;
579
	unsigned long cr8;
580
	u32 host_pkru;
581
	u32 pkru;
582
	u32 hflags;
583
	u64 efer;
584
585
	u64 apic_base;
	struct kvm_lapic *apic;    /* kernel irqchip context */
586
	bool apicv_active;
587
	bool load_eoi_exitmap_pending;
588
	DECLARE_BITMAP(ioapic_handled_vectors, 256);
589
	unsigned long apic_attention;
590
	int32_t apic_arb_prio;
591
592
	int mp_state;
	u64 ia32_misc_enable_msr;
593
	u64 smbase;
594
	u64 smi_count;
595
	bool tpr_access_reporting;
596
	bool xsaves_enabled;
597
	u64 ia32_xss;
598
	u64 microcode_version;
599
	u64 arch_capabilities;
600
	u64 perf_capabilities;
601

602
603
604
605
606
607
608
	/*
	 * 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.
	 */
609
610
611
612
	struct kvm_mmu *mmu;

	/* Non-nested MMU for L1 */
	struct kvm_mmu root_mmu;
613

614
615
616
	/* L1 MMU when running nested */
	struct kvm_mmu guest_mmu;

617
618
619
620
	/*
	 * Paging state of an L2 guest (used for nested npt)
	 *
	 * This context will save all necessary information to walk page tables
Miaohe Lin's avatar
Miaohe Lin committed
621
	 * of an L2 guest. This context is only initialized for page table
622
623
624
625
626
	 * walking and not for faulting since we never handle l2 page faults on
	 * the host.
	 */
	struct kvm_mmu nested_mmu;

627
628
629
630
631
632
	/*
	 * Pointer to the mmu context currently used for
	 * gva_to_gpa translations.
	 */
	struct kvm_mmu *walk_mmu;

633
	struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
634
635
	struct kvm_mmu_memory_cache mmu_shadow_page_cache;
	struct kvm_mmu_memory_cache mmu_gfn_array_cache;
636
637
	struct kvm_mmu_memory_cache mmu_page_header_cache;

638
639
	/*
	 * QEMU userspace and the guest each have their own FPU state.
640
641
642
	 * 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.
643
644
645
646
647
648
	 *
	 * 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.
	 */
649
	struct fpu *user_fpu;
650
	struct fpu *guest_fpu;
651

652
	u64 xcr0;
653
	u64 guest_supported_xcr0;
654
655
656

	struct kvm_pio_request pio;
	void *pio_data;
657
	void *guest_ins_data;
658

659
660
	u8 event_exit_inst_len;

661
662
	struct kvm_queued_exception {
		bool pending;
663
		bool injected;
664
665
666
		bool has_error_code;
		u8 nr;
		u32 error_code;
667
668
		unsigned long payload;
		bool has_payload;
669
		u8 nested_apf;
670
671
	} exception;

672
	struct kvm_queued_interrupt {
673
		bool injected;
674
		bool soft;
675
676
677
		u8 nr;
	} interrupt;

678
679
680
	int halt_request; /* real mode on Intel only */

	int cpuid_nent;
681
	struct kvm_cpuid_entry2 *cpuid_entries;
682

683
	u64 reserved_gpa_bits;
684
	int maxphyaddr;
685
	int max_tdp_level;
686

687
688
	/* emulate context */

689
	struct x86_emulate_ctxt *emulate_ctxt;
690
691
	bool emulate_regs_need_sync_to_vcpu;
	bool emulate_regs_need_sync_from_vcpu;
692
	int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
693
694

	gpa_t time;
695
	struct pvclock_vcpu_time_info hv_clock;
Zachary Amsden's avatar
Zachary Amsden committed
696
	unsigned int hw_tsc_khz;
697
698
	struct gfn_to_hva_cache pv_time;
	bool pv_time_enabled;
699
700
	/* set guest stopped flag in pvclock flags field */
	bool pvclock_set_guest_stopped_request;
Glauber Costa's avatar
Glauber Costa committed
701
702

	struct {
703
		u8 preempted;
Glauber Costa's avatar
Glauber Costa committed
704
705
		u64 msr_val;
		u64 last_steal;
706
		struct gfn_to_pfn_cache cache;
Glauber Costa's avatar
Glauber Costa committed
707
708
	} st;

709
	u64 l1_tsc_offset;
710
	u64 tsc_offset;
711
	u64 last_guest_tsc;
712
	u64 last_host_tsc;
713
	u64 tsc_offset_adjustment;
714
715
	u64 this_tsc_nsec;
	u64 this_tsc_write;
Tomasz Grabiec's avatar
Tomasz Grabiec committed
716
	u64 this_tsc_generation;
Zachary Amsden's avatar
Zachary Amsden committed
717
	bool tsc_catchup;
718
719
720
721
	bool tsc_always_catchup;
	s8 virtual_tsc_shift;
	u32 virtual_tsc_mult;
	u32 virtual_tsc_khz;
722
	s64 ia32_tsc_adjust_msr;
723
	u64 msr_ia32_power_ctl;
724
	u64 tsc_scaling_ratio;
725

Avi Kivity's avatar
Avi Kivity committed
726
727
728
	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 */
729
	bool smi_pending;    /* SMI queued after currently running handler */
Avi Kivity's avatar
Avi Kivity committed
730

731
	struct kvm_mtrr mtrr_state;
732
	u64 pat;
733

734
	unsigned switch_db_regs;
735
736
737
738
	unsigned long db[KVM_NR_DB_REGS];
	unsigned long dr6;
	unsigned long dr7;
	unsigned long eff_db[KVM_NR_DB_REGS];
739
	unsigned long guest_debug_dr7;
740
741
	u64 msr_platform_info;
	u64 msr_misc_features_enables;
Huang Ying's avatar
Huang Ying committed
742
743
744
745

	u64 mcg_cap;
	u64 mcg_status;
	u64 mcg_ctl;
746
	u64 mcg_ext_ctl;
Huang Ying's avatar
Huang Ying committed
747
	u64 *mce_banks;
748

749
750
	/* Cache MMIO info */
	u64 mmio_gva;
751
	unsigned mmio_access;
752
	gfn_t mmio_gfn;
753
	u64 mmio_gen;
754

755
756
	struct kvm_pmu pmu;

757
758
	/* used for guest single stepping over the given code position */
	unsigned long singlestep_rip;
Jan Kiszka's avatar
Jan Kiszka committed
759

760
	bool hyperv_enabled;
761
	struct kvm_vcpu_hv *hyperv;
762
	struct kvm_vcpu_xen xen;
763
764

	cpumask_var_t wbinvd_dirty_mask;
765

766
767
768
	unsigned long last_retry_eip;
	unsigned long last_retry_addr;

769
770
	struct {
		bool halted;
771
		gfn_t gfns[ASYNC_PF_PER_VCPU];
772
		struct gfn_to_hva_cache data;
773
774
775
		u64 msr_en_val; /* MSR_KVM_ASYNC_PF_EN */
		u64 msr_int_val; /* MSR_KVM_ASYNC_PF_INT */
		u16 vec;
776
		u32 id;
777
		bool send_user_only;
778
		u32 host_apf_flags;
779
		unsigned long nested_apf_token;
780
		bool delivery_as_pf_vmexit;
781
		bool pageready_pending;
782
	} apf;
783
784
785
786
787
788

	/* OSVW MSRs (AMD only) */
	struct {
		u64 length;
		u64 status;
	} osvw;
789
790
791
792
793

	struct {
		u64 msr_val;
		struct gfn_to_hva_cache data;
	} pv_eoi;
794

795
796
	u64 msr_kvm_poll_control;

797
	/*
798
799
800
801
802
803
804
805
806
807
808
809
810
	 * Indicates the guest is trying to write a gfn that contains one or
	 * more of the PTEs used to translate the write itself, i.e. the access
	 * is changing its own translation in the guest page tables.  KVM exits
	 * to userspace if emulation of the faulting instruction fails and this
	 * flag is set, as KVM cannot make forward progress.
	 *
	 * If emulation fails for a write to guest page tables, KVM unprotects
	 * (zaps) the shadow page for the target gfn and resumes the guest to
	 * retry the non-emulatable instruction (on hardware).  Unprotecting the
	 * gfn doesn't allow forward progress for a self-changing access because
	 * doing so also zaps the translation for the gfn, i.e. retrying the
	 * instruction will hit a !PRESENT fault, which results in a new shadow
	 * page and sends KVM back to square one.
811
812
	 */
	bool write_fault_to_shadow_pgtable;
813
814
815

	/* set at EPT violation at this point */
	unsigned long exit_qualification;
816
817
818
819
820

	/* pv related host specific info */
	struct {
		bool pv_unhalted;
	} pv;
821
822

	int pending_ioapic_eoi;
823
	int pending_external_vector;
824

825
826
	/* be preempted when it's in kernel-mode(cpl=0) */
	bool preempted_in_kernel;
827
828
829

	/* Flush the L1 Data cache for L1TF mitigation on VMENTER */
	bool l1tf_flush_l1d;
830

831
832
833
	/* Host CPU on which VM-entry was most recently attempted */
	unsigned int last_vmentry_cpu;

834
835
	/* AMD MSRC001_0015 Hardware Configuration */
	u64 msr_hwcr;
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850

	/* pv related cpuid info */
	struct {
		/*
		 * value of the eax register in the KVM_CPUID_FEATURES CPUID
		 * leaf.
		 */
		u32 features;

		/*
		 * indicates whether pv emulation should be disabled if features
		 * are not present in the guest's cpuid
		 */
		bool enforce;
	} pv_cpuid;
851
852
853

	/* Protected Guests */
	bool guest_state_protected;
854
855
};

856
struct kvm_lpage_info {
857
	int disallow_lpage;
858
859
860
};

struct kvm_arch_memory_slot {
861
	struct kvm_rmap_head *rmap[KVM_NR_PAGE_SIZES];
862
	struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
863
	unsigned short *gfn_track[KVM_PAGE_TRACK_MAX];
864
865
};

866
867
868
869
870
871
872
873
874
875
876
/*
 * 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

877
878
struct kvm_apic_map {
	struct rcu_head rcu;
879
	u8 mode;
880
	u32 max_apic_id;
881
882
883
884
	union {
		struct kvm_lapic *xapic_flat_map[8];
		struct kvm_lapic *xapic_cluster_map[16][4];
	};
885
	struct kvm_lapic *phys_map[];
886
887
};

888
889
890
891
892
893
894
895
896
897
898
899
/* Hyper-V synthetic debugger (SynDbg)*/
struct kvm_hv_syndbg {
	struct {
		u64 control;
		u64 status;
		u64 send_page;
		u64 recv_page;
		u64 pending_page;
	} control;
	u64 options;
};

900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/* Current state of Hyper-V TSC page clocksource */
enum hv_tsc_page_status {
	/* TSC page was not set up or disabled */
	HV_TSC_PAGE_UNSET = 0,
	/* TSC page MSR was written by the guest, update pending */
	HV_TSC_PAGE_GUEST_CHANGED,
	/* TSC page MSR was written by KVM userspace, update pending */
	HV_TSC_PAGE_HOST_CHANGED,
	/* TSC page was properly set up and is currently active  */
	HV_TSC_PAGE_SET,
	/* TSC page is currently being updated and therefore is inactive */
	HV_TSC_PAGE_UPDATING,
	/* TSC page was set up with an inaccessible GPA */
	HV_TSC_PAGE_BROKEN,
};

916
917
/* Hyper-V emulation context */
struct kvm_hv {
918
	struct mutex hv_lock;
919
920
921
	u64 hv_guest_os_id;
	u64 hv_hypercall;
	u64 hv_tsc_page;
922
	enum hv_tsc_page_status hv_tsc_page_status;
923
924
925
926

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

928
	struct ms_hyperv_tsc_page tsc_ref;
929
930

	struct idr conn_to_evt;
931
932
933
934

	u64 hv_reenlightenment_control;
	u64 hv_tsc_emulation_control;
	u64 hv_tsc_emulation_status;
935
936
937

	/* How many vCPUs have VP index != vCPU index */
	atomic_t num_mismatched_vp_indexes;
938
939

	struct hv_partition_assist_pg *hv_pa_pg;
940
	struct kvm_hv_syndbg hv_syndbg;
941
942
};

943
944
945
946
947
948
949
struct msr_bitmap_range {
	u32 flags;
	u32 nmsrs;
	u32 base;
	unsigned long *bitmap;
};

950
951
952
/* Xen emulation context */
struct kvm_xen {
	bool long_mode;
953
	bool shinfo_set;
954
	u8 upcall_vector;
955
	struct gfn_to_hva_cache shinfo_cache;
956
957
};

958
959
960
961
962
963
enum kvm_irqchip_mode {
	KVM_IRQCHIP_NONE,
	KVM_IRQCHIP_KERNEL,       /* created with KVM_CREATE_IRQCHIP */
	KVM_IRQCHIP_SPLIT,        /* created with KVM_CAP_SPLIT_IRQCHIP */
};

964
965
966
967
968
969
struct kvm_x86_msr_filter {
	u8 count;
	bool default_allow:1;
	struct