blob: 8ea4a79bc4ef10cff494c0f3af43e99156a3cb27 [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
Avi Kivitydacccfd2010-10-21 12:20:33 +0200190static void save_host_msrs(struct kvm_vcpu *vcpu);
191static void load_host_msrs(struct kvm_vcpu *vcpu);
192
Avi Kivity6aa8b732006-12-10 02:21:36 -0800193static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
194{
195 return container_of(vcpu, struct vcpu_svm, vcpu);
196}
197
198static inline bool is_nested(struct vcpu_svm *svm)
199{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200200 return svm->nested.vmcb;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800201}
202
Joerg Roedel2af91942009-08-07 11:49:28 +0200203static inline void enable_gif(struct vcpu_svm *svm)
204{
205 svm->vcpu.arch.hflags |= HF_GIF_MASK;
206}
207
208static inline void disable_gif(struct vcpu_svm *svm)
209{
210 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
211}
212
213static inline bool gif_set(struct vcpu_svm *svm)
214{
215 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
216}
217
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218static unsigned long iopm_base;
219
220struct kvm_ldttss_desc {
221 u16 limit0;
222 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100223 unsigned base1:8, type:5, dpl:2, p:1;
224 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800225 u32 base3;
226 u32 zero1;
227} __attribute__((packed));
228
229struct svm_cpu_data {
230 int cpu;
231
Avi Kivity5008fdf2007-04-02 13:05:50 +0300232 u64 asid_generation;
233 u32 max_asid;
234 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800235 struct kvm_ldttss_desc *tss_desc;
236
237 struct page *save_area;
238};
239
240static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300241static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800242
243struct svm_init_data {
244 int cpu;
245 int r;
246};
247
248static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
249
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200250#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800251#define MSRS_RANGE_SIZE 2048
252#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
253
Joerg Roedel455716f2010-03-01 15:34:35 +0100254static u32 svm_msrpm_offset(u32 msr)
255{
256 u32 offset;
257 int i;
258
259 for (i = 0; i < NUM_MSR_MAPS; i++) {
260 if (msr < msrpm_ranges[i] ||
261 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
262 continue;
263
264 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
265 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
266
267 /* Now we have the u8 offset - but need the u32 offset */
268 return offset / 4;
269 }
270
271 /* MSR not in any range */
272 return MSR_INVALID;
273}
274
Avi Kivity6aa8b732006-12-10 02:21:36 -0800275#define MAX_INST_SIZE 15
276
Joerg Roedel80b77062007-03-30 17:02:14 +0300277static inline u32 svm_has(u32 feat)
278{
279 return svm_features & feat;
280}
281
Avi Kivity6aa8b732006-12-10 02:21:36 -0800282static inline void clgi(void)
283{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300284 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800285}
286
287static inline void stgi(void)
288{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300289 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290}
291
292static inline void invlpga(unsigned long addr, u32 asid)
293{
Joerg Roedele0231712010-02-24 18:59:10 +0100294 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295}
296
Avi Kivity6aa8b732006-12-10 02:21:36 -0800297static inline void force_new_asid(struct kvm_vcpu *vcpu)
298{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400299 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800300}
301
302static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
303{
304 force_new_asid(vcpu);
305}
306
Joerg Roedel4b161842010-09-10 17:31:03 +0200307static int get_npt_level(void)
308{
309#ifdef CONFIG_X86_64
310 return PT64_ROOT_LEVEL;
311#else
312 return PT32E_ROOT_LEVEL;
313#endif
314}
315
Avi Kivity6aa8b732006-12-10 02:21:36 -0800316static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
317{
Zachary Amsden6dc696d2010-05-26 15:09:43 -1000318 vcpu->arch.efer = efer;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100319 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600320 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800321
Alexander Graf9962d032008-11-25 20:17:02 +0100322 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800323}
324
Avi Kivity6aa8b732006-12-10 02:21:36 -0800325static int is_external_interrupt(u32 info)
326{
327 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
328 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
329}
330
Glauber Costa2809f5d2009-05-12 16:21:05 -0400331static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
332{
333 struct vcpu_svm *svm = to_svm(vcpu);
334 u32 ret = 0;
335
336 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100337 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400338 return ret & mask;
339}
340
341static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
342{
343 struct vcpu_svm *svm = to_svm(vcpu);
344
345 if (mask == 0)
346 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
347 else
348 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
349
350}
351
Avi Kivity6aa8b732006-12-10 02:21:36 -0800352static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
353{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400354 struct vcpu_svm *svm = to_svm(vcpu);
355
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200356 if (svm->vmcb->control.next_rip != 0)
357 svm->next_rip = svm->vmcb->control.next_rip;
358
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400359 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300360 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300361 EMULATE_DONE)
362 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800363 return;
364 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300365 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
366 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
367 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800368
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300369 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400370 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800371}
372
Jan Kiszka116a4752010-02-23 17:47:54 +0100373static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200374 bool has_error_code, u32 error_code,
375 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100376{
377 struct vcpu_svm *svm = to_svm(vcpu);
378
Joerg Roedele0231712010-02-24 18:59:10 +0100379 /*
380 * If we are within a nested VM we'd better #VMEXIT and let the guest
381 * handle the exception
382 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200383 if (!reinject &&
384 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100385 return;
386
Jan Kiszka66b71382010-02-23 17:47:56 +0100387 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
388 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
389
390 /*
391 * For guest debugging where we have to reinject #BP if some
392 * INT3 is guest-owned:
393 * Emulate nRIP by moving RIP forward. Will fail if injection
394 * raises a fault that is not intercepted. Still better than
395 * failing in all cases.
396 */
397 skip_emulated_instruction(&svm->vcpu);
398 rip = kvm_rip_read(&svm->vcpu);
399 svm->int3_rip = rip + svm->vmcb->save.cs.base;
400 svm->int3_injected = rip - old_rip;
401 }
402
Jan Kiszka116a4752010-02-23 17:47:54 +0100403 svm->vmcb->control.event_inj = nr
404 | SVM_EVTINJ_VALID
405 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
406 | SVM_EVTINJ_TYPE_EXEPT;
407 svm->vmcb->control.event_inj_err = error_code;
408}
409
Joerg Roedel67ec6602010-05-17 14:43:35 +0200410static void svm_init_erratum_383(void)
411{
412 u32 low, high;
413 int err;
414 u64 val;
415
Hans Rosenfeld1be85a62010-07-28 19:09:32 +0200416 if (!cpu_has_amd_erratum(amd_erratum_383))
Joerg Roedel67ec6602010-05-17 14:43:35 +0200417 return;
418
419 /* Use _safe variants to not break nested virtualization */
420 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
421 if (err)
422 return;
423
424 val |= (1ULL << 47);
425
426 low = lower_32_bits(val);
427 high = upper_32_bits(val);
428
429 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
430
431 erratum_383_found = true;
432}
433
Avi Kivity6aa8b732006-12-10 02:21:36 -0800434static int has_svm(void)
435{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200436 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800437
Eduardo Habkost63d11422008-11-17 19:03:20 -0200438 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800439 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800440 return 0;
441 }
442
Avi Kivity6aa8b732006-12-10 02:21:36 -0800443 return 1;
444}
445
446static void svm_hardware_disable(void *garbage)
447{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200448 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800449}
450
Alexander Graf10474ae2009-09-15 11:37:46 +0200451static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800452{
453
Tejun Heo0fe1e002009-10-29 22:34:14 +0900454 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200456 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457 struct desc_struct *gdt;
458 int me = raw_smp_processor_id();
459
Alexander Graf10474ae2009-09-15 11:37:46 +0200460 rdmsrl(MSR_EFER, efer);
461 if (efer & EFER_SVME)
462 return -EBUSY;
463
Avi Kivity6aa8b732006-12-10 02:21:36 -0800464 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000465 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
466 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200467 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800468 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900469 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800470
Tejun Heo0fe1e002009-10-29 22:34:14 +0900471 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000472 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800473 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200474 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800475 }
476
Tejun Heo0fe1e002009-10-29 22:34:14 +0900477 sd->asid_generation = 1;
478 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
479 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200481 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200482 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900483 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484
Alexander Graf9962d032008-11-25 20:17:02 +0100485 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800486
Linus Torvaldsd0316552009-12-14 09:58:24 -0800487 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200488
Joerg Roedel67ec6602010-05-17 14:43:35 +0200489 svm_init_erratum_383();
490
Alexander Graf10474ae2009-09-15 11:37:46 +0200491 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492}
493
Joerg Roedel0da1db752008-07-02 16:02:11 +0200494static void svm_cpu_uninit(int cpu)
495{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900496 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200497
Tejun Heo0fe1e002009-10-29 22:34:14 +0900498 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200499 return;
500
501 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900502 __free_page(sd->save_area);
503 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200504}
505
Avi Kivity6aa8b732006-12-10 02:21:36 -0800506static int svm_cpu_init(int cpu)
507{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900508 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800509 int r;
510
Tejun Heo0fe1e002009-10-29 22:34:14 +0900511 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
512 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800513 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900514 sd->cpu = cpu;
515 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800516 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900517 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518 goto err_1;
519
Tejun Heo0fe1e002009-10-29 22:34:14 +0900520 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800521
522 return 0;
523
524err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900525 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800526 return r;
527
528}
529
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100530static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800531{
532 int i;
533
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100534 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
535 if (direct_access_msrs[i].index == index)
536 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800537
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100538 return false;
539}
540
Avi Kivity6aa8b732006-12-10 02:21:36 -0800541static void set_msr_interception(u32 *msrpm, unsigned msr,
542 int read, int write)
543{
Joerg Roedel455716f2010-03-01 15:34:35 +0100544 u8 bit_read, bit_write;
545 unsigned long tmp;
546 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800547
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100548 /*
549 * If this warning triggers extend the direct_access_msrs list at the
550 * beginning of the file
551 */
552 WARN_ON(!valid_msr_intercept(msr));
553
Joerg Roedel455716f2010-03-01 15:34:35 +0100554 offset = svm_msrpm_offset(msr);
555 bit_read = 2 * (msr & 0x0f);
556 bit_write = 2 * (msr & 0x0f) + 1;
557 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800558
Joerg Roedel455716f2010-03-01 15:34:35 +0100559 BUG_ON(offset == MSR_INVALID);
560
561 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
562 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
563
564 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800565}
566
Joerg Roedelf65c2292008-02-13 18:58:46 +0100567static void svm_vcpu_init_msrpm(u32 *msrpm)
568{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100569 int i;
570
Joerg Roedelf65c2292008-02-13 18:58:46 +0100571 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
572
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100573 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
574 if (!direct_access_msrs[i].always)
575 continue;
576
577 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
578 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100579}
580
Joerg Roedel323c3d82010-03-01 15:34:37 +0100581static void add_msr_offset(u32 offset)
582{
583 int i;
584
585 for (i = 0; i < MSRPM_OFFSETS; ++i) {
586
587 /* Offset already in list? */
588 if (msrpm_offsets[i] == offset)
589 return;
590
591 /* Slot used by another offset? */
592 if (msrpm_offsets[i] != MSR_INVALID)
593 continue;
594
595 /* Add offset to list */
596 msrpm_offsets[i] = offset;
597
598 return;
599 }
600
601 /*
602 * If this BUG triggers the msrpm_offsets table has an overflow. Just
603 * increase MSRPM_OFFSETS in this case.
604 */
605 BUG();
606}
607
608static void init_msrpm_offsets(void)
609{
610 int i;
611
612 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
613
614 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
615 u32 offset;
616
617 offset = svm_msrpm_offset(direct_access_msrs[i].index);
618 BUG_ON(offset == MSR_INVALID);
619
620 add_msr_offset(offset);
621 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622}
623
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100624static void svm_enable_lbrv(struct vcpu_svm *svm)
625{
626 u32 *msrpm = svm->msrpm;
627
628 svm->vmcb->control.lbr_ctl = 1;
629 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
630 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
631 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
632 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
633}
634
635static void svm_disable_lbrv(struct vcpu_svm *svm)
636{
637 u32 *msrpm = svm->msrpm;
638
639 svm->vmcb->control.lbr_ctl = 0;
640 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
641 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
642 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
643 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
644}
645
Avi Kivity6aa8b732006-12-10 02:21:36 -0800646static __init int svm_hardware_setup(void)
647{
648 int cpu;
649 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100650 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800651 int r;
652
Avi Kivity6aa8b732006-12-10 02:21:36 -0800653 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
654
655 if (!iopm_pages)
656 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300657
658 iopm_va = page_address(iopm_pages);
659 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800660 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
661
Joerg Roedel323c3d82010-03-01 15:34:37 +0100662 init_msrpm_offsets();
663
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100664 if (boot_cpu_has(X86_FEATURE_NX))
665 kvm_enable_efer_bits(EFER_NX);
666
Alexander Graf1b2fd702009-02-02 16:23:51 +0100667 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
668 kvm_enable_efer_bits(EFER_FFXSR);
669
Alexander Graf236de052008-11-25 20:17:10 +0100670 if (nested) {
671 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200672 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100673 }
674
Zachary Amsden3230bb42009-09-29 11:38:37 -1000675 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800676 r = svm_cpu_init(cpu);
677 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100678 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100680
681 svm_features = cpuid_edx(SVM_CPUID_FUNC);
682
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100683 if (!svm_has(SVM_FEATURE_NPT))
684 npt_enabled = false;
685
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100686 if (npt_enabled && !npt) {
687 printk(KERN_INFO "kvm: Nested Paging disabled\n");
688 npt_enabled = false;
689 }
690
Joerg Roedel18552672008-02-07 13:47:41 +0100691 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100692 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100693 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200694 } else
695 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100696
Avi Kivity6aa8b732006-12-10 02:21:36 -0800697 return 0;
698
Joerg Roedelf65c2292008-02-13 18:58:46 +0100699err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800700 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
701 iopm_base = 0;
702 return r;
703}
704
705static __exit void svm_hardware_unsetup(void)
706{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200707 int cpu;
708
Zachary Amsden3230bb42009-09-29 11:38:37 -1000709 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200710 svm_cpu_uninit(cpu);
711
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100713 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714}
715
716static void init_seg(struct vmcb_seg *seg)
717{
718 seg->selector = 0;
719 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100720 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 seg->limit = 0xffff;
722 seg->base = 0;
723}
724
725static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
726{
727 seg->selector = 0;
728 seg->attrib = SVM_SELECTOR_P_MASK | type;
729 seg->limit = 0xffff;
730 seg->base = 0;
731}
732
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -1000733static void svm_write_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
734{
735 struct vcpu_svm *svm = to_svm(vcpu);
736 u64 g_tsc_offset = 0;
737
738 if (is_nested(svm)) {
739 g_tsc_offset = svm->vmcb->control.tsc_offset -
740 svm->nested.hsave->control.tsc_offset;
741 svm->nested.hsave->control.tsc_offset = offset;
742 }
743
744 svm->vmcb->control.tsc_offset = offset + g_tsc_offset;
745}
746
Zachary Amsdene48672f2010-08-19 22:07:23 -1000747static void svm_adjust_tsc_offset(struct kvm_vcpu *vcpu, s64 adjustment)
748{
749 struct vcpu_svm *svm = to_svm(vcpu);
750
751 svm->vmcb->control.tsc_offset += adjustment;
752 if (is_nested(svm))
753 svm->nested.hsave->control.tsc_offset += adjustment;
754}
755
Joerg Roedele6101a92008-02-13 18:58:45 +0100756static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800757{
Joerg Roedele6101a92008-02-13 18:58:45 +0100758 struct vmcb_control_area *control = &svm->vmcb->control;
759 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800760
Avi Kivitybff78272010-01-07 13:16:08 +0200761 svm->vcpu.fpu_active = 1;
762
Joerg Roedele0231712010-02-24 18:59:10 +0100763 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800764 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200765 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766
Joerg Roedele0231712010-02-24 18:59:10 +0100767 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800768 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200769 INTERCEPT_CR4_MASK |
770 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800771
Joerg Roedele0231712010-02-24 18:59:10 +0100772 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800773 INTERCEPT_DR1_MASK |
774 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100775 INTERCEPT_DR3_MASK |
776 INTERCEPT_DR4_MASK |
777 INTERCEPT_DR5_MASK |
778 INTERCEPT_DR6_MASK |
779 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800780
Joerg Roedele0231712010-02-24 18:59:10 +0100781 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800782 INTERCEPT_DR1_MASK |
783 INTERCEPT_DR2_MASK |
784 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100785 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800786 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100787 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800788 INTERCEPT_DR7_MASK;
789
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500790 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200791 (1 << UD_VECTOR) |
792 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800793
794
Joerg Roedele0231712010-02-24 18:59:10 +0100795 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800796 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200797 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200798 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200800 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800801 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300802 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803 (1ULL << INTERCEPT_INVLPGA) |
804 (1ULL << INTERCEPT_IOIO_PROT) |
805 (1ULL << INTERCEPT_MSR_PROT) |
806 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800807 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800808 (1ULL << INTERCEPT_VMRUN) |
809 (1ULL << INTERCEPT_VMMCALL) |
810 (1ULL << INTERCEPT_VMLOAD) |
811 (1ULL << INTERCEPT_VMSAVE) |
812 (1ULL << INTERCEPT_STGI) |
813 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100814 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200815 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100816 (1ULL << INTERCEPT_MONITOR) |
817 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800818
819 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100820 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800821 control->int_ctl = V_INTR_MASKING_MASK;
822
823 init_seg(&save->es);
824 init_seg(&save->ss);
825 init_seg(&save->ds);
826 init_seg(&save->fs);
827 init_seg(&save->gs);
828
829 save->cs.selector = 0xf000;
830 /* Executable/Readable Code Segment */
831 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
832 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
833 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800834 /*
835 * cs.base should really be 0xffff0000, but vmx can't handle that, so
836 * be consistent with it.
837 *
838 * Replace when we have real mode working for vmx.
839 */
840 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800841
842 save->gdtr.limit = 0xffff;
843 save->idtr.limit = 0xffff;
844
845 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
846 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
847
Marcelo Tosattieaa48512010-08-31 19:13:14 -0300848 svm_set_efer(&svm->vcpu, 0);
Mike Dayd77c26f2007-10-08 09:02:08 -0400849 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800850 save->dr7 = 0x400;
851 save->rflags = 2;
852 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300853 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800854
Joerg Roedele0231712010-02-24 18:59:10 +0100855 /*
856 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200857 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800858 */
Marcelo Tosatti678041a2010-08-31 19:13:13 -0300859 svm->vcpu.arch.cr0 = 0;
860 (void)kvm_set_cr0(&svm->vcpu, X86_CR0_NW | X86_CR0_CD | X86_CR0_ET);
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200861
Rusty Russell66aee912007-07-17 23:34:16 +1000862 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800863 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100864
865 if (npt_enabled) {
866 /* Setup VMCB for Nested Paging */
867 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300868 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
869 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100870 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200871 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
872 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100873 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100874 save->cr3 = 0;
875 save->cr4 = 0;
876 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300877 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100878
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200879 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200880 svm->vcpu.arch.hflags = 0;
881
Mark Langsdorf565d0992009-10-06 14:25:02 -0500882 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
883 control->pause_filter_count = 3000;
884 control->intercept |= (1ULL << INTERCEPT_PAUSE);
885 }
886
Joerg Roedel2af91942009-08-07 11:49:28 +0200887 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800888}
889
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200890static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300891{
892 struct vcpu_svm *svm = to_svm(vcpu);
893
Joerg Roedele6101a92008-02-13 18:58:45 +0100894 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200895
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300896 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300897 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800898 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
899 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200900 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300901 vcpu->arch.regs_avail = ~0;
902 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200903
904 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300905}
906
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000907static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800908{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400909 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100911 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100912 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100913 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000914 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800915
Rusty Russellc16f8622007-07-30 21:12:19 +1000916 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000917 if (!svm) {
918 err = -ENOMEM;
919 goto out;
920 }
921
922 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
923 if (err)
924 goto free_svm;
925
Joerg Roedelf65c2292008-02-13 18:58:46 +0100926 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900927 page = alloc_page(GFP_KERNEL);
928 if (!page)
929 goto uninit;
930
Joerg Roedelf65c2292008-02-13 18:58:46 +0100931 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
932 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900933 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100934
935 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
936 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900937 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100938
Alexander Grafb286d5d2008-11-25 20:17:05 +0100939 hsave_page = alloc_page(GFP_KERNEL);
940 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900941 goto free_page3;
942
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200943 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100944
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900945 svm->msrpm = page_address(msrpm_pages);
946 svm_vcpu_init_msrpm(svm->msrpm);
947
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200948 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100949 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100950
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400951 svm->vmcb = page_address(page);
952 clear_page(svm->vmcb);
953 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
954 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100955 init_vmcb(svm);
Zachary Amsden99e3e302010-08-19 22:07:17 -1000956 kvm_write_tsc(&svm->vcpu, 0);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400957
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200958 err = fx_init(&svm->vcpu);
959 if (err)
960 goto free_page4;
961
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800962 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300963 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800964 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800965
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000966 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800967
Jan Kiszka10ab25c2010-05-25 16:01:50 +0200968free_page4:
969 __free_page(hsave_page);
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900970free_page3:
971 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
972free_page2:
973 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
974free_page1:
975 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000976uninit:
977 kvm_vcpu_uninit(&svm->vcpu);
978free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000979 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000980out:
981 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800982}
983
984static void svm_free_vcpu(struct kvm_vcpu *vcpu)
985{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400986 struct vcpu_svm *svm = to_svm(vcpu);
987
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000988 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100989 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200990 __free_page(virt_to_page(svm->nested.hsave));
991 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000992 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000993 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800994}
995
Avi Kivity15ad7142007-07-11 18:17:21 +0300996static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800997{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400998 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300999 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +02001000
Avi Kivity0cc50642007-03-25 12:07:27 +02001001 if (unlikely(cpu != vcpu->cpu)) {
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03001002 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +02001003 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001004
Avi Kivitydacccfd2010-10-21 12:20:33 +02001005 save_host_msrs(vcpu);
1006 savesegment(fs, svm->host.fs);
1007 savesegment(gs, svm->host.gs);
1008 svm->host.ldt = kvm_read_ldt();
1009
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001010 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001011 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001012}
1013
1014static void svm_vcpu_put(struct kvm_vcpu *vcpu)
1015{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001016 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001017 int i;
1018
Avi Kivitye1beb1d2007-11-18 13:50:24 +02001019 ++vcpu->stat.host_state_reload;
Avi Kivitydacccfd2010-10-21 12:20:33 +02001020 kvm_load_ldt(svm->host.ldt);
1021#ifdef CONFIG_X86_64
1022 loadsegment(fs, svm->host.fs);
1023 load_gs_index(svm->host.gs);
1024 wrmsrl(MSR_KERNEL_GS_BASE, current->thread.gs);
1025#else
1026 loadsegment(gs, svm->host.gs);
1027#endif
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001028 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001029 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001030}
1031
Avi Kivity6aa8b732006-12-10 02:21:36 -08001032static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
1033{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001034 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001035}
1036
1037static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
1038{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001039 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001040}
1041
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001042static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
1043{
1044 switch (reg) {
1045 case VCPU_EXREG_PDPTR:
1046 BUG_ON(!npt_enabled);
Joerg Roedelff03a072010-09-10 17:30:57 +02001047 load_pdptrs(vcpu, vcpu->arch.walk_mmu, vcpu->arch.cr3);
Avi Kivity6de4f3a2009-05-31 22:58:47 +03001048 break;
1049 default:
1050 BUG();
1051 }
1052}
1053
Alexander Graff0b85052008-11-25 20:17:01 +01001054static void svm_set_vintr(struct vcpu_svm *svm)
1055{
1056 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1057}
1058
1059static void svm_clear_vintr(struct vcpu_svm *svm)
1060{
1061 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1062}
1063
Avi Kivity6aa8b732006-12-10 02:21:36 -08001064static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1065{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001066 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001067
1068 switch (seg) {
1069 case VCPU_SREG_CS: return &save->cs;
1070 case VCPU_SREG_DS: return &save->ds;
1071 case VCPU_SREG_ES: return &save->es;
1072 case VCPU_SREG_FS: return &save->fs;
1073 case VCPU_SREG_GS: return &save->gs;
1074 case VCPU_SREG_SS: return &save->ss;
1075 case VCPU_SREG_TR: return &save->tr;
1076 case VCPU_SREG_LDTR: return &save->ldtr;
1077 }
1078 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001079 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001080}
1081
1082static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1083{
1084 struct vmcb_seg *s = svm_seg(vcpu, seg);
1085
1086 return s->base;
1087}
1088
1089static void svm_get_segment(struct kvm_vcpu *vcpu,
1090 struct kvm_segment *var, int seg)
1091{
1092 struct vmcb_seg *s = svm_seg(vcpu, seg);
1093
1094 var->base = s->base;
1095 var->limit = s->limit;
1096 var->selector = s->selector;
1097 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1098 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1099 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1100 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1101 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1102 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1103 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1104 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001105
Joerg Roedele0231712010-02-24 18:59:10 +01001106 /*
1107 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001108 * for cross vendor migration purposes by "not present"
1109 */
1110 var->unusable = !var->present || (var->type == 0);
1111
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001112 switch (seg) {
1113 case VCPU_SREG_CS:
1114 /*
1115 * SVM always stores 0 for the 'G' bit in the CS selector in
1116 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1117 * Intel's VMENTRY has a check on the 'G' bit.
1118 */
Amit Shah25022ac2008-10-27 09:04:17 +00001119 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001120 break;
1121 case VCPU_SREG_TR:
1122 /*
1123 * Work around a bug where the busy flag in the tr selector
1124 * isn't exposed
1125 */
Amit Shahc0d09822008-10-27 09:04:18 +00001126 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001127 break;
1128 case VCPU_SREG_DS:
1129 case VCPU_SREG_ES:
1130 case VCPU_SREG_FS:
1131 case VCPU_SREG_GS:
1132 /*
1133 * The accessed bit must always be set in the segment
1134 * descriptor cache, although it can be cleared in the
1135 * descriptor, the cached bit always remains at 1. Since
1136 * Intel has a check on this, set it here to support
1137 * cross-vendor migration.
1138 */
1139 if (!var->unusable)
1140 var->type |= 0x1;
1141 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001142 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001143 /*
1144 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001145 * descriptor is left as 1, although the whole segment has
1146 * been made unusable. Clear it here to pass an Intel VMX
1147 * entry check when cross vendor migrating.
1148 */
1149 if (var->unusable)
1150 var->db = 0;
1151 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001152 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001153}
1154
Izik Eidus2e4d2652008-03-24 19:38:34 +02001155static int svm_get_cpl(struct kvm_vcpu *vcpu)
1156{
1157 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1158
1159 return save->cpl;
1160}
1161
Gleb Natapov89a27f42010-02-16 10:51:48 +02001162static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001163{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001164 struct vcpu_svm *svm = to_svm(vcpu);
1165
Gleb Natapov89a27f42010-02-16 10:51:48 +02001166 dt->size = svm->vmcb->save.idtr.limit;
1167 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001168}
1169
Gleb Natapov89a27f42010-02-16 10:51:48 +02001170static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001171{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001172 struct vcpu_svm *svm = to_svm(vcpu);
1173
Gleb Natapov89a27f42010-02-16 10:51:48 +02001174 svm->vmcb->save.idtr.limit = dt->size;
1175 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001176}
1177
Gleb Natapov89a27f42010-02-16 10:51:48 +02001178static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001179{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001180 struct vcpu_svm *svm = to_svm(vcpu);
1181
Gleb Natapov89a27f42010-02-16 10:51:48 +02001182 dt->size = svm->vmcb->save.gdtr.limit;
1183 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184}
1185
Gleb Natapov89a27f42010-02-16 10:51:48 +02001186static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001187{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001188 struct vcpu_svm *svm = to_svm(vcpu);
1189
Gleb Natapov89a27f42010-02-16 10:51:48 +02001190 svm->vmcb->save.gdtr.limit = dt->size;
1191 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001192}
1193
Avi Kivitye8467fd2009-12-29 18:43:06 +02001194static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1195{
1196}
1197
Anthony Liguori25c4c272007-04-27 09:29:21 +03001198static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001199{
1200}
1201
Avi Kivityd2251572010-01-06 10:55:27 +02001202static void update_cr0_intercept(struct vcpu_svm *svm)
1203{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001204 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001205 ulong gcr0 = svm->vcpu.arch.cr0;
1206 u64 *hcr0 = &svm->vmcb->save.cr0;
1207
1208 if (!svm->vcpu.fpu_active)
1209 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1210 else
1211 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1212 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1213
1214
1215 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001216 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1217 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1218 if (is_nested(svm)) {
1219 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 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1224 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1225 }
Avi Kivityd2251572010-01-06 10:55:27 +02001226 } else {
1227 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1228 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001229 if (is_nested(svm)) {
1230 struct vmcb *hsave = svm->nested.hsave;
1231
1232 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1233 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1234 }
Avi Kivityd2251572010-01-06 10:55:27 +02001235 }
1236}
1237
Avi Kivity6aa8b732006-12-10 02:21:36 -08001238static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1239{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001240 struct vcpu_svm *svm = to_svm(vcpu);
1241
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001242 if (is_nested(svm)) {
1243 /*
1244 * We are here because we run in nested mode, the host kvm
1245 * intercepts cr0 writes but the l1 hypervisor does not.
1246 * But the L1 hypervisor may intercept selective cr0 writes.
1247 * This needs to be checked here.
1248 */
1249 unsigned long old, new;
1250
1251 /* Remove bits that would trigger a real cr0 write intercept */
1252 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1253 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1254
1255 if (old == new) {
1256 /* cr0 write with ts and mp unchanged */
1257 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
Joerg Roedelcda00082010-09-02 17:29:46 +02001258 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE) {
1259 svm->nested.vmexit_rip = kvm_rip_read(vcpu);
1260 svm->nested.vmexit_rsp = kvm_register_read(vcpu, VCPU_REGS_RSP);
1261 svm->nested.vmexit_rax = kvm_register_read(vcpu, VCPU_REGS_RAX);
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001262 return;
Joerg Roedelcda00082010-09-02 17:29:46 +02001263 }
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001264 }
1265 }
1266
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001267#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001268 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001269 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001270 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001271 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001272 }
1273
Mike Dayd77c26f2007-10-08 09:02:08 -04001274 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001275 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001276 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001277 }
1278 }
1279#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001280 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001281
1282 if (!npt_enabled)
1283 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001284
1285 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001286 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001287 /*
1288 * re-enable caching here because the QEMU bios
1289 * does not do it - this results in some delay at
1290 * reboot
1291 */
1292 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001293 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001294 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001295}
1296
1297static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1298{
Joerg Roedel6394b642008-04-09 14:15:29 +02001299 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001300 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1301
1302 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1303 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001304
Joerg Roedelec077262008-04-09 14:15:28 +02001305 vcpu->arch.cr4 = cr4;
1306 if (!npt_enabled)
1307 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001308 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001309 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001310}
1311
1312static void svm_set_segment(struct kvm_vcpu *vcpu,
1313 struct kvm_segment *var, int seg)
1314{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001315 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 struct vmcb_seg *s = svm_seg(vcpu, seg);
1317
1318 s->base = var->base;
1319 s->limit = var->limit;
1320 s->selector = var->selector;
1321 if (var->unusable)
1322 s->attrib = 0;
1323 else {
1324 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1325 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1326 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1327 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1328 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1329 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1330 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1331 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1332 }
1333 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001334 svm->vmcb->save.cpl
1335 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1337
1338}
1339
Gleb Natapov44c11432009-05-11 13:35:52 +03001340static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001341{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001342 struct vcpu_svm *svm = to_svm(vcpu);
1343
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001344 svm->vmcb->control.intercept_exceptions &=
1345 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001346
Jan Kiszka6be7d302009-10-18 13:24:54 +02001347 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001348 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1349
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001350 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1351 if (vcpu->guest_debug &
1352 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1353 svm->vmcb->control.intercept_exceptions |=
1354 1 << DB_VECTOR;
1355 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1356 svm->vmcb->control.intercept_exceptions |=
1357 1 << BP_VECTOR;
1358 } else
1359 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001360}
1361
Jan Kiszka355be0b2009-10-03 00:31:21 +02001362static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001363{
Gleb Natapov44c11432009-05-11 13:35:52 +03001364 struct vcpu_svm *svm = to_svm(vcpu);
1365
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001366 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1367 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1368 else
1369 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1370
Jan Kiszka355be0b2009-10-03 00:31:21 +02001371 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001372}
1373
1374static void load_host_msrs(struct kvm_vcpu *vcpu)
1375{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001376#ifdef CONFIG_X86_64
Avi Kivityafe9e662010-10-21 12:20:32 +02001377 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001378#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001379}
1380
1381static void save_host_msrs(struct kvm_vcpu *vcpu)
1382{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001383#ifdef CONFIG_X86_64
Avi Kivityafe9e662010-10-21 12:20:32 +02001384 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host.gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001385#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001386}
1387
Tejun Heo0fe1e002009-10-29 22:34:14 +09001388static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001389{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001390 if (sd->next_asid > sd->max_asid) {
1391 ++sd->asid_generation;
1392 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001393 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001394 }
1395
Tejun Heo0fe1e002009-10-29 22:34:14 +09001396 svm->asid_generation = sd->asid_generation;
1397 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001398}
1399
Gleb Natapov020df072010-04-13 10:05:23 +03001400static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001401{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001402 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001403
Gleb Natapov020df072010-04-13 10:05:23 +03001404 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001405}
1406
Avi Kivity851ba692009-08-24 11:10:17 +03001407static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001408{
Gleb Natapov631bc482010-10-14 11:22:52 +02001409 u64 fault_address = svm->vmcb->control.exit_info_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001410 u32 error_code;
Gleb Natapov631bc482010-10-14 11:22:52 +02001411 int r = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001412
Gleb Natapov631bc482010-10-14 11:22:52 +02001413 switch (svm->apf_reason) {
1414 default:
1415 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001416
Gleb Natapov631bc482010-10-14 11:22:52 +02001417 trace_kvm_page_fault(fault_address, error_code);
1418 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1419 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
1420 r = kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
1421 break;
1422 case KVM_PV_REASON_PAGE_NOT_PRESENT:
1423 svm->apf_reason = 0;
1424 local_irq_disable();
1425 kvm_async_pf_task_wait(fault_address);
1426 local_irq_enable();
1427 break;
1428 case KVM_PV_REASON_PAGE_READY:
1429 svm->apf_reason = 0;
1430 local_irq_disable();
1431 kvm_async_pf_task_wake(fault_address);
1432 local_irq_enable();
1433 break;
1434 }
1435 return r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001436}
1437
Avi Kivity851ba692009-08-24 11:10:17 +03001438static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001439{
Avi Kivity851ba692009-08-24 11:10:17 +03001440 struct kvm_run *kvm_run = svm->vcpu.run;
1441
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001442 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001443 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001444 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001445 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1446 return 1;
1447 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001448
Jan Kiszka6be7d302009-10-18 13:24:54 +02001449 if (svm->nmi_singlestep) {
1450 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001451 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1452 svm->vmcb->save.rflags &=
1453 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1454 update_db_intercept(&svm->vcpu);
1455 }
1456
1457 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001458 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001459 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1460 kvm_run->debug.arch.pc =
1461 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1462 kvm_run->debug.arch.exception = DB_VECTOR;
1463 return 0;
1464 }
1465
1466 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001467}
1468
Avi Kivity851ba692009-08-24 11:10:17 +03001469static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001470{
Avi Kivity851ba692009-08-24 11:10:17 +03001471 struct kvm_run *kvm_run = svm->vcpu.run;
1472
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001473 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1474 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1475 kvm_run->debug.arch.exception = BP_VECTOR;
1476 return 0;
1477}
1478
Avi Kivity851ba692009-08-24 11:10:17 +03001479static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001480{
1481 int er;
1482
Avi Kivity851ba692009-08-24 11:10:17 +03001483 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001484 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001485 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001486 return 1;
1487}
1488
Avi Kivity6b52d182010-01-21 15:31:47 +02001489static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001490{
Avi Kivity6b52d182010-01-21 15:31:47 +02001491 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001492 u32 excp;
1493
1494 if (is_nested(svm)) {
1495 u32 h_excp, n_excp;
1496
1497 h_excp = svm->nested.hsave->control.intercept_exceptions;
1498 n_excp = svm->nested.intercept_exceptions;
1499 h_excp &= ~(1 << NM_VECTOR);
1500 excp = h_excp | n_excp;
1501 } else {
1502 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001503 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001504 }
1505
1506 svm->vmcb->control.intercept_exceptions = excp;
1507
Rusty Russelle756fc62007-07-30 20:07:08 +10001508 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001509 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001510}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001511
Avi Kivity6b52d182010-01-21 15:31:47 +02001512static int nm_interception(struct vcpu_svm *svm)
1513{
1514 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001515 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001516}
1517
Joerg Roedel67ec6602010-05-17 14:43:35 +02001518static bool is_erratum_383(void)
1519{
1520 int err, i;
1521 u64 value;
1522
1523 if (!erratum_383_found)
1524 return false;
1525
1526 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1527 if (err)
1528 return false;
1529
1530 /* Bit 62 may or may not be set for this mce */
1531 value &= ~(1ULL << 62);
1532
1533 if (value != 0xb600000000010015ULL)
1534 return false;
1535
1536 /* Clear MCi_STATUS registers */
1537 for (i = 0; i < 6; ++i)
1538 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1539
1540 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1541 if (!err) {
1542 u32 low, high;
1543
1544 value &= ~(1ULL << 2);
1545 low = lower_32_bits(value);
1546 high = upper_32_bits(value);
1547
1548 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1549 }
1550
1551 /* Flush tlb to evict multi-match entries */
1552 __flush_tlb_all();
1553
1554 return true;
1555}
1556
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001557static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001558{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001559 if (is_erratum_383()) {
1560 /*
1561 * Erratum 383 triggered. Guest state is corrupt so kill the
1562 * guest.
1563 */
1564 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1565
Avi Kivitya8eeb042010-05-10 12:34:53 +03001566 kvm_make_request(KVM_REQ_TRIPLE_FAULT, &svm->vcpu);
Joerg Roedel67ec6602010-05-17 14:43:35 +02001567
1568 return;
1569 }
1570
Joerg Roedel53371b52008-04-09 14:15:30 +02001571 /*
1572 * On an #MC intercept the MCE handler is not called automatically in
1573 * the host. So do it by hand here.
1574 */
1575 asm volatile (
1576 "int $0x12\n");
1577 /* not sure if we ever come back to this point */
1578
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001579 return;
1580}
1581
1582static int mc_interception(struct vcpu_svm *svm)
1583{
Joerg Roedel53371b52008-04-09 14:15:30 +02001584 return 1;
1585}
1586
Avi Kivity851ba692009-08-24 11:10:17 +03001587static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001588{
Avi Kivity851ba692009-08-24 11:10:17 +03001589 struct kvm_run *kvm_run = svm->vcpu.run;
1590
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001591 /*
1592 * VMCB is undefined after a SHUTDOWN intercept
1593 * so reinitialize it.
1594 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001595 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001596 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001597
1598 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1599 return 0;
1600}
1601
Avi Kivity851ba692009-08-24 11:10:17 +03001602static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001603{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001604 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001605 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001606 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001607 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001608
Rusty Russelle756fc62007-07-30 20:07:08 +10001609 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001610 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001611 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001612 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001613 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001614
Avi Kivity039576c2007-03-20 12:46:50 +02001615 port = io_info >> 16;
1616 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001617 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001618 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001619
1620 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001621}
1622
Avi Kivity851ba692009-08-24 11:10:17 +03001623static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001624{
1625 return 1;
1626}
1627
Avi Kivity851ba692009-08-24 11:10:17 +03001628static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001629{
1630 ++svm->vcpu.stat.irq_exits;
1631 return 1;
1632}
1633
Avi Kivity851ba692009-08-24 11:10:17 +03001634static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001635{
1636 return 1;
1637}
1638
Avi Kivity851ba692009-08-24 11:10:17 +03001639static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001640{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001641 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001642 skip_emulated_instruction(&svm->vcpu);
1643 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001644}
1645
Avi Kivity851ba692009-08-24 11:10:17 +03001646static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001647{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001648 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001649 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001650 kvm_emulate_hypercall(&svm->vcpu);
1651 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001652}
1653
Joerg Roedel5bd2edc2010-09-10 17:31:02 +02001654static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
1655{
1656 struct vcpu_svm *svm = to_svm(vcpu);
1657
1658 return svm->nested.nested_cr3;
1659}
1660
1661static void nested_svm_set_tdp_cr3(struct kvm_vcpu *vcpu,
1662 unsigned long root)
1663{
1664 struct vcpu_svm *svm = to_svm(vcpu);
1665
1666 svm->vmcb->control.nested_cr3 = root;
1667 force_new_asid(vcpu);
1668}
1669
1670static void nested_svm_inject_npf_exit(struct kvm_vcpu *vcpu)
1671{
1672 struct vcpu_svm *svm = to_svm(vcpu);
1673
1674 svm->vmcb->control.exit_code = SVM_EXIT_NPF;
1675 svm->vmcb->control.exit_code_hi = 0;
1676 svm->vmcb->control.exit_info_1 = vcpu->arch.fault.error_code;
1677 svm->vmcb->control.exit_info_2 = vcpu->arch.fault.address;
1678
1679 nested_svm_vmexit(svm);
1680}
1681
Joerg Roedel4b161842010-09-10 17:31:03 +02001682static int nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
1683{
1684 int r;
1685
1686 r = kvm_init_shadow_mmu(vcpu, &vcpu->arch.mmu);
1687
1688 vcpu->arch.mmu.set_cr3 = nested_svm_set_tdp_cr3;
1689 vcpu->arch.mmu.get_cr3 = nested_svm_get_tdp_cr3;
1690 vcpu->arch.mmu.inject_page_fault = nested_svm_inject_npf_exit;
1691 vcpu->arch.mmu.shadow_root_level = get_npt_level();
1692 vcpu->arch.walk_mmu = &vcpu->arch.nested_mmu;
1693
1694 return r;
1695}
1696
1697static void nested_svm_uninit_mmu_context(struct kvm_vcpu *vcpu)
1698{
1699 vcpu->arch.walk_mmu = &vcpu->arch.mmu;
1700}
1701
Alexander Grafc0725422008-11-25 20:17:03 +01001702static int nested_svm_check_permissions(struct vcpu_svm *svm)
1703{
Avi Kivityf6801df2010-01-21 15:31:50 +02001704 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001705 || !is_paging(&svm->vcpu)) {
1706 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1707 return 1;
1708 }
1709
1710 if (svm->vmcb->save.cpl) {
1711 kvm_inject_gp(&svm->vcpu, 0);
1712 return 1;
1713 }
1714
1715 return 0;
1716}
1717
Alexander Grafcf74a782008-11-25 20:17:08 +01001718static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1719 bool has_error_code, u32 error_code)
1720{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001721 int vmexit;
1722
Joerg Roedel0295ad72009-08-07 11:49:37 +02001723 if (!is_nested(svm))
1724 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001725
Joerg Roedel0295ad72009-08-07 11:49:37 +02001726 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1727 svm->vmcb->control.exit_code_hi = 0;
1728 svm->vmcb->control.exit_info_1 = error_code;
1729 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1730
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001731 vmexit = nested_svm_intercept(svm);
1732 if (vmexit == NESTED_EXIT_DONE)
1733 svm->nested.exit_required = true;
1734
1735 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001736}
1737
Joerg Roedel8fe54652010-02-19 16:23:01 +01001738/* This function returns true if it is save to enable the irq window */
1739static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001740{
Joerg Roedel26666952009-08-07 11:49:46 +02001741 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001742 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001743
Joerg Roedel26666952009-08-07 11:49:46 +02001744 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001745 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001746
Joerg Roedel26666952009-08-07 11:49:46 +02001747 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001748 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001749
Gleb Natapova0a07cd2010-09-20 10:15:32 +02001750 /*
1751 * if vmexit was already requested (by intercepted exception
1752 * for instance) do not overwrite it with "external interrupt"
1753 * vmexit.
1754 */
1755 if (svm->nested.exit_required)
1756 return false;
1757
Joerg Roedel197717d2010-02-24 18:59:19 +01001758 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1759 svm->vmcb->control.exit_info_1 = 0;
1760 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001761
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001762 if (svm->nested.intercept & 1ULL) {
1763 /*
1764 * The #vmexit can't be emulated here directly because this
1765 * code path runs with irqs and preemtion disabled. A
1766 * #vmexit emulation might sleep. Only signal request for
1767 * the #vmexit here.
1768 */
1769 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001770 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001771 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001772 }
1773
Joerg Roedel8fe54652010-02-19 16:23:01 +01001774 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001775}
1776
Joerg Roedel887f5002010-02-24 18:59:12 +01001777/* This function returns true if it is save to enable the nmi window */
1778static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1779{
1780 if (!is_nested(svm))
1781 return true;
1782
1783 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1784 return true;
1785
1786 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1787 svm->nested.exit_required = true;
1788
1789 return false;
1790}
1791
Joerg Roedel7597f122010-02-19 16:23:00 +01001792static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001793{
1794 struct page *page;
1795
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001796 might_sleep();
1797
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001798 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001799 if (is_error_page(page))
1800 goto error;
1801
Joerg Roedel7597f122010-02-19 16:23:00 +01001802 *_page = page;
1803
1804 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001805
1806error:
1807 kvm_release_page_clean(page);
1808 kvm_inject_gp(&svm->vcpu, 0);
1809
1810 return NULL;
1811}
1812
Joerg Roedel7597f122010-02-19 16:23:00 +01001813static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001814{
Joerg Roedel7597f122010-02-19 16:23:00 +01001815 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001816 kvm_release_page_dirty(page);
1817}
1818
Joerg Roedelce2ac082010-03-01 15:34:39 +01001819static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001820{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001821 unsigned port;
1822 u8 val, bit;
1823 u64 gpa;
1824
1825 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1826 return NESTED_EXIT_HOST;
1827
1828 port = svm->vmcb->control.exit_info_1 >> 16;
1829 gpa = svm->nested.vmcb_iopm + (port / 8);
1830 bit = port % 8;
1831 val = 0;
1832
1833 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1834 val &= (1 << bit);
1835
1836 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1837}
1838
Joerg Roedeld2477822010-03-01 15:34:34 +01001839static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001840{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001841 u32 offset, msr, value;
1842 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001843
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001844 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001845 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001846
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001847 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1848 offset = svm_msrpm_offset(msr);
1849 write = svm->vmcb->control.exit_info_1 & 1;
1850 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001851
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001852 if (offset == MSR_INVALID)
1853 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001854
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001855 /* Offset is in 32 bit units but need in 8 bit units */
1856 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001857
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001858 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1859 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001860
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001861 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001862}
1863
Joerg Roedel410e4d52009-08-07 11:49:44 +02001864static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001865{
Alexander Grafcf74a782008-11-25 20:17:08 +01001866 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001867
Joerg Roedel410e4d52009-08-07 11:49:44 +02001868 switch (exit_code) {
1869 case SVM_EXIT_INTR:
1870 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001871 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001872 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001873 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001874 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001875 if (npt_enabled)
1876 return NESTED_EXIT_HOST;
1877 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001878 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Gleb Natapov631bc482010-10-14 11:22:52 +02001879 /* When we're shadowing, trap PFs, but not async PF */
1880 if (!npt_enabled && svm->apf_reason == 0)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001881 return NESTED_EXIT_HOST;
1882 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001883 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1884 nm_interception(svm);
1885 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001886 default:
1887 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001888 }
1889
Joerg Roedel410e4d52009-08-07 11:49:44 +02001890 return NESTED_EXIT_CONTINUE;
1891}
1892
1893/*
1894 * If this function returns true, this #vmexit was already handled
1895 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001896static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001897{
1898 u32 exit_code = svm->vmcb->control.exit_code;
1899 int vmexit = NESTED_EXIT_HOST;
1900
Alexander Grafcf74a782008-11-25 20:17:08 +01001901 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001902 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001903 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001904 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001905 case SVM_EXIT_IOIO:
1906 vmexit = nested_svm_intercept_ioio(svm);
1907 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001908 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1909 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001910 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001911 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001912 break;
1913 }
1914 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1915 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001916 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001917 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001918 break;
1919 }
1920 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1921 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001922 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001923 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001924 break;
1925 }
1926 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1927 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001928 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001929 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001930 break;
1931 }
1932 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1933 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001934 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001935 vmexit = NESTED_EXIT_DONE;
Gleb Natapov631bc482010-10-14 11:22:52 +02001936 /* async page fault always cause vmexit */
1937 else if ((exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR) &&
1938 svm->apf_reason != 0)
1939 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001940 break;
1941 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001942 case SVM_EXIT_ERR: {
1943 vmexit = NESTED_EXIT_DONE;
1944 break;
1945 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001946 default: {
1947 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001948 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001949 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001950 }
1951 }
1952
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001953 return vmexit;
1954}
1955
1956static int nested_svm_exit_handled(struct vcpu_svm *svm)
1957{
1958 int vmexit;
1959
1960 vmexit = nested_svm_intercept(svm);
1961
1962 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001963 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001964
1965 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001966}
1967
Joerg Roedel0460a972009-08-07 11:49:31 +02001968static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1969{
1970 struct vmcb_control_area *dst = &dst_vmcb->control;
1971 struct vmcb_control_area *from = &from_vmcb->control;
1972
1973 dst->intercept_cr_read = from->intercept_cr_read;
1974 dst->intercept_cr_write = from->intercept_cr_write;
1975 dst->intercept_dr_read = from->intercept_dr_read;
1976 dst->intercept_dr_write = from->intercept_dr_write;
1977 dst->intercept_exceptions = from->intercept_exceptions;
1978 dst->intercept = from->intercept;
1979 dst->iopm_base_pa = from->iopm_base_pa;
1980 dst->msrpm_base_pa = from->msrpm_base_pa;
1981 dst->tsc_offset = from->tsc_offset;
1982 dst->asid = from->asid;
1983 dst->tlb_ctl = from->tlb_ctl;
1984 dst->int_ctl = from->int_ctl;
1985 dst->int_vector = from->int_vector;
1986 dst->int_state = from->int_state;
1987 dst->exit_code = from->exit_code;
1988 dst->exit_code_hi = from->exit_code_hi;
1989 dst->exit_info_1 = from->exit_info_1;
1990 dst->exit_info_2 = from->exit_info_2;
1991 dst->exit_int_info = from->exit_int_info;
1992 dst->exit_int_info_err = from->exit_int_info_err;
1993 dst->nested_ctl = from->nested_ctl;
1994 dst->event_inj = from->event_inj;
1995 dst->event_inj_err = from->event_inj_err;
1996 dst->nested_cr3 = from->nested_cr3;
1997 dst->lbr_ctl = from->lbr_ctl;
1998}
1999
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002000static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01002001{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002002 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002003 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02002004 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002005 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01002006
Joerg Roedel17897f32009-10-09 16:08:29 +02002007 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
2008 vmcb->control.exit_info_1,
2009 vmcb->control.exit_info_2,
2010 vmcb->control.exit_int_info,
2011 vmcb->control.exit_int_info_err);
2012
Joerg Roedel7597f122010-02-19 16:23:00 +01002013 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02002014 if (!nested_vmcb)
2015 return 1;
2016
Joerg Roedel06fc77722010-02-19 16:23:07 +01002017 /* Exit nested SVM mode */
2018 svm->nested.vmcb = 0;
2019
Alexander Grafcf74a782008-11-25 20:17:08 +01002020 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02002021 disable_gif(svm);
2022
2023 nested_vmcb->save.es = vmcb->save.es;
2024 nested_vmcb->save.cs = vmcb->save.cs;
2025 nested_vmcb->save.ss = vmcb->save.ss;
2026 nested_vmcb->save.ds = vmcb->save.ds;
2027 nested_vmcb->save.gdtr = vmcb->save.gdtr;
2028 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedel3f6a9d12010-07-27 18:14:20 +02002029 nested_vmcb->save.efer = svm->vcpu.arch.efer;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002030 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002031 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02002032 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01002033 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02002034 nested_vmcb->save.rflags = vmcb->save.rflags;
2035 nested_vmcb->save.rip = vmcb->save.rip;
2036 nested_vmcb->save.rsp = vmcb->save.rsp;
2037 nested_vmcb->save.rax = vmcb->save.rax;
2038 nested_vmcb->save.dr7 = vmcb->save.dr7;
2039 nested_vmcb->save.dr6 = vmcb->save.dr6;
2040 nested_vmcb->save.cpl = vmcb->save.cpl;
2041
2042 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
2043 nested_vmcb->control.int_vector = vmcb->control.int_vector;
2044 nested_vmcb->control.int_state = vmcb->control.int_state;
2045 nested_vmcb->control.exit_code = vmcb->control.exit_code;
2046 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
2047 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
2048 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
2049 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
2050 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Joerg Roedel7a190662010-07-27 18:14:21 +02002051 nested_vmcb->control.next_rip = vmcb->control.next_rip;
Alexander Graf8d23c462009-10-09 16:08:25 +02002052
2053 /*
2054 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
2055 * to make sure that we do not lose injected events. So check event_inj
2056 * here and copy it to exit_int_info if it is valid.
2057 * Exit_int_info and event_inj can't be both valid because the case
2058 * below only happens on a VMRUN instruction intercept which has
2059 * no valid exit_int_info set.
2060 */
2061 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
2062 struct vmcb_control_area *nc = &nested_vmcb->control;
2063
2064 nc->exit_int_info = vmcb->control.event_inj;
2065 nc->exit_int_info_err = vmcb->control.event_inj_err;
2066 }
2067
Joerg Roedel33740e42009-08-07 11:49:29 +02002068 nested_vmcb->control.tlb_ctl = 0;
2069 nested_vmcb->control.event_inj = 0;
2070 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01002071
2072 /* We always set V_INTR_MASKING and remember the old value in hflags */
2073 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
2074 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
2075
Alexander Grafcf74a782008-11-25 20:17:08 +01002076 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02002077 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01002078
Alexander Graf219b65d2009-06-15 15:21:25 +02002079 kvm_clear_exception_queue(&svm->vcpu);
2080 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002081
Joerg Roedel4b161842010-09-10 17:31:03 +02002082 svm->nested.nested_cr3 = 0;
2083
Alexander Grafcf74a782008-11-25 20:17:08 +01002084 /* Restore selected save entries */
2085 svm->vmcb->save.es = hsave->save.es;
2086 svm->vmcb->save.cs = hsave->save.cs;
2087 svm->vmcb->save.ss = hsave->save.ss;
2088 svm->vmcb->save.ds = hsave->save.ds;
2089 svm->vmcb->save.gdtr = hsave->save.gdtr;
2090 svm->vmcb->save.idtr = hsave->save.idtr;
2091 svm->vmcb->save.rflags = hsave->save.rflags;
2092 svm_set_efer(&svm->vcpu, hsave->save.efer);
2093 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
2094 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
2095 if (npt_enabled) {
2096 svm->vmcb->save.cr3 = hsave->save.cr3;
2097 svm->vcpu.arch.cr3 = hsave->save.cr3;
2098 } else {
Avi Kivity23902182010-06-10 17:02:16 +03002099 (void)kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
Alexander Grafcf74a782008-11-25 20:17:08 +01002100 }
2101 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
2102 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
2103 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
2104 svm->vmcb->save.dr7 = 0;
2105 svm->vmcb->save.cpl = 0;
2106 svm->vmcb->control.exit_int_info = 0;
2107
Joerg Roedel7597f122010-02-19 16:23:00 +01002108 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01002109
Joerg Roedel4b161842010-09-10 17:31:03 +02002110 nested_svm_uninit_mmu_context(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01002111 kvm_mmu_reset_context(&svm->vcpu);
2112 kvm_mmu_load(&svm->vcpu);
2113
2114 return 0;
2115}
Alexander Graf3d6368e2008-11-25 20:17:07 +01002116
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002117static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002118{
Joerg Roedel323c3d82010-03-01 15:34:37 +01002119 /*
2120 * This function merges the msr permission bitmaps of kvm and the
2121 * nested vmcb. It is omptimized in that it only merges the parts where
2122 * the kvm msr permission bitmap may contain zero bits
2123 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01002124 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002125
Joerg Roedel323c3d82010-03-01 15:34:37 +01002126 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
2127 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002128
Joerg Roedel323c3d82010-03-01 15:34:37 +01002129 for (i = 0; i < MSRPM_OFFSETS; i++) {
2130 u32 value, p;
2131 u64 offset;
2132
2133 if (msrpm_offsets[i] == 0xffffffff)
2134 break;
2135
Joerg Roedel0d6b3532010-03-01 15:34:38 +01002136 p = msrpm_offsets[i];
2137 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01002138
2139 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2140 return false;
2141
2142 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2143 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002144
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002145 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002146
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002147 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002148}
2149
Joerg Roedel52c65a302010-08-02 16:46:44 +02002150static bool nested_vmcb_checks(struct vmcb *vmcb)
2151{
2152 if ((vmcb->control.intercept & (1ULL << INTERCEPT_VMRUN)) == 0)
2153 return false;
2154
Joerg Roedeldbe77582010-08-02 16:46:45 +02002155 if (vmcb->control.asid == 0)
2156 return false;
2157
Joerg Roedel4b161842010-09-10 17:31:03 +02002158 if (vmcb->control.nested_ctl && !npt_enabled)
2159 return false;
2160
Joerg Roedel52c65a302010-08-02 16:46:44 +02002161 return true;
2162}
2163
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002164static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002165{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002166 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002167 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002168 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002169 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002170 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002171
Joerg Roedel06fc77722010-02-19 16:23:07 +01002172 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002173
Joerg Roedel7597f122010-02-19 16:23:00 +01002174 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002175 if (!nested_vmcb)
2176 return false;
2177
Joerg Roedel52c65a302010-08-02 16:46:44 +02002178 if (!nested_vmcb_checks(nested_vmcb)) {
2179 nested_vmcb->control.exit_code = SVM_EXIT_ERR;
2180 nested_vmcb->control.exit_code_hi = 0;
2181 nested_vmcb->control.exit_info_1 = 0;
2182 nested_vmcb->control.exit_info_2 = 0;
2183
2184 nested_svm_unmap(page);
2185
2186 return false;
2187 }
2188
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002189 trace_kvm_nested_vmrun(svm->vmcb->save.rip, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002190 nested_vmcb->save.rip,
2191 nested_vmcb->control.int_ctl,
2192 nested_vmcb->control.event_inj,
2193 nested_vmcb->control.nested_ctl);
2194
Joerg Roedel2e554e82010-02-24 18:59:14 +01002195 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2196 nested_vmcb->control.intercept_cr_write,
2197 nested_vmcb->control.intercept_exceptions,
2198 nested_vmcb->control.intercept);
2199
Alexander Graf3d6368e2008-11-25 20:17:07 +01002200 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002201 kvm_clear_exception_queue(&svm->vcpu);
2202 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002203
Joerg Roedele0231712010-02-24 18:59:10 +01002204 /*
2205 * Save the old vmcb, so we don't need to pick what we save, but can
2206 * restore everything when a VMEXIT occurs
2207 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002208 hsave->save.es = vmcb->save.es;
2209 hsave->save.cs = vmcb->save.cs;
2210 hsave->save.ss = vmcb->save.ss;
2211 hsave->save.ds = vmcb->save.ds;
2212 hsave->save.gdtr = vmcb->save.gdtr;
2213 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002214 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002215 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002216 hsave->save.cr4 = svm->vcpu.arch.cr4;
2217 hsave->save.rflags = vmcb->save.rflags;
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002218 hsave->save.rip = kvm_rip_read(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002219 hsave->save.rsp = vmcb->save.rsp;
2220 hsave->save.rax = vmcb->save.rax;
2221 if (npt_enabled)
2222 hsave->save.cr3 = vmcb->save.cr3;
2223 else
2224 hsave->save.cr3 = svm->vcpu.arch.cr3;
2225
Joerg Roedel0460a972009-08-07 11:49:31 +02002226 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002227
2228 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2229 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2230 else
2231 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2232
Joerg Roedel4b161842010-09-10 17:31:03 +02002233 if (nested_vmcb->control.nested_ctl) {
2234 kvm_mmu_unload(&svm->vcpu);
2235 svm->nested.nested_cr3 = nested_vmcb->control.nested_cr3;
2236 nested_svm_init_mmu_context(&svm->vcpu);
2237 }
2238
Alexander Graf3d6368e2008-11-25 20:17:07 +01002239 /* Load the nested guest state */
2240 svm->vmcb->save.es = nested_vmcb->save.es;
2241 svm->vmcb->save.cs = nested_vmcb->save.cs;
2242 svm->vmcb->save.ss = nested_vmcb->save.ss;
2243 svm->vmcb->save.ds = nested_vmcb->save.ds;
2244 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2245 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2246 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2247 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2248 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2249 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2250 if (npt_enabled) {
2251 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2252 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002253 } else
Avi Kivity23902182010-06-10 17:02:16 +03002254 (void)kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002255
2256 /* Guest paging mode is active - reset mmu */
2257 kvm_mmu_reset_context(&svm->vcpu);
2258
Joerg Roedeldefbba52009-08-07 11:49:30 +02002259 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002260 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2261 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2262 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002263
Alexander Graf3d6368e2008-11-25 20:17:07 +01002264 /* In case we don't even reach vcpu_run, the fields are not updated */
2265 svm->vmcb->save.rax = nested_vmcb->save.rax;
2266 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2267 svm->vmcb->save.rip = nested_vmcb->save.rip;
2268 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2269 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2270 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2271
Joerg Roedelf7138532010-03-01 15:34:40 +01002272 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002273 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002274
Joerg Roedelaad42c62009-08-07 11:49:34 +02002275 /* cache intercepts */
2276 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2277 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2278 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2279 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2280 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2281 svm->nested.intercept = nested_vmcb->control.intercept;
2282
Alexander Graf3d6368e2008-11-25 20:17:07 +01002283 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002284 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002285 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2286 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2287 else
2288 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2289
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002290 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2291 /* We only want the cr8 intercept bits of the guest */
2292 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2293 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2294 }
2295
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002296 /* We don't want to see VMMCALLs from a nested guest */
2297 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2298
Joerg Roedele0231712010-02-24 18:59:10 +01002299 /*
2300 * We don't want a nested guest to be more powerful than the guest, so
2301 * all intercepts are ORed
2302 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002303 svm->vmcb->control.intercept_cr_read |=
2304 nested_vmcb->control.intercept_cr_read;
2305 svm->vmcb->control.intercept_cr_write |=
2306 nested_vmcb->control.intercept_cr_write;
2307 svm->vmcb->control.intercept_dr_read |=
2308 nested_vmcb->control.intercept_dr_read;
2309 svm->vmcb->control.intercept_dr_write |=
2310 nested_vmcb->control.intercept_dr_write;
2311 svm->vmcb->control.intercept_exceptions |=
2312 nested_vmcb->control.intercept_exceptions;
2313
2314 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2315
2316 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002317 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2318 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2319 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002320 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2321 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2322
Joerg Roedel7597f122010-02-19 16:23:00 +01002323 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002324
Joerg Roedel06fc77722010-02-19 16:23:07 +01002325 /* nested_vmcb is our indicator if nested SVM is activated */
2326 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002327
Joerg Roedel2af91942009-08-07 11:49:28 +02002328 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002329
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002330 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002331}
2332
Joerg Roedel9966bf62009-08-07 11:49:40 +02002333static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002334{
2335 to_vmcb->save.fs = from_vmcb->save.fs;
2336 to_vmcb->save.gs = from_vmcb->save.gs;
2337 to_vmcb->save.tr = from_vmcb->save.tr;
2338 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2339 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2340 to_vmcb->save.star = from_vmcb->save.star;
2341 to_vmcb->save.lstar = from_vmcb->save.lstar;
2342 to_vmcb->save.cstar = from_vmcb->save.cstar;
2343 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2344 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2345 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2346 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002347}
2348
Avi Kivity851ba692009-08-24 11:10:17 +03002349static int vmload_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(nested_vmcb, svm->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 vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002371{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002372 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002373 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002374
Alexander Graf55426752008-11-25 20:17:06 +01002375 if (nested_svm_check_permissions(svm))
2376 return 1;
2377
2378 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2379 skip_emulated_instruction(&svm->vcpu);
2380
Joerg Roedel7597f122010-02-19 16:23:00 +01002381 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002382 if (!nested_vmcb)
2383 return 1;
2384
2385 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002386 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002387
2388 return 1;
2389}
2390
Avi Kivity851ba692009-08-24 11:10:17 +03002391static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002392{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002393 if (nested_svm_check_permissions(svm))
2394 return 1;
2395
Roedel, Joergb75f4eb2010-09-03 14:21:40 +02002396 /* Save rip after vmrun instruction */
2397 kvm_rip_write(&svm->vcpu, kvm_rip_read(&svm->vcpu) + 3);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002398
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002399 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002400 return 1;
2401
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002402 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002403 goto failed;
2404
2405 return 1;
2406
2407failed:
2408
2409 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2410 svm->vmcb->control.exit_code_hi = 0;
2411 svm->vmcb->control.exit_info_1 = 0;
2412 svm->vmcb->control.exit_info_2 = 0;
2413
2414 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002415
2416 return 1;
2417}
2418
Avi Kivity851ba692009-08-24 11:10:17 +03002419static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002420{
2421 if (nested_svm_check_permissions(svm))
2422 return 1;
2423
2424 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2425 skip_emulated_instruction(&svm->vcpu);
Avi Kivity3842d132010-07-27 12:30:24 +03002426 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +01002427
Joerg Roedel2af91942009-08-07 11:49:28 +02002428 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002429
2430 return 1;
2431}
2432
Avi Kivity851ba692009-08-24 11:10:17 +03002433static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002434{
2435 if (nested_svm_check_permissions(svm))
2436 return 1;
2437
2438 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2439 skip_emulated_instruction(&svm->vcpu);
2440
Joerg Roedel2af91942009-08-07 11:49:28 +02002441 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002442
2443 /* After a CLGI no interrupts should come */
2444 svm_clear_vintr(svm);
2445 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2446
2447 return 1;
2448}
2449
Avi Kivity851ba692009-08-24 11:10:17 +03002450static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002451{
2452 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002453
Joerg Roedelec1ff792009-10-09 16:08:31 +02002454 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2455 vcpu->arch.regs[VCPU_REGS_RAX]);
2456
Alexander Grafff092382009-06-15 15:21:24 +02002457 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2458 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2459
2460 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2461 skip_emulated_instruction(&svm->vcpu);
2462 return 1;
2463}
2464
Joerg Roedel532a46b2009-10-09 16:08:32 +02002465static int skinit_interception(struct vcpu_svm *svm)
2466{
2467 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2468
2469 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2470 return 1;
2471}
2472
Avi Kivity851ba692009-08-24 11:10:17 +03002473static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002474{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002475 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002476 return 1;
2477}
2478
Avi Kivity851ba692009-08-24 11:10:17 +03002479static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002480{
Izik Eidus37817f22008-03-24 23:14:53 +02002481 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002482 int reason;
2483 int int_type = svm->vmcb->control.exit_int_info &
2484 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002485 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002486 uint32_t type =
2487 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2488 uint32_t idt_v =
2489 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002490 bool has_error_code = false;
2491 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002492
2493 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002494
Izik Eidus37817f22008-03-24 23:14:53 +02002495 if (svm->vmcb->control.exit_info_2 &
2496 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002497 reason = TASK_SWITCH_IRET;
2498 else if (svm->vmcb->control.exit_info_2 &
2499 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2500 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002501 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002502 reason = TASK_SWITCH_GATE;
2503 else
2504 reason = TASK_SWITCH_CALL;
2505
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002506 if (reason == TASK_SWITCH_GATE) {
2507 switch (type) {
2508 case SVM_EXITINTINFO_TYPE_NMI:
2509 svm->vcpu.arch.nmi_injected = false;
2510 break;
2511 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002512 if (svm->vmcb->control.exit_info_2 &
2513 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2514 has_error_code = true;
2515 error_code =
2516 (u32)svm->vmcb->control.exit_info_2;
2517 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002518 kvm_clear_exception_queue(&svm->vcpu);
2519 break;
2520 case SVM_EXITINTINFO_TYPE_INTR:
2521 kvm_clear_interrupt_queue(&svm->vcpu);
2522 break;
2523 default:
2524 break;
2525 }
2526 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002527
Gleb Natapov8317c292009-04-12 13:37:02 +03002528 if (reason != TASK_SWITCH_GATE ||
2529 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2530 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002531 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2532 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002533
Gleb Natapovacb54512010-04-15 21:03:50 +03002534 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2535 has_error_code, error_code) == EMULATE_FAIL) {
2536 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2537 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2538 svm->vcpu.run->internal.ndata = 0;
2539 return 0;
2540 }
2541 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002542}
2543
Avi Kivity851ba692009-08-24 11:10:17 +03002544static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002545{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002546 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002547 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002548 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002549}
2550
Avi Kivity851ba692009-08-24 11:10:17 +03002551static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002552{
2553 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002554 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002555 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002556 return 1;
2557}
2558
Avi Kivity851ba692009-08-24 11:10:17 +03002559static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002560{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002561 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002562}
2563
Avi Kivity851ba692009-08-24 11:10:17 +03002564static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002565{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002566 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567}
2568
Joerg Roedelcda00082010-09-02 17:29:46 +02002569static int cr0_write_interception(struct vcpu_svm *svm)
2570{
2571 struct kvm_vcpu *vcpu = &svm->vcpu;
2572 int r;
2573
2574 r = emulate_instruction(&svm->vcpu, 0, 0, 0);
2575
2576 if (svm->nested.vmexit_rip) {
2577 kvm_register_write(vcpu, VCPU_REGS_RIP, svm->nested.vmexit_rip);
2578 kvm_register_write(vcpu, VCPU_REGS_RSP, svm->nested.vmexit_rsp);
2579 kvm_register_write(vcpu, VCPU_REGS_RAX, svm->nested.vmexit_rax);
2580 svm->nested.vmexit_rip = 0;
2581 }
2582
2583 return r == EMULATE_DONE;
2584}
2585
Avi Kivity851ba692009-08-24 11:10:17 +03002586static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002587{
Avi Kivity851ba692009-08-24 11:10:17 +03002588 struct kvm_run *kvm_run = svm->vcpu.run;
2589
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002590 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2591 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002592 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002593 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2594 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002595 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002596 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002597 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2598 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002599 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2600 return 0;
2601}
2602
Avi Kivity6aa8b732006-12-10 02:21:36 -08002603static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2604{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002605 struct vcpu_svm *svm = to_svm(vcpu);
2606
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302608 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002609 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002610
Joerg Roedel20824f32009-09-16 15:24:18 +02002611 if (is_nested(svm))
2612 tsc_offset = svm->nested.hsave->control.tsc_offset;
2613 else
2614 tsc_offset = svm->vmcb->control.tsc_offset;
2615
2616 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002617 break;
2618 }
Brian Gerst8c065852010-07-17 09:03:26 -04002619 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002620 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002621 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002622#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002623 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002624 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002625 break;
2626 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002627 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002628 break;
2629 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002630 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002631 break;
2632 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002633 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002634 break;
2635#endif
2636 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002637 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002638 break;
2639 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002640 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002641 break;
2642 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002643 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002644 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002645 /*
2646 * Nobody will change the following 5 values in the VMCB so we can
2647 * safely return them on rdmsr. They will always be 0 until LBRV is
2648 * implemented.
2649 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002650 case MSR_IA32_DEBUGCTLMSR:
2651 *data = svm->vmcb->save.dbgctl;
2652 break;
2653 case MSR_IA32_LASTBRANCHFROMIP:
2654 *data = svm->vmcb->save.br_from;
2655 break;
2656 case MSR_IA32_LASTBRANCHTOIP:
2657 *data = svm->vmcb->save.br_to;
2658 break;
2659 case MSR_IA32_LASTINTFROMIP:
2660 *data = svm->vmcb->save.last_excp_from;
2661 break;
2662 case MSR_IA32_LASTINTTOIP:
2663 *data = svm->vmcb->save.last_excp_to;
2664 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002665 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002666 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002667 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002668 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002669 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002670 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002671 case MSR_IA32_UCODE_REV:
2672 *data = 0x01000065;
2673 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002674 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002675 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002676 }
2677 return 0;
2678}
2679
Avi Kivity851ba692009-08-24 11:10:17 +03002680static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002681{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002682 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002683 u64 data;
2684
Avi Kivity59200272010-01-25 19:47:02 +02002685 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2686 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002687 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002688 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002689 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002690
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002691 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002692 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002693 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002694 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002695 }
2696 return 1;
2697}
2698
Joerg Roedel4a810182010-02-24 18:59:15 +01002699static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2700{
2701 struct vcpu_svm *svm = to_svm(vcpu);
2702 int svm_dis, chg_mask;
2703
2704 if (data & ~SVM_VM_CR_VALID_MASK)
2705 return 1;
2706
2707 chg_mask = SVM_VM_CR_VALID_MASK;
2708
2709 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2710 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2711
2712 svm->nested.vm_cr_msr &= ~chg_mask;
2713 svm->nested.vm_cr_msr |= (data & chg_mask);
2714
2715 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2716
2717 /* check for svm_disable while efer.svme is set */
2718 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2719 return 1;
2720
2721 return 0;
2722}
2723
Avi Kivity6aa8b732006-12-10 02:21:36 -08002724static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2725{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002726 struct vcpu_svm *svm = to_svm(vcpu);
2727
Avi Kivity6aa8b732006-12-10 02:21:36 -08002728 switch (ecx) {
Zachary Amsdenf4e1b3c2010-08-19 22:07:16 -10002729 case MSR_IA32_TSC:
Zachary Amsden99e3e302010-08-19 22:07:17 -10002730 kvm_write_tsc(vcpu, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002731 break;
Brian Gerst8c065852010-07-17 09:03:26 -04002732 case MSR_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002733 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002734 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002735#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002736 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002737 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002738 break;
2739 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002740 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002741 break;
2742 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002743 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002744 break;
2745 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002746 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002747 break;
2748#endif
2749 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002750 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002751 break;
2752 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002753 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002754 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002755 break;
2756 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002757 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002758 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002759 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002760 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002761 if (!svm_has(SVM_FEATURE_LBRV)) {
2762 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002763 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002764 break;
2765 }
2766 if (data & DEBUGCTL_RESERVED_BITS)
2767 return 1;
2768
2769 svm->vmcb->save.dbgctl = data;
2770 if (data & (1ULL<<0))
2771 svm_enable_lbrv(svm);
2772 else
2773 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002774 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002775 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002776 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002777 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002778 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002779 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002780 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002781 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2782 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002783 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002784 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002785 }
2786 return 0;
2787}
2788
Avi Kivity851ba692009-08-24 11:10:17 +03002789static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002790{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002791 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002792 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002793 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002794
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002795
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002796 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002797 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2798 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002799 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002800 } else {
2801 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002802 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002803 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002804 return 1;
2805}
2806
Avi Kivity851ba692009-08-24 11:10:17 +03002807static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002808{
Rusty Russelle756fc62007-07-30 20:07:08 +10002809 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002810 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002811 else
Avi Kivity851ba692009-08-24 11:10:17 +03002812 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002813}
2814
Avi Kivity851ba692009-08-24 11:10:17 +03002815static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002816{
Avi Kivity851ba692009-08-24 11:10:17 +03002817 struct kvm_run *kvm_run = svm->vcpu.run;
2818
Avi Kivity3842d132010-07-27 12:30:24 +03002819 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
Alexander Graff0b85052008-11-25 20:17:01 +01002820 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002821 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002822 /*
2823 * If the user space waits to inject interrupts, exit as soon as
2824 * possible
2825 */
Gleb Natapov80618232009-04-21 17:44:56 +03002826 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2827 kvm_run->request_interrupt_window &&
2828 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002829 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002830 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2831 return 0;
2832 }
2833
2834 return 1;
2835}
2836
Mark Langsdorf565d0992009-10-06 14:25:02 -05002837static int pause_interception(struct vcpu_svm *svm)
2838{
2839 kvm_vcpu_on_spin(&(svm->vcpu));
2840 return 1;
2841}
2842
Avi Kivity851ba692009-08-24 11:10:17 +03002843static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002844 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2845 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2846 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2847 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002848 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedelcda00082010-09-02 17:29:46 +02002849 [SVM_EXIT_WRITE_CR0] = cr0_write_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002850 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2851 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2852 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2853 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002854 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2855 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2856 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002857 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2858 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2859 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2860 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002861 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2862 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2863 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2864 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002865 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002866 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002867 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002868 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002869 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2870 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002871 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002872 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2873 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2874 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2875 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002876 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 [SVM_EXIT_SMI] = nop_on_interception,
2878 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002879 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002880 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002881 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002882 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002883 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002884 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002885 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002886 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002887 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002888 [SVM_EXIT_MSR] = msr_interception,
2889 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002890 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002891 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002892 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002893 [SVM_EXIT_VMLOAD] = vmload_interception,
2894 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002895 [SVM_EXIT_STGI] = stgi_interception,
2896 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002897 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002898 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002899 [SVM_EXIT_MONITOR] = invalid_op_interception,
2900 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002901 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002902};
2903
Joerg Roedel3f10c842010-05-05 16:04:42 +02002904void dump_vmcb(struct kvm_vcpu *vcpu)
2905{
2906 struct vcpu_svm *svm = to_svm(vcpu);
2907 struct vmcb_control_area *control = &svm->vmcb->control;
2908 struct vmcb_save_area *save = &svm->vmcb->save;
2909
2910 pr_err("VMCB Control Area:\n");
2911 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2912 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2913 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2914 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2915 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2916 pr_err("intercepts: %016llx\n", control->intercept);
2917 pr_err("pause filter count: %d\n", control->pause_filter_count);
2918 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2919 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2920 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2921 pr_err("asid: %d\n", control->asid);
2922 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2923 pr_err("int_ctl: %08x\n", control->int_ctl);
2924 pr_err("int_vector: %08x\n", control->int_vector);
2925 pr_err("int_state: %08x\n", control->int_state);
2926 pr_err("exit_code: %08x\n", control->exit_code);
2927 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2928 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2929 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2930 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2931 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2932 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2933 pr_err("event_inj: %08x\n", control->event_inj);
2934 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2935 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2936 pr_err("next_rip: %016llx\n", control->next_rip);
2937 pr_err("VMCB State Save Area:\n");
2938 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2939 save->es.selector, save->es.attrib,
2940 save->es.limit, save->es.base);
2941 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2942 save->cs.selector, save->cs.attrib,
2943 save->cs.limit, save->cs.base);
2944 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2945 save->ss.selector, save->ss.attrib,
2946 save->ss.limit, save->ss.base);
2947 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2948 save->ds.selector, save->ds.attrib,
2949 save->ds.limit, save->ds.base);
2950 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2951 save->fs.selector, save->fs.attrib,
2952 save->fs.limit, save->fs.base);
2953 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2954 save->gs.selector, save->gs.attrib,
2955 save->gs.limit, save->gs.base);
2956 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2957 save->gdtr.selector, save->gdtr.attrib,
2958 save->gdtr.limit, save->gdtr.base);
2959 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2960 save->ldtr.selector, save->ldtr.attrib,
2961 save->ldtr.limit, save->ldtr.base);
2962 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2963 save->idtr.selector, save->idtr.attrib,
2964 save->idtr.limit, save->idtr.base);
2965 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2966 save->tr.selector, save->tr.attrib,
2967 save->tr.limit, save->tr.base);
2968 pr_err("cpl: %d efer: %016llx\n",
2969 save->cpl, save->efer);
2970 pr_err("cr0: %016llx cr2: %016llx\n",
2971 save->cr0, save->cr2);
2972 pr_err("cr3: %016llx cr4: %016llx\n",
2973 save->cr3, save->cr4);
2974 pr_err("dr6: %016llx dr7: %016llx\n",
2975 save->dr6, save->dr7);
2976 pr_err("rip: %016llx rflags: %016llx\n",
2977 save->rip, save->rflags);
2978 pr_err("rsp: %016llx rax: %016llx\n",
2979 save->rsp, save->rax);
2980 pr_err("star: %016llx lstar: %016llx\n",
2981 save->star, save->lstar);
2982 pr_err("cstar: %016llx sfmask: %016llx\n",
2983 save->cstar, save->sfmask);
2984 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2985 save->kernel_gs_base, save->sysenter_cs);
2986 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2987 save->sysenter_esp, save->sysenter_eip);
2988 pr_err("gpat: %016llx dbgctl: %016llx\n",
2989 save->g_pat, save->dbgctl);
2990 pr_err("br_from: %016llx br_to: %016llx\n",
2991 save->br_from, save->br_to);
2992 pr_err("excp_from: %016llx excp_to: %016llx\n",
2993 save->last_excp_from, save->last_excp_to);
2994
2995}
2996
Avi Kivity851ba692009-08-24 11:10:17 +03002997static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002998{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002999 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03003000 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003001 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003002
Avi Kivity5bfd8b52010-03-11 10:50:44 +02003003 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02003004
Joerg Roedel2be4fc72010-04-22 12:33:09 +02003005 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
3006 vcpu->arch.cr0 = svm->vmcb->save.cr0;
3007 if (npt_enabled)
3008 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003009
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003010 if (unlikely(svm->nested.exit_required)) {
3011 nested_svm_vmexit(svm);
3012 svm->nested.exit_required = false;
3013
3014 return 1;
3015 }
3016
Alexander Grafcf74a782008-11-25 20:17:08 +01003017 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02003018 int vmexit;
3019
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02003020 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
3021 svm->vmcb->control.exit_info_1,
3022 svm->vmcb->control.exit_info_2,
3023 svm->vmcb->control.exit_int_info,
3024 svm->vmcb->control.exit_int_info_err);
3025
Joerg Roedel410e4d52009-08-07 11:49:44 +02003026 vmexit = nested_svm_exit_special(svm);
3027
3028 if (vmexit == NESTED_EXIT_CONTINUE)
3029 vmexit = nested_svm_exit_handled(svm);
3030
3031 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01003032 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01003033 }
3034
Joerg Roedela5c38322009-08-07 11:49:32 +02003035 svm_complete_interrupts(svm);
3036
Avi Kivity04d2cc72007-09-10 18:10:54 +03003037 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
3038 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
3039 kvm_run->fail_entry.hardware_entry_failure_reason
3040 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02003041 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
3042 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03003043 return 0;
3044 }
3045
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003046 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003047 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Joerg Roedel55c5e462010-09-10 17:31:04 +02003048 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH &&
3049 exit_code != SVM_EXIT_INTR && exit_code != SVM_EXIT_NMI)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003050 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
3051 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08003052 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003053 exit_code);
3054
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02003055 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08003056 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003057 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03003058 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003059 return 0;
3060 }
3061
Avi Kivity851ba692009-08-24 11:10:17 +03003062 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003063}
3064
3065static void reload_tss(struct kvm_vcpu *vcpu)
3066{
3067 int cpu = raw_smp_processor_id();
3068
Tejun Heo0fe1e002009-10-29 22:34:14 +09003069 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
3070 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08003071 load_TR_desc();
3072}
3073
Rusty Russelle756fc62007-07-30 20:07:08 +10003074static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003075{
3076 int cpu = raw_smp_processor_id();
3077
Tejun Heo0fe1e002009-10-29 22:34:14 +09003078 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003079
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003080 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03003081 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09003082 if (svm->asid_generation != sd->asid_generation)
3083 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003084}
3085
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003086static void svm_inject_nmi(struct kvm_vcpu *vcpu)
3087{
3088 struct vcpu_svm *svm = to_svm(vcpu);
3089
3090 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
3091 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003092 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003093 ++vcpu->stat.nmi_injections;
3094}
Avi Kivity6aa8b732006-12-10 02:21:36 -08003095
Eddie Dong85f455f2007-07-06 12:20:49 +03003096static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003097{
3098 struct vmcb_control_area *control;
3099
Rusty Russelle756fc62007-07-30 20:07:08 +10003100 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03003101 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003102 control->int_ctl &= ~V_INTR_PRIO_MASK;
3103 control->int_ctl |= V_IRQ_MASK |
3104 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
3105}
3106
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003107static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03003108{
3109 struct vcpu_svm *svm = to_svm(vcpu);
3110
Joerg Roedel2af91942009-08-07 11:49:28 +02003111 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01003112
Gleb Natapov9fb2d2b2010-05-23 14:28:26 +03003113 trace_kvm_inj_virq(vcpu->arch.interrupt.nr);
3114 ++vcpu->stat.irq_injections;
3115
Alexander Graf219b65d2009-06-15 15:21:25 +02003116 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
3117 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03003118}
3119
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003120static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
3121{
3122 struct vcpu_svm *svm = to_svm(vcpu);
3123
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003124 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3125 return;
3126
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003127 if (irr == -1)
3128 return;
3129
3130 if (tpr >= irr)
3131 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
3132}
3133
3134static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003135{
3136 struct vcpu_svm *svm = to_svm(vcpu);
3137 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02003138 int ret;
3139 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
3140 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
3141 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
3142
3143 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02003144}
3145
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003146static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
3147{
3148 struct vcpu_svm *svm = to_svm(vcpu);
3149
3150 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
3151}
3152
3153static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
3154{
3155 struct vcpu_svm *svm = to_svm(vcpu);
3156
3157 if (masked) {
3158 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003159 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003160 } else {
3161 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02003162 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003163 }
3164}
3165
Gleb Natapov78646122009-03-23 12:12:11 +02003166static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
3167{
3168 struct vcpu_svm *svm = to_svm(vcpu);
3169 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02003170 int ret;
3171
3172 if (!gif_set(svm) ||
3173 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
3174 return 0;
3175
3176 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
3177
3178 if (is_nested(svm))
3179 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3180
3181 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003182}
3183
Gleb Natapov9222be12009-04-23 17:14:37 +03003184static void enable_irq_window(struct kvm_vcpu *vcpu)
3185{
Alexander Graf219b65d2009-06-15 15:21:25 +02003186 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003187
Joerg Roedele0231712010-02-24 18:59:10 +01003188 /*
3189 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3190 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3191 * get that intercept, this function will be called again though and
3192 * we'll get the vintr intercept.
3193 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003194 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003195 svm_set_vintr(svm);
3196 svm_inject_irq(svm, 0x0);
3197 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003198}
3199
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003200static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003201{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003202 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003203
Gleb Natapov44c11432009-05-11 13:35:52 +03003204 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3205 == HF_NMI_MASK)
3206 return; /* IRET will cause a vm exit */
3207
Joerg Roedele0231712010-02-24 18:59:10 +01003208 /*
3209 * Something prevents NMI from been injected. Single step over possible
3210 * problem (IRET or exception injection or interrupt shadow)
3211 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003212 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003213 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3214 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003215}
3216
Izik Eiduscbc94022007-10-25 00:29:55 +02003217static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3218{
3219 return 0;
3220}
3221
Avi Kivityd9e368d2007-06-07 19:18:30 +03003222static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3223{
3224 force_new_asid(vcpu);
3225}
3226
Avi Kivity04d2cc72007-09-10 18:10:54 +03003227static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3228{
3229}
3230
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003231static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3232{
3233 struct vcpu_svm *svm = to_svm(vcpu);
3234
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003235 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3236 return;
3237
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003238 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3239 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003240 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003241 }
3242}
3243
Joerg Roedel649d6862008-04-16 16:51:15 +02003244static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3245{
3246 struct vcpu_svm *svm = to_svm(vcpu);
3247 u64 cr8;
3248
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003249 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3250 return;
3251
Joerg Roedel649d6862008-04-16 16:51:15 +02003252 cr8 = kvm_get_cr8(vcpu);
3253 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3254 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3255}
3256
Gleb Natapov9222be12009-04-23 17:14:37 +03003257static void svm_complete_interrupts(struct vcpu_svm *svm)
3258{
3259 u8 vector;
3260 int type;
3261 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003262 unsigned int3_injected = svm->int3_injected;
3263
3264 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003265
Avi Kivity3842d132010-07-27 12:30:24 +03003266 if (svm->vcpu.arch.hflags & HF_IRET_MASK) {
Gleb Natapov44c11432009-05-11 13:35:52 +03003267 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
Avi Kivity3842d132010-07-27 12:30:24 +03003268 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3269 }
Gleb Natapov44c11432009-05-11 13:35:52 +03003270
Gleb Natapov9222be12009-04-23 17:14:37 +03003271 svm->vcpu.arch.nmi_injected = false;
3272 kvm_clear_exception_queue(&svm->vcpu);
3273 kvm_clear_interrupt_queue(&svm->vcpu);
3274
3275 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3276 return;
3277
Avi Kivity3842d132010-07-27 12:30:24 +03003278 kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
3279
Gleb Natapov9222be12009-04-23 17:14:37 +03003280 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3281 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3282
3283 switch (type) {
3284 case SVM_EXITINTINFO_TYPE_NMI:
3285 svm->vcpu.arch.nmi_injected = true;
3286 break;
3287 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003288 /*
3289 * In case of software exceptions, do not reinject the vector,
3290 * but re-execute the instruction instead. Rewind RIP first
3291 * if we emulated INT3 before.
3292 */
3293 if (kvm_exception_is_soft(vector)) {
3294 if (vector == BP_VECTOR && int3_injected &&
3295 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3296 kvm_rip_write(&svm->vcpu,
3297 kvm_rip_read(&svm->vcpu) -
3298 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003299 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003300 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003301 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3302 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003303 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003304
3305 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003306 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003307 break;
3308 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003309 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003310 break;
3311 default:
3312 break;
3313 }
3314}
3315
Avi Kivityb463a6f2010-07-20 15:06:17 +03003316static void svm_cancel_injection(struct kvm_vcpu *vcpu)
3317{
3318 struct vcpu_svm *svm = to_svm(vcpu);
3319 struct vmcb_control_area *control = &svm->vmcb->control;
3320
3321 control->exit_int_info = control->event_inj;
3322 control->exit_int_info_err = control->event_inj_err;
3323 control->event_inj = 0;
3324 svm_complete_interrupts(svm);
3325}
3326
Avi Kivity80e31d42008-07-14 14:44:59 +03003327#ifdef CONFIG_X86_64
3328#define R "r"
3329#else
3330#define R "e"
3331#endif
3332
Avi Kivity851ba692009-08-24 11:10:17 +03003333static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003334{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003335 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +03003336
Joerg Roedel2041a062010-04-22 12:33:08 +02003337 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3338 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3339 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3340
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003341 /*
3342 * A vmexit emulation is required before the vcpu can be executed
3343 * again.
3344 */
3345 if (unlikely(svm->nested.exit_required))
3346 return;
3347
Rusty Russelle756fc62007-07-30 20:07:08 +10003348 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003349
Joerg Roedel649d6862008-04-16 16:51:15 +02003350 sync_lapic_to_cr8(vcpu);
3351
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003352 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003353
Avi Kivity04d2cc72007-09-10 18:10:54 +03003354 clgi();
3355
3356 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003357
Avi Kivity6aa8b732006-12-10 02:21:36 -08003358 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003359 "push %%"R"bp; \n\t"
3360 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3361 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3362 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3363 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3364 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3365 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003366#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003367 "mov %c[r8](%[svm]), %%r8 \n\t"
3368 "mov %c[r9](%[svm]), %%r9 \n\t"
3369 "mov %c[r10](%[svm]), %%r10 \n\t"
3370 "mov %c[r11](%[svm]), %%r11 \n\t"
3371 "mov %c[r12](%[svm]), %%r12 \n\t"
3372 "mov %c[r13](%[svm]), %%r13 \n\t"
3373 "mov %c[r14](%[svm]), %%r14 \n\t"
3374 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003375#endif
3376
Avi Kivity6aa8b732006-12-10 02:21:36 -08003377 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003378 "push %%"R"ax \n\t"
3379 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003380 __ex(SVM_VMLOAD) "\n\t"
3381 __ex(SVM_VMRUN) "\n\t"
3382 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003383 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003384
3385 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003386 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3387 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3388 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3389 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3390 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3391 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003392#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003393 "mov %%r8, %c[r8](%[svm]) \n\t"
3394 "mov %%r9, %c[r9](%[svm]) \n\t"
3395 "mov %%r10, %c[r10](%[svm]) \n\t"
3396 "mov %%r11, %c[r11](%[svm]) \n\t"
3397 "mov %%r12, %c[r12](%[svm]) \n\t"
3398 "mov %%r13, %c[r13](%[svm]) \n\t"
3399 "mov %%r14, %c[r14](%[svm]) \n\t"
3400 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003401#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003402 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003403 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003404 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003405 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003406 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3407 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3408 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3409 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3410 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3411 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003412#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003413 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3414 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3415 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3416 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3417 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3418 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3419 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3420 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003421#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003422 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003423 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003424#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003425 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3426#endif
3427 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003428
Avi Kivity6aa8b732006-12-10 02:21:36 -08003429 load_host_msrs(vcpu);
Avi Kivitydacccfd2010-10-21 12:20:33 +02003430#ifndef CONFIG_X86_64
3431 loadsegment(fs, svm->host.fs);
Avi Kivity9581d442010-10-19 16:46:55 +02003432#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08003433
3434 reload_tss(vcpu);
3435
Avi Kivity56ba47d2007-11-07 17:14:18 +02003436 local_irq_disable();
3437
3438 stgi();
3439
Avi Kivity13c34e02010-10-21 12:20:31 +02003440 vcpu->arch.cr2 = svm->vmcb->save.cr2;
3441 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3442 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3443 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
3444
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003445 sync_cr8_to_lapic(vcpu);
3446
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003447 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003448
Gleb Natapov631bc482010-10-14 11:22:52 +02003449 /* if exit due to PF check for async PF */
3450 if (svm->vmcb->control.exit_code == SVM_EXIT_EXCP_BASE + PF_VECTOR)
3451 svm->apf_reason = kvm_read_and_reset_pf_reason();
3452
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003453 if (npt_enabled) {
3454 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3455 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3456 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003457
3458 /*
3459 * We need to handle MC intercepts here before the vcpu has a chance to
3460 * change the physical cpu
3461 */
3462 if (unlikely(svm->vmcb->control.exit_code ==
3463 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3464 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003465}
3466
Avi Kivity80e31d42008-07-14 14:44:59 +03003467#undef R
3468
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3470{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003471 struct vcpu_svm *svm = to_svm(vcpu);
3472
3473 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 force_new_asid(vcpu);
3475}
3476
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003477static void set_tdp_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3478{
3479 struct vcpu_svm *svm = to_svm(vcpu);
3480
3481 svm->vmcb->control.nested_cr3 = root;
3482
3483 /* Also sync guest cr3 here in case we live migrate */
3484 svm->vmcb->save.cr3 = vcpu->arch.cr3;
3485
3486 force_new_asid(vcpu);
3487}
3488
Avi Kivity6aa8b732006-12-10 02:21:36 -08003489static int is_disabled(void)
3490{
Joerg Roedel6031a612007-06-22 12:29:50 +03003491 u64 vm_cr;
3492
3493 rdmsrl(MSR_VM_CR, vm_cr);
3494 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3495 return 1;
3496
Avi Kivity6aa8b732006-12-10 02:21:36 -08003497 return 0;
3498}
3499
Ingo Molnar102d8322007-02-19 14:37:47 +02003500static void
3501svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3502{
3503 /*
3504 * Patch in the VMMCALL instruction:
3505 */
3506 hypercall[0] = 0x0f;
3507 hypercall[1] = 0x01;
3508 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003509}
3510
Yang, Sheng002c7f72007-07-31 14:23:01 +03003511static void svm_check_processor_compat(void *rtn)
3512{
3513 *(int *)rtn = 0;
3514}
3515
Avi Kivity774ead32007-12-26 13:57:04 +02003516static bool svm_cpu_has_accelerated_tpr(void)
3517{
3518 return false;
3519}
3520
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003521static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003522{
3523 return 0;
3524}
3525
Sheng Yang0e851882009-12-18 16:48:46 +08003526static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3527{
3528}
3529
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003530static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3531{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003532 switch (func) {
Joerg Roedel24d1b152010-12-07 17:15:05 +01003533 case 0x00000001:
3534 /* Mask out xsave bit as long as it is not supported by SVM */
3535 entry->ecx &= ~(bit(X86_FEATURE_XSAVE));
3536 break;
Joerg Roedel4c62a2d2010-09-10 17:31:06 +02003537 case 0x80000001:
3538 if (nested)
3539 entry->ecx |= (1 << 2); /* Set SVM bit */
3540 break;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003541 case 0x8000000A:
3542 entry->eax = 1; /* SVM revision 1 */
3543 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3544 ASID emulation to nested SVM */
3545 entry->ecx = 0; /* Reserved */
Joerg Roedel7a190662010-07-27 18:14:21 +02003546 entry->edx = 0; /* Per default do not support any
3547 additional features */
3548
3549 /* Support next_rip if host supports it */
3550 if (svm_has(SVM_FEATURE_NRIP))
3551 entry->edx |= SVM_FEATURE_NRIP;
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003552
Joerg Roedel3d4aeaa2010-09-10 17:31:05 +02003553 /* Support NPT for the guest if enabled */
3554 if (npt_enabled)
3555 entry->edx |= SVM_FEATURE_NPT;
3556
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003557 break;
3558 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003559}
3560
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003561static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003562 { SVM_EXIT_READ_CR0, "read_cr0" },
3563 { SVM_EXIT_READ_CR3, "read_cr3" },
3564 { SVM_EXIT_READ_CR4, "read_cr4" },
3565 { SVM_EXIT_READ_CR8, "read_cr8" },
3566 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3567 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3568 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3569 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3570 { SVM_EXIT_READ_DR0, "read_dr0" },
3571 { SVM_EXIT_READ_DR1, "read_dr1" },
3572 { SVM_EXIT_READ_DR2, "read_dr2" },
3573 { SVM_EXIT_READ_DR3, "read_dr3" },
3574 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3575 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3576 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3577 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3578 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3579 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003580 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3581 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3582 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3583 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3584 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3585 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3586 { SVM_EXIT_INTR, "interrupt" },
3587 { SVM_EXIT_NMI, "nmi" },
3588 { SVM_EXIT_SMI, "smi" },
3589 { SVM_EXIT_INIT, "init" },
3590 { SVM_EXIT_VINTR, "vintr" },
3591 { SVM_EXIT_CPUID, "cpuid" },
3592 { SVM_EXIT_INVD, "invd" },
3593 { SVM_EXIT_HLT, "hlt" },
3594 { SVM_EXIT_INVLPG, "invlpg" },
3595 { SVM_EXIT_INVLPGA, "invlpga" },
3596 { SVM_EXIT_IOIO, "io" },
3597 { SVM_EXIT_MSR, "msr" },
3598 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3599 { SVM_EXIT_SHUTDOWN, "shutdown" },
3600 { SVM_EXIT_VMRUN, "vmrun" },
3601 { SVM_EXIT_VMMCALL, "hypercall" },
3602 { SVM_EXIT_VMLOAD, "vmload" },
3603 { SVM_EXIT_VMSAVE, "vmsave" },
3604 { SVM_EXIT_STGI, "stgi" },
3605 { SVM_EXIT_CLGI, "clgi" },
3606 { SVM_EXIT_SKINIT, "skinit" },
3607 { SVM_EXIT_WBINVD, "wbinvd" },
3608 { SVM_EXIT_MONITOR, "monitor" },
3609 { SVM_EXIT_MWAIT, "mwait" },
3610 { SVM_EXIT_NPF, "npf" },
3611 { -1, NULL }
3612};
3613
Sheng Yang17cc3932010-01-05 19:02:27 +08003614static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003615{
Sheng Yang17cc3932010-01-05 19:02:27 +08003616 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003617}
3618
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003619static bool svm_rdtscp_supported(void)
3620{
3621 return false;
3622}
3623
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003624static bool svm_has_wbinvd_exit(void)
3625{
3626 return true;
3627}
3628
Avi Kivity02daab22009-12-30 12:40:26 +02003629static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3630{
3631 struct vcpu_svm *svm = to_svm(vcpu);
3632
Avi Kivity02daab22009-12-30 12:40:26 +02003633 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003634 if (is_nested(svm))
3635 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3636 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003637}
3638
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003639static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003640 .cpu_has_kvm_support = has_svm,
3641 .disabled_by_bios = is_disabled,
3642 .hardware_setup = svm_hardware_setup,
3643 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003644 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003645 .hardware_enable = svm_hardware_enable,
3646 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003647 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003648
3649 .vcpu_create = svm_create_vcpu,
3650 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003651 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003652
Avi Kivity04d2cc72007-09-10 18:10:54 +03003653 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003654 .vcpu_load = svm_vcpu_load,
3655 .vcpu_put = svm_vcpu_put,
3656
3657 .set_guest_debug = svm_guest_debug,
3658 .get_msr = svm_get_msr,
3659 .set_msr = svm_set_msr,
3660 .get_segment_base = svm_get_segment_base,
3661 .get_segment = svm_get_segment,
3662 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003663 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003664 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003665 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003666 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003667 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003668 .set_cr3 = svm_set_cr3,
3669 .set_cr4 = svm_set_cr4,
3670 .set_efer = svm_set_efer,
3671 .get_idt = svm_get_idt,
3672 .set_idt = svm_set_idt,
3673 .get_gdt = svm_get_gdt,
3674 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003675 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003676 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003677 .get_rflags = svm_get_rflags,
3678 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003679 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003680 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003681
Avi Kivity6aa8b732006-12-10 02:21:36 -08003682 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003683
Avi Kivity6aa8b732006-12-10 02:21:36 -08003684 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003685 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003686 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003687 .set_interrupt_shadow = svm_set_interrupt_shadow,
3688 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003689 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003690 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003691 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003692 .queue_exception = svm_queue_exception,
Avi Kivityb463a6f2010-07-20 15:06:17 +03003693 .cancel_injection = svm_cancel_injection,
Gleb Natapov78646122009-03-23 12:12:11 +02003694 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003695 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003696 .get_nmi_mask = svm_get_nmi_mask,
3697 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003698 .enable_nmi_window = enable_nmi_window,
3699 .enable_irq_window = enable_irq_window,
3700 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003701
3702 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003703 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003704 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003705
3706 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003707 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003708
3709 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003710
3711 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003712
3713 .set_supported_cpuid = svm_set_supported_cpuid,
Sheng Yangf5f48ee2010-06-30 12:25:15 +08003714
3715 .has_wbinvd_exit = svm_has_wbinvd_exit,
Zachary Amsden99e3e302010-08-19 22:07:17 -10003716
3717 .write_tsc_offset = svm_write_tsc_offset,
Zachary Amsdene48672f2010-08-19 22:07:23 -10003718 .adjust_tsc_offset = svm_adjust_tsc_offset,
Joerg Roedel1c97f0a2010-09-10 17:30:41 +02003719
3720 .set_tdp_cr3 = set_tdp_cr3,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003721};
3722
3723static int __init svm_init(void)
3724{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003725 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003726 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003727}
3728
3729static void __exit svm_exit(void)
3730{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003731 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003732}
3733
3734module_init(svm_init)
3735module_exit(svm_exit)