blob: 134260c36ce2911c241544feeba464fb3159f33b [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.
7 *
8 * Authors:
9 * Yaniv Kamay <yaniv@qumranet.com>
10 * Avi Kivity <avi@qumranet.com>
11 *
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
14 *
15 */
Avi Kivityedf88412007-12-16 11:02:48 +020016#include <linux/kvm_host.h>
17
Eddie Dong85f455f2007-07-06 12:20:49 +030018#include "irq.h"
Zhang Xiantao1d737c82007-12-14 09:35:10 +080019#include "mmu.h"
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -030020#include "kvm_cache_regs.h"
Gleb Natapovfe4c7b12009-03-23 11:23:18 +020021#include "x86.h"
Avi Kivitye4956062007-06-28 14:15:57 -040022
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/module.h>
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +020024#include <linux/kernel.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080025#include <linux/vmalloc.h>
26#include <linux/highmem.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040027#include <linux/sched.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030028#include <linux/ftrace_event.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080030
Joerg Roedel67ec6602010-05-17 14:43:35 +020031#include <asm/tlbflush.h>
Avi Kivitye4956062007-06-28 14:15:57 -040032#include <asm/desc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080033
Eduardo Habkost63d11422008-11-17 19:03:20 -020034#include <asm/virtext.h>
Marcelo Tosatti229456f2009-06-17 09:22:14 -030035#include "trace.h"
Eduardo Habkost63d11422008-11-17 19:03:20 -020036
Avi Kivity4ecac3f2008-05-13 13:23:38 +030037#define __ex(x) __kvm_handle_fault_on_reboot(x)
38
Avi Kivity6aa8b732006-12-10 02:21:36 -080039MODULE_AUTHOR("Qumranet");
40MODULE_LICENSE("GPL");
41
42#define IOPM_ALLOC_ORDER 2
43#define MSRPM_ALLOC_ORDER 1
44
Avi Kivity6aa8b732006-12-10 02:21:36 -080045#define SEG_TYPE_LDT 2
46#define SEG_TYPE_BUSY_TSS16 3
47
Andre Przywara6bc31bd2010-04-11 23:07:28 +020048#define SVM_FEATURE_NPT (1 << 0)
49#define SVM_FEATURE_LBRV (1 << 1)
50#define SVM_FEATURE_SVML (1 << 2)
51#define SVM_FEATURE_NRIP (1 << 3)
52#define SVM_FEATURE_PAUSE_FILTER (1 << 10)
Joerg Roedel80b77062007-03-30 17:02:14 +030053
Joerg Roedel410e4d52009-08-07 11:49:44 +020054#define NESTED_EXIT_HOST 0 /* Exit handled on host level */
55#define NESTED_EXIT_DONE 1 /* Exit caused nested vmexit */
56#define NESTED_EXIT_CONTINUE 2 /* Further checks needed */
57
Joerg Roedel24e09cb2008-02-13 18:58:47 +010058#define DEBUGCTL_RESERVED_BITS (~(0x3fULL))
59
Joerg Roedel67ec6602010-05-17 14:43:35 +020060static bool erratum_383_found __read_mostly;
61
Avi Kivity6c8166a2009-05-31 18:15:37 +030062static const u32 host_save_user_msrs[] = {
63#ifdef CONFIG_X86_64
64 MSR_STAR, MSR_LSTAR, MSR_CSTAR, MSR_SYSCALL_MASK, MSR_KERNEL_GS_BASE,
65 MSR_FS_BASE,
66#endif
67 MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
68};
69
70#define NR_HOST_SAVE_USER_MSRS ARRAY_SIZE(host_save_user_msrs)
71
72struct kvm_vcpu;
73
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020074struct nested_state {
75 struct vmcb *hsave;
76 u64 hsave_msr;
Joerg Roedel4a810182010-02-24 18:59:15 +010077 u64 vm_cr_msr;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020078 u64 vmcb;
79
80 /* These are the merged vectors */
81 u32 *msrpm;
82
83 /* gpa pointers to the real vectors */
84 u64 vmcb_msrpm;
Joerg Roedelce2ac082010-03-01 15:34:39 +010085 u64 vmcb_iopm;
Joerg Roedelaad42c62009-08-07 11:49:34 +020086
Joerg Roedelcd3ff652009-10-09 16:08:26 +020087 /* A VMEXIT is required but not yet emulated */
88 bool exit_required;
89
Joerg Roedelaad42c62009-08-07 11:49:34 +020090 /* cache for intercepts of the guest */
91 u16 intercept_cr_read;
92 u16 intercept_cr_write;
93 u16 intercept_dr_read;
94 u16 intercept_dr_write;
95 u32 intercept_exceptions;
96 u64 intercept;
97
Joerg Roedele6aa9ab2009-08-07 11:49:33 +020098};
99
Joerg Roedel323c3d82010-03-01 15:34:37 +0100100#define MSRPM_OFFSETS 16
101static u32 msrpm_offsets[MSRPM_OFFSETS] __read_mostly;
102
Avi Kivity6c8166a2009-05-31 18:15:37 +0300103struct vcpu_svm {
104 struct kvm_vcpu vcpu;
105 struct vmcb *vmcb;
106 unsigned long vmcb_pa;
107 struct svm_cpu_data *svm_data;
108 uint64_t asid_generation;
109 uint64_t sysenter_esp;
110 uint64_t sysenter_eip;
111
112 u64 next_rip;
113
114 u64 host_user_msrs[NR_HOST_SAVE_USER_MSRS];
115 u64 host_gs_base;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300116
117 u32 *msrpm;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300118
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200119 struct nested_state nested;
Jan Kiszka6be7d302009-10-18 13:24:54 +0200120
121 bool nmi_singlestep;
Jan Kiszka66b71382010-02-23 17:47:56 +0100122
123 unsigned int3_injected;
124 unsigned long int3_rip;
Avi Kivity6c8166a2009-05-31 18:15:37 +0300125};
126
Joerg Roedel455716f2010-03-01 15:34:35 +0100127#define MSR_INVALID 0xffffffffU
128
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100129static struct svm_direct_access_msrs {
130 u32 index; /* Index of the MSR */
131 bool always; /* True if intercept is always on */
132} direct_access_msrs[] = {
133 { .index = MSR_K6_STAR, .always = true },
134 { .index = MSR_IA32_SYSENTER_CS, .always = true },
135#ifdef CONFIG_X86_64
136 { .index = MSR_GS_BASE, .always = true },
137 { .index = MSR_FS_BASE, .always = true },
138 { .index = MSR_KERNEL_GS_BASE, .always = true },
139 { .index = MSR_LSTAR, .always = true },
140 { .index = MSR_CSTAR, .always = true },
141 { .index = MSR_SYSCALL_MASK, .always = true },
142#endif
143 { .index = MSR_IA32_LASTBRANCHFROMIP, .always = false },
144 { .index = MSR_IA32_LASTBRANCHTOIP, .always = false },
145 { .index = MSR_IA32_LASTINTFROMIP, .always = false },
146 { .index = MSR_IA32_LASTINTTOIP, .always = false },
147 { .index = MSR_INVALID, .always = false },
Avi Kivity6aa8b732006-12-10 02:21:36 -0800148};
149
150/* enable NPT for AMD64 and X86 with PAE */
151#if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
152static bool npt_enabled = true;
153#else
Joerg Roedele0231712010-02-24 18:59:10 +0100154static bool npt_enabled;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800155#endif
156static int npt = 1;
157
158module_param(npt, int, S_IRUGO);
159
Joerg Roedel4b6e4dc2009-08-07 11:49:48 +0200160static int nested = 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800161module_param(nested, int, S_IRUGO);
162
163static void svm_flush_tlb(struct kvm_vcpu *vcpu);
Joerg Roedela5c38322009-08-07 11:49:32 +0200164static void svm_complete_interrupts(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800165
Joerg Roedel410e4d52009-08-07 11:49:44 +0200166static int nested_svm_exit_handled(struct vcpu_svm *svm);
Joerg Roedelb8e88bc2010-02-19 16:23:02 +0100167static int nested_svm_intercept(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800168static int nested_svm_vmexit(struct vcpu_svm *svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800169static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
170 bool has_error_code, u32 error_code);
171
172static inline struct vcpu_svm *to_svm(struct kvm_vcpu *vcpu)
173{
174 return container_of(vcpu, struct vcpu_svm, vcpu);
175}
176
177static inline bool is_nested(struct vcpu_svm *svm)
178{
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200179 return svm->nested.vmcb;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800180}
181
Joerg Roedel2af91942009-08-07 11:49:28 +0200182static inline void enable_gif(struct vcpu_svm *svm)
183{
184 svm->vcpu.arch.hflags |= HF_GIF_MASK;
185}
186
187static inline void disable_gif(struct vcpu_svm *svm)
188{
189 svm->vcpu.arch.hflags &= ~HF_GIF_MASK;
190}
191
192static inline bool gif_set(struct vcpu_svm *svm)
193{
194 return !!(svm->vcpu.arch.hflags & HF_GIF_MASK);
195}
196
Avi Kivity6aa8b732006-12-10 02:21:36 -0800197static unsigned long iopm_base;
198
199struct kvm_ldttss_desc {
200 u16 limit0;
201 u16 base0;
Joerg Roedele0231712010-02-24 18:59:10 +0100202 unsigned base1:8, type:5, dpl:2, p:1;
203 unsigned limit1:4, zero0:3, g:1, base2:8;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800204 u32 base3;
205 u32 zero1;
206} __attribute__((packed));
207
208struct svm_cpu_data {
209 int cpu;
210
Avi Kivity5008fdf2007-04-02 13:05:50 +0300211 u64 asid_generation;
212 u32 max_asid;
213 u32 next_asid;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800214 struct kvm_ldttss_desc *tss_desc;
215
216 struct page *save_area;
217};
218
219static DEFINE_PER_CPU(struct svm_cpu_data *, svm_data);
Joerg Roedel80b77062007-03-30 17:02:14 +0300220static uint32_t svm_features;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800221
222struct svm_init_data {
223 int cpu;
224 int r;
225};
226
227static u32 msrpm_ranges[] = {0, 0xc0000000, 0xc0010000};
228
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +0200229#define NUM_MSR_MAPS ARRAY_SIZE(msrpm_ranges)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800230#define MSRS_RANGE_SIZE 2048
231#define MSRS_IN_RANGE (MSRS_RANGE_SIZE * 8 / 2)
232
Joerg Roedel455716f2010-03-01 15:34:35 +0100233static u32 svm_msrpm_offset(u32 msr)
234{
235 u32 offset;
236 int i;
237
238 for (i = 0; i < NUM_MSR_MAPS; i++) {
239 if (msr < msrpm_ranges[i] ||
240 msr >= msrpm_ranges[i] + MSRS_IN_RANGE)
241 continue;
242
243 offset = (msr - msrpm_ranges[i]) / 4; /* 4 msrs per u8 */
244 offset += (i * MSRS_RANGE_SIZE); /* add range offset */
245
246 /* Now we have the u8 offset - but need the u32 offset */
247 return offset / 4;
248 }
249
250 /* MSR not in any range */
251 return MSR_INVALID;
252}
253
Avi Kivity6aa8b732006-12-10 02:21:36 -0800254#define MAX_INST_SIZE 15
255
Joerg Roedel80b77062007-03-30 17:02:14 +0300256static inline u32 svm_has(u32 feat)
257{
258 return svm_features & feat;
259}
260
Avi Kivity6aa8b732006-12-10 02:21:36 -0800261static inline void clgi(void)
262{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300263 asm volatile (__ex(SVM_CLGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800264}
265
266static inline void stgi(void)
267{
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300268 asm volatile (__ex(SVM_STGI));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800269}
270
271static inline void invlpga(unsigned long addr, u32 asid)
272{
Joerg Roedele0231712010-02-24 18:59:10 +0100273 asm volatile (__ex(SVM_INVLPGA) : : "a"(addr), "c"(asid));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800274}
275
Avi Kivity6aa8b732006-12-10 02:21:36 -0800276static inline void force_new_asid(struct kvm_vcpu *vcpu)
277{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400278 to_svm(vcpu)->asid_generation--;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800279}
280
281static inline void flush_guest_tlb(struct kvm_vcpu *vcpu)
282{
283 force_new_asid(vcpu);
284}
285
286static void svm_set_efer(struct kvm_vcpu *vcpu, u64 efer)
287{
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100288 if (!npt_enabled && !(efer & EFER_LMA))
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -0600289 efer &= ~EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800290
Alexander Graf9962d032008-11-25 20:17:02 +0100291 to_svm(vcpu)->vmcb->save.efer = efer | EFER_SVME;
Avi Kivityf6801df2010-01-21 15:31:50 +0200292 vcpu->arch.efer = efer;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800293}
294
Avi Kivity6aa8b732006-12-10 02:21:36 -0800295static int is_external_interrupt(u32 info)
296{
297 info &= SVM_EVTINJ_TYPE_MASK | SVM_EVTINJ_VALID;
298 return info == (SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR);
299}
300
Glauber Costa2809f5d2009-05-12 16:21:05 -0400301static u32 svm_get_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
302{
303 struct vcpu_svm *svm = to_svm(vcpu);
304 u32 ret = 0;
305
306 if (svm->vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK)
Jan Kiszka48005f62010-02-19 19:38:07 +0100307 ret |= KVM_X86_SHADOW_INT_STI | KVM_X86_SHADOW_INT_MOV_SS;
Glauber Costa2809f5d2009-05-12 16:21:05 -0400308 return ret & mask;
309}
310
311static void svm_set_interrupt_shadow(struct kvm_vcpu *vcpu, int mask)
312{
313 struct vcpu_svm *svm = to_svm(vcpu);
314
315 if (mask == 0)
316 svm->vmcb->control.int_state &= ~SVM_INTERRUPT_SHADOW_MASK;
317 else
318 svm->vmcb->control.int_state |= SVM_INTERRUPT_SHADOW_MASK;
319
320}
321
Avi Kivity6aa8b732006-12-10 02:21:36 -0800322static void skip_emulated_instruction(struct kvm_vcpu *vcpu)
323{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400324 struct vcpu_svm *svm = to_svm(vcpu);
325
Andre Przywara6bc31bd2010-04-11 23:07:28 +0200326 if (svm->vmcb->control.next_rip != 0)
327 svm->next_rip = svm->vmcb->control.next_rip;
328
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400329 if (!svm->next_rip) {
Avi Kivity851ba692009-08-24 11:10:17 +0300330 if (emulate_instruction(vcpu, 0, 0, EMULTYPE_SKIP) !=
Gleb Natapovf629cf82009-05-11 13:35:49 +0300331 EMULATE_DONE)
332 printk(KERN_DEBUG "%s: NOP\n", __func__);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800333 return;
334 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300335 if (svm->next_rip - kvm_rip_read(vcpu) > MAX_INST_SIZE)
336 printk(KERN_ERR "%s: ip 0x%lx next 0x%llx\n",
337 __func__, kvm_rip_read(vcpu), svm->next_rip);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800338
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300339 kvm_rip_write(vcpu, svm->next_rip);
Glauber Costa2809f5d2009-05-12 16:21:05 -0400340 svm_set_interrupt_shadow(vcpu, 0);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800341}
342
Jan Kiszka116a4752010-02-23 17:47:54 +0100343static void svm_queue_exception(struct kvm_vcpu *vcpu, unsigned nr,
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200344 bool has_error_code, u32 error_code,
345 bool reinject)
Jan Kiszka116a4752010-02-23 17:47:54 +0100346{
347 struct vcpu_svm *svm = to_svm(vcpu);
348
Joerg Roedele0231712010-02-24 18:59:10 +0100349 /*
350 * If we are within a nested VM we'd better #VMEXIT and let the guest
351 * handle the exception
352 */
Joerg Roedelce7ddec2010-04-22 12:33:13 +0200353 if (!reinject &&
354 nested_svm_check_exception(svm, nr, has_error_code, error_code))
Jan Kiszka116a4752010-02-23 17:47:54 +0100355 return;
356
Jan Kiszka66b71382010-02-23 17:47:56 +0100357 if (nr == BP_VECTOR && !svm_has(SVM_FEATURE_NRIP)) {
358 unsigned long rip, old_rip = kvm_rip_read(&svm->vcpu);
359
360 /*
361 * For guest debugging where we have to reinject #BP if some
362 * INT3 is guest-owned:
363 * Emulate nRIP by moving RIP forward. Will fail if injection
364 * raises a fault that is not intercepted. Still better than
365 * failing in all cases.
366 */
367 skip_emulated_instruction(&svm->vcpu);
368 rip = kvm_rip_read(&svm->vcpu);
369 svm->int3_rip = rip + svm->vmcb->save.cs.base;
370 svm->int3_injected = rip - old_rip;
371 }
372
Jan Kiszka116a4752010-02-23 17:47:54 +0100373 svm->vmcb->control.event_inj = nr
374 | SVM_EVTINJ_VALID
375 | (has_error_code ? SVM_EVTINJ_VALID_ERR : 0)
376 | SVM_EVTINJ_TYPE_EXEPT;
377 svm->vmcb->control.event_inj_err = error_code;
378}
379
Joerg Roedel67ec6602010-05-17 14:43:35 +0200380static void svm_init_erratum_383(void)
381{
382 u32 low, high;
383 int err;
384 u64 val;
385
386 /* Only Fam10h is affected */
387 if (boot_cpu_data.x86 != 0x10)
388 return;
389
390 /* Use _safe variants to not break nested virtualization */
391 val = native_read_msr_safe(MSR_AMD64_DC_CFG, &err);
392 if (err)
393 return;
394
395 val |= (1ULL << 47);
396
397 low = lower_32_bits(val);
398 high = upper_32_bits(val);
399
400 native_write_msr_safe(MSR_AMD64_DC_CFG, low, high);
401
402 erratum_383_found = true;
403}
404
Avi Kivity6aa8b732006-12-10 02:21:36 -0800405static int has_svm(void)
406{
Eduardo Habkost63d11422008-11-17 19:03:20 -0200407 const char *msg;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800408
Eduardo Habkost63d11422008-11-17 19:03:20 -0200409 if (!cpu_has_svm(&msg)) {
Joe Perchesff81ff12009-01-08 11:05:17 -0800410 printk(KERN_INFO "has_svm: %s\n", msg);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800411 return 0;
412 }
413
Avi Kivity6aa8b732006-12-10 02:21:36 -0800414 return 1;
415}
416
417static void svm_hardware_disable(void *garbage)
418{
Eduardo Habkost2c8dcee2008-11-17 19:03:21 -0200419 cpu_svm_disable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800420}
421
Alexander Graf10474ae2009-09-15 11:37:46 +0200422static int svm_hardware_enable(void *garbage)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800423{
424
Tejun Heo0fe1e002009-10-29 22:34:14 +0900425 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800426 uint64_t efer;
Gleb Natapov89a27f42010-02-16 10:51:48 +0200427 struct desc_ptr gdt_descr;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800428 struct desc_struct *gdt;
429 int me = raw_smp_processor_id();
430
Alexander Graf10474ae2009-09-15 11:37:46 +0200431 rdmsrl(MSR_EFER, efer);
432 if (efer & EFER_SVME)
433 return -EBUSY;
434
Avi Kivity6aa8b732006-12-10 02:21:36 -0800435 if (!has_svm()) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000436 printk(KERN_ERR "svm_hardware_enable: err EOPNOTSUPP on %d\n",
437 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200438 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800439 }
Tejun Heo0fe1e002009-10-29 22:34:14 +0900440 sd = per_cpu(svm_data, me);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800441
Tejun Heo0fe1e002009-10-29 22:34:14 +0900442 if (!sd) {
Zachary Amsdene6732a52009-09-29 11:38:36 -1000443 printk(KERN_ERR "svm_hardware_enable: svm_data is NULL on %d\n",
Avi Kivity6aa8b732006-12-10 02:21:36 -0800444 me);
Alexander Graf10474ae2009-09-15 11:37:46 +0200445 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800446 }
447
Tejun Heo0fe1e002009-10-29 22:34:14 +0900448 sd->asid_generation = 1;
449 sd->max_asid = cpuid_ebx(SVM_CPUID_FUNC) - 1;
450 sd->next_asid = sd->max_asid + 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800451
Gleb Natapovd6ab1ed2010-02-25 12:43:07 +0200452 native_store_gdt(&gdt_descr);
Gleb Natapov89a27f42010-02-16 10:51:48 +0200453 gdt = (struct desc_struct *)gdt_descr.address;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900454 sd->tss_desc = (struct kvm_ldttss_desc *)(gdt + GDT_ENTRY_TSS);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800455
Alexander Graf9962d032008-11-25 20:17:02 +0100456 wrmsrl(MSR_EFER, efer | EFER_SVME);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800457
Linus Torvaldsd0316552009-12-14 09:58:24 -0800458 wrmsrl(MSR_VM_HSAVE_PA, page_to_pfn(sd->save_area) << PAGE_SHIFT);
Alexander Graf10474ae2009-09-15 11:37:46 +0200459
Joerg Roedel67ec6602010-05-17 14:43:35 +0200460 svm_init_erratum_383();
461
Alexander Graf10474ae2009-09-15 11:37:46 +0200462 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800463}
464
Joerg Roedel0da1db752008-07-02 16:02:11 +0200465static void svm_cpu_uninit(int cpu)
466{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900467 struct svm_cpu_data *sd = per_cpu(svm_data, raw_smp_processor_id());
Joerg Roedel0da1db752008-07-02 16:02:11 +0200468
Tejun Heo0fe1e002009-10-29 22:34:14 +0900469 if (!sd)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200470 return;
471
472 per_cpu(svm_data, raw_smp_processor_id()) = NULL;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900473 __free_page(sd->save_area);
474 kfree(sd);
Joerg Roedel0da1db752008-07-02 16:02:11 +0200475}
476
Avi Kivity6aa8b732006-12-10 02:21:36 -0800477static int svm_cpu_init(int cpu)
478{
Tejun Heo0fe1e002009-10-29 22:34:14 +0900479 struct svm_cpu_data *sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800480 int r;
481
Tejun Heo0fe1e002009-10-29 22:34:14 +0900482 sd = kzalloc(sizeof(struct svm_cpu_data), GFP_KERNEL);
483 if (!sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800484 return -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900485 sd->cpu = cpu;
486 sd->save_area = alloc_page(GFP_KERNEL);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800487 r = -ENOMEM;
Tejun Heo0fe1e002009-10-29 22:34:14 +0900488 if (!sd->save_area)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800489 goto err_1;
490
Tejun Heo0fe1e002009-10-29 22:34:14 +0900491 per_cpu(svm_data, cpu) = sd;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800492
493 return 0;
494
495err_1:
Tejun Heo0fe1e002009-10-29 22:34:14 +0900496 kfree(sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800497 return r;
498
499}
500
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100501static bool valid_msr_intercept(u32 index)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800502{
503 int i;
504
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100505 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++)
506 if (direct_access_msrs[i].index == index)
507 return true;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800508
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100509 return false;
510}
511
Avi Kivity6aa8b732006-12-10 02:21:36 -0800512static void set_msr_interception(u32 *msrpm, unsigned msr,
513 int read, int write)
514{
Joerg Roedel455716f2010-03-01 15:34:35 +0100515 u8 bit_read, bit_write;
516 unsigned long tmp;
517 u32 offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800518
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100519 /*
520 * If this warning triggers extend the direct_access_msrs list at the
521 * beginning of the file
522 */
523 WARN_ON(!valid_msr_intercept(msr));
524
Joerg Roedel455716f2010-03-01 15:34:35 +0100525 offset = svm_msrpm_offset(msr);
526 bit_read = 2 * (msr & 0x0f);
527 bit_write = 2 * (msr & 0x0f) + 1;
528 tmp = msrpm[offset];
Avi Kivity6aa8b732006-12-10 02:21:36 -0800529
Joerg Roedel455716f2010-03-01 15:34:35 +0100530 BUG_ON(offset == MSR_INVALID);
531
532 read ? clear_bit(bit_read, &tmp) : set_bit(bit_read, &tmp);
533 write ? clear_bit(bit_write, &tmp) : set_bit(bit_write, &tmp);
534
535 msrpm[offset] = tmp;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800536}
537
Joerg Roedelf65c2292008-02-13 18:58:46 +0100538static void svm_vcpu_init_msrpm(u32 *msrpm)
539{
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100540 int i;
541
Joerg Roedelf65c2292008-02-13 18:58:46 +0100542 memset(msrpm, 0xff, PAGE_SIZE * (1 << MSRPM_ALLOC_ORDER));
543
Joerg Roedelac72a9b2010-03-01 15:34:36 +0100544 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
545 if (!direct_access_msrs[i].always)
546 continue;
547
548 set_msr_interception(msrpm, direct_access_msrs[i].index, 1, 1);
549 }
Joerg Roedelf65c2292008-02-13 18:58:46 +0100550}
551
Joerg Roedel323c3d82010-03-01 15:34:37 +0100552static void add_msr_offset(u32 offset)
553{
554 int i;
555
556 for (i = 0; i < MSRPM_OFFSETS; ++i) {
557
558 /* Offset already in list? */
559 if (msrpm_offsets[i] == offset)
560 return;
561
562 /* Slot used by another offset? */
563 if (msrpm_offsets[i] != MSR_INVALID)
564 continue;
565
566 /* Add offset to list */
567 msrpm_offsets[i] = offset;
568
569 return;
570 }
571
572 /*
573 * If this BUG triggers the msrpm_offsets table has an overflow. Just
574 * increase MSRPM_OFFSETS in this case.
575 */
576 BUG();
577}
578
579static void init_msrpm_offsets(void)
580{
581 int i;
582
583 memset(msrpm_offsets, 0xff, sizeof(msrpm_offsets));
584
585 for (i = 0; direct_access_msrs[i].index != MSR_INVALID; i++) {
586 u32 offset;
587
588 offset = svm_msrpm_offset(direct_access_msrs[i].index);
589 BUG_ON(offset == MSR_INVALID);
590
591 add_msr_offset(offset);
592 }
Avi Kivity6aa8b732006-12-10 02:21:36 -0800593}
594
Joerg Roedel24e09cb2008-02-13 18:58:47 +0100595static void svm_enable_lbrv(struct vcpu_svm *svm)
596{
597 u32 *msrpm = svm->msrpm;
598
599 svm->vmcb->control.lbr_ctl = 1;
600 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 1, 1);
601 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 1, 1);
602 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 1, 1);
603 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 1, 1);
604}
605
606static void svm_disable_lbrv(struct vcpu_svm *svm)
607{
608 u32 *msrpm = svm->msrpm;
609
610 svm->vmcb->control.lbr_ctl = 0;
611 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHFROMIP, 0, 0);
612 set_msr_interception(msrpm, MSR_IA32_LASTBRANCHTOIP, 0, 0);
613 set_msr_interception(msrpm, MSR_IA32_LASTINTFROMIP, 0, 0);
614 set_msr_interception(msrpm, MSR_IA32_LASTINTTOIP, 0, 0);
615}
616
Avi Kivity6aa8b732006-12-10 02:21:36 -0800617static __init int svm_hardware_setup(void)
618{
619 int cpu;
620 struct page *iopm_pages;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100621 void *iopm_va;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800622 int r;
623
Avi Kivity6aa8b732006-12-10 02:21:36 -0800624 iopm_pages = alloc_pages(GFP_KERNEL, IOPM_ALLOC_ORDER);
625
626 if (!iopm_pages)
627 return -ENOMEM;
Anthony Liguoric8681332007-04-30 09:48:11 +0300628
629 iopm_va = page_address(iopm_pages);
630 memset(iopm_va, 0xff, PAGE_SIZE * (1 << IOPM_ALLOC_ORDER));
Avi Kivity6aa8b732006-12-10 02:21:36 -0800631 iopm_base = page_to_pfn(iopm_pages) << PAGE_SHIFT;
632
Joerg Roedel323c3d82010-03-01 15:34:37 +0100633 init_msrpm_offsets();
634
Joerg Roedel50a37eb2008-01-31 14:57:38 +0100635 if (boot_cpu_has(X86_FEATURE_NX))
636 kvm_enable_efer_bits(EFER_NX);
637
Alexander Graf1b2fd702009-02-02 16:23:51 +0100638 if (boot_cpu_has(X86_FEATURE_FXSR_OPT))
639 kvm_enable_efer_bits(EFER_FFXSR);
640
Alexander Graf236de052008-11-25 20:17:10 +0100641 if (nested) {
642 printk(KERN_INFO "kvm: Nested Virtualization enabled\n");
Joerg Roedeleec4b142010-05-05 16:04:44 +0200643 kvm_enable_efer_bits(EFER_SVME | EFER_LMSLE);
Alexander Graf236de052008-11-25 20:17:10 +0100644 }
645
Zachary Amsden3230bb42009-09-29 11:38:37 -1000646 for_each_possible_cpu(cpu) {
Avi Kivity6aa8b732006-12-10 02:21:36 -0800647 r = svm_cpu_init(cpu);
648 if (r)
Joerg Roedelf65c2292008-02-13 18:58:46 +0100649 goto err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800650 }
Joerg Roedel33bd6a02008-02-07 13:47:38 +0100651
652 svm_features = cpuid_edx(SVM_CPUID_FUNC);
653
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100654 if (!svm_has(SVM_FEATURE_NPT))
655 npt_enabled = false;
656
Joerg Roedel6c7dac72008-02-07 13:47:40 +0100657 if (npt_enabled && !npt) {
658 printk(KERN_INFO "kvm: Nested Paging disabled\n");
659 npt_enabled = false;
660 }
661
Joerg Roedel18552672008-02-07 13:47:41 +0100662 if (npt_enabled) {
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100663 printk(KERN_INFO "kvm: Nested Paging enabled\n");
Joerg Roedel18552672008-02-07 13:47:41 +0100664 kvm_enable_tdp();
Joerg Roedel5f4cb662008-07-14 20:36:36 +0200665 } else
666 kvm_disable_tdp();
Joerg Roedele3da3ac2008-02-07 13:47:39 +0100667
Avi Kivity6aa8b732006-12-10 02:21:36 -0800668 return 0;
669
Joerg Roedelf65c2292008-02-13 18:58:46 +0100670err:
Avi Kivity6aa8b732006-12-10 02:21:36 -0800671 __free_pages(iopm_pages, IOPM_ALLOC_ORDER);
672 iopm_base = 0;
673 return r;
674}
675
676static __exit void svm_hardware_unsetup(void)
677{
Joerg Roedel0da1db752008-07-02 16:02:11 +0200678 int cpu;
679
Zachary Amsden3230bb42009-09-29 11:38:37 -1000680 for_each_possible_cpu(cpu)
Joerg Roedel0da1db752008-07-02 16:02:11 +0200681 svm_cpu_uninit(cpu);
682
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683 __free_pages(pfn_to_page(iopm_base >> PAGE_SHIFT), IOPM_ALLOC_ORDER);
Joerg Roedelf65c2292008-02-13 18:58:46 +0100684 iopm_base = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800685}
686
687static void init_seg(struct vmcb_seg *seg)
688{
689 seg->selector = 0;
690 seg->attrib = SVM_SELECTOR_P_MASK | SVM_SELECTOR_S_MASK |
Joerg Roedele0231712010-02-24 18:59:10 +0100691 SVM_SELECTOR_WRITE_MASK; /* Read/Write Data Segment */
Avi Kivity6aa8b732006-12-10 02:21:36 -0800692 seg->limit = 0xffff;
693 seg->base = 0;
694}
695
696static void init_sys_seg(struct vmcb_seg *seg, uint32_t type)
697{
698 seg->selector = 0;
699 seg->attrib = SVM_SELECTOR_P_MASK | type;
700 seg->limit = 0xffff;
701 seg->base = 0;
702}
703
Joerg Roedele6101a92008-02-13 18:58:45 +0100704static void init_vmcb(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800705{
Joerg Roedele6101a92008-02-13 18:58:45 +0100706 struct vmcb_control_area *control = &svm->vmcb->control;
707 struct vmcb_save_area *save = &svm->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800708
Avi Kivitybff78272010-01-07 13:16:08 +0200709 svm->vcpu.fpu_active = 1;
710
Joerg Roedele0231712010-02-24 18:59:10 +0100711 control->intercept_cr_read = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800712 INTERCEPT_CR3_MASK |
Joerg Roedel649d6862008-04-16 16:51:15 +0200713 INTERCEPT_CR4_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800714
Joerg Roedele0231712010-02-24 18:59:10 +0100715 control->intercept_cr_write = INTERCEPT_CR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800716 INTERCEPT_CR3_MASK |
Avi Kivity80a81192007-12-06 19:50:00 +0200717 INTERCEPT_CR4_MASK |
718 INTERCEPT_CR8_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800719
Joerg Roedele0231712010-02-24 18:59:10 +0100720 control->intercept_dr_read = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800721 INTERCEPT_DR1_MASK |
722 INTERCEPT_DR2_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100723 INTERCEPT_DR3_MASK |
724 INTERCEPT_DR4_MASK |
725 INTERCEPT_DR5_MASK |
726 INTERCEPT_DR6_MASK |
727 INTERCEPT_DR7_MASK;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800728
Joerg Roedele0231712010-02-24 18:59:10 +0100729 control->intercept_dr_write = INTERCEPT_DR0_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800730 INTERCEPT_DR1_MASK |
731 INTERCEPT_DR2_MASK |
732 INTERCEPT_DR3_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100733 INTERCEPT_DR4_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734 INTERCEPT_DR5_MASK |
Jan Kiszka727f5a22010-01-20 18:20:20 +0100735 INTERCEPT_DR6_MASK |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800736 INTERCEPT_DR7_MASK;
737
Anthony Liguori7aa81cc2007-09-17 14:57:50 -0500738 control->intercept_exceptions = (1 << PF_VECTOR) |
Joerg Roedel53371b52008-04-09 14:15:30 +0200739 (1 << UD_VECTOR) |
740 (1 << MC_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800741
742
Joerg Roedele0231712010-02-24 18:59:10 +0100743 control->intercept = (1ULL << INTERCEPT_INTR) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800744 (1ULL << INTERCEPT_NMI) |
Joerg Roedel01525272007-02-19 14:37:47 +0200745 (1ULL << INTERCEPT_SMI) |
Avi Kivityd2251572010-01-06 10:55:27 +0200746 (1ULL << INTERCEPT_SELECTIVE_CR0) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800747 (1ULL << INTERCEPT_CPUID) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200748 (1ULL << INTERCEPT_INVD) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800749 (1ULL << INTERCEPT_HLT) |
Marcelo Tosattia7052892008-09-23 13:18:35 -0300750 (1ULL << INTERCEPT_INVLPG) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800751 (1ULL << INTERCEPT_INVLPGA) |
752 (1ULL << INTERCEPT_IOIO_PROT) |
753 (1ULL << INTERCEPT_MSR_PROT) |
754 (1ULL << INTERCEPT_TASK_SWITCH) |
Joerg Roedel46fe4dd2007-01-26 00:56:42 -0800755 (1ULL << INTERCEPT_SHUTDOWN) |
Avi Kivity6aa8b732006-12-10 02:21:36 -0800756 (1ULL << INTERCEPT_VMRUN) |
757 (1ULL << INTERCEPT_VMMCALL) |
758 (1ULL << INTERCEPT_VMLOAD) |
759 (1ULL << INTERCEPT_VMSAVE) |
760 (1ULL << INTERCEPT_STGI) |
761 (1ULL << INTERCEPT_CLGI) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100762 (1ULL << INTERCEPT_SKINIT) |
Avi Kivitycf5a94d2007-10-28 16:11:58 +0200763 (1ULL << INTERCEPT_WBINVD) |
Joerg Roedel916ce232007-03-21 19:47:00 +0100764 (1ULL << INTERCEPT_MONITOR) |
765 (1ULL << INTERCEPT_MWAIT);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800766
767 control->iopm_base_pa = iopm_base;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100768 control->msrpm_base_pa = __pa(svm->msrpm);
Avi Kivity0cc50642007-03-25 12:07:27 +0200769 control->tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800770 control->int_ctl = V_INTR_MASKING_MASK;
771
772 init_seg(&save->es);
773 init_seg(&save->ss);
774 init_seg(&save->ds);
775 init_seg(&save->fs);
776 init_seg(&save->gs);
777
778 save->cs.selector = 0xf000;
779 /* Executable/Readable Code Segment */
780 save->cs.attrib = SVM_SELECTOR_READ_MASK | SVM_SELECTOR_P_MASK |
781 SVM_SELECTOR_S_MASK | SVM_SELECTOR_CODE_MASK;
782 save->cs.limit = 0xffff;
Avi Kivityd92899a2007-02-12 00:54:38 -0800783 /*
784 * cs.base should really be 0xffff0000, but vmx can't handle that, so
785 * be consistent with it.
786 *
787 * Replace when we have real mode working for vmx.
788 */
789 save->cs.base = 0xf0000;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800790
791 save->gdtr.limit = 0xffff;
792 save->idtr.limit = 0xffff;
793
794 init_sys_seg(&save->ldtr, SEG_TYPE_LDT);
795 init_sys_seg(&save->tr, SEG_TYPE_BUSY_TSS16);
796
Alexander Graf9962d032008-11-25 20:17:02 +0100797 save->efer = EFER_SVME;
Mike Dayd77c26f2007-10-08 09:02:08 -0400798 save->dr6 = 0xffff0ff0;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800799 save->dr7 = 0x400;
800 save->rflags = 2;
801 save->rip = 0x0000fff0;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300802 svm->vcpu.arch.regs[VCPU_REGS_RIP] = save->rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800803
Joerg Roedele0231712010-02-24 18:59:10 +0100804 /*
805 * This is the guest-visible cr0 value.
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200806 * svm_set_cr0() sets PG and WP and clears NW and CD on save->cr0.
Avi Kivity6aa8b732006-12-10 02:21:36 -0800807 */
Eduardo Habkost18fa0002009-10-24 02:49:59 -0200808 svm->vcpu.arch.cr0 = X86_CR0_NW | X86_CR0_CD | X86_CR0_ET;
809 kvm_set_cr0(&svm->vcpu, svm->vcpu.arch.cr0);
810
Rusty Russell66aee912007-07-17 23:34:16 +1000811 save->cr4 = X86_CR4_PAE;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800812 /* rdx = ?? */
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100813
814 if (npt_enabled) {
815 /* Setup VMCB for Nested Paging */
816 control->nested_ctl = 1;
Marcelo Tosattia7052892008-09-23 13:18:35 -0300817 control->intercept &= ~((1ULL << INTERCEPT_TASK_SWITCH) |
818 (1ULL << INTERCEPT_INVLPG));
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100819 control->intercept_exceptions &= ~(1 << PF_VECTOR);
Avi Kivity888f9f32010-01-10 12:14:04 +0200820 control->intercept_cr_read &= ~INTERCEPT_CR3_MASK;
821 control->intercept_cr_write &= ~INTERCEPT_CR3_MASK;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100822 save->g_pat = 0x0007040600070406ULL;
Joerg Roedel709ddeb2008-02-07 13:47:45 +0100823 save->cr3 = 0;
824 save->cr4 = 0;
825 }
Avi Kivitya79d2f12008-04-14 13:10:21 +0300826 force_new_asid(&svm->vcpu);
Alexander Graf1371d902008-11-25 20:17:04 +0100827
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200828 svm->nested.vmcb = 0;
Joerg Roedel2af91942009-08-07 11:49:28 +0200829 svm->vcpu.arch.hflags = 0;
830
Mark Langsdorf565d0992009-10-06 14:25:02 -0500831 if (svm_has(SVM_FEATURE_PAUSE_FILTER)) {
832 control->pause_filter_count = 3000;
833 control->intercept |= (1ULL << INTERCEPT_PAUSE);
834 }
835
Joerg Roedel2af91942009-08-07 11:49:28 +0200836 enable_gif(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800837}
838
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200839static int svm_vcpu_reset(struct kvm_vcpu *vcpu)
Avi Kivity04d2cc72007-09-10 18:10:54 +0300840{
841 struct vcpu_svm *svm = to_svm(vcpu);
842
Joerg Roedele6101a92008-02-13 18:58:45 +0100843 init_vmcb(svm);
Avi Kivity70433382007-11-07 12:57:23 +0200844
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300845 if (!kvm_vcpu_is_bsp(vcpu)) {
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300846 kvm_rip_write(vcpu, 0);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800847 svm->vmcb->save.cs.base = svm->vcpu.arch.sipi_vector << 12;
848 svm->vmcb->save.cs.selector = svm->vcpu.arch.sipi_vector << 8;
Avi Kivity70433382007-11-07 12:57:23 +0200849 }
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -0300850 vcpu->arch.regs_avail = ~0;
851 vcpu->arch.regs_dirty = ~0;
Avi Kivitye00c8cf2007-10-21 11:00:39 +0200852
853 return 0;
Avi Kivity04d2cc72007-09-10 18:10:54 +0300854}
855
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000856static struct kvm_vcpu *svm_create_vcpu(struct kvm *kvm, unsigned int id)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800857{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400858 struct vcpu_svm *svm;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800859 struct page *page;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100860 struct page *msrpm_pages;
Alexander Grafb286d5d2008-11-25 20:17:05 +0100861 struct page *hsave_page;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100862 struct page *nested_msrpm_pages;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000863 int err;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800864
Rusty Russellc16f8622007-07-30 21:12:19 +1000865 svm = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000866 if (!svm) {
867 err = -ENOMEM;
868 goto out;
869 }
870
871 err = kvm_vcpu_init(&svm->vcpu, kvm, id);
872 if (err)
873 goto free_svm;
874
Joerg Roedelf65c2292008-02-13 18:58:46 +0100875 err = -ENOMEM;
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900876 page = alloc_page(GFP_KERNEL);
877 if (!page)
878 goto uninit;
879
Joerg Roedelf65c2292008-02-13 18:58:46 +0100880 msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
881 if (!msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900882 goto free_page1;
Alexander Graf3d6368e2008-11-25 20:17:07 +0100883
884 nested_msrpm_pages = alloc_pages(GFP_KERNEL, MSRPM_ALLOC_ORDER);
885 if (!nested_msrpm_pages)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900886 goto free_page2;
Joerg Roedelf65c2292008-02-13 18:58:46 +0100887
Alexander Grafb286d5d2008-11-25 20:17:05 +0100888 hsave_page = alloc_page(GFP_KERNEL);
889 if (!hsave_page)
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900890 goto free_page3;
891
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200892 svm->nested.hsave = page_address(hsave_page);
Alexander Grafb286d5d2008-11-25 20:17:05 +0100893
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900894 svm->msrpm = page_address(msrpm_pages);
895 svm_vcpu_init_msrpm(svm->msrpm);
896
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200897 svm->nested.msrpm = page_address(nested_msrpm_pages);
Joerg Roedel323c3d82010-03-01 15:34:37 +0100898 svm_vcpu_init_msrpm(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +0100899
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400900 svm->vmcb = page_address(page);
901 clear_page(svm->vmcb);
902 svm->vmcb_pa = page_to_pfn(page) << PAGE_SHIFT;
903 svm->asid_generation = 0;
Joerg Roedele6101a92008-02-13 18:58:45 +0100904 init_vmcb(svm);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400905
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000906 fx_init(&svm->vcpu);
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800907 svm->vcpu.arch.apic_base = 0xfee00000 | MSR_IA32_APICBASE_ENABLE;
Gleb Natapovc5af89b2009-06-09 15:56:26 +0300908 if (kvm_vcpu_is_bsp(&svm->vcpu))
Zhang Xiantaoad312c72007-12-13 23:50:52 +0800909 svm->vcpu.arch.apic_base |= MSR_IA32_APICBASE_BSP;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800910
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000911 return &svm->vcpu;
Avi Kivity36241b82006-12-22 01:05:20 -0800912
Takuya Yoshikawab7af4042010-03-09 14:55:19 +0900913free_page3:
914 __free_pages(nested_msrpm_pages, MSRPM_ALLOC_ORDER);
915free_page2:
916 __free_pages(msrpm_pages, MSRPM_ALLOC_ORDER);
917free_page1:
918 __free_page(page);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000919uninit:
920 kvm_vcpu_uninit(&svm->vcpu);
921free_svm:
Rusty Russella4770342007-08-01 14:46:11 +1000922 kmem_cache_free(kvm_vcpu_cache, svm);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000923out:
924 return ERR_PTR(err);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800925}
926
927static void svm_free_vcpu(struct kvm_vcpu *vcpu)
928{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400929 struct vcpu_svm *svm = to_svm(vcpu);
930
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000931 __free_page(pfn_to_page(svm->vmcb_pa >> PAGE_SHIFT));
Joerg Roedelf65c2292008-02-13 18:58:46 +0100932 __free_pages(virt_to_page(svm->msrpm), MSRPM_ALLOC_ORDER);
Joerg Roedele6aa9ab2009-08-07 11:49:33 +0200933 __free_page(virt_to_page(svm->nested.hsave));
934 __free_pages(virt_to_page(svm->nested.msrpm), MSRPM_ALLOC_ORDER);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000935 kvm_vcpu_uninit(vcpu);
Rusty Russella4770342007-08-01 14:46:11 +1000936 kmem_cache_free(kvm_vcpu_cache, svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800937}
938
Avi Kivity15ad7142007-07-11 18:17:21 +0300939static void svm_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800940{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400941 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300942 int i;
Avi Kivity0cc50642007-03-25 12:07:27 +0200943
Avi Kivity0cc50642007-03-25 12:07:27 +0200944 if (unlikely(cpu != vcpu->cpu)) {
Joerg Roedele935d482009-09-16 15:24:19 +0200945 u64 delta;
Avi Kivity0cc50642007-03-25 12:07:27 +0200946
Joerg Roedel953899b2009-12-14 12:22:20 +0100947 if (check_tsc_unstable()) {
948 /*
949 * Make sure that the guest sees a monotonically
950 * increasing TSC.
951 */
952 delta = vcpu->arch.host_tsc - native_read_tsc();
953 svm->vmcb->control.tsc_offset += delta;
954 if (is_nested(svm))
955 svm->nested.hsave->control.tsc_offset += delta;
956 }
Avi Kivity0cc50642007-03-25 12:07:27 +0200957 vcpu->cpu = cpu;
Marcelo Tosatti2f599712008-05-27 12:10:20 -0300958 kvm_migrate_timers(vcpu);
Marcelo Tosatti4b656b12009-07-21 12:47:45 -0300959 svm->asid_generation = 0;
Avi Kivity0cc50642007-03-25 12:07:27 +0200960 }
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300961
962 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400963 rdmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800964}
965
966static void svm_vcpu_put(struct kvm_vcpu *vcpu)
967{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400968 struct vcpu_svm *svm = to_svm(vcpu);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300969 int i;
970
Avi Kivitye1beb1d2007-11-18 13:50:24 +0200971 ++vcpu->stat.host_state_reload;
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300972 for (i = 0; i < NR_HOST_SAVE_USER_MSRS; i++)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400973 wrmsrl(host_save_user_msrs[i], svm->host_user_msrs[i]);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +0300974
Joerg Roedele935d482009-09-16 15:24:19 +0200975 vcpu->arch.host_tsc = native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800976}
977
Avi Kivity6aa8b732006-12-10 02:21:36 -0800978static unsigned long svm_get_rflags(struct kvm_vcpu *vcpu)
979{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400980 return to_svm(vcpu)->vmcb->save.rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800981}
982
983static void svm_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
984{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -0400985 to_svm(vcpu)->vmcb->save.rflags = rflags;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800986}
987
Avi Kivity6de4f3a2009-05-31 22:58:47 +0300988static void svm_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
989{
990 switch (reg) {
991 case VCPU_EXREG_PDPTR:
992 BUG_ON(!npt_enabled);
993 load_pdptrs(vcpu, vcpu->arch.cr3);
994 break;
995 default:
996 BUG();
997 }
998}
999
Alexander Graff0b85052008-11-25 20:17:01 +01001000static void svm_set_vintr(struct vcpu_svm *svm)
1001{
1002 svm->vmcb->control.intercept |= 1ULL << INTERCEPT_VINTR;
1003}
1004
1005static void svm_clear_vintr(struct vcpu_svm *svm)
1006{
1007 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VINTR);
1008}
1009
Avi Kivity6aa8b732006-12-10 02:21:36 -08001010static struct vmcb_seg *svm_seg(struct kvm_vcpu *vcpu, int seg)
1011{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001012 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001013
1014 switch (seg) {
1015 case VCPU_SREG_CS: return &save->cs;
1016 case VCPU_SREG_DS: return &save->ds;
1017 case VCPU_SREG_ES: return &save->es;
1018 case VCPU_SREG_FS: return &save->fs;
1019 case VCPU_SREG_GS: return &save->gs;
1020 case VCPU_SREG_SS: return &save->ss;
1021 case VCPU_SREG_TR: return &save->tr;
1022 case VCPU_SREG_LDTR: return &save->ldtr;
1023 }
1024 BUG();
Al Viro8b6d44c2007-02-09 16:38:40 +00001025 return NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001026}
1027
1028static u64 svm_get_segment_base(struct kvm_vcpu *vcpu, int seg)
1029{
1030 struct vmcb_seg *s = svm_seg(vcpu, seg);
1031
1032 return s->base;
1033}
1034
1035static void svm_get_segment(struct kvm_vcpu *vcpu,
1036 struct kvm_segment *var, int seg)
1037{
1038 struct vmcb_seg *s = svm_seg(vcpu, seg);
1039
1040 var->base = s->base;
1041 var->limit = s->limit;
1042 var->selector = s->selector;
1043 var->type = s->attrib & SVM_SELECTOR_TYPE_MASK;
1044 var->s = (s->attrib >> SVM_SELECTOR_S_SHIFT) & 1;
1045 var->dpl = (s->attrib >> SVM_SELECTOR_DPL_SHIFT) & 3;
1046 var->present = (s->attrib >> SVM_SELECTOR_P_SHIFT) & 1;
1047 var->avl = (s->attrib >> SVM_SELECTOR_AVL_SHIFT) & 1;
1048 var->l = (s->attrib >> SVM_SELECTOR_L_SHIFT) & 1;
1049 var->db = (s->attrib >> SVM_SELECTOR_DB_SHIFT) & 1;
1050 var->g = (s->attrib >> SVM_SELECTOR_G_SHIFT) & 1;
Amit Shah25022ac2008-10-27 09:04:17 +00001051
Joerg Roedele0231712010-02-24 18:59:10 +01001052 /*
1053 * AMD's VMCB does not have an explicit unusable field, so emulate it
Andre Przywara19bca6a2009-04-28 12:45:30 +02001054 * for cross vendor migration purposes by "not present"
1055 */
1056 var->unusable = !var->present || (var->type == 0);
1057
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001058 switch (seg) {
1059 case VCPU_SREG_CS:
1060 /*
1061 * SVM always stores 0 for the 'G' bit in the CS selector in
1062 * the VMCB on a VMEXIT. This hurts cross-vendor migration:
1063 * Intel's VMENTRY has a check on the 'G' bit.
1064 */
Amit Shah25022ac2008-10-27 09:04:17 +00001065 var->g = s->limit > 0xfffff;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001066 break;
1067 case VCPU_SREG_TR:
1068 /*
1069 * Work around a bug where the busy flag in the tr selector
1070 * isn't exposed
1071 */
Amit Shahc0d09822008-10-27 09:04:18 +00001072 var->type |= 0x2;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001073 break;
1074 case VCPU_SREG_DS:
1075 case VCPU_SREG_ES:
1076 case VCPU_SREG_FS:
1077 case VCPU_SREG_GS:
1078 /*
1079 * The accessed bit must always be set in the segment
1080 * descriptor cache, although it can be cleared in the
1081 * descriptor, the cached bit always remains at 1. Since
1082 * Intel has a check on this, set it here to support
1083 * cross-vendor migration.
1084 */
1085 if (!var->unusable)
1086 var->type |= 0x1;
1087 break;
Andre Przywarab586eb02009-04-28 12:45:43 +02001088 case VCPU_SREG_SS:
Joerg Roedele0231712010-02-24 18:59:10 +01001089 /*
1090 * On AMD CPUs sometimes the DB bit in the segment
Andre Przywarab586eb02009-04-28 12:45:43 +02001091 * descriptor is left as 1, although the whole segment has
1092 * been made unusable. Clear it here to pass an Intel VMX
1093 * entry check when cross vendor migrating.
1094 */
1095 if (var->unusable)
1096 var->db = 0;
1097 break;
Andre Przywara1fbdc7a2009-01-11 22:39:44 +01001098 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001099}
1100
Izik Eidus2e4d2652008-03-24 19:38:34 +02001101static int svm_get_cpl(struct kvm_vcpu *vcpu)
1102{
1103 struct vmcb_save_area *save = &to_svm(vcpu)->vmcb->save;
1104
1105 return save->cpl;
1106}
1107
Gleb Natapov89a27f42010-02-16 10:51:48 +02001108static void svm_get_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001109{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001110 struct vcpu_svm *svm = to_svm(vcpu);
1111
Gleb Natapov89a27f42010-02-16 10:51:48 +02001112 dt->size = svm->vmcb->save.idtr.limit;
1113 dt->address = svm->vmcb->save.idtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001114}
1115
Gleb Natapov89a27f42010-02-16 10:51:48 +02001116static void svm_set_idt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001117{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001118 struct vcpu_svm *svm = to_svm(vcpu);
1119
Gleb Natapov89a27f42010-02-16 10:51:48 +02001120 svm->vmcb->save.idtr.limit = dt->size;
1121 svm->vmcb->save.idtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001122}
1123
Gleb Natapov89a27f42010-02-16 10:51:48 +02001124static void svm_get_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001125{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001126 struct vcpu_svm *svm = to_svm(vcpu);
1127
Gleb Natapov89a27f42010-02-16 10:51:48 +02001128 dt->size = svm->vmcb->save.gdtr.limit;
1129 dt->address = svm->vmcb->save.gdtr.base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001130}
1131
Gleb Natapov89a27f42010-02-16 10:51:48 +02001132static void svm_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001133{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001134 struct vcpu_svm *svm = to_svm(vcpu);
1135
Gleb Natapov89a27f42010-02-16 10:51:48 +02001136 svm->vmcb->save.gdtr.limit = dt->size;
1137 svm->vmcb->save.gdtr.base = dt->address ;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001138}
1139
Avi Kivitye8467fd2009-12-29 18:43:06 +02001140static void svm_decache_cr0_guest_bits(struct kvm_vcpu *vcpu)
1141{
1142}
1143
Anthony Liguori25c4c272007-04-27 09:29:21 +03001144static void svm_decache_cr4_guest_bits(struct kvm_vcpu *vcpu)
Avi Kivity399badf2007-01-05 16:36:38 -08001145{
1146}
1147
Avi Kivityd2251572010-01-06 10:55:27 +02001148static void update_cr0_intercept(struct vcpu_svm *svm)
1149{
Joerg Roedel66a562f2010-02-19 16:23:08 +01001150 struct vmcb *vmcb = svm->vmcb;
Avi Kivityd2251572010-01-06 10:55:27 +02001151 ulong gcr0 = svm->vcpu.arch.cr0;
1152 u64 *hcr0 = &svm->vmcb->save.cr0;
1153
1154 if (!svm->vcpu.fpu_active)
1155 *hcr0 |= SVM_CR0_SELECTIVE_MASK;
1156 else
1157 *hcr0 = (*hcr0 & ~SVM_CR0_SELECTIVE_MASK)
1158 | (gcr0 & SVM_CR0_SELECTIVE_MASK);
1159
1160
1161 if (gcr0 == *hcr0 && svm->vcpu.fpu_active) {
Joerg Roedel66a562f2010-02-19 16:23:08 +01001162 vmcb->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1163 vmcb->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1164 if (is_nested(svm)) {
1165 struct vmcb *hsave = svm->nested.hsave;
1166
1167 hsave->control.intercept_cr_read &= ~INTERCEPT_CR0_MASK;
1168 hsave->control.intercept_cr_write &= ~INTERCEPT_CR0_MASK;
1169 vmcb->control.intercept_cr_read |= svm->nested.intercept_cr_read;
1170 vmcb->control.intercept_cr_write |= svm->nested.intercept_cr_write;
1171 }
Avi Kivityd2251572010-01-06 10:55:27 +02001172 } else {
1173 svm->vmcb->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1174 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001175 if (is_nested(svm)) {
1176 struct vmcb *hsave = svm->nested.hsave;
1177
1178 hsave->control.intercept_cr_read |= INTERCEPT_CR0_MASK;
1179 hsave->control.intercept_cr_write |= INTERCEPT_CR0_MASK;
1180 }
Avi Kivityd2251572010-01-06 10:55:27 +02001181 }
1182}
1183
Avi Kivity6aa8b732006-12-10 02:21:36 -08001184static void svm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
1185{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001186 struct vcpu_svm *svm = to_svm(vcpu);
1187
Joerg Roedel7f5d8b52010-02-24 18:59:18 +01001188 if (is_nested(svm)) {
1189 /*
1190 * We are here because we run in nested mode, the host kvm
1191 * intercepts cr0 writes but the l1 hypervisor does not.
1192 * But the L1 hypervisor may intercept selective cr0 writes.
1193 * This needs to be checked here.
1194 */
1195 unsigned long old, new;
1196
1197 /* Remove bits that would trigger a real cr0 write intercept */
1198 old = vcpu->arch.cr0 & SVM_CR0_SELECTIVE_MASK;
1199 new = cr0 & SVM_CR0_SELECTIVE_MASK;
1200
1201 if (old == new) {
1202 /* cr0 write with ts and mp unchanged */
1203 svm->vmcb->control.exit_code = SVM_EXIT_CR0_SEL_WRITE;
1204 if (nested_svm_exit_handled(svm) == NESTED_EXIT_DONE)
1205 return;
1206 }
1207 }
1208
Avi Kivity05b3e0c2006-12-13 00:33:45 -08001209#ifdef CONFIG_X86_64
Avi Kivityf6801df2010-01-21 15:31:50 +02001210 if (vcpu->arch.efer & EFER_LME) {
Rusty Russell707d92fa2007-07-17 23:19:08 +10001211 if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001212 vcpu->arch.efer |= EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001213 svm->vmcb->save.efer |= EFER_LMA | EFER_LME;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214 }
1215
Mike Dayd77c26f2007-10-08 09:02:08 -04001216 if (is_paging(vcpu) && !(cr0 & X86_CR0_PG)) {
Avi Kivityf6801df2010-01-21 15:31:50 +02001217 vcpu->arch.efer &= ~EFER_LMA;
Carlo Marcelo Arenas Belon2b5203e2007-12-01 06:17:11 -06001218 svm->vmcb->save.efer &= ~(EFER_LMA | EFER_LME);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001219 }
1220 }
1221#endif
Zhang Xiantaoad312c72007-12-13 23:50:52 +08001222 vcpu->arch.cr0 = cr0;
Avi Kivity888f9f32010-01-10 12:14:04 +02001223
1224 if (!npt_enabled)
1225 cr0 |= X86_CR0_PG | X86_CR0_WP;
Avi Kivity02daab22009-12-30 12:40:26 +02001226
1227 if (!vcpu->fpu_active)
Joerg Roedel334df502008-01-21 13:09:33 +01001228 cr0 |= X86_CR0_TS;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01001229 /*
1230 * re-enable caching here because the QEMU bios
1231 * does not do it - this results in some delay at
1232 * reboot
1233 */
1234 cr0 &= ~(X86_CR0_CD | X86_CR0_NW);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001235 svm->vmcb->save.cr0 = cr0;
Avi Kivityd2251572010-01-06 10:55:27 +02001236 update_cr0_intercept(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237}
1238
1239static void svm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
1240{
Joerg Roedel6394b642008-04-09 14:15:29 +02001241 unsigned long host_cr4_mce = read_cr4() & X86_CR4_MCE;
Joerg Roedele5eab0c2008-09-09 19:11:51 +02001242 unsigned long old_cr4 = to_svm(vcpu)->vmcb->save.cr4;
1243
1244 if (npt_enabled && ((old_cr4 ^ cr4) & X86_CR4_PGE))
1245 force_new_asid(vcpu);
Joerg Roedel6394b642008-04-09 14:15:29 +02001246
Joerg Roedelec077262008-04-09 14:15:28 +02001247 vcpu->arch.cr4 = cr4;
1248 if (!npt_enabled)
1249 cr4 |= X86_CR4_PAE;
Joerg Roedel6394b642008-04-09 14:15:29 +02001250 cr4 |= host_cr4_mce;
Joerg Roedelec077262008-04-09 14:15:28 +02001251 to_svm(vcpu)->vmcb->save.cr4 = cr4;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001252}
1253
1254static void svm_set_segment(struct kvm_vcpu *vcpu,
1255 struct kvm_segment *var, int seg)
1256{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001257 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001258 struct vmcb_seg *s = svm_seg(vcpu, seg);
1259
1260 s->base = var->base;
1261 s->limit = var->limit;
1262 s->selector = var->selector;
1263 if (var->unusable)
1264 s->attrib = 0;
1265 else {
1266 s->attrib = (var->type & SVM_SELECTOR_TYPE_MASK);
1267 s->attrib |= (var->s & 1) << SVM_SELECTOR_S_SHIFT;
1268 s->attrib |= (var->dpl & 3) << SVM_SELECTOR_DPL_SHIFT;
1269 s->attrib |= (var->present & 1) << SVM_SELECTOR_P_SHIFT;
1270 s->attrib |= (var->avl & 1) << SVM_SELECTOR_AVL_SHIFT;
1271 s->attrib |= (var->l & 1) << SVM_SELECTOR_L_SHIFT;
1272 s->attrib |= (var->db & 1) << SVM_SELECTOR_DB_SHIFT;
1273 s->attrib |= (var->g & 1) << SVM_SELECTOR_G_SHIFT;
1274 }
1275 if (seg == VCPU_SREG_CS)
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001276 svm->vmcb->save.cpl
1277 = (svm->vmcb->save.cs.attrib
Avi Kivity6aa8b732006-12-10 02:21:36 -08001278 >> SVM_SELECTOR_DPL_SHIFT) & 3;
1279
1280}
1281
Gleb Natapov44c11432009-05-11 13:35:52 +03001282static void update_db_intercept(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001283{
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001284 struct vcpu_svm *svm = to_svm(vcpu);
1285
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001286 svm->vmcb->control.intercept_exceptions &=
1287 ~((1 << DB_VECTOR) | (1 << BP_VECTOR));
Gleb Natapov44c11432009-05-11 13:35:52 +03001288
Jan Kiszka6be7d302009-10-18 13:24:54 +02001289 if (svm->nmi_singlestep)
Gleb Natapov44c11432009-05-11 13:35:52 +03001290 svm->vmcb->control.intercept_exceptions |= (1 << DB_VECTOR);
1291
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001292 if (vcpu->guest_debug & KVM_GUESTDBG_ENABLE) {
1293 if (vcpu->guest_debug &
1294 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP))
1295 svm->vmcb->control.intercept_exceptions |=
1296 1 << DB_VECTOR;
1297 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP)
1298 svm->vmcb->control.intercept_exceptions |=
1299 1 << BP_VECTOR;
1300 } else
1301 vcpu->guest_debug = 0;
Gleb Natapov44c11432009-05-11 13:35:52 +03001302}
1303
Jan Kiszka355be0b2009-10-03 00:31:21 +02001304static void svm_guest_debug(struct kvm_vcpu *vcpu, struct kvm_guest_debug *dbg)
Gleb Natapov44c11432009-05-11 13:35:52 +03001305{
Gleb Natapov44c11432009-05-11 13:35:52 +03001306 struct vcpu_svm *svm = to_svm(vcpu);
1307
Jan Kiszkaae675ef2008-12-15 13:52:10 +01001308 if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW_BP)
1309 svm->vmcb->save.dr7 = dbg->arch.debugreg[7];
1310 else
1311 svm->vmcb->save.dr7 = vcpu->arch.dr7;
1312
Jan Kiszka355be0b2009-10-03 00:31:21 +02001313 update_db_intercept(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001314}
1315
1316static void load_host_msrs(struct kvm_vcpu *vcpu)
1317{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001318#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001319 wrmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001320#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321}
1322
1323static void save_host_msrs(struct kvm_vcpu *vcpu)
1324{
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001325#ifdef CONFIG_X86_64
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001326 rdmsrl(MSR_GS_BASE, to_svm(vcpu)->host_gs_base);
Anthony Liguori94dfbdb2007-04-29 11:56:06 +03001327#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -08001328}
1329
Tejun Heo0fe1e002009-10-29 22:34:14 +09001330static void new_asid(struct vcpu_svm *svm, struct svm_cpu_data *sd)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001331{
Tejun Heo0fe1e002009-10-29 22:34:14 +09001332 if (sd->next_asid > sd->max_asid) {
1333 ++sd->asid_generation;
1334 sd->next_asid = 1;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001335 svm->vmcb->control.tlb_ctl = TLB_CONTROL_FLUSH_ALL_ASID;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001336 }
1337
Tejun Heo0fe1e002009-10-29 22:34:14 +09001338 svm->asid_generation = sd->asid_generation;
1339 svm->vmcb->control.asid = sd->next_asid++;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001340}
1341
Gleb Natapov020df072010-04-13 10:05:23 +03001342static void svm_set_dr7(struct kvm_vcpu *vcpu, unsigned long value)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001343{
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001344 struct vcpu_svm *svm = to_svm(vcpu);
Jan Kiszka42dbaa52008-12-15 13:52:10 +01001345
Gleb Natapov020df072010-04-13 10:05:23 +03001346 svm->vmcb->save.dr7 = value;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001347}
1348
Avi Kivity851ba692009-08-24 11:10:17 +03001349static int pf_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001350{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001351 u64 fault_address;
1352 u32 error_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001353
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001354 fault_address = svm->vmcb->control.exit_info_2;
1355 error_code = svm->vmcb->control.exit_info_1;
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02001356
Marcelo Tosatti229456f2009-06-17 09:22:14 -03001357 trace_kvm_page_fault(fault_address, error_code);
Avi Kivity52c78472009-08-10 15:42:41 +03001358 if (!npt_enabled && kvm_event_needs_reinjection(&svm->vcpu))
1359 kvm_mmu_unprotect_page_virt(&svm->vcpu, fault_address);
Avi Kivity30677142007-10-28 18:48:59 +02001360 return kvm_mmu_page_fault(&svm->vcpu, fault_address, error_code);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001361}
1362
Avi Kivity851ba692009-08-24 11:10:17 +03001363static int db_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001364{
Avi Kivity851ba692009-08-24 11:10:17 +03001365 struct kvm_run *kvm_run = svm->vcpu.run;
1366
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001367 if (!(svm->vcpu.guest_debug &
Gleb Natapov44c11432009-05-11 13:35:52 +03001368 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) &&
Jan Kiszka6be7d302009-10-18 13:24:54 +02001369 !svm->nmi_singlestep) {
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001370 kvm_queue_exception(&svm->vcpu, DB_VECTOR);
1371 return 1;
1372 }
Gleb Natapov44c11432009-05-11 13:35:52 +03001373
Jan Kiszka6be7d302009-10-18 13:24:54 +02001374 if (svm->nmi_singlestep) {
1375 svm->nmi_singlestep = false;
Gleb Natapov44c11432009-05-11 13:35:52 +03001376 if (!(svm->vcpu.guest_debug & KVM_GUESTDBG_SINGLESTEP))
1377 svm->vmcb->save.rflags &=
1378 ~(X86_EFLAGS_TF | X86_EFLAGS_RF);
1379 update_db_intercept(&svm->vcpu);
1380 }
1381
1382 if (svm->vcpu.guest_debug &
Joerg Roedele0231712010-02-24 18:59:10 +01001383 (KVM_GUESTDBG_SINGLESTEP | KVM_GUESTDBG_USE_HW_BP)) {
Gleb Natapov44c11432009-05-11 13:35:52 +03001384 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1385 kvm_run->debug.arch.pc =
1386 svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1387 kvm_run->debug.arch.exception = DB_VECTOR;
1388 return 0;
1389 }
1390
1391 return 1;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001392}
1393
Avi Kivity851ba692009-08-24 11:10:17 +03001394static int bp_interception(struct vcpu_svm *svm)
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001395{
Avi Kivity851ba692009-08-24 11:10:17 +03001396 struct kvm_run *kvm_run = svm->vcpu.run;
1397
Jan Kiszkad0bfb942008-12-15 13:52:10 +01001398 kvm_run->exit_reason = KVM_EXIT_DEBUG;
1399 kvm_run->debug.arch.pc = svm->vmcb->save.cs.base + svm->vmcb->save.rip;
1400 kvm_run->debug.arch.exception = BP_VECTOR;
1401 return 0;
1402}
1403
Avi Kivity851ba692009-08-24 11:10:17 +03001404static int ud_interception(struct vcpu_svm *svm)
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001405{
1406 int er;
1407
Avi Kivity851ba692009-08-24 11:10:17 +03001408 er = emulate_instruction(&svm->vcpu, 0, 0, EMULTYPE_TRAP_UD);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001409 if (er != EMULATE_DONE)
Avi Kivity7ee5d9402007-11-25 15:22:50 +02001410 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001411 return 1;
1412}
1413
Avi Kivity6b52d182010-01-21 15:31:47 +02001414static void svm_fpu_activate(struct kvm_vcpu *vcpu)
Anthony Liguori7807fa62007-04-23 09:17:21 -05001415{
Avi Kivity6b52d182010-01-21 15:31:47 +02001416 struct vcpu_svm *svm = to_svm(vcpu);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001417 u32 excp;
1418
1419 if (is_nested(svm)) {
1420 u32 h_excp, n_excp;
1421
1422 h_excp = svm->nested.hsave->control.intercept_exceptions;
1423 n_excp = svm->nested.intercept_exceptions;
1424 h_excp &= ~(1 << NM_VECTOR);
1425 excp = h_excp | n_excp;
1426 } else {
1427 excp = svm->vmcb->control.intercept_exceptions;
Joerg Roedele0231712010-02-24 18:59:10 +01001428 excp &= ~(1 << NM_VECTOR);
Joerg Roedel66a562f2010-02-19 16:23:08 +01001429 }
1430
1431 svm->vmcb->control.intercept_exceptions = excp;
1432
Rusty Russelle756fc62007-07-30 20:07:08 +10001433 svm->vcpu.fpu_active = 1;
Avi Kivityd2251572010-01-06 10:55:27 +02001434 update_cr0_intercept(svm);
Avi Kivity6b52d182010-01-21 15:31:47 +02001435}
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001436
Avi Kivity6b52d182010-01-21 15:31:47 +02001437static int nm_interception(struct vcpu_svm *svm)
1438{
1439 svm_fpu_activate(&svm->vcpu);
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001440 return 1;
Anthony Liguori7807fa62007-04-23 09:17:21 -05001441}
1442
Joerg Roedel67ec6602010-05-17 14:43:35 +02001443static bool is_erratum_383(void)
1444{
1445 int err, i;
1446 u64 value;
1447
1448 if (!erratum_383_found)
1449 return false;
1450
1451 value = native_read_msr_safe(MSR_IA32_MC0_STATUS, &err);
1452 if (err)
1453 return false;
1454
1455 /* Bit 62 may or may not be set for this mce */
1456 value &= ~(1ULL << 62);
1457
1458 if (value != 0xb600000000010015ULL)
1459 return false;
1460
1461 /* Clear MCi_STATUS registers */
1462 for (i = 0; i < 6; ++i)
1463 native_write_msr_safe(MSR_IA32_MCx_STATUS(i), 0, 0);
1464
1465 value = native_read_msr_safe(MSR_IA32_MCG_STATUS, &err);
1466 if (!err) {
1467 u32 low, high;
1468
1469 value &= ~(1ULL << 2);
1470 low = lower_32_bits(value);
1471 high = upper_32_bits(value);
1472
1473 native_write_msr_safe(MSR_IA32_MCG_STATUS, low, high);
1474 }
1475
1476 /* Flush tlb to evict multi-match entries */
1477 __flush_tlb_all();
1478
1479 return true;
1480}
1481
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001482static void svm_handle_mce(struct vcpu_svm *svm)
Joerg Roedel53371b52008-04-09 14:15:30 +02001483{
Joerg Roedel67ec6602010-05-17 14:43:35 +02001484 if (is_erratum_383()) {
1485 /*
1486 * Erratum 383 triggered. Guest state is corrupt so kill the
1487 * guest.
1488 */
1489 pr_err("KVM: Guest triggered AMD Erratum 383\n");
1490
1491 set_bit(KVM_REQ_TRIPLE_FAULT, &svm->vcpu.requests);
1492
1493 return;
1494 }
1495
Joerg Roedel53371b52008-04-09 14:15:30 +02001496 /*
1497 * On an #MC intercept the MCE handler is not called automatically in
1498 * the host. So do it by hand here.
1499 */
1500 asm volatile (
1501 "int $0x12\n");
1502 /* not sure if we ever come back to this point */
1503
Joerg Roedelfe5913e2010-05-17 14:43:34 +02001504 return;
1505}
1506
1507static int mc_interception(struct vcpu_svm *svm)
1508{
Joerg Roedel53371b52008-04-09 14:15:30 +02001509 return 1;
1510}
1511
Avi Kivity851ba692009-08-24 11:10:17 +03001512static int shutdown_interception(struct vcpu_svm *svm)
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001513{
Avi Kivity851ba692009-08-24 11:10:17 +03001514 struct kvm_run *kvm_run = svm->vcpu.run;
1515
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001516 /*
1517 * VMCB is undefined after a SHUTDOWN intercept
1518 * so reinitialize it.
1519 */
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04001520 clear_page(svm->vmcb);
Joerg Roedele6101a92008-02-13 18:58:45 +01001521 init_vmcb(svm);
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08001522
1523 kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
1524 return 0;
1525}
1526
Avi Kivity851ba692009-08-24 11:10:17 +03001527static int io_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001528{
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001529 struct kvm_vcpu *vcpu = &svm->vcpu;
Mike Dayd77c26f2007-10-08 09:02:08 -04001530 u32 io_info = svm->vmcb->control.exit_info_1; /* address size bug? */
Jan Kiszka34c33d12009-02-08 13:28:15 +01001531 int size, in, string;
Avi Kivity039576c2007-03-20 12:46:50 +02001532 unsigned port;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001533
Rusty Russelle756fc62007-07-30 20:07:08 +10001534 ++svm->vcpu.stat.io_exits;
Laurent Viviere70669a2007-08-05 10:36:40 +03001535 string = (io_info & SVM_IOIO_STR_MASK) != 0;
Avi Kivity039576c2007-03-20 12:46:50 +02001536 in = (io_info & SVM_IOIO_TYPE_MASK) != 0;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001537 if (string || in)
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03001538 return emulate_instruction(vcpu, 0, 0, 0) == EMULATE_DONE;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001539
Avi Kivity039576c2007-03-20 12:46:50 +02001540 port = io_info >> 16;
1541 size = (io_info & SVM_IOIO_SIZE_MASK) >> SVM_IOIO_SIZE_SHIFT;
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001542 svm->next_rip = svm->vmcb->control.exit_info_2;
Guillaume Thouvenine93f36b2008-10-28 10:51:30 +01001543 skip_emulated_instruction(&svm->vcpu);
Gleb Natapovcf8f70b2010-03-18 15:20:23 +02001544
1545 return kvm_fast_pio_out(vcpu, size, port);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001546}
1547
Avi Kivity851ba692009-08-24 11:10:17 +03001548static int nmi_interception(struct vcpu_svm *svm)
Joerg Roedelc47f0982008-04-30 17:56:00 +02001549{
1550 return 1;
1551}
1552
Avi Kivity851ba692009-08-24 11:10:17 +03001553static int intr_interception(struct vcpu_svm *svm)
Joerg Roedela0698052008-04-30 17:56:01 +02001554{
1555 ++svm->vcpu.stat.irq_exits;
1556 return 1;
1557}
1558
Avi Kivity851ba692009-08-24 11:10:17 +03001559static int nop_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001560{
1561 return 1;
1562}
1563
Avi Kivity851ba692009-08-24 11:10:17 +03001564static int halt_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001565{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001566 svm->next_rip = kvm_rip_read(&svm->vcpu) + 1;
Rusty Russelle756fc62007-07-30 20:07:08 +10001567 skip_emulated_instruction(&svm->vcpu);
1568 return kvm_emulate_halt(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001569}
1570
Avi Kivity851ba692009-08-24 11:10:17 +03001571static int vmmcall_interception(struct vcpu_svm *svm)
Avi Kivity02e235b2007-02-19 14:37:47 +02001572{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03001573 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
Rusty Russelle756fc62007-07-30 20:07:08 +10001574 skip_emulated_instruction(&svm->vcpu);
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05001575 kvm_emulate_hypercall(&svm->vcpu);
1576 return 1;
Avi Kivity02e235b2007-02-19 14:37:47 +02001577}
1578
Alexander Grafc0725422008-11-25 20:17:03 +01001579static int nested_svm_check_permissions(struct vcpu_svm *svm)
1580{
Avi Kivityf6801df2010-01-21 15:31:50 +02001581 if (!(svm->vcpu.arch.efer & EFER_SVME)
Alexander Grafc0725422008-11-25 20:17:03 +01001582 || !is_paging(&svm->vcpu)) {
1583 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
1584 return 1;
1585 }
1586
1587 if (svm->vmcb->save.cpl) {
1588 kvm_inject_gp(&svm->vcpu, 0);
1589 return 1;
1590 }
1591
1592 return 0;
1593}
1594
Alexander Grafcf74a782008-11-25 20:17:08 +01001595static int nested_svm_check_exception(struct vcpu_svm *svm, unsigned nr,
1596 bool has_error_code, u32 error_code)
1597{
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001598 int vmexit;
1599
Joerg Roedel0295ad72009-08-07 11:49:37 +02001600 if (!is_nested(svm))
1601 return 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001602
Joerg Roedel0295ad72009-08-07 11:49:37 +02001603 svm->vmcb->control.exit_code = SVM_EXIT_EXCP_BASE + nr;
1604 svm->vmcb->control.exit_code_hi = 0;
1605 svm->vmcb->control.exit_info_1 = error_code;
1606 svm->vmcb->control.exit_info_2 = svm->vcpu.arch.cr2;
1607
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001608 vmexit = nested_svm_intercept(svm);
1609 if (vmexit == NESTED_EXIT_DONE)
1610 svm->nested.exit_required = true;
1611
1612 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001613}
1614
Joerg Roedel8fe54652010-02-19 16:23:01 +01001615/* This function returns true if it is save to enable the irq window */
1616static inline bool nested_svm_intr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001617{
Joerg Roedel26666952009-08-07 11:49:46 +02001618 if (!is_nested(svm))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001619 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001620
Joerg Roedel26666952009-08-07 11:49:46 +02001621 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001622 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001623
Joerg Roedel26666952009-08-07 11:49:46 +02001624 if (!(svm->vcpu.arch.hflags & HF_HIF_MASK))
Joerg Roedel8fe54652010-02-19 16:23:01 +01001625 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001626
Joerg Roedel197717d2010-02-24 18:59:19 +01001627 svm->vmcb->control.exit_code = SVM_EXIT_INTR;
1628 svm->vmcb->control.exit_info_1 = 0;
1629 svm->vmcb->control.exit_info_2 = 0;
Joerg Roedel26666952009-08-07 11:49:46 +02001630
Joerg Roedelcd3ff652009-10-09 16:08:26 +02001631 if (svm->nested.intercept & 1ULL) {
1632 /*
1633 * The #vmexit can't be emulated here directly because this
1634 * code path runs with irqs and preemtion disabled. A
1635 * #vmexit emulation might sleep. Only signal request for
1636 * the #vmexit here.
1637 */
1638 svm->nested.exit_required = true;
Joerg Roedel236649d2009-10-09 16:08:30 +02001639 trace_kvm_nested_intr_vmexit(svm->vmcb->save.rip);
Joerg Roedel8fe54652010-02-19 16:23:01 +01001640 return false;
Alexander Grafcf74a782008-11-25 20:17:08 +01001641 }
1642
Joerg Roedel8fe54652010-02-19 16:23:01 +01001643 return true;
Alexander Grafcf74a782008-11-25 20:17:08 +01001644}
1645
Joerg Roedel887f5002010-02-24 18:59:12 +01001646/* This function returns true if it is save to enable the nmi window */
1647static inline bool nested_svm_nmi(struct vcpu_svm *svm)
1648{
1649 if (!is_nested(svm))
1650 return true;
1651
1652 if (!(svm->nested.intercept & (1ULL << INTERCEPT_NMI)))
1653 return true;
1654
1655 svm->vmcb->control.exit_code = SVM_EXIT_NMI;
1656 svm->nested.exit_required = true;
1657
1658 return false;
1659}
1660
Joerg Roedel7597f122010-02-19 16:23:00 +01001661static void *nested_svm_map(struct vcpu_svm *svm, u64 gpa, struct page **_page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001662{
1663 struct page *page;
1664
Joerg Roedel6c3bd3d2010-02-19 16:23:04 +01001665 might_sleep();
1666
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001667 page = gfn_to_page(svm->vcpu.kvm, gpa >> PAGE_SHIFT);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001668 if (is_error_page(page))
1669 goto error;
1670
Joerg Roedel7597f122010-02-19 16:23:00 +01001671 *_page = page;
1672
1673 return kmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001674
1675error:
1676 kvm_release_page_clean(page);
1677 kvm_inject_gp(&svm->vcpu, 0);
1678
1679 return NULL;
1680}
1681
Joerg Roedel7597f122010-02-19 16:23:00 +01001682static void nested_svm_unmap(struct page *page)
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001683{
Joerg Roedel7597f122010-02-19 16:23:00 +01001684 kunmap(page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001685 kvm_release_page_dirty(page);
1686}
1687
Joerg Roedelce2ac082010-03-01 15:34:39 +01001688static int nested_svm_intercept_ioio(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001689{
Joerg Roedelce2ac082010-03-01 15:34:39 +01001690 unsigned port;
1691 u8 val, bit;
1692 u64 gpa;
1693
1694 if (!(svm->nested.intercept & (1ULL << INTERCEPT_IOIO_PROT)))
1695 return NESTED_EXIT_HOST;
1696
1697 port = svm->vmcb->control.exit_info_1 >> 16;
1698 gpa = svm->nested.vmcb_iopm + (port / 8);
1699 bit = port % 8;
1700 val = 0;
1701
1702 if (kvm_read_guest(svm->vcpu.kvm, gpa, &val, 1))
1703 val &= (1 << bit);
1704
1705 return val ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
1706}
1707
Joerg Roedeld2477822010-03-01 15:34:34 +01001708static int nested_svm_exit_handled_msr(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001709{
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001710 u32 offset, msr, value;
1711 int write, mask;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001712
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001713 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
Joerg Roedeld2477822010-03-01 15:34:34 +01001714 return NESTED_EXIT_HOST;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001715
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001716 msr = svm->vcpu.arch.regs[VCPU_REGS_RCX];
1717 offset = svm_msrpm_offset(msr);
1718 write = svm->vmcb->control.exit_info_1 & 1;
1719 mask = 1 << ((2 * (msr & 0xf)) + write);
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001720
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001721 if (offset == MSR_INVALID)
1722 return NESTED_EXIT_DONE;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001723
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001724 /* Offset is in 32 bit units but need in 8 bit units */
1725 offset *= 4;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001726
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001727 if (kvm_read_guest(svm->vcpu.kvm, svm->nested.vmcb_msrpm + offset, &value, 4))
1728 return NESTED_EXIT_DONE;
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001729
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001730 return (value & mask) ? NESTED_EXIT_DONE : NESTED_EXIT_HOST;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001731}
1732
Joerg Roedel410e4d52009-08-07 11:49:44 +02001733static int nested_svm_exit_special(struct vcpu_svm *svm)
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001734{
Alexander Grafcf74a782008-11-25 20:17:08 +01001735 u32 exit_code = svm->vmcb->control.exit_code;
Joerg Roedel4c2161a2009-08-07 11:49:35 +02001736
Joerg Roedel410e4d52009-08-07 11:49:44 +02001737 switch (exit_code) {
1738 case SVM_EXIT_INTR:
1739 case SVM_EXIT_NMI:
Joerg Roedelff47a492010-04-22 12:33:14 +02001740 case SVM_EXIT_EXCP_BASE + MC_VECTOR:
Joerg Roedel410e4d52009-08-07 11:49:44 +02001741 return NESTED_EXIT_HOST;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001742 case SVM_EXIT_NPF:
Joerg Roedele0231712010-02-24 18:59:10 +01001743 /* For now we are always handling NPFs when using them */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001744 if (npt_enabled)
1745 return NESTED_EXIT_HOST;
1746 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001747 case SVM_EXIT_EXCP_BASE + PF_VECTOR:
Joerg Roedele0231712010-02-24 18:59:10 +01001748 /* When we're shadowing, trap PFs */
Joerg Roedel410e4d52009-08-07 11:49:44 +02001749 if (!npt_enabled)
1750 return NESTED_EXIT_HOST;
1751 break;
Joerg Roedel66a562f2010-02-19 16:23:08 +01001752 case SVM_EXIT_EXCP_BASE + NM_VECTOR:
1753 nm_interception(svm);
1754 break;
Joerg Roedel410e4d52009-08-07 11:49:44 +02001755 default:
1756 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001757 }
1758
Joerg Roedel410e4d52009-08-07 11:49:44 +02001759 return NESTED_EXIT_CONTINUE;
1760}
1761
1762/*
1763 * If this function returns true, this #vmexit was already handled
1764 */
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001765static int nested_svm_intercept(struct vcpu_svm *svm)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001766{
1767 u32 exit_code = svm->vmcb->control.exit_code;
1768 int vmexit = NESTED_EXIT_HOST;
1769
Alexander Grafcf74a782008-11-25 20:17:08 +01001770 switch (exit_code) {
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001771 case SVM_EXIT_MSR:
Joerg Roedel3d62d9a2009-08-07 11:49:39 +02001772 vmexit = nested_svm_exit_handled_msr(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001773 break;
Joerg Roedelce2ac082010-03-01 15:34:39 +01001774 case SVM_EXIT_IOIO:
1775 vmexit = nested_svm_intercept_ioio(svm);
1776 break;
Alexander Grafcf74a782008-11-25 20:17:08 +01001777 case SVM_EXIT_READ_CR0 ... SVM_EXIT_READ_CR8: {
1778 u32 cr_bits = 1 << (exit_code - SVM_EXIT_READ_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001779 if (svm->nested.intercept_cr_read & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001780 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001781 break;
1782 }
1783 case SVM_EXIT_WRITE_CR0 ... SVM_EXIT_WRITE_CR8: {
1784 u32 cr_bits = 1 << (exit_code - SVM_EXIT_WRITE_CR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001785 if (svm->nested.intercept_cr_write & cr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001786 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001787 break;
1788 }
1789 case SVM_EXIT_READ_DR0 ... SVM_EXIT_READ_DR7: {
1790 u32 dr_bits = 1 << (exit_code - SVM_EXIT_READ_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001791 if (svm->nested.intercept_dr_read & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001792 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001793 break;
1794 }
1795 case SVM_EXIT_WRITE_DR0 ... SVM_EXIT_WRITE_DR7: {
1796 u32 dr_bits = 1 << (exit_code - SVM_EXIT_WRITE_DR0);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001797 if (svm->nested.intercept_dr_write & dr_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001798 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001799 break;
1800 }
1801 case SVM_EXIT_EXCP_BASE ... SVM_EXIT_EXCP_BASE + 0x1f: {
1802 u32 excp_bits = 1 << (exit_code - SVM_EXIT_EXCP_BASE);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001803 if (svm->nested.intercept_exceptions & excp_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001804 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001805 break;
1806 }
Joerg Roedel228070b2010-04-22 12:33:10 +02001807 case SVM_EXIT_ERR: {
1808 vmexit = NESTED_EXIT_DONE;
1809 break;
1810 }
Alexander Grafcf74a782008-11-25 20:17:08 +01001811 default: {
1812 u64 exit_bits = 1ULL << (exit_code - SVM_EXIT_INTR);
Joerg Roedelaad42c62009-08-07 11:49:34 +02001813 if (svm->nested.intercept & exit_bits)
Joerg Roedel410e4d52009-08-07 11:49:44 +02001814 vmexit = NESTED_EXIT_DONE;
Alexander Grafcf74a782008-11-25 20:17:08 +01001815 }
1816 }
1817
Joerg Roedelb8e88bc2010-02-19 16:23:02 +01001818 return vmexit;
1819}
1820
1821static int nested_svm_exit_handled(struct vcpu_svm *svm)
1822{
1823 int vmexit;
1824
1825 vmexit = nested_svm_intercept(svm);
1826
1827 if (vmexit == NESTED_EXIT_DONE)
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001828 nested_svm_vmexit(svm);
Joerg Roedel9c4e40b92009-08-07 11:49:36 +02001829
1830 return vmexit;
Alexander Grafcf74a782008-11-25 20:17:08 +01001831}
1832
Joerg Roedel0460a972009-08-07 11:49:31 +02001833static inline void copy_vmcb_control_area(struct vmcb *dst_vmcb, struct vmcb *from_vmcb)
1834{
1835 struct vmcb_control_area *dst = &dst_vmcb->control;
1836 struct vmcb_control_area *from = &from_vmcb->control;
1837
1838 dst->intercept_cr_read = from->intercept_cr_read;
1839 dst->intercept_cr_write = from->intercept_cr_write;
1840 dst->intercept_dr_read = from->intercept_dr_read;
1841 dst->intercept_dr_write = from->intercept_dr_write;
1842 dst->intercept_exceptions = from->intercept_exceptions;
1843 dst->intercept = from->intercept;
1844 dst->iopm_base_pa = from->iopm_base_pa;
1845 dst->msrpm_base_pa = from->msrpm_base_pa;
1846 dst->tsc_offset = from->tsc_offset;
1847 dst->asid = from->asid;
1848 dst->tlb_ctl = from->tlb_ctl;
1849 dst->int_ctl = from->int_ctl;
1850 dst->int_vector = from->int_vector;
1851 dst->int_state = from->int_state;
1852 dst->exit_code = from->exit_code;
1853 dst->exit_code_hi = from->exit_code_hi;
1854 dst->exit_info_1 = from->exit_info_1;
1855 dst->exit_info_2 = from->exit_info_2;
1856 dst->exit_int_info = from->exit_int_info;
1857 dst->exit_int_info_err = from->exit_int_info_err;
1858 dst->nested_ctl = from->nested_ctl;
1859 dst->event_inj = from->event_inj;
1860 dst->event_inj_err = from->event_inj_err;
1861 dst->nested_cr3 = from->nested_cr3;
1862 dst->lbr_ctl = from->lbr_ctl;
1863}
1864
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001865static int nested_svm_vmexit(struct vcpu_svm *svm)
Alexander Grafcf74a782008-11-25 20:17:08 +01001866{
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001867 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02001868 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedel33740e42009-08-07 11:49:29 +02001869 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01001870 struct page *page;
Alexander Grafcf74a782008-11-25 20:17:08 +01001871
Joerg Roedel17897f32009-10-09 16:08:29 +02001872 trace_kvm_nested_vmexit_inject(vmcb->control.exit_code,
1873 vmcb->control.exit_info_1,
1874 vmcb->control.exit_info_2,
1875 vmcb->control.exit_int_info,
1876 vmcb->control.exit_int_info_err);
1877
Joerg Roedel7597f122010-02-19 16:23:00 +01001878 nested_vmcb = nested_svm_map(svm, svm->nested.vmcb, &page);
Joerg Roedel34f80cf2009-08-07 11:49:38 +02001879 if (!nested_vmcb)
1880 return 1;
1881
Joerg Roedel06fc77722010-02-19 16:23:07 +01001882 /* Exit nested SVM mode */
1883 svm->nested.vmcb = 0;
1884
Alexander Grafcf74a782008-11-25 20:17:08 +01001885 /* Give the current vmcb to the guest */
Joerg Roedel33740e42009-08-07 11:49:29 +02001886 disable_gif(svm);
1887
1888 nested_vmcb->save.es = vmcb->save.es;
1889 nested_vmcb->save.cs = vmcb->save.cs;
1890 nested_vmcb->save.ss = vmcb->save.ss;
1891 nested_vmcb->save.ds = vmcb->save.ds;
1892 nested_vmcb->save.gdtr = vmcb->save.gdtr;
1893 nested_vmcb->save.idtr = vmcb->save.idtr;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001894 nested_vmcb->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedel2be4fc72010-04-22 12:33:09 +02001895 nested_vmcb->save.cr3 = svm->vcpu.arch.cr3;
Joerg Roedel33740e42009-08-07 11:49:29 +02001896 nested_vmcb->save.cr2 = vmcb->save.cr2;
Joerg Roedelcdbbdc12010-02-19 16:23:03 +01001897 nested_vmcb->save.cr4 = svm->vcpu.arch.cr4;
Joerg Roedel33740e42009-08-07 11:49:29 +02001898 nested_vmcb->save.rflags = vmcb->save.rflags;
1899 nested_vmcb->save.rip = vmcb->save.rip;
1900 nested_vmcb->save.rsp = vmcb->save.rsp;
1901 nested_vmcb->save.rax = vmcb->save.rax;
1902 nested_vmcb->save.dr7 = vmcb->save.dr7;
1903 nested_vmcb->save.dr6 = vmcb->save.dr6;
1904 nested_vmcb->save.cpl = vmcb->save.cpl;
1905
1906 nested_vmcb->control.int_ctl = vmcb->control.int_ctl;
1907 nested_vmcb->control.int_vector = vmcb->control.int_vector;
1908 nested_vmcb->control.int_state = vmcb->control.int_state;
1909 nested_vmcb->control.exit_code = vmcb->control.exit_code;
1910 nested_vmcb->control.exit_code_hi = vmcb->control.exit_code_hi;
1911 nested_vmcb->control.exit_info_1 = vmcb->control.exit_info_1;
1912 nested_vmcb->control.exit_info_2 = vmcb->control.exit_info_2;
1913 nested_vmcb->control.exit_int_info = vmcb->control.exit_int_info;
1914 nested_vmcb->control.exit_int_info_err = vmcb->control.exit_int_info_err;
Alexander Graf8d23c462009-10-09 16:08:25 +02001915
1916 /*
1917 * If we emulate a VMRUN/#VMEXIT in the same host #vmexit cycle we have
1918 * to make sure that we do not lose injected events. So check event_inj
1919 * here and copy it to exit_int_info if it is valid.
1920 * Exit_int_info and event_inj can't be both valid because the case
1921 * below only happens on a VMRUN instruction intercept which has
1922 * no valid exit_int_info set.
1923 */
1924 if (vmcb->control.event_inj & SVM_EVTINJ_VALID) {
1925 struct vmcb_control_area *nc = &nested_vmcb->control;
1926
1927 nc->exit_int_info = vmcb->control.event_inj;
1928 nc->exit_int_info_err = vmcb->control.event_inj_err;
1929 }
1930
Joerg Roedel33740e42009-08-07 11:49:29 +02001931 nested_vmcb->control.tlb_ctl = 0;
1932 nested_vmcb->control.event_inj = 0;
1933 nested_vmcb->control.event_inj_err = 0;
Alexander Grafcf74a782008-11-25 20:17:08 +01001934
1935 /* We always set V_INTR_MASKING and remember the old value in hflags */
1936 if (!(svm->vcpu.arch.hflags & HF_VINTR_MASK))
1937 nested_vmcb->control.int_ctl &= ~V_INTR_MASKING_MASK;
1938
Alexander Grafcf74a782008-11-25 20:17:08 +01001939 /* Restore the original control entries */
Joerg Roedel0460a972009-08-07 11:49:31 +02001940 copy_vmcb_control_area(vmcb, hsave);
Alexander Grafcf74a782008-11-25 20:17:08 +01001941
Alexander Graf219b65d2009-06-15 15:21:25 +02001942 kvm_clear_exception_queue(&svm->vcpu);
1943 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Grafcf74a782008-11-25 20:17:08 +01001944
1945 /* Restore selected save entries */
1946 svm->vmcb->save.es = hsave->save.es;
1947 svm->vmcb->save.cs = hsave->save.cs;
1948 svm->vmcb->save.ss = hsave->save.ss;
1949 svm->vmcb->save.ds = hsave->save.ds;
1950 svm->vmcb->save.gdtr = hsave->save.gdtr;
1951 svm->vmcb->save.idtr = hsave->save.idtr;
1952 svm->vmcb->save.rflags = hsave->save.rflags;
1953 svm_set_efer(&svm->vcpu, hsave->save.efer);
1954 svm_set_cr0(&svm->vcpu, hsave->save.cr0 | X86_CR0_PE);
1955 svm_set_cr4(&svm->vcpu, hsave->save.cr4);
1956 if (npt_enabled) {
1957 svm->vmcb->save.cr3 = hsave->save.cr3;
1958 svm->vcpu.arch.cr3 = hsave->save.cr3;
1959 } else {
1960 kvm_set_cr3(&svm->vcpu, hsave->save.cr3);
1961 }
1962 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, hsave->save.rax);
1963 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, hsave->save.rsp);
1964 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, hsave->save.rip);
1965 svm->vmcb->save.dr7 = 0;
1966 svm->vmcb->save.cpl = 0;
1967 svm->vmcb->control.exit_int_info = 0;
1968
Joerg Roedel7597f122010-02-19 16:23:00 +01001969 nested_svm_unmap(page);
Alexander Grafcf74a782008-11-25 20:17:08 +01001970
1971 kvm_mmu_reset_context(&svm->vcpu);
1972 kvm_mmu_load(&svm->vcpu);
1973
1974 return 0;
1975}
Alexander Graf3d6368e2008-11-25 20:17:07 +01001976
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001977static bool nested_svm_vmrun_msrpm(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01001978{
Joerg Roedel323c3d82010-03-01 15:34:37 +01001979 /*
1980 * This function merges the msr permission bitmaps of kvm and the
1981 * nested vmcb. It is omptimized in that it only merges the parts where
1982 * the kvm msr permission bitmap may contain zero bits
1983 */
Alexander Graf3d6368e2008-11-25 20:17:07 +01001984 int i;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001985
Joerg Roedel323c3d82010-03-01 15:34:37 +01001986 if (!(svm->nested.intercept & (1ULL << INTERCEPT_MSR_PROT)))
1987 return true;
Joerg Roedel9738b2c2009-08-07 11:49:41 +02001988
Joerg Roedel323c3d82010-03-01 15:34:37 +01001989 for (i = 0; i < MSRPM_OFFSETS; i++) {
1990 u32 value, p;
1991 u64 offset;
1992
1993 if (msrpm_offsets[i] == 0xffffffff)
1994 break;
1995
Joerg Roedel0d6b3532010-03-01 15:34:38 +01001996 p = msrpm_offsets[i];
1997 offset = svm->nested.vmcb_msrpm + (p * 4);
Joerg Roedel323c3d82010-03-01 15:34:37 +01001998
1999 if (kvm_read_guest(svm->vcpu.kvm, offset, &value, 4))
2000 return false;
2001
2002 svm->nested.msrpm[p] = svm->msrpm[p] | value;
2003 }
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002004
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002005 svm->vmcb->control.msrpm_base_pa = __pa(svm->nested.msrpm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002006
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002007 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002008}
2009
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002010static bool nested_svm_vmrun(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002011{
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002012 struct vmcb *nested_vmcb;
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002013 struct vmcb *hsave = svm->nested.hsave;
Joerg Roedeldefbba52009-08-07 11:49:30 +02002014 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002015 struct page *page;
Joerg Roedel06fc77722010-02-19 16:23:07 +01002016 u64 vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002017
Joerg Roedel06fc77722010-02-19 16:23:07 +01002018 vmcb_gpa = svm->vmcb->save.rax;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002019
Joerg Roedel7597f122010-02-19 16:23:00 +01002020 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002021 if (!nested_vmcb)
2022 return false;
2023
Joerg Roedelecf14052010-02-24 18:59:13 +01002024 trace_kvm_nested_vmrun(svm->vmcb->save.rip - 3, vmcb_gpa,
Joerg Roedel0ac406d2009-10-09 16:08:27 +02002025 nested_vmcb->save.rip,
2026 nested_vmcb->control.int_ctl,
2027 nested_vmcb->control.event_inj,
2028 nested_vmcb->control.nested_ctl);
2029
Joerg Roedel2e554e82010-02-24 18:59:14 +01002030 trace_kvm_nested_intercepts(nested_vmcb->control.intercept_cr_read,
2031 nested_vmcb->control.intercept_cr_write,
2032 nested_vmcb->control.intercept_exceptions,
2033 nested_vmcb->control.intercept);
2034
Alexander Graf3d6368e2008-11-25 20:17:07 +01002035 /* Clear internal status */
Alexander Graf219b65d2009-06-15 15:21:25 +02002036 kvm_clear_exception_queue(&svm->vcpu);
2037 kvm_clear_interrupt_queue(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002038
Joerg Roedele0231712010-02-24 18:59:10 +01002039 /*
2040 * Save the old vmcb, so we don't need to pick what we save, but can
2041 * restore everything when a VMEXIT occurs
2042 */
Joerg Roedeldefbba52009-08-07 11:49:30 +02002043 hsave->save.es = vmcb->save.es;
2044 hsave->save.cs = vmcb->save.cs;
2045 hsave->save.ss = vmcb->save.ss;
2046 hsave->save.ds = vmcb->save.ds;
2047 hsave->save.gdtr = vmcb->save.gdtr;
2048 hsave->save.idtr = vmcb->save.idtr;
Avi Kivityf6801df2010-01-21 15:31:50 +02002049 hsave->save.efer = svm->vcpu.arch.efer;
Avi Kivity4d4ec082009-12-29 18:07:30 +02002050 hsave->save.cr0 = kvm_read_cr0(&svm->vcpu);
Joerg Roedeldefbba52009-08-07 11:49:30 +02002051 hsave->save.cr4 = svm->vcpu.arch.cr4;
2052 hsave->save.rflags = vmcb->save.rflags;
2053 hsave->save.rip = svm->next_rip;
2054 hsave->save.rsp = vmcb->save.rsp;
2055 hsave->save.rax = vmcb->save.rax;
2056 if (npt_enabled)
2057 hsave->save.cr3 = vmcb->save.cr3;
2058 else
2059 hsave->save.cr3 = svm->vcpu.arch.cr3;
2060
Joerg Roedel0460a972009-08-07 11:49:31 +02002061 copy_vmcb_control_area(hsave, vmcb);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002062
2063 if (svm->vmcb->save.rflags & X86_EFLAGS_IF)
2064 svm->vcpu.arch.hflags |= HF_HIF_MASK;
2065 else
2066 svm->vcpu.arch.hflags &= ~HF_HIF_MASK;
2067
2068 /* Load the nested guest state */
2069 svm->vmcb->save.es = nested_vmcb->save.es;
2070 svm->vmcb->save.cs = nested_vmcb->save.cs;
2071 svm->vmcb->save.ss = nested_vmcb->save.ss;
2072 svm->vmcb->save.ds = nested_vmcb->save.ds;
2073 svm->vmcb->save.gdtr = nested_vmcb->save.gdtr;
2074 svm->vmcb->save.idtr = nested_vmcb->save.idtr;
2075 svm->vmcb->save.rflags = nested_vmcb->save.rflags;
2076 svm_set_efer(&svm->vcpu, nested_vmcb->save.efer);
2077 svm_set_cr0(&svm->vcpu, nested_vmcb->save.cr0);
2078 svm_set_cr4(&svm->vcpu, nested_vmcb->save.cr4);
2079 if (npt_enabled) {
2080 svm->vmcb->save.cr3 = nested_vmcb->save.cr3;
2081 svm->vcpu.arch.cr3 = nested_vmcb->save.cr3;
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002082 } else
Alexander Graf3d6368e2008-11-25 20:17:07 +01002083 kvm_set_cr3(&svm->vcpu, nested_vmcb->save.cr3);
Joerg Roedel0e5cbe32010-02-24 18:59:11 +01002084
2085 /* Guest paging mode is active - reset mmu */
2086 kvm_mmu_reset_context(&svm->vcpu);
2087
Joerg Roedeldefbba52009-08-07 11:49:30 +02002088 svm->vmcb->save.cr2 = svm->vcpu.arch.cr2 = nested_vmcb->save.cr2;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002089 kvm_register_write(&svm->vcpu, VCPU_REGS_RAX, nested_vmcb->save.rax);
2090 kvm_register_write(&svm->vcpu, VCPU_REGS_RSP, nested_vmcb->save.rsp);
2091 kvm_register_write(&svm->vcpu, VCPU_REGS_RIP, nested_vmcb->save.rip);
Joerg Roedele0231712010-02-24 18:59:10 +01002092
Alexander Graf3d6368e2008-11-25 20:17:07 +01002093 /* In case we don't even reach vcpu_run, the fields are not updated */
2094 svm->vmcb->save.rax = nested_vmcb->save.rax;
2095 svm->vmcb->save.rsp = nested_vmcb->save.rsp;
2096 svm->vmcb->save.rip = nested_vmcb->save.rip;
2097 svm->vmcb->save.dr7 = nested_vmcb->save.dr7;
2098 svm->vmcb->save.dr6 = nested_vmcb->save.dr6;
2099 svm->vmcb->save.cpl = nested_vmcb->save.cpl;
2100
Joerg Roedelf7138532010-03-01 15:34:40 +01002101 svm->nested.vmcb_msrpm = nested_vmcb->control.msrpm_base_pa & ~0x0fffULL;
Joerg Roedelce2ac082010-03-01 15:34:39 +01002102 svm->nested.vmcb_iopm = nested_vmcb->control.iopm_base_pa & ~0x0fffULL;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002103
Joerg Roedelaad42c62009-08-07 11:49:34 +02002104 /* cache intercepts */
2105 svm->nested.intercept_cr_read = nested_vmcb->control.intercept_cr_read;
2106 svm->nested.intercept_cr_write = nested_vmcb->control.intercept_cr_write;
2107 svm->nested.intercept_dr_read = nested_vmcb->control.intercept_dr_read;
2108 svm->nested.intercept_dr_write = nested_vmcb->control.intercept_dr_write;
2109 svm->nested.intercept_exceptions = nested_vmcb->control.intercept_exceptions;
2110 svm->nested.intercept = nested_vmcb->control.intercept;
2111
Alexander Graf3d6368e2008-11-25 20:17:07 +01002112 force_new_asid(&svm->vcpu);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002113 svm->vmcb->control.int_ctl = nested_vmcb->control.int_ctl | V_INTR_MASKING_MASK;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002114 if (nested_vmcb->control.int_ctl & V_INTR_MASKING_MASK)
2115 svm->vcpu.arch.hflags |= HF_VINTR_MASK;
2116 else
2117 svm->vcpu.arch.hflags &= ~HF_VINTR_MASK;
2118
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002119 if (svm->vcpu.arch.hflags & HF_VINTR_MASK) {
2120 /* We only want the cr8 intercept bits of the guest */
2121 svm->vmcb->control.intercept_cr_read &= ~INTERCEPT_CR8_MASK;
2122 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
2123 }
2124
Joerg Roedel0d945bd2010-05-05 16:04:45 +02002125 /* We don't want to see VMMCALLs from a nested guest */
2126 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_VMMCALL);
2127
Joerg Roedele0231712010-02-24 18:59:10 +01002128 /*
2129 * We don't want a nested guest to be more powerful than the guest, so
2130 * all intercepts are ORed
2131 */
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002132 svm->vmcb->control.intercept_cr_read |=
2133 nested_vmcb->control.intercept_cr_read;
2134 svm->vmcb->control.intercept_cr_write |=
2135 nested_vmcb->control.intercept_cr_write;
2136 svm->vmcb->control.intercept_dr_read |=
2137 nested_vmcb->control.intercept_dr_read;
2138 svm->vmcb->control.intercept_dr_write |=
2139 nested_vmcb->control.intercept_dr_write;
2140 svm->vmcb->control.intercept_exceptions |=
2141 nested_vmcb->control.intercept_exceptions;
2142
2143 svm->vmcb->control.intercept |= nested_vmcb->control.intercept;
2144
2145 svm->vmcb->control.lbr_ctl = nested_vmcb->control.lbr_ctl;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002146 svm->vmcb->control.int_vector = nested_vmcb->control.int_vector;
2147 svm->vmcb->control.int_state = nested_vmcb->control.int_state;
2148 svm->vmcb->control.tsc_offset += nested_vmcb->control.tsc_offset;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002149 svm->vmcb->control.event_inj = nested_vmcb->control.event_inj;
2150 svm->vmcb->control.event_inj_err = nested_vmcb->control.event_inj_err;
2151
Joerg Roedel7597f122010-02-19 16:23:00 +01002152 nested_svm_unmap(page);
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002153
Joerg Roedel06fc77722010-02-19 16:23:07 +01002154 /* nested_vmcb is our indicator if nested SVM is activated */
2155 svm->nested.vmcb = vmcb_gpa;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002156
Joerg Roedel2af91942009-08-07 11:49:28 +02002157 enable_gif(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002158
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002159 return true;
Alexander Graf3d6368e2008-11-25 20:17:07 +01002160}
2161
Joerg Roedel9966bf62009-08-07 11:49:40 +02002162static void nested_svm_vmloadsave(struct vmcb *from_vmcb, struct vmcb *to_vmcb)
Alexander Graf55426752008-11-25 20:17:06 +01002163{
2164 to_vmcb->save.fs = from_vmcb->save.fs;
2165 to_vmcb->save.gs = from_vmcb->save.gs;
2166 to_vmcb->save.tr = from_vmcb->save.tr;
2167 to_vmcb->save.ldtr = from_vmcb->save.ldtr;
2168 to_vmcb->save.kernel_gs_base = from_vmcb->save.kernel_gs_base;
2169 to_vmcb->save.star = from_vmcb->save.star;
2170 to_vmcb->save.lstar = from_vmcb->save.lstar;
2171 to_vmcb->save.cstar = from_vmcb->save.cstar;
2172 to_vmcb->save.sfmask = from_vmcb->save.sfmask;
2173 to_vmcb->save.sysenter_cs = from_vmcb->save.sysenter_cs;
2174 to_vmcb->save.sysenter_esp = from_vmcb->save.sysenter_esp;
2175 to_vmcb->save.sysenter_eip = from_vmcb->save.sysenter_eip;
Alexander Graf55426752008-11-25 20:17:06 +01002176}
2177
Avi Kivity851ba692009-08-24 11:10:17 +03002178static int vmload_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002179{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002180 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002181 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002182
Alexander Graf55426752008-11-25 20:17:06 +01002183 if (nested_svm_check_permissions(svm))
2184 return 1;
2185
2186 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2187 skip_emulated_instruction(&svm->vcpu);
2188
Joerg Roedel7597f122010-02-19 16:23:00 +01002189 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002190 if (!nested_vmcb)
2191 return 1;
2192
2193 nested_svm_vmloadsave(nested_vmcb, svm->vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002194 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002195
2196 return 1;
2197}
2198
Avi Kivity851ba692009-08-24 11:10:17 +03002199static int vmsave_interception(struct vcpu_svm *svm)
Alexander Graf55426752008-11-25 20:17:06 +01002200{
Joerg Roedel9966bf62009-08-07 11:49:40 +02002201 struct vmcb *nested_vmcb;
Joerg Roedel7597f122010-02-19 16:23:00 +01002202 struct page *page;
Joerg Roedel9966bf62009-08-07 11:49:40 +02002203
Alexander Graf55426752008-11-25 20:17:06 +01002204 if (nested_svm_check_permissions(svm))
2205 return 1;
2206
2207 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2208 skip_emulated_instruction(&svm->vcpu);
2209
Joerg Roedel7597f122010-02-19 16:23:00 +01002210 nested_vmcb = nested_svm_map(svm, svm->vmcb->save.rax, &page);
Joerg Roedel9966bf62009-08-07 11:49:40 +02002211 if (!nested_vmcb)
2212 return 1;
2213
2214 nested_svm_vmloadsave(svm->vmcb, nested_vmcb);
Joerg Roedel7597f122010-02-19 16:23:00 +01002215 nested_svm_unmap(page);
Alexander Graf55426752008-11-25 20:17:06 +01002216
2217 return 1;
2218}
2219
Avi Kivity851ba692009-08-24 11:10:17 +03002220static int vmrun_interception(struct vcpu_svm *svm)
Alexander Graf3d6368e2008-11-25 20:17:07 +01002221{
Alexander Graf3d6368e2008-11-25 20:17:07 +01002222 if (nested_svm_check_permissions(svm))
2223 return 1;
2224
2225 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2226 skip_emulated_instruction(&svm->vcpu);
2227
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002228 if (!nested_svm_vmrun(svm))
Alexander Graf3d6368e2008-11-25 20:17:07 +01002229 return 1;
2230
Joerg Roedel9738b2c2009-08-07 11:49:41 +02002231 if (!nested_svm_vmrun_msrpm(svm))
Joerg Roedel1f8da472009-08-07 11:49:43 +02002232 goto failed;
2233
2234 return 1;
2235
2236failed:
2237
2238 svm->vmcb->control.exit_code = SVM_EXIT_ERR;
2239 svm->vmcb->control.exit_code_hi = 0;
2240 svm->vmcb->control.exit_info_1 = 0;
2241 svm->vmcb->control.exit_info_2 = 0;
2242
2243 nested_svm_vmexit(svm);
Alexander Graf3d6368e2008-11-25 20:17:07 +01002244
2245 return 1;
2246}
2247
Avi Kivity851ba692009-08-24 11:10:17 +03002248static int stgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002249{
2250 if (nested_svm_check_permissions(svm))
2251 return 1;
2252
2253 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2254 skip_emulated_instruction(&svm->vcpu);
2255
Joerg Roedel2af91942009-08-07 11:49:28 +02002256 enable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002257
2258 return 1;
2259}
2260
Avi Kivity851ba692009-08-24 11:10:17 +03002261static int clgi_interception(struct vcpu_svm *svm)
Alexander Graf1371d902008-11-25 20:17:04 +01002262{
2263 if (nested_svm_check_permissions(svm))
2264 return 1;
2265
2266 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2267 skip_emulated_instruction(&svm->vcpu);
2268
Joerg Roedel2af91942009-08-07 11:49:28 +02002269 disable_gif(svm);
Alexander Graf1371d902008-11-25 20:17:04 +01002270
2271 /* After a CLGI no interrupts should come */
2272 svm_clear_vintr(svm);
2273 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
2274
2275 return 1;
2276}
2277
Avi Kivity851ba692009-08-24 11:10:17 +03002278static int invlpga_interception(struct vcpu_svm *svm)
Alexander Grafff092382009-06-15 15:21:24 +02002279{
2280 struct kvm_vcpu *vcpu = &svm->vcpu;
Alexander Grafff092382009-06-15 15:21:24 +02002281
Joerg Roedelec1ff792009-10-09 16:08:31 +02002282 trace_kvm_invlpga(svm->vmcb->save.rip, vcpu->arch.regs[VCPU_REGS_RCX],
2283 vcpu->arch.regs[VCPU_REGS_RAX]);
2284
Alexander Grafff092382009-06-15 15:21:24 +02002285 /* Let's treat INVLPGA the same as INVLPG (can be optimized!) */
2286 kvm_mmu_invlpg(vcpu, vcpu->arch.regs[VCPU_REGS_RAX]);
2287
2288 svm->next_rip = kvm_rip_read(&svm->vcpu) + 3;
2289 skip_emulated_instruction(&svm->vcpu);
2290 return 1;
2291}
2292
Joerg Roedel532a46b2009-10-09 16:08:32 +02002293static int skinit_interception(struct vcpu_svm *svm)
2294{
2295 trace_kvm_skinit(svm->vmcb->save.rip, svm->vcpu.arch.regs[VCPU_REGS_RAX]);
2296
2297 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
2298 return 1;
2299}
2300
Avi Kivity851ba692009-08-24 11:10:17 +03002301static int invalid_op_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002302{
Avi Kivity7ee5d9402007-11-25 15:22:50 +02002303 kvm_queue_exception(&svm->vcpu, UD_VECTOR);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002304 return 1;
2305}
2306
Avi Kivity851ba692009-08-24 11:10:17 +03002307static int task_switch_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002308{
Izik Eidus37817f22008-03-24 23:14:53 +02002309 u16 tss_selector;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002310 int reason;
2311 int int_type = svm->vmcb->control.exit_int_info &
2312 SVM_EXITINTINFO_TYPE_MASK;
Gleb Natapov8317c292009-04-12 13:37:02 +03002313 int int_vec = svm->vmcb->control.exit_int_info & SVM_EVTINJ_VEC_MASK;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002314 uint32_t type =
2315 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_TYPE_MASK;
2316 uint32_t idt_v =
2317 svm->vmcb->control.exit_int_info & SVM_EXITINTINFO_VALID;
Jan Kiszkae269fb22010-04-14 15:51:09 +02002318 bool has_error_code = false;
2319 u32 error_code = 0;
Izik Eidus37817f22008-03-24 23:14:53 +02002320
2321 tss_selector = (u16)svm->vmcb->control.exit_info_1;
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002322
Izik Eidus37817f22008-03-24 23:14:53 +02002323 if (svm->vmcb->control.exit_info_2 &
2324 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_IRET))
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002325 reason = TASK_SWITCH_IRET;
2326 else if (svm->vmcb->control.exit_info_2 &
2327 (1ULL << SVM_EXITINFOSHIFT_TS_REASON_JMP))
2328 reason = TASK_SWITCH_JMP;
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002329 else if (idt_v)
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002330 reason = TASK_SWITCH_GATE;
2331 else
2332 reason = TASK_SWITCH_CALL;
2333
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002334 if (reason == TASK_SWITCH_GATE) {
2335 switch (type) {
2336 case SVM_EXITINTINFO_TYPE_NMI:
2337 svm->vcpu.arch.nmi_injected = false;
2338 break;
2339 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszkae269fb22010-04-14 15:51:09 +02002340 if (svm->vmcb->control.exit_info_2 &
2341 (1ULL << SVM_EXITINFOSHIFT_TS_HAS_ERROR_CODE)) {
2342 has_error_code = true;
2343 error_code =
2344 (u32)svm->vmcb->control.exit_info_2;
2345 }
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002346 kvm_clear_exception_queue(&svm->vcpu);
2347 break;
2348 case SVM_EXITINTINFO_TYPE_INTR:
2349 kvm_clear_interrupt_queue(&svm->vcpu);
2350 break;
2351 default:
2352 break;
2353 }
2354 }
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002355
Gleb Natapov8317c292009-04-12 13:37:02 +03002356 if (reason != TASK_SWITCH_GATE ||
2357 int_type == SVM_EXITINTINFO_TYPE_SOFT ||
2358 (int_type == SVM_EXITINTINFO_TYPE_EXEPT &&
Gleb Natapovf629cf82009-05-11 13:35:49 +03002359 (int_vec == OF_VECTOR || int_vec == BP_VECTOR)))
2360 skip_emulated_instruction(&svm->vcpu);
Gleb Natapov64a7ec02009-03-30 16:03:29 +03002361
Gleb Natapovacb54512010-04-15 21:03:50 +03002362 if (kvm_task_switch(&svm->vcpu, tss_selector, reason,
2363 has_error_code, error_code) == EMULATE_FAIL) {
2364 svm->vcpu.run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2365 svm->vcpu.run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
2366 svm->vcpu.run->internal.ndata = 0;
2367 return 0;
2368 }
2369 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002370}
2371
Avi Kivity851ba692009-08-24 11:10:17 +03002372static int cpuid_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002373{
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002374 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002375 kvm_emulate_cpuid(&svm->vcpu);
Avi Kivity06465c52007-02-28 20:46:53 +02002376 return 1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002377}
2378
Avi Kivity851ba692009-08-24 11:10:17 +03002379static int iret_interception(struct vcpu_svm *svm)
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002380{
2381 ++svm->vcpu.stat.nmi_window_exits;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002382 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Gleb Natapov44c11432009-05-11 13:35:52 +03002383 svm->vcpu.arch.hflags |= HF_IRET_MASK;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002384 return 1;
2385}
2386
Avi Kivity851ba692009-08-24 11:10:17 +03002387static int invlpg_interception(struct vcpu_svm *svm)
Marcelo Tosattia7052892008-09-23 13:18:35 -03002388{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002389 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Marcelo Tosattia7052892008-09-23 13:18:35 -03002390}
2391
Avi Kivity851ba692009-08-24 11:10:17 +03002392static int emulate_on_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002393{
Gleb Natapov6d77dbf2010-05-10 11:16:56 +03002394 return emulate_instruction(&svm->vcpu, 0, 0, 0) == EMULATE_DONE;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002395}
2396
Avi Kivity851ba692009-08-24 11:10:17 +03002397static int cr8_write_interception(struct vcpu_svm *svm)
Joerg Roedel1d075432007-12-06 21:02:25 +01002398{
Avi Kivity851ba692009-08-24 11:10:17 +03002399 struct kvm_run *kvm_run = svm->vcpu.run;
2400
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002401 u8 cr8_prev = kvm_get_cr8(&svm->vcpu);
2402 /* instruction emulation calls kvm_set_cr8() */
Avi Kivity851ba692009-08-24 11:10:17 +03002403 emulate_instruction(&svm->vcpu, 0, 0, 0);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002404 if (irqchip_in_kernel(svm->vcpu.kvm)) {
2405 svm->vmcb->control.intercept_cr_write &= ~INTERCEPT_CR8_MASK;
Joerg Roedel1d075432007-12-06 21:02:25 +01002406 return 1;
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002407 }
Gleb Natapov0a5fff192009-04-21 17:45:06 +03002408 if (cr8_prev <= kvm_get_cr8(&svm->vcpu))
2409 return 1;
Joerg Roedel1d075432007-12-06 21:02:25 +01002410 kvm_run->exit_reason = KVM_EXIT_SET_TPR;
2411 return 0;
2412}
2413
Avi Kivity6aa8b732006-12-10 02:21:36 -08002414static int svm_get_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 *data)
2415{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002416 struct vcpu_svm *svm = to_svm(vcpu);
2417
Avi Kivity6aa8b732006-12-10 02:21:36 -08002418 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302419 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002420 u64 tsc_offset;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002421
Joerg Roedel20824f32009-09-16 15:24:18 +02002422 if (is_nested(svm))
2423 tsc_offset = svm->nested.hsave->control.tsc_offset;
2424 else
2425 tsc_offset = svm->vmcb->control.tsc_offset;
2426
2427 *data = tsc_offset + native_read_tsc();
Avi Kivity6aa8b732006-12-10 02:21:36 -08002428 break;
2429 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002430 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002431 *data = svm->vmcb->save.star;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002432 break;
Avi Kivity0e859ca2006-12-22 01:05:08 -08002433#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002434 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002435 *data = svm->vmcb->save.lstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002436 break;
2437 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002438 *data = svm->vmcb->save.cstar;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002439 break;
2440 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002441 *data = svm->vmcb->save.kernel_gs_base;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002442 break;
2443 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002444 *data = svm->vmcb->save.sfmask;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002445 break;
2446#endif
2447 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002448 *data = svm->vmcb->save.sysenter_cs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002449 break;
2450 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002451 *data = svm->sysenter_eip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002452 break;
2453 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002454 *data = svm->sysenter_esp;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002455 break;
Joerg Roedele0231712010-02-24 18:59:10 +01002456 /*
2457 * Nobody will change the following 5 values in the VMCB so we can
2458 * safely return them on rdmsr. They will always be 0 until LBRV is
2459 * implemented.
2460 */
Joerg Roedela2938c82008-02-13 16:30:28 +01002461 case MSR_IA32_DEBUGCTLMSR:
2462 *data = svm->vmcb->save.dbgctl;
2463 break;
2464 case MSR_IA32_LASTBRANCHFROMIP:
2465 *data = svm->vmcb->save.br_from;
2466 break;
2467 case MSR_IA32_LASTBRANCHTOIP:
2468 *data = svm->vmcb->save.br_to;
2469 break;
2470 case MSR_IA32_LASTINTFROMIP:
2471 *data = svm->vmcb->save.last_excp_from;
2472 break;
2473 case MSR_IA32_LASTINTTOIP:
2474 *data = svm->vmcb->save.last_excp_to;
2475 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002476 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002477 *data = svm->nested.hsave_msr;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002478 break;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002479 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002480 *data = svm->nested.vm_cr_msr;
Joerg Roedeleb6f3022008-11-25 20:17:09 +01002481 break;
Alexander Grafc8a73f12009-01-05 16:02:47 +01002482 case MSR_IA32_UCODE_REV:
2483 *data = 0x01000065;
2484 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002485 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002486 return kvm_get_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002487 }
2488 return 0;
2489}
2490
Avi Kivity851ba692009-08-24 11:10:17 +03002491static int rdmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002492{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002493 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Avi Kivity6aa8b732006-12-10 02:21:36 -08002494 u64 data;
2495
Avi Kivity59200272010-01-25 19:47:02 +02002496 if (svm_get_msr(&svm->vcpu, ecx, &data)) {
2497 trace_kvm_msr_read_ex(ecx);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002498 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002499 } else {
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002500 trace_kvm_msr_read(ecx, data);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002501
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002502 svm->vcpu.arch.regs[VCPU_REGS_RAX] = data & 0xffffffff;
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002503 svm->vcpu.arch.regs[VCPU_REGS_RDX] = data >> 32;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002504 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Rusty Russelle756fc62007-07-30 20:07:08 +10002505 skip_emulated_instruction(&svm->vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002506 }
2507 return 1;
2508}
2509
Joerg Roedel4a810182010-02-24 18:59:15 +01002510static int svm_set_vm_cr(struct kvm_vcpu *vcpu, u64 data)
2511{
2512 struct vcpu_svm *svm = to_svm(vcpu);
2513 int svm_dis, chg_mask;
2514
2515 if (data & ~SVM_VM_CR_VALID_MASK)
2516 return 1;
2517
2518 chg_mask = SVM_VM_CR_VALID_MASK;
2519
2520 if (svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK)
2521 chg_mask &= ~(SVM_VM_CR_SVM_LOCK_MASK | SVM_VM_CR_SVM_DIS_MASK);
2522
2523 svm->nested.vm_cr_msr &= ~chg_mask;
2524 svm->nested.vm_cr_msr |= (data & chg_mask);
2525
2526 svm_dis = svm->nested.vm_cr_msr & SVM_VM_CR_SVM_DIS_MASK;
2527
2528 /* check for svm_disable while efer.svme is set */
2529 if (svm_dis && (vcpu->arch.efer & EFER_SVME))
2530 return 1;
2531
2532 return 0;
2533}
2534
Avi Kivity6aa8b732006-12-10 02:21:36 -08002535static int svm_set_msr(struct kvm_vcpu *vcpu, unsigned ecx, u64 data)
2536{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002537 struct vcpu_svm *svm = to_svm(vcpu);
2538
Avi Kivity6aa8b732006-12-10 02:21:36 -08002539 switch (ecx) {
Jaswinder Singh Rajputaf24a4e2009-05-15 18:42:05 +05302540 case MSR_IA32_TSC: {
Joerg Roedel20824f32009-09-16 15:24:18 +02002541 u64 tsc_offset = data - native_read_tsc();
2542 u64 g_tsc_offset = 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002543
Joerg Roedel20824f32009-09-16 15:24:18 +02002544 if (is_nested(svm)) {
2545 g_tsc_offset = svm->vmcb->control.tsc_offset -
2546 svm->nested.hsave->control.tsc_offset;
2547 svm->nested.hsave->control.tsc_offset = tsc_offset;
2548 }
2549
2550 svm->vmcb->control.tsc_offset = tsc_offset + g_tsc_offset;
2551
Avi Kivity6aa8b732006-12-10 02:21:36 -08002552 break;
2553 }
Avi Kivity0e859ca2006-12-22 01:05:08 -08002554 case MSR_K6_STAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002555 svm->vmcb->save.star = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002556 break;
Robert P. J. Day49b14f22007-01-29 13:19:50 -08002557#ifdef CONFIG_X86_64
Avi Kivity6aa8b732006-12-10 02:21:36 -08002558 case MSR_LSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002559 svm->vmcb->save.lstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002560 break;
2561 case MSR_CSTAR:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002562 svm->vmcb->save.cstar = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002563 break;
2564 case MSR_KERNEL_GS_BASE:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002565 svm->vmcb->save.kernel_gs_base = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002566 break;
2567 case MSR_SYSCALL_MASK:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002568 svm->vmcb->save.sfmask = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 break;
2570#endif
2571 case MSR_IA32_SYSENTER_CS:
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002572 svm->vmcb->save.sysenter_cs = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002573 break;
2574 case MSR_IA32_SYSENTER_EIP:
Andre Przywara017cb992009-05-28 11:56:31 +02002575 svm->sysenter_eip = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002576 svm->vmcb->save.sysenter_eip = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002577 break;
2578 case MSR_IA32_SYSENTER_ESP:
Andre Przywara017cb992009-05-28 11:56:31 +02002579 svm->sysenter_esp = data;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002580 svm->vmcb->save.sysenter_esp = data;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002581 break;
Joerg Roedela2938c82008-02-13 16:30:28 +01002582 case MSR_IA32_DEBUGCTLMSR:
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002583 if (!svm_has(SVM_FEATURE_LBRV)) {
2584 pr_unimpl(vcpu, "%s: MSR_IA32_DEBUGCTL 0x%llx, nop\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002585 __func__, data);
Joerg Roedel24e09cb2008-02-13 18:58:47 +01002586 break;
2587 }
2588 if (data & DEBUGCTL_RESERVED_BITS)
2589 return 1;
2590
2591 svm->vmcb->save.dbgctl = data;
2592 if (data & (1ULL<<0))
2593 svm_enable_lbrv(svm);
2594 else
2595 svm_disable_lbrv(svm);
Joerg Roedela2938c82008-02-13 16:30:28 +01002596 break;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002597 case MSR_VM_HSAVE_PA:
Joerg Roedele6aa9ab2009-08-07 11:49:33 +02002598 svm->nested.hsave_msr = data;
Alexander Grafb286d5d2008-11-25 20:17:05 +01002599 break;
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002600 case MSR_VM_CR:
Joerg Roedel4a810182010-02-24 18:59:15 +01002601 return svm_set_vm_cr(vcpu, data);
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002602 case MSR_VM_IGNNE:
Alexander Graf3c5d0a42009-06-15 15:21:23 +02002603 pr_unimpl(vcpu, "unimplemented wrmsr: 0x%x data 0x%llx\n", ecx, data);
2604 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002605 default:
Avi Kivity3bab1f52006-12-29 16:49:48 -08002606 return kvm_set_msr_common(vcpu, ecx, data);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002607 }
2608 return 0;
2609}
2610
Avi Kivity851ba692009-08-24 11:10:17 +03002611static int wrmsr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002612{
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002613 u32 ecx = svm->vcpu.arch.regs[VCPU_REGS_RCX];
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002614 u64 data = (svm->vcpu.arch.regs[VCPU_REGS_RAX] & -1u)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002615 | ((u64)(svm->vcpu.arch.regs[VCPU_REGS_RDX] & -1u) << 32);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002616
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002617
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03002618 svm->next_rip = kvm_rip_read(&svm->vcpu) + 2;
Avi Kivity59200272010-01-25 19:47:02 +02002619 if (svm_set_msr(&svm->vcpu, ecx, data)) {
2620 trace_kvm_msr_write_ex(ecx, data);
Avi Kivityc1a5d4f2007-11-25 14:12:03 +02002621 kvm_inject_gp(&svm->vcpu, 0);
Avi Kivity59200272010-01-25 19:47:02 +02002622 } else {
2623 trace_kvm_msr_write(ecx, data);
Rusty Russelle756fc62007-07-30 20:07:08 +10002624 skip_emulated_instruction(&svm->vcpu);
Avi Kivity59200272010-01-25 19:47:02 +02002625 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08002626 return 1;
2627}
2628
Avi Kivity851ba692009-08-24 11:10:17 +03002629static int msr_interception(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002630{
Rusty Russelle756fc62007-07-30 20:07:08 +10002631 if (svm->vmcb->control.exit_info_1)
Avi Kivity851ba692009-08-24 11:10:17 +03002632 return wrmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002633 else
Avi Kivity851ba692009-08-24 11:10:17 +03002634 return rdmsr_interception(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002635}
2636
Avi Kivity851ba692009-08-24 11:10:17 +03002637static int interrupt_window_interception(struct vcpu_svm *svm)
Dor Laorc1150d82007-01-05 16:36:24 -08002638{
Avi Kivity851ba692009-08-24 11:10:17 +03002639 struct kvm_run *kvm_run = svm->vcpu.run;
2640
Alexander Graff0b85052008-11-25 20:17:01 +01002641 svm_clear_vintr(svm);
Eddie Dong85f455f2007-07-06 12:20:49 +03002642 svm->vmcb->control.int_ctl &= ~V_IRQ_MASK;
Dor Laorc1150d82007-01-05 16:36:24 -08002643 /*
2644 * If the user space waits to inject interrupts, exit as soon as
2645 * possible
2646 */
Gleb Natapov80618232009-04-21 17:44:56 +03002647 if (!irqchip_in_kernel(svm->vcpu.kvm) &&
2648 kvm_run->request_interrupt_window &&
2649 !kvm_cpu_has_interrupt(&svm->vcpu)) {
Rusty Russelle756fc62007-07-30 20:07:08 +10002650 ++svm->vcpu.stat.irq_window_exits;
Dor Laorc1150d82007-01-05 16:36:24 -08002651 kvm_run->exit_reason = KVM_EXIT_IRQ_WINDOW_OPEN;
2652 return 0;
2653 }
2654
2655 return 1;
2656}
2657
Mark Langsdorf565d0992009-10-06 14:25:02 -05002658static int pause_interception(struct vcpu_svm *svm)
2659{
2660 kvm_vcpu_on_spin(&(svm->vcpu));
2661 return 1;
2662}
2663
Avi Kivity851ba692009-08-24 11:10:17 +03002664static int (*svm_exit_handlers[])(struct vcpu_svm *svm) = {
Joerg Roedele0231712010-02-24 18:59:10 +01002665 [SVM_EXIT_READ_CR0] = emulate_on_interception,
2666 [SVM_EXIT_READ_CR3] = emulate_on_interception,
2667 [SVM_EXIT_READ_CR4] = emulate_on_interception,
2668 [SVM_EXIT_READ_CR8] = emulate_on_interception,
Avi Kivityd2251572010-01-06 10:55:27 +02002669 [SVM_EXIT_CR0_SEL_WRITE] = emulate_on_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002670 [SVM_EXIT_WRITE_CR0] = emulate_on_interception,
2671 [SVM_EXIT_WRITE_CR3] = emulate_on_interception,
2672 [SVM_EXIT_WRITE_CR4] = emulate_on_interception,
2673 [SVM_EXIT_WRITE_CR8] = cr8_write_interception,
2674 [SVM_EXIT_READ_DR0] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002675 [SVM_EXIT_READ_DR1] = emulate_on_interception,
2676 [SVM_EXIT_READ_DR2] = emulate_on_interception,
2677 [SVM_EXIT_READ_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002678 [SVM_EXIT_READ_DR4] = emulate_on_interception,
2679 [SVM_EXIT_READ_DR5] = emulate_on_interception,
2680 [SVM_EXIT_READ_DR6] = emulate_on_interception,
2681 [SVM_EXIT_READ_DR7] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002682 [SVM_EXIT_WRITE_DR0] = emulate_on_interception,
2683 [SVM_EXIT_WRITE_DR1] = emulate_on_interception,
2684 [SVM_EXIT_WRITE_DR2] = emulate_on_interception,
2685 [SVM_EXIT_WRITE_DR3] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002686 [SVM_EXIT_WRITE_DR4] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002687 [SVM_EXIT_WRITE_DR5] = emulate_on_interception,
Jan Kiszka727f5a22010-01-20 18:20:20 +01002688 [SVM_EXIT_WRITE_DR6] = emulate_on_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002689 [SVM_EXIT_WRITE_DR7] = emulate_on_interception,
Jan Kiszkad0bfb942008-12-15 13:52:10 +01002690 [SVM_EXIT_EXCP_BASE + DB_VECTOR] = db_interception,
2691 [SVM_EXIT_EXCP_BASE + BP_VECTOR] = bp_interception,
Anthony Liguori7aa81cc2007-09-17 14:57:50 -05002692 [SVM_EXIT_EXCP_BASE + UD_VECTOR] = ud_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002693 [SVM_EXIT_EXCP_BASE + PF_VECTOR] = pf_interception,
2694 [SVM_EXIT_EXCP_BASE + NM_VECTOR] = nm_interception,
2695 [SVM_EXIT_EXCP_BASE + MC_VECTOR] = mc_interception,
2696 [SVM_EXIT_INTR] = intr_interception,
Joerg Roedelc47f0982008-04-30 17:56:00 +02002697 [SVM_EXIT_NMI] = nmi_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002698 [SVM_EXIT_SMI] = nop_on_interception,
2699 [SVM_EXIT_INIT] = nop_on_interception,
Dor Laorc1150d82007-01-05 16:36:24 -08002700 [SVM_EXIT_VINTR] = interrupt_window_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002701 [SVM_EXIT_CPUID] = cpuid_interception,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002702 [SVM_EXIT_IRET] = iret_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002703 [SVM_EXIT_INVD] = emulate_on_interception,
Mark Langsdorf565d0992009-10-06 14:25:02 -05002704 [SVM_EXIT_PAUSE] = pause_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002705 [SVM_EXIT_HLT] = halt_interception,
Marcelo Tosattia7052892008-09-23 13:18:35 -03002706 [SVM_EXIT_INVLPG] = invlpg_interception,
Alexander Grafff092382009-06-15 15:21:24 +02002707 [SVM_EXIT_INVLPGA] = invlpga_interception,
Joerg Roedele0231712010-02-24 18:59:10 +01002708 [SVM_EXIT_IOIO] = io_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002709 [SVM_EXIT_MSR] = msr_interception,
2710 [SVM_EXIT_TASK_SWITCH] = task_switch_interception,
Joerg Roedel46fe4dd2007-01-26 00:56:42 -08002711 [SVM_EXIT_SHUTDOWN] = shutdown_interception,
Alexander Graf3d6368e2008-11-25 20:17:07 +01002712 [SVM_EXIT_VMRUN] = vmrun_interception,
Avi Kivity02e235b2007-02-19 14:37:47 +02002713 [SVM_EXIT_VMMCALL] = vmmcall_interception,
Alexander Graf55426752008-11-25 20:17:06 +01002714 [SVM_EXIT_VMLOAD] = vmload_interception,
2715 [SVM_EXIT_VMSAVE] = vmsave_interception,
Alexander Graf1371d902008-11-25 20:17:04 +01002716 [SVM_EXIT_STGI] = stgi_interception,
2717 [SVM_EXIT_CLGI] = clgi_interception,
Joerg Roedel532a46b2009-10-09 16:08:32 +02002718 [SVM_EXIT_SKINIT] = skinit_interception,
Avi Kivitycf5a94d2007-10-28 16:11:58 +02002719 [SVM_EXIT_WBINVD] = emulate_on_interception,
Joerg Roedel916ce232007-03-21 19:47:00 +01002720 [SVM_EXIT_MONITOR] = invalid_op_interception,
2721 [SVM_EXIT_MWAIT] = invalid_op_interception,
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002722 [SVM_EXIT_NPF] = pf_interception,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002723};
2724
Joerg Roedel3f10c842010-05-05 16:04:42 +02002725void dump_vmcb(struct kvm_vcpu *vcpu)
2726{
2727 struct vcpu_svm *svm = to_svm(vcpu);
2728 struct vmcb_control_area *control = &svm->vmcb->control;
2729 struct vmcb_save_area *save = &svm->vmcb->save;
2730
2731 pr_err("VMCB Control Area:\n");
2732 pr_err("cr_read: %04x\n", control->intercept_cr_read);
2733 pr_err("cr_write: %04x\n", control->intercept_cr_write);
2734 pr_err("dr_read: %04x\n", control->intercept_dr_read);
2735 pr_err("dr_write: %04x\n", control->intercept_dr_write);
2736 pr_err("exceptions: %08x\n", control->intercept_exceptions);
2737 pr_err("intercepts: %016llx\n", control->intercept);
2738 pr_err("pause filter count: %d\n", control->pause_filter_count);
2739 pr_err("iopm_base_pa: %016llx\n", control->iopm_base_pa);
2740 pr_err("msrpm_base_pa: %016llx\n", control->msrpm_base_pa);
2741 pr_err("tsc_offset: %016llx\n", control->tsc_offset);
2742 pr_err("asid: %d\n", control->asid);
2743 pr_err("tlb_ctl: %d\n", control->tlb_ctl);
2744 pr_err("int_ctl: %08x\n", control->int_ctl);
2745 pr_err("int_vector: %08x\n", control->int_vector);
2746 pr_err("int_state: %08x\n", control->int_state);
2747 pr_err("exit_code: %08x\n", control->exit_code);
2748 pr_err("exit_info1: %016llx\n", control->exit_info_1);
2749 pr_err("exit_info2: %016llx\n", control->exit_info_2);
2750 pr_err("exit_int_info: %08x\n", control->exit_int_info);
2751 pr_err("exit_int_info_err: %08x\n", control->exit_int_info_err);
2752 pr_err("nested_ctl: %lld\n", control->nested_ctl);
2753 pr_err("nested_cr3: %016llx\n", control->nested_cr3);
2754 pr_err("event_inj: %08x\n", control->event_inj);
2755 pr_err("event_inj_err: %08x\n", control->event_inj_err);
2756 pr_err("lbr_ctl: %lld\n", control->lbr_ctl);
2757 pr_err("next_rip: %016llx\n", control->next_rip);
2758 pr_err("VMCB State Save Area:\n");
2759 pr_err("es: s: %04x a: %04x l: %08x b: %016llx\n",
2760 save->es.selector, save->es.attrib,
2761 save->es.limit, save->es.base);
2762 pr_err("cs: s: %04x a: %04x l: %08x b: %016llx\n",
2763 save->cs.selector, save->cs.attrib,
2764 save->cs.limit, save->cs.base);
2765 pr_err("ss: s: %04x a: %04x l: %08x b: %016llx\n",
2766 save->ss.selector, save->ss.attrib,
2767 save->ss.limit, save->ss.base);
2768 pr_err("ds: s: %04x a: %04x l: %08x b: %016llx\n",
2769 save->ds.selector, save->ds.attrib,
2770 save->ds.limit, save->ds.base);
2771 pr_err("fs: s: %04x a: %04x l: %08x b: %016llx\n",
2772 save->fs.selector, save->fs.attrib,
2773 save->fs.limit, save->fs.base);
2774 pr_err("gs: s: %04x a: %04x l: %08x b: %016llx\n",
2775 save->gs.selector, save->gs.attrib,
2776 save->gs.limit, save->gs.base);
2777 pr_err("gdtr: s: %04x a: %04x l: %08x b: %016llx\n",
2778 save->gdtr.selector, save->gdtr.attrib,
2779 save->gdtr.limit, save->gdtr.base);
2780 pr_err("ldtr: s: %04x a: %04x l: %08x b: %016llx\n",
2781 save->ldtr.selector, save->ldtr.attrib,
2782 save->ldtr.limit, save->ldtr.base);
2783 pr_err("idtr: s: %04x a: %04x l: %08x b: %016llx\n",
2784 save->idtr.selector, save->idtr.attrib,
2785 save->idtr.limit, save->idtr.base);
2786 pr_err("tr: s: %04x a: %04x l: %08x b: %016llx\n",
2787 save->tr.selector, save->tr.attrib,
2788 save->tr.limit, save->tr.base);
2789 pr_err("cpl: %d efer: %016llx\n",
2790 save->cpl, save->efer);
2791 pr_err("cr0: %016llx cr2: %016llx\n",
2792 save->cr0, save->cr2);
2793 pr_err("cr3: %016llx cr4: %016llx\n",
2794 save->cr3, save->cr4);
2795 pr_err("dr6: %016llx dr7: %016llx\n",
2796 save->dr6, save->dr7);
2797 pr_err("rip: %016llx rflags: %016llx\n",
2798 save->rip, save->rflags);
2799 pr_err("rsp: %016llx rax: %016llx\n",
2800 save->rsp, save->rax);
2801 pr_err("star: %016llx lstar: %016llx\n",
2802 save->star, save->lstar);
2803 pr_err("cstar: %016llx sfmask: %016llx\n",
2804 save->cstar, save->sfmask);
2805 pr_err("kernel_gs_base: %016llx sysenter_cs: %016llx\n",
2806 save->kernel_gs_base, save->sysenter_cs);
2807 pr_err("sysenter_esp: %016llx sysenter_eip: %016llx\n",
2808 save->sysenter_esp, save->sysenter_eip);
2809 pr_err("gpat: %016llx dbgctl: %016llx\n",
2810 save->g_pat, save->dbgctl);
2811 pr_err("br_from: %016llx br_to: %016llx\n",
2812 save->br_from, save->br_to);
2813 pr_err("excp_from: %016llx excp_to: %016llx\n",
2814 save->last_excp_from, save->last_excp_to);
2815
2816}
2817
Avi Kivity851ba692009-08-24 11:10:17 +03002818static int handle_exit(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002819{
Avi Kivity04d2cc72007-09-10 18:10:54 +03002820 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity851ba692009-08-24 11:10:17 +03002821 struct kvm_run *kvm_run = vcpu->run;
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002822 u32 exit_code = svm->vmcb->control.exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002823
Avi Kivity5bfd8b52010-03-11 10:50:44 +02002824 trace_kvm_exit(exit_code, vcpu);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002825
Joerg Roedel2be4fc72010-04-22 12:33:09 +02002826 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR0_MASK))
2827 vcpu->arch.cr0 = svm->vmcb->save.cr0;
2828 if (npt_enabled)
2829 vcpu->arch.cr3 = svm->vmcb->save.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002830
Joerg Roedelcd3ff652009-10-09 16:08:26 +02002831 if (unlikely(svm->nested.exit_required)) {
2832 nested_svm_vmexit(svm);
2833 svm->nested.exit_required = false;
2834
2835 return 1;
2836 }
2837
Alexander Grafcf74a782008-11-25 20:17:08 +01002838 if (is_nested(svm)) {
Joerg Roedel410e4d52009-08-07 11:49:44 +02002839 int vmexit;
2840
Joerg Roedeld8cabdd2009-10-09 16:08:28 +02002841 trace_kvm_nested_vmexit(svm->vmcb->save.rip, exit_code,
2842 svm->vmcb->control.exit_info_1,
2843 svm->vmcb->control.exit_info_2,
2844 svm->vmcb->control.exit_int_info,
2845 svm->vmcb->control.exit_int_info_err);
2846
Joerg Roedel410e4d52009-08-07 11:49:44 +02002847 vmexit = nested_svm_exit_special(svm);
2848
2849 if (vmexit == NESTED_EXIT_CONTINUE)
2850 vmexit = nested_svm_exit_handled(svm);
2851
2852 if (vmexit == NESTED_EXIT_DONE)
Alexander Grafcf74a782008-11-25 20:17:08 +01002853 return 1;
Alexander Grafcf74a782008-11-25 20:17:08 +01002854 }
2855
Joerg Roedela5c38322009-08-07 11:49:32 +02002856 svm_complete_interrupts(svm);
2857
Avi Kivity04d2cc72007-09-10 18:10:54 +03002858 if (svm->vmcb->control.exit_code == SVM_EXIT_ERR) {
2859 kvm_run->exit_reason = KVM_EXIT_FAIL_ENTRY;
2860 kvm_run->fail_entry.hardware_entry_failure_reason
2861 = svm->vmcb->control.exit_code;
Joerg Roedel3f10c842010-05-05 16:04:42 +02002862 pr_err("KVM: FAILED VMRUN WITH VMCB:\n");
2863 dump_vmcb(vcpu);
Avi Kivity04d2cc72007-09-10 18:10:54 +03002864 return 0;
2865 }
2866
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002867 if (is_external_interrupt(svm->vmcb->control.exit_int_info) &&
Joerg Roedel709ddeb2008-02-07 13:47:45 +01002868 exit_code != SVM_EXIT_EXCP_BASE + PF_VECTOR &&
Gleb Natapovfe8e7f82009-04-23 17:03:48 +03002869 exit_code != SVM_EXIT_NPF && exit_code != SVM_EXIT_TASK_SWITCH)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002870 printk(KERN_ERR "%s: unexpected exit_ini_info 0x%x "
2871 "exit_code 0x%x\n",
Harvey Harrisonb8688d52008-03-03 12:59:56 -08002872 __func__, svm->vmcb->control.exit_int_info,
Avi Kivity6aa8b732006-12-10 02:21:36 -08002873 exit_code);
2874
Ahmed S. Darwish9d8f5492007-02-19 14:37:46 +02002875 if (exit_code >= ARRAY_SIZE(svm_exit_handlers)
Joe Perches56919c52007-11-12 20:06:51 -08002876 || !svm_exit_handlers[exit_code]) {
Avi Kivity6aa8b732006-12-10 02:21:36 -08002877 kvm_run->exit_reason = KVM_EXIT_UNKNOWN;
Avi Kivity364b6252007-04-16 14:28:40 +03002878 kvm_run->hw.hardware_exit_reason = exit_code;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002879 return 0;
2880 }
2881
Avi Kivity851ba692009-08-24 11:10:17 +03002882 return svm_exit_handlers[exit_code](svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002883}
2884
2885static void reload_tss(struct kvm_vcpu *vcpu)
2886{
2887 int cpu = raw_smp_processor_id();
2888
Tejun Heo0fe1e002009-10-29 22:34:14 +09002889 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
2890 sd->tss_desc->type = 9; /* available 32/64-bit TSS */
Avi Kivity6aa8b732006-12-10 02:21:36 -08002891 load_TR_desc();
2892}
2893
Rusty Russelle756fc62007-07-30 20:07:08 +10002894static void pre_svm_run(struct vcpu_svm *svm)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002895{
2896 int cpu = raw_smp_processor_id();
2897
Tejun Heo0fe1e002009-10-29 22:34:14 +09002898 struct svm_cpu_data *sd = per_cpu(svm_data, cpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002899
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04002900 svm->vmcb->control.tlb_ctl = TLB_CONTROL_DO_NOTHING;
Marcelo Tosatti4b656b12009-07-21 12:47:45 -03002901 /* FIXME: handle wraparound of asid_generation */
Tejun Heo0fe1e002009-10-29 22:34:14 +09002902 if (svm->asid_generation != sd->asid_generation)
2903 new_asid(svm, sd);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002904}
2905
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002906static void svm_inject_nmi(struct kvm_vcpu *vcpu)
2907{
2908 struct vcpu_svm *svm = to_svm(vcpu);
2909
2910 svm->vmcb->control.event_inj = SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_NMI;
2911 vcpu->arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002912 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002913 ++vcpu->stat.nmi_injections;
2914}
Avi Kivity6aa8b732006-12-10 02:21:36 -08002915
Eddie Dong85f455f2007-07-06 12:20:49 +03002916static inline void svm_inject_irq(struct vcpu_svm *svm, int irq)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002917{
2918 struct vmcb_control_area *control;
2919
Marcelo Tosatti229456f2009-06-17 09:22:14 -03002920 trace_kvm_inj_virq(irq);
Joerg Roedelaf9ca2d2008-04-30 17:56:03 +02002921
Avi Kivityfa89a812008-09-01 15:57:51 +03002922 ++svm->vcpu.stat.irq_injections;
Rusty Russelle756fc62007-07-30 20:07:08 +10002923 control = &svm->vmcb->control;
Eddie Dong85f455f2007-07-06 12:20:49 +03002924 control->int_vector = irq;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002925 control->int_ctl &= ~V_INTR_PRIO_MASK;
2926 control->int_ctl |= V_IRQ_MASK |
2927 ((/*control->int_vector >> 4*/ 0xf) << V_INTR_PRIO_SHIFT);
2928}
2929
Gleb Natapov66fd3f72009-05-11 13:35:50 +03002930static void svm_set_irq(struct kvm_vcpu *vcpu)
Eddie Dong2a8067f2007-08-06 16:29:07 +03002931{
2932 struct vcpu_svm *svm = to_svm(vcpu);
2933
Joerg Roedel2af91942009-08-07 11:49:28 +02002934 BUG_ON(!(gif_set(svm)));
Alexander Grafcf74a782008-11-25 20:17:08 +01002935
Alexander Graf219b65d2009-06-15 15:21:25 +02002936 svm->vmcb->control.event_inj = vcpu->arch.interrupt.nr |
2937 SVM_EVTINJ_VALID | SVM_EVTINJ_TYPE_INTR;
Eddie Dong2a8067f2007-08-06 16:29:07 +03002938}
2939
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002940static void update_cr8_intercept(struct kvm_vcpu *vcpu, int tpr, int irr)
2941{
2942 struct vcpu_svm *svm = to_svm(vcpu);
2943
Joerg Roedel88ab24a2010-02-19 16:23:06 +01002944 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
2945 return;
2946
Gleb Natapov95ba8273132009-04-21 17:45:08 +03002947 if (irr == -1)
2948 return;
2949
2950 if (tpr >= irr)
2951 svm->vmcb->control.intercept_cr_write |= INTERCEPT_CR8_MASK;
2952}
2953
2954static int svm_nmi_allowed(struct kvm_vcpu *vcpu)
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002955{
2956 struct vcpu_svm *svm = to_svm(vcpu);
2957 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel924584c2010-04-22 12:33:07 +02002958 int ret;
2959 ret = !(vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK) &&
2960 !(svm->vcpu.arch.hflags & HF_NMI_MASK);
2961 ret = ret && gif_set(svm) && nested_svm_nmi(svm);
2962
2963 return ret;
Joerg Roedelaaacfc92008-04-16 16:51:18 +02002964}
2965
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002966static bool svm_get_nmi_mask(struct kvm_vcpu *vcpu)
2967{
2968 struct vcpu_svm *svm = to_svm(vcpu);
2969
2970 return !!(svm->vcpu.arch.hflags & HF_NMI_MASK);
2971}
2972
2973static void svm_set_nmi_mask(struct kvm_vcpu *vcpu, bool masked)
2974{
2975 struct vcpu_svm *svm = to_svm(vcpu);
2976
2977 if (masked) {
2978 svm->vcpu.arch.hflags |= HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002979 svm->vmcb->control.intercept |= (1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002980 } else {
2981 svm->vcpu.arch.hflags &= ~HF_NMI_MASK;
Joerg Roedel061e2fd2010-05-05 16:04:43 +02002982 svm->vmcb->control.intercept &= ~(1ULL << INTERCEPT_IRET);
Jan Kiszka3cfc3092009-11-12 01:04:25 +01002983 }
2984}
2985
Gleb Natapov78646122009-03-23 12:12:11 +02002986static int svm_interrupt_allowed(struct kvm_vcpu *vcpu)
2987{
2988 struct vcpu_svm *svm = to_svm(vcpu);
2989 struct vmcb *vmcb = svm->vmcb;
Joerg Roedel7fcdb512009-09-16 15:24:15 +02002990 int ret;
2991
2992 if (!gif_set(svm) ||
2993 (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK))
2994 return 0;
2995
2996 ret = !!(vmcb->save.rflags & X86_EFLAGS_IF);
2997
2998 if (is_nested(svm))
2999 return ret && !(svm->vcpu.arch.hflags & HF_VINTR_MASK);
3000
3001 return ret;
Gleb Natapov78646122009-03-23 12:12:11 +02003002}
3003
Gleb Natapov9222be12009-04-23 17:14:37 +03003004static void enable_irq_window(struct kvm_vcpu *vcpu)
3005{
Alexander Graf219b65d2009-06-15 15:21:25 +02003006 struct vcpu_svm *svm = to_svm(vcpu);
Alexander Graf219b65d2009-06-15 15:21:25 +02003007
Joerg Roedele0231712010-02-24 18:59:10 +01003008 /*
3009 * In case GIF=0 we can't rely on the CPU to tell us when GIF becomes
3010 * 1, because that's a separate STGI/VMRUN intercept. The next time we
3011 * get that intercept, this function will be called again though and
3012 * we'll get the vintr intercept.
3013 */
Joerg Roedel8fe54652010-02-19 16:23:01 +01003014 if (gif_set(svm) && nested_svm_intr(svm)) {
Alexander Graf219b65d2009-06-15 15:21:25 +02003015 svm_set_vintr(svm);
3016 svm_inject_irq(svm, 0x0);
3017 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003018}
3019
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003020static void enable_nmi_window(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003021{
Avi Kivity04d2cc72007-09-10 18:10:54 +03003022 struct vcpu_svm *svm = to_svm(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003023
Gleb Natapov44c11432009-05-11 13:35:52 +03003024 if ((svm->vcpu.arch.hflags & (HF_NMI_MASK | HF_IRET_MASK))
3025 == HF_NMI_MASK)
3026 return; /* IRET will cause a vm exit */
3027
Joerg Roedele0231712010-02-24 18:59:10 +01003028 /*
3029 * Something prevents NMI from been injected. Single step over possible
3030 * problem (IRET or exception injection or interrupt shadow)
3031 */
Jan Kiszka6be7d302009-10-18 13:24:54 +02003032 svm->nmi_singlestep = true;
Gleb Natapov44c11432009-05-11 13:35:52 +03003033 svm->vmcb->save.rflags |= (X86_EFLAGS_TF | X86_EFLAGS_RF);
3034 update_db_intercept(vcpu);
Eddie Dong85f455f2007-07-06 12:20:49 +03003035}
3036
Izik Eiduscbc94022007-10-25 00:29:55 +02003037static int svm_set_tss_addr(struct kvm *kvm, unsigned int addr)
3038{
3039 return 0;
3040}
3041
Avi Kivityd9e368d2007-06-07 19:18:30 +03003042static void svm_flush_tlb(struct kvm_vcpu *vcpu)
3043{
3044 force_new_asid(vcpu);
3045}
3046
Avi Kivity04d2cc72007-09-10 18:10:54 +03003047static void svm_prepare_guest_switch(struct kvm_vcpu *vcpu)
3048{
3049}
3050
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003051static inline void sync_cr8_to_lapic(struct kvm_vcpu *vcpu)
3052{
3053 struct vcpu_svm *svm = to_svm(vcpu);
3054
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003055 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3056 return;
3057
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003058 if (!(svm->vmcb->control.intercept_cr_write & INTERCEPT_CR8_MASK)) {
3059 int cr8 = svm->vmcb->control.int_ctl & V_TPR_MASK;
Gleb Natapov615d5192009-04-21 17:45:05 +03003060 kvm_set_cr8(vcpu, cr8);
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003061 }
3062}
3063
Joerg Roedel649d6862008-04-16 16:51:15 +02003064static inline void sync_lapic_to_cr8(struct kvm_vcpu *vcpu)
3065{
3066 struct vcpu_svm *svm = to_svm(vcpu);
3067 u64 cr8;
3068
Joerg Roedel88ab24a2010-02-19 16:23:06 +01003069 if (is_nested(svm) && (vcpu->arch.hflags & HF_VINTR_MASK))
3070 return;
3071
Joerg Roedel649d6862008-04-16 16:51:15 +02003072 cr8 = kvm_get_cr8(vcpu);
3073 svm->vmcb->control.int_ctl &= ~V_TPR_MASK;
3074 svm->vmcb->control.int_ctl |= cr8 & V_TPR_MASK;
3075}
3076
Gleb Natapov9222be12009-04-23 17:14:37 +03003077static void svm_complete_interrupts(struct vcpu_svm *svm)
3078{
3079 u8 vector;
3080 int type;
3081 u32 exitintinfo = svm->vmcb->control.exit_int_info;
Jan Kiszka66b71382010-02-23 17:47:56 +01003082 unsigned int3_injected = svm->int3_injected;
3083
3084 svm->int3_injected = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003085
Gleb Natapov44c11432009-05-11 13:35:52 +03003086 if (svm->vcpu.arch.hflags & HF_IRET_MASK)
3087 svm->vcpu.arch.hflags &= ~(HF_NMI_MASK | HF_IRET_MASK);
3088
Gleb Natapov9222be12009-04-23 17:14:37 +03003089 svm->vcpu.arch.nmi_injected = false;
3090 kvm_clear_exception_queue(&svm->vcpu);
3091 kvm_clear_interrupt_queue(&svm->vcpu);
3092
3093 if (!(exitintinfo & SVM_EXITINTINFO_VALID))
3094 return;
3095
3096 vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
3097 type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
3098
3099 switch (type) {
3100 case SVM_EXITINTINFO_TYPE_NMI:
3101 svm->vcpu.arch.nmi_injected = true;
3102 break;
3103 case SVM_EXITINTINFO_TYPE_EXEPT:
Jan Kiszka66b71382010-02-23 17:47:56 +01003104 /*
3105 * In case of software exceptions, do not reinject the vector,
3106 * but re-execute the instruction instead. Rewind RIP first
3107 * if we emulated INT3 before.
3108 */
3109 if (kvm_exception_is_soft(vector)) {
3110 if (vector == BP_VECTOR && int3_injected &&
3111 kvm_is_linear_rip(&svm->vcpu, svm->int3_rip))
3112 kvm_rip_write(&svm->vcpu,
3113 kvm_rip_read(&svm->vcpu) -
3114 int3_injected);
Alexander Graf219b65d2009-06-15 15:21:25 +02003115 break;
Jan Kiszka66b71382010-02-23 17:47:56 +01003116 }
Gleb Natapov9222be12009-04-23 17:14:37 +03003117 if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
3118 u32 err = svm->vmcb->control.exit_int_info_err;
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003119 kvm_requeue_exception_e(&svm->vcpu, vector, err);
Gleb Natapov9222be12009-04-23 17:14:37 +03003120
3121 } else
Joerg Roedelce7ddec2010-04-22 12:33:13 +02003122 kvm_requeue_exception(&svm->vcpu, vector);
Gleb Natapov9222be12009-04-23 17:14:37 +03003123 break;
3124 case SVM_EXITINTINFO_TYPE_INTR:
Gleb Natapov66fd3f72009-05-11 13:35:50 +03003125 kvm_queue_interrupt(&svm->vcpu, vector, false);
Gleb Natapov9222be12009-04-23 17:14:37 +03003126 break;
3127 default:
3128 break;
3129 }
3130}
3131
Avi Kivity80e31d42008-07-14 14:44:59 +03003132#ifdef CONFIG_X86_64
3133#define R "r"
3134#else
3135#define R "e"
3136#endif
3137
Avi Kivity851ba692009-08-24 11:10:17 +03003138static void svm_vcpu_run(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003139{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003140 struct vcpu_svm *svm = to_svm(vcpu);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003141 u16 fs_selector;
3142 u16 gs_selector;
3143 u16 ldt_selector;
Avi Kivityd9e368d2007-06-07 19:18:30 +03003144
Joerg Roedel2041a062010-04-22 12:33:08 +02003145 svm->vmcb->save.rax = vcpu->arch.regs[VCPU_REGS_RAX];
3146 svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
3147 svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
3148
Joerg Roedelcd3ff652009-10-09 16:08:26 +02003149 /*
3150 * A vmexit emulation is required before the vcpu can be executed
3151 * again.
3152 */
3153 if (unlikely(svm->nested.exit_required))
3154 return;
3155
Rusty Russelle756fc62007-07-30 20:07:08 +10003156 pre_svm_run(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003157
Joerg Roedel649d6862008-04-16 16:51:15 +02003158 sync_lapic_to_cr8(vcpu);
3159
Avi Kivity6aa8b732006-12-10 02:21:36 -08003160 save_host_msrs(vcpu);
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003161 fs_selector = kvm_read_fs();
3162 gs_selector = kvm_read_gs();
3163 ldt_selector = kvm_read_ldt();
Joerg Roedelcda0ffd2009-08-07 11:49:45 +02003164 svm->vmcb->save.cr2 = vcpu->arch.cr2;
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003165 /* required for live migration with NPT */
3166 if (npt_enabled)
3167 svm->vmcb->save.cr3 = vcpu->arch.cr3;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003168
Avi Kivity04d2cc72007-09-10 18:10:54 +03003169 clgi();
3170
3171 local_irq_enable();
Avi Kivity36241b82006-12-22 01:05:20 -08003172
Avi Kivity6aa8b732006-12-10 02:21:36 -08003173 asm volatile (
Avi Kivity80e31d42008-07-14 14:44:59 +03003174 "push %%"R"bp; \n\t"
3175 "mov %c[rbx](%[svm]), %%"R"bx \n\t"
3176 "mov %c[rcx](%[svm]), %%"R"cx \n\t"
3177 "mov %c[rdx](%[svm]), %%"R"dx \n\t"
3178 "mov %c[rsi](%[svm]), %%"R"si \n\t"
3179 "mov %c[rdi](%[svm]), %%"R"di \n\t"
3180 "mov %c[rbp](%[svm]), %%"R"bp \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003181#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003182 "mov %c[r8](%[svm]), %%r8 \n\t"
3183 "mov %c[r9](%[svm]), %%r9 \n\t"
3184 "mov %c[r10](%[svm]), %%r10 \n\t"
3185 "mov %c[r11](%[svm]), %%r11 \n\t"
3186 "mov %c[r12](%[svm]), %%r12 \n\t"
3187 "mov %c[r13](%[svm]), %%r13 \n\t"
3188 "mov %c[r14](%[svm]), %%r14 \n\t"
3189 "mov %c[r15](%[svm]), %%r15 \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003190#endif
3191
Avi Kivity6aa8b732006-12-10 02:21:36 -08003192 /* Enter guest mode */
Avi Kivity80e31d42008-07-14 14:44:59 +03003193 "push %%"R"ax \n\t"
3194 "mov %c[vmcb](%[svm]), %%"R"ax \n\t"
Avi Kivity4ecac3f2008-05-13 13:23:38 +03003195 __ex(SVM_VMLOAD) "\n\t"
3196 __ex(SVM_VMRUN) "\n\t"
3197 __ex(SVM_VMSAVE) "\n\t"
Avi Kivity80e31d42008-07-14 14:44:59 +03003198 "pop %%"R"ax \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003199
3200 /* Save guest registers, load host registers */
Avi Kivity80e31d42008-07-14 14:44:59 +03003201 "mov %%"R"bx, %c[rbx](%[svm]) \n\t"
3202 "mov %%"R"cx, %c[rcx](%[svm]) \n\t"
3203 "mov %%"R"dx, %c[rdx](%[svm]) \n\t"
3204 "mov %%"R"si, %c[rsi](%[svm]) \n\t"
3205 "mov %%"R"di, %c[rdi](%[svm]) \n\t"
3206 "mov %%"R"bp, %c[rbp](%[svm]) \n\t"
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003207#ifdef CONFIG_X86_64
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003208 "mov %%r8, %c[r8](%[svm]) \n\t"
3209 "mov %%r9, %c[r9](%[svm]) \n\t"
3210 "mov %%r10, %c[r10](%[svm]) \n\t"
3211 "mov %%r11, %c[r11](%[svm]) \n\t"
3212 "mov %%r12, %c[r12](%[svm]) \n\t"
3213 "mov %%r13, %c[r13](%[svm]) \n\t"
3214 "mov %%r14, %c[r14](%[svm]) \n\t"
3215 "mov %%r15, %c[r15](%[svm]) \n\t"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003216#endif
Avi Kivity80e31d42008-07-14 14:44:59 +03003217 "pop %%"R"bp"
Avi Kivity6aa8b732006-12-10 02:21:36 -08003218 :
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003219 : [svm]"a"(svm),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003220 [vmcb]"i"(offsetof(struct vcpu_svm, vmcb_pa)),
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003221 [rbx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBX])),
3222 [rcx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RCX])),
3223 [rdx]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDX])),
3224 [rsi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RSI])),
3225 [rdi]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RDI])),
3226 [rbp]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_RBP]))
Avi Kivity05b3e0c2006-12-13 00:33:45 -08003227#ifdef CONFIG_X86_64
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003228 , [r8]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R8])),
3229 [r9]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R9])),
3230 [r10]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R10])),
3231 [r11]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R11])),
3232 [r12]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R12])),
3233 [r13]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R13])),
3234 [r14]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R14])),
3235 [r15]"i"(offsetof(struct vcpu_svm, vcpu.arch.regs[VCPU_REGS_R15]))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003236#endif
Laurent Vivier54a08c02007-10-25 14:18:53 +02003237 : "cc", "memory"
Avi Kivity80e31d42008-07-14 14:44:59 +03003238 , R"bx", R"cx", R"dx", R"si", R"di"
Laurent Vivier54a08c02007-10-25 14:18:53 +02003239#ifdef CONFIG_X86_64
Laurent Vivier54a08c02007-10-25 14:18:53 +02003240 , "r8", "r9", "r10", "r11" , "r12", "r13", "r14", "r15"
3241#endif
3242 );
Avi Kivity6aa8b732006-12-10 02:21:36 -08003243
Zhang Xiantaoad312c72007-12-13 23:50:52 +08003244 vcpu->arch.cr2 = svm->vmcb->save.cr2;
Marcelo Tosatti5fdbf972008-06-27 14:58:02 -03003245 vcpu->arch.regs[VCPU_REGS_RAX] = svm->vmcb->save.rax;
3246 vcpu->arch.regs[VCPU_REGS_RSP] = svm->vmcb->save.rsp;
3247 vcpu->arch.regs[VCPU_REGS_RIP] = svm->vmcb->save.rip;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003248
Avi Kivityd6e88ae2008-07-10 16:53:33 +03003249 kvm_load_fs(fs_selector);
3250 kvm_load_gs(gs_selector);
3251 kvm_load_ldt(ldt_selector);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 load_host_msrs(vcpu);
3253
3254 reload_tss(vcpu);
3255
Avi Kivity56ba47d2007-11-07 17:14:18 +02003256 local_irq_disable();
3257
3258 stgi();
3259
Joerg Roedeld7bf8222008-04-16 16:51:17 +02003260 sync_cr8_to_lapic(vcpu);
3261
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003262 svm->next_rip = 0;
Gleb Natapov9222be12009-04-23 17:14:37 +03003263
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003264 if (npt_enabled) {
3265 vcpu->arch.regs_avail &= ~(1 << VCPU_EXREG_PDPTR);
3266 vcpu->arch.regs_dirty &= ~(1 << VCPU_EXREG_PDPTR);
3267 }
Joerg Roedelfe5913e2010-05-17 14:43:34 +02003268
3269 /*
3270 * We need to handle MC intercepts here before the vcpu has a chance to
3271 * change the physical cpu
3272 */
3273 if (unlikely(svm->vmcb->control.exit_code ==
3274 SVM_EXIT_EXCP_BASE + MC_VECTOR))
3275 svm_handle_mce(svm);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003276}
3277
Avi Kivity80e31d42008-07-14 14:44:59 +03003278#undef R
3279
Avi Kivity6aa8b732006-12-10 02:21:36 -08003280static void svm_set_cr3(struct kvm_vcpu *vcpu, unsigned long root)
3281{
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003282 struct vcpu_svm *svm = to_svm(vcpu);
3283
Joerg Roedel709ddeb2008-02-07 13:47:45 +01003284 if (npt_enabled) {
3285 svm->vmcb->control.nested_cr3 = root;
3286 force_new_asid(vcpu);
3287 return;
3288 }
3289
Gregory Haskinsa2fa3e92007-07-27 08:13:10 -04003290 svm->vmcb->save.cr3 = root;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003291 force_new_asid(vcpu);
3292}
3293
Avi Kivity6aa8b732006-12-10 02:21:36 -08003294static int is_disabled(void)
3295{
Joerg Roedel6031a612007-06-22 12:29:50 +03003296 u64 vm_cr;
3297
3298 rdmsrl(MSR_VM_CR, vm_cr);
3299 if (vm_cr & (1 << SVM_VM_CR_SVM_DISABLE))
3300 return 1;
3301
Avi Kivity6aa8b732006-12-10 02:21:36 -08003302 return 0;
3303}
3304
Ingo Molnar102d8322007-02-19 14:37:47 +02003305static void
3306svm_patch_hypercall(struct kvm_vcpu *vcpu, unsigned char *hypercall)
3307{
3308 /*
3309 * Patch in the VMMCALL instruction:
3310 */
3311 hypercall[0] = 0x0f;
3312 hypercall[1] = 0x01;
3313 hypercall[2] = 0xd9;
Ingo Molnar102d8322007-02-19 14:37:47 +02003314}
3315
Yang, Sheng002c7f72007-07-31 14:23:01 +03003316static void svm_check_processor_compat(void *rtn)
3317{
3318 *(int *)rtn = 0;
3319}
3320
Avi Kivity774ead32007-12-26 13:57:04 +02003321static bool svm_cpu_has_accelerated_tpr(void)
3322{
3323 return false;
3324}
3325
Sheng Yang67253af2008-04-25 10:20:22 +08003326static int get_npt_level(void)
3327{
3328#ifdef CONFIG_X86_64
3329 return PT64_ROOT_LEVEL;
3330#else
3331 return PT32E_ROOT_LEVEL;
3332#endif
3333}
3334
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003335static u64 svm_get_mt_mask(struct kvm_vcpu *vcpu, gfn_t gfn, bool is_mmio)
Sheng Yang64d4d522008-10-09 16:01:57 +08003336{
3337 return 0;
3338}
3339
Sheng Yang0e851882009-12-18 16:48:46 +08003340static void svm_cpuid_update(struct kvm_vcpu *vcpu)
3341{
3342}
3343
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003344static void svm_set_supported_cpuid(u32 func, struct kvm_cpuid_entry2 *entry)
3345{
Joerg Roedelc2c63a42010-04-22 12:33:12 +02003346 switch (func) {
3347 case 0x8000000A:
3348 entry->eax = 1; /* SVM revision 1 */
3349 entry->ebx = 8; /* Lets support 8 ASIDs in case we add proper
3350 ASID emulation to nested SVM */
3351 entry->ecx = 0; /* Reserved */
3352 entry->edx = 0; /* Do not support any additional features */
3353
3354 break;
3355 }
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003356}
3357
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003358static const struct trace_print_flags svm_exit_reasons_str[] = {
Joerg Roedele0231712010-02-24 18:59:10 +01003359 { SVM_EXIT_READ_CR0, "read_cr0" },
3360 { SVM_EXIT_READ_CR3, "read_cr3" },
3361 { SVM_EXIT_READ_CR4, "read_cr4" },
3362 { SVM_EXIT_READ_CR8, "read_cr8" },
3363 { SVM_EXIT_WRITE_CR0, "write_cr0" },
3364 { SVM_EXIT_WRITE_CR3, "write_cr3" },
3365 { SVM_EXIT_WRITE_CR4, "write_cr4" },
3366 { SVM_EXIT_WRITE_CR8, "write_cr8" },
3367 { SVM_EXIT_READ_DR0, "read_dr0" },
3368 { SVM_EXIT_READ_DR1, "read_dr1" },
3369 { SVM_EXIT_READ_DR2, "read_dr2" },
3370 { SVM_EXIT_READ_DR3, "read_dr3" },
3371 { SVM_EXIT_WRITE_DR0, "write_dr0" },
3372 { SVM_EXIT_WRITE_DR1, "write_dr1" },
3373 { SVM_EXIT_WRITE_DR2, "write_dr2" },
3374 { SVM_EXIT_WRITE_DR3, "write_dr3" },
3375 { SVM_EXIT_WRITE_DR5, "write_dr5" },
3376 { SVM_EXIT_WRITE_DR7, "write_dr7" },
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003377 { SVM_EXIT_EXCP_BASE + DB_VECTOR, "DB excp" },
3378 { SVM_EXIT_EXCP_BASE + BP_VECTOR, "BP excp" },
3379 { SVM_EXIT_EXCP_BASE + UD_VECTOR, "UD excp" },
3380 { SVM_EXIT_EXCP_BASE + PF_VECTOR, "PF excp" },
3381 { SVM_EXIT_EXCP_BASE + NM_VECTOR, "NM excp" },
3382 { SVM_EXIT_EXCP_BASE + MC_VECTOR, "MC excp" },
3383 { SVM_EXIT_INTR, "interrupt" },
3384 { SVM_EXIT_NMI, "nmi" },
3385 { SVM_EXIT_SMI, "smi" },
3386 { SVM_EXIT_INIT, "init" },
3387 { SVM_EXIT_VINTR, "vintr" },
3388 { SVM_EXIT_CPUID, "cpuid" },
3389 { SVM_EXIT_INVD, "invd" },
3390 { SVM_EXIT_HLT, "hlt" },
3391 { SVM_EXIT_INVLPG, "invlpg" },
3392 { SVM_EXIT_INVLPGA, "invlpga" },
3393 { SVM_EXIT_IOIO, "io" },
3394 { SVM_EXIT_MSR, "msr" },
3395 { SVM_EXIT_TASK_SWITCH, "task_switch" },
3396 { SVM_EXIT_SHUTDOWN, "shutdown" },
3397 { SVM_EXIT_VMRUN, "vmrun" },
3398 { SVM_EXIT_VMMCALL, "hypercall" },
3399 { SVM_EXIT_VMLOAD, "vmload" },
3400 { SVM_EXIT_VMSAVE, "vmsave" },
3401 { SVM_EXIT_STGI, "stgi" },
3402 { SVM_EXIT_CLGI, "clgi" },
3403 { SVM_EXIT_SKINIT, "skinit" },
3404 { SVM_EXIT_WBINVD, "wbinvd" },
3405 { SVM_EXIT_MONITOR, "monitor" },
3406 { SVM_EXIT_MWAIT, "mwait" },
3407 { SVM_EXIT_NPF, "npf" },
3408 { -1, NULL }
3409};
3410
Sheng Yang17cc3932010-01-05 19:02:27 +08003411static int svm_get_lpage_level(void)
Joerg Roedel344f4142009-07-27 16:30:48 +02003412{
Sheng Yang17cc3932010-01-05 19:02:27 +08003413 return PT_PDPE_LEVEL;
Joerg Roedel344f4142009-07-27 16:30:48 +02003414}
3415
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003416static bool svm_rdtscp_supported(void)
3417{
3418 return false;
3419}
3420
Avi Kivity02daab22009-12-30 12:40:26 +02003421static void svm_fpu_deactivate(struct kvm_vcpu *vcpu)
3422{
3423 struct vcpu_svm *svm = to_svm(vcpu);
3424
Avi Kivity02daab22009-12-30 12:40:26 +02003425 svm->vmcb->control.intercept_exceptions |= 1 << NM_VECTOR;
Joerg Roedel66a562f2010-02-19 16:23:08 +01003426 if (is_nested(svm))
3427 svm->nested.hsave->control.intercept_exceptions |= 1 << NM_VECTOR;
3428 update_cr0_intercept(svm);
Avi Kivity02daab22009-12-30 12:40:26 +02003429}
3430
Christian Ehrhardtcbdd1be2007-09-09 15:41:59 +03003431static struct kvm_x86_ops svm_x86_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003432 .cpu_has_kvm_support = has_svm,
3433 .disabled_by_bios = is_disabled,
3434 .hardware_setup = svm_hardware_setup,
3435 .hardware_unsetup = svm_hardware_unsetup,
Yang, Sheng002c7f72007-07-31 14:23:01 +03003436 .check_processor_compatibility = svm_check_processor_compat,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003437 .hardware_enable = svm_hardware_enable,
3438 .hardware_disable = svm_hardware_disable,
Avi Kivity774ead32007-12-26 13:57:04 +02003439 .cpu_has_accelerated_tpr = svm_cpu_has_accelerated_tpr,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003440
3441 .vcpu_create = svm_create_vcpu,
3442 .vcpu_free = svm_free_vcpu,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003443 .vcpu_reset = svm_vcpu_reset,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003444
Avi Kivity04d2cc72007-09-10 18:10:54 +03003445 .prepare_guest_switch = svm_prepare_guest_switch,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003446 .vcpu_load = svm_vcpu_load,
3447 .vcpu_put = svm_vcpu_put,
3448
3449 .set_guest_debug = svm_guest_debug,
3450 .get_msr = svm_get_msr,
3451 .set_msr = svm_set_msr,
3452 .get_segment_base = svm_get_segment_base,
3453 .get_segment = svm_get_segment,
3454 .set_segment = svm_set_segment,
Izik Eidus2e4d2652008-03-24 19:38:34 +02003455 .get_cpl = svm_get_cpl,
Rusty Russell1747fb72007-09-06 01:21:32 +10003456 .get_cs_db_l_bits = kvm_get_cs_db_l_bits,
Avi Kivitye8467fd2009-12-29 18:43:06 +02003457 .decache_cr0_guest_bits = svm_decache_cr0_guest_bits,
Anthony Liguori25c4c272007-04-27 09:29:21 +03003458 .decache_cr4_guest_bits = svm_decache_cr4_guest_bits,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003459 .set_cr0 = svm_set_cr0,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003460 .set_cr3 = svm_set_cr3,
3461 .set_cr4 = svm_set_cr4,
3462 .set_efer = svm_set_efer,
3463 .get_idt = svm_get_idt,
3464 .set_idt = svm_set_idt,
3465 .get_gdt = svm_get_gdt,
3466 .set_gdt = svm_set_gdt,
Gleb Natapov020df072010-04-13 10:05:23 +03003467 .set_dr7 = svm_set_dr7,
Avi Kivity6de4f3a2009-05-31 22:58:47 +03003468 .cache_reg = svm_cache_reg,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003469 .get_rflags = svm_get_rflags,
3470 .set_rflags = svm_set_rflags,
Avi Kivity6b52d182010-01-21 15:31:47 +02003471 .fpu_activate = svm_fpu_activate,
Avi Kivity02daab22009-12-30 12:40:26 +02003472 .fpu_deactivate = svm_fpu_deactivate,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003473
Avi Kivity6aa8b732006-12-10 02:21:36 -08003474 .tlb_flush = svm_flush_tlb,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003475
Avi Kivity6aa8b732006-12-10 02:21:36 -08003476 .run = svm_vcpu_run,
Avi Kivity04d2cc72007-09-10 18:10:54 +03003477 .handle_exit = handle_exit,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003478 .skip_emulated_instruction = skip_emulated_instruction,
Glauber Costa2809f5d2009-05-12 16:21:05 -04003479 .set_interrupt_shadow = svm_set_interrupt_shadow,
3480 .get_interrupt_shadow = svm_get_interrupt_shadow,
Ingo Molnar102d8322007-02-19 14:37:47 +02003481 .patch_hypercall = svm_patch_hypercall,
Eddie Dong2a8067f2007-08-06 16:29:07 +03003482 .set_irq = svm_set_irq,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003483 .set_nmi = svm_inject_nmi,
Avi Kivity298101d2007-11-25 13:41:11 +02003484 .queue_exception = svm_queue_exception,
Gleb Natapov78646122009-03-23 12:12:11 +02003485 .interrupt_allowed = svm_interrupt_allowed,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003486 .nmi_allowed = svm_nmi_allowed,
Jan Kiszka3cfc3092009-11-12 01:04:25 +01003487 .get_nmi_mask = svm_get_nmi_mask,
3488 .set_nmi_mask = svm_set_nmi_mask,
Gleb Natapov95ba8273132009-04-21 17:45:08 +03003489 .enable_nmi_window = enable_nmi_window,
3490 .enable_irq_window = enable_irq_window,
3491 .update_cr8_intercept = update_cr8_intercept,
Izik Eiduscbc94022007-10-25 00:29:55 +02003492
3493 .set_tss_addr = svm_set_tss_addr,
Sheng Yang67253af2008-04-25 10:20:22 +08003494 .get_tdp_level = get_npt_level,
Sheng Yang4b12f0d2009-04-27 20:35:42 +08003495 .get_mt_mask = svm_get_mt_mask,
Marcelo Tosatti229456f2009-06-17 09:22:14 -03003496
3497 .exit_reasons_str = svm_exit_reasons_str,
Sheng Yang17cc3932010-01-05 19:02:27 +08003498 .get_lpage_level = svm_get_lpage_level,
Sheng Yang0e851882009-12-18 16:48:46 +08003499
3500 .cpuid_update = svm_cpuid_update,
Sheng Yang4e47c7a2009-12-18 16:48:47 +08003501
3502 .rdtscp_supported = svm_rdtscp_supported,
Joerg Roedeld4330ef2010-04-22 12:33:11 +02003503
3504 .set_supported_cpuid = svm_set_supported_cpuid,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003505};
3506
3507static int __init svm_init(void)
3508{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003509 return kvm_init(&svm_x86_ops, sizeof(struct vcpu_svm),
Avi Kivity0ee75be2010-04-28 15:39:01 +03003510 __alignof__(struct vcpu_svm), THIS_MODULE);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003511}
3512
3513static void __exit svm_exit(void)
3514{
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08003515 kvm_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08003516}
3517
3518module_init(svm_init)
3519module_exit(svm_exit)