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