]>
Commit | Line | Data |
---|---|---|
6aa8b732 AK |
1 | /* |
2 | * Kernel-based Virtual Machine driver for Linux | |
3 | * | |
4 | * AMD SVM support | |
5 | * | |
6 | * Copyright (C) 2006 Qumranet, Inc. | |
9611c187 | 7 | * Copyright 2010 Red Hat, Inc. and/or its affiliates. |
6aa8b732 AK |
8 | * |
9 | * Authors: | |
10 | * Yaniv Kamay <[email protected]> | |
11 | * Avi Kivity <[email protected]> | |
12 | * | |
13 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
14 | * the COPYING file in the top-level directory. | |
15 | * | |
16 | */ | |
edf88417 AK |
17 | #include <linux/kvm_host.h> |
18 | ||
85f455f7 | 19 | #include "irq.h" |
1d737c8a | 20 | #include "mmu.h" |
5fdbf976 | 21 | #include "kvm_cache_regs.h" |
fe4c7b19 | 22 | #include "x86.h" |
e495606d | 23 | |
6aa8b732 | 24 | #include <linux/module.h> |
9d8f549d | 25 | #include <linux/kernel.h> |
6aa8b732 AK |
26 | #include <linux/vmalloc.h> |
27 | #include <linux/highmem.h> | |
e8edc6e0 | 28 | #include <linux/sched.h> |
229456fc | 29 | #include <linux/ftrace_event.h> |
5a0e3ad6 | 30 | #include <linux/slab.h> |
6aa8b732 | 31 | |
1018faa6 | 32 | #include <asm/perf_event.h> |
67ec6607 | 33 | #include <asm/tlbflush.h> |
e495606d | 34 | #include <asm/desc.h> |
631bc487 | 35 | #include <asm/kvm_para.h> |
6aa8b732 | 36 | |
63d1142f | 37 | #include <asm/virtext.h> |
229456fc | 38 | #include "trace.h" |
63d1142f | 39 | |
4ecac3fd AK |
40 | #define __ex(x) __kvm_handle_fault_on_reboot(x) |
41 | ||
6aa8b732 AK |
42 | MODULE_AUTHOR("Qumranet"); |
43 | MODULE_LICENSE("GPL"); | |
44 | ||
45 | #define IOPM_ALLOC_ORDER 2 | |
46 | #define MSRPM_ALLOC_ORDER 1 | |
47 | ||
6aa8b732 AK |
48 | #define SEG_TYPE_LDT 2 |
49 | #define SEG_TYPE_BUSY_TSS16 3 | |
50 | ||
6bc31bdc AP |
51 | #define SVM_FEATURE_NPT (1 << 0) |
52 | #define SVM_FEATURE_LBRV (1 << 1) | |
53 | #define SVM_FEATURE_SVML (1 << 2) | |
54 | #define SVM_FEATURE_NRIP (1 << 3) | |
ddce97aa AP |
55 | #define SVM_FEATURE_TSC_RATE (1 << 4) |
56 | #define SVM_FEATURE_VMCB_CLEAN (1 << 5) | |
57 | #define SVM_FEATURE_FLUSH_ASID (1 << 6) | |
58 | #define SVM_FEATURE_DECODE_ASSIST (1 << 7) | |
6bc31bdc | 59 | #define SVM_FEATURE_PAUSE_FILTER (1 << 10) |
80b7706e | 60 | |
410e4d57 JR |
61 | #define NESTED_EXIT_HOST 0 /* Exit handled on host level */ |
62 | #define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */ | |
63 | #define NESTED_EXIT_CONTINUE 2 /* Further checks needed */ | |
64 | ||
24e09cbf JR |
65 | #define DEBUGCTL_RESERVED_BITS (~(0x3fULL)) |
66 | ||
fbc0db76 | 67 | #define TSC_RATIO_RSVD 0xffffff0000000000ULL |
92a1f12d JR |
68 | #define TSC_RATIO_MIN 0x0000000000000001ULL |
69 | #define TSC_RATIO_MAX 0x000000ffffffffffULL | |
fbc0db76 | 70 | |
67ec6607 JR |
71 | static bool erratum_383_found __read_mostly; |
72 | ||
6c8166a7 AK |
73 | static const u32 host_save_user_msrs[] = { |
74 | #ifdef CONFIG_X86_64 | |
75 | MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE, | |
76 | MSR_FS_BASE, | |
77 | #endif | |
78 | MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP, | |
79 | }; | |
80 | ||
81 | #define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs) | |
82 | ||
83 | struct kvm_vcpu; | |
84 | ||
e6aa9abd JR |
85 | struct nested_state { |
86 | struct vmcb *hsave; | |
87 | u64 hsave_msr; | |
4a810181 | 88 | u64 vm_cr_msr; |
e6aa9abd JR |
89 | u64 vmcb; |
90 | ||
91 | /* These are the merged vectors */ | |
92 | u32 *msrpm; | |
93 | ||
94 | /* gpa pointers to the real vectors */ | |
95 | u64 vmcb_msrpm; | |
ce2ac085 | 96 | u64 vmcb_iopm; |
aad42c64 | 97 | |
cd3ff653 JR |
98 | /* A VMEXIT is required but not yet emulated */ |
99 | bool exit_required; | |
100 | ||
aad42c64 | 101 | /* cache for intercepts of the guest */ |
4ee546b4 | 102 | u32 intercept_cr; |
3aed041a | 103 | u32 intercept_dr; |
aad42c64 JR |
104 | u32 intercept_exceptions; |
105 | u64 intercept; | |
106 | ||
5bd2edc3 JR |
107 | /* Nested Paging related state */ |
108 | u64 nested_cr3; | |
e6aa9abd JR |
109 | }; |
110 | ||
323c3d80 JR |
111 | #define MSRPM_OFFSETS 16 |
112 | static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly; | |
113 | ||
2b036c6b BO |
114 | /* |
115 | * Set osvw_len to higher value when updated Revision Guides | |
116 | * are published and we know what the new status bits are | |
117 | */ | |
118 | static uint64_t osvw_len = 4, osvw_status; | |
119 | ||
6c8166a7 AK |
120 | struct vcpu_svm { |
121 | struct kvm_vcpu vcpu; | |
122 | struct vmcb *vmcb; | |
123 | unsigned long vmcb_pa; | |
124 | struct svm_cpu_data *svm_data; | |
125 | uint64_t asid_generation; | |
126 | uint64_t sysenter_esp; | |
127 | uint64_t sysenter_eip; | |
128 | ||
129 | u64 next_rip; | |
130 | ||
131 | u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS]; | |
afe9e66f | 132 | struct { |
dacccfdd AK |
133 | u16 fs; |
134 | u16 gs; | |
135 | u16 ldt; | |
afe9e66f AK |
136 | u64 gs_base; |
137 | } host; | |
6c8166a7 AK |
138 | |
139 | u32 *msrpm; | |
6c8166a7 | 140 | |
bd3d1ec3 AK |
141 | ulong nmi_iret_rip; |
142 | ||
e6aa9abd | 143 | struct nested_state nested; |
6be7d306 JK |
144 | |
145 | bool nmi_singlestep; | |
66b7138f JK |
146 | |
147 | unsigned int3_injected; | |
148 | unsigned long int3_rip; | |
631bc487 | 149 | u32 apf_reason; |
fbc0db76 JR |
150 | |
151 | u64 tsc_ratio; | |
6c8166a7 AK |
152 | }; |
153 | ||
fbc0db76 JR |
154 | static DEFINE_PER_CPU(u64, current_tsc_ratio); |
155 | #define TSC_RATIO_DEFAULT 0x0100000000ULL | |
156 | ||
455716fa JR |
157 | #define MSR_INVALID 0xffffffffU |
158 | ||
ac72a9b7 JR |
159 | static struct svm_direct_access_msrs { |
160 | u32 index; /* Index of the MSR */ | |
161 | bool always; /* True if intercept is always on */ | |
162 | } direct_access_msrs[] = { | |
8c06585d | 163 | { .index = MSR_STAR, .always = true }, |
ac72a9b7 JR |
164 | { .index = MSR_IA32_SYSENTER_CS, .always = true }, |
165 | #ifdef CONFIG_X86_64 | |
166 | { .index = MSR_GS_BASE, .always = true }, | |
167 | { .index = MSR_FS_BASE, .always = true }, | |
168 | { .index = MSR_KERNEL_GS_BASE, .always = true }, | |
169 | { .index = MSR_LSTAR, .always = true }, | |
170 | { .index = MSR_CSTAR, .always = true }, | |
171 | { .index = MSR_SYSCALL_MASK, .always = true }, | |
172 | #endif | |
173 | { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false }, | |
174 | { .index = MSR_IA32_LASTBRANCHTOIP, .always = false }, | |
175 | { .index = MSR_IA32_LASTINTFROMIP, .always = false }, | |
176 | { .index = MSR_IA32_LASTINTTOIP, .always = false }, | |
177 | { .index = MSR_INVALID, .always = false }, | |
6c8166a7 AK |
178 | }; |
179 | ||
709ddebf JR |
180 | /* enable NPT for AMD64 and X86 with PAE */ |
181 | #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE) | |
182 | static bool npt_enabled = true; | |
183 | #else | |
e0231715 | 184 | static bool npt_enabled; |
709ddebf | 185 | #endif |
6c7dac72 | 186 | |
e2358851 DB |
187 | /* allow nested paging (virtualized MMU) for all guests */ |
188 | static int npt = true; | |
6c7dac72 | 189 | module_param(npt, int, S_IRUGO); |
e3da3acd | 190 | |
e2358851 DB |
191 | /* allow nested virtualization in KVM/SVM */ |
192 | static int nested = true; | |
236de055 AG |
193 | module_param(nested, int, S_IRUGO); |
194 | ||
44874f84 | 195 | static void svm_flush_tlb(struct kvm_vcpu *vcpu); |
a5c3832d | 196 | static void svm_complete_interrupts(struct vcpu_svm *svm); |
04d2cc77 | 197 | |
410e4d57 | 198 | static int nested_svm_exit_handled(struct vcpu_svm *svm); |
b8e88bc8 | 199 | static int nested_svm_intercept(struct vcpu_svm *svm); |
cf74a78b | 200 | static int nested_svm_vmexit(struct vcpu_svm *svm); |
cf74a78b AG |
201 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
202 | bool has_error_code, u32 error_code); | |
92a1f12d | 203 | static u64 __scale_tsc(u64 ratio, u64 tsc); |
cf74a78b | 204 | |
8d28fec4 | 205 | enum { |
116a0a23 JR |
206 | VMCB_INTERCEPTS, /* Intercept vectors, TSC offset, |
207 | pause filter count */ | |
f56838e4 | 208 | VMCB_PERM_MAP, /* IOPM Base and MSRPM Base */ |
d48086d1 | 209 | VMCB_ASID, /* ASID */ |
decdbf6a | 210 | VMCB_INTR, /* int_ctl, int_vector */ |
b2747166 | 211 | VMCB_NPT, /* npt_en, nCR3, gPAT */ |
dcca1a65 | 212 | VMCB_CR, /* CR0, CR3, CR4, EFER */ |
72214b96 | 213 | VMCB_DR, /* DR6, DR7 */ |
17a703cb | 214 | VMCB_DT, /* GDT, IDT */ |
060d0c9a | 215 | VMCB_SEG, /* CS, DS, SS, ES, CPL */ |
0574dec0 | 216 | VMCB_CR2, /* CR2 only */ |
b53ba3f9 | 217 | VMCB_LBR, /* DBGCTL, BR_FROM, BR_TO, LAST_EX_FROM, LAST_EX_TO */ |
8d28fec4 RJ |
218 | VMCB_DIRTY_MAX, |
219 | }; | |
220 | ||
0574dec0 JR |
221 | /* TPR and CR2 are always written before VMRUN */ |
222 | #define VMCB_ALWAYS_DIRTY_MASK ((1U << VMCB_INTR) | (1U << VMCB_CR2)) | |
8d28fec4 RJ |
223 | |
224 | static inline void mark_all_dirty(struct vmcb *vmcb) | |
225 | { | |
226 | vmcb->control.clean = 0; | |
227 | } | |
228 | ||
229 | static inline void mark_all_clean(struct vmcb *vmcb) | |
230 | { | |
231 | vmcb->control.clean = ((1 << VMCB_DIRTY_MAX) - 1) | |
232 | & ~VMCB_ALWAYS_DIRTY_MASK; | |
233 | } | |
234 | ||
235 | static inline void mark_dirty(struct vmcb *vmcb, int bit) | |
236 | { | |
237 | vmcb->control.clean &= ~(1 << bit); | |
238 | } | |
239 | ||
a2fa3e9f GH |
240 | static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu) |
241 | { | |
fb3f0f51 | 242 | return container_of(vcpu, struct vcpu_svm, vcpu); |
a2fa3e9f GH |
243 | } |
244 | ||
384c6368 JR |
245 | static void recalc_intercepts(struct vcpu_svm *svm) |
246 | { | |
247 | struct vmcb_control_area *c, *h; | |
248 | struct nested_state *g; | |
249 | ||
116a0a23 JR |
250 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
251 | ||
384c6368 JR |
252 | if (!is_guest_mode(&svm->vcpu)) |
253 | return; | |
254 | ||
255 | c = &svm->vmcb->control; | |
256 | h = &svm->nested.hsave->control; | |
257 | g = &svm->nested; | |
258 | ||
4ee546b4 | 259 | c->intercept_cr = h->intercept_cr | g->intercept_cr; |
3aed041a | 260 | c->intercept_dr = h->intercept_dr | g->intercept_dr; |
384c6368 JR |
261 | c->intercept_exceptions = h->intercept_exceptions | g->intercept_exceptions; |
262 | c->intercept = h->intercept | g->intercept; | |
263 | } | |
264 | ||
4ee546b4 RJ |
265 | static inline struct vmcb *get_host_vmcb(struct vcpu_svm *svm) |
266 | { | |
267 | if (is_guest_mode(&svm->vcpu)) | |
268 | return svm->nested.hsave; | |
269 | else | |
270 | return svm->vmcb; | |
271 | } | |
272 | ||
273 | static inline void set_cr_intercept(struct vcpu_svm *svm, int bit) | |
274 | { | |
275 | struct vmcb *vmcb = get_host_vmcb(svm); | |
276 | ||
277 | vmcb->control.intercept_cr |= (1U << bit); | |
278 | ||
279 | recalc_intercepts(svm); | |
280 | } | |
281 | ||
282 | static inline void clr_cr_intercept(struct vcpu_svm *svm, int bit) | |
283 | { | |
284 | struct vmcb *vmcb = get_host_vmcb(svm); | |
285 | ||
286 | vmcb->control.intercept_cr &= ~(1U << bit); | |
287 | ||
288 | recalc_intercepts(svm); | |
289 | } | |
290 | ||
291 | static inline bool is_cr_intercept(struct vcpu_svm *svm, int bit) | |
292 | { | |
293 | struct vmcb *vmcb = get_host_vmcb(svm); | |
294 | ||
295 | return vmcb->control.intercept_cr & (1U << bit); | |
296 | } | |
297 | ||
3aed041a JR |
298 | static inline void set_dr_intercept(struct vcpu_svm *svm, int bit) |
299 | { | |
300 | struct vmcb *vmcb = get_host_vmcb(svm); | |
301 | ||
302 | vmcb->control.intercept_dr |= (1U << bit); | |
303 | ||
304 | recalc_intercepts(svm); | |
305 | } | |
306 | ||
307 | static inline void clr_dr_intercept(struct vcpu_svm *svm, int bit) | |
308 | { | |
309 | struct vmcb *vmcb = get_host_vmcb(svm); | |
310 | ||
311 | vmcb->control.intercept_dr &= ~(1U << bit); | |
312 | ||
313 | recalc_intercepts(svm); | |
314 | } | |
315 | ||
18c918c5 JR |
316 | static inline void set_exception_intercept(struct vcpu_svm *svm, int bit) |
317 | { | |
318 | struct vmcb *vmcb = get_host_vmcb(svm); | |
319 | ||
320 | vmcb->control.intercept_exceptions |= (1U << bit); | |
321 | ||
322 | recalc_intercepts(svm); | |
323 | } | |
324 | ||
325 | static inline void clr_exception_intercept(struct vcpu_svm *svm, int bit) | |
326 | { | |
327 | struct vmcb *vmcb = get_host_vmcb(svm); | |
328 | ||
329 | vmcb->control.intercept_exceptions &= ~(1U << bit); | |
330 | ||
331 | recalc_intercepts(svm); | |
332 | } | |
333 | ||
8a05a1b8 JR |
334 | static inline void set_intercept(struct vcpu_svm *svm, int bit) |
335 | { | |
336 | struct vmcb *vmcb = get_host_vmcb(svm); | |
337 | ||
338 | vmcb->control.intercept |= (1ULL << bit); | |
339 | ||
340 | recalc_intercepts(svm); | |
341 | } | |
342 | ||
343 | static inline void clr_intercept(struct vcpu_svm *svm, int bit) | |
344 | { | |
345 | struct vmcb *vmcb = get_host_vmcb(svm); | |
346 | ||
347 | vmcb->control.intercept &= ~(1ULL << bit); | |
348 | ||
349 | recalc_intercepts(svm); | |
350 | } | |
351 | ||
2af9194d JR |
352 | static inline void enable_gif(struct vcpu_svm *svm) |
353 | { | |
354 | svm->vcpu.arch.hflags |= HF_GIF_MASK; | |
355 | } | |
356 | ||
357 | static inline void disable_gif(struct vcpu_svm *svm) | |
358 | { | |
359 | svm->vcpu.arch.hflags &= ~HF_GIF_MASK; | |
360 | } | |
361 | ||
362 | static inline bool gif_set(struct vcpu_svm *svm) | |
363 | { | |
364 | return !!(svm->vcpu.arch.hflags & HF_GIF_MASK); | |
365 | } | |
366 | ||
4866d5e3 | 367 | static unsigned long iopm_base; |
6aa8b732 AK |
368 | |
369 | struct kvm_ldttss_desc { | |
370 | u16 limit0; | |
371 | u16 base0; | |
e0231715 JR |
372 | unsigned base1:8, type:5, dpl:2, p:1; |
373 | unsigned limit1:4, zero0:3, g:1, base2:8; | |
6aa8b732 AK |
374 | u32 base3; |
375 | u32 zero1; | |
376 | } __attribute__((packed)); | |
377 | ||
378 | struct svm_cpu_data { | |
379 | int cpu; | |
380 | ||
5008fdf5 AK |
381 | u64 asid_generation; |
382 | u32 max_asid; | |
383 | u32 next_asid; | |
6aa8b732 AK |
384 | struct kvm_ldttss_desc *tss_desc; |
385 | ||
386 | struct page *save_area; | |
387 | }; | |
388 | ||
389 | static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data); | |
390 | ||
391 | struct svm_init_data { | |
392 | int cpu; | |
393 | int r; | |
394 | }; | |
395 | ||
396 | static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000}; | |
397 | ||
9d8f549d | 398 | #define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges) |
6aa8b732 AK |
399 | #define MSRS_RANGE_SIZE 2048 |
400 | #define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2) | |
401 | ||
455716fa JR |
402 | static u32 svm_msrpm_offset(u32 msr) |
403 | { | |
404 | u32 offset; | |
405 | int i; | |
406 | ||
407 | for (i = 0; i < NUM_MSR_MAPS; i++) { | |
408 | if (msr < msrpm_ranges[i] || | |
409 | msr >= msrpm_ranges[i] + MSRS_IN_RANGE) | |
410 | continue; | |
411 | ||
412 | offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */ | |
413 | offset += (i * MSRS_RANGE_SIZE); /* add range offset */ | |
414 | ||
415 | /* Now we have the u8 offset - but need the u32 offset */ | |
416 | return offset / 4; | |
417 | } | |
418 | ||
419 | /* MSR not in any range */ | |
420 | return MSR_INVALID; | |
421 | } | |
422 | ||
6aa8b732 AK |
423 | #define MAX_INST_SIZE 15 |
424 | ||
6aa8b732 AK |
425 | static inline void clgi(void) |
426 | { | |
4ecac3fd | 427 | asm volatile (__ex(SVM_CLGI)); |
6aa8b732 AK |
428 | } |
429 | ||
430 | static inline void stgi(void) | |
431 | { | |
4ecac3fd | 432 | asm volatile (__ex(SVM_STGI)); |
6aa8b732 AK |
433 | } |
434 | ||
435 | static inline void invlpga(unsigned long addr, u32 asid) | |
436 | { | |
e0231715 | 437 | asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid)); |
6aa8b732 AK |
438 | } |
439 | ||
4b16184c JR |
440 | static int get_npt_level(void) |
441 | { | |
442 | #ifdef CONFIG_X86_64 | |
443 | return PT64_ROOT_LEVEL; | |
444 | #else | |
445 | return PT32E_ROOT_LEVEL; | |
446 | #endif | |
447 | } | |
448 | ||
6aa8b732 AK |
449 | static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer) |
450 | { | |
6dc696d4 | 451 | vcpu->arch.efer = efer; |
709ddebf | 452 | if (!npt_enabled && !(efer & EFER_LMA)) |
2b5203ee | 453 | efer &= ~EFER_LME; |
6aa8b732 | 454 | |
9962d032 | 455 | to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME; |
dcca1a65 | 456 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
6aa8b732 AK |
457 | } |
458 | ||
6aa8b732 AK |
459 | static int is_external_interrupt(u32 info) |
460 | { | |
461 | info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID; | |
462 | return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR); | |
463 | } | |
464 | ||
2809f5d2 GC |
465 | static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) |
466 | { | |
467 | struct vcpu_svm *svm = to_svm(vcpu); | |
468 | u32 ret = 0; | |
469 | ||
470 | if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) | |
48005f64 | 471 | ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS; |
2809f5d2 GC |
472 | return ret & mask; |
473 | } | |
474 | ||
475 | static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask) | |
476 | { | |
477 | struct vcpu_svm *svm = to_svm(vcpu); | |
478 | ||
479 | if (mask == 0) | |
480 | svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK; | |
481 | else | |
482 | svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK; | |
483 | ||
484 | } | |
485 | ||
6aa8b732 AK |
486 | static void skip_emulated_instruction(struct kvm_vcpu *vcpu) |
487 | { | |
a2fa3e9f GH |
488 | struct vcpu_svm *svm = to_svm(vcpu); |
489 | ||
6bc31bdc AP |
490 | if (svm->vmcb->control.next_rip != 0) |
491 | svm->next_rip = svm->vmcb->control.next_rip; | |
492 | ||
a2fa3e9f | 493 | if (!svm->next_rip) { |
51d8b661 | 494 | if (emulate_instruction(vcpu, EMULTYPE_SKIP) != |
f629cf84 GN |
495 | EMULATE_DONE) |
496 | printk(KERN_DEBUG "%s: NOP\n", __func__); | |
6aa8b732 AK |
497 | return; |
498 | } | |
5fdbf976 MT |
499 | if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE) |
500 | printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n", | |
501 | __func__, kvm_rip_read(vcpu), svm->next_rip); | |
6aa8b732 | 502 | |
5fdbf976 | 503 | kvm_rip_write(vcpu, svm->next_rip); |
2809f5d2 | 504 | svm_set_interrupt_shadow(vcpu, 0); |
6aa8b732 AK |
505 | } |
506 | ||
116a4752 | 507 | static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr, |
ce7ddec4 JR |
508 | bool has_error_code, u32 error_code, |
509 | bool reinject) | |
116a4752 JK |
510 | { |
511 | struct vcpu_svm *svm = to_svm(vcpu); | |
512 | ||
e0231715 JR |
513 | /* |
514 | * If we are within a nested VM we'd better #VMEXIT and let the guest | |
515 | * handle the exception | |
516 | */ | |
ce7ddec4 JR |
517 | if (!reinject && |
518 | nested_svm_check_exception(svm, nr, has_error_code, error_code)) | |
116a4752 JK |
519 | return; |
520 | ||
2a6b20b8 | 521 | if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) { |
66b7138f JK |
522 | unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu); |
523 | ||
524 | /* | |
525 | * For guest debugging where we have to reinject #BP if some | |
526 | * INT3 is guest-owned: | |
527 | * Emulate nRIP by moving RIP forward. Will fail if injection | |
528 | * raises a fault that is not intercepted. Still better than | |
529 | * failing in all cases. | |
530 | */ | |
531 | skip_emulated_instruction(&svm->vcpu); | |
532 | rip = kvm_rip_read(&svm->vcpu); | |
533 | svm->int3_rip = rip + svm->vmcb->save.cs.base; | |
534 | svm->int3_injected = rip - old_rip; | |
535 | } | |
536 | ||
116a4752 JK |
537 | svm->vmcb->control.event_inj = nr |
538 | | SVM_EVTINJ_VALID | |
539 | | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0) | |
540 | | SVM_EVTINJ_TYPE_EXEPT; | |
541 | svm->vmcb->control.event_inj_err = error_code; | |
542 | } | |
543 | ||
67ec6607 JR |
544 | static void svm_init_erratum_383(void) |
545 | { | |
546 | u32 low, high; | |
547 | int err; | |
548 | u64 val; | |
549 | ||
1be85a6d | 550 | if (!cpu_has_amd_erratum(amd_erratum_383)) |
67ec6607 JR |
551 | return; |
552 | ||
553 | /* Use _safe variants to not break nested virtualization */ | |
554 | val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err); | |
555 | if (err) | |
556 | return; | |
557 | ||
558 | val |= (1ULL << 47); | |
559 | ||
560 | low = lower_32_bits(val); | |
561 | high = upper_32_bits(val); | |
562 | ||
563 | native_write_msr_safe(MSR_AMD64_DC_CFG, low, high); | |
564 | ||
565 | erratum_383_found = true; | |
566 | } | |
567 | ||
2b036c6b BO |
568 | static void svm_init_osvw(struct kvm_vcpu *vcpu) |
569 | { | |
570 | /* | |
571 | * Guests should see errata 400 and 415 as fixed (assuming that | |
572 | * HLT and IO instructions are intercepted). | |
573 | */ | |
574 | vcpu->arch.osvw.length = (osvw_len >= 3) ? (osvw_len) : 3; | |
575 | vcpu->arch.osvw.status = osvw_status & ~(6ULL); | |
576 | ||
577 | /* | |
578 | * By increasing VCPU's osvw.length to 3 we are telling the guest that | |
579 | * all osvw.status bits inside that length, including bit 0 (which is | |
580 | * reserved for erratum 298), are valid. However, if host processor's | |
581 | * osvw_len is 0 then osvw_status[0] carries no information. We need to | |
582 | * be conservative here and therefore we tell the guest that erratum 298 | |
583 | * is present (because we really don't know). | |
584 | */ | |
585 | if (osvw_len == 0 && boot_cpu_data.x86 == 0x10) | |
586 | vcpu->arch.osvw.status |= 1; | |
587 | } | |
588 | ||
6aa8b732 AK |
589 | static int has_svm(void) |
590 | { | |
63d1142f | 591 | const char *msg; |
6aa8b732 | 592 | |
63d1142f | 593 | if (!cpu_has_svm(&msg)) { |
ff81ff10 | 594 | printk(KERN_INFO "has_svm: %s\n", msg); |
6aa8b732 AK |
595 | return 0; |
596 | } | |
597 | ||
6aa8b732 AK |
598 | return 1; |
599 | } | |
600 | ||
601 | static void svm_hardware_disable(void *garbage) | |
602 | { | |
fbc0db76 JR |
603 | /* Make sure we clean up behind us */ |
604 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) | |
605 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); | |
606 | ||
2c8dceeb | 607 | cpu_svm_disable(); |
1018faa6 JR |
608 | |
609 | amd_pmu_disable_virt(); | |
6aa8b732 AK |
610 | } |
611 | ||
10474ae8 | 612 | static int svm_hardware_enable(void *garbage) |
6aa8b732 AK |
613 | { |
614 | ||
0fe1e009 | 615 | struct svm_cpu_data *sd; |
6aa8b732 | 616 | uint64_t efer; |
89a27f4d | 617 | struct desc_ptr gdt_descr; |
6aa8b732 AK |
618 | struct desc_struct *gdt; |
619 | int me = raw_smp_processor_id(); | |
620 | ||
10474ae8 AG |
621 | rdmsrl(MSR_EFER, efer); |
622 | if (efer & EFER_SVME) | |
623 | return -EBUSY; | |
624 | ||
6aa8b732 | 625 | if (!has_svm()) { |
e6732a5a ZA |
626 | printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n", |
627 | me); | |
10474ae8 | 628 | return -EINVAL; |
6aa8b732 | 629 | } |
0fe1e009 | 630 | sd = per_cpu(svm_data, me); |
6aa8b732 | 631 | |
0fe1e009 | 632 | if (!sd) { |
e6732a5a | 633 | printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n", |
6aa8b732 | 634 | me); |
10474ae8 | 635 | return -EINVAL; |
6aa8b732 AK |
636 | } |
637 | ||
0fe1e009 TH |
638 | sd->asid_generation = 1; |
639 | sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1; | |
640 | sd->next_asid = sd->max_asid + 1; | |
6aa8b732 | 641 | |
d6ab1ed4 | 642 | native_store_gdt(&gdt_descr); |
89a27f4d | 643 | gdt = (struct desc_struct *)gdt_descr.address; |
0fe1e009 | 644 | sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS); |
6aa8b732 | 645 | |
9962d032 | 646 | wrmsrl(MSR_EFER, efer | EFER_SVME); |
6aa8b732 | 647 | |
d0316554 | 648 | wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT); |
10474ae8 | 649 | |
fbc0db76 JR |
650 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
651 | wrmsrl(MSR_AMD64_TSC_RATIO, TSC_RATIO_DEFAULT); | |
652 | __get_cpu_var(current_tsc_ratio) = TSC_RATIO_DEFAULT; | |
653 | } | |
654 | ||
2b036c6b BO |
655 | |
656 | /* | |
657 | * Get OSVW bits. | |
658 | * | |
659 | * Note that it is possible to have a system with mixed processor | |
660 | * revisions and therefore different OSVW bits. If bits are not the same | |
661 | * on different processors then choose the worst case (i.e. if erratum | |
662 | * is present on one processor and not on another then assume that the | |
663 | * erratum is present everywhere). | |
664 | */ | |
665 | if (cpu_has(&boot_cpu_data, X86_FEATURE_OSVW)) { | |
666 | uint64_t len, status = 0; | |
667 | int err; | |
668 | ||
669 | len = native_read_msr_safe(MSR_AMD64_OSVW_ID_LENGTH, &err); | |
670 | if (!err) | |
671 | status = native_read_msr_safe(MSR_AMD64_OSVW_STATUS, | |
672 | &err); | |
673 | ||
674 | if (err) | |
675 | osvw_status = osvw_len = 0; | |
676 | else { | |
677 | if (len < osvw_len) | |
678 | osvw_len = len; | |
679 | osvw_status |= status; | |
680 | osvw_status &= (1ULL << osvw_len) - 1; | |
681 | } | |
682 | } else | |
683 | osvw_status = osvw_len = 0; | |
684 | ||
67ec6607 JR |
685 | svm_init_erratum_383(); |
686 | ||
1018faa6 JR |
687 | amd_pmu_enable_virt(); |
688 | ||
10474ae8 | 689 | return 0; |
6aa8b732 AK |
690 | } |
691 | ||
0da1db75 JR |
692 | static void svm_cpu_uninit(int cpu) |
693 | { | |
0fe1e009 | 694 | struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id()); |
0da1db75 | 695 | |
0fe1e009 | 696 | if (!sd) |
0da1db75 JR |
697 | return; |
698 | ||
699 | per_cpu(svm_data, raw_smp_processor_id()) = NULL; | |
0fe1e009 TH |
700 | __free_page(sd->save_area); |
701 | kfree(sd); | |
0da1db75 JR |
702 | } |
703 | ||
6aa8b732 AK |
704 | static int svm_cpu_init(int cpu) |
705 | { | |
0fe1e009 | 706 | struct svm_cpu_data *sd; |
6aa8b732 AK |
707 | int r; |
708 | ||
0fe1e009 TH |
709 | sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL); |
710 | if (!sd) | |
6aa8b732 | 711 | return -ENOMEM; |
0fe1e009 TH |
712 | sd->cpu = cpu; |
713 | sd->save_area = alloc_page(GFP_KERNEL); | |
6aa8b732 | 714 | r = -ENOMEM; |
0fe1e009 | 715 | if (!sd->save_area) |
6aa8b732 AK |
716 | goto err_1; |
717 | ||
0fe1e009 | 718 | per_cpu(svm_data, cpu) = sd; |
6aa8b732 AK |
719 | |
720 | return 0; | |
721 | ||
722 | err_1: | |
0fe1e009 | 723 | kfree(sd); |
6aa8b732 AK |
724 | return r; |
725 | ||
726 | } | |
727 | ||
ac72a9b7 JR |
728 | static bool valid_msr_intercept(u32 index) |
729 | { | |
730 | int i; | |
731 | ||
732 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) | |
733 | if (direct_access_msrs[i].index == index) | |
734 | return true; | |
735 | ||
736 | return false; | |
737 | } | |
738 | ||
bfc733a7 RR |
739 | static void set_msr_interception(u32 *msrpm, unsigned msr, |
740 | int read, int write) | |
6aa8b732 | 741 | { |
455716fa JR |
742 | u8 bit_read, bit_write; |
743 | unsigned long tmp; | |
744 | u32 offset; | |
6aa8b732 | 745 | |
ac72a9b7 JR |
746 | /* |
747 | * If this warning triggers extend the direct_access_msrs list at the | |
748 | * beginning of the file | |
749 | */ | |
750 | WARN_ON(!valid_msr_intercept(msr)); | |
751 | ||
455716fa JR |
752 | offset = svm_msrpm_offset(msr); |
753 | bit_read = 2 * (msr & 0x0f); | |
754 | bit_write = 2 * (msr & 0x0f) + 1; | |
755 | tmp = msrpm[offset]; | |
756 | ||
757 | BUG_ON(offset == MSR_INVALID); | |
758 | ||
759 | read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp); | |
760 | write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp); | |
761 | ||
762 | msrpm[offset] = tmp; | |
6aa8b732 AK |
763 | } |
764 | ||
f65c229c | 765 | static void svm_vcpu_init_msrpm(u32 *msrpm) |
6aa8b732 AK |
766 | { |
767 | int i; | |
768 | ||
f65c229c JR |
769 | memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER)); |
770 | ||
ac72a9b7 JR |
771 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { |
772 | if (!direct_access_msrs[i].always) | |
773 | continue; | |
774 | ||
775 | set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1); | |
776 | } | |
f65c229c JR |
777 | } |
778 | ||
323c3d80 JR |
779 | static void add_msr_offset(u32 offset) |
780 | { | |
781 | int i; | |
782 | ||
783 | for (i = 0; i < MSRPM_OFFSETS; ++i) { | |
784 | ||
785 | /* Offset already in list? */ | |
786 | if (msrpm_offsets[i] == offset) | |
bfc733a7 | 787 | return; |
323c3d80 JR |
788 | |
789 | /* Slot used by another offset? */ | |
790 | if (msrpm_offsets[i] != MSR_INVALID) | |
791 | continue; | |
792 | ||
793 | /* Add offset to list */ | |
794 | msrpm_offsets[i] = offset; | |
795 | ||
796 | return; | |
6aa8b732 | 797 | } |
323c3d80 JR |
798 | |
799 | /* | |
800 | * If this BUG triggers the msrpm_offsets table has an overflow. Just | |
801 | * increase MSRPM_OFFSETS in this case. | |
802 | */ | |
bfc733a7 | 803 | BUG(); |
6aa8b732 AK |
804 | } |
805 | ||
323c3d80 | 806 | static void init_msrpm_offsets(void) |
f65c229c | 807 | { |
323c3d80 | 808 | int i; |
f65c229c | 809 | |
323c3d80 JR |
810 | memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets)); |
811 | ||
812 | for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) { | |
813 | u32 offset; | |
814 | ||
815 | offset = svm_msrpm_offset(direct_access_msrs[i].index); | |
816 | BUG_ON(offset == MSR_INVALID); | |
817 | ||
818 | add_msr_offset(offset); | |
819 | } | |
f65c229c JR |
820 | } |
821 | ||
24e09cbf JR |
822 | static void svm_enable_lbrv(struct vcpu_svm *svm) |
823 | { | |
824 | u32 *msrpm = svm->msrpm; | |
825 | ||
826 | svm->vmcb->control.lbr_ctl = 1; | |
827 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1); | |
828 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1); | |
829 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1); | |
830 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1); | |
831 | } | |
832 | ||
833 | static void svm_disable_lbrv(struct vcpu_svm *svm) | |
834 | { | |
835 | u32 *msrpm = svm->msrpm; | |
836 | ||
837 | svm->vmcb->control.lbr_ctl = 0; | |
838 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0); | |
839 | set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0); | |
840 | set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0); | |
841 | set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0); | |
842 | } | |
843 | ||
6aa8b732 AK |
844 | static __init int svm_hardware_setup(void) |
845 | { | |
846 | int cpu; | |
847 | struct page *iopm_pages; | |
f65c229c | 848 | void *iopm_va; |
6aa8b732 AK |
849 | int r; |
850 | ||
6aa8b732 AK |
851 | iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER); |
852 | ||
853 | if (!iopm_pages) | |
854 | return -ENOMEM; | |
c8681339 AL |
855 | |
856 | iopm_va = page_address(iopm_pages); | |
857 | memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER)); | |
6aa8b732 AK |
858 | iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT; |
859 | ||
323c3d80 JR |
860 | init_msrpm_offsets(); |
861 | ||
50a37eb4 JR |
862 | if (boot_cpu_has(X86_FEATURE_NX)) |
863 | kvm_enable_efer_bits(EFER_NX); | |
864 | ||
1b2fd70c AG |
865 | if (boot_cpu_has(X86_FEATURE_FXSR_OPT)) |
866 | kvm_enable_efer_bits(EFER_FFXSR); | |
867 | ||
92a1f12d JR |
868 | if (boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { |
869 | u64 max; | |
870 | ||
871 | kvm_has_tsc_control = true; | |
872 | ||
873 | /* | |
874 | * Make sure the user can only configure tsc_khz values that | |
875 | * fit into a signed integer. | |
876 | * A min value is not calculated needed because it will always | |
877 | * be 1 on all machines and a value of 0 is used to disable | |
878 | * tsc-scaling for the vcpu. | |
879 | */ | |
880 | max = min(0x7fffffffULL, __scale_tsc(tsc_khz, TSC_RATIO_MAX)); | |
881 | ||
882 | kvm_max_guest_tsc_khz = max; | |
883 | } | |
884 | ||
236de055 AG |
885 | if (nested) { |
886 | printk(KERN_INFO "kvm: Nested Virtualization enabled\n"); | |
eec4b140 | 887 | kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE); |
236de055 AG |
888 | } |
889 | ||
3230bb47 | 890 | for_each_possible_cpu(cpu) { |
6aa8b732 AK |
891 | r = svm_cpu_init(cpu); |
892 | if (r) | |
f65c229c | 893 | goto err; |
6aa8b732 | 894 | } |
33bd6a0b | 895 | |
2a6b20b8 | 896 | if (!boot_cpu_has(X86_FEATURE_NPT)) |
e3da3acd JR |
897 | npt_enabled = false; |
898 | ||
6c7dac72 JR |
899 | if (npt_enabled && !npt) { |
900 | printk(KERN_INFO "kvm: Nested Paging disabled\n"); | |
901 | npt_enabled = false; | |
902 | } | |
903 | ||
18552672 | 904 | if (npt_enabled) { |
e3da3acd | 905 | printk(KERN_INFO "kvm: Nested Paging enabled\n"); |
18552672 | 906 | kvm_enable_tdp(); |
5f4cb662 JR |
907 | } else |
908 | kvm_disable_tdp(); | |
e3da3acd | 909 | |
6aa8b732 AK |
910 | return 0; |
911 | ||
f65c229c | 912 | err: |
6aa8b732 AK |
913 | __free_pages(iopm_pages, IOPM_ALLOC_ORDER); |
914 | iopm_base = 0; | |
915 | return r; | |
916 | } | |
917 | ||
918 | static __exit void svm_hardware_unsetup(void) | |
919 | { | |
0da1db75 JR |
920 | int cpu; |
921 | ||
3230bb47 | 922 | for_each_possible_cpu(cpu) |
0da1db75 JR |
923 | svm_cpu_uninit(cpu); |
924 | ||
6aa8b732 | 925 | __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER); |
f65c229c | 926 | iopm_base = 0; |
6aa8b732 AK |
927 | } |
928 | ||
929 | static void init_seg(struct vmcb_seg *seg) | |
930 | { | |
931 | seg->selector = 0; | |
932 | seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK | | |
e0231715 | 933 | SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */ |
6aa8b732 AK |
934 | seg->limit = 0xffff; |
935 | seg->base = 0; | |
936 | } | |
937 | ||
938 | static void init_sys_seg(struct vmcb_seg *seg, uint32_t type) | |
939 | { | |
940 | seg->selector = 0; | |
941 | seg->attrib = SVM_SELECTOR_P_MASK | type; | |
942 | seg->limit = 0xffff; | |
943 | seg->base = 0; | |
944 | } | |
945 | ||
fbc0db76 JR |
946 | static u64 __scale_tsc(u64 ratio, u64 tsc) |
947 | { | |
948 | u64 mult, frac, _tsc; | |
949 | ||
950 | mult = ratio >> 32; | |
951 | frac = ratio & ((1ULL << 32) - 1); | |
952 | ||
953 | _tsc = tsc; | |
954 | _tsc *= mult; | |
955 | _tsc += (tsc >> 32) * frac; | |
956 | _tsc += ((tsc & ((1ULL << 32) - 1)) * frac) >> 32; | |
957 | ||
958 | return _tsc; | |
959 | } | |
960 | ||
961 | static u64 svm_scale_tsc(struct kvm_vcpu *vcpu, u64 tsc) | |
962 | { | |
963 | struct vcpu_svm *svm = to_svm(vcpu); | |
964 | u64 _tsc = tsc; | |
965 | ||
966 | if (svm->tsc_ratio != TSC_RATIO_DEFAULT) | |
967 | _tsc = __scale_tsc(svm->tsc_ratio, tsc); | |
968 | ||
969 | return _tsc; | |
970 | } | |
971 | ||
cc578287 | 972 | static void svm_set_tsc_khz(struct kvm_vcpu *vcpu, u32 user_tsc_khz, bool scale) |
4051b188 JR |
973 | { |
974 | struct vcpu_svm *svm = to_svm(vcpu); | |
975 | u64 ratio; | |
976 | u64 khz; | |
977 | ||
cc578287 ZA |
978 | /* Guest TSC same frequency as host TSC? */ |
979 | if (!scale) { | |
980 | svm->tsc_ratio = TSC_RATIO_DEFAULT; | |
4051b188 | 981 | return; |
cc578287 | 982 | } |
4051b188 | 983 | |
cc578287 ZA |
984 | /* TSC scaling supported? */ |
985 | if (!boot_cpu_has(X86_FEATURE_TSCRATEMSR)) { | |
986 | if (user_tsc_khz > tsc_khz) { | |
987 | vcpu->arch.tsc_catchup = 1; | |
988 | vcpu->arch.tsc_always_catchup = 1; | |
989 | } else | |
990 | WARN(1, "user requested TSC rate below hardware speed\n"); | |
4051b188 JR |
991 | return; |
992 | } | |
993 | ||
994 | khz = user_tsc_khz; | |
995 | ||
996 | /* TSC scaling required - calculate ratio */ | |
997 | ratio = khz << 32; | |
998 | do_div(ratio, tsc_khz); | |
999 | ||
1000 | if (ratio == 0 || ratio & TSC_RATIO_RSVD) { | |
1001 | WARN_ONCE(1, "Invalid TSC ratio - virtual-tsc-khz=%u\n", | |
1002 | user_tsc_khz); | |
1003 | return; | |
1004 | } | |
4051b188 JR |
1005 | svm->tsc_ratio = ratio; |
1006 | } | |
1007 | ||
f4e1b3c8 ZA |
1008 | static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset) |
1009 | { | |
1010 | struct vcpu_svm *svm = to_svm(vcpu); | |
1011 | u64 g_tsc_offset = 0; | |
1012 | ||
2030753d | 1013 | if (is_guest_mode(vcpu)) { |
f4e1b3c8 ZA |
1014 | g_tsc_offset = svm->vmcb->control.tsc_offset - |
1015 | svm->nested.hsave->control.tsc_offset; | |
1016 | svm->nested.hsave->control.tsc_offset = offset; | |
1017 | } | |
1018 | ||
1019 | svm->vmcb->control.tsc_offset = offset + g_tsc_offset; | |
116a0a23 JR |
1020 | |
1021 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); | |
f4e1b3c8 ZA |
1022 | } |
1023 | ||
f1e2b260 | 1024 | static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment, bool host) |
e48672fa ZA |
1025 | { |
1026 | struct vcpu_svm *svm = to_svm(vcpu); | |
1027 | ||
f1e2b260 MT |
1028 | WARN_ON(adjustment < 0); |
1029 | if (host) | |
1030 | adjustment = svm_scale_tsc(vcpu, adjustment); | |
1031 | ||
e48672fa | 1032 | svm->vmcb->control.tsc_offset += adjustment; |
2030753d | 1033 | if (is_guest_mode(vcpu)) |
e48672fa | 1034 | svm->nested.hsave->control.tsc_offset += adjustment; |
116a0a23 | 1035 | mark_dirty(svm->vmcb, VMCB_INTERCEPTS); |
e48672fa ZA |
1036 | } |
1037 | ||
857e4099 JR |
1038 | static u64 svm_compute_tsc_offset(struct kvm_vcpu *vcpu, u64 target_tsc) |
1039 | { | |
1040 | u64 tsc; | |
1041 | ||
1042 | tsc = svm_scale_tsc(vcpu, native_read_tsc()); | |
1043 | ||
1044 | return target_tsc - tsc; | |
1045 | } | |
1046 | ||
e6101a96 | 1047 | static void init_vmcb(struct vcpu_svm *svm) |
6aa8b732 | 1048 | { |
e6101a96 JR |
1049 | struct vmcb_control_area *control = &svm->vmcb->control; |
1050 | struct vmcb_save_area *save = &svm->vmcb->save; | |
6aa8b732 | 1051 | |
bff78274 | 1052 | svm->vcpu.fpu_active = 1; |
4ee546b4 | 1053 | svm->vcpu.arch.hflags = 0; |
bff78274 | 1054 | |
4ee546b4 RJ |
1055 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
1056 | set_cr_intercept(svm, INTERCEPT_CR3_READ); | |
1057 | set_cr_intercept(svm, INTERCEPT_CR4_READ); | |
1058 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); | |
1059 | set_cr_intercept(svm, INTERCEPT_CR3_WRITE); | |
1060 | set_cr_intercept(svm, INTERCEPT_CR4_WRITE); | |
1061 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); | |
6aa8b732 | 1062 | |
3aed041a JR |
1063 | set_dr_intercept(svm, INTERCEPT_DR0_READ); |
1064 | set_dr_intercept(svm, INTERCEPT_DR1_READ); | |
1065 | set_dr_intercept(svm, INTERCEPT_DR2_READ); | |
1066 | set_dr_intercept(svm, INTERCEPT_DR3_READ); | |
1067 | set_dr_intercept(svm, INTERCEPT_DR4_READ); | |
1068 | set_dr_intercept(svm, INTERCEPT_DR5_READ); | |
1069 | set_dr_intercept(svm, INTERCEPT_DR6_READ); | |
1070 | set_dr_intercept(svm, INTERCEPT_DR7_READ); | |
1071 | ||
1072 | set_dr_intercept(svm, INTERCEPT_DR0_WRITE); | |
1073 | set_dr_intercept(svm, INTERCEPT_DR1_WRITE); | |
1074 | set_dr_intercept(svm, INTERCEPT_DR2_WRITE); | |
1075 | set_dr_intercept(svm, INTERCEPT_DR3_WRITE); | |
1076 | set_dr_intercept(svm, INTERCEPT_DR4_WRITE); | |
1077 | set_dr_intercept(svm, INTERCEPT_DR5_WRITE); | |
1078 | set_dr_intercept(svm, INTERCEPT_DR6_WRITE); | |
1079 | set_dr_intercept(svm, INTERCEPT_DR7_WRITE); | |
6aa8b732 | 1080 | |
18c918c5 JR |
1081 | set_exception_intercept(svm, PF_VECTOR); |
1082 | set_exception_intercept(svm, UD_VECTOR); | |
1083 | set_exception_intercept(svm, MC_VECTOR); | |
6aa8b732 | 1084 | |
8a05a1b8 JR |
1085 | set_intercept(svm, INTERCEPT_INTR); |
1086 | set_intercept(svm, INTERCEPT_NMI); | |
1087 | set_intercept(svm, INTERCEPT_SMI); | |
1088 | set_intercept(svm, INTERCEPT_SELECTIVE_CR0); | |
332b56e4 | 1089 | set_intercept(svm, INTERCEPT_RDPMC); |
8a05a1b8 JR |
1090 | set_intercept(svm, INTERCEPT_CPUID); |
1091 | set_intercept(svm, INTERCEPT_INVD); | |
1092 | set_intercept(svm, INTERCEPT_HLT); | |
1093 | set_intercept(svm, INTERCEPT_INVLPG); | |
1094 | set_intercept(svm, INTERCEPT_INVLPGA); | |
1095 | set_intercept(svm, INTERCEPT_IOIO_PROT); | |
1096 | set_intercept(svm, INTERCEPT_MSR_PROT); | |
1097 | set_intercept(svm, INTERCEPT_TASK_SWITCH); | |
1098 | set_intercept(svm, INTERCEPT_SHUTDOWN); | |
1099 | set_intercept(svm, INTERCEPT_VMRUN); | |
1100 | set_intercept(svm, INTERCEPT_VMMCALL); | |
1101 | set_intercept(svm, INTERCEPT_VMLOAD); | |
1102 | set_intercept(svm, INTERCEPT_VMSAVE); | |
1103 | set_intercept(svm, INTERCEPT_STGI); | |
1104 | set_intercept(svm, INTERCEPT_CLGI); | |
1105 | set_intercept(svm, INTERCEPT_SKINIT); | |
1106 | set_intercept(svm, INTERCEPT_WBINVD); | |
1107 | set_intercept(svm, INTERCEPT_MONITOR); | |
1108 | set_intercept(svm, INTERCEPT_MWAIT); | |
81dd35d4 | 1109 | set_intercept(svm, INTERCEPT_XSETBV); |
6aa8b732 AK |
1110 | |
1111 | control->iopm_base_pa = iopm_base; | |
f65c229c | 1112 | control->msrpm_base_pa = __pa(svm->msrpm); |
6aa8b732 AK |
1113 | control->int_ctl = V_INTR_MASKING_MASK; |
1114 | ||
1115 | init_seg(&save->es); | |
1116 | init_seg(&save->ss); | |
1117 | init_seg(&save->ds); | |
1118 | init_seg(&save->fs); | |
1119 | init_seg(&save->gs); | |
1120 | ||
1121 | save->cs.selector = 0xf000; | |
1122 | /* Executable/Readable Code Segment */ | |
1123 | save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK | | |
1124 | SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK; | |
1125 | save->cs.limit = 0xffff; | |
d92899a0 AK |
1126 | /* |
1127 | * cs.base should really be 0xffff0000, but vmx can't handle that, so | |
1128 | * be consistent with it. | |
1129 | * | |
1130 | * Replace when we have real mode working for vmx. | |
1131 | */ | |
1132 | save->cs.base = 0xf0000; | |
6aa8b732 AK |
1133 | |
1134 | save->gdtr.limit = 0xffff; | |
1135 | save->idtr.limit = 0xffff; | |
1136 | ||
1137 | init_sys_seg(&save->ldtr, SEG_TYPE_LDT); | |
1138 | init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16); | |
1139 | ||
eaa48512 | 1140 | svm_set_efer(&svm->vcpu, 0); |
d77c26fc | 1141 | save->dr6 = 0xffff0ff0; |
6aa8b732 | 1142 | save->dr7 = 0x400; |
f6e78475 | 1143 | kvm_set_rflags(&svm->vcpu, 2); |
6aa8b732 | 1144 | save->rip = 0x0000fff0; |
5fdbf976 | 1145 | svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip; |
6aa8b732 | 1146 | |
e0231715 JR |
1147 | /* |
1148 | * This is the guest-visible cr0 value. | |
18fa000a | 1149 | * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0. |
6aa8b732 | 1150 | */ |
678041ad MT |
1151 | svm->vcpu.arch.cr0 = 0; |
1152 | (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET); | |
18fa000a | 1153 | |
66aee91a | 1154 | save->cr4 = X86_CR4_PAE; |
6aa8b732 | 1155 | /* rdx = ?? */ |
709ddebf JR |
1156 | |
1157 | if (npt_enabled) { | |
1158 | /* Setup VMCB for Nested Paging */ | |
1159 | control->nested_ctl = 1; | |
8a05a1b8 | 1160 | clr_intercept(svm, INTERCEPT_INVLPG); |
18c918c5 | 1161 | clr_exception_intercept(svm, PF_VECTOR); |
4ee546b4 RJ |
1162 | clr_cr_intercept(svm, INTERCEPT_CR3_READ); |
1163 | clr_cr_intercept(svm, INTERCEPT_CR3_WRITE); | |
709ddebf | 1164 | save->g_pat = 0x0007040600070406ULL; |
709ddebf JR |
1165 | save->cr3 = 0; |
1166 | save->cr4 = 0; | |
1167 | } | |
f40f6a45 | 1168 | svm->asid_generation = 0; |
1371d904 | 1169 | |
e6aa9abd | 1170 | svm->nested.vmcb = 0; |
2af9194d JR |
1171 | svm->vcpu.arch.hflags = 0; |
1172 | ||
2a6b20b8 | 1173 | if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) { |
565d0998 | 1174 | control->pause_filter_count = 3000; |
8a05a1b8 | 1175 | set_intercept(svm, INTERCEPT_PAUSE); |
565d0998 ML |
1176 | } |
1177 | ||
8d28fec4 RJ |
1178 | mark_all_dirty(svm->vmcb); |
1179 | ||
2af9194d | 1180 | enable_gif(svm); |
6aa8b732 AK |
1181 | } |
1182 | ||
e00c8cf2 | 1183 | static int svm_vcpu_reset(struct kvm_vcpu *vcpu) |
04d2cc77 AK |
1184 | { |
1185 | struct vcpu_svm *svm = to_svm(vcpu); | |
1186 | ||
e6101a96 | 1187 | init_vmcb(svm); |
70433389 | 1188 | |
c5af89b6 | 1189 | if (!kvm_vcpu_is_bsp(vcpu)) { |
5fdbf976 | 1190 | kvm_rip_write(vcpu, 0); |
ad312c7c ZX |
1191 | svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12; |
1192 | svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8; | |
70433389 | 1193 | } |
5fdbf976 MT |
1194 | vcpu->arch.regs_avail = ~0; |
1195 | vcpu->arch.regs_dirty = ~0; | |
e00c8cf2 AK |
1196 | |
1197 | return 0; | |
04d2cc77 AK |
1198 | } |
1199 | ||
fb3f0f51 | 1200 | static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id) |
6aa8b732 | 1201 | { |
a2fa3e9f | 1202 | struct vcpu_svm *svm; |
6aa8b732 | 1203 | struct page *page; |
f65c229c | 1204 | struct page *msrpm_pages; |
b286d5d8 | 1205 | struct page *hsave_page; |
3d6368ef | 1206 | struct page *nested_msrpm_pages; |
fb3f0f51 | 1207 | int err; |
6aa8b732 | 1208 | |
c16f862d | 1209 | svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
fb3f0f51 RR |
1210 | if (!svm) { |
1211 | err = -ENOMEM; | |
1212 | goto out; | |
1213 | } | |
1214 | ||
fbc0db76 JR |
1215 | svm->tsc_ratio = TSC_RATIO_DEFAULT; |
1216 | ||
fb3f0f51 RR |
1217 | err = kvm_vcpu_init(&svm->vcpu, kvm, id); |
1218 | if (err) | |
1219 | goto free_svm; | |
1220 | ||
b7af4043 | 1221 | err = -ENOMEM; |
6aa8b732 | 1222 | page = alloc_page(GFP_KERNEL); |
b7af4043 | 1223 | if (!page) |
fb3f0f51 | 1224 | goto uninit; |
6aa8b732 | 1225 | |
f65c229c JR |
1226 | msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); |
1227 | if (!msrpm_pages) | |
b7af4043 | 1228 | goto free_page1; |
3d6368ef AG |
1229 | |
1230 | nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER); | |
1231 | if (!nested_msrpm_pages) | |
b7af4043 | 1232 | goto free_page2; |
f65c229c | 1233 | |
b286d5d8 AG |
1234 | hsave_page = alloc_page(GFP_KERNEL); |
1235 | if (!hsave_page) | |
b7af4043 TY |
1236 | goto free_page3; |
1237 | ||
e6aa9abd | 1238 | svm->nested.hsave = page_address(hsave_page); |
b286d5d8 | 1239 | |
b7af4043 TY |
1240 | svm->msrpm = page_address(msrpm_pages); |
1241 | svm_vcpu_init_msrpm(svm->msrpm); | |
1242 | ||
e6aa9abd | 1243 | svm->nested.msrpm = page_address(nested_msrpm_pages); |
323c3d80 | 1244 | svm_vcpu_init_msrpm(svm->nested.msrpm); |
3d6368ef | 1245 | |
a2fa3e9f GH |
1246 | svm->vmcb = page_address(page); |
1247 | clear_page(svm->vmcb); | |
1248 | svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT; | |
1249 | svm->asid_generation = 0; | |
e6101a96 | 1250 | init_vmcb(svm); |
99e3e30a | 1251 | kvm_write_tsc(&svm->vcpu, 0); |
a2fa3e9f | 1252 | |
10ab25cd JK |
1253 | err = fx_init(&svm->vcpu); |
1254 | if (err) | |
1255 | goto free_page4; | |
1256 | ||
ad312c7c | 1257 | svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE; |
c5af89b6 | 1258 | if (kvm_vcpu_is_bsp(&svm->vcpu)) |
ad312c7c | 1259 | svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP; |
6aa8b732 | 1260 | |
2b036c6b BO |
1261 | svm_init_osvw(&svm->vcpu); |
1262 | ||
fb3f0f51 | 1263 | return &svm->vcpu; |
36241b8c | 1264 | |
10ab25cd JK |
1265 | free_page4: |
1266 | __free_page(hsave_page); | |
b7af4043 TY |
1267 | free_page3: |
1268 | __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER); | |
1269 | free_page2: | |
1270 | __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER); | |
1271 | free_page1: | |
1272 | __free_page(page); | |
fb3f0f51 RR |
1273 | uninit: |
1274 | kvm_vcpu_uninit(&svm->vcpu); | |
1275 | free_svm: | |
a4770347 | 1276 | kmem_cache_free(kvm_vcpu_cache, svm); |
fb3f0f51 RR |
1277 | out: |
1278 | return ERR_PTR(err); | |
6aa8b732 AK |
1279 | } |
1280 | ||
1281 | static void svm_free_vcpu(struct kvm_vcpu *vcpu) | |
1282 | { | |
a2fa3e9f GH |
1283 | struct vcpu_svm *svm = to_svm(vcpu); |
1284 | ||
fb3f0f51 | 1285 | __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT)); |
f65c229c | 1286 | __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER); |
e6aa9abd JR |
1287 | __free_page(virt_to_page(svm->nested.hsave)); |
1288 | __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER); | |
fb3f0f51 | 1289 | kvm_vcpu_uninit(vcpu); |
a4770347 | 1290 | kmem_cache_free(kvm_vcpu_cache, svm); |
6aa8b732 AK |
1291 | } |
1292 | ||
15ad7146 | 1293 | static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
6aa8b732 | 1294 | { |
a2fa3e9f | 1295 | struct vcpu_svm *svm = to_svm(vcpu); |
15ad7146 | 1296 | int i; |
0cc5064d | 1297 | |
0cc5064d | 1298 | if (unlikely(cpu != vcpu->cpu)) { |
4b656b12 | 1299 | svm->asid_generation = 0; |
8d28fec4 | 1300 | mark_all_dirty(svm->vmcb); |
0cc5064d | 1301 | } |
94dfbdb3 | 1302 | |
82ca2d10 AK |
1303 | #ifdef CONFIG_X86_64 |
1304 | rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base); | |
1305 | #endif | |
dacccfdd AK |
1306 | savesegment(fs, svm->host.fs); |
1307 | savesegment(gs, svm->host.gs); | |
1308 | svm->host.ldt = kvm_read_ldt(); | |
1309 | ||
94dfbdb3 | 1310 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
a2fa3e9f | 1311 | rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
fbc0db76 JR |
1312 | |
1313 | if (static_cpu_has(X86_FEATURE_TSCRATEMSR) && | |
1314 | svm->tsc_ratio != __get_cpu_var(current_tsc_ratio)) { | |
1315 | __get_cpu_var(current_tsc_ratio) = svm->tsc_ratio; | |
1316 | wrmsrl(MSR_AMD64_TSC_RATIO, svm->tsc_ratio); | |
1317 | } | |
6aa8b732 AK |
1318 | } |
1319 | ||
1320 | static void svm_vcpu_put(struct kvm_vcpu *vcpu) | |
1321 | { | |
a2fa3e9f | 1322 | struct vcpu_svm *svm = to_svm(vcpu); |
94dfbdb3 AL |
1323 | int i; |
1324 | ||
e1beb1d3 | 1325 | ++vcpu->stat.host_state_reload; |
dacccfdd AK |
1326 | kvm_load_ldt(svm->host.ldt); |
1327 | #ifdef CONFIG_X86_64 | |
1328 | loadsegment(fs, svm->host.fs); | |
dacccfdd | 1329 | wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs); |
893a5ab6 | 1330 | load_gs_index(svm->host.gs); |
dacccfdd | 1331 | #else |
831ca609 | 1332 | #ifdef CONFIG_X86_32_LAZY_GS |
dacccfdd | 1333 | loadsegment(gs, svm->host.gs); |
831ca609 | 1334 | #endif |
dacccfdd | 1335 | #endif |
94dfbdb3 | 1336 | for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++) |
a2fa3e9f | 1337 | wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]); |
6aa8b732 AK |
1338 | } |
1339 | ||
ea5e97e8 KW |
1340 | static void svm_update_cpl(struct kvm_vcpu *vcpu) |
1341 | { | |
1342 | struct vcpu_svm *svm = to_svm(vcpu); | |
1343 | int cpl; | |
1344 | ||
1345 | if (!is_protmode(vcpu)) | |
1346 | cpl = 0; | |
1347 | else if (svm->vmcb->save.rflags & X86_EFLAGS_VM) | |
1348 | cpl = 3; | |
1349 | else | |
1350 | cpl = svm->vmcb->save.cs.selector & 0x3; | |
1351 | ||
1352 | svm->vmcb->save.cpl = cpl; | |
1353 | } | |
1354 | ||
6aa8b732 AK |
1355 | static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu) |
1356 | { | |
a2fa3e9f | 1357 | return to_svm(vcpu)->vmcb->save.rflags; |
6aa8b732 AK |
1358 | } |
1359 | ||
1360 | static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags) | |
1361 | { | |
4cee4798 KW |
1362 | unsigned long old_rflags = to_svm(vcpu)->vmcb->save.rflags; |
1363 | ||
a2fa3e9f | 1364 | to_svm(vcpu)->vmcb->save.rflags = rflags; |
4cee4798 KW |
1365 | if ((old_rflags ^ rflags) & X86_EFLAGS_VM) |
1366 | svm_update_cpl(vcpu); | |
6aa8b732 AK |
1367 | } |
1368 | ||
6de4f3ad AK |
1369 | static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg) |
1370 | { | |
1371 | switch (reg) { | |
1372 | case VCPU_EXREG_PDPTR: | |
1373 | BUG_ON(!npt_enabled); | |
9f8fe504 | 1374 | load_pdptrs(vcpu, vcpu->arch.walk_mmu, kvm_read_cr3(vcpu)); |
6de4f3ad AK |
1375 | break; |
1376 | default: | |
1377 | BUG(); | |
1378 | } | |
1379 | } | |
1380 | ||
f0b85051 AG |
1381 | static void svm_set_vintr(struct vcpu_svm *svm) |
1382 | { | |
8a05a1b8 | 1383 | set_intercept(svm, INTERCEPT_VINTR); |
f0b85051 AG |
1384 | } |
1385 | ||
1386 | static void svm_clear_vintr(struct vcpu_svm *svm) | |
1387 | { | |
8a05a1b8 | 1388 | clr_intercept(svm, INTERCEPT_VINTR); |
f0b85051 AG |
1389 | } |
1390 | ||
6aa8b732 AK |
1391 | static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg) |
1392 | { | |
a2fa3e9f | 1393 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; |
6aa8b732 AK |
1394 | |
1395 | switch (seg) { | |
1396 | case VCPU_SREG_CS: return &save->cs; | |
1397 | case VCPU_SREG_DS: return &save->ds; | |
1398 | case VCPU_SREG_ES: return &save->es; | |
1399 | case VCPU_SREG_FS: return &save->fs; | |
1400 | case VCPU_SREG_GS: return &save->gs; | |
1401 | case VCPU_SREG_SS: return &save->ss; | |
1402 | case VCPU_SREG_TR: return &save->tr; | |
1403 | case VCPU_SREG_LDTR: return &save->ldtr; | |
1404 | } | |
1405 | BUG(); | |
8b6d44c7 | 1406 | return NULL; |
6aa8b732 AK |
1407 | } |
1408 | ||
1409 | static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg) | |
1410 | { | |
1411 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
1412 | ||
1413 | return s->base; | |
1414 | } | |
1415 | ||
1416 | static void svm_get_segment(struct kvm_vcpu *vcpu, | |
1417 | struct kvm_segment *var, int seg) | |
1418 | { | |
1419 | struct vmcb_seg *s = svm_seg(vcpu, seg); | |
1420 | ||
1421 | var->base = s->base; | |
1422 | var->limit = s->limit; | |
1423 | var->selector = s->selector; | |
1424 | var->type = s->attrib & SVM_SELECTOR_TYPE_MASK; | |
1425 | var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1; | |
1426 | var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3; | |
1427 | var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1; | |
1428 | var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1; | |
1429 | var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1; | |
1430 | var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1; | |
1431 | var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1; | |
25022acc | 1432 | |
e0231715 JR |
1433 | /* |
1434 | * AMD's VMCB does not have an explicit unusable field, so emulate it | |
19bca6ab AP |
1435 | * for cross vendor migration purposes by "not present" |
1436 | */ | |
1437 | var->unusable = !var->present || (var->type == 0); | |
1438 | ||
1fbdc7a5 AP |
1439 | switch (seg) { |
1440 | case VCPU_SREG_CS: | |
1441 | /* | |
1442 | * SVM always stores 0 for the 'G' bit in the CS selector in | |
1443 | * the VMCB on a VMEXIT. This hurts cross-vendor migration: | |
1444 | * Intel's VMENTRY has a check on the 'G' bit. | |
1445 | */ | |
25022acc | 1446 | var->g = s->limit > 0xfffff; |
1fbdc7a5 AP |
1447 | break; |
1448 | case VCPU_SREG_TR: | |
1449 | /* | |
1450 | * Work around a bug where the busy flag in the tr selector | |
1451 | * isn't exposed | |
1452 | */ | |
c0d09828 | 1453 | var->type |= 0x2; |
1fbdc7a5 AP |
1454 | break; |
1455 | case VCPU_SREG_DS: | |
1456 | case VCPU_SREG_ES: | |
1457 | case VCPU_SREG_FS: | |
1458 | case VCPU_SREG_GS: | |
1459 | /* | |
1460 | * The accessed bit must always be set in the segment | |
1461 | * descriptor cache, although it can be cleared in the | |
1462 | * descriptor, the cached bit always remains at 1. Since | |
1463 | * Intel has a check on this, set it here to support | |
1464 | * cross-vendor migration. | |
1465 | */ | |
1466 | if (!var->unusable) | |
1467 | var->type |= 0x1; | |
1468 | break; | |
b586eb02 | 1469 | case VCPU_SREG_SS: |
e0231715 JR |
1470 | /* |
1471 | * On AMD CPUs sometimes the DB bit in the segment | |
b586eb02 AP |
1472 | * descriptor is left as 1, although the whole segment has |
1473 | * been made unusable. Clear it here to pass an Intel VMX | |
1474 | * entry check when cross vendor migrating. | |
1475 | */ | |
1476 | if (var->unusable) | |
1477 | var->db = 0; | |
1478 | break; | |
1fbdc7a5 | 1479 | } |
6aa8b732 AK |
1480 | } |
1481 | ||
2e4d2653 IE |
1482 | static int svm_get_cpl(struct kvm_vcpu *vcpu) |
1483 | { | |
1484 | struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save; | |
1485 | ||
1486 | return save->cpl; | |
1487 | } | |
1488 | ||
89a27f4d | 1489 | static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1490 | { |
a2fa3e9f GH |
1491 | struct vcpu_svm *svm = to_svm(vcpu); |
1492 | ||
89a27f4d GN |
1493 | dt->size = svm->vmcb->save.idtr.limit; |
1494 | dt->address = svm->vmcb->save.idtr.base; | |
6aa8b732 AK |
1495 | } |
1496 | ||
89a27f4d | 1497 | static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1498 | { |
a2fa3e9f GH |
1499 | struct vcpu_svm *svm = to_svm(vcpu); |
1500 | ||
89a27f4d GN |
1501 | svm->vmcb->save.idtr.limit = dt->size; |
1502 | svm->vmcb->save.idtr.base = dt->address ; | |
17a703cb | 1503 | mark_dirty(svm->vmcb, VMCB_DT); |
6aa8b732 AK |
1504 | } |
1505 | ||
89a27f4d | 1506 | static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1507 | { |
a2fa3e9f GH |
1508 | struct vcpu_svm *svm = to_svm(vcpu); |
1509 | ||
89a27f4d GN |
1510 | dt->size = svm->vmcb->save.gdtr.limit; |
1511 | dt->address = svm->vmcb->save.gdtr.base; | |
6aa8b732 AK |
1512 | } |
1513 | ||
89a27f4d | 1514 | static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt) |
6aa8b732 | 1515 | { |
a2fa3e9f GH |
1516 | struct vcpu_svm *svm = to_svm(vcpu); |
1517 | ||
89a27f4d GN |
1518 | svm->vmcb->save.gdtr.limit = dt->size; |
1519 | svm->vmcb->save.gdtr.base = dt->address ; | |
17a703cb | 1520 | mark_dirty(svm->vmcb, VMCB_DT); |
6aa8b732 AK |
1521 | } |
1522 | ||
e8467fda AK |
1523 | static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu) |
1524 | { | |
1525 | } | |
1526 | ||
aff48baa AK |
1527 | static void svm_decache_cr3(struct kvm_vcpu *vcpu) |
1528 | { | |
1529 | } | |
1530 | ||
25c4c276 | 1531 | static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu) |
399badf3 AK |
1532 | { |
1533 | } | |
1534 | ||
d225157b AK |
1535 | static void update_cr0_intercept(struct vcpu_svm *svm) |
1536 | { | |
1537 | ulong gcr0 = svm->vcpu.arch.cr0; | |
1538 | u64 *hcr0 = &svm->vmcb->save.cr0; | |
1539 | ||
1540 | if (!svm->vcpu.fpu_active) | |
1541 | *hcr0 |= SVM_CR0_SELECTIVE_MASK; | |
1542 | else | |
1543 | *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK) | |
1544 | | (gcr0 & SVM_CR0_SELECTIVE_MASK); | |
1545 | ||
dcca1a65 | 1546 | mark_dirty(svm->vmcb, VMCB_CR); |
d225157b AK |
1547 | |
1548 | if (gcr0 == *hcr0 && svm->vcpu.fpu_active) { | |
4ee546b4 RJ |
1549 | clr_cr_intercept(svm, INTERCEPT_CR0_READ); |
1550 | clr_cr_intercept(svm, INTERCEPT_CR0_WRITE); | |
d225157b | 1551 | } else { |
4ee546b4 RJ |
1552 | set_cr_intercept(svm, INTERCEPT_CR0_READ); |
1553 | set_cr_intercept(svm, INTERCEPT_CR0_WRITE); | |
d225157b AK |
1554 | } |
1555 | } | |
1556 | ||
6aa8b732 AK |
1557 | static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0) |
1558 | { | |
a2fa3e9f GH |
1559 | struct vcpu_svm *svm = to_svm(vcpu); |
1560 | ||
05b3e0c2 | 1561 | #ifdef CONFIG_X86_64 |
f6801dff | 1562 | if (vcpu->arch.efer & EFER_LME) { |
707d92fa | 1563 | if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) { |
f6801dff | 1564 | vcpu->arch.efer |= EFER_LMA; |
2b5203ee | 1565 | svm->vmcb->save.efer |= EFER_LMA | EFER_LME; |
6aa8b732 AK |
1566 | } |
1567 | ||
d77c26fc | 1568 | if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) { |
f6801dff | 1569 | vcpu->arch.efer &= ~EFER_LMA; |
2b5203ee | 1570 | svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME); |
6aa8b732 AK |
1571 | } |
1572 | } | |
1573 | #endif | |
ad312c7c | 1574 | vcpu->arch.cr0 = cr0; |
888f9f3e AK |
1575 | |
1576 | if (!npt_enabled) | |
1577 | cr0 |= X86_CR0_PG | X86_CR0_WP; | |
02daab21 AK |
1578 | |
1579 | if (!vcpu->fpu_active) | |
334df50a | 1580 | cr0 |= X86_CR0_TS; |
709ddebf JR |
1581 | /* |
1582 | * re-enable caching here because the QEMU bios | |
1583 | * does not do it - this results in some delay at | |
1584 | * reboot | |
1585 | */ | |
1586 | cr0 &= ~(X86_CR0_CD | X86_CR0_NW); | |
a2fa3e9f | 1587 | svm->vmcb->save.cr0 = cr0; |
dcca1a65 | 1588 | mark_dirty(svm->vmcb, VMCB_CR); |
d225157b | 1589 | update_cr0_intercept(svm); |
6aa8b732 AK |
1590 | } |
1591 | ||
5e1746d6 | 1592 | static int svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4) |
6aa8b732 | 1593 | { |
6394b649 | 1594 | unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE; |
e5eab0ce JR |
1595 | unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4; |
1596 | ||
5e1746d6 NHE |
1597 | if (cr4 & X86_CR4_VMXE) |
1598 | return 1; | |
1599 | ||
e5eab0ce | 1600 | if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE)) |
f40f6a45 | 1601 | svm_flush_tlb(vcpu); |
6394b649 | 1602 | |
ec077263 JR |
1603 | vcpu->arch.cr4 = cr4; |
1604 | if (!npt_enabled) | |
1605 | cr4 |= X86_CR4_PAE; | |
6394b649 | 1606 | cr4 |= host_cr4_mce; |
ec077263 | 1607 | to_svm(vcpu)->vmcb->save.cr4 = cr4; |
dcca1a65 | 1608 | mark_dirty(to_svm(vcpu)->vmcb, VMCB_CR); |
5e1746d6 | 1609 | return 0; |
6aa8b732 AK |
1610 | } |
1611 | ||
1612 | static void svm_set_segment(struct kvm_vcpu *vcpu, | |
1613 | struct kvm_segment *var, int seg) | |
1614 | { | |
a2fa3e9f | 1615 | struct vcpu_svm *svm = to_svm(vcpu); |
6aa8b732 AK |
1616 | struct vmcb_seg *s = svm_seg(vcpu, seg); |
1617 | ||
1618 | s->base = var->base; | |
1619 | s->limit = var->limit; | |
1620 | s->selector = var->selector; | |
1621 | if (var->unusable) | |
1622 | s->attrib = 0; | |
1623 | else { | |
1624 | s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK); | |
1625 | s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT; | |
1626 | s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT; | |
1627 | s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT; | |
1628 | s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT; | |
1629 | s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT; | |
1630 | s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT; | |
1631 | s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT; | |
1632 | } | |
1633 | if (seg == VCPU_SREG_CS) | |
ea5e97e8 | 1634 | svm_update_cpl(vcpu); |
6aa8b732 | 1635 | |
060d0c9a | 1636 | mark_dirty(svm->vmcb, VMCB_SEG); |
6aa8b732 AK |
1637 | } |
1638 | ||
44c11430 | 1639 | static void update_db_intercept(struct kvm_vcpu *vcpu) |
6aa8b732 | 1640 | { |
d0bfb940 JK |
1641 | struct vcpu_svm *svm = to_svm(vcpu); |
1642 | ||
18c918c5 JR |
1643 | clr_exception_intercept(svm, DB_VECTOR); |
1644 | clr_exception_intercept(svm, BP_VECTOR); | |
44c11430 | 1645 | |
6be7d306 | 1646 | if (svm->nmi_singlestep) |
18c918c5 | 1647 | set_exception_intercept(svm, DB_VECTOR); |
44c11430 | 1648 | |
d0bfb940 JK |
1649 | if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) { |
1650 | if (vcpu->guest_debug & | |
1651 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) | |
18c918c5 | 1652 | set_exception_intercept(svm, DB_VECTOR); |
d0bfb940 | 1653 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) |
18c918c5 | 1654 | set_exception_intercept(svm, BP_VECTOR); |
d0bfb940 JK |
1655 | } else |
1656 | vcpu->guest_debug = 0; | |
44c11430 GN |
1657 | } |
1658 | ||
355be0b9 | 1659 | static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg) |
44c11430 | 1660 | { |
44c11430 GN |
1661 | struct vcpu_svm *svm = to_svm(vcpu); |
1662 | ||
ae675ef0 JK |
1663 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP) |
1664 | svm->vmcb->save.dr7 = dbg->arch.debugreg[7]; | |
1665 | else | |
1666 | svm->vmcb->save.dr7 = vcpu->arch.dr7; | |
1667 | ||
72214b96 JR |
1668 | mark_dirty(svm->vmcb, VMCB_DR); |
1669 | ||
355be0b9 | 1670 | update_db_intercept(vcpu); |
6aa8b732 AK |
1671 | } |
1672 | ||
0fe1e009 | 1673 | static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd) |
6aa8b732 | 1674 | { |
0fe1e009 TH |
1675 | if (sd->next_asid > sd->max_asid) { |
1676 | ++sd->asid_generation; | |
1677 | sd->next_asid = 1; | |
a2fa3e9f | 1678 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID; |
6aa8b732 AK |
1679 | } |
1680 | ||
0fe1e009 TH |
1681 | svm->asid_generation = sd->asid_generation; |
1682 | svm->vmcb->control.asid = sd->next_asid++; | |
d48086d1 JR |
1683 | |
1684 | mark_dirty(svm->vmcb, VMCB_ASID); | |
6aa8b732 AK |
1685 | } |
1686 | ||
020df079 | 1687 | static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value) |
6aa8b732 | 1688 | { |
42dbaa5a | 1689 | struct vcpu_svm *svm = to_svm(vcpu); |
42dbaa5a | 1690 | |
020df079 | 1691 | svm->vmcb->save.dr7 = value; |
72214b96 | 1692 | mark_dirty(svm->vmcb, VMCB_DR); |
6aa8b732 AK |
1693 | } |
1694 | ||
851ba692 | 1695 | static int pf_interception(struct vcpu_svm *svm) |
6aa8b732 | 1696 | { |
631bc487 | 1697 | u64 fault_address = svm->vmcb->control.exit_info_2; |
6aa8b732 | 1698 | u32 error_code; |
631bc487 | 1699 | int r = 1; |
6aa8b732 | 1700 | |
631bc487 GN |
1701 | switch (svm->apf_reason) { |
1702 | default: | |
1703 | error_code = svm->vmcb->control.exit_info_1; | |
af9ca2d7 | 1704 | |
631bc487 GN |
1705 | trace_kvm_page_fault(fault_address, error_code); |
1706 | if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu)) | |
1707 | kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address); | |
dc25e89e AP |
1708 | r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code, |
1709 | svm->vmcb->control.insn_bytes, | |
1710 | svm->vmcb->control.insn_len); | |
631bc487 GN |
1711 | break; |
1712 | case KVM_PV_REASON_PAGE_NOT_PRESENT: | |
1713 | svm->apf_reason = 0; | |
1714 | local_irq_disable(); | |
1715 | kvm_async_pf_task_wait(fault_address); | |
1716 | local_irq_enable(); | |
1717 | break; | |
1718 | case KVM_PV_REASON_PAGE_READY: | |
1719 | svm->apf_reason = 0; | |
1720 | local_irq_disable(); | |
1721 | kvm_async_pf_task_wake(fault_address); | |
1722 | local_irq_enable(); | |
1723 | break; | |
1724 | } | |
1725 | return r; | |
6aa8b732 AK |
1726 | } |
1727 | ||
851ba692 | 1728 | static int db_interception(struct vcpu_svm *svm) |
d0bfb940 | 1729 | { |
851ba692 AK |
1730 | struct kvm_run *kvm_run = svm->vcpu.run; |
1731 | ||
d0bfb940 | 1732 | if (!(svm->vcpu.guest_debug & |
44c11430 | 1733 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) && |
6be7d306 | 1734 | !svm->nmi_singlestep) { |
d0bfb940 JK |
1735 | kvm_queue_exception(&svm->vcpu, DB_VECTOR); |
1736 | return 1; | |
1737 | } | |
44c11430 | 1738 | |
6be7d306 JK |
1739 | if (svm->nmi_singlestep) { |
1740 | svm->nmi_singlestep = false; | |
44c11430 GN |
1741 | if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP)) |
1742 | svm->vmcb->save.rflags &= | |
1743 | ~(X86_EFLAGS_TF | X86_EFLAGS_RF); | |
1744 | update_db_intercept(&svm->vcpu); | |
1745 | } | |
1746 | ||
1747 | if (svm->vcpu.guest_debug & | |
e0231715 | 1748 | (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) { |
44c11430 GN |
1749 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
1750 | kvm_run->debug.arch.pc = | |
1751 | svm->vmcb->save.cs.base + svm->vmcb->save.rip; | |
1752 | kvm_run->debug.arch.exception = DB_VECTOR; | |
1753 | return 0; | |
1754 | } | |
1755 | ||
1756 | return 1; | |
d0bfb940 JK |
1757 | } |
1758 | ||
851ba692 | 1759 | static int bp_interception(struct vcpu_svm *svm) |
d0bfb940 | 1760 | { |
851ba692 AK |
1761 | struct kvm_run *kvm_run = svm->vcpu.run; |
1762 | ||
d0bfb940 JK |
1763 | kvm_run->exit_reason = KVM_EXIT_DEBUG; |
1764 | kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip; | |
1765 | kvm_run->debug.arch.exception = BP_VECTOR; | |
1766 | return 0; | |
1767 | } | |
1768 | ||
851ba692 | 1769 | static int ud_interception(struct vcpu_svm *svm) |
7aa81cc0 AL |
1770 | { |
1771 | int er; | |
1772 | ||
51d8b661 | 1773 | er = emulate_instruction(&svm->vcpu, EMULTYPE_TRAP_UD); |
7aa81cc0 | 1774 | if (er != EMULATE_DONE) |
7ee5d940 | 1775 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
7aa81cc0 AL |
1776 | return 1; |
1777 | } | |
1778 | ||
6b52d186 | 1779 | static void svm_fpu_activate(struct kvm_vcpu *vcpu) |
7807fa6c | 1780 | { |
6b52d186 | 1781 | struct vcpu_svm *svm = to_svm(vcpu); |
66a562f7 | 1782 | |
18c918c5 | 1783 | clr_exception_intercept(svm, NM_VECTOR); |
66a562f7 | 1784 | |
e756fc62 | 1785 | svm->vcpu.fpu_active = 1; |
d225157b | 1786 | update_cr0_intercept(svm); |
6b52d186 | 1787 | } |
a2fa3e9f | 1788 | |
6b52d186 AK |
1789 | static int nm_interception(struct vcpu_svm *svm) |
1790 | { | |
1791 | svm_fpu_activate(&svm->vcpu); | |
a2fa3e9f | 1792 | return 1; |
7807fa6c AL |
1793 | } |
1794 | ||
67ec6607 JR |
1795 | static bool is_erratum_383(void) |
1796 | { | |
1797 | int err, i; | |
1798 | u64 value; | |
1799 | ||
1800 | if (!erratum_383_found) | |
1801 | return false; | |
1802 | ||
1803 | value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err); | |
1804 | if (err) | |
1805 | return false; | |
1806 | ||
1807 | /* Bit 62 may or may not be set for this mce */ | |
1808 | value &= ~(1ULL << 62); | |
1809 | ||
1810 | if (value != 0xb600000000010015ULL) | |
1811 | return false; | |
1812 | ||
1813 | /* Clear MCi_STATUS registers */ | |
1814 | for (i = 0; i < 6; ++i) | |
1815 | native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0); | |
1816 | ||
1817 | value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err); | |
1818 | if (!err) { | |
1819 | u32 low, high; | |
1820 | ||
1821 | value &= ~(1ULL << 2); | |
1822 | low = lower_32_bits(value); | |
1823 | high = upper_32_bits(value); | |
1824 | ||
1825 | native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high); | |
1826 | } | |
1827 | ||
1828 | /* Flush tlb to evict multi-match entries */ | |
1829 | __flush_tlb_all(); | |
1830 | ||
1831 | return true; | |
1832 | } | |
1833 | ||
fe5913e4 | 1834 | static void svm_handle_mce(struct vcpu_svm *svm) |
53371b50 | 1835 | { |
67ec6607 JR |
1836 | if (is_erratum_383()) { |
1837 | /* | |
1838 | * Erratum 383 triggered. Guest state is corrupt so kill the | |
1839 | * guest. | |
1840 | */ | |
1841 | pr_err("KVM: Guest triggered AMD Erratum 383\n"); | |
1842 | ||
a8eeb04a | 1843 | kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu); |
67ec6607 JR |
1844 | |
1845 | return; | |
1846 | } | |
1847 | ||
53371b50 JR |
1848 | /* |
1849 | * On an #MC intercept the MCE handler is not called automatically in | |
1850 | * the host. So do it by hand here. | |
1851 | */ | |
1852 | asm volatile ( | |
1853 | "int $0x12\n"); | |
1854 | /* not sure if we ever come back to this point */ | |
1855 | ||
fe5913e4 JR |
1856 | return; |
1857 | } | |
1858 | ||
1859 | static int mc_interception(struct vcpu_svm *svm) | |
1860 | { | |
53371b50 JR |
1861 | return 1; |
1862 | } | |
1863 | ||
851ba692 | 1864 | static int shutdown_interception(struct vcpu_svm *svm) |
46fe4ddd | 1865 | { |
851ba692 AK |
1866 | struct kvm_run *kvm_run = svm->vcpu.run; |
1867 | ||
46fe4ddd JR |
1868 | /* |
1869 | * VMCB is undefined after a SHUTDOWN intercept | |
1870 | * so reinitialize it. | |
1871 | */ | |
a2fa3e9f | 1872 | clear_page(svm->vmcb); |
e6101a96 | 1873 | init_vmcb(svm); |
46fe4ddd JR |
1874 | |
1875 | kvm_run->exit_reason = KVM_EXIT_SHUTDOWN; | |
1876 | return 0; | |
1877 | } | |
1878 | ||
851ba692 | 1879 | static int io_interception(struct vcpu_svm *svm) |
6aa8b732 | 1880 | { |
cf8f70bf | 1881 | struct kvm_vcpu *vcpu = &svm->vcpu; |
d77c26fc | 1882 | u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */ |
34c33d16 | 1883 | int size, in, string; |
039576c0 | 1884 | unsigned port; |
6aa8b732 | 1885 | |
e756fc62 | 1886 | ++svm->vcpu.stat.io_exits; |
e70669ab | 1887 | string = (io_info & SVM_IOIO_STR_MASK) != 0; |
039576c0 | 1888 | in = (io_info & SVM_IOIO_TYPE_MASK) != 0; |
cf8f70bf | 1889 | if (string || in) |
51d8b661 | 1890 | return emulate_instruction(vcpu, 0) == EMULATE_DONE; |
cf8f70bf | 1891 | |
039576c0 AK |
1892 | port = io_info >> 16; |
1893 | size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT; | |
cf8f70bf | 1894 | svm->next_rip = svm->vmcb->control.exit_info_2; |
e93f36bc | 1895 | skip_emulated_instruction(&svm->vcpu); |
cf8f70bf GN |
1896 | |
1897 | return kvm_fast_pio_out(vcpu, size, port); | |
6aa8b732 AK |
1898 | } |
1899 | ||
851ba692 | 1900 | static int nmi_interception(struct vcpu_svm *svm) |
c47f098d JR |
1901 | { |
1902 | return 1; | |
1903 | } | |
1904 | ||
851ba692 | 1905 | static int intr_interception(struct vcpu_svm *svm) |
a0698055 JR |
1906 | { |
1907 | ++svm->vcpu.stat.irq_exits; | |
1908 | return 1; | |
1909 | } | |
1910 | ||
851ba692 | 1911 | static int nop_on_interception(struct vcpu_svm *svm) |
6aa8b732 AK |
1912 | { |
1913 | return 1; | |
1914 | } | |
1915 | ||
851ba692 | 1916 | static int halt_interception(struct vcpu_svm *svm) |
6aa8b732 | 1917 | { |
5fdbf976 | 1918 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 1; |
e756fc62 RR |
1919 | skip_emulated_instruction(&svm->vcpu); |
1920 | return kvm_emulate_halt(&svm->vcpu); | |
6aa8b732 AK |
1921 | } |
1922 | ||
851ba692 | 1923 | static int vmmcall_interception(struct vcpu_svm *svm) |
02e235bc | 1924 | { |
5fdbf976 | 1925 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
e756fc62 | 1926 | skip_emulated_instruction(&svm->vcpu); |
7aa81cc0 AL |
1927 | kvm_emulate_hypercall(&svm->vcpu); |
1928 | return 1; | |
02e235bc AK |
1929 | } |
1930 | ||
5bd2edc3 JR |
1931 | static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu) |
1932 | { | |
1933 | struct vcpu_svm *svm = to_svm(vcpu); | |
1934 | ||
1935 | return svm->nested.nested_cr3; | |
1936 | } | |
1937 | ||
e4e517b4 AK |
1938 | static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index) |
1939 | { | |
1940 | struct vcpu_svm *svm = to_svm(vcpu); | |
1941 | u64 cr3 = svm->nested.nested_cr3; | |
1942 | u64 pdpte; | |
1943 | int ret; | |
1944 | ||
1945 | ret = kvm_read_guest_page(vcpu->kvm, gpa_to_gfn(cr3), &pdpte, | |
1946 | offset_in_page(cr3) + index * 8, 8); | |
1947 | if (ret) | |
1948 | return 0; | |
1949 | return pdpte; | |
1950 | } | |
1951 | ||
5bd2edc3 JR |
1952 | static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu, |
1953 | unsigned long root) | |
1954 | { | |
1955 | struct vcpu_svm *svm = to_svm(vcpu); | |
1956 | ||
1957 | svm->vmcb->control.nested_cr3 = root; | |
b2747166 | 1958 | mark_dirty(svm->vmcb, VMCB_NPT); |
f40f6a45 | 1959 | svm_flush_tlb(vcpu); |
5bd2edc3 JR |
1960 | } |
1961 | ||
6389ee94 AK |
1962 | static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu, |
1963 | struct x86_exception *fault) | |
5bd2edc3 JR |
1964 | { |
1965 | struct vcpu_svm *svm = to_svm(vcpu); | |
1966 | ||
1967 | svm->vmcb->control.exit_code = SVM_EXIT_NPF; | |
1968 | svm->vmcb->control.exit_code_hi = 0; | |
6389ee94 AK |
1969 | svm->vmcb->control.exit_info_1 = fault->error_code; |
1970 | svm->vmcb->control.exit_info_2 = fault->address; | |
5bd2edc3 JR |
1971 | |
1972 | nested_svm_vmexit(svm); | |
1973 | } | |
1974 | ||
4b16184c JR |
1975 | static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu) |
1976 | { | |
1977 | int r; | |
1978 | ||
1979 | r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu); | |
1980 | ||
1981 | vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3; | |
1982 | vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3; | |
e4e517b4 | 1983 | vcpu->arch.mmu.get_pdptr = nested_svm_get_tdp_pdptr; |
4b16184c JR |
1984 | vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit; |
1985 | vcpu->arch.mmu.shadow_root_level = get_npt_level(); | |
1986 | vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu; | |
1987 | ||
1988 | return r; | |
1989 | } | |
1990 | ||
1991 | static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu) | |
1992 | { | |
1993 | vcpu->arch.walk_mmu = &vcpu->arch.mmu; | |
1994 | } | |
1995 | ||
c0725420 AG |
1996 | static int nested_svm_check_permissions(struct vcpu_svm *svm) |
1997 | { | |
f6801dff | 1998 | if (!(svm->vcpu.arch.efer & EFER_SVME) |
c0725420 AG |
1999 | || !is_paging(&svm->vcpu)) { |
2000 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
2001 | return 1; | |
2002 | } | |
2003 | ||
2004 | if (svm->vmcb->save.cpl) { | |
2005 | kvm_inject_gp(&svm->vcpu, 0); | |
2006 | return 1; | |
2007 | } | |
2008 | ||
2009 | return 0; | |
2010 | } | |
2011 | ||
cf74a78b AG |
2012 | static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr, |
2013 | bool has_error_code, u32 error_code) | |
2014 | { | |
b8e88bc8 JR |
2015 | int vmexit; |
2016 | ||
2030753d | 2017 | if (!is_guest_mode(&svm->vcpu)) |
0295ad7d | 2018 | return 0; |
cf74a78b | 2019 | |
0295ad7d JR |
2020 | svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr; |
2021 | svm->vmcb->control.exit_code_hi = 0; | |
2022 | svm->vmcb->control.exit_info_1 = error_code; | |
2023 | svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2; | |
2024 | ||
b8e88bc8 JR |
2025 | vmexit = nested_svm_intercept(svm); |
2026 | if (vmexit == NESTED_EXIT_DONE) | |
2027 | svm->nested.exit_required = true; | |
2028 | ||
2029 | return vmexit; | |
cf74a78b AG |
2030 | } |
2031 | ||
8fe54654 JR |
2032 | /* This function returns true if it is save to enable the irq window */ |
2033 | static inline bool nested_svm_intr(struct vcpu_svm *svm) | |
cf74a78b | 2034 | { |
2030753d | 2035 | if (!is_guest_mode(&svm->vcpu)) |
8fe54654 | 2036 | return true; |
cf74a78b | 2037 | |
26666957 | 2038 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) |
8fe54654 | 2039 | return true; |
cf74a78b | 2040 | |
26666957 | 2041 | if (!(svm->vcpu.arch.hflags & HF_HIF_MASK)) |
8fe54654 | 2042 | return false; |
cf74a78b | 2043 | |
a0a07cd2 GN |
2044 | /* |
2045 | * if vmexit was already requested (by intercepted exception | |
2046 | * for instance) do not overwrite it with "external interrupt" | |
2047 | * vmexit. | |
2048 | */ | |
2049 | if (svm->nested.exit_required) | |
2050 | return false; | |
2051 | ||
197717d5 JR |
2052 | svm->vmcb->control.exit_code = SVM_EXIT_INTR; |
2053 | svm->vmcb->control.exit_info_1 = 0; | |
2054 | svm->vmcb->control.exit_info_2 = 0; | |
26666957 | 2055 | |
cd3ff653 JR |
2056 | if (svm->nested.intercept & 1ULL) { |
2057 | /* | |
2058 | * The #vmexit can't be emulated here directly because this | |
2059 | * code path runs with irqs and preemtion disabled. A | |
2060 | * #vmexit emulation might sleep. Only signal request for | |
2061 | * the #vmexit here. | |
2062 | */ | |
2063 | svm->nested.exit_required = true; | |
236649de | 2064 | trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip); |
8fe54654 | 2065 | return false; |
cf74a78b AG |
2066 | } |
2067 | ||
8fe54654 | 2068 | return true; |
cf74a78b AG |
2069 | } |
2070 | ||
887f500c JR |
2071 | /* This function returns true if it is save to enable the nmi window */ |
2072 | static inline bool nested_svm_nmi(struct vcpu_svm *svm) | |
2073 | { | |
2030753d | 2074 | if (!is_guest_mode(&svm->vcpu)) |
887f500c JR |
2075 | return true; |
2076 | ||
2077 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI))) | |
2078 | return true; | |
2079 | ||
2080 | svm->vmcb->control.exit_code = SVM_EXIT_NMI; | |
2081 | svm->nested.exit_required = true; | |
2082 | ||
2083 | return false; | |
cf74a78b AG |
2084 | } |
2085 | ||
7597f129 | 2086 | static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page) |
34f80cfa JR |
2087 | { |
2088 | struct page *page; | |
2089 | ||
6c3bd3d7 JR |
2090 | might_sleep(); |
2091 | ||
34f80cfa | 2092 | page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT); |
34f80cfa JR |
2093 | if (is_error_page(page)) |
2094 | goto error; | |
2095 | ||
7597f129 JR |
2096 | *_page = page; |
2097 | ||
2098 | return kmap(page); | |
34f80cfa JR |
2099 | |
2100 | error: | |
2101 | kvm_release_page_clean(page); | |
2102 | kvm_inject_gp(&svm->vcpu, 0); | |
2103 | ||
2104 | return NULL; | |
2105 | } | |
2106 | ||
7597f129 | 2107 | static void nested_svm_unmap(struct page *page) |
34f80cfa | 2108 | { |
7597f129 | 2109 | kunmap(page); |
34f80cfa JR |
2110 | kvm_release_page_dirty(page); |
2111 | } | |
34f80cfa | 2112 | |
ce2ac085 JR |
2113 | static int nested_svm_intercept_ioio(struct vcpu_svm *svm) |
2114 | { | |
2115 | unsigned port; | |
2116 | u8 val, bit; | |
2117 | u64 gpa; | |
34f80cfa | 2118 | |
ce2ac085 JR |
2119 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT))) |
2120 | return NESTED_EXIT_HOST; | |
34f80cfa | 2121 | |
ce2ac085 JR |
2122 | port = svm->vmcb->control.exit_info_1 >> 16; |
2123 | gpa = svm->nested.vmcb_iopm + (port / 8); | |
2124 | bit = port % 8; | |
2125 | val = 0; | |
2126 | ||
2127 | if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1)) | |
2128 | val &= (1 << bit); | |
2129 | ||
2130 | return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; | |
34f80cfa JR |
2131 | } |
2132 | ||
d2477826 | 2133 | static int nested_svm_exit_handled_msr(struct vcpu_svm *svm) |
4c2161ae | 2134 | { |
0d6b3537 JR |
2135 | u32 offset, msr, value; |
2136 | int write, mask; | |
4c2161ae | 2137 | |
3d62d9aa | 2138 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
d2477826 | 2139 | return NESTED_EXIT_HOST; |
3d62d9aa | 2140 | |
0d6b3537 JR |
2141 | msr = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
2142 | offset = svm_msrpm_offset(msr); | |
2143 | write = svm->vmcb->control.exit_info_1 & 1; | |
2144 | mask = 1 << ((2 * (msr & 0xf)) + write); | |
3d62d9aa | 2145 | |
0d6b3537 JR |
2146 | if (offset == MSR_INVALID) |
2147 | return NESTED_EXIT_DONE; | |
4c2161ae | 2148 | |
0d6b3537 JR |
2149 | /* Offset is in 32 bit units but need in 8 bit units */ |
2150 | offset *= 4; | |
4c2161ae | 2151 | |
0d6b3537 JR |
2152 | if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4)) |
2153 | return NESTED_EXIT_DONE; | |
3d62d9aa | 2154 | |
0d6b3537 | 2155 | return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST; |
4c2161ae JR |
2156 | } |
2157 | ||
410e4d57 | 2158 | static int nested_svm_exit_special(struct vcpu_svm *svm) |
cf74a78b | 2159 | { |
cf74a78b | 2160 | u32 exit_code = svm->vmcb->control.exit_code; |
4c2161ae | 2161 | |
410e4d57 JR |
2162 | switch (exit_code) { |
2163 | case SVM_EXIT_INTR: | |
2164 | case SVM_EXIT_NMI: | |
ff47a49b | 2165 | case SVM_EXIT_EXCP_BASE + MC_VECTOR: |
410e4d57 | 2166 | return NESTED_EXIT_HOST; |
410e4d57 | 2167 | case SVM_EXIT_NPF: |
e0231715 | 2168 | /* For now we are always handling NPFs when using them */ |
410e4d57 JR |
2169 | if (npt_enabled) |
2170 | return NESTED_EXIT_HOST; | |
2171 | break; | |
410e4d57 | 2172 | case SVM_EXIT_EXCP_BASE + PF_VECTOR: |
631bc487 GN |
2173 | /* When we're shadowing, trap PFs, but not async PF */ |
2174 | if (!npt_enabled && svm->apf_reason == 0) | |
410e4d57 JR |
2175 | return NESTED_EXIT_HOST; |
2176 | break; | |
66a562f7 JR |
2177 | case SVM_EXIT_EXCP_BASE + NM_VECTOR: |
2178 | nm_interception(svm); | |
2179 | break; | |
410e4d57 JR |
2180 | default: |
2181 | break; | |
cf74a78b AG |
2182 | } |
2183 | ||
410e4d57 JR |
2184 | return NESTED_EXIT_CONTINUE; |
2185 | } | |
2186 | ||
2187 | /* | |
2188 | * If this function returns true, this #vmexit was already handled | |
2189 | */ | |
b8e88bc8 | 2190 | static int nested_svm_intercept(struct vcpu_svm *svm) |
410e4d57 JR |
2191 | { |
2192 | u32 exit_code = svm->vmcb->control.exit_code; | |
2193 | int vmexit = NESTED_EXIT_HOST; | |
2194 | ||
cf74a78b | 2195 | switch (exit_code) { |
9c4e40b9 | 2196 | case SVM_EXIT_MSR: |
3d62d9aa | 2197 | vmexit = nested_svm_exit_handled_msr(svm); |
9c4e40b9 | 2198 | break; |
ce2ac085 JR |
2199 | case SVM_EXIT_IOIO: |
2200 | vmexit = nested_svm_intercept_ioio(svm); | |
2201 | break; | |
4ee546b4 RJ |
2202 | case SVM_EXIT_READ_CR0 ... SVM_EXIT_WRITE_CR8: { |
2203 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_CR0); | |
2204 | if (svm->nested.intercept_cr & bit) | |
410e4d57 | 2205 | vmexit = NESTED_EXIT_DONE; |
cf74a78b AG |
2206 | break; |
2207 | } | |
3aed041a JR |
2208 | case SVM_EXIT_READ_DR0 ... SVM_EXIT_WRITE_DR7: { |
2209 | u32 bit = 1U << (exit_code - SVM_EXIT_READ_DR0); | |
2210 | if (svm->nested.intercept_dr & bit) | |
410e4d57 | 2211 | vmexit = NESTED_EXIT_DONE; |
cf74a78b AG |
2212 | break; |
2213 | } | |
2214 | case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: { | |
2215 | u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE); | |
aad42c64 | 2216 | if (svm->nested.intercept_exceptions & excp_bits) |
410e4d57 | 2217 | vmexit = NESTED_EXIT_DONE; |
631bc487 GN |
2218 | /* async page fault always cause vmexit */ |
2219 | else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) && | |
2220 | svm->apf_reason != 0) | |
2221 | vmexit = NESTED_EXIT_DONE; | |
cf74a78b AG |
2222 | break; |
2223 | } | |
228070b1 JR |
2224 | case SVM_EXIT_ERR: { |
2225 | vmexit = NESTED_EXIT_DONE; | |
2226 | break; | |
2227 | } | |
cf74a78b AG |
2228 | default: { |
2229 | u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR); | |
aad42c64 | 2230 | if (svm->nested.intercept & exit_bits) |
410e4d57 | 2231 | vmexit = NESTED_EXIT_DONE; |
cf74a78b AG |
2232 | } |
2233 | } | |
2234 | ||
b8e88bc8 JR |
2235 | return vmexit; |
2236 | } | |
2237 | ||
2238 | static int nested_svm_exit_handled(struct vcpu_svm *svm) | |
2239 | { | |
2240 | int vmexit; | |
2241 | ||
2242 | vmexit = nested_svm_intercept(svm); | |
2243 | ||
2244 | if (vmexit == NESTED_EXIT_DONE) | |
9c4e40b9 | 2245 | nested_svm_vmexit(svm); |
9c4e40b9 JR |
2246 | |
2247 | return vmexit; | |
cf74a78b AG |
2248 | } |
2249 | ||
0460a979 JR |
2250 | static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb) |
2251 | { | |
2252 | struct vmcb_control_area *dst = &dst_vmcb->control; | |
2253 | struct vmcb_control_area *from = &from_vmcb->control; | |
2254 | ||
4ee546b4 | 2255 | dst->intercept_cr = from->intercept_cr; |
3aed041a | 2256 | dst->intercept_dr = from->intercept_dr; |
0460a979 JR |
2257 | dst->intercept_exceptions = from->intercept_exceptions; |
2258 | dst->intercept = from->intercept; | |
2259 | dst->iopm_base_pa = from->iopm_base_pa; | |
2260 | dst->msrpm_base_pa = from->msrpm_base_pa; | |
2261 | dst->tsc_offset = from->tsc_offset; | |
2262 | dst->asid = from->asid; | |
2263 | dst->tlb_ctl = from->tlb_ctl; | |
2264 | dst->int_ctl = from->int_ctl; | |
2265 | dst->int_vector = from->int_vector; | |
2266 | dst->int_state = from->int_state; | |
2267 | dst->exit_code = from->exit_code; | |
2268 | dst->exit_code_hi = from->exit_code_hi; | |
2269 | dst->exit_info_1 = from->exit_info_1; | |
2270 | dst->exit_info_2 = from->exit_info_2; | |
2271 | dst->exit_int_info = from->exit_int_info; | |
2272 | dst->exit_int_info_err = from->exit_int_info_err; | |
2273 | dst->nested_ctl = from->nested_ctl; | |
2274 | dst->event_inj = from->event_inj; | |
2275 | dst->event_inj_err = from->event_inj_err; | |
2276 | dst->nested_cr3 = from->nested_cr3; | |
2277 | dst->lbr_ctl = from->lbr_ctl; | |
2278 | } | |
2279 | ||
34f80cfa | 2280 | static int nested_svm_vmexit(struct vcpu_svm *svm) |
cf74a78b | 2281 | { |
34f80cfa | 2282 | struct vmcb *nested_vmcb; |
e6aa9abd | 2283 | struct vmcb *hsave = svm->nested.hsave; |
33740e40 | 2284 | struct vmcb *vmcb = svm->vmcb; |
7597f129 | 2285 | struct page *page; |
cf74a78b | 2286 | |
17897f36 JR |
2287 | trace_kvm_nested_vmexit_inject(vmcb->control.exit_code, |
2288 | vmcb->control.exit_info_1, | |
2289 | vmcb->control.exit_info_2, | |
2290 | vmcb->control.exit_int_info, | |
e097e5ff SH |
2291 | vmcb->control.exit_int_info_err, |
2292 | KVM_ISA_SVM); | |
17897f36 | 2293 | |
7597f129 | 2294 | nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page); |
34f80cfa JR |
2295 | if (!nested_vmcb) |
2296 | return 1; | |
2297 | ||
2030753d JR |
2298 | /* Exit Guest-Mode */ |
2299 | leave_guest_mode(&svm->vcpu); | |
06fc7772 JR |
2300 | svm->nested.vmcb = 0; |
2301 | ||
cf74a78b | 2302 | /* Give the current vmcb to the guest */ |
33740e40 JR |
2303 | disable_gif(svm); |
2304 | ||
2305 | nested_vmcb->save.es = vmcb->save.es; | |
2306 | nested_vmcb->save.cs = vmcb->save.cs; | |
2307 | nested_vmcb->save.ss = vmcb->save.ss; | |
2308 | nested_vmcb->save.ds = vmcb->save.ds; | |
2309 | nested_vmcb->save.gdtr = vmcb->save.gdtr; | |
2310 | nested_vmcb->save.idtr = vmcb->save.idtr; | |
3f6a9d16 | 2311 | nested_vmcb->save.efer = svm->vcpu.arch.efer; |
cdbbdc12 | 2312 | nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu); |
9f8fe504 | 2313 | nested_vmcb->save.cr3 = kvm_read_cr3(&svm->vcpu); |
33740e40 | 2314 | nested_vmcb->save.cr2 = vmcb->save.cr2; |
cdbbdc12 | 2315 | nested_vmcb->save.cr4 = svm->vcpu.arch.cr4; |
f6e78475 | 2316 | nested_vmcb->save.rflags = kvm_get_rflags(&svm->vcpu); |
33740e40 JR |
2317 | nested_vmcb->save.rip = vmcb->save.rip; |
2318 | nested_vmcb->save.rsp = vmcb->save.rsp; | |
2319 | nested_vmcb->save.rax = vmcb->save.rax; | |
2320 | nested_vmcb->save.dr7 = vmcb->save.dr7; | |
2321 | nested_vmcb->save.dr6 = vmcb->save.dr6; | |
2322 | nested_vmcb->save.cpl = vmcb->save.cpl; | |
2323 | ||
2324 | nested_vmcb->control.int_ctl = vmcb->control.int_ctl; | |
2325 | nested_vmcb->control.int_vector = vmcb->control.int_vector; | |
2326 | nested_vmcb->control.int_state = vmcb->control.int_state; | |
2327 | nested_vmcb->control.exit_code = vmcb->control.exit_code; | |
2328 | nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi; | |
2329 | nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1; | |
2330 | nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2; | |
2331 | nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info; | |
2332 | nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err; | |
7a190667 | 2333 | nested_vmcb->control.next_rip = vmcb->control.next_rip; |
8d23c466 AG |
2334 | |
2335 | /* | |
2336 | * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have | |
2337 | * to make sure that we do not lose injected events. So check event_inj | |
2338 | * here and copy it to exit_int_info if it is valid. | |
2339 | * Exit_int_info and event_inj can't be both valid because the case | |
2340 | * below only happens on a VMRUN instruction intercept which has | |
2341 | * no valid exit_int_info set. | |
2342 | */ | |
2343 | if (vmcb->control.event_inj & SVM_EVTINJ_VALID) { | |
2344 | struct vmcb_control_area *nc = &nested_vmcb->control; | |
2345 | ||
2346 | nc->exit_int_info = vmcb->control.event_inj; | |
2347 | nc->exit_int_info_err = vmcb->control.event_inj_err; | |
2348 | } | |
2349 | ||
33740e40 JR |
2350 | nested_vmcb->control.tlb_ctl = 0; |
2351 | nested_vmcb->control.event_inj = 0; | |
2352 | nested_vmcb->control.event_inj_err = 0; | |
cf74a78b AG |
2353 | |
2354 | /* We always set V_INTR_MASKING and remember the old value in hflags */ | |
2355 | if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK)) | |
2356 | nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK; | |
2357 | ||
cf74a78b | 2358 | /* Restore the original control entries */ |
0460a979 | 2359 | copy_vmcb_control_area(vmcb, hsave); |
cf74a78b | 2360 | |
219b65dc AG |
2361 | kvm_clear_exception_queue(&svm->vcpu); |
2362 | kvm_clear_interrupt_queue(&svm->vcpu); | |
cf74a78b | 2363 | |
4b16184c JR |
2364 | svm->nested.nested_cr3 = 0; |
2365 | ||
cf74a78b AG |
2366 | /* Restore selected save entries */ |
2367 | svm->vmcb->save.es = hsave->save.es; | |
2368 | svm->vmcb->save.cs = hsave->save.cs; | |
2369 | svm->vmcb->save.ss = hsave->save.ss; | |
2370 | svm->vmcb->save.ds = hsave->save.ds; | |
2371 | svm->vmcb->save.gdtr = hsave->save.gdtr; | |
2372 | svm->vmcb->save.idtr = hsave->save.idtr; | |
f6e78475 | 2373 | kvm_set_rflags(&svm->vcpu, hsave->save.rflags); |
cf74a78b AG |
2374 | svm_set_efer(&svm->vcpu, hsave->save.efer); |
2375 | svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE); | |
2376 | svm_set_cr4(&svm->vcpu, hsave->save.cr4); | |
2377 | if (npt_enabled) { | |
2378 | svm->vmcb->save.cr3 = hsave->save.cr3; | |
2379 | svm->vcpu.arch.cr3 = hsave->save.cr3; | |
2380 | } else { | |
2390218b | 2381 | (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3); |
cf74a78b AG |
2382 | } |
2383 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax); | |
2384 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp); | |
2385 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip); | |
2386 | svm->vmcb->save.dr7 = 0; | |
2387 | svm->vmcb->save.cpl = 0; | |
2388 | svm->vmcb->control.exit_int_info = 0; | |
2389 | ||
8d28fec4 RJ |
2390 | mark_all_dirty(svm->vmcb); |
2391 | ||
7597f129 | 2392 | nested_svm_unmap(page); |
cf74a78b | 2393 | |
4b16184c | 2394 | nested_svm_uninit_mmu_context(&svm->vcpu); |
cf74a78b AG |
2395 | kvm_mmu_reset_context(&svm->vcpu); |
2396 | kvm_mmu_load(&svm->vcpu); | |
2397 | ||
2398 | return 0; | |
2399 | } | |
3d6368ef | 2400 | |
9738b2c9 | 2401 | static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm) |
3d6368ef | 2402 | { |
323c3d80 JR |
2403 | /* |
2404 | * This function merges the msr permission bitmaps of kvm and the | |
2405 | * nested vmcb. It is omptimized in that it only merges the parts where | |
2406 | * the kvm msr permission bitmap may contain zero bits | |
2407 | */ | |
3d6368ef | 2408 | int i; |
9738b2c9 | 2409 | |
323c3d80 JR |
2410 | if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT))) |
2411 | return true; | |
9738b2c9 | 2412 | |
323c3d80 JR |
2413 | for (i = 0; i < MSRPM_OFFSETS; i++) { |
2414 | u32 value, p; | |
2415 | u64 offset; | |
9738b2c9 | 2416 | |
323c3d80 JR |
2417 | if (msrpm_offsets[i] == 0xffffffff) |
2418 | break; | |
3d6368ef | 2419 | |
0d6b3537 JR |
2420 | p = msrpm_offsets[i]; |
2421 | offset = svm->nested.vmcb_msrpm + (p * 4); | |
323c3d80 JR |
2422 | |
2423 | if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4)) | |
2424 | return false; | |
2425 | ||
2426 | svm->nested.msrpm[p] = svm->msrpm[p] | value; | |
2427 | } | |
3d6368ef | 2428 | |
323c3d80 | 2429 | svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm); |
9738b2c9 JR |
2430 | |
2431 | return true; | |
3d6368ef AG |
2432 | } |
2433 | ||
52c65a30 JR |
2434 | static bool nested_vmcb_checks(struct vmcb *vmcb) |
2435 | { | |
2436 | if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0) | |
2437 | return false; | |
2438 | ||
dbe77584 JR |
2439 | if (vmcb->control.asid == 0) |
2440 | return false; | |
2441 | ||
4b16184c JR |
2442 | if (vmcb->control.nested_ctl && !npt_enabled) |
2443 | return false; | |
2444 | ||
52c65a30 JR |
2445 | return true; |
2446 | } | |
2447 | ||
9738b2c9 | 2448 | static bool nested_svm_vmrun(struct vcpu_svm *svm) |
3d6368ef | 2449 | { |
9738b2c9 | 2450 | struct vmcb *nested_vmcb; |
e6aa9abd | 2451 | struct vmcb *hsave = svm->nested.hsave; |
defbba56 | 2452 | struct vmcb *vmcb = svm->vmcb; |
7597f129 | 2453 | struct page *page; |
06fc7772 | 2454 | u64 vmcb_gpa; |
3d6368ef | 2455 | |
06fc7772 | 2456 | vmcb_gpa = svm->vmcb->save.rax; |
3d6368ef | 2457 | |
7597f129 | 2458 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
9738b2c9 JR |
2459 | if (!nested_vmcb) |
2460 | return false; | |
2461 | ||
52c65a30 JR |
2462 | if (!nested_vmcb_checks(nested_vmcb)) { |
2463 | nested_vmcb->control.exit_code = SVM_EXIT_ERR; | |
2464 | nested_vmcb->control.exit_code_hi = 0; | |
2465 | nested_vmcb->control.exit_info_1 = 0; | |
2466 | nested_vmcb->control.exit_info_2 = 0; | |
2467 | ||
2468 | nested_svm_unmap(page); | |
2469 | ||
2470 | return false; | |
2471 | } | |
2472 | ||
b75f4eb3 | 2473 | trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa, |
0ac406de JR |
2474 | nested_vmcb->save.rip, |
2475 | nested_vmcb->control.int_ctl, | |
2476 | nested_vmcb->control.event_inj, | |
2477 | nested_vmcb->control.nested_ctl); | |
2478 | ||
4ee546b4 RJ |
2479 | trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr & 0xffff, |
2480 | nested_vmcb->control.intercept_cr >> 16, | |
2e554e8d JR |
2481 | nested_vmcb->control.intercept_exceptions, |
2482 | nested_vmcb->control.intercept); | |
2483 | ||
3d6368ef | 2484 | /* Clear internal status */ |
219b65dc AG |
2485 | kvm_clear_exception_queue(&svm->vcpu); |
2486 | kvm_clear_interrupt_queue(&svm->vcpu); | |
3d6368ef | 2487 | |
e0231715 JR |
2488 | /* |
2489 | * Save the old vmcb, so we don't need to pick what we save, but can | |
2490 | * restore everything when a VMEXIT occurs | |
2491 | */ | |
defbba56 JR |
2492 | hsave->save.es = vmcb->save.es; |
2493 | hsave->save.cs = vmcb->save.cs; | |
2494 | hsave->save.ss = vmcb->save.ss; | |
2495 | hsave->save.ds = vmcb->save.ds; | |
2496 | hsave->save.gdtr = vmcb->save.gdtr; | |
2497 | hsave->save.idtr = vmcb->save.idtr; | |
f6801dff | 2498 | hsave->save.efer = svm->vcpu.arch.efer; |
4d4ec087 | 2499 | hsave->save.cr0 = kvm_read_cr0(&svm->vcpu); |
defbba56 | 2500 | hsave->save.cr4 = svm->vcpu.arch.cr4; |
f6e78475 | 2501 | hsave->save.rflags = kvm_get_rflags(&svm->vcpu); |
b75f4eb3 | 2502 | hsave->save.rip = kvm_rip_read(&svm->vcpu); |
defbba56 JR |
2503 | hsave->save.rsp = vmcb->save.rsp; |
2504 | hsave->save.rax = vmcb->save.rax; | |
2505 | if (npt_enabled) | |
2506 | hsave->save.cr3 = vmcb->save.cr3; | |
2507 | else | |
9f8fe504 | 2508 | hsave->save.cr3 = kvm_read_cr3(&svm->vcpu); |
defbba56 | 2509 | |
0460a979 | 2510 | copy_vmcb_control_area(hsave, vmcb); |
3d6368ef | 2511 | |
f6e78475 | 2512 | if (kvm_get_rflags(&svm->vcpu) & X86_EFLAGS_IF) |
3d6368ef AG |
2513 | svm->vcpu.arch.hflags |= HF_HIF_MASK; |
2514 | else | |
2515 | svm->vcpu.arch.hflags &= ~HF_HIF_MASK; | |
2516 | ||
4b16184c JR |
2517 | if (nested_vmcb->control.nested_ctl) { |
2518 | kvm_mmu_unload(&svm->vcpu); | |
2519 | svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3; | |
2520 | nested_svm_init_mmu_context(&svm->vcpu); | |
2521 | } | |
2522 | ||
3d6368ef AG |
2523 | /* Load the nested guest state */ |
2524 | svm->vmcb->save.es = nested_vmcb->save.es; | |
2525 | svm->vmcb->save.cs = nested_vmcb->save.cs; | |
2526 | svm->vmcb->save.ss = nested_vmcb->save.ss; | |
2527 | svm->vmcb->save.ds = nested_vmcb->save.ds; | |
2528 | svm->vmcb->save.gdtr = nested_vmcb->save.gdtr; | |
2529 | svm->vmcb->save.idtr = nested_vmcb->save.idtr; | |
f6e78475 | 2530 | kvm_set_rflags(&svm->vcpu, nested_vmcb->save.rflags); |
3d6368ef AG |
2531 | svm_set_efer(&svm->vcpu, nested_vmcb->save.efer); |
2532 | svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0); | |
2533 | svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4); | |
2534 | if (npt_enabled) { | |
2535 | svm->vmcb->save.cr3 = nested_vmcb->save.cr3; | |
2536 | svm->vcpu.arch.cr3 = nested_vmcb->save.cr3; | |
0e5cbe36 | 2537 | } else |
2390218b | 2538 | (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3); |
0e5cbe36 JR |
2539 | |
2540 | /* Guest paging mode is active - reset mmu */ | |
2541 | kvm_mmu_reset_context(&svm->vcpu); | |
2542 | ||
defbba56 | 2543 | svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2; |
3d6368ef AG |
2544 | kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax); |
2545 | kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp); | |
2546 | kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip); | |
e0231715 | 2547 | |
3d6368ef AG |
2548 | /* In case we don't even reach vcpu_run, the fields are not updated */ |
2549 | svm->vmcb->save.rax = nested_vmcb->save.rax; | |
2550 | svm->vmcb->save.rsp = nested_vmcb->save.rsp; | |
2551 | svm->vmcb->save.rip = nested_vmcb->save.rip; | |
2552 | svm->vmcb->save.dr7 = nested_vmcb->save.dr7; | |
2553 | svm->vmcb->save.dr6 = nested_vmcb->save.dr6; | |
2554 | svm->vmcb->save.cpl = nested_vmcb->save.cpl; | |
2555 | ||
f7138538 | 2556 | svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL; |
ce2ac085 | 2557 | svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL; |
3d6368ef | 2558 | |
aad42c64 | 2559 | /* cache intercepts */ |
4ee546b4 | 2560 | svm->nested.intercept_cr = nested_vmcb->control.intercept_cr; |
3aed041a | 2561 | svm->nested.intercept_dr = nested_vmcb->control.intercept_dr; |
aad42c64 JR |
2562 | svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions; |
2563 | svm->nested.intercept = nested_vmcb->control.intercept; | |
2564 | ||
f40f6a45 | 2565 | svm_flush_tlb(&svm->vcpu); |
3d6368ef | 2566 | svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK; |
3d6368ef AG |
2567 | if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK) |
2568 | svm->vcpu.arch.hflags |= HF_VINTR_MASK; | |
2569 | else | |
2570 | svm->vcpu.arch.hflags &= ~HF_VINTR_MASK; | |
2571 | ||
88ab24ad JR |
2572 | if (svm->vcpu.arch.hflags & HF_VINTR_MASK) { |
2573 | /* We only want the cr8 intercept bits of the guest */ | |
4ee546b4 RJ |
2574 | clr_cr_intercept(svm, INTERCEPT_CR8_READ); |
2575 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); | |
88ab24ad JR |
2576 | } |
2577 | ||
0d945bd9 | 2578 | /* We don't want to see VMMCALLs from a nested guest */ |
8a05a1b8 | 2579 | clr_intercept(svm, INTERCEPT_VMMCALL); |
0d945bd9 | 2580 | |
88ab24ad | 2581 | svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl; |
3d6368ef AG |
2582 | svm->vmcb->control.int_vector = nested_vmcb->control.int_vector; |
2583 | svm->vmcb->control.int_state = nested_vmcb->control.int_state; | |
2584 | svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset; | |
3d6368ef AG |
2585 | svm->vmcb->control.event_inj = nested_vmcb->control.event_inj; |
2586 | svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err; | |
2587 | ||
7597f129 | 2588 | nested_svm_unmap(page); |
9738b2c9 | 2589 | |
2030753d JR |
2590 | /* Enter Guest-Mode */ |
2591 | enter_guest_mode(&svm->vcpu); | |
2592 | ||
384c6368 JR |
2593 | /* |
2594 | * Merge guest and host intercepts - must be called with vcpu in | |
2595 | * guest-mode to take affect here | |
2596 | */ | |
2597 | recalc_intercepts(svm); | |
2598 | ||
06fc7772 | 2599 | svm->nested.vmcb = vmcb_gpa; |
9738b2c9 | 2600 | |
2af9194d | 2601 | enable_gif(svm); |
3d6368ef | 2602 | |
8d28fec4 RJ |
2603 | mark_all_dirty(svm->vmcb); |
2604 | ||
9738b2c9 | 2605 | return true; |
3d6368ef AG |
2606 | } |
2607 | ||
9966bf68 | 2608 | static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb) |
5542675b AG |
2609 | { |
2610 | to_vmcb->save.fs = from_vmcb->save.fs; | |
2611 | to_vmcb->save.gs = from_vmcb->save.gs; | |
2612 | to_vmcb->save.tr = from_vmcb->save.tr; | |
2613 | to_vmcb->save.ldtr = from_vmcb->save.ldtr; | |
2614 | to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base; | |
2615 | to_vmcb->save.star = from_vmcb->save.star; | |
2616 | to_vmcb->save.lstar = from_vmcb->save.lstar; | |
2617 | to_vmcb->save.cstar = from_vmcb->save.cstar; | |
2618 | to_vmcb->save.sfmask = from_vmcb->save.sfmask; | |
2619 | to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs; | |
2620 | to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp; | |
2621 | to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip; | |
5542675b AG |
2622 | } |
2623 | ||
851ba692 | 2624 | static int vmload_interception(struct vcpu_svm *svm) |
5542675b | 2625 | { |
9966bf68 | 2626 | struct vmcb *nested_vmcb; |
7597f129 | 2627 | struct page *page; |
9966bf68 | 2628 | |
5542675b AG |
2629 | if (nested_svm_check_permissions(svm)) |
2630 | return 1; | |
2631 | ||
7597f129 | 2632 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
9966bf68 JR |
2633 | if (!nested_vmcb) |
2634 | return 1; | |
2635 | ||
e3e9ed3d JR |
2636 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
2637 | skip_emulated_instruction(&svm->vcpu); | |
2638 | ||
9966bf68 | 2639 | nested_svm_vmloadsave(nested_vmcb, svm->vmcb); |
7597f129 | 2640 | nested_svm_unmap(page); |
5542675b AG |
2641 | |
2642 | return 1; | |
2643 | } | |
2644 | ||
851ba692 | 2645 | static int vmsave_interception(struct vcpu_svm *svm) |
5542675b | 2646 | { |
9966bf68 | 2647 | struct vmcb *nested_vmcb; |
7597f129 | 2648 | struct page *page; |
9966bf68 | 2649 | |
5542675b AG |
2650 | if (nested_svm_check_permissions(svm)) |
2651 | return 1; | |
2652 | ||
7597f129 | 2653 | nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page); |
9966bf68 JR |
2654 | if (!nested_vmcb) |
2655 | return 1; | |
2656 | ||
e3e9ed3d JR |
2657 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; |
2658 | skip_emulated_instruction(&svm->vcpu); | |
2659 | ||
9966bf68 | 2660 | nested_svm_vmloadsave(svm->vmcb, nested_vmcb); |
7597f129 | 2661 | nested_svm_unmap(page); |
5542675b AG |
2662 | |
2663 | return 1; | |
2664 | } | |
2665 | ||
851ba692 | 2666 | static int vmrun_interception(struct vcpu_svm *svm) |
3d6368ef | 2667 | { |
3d6368ef AG |
2668 | if (nested_svm_check_permissions(svm)) |
2669 | return 1; | |
2670 | ||
b75f4eb3 RJ |
2671 | /* Save rip after vmrun instruction */ |
2672 | kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3); | |
3d6368ef | 2673 | |
9738b2c9 | 2674 | if (!nested_svm_vmrun(svm)) |
3d6368ef AG |
2675 | return 1; |
2676 | ||
9738b2c9 | 2677 | if (!nested_svm_vmrun_msrpm(svm)) |
1f8da478 JR |
2678 | goto failed; |
2679 | ||
2680 | return 1; | |
2681 | ||
2682 | failed: | |
2683 | ||
2684 | svm->vmcb->control.exit_code = SVM_EXIT_ERR; | |
2685 | svm->vmcb->control.exit_code_hi = 0; | |
2686 | svm->vmcb->control.exit_info_1 = 0; | |
2687 | svm->vmcb->control.exit_info_2 = 0; | |
2688 | ||
2689 | nested_svm_vmexit(svm); | |
3d6368ef AG |
2690 | |
2691 | return 1; | |
2692 | } | |
2693 | ||
851ba692 | 2694 | static int stgi_interception(struct vcpu_svm *svm) |
1371d904 AG |
2695 | { |
2696 | if (nested_svm_check_permissions(svm)) | |
2697 | return 1; | |
2698 | ||
2699 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2700 | skip_emulated_instruction(&svm->vcpu); | |
3842d135 | 2701 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
1371d904 | 2702 | |
2af9194d | 2703 | enable_gif(svm); |
1371d904 AG |
2704 | |
2705 | return 1; | |
2706 | } | |
2707 | ||
851ba692 | 2708 | static int clgi_interception(struct vcpu_svm *svm) |
1371d904 AG |
2709 | { |
2710 | if (nested_svm_check_permissions(svm)) | |
2711 | return 1; | |
2712 | ||
2713 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2714 | skip_emulated_instruction(&svm->vcpu); | |
2715 | ||
2af9194d | 2716 | disable_gif(svm); |
1371d904 AG |
2717 | |
2718 | /* After a CLGI no interrupts should come */ | |
2719 | svm_clear_vintr(svm); | |
2720 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; | |
2721 | ||
decdbf6a JR |
2722 | mark_dirty(svm->vmcb, VMCB_INTR); |
2723 | ||
1371d904 AG |
2724 | return 1; |
2725 | } | |
2726 | ||
851ba692 | 2727 | static int invlpga_interception(struct vcpu_svm *svm) |
ff092385 AG |
2728 | { |
2729 | struct kvm_vcpu *vcpu = &svm->vcpu; | |
ff092385 | 2730 | |
ec1ff790 JR |
2731 | trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX], |
2732 | vcpu->arch.regs[VCPU_REGS_RAX]); | |
2733 | ||
ff092385 AG |
2734 | /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */ |
2735 | kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]); | |
2736 | ||
2737 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2738 | skip_emulated_instruction(&svm->vcpu); | |
2739 | return 1; | |
2740 | } | |
2741 | ||
532a46b9 JR |
2742 | static int skinit_interception(struct vcpu_svm *svm) |
2743 | { | |
2744 | trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]); | |
2745 | ||
2746 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
2747 | return 1; | |
2748 | } | |
2749 | ||
81dd35d4 JR |
2750 | static int xsetbv_interception(struct vcpu_svm *svm) |
2751 | { | |
2752 | u64 new_bv = kvm_read_edx_eax(&svm->vcpu); | |
2753 | u32 index = kvm_register_read(&svm->vcpu, VCPU_REGS_RCX); | |
2754 | ||
2755 | if (kvm_set_xcr(&svm->vcpu, index, new_bv) == 0) { | |
2756 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 3; | |
2757 | skip_emulated_instruction(&svm->vcpu); | |
2758 | } | |
2759 | ||
2760 | return 1; | |
2761 | } | |
2762 | ||
851ba692 | 2763 | static int invalid_op_interception(struct vcpu_svm *svm) |
6aa8b732 | 2764 | { |
7ee5d940 | 2765 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); |
6aa8b732 AK |
2766 | return 1; |
2767 | } | |
2768 | ||
851ba692 | 2769 | static int task_switch_interception(struct vcpu_svm *svm) |
6aa8b732 | 2770 | { |
37817f29 | 2771 | u16 tss_selector; |
64a7ec06 GN |
2772 | int reason; |
2773 | int int_type = svm->vmcb->control.exit_int_info & | |
2774 | SVM_EXITINTINFO_TYPE_MASK; | |
8317c298 | 2775 | int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK; |
fe8e7f83 GN |
2776 | uint32_t type = |
2777 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK; | |
2778 | uint32_t idt_v = | |
2779 | svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID; | |
e269fb21 JK |
2780 | bool has_error_code = false; |
2781 | u32 error_code = 0; | |
37817f29 IE |
2782 | |
2783 | tss_selector = (u16)svm->vmcb->control.exit_info_1; | |
64a7ec06 | 2784 | |
37817f29 IE |
2785 | if (svm->vmcb->control.exit_info_2 & |
2786 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET)) | |
64a7ec06 GN |
2787 | reason = TASK_SWITCH_IRET; |
2788 | else if (svm->vmcb->control.exit_info_2 & | |
2789 | (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP)) | |
2790 | reason = TASK_SWITCH_JMP; | |
fe8e7f83 | 2791 | else if (idt_v) |
64a7ec06 GN |
2792 | reason = TASK_SWITCH_GATE; |
2793 | else | |
2794 | reason = TASK_SWITCH_CALL; | |
2795 | ||
fe8e7f83 GN |
2796 | if (reason == TASK_SWITCH_GATE) { |
2797 | switch (type) { | |
2798 | case SVM_EXITINTINFO_TYPE_NMI: | |
2799 | svm->vcpu.arch.nmi_injected = false; | |
2800 | break; | |
2801 | case SVM_EXITINTINFO_TYPE_EXEPT: | |
e269fb21 JK |
2802 | if (svm->vmcb->control.exit_info_2 & |
2803 | (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) { | |
2804 | has_error_code = true; | |
2805 | error_code = | |
2806 | (u32)svm->vmcb->control.exit_info_2; | |
2807 | } | |
fe8e7f83 GN |
2808 | kvm_clear_exception_queue(&svm->vcpu); |
2809 | break; | |
2810 | case SVM_EXITINTINFO_TYPE_INTR: | |
2811 | kvm_clear_interrupt_queue(&svm->vcpu); | |
2812 | break; | |
2813 | default: | |
2814 | break; | |
2815 | } | |
2816 | } | |
64a7ec06 | 2817 | |
8317c298 GN |
2818 | if (reason != TASK_SWITCH_GATE || |
2819 | int_type == SVM_EXITINTINFO_TYPE_SOFT || | |
2820 | (int_type == SVM_EXITINTINFO_TYPE_EXEPT && | |
f629cf84 GN |
2821 | (int_vec == OF_VECTOR || int_vec == BP_VECTOR))) |
2822 | skip_emulated_instruction(&svm->vcpu); | |
64a7ec06 | 2823 | |
7f3d35fd KW |
2824 | if (int_type != SVM_EXITINTINFO_TYPE_SOFT) |
2825 | int_vec = -1; | |
2826 | ||
2827 | if (kvm_task_switch(&svm->vcpu, tss_selector, int_vec, reason, | |
acb54517 GN |
2828 | has_error_code, error_code) == EMULATE_FAIL) { |
2829 | svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
2830 | svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION; | |
2831 | svm->vcpu.run->internal.ndata = 0; | |
2832 | return 0; | |
2833 | } | |
2834 | return 1; | |
6aa8b732 AK |
2835 | } |
2836 | ||
851ba692 | 2837 | static int cpuid_interception(struct vcpu_svm *svm) |
6aa8b732 | 2838 | { |
5fdbf976 | 2839 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
e756fc62 | 2840 | kvm_emulate_cpuid(&svm->vcpu); |
06465c5a | 2841 | return 1; |
6aa8b732 AK |
2842 | } |
2843 | ||
851ba692 | 2844 | static int iret_interception(struct vcpu_svm *svm) |
95ba8273 GN |
2845 | { |
2846 | ++svm->vcpu.stat.nmi_window_exits; | |
8a05a1b8 | 2847 | clr_intercept(svm, INTERCEPT_IRET); |
44c11430 | 2848 | svm->vcpu.arch.hflags |= HF_IRET_MASK; |
bd3d1ec3 | 2849 | svm->nmi_iret_rip = kvm_rip_read(&svm->vcpu); |
95ba8273 GN |
2850 | return 1; |
2851 | } | |
2852 | ||
851ba692 | 2853 | static int invlpg_interception(struct vcpu_svm *svm) |
a7052897 | 2854 | { |
df4f3108 AP |
2855 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) |
2856 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; | |
2857 | ||
2858 | kvm_mmu_invlpg(&svm->vcpu, svm->vmcb->control.exit_info_1); | |
2859 | skip_emulated_instruction(&svm->vcpu); | |
2860 | return 1; | |
a7052897 MT |
2861 | } |
2862 | ||
851ba692 | 2863 | static int emulate_on_interception(struct vcpu_svm *svm) |
6aa8b732 | 2864 | { |
51d8b661 | 2865 | return emulate_instruction(&svm->vcpu, 0) == EMULATE_DONE; |
6aa8b732 AK |
2866 | } |
2867 | ||
332b56e4 AK |
2868 | static int rdpmc_interception(struct vcpu_svm *svm) |
2869 | { | |
2870 | int err; | |
2871 | ||
2872 | if (!static_cpu_has(X86_FEATURE_NRIPS)) | |
2873 | return emulate_on_interception(svm); | |
2874 | ||
2875 | err = kvm_rdpmc(&svm->vcpu); | |
2876 | kvm_complete_insn_gp(&svm->vcpu, err); | |
2877 | ||
2878 | return 1; | |
2879 | } | |
2880 | ||
628afd2a JR |
2881 | bool check_selective_cr0_intercepted(struct vcpu_svm *svm, unsigned long val) |
2882 | { | |
2883 | unsigned long cr0 = svm->vcpu.arch.cr0; | |
2884 | bool ret = false; | |
2885 | u64 intercept; | |
2886 | ||
2887 | intercept = svm->nested.intercept; | |
2888 | ||
2889 | if (!is_guest_mode(&svm->vcpu) || | |
2890 | (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0)))) | |
2891 | return false; | |
2892 | ||
2893 | cr0 &= ~SVM_CR0_SELECTIVE_MASK; | |
2894 | val &= ~SVM_CR0_SELECTIVE_MASK; | |
2895 | ||
2896 | if (cr0 ^ val) { | |
2897 | svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE; | |
2898 | ret = (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE); | |
2899 | } | |
2900 | ||
2901 | return ret; | |
2902 | } | |
2903 | ||
7ff76d58 AP |
2904 | #define CR_VALID (1ULL << 63) |
2905 | ||
2906 | static int cr_interception(struct vcpu_svm *svm) | |
2907 | { | |
2908 | int reg, cr; | |
2909 | unsigned long val; | |
2910 | int err; | |
2911 | ||
2912 | if (!static_cpu_has(X86_FEATURE_DECODEASSISTS)) | |
2913 | return emulate_on_interception(svm); | |
2914 | ||
2915 | if (unlikely((svm->vmcb->control.exit_info_1 & CR_VALID) == 0)) | |
2916 | return emulate_on_interception(svm); | |
2917 | ||
2918 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; | |
2919 | cr = svm->vmcb->control.exit_code - SVM_EXIT_READ_CR0; | |
2920 | ||
2921 | err = 0; | |
2922 | if (cr >= 16) { /* mov to cr */ | |
2923 | cr -= 16; | |
2924 | val = kvm_register_read(&svm->vcpu, reg); | |
2925 | switch (cr) { | |
2926 | case 0: | |
628afd2a JR |
2927 | if (!check_selective_cr0_intercepted(svm, val)) |
2928 | err = kvm_set_cr0(&svm->vcpu, val); | |
977b2d03 JR |
2929 | else |
2930 | return 1; | |
2931 | ||
7ff76d58 AP |
2932 | break; |
2933 | case 3: | |
2934 | err = kvm_set_cr3(&svm->vcpu, val); | |
2935 | break; | |
2936 | case 4: | |
2937 | err = kvm_set_cr4(&svm->vcpu, val); | |
2938 | break; | |
2939 | case 8: | |
2940 | err = kvm_set_cr8(&svm->vcpu, val); | |
2941 | break; | |
2942 | default: | |
2943 | WARN(1, "unhandled write to CR%d", cr); | |
2944 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
2945 | return 1; | |
2946 | } | |
2947 | } else { /* mov from cr */ | |
2948 | switch (cr) { | |
2949 | case 0: | |
2950 | val = kvm_read_cr0(&svm->vcpu); | |
2951 | break; | |
2952 | case 2: | |
2953 | val = svm->vcpu.arch.cr2; | |
2954 | break; | |
2955 | case 3: | |
9f8fe504 | 2956 | val = kvm_read_cr3(&svm->vcpu); |
7ff76d58 AP |
2957 | break; |
2958 | case 4: | |
2959 | val = kvm_read_cr4(&svm->vcpu); | |
2960 | break; | |
2961 | case 8: | |
2962 | val = kvm_get_cr8(&svm->vcpu); | |
2963 | break; | |
2964 | default: | |
2965 | WARN(1, "unhandled read from CR%d", cr); | |
2966 | kvm_queue_exception(&svm->vcpu, UD_VECTOR); | |
2967 | return 1; | |
2968 | } | |
2969 | kvm_register_write(&svm->vcpu, reg, val); | |
2970 | } | |
2971 | kvm_complete_insn_gp(&svm->vcpu, err); | |
2972 | ||
2973 | return 1; | |
2974 | } | |
2975 | ||
cae3797a AP |
2976 | static int dr_interception(struct vcpu_svm *svm) |
2977 | { | |
2978 | int reg, dr; | |
2979 | unsigned long val; | |
2980 | int err; | |
2981 | ||
2982 | if (!boot_cpu_has(X86_FEATURE_DECODEASSISTS)) | |
2983 | return emulate_on_interception(svm); | |
2984 | ||
2985 | reg = svm->vmcb->control.exit_info_1 & SVM_EXITINFO_REG_MASK; | |
2986 | dr = svm->vmcb->control.exit_code - SVM_EXIT_READ_DR0; | |
2987 | ||
2988 | if (dr >= 16) { /* mov to DRn */ | |
2989 | val = kvm_register_read(&svm->vcpu, reg); | |
2990 | kvm_set_dr(&svm->vcpu, dr - 16, val); | |
2991 | } else { | |
2992 | err = kvm_get_dr(&svm->vcpu, dr, &val); | |
2993 | if (!err) | |
2994 | kvm_register_write(&svm->vcpu, reg, val); | |
2995 | } | |
2996 | ||
2c46d2ae JR |
2997 | skip_emulated_instruction(&svm->vcpu); |
2998 | ||
cae3797a AP |
2999 | return 1; |
3000 | } | |
3001 | ||
851ba692 | 3002 | static int cr8_write_interception(struct vcpu_svm *svm) |
1d075434 | 3003 | { |
851ba692 | 3004 | struct kvm_run *kvm_run = svm->vcpu.run; |
eea1cff9 | 3005 | int r; |
851ba692 | 3006 | |
0a5fff19 GN |
3007 | u8 cr8_prev = kvm_get_cr8(&svm->vcpu); |
3008 | /* instruction emulation calls kvm_set_cr8() */ | |
7ff76d58 | 3009 | r = cr_interception(svm); |
95ba8273 | 3010 | if (irqchip_in_kernel(svm->vcpu.kvm)) { |
4ee546b4 | 3011 | clr_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
7ff76d58 | 3012 | return r; |
95ba8273 | 3013 | } |
0a5fff19 | 3014 | if (cr8_prev <= kvm_get_cr8(&svm->vcpu)) |
7ff76d58 | 3015 | return r; |
1d075434 JR |
3016 | kvm_run->exit_reason = KVM_EXIT_SET_TPR; |
3017 | return 0; | |
3018 | } | |
3019 | ||
d5c1785d NHE |
3020 | u64 svm_read_l1_tsc(struct kvm_vcpu *vcpu) |
3021 | { | |
3022 | struct vmcb *vmcb = get_host_vmcb(to_svm(vcpu)); | |
3023 | return vmcb->control.tsc_offset + | |
3024 | svm_scale_tsc(vcpu, native_read_tsc()); | |
3025 | } | |
3026 | ||
6aa8b732 AK |
3027 | static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data) |
3028 | { | |
a2fa3e9f GH |
3029 | struct vcpu_svm *svm = to_svm(vcpu); |
3030 | ||
6aa8b732 | 3031 | switch (ecx) { |
af24a4e4 | 3032 | case MSR_IA32_TSC: { |
45133eca | 3033 | *data = svm->vmcb->control.tsc_offset + |
fbc0db76 JR |
3034 | svm_scale_tsc(vcpu, native_read_tsc()); |
3035 | ||
6aa8b732 AK |
3036 | break; |
3037 | } | |
8c06585d | 3038 | case MSR_STAR: |
a2fa3e9f | 3039 | *data = svm->vmcb->save.star; |
6aa8b732 | 3040 | break; |
0e859cac | 3041 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3042 | case MSR_LSTAR: |
a2fa3e9f | 3043 | *data = svm->vmcb->save.lstar; |
6aa8b732 AK |
3044 | break; |
3045 | case MSR_CSTAR: | |
a2fa3e9f | 3046 | *data = svm->vmcb->save.cstar; |
6aa8b732 AK |
3047 | break; |
3048 | case MSR_KERNEL_GS_BASE: | |
a2fa3e9f | 3049 | *data = svm->vmcb->save.kernel_gs_base; |
6aa8b732 AK |
3050 | break; |
3051 | case MSR_SYSCALL_MASK: | |
a2fa3e9f | 3052 | *data = svm->vmcb->save.sfmask; |
6aa8b732 AK |
3053 | break; |
3054 | #endif | |
3055 | case MSR_IA32_SYSENTER_CS: | |
a2fa3e9f | 3056 | *data = svm->vmcb->save.sysenter_cs; |
6aa8b732 AK |
3057 | break; |
3058 | case MSR_IA32_SYSENTER_EIP: | |
017cb99e | 3059 | *data = svm->sysenter_eip; |
6aa8b732 AK |
3060 | break; |
3061 | case MSR_IA32_SYSENTER_ESP: | |
017cb99e | 3062 | *data = svm->sysenter_esp; |
6aa8b732 | 3063 | break; |
e0231715 JR |
3064 | /* |
3065 | * Nobody will change the following 5 values in the VMCB so we can | |
3066 | * safely return them on rdmsr. They will always be 0 until LBRV is | |
3067 | * implemented. | |
3068 | */ | |
a2938c80 JR |
3069 | case MSR_IA32_DEBUGCTLMSR: |
3070 | *data = svm->vmcb->save.dbgctl; | |
3071 | break; | |
3072 | case MSR_IA32_LASTBRANCHFROMIP: | |
3073 | *data = svm->vmcb->save.br_from; | |
3074 | break; | |
3075 | case MSR_IA32_LASTBRANCHTOIP: | |
3076 | *data = svm->vmcb->save.br_to; | |
3077 | break; | |
3078 | case MSR_IA32_LASTINTFROMIP: | |
3079 | *data = svm->vmcb->save.last_excp_from; | |
3080 | break; | |
3081 | case MSR_IA32_LASTINTTOIP: | |
3082 | *data = svm->vmcb->save.last_excp_to; | |
3083 | break; | |
b286d5d8 | 3084 | case MSR_VM_HSAVE_PA: |
e6aa9abd | 3085 | *data = svm->nested.hsave_msr; |
b286d5d8 | 3086 | break; |
eb6f302e | 3087 | case MSR_VM_CR: |
4a810181 | 3088 | *data = svm->nested.vm_cr_msr; |
eb6f302e | 3089 | break; |
c8a73f18 AG |
3090 | case MSR_IA32_UCODE_REV: |
3091 | *data = 0x01000065; | |
3092 | break; | |
6aa8b732 | 3093 | default: |
3bab1f5d | 3094 | return kvm_get_msr_common(vcpu, ecx, data); |
6aa8b732 AK |
3095 | } |
3096 | return 0; | |
3097 | } | |
3098 | ||
851ba692 | 3099 | static int rdmsr_interception(struct vcpu_svm *svm) |
6aa8b732 | 3100 | { |
ad312c7c | 3101 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
6aa8b732 AK |
3102 | u64 data; |
3103 | ||
59200273 AK |
3104 | if (svm_get_msr(&svm->vcpu, ecx, &data)) { |
3105 | trace_kvm_msr_read_ex(ecx); | |
c1a5d4f9 | 3106 | kvm_inject_gp(&svm->vcpu, 0); |
59200273 | 3107 | } else { |
229456fc | 3108 | trace_kvm_msr_read(ecx, data); |
af9ca2d7 | 3109 | |
5fdbf976 | 3110 | svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff; |
ad312c7c | 3111 | svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32; |
5fdbf976 | 3112 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
e756fc62 | 3113 | skip_emulated_instruction(&svm->vcpu); |
6aa8b732 AK |
3114 | } |
3115 | return 1; | |
3116 | } | |
3117 | ||
4a810181 JR |
3118 | static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data) |
3119 | { | |
3120 | struct vcpu_svm *svm = to_svm(vcpu); | |
3121 | int svm_dis, chg_mask; | |
3122 | ||
3123 | if (data & ~SVM_VM_CR_VALID_MASK) | |
3124 | return 1; | |
3125 | ||
3126 | chg_mask = SVM_VM_CR_VALID_MASK; | |
3127 | ||
3128 | if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK) | |
3129 | chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK); | |
3130 | ||
3131 | svm->nested.vm_cr_msr &= ~chg_mask; | |
3132 | svm->nested.vm_cr_msr |= (data & chg_mask); | |
3133 | ||
3134 | svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK; | |
3135 | ||
3136 | /* check for svm_disable while efer.svme is set */ | |
3137 | if (svm_dis && (vcpu->arch.efer & EFER_SVME)) | |
3138 | return 1; | |
3139 | ||
3140 | return 0; | |
3141 | } | |
3142 | ||
6aa8b732 AK |
3143 | static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data) |
3144 | { | |
a2fa3e9f GH |
3145 | struct vcpu_svm *svm = to_svm(vcpu); |
3146 | ||
6aa8b732 | 3147 | switch (ecx) { |
f4e1b3c8 | 3148 | case MSR_IA32_TSC: |
99e3e30a | 3149 | kvm_write_tsc(vcpu, data); |
6aa8b732 | 3150 | break; |
8c06585d | 3151 | case MSR_STAR: |
a2fa3e9f | 3152 | svm->vmcb->save.star = data; |
6aa8b732 | 3153 | break; |
49b14f24 | 3154 | #ifdef CONFIG_X86_64 |
6aa8b732 | 3155 | case MSR_LSTAR: |
a2fa3e9f | 3156 | svm->vmcb->save.lstar = data; |
6aa8b732 AK |
3157 | break; |
3158 | case MSR_CSTAR: | |
a2fa3e9f | 3159 | svm->vmcb->save.cstar = data; |
6aa8b732 AK |
3160 | break; |
3161 | case MSR_KERNEL_GS_BASE: | |
a2fa3e9f | 3162 | svm->vmcb->save.kernel_gs_base = data; |
6aa8b732 AK |
3163 | break; |
3164 | case MSR_SYSCALL_MASK: | |
a2fa3e9f | 3165 | svm->vmcb->save.sfmask = data; |
6aa8b732 AK |
3166 | break; |
3167 | #endif | |
3168 | case MSR_IA32_SYSENTER_CS: | |
a2fa3e9f | 3169 | svm->vmcb->save.sysenter_cs = data; |
6aa8b732 AK |
3170 | break; |
3171 | case MSR_IA32_SYSENTER_EIP: | |
017cb99e | 3172 | svm->sysenter_eip = data; |
a2fa3e9f | 3173 | svm->vmcb->save.sysenter_eip = data; |
6aa8b732 AK |
3174 | break; |
3175 | case MSR_IA32_SYSENTER_ESP: | |
017cb99e | 3176 | svm->sysenter_esp = data; |
a2fa3e9f | 3177 | svm->vmcb->save.sysenter_esp = data; |
6aa8b732 | 3178 | break; |
a2938c80 | 3179 | case MSR_IA32_DEBUGCTLMSR: |
2a6b20b8 | 3180 | if (!boot_cpu_has(X86_FEATURE_LBRV)) { |
24e09cbf | 3181 | pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n", |
b8688d51 | 3182 | __func__, data); |
24e09cbf JR |
3183 | break; |
3184 | } | |
3185 | if (data & DEBUGCTL_RESERVED_BITS) | |
3186 | return 1; | |
3187 | ||
3188 | svm->vmcb->save.dbgctl = data; | |
b53ba3f9 | 3189 | mark_dirty(svm->vmcb, VMCB_LBR); |
24e09cbf JR |
3190 | if (data & (1ULL<<0)) |
3191 | svm_enable_lbrv(svm); | |
3192 | else | |
3193 | svm_disable_lbrv(svm); | |
a2938c80 | 3194 | break; |
b286d5d8 | 3195 | case MSR_VM_HSAVE_PA: |
e6aa9abd | 3196 | svm->nested.hsave_msr = data; |
62b9abaa | 3197 | break; |
3c5d0a44 | 3198 | case MSR_VM_CR: |
4a810181 | 3199 | return svm_set_vm_cr(vcpu, data); |
3c5d0a44 | 3200 | case MSR_VM_IGNNE: |
3c5d0a44 AG |
3201 | pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data); |
3202 | break; | |
6aa8b732 | 3203 | default: |
3bab1f5d | 3204 | return kvm_set_msr_common(vcpu, ecx, data); |
6aa8b732 AK |
3205 | } |
3206 | return 0; | |
3207 | } | |
3208 | ||
851ba692 | 3209 | static int wrmsr_interception(struct vcpu_svm *svm) |
6aa8b732 | 3210 | { |
ad312c7c | 3211 | u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX]; |
5fdbf976 | 3212 | u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u) |
ad312c7c | 3213 | | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32); |
af9ca2d7 | 3214 | |
af9ca2d7 | 3215 | |
5fdbf976 | 3216 | svm->next_rip = kvm_rip_read(&svm->vcpu) + 2; |
59200273 AK |
3217 | if (svm_set_msr(&svm->vcpu, ecx, data)) { |
3218 | trace_kvm_msr_write_ex(ecx, data); | |
c1a5d4f9 | 3219 | kvm_inject_gp(&svm->vcpu, 0); |
59200273 AK |
3220 | } else { |
3221 | trace_kvm_msr_write(ecx, data); | |
e756fc62 | 3222 | skip_emulated_instruction(&svm->vcpu); |
59200273 | 3223 | } |
6aa8b732 AK |
3224 | return 1; |
3225 | } | |
3226 | ||
851ba692 | 3227 | static int msr_interception(struct vcpu_svm *svm) |
6aa8b732 | 3228 | { |
e756fc62 | 3229 | if (svm->vmcb->control.exit_info_1) |
851ba692 | 3230 | return wrmsr_interception(svm); |
6aa8b732 | 3231 | else |
851ba692 | 3232 | return rdmsr_interception(svm); |
6aa8b732 AK |
3233 | } |
3234 | ||
851ba692 | 3235 | static int interrupt_window_interception(struct vcpu_svm *svm) |
c1150d8c | 3236 | { |
851ba692 AK |
3237 | struct kvm_run *kvm_run = svm->vcpu.run; |
3238 | ||
3842d135 | 3239 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
f0b85051 | 3240 | svm_clear_vintr(svm); |
85f455f7 | 3241 | svm->vmcb->control.int_ctl &= ~V_IRQ_MASK; |
decdbf6a | 3242 | mark_dirty(svm->vmcb, VMCB_INTR); |
c1150d8c DL |
3243 | /* |
3244 | * If the user space waits to inject interrupts, exit as soon as | |
3245 | * possible | |
3246 | */ | |
8061823a GN |
3247 | if (!irqchip_in_kernel(svm->vcpu.kvm) && |
3248 | kvm_run->request_interrupt_window && | |
3249 | !kvm_cpu_has_interrupt(&svm->vcpu)) { | |
e756fc62 | 3250 | ++svm->vcpu.stat.irq_window_exits; |
c1150d8c DL |
3251 | kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN; |
3252 | return 0; | |
3253 | } | |
3254 | ||
3255 | return 1; | |
3256 | } | |
3257 | ||
565d0998 ML |
3258 | static int pause_interception(struct vcpu_svm *svm) |
3259 | { | |
3260 | kvm_vcpu_on_spin(&(svm->vcpu)); | |
3261 | return 1; | |
3262 | } | |
3263 | ||
851ba692 | 3264 | static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = { |
7ff76d58 AP |
3265 | [SVM_EXIT_READ_CR0] = cr_interception, |
3266 | [SVM_EXIT_READ_CR3] = cr_interception, | |
3267 | [SVM_EXIT_READ_CR4] = cr_interception, | |
3268 | [SVM_EXIT_READ_CR8] = cr_interception, | |
d225157b | 3269 | [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception, |
628afd2a | 3270 | [SVM_EXIT_WRITE_CR0] = cr_interception, |
7ff76d58 AP |
3271 | [SVM_EXIT_WRITE_CR3] = cr_interception, |
3272 | [SVM_EXIT_WRITE_CR4] = cr_interception, | |
e0231715 | 3273 | [SVM_EXIT_WRITE_CR8] = cr8_write_interception, |
cae3797a AP |
3274 | [SVM_EXIT_READ_DR0] = dr_interception, |
3275 | [SVM_EXIT_READ_DR1] = dr_interception, | |
3276 | [SVM_EXIT_READ_DR2] = dr_interception, | |
3277 | [SVM_EXIT_READ_DR3] = dr_interception, | |
3278 | [SVM_EXIT_READ_DR4] = dr_interception, | |
3279 | [SVM_EXIT_READ_DR5] = dr_interception, | |
3280 | [SVM_EXIT_READ_DR6] = dr_interception, | |
3281 | [SVM_EXIT_READ_DR7] = dr_interception, | |
3282 | [SVM_EXIT_WRITE_DR0] = dr_interception, | |
3283 | [SVM_EXIT_WRITE_DR1] = dr_interception, | |
3284 | [SVM_EXIT_WRITE_DR2] = dr_interception, | |
3285 | [SVM_EXIT_WRITE_DR3] = dr_interception, | |
3286 | [SVM_EXIT_WRITE_DR4] = dr_interception, | |
3287 | [SVM_EXIT_WRITE_DR5] = dr_interception, | |
3288 | [SVM_EXIT_WRITE_DR6] = dr_interception, | |
3289 | [SVM_EXIT_WRITE_DR7] = dr_interception, | |
d0bfb940 JK |
3290 | [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception, |
3291 | [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception, | |
7aa81cc0 | 3292 | [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception, |
e0231715 JR |
3293 | [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception, |
3294 | [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception, | |
3295 | [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception, | |
3296 | [SVM_EXIT_INTR] = intr_interception, | |
c47f098d | 3297 | [SVM_EXIT_NMI] = nmi_interception, |
6aa8b732 AK |
3298 | [SVM_EXIT_SMI] = nop_on_interception, |
3299 | [SVM_EXIT_INIT] = nop_on_interception, | |
c1150d8c | 3300 | [SVM_EXIT_VINTR] = interrupt_window_interception, |
332b56e4 | 3301 | [SVM_EXIT_RDPMC] = rdpmc_interception, |
6aa8b732 | 3302 | [SVM_EXIT_CPUID] = cpuid_interception, |
95ba8273 | 3303 | [SVM_EXIT_IRET] = iret_interception, |
cf5a94d1 | 3304 | [SVM_EXIT_INVD] = emulate_on_interception, |
565d0998 | 3305 | [SVM_EXIT_PAUSE] = pause_interception, |
6aa8b732 | 3306 | [SVM_EXIT_HLT] = halt_interception, |
a7052897 | 3307 | [SVM_EXIT_INVLPG] = invlpg_interception, |
ff092385 | 3308 | [SVM_EXIT_INVLPGA] = invlpga_interception, |
e0231715 | 3309 | [SVM_EXIT_IOIO] = io_interception, |
6aa8b732 AK |
3310 | [SVM_EXIT_MSR] = msr_interception, |
3311 | [SVM_EXIT_TASK_SWITCH] = task_switch_interception, | |
46fe4ddd | 3312 | [SVM_EXIT_SHUTDOWN] = shutdown_interception, |
3d6368ef | 3313 | [SVM_EXIT_VMRUN] = vmrun_interception, |
02e235bc | 3314 | [SVM_EXIT_VMMCALL] = vmmcall_interception, |
5542675b AG |
3315 | [SVM_EXIT_VMLOAD] = vmload_interception, |
3316 | [SVM_EXIT_VMSAVE] = vmsave_interception, | |
1371d904 AG |
3317 | [SVM_EXIT_STGI] = stgi_interception, |
3318 | [SVM_EXIT_CLGI] = clgi_interception, | |
532a46b9 | 3319 | [SVM_EXIT_SKINIT] = skinit_interception, |
cf5a94d1 | 3320 | [SVM_EXIT_WBINVD] = emulate_on_interception, |
916ce236 JR |
3321 | [SVM_EXIT_MONITOR] = invalid_op_interception, |
3322 | [SVM_EXIT_MWAIT] = invalid_op_interception, | |
81dd35d4 | 3323 | [SVM_EXIT_XSETBV] = xsetbv_interception, |
709ddebf | 3324 | [SVM_EXIT_NPF] = pf_interception, |
6aa8b732 AK |
3325 | }; |
3326 | ||
ae8cc059 | 3327 | static void dump_vmcb(struct kvm_vcpu *vcpu) |
3f10c846 JR |
3328 | { |
3329 | struct vcpu_svm *svm = to_svm(vcpu); | |
3330 | struct vmcb_control_area *control = &svm->vmcb->control; | |
3331 | struct vmcb_save_area *save = &svm->vmcb->save; | |
3332 | ||
3333 | pr_err("VMCB Control Area:\n"); | |
ae8cc059 JP |
3334 | pr_err("%-20s%04x\n", "cr_read:", control->intercept_cr & 0xffff); |
3335 | pr_err("%-20s%04x\n", "cr_write:", control->intercept_cr >> 16); | |
3336 | pr_err("%-20s%04x\n", "dr_read:", control->intercept_dr & 0xffff); | |
3337 | pr_err("%-20s%04x\n", "dr_write:", control->intercept_dr >> 16); | |
3338 | pr_err("%-20s%08x\n", "exceptions:", control->intercept_exceptions); | |
3339 | pr_err("%-20s%016llx\n", "intercepts:", control->intercept); | |
3340 | pr_err("%-20s%d\n", "pause filter count:", control->pause_filter_count); | |
3341 | pr_err("%-20s%016llx\n", "iopm_base_pa:", control->iopm_base_pa); | |
3342 | pr_err("%-20s%016llx\n", "msrpm_base_pa:", control->msrpm_base_pa); | |
3343 | pr_err("%-20s%016llx\n", "tsc_offset:", control->tsc_offset); | |
3344 | pr_err("%-20s%d\n", "asid:", control->asid); | |
3345 | pr_err("%-20s%d\n", "tlb_ctl:", control->tlb_ctl); | |
3346 | pr_err("%-20s%08x\n", "int_ctl:", control->int_ctl); | |
3347 | pr_err("%-20s%08x\n", "int_vector:", control->int_vector); | |
3348 | pr_err("%-20s%08x\n", "int_state:", control->int_state); | |
3349 | pr_err("%-20s%08x\n", "exit_code:", control->exit_code); | |
3350 | pr_err("%-20s%016llx\n", "exit_info1:", control->exit_info_1); | |
3351 | pr_err("%-20s%016llx\n", "exit_info2:", control->exit_info_2); | |
3352 | pr_err("%-20s%08x\n", "exit_int_info:", control->exit_int_info); | |
3353 | pr_err("%-20s%08x\n", "exit_int_info_err:", control->exit_int_info_err); | |
3354 | pr_err("%-20s%lld\n", "nested_ctl:", control->nested_ctl); | |
3355 | pr_err("%-20s%016llx\n", "nested_cr3:", control->nested_cr3); | |
3356 | pr_err("%-20s%08x\n", "event_inj:", control->event_inj); | |
3357 | pr_err("%-20s%08x\n", "event_inj_err:", control->event_inj_err); | |
3358 | pr_err("%-20s%lld\n", "lbr_ctl:", control->lbr_ctl); | |
3359 | pr_err("%-20s%016llx\n", "next_rip:", control->next_rip); | |
3f10c846 | 3360 | pr_err("VMCB State Save Area:\n"); |
ae8cc059 JP |
3361 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", |
3362 | "es:", | |
3363 | save->es.selector, save->es.attrib, | |
3364 | save->es.limit, save->es.base); | |
3365 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3366 | "cs:", | |
3367 | save->cs.selector, save->cs.attrib, | |
3368 | save->cs.limit, save->cs.base); | |
3369 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3370 | "ss:", | |
3371 | save->ss.selector, save->ss.attrib, | |
3372 | save->ss.limit, save->ss.base); | |
3373 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3374 | "ds:", | |
3375 | save->ds.selector, save->ds.attrib, | |
3376 | save->ds.limit, save->ds.base); | |
3377 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3378 | "fs:", | |
3379 | save->fs.selector, save->fs.attrib, | |
3380 | save->fs.limit, save->fs.base); | |
3381 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3382 | "gs:", | |
3383 | save->gs.selector, save->gs.attrib, | |
3384 | save->gs.limit, save->gs.base); | |
3385 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3386 | "gdtr:", | |
3387 | save->gdtr.selector, save->gdtr.attrib, | |
3388 | save->gdtr.limit, save->gdtr.base); | |
3389 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3390 | "ldtr:", | |
3391 | save->ldtr.selector, save->ldtr.attrib, | |
3392 | save->ldtr.limit, save->ldtr.base); | |
3393 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3394 | "idtr:", | |
3395 | save->idtr.selector, save->idtr.attrib, | |
3396 | save->idtr.limit, save->idtr.base); | |
3397 | pr_err("%-5s s: %04x a: %04x l: %08x b: %016llx\n", | |
3398 | "tr:", | |
3399 | save->tr.selector, save->tr.attrib, | |
3400 | save->tr.limit, save->tr.base); | |
3f10c846 JR |
3401 | pr_err("cpl: %d efer: %016llx\n", |
3402 | save->cpl, save->efer); | |
ae8cc059 JP |
3403 | pr_err("%-15s %016llx %-13s %016llx\n", |
3404 | "cr0:", save->cr0, "cr2:", save->cr2); | |
3405 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3406 | "cr3:", save->cr3, "cr4:", save->cr4); | |
3407 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3408 | "dr6:", save->dr6, "dr7:", save->dr7); | |
3409 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3410 | "rip:", save->rip, "rflags:", save->rflags); | |
3411 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3412 | "rsp:", save->rsp, "rax:", save->rax); | |
3413 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3414 | "star:", save->star, "lstar:", save->lstar); | |
3415 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3416 | "cstar:", save->cstar, "sfmask:", save->sfmask); | |
3417 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3418 | "kernel_gs_base:", save->kernel_gs_base, | |
3419 | "sysenter_cs:", save->sysenter_cs); | |
3420 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3421 | "sysenter_esp:", save->sysenter_esp, | |
3422 | "sysenter_eip:", save->sysenter_eip); | |
3423 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3424 | "gpat:", save->g_pat, "dbgctl:", save->dbgctl); | |
3425 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3426 | "br_from:", save->br_from, "br_to:", save->br_to); | |
3427 | pr_err("%-15s %016llx %-13s %016llx\n", | |
3428 | "excp_from:", save->last_excp_from, | |
3429 | "excp_to:", save->last_excp_to); | |
3f10c846 JR |
3430 | } |
3431 | ||
586f9607 AK |
3432 | static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2) |
3433 | { | |
3434 | struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control; | |
3435 | ||
3436 | *info1 = control->exit_info_1; | |
3437 | *info2 = control->exit_info_2; | |
3438 | } | |
3439 | ||
851ba692 | 3440 | static int handle_exit(struct kvm_vcpu *vcpu) |
6aa8b732 | 3441 | { |
04d2cc77 | 3442 | struct vcpu_svm *svm = to_svm(vcpu); |
851ba692 | 3443 | struct kvm_run *kvm_run = vcpu->run; |
a2fa3e9f | 3444 | u32 exit_code = svm->vmcb->control.exit_code; |
6aa8b732 | 3445 | |
4ee546b4 | 3446 | if (!is_cr_intercept(svm, INTERCEPT_CR0_WRITE)) |
2be4fc7a JR |
3447 | vcpu->arch.cr0 = svm->vmcb->save.cr0; |
3448 | if (npt_enabled) | |
3449 | vcpu->arch.cr3 = svm->vmcb->save.cr3; | |
af9ca2d7 | 3450 | |
cd3ff653 JR |
3451 | if (unlikely(svm->nested.exit_required)) { |
3452 | nested_svm_vmexit(svm); | |
3453 | svm->nested.exit_required = false; | |
3454 | ||
3455 | return 1; | |
3456 | } | |
3457 | ||
2030753d | 3458 | if (is_guest_mode(vcpu)) { |
410e4d57 JR |
3459 | int vmexit; |
3460 | ||
d8cabddf JR |
3461 | trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code, |
3462 | svm->vmcb->control.exit_info_1, | |
3463 | svm->vmcb->control.exit_info_2, | |
3464 | svm->vmcb->control.exit_int_info, | |
e097e5ff SH |
3465 | svm->vmcb->control.exit_int_info_err, |
3466 | KVM_ISA_SVM); | |
d8cabddf | 3467 | |
410e4d57 JR |
3468 | vmexit = nested_svm_exit_special(svm); |
3469 | ||
3470 | if (vmexit == NESTED_EXIT_CONTINUE) | |
3471 | vmexit = nested_svm_exit_handled(svm); | |
3472 | ||
3473 | if (vmexit == NESTED_EXIT_DONE) | |
cf74a78b | 3474 | return 1; |
cf74a78b AG |
3475 | } |
3476 | ||
a5c3832d JR |
3477 | svm_complete_interrupts(svm); |
3478 | ||
04d2cc77 AK |
3479 | if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) { |
3480 | kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY; | |
3481 | kvm_run->fail_entry.hardware_entry_failure_reason | |
3482 | = svm->vmcb->control.exit_code; | |
3f10c846 JR |
3483 | pr_err("KVM: FAILED VMRUN WITH VMCB:\n"); |
3484 | dump_vmcb(vcpu); | |
04d2cc77 AK |
3485 | return 0; |
3486 | } | |
3487 | ||
a2fa3e9f | 3488 | if (is_external_interrupt(svm->vmcb->control.exit_int_info) && |
709ddebf | 3489 | exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR && |
55c5e464 JR |
3490 | exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH && |
3491 | exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI) | |
6aa8b732 AK |
3492 | printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x " |
3493 | "exit_code 0x%x\n", | |
b8688d51 | 3494 | __func__, svm->vmcb->control.exit_int_info, |
6aa8b732 AK |
3495 | exit_code); |
3496 | ||
9d8f549d | 3497 | if (exit_code >= ARRAY_SIZE(svm_exit_handlers) |
56919c5c | 3498 | || !svm_exit_handlers[exit_code]) { |
6aa8b732 | 3499 | kvm_run->exit_reason = KVM_EXIT_UNKNOWN; |
364b625b | 3500 | kvm_run->hw.hardware_exit_reason = exit_code; |
6aa8b732 AK |
3501 | return 0; |
3502 | } | |
3503 | ||
851ba692 | 3504 | return svm_exit_handlers[exit_code](svm); |
6aa8b732 AK |
3505 | } |
3506 | ||
3507 | static void reload_tss(struct kvm_vcpu *vcpu) | |
3508 | { | |
3509 | int cpu = raw_smp_processor_id(); | |
3510 | ||
0fe1e009 TH |
3511 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
3512 | sd->tss_desc->type = 9; /* available 32/64-bit TSS */ | |
6aa8b732 AK |
3513 | load_TR_desc(); |
3514 | } | |
3515 | ||
e756fc62 | 3516 | static void pre_svm_run(struct vcpu_svm *svm) |
6aa8b732 AK |
3517 | { |
3518 | int cpu = raw_smp_processor_id(); | |
3519 | ||
0fe1e009 | 3520 | struct svm_cpu_data *sd = per_cpu(svm_data, cpu); |
6aa8b732 | 3521 | |
4b656b12 | 3522 | /* FIXME: handle wraparound of asid_generation */ |
0fe1e009 TH |
3523 | if (svm->asid_generation != sd->asid_generation) |
3524 | new_asid(svm, sd); | |
6aa8b732 AK |
3525 | } |
3526 | ||
95ba8273 GN |
3527 | static void svm_inject_nmi(struct kvm_vcpu *vcpu) |
3528 | { | |
3529 | struct vcpu_svm *svm = to_svm(vcpu); | |
3530 | ||
3531 | svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI; | |
3532 | vcpu->arch.hflags |= HF_NMI_MASK; | |
8a05a1b8 | 3533 | set_intercept(svm, INTERCEPT_IRET); |
95ba8273 GN |
3534 | ++vcpu->stat.nmi_injections; |
3535 | } | |
6aa8b732 | 3536 | |
85f455f7 | 3537 | static inline void svm_inject_irq(struct vcpu_svm *svm, int irq) |
6aa8b732 AK |
3538 | { |
3539 | struct vmcb_control_area *control; | |
3540 | ||
e756fc62 | 3541 | control = &svm->vmcb->control; |
85f455f7 | 3542 | control->int_vector = irq; |
6aa8b732 AK |
3543 | control->int_ctl &= ~V_INTR_PRIO_MASK; |
3544 | control->int_ctl |= V_IRQ_MASK | | |
3545 | ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT); | |
decdbf6a | 3546 | mark_dirty(svm->vmcb, VMCB_INTR); |
6aa8b732 AK |
3547 | } |
3548 | ||
66fd3f7f | 3549 | static void svm_set_irq(struct kvm_vcpu *vcpu) |
2a8067f1 ED |
3550 | { |
3551 | struct vcpu_svm *svm = to_svm(vcpu); | |
3552 | ||
2af9194d | 3553 | BUG_ON(!(gif_set(svm))); |
cf74a78b | 3554 | |
9fb2d2b4 GN |
3555 | trace_kvm_inj_virq(vcpu->arch.interrupt.nr); |
3556 | ++vcpu->stat.irq_injections; | |
3557 | ||
219b65dc AG |
3558 | svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr | |
3559 | SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR; | |
2a8067f1 ED |
3560 | } |
3561 | ||
95ba8273 | 3562 | static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr) |
aaacfc9a JR |
3563 | { |
3564 | struct vcpu_svm *svm = to_svm(vcpu); | |
aaacfc9a | 3565 | |
2030753d | 3566 | if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
88ab24ad JR |
3567 | return; |
3568 | ||
95ba8273 | 3569 | if (irr == -1) |
aaacfc9a JR |
3570 | return; |
3571 | ||
95ba8273 | 3572 | if (tpr >= irr) |
4ee546b4 | 3573 | set_cr_intercept(svm, INTERCEPT_CR8_WRITE); |
95ba8273 | 3574 | } |
aaacfc9a | 3575 | |
95ba8273 GN |
3576 | static int svm_nmi_allowed(struct kvm_vcpu *vcpu) |
3577 | { | |
3578 | struct vcpu_svm *svm = to_svm(vcpu); | |
3579 | struct vmcb *vmcb = svm->vmcb; | |
924584cc JR |
3580 | int ret; |
3581 | ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) && | |
3582 | !(svm->vcpu.arch.hflags & HF_NMI_MASK); | |
3583 | ret = ret && gif_set(svm) && nested_svm_nmi(svm); | |
3584 | ||
3585 | return ret; | |
aaacfc9a JR |
3586 | } |
3587 | ||
3cfc3092 JK |
3588 | static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu) |
3589 | { | |
3590 | struct vcpu_svm *svm = to_svm(vcpu); | |
3591 | ||
3592 | return !!(svm->vcpu.arch.hflags & HF_NMI_MASK); | |
3593 | } | |
3594 | ||
3595 | static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked) | |
3596 | { | |
3597 | struct vcpu_svm *svm = to_svm(vcpu); | |
3598 | ||
3599 | if (masked) { | |
3600 | svm->vcpu.arch.hflags |= HF_NMI_MASK; | |
8a05a1b8 | 3601 | set_intercept(svm, INTERCEPT_IRET); |
3cfc3092 JK |
3602 | } else { |
3603 | svm->vcpu.arch.hflags &= ~HF_NMI_MASK; | |
8a05a1b8 | 3604 | clr_intercept(svm, INTERCEPT_IRET); |
3cfc3092 JK |
3605 | } |
3606 | } | |
3607 | ||
78646121 GN |
3608 | static int svm_interrupt_allowed(struct kvm_vcpu *vcpu) |
3609 | { | |
3610 | struct vcpu_svm *svm = to_svm(vcpu); | |
3611 | struct vmcb *vmcb = svm->vmcb; | |
7fcdb510 JR |
3612 | int ret; |
3613 | ||
3614 | if (!gif_set(svm) || | |
3615 | (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)) | |
3616 | return 0; | |
3617 | ||
f6e78475 | 3618 | ret = !!(kvm_get_rflags(vcpu) & X86_EFLAGS_IF); |
7fcdb510 | 3619 | |
2030753d | 3620 | if (is_guest_mode(vcpu)) |
7fcdb510 JR |
3621 | return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK); |
3622 | ||
3623 | return ret; | |
78646121 GN |
3624 | } |
3625 | ||
9222be18 | 3626 | static void enable_irq_window(struct kvm_vcpu *vcpu) |
6aa8b732 | 3627 | { |
219b65dc | 3628 | struct vcpu_svm *svm = to_svm(vcpu); |
219b65dc | 3629 | |
e0231715 JR |
3630 | /* |
3631 | * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes | |
3632 | * 1, because that's a separate STGI/VMRUN intercept. The next time we | |
3633 | * get that intercept, this function will be called again though and | |
3634 | * we'll get the vintr intercept. | |
3635 | */ | |
8fe54654 | 3636 | if (gif_set(svm) && nested_svm_intr(svm)) { |
219b65dc AG |
3637 | svm_set_vintr(svm); |
3638 | svm_inject_irq(svm, 0x0); | |
3639 | } | |
85f455f7 ED |
3640 | } |
3641 | ||
95ba8273 | 3642 | static void enable_nmi_window(struct kvm_vcpu *vcpu) |
c1150d8c | 3643 | { |
04d2cc77 | 3644 | struct vcpu_svm *svm = to_svm(vcpu); |
c1150d8c | 3645 | |
44c11430 GN |
3646 | if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK)) |
3647 | == HF_NMI_MASK) | |
3648 | return; /* IRET will cause a vm exit */ | |
3649 | ||
e0231715 JR |
3650 | /* |
3651 | * Something prevents NMI from been injected. Single step over possible | |
3652 | * problem (IRET or exception injection or interrupt shadow) | |
3653 | */ | |
6be7d306 | 3654 | svm->nmi_singlestep = true; |
44c11430 GN |
3655 | svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF); |
3656 | update_db_intercept(vcpu); | |
c1150d8c DL |
3657 | } |
3658 | ||
cbc94022 IE |
3659 | static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr) |
3660 | { | |
3661 | return 0; | |
3662 | } | |
3663 | ||
d9e368d6 AK |
3664 | static void svm_flush_tlb(struct kvm_vcpu *vcpu) |
3665 | { | |
38e5e92f JR |
3666 | struct vcpu_svm *svm = to_svm(vcpu); |
3667 | ||
3668 | if (static_cpu_has(X86_FEATURE_FLUSHBYASID)) | |
3669 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ASID; | |
3670 | else | |
3671 | svm->asid_generation--; | |
d9e368d6 AK |
3672 | } |
3673 | ||
04d2cc77 AK |
3674 | static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu) |
3675 | { | |
3676 | } | |
3677 | ||
d7bf8221 JR |
3678 | static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu) |
3679 | { | |
3680 | struct vcpu_svm *svm = to_svm(vcpu); | |
3681 | ||
2030753d | 3682 | if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
88ab24ad JR |
3683 | return; |
3684 | ||
4ee546b4 | 3685 | if (!is_cr_intercept(svm, INTERCEPT_CR8_WRITE)) { |
d7bf8221 | 3686 | int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK; |
615d5193 | 3687 | kvm_set_cr8(vcpu, cr8); |
d7bf8221 JR |
3688 | } |
3689 | } | |
3690 | ||
649d6864 JR |
3691 | static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu) |
3692 | { | |
3693 | struct vcpu_svm *svm = to_svm(vcpu); | |
3694 | u64 cr8; | |
3695 | ||
2030753d | 3696 | if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK)) |
88ab24ad JR |
3697 | return; |
3698 | ||
649d6864 JR |
3699 | cr8 = kvm_get_cr8(vcpu); |
3700 | svm->vmcb->control.int_ctl &= ~V_TPR_MASK; | |
3701 | svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK; | |
3702 | } | |
3703 | ||
9222be18 GN |
3704 | static void svm_complete_interrupts(struct vcpu_svm *svm) |
3705 | { | |
3706 | u8 vector; | |
3707 | int type; | |
3708 | u32 exitintinfo = svm->vmcb->control.exit_int_info; | |
66b7138f JK |
3709 | unsigned int3_injected = svm->int3_injected; |
3710 | ||
3711 | svm->int3_injected = 0; | |
9222be18 | 3712 | |
bd3d1ec3 AK |
3713 | /* |
3714 | * If we've made progress since setting HF_IRET_MASK, we've | |
3715 | * executed an IRET and can allow NMI injection. | |
3716 | */ | |
3717 | if ((svm->vcpu.arch.hflags & HF_IRET_MASK) | |
3718 | && kvm_rip_read(&svm->vcpu) != svm->nmi_iret_rip) { | |
44c11430 | 3719 | svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK); |
3842d135 AK |
3720 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
3721 | } | |
44c11430 | 3722 | |
9222be18 GN |
3723 | svm->vcpu.arch.nmi_injected = false; |
3724 | kvm_clear_exception_queue(&svm->vcpu); | |
3725 | kvm_clear_interrupt_queue(&svm->vcpu); | |
3726 | ||
3727 | if (!(exitintinfo & SVM_EXITINTINFO_VALID)) | |
3728 | return; | |
3729 | ||
3842d135 AK |
3730 | kvm_make_request(KVM_REQ_EVENT, &svm->vcpu); |
3731 | ||
9222be18 GN |
3732 | vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK; |
3733 | type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK; | |
3734 | ||
3735 | switch (type) { | |
3736 | case SVM_EXITINTINFO_TYPE_NMI: | |
3737 | svm->vcpu.arch.nmi_injected = true; | |
3738 | break; | |
3739 | case SVM_EXITINTINFO_TYPE_EXEPT: | |
66b7138f JK |
3740 | /* |
3741 | * In case of software exceptions, do not reinject the vector, | |
3742 | * but re-execute the instruction instead. Rewind RIP first | |
3743 | * if we emulated INT3 before. | |
3744 | */ | |
3745 | if (kvm_exception_is_soft(vector)) { | |
3746 | if (vector == BP_VECTOR && int3_injected && | |
3747 | kvm_is_linear_rip(&svm->vcpu, svm->int3_rip)) | |
3748 | kvm_rip_write(&svm->vcpu, | |
3749 | kvm_rip_read(&svm->vcpu) - | |
3750 | int3_injected); | |
9222be18 | 3751 | break; |
66b7138f | 3752 | } |
9222be18 GN |
3753 | if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) { |
3754 | u32 err = svm->vmcb->control.exit_int_info_err; | |
ce7ddec4 | 3755 | kvm_requeue_exception_e(&svm->vcpu, vector, err); |
9222be18 GN |
3756 | |
3757 | } else | |
ce7ddec4 | 3758 | kvm_requeue_exception(&svm->vcpu, vector); |
9222be18 GN |
3759 | break; |
3760 | case SVM_EXITINTINFO_TYPE_INTR: | |
66fd3f7f | 3761 | kvm_queue_interrupt(&svm->vcpu, vector, false); |
9222be18 GN |
3762 | break; |
3763 | default: | |
3764 | break; | |
3765 | } | |
3766 | } | |
3767 | ||
b463a6f7 AK |
3768 | static void svm_cancel_injection(struct kvm_vcpu *vcpu) |
3769 | { | |
3770 | struct vcpu_svm *svm = to_svm(vcpu); | |
3771 | struct vmcb_control_area *control = &svm->vmcb->control; | |
3772 | ||
3773 | control->exit_int_info = control->event_inj; | |
3774 | control->exit_int_info_err = control->event_inj_err; | |
3775 | control->event_inj = 0; | |
3776 | svm_complete_interrupts(svm); | |
3777 | } | |
3778 | ||
80e31d4f AK |
3779 | #ifdef CONFIG_X86_64 |
3780 | #define R "r" | |
3781 | #else | |
3782 | #define R "e" | |
3783 | #endif | |
3784 | ||
851ba692 | 3785 | static void svm_vcpu_run(struct kvm_vcpu *vcpu) |
6aa8b732 | 3786 | { |
a2fa3e9f | 3787 | struct vcpu_svm *svm = to_svm(vcpu); |
d9e368d6 | 3788 | |
2041a06a JR |
3789 | svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX]; |
3790 | svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP]; | |
3791 | svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP]; | |
3792 | ||
cd3ff653 JR |
3793 | /* |
3794 | * A vmexit emulation is required before the vcpu can be executed | |
3795 | * again. | |
3796 | */ | |
3797 | if (unlikely(svm->nested.exit_required)) | |
3798 | return; | |
3799 | ||
e756fc62 | 3800 | pre_svm_run(svm); |
6aa8b732 | 3801 | |
649d6864 JR |
3802 | sync_lapic_to_cr8(vcpu); |
3803 | ||
cda0ffdd | 3804 | svm->vmcb->save.cr2 = vcpu->arch.cr2; |
6aa8b732 | 3805 | |
04d2cc77 AK |
3806 | clgi(); |
3807 | ||
3808 | local_irq_enable(); | |
36241b8c | 3809 | |
6aa8b732 | 3810 | asm volatile ( |
80e31d4f AK |
3811 | "push %%"R"bp; \n\t" |
3812 | "mov %c[rbx](%[svm]), %%"R"bx \n\t" | |
3813 | "mov %c[rcx](%[svm]), %%"R"cx \n\t" | |
3814 | "mov %c[rdx](%[svm]), %%"R"dx \n\t" | |
3815 | "mov %c[rsi](%[svm]), %%"R"si \n\t" | |
3816 | "mov %c[rdi](%[svm]), %%"R"di \n\t" | |
3817 | "mov %c[rbp](%[svm]), %%"R"bp \n\t" | |
05b3e0c2 | 3818 | #ifdef CONFIG_X86_64 |
fb3f0f51 RR |
3819 | "mov %c[r8](%[svm]), %%r8 \n\t" |
3820 | "mov %c[r9](%[svm]), %%r9 \n\t" | |
3821 | "mov %c[r10](%[svm]), %%r10 \n\t" | |
3822 | "mov %c[r11](%[svm]), %%r11 \n\t" | |
3823 | "mov %c[r12](%[svm]), %%r12 \n\t" | |
3824 | "mov %c[r13](%[svm]), %%r13 \n\t" | |
3825 | "mov %c[r14](%[svm]), %%r14 \n\t" | |
3826 | "mov %c[r15](%[svm]), %%r15 \n\t" | |
6aa8b732 AK |
3827 | #endif |
3828 | ||
6aa8b732 | 3829 | /* Enter guest mode */ |
80e31d4f AK |
3830 | "push %%"R"ax \n\t" |
3831 | "mov %c[vmcb](%[svm]), %%"R"ax \n\t" | |
4ecac3fd AK |
3832 | __ex(SVM_VMLOAD) "\n\t" |
3833 | __ex(SVM_VMRUN) "\n\t" | |
3834 | __ex(SVM_VMSAVE) "\n\t" | |
80e31d4f | 3835 | "pop %%"R"ax \n\t" |
6aa8b732 AK |
3836 | |
3837 | /* Save guest registers, load host registers */ | |
80e31d4f AK |
3838 | "mov %%"R"bx, %c[rbx](%[svm]) \n\t" |
3839 | "mov %%"R"cx, %c[rcx](%[svm]) \n\t" | |
3840 | "mov %%"R"dx, %c[rdx](%[svm]) \n\t" | |
3841 | "mov %%"R"si, %c[rsi](%[svm]) \n\t" | |
3842 | "mov %%"R"di, %c[rdi](%[svm]) \n\t" | |
3843 | "mov %%"R"bp, %c[rbp](%[svm]) \n\t" | |
05b3e0c2 | 3844 | #ifdef CONFIG_X86_64 |
fb3f0f51 RR |
3845 | "mov %%r8, %c[r8](%[svm]) \n\t" |
3846 | "mov %%r9, %c[r9](%[svm]) \n\t" | |
3847 | "mov %%r10, %c[r10](%[svm]) \n\t" | |
3848 | "mov %%r11, %c[r11](%[svm]) \n\t" | |
3849 | "mov %%r12, %c[r12](%[svm]) \n\t" | |
3850 | "mov %%r13, %c[r13](%[svm]) \n\t" | |
3851 | "mov %%r14, %c[r14](%[svm]) \n\t" | |
3852 | "mov %%r15, %c[r15](%[svm]) \n\t" | |
6aa8b732 | 3853 | #endif |
80e31d4f | 3854 | "pop %%"R"bp" |
6aa8b732 | 3855 | : |
fb3f0f51 | 3856 | : [svm]"a"(svm), |
6aa8b732 | 3857 | [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)), |
ad312c7c ZX |
3858 | [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])), |
3859 | [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])), | |
3860 | [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])), | |
3861 | [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])), | |
3862 | [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])), | |
3863 | [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP])) | |
05b3e0c2 | 3864 | #ifdef CONFIG_X86_64 |
ad312c7c ZX |
3865 | , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])), |
3866 | [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])), | |
3867 | [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])), | |
3868 | [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])), | |
3869 | [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])), | |
3870 | [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])), | |
3871 | [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])), | |
3872 | [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15])) | |
6aa8b732 | 3873 | #endif |
54a08c04 | 3874 | : "cc", "memory" |
80e31d4f | 3875 | , R"bx", R"cx", R"dx", R"si", R"di" |
54a08c04 | 3876 | #ifdef CONFIG_X86_64 |
54a08c04 LV |
3877 | , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15" |
3878 | #endif | |
3879 | ); | |
6aa8b732 | 3880 | |
82ca2d10 AK |
3881 | #ifdef CONFIG_X86_64 |
3882 | wrmsrl(MSR_GS_BASE, svm->host.gs_base); | |
3883 | #else | |
dacccfdd | 3884 | loadsegment(fs, svm->host.fs); |
831ca609 AK |
3885 | #ifndef CONFIG_X86_32_LAZY_GS |
3886 | loadsegment(gs, svm->host.gs); | |
3887 | #endif | |
9581d442 | 3888 | #endif |
6aa8b732 AK |
3889 | |
3890 | reload_tss(vcpu); | |
3891 | ||
56ba47dd AK |
3892 | local_irq_disable(); |
3893 | ||
13c34e07 AK |
3894 | vcpu->arch.cr2 = svm->vmcb->save.cr2; |
3895 | vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax; | |
3896 | vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp; | |
3897 | vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip; | |
3898 | ||
1e2b1dd7 JK |
3899 | trace_kvm_exit(svm->vmcb->control.exit_code, vcpu, KVM_ISA_SVM); |
3900 | ||
3781c01c JR |
3901 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) |
3902 | kvm_before_handle_nmi(&svm->vcpu); | |
3903 | ||
3904 | stgi(); | |
3905 | ||
3906 | /* Any pending NMI will happen here */ | |
3907 | ||
3908 | if (unlikely(svm->vmcb->control.exit_code == SVM_EXIT_NMI)) | |
3909 | kvm_after_handle_nmi(&svm->vcpu); | |
3910 | ||
d7bf8221 JR |
3911 | sync_cr8_to_lapic(vcpu); |
3912 | ||
a2fa3e9f | 3913 | svm->next_rip = 0; |
9222be18 | 3914 | |
38e5e92f JR |
3915 | svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING; |
3916 | ||
631bc487 GN |
3917 | /* if exit due to PF check for async PF */ |
3918 | if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) | |
3919 | svm->apf_reason = kvm_read_and_reset_pf_reason(); | |
3920 | ||
6de4f3ad AK |
3921 | if (npt_enabled) { |
3922 | vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR); | |
3923 | vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR); | |
3924 | } | |
fe5913e4 JR |
3925 | |
3926 | /* | |
3927 | * We need to handle MC intercepts here before the vcpu has a chance to | |
3928 | * change the physical cpu | |
3929 | */ | |
3930 | if (unlikely(svm->vmcb->control.exit_code == | |
3931 | SVM_EXIT_EXCP_BASE + MC_VECTOR)) | |
3932 | svm_handle_mce(svm); | |
8d28fec4 RJ |
3933 | |
3934 | mark_all_clean(svm->vmcb); | |
6aa8b732 AK |
3935 | } |
3936 | ||
80e31d4f AK |
3937 | #undef R |
3938 | ||
6aa8b732 AK |
3939 | static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
3940 | { | |
a2fa3e9f GH |
3941 | struct vcpu_svm *svm = to_svm(vcpu); |
3942 | ||
3943 | svm->vmcb->save.cr3 = root; | |
dcca1a65 | 3944 | mark_dirty(svm->vmcb, VMCB_CR); |
f40f6a45 | 3945 | svm_flush_tlb(vcpu); |
6aa8b732 AK |
3946 | } |
3947 | ||
1c97f0a0 JR |
3948 | static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root) |
3949 | { | |
3950 | struct vcpu_svm *svm = to_svm(vcpu); | |
3951 | ||
3952 | svm->vmcb->control.nested_cr3 = root; | |
b2747166 | 3953 | mark_dirty(svm->vmcb, VMCB_NPT); |
1c97f0a0 JR |
3954 | |
3955 | /* Also sync guest cr3 here in case we live migrate */ | |
9f8fe504 | 3956 | svm->vmcb->save.cr3 = kvm_read_cr3(vcpu); |
dcca1a65 | 3957 | mark_dirty(svm->vmcb, VMCB_CR); |
1c97f0a0 | 3958 | |
f40f6a45 | 3959 | svm_flush_tlb(vcpu); |
1c97f0a0 JR |
3960 | } |
3961 | ||
6aa8b732 AK |
3962 | static int is_disabled(void) |
3963 | { | |
6031a61c JR |
3964 | u64 vm_cr; |
3965 | ||
3966 | rdmsrl(MSR_VM_CR, vm_cr); | |
3967 | if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE)) | |
3968 | return 1; | |
3969 | ||
6aa8b732 AK |
3970 | return 0; |
3971 | } | |
3972 | ||
102d8325 IM |
3973 | static void |
3974 | svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall) | |
3975 | { | |
3976 | /* | |
3977 | * Patch in the VMMCALL instruction: | |
3978 | */ | |
3979 | hypercall[0] = 0x0f; | |
3980 | hypercall[1] = 0x01; | |
3981 | hypercall[2] = 0xd9; | |
102d8325 IM |
3982 | } |
3983 | ||
002c7f7c YS |
3984 | static void svm_check_processor_compat(void *rtn) |
3985 | { | |
3986 | *(int *)rtn = 0; | |
3987 | } | |
3988 | ||
774ead3a AK |
3989 | static bool svm_cpu_has_accelerated_tpr(void) |
3990 | { | |
3991 | return false; | |
3992 | } | |
3993 | ||
4b12f0de | 3994 | static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio) |
64d4d521 SY |
3995 | { |
3996 | return 0; | |
3997 | } | |
3998 | ||
0e851880 SY |
3999 | static void svm_cpuid_update(struct kvm_vcpu *vcpu) |
4000 | { | |
4001 | } | |
4002 | ||
d4330ef2 JR |
4003 | static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry) |
4004 | { | |
c2c63a49 | 4005 | switch (func) { |
4c62a2dc JR |
4006 | case 0x80000001: |
4007 | if (nested) | |
4008 | entry->ecx |= (1 << 2); /* Set SVM bit */ | |
4009 | break; | |
c2c63a49 JR |
4010 | case 0x8000000A: |
4011 | entry->eax = 1; /* SVM revision 1 */ | |
4012 | entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper | |
4013 | ASID emulation to nested SVM */ | |
4014 | entry->ecx = 0; /* Reserved */ | |
7a190667 JR |
4015 | entry->edx = 0; /* Per default do not support any |
4016 | additional features */ | |
4017 | ||
4018 | /* Support next_rip if host supports it */ | |
2a6b20b8 | 4019 | if (boot_cpu_has(X86_FEATURE_NRIPS)) |
7a190667 | 4020 | entry->edx |= SVM_FEATURE_NRIP; |
c2c63a49 | 4021 | |
3d4aeaad JR |
4022 | /* Support NPT for the guest if enabled */ |
4023 | if (npt_enabled) | |
4024 | entry->edx |= SVM_FEATURE_NPT; | |
4025 | ||
c2c63a49 JR |
4026 | break; |
4027 | } | |
d4330ef2 JR |
4028 | } |
4029 | ||
17cc3935 | 4030 | static int svm_get_lpage_level(void) |
344f414f | 4031 | { |
17cc3935 | 4032 | return PT_PDPE_LEVEL; |
344f414f JR |
4033 | } |
4034 | ||
4e47c7a6 SY |
4035 | static bool svm_rdtscp_supported(void) |
4036 | { | |
4037 | return false; | |
4038 | } | |
4039 | ||
f5f48ee1 SY |
4040 | static bool svm_has_wbinvd_exit(void) |
4041 | { | |
4042 | return true; | |
4043 | } | |
4044 | ||
02daab21 AK |
4045 | static void svm_fpu_deactivate(struct kvm_vcpu *vcpu) |
4046 | { | |
4047 | struct vcpu_svm *svm = to_svm(vcpu); | |
4048 | ||
18c918c5 | 4049 | set_exception_intercept(svm, NM_VECTOR); |
66a562f7 | 4050 | update_cr0_intercept(svm); |
02daab21 AK |
4051 | } |
4052 | ||
8061252e | 4053 | #define PRE_EX(exit) { .exit_code = (exit), \ |
40e19b51 | 4054 | .stage = X86_ICPT_PRE_EXCEPT, } |
cfec82cb | 4055 | #define POST_EX(exit) { .exit_code = (exit), \ |
40e19b51 | 4056 | .stage = X86_ICPT_POST_EXCEPT, } |
d7eb8203 | 4057 | #define POST_MEM(exit) { .exit_code = (exit), \ |
40e19b51 | 4058 | .stage = X86_ICPT_POST_MEMACCESS, } |
cfec82cb JR |
4059 | |
4060 | static struct __x86_intercept { | |
4061 | u32 exit_code; | |
4062 | enum x86_intercept_stage stage; | |
cfec82cb JR |
4063 | } x86_intercept_map[] = { |
4064 | [x86_intercept_cr_read] = POST_EX(SVM_EXIT_READ_CR0), | |
4065 | [x86_intercept_cr_write] = POST_EX(SVM_EXIT_WRITE_CR0), | |
4066 | [x86_intercept_clts] = POST_EX(SVM_EXIT_WRITE_CR0), | |
4067 | [x86_intercept_lmsw] = POST_EX(SVM_EXIT_WRITE_CR0), | |
4068 | [x86_intercept_smsw] = POST_EX(SVM_EXIT_READ_CR0), | |
3b88e41a JR |
4069 | [x86_intercept_dr_read] = POST_EX(SVM_EXIT_READ_DR0), |
4070 | [x86_intercept_dr_write] = POST_EX(SVM_EXIT_WRITE_DR0), | |
dee6bb70 JR |
4071 | [x86_intercept_sldt] = POST_EX(SVM_EXIT_LDTR_READ), |
4072 | [x86_intercept_str] = POST_EX(SVM_EXIT_TR_READ), | |
4073 | [x86_intercept_lldt] = POST_EX(SVM_EXIT_LDTR_WRITE), | |
4074 | [x86_intercept_ltr] = POST_EX(SVM_EXIT_TR_WRITE), | |
4075 | [x86_intercept_sgdt] = POST_EX(SVM_EXIT_GDTR_READ), | |
4076 | [x86_intercept_sidt] = POST_EX(SVM_EXIT_IDTR_READ), | |
4077 | [x86_intercept_lgdt] = POST_EX(SVM_EXIT_GDTR_WRITE), | |
4078 | [x86_intercept_lidt] = POST_EX(SVM_EXIT_IDTR_WRITE), | |
01de8b09 JR |
4079 | [x86_intercept_vmrun] = POST_EX(SVM_EXIT_VMRUN), |
4080 | [x86_intercept_vmmcall] = POST_EX(SVM_EXIT_VMMCALL), | |
4081 | [x86_intercept_vmload] = POST_EX(SVM_EXIT_VMLOAD), | |
4082 | [x86_intercept_vmsave] = POST_EX(SVM_EXIT_VMSAVE), | |
4083 | [x86_intercept_stgi] = POST_EX(SVM_EXIT_STGI), | |
4084 | [x86_intercept_clgi] = POST_EX(SVM_EXIT_CLGI), | |
4085 | [x86_intercept_skinit] = POST_EX(SVM_EXIT_SKINIT), | |
4086 | [x86_intercept_invlpga] = POST_EX(SVM_EXIT_INVLPGA), | |
d7eb8203 JR |
4087 | [x86_intercept_rdtscp] = POST_EX(SVM_EXIT_RDTSCP), |
4088 | [x86_intercept_monitor] = POST_MEM(SVM_EXIT_MONITOR), | |
4089 | [x86_intercept_mwait] = POST_EX(SVM_EXIT_MWAIT), | |
8061252e JR |
4090 | [x86_intercept_invlpg] = POST_EX(SVM_EXIT_INVLPG), |
4091 | [x86_intercept_invd] = POST_EX(SVM_EXIT_INVD), | |
4092 | [x86_intercept_wbinvd] = POST_EX(SVM_EXIT_WBINVD), | |
4093 | [x86_intercept_wrmsr] = POST_EX(SVM_EXIT_MSR), | |
4094 | [x86_intercept_rdtsc] = POST_EX(SVM_EXIT_RDTSC), | |
4095 | [x86_intercept_rdmsr] = POST_EX(SVM_EXIT_MSR), | |
4096 | [x86_intercept_rdpmc] = POST_EX(SVM_EXIT_RDPMC), | |
4097 | [x86_intercept_cpuid] = PRE_EX(SVM_EXIT_CPUID), | |
4098 | [x86_intercept_rsm] = PRE_EX(SVM_EXIT_RSM), | |
bf608f88 JR |
4099 | [x86_intercept_pause] = PRE_EX(SVM_EXIT_PAUSE), |
4100 | [x86_intercept_pushf] = PRE_EX(SVM_EXIT_PUSHF), | |
4101 | [x86_intercept_popf] = PRE_EX(SVM_EXIT_POPF), | |
4102 | [x86_intercept_intn] = PRE_EX(SVM_EXIT_SWINT), | |
4103 | [x86_intercept_iret] = PRE_EX(SVM_EXIT_IRET), | |
4104 | [x86_intercept_icebp] = PRE_EX(SVM_EXIT_ICEBP), | |
4105 | [x86_intercept_hlt] = POST_EX(SVM_EXIT_HLT), | |
f6511935 JR |
4106 | [x86_intercept_in] = POST_EX(SVM_EXIT_IOIO), |
4107 | [x86_intercept_ins] = POST_EX(SVM_EXIT_IOIO), | |
4108 | [x86_intercept_out] = POST_EX(SVM_EXIT_IOIO), | |
4109 | [x86_intercept_outs] = POST_EX(SVM_EXIT_IOIO), | |
cfec82cb JR |
4110 | }; |
4111 | ||
8061252e | 4112 | #undef PRE_EX |
cfec82cb | 4113 | #undef POST_EX |
d7eb8203 | 4114 | #undef POST_MEM |
cfec82cb | 4115 | |
8a76d7f2 JR |
4116 | static int svm_check_intercept(struct kvm_vcpu *vcpu, |
4117 | struct x86_instruction_info *info, | |
4118 | enum x86_intercept_stage stage) | |
4119 | { | |
cfec82cb JR |
4120 | struct vcpu_svm *svm = to_svm(vcpu); |
4121 | int vmexit, ret = X86EMUL_CONTINUE; | |
4122 | struct __x86_intercept icpt_info; | |
4123 | struct vmcb *vmcb = svm->vmcb; | |
4124 | ||
4125 | if (info->intercept >= ARRAY_SIZE(x86_intercept_map)) | |
4126 | goto out; | |
4127 | ||
4128 | icpt_info = x86_intercept_map[info->intercept]; | |
4129 | ||
40e19b51 | 4130 | if (stage != icpt_info.stage) |
cfec82cb JR |
4131 | goto out; |
4132 | ||
4133 | switch (icpt_info.exit_code) { | |
4134 | case SVM_EXIT_READ_CR0: | |
4135 | if (info->intercept == x86_intercept_cr_read) | |
4136 | icpt_info.exit_code += info->modrm_reg; | |
4137 | break; | |
4138 | case SVM_EXIT_WRITE_CR0: { | |
4139 | unsigned long cr0, val; | |
4140 | u64 intercept; | |
4141 | ||
4142 | if (info->intercept == x86_intercept_cr_write) | |
4143 | icpt_info.exit_code += info->modrm_reg; | |
4144 | ||
4145 | if (icpt_info.exit_code != SVM_EXIT_WRITE_CR0) | |
4146 | break; | |
4147 | ||
4148 | intercept = svm->nested.intercept; | |
4149 | ||
4150 | if (!(intercept & (1ULL << INTERCEPT_SELECTIVE_CR0))) | |
4151 | break; | |
4152 | ||
4153 | cr0 = vcpu->arch.cr0 & ~SVM_CR0_SELECTIVE_MASK; | |
4154 | val = info->src_val & ~SVM_CR0_SELECTIVE_MASK; | |
4155 | ||
4156 | if (info->intercept == x86_intercept_lmsw) { | |
4157 | cr0 &= 0xfUL; | |
4158 | val &= 0xfUL; | |
4159 | /* lmsw can't clear PE - catch this here */ | |
4160 | if (cr0 & X86_CR0_PE) | |
4161 | val |= X86_CR0_PE; | |
4162 | } | |
4163 | ||
4164 | if (cr0 ^ val) | |
4165 | icpt_info.exit_code = SVM_EXIT_CR0_SEL_WRITE; | |
4166 | ||
4167 | break; | |
4168 | } | |
3b88e41a JR |
4169 | case SVM_EXIT_READ_DR0: |
4170 | case SVM_EXIT_WRITE_DR0: | |
4171 | icpt_info.exit_code += info->modrm_reg; | |
4172 | break; | |
8061252e JR |
4173 | case SVM_EXIT_MSR: |
4174 | if (info->intercept == x86_intercept_wrmsr) | |
4175 | vmcb->control.exit_info_1 = 1; | |
4176 | else | |
4177 | vmcb->control.exit_info_1 = 0; | |
4178 | break; | |
bf608f88 JR |
4179 | case SVM_EXIT_PAUSE: |
4180 | /* | |
4181 | * We get this for NOP only, but pause | |
4182 | * is rep not, check this here | |
4183 | */ | |
4184 | if (info->rep_prefix != REPE_PREFIX) | |
4185 | goto out; | |
f6511935 JR |
4186 | case SVM_EXIT_IOIO: { |
4187 | u64 exit_info; | |
4188 | u32 bytes; | |
4189 | ||
4190 | exit_info = (vcpu->arch.regs[VCPU_REGS_RDX] & 0xffff) << 16; | |
4191 | ||
4192 | if (info->intercept == x86_intercept_in || | |
4193 | info->intercept == x86_intercept_ins) { | |
4194 | exit_info |= SVM_IOIO_TYPE_MASK; | |
4195 | bytes = info->src_bytes; | |
4196 | } else { | |
4197 | bytes = info->dst_bytes; | |
4198 | } | |
4199 | ||
4200 | if (info->intercept == x86_intercept_outs || | |
4201 | info->intercept == x86_intercept_ins) | |
4202 | exit_info |= SVM_IOIO_STR_MASK; | |
4203 | ||
4204 | if (info->rep_prefix) | |
4205 | exit_info |= SVM_IOIO_REP_MASK; | |
4206 | ||
4207 | bytes = min(bytes, 4u); | |
4208 | ||
4209 | exit_info |= bytes << SVM_IOIO_SIZE_SHIFT; | |
4210 | ||
4211 | exit_info |= (u32)info->ad_bytes << (SVM_IOIO_ASIZE_SHIFT - 1); | |
4212 | ||
4213 | vmcb->control.exit_info_1 = exit_info; | |
4214 | vmcb->control.exit_info_2 = info->next_rip; | |
4215 | ||
4216 | break; | |
4217 | } | |
cfec82cb JR |
4218 | default: |
4219 | break; | |
4220 | } | |
4221 | ||
4222 | vmcb->control.next_rip = info->next_rip; | |
4223 | vmcb->control.exit_code = icpt_info.exit_code; | |
4224 | vmexit = nested_svm_exit_handled(svm); | |
4225 | ||
4226 | ret = (vmexit == NESTED_EXIT_DONE) ? X86EMUL_INTERCEPTED | |
4227 | : X86EMUL_CONTINUE; | |
4228 | ||
4229 | out: | |
4230 | return ret; | |
8a76d7f2 JR |
4231 | } |
4232 | ||
cbdd1bea | 4233 | static struct kvm_x86_ops svm_x86_ops = { |
6aa8b732 AK |
4234 | .cpu_has_kvm_support = has_svm, |
4235 | .disabled_by_bios = is_disabled, | |
4236 | .hardware_setup = svm_hardware_setup, | |
4237 | .hardware_unsetup = svm_hardware_unsetup, | |
002c7f7c | 4238 | .check_processor_compatibility = svm_check_processor_compat, |
6aa8b732 AK |
4239 | .hardware_enable = svm_hardware_enable, |
4240 | .hardware_disable = svm_hardware_disable, | |
774ead3a | 4241 | .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr, |
6aa8b732 AK |
4242 | |
4243 | .vcpu_create = svm_create_vcpu, | |
4244 | .vcpu_free = svm_free_vcpu, | |
04d2cc77 | 4245 | .vcpu_reset = svm_vcpu_reset, |
6aa8b732 | 4246 | |
04d2cc77 | 4247 | .prepare_guest_switch = svm_prepare_guest_switch, |
6aa8b732 AK |
4248 | .vcpu_load = svm_vcpu_load, |
4249 | .vcpu_put = svm_vcpu_put, | |
4250 | ||
4251 | .set_guest_debug = svm_guest_debug, | |
4252 | .get_msr = svm_get_msr, | |
4253 | .set_msr = svm_set_msr, | |
4254 | .get_segment_base = svm_get_segment_base, | |
4255 | .get_segment = svm_get_segment, | |
4256 | .set_segment = svm_set_segment, | |
2e4d2653 | 4257 | .get_cpl = svm_get_cpl, |
1747fb71 | 4258 | .get_cs_db_l_bits = kvm_get_cs_db_l_bits, |
e8467fda | 4259 | .decache_cr0_guest_bits = svm_decache_cr0_guest_bits, |
aff48baa | 4260 | .decache_cr3 = svm_decache_cr3, |
25c4c276 | 4261 | .decache_cr4_guest_bits = svm_decache_cr4_guest_bits, |
6aa8b732 | 4262 | .set_cr0 = svm_set_cr0, |
6aa8b732 AK |
4263 | .set_cr3 = svm_set_cr3, |
4264 | .set_cr4 = svm_set_cr4, | |
4265 | .set_efer = svm_set_efer, | |
4266 | .get_idt = svm_get_idt, | |
4267 | .set_idt = svm_set_idt, | |
4268 | .get_gdt = svm_get_gdt, | |
4269 | .set_gdt = svm_set_gdt, | |
020df079 | 4270 | .set_dr7 = svm_set_dr7, |
6de4f3ad | 4271 | .cache_reg = svm_cache_reg, |
6aa8b732 AK |
4272 | .get_rflags = svm_get_rflags, |
4273 | .set_rflags = svm_set_rflags, | |
6b52d186 | 4274 | .fpu_activate = svm_fpu_activate, |
02daab21 | 4275 | .fpu_deactivate = svm_fpu_deactivate, |
6aa8b732 | 4276 | |
6aa8b732 | 4277 | .tlb_flush = svm_flush_tlb, |
6aa8b732 | 4278 | |
6aa8b732 | 4279 | .run = svm_vcpu_run, |
04d2cc77 | 4280 | .handle_exit = handle_exit, |
6aa8b732 | 4281 | .skip_emulated_instruction = skip_emulated_instruction, |
2809f5d2 GC |
4282 | .set_interrupt_shadow = svm_set_interrupt_shadow, |
4283 | .get_interrupt_shadow = svm_get_interrupt_shadow, | |
102d8325 | 4284 | .patch_hypercall = svm_patch_hypercall, |
2a8067f1 | 4285 | .set_irq = svm_set_irq, |
95ba8273 | 4286 | .set_nmi = svm_inject_nmi, |
298101da | 4287 | .queue_exception = svm_queue_exception, |
b463a6f7 | 4288 | .cancel_injection = svm_cancel_injection, |
78646121 | 4289 | .interrupt_allowed = svm_interrupt_allowed, |
95ba8273 | 4290 | .nmi_allowed = svm_nmi_allowed, |
3cfc3092 JK |
4291 | .get_nmi_mask = svm_get_nmi_mask, |
4292 | .set_nmi_mask = svm_set_nmi_mask, | |
95ba8273 GN |
4293 | .enable_nmi_window = enable_nmi_window, |
4294 | .enable_irq_window = enable_irq_window, | |
4295 | .update_cr8_intercept = update_cr8_intercept, | |
cbc94022 IE |
4296 | |
4297 | .set_tss_addr = svm_set_tss_addr, | |
67253af5 | 4298 | .get_tdp_level = get_npt_level, |
4b12f0de | 4299 | .get_mt_mask = svm_get_mt_mask, |
229456fc | 4300 | |
586f9607 | 4301 | .get_exit_info = svm_get_exit_info, |
586f9607 | 4302 | |
17cc3935 | 4303 | .get_lpage_level = svm_get_lpage_level, |
0e851880 SY |
4304 | |
4305 | .cpuid_update = svm_cpuid_update, | |
4e47c7a6 SY |
4306 | |
4307 | .rdtscp_supported = svm_rdtscp_supported, | |
d4330ef2 JR |
4308 | |
4309 | .set_supported_cpuid = svm_set_supported_cpuid, | |
f5f48ee1 SY |
4310 | |
4311 | .has_wbinvd_exit = svm_has_wbinvd_exit, | |
99e3e30a | 4312 | |
4051b188 | 4313 | .set_tsc_khz = svm_set_tsc_khz, |
99e3e30a | 4314 | .write_tsc_offset = svm_write_tsc_offset, |
e48672fa | 4315 | .adjust_tsc_offset = svm_adjust_tsc_offset, |
857e4099 | 4316 | .compute_tsc_offset = svm_compute_tsc_offset, |
d5c1785d | 4317 | .read_l1_tsc = svm_read_l1_tsc, |
1c97f0a0 JR |
4318 | |
4319 | .set_tdp_cr3 = set_tdp_cr3, | |
8a76d7f2 JR |
4320 | |
4321 | .check_intercept = svm_check_intercept, | |
6aa8b732 AK |
4322 | }; |
4323 | ||
4324 | static int __init svm_init(void) | |
4325 | { | |
cb498ea2 | 4326 | return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm), |
0ee75bea | 4327 | __alignof__(struct vcpu_svm), THIS_MODULE); |
6aa8b732 AK |
4328 | } |
4329 | ||
4330 | static void __exit svm_exit(void) | |
4331 | { | |
cb498ea2 | 4332 | kvm_exit(); |
6aa8b732 AK |
4333 | } |
4334 | ||
4335 | module_init(svm_init) | |
4336 | module_exit(svm_exit) |