]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - arch/x86/include/asm/kvm_host.h
kvm: fix potentially corrupt mmio cache
[mirror_ubuntu-hirsute-kernel.git] / arch / x86 / include / asm / kvm_host.h
CommitLineData
a656c8ef 1/*
043405e1
CO
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * This header defines architecture specific interfaces, x86 version
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2. See
7 * the COPYING file in the top-level directory.
8 *
9 */
10
1965aae3
PA
11#ifndef _ASM_X86_KVM_HOST_H
12#define _ASM_X86_KVM_HOST_H
043405e1 13
34c16eec
ZX
14#include <linux/types.h>
15#include <linux/mm.h>
e930bffe 16#include <linux/mmu_notifier.h>
229456fc 17#include <linux/tracepoint.h>
f5f48ee1 18#include <linux/cpumask.h>
f5132b01 19#include <linux/irq_work.h>
34c16eec
ZX
20
21#include <linux/kvm.h>
22#include <linux/kvm_para.h>
edf88417 23#include <linux/kvm_types.h>
f5132b01 24#include <linux/perf_event.h>
d828199e
MT
25#include <linux/pvclock_gtod.h>
26#include <linux/clocksource.h>
34c16eec 27
50d0a0f9 28#include <asm/pvclock-abi.h>
e01a1b57 29#include <asm/desc.h>
0bed3b56 30#include <asm/mtrr.h>
9962d032 31#include <asm/msr-index.h>
3ee89722 32#include <asm/asm.h>
e01a1b57 33
cbf64358 34#define KVM_MAX_VCPUS 255
a59cb29e 35#define KVM_SOFT_MAX_VCPUS 160
0f888f5a 36#define KVM_USER_MEM_SLOTS 125
0743247f
AW
37/* memory slots that are not exposed to userspace */
38#define KVM_PRIVATE_MEM_SLOTS 3
bbacc0c1 39#define KVM_MEM_SLOTS_NUM (KVM_USER_MEM_SLOTS + KVM_PRIVATE_MEM_SLOTS)
93a5cef0 40
cef4dea0 41#define KVM_MMIO_SIZE 16
69a9f69b
AK
42
43#define KVM_PIO_PAGE_OFFSET 1
542472b5 44#define KVM_COALESCED_MMIO_PAGE_OFFSET 2
69a9f69b 45
8175e5b7
AG
46#define KVM_IRQCHIP_NUM_PINS KVM_IOAPIC_NUM_PINS
47
cfec82cb
JR
48#define CR0_RESERVED_BITS \
49 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
50 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
51 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
52
346874c9 53#define CR3_L_MODE_RESERVED_BITS 0xFFFFFF0000000000ULL
cfec82cb
JR
54#define CR4_RESERVED_BITS \
55 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
56 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
ad756a16 57 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR | X86_CR4_PCIDE \
afcbf13f 58 | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_FSGSBASE \
56d6efc2 59 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE | X86_CR4_SMAP))
cfec82cb
JR
60
61#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
62
63
cd6e8f87 64
cd6e8f87 65#define INVALID_PAGE (~(hpa_t)0)
dd180b3e
XG
66#define VALID_PAGE(x) ((x) != INVALID_PAGE)
67
cd6e8f87
ZX
68#define UNMAPPED_GVA (~(gpa_t)0)
69
ec04b260 70/* KVM Hugepage definitions for x86 */
04326caa 71#define KVM_NR_PAGE_SIZES 3
82855413
JR
72#define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 9)
73#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
ec04b260
JR
74#define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
75#define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
76#define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
05da4558 77
6d9d41e5
CD
78static inline gfn_t gfn_to_index(gfn_t gfn, gfn_t base_gfn, int level)
79{
80 /* KVM_HPAGE_GFN_SHIFT(PT_PAGE_TABLE_LEVEL) must be 0. */
81 return (gfn >> KVM_HPAGE_GFN_SHIFT(level)) -
82 (base_gfn >> KVM_HPAGE_GFN_SHIFT(level));
83}
84
cd6e8f87
ZX
85#define SELECTOR_TI_MASK (1 << 2)
86#define SELECTOR_RPL_MASK 0x03
87
88#define IOPL_SHIFT 12
89
d657a98e
ZX
90#define KVM_PERMILLE_MMU_PAGES 20
91#define KVM_MIN_ALLOC_MMU_PAGES 64
1ae0a13d
DE
92#define KVM_MMU_HASH_SHIFT 10
93#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
d657a98e
ZX
94#define KVM_MIN_FREE_MMU_PAGES 5
95#define KVM_REFILL_PAGES 25
73c1160c 96#define KVM_MAX_CPUID_ENTRIES 80
0bed3b56 97#define KVM_NR_FIXED_MTRR_REGION 88
0d234daf 98#define KVM_NR_VAR_MTRR 8
d657a98e 99
af585b92
GN
100#define ASYNC_PF_PER_VCPU 64
101
5fdbf976 102enum kvm_reg {
2b3ccfa0
ZX
103 VCPU_REGS_RAX = 0,
104 VCPU_REGS_RCX = 1,
105 VCPU_REGS_RDX = 2,
106 VCPU_REGS_RBX = 3,
107 VCPU_REGS_RSP = 4,
108 VCPU_REGS_RBP = 5,
109 VCPU_REGS_RSI = 6,
110 VCPU_REGS_RDI = 7,
111#ifdef CONFIG_X86_64
112 VCPU_REGS_R8 = 8,
113 VCPU_REGS_R9 = 9,
114 VCPU_REGS_R10 = 10,
115 VCPU_REGS_R11 = 11,
116 VCPU_REGS_R12 = 12,
117 VCPU_REGS_R13 = 13,
118 VCPU_REGS_R14 = 14,
119 VCPU_REGS_R15 = 15,
120#endif
5fdbf976 121 VCPU_REGS_RIP,
2b3ccfa0
ZX
122 NR_VCPU_REGS
123};
124
6de4f3ad
AK
125enum kvm_reg_ex {
126 VCPU_EXREG_PDPTR = NR_VCPU_REGS,
aff48baa 127 VCPU_EXREG_CR3,
6de12732 128 VCPU_EXREG_RFLAGS,
2fb92db1 129 VCPU_EXREG_SEGMENTS,
6de4f3ad
AK
130};
131
2b3ccfa0 132enum {
81609e3e 133 VCPU_SREG_ES,
2b3ccfa0 134 VCPU_SREG_CS,
81609e3e 135 VCPU_SREG_SS,
2b3ccfa0 136 VCPU_SREG_DS,
2b3ccfa0
ZX
137 VCPU_SREG_FS,
138 VCPU_SREG_GS,
2b3ccfa0
ZX
139 VCPU_SREG_TR,
140 VCPU_SREG_LDTR,
141};
142
56e82318 143#include <asm/kvm_emulate.h>
2b3ccfa0 144
d657a98e
ZX
145#define KVM_NR_MEM_OBJS 40
146
42dbaa5a
JK
147#define KVM_NR_DB_REGS 4
148
149#define DR6_BD (1 << 13)
150#define DR6_BS (1 << 14)
6f43ed01
NA
151#define DR6_RTM (1 << 16)
152#define DR6_FIXED_1 0xfffe0ff0
153#define DR6_INIT 0xffff0ff0
154#define DR6_VOLATILE 0x0001e00f
42dbaa5a
JK
155
156#define DR7_BP_EN_MASK 0x000000ff
157#define DR7_GE (1 << 9)
158#define DR7_GD (1 << 13)
159#define DR7_FIXED_1 0x00000400
6f43ed01 160#define DR7_VOLATILE 0xffff2bff
42dbaa5a 161
41383771
GN
162/* apic attention bits */
163#define KVM_APIC_CHECK_VAPIC 0
ae7a2a3f
MT
164/*
165 * The following bit is set with PV-EOI, unset on EOI.
166 * We detect PV-EOI changes by guest by comparing
167 * this bit with PV-EOI in guest memory.
168 * See the implementation in apic_update_pv_eoi.
169 */
170#define KVM_APIC_PV_EOI_PENDING 1
41383771 171
d657a98e
ZX
172/*
173 * We don't want allocation failures within the mmu code, so we preallocate
174 * enough memory for a single page fault in a cache.
175 */
176struct kvm_mmu_memory_cache {
177 int nobjs;
178 void *objects[KVM_NR_MEM_OBJS];
179};
180
d657a98e
ZX
181/*
182 * kvm_mmu_page_role, below, is defined as:
183 *
184 * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
185 * bits 4:7 - page table level for this shadow (1-4)
186 * bits 8:9 - page table quadrant for 2-level guests
f6e2c02b
AK
187 * bit 16 - direct mapping of virtual to physical mapping at gfn
188 * used for real mode and two-dimensional paging
d657a98e
ZX
189 * bits 17:19 - common access permissions for all ptes in this shadow page
190 */
191union kvm_mmu_page_role {
192 unsigned word;
193 struct {
7d76b4d3 194 unsigned level:4;
5b7e0102 195 unsigned cr4_pae:1;
7d76b4d3
JP
196 unsigned quadrant:2;
197 unsigned pad_for_nice_hex_output:6;
f6e2c02b 198 unsigned direct:1;
7d76b4d3 199 unsigned access:3;
2e53d63a 200 unsigned invalid:1;
9645bb56 201 unsigned nxe:1;
3dbe1415 202 unsigned cr0_wp:1;
411c588d 203 unsigned smep_andnot_wp:1;
d657a98e
ZX
204 };
205};
206
207struct kvm_mmu_page {
208 struct list_head link;
209 struct hlist_node hash_link;
210
211 /*
212 * The following two entries are used to key the shadow page in the
213 * hash table.
214 */
215 gfn_t gfn;
216 union kvm_mmu_page_role role;
217
218 u64 *spt;
219 /* hold the gfn of each spte inside spt */
220 gfn_t *gfns;
4731d4c7 221 bool unsync;
0571d366 222 int root_count; /* Currently serving as active root */
60c8aec6 223 unsigned int unsync_children;
67052b35 224 unsigned long parent_ptes; /* Reverse mapping for parent_pte */
f6f8adee
XG
225
226 /* The page is obsolete if mmu_valid_gen != kvm->arch.mmu_valid_gen. */
5304b8d3 227 unsigned long mmu_valid_gen;
f6f8adee 228
0074ff63 229 DECLARE_BITMAP(unsync_child_bitmap, 512);
c2a2ac2b
XG
230
231#ifdef CONFIG_X86_32
accaefe0
XG
232 /*
233 * Used out of the mmu-lock to avoid reading spte values while an
234 * update is in progress; see the comments in __get_spte_lockless().
235 */
c2a2ac2b
XG
236 int clear_spte_count;
237#endif
238
0cbf8e43 239 /* Number of writes since the last time traversal visited this page. */
a30f47cb 240 int write_flooding_count;
d657a98e
ZX
241};
242
1c08364c
AK
243struct kvm_pio_request {
244 unsigned long count;
1c08364c
AK
245 int in;
246 int port;
247 int size;
1c08364c
AK
248};
249
d657a98e
ZX
250/*
251 * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
252 * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
253 * mode.
254 */
255struct kvm_mmu {
f43addd4 256 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
5777ed34 257 unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
e4e517b4 258 u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
78b2c54a
XG
259 int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err,
260 bool prefault);
6389ee94
AK
261 void (*inject_page_fault)(struct kvm_vcpu *vcpu,
262 struct x86_exception *fault);
1871c602 263 gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
ab9ae313 264 struct x86_exception *exception);
c30a358d 265 gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access);
e8bc217a 266 int (*sync_page)(struct kvm_vcpu *vcpu,
a4a8e6f7 267 struct kvm_mmu_page *sp);
a7052897 268 void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
0f53b5b1 269 void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
7c562522 270 u64 *spte, const void *pte);
d657a98e
ZX
271 hpa_t root_hpa;
272 int root_level;
273 int shadow_root_level;
a770f6f2 274 union kvm_mmu_page_role base_role;
c5a78f2b 275 bool direct_map;
d657a98e 276
97d64b78
AK
277 /*
278 * Bitmap; bit set = permission fault
279 * Byte index: page fault error code [4:1]
280 * Bit index: pte permissions in ACC_* format
281 */
282 u8 permissions[16];
283
d657a98e 284 u64 *pae_root;
81407ca5 285 u64 *lm_root;
82725b20 286 u64 rsvd_bits_mask[2][4];
25d92081 287 u64 bad_mt_xwr;
ff03a073 288
6fd01b71
AK
289 /*
290 * Bitmap: bit set = last pte in walk
291 * index[0:1]: level (zero-based)
292 * index[2]: pte.ps
293 */
294 u8 last_pte_bitmap;
295
2d48a985
JR
296 bool nx;
297
ff03a073 298 u64 pdptrs[4]; /* pae */
d657a98e
ZX
299};
300
f5132b01
GN
301enum pmc_type {
302 KVM_PMC_GP = 0,
303 KVM_PMC_FIXED,
304};
305
306struct kvm_pmc {
307 enum pmc_type type;
308 u8 idx;
309 u64 counter;
310 u64 eventsel;
311 struct perf_event *perf_event;
312 struct kvm_vcpu *vcpu;
313};
314
315struct kvm_pmu {
316 unsigned nr_arch_gp_counters;
317 unsigned nr_arch_fixed_counters;
318 unsigned available_event_types;
319 u64 fixed_ctr_ctrl;
320 u64 global_ctrl;
321 u64 global_status;
322 u64 global_ovf_ctrl;
323 u64 counter_bitmask[2];
324 u64 global_ctrl_mask;
103af0a9 325 u64 reserved_bits;
f5132b01 326 u8 version;
15c7ad51
RR
327 struct kvm_pmc gp_counters[INTEL_PMC_MAX_GENERIC];
328 struct kvm_pmc fixed_counters[INTEL_PMC_MAX_FIXED];
f5132b01
GN
329 struct irq_work irq_work;
330 u64 reprogram_pmi;
331};
332
360b948d
PB
333enum {
334 KVM_DEBUGREG_BP_ENABLED = 1,
c77fb5fe 335 KVM_DEBUGREG_WONT_EXIT = 2,
360b948d
PB
336};
337
ad312c7c 338struct kvm_vcpu_arch {
5fdbf976
MT
339 /*
340 * rip and regs accesses must go through
341 * kvm_{register,rip}_{read,write} functions.
342 */
343 unsigned long regs[NR_VCPU_REGS];
344 u32 regs_avail;
345 u32 regs_dirty;
34c16eec
ZX
346
347 unsigned long cr0;
e8467fda 348 unsigned long cr0_guest_owned_bits;
34c16eec
ZX
349 unsigned long cr2;
350 unsigned long cr3;
351 unsigned long cr4;
fc78f519 352 unsigned long cr4_guest_owned_bits;
34c16eec 353 unsigned long cr8;
1371d904 354 u32 hflags;
f6801dff 355 u64 efer;
34c16eec
ZX
356 u64 apic_base;
357 struct kvm_lapic *apic; /* kernel irqchip context */
41383771 358 unsigned long apic_attention;
e1035715 359 int32_t apic_arb_prio;
34c16eec 360 int mp_state;
34c16eec 361 u64 ia32_misc_enable_msr;
b209749f 362 bool tpr_access_reporting;
34c16eec 363
14dfe855
JR
364 /*
365 * Paging state of the vcpu
366 *
367 * If the vcpu runs in guest mode with two level paging this still saves
368 * the paging mode of the l1 guest. This context is always used to
369 * handle faults.
370 */
34c16eec 371 struct kvm_mmu mmu;
8df25a32 372
6539e738
JR
373 /*
374 * Paging state of an L2 guest (used for nested npt)
375 *
376 * This context will save all necessary information to walk page tables
377 * of the an L2 guest. This context is only initialized for page table
378 * walking and not for faulting since we never handle l2 page faults on
379 * the host.
380 */
381 struct kvm_mmu nested_mmu;
382
14dfe855
JR
383 /*
384 * Pointer to the mmu context currently used for
385 * gva_to_gpa translations.
386 */
387 struct kvm_mmu *walk_mmu;
388
53c07b18 389 struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
34c16eec
ZX
390 struct kvm_mmu_memory_cache mmu_page_cache;
391 struct kvm_mmu_memory_cache mmu_page_header_cache;
392
98918833 393 struct fpu guest_fpu;
2acf923e 394 u64 xcr0;
d7876f1b 395 u64 guest_supported_xcr0;
4344ee98 396 u32 guest_xstate_size;
34c16eec 397
34c16eec
ZX
398 struct kvm_pio_request pio;
399 void *pio_data;
400
66fd3f7f
GN
401 u8 event_exit_inst_len;
402
298101da
AK
403 struct kvm_queued_exception {
404 bool pending;
405 bool has_error_code;
ce7ddec4 406 bool reinject;
298101da
AK
407 u8 nr;
408 u32 error_code;
409 } exception;
410
937a7eae
AK
411 struct kvm_queued_interrupt {
412 bool pending;
66fd3f7f 413 bool soft;
937a7eae
AK
414 u8 nr;
415 } interrupt;
416
34c16eec
ZX
417 int halt_request; /* real mode on Intel only */
418
419 int cpuid_nent;
07716717 420 struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
34c16eec
ZX
421 /* emulate context */
422
423 struct x86_emulate_ctxt emulate_ctxt;
7ae441ea
GN
424 bool emulate_regs_need_sync_to_vcpu;
425 bool emulate_regs_need_sync_from_vcpu;
716d51ab 426 int (*complete_userspace_io)(struct kvm_vcpu *vcpu);
18068523
GOC
427
428 gpa_t time;
50d0a0f9 429 struct pvclock_vcpu_time_info hv_clock;
e48672fa 430 unsigned int hw_tsc_khz;
0b79459b
AH
431 struct gfn_to_hva_cache pv_time;
432 bool pv_time_enabled;
51d59c6b
MT
433 /* set guest stopped flag in pvclock flags field */
434 bool pvclock_set_guest_stopped_request;
c9aaa895
GC
435
436 struct {
437 u64 msr_val;
438 u64 last_steal;
439 u64 accum_steal;
440 struct gfn_to_hva_cache stime;
441 struct kvm_steal_time steal;
442 } st;
443
1d5f066e 444 u64 last_guest_tsc;
6f526ec5 445 u64 last_host_tsc;
0dd6a6ed 446 u64 tsc_offset_adjustment;
e26101b1
ZA
447 u64 this_tsc_nsec;
448 u64 this_tsc_write;
0d3da0d2 449 u64 this_tsc_generation;
c285545f 450 bool tsc_catchup;
cc578287
ZA
451 bool tsc_always_catchup;
452 s8 virtual_tsc_shift;
453 u32 virtual_tsc_mult;
454 u32 virtual_tsc_khz;
ba904635 455 s64 ia32_tsc_adjust_msr;
3419ffc8 456
7460fb4a
AK
457 atomic_t nmi_queued; /* unprocessed asynchronous NMIs */
458 unsigned nmi_pending; /* NMI queued after currently running handler */
459 bool nmi_injected; /* Trying to inject an NMI this entry */
9ba075a6 460
0bed3b56 461 struct mtrr_state_type mtrr_state;
7cb060a9 462 u64 pat;
42dbaa5a 463
360b948d 464 unsigned switch_db_regs;
42dbaa5a
JK
465 unsigned long db[KVM_NR_DB_REGS];
466 unsigned long dr6;
467 unsigned long dr7;
468 unsigned long eff_db[KVM_NR_DB_REGS];
c8639010 469 unsigned long guest_debug_dr7;
890ca9ae
HY
470
471 u64 mcg_cap;
472 u64 mcg_status;
473 u64 mcg_ctl;
474 u64 *mce_banks;
94fe45da 475
bebb106a
XG
476 /* Cache MMIO info */
477 u64 mmio_gva;
478 unsigned access;
479 gfn_t mmio_gfn;
480
f5132b01
GN
481 struct kvm_pmu pmu;
482
94fe45da 483 /* used for guest single stepping over the given code position */
94fe45da 484 unsigned long singlestep_rip;
f92653ee 485
10388a07
GN
486 /* fields used by HYPER-V emulation */
487 u64 hv_vapic;
f5f48ee1
SY
488
489 cpumask_var_t wbinvd_dirty_mask;
af585b92 490
1cb3f3ae
XG
491 unsigned long last_retry_eip;
492 unsigned long last_retry_addr;
493
af585b92
GN
494 struct {
495 bool halted;
496 gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
344d9588
GN
497 struct gfn_to_hva_cache data;
498 u64 msr_val;
7c90705b 499 u32 id;
6adba527 500 bool send_user_only;
af585b92 501 } apf;
2b036c6b
BO
502
503 /* OSVW MSRs (AMD only) */
504 struct {
505 u64 length;
506 u64 status;
507 } osvw;
ae7a2a3f
MT
508
509 struct {
510 u64 msr_val;
511 struct gfn_to_hva_cache data;
512 } pv_eoi;
93c05d3e
XG
513
514 /*
515 * Indicate whether the access faults on its page table in guest
516 * which is set when fix page fault and used to detect unhandeable
517 * instruction.
518 */
519 bool write_fault_to_shadow_pgtable;
25d92081
YZ
520
521 /* set at EPT violation at this point */
522 unsigned long exit_qualification;
6aef266c
SV
523
524 /* pv related host specific info */
525 struct {
526 bool pv_unhalted;
527 } pv;
34c16eec
ZX
528};
529
db3fe4eb 530struct kvm_lpage_info {
db3fe4eb
TY
531 int write_count;
532};
533
534struct kvm_arch_memory_slot {
d89cc617 535 unsigned long *rmap[KVM_NR_PAGE_SIZES];
db3fe4eb
TY
536 struct kvm_lpage_info *lpage_info[KVM_NR_PAGE_SIZES - 1];
537};
538
1e08ec4a
GN
539struct kvm_apic_map {
540 struct rcu_head rcu;
541 u8 ldr_bits;
542 /* fields bellow are used to decode ldr values in different modes */
543 u32 cid_shift, cid_mask, lid_mask;
544 struct kvm_lapic *phys_map[256];
545 /* first index is cluster id second is cpu id in a cluster */
546 struct kvm_lapic *logical_map[16][16];
547};
548
fef9cce0 549struct kvm_arch {
49d5ca26 550 unsigned int n_used_mmu_pages;
f05e70ac 551 unsigned int n_requested_mmu_pages;
39de71ec 552 unsigned int n_max_mmu_pages;
332b207d 553 unsigned int indirect_shadow_pages;
5304b8d3 554 unsigned long mmu_valid_gen;
f05e70ac
ZX
555 struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
556 /*
557 * Hash table of struct kvm_mmu_page.
558 */
559 struct list_head active_mmu_pages;
365c8868
XG
560 struct list_head zapped_obsolete_pages;
561
4d5c5d0f 562 struct list_head assigned_dev_head;
19de40a8 563 struct iommu_domain *iommu_domain;
d96eb2c6 564 bool iommu_noncoherent;
e0f0bbc5
AW
565#define __KVM_HAVE_ARCH_NONCOHERENT_DMA
566 atomic_t noncoherent_dma_count;
d7deeeb0
ZX
567 struct kvm_pic *vpic;
568 struct kvm_ioapic *vioapic;
7837699f 569 struct kvm_pit *vpit;
cc6e462c 570 int vapics_in_nmi_mode;
1e08ec4a
GN
571 struct mutex apic_map_lock;
572 struct kvm_apic_map *apic_map;
bfc6d222 573
bfc6d222
ZX
574 unsigned int tss_addr;
575 struct page *apic_access_page;
18068523
GOC
576
577 gpa_t wall_clock;
b7ebfb05
SY
578
579 struct page *ept_identity_pagetable;
580 bool ept_identity_pagetable_done;
b927a3ce 581 gpa_t ept_identity_map_addr;
5550af4d
SY
582
583 unsigned long irq_sources_bitmap;
afbcf7ab 584 s64 kvmclock_offset;
038f8c11 585 raw_spinlock_t tsc_write_lock;
f38e098f 586 u64 last_tsc_nsec;
f38e098f 587 u64 last_tsc_write;
5d3cb0f6 588 u32 last_tsc_khz;
e26101b1
ZA
589 u64 cur_tsc_nsec;
590 u64 cur_tsc_write;
591 u64 cur_tsc_offset;
0d3da0d2 592 u64 cur_tsc_generation;
b48aa97e 593 int nr_vcpus_matched_tsc;
ffde22ac 594
d828199e
MT
595 spinlock_t pvclock_gtod_sync_lock;
596 bool use_master_clock;
597 u64 master_kernel_ns;
598 cycle_t master_cycle_now;
7e44e449 599 struct delayed_work kvmclock_update_work;
332967a3 600 struct delayed_work kvmclock_sync_work;
d828199e 601
ffde22ac 602 struct kvm_xen_hvm_config xen_hvm_config;
55cd8e5a
GN
603
604 /* fields used by HYPER-V emulation */
605 u64 hv_guest_os_id;
606 u64 hv_hypercall;
e984097b 607 u64 hv_tsc_page;
b034cf01
XG
608
609 #ifdef CONFIG_KVM_MMU_AUDIT
610 int audit_point;
611 #endif
d69fb81f
ZX
612};
613
0711456c
ZX
614struct kvm_vm_stat {
615 u32 mmu_shadow_zapped;
616 u32 mmu_pte_write;
617 u32 mmu_pte_updated;
618 u32 mmu_pde_zapped;
619 u32 mmu_flooded;
620 u32 mmu_recycled;
dfc5aa00 621 u32 mmu_cache_miss;
4731d4c7 622 u32 mmu_unsync;
0711456c 623 u32 remote_tlb_flush;
05da4558 624 u32 lpages;
0711456c
ZX
625};
626
77b4c255
ZX
627struct kvm_vcpu_stat {
628 u32 pf_fixed;
629 u32 pf_guest;
630 u32 tlb_flush;
631 u32 invlpg;
632
633 u32 exits;
634 u32 io_exits;
635 u32 mmio_exits;
636 u32 signal_exits;
637 u32 irq_window_exits;
f08864b4 638 u32 nmi_window_exits;
77b4c255
ZX
639 u32 halt_exits;
640 u32 halt_wakeup;
641 u32 request_irq_exits;
642 u32 irq_exits;
643 u32 host_state_reload;
644 u32 efer_reload;
645 u32 fpu_reload;
646 u32 insn_emulation;
647 u32 insn_emulation_fail;
f11c3a8d 648 u32 hypercalls;
fa89a817 649 u32 irq_injections;
c4abb7c9 650 u32 nmi_injections;
77b4c255 651};
ad312c7c 652
8a76d7f2
JR
653struct x86_instruction_info;
654
8fe8ab46
WA
655struct msr_data {
656 bool host_initiated;
657 u32 index;
658 u64 data;
659};
660
ea4a5ff8
ZX
661struct kvm_x86_ops {
662 int (*cpu_has_kvm_support)(void); /* __init */
663 int (*disabled_by_bios)(void); /* __init */
13a34e06
RK
664 int (*hardware_enable)(void);
665 void (*hardware_disable)(void);
ea4a5ff8
ZX
666 void (*check_processor_compatibility)(void *rtn);
667 int (*hardware_setup)(void); /* __init */
668 void (*hardware_unsetup)(void); /* __exit */
774ead3a 669 bool (*cpu_has_accelerated_tpr)(void);
0e851880 670 void (*cpuid_update)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
671
672 /* Create, but do not attach this VCPU */
673 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
674 void (*vcpu_free)(struct kvm_vcpu *vcpu);
57f252f2 675 void (*vcpu_reset)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
676
677 void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
678 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
679 void (*vcpu_put)(struct kvm_vcpu *vcpu);
ea4a5ff8 680
c8639010 681 void (*update_db_bp_intercept)(struct kvm_vcpu *vcpu);
ea4a5ff8 682 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
8fe8ab46 683 int (*set_msr)(struct kvm_vcpu *vcpu, struct msr_data *msr);
ea4a5ff8
ZX
684 u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
685 void (*get_segment)(struct kvm_vcpu *vcpu,
686 struct kvm_segment *var, int seg);
2e4d2653 687 int (*get_cpl)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
688 void (*set_segment)(struct kvm_vcpu *vcpu,
689 struct kvm_segment *var, int seg);
690 void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
e8467fda 691 void (*decache_cr0_guest_bits)(struct kvm_vcpu *vcpu);
aff48baa 692 void (*decache_cr3)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
693 void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
694 void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
695 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
5e1746d6 696 int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
ea4a5ff8 697 void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
89a27f4d
GN
698 void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
699 void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
700 void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
701 void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
73aaf249
JK
702 u64 (*get_dr6)(struct kvm_vcpu *vcpu);
703 void (*set_dr6)(struct kvm_vcpu *vcpu, unsigned long value);
c77fb5fe 704 void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
020df079 705 void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
5fdbf976 706 void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
ea4a5ff8
ZX
707 unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
708 void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
02daab21 709 void (*fpu_deactivate)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
710
711 void (*tlb_flush)(struct kvm_vcpu *vcpu);
ea4a5ff8 712
851ba692
AK
713 void (*run)(struct kvm_vcpu *vcpu);
714 int (*handle_exit)(struct kvm_vcpu *vcpu);
ea4a5ff8 715 void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
2809f5d2 716 void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
37ccdcbe 717 u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
718 void (*patch_hypercall)(struct kvm_vcpu *vcpu,
719 unsigned char *hypercall_addr);
66fd3f7f 720 void (*set_irq)(struct kvm_vcpu *vcpu);
95ba8273 721 void (*set_nmi)(struct kvm_vcpu *vcpu);
298101da 722 void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
ce7ddec4
JR
723 bool has_error_code, u32 error_code,
724 bool reinject);
b463a6f7 725 void (*cancel_injection)(struct kvm_vcpu *vcpu);
78646121 726 int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
95ba8273 727 int (*nmi_allowed)(struct kvm_vcpu *vcpu);
3cfc3092
JK
728 bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
729 void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
c9a7953f
JK
730 void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
731 void (*enable_irq_window)(struct kvm_vcpu *vcpu);
95ba8273 732 void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
c7c9c56c
YZ
733 int (*vm_has_apicv)(struct kvm *kvm);
734 void (*hwapic_irr_update)(struct kvm_vcpu *vcpu, int max_irr);
735 void (*hwapic_isr_update)(struct kvm *kvm, int isr);
736 void (*load_eoi_exitmap)(struct kvm_vcpu *vcpu, u64 *eoi_exit_bitmap);
8d14695f 737 void (*set_virtual_x2apic_mode)(struct kvm_vcpu *vcpu, bool set);
a20ed54d
YZ
738 void (*deliver_posted_interrupt)(struct kvm_vcpu *vcpu, int vector);
739 void (*sync_pir_to_irr)(struct kvm_vcpu *vcpu);
ea4a5ff8 740 int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
67253af5 741 int (*get_tdp_level)(void);
4b12f0de 742 u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
17cc3935 743 int (*get_lpage_level)(void);
4e47c7a6 744 bool (*rdtscp_supported)(void);
ad756a16 745 bool (*invpcid_supported)(void);
f1e2b260 746 void (*adjust_tsc_offset)(struct kvm_vcpu *vcpu, s64 adjustment, bool host);
344f414f 747
1c97f0a0
JR
748 void (*set_tdp_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
749
d4330ef2
JR
750 void (*set_supported_cpuid)(u32 func, struct kvm_cpuid_entry2 *entry);
751
f5f48ee1
SY
752 bool (*has_wbinvd_exit)(void);
753
cc578287 754 void (*set_tsc_khz)(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale);
ba904635 755 u64 (*read_tsc_offset)(struct kvm_vcpu *vcpu);
99e3e30a
ZA
756 void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
757
857e4099 758 u64 (*compute_tsc_offset)(struct kvm_vcpu *vcpu, u64 target_tsc);
886b470c 759 u64 (*read_l1_tsc)(struct kvm_vcpu *vcpu, u64 host_tsc);
857e4099 760
586f9607 761 void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
8a76d7f2
JR
762
763 int (*check_intercept)(struct kvm_vcpu *vcpu,
764 struct x86_instruction_info *info,
765 enum x86_intercept_stage stage);
a547c6db 766 void (*handle_external_intr)(struct kvm_vcpu *vcpu);
da8999d3 767 bool (*mpx_supported)(void);
b6b8a145
JK
768
769 int (*check_nested_events)(struct kvm_vcpu *vcpu, bool external_intr);
ae97a3b8
RK
770
771 void (*sched_in)(struct kvm_vcpu *kvm, int cpu);
ea4a5ff8
ZX
772};
773
af585b92 774struct kvm_arch_async_pf {
7c90705b 775 u32 token;
af585b92 776 gfn_t gfn;
fb67e14f 777 unsigned long cr3;
c4806acd 778 bool direct_map;
af585b92
GN
779};
780
97896d04
ZX
781extern struct kvm_x86_ops *kvm_x86_ops;
782
f1e2b260
MT
783static inline void adjust_tsc_offset_guest(struct kvm_vcpu *vcpu,
784 s64 adjustment)
785{
786 kvm_x86_ops->adjust_tsc_offset(vcpu, adjustment, false);
787}
788
789static inline void adjust_tsc_offset_host(struct kvm_vcpu *vcpu, s64 adjustment)
790{
791 kvm_x86_ops->adjust_tsc_offset(vcpu, adjustment, true);
792}
793
54f1585a
ZX
794int kvm_mmu_module_init(void);
795void kvm_mmu_module_exit(void);
796
797void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
798int kvm_mmu_create(struct kvm_vcpu *vcpu);
8a3c1a33 799void kvm_mmu_setup(struct kvm_vcpu *vcpu);
7b52345e 800void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
4b12f0de 801 u64 dirty_mask, u64 nx_mask, u64 x_mask);
54f1585a 802
8a3c1a33 803void kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
54f1585a 804void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
5dc99b23
TY
805void kvm_mmu_write_protect_pt_masked(struct kvm *kvm,
806 struct kvm_memory_slot *slot,
807 gfn_t gfn_offset, unsigned long mask);
54f1585a 808void kvm_mmu_zap_all(struct kvm *kvm);
f8f55942 809void kvm_mmu_invalidate_mmio_sptes(struct kvm *kvm);
3ad82a7e 810unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
54f1585a
ZX
811void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
812
ff03a073 813int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
cc4b6871 814
3200f405 815int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
9f811285 816 const void *val, int bytes);
4b12f0de 817u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
2f333bcb
MT
818
819extern bool tdp_enabled;
9f811285 820
a3e06bbe
LJ
821u64 vcpu_tsc_khz(struct kvm_vcpu *vcpu);
822
92a1f12d
JR
823/* control of guest tsc rate supported? */
824extern bool kvm_has_tsc_control;
825/* minimum supported tsc_khz for guests */
826extern u32 kvm_min_guest_tsc_khz;
827/* maximum supported tsc_khz for guests */
828extern u32 kvm_max_guest_tsc_khz;
829
54f1585a 830enum emulation_result {
ac0a48c3
PB
831 EMULATE_DONE, /* no further processing */
832 EMULATE_USER_EXIT, /* kvm_run ready for userspace exit */
54f1585a
ZX
833 EMULATE_FAIL, /* can't emulate this instruction */
834};
835
571008da
SY
836#define EMULTYPE_NO_DECODE (1 << 0)
837#define EMULTYPE_TRAP_UD (1 << 1)
ba8afb6b 838#define EMULTYPE_SKIP (1 << 2)
1cb3f3ae 839#define EMULTYPE_RETRY (1 << 3)
991eebf9 840#define EMULTYPE_NO_REEXECUTE (1 << 4)
dc25e89e
AP
841int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,
842 int emulation_type, void *insn, int insn_len);
51d8b661
AP
843
844static inline int emulate_instruction(struct kvm_vcpu *vcpu,
845 int emulation_type)
846{
dc25e89e 847 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
51d8b661
AP
848}
849
f2b4b7dd 850void kvm_enable_efer_bits(u64);
384bb783 851bool kvm_valid_efer(struct kvm_vcpu *vcpu, u64 efer);
54f1585a 852int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
8fe8ab46 853int kvm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr);
54f1585a
ZX
854
855struct x86_emulate_ctxt;
856
cf8f70bf 857int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port);
54f1585a
ZX
858void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
859int kvm_emulate_halt(struct kvm_vcpu *vcpu);
f5f48ee1 860int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
54f1585a 861
3e6e0aab 862void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
c697518a 863int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg);
66450a21 864void kvm_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, unsigned int vector);
3e6e0aab 865
7f3d35fd
KW
866int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int idt_index,
867 int reason, bool has_error_code, u32 error_code);
37817f29 868
49a9b07e 869int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
2390218b 870int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
a83b29c6 871int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
eea1cff9 872int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
020df079
GN
873int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
874int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
2d3ad1f4
AK
875unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
876void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
54f1585a 877void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
2acf923e 878int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr);
54f1585a
ZX
879
880int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
8fe8ab46 881int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr);
54f1585a 882
91586a3b
JK
883unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu);
884void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
022cd0e8 885bool kvm_rdpmc(struct kvm_vcpu *vcpu);
91586a3b 886
298101da
AK
887void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
888void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
ce7ddec4
JR
889void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr);
890void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
6389ee94 891void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
ec92fe44
JR
892int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
893 gfn_t gfn, void *data, int offset, int len,
894 u32 access);
6389ee94 895void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
0a79b009 896bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
298101da 897
1a577b72
MT
898static inline int __kvm_irq_line_state(unsigned long *irq_state,
899 int irq_source_id, int level)
900{
901 /* Logical OR for level trig interrupt */
902 if (level)
903 __set_bit(irq_source_id, irq_state);
904 else
905 __clear_bit(irq_source_id, irq_state);
906
907 return !!(*irq_state);
908}
909
910int kvm_pic_set_irq(struct kvm_pic *pic, int irq, int irq_source_id, int level);
911void kvm_pic_clear_all(struct kvm_pic *pic, int irq_source_id);
3de42dc0 912
3419ffc8
SY
913void kvm_inject_nmi(struct kvm_vcpu *vcpu);
914
10ab25cd 915int fx_init(struct kvm_vcpu *vcpu);
54f1585a 916
d835dfec 917void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
54f1585a 918void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
f57f2ef5 919 const u8 *new, int bytes);
1cb3f3ae 920int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn);
54f1585a
ZX
921int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
922void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
923int kvm_mmu_load(struct kvm_vcpu *vcpu);
924void kvm_mmu_unload(struct kvm_vcpu *vcpu);
0ba73cda 925void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
e459e322 926gpa_t translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access);
ab9ae313
AK
927gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
928 struct x86_exception *exception);
929gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
930 struct x86_exception *exception);
931gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
932 struct x86_exception *exception);
933gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
934 struct x86_exception *exception);
54f1585a
ZX
935
936int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
937
dc25e89e
AP
938int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code,
939 void *insn, int insn_len);
a7052897 940void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
d8d173da 941void kvm_mmu_new_cr3(struct kvm_vcpu *vcpu);
34c16eec 942
18552672 943void kvm_enable_tdp(void);
5f4cb662 944void kvm_disable_tdp(void);
18552672 945
e459e322
XG
946static inline gpa_t translate_gpa(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access)
947{
948 return gpa;
949}
950
ec6d273d
ZX
951static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
952{
953 struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
954
955 return (struct kvm_mmu_page *)page_private(page);
956}
957
d6e88aec 958static inline u16 kvm_read_ldt(void)
ec6d273d
ZX
959{
960 u16 ldt;
961 asm("sldt %0" : "=g"(ldt));
962 return ldt;
963}
964
d6e88aec 965static inline void kvm_load_ldt(u16 sel)
ec6d273d
ZX
966{
967 asm("lldt %0" : : "rm"(sel));
968}
ec6d273d 969
ec6d273d
ZX
970#ifdef CONFIG_X86_64
971static inline unsigned long read_msr(unsigned long msr)
972{
973 u64 value;
974
975 rdmsrl(msr, value);
976 return value;
977}
978#endif
979
ec6d273d
ZX
980static inline u32 get_rdx_init_val(void)
981{
982 return 0x600; /* P6 family */
983}
984
c1a5d4f9
AK
985static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
986{
987 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
988}
989
ec6d273d
ZX
990#define TSS_IOPB_BASE_OFFSET 0x66
991#define TSS_BASE_SIZE 0x68
992#define TSS_IOPB_SIZE (65536 / 8)
993#define TSS_REDIRECTION_SIZE (256 / 8)
7d76b4d3
JP
994#define RMODE_TSS_SIZE \
995 (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
53e0aa7b 996
37817f29
IE
997enum {
998 TASK_SWITCH_CALL = 0,
999 TASK_SWITCH_IRET = 1,
1000 TASK_SWITCH_JMP = 2,
1001 TASK_SWITCH_GATE = 3,
1002};
1003
1371d904 1004#define HF_GIF_MASK (1 << 0)
3d6368ef
AG
1005#define HF_HIF_MASK (1 << 1)
1006#define HF_VINTR_MASK (1 << 2)
95ba8273 1007#define HF_NMI_MASK (1 << 3)
44c11430 1008#define HF_IRET_MASK (1 << 4)
ec9e60b2 1009#define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
1371d904 1010
4ecac3fd
AK
1011/*
1012 * Hardware virtualization extension instructions may fault if a
1013 * reboot turns off virtualization while processes are running.
1014 * Trap the fault and ignore the instruction if that happens.
1015 */
b7c4145b 1016asmlinkage void kvm_spurious_fault(void);
4ecac3fd 1017
5e520e62 1018#define ____kvm_handle_fault_on_reboot(insn, cleanup_insn) \
4ecac3fd 1019 "666: " insn "\n\t" \
b7c4145b 1020 "668: \n\t" \
18b13e54 1021 ".pushsection .fixup, \"ax\" \n" \
4ecac3fd 1022 "667: \n\t" \
5e520e62 1023 cleanup_insn "\n\t" \
b7c4145b
AK
1024 "cmpb $0, kvm_rebooting \n\t" \
1025 "jne 668b \n\t" \
8ceed347 1026 __ASM_SIZE(push) " $666b \n\t" \
b7c4145b 1027 "call kvm_spurious_fault \n\t" \
4ecac3fd 1028 ".popsection \n\t" \
3ee89722 1029 _ASM_EXTABLE(666b, 667b)
4ecac3fd 1030
5e520e62
AK
1031#define __kvm_handle_fault_on_reboot(insn) \
1032 ____kvm_handle_fault_on_reboot(insn, "")
1033
e930bffe
AA
1034#define KVM_ARCH_WANT_MMU_NOTIFIER
1035int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
b3ae2096 1036int kvm_unmap_hva_range(struct kvm *kvm, unsigned long start, unsigned long end);
e930bffe 1037int kvm_age_hva(struct kvm *kvm, unsigned long hva);
8ee53820 1038int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
3da0dd43 1039void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
82725b20 1040int cpuid_maxphyaddr(struct kvm_vcpu *vcpu);
c7c9c56c 1041int kvm_cpu_has_injectable_intr(struct kvm_vcpu *v);
a1b37100
GN
1042int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
1043int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
0b71785d 1044int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
66450a21 1045void kvm_vcpu_reset(struct kvm_vcpu *vcpu);
e930bffe 1046
18863bdd 1047void kvm_define_shared_msr(unsigned index, u32 msr);
d5696725 1048void kvm_set_shared_msr(unsigned index, u64 val, u64 mask);
18863bdd 1049
f92653ee
JK
1050bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip);
1051
af585b92
GN
1052void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
1053 struct kvm_async_pf *work);
1054void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
1055 struct kvm_async_pf *work);
56028d08
GN
1056void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
1057 struct kvm_async_pf *work);
7c90705b 1058bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu);
af585b92
GN
1059extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
1060
db8fcefa
AP
1061void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
1062
f5132b01
GN
1063int kvm_is_in_guest(void);
1064
1065void kvm_pmu_init(struct kvm_vcpu *vcpu);
1066void kvm_pmu_destroy(struct kvm_vcpu *vcpu);
1067void kvm_pmu_reset(struct kvm_vcpu *vcpu);
1068void kvm_pmu_cpuid_update(struct kvm_vcpu *vcpu);
1069bool kvm_pmu_msr(struct kvm_vcpu *vcpu, u32 msr);
1070int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
afd80d85 1071int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info);
67f4d428 1072int kvm_pmu_check_pmc(struct kvm_vcpu *vcpu, unsigned pmc);
f5132b01
GN
1073int kvm_pmu_read_pmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
1074void kvm_handle_pmu_event(struct kvm_vcpu *vcpu);
1075void kvm_deliver_pmi(struct kvm_vcpu *vcpu);
1076
1965aae3 1077#endif /* _ASM_X86_KVM_HOST_H */