blob: 2ae94b540358eb9e824a7ab739271c7e0ac63a22 [file] [log] [blame]
Avi Kivity6aa8b732006-12-10 02:21:36 -08001/*
2 * Kernel-based Virtual Machine driver for Linux
3 *
4 * AMD SVM support
5 *
6 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02007 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -08008 *
9 * Authors:
10 * Yaniv Kamay <yaniv@qumranet.com>
11 * Avi Kivity <avi@qumranet.com>
12 *
13 * This work is licensed under the terms of the GNU GPL, version 2. See
14 * the COPYING file in the top-level directory.
15 *
16 */
Avi Kivityedf88412007-12-16 11:02:48 +020017#include <linux/kvm_host.h>
18
Eddie Dong85f455f2007-07-06 12:20:49 +030019#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080020#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030021#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020022#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040023
Avi Kivity6aa8b732006-12-10 02:21:36 -080024#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020025#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/vmalloc.h>
27#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040028#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030029#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080031
Joerg Roedel67ec6602010-05-17 14:43:35 +020032#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040033#include <asm/desc.h>
Gleb Natapov631bc482010-10-14 11:22:52 +020034#include <asm/kvm_para.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080035
Eduardo Habkost63d11422008-11-17 19:03:20 -020036#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030037#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020038
Avi Kivity4ecac3f2008-05-13 13:23:38 +030039#define __ex(x) __kvm_handle_fault_on_reboot(x)
40
Avi Kivity6aa8b732006-12-10 02:21:36 -080041MODULE_AUTHOR("Qumranet");
42MODULE_LICENSE("GPL");
43
44#define IOPM_ALLOC_ORDER 2
45#define MSRPM_ALLOC_ORDER 1
46
Avi Kivity6aa8b732006-12-10 02:21:36 -080047#define SEG_TYPE_LDT 2
48#define SEG_TYPE_BUSY_TSS16 3
49
Andre Przywara6bc31bd2010-04-11 23:07:28 +020050#define SVM_FEATURE_NPT (1 << 0)
51#define SVM_FEATURE_LBRV (1 << 1)
52#define SVM_FEATURE_SVML (1 << 2)
53#define SVM_FEATURE_NRIP (1 << 3)
54#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030055
Joerg Roedel410e4d52009-08-07 11:49:44 +020056#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
57#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
58#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
59
Joerg Roedel24e09cb2008-02-13 18:58:47 +010060#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
61
Joerg Roedel67ec6602010-05-17 14:43:35 +020062static bool erratum_383_found __read_mostly;
63
Avi Kivity6c8166a2009-05-31 18:15:37 +030064static const u32 host_save_user_msrs[] = {
65#ifdef CONFIG_X86_64
66 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
67 MSR_FS_BASE,
68#endif
69 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
70};
71
72#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
73
74struct kvm_vcpu;
75
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020076struct nested_state {
77 struct vmcb *hsave;
78 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010079 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020080 u64 vmcb;
81
82 /* These are the merged vectors */
83 u32 *msrpm;
84
85 /* gpa pointers to the real vectors */
86 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010087 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020088
Joerg Roedelcd3ff652009-10-09 16:08:26 +020089 /* A VMEXIT is required but not yet emulated */
90 bool exit_required;
91
Joerg Roedelcda00082010-09-02 17:29:46 +020092 /*
93 * If we vmexit during an instruction emulation we need this to restore
94 * the l1 guest rip after the emulation
95 */
96 unsigned long vmexit_rip;
97 unsigned long vmexit_rsp;
98 unsigned long vmexit_rax;
99
Joerg Roedelaad42c62009-08-07 11:49:34 +0200100 /* cache for intercepts of the guest */
101 u16 intercept_cr_read;
102 u16 intercept_cr_write;
103 u16 intercept_dr_read;
104 u16 intercept_dr_write;
105 u32 intercept_exceptions;
106 u64 intercept;
107
Joerg Roedel5bd2edc2010-09-10 17:31:02 +0200108 /* Nested Paging related state */
109 u64 nested_cr3;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200110};
111
Joerg Roedel323c3d82010-03-01 15:34:37 +0100112#define MSRPM_OFFSETS 16
113static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
114
Avi Kivity6c8166a2009-05-31 18:15:37 +0300115struct vcpu_svm {
116 struct kvm_vcpu vcpu;
117 struct vmcb *vmcb;
118 unsigned long vmcb_pa;
119 struct svm_cpu_data *svm_data;
120 uint64_t asid_generation;
121 uint64_t sysenter_esp;
122 uint64_t sysenter_eip;
123
124 u64 next_rip;
125
126 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
Avi Kivityafe9e662010-10-21 12:20:32 +0200127 struct {
Avi Kivitydacccfd2010-10-21 12:20:33 +0200128 u16 fs;
129 u16 gs;
130 u16 ldt;
Avi Kivityafe9e662010-10-21 12:20:32 +0200131 u64 gs_base;
132 } host;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300133
134 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300135
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200136 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200137
138 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100139
140 unsigned int3_injected;
141 unsigned long int3_rip;
Gleb Natapov631bc482010-10-14 11:22:52 +0200142 u32 apf_reason;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300143};
144
Joerg Roedel455716f2010-03-01 15:34:35 +0100145#define MSR_INVALID 0xffffffffU
146
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100147static struct svm_direct_access_msrs {
148 u32 index; /* Index of the MSR */
149 bool always; /* True if intercept is always on */
150} direct_access_msrs[] = {
Brian Gerst8c065852010-07-17 09:03:26 -0400151 { .index = MSR_STAR, .always = true },
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100152 { .index = MSR_IA32_SYSENTER_CS, .always = true },
153#ifdef CONFIG_X86_64
154 { .index = MSR_GS_BASE, .always = true },
155 { .index = MSR_FS_BASE, .always = true },
156 { .index = MSR_KERNEL_GS_BASE, .always = true },
157 { .index = MSR_LSTAR, .always = true },
158 { .index = MSR_CSTAR, .always = true },
159 { .index = MSR_SYSCALL_MASK, .always = true },
160#endif
161 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
162 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
163 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
164 { .index = MSR_IA32_LASTINTTOIP, .always = false },
165 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800166};
167
168/* enable NPT for AMD64 and X86 with PAE */
169#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
170static bool npt_enabled = true;
171#else
Joerg Roedele0231712010-02-24 18:59:10 +0100172static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800173#endif
174static int npt = 1;
175
176module_param(npt, int, S_IRUGO);
177
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200178static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800179module_param(nested, int, S_IRUGO);
180
181static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200182static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800183
Joerg Roedel410e4d52009-08-07 11:49:44 +0200184static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100185static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800186static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800187static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
188 bool has_error_code, u32 error_code);
189
190static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
191{
192 return container_of(vcpu, struct vcpu_svm, vcpu);
193}
194
Joerg Roedel2af91942009-08-07 11:49:28 +0200195static inline void enable_gif(struct vcpu_svm *svm)
196{
197 svm->vcpu.arch.hflags |= HF_GIF_MASK;
198}
199
200static inline void disable_gif(struct vcpu_svm *svm)
201{
202 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
203}
204
205static inline bool gif_set(struct vcpu_svm *svm)
206{
207 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
208}
209
Avi Kivity6aa8b732006-12-10 02:21:36 -0800210static unsigned long iopm_base;
211
212struct kvm_ldttss_desc {
213 u16 limit0;
214 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100215 unsigned base1:8, type:5, dpl:2, p:1;
216 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800217 u32 base3;
218 u32 zero1;
219} __attribute__((packed));
220
221struct svm_cpu_data {
222 int cpu;
223
Avi Kivity5008fdf2007-04-02 13:05:50 +0300224 u64 asid_generation;
225 u32 max_asid;
226 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800227 struct kvm_ldttss_desc *tss_desc;
228
229 struct page *save_area;
230};
231
232static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300233static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800234
235struct svm_init_data {
236 int cpu;
237 int r;
238};
239
240static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
241
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200242#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800243#define MSRS_RANGE_SIZE 2048
244#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
245
Joerg Roedel455716f2010-03-01 15:34:35 +0100246static u32 svm_msrpm_offset(u32 msr)
247{
248 u32 offset;
249 int i;
250
251 for (i = 0; i < NUM_MSR_MAPS; i++) {
252 if (msr < msrpm_ranges[i] ||
253 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
254 continue;
255
256 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
257 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
258
259 /* Now we have the u8 offset - but need the u32 offset */
260 return offset / 4;
261 }
262
263 /* MSR not in any range */
264 return MSR_INVALID;
265}
266
Avi Kivity6aa8b732006-12-10 02:21:36 -0800267#define MAX_INST_SIZE 15
268
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269static inline void clgi(void)
270{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300271 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800272}
273
274static inline void stgi(void)
275{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300276 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800277}
278
279static inline void invlpga(unsigned long addr, u32 asid)
280{
Joerg Roedele0231712010-02-24 18:59:10 +0100281 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282}
283
Avi Kivity6aa8b732006-12-10 02:21:36 -0800284static inline void force_new_asid(struct kvm_vcpu *vcpu)
285{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400286 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800287}
288
289static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
290{
291 force_new_asid(vcpu);
292}
293
Joerg Roedel4b161842010-09-10 17:31:03 +0200294static int get_npt_level(void)
295{
296#ifdef CONFIG_X86_64
297 return PT64_ROOT_LEVEL;
298#else
299 return PT32E_ROOT_LEVEL;
300#endif
301}
302
Avi Kivity6aa8b732006-12-10 02:21:36 -0800303static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
304{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000305 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100306 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600307 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800308
Alexander Graf9962d032008-11-25 20:17:02 +0100309 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800310}
311
Avi Kivity6aa8b732006-12-10 02:21:36 -0800312static int is_external_interrupt(u32 info)
313{
314 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
315 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
316}
317
Glauber Costa2809f5d2009-05-12 16:21:05 -0400318static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
319{
320 struct vcpu_svm *svm = to_svm(vcpu);
321 u32 ret = 0;
322
323 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100324 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400325 return ret & mask;
326}
327
328static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
329{
330 struct vcpu_svm *svm = to_svm(vcpu);
331
332 if (mask == 0)
333 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
334 else
335 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
336
337}
338
Avi Kivity6aa8b732006-12-10 02:21:36 -0800339static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
340{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400341 struct vcpu_svm *svm = to_svm(vcpu);
342
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200343 if (svm->vmcb->control.next_rip != 0)
344 svm->next_rip = svm->vmcb->control.next_rip;
345
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400346 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300347 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300348 EMULATE_DONE)
349 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800350 return;
351 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300352 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
353 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
354 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800355
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300356 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400357 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800358}
359
Jan Kiszka116a4752010-02-23 17:47:54 +0100360static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200361 bool has_error_code, u32 error_code,
362 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100363{
364 struct vcpu_svm *svm = to_svm(vcpu);
365
Joerg Roedele0231712010-02-24 18:59:10 +0100366 /*
367 * If we are within a nested VM we'd better #VMEXIT and let the guest
368 * handle the exception
369 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200370 if (!reinject &&
371 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100372 return;
373
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200374 if (nr == BP_VECTOR && !static_cpu_has(X86_FEATURE_NRIPS)) {
Jan Kiszka66b71382010-02-23 17:47:56 +0100375 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
376
377 /*
378 * For guest debugging where we have to reinject #BP if some
379 * INT3 is guest-owned:
380 * Emulate nRIP by moving RIP forward. Will fail if injection
381 * raises a fault that is not intercepted. Still better than
382 * failing in all cases.
383 */
384 skip_emulated_instruction(&svm->vcpu);
385 rip = kvm_rip_read(&svm->vcpu);
386 svm->int3_rip = rip + svm->vmcb->save.cs.base;
387 svm->int3_injected = rip - old_rip;
388 }
389
Jan Kiszka116a4752010-02-23 17:47:54 +0100390 svm->vmcb->control.event_inj = nr
391 | SVM_EVTINJ_VALID
392 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
393 | SVM_EVTINJ_TYPE_EXEPT;
394 svm->vmcb->control.event_inj_err = error_code;
395}
396
Joerg Roedel67ec6602010-05-17 14:43:35 +0200397static void svm_init_erratum_383(void)
398{
399 u32 low, high;
400 int err;
401 u64 val;
402
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200403 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200404 return;
405
406 /* Use _safe variants to not break nested virtualization */
407 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
408 if (err)
409 return;
410
411 val |= (1ULL << 47);
412
413 low = lower_32_bits(val);
414 high = upper_32_bits(val);
415
416 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
417
418 erratum_383_found = true;
419}
420
Avi Kivity6aa8b732006-12-10 02:21:36 -0800421static int has_svm(void)
422{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200423 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800424
Eduardo Habkost63d11422008-11-17 19:03:20 -0200425 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800426 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800427 return 0;
428 }
429
Avi Kivity6aa8b732006-12-10 02:21:36 -0800430 return 1;
431}
432
433static void svm_hardware_disable(void *garbage)
434{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200435 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800436}
437
Alexander Graf10474ae2009-09-15 11:37:46 +0200438static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439{
440
Tejun Heo0fe1e002009-10-29 22:34:14 +0900441 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800442 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200443 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 struct desc_struct *gdt;
445 int me = raw_smp_processor_id();
446
Alexander Graf10474ae2009-09-15 11:37:46 +0200447 rdmsrl(MSR_EFER, efer);
448 if (efer & EFER_SVME)
449 return -EBUSY;
450
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000452 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
453 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200454 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900456 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Tejun Heo0fe1e002009-10-29 22:34:14 +0900458 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000459 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800460 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200461 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800462 }
463
Tejun Heo0fe1e002009-10-29 22:34:14 +0900464 sd->asid_generation = 1;
465 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
466 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800467
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200468 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200469 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900470 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800471
Alexander Graf9962d032008-11-25 20:17:02 +0100472 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473
Linus Torvaldsd0316552009-12-14 09:58:24 -0800474 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200475
Joerg Roedel67ec6602010-05-17 14:43:35 +0200476 svm_init_erratum_383();
477
Alexander Graf10474ae2009-09-15 11:37:46 +0200478 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800479}
480
Joerg Roedel0da1db752008-07-02 16:02:11 +0200481static void svm_cpu_uninit(int cpu)
482{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900483 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200484
Tejun Heo0fe1e002009-10-29 22:34:14 +0900485 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200486 return;
487
488 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900489 __free_page(sd->save_area);
490 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200491}
492
Avi Kivity6aa8b732006-12-10 02:21:36 -0800493static int svm_cpu_init(int cpu)
494{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900495 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800496 int r;
497
Tejun Heo0fe1e002009-10-29 22:34:14 +0900498 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
499 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800500 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900501 sd->cpu = cpu;
502 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800503 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900504 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800505 goto err_1;
506
Tejun Heo0fe1e002009-10-29 22:34:14 +0900507 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508
509 return 0;
510
511err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900512 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 return r;
514
515}
516
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100517static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518{
519 int i;
520
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100521 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
522 if (direct_access_msrs[i].index == index)
523 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800524
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100525 return false;
526}
527
Avi Kivity6aa8b732006-12-10 02:21:36 -0800528static void set_msr_interception(u32 *msrpm, unsigned msr,
529 int read, int write)
530{
Joerg Roedel455716f2010-03-01 15:34:35 +0100531 u8 bit_read, bit_write;
532 unsigned long tmp;
533 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800534
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100535 /*
536 * If this warning triggers extend the direct_access_msrs list at the
537 * beginning of the file
538 */
539 WARN_ON(!valid_msr_intercept(msr));
540
Joerg Roedel455716f2010-03-01 15:34:35 +0100541 offset = svm_msrpm_offset(msr);
542 bit_read = 2 * (msr & 0x0f);
543 bit_write = 2 * (msr & 0x0f) + 1;
544 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800545
Joerg Roedel455716f2010-03-01 15:34:35 +0100546 BUG_ON(offset == MSR_INVALID);
547
548 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
549 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
550
551 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800552}
553
Joerg Roedelf65c2292008-02-13 18:58:46 +0100554static void svm_vcpu_init_msrpm(u32 *msrpm)
555{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100556 int i;
557
Joerg Roedelf65c2292008-02-13 18:58:46 +0100558 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
559
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100560 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
561 if (!direct_access_msrs[i].always)
562 continue;
563
564 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
565 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100566}
567
Joerg Roedel323c3d82010-03-01 15:34:37 +0100568static void add_msr_offset(u32 offset)
569{
570 int i;
571
572 for (i = 0; i < MSRPM_OFFSETS; ++i) {
573
574 /* Offset already in list? */
575 if (msrpm_offsets[i] == offset)
576 return;
577
578 /* Slot used by another offset? */
579 if (msrpm_offsets[i] != MSR_INVALID)
580 continue;
581
582 /* Add offset to list */
583 msrpm_offsets[i] = offset;
584
585 return;
586 }
587
588 /*
589 * If this BUG triggers the msrpm_offsets table has an overflow. Just
590 * increase MSRPM_OFFSETS in this case.
591 */
592 BUG();
593}
594
595static void init_msrpm_offsets(void)
596{
597 int i;
598
599 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
600
601 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
602 u32 offset;
603
604 offset = svm_msrpm_offset(direct_access_msrs[i].index);
605 BUG_ON(offset == MSR_INVALID);
606
607 add_msr_offset(offset);
608 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800609}
610
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100611static void svm_enable_lbrv(struct vcpu_svm *svm)
612{
613 u32 *msrpm = svm->msrpm;
614
615 svm->vmcb->control.lbr_ctl = 1;
616 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
617 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
618 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
619 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
620}
621
622static void svm_disable_lbrv(struct vcpu_svm *svm)
623{
624 u32 *msrpm = svm->msrpm;
625
626 svm->vmcb->control.lbr_ctl = 0;
627 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
628 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
629 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
630 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
631}
632
Avi Kivity6aa8b732006-12-10 02:21:36 -0800633static __init int svm_hardware_setup(void)
634{
635 int cpu;
636 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100637 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800638 int r;
639
Avi Kivity6aa8b732006-12-10 02:21:36 -0800640 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
641
642 if (!iopm_pages)
643 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300644
645 iopm_va = page_address(iopm_pages);
646 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
648
Joerg Roedel323c3d82010-03-01 15:34:37 +0100649 init_msrpm_offsets();
650
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100651 if (boot_cpu_has(X86_FEATURE_NX))
652 kvm_enable_efer_bits(EFER_NX);
653
Alexander Graf1b2fd702009-02-02 16:23:51 +0100654 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
655 kvm_enable_efer_bits(EFER_FFXSR);
656
Alexander Graf236de052008-11-25 20:17:10 +0100657 if (nested) {
658 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200659 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100660 }
661
Zachary Amsden3230bb42009-09-29 11:38:37 -1000662 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800663 r = svm_cpu_init(cpu);
664 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100665 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800666 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100667
668 svm_features = cpuid_edx(SVM_CPUID_FUNC);
669
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200670 if (!boot_cpu_has(X86_FEATURE_NPT))
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100671 npt_enabled = false;
672
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100673 if (npt_enabled && !npt) {
674 printk(KERN_INFO "kvm: Nested Paging disabled\n");
675 npt_enabled = false;
676 }
677
Joerg Roedel18552672008-02-07 13:47:41 +0100678 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100679 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100680 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200681 } else
682 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100683
Avi Kivity6aa8b732006-12-10 02:21:36 -0800684 return 0;
685
Joerg Roedelf65c2292008-02-13 18:58:46 +0100686err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800687 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
688 iopm_base = 0;
689 return r;
690}
691
692static __exit void svm_hardware_unsetup(void)
693{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200694 int cpu;
695
Zachary Amsden3230bb42009-09-29 11:38:37 -1000696 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200697 svm_cpu_uninit(cpu);
698
Avi Kivity6aa8b732006-12-10 02:21:36 -0800699 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100700 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800701}
702
703static void init_seg(struct vmcb_seg *seg)
704{
705 seg->selector = 0;
706 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100707 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708 seg->limit = 0xffff;
709 seg->base = 0;
710}
711
712static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
713{
714 seg->selector = 0;
715 seg->attrib = SVM_SELECTOR_P_MASK | type;
716 seg->limit = 0xffff;
717 seg->base = 0;
718}
719
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000720static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
721{
722 struct vcpu_svm *svm = to_svm(vcpu);
723 u64 g_tsc_offset = 0;
724
Joerg Roedel20307532010-11-29 17:51:48 +0100725 if (is_guest_mode(vcpu)) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000726 g_tsc_offset = svm->vmcb->control.tsc_offset -
727 svm->nested.hsave->control.tsc_offset;
728 svm->nested.hsave->control.tsc_offset = offset;
729 }
730
731 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
732}
733
Zachary Amsdene48672f2010-08-19 22:07:23 -1000734static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
735{
736 struct vcpu_svm *svm = to_svm(vcpu);
737
738 svm->vmcb->control.tsc_offset += adjustment;
Joerg Roedel20307532010-11-29 17:51:48 +0100739 if (is_guest_mode(vcpu))
Zachary Amsdene48672f2010-08-19 22:07:23 -1000740 svm->nested.hsave->control.tsc_offset += adjustment;
741}
742
Joerg Roedele6101a92008-02-13 18:58:45 +0100743static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744{
Joerg Roedele6101a92008-02-13 18:58:45 +0100745 struct vmcb_control_area *control = &svm->vmcb->control;
746 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747
Avi Kivitybff78272010-01-07 13:16:08 +0200748 svm->vcpu.fpu_active = 1;
749
Joerg Roedele0231712010-02-24 18:59:10 +0100750 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200752 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800753
Joerg Roedele0231712010-02-24 18:59:10 +0100754 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800755 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200756 INTERCEPT_CR4_MASK |
757 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800758
Joerg Roedele0231712010-02-24 18:59:10 +0100759 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760 INTERCEPT_DR1_MASK |
761 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100762 INTERCEPT_DR3_MASK |
763 INTERCEPT_DR4_MASK |
764 INTERCEPT_DR5_MASK |
765 INTERCEPT_DR6_MASK |
766 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800767
Joerg Roedele0231712010-02-24 18:59:10 +0100768 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800769 INTERCEPT_DR1_MASK |
770 INTERCEPT_DR2_MASK |
771 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100772 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100774 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800775 INTERCEPT_DR7_MASK;
776
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500777 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200778 (1 << UD_VECTOR) |
779 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
781
Joerg Roedele0231712010-02-24 18:59:10 +0100782 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800783 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200784 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200785 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200787 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300789 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790 (1ULL << INTERCEPT_INVLPGA) |
791 (1ULL << INTERCEPT_IOIO_PROT) |
792 (1ULL << INTERCEPT_MSR_PROT) |
793 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800794 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800795 (1ULL << INTERCEPT_VMRUN) |
796 (1ULL << INTERCEPT_VMMCALL) |
797 (1ULL << INTERCEPT_VMLOAD) |
798 (1ULL << INTERCEPT_VMSAVE) |
799 (1ULL << INTERCEPT_STGI) |
800 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100801 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200802 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100803 (1ULL << INTERCEPT_MONITOR) |
804 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800805
806 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100807 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 control->int_ctl = V_INTR_MASKING_MASK;
809
810 init_seg(&save->es);
811 init_seg(&save->ss);
812 init_seg(&save->ds);
813 init_seg(&save->fs);
814 init_seg(&save->gs);
815
816 save->cs.selector = 0xf000;
817 /* Executable/Readable Code Segment */
818 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
819 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
820 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800821 /*
822 * cs.base should really be 0xffff0000, but vmx can't handle that, so
823 * be consistent with it.
824 *
825 * Replace when we have real mode working for vmx.
826 */
827 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800828
829 save->gdtr.limit = 0xffff;
830 save->idtr.limit = 0xffff;
831
832 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
833 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
834
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300835 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400836 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837 save->dr7 = 0x400;
838 save->rflags = 2;
839 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300840 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
Joerg Roedele0231712010-02-24 18:59:10 +0100842 /*
843 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200844 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800845 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300846 svm->vcpu.arch.cr0 = 0;
847 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200848
Rusty Russell66aee912007-07-17 23:34:16 +1000849 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100851
852 if (npt_enabled) {
853 /* Setup VMCB for Nested Paging */
854 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300855 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
856 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100857 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200858 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
859 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100860 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100861 save->cr3 = 0;
862 save->cr4 = 0;
863 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300864 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100865
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200866 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200867 svm->vcpu.arch.hflags = 0;
868
Avi Kivity2a6b20b2010-11-09 16:15:42 +0200869 if (boot_cpu_has(X86_FEATURE_PAUSEFILTER)) {
Mark Langsdorf565d0992009-10-06 14:25:02 -0500870 control->pause_filter_count = 3000;
871 control->intercept |= (1ULL << INTERCEPT_PAUSE);
872 }
873
Joerg Roedel2af91942009-08-07 11:49:28 +0200874 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800875}
876
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200877static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300878{
879 struct vcpu_svm *svm = to_svm(vcpu);
880
Joerg Roedele6101a92008-02-13 18:58:45 +0100881 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200882
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300883 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300884 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800885 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
886 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200887 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300888 vcpu->arch.regs_avail = ~0;
889 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200890
891 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300892}
893
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000894static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800895{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400896 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800897 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100898 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100899 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100900 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000901 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800902
Rusty Russellc16f8622007-07-30 21:12:19 +1000903 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000904 if (!svm) {
905 err = -ENOMEM;
906 goto out;
907 }
908
909 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
910 if (err)
911 goto free_svm;
912
Joerg Roedelf65c2292008-02-13 18:58:46 +0100913 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900914 page = alloc_page(GFP_KERNEL);
915 if (!page)
916 goto uninit;
917
Joerg Roedelf65c2292008-02-13 18:58:46 +0100918 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
919 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900920 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100921
922 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
923 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900924 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100925
Alexander Grafb286d5d2008-11-25 20:17:05 +0100926 hsave_page = alloc_page(GFP_KERNEL);
927 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900928 goto free_page3;
929
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200930 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100931
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900932 svm->msrpm = page_address(msrpm_pages);
933 svm_vcpu_init_msrpm(svm->msrpm);
934
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200935 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100936 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100937
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400938 svm->vmcb = page_address(page);
939 clear_page(svm->vmcb);
940 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
941 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100942 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000943 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400944
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200945 err = fx_init(&svm->vcpu);
946 if (err)
947 goto free_page4;
948
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800949 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300950 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800951 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800952
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000953 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800954
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200955free_page4:
956 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900957free_page3:
958 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
959free_page2:
960 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
961free_page1:
962 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000963uninit:
964 kvm_vcpu_uninit(&svm->vcpu);
965free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000966 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000967out:
968 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800969}
970
971static void svm_free_vcpu(struct kvm_vcpu *vcpu)
972{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400973 struct vcpu_svm *svm = to_svm(vcpu);
974
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000975 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100976 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200977 __free_page(virt_to_page(svm->nested.hsave));
978 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000979 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000980 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981}
982
Avi Kivity15ad7142007-07-11 18:17:21 +0300983static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800984{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400985 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300986 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200987
Avi Kivity0cc50642007-03-25 12:07:27 +0200988 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300989 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200990 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300991
Avi Kivity82ca2d12010-10-21 12:20:34 +0200992#ifdef CONFIG_X86_64
993 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
994#endif
Avi Kivitydacccfd2010-10-21 12:20:33 +0200995 savesegment(fs, svm->host.fs);
996 savesegment(gs, svm->host.gs);
997 svm->host.ldt = kvm_read_ldt();
998
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300999 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001000 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001001}
1002
1003static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1004{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001005 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001006 int i;
1007
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001008 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001009 kvm_load_ldt(svm->host.ldt);
1010#ifdef CONFIG_X86_64
1011 loadsegment(fs, svm->host.fs);
1012 load_gs_index(svm->host.gs);
1013 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1014#else
1015 loadsegment(gs, svm->host.gs);
1016#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001017 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001018 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001019}
1020
Avi Kivity6aa8b732006-12-10 02:21:36 -08001021static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1022{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001023 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001024}
1025
1026static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1027{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001028 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001029}
1030
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001031static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1032{
1033 switch (reg) {
1034 case VCPU_EXREG_PDPTR:
1035 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001036 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001037 break;
1038 default:
1039 BUG();
1040 }
1041}
1042
Alexander Graff0b85052008-11-25 20:17:01 +01001043static void svm_set_vintr(struct vcpu_svm *svm)
1044{
1045 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1046}
1047
1048static void svm_clear_vintr(struct vcpu_svm *svm)
1049{
1050 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1051}
1052
Avi Kivity6aa8b732006-12-10 02:21:36 -08001053static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1054{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001055 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001056
1057 switch (seg) {
1058 case VCPU_SREG_CS: return &save->cs;
1059 case VCPU_SREG_DS: return &save->ds;
1060 case VCPU_SREG_ES: return &save->es;
1061 case VCPU_SREG_FS: return &save->fs;
1062 case VCPU_SREG_GS: return &save->gs;
1063 case VCPU_SREG_SS: return &save->ss;
1064 case VCPU_SREG_TR: return &save->tr;
1065 case VCPU_SREG_LDTR: return &save->ldtr;
1066 }
1067 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001068 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001069}
1070
1071static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1072{
1073 struct vmcb_seg *s = svm_seg(vcpu, seg);
1074
1075 return s->base;
1076}
1077
1078static void svm_get_segment(struct kvm_vcpu *vcpu,
1079 struct kvm_segment *var, int seg)
1080{
1081 struct vmcb_seg *s = svm_seg(vcpu, seg);
1082
1083 var->base = s->base;
1084 var->limit = s->limit;
1085 var->selector = s->selector;
1086 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1087 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1088 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1089 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1090 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1091 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1092 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1093 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001094
Joerg Roedele0231712010-02-24 18:59:10 +01001095 /*
1096 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001097 * for cross vendor migration purposes by "not present"
1098 */
1099 var->unusable = !var->present || (var->type == 0);
1100
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001101 switch (seg) {
1102 case VCPU_SREG_CS:
1103 /*
1104 * SVM always stores 0 for the 'G' bit in the CS selector in
1105 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1106 * Intel's VMENTRY has a check on the 'G' bit.
1107 */
Amit Shah25022ac2008-10-27 09:04:17 +00001108 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001109 break;
1110 case VCPU_SREG_TR:
1111 /*
1112 * Work around a bug where the busy flag in the tr selector
1113 * isn't exposed
1114 */
Amit Shahc0d09822008-10-27 09:04:18 +00001115 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001116 break;
1117 case VCPU_SREG_DS:
1118 case VCPU_SREG_ES:
1119 case VCPU_SREG_FS:
1120 case VCPU_SREG_GS:
1121 /*
1122 * The accessed bit must always be set in the segment
1123 * descriptor cache, although it can be cleared in the
1124 * descriptor, the cached bit always remains at 1. Since
1125 * Intel has a check on this, set it here to support
1126 * cross-vendor migration.
1127 */
1128 if (!var->unusable)
1129 var->type |= 0x1;
1130 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001131 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001132 /*
1133 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001134 * descriptor is left as 1, although the whole segment has
1135 * been made unusable. Clear it here to pass an Intel VMX
1136 * entry check when cross vendor migrating.
1137 */
1138 if (var->unusable)
1139 var->db = 0;
1140 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001141 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001142}
1143
Izik Eidus2e4d2652008-03-24 19:38:34 +02001144static int svm_get_cpl(struct kvm_vcpu *vcpu)
1145{
1146 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1147
1148 return save->cpl;
1149}
1150
Gleb Natapov89a27f42010-02-16 10:51:48 +02001151static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001152{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001153 struct vcpu_svm *svm = to_svm(vcpu);
1154
Gleb Natapov89a27f42010-02-16 10:51:48 +02001155 dt->size = svm->vmcb->save.idtr.limit;
1156 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001157}
1158
Gleb Natapov89a27f42010-02-16 10:51:48 +02001159static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001160{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001161 struct vcpu_svm *svm = to_svm(vcpu);
1162
Gleb Natapov89a27f42010-02-16 10:51:48 +02001163 svm->vmcb->save.idtr.limit = dt->size;
1164 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001165}
1166
Gleb Natapov89a27f42010-02-16 10:51:48 +02001167static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001169 struct vcpu_svm *svm = to_svm(vcpu);
1170
Gleb Natapov89a27f42010-02-16 10:51:48 +02001171 dt->size = svm->vmcb->save.gdtr.limit;
1172 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001173}
1174
Gleb Natapov89a27f42010-02-16 10:51:48 +02001175static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001177 struct vcpu_svm *svm = to_svm(vcpu);
1178
Gleb Natapov89a27f42010-02-16 10:51:48 +02001179 svm->vmcb->save.gdtr.limit = dt->size;
1180 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001181}
1182
Avi Kivitye8467fd2009-12-29 18:43:06 +02001183static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1184{
1185}
1186
Anthony Liguori25c4c272007-04-27 09:29:21 +03001187static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001188{
1189}
1190
Avi Kivityd2251572010-01-06 10:55:27 +02001191static void update_cr0_intercept(struct vcpu_svm *svm)
1192{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001193 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001194 ulong gcr0 = svm->vcpu.arch.cr0;
1195 u64 *hcr0 = &svm->vmcb->save.cr0;
1196
1197 if (!svm->vcpu.fpu_active)
1198 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1199 else
1200 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1201 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1202
1203
1204 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001205 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1206 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
Joerg Roedel20307532010-11-29 17:51:48 +01001207 if (is_guest_mode(&svm->vcpu)) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001208 struct vmcb *hsave = svm->nested.hsave;
1209
1210 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1211 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1212 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1213 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1214 }
Avi Kivityd2251572010-01-06 10:55:27 +02001215 } else {
1216 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1217 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel20307532010-11-29 17:51:48 +01001218 if (is_guest_mode(&svm->vcpu)) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001219 struct vmcb *hsave = svm->nested.hsave;
1220
1221 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1222 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1223 }
Avi Kivityd2251572010-01-06 10:55:27 +02001224 }
1225}
1226
Avi Kivity6aa8b732006-12-10 02:21:36 -08001227static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1228{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001229 struct vcpu_svm *svm = to_svm(vcpu);
1230
Joerg Roedel20307532010-11-29 17:51:48 +01001231 if (is_guest_mode(vcpu)) {
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001232 /*
1233 * We are here because we run in nested mode, the host kvm
1234 * intercepts cr0 writes but the l1 hypervisor does not.
1235 * But the L1 hypervisor may intercept selective cr0 writes.
1236 * This needs to be checked here.
1237 */
1238 unsigned long old, new;
1239
1240 /* Remove bits that would trigger a real cr0 write intercept */
1241 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1242 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1243
1244 if (old == new) {
1245 /* cr0 write with ts and mp unchanged */
1246 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001247 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1248 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1249 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1250 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001251 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001252 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001253 }
1254 }
1255
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001256#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001257 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001258 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001259 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001260 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261 }
1262
Mike Dayd77c26f2007-10-08 09:02:08 -04001263 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001264 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001265 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001266 }
1267 }
1268#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001269 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001270
1271 if (!npt_enabled)
1272 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001273
1274 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001275 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001276 /*
1277 * re-enable caching here because the QEMU bios
1278 * does not do it - this results in some delay at
1279 * reboot
1280 */
1281 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001282 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001283 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001284}
1285
1286static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1287{
Joerg Roedel6394b642008-04-09 14:15:29 +02001288 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001289 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1290
1291 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1292 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001293
Joerg Roedelec077262008-04-09 14:15:28 +02001294 vcpu->arch.cr4 = cr4;
1295 if (!npt_enabled)
1296 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001297 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001298 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299}
1300
1301static void svm_set_segment(struct kvm_vcpu *vcpu,
1302 struct kvm_segment *var, int seg)
1303{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001304 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001305 struct vmcb_seg *s = svm_seg(vcpu, seg);
1306
1307 s->base = var->base;
1308 s->limit = var->limit;
1309 s->selector = var->selector;
1310 if (var->unusable)
1311 s->attrib = 0;
1312 else {
1313 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1314 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1315 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1316 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1317 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1318 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1319 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1320 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1321 }
1322 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001323 svm->vmcb->save.cpl
1324 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001325 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1326
1327}
1328
Gleb Natapov44c11432009-05-11 13:35:52 +03001329static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001330{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001331 struct vcpu_svm *svm = to_svm(vcpu);
1332
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001333 svm->vmcb->control.intercept_exceptions &=
1334 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001335
Jan Kiszka6be7d302009-10-18 13:24:54 +02001336 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001337 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1338
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001339 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1340 if (vcpu->guest_debug &
1341 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1342 svm->vmcb->control.intercept_exceptions |=
1343 1 << DB_VECTOR;
1344 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1345 svm->vmcb->control.intercept_exceptions |=
1346 1 << BP_VECTOR;
1347 } else
1348 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001349}
1350
Jan Kiszka355be0b2009-10-03 00:31:21 +02001351static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001352{
Gleb Natapov44c11432009-05-11 13:35:52 +03001353 struct vcpu_svm *svm = to_svm(vcpu);
1354
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001355 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1356 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1357 else
1358 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1359
Jan Kiszka355be0b2009-10-03 00:31:21 +02001360 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361}
1362
Tejun Heo0fe1e002009-10-29 22:34:14 +09001363static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001364{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001365 if (sd->next_asid > sd->max_asid) {
1366 ++sd->asid_generation;
1367 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001368 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369 }
1370
Tejun Heo0fe1e002009-10-29 22:34:14 +09001371 svm->asid_generation = sd->asid_generation;
1372 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001373}
1374
Gleb Natapov020df072010-04-13 10:05:23 +03001375static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001376{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001377 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001378
Gleb Natapov020df072010-04-13 10:05:23 +03001379 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001380}
1381
Avi Kivity851ba692009-08-24 11:10:17 +03001382static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001383{
Gleb Natapov631bc482010-10-14 11:22:52 +02001384 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001385 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001386 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001387
Gleb Natapov631bc482010-10-14 11:22:52 +02001388 switch (svm->apf_reason) {
1389 default:
1390 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001391
Gleb Natapov631bc482010-10-14 11:22:52 +02001392 trace_kvm_page_fault(fault_address, error_code);
1393 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1394 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1395 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1396 break;
1397 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1398 svm->apf_reason = 0;
1399 local_irq_disable();
1400 kvm_async_pf_task_wait(fault_address);
1401 local_irq_enable();
1402 break;
1403 case KVM_PV_REASON_PAGE_READY:
1404 svm->apf_reason = 0;
1405 local_irq_disable();
1406 kvm_async_pf_task_wake(fault_address);
1407 local_irq_enable();
1408 break;
1409 }
1410 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001411}
1412
Avi Kivity851ba692009-08-24 11:10:17 +03001413static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001414{
Avi Kivity851ba692009-08-24 11:10:17 +03001415 struct kvm_run *kvm_run = svm->vcpu.run;
1416
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001417 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001418 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001419 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001420 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1421 return 1;
1422 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001423
Jan Kiszka6be7d302009-10-18 13:24:54 +02001424 if (svm->nmi_singlestep) {
1425 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001426 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1427 svm->vmcb->save.rflags &=
1428 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1429 update_db_intercept(&svm->vcpu);
1430 }
1431
1432 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001433 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001434 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1435 kvm_run->debug.arch.pc =
1436 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1437 kvm_run->debug.arch.exception = DB_VECTOR;
1438 return 0;
1439 }
1440
1441 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001442}
1443
Avi Kivity851ba692009-08-24 11:10:17 +03001444static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001445{
Avi Kivity851ba692009-08-24 11:10:17 +03001446 struct kvm_run *kvm_run = svm->vcpu.run;
1447
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001448 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1449 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1450 kvm_run->debug.arch.exception = BP_VECTOR;
1451 return 0;
1452}
1453
Avi Kivity851ba692009-08-24 11:10:17 +03001454static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001455{
1456 int er;
1457
Avi Kivity851ba692009-08-24 11:10:17 +03001458 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001459 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001460 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001461 return 1;
1462}
1463
Avi Kivity6b52d182010-01-21 15:31:47 +02001464static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001465{
Avi Kivity6b52d182010-01-21 15:31:47 +02001466 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001467 u32 excp;
1468
Joerg Roedel20307532010-11-29 17:51:48 +01001469 if (is_guest_mode(vcpu)) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001470 u32 h_excp, n_excp;
1471
1472 h_excp = svm->nested.hsave->control.intercept_exceptions;
1473 n_excp = svm->nested.intercept_exceptions;
1474 h_excp &= ~(1 << NM_VECTOR);
1475 excp = h_excp | n_excp;
1476 } else {
1477 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001478 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001479 }
1480
1481 svm->vmcb->control.intercept_exceptions = excp;
1482
Rusty Russelle756fc62007-07-30 20:07:08 +10001483 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001484 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001485}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001486
Avi Kivity6b52d182010-01-21 15:31:47 +02001487static int nm_interception(struct vcpu_svm *svm)
1488{
1489 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001490 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001491}
1492
Joerg Roedel67ec6602010-05-17 14:43:35 +02001493static bool is_erratum_383(void)
1494{
1495 int err, i;
1496 u64 value;
1497
1498 if (!erratum_383_found)
1499 return false;
1500
1501 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1502 if (err)
1503 return false;
1504
1505 /* Bit 62 may or may not be set for this mce */
1506 value &= ~(1ULL << 62);
1507
1508 if (value != 0xb600000000010015ULL)
1509 return false;
1510
1511 /* Clear MCi_STATUS registers */
1512 for (i = 0; i < 6; ++i)
1513 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1514
1515 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1516 if (!err) {
1517 u32 low, high;
1518
1519 value &= ~(1ULL << 2);
1520 low = lower_32_bits(value);
1521 high = upper_32_bits(value);
1522
1523 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1524 }
1525
1526 /* Flush tlb to evict multi-match entries */
1527 __flush_tlb_all();
1528
1529 return true;
1530}
1531
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001532static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001533{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001534 if (is_erratum_383()) {
1535 /*
1536 * Erratum 383 triggered. Guest state is corrupt so kill the
1537 * guest.
1538 */
1539 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1540
Avi Kivitya8eeb042010-05-10 12:34:53 +03001541 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001542
1543 return;
1544 }
1545
Joerg Roedel53371b52008-04-09 14:15:30 +02001546 /*
1547 * On an #MC intercept the MCE handler is not called automatically in
1548 * the host. So do it by hand here.
1549 */
1550 asm volatile (
1551 "int $0x12\n");
1552 /* not sure if we ever come back to this point */
1553
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001554 return;
1555}
1556
1557static int mc_interception(struct vcpu_svm *svm)
1558{
Joerg Roedel53371b52008-04-09 14:15:30 +02001559 return 1;
1560}
1561
Avi Kivity851ba692009-08-24 11:10:17 +03001562static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001563{
Avi Kivity851ba692009-08-24 11:10:17 +03001564 struct kvm_run *kvm_run = svm->vcpu.run;
1565
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001566 /*
1567 * VMCB is undefined after a SHUTDOWN intercept
1568 * so reinitialize it.
1569 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001570 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001571 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001572
1573 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1574 return 0;
1575}
1576
Avi Kivity851ba692009-08-24 11:10:17 +03001577static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001578{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001579 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001580 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001581 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001582 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001583
Rusty Russelle756fc62007-07-30 20:07:08 +10001584 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001585 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001586 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001587 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001588 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001589
Avi Kivity039576c2007-03-20 12:46:50 +02001590 port = io_info >> 16;
1591 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001592 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001593 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001594
1595 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001596}
1597
Avi Kivity851ba692009-08-24 11:10:17 +03001598static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001599{
1600 return 1;
1601}
1602
Avi Kivity851ba692009-08-24 11:10:17 +03001603static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001604{
1605 ++svm->vcpu.stat.irq_exits;
1606 return 1;
1607}
1608
Avi Kivity851ba692009-08-24 11:10:17 +03001609static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610{
1611 return 1;
1612}
1613
Avi Kivity851ba692009-08-24 11:10:17 +03001614static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001615{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001616 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001617 skip_emulated_instruction(&svm->vcpu);
1618 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001619}
1620
Avi Kivity851ba692009-08-24 11:10:17 +03001621static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001622{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001623 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001624 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001625 kvm_emulate_hypercall(&svm->vcpu);
1626 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001627}
1628
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001629static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1630{
1631 struct vcpu_svm *svm = to_svm(vcpu);
1632
1633 return svm->nested.nested_cr3;
1634}
1635
1636static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1637 unsigned long root)
1638{
1639 struct vcpu_svm *svm = to_svm(vcpu);
1640
1641 svm->vmcb->control.nested_cr3 = root;
1642 force_new_asid(vcpu);
1643}
1644
Avi Kivity6389ee92010-11-29 16:12:30 +02001645static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu,
1646 struct x86_exception *fault)
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001647{
1648 struct vcpu_svm *svm = to_svm(vcpu);
1649
1650 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1651 svm->vmcb->control.exit_code_hi = 0;
Avi Kivity6389ee92010-11-29 16:12:30 +02001652 svm->vmcb->control.exit_info_1 = fault->error_code;
1653 svm->vmcb->control.exit_info_2 = fault->address;
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001654
1655 nested_svm_vmexit(svm);
1656}
1657
Joerg Roedel4b161842010-09-10 17:31:03 +02001658static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1659{
1660 int r;
1661
1662 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1663
1664 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1665 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1666 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1667 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1668 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1669
1670 return r;
1671}
1672
1673static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1674{
1675 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1676}
1677
Alexander Grafc0725422008-11-25 20:17:03 +01001678static int nested_svm_check_permissions(struct vcpu_svm *svm)
1679{
Avi Kivityf6801df2010-01-21 15:31:50 +02001680 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001681 || !is_paging(&svm->vcpu)) {
1682 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1683 return 1;
1684 }
1685
1686 if (svm->vmcb->save.cpl) {
1687 kvm_inject_gp(&svm->vcpu, 0);
1688 return 1;
1689 }
1690
1691 return 0;
1692}
1693
Alexander Grafcf74a782008-11-25 20:17:08 +01001694static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1695 bool has_error_code, u32 error_code)
1696{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001697 int vmexit;
1698
Joerg Roedel20307532010-11-29 17:51:48 +01001699 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel0295ad72009-08-07 11:49:37 +02001700 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001701
Joerg Roedel0295ad72009-08-07 11:49:37 +02001702 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1703 svm->vmcb->control.exit_code_hi = 0;
1704 svm->vmcb->control.exit_info_1 = error_code;
1705 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1706
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001707 vmexit = nested_svm_intercept(svm);
1708 if (vmexit == NESTED_EXIT_DONE)
1709 svm->nested.exit_required = true;
1710
1711 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001712}
1713
Joerg Roedel8fe54652010-02-19 16:23:01 +01001714/* This function returns true if it is save to enable the irq window */
1715static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001716{
Joerg Roedel20307532010-11-29 17:51:48 +01001717 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001718 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001719
Joerg Roedel26666952009-08-07 11:49:46 +02001720 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001721 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001722
Joerg Roedel26666952009-08-07 11:49:46 +02001723 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001724 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001725
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001726 /*
1727 * if vmexit was already requested (by intercepted exception
1728 * for instance) do not overwrite it with "external interrupt"
1729 * vmexit.
1730 */
1731 if (svm->nested.exit_required)
1732 return false;
1733
Joerg Roedel197717d2010-02-24 18:59:19 +01001734 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1735 svm->vmcb->control.exit_info_1 = 0;
1736 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001737
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001738 if (svm->nested.intercept & 1ULL) {
1739 /*
1740 * The #vmexit can't be emulated here directly because this
1741 * code path runs with irqs and preemtion disabled. A
1742 * #vmexit emulation might sleep. Only signal request for
1743 * the #vmexit here.
1744 */
1745 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001746 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001747 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001748 }
1749
Joerg Roedel8fe54652010-02-19 16:23:01 +01001750 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001751}
1752
Joerg Roedel887f5002010-02-24 18:59:12 +01001753/* This function returns true if it is save to enable the nmi window */
1754static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1755{
Joerg Roedel20307532010-11-29 17:51:48 +01001756 if (!is_guest_mode(&svm->vcpu))
Joerg Roedel887f5002010-02-24 18:59:12 +01001757 return true;
1758
1759 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1760 return true;
1761
1762 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1763 svm->nested.exit_required = true;
1764
1765 return false;
1766}
1767
Joerg Roedel7597f122010-02-19 16:23:00 +01001768static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001769{
1770 struct page *page;
1771
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001772 might_sleep();
1773
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001774 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001775 if (is_error_page(page))
1776 goto error;
1777
Joerg Roedel7597f122010-02-19 16:23:00 +01001778 *_page = page;
1779
1780 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001781
1782error:
1783 kvm_release_page_clean(page);
1784 kvm_inject_gp(&svm->vcpu, 0);
1785
1786 return NULL;
1787}
1788
Joerg Roedel7597f122010-02-19 16:23:00 +01001789static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001790{
Joerg Roedel7597f122010-02-19 16:23:00 +01001791 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001792 kvm_release_page_dirty(page);
1793}
1794
Joerg Roedelce2ac082010-03-01 15:34:39 +01001795static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001796{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001797 unsigned port;
1798 u8 val, bit;
1799 u64 gpa;
1800
1801 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1802 return NESTED_EXIT_HOST;
1803
1804 port = svm->vmcb->control.exit_info_1 >> 16;
1805 gpa = svm->nested.vmcb_iopm + (port / 8);
1806 bit = port % 8;
1807 val = 0;
1808
1809 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1810 val &= (1 << bit);
1811
1812 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1813}
1814
Joerg Roedeld2477822010-03-01 15:34:34 +01001815static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001816{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001817 u32 offset, msr, value;
1818 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001819
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001820 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001821 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001822
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001823 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1824 offset = svm_msrpm_offset(msr);
1825 write = svm->vmcb->control.exit_info_1 & 1;
1826 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001827
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001828 if (offset == MSR_INVALID)
1829 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001830
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001831 /* Offset is in 32 bit units but need in 8 bit units */
1832 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001833
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001834 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1835 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001836
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001837 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001838}
1839
Joerg Roedel410e4d52009-08-07 11:49:44 +02001840static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001841{
Alexander Grafcf74a782008-11-25 20:17:08 +01001842 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001843
Joerg Roedel410e4d52009-08-07 11:49:44 +02001844 switch (exit_code) {
1845 case SVM_EXIT_INTR:
1846 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001847 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001848 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001849 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001850 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001851 if (npt_enabled)
1852 return NESTED_EXIT_HOST;
1853 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001854 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001855 /* When we're shadowing, trap PFs, but not async PF */
1856 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001857 return NESTED_EXIT_HOST;
1858 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001859 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1860 nm_interception(svm);
1861 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001862 default:
1863 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001864 }
1865
Joerg Roedel410e4d52009-08-07 11:49:44 +02001866 return NESTED_EXIT_CONTINUE;
1867}
1868
1869/*
1870 * If this function returns true, this #vmexit was already handled
1871 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001872static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001873{
1874 u32 exit_code = svm->vmcb->control.exit_code;
1875 int vmexit = NESTED_EXIT_HOST;
1876
Alexander Grafcf74a782008-11-25 20:17:08 +01001877 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001878 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001879 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001880 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001881 case SVM_EXIT_IOIO:
1882 vmexit = nested_svm_intercept_ioio(svm);
1883 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001884 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1885 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001886 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001887 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001888 break;
1889 }
1890 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1891 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001892 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001893 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001894 break;
1895 }
1896 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1897 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001898 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001899 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001900 break;
1901 }
1902 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1903 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001904 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001905 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001906 break;
1907 }
1908 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1909 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001910 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001911 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02001912 /* async page fault always cause vmexit */
1913 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
1914 svm->apf_reason != 0)
1915 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001916 break;
1917 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001918 case SVM_EXIT_ERR: {
1919 vmexit = NESTED_EXIT_DONE;
1920 break;
1921 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001922 default: {
1923 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001924 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001925 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001926 }
1927 }
1928
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001929 return vmexit;
1930}
1931
1932static int nested_svm_exit_handled(struct vcpu_svm *svm)
1933{
1934 int vmexit;
1935
1936 vmexit = nested_svm_intercept(svm);
1937
1938 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001939 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001940
1941 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001942}
1943
Joerg Roedel0460a972009-08-07 11:49:31 +02001944static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1945{
1946 struct vmcb_control_area *dst = &dst_vmcb->control;
1947 struct vmcb_control_area *from = &from_vmcb->control;
1948
1949 dst->intercept_cr_read = from->intercept_cr_read;
1950 dst->intercept_cr_write = from->intercept_cr_write;
1951 dst->intercept_dr_read = from->intercept_dr_read;
1952 dst->intercept_dr_write = from->intercept_dr_write;
1953 dst->intercept_exceptions = from->intercept_exceptions;
1954 dst->intercept = from->intercept;
1955 dst->iopm_base_pa = from->iopm_base_pa;
1956 dst->msrpm_base_pa = from->msrpm_base_pa;
1957 dst->tsc_offset = from->tsc_offset;
1958 dst->asid = from->asid;
1959 dst->tlb_ctl = from->tlb_ctl;
1960 dst->int_ctl = from->int_ctl;
1961 dst->int_vector = from->int_vector;
1962 dst->int_state = from->int_state;
1963 dst->exit_code = from->exit_code;
1964 dst->exit_code_hi = from->exit_code_hi;
1965 dst->exit_info_1 = from->exit_info_1;
1966 dst->exit_info_2 = from->exit_info_2;
1967 dst->exit_int_info = from->exit_int_info;
1968 dst->exit_int_info_err = from->exit_int_info_err;
1969 dst->nested_ctl = from->nested_ctl;
1970 dst->event_inj = from->event_inj;
1971 dst->event_inj_err = from->event_inj_err;
1972 dst->nested_cr3 = from->nested_cr3;
1973 dst->lbr_ctl = from->lbr_ctl;
1974}
1975
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001976static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001977{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001978 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001979 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001980 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001981 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001982
Joerg Roedel17897f32009-10-09 16:08:29 +02001983 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1984 vmcb->control.exit_info_1,
1985 vmcb->control.exit_info_2,
1986 vmcb->control.exit_int_info,
1987 vmcb->control.exit_int_info_err);
1988
Joerg Roedel7597f122010-02-19 16:23:00 +01001989 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001990 if (!nested_vmcb)
1991 return 1;
1992
Joerg Roedel20307532010-11-29 17:51:48 +01001993 /* Exit Guest-Mode */
1994 leave_guest_mode(&svm->vcpu);
Joerg Roedel06fc77722010-02-19 16:23:07 +01001995 svm->nested.vmcb = 0;
1996
Alexander Grafcf74a782008-11-25 20:17:08 +01001997 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001998 disable_gif(svm);
1999
2000 nested_vmcb->save.es = vmcb->save.es;
2001 nested_vmcb->save.cs = vmcb->save.cs;
2002 nested_vmcb->save.ss = vmcb->save.ss;
2003 nested_vmcb->save.ds = vmcb->save.ds;
2004 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2005 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002006 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002007 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002008 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002009 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002010 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002011 nested_vmcb->save.rflags = vmcb->save.rflags;
2012 nested_vmcb->save.rip = vmcb->save.rip;
2013 nested_vmcb->save.rsp = vmcb->save.rsp;
2014 nested_vmcb->save.rax = vmcb->save.rax;
2015 nested_vmcb->save.dr7 = vmcb->save.dr7;
2016 nested_vmcb->save.dr6 = vmcb->save.dr6;
2017 nested_vmcb->save.cpl = vmcb->save.cpl;
2018
2019 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2020 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2021 nested_vmcb->control.int_state = vmcb->control.int_state;
2022 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2023 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2024 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2025 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2026 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2027 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002028 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002029
2030 /*
2031 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2032 * to make sure that we do not lose injected events. So check event_inj
2033 * here and copy it to exit_int_info if it is valid.
2034 * Exit_int_info and event_inj can't be both valid because the case
2035 * below only happens on a VMRUN instruction intercept which has
2036 * no valid exit_int_info set.
2037 */
2038 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2039 struct vmcb_control_area *nc = &nested_vmcb->control;
2040
2041 nc->exit_int_info = vmcb->control.event_inj;
2042 nc->exit_int_info_err = vmcb->control.event_inj_err;
2043 }
2044
Joerg Roedel33740e42009-08-07 11:49:29 +02002045 nested_vmcb->control.tlb_ctl = 0;
2046 nested_vmcb->control.event_inj = 0;
2047 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002048
2049 /* We always set V_INTR_MASKING and remember the old value in hflags */
2050 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2051 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2052
Alexander Grafcf74a782008-11-25 20:17:08 +01002053 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002054 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002055
Alexander Graf219b65d2009-06-15 15:21:25 +02002056 kvm_clear_exception_queue(&svm->vcpu);
2057 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002058
Joerg Roedel4b161842010-09-10 17:31:03 +02002059 svm->nested.nested_cr3 = 0;
2060
Alexander Grafcf74a782008-11-25 20:17:08 +01002061 /* Restore selected save entries */
2062 svm->vmcb->save.es = hsave->save.es;
2063 svm->vmcb->save.cs = hsave->save.cs;
2064 svm->vmcb->save.ss = hsave->save.ss;
2065 svm->vmcb->save.ds = hsave->save.ds;
2066 svm->vmcb->save.gdtr = hsave->save.gdtr;
2067 svm->vmcb->save.idtr = hsave->save.idtr;
2068 svm->vmcb->save.rflags = hsave->save.rflags;
2069 svm_set_efer(&svm->vcpu, hsave->save.efer);
2070 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2071 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2072 if (npt_enabled) {
2073 svm->vmcb->save.cr3 = hsave->save.cr3;
2074 svm->vcpu.arch.cr3 = hsave->save.cr3;
2075 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002076 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002077 }
2078 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2079 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2080 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2081 svm->vmcb->save.dr7 = 0;
2082 svm->vmcb->save.cpl = 0;
2083 svm->vmcb->control.exit_int_info = 0;
2084
Joerg Roedel7597f122010-02-19 16:23:00 +01002085 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002086
Joerg Roedel4b161842010-09-10 17:31:03 +02002087 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002088 kvm_mmu_reset_context(&svm->vcpu);
2089 kvm_mmu_load(&svm->vcpu);
2090
2091 return 0;
2092}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002093
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002094static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002095{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002096 /*
2097 * This function merges the msr permission bitmaps of kvm and the
2098 * nested vmcb. It is omptimized in that it only merges the parts where
2099 * the kvm msr permission bitmap may contain zero bits
2100 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002101 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002102
Joerg Roedel323c3d82010-03-01 15:34:37 +01002103 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2104 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002105
Joerg Roedel323c3d82010-03-01 15:34:37 +01002106 for (i = 0; i < MSRPM_OFFSETS; i++) {
2107 u32 value, p;
2108 u64 offset;
2109
2110 if (msrpm_offsets[i] == 0xffffffff)
2111 break;
2112
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002113 p = msrpm_offsets[i];
2114 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002115
2116 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2117 return false;
2118
2119 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2120 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002121
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002122 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002123
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002124 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002125}
2126
Joerg Roedel52c65a302010-08-02 16:46:44 +02002127static bool nested_vmcb_checks(struct vmcb *vmcb)
2128{
2129 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2130 return false;
2131
Joerg Roedeldbe77582010-08-02 16:46:45 +02002132 if (vmcb->control.asid == 0)
2133 return false;
2134
Joerg Roedel4b161842010-09-10 17:31:03 +02002135 if (vmcb->control.nested_ctl && !npt_enabled)
2136 return false;
2137
Joerg Roedel52c65a302010-08-02 16:46:44 +02002138 return true;
2139}
2140
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002141static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002142{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002143 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002144 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002145 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002146 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002147 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002148
Joerg Roedel06fc77722010-02-19 16:23:07 +01002149 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002150
Joerg Roedel7597f122010-02-19 16:23:00 +01002151 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002152 if (!nested_vmcb)
2153 return false;
2154
Joerg Roedel52c65a302010-08-02 16:46:44 +02002155 if (!nested_vmcb_checks(nested_vmcb)) {
2156 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2157 nested_vmcb->control.exit_code_hi = 0;
2158 nested_vmcb->control.exit_info_1 = 0;
2159 nested_vmcb->control.exit_info_2 = 0;
2160
2161 nested_svm_unmap(page);
2162
2163 return false;
2164 }
2165
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002166 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002167 nested_vmcb->save.rip,
2168 nested_vmcb->control.int_ctl,
2169 nested_vmcb->control.event_inj,
2170 nested_vmcb->control.nested_ctl);
2171
Joerg Roedel2e554e82010-02-24 18:59:14 +01002172 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2173 nested_vmcb->control.intercept_cr_write,
2174 nested_vmcb->control.intercept_exceptions,
2175 nested_vmcb->control.intercept);
2176
Alexander Graf3d6368e2008-11-25 20:17:07 +01002177 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002178 kvm_clear_exception_queue(&svm->vcpu);
2179 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002180
Joerg Roedele0231712010-02-24 18:59:10 +01002181 /*
2182 * Save the old vmcb, so we don't need to pick what we save, but can
2183 * restore everything when a VMEXIT occurs
2184 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002185 hsave->save.es = vmcb->save.es;
2186 hsave->save.cs = vmcb->save.cs;
2187 hsave->save.ss = vmcb->save.ss;
2188 hsave->save.ds = vmcb->save.ds;
2189 hsave->save.gdtr = vmcb->save.gdtr;
2190 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002191 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002192 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002193 hsave->save.cr4 = svm->vcpu.arch.cr4;
2194 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002195 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002196 hsave->save.rsp = vmcb->save.rsp;
2197 hsave->save.rax = vmcb->save.rax;
2198 if (npt_enabled)
2199 hsave->save.cr3 = vmcb->save.cr3;
2200 else
2201 hsave->save.cr3 = svm->vcpu.arch.cr3;
2202
Joerg Roedel0460a972009-08-07 11:49:31 +02002203 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002204
2205 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2206 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2207 else
2208 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2209
Joerg Roedel4b161842010-09-10 17:31:03 +02002210 if (nested_vmcb->control.nested_ctl) {
2211 kvm_mmu_unload(&svm->vcpu);
2212 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2213 nested_svm_init_mmu_context(&svm->vcpu);
2214 }
2215
Alexander Graf3d6368e2008-11-25 20:17:07 +01002216 /* Load the nested guest state */
2217 svm->vmcb->save.es = nested_vmcb->save.es;
2218 svm->vmcb->save.cs = nested_vmcb->save.cs;
2219 svm->vmcb->save.ss = nested_vmcb->save.ss;
2220 svm->vmcb->save.ds = nested_vmcb->save.ds;
2221 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2222 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2223 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2224 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2225 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2226 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2227 if (npt_enabled) {
2228 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2229 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002230 } else
Avi Kivity23902182010-06-10 17:02:16 +03002231 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002232
2233 /* Guest paging mode is active - reset mmu */
2234 kvm_mmu_reset_context(&svm->vcpu);
2235
Joerg Roedeldefbba52009-08-07 11:49:30 +02002236 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002237 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2238 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2239 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002240
Alexander Graf3d6368e2008-11-25 20:17:07 +01002241 /* In case we don't even reach vcpu_run, the fields are not updated */
2242 svm->vmcb->save.rax = nested_vmcb->save.rax;
2243 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2244 svm->vmcb->save.rip = nested_vmcb->save.rip;
2245 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2246 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2247 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2248
Joerg Roedelf7138532010-03-01 15:34:40 +01002249 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002250 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002251
Joerg Roedelaad42c62009-08-07 11:49:34 +02002252 /* cache intercepts */
2253 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2254 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2255 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2256 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2257 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2258 svm->nested.intercept = nested_vmcb->control.intercept;
2259
Alexander Graf3d6368e2008-11-25 20:17:07 +01002260 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002261 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002262 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2263 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2264 else
2265 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2266
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002267 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2268 /* We only want the cr8 intercept bits of the guest */
2269 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2270 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2271 }
2272
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002273 /* We don't want to see VMMCALLs from a nested guest */
2274 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2275
Joerg Roedele0231712010-02-24 18:59:10 +01002276 /*
2277 * We don't want a nested guest to be more powerful than the guest, so
2278 * all intercepts are ORed
2279 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002280 svm->vmcb->control.intercept_cr_read |=
2281 nested_vmcb->control.intercept_cr_read;
2282 svm->vmcb->control.intercept_cr_write |=
2283 nested_vmcb->control.intercept_cr_write;
2284 svm->vmcb->control.intercept_dr_read |=
2285 nested_vmcb->control.intercept_dr_read;
2286 svm->vmcb->control.intercept_dr_write |=
2287 nested_vmcb->control.intercept_dr_write;
2288 svm->vmcb->control.intercept_exceptions |=
2289 nested_vmcb->control.intercept_exceptions;
2290
2291 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2292
2293 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002294 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2295 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2296 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002297 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2298 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2299
Joerg Roedel7597f122010-02-19 16:23:00 +01002300 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002301
Joerg Roedel20307532010-11-29 17:51:48 +01002302 /* Enter Guest-Mode */
2303 enter_guest_mode(&svm->vcpu);
2304
Joerg Roedel06fc77722010-02-19 16:23:07 +01002305 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002306
Joerg Roedel2af91942009-08-07 11:49:28 +02002307 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002308
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002309 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002310}
2311
Joerg Roedel9966bf62009-08-07 11:49:40 +02002312static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002313{
2314 to_vmcb->save.fs = from_vmcb->save.fs;
2315 to_vmcb->save.gs = from_vmcb->save.gs;
2316 to_vmcb->save.tr = from_vmcb->save.tr;
2317 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2318 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2319 to_vmcb->save.star = from_vmcb->save.star;
2320 to_vmcb->save.lstar = from_vmcb->save.lstar;
2321 to_vmcb->save.cstar = from_vmcb->save.cstar;
2322 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2323 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2324 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2325 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002326}
2327
Avi Kivity851ba692009-08-24 11:10:17 +03002328static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002329{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002330 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002331 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002332
Alexander Graf55426752008-11-25 20:17:06 +01002333 if (nested_svm_check_permissions(svm))
2334 return 1;
2335
2336 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2337 skip_emulated_instruction(&svm->vcpu);
2338
Joerg Roedel7597f122010-02-19 16:23:00 +01002339 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002340 if (!nested_vmcb)
2341 return 1;
2342
2343 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002344 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002345
2346 return 1;
2347}
2348
Avi Kivity851ba692009-08-24 11:10:17 +03002349static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002350{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002351 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002352 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002353
Alexander Graf55426752008-11-25 20:17:06 +01002354 if (nested_svm_check_permissions(svm))
2355 return 1;
2356
2357 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2358 skip_emulated_instruction(&svm->vcpu);
2359
Joerg Roedel7597f122010-02-19 16:23:00 +01002360 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002361 if (!nested_vmcb)
2362 return 1;
2363
2364 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002365 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002366
2367 return 1;
2368}
2369
Avi Kivity851ba692009-08-24 11:10:17 +03002370static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002371{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002372 if (nested_svm_check_permissions(svm))
2373 return 1;
2374
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002375 /* Save rip after vmrun instruction */
2376 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002377
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002378 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002379 return 1;
2380
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002381 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002382 goto failed;
2383
2384 return 1;
2385
2386failed:
2387
2388 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2389 svm->vmcb->control.exit_code_hi = 0;
2390 svm->vmcb->control.exit_info_1 = 0;
2391 svm->vmcb->control.exit_info_2 = 0;
2392
2393 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002394
2395 return 1;
2396}
2397
Avi Kivity851ba692009-08-24 11:10:17 +03002398static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002399{
2400 if (nested_svm_check_permissions(svm))
2401 return 1;
2402
2403 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2404 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002405 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002406
Joerg Roedel2af91942009-08-07 11:49:28 +02002407 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002408
2409 return 1;
2410}
2411
Avi Kivity851ba692009-08-24 11:10:17 +03002412static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002413{
2414 if (nested_svm_check_permissions(svm))
2415 return 1;
2416
2417 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2418 skip_emulated_instruction(&svm->vcpu);
2419
Joerg Roedel2af91942009-08-07 11:49:28 +02002420 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002421
2422 /* After a CLGI no interrupts should come */
2423 svm_clear_vintr(svm);
2424 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2425
2426 return 1;
2427}
2428
Avi Kivity851ba692009-08-24 11:10:17 +03002429static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002430{
2431 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002432
Joerg Roedelec1ff792009-10-09 16:08:31 +02002433 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2434 vcpu->arch.regs[VCPU_REGS_RAX]);
2435
Alexander Grafff092382009-06-15 15:21:24 +02002436 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2437 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2438
2439 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2440 skip_emulated_instruction(&svm->vcpu);
2441 return 1;
2442}
2443
Joerg Roedel532a46b2009-10-09 16:08:32 +02002444static int skinit_interception(struct vcpu_svm *svm)
2445{
2446 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2447
2448 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2449 return 1;
2450}
2451
Avi Kivity851ba692009-08-24 11:10:17 +03002452static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002453{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002454 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 return 1;
2456}
2457
Avi Kivity851ba692009-08-24 11:10:17 +03002458static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002459{
Izik Eidus37817f22008-03-24 23:14:53 +02002460 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002461 int reason;
2462 int int_type = svm->vmcb->control.exit_int_info &
2463 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002464 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002465 uint32_t type =
2466 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2467 uint32_t idt_v =
2468 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002469 bool has_error_code = false;
2470 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002471
2472 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002473
Izik Eidus37817f22008-03-24 23:14:53 +02002474 if (svm->vmcb->control.exit_info_2 &
2475 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002476 reason = TASK_SWITCH_IRET;
2477 else if (svm->vmcb->control.exit_info_2 &
2478 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2479 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002480 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002481 reason = TASK_SWITCH_GATE;
2482 else
2483 reason = TASK_SWITCH_CALL;
2484
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002485 if (reason == TASK_SWITCH_GATE) {
2486 switch (type) {
2487 case SVM_EXITINTINFO_TYPE_NMI:
2488 svm->vcpu.arch.nmi_injected = false;
2489 break;
2490 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002491 if (svm->vmcb->control.exit_info_2 &
2492 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2493 has_error_code = true;
2494 error_code =
2495 (u32)svm->vmcb->control.exit_info_2;
2496 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002497 kvm_clear_exception_queue(&svm->vcpu);
2498 break;
2499 case SVM_EXITINTINFO_TYPE_INTR:
2500 kvm_clear_interrupt_queue(&svm->vcpu);
2501 break;
2502 default:
2503 break;
2504 }
2505 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002506
Gleb Natapov8317c292009-04-12 13:37:02 +03002507 if (reason != TASK_SWITCH_GATE ||
2508 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2509 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002510 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2511 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002512
Gleb Natapovacb54512010-04-15 21:03:50 +03002513 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2514 has_error_code, error_code) == EMULATE_FAIL) {
2515 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2516 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2517 svm->vcpu.run->internal.ndata = 0;
2518 return 0;
2519 }
2520 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002521}
2522
Avi Kivity851ba692009-08-24 11:10:17 +03002523static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002524{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002525 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002526 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002527 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002528}
2529
Avi Kivity851ba692009-08-24 11:10:17 +03002530static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002531{
2532 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002533 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002534 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002535 return 1;
2536}
2537
Avi Kivity851ba692009-08-24 11:10:17 +03002538static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002539{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002540 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002541}
2542
Avi Kivity851ba692009-08-24 11:10:17 +03002543static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002544{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002545 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002546}
2547
Joerg Roedelcda00082010-09-02 17:29:46 +02002548static int cr0_write_interception(struct vcpu_svm *svm)
2549{
2550 struct kvm_vcpu *vcpu = &svm->vcpu;
2551 int r;
2552
2553 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2554
2555 if (svm->nested.vmexit_rip) {
2556 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2557 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2558 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2559 svm->nested.vmexit_rip = 0;
2560 }
2561
2562 return r == EMULATE_DONE;
2563}
2564
Avi Kivity851ba692009-08-24 11:10:17 +03002565static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002566{
Avi Kivity851ba692009-08-24 11:10:17 +03002567 struct kvm_run *kvm_run = svm->vcpu.run;
2568
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002569 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2570 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002571 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002572 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2573 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002574 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002575 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002576 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2577 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002578 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2579 return 0;
2580}
2581
Avi Kivity6aa8b732006-12-10 02:21:36 -08002582static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2583{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002584 struct vcpu_svm *svm = to_svm(vcpu);
2585
Avi Kivity6aa8b732006-12-10 02:21:36 -08002586 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302587 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002588 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002589
Joerg Roedel20307532010-11-29 17:51:48 +01002590 if (is_guest_mode(vcpu))
Joerg Roedel20824f32009-09-16 15:24:18 +02002591 tsc_offset = svm->nested.hsave->control.tsc_offset;
2592 else
2593 tsc_offset = svm->vmcb->control.tsc_offset;
2594
2595 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002596 break;
2597 }
Brian Gerst8c065852010-07-17 09:03:26 -04002598 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002599 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002600 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002601#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002602 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002603 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002604 break;
2605 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002606 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 break;
2608 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002609 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610 break;
2611 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002612 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002613 break;
2614#endif
2615 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002616 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617 break;
2618 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002619 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002620 break;
2621 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002622 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002624 /*
2625 * Nobody will change the following 5 values in the VMCB so we can
2626 * safely return them on rdmsr. They will always be 0 until LBRV is
2627 * implemented.
2628 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002629 case MSR_IA32_DEBUGCTLMSR:
2630 *data = svm->vmcb->save.dbgctl;
2631 break;
2632 case MSR_IA32_LASTBRANCHFROMIP:
2633 *data = svm->vmcb->save.br_from;
2634 break;
2635 case MSR_IA32_LASTBRANCHTOIP:
2636 *data = svm->vmcb->save.br_to;
2637 break;
2638 case MSR_IA32_LASTINTFROMIP:
2639 *data = svm->vmcb->save.last_excp_from;
2640 break;
2641 case MSR_IA32_LASTINTTOIP:
2642 *data = svm->vmcb->save.last_excp_to;
2643 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002644 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002645 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002646 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002647 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002648 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002649 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002650 case MSR_IA32_UCODE_REV:
2651 *data = 0x01000065;
2652 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002653 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002654 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002655 }
2656 return 0;
2657}
2658
Avi Kivity851ba692009-08-24 11:10:17 +03002659static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002660{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002661 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002662 u64 data;
2663
Avi Kivity59200272010-01-25 19:47:02 +02002664 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2665 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002666 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002667 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002668 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002669
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002670 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002671 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002672 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002673 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 }
2675 return 1;
2676}
2677
Joerg Roedel4a810182010-02-24 18:59:15 +01002678static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2679{
2680 struct vcpu_svm *svm = to_svm(vcpu);
2681 int svm_dis, chg_mask;
2682
2683 if (data & ~SVM_VM_CR_VALID_MASK)
2684 return 1;
2685
2686 chg_mask = SVM_VM_CR_VALID_MASK;
2687
2688 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2689 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2690
2691 svm->nested.vm_cr_msr &= ~chg_mask;
2692 svm->nested.vm_cr_msr |= (data & chg_mask);
2693
2694 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2695
2696 /* check for svm_disable while efer.svme is set */
2697 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2698 return 1;
2699
2700 return 0;
2701}
2702
Avi Kivity6aa8b732006-12-10 02:21:36 -08002703static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2704{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002705 struct vcpu_svm *svm = to_svm(vcpu);
2706
Avi Kivity6aa8b732006-12-10 02:21:36 -08002707 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002708 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002709 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002710 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002711 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002712 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002713 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002714#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002715 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002716 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002717 break;
2718 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002719 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002720 break;
2721 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002722 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723 break;
2724 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002725 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002726 break;
2727#endif
2728 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002729 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002730 break;
2731 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002732 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002733 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 break;
2735 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002736 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002737 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002739 case MSR_IA32_DEBUGCTLMSR:
Avi Kivity2a6b20b2010-11-09 16:15:42 +02002740 if (!boot_cpu_has(X86_FEATURE_LBRV)) {
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002741 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002742 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002743 break;
2744 }
2745 if (data & DEBUGCTL_RESERVED_BITS)
2746 return 1;
2747
2748 svm->vmcb->save.dbgctl = data;
2749 if (data & (1ULL<<0))
2750 svm_enable_lbrv(svm);
2751 else
2752 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002753 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002754 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002755 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002756 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002757 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002758 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002759 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002760 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2761 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002762 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002763 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002764 }
2765 return 0;
2766}
2767
Avi Kivity851ba692009-08-24 11:10:17 +03002768static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002769{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002770 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002771 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002772 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002773
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002774
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002775 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002776 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2777 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002778 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002779 } else {
2780 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002781 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002782 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 return 1;
2784}
2785
Avi Kivity851ba692009-08-24 11:10:17 +03002786static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002787{
Rusty Russelle756fc62007-07-30 20:07:08 +10002788 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002789 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790 else
Avi Kivity851ba692009-08-24 11:10:17 +03002791 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002792}
2793
Avi Kivity851ba692009-08-24 11:10:17 +03002794static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002795{
Avi Kivity851ba692009-08-24 11:10:17 +03002796 struct kvm_run *kvm_run = svm->vcpu.run;
2797
Avi Kivity3842d132010-07-27 12:30:24 +03002798 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002799 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002800 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002801 /*
2802 * If the user space waits to inject interrupts, exit as soon as
2803 * possible
2804 */
Gleb Natapov80618232009-04-21 17:44:56 +03002805 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2806 kvm_run->request_interrupt_window &&
2807 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002808 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002809 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2810 return 0;
2811 }
2812
2813 return 1;
2814}
2815
Mark Langsdorf565d0992009-10-06 14:25:02 -05002816static int pause_interception(struct vcpu_svm *svm)
2817{
2818 kvm_vcpu_on_spin(&(svm->vcpu));
2819 return 1;
2820}
2821
Avi Kivity851ba692009-08-24 11:10:17 +03002822static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002823 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2824 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2825 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2826 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002827 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002828 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002829 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2830 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2831 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2832 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002833 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2834 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2835 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002836 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2837 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2838 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2839 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002840 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2841 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2842 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2843 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002844 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002845 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002846 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002847 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002848 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2849 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002850 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002851 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2852 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2853 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2854 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002855 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002856 [SVM_EXIT_SMI] = nop_on_interception,
2857 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002858 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002859 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002860 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002861 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002862 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002863 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002864 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002865 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002866 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002867 [SVM_EXIT_MSR] = msr_interception,
2868 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002869 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002870 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002871 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002872 [SVM_EXIT_VMLOAD] = vmload_interception,
2873 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002874 [SVM_EXIT_STGI] = stgi_interception,
2875 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002876 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002877 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002878 [SVM_EXIT_MONITOR] = invalid_op_interception,
2879 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002880 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002881};
2882
Joerg Roedel3f10c842010-05-05 16:04:42 +02002883void dump_vmcb(struct kvm_vcpu *vcpu)
2884{
2885 struct vcpu_svm *svm = to_svm(vcpu);
2886 struct vmcb_control_area *control = &svm->vmcb->control;
2887 struct vmcb_save_area *save = &svm->vmcb->save;
2888
2889 pr_err("VMCB Control Area:\n");
2890 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2891 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2892 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2893 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2894 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2895 pr_err("intercepts: %016llx\n", control->intercept);
2896 pr_err("pause filter count: %d\n", control->pause_filter_count);
2897 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2898 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2899 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2900 pr_err("asid: %d\n", control->asid);
2901 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2902 pr_err("int_ctl: %08x\n", control->int_ctl);
2903 pr_err("int_vector: %08x\n", control->int_vector);
2904 pr_err("int_state: %08x\n", control->int_state);
2905 pr_err("exit_code: %08x\n", control->exit_code);
2906 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2907 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2908 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2909 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2910 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2911 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2912 pr_err("event_inj: %08x\n", control->event_inj);
2913 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2914 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2915 pr_err("next_rip: %016llx\n", control->next_rip);
2916 pr_err("VMCB State Save Area:\n");
2917 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2918 save->es.selector, save->es.attrib,
2919 save->es.limit, save->es.base);
2920 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2921 save->cs.selector, save->cs.attrib,
2922 save->cs.limit, save->cs.base);
2923 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2924 save->ss.selector, save->ss.attrib,
2925 save->ss.limit, save->ss.base);
2926 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2927 save->ds.selector, save->ds.attrib,
2928 save->ds.limit, save->ds.base);
2929 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2930 save->fs.selector, save->fs.attrib,
2931 save->fs.limit, save->fs.base);
2932 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2933 save->gs.selector, save->gs.attrib,
2934 save->gs.limit, save->gs.base);
2935 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2936 save->gdtr.selector, save->gdtr.attrib,
2937 save->gdtr.limit, save->gdtr.base);
2938 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2939 save->ldtr.selector, save->ldtr.attrib,
2940 save->ldtr.limit, save->ldtr.base);
2941 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2942 save->idtr.selector, save->idtr.attrib,
2943 save->idtr.limit, save->idtr.base);
2944 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2945 save->tr.selector, save->tr.attrib,
2946 save->tr.limit, save->tr.base);
2947 pr_err("cpl: %d efer: %016llx\n",
2948 save->cpl, save->efer);
2949 pr_err("cr0: %016llx cr2: %016llx\n",
2950 save->cr0, save->cr2);
2951 pr_err("cr3: %016llx cr4: %016llx\n",
2952 save->cr3, save->cr4);
2953 pr_err("dr6: %016llx dr7: %016llx\n",
2954 save->dr6, save->dr7);
2955 pr_err("rip: %016llx rflags: %016llx\n",
2956 save->rip, save->rflags);
2957 pr_err("rsp: %016llx rax: %016llx\n",
2958 save->rsp, save->rax);
2959 pr_err("star: %016llx lstar: %016llx\n",
2960 save->star, save->lstar);
2961 pr_err("cstar: %016llx sfmask: %016llx\n",
2962 save->cstar, save->sfmask);
2963 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2964 save->kernel_gs_base, save->sysenter_cs);
2965 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2966 save->sysenter_esp, save->sysenter_eip);
2967 pr_err("gpat: %016llx dbgctl: %016llx\n",
2968 save->g_pat, save->dbgctl);
2969 pr_err("br_from: %016llx br_to: %016llx\n",
2970 save->br_from, save->br_to);
2971 pr_err("excp_from: %016llx excp_to: %016llx\n",
2972 save->last_excp_from, save->last_excp_to);
2973
2974}
2975
Avi Kivity586f9602010-11-18 13:09:54 +02002976static void svm_get_exit_info(struct kvm_vcpu *vcpu, u64 *info1, u64 *info2)
2977{
2978 struct vmcb_control_area *control = &to_svm(vcpu)->vmcb->control;
2979
2980 *info1 = control->exit_info_1;
2981 *info2 = control->exit_info_2;
2982}
2983
Avi Kivity851ba692009-08-24 11:10:17 +03002984static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002985{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002986 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002987 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002988 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002989
Avi Kivityaa179112010-11-17 18:44:19 +02002990 trace_kvm_exit(exit_code, vcpu, KVM_ISA_SVM);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002991
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002992 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2993 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2994 if (npt_enabled)
2995 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002996
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002997 if (unlikely(svm->nested.exit_required)) {
2998 nested_svm_vmexit(svm);
2999 svm->nested.exit_required = false;
3000
3001 return 1;
3002 }
3003
Joerg Roedel20307532010-11-29 17:51:48 +01003004 if (is_guest_mode(vcpu)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003005 int vmexit;
3006
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003007 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3008 svm->vmcb->control.exit_info_1,
3009 svm->vmcb->control.exit_info_2,
3010 svm->vmcb->control.exit_int_info,
3011 svm->vmcb->control.exit_int_info_err);
3012
Joerg Roedel410e4d52009-08-07 11:49:44 +02003013 vmexit = nested_svm_exit_special(svm);
3014
3015 if (vmexit == NESTED_EXIT_CONTINUE)
3016 vmexit = nested_svm_exit_handled(svm);
3017
3018 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003019 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003020 }
3021
Joerg Roedela5c38322009-08-07 11:49:32 +02003022 svm_complete_interrupts(svm);
3023
Avi Kivity04d2cc72007-09-10 18:10:54 +03003024 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3025 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3026 kvm_run->fail_entry.hardware_entry_failure_reason
3027 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003028 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3029 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003030 return 0;
3031 }
3032
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003033 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003034 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003035 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3036 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003037 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3038 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003039 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003040 exit_code);
3041
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003042 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003043 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003044 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003045 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003046 return 0;
3047 }
3048
Avi Kivity851ba692009-08-24 11:10:17 +03003049 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050}
3051
3052static void reload_tss(struct kvm_vcpu *vcpu)
3053{
3054 int cpu = raw_smp_processor_id();
3055
Tejun Heo0fe1e002009-10-29 22:34:14 +09003056 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3057 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003058 load_TR_desc();
3059}
3060
Rusty Russelle756fc62007-07-30 20:07:08 +10003061static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003062{
3063 int cpu = raw_smp_processor_id();
3064
Tejun Heo0fe1e002009-10-29 22:34:14 +09003065 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003066
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003067 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003068 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003069 if (svm->asid_generation != sd->asid_generation)
3070 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071}
3072
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003073static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3074{
3075 struct vcpu_svm *svm = to_svm(vcpu);
3076
3077 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3078 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003079 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003080 ++vcpu->stat.nmi_injections;
3081}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003082
Eddie Dong85f455f2007-07-06 12:20:49 +03003083static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084{
3085 struct vmcb_control_area *control;
3086
Rusty Russelle756fc62007-07-30 20:07:08 +10003087 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003088 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003089 control->int_ctl &= ~V_INTR_PRIO_MASK;
3090 control->int_ctl |= V_IRQ_MASK |
3091 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3092}
3093
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003094static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003095{
3096 struct vcpu_svm *svm = to_svm(vcpu);
3097
Joerg Roedel2af91942009-08-07 11:49:28 +02003098 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003099
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003100 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3101 ++vcpu->stat.irq_injections;
3102
Alexander Graf219b65d2009-06-15 15:21:25 +02003103 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3104 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003105}
3106
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003107static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3108{
3109 struct vcpu_svm *svm = to_svm(vcpu);
3110
Joerg Roedel20307532010-11-29 17:51:48 +01003111 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003112 return;
3113
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003114 if (irr == -1)
3115 return;
3116
3117 if (tpr >= irr)
3118 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
3119}
3120
3121static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003122{
3123 struct vcpu_svm *svm = to_svm(vcpu);
3124 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003125 int ret;
3126 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3127 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3128 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3129
3130 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003131}
3132
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003133static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3134{
3135 struct vcpu_svm *svm = to_svm(vcpu);
3136
3137 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3138}
3139
3140static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3141{
3142 struct vcpu_svm *svm = to_svm(vcpu);
3143
3144 if (masked) {
3145 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003146 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003147 } else {
3148 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003149 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003150 }
3151}
3152
Gleb Natapov78646122009-03-23 12:12:11 +02003153static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3154{
3155 struct vcpu_svm *svm = to_svm(vcpu);
3156 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003157 int ret;
3158
3159 if (!gif_set(svm) ||
3160 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3161 return 0;
3162
3163 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3164
Joerg Roedel20307532010-11-29 17:51:48 +01003165 if (is_guest_mode(vcpu))
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003166 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3167
3168 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003169}
3170
Gleb Natapov9222be12009-04-23 17:14:37 +03003171static void enable_irq_window(struct kvm_vcpu *vcpu)
3172{
Alexander Graf219b65d2009-06-15 15:21:25 +02003173 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003174
Joerg Roedele0231712010-02-24 18:59:10 +01003175 /*
3176 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3177 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3178 * get that intercept, this function will be called again though and
3179 * we'll get the vintr intercept.
3180 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003181 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003182 svm_set_vintr(svm);
3183 svm_inject_irq(svm, 0x0);
3184 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003185}
3186
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003187static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003188{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003189 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003190
Gleb Natapov44c11432009-05-11 13:35:52 +03003191 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3192 == HF_NMI_MASK)
3193 return; /* IRET will cause a vm exit */
3194
Joerg Roedele0231712010-02-24 18:59:10 +01003195 /*
3196 * Something prevents NMI from been injected. Single step over possible
3197 * problem (IRET or exception injection or interrupt shadow)
3198 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003199 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003200 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3201 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003202}
3203
Izik Eiduscbc94022007-10-25 00:29:55 +02003204static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3205{
3206 return 0;
3207}
3208
Avi Kivityd9e368d2007-06-07 19:18:30 +03003209static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3210{
3211 force_new_asid(vcpu);
3212}
3213
Avi Kivity04d2cc72007-09-10 18:10:54 +03003214static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3215{
3216}
3217
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003218static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3219{
3220 struct vcpu_svm *svm = to_svm(vcpu);
3221
Joerg Roedel20307532010-11-29 17:51:48 +01003222 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003223 return;
3224
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003225 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3226 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003227 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003228 }
3229}
3230
Joerg Roedel649d6862008-04-16 16:51:15 +02003231static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3232{
3233 struct vcpu_svm *svm = to_svm(vcpu);
3234 u64 cr8;
3235
Joerg Roedel20307532010-11-29 17:51:48 +01003236 if (is_guest_mode(vcpu) && (vcpu->arch.hflags & HF_VINTR_MASK))
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003237 return;
3238
Joerg Roedel649d6862008-04-16 16:51:15 +02003239 cr8 = kvm_get_cr8(vcpu);
3240 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3241 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3242}
3243
Gleb Natapov9222be12009-04-23 17:14:37 +03003244static void svm_complete_interrupts(struct vcpu_svm *svm)
3245{
3246 u8 vector;
3247 int type;
3248 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003249 unsigned int3_injected = svm->int3_injected;
3250
3251 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003252
Avi Kivity3842d132010-07-27 12:30:24 +03003253 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003254 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003255 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3256 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003257
Gleb Natapov9222be12009-04-23 17:14:37 +03003258 svm->vcpu.arch.nmi_injected = false;
3259 kvm_clear_exception_queue(&svm->vcpu);
3260 kvm_clear_interrupt_queue(&svm->vcpu);
3261
3262 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3263 return;
3264
Avi Kivity3842d132010-07-27 12:30:24 +03003265 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3266
Gleb Natapov9222be12009-04-23 17:14:37 +03003267 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3268 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3269
3270 switch (type) {
3271 case SVM_EXITINTINFO_TYPE_NMI:
3272 svm->vcpu.arch.nmi_injected = true;
3273 break;
3274 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003275 /*
3276 * In case of software exceptions, do not reinject the vector,
3277 * but re-execute the instruction instead. Rewind RIP first
3278 * if we emulated INT3 before.
3279 */
3280 if (kvm_exception_is_soft(vector)) {
3281 if (vector == BP_VECTOR && int3_injected &&
3282 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3283 kvm_rip_write(&svm->vcpu,
3284 kvm_rip_read(&svm->vcpu) -
3285 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003286 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003287 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003288 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3289 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003290 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003291
3292 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003293 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003294 break;
3295 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003296 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003297 break;
3298 default:
3299 break;
3300 }
3301}
3302
Avi Kivityb463a6f2010-07-20 15:06:17 +03003303static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3304{
3305 struct vcpu_svm *svm = to_svm(vcpu);
3306 struct vmcb_control_area *control = &svm->vmcb->control;
3307
3308 control->exit_int_info = control->event_inj;
3309 control->exit_int_info_err = control->event_inj_err;
3310 control->event_inj = 0;
3311 svm_complete_interrupts(svm);
3312}
3313
Avi Kivity80e31d42008-07-14 14:44:59 +03003314#ifdef CONFIG_X86_64
3315#define R "r"
3316#else
3317#define R "e"
3318#endif
3319
Avi Kivity851ba692009-08-24 11:10:17 +03003320static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003321{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003322 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003323
Joerg Roedel2041a062010-04-22 12:33:08 +02003324 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3325 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3326 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3327
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003328 /*
3329 * A vmexit emulation is required before the vcpu can be executed
3330 * again.
3331 */
3332 if (unlikely(svm->nested.exit_required))
3333 return;
3334
Rusty Russelle756fc62007-07-30 20:07:08 +10003335 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003336
Joerg Roedel649d6862008-04-16 16:51:15 +02003337 sync_lapic_to_cr8(vcpu);
3338
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003339 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003340
Avi Kivity04d2cc72007-09-10 18:10:54 +03003341 clgi();
3342
3343 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003344
Avi Kivity6aa8b732006-12-10 02:21:36 -08003345 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003346 "push %%"R"bp; \n\t"
3347 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3348 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3349 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3350 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3351 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3352 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003353#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003354 "mov %c[r8](%[svm]), %%r8 \n\t"
3355 "mov %c[r9](%[svm]), %%r9 \n\t"
3356 "mov %c[r10](%[svm]), %%r10 \n\t"
3357 "mov %c[r11](%[svm]), %%r11 \n\t"
3358 "mov %c[r12](%[svm]), %%r12 \n\t"
3359 "mov %c[r13](%[svm]), %%r13 \n\t"
3360 "mov %c[r14](%[svm]), %%r14 \n\t"
3361 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003362#endif
3363
Avi Kivity6aa8b732006-12-10 02:21:36 -08003364 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003365 "push %%"R"ax \n\t"
3366 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003367 __ex(SVM_VMLOAD) "\n\t"
3368 __ex(SVM_VMRUN) "\n\t"
3369 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003370 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003371
3372 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003373 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3374 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3375 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3376 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3377 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3378 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003379#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003380 "mov %%r8, %c[r8](%[svm]) \n\t"
3381 "mov %%r9, %c[r9](%[svm]) \n\t"
3382 "mov %%r10, %c[r10](%[svm]) \n\t"
3383 "mov %%r11, %c[r11](%[svm]) \n\t"
3384 "mov %%r12, %c[r12](%[svm]) \n\t"
3385 "mov %%r13, %c[r13](%[svm]) \n\t"
3386 "mov %%r14, %c[r14](%[svm]) \n\t"
3387 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003388#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003389 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003390 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003391 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003392 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003393 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3394 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3395 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3396 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3397 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3398 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003399#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003400 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3401 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3402 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3403 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3404 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3405 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3406 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3407 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003408#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003409 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003410 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003411#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003412 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3413#endif
3414 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003415
Avi Kivity82ca2d12010-10-21 12:20:34 +02003416#ifdef CONFIG_X86_64
3417 wrmsrl(MSR_GS_BASE, svm->host.gs_base);
3418#else
Avi Kivitydacccfd2010-10-21 12:20:33 +02003419 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003420#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421
3422 reload_tss(vcpu);
3423
Avi Kivity56ba47d2007-11-07 17:14:18 +02003424 local_irq_disable();
3425
3426 stgi();
3427
Avi Kivity13c34e02010-10-21 12:20:31 +02003428 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3429 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3430 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3431 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3432
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003433 sync_cr8_to_lapic(vcpu);
3434
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003435 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003436
Gleb Natapov631bc482010-10-14 11:22:52 +02003437 /* if exit due to PF check for async PF */
3438 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3439 svm->apf_reason = kvm_read_and_reset_pf_reason();
3440
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003441 if (npt_enabled) {
3442 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3443 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3444 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003445
3446 /*
3447 * We need to handle MC intercepts here before the vcpu has a chance to
3448 * change the physical cpu
3449 */
3450 if (unlikely(svm->vmcb->control.exit_code ==
3451 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3452 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003453}
3454
Avi Kivity80e31d42008-07-14 14:44:59 +03003455#undef R
3456
Avi Kivity6aa8b732006-12-10 02:21:36 -08003457static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3458{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003459 struct vcpu_svm *svm = to_svm(vcpu);
3460
3461 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003462 force_new_asid(vcpu);
3463}
3464
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003465static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3466{
3467 struct vcpu_svm *svm = to_svm(vcpu);
3468
3469 svm->vmcb->control.nested_cr3 = root;
3470
3471 /* Also sync guest cr3 here in case we live migrate */
3472 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3473
3474 force_new_asid(vcpu);
3475}
3476
Avi Kivity6aa8b732006-12-10 02:21:36 -08003477static int is_disabled(void)
3478{
Joerg Roedel6031a612007-06-22 12:29:50 +03003479 u64 vm_cr;
3480
3481 rdmsrl(MSR_VM_CR, vm_cr);
3482 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3483 return 1;
3484
Avi Kivity6aa8b732006-12-10 02:21:36 -08003485 return 0;
3486}
3487
Ingo Molnar102d8322007-02-19 14:37:47 +02003488static void
3489svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3490{
3491 /*
3492 * Patch in the VMMCALL instruction:
3493 */
3494 hypercall[0] = 0x0f;
3495 hypercall[1] = 0x01;
3496 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003497}
3498
Yang, Sheng002c7f72007-07-31 14:23:01 +03003499static void svm_check_processor_compat(void *rtn)
3500{
3501 *(int *)rtn = 0;
3502}
3503
Avi Kivity774ead32007-12-26 13:57:04 +02003504static bool svm_cpu_has_accelerated_tpr(void)
3505{
3506 return false;
3507}
3508
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003509static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003510{
3511 return 0;
3512}
3513
Sheng Yang0e851882009-12-18 16:48:46 +08003514static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3515{
3516}
3517
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003518static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3519{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003520 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003521 case 0x00000001:
3522 /* Mask out xsave bit as long as it is not supported by SVM */
3523 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3524 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003525 case 0x80000001:
3526 if (nested)
3527 entry->ecx |= (1 << 2); /* Set SVM bit */
3528 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003529 case 0x8000000A:
3530 entry->eax = 1; /* SVM revision 1 */
3531 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3532 ASID emulation to nested SVM */
3533 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003534 entry->edx = 0; /* Per default do not support any
3535 additional features */
3536
3537 /* Support next_rip if host supports it */
Avi Kivity2a6b20b2010-11-09 16:15:42 +02003538 if (boot_cpu_has(X86_FEATURE_NRIPS))
Joerg Roedel7a190662010-07-27 18:14:21 +02003539 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003540
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003541 /* Support NPT for the guest if enabled */
3542 if (npt_enabled)
3543 entry->edx |= SVM_FEATURE_NPT;
3544
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003545 break;
3546 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003547}
3548
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003549static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003550 { SVM_EXIT_READ_CR0, "read_cr0" },
3551 { SVM_EXIT_READ_CR3, "read_cr3" },
3552 { SVM_EXIT_READ_CR4, "read_cr4" },
3553 { SVM_EXIT_READ_CR8, "read_cr8" },
3554 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3555 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3556 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3557 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3558 { SVM_EXIT_READ_DR0, "read_dr0" },
3559 { SVM_EXIT_READ_DR1, "read_dr1" },
3560 { SVM_EXIT_READ_DR2, "read_dr2" },
3561 { SVM_EXIT_READ_DR3, "read_dr3" },
3562 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3563 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3564 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3565 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3566 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3567 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003568 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3569 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3570 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3571 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3572 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3573 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3574 { SVM_EXIT_INTR, "interrupt" },
3575 { SVM_EXIT_NMI, "nmi" },
3576 { SVM_EXIT_SMI, "smi" },
3577 { SVM_EXIT_INIT, "init" },
3578 { SVM_EXIT_VINTR, "vintr" },
3579 { SVM_EXIT_CPUID, "cpuid" },
3580 { SVM_EXIT_INVD, "invd" },
3581 { SVM_EXIT_HLT, "hlt" },
3582 { SVM_EXIT_INVLPG, "invlpg" },
3583 { SVM_EXIT_INVLPGA, "invlpga" },
3584 { SVM_EXIT_IOIO, "io" },
3585 { SVM_EXIT_MSR, "msr" },
3586 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3587 { SVM_EXIT_SHUTDOWN, "shutdown" },
3588 { SVM_EXIT_VMRUN, "vmrun" },
3589 { SVM_EXIT_VMMCALL, "hypercall" },
3590 { SVM_EXIT_VMLOAD, "vmload" },
3591 { SVM_EXIT_VMSAVE, "vmsave" },
3592 { SVM_EXIT_STGI, "stgi" },
3593 { SVM_EXIT_CLGI, "clgi" },
3594 { SVM_EXIT_SKINIT, "skinit" },
3595 { SVM_EXIT_WBINVD, "wbinvd" },
3596 { SVM_EXIT_MONITOR, "monitor" },
3597 { SVM_EXIT_MWAIT, "mwait" },
3598 { SVM_EXIT_NPF, "npf" },
3599 { -1, NULL }
3600};
3601
Sheng Yang17cc3932010-01-05 19:02:27 +08003602static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003603{
Sheng Yang17cc3932010-01-05 19:02:27 +08003604 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003605}
3606
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003607static bool svm_rdtscp_supported(void)
3608{
3609 return false;
3610}
3611
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003612static bool svm_has_wbinvd_exit(void)
3613{
3614 return true;
3615}
3616
Avi Kivity02daab22009-12-30 12:40:26 +02003617static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3618{
3619 struct vcpu_svm *svm = to_svm(vcpu);
3620
Avi Kivity02daab22009-12-30 12:40:26 +02003621 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel20307532010-11-29 17:51:48 +01003622 if (is_guest_mode(vcpu))
Joerg Roedel66a562f2010-02-19 16:23:08 +01003623 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3624 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003625}
3626
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003627static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003628 .cpu_has_kvm_support = has_svm,
3629 .disabled_by_bios = is_disabled,
3630 .hardware_setup = svm_hardware_setup,
3631 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003632 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 .hardware_enable = svm_hardware_enable,
3634 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003635 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003636
3637 .vcpu_create = svm_create_vcpu,
3638 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003639 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640
Avi Kivity04d2cc72007-09-10 18:10:54 +03003641 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003642 .vcpu_load = svm_vcpu_load,
3643 .vcpu_put = svm_vcpu_put,
3644
3645 .set_guest_debug = svm_guest_debug,
3646 .get_msr = svm_get_msr,
3647 .set_msr = svm_set_msr,
3648 .get_segment_base = svm_get_segment_base,
3649 .get_segment = svm_get_segment,
3650 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003651 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003652 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003653 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003654 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003655 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003656 .set_cr3 = svm_set_cr3,
3657 .set_cr4 = svm_set_cr4,
3658 .set_efer = svm_set_efer,
3659 .get_idt = svm_get_idt,
3660 .set_idt = svm_set_idt,
3661 .get_gdt = svm_get_gdt,
3662 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003663 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003664 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003665 .get_rflags = svm_get_rflags,
3666 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003667 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003668 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003669
Avi Kivity6aa8b732006-12-10 02:21:36 -08003670 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003671
Avi Kivity6aa8b732006-12-10 02:21:36 -08003672 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003673 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003674 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003675 .set_interrupt_shadow = svm_set_interrupt_shadow,
3676 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003677 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003678 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003679 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003680 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003681 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003682 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003683 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003684 .get_nmi_mask = svm_get_nmi_mask,
3685 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003686 .enable_nmi_window = enable_nmi_window,
3687 .enable_irq_window = enable_irq_window,
3688 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003689
3690 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003691 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003692 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003693
Avi Kivity586f9602010-11-18 13:09:54 +02003694 .get_exit_info = svm_get_exit_info,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003695 .exit_reasons_str = svm_exit_reasons_str,
Avi Kivity586f9602010-11-18 13:09:54 +02003696
Sheng Yang17cc3932010-01-05 19:02:27 +08003697 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003698
3699 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003700
3701 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003702
3703 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003704
3705 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003706
3707 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003708 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003709
3710 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003711};
3712
3713static int __init svm_init(void)
3714{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003715 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003716 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003717}
3718
3719static void __exit svm_exit(void)
3720{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003721 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003722}
3723
3724module_init(svm_init)
3725module_exit(svm_exit)