kvm: Add x86 kvm headers
[qemu-kvm/fedora.git] / kvm / kernel / arch / x86 / include / asm / kvm_host.h
blob07e10584867e545501c3a092ae938cee25bb54a3
1 #ifndef KVM_UNIFDEF_H
2 #define KVM_UNIFDEF_H
4 #ifdef __i386__
5 #ifndef CONFIG_X86_32
6 #define CONFIG_X86_32 1
7 #endif
8 #endif
10 #ifdef __x86_64__
11 #ifndef CONFIG_X86_64
12 #define CONFIG_X86_64 1
13 #endif
14 #endif
16 #if defined(__i386__) || defined (__x86_64__)
17 #ifndef CONFIG_X86
18 #define CONFIG_X86 1
19 #endif
20 #endif
22 #ifdef __ia64__
23 #ifndef CONFIG_IA64
24 #define CONFIG_IA64 1
25 #endif
26 #endif
28 #ifdef __PPC__
29 #ifndef CONFIG_PPC
30 #define CONFIG_PPC 1
31 #endif
32 #endif
34 #ifdef __s390__
35 #ifndef CONFIG_S390
36 #define CONFIG_S390 1
37 #endif
38 #endif
40 #endif
42 * Kernel-based Virtual Machine driver for Linux
44 * This header defines architecture specific interfaces, x86 version
46 * This work is licensed under the terms of the GNU GPL, version 2. See
47 * the COPYING file in the top-level directory.
51 #ifndef _ASM_X86_KVM_HOST_H
52 #define _ASM_X86_KVM_HOST_H
54 #include <linux/types.h>
55 #include <linux/mm.h>
56 #include <linux/mmu_notifier.h>
58 #include <linux/kvm.h>
59 #include <linux/kvm_para.h>
60 #include <linux/kvm_types.h>
62 #include <asm/pvclock-abi.h>
63 #include <asm/desc.h>
64 #include <asm/mtrr.h>
65 #include <asm/msr-index.h>
67 #define KVM_MAX_VCPUS 16
68 #define KVM_MEMORY_SLOTS 32
69 /* memory slots that does not exposed to userspace */
70 #define KVM_PRIVATE_MEM_SLOTS 4
72 #define KVM_PIO_PAGE_OFFSET 1
73 #define KVM_COALESCED_MMIO_PAGE_OFFSET 2
75 #define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
76 #define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
77 #define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS | \
78 0xFFFFFF0000000000ULL)
80 #define KVM_GUEST_CR0_MASK \
81 (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE \
82 | X86_CR0_NW | X86_CR0_CD)
83 #define KVM_VM_CR0_ALWAYS_ON \
84 (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE | X86_CR0_TS \
85 | X86_CR0_MP)
86 #define KVM_GUEST_CR4_MASK \
87 (X86_CR4_VME | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_VMXE)
88 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
89 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
91 #define INVALID_PAGE (~(hpa_t)0)
92 #define UNMAPPED_GVA (~(gpa_t)0)
94 /* shadow tables are PAE even on non-PAE hosts */
95 #define KVM_HPAGE_SHIFT 21
96 #define KVM_HPAGE_SIZE (1UL << KVM_HPAGE_SHIFT)
97 #define KVM_HPAGE_MASK (~(KVM_HPAGE_SIZE - 1))
99 #define KVM_PAGES_PER_HPAGE (KVM_HPAGE_SIZE / PAGE_SIZE)
101 #define DE_VECTOR 0
102 #define DB_VECTOR 1
103 #define BP_VECTOR 3
104 #define OF_VECTOR 4
105 #define BR_VECTOR 5
106 #define UD_VECTOR 6
107 #define NM_VECTOR 7
108 #define DF_VECTOR 8
109 #define TS_VECTOR 10
110 #define NP_VECTOR 11
111 #define SS_VECTOR 12
112 #define GP_VECTOR 13
113 #define PF_VECTOR 14
114 #define MF_VECTOR 16
115 #define MC_VECTOR 18
117 #define SELECTOR_TI_MASK (1 << 2)
118 #define SELECTOR_RPL_MASK 0x03
120 #define IOPL_SHIFT 12
122 #define KVM_ALIAS_SLOTS 4
124 #define KVM_PERMILLE_MMU_PAGES 20
125 #define KVM_MIN_ALLOC_MMU_PAGES 64
126 #define KVM_MMU_HASH_SHIFT 10
127 #define KVM_NUM_MMU_PAGES (1 << KVM_MMU_HASH_SHIFT)
128 #define KVM_MIN_FREE_MMU_PAGES 5
129 #define KVM_REFILL_PAGES 25
130 #define KVM_MAX_CPUID_ENTRIES 40
131 #define KVM_NR_FIXED_MTRR_REGION 88
132 #define KVM_NR_VAR_MTRR 8
134 extern spinlock_t kvm_lock;
135 extern struct list_head vm_list;
137 struct kvm_vcpu;
138 struct kvm;
140 enum kvm_reg {
141 VCPU_REGS_RAX = 0,
142 VCPU_REGS_RCX = 1,
143 VCPU_REGS_RDX = 2,
144 VCPU_REGS_RBX = 3,
145 VCPU_REGS_RSP = 4,
146 VCPU_REGS_RBP = 5,
147 VCPU_REGS_RSI = 6,
148 VCPU_REGS_RDI = 7,
149 #ifdef CONFIG_X86_64
150 VCPU_REGS_R8 = 8,
151 VCPU_REGS_R9 = 9,
152 VCPU_REGS_R10 = 10,
153 VCPU_REGS_R11 = 11,
154 VCPU_REGS_R12 = 12,
155 VCPU_REGS_R13 = 13,
156 VCPU_REGS_R14 = 14,
157 VCPU_REGS_R15 = 15,
158 #endif
159 VCPU_REGS_RIP,
160 NR_VCPU_REGS
163 enum {
164 VCPU_SREG_ES,
165 VCPU_SREG_CS,
166 VCPU_SREG_SS,
167 VCPU_SREG_DS,
168 VCPU_SREG_FS,
169 VCPU_SREG_GS,
170 VCPU_SREG_TR,
171 VCPU_SREG_LDTR,
174 #include <asm/kvm_x86_emulate.h>
176 #define KVM_NR_MEM_OBJS 40
178 #define KVM_NR_DB_REGS 4
180 #define DR6_BD (1 << 13)
181 #define DR6_BS (1 << 14)
182 #define DR6_FIXED_1 0xffff0ff0
183 #define DR6_VOLATILE 0x0000e00f
185 #define DR7_BP_EN_MASK 0x000000ff
186 #define DR7_GE (1 << 9)
187 #define DR7_GD (1 << 13)
188 #define DR7_FIXED_1 0x00000400
189 #define DR7_VOLATILE 0xffff23ff
192 * We don't want allocation failures within the mmu code, so we preallocate
193 * enough memory for a single page fault in a cache.
195 struct kvm_mmu_memory_cache {
196 int nobjs;
197 void *objects[KVM_NR_MEM_OBJS];
200 #define NR_PTE_CHAIN_ENTRIES 5
202 struct kvm_pte_chain {
203 u64 *parent_ptes[NR_PTE_CHAIN_ENTRIES];
204 struct hlist_node link;
208 * kvm_mmu_page_role, below, is defined as:
210 * bits 0:3 - total guest paging levels (2-4, or zero for real mode)
211 * bits 4:7 - page table level for this shadow (1-4)
212 * bits 8:9 - page table quadrant for 2-level guests
213 * bit 16 - direct mapping of virtual to physical mapping at gfn
214 * used for real mode and two-dimensional paging
215 * bits 17:19 - common access permissions for all ptes in this shadow page
217 union kvm_mmu_page_role {
218 unsigned word;
219 struct {
220 unsigned glevels:4;
221 unsigned level:4;
222 unsigned quadrant:2;
223 unsigned pad_for_nice_hex_output:6;
224 unsigned direct:1;
225 unsigned access:3;
226 unsigned invalid:1;
227 unsigned cr4_pge:1;
228 unsigned nxe:1;
232 struct kvm_mmu_page {
233 struct list_head link;
234 struct hlist_node hash_link;
236 struct list_head oos_link;
239 * The following two entries are used to key the shadow page in the
240 * hash table.
242 gfn_t gfn;
243 union kvm_mmu_page_role role;
245 u64 *spt;
246 /* hold the gfn of each spte inside spt */
247 gfn_t *gfns;
249 * One bit set per slot which has memory
250 * in this shadow page.
252 DECLARE_BITMAP(slot_bitmap, KVM_MEMORY_SLOTS + KVM_PRIVATE_MEM_SLOTS);
253 int multimapped; /* More than one parent_pte? */
254 int root_count; /* Currently serving as active root */
255 bool unsync;
256 unsigned int unsync_children;
257 union {
258 u64 *parent_pte; /* !multimapped */
259 struct hlist_head parent_ptes; /* multimapped, kvm_pte_chain */
261 DECLARE_BITMAP(unsync_child_bitmap, 512);
264 struct kvm_pv_mmu_op_buffer {
265 void *ptr;
266 unsigned len;
267 unsigned processed;
268 char buf[512] __aligned(sizeof(long));
271 struct kvm_pio_request {
272 unsigned long count;
273 int cur_count;
274 gva_t guest_gva;
275 int in;
276 int port;
277 int size;
278 int string;
279 int down;
280 int rep;
284 * x86 supports 3 paging modes (4-level 64-bit, 3-level 64-bit, and 2-level
285 * 32-bit). The kvm_mmu structure abstracts the details of the current mmu
286 * mode.
288 struct kvm_mmu {
289 void (*new_cr3)(struct kvm_vcpu *vcpu);
290 int (*page_fault)(struct kvm_vcpu *vcpu, gva_t gva, u32 err);
291 void (*free)(struct kvm_vcpu *vcpu);
292 gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, gva_t gva);
293 void (*prefetch_page)(struct kvm_vcpu *vcpu,
294 struct kvm_mmu_page *page);
295 int (*sync_page)(struct kvm_vcpu *vcpu,
296 struct kvm_mmu_page *sp);
297 void (*invlpg)(struct kvm_vcpu *vcpu, gva_t gva);
298 hpa_t root_hpa;
299 int root_level;
300 int shadow_root_level;
301 union kvm_mmu_page_role base_role;
303 u64 *pae_root;
304 u64 rsvd_bits_mask[2][4];
307 struct kvm_vcpu_arch {
308 u64 host_tsc;
309 int interrupt_window_open;
310 unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
311 DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
313 * rip and regs accesses must go through
314 * kvm_{register,rip}_{read,write} functions.
316 unsigned long regs[NR_VCPU_REGS];
317 u32 regs_avail;
318 u32 regs_dirty;
320 unsigned long cr0;
321 unsigned long cr2;
322 unsigned long cr3;
323 unsigned long cr4;
324 unsigned long cr8;
325 u32 hflags;
326 u64 pdptrs[4]; /* pae */
327 u64 shadow_efer;
328 u64 apic_base;
329 struct kvm_lapic *apic; /* kernel irqchip context */
330 int32_t apic_arb_prio;
331 int mp_state;
332 int sipi_vector;
333 u64 ia32_misc_enable_msr;
334 bool tpr_access_reporting;
336 struct kvm_mmu mmu;
337 /* only needed in kvm_pv_mmu_op() path, but it's hot so
338 * put it here to avoid allocation */
339 struct kvm_pv_mmu_op_buffer mmu_op_buffer;
341 struct kvm_mmu_memory_cache mmu_pte_chain_cache;
342 struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
343 struct kvm_mmu_memory_cache mmu_page_cache;
344 struct kvm_mmu_memory_cache mmu_page_header_cache;
346 gfn_t last_pt_write_gfn;
347 int last_pt_write_count;
348 u64 *last_pte_updated;
349 gfn_t last_pte_gfn;
351 struct {
352 gfn_t gfn; /* presumed gfn during guest pte update */
353 pfn_t pfn; /* pfn corresponding to that gfn */
354 int largepage;
355 unsigned long mmu_seq;
356 } update_pte;
358 struct i387_fxsave_struct host_fx_image;
359 struct i387_fxsave_struct guest_fx_image;
361 gva_t mmio_fault_cr2;
362 struct kvm_pio_request pio;
363 void *pio_data;
365 struct kvm_queued_exception {
366 bool pending;
367 bool has_error_code;
368 u8 nr;
369 u32 error_code;
370 } exception;
372 struct kvm_queued_interrupt {
373 bool pending;
374 u8 nr;
375 } interrupt;
377 struct {
378 int active;
379 u8 save_iopl;
380 struct kvm_save_segment {
381 u16 selector;
382 unsigned long base;
383 u32 limit;
384 u32 ar;
385 } tr, es, ds, fs, gs;
386 } rmode;
387 int halt_request; /* real mode on Intel only */
389 int cpuid_nent;
390 struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
391 /* emulate context */
393 struct x86_emulate_ctxt emulate_ctxt;
395 gpa_t time;
396 struct pvclock_vcpu_time_info hv_clock;
397 unsigned int hv_clock_tsc_khz;
398 unsigned int time_offset;
399 struct page *time_page;
401 bool nmi_pending;
402 bool nmi_injected;
403 bool nmi_window_open;
405 struct mtrr_state_type mtrr_state;
406 u32 pat;
408 int switch_db_regs;
409 unsigned long host_db[KVM_NR_DB_REGS];
410 unsigned long host_dr6;
411 unsigned long host_dr7;
412 unsigned long db[KVM_NR_DB_REGS];
413 unsigned long dr6;
414 unsigned long dr7;
415 unsigned long eff_db[KVM_NR_DB_REGS];
418 struct kvm_mem_alias {
419 gfn_t base_gfn;
420 unsigned long npages;
421 gfn_t target_gfn;
424 struct kvm_arch{
425 int naliases;
426 struct kvm_mem_alias aliases[KVM_ALIAS_SLOTS];
428 unsigned int n_free_mmu_pages;
429 unsigned int n_requested_mmu_pages;
430 unsigned int n_alloc_mmu_pages;
431 struct hlist_head mmu_page_hash[KVM_NUM_MMU_PAGES];
433 * Hash table of struct kvm_mmu_page.
435 struct list_head active_mmu_pages;
436 struct list_head assigned_dev_head;
437 struct iommu_domain *iommu_domain;
438 struct kvm_pic *vpic;
439 struct kvm_ioapic *vioapic;
440 struct kvm_pit *vpit;
441 struct hlist_head irq_ack_notifier_list;
442 int vapics_in_nmi_mode;
444 unsigned int tss_addr;
445 struct page *apic_access_page;
447 gpa_t wall_clock;
449 struct page *ept_identity_pagetable;
450 bool ept_identity_pagetable_done;
452 unsigned long irq_sources_bitmap;
453 unsigned long irq_states[KVM_IOAPIC_NUM_PINS];
454 u64 vm_init_tsc;
457 struct kvm_vm_stat {
458 u32 mmu_shadow_zapped;
459 u32 mmu_pte_write;
460 u32 mmu_pte_updated;
461 u32 mmu_pde_zapped;
462 u32 mmu_flooded;
463 u32 mmu_recycled;
464 u32 mmu_cache_miss;
465 u32 mmu_unsync;
466 u32 remote_tlb_flush;
467 u32 lpages;
470 struct kvm_vcpu_stat {
471 u32 pf_fixed;
472 u32 pf_guest;
473 u32 tlb_flush;
474 u32 invlpg;
476 u32 exits;
477 u32 io_exits;
478 u32 mmio_exits;
479 u32 signal_exits;
480 u32 irq_window_exits;
481 u32 nmi_window_exits;
482 u32 halt_exits;
483 u32 halt_wakeup;
484 u32 request_irq_exits;
485 u32 request_nmi_exits;
486 u32 irq_exits;
487 u32 host_state_reload;
488 u32 efer_reload;
489 u32 fpu_reload;
490 u32 insn_emulation;
491 u32 insn_emulation_fail;
492 u32 hypercalls;
493 u32 irq_injections;
494 u32 nmi_injections;
497 struct descriptor_table {
498 u16 limit;
499 unsigned long base;
500 } __attribute__((packed));
502 struct kvm_x86_ops {
503 int (*cpu_has_kvm_support)(void); /* __init */
504 int (*disabled_by_bios)(void); /* __init */
505 void (*hardware_enable)(void *dummy); /* __init */
506 void (*hardware_disable)(void *dummy);
507 void (*check_processor_compatibility)(void *rtn);
508 int (*hardware_setup)(void); /* __init */
509 void (*hardware_unsetup)(void); /* __exit */
510 bool (*cpu_has_accelerated_tpr)(void);
512 /* Create, but do not attach this VCPU */
513 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
514 void (*vcpu_free)(struct kvm_vcpu *vcpu);
515 int (*vcpu_reset)(struct kvm_vcpu *vcpu);
517 void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
518 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
519 void (*vcpu_put)(struct kvm_vcpu *vcpu);
521 int (*set_guest_debug)(struct kvm_vcpu *vcpu,
522 struct kvm_guest_debug *dbg);
523 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
524 int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
525 u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
526 void (*get_segment)(struct kvm_vcpu *vcpu,
527 struct kvm_segment *var, int seg);
528 int (*get_cpl)(struct kvm_vcpu *vcpu);
529 void (*set_segment)(struct kvm_vcpu *vcpu,
530 struct kvm_segment *var, int seg);
531 void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
532 void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
533 void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
534 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
535 void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
536 void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
537 void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
538 void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
539 void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
540 void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
541 unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
542 void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
543 int *exception);
544 void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
545 unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
546 void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
548 void (*tlb_flush)(struct kvm_vcpu *vcpu);
550 void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
551 int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
552 void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
553 void (*patch_hypercall)(struct kvm_vcpu *vcpu,
554 unsigned char *hypercall_addr);
555 int (*get_irq)(struct kvm_vcpu *vcpu);
556 void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
557 void (*queue_exception)(struct kvm_vcpu *vcpu, unsigned nr,
558 bool has_error_code, u32 error_code);
559 bool (*exception_injected)(struct kvm_vcpu *vcpu);
560 void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
561 void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
562 struct kvm_run *run);
563 int (*interrupt_allowed)(struct kvm_vcpu *vcpu);
564 int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
565 int (*get_tdp_level)(void);
566 int (*get_mt_mask_shift)(void);
569 extern struct kvm_x86_ops *kvm_x86_ops;
571 int kvm_mmu_module_init(void);
572 void kvm_mmu_module_exit(void);
574 void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
575 int kvm_mmu_create(struct kvm_vcpu *vcpu);
576 int kvm_mmu_setup(struct kvm_vcpu *vcpu);
577 void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
578 void kvm_mmu_set_base_ptes(u64 base_pte);
579 void kvm_mmu_set_mask_ptes(u64 user_mask, u64 accessed_mask,
580 u64 dirty_mask, u64 nx_mask, u64 x_mask, u64 mt_mask);
582 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
583 void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
584 void kvm_mmu_zap_all(struct kvm *kvm);
585 unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
586 void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
588 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
590 int emulator_write_phys(struct kvm_vcpu *vcpu, gpa_t gpa,
591 const void *val, int bytes);
592 int kvm_pv_mmu_op(struct kvm_vcpu *vcpu, unsigned long bytes,
593 gpa_t addr, unsigned long *ret);
595 extern bool tdp_enabled;
597 enum emulation_result {
598 EMULATE_DONE, /* no further processing */
599 EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
600 EMULATE_FAIL, /* can't emulate this instruction */
603 #define EMULTYPE_NO_DECODE (1 << 0)
604 #define EMULTYPE_TRAP_UD (1 << 1)
605 #define EMULTYPE_SKIP (1 << 2)
606 int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
607 unsigned long cr2, u16 error_code, int emulation_type);
608 void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
609 void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
610 void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
611 void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
612 unsigned long *rflags);
614 unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
615 void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
616 unsigned long *rflags);
617 void kvm_enable_efer_bits(u64);
618 int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
619 int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
621 struct x86_emulate_ctxt;
623 int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
624 int size, unsigned port);
625 int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
626 int size, unsigned long count, int down,
627 gva_t address, int rep, unsigned port);
628 void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
629 int kvm_emulate_halt(struct kvm_vcpu *vcpu);
630 int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
631 int emulate_clts(struct kvm_vcpu *vcpu);
632 int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
633 unsigned long *dest);
634 int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
635 unsigned long value);
637 void kvm_get_segment(struct kvm_vcpu *vcpu, struct kvm_segment *var, int seg);
638 int kvm_load_segment_descriptor(struct kvm_vcpu *vcpu, u16 selector,
639 int type_bits, int seg);
641 int kvm_task_switch(struct kvm_vcpu *vcpu, u16 tss_selector, int reason);
643 void kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
644 void kvm_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3);
645 void kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4);
646 void kvm_set_cr8(struct kvm_vcpu *vcpu, unsigned long cr8);
647 unsigned long kvm_get_cr8(struct kvm_vcpu *vcpu);
648 void kvm_lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
649 void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
651 int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
652 int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
654 void kvm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr);
655 void kvm_queue_exception_e(struct kvm_vcpu *vcpu, unsigned nr, u32 error_code);
656 void kvm_inject_page_fault(struct kvm_vcpu *vcpu, unsigned long cr2,
657 u32 error_code);
659 int kvm_pic_set_irq(void *opaque, int irq, int level);
661 void kvm_inject_nmi(struct kvm_vcpu *vcpu);
663 void fx_init(struct kvm_vcpu *vcpu);
665 int emulator_write_emulated(unsigned long addr,
666 const void *val,
667 unsigned int bytes,
668 struct kvm_vcpu *vcpu);
670 unsigned long segment_base(u16 selector);
672 void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
673 void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
674 const u8 *new, int bytes,
675 bool guest_initiated);
676 int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
677 void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
678 int kvm_mmu_load(struct kvm_vcpu *vcpu);
679 void kvm_mmu_unload(struct kvm_vcpu *vcpu);
680 void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu);
682 int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
684 int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
686 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code);
687 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
689 void kvm_enable_tdp(void);
690 void kvm_disable_tdp(void);
692 int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
693 int complete_pio(struct kvm_vcpu *vcpu);
695 struct kvm_memory_slot *gfn_to_memslot_unaliased(struct kvm *kvm, gfn_t gfn);
697 static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
699 struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
701 return (struct kvm_mmu_page *)page_private(page);
704 static inline u16 kvm_read_fs(void)
706 u16 seg;
707 asm("mov %%fs, %0" : "=g"(seg));
708 return seg;
711 static inline u16 kvm_read_gs(void)
713 u16 seg;
714 asm("mov %%gs, %0" : "=g"(seg));
715 return seg;
718 static inline u16 kvm_read_ldt(void)
720 u16 ldt;
721 asm("sldt %0" : "=g"(ldt));
722 return ldt;
725 static inline void kvm_load_fs(u16 sel)
727 asm("mov %0, %%fs" : : "rm"(sel));
730 static inline void kvm_load_gs(u16 sel)
732 asm("mov %0, %%gs" : : "rm"(sel));
735 static inline void kvm_load_ldt(u16 sel)
737 asm("lldt %0" : : "rm"(sel));
740 static inline void kvm_get_idt(struct descriptor_table *table)
742 asm("sidt %0" : "=m"(*table));
745 static inline void kvm_get_gdt(struct descriptor_table *table)
747 asm("sgdt %0" : "=m"(*table));
750 static inline unsigned long kvm_read_tr_base(void)
752 u16 tr;
753 asm("str %0" : "=g"(tr));
754 return segment_base(tr);
757 #ifdef CONFIG_X86_64
758 static inline unsigned long read_msr(unsigned long msr)
760 u64 value;
762 rdmsrl(msr, value);
763 return value;
765 #endif
767 static inline void kvm_fx_save(struct i387_fxsave_struct *image)
769 asm("fxsave (%0)":: "r" (image));
772 static inline void kvm_fx_restore(struct i387_fxsave_struct *image)
774 asm("fxrstor (%0)":: "r" (image));
777 static inline void kvm_fx_finit(void)
779 asm("finit");
782 static inline u32 get_rdx_init_val(void)
784 return 0x600; /* P6 family */
787 static inline void kvm_inject_gp(struct kvm_vcpu *vcpu, u32 error_code)
789 kvm_queue_exception_e(vcpu, GP_VECTOR, error_code);
792 #define MSR_IA32_TIME_STAMP_COUNTER 0x010
794 #define TSS_IOPB_BASE_OFFSET 0x66
795 #define TSS_BASE_SIZE 0x68
796 #define TSS_IOPB_SIZE (65536 / 8)
797 #define TSS_REDIRECTION_SIZE (256 / 8)
798 #define RMODE_TSS_SIZE \
799 (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
801 enum {
802 TASK_SWITCH_CALL = 0,
803 TASK_SWITCH_IRET = 1,
804 TASK_SWITCH_JMP = 2,
805 TASK_SWITCH_GATE = 3,
808 #define HF_GIF_MASK (1 << 0)
809 #define HF_HIF_MASK (1 << 1)
810 #define HF_VINTR_MASK (1 << 2)
813 * Hardware virtualization extension instructions may fault if a
814 * reboot turns off virtualization while processes are running.
815 * Trap the fault and ignore the instruction if that happens.
817 asmlinkage void kvm_handle_fault_on_reboot(void);
819 #define __kvm_handle_fault_on_reboot(insn) \
820 "666: " insn "\n\t" \
821 ".pushsection .fixup, \"ax\" \n" \
822 "667: \n\t" \
823 __ASM_SIZE(push) " $666b \n\t" \
824 "jmp kvm_handle_fault_on_reboot \n\t" \
825 ".popsection \n\t" \
826 ".pushsection __ex_table, \"a\" \n\t" \
827 _ASM_PTR " 666b, 667b \n\t" \
828 ".popsection"
830 #define KVM_ARCH_WANT_MMU_NOTIFIER
831 int kvm_unmap_hva(struct kvm *kvm, unsigned long hva);
832 int kvm_age_hva(struct kvm *kvm, unsigned long hva);
833 int cpuid_maxphyaddr(struct kvm_vcpu *vcpu);
835 #endif /* _ASM_X86_KVM_HOST_H */