]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - arch/x86/include/asm/kvm_host.h
KVM: MMU: cache mmio info on page fault path
[mirror_ubuntu-artful-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>
34c16eec
ZX
19
20#include <linux/kvm.h>
21#include <linux/kvm_para.h>
edf88417 22#include <linux/kvm_types.h>
34c16eec 23
50d0a0f9 24#include <asm/pvclock-abi.h>
e01a1b57 25#include <asm/desc.h>
0bed3b56 26#include <asm/mtrr.h>
9962d032 27#include <asm/msr-index.h>
e01a1b57 28
0680fe52 29#define KVM_MAX_VCPUS 64
69a9f69b
AK
30#define KVM_MEMORY_SLOTS 32
31/* memory slots that does not exposed to userspace */
32#define KVM_PRIVATE_MEM_SLOTS 4
cef4dea0 33#define KVM_MMIO_SIZE 16
69a9f69b
AK
34
35#define KVM_PIO_PAGE_OFFSET 1
542472b5 36#define KVM_COALESCED_MMIO_PAGE_OFFSET 2
69a9f69b 37
cfec82cb
JR
38#define CR0_RESERVED_BITS \
39 (~(unsigned long)(X86_CR0_PE | X86_CR0_MP | X86_CR0_EM | X86_CR0_TS \
40 | X86_CR0_ET | X86_CR0_NE | X86_CR0_WP | X86_CR0_AM \
41 | X86_CR0_NW | X86_CR0_CD | X86_CR0_PG))
42
cd6e8f87
ZX
43#define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
44#define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
7d76b4d3
JP
45#define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS | \
46 0xFFFFFF0000000000ULL)
cfec82cb
JR
47#define CR4_RESERVED_BITS \
48 (~(unsigned long)(X86_CR4_VME | X86_CR4_PVI | X86_CR4_TSD | X86_CR4_DE\
49 | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_MCE \
50 | X86_CR4_PGE | X86_CR4_PCE | X86_CR4_OSFXSR \
d9c3476d 51 | X86_CR4_OSXSAVE | X86_CR4_SMEP | X86_CR4_RDWRGSFS \
cfec82cb
JR
52 | X86_CR4_OSXMMEXCPT | X86_CR4_VMXE))
53
54#define CR8_RESERVED_BITS (~(unsigned long)X86_CR8_TPR)
55
56
cd6e8f87 57
cd6e8f87 58#define INVALID_PAGE (~(hpa_t)0)
dd180b3e
XG
59#define VALID_PAGE(x) ((x) != INVALID_PAGE)
60
cd6e8f87
ZX
61#define UNMAPPED_GVA (~(gpa_t)0)
62
ec04b260 63/* KVM Hugepage definitions for x86 */
04326caa 64#define KVM_NR_PAGE_SIZES 3
82855413
JR
65#define KVM_HPAGE_GFN_SHIFT(x) (((x) - 1) * 9)
66#define KVM_HPAGE_SHIFT(x) (PAGE_SHIFT + KVM_HPAGE_GFN_SHIFT(x))
ec04b260
JR
67#define KVM_HPAGE_SIZE(x) (1UL << KVM_HPAGE_SHIFT(x))
68#define KVM_HPAGE_MASK(x) (~(KVM_HPAGE_SIZE(x) - 1))
69#define KVM_PAGES_PER_HPAGE(x) (KVM_HPAGE_SIZE(x) / PAGE_SIZE)
05da4558 70
cd6e8f87 71#define DE_VECTOR 0
19bd8afd 72#define DB_VECTOR 1
77ab6db0
JK
73#define BP_VECTOR 3
74#define OF_VECTOR 4
75#define BR_VECTOR 5
cd6e8f87
ZX
76#define UD_VECTOR 6
77#define NM_VECTOR 7
78#define DF_VECTOR 8
79#define TS_VECTOR 10
80#define NP_VECTOR 11
81#define SS_VECTOR 12
82#define GP_VECTOR 13
83#define PF_VECTOR 14
77ab6db0 84#define MF_VECTOR 16
53371b50 85#define MC_VECTOR 18
cd6e8f87
ZX
86
87#define SELECTOR_TI_MASK (1 << 2)
88#define SELECTOR_RPL_MASK 0x03
89
90#define IOPL_SHIFT 12
91
d657a98e
ZX
92#define KVM_PERMILLE_MMU_PAGES 20
93#define KVM_MIN_ALLOC_MMU_PAGES 64
1ae0a13d
DE
94#define KVM_MMU_HASH_SHIFT 10
95#define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
d657a98e
ZX
96#define KVM_MIN_FREE_MMU_PAGES 5
97#define KVM_REFILL_PAGES 25
73c1160c 98#define KVM_MAX_CPUID_ENTRIES 80
0bed3b56 99#define KVM_NR_FIXED_MTRR_REGION 88
9ba075a6 100#define KVM_NR_VAR_MTRR 8
d657a98e 101
af585b92
GN
102#define ASYNC_PF_PER_VCPU 64
103
e935b837 104extern raw_spinlock_t kvm_lock;
e9b11c17
ZX
105extern struct list_head vm_list;
106
d657a98e
ZX
107struct kvm_vcpu;
108struct kvm;
af585b92 109struct kvm_async_pf;
d657a98e 110
5fdbf976 111enum kvm_reg {
2b3ccfa0
ZX
112 VCPU_REGS_RAX = 0,
113 VCPU_REGS_RCX = 1,
114 VCPU_REGS_RDX = 2,
115 VCPU_REGS_RBX = 3,
116 VCPU_REGS_RSP = 4,
117 VCPU_REGS_RBP = 5,
118 VCPU_REGS_RSI = 6,
119 VCPU_REGS_RDI = 7,
120#ifdef CONFIG_X86_64
121 VCPU_REGS_R8 = 8,
122 VCPU_REGS_R9 = 9,
123 VCPU_REGS_R10 = 10,
124 VCPU_REGS_R11 = 11,
125 VCPU_REGS_R12 = 12,
126 VCPU_REGS_R13 = 13,
127 VCPU_REGS_R14 = 14,
128 VCPU_REGS_R15 = 15,
129#endif
5fdbf976 130 VCPU_REGS_RIP,
2b3ccfa0
ZX
131 NR_VCPU_REGS
132};
133
6de4f3ad
AK
134enum kvm_reg_ex {
135 VCPU_EXREG_PDPTR = NR_VCPU_REGS,
aff48baa 136 VCPU_EXREG_CR3,
6de12732 137 VCPU_EXREG_RFLAGS,
69c73028 138 VCPU_EXREG_CPL,
2fb92db1 139 VCPU_EXREG_SEGMENTS,
6de4f3ad
AK
140};
141
2b3ccfa0 142enum {
81609e3e 143 VCPU_SREG_ES,
2b3ccfa0 144 VCPU_SREG_CS,
81609e3e 145 VCPU_SREG_SS,
2b3ccfa0 146 VCPU_SREG_DS,
2b3ccfa0
ZX
147 VCPU_SREG_FS,
148 VCPU_SREG_GS,
2b3ccfa0
ZX
149 VCPU_SREG_TR,
150 VCPU_SREG_LDTR,
151};
152
56e82318 153#include <asm/kvm_emulate.h>
2b3ccfa0 154
d657a98e
ZX
155#define KVM_NR_MEM_OBJS 40
156
42dbaa5a
JK
157#define KVM_NR_DB_REGS 4
158
159#define DR6_BD (1 << 13)
160#define DR6_BS (1 << 14)
161#define DR6_FIXED_1 0xffff0ff0
162#define DR6_VOLATILE 0x0000e00f
163
164#define DR7_BP_EN_MASK 0x000000ff
165#define DR7_GE (1 << 9)
166#define DR7_GD (1 << 13)
167#define DR7_FIXED_1 0x00000400
168#define DR7_VOLATILE 0xffff23ff
169
d657a98e
ZX
170/*
171 * We don't want allocation failures within the mmu code, so we preallocate
172 * enough memory for a single page fault in a cache.
173 */
174struct kvm_mmu_memory_cache {
175 int nobjs;
176 void *objects[KVM_NR_MEM_OBJS];
177};
178
179#define NR_PTE_CHAIN_ENTRIES 5
180
181struct kvm_pte_chain {
182 u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
183 struct hlist_node link;
184};
185
186/*
187 * kvm_mmu_page_role, below, is defined as:
188 *
189 * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
190 * bits 4:7 - page table level for this shadow (1-4)
191 * bits 8:9 - page table quadrant for 2-level guests
f6e2c02b
AK
192 * bit 16 - direct mapping of virtual to physical mapping at gfn
193 * used for real mode and two-dimensional paging
d657a98e
ZX
194 * bits 17:19 - common access permissions for all ptes in this shadow page
195 */
196union kvm_mmu_page_role {
197 unsigned word;
198 struct {
7d76b4d3 199 unsigned level:4;
5b7e0102 200 unsigned cr4_pae:1;
7d76b4d3
JP
201 unsigned quadrant:2;
202 unsigned pad_for_nice_hex_output:6;
f6e2c02b 203 unsigned direct:1;
7d76b4d3 204 unsigned access:3;
2e53d63a 205 unsigned invalid:1;
9645bb56 206 unsigned nxe:1;
3dbe1415 207 unsigned cr0_wp:1;
411c588d 208 unsigned smep_andnot_wp:1;
d657a98e
ZX
209 };
210};
211
212struct kvm_mmu_page {
213 struct list_head link;
214 struct hlist_node hash_link;
215
216 /*
217 * The following two entries are used to key the shadow page in the
218 * hash table.
219 */
220 gfn_t gfn;
221 union kvm_mmu_page_role role;
222
223 u64 *spt;
224 /* hold the gfn of each spte inside spt */
225 gfn_t *gfns;
291f26bc
SY
226 /*
227 * One bit set per slot which has memory
228 * in this shadow page.
229 */
230 DECLARE_BITMAP(slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
4731d4c7 231 bool unsync;
0571d366 232 int root_count; /* Currently serving as active root */
60c8aec6 233 unsigned int unsync_children;
67052b35 234 unsigned long parent_ptes; /* Reverse mapping for parent_pte */
0074ff63 235 DECLARE_BITMAP(unsync_child_bitmap, 512);
d657a98e
ZX
236};
237
6ad18fba
DH
238struct kvm_pv_mmu_op_buffer {
239 void *ptr;
240 unsigned len;
241 unsigned processed;
242 char buf[512] __aligned(sizeof(long));
243};
244
1c08364c
AK
245struct kvm_pio_request {
246 unsigned long count;
1c08364c
AK
247 int in;
248 int port;
249 int size;
1c08364c
AK
250};
251
d657a98e
ZX
252/*
253 * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
254 * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
255 * mode.
256 */
257struct kvm_mmu {
258 void (*new_cr3)(struct kvm_vcpu *vcpu);
f43addd4 259 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long root);
5777ed34 260 unsigned long (*get_cr3)(struct kvm_vcpu *vcpu);
78b2c54a
XG
261 int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err,
262 bool prefault);
6389ee94
AK
263 void (*inject_page_fault)(struct kvm_vcpu *vcpu,
264 struct x86_exception *fault);
d657a98e 265 void (*free)(struct kvm_vcpu *vcpu);
1871c602 266 gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva, u32 access,
ab9ae313 267 struct x86_exception *exception);
c30a358d 268 gpa_t (*translate_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa, u32 access);
d657a98e
ZX
269 void (*prefetch_page)(struct kvm_vcpu *vcpu,
270 struct kvm_mmu_page *page);
e8bc217a 271 int (*sync_page)(struct kvm_vcpu *vcpu,
a4a8e6f7 272 struct kvm_mmu_page *sp);
a7052897 273 void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
0f53b5b1 274 void (*update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
7c562522 275 u64 *spte, const void *pte);
d657a98e
ZX
276 hpa_t root_hpa;
277 int root_level;
278 int shadow_root_level;
a770f6f2 279 union kvm_mmu_page_role base_role;
c5a78f2b 280 bool direct_map;
d657a98e
ZX
281
282 u64 *pae_root;
81407ca5 283 u64 *lm_root;
82725b20 284 u64 rsvd_bits_mask[2][4];
ff03a073 285
2d48a985
JR
286 bool nx;
287
ff03a073 288 u64 pdptrs[4]; /* pae */
d657a98e
ZX
289};
290
ad312c7c 291struct kvm_vcpu_arch {
5fdbf976
MT
292 /*
293 * rip and regs accesses must go through
294 * kvm_{register,rip}_{read,write} functions.
295 */
296 unsigned long regs[NR_VCPU_REGS];
297 u32 regs_avail;
298 u32 regs_dirty;
34c16eec
ZX
299
300 unsigned long cr0;
e8467fda 301 unsigned long cr0_guest_owned_bits;
34c16eec
ZX
302 unsigned long cr2;
303 unsigned long cr3;
304 unsigned long cr4;
fc78f519 305 unsigned long cr4_guest_owned_bits;
34c16eec 306 unsigned long cr8;
1371d904 307 u32 hflags;
f6801dff 308 u64 efer;
34c16eec
ZX
309 u64 apic_base;
310 struct kvm_lapic *apic; /* kernel irqchip context */
e1035715 311 int32_t apic_arb_prio;
34c16eec
ZX
312 int mp_state;
313 int sipi_vector;
314 u64 ia32_misc_enable_msr;
b209749f 315 bool tpr_access_reporting;
34c16eec 316
14dfe855
JR
317 /*
318 * Paging state of the vcpu
319 *
320 * If the vcpu runs in guest mode with two level paging this still saves
321 * the paging mode of the l1 guest. This context is always used to
322 * handle faults.
323 */
34c16eec 324 struct kvm_mmu mmu;
8df25a32 325
6539e738
JR
326 /*
327 * Paging state of an L2 guest (used for nested npt)
328 *
329 * This context will save all necessary information to walk page tables
330 * of the an L2 guest. This context is only initialized for page table
331 * walking and not for faulting since we never handle l2 page faults on
332 * the host.
333 */
334 struct kvm_mmu nested_mmu;
335
14dfe855
JR
336 /*
337 * Pointer to the mmu context currently used for
338 * gva_to_gpa translations.
339 */
340 struct kvm_mmu *walk_mmu;
341
6ad18fba
DH
342 /* only needed in kvm_pv_mmu_op() path, but it's hot so
343 * put it here to avoid allocation */
344 struct kvm_pv_mmu_op_buffer mmu_op_buffer;
34c16eec 345
53c07b18 346 struct kvm_mmu_memory_cache mmu_pte_list_desc_cache;
34c16eec
ZX
347 struct kvm_mmu_memory_cache mmu_page_cache;
348 struct kvm_mmu_memory_cache mmu_page_header_cache;
349
350 gfn_t last_pt_write_gfn;
351 int last_pt_write_count;
352 u64 *last_pte_updated;
1b7fcd32 353 gfn_t last_pte_gfn;
34c16eec 354
98918833 355 struct fpu guest_fpu;
2acf923e 356 u64 xcr0;
34c16eec 357
34c16eec
ZX
358 struct kvm_pio_request pio;
359 void *pio_data;
360
66fd3f7f
GN
361 u8 event_exit_inst_len;
362
298101da
AK
363 struct kvm_queued_exception {
364 bool pending;
365 bool has_error_code;
ce7ddec4 366 bool reinject;
298101da
AK
367 u8 nr;
368 u32 error_code;
369 } exception;
370
937a7eae
AK
371 struct kvm_queued_interrupt {
372 bool pending;
66fd3f7f 373 bool soft;
937a7eae
AK
374 u8 nr;
375 } interrupt;
376
34c16eec
ZX
377 int halt_request; /* real mode on Intel only */
378
379 int cpuid_nent;
07716717 380 struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
34c16eec
ZX
381 /* emulate context */
382
383 struct x86_emulate_ctxt emulate_ctxt;
7ae441ea
GN
384 bool emulate_regs_need_sync_to_vcpu;
385 bool emulate_regs_need_sync_from_vcpu;
18068523
GOC
386
387 gpa_t time;
50d0a0f9 388 struct pvclock_vcpu_time_info hv_clock;
e48672fa 389 unsigned int hw_tsc_khz;
18068523
GOC
390 unsigned int time_offset;
391 struct page *time_page;
c9aaa895
GC
392
393 struct {
394 u64 msr_val;
395 u64 last_steal;
396 u64 accum_steal;
397 struct gfn_to_hva_cache stime;
398 struct kvm_steal_time steal;
399 } st;
400
1d5f066e
ZA
401 u64 last_guest_tsc;
402 u64 last_kernel_ns;
c285545f
ZA
403 u64 last_tsc_nsec;
404 u64 last_tsc_write;
1e993611 405 u32 virtual_tsc_khz;
c285545f 406 bool tsc_catchup;
1e993611
JR
407 u32 tsc_catchup_mult;
408 s8 tsc_catchup_shift;
3419ffc8
SY
409
410 bool nmi_pending;
668f612f 411 bool nmi_injected;
9ba075a6 412
0bed3b56
SY
413 struct mtrr_state_type mtrr_state;
414 u32 pat;
42dbaa5a
JK
415
416 int switch_db_regs;
42dbaa5a
JK
417 unsigned long db[KVM_NR_DB_REGS];
418 unsigned long dr6;
419 unsigned long dr7;
420 unsigned long eff_db[KVM_NR_DB_REGS];
890ca9ae
HY
421
422 u64 mcg_cap;
423 u64 mcg_status;
424 u64 mcg_ctl;
425 u64 *mce_banks;
94fe45da 426
bebb106a
XG
427 /* Cache MMIO info */
428 u64 mmio_gva;
429 unsigned access;
430 gfn_t mmio_gfn;
431
94fe45da 432 /* used for guest single stepping over the given code position */
94fe45da 433 unsigned long singlestep_rip;
f92653ee 434
10388a07
GN
435 /* fields used by HYPER-V emulation */
436 u64 hv_vapic;
f5f48ee1
SY
437
438 cpumask_var_t wbinvd_dirty_mask;
af585b92
GN
439
440 struct {
441 bool halted;
442 gfn_t gfns[roundup_pow_of_two(ASYNC_PF_PER_VCPU)];
344d9588
GN
443 struct gfn_to_hva_cache data;
444 u64 msr_val;
7c90705b 445 u32 id;
6adba527 446 bool send_user_only;
af585b92 447 } apf;
34c16eec
ZX
448};
449
fef9cce0 450struct kvm_arch {
49d5ca26 451 unsigned int n_used_mmu_pages;
f05e70ac 452 unsigned int n_requested_mmu_pages;
39de71ec 453 unsigned int n_max_mmu_pages;
332b207d 454 unsigned int indirect_shadow_pages;
08e850c6 455 atomic_t invlpg_counter;
f05e70ac
ZX
456 struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
457 /*
458 * Hash table of struct kvm_mmu_page.
459 */
460 struct list_head active_mmu_pages;
4d5c5d0f 461 struct list_head assigned_dev_head;
19de40a8 462 struct iommu_domain *iommu_domain;
522c68c4 463 int iommu_flags;
d7deeeb0
ZX
464 struct kvm_pic *vpic;
465 struct kvm_ioapic *vioapic;
7837699f 466 struct kvm_pit *vpit;
cc6e462c 467 int vapics_in_nmi_mode;
bfc6d222 468
bfc6d222
ZX
469 unsigned int tss_addr;
470 struct page *apic_access_page;
18068523
GOC
471
472 gpa_t wall_clock;
b7ebfb05
SY
473
474 struct page *ept_identity_pagetable;
475 bool ept_identity_pagetable_done;
b927a3ce 476 gpa_t ept_identity_map_addr;
5550af4d
SY
477
478 unsigned long irq_sources_bitmap;
afbcf7ab 479 s64 kvmclock_offset;
038f8c11 480 raw_spinlock_t tsc_write_lock;
f38e098f
ZA
481 u64 last_tsc_nsec;
482 u64 last_tsc_offset;
483 u64 last_tsc_write;
ffde22ac
ES
484
485 struct kvm_xen_hvm_config xen_hvm_config;
55cd8e5a
GN
486
487 /* fields used by HYPER-V emulation */
488 u64 hv_guest_os_id;
489 u64 hv_hypercall;
b034cf01
XG
490
491 #ifdef CONFIG_KVM_MMU_AUDIT
492 int audit_point;
493 #endif
d69fb81f
ZX
494};
495
0711456c
ZX
496struct kvm_vm_stat {
497 u32 mmu_shadow_zapped;
498 u32 mmu_pte_write;
499 u32 mmu_pte_updated;
500 u32 mmu_pde_zapped;
501 u32 mmu_flooded;
502 u32 mmu_recycled;
dfc5aa00 503 u32 mmu_cache_miss;
4731d4c7 504 u32 mmu_unsync;
0711456c 505 u32 remote_tlb_flush;
05da4558 506 u32 lpages;
0711456c
ZX
507};
508
77b4c255
ZX
509struct kvm_vcpu_stat {
510 u32 pf_fixed;
511 u32 pf_guest;
512 u32 tlb_flush;
513 u32 invlpg;
514
515 u32 exits;
516 u32 io_exits;
517 u32 mmio_exits;
518 u32 signal_exits;
519 u32 irq_window_exits;
f08864b4 520 u32 nmi_window_exits;
77b4c255
ZX
521 u32 halt_exits;
522 u32 halt_wakeup;
523 u32 request_irq_exits;
524 u32 irq_exits;
525 u32 host_state_reload;
526 u32 efer_reload;
527 u32 fpu_reload;
528 u32 insn_emulation;
529 u32 insn_emulation_fail;
f11c3a8d 530 u32 hypercalls;
fa89a817 531 u32 irq_injections;
c4abb7c9 532 u32 nmi_injections;
77b4c255 533};
ad312c7c 534
8a76d7f2
JR
535struct x86_instruction_info;
536
ea4a5ff8
ZX
537struct kvm_x86_ops {
538 int (*cpu_has_kvm_support)(void); /* __init */
539 int (*disabled_by_bios)(void); /* __init */
10474ae8 540 int (*hardware_enable)(void *dummy);
ea4a5ff8
ZX
541 void (*hardware_disable)(void *dummy);
542 void (*check_processor_compatibility)(void *rtn);
543 int (*hardware_setup)(void); /* __init */
544 void (*hardware_unsetup)(void); /* __exit */
774ead3a 545 bool (*cpu_has_accelerated_tpr)(void);
0e851880 546 void (*cpuid_update)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
547
548 /* Create, but do not attach this VCPU */
549 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
550 void (*vcpu_free)(struct kvm_vcpu *vcpu);
551 int (*vcpu_reset)(struct kvm_vcpu *vcpu);
552
553 void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
554 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
555 void (*vcpu_put)(struct kvm_vcpu *vcpu);
ea4a5ff8 556
355be0b9
JK
557 void (*set_guest_debug)(struct kvm_vcpu *vcpu,
558 struct kvm_guest_debug *dbg);
ea4a5ff8
ZX
559 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
560 int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
561 u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
562 void (*get_segment)(struct kvm_vcpu *vcpu,
563 struct kvm_segment *var, int seg);
2e4d2653 564 int (*get_cpl)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
565 void (*set_segment)(struct kvm_vcpu *vcpu,
566 struct kvm_segment *var, int seg);
567 void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
e8467fda 568 void (*decache_cr0_guest_bits)(struct kvm_vcpu *vcpu);
aff48baa 569 void (*decache_cr3)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
570 void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
571 void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
572 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
5e1746d6 573 int (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
ea4a5ff8 574 void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
89a27f4d
GN
575 void (*get_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
576 void (*set_idt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
577 void (*get_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
578 void (*set_gdt)(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
020df079 579 void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
5fdbf976 580 void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
ea4a5ff8
ZX
581 unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
582 void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
6b52d186 583 void (*fpu_activate)(struct kvm_vcpu *vcpu);
02daab21 584 void (*fpu_deactivate)(struct kvm_vcpu *vcpu);
ea4a5ff8
ZX
585
586 void (*tlb_flush)(struct kvm_vcpu *vcpu);
ea4a5ff8 587
851ba692
AK
588 void (*run)(struct kvm_vcpu *vcpu);
589 int (*handle_exit)(struct kvm_vcpu *vcpu);
ea4a5ff8 590 void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
2809f5d2
GC
591 void (*set_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
592 u32 (*get_interrupt_shadow)(struct kvm_vcpu *vcpu, int mask);
ea4a5ff8
ZX
593 void (*patch_hypercall)(struct kvm_vcpu *vcpu,
594 unsigned char *hypercall_addr);
66fd3f7f 595 void (*set_irq)(struct kvm_vcpu *vcpu);
95ba8273 596 void (*set_nmi)(struct kvm_vcpu *vcpu);
298101da 597 void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
ce7ddec4
JR
598 bool has_error_code, u32 error_code,
599 bool reinject);
b463a6f7 600 void (*cancel_injection)(struct kvm_vcpu *vcpu);
78646121 601 int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
95ba8273 602 int (*nmi_allowed)(struct kvm_vcpu *vcpu);
3cfc3092
JK
603 bool (*get_nmi_mask)(struct kvm_vcpu *vcpu);
604 void (*set_nmi_mask)(struct kvm_vcpu *vcpu, bool masked);
95ba8273
GN
605 void (*enable_nmi_window)(struct kvm_vcpu *vcpu);
606 void (*enable_irq_window)(struct kvm_vcpu *vcpu);
607 void (*update_cr8_intercept)(struct kvm_vcpu *vcpu, int tpr, int irr);
ea4a5ff8 608 int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
67253af5 609 int (*get_tdp_level)(void);
4b12f0de 610 u64 (*get_mt_mask)(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio);
17cc3935 611 int (*get_lpage_level)(void);
4e47c7a6 612 bool (*rdtscp_supported)(void);
e48672fa 613 void (*adjust_tsc_offset)(struct kvm_vcpu *vcpu, s64 adjustment);
344f414f 614
1c97f0a0
JR
615 void (*set_tdp_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
616
d4330ef2
JR
617 void (*set_supported_cpuid)(u32 func, struct kvm_cpuid_entry2 *entry);
618
f5f48ee1
SY
619 bool (*has_wbinvd_exit)(void);
620
4051b188 621 void (*set_tsc_khz)(struct kvm_vcpu *vcpu, u32 user_tsc_khz);
99e3e30a
ZA
622 void (*write_tsc_offset)(struct kvm_vcpu *vcpu, u64 offset);
623
857e4099
JR
624 u64 (*compute_tsc_offset)(struct kvm_vcpu *vcpu, u64 target_tsc);
625
586f9607 626 void (*get_exit_info)(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2);
8a76d7f2
JR
627
628 int (*check_intercept)(struct kvm_vcpu *vcpu,
629 struct x86_instruction_info *info,
630 enum x86_intercept_stage stage);
631
229456fc 632 const struct trace_print_flags *exit_reasons_str;
ea4a5ff8
ZX
633};
634
af585b92 635struct kvm_arch_async_pf {
7c90705b 636 u32 token;
af585b92 637 gfn_t gfn;
fb67e14f 638 unsigned long cr3;
c4806acd 639 bool direct_map;
af585b92
GN
640};
641
97896d04
ZX
642extern struct kvm_x86_ops *kvm_x86_ops;
643
54f1585a
ZX
644int kvm_mmu_module_init(void);
645void kvm_mmu_module_exit(void);
646
647void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
648int kvm_mmu_create(struct kvm_vcpu *vcpu);
649int kvm_mmu_setup(struct kvm_vcpu *vcpu);
650void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
7b52345e 651void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
4b12f0de 652 u64 dirty_mask, u64 nx_mask, u64 x_mask);
54f1585a
ZX
653
654int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
655void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
656void kvm_mmu_zap_all(struct kvm *kvm);
3ad82a7e 657unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
54f1585a
ZX
658void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
659
ff03a073 660int load_pdptrs(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu, unsigned long cr3);
cc4b6871 661
3200f405 662int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
9f811285 663 const void *val, int bytes);
2f333bcb
MT
664int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
665 gpa_t addr, unsigned long *ret);
4b12f0de 666u8 kvm_get_guest_memory_type(struct kvm_vcpu *vcpu, gfn_t gfn);
2f333bcb
MT
667
668extern bool tdp_enabled;
9f811285 669
92a1f12d
JR
670/* control of guest tsc rate supported? */
671extern bool kvm_has_tsc_control;
672/* minimum supported tsc_khz for guests */
673extern u32 kvm_min_guest_tsc_khz;
674/* maximum supported tsc_khz for guests */
675extern u32 kvm_max_guest_tsc_khz;
676
54f1585a
ZX
677enum emulation_result {
678 EMULATE_DONE, /* no further processing */
679 EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
680 EMULATE_FAIL, /* can't emulate this instruction */
681};
682
571008da
SY
683#define EMULTYPE_NO_DECODE (1 << 0)
684#define EMULTYPE_TRAP_UD (1 << 1)
ba8afb6b 685#define EMULTYPE_SKIP (1 << 2)
dc25e89e
AP
686int x86_emulate_instruction(struct kvm_vcpu *vcpu, unsigned long cr2,
687 int emulation_type, void *insn, int insn_len);
51d8b661
AP
688
689static inline int emulate_instruction(struct kvm_vcpu *vcpu,
690 int emulation_type)
691{
dc25e89e 692 return x86_emulate_instruction(vcpu, 0, emulation_type, NULL, 0);
51d8b661
AP
693}
694
f2b4b7dd 695void kvm_enable_efer_bits(u64);
54f1585a
ZX
696int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
697int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
698
699struct x86_emulate_ctxt;
700
cf8f70bf 701int kvm_fast_pio_out(struct kvm_vcpu *vcpu, int size, unsigned short port);
54f1585a
ZX
702void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
703int kvm_emulate_halt(struct kvm_vcpu *vcpu);
f5f48ee1 704int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu);
54f1585a 705
3e6e0aab 706void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
c697518a 707int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector, int seg);
3e6e0aab 708
e269fb21
JK
709int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason,
710 bool has_error_code, u32 error_code);
37817f29 711
49a9b07e 712int kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
2390218b 713int kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
a83b29c6 714int kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
eea1cff9 715int kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
020df079
GN
716int kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val);
717int kvm_get_dr(struct kvm_vcpu *vcpu, int dr, unsigned long *val);
2d3ad1f4
AK
718unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
719void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
54f1585a 720void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
2acf923e 721int kvm_set_xcr(struct kvm_vcpu *vcpu, u32 index, u64 xcr);
54f1585a
ZX
722
723int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
724int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
725
91586a3b
JK
726unsigned long kvm_get_rflags(struct kvm_vcpu *vcpu);
727void kvm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
728
298101da
AK
729void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
730void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
ce7ddec4
JR
731void kvm_requeue_exception(struct kvm_vcpu *vcpu, unsigned nr);
732void kvm_requeue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
6389ee94 733void kvm_inject_page_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
ec92fe44
JR
734int kvm_read_guest_page_mmu(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
735 gfn_t gfn, void *data, int offset, int len,
736 u32 access);
6389ee94 737void kvm_propagate_fault(struct kvm_vcpu *vcpu, struct x86_exception *fault);
0a79b009 738bool kvm_require_cpl(struct kvm_vcpu *vcpu, int required_cpl);
298101da 739
4925663a 740int kvm_pic_set_irq(void *opaque, int irq, int level);
3de42dc0 741
3419ffc8
SY
742void kvm_inject_nmi(struct kvm_vcpu *vcpu);
743
10ab25cd 744int fx_init(struct kvm_vcpu *vcpu);
54f1585a 745
d835dfec 746void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
54f1585a 747void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
ad218f85
MT
748 const u8 *new, int bytes,
749 bool guest_initiated);
54f1585a
ZX
750int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
751void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
752int kvm_mmu_load(struct kvm_vcpu *vcpu);
753void kvm_mmu_unload(struct kvm_vcpu *vcpu);
0ba73cda 754void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
ab9ae313
AK
755gpa_t kvm_mmu_gva_to_gpa_read(struct kvm_vcpu *vcpu, gva_t gva,
756 struct x86_exception *exception);
757gpa_t kvm_mmu_gva_to_gpa_fetch(struct kvm_vcpu *vcpu, gva_t gva,
758 struct x86_exception *exception);
759gpa_t kvm_mmu_gva_to_gpa_write(struct kvm_vcpu *vcpu, gva_t gva,
760 struct x86_exception *exception);
761gpa_t kvm_mmu_gva_to_gpa_system(struct kvm_vcpu *vcpu, gva_t gva,
762 struct x86_exception *exception);
54f1585a
ZX
763
764int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
765
dc25e89e
AP
766int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code,
767 void *insn, int insn_len);
a7052897 768void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
34c16eec 769
18552672 770void kvm_enable_tdp(void);
5f4cb662 771void kvm_disable_tdp(void);
18552672 772
de7d789a 773int complete_pio(struct kvm_vcpu *vcpu);
f850e2e6 774bool kvm_check_iopl(struct kvm_vcpu *vcpu);
ec6d273d
ZX
775
776static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
777{
778 struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
779
780 return (struct kvm_mmu_page *)page_private(page);
781}
782
d6e88aec 783static inline u16 kvm_read_ldt(void)
ec6d273d
ZX
784{
785 u16 ldt;
786 asm("sldt %0" : "=g"(ldt));
787 return ldt;
788}
789
d6e88aec 790static inline void kvm_load_ldt(u16 sel)
ec6d273d
ZX
791{
792 asm("lldt %0" : : "rm"(sel));
793}
ec6d273d 794
ec6d273d
ZX
795#ifdef CONFIG_X86_64
796static inline unsigned long read_msr(unsigned long msr)
797{
798 u64 value;
799
800 rdmsrl(msr, value);
801 return value;
802}
803#endif
804
ec6d273d
ZX
805static inline u32 get_rdx_init_val(void)
806{
807 return 0x600; /* P6 family */
808}
809
c1a5d4f9
AK
810static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
811{
812 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
813}
814
ec6d273d
ZX
815#define TSS_IOPB_BASE_OFFSET 0x66
816#define TSS_BASE_SIZE 0x68
817#define TSS_IOPB_SIZE (65536 / 8)
818#define TSS_REDIRECTION_SIZE (256 / 8)
7d76b4d3
JP
819#define RMODE_TSS_SIZE \
820 (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
53e0aa7b 821
37817f29
IE
822enum {
823 TASK_SWITCH_CALL = 0,
824 TASK_SWITCH_IRET = 1,
825 TASK_SWITCH_JMP = 2,
826 TASK_SWITCH_GATE = 3,
827};
828
1371d904 829#define HF_GIF_MASK (1 << 0)
3d6368ef
AG
830#define HF_HIF_MASK (1 << 1)
831#define HF_VINTR_MASK (1 << 2)
95ba8273 832#define HF_NMI_MASK (1 << 3)
44c11430 833#define HF_IRET_MASK (1 << 4)
ec9e60b2 834#define HF_GUEST_MASK (1 << 5) /* VCPU is in guest-mode */
1371d904 835
4ecac3fd
AK
836/*
837 * Hardware virtualization extension instructions may fault if a
838 * reboot turns off virtualization while processes are running.
839 * Trap the fault and ignore the instruction if that happens.
840 */
b7c4145b
AK
841asmlinkage void kvm_spurious_fault(void);
842extern bool kvm_rebooting;
4ecac3fd 843
5e520e62 844#define ____kvm_handle_fault_on_reboot(insn, cleanup_insn) \
4ecac3fd 845 "666: " insn "\n\t" \
b7c4145b 846 "668: \n\t" \
18b13e54 847 ".pushsection .fixup, \"ax\" \n" \
4ecac3fd 848 "667: \n\t" \
5e520e62 849 cleanup_insn "\n\t" \
b7c4145b
AK
850 "cmpb $0, kvm_rebooting \n\t" \
851 "jne 668b \n\t" \
8ceed347 852 __ASM_SIZE(push) " $666b \n\t" \
b7c4145b 853 "call kvm_spurious_fault \n\t" \
4ecac3fd
AK
854 ".popsection \n\t" \
855 ".pushsection __ex_table, \"a\" \n\t" \
8ceed347 856 _ASM_PTR " 666b, 667b \n\t" \
4ecac3fd
AK
857 ".popsection"
858
5e520e62
AK
859#define __kvm_handle_fault_on_reboot(insn) \
860 ____kvm_handle_fault_on_reboot(insn, "")
861
e930bffe
AA
862#define KVM_ARCH_WANT_MMU_NOTIFIER
863int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
864int kvm_age_hva(struct kvm *kvm, unsigned long hva);
8ee53820 865int kvm_test_age_hva(struct kvm *kvm, unsigned long hva);
3da0dd43 866void kvm_set_spte_hva(struct kvm *kvm, unsigned long hva, pte_t pte);
82725b20 867int cpuid_maxphyaddr(struct kvm_vcpu *vcpu);
a1b37100
GN
868int kvm_cpu_has_interrupt(struct kvm_vcpu *vcpu);
869int kvm_arch_interrupt_allowed(struct kvm_vcpu *vcpu);
0b71785d 870int kvm_cpu_get_interrupt(struct kvm_vcpu *v);
e930bffe 871
18863bdd 872void kvm_define_shared_msr(unsigned index, u32 msr);
d5696725 873void kvm_set_shared_msr(unsigned index, u64 val, u64 mask);
18863bdd 874
f92653ee
JK
875bool kvm_is_linear_rip(struct kvm_vcpu *vcpu, unsigned long linear_rip);
876
af585b92
GN
877void kvm_arch_async_page_not_present(struct kvm_vcpu *vcpu,
878 struct kvm_async_pf *work);
879void kvm_arch_async_page_present(struct kvm_vcpu *vcpu,
880 struct kvm_async_pf *work);
56028d08
GN
881void kvm_arch_async_page_ready(struct kvm_vcpu *vcpu,
882 struct kvm_async_pf *work);
7c90705b 883bool kvm_arch_can_inject_async_page_present(struct kvm_vcpu *vcpu);
af585b92
GN
884extern bool kvm_find_async_pf_gfn(struct kvm_vcpu *vcpu, gfn_t gfn);
885
db8fcefa
AP
886void kvm_complete_insn_gp(struct kvm_vcpu *vcpu, int err);
887
1965aae3 888#endif /* _ASM_X86_KVM_HOST_H */