]>
Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * This module enables machines with Intel VT-x extensions to run virtual | |
5 | * machines without emulation or binary translation. | |
6 | * | |
7 | * Copyright (C) 2006 Qumranet, Inc. | |
8 | * | |
9 | * Authors: | |
10 | * Avi Kivity <avi@qumranet.com> | |
11 | * Yaniv Kamay <yaniv@qumranet.com> | |
12 | * | |
13 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
14 | * the COPYING file in the top-level directory. | |
15 | * | |
16 | */ | |
17 | ||
18 | #include "kvm.h" | |
19 | #include "vmx.h" | |
20 | #include "kvm_vmx.h" | |
21 | #include <linux/module.h> | |
22 | #include <linux/mm.h> | |
23 | #include <linux/highmem.h> | |
24 | #include <asm/io.h> | |
3b3be0d1 | 25 | #include <asm/desc.h> |
6aa8b732 AK |
26 | |
27 | #include "segment_descriptor.h" | |
28 | ||
29 | #define MSR_IA32_FEATURE_CONTROL 0x03a | |
30 | ||
31 | MODULE_AUTHOR("Qumranet"); | |
32 | MODULE_LICENSE("GPL"); | |
33 | ||
34 | static DEFINE_PER_CPU(struct vmcs *, vmxarea); | |
35 | static DEFINE_PER_CPU(struct vmcs *, current_vmcs); | |
36 | ||
05b3e0c2 | 37 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
38 | #define HOST_IS_64 1 |
39 | #else | |
40 | #define HOST_IS_64 0 | |
41 | #endif | |
42 | ||
43 | static struct vmcs_descriptor { | |
44 | int size; | |
45 | int order; | |
46 | u32 revision_id; | |
47 | } vmcs_descriptor; | |
48 | ||
49 | #define VMX_SEGMENT_FIELD(seg) \ | |
50 | [VCPU_SREG_##seg] = { \ | |
51 | .selector = GUEST_##seg##_SELECTOR, \ | |
52 | .base = GUEST_##seg##_BASE, \ | |
53 | .limit = GUEST_##seg##_LIMIT, \ | |
54 | .ar_bytes = GUEST_##seg##_AR_BYTES, \ | |
55 | } | |
56 | ||
57 | static struct kvm_vmx_segment_field { | |
58 | unsigned selector; | |
59 | unsigned base; | |
60 | unsigned limit; | |
61 | unsigned ar_bytes; | |
62 | } kvm_vmx_segment_fields[] = { | |
63 | VMX_SEGMENT_FIELD(CS), | |
64 | VMX_SEGMENT_FIELD(DS), | |
65 | VMX_SEGMENT_FIELD(ES), | |
66 | VMX_SEGMENT_FIELD(FS), | |
67 | VMX_SEGMENT_FIELD(GS), | |
68 | VMX_SEGMENT_FIELD(SS), | |
69 | VMX_SEGMENT_FIELD(TR), | |
70 | VMX_SEGMENT_FIELD(LDTR), | |
71 | }; | |
72 | ||
73 | static const u32 vmx_msr_index[] = { | |
05b3e0c2 | 74 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
75 | MSR_SYSCALL_MASK, MSR_LSTAR, MSR_CSTAR, MSR_KERNEL_GS_BASE, |
76 | #endif | |
77 | MSR_EFER, MSR_K6_STAR, | |
78 | }; | |
79 | #define NR_VMX_MSR (sizeof(vmx_msr_index) / sizeof(*vmx_msr_index)) | |
80 | ||
6aa8b732 AK |
81 | static inline int is_page_fault(u32 intr_info) |
82 | { | |
83 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK | | |
84 | INTR_INFO_VALID_MASK)) == | |
85 | (INTR_TYPE_EXCEPTION | PF_VECTOR | INTR_INFO_VALID_MASK); | |
86 | } | |
87 | ||
88 | static inline int is_external_interrupt(u32 intr_info) | |
89 | { | |
90 | return (intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VALID_MASK)) | |
91 | == (INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
92 | } | |
93 | ||
7725f0ba AK |
94 | static struct vmx_msr_entry *find_msr_entry(struct kvm_vcpu *vcpu, u32 msr) |
95 | { | |
96 | int i; | |
97 | ||
98 | for (i = 0; i < vcpu->nmsrs; ++i) | |
99 | if (vcpu->guest_msrs[i].index == msr) | |
100 | return &vcpu->guest_msrs[i]; | |
101 | return 0; | |
102 | } | |
103 | ||
6aa8b732 AK |
104 | static void vmcs_clear(struct vmcs *vmcs) |
105 | { | |
106 | u64 phys_addr = __pa(vmcs); | |
107 | u8 error; | |
108 | ||
109 | asm volatile (ASM_VMX_VMCLEAR_RAX "; setna %0" | |
110 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) | |
111 | : "cc", "memory"); | |
112 | if (error) | |
113 | printk(KERN_ERR "kvm: vmclear fail: %p/%llx\n", | |
114 | vmcs, phys_addr); | |
115 | } | |
116 | ||
117 | static void __vcpu_clear(void *arg) | |
118 | { | |
119 | struct kvm_vcpu *vcpu = arg; | |
120 | int cpu = smp_processor_id(); | |
121 | ||
122 | if (vcpu->cpu == cpu) | |
123 | vmcs_clear(vcpu->vmcs); | |
124 | if (per_cpu(current_vmcs, cpu) == vcpu->vmcs) | |
125 | per_cpu(current_vmcs, cpu) = NULL; | |
126 | } | |
127 | ||
128 | static unsigned long vmcs_readl(unsigned long field) | |
129 | { | |
130 | unsigned long value; | |
131 | ||
132 | asm volatile (ASM_VMX_VMREAD_RDX_RAX | |
133 | : "=a"(value) : "d"(field) : "cc"); | |
134 | return value; | |
135 | } | |
136 | ||
137 | static u16 vmcs_read16(unsigned long field) | |
138 | { | |
139 | return vmcs_readl(field); | |
140 | } | |
141 | ||
142 | static u32 vmcs_read32(unsigned long field) | |
143 | { | |
144 | return vmcs_readl(field); | |
145 | } | |
146 | ||
147 | static u64 vmcs_read64(unsigned long field) | |
148 | { | |
05b3e0c2 | 149 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
150 | return vmcs_readl(field); |
151 | #else | |
152 | return vmcs_readl(field) | ((u64)vmcs_readl(field+1) << 32); | |
153 | #endif | |
154 | } | |
155 | ||
156 | static void vmcs_writel(unsigned long field, unsigned long value) | |
157 | { | |
158 | u8 error; | |
159 | ||
160 | asm volatile (ASM_VMX_VMWRITE_RAX_RDX "; setna %0" | |
161 | : "=q"(error) : "a"(value), "d"(field) : "cc" ); | |
162 | if (error) | |
163 | printk(KERN_ERR "vmwrite error: reg %lx value %lx (err %d)\n", | |
164 | field, value, vmcs_read32(VM_INSTRUCTION_ERROR)); | |
165 | } | |
166 | ||
167 | static void vmcs_write16(unsigned long field, u16 value) | |
168 | { | |
169 | vmcs_writel(field, value); | |
170 | } | |
171 | ||
172 | static void vmcs_write32(unsigned long field, u32 value) | |
173 | { | |
174 | vmcs_writel(field, value); | |
175 | } | |
176 | ||
177 | static void vmcs_write64(unsigned long field, u64 value) | |
178 | { | |
05b3e0c2 | 179 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
180 | vmcs_writel(field, value); |
181 | #else | |
182 | vmcs_writel(field, value); | |
183 | asm volatile (""); | |
184 | vmcs_writel(field+1, value >> 32); | |
185 | #endif | |
186 | } | |
187 | ||
188 | /* | |
189 | * Switches to specified vcpu, until a matching vcpu_put(), but assumes | |
190 | * vcpu mutex is already taken. | |
191 | */ | |
192 | static struct kvm_vcpu *vmx_vcpu_load(struct kvm_vcpu *vcpu) | |
193 | { | |
194 | u64 phys_addr = __pa(vcpu->vmcs); | |
195 | int cpu; | |
196 | ||
197 | cpu = get_cpu(); | |
198 | ||
199 | if (vcpu->cpu != cpu) { | |
200 | smp_call_function(__vcpu_clear, vcpu, 0, 1); | |
201 | vcpu->launched = 0; | |
202 | } | |
203 | ||
204 | if (per_cpu(current_vmcs, cpu) != vcpu->vmcs) { | |
205 | u8 error; | |
206 | ||
207 | per_cpu(current_vmcs, cpu) = vcpu->vmcs; | |
208 | asm volatile (ASM_VMX_VMPTRLD_RAX "; setna %0" | |
209 | : "=g"(error) : "a"(&phys_addr), "m"(phys_addr) | |
210 | : "cc"); | |
211 | if (error) | |
212 | printk(KERN_ERR "kvm: vmptrld %p/%llx fail\n", | |
213 | vcpu->vmcs, phys_addr); | |
214 | } | |
215 | ||
216 | if (vcpu->cpu != cpu) { | |
217 | struct descriptor_table dt; | |
218 | unsigned long sysenter_esp; | |
219 | ||
220 | vcpu->cpu = cpu; | |
221 | /* | |
222 | * Linux uses per-cpu TSS and GDT, so set these when switching | |
223 | * processors. | |
224 | */ | |
225 | vmcs_writel(HOST_TR_BASE, read_tr_base()); /* 22.2.4 */ | |
226 | get_gdt(&dt); | |
227 | vmcs_writel(HOST_GDTR_BASE, dt.base); /* 22.2.4 */ | |
228 | ||
229 | rdmsrl(MSR_IA32_SYSENTER_ESP, sysenter_esp); | |
230 | vmcs_writel(HOST_IA32_SYSENTER_ESP, sysenter_esp); /* 22.2.3 */ | |
231 | } | |
232 | return vcpu; | |
233 | } | |
234 | ||
235 | static void vmx_vcpu_put(struct kvm_vcpu *vcpu) | |
236 | { | |
237 | put_cpu(); | |
238 | } | |
239 | ||
240 | static unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu) | |
241 | { | |
242 | return vmcs_readl(GUEST_RFLAGS); | |
243 | } | |
244 | ||
245 | static void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
246 | { | |
247 | vmcs_writel(GUEST_RFLAGS, rflags); | |
248 | } | |
249 | ||
250 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) | |
251 | { | |
252 | unsigned long rip; | |
253 | u32 interruptibility; | |
254 | ||
255 | rip = vmcs_readl(GUEST_RIP); | |
256 | rip += vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
257 | vmcs_writel(GUEST_RIP, rip); | |
258 | ||
259 | /* | |
260 | * We emulated an instruction, so temporary interrupt blocking | |
261 | * should be removed, if set. | |
262 | */ | |
263 | interruptibility = vmcs_read32(GUEST_INTERRUPTIBILITY_INFO); | |
264 | if (interruptibility & 3) | |
265 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, | |
266 | interruptibility & ~3); | |
267 | } | |
268 | ||
269 | static void vmx_inject_gp(struct kvm_vcpu *vcpu, unsigned error_code) | |
270 | { | |
271 | printk(KERN_DEBUG "inject_general_protection: rip 0x%lx\n", | |
272 | vmcs_readl(GUEST_RIP)); | |
273 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, error_code); | |
274 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
275 | GP_VECTOR | | |
276 | INTR_TYPE_EXCEPTION | | |
277 | INTR_INFO_DELIEVER_CODE_MASK | | |
278 | INTR_INFO_VALID_MASK); | |
279 | } | |
280 | ||
281 | /* | |
282 | * reads and returns guest's timestamp counter "register" | |
283 | * guest_tsc = host_tsc + tsc_offset -- 21.3 | |
284 | */ | |
285 | static u64 guest_read_tsc(void) | |
286 | { | |
287 | u64 host_tsc, tsc_offset; | |
288 | ||
289 | rdtscll(host_tsc); | |
290 | tsc_offset = vmcs_read64(TSC_OFFSET); | |
291 | return host_tsc + tsc_offset; | |
292 | } | |
293 | ||
294 | /* | |
295 | * writes 'guest_tsc' into guest's timestamp counter "register" | |
296 | * guest_tsc = host_tsc + tsc_offset ==> tsc_offset = guest_tsc - host_tsc | |
297 | */ | |
298 | static void guest_write_tsc(u64 guest_tsc) | |
299 | { | |
300 | u64 host_tsc; | |
301 | ||
302 | rdtscll(host_tsc); | |
303 | vmcs_write64(TSC_OFFSET, guest_tsc - host_tsc); | |
304 | } | |
305 | ||
306 | static void reload_tss(void) | |
307 | { | |
05b3e0c2 | 308 | #ifndef CONFIG_X86_64 |
6aa8b732 AK |
309 | |
310 | /* | |
311 | * VT restores TR but not its size. Useless. | |
312 | */ | |
313 | struct descriptor_table gdt; | |
314 | struct segment_descriptor *descs; | |
315 | ||
316 | get_gdt(&gdt); | |
317 | descs = (void *)gdt.base; | |
318 | descs[GDT_ENTRY_TSS].type = 9; /* available TSS */ | |
319 | load_TR_desc(); | |
320 | #endif | |
321 | } | |
322 | ||
323 | /* | |
324 | * Reads an msr value (of 'msr_index') into 'pdata'. | |
325 | * Returns 0 on success, non-0 otherwise. | |
326 | * Assumes vcpu_load() was already called. | |
327 | */ | |
328 | static int vmx_get_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 *pdata) | |
329 | { | |
330 | u64 data; | |
331 | struct vmx_msr_entry *msr; | |
332 | ||
333 | if (!pdata) { | |
334 | printk(KERN_ERR "BUG: get_msr called with NULL pdata\n"); | |
335 | return -EINVAL; | |
336 | } | |
337 | ||
338 | switch (msr_index) { | |
05b3e0c2 | 339 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
340 | case MSR_FS_BASE: |
341 | data = vmcs_readl(GUEST_FS_BASE); | |
342 | break; | |
343 | case MSR_GS_BASE: | |
344 | data = vmcs_readl(GUEST_GS_BASE); | |
345 | break; | |
346 | case MSR_EFER: | |
3bab1f5d | 347 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
6aa8b732 AK |
348 | #endif |
349 | case MSR_IA32_TIME_STAMP_COUNTER: | |
350 | data = guest_read_tsc(); | |
351 | break; | |
352 | case MSR_IA32_SYSENTER_CS: | |
353 | data = vmcs_read32(GUEST_SYSENTER_CS); | |
354 | break; | |
355 | case MSR_IA32_SYSENTER_EIP: | |
356 | data = vmcs_read32(GUEST_SYSENTER_EIP); | |
357 | break; | |
358 | case MSR_IA32_SYSENTER_ESP: | |
359 | data = vmcs_read32(GUEST_SYSENTER_ESP); | |
360 | break; | |
6aa8b732 AK |
361 | default: |
362 | msr = find_msr_entry(vcpu, msr_index); | |
3bab1f5d AK |
363 | if (msr) { |
364 | data = msr->data; | |
365 | break; | |
6aa8b732 | 366 | } |
3bab1f5d | 367 | return kvm_get_msr_common(vcpu, msr_index, pdata); |
6aa8b732 AK |
368 | } |
369 | ||
370 | *pdata = data; | |
371 | return 0; | |
372 | } | |
373 | ||
374 | /* | |
375 | * Writes msr value into into the appropriate "register". | |
376 | * Returns 0 on success, non-0 otherwise. | |
377 | * Assumes vcpu_load() was already called. | |
378 | */ | |
379 | static int vmx_set_msr(struct kvm_vcpu *vcpu, u32 msr_index, u64 data) | |
380 | { | |
381 | struct vmx_msr_entry *msr; | |
382 | switch (msr_index) { | |
05b3e0c2 | 383 | #ifdef CONFIG_X86_64 |
3bab1f5d AK |
384 | case MSR_EFER: |
385 | return kvm_set_msr_common(vcpu, msr_index, data); | |
6aa8b732 AK |
386 | case MSR_FS_BASE: |
387 | vmcs_writel(GUEST_FS_BASE, data); | |
388 | break; | |
389 | case MSR_GS_BASE: | |
390 | vmcs_writel(GUEST_GS_BASE, data); | |
391 | break; | |
392 | #endif | |
393 | case MSR_IA32_SYSENTER_CS: | |
394 | vmcs_write32(GUEST_SYSENTER_CS, data); | |
395 | break; | |
396 | case MSR_IA32_SYSENTER_EIP: | |
397 | vmcs_write32(GUEST_SYSENTER_EIP, data); | |
398 | break; | |
399 | case MSR_IA32_SYSENTER_ESP: | |
400 | vmcs_write32(GUEST_SYSENTER_ESP, data); | |
401 | break; | |
6aa8b732 AK |
402 | case MSR_IA32_TIME_STAMP_COUNTER: { |
403 | guest_write_tsc(data); | |
404 | break; | |
405 | } | |
6aa8b732 AK |
406 | default: |
407 | msr = find_msr_entry(vcpu, msr_index); | |
3bab1f5d AK |
408 | if (msr) { |
409 | msr->data = data; | |
410 | break; | |
6aa8b732 | 411 | } |
3bab1f5d | 412 | return kvm_set_msr_common(vcpu, msr_index, data); |
6aa8b732 AK |
413 | msr->data = data; |
414 | break; | |
415 | } | |
416 | ||
417 | return 0; | |
418 | } | |
419 | ||
420 | /* | |
421 | * Sync the rsp and rip registers into the vcpu structure. This allows | |
422 | * registers to be accessed by indexing vcpu->regs. | |
423 | */ | |
424 | static void vcpu_load_rsp_rip(struct kvm_vcpu *vcpu) | |
425 | { | |
426 | vcpu->regs[VCPU_REGS_RSP] = vmcs_readl(GUEST_RSP); | |
427 | vcpu->rip = vmcs_readl(GUEST_RIP); | |
428 | } | |
429 | ||
430 | /* | |
431 | * Syncs rsp and rip back into the vmcs. Should be called after possible | |
432 | * modification. | |
433 | */ | |
434 | static void vcpu_put_rsp_rip(struct kvm_vcpu *vcpu) | |
435 | { | |
436 | vmcs_writel(GUEST_RSP, vcpu->regs[VCPU_REGS_RSP]); | |
437 | vmcs_writel(GUEST_RIP, vcpu->rip); | |
438 | } | |
439 | ||
440 | static int set_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) | |
441 | { | |
442 | unsigned long dr7 = 0x400; | |
443 | u32 exception_bitmap; | |
444 | int old_singlestep; | |
445 | ||
446 | exception_bitmap = vmcs_read32(EXCEPTION_BITMAP); | |
447 | old_singlestep = vcpu->guest_debug.singlestep; | |
448 | ||
449 | vcpu->guest_debug.enabled = dbg->enabled; | |
450 | if (vcpu->guest_debug.enabled) { | |
451 | int i; | |
452 | ||
453 | dr7 |= 0x200; /* exact */ | |
454 | for (i = 0; i < 4; ++i) { | |
455 | if (!dbg->breakpoints[i].enabled) | |
456 | continue; | |
457 | vcpu->guest_debug.bp[i] = dbg->breakpoints[i].address; | |
458 | dr7 |= 2 << (i*2); /* global enable */ | |
459 | dr7 |= 0 << (i*4+16); /* execution breakpoint */ | |
460 | } | |
461 | ||
462 | exception_bitmap |= (1u << 1); /* Trap debug exceptions */ | |
463 | ||
464 | vcpu->guest_debug.singlestep = dbg->singlestep; | |
465 | } else { | |
466 | exception_bitmap &= ~(1u << 1); /* Ignore debug exceptions */ | |
467 | vcpu->guest_debug.singlestep = 0; | |
468 | } | |
469 | ||
470 | if (old_singlestep && !vcpu->guest_debug.singlestep) { | |
471 | unsigned long flags; | |
472 | ||
473 | flags = vmcs_readl(GUEST_RFLAGS); | |
474 | flags &= ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
475 | vmcs_writel(GUEST_RFLAGS, flags); | |
476 | } | |
477 | ||
478 | vmcs_write32(EXCEPTION_BITMAP, exception_bitmap); | |
479 | vmcs_writel(GUEST_DR7, dr7); | |
480 | ||
481 | return 0; | |
482 | } | |
483 | ||
484 | static __init int cpu_has_kvm_support(void) | |
485 | { | |
486 | unsigned long ecx = cpuid_ecx(1); | |
487 | return test_bit(5, &ecx); /* CPUID.1:ECX.VMX[bit 5] -> VT */ | |
488 | } | |
489 | ||
490 | static __init int vmx_disabled_by_bios(void) | |
491 | { | |
492 | u64 msr; | |
493 | ||
494 | rdmsrl(MSR_IA32_FEATURE_CONTROL, msr); | |
495 | return (msr & 5) == 1; /* locked but not enabled */ | |
496 | } | |
497 | ||
498 | static __init void hardware_enable(void *garbage) | |
499 | { | |
500 | int cpu = raw_smp_processor_id(); | |
501 | u64 phys_addr = __pa(per_cpu(vmxarea, cpu)); | |
502 | u64 old; | |
503 | ||
504 | rdmsrl(MSR_IA32_FEATURE_CONTROL, old); | |
bfdc0c28 | 505 | if ((old & 5) != 5) |
6aa8b732 AK |
506 | /* enable and lock */ |
507 | wrmsrl(MSR_IA32_FEATURE_CONTROL, old | 5); | |
508 | write_cr4(read_cr4() | CR4_VMXE); /* FIXME: not cpu hotplug safe */ | |
509 | asm volatile (ASM_VMX_VMXON_RAX : : "a"(&phys_addr), "m"(phys_addr) | |
510 | : "memory", "cc"); | |
511 | } | |
512 | ||
513 | static void hardware_disable(void *garbage) | |
514 | { | |
515 | asm volatile (ASM_VMX_VMXOFF : : : "cc"); | |
516 | } | |
517 | ||
518 | static __init void setup_vmcs_descriptor(void) | |
519 | { | |
520 | u32 vmx_msr_low, vmx_msr_high; | |
521 | ||
522 | rdmsr(MSR_IA32_VMX_BASIC_MSR, vmx_msr_low, vmx_msr_high); | |
523 | vmcs_descriptor.size = vmx_msr_high & 0x1fff; | |
524 | vmcs_descriptor.order = get_order(vmcs_descriptor.size); | |
525 | vmcs_descriptor.revision_id = vmx_msr_low; | |
526 | }; | |
527 | ||
528 | static struct vmcs *alloc_vmcs_cpu(int cpu) | |
529 | { | |
530 | int node = cpu_to_node(cpu); | |
531 | struct page *pages; | |
532 | struct vmcs *vmcs; | |
533 | ||
534 | pages = alloc_pages_node(node, GFP_KERNEL, vmcs_descriptor.order); | |
535 | if (!pages) | |
536 | return NULL; | |
537 | vmcs = page_address(pages); | |
538 | memset(vmcs, 0, vmcs_descriptor.size); | |
539 | vmcs->revision_id = vmcs_descriptor.revision_id; /* vmcs revision id */ | |
540 | return vmcs; | |
541 | } | |
542 | ||
543 | static struct vmcs *alloc_vmcs(void) | |
544 | { | |
545 | return alloc_vmcs_cpu(smp_processor_id()); | |
546 | } | |
547 | ||
548 | static void free_vmcs(struct vmcs *vmcs) | |
549 | { | |
550 | free_pages((unsigned long)vmcs, vmcs_descriptor.order); | |
551 | } | |
552 | ||
553 | static __exit void free_kvm_area(void) | |
554 | { | |
555 | int cpu; | |
556 | ||
557 | for_each_online_cpu(cpu) | |
558 | free_vmcs(per_cpu(vmxarea, cpu)); | |
559 | } | |
560 | ||
561 | extern struct vmcs *alloc_vmcs_cpu(int cpu); | |
562 | ||
563 | static __init int alloc_kvm_area(void) | |
564 | { | |
565 | int cpu; | |
566 | ||
567 | for_each_online_cpu(cpu) { | |
568 | struct vmcs *vmcs; | |
569 | ||
570 | vmcs = alloc_vmcs_cpu(cpu); | |
571 | if (!vmcs) { | |
572 | free_kvm_area(); | |
573 | return -ENOMEM; | |
574 | } | |
575 | ||
576 | per_cpu(vmxarea, cpu) = vmcs; | |
577 | } | |
578 | return 0; | |
579 | } | |
580 | ||
581 | static __init int hardware_setup(void) | |
582 | { | |
583 | setup_vmcs_descriptor(); | |
584 | return alloc_kvm_area(); | |
585 | } | |
586 | ||
587 | static __exit void hardware_unsetup(void) | |
588 | { | |
589 | free_kvm_area(); | |
590 | } | |
591 | ||
592 | static void update_exception_bitmap(struct kvm_vcpu *vcpu) | |
593 | { | |
594 | if (vcpu->rmode.active) | |
595 | vmcs_write32(EXCEPTION_BITMAP, ~0); | |
596 | else | |
597 | vmcs_write32(EXCEPTION_BITMAP, 1 << PF_VECTOR); | |
598 | } | |
599 | ||
600 | static void fix_pmode_dataseg(int seg, struct kvm_save_segment *save) | |
601 | { | |
602 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; | |
603 | ||
604 | if (vmcs_readl(sf->base) == save->base) { | |
605 | vmcs_write16(sf->selector, save->selector); | |
606 | vmcs_writel(sf->base, save->base); | |
607 | vmcs_write32(sf->limit, save->limit); | |
608 | vmcs_write32(sf->ar_bytes, save->ar); | |
609 | } else { | |
610 | u32 dpl = (vmcs_read16(sf->selector) & SELECTOR_RPL_MASK) | |
611 | << AR_DPL_SHIFT; | |
612 | vmcs_write32(sf->ar_bytes, 0x93 | dpl); | |
613 | } | |
614 | } | |
615 | ||
616 | static void enter_pmode(struct kvm_vcpu *vcpu) | |
617 | { | |
618 | unsigned long flags; | |
619 | ||
620 | vcpu->rmode.active = 0; | |
621 | ||
622 | vmcs_writel(GUEST_TR_BASE, vcpu->rmode.tr.base); | |
623 | vmcs_write32(GUEST_TR_LIMIT, vcpu->rmode.tr.limit); | |
624 | vmcs_write32(GUEST_TR_AR_BYTES, vcpu->rmode.tr.ar); | |
625 | ||
626 | flags = vmcs_readl(GUEST_RFLAGS); | |
627 | flags &= ~(IOPL_MASK | X86_EFLAGS_VM); | |
628 | flags |= (vcpu->rmode.save_iopl << IOPL_SHIFT); | |
629 | vmcs_writel(GUEST_RFLAGS, flags); | |
630 | ||
631 | vmcs_writel(GUEST_CR4, (vmcs_readl(GUEST_CR4) & ~CR4_VME_MASK) | | |
632 | (vmcs_readl(CR4_READ_SHADOW) & CR4_VME_MASK)); | |
633 | ||
634 | update_exception_bitmap(vcpu); | |
635 | ||
636 | fix_pmode_dataseg(VCPU_SREG_ES, &vcpu->rmode.es); | |
637 | fix_pmode_dataseg(VCPU_SREG_DS, &vcpu->rmode.ds); | |
638 | fix_pmode_dataseg(VCPU_SREG_GS, &vcpu->rmode.gs); | |
639 | fix_pmode_dataseg(VCPU_SREG_FS, &vcpu->rmode.fs); | |
640 | ||
641 | vmcs_write16(GUEST_SS_SELECTOR, 0); | |
642 | vmcs_write32(GUEST_SS_AR_BYTES, 0x93); | |
643 | ||
644 | vmcs_write16(GUEST_CS_SELECTOR, | |
645 | vmcs_read16(GUEST_CS_SELECTOR) & ~SELECTOR_RPL_MASK); | |
646 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); | |
647 | } | |
648 | ||
649 | static int rmode_tss_base(struct kvm* kvm) | |
650 | { | |
651 | gfn_t base_gfn = kvm->memslots[0].base_gfn + kvm->memslots[0].npages - 3; | |
652 | return base_gfn << PAGE_SHIFT; | |
653 | } | |
654 | ||
655 | static void fix_rmode_seg(int seg, struct kvm_save_segment *save) | |
656 | { | |
657 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; | |
658 | ||
659 | save->selector = vmcs_read16(sf->selector); | |
660 | save->base = vmcs_readl(sf->base); | |
661 | save->limit = vmcs_read32(sf->limit); | |
662 | save->ar = vmcs_read32(sf->ar_bytes); | |
663 | vmcs_write16(sf->selector, vmcs_readl(sf->base) >> 4); | |
664 | vmcs_write32(sf->limit, 0xffff); | |
665 | vmcs_write32(sf->ar_bytes, 0xf3); | |
666 | } | |
667 | ||
668 | static void enter_rmode(struct kvm_vcpu *vcpu) | |
669 | { | |
670 | unsigned long flags; | |
671 | ||
672 | vcpu->rmode.active = 1; | |
673 | ||
674 | vcpu->rmode.tr.base = vmcs_readl(GUEST_TR_BASE); | |
675 | vmcs_writel(GUEST_TR_BASE, rmode_tss_base(vcpu->kvm)); | |
676 | ||
677 | vcpu->rmode.tr.limit = vmcs_read32(GUEST_TR_LIMIT); | |
678 | vmcs_write32(GUEST_TR_LIMIT, RMODE_TSS_SIZE - 1); | |
679 | ||
680 | vcpu->rmode.tr.ar = vmcs_read32(GUEST_TR_AR_BYTES); | |
681 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
682 | ||
683 | flags = vmcs_readl(GUEST_RFLAGS); | |
684 | vcpu->rmode.save_iopl = (flags & IOPL_MASK) >> IOPL_SHIFT; | |
685 | ||
686 | flags |= IOPL_MASK | X86_EFLAGS_VM; | |
687 | ||
688 | vmcs_writel(GUEST_RFLAGS, flags); | |
689 | vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | CR4_VME_MASK); | |
690 | update_exception_bitmap(vcpu); | |
691 | ||
692 | vmcs_write16(GUEST_SS_SELECTOR, vmcs_readl(GUEST_SS_BASE) >> 4); | |
693 | vmcs_write32(GUEST_SS_LIMIT, 0xffff); | |
694 | vmcs_write32(GUEST_SS_AR_BYTES, 0xf3); | |
695 | ||
696 | vmcs_write32(GUEST_CS_AR_BYTES, 0xf3); | |
abacf8df | 697 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); |
6aa8b732 AK |
698 | vmcs_write16(GUEST_CS_SELECTOR, vmcs_readl(GUEST_CS_BASE) >> 4); |
699 | ||
700 | fix_rmode_seg(VCPU_SREG_ES, &vcpu->rmode.es); | |
701 | fix_rmode_seg(VCPU_SREG_DS, &vcpu->rmode.ds); | |
702 | fix_rmode_seg(VCPU_SREG_GS, &vcpu->rmode.gs); | |
703 | fix_rmode_seg(VCPU_SREG_FS, &vcpu->rmode.fs); | |
704 | } | |
705 | ||
05b3e0c2 | 706 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
707 | |
708 | static void enter_lmode(struct kvm_vcpu *vcpu) | |
709 | { | |
710 | u32 guest_tr_ar; | |
711 | ||
712 | guest_tr_ar = vmcs_read32(GUEST_TR_AR_BYTES); | |
713 | if ((guest_tr_ar & AR_TYPE_MASK) != AR_TYPE_BUSY_64_TSS) { | |
714 | printk(KERN_DEBUG "%s: tss fixup for long mode. \n", | |
715 | __FUNCTION__); | |
716 | vmcs_write32(GUEST_TR_AR_BYTES, | |
717 | (guest_tr_ar & ~AR_TYPE_MASK) | |
718 | | AR_TYPE_BUSY_64_TSS); | |
719 | } | |
720 | ||
721 | vcpu->shadow_efer |= EFER_LMA; | |
722 | ||
723 | find_msr_entry(vcpu, MSR_EFER)->data |= EFER_LMA | EFER_LME; | |
724 | vmcs_write32(VM_ENTRY_CONTROLS, | |
725 | vmcs_read32(VM_ENTRY_CONTROLS) | |
726 | | VM_ENTRY_CONTROLS_IA32E_MASK); | |
727 | } | |
728 | ||
729 | static void exit_lmode(struct kvm_vcpu *vcpu) | |
730 | { | |
731 | vcpu->shadow_efer &= ~EFER_LMA; | |
732 | ||
733 | vmcs_write32(VM_ENTRY_CONTROLS, | |
734 | vmcs_read32(VM_ENTRY_CONTROLS) | |
735 | & ~VM_ENTRY_CONTROLS_IA32E_MASK); | |
736 | } | |
737 | ||
738 | #endif | |
739 | ||
740 | static void vmx_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) | |
741 | { | |
742 | if (vcpu->rmode.active && (cr0 & CR0_PE_MASK)) | |
743 | enter_pmode(vcpu); | |
744 | ||
745 | if (!vcpu->rmode.active && !(cr0 & CR0_PE_MASK)) | |
746 | enter_rmode(vcpu); | |
747 | ||
05b3e0c2 | 748 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
749 | if (vcpu->shadow_efer & EFER_LME) { |
750 | if (!is_paging(vcpu) && (cr0 & CR0_PG_MASK)) | |
751 | enter_lmode(vcpu); | |
752 | if (is_paging(vcpu) && !(cr0 & CR0_PG_MASK)) | |
753 | exit_lmode(vcpu); | |
754 | } | |
755 | #endif | |
756 | ||
757 | vmcs_writel(CR0_READ_SHADOW, cr0); | |
758 | vmcs_writel(GUEST_CR0, | |
759 | (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON); | |
760 | vcpu->cr0 = cr0; | |
761 | } | |
762 | ||
763 | /* | |
764 | * Used when restoring the VM to avoid corrupting segment registers | |
765 | */ | |
766 | static void vmx_set_cr0_no_modeswitch(struct kvm_vcpu *vcpu, unsigned long cr0) | |
767 | { | |
768 | vcpu->rmode.active = ((cr0 & CR0_PE_MASK) == 0); | |
769 | update_exception_bitmap(vcpu); | |
770 | vmcs_writel(CR0_READ_SHADOW, cr0); | |
771 | vmcs_writel(GUEST_CR0, | |
772 | (cr0 & ~KVM_GUEST_CR0_MASK) | KVM_VM_CR0_ALWAYS_ON); | |
773 | vcpu->cr0 = cr0; | |
774 | } | |
775 | ||
776 | static void vmx_set_cr3(struct kvm_vcpu *vcpu, unsigned long cr3) | |
777 | { | |
778 | vmcs_writel(GUEST_CR3, cr3); | |
779 | } | |
780 | ||
781 | static void vmx_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
782 | { | |
783 | vmcs_writel(CR4_READ_SHADOW, cr4); | |
784 | vmcs_writel(GUEST_CR4, cr4 | (vcpu->rmode.active ? | |
785 | KVM_RMODE_VM_CR4_ALWAYS_ON : KVM_PMODE_VM_CR4_ALWAYS_ON)); | |
786 | vcpu->cr4 = cr4; | |
787 | } | |
788 | ||
05b3e0c2 | 789 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
790 | |
791 | static void vmx_set_efer(struct kvm_vcpu *vcpu, u64 efer) | |
792 | { | |
793 | struct vmx_msr_entry *msr = find_msr_entry(vcpu, MSR_EFER); | |
794 | ||
795 | vcpu->shadow_efer = efer; | |
796 | if (efer & EFER_LMA) { | |
797 | vmcs_write32(VM_ENTRY_CONTROLS, | |
798 | vmcs_read32(VM_ENTRY_CONTROLS) | | |
799 | VM_ENTRY_CONTROLS_IA32E_MASK); | |
800 | msr->data = efer; | |
801 | ||
802 | } else { | |
803 | vmcs_write32(VM_ENTRY_CONTROLS, | |
804 | vmcs_read32(VM_ENTRY_CONTROLS) & | |
805 | ~VM_ENTRY_CONTROLS_IA32E_MASK); | |
806 | ||
807 | msr->data = efer & ~EFER_LME; | |
808 | } | |
809 | } | |
810 | ||
811 | #endif | |
812 | ||
813 | static u64 vmx_get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
814 | { | |
815 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; | |
816 | ||
817 | return vmcs_readl(sf->base); | |
818 | } | |
819 | ||
820 | static void vmx_get_segment(struct kvm_vcpu *vcpu, | |
821 | struct kvm_segment *var, int seg) | |
822 | { | |
823 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; | |
824 | u32 ar; | |
825 | ||
826 | var->base = vmcs_readl(sf->base); | |
827 | var->limit = vmcs_read32(sf->limit); | |
828 | var->selector = vmcs_read16(sf->selector); | |
829 | ar = vmcs_read32(sf->ar_bytes); | |
830 | if (ar & AR_UNUSABLE_MASK) | |
831 | ar = 0; | |
832 | var->type = ar & 15; | |
833 | var->s = (ar >> 4) & 1; | |
834 | var->dpl = (ar >> 5) & 3; | |
835 | var->present = (ar >> 7) & 1; | |
836 | var->avl = (ar >> 12) & 1; | |
837 | var->l = (ar >> 13) & 1; | |
838 | var->db = (ar >> 14) & 1; | |
839 | var->g = (ar >> 15) & 1; | |
840 | var->unusable = (ar >> 16) & 1; | |
841 | } | |
842 | ||
843 | static void vmx_set_segment(struct kvm_vcpu *vcpu, | |
844 | struct kvm_segment *var, int seg) | |
845 | { | |
846 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; | |
847 | u32 ar; | |
848 | ||
849 | vmcs_writel(sf->base, var->base); | |
850 | vmcs_write32(sf->limit, var->limit); | |
851 | vmcs_write16(sf->selector, var->selector); | |
852 | if (var->unusable) | |
853 | ar = 1 << 16; | |
854 | else { | |
855 | ar = var->type & 15; | |
856 | ar |= (var->s & 1) << 4; | |
857 | ar |= (var->dpl & 3) << 5; | |
858 | ar |= (var->present & 1) << 7; | |
859 | ar |= (var->avl & 1) << 12; | |
860 | ar |= (var->l & 1) << 13; | |
861 | ar |= (var->db & 1) << 14; | |
862 | ar |= (var->g & 1) << 15; | |
863 | } | |
f7fbf1fd UL |
864 | if (ar == 0) /* a 0 value means unusable */ |
865 | ar = AR_UNUSABLE_MASK; | |
6aa8b732 AK |
866 | vmcs_write32(sf->ar_bytes, ar); |
867 | } | |
868 | ||
6aa8b732 AK |
869 | static void vmx_get_cs_db_l_bits(struct kvm_vcpu *vcpu, int *db, int *l) |
870 | { | |
871 | u32 ar = vmcs_read32(GUEST_CS_AR_BYTES); | |
872 | ||
873 | *db = (ar >> 14) & 1; | |
874 | *l = (ar >> 13) & 1; | |
875 | } | |
876 | ||
877 | static void vmx_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
878 | { | |
879 | dt->limit = vmcs_read32(GUEST_IDTR_LIMIT); | |
880 | dt->base = vmcs_readl(GUEST_IDTR_BASE); | |
881 | } | |
882 | ||
883 | static void vmx_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
884 | { | |
885 | vmcs_write32(GUEST_IDTR_LIMIT, dt->limit); | |
886 | vmcs_writel(GUEST_IDTR_BASE, dt->base); | |
887 | } | |
888 | ||
889 | static void vmx_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
890 | { | |
891 | dt->limit = vmcs_read32(GUEST_GDTR_LIMIT); | |
892 | dt->base = vmcs_readl(GUEST_GDTR_BASE); | |
893 | } | |
894 | ||
895 | static void vmx_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
896 | { | |
897 | vmcs_write32(GUEST_GDTR_LIMIT, dt->limit); | |
898 | vmcs_writel(GUEST_GDTR_BASE, dt->base); | |
899 | } | |
900 | ||
901 | static int init_rmode_tss(struct kvm* kvm) | |
902 | { | |
903 | struct page *p1, *p2, *p3; | |
904 | gfn_t fn = rmode_tss_base(kvm) >> PAGE_SHIFT; | |
905 | char *page; | |
906 | ||
907 | p1 = _gfn_to_page(kvm, fn++); | |
908 | p2 = _gfn_to_page(kvm, fn++); | |
909 | p3 = _gfn_to_page(kvm, fn); | |
910 | ||
911 | if (!p1 || !p2 || !p3) { | |
912 | kvm_printf(kvm,"%s: gfn_to_page failed\n", __FUNCTION__); | |
913 | return 0; | |
914 | } | |
915 | ||
916 | page = kmap_atomic(p1, KM_USER0); | |
917 | memset(page, 0, PAGE_SIZE); | |
918 | *(u16*)(page + 0x66) = TSS_BASE_SIZE + TSS_REDIRECTION_SIZE; | |
919 | kunmap_atomic(page, KM_USER0); | |
920 | ||
921 | page = kmap_atomic(p2, KM_USER0); | |
922 | memset(page, 0, PAGE_SIZE); | |
923 | kunmap_atomic(page, KM_USER0); | |
924 | ||
925 | page = kmap_atomic(p3, KM_USER0); | |
926 | memset(page, 0, PAGE_SIZE); | |
927 | *(page + RMODE_TSS_SIZE - 2 * PAGE_SIZE - 1) = ~0; | |
928 | kunmap_atomic(page, KM_USER0); | |
929 | ||
930 | return 1; | |
931 | } | |
932 | ||
933 | static void vmcs_write32_fixedbits(u32 msr, u32 vmcs_field, u32 val) | |
934 | { | |
935 | u32 msr_high, msr_low; | |
936 | ||
937 | rdmsr(msr, msr_low, msr_high); | |
938 | ||
939 | val &= msr_high; | |
940 | val |= msr_low; | |
941 | vmcs_write32(vmcs_field, val); | |
942 | } | |
943 | ||
944 | static void seg_setup(int seg) | |
945 | { | |
946 | struct kvm_vmx_segment_field *sf = &kvm_vmx_segment_fields[seg]; | |
947 | ||
948 | vmcs_write16(sf->selector, 0); | |
949 | vmcs_writel(sf->base, 0); | |
950 | vmcs_write32(sf->limit, 0xffff); | |
951 | vmcs_write32(sf->ar_bytes, 0x93); | |
952 | } | |
953 | ||
954 | /* | |
955 | * Sets up the vmcs for emulated real mode. | |
956 | */ | |
957 | static int vmx_vcpu_setup(struct kvm_vcpu *vcpu) | |
958 | { | |
959 | u32 host_sysenter_cs; | |
960 | u32 junk; | |
961 | unsigned long a; | |
962 | struct descriptor_table dt; | |
963 | int i; | |
964 | int ret = 0; | |
965 | int nr_good_msrs; | |
966 | extern asmlinkage void kvm_vmx_return(void); | |
967 | ||
968 | if (!init_rmode_tss(vcpu->kvm)) { | |
969 | ret = -ENOMEM; | |
970 | goto out; | |
971 | } | |
972 | ||
973 | memset(vcpu->regs, 0, sizeof(vcpu->regs)); | |
974 | vcpu->regs[VCPU_REGS_RDX] = get_rdx_init_val(); | |
975 | vcpu->cr8 = 0; | |
976 | vcpu->apic_base = 0xfee00000 | | |
977 | /*for vcpu 0*/ MSR_IA32_APICBASE_BSP | | |
978 | MSR_IA32_APICBASE_ENABLE; | |
979 | ||
980 | fx_init(vcpu); | |
981 | ||
982 | /* | |
983 | * GUEST_CS_BASE should really be 0xffff0000, but VT vm86 mode | |
984 | * insists on having GUEST_CS_BASE == GUEST_CS_SELECTOR << 4. Sigh. | |
985 | */ | |
986 | vmcs_write16(GUEST_CS_SELECTOR, 0xf000); | |
987 | vmcs_writel(GUEST_CS_BASE, 0x000f0000); | |
988 | vmcs_write32(GUEST_CS_LIMIT, 0xffff); | |
989 | vmcs_write32(GUEST_CS_AR_BYTES, 0x9b); | |
990 | ||
991 | seg_setup(VCPU_SREG_DS); | |
992 | seg_setup(VCPU_SREG_ES); | |
993 | seg_setup(VCPU_SREG_FS); | |
994 | seg_setup(VCPU_SREG_GS); | |
995 | seg_setup(VCPU_SREG_SS); | |
996 | ||
997 | vmcs_write16(GUEST_TR_SELECTOR, 0); | |
998 | vmcs_writel(GUEST_TR_BASE, 0); | |
999 | vmcs_write32(GUEST_TR_LIMIT, 0xffff); | |
1000 | vmcs_write32(GUEST_TR_AR_BYTES, 0x008b); | |
1001 | ||
1002 | vmcs_write16(GUEST_LDTR_SELECTOR, 0); | |
1003 | vmcs_writel(GUEST_LDTR_BASE, 0); | |
1004 | vmcs_write32(GUEST_LDTR_LIMIT, 0xffff); | |
1005 | vmcs_write32(GUEST_LDTR_AR_BYTES, 0x00082); | |
1006 | ||
1007 | vmcs_write32(GUEST_SYSENTER_CS, 0); | |
1008 | vmcs_writel(GUEST_SYSENTER_ESP, 0); | |
1009 | vmcs_writel(GUEST_SYSENTER_EIP, 0); | |
1010 | ||
1011 | vmcs_writel(GUEST_RFLAGS, 0x02); | |
1012 | vmcs_writel(GUEST_RIP, 0xfff0); | |
1013 | vmcs_writel(GUEST_RSP, 0); | |
1014 | ||
1015 | vmcs_writel(GUEST_CR3, 0); | |
1016 | ||
1017 | //todo: dr0 = dr1 = dr2 = dr3 = 0; dr6 = 0xffff0ff0 | |
1018 | vmcs_writel(GUEST_DR7, 0x400); | |
1019 | ||
1020 | vmcs_writel(GUEST_GDTR_BASE, 0); | |
1021 | vmcs_write32(GUEST_GDTR_LIMIT, 0xffff); | |
1022 | ||
1023 | vmcs_writel(GUEST_IDTR_BASE, 0); | |
1024 | vmcs_write32(GUEST_IDTR_LIMIT, 0xffff); | |
1025 | ||
1026 | vmcs_write32(GUEST_ACTIVITY_STATE, 0); | |
1027 | vmcs_write32(GUEST_INTERRUPTIBILITY_INFO, 0); | |
1028 | vmcs_write32(GUEST_PENDING_DBG_EXCEPTIONS, 0); | |
1029 | ||
1030 | /* I/O */ | |
1031 | vmcs_write64(IO_BITMAP_A, 0); | |
1032 | vmcs_write64(IO_BITMAP_B, 0); | |
1033 | ||
1034 | guest_write_tsc(0); | |
1035 | ||
1036 | vmcs_write64(VMCS_LINK_POINTER, -1ull); /* 22.3.1.5 */ | |
1037 | ||
1038 | /* Special registers */ | |
1039 | vmcs_write64(GUEST_IA32_DEBUGCTL, 0); | |
1040 | ||
1041 | /* Control */ | |
1042 | vmcs_write32_fixedbits(MSR_IA32_VMX_PINBASED_CTLS_MSR, | |
1043 | PIN_BASED_VM_EXEC_CONTROL, | |
1044 | PIN_BASED_EXT_INTR_MASK /* 20.6.1 */ | |
1045 | | PIN_BASED_NMI_EXITING /* 20.6.1 */ | |
1046 | ); | |
1047 | vmcs_write32_fixedbits(MSR_IA32_VMX_PROCBASED_CTLS_MSR, | |
1048 | CPU_BASED_VM_EXEC_CONTROL, | |
1049 | CPU_BASED_HLT_EXITING /* 20.6.2 */ | |
1050 | | CPU_BASED_CR8_LOAD_EXITING /* 20.6.2 */ | |
1051 | | CPU_BASED_CR8_STORE_EXITING /* 20.6.2 */ | |
1052 | | CPU_BASED_UNCOND_IO_EXITING /* 20.6.2 */ | |
1053 | | CPU_BASED_INVDPG_EXITING | |
1054 | | CPU_BASED_MOV_DR_EXITING | |
1055 | | CPU_BASED_USE_TSC_OFFSETING /* 21.3 */ | |
1056 | ); | |
1057 | ||
1058 | vmcs_write32(EXCEPTION_BITMAP, 1 << PF_VECTOR); | |
1059 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MASK, 0); | |
1060 | vmcs_write32(PAGE_FAULT_ERROR_CODE_MATCH, 0); | |
1061 | vmcs_write32(CR3_TARGET_COUNT, 0); /* 22.2.1 */ | |
1062 | ||
1063 | vmcs_writel(HOST_CR0, read_cr0()); /* 22.2.3 */ | |
1064 | vmcs_writel(HOST_CR4, read_cr4()); /* 22.2.3, 22.2.5 */ | |
1065 | vmcs_writel(HOST_CR3, read_cr3()); /* 22.2.3 FIXME: shadow tables */ | |
1066 | ||
1067 | vmcs_write16(HOST_CS_SELECTOR, __KERNEL_CS); /* 22.2.4 */ | |
1068 | vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
1069 | vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
1070 | vmcs_write16(HOST_FS_SELECTOR, read_fs()); /* 22.2.4 */ | |
1071 | vmcs_write16(HOST_GS_SELECTOR, read_gs()); /* 22.2.4 */ | |
1072 | vmcs_write16(HOST_SS_SELECTOR, __KERNEL_DS); /* 22.2.4 */ | |
05b3e0c2 | 1073 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1074 | rdmsrl(MSR_FS_BASE, a); |
1075 | vmcs_writel(HOST_FS_BASE, a); /* 22.2.4 */ | |
1076 | rdmsrl(MSR_GS_BASE, a); | |
1077 | vmcs_writel(HOST_GS_BASE, a); /* 22.2.4 */ | |
1078 | #else | |
1079 | vmcs_writel(HOST_FS_BASE, 0); /* 22.2.4 */ | |
1080 | vmcs_writel(HOST_GS_BASE, 0); /* 22.2.4 */ | |
1081 | #endif | |
1082 | ||
1083 | vmcs_write16(HOST_TR_SELECTOR, GDT_ENTRY_TSS*8); /* 22.2.4 */ | |
1084 | ||
1085 | get_idt(&dt); | |
1086 | vmcs_writel(HOST_IDTR_BASE, dt.base); /* 22.2.4 */ | |
1087 | ||
1088 | ||
1089 | vmcs_writel(HOST_RIP, (unsigned long)kvm_vmx_return); /* 22.2.5 */ | |
1090 | ||
1091 | rdmsr(MSR_IA32_SYSENTER_CS, host_sysenter_cs, junk); | |
1092 | vmcs_write32(HOST_IA32_SYSENTER_CS, host_sysenter_cs); | |
1093 | rdmsrl(MSR_IA32_SYSENTER_ESP, a); | |
1094 | vmcs_writel(HOST_IA32_SYSENTER_ESP, a); /* 22.2.3 */ | |
1095 | rdmsrl(MSR_IA32_SYSENTER_EIP, a); | |
1096 | vmcs_writel(HOST_IA32_SYSENTER_EIP, a); /* 22.2.3 */ | |
1097 | ||
1098 | ret = -ENOMEM; | |
1099 | vcpu->guest_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1100 | if (!vcpu->guest_msrs) | |
1101 | goto out; | |
1102 | vcpu->host_msrs = kmalloc(PAGE_SIZE, GFP_KERNEL); | |
1103 | if (!vcpu->host_msrs) | |
1104 | goto out_free_guest_msrs; | |
1105 | ||
1106 | for (i = 0; i < NR_VMX_MSR; ++i) { | |
1107 | u32 index = vmx_msr_index[i]; | |
1108 | u32 data_low, data_high; | |
1109 | u64 data; | |
1110 | int j = vcpu->nmsrs; | |
1111 | ||
1112 | if (rdmsr_safe(index, &data_low, &data_high) < 0) | |
1113 | continue; | |
1114 | data = data_low | ((u64)data_high << 32); | |
1115 | vcpu->host_msrs[j].index = index; | |
1116 | vcpu->host_msrs[j].reserved = 0; | |
1117 | vcpu->host_msrs[j].data = data; | |
1118 | vcpu->guest_msrs[j] = vcpu->host_msrs[j]; | |
1119 | ++vcpu->nmsrs; | |
1120 | } | |
1121 | printk(KERN_DEBUG "kvm: msrs: %d\n", vcpu->nmsrs); | |
1122 | ||
1123 | nr_good_msrs = vcpu->nmsrs - NR_BAD_MSRS; | |
1124 | vmcs_writel(VM_ENTRY_MSR_LOAD_ADDR, | |
1125 | virt_to_phys(vcpu->guest_msrs + NR_BAD_MSRS)); | |
1126 | vmcs_writel(VM_EXIT_MSR_STORE_ADDR, | |
1127 | virt_to_phys(vcpu->guest_msrs + NR_BAD_MSRS)); | |
1128 | vmcs_writel(VM_EXIT_MSR_LOAD_ADDR, | |
1129 | virt_to_phys(vcpu->host_msrs + NR_BAD_MSRS)); | |
1130 | vmcs_write32_fixedbits(MSR_IA32_VMX_EXIT_CTLS_MSR, VM_EXIT_CONTROLS, | |
1131 | (HOST_IS_64 << 9)); /* 22.2,1, 20.7.1 */ | |
1132 | vmcs_write32(VM_EXIT_MSR_STORE_COUNT, nr_good_msrs); /* 22.2.2 */ | |
1133 | vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, nr_good_msrs); /* 22.2.2 */ | |
1134 | vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, nr_good_msrs); /* 22.2.2 */ | |
1135 | ||
1136 | ||
1137 | /* 22.2.1, 20.8.1 */ | |
1138 | vmcs_write32_fixedbits(MSR_IA32_VMX_ENTRY_CTLS_MSR, | |
1139 | VM_ENTRY_CONTROLS, 0); | |
1140 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, 0); /* 22.2.1 */ | |
1141 | ||
3b99ab24 | 1142 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1143 | vmcs_writel(VIRTUAL_APIC_PAGE_ADDR, 0); |
1144 | vmcs_writel(TPR_THRESHOLD, 0); | |
3b99ab24 | 1145 | #endif |
6aa8b732 AK |
1146 | |
1147 | vmcs_writel(CR0_GUEST_HOST_MASK, KVM_GUEST_CR0_MASK); | |
1148 | vmcs_writel(CR4_GUEST_HOST_MASK, KVM_GUEST_CR4_MASK); | |
1149 | ||
1150 | vcpu->cr0 = 0x60000010; | |
1151 | vmx_set_cr0(vcpu, vcpu->cr0); // enter rmode | |
1152 | vmx_set_cr4(vcpu, 0); | |
05b3e0c2 | 1153 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1154 | vmx_set_efer(vcpu, 0); |
1155 | #endif | |
1156 | ||
1157 | return 0; | |
1158 | ||
1159 | out_free_guest_msrs: | |
1160 | kfree(vcpu->guest_msrs); | |
1161 | out: | |
1162 | return ret; | |
1163 | } | |
1164 | ||
1165 | static void inject_rmode_irq(struct kvm_vcpu *vcpu, int irq) | |
1166 | { | |
1167 | u16 ent[2]; | |
1168 | u16 cs; | |
1169 | u16 ip; | |
1170 | unsigned long flags; | |
1171 | unsigned long ss_base = vmcs_readl(GUEST_SS_BASE); | |
1172 | u16 sp = vmcs_readl(GUEST_RSP); | |
1173 | u32 ss_limit = vmcs_read32(GUEST_SS_LIMIT); | |
1174 | ||
1175 | if (sp > ss_limit || sp - 6 > sp) { | |
1176 | vcpu_printf(vcpu, "%s: #SS, rsp 0x%lx ss 0x%lx limit 0x%x\n", | |
1177 | __FUNCTION__, | |
1178 | vmcs_readl(GUEST_RSP), | |
1179 | vmcs_readl(GUEST_SS_BASE), | |
1180 | vmcs_read32(GUEST_SS_LIMIT)); | |
1181 | return; | |
1182 | } | |
1183 | ||
1184 | if (kvm_read_guest(vcpu, irq * sizeof(ent), sizeof(ent), &ent) != | |
1185 | sizeof(ent)) { | |
1186 | vcpu_printf(vcpu, "%s: read guest err\n", __FUNCTION__); | |
1187 | return; | |
1188 | } | |
1189 | ||
1190 | flags = vmcs_readl(GUEST_RFLAGS); | |
1191 | cs = vmcs_readl(GUEST_CS_BASE) >> 4; | |
1192 | ip = vmcs_readl(GUEST_RIP); | |
1193 | ||
1194 | ||
1195 | if (kvm_write_guest(vcpu, ss_base + sp - 2, 2, &flags) != 2 || | |
1196 | kvm_write_guest(vcpu, ss_base + sp - 4, 2, &cs) != 2 || | |
1197 | kvm_write_guest(vcpu, ss_base + sp - 6, 2, &ip) != 2) { | |
1198 | vcpu_printf(vcpu, "%s: write guest err\n", __FUNCTION__); | |
1199 | return; | |
1200 | } | |
1201 | ||
1202 | vmcs_writel(GUEST_RFLAGS, flags & | |
1203 | ~( X86_EFLAGS_IF | X86_EFLAGS_AC | X86_EFLAGS_TF)); | |
1204 | vmcs_write16(GUEST_CS_SELECTOR, ent[1]) ; | |
1205 | vmcs_writel(GUEST_CS_BASE, ent[1] << 4); | |
1206 | vmcs_writel(GUEST_RIP, ent[0]); | |
1207 | vmcs_writel(GUEST_RSP, (vmcs_readl(GUEST_RSP) & ~0xffff) | (sp - 6)); | |
1208 | } | |
1209 | ||
1210 | static void kvm_do_inject_irq(struct kvm_vcpu *vcpu) | |
1211 | { | |
1212 | int word_index = __ffs(vcpu->irq_summary); | |
1213 | int bit_index = __ffs(vcpu->irq_pending[word_index]); | |
1214 | int irq = word_index * BITS_PER_LONG + bit_index; | |
1215 | ||
1216 | clear_bit(bit_index, &vcpu->irq_pending[word_index]); | |
1217 | if (!vcpu->irq_pending[word_index]) | |
1218 | clear_bit(word_index, &vcpu->irq_summary); | |
1219 | ||
1220 | if (vcpu->rmode.active) { | |
1221 | inject_rmode_irq(vcpu, irq); | |
1222 | return; | |
1223 | } | |
1224 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
1225 | irq | INTR_TYPE_EXT_INTR | INTR_INFO_VALID_MASK); | |
1226 | } | |
1227 | ||
1228 | static void kvm_try_inject_irq(struct kvm_vcpu *vcpu) | |
1229 | { | |
1230 | if ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF) | |
1231 | && (vmcs_read32(GUEST_INTERRUPTIBILITY_INFO) & 3) == 0) | |
1232 | /* | |
1233 | * Interrupts enabled, and not blocked by sti or mov ss. Good. | |
1234 | */ | |
1235 | kvm_do_inject_irq(vcpu); | |
1236 | else | |
1237 | /* | |
1238 | * Interrupts blocked. Wait for unblock. | |
1239 | */ | |
1240 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
1241 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1242 | | CPU_BASED_VIRTUAL_INTR_PENDING); | |
1243 | } | |
1244 | ||
1245 | static void kvm_guest_debug_pre(struct kvm_vcpu *vcpu) | |
1246 | { | |
1247 | struct kvm_guest_debug *dbg = &vcpu->guest_debug; | |
1248 | ||
1249 | set_debugreg(dbg->bp[0], 0); | |
1250 | set_debugreg(dbg->bp[1], 1); | |
1251 | set_debugreg(dbg->bp[2], 2); | |
1252 | set_debugreg(dbg->bp[3], 3); | |
1253 | ||
1254 | if (dbg->singlestep) { | |
1255 | unsigned long flags; | |
1256 | ||
1257 | flags = vmcs_readl(GUEST_RFLAGS); | |
1258 | flags |= X86_EFLAGS_TF | X86_EFLAGS_RF; | |
1259 | vmcs_writel(GUEST_RFLAGS, flags); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | static int handle_rmode_exception(struct kvm_vcpu *vcpu, | |
1264 | int vec, u32 err_code) | |
1265 | { | |
1266 | if (!vcpu->rmode.active) | |
1267 | return 0; | |
1268 | ||
1269 | if (vec == GP_VECTOR && err_code == 0) | |
1270 | if (emulate_instruction(vcpu, NULL, 0, 0) == EMULATE_DONE) | |
1271 | return 1; | |
1272 | return 0; | |
1273 | } | |
1274 | ||
1275 | static int handle_exception(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1276 | { | |
1277 | u32 intr_info, error_code; | |
1278 | unsigned long cr2, rip; | |
1279 | u32 vect_info; | |
1280 | enum emulation_result er; | |
1281 | ||
1282 | vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); | |
1283 | intr_info = vmcs_read32(VM_EXIT_INTR_INFO); | |
1284 | ||
1285 | if ((vect_info & VECTORING_INFO_VALID_MASK) && | |
1286 | !is_page_fault(intr_info)) { | |
1287 | printk(KERN_ERR "%s: unexpected, vectoring info 0x%x " | |
1288 | "intr info 0x%x\n", __FUNCTION__, vect_info, intr_info); | |
1289 | } | |
1290 | ||
1291 | if (is_external_interrupt(vect_info)) { | |
1292 | int irq = vect_info & VECTORING_INFO_VECTOR_MASK; | |
1293 | set_bit(irq, vcpu->irq_pending); | |
1294 | set_bit(irq / BITS_PER_LONG, &vcpu->irq_summary); | |
1295 | } | |
1296 | ||
1297 | if ((intr_info & INTR_INFO_INTR_TYPE_MASK) == 0x200) { /* nmi */ | |
1298 | asm ("int $2"); | |
1299 | return 1; | |
1300 | } | |
1301 | error_code = 0; | |
1302 | rip = vmcs_readl(GUEST_RIP); | |
1303 | if (intr_info & INTR_INFO_DELIEVER_CODE_MASK) | |
1304 | error_code = vmcs_read32(VM_EXIT_INTR_ERROR_CODE); | |
1305 | if (is_page_fault(intr_info)) { | |
1306 | cr2 = vmcs_readl(EXIT_QUALIFICATION); | |
1307 | ||
1308 | spin_lock(&vcpu->kvm->lock); | |
1309 | if (!vcpu->mmu.page_fault(vcpu, cr2, error_code)) { | |
1310 | spin_unlock(&vcpu->kvm->lock); | |
1311 | return 1; | |
1312 | } | |
1313 | ||
1314 | er = emulate_instruction(vcpu, kvm_run, cr2, error_code); | |
1315 | spin_unlock(&vcpu->kvm->lock); | |
1316 | ||
1317 | switch (er) { | |
1318 | case EMULATE_DONE: | |
1319 | return 1; | |
1320 | case EMULATE_DO_MMIO: | |
1321 | ++kvm_stat.mmio_exits; | |
1322 | kvm_run->exit_reason = KVM_EXIT_MMIO; | |
1323 | return 0; | |
1324 | case EMULATE_FAIL: | |
1325 | vcpu_printf(vcpu, "%s: emulate fail\n", __FUNCTION__); | |
1326 | break; | |
1327 | default: | |
1328 | BUG(); | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | if (vcpu->rmode.active && | |
1333 | handle_rmode_exception(vcpu, intr_info & INTR_INFO_VECTOR_MASK, | |
1334 | error_code)) | |
1335 | return 1; | |
1336 | ||
1337 | if ((intr_info & (INTR_INFO_INTR_TYPE_MASK | INTR_INFO_VECTOR_MASK)) == (INTR_TYPE_EXCEPTION | 1)) { | |
1338 | kvm_run->exit_reason = KVM_EXIT_DEBUG; | |
1339 | return 0; | |
1340 | } | |
1341 | kvm_run->exit_reason = KVM_EXIT_EXCEPTION; | |
1342 | kvm_run->ex.exception = intr_info & INTR_INFO_VECTOR_MASK; | |
1343 | kvm_run->ex.error_code = error_code; | |
1344 | return 0; | |
1345 | } | |
1346 | ||
1347 | static int handle_external_interrupt(struct kvm_vcpu *vcpu, | |
1348 | struct kvm_run *kvm_run) | |
1349 | { | |
1350 | ++kvm_stat.irq_exits; | |
1351 | return 1; | |
1352 | } | |
1353 | ||
1354 | ||
1355 | static int get_io_count(struct kvm_vcpu *vcpu, u64 *count) | |
1356 | { | |
1357 | u64 inst; | |
1358 | gva_t rip; | |
1359 | int countr_size; | |
1360 | int i, n; | |
1361 | ||
1362 | if ((vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_VM)) { | |
1363 | countr_size = 2; | |
1364 | } else { | |
1365 | u32 cs_ar = vmcs_read32(GUEST_CS_AR_BYTES); | |
1366 | ||
1367 | countr_size = (cs_ar & AR_L_MASK) ? 8: | |
1368 | (cs_ar & AR_DB_MASK) ? 4: 2; | |
1369 | } | |
1370 | ||
1371 | rip = vmcs_readl(GUEST_RIP); | |
1372 | if (countr_size != 8) | |
1373 | rip += vmcs_readl(GUEST_CS_BASE); | |
1374 | ||
1375 | n = kvm_read_guest(vcpu, rip, sizeof(inst), &inst); | |
1376 | ||
1377 | for (i = 0; i < n; i++) { | |
1378 | switch (((u8*)&inst)[i]) { | |
1379 | case 0xf0: | |
1380 | case 0xf2: | |
1381 | case 0xf3: | |
1382 | case 0x2e: | |
1383 | case 0x36: | |
1384 | case 0x3e: | |
1385 | case 0x26: | |
1386 | case 0x64: | |
1387 | case 0x65: | |
1388 | case 0x66: | |
1389 | break; | |
1390 | case 0x67: | |
1391 | countr_size = (countr_size == 2) ? 4: (countr_size >> 1); | |
1392 | default: | |
1393 | goto done; | |
1394 | } | |
1395 | } | |
1396 | return 0; | |
1397 | done: | |
1398 | countr_size *= 8; | |
1399 | *count = vcpu->regs[VCPU_REGS_RCX] & (~0ULL >> (64 - countr_size)); | |
1400 | return 1; | |
1401 | } | |
1402 | ||
1403 | static int handle_io(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1404 | { | |
1405 | u64 exit_qualification; | |
1406 | ||
1407 | ++kvm_stat.io_exits; | |
1408 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); | |
1409 | kvm_run->exit_reason = KVM_EXIT_IO; | |
1410 | if (exit_qualification & 8) | |
1411 | kvm_run->io.direction = KVM_EXIT_IO_IN; | |
1412 | else | |
1413 | kvm_run->io.direction = KVM_EXIT_IO_OUT; | |
1414 | kvm_run->io.size = (exit_qualification & 7) + 1; | |
1415 | kvm_run->io.string = (exit_qualification & 16) != 0; | |
1416 | kvm_run->io.string_down | |
1417 | = (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_DF) != 0; | |
1418 | kvm_run->io.rep = (exit_qualification & 32) != 0; | |
1419 | kvm_run->io.port = exit_qualification >> 16; | |
1420 | if (kvm_run->io.string) { | |
1421 | if (!get_io_count(vcpu, &kvm_run->io.count)) | |
1422 | return 1; | |
1423 | kvm_run->io.address = vmcs_readl(GUEST_LINEAR_ADDRESS); | |
1424 | } else | |
1425 | kvm_run->io.value = vcpu->regs[VCPU_REGS_RAX]; /* rax */ | |
1426 | return 0; | |
1427 | } | |
1428 | ||
1429 | static int handle_invlpg(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1430 | { | |
1431 | u64 address = vmcs_read64(EXIT_QUALIFICATION); | |
1432 | int instruction_length = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
1433 | spin_lock(&vcpu->kvm->lock); | |
1434 | vcpu->mmu.inval_page(vcpu, address); | |
1435 | spin_unlock(&vcpu->kvm->lock); | |
1436 | vmcs_writel(GUEST_RIP, vmcs_readl(GUEST_RIP) + instruction_length); | |
1437 | return 1; | |
1438 | } | |
1439 | ||
1440 | static int handle_cr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1441 | { | |
1442 | u64 exit_qualification; | |
1443 | int cr; | |
1444 | int reg; | |
1445 | ||
1446 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); | |
1447 | cr = exit_qualification & 15; | |
1448 | reg = (exit_qualification >> 8) & 15; | |
1449 | switch ((exit_qualification >> 4) & 3) { | |
1450 | case 0: /* mov to cr */ | |
1451 | switch (cr) { | |
1452 | case 0: | |
1453 | vcpu_load_rsp_rip(vcpu); | |
1454 | set_cr0(vcpu, vcpu->regs[reg]); | |
1455 | skip_emulated_instruction(vcpu); | |
1456 | return 1; | |
1457 | case 3: | |
1458 | vcpu_load_rsp_rip(vcpu); | |
1459 | set_cr3(vcpu, vcpu->regs[reg]); | |
1460 | skip_emulated_instruction(vcpu); | |
1461 | return 1; | |
1462 | case 4: | |
1463 | vcpu_load_rsp_rip(vcpu); | |
1464 | set_cr4(vcpu, vcpu->regs[reg]); | |
1465 | skip_emulated_instruction(vcpu); | |
1466 | return 1; | |
1467 | case 8: | |
1468 | vcpu_load_rsp_rip(vcpu); | |
1469 | set_cr8(vcpu, vcpu->regs[reg]); | |
1470 | skip_emulated_instruction(vcpu); | |
1471 | return 1; | |
1472 | }; | |
1473 | break; | |
1474 | case 1: /*mov from cr*/ | |
1475 | switch (cr) { | |
1476 | case 3: | |
1477 | vcpu_load_rsp_rip(vcpu); | |
1478 | vcpu->regs[reg] = vcpu->cr3; | |
1479 | vcpu_put_rsp_rip(vcpu); | |
1480 | skip_emulated_instruction(vcpu); | |
1481 | return 1; | |
1482 | case 8: | |
1483 | printk(KERN_DEBUG "handle_cr: read CR8 " | |
1484 | "cpu erratum AA15\n"); | |
1485 | vcpu_load_rsp_rip(vcpu); | |
1486 | vcpu->regs[reg] = vcpu->cr8; | |
1487 | vcpu_put_rsp_rip(vcpu); | |
1488 | skip_emulated_instruction(vcpu); | |
1489 | return 1; | |
1490 | } | |
1491 | break; | |
1492 | case 3: /* lmsw */ | |
1493 | lmsw(vcpu, (exit_qualification >> LMSW_SOURCE_DATA_SHIFT) & 0x0f); | |
1494 | ||
1495 | skip_emulated_instruction(vcpu); | |
1496 | return 1; | |
1497 | default: | |
1498 | break; | |
1499 | } | |
1500 | kvm_run->exit_reason = 0; | |
1501 | printk(KERN_ERR "kvm: unhandled control register: op %d cr %d\n", | |
1502 | (int)(exit_qualification >> 4) & 3, cr); | |
1503 | return 0; | |
1504 | } | |
1505 | ||
1506 | static int handle_dr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1507 | { | |
1508 | u64 exit_qualification; | |
1509 | unsigned long val; | |
1510 | int dr, reg; | |
1511 | ||
1512 | /* | |
1513 | * FIXME: this code assumes the host is debugging the guest. | |
1514 | * need to deal with guest debugging itself too. | |
1515 | */ | |
1516 | exit_qualification = vmcs_read64(EXIT_QUALIFICATION); | |
1517 | dr = exit_qualification & 7; | |
1518 | reg = (exit_qualification >> 8) & 15; | |
1519 | vcpu_load_rsp_rip(vcpu); | |
1520 | if (exit_qualification & 16) { | |
1521 | /* mov from dr */ | |
1522 | switch (dr) { | |
1523 | case 6: | |
1524 | val = 0xffff0ff0; | |
1525 | break; | |
1526 | case 7: | |
1527 | val = 0x400; | |
1528 | break; | |
1529 | default: | |
1530 | val = 0; | |
1531 | } | |
1532 | vcpu->regs[reg] = val; | |
1533 | } else { | |
1534 | /* mov to dr */ | |
1535 | } | |
1536 | vcpu_put_rsp_rip(vcpu); | |
1537 | skip_emulated_instruction(vcpu); | |
1538 | return 1; | |
1539 | } | |
1540 | ||
1541 | static int handle_cpuid(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1542 | { | |
1543 | kvm_run->exit_reason = KVM_EXIT_CPUID; | |
1544 | return 0; | |
1545 | } | |
1546 | ||
1547 | static int handle_rdmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1548 | { | |
1549 | u32 ecx = vcpu->regs[VCPU_REGS_RCX]; | |
1550 | u64 data; | |
1551 | ||
1552 | if (vmx_get_msr(vcpu, ecx, &data)) { | |
1553 | vmx_inject_gp(vcpu, 0); | |
1554 | return 1; | |
1555 | } | |
1556 | ||
1557 | /* FIXME: handling of bits 32:63 of rax, rdx */ | |
1558 | vcpu->regs[VCPU_REGS_RAX] = data & -1u; | |
1559 | vcpu->regs[VCPU_REGS_RDX] = (data >> 32) & -1u; | |
1560 | skip_emulated_instruction(vcpu); | |
1561 | return 1; | |
1562 | } | |
1563 | ||
1564 | static int handle_wrmsr(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1565 | { | |
1566 | u32 ecx = vcpu->regs[VCPU_REGS_RCX]; | |
1567 | u64 data = (vcpu->regs[VCPU_REGS_RAX] & -1u) | |
1568 | | ((u64)(vcpu->regs[VCPU_REGS_RDX] & -1u) << 32); | |
1569 | ||
1570 | if (vmx_set_msr(vcpu, ecx, data) != 0) { | |
1571 | vmx_inject_gp(vcpu, 0); | |
1572 | return 1; | |
1573 | } | |
1574 | ||
1575 | skip_emulated_instruction(vcpu); | |
1576 | return 1; | |
1577 | } | |
1578 | ||
1579 | static int handle_interrupt_window(struct kvm_vcpu *vcpu, | |
1580 | struct kvm_run *kvm_run) | |
1581 | { | |
1582 | /* Turn off interrupt window reporting. */ | |
1583 | vmcs_write32(CPU_BASED_VM_EXEC_CONTROL, | |
1584 | vmcs_read32(CPU_BASED_VM_EXEC_CONTROL) | |
1585 | & ~CPU_BASED_VIRTUAL_INTR_PENDING); | |
1586 | return 1; | |
1587 | } | |
1588 | ||
1589 | static int handle_halt(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1590 | { | |
1591 | skip_emulated_instruction(vcpu); | |
1592 | if (vcpu->irq_summary && (vmcs_readl(GUEST_RFLAGS) & X86_EFLAGS_IF)) | |
1593 | return 1; | |
1594 | ||
1595 | kvm_run->exit_reason = KVM_EXIT_HLT; | |
1596 | return 0; | |
1597 | } | |
1598 | ||
1599 | /* | |
1600 | * The exit handlers return 1 if the exit was handled fully and guest execution | |
1601 | * may resume. Otherwise they set the kvm_run parameter to indicate what needs | |
1602 | * to be done to userspace and return 0. | |
1603 | */ | |
1604 | static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu, | |
1605 | struct kvm_run *kvm_run) = { | |
1606 | [EXIT_REASON_EXCEPTION_NMI] = handle_exception, | |
1607 | [EXIT_REASON_EXTERNAL_INTERRUPT] = handle_external_interrupt, | |
1608 | [EXIT_REASON_IO_INSTRUCTION] = handle_io, | |
1609 | [EXIT_REASON_INVLPG] = handle_invlpg, | |
1610 | [EXIT_REASON_CR_ACCESS] = handle_cr, | |
1611 | [EXIT_REASON_DR_ACCESS] = handle_dr, | |
1612 | [EXIT_REASON_CPUID] = handle_cpuid, | |
1613 | [EXIT_REASON_MSR_READ] = handle_rdmsr, | |
1614 | [EXIT_REASON_MSR_WRITE] = handle_wrmsr, | |
1615 | [EXIT_REASON_PENDING_INTERRUPT] = handle_interrupt_window, | |
1616 | [EXIT_REASON_HLT] = handle_halt, | |
1617 | }; | |
1618 | ||
1619 | static const int kvm_vmx_max_exit_handlers = | |
1620 | sizeof(kvm_vmx_exit_handlers) / sizeof(*kvm_vmx_exit_handlers); | |
1621 | ||
1622 | /* | |
1623 | * The guest has exited. See if we can fix it or if we need userspace | |
1624 | * assistance. | |
1625 | */ | |
1626 | static int kvm_handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) | |
1627 | { | |
1628 | u32 vectoring_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); | |
1629 | u32 exit_reason = vmcs_read32(VM_EXIT_REASON); | |
1630 | ||
1631 | if ( (vectoring_info & VECTORING_INFO_VALID_MASK) && | |
1632 | exit_reason != EXIT_REASON_EXCEPTION_NMI ) | |
1633 | printk(KERN_WARNING "%s: unexpected, valid vectoring info and " | |
1634 | "exit reason is 0x%x\n", __FUNCTION__, exit_reason); | |
1635 | kvm_run->instruction_length = vmcs_read32(VM_EXIT_INSTRUCTION_LEN); | |
1636 | if (exit_reason < kvm_vmx_max_exit_handlers | |
1637 | && kvm_vmx_exit_handlers[exit_reason]) | |
1638 | return kvm_vmx_exit_handlers[exit_reason](vcpu, kvm_run); | |
1639 | else { | |
1640 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; | |
1641 | kvm_run->hw.hardware_exit_reason = exit_reason; | |
1642 | } | |
1643 | return 0; | |
1644 | } | |
1645 | ||
1646 | static int vmx_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
1647 | { | |
1648 | u8 fail; | |
1649 | u16 fs_sel, gs_sel, ldt_sel; | |
1650 | int fs_gs_ldt_reload_needed; | |
1651 | ||
1652 | again: | |
1653 | /* | |
1654 | * Set host fs and gs selectors. Unfortunately, 22.2.3 does not | |
1655 | * allow segment selectors with cpl > 0 or ti == 1. | |
1656 | */ | |
1657 | fs_sel = read_fs(); | |
1658 | gs_sel = read_gs(); | |
1659 | ldt_sel = read_ldt(); | |
1660 | fs_gs_ldt_reload_needed = (fs_sel & 7) | (gs_sel & 7) | ldt_sel; | |
1661 | if (!fs_gs_ldt_reload_needed) { | |
1662 | vmcs_write16(HOST_FS_SELECTOR, fs_sel); | |
1663 | vmcs_write16(HOST_GS_SELECTOR, gs_sel); | |
1664 | } else { | |
1665 | vmcs_write16(HOST_FS_SELECTOR, 0); | |
1666 | vmcs_write16(HOST_GS_SELECTOR, 0); | |
1667 | } | |
1668 | ||
05b3e0c2 | 1669 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1670 | vmcs_writel(HOST_FS_BASE, read_msr(MSR_FS_BASE)); |
1671 | vmcs_writel(HOST_GS_BASE, read_msr(MSR_GS_BASE)); | |
1672 | #else | |
1673 | vmcs_writel(HOST_FS_BASE, segment_base(fs_sel)); | |
1674 | vmcs_writel(HOST_GS_BASE, segment_base(gs_sel)); | |
1675 | #endif | |
1676 | ||
1677 | if (vcpu->irq_summary && | |
1678 | !(vmcs_read32(VM_ENTRY_INTR_INFO_FIELD) & INTR_INFO_VALID_MASK)) | |
1679 | kvm_try_inject_irq(vcpu); | |
1680 | ||
1681 | if (vcpu->guest_debug.enabled) | |
1682 | kvm_guest_debug_pre(vcpu); | |
1683 | ||
1684 | fx_save(vcpu->host_fx_image); | |
1685 | fx_restore(vcpu->guest_fx_image); | |
1686 | ||
1687 | save_msrs(vcpu->host_msrs, vcpu->nmsrs); | |
1688 | load_msrs(vcpu->guest_msrs, NR_BAD_MSRS); | |
1689 | ||
1690 | asm ( | |
1691 | /* Store host registers */ | |
1692 | "pushf \n\t" | |
05b3e0c2 | 1693 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1694 | "push %%rax; push %%rbx; push %%rdx;" |
1695 | "push %%rsi; push %%rdi; push %%rbp;" | |
1696 | "push %%r8; push %%r9; push %%r10; push %%r11;" | |
1697 | "push %%r12; push %%r13; push %%r14; push %%r15;" | |
1698 | "push %%rcx \n\t" | |
1699 | ASM_VMX_VMWRITE_RSP_RDX "\n\t" | |
1700 | #else | |
1701 | "pusha; push %%ecx \n\t" | |
1702 | ASM_VMX_VMWRITE_RSP_RDX "\n\t" | |
1703 | #endif | |
1704 | /* Check if vmlaunch of vmresume is needed */ | |
1705 | "cmp $0, %1 \n\t" | |
1706 | /* Load guest registers. Don't clobber flags. */ | |
05b3e0c2 | 1707 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1708 | "mov %c[cr2](%3), %%rax \n\t" |
1709 | "mov %%rax, %%cr2 \n\t" | |
1710 | "mov %c[rax](%3), %%rax \n\t" | |
1711 | "mov %c[rbx](%3), %%rbx \n\t" | |
1712 | "mov %c[rdx](%3), %%rdx \n\t" | |
1713 | "mov %c[rsi](%3), %%rsi \n\t" | |
1714 | "mov %c[rdi](%3), %%rdi \n\t" | |
1715 | "mov %c[rbp](%3), %%rbp \n\t" | |
1716 | "mov %c[r8](%3), %%r8 \n\t" | |
1717 | "mov %c[r9](%3), %%r9 \n\t" | |
1718 | "mov %c[r10](%3), %%r10 \n\t" | |
1719 | "mov %c[r11](%3), %%r11 \n\t" | |
1720 | "mov %c[r12](%3), %%r12 \n\t" | |
1721 | "mov %c[r13](%3), %%r13 \n\t" | |
1722 | "mov %c[r14](%3), %%r14 \n\t" | |
1723 | "mov %c[r15](%3), %%r15 \n\t" | |
1724 | "mov %c[rcx](%3), %%rcx \n\t" /* kills %3 (rcx) */ | |
1725 | #else | |
1726 | "mov %c[cr2](%3), %%eax \n\t" | |
1727 | "mov %%eax, %%cr2 \n\t" | |
1728 | "mov %c[rax](%3), %%eax \n\t" | |
1729 | "mov %c[rbx](%3), %%ebx \n\t" | |
1730 | "mov %c[rdx](%3), %%edx \n\t" | |
1731 | "mov %c[rsi](%3), %%esi \n\t" | |
1732 | "mov %c[rdi](%3), %%edi \n\t" | |
1733 | "mov %c[rbp](%3), %%ebp \n\t" | |
1734 | "mov %c[rcx](%3), %%ecx \n\t" /* kills %3 (ecx) */ | |
1735 | #endif | |
1736 | /* Enter guest mode */ | |
1737 | "jne launched \n\t" | |
1738 | ASM_VMX_VMLAUNCH "\n\t" | |
1739 | "jmp kvm_vmx_return \n\t" | |
1740 | "launched: " ASM_VMX_VMRESUME "\n\t" | |
1741 | ".globl kvm_vmx_return \n\t" | |
1742 | "kvm_vmx_return: " | |
1743 | /* Save guest registers, load host registers, keep flags */ | |
05b3e0c2 | 1744 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1745 | "xchg %3, 0(%%rsp) \n\t" |
1746 | "mov %%rax, %c[rax](%3) \n\t" | |
1747 | "mov %%rbx, %c[rbx](%3) \n\t" | |
1748 | "pushq 0(%%rsp); popq %c[rcx](%3) \n\t" | |
1749 | "mov %%rdx, %c[rdx](%3) \n\t" | |
1750 | "mov %%rsi, %c[rsi](%3) \n\t" | |
1751 | "mov %%rdi, %c[rdi](%3) \n\t" | |
1752 | "mov %%rbp, %c[rbp](%3) \n\t" | |
1753 | "mov %%r8, %c[r8](%3) \n\t" | |
1754 | "mov %%r9, %c[r9](%3) \n\t" | |
1755 | "mov %%r10, %c[r10](%3) \n\t" | |
1756 | "mov %%r11, %c[r11](%3) \n\t" | |
1757 | "mov %%r12, %c[r12](%3) \n\t" | |
1758 | "mov %%r13, %c[r13](%3) \n\t" | |
1759 | "mov %%r14, %c[r14](%3) \n\t" | |
1760 | "mov %%r15, %c[r15](%3) \n\t" | |
1761 | "mov %%cr2, %%rax \n\t" | |
1762 | "mov %%rax, %c[cr2](%3) \n\t" | |
1763 | "mov 0(%%rsp), %3 \n\t" | |
1764 | ||
1765 | "pop %%rcx; pop %%r15; pop %%r14; pop %%r13; pop %%r12;" | |
1766 | "pop %%r11; pop %%r10; pop %%r9; pop %%r8;" | |
1767 | "pop %%rbp; pop %%rdi; pop %%rsi;" | |
1768 | "pop %%rdx; pop %%rbx; pop %%rax \n\t" | |
1769 | #else | |
1770 | "xchg %3, 0(%%esp) \n\t" | |
1771 | "mov %%eax, %c[rax](%3) \n\t" | |
1772 | "mov %%ebx, %c[rbx](%3) \n\t" | |
1773 | "pushl 0(%%esp); popl %c[rcx](%3) \n\t" | |
1774 | "mov %%edx, %c[rdx](%3) \n\t" | |
1775 | "mov %%esi, %c[rsi](%3) \n\t" | |
1776 | "mov %%edi, %c[rdi](%3) \n\t" | |
1777 | "mov %%ebp, %c[rbp](%3) \n\t" | |
1778 | "mov %%cr2, %%eax \n\t" | |
1779 | "mov %%eax, %c[cr2](%3) \n\t" | |
1780 | "mov 0(%%esp), %3 \n\t" | |
1781 | ||
1782 | "pop %%ecx; popa \n\t" | |
1783 | #endif | |
1784 | "setbe %0 \n\t" | |
1785 | "popf \n\t" | |
1786 | : "=g" (fail) | |
1787 | : "r"(vcpu->launched), "d"((unsigned long)HOST_RSP), | |
1788 | "c"(vcpu), | |
1789 | [rax]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RAX])), | |
1790 | [rbx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBX])), | |
1791 | [rcx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RCX])), | |
1792 | [rdx]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDX])), | |
1793 | [rsi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RSI])), | |
1794 | [rdi]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RDI])), | |
1795 | [rbp]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_RBP])), | |
05b3e0c2 | 1796 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1797 | [r8 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R8 ])), |
1798 | [r9 ]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R9 ])), | |
1799 | [r10]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R10])), | |
1800 | [r11]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R11])), | |
1801 | [r12]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R12])), | |
1802 | [r13]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R13])), | |
1803 | [r14]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R14])), | |
1804 | [r15]"i"(offsetof(struct kvm_vcpu, regs[VCPU_REGS_R15])), | |
1805 | #endif | |
1806 | [cr2]"i"(offsetof(struct kvm_vcpu, cr2)) | |
1807 | : "cc", "memory" ); | |
1808 | ||
1809 | ++kvm_stat.exits; | |
1810 | ||
1811 | save_msrs(vcpu->guest_msrs, NR_BAD_MSRS); | |
1812 | load_msrs(vcpu->host_msrs, NR_BAD_MSRS); | |
1813 | ||
1814 | fx_save(vcpu->guest_fx_image); | |
1815 | fx_restore(vcpu->host_fx_image); | |
1816 | ||
05b3e0c2 | 1817 | #ifndef CONFIG_X86_64 |
6aa8b732 AK |
1818 | asm ("mov %0, %%ds; mov %0, %%es" : : "r"(__USER_DS)); |
1819 | #endif | |
1820 | ||
1821 | kvm_run->exit_type = 0; | |
1822 | if (fail) { | |
1823 | kvm_run->exit_type = KVM_EXIT_TYPE_FAIL_ENTRY; | |
1824 | kvm_run->exit_reason = vmcs_read32(VM_INSTRUCTION_ERROR); | |
1825 | } else { | |
1826 | if (fs_gs_ldt_reload_needed) { | |
1827 | load_ldt(ldt_sel); | |
1828 | load_fs(fs_sel); | |
1829 | /* | |
1830 | * If we have to reload gs, we must take care to | |
1831 | * preserve our gs base. | |
1832 | */ | |
1833 | local_irq_disable(); | |
1834 | load_gs(gs_sel); | |
05b3e0c2 | 1835 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1836 | wrmsrl(MSR_GS_BASE, vmcs_readl(HOST_GS_BASE)); |
1837 | #endif | |
1838 | local_irq_enable(); | |
1839 | ||
1840 | reload_tss(); | |
1841 | } | |
1842 | vcpu->launched = 1; | |
1843 | kvm_run->exit_type = KVM_EXIT_TYPE_VM_EXIT; | |
1844 | if (kvm_handle_exit(kvm_run, vcpu)) { | |
1845 | /* Give scheduler a change to reschedule. */ | |
1846 | if (signal_pending(current)) { | |
1847 | ++kvm_stat.signal_exits; | |
1848 | return -EINTR; | |
1849 | } | |
1850 | kvm_resched(vcpu); | |
1851 | goto again; | |
1852 | } | |
1853 | } | |
1854 | return 0; | |
1855 | } | |
1856 | ||
1857 | static void vmx_flush_tlb(struct kvm_vcpu *vcpu) | |
1858 | { | |
1859 | vmcs_writel(GUEST_CR3, vmcs_readl(GUEST_CR3)); | |
1860 | } | |
1861 | ||
1862 | static void vmx_inject_page_fault(struct kvm_vcpu *vcpu, | |
1863 | unsigned long addr, | |
1864 | u32 err_code) | |
1865 | { | |
1866 | u32 vect_info = vmcs_read32(IDT_VECTORING_INFO_FIELD); | |
1867 | ||
1868 | ++kvm_stat.pf_guest; | |
1869 | ||
1870 | if (is_page_fault(vect_info)) { | |
1871 | printk(KERN_DEBUG "inject_page_fault: " | |
1872 | "double fault 0x%lx @ 0x%lx\n", | |
1873 | addr, vmcs_readl(GUEST_RIP)); | |
1874 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, 0); | |
1875 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
1876 | DF_VECTOR | | |
1877 | INTR_TYPE_EXCEPTION | | |
1878 | INTR_INFO_DELIEVER_CODE_MASK | | |
1879 | INTR_INFO_VALID_MASK); | |
1880 | return; | |
1881 | } | |
1882 | vcpu->cr2 = addr; | |
1883 | vmcs_write32(VM_ENTRY_EXCEPTION_ERROR_CODE, err_code); | |
1884 | vmcs_write32(VM_ENTRY_INTR_INFO_FIELD, | |
1885 | PF_VECTOR | | |
1886 | INTR_TYPE_EXCEPTION | | |
1887 | INTR_INFO_DELIEVER_CODE_MASK | | |
1888 | INTR_INFO_VALID_MASK); | |
1889 | ||
1890 | } | |
1891 | ||
1892 | static void vmx_free_vmcs(struct kvm_vcpu *vcpu) | |
1893 | { | |
1894 | if (vcpu->vmcs) { | |
1895 | on_each_cpu(__vcpu_clear, vcpu, 0, 1); | |
1896 | free_vmcs(vcpu->vmcs); | |
1897 | vcpu->vmcs = NULL; | |
1898 | } | |
1899 | } | |
1900 | ||
1901 | static void vmx_free_vcpu(struct kvm_vcpu *vcpu) | |
1902 | { | |
1903 | vmx_free_vmcs(vcpu); | |
1904 | } | |
1905 | ||
1906 | static int vmx_create_vcpu(struct kvm_vcpu *vcpu) | |
1907 | { | |
1908 | struct vmcs *vmcs; | |
1909 | ||
1910 | vmcs = alloc_vmcs(); | |
1911 | if (!vmcs) | |
1912 | return -ENOMEM; | |
1913 | vmcs_clear(vmcs); | |
1914 | vcpu->vmcs = vmcs; | |
1915 | vcpu->launched = 0; | |
1916 | return 0; | |
1917 | } | |
1918 | ||
1919 | static struct kvm_arch_ops vmx_arch_ops = { | |
1920 | .cpu_has_kvm_support = cpu_has_kvm_support, | |
1921 | .disabled_by_bios = vmx_disabled_by_bios, | |
1922 | .hardware_setup = hardware_setup, | |
1923 | .hardware_unsetup = hardware_unsetup, | |
1924 | .hardware_enable = hardware_enable, | |
1925 | .hardware_disable = hardware_disable, | |
1926 | ||
1927 | .vcpu_create = vmx_create_vcpu, | |
1928 | .vcpu_free = vmx_free_vcpu, | |
1929 | ||
1930 | .vcpu_load = vmx_vcpu_load, | |
1931 | .vcpu_put = vmx_vcpu_put, | |
1932 | ||
1933 | .set_guest_debug = set_guest_debug, | |
1934 | .get_msr = vmx_get_msr, | |
1935 | .set_msr = vmx_set_msr, | |
1936 | .get_segment_base = vmx_get_segment_base, | |
1937 | .get_segment = vmx_get_segment, | |
1938 | .set_segment = vmx_set_segment, | |
6aa8b732 AK |
1939 | .get_cs_db_l_bits = vmx_get_cs_db_l_bits, |
1940 | .set_cr0 = vmx_set_cr0, | |
1941 | .set_cr0_no_modeswitch = vmx_set_cr0_no_modeswitch, | |
1942 | .set_cr3 = vmx_set_cr3, | |
1943 | .set_cr4 = vmx_set_cr4, | |
05b3e0c2 | 1944 | #ifdef CONFIG_X86_64 |
6aa8b732 AK |
1945 | .set_efer = vmx_set_efer, |
1946 | #endif | |
1947 | .get_idt = vmx_get_idt, | |
1948 | .set_idt = vmx_set_idt, | |
1949 | .get_gdt = vmx_get_gdt, | |
1950 | .set_gdt = vmx_set_gdt, | |
1951 | .cache_regs = vcpu_load_rsp_rip, | |
1952 | .decache_regs = vcpu_put_rsp_rip, | |
1953 | .get_rflags = vmx_get_rflags, | |
1954 | .set_rflags = vmx_set_rflags, | |
1955 | ||
1956 | .tlb_flush = vmx_flush_tlb, | |
1957 | .inject_page_fault = vmx_inject_page_fault, | |
1958 | ||
1959 | .inject_gp = vmx_inject_gp, | |
1960 | ||
1961 | .run = vmx_vcpu_run, | |
1962 | .skip_emulated_instruction = skip_emulated_instruction, | |
1963 | .vcpu_setup = vmx_vcpu_setup, | |
1964 | }; | |
1965 | ||
1966 | static int __init vmx_init(void) | |
1967 | { | |
873a7c42 | 1968 | return kvm_init_arch(&vmx_arch_ops, THIS_MODULE); |
6aa8b732 AK |
1969 | } |
1970 | ||
1971 | static void __exit vmx_exit(void) | |
1972 | { | |
1973 | kvm_exit_arch(); | |
1974 | } | |
1975 | ||
1976 | module_init(vmx_init) | |
1977 | module_exit(vmx_exit) |