KVM: MMU: Remove unused prev_shadow_ent variable from fetch()
[linux-block.git] / drivers / kvm / x86.h
CommitLineData
043405e1
CO
1#/*
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
11#ifndef KVM_X86_H
12#define KVM_X86_H
13
14#include "kvm.h"
e01a1b57 15#include "irq.h"
043405e1 16
34c16eec
ZX
17#include <linux/types.h>
18#include <linux/mm.h>
19
20#include <linux/kvm.h>
21#include <linux/kvm_para.h>
22
e01a1b57
HB
23#include <asm/desc.h>
24
cd6e8f87
ZX
25#define CR3_PAE_RESERVED_BITS ((X86_CR3_PWT | X86_CR3_PCD) - 1)
26#define CR3_NONPAE_RESERVED_BITS ((PAGE_SIZE-1) & ~(X86_CR3_PWT | X86_CR3_PCD))
27#define CR3_L_MODE_RESERVED_BITS (CR3_NONPAE_RESERVED_BITS|0xFFFFFF0000000000ULL)
28
29#define KVM_GUEST_CR0_MASK \
30 (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE \
31 | X86_CR0_NW | X86_CR0_CD)
32#define KVM_VM_CR0_ALWAYS_ON \
33 (X86_CR0_PG | X86_CR0_PE | X86_CR0_WP | X86_CR0_NE | X86_CR0_TS \
34 | X86_CR0_MP)
35#define KVM_GUEST_CR4_MASK \
36 (X86_CR4_VME | X86_CR4_PSE | X86_CR4_PAE | X86_CR4_PGE | X86_CR4_VMXE)
37#define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
38#define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
39
40#define INVALID_PAGE (~(hpa_t)0)
41#define UNMAPPED_GVA (~(gpa_t)0)
42
43#define DE_VECTOR 0
44#define UD_VECTOR 6
45#define NM_VECTOR 7
46#define DF_VECTOR 8
47#define TS_VECTOR 10
48#define NP_VECTOR 11
49#define SS_VECTOR 12
50#define GP_VECTOR 13
51#define PF_VECTOR 14
52
53#define SELECTOR_TI_MASK (1 << 2)
54#define SELECTOR_RPL_MASK 0x03
55
56#define IOPL_SHIFT 12
57
e9b11c17
ZX
58extern spinlock_t kvm_lock;
59extern struct list_head vm_list;
60
2b3ccfa0
ZX
61enum {
62 VCPU_REGS_RAX = 0,
63 VCPU_REGS_RCX = 1,
64 VCPU_REGS_RDX = 2,
65 VCPU_REGS_RBX = 3,
66 VCPU_REGS_RSP = 4,
67 VCPU_REGS_RBP = 5,
68 VCPU_REGS_RSI = 6,
69 VCPU_REGS_RDI = 7,
70#ifdef CONFIG_X86_64
71 VCPU_REGS_R8 = 8,
72 VCPU_REGS_R9 = 9,
73 VCPU_REGS_R10 = 10,
74 VCPU_REGS_R11 = 11,
75 VCPU_REGS_R12 = 12,
76 VCPU_REGS_R13 = 13,
77 VCPU_REGS_R14 = 14,
78 VCPU_REGS_R15 = 15,
79#endif
80 NR_VCPU_REGS
81};
82
83enum {
84 VCPU_SREG_CS,
85 VCPU_SREG_DS,
86 VCPU_SREG_ES,
87 VCPU_SREG_FS,
88 VCPU_SREG_GS,
89 VCPU_SREG_SS,
90 VCPU_SREG_TR,
91 VCPU_SREG_LDTR,
92};
93
94#include "x86_emulate.h"
95
34c16eec
ZX
96struct kvm_vcpu {
97 KVM_VCPU_COMM;
98 u64 host_tsc;
99 int interrupt_window_open;
100 unsigned long irq_summary; /* bit vector: 1 per word in irq_pending */
101 DECLARE_BITMAP(irq_pending, KVM_NR_INTERRUPTS);
102 unsigned long regs[NR_VCPU_REGS]; /* for rsp: vcpu_load_rsp_rip() */
103 unsigned long rip; /* needs vcpu_load_rsp_rip() */
104
105 unsigned long cr0;
106 unsigned long cr2;
107 unsigned long cr3;
108 unsigned long cr4;
109 unsigned long cr8;
110 u64 pdptrs[4]; /* pae */
111 u64 shadow_efer;
112 u64 apic_base;
113 struct kvm_lapic *apic; /* kernel irqchip context */
114#define VCPU_MP_STATE_RUNNABLE 0
115#define VCPU_MP_STATE_UNINITIALIZED 1
116#define VCPU_MP_STATE_INIT_RECEIVED 2
117#define VCPU_MP_STATE_SIPI_RECEIVED 3
118#define VCPU_MP_STATE_HALTED 4
119 int mp_state;
120 int sipi_vector;
121 u64 ia32_misc_enable_msr;
122
123 struct kvm_mmu mmu;
124
125 struct kvm_mmu_memory_cache mmu_pte_chain_cache;
126 struct kvm_mmu_memory_cache mmu_rmap_desc_cache;
127 struct kvm_mmu_memory_cache mmu_page_cache;
128 struct kvm_mmu_memory_cache mmu_page_header_cache;
129
130 gfn_t last_pt_write_gfn;
131 int last_pt_write_count;
132 u64 *last_pte_updated;
133
134
135 struct i387_fxsave_struct host_fx_image;
136 struct i387_fxsave_struct guest_fx_image;
137
138 gva_t mmio_fault_cr2;
139 struct kvm_pio_request pio;
140 void *pio_data;
141
142 struct {
143 int active;
144 u8 save_iopl;
145 struct kvm_save_segment {
146 u16 selector;
147 unsigned long base;
148 u32 limit;
149 u32 ar;
150 } tr, es, ds, fs, gs;
151 } rmode;
152 int halt_request; /* real mode on Intel only */
153
154 int cpuid_nent;
07716717 155 struct kvm_cpuid_entry2 cpuid_entries[KVM_MAX_CPUID_ENTRIES];
34c16eec
ZX
156
157 /* emulate context */
158
159 struct x86_emulate_ctxt emulate_ctxt;
160};
161
e01a1b57
HB
162struct descriptor_table {
163 u16 limit;
164 unsigned long base;
165} __attribute__((packed));
166
ea4a5ff8
ZX
167struct kvm_x86_ops {
168 int (*cpu_has_kvm_support)(void); /* __init */
169 int (*disabled_by_bios)(void); /* __init */
170 void (*hardware_enable)(void *dummy); /* __init */
171 void (*hardware_disable)(void *dummy);
172 void (*check_processor_compatibility)(void *rtn);
173 int (*hardware_setup)(void); /* __init */
174 void (*hardware_unsetup)(void); /* __exit */
175
176 /* Create, but do not attach this VCPU */
177 struct kvm_vcpu *(*vcpu_create)(struct kvm *kvm, unsigned id);
178 void (*vcpu_free)(struct kvm_vcpu *vcpu);
179 int (*vcpu_reset)(struct kvm_vcpu *vcpu);
180
181 void (*prepare_guest_switch)(struct kvm_vcpu *vcpu);
182 void (*vcpu_load)(struct kvm_vcpu *vcpu, int cpu);
183 void (*vcpu_put)(struct kvm_vcpu *vcpu);
184 void (*vcpu_decache)(struct kvm_vcpu *vcpu);
185
186 int (*set_guest_debug)(struct kvm_vcpu *vcpu,
187 struct kvm_debug_guest *dbg);
188 void (*guest_debug_pre)(struct kvm_vcpu *vcpu);
189 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata);
190 int (*set_msr)(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
191 u64 (*get_segment_base)(struct kvm_vcpu *vcpu, int seg);
192 void (*get_segment)(struct kvm_vcpu *vcpu,
193 struct kvm_segment *var, int seg);
194 void (*set_segment)(struct kvm_vcpu *vcpu,
195 struct kvm_segment *var, int seg);
196 void (*get_cs_db_l_bits)(struct kvm_vcpu *vcpu, int *db, int *l);
197 void (*decache_cr4_guest_bits)(struct kvm_vcpu *vcpu);
198 void (*set_cr0)(struct kvm_vcpu *vcpu, unsigned long cr0);
199 void (*set_cr3)(struct kvm_vcpu *vcpu, unsigned long cr3);
200 void (*set_cr4)(struct kvm_vcpu *vcpu, unsigned long cr4);
201 void (*set_efer)(struct kvm_vcpu *vcpu, u64 efer);
202 void (*get_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
203 void (*set_idt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
204 void (*get_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
205 void (*set_gdt)(struct kvm_vcpu *vcpu, struct descriptor_table *dt);
206 unsigned long (*get_dr)(struct kvm_vcpu *vcpu, int dr);
207 void (*set_dr)(struct kvm_vcpu *vcpu, int dr, unsigned long value,
208 int *exception);
209 void (*cache_regs)(struct kvm_vcpu *vcpu);
210 void (*decache_regs)(struct kvm_vcpu *vcpu);
211 unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
212 void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
213
214 void (*tlb_flush)(struct kvm_vcpu *vcpu);
215 void (*inject_page_fault)(struct kvm_vcpu *vcpu,
216 unsigned long addr, u32 err_code);
217
218 void (*inject_gp)(struct kvm_vcpu *vcpu, unsigned err_code);
219
220 void (*run)(struct kvm_vcpu *vcpu, struct kvm_run *run);
221 int (*handle_exit)(struct kvm_run *run, struct kvm_vcpu *vcpu);
222 void (*skip_emulated_instruction)(struct kvm_vcpu *vcpu);
223 void (*patch_hypercall)(struct kvm_vcpu *vcpu,
224 unsigned char *hypercall_addr);
225 int (*get_irq)(struct kvm_vcpu *vcpu);
226 void (*set_irq)(struct kvm_vcpu *vcpu, int vec);
227 void (*inject_pending_irq)(struct kvm_vcpu *vcpu);
228 void (*inject_pending_vectors)(struct kvm_vcpu *vcpu,
229 struct kvm_run *run);
230
231 int (*set_tss_addr)(struct kvm *kvm, unsigned int addr);
232};
233
97896d04
ZX
234extern struct kvm_x86_ops *kvm_x86_ops;
235
54f1585a
ZX
236int kvm_mmu_module_init(void);
237void kvm_mmu_module_exit(void);
238
239void kvm_mmu_destroy(struct kvm_vcpu *vcpu);
240int kvm_mmu_create(struct kvm_vcpu *vcpu);
241int kvm_mmu_setup(struct kvm_vcpu *vcpu);
242void kvm_mmu_set_nonpresent_ptes(u64 trap_pte, u64 notrap_pte);
243
244int kvm_mmu_reset_context(struct kvm_vcpu *vcpu);
245void kvm_mmu_slot_remove_write_access(struct kvm *kvm, int slot);
246void kvm_mmu_zap_all(struct kvm *kvm);
3ad82a7e 247unsigned int kvm_mmu_calculate_mmu_pages(struct kvm *kvm);
54f1585a
ZX
248void kvm_mmu_change_mmu_pages(struct kvm *kvm, unsigned int kvm_nr_mmu_pages);
249
250enum emulation_result {
251 EMULATE_DONE, /* no further processing */
252 EMULATE_DO_MMIO, /* kvm_run filled with mmio request */
253 EMULATE_FAIL, /* can't emulate this instruction */
254};
255
256int emulate_instruction(struct kvm_vcpu *vcpu, struct kvm_run *run,
257 unsigned long cr2, u16 error_code, int no_decode);
258void kvm_report_emulation_failure(struct kvm_vcpu *cvpu, const char *context);
259void realmode_lgdt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
260void realmode_lidt(struct kvm_vcpu *vcpu, u16 size, unsigned long address);
261void realmode_lmsw(struct kvm_vcpu *vcpu, unsigned long msw,
262 unsigned long *rflags);
263
264unsigned long realmode_get_cr(struct kvm_vcpu *vcpu, int cr);
265void realmode_set_cr(struct kvm_vcpu *vcpu, int cr, unsigned long value,
266 unsigned long *rflags);
267int kvm_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *data);
268int kvm_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data);
269
270struct x86_emulate_ctxt;
271
272int kvm_emulate_pio(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
273 int size, unsigned port);
274int kvm_emulate_pio_string(struct kvm_vcpu *vcpu, struct kvm_run *run, int in,
275 int size, unsigned long count, int down,
276 gva_t address, int rep, unsigned port);
277void kvm_emulate_cpuid(struct kvm_vcpu *vcpu);
278int kvm_emulate_halt(struct kvm_vcpu *vcpu);
279int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address);
280int emulate_clts(struct kvm_vcpu *vcpu);
281int emulator_get_dr(struct x86_emulate_ctxt *ctxt, int dr,
282 unsigned long *dest);
283int emulator_set_dr(struct x86_emulate_ctxt *ctxt, int dr,
284 unsigned long value);
285
286void set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0);
287void set_cr3(struct kvm_vcpu *vcpu, unsigned long cr0);
288void set_cr4(struct kvm_vcpu *vcpu, unsigned long cr0);
289void set_cr8(struct kvm_vcpu *vcpu, unsigned long cr0);
290unsigned long get_cr8(struct kvm_vcpu *vcpu);
291void lmsw(struct kvm_vcpu *vcpu, unsigned long msw);
292void kvm_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l);
293
294int kvm_get_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
295int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data);
296
297void fx_init(struct kvm_vcpu *vcpu);
298
299int emulator_read_std(unsigned long addr,
300 void *val,
301 unsigned int bytes,
302 struct kvm_vcpu *vcpu);
303int emulator_write_emulated(unsigned long addr,
304 const void *val,
305 unsigned int bytes,
306 struct kvm_vcpu *vcpu);
307
308unsigned long segment_base(u16 selector);
309
d835dfec 310void kvm_mmu_flush_tlb(struct kvm_vcpu *vcpu);
54f1585a
ZX
311void kvm_mmu_pte_write(struct kvm_vcpu *vcpu, gpa_t gpa,
312 const u8 *new, int bytes);
313int kvm_mmu_unprotect_page_virt(struct kvm_vcpu *vcpu, gva_t gva);
314void __kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu);
315int kvm_mmu_load(struct kvm_vcpu *vcpu);
316void kvm_mmu_unload(struct kvm_vcpu *vcpu);
317
318int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
319
320int kvm_fix_hypercall(struct kvm_vcpu *vcpu);
321
3067714c 322int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code);
34c16eec
ZX
323
324static inline void kvm_mmu_free_some_pages(struct kvm_vcpu *vcpu)
325{
326 if (unlikely(vcpu->kvm->n_free_mmu_pages < KVM_MIN_FREE_MMU_PAGES))
327 __kvm_mmu_free_some_pages(vcpu);
328}
329
330static inline int kvm_mmu_reload(struct kvm_vcpu *vcpu)
331{
332 if (likely(vcpu->mmu.root_hpa != INVALID_PAGE))
333 return 0;
334
335 return kvm_mmu_load(vcpu);
336}
337
338static inline int is_long_mode(struct kvm_vcpu *vcpu)
339{
340#ifdef CONFIG_X86_64
341 return vcpu->shadow_efer & EFER_LME;
342#else
343 return 0;
344#endif
345}
346
347static inline int is_pae(struct kvm_vcpu *vcpu)
348{
349 return vcpu->cr4 & X86_CR4_PAE;
350}
351
352static inline int is_pse(struct kvm_vcpu *vcpu)
353{
354 return vcpu->cr4 & X86_CR4_PSE;
355}
356
357static inline int is_paging(struct kvm_vcpu *vcpu)
358{
359 return vcpu->cr0 & X86_CR0_PG;
360}
361
a03490ed 362int load_pdptrs(struct kvm_vcpu *vcpu, unsigned long cr3);
de7d789a 363int complete_pio(struct kvm_vcpu *vcpu);
ec6d273d
ZX
364
365static inline struct kvm_mmu_page *page_header(hpa_t shadow_page)
366{
367 struct page *page = pfn_to_page(shadow_page >> PAGE_SHIFT);
368
369 return (struct kvm_mmu_page *)page_private(page);
370}
371
372static inline u16 read_fs(void)
373{
374 u16 seg;
375 asm("mov %%fs, %0" : "=g"(seg));
376 return seg;
377}
378
379static inline u16 read_gs(void)
380{
381 u16 seg;
382 asm("mov %%gs, %0" : "=g"(seg));
383 return seg;
384}
385
386static inline u16 read_ldt(void)
387{
388 u16 ldt;
389 asm("sldt %0" : "=g"(ldt));
390 return ldt;
391}
392
393static inline void load_fs(u16 sel)
394{
395 asm("mov %0, %%fs" : : "rm"(sel));
396}
397
398static inline void load_gs(u16 sel)
399{
400 asm("mov %0, %%gs" : : "rm"(sel));
401}
402
403#ifndef load_ldt
404static inline void load_ldt(u16 sel)
405{
406 asm("lldt %0" : : "rm"(sel));
407}
408#endif
409
410static inline void get_idt(struct descriptor_table *table)
411{
412 asm("sidt %0" : "=m"(*table));
413}
414
415static inline void get_gdt(struct descriptor_table *table)
416{
417 asm("sgdt %0" : "=m"(*table));
418}
419
420static inline unsigned long read_tr_base(void)
421{
422 u16 tr;
423 asm("str %0" : "=g"(tr));
424 return segment_base(tr);
425}
426
427#ifdef CONFIG_X86_64
428static inline unsigned long read_msr(unsigned long msr)
429{
430 u64 value;
431
432 rdmsrl(msr, value);
433 return value;
434}
435#endif
436
437static inline void fx_save(struct i387_fxsave_struct *image)
438{
439 asm("fxsave (%0)":: "r" (image));
440}
441
442static inline void fx_restore(struct i387_fxsave_struct *image)
443{
444 asm("fxrstor (%0)":: "r" (image));
445}
446
447static inline void fpu_init(void)
448{
449 asm("finit");
450}
451
452static inline u32 get_rdx_init_val(void)
453{
454 return 0x600; /* P6 family */
455}
456
457#define ASM_VMX_VMCLEAR_RAX ".byte 0x66, 0x0f, 0xc7, 0x30"
458#define ASM_VMX_VMLAUNCH ".byte 0x0f, 0x01, 0xc2"
459#define ASM_VMX_VMRESUME ".byte 0x0f, 0x01, 0xc3"
460#define ASM_VMX_VMPTRLD_RAX ".byte 0x0f, 0xc7, 0x30"
461#define ASM_VMX_VMREAD_RDX_RAX ".byte 0x0f, 0x78, 0xd0"
462#define ASM_VMX_VMWRITE_RAX_RDX ".byte 0x0f, 0x79, 0xd0"
463#define ASM_VMX_VMWRITE_RSP_RDX ".byte 0x0f, 0x79, 0xd4"
464#define ASM_VMX_VMXOFF ".byte 0x0f, 0x01, 0xc4"
465#define ASM_VMX_VMXON_RAX ".byte 0xf3, 0x0f, 0xc7, 0x30"
466
467#define MSR_IA32_TIME_STAMP_COUNTER 0x010
468
469#define TSS_IOPB_BASE_OFFSET 0x66
470#define TSS_BASE_SIZE 0x68
471#define TSS_IOPB_SIZE (65536 / 8)
472#define TSS_REDIRECTION_SIZE (256 / 8)
473#define RMODE_TSS_SIZE (TSS_BASE_SIZE + TSS_REDIRECTION_SIZE + TSS_IOPB_SIZE + 1)
53e0aa7b
HB
474
475static inline int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
476{
477 return vcpu->mp_state == VCPU_MP_STATE_RUNNABLE
478 || vcpu->mp_state == VCPU_MP_STATE_SIPI_RECEIVED;
479}
480
043405e1 481#endif