]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * AMD SVM support | |
5 | * | |
6 | * Copyright (C) 2006 Qumranet, Inc. | |
7 | * | |
8 | * Authors: | |
9 | * Yaniv Kamay <yaniv@qumranet.com> | |
10 | * Avi Kivity <avi@qumranet.com> | |
11 | * | |
12 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
13 | * the COPYING file in the top-level directory. | |
14 | * | |
15 | */ | |
16 | #include <linux/kvm_host.h> | |
17 | ||
18 | #include "kvm_svm.h" | |
19 | #include "irq.h" | |
20 | #include "mmu.h" | |
21 | #include "kvm_cache_regs.h" | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/kernel.h> | |
25 | #include <linux/vmalloc.h> | |
26 | #include <linux/highmem.h> | |
27 | #include <linux/sched.h> | |
28 | ||
29 | #include <asm/desc.h> | |
30 | ||
31 | #include <asm/virtext.h> | |
32 | ||
33 | #define __ex(x) __kvm_handle_fault_on_reboot(x) | |
34 | ||
35 | MODULE_AUTHOR("Qumranet"); | |
36 | MODULE_LICENSE("GPL"); | |
37 | ||
38 | #define IOPM_ALLOC_ORDER 2 | |
39 | #define MSRPM_ALLOC_ORDER 1 | |
40 | ||
41 | #define DR7_GD_MASK (1 << 13) | |
42 | #define DR6_BD_MASK (1 << 13) | |
43 | ||
44 | #define SEG_TYPE_LDT 2 | |
45 | #define SEG_TYPE_BUSY_TSS16 3 | |
46 | ||
47 | #define SVM_FEATURE_NPT (1 << 0) | |
48 | #define SVM_FEATURE_LBRV (1 << 1) | |
49 | #define SVM_FEATURE_SVML (1 << 2) | |
50 | ||
51 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) | |
52 | ||
53 | /* Turn on to get debugging output*/ | |
54 | /* #define NESTED_DEBUG */ | |
55 | ||
56 | #ifdef NESTED_DEBUG | |
57 | #define nsvm_printk(fmt, args...) printk(KERN_INFO fmt, ## args) | |
58 | #else | |
59 | #define nsvm_printk(fmt, args...) do {} while(0) | |
60 | #endif | |
61 | ||
62 | /* enable NPT for AMD64 and X86 with PAE */ | |
63 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) | |
64 | static bool npt_enabled = true; | |
65 | #else | |
66 | static bool npt_enabled = false; | |
67 | #endif | |
68 | static int npt = 1; | |
69 | ||
70 | module_param(npt, int, S_IRUGO); | |
71 | ||
72 | static int nested = 0; | |
73 | module_param(nested, int, S_IRUGO); | |
74 | ||
75 | static void kvm_reput_irq(struct vcpu_svm *svm); | |
76 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); | |
77 | ||
78 | static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override); | |
79 | static int nested_svm_vmexit(struct vcpu_svm *svm); | |
80 | static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb, | |
81 | void *arg2, void *opaque); | |
82 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, | |
83 | bool has_error_code, u32 error_code); | |
84 | ||
85 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) | |
86 | { | |
87 | return container_of(vcpu, struct vcpu_svm, vcpu); | |
88 | } | |
89 | ||
90 | static inline bool is_nested(struct vcpu_svm *svm) | |
91 | { | |
92 | return svm->nested_vmcb; | |
93 | } | |
94 | ||
95 | static unsigned long iopm_base; | |
96 | ||
97 | struct kvm_ldttss_desc { | |
98 | u16 limit0; | |
99 | u16 base0; | |
100 | unsigned base1 : 8, type : 5, dpl : 2, p : 1; | |
101 | unsigned limit1 : 4, zero0 : 3, g : 1, base2 : 8; | |
102 | u32 base3; | |
103 | u32 zero1; | |
104 | } __attribute__((packed)); | |
105 | ||
106 | struct svm_cpu_data { | |
107 | int cpu; | |
108 | ||
109 | u64 asid_generation; | |
110 | u32 max_asid; | |
111 | u32 next_asid; | |
112 | struct kvm_ldttss_desc *tss_desc; | |
113 | ||
114 | struct page *save_area; | |
115 | }; | |
116 | ||
117 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); | |
118 | static uint32_t svm_features; | |
119 | ||
120 | struct svm_init_data { | |
121 | int cpu; | |
122 | int r; | |
123 | }; | |
124 | ||
125 | static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; | |
126 | ||
127 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) | |
128 | #define MSRS_RANGE_SIZE 2048 | |
129 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) | |
130 | ||
131 | #define MAX_INST_SIZE 15 | |
132 | ||
133 | static inline u32 svm_has(u32 feat) | |
134 | { | |
135 | return svm_features & feat; | |
136 | } | |
137 | ||
138 | static inline u8 pop_irq(struct kvm_vcpu *vcpu) | |
139 | { | |
140 | int word_index = __ffs(vcpu->arch.irq_summary); | |
141 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); | |
142 | int irq = word_index * BITS_PER_LONG + bit_index; | |
143 | ||
144 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); | |
145 | if (!vcpu->arch.irq_pending[word_index]) | |
146 | clear_bit(word_index, &vcpu->arch.irq_summary); | |
147 | return irq; | |
148 | } | |
149 | ||
150 | static inline void push_irq(struct kvm_vcpu *vcpu, u8 irq) | |
151 | { | |
152 | set_bit(irq, vcpu->arch.irq_pending); | |
153 | set_bit(irq / BITS_PER_LONG, &vcpu->arch.irq_summary); | |
154 | } | |
155 | ||
156 | static inline void clgi(void) | |
157 | { | |
158 | asm volatile (__ex(SVM_CLGI)); | |
159 | } | |
160 | ||
161 | static inline void stgi(void) | |
162 | { | |
163 | asm volatile (__ex(SVM_STGI)); | |
164 | } | |
165 | ||
166 | static inline void invlpga(unsigned long addr, u32 asid) | |
167 | { | |
168 | asm volatile (__ex(SVM_INVLPGA) :: "a"(addr), "c"(asid)); | |
169 | } | |
170 | ||
171 | static inline unsigned long kvm_read_cr2(void) | |
172 | { | |
173 | unsigned long cr2; | |
174 | ||
175 | asm volatile ("mov %%cr2, %0" : "=r" (cr2)); | |
176 | return cr2; | |
177 | } | |
178 | ||
179 | static inline void kvm_write_cr2(unsigned long val) | |
180 | { | |
181 | asm volatile ("mov %0, %%cr2" :: "r" (val)); | |
182 | } | |
183 | ||
184 | static inline unsigned long read_dr6(void) | |
185 | { | |
186 | unsigned long dr6; | |
187 | ||
188 | asm volatile ("mov %%dr6, %0" : "=r" (dr6)); | |
189 | return dr6; | |
190 | } | |
191 | ||
192 | static inline void write_dr6(unsigned long val) | |
193 | { | |
194 | asm volatile ("mov %0, %%dr6" :: "r" (val)); | |
195 | } | |
196 | ||
197 | static inline unsigned long read_dr7(void) | |
198 | { | |
199 | unsigned long dr7; | |
200 | ||
201 | asm volatile ("mov %%dr7, %0" : "=r" (dr7)); | |
202 | return dr7; | |
203 | } | |
204 | ||
205 | static inline void write_dr7(unsigned long val) | |
206 | { | |
207 | asm volatile ("mov %0, %%dr7" :: "r" (val)); | |
208 | } | |
209 | ||
210 | static inline void force_new_asid(struct kvm_vcpu *vcpu) | |
211 | { | |
212 | to_svm(vcpu)->asid_generation--; | |
213 | } | |
214 | ||
215 | static inline void flush_guest_tlb(struct kvm_vcpu *vcpu) | |
216 | { | |
217 | force_new_asid(vcpu); | |
218 | } | |
219 | ||
220 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) | |
221 | { | |
222 | if (!npt_enabled && !(efer & EFER_LMA)) | |
223 | efer &= ~EFER_LME; | |
224 | ||
225 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; | |
226 | vcpu->arch.shadow_efer = efer; | |
227 | } | |
228 | ||
229 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, | |
230 | bool has_error_code, u32 error_code) | |
231 | { | |
232 | struct vcpu_svm *svm = to_svm(vcpu); | |
233 | ||
234 | /* If we are within a nested VM we'd better #VMEXIT and let the | |
235 | guest handle the exception */ | |
236 | if (nested_svm_check_exception(svm, nr, has_error_code, error_code)) | |
237 | return; | |
238 | ||
239 | svm->vmcb->control.event_inj = nr | |
240 | | SVM_EVTINJ_VALID | |
241 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) | |
242 | | SVM_EVTINJ_TYPE_EXEPT; | |
243 | svm->vmcb->control.event_inj_err = error_code; | |
244 | } | |
245 | ||
246 | static bool svm_exception_injected(struct kvm_vcpu *vcpu) | |
247 | { | |
248 | struct vcpu_svm *svm = to_svm(vcpu); | |
249 | ||
250 | return !(svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID); | |
251 | } | |
252 | ||
253 | static int is_external_interrupt(u32 info) | |
254 | { | |
255 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; | |
256 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); | |
257 | } | |
258 | ||
259 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) | |
260 | { | |
261 | struct vcpu_svm *svm = to_svm(vcpu); | |
262 | ||
263 | if (!svm->next_rip) { | |
264 | printk(KERN_DEBUG "%s: NOP\n", __func__); | |
265 | return; | |
266 | } | |
267 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) | |
268 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", | |
269 | __func__, kvm_rip_read(vcpu), svm->next_rip); | |
270 | ||
271 | kvm_rip_write(vcpu, svm->next_rip); | |
272 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; | |
273 | ||
274 | vcpu->arch.interrupt_window_open = (svm->vcpu.arch.hflags & HF_GIF_MASK); | |
275 | } | |
276 | ||
277 | static int has_svm(void) | |
278 | { | |
279 | const char *msg; | |
280 | ||
281 | if (!cpu_has_svm(&msg)) { | |
282 | printk(KERN_INFO "has_svn: %s\n", msg); | |
283 | return 0; | |
284 | } | |
285 | ||
286 | return 1; | |
287 | } | |
288 | ||
289 | static void svm_hardware_disable(void *garbage) | |
290 | { | |
291 | cpu_svm_disable(); | |
292 | } | |
293 | ||
294 | static void svm_hardware_enable(void *garbage) | |
295 | { | |
296 | ||
297 | struct svm_cpu_data *svm_data; | |
298 | uint64_t efer; | |
299 | struct desc_ptr gdt_descr; | |
300 | struct desc_struct *gdt; | |
301 | int me = raw_smp_processor_id(); | |
302 | ||
303 | if (!has_svm()) { | |
304 | printk(KERN_ERR "svm_cpu_init: err EOPNOTSUPP on %d\n", me); | |
305 | return; | |
306 | } | |
307 | svm_data = per_cpu(svm_data, me); | |
308 | ||
309 | if (!svm_data) { | |
310 | printk(KERN_ERR "svm_cpu_init: svm_data is NULL on %d\n", | |
311 | me); | |
312 | return; | |
313 | } | |
314 | ||
315 | svm_data->asid_generation = 1; | |
316 | svm_data->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; | |
317 | svm_data->next_asid = svm_data->max_asid + 1; | |
318 | ||
319 | asm volatile ("sgdt %0" : "=m"(gdt_descr)); | |
320 | gdt = (struct desc_struct *)gdt_descr.address; | |
321 | svm_data->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); | |
322 | ||
323 | rdmsrl(MSR_EFER, efer); | |
324 | wrmsrl(MSR_EFER, efer | EFER_SVME); | |
325 | ||
326 | wrmsrl(MSR_VM_HSAVE_PA, | |
327 | page_to_pfn(svm_data->save_area) << PAGE_SHIFT); | |
328 | } | |
329 | ||
330 | static void svm_cpu_uninit(int cpu) | |
331 | { | |
332 | struct svm_cpu_data *svm_data | |
333 | = per_cpu(svm_data, raw_smp_processor_id()); | |
334 | ||
335 | if (!svm_data) | |
336 | return; | |
337 | ||
338 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; | |
339 | __free_page(svm_data->save_area); | |
340 | kfree(svm_data); | |
341 | } | |
342 | ||
343 | static int svm_cpu_init(int cpu) | |
344 | { | |
345 | struct svm_cpu_data *svm_data; | |
346 | int r; | |
347 | ||
348 | svm_data = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); | |
349 | if (!svm_data) | |
350 | return -ENOMEM; | |
351 | svm_data->cpu = cpu; | |
352 | svm_data->save_area = alloc_page(GFP_KERNEL); | |
353 | r = -ENOMEM; | |
354 | if (!svm_data->save_area) | |
355 | goto err_1; | |
356 | ||
357 | per_cpu(svm_data, cpu) = svm_data; | |
358 | ||
359 | return 0; | |
360 | ||
361 | err_1: | |
362 | kfree(svm_data); | |
363 | return r; | |
364 | ||
365 | } | |
366 | ||
367 | static void set_msr_interception(u32 *msrpm, unsigned msr, | |
368 | int read, int write) | |
369 | { | |
370 | int i; | |
371 | ||
372 | for (i = 0; i < NUM_MSR_MAPS; i++) { | |
373 | if (msr >= msrpm_ranges[i] && | |
374 | msr < msrpm_ranges[i] + MSRS_IN_RANGE) { | |
375 | u32 msr_offset = (i * MSRS_IN_RANGE + msr - | |
376 | msrpm_ranges[i]) * 2; | |
377 | ||
378 | u32 *base = msrpm + (msr_offset / 32); | |
379 | u32 msr_shift = msr_offset % 32; | |
380 | u32 mask = ((write) ? 0 : 2) | ((read) ? 0 : 1); | |
381 | *base = (*base & ~(0x3 << msr_shift)) | | |
382 | (mask << msr_shift); | |
383 | return; | |
384 | } | |
385 | } | |
386 | BUG(); | |
387 | } | |
388 | ||
389 | static void svm_vcpu_init_msrpm(u32 *msrpm) | |
390 | { | |
391 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); | |
392 | ||
393 | #ifdef CONFIG_X86_64 | |
394 | set_msr_interception(msrpm, MSR_GS_BASE, 1, 1); | |
395 | set_msr_interception(msrpm, MSR_FS_BASE, 1, 1); | |
396 | set_msr_interception(msrpm, MSR_KERNEL_GS_BASE, 1, 1); | |
397 | set_msr_interception(msrpm, MSR_LSTAR, 1, 1); | |
398 | set_msr_interception(msrpm, MSR_CSTAR, 1, 1); | |
399 | set_msr_interception(msrpm, MSR_SYSCALL_MASK, 1, 1); | |
400 | #endif | |
401 | set_msr_interception(msrpm, MSR_K6_STAR, 1, 1); | |
402 | set_msr_interception(msrpm, MSR_IA32_SYSENTER_CS, 1, 1); | |
403 | set_msr_interception(msrpm, MSR_IA32_SYSENTER_ESP, 1, 1); | |
404 | set_msr_interception(msrpm, MSR_IA32_SYSENTER_EIP, 1, 1); | |
405 | } | |
406 | ||
407 | static void svm_enable_lbrv(struct vcpu_svm *svm) | |
408 | { | |
409 | u32 *msrpm = svm->msrpm; | |
410 | ||
411 | svm->vmcb->control.lbr_ctl = 1; | |
412 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); | |
413 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); | |
414 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); | |
415 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); | |
416 | } | |
417 | ||
418 | static void svm_disable_lbrv(struct vcpu_svm *svm) | |
419 | { | |
420 | u32 *msrpm = svm->msrpm; | |
421 | ||
422 | svm->vmcb->control.lbr_ctl = 0; | |
423 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); | |
424 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); | |
425 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); | |
426 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); | |
427 | } | |
428 | ||
429 | static __init int svm_hardware_setup(void) | |
430 | { | |
431 | int cpu; | |
432 | struct page *iopm_pages; | |
433 | void *iopm_va; | |
434 | int r; | |
435 | ||
436 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); | |
437 | ||
438 | if (!iopm_pages) | |
439 | return -ENOMEM; | |
440 | ||
441 | iopm_va = page_address(iopm_pages); | |
442 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); | |
443 | clear_bit(0x80, iopm_va); /* allow direct access to PC debug port */ | |
444 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; | |
445 | ||
446 | if (boot_cpu_has(X86_FEATURE_NX)) | |
447 | kvm_enable_efer_bits(EFER_NX); | |
448 | ||
449 | if (nested) { | |
450 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); | |
451 | kvm_enable_efer_bits(EFER_SVME); | |
452 | } | |
453 | ||
454 | for_each_online_cpu(cpu) { | |
455 | r = svm_cpu_init(cpu); | |
456 | if (r) | |
457 | goto err; | |
458 | } | |
459 | ||
460 | svm_features = cpuid_edx(SVM_CPUID_FUNC); | |
461 | ||
462 | if (!svm_has(SVM_FEATURE_NPT)) | |
463 | npt_enabled = false; | |
464 | ||
465 | if (npt_enabled && !npt) { | |
466 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); | |
467 | npt_enabled = false; | |
468 | } | |
469 | ||
470 | if (npt_enabled) { | |
471 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); | |
472 | kvm_enable_tdp(); | |
473 | } else | |
474 | kvm_disable_tdp(); | |
475 | ||
476 | return 0; | |
477 | ||
478 | err: | |
479 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); | |
480 | iopm_base = 0; | |
481 | return r; | |
482 | } | |
483 | ||
484 | static __exit void svm_hardware_unsetup(void) | |
485 | { | |
486 | int cpu; | |
487 | ||
488 | for_each_online_cpu(cpu) | |
489 | svm_cpu_uninit(cpu); | |
490 | ||
491 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); | |
492 | iopm_base = 0; | |
493 | } | |
494 | ||
495 | static void init_seg(struct vmcb_seg *seg) | |
496 | { | |
497 | seg->selector = 0; | |
498 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | | |
499 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ | |
500 | seg->limit = 0xffff; | |
501 | seg->base = 0; | |
502 | } | |
503 | ||
504 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) | |
505 | { | |
506 | seg->selector = 0; | |
507 | seg->attrib = SVM_SELECTOR_P_MASK | type; | |
508 | seg->limit = 0xffff; | |
509 | seg->base = 0; | |
510 | } | |
511 | ||
512 | static void init_vmcb(struct vcpu_svm *svm) | |
513 | { | |
514 | struct vmcb_control_area *control = &svm->vmcb->control; | |
515 | struct vmcb_save_area *save = &svm->vmcb->save; | |
516 | ||
517 | control->intercept_cr_read = INTERCEPT_CR0_MASK | | |
518 | INTERCEPT_CR3_MASK | | |
519 | INTERCEPT_CR4_MASK; | |
520 | ||
521 | control->intercept_cr_write = INTERCEPT_CR0_MASK | | |
522 | INTERCEPT_CR3_MASK | | |
523 | INTERCEPT_CR4_MASK | | |
524 | INTERCEPT_CR8_MASK; | |
525 | ||
526 | control->intercept_dr_read = INTERCEPT_DR0_MASK | | |
527 | INTERCEPT_DR1_MASK | | |
528 | INTERCEPT_DR2_MASK | | |
529 | INTERCEPT_DR3_MASK; | |
530 | ||
531 | control->intercept_dr_write = INTERCEPT_DR0_MASK | | |
532 | INTERCEPT_DR1_MASK | | |
533 | INTERCEPT_DR2_MASK | | |
534 | INTERCEPT_DR3_MASK | | |
535 | INTERCEPT_DR5_MASK | | |
536 | INTERCEPT_DR7_MASK; | |
537 | ||
538 | control->intercept_exceptions = (1 << PF_VECTOR) | | |
539 | (1 << UD_VECTOR) | | |
540 | (1 << MC_VECTOR); | |
541 | ||
542 | ||
543 | control->intercept = (1ULL << INTERCEPT_INTR) | | |
544 | (1ULL << INTERCEPT_NMI) | | |
545 | (1ULL << INTERCEPT_SMI) | | |
546 | (1ULL << INTERCEPT_CPUID) | | |
547 | (1ULL << INTERCEPT_INVD) | | |
548 | (1ULL << INTERCEPT_HLT) | | |
549 | (1ULL << INTERCEPT_INVLPG) | | |
550 | (1ULL << INTERCEPT_INVLPGA) | | |
551 | (1ULL << INTERCEPT_IOIO_PROT) | | |
552 | (1ULL << INTERCEPT_MSR_PROT) | | |
553 | (1ULL << INTERCEPT_TASK_SWITCH) | | |
554 | (1ULL << INTERCEPT_SHUTDOWN) | | |
555 | (1ULL << INTERCEPT_VMRUN) | | |
556 | (1ULL << INTERCEPT_VMMCALL) | | |
557 | (1ULL << INTERCEPT_VMLOAD) | | |
558 | (1ULL << INTERCEPT_VMSAVE) | | |
559 | (1ULL << INTERCEPT_STGI) | | |
560 | (1ULL << INTERCEPT_CLGI) | | |
561 | (1ULL << INTERCEPT_SKINIT) | | |
562 | (1ULL << INTERCEPT_WBINVD) | | |
563 | (1ULL << INTERCEPT_MONITOR) | | |
564 | (1ULL << INTERCEPT_MWAIT); | |
565 | ||
566 | control->iopm_base_pa = iopm_base; | |
567 | control->msrpm_base_pa = __pa(svm->msrpm); | |
568 | control->tsc_offset = 0; | |
569 | control->int_ctl = V_INTR_MASKING_MASK; | |
570 | ||
571 | init_seg(&save->es); | |
572 | init_seg(&save->ss); | |
573 | init_seg(&save->ds); | |
574 | init_seg(&save->fs); | |
575 | init_seg(&save->gs); | |
576 | ||
577 | save->cs.selector = 0xf000; | |
578 | /* Executable/Readable Code Segment */ | |
579 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | | |
580 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; | |
581 | save->cs.limit = 0xffff; | |
582 | /* | |
583 | * cs.base should really be 0xffff0000, but vmx can't handle that, so | |
584 | * be consistent with it. | |
585 | * | |
586 | * Replace when we have real mode working for vmx. | |
587 | */ | |
588 | save->cs.base = 0xf0000; | |
589 | ||
590 | save->gdtr.limit = 0xffff; | |
591 | save->idtr.limit = 0xffff; | |
592 | ||
593 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); | |
594 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); | |
595 | ||
596 | save->efer = EFER_SVME; | |
597 | save->dr6 = 0xffff0ff0; | |
598 | save->dr7 = 0x400; | |
599 | save->rflags = 2; | |
600 | save->rip = 0x0000fff0; | |
601 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; | |
602 | ||
603 | /* | |
604 | * cr0 val on cpu init should be 0x60000010, we enable cpu | |
605 | * cache by default. the orderly way is to enable cache in bios. | |
606 | */ | |
607 | save->cr0 = 0x00000010 | X86_CR0_PG | X86_CR0_WP; | |
608 | save->cr4 = X86_CR4_PAE; | |
609 | /* rdx = ?? */ | |
610 | ||
611 | if (npt_enabled) { | |
612 | /* Setup VMCB for Nested Paging */ | |
613 | control->nested_ctl = 1; | |
614 | control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) | | |
615 | (1ULL << INTERCEPT_INVLPG)); | |
616 | control->intercept_exceptions &= ~(1 << PF_VECTOR); | |
617 | control->intercept_cr_read &= ~(INTERCEPT_CR0_MASK| | |
618 | INTERCEPT_CR3_MASK); | |
619 | control->intercept_cr_write &= ~(INTERCEPT_CR0_MASK| | |
620 | INTERCEPT_CR3_MASK); | |
621 | save->g_pat = 0x0007040600070406ULL; | |
622 | /* enable caching because the QEMU Bios doesn't enable it */ | |
623 | save->cr0 = X86_CR0_ET; | |
624 | save->cr3 = 0; | |
625 | save->cr4 = 0; | |
626 | } | |
627 | force_new_asid(&svm->vcpu); | |
628 | ||
629 | svm->nested_vmcb = 0; | |
630 | svm->vcpu.arch.hflags = HF_GIF_MASK; | |
631 | } | |
632 | ||
633 | static int svm_vcpu_reset(struct kvm_vcpu *vcpu) | |
634 | { | |
635 | struct vcpu_svm *svm = to_svm(vcpu); | |
636 | ||
637 | init_vmcb(svm); | |
638 | ||
639 | if (vcpu->vcpu_id != 0) { | |
640 | kvm_rip_write(vcpu, 0); | |
641 | svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; | |
642 | svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; | |
643 | } | |
644 | vcpu->arch.regs_avail = ~0; | |
645 | vcpu->arch.regs_dirty = ~0; | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) | |
651 | { | |
652 | struct vcpu_svm *svm; | |
653 | struct page *page; | |
654 | struct page *msrpm_pages; | |
655 | struct page *hsave_page; | |
656 | struct page *nested_msrpm_pages; | |
657 | int err; | |
658 | ||
659 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); | |
660 | if (!svm) { | |
661 | err = -ENOMEM; | |
662 | goto out; | |
663 | } | |
664 | ||
665 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); | |
666 | if (err) | |
667 | goto free_svm; | |
668 | ||
669 | page = alloc_page(GFP_KERNEL); | |
670 | if (!page) { | |
671 | err = -ENOMEM; | |
672 | goto uninit; | |
673 | } | |
674 | ||
675 | err = -ENOMEM; | |
676 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); | |
677 | if (!msrpm_pages) | |
678 | goto uninit; | |
679 | ||
680 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); | |
681 | if (!nested_msrpm_pages) | |
682 | goto uninit; | |
683 | ||
684 | svm->msrpm = page_address(msrpm_pages); | |
685 | svm_vcpu_init_msrpm(svm->msrpm); | |
686 | ||
687 | hsave_page = alloc_page(GFP_KERNEL); | |
688 | if (!hsave_page) | |
689 | goto uninit; | |
690 | svm->hsave = page_address(hsave_page); | |
691 | ||
692 | svm->nested_msrpm = page_address(nested_msrpm_pages); | |
693 | ||
694 | svm->vmcb = page_address(page); | |
695 | clear_page(svm->vmcb); | |
696 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; | |
697 | svm->asid_generation = 0; | |
698 | memset(svm->db_regs, 0, sizeof(svm->db_regs)); | |
699 | init_vmcb(svm); | |
700 | ||
701 | fx_init(&svm->vcpu); | |
702 | svm->vcpu.fpu_active = 1; | |
703 | svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; | |
704 | if (svm->vcpu.vcpu_id == 0) | |
705 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; | |
706 | ||
707 | return &svm->vcpu; | |
708 | ||
709 | uninit: | |
710 | kvm_vcpu_uninit(&svm->vcpu); | |
711 | free_svm: | |
712 | kmem_cache_free(kvm_vcpu_cache, svm); | |
713 | out: | |
714 | return ERR_PTR(err); | |
715 | } | |
716 | ||
717 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) | |
718 | { | |
719 | struct vcpu_svm *svm = to_svm(vcpu); | |
720 | ||
721 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); | |
722 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); | |
723 | __free_page(virt_to_page(svm->hsave)); | |
724 | __free_pages(virt_to_page(svm->nested_msrpm), MSRPM_ALLOC_ORDER); | |
725 | kvm_vcpu_uninit(vcpu); | |
726 | kmem_cache_free(kvm_vcpu_cache, svm); | |
727 | } | |
728 | ||
729 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) | |
730 | { | |
731 | struct vcpu_svm *svm = to_svm(vcpu); | |
732 | int i; | |
733 | ||
734 | if (unlikely(cpu != vcpu->cpu)) { | |
735 | u64 tsc_this, delta; | |
736 | ||
737 | /* | |
738 | * Make sure that the guest sees a monotonically | |
739 | * increasing TSC. | |
740 | */ | |
741 | rdtscll(tsc_this); | |
742 | delta = vcpu->arch.host_tsc - tsc_this; | |
743 | svm->vmcb->control.tsc_offset += delta; | |
744 | vcpu->cpu = cpu; | |
745 | kvm_migrate_timers(vcpu); | |
746 | } | |
747 | ||
748 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) | |
749 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); | |
750 | } | |
751 | ||
752 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) | |
753 | { | |
754 | struct vcpu_svm *svm = to_svm(vcpu); | |
755 | int i; | |
756 | ||
757 | ++vcpu->stat.host_state_reload; | |
758 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) | |
759 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); | |
760 | ||
761 | rdtscll(vcpu->arch.host_tsc); | |
762 | } | |
763 | ||
764 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) | |
765 | { | |
766 | return to_svm(vcpu)->vmcb->save.rflags; | |
767 | } | |
768 | ||
769 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
770 | { | |
771 | to_svm(vcpu)->vmcb->save.rflags = rflags; | |
772 | } | |
773 | ||
774 | static void svm_set_vintr(struct vcpu_svm *svm) | |
775 | { | |
776 | svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR; | |
777 | } | |
778 | ||
779 | static void svm_clear_vintr(struct vcpu_svm *svm) | |
780 | { | |
781 | svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR); | |
782 | } | |
783 | ||
784 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) | |
785 | { | |
786 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; | |
787 | ||
788 | switch (seg) { | |
789 | case VCPU_SREG_CS: return &save->cs; | |
790 | case VCPU_SREG_DS: return &save->ds; | |
791 | case VCPU_SREG_ES: return &save->es; | |
792 | case VCPU_SREG_FS: return &save->fs; | |
793 | case VCPU_SREG_GS: return &save->gs; | |
794 | case VCPU_SREG_SS: return &save->ss; | |
795 | case VCPU_SREG_TR: return &save->tr; | |
796 | case VCPU_SREG_LDTR: return &save->ldtr; | |
797 | } | |
798 | BUG(); | |
799 | return NULL; | |
800 | } | |
801 | ||
802 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
803 | { | |
804 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
805 | ||
806 | return s->base; | |
807 | } | |
808 | ||
809 | static void svm_get_segment(struct kvm_vcpu *vcpu, | |
810 | struct kvm_segment *var, int seg) | |
811 | { | |
812 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
813 | ||
814 | var->base = s->base; | |
815 | var->limit = s->limit; | |
816 | var->selector = s->selector; | |
817 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; | |
818 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; | |
819 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; | |
820 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; | |
821 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; | |
822 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; | |
823 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; | |
824 | var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1; | |
825 | ||
826 | /* | |
827 | * SVM always stores 0 for the 'G' bit in the CS selector in | |
828 | * the VMCB on a VMEXIT. This hurts cross-vendor migration: | |
829 | * Intel's VMENTRY has a check on the 'G' bit. | |
830 | */ | |
831 | if (seg == VCPU_SREG_CS) | |
832 | var->g = s->limit > 0xfffff; | |
833 | ||
834 | /* | |
835 | * Work around a bug where the busy flag in the tr selector | |
836 | * isn't exposed | |
837 | */ | |
838 | if (seg == VCPU_SREG_TR) | |
839 | var->type |= 0x2; | |
840 | ||
841 | var->unusable = !var->present; | |
842 | } | |
843 | ||
844 | static int svm_get_cpl(struct kvm_vcpu *vcpu) | |
845 | { | |
846 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; | |
847 | ||
848 | return save->cpl; | |
849 | } | |
850 | ||
851 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
852 | { | |
853 | struct vcpu_svm *svm = to_svm(vcpu); | |
854 | ||
855 | dt->limit = svm->vmcb->save.idtr.limit; | |
856 | dt->base = svm->vmcb->save.idtr.base; | |
857 | } | |
858 | ||
859 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
860 | { | |
861 | struct vcpu_svm *svm = to_svm(vcpu); | |
862 | ||
863 | svm->vmcb->save.idtr.limit = dt->limit; | |
864 | svm->vmcb->save.idtr.base = dt->base ; | |
865 | } | |
866 | ||
867 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
868 | { | |
869 | struct vcpu_svm *svm = to_svm(vcpu); | |
870 | ||
871 | dt->limit = svm->vmcb->save.gdtr.limit; | |
872 | dt->base = svm->vmcb->save.gdtr.base; | |
873 | } | |
874 | ||
875 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct descriptor_table *dt) | |
876 | { | |
877 | struct vcpu_svm *svm = to_svm(vcpu); | |
878 | ||
879 | svm->vmcb->save.gdtr.limit = dt->limit; | |
880 | svm->vmcb->save.gdtr.base = dt->base ; | |
881 | } | |
882 | ||
883 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) | |
884 | { | |
885 | } | |
886 | ||
887 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) | |
888 | { | |
889 | struct vcpu_svm *svm = to_svm(vcpu); | |
890 | ||
891 | #ifdef CONFIG_X86_64 | |
892 | if (vcpu->arch.shadow_efer & EFER_LME) { | |
893 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { | |
894 | vcpu->arch.shadow_efer |= EFER_LMA; | |
895 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; | |
896 | } | |
897 | ||
898 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { | |
899 | vcpu->arch.shadow_efer &= ~EFER_LMA; | |
900 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); | |
901 | } | |
902 | } | |
903 | #endif | |
904 | if (npt_enabled) | |
905 | goto set; | |
906 | ||
907 | if ((vcpu->arch.cr0 & X86_CR0_TS) && !(cr0 & X86_CR0_TS)) { | |
908 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); | |
909 | vcpu->fpu_active = 1; | |
910 | } | |
911 | ||
912 | vcpu->arch.cr0 = cr0; | |
913 | cr0 |= X86_CR0_PG | X86_CR0_WP; | |
914 | if (!vcpu->fpu_active) { | |
915 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); | |
916 | cr0 |= X86_CR0_TS; | |
917 | } | |
918 | set: | |
919 | /* | |
920 | * re-enable caching here because the QEMU bios | |
921 | * does not do it - this results in some delay at | |
922 | * reboot | |
923 | */ | |
924 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); | |
925 | svm->vmcb->save.cr0 = cr0; | |
926 | } | |
927 | ||
928 | static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) | |
929 | { | |
930 | unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE; | |
931 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; | |
932 | ||
933 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) | |
934 | force_new_asid(vcpu); | |
935 | ||
936 | vcpu->arch.cr4 = cr4; | |
937 | if (!npt_enabled) | |
938 | cr4 |= X86_CR4_PAE; | |
939 | cr4 |= host_cr4_mce; | |
940 | to_svm(vcpu)->vmcb->save.cr4 = cr4; | |
941 | } | |
942 | ||
943 | static void svm_set_segment(struct kvm_vcpu *vcpu, | |
944 | struct kvm_segment *var, int seg) | |
945 | { | |
946 | struct vcpu_svm *svm = to_svm(vcpu); | |
947 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
948 | ||
949 | s->base = var->base; | |
950 | s->limit = var->limit; | |
951 | s->selector = var->selector; | |
952 | if (var->unusable) | |
953 | s->attrib = 0; | |
954 | else { | |
955 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); | |
956 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; | |
957 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; | |
958 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; | |
959 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; | |
960 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; | |
961 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; | |
962 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; | |
963 | } | |
964 | if (seg == VCPU_SREG_CS) | |
965 | svm->vmcb->save.cpl | |
966 | = (svm->vmcb->save.cs.attrib | |
967 | >> SVM_SELECTOR_DPL_SHIFT) & 3; | |
968 | ||
969 | } | |
970 | ||
971 | static int svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_debug_guest *dbg) | |
972 | { | |
973 | return -EOPNOTSUPP; | |
974 | } | |
975 | ||
976 | static int svm_get_irq(struct kvm_vcpu *vcpu) | |
977 | { | |
978 | struct vcpu_svm *svm = to_svm(vcpu); | |
979 | u32 exit_int_info = svm->vmcb->control.exit_int_info; | |
980 | ||
981 | if (is_external_interrupt(exit_int_info)) | |
982 | return exit_int_info & SVM_EVTINJ_VEC_MASK; | |
983 | return -1; | |
984 | } | |
985 | ||
986 | static void load_host_msrs(struct kvm_vcpu *vcpu) | |
987 | { | |
988 | #ifdef CONFIG_X86_64 | |
989 | wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); | |
990 | #endif | |
991 | } | |
992 | ||
993 | static void save_host_msrs(struct kvm_vcpu *vcpu) | |
994 | { | |
995 | #ifdef CONFIG_X86_64 | |
996 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base); | |
997 | #endif | |
998 | } | |
999 | ||
1000 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *svm_data) | |
1001 | { | |
1002 | if (svm_data->next_asid > svm_data->max_asid) { | |
1003 | ++svm_data->asid_generation; | |
1004 | svm_data->next_asid = 1; | |
1005 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; | |
1006 | } | |
1007 | ||
1008 | svm->vcpu.cpu = svm_data->cpu; | |
1009 | svm->asid_generation = svm_data->asid_generation; | |
1010 | svm->vmcb->control.asid = svm_data->next_asid++; | |
1011 | } | |
1012 | ||
1013 | static unsigned long svm_get_dr(struct kvm_vcpu *vcpu, int dr) | |
1014 | { | |
1015 | unsigned long val = to_svm(vcpu)->db_regs[dr]; | |
1016 | KVMTRACE_2D(DR_READ, vcpu, (u32)dr, (u32)val, handler); | |
1017 | return val; | |
1018 | } | |
1019 | ||
1020 | static void svm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long value, | |
1021 | int *exception) | |
1022 | { | |
1023 | struct vcpu_svm *svm = to_svm(vcpu); | |
1024 | ||
1025 | *exception = 0; | |
1026 | ||
1027 | if (svm->vmcb->save.dr7 & DR7_GD_MASK) { | |
1028 | svm->vmcb->save.dr7 &= ~DR7_GD_MASK; | |
1029 | svm->vmcb->save.dr6 |= DR6_BD_MASK; | |
1030 | *exception = DB_VECTOR; | |
1031 | return; | |
1032 | } | |
1033 | ||
1034 | switch (dr) { | |
1035 | case 0 ... 3: | |
1036 | svm->db_regs[dr] = value; | |
1037 | return; | |
1038 | case 4 ... 5: | |
1039 | if (vcpu->arch.cr4 & X86_CR4_DE) { | |
1040 | *exception = UD_VECTOR; | |
1041 | return; | |
1042 | } | |
1043 | case 7: { | |
1044 | if (value & ~((1ULL << 32) - 1)) { | |
1045 | *exception = GP_VECTOR; | |
1046 | return; | |
1047 | } | |
1048 | svm->vmcb->save.dr7 = value; | |
1049 | return; | |
1050 | } | |
1051 | default: | |
1052 | printk(KERN_DEBUG "%s: unexpected dr %u\n", | |
1053 | __func__, dr); | |
1054 | *exception = UD_VECTOR; | |
1055 | return; | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | static int pf_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1060 | { | |
1061 | u32 exit_int_info = svm->vmcb->control.exit_int_info; | |
1062 | struct kvm *kvm = svm->vcpu.kvm; | |
1063 | u64 fault_address; | |
1064 | u32 error_code; | |
1065 | bool event_injection = false; | |
1066 | ||
1067 | if (!irqchip_in_kernel(kvm) && | |
1068 | is_external_interrupt(exit_int_info)) { | |
1069 | event_injection = true; | |
1070 | push_irq(&svm->vcpu, exit_int_info & SVM_EVTINJ_VEC_MASK); | |
1071 | } | |
1072 | ||
1073 | fault_address = svm->vmcb->control.exit_info_2; | |
1074 | error_code = svm->vmcb->control.exit_info_1; | |
1075 | ||
1076 | if (!npt_enabled) | |
1077 | KVMTRACE_3D(PAGE_FAULT, &svm->vcpu, error_code, | |
1078 | (u32)fault_address, (u32)(fault_address >> 32), | |
1079 | handler); | |
1080 | else | |
1081 | KVMTRACE_3D(TDP_FAULT, &svm->vcpu, error_code, | |
1082 | (u32)fault_address, (u32)(fault_address >> 32), | |
1083 | handler); | |
1084 | /* | |
1085 | * FIXME: Tis shouldn't be necessary here, but there is a flush | |
1086 | * missing in the MMU code. Until we find this bug, flush the | |
1087 | * complete TLB here on an NPF | |
1088 | */ | |
1089 | if (npt_enabled) | |
1090 | svm_flush_tlb(&svm->vcpu); | |
1091 | ||
1092 | if (!npt_enabled && event_injection) | |
1093 | kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); | |
1094 | return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code); | |
1095 | } | |
1096 | ||
1097 | static int ud_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1098 | { | |
1099 | int er; | |
1100 | ||
1101 | er = emulate_instruction(&svm->vcpu, kvm_run, 0, 0, EMULTYPE_TRAP_UD); | |
1102 | if (er != EMULATE_DONE) | |
1103 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
1104 | return 1; | |
1105 | } | |
1106 | ||
1107 | static int nm_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1108 | { | |
1109 | svm->vmcb->control.intercept_exceptions &= ~(1 << NM_VECTOR); | |
1110 | if (!(svm->vcpu.arch.cr0 & X86_CR0_TS)) | |
1111 | svm->vmcb->save.cr0 &= ~X86_CR0_TS; | |
1112 | svm->vcpu.fpu_active = 1; | |
1113 | ||
1114 | return 1; | |
1115 | } | |
1116 | ||
1117 | static int mc_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1118 | { | |
1119 | /* | |
1120 | * On an #MC intercept the MCE handler is not called automatically in | |
1121 | * the host. So do it by hand here. | |
1122 | */ | |
1123 | asm volatile ( | |
1124 | "int $0x12\n"); | |
1125 | /* not sure if we ever come back to this point */ | |
1126 | ||
1127 | return 1; | |
1128 | } | |
1129 | ||
1130 | static int shutdown_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1131 | { | |
1132 | /* | |
1133 | * VMCB is undefined after a SHUTDOWN intercept | |
1134 | * so reinitialize it. | |
1135 | */ | |
1136 | clear_page(svm->vmcb); | |
1137 | init_vmcb(svm); | |
1138 | ||
1139 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; | |
1140 | return 0; | |
1141 | } | |
1142 | ||
1143 | static int io_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1144 | { | |
1145 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ | |
1146 | int size, down, in, string, rep; | |
1147 | unsigned port; | |
1148 | ||
1149 | ++svm->vcpu.stat.io_exits; | |
1150 | ||
1151 | svm->next_rip = svm->vmcb->control.exit_info_2; | |
1152 | ||
1153 | string = (io_info & SVM_IOIO_STR_MASK) != 0; | |
1154 | ||
1155 | if (string) { | |
1156 | if (emulate_instruction(&svm->vcpu, | |
1157 | kvm_run, 0, 0, 0) == EMULATE_DO_MMIO) | |
1158 | return 0; | |
1159 | return 1; | |
1160 | } | |
1161 | ||
1162 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; | |
1163 | port = io_info >> 16; | |
1164 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; | |
1165 | rep = (io_info & SVM_IOIO_REP_MASK) != 0; | |
1166 | down = (svm->vmcb->save.rflags & X86_EFLAGS_DF) != 0; | |
1167 | ||
1168 | skip_emulated_instruction(&svm->vcpu); | |
1169 | return kvm_emulate_pio(&svm->vcpu, kvm_run, in, size, port); | |
1170 | } | |
1171 | ||
1172 | static int nmi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1173 | { | |
1174 | KVMTRACE_0D(NMI, &svm->vcpu, handler); | |
1175 | return 1; | |
1176 | } | |
1177 | ||
1178 | static int intr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1179 | { | |
1180 | ++svm->vcpu.stat.irq_exits; | |
1181 | KVMTRACE_0D(INTR, &svm->vcpu, handler); | |
1182 | return 1; | |
1183 | } | |
1184 | ||
1185 | static int nop_on_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1186 | { | |
1187 | return 1; | |
1188 | } | |
1189 | ||
1190 | static int halt_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1191 | { | |
1192 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; | |
1193 | skip_emulated_instruction(&svm->vcpu); | |
1194 | return kvm_emulate_halt(&svm->vcpu); | |
1195 | } | |
1196 | ||
1197 | static int vmmcall_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1198 | { | |
1199 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
1200 | skip_emulated_instruction(&svm->vcpu); | |
1201 | kvm_emulate_hypercall(&svm->vcpu); | |
1202 | return 1; | |
1203 | } | |
1204 | ||
1205 | static int nested_svm_check_permissions(struct vcpu_svm *svm) | |
1206 | { | |
1207 | if (!(svm->vcpu.arch.shadow_efer & EFER_SVME) | |
1208 | || !is_paging(&svm->vcpu)) { | |
1209 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
1210 | return 1; | |
1211 | } | |
1212 | ||
1213 | if (svm->vmcb->save.cpl) { | |
1214 | kvm_inject_gp(&svm->vcpu, 0); | |
1215 | return 1; | |
1216 | } | |
1217 | ||
1218 | return 0; | |
1219 | } | |
1220 | ||
1221 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, | |
1222 | bool has_error_code, u32 error_code) | |
1223 | { | |
1224 | if (is_nested(svm)) { | |
1225 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; | |
1226 | svm->vmcb->control.exit_code_hi = 0; | |
1227 | svm->vmcb->control.exit_info_1 = error_code; | |
1228 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; | |
1229 | if (nested_svm_exit_handled(svm, false)) { | |
1230 | nsvm_printk("VMexit -> EXCP 0x%x\n", nr); | |
1231 | ||
1232 | nested_svm_vmexit(svm); | |
1233 | return 1; | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | return 0; | |
1238 | } | |
1239 | ||
1240 | static inline int nested_svm_intr(struct vcpu_svm *svm) | |
1241 | { | |
1242 | if (is_nested(svm)) { | |
1243 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) | |
1244 | return 0; | |
1245 | ||
1246 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) | |
1247 | return 0; | |
1248 | ||
1249 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; | |
1250 | ||
1251 | if (nested_svm_exit_handled(svm, false)) { | |
1252 | nsvm_printk("VMexit -> INTR\n"); | |
1253 | nested_svm_vmexit(svm); | |
1254 | return 1; | |
1255 | } | |
1256 | } | |
1257 | ||
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | static struct page *nested_svm_get_page(struct vcpu_svm *svm, u64 gpa) | |
1262 | { | |
1263 | struct page *page; | |
1264 | ||
1265 | down_read(¤t->mm->mmap_sem); | |
1266 | page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT); | |
1267 | up_read(¤t->mm->mmap_sem); | |
1268 | ||
1269 | if (is_error_page(page)) { | |
1270 | printk(KERN_INFO "%s: could not find page at 0x%llx\n", | |
1271 | __func__, gpa); | |
1272 | kvm_release_page_clean(page); | |
1273 | kvm_inject_gp(&svm->vcpu, 0); | |
1274 | return NULL; | |
1275 | } | |
1276 | return page; | |
1277 | } | |
1278 | ||
1279 | static int nested_svm_do(struct vcpu_svm *svm, | |
1280 | u64 arg1_gpa, u64 arg2_gpa, void *opaque, | |
1281 | int (*handler)(struct vcpu_svm *svm, | |
1282 | void *arg1, | |
1283 | void *arg2, | |
1284 | void *opaque)) | |
1285 | { | |
1286 | struct page *arg1_page; | |
1287 | struct page *arg2_page = NULL; | |
1288 | void *arg1; | |
1289 | void *arg2 = NULL; | |
1290 | int retval; | |
1291 | ||
1292 | arg1_page = nested_svm_get_page(svm, arg1_gpa); | |
1293 | if(arg1_page == NULL) | |
1294 | return 1; | |
1295 | ||
1296 | if (arg2_gpa) { | |
1297 | arg2_page = nested_svm_get_page(svm, arg2_gpa); | |
1298 | if(arg2_page == NULL) { | |
1299 | kvm_release_page_clean(arg1_page); | |
1300 | return 1; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | arg1 = kmap_atomic(arg1_page, KM_USER0); | |
1305 | if (arg2_gpa) | |
1306 | arg2 = kmap_atomic(arg2_page, KM_USER1); | |
1307 | ||
1308 | retval = handler(svm, arg1, arg2, opaque); | |
1309 | ||
1310 | kunmap_atomic(arg1, KM_USER0); | |
1311 | if (arg2_gpa) | |
1312 | kunmap_atomic(arg2, KM_USER1); | |
1313 | ||
1314 | kvm_release_page_dirty(arg1_page); | |
1315 | if (arg2_gpa) | |
1316 | kvm_release_page_dirty(arg2_page); | |
1317 | ||
1318 | return retval; | |
1319 | } | |
1320 | ||
1321 | static int nested_svm_exit_handled_real(struct vcpu_svm *svm, | |
1322 | void *arg1, | |
1323 | void *arg2, | |
1324 | void *opaque) | |
1325 | { | |
1326 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; | |
1327 | bool kvm_overrides = *(bool *)opaque; | |
1328 | u32 exit_code = svm->vmcb->control.exit_code; | |
1329 | ||
1330 | if (kvm_overrides) { | |
1331 | switch (exit_code) { | |
1332 | case SVM_EXIT_INTR: | |
1333 | case SVM_EXIT_NMI: | |
1334 | return 0; | |
1335 | /* For now we are always handling NPFs when using them */ | |
1336 | case SVM_EXIT_NPF: | |
1337 | if (npt_enabled) | |
1338 | return 0; | |
1339 | break; | |
1340 | /* When we're shadowing, trap PFs */ | |
1341 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: | |
1342 | if (!npt_enabled) | |
1343 | return 0; | |
1344 | break; | |
1345 | default: | |
1346 | break; | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | switch (exit_code) { | |
1351 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: { | |
1352 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0); | |
1353 | if (nested_vmcb->control.intercept_cr_read & cr_bits) | |
1354 | return 1; | |
1355 | break; | |
1356 | } | |
1357 | case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: { | |
1358 | u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0); | |
1359 | if (nested_vmcb->control.intercept_cr_write & cr_bits) | |
1360 | return 1; | |
1361 | break; | |
1362 | } | |
1363 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: { | |
1364 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0); | |
1365 | if (nested_vmcb->control.intercept_dr_read & dr_bits) | |
1366 | return 1; | |
1367 | break; | |
1368 | } | |
1369 | case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: { | |
1370 | u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0); | |
1371 | if (nested_vmcb->control.intercept_dr_write & dr_bits) | |
1372 | return 1; | |
1373 | break; | |
1374 | } | |
1375 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { | |
1376 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); | |
1377 | if (nested_vmcb->control.intercept_exceptions & excp_bits) | |
1378 | return 1; | |
1379 | break; | |
1380 | } | |
1381 | default: { | |
1382 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); | |
1383 | nsvm_printk("exit code: 0x%x\n", exit_code); | |
1384 | if (nested_vmcb->control.intercept & exit_bits) | |
1385 | return 1; | |
1386 | } | |
1387 | } | |
1388 | ||
1389 | return 0; | |
1390 | } | |
1391 | ||
1392 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm, | |
1393 | void *arg1, void *arg2, | |
1394 | void *opaque) | |
1395 | { | |
1396 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; | |
1397 | u8 *msrpm = (u8 *)arg2; | |
1398 | u32 t0, t1; | |
1399 | u32 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; | |
1400 | u32 param = svm->vmcb->control.exit_info_1 & 1; | |
1401 | ||
1402 | if (!(nested_vmcb->control.intercept & (1ULL << INTERCEPT_MSR_PROT))) | |
1403 | return 0; | |
1404 | ||
1405 | switch(msr) { | |
1406 | case 0 ... 0x1fff: | |
1407 | t0 = (msr * 2) % 8; | |
1408 | t1 = msr / 8; | |
1409 | break; | |
1410 | case 0xc0000000 ... 0xc0001fff: | |
1411 | t0 = (8192 + msr - 0xc0000000) * 2; | |
1412 | t1 = (t0 / 8); | |
1413 | t0 %= 8; | |
1414 | break; | |
1415 | case 0xc0010000 ... 0xc0011fff: | |
1416 | t0 = (16384 + msr - 0xc0010000) * 2; | |
1417 | t1 = (t0 / 8); | |
1418 | t0 %= 8; | |
1419 | break; | |
1420 | default: | |
1421 | return 1; | |
1422 | break; | |
1423 | } | |
1424 | if (msrpm[t1] & ((1 << param) << t0)) | |
1425 | return 1; | |
1426 | ||
1427 | return 0; | |
1428 | } | |
1429 | ||
1430 | static int nested_svm_exit_handled(struct vcpu_svm *svm, bool kvm_override) | |
1431 | { | |
1432 | bool k = kvm_override; | |
1433 | ||
1434 | switch (svm->vmcb->control.exit_code) { | |
1435 | case SVM_EXIT_MSR: | |
1436 | return nested_svm_do(svm, svm->nested_vmcb, | |
1437 | svm->nested_vmcb_msrpm, NULL, | |
1438 | nested_svm_exit_handled_msr); | |
1439 | default: break; | |
1440 | } | |
1441 | ||
1442 | return nested_svm_do(svm, svm->nested_vmcb, 0, &k, | |
1443 | nested_svm_exit_handled_real); | |
1444 | } | |
1445 | ||
1446 | static int nested_svm_vmexit_real(struct vcpu_svm *svm, void *arg1, | |
1447 | void *arg2, void *opaque) | |
1448 | { | |
1449 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; | |
1450 | struct vmcb *hsave = svm->hsave; | |
1451 | u64 nested_save[] = { nested_vmcb->save.cr0, | |
1452 | nested_vmcb->save.cr3, | |
1453 | nested_vmcb->save.cr4, | |
1454 | nested_vmcb->save.efer, | |
1455 | nested_vmcb->control.intercept_cr_read, | |
1456 | nested_vmcb->control.intercept_cr_write, | |
1457 | nested_vmcb->control.intercept_dr_read, | |
1458 | nested_vmcb->control.intercept_dr_write, | |
1459 | nested_vmcb->control.intercept_exceptions, | |
1460 | nested_vmcb->control.intercept, | |
1461 | nested_vmcb->control.msrpm_base_pa, | |
1462 | nested_vmcb->control.iopm_base_pa, | |
1463 | nested_vmcb->control.tsc_offset }; | |
1464 | ||
1465 | /* Give the current vmcb to the guest */ | |
1466 | memcpy(nested_vmcb, svm->vmcb, sizeof(struct vmcb)); | |
1467 | nested_vmcb->save.cr0 = nested_save[0]; | |
1468 | if (!npt_enabled) | |
1469 | nested_vmcb->save.cr3 = nested_save[1]; | |
1470 | nested_vmcb->save.cr4 = nested_save[2]; | |
1471 | nested_vmcb->save.efer = nested_save[3]; | |
1472 | nested_vmcb->control.intercept_cr_read = nested_save[4]; | |
1473 | nested_vmcb->control.intercept_cr_write = nested_save[5]; | |
1474 | nested_vmcb->control.intercept_dr_read = nested_save[6]; | |
1475 | nested_vmcb->control.intercept_dr_write = nested_save[7]; | |
1476 | nested_vmcb->control.intercept_exceptions = nested_save[8]; | |
1477 | nested_vmcb->control.intercept = nested_save[9]; | |
1478 | nested_vmcb->control.msrpm_base_pa = nested_save[10]; | |
1479 | nested_vmcb->control.iopm_base_pa = nested_save[11]; | |
1480 | nested_vmcb->control.tsc_offset = nested_save[12]; | |
1481 | ||
1482 | /* We always set V_INTR_MASKING and remember the old value in hflags */ | |
1483 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) | |
1484 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; | |
1485 | ||
1486 | if ((nested_vmcb->control.int_ctl & V_IRQ_MASK) && | |
1487 | (nested_vmcb->control.int_vector)) { | |
1488 | nsvm_printk("WARNING: IRQ 0x%x still enabled on #VMEXIT\n", | |
1489 | nested_vmcb->control.int_vector); | |
1490 | } | |
1491 | ||
1492 | /* Restore the original control entries */ | |
1493 | svm->vmcb->control = hsave->control; | |
1494 | ||
1495 | /* Kill any pending exceptions */ | |
1496 | if (svm->vcpu.arch.exception.pending == true) | |
1497 | nsvm_printk("WARNING: Pending Exception\n"); | |
1498 | svm->vcpu.arch.exception.pending = false; | |
1499 | ||
1500 | /* Restore selected save entries */ | |
1501 | svm->vmcb->save.es = hsave->save.es; | |
1502 | svm->vmcb->save.cs = hsave->save.cs; | |
1503 | svm->vmcb->save.ss = hsave->save.ss; | |
1504 | svm->vmcb->save.ds = hsave->save.ds; | |
1505 | svm->vmcb->save.gdtr = hsave->save.gdtr; | |
1506 | svm->vmcb->save.idtr = hsave->save.idtr; | |
1507 | svm->vmcb->save.rflags = hsave->save.rflags; | |
1508 | svm_set_efer(&svm->vcpu, hsave->save.efer); | |
1509 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); | |
1510 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); | |
1511 | if (npt_enabled) { | |
1512 | svm->vmcb->save.cr3 = hsave->save.cr3; | |
1513 | svm->vcpu.arch.cr3 = hsave->save.cr3; | |
1514 | } else { | |
1515 | kvm_set_cr3(&svm->vcpu, hsave->save.cr3); | |
1516 | } | |
1517 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); | |
1518 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); | |
1519 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); | |
1520 | svm->vmcb->save.dr7 = 0; | |
1521 | svm->vmcb->save.cpl = 0; | |
1522 | svm->vmcb->control.exit_int_info = 0; | |
1523 | ||
1524 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; | |
1525 | /* Exit nested SVM mode */ | |
1526 | svm->nested_vmcb = 0; | |
1527 | ||
1528 | return 0; | |
1529 | } | |
1530 | ||
1531 | static int nested_svm_vmexit(struct vcpu_svm *svm) | |
1532 | { | |
1533 | nsvm_printk("VMexit\n"); | |
1534 | if (nested_svm_do(svm, svm->nested_vmcb, 0, | |
1535 | NULL, nested_svm_vmexit_real)) | |
1536 | return 1; | |
1537 | ||
1538 | kvm_mmu_reset_context(&svm->vcpu); | |
1539 | kvm_mmu_load(&svm->vcpu); | |
1540 | ||
1541 | return 0; | |
1542 | } | |
1543 | ||
1544 | static int nested_svm_vmrun_msrpm(struct vcpu_svm *svm, void *arg1, | |
1545 | void *arg2, void *opaque) | |
1546 | { | |
1547 | int i; | |
1548 | u32 *nested_msrpm = (u32*)arg1; | |
1549 | for (i=0; i< PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER) / 4; i++) | |
1550 | svm->nested_msrpm[i] = svm->msrpm[i] | nested_msrpm[i]; | |
1551 | svm->vmcb->control.msrpm_base_pa = __pa(svm->nested_msrpm); | |
1552 | ||
1553 | return 0; | |
1554 | } | |
1555 | ||
1556 | static int nested_svm_vmrun(struct vcpu_svm *svm, void *arg1, | |
1557 | void *arg2, void *opaque) | |
1558 | { | |
1559 | struct vmcb *nested_vmcb = (struct vmcb *)arg1; | |
1560 | struct vmcb *hsave = svm->hsave; | |
1561 | ||
1562 | /* nested_vmcb is our indicator if nested SVM is activated */ | |
1563 | svm->nested_vmcb = svm->vmcb->save.rax; | |
1564 | ||
1565 | /* Clear internal status */ | |
1566 | svm->vcpu.arch.exception.pending = false; | |
1567 | ||
1568 | /* Save the old vmcb, so we don't need to pick what we save, but | |
1569 | can restore everything when a VMEXIT occurs */ | |
1570 | memcpy(hsave, svm->vmcb, sizeof(struct vmcb)); | |
1571 | /* We need to remember the original CR3 in the SPT case */ | |
1572 | if (!npt_enabled) | |
1573 | hsave->save.cr3 = svm->vcpu.arch.cr3; | |
1574 | hsave->save.cr4 = svm->vcpu.arch.cr4; | |
1575 | hsave->save.rip = svm->next_rip; | |
1576 | ||
1577 | if (svm->vmcb->save.rflags & X86_EFLAGS_IF) | |
1578 | svm->vcpu.arch.hflags |= HF_HIF_MASK; | |
1579 | else | |
1580 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; | |
1581 | ||
1582 | /* Load the nested guest state */ | |
1583 | svm->vmcb->save.es = nested_vmcb->save.es; | |
1584 | svm->vmcb->save.cs = nested_vmcb->save.cs; | |
1585 | svm->vmcb->save.ss = nested_vmcb->save.ss; | |
1586 | svm->vmcb->save.ds = nested_vmcb->save.ds; | |
1587 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; | |
1588 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; | |
1589 | svm->vmcb->save.rflags = nested_vmcb->save.rflags; | |
1590 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); | |
1591 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); | |
1592 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); | |
1593 | if (npt_enabled) { | |
1594 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; | |
1595 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; | |
1596 | } else { | |
1597 | kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); | |
1598 | kvm_mmu_reset_context(&svm->vcpu); | |
1599 | } | |
1600 | svm->vmcb->save.cr2 = nested_vmcb->save.cr2; | |
1601 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); | |
1602 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); | |
1603 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); | |
1604 | /* In case we don't even reach vcpu_run, the fields are not updated */ | |
1605 | svm->vmcb->save.rax = nested_vmcb->save.rax; | |
1606 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; | |
1607 | svm->vmcb->save.rip = nested_vmcb->save.rip; | |
1608 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; | |
1609 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; | |
1610 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; | |
1611 | ||
1612 | /* We don't want a nested guest to be more powerful than the guest, | |
1613 | so all intercepts are ORed */ | |
1614 | svm->vmcb->control.intercept_cr_read |= | |
1615 | nested_vmcb->control.intercept_cr_read; | |
1616 | svm->vmcb->control.intercept_cr_write |= | |
1617 | nested_vmcb->control.intercept_cr_write; | |
1618 | svm->vmcb->control.intercept_dr_read |= | |
1619 | nested_vmcb->control.intercept_dr_read; | |
1620 | svm->vmcb->control.intercept_dr_write |= | |
1621 | nested_vmcb->control.intercept_dr_write; | |
1622 | svm->vmcb->control.intercept_exceptions |= | |
1623 | nested_vmcb->control.intercept_exceptions; | |
1624 | ||
1625 | svm->vmcb->control.intercept |= nested_vmcb->control.intercept; | |
1626 | ||
1627 | svm->nested_vmcb_msrpm = nested_vmcb->control.msrpm_base_pa; | |
1628 | ||
1629 | force_new_asid(&svm->vcpu); | |
1630 | svm->vmcb->control.exit_int_info = nested_vmcb->control.exit_int_info; | |
1631 | svm->vmcb->control.exit_int_info_err = nested_vmcb->control.exit_int_info_err; | |
1632 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; | |
1633 | if (nested_vmcb->control.int_ctl & V_IRQ_MASK) { | |
1634 | nsvm_printk("nSVM Injecting Interrupt: 0x%x\n", | |
1635 | nested_vmcb->control.int_ctl); | |
1636 | } | |
1637 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) | |
1638 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; | |
1639 | else | |
1640 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; | |
1641 | ||
1642 | nsvm_printk("nSVM exit_int_info: 0x%x | int_state: 0x%x\n", | |
1643 | nested_vmcb->control.exit_int_info, | |
1644 | nested_vmcb->control.int_state); | |
1645 | ||
1646 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; | |
1647 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; | |
1648 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; | |
1649 | if (nested_vmcb->control.event_inj & SVM_EVTINJ_VALID) | |
1650 | nsvm_printk("Injecting Event: 0x%x\n", | |
1651 | nested_vmcb->control.event_inj); | |
1652 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; | |
1653 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; | |
1654 | ||
1655 | svm->vcpu.arch.hflags |= HF_GIF_MASK; | |
1656 | ||
1657 | return 0; | |
1658 | } | |
1659 | ||
1660 | static int nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) | |
1661 | { | |
1662 | to_vmcb->save.fs = from_vmcb->save.fs; | |
1663 | to_vmcb->save.gs = from_vmcb->save.gs; | |
1664 | to_vmcb->save.tr = from_vmcb->save.tr; | |
1665 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; | |
1666 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; | |
1667 | to_vmcb->save.star = from_vmcb->save.star; | |
1668 | to_vmcb->save.lstar = from_vmcb->save.lstar; | |
1669 | to_vmcb->save.cstar = from_vmcb->save.cstar; | |
1670 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; | |
1671 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; | |
1672 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; | |
1673 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; | |
1674 | ||
1675 | return 1; | |
1676 | } | |
1677 | ||
1678 | static int nested_svm_vmload(struct vcpu_svm *svm, void *nested_vmcb, | |
1679 | void *arg2, void *opaque) | |
1680 | { | |
1681 | return nested_svm_vmloadsave((struct vmcb *)nested_vmcb, svm->vmcb); | |
1682 | } | |
1683 | ||
1684 | static int nested_svm_vmsave(struct vcpu_svm *svm, void *nested_vmcb, | |
1685 | void *arg2, void *opaque) | |
1686 | { | |
1687 | return nested_svm_vmloadsave(svm->vmcb, (struct vmcb *)nested_vmcb); | |
1688 | } | |
1689 | ||
1690 | static int vmload_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1691 | { | |
1692 | if (nested_svm_check_permissions(svm)) | |
1693 | return 1; | |
1694 | ||
1695 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
1696 | skip_emulated_instruction(&svm->vcpu); | |
1697 | ||
1698 | nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmload); | |
1699 | ||
1700 | return 1; | |
1701 | } | |
1702 | ||
1703 | static int vmsave_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1704 | { | |
1705 | if (nested_svm_check_permissions(svm)) | |
1706 | return 1; | |
1707 | ||
1708 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
1709 | skip_emulated_instruction(&svm->vcpu); | |
1710 | ||
1711 | nested_svm_do(svm, svm->vmcb->save.rax, 0, NULL, nested_svm_vmsave); | |
1712 | ||
1713 | return 1; | |
1714 | } | |
1715 | ||
1716 | static int vmrun_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1717 | { | |
1718 | nsvm_printk("VMrun\n"); | |
1719 | if (nested_svm_check_permissions(svm)) | |
1720 | return 1; | |
1721 | ||
1722 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
1723 | skip_emulated_instruction(&svm->vcpu); | |
1724 | ||
1725 | if (nested_svm_do(svm, svm->vmcb->save.rax, 0, | |
1726 | NULL, nested_svm_vmrun)) | |
1727 | return 1; | |
1728 | ||
1729 | if (nested_svm_do(svm, svm->nested_vmcb_msrpm, 0, | |
1730 | NULL, nested_svm_vmrun_msrpm)) | |
1731 | return 1; | |
1732 | ||
1733 | return 1; | |
1734 | } | |
1735 | ||
1736 | static int stgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1737 | { | |
1738 | if (nested_svm_check_permissions(svm)) | |
1739 | return 1; | |
1740 | ||
1741 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
1742 | skip_emulated_instruction(&svm->vcpu); | |
1743 | ||
1744 | svm->vcpu.arch.hflags |= HF_GIF_MASK; | |
1745 | ||
1746 | return 1; | |
1747 | } | |
1748 | ||
1749 | static int clgi_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1750 | { | |
1751 | if (nested_svm_check_permissions(svm)) | |
1752 | return 1; | |
1753 | ||
1754 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
1755 | skip_emulated_instruction(&svm->vcpu); | |
1756 | ||
1757 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; | |
1758 | ||
1759 | /* After a CLGI no interrupts should come */ | |
1760 | svm_clear_vintr(svm); | |
1761 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; | |
1762 | ||
1763 | return 1; | |
1764 | } | |
1765 | ||
1766 | static int invalid_op_interception(struct vcpu_svm *svm, | |
1767 | struct kvm_run *kvm_run) | |
1768 | { | |
1769 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
1770 | return 1; | |
1771 | } | |
1772 | ||
1773 | static int task_switch_interception(struct vcpu_svm *svm, | |
1774 | struct kvm_run *kvm_run) | |
1775 | { | |
1776 | u16 tss_selector; | |
1777 | ||
1778 | tss_selector = (u16)svm->vmcb->control.exit_info_1; | |
1779 | if (svm->vmcb->control.exit_info_2 & | |
1780 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) | |
1781 | return kvm_task_switch(&svm->vcpu, tss_selector, | |
1782 | TASK_SWITCH_IRET); | |
1783 | if (svm->vmcb->control.exit_info_2 & | |
1784 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) | |
1785 | return kvm_task_switch(&svm->vcpu, tss_selector, | |
1786 | TASK_SWITCH_JMP); | |
1787 | return kvm_task_switch(&svm->vcpu, tss_selector, TASK_SWITCH_CALL); | |
1788 | } | |
1789 | ||
1790 | static int cpuid_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1791 | { | |
1792 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; | |
1793 | kvm_emulate_cpuid(&svm->vcpu); | |
1794 | return 1; | |
1795 | } | |
1796 | ||
1797 | static int invlpg_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1798 | { | |
1799 | if (emulate_instruction(&svm->vcpu, kvm_run, 0, 0, 0) != EMULATE_DONE) | |
1800 | pr_unimpl(&svm->vcpu, "%s: failed\n", __func__); | |
1801 | return 1; | |
1802 | } | |
1803 | ||
1804 | static int emulate_on_interception(struct vcpu_svm *svm, | |
1805 | struct kvm_run *kvm_run) | |
1806 | { | |
1807 | if (emulate_instruction(&svm->vcpu, NULL, 0, 0, 0) != EMULATE_DONE) | |
1808 | pr_unimpl(&svm->vcpu, "%s: failed\n", __func__); | |
1809 | return 1; | |
1810 | } | |
1811 | ||
1812 | static int cr8_write_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1813 | { | |
1814 | emulate_instruction(&svm->vcpu, NULL, 0, 0, 0); | |
1815 | if (irqchip_in_kernel(svm->vcpu.kvm)) | |
1816 | return 1; | |
1817 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; | |
1818 | return 0; | |
1819 | } | |
1820 | ||
1821 | static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) | |
1822 | { | |
1823 | struct vcpu_svm *svm = to_svm(vcpu); | |
1824 | ||
1825 | switch (ecx) { | |
1826 | case MSR_IA32_TIME_STAMP_COUNTER: { | |
1827 | u64 tsc; | |
1828 | ||
1829 | rdtscll(tsc); | |
1830 | *data = svm->vmcb->control.tsc_offset + tsc; | |
1831 | break; | |
1832 | } | |
1833 | case MSR_K6_STAR: | |
1834 | *data = svm->vmcb->save.star; | |
1835 | break; | |
1836 | #ifdef CONFIG_X86_64 | |
1837 | case MSR_LSTAR: | |
1838 | *data = svm->vmcb->save.lstar; | |
1839 | break; | |
1840 | case MSR_CSTAR: | |
1841 | *data = svm->vmcb->save.cstar; | |
1842 | break; | |
1843 | case MSR_KERNEL_GS_BASE: | |
1844 | *data = svm->vmcb->save.kernel_gs_base; | |
1845 | break; | |
1846 | case MSR_SYSCALL_MASK: | |
1847 | *data = svm->vmcb->save.sfmask; | |
1848 | break; | |
1849 | #endif | |
1850 | case MSR_IA32_SYSENTER_CS: | |
1851 | *data = svm->vmcb->save.sysenter_cs; | |
1852 | break; | |
1853 | case MSR_IA32_SYSENTER_EIP: | |
1854 | *data = svm->vmcb->save.sysenter_eip; | |
1855 | break; | |
1856 | case MSR_IA32_SYSENTER_ESP: | |
1857 | *data = svm->vmcb->save.sysenter_esp; | |
1858 | break; | |
1859 | /* Nobody will change the following 5 values in the VMCB so | |
1860 | we can safely return them on rdmsr. They will always be 0 | |
1861 | until LBRV is implemented. */ | |
1862 | case MSR_IA32_DEBUGCTLMSR: | |
1863 | *data = svm->vmcb->save.dbgctl; | |
1864 | break; | |
1865 | case MSR_IA32_LASTBRANCHFROMIP: | |
1866 | *data = svm->vmcb->save.br_from; | |
1867 | break; | |
1868 | case MSR_IA32_LASTBRANCHTOIP: | |
1869 | *data = svm->vmcb->save.br_to; | |
1870 | break; | |
1871 | case MSR_IA32_LASTINTFROMIP: | |
1872 | *data = svm->vmcb->save.last_excp_from; | |
1873 | break; | |
1874 | case MSR_IA32_LASTINTTOIP: | |
1875 | *data = svm->vmcb->save.last_excp_to; | |
1876 | break; | |
1877 | case MSR_VM_HSAVE_PA: | |
1878 | *data = svm->hsave_msr; | |
1879 | break; | |
1880 | case MSR_VM_CR: | |
1881 | *data = 0; | |
1882 | break; | |
1883 | default: | |
1884 | return kvm_get_msr_common(vcpu, ecx, data); | |
1885 | } | |
1886 | return 0; | |
1887 | } | |
1888 | ||
1889 | static int rdmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1890 | { | |
1891 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; | |
1892 | u64 data; | |
1893 | ||
1894 | if (svm_get_msr(&svm->vcpu, ecx, &data)) | |
1895 | kvm_inject_gp(&svm->vcpu, 0); | |
1896 | else { | |
1897 | KVMTRACE_3D(MSR_READ, &svm->vcpu, ecx, (u32)data, | |
1898 | (u32)(data >> 32), handler); | |
1899 | ||
1900 | svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff; | |
1901 | svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32; | |
1902 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; | |
1903 | skip_emulated_instruction(&svm->vcpu); | |
1904 | } | |
1905 | return 1; | |
1906 | } | |
1907 | ||
1908 | static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) | |
1909 | { | |
1910 | struct vcpu_svm *svm = to_svm(vcpu); | |
1911 | ||
1912 | switch (ecx) { | |
1913 | case MSR_IA32_TIME_STAMP_COUNTER: { | |
1914 | u64 tsc; | |
1915 | ||
1916 | rdtscll(tsc); | |
1917 | svm->vmcb->control.tsc_offset = data - tsc; | |
1918 | break; | |
1919 | } | |
1920 | case MSR_K6_STAR: | |
1921 | svm->vmcb->save.star = data; | |
1922 | break; | |
1923 | #ifdef CONFIG_X86_64 | |
1924 | case MSR_LSTAR: | |
1925 | svm->vmcb->save.lstar = data; | |
1926 | break; | |
1927 | case MSR_CSTAR: | |
1928 | svm->vmcb->save.cstar = data; | |
1929 | break; | |
1930 | case MSR_KERNEL_GS_BASE: | |
1931 | svm->vmcb->save.kernel_gs_base = data; | |
1932 | break; | |
1933 | case MSR_SYSCALL_MASK: | |
1934 | svm->vmcb->save.sfmask = data; | |
1935 | break; | |
1936 | #endif | |
1937 | case MSR_IA32_SYSENTER_CS: | |
1938 | svm->vmcb->save.sysenter_cs = data; | |
1939 | break; | |
1940 | case MSR_IA32_SYSENTER_EIP: | |
1941 | svm->vmcb->save.sysenter_eip = data; | |
1942 | break; | |
1943 | case MSR_IA32_SYSENTER_ESP: | |
1944 | svm->vmcb->save.sysenter_esp = data; | |
1945 | break; | |
1946 | case MSR_IA32_DEBUGCTLMSR: | |
1947 | if (!svm_has(SVM_FEATURE_LBRV)) { | |
1948 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", | |
1949 | __func__, data); | |
1950 | break; | |
1951 | } | |
1952 | if (data & DEBUGCTL_RESERVED_BITS) | |
1953 | return 1; | |
1954 | ||
1955 | svm->vmcb->save.dbgctl = data; | |
1956 | if (data & (1ULL<<0)) | |
1957 | svm_enable_lbrv(svm); | |
1958 | else | |
1959 | svm_disable_lbrv(svm); | |
1960 | break; | |
1961 | case MSR_K7_EVNTSEL0: | |
1962 | case MSR_K7_EVNTSEL1: | |
1963 | case MSR_K7_EVNTSEL2: | |
1964 | case MSR_K7_EVNTSEL3: | |
1965 | case MSR_K7_PERFCTR0: | |
1966 | case MSR_K7_PERFCTR1: | |
1967 | case MSR_K7_PERFCTR2: | |
1968 | case MSR_K7_PERFCTR3: | |
1969 | /* | |
1970 | * Just discard all writes to the performance counters; this | |
1971 | * should keep both older linux and windows 64-bit guests | |
1972 | * happy | |
1973 | */ | |
1974 | pr_unimpl(vcpu, "unimplemented perfctr wrmsr: 0x%x data 0x%llx\n", ecx, data); | |
1975 | ||
1976 | break; | |
1977 | case MSR_VM_HSAVE_PA: | |
1978 | svm->hsave_msr = data; | |
1979 | break; | |
1980 | default: | |
1981 | return kvm_set_msr_common(vcpu, ecx, data); | |
1982 | } | |
1983 | return 0; | |
1984 | } | |
1985 | ||
1986 | static int wrmsr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
1987 | { | |
1988 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; | |
1989 | u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u) | |
1990 | | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32); | |
1991 | ||
1992 | KVMTRACE_3D(MSR_WRITE, &svm->vcpu, ecx, (u32)data, (u32)(data >> 32), | |
1993 | handler); | |
1994 | ||
1995 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; | |
1996 | if (svm_set_msr(&svm->vcpu, ecx, data)) | |
1997 | kvm_inject_gp(&svm->vcpu, 0); | |
1998 | else | |
1999 | skip_emulated_instruction(&svm->vcpu); | |
2000 | return 1; | |
2001 | } | |
2002 | ||
2003 | static int msr_interception(struct vcpu_svm *svm, struct kvm_run *kvm_run) | |
2004 | { | |
2005 | if (svm->vmcb->control.exit_info_1) | |
2006 | return wrmsr_interception(svm, kvm_run); | |
2007 | else | |
2008 | return rdmsr_interception(svm, kvm_run); | |
2009 | } | |
2010 | ||
2011 | static int interrupt_window_interception(struct vcpu_svm *svm, | |
2012 | struct kvm_run *kvm_run) | |
2013 | { | |
2014 | KVMTRACE_0D(PEND_INTR, &svm->vcpu, handler); | |
2015 | ||
2016 | svm_clear_vintr(svm); | |
2017 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; | |
2018 | /* | |
2019 | * If the user space waits to inject interrupts, exit as soon as | |
2020 | * possible | |
2021 | */ | |
2022 | if (kvm_run->request_interrupt_window && | |
2023 | !svm->vcpu.arch.irq_summary) { | |
2024 | ++svm->vcpu.stat.irq_window_exits; | |
2025 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; | |
2026 | return 0; | |
2027 | } | |
2028 | ||
2029 | return 1; | |
2030 | } | |
2031 | ||
2032 | static int (*svm_exit_handlers[])(struct vcpu_svm *svm, | |
2033 | struct kvm_run *kvm_run) = { | |
2034 | [SVM_EXIT_READ_CR0] = emulate_on_interception, | |
2035 | [SVM_EXIT_READ_CR3] = emulate_on_interception, | |
2036 | [SVM_EXIT_READ_CR4] = emulate_on_interception, | |
2037 | [SVM_EXIT_READ_CR8] = emulate_on_interception, | |
2038 | /* for now: */ | |
2039 | [SVM_EXIT_WRITE_CR0] = emulate_on_interception, | |
2040 | [SVM_EXIT_WRITE_CR3] = emulate_on_interception, | |
2041 | [SVM_EXIT_WRITE_CR4] = emulate_on_interception, | |
2042 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, | |
2043 | [SVM_EXIT_READ_DR0] = emulate_on_interception, | |
2044 | [SVM_EXIT_READ_DR1] = emulate_on_interception, | |
2045 | [SVM_EXIT_READ_DR2] = emulate_on_interception, | |
2046 | [SVM_EXIT_READ_DR3] = emulate_on_interception, | |
2047 | [SVM_EXIT_WRITE_DR0] = emulate_on_interception, | |
2048 | [SVM_EXIT_WRITE_DR1] = emulate_on_interception, | |
2049 | [SVM_EXIT_WRITE_DR2] = emulate_on_interception, | |
2050 | [SVM_EXIT_WRITE_DR3] = emulate_on_interception, | |
2051 | [SVM_EXIT_WRITE_DR5] = emulate_on_interception, | |
2052 | [SVM_EXIT_WRITE_DR7] = emulate_on_interception, | |
2053 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, | |
2054 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, | |
2055 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, | |
2056 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, | |
2057 | [SVM_EXIT_INTR] = intr_interception, | |
2058 | [SVM_EXIT_NMI] = nmi_interception, | |
2059 | [SVM_EXIT_SMI] = nop_on_interception, | |
2060 | [SVM_EXIT_INIT] = nop_on_interception, | |
2061 | [SVM_EXIT_VINTR] = interrupt_window_interception, | |
2062 | /* [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, */ | |
2063 | [SVM_EXIT_CPUID] = cpuid_interception, | |
2064 | [SVM_EXIT_INVD] = emulate_on_interception, | |
2065 | [SVM_EXIT_HLT] = halt_interception, | |
2066 | [SVM_EXIT_INVLPG] = invlpg_interception, | |
2067 | [SVM_EXIT_INVLPGA] = invalid_op_interception, | |
2068 | [SVM_EXIT_IOIO] = io_interception, | |
2069 | [SVM_EXIT_MSR] = msr_interception, | |
2070 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, | |
2071 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, | |
2072 | [SVM_EXIT_VMRUN] = vmrun_interception, | |
2073 | [SVM_EXIT_VMMCALL] = vmmcall_interception, | |
2074 | [SVM_EXIT_VMLOAD] = vmload_interception, | |
2075 | [SVM_EXIT_VMSAVE] = vmsave_interception, | |
2076 | [SVM_EXIT_STGI] = stgi_interception, | |
2077 | [SVM_EXIT_CLGI] = clgi_interception, | |
2078 | [SVM_EXIT_SKINIT] = invalid_op_interception, | |
2079 | [SVM_EXIT_WBINVD] = emulate_on_interception, | |
2080 | [SVM_EXIT_MONITOR] = invalid_op_interception, | |
2081 | [SVM_EXIT_MWAIT] = invalid_op_interception, | |
2082 | [SVM_EXIT_NPF] = pf_interception, | |
2083 | }; | |
2084 | ||
2085 | static int handle_exit(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) | |
2086 | { | |
2087 | struct vcpu_svm *svm = to_svm(vcpu); | |
2088 | u32 exit_code = svm->vmcb->control.exit_code; | |
2089 | ||
2090 | KVMTRACE_3D(VMEXIT, vcpu, exit_code, (u32)svm->vmcb->save.rip, | |
2091 | (u32)((u64)svm->vmcb->save.rip >> 32), entryexit); | |
2092 | ||
2093 | if (is_nested(svm)) { | |
2094 | nsvm_printk("nested handle_exit: 0x%x | 0x%lx | 0x%lx | 0x%lx\n", | |
2095 | exit_code, svm->vmcb->control.exit_info_1, | |
2096 | svm->vmcb->control.exit_info_2, svm->vmcb->save.rip); | |
2097 | if (nested_svm_exit_handled(svm, true)) { | |
2098 | nested_svm_vmexit(svm); | |
2099 | nsvm_printk("-> #VMEXIT\n"); | |
2100 | return 1; | |
2101 | } | |
2102 | } | |
2103 | ||
2104 | if (npt_enabled) { | |
2105 | int mmu_reload = 0; | |
2106 | if ((vcpu->arch.cr0 ^ svm->vmcb->save.cr0) & X86_CR0_PG) { | |
2107 | svm_set_cr0(vcpu, svm->vmcb->save.cr0); | |
2108 | mmu_reload = 1; | |
2109 | } | |
2110 | vcpu->arch.cr0 = svm->vmcb->save.cr0; | |
2111 | vcpu->arch.cr3 = svm->vmcb->save.cr3; | |
2112 | if (is_paging(vcpu) && is_pae(vcpu) && !is_long_mode(vcpu)) { | |
2113 | if (!load_pdptrs(vcpu, vcpu->arch.cr3)) { | |
2114 | kvm_inject_gp(vcpu, 0); | |
2115 | return 1; | |
2116 | } | |
2117 | } | |
2118 | if (mmu_reload) { | |
2119 | kvm_mmu_reset_context(vcpu); | |
2120 | kvm_mmu_load(vcpu); | |
2121 | } | |
2122 | } | |
2123 | ||
2124 | kvm_reput_irq(svm); | |
2125 | ||
2126 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { | |
2127 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; | |
2128 | kvm_run->fail_entry.hardware_entry_failure_reason | |
2129 | = svm->vmcb->control.exit_code; | |
2130 | return 0; | |
2131 | } | |
2132 | ||
2133 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && | |
2134 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && | |
2135 | exit_code != SVM_EXIT_NPF) | |
2136 | printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x " | |
2137 | "exit_code 0x%x\n", | |
2138 | __func__, svm->vmcb->control.exit_int_info, | |
2139 | exit_code); | |
2140 | ||
2141 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) | |
2142 | || !svm_exit_handlers[exit_code]) { | |
2143 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; | |
2144 | kvm_run->hw.hardware_exit_reason = exit_code; | |
2145 | return 0; | |
2146 | } | |
2147 | ||
2148 | return svm_exit_handlers[exit_code](svm, kvm_run); | |
2149 | } | |
2150 | ||
2151 | static void reload_tss(struct kvm_vcpu *vcpu) | |
2152 | { | |
2153 | int cpu = raw_smp_processor_id(); | |
2154 | ||
2155 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); | |
2156 | svm_data->tss_desc->type = 9; /* available 32/64-bit TSS */ | |
2157 | load_TR_desc(); | |
2158 | } | |
2159 | ||
2160 | static void pre_svm_run(struct vcpu_svm *svm) | |
2161 | { | |
2162 | int cpu = raw_smp_processor_id(); | |
2163 | ||
2164 | struct svm_cpu_data *svm_data = per_cpu(svm_data, cpu); | |
2165 | ||
2166 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; | |
2167 | if (svm->vcpu.cpu != cpu || | |
2168 | svm->asid_generation != svm_data->asid_generation) | |
2169 | new_asid(svm, svm_data); | |
2170 | } | |
2171 | ||
2172 | ||
2173 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) | |
2174 | { | |
2175 | struct vmcb_control_area *control; | |
2176 | ||
2177 | KVMTRACE_1D(INJ_VIRQ, &svm->vcpu, (u32)irq, handler); | |
2178 | ||
2179 | ++svm->vcpu.stat.irq_injections; | |
2180 | control = &svm->vmcb->control; | |
2181 | control->int_vector = irq; | |
2182 | control->int_ctl &= ~V_INTR_PRIO_MASK; | |
2183 | control->int_ctl |= V_IRQ_MASK | | |
2184 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); | |
2185 | } | |
2186 | ||
2187 | static void svm_set_irq(struct kvm_vcpu *vcpu, int irq) | |
2188 | { | |
2189 | struct vcpu_svm *svm = to_svm(vcpu); | |
2190 | ||
2191 | nested_svm_intr(svm); | |
2192 | ||
2193 | svm_inject_irq(svm, irq); | |
2194 | } | |
2195 | ||
2196 | static void update_cr8_intercept(struct kvm_vcpu *vcpu) | |
2197 | { | |
2198 | struct vcpu_svm *svm = to_svm(vcpu); | |
2199 | struct vmcb *vmcb = svm->vmcb; | |
2200 | int max_irr, tpr; | |
2201 | ||
2202 | if (!irqchip_in_kernel(vcpu->kvm) || vcpu->arch.apic->vapic_addr) | |
2203 | return; | |
2204 | ||
2205 | vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK; | |
2206 | ||
2207 | max_irr = kvm_lapic_find_highest_irr(vcpu); | |
2208 | if (max_irr == -1) | |
2209 | return; | |
2210 | ||
2211 | tpr = kvm_lapic_get_cr8(vcpu) << 4; | |
2212 | ||
2213 | if (tpr >= (max_irr & 0xf0)) | |
2214 | vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK; | |
2215 | } | |
2216 | ||
2217 | static void svm_intr_assist(struct kvm_vcpu *vcpu) | |
2218 | { | |
2219 | struct vcpu_svm *svm = to_svm(vcpu); | |
2220 | struct vmcb *vmcb = svm->vmcb; | |
2221 | int intr_vector = -1; | |
2222 | ||
2223 | if ((vmcb->control.exit_int_info & SVM_EVTINJ_VALID) && | |
2224 | ((vmcb->control.exit_int_info & SVM_EVTINJ_TYPE_MASK) == 0)) { | |
2225 | intr_vector = vmcb->control.exit_int_info & | |
2226 | SVM_EVTINJ_VEC_MASK; | |
2227 | vmcb->control.exit_int_info = 0; | |
2228 | svm_inject_irq(svm, intr_vector); | |
2229 | goto out; | |
2230 | } | |
2231 | ||
2232 | if (vmcb->control.int_ctl & V_IRQ_MASK) | |
2233 | goto out; | |
2234 | ||
2235 | if (!kvm_cpu_has_interrupt(vcpu)) | |
2236 | goto out; | |
2237 | ||
2238 | if (nested_svm_intr(svm)) | |
2239 | goto out; | |
2240 | ||
2241 | if (!(svm->vcpu.arch.hflags & HF_GIF_MASK)) | |
2242 | goto out; | |
2243 | ||
2244 | if (!(vmcb->save.rflags & X86_EFLAGS_IF) || | |
2245 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) || | |
2246 | (vmcb->control.event_inj & SVM_EVTINJ_VALID)) { | |
2247 | /* unable to deliver irq, set pending irq */ | |
2248 | svm_set_vintr(svm); | |
2249 | svm_inject_irq(svm, 0x0); | |
2250 | goto out; | |
2251 | } | |
2252 | /* Okay, we can deliver the interrupt: grab it and update PIC state. */ | |
2253 | intr_vector = kvm_cpu_get_interrupt(vcpu); | |
2254 | svm_inject_irq(svm, intr_vector); | |
2255 | out: | |
2256 | update_cr8_intercept(vcpu); | |
2257 | } | |
2258 | ||
2259 | static void kvm_reput_irq(struct vcpu_svm *svm) | |
2260 | { | |
2261 | struct vmcb_control_area *control = &svm->vmcb->control; | |
2262 | ||
2263 | if ((control->int_ctl & V_IRQ_MASK) | |
2264 | && !irqchip_in_kernel(svm->vcpu.kvm)) { | |
2265 | control->int_ctl &= ~V_IRQ_MASK; | |
2266 | push_irq(&svm->vcpu, control->int_vector); | |
2267 | } | |
2268 | ||
2269 | svm->vcpu.arch.interrupt_window_open = | |
2270 | !(control->int_state & SVM_INTERRUPT_SHADOW_MASK) && | |
2271 | (svm->vcpu.arch.hflags & HF_GIF_MASK); | |
2272 | } | |
2273 | ||
2274 | static void svm_do_inject_vector(struct vcpu_svm *svm) | |
2275 | { | |
2276 | struct kvm_vcpu *vcpu = &svm->vcpu; | |
2277 | int word_index = __ffs(vcpu->arch.irq_summary); | |
2278 | int bit_index = __ffs(vcpu->arch.irq_pending[word_index]); | |
2279 | int irq = word_index * BITS_PER_LONG + bit_index; | |
2280 | ||
2281 | clear_bit(bit_index, &vcpu->arch.irq_pending[word_index]); | |
2282 | if (!vcpu->arch.irq_pending[word_index]) | |
2283 | clear_bit(word_index, &vcpu->arch.irq_summary); | |
2284 | svm_inject_irq(svm, irq); | |
2285 | } | |
2286 | ||
2287 | static void do_interrupt_requests(struct kvm_vcpu *vcpu, | |
2288 | struct kvm_run *kvm_run) | |
2289 | { | |
2290 | struct vcpu_svm *svm = to_svm(vcpu); | |
2291 | struct vmcb_control_area *control = &svm->vmcb->control; | |
2292 | ||
2293 | if (nested_svm_intr(svm)) | |
2294 | return; | |
2295 | ||
2296 | svm->vcpu.arch.interrupt_window_open = | |
2297 | (!(control->int_state & SVM_INTERRUPT_SHADOW_MASK) && | |
2298 | (svm->vmcb->save.rflags & X86_EFLAGS_IF) && | |
2299 | (svm->vcpu.arch.hflags & HF_GIF_MASK)); | |
2300 | ||
2301 | if (svm->vcpu.arch.interrupt_window_open && svm->vcpu.arch.irq_summary) | |
2302 | /* | |
2303 | * If interrupts enabled, and not blocked by sti or mov ss. Good. | |
2304 | */ | |
2305 | svm_do_inject_vector(svm); | |
2306 | ||
2307 | /* | |
2308 | * Interrupts blocked. Wait for unblock. | |
2309 | */ | |
2310 | if (!svm->vcpu.arch.interrupt_window_open && | |
2311 | (svm->vcpu.arch.irq_summary || kvm_run->request_interrupt_window)) | |
2312 | svm_set_vintr(svm); | |
2313 | else | |
2314 | svm_clear_vintr(svm); | |
2315 | } | |
2316 | ||
2317 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) | |
2318 | { | |
2319 | return 0; | |
2320 | } | |
2321 | ||
2322 | static void save_db_regs(unsigned long *db_regs) | |
2323 | { | |
2324 | asm volatile ("mov %%dr0, %0" : "=r"(db_regs[0])); | |
2325 | asm volatile ("mov %%dr1, %0" : "=r"(db_regs[1])); | |
2326 | asm volatile ("mov %%dr2, %0" : "=r"(db_regs[2])); | |
2327 | asm volatile ("mov %%dr3, %0" : "=r"(db_regs[3])); | |
2328 | } | |
2329 | ||
2330 | static void load_db_regs(unsigned long *db_regs) | |
2331 | { | |
2332 | asm volatile ("mov %0, %%dr0" : : "r"(db_regs[0])); | |
2333 | asm volatile ("mov %0, %%dr1" : : "r"(db_regs[1])); | |
2334 | asm volatile ("mov %0, %%dr2" : : "r"(db_regs[2])); | |
2335 | asm volatile ("mov %0, %%dr3" : : "r"(db_regs[3])); | |
2336 | } | |
2337 | ||
2338 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) | |
2339 | { | |
2340 | force_new_asid(vcpu); | |
2341 | } | |
2342 | ||
2343 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) | |
2344 | { | |
2345 | } | |
2346 | ||
2347 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) | |
2348 | { | |
2349 | struct vcpu_svm *svm = to_svm(vcpu); | |
2350 | ||
2351 | if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) { | |
2352 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; | |
2353 | kvm_lapic_set_tpr(vcpu, cr8); | |
2354 | } | |
2355 | } | |
2356 | ||
2357 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) | |
2358 | { | |
2359 | struct vcpu_svm *svm = to_svm(vcpu); | |
2360 | u64 cr8; | |
2361 | ||
2362 | if (!irqchip_in_kernel(vcpu->kvm)) | |
2363 | return; | |
2364 | ||
2365 | cr8 = kvm_get_cr8(vcpu); | |
2366 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; | |
2367 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; | |
2368 | } | |
2369 | ||
2370 | #ifdef CONFIG_X86_64 | |
2371 | #define R "r" | |
2372 | #else | |
2373 | #define R "e" | |
2374 | #endif | |
2375 | ||
2376 | static void svm_vcpu_run(struct kvm_vcpu *vcpu, struct kvm_run *kvm_run) | |
2377 | { | |
2378 | struct vcpu_svm *svm = to_svm(vcpu); | |
2379 | u16 fs_selector; | |
2380 | u16 gs_selector; | |
2381 | u16 ldt_selector; | |
2382 | ||
2383 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; | |
2384 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; | |
2385 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; | |
2386 | ||
2387 | pre_svm_run(svm); | |
2388 | ||
2389 | sync_lapic_to_cr8(vcpu); | |
2390 | ||
2391 | save_host_msrs(vcpu); | |
2392 | fs_selector = kvm_read_fs(); | |
2393 | gs_selector = kvm_read_gs(); | |
2394 | ldt_selector = kvm_read_ldt(); | |
2395 | svm->host_cr2 = kvm_read_cr2(); | |
2396 | svm->host_dr6 = read_dr6(); | |
2397 | svm->host_dr7 = read_dr7(); | |
2398 | if (!is_nested(svm)) | |
2399 | svm->vmcb->save.cr2 = vcpu->arch.cr2; | |
2400 | /* required for live migration with NPT */ | |
2401 | if (npt_enabled) | |
2402 | svm->vmcb->save.cr3 = vcpu->arch.cr3; | |
2403 | ||
2404 | if (svm->vmcb->save.dr7 & 0xff) { | |
2405 | write_dr7(0); | |
2406 | save_db_regs(svm->host_db_regs); | |
2407 | load_db_regs(svm->db_regs); | |
2408 | } | |
2409 | ||
2410 | clgi(); | |
2411 | ||
2412 | local_irq_enable(); | |
2413 | ||
2414 | asm volatile ( | |
2415 | "push %%"R"bp; \n\t" | |
2416 | "mov %c[rbx](%[svm]), %%"R"bx \n\t" | |
2417 | "mov %c[rcx](%[svm]), %%"R"cx \n\t" | |
2418 | "mov %c[rdx](%[svm]), %%"R"dx \n\t" | |
2419 | "mov %c[rsi](%[svm]), %%"R"si \n\t" | |
2420 | "mov %c[rdi](%[svm]), %%"R"di \n\t" | |
2421 | "mov %c[rbp](%[svm]), %%"R"bp \n\t" | |
2422 | #ifdef CONFIG_X86_64 | |
2423 | "mov %c[r8](%[svm]), %%r8 \n\t" | |
2424 | "mov %c[r9](%[svm]), %%r9 \n\t" | |
2425 | "mov %c[r10](%[svm]), %%r10 \n\t" | |
2426 | "mov %c[r11](%[svm]), %%r11 \n\t" | |
2427 | "mov %c[r12](%[svm]), %%r12 \n\t" | |
2428 | "mov %c[r13](%[svm]), %%r13 \n\t" | |
2429 | "mov %c[r14](%[svm]), %%r14 \n\t" | |
2430 | "mov %c[r15](%[svm]), %%r15 \n\t" | |
2431 | #endif | |
2432 | ||
2433 | /* Enter guest mode */ | |
2434 | "push %%"R"ax \n\t" | |
2435 | "mov %c[vmcb](%[svm]), %%"R"ax \n\t" | |
2436 | __ex(SVM_VMLOAD) "\n\t" | |
2437 | __ex(SVM_VMRUN) "\n\t" | |
2438 | __ex(SVM_VMSAVE) "\n\t" | |
2439 | "pop %%"R"ax \n\t" | |
2440 | ||
2441 | /* Save guest registers, load host registers */ | |
2442 | "mov %%"R"bx, %c[rbx](%[svm]) \n\t" | |
2443 | "mov %%"R"cx, %c[rcx](%[svm]) \n\t" | |
2444 | "mov %%"R"dx, %c[rdx](%[svm]) \n\t" | |
2445 | "mov %%"R"si, %c[rsi](%[svm]) \n\t" | |
2446 | "mov %%"R"di, %c[rdi](%[svm]) \n\t" | |
2447 | "mov %%"R"bp, %c[rbp](%[svm]) \n\t" | |
2448 | #ifdef CONFIG_X86_64 | |
2449 | "mov %%r8, %c[r8](%[svm]) \n\t" | |
2450 | "mov %%r9, %c[r9](%[svm]) \n\t" | |
2451 | "mov %%r10, %c[r10](%[svm]) \n\t" | |
2452 | "mov %%r11, %c[r11](%[svm]) \n\t" | |
2453 | "mov %%r12, %c[r12](%[svm]) \n\t" | |
2454 | "mov %%r13, %c[r13](%[svm]) \n\t" | |
2455 | "mov %%r14, %c[r14](%[svm]) \n\t" | |
2456 | "mov %%r15, %c[r15](%[svm]) \n\t" | |
2457 | #endif | |
2458 | "pop %%"R"bp" | |
2459 | : | |
2460 | : [svm]"a"(svm), | |
2461 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), | |
2462 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), | |
2463 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), | |
2464 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), | |
2465 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), | |
2466 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), | |
2467 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) | |
2468 | #ifdef CONFIG_X86_64 | |
2469 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), | |
2470 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), | |
2471 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), | |
2472 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), | |
2473 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), | |
2474 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), | |
2475 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), | |
2476 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) | |
2477 | #endif | |
2478 | : "cc", "memory" | |
2479 | , R"bx", R"cx", R"dx", R"si", R"di" | |
2480 | #ifdef CONFIG_X86_64 | |
2481 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" | |
2482 | #endif | |
2483 | ); | |
2484 | ||
2485 | if ((svm->vmcb->save.dr7 & 0xff)) | |
2486 | load_db_regs(svm->host_db_regs); | |
2487 | ||
2488 | vcpu->arch.cr2 = svm->vmcb->save.cr2; | |
2489 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; | |
2490 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; | |
2491 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; | |
2492 | ||
2493 | write_dr6(svm->host_dr6); | |
2494 | write_dr7(svm->host_dr7); | |
2495 | kvm_write_cr2(svm->host_cr2); | |
2496 | ||
2497 | kvm_load_fs(fs_selector); | |
2498 | kvm_load_gs(gs_selector); | |
2499 | kvm_load_ldt(ldt_selector); | |
2500 | load_host_msrs(vcpu); | |
2501 | ||
2502 | reload_tss(vcpu); | |
2503 | ||
2504 | local_irq_disable(); | |
2505 | ||
2506 | stgi(); | |
2507 | ||
2508 | sync_cr8_to_lapic(vcpu); | |
2509 | ||
2510 | svm->next_rip = 0; | |
2511 | } | |
2512 | ||
2513 | #undef R | |
2514 | ||
2515 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) | |
2516 | { | |
2517 | struct vcpu_svm *svm = to_svm(vcpu); | |
2518 | ||
2519 | if (npt_enabled) { | |
2520 | svm->vmcb->control.nested_cr3 = root; | |
2521 | force_new_asid(vcpu); | |
2522 | return; | |
2523 | } | |
2524 | ||
2525 | svm->vmcb->save.cr3 = root; | |
2526 | force_new_asid(vcpu); | |
2527 | ||
2528 | if (vcpu->fpu_active) { | |
2529 | svm->vmcb->control.intercept_exceptions |= (1 << NM_VECTOR); | |
2530 | svm->vmcb->save.cr0 |= X86_CR0_TS; | |
2531 | vcpu->fpu_active = 0; | |
2532 | } | |
2533 | } | |
2534 | ||
2535 | static int is_disabled(void) | |
2536 | { | |
2537 | u64 vm_cr; | |
2538 | ||
2539 | rdmsrl(MSR_VM_CR, vm_cr); | |
2540 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) | |
2541 | return 1; | |
2542 | ||
2543 | return 0; | |
2544 | } | |
2545 | ||
2546 | static void | |
2547 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
2548 | { | |
2549 | /* | |
2550 | * Patch in the VMMCALL instruction: | |
2551 | */ | |
2552 | hypercall[0] = 0x0f; | |
2553 | hypercall[1] = 0x01; | |
2554 | hypercall[2] = 0xd9; | |
2555 | } | |
2556 | ||
2557 | static void svm_check_processor_compat(void *rtn) | |
2558 | { | |
2559 | *(int *)rtn = 0; | |
2560 | } | |
2561 | ||
2562 | static bool svm_cpu_has_accelerated_tpr(void) | |
2563 | { | |
2564 | return false; | |
2565 | } | |
2566 | ||
2567 | static int get_npt_level(void) | |
2568 | { | |
2569 | #ifdef CONFIG_X86_64 | |
2570 | return PT64_ROOT_LEVEL; | |
2571 | #else | |
2572 | return PT32E_ROOT_LEVEL; | |
2573 | #endif | |
2574 | } | |
2575 | ||
2576 | static int svm_get_mt_mask_shift(void) | |
2577 | { | |
2578 | return 0; | |
2579 | } | |
2580 | ||
2581 | static struct kvm_x86_ops svm_x86_ops = { | |
2582 | .cpu_has_kvm_support = has_svm, | |
2583 | .disabled_by_bios = is_disabled, | |
2584 | .hardware_setup = svm_hardware_setup, | |
2585 | .hardware_unsetup = svm_hardware_unsetup, | |
2586 | .check_processor_compatibility = svm_check_processor_compat, | |
2587 | .hardware_enable = svm_hardware_enable, | |
2588 | .hardware_disable = svm_hardware_disable, | |
2589 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, | |
2590 | ||
2591 | .vcpu_create = svm_create_vcpu, | |
2592 | .vcpu_free = svm_free_vcpu, | |
2593 | .vcpu_reset = svm_vcpu_reset, | |
2594 | ||
2595 | .prepare_guest_switch = svm_prepare_guest_switch, | |
2596 | .vcpu_load = svm_vcpu_load, | |
2597 | .vcpu_put = svm_vcpu_put, | |
2598 | ||
2599 | .set_guest_debug = svm_guest_debug, | |
2600 | .get_msr = svm_get_msr, | |
2601 | .set_msr = svm_set_msr, | |
2602 | .get_segment_base = svm_get_segment_base, | |
2603 | .get_segment = svm_get_segment, | |
2604 | .set_segment = svm_set_segment, | |
2605 | .get_cpl = svm_get_cpl, | |
2606 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, | |
2607 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, | |
2608 | .set_cr0 = svm_set_cr0, | |
2609 | .set_cr3 = svm_set_cr3, | |
2610 | .set_cr4 = svm_set_cr4, | |
2611 | .set_efer = svm_set_efer, | |
2612 | .get_idt = svm_get_idt, | |
2613 | .set_idt = svm_set_idt, | |
2614 | .get_gdt = svm_get_gdt, | |
2615 | .set_gdt = svm_set_gdt, | |
2616 | .get_dr = svm_get_dr, | |
2617 | .set_dr = svm_set_dr, | |
2618 | .get_rflags = svm_get_rflags, | |
2619 | .set_rflags = svm_set_rflags, | |
2620 | ||
2621 | .tlb_flush = svm_flush_tlb, | |
2622 | ||
2623 | .run = svm_vcpu_run, | |
2624 | .handle_exit = handle_exit, | |
2625 | .skip_emulated_instruction = skip_emulated_instruction, | |
2626 | .patch_hypercall = svm_patch_hypercall, | |
2627 | .get_irq = svm_get_irq, | |
2628 | .set_irq = svm_set_irq, | |
2629 | .queue_exception = svm_queue_exception, | |
2630 | .exception_injected = svm_exception_injected, | |
2631 | .inject_pending_irq = svm_intr_assist, | |
2632 | .inject_pending_vectors = do_interrupt_requests, | |
2633 | ||
2634 | .set_tss_addr = svm_set_tss_addr, | |
2635 | .get_tdp_level = get_npt_level, | |
2636 | .get_mt_mask_shift = svm_get_mt_mask_shift, | |
2637 | }; | |
2638 | ||
2639 | static int __init svm_init(void) | |
2640 | { | |
2641 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), | |
2642 | THIS_MODULE); | |
2643 | } | |
2644 | ||
2645 | static void __exit svm_exit(void) | |
2646 | { | |
2647 | kvm_exit(); | |
2648 | } | |
2649 | ||
2650 | module_init(svm_init) | |
2651 | module_exit(svm_exit) |